Commit | Line | Data |
---|---|---|
3038e353 KH |
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" | |
19a15b93 | 36 | #include "fw-device.h" |
3038e353 KH |
37 | |
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) | |
9fc82689 | 51 | #define header_get_rcode(q) (((q) >> 12) & 0x0f) |
3038e353 KH |
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 | ||
58 | #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22)) | |
907293d7 | 59 | #define phy_config_root_id(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23)) |
3038e353 KH |
60 | #define phy_identifier(id) ((id) << 30) |
61 | ||
62 | static void | |
63 | close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode, | |
64 | u32 * payload, size_t length) | |
65 | { | |
66 | unsigned long flags; | |
67 | ||
68 | spin_lock_irqsave(&card->lock, flags); | |
69 | card->tlabel_mask &= ~(1 << t->tlabel); | |
70 | list_del(&t->link); | |
71 | spin_unlock_irqrestore(&card->lock, flags); | |
72 | ||
73 | t->callback(card, rcode, payload, length, t->callback_data); | |
74 | } | |
75 | ||
76 | static void | |
77 | transmit_complete_callback(struct fw_packet *packet, | |
78 | struct fw_card *card, int status) | |
79 | { | |
80 | struct fw_transaction *t = | |
81 | container_of(packet, struct fw_transaction, packet); | |
82 | ||
83 | switch (status) { | |
84 | case ACK_COMPLETE: | |
85 | close_transaction(t, card, RCODE_COMPLETE, NULL, 0); | |
86 | break; | |
87 | case ACK_PENDING: | |
88 | t->timestamp = packet->timestamp; | |
89 | break; | |
90 | case ACK_BUSY_X: | |
91 | case ACK_BUSY_A: | |
92 | case ACK_BUSY_B: | |
93 | close_transaction(t, card, RCODE_BUSY, NULL, 0); | |
94 | break; | |
95 | case ACK_DATA_ERROR: | |
e5f49c3b KH |
96 | close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0); |
97 | break; | |
3038e353 | 98 | case ACK_TYPE_ERROR: |
e5f49c3b | 99 | close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0); |
3038e353 KH |
100 | break; |
101 | default: | |
e5f49c3b KH |
102 | /* In this case the ack is really a juju specific |
103 | * rcode, so just forward that to the callback. */ | |
104 | close_transaction(t, card, status, NULL, 0); | |
3038e353 KH |
105 | break; |
106 | } | |
107 | } | |
108 | ||
95688e97 | 109 | static void |
36bfe49d KH |
110 | fw_fill_request(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) | |
3038e353 KH |
113 | { |
114 | int ext_tcode; | |
115 | ||
116 | if (tcode > 0x10) { | |
117 | ext_tcode = tcode - 0x10; | |
118 | tcode = TCODE_LOCK_REQUEST; | |
119 | } else | |
120 | ext_tcode = 0; | |
121 | ||
122 | packet->header[0] = | |
123 | header_retry(RETRY_X) | | |
124 | header_tlabel(tlabel) | | |
125 | header_tcode(tcode) | | |
907293d7 | 126 | header_destination(node_id); |
3038e353 KH |
127 | packet->header[1] = |
128 | header_offset_high(offset >> 32) | header_source(0); | |
129 | packet->header[2] = | |
130 | offset; | |
131 | ||
132 | switch (tcode) { | |
133 | case TCODE_WRITE_QUADLET_REQUEST: | |
134 | packet->header[3] = *(u32 *)payload; | |
135 | packet->header_length = 16; | |
136 | packet->payload_length = 0; | |
137 | break; | |
138 | ||
139 | case TCODE_LOCK_REQUEST: | |
140 | case TCODE_WRITE_BLOCK_REQUEST: | |
141 | packet->header[3] = | |
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; | |
147 | break; | |
148 | ||
149 | case TCODE_READ_QUADLET_REQUEST: | |
150 | packet->header_length = 12; | |
151 | packet->payload_length = 0; | |
152 | break; | |
153 | ||
154 | case TCODE_READ_BLOCK_REQUEST: | |
155 | packet->header[3] = | |
156 | header_data_length(length) | | |
157 | header_extended_tcode(ext_tcode); | |
158 | packet->header_length = 16; | |
159 | packet->payload_length = 0; | |
160 | break; | |
161 | } | |
162 | ||
163 | packet->speed = speed; | |
164 | packet->generation = generation; | |
165 | } | |
166 | ||
167 | /** | |
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. | |
174 | * | |
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. | |
181 | * | |
182 | * Can not expect the callback to be called before the function | |
183 | * returns, though this does happen in some cases (ACK_COMPLETE and | |
184 | * errors). | |
185 | * | |
186 | * The payload is only used for write requests and must not be freed | |
187 | * until the callback has been called. | |
188 | * | |
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. | |
907293d7 | 193 | * @param node_id the destination node ID (bus ID and PHY ID concatenated) |
3038e353 KH |
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 | |
202 | */ | |
203 | void | |
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) | |
209 | { | |
210 | unsigned long flags; | |
211 | int tlabel; | |
212 | ||
213 | /* Bump the flush timer up 100ms first of all so we | |
214 | * don't race with a flush timer callback. */ | |
215 | ||
216 | mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10)); | |
217 | ||
218 | /* Allocate tlabel from the bitmap and put the transaction on | |
219 | * the list while holding the card spinlock. */ | |
220 | ||
221 | spin_lock_irqsave(&card->lock, flags); | |
222 | ||
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); | |
227 | return; | |
228 | } | |
229 | ||
230 | card->current_tlabel = (card->current_tlabel + 1) & 0x1f; | |
231 | card->tlabel_mask |= (1 << tlabel); | |
232 | ||
233 | list_add_tail(&t->link, &card->transaction_list); | |
234 | ||
235 | spin_unlock_irqrestore(&card->lock, flags); | |
236 | ||
237 | /* Initialize rest of transaction, fill out packet and send it. */ | |
238 | t->node_id = node_id; | |
239 | t->tlabel = tlabel; | |
240 | t->callback = callback; | |
241 | t->callback_data = callback_data; | |
242 | ||
36bfe49d KH |
243 | fw_fill_request(&t->packet, tcode, t->tlabel, |
244 | node_id, generation, speed, offset, payload, length); | |
3038e353 KH |
245 | t->packet.callback = transmit_complete_callback; |
246 | ||
247 | card->driver->send_request(card, &t->packet); | |
248 | } | |
249 | EXPORT_SYMBOL(fw_send_request); | |
250 | ||
251 | static void | |
252 | transmit_phy_packet_callback(struct fw_packet *packet, | |
253 | struct fw_card *card, int status) | |
254 | { | |
255 | kfree(packet); | |
256 | } | |
257 | ||
258 | static void send_phy_packet(struct fw_card *card, u32 data, int generation) | |
259 | { | |
260 | struct fw_packet *packet; | |
261 | ||
262 | packet = kzalloc(sizeof *packet, GFP_ATOMIC); | |
263 | if (packet == NULL) | |
264 | return; | |
265 | ||
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; | |
273 | ||
274 | card->driver->send_request(card, packet); | |
275 | } | |
276 | ||
83db801c KH |
277 | void fw_send_phy_config(struct fw_card *card, |
278 | int node_id, int generation, int gap_count) | |
3038e353 KH |
279 | { |
280 | u32 q; | |
281 | ||
83db801c KH |
282 | q = phy_identifier(PHY_PACKET_CONFIG) | |
283 | phy_config_root_id(node_id) | | |
284 | phy_config_gap_count(gap_count); | |
285 | ||
3038e353 KH |
286 | send_phy_packet(card, q, generation); |
287 | } | |
288 | ||
289 | void fw_flush_transactions(struct fw_card *card) | |
290 | { | |
291 | struct fw_transaction *t, *next; | |
292 | struct list_head list; | |
293 | unsigned long flags; | |
294 | ||
295 | INIT_LIST_HEAD(&list); | |
296 | spin_lock_irqsave(&card->lock, flags); | |
297 | list_splice_init(&card->transaction_list, &list); | |
298 | card->tlabel_mask = 0; | |
299 | spin_unlock_irqrestore(&card->lock, flags); | |
300 | ||
301 | list_for_each_entry_safe(t, next, &list, link) | |
302 | t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data); | |
303 | } | |
304 | ||
305 | static struct fw_address_handler * | |
306 | lookup_overlapping_address_handler(struct list_head *list, | |
307 | unsigned long long offset, size_t length) | |
308 | { | |
309 | struct fw_address_handler *handler; | |
310 | ||
311 | list_for_each_entry(handler, list, link) { | |
312 | if (handler->offset < offset + length && | |
313 | offset < handler->offset + handler->length) | |
314 | return handler; | |
315 | } | |
316 | ||
317 | return NULL; | |
318 | } | |
319 | ||
320 | static struct fw_address_handler * | |
321 | lookup_enclosing_address_handler(struct list_head *list, | |
322 | unsigned long long offset, size_t length) | |
323 | { | |
324 | struct fw_address_handler *handler; | |
325 | ||
326 | list_for_each_entry(handler, list, link) { | |
327 | if (handler->offset <= offset && | |
328 | offset + length <= handler->offset + handler->length) | |
329 | return handler; | |
330 | } | |
331 | ||
332 | return NULL; | |
333 | } | |
334 | ||
335 | static DEFINE_SPINLOCK(address_handler_lock); | |
336 | static LIST_HEAD(address_handler_list); | |
337 | ||
21ebcd12 | 338 | const struct fw_address_region fw_low_memory_region = |
5af4e5ea | 339 | { .start = 0x000000000000ULL, .end = 0x000100000000ULL, }; |
21ebcd12 | 340 | const struct fw_address_region fw_high_memory_region = |
5af4e5ea | 341 | { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, }; |
21ebcd12 | 342 | const struct fw_address_region fw_private_region = |
5af4e5ea | 343 | { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, }; |
21ebcd12 | 344 | const struct fw_address_region fw_csr_region = |
5af4e5ea | 345 | { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, }; |
21ebcd12 | 346 | const struct fw_address_region fw_unit_space_region = |
5af4e5ea | 347 | { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, }; |
3038e353 KH |
348 | EXPORT_SYMBOL(fw_low_memory_region); |
349 | EXPORT_SYMBOL(fw_high_memory_region); | |
350 | EXPORT_SYMBOL(fw_private_region); | |
351 | EXPORT_SYMBOL(fw_csr_region); | |
352 | EXPORT_SYMBOL(fw_unit_space_region); | |
353 | ||
354 | /** | |
355 | * Allocate a range of addresses in the node space of the OHCI | |
356 | * controller. When a request is received that falls within the | |
357 | * specified address range, the specified callback is invoked. The | |
358 | * parameters passed to the callback give the details of the | |
359 | * particular request | |
360 | */ | |
3038e353 KH |
361 | int |
362 | fw_core_add_address_handler(struct fw_address_handler *handler, | |
21ebcd12 | 363 | const struct fw_address_region *region) |
3038e353 KH |
364 | { |
365 | struct fw_address_handler *other; | |
366 | unsigned long flags; | |
367 | int ret = -EBUSY; | |
368 | ||
369 | spin_lock_irqsave(&address_handler_lock, flags); | |
370 | ||
371 | handler->offset = region->start; | |
372 | while (handler->offset + handler->length <= region->end) { | |
373 | other = | |
374 | lookup_overlapping_address_handler(&address_handler_list, | |
375 | handler->offset, | |
376 | handler->length); | |
377 | if (other != NULL) { | |
378 | handler->offset += other->length; | |
379 | } else { | |
380 | list_add_tail(&handler->link, &address_handler_list); | |
381 | ret = 0; | |
382 | break; | |
383 | } | |
384 | } | |
385 | ||
386 | spin_unlock_irqrestore(&address_handler_lock, flags); | |
387 | ||
388 | return ret; | |
389 | } | |
3038e353 KH |
390 | EXPORT_SYMBOL(fw_core_add_address_handler); |
391 | ||
392 | /** | |
393 | * Deallocate a range of addresses allocated with fw_allocate. This | |
394 | * will call the associated callback one last time with a the special | |
395 | * tcode TCODE_DEALLOCATE, to let the client destroy the registered | |
396 | * callback data. For convenience, the callback parameters offset and | |
397 | * length are set to the start and the length respectively for the | |
398 | * deallocated region, payload is set to NULL. | |
399 | */ | |
3038e353 KH |
400 | void fw_core_remove_address_handler(struct fw_address_handler *handler) |
401 | { | |
402 | unsigned long flags; | |
403 | ||
404 | spin_lock_irqsave(&address_handler_lock, flags); | |
405 | list_del(&handler->link); | |
406 | spin_unlock_irqrestore(&address_handler_lock, flags); | |
407 | } | |
3038e353 KH |
408 | EXPORT_SYMBOL(fw_core_remove_address_handler); |
409 | ||
410 | struct fw_request { | |
411 | struct fw_packet response; | |
36bfe49d | 412 | u32 request_header[4]; |
3038e353 KH |
413 | int ack; |
414 | u32 length; | |
415 | u32 data[0]; | |
416 | }; | |
417 | ||
418 | static void | |
419 | free_response_callback(struct fw_packet *packet, | |
420 | struct fw_card *card, int status) | |
421 | { | |
422 | struct fw_request *request; | |
423 | ||
424 | request = container_of(packet, struct fw_request, response); | |
425 | kfree(request); | |
426 | } | |
427 | ||
93c4cceb | 428 | void |
36bfe49d KH |
429 | fw_fill_response(struct fw_packet *response, u32 *request_header, |
430 | int rcode, void *payload, size_t length) | |
3038e353 KH |
431 | { |
432 | int tcode, tlabel, extended_tcode, source, destination; | |
433 | ||
36bfe49d KH |
434 | tcode = header_get_tcode(request_header[0]); |
435 | tlabel = header_get_tlabel(request_header[0]); | |
436 | source = header_get_destination(request_header[0]); | |
437 | destination = header_get_source(request_header[1]); | |
438 | extended_tcode = header_get_extended_tcode(request_header[3]); | |
3038e353 KH |
439 | |
440 | response->header[0] = | |
441 | header_retry(RETRY_1) | | |
442 | header_tlabel(tlabel) | | |
443 | header_destination(destination); | |
36bfe49d KH |
444 | response->header[1] = |
445 | header_source(source) | | |
446 | header_rcode(rcode); | |
3038e353 KH |
447 | response->header[2] = 0; |
448 | ||
449 | switch (tcode) { | |
450 | case TCODE_WRITE_QUADLET_REQUEST: | |
451 | case TCODE_WRITE_BLOCK_REQUEST: | |
452 | response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE); | |
453 | response->header_length = 12; | |
454 | response->payload_length = 0; | |
455 | break; | |
456 | ||
457 | case TCODE_READ_QUADLET_REQUEST: | |
458 | response->header[0] |= | |
459 | header_tcode(TCODE_READ_QUADLET_RESPONSE); | |
93c4cceb KH |
460 | if (payload != NULL) |
461 | response->header[3] = *(u32 *)payload; | |
462 | else | |
463 | response->header[3] = 0; | |
3038e353 KH |
464 | response->header_length = 16; |
465 | response->payload_length = 0; | |
466 | break; | |
467 | ||
468 | case TCODE_READ_BLOCK_REQUEST: | |
469 | case TCODE_LOCK_REQUEST: | |
470 | response->header[0] |= header_tcode(tcode + 2); | |
471 | response->header[3] = | |
36bfe49d | 472 | header_data_length(length) | |
3038e353 KH |
473 | header_extended_tcode(extended_tcode); |
474 | response->header_length = 16; | |
36bfe49d KH |
475 | response->payload = payload; |
476 | response->payload_length = length; | |
3038e353 KH |
477 | break; |
478 | ||
479 | default: | |
480 | BUG(); | |
481 | return; | |
482 | } | |
483 | } | |
93c4cceb | 484 | EXPORT_SYMBOL(fw_fill_response); |
3038e353 KH |
485 | |
486 | static struct fw_request * | |
2639a6fb | 487 | allocate_request(struct fw_packet *p) |
3038e353 KH |
488 | { |
489 | struct fw_request *request; | |
490 | u32 *data, length; | |
2639a6fb | 491 | int request_tcode, t; |
3038e353 | 492 | |
2639a6fb | 493 | request_tcode = header_get_tcode(p->header[0]); |
3038e353 KH |
494 | switch (request_tcode) { |
495 | case TCODE_WRITE_QUADLET_REQUEST: | |
2639a6fb | 496 | data = &p->header[3]; |
3038e353 KH |
497 | length = 4; |
498 | break; | |
499 | ||
500 | case TCODE_WRITE_BLOCK_REQUEST: | |
501 | case TCODE_LOCK_REQUEST: | |
2639a6fb KH |
502 | data = p->payload; |
503 | length = header_get_data_length(p->header[3]); | |
3038e353 KH |
504 | break; |
505 | ||
506 | case TCODE_READ_QUADLET_REQUEST: | |
507 | data = NULL; | |
508 | length = 4; | |
509 | break; | |
510 | ||
511 | case TCODE_READ_BLOCK_REQUEST: | |
512 | data = NULL; | |
2639a6fb | 513 | length = header_get_data_length(p->header[3]); |
3038e353 KH |
514 | break; |
515 | ||
516 | default: | |
517 | BUG(); | |
518 | return NULL; | |
519 | } | |
520 | ||
521 | request = kmalloc(sizeof *request + length, GFP_ATOMIC); | |
522 | if (request == NULL) | |
523 | return NULL; | |
524 | ||
2639a6fb KH |
525 | t = (p->timestamp & 0x1fff) + 4000; |
526 | if (t >= 8000) | |
527 | t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000; | |
528 | else | |
529 | t = (p->timestamp & ~0x1fff) + t; | |
530 | ||
531 | request->response.speed = p->speed; | |
532 | request->response.timestamp = t; | |
533 | request->response.generation = p->generation; | |
3038e353 | 534 | request->response.callback = free_response_callback; |
2639a6fb | 535 | request->ack = p->ack; |
93c4cceb | 536 | request->length = length; |
3038e353 | 537 | if (data) |
93c4cceb | 538 | memcpy(request->data, p->payload, length); |
3038e353 | 539 | |
36bfe49d | 540 | memcpy(request->request_header, p->header, sizeof p->header); |
3038e353 KH |
541 | |
542 | return request; | |
543 | } | |
544 | ||
545 | void | |
546 | fw_send_response(struct fw_card *card, struct fw_request *request, int rcode) | |
547 | { | |
3038e353 KH |
548 | /* Broadcast packets are reported as ACK_COMPLETE, so this |
549 | * check is sufficient to ensure we don't send response to | |
550 | * broadcast packets or posted writes. */ | |
551 | if (request->ack != ACK_PENDING) | |
552 | return; | |
553 | ||
36bfe49d KH |
554 | if (rcode == RCODE_COMPLETE) |
555 | fw_fill_response(&request->response, request->request_header, | |
556 | rcode, request->data, request->length); | |
557 | else | |
558 | fw_fill_response(&request->response, request->request_header, | |
559 | rcode, NULL, 0); | |
3038e353 KH |
560 | |
561 | card->driver->send_response(card, &request->response); | |
562 | } | |
3038e353 KH |
563 | EXPORT_SYMBOL(fw_send_response); |
564 | ||
565 | void | |
2639a6fb | 566 | fw_core_handle_request(struct fw_card *card, struct fw_packet *p) |
3038e353 KH |
567 | { |
568 | struct fw_address_handler *handler; | |
569 | struct fw_request *request; | |
570 | unsigned long long offset; | |
571 | unsigned long flags; | |
2639a6fb | 572 | int tcode, destination, source; |
3038e353 | 573 | |
2639a6fb | 574 | if (p->payload_length > 2048) { |
3038e353 KH |
575 | /* FIXME: send error response. */ |
576 | return; | |
577 | } | |
578 | ||
2639a6fb | 579 | if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE) |
3038e353 KH |
580 | return; |
581 | ||
2639a6fb | 582 | request = allocate_request(p); |
3038e353 KH |
583 | if (request == NULL) { |
584 | /* FIXME: send statically allocated busy packet. */ | |
585 | return; | |
586 | } | |
587 | ||
588 | offset = | |
589 | ((unsigned long long) | |
2639a6fb KH |
590 | header_get_offset_high(p->header[1]) << 32) | p->header[2]; |
591 | tcode = header_get_tcode(p->header[0]); | |
592 | destination = header_get_destination(p->header[0]); | |
593 | source = header_get_source(p->header[0]); | |
3038e353 KH |
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, | |
2639a6fb | 611 | p->generation, p->speed, offset, |
3038e353 KH |
612 | request->data, request->length, |
613 | handler->callback_data); | |
614 | } | |
3038e353 KH |
615 | EXPORT_SYMBOL(fw_core_handle_request); |
616 | ||
617 | void | |
2639a6fb | 618 | fw_core_handle_response(struct fw_card *card, struct fw_packet *p) |
3038e353 KH |
619 | { |
620 | struct fw_transaction *t; | |
621 | unsigned long flags; | |
622 | u32 *data; | |
623 | size_t data_length; | |
624 | int tcode, tlabel, destination, source, rcode; | |
625 | ||
2639a6fb KH |
626 | tcode = header_get_tcode(p->header[0]); |
627 | tlabel = header_get_tlabel(p->header[0]); | |
628 | destination = header_get_destination(p->header[0]); | |
629 | source = header_get_source(p->header[1]); | |
630 | rcode = header_get_rcode(p->header[1]); | |
3038e353 KH |
631 | |
632 | spin_lock_irqsave(&card->lock, flags); | |
633 | list_for_each_entry(t, &card->transaction_list, link) { | |
634 | if (t->node_id == source && t->tlabel == tlabel) { | |
635 | list_del(&t->link); | |
636 | card->tlabel_mask &= ~(1 << t->tlabel); | |
637 | break; | |
638 | } | |
639 | } | |
640 | spin_unlock_irqrestore(&card->lock, flags); | |
641 | ||
642 | if (&t->link == &card->transaction_list) { | |
32b46093 KH |
643 | fw_notify("Unsolicited response (source %x, tlabel %x)\n", |
644 | source, tlabel); | |
3038e353 KH |
645 | return; |
646 | } | |
647 | ||
648 | /* FIXME: sanity check packet, is length correct, does tcodes | |
649 | * and addresses match. */ | |
650 | ||
651 | switch (tcode) { | |
652 | case TCODE_READ_QUADLET_RESPONSE: | |
2639a6fb | 653 | data = (u32 *) &p->header[3]; |
3038e353 KH |
654 | data_length = 4; |
655 | break; | |
656 | ||
657 | case TCODE_WRITE_RESPONSE: | |
658 | data = NULL; | |
659 | data_length = 0; | |
660 | break; | |
661 | ||
662 | case TCODE_READ_BLOCK_RESPONSE: | |
663 | case TCODE_LOCK_RESPONSE: | |
93c4cceb | 664 | data = p->payload; |
2639a6fb | 665 | data_length = header_get_data_length(p->header[3]); |
3038e353 KH |
666 | break; |
667 | ||
668 | default: | |
669 | /* Should never happen, this is just to shut up gcc. */ | |
670 | data = NULL; | |
671 | data_length = 0; | |
672 | break; | |
673 | } | |
674 | ||
675 | t->callback(card, rcode, data, data_length, t->callback_data); | |
676 | } | |
3038e353 KH |
677 | EXPORT_SYMBOL(fw_core_handle_response); |
678 | ||
679 | MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); | |
680 | MODULE_DESCRIPTION("Core IEEE1394 transaction logic"); | |
681 | MODULE_LICENSE("GPL"); | |
682 | ||
21ebcd12 | 683 | static const u32 vendor_textual_descriptor_data[] = { |
3038e353 KH |
684 | /* textual descriptor leaf () */ |
685 | 0x00080000, | |
686 | 0x00000000, | |
687 | 0x00000000, | |
688 | 0x4c696e75, /* L i n u */ | |
689 | 0x78204669, /* x F i */ | |
690 | 0x72657769, /* r e w i */ | |
691 | 0x72652028, /* r e ( */ | |
692 | 0x4a554a55, /* J U J U */ | |
693 | 0x29000000, /* ) */ | |
694 | }; | |
695 | ||
696 | static struct fw_descriptor vendor_textual_descriptor = { | |
697 | .length = ARRAY_SIZE(vendor_textual_descriptor_data), | |
698 | .key = 0x81000000, | |
5af4e5ea | 699 | .data = vendor_textual_descriptor_data, |
3038e353 KH |
700 | }; |
701 | ||
3038e353 KH |
702 | static int __init fw_core_init(void) |
703 | { | |
704 | int retval; | |
705 | ||
706 | retval = bus_register(&fw_bus_type); | |
707 | if (retval < 0) | |
708 | return retval; | |
709 | ||
710 | /* Add the vendor textual descriptor. */ | |
711 | retval = fw_core_add_descriptor(&vendor_textual_descriptor); | |
712 | BUG_ON(retval < 0); | |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
717 | static void __exit fw_core_cleanup(void) | |
718 | { | |
719 | bus_unregister(&fw_bus_type); | |
720 | } | |
721 | ||
722 | module_init(fw_core_init); | |
723 | module_exit(fw_core_cleanup); |