fw-transaction.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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, insted 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. int
  360. fw_core_add_address_handler(struct fw_address_handler *handler,
  361. const struct fw_address_region *region)
  362. {
  363. struct fw_address_handler *other;
  364. unsigned long flags;
  365. int ret = -EBUSY;
  366. spin_lock_irqsave(&address_handler_lock, flags);
  367. handler->offset = region->start;
  368. while (handler->offset + handler->length <= region->end) {
  369. other =
  370. lookup_overlapping_address_handler(&address_handler_list,
  371. handler->offset,
  372. handler->length);
  373. if (other != NULL) {
  374. handler->offset += other->length;
  375. } else {
  376. list_add_tail(&handler->link, &address_handler_list);
  377. ret = 0;
  378. break;
  379. }
  380. }
  381. spin_unlock_irqrestore(&address_handler_lock, flags);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL(fw_core_add_address_handler);
  385. /**
  386. * Deallocate a range of addresses allocated with fw_allocate. This
  387. * will call the associated callback one last time with a the special
  388. * tcode TCODE_DEALLOCATE, to let the client destroy the registered
  389. * callback data. For convenience, the callback parameters offset and
  390. * length are set to the start and the length respectively for the
  391. * deallocated region, payload is set to NULL.
  392. */
  393. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  394. {
  395. unsigned long flags;
  396. spin_lock_irqsave(&address_handler_lock, flags);
  397. list_del(&handler->link);
  398. spin_unlock_irqrestore(&address_handler_lock, flags);
  399. }
  400. EXPORT_SYMBOL(fw_core_remove_address_handler);
  401. struct fw_request {
  402. struct fw_packet response;
  403. u32 request_header[4];
  404. int ack;
  405. u32 length;
  406. u32 data[0];
  407. };
  408. static void
  409. free_response_callback(struct fw_packet *packet,
  410. struct fw_card *card, int status)
  411. {
  412. struct fw_request *request;
  413. request = container_of(packet, struct fw_request, response);
  414. kfree(request);
  415. }
  416. void
  417. fw_fill_response(struct fw_packet *response, u32 *request_header,
  418. int rcode, void *payload, size_t length)
  419. {
  420. int tcode, tlabel, extended_tcode, source, destination;
  421. tcode = HEADER_GET_TCODE(request_header[0]);
  422. tlabel = HEADER_GET_TLABEL(request_header[0]);
  423. source = HEADER_GET_DESTINATION(request_header[0]);
  424. destination = HEADER_GET_SOURCE(request_header[1]);
  425. extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
  426. response->header[0] =
  427. HEADER_RETRY(RETRY_1) |
  428. HEADER_TLABEL(tlabel) |
  429. HEADER_DESTINATION(destination);
  430. response->header[1] =
  431. HEADER_SOURCE(source) |
  432. HEADER_RCODE(rcode);
  433. response->header[2] = 0;
  434. switch (tcode) {
  435. case TCODE_WRITE_QUADLET_REQUEST:
  436. case TCODE_WRITE_BLOCK_REQUEST:
  437. response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
  438. response->header_length = 12;
  439. response->payload_length = 0;
  440. break;
  441. case TCODE_READ_QUADLET_REQUEST:
  442. response->header[0] |=
  443. HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
  444. if (payload != NULL)
  445. response->header[3] = *(u32 *)payload;
  446. else
  447. response->header[3] = 0;
  448. response->header_length = 16;
  449. response->payload_length = 0;
  450. break;
  451. case TCODE_READ_BLOCK_REQUEST:
  452. case TCODE_LOCK_REQUEST:
  453. response->header[0] |= HEADER_TCODE(tcode + 2);
  454. response->header[3] =
  455. HEADER_DATA_LENGTH(length) |
  456. HEADER_EXTENDED_TCODE(extended_tcode);
  457. response->header_length = 16;
  458. response->payload = payload;
  459. response->payload_length = length;
  460. break;
  461. default:
  462. BUG();
  463. return;
  464. }
  465. }
  466. EXPORT_SYMBOL(fw_fill_response);
  467. static struct fw_request *
  468. allocate_request(struct fw_packet *p)
  469. {
  470. struct fw_request *request;
  471. u32 *data, length;
  472. int request_tcode, t;
  473. request_tcode = HEADER_GET_TCODE(p->header[0]);
  474. switch (request_tcode) {
  475. case TCODE_WRITE_QUADLET_REQUEST:
  476. data = &p->header[3];
  477. length = 4;
  478. break;
  479. case TCODE_WRITE_BLOCK_REQUEST:
  480. case TCODE_LOCK_REQUEST:
  481. data = p->payload;
  482. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  483. break;
  484. case TCODE_READ_QUADLET_REQUEST:
  485. data = NULL;
  486. length = 4;
  487. break;
  488. case TCODE_READ_BLOCK_REQUEST:
  489. data = NULL;
  490. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  491. break;
  492. default:
  493. BUG();
  494. return NULL;
  495. }
  496. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  497. if (request == NULL)
  498. return NULL;
  499. t = (p->timestamp & 0x1fff) + 4000;
  500. if (t >= 8000)
  501. t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
  502. else
  503. t = (p->timestamp & ~0x1fff) + t;
  504. request->response.speed = p->speed;
  505. request->response.timestamp = t;
  506. request->response.generation = p->generation;
  507. request->response.ack = 0;
  508. request->response.callback = free_response_callback;
  509. request->ack = p->ack;
  510. request->length = length;
  511. if (data)
  512. memcpy(request->data, data, length);
  513. memcpy(request->request_header, p->header, sizeof(p->header));
  514. return request;
  515. }
  516. void
  517. fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
  518. {
  519. /*
  520. * Broadcast packets are reported as ACK_COMPLETE, so this
  521. * check is sufficient to ensure we don't send response to
  522. * broadcast packets or posted writes.
  523. */
  524. if (request->ack != ACK_PENDING)
  525. return;
  526. if (rcode == RCODE_COMPLETE)
  527. fw_fill_response(&request->response, request->request_header,
  528. rcode, request->data, request->length);
  529. else
  530. fw_fill_response(&request->response, request->request_header,
  531. rcode, NULL, 0);
  532. card->driver->send_response(card, &request->response);
  533. }
  534. EXPORT_SYMBOL(fw_send_response);
  535. void
  536. fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  537. {
  538. struct fw_address_handler *handler;
  539. struct fw_request *request;
  540. unsigned long long offset;
  541. unsigned long flags;
  542. int tcode, destination, source;
  543. if (p->payload_length > 2048) {
  544. /* FIXME: send error response. */
  545. return;
  546. }
  547. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  548. return;
  549. request = allocate_request(p);
  550. if (request == NULL) {
  551. /* FIXME: send statically allocated busy packet. */
  552. return;
  553. }
  554. offset =
  555. ((unsigned long long)
  556. HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) | p->header[2];
  557. tcode = HEADER_GET_TCODE(p->header[0]);
  558. destination = HEADER_GET_DESTINATION(p->header[0]);
  559. source = HEADER_GET_SOURCE(p->header[0]);
  560. spin_lock_irqsave(&address_handler_lock, flags);
  561. handler = lookup_enclosing_address_handler(&address_handler_list,
  562. offset, request->length);
  563. spin_unlock_irqrestore(&address_handler_lock, flags);
  564. /*
  565. * FIXME: lookup the fw_node corresponding to the sender of
  566. * this request and pass that to the address handler instead
  567. * of the node ID. We may also want to move the address
  568. * allocations to fw_node so we only do this callback if the
  569. * upper layers registered it for this node.
  570. */
  571. if (handler == NULL)
  572. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  573. else
  574. handler->address_callback(card, request,
  575. tcode, destination, source,
  576. p->generation, p->speed, offset,
  577. request->data, request->length,
  578. handler->callback_data);
  579. }
  580. EXPORT_SYMBOL(fw_core_handle_request);
  581. void
  582. fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  583. {
  584. struct fw_transaction *t;
  585. unsigned long flags;
  586. u32 *data;
  587. size_t data_length;
  588. int tcode, tlabel, destination, source, rcode;
  589. tcode = HEADER_GET_TCODE(p->header[0]);
  590. tlabel = HEADER_GET_TLABEL(p->header[0]);
  591. destination = HEADER_GET_DESTINATION(p->header[0]);
  592. source = HEADER_GET_SOURCE(p->header[1]);
  593. rcode = HEADER_GET_RCODE(p->header[1]);
  594. spin_lock_irqsave(&card->lock, flags);
  595. list_for_each_entry(t, &card->transaction_list, link) {
  596. if (t->node_id == source && t->tlabel == tlabel) {
  597. list_del(&t->link);
  598. card->tlabel_mask &= ~(1 << t->tlabel);
  599. break;
  600. }
  601. }
  602. spin_unlock_irqrestore(&card->lock, flags);
  603. if (&t->link == &card->transaction_list) {
  604. fw_notify("Unsolicited response (source %x, tlabel %x)\n",
  605. source, tlabel);
  606. return;
  607. }
  608. /*
  609. * FIXME: sanity check packet, is length correct, does tcodes
  610. * and addresses match.
  611. */
  612. switch (tcode) {
  613. case TCODE_READ_QUADLET_RESPONSE:
  614. data = (u32 *) &p->header[3];
  615. data_length = 4;
  616. break;
  617. case TCODE_WRITE_RESPONSE:
  618. data = NULL;
  619. data_length = 0;
  620. break;
  621. case TCODE_READ_BLOCK_RESPONSE:
  622. case TCODE_LOCK_RESPONSE:
  623. data = p->payload;
  624. data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
  625. break;
  626. default:
  627. /* Should never happen, this is just to shut up gcc. */
  628. data = NULL;
  629. data_length = 0;
  630. break;
  631. }
  632. t->callback(card, rcode, data, data_length, t->callback_data);
  633. }
  634. EXPORT_SYMBOL(fw_core_handle_response);
  635. const struct fw_address_region topology_map_region =
  636. { .start = 0xfffff0001000ull, .end = 0xfffff0001400ull, };
  637. static void
  638. handle_topology_map(struct fw_card *card, struct fw_request *request,
  639. int tcode, int destination, int source,
  640. int generation, int speed,
  641. unsigned long long offset,
  642. void *payload, size_t length, void *callback_data)
  643. {
  644. int i, start, end;
  645. u32 *map;
  646. if (!TCODE_IS_READ_REQUEST(tcode)) {
  647. fw_send_response(card, request, RCODE_TYPE_ERROR);
  648. return;
  649. }
  650. if ((offset & 3) > 0 || (length & 3) > 0) {
  651. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  652. return;
  653. }
  654. start = (offset - topology_map_region.start) / 4;
  655. end = start + length / 4;
  656. map = payload;
  657. for (i = 0; i < length / 4; i++)
  658. map[i] = cpu_to_be32(card->topology_map[start + i]);
  659. fw_send_response(card, request, RCODE_COMPLETE);
  660. }
  661. static struct fw_address_handler topology_map = {
  662. .length = 0x200,
  663. .address_callback = handle_topology_map,
  664. };
  665. const struct fw_address_region registers_region =
  666. { .start = 0xfffff0000000ull, .end = 0xfffff0000400ull, };
  667. static void
  668. handle_registers(struct fw_card *card, struct fw_request *request,
  669. int tcode, int destination, int source,
  670. int generation, int speed,
  671. unsigned long long offset,
  672. void *payload, size_t length, void *callback_data)
  673. {
  674. int reg = offset - CSR_REGISTER_BASE;
  675. unsigned long long bus_time;
  676. __be32 *data = payload;
  677. switch (reg) {
  678. case CSR_CYCLE_TIME:
  679. case CSR_BUS_TIME:
  680. if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
  681. fw_send_response(card, request, RCODE_TYPE_ERROR);
  682. break;
  683. }
  684. bus_time = card->driver->get_bus_time(card);
  685. if (reg == CSR_CYCLE_TIME)
  686. *data = cpu_to_be32(bus_time);
  687. else
  688. *data = cpu_to_be32(bus_time >> 25);
  689. fw_send_response(card, request, RCODE_COMPLETE);
  690. break;
  691. case CSR_BUS_MANAGER_ID:
  692. case CSR_BANDWIDTH_AVAILABLE:
  693. case CSR_CHANNELS_AVAILABLE_HI:
  694. case CSR_CHANNELS_AVAILABLE_LO:
  695. /*
  696. * FIXME: these are handled by the OHCI hardware and
  697. * the stack never sees these request. If we add
  698. * support for a new type of controller that doesn't
  699. * handle this in hardware we need to deal with these
  700. * transactions.
  701. */
  702. BUG();
  703. break;
  704. case CSR_BUSY_TIMEOUT:
  705. /* FIXME: Implement this. */
  706. default:
  707. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  708. break;
  709. }
  710. }
  711. static struct fw_address_handler registers = {
  712. .length = 0x400,
  713. .address_callback = handle_registers,
  714. };
  715. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  716. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  717. MODULE_LICENSE("GPL");
  718. static const u32 vendor_textual_descriptor[] = {
  719. /* textual descriptor leaf () */
  720. 0x00060000,
  721. 0x00000000,
  722. 0x00000000,
  723. 0x4c696e75, /* L i n u */
  724. 0x78204669, /* x F i */
  725. 0x72657769, /* r e w i */
  726. 0x72650000, /* r e */
  727. };
  728. static const u32 model_textual_descriptor[] = {
  729. /* model descriptor leaf () */
  730. 0x00030000,
  731. 0x00000000,
  732. 0x00000000,
  733. 0x4a756a75, /* J u j u */
  734. };
  735. static struct fw_descriptor vendor_id_descriptor = {
  736. .length = ARRAY_SIZE(vendor_textual_descriptor),
  737. .immediate = 0x03d00d1e,
  738. .key = 0x81000000,
  739. .data = vendor_textual_descriptor,
  740. };
  741. static struct fw_descriptor model_id_descriptor = {
  742. .length = ARRAY_SIZE(model_textual_descriptor),
  743. .immediate = 0x17000001,
  744. .key = 0x81000000,
  745. .data = model_textual_descriptor,
  746. };
  747. static int __init fw_core_init(void)
  748. {
  749. int retval;
  750. retval = bus_register(&fw_bus_type);
  751. if (retval < 0)
  752. return retval;
  753. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  754. if (fw_cdev_major < 0) {
  755. bus_unregister(&fw_bus_type);
  756. return fw_cdev_major;
  757. }
  758. retval = fw_core_add_address_handler(&topology_map,
  759. &topology_map_region);
  760. BUG_ON(retval < 0);
  761. retval = fw_core_add_address_handler(&registers,
  762. &registers_region);
  763. BUG_ON(retval < 0);
  764. /* Add the vendor textual descriptor. */
  765. retval = fw_core_add_descriptor(&vendor_id_descriptor);
  766. BUG_ON(retval < 0);
  767. retval = fw_core_add_descriptor(&model_id_descriptor);
  768. BUG_ON(retval < 0);
  769. return 0;
  770. }
  771. static void __exit fw_core_cleanup(void)
  772. {
  773. unregister_chrdev(fw_cdev_major, "firewire");
  774. bus_unregister(&fw_bus_type);
  775. }
  776. module_init(fw_core_init);
  777. module_exit(fw_core_cleanup);