core-transaction.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. * fw_send_request() - submit a request packet for transmission
  240. * @card: interface to send the request at
  241. * @t: transaction instance to which the request belongs
  242. * @tcode: transaction code
  243. * @destination_id: destination node ID, consisting of bus_ID and phy_ID
  244. * @generation: bus generation in which request and response are valid
  245. * @speed: transmission speed
  246. * @offset: 48bit wide offset into destination's address space
  247. * @payload: data payload for the request subaction
  248. * @length: length of the payload, in bytes
  249. * @callback: function to be called when the transaction is completed
  250. * @callback_data: data to be passed to the transaction completion callback
  251. *
  252. * Submit a request packet into the asynchronous request transmission queue.
  253. * Can be called from atomic context. If you prefer a blocking API, use
  254. * fw_run_transaction() in a context that can sleep.
  255. *
  256. * In case of lock requests, specify one of the firewire-core specific %TCODE_
  257. * constants instead of %TCODE_LOCK_REQUEST in @tcode.
  258. *
  259. * Make sure that the value in @destination_id is not older than the one in
  260. * @generation. Otherwise the request is in danger to be sent to a wrong node.
  261. *
  262. * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
  263. * needs to synthesize @destination_id with fw_stream_packet_destination_id().
  264. * It will contain tag, channel, and sy data instead of a node ID then.
  265. *
  266. * The payload buffer at @data is going to be DMA-mapped except in case of
  267. * quadlet-sized payload or of local (loopback) requests. Hence make sure that
  268. * the buffer complies with the restrictions for DMA-mapped memory. The
  269. * @payload must not be freed before the @callback is called.
  270. *
  271. * In case of request types without payload, @data is NULL and @length is 0.
  272. *
  273. * After the transaction is completed successfully or unsuccessfully, the
  274. * @callback will be called. Among its parameters is the response code which
  275. * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
  276. * the firewire-core specific %RCODE_SEND_ERROR.
  277. *
  278. * Note some timing corner cases: fw_send_request() may complete much earlier
  279. * than when the request packet actually hits the wire. On the other hand,
  280. * transaction completion and hence execution of @callback may happen even
  281. * before fw_send_request() returns.
  282. */
  283. void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
  284. int destination_id, int generation, int speed,
  285. unsigned long long offset, void *payload, size_t length,
  286. fw_transaction_callback_t callback, void *callback_data)
  287. {
  288. unsigned long flags;
  289. int tlabel;
  290. /*
  291. * Allocate tlabel from the bitmap and put the transaction on
  292. * the list while holding the card spinlock.
  293. */
  294. spin_lock_irqsave(&card->lock, flags);
  295. tlabel = allocate_tlabel(card);
  296. if (tlabel < 0) {
  297. spin_unlock_irqrestore(&card->lock, flags);
  298. callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  299. return;
  300. }
  301. t->node_id = destination_id;
  302. t->tlabel = tlabel;
  303. t->card = card;
  304. setup_timer(&t->split_timeout_timer,
  305. split_transaction_timeout_callback, (unsigned long)t);
  306. /* FIXME: start this timer later, relative to t->timestamp */
  307. mod_timer(&t->split_timeout_timer,
  308. jiffies + card->split_timeout_jiffies);
  309. t->callback = callback;
  310. t->callback_data = callback_data;
  311. fw_fill_request(&t->packet, tcode, t->tlabel,
  312. destination_id, card->node_id, generation,
  313. speed, offset, payload, length);
  314. t->packet.callback = transmit_complete_callback;
  315. list_add_tail(&t->link, &card->transaction_list);
  316. spin_unlock_irqrestore(&card->lock, flags);
  317. card->driver->send_request(card, &t->packet);
  318. }
  319. EXPORT_SYMBOL(fw_send_request);
  320. struct transaction_callback_data {
  321. struct completion done;
  322. void *payload;
  323. int rcode;
  324. };
  325. static void transaction_callback(struct fw_card *card, int rcode,
  326. void *payload, size_t length, void *data)
  327. {
  328. struct transaction_callback_data *d = data;
  329. if (rcode == RCODE_COMPLETE)
  330. memcpy(d->payload, payload, length);
  331. d->rcode = rcode;
  332. complete(&d->done);
  333. }
  334. /**
  335. * fw_run_transaction() - send request and sleep until transaction is completed
  336. *
  337. * Returns the RCODE. See fw_send_request() for parameter documentation.
  338. * Unlike fw_send_request(), @data points to the payload of the request or/and
  339. * to the payload of the response.
  340. */
  341. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  342. int generation, int speed, unsigned long long offset,
  343. void *payload, size_t length)
  344. {
  345. struct transaction_callback_data d;
  346. struct fw_transaction t;
  347. init_timer_on_stack(&t.split_timeout_timer);
  348. init_completion(&d.done);
  349. d.payload = payload;
  350. fw_send_request(card, &t, tcode, destination_id, generation, speed,
  351. offset, payload, length, transaction_callback, &d);
  352. wait_for_completion(&d.done);
  353. destroy_timer_on_stack(&t.split_timeout_timer);
  354. return d.rcode;
  355. }
  356. EXPORT_SYMBOL(fw_run_transaction);
  357. static DEFINE_MUTEX(phy_config_mutex);
  358. static DECLARE_COMPLETION(phy_config_done);
  359. static void transmit_phy_packet_callback(struct fw_packet *packet,
  360. struct fw_card *card, int status)
  361. {
  362. complete(&phy_config_done);
  363. }
  364. static struct fw_packet phy_config_packet = {
  365. .header_length = 8,
  366. .payload_length = 0,
  367. .speed = SCODE_100,
  368. .callback = transmit_phy_packet_callback,
  369. };
  370. void fw_send_phy_config(struct fw_card *card,
  371. int node_id, int generation, int gap_count)
  372. {
  373. long timeout = DIV_ROUND_UP(HZ, 10);
  374. u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
  375. PHY_CONFIG_ROOT_ID(node_id) |
  376. PHY_CONFIG_GAP_COUNT(gap_count);
  377. mutex_lock(&phy_config_mutex);
  378. phy_config_packet.header[0] = data;
  379. phy_config_packet.header[1] = ~data;
  380. phy_config_packet.generation = generation;
  381. INIT_COMPLETION(phy_config_done);
  382. card->driver->send_request(card, &phy_config_packet);
  383. wait_for_completion_timeout(&phy_config_done, timeout);
  384. mutex_unlock(&phy_config_mutex);
  385. }
  386. static struct fw_address_handler *lookup_overlapping_address_handler(
  387. struct list_head *list, unsigned long long offset, size_t length)
  388. {
  389. struct fw_address_handler *handler;
  390. list_for_each_entry(handler, list, link) {
  391. if (handler->offset < offset + length &&
  392. offset < handler->offset + handler->length)
  393. return handler;
  394. }
  395. return NULL;
  396. }
  397. static bool is_enclosing_handler(struct fw_address_handler *handler,
  398. unsigned long long offset, size_t length)
  399. {
  400. return handler->offset <= offset &&
  401. offset + length <= handler->offset + handler->length;
  402. }
  403. static struct fw_address_handler *lookup_enclosing_address_handler(
  404. struct list_head *list, unsigned long long offset, size_t length)
  405. {
  406. struct fw_address_handler *handler;
  407. list_for_each_entry(handler, list, link) {
  408. if (is_enclosing_handler(handler, offset, length))
  409. return handler;
  410. }
  411. return NULL;
  412. }
  413. static DEFINE_SPINLOCK(address_handler_lock);
  414. static LIST_HEAD(address_handler_list);
  415. const struct fw_address_region fw_high_memory_region =
  416. { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
  417. EXPORT_SYMBOL(fw_high_memory_region);
  418. #if 0
  419. const struct fw_address_region fw_low_memory_region =
  420. { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
  421. const struct fw_address_region fw_private_region =
  422. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  423. const struct fw_address_region fw_csr_region =
  424. { .start = CSR_REGISTER_BASE,
  425. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
  426. const struct fw_address_region fw_unit_space_region =
  427. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  428. #endif /* 0 */
  429. static bool is_in_fcp_region(u64 offset, size_t length)
  430. {
  431. return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  432. offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END);
  433. }
  434. /**
  435. * fw_core_add_address_handler() - register for incoming requests
  436. * @handler: callback
  437. * @region: region in the IEEE 1212 node space address range
  438. *
  439. * region->start, ->end, and handler->length have to be quadlet-aligned.
  440. *
  441. * When a request is received that falls within the specified address range,
  442. * the specified callback is invoked. The parameters passed to the callback
  443. * give the details of the particular request.
  444. *
  445. * Return value: 0 on success, non-zero otherwise.
  446. *
  447. * The start offset of the handler's address region is determined by
  448. * fw_core_add_address_handler() and is returned in handler->offset.
  449. *
  450. * Address allocations are exclusive, except for the FCP registers.
  451. */
  452. int fw_core_add_address_handler(struct fw_address_handler *handler,
  453. const struct fw_address_region *region)
  454. {
  455. struct fw_address_handler *other;
  456. unsigned long flags;
  457. int ret = -EBUSY;
  458. if (region->start & 0xffff000000000003ULL ||
  459. region->end & 0xffff000000000003ULL ||
  460. region->start >= region->end ||
  461. handler->length & 3 ||
  462. handler->length == 0)
  463. return -EINVAL;
  464. spin_lock_irqsave(&address_handler_lock, flags);
  465. handler->offset = region->start;
  466. while (handler->offset + handler->length <= region->end) {
  467. if (is_in_fcp_region(handler->offset, handler->length))
  468. other = NULL;
  469. else
  470. other = lookup_overlapping_address_handler
  471. (&address_handler_list,
  472. handler->offset, handler->length);
  473. if (other != NULL) {
  474. handler->offset += other->length;
  475. } else {
  476. list_add_tail(&handler->link, &address_handler_list);
  477. ret = 0;
  478. break;
  479. }
  480. }
  481. spin_unlock_irqrestore(&address_handler_lock, flags);
  482. return ret;
  483. }
  484. EXPORT_SYMBOL(fw_core_add_address_handler);
  485. /**
  486. * fw_core_remove_address_handler() - unregister an address handler
  487. */
  488. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  489. {
  490. unsigned long flags;
  491. spin_lock_irqsave(&address_handler_lock, flags);
  492. list_del(&handler->link);
  493. spin_unlock_irqrestore(&address_handler_lock, flags);
  494. }
  495. EXPORT_SYMBOL(fw_core_remove_address_handler);
  496. struct fw_request {
  497. struct fw_packet response;
  498. u32 request_header[4];
  499. int ack;
  500. u32 length;
  501. u32 data[0];
  502. };
  503. static void free_response_callback(struct fw_packet *packet,
  504. struct fw_card *card, int status)
  505. {
  506. struct fw_request *request;
  507. request = container_of(packet, struct fw_request, response);
  508. kfree(request);
  509. }
  510. int fw_get_response_length(struct fw_request *r)
  511. {
  512. int tcode, ext_tcode, data_length;
  513. tcode = HEADER_GET_TCODE(r->request_header[0]);
  514. switch (tcode) {
  515. case TCODE_WRITE_QUADLET_REQUEST:
  516. case TCODE_WRITE_BLOCK_REQUEST:
  517. return 0;
  518. case TCODE_READ_QUADLET_REQUEST:
  519. return 4;
  520. case TCODE_READ_BLOCK_REQUEST:
  521. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  522. return data_length;
  523. case TCODE_LOCK_REQUEST:
  524. ext_tcode = HEADER_GET_EXTENDED_TCODE(r->request_header[3]);
  525. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  526. switch (ext_tcode) {
  527. case EXTCODE_FETCH_ADD:
  528. case EXTCODE_LITTLE_ADD:
  529. return data_length;
  530. default:
  531. return data_length / 2;
  532. }
  533. default:
  534. WARN(1, "wrong tcode %d", tcode);
  535. return 0;
  536. }
  537. }
  538. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  539. int rcode, void *payload, size_t length)
  540. {
  541. int tcode, tlabel, extended_tcode, source, destination;
  542. tcode = HEADER_GET_TCODE(request_header[0]);
  543. tlabel = HEADER_GET_TLABEL(request_header[0]);
  544. source = HEADER_GET_DESTINATION(request_header[0]);
  545. destination = HEADER_GET_SOURCE(request_header[1]);
  546. extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
  547. response->header[0] =
  548. HEADER_RETRY(RETRY_1) |
  549. HEADER_TLABEL(tlabel) |
  550. HEADER_DESTINATION(destination);
  551. response->header[1] =
  552. HEADER_SOURCE(source) |
  553. HEADER_RCODE(rcode);
  554. response->header[2] = 0;
  555. switch (tcode) {
  556. case TCODE_WRITE_QUADLET_REQUEST:
  557. case TCODE_WRITE_BLOCK_REQUEST:
  558. response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
  559. response->header_length = 12;
  560. response->payload_length = 0;
  561. break;
  562. case TCODE_READ_QUADLET_REQUEST:
  563. response->header[0] |=
  564. HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
  565. if (payload != NULL)
  566. response->header[3] = *(u32 *)payload;
  567. else
  568. response->header[3] = 0;
  569. response->header_length = 16;
  570. response->payload_length = 0;
  571. break;
  572. case TCODE_READ_BLOCK_REQUEST:
  573. case TCODE_LOCK_REQUEST:
  574. response->header[0] |= HEADER_TCODE(tcode + 2);
  575. response->header[3] =
  576. HEADER_DATA_LENGTH(length) |
  577. HEADER_EXTENDED_TCODE(extended_tcode);
  578. response->header_length = 16;
  579. response->payload = payload;
  580. response->payload_length = length;
  581. break;
  582. default:
  583. WARN(1, "wrong tcode %d", tcode);
  584. }
  585. response->payload_mapped = false;
  586. }
  587. EXPORT_SYMBOL(fw_fill_response);
  588. static u32 compute_split_timeout_timestamp(struct fw_card *card,
  589. u32 request_timestamp)
  590. {
  591. unsigned int cycles;
  592. u32 timestamp;
  593. cycles = card->split_timeout_cycles;
  594. cycles += request_timestamp & 0x1fff;
  595. timestamp = request_timestamp & ~0x1fff;
  596. timestamp += (cycles / 8000) << 13;
  597. timestamp |= cycles % 8000;
  598. return timestamp;
  599. }
  600. static struct fw_request *allocate_request(struct fw_card *card,
  601. struct fw_packet *p)
  602. {
  603. struct fw_request *request;
  604. u32 *data, length;
  605. int request_tcode;
  606. request_tcode = HEADER_GET_TCODE(p->header[0]);
  607. switch (request_tcode) {
  608. case TCODE_WRITE_QUADLET_REQUEST:
  609. data = &p->header[3];
  610. length = 4;
  611. break;
  612. case TCODE_WRITE_BLOCK_REQUEST:
  613. case TCODE_LOCK_REQUEST:
  614. data = p->payload;
  615. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  616. break;
  617. case TCODE_READ_QUADLET_REQUEST:
  618. data = NULL;
  619. length = 4;
  620. break;
  621. case TCODE_READ_BLOCK_REQUEST:
  622. data = NULL;
  623. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  624. break;
  625. default:
  626. fw_error("ERROR - corrupt request received - %08x %08x %08x\n",
  627. p->header[0], p->header[1], p->header[2]);
  628. return NULL;
  629. }
  630. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  631. if (request == NULL)
  632. return NULL;
  633. request->response.speed = p->speed;
  634. request->response.timestamp =
  635. compute_split_timeout_timestamp(card, p->timestamp);
  636. request->response.generation = p->generation;
  637. request->response.ack = 0;
  638. request->response.callback = free_response_callback;
  639. request->ack = p->ack;
  640. request->length = length;
  641. if (data)
  642. memcpy(request->data, data, length);
  643. memcpy(request->request_header, p->header, sizeof(p->header));
  644. return request;
  645. }
  646. void fw_send_response(struct fw_card *card,
  647. struct fw_request *request, int rcode)
  648. {
  649. if (WARN_ONCE(!request, "invalid for FCP address handlers"))
  650. return;
  651. /* unified transaction or broadcast transaction: don't respond */
  652. if (request->ack != ACK_PENDING ||
  653. HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
  654. kfree(request);
  655. return;
  656. }
  657. if (rcode == RCODE_COMPLETE)
  658. fw_fill_response(&request->response, request->request_header,
  659. rcode, request->data,
  660. fw_get_response_length(request));
  661. else
  662. fw_fill_response(&request->response, request->request_header,
  663. rcode, NULL, 0);
  664. card->driver->send_response(card, &request->response);
  665. }
  666. EXPORT_SYMBOL(fw_send_response);
  667. static void handle_exclusive_region_request(struct fw_card *card,
  668. struct fw_packet *p,
  669. struct fw_request *request,
  670. unsigned long long offset)
  671. {
  672. struct fw_address_handler *handler;
  673. unsigned long flags;
  674. int tcode, destination, source;
  675. destination = HEADER_GET_DESTINATION(p->header[0]);
  676. source = HEADER_GET_SOURCE(p->header[1]);
  677. tcode = HEADER_GET_TCODE(p->header[0]);
  678. if (tcode == TCODE_LOCK_REQUEST)
  679. tcode = 0x10 + HEADER_GET_EXTENDED_TCODE(p->header[3]);
  680. spin_lock_irqsave(&address_handler_lock, flags);
  681. handler = lookup_enclosing_address_handler(&address_handler_list,
  682. offset, request->length);
  683. spin_unlock_irqrestore(&address_handler_lock, flags);
  684. /*
  685. * FIXME: lookup the fw_node corresponding to the sender of
  686. * this request and pass that to the address handler instead
  687. * of the node ID. We may also want to move the address
  688. * allocations to fw_node so we only do this callback if the
  689. * upper layers registered it for this node.
  690. */
  691. if (handler == NULL)
  692. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  693. else
  694. handler->address_callback(card, request,
  695. tcode, destination, source,
  696. p->generation, offset,
  697. request->data, request->length,
  698. handler->callback_data);
  699. }
  700. static void handle_fcp_region_request(struct fw_card *card,
  701. struct fw_packet *p,
  702. struct fw_request *request,
  703. unsigned long long offset)
  704. {
  705. struct fw_address_handler *handler;
  706. unsigned long flags;
  707. int tcode, destination, source;
  708. if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  709. offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
  710. request->length > 0x200) {
  711. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  712. return;
  713. }
  714. tcode = HEADER_GET_TCODE(p->header[0]);
  715. destination = HEADER_GET_DESTINATION(p->header[0]);
  716. source = HEADER_GET_SOURCE(p->header[1]);
  717. if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
  718. tcode != TCODE_WRITE_BLOCK_REQUEST) {
  719. fw_send_response(card, request, RCODE_TYPE_ERROR);
  720. return;
  721. }
  722. spin_lock_irqsave(&address_handler_lock, flags);
  723. list_for_each_entry(handler, &address_handler_list, link) {
  724. if (is_enclosing_handler(handler, offset, request->length))
  725. handler->address_callback(card, NULL, tcode,
  726. destination, source,
  727. p->generation, offset,
  728. request->data,
  729. request->length,
  730. handler->callback_data);
  731. }
  732. spin_unlock_irqrestore(&address_handler_lock, flags);
  733. fw_send_response(card, request, RCODE_COMPLETE);
  734. }
  735. void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  736. {
  737. struct fw_request *request;
  738. unsigned long long offset;
  739. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  740. return;
  741. request = allocate_request(card, p);
  742. if (request == NULL) {
  743. /* FIXME: send statically allocated busy packet. */
  744. return;
  745. }
  746. offset = ((u64)HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) |
  747. p->header[2];
  748. if (!is_in_fcp_region(offset, request->length))
  749. handle_exclusive_region_request(card, p, request, offset);
  750. else
  751. handle_fcp_region_request(card, p, request, offset);
  752. }
  753. EXPORT_SYMBOL(fw_core_handle_request);
  754. void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  755. {
  756. struct fw_transaction *t;
  757. unsigned long flags;
  758. u32 *data;
  759. size_t data_length;
  760. int tcode, tlabel, source, rcode;
  761. tcode = HEADER_GET_TCODE(p->header[0]);
  762. tlabel = HEADER_GET_TLABEL(p->header[0]);
  763. source = HEADER_GET_SOURCE(p->header[1]);
  764. rcode = HEADER_GET_RCODE(p->header[1]);
  765. spin_lock_irqsave(&card->lock, flags);
  766. list_for_each_entry(t, &card->transaction_list, link) {
  767. if (t->node_id == source && t->tlabel == tlabel) {
  768. list_del_init(&t->link);
  769. card->tlabel_mask &= ~(1ULL << t->tlabel);
  770. break;
  771. }
  772. }
  773. spin_unlock_irqrestore(&card->lock, flags);
  774. if (&t->link == &card->transaction_list) {
  775. fw_notify("Unsolicited response (source %x, tlabel %x)\n",
  776. source, tlabel);
  777. return;
  778. }
  779. /*
  780. * FIXME: sanity check packet, is length correct, does tcodes
  781. * and addresses match.
  782. */
  783. switch (tcode) {
  784. case TCODE_READ_QUADLET_RESPONSE:
  785. data = (u32 *) &p->header[3];
  786. data_length = 4;
  787. break;
  788. case TCODE_WRITE_RESPONSE:
  789. data = NULL;
  790. data_length = 0;
  791. break;
  792. case TCODE_READ_BLOCK_RESPONSE:
  793. case TCODE_LOCK_RESPONSE:
  794. data = p->payload;
  795. data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
  796. break;
  797. default:
  798. /* Should never happen, this is just to shut up gcc. */
  799. data = NULL;
  800. data_length = 0;
  801. break;
  802. }
  803. del_timer_sync(&t->split_timeout_timer);
  804. /*
  805. * The response handler may be executed while the request handler
  806. * is still pending. Cancel the request handler.
  807. */
  808. card->driver->cancel_packet(card, &t->packet);
  809. t->callback(card, rcode, data, data_length, t->callback_data);
  810. }
  811. EXPORT_SYMBOL(fw_core_handle_response);
  812. static const struct fw_address_region topology_map_region =
  813. { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
  814. .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
  815. static void handle_topology_map(struct fw_card *card, struct fw_request *request,
  816. int tcode, int destination, int source, int generation,
  817. unsigned long long offset, void *payload, size_t length,
  818. void *callback_data)
  819. {
  820. int start;
  821. if (!TCODE_IS_READ_REQUEST(tcode)) {
  822. fw_send_response(card, request, RCODE_TYPE_ERROR);
  823. return;
  824. }
  825. if ((offset & 3) > 0 || (length & 3) > 0) {
  826. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  827. return;
  828. }
  829. start = (offset - topology_map_region.start) / 4;
  830. memcpy(payload, &card->topology_map[start], length);
  831. fw_send_response(card, request, RCODE_COMPLETE);
  832. }
  833. static struct fw_address_handler topology_map = {
  834. .length = 0x400,
  835. .address_callback = handle_topology_map,
  836. };
  837. static const struct fw_address_region registers_region =
  838. { .start = CSR_REGISTER_BASE,
  839. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
  840. static void update_split_timeout(struct fw_card *card)
  841. {
  842. unsigned int cycles;
  843. cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
  844. cycles = max(cycles, 800u); /* minimum as per the spec */
  845. cycles = min(cycles, 3u * 8000u); /* maximum OHCI timeout */
  846. card->split_timeout_cycles = cycles;
  847. card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
  848. }
  849. static void handle_registers(struct fw_card *card, struct fw_request *request,
  850. int tcode, int destination, int source, int generation,
  851. unsigned long long offset, void *payload, size_t length,
  852. void *callback_data)
  853. {
  854. int reg = offset & ~CSR_REGISTER_BASE;
  855. __be32 *data = payload;
  856. int rcode = RCODE_COMPLETE;
  857. unsigned long flags;
  858. switch (reg) {
  859. case CSR_PRIORITY_BUDGET:
  860. if (!card->priority_budget_implemented) {
  861. rcode = RCODE_ADDRESS_ERROR;
  862. break;
  863. }
  864. /* else fall through */
  865. case CSR_NODE_IDS:
  866. /*
  867. * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
  868. * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
  869. */
  870. /* fall through */
  871. case CSR_STATE_CLEAR:
  872. case CSR_STATE_SET:
  873. case CSR_CYCLE_TIME:
  874. case CSR_BUS_TIME:
  875. case CSR_BUSY_TIMEOUT:
  876. if (tcode == TCODE_READ_QUADLET_REQUEST)
  877. *data = cpu_to_be32(card->driver->read_csr(card, reg));
  878. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  879. card->driver->write_csr(card, reg, be32_to_cpu(*data));
  880. else
  881. rcode = RCODE_TYPE_ERROR;
  882. break;
  883. case CSR_RESET_START:
  884. if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  885. card->driver->write_csr(card, CSR_STATE_CLEAR,
  886. CSR_STATE_BIT_ABDICATE);
  887. else
  888. rcode = RCODE_TYPE_ERROR;
  889. break;
  890. case CSR_SPLIT_TIMEOUT_HI:
  891. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  892. *data = cpu_to_be32(card->split_timeout_hi);
  893. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  894. spin_lock_irqsave(&card->lock, flags);
  895. card->split_timeout_hi = be32_to_cpu(*data) & 7;
  896. update_split_timeout(card);
  897. spin_unlock_irqrestore(&card->lock, flags);
  898. } else {
  899. rcode = RCODE_TYPE_ERROR;
  900. }
  901. break;
  902. case CSR_SPLIT_TIMEOUT_LO:
  903. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  904. *data = cpu_to_be32(card->split_timeout_lo);
  905. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  906. spin_lock_irqsave(&card->lock, flags);
  907. card->split_timeout_lo =
  908. be32_to_cpu(*data) & 0xfff80000;
  909. update_split_timeout(card);
  910. spin_unlock_irqrestore(&card->lock, flags);
  911. } else {
  912. rcode = RCODE_TYPE_ERROR;
  913. }
  914. break;
  915. case CSR_MAINT_UTILITY:
  916. if (tcode == TCODE_READ_QUADLET_REQUEST)
  917. *data = card->maint_utility_register;
  918. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  919. card->maint_utility_register = *data;
  920. else
  921. rcode = RCODE_TYPE_ERROR;
  922. break;
  923. case CSR_BROADCAST_CHANNEL:
  924. if (tcode == TCODE_READ_QUADLET_REQUEST)
  925. *data = cpu_to_be32(card->broadcast_channel);
  926. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  927. card->broadcast_channel =
  928. (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
  929. BROADCAST_CHANNEL_INITIAL;
  930. else
  931. rcode = RCODE_TYPE_ERROR;
  932. break;
  933. case CSR_BUS_MANAGER_ID:
  934. case CSR_BANDWIDTH_AVAILABLE:
  935. case CSR_CHANNELS_AVAILABLE_HI:
  936. case CSR_CHANNELS_AVAILABLE_LO:
  937. /*
  938. * FIXME: these are handled by the OHCI hardware and
  939. * the stack never sees these request. If we add
  940. * support for a new type of controller that doesn't
  941. * handle this in hardware we need to deal with these
  942. * transactions.
  943. */
  944. BUG();
  945. break;
  946. default:
  947. rcode = RCODE_ADDRESS_ERROR;
  948. break;
  949. }
  950. fw_send_response(card, request, rcode);
  951. }
  952. static struct fw_address_handler registers = {
  953. .length = 0x400,
  954. .address_callback = handle_registers,
  955. };
  956. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  957. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  958. MODULE_LICENSE("GPL");
  959. static const u32 vendor_textual_descriptor[] = {
  960. /* textual descriptor leaf () */
  961. 0x00060000,
  962. 0x00000000,
  963. 0x00000000,
  964. 0x4c696e75, /* L i n u */
  965. 0x78204669, /* x F i */
  966. 0x72657769, /* r e w i */
  967. 0x72650000, /* r e */
  968. };
  969. static const u32 model_textual_descriptor[] = {
  970. /* model descriptor leaf () */
  971. 0x00030000,
  972. 0x00000000,
  973. 0x00000000,
  974. 0x4a756a75, /* J u j u */
  975. };
  976. static struct fw_descriptor vendor_id_descriptor = {
  977. .length = ARRAY_SIZE(vendor_textual_descriptor),
  978. .immediate = 0x03d00d1e,
  979. .key = 0x81000000,
  980. .data = vendor_textual_descriptor,
  981. };
  982. static struct fw_descriptor model_id_descriptor = {
  983. .length = ARRAY_SIZE(model_textual_descriptor),
  984. .immediate = 0x17000001,
  985. .key = 0x81000000,
  986. .data = model_textual_descriptor,
  987. };
  988. static int __init fw_core_init(void)
  989. {
  990. int ret;
  991. ret = bus_register(&fw_bus_type);
  992. if (ret < 0)
  993. return ret;
  994. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  995. if (fw_cdev_major < 0) {
  996. bus_unregister(&fw_bus_type);
  997. return fw_cdev_major;
  998. }
  999. fw_core_add_address_handler(&topology_map, &topology_map_region);
  1000. fw_core_add_address_handler(&registers, &registers_region);
  1001. fw_core_add_descriptor(&vendor_id_descriptor);
  1002. fw_core_add_descriptor(&model_id_descriptor);
  1003. return 0;
  1004. }
  1005. static void __exit fw_core_cleanup(void)
  1006. {
  1007. unregister_chrdev(fw_cdev_major, "firewire");
  1008. bus_unregister(&fw_bus_type);
  1009. idr_destroy(&fw_device_idr);
  1010. }
  1011. module_init(fw_core_init);
  1012. module_exit(fw_core_cleanup);