📄 eth1394.c
字号:
ETH1394_PRINT (KERN_ERR, dev->name, "IEEE-1394 IPv4 over 1394 Ethernet (%s)\n", host->driver->name); INIT_LIST_HEAD (&hi->list); hi->host = host; hi->dev = dev; spin_lock_irq (&host_info_lock); list_add_tail (&hi->list, &host_info_list); spin_unlock_irq (&host_info_lock); return;out: if (dev != NULL) kfree (dev); ETH1394_PRINT_G (KERN_ERR, "Out of memory\n"); return;}/* Remove a card from our list */static void ether1394_remove_host (struct hpsb_host *host){ struct host_info *hi; spin_lock_irq (&host_info_lock); hi = find_host_info (host); if (hi != NULL) { unregister_netdev (hi->dev); kfree (hi->dev); list_del (&hi->list); kfree (hi); } spin_unlock_irq (&host_info_lock); return;}/* A reset has just arisen */static void ether1394_host_reset (struct hpsb_host *host){ struct net_device *dev = ether1394_find_dev(host); /* This can happen for hosts that we don't use */ if (dev == NULL) return; /* Reset our private host data, but not our mtu */ netif_stop_queue (dev); ether1394_reset_priv (dev, 0); netif_wake_queue (dev);}/* Copied from net/ethernet/eth.c */static inline unsigned short ether1394_type_trans(struct sk_buff *skb, struct net_device *dev){ struct ethhdr *eth; unsigned char *rawp; skb->mac.raw = skb->data; skb_pull (skb, ETH_HLEN); eth = skb->mac.ethernet;#if 0 if(*eth->h_dest & 1) { if(memcmp(eth->h_dest, dev->broadcast, dev->addr_len)==0) skb->pkt_type = PACKET_BROADCAST; else skb->pkt_type = PACKET_MULTICAST; } else { if(memcmp(eth->h_dest, dev->dev_addr, dev->addr_len)) skb->pkt_type = PACKET_OTHERHOST; }#endif if (ntohs (eth->h_proto) >= 1536) return eth->h_proto; rawp = skb->data; if (*(unsigned short *)rawp == 0xFFFF) return htons (ETH_P_802_3); return htons (ETH_P_802_2);}/* Parse an encapsulated IP1394 header into an ethernet frame packet. * We also perform ARP translation here, if need be. */static inline unsigned short ether1394_parse_encap (struct sk_buff *skb, struct net_device *dev, nodeid_t srcid, nodeid_t destid){ union eth1394_hdr *hdr = (union eth1394_hdr *)skb->data; unsigned char src_hw[ETH_ALEN], dest_hw[ETH_ALEN]; unsigned short ret = 0; /* Setup our hw addresses. We use these to build the * ethernet header. */ *(u16 *)dest_hw = htons(destid); *(u16 *)src_hw = htons(srcid); /* Remove the encapsulation header */ hdr->words.word1 = ntohs(hdr->words.word1); skb_pull (skb, hdr_type_len[hdr->common.lf]); /* If this is an ARP packet, convert it. First, we want to make * use of some of the fields, since they tell us a little bit * about the sending machine. */ if (hdr->uf.ether_type == __constant_htons (ETH_P_ARP)) { unsigned long flags; u16 phy_id = srcid & NODE_MASK; struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv; struct eth1394_arp arp1394; struct arphdr *arp = (struct arphdr *)skb->data; unsigned char *arp_ptr = (unsigned char *)(arp + 1); memcpy (&arp1394, arp, sizeof (struct eth1394_arp)); /* Update our speed/payload/fifo_offset table */ spin_lock_irqsave (&priv->lock, flags); ether1394_register_limits (phy_id, arp1394.max_rec, arp1394.sspd, le64_to_cpu (arp1394.s_uniq_id), ntohs (arp1394.fifo_hi), ntohl (arp1394.fifo_lo), priv); spin_unlock_irqrestore (&priv->lock, flags);#define PROCESS_MEMBER(ptr,val,len) \ ptr = memcpy (ptr, val, len) + len PROCESS_MEMBER (arp_ptr, src_hw, dev->addr_len); PROCESS_MEMBER (arp_ptr, &arp1394.sip, 4); PROCESS_MEMBER (arp_ptr, dest_hw, dev->addr_len); PROCESS_MEMBER (arp_ptr, &arp1394.tip, 4);#undef PROCESS_MEMBER arp->ar_hln = dev->addr_len; arp->ar_hrd = __constant_htons (ARPHRD_ETHER); skb_trim (skb, sizeof (struct arphdr) + 2*(dev->addr_len+4)); } /* Now add the ethernet header. */ if (dev->hard_header (skb, dev, __constant_ntohs (hdr->uf.ether_type), dest_hw, src_hw, skb->len) >= 0) ret = ether1394_type_trans(skb, dev); return ret;}/* Packet reception. We convert the IP1394 encapsulation header to an * ethernet header, and fill it with some of our other fields. This is * an incoming packet from the 1394 bus. */static int ether1394_write (struct hpsb_host *host, int srcid, int destid, quadlet_t *data, u64 addr, unsigned int len){ struct sk_buff *skb; char *buf = (char *)data; unsigned long flags; struct net_device *dev = ether1394_find_dev (host); struct eth1394_priv *priv; if (dev == NULL) { ETH1394_PRINT_G (KERN_ERR, "Could not find net device for host %p\n", host); return RCODE_ADDRESS_ERROR; } priv = (struct eth1394_priv *)dev->priv; /* A packet has been received by the ieee1394 bus. Build an skbuff * around it so we can pass it to the high level network layer. */ skb = dev_alloc_skb (len + dev->hard_header_len + 15); if (!skb) { HPSB_PRINT (KERN_ERR, "ether1394 rx: low on mem\n"); priv->stats.rx_dropped++; return RCODE_ADDRESS_ERROR; } skb_reserve(skb, (dev->hard_header_len + 15) & ~15); memcpy (skb_put (skb, len), buf, len); /* Write metadata, and then pass to the receive level */ skb->dev = dev; skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ /* Parse the encapsulation header. This actually does the job of * converting to an ethernet frame header, aswell as arp * conversion if needed. ARP conversion is easier in this * direction, since we are using ethernet as our backend. */ skb->protocol = ether1394_parse_encap (skb, dev, srcid, destid); spin_lock_irqsave (&priv->lock, flags); if (!skb->protocol) { priv->stats.rx_errors++; priv->stats.rx_dropped++; dev_kfree_skb_any(skb); goto bad_proto; } netif_stop_queue(dev); if (netif_rx (skb) == NET_RX_DROP) { priv->stats.rx_errors++; priv->stats.rx_dropped++; goto bad_proto; } /* Statistics */ priv->stats.rx_packets++; priv->stats.rx_bytes += skb->len;bad_proto: netif_start_queue(dev); spin_unlock_irqrestore (&priv->lock, flags); dev->last_rx = jiffies; return RCODE_COMPLETE;}/* This function is our scheduled write */static void hpsb_write_sched (void *__ptask){ struct packet_task *ptask = (struct packet_task *)__ptask; struct sk_buff *skb = ptask->skb; struct net_device *dev = ptask->skb->dev; struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv; unsigned long flags; /* Statistics */ spin_lock_irqsave (&priv->lock, flags); if (!hpsb_write(priv->host, ptask->dest_node, get_hpsb_generation(priv->host), ptask->addr, (quadlet_t *)skb->data, skb->len)) { priv->stats.tx_bytes += skb->len; priv->stats.tx_packets++; } else { //printk("Failed in hpsb_write_sched\n"); priv->stats.tx_dropped++; priv->stats.tx_errors++; if (netif_queue_stopped (dev)) netif_wake_queue (dev); } spin_unlock_irqrestore (&priv->lock, flags); dev->trans_start = jiffies; dev_kfree_skb(skb); kmem_cache_free(packet_task_cache, ptask); return;}/* Transmit a packet (called by kernel) */static int ether1394_tx (struct sk_buff *skb, struct net_device *dev){ int kmflags = in_interrupt () ? GFP_ATOMIC : GFP_KERNEL; struct ethhdr *eth; struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv; int proto; unsigned long flags; nodeid_t dest_node; u64 addr; struct packet_task *ptask = NULL; int ret = 0; if ((skb = skb_share_check (skb, kmflags)) == NULL) { ret = -ENOMEM; goto fail; } /* Get rid of the ethernet header, but save a pointer */ eth = (struct ethhdr *)skb->data; skb_pull (skb, ETH_HLEN); /* Save the destination id, and proto for our encapsulation, then * toss the ethernet header aside like the cheap whore it is. */ dest_node = ntohs (*(nodeid_t *)(eth->h_dest)); proto = eth->h_proto; /* If this is an ARP packet, convert it */ if (proto == __constant_htons (ETH_P_ARP)) ether1394_arp_to_1394arp (skb, dev); /* Now add our encapsulation header */ ether1394_encapsulate (skb, dev, proto); /* TODO: The above encapsulate function needs to recognize when a * packet needs to be split for a specified node. It should create * a list of skb's that we could then iterate over for the below * call to schedule our writes. */ /* XXX: Right now we accept that we don't exactly follow RFC. When * we do, we will send ARP requests via GASP format, and so we wont * need this hack. */ spin_lock_irqsave (&priv->lock, flags); addr = (u64)priv->fifo_hi[dest_node & NODE_MASK] << 32 | priv->fifo_lo[dest_node & NODE_MASK]; spin_unlock_irqrestore (&priv->lock, flags); if (!addr) addr = ETHER1394_REGION_ADDR; ptask = kmem_cache_alloc(packet_task_cache, kmflags); if (ptask == NULL) { ret = -ENOMEM; goto fail; } ptask->skb = skb; ptask->addr = addr; ptask->dest_node = dest_node; INIT_TQUEUE(&ptask->tq, hpsb_write_sched, ptask); schedule_task(&ptask->tq); return 0;fail: printk("Failed in ether1394_tx\n"); if (skb != NULL) dev_kfree_skb (skb); spin_lock_irqsave (&priv->lock, flags); priv->stats.tx_dropped++; priv->stats.tx_errors++; if (netif_queue_stopped (dev)) netif_wake_queue (dev); spin_unlock_irqrestore (&priv->lock, flags); return ret;}/* Function for incoming 1394 packets */static struct hpsb_address_ops addr_ops = { .write = ether1394_write,};/* Ieee1394 highlevel driver functions */static struct hpsb_highlevel_ops hl_ops = { .add_host = ether1394_add_host, .remove_host = ether1394_remove_host, .host_reset = ether1394_host_reset,};static int __init ether1394_init_module (void){ packet_task_cache = kmem_cache_create("packet_task", sizeof(struct packet_task), 0, 0, NULL, NULL); /* Register ourselves as a highlevel driver */ hl_handle = hpsb_register_highlevel (ETHER1394_DRIVER_NAME, &hl_ops); if (hl_handle == NULL) { ETH1394_PRINT_G (KERN_ERR, "No more memory for driver\n"); return -ENOMEM; } hpsb_register_addrspace (hl_handle, &addr_ops, ETHER1394_REGION_ADDR, ETHER1394_REGION_ADDR_END); return 0;}static void __exit ether1394_exit_module (void){ hpsb_unregister_highlevel (hl_handle); kmem_cache_destroy(packet_task_cache);}module_init(ether1394_init_module);module_exit(ether1394_exit_module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -