core-transaction.c 31 KB

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