blob: 2296d43a24140ab121c3467313af3a5c4df2acb6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_ether.h>
55#include <linux/ip.h>
56#include <linux/in.h>
57#include <linux/tcp.h>
58#include <linux/skbuff.h>
59#include <linux/bitops.h>
60#include <linux/ethtool.h>
61#include <asm/uaccess.h>
62#include <asm/delay.h>
David S. Millerc20e3942006-10-29 16:14:55 -080063#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/arp.h>
65
Stefan Richterde4394f2006-07-03 12:02:29 -040066#include "config_roms.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "csr1212.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040068#include "eth1394.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "highlevel.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040070#include "ieee1394.h"
71#include "ieee1394_core.h"
72#include "ieee1394_hotplug.h"
73#include "ieee1394_transactions.h"
74#include "ieee1394_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include "iso.h"
76#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define ETH1394_PRINT_G(level, fmt, args...) \
79 printk(level "%s: " fmt, driver_name, ## args)
80
81#define ETH1394_PRINT(level, dev_name, fmt, args...) \
82 printk(level "%s: %s: " fmt, driver_name, dev_name, ## args)
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct fragment_info {
85 struct list_head list;
86 int offset;
87 int len;
88};
89
90struct partial_datagram {
91 struct list_head list;
92 u16 dgl;
93 u16 dg_size;
94 u16 ether_type;
95 struct sk_buff *skb;
96 char *pbuf;
97 struct list_head frag_info;
98};
99
100struct pdg_list {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200101 struct list_head list; /* partial datagram list per node */
102 unsigned int sz; /* partial datagram list size per node */
103 spinlock_t lock; /* partial datagram lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct eth1394_host_info {
107 struct hpsb_host *host;
108 struct net_device *dev;
109};
110
111struct eth1394_node_ref {
112 struct unit_directory *ud;
113 struct list_head list;
114};
115
116struct eth1394_node_info {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200117 u16 maxpayload; /* max payload */
118 u8 sspd; /* max speed */
119 u64 fifo; /* FIFO address */
120 struct pdg_list pdg; /* partial RX datagram lists */
121 int dgl; /* outgoing datagram label */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Stefan Richterefbeccf2007-04-02 02:12:32 +0200124static const char driver_name[] = "eth1394";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Lametere18b8902006-12-06 20:33:20 -0800126static struct kmem_cache *packet_task_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128static struct hpsb_highlevel eth1394_highlevel;
129
130/* Use common.lf to determine header len */
131static const int hdr_type_len[] = {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200132 sizeof(struct eth1394_uf_hdr),
133 sizeof(struct eth1394_ff_hdr),
134 sizeof(struct eth1394_sf_hdr),
135 sizeof(struct eth1394_sf_hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static const u16 eth1394_speedto_maxpayload[] = {
139/* S100, S200, S400, S800, S1600, S3200 */
140 512, 1024, 2048, 4096, 4096, 4096
141};
142
143MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
144MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
145MODULE_LICENSE("GPL");
146
Stefan Richterefbeccf2007-04-02 02:12:32 +0200147/*
148 * The max_partial_datagrams parameter is the maximum number of fragmented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * datagrams per node that eth1394 will keep in memory. Providing an upper
150 * bound allows us to limit the amount of memory that partial datagrams
151 * consume in the event that some partial datagrams are never completed.
152 */
153static int max_partial_datagrams = 25;
154module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR);
155MODULE_PARM_DESC(max_partial_datagrams,
156 "Maximum number of partially received fragmented datagrams "
157 "(default = 25).");
158
159
160static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
161 unsigned short type, void *daddr, void *saddr,
162 unsigned len);
163static int ether1394_rebuild_header(struct sk_buff *skb);
164static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr);
165static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh);
166static void ether1394_header_cache_update(struct hh_cache *hh,
167 struct net_device *dev,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200168 unsigned char *haddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int ether1394_tx(struct sk_buff *skb, struct net_device *dev);
170static void ether1394_iso(struct hpsb_iso *iso);
171
172static struct ethtool_ops ethtool_ops;
173
174static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
175 quadlet_t *data, u64 addr, size_t len, u16 flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200176static void ether1394_add_host(struct hpsb_host *host);
177static void ether1394_remove_host(struct hpsb_host *host);
178static void ether1394_host_reset(struct hpsb_host *host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/* Function for incoming 1394 packets */
181static struct hpsb_address_ops addr_ops = {
182 .write = ether1394_write,
183};
184
185/* Ieee1394 highlevel driver functions */
186static struct hpsb_highlevel eth1394_highlevel = {
187 .name = driver_name,
188 .add_host = ether1394_add_host,
189 .remove_host = ether1394_remove_host,
190 .host_reset = ether1394_host_reset,
191};
192
Stefan Richter5009d262007-04-02 02:15:53 +0200193static int ether1394_recv_init(struct eth1394_priv *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200195 unsigned int iso_buf_size;
196
197 /* FIXME: rawiso limits us to PAGE_SIZE */
198 iso_buf_size = min((unsigned int)PAGE_SIZE,
199 2 * (1U << (priv->host->csr.max_rec + 1)));
Jean Delvare09d7a962007-04-01 10:06:33 +0200200
201 priv->iso = hpsb_iso_recv_init(priv->host,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200202 ETHER1394_GASP_BUFFERS * iso_buf_size,
Jean Delvare09d7a962007-04-01 10:06:33 +0200203 ETHER1394_GASP_BUFFERS,
204 priv->broadcast_channel,
205 HPSB_ISO_DMA_PACKET_PER_BUFFER,
206 1, ether1394_iso);
207 if (priv->iso == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200208 ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n");
Jean Delvare09d7a962007-04-01 10:06:33 +0200209 priv->bc_state = ETHER1394_BC_ERROR;
210 return -EAGAIN;
211 }
212
213 if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
214 priv->bc_state = ETHER1394_BC_STOPPED;
215 else
216 priv->bc_state = ETHER1394_BC_RUNNING;
217 return 0;
218}
219
220/* This is called after an "ifup" */
221static int ether1394_open(struct net_device *dev)
222{
223 struct eth1394_priv *priv = netdev_priv(dev);
224 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (priv->bc_state == ETHER1394_BC_ERROR) {
Stefan Richter5009d262007-04-02 02:15:53 +0200227 ret = ether1394_recv_init(priv);
Jean Delvare09d7a962007-04-01 10:06:33 +0200228 if (ret)
229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Stefan Richterefbeccf2007-04-02 02:12:32 +0200231 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
235/* This is called after an "ifdown" */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200236static int ether1394_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200238 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
240}
241
242/* Return statistics to the caller */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200243static struct net_device_stats *ether1394_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 return &(((struct eth1394_priv *)netdev_priv(dev))->stats);
246}
247
Stefan Richterefbeccf2007-04-02 02:12:32 +0200248/* FIXME: What to do if we timeout? I think a host reset is probably in order,
249 * so that's what we do. Should we increment the stat counters too? */
250static void ether1394_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200252 struct hpsb_host *host =
253 ((struct eth1394_priv *)netdev_priv(dev))->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Stefan Richter246a5fd2007-04-02 02:16:40 +0200255 ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n");
256 ether1394_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Stefan Richter17bab402007-04-03 23:55:40 +0200259static inline int ether1394_max_mtu(struct hpsb_host* host)
260{
261 return (1 << (host->csr.max_rec + 1))
262 - sizeof(union eth1394_hdr) - ETHER1394_GASP_OVERHEAD;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
266{
Stefan Richter17bab402007-04-03 23:55:40 +0200267 int max_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Stefan Richter17bab402007-04-03 23:55:40 +0200269 if (new_mtu < 68)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EINVAL;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200271
Stefan Richter17bab402007-04-03 23:55:40 +0200272 max_mtu = ether1394_max_mtu(
273 ((struct eth1394_priv *)netdev_priv(dev))->host);
274 if (new_mtu > max_mtu) {
275 ETH1394_PRINT(KERN_INFO, dev->name,
276 "Local node constrains MTU to %d\n", max_mtu);
277 return -ERANGE;
278 }
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dev->mtu = new_mtu;
281 return 0;
282}
283
284static void purge_partial_datagram(struct list_head *old)
285{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200286 struct partial_datagram *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct list_head *lh, *n;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200288 struct fragment_info *fi;
289
290 pd = list_entry(old, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 list_for_each_safe(lh, n, &pd->frag_info) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200293 fi = list_entry(lh, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_del(lh);
295 kfree(fi);
296 }
297 list_del(old);
298 kfree_skb(pd->skb);
299 kfree(pd);
300}
301
302/******************************************
303 * 1394 bus activity functions
304 ******************************************/
305
306static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl,
307 struct unit_directory *ud)
308{
309 struct eth1394_node_ref *node;
310
311 list_for_each_entry(node, inl, list)
312 if (node->ud == ud)
313 return node;
314
315 return NULL;
316}
317
318static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl,
319 u64 guid)
320{
321 struct eth1394_node_ref *node;
322
323 list_for_each_entry(node, inl, list)
324 if (node->ud->ne->guid == guid)
325 return node;
326
327 return NULL;
328}
329
330static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl,
331 nodeid_t nodeid)
332{
333 struct eth1394_node_ref *node;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200334
335 list_for_each_entry(node, inl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (node->ud->ne->nodeid == nodeid)
337 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 return NULL;
340}
341
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200342static int eth1394_new_node(struct eth1394_host_info *hi,
343 struct unit_directory *ud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct eth1394_priv *priv;
346 struct eth1394_node_ref *new_node;
347 struct eth1394_node_info *node_info;
348
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200349 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!new_node)
351 return -ENOMEM;
352
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200353 node_info = kmalloc(sizeof(*node_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!node_info) {
355 kfree(new_node);
356 return -ENOMEM;
357 }
358
359 spin_lock_init(&node_info->pdg.lock);
360 INIT_LIST_HEAD(&node_info->pdg.list);
361 node_info->pdg.sz = 0;
Ben Collins67372312006-06-12 18:15:31 -0400362 node_info->fifo = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 ud->device.driver_data = node_info;
365 new_node->ud = ud;
366
367 priv = netdev_priv(hi->dev);
368 list_add_tail(&new_node->list, &priv->ip_node_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200372static int eth1394_probe(struct device *dev)
373{
374 struct unit_directory *ud;
375 struct eth1394_host_info *hi;
376
377 ud = container_of(dev, struct unit_directory, device);
378 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
379 if (!hi)
380 return -ENOENT;
381
382 return eth1394_new_node(hi, ud);
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static int eth1394_remove(struct device *dev)
386{
387 struct unit_directory *ud;
388 struct eth1394_host_info *hi;
389 struct eth1394_priv *priv;
390 struct eth1394_node_ref *old_node;
391 struct eth1394_node_info *node_info;
392 struct list_head *lh, *n;
393 unsigned long flags;
394
395 ud = container_of(dev, struct unit_directory, device);
396 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
397 if (!hi)
398 return -ENOENT;
399
400 priv = netdev_priv(hi->dev);
401
402 old_node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200403 if (!old_node)
404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Stefan Richterefbeccf2007-04-02 02:12:32 +0200406 list_del(&old_node->list);
407 kfree(old_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stefan Richterefbeccf2007-04-02 02:12:32 +0200409 node_info = (struct eth1394_node_info*)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Stefan Richterefbeccf2007-04-02 02:12:32 +0200411 spin_lock_irqsave(&node_info->pdg.lock, flags);
412 /* The partial datagram list should be empty, but we'll just
413 * make sure anyway... */
414 list_for_each_safe(lh, n, &node_info->pdg.list)
415 purge_partial_datagram(lh);
416 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Stefan Richterefbeccf2007-04-02 02:12:32 +0200418 kfree(node_info);
419 ud->device.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
423static int eth1394_update(struct unit_directory *ud)
424{
425 struct eth1394_host_info *hi;
426 struct eth1394_priv *priv;
427 struct eth1394_node_ref *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
430 if (!hi)
431 return -ENOENT;
432
433 priv = netdev_priv(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200435 if (node)
436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200438 return eth1394_new_node(hi, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static struct ieee1394_device_id eth1394_id_table[] = {
442 {
443 .match_flags = (IEEE1394_MATCH_SPECIFIER_ID |
444 IEEE1394_MATCH_VERSION),
445 .specifier_id = ETHER1394_GASP_SPECIFIER_ID,
446 .version = ETHER1394_GASP_VERSION,
447 },
448 {}
449};
450
451MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table);
452
453static struct hpsb_protocol_driver eth1394_proto_driver = {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200454 .name = driver_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .id_table = eth1394_id_table,
456 .update = eth1394_update,
457 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .probe = eth1394_probe,
459 .remove = eth1394_remove,
460 },
461};
462
Stefan Richterefbeccf2007-04-02 02:12:32 +0200463static void ether1394_reset_priv(struct net_device *dev, int set_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 unsigned long flags;
466 int i;
467 struct eth1394_priv *priv = netdev_priv(dev);
468 struct hpsb_host *host = priv->host;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200469 u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int max_speed = IEEE1394_SPEED_MAX;
471
Stefan Richterefbeccf2007-04-02 02:12:32 +0200472 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Stefan Richter027611b2007-04-02 02:15:21 +0200474 memset(priv->ud_list, 0, sizeof(priv->ud_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 priv->bc_maxpayload = 512;
476
477 /* Determine speed limit */
Stefan Richter21b2c562007-04-23 21:28:47 +0200478 /* FIXME: This is broken for nodes with link speed < PHY speed,
479 * and it is suboptimal for S200B...S800B hardware.
480 * The result of nodemgr's speed probe should be used somehow. */
481 for (i = 0; i < host->node_count; i++) {
482 /* take care of S100B...S400B PHY ports */
483 if (host->speed[i] == SELFID_SPEED_UNKNOWN) {
484 max_speed = IEEE1394_SPEED_100;
485 break;
486 }
Ben Collins647dcb52006-06-12 18:12:37 -0400487 if (max_speed > host->speed[i])
488 max_speed = host->speed[i];
Stefan Richter21b2c562007-04-23 21:28:47 +0200489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 priv->bc_sspd = max_speed;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (set_mtu) {
Stefan Richter17bab402007-04-03 23:55:40 +0200493 /* Use the RFC 2734 default 1500 octets or the maximum payload
494 * as initial MTU */
495 dev->mtu = min(1500, ether1394_max_mtu(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Set our hardware address while we're at it */
David S. Millerc20e3942006-10-29 16:14:55 -0800498 memcpy(dev->dev_addr, &guid, sizeof(u64));
499 memset(dev->broadcast, 0xff, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
Stefan Richterefbeccf2007-04-02 02:12:32 +0200502 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Stefan Richterefbeccf2007-04-02 02:12:32 +0200505static void ether1394_init_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dev->open = ether1394_open;
508 dev->stop = ether1394_stop;
509 dev->hard_start_xmit = ether1394_tx;
510 dev->get_stats = ether1394_stats;
511 dev->tx_timeout = ether1394_tx_timeout;
512 dev->change_mtu = ether1394_change_mtu;
513
514 dev->hard_header = ether1394_header;
515 dev->rebuild_header = ether1394_rebuild_header;
516 dev->hard_header_cache = ether1394_header_cache;
517 dev->header_cache_update= ether1394_header_cache_update;
518 dev->hard_header_parse = ether1394_header_parse;
Stefan Richter01590d22007-04-02 02:20:37 +0200519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 SET_ETHTOOL_OPS(dev, &ethtool_ops);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dev->watchdog_timeo = ETHER1394_TIMEOUT;
523 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
524 dev->features = NETIF_F_HIGHDMA;
525 dev->addr_len = ETH1394_ALEN;
526 dev->hard_header_len = ETH1394_HLEN;
527 dev->type = ARPHRD_IEEE1394;
528
Stefan Richter01590d22007-04-02 02:20:37 +0200529 /* FIXME: This value was copied from ether_setup(). Is it too much? */
530 dev->tx_queue_len = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
534 * This function is called every time a card is found. It is generally called
535 * when the module is installed. This is where we add all of our ethernet
536 * devices. One for each host.
537 */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200538static void ether1394_add_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct eth1394_host_info *hi = NULL;
541 struct net_device *dev = NULL;
542 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 u64 fifo_addr;
544
Stefan Richter70093cf2007-03-27 01:36:50 +0200545 if (hpsb_config_rom_ip1394_add(host) != 0) {
546 ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return;
Stefan Richter70093cf2007-03-27 01:36:50 +0200548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ben Collins67372312006-06-12 18:15:31 -0400550 fifo_addr = hpsb_allocate_and_register_addrspace(
551 &eth1394_highlevel, host, &addr_ops,
552 ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN,
553 CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE);
Stefan Richter157188c2007-02-10 23:56:38 +0100554 if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
555 ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n");
Stefan Richter70093cf2007-03-27 01:36:50 +0200556 hpsb_config_rom_ip1394_remove(host);
Stefan Richter157188c2007-02-10 23:56:38 +0100557 return;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Stefan Richter01590d22007-04-02 02:20:37 +0200560 dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (dev == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200562 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 SET_MODULE_OWNER(dev);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100567#if 0
568 /* FIXME - Is this the correct parent device anyway? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 SET_NETDEV_DEV(dev, &host->device);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 INIT_LIST_HEAD(&priv->ip_node_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200580 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Stefan Richter01590d22007-04-02 02:20:37 +0200584 ether1394_reset_priv(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Stefan Richter5009d262007-04-02 02:15:53 +0200586 if (register_netdev(dev)) {
587 ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto out;
589 }
590
Stefan Richter5009d262007-04-02 02:15:53 +0200591 ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n",
592 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 hi->host = host;
595 hi->dev = dev;
596
597 /* Ignore validity in hopes that it will be set in the future. It'll
598 * be checked when the eth device is opened. */
599 priv->broadcast_channel = host->csr.broadcast_channel & 0x3f;
600
Stefan Richter5009d262007-04-02 02:15:53 +0200601 ether1394_recv_init(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603out:
Stefan Richter157188c2007-02-10 23:56:38 +0100604 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 free_netdev(dev);
606 if (hi)
607 hpsb_destroy_hostinfo(&eth1394_highlevel, host);
Stefan Richter157188c2007-02-10 23:56:38 +0100608 hpsb_unregister_addrspace(&eth1394_highlevel, host, fifo_addr);
Stefan Richter70093cf2007-03-27 01:36:50 +0200609 hpsb_config_rom_ip1394_remove(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/* Remove a card from our list */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200613static void ether1394_remove_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct eth1394_host_info *hi;
Stefan Richter2cd556a2007-02-10 23:57:57 +0100616 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100619 if (!hi)
620 return;
621 priv = netdev_priv(hi->dev);
622 hpsb_unregister_addrspace(&eth1394_highlevel, host, priv->local_fifo);
Stefan Richter70093cf2007-03-27 01:36:50 +0200623 hpsb_config_rom_ip1394_remove(host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100624 if (priv->iso)
625 hpsb_iso_shutdown(priv->iso);
626 unregister_netdev(hi->dev);
627 free_netdev(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Stefan Richterefbeccf2007-04-02 02:12:32 +0200630/* A bus reset happened */
631static void ether1394_host_reset(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct eth1394_host_info *hi;
634 struct eth1394_priv *priv;
635 struct net_device *dev;
636 struct list_head *lh, *n;
637 struct eth1394_node_ref *node;
638 struct eth1394_node_info *node_info;
639 unsigned long flags;
640
641 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
642
643 /* This can happen for hosts that we don't use */
Stefan Richter2cd556a2007-02-10 23:57:57 +0100644 if (!hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
646
647 dev = hi->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200648 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Stefan Richterefbeccf2007-04-02 02:12:32 +0200650 /* Reset our private host data, but not our MTU */
651 netif_stop_queue(dev);
652 ether1394_reset_priv(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 list_for_each_entry(node, &priv->ip_node_list, list) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200655 node_info = node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 spin_lock_irqsave(&node_info->pdg.lock, flags);
658
Stefan Richterefbeccf2007-04-02 02:12:32 +0200659 list_for_each_safe(lh, n, &node_info->pdg.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 purge_partial_datagram(lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 INIT_LIST_HEAD(&(node_info->pdg.list));
663 node_info->pdg.sz = 0;
664
665 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
666 }
667
Stefan Richterefbeccf2007-04-02 02:12:32 +0200668 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/******************************************
672 * HW Header net device functions
673 ******************************************/
674/* These functions have been adapted from net/ethernet/eth.c */
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/* Create a fake MAC header for an arbitrary protocol layer.
677 * saddr=NULL means use device source address
678 * daddr=NULL means leave destination address (eg unresolved arp). */
679static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
680 unsigned short type, void *daddr, void *saddr,
681 unsigned len)
682{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200683 struct eth1394hdr *eth =
684 (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 eth->h_proto = htons(type);
687
Stefan Richterefbeccf2007-04-02 02:12:32 +0200688 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 memset(eth->h_dest, 0, dev->addr_len);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200690 return dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 if (daddr) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200694 memcpy(eth->h_dest, daddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return dev->hard_header_len;
696 }
697
698 return -dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/* Rebuild the faked MAC header. This is called after an ARP
702 * (or in future other address resolution) has completed on this
703 * sk_buff. We now let ARP fill in the other fields.
704 *
705 * This routine CANNOT use cached dst->neigh!
706 * Really, it is used only when dst->neigh is wrong.
707 */
708static int ether1394_rebuild_header(struct sk_buff *skb)
709{
710 struct eth1394hdr *eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Stefan Richter599bba92007-04-02 02:19:02 +0200712 if (eth->h_proto == htons(ETH_P_IP))
Stefan Richterefbeccf2007-04-02 02:12:32 +0200713 return arp_find((unsigned char *)&eth->h_dest, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Stefan Richter599bba92007-04-02 02:19:02 +0200715 ETH1394_PRINT(KERN_DEBUG, skb->dev->name,
716 "unable to resolve type %04x addresses\n",
717 ntohs(eth->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
721static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr)
722{
723 struct net_device *dev = skb->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
726 return ETH1394_ALEN;
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
730{
731 unsigned short type = hh->hh_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct net_device *dev = neigh->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200733 struct eth1394hdr *eth =
734 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Ben Collins7136b802006-06-12 18:16:25 -0400736 if (type == htons(ETH_P_802_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 eth->h_proto = type;
740 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
741
742 hh->hh_len = ETH1394_HLEN;
743 return 0;
744}
745
746/* Called by Address Resolution module to notify changes in address. */
747static void ether1394_header_cache_update(struct hh_cache *hh,
748 struct net_device *dev,
749 unsigned char * haddr)
750{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200751 memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/******************************************
755 * Datagram reception code
756 ******************************************/
757
758/* Copied from net/ethernet/eth.c */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100759static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct eth1394hdr *eth;
762 unsigned char *rawp;
763
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700764 skb_reset_mac_header(skb);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200765 skb_pull(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 eth = eth1394_hdr(skb);
767
768 if (*eth->h_dest & 1) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200769 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 skb->pkt_type = PACKET_BROADCAST;
771#if 0
772 else
773 skb->pkt_type = PACKET_MULTICAST;
774#endif
775 } else {
776 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
777 skb->pkt_type = PACKET_OTHERHOST;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Stefan Richterefbeccf2007-04-02 02:12:32 +0200780 if (ntohs(eth->h_proto) >= 1536)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return eth->h_proto;
782
783 rawp = skb->data;
784
Stefan Richterefbeccf2007-04-02 02:12:32 +0200785 if (*(unsigned short *)rawp == 0xFFFF)
786 return htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Stefan Richterefbeccf2007-04-02 02:12:32 +0200788 return htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/* Parse an encapsulated IP1394 header into an ethernet frame packet.
792 * We also perform ARP translation here, if need be. */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100793static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev,
794 nodeid_t srcid, nodeid_t destid,
795 u16 ether_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct eth1394_priv *priv = netdev_priv(dev);
798 u64 dest_hw;
799 unsigned short ret = 0;
800
Stefan Richterefbeccf2007-04-02 02:12:32 +0200801 /* Setup our hw addresses. We use these to build the ethernet header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (destid == (LOCAL_BUS | ALL_NODES))
803 dest_hw = ~0ULL; /* broadcast */
804 else
Stefan Richterefbeccf2007-04-02 02:12:32 +0200805 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 priv->host->csr.guid_lo);
807
808 /* If this is an ARP packet, convert it. First, we want to make
809 * use of some of the fields, since they tell us a little bit
810 * about the sending machine. */
Ben Collins7136b802006-06-12 18:16:25 -0400811 if (ether_type == htons(ETH_P_ARP)) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200812 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct arphdr *arp = (struct arphdr *)skb->data;
814 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
815 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
Stefan Richterefbeccf2007-04-02 02:12:32 +0200816 ntohl(arp1394->fifo_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 u8 max_rec = min(priv->host->csr.max_rec,
818 (u8)(arp1394->max_rec));
819 int sspd = arp1394->sspd;
820 u16 maxpayload;
821 struct eth1394_node_ref *node;
822 struct eth1394_node_info *node_info;
David S. Millerc20e3942006-10-29 16:14:55 -0800823 __be64 guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Sanity check. MacOSX seems to be sending us 131 in this
826 * field (atleast on my Panther G5). Not sure why. */
827 if (sspd > 5 || sspd < 0)
828 sspd = 0;
829
Stefan Richterefbeccf2007-04-02 02:12:32 +0200830 maxpayload = min(eth1394_speedto_maxpayload[sspd],
831 (u16)(1 << (max_rec + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David S. Millerc20e3942006-10-29 16:14:55 -0800833 guid = get_unaligned(&arp1394->s_uniq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -0800835 be64_to_cpu(guid));
Stefan Richterefbeccf2007-04-02 02:12:32 +0200836 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Stefan Richterefbeccf2007-04-02 02:12:32 +0200839 node_info =
840 (struct eth1394_node_info *)node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* Update our speed/payload/fifo_offset table */
843 node_info->maxpayload = maxpayload;
844 node_info->sspd = sspd;
845 node_info->fifo = fifo_addr;
846
847 /* Now that we're done with the 1394 specific stuff, we'll
848 * need to alter some of the data. Believe it or not, all
849 * that needs to be done is sender_IP_address needs to be
850 * moved, the destination hardware address get stuffed
851 * in and the hardware address length set to 8.
852 *
853 * IMPORTANT: The code below overwrites 1394 specific data
854 * needed above so keep the munging of the data for the
855 * higher level IP stack last. */
856
857 arp->ar_hln = 8;
858 arp_ptr += arp->ar_hln; /* skip over sender unique id */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200859 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 arp_ptr += arp->ar_pln; /* skip over sender IP addr */
861
Ben Collins02f42132006-06-12 18:15:11 -0400862 if (arp->ar_op == htons(ARPOP_REQUEST))
David S. Millerc20e3942006-10-29 16:14:55 -0800863 memset(arp_ptr, 0, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 else
David S. Millerc20e3942006-10-29 16:14:55 -0800865 memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868 /* Now add the ethernet header. */
Ben Collins7136b802006-06-12 18:16:25 -0400869 if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
870 skb->len) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ret = ether1394_type_trans(skb, dev);
872
873 return ret;
874}
875
Stefan Richtere00f04a2007-03-18 12:23:11 +0100876static int fragment_overlap(struct list_head *frag_list, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct fragment_info *fi;
Stefan Richter2e2173d2007-04-02 02:21:46 +0200879 int end = offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Stefan Richter2e2173d2007-04-02 02:21:46 +0200881 list_for_each_entry(fi, frag_list, list)
882 if (offset < fi->offset + fi->len && end > fi->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 1;
Stefan Richter2e2173d2007-04-02 02:21:46 +0200884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
886}
887
Stefan Richtere00f04a2007-03-18 12:23:11 +0100888static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct partial_datagram *pd;
891
Stefan Richterefbeccf2007-04-02 02:12:32 +0200892 list_for_each_entry(pd, pdgl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (pd->dgl == dgl)
894 return &pd->list;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return NULL;
897}
898
899/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100900static int new_fragment(struct list_head *frag_info, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct list_head *lh;
903 struct fragment_info *fi, *fi2, *new;
904
905 list_for_each(lh, frag_info) {
906 fi = list_entry(lh, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200907 if (fi->offset + fi->len == offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* The new fragment can be tacked on to the end */
909 fi->len += len;
910 /* Did the new fragment plug a hole? */
911 fi2 = list_entry(lh->next, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200912 if (fi->offset + fi->len == fi2->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* glue fragments together */
914 fi->len += fi2->len;
915 list_del(lh->next);
916 kfree(fi2);
917 }
918 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200919 } else if (offset + len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* The new fragment can be tacked on to the beginning */
921 fi->offset = offset;
922 fi->len += len;
923 /* Did the new fragment plug a hole? */
924 fi2 = list_entry(lh->prev, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200925 if (fi2->offset + fi2->len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* glue fragments together */
927 fi2->len += fi->len;
928 list_del(lh);
929 kfree(fi);
930 }
931 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200932 } else if (offset > fi->offset + fi->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200934 } else if (offset + len < fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 lh = lh->prev;
936 break;
937 }
938 }
939
Stefan Richter85511582005-11-07 06:31:45 -0500940 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!new)
942 return -ENOMEM;
943
944 new->offset = offset;
945 new->len = len;
946
947 list_add(&new->list, lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949}
950
Stefan Richtere00f04a2007-03-18 12:23:11 +0100951static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl,
952 int dgl, int dg_size, char *frag_buf,
953 int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct partial_datagram *new;
956
Stefan Richter85511582005-11-07 06:31:45 -0500957 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!new)
959 return -ENOMEM;
960
961 INIT_LIST_HEAD(&new->frag_info);
962
963 if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
964 kfree(new);
965 return -ENOMEM;
966 }
967
968 new->dgl = dgl;
969 new->dg_size = dg_size;
970
971 new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
972 if (!new->skb) {
973 struct fragment_info *fi = list_entry(new->frag_info.next,
974 struct fragment_info,
975 list);
976 kfree(fi);
977 kfree(new);
978 return -ENOMEM;
979 }
980
981 skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
982 new->pbuf = skb_put(new->skb, dg_size);
983 memcpy(new->pbuf + frag_off, frag_buf, frag_len);
984
985 list_add(&new->list, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Stefan Richtere00f04a2007-03-18 12:23:11 +0100989static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
990 char *frag_buf, int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200992 struct partial_datagram *pd =
993 list_entry(lh, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Stefan Richterefbeccf2007-04-02 02:12:32 +0200995 if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
999
1000 /* Move list entry to beginnig of list so that oldest partial
1001 * datagrams percolate to the end of the list */
Akinobu Mita179e0912006-06-26 00:24:41 -07001002 list_move(lh, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
1004}
1005
Stefan Richtere00f04a2007-03-18 12:23:11 +01001006static int is_datagram_complete(struct list_head *lh, int dg_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Stefan Richtere00f04a2007-03-18 12:23:11 +01001008 struct partial_datagram *pd;
1009 struct fragment_info *fi;
1010
1011 pd = list_entry(lh, struct partial_datagram, list);
1012 fi = list_entry(pd->frag_info.next, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return (fi->len == dg_size);
1015}
1016
1017/* Packet reception. We convert the IP1394 encapsulation header to an
1018 * ethernet header, and fill it with some of our other fields. This is
1019 * an incoming packet from the 1394 bus. */
1020static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
1021 char *buf, int len)
1022{
1023 struct sk_buff *skb;
1024 unsigned long flags;
1025 struct eth1394_priv *priv = netdev_priv(dev);
1026 union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
1027 u16 ether_type = 0; /* initialized to clear warning */
1028 int hdr_len;
1029 struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
1030 struct eth1394_node_info *node_info;
1031
1032 if (!ud) {
1033 struct eth1394_node_ref *node;
1034 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
Stefan Richter09939872007-04-02 02:22:21 +02001035 if (unlikely(!node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
1037 "lookup failure: " NODE_BUS_FMT,
1038 NODE_BUS_ARGS(priv->host, srcid));
1039 priv->stats.rx_dropped++;
1040 return -1;
1041 }
1042 ud = node->ud;
1043
1044 priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
1045 }
1046
Stefan Richterefbeccf2007-04-02 02:12:32 +02001047 node_info = (struct eth1394_node_info *)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /* First, did we receive a fragmented or unfragmented datagram? */
1050 hdr->words.word1 = ntohs(hdr->words.word1);
1051
1052 hdr_len = hdr_type_len[hdr->common.lf];
1053
1054 if (hdr->common.lf == ETH1394_HDR_LF_UF) {
1055 /* An unfragmented datagram has been received by the ieee1394
1056 * bus. Build an skbuff around it so we can pass it to the
1057 * high level network layer. */
1058
1059 skb = dev_alloc_skb(len + dev->hard_header_len + 15);
Stefan Richter09939872007-04-02 02:22:21 +02001060 if (unlikely(!skb)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001061 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 priv->stats.rx_dropped++;
1063 return -1;
1064 }
1065 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001066 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
1067 len - hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ether_type = hdr->uf.ether_type;
1069 } else {
1070 /* A datagram fragment has been received, now the fun begins. */
1071
1072 struct list_head *pdgl, *lh;
1073 struct partial_datagram *pd;
1074 int fg_off;
1075 int fg_len = len - hdr_len;
1076 int dg_size;
1077 int dgl;
1078 int retval;
1079 struct pdg_list *pdg = &(node_info->pdg);
1080
1081 hdr->words.word3 = ntohs(hdr->words.word3);
1082 /* The 4th header word is reserved so no need to do ntohs() */
1083
1084 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1085 ether_type = hdr->ff.ether_type;
1086 dgl = hdr->ff.dgl;
1087 dg_size = hdr->ff.dg_size + 1;
1088 fg_off = 0;
1089 } else {
1090 hdr->words.word2 = ntohs(hdr->words.word2);
1091 dgl = hdr->sf.dgl;
1092 dg_size = hdr->sf.dg_size + 1;
1093 fg_off = hdr->sf.fg_off;
1094 }
1095 spin_lock_irqsave(&pdg->lock, flags);
1096
1097 pdgl = &(pdg->list);
1098 lh = find_partial_datagram(pdgl, dgl);
1099
1100 if (lh == NULL) {
1101 while (pdg->sz >= max_partial_datagrams) {
1102 /* remove the oldest */
1103 purge_partial_datagram(pdgl->prev);
1104 pdg->sz--;
1105 }
1106
1107 retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
1108 buf + hdr_len, fg_off,
1109 fg_len);
1110 if (retval < 0) {
1111 spin_unlock_irqrestore(&pdg->lock, flags);
1112 goto bad_proto;
1113 }
1114 pdg->sz++;
1115 lh = find_partial_datagram(pdgl, dgl);
1116 } else {
1117 struct partial_datagram *pd;
1118
1119 pd = list_entry(lh, struct partial_datagram, list);
1120
1121 if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
1122 /* Overlapping fragments, obliterate old
1123 * datagram and start new one. */
1124 purge_partial_datagram(lh);
1125 retval = new_partial_datagram(dev, pdgl, dgl,
1126 dg_size,
1127 buf + hdr_len,
1128 fg_off, fg_len);
1129 if (retval < 0) {
1130 pdg->sz--;
1131 spin_unlock_irqrestore(&pdg->lock, flags);
1132 goto bad_proto;
1133 }
1134 } else {
1135 retval = update_partial_datagram(pdgl, lh,
1136 buf + hdr_len,
1137 fg_off, fg_len);
1138 if (retval < 0) {
1139 /* Couldn't save off fragment anyway
1140 * so might as well obliterate the
1141 * datagram now. */
1142 purge_partial_datagram(lh);
1143 pdg->sz--;
1144 spin_unlock_irqrestore(&pdg->lock, flags);
1145 goto bad_proto;
1146 }
1147 } /* fragment overlap */
1148 } /* new datagram or add to existing one */
1149
1150 pd = list_entry(lh, struct partial_datagram, list);
1151
Stefan Richterefbeccf2007-04-02 02:12:32 +02001152 if (hdr->common.lf == ETH1394_HDR_LF_FF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 pd->ether_type = ether_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 if (is_datagram_complete(lh, dg_size)) {
1156 ether_type = pd->ether_type;
1157 pdg->sz--;
1158 skb = skb_get(pd->skb);
1159 purge_partial_datagram(lh);
1160 spin_unlock_irqrestore(&pdg->lock, flags);
1161 } else {
1162 /* Datagram is not complete, we're done for the
1163 * moment. */
1164 spin_unlock_irqrestore(&pdg->lock, flags);
1165 return 0;
1166 }
1167 } /* unframgented datagram or fragmented one */
1168
1169 /* Write metadata, and then pass to the receive level */
1170 skb->dev = dev;
1171 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
1172
1173 /* Parse the encapsulation header. This actually does the job of
1174 * converting to an ethernet frame header, aswell as arp
1175 * conversion if needed. ARP conversion is easier in this
1176 * direction, since we are using ethernet as our backend. */
1177 skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
1178 ether_type);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_lock_irqsave(&priv->lock, flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!skb->protocol) {
1183 priv->stats.rx_errors++;
1184 priv->stats.rx_dropped++;
1185 dev_kfree_skb_any(skb);
1186 goto bad_proto;
1187 }
1188
1189 if (netif_rx(skb) == NET_RX_DROP) {
1190 priv->stats.rx_errors++;
1191 priv->stats.rx_dropped++;
1192 goto bad_proto;
1193 }
1194
1195 /* Statistics */
1196 priv->stats.rx_packets++;
1197 priv->stats.rx_bytes += skb->len;
1198
1199bad_proto:
1200 if (netif_queue_stopped(dev))
1201 netif_wake_queue(dev);
1202 spin_unlock_irqrestore(&priv->lock, flags);
1203
1204 dev->last_rx = jiffies;
1205
1206 return 0;
1207}
1208
1209static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
1210 quadlet_t *data, u64 addr, size_t len, u16 flags)
1211{
1212 struct eth1394_host_info *hi;
1213
1214 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
Stefan Richter09939872007-04-02 02:22:21 +02001215 if (unlikely(!hi)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001216 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1217 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return RCODE_ADDRESS_ERROR;
1219 }
1220
1221 if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
1222 return RCODE_ADDRESS_ERROR;
1223 else
1224 return RCODE_COMPLETE;
1225}
1226
1227static void ether1394_iso(struct hpsb_iso *iso)
1228{
1229 quadlet_t *data;
1230 char *buf;
1231 struct eth1394_host_info *hi;
1232 struct net_device *dev;
1233 struct eth1394_priv *priv;
1234 unsigned int len;
1235 u32 specifier_id;
1236 u16 source_id;
1237 int i;
1238 int nready;
1239
1240 hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
Stefan Richter09939872007-04-02 02:22:21 +02001241 if (unlikely(!hi)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001242 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1243 iso->host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return;
1245 }
1246
1247 dev = hi->dev;
1248
1249 nready = hpsb_iso_n_ready(iso);
1250 for (i = 0; i < nready; i++) {
1251 struct hpsb_iso_packet_info *info =
1252 &iso->infos[(iso->first_packet + i) % iso->buf_packets];
Stefan Richterefbeccf2007-04-02 02:12:32 +02001253 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /* skip over GASP header */
1256 buf = (char *)data + 8;
1257 len = info->len - 8;
1258
Stefan Richterefbeccf2007-04-02 02:12:32 +02001259 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
1260 (be32_to_cpu(data[1]) & 0xff000000) >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 source_id = be32_to_cpu(data[0]) >> 16;
1262
1263 priv = netdev_priv(dev);
1264
Stefan Richterefbeccf2007-04-02 02:12:32 +02001265 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
1266 || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* This packet is not for us */
1268 continue;
1269 }
1270 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
1271 buf, len);
1272 }
1273
1274 hpsb_iso_recv_release_packets(iso, i);
1275
1276 dev->last_rx = jiffies;
1277}
1278
1279/******************************************
1280 * Datagram transmission code
1281 ******************************************/
1282
1283/* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
1284 * arphdr) is the same format as the ip1394 header, so they overlap. The rest
1285 * needs to be munged a bit. The remainder of the arphdr is formatted based
1286 * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to
1287 * judge.
1288 *
1289 * Now that the EUI is used for the hardware address all we need to do to make
1290 * this work for 1394 is to insert 2 quadlets that contain max_rec size,
1291 * speed, and unicast FIFO address information between the sender_unique_id
1292 * and the IP addresses.
1293 */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001294static void ether1394_arp_to_1394arp(struct sk_buff *skb,
1295 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct eth1394_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct arphdr *arp = (struct arphdr *)skb->data;
1299 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1300 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 arp1394->hw_addr_len = 16;
1303 arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN);
1304 arp1394->max_rec = priv->host->csr.max_rec;
1305 arp1394->sspd = priv->host->csr.lnk_spd;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001306 arp1394->fifo_hi = htons(priv->local_fifo >> 32);
1307 arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310/* We need to encapsulate the standard header with our own. We use the
1311 * ethernet header's proto for our own. */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001312static unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
1313 __be16 proto,
1314 union eth1394_hdr *hdr,
1315 u16 dg_size, u16 dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001317 unsigned int adj_max_payload =
1318 max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* Does it all fit in one packet? */
1321 if (dg_size <= adj_max_payload) {
1322 hdr->uf.lf = ETH1394_HDR_LF_UF;
1323 hdr->uf.ether_type = proto;
1324 } else {
1325 hdr->ff.lf = ETH1394_HDR_LF_FF;
1326 hdr->ff.ether_type = proto;
1327 hdr->ff.dg_size = dg_size - 1;
1328 hdr->ff.dgl = dgl;
1329 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
1330 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001331 return (dg_size + adj_max_payload - 1) / adj_max_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Stefan Richtere00f04a2007-03-18 12:23:11 +01001334static unsigned int ether1394_encapsulate(struct sk_buff *skb,
1335 unsigned int max_payload,
1336 union eth1394_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 union eth1394_hdr *bufhdr;
1339 int ftype = hdr->common.lf;
1340 int hdrsz = hdr_type_len[ftype];
1341 unsigned int adj_max_payload = max_payload - hdrsz;
1342
Stefan Richterefbeccf2007-04-02 02:12:32 +02001343 switch (ftype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 case ETH1394_HDR_LF_UF:
1345 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1346 bufhdr->words.word1 = htons(hdr->words.word1);
1347 bufhdr->words.word2 = hdr->words.word2;
1348 break;
1349
1350 case ETH1394_HDR_LF_FF:
1351 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1352 bufhdr->words.word1 = htons(hdr->words.word1);
1353 bufhdr->words.word2 = hdr->words.word2;
1354 bufhdr->words.word3 = htons(hdr->words.word3);
1355 bufhdr->words.word4 = 0;
1356
1357 /* Set frag type here for future interior fragments */
1358 hdr->common.lf = ETH1394_HDR_LF_IF;
1359 hdr->sf.fg_off = 0;
1360 break;
1361
1362 default:
1363 hdr->sf.fg_off += adj_max_payload;
1364 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
1365 if (max_payload >= skb->len)
1366 hdr->common.lf = ETH1394_HDR_LF_LF;
1367 bufhdr->words.word1 = htons(hdr->words.word1);
1368 bufhdr->words.word2 = htons(hdr->words.word2);
1369 bufhdr->words.word3 = htons(hdr->words.word3);
1370 bufhdr->words.word4 = 0;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return min(max_payload, skb->len);
1373}
1374
Stefan Richtere00f04a2007-03-18 12:23:11 +01001375static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct hpsb_packet *p;
1378
1379 p = hpsb_alloc_packet(0);
1380 if (p) {
1381 p->host = host;
1382 p->generation = get_hpsb_generation(host);
1383 p->type = hpsb_async;
1384 }
1385 return p;
1386}
1387
Stefan Richtere00f04a2007-03-18 12:23:11 +01001388static int ether1394_prep_write_packet(struct hpsb_packet *p,
1389 struct hpsb_host *host, nodeid_t node,
Stefan Richterefbeccf2007-04-02 02:12:32 +02001390 u64 addr, void *data, int tx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 p->node_id = node;
1393 p->data = NULL;
1394
1395 p->tcode = TCODE_WRITEB;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001396 p->header[1] = host->node_id << 16 | addr >> 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 p->header[2] = addr & 0xffffffff;
1398
1399 p->header_size = 16;
1400 p->expect_response = 1;
1401
1402 if (hpsb_get_tlabel(p)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001403 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -1;
1405 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001406 p->header[0] =
1407 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 p->header[3] = tx_len << 16;
1410 p->data_size = (tx_len + 3) & ~3;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001411 p->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 return 0;
1414}
1415
Stefan Richtere00f04a2007-03-18 12:23:11 +01001416static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1417 struct eth1394_priv *priv,
1418 struct sk_buff *skb, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 p->header_size = 4;
1421 p->tcode = TCODE_STREAM_DATA;
1422
Stefan Richterefbeccf2007-04-02 02:12:32 +02001423 p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
1424 TCODE_STREAM_DATA << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 p->data_size = length;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001426 p->data = (quadlet_t *)skb->data - 2;
1427 p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ETHER1394_GASP_SPECIFIER_ID_HI);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001429 p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
Ben Collins7136b802006-06-12 18:16:25 -04001430 ETHER1394_GASP_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 p->speed_code = priv->bc_sspd;
Stefan Richter21b2c562007-04-23 21:28:47 +02001433
1434 /* prevent hpsb_send_packet() from overriding our speed code */
1435 p->node_id = LOCAL_BUS | ALL_NODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Stefan Richtere00f04a2007-03-18 12:23:11 +01001438static void ether1394_free_packet(struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 if (packet->tcode != TCODE_STREAM_DATA)
1441 hpsb_free_tlabel(packet);
1442 hpsb_free_packet(packet);
1443}
1444
1445static void ether1394_complete_cb(void *__ptask);
1446
1447static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
1448{
1449 struct eth1394_priv *priv = ptask->priv;
1450 struct hpsb_packet *packet = NULL;
1451
1452 packet = ether1394_alloc_common_packet(priv->host);
1453 if (!packet)
1454 return -1;
1455
1456 if (ptask->tx_type == ETH1394_GASP) {
Stefan Richterefbeccf2007-04-02 02:12:32 +02001457 int length = tx_len + 2 * sizeof(quadlet_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
1460 } else if (ether1394_prep_write_packet(packet, priv->host,
1461 ptask->dest_node,
1462 ptask->addr, ptask->skb->data,
1463 tx_len)) {
1464 hpsb_free_packet(packet);
1465 return -1;
1466 }
1467
1468 ptask->packet = packet;
1469 hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
1470 ptask);
1471
1472 if (hpsb_send_packet(packet) < 0) {
1473 ether1394_free_packet(packet);
1474 return -1;
1475 }
1476
1477 return 0;
1478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/* Task function to be run when a datagram transmission is completed */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001481static void ether1394_dg_complete(struct packet_task *ptask, int fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 struct sk_buff *skb = ptask->skb;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001484 struct eth1394_priv *priv = netdev_priv(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 unsigned long flags;
1486
1487 /* Statistics */
1488 spin_lock_irqsave(&priv->lock, flags);
1489 if (fail) {
1490 priv->stats.tx_dropped++;
1491 priv->stats.tx_errors++;
1492 } else {
1493 priv->stats.tx_bytes += skb->len;
1494 priv->stats.tx_packets++;
1495 }
1496 spin_unlock_irqrestore(&priv->lock, flags);
1497
1498 dev_kfree_skb_any(skb);
1499 kmem_cache_free(packet_task_cache, ptask);
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/* Callback for when a packet has been sent and the status of that packet is
1503 * known */
1504static void ether1394_complete_cb(void *__ptask)
1505{
1506 struct packet_task *ptask = (struct packet_task *)__ptask;
1507 struct hpsb_packet *packet = ptask->packet;
1508 int fail = 0;
1509
1510 if (packet->tcode != TCODE_STREAM_DATA)
1511 fail = hpsb_packet_success(packet);
1512
1513 ether1394_free_packet(packet);
1514
1515 ptask->outstanding_pkts--;
1516 if (ptask->outstanding_pkts > 0 && !fail) {
1517 int tx_len;
1518
1519 /* Add the encapsulation header to the fragment */
1520 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
1521 &ptask->hdr);
1522 if (ether1394_send_packet(ptask, tx_len))
1523 ether1394_dg_complete(ptask, 1);
1524 } else {
1525 ether1394_dg_complete(ptask, fail);
1526 }
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/* Transmit a packet (called by kernel) */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001530static int ether1394_tx(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct eth1394hdr *eth;
1533 struct eth1394_priv *priv = netdev_priv(dev);
Ben Collins02f42132006-06-12 18:15:11 -04001534 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unsigned long flags;
1536 nodeid_t dest_node;
1537 eth1394_tx_type tx_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned int tx_len;
1539 unsigned int max_payload;
1540 u16 dg_size;
1541 u16 dgl;
1542 struct packet_task *ptask;
1543 struct eth1394_node_ref *node;
1544 struct eth1394_node_info *node_info = NULL;
1545
Stefan Richter53f374e2007-04-02 02:23:19 +02001546 ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC);
Stefan Richterfdc00922007-04-02 02:24:27 +02001547 if (ptask == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
1551 * it does not set our validity bit. We need to compensate for
1552 * that somewhere else, but not in eth1394. */
1553#if 0
Stefan Richterfdc00922007-04-02 02:24:27 +02001554 if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#endif
1557
Stefan Richter53f374e2007-04-02 02:23:19 +02001558 skb = skb_share_check(skb, GFP_ATOMIC);
Stefan Richterfdc00922007-04-02 02:24:27 +02001559 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Get rid of the fake eth1394 header, but save a pointer */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001563 eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 skb_pull(skb, ETH1394_HLEN);
1565
1566 proto = eth->h_proto;
1567 dg_size = skb->len;
1568
1569 /* Set the transmission type for the packet. ARP packets and IP
1570 * broadcast packets are sent via GASP. */
1571 if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
Ben Collins7136b802006-06-12 18:16:25 -04001572 proto == htons(ETH_P_ARP) ||
1573 (proto == htons(ETH_P_IP) &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001574 IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 tx_type = ETH1394_GASP;
1576 dest_node = LOCAL_BUS | ALL_NODES;
1577 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001578 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 dgl = priv->bc_dgl;
1580 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1581 priv->bc_dgl++;
1582 } else {
David S. Millerc20e3942006-10-29 16:14:55 -08001583 __be64 guid = get_unaligned((u64 *)eth->h_dest);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -08001586 be64_to_cpu(guid));
Stefan Richterfdc00922007-04-02 02:24:27 +02001587 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto fail;
Stefan Richterfdc00922007-04-02 02:24:27 +02001589
Stefan Richterefbeccf2007-04-02 02:12:32 +02001590 node_info =
1591 (struct eth1394_node_info *)node->ud->device.driver_data;
Stefan Richterfdc00922007-04-02 02:24:27 +02001592 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 dest_node = node->ud->ne->nodeid;
1596 max_payload = node_info->maxpayload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001597 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 dgl = node_info->dgl;
1600 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1601 node_info->dgl++;
1602 tx_type = ETH1394_WRREQ;
1603 }
1604
1605 /* If this is an ARP packet, convert it */
Ben Collins7136b802006-06-12 18:16:25 -04001606 if (proto == htons(ETH_P_ARP))
Stefan Richterefbeccf2007-04-02 02:12:32 +02001607 ether1394_arp_to_1394arp(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 ptask->hdr.words.word1 = 0;
1610 ptask->hdr.words.word2 = 0;
1611 ptask->hdr.words.word3 = 0;
1612 ptask->hdr.words.word4 = 0;
1613 ptask->skb = skb;
1614 ptask->priv = priv;
1615 ptask->tx_type = tx_type;
1616
1617 if (tx_type != ETH1394_GASP) {
1618 u64 addr;
1619
1620 spin_lock_irqsave(&priv->lock, flags);
1621 addr = node_info->fifo;
1622 spin_unlock_irqrestore(&priv->lock, flags);
1623
1624 ptask->addr = addr;
1625 ptask->dest_node = dest_node;
1626 }
1627
1628 ptask->tx_type = tx_type;
1629 ptask->max_payload = max_payload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001630 ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
1631 proto, &ptask->hdr, dg_size, dgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* Add the encapsulation header to the fragment */
1634 tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
1635 dev->trans_start = jiffies;
1636 if (ether1394_send_packet(ptask, tx_len))
1637 goto fail;
1638
1639 netif_wake_queue(dev);
Stefan Richterfdc00922007-04-02 02:24:27 +02001640 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641fail:
1642 if (ptask)
1643 kmem_cache_free(packet_task_cache, ptask);
1644
1645 if (skb != NULL)
1646 dev_kfree_skb(skb);
1647
Stefan Richterefbeccf2007-04-02 02:12:32 +02001648 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 priv->stats.tx_dropped++;
1650 priv->stats.tx_errors++;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001651 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 if (netif_queue_stopped(dev))
1654 netif_wake_queue(dev);
1655
Stefan Richterfdc00922007-04-02 02:24:27 +02001656 /*
1657 * FIXME: According to a patch from 2003-02-26, "returning non-zero
1658 * causes serious problems" here, allegedly. Before that patch,
1659 * -ERRNO was returned which is not appropriate under Linux 2.6.
1660 * Perhaps more needs to be done? Stop the queue in serious
1661 * conditions and restart it elsewhere?
1662 */
1663 /* return NETDEV_TX_BUSY; */
1664 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Stefan Richterefbeccf2007-04-02 02:12:32 +02001667static void ether1394_get_drvinfo(struct net_device *dev,
1668 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001670 strcpy(info->driver, driver_name);
1671 strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674static struct ethtool_ops ethtool_ops = {
1675 .get_drvinfo = ether1394_get_drvinfo
1676};
1677
Akinobu Mita809e9052007-04-21 18:36:26 +09001678static int __init ether1394_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Akinobu Mita809e9052007-04-21 18:36:26 +09001680 int err;
1681
Stefan Richterefbeccf2007-04-02 02:12:32 +02001682 packet_task_cache = kmem_cache_create("packet_task",
1683 sizeof(struct packet_task),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 0, 0, NULL, NULL);
Akinobu Mita809e9052007-04-21 18:36:26 +09001685 if (!packet_task_cache)
1686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 hpsb_register_highlevel(&eth1394_highlevel);
Akinobu Mita809e9052007-04-21 18:36:26 +09001689 err = hpsb_register_protocol(&eth1394_proto_driver);
1690 if (err) {
1691 hpsb_unregister_highlevel(&eth1394_highlevel);
1692 kmem_cache_destroy(packet_task_cache);
1693 }
1694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Akinobu Mita809e9052007-04-21 18:36:26 +09001697static void __exit ether1394_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 hpsb_unregister_protocol(&eth1394_proto_driver);
1700 hpsb_unregister_highlevel(&eth1394_highlevel);
1701 kmem_cache_destroy(packet_task_cache);
1702}
1703
1704module_init(ether1394_init_module);
1705module_exit(ether1394_exit_module);