iso-resources.c 6.4 KB

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