aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firewire/fw-transaction.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/firewire/fw-transaction.c')
-rw-r--r--drivers/firewire/fw-transaction.c730
1 files changed, 730 insertions, 0 deletions
diff --git a/drivers/firewire/fw-transaction.c b/drivers/firewire/fw-transaction.c
new file mode 100644
index 000000000000..c3acf7431279
--- /dev/null
+++ b/drivers/firewire/fw-transaction.c
@@ -0,0 +1,730 @@
1/* -*- c-basic-offset: 8 -*-
2 *
3 * fw-transaction.c - core IEEE1394 transaction logic
4 *
5 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
6 *
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.
11 *
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.
16 *
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.
20 */
21
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>
33
34#include "fw-transaction.h"
35#include "fw-topology.h"
36
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)
47
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)
56
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)
60
61static void
62close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode,
63 u32 * payload, size_t length)
64{
65 unsigned long flags;
66
67 spin_lock_irqsave(&card->lock, flags);
68 card->tlabel_mask &= ~(1 << t->tlabel);
69 list_del(&t->link);
70 spin_unlock_irqrestore(&card->lock, flags);
71
72 t->callback(card, rcode, payload, length, t->callback_data);
73}
74
75static void
76transmit_complete_callback(struct fw_packet *packet,
77 struct fw_card *card, int status)
78{
79 struct fw_transaction *t =
80 container_of(packet, struct fw_transaction, packet);
81
82 switch (status) {
83 case ACK_COMPLETE:
84 close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
85 break;
86 case ACK_PENDING:
87 t->timestamp = packet->timestamp;
88 break;
89 case ACK_BUSY_X:
90 case ACK_BUSY_A:
91 case ACK_BUSY_B:
92 close_transaction(t, card, RCODE_BUSY, NULL, 0);
93 break;
94 case ACK_DATA_ERROR:
95 case ACK_TYPE_ERROR:
96 close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
97 break;
98 default:
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);
104 break;
105 }
106}
107
108void
109fw_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)
112{
113 int ext_tcode;
114
115 if (tcode > 0x10) {
116 ext_tcode = tcode - 0x10;
117 tcode = TCODE_LOCK_REQUEST;
118 } else
119 ext_tcode = 0;
120
121 packet->header[0] =
122 header_retry(RETRY_X) |
123 header_tlabel(tlabel) |
124 header_tcode(tcode) |
125 header_destination(node_id | LOCAL_BUS);
126 packet->header[1] =
127 header_offset_high(offset >> 32) | header_source(0);
128 packet->header[2] =
129 offset;
130
131 switch (tcode) {
132 case TCODE_WRITE_QUADLET_REQUEST:
133 packet->header[3] = *(u32 *)payload;
134 packet->header_length = 16;
135 packet->payload_length = 0;
136 break;
137
138 case TCODE_LOCK_REQUEST:
139 case TCODE_WRITE_BLOCK_REQUEST:
140 packet->header[3] =
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;
146 break;
147
148 case TCODE_READ_QUADLET_REQUEST:
149 packet->header_length = 12;
150 packet->payload_length = 0;
151 break;
152
153 case TCODE_READ_BLOCK_REQUEST:
154 packet->header[3] =
155 header_data_length(length) |
156 header_extended_tcode(ext_tcode);
157 packet->header_length = 16;
158 packet->payload_length = 0;
159 break;
160 }
161
162 packet->speed = speed;
163 packet->generation = generation;
164}
165
166/**
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.
173 *
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.
180 *
181 * Can not expect the callback to be called before the function
182 * returns, though this does happen in some cases (ACK_COMPLETE and
183 * errors).
184 *
185 * The payload is only used for write requests and must not be freed
186 * until the callback has been called.
187 *
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
201 */
202void
203fw_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)
208{
209 unsigned long flags;
210 int tlabel;
211
212 /* Bump the flush timer up 100ms first of all so we
213 * don't race with a flush timer callback. */
214
215 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
216
217 /* Allocate tlabel from the bitmap and put the transaction on
218 * the list while holding the card spinlock. */
219
220 spin_lock_irqsave(&card->lock, flags);
221
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);
226 return;
227 }
228
229 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
230 card->tlabel_mask |= (1 << tlabel);
231
232 list_add_tail(&t->link, &card->transaction_list);
233
234 spin_unlock_irqrestore(&card->lock, flags);
235
236 /* Initialize rest of transaction, fill out packet and send it. */
237 t->node_id = node_id;
238 t->tlabel = tlabel;
239 t->callback = callback;
240 t->callback_data = callback_data;
241
242 fw_fill_packet(&t->packet, tcode, t->tlabel,
243 node_id, generation, speed, offset, payload, length);
244 t->packet.callback = transmit_complete_callback;
245
246 card->driver->send_request(card, &t->packet);
247}
248EXPORT_SYMBOL(fw_send_request);
249
250static void
251transmit_phy_packet_callback(struct fw_packet *packet,
252 struct fw_card *card, int status)
253{
254 kfree(packet);
255}
256
257static void send_phy_packet(struct fw_card *card, u32 data, int generation)
258{
259 struct fw_packet *packet;
260
261 packet = kzalloc(sizeof *packet, GFP_ATOMIC);
262 if (packet == NULL)
263 return;
264
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;
272
273 card->driver->send_request(card, packet);
274}
275
276void fw_send_force_root(struct fw_card *card, int node_id, int generation)
277{
278 u32 q;
279
280 q = phy_identifier(PHY_PACKET_CONFIG) | phy_config_root_id(node_id);
281 send_phy_packet(card, q, generation);
282}
283
284void fw_flush_transactions(struct fw_card *card)
285{
286 struct fw_transaction *t, *next;
287 struct list_head list;
288 unsigned long flags;
289
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);
295
296 list_for_each_entry_safe(t, next, &list, link)
297 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
298}
299
300static struct fw_address_handler *
301lookup_overlapping_address_handler(struct list_head *list,
302 unsigned long long offset, size_t length)
303{
304 struct fw_address_handler *handler;
305
306 list_for_each_entry(handler, list, link) {
307 if (handler->offset < offset + length &&
308 offset < handler->offset + handler->length)
309 return handler;
310 }
311
312 return NULL;
313}
314
315static struct fw_address_handler *
316lookup_enclosing_address_handler(struct list_head *list,
317 unsigned long long offset, size_t length)
318{
319 struct fw_address_handler *handler;
320
321 list_for_each_entry(handler, list, link) {
322 if (handler->offset <= offset &&
323 offset + length <= handler->offset + handler->length)
324 return handler;
325 }
326
327 return NULL;
328}
329
330static DEFINE_SPINLOCK(address_handler_lock);
331static LIST_HEAD(address_handler_list);
332
333struct fw_address_region fw_low_memory_region =
334 { 0x000000000000ull, 0x000100000000ull };
335struct fw_address_region fw_high_memory_region =
336 { 0x000100000000ull, 0xffffe0000000ull };
337struct fw_address_region fw_private_region =
338 { 0xffffe0000000ull, 0xfffff0000000ull };
339struct fw_address_region fw_csr_region =
340 { 0xfffff0000000ULL, 0xfffff0000800ull };
341struct fw_address_region fw_unit_space_region =
342 { 0xfffff0000900ull, 0x1000000000000ull };
343
344EXPORT_SYMBOL(fw_low_memory_region);
345EXPORT_SYMBOL(fw_high_memory_region);
346EXPORT_SYMBOL(fw_private_region);
347EXPORT_SYMBOL(fw_csr_region);
348EXPORT_SYMBOL(fw_unit_space_region);
349
350/**
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
355 * particular request
356 */
357
358int
359fw_core_add_address_handler(struct fw_address_handler *handler,
360 struct fw_address_region *region)
361{
362 struct fw_address_handler *other;
363 unsigned long flags;
364 int ret = -EBUSY;
365
366 spin_lock_irqsave(&address_handler_lock, flags);
367
368 handler->offset = region->start;
369 while (handler->offset + handler->length <= region->end) {
370 other =
371 lookup_overlapping_address_handler(&address_handler_list,
372 handler->offset,
373 handler->length);
374 if (other != NULL) {
375 handler->offset += other->length;
376 } else {
377 list_add_tail(&handler->link, &address_handler_list);
378 ret = 0;
379 break;
380 }
381 }
382
383 spin_unlock_irqrestore(&address_handler_lock, flags);
384
385 return ret;
386}
387
388EXPORT_SYMBOL(fw_core_add_address_handler);
389
390/**
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.
397 */
398
399void fw_core_remove_address_handler(struct fw_address_handler *handler)
400{
401 unsigned long flags;
402
403 spin_lock_irqsave(&address_handler_lock, flags);
404 list_del(&handler->link);
405 spin_unlock_irqrestore(&address_handler_lock, flags);
406}
407
408EXPORT_SYMBOL(fw_core_remove_address_handler);
409
410struct fw_request {
411 struct fw_packet response;
412 int ack;
413 u32 length;
414 u32 data[0];
415};
416
417static void
418free_response_callback(struct fw_packet *packet,
419 struct fw_card *card, int status)
420{
421 struct fw_request *request;
422
423 request = container_of(packet, struct fw_request, response);
424 kfree(request);
425}
426
427static void
428fw_fill_response(struct fw_packet *response,
429 u32 *request, u32 *data, size_t length)
430{
431 int tcode, tlabel, extended_tcode, source, destination;
432
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]);
438
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;
445
446 switch (tcode) {
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;
452 break;
453
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;
460 break;
461
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;
471 break;
472
473 default:
474 BUG();
475 return;
476 }
477}
478
479static struct fw_request *
480allocate_request(u32 *header, int ack,
481 int speed, int timestamp, int generation)
482{
483 struct fw_request *request;
484 u32 *data, length;
485 int request_tcode;
486
487 request_tcode = header_get_tcode(header[0]);
488 switch (request_tcode) {
489 case TCODE_WRITE_QUADLET_REQUEST:
490 data = &header[3];
491 length = 4;
492 break;
493
494 case TCODE_WRITE_BLOCK_REQUEST:
495 case TCODE_LOCK_REQUEST:
496 data = &header[4];
497 length = header_get_data_length(header[3]);
498 break;
499
500 case TCODE_READ_QUADLET_REQUEST:
501 data = NULL;
502 length = 4;
503 break;
504
505 case TCODE_READ_BLOCK_REQUEST:
506 data = NULL;
507 length = header_get_data_length(header[3]);
508 break;
509
510 default:
511 BUG();
512 return NULL;
513 }
514
515 request = kmalloc(sizeof *request + length, GFP_ATOMIC);
516 if (request == NULL)
517 return NULL;
518
519 request->response.speed = speed;
520 request->response.timestamp = timestamp;
521 request->response.generation = generation;
522 request->response.callback = free_response_callback;
523 request->ack = ack;
524 request->length = length;
525 if (data)
526 memcpy(request->data, data, length);
527
528 fw_fill_response(&request->response, header, request->data, length);
529
530 return request;
531}
532
533void
534fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
535{
536 int response_tcode;
537
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)
542 return;
543
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];
551
552 card->driver->send_response(card, &request->response);
553}
554
555EXPORT_SYMBOL(fw_send_response);
556
557void
558fw_core_handle_request(struct fw_card *card,
559 int speed, int ack, int timestamp,
560 int generation, u32 length, u32 *header)
561{
562 struct fw_address_handler *handler;
563 struct fw_request *request;
564 unsigned long long offset;
565 unsigned long flags;
566 int tcode, destination, source, t;
567
568 if (length > 2048) {
569 /* FIXME: send error response. */
570 return;
571 }
572
573 if (ack != ACK_PENDING && ack != ACK_COMPLETE)
574 return;
575
576 t = (timestamp & 0x1fff) + 4000;
577 if (t >= 8000)
578 t = (timestamp & ~0x1fff) + 0x2000 + t - 8000;
579 else
580 t = (timestamp & ~0x1fff) + t;
581
582 request = allocate_request(header, ack, speed, t, generation);
583 if (request == NULL) {
584 /* FIXME: send statically allocated busy packet. */
585 return;
586 }
587
588 offset =
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]);
594
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);
599
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. */
605
606 if (handler == NULL)
607 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
608 else
609 handler->address_callback(card, request,
610 tcode, destination, source,
611 generation, speed, offset,
612 request->data, request->length,
613 handler->callback_data);
614}
615
616EXPORT_SYMBOL(fw_core_handle_request);
617
618void
619fw_core_handle_response(struct fw_card *card,
620 int speed, int ack, int timestamp,
621 u32 length, u32 *header)
622{
623 struct fw_transaction *t;
624 unsigned long flags;
625 u32 *data;
626 size_t data_length;
627 int tcode, tlabel, destination, source, rcode;
628
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]);
634
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) {
638 list_del(&t->link);
639 card->tlabel_mask &= ~(1 << t->tlabel);
640 break;
641 }
642 }
643 spin_unlock_irqrestore(&card->lock, flags);
644
645 if (&t->link == &card->transaction_list) {
646 fw_notify("Unsolicited response\n");
647 return;
648 }
649
650 /* FIXME: sanity check packet, is length correct, does tcodes
651 * and addresses match. */
652
653 switch (tcode) {
654 case TCODE_READ_QUADLET_RESPONSE:
655 data = (u32 *) &header[3];
656 data_length = 4;
657 break;
658
659 case TCODE_WRITE_RESPONSE:
660 data = NULL;
661 data_length = 0;
662 break;
663
664 case TCODE_READ_BLOCK_RESPONSE:
665 case TCODE_LOCK_RESPONSE:
666 data = &header[4];
667 data_length = header_get_data_length(header[3]);
668 break;
669
670 default:
671 /* Should never happen, this is just to shut up gcc. */
672 data = NULL;
673 data_length = 0;
674 break;
675 }
676
677 t->callback(card, rcode, data, data_length, t->callback_data);
678}
679
680EXPORT_SYMBOL(fw_core_handle_response);
681
682MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
683MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
684MODULE_LICENSE("GPL");
685
686static u32 vendor_textual_descriptor_data[] = {
687 /* textual descriptor leaf () */
688 0x00080000,
689 0x00000000,
690 0x00000000,
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 */
696 0x29000000, /* ) */
697};
698
699static struct fw_descriptor vendor_textual_descriptor = {
700 .length = ARRAY_SIZE(vendor_textual_descriptor_data),
701 .key = 0x81000000,
702 .data = vendor_textual_descriptor_data
703};
704
705struct bus_type fw_bus_type = {
706 .name = "fw",
707};
708
709static int __init fw_core_init(void)
710{
711 int retval;
712
713 retval = bus_register(&fw_bus_type);
714 if (retval < 0)
715 return retval;
716
717 /* Add the vendor textual descriptor. */
718 retval = fw_core_add_descriptor(&vendor_textual_descriptor);
719 BUG_ON(retval < 0);
720
721 return 0;
722}
723
724static void __exit fw_core_cleanup(void)
725{
726 bus_unregister(&fw_bus_type);
727}
728
729module_init(fw_core_init);
730module_exit(fw_core_cleanup);