cmp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Connection Management Procedures (IEC 61883-1) 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/module.h>
  11. #include <linux/sched.h>
  12. #include "lib.h"
  13. #include "iso-resources.h"
  14. #include "cmp.h"
  15. #define IMPR_SPEED_MASK 0xc0000000
  16. #define IMPR_SPEED_SHIFT 30
  17. #define IMPR_XSPEED_MASK 0x00000060
  18. #define IMPR_XSPEED_SHIFT 5
  19. #define IMPR_PLUGS_MASK 0x0000001f
  20. #define IPCR_ONLINE 0x80000000
  21. #define IPCR_BCAST_CONN 0x40000000
  22. #define IPCR_P2P_CONN_MASK 0x3f000000
  23. #define IPCR_P2P_CONN_SHIFT 24
  24. #define IPCR_CHANNEL_MASK 0x003f0000
  25. #define IPCR_CHANNEL_SHIFT 16
  26. enum bus_reset_handling {
  27. ABORT_ON_BUS_RESET,
  28. SUCCEED_ON_BUS_RESET,
  29. };
  30. static __attribute__((format(printf, 2, 3)))
  31. void cmp_error(struct cmp_connection *c, const char *fmt, ...)
  32. {
  33. va_list va;
  34. va_start(va, fmt);
  35. dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
  36. 'i', c->pcr_index, &(struct va_format){ fmt, &va });
  37. va_end(va);
  38. }
  39. static int pcr_modify(struct cmp_connection *c,
  40. __be32 (*modify)(struct cmp_connection *c, __be32 old),
  41. int (*check)(struct cmp_connection *c, __be32 pcr),
  42. enum bus_reset_handling bus_reset_handling)
  43. {
  44. struct fw_device *device = fw_parent_device(c->resources.unit);
  45. int generation = c->resources.generation;
  46. int rcode, errors = 0;
  47. __be32 old_arg, buffer[2];
  48. int err;
  49. buffer[0] = c->last_pcr_value;
  50. for (;;) {
  51. old_arg = buffer[0];
  52. buffer[1] = modify(c, buffer[0]);
  53. rcode = fw_run_transaction(
  54. device->card, TCODE_LOCK_COMPARE_SWAP,
  55. device->node_id, generation, device->max_speed,
  56. CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index),
  57. buffer, 8);
  58. if (rcode == RCODE_COMPLETE) {
  59. if (buffer[0] == old_arg) /* success? */
  60. break;
  61. if (check) {
  62. err = check(c, buffer[0]);
  63. if (err < 0)
  64. return err;
  65. }
  66. } else if (rcode == RCODE_GENERATION)
  67. goto bus_reset;
  68. else if (rcode_is_permanent_error(rcode) || ++errors >= 3)
  69. goto io_error;
  70. }
  71. c->last_pcr_value = buffer[1];
  72. return 0;
  73. io_error:
  74. cmp_error(c, "transaction failed: %s\n", rcode_string(rcode));
  75. return -EIO;
  76. bus_reset:
  77. return bus_reset_handling == ABORT_ON_BUS_RESET ? -EAGAIN : 0;
  78. }
  79. /**
  80. * cmp_connection_init - initializes a connection manager
  81. * @c: the connection manager to initialize
  82. * @unit: a unit of the target device
  83. * @ipcr_index: the index of the iPCR on the target device
  84. */
  85. int cmp_connection_init(struct cmp_connection *c,
  86. struct fw_unit *unit,
  87. unsigned int ipcr_index)
  88. {
  89. __be32 impr_be;
  90. u32 impr;
  91. int err;
  92. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  93. CSR_REGISTER_BASE + CSR_IMPR,
  94. &impr_be, 4);
  95. if (err < 0)
  96. return err;
  97. impr = be32_to_cpu(impr_be);
  98. if (ipcr_index >= (impr & IMPR_PLUGS_MASK))
  99. return -EINVAL;
  100. err = fw_iso_resources_init(&c->resources, unit);
  101. if (err < 0)
  102. return err;
  103. c->connected = false;
  104. mutex_init(&c->mutex);
  105. c->last_pcr_value = cpu_to_be32(0x80000000);
  106. c->pcr_index = ipcr_index;
  107. c->max_speed = (impr & IMPR_SPEED_MASK) >> IMPR_SPEED_SHIFT;
  108. if (c->max_speed == SCODE_BETA)
  109. c->max_speed += (impr & IMPR_XSPEED_MASK) >> IMPR_XSPEED_SHIFT;
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(cmp_connection_init);
  113. /**
  114. * cmp_connection_destroy - free connection manager resources
  115. * @c: the connection manager
  116. */
  117. void cmp_connection_destroy(struct cmp_connection *c)
  118. {
  119. WARN_ON(c->connected);
  120. mutex_destroy(&c->mutex);
  121. fw_iso_resources_destroy(&c->resources);
  122. }
  123. EXPORT_SYMBOL(cmp_connection_destroy);
  124. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  125. {
  126. ipcr &= ~cpu_to_be32(IPCR_BCAST_CONN |
  127. IPCR_P2P_CONN_MASK |
  128. IPCR_CHANNEL_MASK);
  129. ipcr |= cpu_to_be32(1 << IPCR_P2P_CONN_SHIFT);
  130. ipcr |= cpu_to_be32(c->resources.channel << IPCR_CHANNEL_SHIFT);
  131. return ipcr;
  132. }
  133. static int ipcr_set_check(struct cmp_connection *c, __be32 ipcr)
  134. {
  135. if (ipcr & cpu_to_be32(IPCR_BCAST_CONN |
  136. IPCR_P2P_CONN_MASK)) {
  137. cmp_error(c, "plug is already in use\n");
  138. return -EBUSY;
  139. }
  140. if (!(ipcr & cpu_to_be32(IPCR_ONLINE))) {
  141. cmp_error(c, "plug is not on-line\n");
  142. return -ECONNREFUSED;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * cmp_connection_establish - establish a connection to the target
  148. * @c: the connection manager
  149. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  150. *
  151. * This function establishes a point-to-point connection from the local
  152. * computer to the target by allocating isochronous resources (channel and
  153. * bandwidth) and setting the target's input plug control register. When this
  154. * function succeeds, the caller is responsible for starting transmitting
  155. * packets.
  156. */
  157. int cmp_connection_establish(struct cmp_connection *c,
  158. unsigned int max_payload_bytes)
  159. {
  160. int err;
  161. if (WARN_ON(c->connected))
  162. return -EISCONN;
  163. c->speed = min(c->max_speed,
  164. fw_parent_device(c->resources.unit)->max_speed);
  165. mutex_lock(&c->mutex);
  166. retry_after_bus_reset:
  167. err = fw_iso_resources_allocate(&c->resources,
  168. max_payload_bytes, c->speed);
  169. if (err < 0)
  170. goto err_mutex;
  171. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  172. ABORT_ON_BUS_RESET);
  173. if (err == -EAGAIN) {
  174. fw_iso_resources_free(&c->resources);
  175. goto retry_after_bus_reset;
  176. }
  177. if (err < 0)
  178. goto err_resources;
  179. c->connected = true;
  180. mutex_unlock(&c->mutex);
  181. return 0;
  182. err_resources:
  183. fw_iso_resources_free(&c->resources);
  184. err_mutex:
  185. mutex_unlock(&c->mutex);
  186. return err;
  187. }
  188. EXPORT_SYMBOL(cmp_connection_establish);
  189. /**
  190. * cmp_connection_update - update the connection after a bus reset
  191. * @c: the connection manager
  192. *
  193. * This function must be called from the driver's .update handler to reestablish
  194. * any connection that might have been active.
  195. *
  196. * Returns zero on success, or a negative error code. On an error, the
  197. * connection is broken and the caller must stop transmitting iso packets.
  198. */
  199. int cmp_connection_update(struct cmp_connection *c)
  200. {
  201. int err;
  202. mutex_lock(&c->mutex);
  203. if (!c->connected) {
  204. mutex_unlock(&c->mutex);
  205. return 0;
  206. }
  207. err = fw_iso_resources_update(&c->resources);
  208. if (err < 0)
  209. goto err_unconnect;
  210. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  211. SUCCEED_ON_BUS_RESET);
  212. if (err < 0)
  213. goto err_resources;
  214. mutex_unlock(&c->mutex);
  215. return 0;
  216. err_resources:
  217. fw_iso_resources_free(&c->resources);
  218. err_unconnect:
  219. c->connected = false;
  220. mutex_unlock(&c->mutex);
  221. return err;
  222. }
  223. EXPORT_SYMBOL(cmp_connection_update);
  224. static __be32 ipcr_break_modify(struct cmp_connection *c, __be32 ipcr)
  225. {
  226. return ipcr & ~cpu_to_be32(IPCR_BCAST_CONN | IPCR_P2P_CONN_MASK);
  227. }
  228. /**
  229. * cmp_connection_break - break the connection to the target
  230. * @c: the connection manager
  231. *
  232. * This function deactives the connection in the target's input plug control
  233. * register, and frees the isochronous resources of the connection. Before
  234. * calling this function, the caller should cease transmitting packets.
  235. */
  236. void cmp_connection_break(struct cmp_connection *c)
  237. {
  238. int err;
  239. mutex_lock(&c->mutex);
  240. if (!c->connected) {
  241. mutex_unlock(&c->mutex);
  242. return;
  243. }
  244. err = pcr_modify(c, ipcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  245. if (err < 0)
  246. cmp_error(c, "plug is still connected\n");
  247. fw_iso_resources_free(&c->resources);
  248. c->connected = false;
  249. mutex_unlock(&c->mutex);
  250. }
  251. EXPORT_SYMBOL(cmp_connection_break);