core-transaction.c 33 KB

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