blob: bd67c862099e05121d2d086a080c4bf16ff4879a [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 */
478 for (i = 0; i < host->node_count; i++)
Ben Collins647dcb52006-06-12 18:12:37 -0400479 if (max_speed > host->speed[i])
480 max_speed = host->speed[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 priv->bc_sspd = max_speed;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (set_mtu) {
Stefan Richter17bab402007-04-03 23:55:40 +0200484 /* Use the RFC 2734 default 1500 octets or the maximum payload
485 * as initial MTU */
486 dev->mtu = min(1500, ether1394_max_mtu(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* Set our hardware address while we're at it */
David S. Millerc20e3942006-10-29 16:14:55 -0800489 memcpy(dev->dev_addr, &guid, sizeof(u64));
490 memset(dev->broadcast, 0xff, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Stefan Richterefbeccf2007-04-02 02:12:32 +0200493 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Stefan Richterefbeccf2007-04-02 02:12:32 +0200496static void ether1394_init_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dev->open = ether1394_open;
499 dev->stop = ether1394_stop;
500 dev->hard_start_xmit = ether1394_tx;
501 dev->get_stats = ether1394_stats;
502 dev->tx_timeout = ether1394_tx_timeout;
503 dev->change_mtu = ether1394_change_mtu;
504
505 dev->hard_header = ether1394_header;
506 dev->rebuild_header = ether1394_rebuild_header;
507 dev->hard_header_cache = ether1394_header_cache;
508 dev->header_cache_update= ether1394_header_cache_update;
509 dev->hard_header_parse = ether1394_header_parse;
Stefan Richter01590d22007-04-02 02:20:37 +0200510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 SET_ETHTOOL_OPS(dev, &ethtool_ops);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dev->watchdog_timeo = ETHER1394_TIMEOUT;
514 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
515 dev->features = NETIF_F_HIGHDMA;
516 dev->addr_len = ETH1394_ALEN;
517 dev->hard_header_len = ETH1394_HLEN;
518 dev->type = ARPHRD_IEEE1394;
519
Stefan Richter01590d22007-04-02 02:20:37 +0200520 /* FIXME: This value was copied from ether_setup(). Is it too much? */
521 dev->tx_queue_len = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/*
525 * This function is called every time a card is found. It is generally called
526 * when the module is installed. This is where we add all of our ethernet
527 * devices. One for each host.
528 */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200529static void ether1394_add_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct eth1394_host_info *hi = NULL;
532 struct net_device *dev = NULL;
533 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 u64 fifo_addr;
535
Stefan Richter70093cf2007-03-27 01:36:50 +0200536 if (hpsb_config_rom_ip1394_add(host) != 0) {
537 ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
Stefan Richter70093cf2007-03-27 01:36:50 +0200539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Ben Collins67372312006-06-12 18:15:31 -0400541 fifo_addr = hpsb_allocate_and_register_addrspace(
542 &eth1394_highlevel, host, &addr_ops,
543 ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN,
544 CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE);
Stefan Richter157188c2007-02-10 23:56:38 +0100545 if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
546 ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n");
Stefan Richter70093cf2007-03-27 01:36:50 +0200547 hpsb_config_rom_ip1394_remove(host);
Stefan Richter157188c2007-02-10 23:56:38 +0100548 return;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Stefan Richter01590d22007-04-02 02:20:37 +0200551 dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (dev == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200553 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 SET_MODULE_OWNER(dev);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100558#if 0
559 /* FIXME - Is this the correct parent device anyway? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 SET_NETDEV_DEV(dev, &host->device);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 INIT_LIST_HEAD(&priv->ip_node_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_lock_init(&priv->lock);
566 priv->host = host;
567 priv->local_fifo = fifo_addr;
568
569 hi = hpsb_create_hostinfo(&eth1394_highlevel, host, sizeof(*hi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200571 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Stefan Richter01590d22007-04-02 02:20:37 +0200575 ether1394_reset_priv(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Stefan Richter5009d262007-04-02 02:15:53 +0200577 if (register_netdev(dev)) {
578 ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out;
580 }
581
Stefan Richter5009d262007-04-02 02:15:53 +0200582 ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n",
583 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 hi->host = host;
586 hi->dev = dev;
587
588 /* Ignore validity in hopes that it will be set in the future. It'll
589 * be checked when the eth device is opened. */
590 priv->broadcast_channel = host->csr.broadcast_channel & 0x3f;
591
Stefan Richter5009d262007-04-02 02:15:53 +0200592 ether1394_recv_init(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594out:
Stefan Richter157188c2007-02-10 23:56:38 +0100595 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 free_netdev(dev);
597 if (hi)
598 hpsb_destroy_hostinfo(&eth1394_highlevel, host);
Stefan Richter157188c2007-02-10 23:56:38 +0100599 hpsb_unregister_addrspace(&eth1394_highlevel, host, fifo_addr);
Stefan Richter70093cf2007-03-27 01:36:50 +0200600 hpsb_config_rom_ip1394_remove(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/* Remove a card from our list */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200604static void ether1394_remove_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct eth1394_host_info *hi;
Stefan Richter2cd556a2007-02-10 23:57:57 +0100607 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100610 if (!hi)
611 return;
612 priv = netdev_priv(hi->dev);
613 hpsb_unregister_addrspace(&eth1394_highlevel, host, priv->local_fifo);
Stefan Richter70093cf2007-03-27 01:36:50 +0200614 hpsb_config_rom_ip1394_remove(host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100615 if (priv->iso)
616 hpsb_iso_shutdown(priv->iso);
617 unregister_netdev(hi->dev);
618 free_netdev(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Stefan Richterefbeccf2007-04-02 02:12:32 +0200621/* A bus reset happened */
622static void ether1394_host_reset(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct eth1394_host_info *hi;
625 struct eth1394_priv *priv;
626 struct net_device *dev;
627 struct list_head *lh, *n;
628 struct eth1394_node_ref *node;
629 struct eth1394_node_info *node_info;
630 unsigned long flags;
631
632 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
633
634 /* This can happen for hosts that we don't use */
Stefan Richter2cd556a2007-02-10 23:57:57 +0100635 if (!hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return;
637
638 dev = hi->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200639 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Stefan Richterefbeccf2007-04-02 02:12:32 +0200641 /* Reset our private host data, but not our MTU */
642 netif_stop_queue(dev);
643 ether1394_reset_priv(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 list_for_each_entry(node, &priv->ip_node_list, list) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200646 node_info = node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 spin_lock_irqsave(&node_info->pdg.lock, flags);
649
Stefan Richterefbeccf2007-04-02 02:12:32 +0200650 list_for_each_safe(lh, n, &node_info->pdg.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 purge_partial_datagram(lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 INIT_LIST_HEAD(&(node_info->pdg.list));
654 node_info->pdg.sz = 0;
655
656 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
657 }
658
Stefan Richterefbeccf2007-04-02 02:12:32 +0200659 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/******************************************
663 * HW Header net device functions
664 ******************************************/
665/* These functions have been adapted from net/ethernet/eth.c */
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/* Create a fake MAC header for an arbitrary protocol layer.
668 * saddr=NULL means use device source address
669 * daddr=NULL means leave destination address (eg unresolved arp). */
670static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
671 unsigned short type, void *daddr, void *saddr,
672 unsigned len)
673{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200674 struct eth1394hdr *eth =
675 (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 eth->h_proto = htons(type);
678
Stefan Richterefbeccf2007-04-02 02:12:32 +0200679 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 memset(eth->h_dest, 0, dev->addr_len);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200681 return dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 if (daddr) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200685 memcpy(eth->h_dest, daddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return dev->hard_header_len;
687 }
688
689 return -dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/* Rebuild the faked MAC header. This is called after an ARP
693 * (or in future other address resolution) has completed on this
694 * sk_buff. We now let ARP fill in the other fields.
695 *
696 * This routine CANNOT use cached dst->neigh!
697 * Really, it is used only when dst->neigh is wrong.
698 */
699static int ether1394_rebuild_header(struct sk_buff *skb)
700{
701 struct eth1394hdr *eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Stefan Richter599bba92007-04-02 02:19:02 +0200703 if (eth->h_proto == htons(ETH_P_IP))
Stefan Richterefbeccf2007-04-02 02:12:32 +0200704 return arp_find((unsigned char *)&eth->h_dest, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Stefan Richter599bba92007-04-02 02:19:02 +0200706 ETH1394_PRINT(KERN_DEBUG, skb->dev->name,
707 "unable to resolve type %04x addresses\n",
708 ntohs(eth->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
712static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr)
713{
714 struct net_device *dev = skb->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
717 return ETH1394_ALEN;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
721{
722 unsigned short type = hh->hh_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct net_device *dev = neigh->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200724 struct eth1394hdr *eth =
725 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ben Collins7136b802006-06-12 18:16:25 -0400727 if (type == htons(ETH_P_802_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 eth->h_proto = type;
731 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
732
733 hh->hh_len = ETH1394_HLEN;
734 return 0;
735}
736
737/* Called by Address Resolution module to notify changes in address. */
738static void ether1394_header_cache_update(struct hh_cache *hh,
739 struct net_device *dev,
740 unsigned char * haddr)
741{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200742 memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/******************************************
746 * Datagram reception code
747 ******************************************/
748
749/* Copied from net/ethernet/eth.c */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100750static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct eth1394hdr *eth;
753 unsigned char *rawp;
754
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700755 skb_reset_mac_header(skb);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200756 skb_pull(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 eth = eth1394_hdr(skb);
758
759 if (*eth->h_dest & 1) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200760 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 skb->pkt_type = PACKET_BROADCAST;
762#if 0
763 else
764 skb->pkt_type = PACKET_MULTICAST;
765#endif
766 } else {
767 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
768 skb->pkt_type = PACKET_OTHERHOST;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Stefan Richterefbeccf2007-04-02 02:12:32 +0200771 if (ntohs(eth->h_proto) >= 1536)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return eth->h_proto;
773
774 rawp = skb->data;
775
Stefan Richterefbeccf2007-04-02 02:12:32 +0200776 if (*(unsigned short *)rawp == 0xFFFF)
777 return htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Stefan Richterefbeccf2007-04-02 02:12:32 +0200779 return htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/* Parse an encapsulated IP1394 header into an ethernet frame packet.
783 * We also perform ARP translation here, if need be. */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100784static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev,
785 nodeid_t srcid, nodeid_t destid,
786 u16 ether_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct eth1394_priv *priv = netdev_priv(dev);
789 u64 dest_hw;
790 unsigned short ret = 0;
791
Stefan Richterefbeccf2007-04-02 02:12:32 +0200792 /* Setup our hw addresses. We use these to build the ethernet header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (destid == (LOCAL_BUS | ALL_NODES))
794 dest_hw = ~0ULL; /* broadcast */
795 else
Stefan Richterefbeccf2007-04-02 02:12:32 +0200796 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 priv->host->csr.guid_lo);
798
799 /* If this is an ARP packet, convert it. First, we want to make
800 * use of some of the fields, since they tell us a little bit
801 * about the sending machine. */
Ben Collins7136b802006-06-12 18:16:25 -0400802 if (ether_type == htons(ETH_P_ARP)) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200803 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct arphdr *arp = (struct arphdr *)skb->data;
805 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
806 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
Stefan Richterefbeccf2007-04-02 02:12:32 +0200807 ntohl(arp1394->fifo_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 u8 max_rec = min(priv->host->csr.max_rec,
809 (u8)(arp1394->max_rec));
810 int sspd = arp1394->sspd;
811 u16 maxpayload;
812 struct eth1394_node_ref *node;
813 struct eth1394_node_info *node_info;
David S. Millerc20e3942006-10-29 16:14:55 -0800814 __be64 guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* Sanity check. MacOSX seems to be sending us 131 in this
817 * field (atleast on my Panther G5). Not sure why. */
818 if (sspd > 5 || sspd < 0)
819 sspd = 0;
820
Stefan Richterefbeccf2007-04-02 02:12:32 +0200821 maxpayload = min(eth1394_speedto_maxpayload[sspd],
822 (u16)(1 << (max_rec + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
David S. Millerc20e3942006-10-29 16:14:55 -0800824 guid = get_unaligned(&arp1394->s_uniq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -0800826 be64_to_cpu(guid));
Stefan Richterefbeccf2007-04-02 02:12:32 +0200827 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Stefan Richterefbeccf2007-04-02 02:12:32 +0200830 node_info =
831 (struct eth1394_node_info *)node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Update our speed/payload/fifo_offset table */
834 node_info->maxpayload = maxpayload;
835 node_info->sspd = sspd;
836 node_info->fifo = fifo_addr;
837
838 /* Now that we're done with the 1394 specific stuff, we'll
839 * need to alter some of the data. Believe it or not, all
840 * that needs to be done is sender_IP_address needs to be
841 * moved, the destination hardware address get stuffed
842 * in and the hardware address length set to 8.
843 *
844 * IMPORTANT: The code below overwrites 1394 specific data
845 * needed above so keep the munging of the data for the
846 * higher level IP stack last. */
847
848 arp->ar_hln = 8;
849 arp_ptr += arp->ar_hln; /* skip over sender unique id */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200850 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 arp_ptr += arp->ar_pln; /* skip over sender IP addr */
852
Ben Collins02f42132006-06-12 18:15:11 -0400853 if (arp->ar_op == htons(ARPOP_REQUEST))
David S. Millerc20e3942006-10-29 16:14:55 -0800854 memset(arp_ptr, 0, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 else
David S. Millerc20e3942006-10-29 16:14:55 -0800856 memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 /* Now add the ethernet header. */
Ben Collins7136b802006-06-12 18:16:25 -0400860 if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
861 skb->len) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ret = ether1394_type_trans(skb, dev);
863
864 return ret;
865}
866
Stefan Richtere00f04a2007-03-18 12:23:11 +0100867static int fragment_overlap(struct list_head *frag_list, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct fragment_info *fi;
Stefan Richter2e2173d2007-04-02 02:21:46 +0200870 int end = offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Stefan Richter2e2173d2007-04-02 02:21:46 +0200872 list_for_each_entry(fi, frag_list, list)
873 if (offset < fi->offset + fi->len && end > fi->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 1;
Stefan Richter2e2173d2007-04-02 02:21:46 +0200875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877}
878
Stefan Richtere00f04a2007-03-18 12:23:11 +0100879static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct partial_datagram *pd;
882
Stefan Richterefbeccf2007-04-02 02:12:32 +0200883 list_for_each_entry(pd, pdgl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (pd->dgl == dgl)
885 return &pd->list;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return NULL;
888}
889
890/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100891static int new_fragment(struct list_head *frag_info, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct list_head *lh;
894 struct fragment_info *fi, *fi2, *new;
895
896 list_for_each(lh, frag_info) {
897 fi = list_entry(lh, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200898 if (fi->offset + fi->len == offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* The new fragment can be tacked on to the end */
900 fi->len += len;
901 /* Did the new fragment plug a hole? */
902 fi2 = list_entry(lh->next, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200903 if (fi->offset + fi->len == fi2->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* glue fragments together */
905 fi->len += fi2->len;
906 list_del(lh->next);
907 kfree(fi2);
908 }
909 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200910 } else if (offset + len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* The new fragment can be tacked on to the beginning */
912 fi->offset = offset;
913 fi->len += len;
914 /* Did the new fragment plug a hole? */
915 fi2 = list_entry(lh->prev, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200916 if (fi2->offset + fi2->len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* glue fragments together */
918 fi2->len += fi->len;
919 list_del(lh);
920 kfree(fi);
921 }
922 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200923 } else if (offset > fi->offset + fi->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200925 } else if (offset + len < fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 lh = lh->prev;
927 break;
928 }
929 }
930
Stefan Richter85511582005-11-07 06:31:45 -0500931 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!new)
933 return -ENOMEM;
934
935 new->offset = offset;
936 new->len = len;
937
938 list_add(&new->list, lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
940}
941
Stefan Richtere00f04a2007-03-18 12:23:11 +0100942static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl,
943 int dgl, int dg_size, char *frag_buf,
944 int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct partial_datagram *new;
947
Stefan Richter85511582005-11-07 06:31:45 -0500948 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!new)
950 return -ENOMEM;
951
952 INIT_LIST_HEAD(&new->frag_info);
953
954 if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
955 kfree(new);
956 return -ENOMEM;
957 }
958
959 new->dgl = dgl;
960 new->dg_size = dg_size;
961
962 new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
963 if (!new->skb) {
964 struct fragment_info *fi = list_entry(new->frag_info.next,
965 struct fragment_info,
966 list);
967 kfree(fi);
968 kfree(new);
969 return -ENOMEM;
970 }
971
972 skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
973 new->pbuf = skb_put(new->skb, dg_size);
974 memcpy(new->pbuf + frag_off, frag_buf, frag_len);
975
976 list_add(&new->list, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978}
979
Stefan Richtere00f04a2007-03-18 12:23:11 +0100980static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
981 char *frag_buf, int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200983 struct partial_datagram *pd =
984 list_entry(lh, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Stefan Richterefbeccf2007-04-02 02:12:32 +0200986 if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
990
991 /* Move list entry to beginnig of list so that oldest partial
992 * datagrams percolate to the end of the list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700993 list_move(lh, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
Stefan Richtere00f04a2007-03-18 12:23:11 +0100997static int is_datagram_complete(struct list_head *lh, int dg_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Stefan Richtere00f04a2007-03-18 12:23:11 +0100999 struct partial_datagram *pd;
1000 struct fragment_info *fi;
1001
1002 pd = list_entry(lh, struct partial_datagram, list);
1003 fi = list_entry(pd->frag_info.next, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 return (fi->len == dg_size);
1006}
1007
1008/* Packet reception. We convert the IP1394 encapsulation header to an
1009 * ethernet header, and fill it with some of our other fields. This is
1010 * an incoming packet from the 1394 bus. */
1011static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
1012 char *buf, int len)
1013{
1014 struct sk_buff *skb;
1015 unsigned long flags;
1016 struct eth1394_priv *priv = netdev_priv(dev);
1017 union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
1018 u16 ether_type = 0; /* initialized to clear warning */
1019 int hdr_len;
1020 struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
1021 struct eth1394_node_info *node_info;
1022
1023 if (!ud) {
1024 struct eth1394_node_ref *node;
1025 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
Stefan Richter09939872007-04-02 02:22:21 +02001026 if (unlikely(!node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
1028 "lookup failure: " NODE_BUS_FMT,
1029 NODE_BUS_ARGS(priv->host, srcid));
1030 priv->stats.rx_dropped++;
1031 return -1;
1032 }
1033 ud = node->ud;
1034
1035 priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
1036 }
1037
Stefan Richterefbeccf2007-04-02 02:12:32 +02001038 node_info = (struct eth1394_node_info *)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* First, did we receive a fragmented or unfragmented datagram? */
1041 hdr->words.word1 = ntohs(hdr->words.word1);
1042
1043 hdr_len = hdr_type_len[hdr->common.lf];
1044
1045 if (hdr->common.lf == ETH1394_HDR_LF_UF) {
1046 /* An unfragmented datagram has been received by the ieee1394
1047 * bus. Build an skbuff around it so we can pass it to the
1048 * high level network layer. */
1049
1050 skb = dev_alloc_skb(len + dev->hard_header_len + 15);
Stefan Richter09939872007-04-02 02:22:21 +02001051 if (unlikely(!skb)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001052 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 priv->stats.rx_dropped++;
1054 return -1;
1055 }
1056 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001057 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
1058 len - hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ether_type = hdr->uf.ether_type;
1060 } else {
1061 /* A datagram fragment has been received, now the fun begins. */
1062
1063 struct list_head *pdgl, *lh;
1064 struct partial_datagram *pd;
1065 int fg_off;
1066 int fg_len = len - hdr_len;
1067 int dg_size;
1068 int dgl;
1069 int retval;
1070 struct pdg_list *pdg = &(node_info->pdg);
1071
1072 hdr->words.word3 = ntohs(hdr->words.word3);
1073 /* The 4th header word is reserved so no need to do ntohs() */
1074
1075 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1076 ether_type = hdr->ff.ether_type;
1077 dgl = hdr->ff.dgl;
1078 dg_size = hdr->ff.dg_size + 1;
1079 fg_off = 0;
1080 } else {
1081 hdr->words.word2 = ntohs(hdr->words.word2);
1082 dgl = hdr->sf.dgl;
1083 dg_size = hdr->sf.dg_size + 1;
1084 fg_off = hdr->sf.fg_off;
1085 }
1086 spin_lock_irqsave(&pdg->lock, flags);
1087
1088 pdgl = &(pdg->list);
1089 lh = find_partial_datagram(pdgl, dgl);
1090
1091 if (lh == NULL) {
1092 while (pdg->sz >= max_partial_datagrams) {
1093 /* remove the oldest */
1094 purge_partial_datagram(pdgl->prev);
1095 pdg->sz--;
1096 }
1097
1098 retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
1099 buf + hdr_len, fg_off,
1100 fg_len);
1101 if (retval < 0) {
1102 spin_unlock_irqrestore(&pdg->lock, flags);
1103 goto bad_proto;
1104 }
1105 pdg->sz++;
1106 lh = find_partial_datagram(pdgl, dgl);
1107 } else {
1108 struct partial_datagram *pd;
1109
1110 pd = list_entry(lh, struct partial_datagram, list);
1111
1112 if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
1113 /* Overlapping fragments, obliterate old
1114 * datagram and start new one. */
1115 purge_partial_datagram(lh);
1116 retval = new_partial_datagram(dev, pdgl, dgl,
1117 dg_size,
1118 buf + hdr_len,
1119 fg_off, fg_len);
1120 if (retval < 0) {
1121 pdg->sz--;
1122 spin_unlock_irqrestore(&pdg->lock, flags);
1123 goto bad_proto;
1124 }
1125 } else {
1126 retval = update_partial_datagram(pdgl, lh,
1127 buf + hdr_len,
1128 fg_off, fg_len);
1129 if (retval < 0) {
1130 /* Couldn't save off fragment anyway
1131 * so might as well obliterate the
1132 * datagram now. */
1133 purge_partial_datagram(lh);
1134 pdg->sz--;
1135 spin_unlock_irqrestore(&pdg->lock, flags);
1136 goto bad_proto;
1137 }
1138 } /* fragment overlap */
1139 } /* new datagram or add to existing one */
1140
1141 pd = list_entry(lh, struct partial_datagram, list);
1142
Stefan Richterefbeccf2007-04-02 02:12:32 +02001143 if (hdr->common.lf == ETH1394_HDR_LF_FF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 pd->ether_type = ether_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (is_datagram_complete(lh, dg_size)) {
1147 ether_type = pd->ether_type;
1148 pdg->sz--;
1149 skb = skb_get(pd->skb);
1150 purge_partial_datagram(lh);
1151 spin_unlock_irqrestore(&pdg->lock, flags);
1152 } else {
1153 /* Datagram is not complete, we're done for the
1154 * moment. */
1155 spin_unlock_irqrestore(&pdg->lock, flags);
1156 return 0;
1157 }
1158 } /* unframgented datagram or fragmented one */
1159
1160 /* Write metadata, and then pass to the receive level */
1161 skb->dev = dev;
1162 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
1163
1164 /* Parse the encapsulation header. This actually does the job of
1165 * converting to an ethernet frame header, aswell as arp
1166 * conversion if needed. ARP conversion is easier in this
1167 * direction, since we are using ethernet as our backend. */
1168 skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
1169 ether_type);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 spin_lock_irqsave(&priv->lock, flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!skb->protocol) {
1174 priv->stats.rx_errors++;
1175 priv->stats.rx_dropped++;
1176 dev_kfree_skb_any(skb);
1177 goto bad_proto;
1178 }
1179
1180 if (netif_rx(skb) == NET_RX_DROP) {
1181 priv->stats.rx_errors++;
1182 priv->stats.rx_dropped++;
1183 goto bad_proto;
1184 }
1185
1186 /* Statistics */
1187 priv->stats.rx_packets++;
1188 priv->stats.rx_bytes += skb->len;
1189
1190bad_proto:
1191 if (netif_queue_stopped(dev))
1192 netif_wake_queue(dev);
1193 spin_unlock_irqrestore(&priv->lock, flags);
1194
1195 dev->last_rx = jiffies;
1196
1197 return 0;
1198}
1199
1200static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
1201 quadlet_t *data, u64 addr, size_t len, u16 flags)
1202{
1203 struct eth1394_host_info *hi;
1204
1205 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
Stefan Richter09939872007-04-02 02:22:21 +02001206 if (unlikely(!hi)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001207 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1208 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return RCODE_ADDRESS_ERROR;
1210 }
1211
1212 if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
1213 return RCODE_ADDRESS_ERROR;
1214 else
1215 return RCODE_COMPLETE;
1216}
1217
1218static void ether1394_iso(struct hpsb_iso *iso)
1219{
1220 quadlet_t *data;
1221 char *buf;
1222 struct eth1394_host_info *hi;
1223 struct net_device *dev;
1224 struct eth1394_priv *priv;
1225 unsigned int len;
1226 u32 specifier_id;
1227 u16 source_id;
1228 int i;
1229 int nready;
1230
1231 hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
Stefan Richter09939872007-04-02 02:22:21 +02001232 if (unlikely(!hi)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001233 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1234 iso->host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return;
1236 }
1237
1238 dev = hi->dev;
1239
1240 nready = hpsb_iso_n_ready(iso);
1241 for (i = 0; i < nready; i++) {
1242 struct hpsb_iso_packet_info *info =
1243 &iso->infos[(iso->first_packet + i) % iso->buf_packets];
Stefan Richterefbeccf2007-04-02 02:12:32 +02001244 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* skip over GASP header */
1247 buf = (char *)data + 8;
1248 len = info->len - 8;
1249
Stefan Richterefbeccf2007-04-02 02:12:32 +02001250 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
1251 (be32_to_cpu(data[1]) & 0xff000000) >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 source_id = be32_to_cpu(data[0]) >> 16;
1253
1254 priv = netdev_priv(dev);
1255
Stefan Richterefbeccf2007-04-02 02:12:32 +02001256 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
1257 || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* This packet is not for us */
1259 continue;
1260 }
1261 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
1262 buf, len);
1263 }
1264
1265 hpsb_iso_recv_release_packets(iso, i);
1266
1267 dev->last_rx = jiffies;
1268}
1269
1270/******************************************
1271 * Datagram transmission code
1272 ******************************************/
1273
1274/* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
1275 * arphdr) is the same format as the ip1394 header, so they overlap. The rest
1276 * needs to be munged a bit. The remainder of the arphdr is formatted based
1277 * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to
1278 * judge.
1279 *
1280 * Now that the EUI is used for the hardware address all we need to do to make
1281 * this work for 1394 is to insert 2 quadlets that contain max_rec size,
1282 * speed, and unicast FIFO address information between the sender_unique_id
1283 * and the IP addresses.
1284 */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001285static void ether1394_arp_to_1394arp(struct sk_buff *skb,
1286 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct eth1394_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct arphdr *arp = (struct arphdr *)skb->data;
1290 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1291 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 arp1394->hw_addr_len = 16;
1294 arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN);
1295 arp1394->max_rec = priv->host->csr.max_rec;
1296 arp1394->sspd = priv->host->csr.lnk_spd;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001297 arp1394->fifo_hi = htons(priv->local_fifo >> 32);
1298 arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/* We need to encapsulate the standard header with our own. We use the
1302 * ethernet header's proto for our own. */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001303static unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
1304 __be16 proto,
1305 union eth1394_hdr *hdr,
1306 u16 dg_size, u16 dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001308 unsigned int adj_max_payload =
1309 max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* Does it all fit in one packet? */
1312 if (dg_size <= adj_max_payload) {
1313 hdr->uf.lf = ETH1394_HDR_LF_UF;
1314 hdr->uf.ether_type = proto;
1315 } else {
1316 hdr->ff.lf = ETH1394_HDR_LF_FF;
1317 hdr->ff.ether_type = proto;
1318 hdr->ff.dg_size = dg_size - 1;
1319 hdr->ff.dgl = dgl;
1320 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
1321 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001322 return (dg_size + adj_max_payload - 1) / adj_max_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Stefan Richtere00f04a2007-03-18 12:23:11 +01001325static unsigned int ether1394_encapsulate(struct sk_buff *skb,
1326 unsigned int max_payload,
1327 union eth1394_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 union eth1394_hdr *bufhdr;
1330 int ftype = hdr->common.lf;
1331 int hdrsz = hdr_type_len[ftype];
1332 unsigned int adj_max_payload = max_payload - hdrsz;
1333
Stefan Richterefbeccf2007-04-02 02:12:32 +02001334 switch (ftype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case ETH1394_HDR_LF_UF:
1336 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1337 bufhdr->words.word1 = htons(hdr->words.word1);
1338 bufhdr->words.word2 = hdr->words.word2;
1339 break;
1340
1341 case ETH1394_HDR_LF_FF:
1342 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1343 bufhdr->words.word1 = htons(hdr->words.word1);
1344 bufhdr->words.word2 = hdr->words.word2;
1345 bufhdr->words.word3 = htons(hdr->words.word3);
1346 bufhdr->words.word4 = 0;
1347
1348 /* Set frag type here for future interior fragments */
1349 hdr->common.lf = ETH1394_HDR_LF_IF;
1350 hdr->sf.fg_off = 0;
1351 break;
1352
1353 default:
1354 hdr->sf.fg_off += adj_max_payload;
1355 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
1356 if (max_payload >= skb->len)
1357 hdr->common.lf = ETH1394_HDR_LF_LF;
1358 bufhdr->words.word1 = htons(hdr->words.word1);
1359 bufhdr->words.word2 = htons(hdr->words.word2);
1360 bufhdr->words.word3 = htons(hdr->words.word3);
1361 bufhdr->words.word4 = 0;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return min(max_payload, skb->len);
1364}
1365
Stefan Richtere00f04a2007-03-18 12:23:11 +01001366static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct hpsb_packet *p;
1369
1370 p = hpsb_alloc_packet(0);
1371 if (p) {
1372 p->host = host;
1373 p->generation = get_hpsb_generation(host);
1374 p->type = hpsb_async;
1375 }
1376 return p;
1377}
1378
Stefan Richtere00f04a2007-03-18 12:23:11 +01001379static int ether1394_prep_write_packet(struct hpsb_packet *p,
1380 struct hpsb_host *host, nodeid_t node,
Stefan Richterefbeccf2007-04-02 02:12:32 +02001381 u64 addr, void *data, int tx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 p->node_id = node;
1384 p->data = NULL;
1385
1386 p->tcode = TCODE_WRITEB;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001387 p->header[1] = host->node_id << 16 | addr >> 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 p->header[2] = addr & 0xffffffff;
1389
1390 p->header_size = 16;
1391 p->expect_response = 1;
1392
1393 if (hpsb_get_tlabel(p)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001394 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -1;
1396 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001397 p->header[0] =
1398 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 p->header[3] = tx_len << 16;
1401 p->data_size = (tx_len + 3) & ~3;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001402 p->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 0;
1405}
1406
Stefan Richtere00f04a2007-03-18 12:23:11 +01001407static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1408 struct eth1394_priv *priv,
1409 struct sk_buff *skb, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 p->header_size = 4;
1412 p->tcode = TCODE_STREAM_DATA;
1413
Stefan Richterefbeccf2007-04-02 02:12:32 +02001414 p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
1415 TCODE_STREAM_DATA << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 p->data_size = length;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001417 p->data = (quadlet_t *)skb->data - 2;
1418 p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ETHER1394_GASP_SPECIFIER_ID_HI);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001420 p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
Ben Collins7136b802006-06-12 18:16:25 -04001421 ETHER1394_GASP_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* Setting the node id to ALL_NODES (not LOCAL_BUS | ALL_NODES)
1424 * prevents hpsb_send_packet() from setting the speed to an arbitrary
1425 * value based on packet->node_id if packet->node_id is not set. */
1426 p->node_id = ALL_NODES;
1427 p->speed_code = priv->bc_sspd;
1428}
1429
Stefan Richtere00f04a2007-03-18 12:23:11 +01001430static void ether1394_free_packet(struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 if (packet->tcode != TCODE_STREAM_DATA)
1433 hpsb_free_tlabel(packet);
1434 hpsb_free_packet(packet);
1435}
1436
1437static void ether1394_complete_cb(void *__ptask);
1438
1439static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
1440{
1441 struct eth1394_priv *priv = ptask->priv;
1442 struct hpsb_packet *packet = NULL;
1443
1444 packet = ether1394_alloc_common_packet(priv->host);
1445 if (!packet)
1446 return -1;
1447
1448 if (ptask->tx_type == ETH1394_GASP) {
Stefan Richterefbeccf2007-04-02 02:12:32 +02001449 int length = tx_len + 2 * sizeof(quadlet_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
1452 } else if (ether1394_prep_write_packet(packet, priv->host,
1453 ptask->dest_node,
1454 ptask->addr, ptask->skb->data,
1455 tx_len)) {
1456 hpsb_free_packet(packet);
1457 return -1;
1458 }
1459
1460 ptask->packet = packet;
1461 hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
1462 ptask);
1463
1464 if (hpsb_send_packet(packet) < 0) {
1465 ether1394_free_packet(packet);
1466 return -1;
1467 }
1468
1469 return 0;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/* Task function to be run when a datagram transmission is completed */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001473static void ether1394_dg_complete(struct packet_task *ptask, int fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct sk_buff *skb = ptask->skb;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001476 struct eth1394_priv *priv = netdev_priv(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 unsigned long flags;
1478
1479 /* Statistics */
1480 spin_lock_irqsave(&priv->lock, flags);
1481 if (fail) {
1482 priv->stats.tx_dropped++;
1483 priv->stats.tx_errors++;
1484 } else {
1485 priv->stats.tx_bytes += skb->len;
1486 priv->stats.tx_packets++;
1487 }
1488 spin_unlock_irqrestore(&priv->lock, flags);
1489
1490 dev_kfree_skb_any(skb);
1491 kmem_cache_free(packet_task_cache, ptask);
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/* Callback for when a packet has been sent and the status of that packet is
1495 * known */
1496static void ether1394_complete_cb(void *__ptask)
1497{
1498 struct packet_task *ptask = (struct packet_task *)__ptask;
1499 struct hpsb_packet *packet = ptask->packet;
1500 int fail = 0;
1501
1502 if (packet->tcode != TCODE_STREAM_DATA)
1503 fail = hpsb_packet_success(packet);
1504
1505 ether1394_free_packet(packet);
1506
1507 ptask->outstanding_pkts--;
1508 if (ptask->outstanding_pkts > 0 && !fail) {
1509 int tx_len;
1510
1511 /* Add the encapsulation header to the fragment */
1512 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
1513 &ptask->hdr);
1514 if (ether1394_send_packet(ptask, tx_len))
1515 ether1394_dg_complete(ptask, 1);
1516 } else {
1517 ether1394_dg_complete(ptask, fail);
1518 }
1519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/* Transmit a packet (called by kernel) */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001522static int ether1394_tx(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct eth1394hdr *eth;
1525 struct eth1394_priv *priv = netdev_priv(dev);
Ben Collins02f42132006-06-12 18:15:11 -04001526 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 unsigned long flags;
1528 nodeid_t dest_node;
1529 eth1394_tx_type tx_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 unsigned int tx_len;
1531 unsigned int max_payload;
1532 u16 dg_size;
1533 u16 dgl;
1534 struct packet_task *ptask;
1535 struct eth1394_node_ref *node;
1536 struct eth1394_node_info *node_info = NULL;
1537
Stefan Richter53f374e2007-04-02 02:23:19 +02001538 ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC);
Stefan Richterfdc00922007-04-02 02:24:27 +02001539 if (ptask == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
1543 * it does not set our validity bit. We need to compensate for
1544 * that somewhere else, but not in eth1394. */
1545#if 0
Stefan Richterfdc00922007-04-02 02:24:27 +02001546 if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548#endif
1549
Stefan Richter53f374e2007-04-02 02:23:19 +02001550 skb = skb_share_check(skb, GFP_ATOMIC);
Stefan Richterfdc00922007-04-02 02:24:27 +02001551 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* Get rid of the fake eth1394 header, but save a pointer */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001555 eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 skb_pull(skb, ETH1394_HLEN);
1557
1558 proto = eth->h_proto;
1559 dg_size = skb->len;
1560
1561 /* Set the transmission type for the packet. ARP packets and IP
1562 * broadcast packets are sent via GASP. */
1563 if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
Ben Collins7136b802006-06-12 18:16:25 -04001564 proto == htons(ETH_P_ARP) ||
1565 (proto == htons(ETH_P_IP) &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001566 IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 tx_type = ETH1394_GASP;
1568 dest_node = LOCAL_BUS | ALL_NODES;
1569 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001570 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 dgl = priv->bc_dgl;
1572 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1573 priv->bc_dgl++;
1574 } else {
David S. Millerc20e3942006-10-29 16:14:55 -08001575 __be64 guid = get_unaligned((u64 *)eth->h_dest);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -08001578 be64_to_cpu(guid));
Stefan Richterfdc00922007-04-02 02:24:27 +02001579 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto fail;
Stefan Richterfdc00922007-04-02 02:24:27 +02001581
Stefan Richterefbeccf2007-04-02 02:12:32 +02001582 node_info =
1583 (struct eth1394_node_info *)node->ud->device.driver_data;
Stefan Richterfdc00922007-04-02 02:24:27 +02001584 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 dest_node = node->ud->ne->nodeid;
1588 max_payload = node_info->maxpayload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001589 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 dgl = node_info->dgl;
1592 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1593 node_info->dgl++;
1594 tx_type = ETH1394_WRREQ;
1595 }
1596
1597 /* If this is an ARP packet, convert it */
Ben Collins7136b802006-06-12 18:16:25 -04001598 if (proto == htons(ETH_P_ARP))
Stefan Richterefbeccf2007-04-02 02:12:32 +02001599 ether1394_arp_to_1394arp(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 ptask->hdr.words.word1 = 0;
1602 ptask->hdr.words.word2 = 0;
1603 ptask->hdr.words.word3 = 0;
1604 ptask->hdr.words.word4 = 0;
1605 ptask->skb = skb;
1606 ptask->priv = priv;
1607 ptask->tx_type = tx_type;
1608
1609 if (tx_type != ETH1394_GASP) {
1610 u64 addr;
1611
1612 spin_lock_irqsave(&priv->lock, flags);
1613 addr = node_info->fifo;
1614 spin_unlock_irqrestore(&priv->lock, flags);
1615
1616 ptask->addr = addr;
1617 ptask->dest_node = dest_node;
1618 }
1619
1620 ptask->tx_type = tx_type;
1621 ptask->max_payload = max_payload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001622 ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
1623 proto, &ptask->hdr, dg_size, dgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* Add the encapsulation header to the fragment */
1626 tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
1627 dev->trans_start = jiffies;
1628 if (ether1394_send_packet(ptask, tx_len))
1629 goto fail;
1630
1631 netif_wake_queue(dev);
Stefan Richterfdc00922007-04-02 02:24:27 +02001632 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633fail:
1634 if (ptask)
1635 kmem_cache_free(packet_task_cache, ptask);
1636
1637 if (skb != NULL)
1638 dev_kfree_skb(skb);
1639
Stefan Richterefbeccf2007-04-02 02:12:32 +02001640 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 priv->stats.tx_dropped++;
1642 priv->stats.tx_errors++;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001643 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (netif_queue_stopped(dev))
1646 netif_wake_queue(dev);
1647
Stefan Richterfdc00922007-04-02 02:24:27 +02001648 /*
1649 * FIXME: According to a patch from 2003-02-26, "returning non-zero
1650 * causes serious problems" here, allegedly. Before that patch,
1651 * -ERRNO was returned which is not appropriate under Linux 2.6.
1652 * Perhaps more needs to be done? Stop the queue in serious
1653 * conditions and restart it elsewhere?
1654 */
1655 /* return NETDEV_TX_BUSY; */
1656 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Stefan Richterefbeccf2007-04-02 02:12:32 +02001659static void ether1394_get_drvinfo(struct net_device *dev,
1660 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001662 strcpy(info->driver, driver_name);
1663 strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666static struct ethtool_ops ethtool_ops = {
1667 .get_drvinfo = ether1394_get_drvinfo
1668};
1669
Akinobu Mita809e9052007-04-21 18:36:26 +09001670static int __init ether1394_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Akinobu Mita809e9052007-04-21 18:36:26 +09001672 int err;
1673
Stefan Richterefbeccf2007-04-02 02:12:32 +02001674 packet_task_cache = kmem_cache_create("packet_task",
1675 sizeof(struct packet_task),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 0, 0, NULL, NULL);
Akinobu Mita809e9052007-04-21 18:36:26 +09001677 if (!packet_task_cache)
1678 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 hpsb_register_highlevel(&eth1394_highlevel);
Akinobu Mita809e9052007-04-21 18:36:26 +09001681 err = hpsb_register_protocol(&eth1394_proto_driver);
1682 if (err) {
1683 hpsb_unregister_highlevel(&eth1394_highlevel);
1684 kmem_cache_destroy(packet_task_cache);
1685 }
1686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Akinobu Mita809e9052007-04-21 18:36:26 +09001689static void __exit ether1394_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 hpsb_unregister_protocol(&eth1394_proto_driver);
1692 hpsb_unregister_highlevel(&eth1394_highlevel);
1693 kmem_cache_destroy(packet_task_cache);
1694}
1695
1696module_init(ether1394_init_module);
1697module_exit(ether1394_exit_module);