1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_FIREWIRE_H 3 #define _LINUX_FIREWIRE_H 4 5 #include <linux/completion.h> 6 #include <linux/device.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/kernel.h> 9 #include <linux/kref.h> 10 #include <linux/list.h> 11 #include <linux/mutex.h> 12 #include <linux/spinlock.h> 13 #include <linux/sysfs.h> 14 #include <linux/timer.h> 15 #include <linux/types.h> 16 #include <linux/workqueue.h> 17 #include <linux/device-id/ieee1394.h> 18 19 #include <linux/atomic.h> 20 #include <asm/byteorder.h> 21 22 #define CSR_REGISTER_BASE 0xfffff0000000ULL 23 24 /* register offsets are relative to CSR_REGISTER_BASE */ 25 #define CSR_STATE_CLEAR 0x0 26 #define CSR_STATE_SET 0x4 27 #define CSR_NODE_IDS 0x8 28 #define CSR_RESET_START 0xc 29 #define CSR_SPLIT_TIMEOUT_HI 0x18 30 #define CSR_SPLIT_TIMEOUT_LO 0x1c 31 #define CSR_CYCLE_TIME 0x200 32 #define CSR_BUS_TIME 0x204 33 #define CSR_BUSY_TIMEOUT 0x210 34 #define CSR_PRIORITY_BUDGET 0x218 35 #define CSR_BUS_MANAGER_ID 0x21c 36 #define CSR_BANDWIDTH_AVAILABLE 0x220 37 #define CSR_CHANNELS_AVAILABLE 0x224 38 #define CSR_CHANNELS_AVAILABLE_HI 0x224 39 #define CSR_CHANNELS_AVAILABLE_LO 0x228 40 #define CSR_MAINT_UTILITY 0x230 41 #define CSR_BROADCAST_CHANNEL 0x234 42 #define CSR_CONFIG_ROM 0x400 43 #define CSR_CONFIG_ROM_END 0x800 44 #define CSR_OMPR 0x900 45 #define CSR_OPCR(i) (0x904 + (i) * 4) 46 #define CSR_IMPR 0x980 47 #define CSR_IPCR(i) (0x984 + (i) * 4) 48 #define CSR_FCP_COMMAND 0xB00 49 #define CSR_FCP_RESPONSE 0xD00 50 #define CSR_FCP_END 0xF00 51 #define CSR_TOPOLOGY_MAP 0x1000 52 #define CSR_TOPOLOGY_MAP_END 0x1400 53 #define CSR_SPEED_MAP 0x2000 54 #define CSR_SPEED_MAP_END 0x3000 55 56 #define CSR_OFFSET 0x40 57 #define CSR_LEAF 0x80 58 #define CSR_DIRECTORY 0xc0 59 60 #define CSR_DESCRIPTOR 0x01 61 #define CSR_VENDOR 0x03 62 #define CSR_HARDWARE_VERSION 0x04 63 #define CSR_UNIT 0x11 64 #define CSR_SPECIFIER_ID 0x12 65 #define CSR_VERSION 0x13 66 #define CSR_DEPENDENT_INFO 0x14 67 #define CSR_MODEL 0x17 68 #define CSR_DIRECTORY_ID 0x20 69 70 struct fw_csr_iterator { 71 const u32 *p; 72 const u32 *end; 73 }; 74 75 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p); 76 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value); 77 int fw_csr_string(const u32 *directory, int key, char *buf, size_t size); 78 79 extern const struct bus_type fw_bus_type; 80 81 struct fw_card_driver; 82 struct fw_node; 83 84 struct fw_card { 85 const struct fw_card_driver *driver; 86 struct device *device; 87 struct kref kref; 88 struct completion done; 89 90 int node_id; 91 int generation; 92 u64 reset_jiffies; 93 94 struct { 95 int current_tlabel; 96 u64 tlabel_mask; 97 struct list_head list; 98 spinlock_t lock; 99 } transactions; 100 101 struct { 102 u32 hi; 103 u32 lo; 104 unsigned int cycles; 105 unsigned int jiffies; 106 spinlock_t lock; 107 } split_timeout; 108 109 unsigned long long guid; 110 unsigned max_receive; 111 int link_speed; 112 int config_rom_generation; 113 114 spinlock_t lock; 115 116 struct fw_node *local_node; 117 struct fw_node *root_node; 118 struct fw_node *irm_node; 119 u8 color; /* must be u8 to match the definition in struct fw_node */ 120 int gap_count; 121 bool beta_repeaters_present; 122 123 int index; 124 struct list_head link; 125 126 struct delayed_work br_work; /* bus reset job */ 127 bool br_short; 128 129 struct delayed_work bm_work; /* bus manager job */ 130 int bm_retries; 131 int bm_generation; 132 int bm_node_id; 133 bool bm_abdicate; 134 135 bool priority_budget_implemented; /* controller feature */ 136 bool broadcast_channel_auto_allocated; /* controller feature */ 137 138 bool broadcast_channel_allocated; 139 u32 broadcast_channel; 140 141 struct { 142 __be32 buffer[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4]; 143 spinlock_t lock; 144 } topology_map; 145 146 __be32 maint_utility_register; 147 148 struct workqueue_struct *isoc_wq; 149 struct workqueue_struct *async_wq; 150 }; 151 152 static inline struct fw_card *fw_card_get(struct fw_card *card) 153 { 154 kref_get(&card->kref); 155 156 return card; 157 } 158 159 void fw_card_release(struct kref *kref); 160 161 static inline void fw_card_put(struct fw_card *card) 162 { 163 kref_put(&card->kref, fw_card_release); 164 } 165 166 int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time); 167 168 struct fw_attribute_group { 169 struct attribute_group *groups[2]; 170 struct attribute_group group; 171 struct attribute *attrs[13]; 172 }; 173 174 enum fw_device_quirk { 175 // See afa1282a35d3 ("firewire: core: check for 1394a compliant IRM, fix inaccessibility of Sony camcorder"). 176 FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY = BIT(0), 177 178 // See a509e43ff338 ("firewire: core: fix unstable I/O with Canon camcorder"). 179 FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER = BIT(1), 180 181 // MOTU Audio Express transfers acknowledge packet with 0x10 for pending state. 182 FW_DEVICE_QUIRK_ACK_PACKET_WITH_INVALID_PENDING_CODE = BIT(2), 183 184 // TASCAM FW-1082/FW-1804/FW-1884 often freezes when receiving S400 packets. 185 FW_DEVICE_QUIRK_UNSTABLE_AT_S400 = BIT(3), 186 }; 187 188 enum fw_device_state { 189 FW_DEVICE_INITIALIZING, 190 FW_DEVICE_RUNNING, 191 FW_DEVICE_GONE, 192 FW_DEVICE_SHUTDOWN, 193 }; 194 195 /* 196 * Note, fw_device.generation always has to be read before fw_device.node_id. 197 * Use SMP memory barriers to ensure this. Otherwise requests will be sent 198 * to an outdated node_id if the generation was updated in the meantime due 199 * to a bus reset. 200 * 201 * Likewise, fw-core will take care to update .node_id before .generation so 202 * that whenever fw_device.generation is current WRT the actual bus generation, 203 * fw_device.node_id is guaranteed to be current too. 204 * 205 * The same applies to fw_device.card->node_id vs. fw_device.generation. 206 * 207 * fw_device.config_rom and fw_device.config_rom_length may be accessed during 208 * the lifetime of any fw_unit belonging to the fw_device, before device_del() 209 * was called on the last fw_unit. Alternatively, they may be accessed while 210 * holding fw_device_rwsem. 211 */ 212 struct fw_device { 213 atomic_t state; 214 struct fw_node *node; 215 int node_id; 216 int generation; 217 unsigned max_speed; 218 struct fw_card *card; 219 struct device device; 220 221 // A set of enum fw_device_quirk. 222 int quirks; 223 224 struct mutex client_list_mutex; 225 struct list_head client_list; 226 227 const u32 *config_rom; 228 size_t config_rom_length; 229 int config_rom_retries; 230 unsigned is_local:1; 231 unsigned max_rec:4; 232 unsigned cmc:1; 233 unsigned irmc:1; 234 unsigned bc_implemented:2; 235 236 work_func_t workfn; 237 struct delayed_work work; 238 struct fw_attribute_group attribute_group; 239 }; 240 241 #define fw_device(dev) container_of_const(dev, struct fw_device, device) 242 243 static inline int fw_device_is_shutdown(struct fw_device *device) 244 { 245 return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN; 246 } 247 248 int fw_device_enable_phys_dma(struct fw_device *device); 249 250 /* 251 * fw_unit.directory must not be accessed after device_del(&fw_unit.device). 252 */ 253 struct fw_unit { 254 struct device device; 255 const u32 *directory; 256 struct fw_attribute_group attribute_group; 257 }; 258 259 #define fw_unit(dev) container_of_const(dev, struct fw_unit, device) 260 261 static inline struct fw_unit *fw_unit_get(struct fw_unit *unit) 262 { 263 get_device(&unit->device); 264 265 return unit; 266 } 267 268 static inline void fw_unit_put(struct fw_unit *unit) 269 { 270 put_device(&unit->device); 271 } 272 273 #define fw_parent_device(unit) fw_device(unit->device.parent) 274 275 struct fw_driver { 276 struct device_driver driver; 277 int (*probe)(struct fw_unit *unit, const struct ieee1394_device_id *id); 278 /* Called when the parent device sits through a bus reset. */ 279 void (*update)(struct fw_unit *unit); 280 void (*remove)(struct fw_unit *unit); 281 const struct ieee1394_device_id *id_table; 282 }; 283 284 struct fw_packet; 285 struct fw_request; 286 287 typedef void (*fw_packet_callback_t)(struct fw_packet *packet, 288 struct fw_card *card, int status); 289 typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode, 290 void *data, size_t length, 291 void *callback_data); 292 typedef void (*fw_transaction_callback_with_tstamp_t)(struct fw_card *card, int rcode, 293 u32 request_tstamp, u32 response_tstamp, void *data, 294 size_t length, void *callback_data); 295 296 union fw_transaction_callback { 297 fw_transaction_callback_t without_tstamp; 298 fw_transaction_callback_with_tstamp_t with_tstamp; 299 }; 300 301 /* 302 * This callback handles an inbound request subaction. It is called in 303 * RCU read-side context, therefore must not sleep. 304 * 305 * The callback should not initiate outbound request subactions directly. 306 * Otherwise there is a danger of recursion of inbound and outbound 307 * transactions from and to the local node. 308 * 309 * The callback is responsible that fw_send_response() is called on the @request, except for FCP 310 * registers for which the core takes care of that. 311 */ 312 typedef void (*fw_address_callback_t)(struct fw_card *card, 313 struct fw_request *request, 314 int tcode, int destination, int source, 315 int generation, 316 unsigned long long offset, 317 void *data, size_t length, 318 void *callback_data); 319 320 struct fw_packet { 321 int speed; 322 int generation; 323 u32 header[4]; 324 size_t header_length; 325 void *payload; 326 size_t payload_length; 327 dma_addr_t payload_bus; 328 bool payload_mapped; 329 u32 timestamp; 330 331 /* 332 * This callback is called when the packet transmission has completed. 333 * For successful transmission, the status code is the ack received 334 * from the destination. Otherwise it is one of the juju-specific 335 * rcodes: RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK. 336 * The callback can be called from workqueue and thus must never block. 337 */ 338 fw_packet_callback_t callback; 339 int ack; 340 struct list_head link; 341 void *driver_data; 342 }; 343 344 struct fw_transaction { 345 int node_id; /* The generation is implied; it is always the current. */ 346 int tlabel; 347 struct list_head link; 348 struct fw_card *card; 349 bool is_split_transaction; 350 struct timer_list split_timeout_timer; 351 u32 split_timeout_cycle; 352 353 struct fw_packet packet; 354 355 /* 356 * The data passed to the callback is valid only during the 357 * callback. 358 */ 359 union fw_transaction_callback callback; 360 bool with_tstamp; 361 void *callback_data; 362 }; 363 364 struct fw_address_handler { 365 u64 offset; 366 u64 length; 367 fw_address_callback_t address_callback; 368 void *callback_data; 369 370 // Only for core functions. 371 struct list_head link; 372 struct kref kref; 373 struct completion done; 374 }; 375 376 struct fw_address_region { 377 u64 start; 378 u64 end; 379 }; 380 381 extern const struct fw_address_region fw_high_memory_region; 382 383 int fw_core_add_address_handler(struct fw_address_handler *handler, 384 const struct fw_address_region *region); 385 void fw_core_remove_address_handler(struct fw_address_handler *handler); 386 void fw_send_response(struct fw_card *card, 387 struct fw_request *request, int rcode); 388 int fw_get_request_speed(struct fw_request *request); 389 u32 fw_request_get_timestamp(const struct fw_request *request); 390 391 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode, 392 int destination_id, int generation, int speed, unsigned long long offset, 393 void *payload, size_t length, union fw_transaction_callback callback, 394 bool with_tstamp, void *callback_data); 395 396 /** 397 * fw_send_request() - submit a request packet for transmission to generate callback for response 398 * subaction without time stamp. 399 * @card: interface to send the request at 400 * @t: transaction instance to which the request belongs 401 * @tcode: transaction code 402 * @destination_id: destination node ID, consisting of bus_ID and phy_ID 403 * @generation: bus generation in which request and response are valid 404 * @speed: transmission speed 405 * @offset: 48bit wide offset into destination's address space 406 * @payload: data payload for the request subaction 407 * @length: length of the payload, in bytes 408 * @callback: function to be called when the transaction is completed 409 * @callback_data: data to be passed to the transaction completion callback 410 * 411 * A variation of __fw_send_request() to generate callback for response subaction without time 412 * stamp. 413 * 414 * The callback is invoked in the workqueue context in most cases. However, if an error is detected 415 * before queueing or the destination address refers to the local node, it is invoked in the 416 * current context instead. 417 */ 418 static inline void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode, 419 int destination_id, int generation, int speed, 420 unsigned long long offset, void *payload, size_t length, 421 fw_transaction_callback_t callback, void *callback_data) 422 { 423 union fw_transaction_callback cb = { 424 .without_tstamp = callback, 425 }; 426 __fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload, 427 length, cb, false, callback_data); 428 } 429 430 /** 431 * fw_send_request_with_tstamp() - submit a request packet for transmission to generate callback for 432 * response with time stamp. 433 * @card: interface to send the request at 434 * @t: transaction instance to which the request belongs 435 * @tcode: transaction code 436 * @destination_id: destination node ID, consisting of bus_ID and phy_ID 437 * @generation: bus generation in which request and response are valid 438 * @speed: transmission speed 439 * @offset: 48bit wide offset into destination's address space 440 * @payload: data payload for the request subaction 441 * @length: length of the payload, in bytes 442 * @callback: function to be called when the transaction is completed 443 * @callback_data: data to be passed to the transaction completion callback 444 * 445 * A variation of __fw_send_request() to generate callback for response subaction with time stamp. 446 * 447 * The callback is invoked in the workqueue context in most cases. However, if an error is detected 448 * before queueing or the destination address refers to the local node, it is invoked in the current 449 * context instead. 450 */ 451 static inline void fw_send_request_with_tstamp(struct fw_card *card, struct fw_transaction *t, 452 int tcode, int destination_id, int generation, int speed, unsigned long long offset, 453 void *payload, size_t length, fw_transaction_callback_with_tstamp_t callback, 454 void *callback_data) 455 { 456 union fw_transaction_callback cb = { 457 .with_tstamp = callback, 458 }; 459 __fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload, 460 length, cb, true, callback_data); 461 } 462 463 int fw_cancel_transaction(struct fw_card *card, 464 struct fw_transaction *transaction); 465 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id, 466 int generation, int speed, unsigned long long offset, 467 void *payload, size_t length); 468 const char *fw_rcode_string(int rcode); 469 470 static inline int fw_stream_packet_destination_id(int tag, int channel, int sy) 471 { 472 return tag << 14 | channel << 8 | sy; 473 } 474 475 void fw_schedule_bus_reset(struct fw_card *card, bool delayed, 476 bool short_reset); 477 478 struct fw_descriptor { 479 struct list_head link; 480 size_t length; 481 u32 immediate; 482 u32 key; 483 const u32 *data; 484 }; 485 486 int fw_core_add_descriptor(struct fw_descriptor *desc); 487 void fw_core_remove_descriptor(struct fw_descriptor *desc); 488 489 /* 490 * The iso packet format allows for an immediate header/payload part 491 * stored in 'header' immediately after the packet info plus an 492 * indirect payload part that is pointer to by the 'payload' field. 493 * Applications can use one or the other or both to implement simple 494 * low-bandwidth streaming (e.g. audio) or more advanced 495 * scatter-gather streaming (e.g. assembling video frame automatically). 496 */ 497 struct fw_iso_packet { 498 u16 payload_length; /* Length of indirect payload */ 499 u32 interrupt:1; /* Generate interrupt on this packet */ 500 u32 skip:1; /* tx: Set to not send packet at all */ 501 /* rx: Sync bit, wait for matching sy */ 502 u32 tag:2; /* tx: Tag in packet header */ 503 u32 sy:4; /* tx: Sy in packet header */ 504 u32 header_length:8; /* Size of immediate header */ 505 u32 header[]; /* tx: Top of 1394 isoch. data_block */ 506 }; 507 508 #define FW_ISO_CONTEXT_TRANSMIT 0 509 #define FW_ISO_CONTEXT_RECEIVE 1 510 #define FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL 2 511 512 #define FW_ISO_CONTEXT_MATCH_TAG0 1 513 #define FW_ISO_CONTEXT_MATCH_TAG1 2 514 #define FW_ISO_CONTEXT_MATCH_TAG2 4 515 #define FW_ISO_CONTEXT_MATCH_TAG3 8 516 #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15 517 518 /* 519 * An iso buffer is just a set of pages mapped for DMA in the 520 * specified direction. Since the pages are to be used for DMA, they 521 * are not mapped into the kernel virtual address space. We store the 522 * DMA address in the page private. The helper function 523 * fw_iso_buffer_map() will map the pages into a given vma. 524 */ 525 struct fw_iso_buffer { 526 enum dma_data_direction direction; 527 struct page **pages; 528 dma_addr_t *dma_addrs; 529 int page_count; 530 }; 531 532 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card, 533 int page_count, enum dma_data_direction direction); 534 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card); 535 536 struct fw_iso_context; 537 typedef void (*fw_iso_callback_t)(struct fw_iso_context *context, 538 u32 cycle, size_t header_length, 539 void *header, void *data); 540 typedef void (*fw_iso_mc_callback_t)(struct fw_iso_context *context, 541 dma_addr_t completed, void *data); 542 543 union fw_iso_callback { 544 fw_iso_callback_t sc; 545 fw_iso_mc_callback_t mc; 546 }; 547 548 enum fw_iso_context_flag { 549 FW_ISO_CONTEXT_FLAG_DROP_OVERFLOW_HEADERS = BIT(0), 550 }; 551 552 struct fw_iso_context { 553 struct fw_card *card; 554 struct work_struct work; 555 int type; 556 int channel; 557 int speed; 558 int flags; 559 size_t header_size; 560 size_t header_storage_size; 561 union fw_iso_callback callback; 562 void *callback_data; 563 }; 564 565 struct fw_iso_context *__fw_iso_context_create(struct fw_card *card, int type, int channel, 566 int speed, size_t header_size, size_t header_storage_size, 567 union fw_iso_callback callback, void *callback_data); 568 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels); 569 int fw_iso_context_queue(struct fw_iso_context *ctx, 570 struct fw_iso_packet *packet, 571 struct fw_iso_buffer *buffer, 572 unsigned long payload); 573 void fw_iso_context_queue_flush(struct fw_iso_context *ctx); 574 int fw_iso_context_flush_completions(struct fw_iso_context *ctx); 575 576 static inline struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type, 577 int channel, int speed, size_t header_size, fw_iso_callback_t callback, 578 void *callback_data) 579 { 580 union fw_iso_callback cb = { .sc = callback }; 581 582 return __fw_iso_context_create(card, type, channel, speed, header_size, PAGE_SIZE, cb, 583 callback_data); 584 } 585 586 static inline struct fw_iso_context *fw_iso_context_create_with_header_storage_size( 587 struct fw_card *card, int type, int channel, int speed, size_t header_size, 588 size_t header_storage_size, fw_iso_callback_t callback, void *callback_data) 589 { 590 union fw_iso_callback cb = { .sc = callback }; 591 592 return __fw_iso_context_create(card, type, channel, speed, header_size, header_storage_size, 593 cb, callback_data); 594 } 595 596 /** 597 * fw_iso_context_schedule_flush_completions() - schedule work item to process isochronous context. 598 * @ctx: the isochronous context 599 * 600 * Schedule a work item on workqueue to process the isochronous context. The registered callback 601 * function is called by the worker when a queued packet buffer with the interrupt flag is 602 * completed, either after transmission in the IT context or after being filled in the IR context. 603 * The callback function is also called when the header buffer in the context becomes full, If it 604 * is required to process the context in the current context, fw_iso_context_flush_completions() is 605 * available instead. 606 * 607 * Context: Any context. 608 */ 609 static inline void fw_iso_context_schedule_flush_completions(struct fw_iso_context *ctx) 610 { 611 queue_work(ctx->card->isoc_wq, &ctx->work); 612 } 613 614 int fw_iso_context_start(struct fw_iso_context *ctx, 615 int cycle, int sync, int tags); 616 int fw_iso_context_stop(struct fw_iso_context *ctx); 617 void fw_iso_context_destroy(struct fw_iso_context *ctx); 618 void fw_iso_resource_manage(struct fw_card *card, int generation, 619 u64 channels_mask, int *channel, int *bandwidth, 620 bool allocate); 621 622 extern struct workqueue_struct *fw_workqueue; 623 624 #endif /* _LINUX_FIREWIRE_H */ 625