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