fw-transaction.c 26 KB

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