fw-transaction.c 26 KB

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