ohci.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /*
  2. * Driver for OHCI 1394 controllers
  3. *
  4. * Copyright (C) 2003-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/compiler.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/firewire.h>
  25. #include <linux/firewire-constants.h>
  26. #include <linux/gfp.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/pci.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/string.h>
  38. #include <asm/atomic.h>
  39. #include <asm/byteorder.h>
  40. #include <asm/page.h>
  41. #include <asm/system.h>
  42. #ifdef CONFIG_PPC_PMAC
  43. #include <asm/pmac_feature.h>
  44. #endif
  45. #include "core.h"
  46. #include "ohci.h"
  47. #define DESCRIPTOR_OUTPUT_MORE 0
  48. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  49. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  50. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  51. #define DESCRIPTOR_STATUS (1 << 11)
  52. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  53. #define DESCRIPTOR_PING (1 << 7)
  54. #define DESCRIPTOR_YY (1 << 6)
  55. #define DESCRIPTOR_NO_IRQ (0 << 4)
  56. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  57. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  58. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  59. #define DESCRIPTOR_WAIT (3 << 0)
  60. struct descriptor {
  61. __le16 req_count;
  62. __le16 control;
  63. __le32 data_address;
  64. __le32 branch_address;
  65. __le16 res_count;
  66. __le16 transfer_status;
  67. } __attribute__((aligned(16)));
  68. struct db_descriptor {
  69. __le16 first_size;
  70. __le16 control;
  71. __le16 second_req_count;
  72. __le16 first_req_count;
  73. __le32 branch_address;
  74. __le16 second_res_count;
  75. __le16 first_res_count;
  76. __le32 reserved0;
  77. __le32 first_buffer;
  78. __le32 second_buffer;
  79. __le32 reserved1;
  80. } __attribute__((aligned(16)));
  81. #define CONTROL_SET(regs) (regs)
  82. #define CONTROL_CLEAR(regs) ((regs) + 4)
  83. #define COMMAND_PTR(regs) ((regs) + 12)
  84. #define CONTEXT_MATCH(regs) ((regs) + 16)
  85. struct ar_buffer {
  86. struct descriptor descriptor;
  87. struct ar_buffer *next;
  88. __le32 data[0];
  89. };
  90. struct ar_context {
  91. struct fw_ohci *ohci;
  92. struct ar_buffer *current_buffer;
  93. struct ar_buffer *last_buffer;
  94. void *pointer;
  95. u32 regs;
  96. struct tasklet_struct tasklet;
  97. };
  98. struct context;
  99. typedef int (*descriptor_callback_t)(struct context *ctx,
  100. struct descriptor *d,
  101. struct descriptor *last);
  102. /*
  103. * A buffer that contains a block of DMA-able coherent memory used for
  104. * storing a portion of a DMA descriptor program.
  105. */
  106. struct descriptor_buffer {
  107. struct list_head list;
  108. dma_addr_t buffer_bus;
  109. size_t buffer_size;
  110. size_t used;
  111. struct descriptor buffer[0];
  112. };
  113. struct context {
  114. struct fw_ohci *ohci;
  115. u32 regs;
  116. int total_allocation;
  117. /*
  118. * List of page-sized buffers for storing DMA descriptors.
  119. * Head of list contains buffers in use and tail of list contains
  120. * free buffers.
  121. */
  122. struct list_head buffer_list;
  123. /*
  124. * Pointer to a buffer inside buffer_list that contains the tail
  125. * end of the current DMA program.
  126. */
  127. struct descriptor_buffer *buffer_tail;
  128. /*
  129. * The descriptor containing the branch address of the first
  130. * descriptor that has not yet been filled by the device.
  131. */
  132. struct descriptor *last;
  133. /*
  134. * The last descriptor in the DMA program. It contains the branch
  135. * address that must be updated upon appending a new descriptor.
  136. */
  137. struct descriptor *prev;
  138. descriptor_callback_t callback;
  139. struct tasklet_struct tasklet;
  140. };
  141. #define IT_HEADER_SY(v) ((v) << 0)
  142. #define IT_HEADER_TCODE(v) ((v) << 4)
  143. #define IT_HEADER_CHANNEL(v) ((v) << 8)
  144. #define IT_HEADER_TAG(v) ((v) << 14)
  145. #define IT_HEADER_SPEED(v) ((v) << 16)
  146. #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
  147. struct iso_context {
  148. struct fw_iso_context base;
  149. struct context context;
  150. int excess_bytes;
  151. void *header;
  152. size_t header_length;
  153. };
  154. #define CONFIG_ROM_SIZE 1024
  155. struct fw_ohci {
  156. struct fw_card card;
  157. __iomem char *registers;
  158. dma_addr_t self_id_bus;
  159. __le32 *self_id_cpu;
  160. struct tasklet_struct bus_reset_tasklet;
  161. int node_id;
  162. int generation;
  163. int request_generation; /* for timestamping incoming requests */
  164. atomic_t bus_seconds;
  165. bool use_dualbuffer;
  166. bool old_uninorth;
  167. bool bus_reset_packet_quirk;
  168. /*
  169. * Spinlock for accessing fw_ohci data. Never call out of
  170. * this driver with this lock held.
  171. */
  172. spinlock_t lock;
  173. u32 self_id_buffer[512];
  174. /* Config rom buffers */
  175. __be32 *config_rom;
  176. dma_addr_t config_rom_bus;
  177. __be32 *next_config_rom;
  178. dma_addr_t next_config_rom_bus;
  179. u32 next_header;
  180. struct ar_context ar_request_ctx;
  181. struct ar_context ar_response_ctx;
  182. struct context at_request_ctx;
  183. struct context at_response_ctx;
  184. u32 it_context_mask;
  185. struct iso_context *it_context_list;
  186. u64 ir_context_channels;
  187. u32 ir_context_mask;
  188. struct iso_context *ir_context_list;
  189. };
  190. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  191. {
  192. return container_of(card, struct fw_ohci, card);
  193. }
  194. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  195. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  196. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  197. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  198. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  199. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  200. #define CONTEXT_RUN 0x8000
  201. #define CONTEXT_WAKE 0x1000
  202. #define CONTEXT_DEAD 0x0800
  203. #define CONTEXT_ACTIVE 0x0400
  204. #define OHCI1394_MAX_AT_REQ_RETRIES 0xf
  205. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  206. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  207. #define OHCI1394_REGISTER_SIZE 0x800
  208. #define OHCI_LOOP_COUNT 500
  209. #define OHCI1394_PCI_HCI_Control 0x40
  210. #define SELF_ID_BUF_SIZE 0x800
  211. #define OHCI_TCODE_PHY_PACKET 0x0e
  212. #define OHCI_VERSION_1_1 0x010010
  213. static char ohci_driver_name[] = KBUILD_MODNAME;
  214. #ifdef CONFIG_FIREWIRE_OHCI_DEBUG
  215. #define OHCI_PARAM_DEBUG_AT_AR 1
  216. #define OHCI_PARAM_DEBUG_SELFIDS 2
  217. #define OHCI_PARAM_DEBUG_IRQS 4
  218. #define OHCI_PARAM_DEBUG_BUSRESETS 8 /* only effective before chip init */
  219. static int param_debug;
  220. module_param_named(debug, param_debug, int, 0644);
  221. MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
  222. ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR)
  223. ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS)
  224. ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS)
  225. ", busReset events = " __stringify(OHCI_PARAM_DEBUG_BUSRESETS)
  226. ", or a combination, or all = -1)");
  227. static void log_irqs(u32 evt)
  228. {
  229. if (likely(!(param_debug &
  230. (OHCI_PARAM_DEBUG_IRQS | OHCI_PARAM_DEBUG_BUSRESETS))))
  231. return;
  232. if (!(param_debug & OHCI_PARAM_DEBUG_IRQS) &&
  233. !(evt & OHCI1394_busReset))
  234. return;
  235. fw_notify("IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
  236. evt & OHCI1394_selfIDComplete ? " selfID" : "",
  237. evt & OHCI1394_RQPkt ? " AR_req" : "",
  238. evt & OHCI1394_RSPkt ? " AR_resp" : "",
  239. evt & OHCI1394_reqTxComplete ? " AT_req" : "",
  240. evt & OHCI1394_respTxComplete ? " AT_resp" : "",
  241. evt & OHCI1394_isochRx ? " IR" : "",
  242. evt & OHCI1394_isochTx ? " IT" : "",
  243. evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "",
  244. evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "",
  245. evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "",
  246. evt & OHCI1394_regAccessFail ? " regAccessFail" : "",
  247. evt & OHCI1394_busReset ? " busReset" : "",
  248. evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
  249. OHCI1394_RSPkt | OHCI1394_reqTxComplete |
  250. OHCI1394_respTxComplete | OHCI1394_isochRx |
  251. OHCI1394_isochTx | OHCI1394_postedWriteErr |
  252. OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
  253. OHCI1394_regAccessFail | OHCI1394_busReset)
  254. ? " ?" : "");
  255. }
  256. static const char *speed[] = {
  257. [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta",
  258. };
  259. static const char *power[] = {
  260. [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W",
  261. [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W",
  262. };
  263. static const char port[] = { '.', '-', 'p', 'c', };
  264. static char _p(u32 *s, int shift)
  265. {
  266. return port[*s >> shift & 3];
  267. }
  268. static void log_selfids(int node_id, int generation, int self_id_count, u32 *s)
  269. {
  270. if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
  271. return;
  272. fw_notify("%d selfIDs, generation %d, local node ID %04x\n",
  273. self_id_count, generation, node_id);
  274. for (; self_id_count--; ++s)
  275. if ((*s & 1 << 23) == 0)
  276. fw_notify("selfID 0: %08x, phy %d [%c%c%c] "
  277. "%s gc=%d %s %s%s%s\n",
  278. *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
  279. speed[*s >> 14 & 3], *s >> 16 & 63,
  280. power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
  281. *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
  282. else
  283. fw_notify("selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
  284. *s, *s >> 24 & 63,
  285. _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
  286. _p(s, 8), _p(s, 6), _p(s, 4), _p(s, 2));
  287. }
  288. static const char *evts[] = {
  289. [0x00] = "evt_no_status", [0x01] = "-reserved-",
  290. [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack",
  291. [0x04] = "evt_underrun", [0x05] = "evt_overrun",
  292. [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read",
  293. [0x08] = "evt_data_write", [0x09] = "evt_bus_reset",
  294. [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err",
  295. [0x0c] = "-reserved-", [0x0d] = "-reserved-",
  296. [0x0e] = "evt_unknown", [0x0f] = "evt_flushed",
  297. [0x10] = "-reserved-", [0x11] = "ack_complete",
  298. [0x12] = "ack_pending ", [0x13] = "-reserved-",
  299. [0x14] = "ack_busy_X", [0x15] = "ack_busy_A",
  300. [0x16] = "ack_busy_B", [0x17] = "-reserved-",
  301. [0x18] = "-reserved-", [0x19] = "-reserved-",
  302. [0x1a] = "-reserved-", [0x1b] = "ack_tardy",
  303. [0x1c] = "-reserved-", [0x1d] = "ack_data_error",
  304. [0x1e] = "ack_type_error", [0x1f] = "-reserved-",
  305. [0x20] = "pending/cancelled",
  306. };
  307. static const char *tcodes[] = {
  308. [0x0] = "QW req", [0x1] = "BW req",
  309. [0x2] = "W resp", [0x3] = "-reserved-",
  310. [0x4] = "QR req", [0x5] = "BR req",
  311. [0x6] = "QR resp", [0x7] = "BR resp",
  312. [0x8] = "cycle start", [0x9] = "Lk req",
  313. [0xa] = "async stream packet", [0xb] = "Lk resp",
  314. [0xc] = "-reserved-", [0xd] = "-reserved-",
  315. [0xe] = "link internal", [0xf] = "-reserved-",
  316. };
  317. static const char *phys[] = {
  318. [0x0] = "phy config packet", [0x1] = "link-on packet",
  319. [0x2] = "self-id packet", [0x3] = "-reserved-",
  320. };
  321. static void log_ar_at_event(char dir, int speed, u32 *header, int evt)
  322. {
  323. int tcode = header[0] >> 4 & 0xf;
  324. char specific[12];
  325. if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
  326. return;
  327. if (unlikely(evt >= ARRAY_SIZE(evts)))
  328. evt = 0x1f;
  329. if (evt == OHCI1394_evt_bus_reset) {
  330. fw_notify("A%c evt_bus_reset, generation %d\n",
  331. dir, (header[2] >> 16) & 0xff);
  332. return;
  333. }
  334. if (header[0] == ~header[1]) {
  335. fw_notify("A%c %s, %s, %08x\n",
  336. dir, evts[evt], phys[header[0] >> 30 & 0x3], header[0]);
  337. return;
  338. }
  339. switch (tcode) {
  340. case 0x0: case 0x6: case 0x8:
  341. snprintf(specific, sizeof(specific), " = %08x",
  342. be32_to_cpu((__force __be32)header[3]));
  343. break;
  344. case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
  345. snprintf(specific, sizeof(specific), " %x,%x",
  346. header[3] >> 16, header[3] & 0xffff);
  347. break;
  348. default:
  349. specific[0] = '\0';
  350. }
  351. switch (tcode) {
  352. case 0xe: case 0xa:
  353. fw_notify("A%c %s, %s\n", dir, evts[evt], tcodes[tcode]);
  354. break;
  355. case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
  356. fw_notify("A%c spd %x tl %02x, "
  357. "%04x -> %04x, %s, "
  358. "%s, %04x%08x%s\n",
  359. dir, speed, header[0] >> 10 & 0x3f,
  360. header[1] >> 16, header[0] >> 16, evts[evt],
  361. tcodes[tcode], header[1] & 0xffff, header[2], specific);
  362. break;
  363. default:
  364. fw_notify("A%c spd %x tl %02x, "
  365. "%04x -> %04x, %s, "
  366. "%s%s\n",
  367. dir, speed, header[0] >> 10 & 0x3f,
  368. header[1] >> 16, header[0] >> 16, evts[evt],
  369. tcodes[tcode], specific);
  370. }
  371. }
  372. #else
  373. #define log_irqs(evt)
  374. #define log_selfids(node_id, generation, self_id_count, sid)
  375. #define log_ar_at_event(dir, speed, header, evt)
  376. #endif /* CONFIG_FIREWIRE_OHCI_DEBUG */
  377. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  378. {
  379. writel(data, ohci->registers + offset);
  380. }
  381. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  382. {
  383. return readl(ohci->registers + offset);
  384. }
  385. static inline void flush_writes(const struct fw_ohci *ohci)
  386. {
  387. /* Do a dummy read to flush writes. */
  388. reg_read(ohci, OHCI1394_Version);
  389. }
  390. static int ohci_update_phy_reg(struct fw_card *card, int addr,
  391. int clear_bits, int set_bits)
  392. {
  393. struct fw_ohci *ohci = fw_ohci(card);
  394. u32 val, old;
  395. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  396. flush_writes(ohci);
  397. msleep(2);
  398. val = reg_read(ohci, OHCI1394_PhyControl);
  399. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  400. fw_error("failed to set phy reg bits.\n");
  401. return -EBUSY;
  402. }
  403. old = OHCI1394_PhyControl_ReadData(val);
  404. old = (old & ~clear_bits) | set_bits;
  405. reg_write(ohci, OHCI1394_PhyControl,
  406. OHCI1394_PhyControl_Write(addr, old));
  407. return 0;
  408. }
  409. static int ar_context_add_page(struct ar_context *ctx)
  410. {
  411. struct device *dev = ctx->ohci->card.device;
  412. struct ar_buffer *ab;
  413. dma_addr_t uninitialized_var(ab_bus);
  414. size_t offset;
  415. ab = dma_alloc_coherent(dev, PAGE_SIZE, &ab_bus, GFP_ATOMIC);
  416. if (ab == NULL)
  417. return -ENOMEM;
  418. ab->next = NULL;
  419. memset(&ab->descriptor, 0, sizeof(ab->descriptor));
  420. ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  421. DESCRIPTOR_STATUS |
  422. DESCRIPTOR_BRANCH_ALWAYS);
  423. offset = offsetof(struct ar_buffer, data);
  424. ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
  425. ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
  426. ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
  427. ab->descriptor.branch_address = 0;
  428. ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
  429. ctx->last_buffer->next = ab;
  430. ctx->last_buffer = ab;
  431. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  432. flush_writes(ctx->ohci);
  433. return 0;
  434. }
  435. static void ar_context_release(struct ar_context *ctx)
  436. {
  437. struct ar_buffer *ab, *ab_next;
  438. size_t offset;
  439. dma_addr_t ab_bus;
  440. for (ab = ctx->current_buffer; ab; ab = ab_next) {
  441. ab_next = ab->next;
  442. offset = offsetof(struct ar_buffer, data);
  443. ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  444. dma_free_coherent(ctx->ohci->card.device, PAGE_SIZE,
  445. ab, ab_bus);
  446. }
  447. }
  448. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  449. #define cond_le32_to_cpu(v) \
  450. (ohci->old_uninorth ? (__force __u32)(v) : le32_to_cpu(v))
  451. #else
  452. #define cond_le32_to_cpu(v) le32_to_cpu(v)
  453. #endif
  454. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  455. {
  456. struct fw_ohci *ohci = ctx->ohci;
  457. struct fw_packet p;
  458. u32 status, length, tcode;
  459. int evt;
  460. p.header[0] = cond_le32_to_cpu(buffer[0]);
  461. p.header[1] = cond_le32_to_cpu(buffer[1]);
  462. p.header[2] = cond_le32_to_cpu(buffer[2]);
  463. tcode = (p.header[0] >> 4) & 0x0f;
  464. switch (tcode) {
  465. case TCODE_WRITE_QUADLET_REQUEST:
  466. case TCODE_READ_QUADLET_RESPONSE:
  467. p.header[3] = (__force __u32) buffer[3];
  468. p.header_length = 16;
  469. p.payload_length = 0;
  470. break;
  471. case TCODE_READ_BLOCK_REQUEST :
  472. p.header[3] = cond_le32_to_cpu(buffer[3]);
  473. p.header_length = 16;
  474. p.payload_length = 0;
  475. break;
  476. case TCODE_WRITE_BLOCK_REQUEST:
  477. case TCODE_READ_BLOCK_RESPONSE:
  478. case TCODE_LOCK_REQUEST:
  479. case TCODE_LOCK_RESPONSE:
  480. p.header[3] = cond_le32_to_cpu(buffer[3]);
  481. p.header_length = 16;
  482. p.payload_length = p.header[3] >> 16;
  483. break;
  484. case TCODE_WRITE_RESPONSE:
  485. case TCODE_READ_QUADLET_REQUEST:
  486. case OHCI_TCODE_PHY_PACKET:
  487. p.header_length = 12;
  488. p.payload_length = 0;
  489. break;
  490. default:
  491. /* FIXME: Stop context, discard everything, and restart? */
  492. p.header_length = 0;
  493. p.payload_length = 0;
  494. }
  495. p.payload = (void *) buffer + p.header_length;
  496. /* FIXME: What to do about evt_* errors? */
  497. length = (p.header_length + p.payload_length + 3) / 4;
  498. status = cond_le32_to_cpu(buffer[length]);
  499. evt = (status >> 16) & 0x1f;
  500. p.ack = evt - 16;
  501. p.speed = (status >> 21) & 0x7;
  502. p.timestamp = status & 0xffff;
  503. p.generation = ohci->request_generation;
  504. log_ar_at_event('R', p.speed, p.header, evt);
  505. /*
  506. * The OHCI bus reset handler synthesizes a phy packet with
  507. * the new generation number when a bus reset happens (see
  508. * section 8.4.2.3). This helps us determine when a request
  509. * was received and make sure we send the response in the same
  510. * generation. We only need this for requests; for responses
  511. * we use the unique tlabel for finding the matching
  512. * request.
  513. *
  514. * Alas some chips sometimes emit bus reset packets with a
  515. * wrong generation. We set the correct generation for these
  516. * at a slightly incorrect time (in bus_reset_tasklet).
  517. */
  518. if (evt == OHCI1394_evt_bus_reset) {
  519. if (!ohci->bus_reset_packet_quirk)
  520. ohci->request_generation = (p.header[2] >> 16) & 0xff;
  521. } else if (ctx == &ohci->ar_request_ctx) {
  522. fw_core_handle_request(&ohci->card, &p);
  523. } else {
  524. fw_core_handle_response(&ohci->card, &p);
  525. }
  526. return buffer + length + 1;
  527. }
  528. static void ar_context_tasklet(unsigned long data)
  529. {
  530. struct ar_context *ctx = (struct ar_context *)data;
  531. struct fw_ohci *ohci = ctx->ohci;
  532. struct ar_buffer *ab;
  533. struct descriptor *d;
  534. void *buffer, *end;
  535. ab = ctx->current_buffer;
  536. d = &ab->descriptor;
  537. if (d->res_count == 0) {
  538. size_t size, rest, offset;
  539. dma_addr_t start_bus;
  540. void *start;
  541. /*
  542. * This descriptor is finished and we may have a
  543. * packet split across this and the next buffer. We
  544. * reuse the page for reassembling the split packet.
  545. */
  546. offset = offsetof(struct ar_buffer, data);
  547. start = buffer = ab;
  548. start_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  549. ab = ab->next;
  550. d = &ab->descriptor;
  551. size = buffer + PAGE_SIZE - ctx->pointer;
  552. rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
  553. memmove(buffer, ctx->pointer, size);
  554. memcpy(buffer + size, ab->data, rest);
  555. ctx->current_buffer = ab;
  556. ctx->pointer = (void *) ab->data + rest;
  557. end = buffer + size + rest;
  558. while (buffer < end)
  559. buffer = handle_ar_packet(ctx, buffer);
  560. dma_free_coherent(ohci->card.device, PAGE_SIZE,
  561. start, start_bus);
  562. ar_context_add_page(ctx);
  563. } else {
  564. buffer = ctx->pointer;
  565. ctx->pointer = end =
  566. (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
  567. while (buffer < end)
  568. buffer = handle_ar_packet(ctx, buffer);
  569. }
  570. }
  571. static int ar_context_init(struct ar_context *ctx,
  572. struct fw_ohci *ohci, u32 regs)
  573. {
  574. struct ar_buffer ab;
  575. ctx->regs = regs;
  576. ctx->ohci = ohci;
  577. ctx->last_buffer = &ab;
  578. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  579. ar_context_add_page(ctx);
  580. ar_context_add_page(ctx);
  581. ctx->current_buffer = ab.next;
  582. ctx->pointer = ctx->current_buffer->data;
  583. return 0;
  584. }
  585. static void ar_context_run(struct ar_context *ctx)
  586. {
  587. struct ar_buffer *ab = ctx->current_buffer;
  588. dma_addr_t ab_bus;
  589. size_t offset;
  590. offset = offsetof(struct ar_buffer, data);
  591. ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  592. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
  593. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  594. flush_writes(ctx->ohci);
  595. }
  596. static struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
  597. {
  598. int b, key;
  599. b = (le16_to_cpu(d->control) & DESCRIPTOR_BRANCH_ALWAYS) >> 2;
  600. key = (le16_to_cpu(d->control) & DESCRIPTOR_KEY_IMMEDIATE) >> 8;
  601. /* figure out which descriptor the branch address goes in */
  602. if (z == 2 && (b == 3 || key == 2))
  603. return d;
  604. else
  605. return d + z - 1;
  606. }
  607. static void context_tasklet(unsigned long data)
  608. {
  609. struct context *ctx = (struct context *) data;
  610. struct descriptor *d, *last;
  611. u32 address;
  612. int z;
  613. struct descriptor_buffer *desc;
  614. desc = list_entry(ctx->buffer_list.next,
  615. struct descriptor_buffer, list);
  616. last = ctx->last;
  617. while (last->branch_address != 0) {
  618. struct descriptor_buffer *old_desc = desc;
  619. address = le32_to_cpu(last->branch_address);
  620. z = address & 0xf;
  621. address &= ~0xf;
  622. /* If the branch address points to a buffer outside of the
  623. * current buffer, advance to the next buffer. */
  624. if (address < desc->buffer_bus ||
  625. address >= desc->buffer_bus + desc->used)
  626. desc = list_entry(desc->list.next,
  627. struct descriptor_buffer, list);
  628. d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
  629. last = find_branch_descriptor(d, z);
  630. if (!ctx->callback(ctx, d, last))
  631. break;
  632. if (old_desc != desc) {
  633. /* If we've advanced to the next buffer, move the
  634. * previous buffer to the free list. */
  635. unsigned long flags;
  636. old_desc->used = 0;
  637. spin_lock_irqsave(&ctx->ohci->lock, flags);
  638. list_move_tail(&old_desc->list, &ctx->buffer_list);
  639. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  640. }
  641. ctx->last = last;
  642. }
  643. }
  644. /*
  645. * Allocate a new buffer and add it to the list of free buffers for this
  646. * context. Must be called with ohci->lock held.
  647. */
  648. static int context_add_buffer(struct context *ctx)
  649. {
  650. struct descriptor_buffer *desc;
  651. dma_addr_t uninitialized_var(bus_addr);
  652. int offset;
  653. /*
  654. * 16MB of descriptors should be far more than enough for any DMA
  655. * program. This will catch run-away userspace or DoS attacks.
  656. */
  657. if (ctx->total_allocation >= 16*1024*1024)
  658. return -ENOMEM;
  659. desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
  660. &bus_addr, GFP_ATOMIC);
  661. if (!desc)
  662. return -ENOMEM;
  663. offset = (void *)&desc->buffer - (void *)desc;
  664. desc->buffer_size = PAGE_SIZE - offset;
  665. desc->buffer_bus = bus_addr + offset;
  666. desc->used = 0;
  667. list_add_tail(&desc->list, &ctx->buffer_list);
  668. ctx->total_allocation += PAGE_SIZE;
  669. return 0;
  670. }
  671. static int context_init(struct context *ctx, struct fw_ohci *ohci,
  672. u32 regs, descriptor_callback_t callback)
  673. {
  674. ctx->ohci = ohci;
  675. ctx->regs = regs;
  676. ctx->total_allocation = 0;
  677. INIT_LIST_HEAD(&ctx->buffer_list);
  678. if (context_add_buffer(ctx) < 0)
  679. return -ENOMEM;
  680. ctx->buffer_tail = list_entry(ctx->buffer_list.next,
  681. struct descriptor_buffer, list);
  682. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  683. ctx->callback = callback;
  684. /*
  685. * We put a dummy descriptor in the buffer that has a NULL
  686. * branch address and looks like it's been sent. That way we
  687. * have a descriptor to append DMA programs to.
  688. */
  689. memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
  690. ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  691. ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
  692. ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
  693. ctx->last = ctx->buffer_tail->buffer;
  694. ctx->prev = ctx->buffer_tail->buffer;
  695. return 0;
  696. }
  697. static void context_release(struct context *ctx)
  698. {
  699. struct fw_card *card = &ctx->ohci->card;
  700. struct descriptor_buffer *desc, *tmp;
  701. list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
  702. dma_free_coherent(card->device, PAGE_SIZE, desc,
  703. desc->buffer_bus -
  704. ((void *)&desc->buffer - (void *)desc));
  705. }
  706. /* Must be called with ohci->lock held */
  707. static struct descriptor *context_get_descriptors(struct context *ctx,
  708. int z, dma_addr_t *d_bus)
  709. {
  710. struct descriptor *d = NULL;
  711. struct descriptor_buffer *desc = ctx->buffer_tail;
  712. if (z * sizeof(*d) > desc->buffer_size)
  713. return NULL;
  714. if (z * sizeof(*d) > desc->buffer_size - desc->used) {
  715. /* No room for the descriptor in this buffer, so advance to the
  716. * next one. */
  717. if (desc->list.next == &ctx->buffer_list) {
  718. /* If there is no free buffer next in the list,
  719. * allocate one. */
  720. if (context_add_buffer(ctx) < 0)
  721. return NULL;
  722. }
  723. desc = list_entry(desc->list.next,
  724. struct descriptor_buffer, list);
  725. ctx->buffer_tail = desc;
  726. }
  727. d = desc->buffer + desc->used / sizeof(*d);
  728. memset(d, 0, z * sizeof(*d));
  729. *d_bus = desc->buffer_bus + desc->used;
  730. return d;
  731. }
  732. static void context_run(struct context *ctx, u32 extra)
  733. {
  734. struct fw_ohci *ohci = ctx->ohci;
  735. reg_write(ohci, COMMAND_PTR(ctx->regs),
  736. le32_to_cpu(ctx->last->branch_address));
  737. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  738. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  739. flush_writes(ohci);
  740. }
  741. static void context_append(struct context *ctx,
  742. struct descriptor *d, int z, int extra)
  743. {
  744. dma_addr_t d_bus;
  745. struct descriptor_buffer *desc = ctx->buffer_tail;
  746. d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
  747. desc->used += (z + extra) * sizeof(*d);
  748. ctx->prev->branch_address = cpu_to_le32(d_bus | z);
  749. ctx->prev = find_branch_descriptor(d, z);
  750. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  751. flush_writes(ctx->ohci);
  752. }
  753. static void context_stop(struct context *ctx)
  754. {
  755. u32 reg;
  756. int i;
  757. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  758. flush_writes(ctx->ohci);
  759. for (i = 0; i < 10; i++) {
  760. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  761. if ((reg & CONTEXT_ACTIVE) == 0)
  762. return;
  763. mdelay(1);
  764. }
  765. fw_error("Error: DMA context still active (0x%08x)\n", reg);
  766. }
  767. struct driver_data {
  768. struct fw_packet *packet;
  769. };
  770. /*
  771. * This function apppends a packet to the DMA queue for transmission.
  772. * Must always be called with the ochi->lock held to ensure proper
  773. * generation handling and locking around packet queue manipulation.
  774. */
  775. static int at_context_queue_packet(struct context *ctx,
  776. struct fw_packet *packet)
  777. {
  778. struct fw_ohci *ohci = ctx->ohci;
  779. dma_addr_t d_bus, uninitialized_var(payload_bus);
  780. struct driver_data *driver_data;
  781. struct descriptor *d, *last;
  782. __le32 *header;
  783. int z, tcode;
  784. u32 reg;
  785. d = context_get_descriptors(ctx, 4, &d_bus);
  786. if (d == NULL) {
  787. packet->ack = RCODE_SEND_ERROR;
  788. return -1;
  789. }
  790. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  791. d[0].res_count = cpu_to_le16(packet->timestamp);
  792. /*
  793. * The DMA format for asyncronous link packets is different
  794. * from the IEEE1394 layout, so shift the fields around
  795. * accordingly. If header_length is 8, it's a PHY packet, to
  796. * which we need to prepend an extra quadlet.
  797. */
  798. header = (__le32 *) &d[1];
  799. switch (packet->header_length) {
  800. case 16:
  801. case 12:
  802. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  803. (packet->speed << 16));
  804. header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  805. (packet->header[0] & 0xffff0000));
  806. header[2] = cpu_to_le32(packet->header[2]);
  807. tcode = (packet->header[0] >> 4) & 0x0f;
  808. if (TCODE_IS_BLOCK_PACKET(tcode))
  809. header[3] = cpu_to_le32(packet->header[3]);
  810. else
  811. header[3] = (__force __le32) packet->header[3];
  812. d[0].req_count = cpu_to_le16(packet->header_length);
  813. break;
  814. case 8:
  815. header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
  816. (packet->speed << 16));
  817. header[1] = cpu_to_le32(packet->header[0]);
  818. header[2] = cpu_to_le32(packet->header[1]);
  819. d[0].req_count = cpu_to_le16(12);
  820. break;
  821. case 4:
  822. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  823. (packet->speed << 16));
  824. header[1] = cpu_to_le32(packet->header[0] & 0xffff0000);
  825. d[0].req_count = cpu_to_le16(8);
  826. break;
  827. default:
  828. /* BUG(); */
  829. packet->ack = RCODE_SEND_ERROR;
  830. return -1;
  831. }
  832. driver_data = (struct driver_data *) &d[3];
  833. driver_data->packet = packet;
  834. packet->driver_data = driver_data;
  835. if (packet->payload_length > 0) {
  836. payload_bus =
  837. dma_map_single(ohci->card.device, packet->payload,
  838. packet->payload_length, DMA_TO_DEVICE);
  839. if (dma_mapping_error(ohci->card.device, payload_bus)) {
  840. packet->ack = RCODE_SEND_ERROR;
  841. return -1;
  842. }
  843. packet->payload_bus = payload_bus;
  844. d[2].req_count = cpu_to_le16(packet->payload_length);
  845. d[2].data_address = cpu_to_le32(payload_bus);
  846. last = &d[2];
  847. z = 3;
  848. } else {
  849. last = &d[0];
  850. z = 2;
  851. }
  852. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  853. DESCRIPTOR_IRQ_ALWAYS |
  854. DESCRIPTOR_BRANCH_ALWAYS);
  855. /*
  856. * If the controller and packet generations don't match, we need to
  857. * bail out and try again. If IntEvent.busReset is set, the AT context
  858. * is halted, so appending to the context and trying to run it is
  859. * futile. Most controllers do the right thing and just flush the AT
  860. * queue (per section 7.2.3.2 of the OHCI 1.1 specification), but
  861. * some controllers (like a JMicron JMB381 PCI-e) misbehave and wind
  862. * up stalling out. So we just bail out in software and try again
  863. * later, and everyone is happy.
  864. * FIXME: Document how the locking works.
  865. */
  866. if (ohci->generation != packet->generation ||
  867. reg_read(ohci, OHCI1394_IntEventSet) & OHCI1394_busReset) {
  868. if (packet->payload_length > 0)
  869. dma_unmap_single(ohci->card.device, payload_bus,
  870. packet->payload_length, DMA_TO_DEVICE);
  871. packet->ack = RCODE_GENERATION;
  872. return -1;
  873. }
  874. context_append(ctx, d, z, 4 - z);
  875. /* If the context isn't already running, start it up. */
  876. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  877. if ((reg & CONTEXT_RUN) == 0)
  878. context_run(ctx, 0);
  879. return 0;
  880. }
  881. static int handle_at_packet(struct context *context,
  882. struct descriptor *d,
  883. struct descriptor *last)
  884. {
  885. struct driver_data *driver_data;
  886. struct fw_packet *packet;
  887. struct fw_ohci *ohci = context->ohci;
  888. int evt;
  889. if (last->transfer_status == 0)
  890. /* This descriptor isn't done yet, stop iteration. */
  891. return 0;
  892. driver_data = (struct driver_data *) &d[3];
  893. packet = driver_data->packet;
  894. if (packet == NULL)
  895. /* This packet was cancelled, just continue. */
  896. return 1;
  897. if (packet->payload_bus)
  898. dma_unmap_single(ohci->card.device, packet->payload_bus,
  899. packet->payload_length, DMA_TO_DEVICE);
  900. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  901. packet->timestamp = le16_to_cpu(last->res_count);
  902. log_ar_at_event('T', packet->speed, packet->header, evt);
  903. switch (evt) {
  904. case OHCI1394_evt_timeout:
  905. /* Async response transmit timed out. */
  906. packet->ack = RCODE_CANCELLED;
  907. break;
  908. case OHCI1394_evt_flushed:
  909. /*
  910. * The packet was flushed should give same error as
  911. * when we try to use a stale generation count.
  912. */
  913. packet->ack = RCODE_GENERATION;
  914. break;
  915. case OHCI1394_evt_missing_ack:
  916. /*
  917. * Using a valid (current) generation count, but the
  918. * node is not on the bus or not sending acks.
  919. */
  920. packet->ack = RCODE_NO_ACK;
  921. break;
  922. case ACK_COMPLETE + 0x10:
  923. case ACK_PENDING + 0x10:
  924. case ACK_BUSY_X + 0x10:
  925. case ACK_BUSY_A + 0x10:
  926. case ACK_BUSY_B + 0x10:
  927. case ACK_DATA_ERROR + 0x10:
  928. case ACK_TYPE_ERROR + 0x10:
  929. packet->ack = evt - 0x10;
  930. break;
  931. default:
  932. packet->ack = RCODE_SEND_ERROR;
  933. break;
  934. }
  935. packet->callback(packet, &ohci->card, packet->ack);
  936. return 1;
  937. }
  938. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  939. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  940. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  941. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  942. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  943. static void handle_local_rom(struct fw_ohci *ohci,
  944. struct fw_packet *packet, u32 csr)
  945. {
  946. struct fw_packet response;
  947. int tcode, length, i;
  948. tcode = HEADER_GET_TCODE(packet->header[0]);
  949. if (TCODE_IS_BLOCK_PACKET(tcode))
  950. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  951. else
  952. length = 4;
  953. i = csr - CSR_CONFIG_ROM;
  954. if (i + length > CONFIG_ROM_SIZE) {
  955. fw_fill_response(&response, packet->header,
  956. RCODE_ADDRESS_ERROR, NULL, 0);
  957. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  958. fw_fill_response(&response, packet->header,
  959. RCODE_TYPE_ERROR, NULL, 0);
  960. } else {
  961. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  962. (void *) ohci->config_rom + i, length);
  963. }
  964. fw_core_handle_response(&ohci->card, &response);
  965. }
  966. static void handle_local_lock(struct fw_ohci *ohci,
  967. struct fw_packet *packet, u32 csr)
  968. {
  969. struct fw_packet response;
  970. int tcode, length, ext_tcode, sel;
  971. __be32 *payload, lock_old;
  972. u32 lock_arg, lock_data;
  973. tcode = HEADER_GET_TCODE(packet->header[0]);
  974. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  975. payload = packet->payload;
  976. ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
  977. if (tcode == TCODE_LOCK_REQUEST &&
  978. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  979. lock_arg = be32_to_cpu(payload[0]);
  980. lock_data = be32_to_cpu(payload[1]);
  981. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  982. lock_arg = 0;
  983. lock_data = 0;
  984. } else {
  985. fw_fill_response(&response, packet->header,
  986. RCODE_TYPE_ERROR, NULL, 0);
  987. goto out;
  988. }
  989. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  990. reg_write(ohci, OHCI1394_CSRData, lock_data);
  991. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  992. reg_write(ohci, OHCI1394_CSRControl, sel);
  993. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  994. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  995. else
  996. fw_notify("swap not done yet\n");
  997. fw_fill_response(&response, packet->header,
  998. RCODE_COMPLETE, &lock_old, sizeof(lock_old));
  999. out:
  1000. fw_core_handle_response(&ohci->card, &response);
  1001. }
  1002. static void handle_local_request(struct context *ctx, struct fw_packet *packet)
  1003. {
  1004. u64 offset;
  1005. u32 csr;
  1006. if (ctx == &ctx->ohci->at_request_ctx) {
  1007. packet->ack = ACK_PENDING;
  1008. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1009. }
  1010. offset =
  1011. ((unsigned long long)
  1012. HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
  1013. packet->header[2];
  1014. csr = offset - CSR_REGISTER_BASE;
  1015. /* Handle config rom reads. */
  1016. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  1017. handle_local_rom(ctx->ohci, packet, csr);
  1018. else switch (csr) {
  1019. case CSR_BUS_MANAGER_ID:
  1020. case CSR_BANDWIDTH_AVAILABLE:
  1021. case CSR_CHANNELS_AVAILABLE_HI:
  1022. case CSR_CHANNELS_AVAILABLE_LO:
  1023. handle_local_lock(ctx->ohci, packet, csr);
  1024. break;
  1025. default:
  1026. if (ctx == &ctx->ohci->at_request_ctx)
  1027. fw_core_handle_request(&ctx->ohci->card, packet);
  1028. else
  1029. fw_core_handle_response(&ctx->ohci->card, packet);
  1030. break;
  1031. }
  1032. if (ctx == &ctx->ohci->at_response_ctx) {
  1033. packet->ack = ACK_COMPLETE;
  1034. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1035. }
  1036. }
  1037. static void at_context_transmit(struct context *ctx, struct fw_packet *packet)
  1038. {
  1039. unsigned long flags;
  1040. int ret;
  1041. spin_lock_irqsave(&ctx->ohci->lock, flags);
  1042. if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
  1043. ctx->ohci->generation == packet->generation) {
  1044. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1045. handle_local_request(ctx, packet);
  1046. return;
  1047. }
  1048. ret = at_context_queue_packet(ctx, packet);
  1049. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1050. if (ret < 0)
  1051. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1052. }
  1053. static void bus_reset_tasklet(unsigned long data)
  1054. {
  1055. struct fw_ohci *ohci = (struct fw_ohci *)data;
  1056. int self_id_count, i, j, reg;
  1057. int generation, new_generation;
  1058. unsigned long flags;
  1059. void *free_rom = NULL;
  1060. dma_addr_t free_rom_bus = 0;
  1061. reg = reg_read(ohci, OHCI1394_NodeID);
  1062. if (!(reg & OHCI1394_NodeID_idValid)) {
  1063. fw_notify("node ID not valid, new bus reset in progress\n");
  1064. return;
  1065. }
  1066. if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
  1067. fw_notify("malconfigured bus\n");
  1068. return;
  1069. }
  1070. ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
  1071. OHCI1394_NodeID_nodeNumber);
  1072. reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1073. if (reg & OHCI1394_SelfIDCount_selfIDError) {
  1074. fw_notify("inconsistent self IDs\n");
  1075. return;
  1076. }
  1077. /*
  1078. * The count in the SelfIDCount register is the number of
  1079. * bytes in the self ID receive buffer. Since we also receive
  1080. * the inverted quadlets and a header quadlet, we shift one
  1081. * bit extra to get the actual number of self IDs.
  1082. */
  1083. self_id_count = (reg >> 3) & 0x3ff;
  1084. if (self_id_count == 0) {
  1085. fw_notify("inconsistent self IDs\n");
  1086. return;
  1087. }
  1088. generation = (cond_le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  1089. rmb();
  1090. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  1091. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1]) {
  1092. fw_notify("inconsistent self IDs\n");
  1093. return;
  1094. }
  1095. ohci->self_id_buffer[j] =
  1096. cond_le32_to_cpu(ohci->self_id_cpu[i]);
  1097. }
  1098. rmb();
  1099. /*
  1100. * Check the consistency of the self IDs we just read. The
  1101. * problem we face is that a new bus reset can start while we
  1102. * read out the self IDs from the DMA buffer. If this happens,
  1103. * the DMA buffer will be overwritten with new self IDs and we
  1104. * will read out inconsistent data. The OHCI specification
  1105. * (section 11.2) recommends a technique similar to
  1106. * linux/seqlock.h, where we remember the generation of the
  1107. * self IDs in the buffer before reading them out and compare
  1108. * it to the current generation after reading them out. If
  1109. * the two generations match we know we have a consistent set
  1110. * of self IDs.
  1111. */
  1112. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  1113. if (new_generation != generation) {
  1114. fw_notify("recursive bus reset detected, "
  1115. "discarding self ids\n");
  1116. return;
  1117. }
  1118. /* FIXME: Document how the locking works. */
  1119. spin_lock_irqsave(&ohci->lock, flags);
  1120. ohci->generation = generation;
  1121. context_stop(&ohci->at_request_ctx);
  1122. context_stop(&ohci->at_response_ctx);
  1123. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  1124. if (ohci->bus_reset_packet_quirk)
  1125. ohci->request_generation = generation;
  1126. /*
  1127. * This next bit is unrelated to the AT context stuff but we
  1128. * have to do it under the spinlock also. If a new config rom
  1129. * was set up before this reset, the old one is now no longer
  1130. * in use and we can free it. Update the config rom pointers
  1131. * to point to the current config rom and clear the
  1132. * next_config_rom pointer so a new udpate can take place.
  1133. */
  1134. if (ohci->next_config_rom != NULL) {
  1135. if (ohci->next_config_rom != ohci->config_rom) {
  1136. free_rom = ohci->config_rom;
  1137. free_rom_bus = ohci->config_rom_bus;
  1138. }
  1139. ohci->config_rom = ohci->next_config_rom;
  1140. ohci->config_rom_bus = ohci->next_config_rom_bus;
  1141. ohci->next_config_rom = NULL;
  1142. /*
  1143. * Restore config_rom image and manually update
  1144. * config_rom registers. Writing the header quadlet
  1145. * will indicate that the config rom is ready, so we
  1146. * do that last.
  1147. */
  1148. reg_write(ohci, OHCI1394_BusOptions,
  1149. be32_to_cpu(ohci->config_rom[2]));
  1150. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  1151. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  1152. }
  1153. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1154. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
  1155. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
  1156. #endif
  1157. spin_unlock_irqrestore(&ohci->lock, flags);
  1158. if (free_rom)
  1159. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1160. free_rom, free_rom_bus);
  1161. log_selfids(ohci->node_id, generation,
  1162. self_id_count, ohci->self_id_buffer);
  1163. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  1164. self_id_count, ohci->self_id_buffer);
  1165. }
  1166. static irqreturn_t irq_handler(int irq, void *data)
  1167. {
  1168. struct fw_ohci *ohci = data;
  1169. u32 event, iso_event, cycle_time;
  1170. int i;
  1171. event = reg_read(ohci, OHCI1394_IntEventClear);
  1172. if (!event || !~event)
  1173. return IRQ_NONE;
  1174. /* busReset must not be cleared yet, see OHCI 1.1 clause 7.2.3.2 */
  1175. reg_write(ohci, OHCI1394_IntEventClear, event & ~OHCI1394_busReset);
  1176. log_irqs(event);
  1177. if (event & OHCI1394_selfIDComplete)
  1178. tasklet_schedule(&ohci->bus_reset_tasklet);
  1179. if (event & OHCI1394_RQPkt)
  1180. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  1181. if (event & OHCI1394_RSPkt)
  1182. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  1183. if (event & OHCI1394_reqTxComplete)
  1184. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  1185. if (event & OHCI1394_respTxComplete)
  1186. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  1187. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  1188. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  1189. while (iso_event) {
  1190. i = ffs(iso_event) - 1;
  1191. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  1192. iso_event &= ~(1 << i);
  1193. }
  1194. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  1195. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  1196. while (iso_event) {
  1197. i = ffs(iso_event) - 1;
  1198. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  1199. iso_event &= ~(1 << i);
  1200. }
  1201. if (unlikely(event & OHCI1394_regAccessFail))
  1202. fw_error("Register access failure - "
  1203. "please notify linux1394-devel@lists.sf.net\n");
  1204. if (unlikely(event & OHCI1394_postedWriteErr))
  1205. fw_error("PCI posted write error\n");
  1206. if (unlikely(event & OHCI1394_cycleTooLong)) {
  1207. if (printk_ratelimit())
  1208. fw_notify("isochronous cycle too long\n");
  1209. reg_write(ohci, OHCI1394_LinkControlSet,
  1210. OHCI1394_LinkControl_cycleMaster);
  1211. }
  1212. if (event & OHCI1394_cycle64Seconds) {
  1213. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1214. if ((cycle_time & 0x80000000) == 0)
  1215. atomic_inc(&ohci->bus_seconds);
  1216. }
  1217. return IRQ_HANDLED;
  1218. }
  1219. static int software_reset(struct fw_ohci *ohci)
  1220. {
  1221. int i;
  1222. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1223. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1224. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1225. OHCI1394_HCControl_softReset) == 0)
  1226. return 0;
  1227. msleep(1);
  1228. }
  1229. return -EBUSY;
  1230. }
  1231. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  1232. {
  1233. struct fw_ohci *ohci = fw_ohci(card);
  1234. struct pci_dev *dev = to_pci_dev(card->device);
  1235. u32 lps;
  1236. int i;
  1237. if (software_reset(ohci)) {
  1238. fw_error("Failed to reset ohci card.\n");
  1239. return -EBUSY;
  1240. }
  1241. /*
  1242. * Now enable LPS, which we need in order to start accessing
  1243. * most of the registers. In fact, on some cards (ALI M5251),
  1244. * accessing registers in the SClk domain without LPS enabled
  1245. * will lock up the machine. Wait 50msec to make sure we have
  1246. * full link enabled. However, with some cards (well, at least
  1247. * a JMicron PCIe card), we have to try again sometimes.
  1248. */
  1249. reg_write(ohci, OHCI1394_HCControlSet,
  1250. OHCI1394_HCControl_LPS |
  1251. OHCI1394_HCControl_postedWriteEnable);
  1252. flush_writes(ohci);
  1253. for (lps = 0, i = 0; !lps && i < 3; i++) {
  1254. msleep(50);
  1255. lps = reg_read(ohci, OHCI1394_HCControlSet) &
  1256. OHCI1394_HCControl_LPS;
  1257. }
  1258. if (!lps) {
  1259. fw_error("Failed to set Link Power Status\n");
  1260. return -EIO;
  1261. }
  1262. reg_write(ohci, OHCI1394_HCControlClear,
  1263. OHCI1394_HCControl_noByteSwapData);
  1264. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1265. reg_write(ohci, OHCI1394_LinkControlClear,
  1266. OHCI1394_LinkControl_rcvPhyPkt);
  1267. reg_write(ohci, OHCI1394_LinkControlSet,
  1268. OHCI1394_LinkControl_rcvSelfID |
  1269. OHCI1394_LinkControl_cycleTimerEnable |
  1270. OHCI1394_LinkControl_cycleMaster);
  1271. reg_write(ohci, OHCI1394_ATRetries,
  1272. OHCI1394_MAX_AT_REQ_RETRIES |
  1273. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1274. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1275. ar_context_run(&ohci->ar_request_ctx);
  1276. ar_context_run(&ohci->ar_response_ctx);
  1277. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1278. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1279. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1280. reg_write(ohci, OHCI1394_IntMaskSet,
  1281. OHCI1394_selfIDComplete |
  1282. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1283. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1284. OHCI1394_isochRx | OHCI1394_isochTx |
  1285. OHCI1394_postedWriteErr | OHCI1394_cycleTooLong |
  1286. OHCI1394_cycle64Seconds | OHCI1394_regAccessFail |
  1287. OHCI1394_masterIntEnable);
  1288. if (param_debug & OHCI_PARAM_DEBUG_BUSRESETS)
  1289. reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset);
  1290. /* Activate link_on bit and contender bit in our self ID packets.*/
  1291. if (ohci_update_phy_reg(card, 4, 0,
  1292. PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
  1293. return -EIO;
  1294. /*
  1295. * When the link is not yet enabled, the atomic config rom
  1296. * update mechanism described below in ohci_set_config_rom()
  1297. * is not active. We have to update ConfigRomHeader and
  1298. * BusOptions manually, and the write to ConfigROMmap takes
  1299. * effect immediately. We tie this to the enabling of the
  1300. * link, so we have a valid config rom before enabling - the
  1301. * OHCI requires that ConfigROMhdr and BusOptions have valid
  1302. * values before enabling.
  1303. *
  1304. * However, when the ConfigROMmap is written, some controllers
  1305. * always read back quadlets 0 and 2 from the config rom to
  1306. * the ConfigRomHeader and BusOptions registers on bus reset.
  1307. * They shouldn't do that in this initial case where the link
  1308. * isn't enabled. This means we have to use the same
  1309. * workaround here, setting the bus header to 0 and then write
  1310. * the right values in the bus reset tasklet.
  1311. */
  1312. if (config_rom) {
  1313. ohci->next_config_rom =
  1314. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1315. &ohci->next_config_rom_bus,
  1316. GFP_KERNEL);
  1317. if (ohci->next_config_rom == NULL)
  1318. return -ENOMEM;
  1319. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1320. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  1321. } else {
  1322. /*
  1323. * In the suspend case, config_rom is NULL, which
  1324. * means that we just reuse the old config rom.
  1325. */
  1326. ohci->next_config_rom = ohci->config_rom;
  1327. ohci->next_config_rom_bus = ohci->config_rom_bus;
  1328. }
  1329. ohci->next_header = be32_to_cpu(ohci->next_config_rom[0]);
  1330. ohci->next_config_rom[0] = 0;
  1331. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  1332. reg_write(ohci, OHCI1394_BusOptions,
  1333. be32_to_cpu(ohci->next_config_rom[2]));
  1334. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  1335. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  1336. if (request_irq(dev->irq, irq_handler,
  1337. IRQF_SHARED, ohci_driver_name, ohci)) {
  1338. fw_error("Failed to allocate shared interrupt %d.\n",
  1339. dev->irq);
  1340. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1341. ohci->config_rom, ohci->config_rom_bus);
  1342. return -EIO;
  1343. }
  1344. reg_write(ohci, OHCI1394_HCControlSet,
  1345. OHCI1394_HCControl_linkEnable |
  1346. OHCI1394_HCControl_BIBimageValid);
  1347. flush_writes(ohci);
  1348. /*
  1349. * We are ready to go, initiate bus reset to finish the
  1350. * initialization.
  1351. */
  1352. fw_core_initiate_bus_reset(&ohci->card, 1);
  1353. return 0;
  1354. }
  1355. static int ohci_set_config_rom(struct fw_card *card,
  1356. u32 *config_rom, size_t length)
  1357. {
  1358. struct fw_ohci *ohci;
  1359. unsigned long flags;
  1360. int ret = -EBUSY;
  1361. __be32 *next_config_rom;
  1362. dma_addr_t uninitialized_var(next_config_rom_bus);
  1363. ohci = fw_ohci(card);
  1364. /*
  1365. * When the OHCI controller is enabled, the config rom update
  1366. * mechanism is a bit tricky, but easy enough to use. See
  1367. * section 5.5.6 in the OHCI specification.
  1368. *
  1369. * The OHCI controller caches the new config rom address in a
  1370. * shadow register (ConfigROMmapNext) and needs a bus reset
  1371. * for the changes to take place. When the bus reset is
  1372. * detected, the controller loads the new values for the
  1373. * ConfigRomHeader and BusOptions registers from the specified
  1374. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  1375. * shadow register. All automatically and atomically.
  1376. *
  1377. * Now, there's a twist to this story. The automatic load of
  1378. * ConfigRomHeader and BusOptions doesn't honor the
  1379. * noByteSwapData bit, so with a be32 config rom, the
  1380. * controller will load be32 values in to these registers
  1381. * during the atomic update, even on litte endian
  1382. * architectures. The workaround we use is to put a 0 in the
  1383. * header quadlet; 0 is endian agnostic and means that the
  1384. * config rom isn't ready yet. In the bus reset tasklet we
  1385. * then set up the real values for the two registers.
  1386. *
  1387. * We use ohci->lock to avoid racing with the code that sets
  1388. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  1389. */
  1390. next_config_rom =
  1391. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1392. &next_config_rom_bus, GFP_KERNEL);
  1393. if (next_config_rom == NULL)
  1394. return -ENOMEM;
  1395. spin_lock_irqsave(&ohci->lock, flags);
  1396. if (ohci->next_config_rom == NULL) {
  1397. ohci->next_config_rom = next_config_rom;
  1398. ohci->next_config_rom_bus = next_config_rom_bus;
  1399. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1400. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  1401. length * 4);
  1402. ohci->next_header = config_rom[0];
  1403. ohci->next_config_rom[0] = 0;
  1404. reg_write(ohci, OHCI1394_ConfigROMmap,
  1405. ohci->next_config_rom_bus);
  1406. ret = 0;
  1407. }
  1408. spin_unlock_irqrestore(&ohci->lock, flags);
  1409. /*
  1410. * Now initiate a bus reset to have the changes take
  1411. * effect. We clean up the old config rom memory and DMA
  1412. * mappings in the bus reset tasklet, since the OHCI
  1413. * controller could need to access it before the bus reset
  1414. * takes effect.
  1415. */
  1416. if (ret == 0)
  1417. fw_core_initiate_bus_reset(&ohci->card, 1);
  1418. else
  1419. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1420. next_config_rom, next_config_rom_bus);
  1421. return ret;
  1422. }
  1423. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  1424. {
  1425. struct fw_ohci *ohci = fw_ohci(card);
  1426. at_context_transmit(&ohci->at_request_ctx, packet);
  1427. }
  1428. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  1429. {
  1430. struct fw_ohci *ohci = fw_ohci(card);
  1431. at_context_transmit(&ohci->at_response_ctx, packet);
  1432. }
  1433. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  1434. {
  1435. struct fw_ohci *ohci = fw_ohci(card);
  1436. struct context *ctx = &ohci->at_request_ctx;
  1437. struct driver_data *driver_data = packet->driver_data;
  1438. int ret = -ENOENT;
  1439. tasklet_disable(&ctx->tasklet);
  1440. if (packet->ack != 0)
  1441. goto out;
  1442. if (packet->payload_bus)
  1443. dma_unmap_single(ohci->card.device, packet->payload_bus,
  1444. packet->payload_length, DMA_TO_DEVICE);
  1445. log_ar_at_event('T', packet->speed, packet->header, 0x20);
  1446. driver_data->packet = NULL;
  1447. packet->ack = RCODE_CANCELLED;
  1448. packet->callback(packet, &ohci->card, packet->ack);
  1449. ret = 0;
  1450. out:
  1451. tasklet_enable(&ctx->tasklet);
  1452. return ret;
  1453. }
  1454. static int ohci_enable_phys_dma(struct fw_card *card,
  1455. int node_id, int generation)
  1456. {
  1457. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1458. return 0;
  1459. #else
  1460. struct fw_ohci *ohci = fw_ohci(card);
  1461. unsigned long flags;
  1462. int n, ret = 0;
  1463. /*
  1464. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  1465. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  1466. */
  1467. spin_lock_irqsave(&ohci->lock, flags);
  1468. if (ohci->generation != generation) {
  1469. ret = -ESTALE;
  1470. goto out;
  1471. }
  1472. /*
  1473. * Note, if the node ID contains a non-local bus ID, physical DMA is
  1474. * enabled for _all_ nodes on remote buses.
  1475. */
  1476. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1477. if (n < 32)
  1478. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1479. else
  1480. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1481. flush_writes(ohci);
  1482. out:
  1483. spin_unlock_irqrestore(&ohci->lock, flags);
  1484. return ret;
  1485. #endif /* CONFIG_FIREWIRE_OHCI_REMOTE_DMA */
  1486. }
  1487. static u64 ohci_get_bus_time(struct fw_card *card)
  1488. {
  1489. struct fw_ohci *ohci = fw_ohci(card);
  1490. u32 cycle_time;
  1491. u64 bus_time;
  1492. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1493. bus_time = ((u64)atomic_read(&ohci->bus_seconds) << 32) | cycle_time;
  1494. return bus_time;
  1495. }
  1496. static void copy_iso_headers(struct iso_context *ctx, void *p)
  1497. {
  1498. int i = ctx->header_length;
  1499. if (i + ctx->base.header_size > PAGE_SIZE)
  1500. return;
  1501. /*
  1502. * The iso header is byteswapped to little endian by
  1503. * the controller, but the remaining header quadlets
  1504. * are big endian. We want to present all the headers
  1505. * as big endian, so we have to swap the first quadlet.
  1506. */
  1507. if (ctx->base.header_size > 0)
  1508. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1509. if (ctx->base.header_size > 4)
  1510. *(u32 *) (ctx->header + i + 4) = __swab32(*(u32 *) p);
  1511. if (ctx->base.header_size > 8)
  1512. memcpy(ctx->header + i + 8, p + 8, ctx->base.header_size - 8);
  1513. ctx->header_length += ctx->base.header_size;
  1514. }
  1515. static int handle_ir_dualbuffer_packet(struct context *context,
  1516. struct descriptor *d,
  1517. struct descriptor *last)
  1518. {
  1519. struct iso_context *ctx =
  1520. container_of(context, struct iso_context, context);
  1521. struct db_descriptor *db = (struct db_descriptor *) d;
  1522. __le32 *ir_header;
  1523. size_t header_length;
  1524. void *p, *end;
  1525. if (db->first_res_count != 0 && db->second_res_count != 0) {
  1526. if (ctx->excess_bytes <= le16_to_cpu(db->second_req_count)) {
  1527. /* This descriptor isn't done yet, stop iteration. */
  1528. return 0;
  1529. }
  1530. ctx->excess_bytes -= le16_to_cpu(db->second_req_count);
  1531. }
  1532. header_length = le16_to_cpu(db->first_req_count) -
  1533. le16_to_cpu(db->first_res_count);
  1534. p = db + 1;
  1535. end = p + header_length;
  1536. while (p < end) {
  1537. copy_iso_headers(ctx, p);
  1538. ctx->excess_bytes +=
  1539. (le32_to_cpu(*(__le32 *)(p + 4)) >> 16) & 0xffff;
  1540. p += max(ctx->base.header_size, (size_t)8);
  1541. }
  1542. ctx->excess_bytes -= le16_to_cpu(db->second_req_count) -
  1543. le16_to_cpu(db->second_res_count);
  1544. if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1545. ir_header = (__le32 *) (db + 1);
  1546. ctx->base.callback(&ctx->base,
  1547. le32_to_cpu(ir_header[0]) & 0xffff,
  1548. ctx->header_length, ctx->header,
  1549. ctx->base.callback_data);
  1550. ctx->header_length = 0;
  1551. }
  1552. return 1;
  1553. }
  1554. static int handle_ir_packet_per_buffer(struct context *context,
  1555. struct descriptor *d,
  1556. struct descriptor *last)
  1557. {
  1558. struct iso_context *ctx =
  1559. container_of(context, struct iso_context, context);
  1560. struct descriptor *pd;
  1561. __le32 *ir_header;
  1562. void *p;
  1563. for (pd = d; pd <= last; pd++) {
  1564. if (pd->transfer_status)
  1565. break;
  1566. }
  1567. if (pd > last)
  1568. /* Descriptor(s) not done yet, stop iteration */
  1569. return 0;
  1570. p = last + 1;
  1571. copy_iso_headers(ctx, p);
  1572. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1573. ir_header = (__le32 *) p;
  1574. ctx->base.callback(&ctx->base,
  1575. le32_to_cpu(ir_header[0]) & 0xffff,
  1576. ctx->header_length, ctx->header,
  1577. ctx->base.callback_data);
  1578. ctx->header_length = 0;
  1579. }
  1580. return 1;
  1581. }
  1582. static int handle_it_packet(struct context *context,
  1583. struct descriptor *d,
  1584. struct descriptor *last)
  1585. {
  1586. struct iso_context *ctx =
  1587. container_of(context, struct iso_context, context);
  1588. if (last->transfer_status == 0)
  1589. /* This descriptor isn't done yet, stop iteration. */
  1590. return 0;
  1591. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
  1592. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1593. 0, NULL, ctx->base.callback_data);
  1594. return 1;
  1595. }
  1596. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  1597. int type, int channel, size_t header_size)
  1598. {
  1599. struct fw_ohci *ohci = fw_ohci(card);
  1600. struct iso_context *ctx, *list;
  1601. descriptor_callback_t callback;
  1602. u64 *channels, dont_care = ~0ULL;
  1603. u32 *mask, regs;
  1604. unsigned long flags;
  1605. int index, ret = -ENOMEM;
  1606. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1607. channels = &dont_care;
  1608. mask = &ohci->it_context_mask;
  1609. list = ohci->it_context_list;
  1610. callback = handle_it_packet;
  1611. } else {
  1612. channels = &ohci->ir_context_channels;
  1613. mask = &ohci->ir_context_mask;
  1614. list = ohci->ir_context_list;
  1615. if (ohci->use_dualbuffer)
  1616. callback = handle_ir_dualbuffer_packet;
  1617. else
  1618. callback = handle_ir_packet_per_buffer;
  1619. }
  1620. spin_lock_irqsave(&ohci->lock, flags);
  1621. index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
  1622. if (index >= 0) {
  1623. *channels &= ~(1ULL << channel);
  1624. *mask &= ~(1 << index);
  1625. }
  1626. spin_unlock_irqrestore(&ohci->lock, flags);
  1627. if (index < 0)
  1628. return ERR_PTR(-EBUSY);
  1629. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1630. regs = OHCI1394_IsoXmitContextBase(index);
  1631. else
  1632. regs = OHCI1394_IsoRcvContextBase(index);
  1633. ctx = &list[index];
  1634. memset(ctx, 0, sizeof(*ctx));
  1635. ctx->header_length = 0;
  1636. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1637. if (ctx->header == NULL)
  1638. goto out;
  1639. ret = context_init(&ctx->context, ohci, regs, callback);
  1640. if (ret < 0)
  1641. goto out_with_header;
  1642. return &ctx->base;
  1643. out_with_header:
  1644. free_page((unsigned long)ctx->header);
  1645. out:
  1646. spin_lock_irqsave(&ohci->lock, flags);
  1647. *mask |= 1 << index;
  1648. spin_unlock_irqrestore(&ohci->lock, flags);
  1649. return ERR_PTR(ret);
  1650. }
  1651. static int ohci_start_iso(struct fw_iso_context *base,
  1652. s32 cycle, u32 sync, u32 tags)
  1653. {
  1654. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1655. struct fw_ohci *ohci = ctx->context.ohci;
  1656. u32 control, match;
  1657. int index;
  1658. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1659. index = ctx - ohci->it_context_list;
  1660. match = 0;
  1661. if (cycle >= 0)
  1662. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1663. (cycle & 0x7fff) << 16;
  1664. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1665. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1666. context_run(&ctx->context, match);
  1667. } else {
  1668. index = ctx - ohci->ir_context_list;
  1669. control = IR_CONTEXT_ISOCH_HEADER;
  1670. if (ohci->use_dualbuffer)
  1671. control |= IR_CONTEXT_DUAL_BUFFER_MODE;
  1672. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  1673. if (cycle >= 0) {
  1674. match |= (cycle & 0x07fff) << 12;
  1675. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  1676. }
  1677. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1678. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1679. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  1680. context_run(&ctx->context, control);
  1681. }
  1682. return 0;
  1683. }
  1684. static int ohci_stop_iso(struct fw_iso_context *base)
  1685. {
  1686. struct fw_ohci *ohci = fw_ohci(base->card);
  1687. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1688. int index;
  1689. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1690. index = ctx - ohci->it_context_list;
  1691. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1692. } else {
  1693. index = ctx - ohci->ir_context_list;
  1694. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1695. }
  1696. flush_writes(ohci);
  1697. context_stop(&ctx->context);
  1698. return 0;
  1699. }
  1700. static void ohci_free_iso_context(struct fw_iso_context *base)
  1701. {
  1702. struct fw_ohci *ohci = fw_ohci(base->card);
  1703. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1704. unsigned long flags;
  1705. int index;
  1706. ohci_stop_iso(base);
  1707. context_release(&ctx->context);
  1708. free_page((unsigned long)ctx->header);
  1709. spin_lock_irqsave(&ohci->lock, flags);
  1710. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1711. index = ctx - ohci->it_context_list;
  1712. ohci->it_context_mask |= 1 << index;
  1713. } else {
  1714. index = ctx - ohci->ir_context_list;
  1715. ohci->ir_context_mask |= 1 << index;
  1716. ohci->ir_context_channels |= 1ULL << base->channel;
  1717. }
  1718. spin_unlock_irqrestore(&ohci->lock, flags);
  1719. }
  1720. static int ohci_queue_iso_transmit(struct fw_iso_context *base,
  1721. struct fw_iso_packet *packet,
  1722. struct fw_iso_buffer *buffer,
  1723. unsigned long payload)
  1724. {
  1725. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1726. struct descriptor *d, *last, *pd;
  1727. struct fw_iso_packet *p;
  1728. __le32 *header;
  1729. dma_addr_t d_bus, page_bus;
  1730. u32 z, header_z, payload_z, irq;
  1731. u32 payload_index, payload_end_index, next_page_index;
  1732. int page, end_page, i, length, offset;
  1733. /*
  1734. * FIXME: Cycle lost behavior should be configurable: lose
  1735. * packet, retransmit or terminate..
  1736. */
  1737. p = packet;
  1738. payload_index = payload;
  1739. if (p->skip)
  1740. z = 1;
  1741. else
  1742. z = 2;
  1743. if (p->header_length > 0)
  1744. z++;
  1745. /* Determine the first page the payload isn't contained in. */
  1746. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1747. if (p->payload_length > 0)
  1748. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1749. else
  1750. payload_z = 0;
  1751. z += payload_z;
  1752. /* Get header size in number of descriptors. */
  1753. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  1754. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1755. if (d == NULL)
  1756. return -ENOMEM;
  1757. if (!p->skip) {
  1758. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1759. d[0].req_count = cpu_to_le16(8);
  1760. header = (__le32 *) &d[1];
  1761. header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
  1762. IT_HEADER_TAG(p->tag) |
  1763. IT_HEADER_TCODE(TCODE_STREAM_DATA) |
  1764. IT_HEADER_CHANNEL(ctx->base.channel) |
  1765. IT_HEADER_SPEED(ctx->base.speed));
  1766. header[1] =
  1767. cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
  1768. p->payload_length));
  1769. }
  1770. if (p->header_length > 0) {
  1771. d[2].req_count = cpu_to_le16(p->header_length);
  1772. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  1773. memcpy(&d[z], p->header, p->header_length);
  1774. }
  1775. pd = d + z - payload_z;
  1776. payload_end_index = payload_index + p->payload_length;
  1777. for (i = 0; i < payload_z; i++) {
  1778. page = payload_index >> PAGE_SHIFT;
  1779. offset = payload_index & ~PAGE_MASK;
  1780. next_page_index = (page + 1) << PAGE_SHIFT;
  1781. length =
  1782. min(next_page_index, payload_end_index) - payload_index;
  1783. pd[i].req_count = cpu_to_le16(length);
  1784. page_bus = page_private(buffer->pages[page]);
  1785. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1786. payload_index += length;
  1787. }
  1788. if (p->interrupt)
  1789. irq = DESCRIPTOR_IRQ_ALWAYS;
  1790. else
  1791. irq = DESCRIPTOR_NO_IRQ;
  1792. last = z == 2 ? d : d + z - 1;
  1793. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1794. DESCRIPTOR_STATUS |
  1795. DESCRIPTOR_BRANCH_ALWAYS |
  1796. irq);
  1797. context_append(&ctx->context, d, z, header_z);
  1798. return 0;
  1799. }
  1800. static int ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1801. struct fw_iso_packet *packet,
  1802. struct fw_iso_buffer *buffer,
  1803. unsigned long payload)
  1804. {
  1805. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1806. struct db_descriptor *db = NULL;
  1807. struct descriptor *d;
  1808. struct fw_iso_packet *p;
  1809. dma_addr_t d_bus, page_bus;
  1810. u32 z, header_z, length, rest;
  1811. int page, offset, packet_count, header_size;
  1812. /*
  1813. * FIXME: Cycle lost behavior should be configurable: lose
  1814. * packet, retransmit or terminate..
  1815. */
  1816. p = packet;
  1817. z = 2;
  1818. /*
  1819. * The OHCI controller puts the isochronous header and trailer in the
  1820. * buffer, so we need at least 8 bytes.
  1821. */
  1822. packet_count = p->header_length / ctx->base.header_size;
  1823. header_size = packet_count * max(ctx->base.header_size, (size_t)8);
  1824. /* Get header size in number of descriptors. */
  1825. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1826. page = payload >> PAGE_SHIFT;
  1827. offset = payload & ~PAGE_MASK;
  1828. rest = p->payload_length;
  1829. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1830. while (rest > 0) {
  1831. d = context_get_descriptors(&ctx->context,
  1832. z + header_z, &d_bus);
  1833. if (d == NULL)
  1834. return -ENOMEM;
  1835. db = (struct db_descriptor *) d;
  1836. db->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1837. DESCRIPTOR_BRANCH_ALWAYS);
  1838. db->first_size =
  1839. cpu_to_le16(max(ctx->base.header_size, (size_t)8));
  1840. if (p->skip && rest == p->payload_length) {
  1841. db->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1842. db->first_req_count = db->first_size;
  1843. } else {
  1844. db->first_req_count = cpu_to_le16(header_size);
  1845. }
  1846. db->first_res_count = db->first_req_count;
  1847. db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
  1848. if (p->skip && rest == p->payload_length)
  1849. length = 4;
  1850. else if (offset + rest < PAGE_SIZE)
  1851. length = rest;
  1852. else
  1853. length = PAGE_SIZE - offset;
  1854. db->second_req_count = cpu_to_le16(length);
  1855. db->second_res_count = db->second_req_count;
  1856. page_bus = page_private(buffer->pages[page]);
  1857. db->second_buffer = cpu_to_le32(page_bus + offset);
  1858. if (p->interrupt && length == rest)
  1859. db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1860. context_append(&ctx->context, d, z, header_z);
  1861. offset = (offset + length) & ~PAGE_MASK;
  1862. rest -= length;
  1863. if (offset == 0)
  1864. page++;
  1865. }
  1866. return 0;
  1867. }
  1868. static int ohci_queue_iso_receive_packet_per_buffer(struct fw_iso_context *base,
  1869. struct fw_iso_packet *packet,
  1870. struct fw_iso_buffer *buffer,
  1871. unsigned long payload)
  1872. {
  1873. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1874. struct descriptor *d = NULL, *pd = NULL;
  1875. struct fw_iso_packet *p = packet;
  1876. dma_addr_t d_bus, page_bus;
  1877. u32 z, header_z, rest;
  1878. int i, j, length;
  1879. int page, offset, packet_count, header_size, payload_per_buffer;
  1880. /*
  1881. * The OHCI controller puts the isochronous header and trailer in the
  1882. * buffer, so we need at least 8 bytes.
  1883. */
  1884. packet_count = p->header_length / ctx->base.header_size;
  1885. header_size = max(ctx->base.header_size, (size_t)8);
  1886. /* Get header size in number of descriptors. */
  1887. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1888. page = payload >> PAGE_SHIFT;
  1889. offset = payload & ~PAGE_MASK;
  1890. payload_per_buffer = p->payload_length / packet_count;
  1891. for (i = 0; i < packet_count; i++) {
  1892. /* d points to the header descriptor */
  1893. z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
  1894. d = context_get_descriptors(&ctx->context,
  1895. z + header_z, &d_bus);
  1896. if (d == NULL)
  1897. return -ENOMEM;
  1898. d->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1899. DESCRIPTOR_INPUT_MORE);
  1900. if (p->skip && i == 0)
  1901. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1902. d->req_count = cpu_to_le16(header_size);
  1903. d->res_count = d->req_count;
  1904. d->transfer_status = 0;
  1905. d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
  1906. rest = payload_per_buffer;
  1907. for (j = 1; j < z; j++) {
  1908. pd = d + j;
  1909. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1910. DESCRIPTOR_INPUT_MORE);
  1911. if (offset + rest < PAGE_SIZE)
  1912. length = rest;
  1913. else
  1914. length = PAGE_SIZE - offset;
  1915. pd->req_count = cpu_to_le16(length);
  1916. pd->res_count = pd->req_count;
  1917. pd->transfer_status = 0;
  1918. page_bus = page_private(buffer->pages[page]);
  1919. pd->data_address = cpu_to_le32(page_bus + offset);
  1920. offset = (offset + length) & ~PAGE_MASK;
  1921. rest -= length;
  1922. if (offset == 0)
  1923. page++;
  1924. }
  1925. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1926. DESCRIPTOR_INPUT_LAST |
  1927. DESCRIPTOR_BRANCH_ALWAYS);
  1928. if (p->interrupt && i == packet_count - 1)
  1929. pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1930. context_append(&ctx->context, d, z, header_z);
  1931. }
  1932. return 0;
  1933. }
  1934. static int ohci_queue_iso(struct fw_iso_context *base,
  1935. struct fw_iso_packet *packet,
  1936. struct fw_iso_buffer *buffer,
  1937. unsigned long payload)
  1938. {
  1939. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1940. unsigned long flags;
  1941. int ret;
  1942. spin_lock_irqsave(&ctx->context.ohci->lock, flags);
  1943. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1944. ret = ohci_queue_iso_transmit(base, packet, buffer, payload);
  1945. else if (ctx->context.ohci->use_dualbuffer)
  1946. ret = ohci_queue_iso_receive_dualbuffer(base, packet,
  1947. buffer, payload);
  1948. else
  1949. ret = ohci_queue_iso_receive_packet_per_buffer(base, packet,
  1950. buffer, payload);
  1951. spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
  1952. return ret;
  1953. }
  1954. static const struct fw_card_driver ohci_driver = {
  1955. .enable = ohci_enable,
  1956. .update_phy_reg = ohci_update_phy_reg,
  1957. .set_config_rom = ohci_set_config_rom,
  1958. .send_request = ohci_send_request,
  1959. .send_response = ohci_send_response,
  1960. .cancel_packet = ohci_cancel_packet,
  1961. .enable_phys_dma = ohci_enable_phys_dma,
  1962. .get_bus_time = ohci_get_bus_time,
  1963. .allocate_iso_context = ohci_allocate_iso_context,
  1964. .free_iso_context = ohci_free_iso_context,
  1965. .queue_iso = ohci_queue_iso,
  1966. .start_iso = ohci_start_iso,
  1967. .stop_iso = ohci_stop_iso,
  1968. };
  1969. #ifdef CONFIG_PPC_PMAC
  1970. static void ohci_pmac_on(struct pci_dev *dev)
  1971. {
  1972. if (machine_is(powermac)) {
  1973. struct device_node *ofn = pci_device_to_OF_node(dev);
  1974. if (ofn) {
  1975. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
  1976. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  1977. }
  1978. }
  1979. }
  1980. static void ohci_pmac_off(struct pci_dev *dev)
  1981. {
  1982. if (machine_is(powermac)) {
  1983. struct device_node *ofn = pci_device_to_OF_node(dev);
  1984. if (ofn) {
  1985. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  1986. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
  1987. }
  1988. }
  1989. }
  1990. #else
  1991. #define ohci_pmac_on(dev)
  1992. #define ohci_pmac_off(dev)
  1993. #endif /* CONFIG_PPC_PMAC */
  1994. static int __devinit pci_probe(struct pci_dev *dev,
  1995. const struct pci_device_id *ent)
  1996. {
  1997. struct fw_ohci *ohci;
  1998. u32 bus_options, max_receive, link_speed, version;
  1999. u64 guid;
  2000. int err;
  2001. size_t size;
  2002. ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
  2003. if (ohci == NULL) {
  2004. err = -ENOMEM;
  2005. goto fail;
  2006. }
  2007. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  2008. ohci_pmac_on(dev);
  2009. err = pci_enable_device(dev);
  2010. if (err) {
  2011. fw_error("Failed to enable OHCI hardware\n");
  2012. goto fail_free;
  2013. }
  2014. pci_set_master(dev);
  2015. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  2016. pci_set_drvdata(dev, ohci);
  2017. spin_lock_init(&ohci->lock);
  2018. tasklet_init(&ohci->bus_reset_tasklet,
  2019. bus_reset_tasklet, (unsigned long)ohci);
  2020. err = pci_request_region(dev, 0, ohci_driver_name);
  2021. if (err) {
  2022. fw_error("MMIO resource unavailable\n");
  2023. goto fail_disable;
  2024. }
  2025. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  2026. if (ohci->registers == NULL) {
  2027. fw_error("Failed to remap registers\n");
  2028. err = -ENXIO;
  2029. goto fail_iomem;
  2030. }
  2031. version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  2032. ohci->use_dualbuffer = version >= OHCI_VERSION_1_1;
  2033. /* x86-32 currently doesn't use highmem for dma_alloc_coherent */
  2034. #if !defined(CONFIG_X86_32)
  2035. /* dual-buffer mode is broken with descriptor addresses above 2G */
  2036. if (dev->vendor == PCI_VENDOR_ID_TI &&
  2037. dev->device == PCI_DEVICE_ID_TI_TSB43AB22)
  2038. ohci->use_dualbuffer = false;
  2039. #endif
  2040. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  2041. ohci->old_uninorth = dev->vendor == PCI_VENDOR_ID_APPLE &&
  2042. dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW;
  2043. #endif
  2044. ohci->bus_reset_packet_quirk = dev->vendor == PCI_VENDOR_ID_TI;
  2045. ar_context_init(&ohci->ar_request_ctx, ohci,
  2046. OHCI1394_AsReqRcvContextControlSet);
  2047. ar_context_init(&ohci->ar_response_ctx, ohci,
  2048. OHCI1394_AsRspRcvContextControlSet);
  2049. context_init(&ohci->at_request_ctx, ohci,
  2050. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  2051. context_init(&ohci->at_response_ctx, ohci,
  2052. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  2053. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  2054. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  2055. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  2056. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  2057. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  2058. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  2059. ohci->ir_context_channels = ~0ULL;
  2060. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  2061. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  2062. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  2063. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  2064. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  2065. err = -ENOMEM;
  2066. goto fail_contexts;
  2067. }
  2068. /* self-id dma buffer allocation */
  2069. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  2070. SELF_ID_BUF_SIZE,
  2071. &ohci->self_id_bus,
  2072. GFP_KERNEL);
  2073. if (ohci->self_id_cpu == NULL) {
  2074. err = -ENOMEM;
  2075. goto fail_contexts;
  2076. }
  2077. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  2078. max_receive = (bus_options >> 12) & 0xf;
  2079. link_speed = bus_options & 0x7;
  2080. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  2081. reg_read(ohci, OHCI1394_GUIDLo);
  2082. err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  2083. if (err)
  2084. goto fail_self_id;
  2085. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  2086. dev_name(&dev->dev), version >> 16, version & 0xff);
  2087. return 0;
  2088. fail_self_id:
  2089. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  2090. ohci->self_id_cpu, ohci->self_id_bus);
  2091. fail_contexts:
  2092. kfree(ohci->ir_context_list);
  2093. kfree(ohci->it_context_list);
  2094. context_release(&ohci->at_response_ctx);
  2095. context_release(&ohci->at_request_ctx);
  2096. ar_context_release(&ohci->ar_response_ctx);
  2097. ar_context_release(&ohci->ar_request_ctx);
  2098. pci_iounmap(dev, ohci->registers);
  2099. fail_iomem:
  2100. pci_release_region(dev, 0);
  2101. fail_disable:
  2102. pci_disable_device(dev);
  2103. fail_free:
  2104. kfree(&ohci->card);
  2105. ohci_pmac_off(dev);
  2106. fail:
  2107. if (err == -ENOMEM)
  2108. fw_error("Out of memory\n");
  2109. return err;
  2110. }
  2111. static void pci_remove(struct pci_dev *dev)
  2112. {
  2113. struct fw_ohci *ohci;
  2114. ohci = pci_get_drvdata(dev);
  2115. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  2116. flush_writes(ohci);
  2117. fw_core_remove_card(&ohci->card);
  2118. /*
  2119. * FIXME: Fail all pending packets here, now that the upper
  2120. * layers can't queue any more.
  2121. */
  2122. software_reset(ohci);
  2123. free_irq(dev->irq, ohci);
  2124. if (ohci->next_config_rom && ohci->next_config_rom != ohci->config_rom)
  2125. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2126. ohci->next_config_rom, ohci->next_config_rom_bus);
  2127. if (ohci->config_rom)
  2128. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2129. ohci->config_rom, ohci->config_rom_bus);
  2130. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  2131. ohci->self_id_cpu, ohci->self_id_bus);
  2132. ar_context_release(&ohci->ar_request_ctx);
  2133. ar_context_release(&ohci->ar_response_ctx);
  2134. context_release(&ohci->at_request_ctx);
  2135. context_release(&ohci->at_response_ctx);
  2136. kfree(ohci->it_context_list);
  2137. kfree(ohci->ir_context_list);
  2138. pci_iounmap(dev, ohci->registers);
  2139. pci_release_region(dev, 0);
  2140. pci_disable_device(dev);
  2141. kfree(&ohci->card);
  2142. ohci_pmac_off(dev);
  2143. fw_notify("Removed fw-ohci device.\n");
  2144. }
  2145. #ifdef CONFIG_PM
  2146. static int pci_suspend(struct pci_dev *dev, pm_message_t state)
  2147. {
  2148. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2149. int err;
  2150. software_reset(ohci);
  2151. free_irq(dev->irq, ohci);
  2152. err = pci_save_state(dev);
  2153. if (err) {
  2154. fw_error("pci_save_state failed\n");
  2155. return err;
  2156. }
  2157. err = pci_set_power_state(dev, pci_choose_state(dev, state));
  2158. if (err)
  2159. fw_error("pci_set_power_state failed with %d\n", err);
  2160. ohci_pmac_off(dev);
  2161. return 0;
  2162. }
  2163. static int pci_resume(struct pci_dev *dev)
  2164. {
  2165. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2166. int err;
  2167. ohci_pmac_on(dev);
  2168. pci_set_power_state(dev, PCI_D0);
  2169. pci_restore_state(dev);
  2170. err = pci_enable_device(dev);
  2171. if (err) {
  2172. fw_error("pci_enable_device failed\n");
  2173. return err;
  2174. }
  2175. return ohci_enable(&ohci->card, NULL, 0);
  2176. }
  2177. #endif
  2178. static struct pci_device_id pci_table[] = {
  2179. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  2180. { }
  2181. };
  2182. MODULE_DEVICE_TABLE(pci, pci_table);
  2183. static struct pci_driver fw_ohci_pci_driver = {
  2184. .name = ohci_driver_name,
  2185. .id_table = pci_table,
  2186. .probe = pci_probe,
  2187. .remove = pci_remove,
  2188. #ifdef CONFIG_PM
  2189. .resume = pci_resume,
  2190. .suspend = pci_suspend,
  2191. #endif
  2192. };
  2193. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  2194. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  2195. MODULE_LICENSE("GPL");
  2196. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  2197. #ifndef CONFIG_IEEE1394_OHCI1394_MODULE
  2198. MODULE_ALIAS("ohci1394");
  2199. #endif
  2200. static int __init fw_ohci_init(void)
  2201. {
  2202. return pci_register_driver(&fw_ohci_pci_driver);
  2203. }
  2204. static void __exit fw_ohci_cleanup(void)
  2205. {
  2206. pci_unregister_driver(&fw_ohci_pci_driver);
  2207. }
  2208. module_init(fw_ohci_init);
  2209. module_exit(fw_ohci_cleanup);