fw-transaction.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Core IEEE1394 transaction logic
  3. *
  4. * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/completion.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kref.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <linux/poll.h>
  29. #include <linux/list.h>
  30. #include <linux/kthread.h>
  31. #include <asm/uaccess.h>
  32. #include "fw-transaction.h"
  33. #include "fw-topology.h"
  34. #include "fw-device.h"
  35. #define HEADER_PRI(pri) ((pri) << 0)
  36. #define HEADER_TCODE(tcode) ((tcode) << 4)
  37. #define HEADER_RETRY(retry) ((retry) << 8)
  38. #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
  39. #define HEADER_DESTINATION(destination) ((destination) << 16)
  40. #define HEADER_SOURCE(source) ((source) << 16)
  41. #define HEADER_RCODE(rcode) ((rcode) << 12)
  42. #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
  43. #define HEADER_DATA_LENGTH(length) ((length) << 16)
  44. #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
  45. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  46. #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
  47. #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
  48. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  49. #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
  50. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  51. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  52. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  53. #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
  54. #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
  55. #define PHY_IDENTIFIER(id) ((id) << 30)
  56. static int
  57. close_transaction(struct fw_transaction *transaction,
  58. struct fw_card *card, int rcode,
  59. u32 *payload, size_t length)
  60. {
  61. struct fw_transaction *t;
  62. unsigned long flags;
  63. spin_lock_irqsave(&card->lock, flags);
  64. list_for_each_entry(t, &card->transaction_list, link) {
  65. if (t == transaction) {
  66. list_del(&t->link);
  67. card->tlabel_mask &= ~(1 << t->tlabel);
  68. break;
  69. }
  70. }
  71. spin_unlock_irqrestore(&card->lock, flags);
  72. if (&t->link != &card->transaction_list) {
  73. t->callback(card, rcode, payload, length, t->callback_data);
  74. return 0;
  75. }
  76. return -ENOENT;
  77. }
  78. /*
  79. * Only valid for transactions that are potentially pending (ie have
  80. * been sent).
  81. */
  82. int
  83. fw_cancel_transaction(struct fw_card *card,
  84. struct fw_transaction *transaction)
  85. {
  86. /*
  87. * Cancel the packet transmission if it's still queued. That
  88. * will call the packet transmission callback which cancels
  89. * the transaction.
  90. */
  91. if (card->driver->cancel_packet(card, &transaction->packet) == 0)
  92. return 0;
  93. /*
  94. * If the request packet has already been sent, we need to see
  95. * if the transaction is still pending and remove it in that case.
  96. */
  97. return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
  98. }
  99. EXPORT_SYMBOL(fw_cancel_transaction);
  100. static void
  101. transmit_complete_callback(struct fw_packet *packet,
  102. struct fw_card *card, int status)
  103. {
  104. struct fw_transaction *t =
  105. container_of(packet, struct fw_transaction, packet);
  106. switch (status) {
  107. case ACK_COMPLETE:
  108. close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
  109. break;
  110. case ACK_PENDING:
  111. t->timestamp = packet->timestamp;
  112. break;
  113. case ACK_BUSY_X:
  114. case ACK_BUSY_A:
  115. case ACK_BUSY_B:
  116. close_transaction(t, card, RCODE_BUSY, NULL, 0);
  117. break;
  118. case ACK_DATA_ERROR:
  119. close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0);
  120. break;
  121. case ACK_TYPE_ERROR:
  122. close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0);
  123. break;
  124. default:
  125. /*
  126. * In this case the ack is really a juju specific
  127. * rcode, so just forward that to the callback.
  128. */
  129. close_transaction(t, card, status, NULL, 0);
  130. break;
  131. }
  132. }
  133. static void
  134. fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  135. int node_id, int source_id, int generation, int speed,
  136. unsigned long long offset, void *payload, size_t length)
  137. {
  138. int ext_tcode;
  139. if (tcode > 0x10) {
  140. ext_tcode = tcode & ~0x10;
  141. tcode = TCODE_LOCK_REQUEST;
  142. } else
  143. ext_tcode = 0;
  144. packet->header[0] =
  145. HEADER_RETRY(RETRY_X) |
  146. HEADER_TLABEL(tlabel) |
  147. HEADER_TCODE(tcode) |
  148. HEADER_DESTINATION(node_id);
  149. packet->header[1] =
  150. HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
  151. packet->header[2] =
  152. offset;
  153. switch (tcode) {
  154. case TCODE_WRITE_QUADLET_REQUEST:
  155. packet->header[3] = *(u32 *)payload;
  156. packet->header_length = 16;
  157. packet->payload_length = 0;
  158. break;
  159. case TCODE_LOCK_REQUEST:
  160. case TCODE_WRITE_BLOCK_REQUEST:
  161. packet->header[3] =
  162. HEADER_DATA_LENGTH(length) |
  163. HEADER_EXTENDED_TCODE(ext_tcode);
  164. packet->header_length = 16;
  165. packet->payload = payload;
  166. packet->payload_length = length;
  167. break;
  168. case TCODE_READ_QUADLET_REQUEST:
  169. packet->header_length = 12;
  170. packet->payload_length = 0;
  171. break;
  172. case TCODE_READ_BLOCK_REQUEST:
  173. packet->header[3] =
  174. HEADER_DATA_LENGTH(length) |
  175. HEADER_EXTENDED_TCODE(ext_tcode);
  176. packet->header_length = 16;
  177. packet->payload_length = 0;
  178. break;
  179. }
  180. packet->speed = speed;
  181. packet->generation = generation;
  182. packet->ack = 0;
  183. }
  184. /**
  185. * This function provides low-level access to the IEEE1394 transaction
  186. * logic. Most C programs would use either fw_read(), fw_write() or
  187. * fw_lock() instead - those function are convenience wrappers for
  188. * this function. The fw_send_request() function is primarily
  189. * provided as a flexible, one-stop entry point for languages bindings
  190. * and protocol bindings.
  191. *
  192. * FIXME: Document this function further, in particular the possible
  193. * values for rcode in the callback. In short, we map ACK_COMPLETE to
  194. * RCODE_COMPLETE, internal errors set errno and set rcode to
  195. * RCODE_SEND_ERROR (which is out of range for standard ieee1394
  196. * rcodes). All other rcodes are forwarded unchanged. For all
  197. * errors, payload is NULL, length is 0.
  198. *
  199. * Can not expect the callback to be called before the function
  200. * returns, though this does happen in some cases (ACK_COMPLETE and
  201. * errors).
  202. *
  203. * The payload is only used for write requests and must not be freed
  204. * until the callback has been called.
  205. *
  206. * @param card the card from which to send the request
  207. * @param tcode the tcode for this transaction. Do not use
  208. * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
  209. * etc. to specify tcode and ext_tcode.
  210. * @param node_id the destination node ID (bus ID and PHY ID concatenated)
  211. * @param generation the generation for which node_id is valid
  212. * @param speed the speed to use for sending the request
  213. * @param offset the 48 bit offset on the destination node
  214. * @param payload the data payload for the request subaction
  215. * @param length the length in bytes of the data to read
  216. * @param callback function to be called when the transaction is completed
  217. * @param callback_data pointer to arbitrary data, which will be
  218. * passed to the callback
  219. */
  220. void
  221. fw_send_request(struct fw_card *card, struct fw_transaction *t,
  222. int tcode, int node_id, int generation, int speed,
  223. unsigned long long offset,
  224. void *payload, size_t length,
  225. fw_transaction_callback_t callback, void *callback_data)
  226. {
  227. unsigned long flags;
  228. int tlabel, source;
  229. /*
  230. * Bump the flush timer up 100ms first of all so we
  231. * don't race with a flush timer callback.
  232. */
  233. mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
  234. /*
  235. * Allocate tlabel from the bitmap and put the transaction on
  236. * the list while holding the card spinlock.
  237. */
  238. spin_lock_irqsave(&card->lock, flags);
  239. source = card->node_id;
  240. tlabel = card->current_tlabel;
  241. if (card->tlabel_mask & (1 << tlabel)) {
  242. spin_unlock_irqrestore(&card->lock, flags);
  243. callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  244. return;
  245. }
  246. card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
  247. card->tlabel_mask |= (1 << tlabel);
  248. list_add_tail(&t->link, &card->transaction_list);
  249. spin_unlock_irqrestore(&card->lock, flags);
  250. /* Initialize rest of transaction, fill out packet and send it. */
  251. t->node_id = node_id;
  252. t->tlabel = tlabel;
  253. t->callback = callback;
  254. t->callback_data = callback_data;
  255. fw_fill_request(&t->packet, tcode, t->tlabel,
  256. node_id, source, generation,
  257. speed, offset, payload, length);
  258. t->packet.callback = transmit_complete_callback;
  259. card->driver->send_request(card, &t->packet);
  260. }
  261. EXPORT_SYMBOL(fw_send_request);
  262. struct fw_phy_packet {
  263. struct fw_packet packet;
  264. struct completion done;
  265. struct kref kref;
  266. };
  267. static void phy_packet_release(struct kref *kref)
  268. {
  269. struct fw_phy_packet *p =
  270. container_of(kref, struct fw_phy_packet, kref);
  271. kfree(p);
  272. }
  273. static void transmit_phy_packet_callback(struct fw_packet *packet,
  274. struct fw_card *card, int status)
  275. {
  276. struct fw_phy_packet *p =
  277. container_of(packet, struct fw_phy_packet, packet);
  278. complete(&p->done);
  279. kref_put(&p->kref, phy_packet_release);
  280. }
  281. void fw_send_phy_config(struct fw_card *card,
  282. int node_id, int generation, int gap_count)
  283. {
  284. struct fw_phy_packet *p;
  285. long timeout = DIV_ROUND_UP(HZ, 10);
  286. u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
  287. PHY_CONFIG_ROOT_ID(node_id) |
  288. PHY_CONFIG_GAP_COUNT(gap_count);
  289. p = kmalloc(sizeof(*p), GFP_KERNEL);
  290. if (p == NULL)
  291. return;
  292. p->packet.header[0] = data;
  293. p->packet.header[1] = ~data;
  294. p->packet.header_length = 8;
  295. p->packet.payload_length = 0;
  296. p->packet.speed = SCODE_100;
  297. p->packet.generation = generation;
  298. p->packet.callback = transmit_phy_packet_callback;
  299. init_completion(&p->done);
  300. kref_set(&p->kref, 2);
  301. card->driver->send_request(card, &p->packet);
  302. timeout = wait_for_completion_timeout(&p->done, timeout);
  303. kref_put(&p->kref, phy_packet_release);
  304. /* will leak p if the callback is never executed */
  305. WARN_ON(timeout == 0);
  306. }
  307. void fw_flush_transactions(struct fw_card *card)
  308. {
  309. struct fw_transaction *t, *next;
  310. struct list_head list;
  311. unsigned long flags;
  312. INIT_LIST_HEAD(&list);
  313. spin_lock_irqsave(&card->lock, flags);
  314. list_splice_init(&card->transaction_list, &list);
  315. card->tlabel_mask = 0;
  316. spin_unlock_irqrestore(&card->lock, flags);
  317. list_for_each_entry_safe(t, next, &list, link) {
  318. card->driver->cancel_packet(card, &t->packet);
  319. /*
  320. * At this point cancel_packet will never call the
  321. * transaction callback, since we just took all the
  322. * transactions out of the list. So do it here.
  323. */
  324. t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
  325. }
  326. }
  327. static struct fw_address_handler *
  328. lookup_overlapping_address_handler(struct list_head *list,
  329. unsigned long long offset, size_t length)
  330. {
  331. struct fw_address_handler *handler;
  332. list_for_each_entry(handler, list, link) {
  333. if (handler->offset < offset + length &&
  334. offset < handler->offset + handler->length)
  335. return handler;
  336. }
  337. return NULL;
  338. }
  339. static struct fw_address_handler *
  340. lookup_enclosing_address_handler(struct list_head *list,
  341. unsigned long long offset, size_t length)
  342. {
  343. struct fw_address_handler *handler;
  344. list_for_each_entry(handler, list, link) {
  345. if (handler->offset <= offset &&
  346. offset + length <= handler->offset + handler->length)
  347. return handler;
  348. }
  349. return NULL;
  350. }
  351. static DEFINE_SPINLOCK(address_handler_lock);
  352. static LIST_HEAD(address_handler_list);
  353. const struct fw_address_region fw_high_memory_region =
  354. { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
  355. EXPORT_SYMBOL(fw_high_memory_region);
  356. #if 0
  357. const struct fw_address_region fw_low_memory_region =
  358. { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
  359. const struct fw_address_region fw_private_region =
  360. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  361. const struct fw_address_region fw_csr_region =
  362. { .start = CSR_REGISTER_BASE,
  363. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
  364. const struct fw_address_region fw_unit_space_region =
  365. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  366. #endif /* 0 */
  367. /**
  368. * Allocate a range of addresses in the node space of the OHCI
  369. * controller. When a request is received that falls within the
  370. * specified address range, the specified callback is invoked. The
  371. * parameters passed to the callback give the details of the
  372. * particular request.
  373. *
  374. * Return value: 0 on success, non-zero otherwise.
  375. * The start offset of the handler's address region is determined by
  376. * fw_core_add_address_handler() and is returned in handler->offset.
  377. * The offset is quadlet-aligned.
  378. */
  379. int
  380. fw_core_add_address_handler(struct fw_address_handler *handler,
  381. const struct fw_address_region *region)
  382. {
  383. struct fw_address_handler *other;
  384. unsigned long flags;
  385. int ret = -EBUSY;
  386. spin_lock_irqsave(&address_handler_lock, flags);
  387. handler->offset = roundup(region->start, 4);
  388. while (handler->offset + handler->length <= region->end) {
  389. other =
  390. lookup_overlapping_address_handler(&address_handler_list,
  391. handler->offset,
  392. handler->length);
  393. if (other != NULL) {
  394. handler->offset =
  395. roundup(other->offset + other->length, 4);
  396. } else {
  397. list_add_tail(&handler->link, &address_handler_list);
  398. ret = 0;
  399. break;
  400. }
  401. }
  402. spin_unlock_irqrestore(&address_handler_lock, flags);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL(fw_core_add_address_handler);
  406. /**
  407. * Deallocate a range of addresses allocated with fw_allocate. This
  408. * will call the associated callback one last time with a the special
  409. * tcode TCODE_DEALLOCATE, to let the client destroy the registered
  410. * callback data. For convenience, the callback parameters offset and
  411. * length are set to the start and the length respectively for the
  412. * deallocated region, payload is set to NULL.
  413. */
  414. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  415. {
  416. unsigned long flags;
  417. spin_lock_irqsave(&address_handler_lock, flags);
  418. list_del(&handler->link);
  419. spin_unlock_irqrestore(&address_handler_lock, flags);
  420. }
  421. EXPORT_SYMBOL(fw_core_remove_address_handler);
  422. struct fw_request {
  423. struct fw_packet response;
  424. u32 request_header[4];
  425. int ack;
  426. u32 length;
  427. u32 data[0];
  428. };
  429. static void
  430. free_response_callback(struct fw_packet *packet,
  431. struct fw_card *card, int status)
  432. {
  433. struct fw_request *request;
  434. request = container_of(packet, struct fw_request, response);
  435. kfree(request);
  436. }
  437. void
  438. fw_fill_response(struct fw_packet *response, u32 *request_header,
  439. int rcode, void *payload, size_t length)
  440. {
  441. int tcode, tlabel, extended_tcode, source, destination;
  442. tcode = HEADER_GET_TCODE(request_header[0]);
  443. tlabel = HEADER_GET_TLABEL(request_header[0]);
  444. source = HEADER_GET_DESTINATION(request_header[0]);
  445. destination = HEADER_GET_SOURCE(request_header[1]);
  446. extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
  447. response->header[0] =
  448. HEADER_RETRY(RETRY_1) |
  449. HEADER_TLABEL(tlabel) |
  450. HEADER_DESTINATION(destination);
  451. response->header[1] =
  452. HEADER_SOURCE(source) |
  453. HEADER_RCODE(rcode);
  454. response->header[2] = 0;
  455. switch (tcode) {
  456. case TCODE_WRITE_QUADLET_REQUEST:
  457. case TCODE_WRITE_BLOCK_REQUEST:
  458. response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
  459. response->header_length = 12;
  460. response->payload_length = 0;
  461. break;
  462. case TCODE_READ_QUADLET_REQUEST:
  463. response->header[0] |=
  464. HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
  465. if (payload != NULL)
  466. response->header[3] = *(u32 *)payload;
  467. else
  468. response->header[3] = 0;
  469. response->header_length = 16;
  470. response->payload_length = 0;
  471. break;
  472. case TCODE_READ_BLOCK_REQUEST:
  473. case TCODE_LOCK_REQUEST:
  474. response->header[0] |= HEADER_TCODE(tcode + 2);
  475. response->header[3] =
  476. HEADER_DATA_LENGTH(length) |
  477. HEADER_EXTENDED_TCODE(extended_tcode);
  478. response->header_length = 16;
  479. response->payload = payload;
  480. response->payload_length = length;
  481. break;
  482. default:
  483. BUG();
  484. return;
  485. }
  486. }
  487. EXPORT_SYMBOL(fw_fill_response);
  488. static struct fw_request *
  489. allocate_request(struct fw_packet *p)
  490. {
  491. struct fw_request *request;
  492. u32 *data, length;
  493. int request_tcode, t;
  494. request_tcode = HEADER_GET_TCODE(p->header[0]);
  495. switch (request_tcode) {
  496. case TCODE_WRITE_QUADLET_REQUEST:
  497. data = &p->header[3];
  498. length = 4;
  499. break;
  500. case TCODE_WRITE_BLOCK_REQUEST:
  501. case TCODE_LOCK_REQUEST:
  502. data = p->payload;
  503. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  504. break;
  505. case TCODE_READ_QUADLET_REQUEST:
  506. data = NULL;
  507. length = 4;
  508. break;
  509. case TCODE_READ_BLOCK_REQUEST:
  510. data = NULL;
  511. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  512. break;
  513. default:
  514. fw_error("ERROR - corrupt request received - %08x %08x %08x\n",
  515. p->header[0], p->header[1], p->header[2]);
  516. return NULL;
  517. }
  518. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  519. if (request == NULL)
  520. return NULL;
  521. t = (p->timestamp & 0x1fff) + 4000;
  522. if (t >= 8000)
  523. t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
  524. else
  525. t = (p->timestamp & ~0x1fff) + t;
  526. request->response.speed = p->speed;
  527. request->response.timestamp = t;
  528. request->response.generation = p->generation;
  529. request->response.ack = 0;
  530. request->response.callback = free_response_callback;
  531. request->ack = p->ack;
  532. request->length = length;
  533. if (data)
  534. memcpy(request->data, data, length);
  535. memcpy(request->request_header, p->header, sizeof(p->header));
  536. return request;
  537. }
  538. void
  539. fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
  540. {
  541. /*
  542. * Broadcast packets are reported as ACK_COMPLETE, so this
  543. * check is sufficient to ensure we don't send response to
  544. * broadcast packets or posted writes.
  545. */
  546. if (request->ack != ACK_PENDING) {
  547. kfree(request);
  548. return;
  549. }
  550. if (rcode == RCODE_COMPLETE)
  551. fw_fill_response(&request->response, request->request_header,
  552. rcode, request->data, request->length);
  553. else
  554. fw_fill_response(&request->response, request->request_header,
  555. rcode, NULL, 0);
  556. card->driver->send_response(card, &request->response);
  557. }
  558. EXPORT_SYMBOL(fw_send_response);
  559. void
  560. fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  561. {
  562. struct fw_address_handler *handler;
  563. struct fw_request *request;
  564. unsigned long long offset;
  565. unsigned long flags;
  566. int tcode, destination, source;
  567. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  568. return;
  569. request = allocate_request(p);
  570. if (request == NULL) {
  571. /* FIXME: send statically allocated busy packet. */
  572. return;
  573. }
  574. offset =
  575. ((unsigned long long)
  576. HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) | p->header[2];
  577. tcode = HEADER_GET_TCODE(p->header[0]);
  578. destination = HEADER_GET_DESTINATION(p->header[0]);
  579. source = HEADER_GET_SOURCE(p->header[1]);
  580. spin_lock_irqsave(&address_handler_lock, flags);
  581. handler = lookup_enclosing_address_handler(&address_handler_list,
  582. offset, request->length);
  583. spin_unlock_irqrestore(&address_handler_lock, flags);
  584. /*
  585. * FIXME: lookup the fw_node corresponding to the sender of
  586. * this request and pass that to the address handler instead
  587. * of the node ID. We may also want to move the address
  588. * allocations to fw_node so we only do this callback if the
  589. * upper layers registered it for this node.
  590. */
  591. if (handler == NULL)
  592. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  593. else
  594. handler->address_callback(card, request,
  595. tcode, destination, source,
  596. p->generation, p->speed, offset,
  597. request->data, request->length,
  598. handler->callback_data);
  599. }
  600. EXPORT_SYMBOL(fw_core_handle_request);
  601. void
  602. fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  603. {
  604. struct fw_transaction *t;
  605. unsigned long flags;
  606. u32 *data;
  607. size_t data_length;
  608. int tcode, tlabel, destination, source, rcode;
  609. tcode = HEADER_GET_TCODE(p->header[0]);
  610. tlabel = HEADER_GET_TLABEL(p->header[0]);
  611. destination = HEADER_GET_DESTINATION(p->header[0]);
  612. source = HEADER_GET_SOURCE(p->header[1]);
  613. rcode = HEADER_GET_RCODE(p->header[1]);
  614. spin_lock_irqsave(&card->lock, flags);
  615. list_for_each_entry(t, &card->transaction_list, link) {
  616. if (t->node_id == source && t->tlabel == tlabel) {
  617. list_del(&t->link);
  618. card->tlabel_mask &= ~(1 << t->tlabel);
  619. break;
  620. }
  621. }
  622. spin_unlock_irqrestore(&card->lock, flags);
  623. if (&t->link == &card->transaction_list) {
  624. fw_notify("Unsolicited response (source %x, tlabel %x)\n",
  625. source, tlabel);
  626. return;
  627. }
  628. /*
  629. * FIXME: sanity check packet, is length correct, does tcodes
  630. * and addresses match.
  631. */
  632. switch (tcode) {
  633. case TCODE_READ_QUADLET_RESPONSE:
  634. data = (u32 *) &p->header[3];
  635. data_length = 4;
  636. break;
  637. case TCODE_WRITE_RESPONSE:
  638. data = NULL;
  639. data_length = 0;
  640. break;
  641. case TCODE_READ_BLOCK_RESPONSE:
  642. case TCODE_LOCK_RESPONSE:
  643. data = p->payload;
  644. data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
  645. break;
  646. default:
  647. /* Should never happen, this is just to shut up gcc. */
  648. data = NULL;
  649. data_length = 0;
  650. break;
  651. }
  652. /*
  653. * The response handler may be executed while the request handler
  654. * is still pending. Cancel the request handler.
  655. */
  656. card->driver->cancel_packet(card, &t->packet);
  657. t->callback(card, rcode, data, data_length, t->callback_data);
  658. }
  659. EXPORT_SYMBOL(fw_core_handle_response);
  660. static const struct fw_address_region topology_map_region =
  661. { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
  662. .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
  663. static void
  664. handle_topology_map(struct fw_card *card, struct fw_request *request,
  665. int tcode, int destination, int source,
  666. int generation, int speed,
  667. unsigned long long offset,
  668. void *payload, size_t length, void *callback_data)
  669. {
  670. int i, start, end;
  671. __be32 *map;
  672. if (!TCODE_IS_READ_REQUEST(tcode)) {
  673. fw_send_response(card, request, RCODE_TYPE_ERROR);
  674. return;
  675. }
  676. if ((offset & 3) > 0 || (length & 3) > 0) {
  677. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  678. return;
  679. }
  680. start = (offset - topology_map_region.start) / 4;
  681. end = start + length / 4;
  682. map = payload;
  683. for (i = 0; i < length / 4; i++)
  684. map[i] = cpu_to_be32(card->topology_map[start + i]);
  685. fw_send_response(card, request, RCODE_COMPLETE);
  686. }
  687. static struct fw_address_handler topology_map = {
  688. .length = 0x200,
  689. .address_callback = handle_topology_map,
  690. };
  691. static const struct fw_address_region registers_region =
  692. { .start = CSR_REGISTER_BASE,
  693. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
  694. static void
  695. handle_registers(struct fw_card *card, struct fw_request *request,
  696. int tcode, int destination, int source,
  697. int generation, int speed,
  698. unsigned long long offset,
  699. void *payload, size_t length, void *callback_data)
  700. {
  701. int reg = offset & ~CSR_REGISTER_BASE;
  702. unsigned long long bus_time;
  703. __be32 *data = payload;
  704. switch (reg) {
  705. case CSR_CYCLE_TIME:
  706. case CSR_BUS_TIME:
  707. if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
  708. fw_send_response(card, request, RCODE_TYPE_ERROR);
  709. break;
  710. }
  711. bus_time = card->driver->get_bus_time(card);
  712. if (reg == CSR_CYCLE_TIME)
  713. *data = cpu_to_be32(bus_time);
  714. else
  715. *data = cpu_to_be32(bus_time >> 25);
  716. fw_send_response(card, request, RCODE_COMPLETE);
  717. break;
  718. case CSR_BUS_MANAGER_ID:
  719. case CSR_BANDWIDTH_AVAILABLE:
  720. case CSR_CHANNELS_AVAILABLE_HI:
  721. case CSR_CHANNELS_AVAILABLE_LO:
  722. /*
  723. * FIXME: these are handled by the OHCI hardware and
  724. * the stack never sees these request. If we add
  725. * support for a new type of controller that doesn't
  726. * handle this in hardware we need to deal with these
  727. * transactions.
  728. */
  729. BUG();
  730. break;
  731. case CSR_BUSY_TIMEOUT:
  732. /* FIXME: Implement this. */
  733. default:
  734. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  735. break;
  736. }
  737. }
  738. static struct fw_address_handler registers = {
  739. .length = 0x400,
  740. .address_callback = handle_registers,
  741. };
  742. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  743. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  744. MODULE_LICENSE("GPL");
  745. static const u32 vendor_textual_descriptor[] = {
  746. /* textual descriptor leaf () */
  747. 0x00060000,
  748. 0x00000000,
  749. 0x00000000,
  750. 0x4c696e75, /* L i n u */
  751. 0x78204669, /* x F i */
  752. 0x72657769, /* r e w i */
  753. 0x72650000, /* r e */
  754. };
  755. static const u32 model_textual_descriptor[] = {
  756. /* model descriptor leaf () */
  757. 0x00030000,
  758. 0x00000000,
  759. 0x00000000,
  760. 0x4a756a75, /* J u j u */
  761. };
  762. static struct fw_descriptor vendor_id_descriptor = {
  763. .length = ARRAY_SIZE(vendor_textual_descriptor),
  764. .immediate = 0x03d00d1e,
  765. .key = 0x81000000,
  766. .data = vendor_textual_descriptor,
  767. };
  768. static struct fw_descriptor model_id_descriptor = {
  769. .length = ARRAY_SIZE(model_textual_descriptor),
  770. .immediate = 0x17000001,
  771. .key = 0x81000000,
  772. .data = model_textual_descriptor,
  773. };
  774. static int __init fw_core_init(void)
  775. {
  776. int retval;
  777. retval = bus_register(&fw_bus_type);
  778. if (retval < 0)
  779. return retval;
  780. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  781. if (fw_cdev_major < 0) {
  782. bus_unregister(&fw_bus_type);
  783. return fw_cdev_major;
  784. }
  785. retval = fw_core_add_address_handler(&topology_map,
  786. &topology_map_region);
  787. BUG_ON(retval < 0);
  788. retval = fw_core_add_address_handler(&registers,
  789. &registers_region);
  790. BUG_ON(retval < 0);
  791. /* Add the vendor textual descriptor. */
  792. retval = fw_core_add_descriptor(&vendor_id_descriptor);
  793. BUG_ON(retval < 0);
  794. retval = fw_core_add_descriptor(&model_id_descriptor);
  795. BUG_ON(retval < 0);
  796. return 0;
  797. }
  798. static void __exit fw_core_cleanup(void)
  799. {
  800. unregister_chrdev(fw_cdev_major, "firewire");
  801. bus_unregister(&fw_bus_type);
  802. }
  803. module_init(fw_core_init);
  804. module_exit(fw_core_cleanup);