2 * Core IEEE1394 transaction logic
4 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/bug.h>
22 #include <linux/completion.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/firewire.h>
26 #include <linux/firewire-constants.h>
28 #include <linux/init.h>
29 #include <linux/idr.h>
30 #include <linux/jiffies.h>
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/rculist.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/types.h>
40 #include <linux/workqueue.h>
42 #include <asm/byteorder.h>
46 #define HEADER_PRI(pri) ((pri) << 0)
47 #define HEADER_TCODE(tcode) ((tcode) << 4)
48 #define HEADER_RETRY(retry) ((retry) << 8)
49 #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
50 #define HEADER_DESTINATION(destination) ((destination) << 16)
51 #define HEADER_SOURCE(source) ((source) << 16)
52 #define HEADER_RCODE(rcode) ((rcode) << 12)
53 #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
54 #define HEADER_DATA_LENGTH(length) ((length) << 16)
55 #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
57 #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
58 #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
59 #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
60 #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
61 #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
62 #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
63 #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
64 #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
66 #define HEADER_DESTINATION_IS_BROADCAST(q) \
67 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
69 #define PHY_PACKET_CONFIG 0x0
70 #define PHY_PACKET_LINK_ON 0x1
71 #define PHY_PACKET_SELF_ID 0x2
73 #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
74 #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
75 #define PHY_IDENTIFIER(id) ((id) << 30)
77 /* returns 0 if the split timeout handler is already running */
78 static int try_cancel_split_timeout(struct fw_transaction *t)
80 if (t->is_split_transaction)
81 return del_timer(&t->split_timeout_timer);
86 static int close_transaction(struct fw_transaction *transaction,
87 struct fw_card *card, int rcode)
89 struct fw_transaction *t = NULL, *iter;
92 spin_lock_irqsave(&card->lock, flags);
93 list_for_each_entry(iter, &card->transaction_list, link) {
94 if (iter == transaction) {
95 if (!try_cancel_split_timeout(iter)) {
96 spin_unlock_irqrestore(&card->lock, flags);
99 list_del_init(&iter->link);
100 card->tlabel_mask &= ~(1ULL << iter->tlabel);
105 spin_unlock_irqrestore(&card->lock, flags);
108 t->callback(card, rcode, NULL, 0, t->callback_data);
117 * Only valid for transactions that are potentially pending (ie have
120 int fw_cancel_transaction(struct fw_card *card,
121 struct fw_transaction *transaction)
124 * Cancel the packet transmission if it's still queued. That
125 * will call the packet transmission callback which cancels
129 if (card->driver->cancel_packet(card, &transaction->packet) == 0)
133 * If the request packet has already been sent, we need to see
134 * if the transaction is still pending and remove it in that case.
137 return close_transaction(transaction, card, RCODE_CANCELLED);
139 EXPORT_SYMBOL(fw_cancel_transaction);
141 static void split_transaction_timeout_callback(unsigned long data)
143 struct fw_transaction *t = (struct fw_transaction *)data;
144 struct fw_card *card = t->card;
147 spin_lock_irqsave(&card->lock, flags);
148 if (list_empty(&t->link)) {
149 spin_unlock_irqrestore(&card->lock, flags);
153 card->tlabel_mask &= ~(1ULL << t->tlabel);
154 spin_unlock_irqrestore(&card->lock, flags);
156 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
159 static void start_split_transaction_timeout(struct fw_transaction *t,
160 struct fw_card *card)
164 spin_lock_irqsave(&card->lock, flags);
166 if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) {
167 spin_unlock_irqrestore(&card->lock, flags);
171 t->is_split_transaction = true;
172 mod_timer(&t->split_timeout_timer,
173 jiffies + card->split_timeout_jiffies);
175 spin_unlock_irqrestore(&card->lock, flags);
178 static void transmit_complete_callback(struct fw_packet *packet,
179 struct fw_card *card, int status)
181 struct fw_transaction *t =
182 container_of(packet, struct fw_transaction, packet);
186 close_transaction(t, card, RCODE_COMPLETE);
189 start_split_transaction_timeout(t, card);
194 close_transaction(t, card, RCODE_BUSY);
197 close_transaction(t, card, RCODE_DATA_ERROR);
200 close_transaction(t, card, RCODE_TYPE_ERROR);
204 * In this case the ack is really a juju specific
205 * rcode, so just forward that to the callback.
207 close_transaction(t, card, status);
212 static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
213 int destination_id, int source_id, int generation, int speed,
214 unsigned long long offset, void *payload, size_t length)
218 if (tcode == TCODE_STREAM_DATA) {
220 HEADER_DATA_LENGTH(length) |
222 HEADER_TCODE(TCODE_STREAM_DATA);
223 packet->header_length = 4;
224 packet->payload = payload;
225 packet->payload_length = length;
231 ext_tcode = tcode & ~0x10;
232 tcode = TCODE_LOCK_REQUEST;
237 HEADER_RETRY(RETRY_X) |
238 HEADER_TLABEL(tlabel) |
239 HEADER_TCODE(tcode) |
240 HEADER_DESTINATION(destination_id);
242 HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
247 case TCODE_WRITE_QUADLET_REQUEST:
248 packet->header[3] = *(u32 *)payload;
249 packet->header_length = 16;
250 packet->payload_length = 0;
253 case TCODE_LOCK_REQUEST:
254 case TCODE_WRITE_BLOCK_REQUEST:
256 HEADER_DATA_LENGTH(length) |
257 HEADER_EXTENDED_TCODE(ext_tcode);
258 packet->header_length = 16;
259 packet->payload = payload;
260 packet->payload_length = length;
263 case TCODE_READ_QUADLET_REQUEST:
264 packet->header_length = 12;
265 packet->payload_length = 0;
268 case TCODE_READ_BLOCK_REQUEST:
270 HEADER_DATA_LENGTH(length) |
271 HEADER_EXTENDED_TCODE(ext_tcode);
272 packet->header_length = 16;
273 packet->payload_length = 0;
277 WARN(1, "wrong tcode %d\n", tcode);
280 packet->speed = speed;
281 packet->generation = generation;
283 packet->payload_mapped = false;
286 static int allocate_tlabel(struct fw_card *card)
290 tlabel = card->current_tlabel;
291 while (card->tlabel_mask & (1ULL << tlabel)) {
292 tlabel = (tlabel + 1) & 0x3f;
293 if (tlabel == card->current_tlabel)
297 card->current_tlabel = (tlabel + 1) & 0x3f;
298 card->tlabel_mask |= 1ULL << tlabel;
304 * fw_send_request() - submit a request packet for transmission
305 * @card: interface to send the request at
306 * @t: transaction instance to which the request belongs
307 * @tcode: transaction code
308 * @destination_id: destination node ID, consisting of bus_ID and phy_ID
309 * @generation: bus generation in which request and response are valid
310 * @speed: transmission speed
311 * @offset: 48bit wide offset into destination's address space
312 * @payload: data payload for the request subaction
313 * @length: length of the payload, in bytes
314 * @callback: function to be called when the transaction is completed
315 * @callback_data: data to be passed to the transaction completion callback
317 * Submit a request packet into the asynchronous request transmission queue.
318 * Can be called from atomic context. If you prefer a blocking API, use
319 * fw_run_transaction() in a context that can sleep.
321 * In case of lock requests, specify one of the firewire-core specific %TCODE_
322 * constants instead of %TCODE_LOCK_REQUEST in @tcode.
324 * Make sure that the value in @destination_id is not older than the one in
325 * @generation. Otherwise the request is in danger to be sent to a wrong node.
327 * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
328 * needs to synthesize @destination_id with fw_stream_packet_destination_id().
329 * It will contain tag, channel, and sy data instead of a node ID then.
331 * The payload buffer at @data is going to be DMA-mapped except in case of
332 * @length <= 8 or of local (loopback) requests. Hence make sure that the
333 * buffer complies with the restrictions of the streaming DMA mapping API.
334 * @payload must not be freed before the @callback is called.
336 * In case of request types without payload, @data is NULL and @length is 0.
338 * After the transaction is completed successfully or unsuccessfully, the
339 * @callback will be called. Among its parameters is the response code which
340 * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
341 * the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core
342 * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION,
343 * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request
344 * generation, or missing ACK respectively.
346 * Note some timing corner cases: fw_send_request() may complete much earlier
347 * than when the request packet actually hits the wire. On the other hand,
348 * transaction completion and hence execution of @callback may happen even
349 * before fw_send_request() returns.
351 void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
352 int destination_id, int generation, int speed,
353 unsigned long long offset, void *payload, size_t length,
354 fw_transaction_callback_t callback, void *callback_data)
360 * Allocate tlabel from the bitmap and put the transaction on
361 * the list while holding the card spinlock.
364 spin_lock_irqsave(&card->lock, flags);
366 tlabel = allocate_tlabel(card);
368 spin_unlock_irqrestore(&card->lock, flags);
369 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
373 t->node_id = destination_id;
376 t->is_split_transaction = false;
377 setup_timer(&t->split_timeout_timer,
378 split_transaction_timeout_callback, (unsigned long)t);
379 t->callback = callback;
380 t->callback_data = callback_data;
382 fw_fill_request(&t->packet, tcode, t->tlabel,
383 destination_id, card->node_id, generation,
384 speed, offset, payload, length);
385 t->packet.callback = transmit_complete_callback;
387 list_add_tail(&t->link, &card->transaction_list);
389 spin_unlock_irqrestore(&card->lock, flags);
391 card->driver->send_request(card, &t->packet);
393 EXPORT_SYMBOL(fw_send_request);
395 struct transaction_callback_data {
396 struct completion done;
401 static void transaction_callback(struct fw_card *card, int rcode,
402 void *payload, size_t length, void *data)
404 struct transaction_callback_data *d = data;
406 if (rcode == RCODE_COMPLETE)
407 memcpy(d->payload, payload, length);
413 * fw_run_transaction() - send request and sleep until transaction is completed
415 * Returns the RCODE. See fw_send_request() for parameter documentation.
416 * Unlike fw_send_request(), @data points to the payload of the request or/and
417 * to the payload of the response. DMA mapping restrictions apply to outbound
418 * request payloads of >= 8 bytes but not to inbound response payloads.
420 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
421 int generation, int speed, unsigned long long offset,
422 void *payload, size_t length)
424 struct transaction_callback_data d;
425 struct fw_transaction t;
427 init_timer_on_stack(&t.split_timeout_timer);
428 init_completion(&d.done);
430 fw_send_request(card, &t, tcode, destination_id, generation, speed,
431 offset, payload, length, transaction_callback, &d);
432 wait_for_completion(&d.done);
433 destroy_timer_on_stack(&t.split_timeout_timer);
437 EXPORT_SYMBOL(fw_run_transaction);
439 static DEFINE_MUTEX(phy_config_mutex);
440 static DECLARE_COMPLETION(phy_config_done);
442 static void transmit_phy_packet_callback(struct fw_packet *packet,
443 struct fw_card *card, int status)
445 complete(&phy_config_done);
448 static struct fw_packet phy_config_packet = {
450 .header[0] = TCODE_LINK_INTERNAL << 4,
453 .callback = transmit_phy_packet_callback,
456 void fw_send_phy_config(struct fw_card *card,
457 int node_id, int generation, int gap_count)
459 long timeout = DIV_ROUND_UP(HZ, 10);
460 u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG);
462 if (node_id != FW_PHY_CONFIG_NO_NODE_ID)
463 data |= PHY_CONFIG_ROOT_ID(node_id);
465 if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
466 gap_count = card->driver->read_phy_reg(card, 1);
474 data |= PHY_CONFIG_GAP_COUNT(gap_count);
476 mutex_lock(&phy_config_mutex);
478 phy_config_packet.header[1] = data;
479 phy_config_packet.header[2] = ~data;
480 phy_config_packet.generation = generation;
481 reinit_completion(&phy_config_done);
483 card->driver->send_request(card, &phy_config_packet);
484 wait_for_completion_timeout(&phy_config_done, timeout);
486 mutex_unlock(&phy_config_mutex);
489 static struct fw_address_handler *lookup_overlapping_address_handler(
490 struct list_head *list, unsigned long long offset, size_t length)
492 struct fw_address_handler *handler;
494 list_for_each_entry_rcu(handler, list, link) {
495 if (handler->offset < offset + length &&
496 offset < handler->offset + handler->length)
503 static bool is_enclosing_handler(struct fw_address_handler *handler,
504 unsigned long long offset, size_t length)
506 return handler->offset <= offset &&
507 offset + length <= handler->offset + handler->length;
510 static struct fw_address_handler *lookup_enclosing_address_handler(
511 struct list_head *list, unsigned long long offset, size_t length)
513 struct fw_address_handler *handler;
515 list_for_each_entry_rcu(handler, list, link) {
516 if (is_enclosing_handler(handler, offset, length))
523 static DEFINE_SPINLOCK(address_handler_list_lock);
524 static LIST_HEAD(address_handler_list);
526 const struct fw_address_region fw_high_memory_region =
527 { .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, };
528 EXPORT_SYMBOL(fw_high_memory_region);
530 static const struct fw_address_region low_memory_region =
531 { .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, };
534 const struct fw_address_region fw_private_region =
535 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
536 const struct fw_address_region fw_csr_region =
537 { .start = CSR_REGISTER_BASE,
538 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
539 const struct fw_address_region fw_unit_space_region =
540 { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
543 static bool is_in_fcp_region(u64 offset, size_t length)
545 return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
546 offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END);
550 * fw_core_add_address_handler() - register for incoming requests
552 * @region: region in the IEEE 1212 node space address range
554 * region->start, ->end, and handler->length have to be quadlet-aligned.
556 * When a request is received that falls within the specified address range,
557 * the specified callback is invoked. The parameters passed to the callback
558 * give the details of the particular request.
560 * To be called in process context.
561 * Return value: 0 on success, non-zero otherwise.
563 * The start offset of the handler's address region is determined by
564 * fw_core_add_address_handler() and is returned in handler->offset.
566 * Address allocations are exclusive, except for the FCP registers.
568 int fw_core_add_address_handler(struct fw_address_handler *handler,
569 const struct fw_address_region *region)
571 struct fw_address_handler *other;
574 if (region->start & 0xffff000000000003ULL ||
575 region->start >= region->end ||
576 region->end > 0x0001000000000000ULL ||
577 handler->length & 3 ||
578 handler->length == 0)
581 spin_lock(&address_handler_list_lock);
583 handler->offset = region->start;
584 while (handler->offset + handler->length <= region->end) {
585 if (is_in_fcp_region(handler->offset, handler->length))
588 other = lookup_overlapping_address_handler
589 (&address_handler_list,
590 handler->offset, handler->length);
592 handler->offset += other->length;
594 list_add_tail_rcu(&handler->link, &address_handler_list);
600 spin_unlock(&address_handler_list_lock);
604 EXPORT_SYMBOL(fw_core_add_address_handler);
607 * fw_core_remove_address_handler() - unregister an address handler
609 * To be called in process context.
611 * When fw_core_remove_address_handler() returns, @handler->callback() is
612 * guaranteed to not run on any CPU anymore.
614 void fw_core_remove_address_handler(struct fw_address_handler *handler)
616 spin_lock(&address_handler_list_lock);
617 list_del_rcu(&handler->link);
618 spin_unlock(&address_handler_list_lock);
621 EXPORT_SYMBOL(fw_core_remove_address_handler);
624 struct fw_packet response;
625 u32 request_header[4];
631 static void free_response_callback(struct fw_packet *packet,
632 struct fw_card *card, int status)
634 struct fw_request *request;
636 request = container_of(packet, struct fw_request, response);
640 int fw_get_response_length(struct fw_request *r)
642 int tcode, ext_tcode, data_length;
644 tcode = HEADER_GET_TCODE(r->request_header[0]);
647 case TCODE_WRITE_QUADLET_REQUEST:
648 case TCODE_WRITE_BLOCK_REQUEST:
651 case TCODE_READ_QUADLET_REQUEST:
654 case TCODE_READ_BLOCK_REQUEST:
655 data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
658 case TCODE_LOCK_REQUEST:
659 ext_tcode = HEADER_GET_EXTENDED_TCODE(r->request_header[3]);
660 data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
662 case EXTCODE_FETCH_ADD:
663 case EXTCODE_LITTLE_ADD:
666 return data_length / 2;
670 WARN(1, "wrong tcode %d\n", tcode);
675 void fw_fill_response(struct fw_packet *response, u32 *request_header,
676 int rcode, void *payload, size_t length)
678 int tcode, tlabel, extended_tcode, source, destination;
680 tcode = HEADER_GET_TCODE(request_header[0]);
681 tlabel = HEADER_GET_TLABEL(request_header[0]);
682 source = HEADER_GET_DESTINATION(request_header[0]);
683 destination = HEADER_GET_SOURCE(request_header[1]);
684 extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
686 response->header[0] =
687 HEADER_RETRY(RETRY_1) |
688 HEADER_TLABEL(tlabel) |
689 HEADER_DESTINATION(destination);
690 response->header[1] =
691 HEADER_SOURCE(source) |
693 response->header[2] = 0;
696 case TCODE_WRITE_QUADLET_REQUEST:
697 case TCODE_WRITE_BLOCK_REQUEST:
698 response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
699 response->header_length = 12;
700 response->payload_length = 0;
703 case TCODE_READ_QUADLET_REQUEST:
704 response->header[0] |=
705 HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
707 response->header[3] = *(u32 *)payload;
709 response->header[3] = 0;
710 response->header_length = 16;
711 response->payload_length = 0;
714 case TCODE_READ_BLOCK_REQUEST:
715 case TCODE_LOCK_REQUEST:
716 response->header[0] |= HEADER_TCODE(tcode + 2);
717 response->header[3] =
718 HEADER_DATA_LENGTH(length) |
719 HEADER_EXTENDED_TCODE(extended_tcode);
720 response->header_length = 16;
721 response->payload = payload;
722 response->payload_length = length;
726 WARN(1, "wrong tcode %d\n", tcode);
729 response->payload_mapped = false;
731 EXPORT_SYMBOL(fw_fill_response);
733 static u32 compute_split_timeout_timestamp(struct fw_card *card,
734 u32 request_timestamp)
739 cycles = card->split_timeout_cycles;
740 cycles += request_timestamp & 0x1fff;
742 timestamp = request_timestamp & ~0x1fff;
743 timestamp += (cycles / 8000) << 13;
744 timestamp |= cycles % 8000;
749 static struct fw_request *allocate_request(struct fw_card *card,
752 struct fw_request *request;
756 request_tcode = HEADER_GET_TCODE(p->header[0]);
757 switch (request_tcode) {
758 case TCODE_WRITE_QUADLET_REQUEST:
759 data = &p->header[3];
763 case TCODE_WRITE_BLOCK_REQUEST:
764 case TCODE_LOCK_REQUEST:
766 length = HEADER_GET_DATA_LENGTH(p->header[3]);
769 case TCODE_READ_QUADLET_REQUEST:
774 case TCODE_READ_BLOCK_REQUEST:
776 length = HEADER_GET_DATA_LENGTH(p->header[3]);
780 fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
781 p->header[0], p->header[1], p->header[2]);
785 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
789 request->response.speed = p->speed;
790 request->response.timestamp =
791 compute_split_timeout_timestamp(card, p->timestamp);
792 request->response.generation = p->generation;
793 request->response.ack = 0;
794 request->response.callback = free_response_callback;
795 request->ack = p->ack;
796 request->length = length;
798 memcpy(request->data, data, length);
800 memcpy(request->request_header, p->header, sizeof(p->header));
805 void fw_send_response(struct fw_card *card,
806 struct fw_request *request, int rcode)
808 if (WARN_ONCE(!request, "invalid for FCP address handlers"))
811 /* unified transaction or broadcast transaction: don't respond */
812 if (request->ack != ACK_PENDING ||
813 HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
818 if (rcode == RCODE_COMPLETE)
819 fw_fill_response(&request->response, request->request_header,
820 rcode, request->data,
821 fw_get_response_length(request));
823 fw_fill_response(&request->response, request->request_header,
826 card->driver->send_response(card, &request->response);
828 EXPORT_SYMBOL(fw_send_response);
831 * fw_get_request_speed() - returns speed at which the @request was received
833 int fw_get_request_speed(struct fw_request *request)
835 return request->response.speed;
837 EXPORT_SYMBOL(fw_get_request_speed);
839 static void handle_exclusive_region_request(struct fw_card *card,
841 struct fw_request *request,
842 unsigned long long offset)
844 struct fw_address_handler *handler;
845 int tcode, destination, source;
847 destination = HEADER_GET_DESTINATION(p->header[0]);
848 source = HEADER_GET_SOURCE(p->header[1]);
849 tcode = HEADER_GET_TCODE(p->header[0]);
850 if (tcode == TCODE_LOCK_REQUEST)
851 tcode = 0x10 + HEADER_GET_EXTENDED_TCODE(p->header[3]);
854 handler = lookup_enclosing_address_handler(&address_handler_list,
855 offset, request->length);
857 handler->address_callback(card, request,
858 tcode, destination, source,
859 p->generation, offset,
860 request->data, request->length,
861 handler->callback_data);
865 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
868 static void handle_fcp_region_request(struct fw_card *card,
870 struct fw_request *request,
871 unsigned long long offset)
873 struct fw_address_handler *handler;
874 int tcode, destination, source;
876 if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
877 offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
878 request->length > 0x200) {
879 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
884 tcode = HEADER_GET_TCODE(p->header[0]);
885 destination = HEADER_GET_DESTINATION(p->header[0]);
886 source = HEADER_GET_SOURCE(p->header[1]);
888 if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
889 tcode != TCODE_WRITE_BLOCK_REQUEST) {
890 fw_send_response(card, request, RCODE_TYPE_ERROR);
896 list_for_each_entry_rcu(handler, &address_handler_list, link) {
897 if (is_enclosing_handler(handler, offset, request->length))
898 handler->address_callback(card, NULL, tcode,
900 p->generation, offset,
903 handler->callback_data);
907 fw_send_response(card, request, RCODE_COMPLETE);
910 void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
912 struct fw_request *request;
913 unsigned long long offset;
915 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
918 if (TCODE_IS_LINK_INTERNAL(HEADER_GET_TCODE(p->header[0]))) {
919 fw_cdev_handle_phy_packet(card, p);
923 request = allocate_request(card, p);
924 if (request == NULL) {
925 /* FIXME: send statically allocated busy packet. */
929 offset = ((u64)HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) |
932 if (!is_in_fcp_region(offset, request->length))
933 handle_exclusive_region_request(card, p, request, offset);
935 handle_fcp_region_request(card, p, request, offset);
938 EXPORT_SYMBOL(fw_core_handle_request);
940 void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
942 struct fw_transaction *t = NULL, *iter;
946 int tcode, tlabel, source, rcode;
948 tcode = HEADER_GET_TCODE(p->header[0]);
949 tlabel = HEADER_GET_TLABEL(p->header[0]);
950 source = HEADER_GET_SOURCE(p->header[1]);
951 rcode = HEADER_GET_RCODE(p->header[1]);
953 spin_lock_irqsave(&card->lock, flags);
954 list_for_each_entry(iter, &card->transaction_list, link) {
955 if (iter->node_id == source && iter->tlabel == tlabel) {
956 if (!try_cancel_split_timeout(iter)) {
957 spin_unlock_irqrestore(&card->lock, flags);
960 list_del_init(&iter->link);
961 card->tlabel_mask &= ~(1ULL << iter->tlabel);
966 spin_unlock_irqrestore(&card->lock, flags);
970 fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
976 * FIXME: sanity check packet, is length correct, does tcodes
977 * and addresses match.
981 case TCODE_READ_QUADLET_RESPONSE:
982 data = (u32 *) &p->header[3];
986 case TCODE_WRITE_RESPONSE:
991 case TCODE_READ_BLOCK_RESPONSE:
992 case TCODE_LOCK_RESPONSE:
994 data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
998 /* Should never happen, this is just to shut up gcc. */
1005 * The response handler may be executed while the request handler
1006 * is still pending. Cancel the request handler.
1008 card->driver->cancel_packet(card, &t->packet);
1010 t->callback(card, rcode, data, data_length, t->callback_data);
1012 EXPORT_SYMBOL(fw_core_handle_response);
1015 * fw_rcode_string - convert a firewire result code to an error description
1016 * @rcode: the result code
1018 const char *fw_rcode_string(int rcode)
1020 static const char *const names[] = {
1021 [RCODE_COMPLETE] = "no error",
1022 [RCODE_CONFLICT_ERROR] = "conflict error",
1023 [RCODE_DATA_ERROR] = "data error",
1024 [RCODE_TYPE_ERROR] = "type error",
1025 [RCODE_ADDRESS_ERROR] = "address error",
1026 [RCODE_SEND_ERROR] = "send error",
1027 [RCODE_CANCELLED] = "timeout",
1028 [RCODE_BUSY] = "busy",
1029 [RCODE_GENERATION] = "bus reset",
1030 [RCODE_NO_ACK] = "no ack",
1033 if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode])
1034 return names[rcode];
1038 EXPORT_SYMBOL(fw_rcode_string);
1040 static const struct fw_address_region topology_map_region =
1041 { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
1042 .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
1044 static void handle_topology_map(struct fw_card *card, struct fw_request *request,
1045 int tcode, int destination, int source, int generation,
1046 unsigned long long offset, void *payload, size_t length,
1047 void *callback_data)
1051 if (!TCODE_IS_READ_REQUEST(tcode)) {
1052 fw_send_response(card, request, RCODE_TYPE_ERROR);
1056 if ((offset & 3) > 0 || (length & 3) > 0) {
1057 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
1061 start = (offset - topology_map_region.start) / 4;
1062 memcpy(payload, &card->topology_map[start], length);
1064 fw_send_response(card, request, RCODE_COMPLETE);
1067 static struct fw_address_handler topology_map = {
1069 .address_callback = handle_topology_map,
1072 static const struct fw_address_region registers_region =
1073 { .start = CSR_REGISTER_BASE,
1074 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
1076 static void update_split_timeout(struct fw_card *card)
1078 unsigned int cycles;
1080 cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
1082 /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
1083 cycles = clamp(cycles, 800u, 3u * 8000u);
1085 card->split_timeout_cycles = cycles;
1086 card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
1089 static void handle_registers(struct fw_card *card, struct fw_request *request,
1090 int tcode, int destination, int source, int generation,
1091 unsigned long long offset, void *payload, size_t length,
1092 void *callback_data)
1094 int reg = offset & ~CSR_REGISTER_BASE;
1095 __be32 *data = payload;
1096 int rcode = RCODE_COMPLETE;
1097 unsigned long flags;
1100 case CSR_PRIORITY_BUDGET:
1101 if (!card->priority_budget_implemented) {
1102 rcode = RCODE_ADDRESS_ERROR;
1105 /* else fall through */
1109 * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
1110 * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
1114 case CSR_STATE_CLEAR:
1116 case CSR_CYCLE_TIME:
1118 case CSR_BUSY_TIMEOUT:
1119 if (tcode == TCODE_READ_QUADLET_REQUEST)
1120 *data = cpu_to_be32(card->driver->read_csr(card, reg));
1121 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1122 card->driver->write_csr(card, reg, be32_to_cpu(*data));
1124 rcode = RCODE_TYPE_ERROR;
1127 case CSR_RESET_START:
1128 if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1129 card->driver->write_csr(card, CSR_STATE_CLEAR,
1130 CSR_STATE_BIT_ABDICATE);
1132 rcode = RCODE_TYPE_ERROR;
1135 case CSR_SPLIT_TIMEOUT_HI:
1136 if (tcode == TCODE_READ_QUADLET_REQUEST) {
1137 *data = cpu_to_be32(card->split_timeout_hi);
1138 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1139 spin_lock_irqsave(&card->lock, flags);
1140 card->split_timeout_hi = be32_to_cpu(*data) & 7;
1141 update_split_timeout(card);
1142 spin_unlock_irqrestore(&card->lock, flags);
1144 rcode = RCODE_TYPE_ERROR;
1148 case CSR_SPLIT_TIMEOUT_LO:
1149 if (tcode == TCODE_READ_QUADLET_REQUEST) {
1150 *data = cpu_to_be32(card->split_timeout_lo);
1151 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1152 spin_lock_irqsave(&card->lock, flags);
1153 card->split_timeout_lo =
1154 be32_to_cpu(*data) & 0xfff80000;
1155 update_split_timeout(card);
1156 spin_unlock_irqrestore(&card->lock, flags);
1158 rcode = RCODE_TYPE_ERROR;
1162 case CSR_MAINT_UTILITY:
1163 if (tcode == TCODE_READ_QUADLET_REQUEST)
1164 *data = card->maint_utility_register;
1165 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1166 card->maint_utility_register = *data;
1168 rcode = RCODE_TYPE_ERROR;
1171 case CSR_BROADCAST_CHANNEL:
1172 if (tcode == TCODE_READ_QUADLET_REQUEST)
1173 *data = cpu_to_be32(card->broadcast_channel);
1174 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1175 card->broadcast_channel =
1176 (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
1177 BROADCAST_CHANNEL_INITIAL;
1179 rcode = RCODE_TYPE_ERROR;
1182 case CSR_BUS_MANAGER_ID:
1183 case CSR_BANDWIDTH_AVAILABLE:
1184 case CSR_CHANNELS_AVAILABLE_HI:
1185 case CSR_CHANNELS_AVAILABLE_LO:
1187 * FIXME: these are handled by the OHCI hardware and
1188 * the stack never sees these request. If we add
1189 * support for a new type of controller that doesn't
1190 * handle this in hardware we need to deal with these
1197 rcode = RCODE_ADDRESS_ERROR;
1201 fw_send_response(card, request, rcode);
1204 static struct fw_address_handler registers = {
1206 .address_callback = handle_registers,
1209 static void handle_low_memory(struct fw_card *card, struct fw_request *request,
1210 int tcode, int destination, int source, int generation,
1211 unsigned long long offset, void *payload, size_t length,
1212 void *callback_data)
1215 * This catches requests not handled by the physical DMA unit,
1216 * i.e., wrong transaction types or unauthorized source nodes.
1218 fw_send_response(card, request, RCODE_TYPE_ERROR);
1221 static struct fw_address_handler low_memory = {
1222 .length = FW_MAX_PHYSICAL_RANGE,
1223 .address_callback = handle_low_memory,
1226 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1227 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
1228 MODULE_LICENSE("GPL");
1230 static const u32 vendor_textual_descriptor[] = {
1231 /* textual descriptor leaf () */
1235 0x4c696e75, /* L i n u */
1236 0x78204669, /* x F i */
1237 0x72657769, /* r e w i */
1238 0x72650000, /* r e */
1241 static const u32 model_textual_descriptor[] = {
1242 /* model descriptor leaf () */
1246 0x4a756a75, /* J u j u */
1249 static struct fw_descriptor vendor_id_descriptor = {
1250 .length = ARRAY_SIZE(vendor_textual_descriptor),
1251 .immediate = 0x03001f11,
1253 .data = vendor_textual_descriptor,
1256 static struct fw_descriptor model_id_descriptor = {
1257 .length = ARRAY_SIZE(model_textual_descriptor),
1258 .immediate = 0x17023901,
1260 .data = model_textual_descriptor,
1263 static int __init fw_core_init(void)
1267 fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM, 0);
1271 ret = bus_register(&fw_bus_type);
1273 destroy_workqueue(fw_workqueue);
1277 fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
1278 if (fw_cdev_major < 0) {
1279 bus_unregister(&fw_bus_type);
1280 destroy_workqueue(fw_workqueue);
1281 return fw_cdev_major;
1284 fw_core_add_address_handler(&topology_map, &topology_map_region);
1285 fw_core_add_address_handler(®isters, ®isters_region);
1286 fw_core_add_address_handler(&low_memory, &low_memory_region);
1287 fw_core_add_descriptor(&vendor_id_descriptor);
1288 fw_core_add_descriptor(&model_id_descriptor);
1293 static void __exit fw_core_cleanup(void)
1295 unregister_chrdev(fw_cdev_major, "firewire");
1296 bus_unregister(&fw_bus_type);
1297 destroy_workqueue(fw_workqueue);
1298 idr_destroy(&fw_device_idr);
1301 module_init(fw_core_init);
1302 module_exit(fw_core_cleanup);