ohci.c 72 KB

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