fw-transaction.c 25 KB

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