firewire: ohci: access bus_seconds atomically
[deliverable/linux.git] / drivers / firewire / fw-transaction.c
CommitLineData
c781c06d
KH
1/*
2 * Core IEEE1394 transaction logic
3038e353
KH
3 *
4 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
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.
10 *
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.
15 *
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.
19 */
20
2a0a2590 21#include <linux/completion.h>
d6053e08 22#include <linux/idr.h>
3038e353 23#include <linux/kernel.h>
ae1e5355 24#include <linux/kref.h>
3038e353 25#include <linux/module.h>
c0220d68 26#include <linux/mutex.h>
3038e353
KH
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/pci.h>
30#include <linux/delay.h>
31#include <linux/poll.h>
32#include <linux/list.h>
33#include <linux/kthread.h>
34#include <asm/uaccess.h>
3038e353
KH
35
36#include "fw-transaction.h"
37#include "fw-topology.h"
19a15b93 38#include "fw-device.h"
3038e353 39
a77754a7
KH
40#define HEADER_PRI(pri) ((pri) << 0)
41#define HEADER_TCODE(tcode) ((tcode) << 4)
42#define HEADER_RETRY(retry) ((retry) << 8)
43#define HEADER_TLABEL(tlabel) ((tlabel) << 10)
44#define HEADER_DESTINATION(destination) ((destination) << 16)
45#define HEADER_SOURCE(source) ((source) << 16)
46#define HEADER_RCODE(rcode) ((rcode) << 12)
47#define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
48#define HEADER_DATA_LENGTH(length) ((length) << 16)
49#define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
50
51#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
52#define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
53#define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
54#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
55#define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
56#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
57#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
58#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
59
a7ea6782
SR
60#define HEADER_DESTINATION_IS_BROADCAST(q) \
61 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
62
a77754a7
KH
63#define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
64#define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
65#define PHY_IDENTIFIER(id) ((id) << 30)
3038e353 66
53dca511 67static int close_transaction(struct fw_transaction *transaction,
a38a00fd 68 struct fw_card *card, int rcode)
3038e353 69{
730c32f5 70 struct fw_transaction *t;
3038e353
KH
71 unsigned long flags;
72
73 spin_lock_irqsave(&card->lock, flags);
730c32f5
KH
74 list_for_each_entry(t, &card->transaction_list, link) {
75 if (t == transaction) {
76 list_del(&t->link);
77 card->tlabel_mask &= ~(1 << t->tlabel);
78 break;
79 }
80 }
3038e353
KH
81 spin_unlock_irqrestore(&card->lock, flags);
82
730c32f5 83 if (&t->link != &card->transaction_list) {
a38a00fd 84 t->callback(card, rcode, NULL, 0, t->callback_data);
730c32f5
KH
85 return 0;
86 }
87
88 return -ENOENT;
3038e353
KH
89}
90
c781c06d
KH
91/*
92 * Only valid for transactions that are potentially pending (ie have
93 * been sent).
94 */
53dca511
SR
95int fw_cancel_transaction(struct fw_card *card,
96 struct fw_transaction *transaction)
730c32f5 97{
c781c06d
KH
98 /*
99 * Cancel the packet transmission if it's still queued. That
730c32f5 100 * will call the packet transmission callback which cancels
c781c06d
KH
101 * the transaction.
102 */
730c32f5
KH
103
104 if (card->driver->cancel_packet(card, &transaction->packet) == 0)
105 return 0;
106
c781c06d
KH
107 /*
108 * If the request packet has already been sent, we need to see
109 * if the transaction is still pending and remove it in that case.
110 */
730c32f5 111
a38a00fd 112 return close_transaction(transaction, card, RCODE_CANCELLED);
730c32f5
KH
113}
114EXPORT_SYMBOL(fw_cancel_transaction);
115
53dca511
SR
116static void transmit_complete_callback(struct fw_packet *packet,
117 struct fw_card *card, int status)
3038e353
KH
118{
119 struct fw_transaction *t =
120 container_of(packet, struct fw_transaction, packet);
121
122 switch (status) {
123 case ACK_COMPLETE:
a38a00fd 124 close_transaction(t, card, RCODE_COMPLETE);
3038e353
KH
125 break;
126 case ACK_PENDING:
127 t->timestamp = packet->timestamp;
128 break;
129 case ACK_BUSY_X:
130 case ACK_BUSY_A:
131 case ACK_BUSY_B:
a38a00fd 132 close_transaction(t, card, RCODE_BUSY);
3038e353
KH
133 break;
134 case ACK_DATA_ERROR:
a38a00fd 135 close_transaction(t, card, RCODE_DATA_ERROR);
e5f49c3b 136 break;
3038e353 137 case ACK_TYPE_ERROR:
a38a00fd 138 close_transaction(t, card, RCODE_TYPE_ERROR);
3038e353
KH
139 break;
140 default:
c781c06d
KH
141 /*
142 * In this case the ack is really a juju specific
143 * rcode, so just forward that to the callback.
144 */
a38a00fd 145 close_transaction(t, card, status);
3038e353
KH
146 break;
147 }
148}
149
53dca511 150static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
b9549bc6 151 int destination_id, int source_id, int generation, int speed,
36bfe49d 152 unsigned long long offset, void *payload, size_t length)
3038e353
KH
153{
154 int ext_tcode;
155
18e9b10f
SR
156 if (tcode == TCODE_STREAM_DATA) {
157 packet->header[0] =
158 HEADER_DATA_LENGTH(length) |
159 destination_id |
160 HEADER_TCODE(TCODE_STREAM_DATA);
161 packet->header_length = 4;
162 packet->payload = payload;
163 packet->payload_length = length;
164
165 goto common;
166 }
167
3038e353 168 if (tcode > 0x10) {
8f9f963e 169 ext_tcode = tcode & ~0x10;
3038e353
KH
170 tcode = TCODE_LOCK_REQUEST;
171 } else
172 ext_tcode = 0;
173
174 packet->header[0] =
a77754a7
KH
175 HEADER_RETRY(RETRY_X) |
176 HEADER_TLABEL(tlabel) |
177 HEADER_TCODE(tcode) |
b9549bc6 178 HEADER_DESTINATION(destination_id);
3038e353 179 packet->header[1] =
a77754a7 180 HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
3038e353
KH
181 packet->header[2] =
182 offset;
183
184 switch (tcode) {
185 case TCODE_WRITE_QUADLET_REQUEST:
186 packet->header[3] = *(u32 *)payload;
187 packet->header_length = 16;
188 packet->payload_length = 0;
189 break;
190
191 case TCODE_LOCK_REQUEST:
192 case TCODE_WRITE_BLOCK_REQUEST:
193 packet->header[3] =
a77754a7
KH
194 HEADER_DATA_LENGTH(length) |
195 HEADER_EXTENDED_TCODE(ext_tcode);
3038e353
KH
196 packet->header_length = 16;
197 packet->payload = payload;
198 packet->payload_length = length;
199 break;
200
201 case TCODE_READ_QUADLET_REQUEST:
202 packet->header_length = 12;
203 packet->payload_length = 0;
204 break;
205
206 case TCODE_READ_BLOCK_REQUEST:
207 packet->header[3] =
a77754a7
KH
208 HEADER_DATA_LENGTH(length) |
209 HEADER_EXTENDED_TCODE(ext_tcode);
3038e353
KH
210 packet->header_length = 16;
211 packet->payload_length = 0;
212 break;
213 }
18e9b10f 214 common:
3038e353
KH
215 packet->speed = speed;
216 packet->generation = generation;
730c32f5 217 packet->ack = 0;
1d1dc5e8 218 packet->payload_bus = 0;
3038e353
KH
219}
220
221/**
222 * This function provides low-level access to the IEEE1394 transaction
223 * logic. Most C programs would use either fw_read(), fw_write() or
224 * fw_lock() instead - those function are convenience wrappers for
225 * this function. The fw_send_request() function is primarily
226 * provided as a flexible, one-stop entry point for languages bindings
227 * and protocol bindings.
228 *
229 * FIXME: Document this function further, in particular the possible
230 * values for rcode in the callback. In short, we map ACK_COMPLETE to
231 * RCODE_COMPLETE, internal errors set errno and set rcode to
232 * RCODE_SEND_ERROR (which is out of range for standard ieee1394
233 * rcodes). All other rcodes are forwarded unchanged. For all
234 * errors, payload is NULL, length is 0.
235 *
236 * Can not expect the callback to be called before the function
237 * returns, though this does happen in some cases (ACK_COMPLETE and
238 * errors).
239 *
240 * The payload is only used for write requests and must not be freed
241 * until the callback has been called.
242 *
243 * @param card the card from which to send the request
244 * @param tcode the tcode for this transaction. Do not use
dbe7f76d 245 * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
3038e353 246 * etc. to specify tcode and ext_tcode.
907293d7 247 * @param node_id the destination node ID (bus ID and PHY ID concatenated)
3038e353
KH
248 * @param generation the generation for which node_id is valid
249 * @param speed the speed to use for sending the request
250 * @param offset the 48 bit offset on the destination node
251 * @param payload the data payload for the request subaction
252 * @param length the length in bytes of the data to read
253 * @param callback function to be called when the transaction is completed
254 * @param callback_data pointer to arbitrary data, which will be
255 * passed to the callback
18e9b10f
SR
256 *
257 * In case of asynchronous stream packets i.e. TCODE_STREAM_DATA, the caller
258 * needs to synthesize @destination_id with fw_stream_packet_destination_id().
3038e353 259 */
53dca511
SR
260void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
261 int destination_id, int generation, int speed,
262 unsigned long long offset, void *payload, size_t length,
263 fw_transaction_callback_t callback, void *callback_data)
3038e353
KH
264{
265 unsigned long flags;
b9549bc6 266 int tlabel;
3038e353 267
c781c06d
KH
268 /*
269 * Bump the flush timer up 100ms first of all so we
270 * don't race with a flush timer callback.
271 */
3038e353
KH
272
273 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
274
c781c06d
KH
275 /*
276 * Allocate tlabel from the bitmap and put the transaction on
277 * the list while holding the card spinlock.
278 */
3038e353
KH
279
280 spin_lock_irqsave(&card->lock, flags);
281
282 tlabel = card->current_tlabel;
283 if (card->tlabel_mask & (1 << tlabel)) {
284 spin_unlock_irqrestore(&card->lock, flags);
285 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
286 return;
287 }
288
289 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
290 card->tlabel_mask |= (1 << tlabel);
291
1e119fa9 292 t->node_id = destination_id;
3038e353
KH
293 t->tlabel = tlabel;
294 t->callback = callback;
295 t->callback_data = callback_data;
296
1e119fa9
JF
297 fw_fill_request(&t->packet, tcode, t->tlabel,
298 destination_id, card->node_id, generation,
299 speed, offset, payload, length);
3038e353
KH
300 t->packet.callback = transmit_complete_callback;
301
e9aeb46c
SR
302 list_add_tail(&t->link, &card->transaction_list);
303
304 spin_unlock_irqrestore(&card->lock, flags);
305
3038e353
KH
306 card->driver->send_request(card, &t->packet);
307}
308EXPORT_SYMBOL(fw_send_request);
309
1e119fa9
JF
310struct transaction_callback_data {
311 struct completion done;
312 void *payload;
313 int rcode;
314};
315
316static void transaction_callback(struct fw_card *card, int rcode,
317 void *payload, size_t length, void *data)
318{
319 struct transaction_callback_data *d = data;
320
321 if (rcode == RCODE_COMPLETE)
322 memcpy(d->payload, payload, length);
323 d->rcode = rcode;
324 complete(&d->done);
325}
326
327/**
328 * fw_run_transaction - send request and sleep until transaction is completed
329 *
330 * Returns the RCODE.
331 */
332int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
53dca511 333 int generation, int speed, unsigned long long offset,
ba27e1f7 334 void *payload, size_t length)
1e119fa9
JF
335{
336 struct transaction_callback_data d;
337 struct fw_transaction t;
338
339 init_completion(&d.done);
ba27e1f7 340 d.payload = payload;
1e119fa9 341 fw_send_request(card, &t, tcode, destination_id, generation, speed,
ba27e1f7 342 offset, payload, length, transaction_callback, &d);
1e119fa9
JF
343 wait_for_completion(&d.done);
344
345 return d.rcode;
346}
347EXPORT_SYMBOL(fw_run_transaction);
348
c0220d68
SR
349static DEFINE_MUTEX(phy_config_mutex);
350static DECLARE_COMPLETION(phy_config_done);
ae1e5355
SR
351
352static void transmit_phy_packet_callback(struct fw_packet *packet,
353 struct fw_card *card, int status)
3038e353 354{
c0220d68 355 complete(&phy_config_done);
3038e353
KH
356}
357
c0220d68
SR
358static struct fw_packet phy_config_packet = {
359 .header_length = 8,
360 .payload_length = 0,
361 .speed = SCODE_100,
362 .callback = transmit_phy_packet_callback,
363};
364
83db801c
KH
365void fw_send_phy_config(struct fw_card *card,
366 int node_id, int generation, int gap_count)
3038e353 367{
ae1e5355 368 long timeout = DIV_ROUND_UP(HZ, 10);
2a0a2590
SR
369 u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
370 PHY_CONFIG_ROOT_ID(node_id) |
371 PHY_CONFIG_GAP_COUNT(gap_count);
372
c0220d68
SR
373 mutex_lock(&phy_config_mutex);
374
375 phy_config_packet.header[0] = data;
376 phy_config_packet.header[1] = ~data;
377 phy_config_packet.generation = generation;
378 INIT_COMPLETION(phy_config_done);
379
380 card->driver->send_request(card, &phy_config_packet);
381 wait_for_completion_timeout(&phy_config_done, timeout);
ae1e5355 382
c0220d68 383 mutex_unlock(&phy_config_mutex);
3038e353
KH
384}
385
386void fw_flush_transactions(struct fw_card *card)
387{
388 struct fw_transaction *t, *next;
389 struct list_head list;
390 unsigned long flags;
391
392 INIT_LIST_HEAD(&list);
393 spin_lock_irqsave(&card->lock, flags);
394 list_splice_init(&card->transaction_list, &list);
395 card->tlabel_mask = 0;
396 spin_unlock_irqrestore(&card->lock, flags);
397
730c32f5
KH
398 list_for_each_entry_safe(t, next, &list, link) {
399 card->driver->cancel_packet(card, &t->packet);
400
c781c06d
KH
401 /*
402 * At this point cancel_packet will never call the
730c32f5 403 * transaction callback, since we just took all the
c781c06d
KH
404 * transactions out of the list. So do it here.
405 */
3038e353 406 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
730c32f5 407 }
3038e353
KH
408}
409
53dca511
SR
410static struct fw_address_handler *lookup_overlapping_address_handler(
411 struct list_head *list, unsigned long long offset, size_t length)
3038e353
KH
412{
413 struct fw_address_handler *handler;
414
415 list_for_each_entry(handler, list, link) {
416 if (handler->offset < offset + length &&
417 offset < handler->offset + handler->length)
418 return handler;
419 }
420
421 return NULL;
422}
423
53dca511
SR
424static struct fw_address_handler *lookup_enclosing_address_handler(
425 struct list_head *list, unsigned long long offset, size_t length)
3038e353
KH
426{
427 struct fw_address_handler *handler;
428
429 list_for_each_entry(handler, list, link) {
430 if (handler->offset <= offset &&
431 offset + length <= handler->offset + handler->length)
432 return handler;
433 }
434
435 return NULL;
436}
437
438static DEFINE_SPINLOCK(address_handler_lock);
439static LIST_HEAD(address_handler_list);
440
21ebcd12 441const struct fw_address_region fw_high_memory_region =
5af4e5ea 442 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
db8be076
AB
443EXPORT_SYMBOL(fw_high_memory_region);
444
445#if 0
446const struct fw_address_region fw_low_memory_region =
447 { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
21ebcd12 448const struct fw_address_region fw_private_region =
5af4e5ea 449 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
21ebcd12 450const struct fw_address_region fw_csr_region =
cca60977
JW
451 { .start = CSR_REGISTER_BASE,
452 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
21ebcd12 453const struct fw_address_region fw_unit_space_region =
5af4e5ea 454 { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
db8be076 455#endif /* 0 */
3038e353
KH
456
457/**
3e0b5f0d
SR
458 * fw_core_add_address_handler - register for incoming requests
459 * @handler: callback
460 * @region: region in the IEEE 1212 node space address range
461 *
462 * region->start, ->end, and handler->length have to be quadlet-aligned.
463 *
464 * When a request is received that falls within the specified address range,
465 * the specified callback is invoked. The parameters passed to the callback
466 * give the details of the particular request.
1415d918
SR
467 *
468 * Return value: 0 on success, non-zero otherwise.
469 * The start offset of the handler's address region is determined by
470 * fw_core_add_address_handler() and is returned in handler->offset.
3038e353 471 */
53dca511
SR
472int fw_core_add_address_handler(struct fw_address_handler *handler,
473 const struct fw_address_region *region)
3038e353
KH
474{
475 struct fw_address_handler *other;
476 unsigned long flags;
477 int ret = -EBUSY;
478
3e0b5f0d
SR
479 if (region->start & 0xffff000000000003ULL ||
480 region->end & 0xffff000000000003ULL ||
481 region->start >= region->end ||
482 handler->length & 3 ||
483 handler->length == 0)
484 return -EINVAL;
485
3038e353
KH
486 spin_lock_irqsave(&address_handler_lock, flags);
487
3e0b5f0d 488 handler->offset = region->start;
3038e353
KH
489 while (handler->offset + handler->length <= region->end) {
490 other =
491 lookup_overlapping_address_handler(&address_handler_list,
492 handler->offset,
493 handler->length);
494 if (other != NULL) {
3e0b5f0d 495 handler->offset += other->length;
3038e353
KH
496 } else {
497 list_add_tail(&handler->link, &address_handler_list);
498 ret = 0;
499 break;
500 }
501 }
502
503 spin_unlock_irqrestore(&address_handler_lock, flags);
504
505 return ret;
506}
3038e353
KH
507EXPORT_SYMBOL(fw_core_add_address_handler);
508
509/**
44be21b6 510 * fw_core_remove_address_handler - unregister an address handler
3038e353 511 */
3038e353
KH
512void fw_core_remove_address_handler(struct fw_address_handler *handler)
513{
514 unsigned long flags;
515
516 spin_lock_irqsave(&address_handler_lock, flags);
517 list_del(&handler->link);
518 spin_unlock_irqrestore(&address_handler_lock, flags);
519}
3038e353
KH
520EXPORT_SYMBOL(fw_core_remove_address_handler);
521
522struct fw_request {
523 struct fw_packet response;
36bfe49d 524 u32 request_header[4];
3038e353
KH
525 int ack;
526 u32 length;
527 u32 data[0];
528};
529
53dca511
SR
530static void free_response_callback(struct fw_packet *packet,
531 struct fw_card *card, int status)
3038e353
KH
532{
533 struct fw_request *request;
534
535 request = container_of(packet, struct fw_request, response);
536 kfree(request);
537}
538
53dca511
SR
539void fw_fill_response(struct fw_packet *response, u32 *request_header,
540 int rcode, void *payload, size_t length)
3038e353
KH
541{
542 int tcode, tlabel, extended_tcode, source, destination;
543
a77754a7
KH
544 tcode = HEADER_GET_TCODE(request_header[0]);
545 tlabel = HEADER_GET_TLABEL(request_header[0]);
546 source = HEADER_GET_DESTINATION(request_header[0]);
547 destination = HEADER_GET_SOURCE(request_header[1]);
548 extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
3038e353
KH
549
550 response->header[0] =
a77754a7
KH
551 HEADER_RETRY(RETRY_1) |
552 HEADER_TLABEL(tlabel) |
553 HEADER_DESTINATION(destination);
36bfe49d 554 response->header[1] =
a77754a7
KH
555 HEADER_SOURCE(source) |
556 HEADER_RCODE(rcode);
3038e353
KH
557 response->header[2] = 0;
558
559 switch (tcode) {
560 case TCODE_WRITE_QUADLET_REQUEST:
561 case TCODE_WRITE_BLOCK_REQUEST:
a77754a7 562 response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
3038e353
KH
563 response->header_length = 12;
564 response->payload_length = 0;
565 break;
566
567 case TCODE_READ_QUADLET_REQUEST:
568 response->header[0] |=
a77754a7 569 HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
93c4cceb
KH
570 if (payload != NULL)
571 response->header[3] = *(u32 *)payload;
572 else
573 response->header[3] = 0;
3038e353
KH
574 response->header_length = 16;
575 response->payload_length = 0;
576 break;
577
578 case TCODE_READ_BLOCK_REQUEST:
579 case TCODE_LOCK_REQUEST:
a77754a7 580 response->header[0] |= HEADER_TCODE(tcode + 2);
3038e353 581 response->header[3] =
a77754a7
KH
582 HEADER_DATA_LENGTH(length) |
583 HEADER_EXTENDED_TCODE(extended_tcode);
3038e353 584 response->header_length = 16;
36bfe49d
KH
585 response->payload = payload;
586 response->payload_length = length;
3038e353
KH
587 break;
588
589 default:
590 BUG();
591 return;
592 }
1d1dc5e8
SR
593
594 response->payload_bus = 0;
3038e353 595}
93c4cceb 596EXPORT_SYMBOL(fw_fill_response);
3038e353 597
53dca511 598static struct fw_request *allocate_request(struct fw_packet *p)
3038e353
KH
599{
600 struct fw_request *request;
601 u32 *data, length;
2639a6fb 602 int request_tcode, t;
3038e353 603
a77754a7 604 request_tcode = HEADER_GET_TCODE(p->header[0]);
3038e353
KH
605 switch (request_tcode) {
606 case TCODE_WRITE_QUADLET_REQUEST:
2639a6fb 607 data = &p->header[3];
3038e353
KH
608 length = 4;
609 break;
610
611 case TCODE_WRITE_BLOCK_REQUEST:
612 case TCODE_LOCK_REQUEST:
2639a6fb 613 data = p->payload;
a77754a7 614 length = HEADER_GET_DATA_LENGTH(p->header[3]);
3038e353
KH
615 break;
616
617 case TCODE_READ_QUADLET_REQUEST:
618 data = NULL;
619 length = 4;
620 break;
621
622 case TCODE_READ_BLOCK_REQUEST:
623 data = NULL;
a77754a7 624 length = HEADER_GET_DATA_LENGTH(p->header[3]);
3038e353
KH
625 break;
626
627 default:
0bf607c5
SR
628 fw_error("ERROR - corrupt request received - %08x %08x %08x\n",
629 p->header[0], p->header[1], p->header[2]);
3038e353
KH
630 return NULL;
631 }
632
2d826cc5 633 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
3038e353
KH
634 if (request == NULL)
635 return NULL;
636
2639a6fb
KH
637 t = (p->timestamp & 0x1fff) + 4000;
638 if (t >= 8000)
639 t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
640 else
641 t = (p->timestamp & ~0x1fff) + t;
642
643 request->response.speed = p->speed;
644 request->response.timestamp = t;
645 request->response.generation = p->generation;
730c32f5 646 request->response.ack = 0;
3038e353 647 request->response.callback = free_response_callback;
2639a6fb 648 request->ack = p->ack;
93c4cceb 649 request->length = length;
3038e353 650 if (data)
6e2e8424 651 memcpy(request->data, data, length);
3038e353 652
2d826cc5 653 memcpy(request->request_header, p->header, sizeof(p->header));
3038e353
KH
654
655 return request;
656}
657
53dca511
SR
658void fw_send_response(struct fw_card *card,
659 struct fw_request *request, int rcode)
3038e353 660{
a7ea6782
SR
661 /* unified transaction or broadcast transaction: don't respond */
662 if (request->ack != ACK_PENDING ||
663 HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
9c9bdf4d 664 kfree(request);
3038e353 665 return;
9c9bdf4d 666 }
3038e353 667
36bfe49d
KH
668 if (rcode == RCODE_COMPLETE)
669 fw_fill_response(&request->response, request->request_header,
670 rcode, request->data, request->length);
671 else
672 fw_fill_response(&request->response, request->request_header,
673 rcode, NULL, 0);
3038e353
KH
674
675 card->driver->send_response(card, &request->response);
676}
3038e353
KH
677EXPORT_SYMBOL(fw_send_response);
678
53dca511 679void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
3038e353
KH
680{
681 struct fw_address_handler *handler;
682 struct fw_request *request;
683 unsigned long long offset;
684 unsigned long flags;
2639a6fb 685 int tcode, destination, source;
3038e353 686
2639a6fb 687 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
3038e353
KH
688 return;
689
2639a6fb 690 request = allocate_request(p);
3038e353
KH
691 if (request == NULL) {
692 /* FIXME: send statically allocated busy packet. */
693 return;
694 }
695
696 offset =
697 ((unsigned long long)
a77754a7
KH
698 HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) | p->header[2];
699 tcode = HEADER_GET_TCODE(p->header[0]);
700 destination = HEADER_GET_DESTINATION(p->header[0]);
478b233e 701 source = HEADER_GET_SOURCE(p->header[1]);
3038e353
KH
702
703 spin_lock_irqsave(&address_handler_lock, flags);
704 handler = lookup_enclosing_address_handler(&address_handler_list,
705 offset, request->length);
706 spin_unlock_irqrestore(&address_handler_lock, flags);
707
c781c06d
KH
708 /*
709 * FIXME: lookup the fw_node corresponding to the sender of
3038e353
KH
710 * this request and pass that to the address handler instead
711 * of the node ID. We may also want to move the address
712 * allocations to fw_node so we only do this callback if the
c781c06d
KH
713 * upper layers registered it for this node.
714 */
3038e353
KH
715
716 if (handler == NULL)
717 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
718 else
719 handler->address_callback(card, request,
720 tcode, destination, source,
2639a6fb 721 p->generation, p->speed, offset,
3038e353
KH
722 request->data, request->length,
723 handler->callback_data);
724}
3038e353
KH
725EXPORT_SYMBOL(fw_core_handle_request);
726
53dca511 727void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
3038e353
KH
728{
729 struct fw_transaction *t;
730 unsigned long flags;
731 u32 *data;
732 size_t data_length;
733 int tcode, tlabel, destination, source, rcode;
734
a77754a7
KH
735 tcode = HEADER_GET_TCODE(p->header[0]);
736 tlabel = HEADER_GET_TLABEL(p->header[0]);
737 destination = HEADER_GET_DESTINATION(p->header[0]);
738 source = HEADER_GET_SOURCE(p->header[1]);
739 rcode = HEADER_GET_RCODE(p->header[1]);
3038e353
KH
740
741 spin_lock_irqsave(&card->lock, flags);
742 list_for_each_entry(t, &card->transaction_list, link) {
743 if (t->node_id == source && t->tlabel == tlabel) {
744 list_del(&t->link);
745 card->tlabel_mask &= ~(1 << t->tlabel);
746 break;
747 }
748 }
749 spin_unlock_irqrestore(&card->lock, flags);
750
751 if (&t->link == &card->transaction_list) {
32b46093
KH
752 fw_notify("Unsolicited response (source %x, tlabel %x)\n",
753 source, tlabel);
3038e353
KH
754 return;
755 }
756
c781c06d
KH
757 /*
758 * FIXME: sanity check packet, is length correct, does tcodes
759 * and addresses match.
760 */
3038e353
KH
761
762 switch (tcode) {
763 case TCODE_READ_QUADLET_RESPONSE:
2639a6fb 764 data = (u32 *) &p->header[3];
3038e353
KH
765 data_length = 4;
766 break;
767
768 case TCODE_WRITE_RESPONSE:
769 data = NULL;
770 data_length = 0;
771 break;
772
773 case TCODE_READ_BLOCK_RESPONSE:
774 case TCODE_LOCK_RESPONSE:
93c4cceb 775 data = p->payload;
a77754a7 776 data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
3038e353
KH
777 break;
778
779 default:
780 /* Should never happen, this is just to shut up gcc. */
781 data = NULL;
782 data_length = 0;
783 break;
784 }
785
10a4c735
SR
786 /*
787 * The response handler may be executed while the request handler
788 * is still pending. Cancel the request handler.
789 */
790 card->driver->cancel_packet(card, &t->packet);
791
3038e353
KH
792 t->callback(card, rcode, data, data_length, t->callback_data);
793}
3038e353
KH
794EXPORT_SYMBOL(fw_core_handle_response);
795
ae57988f 796static const struct fw_address_region topology_map_region =
cca60977
JW
797 { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
798 .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
473d28c7 799
53dca511
SR
800static void handle_topology_map(struct fw_card *card, struct fw_request *request,
801 int tcode, int destination, int source, int generation,
802 int speed, unsigned long long offset,
803 void *payload, size_t length, void *callback_data)
473d28c7
KH
804{
805 int i, start, end;
efbf390a 806 __be32 *map;
473d28c7
KH
807
808 if (!TCODE_IS_READ_REQUEST(tcode)) {
809 fw_send_response(card, request, RCODE_TYPE_ERROR);
810 return;
811 }
812
813 if ((offset & 3) > 0 || (length & 3) > 0) {
814 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
815 return;
816 }
817
818 start = (offset - topology_map_region.start) / 4;
819 end = start + length / 4;
820 map = payload;
821
822 for (i = 0; i < length / 4; i++)
823 map[i] = cpu_to_be32(card->topology_map[start + i]);
824
825 fw_send_response(card, request, RCODE_COMPLETE);
826}
827
828static struct fw_address_handler topology_map = {
d60d7f1d 829 .length = 0x200,
473d28c7
KH
830 .address_callback = handle_topology_map,
831};
832
ae57988f 833static const struct fw_address_region registers_region =
cca60977
JW
834 { .start = CSR_REGISTER_BASE,
835 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
d60d7f1d 836
53dca511
SR
837static void handle_registers(struct fw_card *card, struct fw_request *request,
838 int tcode, int destination, int source, int generation,
839 int speed, unsigned long long offset,
840 void *payload, size_t length, void *callback_data)
d60d7f1d 841{
15f0d833 842 int reg = offset & ~CSR_REGISTER_BASE;
d60d7f1d
KH
843 unsigned long long bus_time;
844 __be32 *data = payload;
e534fe16 845 int rcode = RCODE_COMPLETE;
d60d7f1d
KH
846
847 switch (reg) {
848 case CSR_CYCLE_TIME:
849 case CSR_BUS_TIME:
850 if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
e534fe16 851 rcode = RCODE_TYPE_ERROR;
d60d7f1d
KH
852 break;
853 }
854
855 bus_time = card->driver->get_bus_time(card);
856 if (reg == CSR_CYCLE_TIME)
857 *data = cpu_to_be32(bus_time);
858 else
859 *data = cpu_to_be32(bus_time >> 25);
e534fe16
SR
860 break;
861
862 case CSR_BROADCAST_CHANNEL:
863 if (tcode == TCODE_READ_QUADLET_REQUEST)
864 *data = cpu_to_be32(card->broadcast_channel);
865 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
866 card->broadcast_channel =
867 (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
868 BROADCAST_CHANNEL_INITIAL;
869 else
870 rcode = RCODE_TYPE_ERROR;
d60d7f1d
KH
871 break;
872
873 case CSR_BUS_MANAGER_ID:
874 case CSR_BANDWIDTH_AVAILABLE:
875 case CSR_CHANNELS_AVAILABLE_HI:
876 case CSR_CHANNELS_AVAILABLE_LO:
c781c06d
KH
877 /*
878 * FIXME: these are handled by the OHCI hardware and
d60d7f1d
KH
879 * the stack never sees these request. If we add
880 * support for a new type of controller that doesn't
881 * handle this in hardware we need to deal with these
c781c06d
KH
882 * transactions.
883 */
d60d7f1d
KH
884 BUG();
885 break;
886
887 case CSR_BUSY_TIMEOUT:
888 /* FIXME: Implement this. */
e534fe16 889
d60d7f1d 890 default:
e534fe16 891 rcode = RCODE_ADDRESS_ERROR;
d60d7f1d
KH
892 break;
893 }
e534fe16
SR
894
895 fw_send_response(card, request, rcode);
d60d7f1d
KH
896}
897
898static struct fw_address_handler registers = {
899 .length = 0x400,
900 .address_callback = handle_registers,
901};
902
3038e353
KH
903MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
904MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
905MODULE_LICENSE("GPL");
906
937f6879 907static const u32 vendor_textual_descriptor[] = {
3038e353 908 /* textual descriptor leaf () */
937f6879 909 0x00060000,
3038e353
KH
910 0x00000000,
911 0x00000000,
912 0x4c696e75, /* L i n u */
913 0x78204669, /* x F i */
914 0x72657769, /* r e w i */
937f6879 915 0x72650000, /* r e */
3038e353
KH
916};
917
937f6879
KH
918static const u32 model_textual_descriptor[] = {
919 /* model descriptor leaf () */
920 0x00030000,
921 0x00000000,
922 0x00000000,
923 0x4a756a75, /* J u j u */
924};
925
926static struct fw_descriptor vendor_id_descriptor = {
927 .length = ARRAY_SIZE(vendor_textual_descriptor),
928 .immediate = 0x03d00d1e,
3038e353 929 .key = 0x81000000,
937f6879
KH
930 .data = vendor_textual_descriptor,
931};
932
933static struct fw_descriptor model_id_descriptor = {
934 .length = ARRAY_SIZE(model_textual_descriptor),
935 .immediate = 0x17000001,
936 .key = 0x81000000,
937 .data = model_textual_descriptor,
3038e353
KH
938};
939
3038e353
KH
940static int __init fw_core_init(void)
941{
2dbd7d7e 942 int ret;
3038e353 943
2dbd7d7e
SR
944 ret = bus_register(&fw_bus_type);
945 if (ret < 0)
946 return ret;
3038e353 947
a3aca3da
KH
948 fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
949 if (fw_cdev_major < 0) {
950 bus_unregister(&fw_bus_type);
951 return fw_cdev_major;
952 }
953
c490a6de
SR
954 fw_core_add_address_handler(&topology_map, &topology_map_region);
955 fw_core_add_address_handler(&registers, &registers_region);
956 fw_core_add_descriptor(&vendor_id_descriptor);
957 fw_core_add_descriptor(&model_id_descriptor);
3038e353
KH
958
959 return 0;
960}
961
962static void __exit fw_core_cleanup(void)
963{
a3aca3da 964 unregister_chrdev(fw_cdev_major, "firewire");
3038e353 965 bus_unregister(&fw_bus_type);
d6053e08 966 idr_destroy(&fw_device_idr);
3038e353
KH
967}
968
969module_init(fw_core_init);
970module_exit(fw_core_cleanup);
This page took 0.246744 seconds and 5 git commands to generate.