iso-resources.c 6.5 KB

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