cmp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /* MPR common fields */
  16. #define MPR_SPEED_MASK 0xc0000000
  17. #define MPR_SPEED_SHIFT 30
  18. #define MPR_XSPEED_MASK 0x00000060
  19. #define MPR_XSPEED_SHIFT 5
  20. #define MPR_PLUGS_MASK 0x0000001f
  21. /* PCR common fields */
  22. #define PCR_ONLINE 0x80000000
  23. #define PCR_BCAST_CONN 0x40000000
  24. #define PCR_P2P_CONN_MASK 0x3f000000
  25. #define PCR_P2P_CONN_SHIFT 24
  26. #define PCR_CHANNEL_MASK 0x003f0000
  27. #define PCR_CHANNEL_SHIFT 16
  28. /* oPCR specific fields */
  29. #define OPCR_XSPEED_MASK 0x00C00000
  30. #define OPCR_XSPEED_SHIFT 22
  31. #define OPCR_SPEED_MASK 0x0000C000
  32. #define OPCR_SPEED_SHIFT 14
  33. #define OPCR_OVERHEAD_ID_MASK 0x00003C00
  34. #define OPCR_OVERHEAD_ID_SHIFT 10
  35. enum bus_reset_handling {
  36. ABORT_ON_BUS_RESET,
  37. SUCCEED_ON_BUS_RESET,
  38. };
  39. static __printf(2, 3)
  40. void cmp_error(struct cmp_connection *c, const char *fmt, ...)
  41. {
  42. va_list va;
  43. va_start(va, fmt);
  44. dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
  45. (c->direction == CMP_INPUT) ? 'i' : 'o',
  46. c->pcr_index, &(struct va_format){ fmt, &va });
  47. va_end(va);
  48. }
  49. static u64 mpr_address(struct cmp_connection *c)
  50. {
  51. if (c->direction == CMP_INPUT)
  52. return CSR_REGISTER_BASE + CSR_IMPR;
  53. else
  54. return CSR_REGISTER_BASE + CSR_OMPR;
  55. }
  56. static u64 pcr_address(struct cmp_connection *c)
  57. {
  58. if (c->direction == CMP_INPUT)
  59. return CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index);
  60. else
  61. return CSR_REGISTER_BASE + CSR_OPCR(c->pcr_index);
  62. }
  63. static int pcr_modify(struct cmp_connection *c,
  64. __be32 (*modify)(struct cmp_connection *c, __be32 old),
  65. int (*check)(struct cmp_connection *c, __be32 pcr),
  66. enum bus_reset_handling bus_reset_handling)
  67. {
  68. __be32 old_arg, buffer[2];
  69. int err;
  70. buffer[0] = c->last_pcr_value;
  71. for (;;) {
  72. old_arg = buffer[0];
  73. buffer[1] = modify(c, buffer[0]);
  74. err = snd_fw_transaction(
  75. c->resources.unit, TCODE_LOCK_COMPARE_SWAP,
  76. pcr_address(c), buffer, 8,
  77. FW_FIXED_GENERATION | c->resources.generation);
  78. if (err < 0) {
  79. if (err == -EAGAIN &&
  80. bus_reset_handling == SUCCEED_ON_BUS_RESET)
  81. err = 0;
  82. return err;
  83. }
  84. if (buffer[0] == old_arg) /* success? */
  85. break;
  86. if (check) {
  87. err = check(c, buffer[0]);
  88. if (err < 0)
  89. return err;
  90. }
  91. }
  92. c->last_pcr_value = buffer[1];
  93. return 0;
  94. }
  95. /**
  96. * cmp_connection_init - initializes a connection manager
  97. * @c: the connection manager to initialize
  98. * @unit: a unit of the target device
  99. * @pcr_index: the index of the iPCR/oPCR on the target device
  100. */
  101. int cmp_connection_init(struct cmp_connection *c,
  102. struct fw_unit *unit,
  103. enum cmp_direction direction,
  104. unsigned int pcr_index)
  105. {
  106. __be32 mpr_be;
  107. u32 mpr;
  108. int err;
  109. c->direction = direction;
  110. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  111. mpr_address(c), &mpr_be, 4, 0);
  112. if (err < 0)
  113. return err;
  114. mpr = be32_to_cpu(mpr_be);
  115. if (pcr_index >= (mpr & MPR_PLUGS_MASK))
  116. return -EINVAL;
  117. err = fw_iso_resources_init(&c->resources, unit);
  118. if (err < 0)
  119. return err;
  120. c->connected = false;
  121. mutex_init(&c->mutex);
  122. c->last_pcr_value = cpu_to_be32(0x80000000);
  123. c->pcr_index = pcr_index;
  124. c->max_speed = (mpr & MPR_SPEED_MASK) >> MPR_SPEED_SHIFT;
  125. if (c->max_speed == SCODE_BETA)
  126. c->max_speed += (mpr & MPR_XSPEED_MASK) >> MPR_XSPEED_SHIFT;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(cmp_connection_init);
  130. /**
  131. * cmp_connection_check_used - check connection is already esablished or not
  132. * @c: the connection manager to be checked
  133. */
  134. int cmp_connection_check_used(struct cmp_connection *c, bool *used)
  135. {
  136. __be32 pcr;
  137. int err;
  138. err = snd_fw_transaction(
  139. c->resources.unit, TCODE_READ_QUADLET_REQUEST,
  140. pcr_address(c), &pcr, 4, 0);
  141. if (err >= 0)
  142. *used = !!(pcr & cpu_to_be32(PCR_BCAST_CONN |
  143. PCR_P2P_CONN_MASK));
  144. return err;
  145. }
  146. EXPORT_SYMBOL(cmp_connection_check_used);
  147. /**
  148. * cmp_connection_destroy - free connection manager resources
  149. * @c: the connection manager
  150. */
  151. void cmp_connection_destroy(struct cmp_connection *c)
  152. {
  153. WARN_ON(c->connected);
  154. mutex_destroy(&c->mutex);
  155. fw_iso_resources_destroy(&c->resources);
  156. }
  157. EXPORT_SYMBOL(cmp_connection_destroy);
  158. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  159. {
  160. ipcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  161. PCR_P2P_CONN_MASK |
  162. PCR_CHANNEL_MASK);
  163. ipcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  164. ipcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  165. return ipcr;
  166. }
  167. static int get_overhead_id(struct cmp_connection *c)
  168. {
  169. int id;
  170. /*
  171. * apply "oPCR overhead ID encoding"
  172. * the encoding table can convert up to 512.
  173. * here the value over 512 is converted as the same way as 512.
  174. */
  175. for (id = 1; id < 16; id++) {
  176. if (c->resources.bandwidth_overhead < (id << 5))
  177. break;
  178. }
  179. if (id == 16)
  180. id = 0;
  181. return id;
  182. }
  183. static __be32 opcr_set_modify(struct cmp_connection *c, __be32 opcr)
  184. {
  185. unsigned int spd, xspd;
  186. /* generate speed and extended speed field value */
  187. if (c->speed > SCODE_400) {
  188. spd = SCODE_800;
  189. xspd = c->speed - SCODE_800;
  190. } else {
  191. spd = c->speed;
  192. xspd = 0;
  193. }
  194. opcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  195. PCR_P2P_CONN_MASK |
  196. OPCR_XSPEED_MASK |
  197. PCR_CHANNEL_MASK |
  198. OPCR_SPEED_MASK |
  199. OPCR_OVERHEAD_ID_MASK);
  200. opcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  201. opcr |= cpu_to_be32(xspd << OPCR_XSPEED_SHIFT);
  202. opcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  203. opcr |= cpu_to_be32(spd << OPCR_SPEED_SHIFT);
  204. opcr |= cpu_to_be32(get_overhead_id(c) << OPCR_OVERHEAD_ID_SHIFT);
  205. return opcr;
  206. }
  207. static int pcr_set_check(struct cmp_connection *c, __be32 pcr)
  208. {
  209. if (pcr & cpu_to_be32(PCR_BCAST_CONN |
  210. PCR_P2P_CONN_MASK)) {
  211. cmp_error(c, "plug is already in use\n");
  212. return -EBUSY;
  213. }
  214. if (!(pcr & cpu_to_be32(PCR_ONLINE))) {
  215. cmp_error(c, "plug is not on-line\n");
  216. return -ECONNREFUSED;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * cmp_connection_establish - establish a connection to the target
  222. * @c: the connection manager
  223. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  224. *
  225. * This function establishes a point-to-point connection from the local
  226. * computer to the target by allocating isochronous resources (channel and
  227. * bandwidth) and setting the target's input/output plug control register.
  228. * When this function succeeds, the caller is responsible for starting
  229. * transmitting packets.
  230. */
  231. int cmp_connection_establish(struct cmp_connection *c,
  232. unsigned int max_payload_bytes)
  233. {
  234. int err;
  235. if (WARN_ON(c->connected))
  236. return -EISCONN;
  237. c->speed = min(c->max_speed,
  238. fw_parent_device(c->resources.unit)->max_speed);
  239. mutex_lock(&c->mutex);
  240. retry_after_bus_reset:
  241. err = fw_iso_resources_allocate(&c->resources,
  242. max_payload_bytes, c->speed);
  243. if (err < 0)
  244. goto err_mutex;
  245. if (c->direction == CMP_OUTPUT)
  246. err = pcr_modify(c, opcr_set_modify, pcr_set_check,
  247. ABORT_ON_BUS_RESET);
  248. else
  249. err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
  250. ABORT_ON_BUS_RESET);
  251. if (err == -EAGAIN) {
  252. fw_iso_resources_free(&c->resources);
  253. goto retry_after_bus_reset;
  254. }
  255. if (err < 0)
  256. goto err_resources;
  257. c->connected = true;
  258. mutex_unlock(&c->mutex);
  259. return 0;
  260. err_resources:
  261. fw_iso_resources_free(&c->resources);
  262. err_mutex:
  263. mutex_unlock(&c->mutex);
  264. return err;
  265. }
  266. EXPORT_SYMBOL(cmp_connection_establish);
  267. /**
  268. * cmp_connection_update - update the connection after a bus reset
  269. * @c: the connection manager
  270. *
  271. * This function must be called from the driver's .update handler to
  272. * reestablish any connection that might have been active.
  273. *
  274. * Returns zero on success, or a negative error code. On an error, the
  275. * connection is broken and the caller must stop transmitting iso packets.
  276. */
  277. int cmp_connection_update(struct cmp_connection *c)
  278. {
  279. int err;
  280. mutex_lock(&c->mutex);
  281. if (!c->connected) {
  282. mutex_unlock(&c->mutex);
  283. return 0;
  284. }
  285. err = fw_iso_resources_update(&c->resources);
  286. if (err < 0)
  287. goto err_unconnect;
  288. if (c->direction == CMP_OUTPUT)
  289. err = pcr_modify(c, opcr_set_modify, pcr_set_check,
  290. SUCCEED_ON_BUS_RESET);
  291. else
  292. err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
  293. SUCCEED_ON_BUS_RESET);
  294. if (err < 0)
  295. goto err_resources;
  296. mutex_unlock(&c->mutex);
  297. return 0;
  298. err_resources:
  299. fw_iso_resources_free(&c->resources);
  300. err_unconnect:
  301. c->connected = false;
  302. mutex_unlock(&c->mutex);
  303. return err;
  304. }
  305. EXPORT_SYMBOL(cmp_connection_update);
  306. static __be32 pcr_break_modify(struct cmp_connection *c, __be32 pcr)
  307. {
  308. return pcr & ~cpu_to_be32(PCR_BCAST_CONN | PCR_P2P_CONN_MASK);
  309. }
  310. /**
  311. * cmp_connection_break - break the connection to the target
  312. * @c: the connection manager
  313. *
  314. * This function deactives the connection in the target's input/output plug
  315. * control register, and frees the isochronous resources of the connection.
  316. * Before calling this function, the caller should cease transmitting packets.
  317. */
  318. void cmp_connection_break(struct cmp_connection *c)
  319. {
  320. int err;
  321. mutex_lock(&c->mutex);
  322. if (!c->connected) {
  323. mutex_unlock(&c->mutex);
  324. return;
  325. }
  326. err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  327. if (err < 0)
  328. cmp_error(c, "plug is still connected\n");
  329. fw_iso_resources_free(&c->resources);
  330. c->connected = false;
  331. mutex_unlock(&c->mutex);
  332. }
  333. EXPORT_SYMBOL(cmp_connection_break);