ohci.c 86 KB

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