core-transaction.c 34 KB

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