fw-transaction.c 26 KB

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