ieee1394_transactions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. BUG();
  214. case ACK_BUSY_X:
  215. case ACK_BUSY_A:
  216. case ACK_BUSY_B:
  217. return -EBUSY;
  218. case ACK_TYPE_ERROR:
  219. return -EACCES;
  220. case ACK_COMPLETE:
  221. if (packet->tcode == TCODE_WRITEQ
  222. || packet->tcode == TCODE_WRITEB) {
  223. return 0;
  224. } else {
  225. HPSB_ERR("impossible ack_complete from node %d "
  226. "(tcode %d)", packet->node_id, packet->tcode);
  227. return -EAGAIN;
  228. }
  229. case ACK_DATA_ERROR:
  230. if (packet->tcode == TCODE_WRITEB
  231. || packet->tcode == TCODE_LOCK_REQUEST) {
  232. return -EAGAIN;
  233. } else {
  234. HPSB_ERR("impossible ack_data_error from node %d "
  235. "(tcode %d)", packet->node_id, packet->tcode);
  236. return -EAGAIN;
  237. }
  238. case ACK_ADDRESS_ERROR:
  239. return -EINVAL;
  240. case ACK_TARDY:
  241. case ACK_CONFLICT_ERROR:
  242. case ACKX_NONE:
  243. case ACKX_SEND_ERROR:
  244. case ACKX_ABORTED:
  245. case ACKX_TIMEOUT:
  246. /* error while sending */
  247. return -EAGAIN;
  248. default:
  249. HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
  250. packet->ack_code, packet->node_id, packet->tcode);
  251. return -EAGAIN;
  252. }
  253. BUG();
  254. }
  255. struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
  256. u64 addr, size_t length)
  257. {
  258. struct hpsb_packet *packet;
  259. if (length == 0)
  260. return NULL;
  261. packet = hpsb_alloc_packet(length);
  262. if (!packet)
  263. return NULL;
  264. packet->host = host;
  265. packet->node_id = node;
  266. if (hpsb_get_tlabel(packet)) {
  267. hpsb_free_packet(packet);
  268. return NULL;
  269. }
  270. if (length == 4)
  271. fill_async_readquad(packet, addr);
  272. else
  273. fill_async_readblock(packet, addr, length);
  274. return packet;
  275. }
  276. struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
  277. u64 addr, quadlet_t * buffer,
  278. size_t length)
  279. {
  280. struct hpsb_packet *packet;
  281. if (length == 0)
  282. return NULL;
  283. packet = hpsb_alloc_packet(length);
  284. if (!packet)
  285. return NULL;
  286. if (length % 4) { /* zero padding bytes */
  287. packet->data[length >> 2] = 0;
  288. }
  289. packet->host = host;
  290. packet->node_id = node;
  291. if (hpsb_get_tlabel(packet)) {
  292. hpsb_free_packet(packet);
  293. return NULL;
  294. }
  295. if (length == 4) {
  296. fill_async_writequad(packet, addr, buffer ? *buffer : 0);
  297. } else {
  298. fill_async_writeblock(packet, addr, length);
  299. if (buffer)
  300. memcpy(packet->data, buffer, length);
  301. }
  302. return packet;
  303. }
  304. struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
  305. int length, int channel, int tag,
  306. int sync)
  307. {
  308. struct hpsb_packet *packet;
  309. if (length == 0)
  310. return NULL;
  311. packet = hpsb_alloc_packet(length);
  312. if (!packet)
  313. return NULL;
  314. if (length % 4) { /* zero padding bytes */
  315. packet->data[length >> 2] = 0;
  316. }
  317. packet->host = host;
  318. /* Because it is too difficult to determine all PHY speeds and link
  319. * speeds here, we use S100... */
  320. packet->speed_code = IEEE1394_SPEED_100;
  321. /* ...and prevent hpsb_send_packet() from overriding it. */
  322. packet->node_id = LOCAL_BUS | ALL_NODES;
  323. if (hpsb_get_tlabel(packet)) {
  324. hpsb_free_packet(packet);
  325. return NULL;
  326. }
  327. fill_async_stream_packet(packet, length, channel, tag, sync);
  328. if (buffer)
  329. memcpy(packet->data, buffer, length);
  330. return packet;
  331. }
  332. struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
  333. u64 addr, int extcode,
  334. quadlet_t * data, quadlet_t arg)
  335. {
  336. struct hpsb_packet *p;
  337. u32 length;
  338. p = hpsb_alloc_packet(8);
  339. if (!p)
  340. return NULL;
  341. p->host = host;
  342. p->node_id = node;
  343. if (hpsb_get_tlabel(p)) {
  344. hpsb_free_packet(p);
  345. return NULL;
  346. }
  347. switch (extcode) {
  348. case EXTCODE_FETCH_ADD:
  349. case EXTCODE_LITTLE_ADD:
  350. length = 4;
  351. if (data)
  352. p->data[0] = *data;
  353. break;
  354. default:
  355. length = 8;
  356. if (data) {
  357. p->data[0] = arg;
  358. p->data[1] = *data;
  359. }
  360. break;
  361. }
  362. fill_async_lock(p, addr, extcode, length);
  363. return p;
  364. }
  365. struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
  366. nodeid_t node, u64 addr, int extcode,
  367. octlet_t * data, octlet_t arg)
  368. {
  369. struct hpsb_packet *p;
  370. u32 length;
  371. p = hpsb_alloc_packet(16);
  372. if (!p)
  373. return NULL;
  374. p->host = host;
  375. p->node_id = node;
  376. if (hpsb_get_tlabel(p)) {
  377. hpsb_free_packet(p);
  378. return NULL;
  379. }
  380. switch (extcode) {
  381. case EXTCODE_FETCH_ADD:
  382. case EXTCODE_LITTLE_ADD:
  383. length = 8;
  384. if (data) {
  385. p->data[0] = *data >> 32;
  386. p->data[1] = *data & 0xffffffff;
  387. }
  388. break;
  389. default:
  390. length = 16;
  391. if (data) {
  392. p->data[0] = arg >> 32;
  393. p->data[1] = arg & 0xffffffff;
  394. p->data[2] = *data >> 32;
  395. p->data[3] = *data & 0xffffffff;
  396. }
  397. break;
  398. }
  399. fill_async_lock(p, addr, extcode, length);
  400. return p;
  401. }
  402. struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
  403. {
  404. struct hpsb_packet *p;
  405. p = hpsb_alloc_packet(0);
  406. if (!p)
  407. return NULL;
  408. p->host = host;
  409. fill_phy_packet(p, data);
  410. return p;
  411. }
  412. /*
  413. * FIXME - these functions should probably read from / write to user space to
  414. * avoid in kernel buffers for user space callers
  415. */
  416. /**
  417. * hpsb_read - generic read function
  418. *
  419. * Recognizes the local node ID and act accordingly. Automatically uses a
  420. * quadlet read request if @length == 4 and and a block read request otherwise.
  421. * It does not yet support lengths that are not a multiple of 4.
  422. *
  423. * You must explicitly specifiy the @generation for which the node ID is valid,
  424. * to avoid sending packets to the wrong nodes when we race with a bus reset.
  425. */
  426. int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  427. u64 addr, quadlet_t * buffer, size_t length)
  428. {
  429. struct hpsb_packet *packet;
  430. int retval = 0;
  431. if (length == 0)
  432. return -EINVAL;
  433. BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
  434. packet = hpsb_make_readpacket(host, node, addr, length);
  435. if (!packet) {
  436. return -ENOMEM;
  437. }
  438. packet->generation = generation;
  439. retval = hpsb_send_packet_and_wait(packet);
  440. if (retval < 0)
  441. goto hpsb_read_fail;
  442. retval = hpsb_packet_success(packet);
  443. if (retval == 0) {
  444. if (length == 4) {
  445. *buffer = packet->header[3];
  446. } else {
  447. memcpy(buffer, packet->data, length);
  448. }
  449. }
  450. hpsb_read_fail:
  451. hpsb_free_tlabel(packet);
  452. hpsb_free_packet(packet);
  453. return retval;
  454. }
  455. /**
  456. * hpsb_write - generic write function
  457. *
  458. * Recognizes the local node ID and act accordingly. Automatically uses a
  459. * quadlet write request if @length == 4 and and a block write request
  460. * otherwise. It does not yet support lengths that are not a multiple of 4.
  461. *
  462. * You must explicitly specifiy the @generation for which the node ID is valid,
  463. * to avoid sending packets to the wrong nodes when we race with a bus reset.
  464. */
  465. int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  466. u64 addr, quadlet_t * buffer, size_t length)
  467. {
  468. struct hpsb_packet *packet;
  469. int retval;
  470. if (length == 0)
  471. return -EINVAL;
  472. BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
  473. packet = hpsb_make_writepacket(host, node, addr, buffer, length);
  474. if (!packet)
  475. return -ENOMEM;
  476. packet->generation = generation;
  477. retval = hpsb_send_packet_and_wait(packet);
  478. if (retval < 0)
  479. goto hpsb_write_fail;
  480. retval = hpsb_packet_success(packet);
  481. hpsb_write_fail:
  482. hpsb_free_tlabel(packet);
  483. hpsb_free_packet(packet);
  484. return retval;
  485. }
  486. #if 0
  487. int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
  488. u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
  489. {
  490. struct hpsb_packet *packet;
  491. int retval = 0;
  492. BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
  493. packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
  494. if (!packet)
  495. return -ENOMEM;
  496. packet->generation = generation;
  497. retval = hpsb_send_packet_and_wait(packet);
  498. if (retval < 0)
  499. goto hpsb_lock_fail;
  500. retval = hpsb_packet_success(packet);
  501. if (retval == 0) {
  502. *data = packet->data[0];
  503. }
  504. hpsb_lock_fail:
  505. hpsb_free_tlabel(packet);
  506. hpsb_free_packet(packet);
  507. return retval;
  508. }
  509. int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
  510. quadlet_t * buffer, size_t length, u32 specifier_id,
  511. unsigned int version)
  512. {
  513. struct hpsb_packet *packet;
  514. int retval = 0;
  515. u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
  516. u8 specifier_id_lo = specifier_id & 0xff;
  517. HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
  518. length += 8;
  519. packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
  520. if (!packet)
  521. return -ENOMEM;
  522. packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
  523. packet->data[1] =
  524. cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
  525. memcpy(&(packet->data[2]), buffer, length - 8);
  526. packet->generation = generation;
  527. packet->no_waiter = 1;
  528. retval = hpsb_send_packet(packet);
  529. if (retval < 0)
  530. hpsb_free_packet(packet);
  531. return retval;
  532. }
  533. #endif /* 0 */