core-transaction.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Core IEEE1394 transaction logic
  4. *
  5. * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. */
  7. #include <linux/bug.h>
  8. #include <linux/completion.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/firewire.h>
  12. #include <linux/firewire-constants.h>
  13. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/rculist.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. #include <linux/timer.h>
  24. #include <linux/types.h>
  25. #include <linux/workqueue.h>
  26. #include <asm/byteorder.h>
  27. #include "core.h"
  28. #include "packet-header-definitions.h"
  29. #include "phy-packet-definitions.h"
  30. #include <trace/events/firewire.h>
  31. #define HEADER_DESTINATION_IS_BROADCAST(header) \
  32. ((async_header_get_destination(header) & 0x3f) == 0x3f)
  33. /* returns 0 if the split timeout handler is already running */
  34. static int try_cancel_split_timeout(struct fw_transaction *t)
  35. {
  36. if (t->is_split_transaction)
  37. return timer_delete(&t->split_timeout_timer);
  38. else
  39. return 1;
  40. }
  41. // card->transactions.lock must be acquired in advance.
  42. static void remove_transaction_entry(struct fw_card *card, struct fw_transaction *entry)
  43. {
  44. list_del_init(&entry->link);
  45. card->transactions.tlabel_mask &= ~(1ULL << entry->tlabel);
  46. }
  47. // Must be called without holding card->transactions.lock.
  48. void fw_cancel_pending_transactions(struct fw_card *card)
  49. {
  50. struct fw_transaction *t, *tmp;
  51. LIST_HEAD(pending_list);
  52. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  53. // local destination never runs in any type of IRQ context.
  54. scoped_guard(spinlock_irqsave, &card->transactions.lock) {
  55. list_for_each_entry_safe(t, tmp, &card->transactions.list, link) {
  56. if (try_cancel_split_timeout(t))
  57. list_move(&t->link, &pending_list);
  58. }
  59. }
  60. list_for_each_entry_safe(t, tmp, &pending_list, link) {
  61. list_del(&t->link);
  62. if (!t->with_tstamp) {
  63. t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0,
  64. t->callback_data);
  65. } else {
  66. t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp, 0,
  67. NULL, 0, t->callback_data);
  68. }
  69. }
  70. }
  71. // card->transactions.lock must be acquired in advance.
  72. #define find_and_pop_transaction_entry(card, condition) \
  73. ({ \
  74. struct fw_transaction *iter, *t = NULL; \
  75. list_for_each_entry(iter, &card->transactions.list, link) { \
  76. if (condition) { \
  77. t = iter; \
  78. break; \
  79. } \
  80. } \
  81. if (t && try_cancel_split_timeout(t)) \
  82. remove_transaction_entry(card, t); \
  83. t; \
  84. })
  85. static int close_transaction(struct fw_transaction *transaction, struct fw_card *card, int rcode,
  86. u32 response_tstamp)
  87. {
  88. struct fw_transaction *t;
  89. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  90. // local destination never runs in any type of IRQ context.
  91. scoped_guard(spinlock_irqsave, &card->transactions.lock) {
  92. t = find_and_pop_transaction_entry(card, iter == transaction);
  93. if (!t)
  94. return -ENOENT;
  95. }
  96. if (!t->with_tstamp) {
  97. t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data);
  98. } else {
  99. t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp, NULL, 0,
  100. t->callback_data);
  101. }
  102. return 0;
  103. }
  104. /*
  105. * Only valid for transactions that are potentially pending (ie have
  106. * been sent).
  107. */
  108. int fw_cancel_transaction(struct fw_card *card,
  109. struct fw_transaction *transaction)
  110. {
  111. u32 tstamp;
  112. /*
  113. * Cancel the packet transmission if it's still queued. That
  114. * will call the packet transmission callback which cancels
  115. * the transaction.
  116. */
  117. if (card->driver->cancel_packet(card, &transaction->packet) == 0)
  118. return 0;
  119. /*
  120. * If the request packet has already been sent, we need to see
  121. * if the transaction is still pending and remove it in that case.
  122. */
  123. if (transaction->packet.ack == 0) {
  124. // The timestamp is reused since it was just read now.
  125. tstamp = transaction->packet.timestamp;
  126. } else {
  127. u32 curr_cycle_time = 0;
  128. (void)fw_card_read_cycle_time(card, &curr_cycle_time);
  129. tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time);
  130. }
  131. return close_transaction(transaction, card, RCODE_CANCELLED, tstamp);
  132. }
  133. EXPORT_SYMBOL(fw_cancel_transaction);
  134. static void split_transaction_timeout_callback(struct timer_list *timer)
  135. {
  136. struct fw_transaction *t = timer_container_of(t, timer, split_timeout_timer);
  137. struct fw_card *card = t->card;
  138. scoped_guard(spinlock_irqsave, &card->transactions.lock) {
  139. if (list_empty(&t->link))
  140. return;
  141. remove_transaction_entry(card, t);
  142. }
  143. if (!t->with_tstamp) {
  144. t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
  145. } else {
  146. t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp,
  147. t->split_timeout_cycle, NULL, 0, t->callback_data);
  148. }
  149. }
  150. // card->transactions.lock should be acquired in advance for the linked list.
  151. static void start_split_transaction_timeout(struct fw_transaction *t, unsigned int delta)
  152. {
  153. if (list_empty(&t->link) || WARN_ON(t->is_split_transaction))
  154. return;
  155. t->is_split_transaction = true;
  156. mod_timer(&t->split_timeout_timer, jiffies + delta);
  157. }
  158. static u32 compute_split_timeout_timestamp(struct fw_card *card, u32 request_timestamp);
  159. static void transmit_complete_callback(struct fw_packet *packet,
  160. struct fw_card *card, int status)
  161. {
  162. struct fw_transaction *t =
  163. container_of(packet, struct fw_transaction, packet);
  164. trace_async_request_outbound_complete((uintptr_t)t, card->index, packet->generation,
  165. packet->speed, status, packet->timestamp);
  166. switch (status) {
  167. case ACK_COMPLETE:
  168. close_transaction(t, card, RCODE_COMPLETE, packet->timestamp);
  169. break;
  170. case ACK_PENDING:
  171. {
  172. unsigned int delta;
  173. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  174. // local destination never runs in any type of IRQ context.
  175. scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
  176. t->split_timeout_cycle =
  177. compute_split_timeout_timestamp(card, packet->timestamp) & 0xffff;
  178. delta = card->split_timeout.jiffies;
  179. }
  180. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  181. // local destination never runs in any type of IRQ context.
  182. scoped_guard(spinlock_irqsave, &card->transactions.lock)
  183. start_split_transaction_timeout(t, delta);
  184. break;
  185. }
  186. case ACK_BUSY_X:
  187. case ACK_BUSY_A:
  188. case ACK_BUSY_B:
  189. close_transaction(t, card, RCODE_BUSY, packet->timestamp);
  190. break;
  191. case ACK_DATA_ERROR:
  192. close_transaction(t, card, RCODE_DATA_ERROR, packet->timestamp);
  193. break;
  194. case ACK_TYPE_ERROR:
  195. close_transaction(t, card, RCODE_TYPE_ERROR, packet->timestamp);
  196. break;
  197. default:
  198. /*
  199. * In this case the ack is really a juju specific
  200. * rcode, so just forward that to the callback.
  201. */
  202. close_transaction(t, card, status, packet->timestamp);
  203. break;
  204. }
  205. }
  206. static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  207. int destination_id, int source_id, int generation, int speed,
  208. unsigned long long offset, void *payload, size_t length)
  209. {
  210. int ext_tcode;
  211. if (tcode == TCODE_STREAM_DATA) {
  212. // The value of destination_id argument should include tag, channel, and sy fields
  213. // as isochronous packet header has.
  214. packet->header[0] = destination_id;
  215. isoc_header_set_data_length(packet->header, length);
  216. isoc_header_set_tcode(packet->header, TCODE_STREAM_DATA);
  217. packet->header_length = 4;
  218. packet->payload = payload;
  219. packet->payload_length = length;
  220. goto common;
  221. }
  222. if (tcode > 0x10) {
  223. ext_tcode = tcode & ~0x10;
  224. tcode = TCODE_LOCK_REQUEST;
  225. } else
  226. ext_tcode = 0;
  227. async_header_set_retry(packet->header, RETRY_X);
  228. async_header_set_tlabel(packet->header, tlabel);
  229. async_header_set_tcode(packet->header, tcode);
  230. async_header_set_destination(packet->header, destination_id);
  231. async_header_set_source(packet->header, source_id);
  232. async_header_set_offset(packet->header, offset);
  233. switch (tcode) {
  234. case TCODE_WRITE_QUADLET_REQUEST:
  235. async_header_set_quadlet_data(packet->header, *(u32 *)payload);
  236. packet->header_length = 16;
  237. packet->payload_length = 0;
  238. break;
  239. case TCODE_LOCK_REQUEST:
  240. case TCODE_WRITE_BLOCK_REQUEST:
  241. async_header_set_data_length(packet->header, length);
  242. async_header_set_extended_tcode(packet->header, ext_tcode);
  243. packet->header_length = 16;
  244. packet->payload = payload;
  245. packet->payload_length = length;
  246. break;
  247. case TCODE_READ_QUADLET_REQUEST:
  248. packet->header_length = 12;
  249. packet->payload_length = 0;
  250. break;
  251. case TCODE_READ_BLOCK_REQUEST:
  252. async_header_set_data_length(packet->header, length);
  253. async_header_set_extended_tcode(packet->header, ext_tcode);
  254. packet->header_length = 16;
  255. packet->payload_length = 0;
  256. break;
  257. default:
  258. WARN(1, "wrong tcode %d\n", tcode);
  259. }
  260. common:
  261. packet->speed = speed;
  262. packet->generation = generation;
  263. packet->ack = 0;
  264. packet->payload_mapped = false;
  265. }
  266. static int allocate_tlabel(struct fw_card *card)
  267. __must_hold(&card->transactions.lock)
  268. {
  269. int tlabel;
  270. lockdep_assert_held(&card->transactions.lock);
  271. tlabel = card->transactions.current_tlabel;
  272. while (card->transactions.tlabel_mask & (1ULL << tlabel)) {
  273. tlabel = (tlabel + 1) & 0x3f;
  274. if (tlabel == card->transactions.current_tlabel)
  275. return -EBUSY;
  276. }
  277. card->transactions.current_tlabel = (tlabel + 1) & 0x3f;
  278. card->transactions.tlabel_mask |= 1ULL << tlabel;
  279. return tlabel;
  280. }
  281. /**
  282. * __fw_send_request() - submit a request packet for transmission to generate callback for response
  283. * subaction with or without time stamp.
  284. * @card: interface to send the request at
  285. * @t: transaction instance to which the request belongs
  286. * @tcode: transaction code
  287. * @destination_id: destination node ID, consisting of bus_ID and phy_ID
  288. * @generation: bus generation in which request and response are valid
  289. * @speed: transmission speed
  290. * @offset: 48bit wide offset into destination's address space
  291. * @payload: data payload for the request subaction
  292. * @length: length of the payload, in bytes
  293. * @callback: union of two functions whether to receive time stamp or not for response
  294. * subaction.
  295. * @with_tstamp: Whether to receive time stamp or not for response subaction.
  296. * @callback_data: data to be passed to the transaction completion callback
  297. *
  298. * Submit a request packet into the asynchronous request transmission queue.
  299. * Can be called from atomic context. If you prefer a blocking API, use
  300. * fw_run_transaction() in a context that can sleep.
  301. *
  302. * In case of lock requests, specify one of the firewire-core specific %TCODE_
  303. * constants instead of %TCODE_LOCK_REQUEST in @tcode.
  304. *
  305. * Make sure that the value in @destination_id is not older than the one in
  306. * @generation. Otherwise the request is in danger to be sent to a wrong node.
  307. *
  308. * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
  309. * needs to synthesize @destination_id with fw_stream_packet_destination_id().
  310. * It will contain tag, channel, and sy data instead of a node ID then.
  311. *
  312. * The payload buffer at @data is going to be DMA-mapped except in case of
  313. * @length <= 8 or of local (loopback) requests. Hence make sure that the
  314. * buffer complies with the restrictions of the streaming DMA mapping API.
  315. * @payload must not be freed before the @callback is called.
  316. *
  317. * In case of request types without payload, @data is NULL and @length is 0.
  318. *
  319. * After the transaction is completed successfully or unsuccessfully, the
  320. * @callback will be called. Among its parameters is the response code which
  321. * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
  322. * the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core
  323. * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION,
  324. * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request
  325. * generation, or missing ACK respectively.
  326. *
  327. * Note some timing corner cases: fw_send_request() may complete much earlier
  328. * than when the request packet actually hits the wire. On the other hand,
  329. * transaction completion and hence execution of @callback may happen even
  330. * before fw_send_request() returns.
  331. */
  332. void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
  333. int destination_id, int generation, int speed, unsigned long long offset,
  334. void *payload, size_t length, union fw_transaction_callback callback,
  335. bool with_tstamp, void *callback_data)
  336. {
  337. int tlabel;
  338. /*
  339. * Allocate tlabel from the bitmap and put the transaction on
  340. * the list while holding the card spinlock.
  341. */
  342. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  343. // local destination never runs in any type of IRQ context.
  344. scoped_guard(spinlock_irqsave, &card->transactions.lock)
  345. tlabel = allocate_tlabel(card);
  346. if (tlabel < 0) {
  347. if (!with_tstamp) {
  348. callback.without_tstamp(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  349. } else {
  350. // Timestamping on behalf of hardware.
  351. u32 curr_cycle_time = 0;
  352. u32 tstamp;
  353. (void)fw_card_read_cycle_time(card, &curr_cycle_time);
  354. tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time);
  355. callback.with_tstamp(card, RCODE_SEND_ERROR, tstamp, tstamp, NULL, 0,
  356. callback_data);
  357. }
  358. return;
  359. }
  360. t->node_id = destination_id;
  361. t->tlabel = tlabel;
  362. t->card = card;
  363. t->is_split_transaction = false;
  364. timer_setup(&t->split_timeout_timer, split_transaction_timeout_callback, 0);
  365. t->callback = callback;
  366. t->with_tstamp = with_tstamp;
  367. t->callback_data = callback_data;
  368. t->packet.callback = transmit_complete_callback;
  369. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  370. // local destination never runs in any type of IRQ context.
  371. scoped_guard(spinlock_irqsave, &card->lock) {
  372. // The node_id field of fw_card can be updated when handling SelfIDComplete.
  373. fw_fill_request(&t->packet, tcode, t->tlabel, destination_id, card->node_id,
  374. generation, speed, offset, payload, length);
  375. }
  376. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  377. // local destination never runs in any type of IRQ context.
  378. scoped_guard(spinlock_irqsave, &card->transactions.lock)
  379. list_add_tail(&t->link, &card->transactions.list);
  380. // Safe with no lock, since the index field of fw_card is immutable once assigned.
  381. trace_async_request_outbound_initiate((uintptr_t)t, card->index, generation, speed,
  382. t->packet.header, payload,
  383. tcode_is_read_request(tcode) ? 0 : length / 4);
  384. card->driver->send_request(card, &t->packet);
  385. }
  386. EXPORT_SYMBOL_GPL(__fw_send_request);
  387. struct transaction_callback_data {
  388. struct completion done;
  389. void *payload;
  390. int rcode;
  391. };
  392. static void transaction_callback(struct fw_card *card, int rcode,
  393. void *payload, size_t length, void *data)
  394. {
  395. struct transaction_callback_data *d = data;
  396. if (rcode == RCODE_COMPLETE)
  397. memcpy(d->payload, payload, length);
  398. d->rcode = rcode;
  399. complete(&d->done);
  400. }
  401. /**
  402. * fw_run_transaction() - send request and sleep until transaction is completed
  403. * @card: card interface for this request
  404. * @tcode: transaction code
  405. * @destination_id: destination node ID, consisting of bus_ID and phy_ID
  406. * @generation: bus generation in which request and response are valid
  407. * @speed: transmission speed
  408. * @offset: 48bit wide offset into destination's address space
  409. * @payload: data payload for the request subaction
  410. * @length: length of the payload, in bytes
  411. *
  412. * Returns the RCODE. See fw_send_request() for parameter documentation.
  413. * Unlike fw_send_request(), @data points to the payload of the request or/and
  414. * to the payload of the response. DMA mapping restrictions apply to outbound
  415. * request payloads of >= 8 bytes but not to inbound response payloads.
  416. */
  417. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  418. int generation, int speed, unsigned long long offset,
  419. void *payload, size_t length)
  420. {
  421. struct transaction_callback_data d;
  422. struct fw_transaction t;
  423. timer_setup_on_stack(&t.split_timeout_timer, NULL, 0);
  424. init_completion(&d.done);
  425. d.payload = payload;
  426. fw_send_request(card, &t, tcode, destination_id, generation, speed,
  427. offset, payload, length, transaction_callback, &d);
  428. wait_for_completion(&d.done);
  429. timer_destroy_on_stack(&t.split_timeout_timer);
  430. return d.rcode;
  431. }
  432. EXPORT_SYMBOL(fw_run_transaction);
  433. static DEFINE_MUTEX(phy_config_mutex);
  434. static DECLARE_COMPLETION(phy_config_done);
  435. static void transmit_phy_packet_callback(struct fw_packet *packet,
  436. struct fw_card *card, int status)
  437. {
  438. trace_async_phy_outbound_complete((uintptr_t)packet, card->index, packet->generation, status,
  439. packet->timestamp);
  440. complete(&phy_config_done);
  441. }
  442. static struct fw_packet phy_config_packet = {
  443. .header_length = 12,
  444. .payload_length = 0,
  445. .speed = SCODE_100,
  446. .callback = transmit_phy_packet_callback,
  447. };
  448. void fw_send_phy_config(struct fw_card *card,
  449. int node_id, int generation, int gap_count)
  450. {
  451. long timeout = msecs_to_jiffies(100);
  452. u32 data = 0;
  453. phy_packet_set_packet_identifier(&data, PHY_PACKET_PACKET_IDENTIFIER_PHY_CONFIG);
  454. if (node_id != FW_PHY_CONFIG_NO_NODE_ID) {
  455. phy_packet_phy_config_set_root_id(&data, node_id);
  456. phy_packet_phy_config_set_force_root_node(&data, true);
  457. }
  458. if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
  459. gap_count = card->driver->read_phy_reg(card, 1);
  460. if (gap_count < 0)
  461. return;
  462. gap_count &= 63;
  463. if (gap_count == 63)
  464. return;
  465. }
  466. phy_packet_phy_config_set_gap_count(&data, gap_count);
  467. phy_packet_phy_config_set_gap_count_optimization(&data, true);
  468. guard(mutex)(&phy_config_mutex);
  469. async_header_set_tcode(phy_config_packet.header, TCODE_LINK_INTERNAL);
  470. phy_config_packet.header[1] = data;
  471. phy_config_packet.header[2] = ~data;
  472. phy_config_packet.generation = generation;
  473. reinit_completion(&phy_config_done);
  474. trace_async_phy_outbound_initiate((uintptr_t)&phy_config_packet, card->index,
  475. phy_config_packet.generation, phy_config_packet.header[1],
  476. phy_config_packet.header[2]);
  477. card->driver->send_request(card, &phy_config_packet);
  478. wait_for_completion_timeout(&phy_config_done, timeout);
  479. }
  480. static struct fw_address_handler *lookup_overlapping_address_handler(
  481. struct list_head *list, unsigned long long offset, size_t length)
  482. {
  483. struct fw_address_handler *handler;
  484. list_for_each_entry_rcu(handler, list, link) {
  485. if (handler->offset < offset + length &&
  486. offset < handler->offset + handler->length)
  487. return handler;
  488. }
  489. return NULL;
  490. }
  491. static bool is_enclosing_handler(struct fw_address_handler *handler,
  492. unsigned long long offset, size_t length)
  493. {
  494. return handler->offset <= offset &&
  495. offset + length <= handler->offset + handler->length;
  496. }
  497. static struct fw_address_handler *lookup_enclosing_address_handler(
  498. struct list_head *list, unsigned long long offset, size_t length)
  499. {
  500. struct fw_address_handler *handler;
  501. list_for_each_entry_rcu(handler, list, link) {
  502. if (is_enclosing_handler(handler, offset, length))
  503. return handler;
  504. }
  505. return NULL;
  506. }
  507. static DEFINE_SPINLOCK(address_handler_list_lock);
  508. static LIST_HEAD(address_handler_list);
  509. const struct fw_address_region fw_high_memory_region =
  510. { .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, };
  511. EXPORT_SYMBOL(fw_high_memory_region);
  512. static const struct fw_address_region low_memory_region =
  513. { .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, };
  514. #if 0
  515. const struct fw_address_region fw_private_region =
  516. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  517. const struct fw_address_region fw_csr_region =
  518. { .start = CSR_REGISTER_BASE,
  519. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
  520. const struct fw_address_region fw_unit_space_region =
  521. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  522. #endif /* 0 */
  523. static void complete_address_handler(struct kref *kref)
  524. {
  525. struct fw_address_handler *handler = container_of(kref, struct fw_address_handler, kref);
  526. complete(&handler->done);
  527. }
  528. static void get_address_handler(struct fw_address_handler *handler)
  529. {
  530. kref_get(&handler->kref);
  531. }
  532. static int put_address_handler(struct fw_address_handler *handler)
  533. {
  534. return kref_put(&handler->kref, complete_address_handler);
  535. }
  536. /**
  537. * fw_core_add_address_handler() - register for incoming requests
  538. * @handler: callback
  539. * @region: region in the IEEE 1212 node space address range
  540. *
  541. * region->start, ->end, and handler->length have to be quadlet-aligned.
  542. *
  543. * When a request is received that falls within the specified address range, the specified callback
  544. * is invoked. The parameters passed to the callback give the details of the particular request.
  545. * The callback is invoked in the workqueue context in most cases. However, if the request is
  546. * initiated by the local node, the callback is invoked in the initiator's context.
  547. *
  548. * To be called in process context.
  549. * Return value: 0 on success, non-zero otherwise.
  550. *
  551. * The start offset of the handler's address region is determined by
  552. * fw_core_add_address_handler() and is returned in handler->offset.
  553. *
  554. * Address allocations are exclusive, except for the FCP registers.
  555. */
  556. int fw_core_add_address_handler(struct fw_address_handler *handler,
  557. const struct fw_address_region *region)
  558. {
  559. struct fw_address_handler *other;
  560. int ret = -EBUSY;
  561. if (region->start & 0xffff000000000003ULL ||
  562. region->start >= region->end ||
  563. region->end > 0x0001000000000000ULL ||
  564. handler->length & 3 ||
  565. handler->length == 0)
  566. return -EINVAL;
  567. guard(spinlock)(&address_handler_list_lock);
  568. handler->offset = region->start;
  569. while (handler->offset + handler->length <= region->end) {
  570. if (is_in_fcp_region(handler->offset, handler->length))
  571. other = NULL;
  572. else
  573. other = lookup_overlapping_address_handler
  574. (&address_handler_list,
  575. handler->offset, handler->length);
  576. if (other != NULL) {
  577. handler->offset += other->length;
  578. } else {
  579. init_completion(&handler->done);
  580. kref_init(&handler->kref);
  581. list_add_tail_rcu(&handler->link, &address_handler_list);
  582. ret = 0;
  583. break;
  584. }
  585. }
  586. return ret;
  587. }
  588. EXPORT_SYMBOL(fw_core_add_address_handler);
  589. /**
  590. * fw_core_remove_address_handler() - unregister an address handler
  591. * @handler: callback
  592. *
  593. * To be called in process context.
  594. *
  595. * When fw_core_remove_address_handler() returns, @handler->callback() is
  596. * guaranteed to not run on any CPU anymore.
  597. */
  598. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  599. {
  600. scoped_guard(spinlock, &address_handler_list_lock)
  601. list_del_rcu(&handler->link);
  602. synchronize_rcu();
  603. if (!put_address_handler(handler))
  604. wait_for_completion(&handler->done);
  605. }
  606. EXPORT_SYMBOL(fw_core_remove_address_handler);
  607. struct fw_request {
  608. struct kref kref;
  609. struct fw_packet response;
  610. u32 request_header[ASYNC_HEADER_QUADLET_COUNT];
  611. int ack;
  612. u32 timestamp;
  613. u32 length;
  614. u32 data[];
  615. };
  616. void fw_request_get(struct fw_request *request)
  617. {
  618. kref_get(&request->kref);
  619. }
  620. static void release_request(struct kref *kref)
  621. {
  622. struct fw_request *request = container_of(kref, struct fw_request, kref);
  623. kfree(request);
  624. }
  625. void fw_request_put(struct fw_request *request)
  626. {
  627. kref_put(&request->kref, release_request);
  628. }
  629. static void free_response_callback(struct fw_packet *packet,
  630. struct fw_card *card, int status)
  631. {
  632. struct fw_request *request = container_of(packet, struct fw_request, response);
  633. trace_async_response_outbound_complete((uintptr_t)request, card->index, packet->generation,
  634. packet->speed, status, packet->timestamp);
  635. // Decrease the reference count since not at in-flight.
  636. fw_request_put(request);
  637. // Decrease the reference count to release the object.
  638. fw_request_put(request);
  639. }
  640. int fw_get_response_length(struct fw_request *r)
  641. {
  642. int tcode, ext_tcode, data_length;
  643. tcode = async_header_get_tcode(r->request_header);
  644. switch (tcode) {
  645. case TCODE_WRITE_QUADLET_REQUEST:
  646. case TCODE_WRITE_BLOCK_REQUEST:
  647. return 0;
  648. case TCODE_READ_QUADLET_REQUEST:
  649. return 4;
  650. case TCODE_READ_BLOCK_REQUEST:
  651. data_length = async_header_get_data_length(r->request_header);
  652. return data_length;
  653. case TCODE_LOCK_REQUEST:
  654. ext_tcode = async_header_get_extended_tcode(r->request_header);
  655. data_length = async_header_get_data_length(r->request_header);
  656. switch (ext_tcode) {
  657. case EXTCODE_FETCH_ADD:
  658. case EXTCODE_LITTLE_ADD:
  659. return data_length;
  660. default:
  661. return data_length / 2;
  662. }
  663. default:
  664. WARN(1, "wrong tcode %d\n", tcode);
  665. return 0;
  666. }
  667. }
  668. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  669. int rcode, void *payload, size_t length)
  670. {
  671. int tcode, tlabel, extended_tcode, source, destination;
  672. tcode = async_header_get_tcode(request_header);
  673. tlabel = async_header_get_tlabel(request_header);
  674. source = async_header_get_destination(request_header); // Exchange.
  675. destination = async_header_get_source(request_header); // Exchange.
  676. extended_tcode = async_header_get_extended_tcode(request_header);
  677. async_header_set_retry(response->header, RETRY_1);
  678. async_header_set_tlabel(response->header, tlabel);
  679. async_header_set_destination(response->header, destination);
  680. async_header_set_source(response->header, source);
  681. async_header_set_rcode(response->header, rcode);
  682. response->header[2] = 0; // The field is reserved.
  683. switch (tcode) {
  684. case TCODE_WRITE_QUADLET_REQUEST:
  685. case TCODE_WRITE_BLOCK_REQUEST:
  686. async_header_set_tcode(response->header, TCODE_WRITE_RESPONSE);
  687. response->header_length = 12;
  688. response->payload_length = 0;
  689. break;
  690. case TCODE_READ_QUADLET_REQUEST:
  691. async_header_set_tcode(response->header, TCODE_READ_QUADLET_RESPONSE);
  692. if (payload != NULL)
  693. async_header_set_quadlet_data(response->header, *(u32 *)payload);
  694. else
  695. async_header_set_quadlet_data(response->header, 0);
  696. response->header_length = 16;
  697. response->payload_length = 0;
  698. break;
  699. case TCODE_READ_BLOCK_REQUEST:
  700. case TCODE_LOCK_REQUEST:
  701. async_header_set_tcode(response->header, tcode + 2);
  702. async_header_set_data_length(response->header, length);
  703. async_header_set_extended_tcode(response->header, extended_tcode);
  704. response->header_length = 16;
  705. response->payload = payload;
  706. response->payload_length = length;
  707. break;
  708. default:
  709. WARN(1, "wrong tcode %d\n", tcode);
  710. }
  711. response->payload_mapped = false;
  712. }
  713. EXPORT_SYMBOL(fw_fill_response);
  714. static u32 compute_split_timeout_timestamp(struct fw_card *card,
  715. u32 request_timestamp)
  716. __must_hold(&card->split_timeout.lock)
  717. {
  718. unsigned int cycles;
  719. u32 timestamp;
  720. lockdep_assert_held(&card->split_timeout.lock);
  721. cycles = card->split_timeout.cycles;
  722. cycles += request_timestamp & 0x1fff;
  723. timestamp = request_timestamp & ~0x1fff;
  724. timestamp += (cycles / 8000) << 13;
  725. timestamp |= cycles % 8000;
  726. return timestamp;
  727. }
  728. static struct fw_request *allocate_request(struct fw_card *card,
  729. struct fw_packet *p)
  730. {
  731. struct fw_request *request;
  732. u32 *data, length;
  733. int request_tcode;
  734. request_tcode = async_header_get_tcode(p->header);
  735. switch (request_tcode) {
  736. case TCODE_WRITE_QUADLET_REQUEST:
  737. data = &p->header[3];
  738. length = 4;
  739. break;
  740. case TCODE_WRITE_BLOCK_REQUEST:
  741. case TCODE_LOCK_REQUEST:
  742. data = p->payload;
  743. length = async_header_get_data_length(p->header);
  744. break;
  745. case TCODE_READ_QUADLET_REQUEST:
  746. data = NULL;
  747. length = 4;
  748. break;
  749. case TCODE_READ_BLOCK_REQUEST:
  750. data = NULL;
  751. length = async_header_get_data_length(p->header);
  752. break;
  753. default:
  754. fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
  755. p->header[0], p->header[1], p->header[2]);
  756. return NULL;
  757. }
  758. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  759. if (request == NULL)
  760. return NULL;
  761. kref_init(&request->kref);
  762. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  763. // local destination never runs in any type of IRQ context.
  764. scoped_guard(spinlock_irqsave, &card->split_timeout.lock)
  765. request->response.timestamp = compute_split_timeout_timestamp(card, p->timestamp);
  766. request->response.speed = p->speed;
  767. request->response.generation = p->generation;
  768. request->response.ack = 0;
  769. request->response.callback = free_response_callback;
  770. request->ack = p->ack;
  771. request->timestamp = p->timestamp;
  772. request->length = length;
  773. if (data)
  774. memcpy(request->data, data, length);
  775. memcpy(request->request_header, p->header, sizeof(p->header));
  776. return request;
  777. }
  778. /**
  779. * fw_send_response: - send response packet for asynchronous transaction.
  780. * @card: interface to send the response at.
  781. * @request: firewire request data for the transaction.
  782. * @rcode: response code to send.
  783. *
  784. * Submit a response packet into the asynchronous response transmission queue. The @request
  785. * is going to be released when the transmission successfully finishes later.
  786. */
  787. void fw_send_response(struct fw_card *card,
  788. struct fw_request *request, int rcode)
  789. {
  790. u32 *data = NULL;
  791. unsigned int data_length = 0;
  792. /* unified transaction or broadcast transaction: don't respond */
  793. if (request->ack != ACK_PENDING ||
  794. HEADER_DESTINATION_IS_BROADCAST(request->request_header)) {
  795. fw_request_put(request);
  796. return;
  797. }
  798. if (rcode == RCODE_COMPLETE) {
  799. data = request->data;
  800. data_length = fw_get_response_length(request);
  801. }
  802. fw_fill_response(&request->response, request->request_header, rcode, data, data_length);
  803. // Increase the reference count so that the object is kept during in-flight.
  804. fw_request_get(request);
  805. trace_async_response_outbound_initiate((uintptr_t)request, card->index,
  806. request->response.generation, request->response.speed,
  807. request->response.header, data,
  808. data ? data_length / 4 : 0);
  809. card->driver->send_response(card, &request->response);
  810. }
  811. EXPORT_SYMBOL(fw_send_response);
  812. /**
  813. * fw_get_request_speed() - returns speed at which the @request was received
  814. * @request: firewire request data
  815. */
  816. int fw_get_request_speed(struct fw_request *request)
  817. {
  818. return request->response.speed;
  819. }
  820. EXPORT_SYMBOL(fw_get_request_speed);
  821. /**
  822. * fw_request_get_timestamp: Get timestamp of the request.
  823. * @request: The opaque pointer to request structure.
  824. *
  825. * Get timestamp when 1394 OHCI controller receives the asynchronous request subaction. The
  826. * timestamp consists of the low order 3 bits of second field and the full 13 bits of count
  827. * field of isochronous cycle time register.
  828. *
  829. * Returns: timestamp of the request.
  830. */
  831. u32 fw_request_get_timestamp(const struct fw_request *request)
  832. {
  833. return request->timestamp;
  834. }
  835. EXPORT_SYMBOL_GPL(fw_request_get_timestamp);
  836. static void handle_exclusive_region_request(struct fw_card *card,
  837. struct fw_packet *p,
  838. struct fw_request *request,
  839. unsigned long long offset)
  840. {
  841. struct fw_address_handler *handler;
  842. int tcode, destination, source;
  843. destination = async_header_get_destination(p->header);
  844. source = async_header_get_source(p->header);
  845. tcode = async_header_get_tcode(p->header);
  846. if (tcode == TCODE_LOCK_REQUEST)
  847. tcode = 0x10 + async_header_get_extended_tcode(p->header);
  848. scoped_guard(rcu) {
  849. handler = lookup_enclosing_address_handler(&address_handler_list, offset,
  850. request->length);
  851. if (handler)
  852. get_address_handler(handler);
  853. }
  854. if (!handler) {
  855. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  856. return;
  857. }
  858. // Outside the RCU read-side critical section. Without spinlock. With reference count.
  859. handler->address_callback(card, request, tcode, destination, source, p->generation, offset,
  860. request->data, request->length, handler->callback_data);
  861. put_address_handler(handler);
  862. }
  863. // To use kmalloc allocator efficiently, this should be power of two.
  864. #define BUFFER_ON_KERNEL_STACK_SIZE 4
  865. static void handle_fcp_region_request(struct fw_card *card,
  866. struct fw_packet *p,
  867. struct fw_request *request,
  868. unsigned long long offset)
  869. {
  870. struct fw_address_handler *buffer_on_kernel_stack[BUFFER_ON_KERNEL_STACK_SIZE];
  871. struct fw_address_handler *handler, **handlers;
  872. int tcode, destination, source, i, count, buffer_size;
  873. if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  874. offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
  875. request->length > 0x200) {
  876. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  877. return;
  878. }
  879. tcode = async_header_get_tcode(p->header);
  880. destination = async_header_get_destination(p->header);
  881. source = async_header_get_source(p->header);
  882. if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
  883. tcode != TCODE_WRITE_BLOCK_REQUEST) {
  884. fw_send_response(card, request, RCODE_TYPE_ERROR);
  885. return;
  886. }
  887. count = 0;
  888. handlers = buffer_on_kernel_stack;
  889. buffer_size = ARRAY_SIZE(buffer_on_kernel_stack);
  890. scoped_guard(rcu) {
  891. list_for_each_entry_rcu(handler, &address_handler_list, link) {
  892. if (is_enclosing_handler(handler, offset, request->length)) {
  893. if (count >= buffer_size) {
  894. int next_size = buffer_size * 2;
  895. struct fw_address_handler **buffer_on_kernel_heap;
  896. if (handlers == buffer_on_kernel_stack)
  897. buffer_on_kernel_heap = NULL;
  898. else
  899. buffer_on_kernel_heap = handlers;
  900. buffer_on_kernel_heap =
  901. krealloc_array(buffer_on_kernel_heap, next_size,
  902. sizeof(*buffer_on_kernel_heap), GFP_ATOMIC);
  903. // FCP is used for purposes unrelated to significant system
  904. // resources (e.g. storage or networking), so allocation
  905. // failures are not considered so critical.
  906. if (!buffer_on_kernel_heap)
  907. break;
  908. if (handlers == buffer_on_kernel_stack) {
  909. memcpy(buffer_on_kernel_heap, buffer_on_kernel_stack,
  910. sizeof(buffer_on_kernel_stack));
  911. }
  912. handlers = buffer_on_kernel_heap;
  913. buffer_size = next_size;
  914. }
  915. get_address_handler(handler);
  916. handlers[count++] = handler;
  917. }
  918. }
  919. }
  920. for (i = 0; i < count; ++i) {
  921. handler = handlers[i];
  922. handler->address_callback(card, request, tcode, destination, source,
  923. p->generation, offset, request->data,
  924. request->length, handler->callback_data);
  925. put_address_handler(handler);
  926. }
  927. if (handlers != buffer_on_kernel_stack)
  928. kfree(handlers);
  929. fw_send_response(card, request, RCODE_COMPLETE);
  930. }
  931. void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  932. {
  933. struct fw_request *request;
  934. unsigned long long offset;
  935. unsigned int tcode;
  936. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  937. return;
  938. tcode = async_header_get_tcode(p->header);
  939. if (tcode_is_link_internal(tcode)) {
  940. trace_async_phy_inbound((uintptr_t)p, card->index, p->generation, p->ack, p->timestamp,
  941. p->header[1], p->header[2]);
  942. fw_cdev_handle_phy_packet(card, p);
  943. return;
  944. }
  945. request = allocate_request(card, p);
  946. if (request == NULL) {
  947. /* FIXME: send statically allocated busy packet. */
  948. return;
  949. }
  950. trace_async_request_inbound((uintptr_t)request, card->index, p->generation, p->speed,
  951. p->ack, p->timestamp, p->header, request->data,
  952. tcode_is_read_request(tcode) ? 0 : request->length / 4);
  953. offset = async_header_get_offset(p->header);
  954. if (!is_in_fcp_region(offset, request->length))
  955. handle_exclusive_region_request(card, p, request, offset);
  956. else
  957. handle_fcp_region_request(card, p, request, offset);
  958. }
  959. EXPORT_SYMBOL(fw_core_handle_request);
  960. void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  961. {
  962. struct fw_transaction *t = NULL;
  963. u32 *data;
  964. size_t data_length;
  965. int tcode, tlabel, source, rcode;
  966. tcode = async_header_get_tcode(p->header);
  967. tlabel = async_header_get_tlabel(p->header);
  968. source = async_header_get_source(p->header);
  969. rcode = async_header_get_rcode(p->header);
  970. // FIXME: sanity check packet, is length correct, does tcodes
  971. // and addresses match to the transaction request queried later.
  972. //
  973. // For the tracepoints event, let us decode the header here against the concern.
  974. switch (tcode) {
  975. case TCODE_READ_QUADLET_RESPONSE:
  976. data = (u32 *) &p->header[3];
  977. data_length = 4;
  978. break;
  979. case TCODE_WRITE_RESPONSE:
  980. data = NULL;
  981. data_length = 0;
  982. break;
  983. case TCODE_READ_BLOCK_RESPONSE:
  984. case TCODE_LOCK_RESPONSE:
  985. data = p->payload;
  986. data_length = async_header_get_data_length(p->header);
  987. break;
  988. default:
  989. /* Should never happen, this is just to shut up gcc. */
  990. data = NULL;
  991. data_length = 0;
  992. break;
  993. }
  994. // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
  995. // local destination never runs in any type of IRQ context.
  996. scoped_guard(spinlock_irqsave, &card->transactions.lock) {
  997. t = find_and_pop_transaction_entry(card,
  998. iter->node_id == source && iter->tlabel == tlabel);
  999. }
  1000. trace_async_response_inbound((uintptr_t)t, card->index, p->generation, p->speed, p->ack,
  1001. p->timestamp, p->header, data, data_length / 4);
  1002. if (!t) {
  1003. fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
  1004. source, tlabel);
  1005. return;
  1006. }
  1007. /*
  1008. * The response handler may be executed while the request handler
  1009. * is still pending. Cancel the request handler.
  1010. */
  1011. card->driver->cancel_packet(card, &t->packet);
  1012. if (!t->with_tstamp) {
  1013. t->callback.without_tstamp(card, rcode, data, data_length, t->callback_data);
  1014. } else {
  1015. t->callback.with_tstamp(card, rcode, t->packet.timestamp, p->timestamp, data,
  1016. data_length, t->callback_data);
  1017. }
  1018. }
  1019. EXPORT_SYMBOL(fw_core_handle_response);
  1020. /**
  1021. * fw_rcode_string - convert a firewire result code to an error description
  1022. * @rcode: the result code
  1023. */
  1024. const char *fw_rcode_string(int rcode)
  1025. {
  1026. static const char *const names[] = {
  1027. [RCODE_COMPLETE] = "no error",
  1028. [RCODE_CONFLICT_ERROR] = "conflict error",
  1029. [RCODE_DATA_ERROR] = "data error",
  1030. [RCODE_TYPE_ERROR] = "type error",
  1031. [RCODE_ADDRESS_ERROR] = "address error",
  1032. [RCODE_SEND_ERROR] = "send error",
  1033. [RCODE_CANCELLED] = "timeout",
  1034. [RCODE_BUSY] = "busy",
  1035. [RCODE_GENERATION] = "bus reset",
  1036. [RCODE_NO_ACK] = "no ack",
  1037. };
  1038. if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode])
  1039. return names[rcode];
  1040. else
  1041. return "unknown";
  1042. }
  1043. EXPORT_SYMBOL(fw_rcode_string);
  1044. static const struct fw_address_region topology_map_region =
  1045. { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
  1046. .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
  1047. static void handle_topology_map(struct fw_card *card, struct fw_request *request,
  1048. int tcode, int destination, int source, int generation,
  1049. unsigned long long offset, void *payload, size_t length,
  1050. void *callback_data)
  1051. {
  1052. int start;
  1053. if (!tcode_is_read_request(tcode)) {
  1054. fw_send_response(card, request, RCODE_TYPE_ERROR);
  1055. return;
  1056. }
  1057. if ((offset & 3) > 0 || (length & 3) > 0) {
  1058. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  1059. return;
  1060. }
  1061. start = (offset - topology_map_region.start) / 4;
  1062. // NOTE: This can be without irqsave when we can guarantee that fw_send_request() for local
  1063. // destination never runs in any type of IRQ context.
  1064. scoped_guard(spinlock_irqsave, &card->topology_map.lock)
  1065. memcpy(payload, &card->topology_map.buffer[start], length);
  1066. fw_send_response(card, request, RCODE_COMPLETE);
  1067. }
  1068. static struct fw_address_handler topology_map = {
  1069. .length = 0x400,
  1070. .address_callback = handle_topology_map,
  1071. };
  1072. static const struct fw_address_region registers_region =
  1073. { .start = CSR_REGISTER_BASE,
  1074. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
  1075. static void update_split_timeout(struct fw_card *card)
  1076. __must_hold(&card->split_timeout.lock)
  1077. {
  1078. unsigned int cycles;
  1079. cycles = card->split_timeout.hi * 8000 + (card->split_timeout.lo >> 19);
  1080. /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
  1081. cycles = clamp(cycles, 800u, 3u * 8000u);
  1082. card->split_timeout.cycles = cycles;
  1083. card->split_timeout.jiffies = isoc_cycles_to_jiffies(cycles);
  1084. }
  1085. static void handle_registers(struct fw_card *card, struct fw_request *request,
  1086. int tcode, int destination, int source, int generation,
  1087. unsigned long long offset, void *payload, size_t length,
  1088. void *callback_data)
  1089. {
  1090. int reg = offset & ~CSR_REGISTER_BASE;
  1091. __be32 *data = payload;
  1092. int rcode = RCODE_COMPLETE;
  1093. switch (reg) {
  1094. case CSR_PRIORITY_BUDGET:
  1095. if (!card->priority_budget_implemented) {
  1096. rcode = RCODE_ADDRESS_ERROR;
  1097. break;
  1098. }
  1099. fallthrough;
  1100. case CSR_NODE_IDS:
  1101. /*
  1102. * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
  1103. * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
  1104. */
  1105. fallthrough;
  1106. case CSR_STATE_CLEAR:
  1107. case CSR_STATE_SET:
  1108. case CSR_CYCLE_TIME:
  1109. case CSR_BUS_TIME:
  1110. case CSR_BUSY_TIMEOUT:
  1111. if (tcode == TCODE_READ_QUADLET_REQUEST)
  1112. *data = cpu_to_be32(card->driver->read_csr(card, reg));
  1113. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1114. card->driver->write_csr(card, reg, be32_to_cpu(*data));
  1115. else
  1116. rcode = RCODE_TYPE_ERROR;
  1117. break;
  1118. case CSR_RESET_START:
  1119. if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1120. card->driver->write_csr(card, CSR_STATE_CLEAR,
  1121. CSR_STATE_BIT_ABDICATE);
  1122. else
  1123. rcode = RCODE_TYPE_ERROR;
  1124. break;
  1125. case CSR_SPLIT_TIMEOUT_HI:
  1126. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  1127. *data = cpu_to_be32(card->split_timeout.hi);
  1128. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  1129. // NOTE: This can be without irqsave when we can guarantee that
  1130. // __fw_send_request() for local destination never runs in any type of IRQ
  1131. // context.
  1132. scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
  1133. card->split_timeout.hi = be32_to_cpu(*data) & 7;
  1134. update_split_timeout(card);
  1135. }
  1136. } else {
  1137. rcode = RCODE_TYPE_ERROR;
  1138. }
  1139. break;
  1140. case CSR_SPLIT_TIMEOUT_LO:
  1141. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  1142. *data = cpu_to_be32(card->split_timeout.lo);
  1143. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  1144. // NOTE: This can be without irqsave when we can guarantee that
  1145. // __fw_send_request() for local destination never runs in any type of IRQ
  1146. // context.
  1147. scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
  1148. card->split_timeout.lo = be32_to_cpu(*data) & 0xfff80000;
  1149. update_split_timeout(card);
  1150. }
  1151. } else {
  1152. rcode = RCODE_TYPE_ERROR;
  1153. }
  1154. break;
  1155. case CSR_MAINT_UTILITY:
  1156. if (tcode == TCODE_READ_QUADLET_REQUEST)
  1157. *data = card->maint_utility_register;
  1158. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1159. card->maint_utility_register = *data;
  1160. else
  1161. rcode = RCODE_TYPE_ERROR;
  1162. break;
  1163. case CSR_BROADCAST_CHANNEL:
  1164. if (tcode == TCODE_READ_QUADLET_REQUEST)
  1165. *data = cpu_to_be32(card->broadcast_channel);
  1166. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1167. card->broadcast_channel =
  1168. (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
  1169. BROADCAST_CHANNEL_INITIAL;
  1170. else
  1171. rcode = RCODE_TYPE_ERROR;
  1172. break;
  1173. case CSR_BUS_MANAGER_ID:
  1174. case CSR_BANDWIDTH_AVAILABLE:
  1175. case CSR_CHANNELS_AVAILABLE_HI:
  1176. case CSR_CHANNELS_AVAILABLE_LO:
  1177. /*
  1178. * FIXME: these are handled by the OHCI hardware and
  1179. * the stack never sees these request. If we add
  1180. * support for a new type of controller that doesn't
  1181. * handle this in hardware we need to deal with these
  1182. * transactions.
  1183. */
  1184. BUG();
  1185. break;
  1186. default:
  1187. rcode = RCODE_ADDRESS_ERROR;
  1188. break;
  1189. }
  1190. fw_send_response(card, request, rcode);
  1191. }
  1192. static struct fw_address_handler registers = {
  1193. .length = 0x400,
  1194. .address_callback = handle_registers,
  1195. };
  1196. static void handle_low_memory(struct fw_card *card, struct fw_request *request,
  1197. int tcode, int destination, int source, int generation,
  1198. unsigned long long offset, void *payload, size_t length,
  1199. void *callback_data)
  1200. {
  1201. /*
  1202. * This catches requests not handled by the physical DMA unit,
  1203. * i.e., wrong transaction types or unauthorized source nodes.
  1204. */
  1205. fw_send_response(card, request, RCODE_TYPE_ERROR);
  1206. }
  1207. static struct fw_address_handler low_memory = {
  1208. .length = FW_MAX_PHYSICAL_RANGE,
  1209. .address_callback = handle_low_memory,
  1210. };
  1211. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1212. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  1213. MODULE_LICENSE("GPL");
  1214. static const u32 vendor_textual_descriptor[] = {
  1215. /* textual descriptor leaf () */
  1216. 0x00060000,
  1217. 0x00000000,
  1218. 0x00000000,
  1219. 0x4c696e75, /* L i n u */
  1220. 0x78204669, /* x F i */
  1221. 0x72657769, /* r e w i */
  1222. 0x72650000, /* r e */
  1223. };
  1224. static const u32 model_textual_descriptor[] = {
  1225. /* model descriptor leaf () */
  1226. 0x00030000,
  1227. 0x00000000,
  1228. 0x00000000,
  1229. 0x4a756a75, /* J u j u */
  1230. };
  1231. static struct fw_descriptor vendor_id_descriptor = {
  1232. .length = ARRAY_SIZE(vendor_textual_descriptor),
  1233. .immediate = 0x03001f11,
  1234. .key = 0x81000000,
  1235. .data = vendor_textual_descriptor,
  1236. };
  1237. static struct fw_descriptor model_id_descriptor = {
  1238. .length = ARRAY_SIZE(model_textual_descriptor),
  1239. .immediate = 0x17023901,
  1240. .key = 0x81000000,
  1241. .data = model_textual_descriptor,
  1242. };
  1243. static int __init fw_core_init(void)
  1244. {
  1245. int ret;
  1246. fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM | WQ_UNBOUND,
  1247. 0);
  1248. if (!fw_workqueue)
  1249. return -ENOMEM;
  1250. ret = bus_register(&fw_bus_type);
  1251. if (ret < 0) {
  1252. destroy_workqueue(fw_workqueue);
  1253. return ret;
  1254. }
  1255. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  1256. if (fw_cdev_major < 0) {
  1257. bus_unregister(&fw_bus_type);
  1258. destroy_workqueue(fw_workqueue);
  1259. return fw_cdev_major;
  1260. }
  1261. fw_core_add_address_handler(&topology_map, &topology_map_region);
  1262. fw_core_add_address_handler(&registers, &registers_region);
  1263. fw_core_add_address_handler(&low_memory, &low_memory_region);
  1264. fw_core_add_descriptor(&vendor_id_descriptor);
  1265. fw_core_add_descriptor(&model_id_descriptor);
  1266. return 0;
  1267. }
  1268. static void __exit fw_core_cleanup(void)
  1269. {
  1270. unregister_chrdev(fw_cdev_major, "firewire");
  1271. bus_unregister(&fw_bus_type);
  1272. destroy_workqueue(fw_workqueue);
  1273. xa_destroy(&fw_device_xa);
  1274. }
  1275. module_init(fw_core_init);
  1276. module_exit(fw_core_cleanup);