core-transaction.c 29 KB

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