cmp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. * @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. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  161. {
  162. ipcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  163. PCR_P2P_CONN_MASK |
  164. PCR_CHANNEL_MASK);
  165. ipcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  166. ipcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  167. return ipcr;
  168. }
  169. static int get_overhead_id(struct cmp_connection *c)
  170. {
  171. int id;
  172. /*
  173. * apply "oPCR overhead ID encoding"
  174. * the encoding table can convert up to 512.
  175. * here the value over 512 is converted as the same way as 512.
  176. */
  177. for (id = 1; id < 16; id++) {
  178. if (c->resources.bandwidth_overhead < (id << 5))
  179. break;
  180. }
  181. if (id == 16)
  182. id = 0;
  183. return id;
  184. }
  185. static __be32 opcr_set_modify(struct cmp_connection *c, __be32 opcr)
  186. {
  187. unsigned int spd, xspd;
  188. /* generate speed and extended speed field value */
  189. if (c->speed > SCODE_400) {
  190. spd = SCODE_800;
  191. xspd = c->speed - SCODE_800;
  192. } else {
  193. spd = c->speed;
  194. xspd = 0;
  195. }
  196. opcr &= ~cpu_to_be32(PCR_BCAST_CONN |
  197. PCR_P2P_CONN_MASK |
  198. OPCR_XSPEED_MASK |
  199. PCR_CHANNEL_MASK |
  200. OPCR_SPEED_MASK |
  201. OPCR_OVERHEAD_ID_MASK);
  202. opcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
  203. opcr |= cpu_to_be32(xspd << OPCR_XSPEED_SHIFT);
  204. opcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
  205. opcr |= cpu_to_be32(spd << OPCR_SPEED_SHIFT);
  206. opcr |= cpu_to_be32(get_overhead_id(c) << OPCR_OVERHEAD_ID_SHIFT);
  207. return opcr;
  208. }
  209. static int pcr_set_check(struct cmp_connection *c, __be32 pcr)
  210. {
  211. if (pcr & cpu_to_be32(PCR_BCAST_CONN |
  212. PCR_P2P_CONN_MASK)) {
  213. cmp_error(c, "plug is already in use\n");
  214. return -EBUSY;
  215. }
  216. if (!(pcr & cpu_to_be32(PCR_ONLINE))) {
  217. cmp_error(c, "plug is not on-line\n");
  218. return -ECONNREFUSED;
  219. }
  220. return 0;
  221. }
  222. /**
  223. * cmp_connection_establish - establish a connection to the target
  224. * @c: the connection manager
  225. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  226. *
  227. * This function establishes a point-to-point connection from the local
  228. * computer to the target by allocating isochronous resources (channel and
  229. * bandwidth) and setting the target's input/output plug control register.
  230. * When this function succeeds, the caller is responsible for starting
  231. * transmitting packets.
  232. */
  233. int cmp_connection_establish(struct cmp_connection *c,
  234. unsigned int max_payload_bytes)
  235. {
  236. int err;
  237. if (WARN_ON(c->connected))
  238. return -EISCONN;
  239. c->speed = min(c->max_speed,
  240. fw_parent_device(c->resources.unit)->max_speed);
  241. mutex_lock(&c->mutex);
  242. retry_after_bus_reset:
  243. err = fw_iso_resources_allocate(&c->resources,
  244. max_payload_bytes, c->speed);
  245. if (err < 0)
  246. goto err_mutex;
  247. if (c->direction == CMP_OUTPUT)
  248. err = pcr_modify(c, opcr_set_modify, pcr_set_check,
  249. ABORT_ON_BUS_RESET);
  250. else
  251. err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
  252. ABORT_ON_BUS_RESET);
  253. if (err == -EAGAIN) {
  254. fw_iso_resources_free(&c->resources);
  255. goto retry_after_bus_reset;
  256. }
  257. if (err < 0)
  258. goto err_resources;
  259. c->connected = true;
  260. mutex_unlock(&c->mutex);
  261. return 0;
  262. err_resources:
  263. fw_iso_resources_free(&c->resources);
  264. err_mutex:
  265. mutex_unlock(&c->mutex);
  266. return err;
  267. }
  268. EXPORT_SYMBOL(cmp_connection_establish);
  269. /**
  270. * cmp_connection_update - update the connection after a bus reset
  271. * @c: the connection manager
  272. *
  273. * This function must be called from the driver's .update handler to
  274. * reestablish any connection that might have been active.
  275. *
  276. * Returns zero on success, or a negative error code. On an error, the
  277. * connection is broken and the caller must stop transmitting iso packets.
  278. */
  279. int cmp_connection_update(struct cmp_connection *c)
  280. {
  281. int err;
  282. mutex_lock(&c->mutex);
  283. if (!c->connected) {
  284. mutex_unlock(&c->mutex);
  285. return 0;
  286. }
  287. err = fw_iso_resources_update(&c->resources);
  288. if (err < 0)
  289. goto err_unconnect;
  290. if (c->direction == CMP_OUTPUT)
  291. err = pcr_modify(c, opcr_set_modify, pcr_set_check,
  292. SUCCEED_ON_BUS_RESET);
  293. else
  294. err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
  295. SUCCEED_ON_BUS_RESET);
  296. if (err < 0)
  297. goto err_resources;
  298. mutex_unlock(&c->mutex);
  299. return 0;
  300. err_resources:
  301. fw_iso_resources_free(&c->resources);
  302. err_unconnect:
  303. c->connected = false;
  304. mutex_unlock(&c->mutex);
  305. return err;
  306. }
  307. EXPORT_SYMBOL(cmp_connection_update);
  308. static __be32 pcr_break_modify(struct cmp_connection *c, __be32 pcr)
  309. {
  310. return pcr & ~cpu_to_be32(PCR_BCAST_CONN | PCR_P2P_CONN_MASK);
  311. }
  312. /**
  313. * cmp_connection_break - break the connection to the target
  314. * @c: the connection manager
  315. *
  316. * This function deactives the connection in the target's input/output plug
  317. * control register, and frees the isochronous resources of the connection.
  318. * Before calling this function, the caller should cease transmitting packets.
  319. */
  320. void cmp_connection_break(struct cmp_connection *c)
  321. {
  322. int err;
  323. mutex_lock(&c->mutex);
  324. if (!c->connected) {
  325. mutex_unlock(&c->mutex);
  326. return;
  327. }
  328. err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  329. if (err < 0)
  330. cmp_error(c, "plug is still connected\n");
  331. fw_iso_resources_free(&c->resources);
  332. c->connected = false;
  333. mutex_unlock(&c->mutex);
  334. }
  335. EXPORT_SYMBOL(cmp_connection_break);