eth1394.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:21k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * eth1394.c -- Ethernet driver for Linux IEEE-1394 Subsystem
  3.  * 
  4.  * Copyright (C) 2001 Ben Collins <bcollins@debian.org>
  5.  *               2000 Bonin Franck <boninf@free.fr>
  6.  *
  7.  * Mainly based on work by Emanuel Pirker and Andreas E. Bombe
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software Foundation,
  21.  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  */
  23. /* State of this driver:
  24.  *
  25.  * This driver intends to support RFC 2734, which describes a method for
  26.  * transporting IPv4 datagrams over IEEE-1394 serial busses. This driver
  27.  * will ultimately support that method, but currently falls short in
  28.  * several areas. A few issues are:
  29.  *
  30.  *   - Does not support send/recv over Async streams using GASP
  31.  *     packet formats, as per the RFC for ARP requests.
  32.  *   - Does not yet support fragmented packets.
  33.  *   - Relies on hardware address being equal to the nodeid for some things.
  34.  *   - Does not support multicast
  35.  *   - Hardcoded address for sending packets, instead of using discovery
  36.  *     (ARP, see first item)
  37.  */
  38. #include <linux/module.h>
  39. #include <linux/sched.h>
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/errno.h>
  43. #include <linux/types.h>
  44. #include <linux/delay.h>
  45. #include <linux/init.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/inetdevice.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/if_arp.h>
  50. #include <linux/if_ether.h>
  51. #include <linux/ip.h>
  52. #include <linux/tcp.h>
  53. #include <linux/skbuff.h>
  54. #include <asm/delay.h>
  55. #include <asm/semaphore.h>
  56. #include <asm/bitops.h>
  57. #include <net/arp.h>
  58. #include "ieee1394_types.h"
  59. #include "ieee1394_core.h"
  60. #include "ieee1394_transactions.h"
  61. #include "ieee1394.h"
  62. #include "highlevel.h"
  63. #include "eth1394.h"
  64. #define ETH1394_PRINT_G(level, fmt, args...) 
  65. printk(level ETHER1394_DRIVER_NAME": "fmt, ## args)
  66. #define ETH1394_PRINT(level, dev_name, fmt, args...) 
  67. printk(level ETHER1394_DRIVER_NAME": %s: " fmt, dev_name, ## args)
  68. #define DEBUG(fmt, args...) 
  69. printk(KERN_ERR fmt, ## args)
  70. static char version[] __devinitdata =
  71. "$Rev: 546 $ Ben Collins <bcollins@debian.org>";
  72. /* Our ieee1394 highlevel driver */
  73. #define ETHER1394_DRIVER_NAME "ether1394"
  74. static kmem_cache_t *packet_task_cache;
  75. static struct hpsb_highlevel *hl_handle = NULL;
  76. /* Card handling */
  77. static LIST_HEAD (host_info_list);
  78. static spinlock_t host_info_lock = SPIN_LOCK_UNLOCKED;
  79. /* Use common.lf to determine header len */
  80. static int hdr_type_len[] = {
  81. sizeof (struct eth1394_uf_hdr),
  82. sizeof (struct eth1394_ff_hdr),
  83. sizeof (struct eth1394_sf_hdr),
  84. sizeof (struct eth1394_sf_hdr)
  85. };
  86. MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
  87. MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
  88. MODULE_LICENSE("GPL");
  89. /* Find our host_info struct for a given host pointer. Must be called
  90.  * under spinlock.  */
  91. static inline struct host_info *find_host_info (struct hpsb_host *host)
  92. {
  93. struct list_head *lh;
  94. struct host_info *hi;
  95. lh = host_info_list.next;
  96. while (lh != &host_info_list) {
  97. hi = list_entry (lh, struct host_info, list);
  98. if (hi->host == host)
  99. return hi;
  100. lh = lh->next;
  101. }
  102. return NULL;
  103. }
  104. /* Find the network device for our host */
  105. static inline struct net_device *ether1394_find_dev (struct hpsb_host *host)
  106. {
  107. struct host_info *hi;
  108. spin_lock_irq (&host_info_lock);
  109. hi = find_host_info (host);
  110. spin_unlock_irq (&host_info_lock);
  111. if (hi == NULL)
  112. return NULL;
  113. return hi->dev;
  114. }
  115. /* This is called after an "ifup" */
  116. static int ether1394_open (struct net_device *dev)
  117. {
  118. struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
  119. /* Set the spinlock before grabbing IRQ! */
  120. priv->lock = SPIN_LOCK_UNLOCKED;
  121. netif_start_queue (dev);
  122. return 0;
  123. }
  124. /* This is called after an "ifdown" */
  125. static int ether1394_stop (struct net_device *dev)
  126. {
  127. netif_stop_queue (dev);
  128. return 0;
  129. }
  130. /* Return statistics to the caller */
  131. static struct net_device_stats *ether1394_stats (struct net_device *dev)
  132. {
  133. return &(((struct eth1394_priv *)dev->priv)->stats);
  134. }
  135. /* What to do if we timeout. I think a host reset is probably in order, so
  136.  * that's what we do. Should we increment the stat counters too?  */
  137. static void ether1394_tx_timeout (struct net_device *dev)
  138. {
  139. ETH1394_PRINT (KERN_ERR, dev->name, "Timeout, resetting host %sn",
  140.        ((struct eth1394_priv *)(dev->priv))->host->driver->name);
  141. highlevel_host_reset (((struct eth1394_priv *)(dev->priv))->host);
  142. netif_wake_queue (dev);
  143. }
  144. /* We need to encapsulate the standard header with our own. We use the
  145.  * ethernet header's proto for our own.
  146.  *
  147.  * XXX: This is where we need to create a list of skb's for fragmented
  148.  * packets.  */
  149. static inline void ether1394_encapsulate (struct sk_buff *skb, struct net_device *dev,
  150.     int proto)
  151. {
  152. union eth1394_hdr *hdr =
  153. (union eth1394_hdr *)skb_push (skb, hdr_type_len[ETH1394_HDR_LF_UF]);
  154. hdr->common.lf = ETH1394_HDR_LF_UF;
  155. hdr->words.word1 = htons(hdr->words.word1);
  156. hdr->uf.ether_type = proto;
  157. return;
  158. }
  159. /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the
  160.  * entire arphdr) is the same format as the ip1394 header, so they
  161.  * overlap. The rest needs to be munged a bit. The remainder of the
  162.  * arphdr is formatted based on hwaddr len and ipaddr len. We know what
  163.  * they'll be, so it's easy to judge.  */
  164. static inline void ether1394_arp_to_1394arp (struct sk_buff *skb, struct net_device *dev)
  165. {
  166. struct eth1394_priv *priv =
  167. (struct eth1394_priv *)(dev->priv);
  168. u16 phy_id = priv->host->node_id & NODE_MASK;
  169. unsigned char *arp_ptr = (unsigned char *)skb->data;
  170. struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
  171. unsigned char arp_data[2*(dev->addr_len+4)];
  172. /* Copy the main data that we need */
  173. arp_ptr = memcpy (arp_data, arp_ptr + sizeof(struct arphdr), sizeof (arp_data));
  174. /* Extend the buffer enough for our new header */
  175. skb_put (skb, sizeof (struct eth1394_arp) -
  176.  (sizeof (arp_data) + sizeof (struct arphdr)));
  177. #define PROCESS_MEMBER(ptr,val,len) 
  178.   memcpy (val, ptr, len); ptr += len
  179. arp_ptr += arp1394->hw_addr_len;
  180. PROCESS_MEMBER (arp_ptr, &arp1394->sip, arp1394->ip_addr_len);
  181. arp_ptr += arp1394->hw_addr_len;
  182. PROCESS_MEMBER (arp_ptr, &arp1394->tip, arp1394->ip_addr_len);
  183. #undef PROCESS_MEMBER
  184. /* Now add our own flavor of arp header fields to the orig one */
  185. arp1394->hw_addr_len = IP1394_HW_ADDR_LEN;
  186. arp1394->hw_type = __constant_htons (ARPHRD_IEEE1394);
  187. arp1394->s_uniq_id = cpu_to_le64 (priv->eui[phy_id]);
  188. arp1394->max_rec = priv->max_rec[phy_id];
  189. arp1394->sspd = priv->sspd[phy_id];
  190. arp1394->fifo_hi = htons (priv->fifo_hi[phy_id]);
  191. arp1394->fifo_lo = htonl (priv->fifo_lo[phy_id]);
  192. return;
  193. }
  194. static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
  195. {
  196. if ((new_mtu < 68) || (new_mtu > ETHER1394_REGION_ADDR_LEN))
  197. return -EINVAL;
  198. dev->mtu = new_mtu;
  199. return 0;
  200. }
  201. static inline void ether1394_register_limits (int nodeid, unsigned char max_rec,
  202.        unsigned char sspd, u64 eui, u16 fifo_hi,
  203.        u32 fifo_lo, struct eth1394_priv *priv)
  204. {
  205. int i;
  206. if (nodeid < 0 || nodeid >= ALL_NODES) {
  207. ETH1394_PRINT_G (KERN_ERR, "Cannot register invalid nodeid %dn", nodeid);
  208. return;
  209. }
  210. priv->max_rec[nodeid] = max_rec;
  211. priv->sspd[nodeid] = sspd;
  212. priv->fifo_hi[nodeid] = fifo_hi;
  213. priv->fifo_lo[nodeid] = fifo_lo;
  214. priv->eui[nodeid] = eui;
  215. /* 63 is used for broadcasts to all hosts. It is equal to the
  216.  * minimum of all registered nodes. A registered node is one with
  217.  * a nonzero offset. Set the values rediculously high to start. We
  218.  * know we have atleast one to change the default to.  */
  219. sspd = 0xff;
  220. max_rec = 0xff;
  221. for (i = 0; i < ALL_NODES; i++) {
  222. if (!priv->fifo_hi && !priv->fifo_lo) continue; /* Unregistered */
  223. if (priv->max_rec[i] < max_rec) max_rec = priv->max_rec[i];
  224. if (priv->sspd[i] < sspd) sspd = priv->sspd[i];
  225. }
  226. priv->max_rec[ALL_NODES] = max_rec;
  227. priv->sspd[ALL_NODES] = sspd;
  228. return;
  229. }
  230. static void ether1394_reset_priv (struct net_device *dev, int set_mtu)
  231. {
  232. unsigned long flags;
  233. struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
  234. int phy_id = priv->host->node_id & NODE_MASK;
  235. spin_lock_irqsave (&priv->lock, flags);
  236. /* Clear the speed/payload/offset tables */
  237. memset (priv->max_rec, 0, sizeof (priv->max_rec));
  238. memset (priv->sspd, 0, sizeof (priv->sspd));
  239. memset (priv->fifo_hi, 0, sizeof (priv->fifo_hi));
  240. memset (priv->fifo_lo, 0, sizeof (priv->fifo_lo));
  241. /* Register our limits now */
  242. ether1394_register_limits (phy_id, (be32_to_cpu(priv->host->csr.rom[2]) >> 12) & 0xf,
  243.    priv->host->speed_map[(phy_id << 6) + phy_id],
  244.    (u64)(((u64)be32_to_cpu(priv->host->csr.rom[3]) << 32) |
  245.    be32_to_cpu(priv->host->csr.rom[4])),
  246.    ETHER1394_REGION_ADDR >> 32,
  247.    ETHER1394_REGION_ADDR & 0xffffffff, priv);
  248. /* We'll use our max_rec as the default mtu */
  249. if (set_mtu)
  250. dev->mtu = (1 << (priv->max_rec[phy_id] + 1)) - sizeof (union eth1394_hdr);
  251. /* Set our hardware address while we're at it */
  252. *(nodeid_t *)dev->dev_addr = htons (priv->host->node_id);
  253. spin_unlock_irqrestore (&priv->lock, flags);
  254. }
  255. static int ether1394_tx (struct sk_buff *skb, struct net_device *dev);
  256. /* This function is called by register_netdev */
  257. static int ether1394_init_dev (struct net_device *dev)
  258. {
  259. /* Our functions */
  260. dev->open = ether1394_open;
  261. dev->stop = ether1394_stop;
  262. dev->hard_start_xmit = ether1394_tx;
  263. dev->get_stats = ether1394_stats;
  264. dev->tx_timeout = ether1394_tx_timeout;
  265. dev->change_mtu = ether1394_change_mtu;
  266. /* Some constants */
  267. dev->watchdog_timeo = ETHER1394_TIMEOUT;
  268. dev->flags = IFF_BROADCAST; /* TODO: Support MCAP */
  269. dev->features = NETIF_F_NO_CSUM|NETIF_F_SG|NETIF_F_HIGHDMA|NETIF_F_FRAGLIST;
  270. dev->addr_len = 2;
  271. ether1394_reset_priv (dev, 1);
  272. return 0;
  273. }
  274. /*
  275.  * This function is called every time a card is found. It is generally called
  276.  * when the module is installed. This is where we add all of our ethernet
  277.  * devices. One for each host.
  278.  */
  279. static void ether1394_add_host (struct hpsb_host *host)
  280. {
  281. struct host_info *hi = NULL;
  282. struct net_device *dev = NULL;
  283. struct eth1394_priv *priv;
  284. static int version_printed = 0;
  285. if (version_printed++ == 0)
  286. ETH1394_PRINT_G (KERN_INFO, "%sn", version);
  287. dev = alloc_etherdev(sizeof (struct eth1394_priv));
  288. if (dev == NULL)
  289. goto out;
  290. SET_MODULE_OWNER(dev);
  291. dev->init = ether1394_init_dev;
  292. priv = (struct eth1394_priv *)dev->priv;
  293. priv->host = host;
  294. hi = (struct host_info *)kmalloc (sizeof (struct host_info),
  295.   GFP_KERNEL);
  296. if (hi == NULL)
  297. goto out;
  298. if (register_netdev (dev)) {
  299. ETH1394_PRINT (KERN_ERR, dev->name, "Error registering network drivern");
  300. kfree (dev);
  301. return;
  302. }
  303. ETH1394_PRINT (KERN_ERR, dev->name, "IEEE-1394 IPv4 over 1394 Ethernet (%s)n",
  304.        host->driver->name);
  305. INIT_LIST_HEAD (&hi->list);
  306. hi->host = host;
  307. hi->dev = dev;
  308. spin_lock_irq (&host_info_lock);
  309. list_add_tail (&hi->list, &host_info_list);
  310. spin_unlock_irq (&host_info_lock);
  311. return;
  312. out:
  313. if (dev != NULL)
  314. kfree (dev);
  315. ETH1394_PRINT_G (KERN_ERR, "Out of memoryn");
  316. return;
  317. }
  318. /* Remove a card from our list */
  319. static void ether1394_remove_host (struct hpsb_host *host)
  320. {
  321. struct host_info *hi;
  322. spin_lock_irq (&host_info_lock);
  323. hi = find_host_info (host);
  324. if (hi != NULL) {
  325. unregister_netdev (hi->dev);
  326. kfree (hi->dev);
  327. list_del (&hi->list);
  328. kfree (hi);
  329. }
  330. spin_unlock_irq (&host_info_lock);
  331. return;
  332. }
  333. /* A reset has just arisen */
  334. static void ether1394_host_reset (struct hpsb_host *host)
  335. {
  336. struct net_device *dev = ether1394_find_dev(host);
  337. /* This can happen for hosts that we don't use */
  338. if (dev == NULL)
  339. return;
  340. /* Reset our private host data, but not our mtu */
  341. netif_stop_queue (dev);
  342. ether1394_reset_priv (dev, 0);
  343. netif_wake_queue (dev);
  344. }
  345. /* Copied from net/ethernet/eth.c */
  346. static inline unsigned short ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
  347. {
  348. struct ethhdr *eth;
  349. unsigned char *rawp;
  350. skb->mac.raw = skb->data;
  351. skb_pull (skb, ETH_HLEN);
  352. eth = skb->mac.ethernet;
  353. #if 0
  354. if(*eth->h_dest & 1) {
  355. if(memcmp(eth->h_dest, dev->broadcast, dev->addr_len)==0)
  356. skb->pkt_type = PACKET_BROADCAST;
  357. else
  358. skb->pkt_type = PACKET_MULTICAST;
  359. } else {
  360. if(memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
  361. skb->pkt_type = PACKET_OTHERHOST;
  362.         }
  363. #endif
  364. if (ntohs (eth->h_proto) >= 1536)
  365. return eth->h_proto;
  366. rawp = skb->data;
  367.         if (*(unsigned short *)rawp == 0xFFFF)
  368. return htons (ETH_P_802_3);
  369.         return htons (ETH_P_802_2);
  370. }
  371. /* Parse an encapsulated IP1394 header into an ethernet frame packet.
  372.  * We also perform ARP translation here, if need be.  */
  373. static inline unsigned short ether1394_parse_encap (struct sk_buff *skb, struct net_device *dev,
  374.      nodeid_t srcid, nodeid_t destid)
  375. {
  376. union eth1394_hdr *hdr = (union eth1394_hdr *)skb->data;
  377. unsigned char src_hw[ETH_ALEN], dest_hw[ETH_ALEN];
  378. unsigned short ret = 0;
  379. /* Setup our hw addresses. We use these to build the
  380.  * ethernet header.  */
  381. *(u16 *)dest_hw = htons(destid);
  382. *(u16 *)src_hw = htons(srcid);
  383. /* Remove the encapsulation header */
  384. hdr->words.word1 = ntohs(hdr->words.word1);
  385. skb_pull (skb, hdr_type_len[hdr->common.lf]);
  386. /* If this is an ARP packet, convert it. First, we want to make
  387.  * use of some of the fields, since they tell us a little bit
  388.  * about the sending machine.  */
  389. if (hdr->uf.ether_type == __constant_htons (ETH_P_ARP)) {
  390. unsigned long flags;
  391. u16 phy_id = srcid & NODE_MASK;
  392. struct eth1394_priv *priv =
  393. (struct eth1394_priv *)dev->priv;
  394. struct eth1394_arp arp1394;
  395. struct arphdr *arp = (struct arphdr *)skb->data;
  396. unsigned char *arp_ptr = (unsigned char *)(arp + 1);
  397. memcpy (&arp1394, arp, sizeof (struct eth1394_arp));
  398. /* Update our speed/payload/fifo_offset table */
  399. spin_lock_irqsave (&priv->lock, flags);
  400. ether1394_register_limits (phy_id, arp1394.max_rec, arp1394.sspd,
  401.    le64_to_cpu (arp1394.s_uniq_id),
  402.    ntohs (arp1394.fifo_hi),
  403.    ntohl (arp1394.fifo_lo), priv);
  404. spin_unlock_irqrestore (&priv->lock, flags);
  405. #define PROCESS_MEMBER(ptr,val,len) 
  406.   ptr = memcpy (ptr, val, len) + len
  407.                 PROCESS_MEMBER (arp_ptr, src_hw, dev->addr_len);
  408.                 PROCESS_MEMBER (arp_ptr, &arp1394.sip, 4);
  409.                 PROCESS_MEMBER (arp_ptr, dest_hw, dev->addr_len);
  410.                 PROCESS_MEMBER (arp_ptr, &arp1394.tip, 4);
  411. #undef PROCESS_MEMBER
  412. arp->ar_hln = dev->addr_len;
  413. arp->ar_hrd = __constant_htons (ARPHRD_ETHER);
  414. skb_trim (skb, sizeof (struct arphdr) + 2*(dev->addr_len+4));
  415. }
  416. /* Now add the ethernet header. */
  417. if (dev->hard_header (skb, dev, __constant_ntohs (hdr->uf.ether_type),
  418.       dest_hw, src_hw, skb->len) >= 0)
  419. ret = ether1394_type_trans(skb, dev);
  420. return ret;
  421. }
  422. /* Packet reception. We convert the IP1394 encapsulation header to an
  423.  * ethernet header, and fill it with some of our other fields. This is
  424.  * an incoming packet from the 1394 bus.  */
  425. static int ether1394_write (struct hpsb_host *host, int srcid, int destid,
  426.     quadlet_t *data, u64 addr, unsigned int len)
  427. {
  428. struct sk_buff *skb;
  429. char *buf = (char *)data;
  430. unsigned long flags;
  431. struct net_device *dev = ether1394_find_dev (host);
  432. struct eth1394_priv *priv;
  433. if (dev == NULL) {
  434. ETH1394_PRINT_G (KERN_ERR, "Could not find net device for host %pn",
  435.  host);
  436. return RCODE_ADDRESS_ERROR;
  437. }
  438. priv = (struct eth1394_priv *)dev->priv;
  439. /* A packet has been received by the ieee1394 bus. Build an skbuff
  440.  * around it so we can pass it to the high level network layer. */
  441. skb = dev_alloc_skb (len + dev->hard_header_len + 15);
  442. if (!skb) {
  443. HPSB_PRINT (KERN_ERR, "ether1394 rx: low on memn");
  444. priv->stats.rx_dropped++;
  445. return RCODE_ADDRESS_ERROR;
  446. }
  447. skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
  448. memcpy (skb_put (skb, len), buf, len);
  449. /* Write metadata, and then pass to the receive level */
  450. skb->dev = dev;
  451. skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
  452. /* Parse the encapsulation header. This actually does the job of
  453.  * converting to an ethernet frame header, aswell as arp
  454.  * conversion if needed. ARP conversion is easier in this
  455.  * direction, since we are using ethernet as our backend.  */
  456. skb->protocol = ether1394_parse_encap (skb, dev, srcid, destid);
  457. spin_lock_irqsave (&priv->lock, flags);
  458. if (!skb->protocol) {
  459. priv->stats.rx_errors++;
  460. priv->stats.rx_dropped++;
  461. dev_kfree_skb_any(skb);
  462. goto bad_proto;
  463. }
  464. netif_stop_queue(dev);
  465. if (netif_rx (skb) == NET_RX_DROP) {
  466. priv->stats.rx_errors++;
  467. priv->stats.rx_dropped++;
  468. goto bad_proto;
  469. }
  470. /* Statistics */
  471. priv->stats.rx_packets++;
  472. priv->stats.rx_bytes += skb->len;
  473. bad_proto:
  474. netif_start_queue(dev);
  475. spin_unlock_irqrestore (&priv->lock, flags);
  476. dev->last_rx = jiffies;
  477. return RCODE_COMPLETE;
  478. }
  479. /* This function is our scheduled write */
  480. static void hpsb_write_sched (void *__ptask)
  481. {
  482. struct packet_task *ptask = (struct packet_task *)__ptask;
  483. struct sk_buff *skb = ptask->skb;
  484. struct net_device *dev = ptask->skb->dev;
  485. struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
  486.         unsigned long flags;
  487. /* Statistics */
  488. spin_lock_irqsave (&priv->lock, flags);
  489. if (!hpsb_write(priv->host, ptask->dest_node,
  490. get_hpsb_generation(priv->host),
  491. ptask->addr, (quadlet_t *)skb->data, skb->len)) {
  492. priv->stats.tx_bytes += skb->len;
  493. priv->stats.tx_packets++;
  494. } else {
  495. //printk("Failed in hpsb_write_schedn");
  496. priv->stats.tx_dropped++;
  497. priv->stats.tx_errors++;
  498. if (netif_queue_stopped (dev))
  499. netif_wake_queue (dev);
  500. }
  501. spin_unlock_irqrestore (&priv->lock, flags);
  502. dev->trans_start = jiffies;
  503. dev_kfree_skb(skb);
  504. kmem_cache_free(packet_task_cache, ptask);
  505. return;
  506. }
  507. /* Transmit a packet (called by kernel) */
  508. static int ether1394_tx (struct sk_buff *skb, struct net_device *dev)
  509. {
  510. int kmflags = in_interrupt () ? GFP_ATOMIC : GFP_KERNEL;
  511. struct ethhdr *eth;
  512. struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
  513. int proto;
  514. unsigned long flags;
  515. nodeid_t dest_node;
  516. u64 addr;
  517. struct packet_task *ptask = NULL;
  518. int ret = 0;
  519. if ((skb = skb_share_check (skb, kmflags)) == NULL) {
  520. ret = -ENOMEM;
  521. goto fail;
  522. }
  523. /* Get rid of the ethernet header, but save a pointer */
  524. eth = (struct ethhdr *)skb->data;
  525. skb_pull (skb, ETH_HLEN);
  526. /* Save the destination id, and proto for our encapsulation, then
  527.  * toss the ethernet header aside like the cheap whore it is.  */
  528. dest_node = ntohs (*(nodeid_t *)(eth->h_dest));
  529. proto = eth->h_proto;
  530. /* If this is an ARP packet, convert it */
  531. if (proto == __constant_htons (ETH_P_ARP))
  532. ether1394_arp_to_1394arp (skb, dev);
  533. /* Now add our encapsulation header */
  534. ether1394_encapsulate (skb, dev, proto);
  535. /* TODO: The above encapsulate function needs to recognize when a
  536.  * packet needs to be split for a specified node. It should create
  537.  * a list of skb's that we could then iterate over for the below
  538.  * call to schedule our writes.  */
  539. /* XXX: Right now we accept that we don't exactly follow RFC. When
  540.  * we do, we will send ARP requests via GASP format, and so we wont
  541.  * need this hack.  */
  542. spin_lock_irqsave (&priv->lock, flags);
  543. addr = (u64)priv->fifo_hi[dest_node & NODE_MASK] << 32 |
  544. priv->fifo_lo[dest_node & NODE_MASK];
  545. spin_unlock_irqrestore (&priv->lock, flags);
  546. if (!addr)
  547. addr = ETHER1394_REGION_ADDR;
  548. ptask = kmem_cache_alloc(packet_task_cache, kmflags);
  549. if (ptask == NULL) {
  550. ret = -ENOMEM;
  551. goto fail;
  552. }
  553. ptask->skb = skb;
  554. ptask->addr = addr;
  555. ptask->dest_node = dest_node;
  556. INIT_TQUEUE(&ptask->tq, hpsb_write_sched, ptask);
  557. schedule_task(&ptask->tq);
  558. return 0;
  559. fail:
  560. printk("Failed in ether1394_txn");
  561. if (skb != NULL)
  562. dev_kfree_skb (skb);
  563. spin_lock_irqsave (&priv->lock, flags);
  564. priv->stats.tx_dropped++;
  565. priv->stats.tx_errors++;
  566. if (netif_queue_stopped (dev))
  567. netif_wake_queue (dev);
  568. spin_unlock_irqrestore (&priv->lock, flags);
  569. return ret;
  570. }
  571. /* Function for incoming 1394 packets */
  572. static struct hpsb_address_ops addr_ops = {
  573. .write = ether1394_write,
  574. };
  575. /* Ieee1394 highlevel driver functions */
  576. static struct hpsb_highlevel_ops hl_ops = {
  577. .add_host = ether1394_add_host,
  578. .remove_host = ether1394_remove_host,
  579. .host_reset = ether1394_host_reset,
  580. };
  581. static int __init ether1394_init_module (void)
  582. {
  583. packet_task_cache = kmem_cache_create("packet_task", sizeof(struct packet_task),
  584.       0, 0, NULL, NULL);
  585. /* Register ourselves as a highlevel driver */
  586. hl_handle = hpsb_register_highlevel (ETHER1394_DRIVER_NAME, &hl_ops);
  587. if (hl_handle == NULL) {
  588. ETH1394_PRINT_G (KERN_ERR, "No more memory for drivern");
  589. return -ENOMEM;
  590. }
  591. hpsb_register_addrspace (hl_handle, &addr_ops, ETHER1394_REGION_ADDR,
  592.  ETHER1394_REGION_ADDR_END);
  593. return 0;
  594. }
  595. static void __exit ether1394_exit_module (void)
  596. {
  597. hpsb_unregister_highlevel (hl_handle);
  598. kmem_cache_destroy(packet_task_cache);
  599. }
  600. module_init(ether1394_init_module);
  601. module_exit(ether1394_exit_module);