core-transaction.c 29 KB

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