fw-transaction.c 26 KB

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