firewire: core: integrate software-forced bus resets with bus management
[pandora-kernel.git] / drivers / firewire / core-transaction.c
index 62bf305..5f5a785 100644 (file)
@@ -273,43 +273,49 @@ static int allocate_tlabel(struct fw_card *card)
 }
 
 /**
- * This function provides low-level access to the IEEE1394 transaction
- * logic.  Most C programs would use either fw_read(), fw_write() or
- * fw_lock() instead - those function are convenience wrappers for
- * this function.  The fw_send_request() function is primarily
- * provided as a flexible, one-stop entry point for languages bindings
- * and protocol bindings.
+ * fw_send_request() - submit a request packet for transmission
+ * @card:              interface to send the request at
+ * @t:                 transaction instance to which the request belongs
+ * @tcode:             transaction code
+ * @destination_id:    destination node ID, consisting of bus_ID and phy_ID
+ * @generation:                bus generation in which request and response are valid
+ * @speed:             transmission speed
+ * @offset:            48bit wide offset into destination's address space
+ * @payload:           data payload for the request subaction
+ * @length:            length of the payload, in bytes
+ * @callback:          function to be called when the transaction is completed
+ * @callback_data:     data to be passed to the transaction completion callback
  *
- * FIXME: Document this function further, in particular the possible
- * values for rcode in the callback.  In short, we map ACK_COMPLETE to
- * RCODE_COMPLETE, internal errors set errno and set rcode to
- * RCODE_SEND_ERROR (which is out of range for standard ieee1394
- * rcodes).  All other rcodes are forwarded unchanged.  For all
- * errors, payload is NULL, length is 0.
+ * Submit a request packet into the asynchronous request transmission queue.
+ * Can be called from atomic context.  If you prefer a blocking API, use
+ * fw_run_transaction() in a context that can sleep.
  *
- * Can not expect the callback to be called before the function
- * returns, though this does happen in some cases (ACK_COMPLETE and
- * errors).
+ * In case of lock requests, specify one of the firewire-core specific %TCODE_
+ * constants instead of %TCODE_LOCK_REQUEST in @tcode.
  *
- * The payload is only used for write requests and must not be freed
- * until the callback has been called.
+ * Make sure that the value in @destination_id is not older than the one in
+ * @generation.  Otherwise the request is in danger to be sent to a wrong node.
  *
- * @param card the card from which to send the request
- * @param tcode the tcode for this transaction.  Do not use
- *   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
- * @param speed the speed to use for sending the request
- * @param offset the 48 bit offset on the destination node
- * @param payload the data payload for the request subaction
- * @param length the length in bytes of the data to read
- * @param callback function to be called when the transaction is completed
- * @param callback_data pointer to arbitrary data, which will be
- *   passed to the callback
- *
- * In case of asynchronous stream packets i.e. TCODE_STREAM_DATA, the caller
+ * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
  * needs to synthesize @destination_id with fw_stream_packet_destination_id().
+ * It will contain tag, channel, and sy data instead of a node ID then.
+ *
+ * The payload buffer at @data is going to be DMA-mapped except in case of
+ * quadlet-sized payload or of local (loopback) requests.  Hence make sure that
+ * the buffer complies with the restrictions for DMA-mapped memory.  The
+ * @payload must not be freed before the @callback is called.
+ *
+ * In case of request types without payload, @data is NULL and @length is 0.
+ *
+ * After the transaction is completed successfully or unsuccessfully, the
+ * @callback will be called.  Among its parameters is the response code which
+ * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
+ * the firewire-core specific %RCODE_SEND_ERROR.
+ *
+ * Note some timing corner cases:  fw_send_request() may complete much earlier
+ * than when the request packet actually hits the wire.  On the other hand,
+ * transaction completion and hence execution of @callback may happen even
+ * before fw_send_request() returns.
  */
 void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
                     int destination_id, int generation, int speed,
@@ -375,9 +381,11 @@ static void transaction_callback(struct fw_card *card, int rcode,
 }
 
 /**
- * fw_run_transaction - send request and sleep until transaction is completed
+ * fw_run_transaction() - send request and sleep until transaction is completed
  *
- * Returns the RCODE.
+ * Returns the RCODE.  See fw_send_request() for parameter documentation.
+ * Unlike fw_send_request(), @data points to the payload of the request or/and
+ * to the payload of the response.
  */
 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
                       int generation, int speed, unsigned long long offset,
@@ -418,9 +426,21 @@ void fw_send_phy_config(struct fw_card *card,
                        int node_id, int generation, int gap_count)
 {
        long timeout = DIV_ROUND_UP(HZ, 10);
-       u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
-                  PHY_CONFIG_ROOT_ID(node_id) |
-                  PHY_CONFIG_GAP_COUNT(gap_count);
+       u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG);
+
+       if (node_id != FW_PHY_CONFIG_NO_NODE_ID)
+               data |= PHY_CONFIG_ROOT_ID(node_id);
+
+       if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
+               gap_count = card->driver->read_phy_reg(card, 1);
+               if (gap_count < 0)
+                       return;
+
+               gap_count &= 63;
+               if (gap_count == 63)
+                       return;
+       }
+       data |= PHY_CONFIG_GAP_COUNT(gap_count);
 
        mutex_lock(&phy_config_mutex);
 
@@ -495,9 +515,9 @@ static bool is_in_fcp_region(u64 offset, size_t length)
 }
 
 /**
- * fw_core_add_address_handler - register for incoming requests
- * @handler: callback
- * @region: region in the IEEE 1212 node space address range
+ * fw_core_add_address_handler() - register for incoming requests
+ * @handler:   callback
+ * @region:    region in the IEEE 1212 node space address range
  *
  * region->start, ->end, and handler->length have to be quadlet-aligned.
  *
@@ -552,7 +572,7 @@ int fw_core_add_address_handler(struct fw_address_handler *handler,
 EXPORT_SYMBOL(fw_core_add_address_handler);
 
 /**
- * fw_core_remove_address_handler - unregister an address handler
+ * fw_core_remove_address_handler() - unregister an address handler
  */
 void fw_core_remove_address_handler(struct fw_address_handler *handler)
 {
@@ -780,9 +800,11 @@ static void handle_exclusive_region_request(struct fw_card *card,
        unsigned long flags;
        int tcode, destination, source;
 
-       tcode       = HEADER_GET_TCODE(p->header[0]);
        destination = HEADER_GET_DESTINATION(p->header[0]);
        source      = HEADER_GET_SOURCE(p->header[1]);
+       tcode       = HEADER_GET_TCODE(p->header[0]);
+       if (tcode == TCODE_LOCK_REQUEST)
+               tcode = 0x10 + HEADER_GET_EXTENDED_TCODE(p->header[3]);
 
        spin_lock_irqsave(&address_handler_lock, flags);
        handler = lookup_enclosing_address_handler(&address_handler_list,
@@ -802,7 +824,7 @@ static void handle_exclusive_region_request(struct fw_card *card,
        else
                handler->address_callback(card, request,
                                          tcode, destination, source,
-                                         p->generation, p->speed, offset,
+                                         p->generation, offset,
                                          request->data, request->length,
                                          handler->callback_data);
 }
@@ -840,8 +862,8 @@ static void handle_fcp_region_request(struct fw_card *card,
                if (is_enclosing_handler(handler, offset, request->length))
                        handler->address_callback(card, NULL, tcode,
                                                  destination, source,
-                                                 p->generation, p->speed,
-                                                 offset, request->data,
+                                                 p->generation, offset,
+                                                 request->data,
                                                  request->length,
                                                  handler->callback_data);
        }
@@ -881,13 +903,12 @@ void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
        unsigned long flags;
        u32 *data;
        size_t data_length;
-       int tcode, tlabel, destination, source, rcode;
+       int tcode, tlabel, source, rcode;
 
-       tcode       = HEADER_GET_TCODE(p->header[0]);
-       tlabel      = HEADER_GET_TLABEL(p->header[0]);
-       destination = HEADER_GET_DESTINATION(p->header[0]);
-       source      = HEADER_GET_SOURCE(p->header[1]);
-       rcode       = HEADER_GET_RCODE(p->header[1]);
+       tcode   = HEADER_GET_TCODE(p->header[0]);
+       tlabel  = HEADER_GET_TLABEL(p->header[0]);
+       source  = HEADER_GET_SOURCE(p->header[1]);
+       rcode   = HEADER_GET_RCODE(p->header[1]);
 
        spin_lock_irqsave(&card->lock, flags);
        list_for_each_entry(t, &card->transaction_list, link) {
@@ -952,8 +973,8 @@ static const struct fw_address_region topology_map_region =
 
 static void handle_topology_map(struct fw_card *card, struct fw_request *request,
                int tcode, int destination, int source, int generation,
-               int speed, unsigned long long offset,
-               void *payload, size_t length, void *callback_data)
+               unsigned long long offset, void *payload, size_t length,
+               void *callback_data)
 {
        int start;
 
@@ -982,20 +1003,6 @@ static const struct fw_address_region registers_region =
        { .start = CSR_REGISTER_BASE,
          .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
 
-static u32 read_state_register(struct fw_card *card)
-{
-       u32 value;
-
-       /* Bit 8 (cmstr): */
-       value = card->driver->read_csr_reg(card, CSR_STATE_CLEAR);
-
-       /* Bit 10 (abdicate): */
-       if (card->csr_abdicate)
-               value |= CSR_STATE_BIT_ABDICATE;
-
-       return value;
-}
-
 static void update_split_timeout(struct fw_card *card)
 {
        unsigned int cycles;
@@ -1011,8 +1018,8 @@ static void update_split_timeout(struct fw_card *card)
 
 static void handle_registers(struct fw_card *card, struct fw_request *request,
                int tcode, int destination, int source, int generation,
-               int speed, unsigned long long offset,
-               void *payload, size_t length, void *callback_data)
+               unsigned long long offset, void *payload, size_t length,
+               void *callback_data)
 {
        int reg = offset & ~CSR_REGISTER_BASE;
        __be32 *data = payload;
@@ -1020,50 +1027,37 @@ static void handle_registers(struct fw_card *card, struct fw_request *request,
        unsigned long flags;
 
        switch (reg) {
-       case CSR_STATE_CLEAR:
-               if (tcode == TCODE_READ_QUADLET_REQUEST) {
-                       *data = cpu_to_be32(read_state_register(card));
-               } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
-                       card->driver->write_csr_reg(card, CSR_STATE_CLEAR,
-                                                   be32_to_cpu(*data));
-                       if (*data & cpu_to_be32(CSR_STATE_BIT_ABDICATE))
-                               card->csr_abdicate = false;
-               } else {
-                       rcode = RCODE_TYPE_ERROR;
-               }
-               break;
-
-       case CSR_STATE_SET:
-               if (tcode == TCODE_READ_QUADLET_REQUEST) {
-                       *data = cpu_to_be32(read_state_register(card));
-               } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
-                       card->driver->write_csr_reg(card, CSR_STATE_SET,
-                                                   be32_to_cpu(*data));
-                       if (*data & cpu_to_be32(CSR_STATE_BIT_ABDICATE))
-                               card->csr_abdicate = true;
-               } else {
-                       rcode = RCODE_TYPE_ERROR;
+       case CSR_PRIORITY_BUDGET:
+               if (!card->priority_budget_implemented) {
+                       rcode = RCODE_ADDRESS_ERROR;
+                       break;
                }
-               break;
+               /* else fall through */
 
        case CSR_NODE_IDS:
                /*
                 * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
                 * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
                 */
+               /* fall through */
+
+       case CSR_STATE_CLEAR:
+       case CSR_STATE_SET:
+       case CSR_CYCLE_TIME:
+       case CSR_BUS_TIME:
+       case CSR_BUSY_TIMEOUT:
                if (tcode == TCODE_READ_QUADLET_REQUEST)
-                       *data = cpu_to_be32(card->driver->
-                                       read_csr_reg(card, CSR_NODE_IDS));
+                       *data = cpu_to_be32(card->driver->read_csr(card, reg));
                else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->driver->write_csr_reg(card, CSR_NODE_IDS,
-                                                   be32_to_cpu(*data));
+                       card->driver->write_csr(card, reg, be32_to_cpu(*data));
                else
                        rcode = RCODE_TYPE_ERROR;
                break;
 
        case CSR_RESET_START:
                if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->csr_abdicate = false;
+                       card->driver->write_csr(card, CSR_STATE_CLEAR,
+                                               CSR_STATE_BIT_ABDICATE);
                else
                        rcode = RCODE_TYPE_ERROR;
                break;
@@ -1095,52 +1089,6 @@ static void handle_registers(struct fw_card *card, struct fw_request *request,
                }
                break;
 
-       case CSR_CYCLE_TIME:
-               if (TCODE_IS_READ_REQUEST(tcode) && length == 4)
-                       *data = cpu_to_be32(card->driver->
-                                       read_csr_reg(card, CSR_CYCLE_TIME));
-               else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->driver->write_csr_reg(card, CSR_CYCLE_TIME,
-                                                   be32_to_cpu(*data));
-               else
-                       rcode = RCODE_TYPE_ERROR;
-               break;
-
-       case CSR_BUS_TIME:
-               if (tcode == TCODE_READ_QUADLET_REQUEST)
-                       *data = cpu_to_be32(card->driver->
-                                       read_csr_reg(card, CSR_BUS_TIME));
-               else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->driver->write_csr_reg(card, CSR_BUS_TIME,
-                                                   be32_to_cpu(*data));
-               else
-                       rcode = RCODE_TYPE_ERROR;
-               break;
-
-       case CSR_BUSY_TIMEOUT:
-               if (tcode == TCODE_READ_QUADLET_REQUEST)
-                       *data = cpu_to_be32(card->driver->
-                                       read_csr_reg(card, CSR_BUSY_TIMEOUT));
-               else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->driver->write_csr_reg(card, CSR_BUSY_TIMEOUT,
-                                                   be32_to_cpu(*data));
-               else
-                       rcode = RCODE_TYPE_ERROR;
-               break;
-
-       case CSR_PRIORITY_BUDGET:
-               if (!card->priority_budget_implemented)
-                       rcode = RCODE_ADDRESS_ERROR;
-               else if (tcode == TCODE_READ_QUADLET_REQUEST)
-                       *data = cpu_to_be32(card->driver->
-                               read_csr_reg(card, CSR_PRIORITY_BUDGET));
-               else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
-                       card->driver->write_csr_reg(card, CSR_PRIORITY_BUDGET,
-                                                   be32_to_cpu(*data));
-               else
-                       rcode = RCODE_TYPE_ERROR;
-               break;
-
        case CSR_MAINT_UTILITY:
                if (tcode == TCODE_READ_QUADLET_REQUEST)
                        *data = card->maint_utility_register;