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