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