ohci.c 86 KB

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