core-transaction.c 29 KB

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