ieee1394_transactions.c 16 KB

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