ieee1394_core.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:28k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * IEEE 1394 for Linux
  3.  *
  4.  * Core support: hpsb_packet management, packet handling and forwarding to
  5.  *               highlevel or lowlevel code
  6.  *
  7.  * Copyright (C) 1999, 2000 Andreas E. Bombe
  8.  *
  9.  * This code is licensed under the GPL.  See the file COPYING in the root
  10.  * directory of the kernel sources for details.
  11.  */
  12. #include <linux/config.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <asm/bitops.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/semaphore.h>
  23. #include "ieee1394_types.h"
  24. #include "ieee1394.h"
  25. #include "hosts.h"
  26. #include "ieee1394_core.h"
  27. #include "highlevel.h"
  28. #include "ieee1394_transactions.h"
  29. #include "csr.h"
  30. #include "nodemgr.h"
  31. #include "ieee1394_hotplug.h"
  32. /*
  33.  * Disable the nodemgr detection and config rom reading functionality.
  34.  */
  35. MODULE_PARM(disable_nodemgr, "i");
  36. MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
  37. static int disable_nodemgr = 0;
  38. /* We are GPL, so treat us special */
  39. MODULE_LICENSE("GPL");
  40. static kmem_cache_t *hpsb_packet_cache;
  41. /* Some globals used */
  42. const char *hpsb_speedto_str[] = { "S100", "S200", "S400" };
  43. static void dump_packet(const char *text, quadlet_t *data, int size)
  44. {
  45.         int i;
  46.         size /= 4;
  47.         size = (size > 4 ? 4 : size);
  48.         printk(KERN_DEBUG "ieee1394: %s", text);
  49.         for (i = 0; i < size; i++) {
  50.                 printk(" %8.8x", data[i]);
  51.         }
  52.         printk("n");
  53. }
  54. /**
  55.  * alloc_hpsb_packet - allocate new packet structure
  56.  * @data_size: size of the data block to be allocated
  57.  *
  58.  * This function allocates, initializes and returns a new &struct hpsb_packet.
  59.  * It can be used in interrupt context.  A header block is always included, its
  60.  * size is big enough to contain all possible 1394 headers.  The data block is
  61.  * only allocated when @data_size is not zero.
  62.  *
  63.  * For packets for which responses will be received the @data_size has to be big
  64.  * enough to contain the response's data block since no further allocation
  65.  * occurs at response matching time.
  66.  *
  67.  * The packet's generation value will be set to the current generation number
  68.  * for ease of use.  Remember to overwrite it with your own recorded generation
  69.  * number if you can not be sure that your code will not race with a bus reset.
  70.  *
  71.  * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
  72.  * failure.
  73.  */
  74. struct hpsb_packet *alloc_hpsb_packet(size_t data_size)
  75. {
  76.         struct hpsb_packet *packet = NULL;
  77.         void *data = NULL;
  78.         int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
  79.         packet = kmem_cache_alloc(hpsb_packet_cache, kmflags);
  80.         if (packet == NULL)
  81.                 return NULL;
  82.         memset(packet, 0, sizeof(struct hpsb_packet));
  83.         packet->header = packet->embedded_header;
  84.         if (data_size) {
  85.                 data = kmalloc(data_size + 8, kmflags);
  86.                 if (data == NULL) {
  87. kmem_cache_free(hpsb_packet_cache, packet);
  88.                         return NULL;
  89.                 }
  90.                 packet->data = data;
  91.                 packet->data_size = data_size;
  92.         }
  93.         INIT_TQ_HEAD(packet->complete_tq);
  94.         INIT_LIST_HEAD(&packet->list);
  95.         sema_init(&packet->state_change, 0);
  96.         packet->state = hpsb_unused;
  97.         packet->generation = -1;
  98.         packet->data_be = 1;
  99.         return packet;
  100. }
  101. /**
  102.  * free_hpsb_packet - free packet and data associated with it
  103.  * @packet: packet to free (is NULL safe)
  104.  *
  105.  * This function will free packet->data, packet->header and finally the packet
  106.  * itself.
  107.  */
  108. void free_hpsb_packet(struct hpsb_packet *packet)
  109. {
  110.         if (!packet) return;
  111.         kfree(packet->data);
  112.         kmem_cache_free(hpsb_packet_cache, packet);
  113. }
  114. int hpsb_reset_bus(struct hpsb_host *host, int type)
  115. {
  116.         if (!host->initialized) {
  117.                 return 1;
  118.         }
  119.         if (!host->in_bus_reset) {
  120.                 host->template->devctl(host, RESET_BUS, type);
  121.                 return 0;
  122.         } else {
  123.                 return 1;
  124.         }
  125. }
  126. int hpsb_bus_reset(struct hpsb_host *host)
  127. {
  128.         if (host->in_bus_reset) {
  129.                 HPSB_NOTICE(__FUNCTION__ 
  130.                             " called while bus reset already in progress");
  131.                 return 1;
  132.         }
  133.         abort_requests(host);
  134.         host->in_bus_reset = 1;
  135.         host->irm_id = -1;
  136.         host->busmgr_id = -1;
  137.         host->node_count = 0;
  138.         host->selfid_count = 0;
  139.         return 0;
  140. }
  141. /*
  142.  * Verify num_of_selfids SelfIDs and return number of nodes.  Return zero in
  143.  * case verification failed.
  144.  */
  145. static int check_selfids(struct hpsb_host *host, unsigned int num_of_selfids)
  146. {
  147.         int nodeid = -1;
  148.         int rest_of_selfids = num_of_selfids;
  149.         struct selfid *sid = (struct selfid *)host->topology_map;
  150.         struct ext_selfid *esid;
  151.         int esid_seq = 23;
  152.         while (rest_of_selfids--) {
  153.                 if (!sid->extended) {
  154.                         nodeid++;
  155.                         esid_seq = 0;
  156.                         
  157.                         if (sid->phy_id != nodeid) {
  158.                                 HPSB_INFO("SelfIDs failed monotony check with "
  159.                                           "%d", sid->phy_id);
  160.                                 return 0;
  161.                         }
  162.                         
  163.                         if (sid->contender && sid->link_active) {
  164.                                 host->irm_id = LOCAL_BUS | sid->phy_id;
  165.                         }
  166.                 } else {
  167.                         esid = (struct ext_selfid *)sid;
  168.                         if ((esid->phy_id != nodeid) 
  169.                             || (esid->seq_nr != esid_seq)) {
  170.                                 HPSB_INFO("SelfIDs failed monotony check with "
  171.                                           "%d/%d", esid->phy_id, esid->seq_nr);
  172.                                 return 0;
  173.                         }
  174.                         esid_seq++;
  175.                 }
  176.                 sid++;
  177.         }
  178.         
  179.         esid = (struct ext_selfid *)(sid - 1);
  180.         while (esid->extended) {
  181.                 if ((esid->porta == 0x2) || (esid->portb == 0x2)
  182.                     || (esid->portc == 0x2) || (esid->portd == 0x2)
  183.                     || (esid->porte == 0x2) || (esid->portf == 0x2)
  184.                     || (esid->portg == 0x2) || (esid->porth == 0x2)) {
  185.                                 HPSB_INFO("SelfIDs failed root check on "
  186.                                           "extended SelfID");
  187.                                 return 0;
  188.                 }
  189.                 esid--;
  190.         }
  191.         sid = (struct selfid *)esid;
  192.         if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
  193.                         HPSB_INFO("SelfIDs failed root check");
  194.                         return 0;
  195.         }
  196.         return nodeid + 1;
  197. }
  198. static void build_speed_map(struct hpsb_host *host, int nodecount)
  199. {
  200.         char speedcap[nodecount];
  201.         char cldcnt[nodecount];
  202.         u8 *map = host->speed_map;
  203.         struct selfid *sid;
  204.         struct ext_selfid *esid;
  205.         int i, j, n;
  206.         for (i = 0; i < (nodecount * 64); i += 64) {
  207.                 for (j = 0; j < nodecount; j++) {
  208.                         map[i+j] = SPEED_400;
  209.                 }
  210.         }
  211.         for (i = 0; i < nodecount; i++) {
  212.                 cldcnt[i] = 0;
  213.         }
  214.         /* find direct children count and speed */
  215.         for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
  216.                      n = nodecount - 1;
  217.              (void *)sid >= (void *)host->topology_map; sid--) {
  218.                 if (sid->extended) {
  219.                         esid = (struct ext_selfid *)sid;
  220.                         if (esid->porta == 0x3) cldcnt[n]++;
  221.                         if (esid->portb == 0x3) cldcnt[n]++;
  222.                         if (esid->portc == 0x3) cldcnt[n]++;
  223.                         if (esid->portd == 0x3) cldcnt[n]++;
  224.                         if (esid->porte == 0x3) cldcnt[n]++;
  225.                         if (esid->portf == 0x3) cldcnt[n]++;
  226.                         if (esid->portg == 0x3) cldcnt[n]++;
  227.                         if (esid->porth == 0x3) cldcnt[n]++;
  228.                 } else {
  229.                         if (sid->port0 == 0x3) cldcnt[n]++;
  230.                         if (sid->port1 == 0x3) cldcnt[n]++;
  231.                         if (sid->port2 == 0x3) cldcnt[n]++;
  232.                         speedcap[n] = sid->speed;
  233.                         n--;
  234.                 }
  235.         }
  236.         /* set self mapping */
  237.         for (i = 0; i < nodecount; i++) {
  238.                 map[64*i + i] = speedcap[i];
  239.         }
  240.         /* fix up direct children count to total children count;
  241.          * also fix up speedcaps for sibling and parent communication */
  242.         for (i = 1; i < nodecount; i++) {
  243.                 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
  244.                         cldcnt[i] += cldcnt[n];
  245.                         speedcap[n] = MIN(speedcap[n], speedcap[i]);
  246.                         n -= cldcnt[n] + 1;
  247.                 }
  248.         }
  249.         for (n = 0; n < nodecount; n++) {
  250.                 for (i = n - cldcnt[n]; i <= n; i++) {
  251.                         for (j = 0; j < (n - cldcnt[n]); j++) {
  252.                                 map[j*64 + i] = map[i*64 + j] =
  253.                                         MIN(map[i*64 + j], speedcap[n]);
  254.                         }
  255.                         for (j = n + 1; j < nodecount; j++) {
  256.                                 map[j*64 + i] = map[i*64 + j] =
  257.                                         MIN(map[i*64 + j], speedcap[n]);
  258.                         }
  259.                 }
  260.         }
  261. }
  262. void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
  263. {
  264.         if (host->in_bus_reset) {
  265. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  266.                 HPSB_INFO("Including SelfID 0x%x", sid);
  267. #endif
  268.                 host->topology_map[host->selfid_count++] = sid;
  269.         } else {
  270.                 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
  271.     sid, (host->node_id & BUS_MASK) >> 6);
  272.         }
  273. }
  274. void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
  275. {
  276.         host->node_id = LOCAL_BUS | phyid;
  277.         host->in_bus_reset = 0;
  278.         host->is_root = isroot;
  279.         host->node_count = check_selfids(host, host->selfid_count);
  280.         if (!host->node_count) {
  281.                 if (host->reset_retries++ < 20) {
  282.                         /* selfid stage did not complete without error */
  283.                         HPSB_NOTICE("Error in SelfID stage, resetting");
  284.                         hpsb_reset_bus(host, LONG_RESET);
  285.                         return;
  286.                 } else {
  287.                         HPSB_NOTICE("Stopping out-of-control reset loop");
  288.                         HPSB_NOTICE("Warning - topology map and speed map will not be valid");
  289.                 }
  290.         } else {
  291.                 build_speed_map(host, host->node_count);
  292.         }
  293.         /* irm_id is kept up to date by check_selfids() */
  294.         if (host->irm_id == host->node_id) {
  295.                 host->is_irm = 1;
  296.                 host->is_busmgr = 1;
  297.                 host->busmgr_id = host->node_id;
  298.                 host->csr.bus_manager_id = host->node_id;
  299.         }
  300.         host->reset_retries = 0;
  301.         atomic_inc(&host->generation);
  302.         if (isroot) host->template->devctl(host, ACT_CYCLE_MASTER, 1);
  303.         highlevel_host_reset(host);
  304. }
  305. void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet, 
  306.                       int ackcode)
  307. {
  308.         unsigned long flags;
  309.         packet->ack_code = ackcode;
  310.         if (packet->no_waiter) {
  311.                 /* must not have a tlabel allocated */
  312.                 free_hpsb_packet(packet);
  313.                 return;
  314.         }
  315.         if (ackcode != ACK_PENDING || !packet->expect_response) {
  316.                 packet->state = hpsb_complete;
  317.                 up(&packet->state_change);
  318.                 up(&packet->state_change);
  319.                 run_task_queue(&packet->complete_tq);
  320.                 return;
  321.         }
  322.         packet->state = hpsb_pending;
  323.         packet->sendtime = jiffies;
  324.         spin_lock_irqsave(&host->pending_pkt_lock, flags);
  325.         list_add_tail(&packet->list, &host->pending_packets);
  326.         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  327.         up(&packet->state_change);
  328.         queue_task(&host->timeout_tq, &tq_timer);
  329. }
  330. /**
  331.  * hpsb_send_packet - transmit a packet on the bus
  332.  * @packet: packet to send
  333.  *
  334.  * The packet is sent through the host specified in the packet->host field.
  335.  * Before sending, the packet's transmit speed is automatically determined using
  336.  * the local speed map when it is an async, non-broadcast packet.
  337.  *
  338.  * Possibilities for failure are that host is either not initialized, in bus
  339.  * reset, the packet's generation number doesn't match the current generation
  340.  * number or the host reports a transmit error.
  341.  *
  342.  * Return value: False (0) on failure, true (1) otherwise.
  343.  */
  344. int hpsb_send_packet(struct hpsb_packet *packet)
  345. {
  346.         struct hpsb_host *host = packet->host;
  347.         if (!host->initialized || host->in_bus_reset 
  348.             || (packet->generation != get_hpsb_generation(host))) {
  349.                 return 0;
  350.         }
  351.         packet->state = hpsb_queued;
  352.         if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
  353.                 packet->speed_code =
  354.                         host->speed_map[(host->node_id & NODE_MASK) * 64
  355.                                        + (packet->node_id & NODE_MASK)];
  356.         }
  357. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  358.         switch (packet->speed_code) {
  359.         case 2:
  360.                 dump_packet("send packet 400:", packet->header,
  361.                             packet->header_size);
  362.                 break;
  363.         case 1:
  364.                 dump_packet("send packet 200:", packet->header,
  365.                             packet->header_size);
  366.                 break;
  367.         default:
  368.                 dump_packet("send packet 100:", packet->header,
  369.                             packet->header_size);
  370.         }
  371. #endif
  372.         return host->template->transmit_packet(host, packet);
  373. }
  374. static void send_packet_nocare(struct hpsb_packet *packet)
  375. {
  376.         if (!hpsb_send_packet(packet)) {
  377.                 free_hpsb_packet(packet);
  378.         }
  379. }
  380. void handle_packet_response(struct hpsb_host *host, int tcode, quadlet_t *data,
  381.                             size_t size)
  382. {
  383.         struct hpsb_packet *packet = NULL;
  384.         struct list_head *lh;
  385.         int tcode_match = 0;
  386.         int tlabel;
  387.         unsigned long flags;
  388.         tlabel = (data[0] >> 10) & 0x3f;
  389.         spin_lock_irqsave(&host->pending_pkt_lock, flags);
  390.         list_for_each(lh, &host->pending_packets) {
  391.                 packet = list_entry(lh, struct hpsb_packet, list);
  392.                 if ((packet->tlabel == tlabel)
  393.                     && (packet->node_id == (data[1] >> 16))){
  394.                         break;
  395.                 }
  396.         }
  397.         if (lh == &host->pending_packets) {
  398.                 HPSB_DEBUG("unsolicited response packet received - np");
  399.                 dump_packet("contents:", data, 16);
  400.                 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  401.                 return;
  402.         }
  403.         switch (packet->tcode) {
  404.         case TCODE_WRITEQ:
  405.         case TCODE_WRITEB:
  406.                 if (tcode == TCODE_WRITE_RESPONSE) tcode_match = 1;
  407.                 break;
  408.         case TCODE_READQ:
  409.                 if (tcode == TCODE_READQ_RESPONSE) tcode_match = 1;
  410.                 break;
  411.         case TCODE_READB:
  412.                 if (tcode == TCODE_READB_RESPONSE) tcode_match = 1;
  413.                 break;
  414.         case TCODE_LOCK_REQUEST:
  415.                 if (tcode == TCODE_LOCK_RESPONSE) tcode_match = 1;
  416.                 break;
  417.         }
  418.         if (!tcode_match || (packet->tlabel != tlabel)
  419.             || (packet->node_id != (data[1] >> 16))) {
  420.                 HPSB_INFO("unsolicited response packet received");
  421.                 dump_packet("contents:", data, 16);
  422.                 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  423.                 return;
  424.         }
  425.         list_del(&packet->list);
  426.         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  427.         /* FIXME - update size fields? */
  428.         switch (tcode) {
  429.         case TCODE_WRITE_RESPONSE:
  430.                 memcpy(packet->header, data, 12);
  431.                 break;
  432.         case TCODE_READQ_RESPONSE:
  433.                 memcpy(packet->header, data, 16);
  434.                 break;
  435.         case TCODE_READB_RESPONSE:
  436.                 memcpy(packet->header, data, 16);
  437.                 memcpy(packet->data, data + 4, size - 16);
  438.                 break;
  439.         case TCODE_LOCK_RESPONSE:
  440.                 memcpy(packet->header, data, 16);
  441.                 memcpy(packet->data, data + 4, (size - 16) > 8 ? 8 : size - 16);
  442.                 break;
  443.         }
  444.         packet->state = hpsb_complete;
  445.         up(&packet->state_change);
  446.         run_task_queue(&packet->complete_tq);
  447. }
  448. static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
  449.        quadlet_t *data, size_t dsize)
  450. {
  451.         struct hpsb_packet *p;
  452.         dsize += (dsize % 4 ? 4 - (dsize % 4) : 0);
  453.         p = alloc_hpsb_packet(dsize);
  454.         if (p == NULL) {
  455.                 /* FIXME - send data_error response */
  456.                 return NULL;
  457.         }
  458.         p->type = hpsb_async;
  459.         p->state = hpsb_unused;
  460.         p->host = host;
  461.         p->node_id = data[1] >> 16;
  462.         p->tlabel = (data[0] >> 10) & 0x3f;
  463.         p->no_waiter = 1;
  464. p->generation = get_hpsb_generation(host);
  465.         if (dsize % 4) {
  466.                 p->data[dsize / 4] = 0;
  467.         }
  468.         return p;
  469. }
  470. #define PREP_REPLY_PACKET(length) 
  471.                 packet = create_reply_packet(host, data, length); 
  472.                 if (packet == NULL) break
  473. static void handle_incoming_packet(struct hpsb_host *host, int tcode,
  474.    quadlet_t *data, size_t size, int write_acked)
  475. {
  476.         struct hpsb_packet *packet;
  477.         int length, rcode, extcode;
  478.         nodeid_t source = data[1] >> 16;
  479. nodeid_t dest = data[0] >> 16;
  480.         u64 addr;
  481.         /* big FIXME - no error checking is done for an out of bounds length */
  482.         switch (tcode) {
  483.         case TCODE_WRITEQ:
  484.                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
  485.                 rcode = highlevel_write(host, source, dest, data+3,
  486. addr, 4);
  487.                 if (!write_acked
  488.                     && ((data[0] >> 16) & NODE_MASK) != NODE_MASK) {
  489.                         /* not a broadcast write, reply */
  490.                         PREP_REPLY_PACKET(0);
  491.                         fill_async_write_resp(packet, rcode);
  492.                         send_packet_nocare(packet);
  493.                 }
  494.                 break;
  495.         case TCODE_WRITEB:
  496.                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
  497.                 rcode = highlevel_write(host, source, dest, data+4,
  498. addr, data[3]>>16);
  499.                 if (!write_acked
  500.                     && ((data[0] >> 16) & NODE_MASK) != NODE_MASK) {
  501.                         /* not a broadcast write, reply */
  502.                         PREP_REPLY_PACKET(0);
  503.                         fill_async_write_resp(packet, rcode);
  504.                         send_packet_nocare(packet);
  505.                 }
  506.                 break;
  507.         case TCODE_READQ:
  508.                 PREP_REPLY_PACKET(0);
  509.                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
  510.                 rcode = highlevel_read(host, source, data, addr, 4);
  511.                 fill_async_readquad_resp(packet, rcode, *data);
  512.                 send_packet_nocare(packet);
  513.                 break;
  514.         case TCODE_READB:
  515.                 length = data[3] >> 16;
  516.                 PREP_REPLY_PACKET(length);
  517.                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
  518.                 rcode = highlevel_read(host, source, packet->data, addr,
  519.                                        length);
  520.                 fill_async_readblock_resp(packet, rcode, length);
  521.                 send_packet_nocare(packet);
  522.                 break;
  523.         case TCODE_LOCK_REQUEST:
  524.                 length = data[3] >> 16;
  525.                 extcode = data[3] & 0xffff;
  526.                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
  527.                 PREP_REPLY_PACKET(8);
  528.                 if ((extcode == 0) || (extcode >= 7)) {
  529.                         /* let switch default handle error */
  530.                         length = 0;
  531.                 }
  532.                 switch (length) {
  533.                 case 4:
  534.                         rcode = highlevel_lock(host, source, packet->data, addr,
  535.                                                data[4], 0, extcode);
  536.                         fill_async_lock_resp(packet, rcode, extcode, 4);
  537.                         break;
  538.                 case 8:
  539.                         if ((extcode != EXTCODE_FETCH_ADD) 
  540.                             && (extcode != EXTCODE_LITTLE_ADD)) {
  541.                                 rcode = highlevel_lock(host, source,
  542.                                                        packet->data, addr,
  543.                                                        data[5], data[4], 
  544.                                                        extcode);
  545.                                 fill_async_lock_resp(packet, rcode, extcode, 4);
  546.                         } else {
  547.                                 rcode = highlevel_lock64(host, source,
  548.                                              (octlet_t *)packet->data, addr,
  549.                                              *(octlet_t *)(data + 4), 0ULL,
  550.                                              extcode);
  551.                                 fill_async_lock_resp(packet, rcode, extcode, 8);
  552.                         }
  553.                         break;
  554.                 case 16:
  555.                         rcode = highlevel_lock64(host, source,
  556.                                                  (octlet_t *)packet->data, addr,
  557.                                                  *(octlet_t *)(data + 6),
  558.                                                  *(octlet_t *)(data + 4), 
  559.                                                  extcode);
  560.                         fill_async_lock_resp(packet, rcode, extcode, 8);
  561.                         break;
  562.                 default:
  563.                         fill_async_lock_resp(packet, RCODE_TYPE_ERROR,
  564.                                              extcode, 0);
  565.                 }
  566.                 send_packet_nocare(packet);
  567.                 break;
  568.         }
  569. }
  570. #undef PREP_REPLY_PACKET
  571. void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
  572.                           int write_acked)
  573. {
  574.         int tcode;
  575.         if (host->in_bus_reset) {
  576.                 HPSB_INFO("received packet during reset; ignoring");
  577.                 return;
  578.         }
  579. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  580.         dump_packet("received packet:", data, size);
  581. #endif
  582.         tcode = (data[0] >> 4) & 0xf;
  583.         switch (tcode) {
  584.         case TCODE_WRITE_RESPONSE:
  585.         case TCODE_READQ_RESPONSE:
  586.         case TCODE_READB_RESPONSE:
  587.         case TCODE_LOCK_RESPONSE:
  588.                 handle_packet_response(host, tcode, data, size);
  589.                 break;
  590.         case TCODE_WRITEQ:
  591.         case TCODE_WRITEB:
  592.         case TCODE_READQ:
  593.         case TCODE_READB:
  594.         case TCODE_LOCK_REQUEST:
  595.                 handle_incoming_packet(host, tcode, data, size, write_acked);
  596.                 break;
  597.         case TCODE_ISO_DATA:
  598.                 highlevel_iso_receive(host, data, size);
  599.                 break;
  600.         case TCODE_CYCLE_START:
  601.                 /* simply ignore this packet if it is passed on */
  602.                 break;
  603.         default:
  604.                 HPSB_NOTICE("received packet with bogus transaction code %d", 
  605.                             tcode);
  606.                 break;
  607.         }
  608. }
  609. void abort_requests(struct hpsb_host *host)
  610. {
  611.         unsigned long flags;
  612.         struct hpsb_packet *packet;
  613.         struct list_head *lh;
  614.         LIST_HEAD(llist);
  615.         host->template->devctl(host, CANCEL_REQUESTS, 0);
  616.         spin_lock_irqsave(&host->pending_pkt_lock, flags);
  617.         list_splice(&host->pending_packets, &llist);
  618.         INIT_LIST_HEAD(&host->pending_packets);
  619.         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  620.         list_for_each(lh, &llist) {
  621.                 packet = list_entry(lh, struct hpsb_packet, list);
  622.                 packet->state = hpsb_complete;
  623.                 packet->ack_code = ACKX_ABORTED;
  624.                 up(&packet->state_change);
  625.                 run_task_queue(&packet->complete_tq);
  626.         }
  627. }
  628. void abort_timedouts(struct hpsb_host *host)
  629. {
  630.         unsigned long flags;
  631.         struct hpsb_packet *packet;
  632.         unsigned long expire;
  633.         struct list_head *lh, *next;
  634.         LIST_HEAD(expiredlist);
  635.         spin_lock_irqsave(&host->csr.lock, flags);
  636.         expire = (host->csr.split_timeout_hi * 8000 
  637.                   + (host->csr.split_timeout_lo >> 19))
  638.                 * HZ / 8000;
  639.         /* Avoid shortening of timeout due to rounding errors: */
  640.         expire++;
  641.         spin_unlock_irqrestore(&host->csr.lock, flags);
  642.         spin_lock_irqsave(&host->pending_pkt_lock, flags);
  643. for (lh = host->pending_packets.next; lh != &host->pending_packets; lh = next) {
  644.                 packet = list_entry(lh, struct hpsb_packet, list);
  645. next = lh->next;
  646.                 if (time_before(packet->sendtime + expire, jiffies)) {
  647.                         list_del(&packet->list);
  648.                         list_add(&packet->list, &expiredlist);
  649.                 }
  650.         }
  651.         if (!list_empty(&host->pending_packets)) {
  652.                 queue_task(&host->timeout_tq, &tq_timer);
  653.         }
  654.         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
  655.         list_for_each(lh, &expiredlist) {
  656.                 packet = list_entry(lh, struct hpsb_packet, list);
  657.                 packet->state = hpsb_complete;
  658.                 packet->ack_code = ACKX_TIMEOUT;
  659.                 up(&packet->state_change);
  660.                 run_task_queue(&packet->complete_tq);
  661.         }
  662. }
  663. static int __init ieee1394_init(void)
  664. {
  665. hpsb_packet_cache = kmem_cache_create("hpsb_packet", sizeof(struct hpsb_packet),
  666.       0, 0, NULL, NULL);
  667. init_hpsb_highlevel();
  668. init_csr();
  669. if (!disable_nodemgr)
  670. init_ieee1394_nodemgr();
  671. else
  672. HPSB_INFO("nodemgr functionality disabled");
  673. return 0;
  674. }
  675. static void __exit ieee1394_cleanup(void)
  676. {
  677. if (!disable_nodemgr)
  678. cleanup_ieee1394_nodemgr();
  679. cleanup_csr();
  680. kmem_cache_destroy(hpsb_packet_cache);
  681. }
  682. module_init(ieee1394_init);
  683. module_exit(ieee1394_cleanup);
  684. /* Exported symbols */
  685. EXPORT_SYMBOL(hpsb_register_lowlevel);
  686. EXPORT_SYMBOL(hpsb_unregister_lowlevel);
  687. EXPORT_SYMBOL(hpsb_get_host);
  688. EXPORT_SYMBOL(hpsb_inc_host_usage);
  689. EXPORT_SYMBOL(hpsb_dec_host_usage);
  690. EXPORT_SYMBOL(hpsb_speedto_str);
  691. EXPORT_SYMBOL(alloc_hpsb_packet);
  692. EXPORT_SYMBOL(free_hpsb_packet);
  693. EXPORT_SYMBOL(hpsb_send_packet);
  694. EXPORT_SYMBOL(hpsb_reset_bus);
  695. EXPORT_SYMBOL(hpsb_bus_reset);
  696. EXPORT_SYMBOL(hpsb_selfid_received);
  697. EXPORT_SYMBOL(hpsb_selfid_complete);
  698. EXPORT_SYMBOL(hpsb_packet_sent);
  699. EXPORT_SYMBOL(hpsb_packet_received);
  700. EXPORT_SYMBOL(get_tlabel);
  701. EXPORT_SYMBOL(free_tlabel);
  702. EXPORT_SYMBOL(fill_async_readquad);
  703. EXPORT_SYMBOL(fill_async_readquad_resp);
  704. EXPORT_SYMBOL(fill_async_readblock);
  705. EXPORT_SYMBOL(fill_async_readblock_resp);
  706. EXPORT_SYMBOL(fill_async_writequad);
  707. EXPORT_SYMBOL(fill_async_writeblock);
  708. EXPORT_SYMBOL(fill_async_write_resp);
  709. EXPORT_SYMBOL(fill_async_lock);
  710. EXPORT_SYMBOL(fill_async_lock_resp);
  711. EXPORT_SYMBOL(fill_iso_packet);
  712. EXPORT_SYMBOL(fill_phy_packet);
  713. EXPORT_SYMBOL(hpsb_make_readqpacket);
  714. EXPORT_SYMBOL(hpsb_make_readbpacket);
  715. EXPORT_SYMBOL(hpsb_make_writeqpacket);
  716. EXPORT_SYMBOL(hpsb_make_writebpacket);
  717. EXPORT_SYMBOL(hpsb_make_lockpacket);
  718. EXPORT_SYMBOL(hpsb_make_phypacket);
  719. EXPORT_SYMBOL(hpsb_packet_success);
  720. EXPORT_SYMBOL(hpsb_make_packet);
  721. EXPORT_SYMBOL(hpsb_read);
  722. EXPORT_SYMBOL(hpsb_write);
  723. EXPORT_SYMBOL(hpsb_lock);
  724. EXPORT_SYMBOL(hpsb_register_highlevel);
  725. EXPORT_SYMBOL(hpsb_unregister_highlevel);
  726. EXPORT_SYMBOL(hpsb_register_addrspace);
  727. EXPORT_SYMBOL(hpsb_listen_channel);
  728. EXPORT_SYMBOL(hpsb_unlisten_channel);
  729. EXPORT_SYMBOL(highlevel_read);
  730. EXPORT_SYMBOL(highlevel_write);
  731. EXPORT_SYMBOL(highlevel_lock);
  732. EXPORT_SYMBOL(highlevel_lock64);
  733. EXPORT_SYMBOL(highlevel_add_host);
  734. EXPORT_SYMBOL(highlevel_remove_host);
  735. EXPORT_SYMBOL(highlevel_host_reset);
  736. EXPORT_SYMBOL(highlevel_add_one_host);
  737. EXPORT_SYMBOL(hpsb_guid_get_entry);
  738. EXPORT_SYMBOL(hpsb_nodeid_get_entry);
  739. EXPORT_SYMBOL(hpsb_get_host_by_ne);
  740. EXPORT_SYMBOL(hpsb_guid_fill_packet);
  741. EXPORT_SYMBOL(hpsb_register_protocol);
  742. EXPORT_SYMBOL(hpsb_unregister_protocol);
  743. EXPORT_SYMBOL(hpsb_release_unit_directory);