1 /* -*- c-basic-offset: 8 -*-
3 * fw-transaction.c - core IEEE1394 transaction logic
5 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/poll.h>
29 #include <linux/list.h>
30 #include <linux/kthread.h>
31 #include <asm/uaccess.h>
32 #include <asm/semaphore.h>
34 #include "fw-transaction.h"
35 #include "fw-topology.h"
37 #define header_pri(pri) ((pri) << 0)
38 #define header_tcode(tcode) ((tcode) << 4)
39 #define header_retry(retry) ((retry) << 8)
40 #define header_tlabel(tlabel) ((tlabel) << 10)
41 #define header_destination(destination) ((destination) << 16)
42 #define header_source(source) ((source) << 16)
43 #define header_rcode(rcode) ((rcode) << 12)
44 #define header_offset_high(offset_high) ((offset_high) << 0)
45 #define header_data_length(length) ((length) << 16)
46 #define header_extended_tcode(tcode) ((tcode) << 0)
48 #define header_get_tcode(q) (((q) >> 4) & 0x0f)
49 #define header_get_tlabel(q) (((q) >> 10) & 0x3f)
50 #define header_get_rcode(q) (((q) >> 4) & 0x0f)
51 #define header_get_destination(q) (((q) >> 16) & 0xffff)
52 #define header_get_source(q) (((q) >> 16) & 0xffff)
53 #define header_get_offset_high(q) (((q) >> 0) & 0xffff)
54 #define header_get_data_length(q) (((q) >> 16) & 0xffff)
55 #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
57 #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22))
58 #define phy_config_root_id(node_id) (((node_id) << 24) | (1 << 23))
59 #define phy_identifier(id) ((id) << 30)
62 close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode,
63 u32 * payload, size_t length)
67 spin_lock_irqsave(&card->lock, flags);
68 card->tlabel_mask &= ~(1 << t->tlabel);
70 spin_unlock_irqrestore(&card->lock, flags);
72 t->callback(card, rcode, payload, length, t->callback_data);
76 transmit_complete_callback(struct fw_packet *packet,
77 struct fw_card *card, int status)
79 struct fw_transaction *t =
80 container_of(packet, struct fw_transaction, packet);
84 close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
87 t->timestamp = packet->timestamp;
92 close_transaction(t, card, RCODE_BUSY, NULL, 0);
96 close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
99 /* FIXME: In this case, status is a negative errno,
100 * corresponding to an OHCI specific transmit error
101 * code. We should map that to an RCODE instead of
102 * just the generic RCODE_SEND_ERROR. */
103 close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
109 fw_fill_packet(struct fw_packet *packet, int tcode, int tlabel,
110 int node_id, int generation, int speed,
111 unsigned long long offset, void *payload, size_t length)
116 ext_tcode = tcode - 0x10;
117 tcode = TCODE_LOCK_REQUEST;
122 header_retry(RETRY_X) |
123 header_tlabel(tlabel) |
124 header_tcode(tcode) |
125 header_destination(node_id | LOCAL_BUS);
127 header_offset_high(offset >> 32) | header_source(0);
132 case TCODE_WRITE_QUADLET_REQUEST:
133 packet->header[3] = *(u32 *)payload;
134 packet->header_length = 16;
135 packet->payload_length = 0;
138 case TCODE_LOCK_REQUEST:
139 case TCODE_WRITE_BLOCK_REQUEST:
141 header_data_length(length) |
142 header_extended_tcode(ext_tcode);
143 packet->header_length = 16;
144 packet->payload = payload;
145 packet->payload_length = length;
148 case TCODE_READ_QUADLET_REQUEST:
149 packet->header_length = 12;
150 packet->payload_length = 0;
153 case TCODE_READ_BLOCK_REQUEST:
155 header_data_length(length) |
156 header_extended_tcode(ext_tcode);
157 packet->header_length = 16;
158 packet->payload_length = 0;
162 packet->speed = speed;
163 packet->generation = generation;
167 * This function provides low-level access to the IEEE1394 transaction
168 * logic. Most C programs would use either fw_read(), fw_write() or
169 * fw_lock() instead - those function are convenience wrappers for
170 * this function. The fw_send_request() function is primarily
171 * provided as a flexible, one-stop entry point for languages bindings
172 * and protocol bindings.
174 * FIXME: Document this function further, in particular the possible
175 * values for rcode in the callback. In short, we map ACK_COMPLETE to
176 * RCODE_COMPLETE, internal errors set errno and set rcode to
177 * RCODE_SEND_ERROR (which is out of range for standard ieee1394
178 * rcodes). All other rcodes are forwarded unchanged. For all
179 * errors, payload is NULL, length is 0.
181 * Can not expect the callback to be called before the function
182 * returns, though this does happen in some cases (ACK_COMPLETE and
185 * The payload is only used for write requests and must not be freed
186 * until the callback has been called.
188 * @param card the card from which to send the request
189 * @param tcode the tcode for this transaction. Do not use
190 * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
191 * etc. to specify tcode and ext_tcode.
192 * @param node_id the node_id of the destination node
193 * @param generation the generation for which node_id is valid
194 * @param speed the speed to use for sending the request
195 * @param offset the 48 bit offset on the destination node
196 * @param payload the data payload for the request subaction
197 * @param length the length in bytes of the data to read
198 * @param callback function to be called when the transaction is completed
199 * @param callback_data pointer to arbitrary data, which will be
200 * passed to the callback
203 fw_send_request(struct fw_card *card, struct fw_transaction *t,
204 int tcode, int node_id, int generation, int speed,
205 unsigned long long offset,
206 void *payload, size_t length,
207 fw_transaction_callback_t callback, void *callback_data)
212 /* Bump the flush timer up 100ms first of all so we
213 * don't race with a flush timer callback. */
215 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
217 /* Allocate tlabel from the bitmap and put the transaction on
218 * the list while holding the card spinlock. */
220 spin_lock_irqsave(&card->lock, flags);
222 tlabel = card->current_tlabel;
223 if (card->tlabel_mask & (1 << tlabel)) {
224 spin_unlock_irqrestore(&card->lock, flags);
225 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
229 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
230 card->tlabel_mask |= (1 << tlabel);
232 list_add_tail(&t->link, &card->transaction_list);
234 spin_unlock_irqrestore(&card->lock, flags);
236 /* Initialize rest of transaction, fill out packet and send it. */
237 t->node_id = node_id;
239 t->callback = callback;
240 t->callback_data = callback_data;
242 fw_fill_packet(&t->packet, tcode, t->tlabel,
243 node_id, generation, speed, offset, payload, length);
244 t->packet.callback = transmit_complete_callback;
246 card->driver->send_request(card, &t->packet);
248 EXPORT_SYMBOL(fw_send_request);
251 transmit_phy_packet_callback(struct fw_packet *packet,
252 struct fw_card *card, int status)
257 static void send_phy_packet(struct fw_card *card, u32 data, int generation)
259 struct fw_packet *packet;
261 packet = kzalloc(sizeof *packet, GFP_ATOMIC);
265 packet->header[0] = data;
266 packet->header[1] = ~data;
267 packet->header_length = 8;
268 packet->payload_length = 0;
269 packet->speed = SCODE_100;
270 packet->generation = generation;
271 packet->callback = transmit_phy_packet_callback;
273 card->driver->send_request(card, packet);
276 void fw_send_force_root(struct fw_card *card, int node_id, int generation)
280 q = phy_identifier(PHY_PACKET_CONFIG) | phy_config_root_id(node_id);
281 send_phy_packet(card, q, generation);
284 void fw_flush_transactions(struct fw_card *card)
286 struct fw_transaction *t, *next;
287 struct list_head list;
290 INIT_LIST_HEAD(&list);
291 spin_lock_irqsave(&card->lock, flags);
292 list_splice_init(&card->transaction_list, &list);
293 card->tlabel_mask = 0;
294 spin_unlock_irqrestore(&card->lock, flags);
296 list_for_each_entry_safe(t, next, &list, link)
297 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
300 static struct fw_address_handler *
301 lookup_overlapping_address_handler(struct list_head *list,
302 unsigned long long offset, size_t length)
304 struct fw_address_handler *handler;
306 list_for_each_entry(handler, list, link) {
307 if (handler->offset < offset + length &&
308 offset < handler->offset + handler->length)
315 static struct fw_address_handler *
316 lookup_enclosing_address_handler(struct list_head *list,
317 unsigned long long offset, size_t length)
319 struct fw_address_handler *handler;
321 list_for_each_entry(handler, list, link) {
322 if (handler->offset <= offset &&
323 offset + length <= handler->offset + handler->length)
330 static DEFINE_SPINLOCK(address_handler_lock);
331 static LIST_HEAD(address_handler_list);
333 struct fw_address_region fw_low_memory_region =
334 { 0x000000000000ull, 0x000100000000ull };
335 struct fw_address_region fw_high_memory_region =
336 { 0x000100000000ull, 0xffffe0000000ull };
337 struct fw_address_region fw_private_region =
338 { 0xffffe0000000ull, 0xfffff0000000ull };
339 struct fw_address_region fw_csr_region =
340 { 0xfffff0000000ULL, 0xfffff0000800ull };
341 struct fw_address_region fw_unit_space_region =
342 { 0xfffff0000900ull, 0x1000000000000ull };
344 EXPORT_SYMBOL(fw_low_memory_region);
345 EXPORT_SYMBOL(fw_high_memory_region);
346 EXPORT_SYMBOL(fw_private_region);
347 EXPORT_SYMBOL(fw_csr_region);
348 EXPORT_SYMBOL(fw_unit_space_region);
351 * Allocate a range of addresses in the node space of the OHCI
352 * controller. When a request is received that falls within the
353 * specified address range, the specified callback is invoked. The
354 * parameters passed to the callback give the details of the
359 fw_core_add_address_handler(struct fw_address_handler *handler,
360 struct fw_address_region *region)
362 struct fw_address_handler *other;
366 spin_lock_irqsave(&address_handler_lock, flags);
368 handler->offset = region->start;
369 while (handler->offset + handler->length <= region->end) {
371 lookup_overlapping_address_handler(&address_handler_list,
375 handler->offset += other->length;
377 list_add_tail(&handler->link, &address_handler_list);
383 spin_unlock_irqrestore(&address_handler_lock, flags);
388 EXPORT_SYMBOL(fw_core_add_address_handler);
391 * Deallocate a range of addresses allocated with fw_allocate. This
392 * will call the associated callback one last time with a the special
393 * tcode TCODE_DEALLOCATE, to let the client destroy the registered
394 * callback data. For convenience, the callback parameters offset and
395 * length are set to the start and the length respectively for the
396 * deallocated region, payload is set to NULL.
399 void fw_core_remove_address_handler(struct fw_address_handler *handler)
403 spin_lock_irqsave(&address_handler_lock, flags);
404 list_del(&handler->link);
405 spin_unlock_irqrestore(&address_handler_lock, flags);
408 EXPORT_SYMBOL(fw_core_remove_address_handler);
411 struct fw_packet response;
418 free_response_callback(struct fw_packet *packet,
419 struct fw_card *card, int status)
421 struct fw_request *request;
423 request = container_of(packet, struct fw_request, response);
428 fw_fill_response(struct fw_packet *response,
429 u32 *request, u32 *data, size_t length)
431 int tcode, tlabel, extended_tcode, source, destination;
433 tcode = header_get_tcode(request[0]);
434 tlabel = header_get_tlabel(request[0]);
435 source = header_get_destination(request[0]);
436 destination = header_get_source(request[1]);
437 extended_tcode = header_get_extended_tcode(request[3]);
439 response->header[0] =
440 header_retry(RETRY_1) |
441 header_tlabel(tlabel) |
442 header_destination(destination);
443 response->header[1] = header_source(source);
444 response->header[2] = 0;
447 case TCODE_WRITE_QUADLET_REQUEST:
448 case TCODE_WRITE_BLOCK_REQUEST:
449 response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE);
450 response->header_length = 12;
451 response->payload_length = 0;
454 case TCODE_READ_QUADLET_REQUEST:
455 response->header[0] |=
456 header_tcode(TCODE_READ_QUADLET_RESPONSE);
457 response->header[3] = 0;
458 response->header_length = 16;
459 response->payload_length = 0;
462 case TCODE_READ_BLOCK_REQUEST:
463 case TCODE_LOCK_REQUEST:
464 response->header[0] |= header_tcode(tcode + 2);
465 response->header[3] =
466 header_data_length(length) |
467 header_extended_tcode(extended_tcode);
468 response->header_length = 16;
469 response->payload = data;
470 response->payload_length = length;
479 static struct fw_request *
480 allocate_request(u32 *header, int ack,
481 int speed, int timestamp, int generation)
483 struct fw_request *request;
487 request_tcode = header_get_tcode(header[0]);
488 switch (request_tcode) {
489 case TCODE_WRITE_QUADLET_REQUEST:
494 case TCODE_WRITE_BLOCK_REQUEST:
495 case TCODE_LOCK_REQUEST:
497 length = header_get_data_length(header[3]);
500 case TCODE_READ_QUADLET_REQUEST:
505 case TCODE_READ_BLOCK_REQUEST:
507 length = header_get_data_length(header[3]);
515 request = kmalloc(sizeof *request + length, GFP_ATOMIC);
519 request->response.speed = speed;
520 request->response.timestamp = timestamp;
521 request->response.generation = generation;
522 request->response.callback = free_response_callback;
524 request->length = length;
526 memcpy(request->data, data, length);
528 fw_fill_response(&request->response, header, request->data, length);
534 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
538 /* Broadcast packets are reported as ACK_COMPLETE, so this
539 * check is sufficient to ensure we don't send response to
540 * broadcast packets or posted writes. */
541 if (request->ack != ACK_PENDING)
544 request->response.header[1] |= header_rcode(rcode);
545 response_tcode = header_get_tcode(request->response.header[0]);
546 if (rcode != RCODE_COMPLETE)
547 /* Clear the data_length field. */
548 request->response.header[3] &= 0xffff;
549 else if (response_tcode == TCODE_READ_QUADLET_RESPONSE)
550 request->response.header[3] = request->data[0];
552 card->driver->send_response(card, &request->response);
555 EXPORT_SYMBOL(fw_send_response);
558 fw_core_handle_request(struct fw_card *card,
559 int speed, int ack, int timestamp,
560 int generation, u32 length, u32 *header)
562 struct fw_address_handler *handler;
563 struct fw_request *request;
564 unsigned long long offset;
566 int tcode, destination, source, t;
569 /* FIXME: send error response. */
573 if (ack != ACK_PENDING && ack != ACK_COMPLETE)
576 t = (timestamp & 0x1fff) + 4000;
578 t = (timestamp & ~0x1fff) + 0x2000 + t - 8000;
580 t = (timestamp & ~0x1fff) + t;
582 request = allocate_request(header, ack, speed, t, generation);
583 if (request == NULL) {
584 /* FIXME: send statically allocated busy packet. */
589 ((unsigned long long)
590 header_get_offset_high(header[1]) << 32) | header[2];
591 tcode = header_get_tcode(header[0]);
592 destination = header_get_destination(header[0]);
593 source = header_get_source(header[0]);
595 spin_lock_irqsave(&address_handler_lock, flags);
596 handler = lookup_enclosing_address_handler(&address_handler_list,
597 offset, request->length);
598 spin_unlock_irqrestore(&address_handler_lock, flags);
600 /* FIXME: lookup the fw_node corresponding to the sender of
601 * this request and pass that to the address handler instead
602 * of the node ID. We may also want to move the address
603 * allocations to fw_node so we only do this callback if the
604 * upper layers registered it for this node. */
607 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
609 handler->address_callback(card, request,
610 tcode, destination, source,
611 generation, speed, offset,
612 request->data, request->length,
613 handler->callback_data);
616 EXPORT_SYMBOL(fw_core_handle_request);
619 fw_core_handle_response(struct fw_card *card,
620 int speed, int ack, int timestamp,
621 u32 length, u32 *header)
623 struct fw_transaction *t;
627 int tcode, tlabel, destination, source, rcode;
629 tcode = header_get_tcode(header[0]);
630 tlabel = header_get_tlabel(header[0]);
631 destination = header_get_destination(header[0]);
632 source = header_get_source(header[1]);
633 rcode = header_get_rcode(header[1]);
635 spin_lock_irqsave(&card->lock, flags);
636 list_for_each_entry(t, &card->transaction_list, link) {
637 if (t->node_id == source && t->tlabel == tlabel) {
639 card->tlabel_mask &= ~(1 << t->tlabel);
643 spin_unlock_irqrestore(&card->lock, flags);
645 if (&t->link == &card->transaction_list) {
646 fw_notify("Unsolicited response\n");
650 /* FIXME: sanity check packet, is length correct, does tcodes
651 * and addresses match. */
654 case TCODE_READ_QUADLET_RESPONSE:
655 data = (u32 *) &header[3];
659 case TCODE_WRITE_RESPONSE:
664 case TCODE_READ_BLOCK_RESPONSE:
665 case TCODE_LOCK_RESPONSE:
667 data_length = header_get_data_length(header[3]);
671 /* Should never happen, this is just to shut up gcc. */
677 t->callback(card, rcode, data, data_length, t->callback_data);
680 EXPORT_SYMBOL(fw_core_handle_response);
682 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
683 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
684 MODULE_LICENSE("GPL");
686 static u32 vendor_textual_descriptor_data[] = {
687 /* textual descriptor leaf () */
691 0x4c696e75, /* L i n u */
692 0x78204669, /* x F i */
693 0x72657769, /* r e w i */
694 0x72652028, /* r e ( */
695 0x4a554a55, /* J U J U */
699 static struct fw_descriptor vendor_textual_descriptor = {
700 .length = ARRAY_SIZE(vendor_textual_descriptor_data),
702 .data = vendor_textual_descriptor_data
705 struct bus_type fw_bus_type = {
709 static int __init fw_core_init(void)
713 retval = bus_register(&fw_bus_type);
717 /* Add the vendor textual descriptor. */
718 retval = fw_core_add_descriptor(&vendor_textual_descriptor);
724 static void __exit fw_core_cleanup(void)
726 bus_unregister(&fw_bus_type);
729 module_init(fw_core_init);
730 module_exit(fw_core_cleanup);