firewire: wait until PHY configuration packet was transmitted (fix bus reset loop)
[linux-2.6-block.git] / drivers / firewire / fw-transaction.c
index 3e1cb12e43cd13f3b8777af28f78c94f7bfb03d0..051be78deabab472fcdf86488c9330e281b742b8 100644 (file)
@@ -18,6 +18,7 @@
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <linux/completion.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
@@ -153,7 +154,7 @@ fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
        int ext_tcode;
 
        if (tcode > 0x10) {
-               ext_tcode = tcode 0x10;
+               ext_tcode = tcode & ~0x10;
                tcode = TCODE_LOCK_REQUEST;
        } else
                ext_tcode = 0;
@@ -228,7 +229,7 @@ fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  *
  * @param card the card from which to send the request
  * @param tcode the tcode for this transaction.  Do not use
- *   TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
+ *   TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
  *   etc. to specify tcode and ext_tcode.
  * @param node_id the destination node ID (bus ID and PHY ID concatenated)
  * @param generation the generation for which node_id is valid
@@ -294,42 +295,40 @@ fw_send_request(struct fw_card *card, struct fw_transaction *t,
 }
 EXPORT_SYMBOL(fw_send_request);
 
+struct fw_phy_packet {
+       struct fw_packet packet;
+       struct completion done;
+};
+
 static void
 transmit_phy_packet_callback(struct fw_packet *packet,
                             struct fw_card *card, int status)
 {
-       kfree(packet);
-}
-
-static void send_phy_packet(struct fw_card *card, u32 data, int generation)
-{
-       struct fw_packet *packet;
-
-       packet = kzalloc(sizeof(*packet), GFP_ATOMIC);
-       if (packet == NULL)
-               return;
-
-       packet->header[0] = data;
-       packet->header[1] = ~data;
-       packet->header_length = 8;
-       packet->payload_length = 0;
-       packet->speed = SCODE_100;
-       packet->generation = generation;
-       packet->callback = transmit_phy_packet_callback;
+       struct fw_phy_packet *p =
+                       container_of(packet, struct fw_phy_packet, packet);
 
-       card->driver->send_request(card, packet);
+       complete(&p->done);
 }
 
 void fw_send_phy_config(struct fw_card *card,
                        int node_id, int generation, int gap_count)
 {
-       u32 q;
-
-       q = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
-               PHY_CONFIG_ROOT_ID(node_id) |
-               PHY_CONFIG_GAP_COUNT(gap_count);
-
-       send_phy_packet(card, q, generation);
+       struct fw_phy_packet p;
+       u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
+                  PHY_CONFIG_ROOT_ID(node_id) |
+                  PHY_CONFIG_GAP_COUNT(gap_count);
+
+       p.packet.header[0] = data;
+       p.packet.header[1] = ~data;
+       p.packet.header_length = 8;
+       p.packet.payload_length = 0;
+       p.packet.speed = SCODE_100;
+       p.packet.generation = generation;
+       p.packet.callback = transmit_phy_packet_callback;
+       init_completion(&p.done);
+
+       card->driver->send_request(card, &p.packet);
+       wait_for_completion(&p.done);
 }
 
 void fw_flush_transactions(struct fw_card *card)
@@ -396,7 +395,8 @@ const struct fw_address_region fw_high_memory_region =
 const struct fw_address_region fw_private_region =
        { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL,  };
 const struct fw_address_region fw_csr_region =
-       { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL,  };
+       { .start = CSR_REGISTER_BASE,
+         .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END,  };
 const struct fw_address_region fw_unit_space_region =
        { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
 EXPORT_SYMBOL(fw_low_memory_region);
@@ -410,7 +410,12 @@ EXPORT_SYMBOL(fw_unit_space_region);
  * controller.  When a request is received that falls within the
  * specified address range, the specified callback is invoked.  The
  * parameters passed to the callback give the details of the
- * particular request
+ * particular request.
+ *
+ * Return value:  0 on success, non-zero otherwise.
+ * The start offset of the handler's address region is determined by
+ * fw_core_add_address_handler() and is returned in handler->offset.
+ * The offset is quadlet-aligned.
  */
 int
 fw_core_add_address_handler(struct fw_address_handler *handler,
@@ -422,14 +427,15 @@ fw_core_add_address_handler(struct fw_address_handler *handler,
 
        spin_lock_irqsave(&address_handler_lock, flags);
 
-       handler->offset = region->start;
+       handler->offset = roundup(region->start, 4);
        while (handler->offset + handler->length <= region->end) {
                other =
                    lookup_overlapping_address_handler(&address_handler_list,
                                                       handler->offset,
                                                       handler->length);
                if (other != NULL) {
-                       handler->offset += other->length;
+                       handler->offset =
+                           roundup(other->offset + other->length, 4);
                } else {
                        list_add_tail(&handler->link, &address_handler_list);
                        ret = 0;
@@ -644,7 +650,7 @@ fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
                 HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) | p->header[2];
        tcode       = HEADER_GET_TCODE(p->header[0]);
        destination = HEADER_GET_DESTINATION(p->header[0]);
-       source      = HEADER_GET_SOURCE(p->header[0]);
+       source      = HEADER_GET_SOURCE(p->header[1]);
 
        spin_lock_irqsave(&address_handler_lock, flags);
        handler = lookup_enclosing_address_handler(&address_handler_list,
@@ -730,12 +736,19 @@ fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
                break;
        }
 
+       /*
+        * The response handler may be executed while the request handler
+        * is still pending.  Cancel the request handler.
+        */
+       card->driver->cancel_packet(card, &t->packet);
+
        t->callback(card, rcode, data, data_length, t->callback_data);
 }
 EXPORT_SYMBOL(fw_core_handle_response);
 
 static const struct fw_address_region topology_map_region =
-       { .start = 0xfffff0001000ull, .end = 0xfffff0001400ull, };
+       { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
+         .end   = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
 
 static void
 handle_topology_map(struct fw_card *card, struct fw_request *request,
@@ -745,7 +758,7 @@ handle_topology_map(struct fw_card *card, struct fw_request *request,
                    void *payload, size_t length, void *callback_data)
 {
        int i, start, end;
-       u32 *map;
+       __be32 *map;
 
        if (!TCODE_IS_READ_REQUEST(tcode)) {
                fw_send_response(card, request, RCODE_TYPE_ERROR);
@@ -773,7 +786,8 @@ static struct fw_address_handler topology_map = {
 };
 
 static const struct fw_address_region registers_region =
-       { .start = 0xfffff0000000ull, .end = 0xfffff0000400ull, };
+       { .start = CSR_REGISTER_BASE,
+         .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
 
 static void
 handle_registers(struct fw_card *card, struct fw_request *request,
@@ -782,7 +796,7 @@ handle_registers(struct fw_card *card, struct fw_request *request,
                 unsigned long long offset,
                 void *payload, size_t length, void *callback_data)
 {
-       int reg = offset CSR_REGISTER_BASE;
+       int reg = offset & ~CSR_REGISTER_BASE;
        unsigned long long bus_time;
        __be32 *data = payload;