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