ohci.c 106 KB

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