fw-transaction.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/poll.h>
  27. #include <linux/list.h>
  28. #include <linux/kthread.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/semaphore.h>
  31. #include "fw-transaction.h"
  32. #include "fw-topology.h"
  33. #include "fw-device.h"
  34. #define HEADER_PRI(pri) ((pri) << 0)
  35. #define HEADER_TCODE(tcode) ((tcode) << 4)
  36. #define HEADER_RETRY(retry) ((retry) << 8)
  37. #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
  38. #define HEADER_DESTINATION(destination) ((destination) << 16)
  39. #define HEADER_SOURCE(source) ((source) << 16)
  40. #define HEADER_RCODE(rcode) ((rcode) << 12)
  41. #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
  42. #define HEADER_DATA_LENGTH(length) ((length) << 16)
  43. #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
  44. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  45. #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
  46. #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
  47. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  48. #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
  49. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  50. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  51. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  52. #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
  53. #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
  54. #define PHY_IDENTIFIER(id) ((id) << 30)
  55. static int
  56. close_transaction(struct fw_transaction *transaction,
  57. struct fw_card *card, int rcode,
  58. u32 *payload, size_t length)
  59. {
  60. struct fw_transaction *t;
  61. unsigned long flags;
  62. spin_lock_irqsave(&card->lock, flags);
  63. list_for_each_entry(t, &card->transaction_list, link) {
  64. if (t == transaction) {
  65. list_del(&t->link);
  66. card->tlabel_mask &= ~(1 << t->tlabel);
  67. break;
  68. }
  69. }
  70. spin_unlock_irqrestore(&card->lock, flags);
  71. if (&t->link != &card->transaction_list) {
  72. t->callback(card, rcode, payload, length, t->callback_data);
  73. return 0;
  74. }
  75. return -ENOENT;
  76. }
  77. /*
  78. * Only valid for transactions that are potentially pending (ie have
  79. * been sent).
  80. */
  81. int
  82. fw_cancel_transaction(struct fw_card *card,
  83. struct fw_transaction *transaction)
  84. {
  85. /*
  86. * Cancel the packet transmission if it's still queued. That
  87. * will call the packet transmission callback which cancels
  88. * the transaction.
  89. */
  90. if (card->driver->cancel_packet(card, &transaction->packet) == 0)
  91. return 0;
  92. /*
  93. * If the request packet has already been sent, we need to see
  94. * if the transaction is still pending and remove it in that case.
  95. */
  96. return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
  97. }
  98. EXPORT_SYMBOL(fw_cancel_transaction);
  99. static void
  100. transmit_complete_callback(struct fw_packet *packet,
  101. struct fw_card *card, int status)
  102. {
  103. struct fw_transaction *t =
  104. container_of(packet, struct fw_transaction, packet);
  105. switch (status) {
  106. case ACK_COMPLETE:
  107. close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
  108. break;
  109. case ACK_PENDING:
  110. t->timestamp = packet->timestamp;
  111. break;
  112. case ACK_BUSY_X:
  113. case ACK_BUSY_A:
  114. case ACK_BUSY_B:
  115. close_transaction(t, card, RCODE_BUSY, NULL, 0);
  116. break;
  117. case ACK_DATA_ERROR:
  118. close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0);
  119. break;
  120. case ACK_TYPE_ERROR:
  121. close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0);
  122. break;
  123. default:
  124. /*
  125. * In this case the ack is really a juju specific
  126. * rcode, so just forward that to the callback.
  127. */
  128. close_transaction(t, card, status, NULL, 0);
  129. break;
  130. }
  131. }
  132. static void
  133. fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  134. int node_id, int source_id, int generation, int speed,
  135. unsigned long long offset, void *payload, size_t length)
  136. {
  137. int ext_tcode;
  138. if (tcode > 0x10) {
  139. ext_tcode = tcode - 0x10;
  140. tcode = TCODE_LOCK_REQUEST;
  141. } else
  142. ext_tcode = 0;
  143. packet->header[0] =
  144. HEADER_RETRY(RETRY_X) |
  145. HEADER_TLABEL(tlabel) |
  146. HEADER_TCODE(tcode) |
  147. HEADER_DESTINATION(node_id);
  148. packet->header[1] =
  149. HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
  150. packet->header[2] =
  151. offset;
  152. switch (tcode) {
  153. case TCODE_WRITE_QUADLET_REQUEST:
  154. packet->header[3] = *(u32 *)payload;
  155. packet->header_length = 16;
  156. packet->payload_length = 0;
  157. break;
  158. case TCODE_LOCK_REQUEST:
  159. case TCODE_WRITE_BLOCK_REQUEST:
  160. packet->header[3] =
  161. HEADER_DATA_LENGTH(length) |
  162. HEADER_EXTENDED_TCODE(ext_tcode);
  163. packet->header_length = 16;
  164. packet->payload = payload;
  165. packet->payload_length = length;
  166. break;
  167. case TCODE_READ_QUADLET_REQUEST:
  168. packet->header_length = 12;
  169. packet->payload_length = 0;
  170. break;
  171. case TCODE_READ_BLOCK_REQUEST:
  172. packet->header[3] =
  173. HEADER_DATA_LENGTH(length) |
  174. HEADER_EXTENDED_TCODE(ext_tcode);
  175. packet->header_length = 16;
  176. packet->payload_length = 0;
  177. break;
  178. }
  179. packet->speed = speed;
  180. packet->generation = generation;
  181. packet->ack = 0;
  182. }
  183. /**
  184. * This function provides low-level access to the IEEE1394 transaction
  185. * logic. Most C programs would use either fw_read(), fw_write() or
  186. * fw_lock() instead - those function are convenience wrappers for
  187. * this function. The fw_send_request() function is primarily
  188. * provided as a flexible, one-stop entry point for languages bindings
  189. * and protocol bindings.
  190. *
  191. * FIXME: Document this function further, in particular the possible
  192. * values for rcode in the callback. In short, we map ACK_COMPLETE to
  193. * RCODE_COMPLETE, internal errors set errno and set rcode to
  194. * RCODE_SEND_ERROR (which is out of range for standard ieee1394
  195. * rcodes). All other rcodes are forwarded unchanged. For all
  196. * errors, payload is NULL, length is 0.
  197. *
  198. * Can not expect the callback to be called before the function
  199. * returns, though this does happen in some cases (ACK_COMPLETE and
  200. * errors).
  201. *
  202. * The payload is only used for write requests and must not be freed
  203. * until the callback has been called.
  204. *
  205. * @param card the card from which to send the request
  206. * @param tcode the tcode for this transaction. Do not use
  207. * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
  208. * etc. to specify tcode and ext_tcode.
  209. * @param node_id the destination node ID (bus ID and PHY ID concatenated)
  210. * @param generation the generation for which node_id is valid
  211. * @param speed the speed to use for sending the request
  212. * @param offset the 48 bit offset on the destination node
  213. * @param payload the data payload for the request subaction
  214. * @param length the length in bytes of the data to read
  215. * @param callback function to be called when the transaction is completed
  216. * @param callback_data pointer to arbitrary data, which will be
  217. * passed to the callback
  218. */
  219. void
  220. fw_send_request(struct fw_card *card, struct fw_transaction *t,
  221. int tcode, int node_id, int generation, int speed,
  222. unsigned long long offset,
  223. void *payload, size_t length,
  224. fw_transaction_callback_t callback, void *callback_data)
  225. {
  226. unsigned long flags;
  227. int tlabel, source;
  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. source = card->node_id;
  239. tlabel = card->current_tlabel;
  240. if (card->tlabel_mask & (1 << tlabel)) {
  241. spin_unlock_irqrestore(&card->lock, flags);
  242. callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  243. return;
  244. }
  245. card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
  246. card->tlabel_mask |= (1 << tlabel);
  247. list_add_tail(&t->link, &card->transaction_list);
  248. spin_unlock_irqrestore(&card->lock, flags);
  249. /* Initialize rest of transaction, fill out packet and send it. */
  250. t->node_id = node_id;
  251. t->tlabel = tlabel;
  252. t->callback = callback;
  253. t->callback_data = callback_data;
  254. fw_fill_request(&t->packet, tcode, t->tlabel,
  255. node_id, source, generation,
  256. speed, offset, payload, length);
  257. t->packet.callback = transmit_complete_callback;
  258. card->driver->send_request(card, &t->packet);
  259. }
  260. EXPORT_SYMBOL(fw_send_request);
  261. static void
  262. transmit_phy_packet_callback(struct fw_packet *packet,
  263. struct fw_card *card, int status)
  264. {
  265. kfree(packet);
  266. }
  267. static void send_phy_packet(struct fw_card *card, u32 data, int generation)
  268. {
  269. struct fw_packet *packet;
  270. packet = kzalloc(sizeof(*packet), GFP_ATOMIC);
  271. if (packet == NULL)
  272. return;
  273. packet->header[0] = data;
  274. packet->header[1] = ~data;
  275. packet->header_length = 8;
  276. packet->payload_length = 0;
  277. packet->speed = SCODE_100;
  278. packet->generation = generation;
  279. packet->callback = transmit_phy_packet_callback;
  280. card->driver->send_request(card, packet);
  281. }
  282. void fw_send_phy_config(struct fw_card *card,
  283. int node_id, int generation, int gap_count)
  284. {
  285. u32 q;
  286. q = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
  287. PHY_CONFIG_ROOT_ID(node_id) |
  288. PHY_CONFIG_GAP_COUNT(gap_count);
  289. send_phy_packet(card, q, generation);
  290. }
  291. void fw_flush_transactions(struct fw_card *card)
  292. {
  293. struct fw_transaction *t, *next;
  294. struct list_head list;
  295. unsigned long flags;
  296. INIT_LIST_HEAD(&list);
  297. spin_lock_irqsave(&card->lock, flags);
  298. list_splice_init(&card->transaction_list, &list);
  299. card->tlabel_mask = 0;
  300. spin_unlock_irqrestore(&card->lock, flags);
  301. list_for_each_entry_safe(t, next, &list, link) {
  302. card->driver->cancel_packet(card, &t->packet);
  303. /*
  304. * At this point cancel_packet will never call the
  305. * transaction callback, since we just took all the
  306. * transactions out of the list. So do it here.
  307. */
  308. t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
  309. }
  310. }
  311. static struct fw_address_handler *
  312. lookup_overlapping_address_handler(struct list_head *list,
  313. unsigned long long offset, size_t length)
  314. {
  315. struct fw_address_handler *handler;
  316. list_for_each_entry(handler, list, link) {
  317. if (handler->offset < offset + length &&
  318. offset < handler->offset + handler->length)
  319. return handler;
  320. }
  321. return NULL;
  322. }
  323. static struct fw_address_handler *
  324. lookup_enclosing_address_handler(struct list_head *list,
  325. unsigned long long offset, size_t length)
  326. {
  327. struct fw_address_handler *handler;
  328. list_for_each_entry(handler, list, link) {
  329. if (handler->offset <= offset &&
  330. offset + length <= handler->offset + handler->length)
  331. return handler;
  332. }
  333. return NULL;
  334. }
  335. static DEFINE_SPINLOCK(address_handler_lock);
  336. static LIST_HEAD(address_handler_list);
  337. const struct fw_address_region fw_low_memory_region =
  338. { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
  339. const struct fw_address_region fw_high_memory_region =
  340. { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
  341. const struct fw_address_region fw_private_region =
  342. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  343. const struct fw_address_region fw_csr_region =
  344. { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, };
  345. const struct fw_address_region fw_unit_space_region =
  346. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  347. EXPORT_SYMBOL(fw_low_memory_region);
  348. EXPORT_SYMBOL(fw_high_memory_region);
  349. EXPORT_SYMBOL(fw_private_region);
  350. EXPORT_SYMBOL(fw_csr_region);
  351. EXPORT_SYMBOL(fw_unit_space_region);
  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[0]);
  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. t->callback(card, rcode, data, data_length, t->callback_data);
  637. }
  638. EXPORT_SYMBOL(fw_core_handle_response);
  639. static const struct fw_address_region topology_map_region =
  640. { .start = 0xfffff0001000ull, .end = 0xfffff0001400ull, };
  641. static void
  642. handle_topology_map(struct fw_card *card, struct fw_request *request,
  643. int tcode, int destination, int source,
  644. int generation, int speed,
  645. unsigned long long offset,
  646. void *payload, size_t length, void *callback_data)
  647. {
  648. int i, start, end;
  649. u32 *map;
  650. if (!TCODE_IS_READ_REQUEST(tcode)) {
  651. fw_send_response(card, request, RCODE_TYPE_ERROR);
  652. return;
  653. }
  654. if ((offset & 3) > 0 || (length & 3) > 0) {
  655. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  656. return;
  657. }
  658. start = (offset - topology_map_region.start) / 4;
  659. end = start + length / 4;
  660. map = payload;
  661. for (i = 0; i < length / 4; i++)
  662. map[i] = cpu_to_be32(card->topology_map[start + i]);
  663. fw_send_response(card, request, RCODE_COMPLETE);
  664. }
  665. static struct fw_address_handler topology_map = {
  666. .length = 0x200,
  667. .address_callback = handle_topology_map,
  668. };
  669. static const struct fw_address_region registers_region =
  670. { .start = 0xfffff0000000ull, .end = 0xfffff0000400ull, };
  671. static void
  672. handle_registers(struct fw_card *card, struct fw_request *request,
  673. int tcode, int destination, int source,
  674. int generation, int speed,
  675. unsigned long long offset,
  676. void *payload, size_t length, void *callback_data)
  677. {
  678. int reg = offset - CSR_REGISTER_BASE;
  679. unsigned long long bus_time;
  680. __be32 *data = payload;
  681. switch (reg) {
  682. case CSR_CYCLE_TIME:
  683. case CSR_BUS_TIME:
  684. if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
  685. fw_send_response(card, request, RCODE_TYPE_ERROR);
  686. break;
  687. }
  688. bus_time = card->driver->get_bus_time(card);
  689. if (reg == CSR_CYCLE_TIME)
  690. *data = cpu_to_be32(bus_time);
  691. else
  692. *data = cpu_to_be32(bus_time >> 25);
  693. fw_send_response(card, request, RCODE_COMPLETE);
  694. break;
  695. case CSR_BUS_MANAGER_ID:
  696. case CSR_BANDWIDTH_AVAILABLE:
  697. case CSR_CHANNELS_AVAILABLE_HI:
  698. case CSR_CHANNELS_AVAILABLE_LO:
  699. /*
  700. * FIXME: these are handled by the OHCI hardware and
  701. * the stack never sees these request. If we add
  702. * support for a new type of controller that doesn't
  703. * handle this in hardware we need to deal with these
  704. * transactions.
  705. */
  706. BUG();
  707. break;
  708. case CSR_BUSY_TIMEOUT:
  709. /* FIXME: Implement this. */
  710. default:
  711. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  712. break;
  713. }
  714. }
  715. static struct fw_address_handler registers = {
  716. .length = 0x400,
  717. .address_callback = handle_registers,
  718. };
  719. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  720. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  721. MODULE_LICENSE("GPL");
  722. static const u32 vendor_textual_descriptor[] = {
  723. /* textual descriptor leaf () */
  724. 0x00060000,
  725. 0x00000000,
  726. 0x00000000,
  727. 0x4c696e75, /* L i n u */
  728. 0x78204669, /* x F i */
  729. 0x72657769, /* r e w i */
  730. 0x72650000, /* r e */
  731. };
  732. static const u32 model_textual_descriptor[] = {
  733. /* model descriptor leaf () */
  734. 0x00030000,
  735. 0x00000000,
  736. 0x00000000,
  737. 0x4a756a75, /* J u j u */
  738. };
  739. static struct fw_descriptor vendor_id_descriptor = {
  740. .length = ARRAY_SIZE(vendor_textual_descriptor),
  741. .immediate = 0x03d00d1e,
  742. .key = 0x81000000,
  743. .data = vendor_textual_descriptor,
  744. };
  745. static struct fw_descriptor model_id_descriptor = {
  746. .length = ARRAY_SIZE(model_textual_descriptor),
  747. .immediate = 0x17000001,
  748. .key = 0x81000000,
  749. .data = model_textual_descriptor,
  750. };
  751. static int __init fw_core_init(void)
  752. {
  753. int retval;
  754. retval = bus_register(&fw_bus_type);
  755. if (retval < 0)
  756. return retval;
  757. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  758. if (fw_cdev_major < 0) {
  759. bus_unregister(&fw_bus_type);
  760. return fw_cdev_major;
  761. }
  762. retval = fw_core_add_address_handler(&topology_map,
  763. &topology_map_region);
  764. BUG_ON(retval < 0);
  765. retval = fw_core_add_address_handler(&registers,
  766. &registers_region);
  767. BUG_ON(retval < 0);
  768. /* Add the vendor textual descriptor. */
  769. retval = fw_core_add_descriptor(&vendor_id_descriptor);
  770. BUG_ON(retval < 0);
  771. retval = fw_core_add_descriptor(&model_id_descriptor);
  772. BUG_ON(retval < 0);
  773. return 0;
  774. }
  775. static void __exit fw_core_cleanup(void)
  776. {
  777. unregister_chrdev(fw_cdev_major, "firewire");
  778. bus_unregister(&fw_bus_type);
  779. }
  780. module_init(fw_core_init);
  781. module_exit(fw_core_cleanup);