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"
36 #include "fw-device.h"
38 #define header_pri(pri) ((pri) << 0)
39 #define header_tcode(tcode) ((tcode) << 4)
40 #define header_retry(retry) ((retry) << 8)
41 #define header_tlabel(tlabel) ((tlabel) << 10)
42 #define header_destination(destination) ((destination) << 16)
43 #define header_source(source) ((source) << 16)
44 #define header_rcode(rcode) ((rcode) << 12)
45 #define header_offset_high(offset_high) ((offset_high) << 0)
46 #define header_data_length(length) ((length) << 16)
47 #define header_extended_tcode(tcode) ((tcode) << 0)
49 #define header_get_tcode(q) (((q) >> 4) & 0x0f)
50 #define header_get_tlabel(q) (((q) >> 10) & 0x3f)
51 #define header_get_rcode(q) (((q) >> 4) & 0x0f)
52 #define header_get_destination(q) (((q) >> 16) & 0xffff)
53 #define header_get_source(q) (((q) >> 16) & 0xffff)
54 #define header_get_offset_high(q) (((q) >> 0) & 0xffff)
55 #define header_get_data_length(q) (((q) >> 16) & 0xffff)
56 #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
58 #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22))
59 #define phy_config_root_id(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
60 #define phy_identifier(id) ((id) << 30)
63 close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode,
64 u32 * payload, size_t length)
68 spin_lock_irqsave(&card->lock, flags);
69 card->tlabel_mask &= ~(1 << t->tlabel);
71 spin_unlock_irqrestore(&card->lock, flags);
73 t->callback(card, rcode, payload, length, t->callback_data);
77 transmit_complete_callback(struct fw_packet *packet,
78 struct fw_card *card, int status)
80 struct fw_transaction *t =
81 container_of(packet, struct fw_transaction, packet);
85 close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
88 t->timestamp = packet->timestamp;
93 close_transaction(t, card, RCODE_BUSY, NULL, 0);
97 close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
100 /* FIXME: In this case, status is a negative errno,
101 * corresponding to an OHCI specific transmit error
102 * code. We should map that to an RCODE instead of
103 * just the generic RCODE_SEND_ERROR. */
104 close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
110 fw_fill_packet(struct fw_packet *packet, int tcode, int tlabel,
111 int node_id, int generation, int speed,
112 unsigned long long offset, void *payload, size_t length)
117 ext_tcode = tcode - 0x10;
118 tcode = TCODE_LOCK_REQUEST;
123 header_retry(RETRY_X) |
124 header_tlabel(tlabel) |
125 header_tcode(tcode) |
126 header_destination(node_id);
128 header_offset_high(offset >> 32) | header_source(0);
133 case TCODE_WRITE_QUADLET_REQUEST:
134 packet->header[3] = *(u32 *)payload;
135 packet->header_length = 16;
136 packet->payload_length = 0;
139 case TCODE_LOCK_REQUEST:
140 case TCODE_WRITE_BLOCK_REQUEST:
142 header_data_length(length) |
143 header_extended_tcode(ext_tcode);
144 packet->header_length = 16;
145 packet->payload = payload;
146 packet->payload_length = length;
149 case TCODE_READ_QUADLET_REQUEST:
150 packet->header_length = 12;
151 packet->payload_length = 0;
154 case TCODE_READ_BLOCK_REQUEST:
156 header_data_length(length) |
157 header_extended_tcode(ext_tcode);
158 packet->header_length = 16;
159 packet->payload_length = 0;
163 packet->speed = speed;
164 packet->generation = generation;
168 * This function provides low-level access to the IEEE1394 transaction
169 * logic. Most C programs would use either fw_read(), fw_write() or
170 * fw_lock() instead - those function are convenience wrappers for
171 * this function. The fw_send_request() function is primarily
172 * provided as a flexible, one-stop entry point for languages bindings
173 * and protocol bindings.
175 * FIXME: Document this function further, in particular the possible
176 * values for rcode in the callback. In short, we map ACK_COMPLETE to
177 * RCODE_COMPLETE, internal errors set errno and set rcode to
178 * RCODE_SEND_ERROR (which is out of range for standard ieee1394
179 * rcodes). All other rcodes are forwarded unchanged. For all
180 * errors, payload is NULL, length is 0.
182 * Can not expect the callback to be called before the function
183 * returns, though this does happen in some cases (ACK_COMPLETE and
186 * The payload is only used for write requests and must not be freed
187 * until the callback has been called.
189 * @param card the card from which to send the request
190 * @param tcode the tcode for this transaction. Do not use
191 * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
192 * etc. to specify tcode and ext_tcode.
193 * @param node_id the destination node ID (bus ID and PHY ID concatenated)
194 * @param generation the generation for which node_id is valid
195 * @param speed the speed to use for sending the request
196 * @param offset the 48 bit offset on the destination node
197 * @param payload the data payload for the request subaction
198 * @param length the length in bytes of the data to read
199 * @param callback function to be called when the transaction is completed
200 * @param callback_data pointer to arbitrary data, which will be
201 * passed to the callback
204 fw_send_request(struct fw_card *card, struct fw_transaction *t,
205 int tcode, int node_id, int generation, int speed,
206 unsigned long long offset,
207 void *payload, size_t length,
208 fw_transaction_callback_t callback, void *callback_data)
213 /* Bump the flush timer up 100ms first of all so we
214 * don't race with a flush timer callback. */
216 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
218 /* Allocate tlabel from the bitmap and put the transaction on
219 * the list while holding the card spinlock. */
221 spin_lock_irqsave(&card->lock, flags);
223 tlabel = card->current_tlabel;
224 if (card->tlabel_mask & (1 << tlabel)) {
225 spin_unlock_irqrestore(&card->lock, flags);
226 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
230 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
231 card->tlabel_mask |= (1 << tlabel);
233 list_add_tail(&t->link, &card->transaction_list);
235 spin_unlock_irqrestore(&card->lock, flags);
237 /* Initialize rest of transaction, fill out packet and send it. */
238 t->node_id = node_id;
240 t->callback = callback;
241 t->callback_data = callback_data;
243 fw_fill_packet(&t->packet, tcode, t->tlabel,
244 node_id, generation, speed, offset, payload, length);
245 t->packet.callback = transmit_complete_callback;
247 card->driver->send_request(card, &t->packet);
249 EXPORT_SYMBOL(fw_send_request);
252 transmit_phy_packet_callback(struct fw_packet *packet,
253 struct fw_card *card, int status)
258 static void send_phy_packet(struct fw_card *card, u32 data, int generation)
260 struct fw_packet *packet;
262 packet = kzalloc(sizeof *packet, GFP_ATOMIC);
266 packet->header[0] = data;
267 packet->header[1] = ~data;
268 packet->header_length = 8;
269 packet->payload_length = 0;
270 packet->speed = SCODE_100;
271 packet->generation = generation;
272 packet->callback = transmit_phy_packet_callback;
274 card->driver->send_request(card, packet);
277 void fw_send_force_root(struct fw_card *card, int node_id, int generation)
281 q = phy_identifier(PHY_PACKET_CONFIG) | phy_config_root_id(node_id);
282 send_phy_packet(card, q, generation);
285 void fw_flush_transactions(struct fw_card *card)
287 struct fw_transaction *t, *next;
288 struct list_head list;
291 INIT_LIST_HEAD(&list);
292 spin_lock_irqsave(&card->lock, flags);
293 list_splice_init(&card->transaction_list, &list);
294 card->tlabel_mask = 0;
295 spin_unlock_irqrestore(&card->lock, flags);
297 list_for_each_entry_safe(t, next, &list, link)
298 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
301 static struct fw_address_handler *
302 lookup_overlapping_address_handler(struct list_head *list,
303 unsigned long long offset, size_t length)
305 struct fw_address_handler *handler;
307 list_for_each_entry(handler, list, link) {
308 if (handler->offset < offset + length &&
309 offset < handler->offset + handler->length)
316 static struct fw_address_handler *
317 lookup_enclosing_address_handler(struct list_head *list,
318 unsigned long long offset, size_t length)
320 struct fw_address_handler *handler;
322 list_for_each_entry(handler, list, link) {
323 if (handler->offset <= offset &&
324 offset + length <= handler->offset + handler->length)
331 static DEFINE_SPINLOCK(address_handler_lock);
332 static LIST_HEAD(address_handler_list);
334 const struct fw_address_region fw_low_memory_region =
335 { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
336 const struct fw_address_region fw_high_memory_region =
337 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
338 const struct fw_address_region fw_private_region =
339 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
340 const struct fw_address_region fw_csr_region =
341 { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, };
342 const struct fw_address_region fw_unit_space_region =
343 { .start = 0xfffff0000900ULL, .end = 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
358 fw_core_add_address_handler(struct fw_address_handler *handler,
359 const struct fw_address_region *region)
361 struct fw_address_handler *other;
365 spin_lock_irqsave(&address_handler_lock, flags);
367 handler->offset = region->start;
368 while (handler->offset + handler->length <= region->end) {
370 lookup_overlapping_address_handler(&address_handler_list,
374 handler->offset += other->length;
376 list_add_tail(&handler->link, &address_handler_list);
382 spin_unlock_irqrestore(&address_handler_lock, flags);
386 EXPORT_SYMBOL(fw_core_add_address_handler);
389 * Deallocate a range of addresses allocated with fw_allocate. This
390 * will call the associated callback one last time with a the special
391 * tcode TCODE_DEALLOCATE, to let the client destroy the registered
392 * callback data. For convenience, the callback parameters offset and
393 * length are set to the start and the length respectively for the
394 * deallocated region, payload is set to NULL.
396 void fw_core_remove_address_handler(struct fw_address_handler *handler)
400 spin_lock_irqsave(&address_handler_lock, flags);
401 list_del(&handler->link);
402 spin_unlock_irqrestore(&address_handler_lock, flags);
404 EXPORT_SYMBOL(fw_core_remove_address_handler);
407 struct fw_packet response;
414 free_response_callback(struct fw_packet *packet,
415 struct fw_card *card, int status)
417 struct fw_request *request;
419 request = container_of(packet, struct fw_request, response);
424 fw_fill_response(struct fw_packet *response,
425 u32 *request, u32 *data, size_t length)
427 int tcode, tlabel, extended_tcode, source, destination;
429 tcode = header_get_tcode(request[0]);
430 tlabel = header_get_tlabel(request[0]);
431 source = header_get_destination(request[0]);
432 destination = header_get_source(request[1]);
433 extended_tcode = header_get_extended_tcode(request[3]);
435 response->header[0] =
436 header_retry(RETRY_1) |
437 header_tlabel(tlabel) |
438 header_destination(destination);
439 response->header[1] = header_source(source);
440 response->header[2] = 0;
443 case TCODE_WRITE_QUADLET_REQUEST:
444 case TCODE_WRITE_BLOCK_REQUEST:
445 response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE);
446 response->header_length = 12;
447 response->payload_length = 0;
450 case TCODE_READ_QUADLET_REQUEST:
451 response->header[0] |=
452 header_tcode(TCODE_READ_QUADLET_RESPONSE);
453 response->header[3] = 0;
454 response->header_length = 16;
455 response->payload_length = 0;
458 case TCODE_READ_BLOCK_REQUEST:
459 case TCODE_LOCK_REQUEST:
460 response->header[0] |= header_tcode(tcode + 2);
461 response->header[3] =
462 header_data_length(length) |
463 header_extended_tcode(extended_tcode);
464 response->header_length = 16;
465 response->payload = data;
466 response->payload_length = length;
475 static struct fw_request *
476 allocate_request(u32 *header, int ack,
477 int speed, int timestamp, int generation)
479 struct fw_request *request;
483 request_tcode = header_get_tcode(header[0]);
484 switch (request_tcode) {
485 case TCODE_WRITE_QUADLET_REQUEST:
490 case TCODE_WRITE_BLOCK_REQUEST:
491 case TCODE_LOCK_REQUEST:
493 length = header_get_data_length(header[3]);
496 case TCODE_READ_QUADLET_REQUEST:
501 case TCODE_READ_BLOCK_REQUEST:
503 length = header_get_data_length(header[3]);
511 request = kmalloc(sizeof *request + length, GFP_ATOMIC);
515 request->response.speed = speed;
516 request->response.timestamp = timestamp;
517 request->response.generation = generation;
518 request->response.callback = free_response_callback;
520 request->length = length;
522 memcpy(request->data, data, length);
524 fw_fill_response(&request->response, header, request->data, length);
530 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
534 /* Broadcast packets are reported as ACK_COMPLETE, so this
535 * check is sufficient to ensure we don't send response to
536 * broadcast packets or posted writes. */
537 if (request->ack != ACK_PENDING)
540 request->response.header[1] |= header_rcode(rcode);
541 response_tcode = header_get_tcode(request->response.header[0]);
542 if (rcode != RCODE_COMPLETE)
543 /* Clear the data_length field. */
544 request->response.header[3] &= 0xffff;
545 else if (response_tcode == TCODE_READ_QUADLET_RESPONSE)
546 request->response.header[3] = request->data[0];
548 card->driver->send_response(card, &request->response);
550 EXPORT_SYMBOL(fw_send_response);
553 fw_core_handle_request(struct fw_card *card,
554 int speed, int ack, int timestamp,
555 int generation, u32 length, u32 *header)
557 struct fw_address_handler *handler;
558 struct fw_request *request;
559 unsigned long long offset;
561 int tcode, destination, source, t;
564 /* FIXME: send error response. */
568 if (ack != ACK_PENDING && ack != ACK_COMPLETE)
571 t = (timestamp & 0x1fff) + 4000;
573 t = (timestamp & ~0x1fff) + 0x2000 + t - 8000;
575 t = (timestamp & ~0x1fff) + t;
577 request = allocate_request(header, ack, speed, t, generation);
578 if (request == NULL) {
579 /* FIXME: send statically allocated busy packet. */
584 ((unsigned long long)
585 header_get_offset_high(header[1]) << 32) | header[2];
586 tcode = header_get_tcode(header[0]);
587 destination = header_get_destination(header[0]);
588 source = header_get_source(header[0]);
590 spin_lock_irqsave(&address_handler_lock, flags);
591 handler = lookup_enclosing_address_handler(&address_handler_list,
592 offset, request->length);
593 spin_unlock_irqrestore(&address_handler_lock, flags);
595 /* FIXME: lookup the fw_node corresponding to the sender of
596 * this request and pass that to the address handler instead
597 * of the node ID. We may also want to move the address
598 * allocations to fw_node so we only do this callback if the
599 * upper layers registered it for this node. */
602 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
604 handler->address_callback(card, request,
605 tcode, destination, source,
606 generation, speed, offset,
607 request->data, request->length,
608 handler->callback_data);
610 EXPORT_SYMBOL(fw_core_handle_request);
613 fw_core_handle_response(struct fw_card *card,
614 int speed, int ack, int timestamp,
615 u32 length, u32 *header)
617 struct fw_transaction *t;
621 int tcode, tlabel, destination, source, rcode;
623 tcode = header_get_tcode(header[0]);
624 tlabel = header_get_tlabel(header[0]);
625 destination = header_get_destination(header[0]);
626 source = header_get_source(header[1]);
627 rcode = header_get_rcode(header[1]);
629 spin_lock_irqsave(&card->lock, flags);
630 list_for_each_entry(t, &card->transaction_list, link) {
631 if (t->node_id == source && t->tlabel == tlabel) {
633 card->tlabel_mask &= ~(1 << t->tlabel);
637 spin_unlock_irqrestore(&card->lock, flags);
639 if (&t->link == &card->transaction_list) {
640 fw_notify("Unsolicited response\n");
644 /* FIXME: sanity check packet, is length correct, does tcodes
645 * and addresses match. */
648 case TCODE_READ_QUADLET_RESPONSE:
649 data = (u32 *) &header[3];
653 case TCODE_WRITE_RESPONSE:
658 case TCODE_READ_BLOCK_RESPONSE:
659 case TCODE_LOCK_RESPONSE:
661 data_length = header_get_data_length(header[3]);
665 /* Should never happen, this is just to shut up gcc. */
671 t->callback(card, rcode, data, data_length, t->callback_data);
673 EXPORT_SYMBOL(fw_core_handle_response);
675 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
676 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
677 MODULE_LICENSE("GPL");
679 static const u32 vendor_textual_descriptor_data[] = {
680 /* textual descriptor leaf () */
684 0x4c696e75, /* L i n u */
685 0x78204669, /* x F i */
686 0x72657769, /* r e w i */
687 0x72652028, /* r e ( */
688 0x4a554a55, /* J U J U */
692 static struct fw_descriptor vendor_textual_descriptor = {
693 .length = ARRAY_SIZE(vendor_textual_descriptor_data),
695 .data = vendor_textual_descriptor_data,
698 static int __init fw_core_init(void)
702 retval = bus_register(&fw_bus_type);
706 /* Add the vendor textual descriptor. */
707 retval = fw_core_add_descriptor(&vendor_textual_descriptor);
713 static void __exit fw_core_cleanup(void)
715 bus_unregister(&fw_bus_type);
718 module_init(fw_core_init);
719 module_exit(fw_core_cleanup);