1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * IPv4 over IEEE 1394, per RFC 2734 4 * IPv6 over IEEE 1394, per RFC 3146 5 * 6 * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com> 7 * 8 * based on eth1394 by Ben Collins et al 9 */ 10 11 #include <linux/bug.h> 12 #include <linux/compiler.h> 13 #include <linux/delay.h> 14 #include <linux/device.h> 15 #include <linux/ethtool.h> 16 #include <linux/firewire.h> 17 #include <linux/firewire-constants.h> 18 #include <linux/highmem.h> 19 #include <linux/in.h> 20 #include <linux/ip.h> 21 #include <linux/jiffies.h> 22 #include <linux/mod_devicetable.h> 23 #include <linux/module.h> 24 #include <linux/moduleparam.h> 25 #include <linux/mutex.h> 26 #include <linux/netdevice.h> 27 #include <linux/skbuff.h> 28 #include <linux/slab.h> 29 #include <linux/spinlock.h> 30 31 #include <linux/unaligned.h> 32 #include <net/arp.h> 33 #include <net/firewire.h> 34 35 /* rx limits */ 36 #define FWNET_MAX_FRAGMENTS 30 /* arbitrary, > TX queue depth */ 37 #define FWNET_ISO_PAGE_COUNT (PAGE_SIZE < 16*1024 ? 4 : 2) 38 39 /* tx limits */ 40 #define FWNET_MAX_QUEUED_DATAGRAMS 20 /* < 64 = number of tlabels */ 41 #define FWNET_MIN_QUEUED_DATAGRAMS 10 /* should keep AT DMA busy enough */ 42 #define FWNET_TX_QUEUE_LEN FWNET_MAX_QUEUED_DATAGRAMS /* ? */ 43 44 #define IEEE1394_BROADCAST_CHANNEL 31 45 #define IEEE1394_ALL_NODES (0xffc0 | 0x003f) 46 #define IEEE1394_MAX_PAYLOAD_S100 512 47 #define FWNET_NO_FIFO_ADDR (~0ULL) 48 49 #define IANA_SPECIFIER_ID 0x00005eU 50 #define RFC2734_SW_VERSION 0x000001U 51 #define RFC3146_SW_VERSION 0x000002U 52 53 #define IEEE1394_GASP_HDR_SIZE 8 54 55 #define RFC2374_UNFRAG_HDR_SIZE 4 56 #define RFC2374_FRAG_HDR_SIZE 8 57 #define RFC2374_FRAG_OVERHEAD 4 58 59 #define RFC2374_HDR_UNFRAG 0 /* unfragmented */ 60 #define RFC2374_HDR_FIRSTFRAG 1 /* first fragment */ 61 #define RFC2374_HDR_LASTFRAG 2 /* last fragment */ 62 #define RFC2374_HDR_INTFRAG 3 /* interior fragment */ 63 64 static bool fwnet_hwaddr_is_multicast(u8 *ha) 65 { 66 return !!(*ha & 1); 67 } 68 69 /* IPv4 and IPv6 encapsulation header */ 70 struct rfc2734_header { 71 u32 w0; 72 u32 w1; 73 }; 74 75 #define fwnet_get_hdr_lf(h) (((h)->w0 & 0xc0000000) >> 30) 76 #define fwnet_get_hdr_ether_type(h) (((h)->w0 & 0x0000ffff)) 77 #define fwnet_get_hdr_dg_size(h) ((((h)->w0 & 0x0fff0000) >> 16) + 1) 78 #define fwnet_get_hdr_fg_off(h) (((h)->w0 & 0x00000fff)) 79 #define fwnet_get_hdr_dgl(h) (((h)->w1 & 0xffff0000) >> 16) 80 81 #define fwnet_set_hdr_lf(lf) ((lf) << 30) 82 #define fwnet_set_hdr_ether_type(et) (et) 83 #define fwnet_set_hdr_dg_size(dgs) (((dgs) - 1) << 16) 84 #define fwnet_set_hdr_fg_off(fgo) (fgo) 85 86 #define fwnet_set_hdr_dgl(dgl) ((dgl) << 16) 87 88 static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr, 89 unsigned ether_type) 90 { 91 hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG) 92 | fwnet_set_hdr_ether_type(ether_type); 93 } 94 95 static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr, 96 unsigned ether_type, unsigned dg_size, unsigned dgl) 97 { 98 hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG) 99 | fwnet_set_hdr_dg_size(dg_size) 100 | fwnet_set_hdr_ether_type(ether_type); 101 hdr->w1 = fwnet_set_hdr_dgl(dgl); 102 } 103 104 static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr, 105 unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl) 106 { 107 hdr->w0 = fwnet_set_hdr_lf(lf) 108 | fwnet_set_hdr_dg_size(dg_size) 109 | fwnet_set_hdr_fg_off(fg_off); 110 hdr->w1 = fwnet_set_hdr_dgl(dgl); 111 } 112 113 /* This list keeps track of what parts of the datagram have been filled in */ 114 struct fwnet_fragment_info { 115 struct list_head fi_link; 116 u16 offset; 117 u16 len; 118 }; 119 120 struct fwnet_partial_datagram { 121 struct list_head pd_link; 122 struct list_head fi_list; 123 struct sk_buff *skb; 124 /* FIXME Why not use skb->data? */ 125 char *pbuf; 126 u16 datagram_label; 127 u16 ether_type; 128 u16 datagram_size; 129 }; 130 131 static DEFINE_MUTEX(fwnet_device_mutex); 132 static LIST_HEAD(fwnet_device_list); 133 134 struct fwnet_device { 135 struct list_head dev_link; 136 spinlock_t lock; 137 enum { 138 FWNET_BROADCAST_ERROR, 139 FWNET_BROADCAST_RUNNING, 140 FWNET_BROADCAST_STOPPED, 141 } broadcast_state; 142 struct fw_iso_context *broadcast_rcv_context; 143 struct fw_iso_buffer broadcast_rcv_buffer; 144 void **broadcast_rcv_buffer_ptrs; 145 unsigned broadcast_rcv_next_ptr; 146 unsigned num_broadcast_rcv_ptrs; 147 unsigned rcv_buffer_size; 148 /* 149 * This value is the maximum unfragmented datagram size that can be 150 * sent by the hardware. It already has the GASP overhead and the 151 * unfragmented datagram header overhead calculated into it. 152 */ 153 unsigned broadcast_xmt_max_payload; 154 u16 broadcast_xmt_datagramlabel; 155 156 /* 157 * The CSR address that remote nodes must send datagrams to for us to 158 * receive them. 159 */ 160 struct fw_address_handler handler; 161 u64 local_fifo; 162 163 /* Number of tx datagrams that have been queued but not yet acked */ 164 int queued_datagrams; 165 166 int peer_count; 167 struct list_head peer_list; 168 struct fw_card *card; 169 struct net_device *netdev; 170 }; 171 172 struct fwnet_peer { 173 struct list_head peer_link; 174 struct fwnet_device *dev; 175 u64 guid; 176 177 /* guarded by dev->lock */ 178 struct list_head pd_list; /* received partial datagrams */ 179 unsigned pdg_size; /* pd_list size */ 180 181 u16 datagram_label; /* outgoing datagram label */ 182 u16 max_payload; /* includes RFC2374_FRAG_HDR_SIZE overhead */ 183 int node_id; 184 int generation; 185 unsigned speed; 186 }; 187 188 /* This is our task struct. It's used for the packet complete callback. */ 189 struct fwnet_packet_task { 190 struct fw_transaction transaction; 191 struct rfc2734_header hdr; 192 struct sk_buff *skb; 193 struct fwnet_device *dev; 194 195 int outstanding_pkts; 196 u64 fifo_addr; 197 u16 dest_node; 198 u16 max_payload; 199 u8 generation; 200 u8 speed; 201 u8 enqueued; 202 }; 203 204 /* 205 * saddr == NULL means use device source address. 206 * daddr == NULL means leave destination address (eg unresolved arp). 207 */ 208 static int fwnet_header_create(struct sk_buff *skb, struct net_device *net, 209 unsigned short type, const void *daddr, 210 const void *saddr, unsigned len) 211 { 212 struct fwnet_header *h; 213 214 h = skb_push(skb, sizeof(*h)); 215 put_unaligned_be16(type, &h->h_proto); 216 217 if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) { 218 memset(h->h_dest, 0, net->addr_len); 219 220 return net->hard_header_len; 221 } 222 223 if (daddr) { 224 memcpy(h->h_dest, daddr, net->addr_len); 225 226 return net->hard_header_len; 227 } 228 229 return -net->hard_header_len; 230 } 231 232 static int fwnet_header_cache(const struct neighbour *neigh, 233 struct hh_cache *hh, __be16 type) 234 { 235 struct net_device *net; 236 struct fwnet_header *h; 237 238 if (type == cpu_to_be16(ETH_P_802_3)) 239 return -1; 240 net = neigh->dev; 241 h = (struct fwnet_header *)((u8 *)hh->hh_data + HH_DATA_OFF(sizeof(*h))); 242 h->h_proto = type; 243 memcpy(h->h_dest, neigh->ha, net->addr_len); 244 245 /* Pairs with the READ_ONCE() in neigh_resolve_output(), 246 * neigh_hh_output() and neigh_update_hhs(). 247 */ 248 smp_store_release(&hh->hh_len, FWNET_HLEN); 249 250 return 0; 251 } 252 253 /* Called by Address Resolution module to notify changes in address. */ 254 static void fwnet_header_cache_update(struct hh_cache *hh, 255 const struct net_device *net, const unsigned char *haddr) 256 { 257 memcpy((u8 *)hh->hh_data + HH_DATA_OFF(FWNET_HLEN), haddr, net->addr_len); 258 } 259 260 static int fwnet_header_parse(const struct sk_buff *skb, const struct net_device *dev, 261 unsigned char *haddr) 262 { 263 memcpy(haddr, dev->dev_addr, FWNET_ALEN); 264 265 return FWNET_ALEN; 266 } 267 268 static const struct header_ops fwnet_header_ops = { 269 .create = fwnet_header_create, 270 .cache = fwnet_header_cache, 271 .cache_update = fwnet_header_cache_update, 272 .parse = fwnet_header_parse, 273 }; 274 275 /* FIXME: is this correct for all cases? */ 276 static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd, 277 unsigned offset, unsigned len) 278 { 279 struct fwnet_fragment_info *fi; 280 unsigned end = offset + len; 281 282 list_for_each_entry(fi, &pd->fi_list, fi_link) 283 if (offset < fi->offset + fi->len && end > fi->offset) 284 return true; 285 286 return false; 287 } 288 289 /* Assumes that new fragment does not overlap any existing fragments */ 290 static struct fwnet_fragment_info *fwnet_frag_new( 291 struct fwnet_partial_datagram *pd, unsigned offset, unsigned len) 292 { 293 struct fwnet_fragment_info *fi, *fi2, *new; 294 struct list_head *list; 295 296 list = &pd->fi_list; 297 list_for_each_entry(fi, &pd->fi_list, fi_link) { 298 if (fi->offset + fi->len == offset) { 299 /* The new fragment can be tacked on to the end */ 300 /* Did the new fragment plug a hole? */ 301 fi2 = list_entry(fi->fi_link.next, 302 struct fwnet_fragment_info, fi_link); 303 if (fi->offset + fi->len == fi2->offset) { 304 /* glue fragments together */ 305 fi->len += len + fi2->len; 306 list_del(&fi2->fi_link); 307 kfree(fi2); 308 } else { 309 fi->len += len; 310 } 311 312 return fi; 313 } 314 if (offset + len == fi->offset) { 315 /* The new fragment can be tacked on to the beginning */ 316 /* Did the new fragment plug a hole? */ 317 fi2 = list_entry(fi->fi_link.prev, 318 struct fwnet_fragment_info, fi_link); 319 if (fi2->offset + fi2->len == fi->offset) { 320 /* glue fragments together */ 321 fi2->len += fi->len + len; 322 list_del(&fi->fi_link); 323 kfree(fi); 324 325 return fi2; 326 } 327 fi->offset = offset; 328 fi->len += len; 329 330 return fi; 331 } 332 if (offset > fi->offset + fi->len) { 333 list = &fi->fi_link; 334 break; 335 } 336 if (offset + len < fi->offset) { 337 list = fi->fi_link.prev; 338 break; 339 } 340 } 341 342 new = kmalloc_obj(*new, GFP_ATOMIC); 343 if (!new) 344 return NULL; 345 346 new->offset = offset; 347 new->len = len; 348 list_add(&new->fi_link, list); 349 350 return new; 351 } 352 353 static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net, 354 struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size, 355 void *frag_buf, unsigned frag_off, unsigned frag_len) 356 { 357 struct fwnet_partial_datagram *new; 358 struct fwnet_fragment_info *fi; 359 360 new = kmalloc_obj(*new, GFP_ATOMIC); 361 if (!new) 362 goto fail; 363 364 INIT_LIST_HEAD(&new->fi_list); 365 fi = fwnet_frag_new(new, frag_off, frag_len); 366 if (fi == NULL) 367 goto fail_w_new; 368 369 new->datagram_label = datagram_label; 370 new->datagram_size = dg_size; 371 new->skb = dev_alloc_skb(dg_size + LL_RESERVED_SPACE(net)); 372 if (new->skb == NULL) 373 goto fail_w_fi; 374 375 skb_reserve(new->skb, LL_RESERVED_SPACE(net)); 376 new->pbuf = skb_put(new->skb, dg_size); 377 memcpy(new->pbuf + frag_off, frag_buf, frag_len); 378 list_add_tail(&new->pd_link, &peer->pd_list); 379 380 return new; 381 382 fail_w_fi: 383 kfree(fi); 384 fail_w_new: 385 kfree(new); 386 fail: 387 return NULL; 388 } 389 390 static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer, 391 u16 datagram_label) 392 { 393 struct fwnet_partial_datagram *pd; 394 395 list_for_each_entry(pd, &peer->pd_list, pd_link) 396 if (pd->datagram_label == datagram_label) 397 return pd; 398 399 return NULL; 400 } 401 402 403 static void fwnet_pd_delete(struct fwnet_partial_datagram *old) 404 { 405 struct fwnet_fragment_info *fi, *n; 406 407 list_for_each_entry_safe(fi, n, &old->fi_list, fi_link) 408 kfree(fi); 409 410 list_del(&old->pd_link); 411 dev_kfree_skb_any(old->skb); 412 kfree(old); 413 } 414 415 static bool fwnet_pd_update(struct fwnet_peer *peer, 416 struct fwnet_partial_datagram *pd, void *frag_buf, 417 unsigned frag_off, unsigned frag_len) 418 { 419 if (fwnet_frag_new(pd, frag_off, frag_len) == NULL) 420 return false; 421 422 memcpy(pd->pbuf + frag_off, frag_buf, frag_len); 423 424 /* 425 * Move list entry to beginning of list so that oldest partial 426 * datagrams percolate to the end of the list 427 */ 428 list_move_tail(&pd->pd_link, &peer->pd_list); 429 430 return true; 431 } 432 433 static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd) 434 { 435 struct fwnet_fragment_info *fi; 436 437 fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link); 438 439 return fi->len == pd->datagram_size; 440 } 441 442 /* caller must hold dev->lock */ 443 static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev, 444 u64 guid) 445 { 446 struct fwnet_peer *peer; 447 448 list_for_each_entry(peer, &dev->peer_list, peer_link) 449 if (peer->guid == guid) 450 return peer; 451 452 return NULL; 453 } 454 455 /* caller must hold dev->lock */ 456 static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev, 457 int node_id, int generation) 458 { 459 struct fwnet_peer *peer; 460 461 list_for_each_entry(peer, &dev->peer_list, peer_link) 462 if (peer->node_id == node_id && 463 peer->generation == generation) 464 return peer; 465 466 return NULL; 467 } 468 469 /* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */ 470 static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed) 471 { 472 max_rec = min(max_rec, speed + 8); 473 max_rec = clamp(max_rec, 8U, 11U); /* 512...4096 */ 474 475 return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE; 476 } 477 478 479 static int fwnet_finish_incoming_packet(struct net_device *net, 480 struct sk_buff *skb, u16 source_node_id, 481 bool is_broadcast, u16 ether_type) 482 { 483 int status, len; 484 485 switch (ether_type) { 486 case ETH_P_ARP: 487 case ETH_P_IP: 488 #if IS_ENABLED(CONFIG_IPV6) 489 case ETH_P_IPV6: 490 #endif 491 break; 492 default: 493 goto err; 494 } 495 496 /* Write metadata, and then pass to the receive level */ 497 skb->dev = net; 498 skb->ip_summed = CHECKSUM_NONE; 499 500 /* 501 * Parse the encapsulation header. This actually does the job of 502 * converting to an ethernet-like pseudo frame header. 503 */ 504 if (dev_hard_header(skb, net, ether_type, 505 is_broadcast ? net->broadcast : net->dev_addr, 506 NULL, skb->len) >= 0) { 507 struct fwnet_header *eth; 508 u16 *rawp; 509 __be16 protocol; 510 511 skb_reset_mac_header(skb); 512 skb_pull(skb, sizeof(*eth)); 513 eth = (struct fwnet_header *)skb_mac_header(skb); 514 if (fwnet_hwaddr_is_multicast(eth->h_dest)) { 515 if (memcmp(eth->h_dest, net->broadcast, 516 net->addr_len) == 0) 517 skb->pkt_type = PACKET_BROADCAST; 518 #if 0 519 else 520 skb->pkt_type = PACKET_MULTICAST; 521 #endif 522 } else { 523 if (memcmp(eth->h_dest, net->dev_addr, net->addr_len)) 524 skb->pkt_type = PACKET_OTHERHOST; 525 } 526 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) { 527 protocol = eth->h_proto; 528 } else { 529 rawp = (u16 *)skb->data; 530 if (*rawp == 0xffff) 531 protocol = htons(ETH_P_802_3); 532 else 533 protocol = htons(ETH_P_802_2); 534 } 535 skb->protocol = protocol; 536 } 537 538 len = skb->len; 539 status = netif_rx(skb); 540 if (status == NET_RX_DROP) { 541 net->stats.rx_errors++; 542 net->stats.rx_dropped++; 543 } else { 544 net->stats.rx_packets++; 545 net->stats.rx_bytes += len; 546 } 547 548 return 0; 549 550 err: 551 net->stats.rx_errors++; 552 net->stats.rx_dropped++; 553 554 dev_kfree_skb_any(skb); 555 556 return -ENOENT; 557 } 558 559 static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len, 560 int source_node_id, int generation, 561 bool is_broadcast) 562 { 563 struct sk_buff *skb; 564 struct net_device *net = dev->netdev; 565 struct rfc2734_header hdr; 566 unsigned lf; 567 unsigned long flags; 568 struct fwnet_peer *peer; 569 struct fwnet_partial_datagram *pd; 570 int fg_off; 571 int dg_size; 572 u16 datagram_label; 573 int retval; 574 u16 ether_type; 575 576 if (len <= RFC2374_UNFRAG_HDR_SIZE) 577 return 0; 578 579 hdr.w0 = be32_to_cpu(buf[0]); 580 lf = fwnet_get_hdr_lf(&hdr); 581 if (lf == RFC2374_HDR_UNFRAG) { 582 /* 583 * An unfragmented datagram has been received by the ieee1394 584 * bus. Build an skbuff around it so we can pass it to the 585 * high level network layer. 586 */ 587 ether_type = fwnet_get_hdr_ether_type(&hdr); 588 buf++; 589 len -= RFC2374_UNFRAG_HDR_SIZE; 590 591 skb = dev_alloc_skb(len + LL_RESERVED_SPACE(net)); 592 if (unlikely(!skb)) { 593 net->stats.rx_dropped++; 594 595 return -ENOMEM; 596 } 597 skb_reserve(skb, LL_RESERVED_SPACE(net)); 598 skb_put_data(skb, buf, len); 599 600 return fwnet_finish_incoming_packet(net, skb, source_node_id, 601 is_broadcast, ether_type); 602 } 603 604 /* A datagram fragment has been received, now the fun begins. */ 605 606 if (len <= RFC2374_FRAG_HDR_SIZE) 607 return 0; 608 609 hdr.w1 = ntohl(buf[1]); 610 buf += 2; 611 len -= RFC2374_FRAG_HDR_SIZE; 612 if (lf == RFC2374_HDR_FIRSTFRAG) { 613 ether_type = fwnet_get_hdr_ether_type(&hdr); 614 fg_off = 0; 615 } else { 616 ether_type = 0; 617 fg_off = fwnet_get_hdr_fg_off(&hdr); 618 } 619 datagram_label = fwnet_get_hdr_dgl(&hdr); 620 dg_size = fwnet_get_hdr_dg_size(&hdr); 621 622 if (fg_off + len > dg_size) 623 return 0; 624 625 spin_lock_irqsave(&dev->lock, flags); 626 627 peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation); 628 if (!peer) { 629 retval = -ENOENT; 630 goto fail; 631 } 632 633 pd = fwnet_pd_find(peer, datagram_label); 634 if (pd == NULL) { 635 while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) { 636 /* remove the oldest */ 637 fwnet_pd_delete(list_first_entry(&peer->pd_list, 638 struct fwnet_partial_datagram, pd_link)); 639 peer->pdg_size--; 640 } 641 pd = fwnet_pd_new(net, peer, datagram_label, 642 dg_size, buf, fg_off, len); 643 if (pd == NULL) { 644 retval = -ENOMEM; 645 goto fail; 646 } 647 peer->pdg_size++; 648 } else { 649 if (fwnet_frag_overlap(pd, fg_off, len) || 650 pd->datagram_size != dg_size) { 651 /* 652 * Differing datagram sizes or overlapping fragments, 653 * discard old datagram and start a new one. 654 */ 655 fwnet_pd_delete(pd); 656 pd = fwnet_pd_new(net, peer, datagram_label, 657 dg_size, buf, fg_off, len); 658 if (pd == NULL) { 659 peer->pdg_size--; 660 retval = -ENOMEM; 661 goto fail; 662 } 663 } else { 664 if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) { 665 /* 666 * Couldn't save off fragment anyway 667 * so might as well obliterate the 668 * datagram now. 669 */ 670 fwnet_pd_delete(pd); 671 peer->pdg_size--; 672 retval = -ENOMEM; 673 goto fail; 674 } 675 } 676 } /* new datagram or add to existing one */ 677 678 if (lf == RFC2374_HDR_FIRSTFRAG) 679 pd->ether_type = ether_type; 680 681 if (fwnet_pd_is_complete(pd)) { 682 ether_type = pd->ether_type; 683 peer->pdg_size--; 684 skb = skb_get(pd->skb); 685 fwnet_pd_delete(pd); 686 687 spin_unlock_irqrestore(&dev->lock, flags); 688 689 return fwnet_finish_incoming_packet(net, skb, source_node_id, 690 false, ether_type); 691 } 692 /* 693 * Datagram is not complete, we're done for the 694 * moment. 695 */ 696 retval = 0; 697 fail: 698 spin_unlock_irqrestore(&dev->lock, flags); 699 700 return retval; 701 } 702 703 static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r, 704 int tcode, int destination, int source, int generation, 705 unsigned long long offset, void *payload, size_t length, 706 void *callback_data) 707 { 708 struct fwnet_device *dev = callback_data; 709 int rcode; 710 711 if (destination == IEEE1394_ALL_NODES) { 712 // Although the response to the broadcast packet is not necessarily required, the 713 // fw_send_response() function should still be called to maintain the reference 714 // counting of the object. In the case, the call of function just releases the 715 // object as a result to decrease the reference counting. 716 rcode = RCODE_COMPLETE; 717 } else if (offset != dev->handler.offset) { 718 rcode = RCODE_ADDRESS_ERROR; 719 } else if (tcode != TCODE_WRITE_BLOCK_REQUEST) { 720 rcode = RCODE_TYPE_ERROR; 721 } else if (fwnet_incoming_packet(dev, payload, length, 722 source, generation, false) != 0) { 723 dev_err(&dev->netdev->dev, "incoming packet failure\n"); 724 rcode = RCODE_CONFLICT_ERROR; 725 } else { 726 rcode = RCODE_COMPLETE; 727 } 728 729 fw_send_response(card, r, rcode); 730 } 731 732 static int gasp_source_id(__be32 *p) 733 { 734 return be32_to_cpu(p[0]) >> 16; 735 } 736 737 static u32 gasp_specifier_id(__be32 *p) 738 { 739 return (be32_to_cpu(p[0]) & 0xffff) << 8 | 740 (be32_to_cpu(p[1]) & 0xff000000) >> 24; 741 } 742 743 static u32 gasp_version(__be32 *p) 744 { 745 return be32_to_cpu(p[1]) & 0xffffff; 746 } 747 748 static void fwnet_receive_broadcast(struct fw_iso_context *context, 749 u32 cycle, size_t header_length, void *header, void *data) 750 { 751 struct fwnet_device *dev; 752 struct fw_iso_packet packet; 753 __be16 *hdr_ptr; 754 __be32 *buf_ptr; 755 int retval; 756 u32 length; 757 unsigned long offset; 758 unsigned long flags; 759 760 dev = data; 761 hdr_ptr = header; 762 length = be16_to_cpup(hdr_ptr); 763 764 spin_lock_irqsave(&dev->lock, flags); 765 766 offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr; 767 buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++]; 768 if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs) 769 dev->broadcast_rcv_next_ptr = 0; 770 771 spin_unlock_irqrestore(&dev->lock, flags); 772 773 if (length > IEEE1394_GASP_HDR_SIZE && 774 gasp_specifier_id(buf_ptr) == IANA_SPECIFIER_ID && 775 (gasp_version(buf_ptr) == RFC2734_SW_VERSION 776 #if IS_ENABLED(CONFIG_IPV6) 777 || gasp_version(buf_ptr) == RFC3146_SW_VERSION 778 #endif 779 )) 780 fwnet_incoming_packet(dev, buf_ptr + 2, 781 length - IEEE1394_GASP_HDR_SIZE, 782 gasp_source_id(buf_ptr), 783 context->card->generation, true); 784 785 packet.payload_length = dev->rcv_buffer_size; 786 packet.interrupt = 1; 787 packet.skip = 0; 788 packet.tag = 3; 789 packet.sy = 0; 790 packet.header_length = IEEE1394_GASP_HDR_SIZE; 791 792 spin_lock_irqsave(&dev->lock, flags); 793 794 retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet, 795 &dev->broadcast_rcv_buffer, offset); 796 797 spin_unlock_irqrestore(&dev->lock, flags); 798 799 if (retval >= 0) 800 fw_iso_context_queue_flush(dev->broadcast_rcv_context); 801 else 802 dev_err(&dev->netdev->dev, "requeue failed\n"); 803 } 804 805 static struct kmem_cache *fwnet_packet_task_cache; 806 807 static void fwnet_free_ptask(struct fwnet_packet_task *ptask) 808 { 809 dev_kfree_skb_any(ptask->skb); 810 kmem_cache_free(fwnet_packet_task_cache, ptask); 811 } 812 813 /* Caller must hold dev->lock. */ 814 static void dec_queued_datagrams(struct fwnet_device *dev) 815 { 816 if (--dev->queued_datagrams == FWNET_MIN_QUEUED_DATAGRAMS) 817 netif_wake_queue(dev->netdev); 818 } 819 820 static int fwnet_send_packet(struct fwnet_packet_task *ptask); 821 822 static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask) 823 { 824 struct fwnet_device *dev = ptask->dev; 825 struct sk_buff *skb = ptask->skb; 826 unsigned long flags; 827 bool free; 828 829 spin_lock_irqsave(&dev->lock, flags); 830 831 ptask->outstanding_pkts--; 832 833 /* Check whether we or the networking TX soft-IRQ is last user. */ 834 free = (ptask->outstanding_pkts == 0 && ptask->enqueued); 835 if (free) 836 dec_queued_datagrams(dev); 837 838 if (ptask->outstanding_pkts == 0) { 839 dev->netdev->stats.tx_packets++; 840 dev->netdev->stats.tx_bytes += skb->len; 841 } 842 843 spin_unlock_irqrestore(&dev->lock, flags); 844 845 if (ptask->outstanding_pkts > 0) { 846 u16 dg_size; 847 u16 fg_off; 848 u16 datagram_label; 849 u16 lf; 850 851 /* Update the ptask to point to the next fragment and send it */ 852 lf = fwnet_get_hdr_lf(&ptask->hdr); 853 switch (lf) { 854 case RFC2374_HDR_LASTFRAG: 855 case RFC2374_HDR_UNFRAG: 856 default: 857 dev_err(&dev->netdev->dev, 858 "outstanding packet %x lf %x, header %x,%x\n", 859 ptask->outstanding_pkts, lf, ptask->hdr.w0, 860 ptask->hdr.w1); 861 BUG(); 862 863 case RFC2374_HDR_FIRSTFRAG: 864 /* Set frag type here for future interior fragments */ 865 dg_size = fwnet_get_hdr_dg_size(&ptask->hdr); 866 fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE; 867 datagram_label = fwnet_get_hdr_dgl(&ptask->hdr); 868 break; 869 870 case RFC2374_HDR_INTFRAG: 871 dg_size = fwnet_get_hdr_dg_size(&ptask->hdr); 872 fg_off = fwnet_get_hdr_fg_off(&ptask->hdr) 873 + ptask->max_payload - RFC2374_FRAG_HDR_SIZE; 874 datagram_label = fwnet_get_hdr_dgl(&ptask->hdr); 875 break; 876 } 877 878 if (ptask->dest_node == IEEE1394_ALL_NODES) { 879 skb_pull(skb, 880 ptask->max_payload + IEEE1394_GASP_HDR_SIZE); 881 } else { 882 skb_pull(skb, ptask->max_payload); 883 } 884 if (ptask->outstanding_pkts > 1) { 885 fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG, 886 dg_size, fg_off, datagram_label); 887 } else { 888 fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG, 889 dg_size, fg_off, datagram_label); 890 ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE; 891 } 892 fwnet_send_packet(ptask); 893 } 894 895 if (free) 896 fwnet_free_ptask(ptask); 897 } 898 899 static void fwnet_transmit_packet_failed(struct fwnet_packet_task *ptask) 900 { 901 struct fwnet_device *dev = ptask->dev; 902 unsigned long flags; 903 bool free; 904 905 spin_lock_irqsave(&dev->lock, flags); 906 907 /* One fragment failed; don't try to send remaining fragments. */ 908 ptask->outstanding_pkts = 0; 909 910 /* Check whether we or the networking TX soft-IRQ is last user. */ 911 free = ptask->enqueued; 912 if (free) 913 dec_queued_datagrams(dev); 914 915 dev->netdev->stats.tx_dropped++; 916 dev->netdev->stats.tx_errors++; 917 918 spin_unlock_irqrestore(&dev->lock, flags); 919 920 if (free) 921 fwnet_free_ptask(ptask); 922 } 923 924 static void fwnet_write_complete(struct fw_card *card, int rcode, 925 void *payload, size_t length, void *data) 926 { 927 struct fwnet_packet_task *ptask = data; 928 static unsigned long j; 929 static int last_rcode, errors_skipped; 930 931 if (rcode == RCODE_COMPLETE) { 932 fwnet_transmit_packet_done(ptask); 933 } else { 934 if (printk_timed_ratelimit(&j, 1000) || rcode != last_rcode) { 935 dev_err(&ptask->dev->netdev->dev, 936 "fwnet_write_complete failed: %x (skipped %d)\n", 937 rcode, errors_skipped); 938 939 errors_skipped = 0; 940 last_rcode = rcode; 941 } else { 942 errors_skipped++; 943 } 944 fwnet_transmit_packet_failed(ptask); 945 } 946 } 947 948 static int fwnet_send_packet(struct fwnet_packet_task *ptask) 949 { 950 struct fwnet_device *dev; 951 unsigned tx_len; 952 struct rfc2734_header *bufhdr; 953 unsigned long flags; 954 bool free; 955 956 dev = ptask->dev; 957 tx_len = ptask->max_payload; 958 switch (fwnet_get_hdr_lf(&ptask->hdr)) { 959 case RFC2374_HDR_UNFRAG: 960 bufhdr = skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE); 961 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0); 962 break; 963 964 case RFC2374_HDR_FIRSTFRAG: 965 case RFC2374_HDR_INTFRAG: 966 case RFC2374_HDR_LASTFRAG: 967 bufhdr = skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE); 968 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0); 969 put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1); 970 break; 971 972 default: 973 BUG(); 974 } 975 if (ptask->dest_node == IEEE1394_ALL_NODES) { 976 u8 *p; 977 int generation; 978 int node_id; 979 unsigned int sw_version; 980 981 /* ptask->generation may not have been set yet */ 982 generation = dev->card->generation; 983 smp_rmb(); 984 node_id = dev->card->node_id; 985 986 switch (ptask->skb->protocol) { 987 default: 988 sw_version = RFC2734_SW_VERSION; 989 break; 990 #if IS_ENABLED(CONFIG_IPV6) 991 case htons(ETH_P_IPV6): 992 sw_version = RFC3146_SW_VERSION; 993 #endif 994 } 995 996 p = skb_push(ptask->skb, IEEE1394_GASP_HDR_SIZE); 997 put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p); 998 put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24 999 | sw_version, &p[4]); 1000 1001 /* We should not transmit if broadcast_channel.valid == 0. */ 1002 fw_send_request(dev->card, &ptask->transaction, 1003 TCODE_STREAM_DATA, 1004 fw_stream_packet_destination_id(3, 1005 IEEE1394_BROADCAST_CHANNEL, 0), 1006 generation, SCODE_100, 0ULL, ptask->skb->data, 1007 tx_len + 8, fwnet_write_complete, ptask); 1008 1009 spin_lock_irqsave(&dev->lock, flags); 1010 1011 /* If the AT work item already ran, we may be last user. */ 1012 free = (ptask->outstanding_pkts == 0 && !ptask->enqueued); 1013 if (!free) 1014 ptask->enqueued = true; 1015 else 1016 dec_queued_datagrams(dev); 1017 1018 spin_unlock_irqrestore(&dev->lock, flags); 1019 1020 goto out; 1021 } 1022 1023 fw_send_request(dev->card, &ptask->transaction, 1024 TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node, 1025 ptask->generation, ptask->speed, ptask->fifo_addr, 1026 ptask->skb->data, tx_len, fwnet_write_complete, ptask); 1027 1028 spin_lock_irqsave(&dev->lock, flags); 1029 1030 /* If the AT work item already ran, we may be last user. */ 1031 free = (ptask->outstanding_pkts == 0 && !ptask->enqueued); 1032 if (!free) 1033 ptask->enqueued = true; 1034 else 1035 dec_queued_datagrams(dev); 1036 1037 spin_unlock_irqrestore(&dev->lock, flags); 1038 1039 netif_trans_update(dev->netdev); 1040 out: 1041 if (free) 1042 fwnet_free_ptask(ptask); 1043 1044 return 0; 1045 } 1046 1047 static void fwnet_fifo_stop(struct fwnet_device *dev) 1048 { 1049 if (dev->local_fifo == FWNET_NO_FIFO_ADDR) 1050 return; 1051 1052 fw_core_remove_address_handler(&dev->handler); 1053 dev->local_fifo = FWNET_NO_FIFO_ADDR; 1054 } 1055 1056 static int fwnet_fifo_start(struct fwnet_device *dev) 1057 { 1058 int retval; 1059 1060 if (dev->local_fifo != FWNET_NO_FIFO_ADDR) 1061 return 0; 1062 1063 dev->handler.length = 4096; 1064 dev->handler.address_callback = fwnet_receive_packet; 1065 dev->handler.callback_data = dev; 1066 1067 retval = fw_core_add_address_handler(&dev->handler, 1068 &fw_high_memory_region); 1069 if (retval < 0) 1070 return retval; 1071 1072 dev->local_fifo = dev->handler.offset; 1073 1074 return 0; 1075 } 1076 1077 static void __fwnet_broadcast_stop(struct fwnet_device *dev) 1078 { 1079 unsigned u; 1080 1081 if (dev->broadcast_state != FWNET_BROADCAST_ERROR) { 1082 for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) 1083 kunmap(dev->broadcast_rcv_buffer.pages[u]); 1084 fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card); 1085 } 1086 if (dev->broadcast_rcv_context) { 1087 fw_iso_context_destroy(dev->broadcast_rcv_context); 1088 dev->broadcast_rcv_context = NULL; 1089 } 1090 kfree(dev->broadcast_rcv_buffer_ptrs); 1091 dev->broadcast_rcv_buffer_ptrs = NULL; 1092 dev->broadcast_state = FWNET_BROADCAST_ERROR; 1093 } 1094 1095 static void fwnet_broadcast_stop(struct fwnet_device *dev) 1096 { 1097 if (dev->broadcast_state == FWNET_BROADCAST_ERROR) 1098 return; 1099 fw_iso_context_stop(dev->broadcast_rcv_context); 1100 __fwnet_broadcast_stop(dev); 1101 } 1102 1103 static int fwnet_broadcast_start(struct fwnet_device *dev) 1104 { 1105 struct fw_iso_context *context; 1106 int retval; 1107 unsigned num_packets; 1108 unsigned max_receive; 1109 struct fw_iso_packet packet; 1110 unsigned long offset; 1111 void **ptrptr; 1112 unsigned u; 1113 1114 if (dev->broadcast_state != FWNET_BROADCAST_ERROR) 1115 return 0; 1116 1117 max_receive = 1U << (dev->card->max_receive + 1); 1118 num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive; 1119 1120 ptrptr = kmalloc_array(num_packets, sizeof(void *), GFP_KERNEL); 1121 if (!ptrptr) { 1122 retval = -ENOMEM; 1123 goto failed; 1124 } 1125 dev->broadcast_rcv_buffer_ptrs = ptrptr; 1126 1127 context = fw_iso_context_create(dev->card, FW_ISO_CONTEXT_RECEIVE, 1128 IEEE1394_BROADCAST_CHANNEL, 1129 dev->card->link_speed, 8, 1130 fwnet_receive_broadcast, dev); 1131 if (IS_ERR(context)) { 1132 retval = PTR_ERR(context); 1133 goto failed; 1134 } 1135 1136 retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer, dev->card, 1137 FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE); 1138 if (retval < 0) 1139 goto failed; 1140 1141 dev->broadcast_state = FWNET_BROADCAST_STOPPED; 1142 1143 for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) { 1144 void *ptr; 1145 unsigned v; 1146 1147 ptr = kmap(dev->broadcast_rcv_buffer.pages[u]); 1148 for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++) 1149 *ptrptr++ = (void *) ((char *)ptr + v * max_receive); 1150 } 1151 dev->broadcast_rcv_context = context; 1152 1153 packet.payload_length = max_receive; 1154 packet.interrupt = 1; 1155 packet.skip = 0; 1156 packet.tag = 3; 1157 packet.sy = 0; 1158 packet.header_length = IEEE1394_GASP_HDR_SIZE; 1159 offset = 0; 1160 1161 for (u = 0; u < num_packets; u++) { 1162 retval = fw_iso_context_queue(context, &packet, 1163 &dev->broadcast_rcv_buffer, offset); 1164 if (retval < 0) 1165 goto failed; 1166 1167 offset += max_receive; 1168 } 1169 dev->num_broadcast_rcv_ptrs = num_packets; 1170 dev->rcv_buffer_size = max_receive; 1171 dev->broadcast_rcv_next_ptr = 0U; 1172 retval = fw_iso_context_start(context, -1, 0, 1173 FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */ 1174 if (retval < 0) 1175 goto failed; 1176 1177 /* FIXME: adjust it according to the min. speed of all known peers? */ 1178 dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100 1179 - IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE; 1180 dev->broadcast_state = FWNET_BROADCAST_RUNNING; 1181 1182 return 0; 1183 1184 failed: 1185 __fwnet_broadcast_stop(dev); 1186 return retval; 1187 } 1188 1189 static void set_carrier_state(struct fwnet_device *dev) 1190 { 1191 if (dev->peer_count > 1) 1192 netif_carrier_on(dev->netdev); 1193 else 1194 netif_carrier_off(dev->netdev); 1195 } 1196 1197 /* ifup */ 1198 static int fwnet_open(struct net_device *net) 1199 { 1200 struct fwnet_device *dev = netdev_priv(net); 1201 int ret; 1202 1203 ret = fwnet_broadcast_start(dev); 1204 if (ret) 1205 return ret; 1206 1207 netif_start_queue(net); 1208 1209 spin_lock_irq(&dev->lock); 1210 set_carrier_state(dev); 1211 spin_unlock_irq(&dev->lock); 1212 1213 return 0; 1214 } 1215 1216 /* ifdown */ 1217 static int fwnet_stop(struct net_device *net) 1218 { 1219 struct fwnet_device *dev = netdev_priv(net); 1220 1221 netif_stop_queue(net); 1222 fwnet_broadcast_stop(dev); 1223 1224 return 0; 1225 } 1226 1227 static netdev_tx_t fwnet_tx(struct sk_buff *skb, struct net_device *net) 1228 { 1229 struct fwnet_header hdr_buf; 1230 struct fwnet_device *dev = netdev_priv(net); 1231 __be16 proto; 1232 u16 dest_node; 1233 unsigned max_payload; 1234 u16 dg_size; 1235 u16 *datagram_label_ptr; 1236 struct fwnet_packet_task *ptask; 1237 struct fwnet_peer *peer; 1238 unsigned long flags; 1239 1240 spin_lock_irqsave(&dev->lock, flags); 1241 1242 /* Can this happen? */ 1243 if (netif_queue_stopped(dev->netdev)) { 1244 spin_unlock_irqrestore(&dev->lock, flags); 1245 1246 return NETDEV_TX_BUSY; 1247 } 1248 1249 ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC); 1250 if (ptask == NULL) 1251 goto fail; 1252 1253 skb = skb_share_check(skb, GFP_ATOMIC); 1254 if (!skb) 1255 goto fail; 1256 1257 /* 1258 * Make a copy of the driver-specific header. 1259 * We might need to rebuild the header on tx failure. 1260 */ 1261 memcpy(&hdr_buf, skb->data, sizeof(hdr_buf)); 1262 proto = hdr_buf.h_proto; 1263 1264 switch (proto) { 1265 case htons(ETH_P_ARP): 1266 case htons(ETH_P_IP): 1267 #if IS_ENABLED(CONFIG_IPV6) 1268 case htons(ETH_P_IPV6): 1269 #endif 1270 break; 1271 default: 1272 goto fail; 1273 } 1274 1275 skb_pull(skb, sizeof(hdr_buf)); 1276 dg_size = skb->len; 1277 1278 /* 1279 * Set the transmission type for the packet. ARP packets and IP 1280 * broadcast packets are sent via GASP. 1281 */ 1282 if (fwnet_hwaddr_is_multicast(hdr_buf.h_dest)) { 1283 max_payload = dev->broadcast_xmt_max_payload; 1284 datagram_label_ptr = &dev->broadcast_xmt_datagramlabel; 1285 1286 ptask->fifo_addr = FWNET_NO_FIFO_ADDR; 1287 ptask->generation = 0; 1288 ptask->dest_node = IEEE1394_ALL_NODES; 1289 ptask->speed = SCODE_100; 1290 } else { 1291 union fwnet_hwaddr *ha = (union fwnet_hwaddr *)hdr_buf.h_dest; 1292 __be64 guid = get_unaligned(&ha->uc.uniq_id); 1293 u8 generation; 1294 1295 peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid)); 1296 if (!peer) 1297 goto fail; 1298 1299 generation = peer->generation; 1300 dest_node = peer->node_id; 1301 max_payload = peer->max_payload; 1302 datagram_label_ptr = &peer->datagram_label; 1303 1304 ptask->fifo_addr = get_unaligned_be48(ha->uc.fifo); 1305 ptask->generation = generation; 1306 ptask->dest_node = dest_node; 1307 ptask->speed = peer->speed; 1308 } 1309 1310 ptask->hdr.w0 = 0; 1311 ptask->hdr.w1 = 0; 1312 ptask->skb = skb; 1313 ptask->dev = dev; 1314 1315 /* Does it all fit in one packet? */ 1316 if (dg_size <= max_payload) { 1317 fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto)); 1318 ptask->outstanding_pkts = 1; 1319 max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE; 1320 } else { 1321 u16 datagram_label; 1322 1323 max_payload -= RFC2374_FRAG_OVERHEAD; 1324 datagram_label = (*datagram_label_ptr)++; 1325 fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size, 1326 datagram_label); 1327 ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload); 1328 max_payload += RFC2374_FRAG_HDR_SIZE; 1329 } 1330 1331 if (++dev->queued_datagrams == FWNET_MAX_QUEUED_DATAGRAMS) 1332 netif_stop_queue(dev->netdev); 1333 1334 spin_unlock_irqrestore(&dev->lock, flags); 1335 1336 ptask->max_payload = max_payload; 1337 ptask->enqueued = 0; 1338 1339 fwnet_send_packet(ptask); 1340 1341 return NETDEV_TX_OK; 1342 1343 fail: 1344 spin_unlock_irqrestore(&dev->lock, flags); 1345 1346 if (ptask) 1347 kmem_cache_free(fwnet_packet_task_cache, ptask); 1348 1349 if (skb != NULL) 1350 dev_kfree_skb(skb); 1351 1352 net->stats.tx_dropped++; 1353 net->stats.tx_errors++; 1354 1355 /* 1356 * FIXME: According to a patch from 2003-02-26, "returning non-zero 1357 * causes serious problems" here, allegedly. Before that patch, 1358 * -ERRNO was returned which is not appropriate under Linux 2.6. 1359 * Perhaps more needs to be done? Stop the queue in serious 1360 * conditions and restart it elsewhere? 1361 */ 1362 return NETDEV_TX_OK; 1363 } 1364 1365 static const struct ethtool_ops fwnet_ethtool_ops = { 1366 .get_link = ethtool_op_get_link, 1367 }; 1368 1369 static const struct net_device_ops fwnet_netdev_ops = { 1370 .ndo_open = fwnet_open, 1371 .ndo_stop = fwnet_stop, 1372 .ndo_start_xmit = fwnet_tx, 1373 }; 1374 1375 static void fwnet_init_dev(struct net_device *net) 1376 { 1377 net->header_ops = &fwnet_header_ops; 1378 net->netdev_ops = &fwnet_netdev_ops; 1379 net->watchdog_timeo = 2 * HZ; 1380 net->flags = IFF_BROADCAST | IFF_MULTICAST; 1381 net->features = NETIF_F_HIGHDMA; 1382 net->addr_len = FWNET_ALEN; 1383 net->hard_header_len = FWNET_HLEN; 1384 net->type = ARPHRD_IEEE1394; 1385 net->tx_queue_len = FWNET_TX_QUEUE_LEN; 1386 net->ethtool_ops = &fwnet_ethtool_ops; 1387 } 1388 1389 /* caller must hold fwnet_device_mutex */ 1390 static struct fwnet_device *fwnet_dev_find(struct fw_card *card) 1391 { 1392 struct fwnet_device *dev; 1393 1394 list_for_each_entry(dev, &fwnet_device_list, dev_link) 1395 if (dev->card == card) 1396 return dev; 1397 1398 return NULL; 1399 } 1400 1401 static int fwnet_add_peer(struct fwnet_device *dev, 1402 struct fw_unit *unit, struct fw_device *device) 1403 { 1404 struct fwnet_peer *peer; 1405 1406 peer = kmalloc_obj(*peer); 1407 if (!peer) 1408 return -ENOMEM; 1409 1410 dev_set_drvdata(&unit->device, peer); 1411 1412 peer->dev = dev; 1413 peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4]; 1414 INIT_LIST_HEAD(&peer->pd_list); 1415 peer->pdg_size = 0; 1416 peer->datagram_label = 0; 1417 peer->speed = device->max_speed; 1418 peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed); 1419 1420 peer->generation = device->generation; 1421 smp_rmb(); 1422 peer->node_id = device->node_id; 1423 1424 spin_lock_irq(&dev->lock); 1425 list_add_tail(&peer->peer_link, &dev->peer_list); 1426 dev->peer_count++; 1427 set_carrier_state(dev); 1428 spin_unlock_irq(&dev->lock); 1429 1430 return 0; 1431 } 1432 1433 static int fwnet_probe(struct fw_unit *unit, 1434 const struct ieee1394_device_id *id) 1435 { 1436 struct fw_device *device = fw_parent_device(unit); 1437 struct fw_card *card = device->card; 1438 struct net_device *net; 1439 bool allocated_netdev = false; 1440 struct fwnet_device *dev; 1441 union fwnet_hwaddr ha; 1442 int ret; 1443 1444 mutex_lock(&fwnet_device_mutex); 1445 1446 dev = fwnet_dev_find(card); 1447 if (dev) { 1448 net = dev->netdev; 1449 goto have_dev; 1450 } 1451 1452 net = alloc_netdev(sizeof(*dev), "firewire%d", NET_NAME_UNKNOWN, 1453 fwnet_init_dev); 1454 if (net == NULL) { 1455 mutex_unlock(&fwnet_device_mutex); 1456 return -ENOMEM; 1457 } 1458 1459 allocated_netdev = true; 1460 SET_NETDEV_DEV(net, card->device); 1461 dev = netdev_priv(net); 1462 1463 spin_lock_init(&dev->lock); 1464 dev->broadcast_state = FWNET_BROADCAST_ERROR; 1465 dev->broadcast_rcv_context = NULL; 1466 dev->broadcast_xmt_max_payload = 0; 1467 dev->broadcast_xmt_datagramlabel = 0; 1468 dev->local_fifo = FWNET_NO_FIFO_ADDR; 1469 dev->queued_datagrams = 0; 1470 INIT_LIST_HEAD(&dev->peer_list); 1471 dev->card = card; 1472 dev->netdev = net; 1473 1474 ret = fwnet_fifo_start(dev); 1475 if (ret < 0) 1476 goto out; 1477 dev->local_fifo = dev->handler.offset; 1478 1479 /* 1480 * default MTU: RFC 2734 cl. 4, RFC 3146 cl. 4 1481 * maximum MTU: RFC 2734 cl. 4.2, fragment encapsulation header's 1482 * maximum possible datagram_size + 1 = 0xfff + 1 1483 */ 1484 net->mtu = 1500U; 1485 net->min_mtu = ETH_MIN_MTU; 1486 net->max_mtu = 4096U; 1487 1488 /* Set our hardware address while we're at it */ 1489 ha.uc.uniq_id = cpu_to_be64(card->guid); 1490 ha.uc.max_rec = dev->card->max_receive; 1491 ha.uc.sspd = dev->card->link_speed; 1492 put_unaligned_be48(dev->local_fifo, ha.uc.fifo); 1493 dev_addr_set(net, ha.u); 1494 1495 memset(net->broadcast, -1, net->addr_len); 1496 1497 ret = register_netdev(net); 1498 if (ret) 1499 goto out; 1500 1501 list_add_tail(&dev->dev_link, &fwnet_device_list); 1502 dev_notice(&net->dev, "IP over IEEE 1394 on card %s\n", 1503 dev_name(card->device)); 1504 have_dev: 1505 ret = fwnet_add_peer(dev, unit, device); 1506 if (ret && allocated_netdev) { 1507 unregister_netdev(net); 1508 list_del(&dev->dev_link); 1509 out: 1510 fwnet_fifo_stop(dev); 1511 free_netdev(net); 1512 } 1513 1514 mutex_unlock(&fwnet_device_mutex); 1515 1516 return ret; 1517 } 1518 1519 /* 1520 * FIXME abort partially sent fragmented datagrams, 1521 * discard partially received fragmented datagrams 1522 */ 1523 static void fwnet_update(struct fw_unit *unit) 1524 { 1525 struct fw_device *device = fw_parent_device(unit); 1526 struct fwnet_peer *peer = dev_get_drvdata(&unit->device); 1527 int generation; 1528 1529 generation = device->generation; 1530 1531 spin_lock_irq(&peer->dev->lock); 1532 peer->node_id = device->node_id; 1533 peer->generation = generation; 1534 spin_unlock_irq(&peer->dev->lock); 1535 } 1536 1537 static void fwnet_remove_peer(struct fwnet_peer *peer, struct fwnet_device *dev) 1538 { 1539 struct fwnet_partial_datagram *pd, *pd_next; 1540 1541 spin_lock_irq(&dev->lock); 1542 list_del(&peer->peer_link); 1543 dev->peer_count--; 1544 set_carrier_state(dev); 1545 spin_unlock_irq(&dev->lock); 1546 1547 list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link) 1548 fwnet_pd_delete(pd); 1549 1550 kfree(peer); 1551 } 1552 1553 static void fwnet_remove(struct fw_unit *unit) 1554 { 1555 struct fwnet_peer *peer = dev_get_drvdata(&unit->device); 1556 struct fwnet_device *dev = peer->dev; 1557 struct net_device *net; 1558 int i; 1559 1560 mutex_lock(&fwnet_device_mutex); 1561 1562 net = dev->netdev; 1563 1564 fwnet_remove_peer(peer, dev); 1565 1566 if (list_empty(&dev->peer_list)) { 1567 unregister_netdev(net); 1568 1569 fwnet_fifo_stop(dev); 1570 1571 for (i = 0; dev->queued_datagrams && i < 5; i++) 1572 ssleep(1); 1573 WARN_ON(dev->queued_datagrams); 1574 list_del(&dev->dev_link); 1575 1576 free_netdev(net); 1577 } 1578 1579 mutex_unlock(&fwnet_device_mutex); 1580 } 1581 1582 static const struct ieee1394_device_id fwnet_id_table[] = { 1583 { 1584 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | 1585 IEEE1394_MATCH_VERSION, 1586 .specifier_id = IANA_SPECIFIER_ID, 1587 .version = RFC2734_SW_VERSION, 1588 }, 1589 #if IS_ENABLED(CONFIG_IPV6) 1590 { 1591 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | 1592 IEEE1394_MATCH_VERSION, 1593 .specifier_id = IANA_SPECIFIER_ID, 1594 .version = RFC3146_SW_VERSION, 1595 }, 1596 #endif 1597 { } 1598 }; 1599 1600 static struct fw_driver fwnet_driver = { 1601 .driver = { 1602 .owner = THIS_MODULE, 1603 .name = KBUILD_MODNAME, 1604 .bus = &fw_bus_type, 1605 }, 1606 .probe = fwnet_probe, 1607 .update = fwnet_update, 1608 .remove = fwnet_remove, 1609 .id_table = fwnet_id_table, 1610 }; 1611 1612 static const u32 rfc2374_unit_directory_data[] = { 1613 0x00040000, /* directory_length */ 1614 0x1200005e, /* unit_specifier_id: IANA */ 1615 0x81000003, /* textual descriptor offset */ 1616 0x13000001, /* unit_sw_version: RFC 2734 */ 1617 0x81000005, /* textual descriptor offset */ 1618 0x00030000, /* descriptor_length */ 1619 0x00000000, /* text */ 1620 0x00000000, /* minimal ASCII, en */ 1621 0x49414e41, /* I A N A */ 1622 0x00030000, /* descriptor_length */ 1623 0x00000000, /* text */ 1624 0x00000000, /* minimal ASCII, en */ 1625 0x49507634, /* I P v 4 */ 1626 }; 1627 1628 static struct fw_descriptor rfc2374_unit_directory = { 1629 .length = ARRAY_SIZE(rfc2374_unit_directory_data), 1630 .key = (CSR_DIRECTORY | CSR_UNIT) << 24, 1631 .data = rfc2374_unit_directory_data 1632 }; 1633 1634 #if IS_ENABLED(CONFIG_IPV6) 1635 static const u32 rfc3146_unit_directory_data[] = { 1636 0x00040000, /* directory_length */ 1637 0x1200005e, /* unit_specifier_id: IANA */ 1638 0x81000003, /* textual descriptor offset */ 1639 0x13000002, /* unit_sw_version: RFC 3146 */ 1640 0x81000005, /* textual descriptor offset */ 1641 0x00030000, /* descriptor_length */ 1642 0x00000000, /* text */ 1643 0x00000000, /* minimal ASCII, en */ 1644 0x49414e41, /* I A N A */ 1645 0x00030000, /* descriptor_length */ 1646 0x00000000, /* text */ 1647 0x00000000, /* minimal ASCII, en */ 1648 0x49507636, /* I P v 6 */ 1649 }; 1650 1651 static struct fw_descriptor rfc3146_unit_directory = { 1652 .length = ARRAY_SIZE(rfc3146_unit_directory_data), 1653 .key = (CSR_DIRECTORY | CSR_UNIT) << 24, 1654 .data = rfc3146_unit_directory_data 1655 }; 1656 #endif 1657 1658 static int __init fwnet_init(void) 1659 { 1660 int err; 1661 1662 err = fw_core_add_descriptor(&rfc2374_unit_directory); 1663 if (err) 1664 return err; 1665 1666 #if IS_ENABLED(CONFIG_IPV6) 1667 err = fw_core_add_descriptor(&rfc3146_unit_directory); 1668 if (err) 1669 goto out; 1670 #endif 1671 1672 fwnet_packet_task_cache = kmem_cache_create("packet_task", 1673 sizeof(struct fwnet_packet_task), 0, 0, NULL); 1674 if (!fwnet_packet_task_cache) { 1675 err = -ENOMEM; 1676 goto out2; 1677 } 1678 1679 err = driver_register(&fwnet_driver.driver); 1680 if (!err) 1681 return 0; 1682 1683 kmem_cache_destroy(fwnet_packet_task_cache); 1684 out2: 1685 #if IS_ENABLED(CONFIG_IPV6) 1686 fw_core_remove_descriptor(&rfc3146_unit_directory); 1687 out: 1688 #endif 1689 fw_core_remove_descriptor(&rfc2374_unit_directory); 1690 1691 return err; 1692 } 1693 module_init(fwnet_init); 1694 1695 static void __exit fwnet_cleanup(void) 1696 { 1697 driver_unregister(&fwnet_driver.driver); 1698 kmem_cache_destroy(fwnet_packet_task_cache); 1699 #if IS_ENABLED(CONFIG_IPV6) 1700 fw_core_remove_descriptor(&rfc3146_unit_directory); 1701 #endif 1702 fw_core_remove_descriptor(&rfc2374_unit_directory); 1703 } 1704 module_exit(fwnet_cleanup); 1705 1706 MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>"); 1707 MODULE_DESCRIPTION("IP over IEEE1394 as per RFC 2734/3146"); 1708 MODULE_LICENSE("GPL"); 1709 MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table); 1710