core-transaction.c 33 KB

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