core-transaction.c 29 KB

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