1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Core IEEE1394 transaction logic 4 * 5 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net> 6 */ 7 8 #include <linux/bug.h> 9 #include <linux/completion.h> 10 #include <linux/device.h> 11 #include <linux/errno.h> 12 #include <linux/firewire.h> 13 #include <linux/firewire-constants.h> 14 #include <linux/fs.h> 15 #include <linux/init.h> 16 #include <linux/jiffies.h> 17 #include <linux/kernel.h> 18 #include <linux/list.h> 19 #include <linux/module.h> 20 #include <linux/rculist.h> 21 #include <linux/slab.h> 22 #include <linux/spinlock.h> 23 #include <linux/string.h> 24 #include <linux/timer.h> 25 #include <linux/types.h> 26 #include <linux/workqueue.h> 27 28 #include <asm/byteorder.h> 29 30 #include "core.h" 31 #include "packet-header-definitions.h" 32 #include "phy-packet-definitions.h" 33 #include <trace/events/firewire.h> 34 35 #define HEADER_DESTINATION_IS_BROADCAST(header) \ 36 ((async_header_get_destination(header) & 0x3f) == 0x3f) 37 38 /* returns 0 if the split timeout handler is already running */ 39 static int try_cancel_split_timeout(struct fw_transaction *t) 40 { 41 if (t->is_split_transaction) 42 return timer_delete(&t->split_timeout_timer); 43 else 44 return 1; 45 } 46 47 static int close_transaction(struct fw_transaction *transaction, struct fw_card *card, int rcode, 48 u32 response_tstamp) 49 { 50 struct fw_transaction *t = NULL, *iter; 51 52 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 53 // local destination never runs in any type of IRQ context. 54 scoped_guard(spinlock_irqsave, &card->transactions.lock) { 55 list_for_each_entry(iter, &card->transactions.list, link) { 56 if (iter == transaction) { 57 if (try_cancel_split_timeout(iter)) { 58 list_del_init(&iter->link); 59 card->transactions.tlabel_mask &= ~(1ULL << iter->tlabel); 60 t = iter; 61 } 62 break; 63 } 64 } 65 } 66 67 if (!t) 68 return -ENOENT; 69 70 if (!t->with_tstamp) { 71 t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data); 72 } else { 73 t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp, NULL, 0, 74 t->callback_data); 75 } 76 77 return 0; 78 } 79 80 /* 81 * Only valid for transactions that are potentially pending (ie have 82 * been sent). 83 */ 84 int fw_cancel_transaction(struct fw_card *card, 85 struct fw_transaction *transaction) 86 { 87 u32 tstamp; 88 89 /* 90 * Cancel the packet transmission if it's still queued. That 91 * will call the packet transmission callback which cancels 92 * the transaction. 93 */ 94 95 if (card->driver->cancel_packet(card, &transaction->packet) == 0) 96 return 0; 97 98 /* 99 * If the request packet has already been sent, we need to see 100 * if the transaction is still pending and remove it in that case. 101 */ 102 103 if (transaction->packet.ack == 0) { 104 // The timestamp is reused since it was just read now. 105 tstamp = transaction->packet.timestamp; 106 } else { 107 u32 curr_cycle_time = 0; 108 109 (void)fw_card_read_cycle_time(card, &curr_cycle_time); 110 tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time); 111 } 112 113 return close_transaction(transaction, card, RCODE_CANCELLED, tstamp); 114 } 115 EXPORT_SYMBOL(fw_cancel_transaction); 116 117 static void split_transaction_timeout_callback(struct timer_list *timer) 118 { 119 struct fw_transaction *t = timer_container_of(t, timer, split_timeout_timer); 120 struct fw_card *card = t->card; 121 122 scoped_guard(spinlock_irqsave, &card->transactions.lock) { 123 if (list_empty(&t->link)) 124 return; 125 list_del(&t->link); 126 card->transactions.tlabel_mask &= ~(1ULL << t->tlabel); 127 } 128 129 if (!t->with_tstamp) { 130 t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0, t->callback_data); 131 } else { 132 t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp, 133 t->split_timeout_cycle, NULL, 0, t->callback_data); 134 } 135 } 136 137 static void start_split_transaction_timeout(struct fw_transaction *t, 138 struct fw_card *card) 139 { 140 unsigned long delta; 141 142 if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) 143 return; 144 145 t->is_split_transaction = true; 146 147 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 148 // local destination never runs in any type of IRQ context. 149 scoped_guard(spinlock_irqsave, &card->split_timeout.lock) 150 delta = card->split_timeout.jiffies; 151 mod_timer(&t->split_timeout_timer, jiffies + delta); 152 } 153 154 static u32 compute_split_timeout_timestamp(struct fw_card *card, u32 request_timestamp); 155 156 static void transmit_complete_callback(struct fw_packet *packet, 157 struct fw_card *card, int status) 158 { 159 struct fw_transaction *t = 160 container_of(packet, struct fw_transaction, packet); 161 162 trace_async_request_outbound_complete((uintptr_t)t, card->index, packet->generation, 163 packet->speed, status, packet->timestamp); 164 165 switch (status) { 166 case ACK_COMPLETE: 167 close_transaction(t, card, RCODE_COMPLETE, packet->timestamp); 168 break; 169 case ACK_PENDING: 170 { 171 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 172 // local destination never runs in any type of IRQ context. 173 scoped_guard(spinlock_irqsave, &card->split_timeout.lock) { 174 t->split_timeout_cycle = 175 compute_split_timeout_timestamp(card, packet->timestamp) & 0xffff; 176 } 177 start_split_transaction_timeout(t, card); 178 break; 179 } 180 case ACK_BUSY_X: 181 case ACK_BUSY_A: 182 case ACK_BUSY_B: 183 close_transaction(t, card, RCODE_BUSY, packet->timestamp); 184 break; 185 case ACK_DATA_ERROR: 186 close_transaction(t, card, RCODE_DATA_ERROR, packet->timestamp); 187 break; 188 case ACK_TYPE_ERROR: 189 close_transaction(t, card, RCODE_TYPE_ERROR, packet->timestamp); 190 break; 191 default: 192 /* 193 * In this case the ack is really a juju specific 194 * rcode, so just forward that to the callback. 195 */ 196 close_transaction(t, card, status, packet->timestamp); 197 break; 198 } 199 } 200 201 static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel, 202 int destination_id, int source_id, int generation, int speed, 203 unsigned long long offset, void *payload, size_t length) 204 { 205 int ext_tcode; 206 207 if (tcode == TCODE_STREAM_DATA) { 208 // The value of destination_id argument should include tag, channel, and sy fields 209 // as isochronous packet header has. 210 packet->header[0] = destination_id; 211 isoc_header_set_data_length(packet->header, length); 212 isoc_header_set_tcode(packet->header, TCODE_STREAM_DATA); 213 packet->header_length = 4; 214 packet->payload = payload; 215 packet->payload_length = length; 216 217 goto common; 218 } 219 220 if (tcode > 0x10) { 221 ext_tcode = tcode & ~0x10; 222 tcode = TCODE_LOCK_REQUEST; 223 } else 224 ext_tcode = 0; 225 226 async_header_set_retry(packet->header, RETRY_X); 227 async_header_set_tlabel(packet->header, tlabel); 228 async_header_set_tcode(packet->header, tcode); 229 async_header_set_destination(packet->header, destination_id); 230 async_header_set_source(packet->header, source_id); 231 async_header_set_offset(packet->header, offset); 232 233 switch (tcode) { 234 case TCODE_WRITE_QUADLET_REQUEST: 235 async_header_set_quadlet_data(packet->header, *(u32 *)payload); 236 packet->header_length = 16; 237 packet->payload_length = 0; 238 break; 239 240 case TCODE_LOCK_REQUEST: 241 case TCODE_WRITE_BLOCK_REQUEST: 242 async_header_set_data_length(packet->header, length); 243 async_header_set_extended_tcode(packet->header, ext_tcode); 244 packet->header_length = 16; 245 packet->payload = payload; 246 packet->payload_length = length; 247 break; 248 249 case TCODE_READ_QUADLET_REQUEST: 250 packet->header_length = 12; 251 packet->payload_length = 0; 252 break; 253 254 case TCODE_READ_BLOCK_REQUEST: 255 async_header_set_data_length(packet->header, length); 256 async_header_set_extended_tcode(packet->header, ext_tcode); 257 packet->header_length = 16; 258 packet->payload_length = 0; 259 break; 260 261 default: 262 WARN(1, "wrong tcode %d\n", tcode); 263 } 264 common: 265 packet->speed = speed; 266 packet->generation = generation; 267 packet->ack = 0; 268 packet->payload_mapped = false; 269 } 270 271 static int allocate_tlabel(struct fw_card *card) 272 __must_hold(&card->transactions_lock) 273 { 274 int tlabel; 275 276 lockdep_assert_held(&card->transactions.lock); 277 278 tlabel = card->transactions.current_tlabel; 279 while (card->transactions.tlabel_mask & (1ULL << tlabel)) { 280 tlabel = (tlabel + 1) & 0x3f; 281 if (tlabel == card->transactions.current_tlabel) 282 return -EBUSY; 283 } 284 285 card->transactions.current_tlabel = (tlabel + 1) & 0x3f; 286 card->transactions.tlabel_mask |= 1ULL << tlabel; 287 288 return tlabel; 289 } 290 291 /** 292 * __fw_send_request() - submit a request packet for transmission to generate callback for response 293 * subaction with or without time stamp. 294 * @card: interface to send the request at 295 * @t: transaction instance to which the request belongs 296 * @tcode: transaction code 297 * @destination_id: destination node ID, consisting of bus_ID and phy_ID 298 * @generation: bus generation in which request and response are valid 299 * @speed: transmission speed 300 * @offset: 48bit wide offset into destination's address space 301 * @payload: data payload for the request subaction 302 * @length: length of the payload, in bytes 303 * @callback: union of two functions whether to receive time stamp or not for response 304 * subaction. 305 * @with_tstamp: Whether to receive time stamp or not for response subaction. 306 * @callback_data: data to be passed to the transaction completion callback 307 * 308 * Submit a request packet into the asynchronous request transmission queue. 309 * Can be called from atomic context. If you prefer a blocking API, use 310 * fw_run_transaction() in a context that can sleep. 311 * 312 * In case of lock requests, specify one of the firewire-core specific %TCODE_ 313 * constants instead of %TCODE_LOCK_REQUEST in @tcode. 314 * 315 * Make sure that the value in @destination_id is not older than the one in 316 * @generation. Otherwise the request is in danger to be sent to a wrong node. 317 * 318 * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller 319 * needs to synthesize @destination_id with fw_stream_packet_destination_id(). 320 * It will contain tag, channel, and sy data instead of a node ID then. 321 * 322 * The payload buffer at @data is going to be DMA-mapped except in case of 323 * @length <= 8 or of local (loopback) requests. Hence make sure that the 324 * buffer complies with the restrictions of the streaming DMA mapping API. 325 * @payload must not be freed before the @callback is called. 326 * 327 * In case of request types without payload, @data is NULL and @length is 0. 328 * 329 * After the transaction is completed successfully or unsuccessfully, the 330 * @callback will be called. Among its parameters is the response code which 331 * is either one of the rcodes per IEEE 1394 or, in case of internal errors, 332 * the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core 333 * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION, 334 * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request 335 * generation, or missing ACK respectively. 336 * 337 * Note some timing corner cases: fw_send_request() may complete much earlier 338 * than when the request packet actually hits the wire. On the other hand, 339 * transaction completion and hence execution of @callback may happen even 340 * before fw_send_request() returns. 341 */ 342 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode, 343 int destination_id, int generation, int speed, unsigned long long offset, 344 void *payload, size_t length, union fw_transaction_callback callback, 345 bool with_tstamp, void *callback_data) 346 { 347 int tlabel; 348 349 /* 350 * Allocate tlabel from the bitmap and put the transaction on 351 * the list while holding the card spinlock. 352 */ 353 354 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 355 // local destination never runs in any type of IRQ context. 356 scoped_guard(spinlock_irqsave, &card->transactions.lock) 357 tlabel = allocate_tlabel(card); 358 if (tlabel < 0) { 359 if (!with_tstamp) { 360 callback.without_tstamp(card, RCODE_SEND_ERROR, NULL, 0, callback_data); 361 } else { 362 // Timestamping on behalf of hardware. 363 u32 curr_cycle_time = 0; 364 u32 tstamp; 365 366 (void)fw_card_read_cycle_time(card, &curr_cycle_time); 367 tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time); 368 369 callback.with_tstamp(card, RCODE_SEND_ERROR, tstamp, tstamp, NULL, 0, 370 callback_data); 371 } 372 return; 373 } 374 375 t->node_id = destination_id; 376 t->tlabel = tlabel; 377 t->card = card; 378 t->is_split_transaction = false; 379 timer_setup(&t->split_timeout_timer, split_transaction_timeout_callback, 0); 380 t->callback = callback; 381 t->with_tstamp = with_tstamp; 382 t->callback_data = callback_data; 383 t->packet.callback = transmit_complete_callback; 384 385 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 386 // local destination never runs in any type of IRQ context. 387 scoped_guard(spinlock_irqsave, &card->lock) { 388 // The node_id field of fw_card can be updated when handling SelfIDComplete. 389 fw_fill_request(&t->packet, tcode, t->tlabel, destination_id, card->node_id, 390 generation, speed, offset, payload, length); 391 } 392 393 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 394 // local destination never runs in any type of IRQ context. 395 scoped_guard(spinlock_irqsave, &card->transactions.lock) 396 list_add_tail(&t->link, &card->transactions.list); 397 398 // Safe with no lock, since the index field of fw_card is immutable once assigned. 399 trace_async_request_outbound_initiate((uintptr_t)t, card->index, generation, speed, 400 t->packet.header, payload, 401 tcode_is_read_request(tcode) ? 0 : length / 4); 402 403 card->driver->send_request(card, &t->packet); 404 } 405 EXPORT_SYMBOL_GPL(__fw_send_request); 406 407 struct transaction_callback_data { 408 struct completion done; 409 void *payload; 410 int rcode; 411 }; 412 413 static void transaction_callback(struct fw_card *card, int rcode, 414 void *payload, size_t length, void *data) 415 { 416 struct transaction_callback_data *d = data; 417 418 if (rcode == RCODE_COMPLETE) 419 memcpy(d->payload, payload, length); 420 d->rcode = rcode; 421 complete(&d->done); 422 } 423 424 /** 425 * fw_run_transaction() - send request and sleep until transaction is completed 426 * @card: card interface for this request 427 * @tcode: transaction code 428 * @destination_id: destination node ID, consisting of bus_ID and phy_ID 429 * @generation: bus generation in which request and response are valid 430 * @speed: transmission speed 431 * @offset: 48bit wide offset into destination's address space 432 * @payload: data payload for the request subaction 433 * @length: length of the payload, in bytes 434 * 435 * Returns the RCODE. See fw_send_request() for parameter documentation. 436 * Unlike fw_send_request(), @data points to the payload of the request or/and 437 * to the payload of the response. DMA mapping restrictions apply to outbound 438 * request payloads of >= 8 bytes but not to inbound response payloads. 439 */ 440 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id, 441 int generation, int speed, unsigned long long offset, 442 void *payload, size_t length) 443 { 444 struct transaction_callback_data d; 445 struct fw_transaction t; 446 447 timer_setup_on_stack(&t.split_timeout_timer, NULL, 0); 448 init_completion(&d.done); 449 d.payload = payload; 450 fw_send_request(card, &t, tcode, destination_id, generation, speed, 451 offset, payload, length, transaction_callback, &d); 452 wait_for_completion(&d.done); 453 timer_destroy_on_stack(&t.split_timeout_timer); 454 455 return d.rcode; 456 } 457 EXPORT_SYMBOL(fw_run_transaction); 458 459 static DEFINE_MUTEX(phy_config_mutex); 460 static DECLARE_COMPLETION(phy_config_done); 461 462 static void transmit_phy_packet_callback(struct fw_packet *packet, 463 struct fw_card *card, int status) 464 { 465 trace_async_phy_outbound_complete((uintptr_t)packet, card->index, packet->generation, status, 466 packet->timestamp); 467 complete(&phy_config_done); 468 } 469 470 static struct fw_packet phy_config_packet = { 471 .header_length = 12, 472 .payload_length = 0, 473 .speed = SCODE_100, 474 .callback = transmit_phy_packet_callback, 475 }; 476 477 void fw_send_phy_config(struct fw_card *card, 478 int node_id, int generation, int gap_count) 479 { 480 long timeout = msecs_to_jiffies(100); 481 u32 data = 0; 482 483 phy_packet_set_packet_identifier(&data, PHY_PACKET_PACKET_IDENTIFIER_PHY_CONFIG); 484 485 if (node_id != FW_PHY_CONFIG_NO_NODE_ID) { 486 phy_packet_phy_config_set_root_id(&data, node_id); 487 phy_packet_phy_config_set_force_root_node(&data, true); 488 } 489 490 if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) { 491 gap_count = card->driver->read_phy_reg(card, 1); 492 if (gap_count < 0) 493 return; 494 495 gap_count &= 63; 496 if (gap_count == 63) 497 return; 498 } 499 phy_packet_phy_config_set_gap_count(&data, gap_count); 500 phy_packet_phy_config_set_gap_count_optimization(&data, true); 501 502 guard(mutex)(&phy_config_mutex); 503 504 async_header_set_tcode(phy_config_packet.header, TCODE_LINK_INTERNAL); 505 phy_config_packet.header[1] = data; 506 phy_config_packet.header[2] = ~data; 507 phy_config_packet.generation = generation; 508 reinit_completion(&phy_config_done); 509 510 trace_async_phy_outbound_initiate((uintptr_t)&phy_config_packet, card->index, 511 phy_config_packet.generation, phy_config_packet.header[1], 512 phy_config_packet.header[2]); 513 514 card->driver->send_request(card, &phy_config_packet); 515 wait_for_completion_timeout(&phy_config_done, timeout); 516 } 517 518 static struct fw_address_handler *lookup_overlapping_address_handler( 519 struct list_head *list, unsigned long long offset, size_t length) 520 { 521 struct fw_address_handler *handler; 522 523 list_for_each_entry_rcu(handler, list, link) { 524 if (handler->offset < offset + length && 525 offset < handler->offset + handler->length) 526 return handler; 527 } 528 529 return NULL; 530 } 531 532 static bool is_enclosing_handler(struct fw_address_handler *handler, 533 unsigned long long offset, size_t length) 534 { 535 return handler->offset <= offset && 536 offset + length <= handler->offset + handler->length; 537 } 538 539 static struct fw_address_handler *lookup_enclosing_address_handler( 540 struct list_head *list, unsigned long long offset, size_t length) 541 { 542 struct fw_address_handler *handler; 543 544 list_for_each_entry_rcu(handler, list, link) { 545 if (is_enclosing_handler(handler, offset, length)) 546 return handler; 547 } 548 549 return NULL; 550 } 551 552 static DEFINE_SPINLOCK(address_handler_list_lock); 553 static LIST_HEAD(address_handler_list); 554 555 const struct fw_address_region fw_high_memory_region = 556 { .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, }; 557 EXPORT_SYMBOL(fw_high_memory_region); 558 559 static const struct fw_address_region low_memory_region = 560 { .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, }; 561 562 #if 0 563 const struct fw_address_region fw_private_region = 564 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, }; 565 const struct fw_address_region fw_csr_region = 566 { .start = CSR_REGISTER_BASE, 567 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, }; 568 const struct fw_address_region fw_unit_space_region = 569 { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, }; 570 #endif /* 0 */ 571 572 static void complete_address_handler(struct kref *kref) 573 { 574 struct fw_address_handler *handler = container_of(kref, struct fw_address_handler, kref); 575 576 complete(&handler->done); 577 } 578 579 static void get_address_handler(struct fw_address_handler *handler) 580 { 581 kref_get(&handler->kref); 582 } 583 584 static int put_address_handler(struct fw_address_handler *handler) 585 { 586 return kref_put(&handler->kref, complete_address_handler); 587 } 588 589 /** 590 * fw_core_add_address_handler() - register for incoming requests 591 * @handler: callback 592 * @region: region in the IEEE 1212 node space address range 593 * 594 * region->start, ->end, and handler->length have to be quadlet-aligned. 595 * 596 * When a request is received that falls within the specified address range, the specified callback 597 * is invoked. The parameters passed to the callback give the details of the particular request. 598 * The callback is invoked in the workqueue context in most cases. However, if the request is 599 * initiated by the local node, the callback is invoked in the initiator's context. 600 * 601 * To be called in process context. 602 * Return value: 0 on success, non-zero otherwise. 603 * 604 * The start offset of the handler's address region is determined by 605 * fw_core_add_address_handler() and is returned in handler->offset. 606 * 607 * Address allocations are exclusive, except for the FCP registers. 608 */ 609 int fw_core_add_address_handler(struct fw_address_handler *handler, 610 const struct fw_address_region *region) 611 { 612 struct fw_address_handler *other; 613 int ret = -EBUSY; 614 615 if (region->start & 0xffff000000000003ULL || 616 region->start >= region->end || 617 region->end > 0x0001000000000000ULL || 618 handler->length & 3 || 619 handler->length == 0) 620 return -EINVAL; 621 622 guard(spinlock)(&address_handler_list_lock); 623 624 handler->offset = region->start; 625 while (handler->offset + handler->length <= region->end) { 626 if (is_in_fcp_region(handler->offset, handler->length)) 627 other = NULL; 628 else 629 other = lookup_overlapping_address_handler 630 (&address_handler_list, 631 handler->offset, handler->length); 632 if (other != NULL) { 633 handler->offset += other->length; 634 } else { 635 init_completion(&handler->done); 636 kref_init(&handler->kref); 637 list_add_tail_rcu(&handler->link, &address_handler_list); 638 ret = 0; 639 break; 640 } 641 } 642 643 return ret; 644 } 645 EXPORT_SYMBOL(fw_core_add_address_handler); 646 647 /** 648 * fw_core_remove_address_handler() - unregister an address handler 649 * @handler: callback 650 * 651 * To be called in process context. 652 * 653 * When fw_core_remove_address_handler() returns, @handler->callback() is 654 * guaranteed to not run on any CPU anymore. 655 */ 656 void fw_core_remove_address_handler(struct fw_address_handler *handler) 657 { 658 scoped_guard(spinlock, &address_handler_list_lock) 659 list_del_rcu(&handler->link); 660 661 synchronize_rcu(); 662 663 if (!put_address_handler(handler)) 664 wait_for_completion(&handler->done); 665 } 666 EXPORT_SYMBOL(fw_core_remove_address_handler); 667 668 struct fw_request { 669 struct kref kref; 670 struct fw_packet response; 671 u32 request_header[ASYNC_HEADER_QUADLET_COUNT]; 672 int ack; 673 u32 timestamp; 674 u32 length; 675 u32 data[]; 676 }; 677 678 void fw_request_get(struct fw_request *request) 679 { 680 kref_get(&request->kref); 681 } 682 683 static void release_request(struct kref *kref) 684 { 685 struct fw_request *request = container_of(kref, struct fw_request, kref); 686 687 kfree(request); 688 } 689 690 void fw_request_put(struct fw_request *request) 691 { 692 kref_put(&request->kref, release_request); 693 } 694 695 static void free_response_callback(struct fw_packet *packet, 696 struct fw_card *card, int status) 697 { 698 struct fw_request *request = container_of(packet, struct fw_request, response); 699 700 trace_async_response_outbound_complete((uintptr_t)request, card->index, packet->generation, 701 packet->speed, status, packet->timestamp); 702 703 // Decrease the reference count since not at in-flight. 704 fw_request_put(request); 705 706 // Decrease the reference count to release the object. 707 fw_request_put(request); 708 } 709 710 int fw_get_response_length(struct fw_request *r) 711 { 712 int tcode, ext_tcode, data_length; 713 714 tcode = async_header_get_tcode(r->request_header); 715 716 switch (tcode) { 717 case TCODE_WRITE_QUADLET_REQUEST: 718 case TCODE_WRITE_BLOCK_REQUEST: 719 return 0; 720 721 case TCODE_READ_QUADLET_REQUEST: 722 return 4; 723 724 case TCODE_READ_BLOCK_REQUEST: 725 data_length = async_header_get_data_length(r->request_header); 726 return data_length; 727 728 case TCODE_LOCK_REQUEST: 729 ext_tcode = async_header_get_extended_tcode(r->request_header); 730 data_length = async_header_get_data_length(r->request_header); 731 switch (ext_tcode) { 732 case EXTCODE_FETCH_ADD: 733 case EXTCODE_LITTLE_ADD: 734 return data_length; 735 default: 736 return data_length / 2; 737 } 738 739 default: 740 WARN(1, "wrong tcode %d\n", tcode); 741 return 0; 742 } 743 } 744 745 void fw_fill_response(struct fw_packet *response, u32 *request_header, 746 int rcode, void *payload, size_t length) 747 { 748 int tcode, tlabel, extended_tcode, source, destination; 749 750 tcode = async_header_get_tcode(request_header); 751 tlabel = async_header_get_tlabel(request_header); 752 source = async_header_get_destination(request_header); // Exchange. 753 destination = async_header_get_source(request_header); // Exchange. 754 extended_tcode = async_header_get_extended_tcode(request_header); 755 756 async_header_set_retry(response->header, RETRY_1); 757 async_header_set_tlabel(response->header, tlabel); 758 async_header_set_destination(response->header, destination); 759 async_header_set_source(response->header, source); 760 async_header_set_rcode(response->header, rcode); 761 response->header[2] = 0; // The field is reserved. 762 763 switch (tcode) { 764 case TCODE_WRITE_QUADLET_REQUEST: 765 case TCODE_WRITE_BLOCK_REQUEST: 766 async_header_set_tcode(response->header, TCODE_WRITE_RESPONSE); 767 response->header_length = 12; 768 response->payload_length = 0; 769 break; 770 771 case TCODE_READ_QUADLET_REQUEST: 772 async_header_set_tcode(response->header, TCODE_READ_QUADLET_RESPONSE); 773 if (payload != NULL) 774 async_header_set_quadlet_data(response->header, *(u32 *)payload); 775 else 776 async_header_set_quadlet_data(response->header, 0); 777 response->header_length = 16; 778 response->payload_length = 0; 779 break; 780 781 case TCODE_READ_BLOCK_REQUEST: 782 case TCODE_LOCK_REQUEST: 783 async_header_set_tcode(response->header, tcode + 2); 784 async_header_set_data_length(response->header, length); 785 async_header_set_extended_tcode(response->header, extended_tcode); 786 response->header_length = 16; 787 response->payload = payload; 788 response->payload_length = length; 789 break; 790 791 default: 792 WARN(1, "wrong tcode %d\n", tcode); 793 } 794 795 response->payload_mapped = false; 796 } 797 EXPORT_SYMBOL(fw_fill_response); 798 799 static u32 compute_split_timeout_timestamp(struct fw_card *card, 800 u32 request_timestamp) 801 __must_hold(&card->split_timeout.lock) 802 { 803 unsigned int cycles; 804 u32 timestamp; 805 806 lockdep_assert_held(&card->split_timeout.lock); 807 808 cycles = card->split_timeout.cycles; 809 cycles += request_timestamp & 0x1fff; 810 811 timestamp = request_timestamp & ~0x1fff; 812 timestamp += (cycles / 8000) << 13; 813 timestamp |= cycles % 8000; 814 815 return timestamp; 816 } 817 818 static struct fw_request *allocate_request(struct fw_card *card, 819 struct fw_packet *p) 820 { 821 struct fw_request *request; 822 u32 *data, length; 823 int request_tcode; 824 825 request_tcode = async_header_get_tcode(p->header); 826 switch (request_tcode) { 827 case TCODE_WRITE_QUADLET_REQUEST: 828 data = &p->header[3]; 829 length = 4; 830 break; 831 832 case TCODE_WRITE_BLOCK_REQUEST: 833 case TCODE_LOCK_REQUEST: 834 data = p->payload; 835 length = async_header_get_data_length(p->header); 836 break; 837 838 case TCODE_READ_QUADLET_REQUEST: 839 data = NULL; 840 length = 4; 841 break; 842 843 case TCODE_READ_BLOCK_REQUEST: 844 data = NULL; 845 length = async_header_get_data_length(p->header); 846 break; 847 848 default: 849 fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n", 850 p->header[0], p->header[1], p->header[2]); 851 return NULL; 852 } 853 854 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC); 855 if (request == NULL) 856 return NULL; 857 kref_init(&request->kref); 858 859 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 860 // local destination never runs in any type of IRQ context. 861 scoped_guard(spinlock_irqsave, &card->split_timeout.lock) 862 request->response.timestamp = compute_split_timeout_timestamp(card, p->timestamp); 863 864 request->response.speed = p->speed; 865 request->response.generation = p->generation; 866 request->response.ack = 0; 867 request->response.callback = free_response_callback; 868 request->ack = p->ack; 869 request->timestamp = p->timestamp; 870 request->length = length; 871 if (data) 872 memcpy(request->data, data, length); 873 874 memcpy(request->request_header, p->header, sizeof(p->header)); 875 876 return request; 877 } 878 879 /** 880 * fw_send_response: - send response packet for asynchronous transaction. 881 * @card: interface to send the response at. 882 * @request: firewire request data for the transaction. 883 * @rcode: response code to send. 884 * 885 * Submit a response packet into the asynchronous response transmission queue. The @request 886 * is going to be released when the transmission successfully finishes later. 887 */ 888 void fw_send_response(struct fw_card *card, 889 struct fw_request *request, int rcode) 890 { 891 u32 *data = NULL; 892 unsigned int data_length = 0; 893 894 /* unified transaction or broadcast transaction: don't respond */ 895 if (request->ack != ACK_PENDING || 896 HEADER_DESTINATION_IS_BROADCAST(request->request_header)) { 897 fw_request_put(request); 898 return; 899 } 900 901 if (rcode == RCODE_COMPLETE) { 902 data = request->data; 903 data_length = fw_get_response_length(request); 904 } 905 906 fw_fill_response(&request->response, request->request_header, rcode, data, data_length); 907 908 // Increase the reference count so that the object is kept during in-flight. 909 fw_request_get(request); 910 911 trace_async_response_outbound_initiate((uintptr_t)request, card->index, 912 request->response.generation, request->response.speed, 913 request->response.header, data, 914 data ? data_length / 4 : 0); 915 916 card->driver->send_response(card, &request->response); 917 } 918 EXPORT_SYMBOL(fw_send_response); 919 920 /** 921 * fw_get_request_speed() - returns speed at which the @request was received 922 * @request: firewire request data 923 */ 924 int fw_get_request_speed(struct fw_request *request) 925 { 926 return request->response.speed; 927 } 928 EXPORT_SYMBOL(fw_get_request_speed); 929 930 /** 931 * fw_request_get_timestamp: Get timestamp of the request. 932 * @request: The opaque pointer to request structure. 933 * 934 * Get timestamp when 1394 OHCI controller receives the asynchronous request subaction. The 935 * timestamp consists of the low order 3 bits of second field and the full 13 bits of count 936 * field of isochronous cycle time register. 937 * 938 * Returns: timestamp of the request. 939 */ 940 u32 fw_request_get_timestamp(const struct fw_request *request) 941 { 942 return request->timestamp; 943 } 944 EXPORT_SYMBOL_GPL(fw_request_get_timestamp); 945 946 static void handle_exclusive_region_request(struct fw_card *card, 947 struct fw_packet *p, 948 struct fw_request *request, 949 unsigned long long offset) 950 { 951 struct fw_address_handler *handler; 952 int tcode, destination, source; 953 954 destination = async_header_get_destination(p->header); 955 source = async_header_get_source(p->header); 956 tcode = async_header_get_tcode(p->header); 957 if (tcode == TCODE_LOCK_REQUEST) 958 tcode = 0x10 + async_header_get_extended_tcode(p->header); 959 960 scoped_guard(rcu) { 961 handler = lookup_enclosing_address_handler(&address_handler_list, offset, 962 request->length); 963 if (handler) 964 get_address_handler(handler); 965 } 966 967 if (!handler) { 968 fw_send_response(card, request, RCODE_ADDRESS_ERROR); 969 return; 970 } 971 972 // Outside the RCU read-side critical section. Without spinlock. With reference count. 973 handler->address_callback(card, request, tcode, destination, source, p->generation, offset, 974 request->data, request->length, handler->callback_data); 975 put_address_handler(handler); 976 } 977 978 // To use kmalloc allocator efficiently, this should be power of two. 979 #define BUFFER_ON_KERNEL_STACK_SIZE 4 980 981 static void handle_fcp_region_request(struct fw_card *card, 982 struct fw_packet *p, 983 struct fw_request *request, 984 unsigned long long offset) 985 { 986 struct fw_address_handler *buffer_on_kernel_stack[BUFFER_ON_KERNEL_STACK_SIZE]; 987 struct fw_address_handler *handler, **handlers; 988 int tcode, destination, source, i, count, buffer_size; 989 990 if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) && 991 offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) || 992 request->length > 0x200) { 993 fw_send_response(card, request, RCODE_ADDRESS_ERROR); 994 995 return; 996 } 997 998 tcode = async_header_get_tcode(p->header); 999 destination = async_header_get_destination(p->header); 1000 source = async_header_get_source(p->header); 1001 1002 if (tcode != TCODE_WRITE_QUADLET_REQUEST && 1003 tcode != TCODE_WRITE_BLOCK_REQUEST) { 1004 fw_send_response(card, request, RCODE_TYPE_ERROR); 1005 1006 return; 1007 } 1008 1009 count = 0; 1010 handlers = buffer_on_kernel_stack; 1011 buffer_size = ARRAY_SIZE(buffer_on_kernel_stack); 1012 scoped_guard(rcu) { 1013 list_for_each_entry_rcu(handler, &address_handler_list, link) { 1014 if (is_enclosing_handler(handler, offset, request->length)) { 1015 if (count >= buffer_size) { 1016 int next_size = buffer_size * 2; 1017 struct fw_address_handler **buffer_on_kernel_heap; 1018 1019 if (handlers == buffer_on_kernel_stack) 1020 buffer_on_kernel_heap = NULL; 1021 else 1022 buffer_on_kernel_heap = handlers; 1023 1024 buffer_on_kernel_heap = 1025 krealloc_array(buffer_on_kernel_heap, next_size, 1026 sizeof(*buffer_on_kernel_heap), GFP_ATOMIC); 1027 // FCP is used for purposes unrelated to significant system 1028 // resources (e.g. storage or networking), so allocation 1029 // failures are not considered so critical. 1030 if (!buffer_on_kernel_heap) 1031 break; 1032 1033 if (handlers == buffer_on_kernel_stack) { 1034 memcpy(buffer_on_kernel_heap, buffer_on_kernel_stack, 1035 sizeof(buffer_on_kernel_stack)); 1036 } 1037 1038 handlers = buffer_on_kernel_heap; 1039 buffer_size = next_size; 1040 } 1041 get_address_handler(handler); 1042 handlers[count++] = handler; 1043 } 1044 } 1045 } 1046 1047 for (i = 0; i < count; ++i) { 1048 handler = handlers[i]; 1049 handler->address_callback(card, request, tcode, destination, source, 1050 p->generation, offset, request->data, 1051 request->length, handler->callback_data); 1052 put_address_handler(handler); 1053 } 1054 1055 if (handlers != buffer_on_kernel_stack) 1056 kfree(handlers); 1057 1058 fw_send_response(card, request, RCODE_COMPLETE); 1059 } 1060 1061 void fw_core_handle_request(struct fw_card *card, struct fw_packet *p) 1062 { 1063 struct fw_request *request; 1064 unsigned long long offset; 1065 unsigned int tcode; 1066 1067 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE) 1068 return; 1069 1070 tcode = async_header_get_tcode(p->header); 1071 if (tcode_is_link_internal(tcode)) { 1072 trace_async_phy_inbound((uintptr_t)p, card->index, p->generation, p->ack, p->timestamp, 1073 p->header[1], p->header[2]); 1074 fw_cdev_handle_phy_packet(card, p); 1075 return; 1076 } 1077 1078 request = allocate_request(card, p); 1079 if (request == NULL) { 1080 /* FIXME: send statically allocated busy packet. */ 1081 return; 1082 } 1083 1084 trace_async_request_inbound((uintptr_t)request, card->index, p->generation, p->speed, 1085 p->ack, p->timestamp, p->header, request->data, 1086 tcode_is_read_request(tcode) ? 0 : request->length / 4); 1087 1088 offset = async_header_get_offset(p->header); 1089 1090 if (!is_in_fcp_region(offset, request->length)) 1091 handle_exclusive_region_request(card, p, request, offset); 1092 else 1093 handle_fcp_region_request(card, p, request, offset); 1094 1095 } 1096 EXPORT_SYMBOL(fw_core_handle_request); 1097 1098 void fw_core_handle_response(struct fw_card *card, struct fw_packet *p) 1099 { 1100 struct fw_transaction *t = NULL, *iter; 1101 u32 *data; 1102 size_t data_length; 1103 int tcode, tlabel, source, rcode; 1104 1105 tcode = async_header_get_tcode(p->header); 1106 tlabel = async_header_get_tlabel(p->header); 1107 source = async_header_get_source(p->header); 1108 rcode = async_header_get_rcode(p->header); 1109 1110 // FIXME: sanity check packet, is length correct, does tcodes 1111 // and addresses match to the transaction request queried later. 1112 // 1113 // For the tracepoints event, let us decode the header here against the concern. 1114 1115 switch (tcode) { 1116 case TCODE_READ_QUADLET_RESPONSE: 1117 data = (u32 *) &p->header[3]; 1118 data_length = 4; 1119 break; 1120 1121 case TCODE_WRITE_RESPONSE: 1122 data = NULL; 1123 data_length = 0; 1124 break; 1125 1126 case TCODE_READ_BLOCK_RESPONSE: 1127 case TCODE_LOCK_RESPONSE: 1128 data = p->payload; 1129 data_length = async_header_get_data_length(p->header); 1130 break; 1131 1132 default: 1133 /* Should never happen, this is just to shut up gcc. */ 1134 data = NULL; 1135 data_length = 0; 1136 break; 1137 } 1138 1139 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for 1140 // local destination never runs in any type of IRQ context. 1141 scoped_guard(spinlock_irqsave, &card->transactions.lock) { 1142 list_for_each_entry(iter, &card->transactions.list, link) { 1143 if (iter->node_id == source && iter->tlabel == tlabel) { 1144 if (try_cancel_split_timeout(iter)) { 1145 list_del_init(&iter->link); 1146 card->transactions.tlabel_mask &= ~(1ULL << iter->tlabel); 1147 t = iter; 1148 } 1149 break; 1150 } 1151 } 1152 } 1153 1154 trace_async_response_inbound((uintptr_t)t, card->index, p->generation, p->speed, p->ack, 1155 p->timestamp, p->header, data, data_length / 4); 1156 1157 if (!t) { 1158 fw_notice(card, "unsolicited response (source %x, tlabel %x)\n", 1159 source, tlabel); 1160 return; 1161 } 1162 1163 /* 1164 * The response handler may be executed while the request handler 1165 * is still pending. Cancel the request handler. 1166 */ 1167 card->driver->cancel_packet(card, &t->packet); 1168 1169 if (!t->with_tstamp) { 1170 t->callback.without_tstamp(card, rcode, data, data_length, t->callback_data); 1171 } else { 1172 t->callback.with_tstamp(card, rcode, t->packet.timestamp, p->timestamp, data, 1173 data_length, t->callback_data); 1174 } 1175 } 1176 EXPORT_SYMBOL(fw_core_handle_response); 1177 1178 /** 1179 * fw_rcode_string - convert a firewire result code to an error description 1180 * @rcode: the result code 1181 */ 1182 const char *fw_rcode_string(int rcode) 1183 { 1184 static const char *const names[] = { 1185 [RCODE_COMPLETE] = "no error", 1186 [RCODE_CONFLICT_ERROR] = "conflict error", 1187 [RCODE_DATA_ERROR] = "data error", 1188 [RCODE_TYPE_ERROR] = "type error", 1189 [RCODE_ADDRESS_ERROR] = "address error", 1190 [RCODE_SEND_ERROR] = "send error", 1191 [RCODE_CANCELLED] = "timeout", 1192 [RCODE_BUSY] = "busy", 1193 [RCODE_GENERATION] = "bus reset", 1194 [RCODE_NO_ACK] = "no ack", 1195 }; 1196 1197 if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode]) 1198 return names[rcode]; 1199 else 1200 return "unknown"; 1201 } 1202 EXPORT_SYMBOL(fw_rcode_string); 1203 1204 static const struct fw_address_region topology_map_region = 1205 { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP, 1206 .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, }; 1207 1208 static void handle_topology_map(struct fw_card *card, struct fw_request *request, 1209 int tcode, int destination, int source, int generation, 1210 unsigned long long offset, void *payload, size_t length, 1211 void *callback_data) 1212 { 1213 int start; 1214 1215 if (!tcode_is_read_request(tcode)) { 1216 fw_send_response(card, request, RCODE_TYPE_ERROR); 1217 return; 1218 } 1219 1220 if ((offset & 3) > 0 || (length & 3) > 0) { 1221 fw_send_response(card, request, RCODE_ADDRESS_ERROR); 1222 return; 1223 } 1224 1225 start = (offset - topology_map_region.start) / 4; 1226 1227 // NOTE: This can be without irqsave when we can guarantee that fw_send_request() for local 1228 // destination never runs in any type of IRQ context. 1229 scoped_guard(spinlock_irqsave, &card->topology_map.lock) 1230 memcpy(payload, &card->topology_map.buffer[start], length); 1231 1232 fw_send_response(card, request, RCODE_COMPLETE); 1233 } 1234 1235 static struct fw_address_handler topology_map = { 1236 .length = 0x400, 1237 .address_callback = handle_topology_map, 1238 }; 1239 1240 static const struct fw_address_region registers_region = 1241 { .start = CSR_REGISTER_BASE, 1242 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, }; 1243 1244 static void update_split_timeout(struct fw_card *card) 1245 __must_hold(&card->split_timeout.lock) 1246 { 1247 unsigned int cycles; 1248 1249 cycles = card->split_timeout.hi * 8000 + (card->split_timeout.lo >> 19); 1250 1251 /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */ 1252 cycles = clamp(cycles, 800u, 3u * 8000u); 1253 1254 card->split_timeout.cycles = cycles; 1255 card->split_timeout.jiffies = isoc_cycles_to_jiffies(cycles); 1256 } 1257 1258 static void handle_registers(struct fw_card *card, struct fw_request *request, 1259 int tcode, int destination, int source, int generation, 1260 unsigned long long offset, void *payload, size_t length, 1261 void *callback_data) 1262 { 1263 int reg = offset & ~CSR_REGISTER_BASE; 1264 __be32 *data = payload; 1265 int rcode = RCODE_COMPLETE; 1266 1267 switch (reg) { 1268 case CSR_PRIORITY_BUDGET: 1269 if (!card->priority_budget_implemented) { 1270 rcode = RCODE_ADDRESS_ERROR; 1271 break; 1272 } 1273 fallthrough; 1274 1275 case CSR_NODE_IDS: 1276 /* 1277 * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8 1278 * and 9.6, but interoperable with IEEE 1394.1-2004 bridges 1279 */ 1280 fallthrough; 1281 1282 case CSR_STATE_CLEAR: 1283 case CSR_STATE_SET: 1284 case CSR_CYCLE_TIME: 1285 case CSR_BUS_TIME: 1286 case CSR_BUSY_TIMEOUT: 1287 if (tcode == TCODE_READ_QUADLET_REQUEST) 1288 *data = cpu_to_be32(card->driver->read_csr(card, reg)); 1289 else if (tcode == TCODE_WRITE_QUADLET_REQUEST) 1290 card->driver->write_csr(card, reg, be32_to_cpu(*data)); 1291 else 1292 rcode = RCODE_TYPE_ERROR; 1293 break; 1294 1295 case CSR_RESET_START: 1296 if (tcode == TCODE_WRITE_QUADLET_REQUEST) 1297 card->driver->write_csr(card, CSR_STATE_CLEAR, 1298 CSR_STATE_BIT_ABDICATE); 1299 else 1300 rcode = RCODE_TYPE_ERROR; 1301 break; 1302 1303 case CSR_SPLIT_TIMEOUT_HI: 1304 if (tcode == TCODE_READ_QUADLET_REQUEST) { 1305 *data = cpu_to_be32(card->split_timeout.hi); 1306 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) { 1307 // NOTE: This can be without irqsave when we can guarantee that 1308 // __fw_send_request() for local destination never runs in any type of IRQ 1309 // context. 1310 scoped_guard(spinlock_irqsave, &card->split_timeout.lock) { 1311 card->split_timeout.hi = be32_to_cpu(*data) & 7; 1312 update_split_timeout(card); 1313 } 1314 } else { 1315 rcode = RCODE_TYPE_ERROR; 1316 } 1317 break; 1318 1319 case CSR_SPLIT_TIMEOUT_LO: 1320 if (tcode == TCODE_READ_QUADLET_REQUEST) { 1321 *data = cpu_to_be32(card->split_timeout.lo); 1322 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) { 1323 // NOTE: This can be without irqsave when we can guarantee that 1324 // __fw_send_request() for local destination never runs in any type of IRQ 1325 // context. 1326 scoped_guard(spinlock_irqsave, &card->split_timeout.lock) { 1327 card->split_timeout.lo = be32_to_cpu(*data) & 0xfff80000; 1328 update_split_timeout(card); 1329 } 1330 } else { 1331 rcode = RCODE_TYPE_ERROR; 1332 } 1333 break; 1334 1335 case CSR_MAINT_UTILITY: 1336 if (tcode == TCODE_READ_QUADLET_REQUEST) 1337 *data = card->maint_utility_register; 1338 else if (tcode == TCODE_WRITE_QUADLET_REQUEST) 1339 card->maint_utility_register = *data; 1340 else 1341 rcode = RCODE_TYPE_ERROR; 1342 break; 1343 1344 case CSR_BROADCAST_CHANNEL: 1345 if (tcode == TCODE_READ_QUADLET_REQUEST) 1346 *data = cpu_to_be32(card->broadcast_channel); 1347 else if (tcode == TCODE_WRITE_QUADLET_REQUEST) 1348 card->broadcast_channel = 1349 (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) | 1350 BROADCAST_CHANNEL_INITIAL; 1351 else 1352 rcode = RCODE_TYPE_ERROR; 1353 break; 1354 1355 case CSR_BUS_MANAGER_ID: 1356 case CSR_BANDWIDTH_AVAILABLE: 1357 case CSR_CHANNELS_AVAILABLE_HI: 1358 case CSR_CHANNELS_AVAILABLE_LO: 1359 /* 1360 * FIXME: these are handled by the OHCI hardware and 1361 * the stack never sees these request. If we add 1362 * support for a new type of controller that doesn't 1363 * handle this in hardware we need to deal with these 1364 * transactions. 1365 */ 1366 BUG(); 1367 break; 1368 1369 default: 1370 rcode = RCODE_ADDRESS_ERROR; 1371 break; 1372 } 1373 1374 fw_send_response(card, request, rcode); 1375 } 1376 1377 static struct fw_address_handler registers = { 1378 .length = 0x400, 1379 .address_callback = handle_registers, 1380 }; 1381 1382 static void handle_low_memory(struct fw_card *card, struct fw_request *request, 1383 int tcode, int destination, int source, int generation, 1384 unsigned long long offset, void *payload, size_t length, 1385 void *callback_data) 1386 { 1387 /* 1388 * This catches requests not handled by the physical DMA unit, 1389 * i.e., wrong transaction types or unauthorized source nodes. 1390 */ 1391 fw_send_response(card, request, RCODE_TYPE_ERROR); 1392 } 1393 1394 static struct fw_address_handler low_memory = { 1395 .length = FW_MAX_PHYSICAL_RANGE, 1396 .address_callback = handle_low_memory, 1397 }; 1398 1399 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); 1400 MODULE_DESCRIPTION("Core IEEE1394 transaction logic"); 1401 MODULE_LICENSE("GPL"); 1402 1403 static const u32 vendor_textual_descriptor[] = { 1404 /* textual descriptor leaf () */ 1405 0x00060000, 1406 0x00000000, 1407 0x00000000, 1408 0x4c696e75, /* L i n u */ 1409 0x78204669, /* x F i */ 1410 0x72657769, /* r e w i */ 1411 0x72650000, /* r e */ 1412 }; 1413 1414 static const u32 model_textual_descriptor[] = { 1415 /* model descriptor leaf () */ 1416 0x00030000, 1417 0x00000000, 1418 0x00000000, 1419 0x4a756a75, /* J u j u */ 1420 }; 1421 1422 static struct fw_descriptor vendor_id_descriptor = { 1423 .length = ARRAY_SIZE(vendor_textual_descriptor), 1424 .immediate = 0x03001f11, 1425 .key = 0x81000000, 1426 .data = vendor_textual_descriptor, 1427 }; 1428 1429 static struct fw_descriptor model_id_descriptor = { 1430 .length = ARRAY_SIZE(model_textual_descriptor), 1431 .immediate = 0x17023901, 1432 .key = 0x81000000, 1433 .data = model_textual_descriptor, 1434 }; 1435 1436 static int __init fw_core_init(void) 1437 { 1438 int ret; 1439 1440 fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM, 0); 1441 if (!fw_workqueue) 1442 return -ENOMEM; 1443 1444 ret = bus_register(&fw_bus_type); 1445 if (ret < 0) { 1446 destroy_workqueue(fw_workqueue); 1447 return ret; 1448 } 1449 1450 fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops); 1451 if (fw_cdev_major < 0) { 1452 bus_unregister(&fw_bus_type); 1453 destroy_workqueue(fw_workqueue); 1454 return fw_cdev_major; 1455 } 1456 1457 fw_core_add_address_handler(&topology_map, &topology_map_region); 1458 fw_core_add_address_handler(®isters, ®isters_region); 1459 fw_core_add_address_handler(&low_memory, &low_memory_region); 1460 fw_core_add_descriptor(&vendor_id_descriptor); 1461 fw_core_add_descriptor(&model_id_descriptor); 1462 1463 return 0; 1464 } 1465 1466 static void __exit fw_core_cleanup(void) 1467 { 1468 unregister_chrdev(fw_cdev_major, "firewire"); 1469 bus_unregister(&fw_bus_type); 1470 destroy_workqueue(fw_workqueue); 1471 xa_destroy(&fw_device_xa); 1472 } 1473 1474 module_init(fw_core_init); 1475 module_exit(fw_core_cleanup); 1476