iso-resources.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * isochronous resources helper functions
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/firewire.h>
  9. #include <linux/firewire-constants.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/mutex.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include "iso-resources.h"
  16. /**
  17. * fw_iso_resources_init - initializes a &struct fw_iso_resources
  18. * @r: the resource manager to initialize
  19. * @unit: the device unit for which the resources will be needed
  20. *
  21. * If the device does not support all channel numbers, change @r->channels_mask
  22. * after calling this function.
  23. */
  24. int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
  25. {
  26. r->buffer = kmalloc(2 * 4, GFP_KERNEL);
  27. if (!r->buffer)
  28. return -ENOMEM;
  29. r->channels_mask = ~0uLL;
  30. r->unit = fw_unit_get(unit);
  31. mutex_init(&r->mutex);
  32. r->allocated = false;
  33. return 0;
  34. }
  35. EXPORT_SYMBOL(fw_iso_resources_init);
  36. /**
  37. * fw_iso_resources_destroy - destroy a resource manager
  38. * @r: the resource manager that is no longer needed
  39. */
  40. void fw_iso_resources_destroy(struct fw_iso_resources *r)
  41. {
  42. WARN_ON(r->allocated);
  43. kfree(r->buffer);
  44. mutex_destroy(&r->mutex);
  45. fw_unit_put(r->unit);
  46. }
  47. EXPORT_SYMBOL(fw_iso_resources_destroy);
  48. static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
  49. {
  50. unsigned int bytes, s400_bytes;
  51. /* iso packets have three header quadlets and quadlet-aligned payload */
  52. bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
  53. /* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
  54. if (speed <= SCODE_400)
  55. s400_bytes = bytes * (1 << (SCODE_400 - speed));
  56. else
  57. s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
  58. return s400_bytes;
  59. }
  60. static int current_bandwidth_overhead(struct fw_card *card)
  61. {
  62. /*
  63. * Under the usual pessimistic assumption (cable length 4.5 m), the
  64. * isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
  65. * 88.3 + N * 24.3 in bandwidth units.
  66. *
  67. * The calculation below tries to deduce N from the current gap count.
  68. * If the gap count has been optimized by measuring the actual packet
  69. * transmission time, this derived overhead should be near the actual
  70. * overhead as well.
  71. */
  72. return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
  73. }
  74. static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
  75. {
  76. for (;;) {
  77. s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
  78. if (delay <= 0)
  79. return 0;
  80. if (schedule_timeout_interruptible(delay) > 0)
  81. return -ERESTARTSYS;
  82. }
  83. }
  84. /**
  85. * fw_iso_resources_allocate - allocate isochronous channel and bandwidth
  86. * @r: the resource manager
  87. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  88. * @speed: the speed (e.g., SCODE_400) at which the packets will be sent
  89. *
  90. * This function allocates one isochronous channel and enough bandwidth for the
  91. * specified packet size.
  92. *
  93. * Returns the channel number that the caller must use for streaming, or
  94. * a negative error code. Due to potentionally long delays, this function is
  95. * interruptible and can return -ERESTARTSYS. On success, the caller is
  96. * responsible for calling fw_iso_resources_update() on bus resets, and
  97. * fw_iso_resources_free() when the resources are not longer needed.
  98. */
  99. int fw_iso_resources_allocate(struct fw_iso_resources *r,
  100. unsigned int max_payload_bytes, int speed)
  101. {
  102. struct fw_card *card = fw_parent_device(r->unit)->card;
  103. int bandwidth, channel, err;
  104. if (WARN_ON(r->allocated))
  105. return -EBADFD;
  106. r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
  107. retry_after_bus_reset:
  108. spin_lock_irq(&card->lock);
  109. r->generation = card->generation;
  110. r->bandwidth_overhead = current_bandwidth_overhead(card);
  111. spin_unlock_irq(&card->lock);
  112. err = wait_isoch_resource_delay_after_bus_reset(card);
  113. if (err < 0)
  114. return err;
  115. mutex_lock(&r->mutex);
  116. bandwidth = r->bandwidth + r->bandwidth_overhead;
  117. fw_iso_resource_manage(card, r->generation, r->channels_mask,
  118. &channel, &bandwidth, true, r->buffer);
  119. if (channel == -EAGAIN) {
  120. mutex_unlock(&r->mutex);
  121. goto retry_after_bus_reset;
  122. }
  123. if (channel >= 0) {
  124. r->channel = channel;
  125. r->allocated = true;
  126. } else {
  127. if (channel == -EBUSY)
  128. dev_err(&r->unit->device,
  129. "isochronous resources exhausted\n");
  130. else
  131. dev_err(&r->unit->device,
  132. "isochronous resource allocation failed\n");
  133. }
  134. mutex_unlock(&r->mutex);
  135. return channel;
  136. }
  137. EXPORT_SYMBOL(fw_iso_resources_allocate);
  138. /**
  139. * fw_iso_resources_update - update resource allocations after a bus reset
  140. * @r: the resource manager
  141. *
  142. * This function must be called from the driver's .update handler to reallocate
  143. * any resources that were allocated before the bus reset. It is safe to call
  144. * this function if no resources are currently allocated.
  145. *
  146. * Returns a negative error code on failure. If this happens, the caller must
  147. * stop streaming.
  148. */
  149. int fw_iso_resources_update(struct fw_iso_resources *r)
  150. {
  151. struct fw_card *card = fw_parent_device(r->unit)->card;
  152. int bandwidth, channel;
  153. mutex_lock(&r->mutex);
  154. if (!r->allocated) {
  155. mutex_unlock(&r->mutex);
  156. return 0;
  157. }
  158. spin_lock_irq(&card->lock);
  159. r->generation = card->generation;
  160. r->bandwidth_overhead = current_bandwidth_overhead(card);
  161. spin_unlock_irq(&card->lock);
  162. bandwidth = r->bandwidth + r->bandwidth_overhead;
  163. fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
  164. &channel, &bandwidth, true, r->buffer);
  165. /*
  166. * When another bus reset happens, pretend that the allocation
  167. * succeeded; we will try again for the new generation later.
  168. */
  169. if (channel < 0 && channel != -EAGAIN) {
  170. r->allocated = false;
  171. if (channel == -EBUSY)
  172. dev_err(&r->unit->device,
  173. "isochronous resources exhausted\n");
  174. else
  175. dev_err(&r->unit->device,
  176. "isochronous resource allocation failed\n");
  177. }
  178. mutex_unlock(&r->mutex);
  179. return channel;
  180. }
  181. EXPORT_SYMBOL(fw_iso_resources_update);
  182. /**
  183. * fw_iso_resources_free - frees allocated resources
  184. * @r: the resource manager
  185. *
  186. * This function deallocates the channel and bandwidth, if allocated.
  187. */
  188. void fw_iso_resources_free(struct fw_iso_resources *r)
  189. {
  190. struct fw_card *card = fw_parent_device(r->unit)->card;
  191. int bandwidth, channel;
  192. mutex_lock(&r->mutex);
  193. if (r->allocated) {
  194. bandwidth = r->bandwidth + r->bandwidth_overhead;
  195. fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
  196. &channel, &bandwidth, false, r->buffer);
  197. if (channel < 0)
  198. dev_err(&r->unit->device,
  199. "isochronous resource deallocation failed\n");
  200. r->allocated = false;
  201. }
  202. mutex_unlock(&r->mutex);
  203. }
  204. EXPORT_SYMBOL(fw_iso_resources_free);