core-transaction.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. int ret = -EBUSY;
  490. if (region->start & 0xffff000000000003ULL ||
  491. region->start >= region->end ||
  492. region->end > 0x0001000000000000ULL ||
  493. handler->length & 3 ||
  494. handler->length == 0)
  495. return -EINVAL;
  496. spin_lock_bh(&address_handler_lock);
  497. handler->offset = region->start;
  498. while (handler->offset + handler->length <= region->end) {
  499. if (is_in_fcp_region(handler->offset, handler->length))
  500. other = NULL;
  501. else
  502. other = lookup_overlapping_address_handler
  503. (&address_handler_list,
  504. handler->offset, handler->length);
  505. if (other != NULL) {
  506. handler->offset += other->length;
  507. } else {
  508. list_add_tail(&handler->link, &address_handler_list);
  509. ret = 0;
  510. break;
  511. }
  512. }
  513. spin_unlock_bh(&address_handler_lock);
  514. return ret;
  515. }
  516. EXPORT_SYMBOL(fw_core_add_address_handler);
  517. /**
  518. * fw_core_remove_address_handler() - unregister an address handler
  519. *
  520. * When fw_core_remove_address_handler() returns, @handler->callback() is
  521. * guaranteed to not run on any CPU anymore.
  522. */
  523. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  524. {
  525. spin_lock_bh(&address_handler_lock);
  526. list_del(&handler->link);
  527. spin_unlock_bh(&address_handler_lock);
  528. }
  529. EXPORT_SYMBOL(fw_core_remove_address_handler);
  530. struct fw_request {
  531. struct fw_packet response;
  532. u32 request_header[4];
  533. int ack;
  534. u32 length;
  535. u32 data[0];
  536. };
  537. static void free_response_callback(struct fw_packet *packet,
  538. struct fw_card *card, int status)
  539. {
  540. struct fw_request *request;
  541. request = container_of(packet, struct fw_request, response);
  542. kfree(request);
  543. }
  544. int fw_get_response_length(struct fw_request *r)
  545. {
  546. int tcode, ext_tcode, data_length;
  547. tcode = HEADER_GET_TCODE(r->request_header[0]);
  548. switch (tcode) {
  549. case TCODE_WRITE_QUADLET_REQUEST:
  550. case TCODE_WRITE_BLOCK_REQUEST:
  551. return 0;
  552. case TCODE_READ_QUADLET_REQUEST:
  553. return 4;
  554. case TCODE_READ_BLOCK_REQUEST:
  555. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  556. return data_length;
  557. case TCODE_LOCK_REQUEST:
  558. ext_tcode = HEADER_GET_EXTENDED_TCODE(r->request_header[3]);
  559. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  560. switch (ext_tcode) {
  561. case EXTCODE_FETCH_ADD:
  562. case EXTCODE_LITTLE_ADD:
  563. return data_length;
  564. default:
  565. return data_length / 2;
  566. }
  567. default:
  568. WARN(1, "wrong tcode %d\n", tcode);
  569. return 0;
  570. }
  571. }
  572. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  573. int rcode, void *payload, size_t length)
  574. {
  575. int tcode, tlabel, extended_tcode, source, destination;
  576. tcode = HEADER_GET_TCODE(request_header[0]);
  577. tlabel = HEADER_GET_TLABEL(request_header[0]);
  578. source = HEADER_GET_DESTINATION(request_header[0]);
  579. destination = HEADER_GET_SOURCE(request_header[1]);
  580. extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
  581. response->header[0] =
  582. HEADER_RETRY(RETRY_1) |
  583. HEADER_TLABEL(tlabel) |
  584. HEADER_DESTINATION(destination);
  585. response->header[1] =
  586. HEADER_SOURCE(source) |
  587. HEADER_RCODE(rcode);
  588. response->header[2] = 0;
  589. switch (tcode) {
  590. case TCODE_WRITE_QUADLET_REQUEST:
  591. case TCODE_WRITE_BLOCK_REQUEST:
  592. response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
  593. response->header_length = 12;
  594. response->payload_length = 0;
  595. break;
  596. case TCODE_READ_QUADLET_REQUEST:
  597. response->header[0] |=
  598. HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
  599. if (payload != NULL)
  600. response->header[3] = *(u32 *)payload;
  601. else
  602. response->header[3] = 0;
  603. response->header_length = 16;
  604. response->payload_length = 0;
  605. break;
  606. case TCODE_READ_BLOCK_REQUEST:
  607. case TCODE_LOCK_REQUEST:
  608. response->header[0] |= HEADER_TCODE(tcode + 2);
  609. response->header[3] =
  610. HEADER_DATA_LENGTH(length) |
  611. HEADER_EXTENDED_TCODE(extended_tcode);
  612. response->header_length = 16;
  613. response->payload = payload;
  614. response->payload_length = length;
  615. break;
  616. default:
  617. WARN(1, "wrong tcode %d\n", tcode);
  618. }
  619. response->payload_mapped = false;
  620. }
  621. EXPORT_SYMBOL(fw_fill_response);
  622. static u32 compute_split_timeout_timestamp(struct fw_card *card,
  623. u32 request_timestamp)
  624. {
  625. unsigned int cycles;
  626. u32 timestamp;
  627. cycles = card->split_timeout_cycles;
  628. cycles += request_timestamp & 0x1fff;
  629. timestamp = request_timestamp & ~0x1fff;
  630. timestamp += (cycles / 8000) << 13;
  631. timestamp |= cycles % 8000;
  632. return timestamp;
  633. }
  634. static struct fw_request *allocate_request(struct fw_card *card,
  635. struct fw_packet *p)
  636. {
  637. struct fw_request *request;
  638. u32 *data, length;
  639. int request_tcode;
  640. request_tcode = HEADER_GET_TCODE(p->header[0]);
  641. switch (request_tcode) {
  642. case TCODE_WRITE_QUADLET_REQUEST:
  643. data = &p->header[3];
  644. length = 4;
  645. break;
  646. case TCODE_WRITE_BLOCK_REQUEST:
  647. case TCODE_LOCK_REQUEST:
  648. data = p->payload;
  649. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  650. break;
  651. case TCODE_READ_QUADLET_REQUEST:
  652. data = NULL;
  653. length = 4;
  654. break;
  655. case TCODE_READ_BLOCK_REQUEST:
  656. data = NULL;
  657. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  658. break;
  659. default:
  660. fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
  661. p->header[0], p->header[1], p->header[2]);
  662. return NULL;
  663. }
  664. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  665. if (request == NULL)
  666. return NULL;
  667. request->response.speed = p->speed;
  668. request->response.timestamp =
  669. compute_split_timeout_timestamp(card, p->timestamp);
  670. request->response.generation = p->generation;
  671. request->response.ack = 0;
  672. request->response.callback = free_response_callback;
  673. request->ack = p->ack;
  674. request->length = length;
  675. if (data)
  676. memcpy(request->data, data, length);
  677. memcpy(request->request_header, p->header, sizeof(p->header));
  678. return request;
  679. }
  680. void fw_send_response(struct fw_card *card,
  681. struct fw_request *request, int rcode)
  682. {
  683. if (WARN_ONCE(!request, "invalid for FCP address handlers"))
  684. return;
  685. /* unified transaction or broadcast transaction: don't respond */
  686. if (request->ack != ACK_PENDING ||
  687. HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
  688. kfree(request);
  689. return;
  690. }
  691. if (rcode == RCODE_COMPLETE)
  692. fw_fill_response(&request->response, request->request_header,
  693. rcode, request->data,
  694. fw_get_response_length(request));
  695. else
  696. fw_fill_response(&request->response, request->request_header,
  697. rcode, NULL, 0);
  698. card->driver->send_response(card, &request->response);
  699. }
  700. EXPORT_SYMBOL(fw_send_response);
  701. static void handle_exclusive_region_request(struct fw_card *card,
  702. struct fw_packet *p,
  703. struct fw_request *request,
  704. unsigned long long offset)
  705. {
  706. struct fw_address_handler *handler;
  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_bh(&address_handler_lock);
  714. handler = lookup_enclosing_address_handler(&address_handler_list,
  715. offset, request->length);
  716. if (handler)
  717. handler->address_callback(card, request,
  718. tcode, destination, source,
  719. p->generation, offset,
  720. request->data, request->length,
  721. handler->callback_data);
  722. spin_unlock_bh(&address_handler_lock);
  723. if (!handler)
  724. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  725. }
  726. static void handle_fcp_region_request(struct fw_card *card,
  727. struct fw_packet *p,
  728. struct fw_request *request,
  729. unsigned long long offset)
  730. {
  731. struct fw_address_handler *handler;
  732. int tcode, destination, source;
  733. if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  734. offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
  735. request->length > 0x200) {
  736. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  737. return;
  738. }
  739. tcode = HEADER_GET_TCODE(p->header[0]);
  740. destination = HEADER_GET_DESTINATION(p->header[0]);
  741. source = HEADER_GET_SOURCE(p->header[1]);
  742. if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
  743. tcode != TCODE_WRITE_BLOCK_REQUEST) {
  744. fw_send_response(card, request, RCODE_TYPE_ERROR);
  745. return;
  746. }
  747. spin_lock_bh(&address_handler_lock);
  748. list_for_each_entry(handler, &address_handler_list, link) {
  749. if (is_enclosing_handler(handler, offset, request->length))
  750. handler->address_callback(card, NULL, tcode,
  751. destination, source,
  752. p->generation, offset,
  753. request->data,
  754. request->length,
  755. handler->callback_data);
  756. }
  757. spin_unlock_bh(&address_handler_lock);
  758. fw_send_response(card, request, RCODE_COMPLETE);
  759. }
  760. void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  761. {
  762. struct fw_request *request;
  763. unsigned long long offset;
  764. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  765. return;
  766. if (TCODE_IS_LINK_INTERNAL(HEADER_GET_TCODE(p->header[0]))) {
  767. fw_cdev_handle_phy_packet(card, p);
  768. return;
  769. }
  770. request = allocate_request(card, p);
  771. if (request == NULL) {
  772. /* FIXME: send statically allocated busy packet. */
  773. return;
  774. }
  775. offset = ((u64)HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) |
  776. p->header[2];
  777. if (!is_in_fcp_region(offset, request->length))
  778. handle_exclusive_region_request(card, p, request, offset);
  779. else
  780. handle_fcp_region_request(card, p, request, offset);
  781. }
  782. EXPORT_SYMBOL(fw_core_handle_request);
  783. void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  784. {
  785. struct fw_transaction *t;
  786. unsigned long flags;
  787. u32 *data;
  788. size_t data_length;
  789. int tcode, tlabel, source, rcode;
  790. tcode = HEADER_GET_TCODE(p->header[0]);
  791. tlabel = HEADER_GET_TLABEL(p->header[0]);
  792. source = HEADER_GET_SOURCE(p->header[1]);
  793. rcode = HEADER_GET_RCODE(p->header[1]);
  794. spin_lock_irqsave(&card->lock, flags);
  795. list_for_each_entry(t, &card->transaction_list, link) {
  796. if (t->node_id == source && t->tlabel == tlabel) {
  797. if (!try_cancel_split_timeout(t)) {
  798. spin_unlock_irqrestore(&card->lock, flags);
  799. goto timed_out;
  800. }
  801. list_del_init(&t->link);
  802. card->tlabel_mask &= ~(1ULL << t->tlabel);
  803. break;
  804. }
  805. }
  806. spin_unlock_irqrestore(&card->lock, flags);
  807. if (&t->link == &card->transaction_list) {
  808. timed_out:
  809. fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
  810. source, tlabel);
  811. return;
  812. }
  813. /*
  814. * FIXME: sanity check packet, is length correct, does tcodes
  815. * and addresses match.
  816. */
  817. switch (tcode) {
  818. case TCODE_READ_QUADLET_RESPONSE:
  819. data = (u32 *) &p->header[3];
  820. data_length = 4;
  821. break;
  822. case TCODE_WRITE_RESPONSE:
  823. data = NULL;
  824. data_length = 0;
  825. break;
  826. case TCODE_READ_BLOCK_RESPONSE:
  827. case TCODE_LOCK_RESPONSE:
  828. data = p->payload;
  829. data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
  830. break;
  831. default:
  832. /* Should never happen, this is just to shut up gcc. */
  833. data = NULL;
  834. data_length = 0;
  835. break;
  836. }
  837. /*
  838. * The response handler may be executed while the request handler
  839. * is still pending. Cancel the request handler.
  840. */
  841. card->driver->cancel_packet(card, &t->packet);
  842. t->callback(card, rcode, data, data_length, t->callback_data);
  843. }
  844. EXPORT_SYMBOL(fw_core_handle_response);
  845. static const struct fw_address_region topology_map_region =
  846. { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
  847. .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
  848. static void handle_topology_map(struct fw_card *card, struct fw_request *request,
  849. int tcode, int destination, int source, int generation,
  850. unsigned long long offset, void *payload, size_t length,
  851. void *callback_data)
  852. {
  853. int start;
  854. if (!TCODE_IS_READ_REQUEST(tcode)) {
  855. fw_send_response(card, request, RCODE_TYPE_ERROR);
  856. return;
  857. }
  858. if ((offset & 3) > 0 || (length & 3) > 0) {
  859. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  860. return;
  861. }
  862. start = (offset - topology_map_region.start) / 4;
  863. memcpy(payload, &card->topology_map[start], length);
  864. fw_send_response(card, request, RCODE_COMPLETE);
  865. }
  866. static struct fw_address_handler topology_map = {
  867. .length = 0x400,
  868. .address_callback = handle_topology_map,
  869. };
  870. static const struct fw_address_region registers_region =
  871. { .start = CSR_REGISTER_BASE,
  872. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
  873. static void update_split_timeout(struct fw_card *card)
  874. {
  875. unsigned int cycles;
  876. cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
  877. /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
  878. cycles = clamp(cycles, 800u, 3u * 8000u);
  879. card->split_timeout_cycles = cycles;
  880. card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
  881. }
  882. static void handle_registers(struct fw_card *card, struct fw_request *request,
  883. int tcode, int destination, int source, int generation,
  884. unsigned long long offset, void *payload, size_t length,
  885. void *callback_data)
  886. {
  887. int reg = offset & ~CSR_REGISTER_BASE;
  888. __be32 *data = payload;
  889. int rcode = RCODE_COMPLETE;
  890. unsigned long flags;
  891. switch (reg) {
  892. case CSR_PRIORITY_BUDGET:
  893. if (!card->priority_budget_implemented) {
  894. rcode = RCODE_ADDRESS_ERROR;
  895. break;
  896. }
  897. /* else fall through */
  898. case CSR_NODE_IDS:
  899. /*
  900. * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
  901. * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
  902. */
  903. /* fall through */
  904. case CSR_STATE_CLEAR:
  905. case CSR_STATE_SET:
  906. case CSR_CYCLE_TIME:
  907. case CSR_BUS_TIME:
  908. case CSR_BUSY_TIMEOUT:
  909. if (tcode == TCODE_READ_QUADLET_REQUEST)
  910. *data = cpu_to_be32(card->driver->read_csr(card, reg));
  911. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  912. card->driver->write_csr(card, reg, be32_to_cpu(*data));
  913. else
  914. rcode = RCODE_TYPE_ERROR;
  915. break;
  916. case CSR_RESET_START:
  917. if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  918. card->driver->write_csr(card, CSR_STATE_CLEAR,
  919. CSR_STATE_BIT_ABDICATE);
  920. else
  921. rcode = RCODE_TYPE_ERROR;
  922. break;
  923. case CSR_SPLIT_TIMEOUT_HI:
  924. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  925. *data = cpu_to_be32(card->split_timeout_hi);
  926. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  927. spin_lock_irqsave(&card->lock, flags);
  928. card->split_timeout_hi = be32_to_cpu(*data) & 7;
  929. update_split_timeout(card);
  930. spin_unlock_irqrestore(&card->lock, flags);
  931. } else {
  932. rcode = RCODE_TYPE_ERROR;
  933. }
  934. break;
  935. case CSR_SPLIT_TIMEOUT_LO:
  936. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  937. *data = cpu_to_be32(card->split_timeout_lo);
  938. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  939. spin_lock_irqsave(&card->lock, flags);
  940. card->split_timeout_lo =
  941. be32_to_cpu(*data) & 0xfff80000;
  942. update_split_timeout(card);
  943. spin_unlock_irqrestore(&card->lock, flags);
  944. } else {
  945. rcode = RCODE_TYPE_ERROR;
  946. }
  947. break;
  948. case CSR_MAINT_UTILITY:
  949. if (tcode == TCODE_READ_QUADLET_REQUEST)
  950. *data = card->maint_utility_register;
  951. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  952. card->maint_utility_register = *data;
  953. else
  954. rcode = RCODE_TYPE_ERROR;
  955. break;
  956. case CSR_BROADCAST_CHANNEL:
  957. if (tcode == TCODE_READ_QUADLET_REQUEST)
  958. *data = cpu_to_be32(card->broadcast_channel);
  959. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  960. card->broadcast_channel =
  961. (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
  962. BROADCAST_CHANNEL_INITIAL;
  963. else
  964. rcode = RCODE_TYPE_ERROR;
  965. break;
  966. case CSR_BUS_MANAGER_ID:
  967. case CSR_BANDWIDTH_AVAILABLE:
  968. case CSR_CHANNELS_AVAILABLE_HI:
  969. case CSR_CHANNELS_AVAILABLE_LO:
  970. /*
  971. * FIXME: these are handled by the OHCI hardware and
  972. * the stack never sees these request. If we add
  973. * support for a new type of controller that doesn't
  974. * handle this in hardware we need to deal with these
  975. * transactions.
  976. */
  977. BUG();
  978. break;
  979. default:
  980. rcode = RCODE_ADDRESS_ERROR;
  981. break;
  982. }
  983. fw_send_response(card, request, rcode);
  984. }
  985. static struct fw_address_handler registers = {
  986. .length = 0x400,
  987. .address_callback = handle_registers,
  988. };
  989. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  990. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  991. MODULE_LICENSE("GPL");
  992. static const u32 vendor_textual_descriptor[] = {
  993. /* textual descriptor leaf () */
  994. 0x00060000,
  995. 0x00000000,
  996. 0x00000000,
  997. 0x4c696e75, /* L i n u */
  998. 0x78204669, /* x F i */
  999. 0x72657769, /* r e w i */
  1000. 0x72650000, /* r e */
  1001. };
  1002. static const u32 model_textual_descriptor[] = {
  1003. /* model descriptor leaf () */
  1004. 0x00030000,
  1005. 0x00000000,
  1006. 0x00000000,
  1007. 0x4a756a75, /* J u j u */
  1008. };
  1009. static struct fw_descriptor vendor_id_descriptor = {
  1010. .length = ARRAY_SIZE(vendor_textual_descriptor),
  1011. .immediate = 0x03d00d1e,
  1012. .key = 0x81000000,
  1013. .data = vendor_textual_descriptor,
  1014. };
  1015. static struct fw_descriptor model_id_descriptor = {
  1016. .length = ARRAY_SIZE(model_textual_descriptor),
  1017. .immediate = 0x17000001,
  1018. .key = 0x81000000,
  1019. .data = model_textual_descriptor,
  1020. };
  1021. static int __init fw_core_init(void)
  1022. {
  1023. int ret;
  1024. fw_workqueue = alloc_workqueue("firewire",
  1025. WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
  1026. if (!fw_workqueue)
  1027. return -ENOMEM;
  1028. ret = bus_register(&fw_bus_type);
  1029. if (ret < 0) {
  1030. destroy_workqueue(fw_workqueue);
  1031. return ret;
  1032. }
  1033. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  1034. if (fw_cdev_major < 0) {
  1035. bus_unregister(&fw_bus_type);
  1036. destroy_workqueue(fw_workqueue);
  1037. return fw_cdev_major;
  1038. }
  1039. fw_core_add_address_handler(&topology_map, &topology_map_region);
  1040. fw_core_add_address_handler(&registers, &registers_region);
  1041. fw_core_add_descriptor(&vendor_id_descriptor);
  1042. fw_core_add_descriptor(&model_id_descriptor);
  1043. return 0;
  1044. }
  1045. static void __exit fw_core_cleanup(void)
  1046. {
  1047. unregister_chrdev(fw_cdev_major, "firewire");
  1048. bus_unregister(&fw_bus_type);
  1049. destroy_workqueue(fw_workqueue);
  1050. idr_destroy(&fw_device_idr);
  1051. }
  1052. module_init(fw_core_init);
  1053. module_exit(fw_core_cleanup);