1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Isochronous I/O functionality: 4 * - Isochronous DMA context management 5 * - Isochronous bus resource management (channels, bandwidth), client side 6 * 7 * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net> 8 */ 9 10 #include <linux/dma-mapping.h> 11 #include <linux/errno.h> 12 #include <linux/firewire.h> 13 #include <linux/firewire-constants.h> 14 #include <linux/kernel.h> 15 #include <linux/mm.h> 16 #include <linux/slab.h> 17 #include <linux/spinlock.h> 18 #include <linux/vmalloc.h> 19 #include <linux/export.h> 20 21 #include <asm/byteorder.h> 22 23 #include "core.h" 24 25 #include <trace/events/firewire.h> 26 27 /* 28 * Isochronous DMA context management 29 */ 30 31 int fw_iso_buffer_alloc(struct fw_iso_buffer *buffer, int page_count) 32 { 33 struct page **page_array __free(kfree) = kcalloc(page_count, sizeof(page_array[0]), GFP_KERNEL); 34 35 if (!page_array) 36 return -ENOMEM; 37 38 // Retrieve noncontiguous pages. The descriptors for 1394 OHCI isochronous DMA contexts 39 // have a set of address and length per each, while the reason to use pages is the 40 // convenience to map them into virtual address space of user process. 41 unsigned long nr_populated = alloc_pages_bulk(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO, 42 page_count, page_array); 43 if (nr_populated != page_count) { 44 // Assuming the above call fills page_array sequentially from the beginning. 45 release_pages(page_array, nr_populated); 46 return -ENOMEM; 47 } 48 49 buffer->page_count = page_count; 50 buffer->pages = no_free_ptr(page_array); 51 52 return 0; 53 } 54 55 int fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card, 56 enum dma_data_direction direction) 57 { 58 dma_addr_t *dma_addrs __free(kfree) = kcalloc(buffer->page_count, sizeof(dma_addrs[0]), 59 GFP_KERNEL); 60 int i; 61 62 if (!dma_addrs) 63 return -ENOMEM; 64 65 // Retrieve DMA mapping addresses for the pages. They are not contiguous. Maintain the cache 66 // coherency for the pages by hand. 67 for (i = 0; i < buffer->page_count; i++) { 68 // The dma_map_phys() with a physical address per page is available here, instead. 69 dma_addr_t dma_addr = dma_map_page(card->device, buffer->pages[i], 0, PAGE_SIZE, 70 direction); 71 if (dma_mapping_error(card->device, dma_addr)) 72 break; 73 74 dma_addrs[i] = dma_addr; 75 } 76 if (i < buffer->page_count) { 77 while (i-- > 0) 78 dma_unmap_page(card->device, dma_addrs[i], PAGE_SIZE, buffer->direction); 79 return -ENOMEM; 80 } 81 82 buffer->direction = direction; 83 buffer->dma_addrs = no_free_ptr(dma_addrs); 84 85 return 0; 86 } 87 88 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card, 89 int page_count, enum dma_data_direction direction) 90 { 91 int ret; 92 93 ret = fw_iso_buffer_alloc(buffer, page_count); 94 if (ret < 0) 95 return ret; 96 97 ret = fw_iso_buffer_map_dma(buffer, card, direction); 98 if (ret < 0) 99 fw_iso_buffer_destroy(buffer, card); 100 101 return ret; 102 } 103 EXPORT_SYMBOL(fw_iso_buffer_init); 104 105 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, 106 struct fw_card *card) 107 { 108 if (buffer->dma_addrs) { 109 for (int i = 0; i < buffer->page_count; ++i) { 110 dma_addr_t dma_addr = buffer->dma_addrs[i]; 111 dma_unmap_page(card->device, dma_addr, PAGE_SIZE, buffer->direction); 112 } 113 kfree(buffer->dma_addrs); 114 buffer->dma_addrs = NULL; 115 } 116 117 if (buffer->pages) { 118 release_pages(buffer->pages, buffer->page_count); 119 kfree(buffer->pages); 120 buffer->pages = NULL; 121 } 122 123 buffer->page_count = 0; 124 } 125 EXPORT_SYMBOL(fw_iso_buffer_destroy); 126 127 /* Convert DMA address to offset into virtually contiguous buffer. */ 128 size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed) 129 { 130 for (int i = 0; i < buffer->page_count; i++) { 131 dma_addr_t dma_addr = buffer->dma_addrs[i]; 132 ssize_t offset = (ssize_t)completed - (ssize_t)dma_addr; 133 if (offset > 0 && offset <= PAGE_SIZE) 134 return (i << PAGE_SHIFT) + offset; 135 } 136 137 return 0; 138 } 139 140 struct fw_iso_context *__fw_iso_context_create(struct fw_card *card, int type, int channel, 141 int speed, size_t header_size, size_t header_storage_size, 142 union fw_iso_callback callback, void *callback_data) 143 { 144 struct fw_iso_context *ctx; 145 146 ctx = card->driver->allocate_iso_context(card, type, channel, header_size, 147 header_storage_size); 148 if (IS_ERR(ctx)) 149 return ctx; 150 151 ctx->card = card; 152 ctx->type = type; 153 ctx->channel = channel; 154 ctx->speed = speed; 155 ctx->flags = 0; 156 ctx->header_size = header_size; 157 ctx->header_storage_size = header_storage_size; 158 ctx->callback = callback; 159 ctx->callback_data = callback_data; 160 161 trace_isoc_outbound_allocate(ctx, channel, speed); 162 trace_isoc_inbound_single_allocate(ctx, channel, header_size); 163 trace_isoc_inbound_multiple_allocate(ctx); 164 165 return ctx; 166 } 167 EXPORT_SYMBOL(__fw_iso_context_create); 168 169 void fw_iso_context_destroy(struct fw_iso_context *ctx) 170 { 171 trace_isoc_outbound_destroy(ctx); 172 trace_isoc_inbound_single_destroy(ctx); 173 trace_isoc_inbound_multiple_destroy(ctx); 174 175 ctx->card->driver->free_iso_context(ctx); 176 } 177 EXPORT_SYMBOL(fw_iso_context_destroy); 178 179 int fw_iso_context_start(struct fw_iso_context *ctx, 180 int cycle, int sync, int tags) 181 { 182 trace_isoc_outbound_start(ctx, cycle); 183 trace_isoc_inbound_single_start(ctx, cycle, sync, tags); 184 trace_isoc_inbound_multiple_start(ctx, cycle, sync, tags); 185 186 return ctx->card->driver->start_iso(ctx, cycle, sync, tags); 187 } 188 EXPORT_SYMBOL(fw_iso_context_start); 189 190 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels) 191 { 192 trace_isoc_inbound_multiple_channels(ctx, *channels); 193 194 return ctx->card->driver->set_iso_channels(ctx, channels); 195 } 196 197 int fw_iso_context_queue(struct fw_iso_context *ctx, 198 struct fw_iso_packet *packet, 199 struct fw_iso_buffer *buffer, 200 unsigned long payload) 201 { 202 trace_isoc_outbound_queue(ctx, payload, packet); 203 trace_isoc_inbound_single_queue(ctx, payload, packet); 204 trace_isoc_inbound_multiple_queue(ctx, payload, packet); 205 206 return ctx->card->driver->queue_iso(ctx, packet, buffer, payload); 207 } 208 EXPORT_SYMBOL(fw_iso_context_queue); 209 210 void fw_iso_context_queue_flush(struct fw_iso_context *ctx) 211 { 212 trace_isoc_outbound_flush(ctx); 213 trace_isoc_inbound_single_flush(ctx); 214 trace_isoc_inbound_multiple_flush(ctx); 215 216 ctx->card->driver->flush_queue_iso(ctx); 217 } 218 EXPORT_SYMBOL(fw_iso_context_queue_flush); 219 220 /** 221 * fw_iso_context_flush_completions() - process isochronous context in current process context. 222 * @ctx: the isochronous context 223 * 224 * Process the isochronous context in the current process context. The registered callback function 225 * is called when a queued packet buffer with the interrupt flag is completed, either after 226 * transmission in the IT context or after being filled in the IR context. Additionally, the 227 * callback function is also called for the packet buffer completed at last. Furthermore, the 228 * callback function is called as well when the header buffer in the context becomes full. If it is 229 * required to process the context asynchronously, fw_iso_context_schedule_flush_completions() is 230 * available instead. 231 * 232 * Context: Process context. May sleep due to disable_work_sync(). 233 */ 234 int fw_iso_context_flush_completions(struct fw_iso_context *ctx) 235 { 236 int err; 237 238 trace_isoc_outbound_flush_completions(ctx); 239 trace_isoc_inbound_single_flush_completions(ctx); 240 trace_isoc_inbound_multiple_flush_completions(ctx); 241 242 might_sleep(); 243 244 // Avoid dead lock due to programming mistake. 245 if (WARN_ON_ONCE(current_work() == &ctx->work)) 246 return 0; 247 248 disable_work_sync(&ctx->work); 249 250 err = ctx->card->driver->flush_iso_completions(ctx); 251 252 enable_work(&ctx->work); 253 254 return err; 255 } 256 EXPORT_SYMBOL(fw_iso_context_flush_completions); 257 258 int fw_iso_context_stop(struct fw_iso_context *ctx) 259 { 260 int err; 261 262 trace_isoc_outbound_stop(ctx); 263 trace_isoc_inbound_single_stop(ctx); 264 trace_isoc_inbound_multiple_stop(ctx); 265 266 might_sleep(); 267 268 // Avoid dead lock due to programming mistake. 269 if (WARN_ON_ONCE(current_work() == &ctx->work)) 270 return 0; 271 272 err = ctx->card->driver->stop_iso(ctx); 273 274 cancel_work_sync(&ctx->work); 275 276 return err; 277 } 278 EXPORT_SYMBOL(fw_iso_context_stop); 279 280 /* 281 * Isochronous bus resource management (channels, bandwidth), client side 282 */ 283 284 static int manage_bandwidth(struct fw_card *card, int irm_id, int generation, 285 int bandwidth, bool allocate) 286 { 287 int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0; 288 __be32 data[2]; 289 290 /* 291 * On a 1394a IRM with low contention, try < 1 is enough. 292 * On a 1394-1995 IRM, we need at least try < 2. 293 * Let's just do try < 5. 294 */ 295 for (try = 0; try < 5; try++) { 296 new = allocate ? old - bandwidth : old + bandwidth; 297 if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL) 298 return -EBUSY; 299 300 data[0] = cpu_to_be32(old); 301 data[1] = cpu_to_be32(new); 302 switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, 303 irm_id, generation, SCODE_100, 304 CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE, 305 data, 8)) { 306 case RCODE_GENERATION: 307 /* A generation change frees all bandwidth. */ 308 return allocate ? -EAGAIN : bandwidth; 309 310 case RCODE_COMPLETE: 311 if (be32_to_cpup(data) == old) 312 return bandwidth; 313 314 old = be32_to_cpup(data); 315 /* Fall through. */ 316 } 317 } 318 319 return -EIO; 320 } 321 322 static int manage_channel(struct fw_card *card, int irm_id, int generation, 323 u32 channels_mask, u64 offset, bool allocate) 324 { 325 __be32 bit, all, old; 326 __be32 data[2]; 327 int channel, ret = -EIO, retry = 5; 328 329 old = all = allocate ? cpu_to_be32(~0) : 0; 330 331 for (channel = 0; channel < 32; channel++) { 332 if (!(channels_mask & 1 << channel)) 333 continue; 334 335 ret = -EBUSY; 336 337 bit = cpu_to_be32(1 << (31 - channel)); 338 if ((old & bit) != (all & bit)) 339 continue; 340 341 data[0] = old; 342 data[1] = old ^ bit; 343 switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, 344 irm_id, generation, SCODE_100, 345 offset, data, 8)) { 346 case RCODE_GENERATION: 347 /* A generation change frees all channels. */ 348 return allocate ? -EAGAIN : channel; 349 350 case RCODE_COMPLETE: 351 if (data[0] == old) 352 return channel; 353 354 old = data[0]; 355 356 /* Is the IRM 1394a-2000 compliant? */ 357 if ((data[0] & bit) == (data[1] & bit)) 358 continue; 359 360 fallthrough; /* It's a 1394-1995 IRM, retry */ 361 default: 362 if (retry) { 363 retry--; 364 channel--; 365 } else { 366 ret = -EIO; 367 } 368 } 369 } 370 371 return ret; 372 } 373 374 static void deallocate_channel(struct fw_card *card, int irm_id, 375 int generation, int channel) 376 { 377 u32 mask; 378 u64 offset; 379 380 mask = channel < 32 ? 1 << channel : 1 << (channel - 32); 381 offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI : 382 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO; 383 384 manage_channel(card, irm_id, generation, mask, offset, false); 385 } 386 387 /** 388 * fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth 389 * @card: card interface for this action 390 * @generation: bus generation 391 * @channels_mask: bitmask for channel allocation 392 * @channel: pointer for returning channel allocation result 393 * @bandwidth: pointer for returning bandwidth allocation result 394 * @allocate: whether to allocate (true) or deallocate (false) 395 * 396 * In parameters: card, generation, channels_mask, bandwidth, allocate 397 * Out parameters: channel, bandwidth 398 * 399 * This function blocks (sleeps) during communication with the IRM. 400 * 401 * Allocates or deallocates at most one channel out of channels_mask. 402 * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0. 403 * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for 404 * channel 0 and LSB for channel 63.) 405 * Allocates or deallocates as many bandwidth allocation units as specified. 406 * 407 * Returns channel < 0 if no channel was allocated or deallocated. 408 * Returns bandwidth = 0 if no bandwidth was allocated or deallocated. 409 * 410 * If generation is stale, deallocations succeed but allocations fail with 411 * channel = -EAGAIN. 412 * 413 * If channel allocation fails, no bandwidth will be allocated either. 414 * If bandwidth allocation fails, no channel will be allocated either. 415 * But deallocations of channel and bandwidth are tried independently 416 * of each other's success. 417 */ 418 void fw_iso_resource_manage(struct fw_card *card, int generation, 419 u64 channels_mask, int *channel, int *bandwidth, 420 bool allocate) 421 { 422 u32 channels_hi = channels_mask; /* channels 31...0 */ 423 u32 channels_lo = channels_mask >> 32; /* channels 63...32 */ 424 int irm_id, ret, c = -EINVAL; 425 426 scoped_guard(spinlock_irq, &card->lock) 427 irm_id = card->irm_node->node_id; 428 429 if (channels_hi) 430 c = manage_channel(card, irm_id, generation, channels_hi, 431 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI, 432 allocate); 433 if (channels_lo && c < 0) { 434 c = manage_channel(card, irm_id, generation, channels_lo, 435 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO, 436 allocate); 437 if (c >= 0) 438 c += 32; 439 } 440 *channel = c; 441 442 if (allocate && channels_mask != 0 && c < 0) 443 *bandwidth = 0; 444 445 if (*bandwidth == 0) 446 return; 447 448 ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate); 449 if (ret < 0) 450 *bandwidth = 0; 451 452 if (allocate && ret < 0) { 453 if (c >= 0) 454 deallocate_channel(card, irm_id, generation, c); 455 *channel = ret; 456 } 457 } 458 EXPORT_SYMBOL(fw_iso_resource_manage); 459