core-transaction.c 34 KB

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