core-iso.c 12 KB

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