1/* 2 * eth1394.c -- IPv4 driver for Linux IEEE-1394 Subsystem 3 * 4 * Copyright (C) 2001-2003 Ben Collins <bcollins@debian.org> 5 * 2000 Bonin Franck <boninf@free.fr> 6 * 2003 Steve Kinneberg <kinnebergsteve@acmsystems.com> 7 * 8 * Mainly based on work by Emanuel Pirker and Andreas E. Bombe 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software Foundation, 22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23 */ 24 25/* 26 * This driver intends to support RFC 2734, which describes a method for 27 * transporting IPv4 datagrams over IEEE-1394 serial busses. 28 * 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 42#include <linux/module.h> 43 44#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#include <linux/workqueue.h> 51 52#include <linux/netdevice.h> 53#include <linux/inetdevice.h> 54#include <linux/if_arp.h> 55#include <linux/if_ether.h> 56#include <linux/ip.h> 57#include <linux/in.h> 58#include <linux/tcp.h> 59#include <linux/skbuff.h> 60#include <linux/bitops.h> 61#include <linux/ethtool.h> 62#include <asm/uaccess.h> 63#include <asm/delay.h> 64#include <asm/unaligned.h> 65#include <net/arp.h> 66 67#include "config_roms.h" 68#include "csr1212.h" 69#include "eth1394.h" 70#include "highlevel.h" 71#include "ieee1394.h" 72#include "ieee1394_core.h" 73#include "ieee1394_hotplug.h" 74#include "ieee1394_transactions.h" 75#include "ieee1394_types.h" 76#include "iso.h" 77#include "nodemgr.h" 78 79#define ETH1394_PRINT_G(level, fmt, args...) \ 80 printk(level "%s: " fmt, driver_name, ## args) 81 82#define ETH1394_PRINT(level, dev_name, fmt, args...) \ 83 printk(level "%s: %s: " fmt, driver_name, dev_name, ## args) 84 85struct fragment_info { 86 struct list_head list; 87 int offset; 88 int len; 89}; 90 91struct partial_datagram { 92 struct list_head list; 93 u16 dgl; 94 u16 dg_size; 95 u16 ether_type; 96 struct sk_buff *skb; 97 char *pbuf; 98 struct list_head frag_info; 99}; 100 101struct pdg_list { 102 struct list_head list; /* partial datagram list per node */ 103 unsigned int sz; /* partial datagram list size per node */ 104 spinlock_t lock; /* partial datagram lock */ 105}; 106 107struct eth1394_host_info { 108 struct hpsb_host *host; 109 struct net_device *dev; 110}; 111 112struct eth1394_node_ref { 113 struct unit_directory *ud; 114 struct list_head list; 115}; 116 117struct eth1394_node_info { 118 u16 maxpayload; /* max payload */ 119 u8 sspd; /* max speed */ 120 u64 fifo; /* FIFO address */ 121 struct pdg_list pdg; /* partial RX datagram lists */ 122 int dgl; /* outgoing datagram label */ 123}; 124 125static const char driver_name[] = "eth1394"; 126 127static struct kmem_cache *packet_task_cache; 128 129static struct hpsb_highlevel eth1394_highlevel; 130 131/* Use common.lf to determine header len */ 132static const int hdr_type_len[] = { 133 sizeof(struct eth1394_uf_hdr), 134 sizeof(struct eth1394_ff_hdr), 135 sizeof(struct eth1394_sf_hdr), 136 sizeof(struct eth1394_sf_hdr) 137}; 138 139static const u16 eth1394_speedto_maxpayload[] = { 140/* S100, S200, S400, S800, S1600, S3200 */ 141 512, 1024, 2048, 4096, 4096, 4096 142}; 143 144MODULE_AUTHOR("Ben Collins (bcollins@debian.org)"); 145MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)"); 146MODULE_LICENSE("GPL"); 147 148/* 149 * The max_partial_datagrams parameter is the maximum number of fragmented 150 * datagrams per node that eth1394 will keep in memory. Providing an upper 151 * bound allows us to limit the amount of memory that partial datagrams 152 * consume in the event that some partial datagrams are never completed. 153 */ 154static int max_partial_datagrams = 25; 155module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR); 156MODULE_PARM_DESC(max_partial_datagrams, 157 "Maximum number of partially received fragmented datagrams " 158 "(default = 25)."); 159 160 161static int ether1394_header(struct sk_buff *skb, struct net_device *dev, 162 unsigned short type, void *daddr, void *saddr, 163 unsigned len); 164static int ether1394_rebuild_header(struct sk_buff *skb); 165static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr); 166static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh); 167static void ether1394_header_cache_update(struct hh_cache *hh, 168 struct net_device *dev, 169 unsigned char *haddr); 170static int ether1394_tx(struct sk_buff *skb, struct net_device *dev); 171static void ether1394_iso(struct hpsb_iso *iso); 172 173static struct ethtool_ops ethtool_ops; 174 175static int ether1394_write(struct hpsb_host *host, int srcid, int destid, 176 quadlet_t *data, u64 addr, size_t len, u16 flags); 177static void ether1394_add_host(struct hpsb_host *host); 178static void ether1394_remove_host(struct hpsb_host *host); 179static void ether1394_host_reset(struct hpsb_host *host); 180 181/* Function for incoming 1394 packets */ 182static struct hpsb_address_ops addr_ops = { 183 .write = ether1394_write, 184}; 185 186/* Ieee1394 highlevel driver functions */ 187static struct hpsb_highlevel eth1394_highlevel = { 188 .name = driver_name, 189 .add_host = ether1394_add_host, 190 .remove_host = ether1394_remove_host, 191 .host_reset = ether1394_host_reset, 192}; 193 194static int ether1394_recv_init(struct eth1394_priv *priv) 195{ 196 unsigned int iso_buf_size; 197 198 iso_buf_size = min((unsigned int)PAGE_SIZE, 199 2 * (1U << (priv->host->csr.max_rec + 1))); 200 201 priv->iso = hpsb_iso_recv_init(priv->host, 202 ETHER1394_GASP_BUFFERS * iso_buf_size, 203 ETHER1394_GASP_BUFFERS, 204 priv->broadcast_channel, 205 HPSB_ISO_DMA_PACKET_PER_BUFFER, 206 1, ether1394_iso); 207 if (priv->iso == NULL) { 208 ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n"); 209 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; 225 226 if (priv->bc_state == ETHER1394_BC_ERROR) { 227 ret = ether1394_recv_init(priv); 228 if (ret) 229 return ret; 230 } 231 netif_start_queue(dev); 232 return 0; 233} 234 235/* This is called after an "ifdown" */ 236static int ether1394_stop(struct net_device *dev) 237{ 238 /* flush priv->wake */ 239 flush_scheduled_work(); 240 241 netif_stop_queue(dev); 242 return 0; 243} 244 245/* Return statistics to the caller */ 246static struct net_device_stats *ether1394_stats(struct net_device *dev) 247{ 248 return &(((struct eth1394_priv *)netdev_priv(dev))->stats); 249} 250 251static void ether1394_tx_timeout(struct net_device *dev) 252{ 253 struct hpsb_host *host = 254 ((struct eth1394_priv *)netdev_priv(dev))->host; 255 256 ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n"); 257 ether1394_host_reset(host); 258} 259 260static inline int ether1394_max_mtu(struct hpsb_host* host) 261{ 262 return (1 << (host->csr.max_rec + 1)) 263 - sizeof(union eth1394_hdr) - ETHER1394_GASP_OVERHEAD; 264} 265 266static int ether1394_change_mtu(struct net_device *dev, int new_mtu) 267{ 268 int max_mtu; 269 270 if (new_mtu < 68) 271 return -EINVAL; 272 273 max_mtu = ether1394_max_mtu( 274 ((struct eth1394_priv *)netdev_priv(dev))->host); 275 if (new_mtu > max_mtu) { 276 ETH1394_PRINT(KERN_INFO, dev->name, 277 "Local node constrains MTU to %d\n", max_mtu); 278 return -ERANGE; 279 } 280 281 dev->mtu = new_mtu; 282 return 0; 283} 284 285static void purge_partial_datagram(struct list_head *old) 286{ 287 struct partial_datagram *pd; 288 struct list_head *lh, *n; 289 struct fragment_info *fi; 290 291 pd = list_entry(old, struct partial_datagram, list); 292 293 list_for_each_safe(lh, n, &pd->frag_info) { 294 fi = list_entry(lh, struct fragment_info, list); 295 list_del(lh); 296 kfree(fi); 297 } 298 list_del(old); 299 kfree_skb(pd->skb); 300 kfree(pd); 301} 302 303/****************************************** 304 * 1394 bus activity functions 305 ******************************************/ 306 307static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl, 308 struct unit_directory *ud) 309{ 310 struct eth1394_node_ref *node; 311 312 list_for_each_entry(node, inl, list) 313 if (node->ud == ud) 314 return node; 315 316 return NULL; 317} 318 319static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl, 320 u64 guid) 321{ 322 struct eth1394_node_ref *node; 323 324 list_for_each_entry(node, inl, list) 325 if (node->ud->ne->guid == guid) 326 return node; 327 328 return NULL; 329} 330 331static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl, 332 nodeid_t nodeid) 333{ 334 struct eth1394_node_ref *node; 335 336 list_for_each_entry(node, inl, list) 337 if (node->ud->ne->nodeid == nodeid) 338 return node; 339 340 return NULL; 341} 342 343static int eth1394_new_node(struct eth1394_host_info *hi, 344 struct unit_directory *ud) 345{ 346 struct eth1394_priv *priv; 347 struct eth1394_node_ref *new_node; 348 struct eth1394_node_info *node_info; 349 350 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL); 351 if (!new_node) 352 return -ENOMEM; 353 354 node_info = kmalloc(sizeof(*node_info), GFP_KERNEL); 355 if (!node_info) { 356 kfree(new_node); 357 return -ENOMEM; 358 } 359 360 spin_lock_init(&node_info->pdg.lock); 361 INIT_LIST_HEAD(&node_info->pdg.list); 362 node_info->pdg.sz = 0; 363 node_info->fifo = CSR1212_INVALID_ADDR_SPACE; 364 365 ud->device.driver_data = node_info; 366 new_node->ud = ud; 367 368 priv = netdev_priv(hi->dev); 369 list_add_tail(&new_node->list, &priv->ip_node_list); 370 return 0; 371} 372 373static int eth1394_probe(struct device *dev) 374{ 375 struct unit_directory *ud; 376 struct eth1394_host_info *hi; 377 378 ud = container_of(dev, struct unit_directory, device); 379 hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); 380 if (!hi) 381 return -ENOENT; 382 383 return eth1394_new_node(hi, ud); 384} 385 386static int eth1394_remove(struct device *dev) 387{ 388 struct unit_directory *ud; 389 struct eth1394_host_info *hi; 390 struct eth1394_priv *priv; 391 struct eth1394_node_ref *old_node; 392 struct eth1394_node_info *node_info; 393 struct list_head *lh, *n; 394 unsigned long flags; 395 396 ud = container_of(dev, struct unit_directory, device); 397 hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); 398 if (!hi) 399 return -ENOENT; 400 401 priv = netdev_priv(hi->dev); 402 403 old_node = eth1394_find_node(&priv->ip_node_list, ud); 404 if (!old_node) 405 return 0; 406 407 list_del(&old_node->list); 408 kfree(old_node); 409 410 node_info = (struct eth1394_node_info*)ud->device.driver_data; 411 412 spin_lock_irqsave(&node_info->pdg.lock, flags); 413 /* The partial datagram list should be empty, but we'll just 414 * make sure anyway... */ 415 list_for_each_safe(lh, n, &node_info->pdg.list) 416 purge_partial_datagram(lh); 417 spin_unlock_irqrestore(&node_info->pdg.lock, flags); 418 419 kfree(node_info); 420 ud->device.driver_data = NULL; 421 return 0; 422} 423 424static int eth1394_update(struct unit_directory *ud) 425{ 426 struct eth1394_host_info *hi; 427 struct eth1394_priv *priv; 428 struct eth1394_node_ref *node; 429 430 hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); 431 if (!hi) 432 return -ENOENT; 433 434 priv = netdev_priv(hi->dev); 435 node = eth1394_find_node(&priv->ip_node_list, ud); 436 if (node) 437 return 0; 438 439 return eth1394_new_node(hi, ud); 440} 441 442static struct ieee1394_device_id eth1394_id_table[] = { 443 { 444 .match_flags = (IEEE1394_MATCH_SPECIFIER_ID | 445 IEEE1394_MATCH_VERSION), 446 .specifier_id = ETHER1394_GASP_SPECIFIER_ID, 447 .version = ETHER1394_GASP_VERSION, 448 }, 449 {} 450}; 451 452MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table); 453 454static struct hpsb_protocol_driver eth1394_proto_driver = { 455 .name = driver_name, 456 .id_table = eth1394_id_table, 457 .update = eth1394_update, 458 .driver = { 459 .probe = eth1394_probe, 460 .remove = eth1394_remove, 461 }, 462}; 463 464static void ether1394_reset_priv(struct net_device *dev, int set_mtu) 465{ 466 unsigned long flags; 467 int i; 468 struct eth1394_priv *priv = netdev_priv(dev); 469 struct hpsb_host *host = priv->host; 470 u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3])); 471 int max_speed = IEEE1394_SPEED_MAX; 472 473 spin_lock_irqsave(&priv->lock, flags); 474 475 memset(priv->ud_list, 0, sizeof(priv->ud_list)); 476 priv->bc_maxpayload = 512; 477 478 /* Determine speed limit */ 479 for (i = 0; i < host->node_count; i++) { 480 /* take care of S100B...S400B PHY ports */ 481 if (host->speed[i] == SELFID_SPEED_UNKNOWN) { 482 max_speed = IEEE1394_SPEED_100; 483 break; 484 } 485 if (max_speed > host->speed[i]) 486 max_speed = host->speed[i]; 487 } 488 priv->bc_sspd = max_speed; 489 490 if (set_mtu) { 491 /* Use the RFC 2734 default 1500 octets or the maximum payload 492 * as initial MTU */ 493 dev->mtu = min(1500, ether1394_max_mtu(host)); 494 495 /* Set our hardware address while we're at it */ 496 memcpy(dev->dev_addr, &guid, sizeof(u64)); 497 memset(dev->broadcast, 0xff, sizeof(u64)); 498 } 499 500 spin_unlock_irqrestore(&priv->lock, flags); 501} 502 503static void ether1394_init_dev(struct net_device *dev) 504{ 505 dev->open = ether1394_open; 506 dev->stop = ether1394_stop; 507 dev->hard_start_xmit = ether1394_tx; 508 dev->get_stats = ether1394_stats; 509 dev->tx_timeout = ether1394_tx_timeout; 510 dev->change_mtu = ether1394_change_mtu; 511 512 dev->hard_header = ether1394_header; 513 dev->rebuild_header = ether1394_rebuild_header; 514 dev->hard_header_cache = ether1394_header_cache; 515 dev->header_cache_update= ether1394_header_cache_update; 516 dev->hard_header_parse = ether1394_header_parse; 517 518 SET_ETHTOOL_OPS(dev, ðtool_ops); 519 520 dev->watchdog_timeo = ETHER1394_TIMEOUT; 521 dev->flags = IFF_BROADCAST | IFF_MULTICAST; 522 dev->features = NETIF_F_HIGHDMA; 523 dev->addr_len = ETH1394_ALEN; 524 dev->hard_header_len = ETH1394_HLEN; 525 dev->type = ARPHRD_IEEE1394; 526 527 dev->tx_queue_len = 1000; 528} 529 530/* 531 * Wake the queue up after commonly encountered transmit failure conditions are 532 * hopefully over. Currently only tlabel exhaustion is accounted for. 533 */ 534static void ether1394_wake_queue(struct work_struct *work) 535{ 536 struct eth1394_priv *priv; 537 struct hpsb_packet *packet; 538 539 priv = container_of(work, struct eth1394_priv, wake); 540 packet = hpsb_alloc_packet(0); 541 542 /* This is really bad, but unjam the queue anyway. */ 543 if (!packet) 544 goto out; 545 546 packet->host = priv->host; 547 packet->node_id = priv->wake_node; 548 /* 549 * A transaction label is all we really want. If we get one, it almost 550 * always means we can get a lot more because the ieee1394 core recycled 551 * a whole batch of tlabels, at last. 552 */ 553 if (hpsb_get_tlabel(packet) == 0) 554 hpsb_free_tlabel(packet); 555 556 hpsb_free_packet(packet); 557out: 558 netif_wake_queue(priv->wake_dev); 559} 560 561/* 562 * This function is called every time a card is found. It is generally called 563 * when the module is installed. This is where we add all of our ethernet 564 * devices. One for each host. 565 */ 566static void ether1394_add_host(struct hpsb_host *host) 567{ 568 struct eth1394_host_info *hi = NULL; 569 struct net_device *dev = NULL; 570 struct eth1394_priv *priv; 571 u64 fifo_addr; 572 573 if (hpsb_config_rom_ip1394_add(host) != 0) { 574 ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n"); 575 return; 576 } 577 578 fifo_addr = hpsb_allocate_and_register_addrspace( 579 ð1394_highlevel, host, &addr_ops, 580 ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN, 581 CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE); 582 if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) { 583 ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n"); 584 hpsb_config_rom_ip1394_remove(host); 585 return; 586 } 587 588 dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev); 589 if (dev == NULL) { 590 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); 591 goto out; 592 } 593 594 SET_MODULE_OWNER(dev); 595 596 /* This used to be &host->device in Linux 2.6.20 and before. */ 597 SET_NETDEV_DEV(dev, host->device.parent); 598 599 priv = netdev_priv(dev); 600 INIT_LIST_HEAD(&priv->ip_node_list); 601 spin_lock_init(&priv->lock); 602 priv->host = host; 603 priv->local_fifo = fifo_addr; 604 INIT_WORK(&priv->wake, ether1394_wake_queue); 605 priv->wake_dev = dev; 606 607 hi = hpsb_create_hostinfo(ð1394_highlevel, host, sizeof(*hi)); 608 if (hi == NULL) { 609 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); 610 goto out; 611 } 612 613 ether1394_reset_priv(dev, 1); 614 615 if (register_netdev(dev)) { 616 ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n"); 617 goto out; 618 } 619 620 ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n", 621 host->id); 622 623 hi->host = host; 624 hi->dev = dev; 625 626 /* Ignore validity in hopes that it will be set in the future. It'll 627 * be checked when the eth device is opened. */ 628 priv->broadcast_channel = host->csr.broadcast_channel & 0x3f; 629 630 ether1394_recv_init(priv); 631 return; 632out: 633 if (dev) 634 free_netdev(dev); 635 if (hi) 636 hpsb_destroy_hostinfo(ð1394_highlevel, host); 637 hpsb_unregister_addrspace(ð1394_highlevel, host, fifo_addr); 638 hpsb_config_rom_ip1394_remove(host); 639} 640 641/* Remove a card from our list */ 642static void ether1394_remove_host(struct hpsb_host *host) 643{ 644 struct eth1394_host_info *hi; 645 struct eth1394_priv *priv; 646 647 hi = hpsb_get_hostinfo(ð1394_highlevel, host); 648 if (!hi) 649 return; 650 priv = netdev_priv(hi->dev); 651 hpsb_unregister_addrspace(ð1394_highlevel, host, priv->local_fifo); 652 hpsb_config_rom_ip1394_remove(host); 653 if (priv->iso) 654 hpsb_iso_shutdown(priv->iso); 655 unregister_netdev(hi->dev); 656 free_netdev(hi->dev); 657} 658 659/* A bus reset happened */ 660static void ether1394_host_reset(struct hpsb_host *host) 661{ 662 struct eth1394_host_info *hi; 663 struct eth1394_priv *priv; 664 struct net_device *dev; 665 struct list_head *lh, *n; 666 struct eth1394_node_ref *node; 667 struct eth1394_node_info *node_info; 668 unsigned long flags; 669 670 hi = hpsb_get_hostinfo(ð1394_highlevel, host); 671 672 /* This can happen for hosts that we don't use */ 673 if (!hi) 674 return; 675 676 dev = hi->dev; 677 priv = netdev_priv(dev); 678 679 /* Reset our private host data, but not our MTU */ 680 netif_stop_queue(dev); 681 ether1394_reset_priv(dev, 0); 682 683 list_for_each_entry(node, &priv->ip_node_list, list) { 684 node_info = node->ud->device.driver_data; 685 686 spin_lock_irqsave(&node_info->pdg.lock, flags); 687 688 list_for_each_safe(lh, n, &node_info->pdg.list) 689 purge_partial_datagram(lh); 690 691 INIT_LIST_HEAD(&(node_info->pdg.list)); 692 node_info->pdg.sz = 0; 693 694 spin_unlock_irqrestore(&node_info->pdg.lock, flags); 695 } 696 697 netif_wake_queue(dev); 698} 699 700/****************************************** 701 * HW Header net device functions 702 ******************************************/ 703/* These functions have been adapted from net/ethernet/eth.c */ 704 705/* Create a fake MAC header for an arbitrary protocol layer. 706 * saddr=NULL means use device source address 707 * daddr=NULL means leave destination address (eg unresolved arp). */ 708static int ether1394_header(struct sk_buff *skb, struct net_device *dev, 709 unsigned short type, void *daddr, void *saddr, 710 unsigned len) 711{ 712 struct eth1394hdr *eth = 713 (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN); 714 715 eth->h_proto = htons(type); 716 717 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { 718 memset(eth->h_dest, 0, dev->addr_len); 719 return dev->hard_header_len; 720 } 721 722 if (daddr) { 723 memcpy(eth->h_dest, daddr, dev->addr_len); 724 return dev->hard_header_len; 725 } 726 727 return -dev->hard_header_len; 728} 729 730/* Rebuild the faked MAC header. This is called after an ARP 731 * (or in future other address resolution) has completed on this 732 * sk_buff. We now let ARP fill in the other fields. 733 * 734 * This routine CANNOT use cached dst->neigh! 735 * Really, it is used only when dst->neigh is wrong. 736 */ 737static int ether1394_rebuild_header(struct sk_buff *skb) 738{ 739 struct eth1394hdr *eth = (struct eth1394hdr *)skb->data; 740 741 if (eth->h_proto == htons(ETH_P_IP)) 742 return arp_find((unsigned char *)ð->h_dest, skb); 743 744 ETH1394_PRINT(KERN_DEBUG, skb->dev->name, 745 "unable to resolve type %04x addresses\n", 746 ntohs(eth->h_proto)); 747 return 0; 748} 749 750static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr) 751{ 752 struct net_device *dev = skb->dev; 753 754 memcpy(haddr, dev->dev_addr, ETH1394_ALEN); 755 return ETH1394_ALEN; 756} 757 758static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh) 759{ 760 unsigned short type = hh->hh_type; 761 struct net_device *dev = neigh->dev; 762 struct eth1394hdr *eth = 763 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN); 764 765 if (type == htons(ETH_P_802_3)) 766 return -1; 767 768 eth->h_proto = type; 769 memcpy(eth->h_dest, neigh->ha, dev->addr_len); 770 771 hh->hh_len = ETH1394_HLEN; 772 return 0; 773} 774 775/* Called by Address Resolution module to notify changes in address. */ 776static void ether1394_header_cache_update(struct hh_cache *hh, 777 struct net_device *dev, 778 unsigned char * haddr) 779{ 780 memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len); 781} 782 783/****************************************** 784 * Datagram reception code 785 ******************************************/ 786 787/* Copied from net/ethernet/eth.c */ 788static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev) 789{ 790 struct eth1394hdr *eth; 791 unsigned char *rawp; 792 793 skb_reset_mac_header(skb); 794 skb_pull(skb, ETH1394_HLEN); 795 eth = eth1394_hdr(skb); 796 797 if (*eth->h_dest & 1) { 798 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0) 799 skb->pkt_type = PACKET_BROADCAST; 800 } else { 801 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len)) 802 skb->pkt_type = PACKET_OTHERHOST; 803 } 804 805 if (ntohs(eth->h_proto) >= 1536) 806 return eth->h_proto; 807 808 rawp = skb->data; 809 810 if (*(unsigned short *)rawp == 0xFFFF) 811 return htons(ETH_P_802_3); 812 813 return htons(ETH_P_802_2); 814} 815 816/* Parse an encapsulated IP1394 header into an ethernet frame packet. 817 * We also perform ARP translation here, if need be. */ 818static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev, 819 nodeid_t srcid, nodeid_t destid, 820 u16 ether_type) 821{ 822 struct eth1394_priv *priv = netdev_priv(dev); 823 u64 dest_hw; 824 unsigned short ret = 0; 825 826 /* Setup our hw addresses. We use these to build the ethernet header. */ 827 if (destid == (LOCAL_BUS | ALL_NODES)) 828 dest_hw = ~0ULL; /* broadcast */ 829 else 830 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 | 831 priv->host->csr.guid_lo); 832 833 /* If this is an ARP packet, convert it. First, we want to make 834 * use of some of the fields, since they tell us a little bit 835 * about the sending machine. */ 836 if (ether_type == htons(ETH_P_ARP)) { 837 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; 838 struct arphdr *arp = (struct arphdr *)skb->data; 839 unsigned char *arp_ptr = (unsigned char *)(arp + 1); 840 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 | 841 ntohl(arp1394->fifo_lo); 842 u8 max_rec = min(priv->host->csr.max_rec, 843 (u8)(arp1394->max_rec)); 844 int sspd = arp1394->sspd; 845 u16 maxpayload; 846 struct eth1394_node_ref *node; 847 struct eth1394_node_info *node_info; 848 __be64 guid; 849 850 /* Sanity check. MacOSX seems to be sending us 131 in this 851 * field (atleast on my Panther G5). Not sure why. */ 852 if (sspd > 5 || sspd < 0) 853 sspd = 0; 854 855 maxpayload = min(eth1394_speedto_maxpayload[sspd], 856 (u16)(1 << (max_rec + 1))); 857 858 guid = get_unaligned(&arp1394->s_uniq_id); 859 node = eth1394_find_node_guid(&priv->ip_node_list, 860 be64_to_cpu(guid)); 861 if (!node) 862 return 0; 863 864 node_info = 865 (struct eth1394_node_info *)node->ud->device.driver_data; 866 867 /* Update our speed/payload/fifo_offset table */ 868 node_info->maxpayload = maxpayload; 869 node_info->sspd = sspd; 870 node_info->fifo = fifo_addr; 871 872 /* Now that we're done with the 1394 specific stuff, we'll 873 * need to alter some of the data. Believe it or not, all 874 * that needs to be done is sender_IP_address needs to be 875 * moved, the destination hardware address get stuffed 876 * in and the hardware address length set to 8. 877 * 878 * IMPORTANT: The code below overwrites 1394 specific data 879 * needed above so keep the munging of the data for the 880 * higher level IP stack last. */ 881 882 arp->ar_hln = 8; 883 arp_ptr += arp->ar_hln; /* skip over sender unique id */ 884 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */ 885 arp_ptr += arp->ar_pln; /* skip over sender IP addr */ 886 887 if (arp->ar_op == htons(ARPOP_REQUEST)) 888 memset(arp_ptr, 0, sizeof(u64)); 889 else 890 memcpy(arp_ptr, dev->dev_addr, sizeof(u64)); 891 } 892 893 /* Now add the ethernet header. */ 894 if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL, 895 skb->len) >= 0) 896 ret = ether1394_type_trans(skb, dev); 897 898 return ret; 899} 900 901static int fragment_overlap(struct list_head *frag_list, int offset, int len) 902{ 903 struct fragment_info *fi; 904 int end = offset + len; 905 906 list_for_each_entry(fi, frag_list, list) 907 if (offset < fi->offset + fi->len && end > fi->offset) 908 return 1; 909 910 return 0; 911} 912 913static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl) 914{ 915 struct partial_datagram *pd; 916 917 list_for_each_entry(pd, pdgl, list) 918 if (pd->dgl == dgl) 919 return &pd->list; 920 921 return NULL; 922} 923 924/* Assumes that new fragment does not overlap any existing fragments */ 925static int new_fragment(struct list_head *frag_info, int offset, int len) 926{ 927 struct list_head *lh; 928 struct fragment_info *fi, *fi2, *new; 929 930 list_for_each(lh, frag_info) { 931 fi = list_entry(lh, struct fragment_info, list); 932 if (fi->offset + fi->len == offset) { 933 /* The new fragment can be tacked on to the end */ 934 fi->len += len; 935 /* Did the new fragment plug a hole? */ 936 fi2 = list_entry(lh->next, struct fragment_info, list); 937 if (fi->offset + fi->len == fi2->offset) { 938 /* glue fragments together */ 939 fi->len += fi2->len; 940 list_del(lh->next); 941 kfree(fi2); 942 } 943 return 0; 944 } else if (offset + len == fi->offset) { 945 /* The new fragment can be tacked on to the beginning */ 946 fi->offset = offset; 947 fi->len += len; 948 /* Did the new fragment plug a hole? */ 949 fi2 = list_entry(lh->prev, struct fragment_info, list); 950 if (fi2->offset + fi2->len == fi->offset) { 951 /* glue fragments together */ 952 fi2->len += fi->len; 953 list_del(lh); 954 kfree(fi); 955 } 956 return 0; 957 } else if (offset > fi->offset + fi->len) { 958 break; 959 } else if (offset + len < fi->offset) { 960 lh = lh->prev; 961 break; 962 } 963 } 964 965 new = kmalloc(sizeof(*new), GFP_ATOMIC); 966 if (!new) 967 return -ENOMEM; 968 969 new->offset = offset; 970 new->len = len; 971 972 list_add(&new->list, lh); 973 return 0; 974} 975 976static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl, 977 int dgl, int dg_size, char *frag_buf, 978 int frag_off, int frag_len) 979{ 980 struct partial_datagram *new; 981 982 new = kmalloc(sizeof(*new), GFP_ATOMIC); 983 if (!new) 984 return -ENOMEM; 985 986 INIT_LIST_HEAD(&new->frag_info); 987 988 if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) { 989 kfree(new); 990 return -ENOMEM; 991 } 992 993 new->dgl = dgl; 994 new->dg_size = dg_size; 995 996 new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15); 997 if (!new->skb) { 998 struct fragment_info *fi = list_entry(new->frag_info.next, 999 struct fragment_info, 1000 list); 1001 kfree(fi); 1002 kfree(new); 1003 return -ENOMEM; 1004 } 1005 1006 skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15); 1007 new->pbuf = skb_put(new->skb, dg_size); 1008 memcpy(new->pbuf + frag_off, frag_buf, frag_len); 1009 1010 list_add(&new->list, pdgl); 1011 return 0; 1012} 1013 1014static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh, 1015 char *frag_buf, int frag_off, int frag_len) 1016{ 1017 struct partial_datagram *pd = 1018 list_entry(lh, struct partial_datagram, list); 1019 1020 if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0) 1021 return -ENOMEM; 1022 1023 memcpy(pd->pbuf + frag_off, frag_buf, frag_len); 1024 1025 /* Move list entry to beginnig of list so that oldest partial 1026 * datagrams percolate to the end of the list */ 1027 list_move(lh, pdgl); 1028 return 0; 1029} 1030 1031static int is_datagram_complete(struct list_head *lh, int dg_size) 1032{ 1033 struct partial_datagram *pd; 1034 struct fragment_info *fi; 1035 1036 pd = list_entry(lh, struct partial_datagram, list); 1037 fi = list_entry(pd->frag_info.next, struct fragment_info, list); 1038 1039 return (fi->len == dg_size); 1040} 1041 1042/* Packet reception. We convert the IP1394 encapsulation header to an 1043 * ethernet header, and fill it with some of our other fields. This is 1044 * an incoming packet from the 1394 bus. */ 1045static int ether1394_data_handler(struct net_device *dev, int srcid, int destid, 1046 char *buf, int len) 1047{ 1048 struct sk_buff *skb; 1049 unsigned long flags; 1050 struct eth1394_priv *priv = netdev_priv(dev); 1051 union eth1394_hdr *hdr = (union eth1394_hdr *)buf; 1052 u16 ether_type = 0; /* initialized to clear warning */ 1053 int hdr_len; 1054 struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)]; 1055 struct eth1394_node_info *node_info; 1056 1057 if (!ud) { 1058 struct eth1394_node_ref *node; 1059 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid); 1060 if (unlikely(!node)) { 1061 HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid " 1062 "lookup failure: " NODE_BUS_FMT, 1063 NODE_BUS_ARGS(priv->host, srcid)); 1064 priv->stats.rx_dropped++; 1065 return -1; 1066 } 1067 ud = node->ud; 1068 1069 priv->ud_list[NODEID_TO_NODE(srcid)] = ud; 1070 } 1071 1072 node_info = (struct eth1394_node_info *)ud->device.driver_data; 1073 1074 /* First, did we receive a fragmented or unfragmented datagram? */ 1075 hdr->words.word1 = ntohs(hdr->words.word1); 1076 1077 hdr_len = hdr_type_len[hdr->common.lf]; 1078 1079 if (hdr->common.lf == ETH1394_HDR_LF_UF) { 1080 /* An unfragmented datagram has been received by the ieee1394 1081 * bus. Build an skbuff around it so we can pass it to the 1082 * high level network layer. */ 1083 1084 skb = dev_alloc_skb(len + dev->hard_header_len + 15); 1085 if (unlikely(!skb)) { 1086 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); 1087 priv->stats.rx_dropped++; 1088 return -1; 1089 } 1090 skb_reserve(skb, (dev->hard_header_len + 15) & ~15); 1091 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len, 1092 len - hdr_len); 1093 ether_type = hdr->uf.ether_type; 1094 } else { 1095 /* A datagram fragment has been received, now the fun begins. */ 1096 1097 struct list_head *pdgl, *lh; 1098 struct partial_datagram *pd; 1099 int fg_off; 1100 int fg_len = len - hdr_len; 1101 int dg_size; 1102 int dgl; 1103 int retval; 1104 struct pdg_list *pdg = &(node_info->pdg); 1105 1106 hdr->words.word3 = ntohs(hdr->words.word3); 1107 /* The 4th header word is reserved so no need to do ntohs() */ 1108 1109 if (hdr->common.lf == ETH1394_HDR_LF_FF) { 1110 ether_type = hdr->ff.ether_type; 1111 dgl = hdr->ff.dgl; 1112 dg_size = hdr->ff.dg_size + 1; 1113 fg_off = 0; 1114 } else { 1115 hdr->words.word2 = ntohs(hdr->words.word2); 1116 dgl = hdr->sf.dgl; 1117 dg_size = hdr->sf.dg_size + 1; 1118 fg_off = hdr->sf.fg_off; 1119 } 1120 spin_lock_irqsave(&pdg->lock, flags); 1121 1122 pdgl = &(pdg->list); 1123 lh = find_partial_datagram(pdgl, dgl); 1124 1125 if (lh == NULL) { 1126 while (pdg->sz >= max_partial_datagrams) { 1127 /* remove the oldest */ 1128 purge_partial_datagram(pdgl->prev); 1129 pdg->sz--; 1130 } 1131 1132 retval = new_partial_datagram(dev, pdgl, dgl, dg_size, 1133 buf + hdr_len, fg_off, 1134 fg_len); 1135 if (retval < 0) { 1136 spin_unlock_irqrestore(&pdg->lock, flags); 1137 goto bad_proto; 1138 } 1139 pdg->sz++; 1140 lh = find_partial_datagram(pdgl, dgl); 1141 } else { 1142 struct partial_datagram *pd; 1143 1144 pd = list_entry(lh, struct partial_datagram, list); 1145 1146 if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) { 1147 /* Overlapping fragments, obliterate old 1148 * datagram and start new one. */ 1149 purge_partial_datagram(lh); 1150 retval = new_partial_datagram(dev, pdgl, dgl, 1151 dg_size, 1152 buf + hdr_len, 1153 fg_off, fg_len); 1154 if (retval < 0) { 1155 pdg->sz--; 1156 spin_unlock_irqrestore(&pdg->lock, flags); 1157 goto bad_proto; 1158 } 1159 } else { 1160 retval = update_partial_datagram(pdgl, lh, 1161 buf + hdr_len, 1162 fg_off, fg_len); 1163 if (retval < 0) { 1164 /* Couldn't save off fragment anyway 1165 * so might as well obliterate the 1166 * datagram now. */ 1167 purge_partial_datagram(lh); 1168 pdg->sz--; 1169 spin_unlock_irqrestore(&pdg->lock, flags); 1170 goto bad_proto; 1171 } 1172 } /* fragment overlap */ 1173 } /* new datagram or add to existing one */ 1174 1175 pd = list_entry(lh, struct partial_datagram, list); 1176 1177 if (hdr->common.lf == ETH1394_HDR_LF_FF) 1178 pd->ether_type = ether_type; 1179 1180 if (is_datagram_complete(lh, dg_size)) { 1181 ether_type = pd->ether_type; 1182 pdg->sz--; 1183 skb = skb_get(pd->skb); 1184 purge_partial_datagram(lh); 1185 spin_unlock_irqrestore(&pdg->lock, flags); 1186 } else { 1187 /* Datagram is not complete, we're done for the 1188 * moment. */ 1189 spin_unlock_irqrestore(&pdg->lock, flags); 1190 return 0; 1191 } 1192 } /* unframgented datagram or fragmented one */ 1193 1194 /* Write metadata, and then pass to the receive level */ 1195 skb->dev = dev; 1196 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ 1197 1198 /* Parse the encapsulation header. This actually does the job of 1199 * converting to an ethernet frame header, aswell as arp 1200 * conversion if needed. ARP conversion is easier in this 1201 * direction, since we are using ethernet as our backend. */ 1202 skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid, 1203 ether_type); 1204 1205 spin_lock_irqsave(&priv->lock, flags); 1206 1207 if (!skb->protocol) { 1208 priv->stats.rx_errors++; 1209 priv->stats.rx_dropped++; 1210 dev_kfree_skb_any(skb); 1211 goto bad_proto; 1212 } 1213 1214 if (netif_rx(skb) == NET_RX_DROP) { 1215 priv->stats.rx_errors++; 1216 priv->stats.rx_dropped++; 1217 goto bad_proto; 1218 } 1219 1220 /* Statistics */ 1221 priv->stats.rx_packets++; 1222 priv->stats.rx_bytes += skb->len; 1223 1224bad_proto: 1225 if (netif_queue_stopped(dev)) 1226 netif_wake_queue(dev); 1227 spin_unlock_irqrestore(&priv->lock, flags); 1228 1229 dev->last_rx = jiffies; 1230 1231 return 0; 1232} 1233 1234static int ether1394_write(struct hpsb_host *host, int srcid, int destid, 1235 quadlet_t *data, u64 addr, size_t len, u16 flags) 1236{ 1237 struct eth1394_host_info *hi; 1238 1239 hi = hpsb_get_hostinfo(ð1394_highlevel, host); 1240 if (unlikely(!hi)) { 1241 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", 1242 host->id); 1243 return RCODE_ADDRESS_ERROR; 1244 } 1245 1246 if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len)) 1247 return RCODE_ADDRESS_ERROR; 1248 else 1249 return RCODE_COMPLETE; 1250} 1251 1252static void ether1394_iso(struct hpsb_iso *iso) 1253{ 1254 quadlet_t *data; 1255 char *buf; 1256 struct eth1394_host_info *hi; 1257 struct net_device *dev; 1258 struct eth1394_priv *priv; 1259 unsigned int len; 1260 u32 specifier_id; 1261 u16 source_id; 1262 int i; 1263 int nready; 1264 1265 hi = hpsb_get_hostinfo(ð1394_highlevel, iso->host); 1266 if (unlikely(!hi)) { 1267 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", 1268 iso->host->id); 1269 return; 1270 } 1271 1272 dev = hi->dev; 1273 1274 nready = hpsb_iso_n_ready(iso); 1275 for (i = 0; i < nready; i++) { 1276 struct hpsb_iso_packet_info *info = 1277 &iso->infos[(iso->first_packet + i) % iso->buf_packets]; 1278 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset); 1279 1280 /* skip over GASP header */ 1281 buf = (char *)data + 8; 1282 len = info->len - 8; 1283 1284 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 | 1285 (be32_to_cpu(data[1]) & 0xff000000) >> 24; 1286 source_id = be32_to_cpu(data[0]) >> 16; 1287 1288 priv = netdev_priv(dev); 1289 1290 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f) 1291 || specifier_id != ETHER1394_GASP_SPECIFIER_ID) { 1292 /* This packet is not for us */ 1293 continue; 1294 } 1295 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES, 1296 buf, len); 1297 } 1298 1299 hpsb_iso_recv_release_packets(iso, i); 1300 1301 dev->last_rx = jiffies; 1302} 1303 1304/****************************************** 1305 * Datagram transmission code 1306 ******************************************/ 1307 1308/* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire 1309 * arphdr) is the same format as the ip1394 header, so they overlap. The rest 1310 * needs to be munged a bit. The remainder of the arphdr is formatted based 1311 * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to 1312 * judge. 1313 * 1314 * Now that the EUI is used for the hardware address all we need to do to make 1315 * this work for 1394 is to insert 2 quadlets that contain max_rec size, 1316 * speed, and unicast FIFO address information between the sender_unique_id 1317 * and the IP addresses. 1318 */ 1319static void ether1394_arp_to_1394arp(struct sk_buff *skb, 1320 struct net_device *dev) 1321{ 1322 struct eth1394_priv *priv = netdev_priv(dev); 1323 struct arphdr *arp = (struct arphdr *)skb->data; 1324 unsigned char *arp_ptr = (unsigned char *)(arp + 1); 1325 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; 1326 1327 arp1394->hw_addr_len = 16; 1328 arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN); 1329 arp1394->max_rec = priv->host->csr.max_rec; 1330 arp1394->sspd = priv->host->csr.lnk_spd; 1331 arp1394->fifo_hi = htons(priv->local_fifo >> 32); 1332 arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0); 1333} 1334 1335/* We need to encapsulate the standard header with our own. We use the 1336 * ethernet header's proto for our own. */ 1337static unsigned int ether1394_encapsulate_prep(unsigned int max_payload, 1338 __be16 proto, 1339 union eth1394_hdr *hdr, 1340 u16 dg_size, u16 dgl) 1341{ 1342 unsigned int adj_max_payload = 1343 max_payload - hdr_type_len[ETH1394_HDR_LF_UF]; 1344 1345 /* Does it all fit in one packet? */ 1346 if (dg_size <= adj_max_payload) { 1347 hdr->uf.lf = ETH1394_HDR_LF_UF; 1348 hdr->uf.ether_type = proto; 1349 } else { 1350 hdr->ff.lf = ETH1394_HDR_LF_FF; 1351 hdr->ff.ether_type = proto; 1352 hdr->ff.dg_size = dg_size - 1; 1353 hdr->ff.dgl = dgl; 1354 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF]; 1355 } 1356 return (dg_size + adj_max_payload - 1) / adj_max_payload; 1357} 1358 1359static unsigned int ether1394_encapsulate(struct sk_buff *skb, 1360 unsigned int max_payload, 1361 union eth1394_hdr *hdr) 1362{ 1363 union eth1394_hdr *bufhdr; 1364 int ftype = hdr->common.lf; 1365 int hdrsz = hdr_type_len[ftype]; 1366 unsigned int adj_max_payload = max_payload - hdrsz; 1367 1368 switch (ftype) { 1369 case ETH1394_HDR_LF_UF: 1370 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); 1371 bufhdr->words.word1 = htons(hdr->words.word1); 1372 bufhdr->words.word2 = hdr->words.word2; 1373 break; 1374 1375 case ETH1394_HDR_LF_FF: 1376 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); 1377 bufhdr->words.word1 = htons(hdr->words.word1); 1378 bufhdr->words.word2 = hdr->words.word2; 1379 bufhdr->words.word3 = htons(hdr->words.word3); 1380 bufhdr->words.word4 = 0; 1381 1382 /* Set frag type here for future interior fragments */ 1383 hdr->common.lf = ETH1394_HDR_LF_IF; 1384 hdr->sf.fg_off = 0; 1385 break; 1386 1387 default: 1388 hdr->sf.fg_off += adj_max_payload; 1389 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload); 1390 if (max_payload >= skb->len) 1391 hdr->common.lf = ETH1394_HDR_LF_LF; 1392 bufhdr->words.word1 = htons(hdr->words.word1); 1393 bufhdr->words.word2 = htons(hdr->words.word2); 1394 bufhdr->words.word3 = htons(hdr->words.word3); 1395 bufhdr->words.word4 = 0; 1396 } 1397 return min(max_payload, skb->len); 1398} 1399 1400static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host) 1401{ 1402 struct hpsb_packet *p; 1403 1404 p = hpsb_alloc_packet(0); 1405 if (p) { 1406 p->host = host; 1407 p->generation = get_hpsb_generation(host); 1408 p->type = hpsb_async; 1409 } 1410 return p; 1411} 1412 1413static int ether1394_prep_write_packet(struct hpsb_packet *p, 1414 struct hpsb_host *host, nodeid_t node, 1415 u64 addr, void *data, int tx_len) 1416{ 1417 p->node_id = node; 1418 1419 if (hpsb_get_tlabel(p)) 1420 return -EAGAIN; 1421 1422 p->tcode = TCODE_WRITEB; 1423 p->header_size = 16; 1424 p->expect_response = 1; 1425 p->header[0] = 1426 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4; 1427 p->header[1] = host->node_id << 16 | addr >> 32; 1428 p->header[2] = addr & 0xffffffff; 1429 p->header[3] = tx_len << 16; 1430 p->data_size = (tx_len + 3) & ~3; 1431 p->data = data; 1432 1433 return 0; 1434} 1435 1436static void ether1394_prep_gasp_packet(struct hpsb_packet *p, 1437 struct eth1394_priv *priv, 1438 struct sk_buff *skb, int length) 1439{ 1440 p->header_size = 4; 1441 p->tcode = TCODE_STREAM_DATA; 1442 1443 p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 | 1444 TCODE_STREAM_DATA << 4; 1445 p->data_size = length; 1446 p->data = (quadlet_t *)skb->data - 2; 1447 p->data[0] = cpu_to_be32(priv->host->node_id << 16 | 1448 ETHER1394_GASP_SPECIFIER_ID_HI); 1449 p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 | 1450 ETHER1394_GASP_VERSION); 1451 1452 p->speed_code = priv->bc_sspd; 1453 1454 /* prevent hpsb_send_packet() from overriding our speed code */ 1455 p->node_id = LOCAL_BUS | ALL_NODES; 1456} 1457 1458static void ether1394_free_packet(struct hpsb_packet *packet) 1459{ 1460 if (packet->tcode != TCODE_STREAM_DATA) 1461 hpsb_free_tlabel(packet); 1462 hpsb_free_packet(packet); 1463} 1464 1465static void ether1394_complete_cb(void *__ptask); 1466 1467static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len) 1468{ 1469 struct eth1394_priv *priv = ptask->priv; 1470 struct hpsb_packet *packet = NULL; 1471 1472 packet = ether1394_alloc_common_packet(priv->host); 1473 if (!packet) 1474 return -ENOMEM; 1475 1476 if (ptask->tx_type == ETH1394_GASP) { 1477 int length = tx_len + 2 * sizeof(quadlet_t); 1478 1479 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length); 1480 } else if (ether1394_prep_write_packet(packet, priv->host, 1481 ptask->dest_node, 1482 ptask->addr, ptask->skb->data, 1483 tx_len)) { 1484 hpsb_free_packet(packet); 1485 return -EAGAIN; 1486 } 1487 1488 ptask->packet = packet; 1489 hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb, 1490 ptask); 1491 1492 if (hpsb_send_packet(packet) < 0) { 1493 ether1394_free_packet(packet); 1494 return -EIO; 1495 } 1496 1497 return 0; 1498} 1499 1500/* Task function to be run when a datagram transmission is completed */ 1501static void ether1394_dg_complete(struct packet_task *ptask, int fail) 1502{ 1503 struct sk_buff *skb = ptask->skb; 1504 struct eth1394_priv *priv = netdev_priv(skb->dev); 1505 unsigned long flags; 1506 1507 /* Statistics */ 1508 spin_lock_irqsave(&priv->lock, flags); 1509 if (fail) { 1510 priv->stats.tx_dropped++; 1511 priv->stats.tx_errors++; 1512 } else { 1513 priv->stats.tx_bytes += skb->len; 1514 priv->stats.tx_packets++; 1515 } 1516 spin_unlock_irqrestore(&priv->lock, flags); 1517 1518 dev_kfree_skb_any(skb); 1519 kmem_cache_free(packet_task_cache, ptask); 1520} 1521 1522/* Callback for when a packet has been sent and the status of that packet is 1523 * known */ 1524static void ether1394_complete_cb(void *__ptask) 1525{ 1526 struct packet_task *ptask = (struct packet_task *)__ptask; 1527 struct hpsb_packet *packet = ptask->packet; 1528 int fail = 0; 1529 1530 if (packet->tcode != TCODE_STREAM_DATA) 1531 fail = hpsb_packet_success(packet); 1532 1533 ether1394_free_packet(packet); 1534 1535 ptask->outstanding_pkts--; 1536 if (ptask->outstanding_pkts > 0 && !fail) { 1537 int tx_len, err; 1538 1539 /* Add the encapsulation header to the fragment */ 1540 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload, 1541 &ptask->hdr); 1542 err = ether1394_send_packet(ptask, tx_len); 1543 if (err) { 1544 if (err == -EAGAIN) 1545 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n"); 1546 1547 ether1394_dg_complete(ptask, 1); 1548 } 1549 } else { 1550 ether1394_dg_complete(ptask, fail); 1551 } 1552} 1553 1554/* Transmit a packet (called by kernel) */ 1555static int ether1394_tx(struct sk_buff *skb, struct net_device *dev) 1556{ 1557 struct eth1394hdr hdr_buf; 1558 struct eth1394_priv *priv = netdev_priv(dev); 1559 __be16 proto; 1560 unsigned long flags; 1561 nodeid_t dest_node; 1562 eth1394_tx_type tx_type; 1563 unsigned int tx_len; 1564 unsigned int max_payload; 1565 u16 dg_size; 1566 u16 dgl; 1567 struct packet_task *ptask; 1568 struct eth1394_node_ref *node; 1569 struct eth1394_node_info *node_info = NULL; 1570 1571 ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC); 1572 if (ptask == NULL) 1573 goto fail; 1574 1575 1576 skb = skb_share_check(skb, GFP_ATOMIC); 1577 if (!skb) 1578 goto fail; 1579 1580 /* Get rid of the fake eth1394 header, but first make a copy. 1581 * We might need to rebuild the header on tx failure. */ 1582 memcpy(&hdr_buf, skb->data, sizeof(hdr_buf)); 1583 skb_pull(skb, ETH1394_HLEN); 1584 1585 proto = hdr_buf.h_proto; 1586 dg_size = skb->len; 1587 1588 /* Set the transmission type for the packet. ARP packets and IP 1589 * broadcast packets are sent via GASP. */ 1590 if (memcmp(hdr_buf.h_dest, dev->broadcast, ETH1394_ALEN) == 0 || 1591 proto == htons(ETH_P_ARP) || 1592 (proto == htons(ETH_P_IP) && 1593 IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) { 1594 tx_type = ETH1394_GASP; 1595 dest_node = LOCAL_BUS | ALL_NODES; 1596 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD; 1597 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); 1598 dgl = priv->bc_dgl; 1599 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) 1600 priv->bc_dgl++; 1601 } else { 1602 __be64 guid = get_unaligned((u64 *)hdr_buf.h_dest); 1603 1604 node = eth1394_find_node_guid(&priv->ip_node_list, 1605 be64_to_cpu(guid)); 1606 if (!node) 1607 goto fail; 1608 1609 node_info = 1610 (struct eth1394_node_info *)node->ud->device.driver_data; 1611 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE) 1612 goto fail; 1613 1614 dest_node = node->ud->ne->nodeid; 1615 max_payload = node_info->maxpayload; 1616 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); 1617 1618 dgl = node_info->dgl; 1619 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) 1620 node_info->dgl++; 1621 tx_type = ETH1394_WRREQ; 1622 } 1623 1624 /* If this is an ARP packet, convert it */ 1625 if (proto == htons(ETH_P_ARP)) 1626 ether1394_arp_to_1394arp(skb, dev); 1627 1628 ptask->hdr.words.word1 = 0; 1629 ptask->hdr.words.word2 = 0; 1630 ptask->hdr.words.word3 = 0; 1631 ptask->hdr.words.word4 = 0; 1632 ptask->skb = skb; 1633 ptask->priv = priv; 1634 ptask->tx_type = tx_type; 1635 1636 if (tx_type != ETH1394_GASP) { 1637 u64 addr; 1638 1639 spin_lock_irqsave(&priv->lock, flags); 1640 addr = node_info->fifo; 1641 spin_unlock_irqrestore(&priv->lock, flags); 1642 1643 ptask->addr = addr; 1644 ptask->dest_node = dest_node; 1645 } 1646 1647 ptask->tx_type = tx_type; 1648 ptask->max_payload = max_payload; 1649 ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload, 1650 proto, &ptask->hdr, dg_size, dgl); 1651 1652 /* Add the encapsulation header to the fragment */ 1653 tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr); 1654 dev->trans_start = jiffies; 1655 if (ether1394_send_packet(ptask, tx_len)) { 1656 if (dest_node == (LOCAL_BUS | ALL_NODES)) 1657 goto fail; 1658 1659 /* At this point we want to restore the packet. When we return 1660 * here with NETDEV_TX_BUSY we will get another entrance in this 1661 * routine with the same skb and we need it to look the same. 1662 * So we pull 4 more bytes, then build the header again. */ 1663 skb_pull(skb, 4); 1664 ether1394_header(skb, dev, ntohs(hdr_buf.h_proto), 1665 hdr_buf.h_dest, NULL, 0); 1666 1667 /* Most failures of ether1394_send_packet are recoverable. */ 1668 netif_stop_queue(dev); 1669 priv->wake_node = dest_node; 1670 schedule_work(&priv->wake); 1671 kmem_cache_free(packet_task_cache, ptask); 1672 return NETDEV_TX_BUSY; 1673 } 1674 1675 return NETDEV_TX_OK; 1676fail: 1677 if (ptask) 1678 kmem_cache_free(packet_task_cache, ptask); 1679 1680 if (skb != NULL) 1681 dev_kfree_skb(skb); 1682 1683 spin_lock_irqsave(&priv->lock, flags); 1684 priv->stats.tx_dropped++; 1685 priv->stats.tx_errors++; 1686 spin_unlock_irqrestore(&priv->lock, flags); 1687 1688 /* return NETDEV_TX_BUSY; */ 1689 return NETDEV_TX_OK; 1690} 1691 1692static void ether1394_get_drvinfo(struct net_device *dev, 1693 struct ethtool_drvinfo *info) 1694{ 1695 strcpy(info->driver, driver_name); 1696 strcpy(info->bus_info, "ieee1394"); 1697} 1698 1699static struct ethtool_ops ethtool_ops = { 1700 .get_drvinfo = ether1394_get_drvinfo 1701}; 1702 1703static int __init ether1394_init_module(void) 1704{ 1705 int err; 1706 1707 packet_task_cache = kmem_cache_create("packet_task", 1708 sizeof(struct packet_task), 1709 0, 0, NULL, NULL); 1710 if (!packet_task_cache) 1711 return -ENOMEM; 1712 1713 hpsb_register_highlevel(ð1394_highlevel); 1714 err = hpsb_register_protocol(ð1394_proto_driver); 1715 if (err) { 1716 hpsb_unregister_highlevel(ð1394_highlevel); 1717 kmem_cache_destroy(packet_task_cache); 1718 } 1719 return err; 1720} 1721 1722static void __exit ether1394_exit_module(void) 1723{ 1724 hpsb_unregister_protocol(ð1394_proto_driver); 1725 hpsb_unregister_highlevel(ð1394_highlevel); 1726 kmem_cache_destroy(packet_task_cache); 1727} 1728 1729module_init(ether1394_init_module); 1730module_exit(ether1394_exit_module); 1731