ohci.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for OHCI 1394 controllers
  4. *
  5. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bug.h>
  9. #include <linux/compiler.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/firewire.h>
  14. #include <linux/firewire-constants.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/mutex.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci_ids.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/string.h>
  29. #include <linux/time.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/workqueue.h>
  32. #include <asm/byteorder.h>
  33. #include <asm/page.h>
  34. #ifdef CONFIG_PPC_PMAC
  35. #include <asm/pmac_feature.h>
  36. #endif
  37. #include "core.h"
  38. #include "ohci.h"
  39. #include "packet-header-definitions.h"
  40. #include "phy-packet-definitions.h"
  41. #include <trace/events/firewire.h>
  42. static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk);
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/firewire_ohci.h>
  45. #define ohci_notice(ohci, f, args...) dev_notice(ohci->card.device, f, ##args)
  46. #define ohci_err(ohci, f, args...) dev_err(ohci->card.device, f, ##args)
  47. #define DESCRIPTOR_OUTPUT_MORE 0
  48. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  49. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  50. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  51. #define DESCRIPTOR_STATUS (1 << 11)
  52. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  53. #define DESCRIPTOR_PING (1 << 7)
  54. #define DESCRIPTOR_YY (1 << 6)
  55. #define DESCRIPTOR_NO_IRQ (0 << 4)
  56. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  57. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  58. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  59. #define DESCRIPTOR_WAIT (3 << 0)
  60. #define DESCRIPTOR_CMD (0xf << 12)
  61. struct descriptor {
  62. __le16 req_count;
  63. __le16 control;
  64. __le32 data_address;
  65. __le32 branch_address;
  66. __le16 res_count;
  67. __le16 transfer_status;
  68. } __aligned(16);
  69. #define CONTROL_SET(regs) (regs)
  70. #define CONTROL_CLEAR(regs) ((regs) + 4)
  71. #define COMMAND_PTR(regs) ((regs) + 12)
  72. #define CONTEXT_MATCH(regs) ((regs) + 16)
  73. #define AR_BUFFER_SIZE (32*1024)
  74. #define AR_BUFFERS_MIN DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE)
  75. /* we need at least two pages for proper list management */
  76. #define AR_BUFFERS (AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2)
  77. #define MAX_ASYNC_PAYLOAD 4096
  78. #define MAX_AR_PACKET_SIZE (16 + MAX_ASYNC_PAYLOAD + 4)
  79. #define AR_WRAPAROUND_PAGES DIV_ROUND_UP(MAX_AR_PACKET_SIZE, PAGE_SIZE)
  80. struct ar_context {
  81. struct fw_ohci *ohci;
  82. struct page *pages[AR_BUFFERS];
  83. void *buffer;
  84. struct descriptor *descriptors;
  85. dma_addr_t descriptors_bus;
  86. void *pointer;
  87. unsigned int last_buffer_index;
  88. u32 regs;
  89. struct tasklet_struct tasklet;
  90. };
  91. struct context;
  92. typedef int (*descriptor_callback_t)(struct context *ctx,
  93. struct descriptor *d,
  94. struct descriptor *last);
  95. /*
  96. * A buffer that contains a block of DMA-able coherent memory used for
  97. * storing a portion of a DMA descriptor program.
  98. */
  99. struct descriptor_buffer {
  100. struct list_head list;
  101. dma_addr_t buffer_bus;
  102. size_t buffer_size;
  103. size_t used;
  104. struct descriptor buffer[];
  105. };
  106. struct context {
  107. struct fw_ohci *ohci;
  108. u32 regs;
  109. int total_allocation;
  110. u32 current_bus;
  111. bool running;
  112. bool flushing;
  113. /*
  114. * List of page-sized buffers for storing DMA descriptors.
  115. * Head of list contains buffers in use and tail of list contains
  116. * free buffers.
  117. */
  118. struct list_head buffer_list;
  119. /*
  120. * Pointer to a buffer inside buffer_list that contains the tail
  121. * end of the current DMA program.
  122. */
  123. struct descriptor_buffer *buffer_tail;
  124. /*
  125. * The descriptor containing the branch address of the first
  126. * descriptor that has not yet been filled by the device.
  127. */
  128. struct descriptor *last;
  129. /*
  130. * The last descriptor block in the DMA program. It contains the branch
  131. * address that must be updated upon appending a new descriptor.
  132. */
  133. struct descriptor *prev;
  134. int prev_z;
  135. descriptor_callback_t callback;
  136. struct tasklet_struct tasklet;
  137. };
  138. struct iso_context {
  139. struct fw_iso_context base;
  140. struct context context;
  141. void *header;
  142. size_t header_length;
  143. unsigned long flushing_completions;
  144. u32 mc_buffer_bus;
  145. u16 mc_completed;
  146. u16 last_timestamp;
  147. u8 sync;
  148. u8 tags;
  149. };
  150. #define CONFIG_ROM_SIZE (CSR_CONFIG_ROM_END - CSR_CONFIG_ROM)
  151. struct fw_ohci {
  152. struct fw_card card;
  153. __iomem char *registers;
  154. int node_id;
  155. int generation;
  156. int request_generation; /* for timestamping incoming requests */
  157. unsigned quirks;
  158. unsigned int pri_req_max;
  159. u32 bus_time;
  160. bool bus_time_running;
  161. bool is_root;
  162. bool csr_state_setclear_abdicate;
  163. int n_ir;
  164. int n_it;
  165. /*
  166. * Spinlock for accessing fw_ohci data. Never call out of
  167. * this driver with this lock held.
  168. */
  169. spinlock_t lock;
  170. struct mutex phy_reg_mutex;
  171. void *misc_buffer;
  172. dma_addr_t misc_buffer_bus;
  173. struct ar_context ar_request_ctx;
  174. struct ar_context ar_response_ctx;
  175. struct context at_request_ctx;
  176. struct context at_response_ctx;
  177. u32 it_context_support;
  178. u32 it_context_mask; /* unoccupied IT contexts */
  179. struct iso_context *it_context_list;
  180. u64 ir_context_channels; /* unoccupied channels */
  181. u32 ir_context_support;
  182. u32 ir_context_mask; /* unoccupied IR contexts */
  183. struct iso_context *ir_context_list;
  184. u64 mc_channels; /* channels in use by the multichannel IR context */
  185. bool mc_allocated;
  186. __be32 *config_rom;
  187. dma_addr_t config_rom_bus;
  188. __be32 *next_config_rom;
  189. dma_addr_t next_config_rom_bus;
  190. __be32 next_header;
  191. __le32 *self_id;
  192. dma_addr_t self_id_bus;
  193. struct work_struct bus_reset_work;
  194. u32 self_id_buffer[512];
  195. };
  196. static struct workqueue_struct *selfid_workqueue;
  197. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  198. {
  199. return container_of(card, struct fw_ohci, card);
  200. }
  201. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  202. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  203. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  204. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  205. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  206. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  207. #define CONTEXT_RUN 0x8000
  208. #define CONTEXT_WAKE 0x1000
  209. #define CONTEXT_DEAD 0x0800
  210. #define CONTEXT_ACTIVE 0x0400
  211. #define OHCI1394_MAX_AT_REQ_RETRIES 0xf
  212. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  213. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  214. #define OHCI1394_REGISTER_SIZE 0x800
  215. #define OHCI1394_PCI_HCI_Control 0x40
  216. #define SELF_ID_BUF_SIZE 0x800
  217. #define OHCI_VERSION_1_1 0x010010
  218. static char ohci_driver_name[] = KBUILD_MODNAME;
  219. #define PCI_VENDOR_ID_PINNACLE_SYSTEMS 0x11bd
  220. #define PCI_DEVICE_ID_AGERE_FW643 0x5901
  221. #define PCI_DEVICE_ID_CREATIVE_SB1394 0x4001
  222. #define PCI_DEVICE_ID_JMICRON_JMB38X_FW 0x2380
  223. #define PCI_DEVICE_ID_TI_TSB12LV22 0x8009
  224. #define PCI_DEVICE_ID_TI_TSB12LV26 0x8020
  225. #define PCI_DEVICE_ID_TI_TSB82AA2 0x8025
  226. #define PCI_DEVICE_ID_VIA_VT630X 0x3044
  227. #define PCI_REV_ID_VIA_VT6306 0x46
  228. #define PCI_DEVICE_ID_VIA_VT6315 0x3403
  229. #define QUIRK_CYCLE_TIMER 0x1
  230. #define QUIRK_RESET_PACKET 0x2
  231. #define QUIRK_BE_HEADERS 0x4
  232. #define QUIRK_NO_1394A 0x8
  233. #define QUIRK_NO_MSI 0x10
  234. #define QUIRK_TI_SLLZ059 0x20
  235. #define QUIRK_IR_WAKE 0x40
  236. // On PCI Express Root Complex in any type of AMD Ryzen machine, VIA VT6306/6307/6308 with Asmedia
  237. // ASM1083/1085 brings an inconvenience that the read accesses to 'Isochronous Cycle Timer' register
  238. // (at offset 0xf0 in PCI I/O space) often causes unexpected system reboot. The mechanism is not
  239. // clear, since the read access to the other registers is enough safe; e.g. 'Node ID' register,
  240. // while it is probable due to detection of any type of PCIe error.
  241. #define QUIRK_REBOOT_BY_CYCLE_TIMER_READ 0x80000000
  242. #if IS_ENABLED(CONFIG_X86)
  243. static bool has_reboot_by_cycle_timer_read_quirk(const struct fw_ohci *ohci)
  244. {
  245. return !!(ohci->quirks & QUIRK_REBOOT_BY_CYCLE_TIMER_READ);
  246. }
  247. #define PCI_DEVICE_ID_ASMEDIA_ASM108X 0x1080
  248. static bool detect_vt630x_with_asm1083_on_amd_ryzen_machine(const struct pci_dev *pdev)
  249. {
  250. const struct pci_dev *pcie_to_pci_bridge;
  251. // Detect any type of AMD Ryzen machine.
  252. if (!static_cpu_has(X86_FEATURE_ZEN))
  253. return false;
  254. // Detect VIA VT6306/6307/6308.
  255. if (pdev->vendor != PCI_VENDOR_ID_VIA)
  256. return false;
  257. if (pdev->device != PCI_DEVICE_ID_VIA_VT630X)
  258. return false;
  259. // Detect Asmedia ASM1083/1085.
  260. pcie_to_pci_bridge = pdev->bus->self;
  261. if (pcie_to_pci_bridge->vendor != PCI_VENDOR_ID_ASMEDIA)
  262. return false;
  263. if (pcie_to_pci_bridge->device != PCI_DEVICE_ID_ASMEDIA_ASM108X)
  264. return false;
  265. return true;
  266. }
  267. #else
  268. #define has_reboot_by_cycle_timer_read_quirk(ohci) false
  269. #define detect_vt630x_with_asm1083_on_amd_ryzen_machine(pdev) false
  270. #endif
  271. /* In case of multiple matches in ohci_quirks[], only the first one is used. */
  272. static const struct {
  273. unsigned short vendor, device, revision, flags;
  274. } ohci_quirks[] = {
  275. {PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID,
  276. QUIRK_CYCLE_TIMER},
  277. {PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_FW, PCI_ANY_ID,
  278. QUIRK_BE_HEADERS},
  279. {PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_AGERE_FW643, 6,
  280. QUIRK_NO_MSI},
  281. {PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_SB1394, PCI_ANY_ID,
  282. QUIRK_RESET_PACKET},
  283. {PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_FW, PCI_ANY_ID,
  284. QUIRK_NO_MSI},
  285. {PCI_VENDOR_ID_NEC, PCI_ANY_ID, PCI_ANY_ID,
  286. QUIRK_CYCLE_TIMER},
  287. {PCI_VENDOR_ID_O2, PCI_ANY_ID, PCI_ANY_ID,
  288. QUIRK_NO_MSI},
  289. {PCI_VENDOR_ID_RICOH, PCI_ANY_ID, PCI_ANY_ID,
  290. QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
  291. {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV22, PCI_ANY_ID,
  292. QUIRK_CYCLE_TIMER | QUIRK_RESET_PACKET | QUIRK_NO_1394A},
  293. {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV26, PCI_ANY_ID,
  294. QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
  295. {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB82AA2, PCI_ANY_ID,
  296. QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
  297. {PCI_VENDOR_ID_TI, PCI_ANY_ID, PCI_ANY_ID,
  298. QUIRK_RESET_PACKET},
  299. {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT630X, PCI_REV_ID_VIA_VT6306,
  300. QUIRK_CYCLE_TIMER | QUIRK_IR_WAKE},
  301. {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, 0,
  302. QUIRK_CYCLE_TIMER /* FIXME: necessary? */ | QUIRK_NO_MSI},
  303. {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, PCI_ANY_ID,
  304. QUIRK_NO_MSI},
  305. {PCI_VENDOR_ID_VIA, PCI_ANY_ID, PCI_ANY_ID,
  306. QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
  307. };
  308. /* This overrides anything that was found in ohci_quirks[]. */
  309. static int param_quirks;
  310. module_param_named(quirks, param_quirks, int, 0644);
  311. MODULE_PARM_DESC(quirks, "Chip quirks (default = 0"
  312. ", nonatomic cycle timer = " __stringify(QUIRK_CYCLE_TIMER)
  313. ", reset packet generation = " __stringify(QUIRK_RESET_PACKET)
  314. ", AR/selfID endianness = " __stringify(QUIRK_BE_HEADERS)
  315. ", no 1394a enhancements = " __stringify(QUIRK_NO_1394A)
  316. ", disable MSI = " __stringify(QUIRK_NO_MSI)
  317. ", TI SLLZ059 erratum = " __stringify(QUIRK_TI_SLLZ059)
  318. ", IR wake unreliable = " __stringify(QUIRK_IR_WAKE)
  319. ")");
  320. #define OHCI_PARAM_DEBUG_AT_AR 1
  321. #define OHCI_PARAM_DEBUG_SELFIDS 2
  322. #define OHCI_PARAM_DEBUG_IRQS 4
  323. static int param_debug;
  324. module_param_named(debug, param_debug, int, 0644);
  325. MODULE_PARM_DESC(debug, "Verbose logging, deprecated in v6.11 kernel or later. (default = 0"
  326. ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR)
  327. ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS)
  328. ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS)
  329. ", or a combination, or all = -1)");
  330. static bool param_remote_dma;
  331. module_param_named(remote_dma, param_remote_dma, bool, 0444);
  332. MODULE_PARM_DESC(remote_dma, "Enable unfiltered remote DMA (default = N)");
  333. static void log_irqs(struct fw_ohci *ohci, u32 evt)
  334. {
  335. if (likely(!(param_debug & OHCI_PARAM_DEBUG_IRQS)))
  336. return;
  337. ohci_notice(ohci, "IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
  338. evt & OHCI1394_selfIDComplete ? " selfID" : "",
  339. evt & OHCI1394_RQPkt ? " AR_req" : "",
  340. evt & OHCI1394_RSPkt ? " AR_resp" : "",
  341. evt & OHCI1394_reqTxComplete ? " AT_req" : "",
  342. evt & OHCI1394_respTxComplete ? " AT_resp" : "",
  343. evt & OHCI1394_isochRx ? " IR" : "",
  344. evt & OHCI1394_isochTx ? " IT" : "",
  345. evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "",
  346. evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "",
  347. evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "",
  348. evt & OHCI1394_cycleInconsistent ? " cycleInconsistent" : "",
  349. evt & OHCI1394_regAccessFail ? " regAccessFail" : "",
  350. evt & OHCI1394_unrecoverableError ? " unrecoverableError" : "",
  351. evt & OHCI1394_busReset ? " busReset" : "",
  352. evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
  353. OHCI1394_RSPkt | OHCI1394_reqTxComplete |
  354. OHCI1394_respTxComplete | OHCI1394_isochRx |
  355. OHCI1394_isochTx | OHCI1394_postedWriteErr |
  356. OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
  357. OHCI1394_cycleInconsistent |
  358. OHCI1394_regAccessFail | OHCI1394_busReset)
  359. ? " ?" : "");
  360. }
  361. static void log_selfids(struct fw_ohci *ohci, int generation, int self_id_count)
  362. {
  363. static const char *const speed[] = {
  364. [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta",
  365. };
  366. static const char *const power[] = {
  367. [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W",
  368. [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W",
  369. };
  370. static const char port[] = {
  371. [PHY_PACKET_SELF_ID_PORT_STATUS_NONE] = '.',
  372. [PHY_PACKET_SELF_ID_PORT_STATUS_NCONN] = '-',
  373. [PHY_PACKET_SELF_ID_PORT_STATUS_PARENT] = 'p',
  374. [PHY_PACKET_SELF_ID_PORT_STATUS_CHILD] = 'c',
  375. };
  376. struct self_id_sequence_enumerator enumerator = {
  377. .cursor = ohci->self_id_buffer,
  378. .quadlet_count = self_id_count,
  379. };
  380. if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
  381. return;
  382. ohci_notice(ohci, "%d selfIDs, generation %d, local node ID %04x\n",
  383. self_id_count, generation, ohci->node_id);
  384. while (enumerator.quadlet_count > 0) {
  385. unsigned int quadlet_count;
  386. unsigned int port_index;
  387. const u32 *s;
  388. int i;
  389. s = self_id_sequence_enumerator_next(&enumerator, &quadlet_count);
  390. if (IS_ERR(s))
  391. break;
  392. ohci_notice(ohci,
  393. "selfID 0: %08x, phy %d [%c%c%c] %s gc=%d %s %s%s%s\n",
  394. *s,
  395. phy_packet_self_id_get_phy_id(*s),
  396. port[self_id_sequence_get_port_status(s, quadlet_count, 0)],
  397. port[self_id_sequence_get_port_status(s, quadlet_count, 1)],
  398. port[self_id_sequence_get_port_status(s, quadlet_count, 2)],
  399. speed[*s >> 14 & 3], *s >> 16 & 63,
  400. power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
  401. *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
  402. port_index = 3;
  403. for (i = 1; i < quadlet_count; ++i) {
  404. ohci_notice(ohci,
  405. "selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
  406. s[i],
  407. phy_packet_self_id_get_phy_id(s[i]),
  408. port[self_id_sequence_get_port_status(s, quadlet_count, port_index)],
  409. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 1)],
  410. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 2)],
  411. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 3)],
  412. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 4)],
  413. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 5)],
  414. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 6)],
  415. port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 7)]
  416. );
  417. port_index += 8;
  418. }
  419. }
  420. }
  421. static const char *evts[] = {
  422. [0x00] = "evt_no_status", [0x01] = "-reserved-",
  423. [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack",
  424. [0x04] = "evt_underrun", [0x05] = "evt_overrun",
  425. [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read",
  426. [0x08] = "evt_data_write", [0x09] = "evt_bus_reset",
  427. [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err",
  428. [0x0c] = "-reserved-", [0x0d] = "-reserved-",
  429. [0x0e] = "evt_unknown", [0x0f] = "evt_flushed",
  430. [0x10] = "-reserved-", [0x11] = "ack_complete",
  431. [0x12] = "ack_pending ", [0x13] = "-reserved-",
  432. [0x14] = "ack_busy_X", [0x15] = "ack_busy_A",
  433. [0x16] = "ack_busy_B", [0x17] = "-reserved-",
  434. [0x18] = "-reserved-", [0x19] = "-reserved-",
  435. [0x1a] = "-reserved-", [0x1b] = "ack_tardy",
  436. [0x1c] = "-reserved-", [0x1d] = "ack_data_error",
  437. [0x1e] = "ack_type_error", [0x1f] = "-reserved-",
  438. [0x20] = "pending/cancelled",
  439. };
  440. static void log_ar_at_event(struct fw_ohci *ohci,
  441. char dir, int speed, u32 *header, int evt)
  442. {
  443. static const char *const tcodes[] = {
  444. [TCODE_WRITE_QUADLET_REQUEST] = "QW req",
  445. [TCODE_WRITE_BLOCK_REQUEST] = "BW req",
  446. [TCODE_WRITE_RESPONSE] = "W resp",
  447. [0x3] = "-reserved-",
  448. [TCODE_READ_QUADLET_REQUEST] = "QR req",
  449. [TCODE_READ_BLOCK_REQUEST] = "BR req",
  450. [TCODE_READ_QUADLET_RESPONSE] = "QR resp",
  451. [TCODE_READ_BLOCK_RESPONSE] = "BR resp",
  452. [TCODE_CYCLE_START] = "cycle start",
  453. [TCODE_LOCK_REQUEST] = "Lk req",
  454. [TCODE_STREAM_DATA] = "async stream packet",
  455. [TCODE_LOCK_RESPONSE] = "Lk resp",
  456. [0xc] = "-reserved-",
  457. [0xd] = "-reserved-",
  458. [TCODE_LINK_INTERNAL] = "link internal",
  459. [0xf] = "-reserved-",
  460. };
  461. int tcode = async_header_get_tcode(header);
  462. char specific[12];
  463. if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
  464. return;
  465. if (unlikely(evt >= ARRAY_SIZE(evts)))
  466. evt = 0x1f;
  467. if (evt == OHCI1394_evt_bus_reset) {
  468. ohci_notice(ohci, "A%c evt_bus_reset, generation %d\n",
  469. dir, (header[2] >> 16) & 0xff);
  470. return;
  471. }
  472. switch (tcode) {
  473. case TCODE_WRITE_QUADLET_REQUEST:
  474. case TCODE_READ_QUADLET_RESPONSE:
  475. case TCODE_CYCLE_START:
  476. snprintf(specific, sizeof(specific), " = %08x",
  477. be32_to_cpu((__force __be32)header[3]));
  478. break;
  479. case TCODE_WRITE_BLOCK_REQUEST:
  480. case TCODE_READ_BLOCK_REQUEST:
  481. case TCODE_READ_BLOCK_RESPONSE:
  482. case TCODE_LOCK_REQUEST:
  483. case TCODE_LOCK_RESPONSE:
  484. snprintf(specific, sizeof(specific), " %x,%x",
  485. async_header_get_data_length(header),
  486. async_header_get_extended_tcode(header));
  487. break;
  488. default:
  489. specific[0] = '\0';
  490. }
  491. switch (tcode) {
  492. case TCODE_STREAM_DATA:
  493. ohci_notice(ohci, "A%c %s, %s\n",
  494. dir, evts[evt], tcodes[tcode]);
  495. break;
  496. case TCODE_LINK_INTERNAL:
  497. ohci_notice(ohci, "A%c %s, PHY %08x %08x\n",
  498. dir, evts[evt], header[1], header[2]);
  499. break;
  500. case TCODE_WRITE_QUADLET_REQUEST:
  501. case TCODE_WRITE_BLOCK_REQUEST:
  502. case TCODE_READ_QUADLET_REQUEST:
  503. case TCODE_READ_BLOCK_REQUEST:
  504. case TCODE_LOCK_REQUEST:
  505. ohci_notice(ohci,
  506. "A%c spd %x tl %02x, %04x -> %04x, %s, %s, %012llx%s\n",
  507. dir, speed, async_header_get_tlabel(header),
  508. async_header_get_source(header), async_header_get_destination(header),
  509. evts[evt], tcodes[tcode], async_header_get_offset(header), specific);
  510. break;
  511. default:
  512. ohci_notice(ohci,
  513. "A%c spd %x tl %02x, %04x -> %04x, %s, %s%s\n",
  514. dir, speed, async_header_get_tlabel(header),
  515. async_header_get_source(header), async_header_get_destination(header),
  516. evts[evt], tcodes[tcode], specific);
  517. }
  518. }
  519. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  520. {
  521. writel(data, ohci->registers + offset);
  522. }
  523. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  524. {
  525. return readl(ohci->registers + offset);
  526. }
  527. static inline void flush_writes(const struct fw_ohci *ohci)
  528. {
  529. /* Do a dummy read to flush writes. */
  530. reg_read(ohci, OHCI1394_Version);
  531. }
  532. /*
  533. * Beware! read_phy_reg(), write_phy_reg(), update_phy_reg(), and
  534. * read_paged_phy_reg() require the caller to hold ohci->phy_reg_mutex.
  535. * In other words, only use ohci_read_phy_reg() and ohci_update_phy_reg()
  536. * directly. Exceptions are intrinsically serialized contexts like pci_probe.
  537. */
  538. static int read_phy_reg(struct fw_ohci *ohci, int addr)
  539. {
  540. u32 val;
  541. int i;
  542. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  543. for (i = 0; i < 3 + 100; i++) {
  544. val = reg_read(ohci, OHCI1394_PhyControl);
  545. if (!~val)
  546. return -ENODEV; /* Card was ejected. */
  547. if (val & OHCI1394_PhyControl_ReadDone)
  548. return OHCI1394_PhyControl_ReadData(val);
  549. /*
  550. * Try a few times without waiting. Sleeping is necessary
  551. * only when the link/PHY interface is busy.
  552. */
  553. if (i >= 3)
  554. msleep(1);
  555. }
  556. ohci_err(ohci, "failed to read phy reg %d\n", addr);
  557. dump_stack();
  558. return -EBUSY;
  559. }
  560. static int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val)
  561. {
  562. int i;
  563. reg_write(ohci, OHCI1394_PhyControl,
  564. OHCI1394_PhyControl_Write(addr, val));
  565. for (i = 0; i < 3 + 100; i++) {
  566. val = reg_read(ohci, OHCI1394_PhyControl);
  567. if (!~val)
  568. return -ENODEV; /* Card was ejected. */
  569. if (!(val & OHCI1394_PhyControl_WritePending))
  570. return 0;
  571. if (i >= 3)
  572. msleep(1);
  573. }
  574. ohci_err(ohci, "failed to write phy reg %d, val %u\n", addr, val);
  575. dump_stack();
  576. return -EBUSY;
  577. }
  578. static int update_phy_reg(struct fw_ohci *ohci, int addr,
  579. int clear_bits, int set_bits)
  580. {
  581. int ret = read_phy_reg(ohci, addr);
  582. if (ret < 0)
  583. return ret;
  584. /*
  585. * The interrupt status bits are cleared by writing a one bit.
  586. * Avoid clearing them unless explicitly requested in set_bits.
  587. */
  588. if (addr == 5)
  589. clear_bits |= PHY_INT_STATUS_BITS;
  590. return write_phy_reg(ohci, addr, (ret & ~clear_bits) | set_bits);
  591. }
  592. static int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr)
  593. {
  594. int ret;
  595. ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5);
  596. if (ret < 0)
  597. return ret;
  598. return read_phy_reg(ohci, addr);
  599. }
  600. static int ohci_read_phy_reg(struct fw_card *card, int addr)
  601. {
  602. struct fw_ohci *ohci = fw_ohci(card);
  603. guard(mutex)(&ohci->phy_reg_mutex);
  604. return read_phy_reg(ohci, addr);
  605. }
  606. static int ohci_update_phy_reg(struct fw_card *card, int addr,
  607. int clear_bits, int set_bits)
  608. {
  609. struct fw_ohci *ohci = fw_ohci(card);
  610. guard(mutex)(&ohci->phy_reg_mutex);
  611. return update_phy_reg(ohci, addr, clear_bits, set_bits);
  612. }
  613. static inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i)
  614. {
  615. return page_private(ctx->pages[i]);
  616. }
  617. static void ar_context_link_page(struct ar_context *ctx, unsigned int index)
  618. {
  619. struct descriptor *d;
  620. d = &ctx->descriptors[index];
  621. d->branch_address &= cpu_to_le32(~0xf);
  622. d->res_count = cpu_to_le16(PAGE_SIZE);
  623. d->transfer_status = 0;
  624. wmb(); /* finish init of new descriptors before branch_address update */
  625. d = &ctx->descriptors[ctx->last_buffer_index];
  626. d->branch_address |= cpu_to_le32(1);
  627. ctx->last_buffer_index = index;
  628. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  629. }
  630. static void ar_context_release(struct ar_context *ctx)
  631. {
  632. struct device *dev = ctx->ohci->card.device;
  633. unsigned int i;
  634. if (!ctx->buffer)
  635. return;
  636. vunmap(ctx->buffer);
  637. for (i = 0; i < AR_BUFFERS; i++) {
  638. if (ctx->pages[i])
  639. dma_free_pages(dev, PAGE_SIZE, ctx->pages[i],
  640. ar_buffer_bus(ctx, i), DMA_FROM_DEVICE);
  641. }
  642. }
  643. static void ar_context_abort(struct ar_context *ctx, const char *error_msg)
  644. {
  645. struct fw_ohci *ohci = ctx->ohci;
  646. if (reg_read(ohci, CONTROL_CLEAR(ctx->regs)) & CONTEXT_RUN) {
  647. reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  648. flush_writes(ohci);
  649. ohci_err(ohci, "AR error: %s; DMA stopped\n", error_msg);
  650. }
  651. /* FIXME: restart? */
  652. }
  653. static inline unsigned int ar_next_buffer_index(unsigned int index)
  654. {
  655. return (index + 1) % AR_BUFFERS;
  656. }
  657. static inline unsigned int ar_first_buffer_index(struct ar_context *ctx)
  658. {
  659. return ar_next_buffer_index(ctx->last_buffer_index);
  660. }
  661. /*
  662. * We search for the buffer that contains the last AR packet DMA data written
  663. * by the controller.
  664. */
  665. static unsigned int ar_search_last_active_buffer(struct ar_context *ctx,
  666. unsigned int *buffer_offset)
  667. {
  668. unsigned int i, next_i, last = ctx->last_buffer_index;
  669. __le16 res_count, next_res_count;
  670. i = ar_first_buffer_index(ctx);
  671. res_count = READ_ONCE(ctx->descriptors[i].res_count);
  672. /* A buffer that is not yet completely filled must be the last one. */
  673. while (i != last && res_count == 0) {
  674. /* Peek at the next descriptor. */
  675. next_i = ar_next_buffer_index(i);
  676. rmb(); /* read descriptors in order */
  677. next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
  678. /*
  679. * If the next descriptor is still empty, we must stop at this
  680. * descriptor.
  681. */
  682. if (next_res_count == cpu_to_le16(PAGE_SIZE)) {
  683. /*
  684. * The exception is when the DMA data for one packet is
  685. * split over three buffers; in this case, the middle
  686. * buffer's descriptor might be never updated by the
  687. * controller and look still empty, and we have to peek
  688. * at the third one.
  689. */
  690. if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) {
  691. next_i = ar_next_buffer_index(next_i);
  692. rmb();
  693. next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
  694. if (next_res_count != cpu_to_le16(PAGE_SIZE))
  695. goto next_buffer_is_active;
  696. }
  697. break;
  698. }
  699. next_buffer_is_active:
  700. i = next_i;
  701. res_count = next_res_count;
  702. }
  703. rmb(); /* read res_count before the DMA data */
  704. *buffer_offset = PAGE_SIZE - le16_to_cpu(res_count);
  705. if (*buffer_offset > PAGE_SIZE) {
  706. *buffer_offset = 0;
  707. ar_context_abort(ctx, "corrupted descriptor");
  708. }
  709. return i;
  710. }
  711. static void ar_sync_buffers_for_cpu(struct ar_context *ctx,
  712. unsigned int end_buffer_index,
  713. unsigned int end_buffer_offset)
  714. {
  715. unsigned int i;
  716. i = ar_first_buffer_index(ctx);
  717. while (i != end_buffer_index) {
  718. dma_sync_single_for_cpu(ctx->ohci->card.device,
  719. ar_buffer_bus(ctx, i),
  720. PAGE_SIZE, DMA_FROM_DEVICE);
  721. i = ar_next_buffer_index(i);
  722. }
  723. if (end_buffer_offset > 0)
  724. dma_sync_single_for_cpu(ctx->ohci->card.device,
  725. ar_buffer_bus(ctx, i),
  726. end_buffer_offset, DMA_FROM_DEVICE);
  727. }
  728. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  729. static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk)
  730. {
  731. return has_be_header_quirk ? (__force __u32)value : le32_to_cpu(value);
  732. }
  733. static bool has_be_header_quirk(const struct fw_ohci *ohci)
  734. {
  735. return !!(ohci->quirks & QUIRK_BE_HEADERS);
  736. }
  737. #else
  738. static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk __maybe_unused)
  739. {
  740. return le32_to_cpu(value);
  741. }
  742. static bool has_be_header_quirk(const struct fw_ohci *ohci)
  743. {
  744. return false;
  745. }
  746. #endif
  747. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  748. {
  749. struct fw_ohci *ohci = ctx->ohci;
  750. struct fw_packet p;
  751. u32 status, length, tcode;
  752. int evt;
  753. p.header[0] = cond_le32_to_cpu(buffer[0], has_be_header_quirk(ohci));
  754. p.header[1] = cond_le32_to_cpu(buffer[1], has_be_header_quirk(ohci));
  755. p.header[2] = cond_le32_to_cpu(buffer[2], has_be_header_quirk(ohci));
  756. tcode = async_header_get_tcode(p.header);
  757. switch (tcode) {
  758. case TCODE_WRITE_QUADLET_REQUEST:
  759. case TCODE_READ_QUADLET_RESPONSE:
  760. p.header[3] = (__force __u32) buffer[3];
  761. p.header_length = 16;
  762. p.payload_length = 0;
  763. break;
  764. case TCODE_READ_BLOCK_REQUEST :
  765. p.header[3] = cond_le32_to_cpu(buffer[3], has_be_header_quirk(ohci));
  766. p.header_length = 16;
  767. p.payload_length = 0;
  768. break;
  769. case TCODE_WRITE_BLOCK_REQUEST:
  770. case TCODE_READ_BLOCK_RESPONSE:
  771. case TCODE_LOCK_REQUEST:
  772. case TCODE_LOCK_RESPONSE:
  773. p.header[3] = cond_le32_to_cpu(buffer[3], has_be_header_quirk(ohci));
  774. p.header_length = 16;
  775. p.payload_length = async_header_get_data_length(p.header);
  776. if (p.payload_length > MAX_ASYNC_PAYLOAD) {
  777. ar_context_abort(ctx, "invalid packet length");
  778. return NULL;
  779. }
  780. break;
  781. case TCODE_WRITE_RESPONSE:
  782. case TCODE_READ_QUADLET_REQUEST:
  783. case TCODE_LINK_INTERNAL:
  784. p.header_length = 12;
  785. p.payload_length = 0;
  786. break;
  787. default:
  788. ar_context_abort(ctx, "invalid tcode");
  789. return NULL;
  790. }
  791. p.payload = (void *) buffer + p.header_length;
  792. /* FIXME: What to do about evt_* errors? */
  793. length = (p.header_length + p.payload_length + 3) / 4;
  794. status = cond_le32_to_cpu(buffer[length], has_be_header_quirk(ohci));
  795. evt = (status >> 16) & 0x1f;
  796. p.ack = evt - 16;
  797. p.speed = (status >> 21) & 0x7;
  798. p.timestamp = status & 0xffff;
  799. p.generation = ohci->request_generation;
  800. log_ar_at_event(ohci, 'R', p.speed, p.header, evt);
  801. /*
  802. * Several controllers, notably from NEC and VIA, forget to
  803. * write ack_complete status at PHY packet reception.
  804. */
  805. if (evt == OHCI1394_evt_no_status && tcode == TCODE_LINK_INTERNAL)
  806. p.ack = ACK_COMPLETE;
  807. /*
  808. * The OHCI bus reset handler synthesizes a PHY packet with
  809. * the new generation number when a bus reset happens (see
  810. * section 8.4.2.3). This helps us determine when a request
  811. * was received and make sure we send the response in the same
  812. * generation. We only need this for requests; for responses
  813. * we use the unique tlabel for finding the matching
  814. * request.
  815. *
  816. * Alas some chips sometimes emit bus reset packets with a
  817. * wrong generation. We set the correct generation for these
  818. * at a slightly incorrect time (in bus_reset_work).
  819. */
  820. if (evt == OHCI1394_evt_bus_reset) {
  821. if (!(ohci->quirks & QUIRK_RESET_PACKET))
  822. ohci->request_generation = (p.header[2] >> 16) & 0xff;
  823. } else if (ctx == &ohci->ar_request_ctx) {
  824. fw_core_handle_request(&ohci->card, &p);
  825. } else {
  826. fw_core_handle_response(&ohci->card, &p);
  827. }
  828. return buffer + length + 1;
  829. }
  830. static void *handle_ar_packets(struct ar_context *ctx, void *p, void *end)
  831. {
  832. void *next;
  833. while (p < end) {
  834. next = handle_ar_packet(ctx, p);
  835. if (!next)
  836. return p;
  837. p = next;
  838. }
  839. return p;
  840. }
  841. static void ar_recycle_buffers(struct ar_context *ctx, unsigned int end_buffer)
  842. {
  843. unsigned int i;
  844. i = ar_first_buffer_index(ctx);
  845. while (i != end_buffer) {
  846. dma_sync_single_for_device(ctx->ohci->card.device,
  847. ar_buffer_bus(ctx, i),
  848. PAGE_SIZE, DMA_FROM_DEVICE);
  849. ar_context_link_page(ctx, i);
  850. i = ar_next_buffer_index(i);
  851. }
  852. }
  853. static void ar_context_tasklet(unsigned long data)
  854. {
  855. struct ar_context *ctx = (struct ar_context *)data;
  856. unsigned int end_buffer_index, end_buffer_offset;
  857. void *p, *end;
  858. p = ctx->pointer;
  859. if (!p)
  860. return;
  861. end_buffer_index = ar_search_last_active_buffer(ctx,
  862. &end_buffer_offset);
  863. ar_sync_buffers_for_cpu(ctx, end_buffer_index, end_buffer_offset);
  864. end = ctx->buffer + end_buffer_index * PAGE_SIZE + end_buffer_offset;
  865. if (end_buffer_index < ar_first_buffer_index(ctx)) {
  866. /*
  867. * The filled part of the overall buffer wraps around; handle
  868. * all packets up to the buffer end here. If the last packet
  869. * wraps around, its tail will be visible after the buffer end
  870. * because the buffer start pages are mapped there again.
  871. */
  872. void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE;
  873. p = handle_ar_packets(ctx, p, buffer_end);
  874. if (p < buffer_end)
  875. goto error;
  876. /* adjust p to point back into the actual buffer */
  877. p -= AR_BUFFERS * PAGE_SIZE;
  878. }
  879. p = handle_ar_packets(ctx, p, end);
  880. if (p != end) {
  881. if (p > end)
  882. ar_context_abort(ctx, "inconsistent descriptor");
  883. goto error;
  884. }
  885. ctx->pointer = p;
  886. ar_recycle_buffers(ctx, end_buffer_index);
  887. return;
  888. error:
  889. ctx->pointer = NULL;
  890. }
  891. static int ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci,
  892. unsigned int descriptors_offset, u32 regs)
  893. {
  894. struct device *dev = ohci->card.device;
  895. unsigned int i;
  896. dma_addr_t dma_addr;
  897. struct page *pages[AR_BUFFERS + AR_WRAPAROUND_PAGES];
  898. struct descriptor *d;
  899. ctx->regs = regs;
  900. ctx->ohci = ohci;
  901. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  902. for (i = 0; i < AR_BUFFERS; i++) {
  903. ctx->pages[i] = dma_alloc_pages(dev, PAGE_SIZE, &dma_addr,
  904. DMA_FROM_DEVICE, GFP_KERNEL);
  905. if (!ctx->pages[i])
  906. goto out_of_memory;
  907. set_page_private(ctx->pages[i], dma_addr);
  908. dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE,
  909. DMA_FROM_DEVICE);
  910. }
  911. for (i = 0; i < AR_BUFFERS; i++)
  912. pages[i] = ctx->pages[i];
  913. for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
  914. pages[AR_BUFFERS + i] = ctx->pages[i];
  915. ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL);
  916. if (!ctx->buffer)
  917. goto out_of_memory;
  918. ctx->descriptors = ohci->misc_buffer + descriptors_offset;
  919. ctx->descriptors_bus = ohci->misc_buffer_bus + descriptors_offset;
  920. for (i = 0; i < AR_BUFFERS; i++) {
  921. d = &ctx->descriptors[i];
  922. d->req_count = cpu_to_le16(PAGE_SIZE);
  923. d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  924. DESCRIPTOR_STATUS |
  925. DESCRIPTOR_BRANCH_ALWAYS);
  926. d->data_address = cpu_to_le32(ar_buffer_bus(ctx, i));
  927. d->branch_address = cpu_to_le32(ctx->descriptors_bus +
  928. ar_next_buffer_index(i) * sizeof(struct descriptor));
  929. }
  930. return 0;
  931. out_of_memory:
  932. ar_context_release(ctx);
  933. return -ENOMEM;
  934. }
  935. static void ar_context_run(struct ar_context *ctx)
  936. {
  937. unsigned int i;
  938. for (i = 0; i < AR_BUFFERS; i++)
  939. ar_context_link_page(ctx, i);
  940. ctx->pointer = ctx->buffer;
  941. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ctx->descriptors_bus | 1);
  942. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  943. }
  944. static struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
  945. {
  946. __le16 branch;
  947. branch = d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS);
  948. /* figure out which descriptor the branch address goes in */
  949. if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
  950. return d;
  951. else
  952. return d + z - 1;
  953. }
  954. static void context_retire_descriptors(struct context *ctx)
  955. {
  956. struct descriptor *d, *last;
  957. u32 address;
  958. int z;
  959. struct descriptor_buffer *desc;
  960. desc = list_entry(ctx->buffer_list.next,
  961. struct descriptor_buffer, list);
  962. last = ctx->last;
  963. while (last->branch_address != 0) {
  964. struct descriptor_buffer *old_desc = desc;
  965. address = le32_to_cpu(last->branch_address);
  966. z = address & 0xf;
  967. address &= ~0xf;
  968. ctx->current_bus = address;
  969. /* If the branch address points to a buffer outside of the
  970. * current buffer, advance to the next buffer. */
  971. if (address < desc->buffer_bus ||
  972. address >= desc->buffer_bus + desc->used)
  973. desc = list_entry(desc->list.next,
  974. struct descriptor_buffer, list);
  975. d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
  976. last = find_branch_descriptor(d, z);
  977. if (!ctx->callback(ctx, d, last))
  978. break;
  979. if (old_desc != desc) {
  980. // If we've advanced to the next buffer, move the previous buffer to the
  981. // free list.
  982. old_desc->used = 0;
  983. guard(spinlock_irqsave)(&ctx->ohci->lock);
  984. list_move_tail(&old_desc->list, &ctx->buffer_list);
  985. }
  986. ctx->last = last;
  987. }
  988. }
  989. static void context_tasklet(unsigned long data)
  990. {
  991. struct context *ctx = (struct context *) data;
  992. context_retire_descriptors(ctx);
  993. }
  994. static void ohci_isoc_context_work(struct work_struct *work)
  995. {
  996. struct fw_iso_context *base = container_of(work, struct fw_iso_context, work);
  997. struct iso_context *isoc_ctx = container_of(base, struct iso_context, base);
  998. context_retire_descriptors(&isoc_ctx->context);
  999. }
  1000. /*
  1001. * Allocate a new buffer and add it to the list of free buffers for this
  1002. * context. Must be called with ohci->lock held.
  1003. */
  1004. static int context_add_buffer(struct context *ctx)
  1005. {
  1006. struct descriptor_buffer *desc;
  1007. dma_addr_t bus_addr;
  1008. int offset;
  1009. /*
  1010. * 16MB of descriptors should be far more than enough for any DMA
  1011. * program. This will catch run-away userspace or DoS attacks.
  1012. */
  1013. if (ctx->total_allocation >= 16*1024*1024)
  1014. return -ENOMEM;
  1015. desc = dmam_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE, &bus_addr, GFP_ATOMIC);
  1016. if (!desc)
  1017. return -ENOMEM;
  1018. offset = (void *)&desc->buffer - (void *)desc;
  1019. /*
  1020. * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads
  1021. * for descriptors, even 0x10-byte ones. This can cause page faults when
  1022. * an IOMMU is in use and the oversized read crosses a page boundary.
  1023. * Work around this by always leaving at least 0x10 bytes of padding.
  1024. */
  1025. desc->buffer_size = PAGE_SIZE - offset - 0x10;
  1026. desc->buffer_bus = bus_addr + offset;
  1027. desc->used = 0;
  1028. list_add_tail(&desc->list, &ctx->buffer_list);
  1029. ctx->total_allocation += PAGE_SIZE;
  1030. return 0;
  1031. }
  1032. static int context_init(struct context *ctx, struct fw_ohci *ohci,
  1033. u32 regs, descriptor_callback_t callback)
  1034. {
  1035. ctx->ohci = ohci;
  1036. ctx->regs = regs;
  1037. ctx->total_allocation = 0;
  1038. INIT_LIST_HEAD(&ctx->buffer_list);
  1039. if (context_add_buffer(ctx) < 0)
  1040. return -ENOMEM;
  1041. ctx->buffer_tail = list_entry(ctx->buffer_list.next,
  1042. struct descriptor_buffer, list);
  1043. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  1044. ctx->callback = callback;
  1045. /*
  1046. * We put a dummy descriptor in the buffer that has a NULL
  1047. * branch address and looks like it's been sent. That way we
  1048. * have a descriptor to append DMA programs to.
  1049. */
  1050. memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
  1051. ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  1052. ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
  1053. ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
  1054. ctx->last = ctx->buffer_tail->buffer;
  1055. ctx->prev = ctx->buffer_tail->buffer;
  1056. ctx->prev_z = 1;
  1057. return 0;
  1058. }
  1059. static void context_release(struct context *ctx)
  1060. {
  1061. struct fw_card *card = &ctx->ohci->card;
  1062. struct descriptor_buffer *desc, *tmp;
  1063. list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list) {
  1064. dmam_free_coherent(card->device, PAGE_SIZE, desc,
  1065. desc->buffer_bus - ((void *)&desc->buffer - (void *)desc));
  1066. }
  1067. }
  1068. /* Must be called with ohci->lock held */
  1069. static struct descriptor *context_get_descriptors(struct context *ctx,
  1070. int z, dma_addr_t *d_bus)
  1071. {
  1072. struct descriptor *d = NULL;
  1073. struct descriptor_buffer *desc = ctx->buffer_tail;
  1074. if (z * sizeof(*d) > desc->buffer_size)
  1075. return NULL;
  1076. if (z * sizeof(*d) > desc->buffer_size - desc->used) {
  1077. /* No room for the descriptor in this buffer, so advance to the
  1078. * next one. */
  1079. if (desc->list.next == &ctx->buffer_list) {
  1080. /* If there is no free buffer next in the list,
  1081. * allocate one. */
  1082. if (context_add_buffer(ctx) < 0)
  1083. return NULL;
  1084. }
  1085. desc = list_entry(desc->list.next,
  1086. struct descriptor_buffer, list);
  1087. ctx->buffer_tail = desc;
  1088. }
  1089. d = desc->buffer + desc->used / sizeof(*d);
  1090. memset(d, 0, z * sizeof(*d));
  1091. *d_bus = desc->buffer_bus + desc->used;
  1092. return d;
  1093. }
  1094. static void context_run(struct context *ctx, u32 extra)
  1095. {
  1096. struct fw_ohci *ohci = ctx->ohci;
  1097. reg_write(ohci, COMMAND_PTR(ctx->regs),
  1098. le32_to_cpu(ctx->last->branch_address));
  1099. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  1100. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  1101. ctx->running = true;
  1102. flush_writes(ohci);
  1103. }
  1104. static void context_append(struct context *ctx,
  1105. struct descriptor *d, int z, int extra)
  1106. {
  1107. dma_addr_t d_bus;
  1108. struct descriptor_buffer *desc = ctx->buffer_tail;
  1109. struct descriptor *d_branch;
  1110. d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
  1111. desc->used += (z + extra) * sizeof(*d);
  1112. wmb(); /* finish init of new descriptors before branch_address update */
  1113. d_branch = find_branch_descriptor(ctx->prev, ctx->prev_z);
  1114. d_branch->branch_address = cpu_to_le32(d_bus | z);
  1115. /*
  1116. * VT6306 incorrectly checks only the single descriptor at the
  1117. * CommandPtr when the wake bit is written, so if it's a
  1118. * multi-descriptor block starting with an INPUT_MORE, put a copy of
  1119. * the branch address in the first descriptor.
  1120. *
  1121. * Not doing this for transmit contexts since not sure how it interacts
  1122. * with skip addresses.
  1123. */
  1124. if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) &&
  1125. d_branch != ctx->prev &&
  1126. (ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) ==
  1127. cpu_to_le16(DESCRIPTOR_INPUT_MORE)) {
  1128. ctx->prev->branch_address = cpu_to_le32(d_bus | z);
  1129. }
  1130. ctx->prev = d;
  1131. ctx->prev_z = z;
  1132. }
  1133. static void context_stop(struct context *ctx)
  1134. {
  1135. struct fw_ohci *ohci = ctx->ohci;
  1136. u32 reg;
  1137. int i;
  1138. reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  1139. ctx->running = false;
  1140. for (i = 0; i < 1000; i++) {
  1141. reg = reg_read(ohci, CONTROL_SET(ctx->regs));
  1142. if ((reg & CONTEXT_ACTIVE) == 0)
  1143. return;
  1144. if (i)
  1145. udelay(10);
  1146. }
  1147. ohci_err(ohci, "DMA context still active (0x%08x)\n", reg);
  1148. }
  1149. struct driver_data {
  1150. u8 inline_data[8];
  1151. struct fw_packet *packet;
  1152. };
  1153. /*
  1154. * This function apppends a packet to the DMA queue for transmission.
  1155. * Must always be called with the ochi->lock held to ensure proper
  1156. * generation handling and locking around packet queue manipulation.
  1157. */
  1158. static int at_context_queue_packet(struct context *ctx,
  1159. struct fw_packet *packet)
  1160. {
  1161. struct fw_ohci *ohci = ctx->ohci;
  1162. dma_addr_t d_bus, payload_bus;
  1163. struct driver_data *driver_data;
  1164. struct descriptor *d, *last;
  1165. __le32 *header;
  1166. int z, tcode;
  1167. d = context_get_descriptors(ctx, 4, &d_bus);
  1168. if (d == NULL) {
  1169. packet->ack = RCODE_SEND_ERROR;
  1170. return -1;
  1171. }
  1172. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1173. d[0].res_count = cpu_to_le16(packet->timestamp);
  1174. tcode = async_header_get_tcode(packet->header);
  1175. header = (__le32 *) &d[1];
  1176. switch (tcode) {
  1177. case TCODE_WRITE_QUADLET_REQUEST:
  1178. case TCODE_WRITE_BLOCK_REQUEST:
  1179. case TCODE_WRITE_RESPONSE:
  1180. case TCODE_READ_QUADLET_REQUEST:
  1181. case TCODE_READ_BLOCK_REQUEST:
  1182. case TCODE_READ_QUADLET_RESPONSE:
  1183. case TCODE_READ_BLOCK_RESPONSE:
  1184. case TCODE_LOCK_REQUEST:
  1185. case TCODE_LOCK_RESPONSE:
  1186. ohci1394_at_data_set_src_bus_id(header, false);
  1187. ohci1394_at_data_set_speed(header, packet->speed);
  1188. ohci1394_at_data_set_tlabel(header, async_header_get_tlabel(packet->header));
  1189. ohci1394_at_data_set_retry(header, async_header_get_retry(packet->header));
  1190. ohci1394_at_data_set_tcode(header, tcode);
  1191. ohci1394_at_data_set_destination_id(header,
  1192. async_header_get_destination(packet->header));
  1193. if (ctx == &ctx->ohci->at_response_ctx) {
  1194. ohci1394_at_data_set_rcode(header, async_header_get_rcode(packet->header));
  1195. } else {
  1196. ohci1394_at_data_set_destination_offset(header,
  1197. async_header_get_offset(packet->header));
  1198. }
  1199. if (tcode_is_block_packet(tcode))
  1200. header[3] = cpu_to_le32(packet->header[3]);
  1201. else
  1202. header[3] = (__force __le32) packet->header[3];
  1203. d[0].req_count = cpu_to_le16(packet->header_length);
  1204. break;
  1205. case TCODE_LINK_INTERNAL:
  1206. ohci1394_at_data_set_speed(header, packet->speed);
  1207. ohci1394_at_data_set_tcode(header, TCODE_LINK_INTERNAL);
  1208. header[1] = cpu_to_le32(packet->header[1]);
  1209. header[2] = cpu_to_le32(packet->header[2]);
  1210. d[0].req_count = cpu_to_le16(12);
  1211. if (is_ping_packet(&packet->header[1]))
  1212. d[0].control |= cpu_to_le16(DESCRIPTOR_PING);
  1213. break;
  1214. case TCODE_STREAM_DATA:
  1215. ohci1394_it_data_set_speed(header, packet->speed);
  1216. ohci1394_it_data_set_tag(header, isoc_header_get_tag(packet->header[0]));
  1217. ohci1394_it_data_set_channel(header, isoc_header_get_channel(packet->header[0]));
  1218. ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA);
  1219. ohci1394_it_data_set_sync(header, isoc_header_get_sy(packet->header[0]));
  1220. ohci1394_it_data_set_data_length(header, isoc_header_get_data_length(packet->header[0]));
  1221. d[0].req_count = cpu_to_le16(8);
  1222. break;
  1223. default:
  1224. /* BUG(); */
  1225. packet->ack = RCODE_SEND_ERROR;
  1226. return -1;
  1227. }
  1228. BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor));
  1229. driver_data = (struct driver_data *) &d[3];
  1230. driver_data->packet = packet;
  1231. packet->driver_data = driver_data;
  1232. if (packet->payload_length > 0) {
  1233. if (packet->payload_length > sizeof(driver_data->inline_data)) {
  1234. payload_bus = dma_map_single(ohci->card.device,
  1235. packet->payload,
  1236. packet->payload_length,
  1237. DMA_TO_DEVICE);
  1238. if (dma_mapping_error(ohci->card.device, payload_bus)) {
  1239. packet->ack = RCODE_SEND_ERROR;
  1240. return -1;
  1241. }
  1242. packet->payload_bus = payload_bus;
  1243. packet->payload_mapped = true;
  1244. } else {
  1245. memcpy(driver_data->inline_data, packet->payload,
  1246. packet->payload_length);
  1247. payload_bus = d_bus + 3 * sizeof(*d);
  1248. }
  1249. d[2].req_count = cpu_to_le16(packet->payload_length);
  1250. d[2].data_address = cpu_to_le32(payload_bus);
  1251. last = &d[2];
  1252. z = 3;
  1253. } else {
  1254. last = &d[0];
  1255. z = 2;
  1256. }
  1257. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1258. DESCRIPTOR_IRQ_ALWAYS |
  1259. DESCRIPTOR_BRANCH_ALWAYS);
  1260. /* FIXME: Document how the locking works. */
  1261. if (ohci->generation != packet->generation) {
  1262. if (packet->payload_mapped)
  1263. dma_unmap_single(ohci->card.device, payload_bus,
  1264. packet->payload_length, DMA_TO_DEVICE);
  1265. packet->ack = RCODE_GENERATION;
  1266. return -1;
  1267. }
  1268. context_append(ctx, d, z, 4 - z);
  1269. if (ctx->running)
  1270. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  1271. else
  1272. context_run(ctx, 0);
  1273. return 0;
  1274. }
  1275. static void at_context_flush(struct context *ctx)
  1276. {
  1277. tasklet_disable(&ctx->tasklet);
  1278. ctx->flushing = true;
  1279. context_tasklet((unsigned long)ctx);
  1280. ctx->flushing = false;
  1281. tasklet_enable(&ctx->tasklet);
  1282. }
  1283. static int handle_at_packet(struct context *context,
  1284. struct descriptor *d,
  1285. struct descriptor *last)
  1286. {
  1287. struct driver_data *driver_data;
  1288. struct fw_packet *packet;
  1289. struct fw_ohci *ohci = context->ohci;
  1290. int evt;
  1291. if (last->transfer_status == 0 && !context->flushing)
  1292. /* This descriptor isn't done yet, stop iteration. */
  1293. return 0;
  1294. driver_data = (struct driver_data *) &d[3];
  1295. packet = driver_data->packet;
  1296. if (packet == NULL)
  1297. /* This packet was cancelled, just continue. */
  1298. return 1;
  1299. if (packet->payload_mapped)
  1300. dma_unmap_single(ohci->card.device, packet->payload_bus,
  1301. packet->payload_length, DMA_TO_DEVICE);
  1302. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  1303. packet->timestamp = le16_to_cpu(last->res_count);
  1304. log_ar_at_event(ohci, 'T', packet->speed, packet->header, evt);
  1305. switch (evt) {
  1306. case OHCI1394_evt_timeout:
  1307. /* Async response transmit timed out. */
  1308. packet->ack = RCODE_CANCELLED;
  1309. break;
  1310. case OHCI1394_evt_flushed:
  1311. /*
  1312. * The packet was flushed should give same error as
  1313. * when we try to use a stale generation count.
  1314. */
  1315. packet->ack = RCODE_GENERATION;
  1316. break;
  1317. case OHCI1394_evt_missing_ack:
  1318. if (context->flushing)
  1319. packet->ack = RCODE_GENERATION;
  1320. else {
  1321. /*
  1322. * Using a valid (current) generation count, but the
  1323. * node is not on the bus or not sending acks.
  1324. */
  1325. packet->ack = RCODE_NO_ACK;
  1326. }
  1327. break;
  1328. case ACK_COMPLETE + 0x10:
  1329. case ACK_PENDING + 0x10:
  1330. case ACK_BUSY_X + 0x10:
  1331. case ACK_BUSY_A + 0x10:
  1332. case ACK_BUSY_B + 0x10:
  1333. case ACK_DATA_ERROR + 0x10:
  1334. case ACK_TYPE_ERROR + 0x10:
  1335. packet->ack = evt - 0x10;
  1336. break;
  1337. case OHCI1394_evt_no_status:
  1338. if (context->flushing) {
  1339. packet->ack = RCODE_GENERATION;
  1340. break;
  1341. }
  1342. fallthrough;
  1343. default:
  1344. packet->ack = RCODE_SEND_ERROR;
  1345. break;
  1346. }
  1347. packet->callback(packet, &ohci->card, packet->ack);
  1348. return 1;
  1349. }
  1350. static u32 get_cycle_time(struct fw_ohci *ohci);
  1351. static void handle_local_rom(struct fw_ohci *ohci,
  1352. struct fw_packet *packet, u32 csr)
  1353. {
  1354. struct fw_packet response;
  1355. int tcode, length, i;
  1356. tcode = async_header_get_tcode(packet->header);
  1357. if (tcode_is_block_packet(tcode))
  1358. length = async_header_get_data_length(packet->header);
  1359. else
  1360. length = 4;
  1361. i = csr - CSR_CONFIG_ROM;
  1362. if (i + length > CONFIG_ROM_SIZE) {
  1363. fw_fill_response(&response, packet->header,
  1364. RCODE_ADDRESS_ERROR, NULL, 0);
  1365. } else if (!tcode_is_read_request(tcode)) {
  1366. fw_fill_response(&response, packet->header,
  1367. RCODE_TYPE_ERROR, NULL, 0);
  1368. } else {
  1369. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  1370. (void *) ohci->config_rom + i, length);
  1371. }
  1372. // Timestamping on behalf of the hardware.
  1373. response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
  1374. fw_core_handle_response(&ohci->card, &response);
  1375. }
  1376. static void handle_local_lock(struct fw_ohci *ohci,
  1377. struct fw_packet *packet, u32 csr)
  1378. {
  1379. struct fw_packet response;
  1380. int tcode, length, ext_tcode, sel, try;
  1381. __be32 *payload, lock_old;
  1382. u32 lock_arg, lock_data;
  1383. tcode = async_header_get_tcode(packet->header);
  1384. length = async_header_get_data_length(packet->header);
  1385. payload = packet->payload;
  1386. ext_tcode = async_header_get_extended_tcode(packet->header);
  1387. if (tcode == TCODE_LOCK_REQUEST &&
  1388. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  1389. lock_arg = be32_to_cpu(payload[0]);
  1390. lock_data = be32_to_cpu(payload[1]);
  1391. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  1392. lock_arg = 0;
  1393. lock_data = 0;
  1394. } else {
  1395. fw_fill_response(&response, packet->header,
  1396. RCODE_TYPE_ERROR, NULL, 0);
  1397. goto out;
  1398. }
  1399. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  1400. reg_write(ohci, OHCI1394_CSRData, lock_data);
  1401. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  1402. reg_write(ohci, OHCI1394_CSRControl, sel);
  1403. for (try = 0; try < 20; try++)
  1404. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) {
  1405. lock_old = cpu_to_be32(reg_read(ohci,
  1406. OHCI1394_CSRData));
  1407. fw_fill_response(&response, packet->header,
  1408. RCODE_COMPLETE,
  1409. &lock_old, sizeof(lock_old));
  1410. goto out;
  1411. }
  1412. ohci_err(ohci, "swap not done (CSR lock timeout)\n");
  1413. fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0);
  1414. out:
  1415. // Timestamping on behalf of the hardware.
  1416. response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
  1417. fw_core_handle_response(&ohci->card, &response);
  1418. }
  1419. static void handle_local_request(struct context *ctx, struct fw_packet *packet)
  1420. {
  1421. u64 offset, csr;
  1422. if (ctx == &ctx->ohci->at_request_ctx) {
  1423. packet->ack = ACK_PENDING;
  1424. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1425. }
  1426. offset = async_header_get_offset(packet->header);
  1427. csr = offset - CSR_REGISTER_BASE;
  1428. /* Handle config rom reads. */
  1429. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  1430. handle_local_rom(ctx->ohci, packet, csr);
  1431. else switch (csr) {
  1432. case CSR_BUS_MANAGER_ID:
  1433. case CSR_BANDWIDTH_AVAILABLE:
  1434. case CSR_CHANNELS_AVAILABLE_HI:
  1435. case CSR_CHANNELS_AVAILABLE_LO:
  1436. handle_local_lock(ctx->ohci, packet, csr);
  1437. break;
  1438. default:
  1439. if (ctx == &ctx->ohci->at_request_ctx)
  1440. fw_core_handle_request(&ctx->ohci->card, packet);
  1441. else
  1442. fw_core_handle_response(&ctx->ohci->card, packet);
  1443. break;
  1444. }
  1445. if (ctx == &ctx->ohci->at_response_ctx) {
  1446. packet->ack = ACK_COMPLETE;
  1447. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1448. }
  1449. }
  1450. static void at_context_transmit(struct context *ctx, struct fw_packet *packet)
  1451. {
  1452. unsigned long flags;
  1453. int ret;
  1454. spin_lock_irqsave(&ctx->ohci->lock, flags);
  1455. if (async_header_get_destination(packet->header) == ctx->ohci->node_id &&
  1456. ctx->ohci->generation == packet->generation) {
  1457. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1458. // Timestamping on behalf of the hardware.
  1459. packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci));
  1460. handle_local_request(ctx, packet);
  1461. return;
  1462. }
  1463. ret = at_context_queue_packet(ctx, packet);
  1464. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1465. if (ret < 0) {
  1466. // Timestamping on behalf of the hardware.
  1467. packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci));
  1468. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1469. }
  1470. }
  1471. static void detect_dead_context(struct fw_ohci *ohci,
  1472. const char *name, unsigned int regs)
  1473. {
  1474. u32 ctl;
  1475. ctl = reg_read(ohci, CONTROL_SET(regs));
  1476. if (ctl & CONTEXT_DEAD)
  1477. ohci_err(ohci, "DMA context %s has stopped, error code: %s\n",
  1478. name, evts[ctl & 0x1f]);
  1479. }
  1480. static void handle_dead_contexts(struct fw_ohci *ohci)
  1481. {
  1482. unsigned int i;
  1483. char name[8];
  1484. detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase);
  1485. detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase);
  1486. detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase);
  1487. detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase);
  1488. for (i = 0; i < 32; ++i) {
  1489. if (!(ohci->it_context_support & (1 << i)))
  1490. continue;
  1491. sprintf(name, "IT%u", i);
  1492. detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i));
  1493. }
  1494. for (i = 0; i < 32; ++i) {
  1495. if (!(ohci->ir_context_support & (1 << i)))
  1496. continue;
  1497. sprintf(name, "IR%u", i);
  1498. detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i));
  1499. }
  1500. /* TODO: maybe try to flush and restart the dead contexts */
  1501. }
  1502. static u32 cycle_timer_ticks(u32 cycle_timer)
  1503. {
  1504. u32 ticks;
  1505. ticks = cycle_timer & 0xfff;
  1506. ticks += 3072 * ((cycle_timer >> 12) & 0x1fff);
  1507. ticks += (3072 * 8000) * (cycle_timer >> 25);
  1508. return ticks;
  1509. }
  1510. /*
  1511. * Some controllers exhibit one or more of the following bugs when updating the
  1512. * iso cycle timer register:
  1513. * - When the lowest six bits are wrapping around to zero, a read that happens
  1514. * at the same time will return garbage in the lowest ten bits.
  1515. * - When the cycleOffset field wraps around to zero, the cycleCount field is
  1516. * not incremented for about 60 ns.
  1517. * - Occasionally, the entire register reads zero.
  1518. *
  1519. * To catch these, we read the register three times and ensure that the
  1520. * difference between each two consecutive reads is approximately the same, i.e.
  1521. * less than twice the other. Furthermore, any negative difference indicates an
  1522. * error. (A PCI read should take at least 20 ticks of the 24.576 MHz timer to
  1523. * execute, so we have enough precision to compute the ratio of the differences.)
  1524. */
  1525. static u32 get_cycle_time(struct fw_ohci *ohci)
  1526. {
  1527. u32 c0, c1, c2;
  1528. u32 t0, t1, t2;
  1529. s32 diff01, diff12;
  1530. int i;
  1531. if (has_reboot_by_cycle_timer_read_quirk(ohci))
  1532. return 0;
  1533. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1534. if (ohci->quirks & QUIRK_CYCLE_TIMER) {
  1535. i = 0;
  1536. c1 = c2;
  1537. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1538. do {
  1539. c0 = c1;
  1540. c1 = c2;
  1541. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1542. t0 = cycle_timer_ticks(c0);
  1543. t1 = cycle_timer_ticks(c1);
  1544. t2 = cycle_timer_ticks(c2);
  1545. diff01 = t1 - t0;
  1546. diff12 = t2 - t1;
  1547. } while ((diff01 <= 0 || diff12 <= 0 ||
  1548. diff01 / diff12 >= 2 || diff12 / diff01 >= 2)
  1549. && i++ < 20);
  1550. }
  1551. return c2;
  1552. }
  1553. /*
  1554. * This function has to be called at least every 64 seconds. The bus_time
  1555. * field stores not only the upper 25 bits of the BUS_TIME register but also
  1556. * the most significant bit of the cycle timer in bit 6 so that we can detect
  1557. * changes in this bit.
  1558. */
  1559. static u32 update_bus_time(struct fw_ohci *ohci)
  1560. {
  1561. u32 cycle_time_seconds = get_cycle_time(ohci) >> 25;
  1562. if (unlikely(!ohci->bus_time_running)) {
  1563. reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_cycle64Seconds);
  1564. ohci->bus_time = (lower_32_bits(ktime_get_seconds()) & ~0x7f) |
  1565. (cycle_time_seconds & 0x40);
  1566. ohci->bus_time_running = true;
  1567. }
  1568. if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40))
  1569. ohci->bus_time += 0x40;
  1570. return ohci->bus_time | cycle_time_seconds;
  1571. }
  1572. static int get_status_for_port(struct fw_ohci *ohci, int port_index,
  1573. enum phy_packet_self_id_port_status *status)
  1574. {
  1575. int reg;
  1576. scoped_guard(mutex, &ohci->phy_reg_mutex) {
  1577. reg = write_phy_reg(ohci, 7, port_index);
  1578. if (reg < 0)
  1579. return reg;
  1580. reg = read_phy_reg(ohci, 8);
  1581. if (reg < 0)
  1582. return reg;
  1583. }
  1584. switch (reg & 0x0f) {
  1585. case 0x06:
  1586. // is child node (connected to parent node)
  1587. *status = PHY_PACKET_SELF_ID_PORT_STATUS_PARENT;
  1588. break;
  1589. case 0x0e:
  1590. // is parent node (connected to child node)
  1591. *status = PHY_PACKET_SELF_ID_PORT_STATUS_CHILD;
  1592. break;
  1593. default:
  1594. // not connected
  1595. *status = PHY_PACKET_SELF_ID_PORT_STATUS_NCONN;
  1596. break;
  1597. }
  1598. return 0;
  1599. }
  1600. static int get_self_id_pos(struct fw_ohci *ohci, u32 self_id,
  1601. int self_id_count)
  1602. {
  1603. unsigned int left_phy_id = phy_packet_self_id_get_phy_id(self_id);
  1604. int i;
  1605. for (i = 0; i < self_id_count; i++) {
  1606. u32 entry = ohci->self_id_buffer[i];
  1607. unsigned int right_phy_id = phy_packet_self_id_get_phy_id(entry);
  1608. if (left_phy_id == right_phy_id)
  1609. return -1;
  1610. if (left_phy_id < right_phy_id)
  1611. return i;
  1612. }
  1613. return i;
  1614. }
  1615. static int detect_initiated_reset(struct fw_ohci *ohci, bool *is_initiated_reset)
  1616. {
  1617. int reg;
  1618. guard(mutex)(&ohci->phy_reg_mutex);
  1619. // Select page 7
  1620. reg = write_phy_reg(ohci, 7, 0xe0);
  1621. if (reg < 0)
  1622. return reg;
  1623. reg = read_phy_reg(ohci, 8);
  1624. if (reg < 0)
  1625. return reg;
  1626. // set PMODE bit
  1627. reg |= 0x40;
  1628. reg = write_phy_reg(ohci, 8, reg);
  1629. if (reg < 0)
  1630. return reg;
  1631. // read register 12
  1632. reg = read_phy_reg(ohci, 12);
  1633. if (reg < 0)
  1634. return reg;
  1635. // bit 3 indicates "initiated reset"
  1636. *is_initiated_reset = !!((reg & 0x08) == 0x08);
  1637. return 0;
  1638. }
  1639. /*
  1640. * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally
  1641. * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059.
  1642. * Construct the selfID from phy register contents.
  1643. */
  1644. static int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count)
  1645. {
  1646. int reg, i, pos, err;
  1647. bool is_initiated_reset;
  1648. u32 self_id = 0;
  1649. // link active 1, speed 3, bridge 0, contender 1, more packets 0.
  1650. phy_packet_set_packet_identifier(&self_id, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID);
  1651. phy_packet_self_id_zero_set_link_active(&self_id, true);
  1652. phy_packet_self_id_zero_set_scode(&self_id, SCODE_800);
  1653. phy_packet_self_id_zero_set_contender(&self_id, true);
  1654. reg = reg_read(ohci, OHCI1394_NodeID);
  1655. if (!(reg & OHCI1394_NodeID_idValid)) {
  1656. ohci_notice(ohci,
  1657. "node ID not valid, new bus reset in progress\n");
  1658. return -EBUSY;
  1659. }
  1660. phy_packet_self_id_set_phy_id(&self_id, reg & 0x3f);
  1661. reg = ohci_read_phy_reg(&ohci->card, 4);
  1662. if (reg < 0)
  1663. return reg;
  1664. phy_packet_self_id_zero_set_power_class(&self_id, reg & 0x07);
  1665. reg = ohci_read_phy_reg(&ohci->card, 1);
  1666. if (reg < 0)
  1667. return reg;
  1668. phy_packet_self_id_zero_set_gap_count(&self_id, reg & 0x3f);
  1669. for (i = 0; i < 3; i++) {
  1670. enum phy_packet_self_id_port_status status;
  1671. err = get_status_for_port(ohci, i, &status);
  1672. if (err < 0)
  1673. return err;
  1674. self_id_sequence_set_port_status(&self_id, 1, i, status);
  1675. }
  1676. err = detect_initiated_reset(ohci, &is_initiated_reset);
  1677. if (err < 0)
  1678. return err;
  1679. phy_packet_self_id_zero_set_initiated_reset(&self_id, is_initiated_reset);
  1680. pos = get_self_id_pos(ohci, self_id, self_id_count);
  1681. if (pos >= 0) {
  1682. memmove(&(ohci->self_id_buffer[pos+1]),
  1683. &(ohci->self_id_buffer[pos]),
  1684. (self_id_count - pos) * sizeof(*ohci->self_id_buffer));
  1685. ohci->self_id_buffer[pos] = self_id;
  1686. self_id_count++;
  1687. }
  1688. return self_id_count;
  1689. }
  1690. static void bus_reset_work(struct work_struct *work)
  1691. {
  1692. struct fw_ohci *ohci =
  1693. container_of(work, struct fw_ohci, bus_reset_work);
  1694. int self_id_count, generation, new_generation, i, j;
  1695. u32 reg, quadlet;
  1696. void *free_rom = NULL;
  1697. dma_addr_t free_rom_bus = 0;
  1698. bool is_new_root;
  1699. reg = reg_read(ohci, OHCI1394_NodeID);
  1700. if (!(reg & OHCI1394_NodeID_idValid)) {
  1701. ohci_notice(ohci,
  1702. "node ID not valid, new bus reset in progress\n");
  1703. return;
  1704. }
  1705. if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
  1706. ohci_notice(ohci, "malconfigured bus\n");
  1707. return;
  1708. }
  1709. ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
  1710. OHCI1394_NodeID_nodeNumber);
  1711. is_new_root = (reg & OHCI1394_NodeID_root) != 0;
  1712. if (!(ohci->is_root && is_new_root))
  1713. reg_write(ohci, OHCI1394_LinkControlSet,
  1714. OHCI1394_LinkControl_cycleMaster);
  1715. ohci->is_root = is_new_root;
  1716. reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1717. if (ohci1394_self_id_count_is_error(reg)) {
  1718. ohci_notice(ohci, "self ID receive error\n");
  1719. return;
  1720. }
  1721. /*
  1722. * The count in the SelfIDCount register is the number of
  1723. * bytes in the self ID receive buffer. Since we also receive
  1724. * the inverted quadlets and a header quadlet, we shift one
  1725. * bit extra to get the actual number of self IDs.
  1726. */
  1727. self_id_count = ohci1394_self_id_count_get_size(reg) >> 1;
  1728. if (self_id_count > 252) {
  1729. ohci_notice(ohci, "bad selfIDSize (%08x)\n", reg);
  1730. return;
  1731. }
  1732. quadlet = cond_le32_to_cpu(ohci->self_id[0], has_be_header_quirk(ohci));
  1733. generation = ohci1394_self_id_receive_q0_get_generation(quadlet);
  1734. rmb();
  1735. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  1736. u32 id = cond_le32_to_cpu(ohci->self_id[i], has_be_header_quirk(ohci));
  1737. u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1], has_be_header_quirk(ohci));
  1738. if (id != ~id2) {
  1739. /*
  1740. * If the invalid data looks like a cycle start packet,
  1741. * it's likely to be the result of the cycle master
  1742. * having a wrong gap count. In this case, the self IDs
  1743. * so far are valid and should be processed so that the
  1744. * bus manager can then correct the gap count.
  1745. */
  1746. if (id == 0xffff008f) {
  1747. ohci_notice(ohci, "ignoring spurious self IDs\n");
  1748. self_id_count = j;
  1749. break;
  1750. }
  1751. ohci_notice(ohci, "bad self ID %d/%d (%08x != ~%08x)\n",
  1752. j, self_id_count, id, id2);
  1753. return;
  1754. }
  1755. ohci->self_id_buffer[j] = id;
  1756. }
  1757. if (ohci->quirks & QUIRK_TI_SLLZ059) {
  1758. self_id_count = find_and_insert_self_id(ohci, self_id_count);
  1759. if (self_id_count < 0) {
  1760. ohci_notice(ohci,
  1761. "could not construct local self ID\n");
  1762. return;
  1763. }
  1764. }
  1765. if (self_id_count == 0) {
  1766. ohci_notice(ohci, "no self IDs\n");
  1767. return;
  1768. }
  1769. rmb();
  1770. /*
  1771. * Check the consistency of the self IDs we just read. The
  1772. * problem we face is that a new bus reset can start while we
  1773. * read out the self IDs from the DMA buffer. If this happens,
  1774. * the DMA buffer will be overwritten with new self IDs and we
  1775. * will read out inconsistent data. The OHCI specification
  1776. * (section 11.2) recommends a technique similar to
  1777. * linux/seqlock.h, where we remember the generation of the
  1778. * self IDs in the buffer before reading them out and compare
  1779. * it to the current generation after reading them out. If
  1780. * the two generations match we know we have a consistent set
  1781. * of self IDs.
  1782. */
  1783. reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1784. new_generation = ohci1394_self_id_count_get_generation(reg);
  1785. if (new_generation != generation) {
  1786. ohci_notice(ohci, "new bus reset, discarding self ids\n");
  1787. return;
  1788. }
  1789. // FIXME: Document how the locking works.
  1790. scoped_guard(spinlock_irq, &ohci->lock) {
  1791. ohci->generation = -1; // prevent AT packet queueing
  1792. context_stop(&ohci->at_request_ctx);
  1793. context_stop(&ohci->at_response_ctx);
  1794. }
  1795. /*
  1796. * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent
  1797. * packets in the AT queues and software needs to drain them.
  1798. * Some OHCI 1.1 controllers (JMicron) apparently require this too.
  1799. */
  1800. at_context_flush(&ohci->at_request_ctx);
  1801. at_context_flush(&ohci->at_response_ctx);
  1802. scoped_guard(spinlock_irq, &ohci->lock) {
  1803. ohci->generation = generation;
  1804. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  1805. reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset);
  1806. if (ohci->quirks & QUIRK_RESET_PACKET)
  1807. ohci->request_generation = generation;
  1808. // This next bit is unrelated to the AT context stuff but we have to do it under the
  1809. // spinlock also. If a new config rom was set up before this reset, the old one is
  1810. // now no longer in use and we can free it. Update the config rom pointers to point
  1811. // to the current config rom and clear the next_config_rom pointer so a new update
  1812. // can take place.
  1813. if (ohci->next_config_rom != NULL) {
  1814. if (ohci->next_config_rom != ohci->config_rom) {
  1815. free_rom = ohci->config_rom;
  1816. free_rom_bus = ohci->config_rom_bus;
  1817. }
  1818. ohci->config_rom = ohci->next_config_rom;
  1819. ohci->config_rom_bus = ohci->next_config_rom_bus;
  1820. ohci->next_config_rom = NULL;
  1821. // Restore config_rom image and manually update config_rom registers.
  1822. // Writing the header quadlet will indicate that the config rom is ready,
  1823. // so we do that last.
  1824. reg_write(ohci, OHCI1394_BusOptions, be32_to_cpu(ohci->config_rom[2]));
  1825. ohci->config_rom[0] = ohci->next_header;
  1826. reg_write(ohci, OHCI1394_ConfigROMhdr, be32_to_cpu(ohci->next_header));
  1827. }
  1828. if (param_remote_dma) {
  1829. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
  1830. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
  1831. }
  1832. }
  1833. if (free_rom)
  1834. dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, free_rom, free_rom_bus);
  1835. log_selfids(ohci, generation, self_id_count);
  1836. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  1837. self_id_count, ohci->self_id_buffer,
  1838. ohci->csr_state_setclear_abdicate);
  1839. ohci->csr_state_setclear_abdicate = false;
  1840. }
  1841. static irqreturn_t irq_handler(int irq, void *data)
  1842. {
  1843. struct fw_ohci *ohci = data;
  1844. u32 event, iso_event;
  1845. int i;
  1846. event = reg_read(ohci, OHCI1394_IntEventClear);
  1847. if (!event || !~event)
  1848. return IRQ_NONE;
  1849. if (unlikely(param_debug > 0)) {
  1850. dev_notice_ratelimited(ohci->card.device,
  1851. "The debug parameter is superceded by tracepoints events, and deprecated.");
  1852. }
  1853. /*
  1854. * busReset and postedWriteErr events must not be cleared yet
  1855. * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1)
  1856. */
  1857. reg_write(ohci, OHCI1394_IntEventClear,
  1858. event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr));
  1859. trace_irqs(ohci->card.index, event);
  1860. log_irqs(ohci, event);
  1861. // The flag is masked again at bus_reset_work() scheduled by selfID event.
  1862. if (event & OHCI1394_busReset)
  1863. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_busReset);
  1864. if (event & OHCI1394_selfIDComplete) {
  1865. if (trace_self_id_complete_enabled()) {
  1866. u32 reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1867. trace_self_id_complete(ohci->card.index, reg, ohci->self_id,
  1868. has_be_header_quirk(ohci));
  1869. }
  1870. queue_work(selfid_workqueue, &ohci->bus_reset_work);
  1871. }
  1872. if (event & OHCI1394_RQPkt)
  1873. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  1874. if (event & OHCI1394_RSPkt)
  1875. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  1876. if (event & OHCI1394_reqTxComplete)
  1877. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  1878. if (event & OHCI1394_respTxComplete)
  1879. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  1880. if (event & OHCI1394_isochRx) {
  1881. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  1882. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  1883. while (iso_event) {
  1884. i = ffs(iso_event) - 1;
  1885. fw_iso_context_schedule_flush_completions(&ohci->ir_context_list[i].base);
  1886. iso_event &= ~(1 << i);
  1887. }
  1888. }
  1889. if (event & OHCI1394_isochTx) {
  1890. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  1891. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  1892. while (iso_event) {
  1893. i = ffs(iso_event) - 1;
  1894. fw_iso_context_schedule_flush_completions(&ohci->it_context_list[i].base);
  1895. iso_event &= ~(1 << i);
  1896. }
  1897. }
  1898. if (unlikely(event & OHCI1394_regAccessFail))
  1899. ohci_err(ohci, "register access failure\n");
  1900. if (unlikely(event & OHCI1394_postedWriteErr)) {
  1901. reg_read(ohci, OHCI1394_PostedWriteAddressHi);
  1902. reg_read(ohci, OHCI1394_PostedWriteAddressLo);
  1903. reg_write(ohci, OHCI1394_IntEventClear,
  1904. OHCI1394_postedWriteErr);
  1905. dev_err_ratelimited(ohci->card.device, "PCI posted write error\n");
  1906. }
  1907. if (unlikely(event & OHCI1394_cycleTooLong)) {
  1908. dev_notice_ratelimited(ohci->card.device, "isochronous cycle too long\n");
  1909. reg_write(ohci, OHCI1394_LinkControlSet,
  1910. OHCI1394_LinkControl_cycleMaster);
  1911. }
  1912. if (unlikely(event & OHCI1394_cycleInconsistent)) {
  1913. /*
  1914. * We need to clear this event bit in order to make
  1915. * cycleMatch isochronous I/O work. In theory we should
  1916. * stop active cycleMatch iso contexts now and restart
  1917. * them at least two cycles later. (FIXME?)
  1918. */
  1919. dev_notice_ratelimited(ohci->card.device, "isochronous cycle inconsistent\n");
  1920. }
  1921. if (unlikely(event & OHCI1394_unrecoverableError))
  1922. handle_dead_contexts(ohci);
  1923. if (event & OHCI1394_cycle64Seconds) {
  1924. guard(spinlock)(&ohci->lock);
  1925. update_bus_time(ohci);
  1926. } else
  1927. flush_writes(ohci);
  1928. return IRQ_HANDLED;
  1929. }
  1930. static int software_reset(struct fw_ohci *ohci)
  1931. {
  1932. u32 val;
  1933. int i;
  1934. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1935. for (i = 0; i < 500; i++) {
  1936. val = reg_read(ohci, OHCI1394_HCControlSet);
  1937. if (!~val)
  1938. return -ENODEV; /* Card was ejected. */
  1939. if (!(val & OHCI1394_HCControl_softReset))
  1940. return 0;
  1941. msleep(1);
  1942. }
  1943. return -EBUSY;
  1944. }
  1945. static void copy_config_rom(__be32 *dest, const __be32 *src, size_t length)
  1946. {
  1947. size_t size = length * 4;
  1948. memcpy(dest, src, size);
  1949. if (size < CONFIG_ROM_SIZE)
  1950. memset(&dest[length], 0, CONFIG_ROM_SIZE - size);
  1951. }
  1952. static int configure_1394a_enhancements(struct fw_ohci *ohci)
  1953. {
  1954. bool enable_1394a;
  1955. int ret, clear, set, offset;
  1956. /* Check if the driver should configure link and PHY. */
  1957. if (!(reg_read(ohci, OHCI1394_HCControlSet) &
  1958. OHCI1394_HCControl_programPhyEnable))
  1959. return 0;
  1960. /* Paranoia: check whether the PHY supports 1394a, too. */
  1961. enable_1394a = false;
  1962. ret = read_phy_reg(ohci, 2);
  1963. if (ret < 0)
  1964. return ret;
  1965. if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) {
  1966. ret = read_paged_phy_reg(ohci, 1, 8);
  1967. if (ret < 0)
  1968. return ret;
  1969. if (ret >= 1)
  1970. enable_1394a = true;
  1971. }
  1972. if (ohci->quirks & QUIRK_NO_1394A)
  1973. enable_1394a = false;
  1974. /* Configure PHY and link consistently. */
  1975. if (enable_1394a) {
  1976. clear = 0;
  1977. set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
  1978. } else {
  1979. clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
  1980. set = 0;
  1981. }
  1982. ret = update_phy_reg(ohci, 5, clear, set);
  1983. if (ret < 0)
  1984. return ret;
  1985. if (enable_1394a)
  1986. offset = OHCI1394_HCControlSet;
  1987. else
  1988. offset = OHCI1394_HCControlClear;
  1989. reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable);
  1990. /* Clean up: configuration has been taken care of. */
  1991. reg_write(ohci, OHCI1394_HCControlClear,
  1992. OHCI1394_HCControl_programPhyEnable);
  1993. return 0;
  1994. }
  1995. static int probe_tsb41ba3d(struct fw_ohci *ohci)
  1996. {
  1997. /* TI vendor ID = 0x080028, TSB41BA3D product ID = 0x833005 (sic) */
  1998. static const u8 id[] = { 0x08, 0x00, 0x28, 0x83, 0x30, 0x05, };
  1999. int reg, i;
  2000. reg = read_phy_reg(ohci, 2);
  2001. if (reg < 0)
  2002. return reg;
  2003. if ((reg & PHY_EXTENDED_REGISTERS) != PHY_EXTENDED_REGISTERS)
  2004. return 0;
  2005. for (i = ARRAY_SIZE(id) - 1; i >= 0; i--) {
  2006. reg = read_paged_phy_reg(ohci, 1, i + 10);
  2007. if (reg < 0)
  2008. return reg;
  2009. if (reg != id[i])
  2010. return 0;
  2011. }
  2012. return 1;
  2013. }
  2014. static int ohci_enable(struct fw_card *card,
  2015. const __be32 *config_rom, size_t length)
  2016. {
  2017. struct fw_ohci *ohci = fw_ohci(card);
  2018. u32 lps, version, irqs;
  2019. int i, ret;
  2020. ret = software_reset(ohci);
  2021. if (ret < 0) {
  2022. ohci_err(ohci, "failed to reset ohci card\n");
  2023. return ret;
  2024. }
  2025. /*
  2026. * Now enable LPS, which we need in order to start accessing
  2027. * most of the registers. In fact, on some cards (ALI M5251),
  2028. * accessing registers in the SClk domain without LPS enabled
  2029. * will lock up the machine. Wait 50msec to make sure we have
  2030. * full link enabled. However, with some cards (well, at least
  2031. * a JMicron PCIe card), we have to try again sometimes.
  2032. *
  2033. * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but
  2034. * cannot actually use the phy at that time. These need tens of
  2035. * millisecods pause between LPS write and first phy access too.
  2036. */
  2037. reg_write(ohci, OHCI1394_HCControlSet,
  2038. OHCI1394_HCControl_LPS |
  2039. OHCI1394_HCControl_postedWriteEnable);
  2040. flush_writes(ohci);
  2041. for (lps = 0, i = 0; !lps && i < 3; i++) {
  2042. msleep(50);
  2043. lps = reg_read(ohci, OHCI1394_HCControlSet) &
  2044. OHCI1394_HCControl_LPS;
  2045. }
  2046. if (!lps) {
  2047. ohci_err(ohci, "failed to set Link Power Status\n");
  2048. return -EIO;
  2049. }
  2050. if (ohci->quirks & QUIRK_TI_SLLZ059) {
  2051. ret = probe_tsb41ba3d(ohci);
  2052. if (ret < 0)
  2053. return ret;
  2054. if (ret)
  2055. ohci_notice(ohci, "local TSB41BA3D phy\n");
  2056. else
  2057. ohci->quirks &= ~QUIRK_TI_SLLZ059;
  2058. }
  2059. reg_write(ohci, OHCI1394_HCControlClear,
  2060. OHCI1394_HCControl_noByteSwapData);
  2061. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  2062. reg_write(ohci, OHCI1394_LinkControlSet,
  2063. OHCI1394_LinkControl_cycleTimerEnable |
  2064. OHCI1394_LinkControl_cycleMaster);
  2065. reg_write(ohci, OHCI1394_ATRetries,
  2066. OHCI1394_MAX_AT_REQ_RETRIES |
  2067. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  2068. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) |
  2069. (200 << 16));
  2070. ohci->bus_time_running = false;
  2071. for (i = 0; i < 32; i++)
  2072. if (ohci->ir_context_support & (1 << i))
  2073. reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i),
  2074. IR_CONTEXT_MULTI_CHANNEL_MODE);
  2075. version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  2076. if (version >= OHCI_VERSION_1_1) {
  2077. reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
  2078. 0xfffffffe);
  2079. card->broadcast_channel_auto_allocated = true;
  2080. }
  2081. /* Get implemented bits of the priority arbitration request counter. */
  2082. reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
  2083. ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
  2084. reg_write(ohci, OHCI1394_FairnessControl, 0);
  2085. card->priority_budget_implemented = ohci->pri_req_max != 0;
  2086. reg_write(ohci, OHCI1394_PhyUpperBound, FW_MAX_PHYSICAL_RANGE >> 16);
  2087. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  2088. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  2089. ret = configure_1394a_enhancements(ohci);
  2090. if (ret < 0)
  2091. return ret;
  2092. /* Activate link_on bit and contender bit in our self ID packets.*/
  2093. ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER);
  2094. if (ret < 0)
  2095. return ret;
  2096. /*
  2097. * When the link is not yet enabled, the atomic config rom
  2098. * update mechanism described below in ohci_set_config_rom()
  2099. * is not active. We have to update ConfigRomHeader and
  2100. * BusOptions manually, and the write to ConfigROMmap takes
  2101. * effect immediately. We tie this to the enabling of the
  2102. * link, so we have a valid config rom before enabling - the
  2103. * OHCI requires that ConfigROMhdr and BusOptions have valid
  2104. * values before enabling.
  2105. *
  2106. * However, when the ConfigROMmap is written, some controllers
  2107. * always read back quadlets 0 and 2 from the config rom to
  2108. * the ConfigRomHeader and BusOptions registers on bus reset.
  2109. * They shouldn't do that in this initial case where the link
  2110. * isn't enabled. This means we have to use the same
  2111. * workaround here, setting the bus header to 0 and then write
  2112. * the right values in the bus reset tasklet.
  2113. */
  2114. if (config_rom) {
  2115. ohci->next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2116. &ohci->next_config_rom_bus, GFP_KERNEL);
  2117. if (ohci->next_config_rom == NULL)
  2118. return -ENOMEM;
  2119. copy_config_rom(ohci->next_config_rom, config_rom, length);
  2120. } else {
  2121. /*
  2122. * In the suspend case, config_rom is NULL, which
  2123. * means that we just reuse the old config rom.
  2124. */
  2125. ohci->next_config_rom = ohci->config_rom;
  2126. ohci->next_config_rom_bus = ohci->config_rom_bus;
  2127. }
  2128. ohci->next_header = ohci->next_config_rom[0];
  2129. ohci->next_config_rom[0] = 0;
  2130. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  2131. reg_write(ohci, OHCI1394_BusOptions,
  2132. be32_to_cpu(ohci->next_config_rom[2]));
  2133. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  2134. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  2135. irqs = OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  2136. OHCI1394_RQPkt | OHCI1394_RSPkt |
  2137. OHCI1394_isochTx | OHCI1394_isochRx |
  2138. OHCI1394_postedWriteErr |
  2139. OHCI1394_selfIDComplete |
  2140. OHCI1394_regAccessFail |
  2141. OHCI1394_cycleInconsistent |
  2142. OHCI1394_unrecoverableError |
  2143. OHCI1394_cycleTooLong |
  2144. OHCI1394_masterIntEnable |
  2145. OHCI1394_busReset;
  2146. reg_write(ohci, OHCI1394_IntMaskSet, irqs);
  2147. reg_write(ohci, OHCI1394_HCControlSet,
  2148. OHCI1394_HCControl_linkEnable |
  2149. OHCI1394_HCControl_BIBimageValid);
  2150. reg_write(ohci, OHCI1394_LinkControlSet,
  2151. OHCI1394_LinkControl_rcvSelfID |
  2152. OHCI1394_LinkControl_rcvPhyPkt);
  2153. ar_context_run(&ohci->ar_request_ctx);
  2154. ar_context_run(&ohci->ar_response_ctx);
  2155. flush_writes(ohci);
  2156. /* We are ready to go, reset bus to finish initialization. */
  2157. fw_schedule_bus_reset(&ohci->card, false, true);
  2158. return 0;
  2159. }
  2160. static int ohci_set_config_rom(struct fw_card *card,
  2161. const __be32 *config_rom, size_t length)
  2162. {
  2163. struct fw_ohci *ohci;
  2164. __be32 *next_config_rom;
  2165. dma_addr_t next_config_rom_bus;
  2166. ohci = fw_ohci(card);
  2167. /*
  2168. * When the OHCI controller is enabled, the config rom update
  2169. * mechanism is a bit tricky, but easy enough to use. See
  2170. * section 5.5.6 in the OHCI specification.
  2171. *
  2172. * The OHCI controller caches the new config rom address in a
  2173. * shadow register (ConfigROMmapNext) and needs a bus reset
  2174. * for the changes to take place. When the bus reset is
  2175. * detected, the controller loads the new values for the
  2176. * ConfigRomHeader and BusOptions registers from the specified
  2177. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  2178. * shadow register. All automatically and atomically.
  2179. *
  2180. * Now, there's a twist to this story. The automatic load of
  2181. * ConfigRomHeader and BusOptions doesn't honor the
  2182. * noByteSwapData bit, so with a be32 config rom, the
  2183. * controller will load be32 values in to these registers
  2184. * during the atomic update, even on litte endian
  2185. * architectures. The workaround we use is to put a 0 in the
  2186. * header quadlet; 0 is endian agnostic and means that the
  2187. * config rom isn't ready yet. In the bus reset tasklet we
  2188. * then set up the real values for the two registers.
  2189. *
  2190. * We use ohci->lock to avoid racing with the code that sets
  2191. * ohci->next_config_rom to NULL (see bus_reset_work).
  2192. */
  2193. next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2194. &next_config_rom_bus, GFP_KERNEL);
  2195. if (next_config_rom == NULL)
  2196. return -ENOMEM;
  2197. scoped_guard(spinlock_irq, &ohci->lock) {
  2198. // If there is not an already pending config_rom update, push our new allocation
  2199. // into the ohci->next_config_rom and then mark the local variable as null so that
  2200. // we won't deallocate the new buffer.
  2201. //
  2202. // OTOH, if there is a pending config_rom update, just use that buffer with the new
  2203. // config_rom data, and let this routine free the unused DMA allocation.
  2204. if (ohci->next_config_rom == NULL) {
  2205. ohci->next_config_rom = next_config_rom;
  2206. ohci->next_config_rom_bus = next_config_rom_bus;
  2207. next_config_rom = NULL;
  2208. }
  2209. copy_config_rom(ohci->next_config_rom, config_rom, length);
  2210. ohci->next_header = config_rom[0];
  2211. ohci->next_config_rom[0] = 0;
  2212. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  2213. }
  2214. /* If we didn't use the DMA allocation, delete it. */
  2215. if (next_config_rom != NULL) {
  2216. dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, next_config_rom,
  2217. next_config_rom_bus);
  2218. }
  2219. /*
  2220. * Now initiate a bus reset to have the changes take
  2221. * effect. We clean up the old config rom memory and DMA
  2222. * mappings in the bus reset tasklet, since the OHCI
  2223. * controller could need to access it before the bus reset
  2224. * takes effect.
  2225. */
  2226. fw_schedule_bus_reset(&ohci->card, true, true);
  2227. return 0;
  2228. }
  2229. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  2230. {
  2231. struct fw_ohci *ohci = fw_ohci(card);
  2232. at_context_transmit(&ohci->at_request_ctx, packet);
  2233. }
  2234. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  2235. {
  2236. struct fw_ohci *ohci = fw_ohci(card);
  2237. at_context_transmit(&ohci->at_response_ctx, packet);
  2238. }
  2239. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  2240. {
  2241. struct fw_ohci *ohci = fw_ohci(card);
  2242. struct context *ctx = &ohci->at_request_ctx;
  2243. struct driver_data *driver_data = packet->driver_data;
  2244. int ret = -ENOENT;
  2245. tasklet_disable_in_atomic(&ctx->tasklet);
  2246. if (packet->ack != 0)
  2247. goto out;
  2248. if (packet->payload_mapped)
  2249. dma_unmap_single(ohci->card.device, packet->payload_bus,
  2250. packet->payload_length, DMA_TO_DEVICE);
  2251. log_ar_at_event(ohci, 'T', packet->speed, packet->header, 0x20);
  2252. driver_data->packet = NULL;
  2253. packet->ack = RCODE_CANCELLED;
  2254. // Timestamping on behalf of the hardware.
  2255. packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
  2256. packet->callback(packet, &ohci->card, packet->ack);
  2257. ret = 0;
  2258. out:
  2259. tasklet_enable(&ctx->tasklet);
  2260. return ret;
  2261. }
  2262. static int ohci_enable_phys_dma(struct fw_card *card,
  2263. int node_id, int generation)
  2264. {
  2265. struct fw_ohci *ohci = fw_ohci(card);
  2266. int n, ret = 0;
  2267. if (param_remote_dma)
  2268. return 0;
  2269. /*
  2270. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  2271. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  2272. */
  2273. guard(spinlock_irqsave)(&ohci->lock);
  2274. if (ohci->generation != generation)
  2275. return -ESTALE;
  2276. /*
  2277. * Note, if the node ID contains a non-local bus ID, physical DMA is
  2278. * enabled for _all_ nodes on remote buses.
  2279. */
  2280. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  2281. if (n < 32)
  2282. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  2283. else
  2284. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  2285. flush_writes(ohci);
  2286. return ret;
  2287. }
  2288. static u32 ohci_read_csr(struct fw_card *card, int csr_offset)
  2289. {
  2290. struct fw_ohci *ohci = fw_ohci(card);
  2291. u32 value;
  2292. switch (csr_offset) {
  2293. case CSR_STATE_CLEAR:
  2294. case CSR_STATE_SET:
  2295. if (ohci->is_root &&
  2296. (reg_read(ohci, OHCI1394_LinkControlSet) &
  2297. OHCI1394_LinkControl_cycleMaster))
  2298. value = CSR_STATE_BIT_CMSTR;
  2299. else
  2300. value = 0;
  2301. if (ohci->csr_state_setclear_abdicate)
  2302. value |= CSR_STATE_BIT_ABDICATE;
  2303. return value;
  2304. case CSR_NODE_IDS:
  2305. return reg_read(ohci, OHCI1394_NodeID) << 16;
  2306. case CSR_CYCLE_TIME:
  2307. return get_cycle_time(ohci);
  2308. case CSR_BUS_TIME:
  2309. {
  2310. // We might be called just after the cycle timer has wrapped around but just before
  2311. // the cycle64Seconds handler, so we better check here, too, if the bus time needs
  2312. // to be updated.
  2313. guard(spinlock_irqsave)(&ohci->lock);
  2314. return update_bus_time(ohci);
  2315. }
  2316. case CSR_BUSY_TIMEOUT:
  2317. value = reg_read(ohci, OHCI1394_ATRetries);
  2318. return (value >> 4) & 0x0ffff00f;
  2319. case CSR_PRIORITY_BUDGET:
  2320. return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) |
  2321. (ohci->pri_req_max << 8);
  2322. default:
  2323. WARN_ON(1);
  2324. return 0;
  2325. }
  2326. }
  2327. static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value)
  2328. {
  2329. struct fw_ohci *ohci = fw_ohci(card);
  2330. switch (csr_offset) {
  2331. case CSR_STATE_CLEAR:
  2332. if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
  2333. reg_write(ohci, OHCI1394_LinkControlClear,
  2334. OHCI1394_LinkControl_cycleMaster);
  2335. flush_writes(ohci);
  2336. }
  2337. if (value & CSR_STATE_BIT_ABDICATE)
  2338. ohci->csr_state_setclear_abdicate = false;
  2339. break;
  2340. case CSR_STATE_SET:
  2341. if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
  2342. reg_write(ohci, OHCI1394_LinkControlSet,
  2343. OHCI1394_LinkControl_cycleMaster);
  2344. flush_writes(ohci);
  2345. }
  2346. if (value & CSR_STATE_BIT_ABDICATE)
  2347. ohci->csr_state_setclear_abdicate = true;
  2348. break;
  2349. case CSR_NODE_IDS:
  2350. reg_write(ohci, OHCI1394_NodeID, value >> 16);
  2351. flush_writes(ohci);
  2352. break;
  2353. case CSR_CYCLE_TIME:
  2354. reg_write(ohci, OHCI1394_IsochronousCycleTimer, value);
  2355. reg_write(ohci, OHCI1394_IntEventSet,
  2356. OHCI1394_cycleInconsistent);
  2357. flush_writes(ohci);
  2358. break;
  2359. case CSR_BUS_TIME:
  2360. {
  2361. guard(spinlock_irqsave)(&ohci->lock);
  2362. ohci->bus_time = (update_bus_time(ohci) & 0x40) | (value & ~0x7f);
  2363. break;
  2364. }
  2365. case CSR_BUSY_TIMEOUT:
  2366. value = (value & 0xf) | ((value & 0xf) << 4) |
  2367. ((value & 0xf) << 8) | ((value & 0x0ffff000) << 4);
  2368. reg_write(ohci, OHCI1394_ATRetries, value);
  2369. flush_writes(ohci);
  2370. break;
  2371. case CSR_PRIORITY_BUDGET:
  2372. reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f);
  2373. flush_writes(ohci);
  2374. break;
  2375. default:
  2376. WARN_ON(1);
  2377. break;
  2378. }
  2379. }
  2380. static void flush_iso_completions(struct iso_context *ctx, enum fw_iso_context_completions_cause cause)
  2381. {
  2382. trace_isoc_inbound_single_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header,
  2383. ctx->header_length);
  2384. trace_isoc_outbound_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header,
  2385. ctx->header_length);
  2386. ctx->base.callback.sc(&ctx->base, ctx->last_timestamp,
  2387. ctx->header_length, ctx->header,
  2388. ctx->base.callback_data);
  2389. ctx->header_length = 0;
  2390. }
  2391. static void copy_iso_headers(struct iso_context *ctx, const u32 *dma_hdr)
  2392. {
  2393. u32 *ctx_hdr;
  2394. if (ctx->header_length + ctx->base.header_size > PAGE_SIZE) {
  2395. if (ctx->base.drop_overflow_headers)
  2396. return;
  2397. flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW);
  2398. }
  2399. ctx_hdr = ctx->header + ctx->header_length;
  2400. ctx->last_timestamp = (u16)le32_to_cpu((__force __le32)dma_hdr[0]);
  2401. /*
  2402. * The two iso header quadlets are byteswapped to little
  2403. * endian by the controller, but we want to present them
  2404. * as big endian for consistency with the bus endianness.
  2405. */
  2406. if (ctx->base.header_size > 0)
  2407. ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */
  2408. if (ctx->base.header_size > 4)
  2409. ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */
  2410. if (ctx->base.header_size > 8)
  2411. memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8);
  2412. ctx->header_length += ctx->base.header_size;
  2413. }
  2414. static int handle_ir_packet_per_buffer(struct context *context,
  2415. struct descriptor *d,
  2416. struct descriptor *last)
  2417. {
  2418. struct iso_context *ctx =
  2419. container_of(context, struct iso_context, context);
  2420. struct descriptor *pd;
  2421. u32 buffer_dma;
  2422. for (pd = d; pd <= last; pd++)
  2423. if (pd->transfer_status)
  2424. break;
  2425. if (pd > last)
  2426. /* Descriptor(s) not done yet, stop iteration */
  2427. return 0;
  2428. while (!(d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))) {
  2429. d++;
  2430. buffer_dma = le32_to_cpu(d->data_address);
  2431. dma_sync_single_range_for_cpu(context->ohci->card.device,
  2432. buffer_dma & PAGE_MASK,
  2433. buffer_dma & ~PAGE_MASK,
  2434. le16_to_cpu(d->req_count),
  2435. DMA_FROM_DEVICE);
  2436. }
  2437. copy_iso_headers(ctx, (u32 *) (last + 1));
  2438. if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
  2439. flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT);
  2440. return 1;
  2441. }
  2442. /* d == last because each descriptor block is only a single descriptor. */
  2443. static int handle_ir_buffer_fill(struct context *context,
  2444. struct descriptor *d,
  2445. struct descriptor *last)
  2446. {
  2447. struct iso_context *ctx =
  2448. container_of(context, struct iso_context, context);
  2449. unsigned int req_count, res_count, completed;
  2450. u32 buffer_dma;
  2451. req_count = le16_to_cpu(last->req_count);
  2452. res_count = le16_to_cpu(READ_ONCE(last->res_count));
  2453. completed = req_count - res_count;
  2454. buffer_dma = le32_to_cpu(last->data_address);
  2455. if (completed > 0) {
  2456. ctx->mc_buffer_bus = buffer_dma;
  2457. ctx->mc_completed = completed;
  2458. }
  2459. if (res_count != 0)
  2460. /* Descriptor(s) not done yet, stop iteration */
  2461. return 0;
  2462. dma_sync_single_range_for_cpu(context->ohci->card.device,
  2463. buffer_dma & PAGE_MASK,
  2464. buffer_dma & ~PAGE_MASK,
  2465. completed, DMA_FROM_DEVICE);
  2466. if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) {
  2467. trace_isoc_inbound_multiple_completions(&ctx->base, completed,
  2468. FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT);
  2469. ctx->base.callback.mc(&ctx->base,
  2470. buffer_dma + completed,
  2471. ctx->base.callback_data);
  2472. ctx->mc_completed = 0;
  2473. }
  2474. return 1;
  2475. }
  2476. static void flush_ir_buffer_fill(struct iso_context *ctx)
  2477. {
  2478. dma_sync_single_range_for_cpu(ctx->context.ohci->card.device,
  2479. ctx->mc_buffer_bus & PAGE_MASK,
  2480. ctx->mc_buffer_bus & ~PAGE_MASK,
  2481. ctx->mc_completed, DMA_FROM_DEVICE);
  2482. trace_isoc_inbound_multiple_completions(&ctx->base, ctx->mc_completed,
  2483. FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH);
  2484. ctx->base.callback.mc(&ctx->base,
  2485. ctx->mc_buffer_bus + ctx->mc_completed,
  2486. ctx->base.callback_data);
  2487. ctx->mc_completed = 0;
  2488. }
  2489. static inline void sync_it_packet_for_cpu(struct context *context,
  2490. struct descriptor *pd)
  2491. {
  2492. __le16 control;
  2493. u32 buffer_dma;
  2494. /* only packets beginning with OUTPUT_MORE* have data buffers */
  2495. if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
  2496. return;
  2497. /* skip over the OUTPUT_MORE_IMMEDIATE descriptor */
  2498. pd += 2;
  2499. /*
  2500. * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's
  2501. * data buffer is in the context program's coherent page and must not
  2502. * be synced.
  2503. */
  2504. if ((le32_to_cpu(pd->data_address) & PAGE_MASK) ==
  2505. (context->current_bus & PAGE_MASK)) {
  2506. if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
  2507. return;
  2508. pd++;
  2509. }
  2510. do {
  2511. buffer_dma = le32_to_cpu(pd->data_address);
  2512. dma_sync_single_range_for_cpu(context->ohci->card.device,
  2513. buffer_dma & PAGE_MASK,
  2514. buffer_dma & ~PAGE_MASK,
  2515. le16_to_cpu(pd->req_count),
  2516. DMA_TO_DEVICE);
  2517. control = pd->control;
  2518. pd++;
  2519. } while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)));
  2520. }
  2521. static int handle_it_packet(struct context *context,
  2522. struct descriptor *d,
  2523. struct descriptor *last)
  2524. {
  2525. struct iso_context *ctx =
  2526. container_of(context, struct iso_context, context);
  2527. struct descriptor *pd;
  2528. __be32 *ctx_hdr;
  2529. for (pd = d; pd <= last; pd++)
  2530. if (pd->transfer_status)
  2531. break;
  2532. if (pd > last)
  2533. /* Descriptor(s) not done yet, stop iteration */
  2534. return 0;
  2535. sync_it_packet_for_cpu(context, d);
  2536. if (ctx->header_length + 4 > PAGE_SIZE) {
  2537. if (ctx->base.drop_overflow_headers)
  2538. return 1;
  2539. flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW);
  2540. }
  2541. ctx_hdr = ctx->header + ctx->header_length;
  2542. ctx->last_timestamp = le16_to_cpu(last->res_count);
  2543. /* Present this value as big-endian to match the receive code */
  2544. *ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) |
  2545. le16_to_cpu(pd->res_count));
  2546. ctx->header_length += 4;
  2547. if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
  2548. flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT);
  2549. return 1;
  2550. }
  2551. static void set_multichannel_mask(struct fw_ohci *ohci, u64 channels)
  2552. {
  2553. u32 hi = channels >> 32, lo = channels;
  2554. reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi);
  2555. reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo);
  2556. reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi);
  2557. reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo);
  2558. ohci->mc_channels = channels;
  2559. }
  2560. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  2561. int type, int channel, size_t header_size)
  2562. {
  2563. struct fw_ohci *ohci = fw_ohci(card);
  2564. struct iso_context *ctx;
  2565. descriptor_callback_t callback;
  2566. u64 *channels;
  2567. u32 *mask, regs;
  2568. int index, ret = -EBUSY;
  2569. scoped_guard(spinlock_irq, &ohci->lock) {
  2570. switch (type) {
  2571. case FW_ISO_CONTEXT_TRANSMIT:
  2572. mask = &ohci->it_context_mask;
  2573. callback = handle_it_packet;
  2574. index = ffs(*mask) - 1;
  2575. if (index >= 0) {
  2576. *mask &= ~(1 << index);
  2577. regs = OHCI1394_IsoXmitContextBase(index);
  2578. ctx = &ohci->it_context_list[index];
  2579. }
  2580. break;
  2581. case FW_ISO_CONTEXT_RECEIVE:
  2582. channels = &ohci->ir_context_channels;
  2583. mask = &ohci->ir_context_mask;
  2584. callback = handle_ir_packet_per_buffer;
  2585. index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
  2586. if (index >= 0) {
  2587. *channels &= ~(1ULL << channel);
  2588. *mask &= ~(1 << index);
  2589. regs = OHCI1394_IsoRcvContextBase(index);
  2590. ctx = &ohci->ir_context_list[index];
  2591. }
  2592. break;
  2593. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2594. mask = &ohci->ir_context_mask;
  2595. callback = handle_ir_buffer_fill;
  2596. index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1;
  2597. if (index >= 0) {
  2598. ohci->mc_allocated = true;
  2599. *mask &= ~(1 << index);
  2600. regs = OHCI1394_IsoRcvContextBase(index);
  2601. ctx = &ohci->ir_context_list[index];
  2602. }
  2603. break;
  2604. default:
  2605. index = -1;
  2606. ret = -ENOSYS;
  2607. }
  2608. if (index < 0)
  2609. return ERR_PTR(ret);
  2610. }
  2611. memset(ctx, 0, sizeof(*ctx));
  2612. ctx->header_length = 0;
  2613. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  2614. if (ctx->header == NULL) {
  2615. ret = -ENOMEM;
  2616. goto out;
  2617. }
  2618. ret = context_init(&ctx->context, ohci, regs, callback);
  2619. if (ret < 0)
  2620. goto out_with_header;
  2621. fw_iso_context_init_work(&ctx->base, ohci_isoc_context_work);
  2622. if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) {
  2623. set_multichannel_mask(ohci, 0);
  2624. ctx->mc_completed = 0;
  2625. }
  2626. return &ctx->base;
  2627. out_with_header:
  2628. free_page((unsigned long)ctx->header);
  2629. out:
  2630. scoped_guard(spinlock_irq, &ohci->lock) {
  2631. switch (type) {
  2632. case FW_ISO_CONTEXT_RECEIVE:
  2633. *channels |= 1ULL << channel;
  2634. break;
  2635. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2636. ohci->mc_allocated = false;
  2637. break;
  2638. }
  2639. *mask |= 1 << index;
  2640. }
  2641. return ERR_PTR(ret);
  2642. }
  2643. static int ohci_start_iso(struct fw_iso_context *base,
  2644. s32 cycle, u32 sync, u32 tags)
  2645. {
  2646. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2647. struct fw_ohci *ohci = ctx->context.ohci;
  2648. u32 control = IR_CONTEXT_ISOCH_HEADER, match;
  2649. int index;
  2650. /* the controller cannot start without any queued packets */
  2651. if (ctx->context.last->branch_address == 0)
  2652. return -ENODATA;
  2653. switch (ctx->base.type) {
  2654. case FW_ISO_CONTEXT_TRANSMIT:
  2655. index = ctx - ohci->it_context_list;
  2656. match = 0;
  2657. if (cycle >= 0)
  2658. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  2659. (cycle & 0x7fff) << 16;
  2660. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  2661. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  2662. context_run(&ctx->context, match);
  2663. break;
  2664. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2665. control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE;
  2666. fallthrough;
  2667. case FW_ISO_CONTEXT_RECEIVE:
  2668. index = ctx - ohci->ir_context_list;
  2669. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  2670. if (cycle >= 0) {
  2671. match |= (cycle & 0x07fff) << 12;
  2672. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  2673. }
  2674. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  2675. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  2676. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  2677. context_run(&ctx->context, control);
  2678. ctx->sync = sync;
  2679. ctx->tags = tags;
  2680. break;
  2681. }
  2682. return 0;
  2683. }
  2684. static int ohci_stop_iso(struct fw_iso_context *base)
  2685. {
  2686. struct fw_ohci *ohci = fw_ohci(base->card);
  2687. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2688. int index;
  2689. switch (ctx->base.type) {
  2690. case FW_ISO_CONTEXT_TRANSMIT:
  2691. index = ctx - ohci->it_context_list;
  2692. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  2693. break;
  2694. case FW_ISO_CONTEXT_RECEIVE:
  2695. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2696. index = ctx - ohci->ir_context_list;
  2697. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  2698. break;
  2699. }
  2700. flush_writes(ohci);
  2701. context_stop(&ctx->context);
  2702. return 0;
  2703. }
  2704. static void ohci_free_iso_context(struct fw_iso_context *base)
  2705. {
  2706. struct fw_ohci *ohci = fw_ohci(base->card);
  2707. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2708. int index;
  2709. ohci_stop_iso(base);
  2710. context_release(&ctx->context);
  2711. free_page((unsigned long)ctx->header);
  2712. guard(spinlock_irqsave)(&ohci->lock);
  2713. switch (base->type) {
  2714. case FW_ISO_CONTEXT_TRANSMIT:
  2715. index = ctx - ohci->it_context_list;
  2716. ohci->it_context_mask |= 1 << index;
  2717. break;
  2718. case FW_ISO_CONTEXT_RECEIVE:
  2719. index = ctx - ohci->ir_context_list;
  2720. ohci->ir_context_mask |= 1 << index;
  2721. ohci->ir_context_channels |= 1ULL << base->channel;
  2722. break;
  2723. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2724. index = ctx - ohci->ir_context_list;
  2725. ohci->ir_context_mask |= 1 << index;
  2726. ohci->ir_context_channels |= ohci->mc_channels;
  2727. ohci->mc_channels = 0;
  2728. ohci->mc_allocated = false;
  2729. break;
  2730. }
  2731. }
  2732. static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels)
  2733. {
  2734. struct fw_ohci *ohci = fw_ohci(base->card);
  2735. switch (base->type) {
  2736. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2737. {
  2738. guard(spinlock_irqsave)(&ohci->lock);
  2739. // Don't allow multichannel to grab other contexts' channels.
  2740. if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) {
  2741. *channels = ohci->ir_context_channels;
  2742. return -EBUSY;
  2743. } else {
  2744. set_multichannel_mask(ohci, *channels);
  2745. return 0;
  2746. }
  2747. }
  2748. default:
  2749. return -EINVAL;
  2750. }
  2751. }
  2752. #ifdef CONFIG_PM
  2753. static void ohci_resume_iso_dma(struct fw_ohci *ohci)
  2754. {
  2755. int i;
  2756. struct iso_context *ctx;
  2757. for (i = 0 ; i < ohci->n_ir ; i++) {
  2758. ctx = &ohci->ir_context_list[i];
  2759. if (ctx->context.running)
  2760. ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
  2761. }
  2762. for (i = 0 ; i < ohci->n_it ; i++) {
  2763. ctx = &ohci->it_context_list[i];
  2764. if (ctx->context.running)
  2765. ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
  2766. }
  2767. }
  2768. #endif
  2769. static int queue_iso_transmit(struct iso_context *ctx,
  2770. struct fw_iso_packet *packet,
  2771. struct fw_iso_buffer *buffer,
  2772. unsigned long payload)
  2773. {
  2774. struct descriptor *d, *last, *pd;
  2775. struct fw_iso_packet *p;
  2776. __le32 *header;
  2777. dma_addr_t d_bus, page_bus;
  2778. u32 z, header_z, payload_z, irq;
  2779. u32 payload_index, payload_end_index, next_page_index;
  2780. int page, end_page, i, length, offset;
  2781. p = packet;
  2782. payload_index = payload;
  2783. if (p->skip)
  2784. z = 1;
  2785. else
  2786. z = 2;
  2787. if (p->header_length > 0)
  2788. z++;
  2789. /* Determine the first page the payload isn't contained in. */
  2790. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  2791. if (p->payload_length > 0)
  2792. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  2793. else
  2794. payload_z = 0;
  2795. z += payload_z;
  2796. /* Get header size in number of descriptors. */
  2797. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  2798. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  2799. if (d == NULL)
  2800. return -ENOMEM;
  2801. if (!p->skip) {
  2802. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  2803. d[0].req_count = cpu_to_le16(8);
  2804. /*
  2805. * Link the skip address to this descriptor itself. This causes
  2806. * a context to skip a cycle whenever lost cycles or FIFO
  2807. * overruns occur, without dropping the data. The application
  2808. * should then decide whether this is an error condition or not.
  2809. * FIXME: Make the context's cycle-lost behaviour configurable?
  2810. */
  2811. d[0].branch_address = cpu_to_le32(d_bus | z);
  2812. header = (__le32 *) &d[1];
  2813. ohci1394_it_data_set_speed(header, ctx->base.speed);
  2814. ohci1394_it_data_set_tag(header, p->tag);
  2815. ohci1394_it_data_set_channel(header, ctx->base.channel);
  2816. ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA);
  2817. ohci1394_it_data_set_sync(header, p->sy);
  2818. ohci1394_it_data_set_data_length(header, p->header_length + p->payload_length);
  2819. }
  2820. if (p->header_length > 0) {
  2821. d[2].req_count = cpu_to_le16(p->header_length);
  2822. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  2823. memcpy(&d[z], p->header, p->header_length);
  2824. }
  2825. pd = d + z - payload_z;
  2826. payload_end_index = payload_index + p->payload_length;
  2827. for (i = 0; i < payload_z; i++) {
  2828. page = payload_index >> PAGE_SHIFT;
  2829. offset = payload_index & ~PAGE_MASK;
  2830. next_page_index = (page + 1) << PAGE_SHIFT;
  2831. length =
  2832. min(next_page_index, payload_end_index) - payload_index;
  2833. pd[i].req_count = cpu_to_le16(length);
  2834. page_bus = page_private(buffer->pages[page]);
  2835. pd[i].data_address = cpu_to_le32(page_bus + offset);
  2836. dma_sync_single_range_for_device(ctx->context.ohci->card.device,
  2837. page_bus, offset, length,
  2838. DMA_TO_DEVICE);
  2839. payload_index += length;
  2840. }
  2841. if (p->interrupt)
  2842. irq = DESCRIPTOR_IRQ_ALWAYS;
  2843. else
  2844. irq = DESCRIPTOR_NO_IRQ;
  2845. last = z == 2 ? d : d + z - 1;
  2846. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  2847. DESCRIPTOR_STATUS |
  2848. DESCRIPTOR_BRANCH_ALWAYS |
  2849. irq);
  2850. context_append(&ctx->context, d, z, header_z);
  2851. return 0;
  2852. }
  2853. static int queue_iso_packet_per_buffer(struct iso_context *ctx,
  2854. struct fw_iso_packet *packet,
  2855. struct fw_iso_buffer *buffer,
  2856. unsigned long payload)
  2857. {
  2858. struct device *device = ctx->context.ohci->card.device;
  2859. struct descriptor *d, *pd;
  2860. dma_addr_t d_bus, page_bus;
  2861. u32 z, header_z, rest;
  2862. int i, j, length;
  2863. int page, offset, packet_count, header_size, payload_per_buffer;
  2864. /*
  2865. * The OHCI controller puts the isochronous header and trailer in the
  2866. * buffer, so we need at least 8 bytes.
  2867. */
  2868. packet_count = packet->header_length / ctx->base.header_size;
  2869. header_size = max(ctx->base.header_size, (size_t)8);
  2870. /* Get header size in number of descriptors. */
  2871. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  2872. page = payload >> PAGE_SHIFT;
  2873. offset = payload & ~PAGE_MASK;
  2874. payload_per_buffer = packet->payload_length / packet_count;
  2875. for (i = 0; i < packet_count; i++) {
  2876. /* d points to the header descriptor */
  2877. z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
  2878. d = context_get_descriptors(&ctx->context,
  2879. z + header_z, &d_bus);
  2880. if (d == NULL)
  2881. return -ENOMEM;
  2882. d->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2883. DESCRIPTOR_INPUT_MORE);
  2884. if (packet->skip && i == 0)
  2885. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  2886. d->req_count = cpu_to_le16(header_size);
  2887. d->res_count = d->req_count;
  2888. d->transfer_status = 0;
  2889. d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
  2890. rest = payload_per_buffer;
  2891. pd = d;
  2892. for (j = 1; j < z; j++) {
  2893. pd++;
  2894. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2895. DESCRIPTOR_INPUT_MORE);
  2896. if (offset + rest < PAGE_SIZE)
  2897. length = rest;
  2898. else
  2899. length = PAGE_SIZE - offset;
  2900. pd->req_count = cpu_to_le16(length);
  2901. pd->res_count = pd->req_count;
  2902. pd->transfer_status = 0;
  2903. page_bus = page_private(buffer->pages[page]);
  2904. pd->data_address = cpu_to_le32(page_bus + offset);
  2905. dma_sync_single_range_for_device(device, page_bus,
  2906. offset, length,
  2907. DMA_FROM_DEVICE);
  2908. offset = (offset + length) & ~PAGE_MASK;
  2909. rest -= length;
  2910. if (offset == 0)
  2911. page++;
  2912. }
  2913. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2914. DESCRIPTOR_INPUT_LAST |
  2915. DESCRIPTOR_BRANCH_ALWAYS);
  2916. if (packet->interrupt && i == packet_count - 1)
  2917. pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  2918. context_append(&ctx->context, d, z, header_z);
  2919. }
  2920. return 0;
  2921. }
  2922. static int queue_iso_buffer_fill(struct iso_context *ctx,
  2923. struct fw_iso_packet *packet,
  2924. struct fw_iso_buffer *buffer,
  2925. unsigned long payload)
  2926. {
  2927. struct descriptor *d;
  2928. dma_addr_t d_bus, page_bus;
  2929. int page, offset, rest, z, i, length;
  2930. page = payload >> PAGE_SHIFT;
  2931. offset = payload & ~PAGE_MASK;
  2932. rest = packet->payload_length;
  2933. /* We need one descriptor for each page in the buffer. */
  2934. z = DIV_ROUND_UP(offset + rest, PAGE_SIZE);
  2935. if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count))
  2936. return -EFAULT;
  2937. for (i = 0; i < z; i++) {
  2938. d = context_get_descriptors(&ctx->context, 1, &d_bus);
  2939. if (d == NULL)
  2940. return -ENOMEM;
  2941. d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  2942. DESCRIPTOR_BRANCH_ALWAYS);
  2943. if (packet->skip && i == 0)
  2944. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  2945. if (packet->interrupt && i == z - 1)
  2946. d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  2947. if (offset + rest < PAGE_SIZE)
  2948. length = rest;
  2949. else
  2950. length = PAGE_SIZE - offset;
  2951. d->req_count = cpu_to_le16(length);
  2952. d->res_count = d->req_count;
  2953. d->transfer_status = 0;
  2954. page_bus = page_private(buffer->pages[page]);
  2955. d->data_address = cpu_to_le32(page_bus + offset);
  2956. dma_sync_single_range_for_device(ctx->context.ohci->card.device,
  2957. page_bus, offset, length,
  2958. DMA_FROM_DEVICE);
  2959. rest -= length;
  2960. offset = 0;
  2961. page++;
  2962. context_append(&ctx->context, d, 1, 0);
  2963. }
  2964. return 0;
  2965. }
  2966. static int ohci_queue_iso(struct fw_iso_context *base,
  2967. struct fw_iso_packet *packet,
  2968. struct fw_iso_buffer *buffer,
  2969. unsigned long payload)
  2970. {
  2971. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2972. guard(spinlock_irqsave)(&ctx->context.ohci->lock);
  2973. switch (base->type) {
  2974. case FW_ISO_CONTEXT_TRANSMIT:
  2975. return queue_iso_transmit(ctx, packet, buffer, payload);
  2976. case FW_ISO_CONTEXT_RECEIVE:
  2977. return queue_iso_packet_per_buffer(ctx, packet, buffer, payload);
  2978. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2979. return queue_iso_buffer_fill(ctx, packet, buffer, payload);
  2980. default:
  2981. return -ENOSYS;
  2982. }
  2983. }
  2984. static void ohci_flush_queue_iso(struct fw_iso_context *base)
  2985. {
  2986. struct context *ctx =
  2987. &container_of(base, struct iso_context, base)->context;
  2988. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  2989. }
  2990. static int ohci_flush_iso_completions(struct fw_iso_context *base)
  2991. {
  2992. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2993. int ret = 0;
  2994. if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) {
  2995. ohci_isoc_context_work(&base->work);
  2996. switch (base->type) {
  2997. case FW_ISO_CONTEXT_TRANSMIT:
  2998. case FW_ISO_CONTEXT_RECEIVE:
  2999. if (ctx->header_length != 0)
  3000. flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH);
  3001. break;
  3002. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  3003. if (ctx->mc_completed != 0)
  3004. flush_ir_buffer_fill(ctx);
  3005. break;
  3006. default:
  3007. ret = -ENOSYS;
  3008. }
  3009. clear_bit_unlock(0, &ctx->flushing_completions);
  3010. smp_mb__after_atomic();
  3011. }
  3012. return ret;
  3013. }
  3014. static const struct fw_card_driver ohci_driver = {
  3015. .enable = ohci_enable,
  3016. .read_phy_reg = ohci_read_phy_reg,
  3017. .update_phy_reg = ohci_update_phy_reg,
  3018. .set_config_rom = ohci_set_config_rom,
  3019. .send_request = ohci_send_request,
  3020. .send_response = ohci_send_response,
  3021. .cancel_packet = ohci_cancel_packet,
  3022. .enable_phys_dma = ohci_enable_phys_dma,
  3023. .read_csr = ohci_read_csr,
  3024. .write_csr = ohci_write_csr,
  3025. .allocate_iso_context = ohci_allocate_iso_context,
  3026. .free_iso_context = ohci_free_iso_context,
  3027. .set_iso_channels = ohci_set_iso_channels,
  3028. .queue_iso = ohci_queue_iso,
  3029. .flush_queue_iso = ohci_flush_queue_iso,
  3030. .flush_iso_completions = ohci_flush_iso_completions,
  3031. .start_iso = ohci_start_iso,
  3032. .stop_iso = ohci_stop_iso,
  3033. };
  3034. #ifdef CONFIG_PPC_PMAC
  3035. static void pmac_ohci_on(struct pci_dev *dev)
  3036. {
  3037. if (machine_is(powermac)) {
  3038. struct device_node *ofn = pci_device_to_OF_node(dev);
  3039. if (ofn) {
  3040. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
  3041. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  3042. }
  3043. }
  3044. }
  3045. static void pmac_ohci_off(struct pci_dev *dev)
  3046. {
  3047. if (machine_is(powermac)) {
  3048. struct device_node *ofn = pci_device_to_OF_node(dev);
  3049. if (ofn) {
  3050. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  3051. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
  3052. }
  3053. }
  3054. }
  3055. #else
  3056. static inline void pmac_ohci_on(struct pci_dev *dev) {}
  3057. static inline void pmac_ohci_off(struct pci_dev *dev) {}
  3058. #endif /* CONFIG_PPC_PMAC */
  3059. static void release_ohci(struct device *dev, void *data)
  3060. {
  3061. struct pci_dev *pdev = to_pci_dev(dev);
  3062. struct fw_ohci *ohci = pci_get_drvdata(pdev);
  3063. pmac_ohci_off(pdev);
  3064. ar_context_release(&ohci->ar_response_ctx);
  3065. ar_context_release(&ohci->ar_request_ctx);
  3066. dev_notice(dev, "removed fw-ohci device\n");
  3067. }
  3068. static int pci_probe(struct pci_dev *dev,
  3069. const struct pci_device_id *ent)
  3070. {
  3071. struct fw_ohci *ohci;
  3072. u32 bus_options, max_receive, link_speed, version;
  3073. u64 guid;
  3074. int i, flags, irq, err;
  3075. size_t size;
  3076. if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) {
  3077. dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n");
  3078. return -ENOSYS;
  3079. }
  3080. ohci = devres_alloc(release_ohci, sizeof(*ohci), GFP_KERNEL);
  3081. if (ohci == NULL)
  3082. return -ENOMEM;
  3083. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  3084. pci_set_drvdata(dev, ohci);
  3085. pmac_ohci_on(dev);
  3086. devres_add(&dev->dev, ohci);
  3087. err = pcim_enable_device(dev);
  3088. if (err) {
  3089. dev_err(&dev->dev, "failed to enable OHCI hardware\n");
  3090. return err;
  3091. }
  3092. pci_set_master(dev);
  3093. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  3094. spin_lock_init(&ohci->lock);
  3095. mutex_init(&ohci->phy_reg_mutex);
  3096. INIT_WORK(&ohci->bus_reset_work, bus_reset_work);
  3097. if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM) ||
  3098. pci_resource_len(dev, 0) < OHCI1394_REGISTER_SIZE) {
  3099. ohci_err(ohci, "invalid MMIO resource\n");
  3100. return -ENXIO;
  3101. }
  3102. err = pcim_iomap_regions(dev, 1 << 0, ohci_driver_name);
  3103. if (err) {
  3104. ohci_err(ohci, "request and map MMIO resource unavailable\n");
  3105. return -ENXIO;
  3106. }
  3107. ohci->registers = pcim_iomap_table(dev)[0];
  3108. for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++)
  3109. if ((ohci_quirks[i].vendor == dev->vendor) &&
  3110. (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID ||
  3111. ohci_quirks[i].device == dev->device) &&
  3112. (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID ||
  3113. ohci_quirks[i].revision >= dev->revision)) {
  3114. ohci->quirks = ohci_quirks[i].flags;
  3115. break;
  3116. }
  3117. if (param_quirks)
  3118. ohci->quirks = param_quirks;
  3119. if (detect_vt630x_with_asm1083_on_amd_ryzen_machine(dev))
  3120. ohci->quirks |= QUIRK_REBOOT_BY_CYCLE_TIMER_READ;
  3121. /*
  3122. * Because dma_alloc_coherent() allocates at least one page,
  3123. * we save space by using a common buffer for the AR request/
  3124. * response descriptors and the self IDs buffer.
  3125. */
  3126. BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4);
  3127. BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2);
  3128. ohci->misc_buffer = dmam_alloc_coherent(&dev->dev, PAGE_SIZE, &ohci->misc_buffer_bus,
  3129. GFP_KERNEL);
  3130. if (!ohci->misc_buffer)
  3131. return -ENOMEM;
  3132. err = ar_context_init(&ohci->ar_request_ctx, ohci, 0,
  3133. OHCI1394_AsReqRcvContextControlSet);
  3134. if (err < 0)
  3135. return err;
  3136. err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4,
  3137. OHCI1394_AsRspRcvContextControlSet);
  3138. if (err < 0)
  3139. return err;
  3140. err = context_init(&ohci->at_request_ctx, ohci,
  3141. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  3142. if (err < 0)
  3143. return err;
  3144. err = context_init(&ohci->at_response_ctx, ohci,
  3145. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  3146. if (err < 0)
  3147. return err;
  3148. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  3149. ohci->ir_context_channels = ~0ULL;
  3150. ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  3151. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  3152. ohci->ir_context_mask = ohci->ir_context_support;
  3153. ohci->n_ir = hweight32(ohci->ir_context_mask);
  3154. size = sizeof(struct iso_context) * ohci->n_ir;
  3155. ohci->ir_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL);
  3156. if (!ohci->ir_context_list)
  3157. return -ENOMEM;
  3158. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  3159. ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  3160. /* JMicron JMB38x often shows 0 at first read, just ignore it */
  3161. if (!ohci->it_context_support) {
  3162. ohci_notice(ohci, "overriding IsoXmitIntMask\n");
  3163. ohci->it_context_support = 0xf;
  3164. }
  3165. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  3166. ohci->it_context_mask = ohci->it_context_support;
  3167. ohci->n_it = hweight32(ohci->it_context_mask);
  3168. size = sizeof(struct iso_context) * ohci->n_it;
  3169. ohci->it_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL);
  3170. if (!ohci->it_context_list)
  3171. return -ENOMEM;
  3172. ohci->self_id = ohci->misc_buffer + PAGE_SIZE/2;
  3173. ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2;
  3174. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  3175. max_receive = (bus_options >> 12) & 0xf;
  3176. link_speed = bus_options & 0x7;
  3177. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  3178. reg_read(ohci, OHCI1394_GUIDLo);
  3179. flags = PCI_IRQ_INTX;
  3180. if (!(ohci->quirks & QUIRK_NO_MSI))
  3181. flags |= PCI_IRQ_MSI;
  3182. err = pci_alloc_irq_vectors(dev, 1, 1, flags);
  3183. if (err < 0)
  3184. return err;
  3185. irq = pci_irq_vector(dev, 0);
  3186. if (irq < 0) {
  3187. err = irq;
  3188. goto fail_msi;
  3189. }
  3190. err = request_threaded_irq(irq, irq_handler, NULL,
  3191. pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED, ohci_driver_name,
  3192. ohci);
  3193. if (err < 0) {
  3194. ohci_err(ohci, "failed to allocate interrupt %d\n", irq);
  3195. goto fail_msi;
  3196. }
  3197. err = fw_card_add(&ohci->card, max_receive, link_speed, guid, ohci->n_it + ohci->n_ir);
  3198. if (err)
  3199. goto fail_irq;
  3200. version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  3201. ohci_notice(ohci,
  3202. "added OHCI v%x.%x device as card %d, "
  3203. "%d IR + %d IT contexts, quirks 0x%x%s\n",
  3204. version >> 16, version & 0xff, ohci->card.index,
  3205. ohci->n_ir, ohci->n_it, ohci->quirks,
  3206. reg_read(ohci, OHCI1394_PhyUpperBound) ?
  3207. ", physUB" : "");
  3208. return 0;
  3209. fail_irq:
  3210. free_irq(irq, ohci);
  3211. fail_msi:
  3212. pci_free_irq_vectors(dev);
  3213. return err;
  3214. }
  3215. static void pci_remove(struct pci_dev *dev)
  3216. {
  3217. struct fw_ohci *ohci = pci_get_drvdata(dev);
  3218. int irq;
  3219. /*
  3220. * If the removal is happening from the suspend state, LPS won't be
  3221. * enabled and host registers (eg., IntMaskClear) won't be accessible.
  3222. */
  3223. if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) {
  3224. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  3225. flush_writes(ohci);
  3226. }
  3227. cancel_work_sync(&ohci->bus_reset_work);
  3228. fw_core_remove_card(&ohci->card);
  3229. /*
  3230. * FIXME: Fail all pending packets here, now that the upper
  3231. * layers can't queue any more.
  3232. */
  3233. software_reset(ohci);
  3234. irq = pci_irq_vector(dev, 0);
  3235. if (irq >= 0)
  3236. free_irq(irq, ohci);
  3237. pci_free_irq_vectors(dev);
  3238. dev_notice(&dev->dev, "removing fw-ohci device\n");
  3239. }
  3240. #ifdef CONFIG_PM
  3241. static int pci_suspend(struct pci_dev *dev, pm_message_t state)
  3242. {
  3243. struct fw_ohci *ohci = pci_get_drvdata(dev);
  3244. int err;
  3245. software_reset(ohci);
  3246. err = pci_save_state(dev);
  3247. if (err) {
  3248. ohci_err(ohci, "pci_save_state failed\n");
  3249. return err;
  3250. }
  3251. err = pci_set_power_state(dev, pci_choose_state(dev, state));
  3252. if (err)
  3253. ohci_err(ohci, "pci_set_power_state failed with %d\n", err);
  3254. pmac_ohci_off(dev);
  3255. return 0;
  3256. }
  3257. static int pci_resume(struct pci_dev *dev)
  3258. {
  3259. struct fw_ohci *ohci = pci_get_drvdata(dev);
  3260. int err;
  3261. pmac_ohci_on(dev);
  3262. pci_set_power_state(dev, PCI_D0);
  3263. pci_restore_state(dev);
  3264. err = pci_enable_device(dev);
  3265. if (err) {
  3266. ohci_err(ohci, "pci_enable_device failed\n");
  3267. return err;
  3268. }
  3269. /* Some systems don't setup GUID register on resume from ram */
  3270. if (!reg_read(ohci, OHCI1394_GUIDLo) &&
  3271. !reg_read(ohci, OHCI1394_GUIDHi)) {
  3272. reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid);
  3273. reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32));
  3274. }
  3275. err = ohci_enable(&ohci->card, NULL, 0);
  3276. if (err)
  3277. return err;
  3278. ohci_resume_iso_dma(ohci);
  3279. return 0;
  3280. }
  3281. #endif
  3282. static const struct pci_device_id pci_table[] = {
  3283. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  3284. { }
  3285. };
  3286. MODULE_DEVICE_TABLE(pci, pci_table);
  3287. static struct pci_driver fw_ohci_pci_driver = {
  3288. .name = ohci_driver_name,
  3289. .id_table = pci_table,
  3290. .probe = pci_probe,
  3291. .remove = pci_remove,
  3292. #ifdef CONFIG_PM
  3293. .resume = pci_resume,
  3294. .suspend = pci_suspend,
  3295. #endif
  3296. };
  3297. static int __init fw_ohci_init(void)
  3298. {
  3299. selfid_workqueue = alloc_workqueue(KBUILD_MODNAME, WQ_MEM_RECLAIM, 0);
  3300. if (!selfid_workqueue)
  3301. return -ENOMEM;
  3302. return pci_register_driver(&fw_ohci_pci_driver);
  3303. }
  3304. static void __exit fw_ohci_cleanup(void)
  3305. {
  3306. pci_unregister_driver(&fw_ohci_pci_driver);
  3307. destroy_workqueue(selfid_workqueue);
  3308. }
  3309. module_init(fw_ohci_init);
  3310. module_exit(fw_ohci_cleanup);
  3311. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  3312. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  3313. MODULE_LICENSE("GPL");
  3314. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  3315. MODULE_ALIAS("ohci1394");