blob: 049f095ddff9003728c7c233abbe15e6e5375655 [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 -0700139/* For now, this needs to be 1500, so that XP works with us */
140#define ETH1394_DATA_LEN ETH_DATA_LEN
141
142static const u16 eth1394_speedto_maxpayload[] = {
143/* S100, S200, S400, S800, S1600, S3200 */
144 512, 1024, 2048, 4096, 4096, 4096
145};
146
147MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
148MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
149MODULE_LICENSE("GPL");
150
Stefan Richterefbeccf2007-04-02 02:12:32 +0200151/*
152 * The max_partial_datagrams parameter is the maximum number of fragmented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * datagrams per node that eth1394 will keep in memory. Providing an upper
154 * bound allows us to limit the amount of memory that partial datagrams
155 * consume in the event that some partial datagrams are never completed.
156 */
157static int max_partial_datagrams = 25;
158module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR);
159MODULE_PARM_DESC(max_partial_datagrams,
160 "Maximum number of partially received fragmented datagrams "
161 "(default = 25).");
162
163
164static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
165 unsigned short type, void *daddr, void *saddr,
166 unsigned len);
167static int ether1394_rebuild_header(struct sk_buff *skb);
168static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr);
169static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh);
170static void ether1394_header_cache_update(struct hh_cache *hh,
171 struct net_device *dev,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200172 unsigned char *haddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int ether1394_mac_addr(struct net_device *dev, void *p);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int ether1394_tx(struct sk_buff *skb, struct net_device *dev);
176static void ether1394_iso(struct hpsb_iso *iso);
177
178static struct ethtool_ops ethtool_ops;
179
180static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
181 quadlet_t *data, u64 addr, size_t len, u16 flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200182static void ether1394_add_host(struct hpsb_host *host);
183static void ether1394_remove_host(struct hpsb_host *host);
184static void ether1394_host_reset(struct hpsb_host *host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* Function for incoming 1394 packets */
187static struct hpsb_address_ops addr_ops = {
188 .write = ether1394_write,
189};
190
191/* Ieee1394 highlevel driver functions */
192static struct hpsb_highlevel eth1394_highlevel = {
193 .name = driver_name,
194 .add_host = ether1394_add_host,
195 .remove_host = ether1394_remove_host,
196 .host_reset = ether1394_host_reset,
197};
198
Stefan Richter5009d262007-04-02 02:15:53 +0200199static int ether1394_recv_init(struct eth1394_priv *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200201 unsigned int iso_buf_size;
202
203 /* FIXME: rawiso limits us to PAGE_SIZE */
204 iso_buf_size = min((unsigned int)PAGE_SIZE,
205 2 * (1U << (priv->host->csr.max_rec + 1)));
Jean Delvare09d7a962007-04-01 10:06:33 +0200206
207 priv->iso = hpsb_iso_recv_init(priv->host,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200208 ETHER1394_GASP_BUFFERS * iso_buf_size,
Jean Delvare09d7a962007-04-01 10:06:33 +0200209 ETHER1394_GASP_BUFFERS,
210 priv->broadcast_channel,
211 HPSB_ISO_DMA_PACKET_PER_BUFFER,
212 1, ether1394_iso);
213 if (priv->iso == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200214 ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n");
Jean Delvare09d7a962007-04-01 10:06:33 +0200215 priv->bc_state = ETHER1394_BC_ERROR;
216 return -EAGAIN;
217 }
218
219 if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
220 priv->bc_state = ETHER1394_BC_STOPPED;
221 else
222 priv->bc_state = ETHER1394_BC_RUNNING;
223 return 0;
224}
225
226/* This is called after an "ifup" */
227static int ether1394_open(struct net_device *dev)
228{
229 struct eth1394_priv *priv = netdev_priv(dev);
230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (priv->bc_state == ETHER1394_BC_ERROR) {
Stefan Richter5009d262007-04-02 02:15:53 +0200233 ret = ether1394_recv_init(priv);
Jean Delvare09d7a962007-04-01 10:06:33 +0200234 if (ret)
235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Stefan Richterefbeccf2007-04-02 02:12:32 +0200237 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239}
240
241/* This is called after an "ifdown" */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200242static int ether1394_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200244 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
248/* Return statistics to the caller */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200249static struct net_device_stats *ether1394_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 return &(((struct eth1394_priv *)netdev_priv(dev))->stats);
252}
253
Stefan Richterefbeccf2007-04-02 02:12:32 +0200254/* FIXME: What to do if we timeout? I think a host reset is probably in order,
255 * so that's what we do. Should we increment the stat counters too? */
256static void ether1394_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200258 struct hpsb_host *host =
259 ((struct eth1394_priv *)netdev_priv(dev))->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Stefan Richterefbeccf2007-04-02 02:12:32 +0200261 ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host %s\n",
262 host->driver->name);
263 highlevel_host_reset(host);
264 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
268{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200269 int max_rec =
270 ((struct eth1394_priv *)netdev_priv(dev))->host->csr.max_rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Stefan Richterefbeccf2007-04-02 02:12:32 +0200272 if (new_mtu < 68 ||
273 new_mtu > ETH1394_DATA_LEN ||
274 new_mtu > (1 << (max_rec + 1)) - sizeof(union eth1394_hdr) -
275 ETHER1394_GASP_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dev->mtu = new_mtu;
279 return 0;
280}
281
282static void purge_partial_datagram(struct list_head *old)
283{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200284 struct partial_datagram *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct list_head *lh, *n;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200286 struct fragment_info *fi;
287
288 pd = list_entry(old, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 list_for_each_safe(lh, n, &pd->frag_info) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200291 fi = list_entry(lh, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 list_del(lh);
293 kfree(fi);
294 }
295 list_del(old);
296 kfree_skb(pd->skb);
297 kfree(pd);
298}
299
300/******************************************
301 * 1394 bus activity functions
302 ******************************************/
303
304static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl,
305 struct unit_directory *ud)
306{
307 struct eth1394_node_ref *node;
308
309 list_for_each_entry(node, inl, list)
310 if (node->ud == ud)
311 return node;
312
313 return NULL;
314}
315
316static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl,
317 u64 guid)
318{
319 struct eth1394_node_ref *node;
320
321 list_for_each_entry(node, inl, list)
322 if (node->ud->ne->guid == guid)
323 return node;
324
325 return NULL;
326}
327
328static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl,
329 nodeid_t nodeid)
330{
331 struct eth1394_node_ref *node;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200332
333 list_for_each_entry(node, inl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (node->ud->ne->nodeid == nodeid)
335 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return NULL;
338}
339
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200340static int eth1394_new_node(struct eth1394_host_info *hi,
341 struct unit_directory *ud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct eth1394_priv *priv;
344 struct eth1394_node_ref *new_node;
345 struct eth1394_node_info *node_info;
346
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200347 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (!new_node)
349 return -ENOMEM;
350
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200351 node_info = kmalloc(sizeof(*node_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!node_info) {
353 kfree(new_node);
354 return -ENOMEM;
355 }
356
357 spin_lock_init(&node_info->pdg.lock);
358 INIT_LIST_HEAD(&node_info->pdg.list);
359 node_info->pdg.sz = 0;
Ben Collins67372312006-06-12 18:15:31 -0400360 node_info->fifo = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 ud->device.driver_data = node_info;
363 new_node->ud = ud;
364
365 priv = netdev_priv(hi->dev);
366 list_add_tail(&new_node->list, &priv->ip_node_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200370static int eth1394_probe(struct device *dev)
371{
372 struct unit_directory *ud;
373 struct eth1394_host_info *hi;
374
375 ud = container_of(dev, struct unit_directory, device);
376 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
377 if (!hi)
378 return -ENOENT;
379
380 return eth1394_new_node(hi, ud);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int eth1394_remove(struct device *dev)
384{
385 struct unit_directory *ud;
386 struct eth1394_host_info *hi;
387 struct eth1394_priv *priv;
388 struct eth1394_node_ref *old_node;
389 struct eth1394_node_info *node_info;
390 struct list_head *lh, *n;
391 unsigned long flags;
392
393 ud = container_of(dev, struct unit_directory, device);
394 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
395 if (!hi)
396 return -ENOENT;
397
398 priv = netdev_priv(hi->dev);
399
400 old_node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200401 if (!old_node)
402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Stefan Richterefbeccf2007-04-02 02:12:32 +0200404 list_del(&old_node->list);
405 kfree(old_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Stefan Richterefbeccf2007-04-02 02:12:32 +0200407 node_info = (struct eth1394_node_info*)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stefan Richterefbeccf2007-04-02 02:12:32 +0200409 spin_lock_irqsave(&node_info->pdg.lock, flags);
410 /* The partial datagram list should be empty, but we'll just
411 * make sure anyway... */
412 list_for_each_safe(lh, n, &node_info->pdg.list)
413 purge_partial_datagram(lh);
414 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Stefan Richterefbeccf2007-04-02 02:12:32 +0200416 kfree(node_info);
417 ud->device.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419}
420
421static int eth1394_update(struct unit_directory *ud)
422{
423 struct eth1394_host_info *hi;
424 struct eth1394_priv *priv;
425 struct eth1394_node_ref *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
428 if (!hi)
429 return -ENOENT;
430
431 priv = netdev_priv(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200433 if (node)
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200436 return eth1394_new_node(hi, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static struct ieee1394_device_id eth1394_id_table[] = {
440 {
441 .match_flags = (IEEE1394_MATCH_SPECIFIER_ID |
442 IEEE1394_MATCH_VERSION),
443 .specifier_id = ETHER1394_GASP_SPECIFIER_ID,
444 .version = ETHER1394_GASP_VERSION,
445 },
446 {}
447};
448
449MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table);
450
451static struct hpsb_protocol_driver eth1394_proto_driver = {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200452 .name = driver_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 .id_table = eth1394_id_table,
454 .update = eth1394_update,
455 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .probe = eth1394_probe,
457 .remove = eth1394_remove,
458 },
459};
460
Stefan Richterefbeccf2007-04-02 02:12:32 +0200461static void ether1394_reset_priv(struct net_device *dev, int set_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 unsigned long flags;
464 int i;
465 struct eth1394_priv *priv = netdev_priv(dev);
466 struct hpsb_host *host = priv->host;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200467 u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int max_speed = IEEE1394_SPEED_MAX;
469
Stefan Richterefbeccf2007-04-02 02:12:32 +0200470 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Stefan Richter027611b2007-04-02 02:15:21 +0200472 memset(priv->ud_list, 0, sizeof(priv->ud_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 priv->bc_maxpayload = 512;
474
475 /* Determine speed limit */
476 for (i = 0; i < host->node_count; i++)
Ben Collins647dcb52006-06-12 18:12:37 -0400477 if (max_speed > host->speed[i])
478 max_speed = host->speed[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 priv->bc_sspd = max_speed;
480
Stefan Richterefbeccf2007-04-02 02:12:32 +0200481 /* We'll use our maximum payload as the default MTU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (set_mtu) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200483 int max_payload = 1 << (host->csr.max_rec + 1);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dev->mtu = min(ETH1394_DATA_LEN,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200486 (int)(max_payload - sizeof(union eth1394_hdr) -
487 ETHER1394_GASP_OVERHEAD));
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;
513 dev->set_mac_address = ether1394_mac_addr;
514 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;
712 struct net_device *dev = skb->dev;
713
714 switch (eth->h_proto) {
715
716#ifdef CONFIG_INET
717 case __constant_htons(ETH_P_IP):
Stefan Richterefbeccf2007-04-02 02:12:32 +0200718 return arp_find((unsigned char *)&eth->h_dest, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#endif
720 default:
721 ETH1394_PRINT(KERN_DEBUG, dev->name,
722 "unable to resolve type %04x addresses.\n",
Ben Collins7136b802006-06-12 18:16:25 -0400723 ntohs(eth->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 }
726
727 return 0;
728}
729
730static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr)
731{
732 struct net_device *dev = skb->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
735 return ETH1394_ALEN;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
739{
740 unsigned short type = hh->hh_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct net_device *dev = neigh->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200742 struct eth1394hdr *eth =
743 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Ben Collins7136b802006-06-12 18:16:25 -0400745 if (type == htons(ETH_P_802_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 eth->h_proto = type;
749 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
750
751 hh->hh_len = ETH1394_HLEN;
752 return 0;
753}
754
755/* Called by Address Resolution module to notify changes in address. */
756static void ether1394_header_cache_update(struct hh_cache *hh,
757 struct net_device *dev,
758 unsigned char * haddr)
759{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200760 memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763static int ether1394_mac_addr(struct net_device *dev, void *p)
764{
765 if (netif_running(dev))
766 return -EBUSY;
767
768 /* Not going to allow setting the MAC address, we really need to use
769 * the real one supplied by the hardware */
770 return -EINVAL;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200771}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773/******************************************
774 * Datagram reception code
775 ******************************************/
776
777/* Copied from net/ethernet/eth.c */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100778static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct eth1394hdr *eth;
781 unsigned char *rawp;
782
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700783 skb_reset_mac_header(skb);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200784 skb_pull(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 eth = eth1394_hdr(skb);
786
787 if (*eth->h_dest & 1) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200788 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 skb->pkt_type = PACKET_BROADCAST;
790#if 0
791 else
792 skb->pkt_type = PACKET_MULTICAST;
793#endif
794 } else {
795 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
796 skb->pkt_type = PACKET_OTHERHOST;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Stefan Richterefbeccf2007-04-02 02:12:32 +0200799 if (ntohs(eth->h_proto) >= 1536)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return eth->h_proto;
801
802 rawp = skb->data;
803
Stefan Richterefbeccf2007-04-02 02:12:32 +0200804 if (*(unsigned short *)rawp == 0xFFFF)
805 return htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Stefan Richterefbeccf2007-04-02 02:12:32 +0200807 return htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/* Parse an encapsulated IP1394 header into an ethernet frame packet.
811 * We also perform ARP translation here, if need be. */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100812static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev,
813 nodeid_t srcid, nodeid_t destid,
814 u16 ether_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct eth1394_priv *priv = netdev_priv(dev);
817 u64 dest_hw;
818 unsigned short ret = 0;
819
Stefan Richterefbeccf2007-04-02 02:12:32 +0200820 /* Setup our hw addresses. We use these to build the ethernet header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (destid == (LOCAL_BUS | ALL_NODES))
822 dest_hw = ~0ULL; /* broadcast */
823 else
Stefan Richterefbeccf2007-04-02 02:12:32 +0200824 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 priv->host->csr.guid_lo);
826
827 /* If this is an ARP packet, convert it. First, we want to make
828 * use of some of the fields, since they tell us a little bit
829 * about the sending machine. */
Ben Collins7136b802006-06-12 18:16:25 -0400830 if (ether_type == htons(ETH_P_ARP)) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200831 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct arphdr *arp = (struct arphdr *)skb->data;
833 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
834 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
Stefan Richterefbeccf2007-04-02 02:12:32 +0200835 ntohl(arp1394->fifo_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 u8 max_rec = min(priv->host->csr.max_rec,
837 (u8)(arp1394->max_rec));
838 int sspd = arp1394->sspd;
839 u16 maxpayload;
840 struct eth1394_node_ref *node;
841 struct eth1394_node_info *node_info;
David S. Millerc20e3942006-10-29 16:14:55 -0800842 __be64 guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Sanity check. MacOSX seems to be sending us 131 in this
845 * field (atleast on my Panther G5). Not sure why. */
846 if (sspd > 5 || sspd < 0)
847 sspd = 0;
848
Stefan Richterefbeccf2007-04-02 02:12:32 +0200849 maxpayload = min(eth1394_speedto_maxpayload[sspd],
850 (u16)(1 << (max_rec + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
David S. Millerc20e3942006-10-29 16:14:55 -0800852 guid = get_unaligned(&arp1394->s_uniq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -0800854 be64_to_cpu(guid));
Stefan Richterefbeccf2007-04-02 02:12:32 +0200855 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Stefan Richterefbeccf2007-04-02 02:12:32 +0200858 node_info =
859 (struct eth1394_node_info *)node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* Update our speed/payload/fifo_offset table */
862 node_info->maxpayload = maxpayload;
863 node_info->sspd = sspd;
864 node_info->fifo = fifo_addr;
865
866 /* Now that we're done with the 1394 specific stuff, we'll
867 * need to alter some of the data. Believe it or not, all
868 * that needs to be done is sender_IP_address needs to be
869 * moved, the destination hardware address get stuffed
870 * in and the hardware address length set to 8.
871 *
872 * IMPORTANT: The code below overwrites 1394 specific data
873 * needed above so keep the munging of the data for the
874 * higher level IP stack last. */
875
876 arp->ar_hln = 8;
877 arp_ptr += arp->ar_hln; /* skip over sender unique id */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200878 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 arp_ptr += arp->ar_pln; /* skip over sender IP addr */
880
Ben Collins02f42132006-06-12 18:15:11 -0400881 if (arp->ar_op == htons(ARPOP_REQUEST))
David S. Millerc20e3942006-10-29 16:14:55 -0800882 memset(arp_ptr, 0, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 else
David S. Millerc20e3942006-10-29 16:14:55 -0800884 memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
887 /* Now add the ethernet header. */
Ben Collins7136b802006-06-12 18:16:25 -0400888 if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
889 skb->len) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ret = ether1394_type_trans(skb, dev);
891
892 return ret;
893}
894
Stefan Richtere00f04a2007-03-18 12:23:11 +0100895static int fragment_overlap(struct list_head *frag_list, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct fragment_info *fi;
898
899 list_for_each_entry(fi, frag_list, list) {
900 if ( ! ((offset > (fi->offset + fi->len - 1)) ||
901 ((offset + len - 1) < fi->offset)))
902 return 1;
903 }
904 return 0;
905}
906
Stefan Richtere00f04a2007-03-18 12:23:11 +0100907static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct partial_datagram *pd;
910
Stefan Richterefbeccf2007-04-02 02:12:32 +0200911 list_for_each_entry(pd, pdgl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (pd->dgl == dgl)
913 return &pd->list;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return NULL;
916}
917
918/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100919static int new_fragment(struct list_head *frag_info, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct list_head *lh;
922 struct fragment_info *fi, *fi2, *new;
923
924 list_for_each(lh, frag_info) {
925 fi = list_entry(lh, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200926 if (fi->offset + fi->len == offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* The new fragment can be tacked on to the end */
928 fi->len += len;
929 /* Did the new fragment plug a hole? */
930 fi2 = list_entry(lh->next, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200931 if (fi->offset + fi->len == fi2->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* glue fragments together */
933 fi->len += fi2->len;
934 list_del(lh->next);
935 kfree(fi2);
936 }
937 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200938 } else if (offset + len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* The new fragment can be tacked on to the beginning */
940 fi->offset = offset;
941 fi->len += len;
942 /* Did the new fragment plug a hole? */
943 fi2 = list_entry(lh->prev, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200944 if (fi2->offset + fi2->len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* glue fragments together */
946 fi2->len += fi->len;
947 list_del(lh);
948 kfree(fi);
949 }
950 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200951 } else if (offset > fi->offset + fi->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200953 } else if (offset + len < fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 lh = lh->prev;
955 break;
956 }
957 }
958
Stefan Richter85511582005-11-07 06:31:45 -0500959 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!new)
961 return -ENOMEM;
962
963 new->offset = offset;
964 new->len = len;
965
966 list_add(&new->list, lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
Stefan Richtere00f04a2007-03-18 12:23:11 +0100970static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl,
971 int dgl, int dg_size, char *frag_buf,
972 int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct partial_datagram *new;
975
Stefan Richter85511582005-11-07 06:31:45 -0500976 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!new)
978 return -ENOMEM;
979
980 INIT_LIST_HEAD(&new->frag_info);
981
982 if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
983 kfree(new);
984 return -ENOMEM;
985 }
986
987 new->dgl = dgl;
988 new->dg_size = dg_size;
989
990 new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
991 if (!new->skb) {
992 struct fragment_info *fi = list_entry(new->frag_info.next,
993 struct fragment_info,
994 list);
995 kfree(fi);
996 kfree(new);
997 return -ENOMEM;
998 }
999
1000 skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
1001 new->pbuf = skb_put(new->skb, dg_size);
1002 memcpy(new->pbuf + frag_off, frag_buf, frag_len);
1003
1004 list_add(&new->list, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006}
1007
Stefan Richtere00f04a2007-03-18 12:23:11 +01001008static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
1009 char *frag_buf, int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001011 struct partial_datagram *pd =
1012 list_entry(lh, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Stefan Richterefbeccf2007-04-02 02:12:32 +02001014 if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
1018
1019 /* Move list entry to beginnig of list so that oldest partial
1020 * datagrams percolate to the end of the list */
Akinobu Mita179e0912006-06-26 00:24:41 -07001021 list_move(lh, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
Stefan Richtere00f04a2007-03-18 12:23:11 +01001025static int is_datagram_complete(struct list_head *lh, int dg_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Stefan Richtere00f04a2007-03-18 12:23:11 +01001027 struct partial_datagram *pd;
1028 struct fragment_info *fi;
1029
1030 pd = list_entry(lh, struct partial_datagram, list);
1031 fi = list_entry(pd->frag_info.next, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 return (fi->len == dg_size);
1034}
1035
1036/* Packet reception. We convert the IP1394 encapsulation header to an
1037 * ethernet header, and fill it with some of our other fields. This is
1038 * an incoming packet from the 1394 bus. */
1039static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
1040 char *buf, int len)
1041{
1042 struct sk_buff *skb;
1043 unsigned long flags;
1044 struct eth1394_priv *priv = netdev_priv(dev);
1045 union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
1046 u16 ether_type = 0; /* initialized to clear warning */
1047 int hdr_len;
1048 struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
1049 struct eth1394_node_info *node_info;
1050
1051 if (!ud) {
1052 struct eth1394_node_ref *node;
1053 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
1054 if (!node) {
1055 HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
1056 "lookup failure: " NODE_BUS_FMT,
1057 NODE_BUS_ARGS(priv->host, srcid));
1058 priv->stats.rx_dropped++;
1059 return -1;
1060 }
1061 ud = node->ud;
1062
1063 priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
1064 }
1065
Stefan Richterefbeccf2007-04-02 02:12:32 +02001066 node_info = (struct eth1394_node_info *)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* First, did we receive a fragmented or unfragmented datagram? */
1069 hdr->words.word1 = ntohs(hdr->words.word1);
1070
1071 hdr_len = hdr_type_len[hdr->common.lf];
1072
1073 if (hdr->common.lf == ETH1394_HDR_LF_UF) {
1074 /* An unfragmented datagram has been received by the ieee1394
1075 * bus. Build an skbuff around it so we can pass it to the
1076 * high level network layer. */
1077
1078 skb = dev_alloc_skb(len + dev->hard_header_len + 15);
1079 if (!skb) {
Stefan Richter5009d262007-04-02 02:15:53 +02001080 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 priv->stats.rx_dropped++;
1082 return -1;
1083 }
1084 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001085 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
1086 len - hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ether_type = hdr->uf.ether_type;
1088 } else {
1089 /* A datagram fragment has been received, now the fun begins. */
1090
1091 struct list_head *pdgl, *lh;
1092 struct partial_datagram *pd;
1093 int fg_off;
1094 int fg_len = len - hdr_len;
1095 int dg_size;
1096 int dgl;
1097 int retval;
1098 struct pdg_list *pdg = &(node_info->pdg);
1099
1100 hdr->words.word3 = ntohs(hdr->words.word3);
1101 /* The 4th header word is reserved so no need to do ntohs() */
1102
1103 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1104 ether_type = hdr->ff.ether_type;
1105 dgl = hdr->ff.dgl;
1106 dg_size = hdr->ff.dg_size + 1;
1107 fg_off = 0;
1108 } else {
1109 hdr->words.word2 = ntohs(hdr->words.word2);
1110 dgl = hdr->sf.dgl;
1111 dg_size = hdr->sf.dg_size + 1;
1112 fg_off = hdr->sf.fg_off;
1113 }
1114 spin_lock_irqsave(&pdg->lock, flags);
1115
1116 pdgl = &(pdg->list);
1117 lh = find_partial_datagram(pdgl, dgl);
1118
1119 if (lh == NULL) {
1120 while (pdg->sz >= max_partial_datagrams) {
1121 /* remove the oldest */
1122 purge_partial_datagram(pdgl->prev);
1123 pdg->sz--;
1124 }
1125
1126 retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
1127 buf + hdr_len, fg_off,
1128 fg_len);
1129 if (retval < 0) {
1130 spin_unlock_irqrestore(&pdg->lock, flags);
1131 goto bad_proto;
1132 }
1133 pdg->sz++;
1134 lh = find_partial_datagram(pdgl, dgl);
1135 } else {
1136 struct partial_datagram *pd;
1137
1138 pd = list_entry(lh, struct partial_datagram, list);
1139
1140 if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
1141 /* Overlapping fragments, obliterate old
1142 * datagram and start new one. */
1143 purge_partial_datagram(lh);
1144 retval = new_partial_datagram(dev, pdgl, dgl,
1145 dg_size,
1146 buf + hdr_len,
1147 fg_off, fg_len);
1148 if (retval < 0) {
1149 pdg->sz--;
1150 spin_unlock_irqrestore(&pdg->lock, flags);
1151 goto bad_proto;
1152 }
1153 } else {
1154 retval = update_partial_datagram(pdgl, lh,
1155 buf + hdr_len,
1156 fg_off, fg_len);
1157 if (retval < 0) {
1158 /* Couldn't save off fragment anyway
1159 * so might as well obliterate the
1160 * datagram now. */
1161 purge_partial_datagram(lh);
1162 pdg->sz--;
1163 spin_unlock_irqrestore(&pdg->lock, flags);
1164 goto bad_proto;
1165 }
1166 } /* fragment overlap */
1167 } /* new datagram or add to existing one */
1168
1169 pd = list_entry(lh, struct partial_datagram, list);
1170
Stefan Richterefbeccf2007-04-02 02:12:32 +02001171 if (hdr->common.lf == ETH1394_HDR_LF_FF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 pd->ether_type = ether_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (is_datagram_complete(lh, dg_size)) {
1175 ether_type = pd->ether_type;
1176 pdg->sz--;
1177 skb = skb_get(pd->skb);
1178 purge_partial_datagram(lh);
1179 spin_unlock_irqrestore(&pdg->lock, flags);
1180 } else {
1181 /* Datagram is not complete, we're done for the
1182 * moment. */
1183 spin_unlock_irqrestore(&pdg->lock, flags);
1184 return 0;
1185 }
1186 } /* unframgented datagram or fragmented one */
1187
1188 /* Write metadata, and then pass to the receive level */
1189 skb->dev = dev;
1190 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
1191
1192 /* Parse the encapsulation header. This actually does the job of
1193 * converting to an ethernet frame header, aswell as arp
1194 * conversion if needed. ARP conversion is easier in this
1195 * direction, since we are using ethernet as our backend. */
1196 skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
1197 ether_type);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 spin_lock_irqsave(&priv->lock, flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!skb->protocol) {
1202 priv->stats.rx_errors++;
1203 priv->stats.rx_dropped++;
1204 dev_kfree_skb_any(skb);
1205 goto bad_proto;
1206 }
1207
1208 if (netif_rx(skb) == NET_RX_DROP) {
1209 priv->stats.rx_errors++;
1210 priv->stats.rx_dropped++;
1211 goto bad_proto;
1212 }
1213
1214 /* Statistics */
1215 priv->stats.rx_packets++;
1216 priv->stats.rx_bytes += skb->len;
1217
1218bad_proto:
1219 if (netif_queue_stopped(dev))
1220 netif_wake_queue(dev);
1221 spin_unlock_irqrestore(&priv->lock, flags);
1222
1223 dev->last_rx = jiffies;
1224
1225 return 0;
1226}
1227
1228static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
1229 quadlet_t *data, u64 addr, size_t len, u16 flags)
1230{
1231 struct eth1394_host_info *hi;
1232
1233 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
1234 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +02001235 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1236 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return RCODE_ADDRESS_ERROR;
1238 }
1239
1240 if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
1241 return RCODE_ADDRESS_ERROR;
1242 else
1243 return RCODE_COMPLETE;
1244}
1245
1246static void ether1394_iso(struct hpsb_iso *iso)
1247{
1248 quadlet_t *data;
1249 char *buf;
1250 struct eth1394_host_info *hi;
1251 struct net_device *dev;
1252 struct eth1394_priv *priv;
1253 unsigned int len;
1254 u32 specifier_id;
1255 u16 source_id;
1256 int i;
1257 int nready;
1258
1259 hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
1260 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +02001261 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1262 iso->host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
1264 }
1265
1266 dev = hi->dev;
1267
1268 nready = hpsb_iso_n_ready(iso);
1269 for (i = 0; i < nready; i++) {
1270 struct hpsb_iso_packet_info *info =
1271 &iso->infos[(iso->first_packet + i) % iso->buf_packets];
Stefan Richterefbeccf2007-04-02 02:12:32 +02001272 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* skip over GASP header */
1275 buf = (char *)data + 8;
1276 len = info->len - 8;
1277
Stefan Richterefbeccf2007-04-02 02:12:32 +02001278 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
1279 (be32_to_cpu(data[1]) & 0xff000000) >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 source_id = be32_to_cpu(data[0]) >> 16;
1281
1282 priv = netdev_priv(dev);
1283
Stefan Richterefbeccf2007-04-02 02:12:32 +02001284 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
1285 || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* This packet is not for us */
1287 continue;
1288 }
1289 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
1290 buf, len);
1291 }
1292
1293 hpsb_iso_recv_release_packets(iso, i);
1294
1295 dev->last_rx = jiffies;
1296}
1297
1298/******************************************
1299 * Datagram transmission code
1300 ******************************************/
1301
1302/* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
1303 * arphdr) is the same format as the ip1394 header, so they overlap. The rest
1304 * needs to be munged a bit. The remainder of the arphdr is formatted based
1305 * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to
1306 * judge.
1307 *
1308 * Now that the EUI is used for the hardware address all we need to do to make
1309 * this work for 1394 is to insert 2 quadlets that contain max_rec size,
1310 * speed, and unicast FIFO address information between the sender_unique_id
1311 * and the IP addresses.
1312 */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001313static void ether1394_arp_to_1394arp(struct sk_buff *skb,
1314 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct eth1394_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct arphdr *arp = (struct arphdr *)skb->data;
1318 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1319 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 arp1394->hw_addr_len = 16;
1322 arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN);
1323 arp1394->max_rec = priv->host->csr.max_rec;
1324 arp1394->sspd = priv->host->csr.lnk_spd;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001325 arp1394->fifo_hi = htons(priv->local_fifo >> 32);
1326 arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329/* We need to encapsulate the standard header with our own. We use the
1330 * ethernet header's proto for our own. */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001331static unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
1332 __be16 proto,
1333 union eth1394_hdr *hdr,
1334 u16 dg_size, u16 dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001336 unsigned int adj_max_payload =
1337 max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* Does it all fit in one packet? */
1340 if (dg_size <= adj_max_payload) {
1341 hdr->uf.lf = ETH1394_HDR_LF_UF;
1342 hdr->uf.ether_type = proto;
1343 } else {
1344 hdr->ff.lf = ETH1394_HDR_LF_FF;
1345 hdr->ff.ether_type = proto;
1346 hdr->ff.dg_size = dg_size - 1;
1347 hdr->ff.dgl = dgl;
1348 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
1349 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001350 return (dg_size + adj_max_payload - 1) / adj_max_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Stefan Richtere00f04a2007-03-18 12:23:11 +01001353static unsigned int ether1394_encapsulate(struct sk_buff *skb,
1354 unsigned int max_payload,
1355 union eth1394_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 union eth1394_hdr *bufhdr;
1358 int ftype = hdr->common.lf;
1359 int hdrsz = hdr_type_len[ftype];
1360 unsigned int adj_max_payload = max_payload - hdrsz;
1361
Stefan Richterefbeccf2007-04-02 02:12:32 +02001362 switch (ftype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case ETH1394_HDR_LF_UF:
1364 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1365 bufhdr->words.word1 = htons(hdr->words.word1);
1366 bufhdr->words.word2 = hdr->words.word2;
1367 break;
1368
1369 case ETH1394_HDR_LF_FF:
1370 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1371 bufhdr->words.word1 = htons(hdr->words.word1);
1372 bufhdr->words.word2 = hdr->words.word2;
1373 bufhdr->words.word3 = htons(hdr->words.word3);
1374 bufhdr->words.word4 = 0;
1375
1376 /* Set frag type here for future interior fragments */
1377 hdr->common.lf = ETH1394_HDR_LF_IF;
1378 hdr->sf.fg_off = 0;
1379 break;
1380
1381 default:
1382 hdr->sf.fg_off += adj_max_payload;
1383 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
1384 if (max_payload >= skb->len)
1385 hdr->common.lf = ETH1394_HDR_LF_LF;
1386 bufhdr->words.word1 = htons(hdr->words.word1);
1387 bufhdr->words.word2 = htons(hdr->words.word2);
1388 bufhdr->words.word3 = htons(hdr->words.word3);
1389 bufhdr->words.word4 = 0;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return min(max_payload, skb->len);
1392}
1393
Stefan Richtere00f04a2007-03-18 12:23:11 +01001394static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct hpsb_packet *p;
1397
1398 p = hpsb_alloc_packet(0);
1399 if (p) {
1400 p->host = host;
1401 p->generation = get_hpsb_generation(host);
1402 p->type = hpsb_async;
1403 }
1404 return p;
1405}
1406
Stefan Richtere00f04a2007-03-18 12:23:11 +01001407static int ether1394_prep_write_packet(struct hpsb_packet *p,
1408 struct hpsb_host *host, nodeid_t node,
Stefan Richterefbeccf2007-04-02 02:12:32 +02001409 u64 addr, void *data, int tx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 p->node_id = node;
1412 p->data = NULL;
1413
1414 p->tcode = TCODE_WRITEB;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001415 p->header[1] = host->node_id << 16 | addr >> 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 p->header[2] = addr & 0xffffffff;
1417
1418 p->header_size = 16;
1419 p->expect_response = 1;
1420
1421 if (hpsb_get_tlabel(p)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001422 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -1;
1424 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001425 p->header[0] =
1426 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 p->header[3] = tx_len << 16;
1429 p->data_size = (tx_len + 3) & ~3;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001430 p->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 return 0;
1433}
1434
Stefan Richtere00f04a2007-03-18 12:23:11 +01001435static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1436 struct eth1394_priv *priv,
1437 struct sk_buff *skb, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 p->header_size = 4;
1440 p->tcode = TCODE_STREAM_DATA;
1441
Stefan Richterefbeccf2007-04-02 02:12:32 +02001442 p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
1443 TCODE_STREAM_DATA << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 p->data_size = length;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001445 p->data = (quadlet_t *)skb->data - 2;
1446 p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 ETHER1394_GASP_SPECIFIER_ID_HI);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001448 p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
Ben Collins7136b802006-06-12 18:16:25 -04001449 ETHER1394_GASP_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /* Setting the node id to ALL_NODES (not LOCAL_BUS | ALL_NODES)
1452 * prevents hpsb_send_packet() from setting the speed to an arbitrary
1453 * value based on packet->node_id if packet->node_id is not set. */
1454 p->node_id = ALL_NODES;
1455 p->speed_code = priv->bc_sspd;
1456}
1457
Stefan Richtere00f04a2007-03-18 12:23:11 +01001458static void ether1394_free_packet(struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 if (packet->tcode != TCODE_STREAM_DATA)
1461 hpsb_free_tlabel(packet);
1462 hpsb_free_packet(packet);
1463}
1464
1465static void ether1394_complete_cb(void *__ptask);
1466
1467static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
1468{
1469 struct eth1394_priv *priv = ptask->priv;
1470 struct hpsb_packet *packet = NULL;
1471
1472 packet = ether1394_alloc_common_packet(priv->host);
1473 if (!packet)
1474 return -1;
1475
1476 if (ptask->tx_type == ETH1394_GASP) {
Stefan Richterefbeccf2007-04-02 02:12:32 +02001477 int length = tx_len + 2 * sizeof(quadlet_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
1480 } else if (ether1394_prep_write_packet(packet, priv->host,
1481 ptask->dest_node,
1482 ptask->addr, ptask->skb->data,
1483 tx_len)) {
1484 hpsb_free_packet(packet);
1485 return -1;
1486 }
1487
1488 ptask->packet = packet;
1489 hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
1490 ptask);
1491
1492 if (hpsb_send_packet(packet) < 0) {
1493 ether1394_free_packet(packet);
1494 return -1;
1495 }
1496
1497 return 0;
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/* Task function to be run when a datagram transmission is completed */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001501static void ether1394_dg_complete(struct packet_task *ptask, int fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct sk_buff *skb = ptask->skb;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001504 struct eth1394_priv *priv = netdev_priv(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 unsigned long flags;
1506
1507 /* Statistics */
1508 spin_lock_irqsave(&priv->lock, flags);
1509 if (fail) {
1510 priv->stats.tx_dropped++;
1511 priv->stats.tx_errors++;
1512 } else {
1513 priv->stats.tx_bytes += skb->len;
1514 priv->stats.tx_packets++;
1515 }
1516 spin_unlock_irqrestore(&priv->lock, flags);
1517
1518 dev_kfree_skb_any(skb);
1519 kmem_cache_free(packet_task_cache, ptask);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/* Callback for when a packet has been sent and the status of that packet is
1523 * known */
1524static void ether1394_complete_cb(void *__ptask)
1525{
1526 struct packet_task *ptask = (struct packet_task *)__ptask;
1527 struct hpsb_packet *packet = ptask->packet;
1528 int fail = 0;
1529
1530 if (packet->tcode != TCODE_STREAM_DATA)
1531 fail = hpsb_packet_success(packet);
1532
1533 ether1394_free_packet(packet);
1534
1535 ptask->outstanding_pkts--;
1536 if (ptask->outstanding_pkts > 0 && !fail) {
1537 int tx_len;
1538
1539 /* Add the encapsulation header to the fragment */
1540 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
1541 &ptask->hdr);
1542 if (ether1394_send_packet(ptask, tx_len))
1543 ether1394_dg_complete(ptask, 1);
1544 } else {
1545 ether1394_dg_complete(ptask, fail);
1546 }
1547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/* Transmit a packet (called by kernel) */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001550static int ether1394_tx(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Al Virob4e3ca12005-10-21 03:22:34 -04001552 gfp_t kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct eth1394hdr *eth;
1554 struct eth1394_priv *priv = netdev_priv(dev);
Ben Collins02f42132006-06-12 18:15:11 -04001555 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 unsigned long flags;
1557 nodeid_t dest_node;
1558 eth1394_tx_type tx_type;
1559 int ret = 0;
1560 unsigned int tx_len;
1561 unsigned int max_payload;
1562 u16 dg_size;
1563 u16 dgl;
1564 struct packet_task *ptask;
1565 struct eth1394_node_ref *node;
1566 struct eth1394_node_info *node_info = NULL;
1567
1568 ptask = kmem_cache_alloc(packet_task_cache, kmflags);
1569 if (ptask == NULL) {
1570 ret = -ENOMEM;
1571 goto fail;
1572 }
1573
1574 /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
1575 * it does not set our validity bit. We need to compensate for
1576 * that somewhere else, but not in eth1394. */
1577#if 0
1578 if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000) {
1579 ret = -EAGAIN;
1580 goto fail;
1581 }
1582#endif
1583
Stefan Richterefbeccf2007-04-02 02:12:32 +02001584 skb = skb_share_check(skb, kmflags);
1585 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 ret = -ENOMEM;
1587 goto fail;
1588 }
1589
1590 /* Get rid of the fake eth1394 header, but save a pointer */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001591 eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 skb_pull(skb, ETH1394_HLEN);
1593
1594 proto = eth->h_proto;
1595 dg_size = skb->len;
1596
1597 /* Set the transmission type for the packet. ARP packets and IP
1598 * broadcast packets are sent via GASP. */
1599 if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
Ben Collins7136b802006-06-12 18:16:25 -04001600 proto == htons(ETH_P_ARP) ||
1601 (proto == htons(ETH_P_IP) &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001602 IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 tx_type = ETH1394_GASP;
1604 dest_node = LOCAL_BUS | ALL_NODES;
1605 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001606 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 dgl = priv->bc_dgl;
1608 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1609 priv->bc_dgl++;
1610 } else {
David S. Millerc20e3942006-10-29 16:14:55 -08001611 __be64 guid = get_unaligned((u64 *)eth->h_dest);
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -08001614 be64_to_cpu(guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!node) {
1616 ret = -EAGAIN;
1617 goto fail;
1618 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001619 node_info =
1620 (struct eth1394_node_info *)node->ud->device.driver_data;
Ben Collins67372312006-06-12 18:15:31 -04001621 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ret = -EAGAIN;
1623 goto fail;
1624 }
1625
1626 dest_node = node->ud->ne->nodeid;
1627 max_payload = node_info->maxpayload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001628 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 dgl = node_info->dgl;
1631 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1632 node_info->dgl++;
1633 tx_type = ETH1394_WRREQ;
1634 }
1635
1636 /* If this is an ARP packet, convert it */
Ben Collins7136b802006-06-12 18:16:25 -04001637 if (proto == htons(ETH_P_ARP))
Stefan Richterefbeccf2007-04-02 02:12:32 +02001638 ether1394_arp_to_1394arp(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 ptask->hdr.words.word1 = 0;
1641 ptask->hdr.words.word2 = 0;
1642 ptask->hdr.words.word3 = 0;
1643 ptask->hdr.words.word4 = 0;
1644 ptask->skb = skb;
1645 ptask->priv = priv;
1646 ptask->tx_type = tx_type;
1647
1648 if (tx_type != ETH1394_GASP) {
1649 u64 addr;
1650
1651 spin_lock_irqsave(&priv->lock, flags);
1652 addr = node_info->fifo;
1653 spin_unlock_irqrestore(&priv->lock, flags);
1654
1655 ptask->addr = addr;
1656 ptask->dest_node = dest_node;
1657 }
1658
1659 ptask->tx_type = tx_type;
1660 ptask->max_payload = max_payload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001661 ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
1662 proto, &ptask->hdr, dg_size, dgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* Add the encapsulation header to the fragment */
1665 tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
1666 dev->trans_start = jiffies;
1667 if (ether1394_send_packet(ptask, tx_len))
1668 goto fail;
1669
1670 netif_wake_queue(dev);
1671 return 0;
1672fail:
1673 if (ptask)
1674 kmem_cache_free(packet_task_cache, ptask);
1675
1676 if (skb != NULL)
1677 dev_kfree_skb(skb);
1678
Stefan Richterefbeccf2007-04-02 02:12:32 +02001679 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 priv->stats.tx_dropped++;
1681 priv->stats.tx_errors++;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001682 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (netif_queue_stopped(dev))
1685 netif_wake_queue(dev);
1686
1687 return 0; /* returning non-zero causes serious problems */
1688}
1689
Stefan Richterefbeccf2007-04-02 02:12:32 +02001690static void ether1394_get_drvinfo(struct net_device *dev,
1691 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001693 strcpy(info->driver, driver_name);
1694 strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697static struct ethtool_ops ethtool_ops = {
1698 .get_drvinfo = ether1394_get_drvinfo
1699};
1700
1701static int __init ether1394_init_module (void)
1702{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001703 packet_task_cache = kmem_cache_create("packet_task",
1704 sizeof(struct packet_task),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 0, 0, NULL, NULL);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 hpsb_register_highlevel(&eth1394_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return hpsb_register_protocol(&eth1394_proto_driver);
1709}
1710
1711static void __exit ether1394_exit_module (void)
1712{
1713 hpsb_unregister_protocol(&eth1394_proto_driver);
1714 hpsb_unregister_highlevel(&eth1394_highlevel);
1715 kmem_cache_destroy(packet_task_cache);
1716}
1717
1718module_init(ether1394_init_module);
1719module_exit(ether1394_exit_module);