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