fw-transaction.c 26 KB

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