cmp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Connection Management Procedures (IEC 61883-1) helper functions
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  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. * @direction: input or output
  100. * @pcr_index: the index of the iPCR/oPCR on the target device
  101. */
  102. int cmp_connection_init(struct cmp_connection *c,
  103. struct fw_unit *unit,
  104. enum cmp_direction direction,
  105. unsigned int pcr_index)
  106. {
  107. __be32 mpr_be;
  108. u32 mpr;
  109. int err;
  110. c->direction = direction;
  111. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  112. mpr_address(c), &mpr_be, 4, 0);
  113. if (err < 0)
  114. return err;
  115. mpr = be32_to_cpu(mpr_be);
  116. if (pcr_index >= (mpr & MPR_PLUGS_MASK))
  117. return -EINVAL;
  118. err = fw_iso_resources_init(&c->resources, unit);
  119. if (err < 0)
  120. return err;
  121. c->connected = false;
  122. mutex_init(&c->mutex);
  123. c->last_pcr_value = cpu_to_be32(0x80000000);
  124. c->pcr_index = pcr_index;
  125. c->max_speed = (mpr & MPR_SPEED_MASK) >> MPR_SPEED_SHIFT;
  126. if (c->max_speed == SCODE_BETA)
  127. c->max_speed += (mpr & MPR_XSPEED_MASK) >> MPR_XSPEED_SHIFT;
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(cmp_connection_init);
  131. /**
  132. * cmp_connection_check_used - check connection is already esablished or not
  133. * @c: the connection manager to be checked
  134. * @used: the pointer to store the result of checking the connection
  135. */
  136. int cmp_connection_check_used(struct cmp_connection *c, bool *used)
  137. {
  138. __be32 pcr;
  139. int err;
  140. err = snd_fw_transaction(
  141. c->resources.unit, TCODE_READ_QUADLET_REQUEST,
  142. pcr_address(c), &pcr, 4, 0);
  143. if (err >= 0)
  144. *used = !!(pcr & cpu_to_be32(PCR_BCAST_CONN |
  145. PCR_P2P_CONN_MASK));
  146. return err;
  147. }
  148. EXPORT_SYMBOL(cmp_connection_check_used);
  149. /**
  150. * cmp_connection_destroy - free connection manager resources
  151. * @c: the connection manager
  152. */
  153. void cmp_connection_destroy(struct cmp_connection *c)
  154. {
  155. WARN_ON(c->connected);
  156. mutex_destroy(&c->mutex);
  157. fw_iso_resources_destroy(&c->resources);
  158. }
  159. EXPORT_SYMBOL(cmp_connection_destroy);
  160. int cmp_connection_reserve(struct cmp_connection *c,
  161. unsigned int max_payload_bytes)
  162. {
  163. guard(mutex)(&c->mutex);
  164. if (WARN_ON(c->resources.allocated))
  165. return -EBUSY;
  166. c->speed = min(c->max_speed,
  167. fw_parent_device(c->resources.unit)->max_speed);
  168. return fw_iso_resources_allocate(&c->resources, max_payload_bytes,
  169. c->speed);
  170. }
  171. EXPORT_SYMBOL(cmp_connection_reserve);
  172. void cmp_connection_release(struct cmp_connection *c)
  173. {
  174. guard(mutex)(&c->mutex);
  175. fw_iso_resources_free(&c->resources);
  176. }
  177. EXPORT_SYMBOL(cmp_connection_release);
  178. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  179. {
  180. ipcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  181. PCR_P2P_CONN_MASK |
  182. PCR_CHANNEL_MASK);
  183. ipcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  184. ipcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  185. return ipcr;
  186. }
  187. static int get_overhead_id(struct cmp_connection *c)
  188. {
  189. int id;
  190. /*
  191. * apply "oPCR overhead ID encoding"
  192. * the encoding table can convert up to 512.
  193. * here the value over 512 is converted as the same way as 512.
  194. */
  195. for (id = 1; id < 16; id++) {
  196. if (c->resources.bandwidth_overhead < (id << 5))
  197. break;
  198. }
  199. if (id == 16)
  200. id = 0;
  201. return id;
  202. }
  203. static __be32 opcr_set_modify(struct cmp_connection *c, __be32 opcr)
  204. {
  205. unsigned int spd, xspd;
  206. /* generate speed and extended speed field value */
  207. if (c->speed > SCODE_400) {
  208. spd = SCODE_800;
  209. xspd = c->speed - SCODE_800;
  210. } else {
  211. spd = c->speed;
  212. xspd = 0;
  213. }
  214. opcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  215. PCR_P2P_CONN_MASK |
  216. OPCR_XSPEED_MASK |
  217. PCR_CHANNEL_MASK |
  218. OPCR_SPEED_MASK |
  219. OPCR_OVERHEAD_ID_MASK);
  220. opcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  221. opcr |= cpu_to_be32(xspd << OPCR_XSPEED_SHIFT);
  222. opcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  223. opcr |= cpu_to_be32(spd << OPCR_SPEED_SHIFT);
  224. opcr |= cpu_to_be32(get_overhead_id(c) << OPCR_OVERHEAD_ID_SHIFT);
  225. return opcr;
  226. }
  227. static int pcr_set_check(struct cmp_connection *c, __be32 pcr)
  228. {
  229. if (pcr & cpu_to_be32(PCR_BCAST_CONN |
  230. PCR_P2P_CONN_MASK)) {
  231. cmp_error(c, "plug is already in use\n");
  232. return -EBUSY;
  233. }
  234. if (!(pcr & cpu_to_be32(PCR_ONLINE))) {
  235. cmp_error(c, "plug is not on-line\n");
  236. return -ECONNREFUSED;
  237. }
  238. return 0;
  239. }
  240. /**
  241. * cmp_connection_establish - establish a connection to the target
  242. * @c: the connection manager
  243. *
  244. * This function establishes a point-to-point connection from the local
  245. * computer to the target by allocating isochronous resources (channel and
  246. * bandwidth) and setting the target's input/output plug control register.
  247. * When this function succeeds, the caller is responsible for starting
  248. * transmitting packets.
  249. */
  250. int cmp_connection_establish(struct cmp_connection *c)
  251. {
  252. int err;
  253. guard(mutex)(&c->mutex);
  254. if (WARN_ON(c->connected))
  255. return -EISCONN;
  256. retry_after_bus_reset:
  257. if (c->direction == CMP_OUTPUT)
  258. err = pcr_modify(c, opcr_set_modify, pcr_set_check,
  259. ABORT_ON_BUS_RESET);
  260. else
  261. err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
  262. ABORT_ON_BUS_RESET);
  263. if (err == -EAGAIN) {
  264. err = fw_iso_resources_update(&c->resources);
  265. if (err >= 0)
  266. goto retry_after_bus_reset;
  267. }
  268. if (err >= 0)
  269. c->connected = true;
  270. return err;
  271. }
  272. EXPORT_SYMBOL(cmp_connection_establish);
  273. static __be32 pcr_break_modify(struct cmp_connection *c, __be32 pcr)
  274. {
  275. return pcr & ~cpu_to_be32(PCR_BCAST_CONN | PCR_P2P_CONN_MASK);
  276. }
  277. /**
  278. * cmp_connection_break - break the connection to the target
  279. * @c: the connection manager
  280. *
  281. * This function deactives the connection in the target's input/output plug
  282. * control register, and frees the isochronous resources of the connection.
  283. * Before calling this function, the caller should cease transmitting packets.
  284. */
  285. void cmp_connection_break(struct cmp_connection *c)
  286. {
  287. int err;
  288. guard(mutex)(&c->mutex);
  289. if (!c->connected)
  290. return;
  291. err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  292. if (err < 0)
  293. cmp_error(c, "plug is still connected\n");
  294. c->connected = false;
  295. }
  296. EXPORT_SYMBOL(cmp_connection_break);