Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 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" |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 36 | #include "fw-device.h" |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 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) |
Kristian Høgsberg | 9fc8268 | 2007-01-26 00:38:38 -0500 | [diff] [blame] | 51 | #define header_get_rcode(q) (((q) >> 12) & 0x0f) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 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)) |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 59 | #define phy_config_root_id(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23)) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 60 | #define phy_identifier(id) ((id) << 30) |
| 61 | |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 62 | static int |
| 63 | close_transaction(struct fw_transaction *transaction, |
| 64 | struct fw_card *card, int rcode, |
| 65 | u32 *payload, size_t length) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 66 | { |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 67 | struct fw_transaction *t; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 68 | unsigned long flags; |
| 69 | |
| 70 | spin_lock_irqsave(&card->lock, flags); |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 71 | list_for_each_entry(t, &card->transaction_list, link) { |
| 72 | if (t == transaction) { |
| 73 | list_del(&t->link); |
| 74 | card->tlabel_mask &= ~(1 << t->tlabel); |
| 75 | break; |
| 76 | } |
| 77 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 78 | spin_unlock_irqrestore(&card->lock, flags); |
| 79 | |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 80 | if (&t->link != &card->transaction_list) { |
| 81 | t->callback(card, rcode, payload, length, t->callback_data); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | return -ENOENT; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 88 | /* Only valid for transactions that are potentially pending (ie have |
| 89 | * been sent). */ |
| 90 | int |
| 91 | fw_cancel_transaction(struct fw_card *card, |
| 92 | struct fw_transaction *transaction) |
| 93 | { |
| 94 | /* Cancel the packet transmission if it's still queued. That |
| 95 | * will call the packet transmission callback which cancels |
| 96 | * the transaction. */ |
| 97 | |
| 98 | if (card->driver->cancel_packet(card, &transaction->packet) == 0) |
| 99 | return 0; |
| 100 | |
| 101 | /* If the request packet has already been sent, we need to see |
| 102 | * if the transaction is still pending and remove it in that case. */ |
| 103 | |
| 104 | return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0); |
| 105 | } |
| 106 | EXPORT_SYMBOL(fw_cancel_transaction); |
| 107 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 108 | static void |
| 109 | transmit_complete_callback(struct fw_packet *packet, |
| 110 | struct fw_card *card, int status) |
| 111 | { |
| 112 | struct fw_transaction *t = |
| 113 | container_of(packet, struct fw_transaction, packet); |
| 114 | |
| 115 | switch (status) { |
| 116 | case ACK_COMPLETE: |
| 117 | close_transaction(t, card, RCODE_COMPLETE, NULL, 0); |
| 118 | break; |
| 119 | case ACK_PENDING: |
| 120 | t->timestamp = packet->timestamp; |
| 121 | break; |
| 122 | case ACK_BUSY_X: |
| 123 | case ACK_BUSY_A: |
| 124 | case ACK_BUSY_B: |
| 125 | close_transaction(t, card, RCODE_BUSY, NULL, 0); |
| 126 | break; |
| 127 | case ACK_DATA_ERROR: |
Kristian Høgsberg | e5f49c3 | 2007-01-26 00:38:34 -0500 | [diff] [blame] | 128 | close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0); |
| 129 | break; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 130 | case ACK_TYPE_ERROR: |
Kristian Høgsberg | e5f49c3 | 2007-01-26 00:38:34 -0500 | [diff] [blame] | 131 | close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 132 | break; |
| 133 | default: |
Kristian Høgsberg | e5f49c3 | 2007-01-26 00:38:34 -0500 | [diff] [blame] | 134 | /* In this case the ack is really a juju specific |
| 135 | * rcode, so just forward that to the callback. */ |
| 136 | close_transaction(t, card, status, NULL, 0); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
Adrian Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame] | 141 | static void |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 142 | fw_fill_request(struct fw_packet *packet, int tcode, int tlabel, |
| 143 | int node_id, int generation, int speed, |
| 144 | unsigned long long offset, void *payload, size_t length) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 145 | { |
| 146 | int ext_tcode; |
| 147 | |
| 148 | if (tcode > 0x10) { |
| 149 | ext_tcode = tcode - 0x10; |
| 150 | tcode = TCODE_LOCK_REQUEST; |
| 151 | } else |
| 152 | ext_tcode = 0; |
| 153 | |
| 154 | packet->header[0] = |
| 155 | header_retry(RETRY_X) | |
| 156 | header_tlabel(tlabel) | |
| 157 | header_tcode(tcode) | |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 158 | header_destination(node_id); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 159 | packet->header[1] = |
| 160 | header_offset_high(offset >> 32) | header_source(0); |
| 161 | packet->header[2] = |
| 162 | offset; |
| 163 | |
| 164 | switch (tcode) { |
| 165 | case TCODE_WRITE_QUADLET_REQUEST: |
| 166 | packet->header[3] = *(u32 *)payload; |
| 167 | packet->header_length = 16; |
| 168 | packet->payload_length = 0; |
| 169 | break; |
| 170 | |
| 171 | case TCODE_LOCK_REQUEST: |
| 172 | case TCODE_WRITE_BLOCK_REQUEST: |
| 173 | packet->header[3] = |
| 174 | header_data_length(length) | |
| 175 | header_extended_tcode(ext_tcode); |
| 176 | packet->header_length = 16; |
| 177 | packet->payload = payload; |
| 178 | packet->payload_length = length; |
| 179 | break; |
| 180 | |
| 181 | case TCODE_READ_QUADLET_REQUEST: |
| 182 | packet->header_length = 12; |
| 183 | packet->payload_length = 0; |
| 184 | break; |
| 185 | |
| 186 | case TCODE_READ_BLOCK_REQUEST: |
| 187 | packet->header[3] = |
| 188 | header_data_length(length) | |
| 189 | header_extended_tcode(ext_tcode); |
| 190 | packet->header_length = 16; |
| 191 | packet->payload_length = 0; |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | packet->speed = speed; |
| 196 | packet->generation = generation; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 197 | packet->ack = 0; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * This function provides low-level access to the IEEE1394 transaction |
| 202 | * logic. Most C programs would use either fw_read(), fw_write() or |
| 203 | * fw_lock() instead - those function are convenience wrappers for |
| 204 | * this function. The fw_send_request() function is primarily |
| 205 | * provided as a flexible, one-stop entry point for languages bindings |
| 206 | * and protocol bindings. |
| 207 | * |
| 208 | * FIXME: Document this function further, in particular the possible |
| 209 | * values for rcode in the callback. In short, we map ACK_COMPLETE to |
| 210 | * RCODE_COMPLETE, internal errors set errno and set rcode to |
| 211 | * RCODE_SEND_ERROR (which is out of range for standard ieee1394 |
| 212 | * rcodes). All other rcodes are forwarded unchanged. For all |
| 213 | * errors, payload is NULL, length is 0. |
| 214 | * |
| 215 | * Can not expect the callback to be called before the function |
| 216 | * returns, though this does happen in some cases (ACK_COMPLETE and |
| 217 | * errors). |
| 218 | * |
| 219 | * The payload is only used for write requests and must not be freed |
| 220 | * until the callback has been called. |
| 221 | * |
| 222 | * @param card the card from which to send the request |
| 223 | * @param tcode the tcode for this transaction. Do not use |
| 224 | * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP |
| 225 | * etc. to specify tcode and ext_tcode. |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 226 | * @param node_id the destination node ID (bus ID and PHY ID concatenated) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 227 | * @param generation the generation for which node_id is valid |
| 228 | * @param speed the speed to use for sending the request |
| 229 | * @param offset the 48 bit offset on the destination node |
| 230 | * @param payload the data payload for the request subaction |
| 231 | * @param length the length in bytes of the data to read |
| 232 | * @param callback function to be called when the transaction is completed |
| 233 | * @param callback_data pointer to arbitrary data, which will be |
| 234 | * passed to the callback |
| 235 | */ |
| 236 | void |
| 237 | fw_send_request(struct fw_card *card, struct fw_transaction *t, |
| 238 | int tcode, int node_id, int generation, int speed, |
| 239 | unsigned long long offset, |
| 240 | void *payload, size_t length, |
| 241 | fw_transaction_callback_t callback, void *callback_data) |
| 242 | { |
| 243 | unsigned long flags; |
| 244 | int tlabel; |
| 245 | |
| 246 | /* Bump the flush timer up 100ms first of all so we |
| 247 | * don't race with a flush timer callback. */ |
| 248 | |
| 249 | mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10)); |
| 250 | |
| 251 | /* Allocate tlabel from the bitmap and put the transaction on |
| 252 | * the list while holding the card spinlock. */ |
| 253 | |
| 254 | spin_lock_irqsave(&card->lock, flags); |
| 255 | |
| 256 | tlabel = card->current_tlabel; |
| 257 | if (card->tlabel_mask & (1 << tlabel)) { |
| 258 | spin_unlock_irqrestore(&card->lock, flags); |
| 259 | callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | card->current_tlabel = (card->current_tlabel + 1) & 0x1f; |
| 264 | card->tlabel_mask |= (1 << tlabel); |
| 265 | |
| 266 | list_add_tail(&t->link, &card->transaction_list); |
| 267 | |
| 268 | spin_unlock_irqrestore(&card->lock, flags); |
| 269 | |
| 270 | /* Initialize rest of transaction, fill out packet and send it. */ |
| 271 | t->node_id = node_id; |
| 272 | t->tlabel = tlabel; |
| 273 | t->callback = callback; |
| 274 | t->callback_data = callback_data; |
| 275 | |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 276 | fw_fill_request(&t->packet, tcode, t->tlabel, |
| 277 | node_id, generation, speed, offset, payload, length); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 278 | t->packet.callback = transmit_complete_callback; |
| 279 | |
| 280 | card->driver->send_request(card, &t->packet); |
| 281 | } |
| 282 | EXPORT_SYMBOL(fw_send_request); |
| 283 | |
| 284 | static void |
| 285 | transmit_phy_packet_callback(struct fw_packet *packet, |
| 286 | struct fw_card *card, int status) |
| 287 | { |
| 288 | kfree(packet); |
| 289 | } |
| 290 | |
| 291 | static void send_phy_packet(struct fw_card *card, u32 data, int generation) |
| 292 | { |
| 293 | struct fw_packet *packet; |
| 294 | |
| 295 | packet = kzalloc(sizeof *packet, GFP_ATOMIC); |
| 296 | if (packet == NULL) |
| 297 | return; |
| 298 | |
| 299 | packet->header[0] = data; |
| 300 | packet->header[1] = ~data; |
| 301 | packet->header_length = 8; |
| 302 | packet->payload_length = 0; |
| 303 | packet->speed = SCODE_100; |
| 304 | packet->generation = generation; |
| 305 | packet->callback = transmit_phy_packet_callback; |
| 306 | |
| 307 | card->driver->send_request(card, packet); |
| 308 | } |
| 309 | |
Kristian Høgsberg | 83db801 | 2007-01-26 00:37:50 -0500 | [diff] [blame] | 310 | void fw_send_phy_config(struct fw_card *card, |
| 311 | int node_id, int generation, int gap_count) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 312 | { |
| 313 | u32 q; |
| 314 | |
Kristian Høgsberg | 83db801 | 2007-01-26 00:37:50 -0500 | [diff] [blame] | 315 | q = phy_identifier(PHY_PACKET_CONFIG) | |
| 316 | phy_config_root_id(node_id) | |
| 317 | phy_config_gap_count(gap_count); |
| 318 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 319 | send_phy_packet(card, q, generation); |
| 320 | } |
| 321 | |
| 322 | void fw_flush_transactions(struct fw_card *card) |
| 323 | { |
| 324 | struct fw_transaction *t, *next; |
| 325 | struct list_head list; |
| 326 | unsigned long flags; |
| 327 | |
| 328 | INIT_LIST_HEAD(&list); |
| 329 | spin_lock_irqsave(&card->lock, flags); |
| 330 | list_splice_init(&card->transaction_list, &list); |
| 331 | card->tlabel_mask = 0; |
| 332 | spin_unlock_irqrestore(&card->lock, flags); |
| 333 | |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 334 | list_for_each_entry_safe(t, next, &list, link) { |
| 335 | card->driver->cancel_packet(card, &t->packet); |
| 336 | |
| 337 | /* At this point cancel_packet will never call the |
| 338 | * transaction callback, since we just took all the |
| 339 | * transactions out of the list. So do it here.*/ |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 340 | t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data); |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 341 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static struct fw_address_handler * |
| 345 | lookup_overlapping_address_handler(struct list_head *list, |
| 346 | unsigned long long offset, size_t length) |
| 347 | { |
| 348 | struct fw_address_handler *handler; |
| 349 | |
| 350 | list_for_each_entry(handler, list, link) { |
| 351 | if (handler->offset < offset + length && |
| 352 | offset < handler->offset + handler->length) |
| 353 | return handler; |
| 354 | } |
| 355 | |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | static struct fw_address_handler * |
| 360 | lookup_enclosing_address_handler(struct list_head *list, |
| 361 | unsigned long long offset, size_t length) |
| 362 | { |
| 363 | struct fw_address_handler *handler; |
| 364 | |
| 365 | list_for_each_entry(handler, list, link) { |
| 366 | if (handler->offset <= offset && |
| 367 | offset + length <= handler->offset + handler->length) |
| 368 | return handler; |
| 369 | } |
| 370 | |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | static DEFINE_SPINLOCK(address_handler_lock); |
| 375 | static LIST_HEAD(address_handler_list); |
| 376 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 377 | const struct fw_address_region fw_low_memory_region = |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 378 | { .start = 0x000000000000ULL, .end = 0x000100000000ULL, }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 379 | const struct fw_address_region fw_high_memory_region = |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 380 | { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 381 | const struct fw_address_region fw_private_region = |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 382 | { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 383 | const struct fw_address_region fw_csr_region = |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 384 | { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 385 | const struct fw_address_region fw_unit_space_region = |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 386 | { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, }; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 387 | EXPORT_SYMBOL(fw_low_memory_region); |
| 388 | EXPORT_SYMBOL(fw_high_memory_region); |
| 389 | EXPORT_SYMBOL(fw_private_region); |
| 390 | EXPORT_SYMBOL(fw_csr_region); |
| 391 | EXPORT_SYMBOL(fw_unit_space_region); |
| 392 | |
| 393 | /** |
| 394 | * Allocate a range of addresses in the node space of the OHCI |
| 395 | * controller. When a request is received that falls within the |
| 396 | * specified address range, the specified callback is invoked. The |
| 397 | * parameters passed to the callback give the details of the |
| 398 | * particular request |
| 399 | */ |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 400 | int |
| 401 | fw_core_add_address_handler(struct fw_address_handler *handler, |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 402 | const struct fw_address_region *region) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 403 | { |
| 404 | struct fw_address_handler *other; |
| 405 | unsigned long flags; |
| 406 | int ret = -EBUSY; |
| 407 | |
| 408 | spin_lock_irqsave(&address_handler_lock, flags); |
| 409 | |
| 410 | handler->offset = region->start; |
| 411 | while (handler->offset + handler->length <= region->end) { |
| 412 | other = |
| 413 | lookup_overlapping_address_handler(&address_handler_list, |
| 414 | handler->offset, |
| 415 | handler->length); |
| 416 | if (other != NULL) { |
| 417 | handler->offset += other->length; |
| 418 | } else { |
| 419 | list_add_tail(&handler->link, &address_handler_list); |
| 420 | ret = 0; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 426 | |
| 427 | return ret; |
| 428 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 429 | EXPORT_SYMBOL(fw_core_add_address_handler); |
| 430 | |
| 431 | /** |
| 432 | * Deallocate a range of addresses allocated with fw_allocate. This |
| 433 | * will call the associated callback one last time with a the special |
| 434 | * tcode TCODE_DEALLOCATE, to let the client destroy the registered |
| 435 | * callback data. For convenience, the callback parameters offset and |
| 436 | * length are set to the start and the length respectively for the |
| 437 | * deallocated region, payload is set to NULL. |
| 438 | */ |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 439 | void fw_core_remove_address_handler(struct fw_address_handler *handler) |
| 440 | { |
| 441 | unsigned long flags; |
| 442 | |
| 443 | spin_lock_irqsave(&address_handler_lock, flags); |
| 444 | list_del(&handler->link); |
| 445 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 446 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 447 | EXPORT_SYMBOL(fw_core_remove_address_handler); |
| 448 | |
| 449 | struct fw_request { |
| 450 | struct fw_packet response; |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 451 | u32 request_header[4]; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 452 | int ack; |
| 453 | u32 length; |
| 454 | u32 data[0]; |
| 455 | }; |
| 456 | |
| 457 | static void |
| 458 | free_response_callback(struct fw_packet *packet, |
| 459 | struct fw_card *card, int status) |
| 460 | { |
| 461 | struct fw_request *request; |
| 462 | |
| 463 | request = container_of(packet, struct fw_request, response); |
| 464 | kfree(request); |
| 465 | } |
| 466 | |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 467 | void |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 468 | fw_fill_response(struct fw_packet *response, u32 *request_header, |
| 469 | int rcode, void *payload, size_t length) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 470 | { |
| 471 | int tcode, tlabel, extended_tcode, source, destination; |
| 472 | |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 473 | tcode = header_get_tcode(request_header[0]); |
| 474 | tlabel = header_get_tlabel(request_header[0]); |
| 475 | source = header_get_destination(request_header[0]); |
| 476 | destination = header_get_source(request_header[1]); |
| 477 | extended_tcode = header_get_extended_tcode(request_header[3]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 478 | |
| 479 | response->header[0] = |
| 480 | header_retry(RETRY_1) | |
| 481 | header_tlabel(tlabel) | |
| 482 | header_destination(destination); |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 483 | response->header[1] = |
| 484 | header_source(source) | |
| 485 | header_rcode(rcode); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 486 | response->header[2] = 0; |
| 487 | |
| 488 | switch (tcode) { |
| 489 | case TCODE_WRITE_QUADLET_REQUEST: |
| 490 | case TCODE_WRITE_BLOCK_REQUEST: |
| 491 | response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE); |
| 492 | response->header_length = 12; |
| 493 | response->payload_length = 0; |
| 494 | break; |
| 495 | |
| 496 | case TCODE_READ_QUADLET_REQUEST: |
| 497 | response->header[0] |= |
| 498 | header_tcode(TCODE_READ_QUADLET_RESPONSE); |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 499 | if (payload != NULL) |
| 500 | response->header[3] = *(u32 *)payload; |
| 501 | else |
| 502 | response->header[3] = 0; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 503 | response->header_length = 16; |
| 504 | response->payload_length = 0; |
| 505 | break; |
| 506 | |
| 507 | case TCODE_READ_BLOCK_REQUEST: |
| 508 | case TCODE_LOCK_REQUEST: |
| 509 | response->header[0] |= header_tcode(tcode + 2); |
| 510 | response->header[3] = |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 511 | header_data_length(length) | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 512 | header_extended_tcode(extended_tcode); |
| 513 | response->header_length = 16; |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 514 | response->payload = payload; |
| 515 | response->payload_length = length; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | default: |
| 519 | BUG(); |
| 520 | return; |
| 521 | } |
| 522 | } |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 523 | EXPORT_SYMBOL(fw_fill_response); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 524 | |
| 525 | static struct fw_request * |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 526 | allocate_request(struct fw_packet *p) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 527 | { |
| 528 | struct fw_request *request; |
| 529 | u32 *data, length; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 530 | int request_tcode, t; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 531 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 532 | request_tcode = header_get_tcode(p->header[0]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 533 | switch (request_tcode) { |
| 534 | case TCODE_WRITE_QUADLET_REQUEST: |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 535 | data = &p->header[3]; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 536 | length = 4; |
| 537 | break; |
| 538 | |
| 539 | case TCODE_WRITE_BLOCK_REQUEST: |
| 540 | case TCODE_LOCK_REQUEST: |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 541 | data = p->payload; |
| 542 | length = header_get_data_length(p->header[3]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 543 | break; |
| 544 | |
| 545 | case TCODE_READ_QUADLET_REQUEST: |
| 546 | data = NULL; |
| 547 | length = 4; |
| 548 | break; |
| 549 | |
| 550 | case TCODE_READ_BLOCK_REQUEST: |
| 551 | data = NULL; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 552 | length = header_get_data_length(p->header[3]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 553 | break; |
| 554 | |
| 555 | default: |
| 556 | BUG(); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | request = kmalloc(sizeof *request + length, GFP_ATOMIC); |
| 561 | if (request == NULL) |
| 562 | return NULL; |
| 563 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 564 | t = (p->timestamp & 0x1fff) + 4000; |
| 565 | if (t >= 8000) |
| 566 | t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000; |
| 567 | else |
| 568 | t = (p->timestamp & ~0x1fff) + t; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 569 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 570 | request->response.speed = p->speed; |
| 571 | request->response.timestamp = t; |
| 572 | request->response.generation = p->generation; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame^] | 573 | request->response.ack = 0; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 574 | request->response.callback = free_response_callback; |
| 575 | request->ack = p->ack; |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 576 | request->length = length; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 577 | if (data) |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 578 | memcpy(request->data, p->payload, length); |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 579 | |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 580 | memcpy(request->request_header, p->header, sizeof p->header); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 581 | |
| 582 | return request; |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | fw_send_response(struct fw_card *card, struct fw_request *request, int rcode) |
| 587 | { |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 588 | /* Broadcast packets are reported as ACK_COMPLETE, so this |
| 589 | * check is sufficient to ensure we don't send response to |
| 590 | * broadcast packets or posted writes. */ |
| 591 | if (request->ack != ACK_PENDING) |
| 592 | return; |
| 593 | |
Kristian Høgsberg | 36bfe49 | 2007-01-26 00:38:13 -0500 | [diff] [blame] | 594 | if (rcode == RCODE_COMPLETE) |
| 595 | fw_fill_response(&request->response, request->request_header, |
| 596 | rcode, request->data, request->length); |
| 597 | else |
| 598 | fw_fill_response(&request->response, request->request_header, |
| 599 | rcode, NULL, 0); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 600 | |
| 601 | card->driver->send_response(card, &request->response); |
| 602 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 603 | EXPORT_SYMBOL(fw_send_response); |
| 604 | |
| 605 | void |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 606 | fw_core_handle_request(struct fw_card *card, struct fw_packet *p) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 607 | { |
| 608 | struct fw_address_handler *handler; |
| 609 | struct fw_request *request; |
| 610 | unsigned long long offset; |
| 611 | unsigned long flags; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 612 | int tcode, destination, source; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 613 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 614 | if (p->payload_length > 2048) { |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 615 | /* FIXME: send error response. */ |
| 616 | return; |
| 617 | } |
| 618 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 619 | if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 620 | return; |
| 621 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 622 | request = allocate_request(p); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 623 | if (request == NULL) { |
| 624 | /* FIXME: send statically allocated busy packet. */ |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | offset = |
| 629 | ((unsigned long long) |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 630 | header_get_offset_high(p->header[1]) << 32) | p->header[2]; |
| 631 | tcode = header_get_tcode(p->header[0]); |
| 632 | destination = header_get_destination(p->header[0]); |
| 633 | source = header_get_source(p->header[0]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 634 | |
| 635 | spin_lock_irqsave(&address_handler_lock, flags); |
| 636 | handler = lookup_enclosing_address_handler(&address_handler_list, |
| 637 | offset, request->length); |
| 638 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 639 | |
| 640 | /* FIXME: lookup the fw_node corresponding to the sender of |
| 641 | * this request and pass that to the address handler instead |
| 642 | * of the node ID. We may also want to move the address |
| 643 | * allocations to fw_node so we only do this callback if the |
| 644 | * upper layers registered it for this node. */ |
| 645 | |
| 646 | if (handler == NULL) |
| 647 | fw_send_response(card, request, RCODE_ADDRESS_ERROR); |
| 648 | else |
| 649 | handler->address_callback(card, request, |
| 650 | tcode, destination, source, |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 651 | p->generation, p->speed, offset, |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 652 | request->data, request->length, |
| 653 | handler->callback_data); |
| 654 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 655 | EXPORT_SYMBOL(fw_core_handle_request); |
| 656 | |
| 657 | void |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 658 | fw_core_handle_response(struct fw_card *card, struct fw_packet *p) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 659 | { |
| 660 | struct fw_transaction *t; |
| 661 | unsigned long flags; |
| 662 | u32 *data; |
| 663 | size_t data_length; |
| 664 | int tcode, tlabel, destination, source, rcode; |
| 665 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 666 | tcode = header_get_tcode(p->header[0]); |
| 667 | tlabel = header_get_tlabel(p->header[0]); |
| 668 | destination = header_get_destination(p->header[0]); |
| 669 | source = header_get_source(p->header[1]); |
| 670 | rcode = header_get_rcode(p->header[1]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 671 | |
| 672 | spin_lock_irqsave(&card->lock, flags); |
| 673 | list_for_each_entry(t, &card->transaction_list, link) { |
| 674 | if (t->node_id == source && t->tlabel == tlabel) { |
| 675 | list_del(&t->link); |
| 676 | card->tlabel_mask &= ~(1 << t->tlabel); |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | spin_unlock_irqrestore(&card->lock, flags); |
| 681 | |
| 682 | if (&t->link == &card->transaction_list) { |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 683 | fw_notify("Unsolicited response (source %x, tlabel %x)\n", |
| 684 | source, tlabel); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | |
| 688 | /* FIXME: sanity check packet, is length correct, does tcodes |
| 689 | * and addresses match. */ |
| 690 | |
| 691 | switch (tcode) { |
| 692 | case TCODE_READ_QUADLET_RESPONSE: |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 693 | data = (u32 *) &p->header[3]; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 694 | data_length = 4; |
| 695 | break; |
| 696 | |
| 697 | case TCODE_WRITE_RESPONSE: |
| 698 | data = NULL; |
| 699 | data_length = 0; |
| 700 | break; |
| 701 | |
| 702 | case TCODE_READ_BLOCK_RESPONSE: |
| 703 | case TCODE_LOCK_RESPONSE: |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 704 | data = p->payload; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 705 | data_length = header_get_data_length(p->header[3]); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 706 | break; |
| 707 | |
| 708 | default: |
| 709 | /* Should never happen, this is just to shut up gcc. */ |
| 710 | data = NULL; |
| 711 | data_length = 0; |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | t->callback(card, rcode, data, data_length, t->callback_data); |
| 716 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 717 | EXPORT_SYMBOL(fw_core_handle_response); |
| 718 | |
| 719 | MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); |
| 720 | MODULE_DESCRIPTION("Core IEEE1394 transaction logic"); |
| 721 | MODULE_LICENSE("GPL"); |
| 722 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 723 | static const u32 vendor_textual_descriptor_data[] = { |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 724 | /* textual descriptor leaf () */ |
| 725 | 0x00080000, |
| 726 | 0x00000000, |
| 727 | 0x00000000, |
| 728 | 0x4c696e75, /* L i n u */ |
| 729 | 0x78204669, /* x F i */ |
| 730 | 0x72657769, /* r e w i */ |
| 731 | 0x72652028, /* r e ( */ |
| 732 | 0x4a554a55, /* J U J U */ |
| 733 | 0x29000000, /* ) */ |
| 734 | }; |
| 735 | |
| 736 | static struct fw_descriptor vendor_textual_descriptor = { |
| 737 | .length = ARRAY_SIZE(vendor_textual_descriptor_data), |
| 738 | .key = 0x81000000, |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 739 | .data = vendor_textual_descriptor_data, |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 740 | }; |
| 741 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 742 | static int __init fw_core_init(void) |
| 743 | { |
| 744 | int retval; |
| 745 | |
| 746 | retval = bus_register(&fw_bus_type); |
| 747 | if (retval < 0) |
| 748 | return retval; |
| 749 | |
| 750 | /* Add the vendor textual descriptor. */ |
| 751 | retval = fw_core_add_descriptor(&vendor_textual_descriptor); |
| 752 | BUG_ON(retval < 0); |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static void __exit fw_core_cleanup(void) |
| 758 | { |
| 759 | bus_unregister(&fw_bus_type); |
| 760 | } |
| 761 | |
| 762 | module_init(fw_core_init); |
| 763 | module_exit(fw_core_cleanup); |