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