fw-ohci.c 70 KB

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