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