ieee1394_transactions.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * IEEE 1394 for Linux
  3. *
  4. * Transaction support.
  5. *
  6. * Copyright (C) 1999 Andreas E. Bombe
  7. *
  8. * This code is licensed under the GPL. See the file COPYING in the root
  9. * directory of the kernel sources for details.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/compiler.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/string.h>
  16. #include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */
  17. #include <linux/wait.h>
  18. #include <asm/bug.h>
  19. #include <asm/errno.h>
  20. #include <asm/system.h>
  21. #include "ieee1394.h"
  22. #include "ieee1394_types.h"
  23. #include "hosts.h"
  24. #include "ieee1394_core.h"
  25. #include "ieee1394_transactions.h"
  26. #define PREP_ASYNC_HEAD_ADDRESS(tc) \
  27. packet->tcode = tc; \
  28. packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
  29. | (1 << 8) | (tc << 4); \
  30. packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
  31. packet->header[2] = addr & 0xffffffff
  32. #ifndef HPSB_DEBUG_TLABELS
  33. static
  34. #endif
  35. DEFINE_SPINLOCK(hpsb_tlabel_lock);
  36. static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
  37. static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
  38. {
  39. PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
  40. packet->header_size = 12;
  41. packet->data_size = 0;
  42. packet->expect_response = 1;
  43. }
  44. static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
  45. int length)
  46. {
  47. PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
  48. packet->header[3] = length << 16;
  49. packet->header_size = 16;
  50. packet->data_size = 0;
  51. packet->expect_response = 1;
  52. }
  53. static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
  54. quadlet_t data)
  55. {
  56. PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
  57. packet->header[3] = data;
  58. packet->header_size = 16;
  59. packet->data_size = 0;
  60. packet->expect_response = 1;
  61. }
  62. static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
  63. int length)
  64. {
  65. PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
  66. packet->header[3] = length << 16;
  67. packet->header_size = 16;
  68. packet->expect_response = 1;
  69. packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
  70. }
  71. static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
  72. int length)
  73. {
  74. PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
  75. packet->header[3] = (length << 16) | extcode;
  76. packet->header_size = 16;
  77. packet->data_size = length;
  78. packet->expect_response = 1;
  79. }
  80. static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
  81. {
  82. packet->header[0] = data;
  83. packet->header[1] = ~data;
  84. packet->header_size = 8;
  85. packet->data_size = 0;
  86. packet->expect_response = 0;
  87. packet->type = hpsb_raw; /* No CRC added */
  88. packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
  89. }
  90. static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
  91. int channel, int tag, int sync)
  92. {
  93. packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
  94. | (TCODE_STREAM_DATA << 4) | sync;
  95. packet->header_size = 4;
  96. packet->data_size = length;
  97. packet->type = hpsb_async;
  98. packet->tcode = TCODE_ISO_DATA;
  99. }
  100. /* same as hpsb_get_tlabel, except that it returns immediately */
  101. static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
  102. {
  103. unsigned long flags, *tp;
  104. u8 *next;
  105. int tlabel, n = NODEID_TO_NODE(packet->node_id);
  106. /* Broadcast transactions are complete once the request has been sent.
  107. * Use the same transaction label for all broadcast transactions. */
  108. if (unlikely(n == ALL_NODES)) {
  109. packet->tlabel = 0;
  110. return 0;
  111. }
  112. tp = packet->host->tl_pool[n].map;
  113. next = &packet->host->next_tl[n];
  114. spin_lock_irqsave(&hpsb_tlabel_lock, flags);
  115. tlabel = find_next_zero_bit(tp, 64, *next);
  116. if (tlabel > 63)
  117. tlabel = find_first_zero_bit(tp, 64);
  118. if (tlabel > 63) {
  119. spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
  120. return -EAGAIN;
  121. }
  122. __set_bit(tlabel, tp);
  123. *next = (tlabel + 1) & 63;
  124. spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
  125. packet->tlabel = tlabel;
  126. return 0;
  127. }
  128. /**
  129. * hpsb_get_tlabel - allocate a transaction label
  130. * @packet: the packet whose tlabel and tl_pool we set
  131. *
  132. * Every asynchronous transaction on the 1394 bus needs a transaction
  133. * label to match the response to the request. This label has to be
  134. * different from any other transaction label in an outstanding request to
  135. * the same node to make matching possible without ambiguity.
  136. *
  137. * There are 64 different tlabels, so an allocated tlabel has to be freed
  138. * with hpsb_free_tlabel() after the transaction is complete (unless it's
  139. * reused again for the same target node).
  140. *
  141. * Return value: Zero on success, otherwise non-zero. A non-zero return
  142. * generally means there are no available tlabels. If this is called out
  143. * of interrupt or atomic context, then it will sleep until can return a
  144. * tlabel or a signal is received.
  145. */
  146. int hpsb_get_tlabel(struct hpsb_packet *packet)
  147. {
  148. if (irqs_disabled() || in_atomic())
  149. return hpsb_get_tlabel_atomic(packet);
  150. /* NB: The macro wait_event_interruptible() is called with a condition
  151. * argument with side effect. This is only possible because the side
  152. * effect does not occur until the condition became true, and
  153. * wait_event_interruptible() won't evaluate the condition again after
  154. * that. */
  155. return wait_event_interruptible(tlabel_wq,
  156. !hpsb_get_tlabel_atomic(packet));
  157. }
  158. /**
  159. * hpsb_free_tlabel - free an allocated transaction label
  160. * @packet: packet whose tlabel and tl_pool needs to be cleared
  161. *
  162. * Frees the transaction label allocated with hpsb_get_tlabel(). The
  163. * tlabel has to be freed after the transaction is complete (i.e. response
  164. * was received for a split transaction or packet was sent for a unified
  165. * transaction).
  166. *
  167. * A tlabel must not be freed twice.
  168. */
  169. void hpsb_free_tlabel(struct hpsb_packet *packet)
  170. {
  171. unsigned long flags, *tp;
  172. int tlabel, n = NODEID_TO_NODE(packet->node_id);
  173. if (unlikely(n == ALL_NODES))
  174. return;
  175. tp = packet->host->tl_pool[n].map;
  176. tlabel = packet->tlabel;
  177. BUG_ON(tlabel > 63 || tlabel < 0);
  178. spin_lock_irqsave(&hpsb_tlabel_lock, flags);
  179. BUG_ON(!__test_and_clear_bit(tlabel, tp));
  180. spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
  181. wake_up_interruptible(&tlabel_wq);
  182. }
  183. /**
  184. * hpsb_packet_success - Make sense of the ack and reply codes
  185. *
  186. * Make sense of the ack and reply codes and return more convenient error codes:
  187. * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can
  188. * probably resolved by retry. -%EREMOTEIO = node suffers from an internal
  189. * error. -%EACCES = this transaction is not allowed on requested address.
  190. * -%EINVAL = invalid address at node.
  191. */
  192. int hpsb_packet_success(struct hpsb_packet *packet)
  193. {
  194. switch (packet->ack_code) {
  195. case ACK_PENDING:
  196. switch ((packet->header[1] >> 12) & 0xf) {
  197. case RCODE_COMPLETE:
  198. return 0;
  199. case RCODE_CONFLICT_ERROR:
  200. return -EAGAIN;
  201. case RCODE_DATA_ERROR:
  202. return -EREMOTEIO;
  203. case RCODE_TYPE_ERROR:
  204. return -EACCES;
  205. case RCODE_ADDRESS_ERROR:
  206. return -EINVAL;
  207. default:
  208. HPSB_ERR("received reserved rcode %d from node %d",
  209. (packet->header[1] >> 12) & 0xf,
  210. packet->node_id);
  211. return -EAGAIN;
  212. }
  213. case ACK_BUSY_X:
  214. case ACK_BUSY_A:
  215. case ACK_BUSY_B:
  216. return -EBUSY;
  217. case ACK_TYPE_ERROR:
  218. return -EACCES;
  219. case ACK_COMPLETE:
  220. if (packet->tcode == TCODE_WRITEQ
  221. || packet->tcode == TCODE_WRITEB) {
  222. return 0;
  223. } else {
  224. HPSB_ERR("impossible ack_complete from node %d "
  225. "(tcode %d)", packet->node_id, packet->tcode);
  226. return -EAGAIN;
  227. }
  228. case ACK_DATA_ERROR:
  229. if (packet->tcode == TCODE_WRITEB
  230. || packet->tcode == TCODE_LOCK_REQUEST) {
  231. return -EAGAIN;
  232. } else {
  233. HPSB_ERR("impossible ack_data_error from node %d "
  234. "(tcode %d)", packet->node_id, packet->tcode);
  235. return -EAGAIN;
  236. }
  237. case ACK_ADDRESS_ERROR:
  238. return -EINVAL;
  239. case ACK_TARDY:
  240. case ACK_CONFLICT_ERROR:
  241. case ACKX_NONE:
  242. case ACKX_SEND_ERROR:
  243. case ACKX_ABORTED:
  244. case ACKX_TIMEOUT:
  245. /* error while sending */
  246. return -EAGAIN;
  247. default:
  248. HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
  249. packet->ack_code, packet->node_id, packet->tcode);
  250. return -EAGAIN;
  251. }
  252. }
  253. struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
  254. u64 addr, size_t length)
  255. {
  256. struct hpsb_packet *packet;
  257. if (length == 0)
  258. return NULL;
  259. packet = hpsb_alloc_packet(length);
  260. if (!packet)
  261. return NULL;
  262. packet->host = host;
  263. packet->node_id = node;
  264. if (hpsb_get_tlabel(packet)) {
  265. hpsb_free_packet(packet);
  266. return NULL;
  267. }
  268. if (length == 4)
  269. fill_async_readquad(packet, addr);
  270. else
  271. fill_async_readblock(packet, addr, length);
  272. return packet;
  273. }
  274. struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
  275. u64 addr, quadlet_t * buffer,
  276. size_t length)
  277. {
  278. struct hpsb_packet *packet;
  279. if (length == 0)
  280. return NULL;
  281. packet = hpsb_alloc_packet(length);
  282. if (!packet)
  283. return NULL;
  284. if (length % 4) { /* zero padding bytes */
  285. packet->data[length >> 2] = 0;
  286. }
  287. packet->host = host;
  288. packet->node_id = node;
  289. if (hpsb_get_tlabel(packet)) {
  290. hpsb_free_packet(packet);
  291. return NULL;
  292. }
  293. if (length == 4) {
  294. fill_async_writequad(packet, addr, buffer ? *buffer : 0);
  295. } else {
  296. fill_async_writeblock(packet, addr, length);
  297. if (buffer)
  298. memcpy(packet->data, buffer, length);
  299. }
  300. return packet;
  301. }
  302. struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
  303. int length, int channel, int tag,
  304. int sync)
  305. {
  306. struct hpsb_packet *packet;
  307. if (length == 0)
  308. return NULL;
  309. packet = hpsb_alloc_packet(length);
  310. if (!packet)
  311. return NULL;
  312. if (length % 4) { /* zero padding bytes */
  313. packet->data[length >> 2] = 0;
  314. }
  315. packet->host = host;
  316. /* Because it is too difficult to determine all PHY speeds and link
  317. * speeds here, we use S100... */
  318. packet->speed_code = IEEE1394_SPEED_100;
  319. /* ...and prevent hpsb_send_packet() from overriding it. */
  320. packet->node_id = LOCAL_BUS | ALL_NODES;
  321. if (hpsb_get_tlabel(packet)) {
  322. hpsb_free_packet(packet);
  323. return NULL;
  324. }
  325. fill_async_stream_packet(packet, length, channel, tag, sync);
  326. if (buffer)
  327. memcpy(packet->data, buffer, length);
  328. return packet;
  329. }
  330. struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
  331. u64 addr, int extcode,
  332. quadlet_t * data, quadlet_t arg)
  333. {
  334. struct hpsb_packet *p;
  335. u32 length;
  336. p = hpsb_alloc_packet(8);
  337. if (!p)
  338. return NULL;
  339. p->host = host;
  340. p->node_id = node;
  341. if (hpsb_get_tlabel(p)) {
  342. hpsb_free_packet(p);
  343. return NULL;
  344. }
  345. switch (extcode) {
  346. case EXTCODE_FETCH_ADD:
  347. case EXTCODE_LITTLE_ADD:
  348. length = 4;
  349. if (data)
  350. p->data[0] = *data;
  351. break;
  352. default:
  353. length = 8;
  354. if (data) {
  355. p->data[0] = arg;
  356. p->data[1] = *data;
  357. }
  358. break;
  359. }
  360. fill_async_lock(p, addr, extcode, length);
  361. return p;
  362. }
  363. struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
  364. nodeid_t node, u64 addr, int extcode,
  365. octlet_t * data, octlet_t arg)
  366. {
  367. struct hpsb_packet *p;
  368. u32 length;
  369. p = hpsb_alloc_packet(16);
  370. if (!p)
  371. return NULL;
  372. p->host = host;
  373. p->node_id = node;
  374. if (hpsb_get_tlabel(p)) {
  375. hpsb_free_packet(p);
  376. return NULL;
  377. }
  378. switch (extcode) {
  379. case EXTCODE_FETCH_ADD:
  380. case EXTCODE_LITTLE_ADD:
  381. length = 8;
  382. if (data) {
  383. p->data[0] = *data >> 32;
  384. p->data[1] = *data & 0xffffffff;
  385. }
  386. break;
  387. default:
  388. length = 16;
  389. if (data) {
  390. p->data[0] = arg >> 32;
  391. p->data[1] = arg & 0xffffffff;
  392. p->data[2] = *data >> 32;
  393. p->data[3] = *data & 0xffffffff;
  394. }
  395. break;
  396. }
  397. fill_async_lock(p, addr, extcode, length);
  398. return p;
  399. }
  400. struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
  401. {
  402. struct hpsb_packet *p;
  403. p = hpsb_alloc_packet(0);
  404. if (!p)
  405. return NULL;
  406. p->host = host;
  407. fill_phy_packet(p, data);
  408. return p;
  409. }
  410. /*
  411. * FIXME - these functions should probably read from / write to user space to
  412. * avoid in kernel buffers for user space callers
  413. */
  414. /**
  415. * hpsb_read - generic read function
  416. *
  417. * Recognizes the local node ID and act accordingly. Automatically uses a
  418. * quadlet read request if @length == 4 and and a block read request otherwise.
  419. * It does not yet support lengths that are not a multiple of 4.
  420. *
  421. * You must explicitly specifiy the @generation for which the node ID is valid,
  422. * to avoid sending packets to the wrong nodes when we race with a bus reset.
  423. */
  424. int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  425. u64 addr, quadlet_t * buffer, size_t length)
  426. {
  427. struct hpsb_packet *packet;
  428. int retval = 0;
  429. if (length == 0)
  430. return -EINVAL;
  431. packet = hpsb_make_readpacket(host, node, addr, length);
  432. if (!packet) {
  433. return -ENOMEM;
  434. }
  435. packet->generation = generation;
  436. retval = hpsb_send_packet_and_wait(packet);
  437. if (retval < 0)
  438. goto hpsb_read_fail;
  439. retval = hpsb_packet_success(packet);
  440. if (retval == 0) {
  441. if (length == 4) {
  442. *buffer = packet->header[3];
  443. } else {
  444. memcpy(buffer, packet->data, length);
  445. }
  446. }
  447. hpsb_read_fail:
  448. hpsb_free_tlabel(packet);
  449. hpsb_free_packet(packet);
  450. return retval;
  451. }
  452. /**
  453. * hpsb_write - generic write function
  454. *
  455. * Recognizes the local node ID and act accordingly. Automatically uses a
  456. * quadlet write request if @length == 4 and and a block write request
  457. * otherwise. It does not yet support lengths that are not a multiple of 4.
  458. *
  459. * You must explicitly specifiy the @generation for which the node ID is valid,
  460. * to avoid sending packets to the wrong nodes when we race with a bus reset.
  461. */
  462. int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  463. u64 addr, quadlet_t * buffer, size_t length)
  464. {
  465. struct hpsb_packet *packet;
  466. int retval;
  467. if (length == 0)
  468. return -EINVAL;
  469. packet = hpsb_make_writepacket(host, node, addr, buffer, length);
  470. if (!packet)
  471. return -ENOMEM;
  472. packet->generation = generation;
  473. retval = hpsb_send_packet_and_wait(packet);
  474. if (retval < 0)
  475. goto hpsb_write_fail;
  476. retval = hpsb_packet_success(packet);
  477. hpsb_write_fail:
  478. hpsb_free_tlabel(packet);
  479. hpsb_free_packet(packet);
  480. return retval;
  481. }
  482. int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  483. u64 addr, int extcode, quadlet_t *data, quadlet_t arg)
  484. {
  485. struct hpsb_packet *packet;
  486. int retval = 0;
  487. packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
  488. if (!packet)
  489. return -ENOMEM;
  490. packet->generation = generation;
  491. retval = hpsb_send_packet_and_wait(packet);
  492. if (retval < 0)
  493. goto hpsb_lock_fail;
  494. retval = hpsb_packet_success(packet);
  495. if (retval == 0)
  496. *data = packet->data[0];
  497. hpsb_lock_fail:
  498. hpsb_free_tlabel(packet);
  499. hpsb_free_packet(packet);
  500. return retval;
  501. }