floppy.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620
  1. /*
  2. * linux/drivers/block/floppy.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 1993, 1994 Alain Knaff
  6. * Copyright (C) 1998 Alan Cox
  7. */
  8. /*
  9. * 02.12.91 - Changed to static variables to indicate need for reset
  10. * and recalibrate. This makes some things easier (output_byte reset
  11. * checking etc), and means less interrupt jumping in case of errors,
  12. * so the code is hopefully easier to understand.
  13. */
  14. /*
  15. * This file is certainly a mess. I've tried my best to get it working,
  16. * but I don't like programming floppies, and I have only one anyway.
  17. * Urgel. I should check for more errors, and do more graceful error
  18. * recovery. Seems there are problems with several drives. I've tried to
  19. * correct them. No promises.
  20. */
  21. /*
  22. * As with hd.c, all routines within this file can (and will) be called
  23. * by interrupts, so extreme caution is needed. A hardware interrupt
  24. * handler may not sleep, or a kernel panic will happen. Thus I cannot
  25. * call "floppy-on" directly, but have to set a special timer interrupt
  26. * etc.
  27. */
  28. /*
  29. * 28.02.92 - made track-buffering routines, based on the routines written
  30. * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
  31. */
  32. /*
  33. * Automatic floppy-detection and formatting written by Werner Almesberger
  34. * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
  35. * the floppy-change signal detection.
  36. */
  37. /*
  38. * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
  39. * FDC data overrun bug, added some preliminary stuff for vertical
  40. * recording support.
  41. *
  42. * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
  43. *
  44. * TODO: Errors are still not counted properly.
  45. */
  46. /* 1992/9/20
  47. * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
  48. * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
  49. * Christoph H. Hochst\"atter.
  50. * I have fixed the shift values to the ones I always use. Maybe a new
  51. * ioctl() should be created to be able to modify them.
  52. * There is a bug in the driver that makes it impossible to format a
  53. * floppy as the first thing after bootup.
  54. */
  55. /*
  56. * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
  57. * this helped the floppy driver as well. Much cleaner, and still seems to
  58. * work.
  59. */
  60. /* 1994/6/24 --bbroad-- added the floppy table entries and made
  61. * minor modifications to allow 2.88 floppies to be run.
  62. */
  63. /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
  64. * disk types.
  65. */
  66. /*
  67. * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
  68. * format bug fixes, but unfortunately some new bugs too...
  69. */
  70. /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
  71. * errors to allow safe writing by specialized programs.
  72. */
  73. /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
  74. * by defining bit 1 of the "stretch" parameter to mean put sectors on the
  75. * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
  76. * drives are "upside-down").
  77. */
  78. /*
  79. * 1995/8/26 -- Andreas Busse -- added Mips support.
  80. */
  81. /*
  82. * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
  83. * features to asm/floppy.h.
  84. */
  85. /*
  86. * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
  87. */
  88. /*
  89. * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
  90. * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
  91. * use of '0' for NULL.
  92. */
  93. /*
  94. * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
  95. * failures.
  96. */
  97. /*
  98. * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
  99. */
  100. /*
  101. * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
  102. * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
  103. * being used to store jiffies, which are unsigned longs).
  104. */
  105. /*
  106. * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  107. * - get rid of check_region
  108. * - s/suser/capable/
  109. */
  110. /*
  111. * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
  112. * floppy controller (lingering task on list after module is gone... boom.)
  113. */
  114. /*
  115. * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
  116. * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
  117. * requires many non-obvious changes in arch dependent code.
  118. */
  119. /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
  120. * Better audit of register_blkdev.
  121. */
  122. #undef FLOPPY_SILENT_DCL_CLEAR
  123. #define REALLY_SLOW_IO
  124. #define DEBUGT 2
  125. #define DPRINT(format, args...) \
  126. pr_info("floppy%d: " format, current_drive, ##args)
  127. #define DCL_DEBUG /* debug disk change line */
  128. #ifdef DCL_DEBUG
  129. #define debug_dcl(test, fmt, args...) \
  130. do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
  131. #else
  132. #define debug_dcl(test, fmt, args...) \
  133. do { if (0) DPRINT(fmt, ##args); } while (0)
  134. #endif
  135. /* do print messages for unexpected interrupts */
  136. static int print_unex = 1;
  137. #include <linux/module.h>
  138. #include <linux/sched.h>
  139. #include <linux/fs.h>
  140. #include <linux/kernel.h>
  141. #include <linux/timer.h>
  142. #include <linux/workqueue.h>
  143. #define FDPATCHES
  144. #include <linux/fdreg.h>
  145. #include <linux/fd.h>
  146. #include <linux/hdreg.h>
  147. #include <linux/errno.h>
  148. #include <linux/slab.h>
  149. #include <linux/mm.h>
  150. #include <linux/bio.h>
  151. #include <linux/string.h>
  152. #include <linux/jiffies.h>
  153. #include <linux/fcntl.h>
  154. #include <linux/delay.h>
  155. #include <linux/mc146818rtc.h> /* CMOS defines */
  156. #include <linux/ioport.h>
  157. #include <linux/interrupt.h>
  158. #include <linux/init.h>
  159. #include <linux/platform_device.h>
  160. #include <linux/mod_devicetable.h>
  161. #include <linux/buffer_head.h> /* for invalidate_buffers() */
  162. #include <linux/mutex.h>
  163. #include <linux/io.h>
  164. #include <linux/uaccess.h>
  165. /*
  166. * PS/2 floppies have much slower step rates than regular floppies.
  167. * It's been recommended that take about 1/4 of the default speed
  168. * in some more extreme cases.
  169. */
  170. static DEFINE_MUTEX(floppy_mutex);
  171. static int slow_floppy;
  172. #include <asm/dma.h>
  173. #include <asm/irq.h>
  174. #include <asm/system.h>
  175. static int FLOPPY_IRQ = 6;
  176. static int FLOPPY_DMA = 2;
  177. static int can_use_virtual_dma = 2;
  178. /* =======
  179. * can use virtual DMA:
  180. * 0 = use of virtual DMA disallowed by config
  181. * 1 = use of virtual DMA prescribed by config
  182. * 2 = no virtual DMA preference configured. By default try hard DMA,
  183. * but fall back on virtual DMA when not enough memory available
  184. */
  185. static int use_virtual_dma;
  186. /* =======
  187. * use virtual DMA
  188. * 0 using hard DMA
  189. * 1 using virtual DMA
  190. * This variable is set to virtual when a DMA mem problem arises, and
  191. * reset back in floppy_grab_irq_and_dma.
  192. * It is not safe to reset it in other circumstances, because the floppy
  193. * driver may have several buffers in use at once, and we do currently not
  194. * record each buffers capabilities
  195. */
  196. static DEFINE_SPINLOCK(floppy_lock);
  197. static unsigned short virtual_dma_port = 0x3f0;
  198. irqreturn_t floppy_interrupt(int irq, void *dev_id);
  199. static int set_dor(int fdc, char mask, char data);
  200. #define K_64 0x10000 /* 64KB */
  201. /* the following is the mask of allowed drives. By default units 2 and
  202. * 3 of both floppy controllers are disabled, because switching on the
  203. * motor of these drives causes system hangs on some PCI computers. drive
  204. * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
  205. * a drive is allowed.
  206. *
  207. * NOTE: This must come before we include the arch floppy header because
  208. * some ports reference this variable from there. -DaveM
  209. */
  210. static int allowed_drive_mask = 0x33;
  211. #include <asm/floppy.h>
  212. static int irqdma_allocated;
  213. #include <linux/blkdev.h>
  214. #include <linux/blkpg.h>
  215. #include <linux/cdrom.h> /* for the compatibility eject ioctl */
  216. #include <linux/completion.h>
  217. static struct request *current_req;
  218. static void do_fd_request(struct request_queue *q);
  219. static int set_next_request(void);
  220. #ifndef fd_get_dma_residue
  221. #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
  222. #endif
  223. /* Dma Memory related stuff */
  224. #ifndef fd_dma_mem_free
  225. #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
  226. #endif
  227. #ifndef fd_dma_mem_alloc
  228. #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
  229. #endif
  230. static inline void fallback_on_nodma_alloc(char **addr, size_t l)
  231. {
  232. #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
  233. if (*addr)
  234. return; /* we have the memory */
  235. if (can_use_virtual_dma != 2)
  236. return; /* no fallback allowed */
  237. pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
  238. *addr = (char *)nodma_mem_alloc(l);
  239. #else
  240. return;
  241. #endif
  242. }
  243. /* End dma memory related stuff */
  244. static unsigned long fake_change;
  245. static bool initialized;
  246. #define ITYPE(x) (((x) >> 2) & 0x1f)
  247. #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
  248. #define UNIT(x) ((x) & 0x03) /* drive on fdc */
  249. #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
  250. /* reverse mapping from unit and fdc to drive */
  251. #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
  252. #define DP (&drive_params[current_drive])
  253. #define DRS (&drive_state[current_drive])
  254. #define DRWE (&write_errors[current_drive])
  255. #define FDCS (&fdc_state[fdc])
  256. #define UDP (&drive_params[drive])
  257. #define UDRS (&drive_state[drive])
  258. #define UDRWE (&write_errors[drive])
  259. #define UFDCS (&fdc_state[FDC(drive)])
  260. #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
  261. #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
  262. /* read/write */
  263. #define COMMAND (raw_cmd->cmd[0])
  264. #define DR_SELECT (raw_cmd->cmd[1])
  265. #define TRACK (raw_cmd->cmd[2])
  266. #define HEAD (raw_cmd->cmd[3])
  267. #define SECTOR (raw_cmd->cmd[4])
  268. #define SIZECODE (raw_cmd->cmd[5])
  269. #define SECT_PER_TRACK (raw_cmd->cmd[6])
  270. #define GAP (raw_cmd->cmd[7])
  271. #define SIZECODE2 (raw_cmd->cmd[8])
  272. #define NR_RW 9
  273. /* format */
  274. #define F_SIZECODE (raw_cmd->cmd[2])
  275. #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
  276. #define F_GAP (raw_cmd->cmd[4])
  277. #define F_FILL (raw_cmd->cmd[5])
  278. #define NR_F 6
  279. /*
  280. * Maximum disk size (in kilobytes).
  281. * This default is used whenever the current disk size is unknown.
  282. * [Now it is rather a minimum]
  283. */
  284. #define MAX_DISK_SIZE 4 /* 3984 */
  285. /*
  286. * globals used by 'result()'
  287. */
  288. #define MAX_REPLIES 16
  289. static unsigned char reply_buffer[MAX_REPLIES];
  290. static int inr; /* size of reply buffer, when called from interrupt */
  291. #define ST0 (reply_buffer[0])
  292. #define ST1 (reply_buffer[1])
  293. #define ST2 (reply_buffer[2])
  294. #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
  295. #define R_TRACK (reply_buffer[3])
  296. #define R_HEAD (reply_buffer[4])
  297. #define R_SECTOR (reply_buffer[5])
  298. #define R_SIZECODE (reply_buffer[6])
  299. #define SEL_DLY (2 * HZ / 100)
  300. /*
  301. * this struct defines the different floppy drive types.
  302. */
  303. static struct {
  304. struct floppy_drive_params params;
  305. const char *name; /* name printed while booting */
  306. } default_drive_params[] = {
  307. /* NOTE: the time values in jiffies should be in msec!
  308. CMOS drive type
  309. | Maximum data rate supported by drive type
  310. | | Head load time, msec
  311. | | | Head unload time, msec (not used)
  312. | | | | Step rate interval, usec
  313. | | | | | Time needed for spinup time (jiffies)
  314. | | | | | | Timeout for spinning down (jiffies)
  315. | | | | | | | Spindown offset (where disk stops)
  316. | | | | | | | | Select delay
  317. | | | | | | | | | RPS
  318. | | | | | | | | | | Max number of tracks
  319. | | | | | | | | | | | Interrupt timeout
  320. | | | | | | | | | | | | Max nonintlv. sectors
  321. | | | | | | | | | | | | | -Max Errors- flags */
  322. {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
  323. 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
  324. {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
  325. 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
  326. {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
  327. 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
  328. {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  329. 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
  330. {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  331. 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
  332. {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  333. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
  334. {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  335. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
  336. /* | --autodetected formats--- | | |
  337. * read_track | | Name printed when booting
  338. * | Native format
  339. * Frequency of disk change checks */
  340. };
  341. static struct floppy_drive_params drive_params[N_DRIVE];
  342. static struct floppy_drive_struct drive_state[N_DRIVE];
  343. static struct floppy_write_errors write_errors[N_DRIVE];
  344. static struct timer_list motor_off_timer[N_DRIVE];
  345. static struct gendisk *disks[N_DRIVE];
  346. static struct block_device *opened_bdev[N_DRIVE];
  347. static DEFINE_MUTEX(open_lock);
  348. static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
  349. static int fdc_queue;
  350. /*
  351. * This struct defines the different floppy types.
  352. *
  353. * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
  354. * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
  355. * tells if the disk is in Commodore 1581 format, which means side 0 sectors
  356. * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
  357. * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
  358. * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
  359. * side 0 is on physical side 0 (but with the misnamed sector IDs).
  360. * 'stretch' should probably be renamed to something more general, like
  361. * 'options'.
  362. *
  363. * Bits 2 through 9 of 'stretch' tell the number of the first sector.
  364. * The LSB (bit 2) is flipped. For most disks, the first sector
  365. * is 1 (represented by 0x00<<2). For some CP/M and music sampler
  366. * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
  367. * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
  368. *
  369. * Other parameters should be self-explanatory (see also setfdprm(8)).
  370. */
  371. /*
  372. Size
  373. | Sectors per track
  374. | | Head
  375. | | | Tracks
  376. | | | | Stretch
  377. | | | | | Gap 1 size
  378. | | | | | | Data rate, | 0x40 for perp
  379. | | | | | | | Spec1 (stepping rate, head unload
  380. | | | | | | | | /fmt gap (gap2) */
  381. static struct floppy_struct floppy_type[32] = {
  382. { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
  383. { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
  384. { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
  385. { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
  386. { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
  387. { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
  388. { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
  389. { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
  390. { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
  391. { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
  392. { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
  393. { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
  394. { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
  395. { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
  396. { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
  397. { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
  398. { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
  399. { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
  400. { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
  401. { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
  402. { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
  403. { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
  404. { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
  405. { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
  406. { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
  407. { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
  408. { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
  409. { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
  410. { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
  411. { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
  412. { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
  413. { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
  414. };
  415. #define SECTSIZE (_FD_SECTSIZE(*floppy))
  416. /* Auto-detection: Disk type used until the next media change occurs. */
  417. static struct floppy_struct *current_type[N_DRIVE];
  418. /*
  419. * User-provided type information. current_type points to
  420. * the respective entry of this array.
  421. */
  422. static struct floppy_struct user_params[N_DRIVE];
  423. static sector_t floppy_sizes[256];
  424. static char floppy_device_name[] = "floppy";
  425. /*
  426. * The driver is trying to determine the correct media format
  427. * while probing is set. rw_interrupt() clears it after a
  428. * successful access.
  429. */
  430. static int probing;
  431. /* Synchronization of FDC access. */
  432. #define FD_COMMAND_NONE -1
  433. #define FD_COMMAND_ERROR 2
  434. #define FD_COMMAND_OKAY 3
  435. static volatile int command_status = FD_COMMAND_NONE;
  436. static unsigned long fdc_busy;
  437. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  438. static DECLARE_WAIT_QUEUE_HEAD(command_done);
  439. /* Errors during formatting are counted here. */
  440. static int format_errors;
  441. /* Format request descriptor. */
  442. static struct format_descr format_req;
  443. /*
  444. * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
  445. * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
  446. * H is head unload time (1=16ms, 2=32ms, etc)
  447. */
  448. /*
  449. * Track buffer
  450. * Because these are written to by the DMA controller, they must
  451. * not contain a 64k byte boundary crossing, or data will be
  452. * corrupted/lost.
  453. */
  454. static char *floppy_track_buffer;
  455. static int max_buffer_sectors;
  456. static int *errors;
  457. typedef void (*done_f)(int);
  458. static const struct cont_t {
  459. void (*interrupt)(void);
  460. /* this is called after the interrupt of the
  461. * main command */
  462. void (*redo)(void); /* this is called to retry the operation */
  463. void (*error)(void); /* this is called to tally an error */
  464. done_f done; /* this is called to say if the operation has
  465. * succeeded/failed */
  466. } *cont;
  467. static void floppy_ready(void);
  468. static void floppy_start(void);
  469. static void process_fd_request(void);
  470. static void recalibrate_floppy(void);
  471. static void floppy_shutdown(unsigned long);
  472. static int floppy_request_regions(int);
  473. static void floppy_release_regions(int);
  474. static int floppy_grab_irq_and_dma(void);
  475. static void floppy_release_irq_and_dma(void);
  476. /*
  477. * The "reset" variable should be tested whenever an interrupt is scheduled,
  478. * after the commands have been sent. This is to ensure that the driver doesn't
  479. * get wedged when the interrupt doesn't come because of a failed command.
  480. * reset doesn't need to be tested before sending commands, because
  481. * output_byte is automatically disabled when reset is set.
  482. */
  483. static void reset_fdc(void);
  484. /*
  485. * These are global variables, as that's the easiest way to give
  486. * information to interrupts. They are the data used for the current
  487. * request.
  488. */
  489. #define NO_TRACK -1
  490. #define NEED_1_RECAL -2
  491. #define NEED_2_RECAL -3
  492. static atomic_t usage_count = ATOMIC_INIT(0);
  493. /* buffer related variables */
  494. static int buffer_track = -1;
  495. static int buffer_drive = -1;
  496. static int buffer_min = -1;
  497. static int buffer_max = -1;
  498. /* fdc related variables, should end up in a struct */
  499. static struct floppy_fdc_state fdc_state[N_FDC];
  500. static int fdc; /* current fdc */
  501. static struct floppy_struct *_floppy = floppy_type;
  502. static unsigned char current_drive;
  503. static long current_count_sectors;
  504. static unsigned char fsector_t; /* sector in track */
  505. static unsigned char in_sector_offset; /* offset within physical sector,
  506. * expressed in units of 512 bytes */
  507. static inline bool drive_no_geom(int drive)
  508. {
  509. return !current_type[drive] && !ITYPE(UDRS->fd_device);
  510. }
  511. #ifndef fd_eject
  512. static inline int fd_eject(int drive)
  513. {
  514. return -EINVAL;
  515. }
  516. #endif
  517. /*
  518. * Debugging
  519. * =========
  520. */
  521. #ifdef DEBUGT
  522. static long unsigned debugtimer;
  523. static inline void set_debugt(void)
  524. {
  525. debugtimer = jiffies;
  526. }
  527. static inline void debugt(const char *func, const char *msg)
  528. {
  529. if (DP->flags & DEBUGT)
  530. pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
  531. }
  532. #else
  533. static inline void set_debugt(void) { }
  534. static inline void debugt(const char *func, const char *msg) { }
  535. #endif /* DEBUGT */
  536. typedef void (*timeout_fn)(unsigned long);
  537. static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
  538. static const char *timeout_message;
  539. static void is_alive(const char *func, const char *message)
  540. {
  541. /* this routine checks whether the floppy driver is "alive" */
  542. if (test_bit(0, &fdc_busy) && command_status < 2 &&
  543. !timer_pending(&fd_timeout)) {
  544. DPRINT("%s: timeout handler died. %s\n", func, message);
  545. }
  546. }
  547. static void (*do_floppy)(void) = NULL;
  548. #define OLOGSIZE 20
  549. static void (*lasthandler)(void);
  550. static unsigned long interruptjiffies;
  551. static unsigned long resultjiffies;
  552. static int resultsize;
  553. static unsigned long lastredo;
  554. static struct output_log {
  555. unsigned char data;
  556. unsigned char status;
  557. unsigned long jiffies;
  558. } output_log[OLOGSIZE];
  559. static int output_log_pos;
  560. #define current_reqD -1
  561. #define MAXTIMEOUT -2
  562. static void __reschedule_timeout(int drive, const char *message)
  563. {
  564. if (drive == current_reqD)
  565. drive = current_drive;
  566. del_timer(&fd_timeout);
  567. if (drive < 0 || drive >= N_DRIVE) {
  568. fd_timeout.expires = jiffies + 20UL * HZ;
  569. drive = 0;
  570. } else
  571. fd_timeout.expires = jiffies + UDP->timeout;
  572. add_timer(&fd_timeout);
  573. if (UDP->flags & FD_DEBUG)
  574. DPRINT("reschedule timeout %s\n", message);
  575. timeout_message = message;
  576. }
  577. static void reschedule_timeout(int drive, const char *message)
  578. {
  579. unsigned long flags;
  580. spin_lock_irqsave(&floppy_lock, flags);
  581. __reschedule_timeout(drive, message);
  582. spin_unlock_irqrestore(&floppy_lock, flags);
  583. }
  584. #define INFBOUND(a, b) (a) = max_t(int, a, b)
  585. #define SUPBOUND(a, b) (a) = min_t(int, a, b)
  586. /*
  587. * Bottom half floppy driver.
  588. * ==========================
  589. *
  590. * This part of the file contains the code talking directly to the hardware,
  591. * and also the main service loop (seek-configure-spinup-command)
  592. */
  593. /*
  594. * disk change.
  595. * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
  596. * and the last_checked date.
  597. *
  598. * last_checked is the date of the last check which showed 'no disk change'
  599. * FD_DISK_CHANGE is set under two conditions:
  600. * 1. The floppy has been changed after some i/o to that floppy already
  601. * took place.
  602. * 2. No floppy disk is in the drive. This is done in order to ensure that
  603. * requests are quickly flushed in case there is no disk in the drive. It
  604. * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
  605. * the drive.
  606. *
  607. * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
  608. * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
  609. * each seek. If a disk is present, the disk change line should also be
  610. * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
  611. * change line is set, this means either that no disk is in the drive, or
  612. * that it has been removed since the last seek.
  613. *
  614. * This means that we really have a third possibility too:
  615. * The floppy has been changed after the last seek.
  616. */
  617. static int disk_change(int drive)
  618. {
  619. int fdc = FDC(drive);
  620. if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
  621. DPRINT("WARNING disk change called early\n");
  622. if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
  623. (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
  624. DPRINT("probing disk change on unselected drive\n");
  625. DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
  626. (unsigned int)FDCS->dor);
  627. }
  628. debug_dcl(UDP->flags,
  629. "checking disk change line for drive %d\n", drive);
  630. debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
  631. debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
  632. debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
  633. if (UDP->flags & FD_BROKEN_DCL)
  634. return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  635. if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
  636. set_bit(FD_VERIFY_BIT, &UDRS->flags);
  637. /* verify write protection */
  638. if (UDRS->maxblock) /* mark it changed */
  639. set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  640. /* invalidate its geometry */
  641. if (UDRS->keep_data >= 0) {
  642. if ((UDP->flags & FTD_MSG) &&
  643. current_type[drive] != NULL)
  644. DPRINT("Disk type is undefined after disk change\n");
  645. current_type[drive] = NULL;
  646. floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
  647. }
  648. return 1;
  649. } else {
  650. UDRS->last_checked = jiffies;
  651. clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
  652. }
  653. return 0;
  654. }
  655. static inline int is_selected(int dor, int unit)
  656. {
  657. return ((dor & (0x10 << unit)) && (dor & 3) == unit);
  658. }
  659. static bool is_ready_state(int status)
  660. {
  661. int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
  662. return state == STATUS_READY;
  663. }
  664. static int set_dor(int fdc, char mask, char data)
  665. {
  666. unsigned char unit;
  667. unsigned char drive;
  668. unsigned char newdor;
  669. unsigned char olddor;
  670. if (FDCS->address == -1)
  671. return -1;
  672. olddor = FDCS->dor;
  673. newdor = (olddor & mask) | data;
  674. if (newdor != olddor) {
  675. unit = olddor & 0x3;
  676. if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
  677. drive = REVDRIVE(fdc, unit);
  678. debug_dcl(UDP->flags,
  679. "calling disk change from set_dor\n");
  680. disk_change(drive);
  681. }
  682. FDCS->dor = newdor;
  683. fd_outb(newdor, FD_DOR);
  684. unit = newdor & 0x3;
  685. if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
  686. drive = REVDRIVE(fdc, unit);
  687. UDRS->select_date = jiffies;
  688. }
  689. }
  690. return olddor;
  691. }
  692. static void twaddle(void)
  693. {
  694. if (DP->select_delay)
  695. return;
  696. fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
  697. fd_outb(FDCS->dor, FD_DOR);
  698. DRS->select_date = jiffies;
  699. }
  700. /*
  701. * Reset all driver information about the current fdc.
  702. * This is needed after a reset, and after a raw command.
  703. */
  704. static void reset_fdc_info(int mode)
  705. {
  706. int drive;
  707. FDCS->spec1 = FDCS->spec2 = -1;
  708. FDCS->need_configure = 1;
  709. FDCS->perp_mode = 1;
  710. FDCS->rawcmd = 0;
  711. for (drive = 0; drive < N_DRIVE; drive++)
  712. if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
  713. UDRS->track = NEED_2_RECAL;
  714. }
  715. /* selects the fdc and drive, and enables the fdc's input/dma. */
  716. static void set_fdc(int drive)
  717. {
  718. if (drive >= 0 && drive < N_DRIVE) {
  719. fdc = FDC(drive);
  720. current_drive = drive;
  721. }
  722. if (fdc != 1 && fdc != 0) {
  723. pr_info("bad fdc value\n");
  724. return;
  725. }
  726. set_dor(fdc, ~0, 8);
  727. #if N_FDC > 1
  728. set_dor(1 - fdc, ~8, 0);
  729. #endif
  730. if (FDCS->rawcmd == 2)
  731. reset_fdc_info(1);
  732. if (fd_inb(FD_STATUS) != STATUS_READY)
  733. FDCS->reset = 1;
  734. }
  735. /* locks the driver */
  736. static int lock_fdc(int drive, bool interruptible)
  737. {
  738. if (WARN(atomic_read(&usage_count) == 0,
  739. "Trying to lock fdc while usage count=0\n"))
  740. return -1;
  741. if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
  742. return -EINTR;
  743. command_status = FD_COMMAND_NONE;
  744. __reschedule_timeout(drive, "lock fdc");
  745. set_fdc(drive);
  746. return 0;
  747. }
  748. /* unlocks the driver */
  749. static void unlock_fdc(void)
  750. {
  751. unsigned long flags;
  752. raw_cmd = NULL;
  753. if (!test_bit(0, &fdc_busy))
  754. DPRINT("FDC access conflict!\n");
  755. if (do_floppy)
  756. DPRINT("device interrupt still active at FDC release: %pf!\n",
  757. do_floppy);
  758. command_status = FD_COMMAND_NONE;
  759. spin_lock_irqsave(&floppy_lock, flags);
  760. del_timer(&fd_timeout);
  761. cont = NULL;
  762. clear_bit(0, &fdc_busy);
  763. if (current_req || set_next_request())
  764. do_fd_request(current_req->q);
  765. spin_unlock_irqrestore(&floppy_lock, flags);
  766. wake_up(&fdc_wait);
  767. }
  768. /* switches the motor off after a given timeout */
  769. static void motor_off_callback(unsigned long nr)
  770. {
  771. unsigned char mask = ~(0x10 << UNIT(nr));
  772. set_dor(FDC(nr), mask, 0);
  773. }
  774. /* schedules motor off */
  775. static void floppy_off(unsigned int drive)
  776. {
  777. unsigned long volatile delta;
  778. int fdc = FDC(drive);
  779. if (!(FDCS->dor & (0x10 << UNIT(drive))))
  780. return;
  781. del_timer(motor_off_timer + drive);
  782. /* make spindle stop in a position which minimizes spinup time
  783. * next time */
  784. if (UDP->rps) {
  785. delta = jiffies - UDRS->first_read_date + HZ -
  786. UDP->spindown_offset;
  787. delta = ((delta * UDP->rps) % HZ) / UDP->rps;
  788. motor_off_timer[drive].expires =
  789. jiffies + UDP->spindown - delta;
  790. }
  791. add_timer(motor_off_timer + drive);
  792. }
  793. /*
  794. * cycle through all N_DRIVE floppy drives, for disk change testing.
  795. * stopping at current drive. This is done before any long operation, to
  796. * be sure to have up to date disk change information.
  797. */
  798. static void scandrives(void)
  799. {
  800. int i;
  801. int drive;
  802. int saved_drive;
  803. if (DP->select_delay)
  804. return;
  805. saved_drive = current_drive;
  806. for (i = 0; i < N_DRIVE; i++) {
  807. drive = (saved_drive + i + 1) % N_DRIVE;
  808. if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
  809. continue; /* skip closed drives */
  810. set_fdc(drive);
  811. if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
  812. (0x10 << UNIT(drive))))
  813. /* switch the motor off again, if it was off to
  814. * begin with */
  815. set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
  816. }
  817. set_fdc(saved_drive);
  818. }
  819. static void empty(void)
  820. {
  821. }
  822. static DECLARE_WORK(floppy_work, NULL);
  823. static void schedule_bh(void (*handler)(void))
  824. {
  825. PREPARE_WORK(&floppy_work, (work_func_t)handler);
  826. schedule_work(&floppy_work);
  827. }
  828. static DEFINE_TIMER(fd_timer, NULL, 0, 0);
  829. static void cancel_activity(void)
  830. {
  831. unsigned long flags;
  832. spin_lock_irqsave(&floppy_lock, flags);
  833. do_floppy = NULL;
  834. PREPARE_WORK(&floppy_work, (work_func_t)empty);
  835. del_timer(&fd_timer);
  836. spin_unlock_irqrestore(&floppy_lock, flags);
  837. }
  838. /* this function makes sure that the disk stays in the drive during the
  839. * transfer */
  840. static void fd_watchdog(void)
  841. {
  842. debug_dcl(DP->flags, "calling disk change from watchdog\n");
  843. if (disk_change(current_drive)) {
  844. DPRINT("disk removed during i/o\n");
  845. cancel_activity();
  846. cont->done(0);
  847. reset_fdc();
  848. } else {
  849. del_timer(&fd_timer);
  850. fd_timer.function = (timeout_fn)fd_watchdog;
  851. fd_timer.expires = jiffies + HZ / 10;
  852. add_timer(&fd_timer);
  853. }
  854. }
  855. static void main_command_interrupt(void)
  856. {
  857. del_timer(&fd_timer);
  858. cont->interrupt();
  859. }
  860. /* waits for a delay (spinup or select) to pass */
  861. static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
  862. {
  863. if (FDCS->reset) {
  864. reset_fdc(); /* do the reset during sleep to win time
  865. * if we don't need to sleep, it's a good
  866. * occasion anyways */
  867. return 1;
  868. }
  869. if (time_before(jiffies, delay)) {
  870. del_timer(&fd_timer);
  871. fd_timer.function = function;
  872. fd_timer.expires = delay;
  873. add_timer(&fd_timer);
  874. return 1;
  875. }
  876. return 0;
  877. }
  878. static DEFINE_SPINLOCK(floppy_hlt_lock);
  879. static int hlt_disabled;
  880. static void floppy_disable_hlt(void)
  881. {
  882. unsigned long flags;
  883. WARN_ONCE(1, "floppy_disable_hlt() scheduled for removal in 2012");
  884. spin_lock_irqsave(&floppy_hlt_lock, flags);
  885. if (!hlt_disabled) {
  886. hlt_disabled = 1;
  887. #ifdef HAVE_DISABLE_HLT
  888. disable_hlt();
  889. #endif
  890. }
  891. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  892. }
  893. static void floppy_enable_hlt(void)
  894. {
  895. unsigned long flags;
  896. spin_lock_irqsave(&floppy_hlt_lock, flags);
  897. if (hlt_disabled) {
  898. hlt_disabled = 0;
  899. #ifdef HAVE_DISABLE_HLT
  900. enable_hlt();
  901. #endif
  902. }
  903. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  904. }
  905. static void setup_DMA(void)
  906. {
  907. unsigned long f;
  908. if (raw_cmd->length == 0) {
  909. int i;
  910. pr_info("zero dma transfer size:");
  911. for (i = 0; i < raw_cmd->cmd_count; i++)
  912. pr_cont("%x,", raw_cmd->cmd[i]);
  913. pr_cont("\n");
  914. cont->done(0);
  915. FDCS->reset = 1;
  916. return;
  917. }
  918. if (((unsigned long)raw_cmd->kernel_data) % 512) {
  919. pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
  920. cont->done(0);
  921. FDCS->reset = 1;
  922. return;
  923. }
  924. f = claim_dma_lock();
  925. fd_disable_dma();
  926. #ifdef fd_dma_setup
  927. if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
  928. (raw_cmd->flags & FD_RAW_READ) ?
  929. DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
  930. release_dma_lock(f);
  931. cont->done(0);
  932. FDCS->reset = 1;
  933. return;
  934. }
  935. release_dma_lock(f);
  936. #else
  937. fd_clear_dma_ff();
  938. fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
  939. fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
  940. DMA_MODE_READ : DMA_MODE_WRITE);
  941. fd_set_dma_addr(raw_cmd->kernel_data);
  942. fd_set_dma_count(raw_cmd->length);
  943. virtual_dma_port = FDCS->address;
  944. fd_enable_dma();
  945. release_dma_lock(f);
  946. #endif
  947. floppy_disable_hlt();
  948. }
  949. static void show_floppy(void);
  950. /* waits until the fdc becomes ready */
  951. static int wait_til_ready(void)
  952. {
  953. int status;
  954. int counter;
  955. if (FDCS->reset)
  956. return -1;
  957. for (counter = 0; counter < 10000; counter++) {
  958. status = fd_inb(FD_STATUS);
  959. if (status & STATUS_READY)
  960. return status;
  961. }
  962. if (initialized) {
  963. DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
  964. show_floppy();
  965. }
  966. FDCS->reset = 1;
  967. return -1;
  968. }
  969. /* sends a command byte to the fdc */
  970. static int output_byte(char byte)
  971. {
  972. int status = wait_til_ready();
  973. if (status < 0)
  974. return -1;
  975. if (is_ready_state(status)) {
  976. fd_outb(byte, FD_DATA);
  977. output_log[output_log_pos].data = byte;
  978. output_log[output_log_pos].status = status;
  979. output_log[output_log_pos].jiffies = jiffies;
  980. output_log_pos = (output_log_pos + 1) % OLOGSIZE;
  981. return 0;
  982. }
  983. FDCS->reset = 1;
  984. if (initialized) {
  985. DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
  986. byte, fdc, status);
  987. show_floppy();
  988. }
  989. return -1;
  990. }
  991. /* gets the response from the fdc */
  992. static int result(void)
  993. {
  994. int i;
  995. int status = 0;
  996. for (i = 0; i < MAX_REPLIES; i++) {
  997. status = wait_til_ready();
  998. if (status < 0)
  999. break;
  1000. status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
  1001. if ((status & ~STATUS_BUSY) == STATUS_READY) {
  1002. resultjiffies = jiffies;
  1003. resultsize = i;
  1004. return i;
  1005. }
  1006. if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
  1007. reply_buffer[i] = fd_inb(FD_DATA);
  1008. else
  1009. break;
  1010. }
  1011. if (initialized) {
  1012. DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
  1013. fdc, status, i);
  1014. show_floppy();
  1015. }
  1016. FDCS->reset = 1;
  1017. return -1;
  1018. }
  1019. #define MORE_OUTPUT -2
  1020. /* does the fdc need more output? */
  1021. static int need_more_output(void)
  1022. {
  1023. int status = wait_til_ready();
  1024. if (status < 0)
  1025. return -1;
  1026. if (is_ready_state(status))
  1027. return MORE_OUTPUT;
  1028. return result();
  1029. }
  1030. /* Set perpendicular mode as required, based on data rate, if supported.
  1031. * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
  1032. */
  1033. static void perpendicular_mode(void)
  1034. {
  1035. unsigned char perp_mode;
  1036. if (raw_cmd->rate & 0x40) {
  1037. switch (raw_cmd->rate & 3) {
  1038. case 0:
  1039. perp_mode = 2;
  1040. break;
  1041. case 3:
  1042. perp_mode = 3;
  1043. break;
  1044. default:
  1045. DPRINT("Invalid data rate for perpendicular mode!\n");
  1046. cont->done(0);
  1047. FDCS->reset = 1;
  1048. /*
  1049. * convenient way to return to
  1050. * redo without too much hassle
  1051. * (deep stack et al.)
  1052. */
  1053. return;
  1054. }
  1055. } else
  1056. perp_mode = 0;
  1057. if (FDCS->perp_mode == perp_mode)
  1058. return;
  1059. if (FDCS->version >= FDC_82077_ORIG) {
  1060. output_byte(FD_PERPENDICULAR);
  1061. output_byte(perp_mode);
  1062. FDCS->perp_mode = perp_mode;
  1063. } else if (perp_mode) {
  1064. DPRINT("perpendicular mode not supported by this FDC.\n");
  1065. }
  1066. } /* perpendicular_mode */
  1067. static int fifo_depth = 0xa;
  1068. static int no_fifo;
  1069. static int fdc_configure(void)
  1070. {
  1071. /* Turn on FIFO */
  1072. output_byte(FD_CONFIGURE);
  1073. if (need_more_output() != MORE_OUTPUT)
  1074. return 0;
  1075. output_byte(0);
  1076. output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
  1077. output_byte(0); /* pre-compensation from track
  1078. 0 upwards */
  1079. return 1;
  1080. }
  1081. #define NOMINAL_DTR 500
  1082. /* Issue a "SPECIFY" command to set the step rate time, head unload time,
  1083. * head load time, and DMA disable flag to values needed by floppy.
  1084. *
  1085. * The value "dtr" is the data transfer rate in Kbps. It is needed
  1086. * to account for the data rate-based scaling done by the 82072 and 82077
  1087. * FDC types. This parameter is ignored for other types of FDCs (i.e.
  1088. * 8272a).
  1089. *
  1090. * Note that changing the data transfer rate has a (probably deleterious)
  1091. * effect on the parameters subject to scaling for 82072/82077 FDCs, so
  1092. * fdc_specify is called again after each data transfer rate
  1093. * change.
  1094. *
  1095. * srt: 1000 to 16000 in microseconds
  1096. * hut: 16 to 240 milliseconds
  1097. * hlt: 2 to 254 milliseconds
  1098. *
  1099. * These values are rounded up to the next highest available delay time.
  1100. */
  1101. static void fdc_specify(void)
  1102. {
  1103. unsigned char spec1;
  1104. unsigned char spec2;
  1105. unsigned long srt;
  1106. unsigned long hlt;
  1107. unsigned long hut;
  1108. unsigned long dtr = NOMINAL_DTR;
  1109. unsigned long scale_dtr = NOMINAL_DTR;
  1110. int hlt_max_code = 0x7f;
  1111. int hut_max_code = 0xf;
  1112. if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
  1113. fdc_configure();
  1114. FDCS->need_configure = 0;
  1115. }
  1116. switch (raw_cmd->rate & 0x03) {
  1117. case 3:
  1118. dtr = 1000;
  1119. break;
  1120. case 1:
  1121. dtr = 300;
  1122. if (FDCS->version >= FDC_82078) {
  1123. /* chose the default rate table, not the one
  1124. * where 1 = 2 Mbps */
  1125. output_byte(FD_DRIVESPEC);
  1126. if (need_more_output() == MORE_OUTPUT) {
  1127. output_byte(UNIT(current_drive));
  1128. output_byte(0xc0);
  1129. }
  1130. }
  1131. break;
  1132. case 2:
  1133. dtr = 250;
  1134. break;
  1135. }
  1136. if (FDCS->version >= FDC_82072) {
  1137. scale_dtr = dtr;
  1138. hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
  1139. hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
  1140. }
  1141. /* Convert step rate from microseconds to milliseconds and 4 bits */
  1142. srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
  1143. if (slow_floppy)
  1144. srt = srt / 4;
  1145. SUPBOUND(srt, 0xf);
  1146. INFBOUND(srt, 0);
  1147. hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
  1148. if (hlt < 0x01)
  1149. hlt = 0x01;
  1150. else if (hlt > 0x7f)
  1151. hlt = hlt_max_code;
  1152. hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
  1153. if (hut < 0x1)
  1154. hut = 0x1;
  1155. else if (hut > 0xf)
  1156. hut = hut_max_code;
  1157. spec1 = (srt << 4) | hut;
  1158. spec2 = (hlt << 1) | (use_virtual_dma & 1);
  1159. /* If these parameters did not change, just return with success */
  1160. if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
  1161. /* Go ahead and set spec1 and spec2 */
  1162. output_byte(FD_SPECIFY);
  1163. output_byte(FDCS->spec1 = spec1);
  1164. output_byte(FDCS->spec2 = spec2);
  1165. }
  1166. } /* fdc_specify */
  1167. /* Set the FDC's data transfer rate on behalf of the specified drive.
  1168. * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
  1169. * of the specify command (i.e. using the fdc_specify function).
  1170. */
  1171. static int fdc_dtr(void)
  1172. {
  1173. /* If data rate not already set to desired value, set it. */
  1174. if ((raw_cmd->rate & 3) == FDCS->dtr)
  1175. return 0;
  1176. /* Set dtr */
  1177. fd_outb(raw_cmd->rate & 3, FD_DCR);
  1178. /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
  1179. * need a stabilization period of several milliseconds to be
  1180. * enforced after data rate changes before R/W operations.
  1181. * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
  1182. */
  1183. FDCS->dtr = raw_cmd->rate & 3;
  1184. return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
  1185. (timeout_fn)floppy_ready);
  1186. } /* fdc_dtr */
  1187. static void tell_sector(void)
  1188. {
  1189. pr_cont(": track %d, head %d, sector %d, size %d",
  1190. R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
  1191. } /* tell_sector */
  1192. static void print_errors(void)
  1193. {
  1194. DPRINT("");
  1195. if (ST0 & ST0_ECE) {
  1196. pr_cont("Recalibrate failed!");
  1197. } else if (ST2 & ST2_CRC) {
  1198. pr_cont("data CRC error");
  1199. tell_sector();
  1200. } else if (ST1 & ST1_CRC) {
  1201. pr_cont("CRC error");
  1202. tell_sector();
  1203. } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
  1204. (ST2 & ST2_MAM)) {
  1205. if (!probing) {
  1206. pr_cont("sector not found");
  1207. tell_sector();
  1208. } else
  1209. pr_cont("probe failed...");
  1210. } else if (ST2 & ST2_WC) { /* seek error */
  1211. pr_cont("wrong cylinder");
  1212. } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
  1213. pr_cont("bad cylinder");
  1214. } else {
  1215. pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
  1216. ST0, ST1, ST2);
  1217. tell_sector();
  1218. }
  1219. pr_cont("\n");
  1220. }
  1221. /*
  1222. * OK, this error interpreting routine is called after a
  1223. * DMA read/write has succeeded
  1224. * or failed, so we check the results, and copy any buffers.
  1225. * hhb: Added better error reporting.
  1226. * ak: Made this into a separate routine.
  1227. */
  1228. static int interpret_errors(void)
  1229. {
  1230. char bad;
  1231. if (inr != 7) {
  1232. DPRINT("-- FDC reply error\n");
  1233. FDCS->reset = 1;
  1234. return 1;
  1235. }
  1236. /* check IC to find cause of interrupt */
  1237. switch (ST0 & ST0_INTR) {
  1238. case 0x40: /* error occurred during command execution */
  1239. if (ST1 & ST1_EOC)
  1240. return 0; /* occurs with pseudo-DMA */
  1241. bad = 1;
  1242. if (ST1 & ST1_WP) {
  1243. DPRINT("Drive is write protected\n");
  1244. clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
  1245. cont->done(0);
  1246. bad = 2;
  1247. } else if (ST1 & ST1_ND) {
  1248. set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
  1249. } else if (ST1 & ST1_OR) {
  1250. if (DP->flags & FTD_MSG)
  1251. DPRINT("Over/Underrun - retrying\n");
  1252. bad = 0;
  1253. } else if (*errors >= DP->max_errors.reporting) {
  1254. print_errors();
  1255. }
  1256. if (ST2 & ST2_WC || ST2 & ST2_BC)
  1257. /* wrong cylinder => recal */
  1258. DRS->track = NEED_2_RECAL;
  1259. return bad;
  1260. case 0x80: /* invalid command given */
  1261. DPRINT("Invalid FDC command given!\n");
  1262. cont->done(0);
  1263. return 2;
  1264. case 0xc0:
  1265. DPRINT("Abnormal termination caused by polling\n");
  1266. cont->error();
  1267. return 2;
  1268. default: /* (0) Normal command termination */
  1269. return 0;
  1270. }
  1271. }
  1272. /*
  1273. * This routine is called when everything should be correctly set up
  1274. * for the transfer (i.e. floppy motor is on, the correct floppy is
  1275. * selected, and the head is sitting on the right track).
  1276. */
  1277. static void setup_rw_floppy(void)
  1278. {
  1279. int i;
  1280. int r;
  1281. int flags;
  1282. int dflags;
  1283. unsigned long ready_date;
  1284. timeout_fn function;
  1285. flags = raw_cmd->flags;
  1286. if (flags & (FD_RAW_READ | FD_RAW_WRITE))
  1287. flags |= FD_RAW_INTR;
  1288. if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
  1289. ready_date = DRS->spinup_date + DP->spinup;
  1290. /* If spinup will take a long time, rerun scandrives
  1291. * again just before spinup completion. Beware that
  1292. * after scandrives, we must again wait for selection.
  1293. */
  1294. if (time_after(ready_date, jiffies + DP->select_delay)) {
  1295. ready_date -= DP->select_delay;
  1296. function = (timeout_fn)floppy_start;
  1297. } else
  1298. function = (timeout_fn)setup_rw_floppy;
  1299. /* wait until the floppy is spinning fast enough */
  1300. if (fd_wait_for_completion(ready_date, function))
  1301. return;
  1302. }
  1303. dflags = DRS->flags;
  1304. if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
  1305. setup_DMA();
  1306. if (flags & FD_RAW_INTR)
  1307. do_floppy = main_command_interrupt;
  1308. r = 0;
  1309. for (i = 0; i < raw_cmd->cmd_count; i++)
  1310. r |= output_byte(raw_cmd->cmd[i]);
  1311. debugt(__func__, "rw_command");
  1312. if (r) {
  1313. cont->error();
  1314. reset_fdc();
  1315. return;
  1316. }
  1317. if (!(flags & FD_RAW_INTR)) {
  1318. inr = result();
  1319. cont->interrupt();
  1320. } else if (flags & FD_RAW_NEED_DISK)
  1321. fd_watchdog();
  1322. }
  1323. static int blind_seek;
  1324. /*
  1325. * This is the routine called after every seek (or recalibrate) interrupt
  1326. * from the floppy controller.
  1327. */
  1328. static void seek_interrupt(void)
  1329. {
  1330. debugt(__func__, "");
  1331. if (inr != 2 || (ST0 & 0xF8) != 0x20) {
  1332. DPRINT("seek failed\n");
  1333. DRS->track = NEED_2_RECAL;
  1334. cont->error();
  1335. cont->redo();
  1336. return;
  1337. }
  1338. if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
  1339. debug_dcl(DP->flags,
  1340. "clearing NEWCHANGE flag because of effective seek\n");
  1341. debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
  1342. clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
  1343. /* effective seek */
  1344. DRS->select_date = jiffies;
  1345. }
  1346. DRS->track = ST1;
  1347. floppy_ready();
  1348. }
  1349. static void check_wp(void)
  1350. {
  1351. if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
  1352. /* check write protection */
  1353. output_byte(FD_GETSTATUS);
  1354. output_byte(UNIT(current_drive));
  1355. if (result() != 1) {
  1356. FDCS->reset = 1;
  1357. return;
  1358. }
  1359. clear_bit(FD_VERIFY_BIT, &DRS->flags);
  1360. clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
  1361. debug_dcl(DP->flags,
  1362. "checking whether disk is write protected\n");
  1363. debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
  1364. if (!(ST3 & 0x40))
  1365. set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
  1366. else
  1367. clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
  1368. }
  1369. }
  1370. static void seek_floppy(void)
  1371. {
  1372. int track;
  1373. blind_seek = 0;
  1374. debug_dcl(DP->flags, "calling disk change from %s\n", __func__);
  1375. if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
  1376. disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
  1377. /* the media changed flag should be cleared after the seek.
  1378. * If it isn't, this means that there is really no disk in
  1379. * the drive.
  1380. */
  1381. set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
  1382. cont->done(0);
  1383. cont->redo();
  1384. return;
  1385. }
  1386. if (DRS->track <= NEED_1_RECAL) {
  1387. recalibrate_floppy();
  1388. return;
  1389. } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
  1390. (raw_cmd->flags & FD_RAW_NEED_DISK) &&
  1391. (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
  1392. /* we seek to clear the media-changed condition. Does anybody
  1393. * know a more elegant way, which works on all drives? */
  1394. if (raw_cmd->track)
  1395. track = raw_cmd->track - 1;
  1396. else {
  1397. if (DP->flags & FD_SILENT_DCL_CLEAR) {
  1398. set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
  1399. blind_seek = 1;
  1400. raw_cmd->flags |= FD_RAW_NEED_SEEK;
  1401. }
  1402. track = 1;
  1403. }
  1404. } else {
  1405. check_wp();
  1406. if (raw_cmd->track != DRS->track &&
  1407. (raw_cmd->flags & FD_RAW_NEED_SEEK))
  1408. track = raw_cmd->track;
  1409. else {
  1410. setup_rw_floppy();
  1411. return;
  1412. }
  1413. }
  1414. do_floppy = seek_interrupt;
  1415. output_byte(FD_SEEK);
  1416. output_byte(UNIT(current_drive));
  1417. if (output_byte(track) < 0) {
  1418. reset_fdc();
  1419. return;
  1420. }
  1421. debugt(__func__, "");
  1422. }
  1423. static void recal_interrupt(void)
  1424. {
  1425. debugt(__func__, "");
  1426. if (inr != 2)
  1427. FDCS->reset = 1;
  1428. else if (ST0 & ST0_ECE) {
  1429. switch (DRS->track) {
  1430. case NEED_1_RECAL:
  1431. debugt(__func__, "need 1 recal");
  1432. /* after a second recalibrate, we still haven't
  1433. * reached track 0. Probably no drive. Raise an
  1434. * error, as failing immediately might upset
  1435. * computers possessed by the Devil :-) */
  1436. cont->error();
  1437. cont->redo();
  1438. return;
  1439. case NEED_2_RECAL:
  1440. debugt(__func__, "need 2 recal");
  1441. /* If we already did a recalibrate,
  1442. * and we are not at track 0, this
  1443. * means we have moved. (The only way
  1444. * not to move at recalibration is to
  1445. * be already at track 0.) Clear the
  1446. * new change flag */
  1447. debug_dcl(DP->flags,
  1448. "clearing NEWCHANGE flag because of second recalibrate\n");
  1449. clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
  1450. DRS->select_date = jiffies;
  1451. /* fall through */
  1452. default:
  1453. debugt(__func__, "default");
  1454. /* Recalibrate moves the head by at
  1455. * most 80 steps. If after one
  1456. * recalibrate we don't have reached
  1457. * track 0, this might mean that we
  1458. * started beyond track 80. Try
  1459. * again. */
  1460. DRS->track = NEED_1_RECAL;
  1461. break;
  1462. }
  1463. } else
  1464. DRS->track = ST1;
  1465. floppy_ready();
  1466. }
  1467. static void print_result(char *message, int inr)
  1468. {
  1469. int i;
  1470. DPRINT("%s ", message);
  1471. if (inr >= 0)
  1472. for (i = 0; i < inr; i++)
  1473. pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
  1474. pr_cont("\n");
  1475. }
  1476. /* interrupt handler. Note that this can be called externally on the Sparc */
  1477. irqreturn_t floppy_interrupt(int irq, void *dev_id)
  1478. {
  1479. int do_print;
  1480. unsigned long f;
  1481. void (*handler)(void) = do_floppy;
  1482. lasthandler = handler;
  1483. interruptjiffies = jiffies;
  1484. f = claim_dma_lock();
  1485. fd_disable_dma();
  1486. release_dma_lock(f);
  1487. floppy_enable_hlt();
  1488. do_floppy = NULL;
  1489. if (fdc >= N_FDC || FDCS->address == -1) {
  1490. /* we don't even know which FDC is the culprit */
  1491. pr_info("DOR0=%x\n", fdc_state[0].dor);
  1492. pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
  1493. pr_info("handler=%pf\n", handler);
  1494. is_alive(__func__, "bizarre fdc");
  1495. return IRQ_NONE;
  1496. }
  1497. FDCS->reset = 0;
  1498. /* We have to clear the reset flag here, because apparently on boxes
  1499. * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
  1500. * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
  1501. * emission of the SENSEI's.
  1502. * It is OK to emit floppy commands because we are in an interrupt
  1503. * handler here, and thus we have to fear no interference of other
  1504. * activity.
  1505. */
  1506. do_print = !handler && print_unex && initialized;
  1507. inr = result();
  1508. if (do_print)
  1509. print_result("unexpected interrupt", inr);
  1510. if (inr == 0) {
  1511. int max_sensei = 4;
  1512. do {
  1513. output_byte(FD_SENSEI);
  1514. inr = result();
  1515. if (do_print)
  1516. print_result("sensei", inr);
  1517. max_sensei--;
  1518. } while ((ST0 & 0x83) != UNIT(current_drive) &&
  1519. inr == 2 && max_sensei);
  1520. }
  1521. if (!handler) {
  1522. FDCS->reset = 1;
  1523. return IRQ_NONE;
  1524. }
  1525. schedule_bh(handler);
  1526. is_alive(__func__, "normal interrupt end");
  1527. /* FIXME! Was it really for us? */
  1528. return IRQ_HANDLED;
  1529. }
  1530. static void recalibrate_floppy(void)
  1531. {
  1532. debugt(__func__, "");
  1533. do_floppy = recal_interrupt;
  1534. output_byte(FD_RECALIBRATE);
  1535. if (output_byte(UNIT(current_drive)) < 0)
  1536. reset_fdc();
  1537. }
  1538. /*
  1539. * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
  1540. */
  1541. static void reset_interrupt(void)
  1542. {
  1543. debugt(__func__, "");
  1544. result(); /* get the status ready for set_fdc */
  1545. if (FDCS->reset) {
  1546. pr_info("reset set in interrupt, calling %pf\n", cont->error);
  1547. cont->error(); /* a reset just after a reset. BAD! */
  1548. }
  1549. cont->redo();
  1550. }
  1551. /*
  1552. * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
  1553. * or by setting the self clearing bit 7 of STATUS (newer FDCs)
  1554. */
  1555. static void reset_fdc(void)
  1556. {
  1557. unsigned long flags;
  1558. do_floppy = reset_interrupt;
  1559. FDCS->reset = 0;
  1560. reset_fdc_info(0);
  1561. /* Pseudo-DMA may intercept 'reset finished' interrupt. */
  1562. /* Irrelevant for systems with true DMA (i386). */
  1563. flags = claim_dma_lock();
  1564. fd_disable_dma();
  1565. release_dma_lock(flags);
  1566. if (FDCS->version >= FDC_82072A)
  1567. fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
  1568. else {
  1569. fd_outb(FDCS->dor & ~0x04, FD_DOR);
  1570. udelay(FD_RESET_DELAY);
  1571. fd_outb(FDCS->dor, FD_DOR);
  1572. }
  1573. }
  1574. static void show_floppy(void)
  1575. {
  1576. int i;
  1577. pr_info("\n");
  1578. pr_info("floppy driver state\n");
  1579. pr_info("-------------------\n");
  1580. pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
  1581. jiffies, interruptjiffies, jiffies - interruptjiffies,
  1582. lasthandler);
  1583. pr_info("timeout_message=%s\n", timeout_message);
  1584. pr_info("last output bytes:\n");
  1585. for (i = 0; i < OLOGSIZE; i++)
  1586. pr_info("%2x %2x %lu\n",
  1587. output_log[(i + output_log_pos) % OLOGSIZE].data,
  1588. output_log[(i + output_log_pos) % OLOGSIZE].status,
  1589. output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
  1590. pr_info("last result at %lu\n", resultjiffies);
  1591. pr_info("last redo_fd_request at %lu\n", lastredo);
  1592. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
  1593. reply_buffer, resultsize, true);
  1594. pr_info("status=%x\n", fd_inb(FD_STATUS));
  1595. pr_info("fdc_busy=%lu\n", fdc_busy);
  1596. if (do_floppy)
  1597. pr_info("do_floppy=%pf\n", do_floppy);
  1598. if (work_pending(&floppy_work))
  1599. pr_info("floppy_work.func=%pf\n", floppy_work.func);
  1600. if (timer_pending(&fd_timer))
  1601. pr_info("fd_timer.function=%pf\n", fd_timer.function);
  1602. if (timer_pending(&fd_timeout)) {
  1603. pr_info("timer_function=%pf\n", fd_timeout.function);
  1604. pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
  1605. pr_info("now=%lu\n", jiffies);
  1606. }
  1607. pr_info("cont=%p\n", cont);
  1608. pr_info("current_req=%p\n", current_req);
  1609. pr_info("command_status=%d\n", command_status);
  1610. pr_info("\n");
  1611. }
  1612. static void floppy_shutdown(unsigned long data)
  1613. {
  1614. unsigned long flags;
  1615. if (initialized)
  1616. show_floppy();
  1617. cancel_activity();
  1618. floppy_enable_hlt();
  1619. flags = claim_dma_lock();
  1620. fd_disable_dma();
  1621. release_dma_lock(flags);
  1622. /* avoid dma going to a random drive after shutdown */
  1623. if (initialized)
  1624. DPRINT("floppy timeout called\n");
  1625. FDCS->reset = 1;
  1626. if (cont) {
  1627. cont->done(0);
  1628. cont->redo(); /* this will recall reset when needed */
  1629. } else {
  1630. pr_info("no cont in shutdown!\n");
  1631. process_fd_request();
  1632. }
  1633. is_alive(__func__, "");
  1634. }
  1635. /* start motor, check media-changed condition and write protection */
  1636. static int start_motor(void (*function)(void))
  1637. {
  1638. int mask;
  1639. int data;
  1640. mask = 0xfc;
  1641. data = UNIT(current_drive);
  1642. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
  1643. if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
  1644. set_debugt();
  1645. /* no read since this drive is running */
  1646. DRS->first_read_date = 0;
  1647. /* note motor start time if motor is not yet running */
  1648. DRS->spinup_date = jiffies;
  1649. data |= (0x10 << UNIT(current_drive));
  1650. }
  1651. } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
  1652. mask &= ~(0x10 << UNIT(current_drive));
  1653. /* starts motor and selects floppy */
  1654. del_timer(motor_off_timer + current_drive);
  1655. set_dor(fdc, mask, data);
  1656. /* wait_for_completion also schedules reset if needed. */
  1657. return fd_wait_for_completion(DRS->select_date + DP->select_delay,
  1658. (timeout_fn)function);
  1659. }
  1660. static void floppy_ready(void)
  1661. {
  1662. if (FDCS->reset) {
  1663. reset_fdc();
  1664. return;
  1665. }
  1666. if (start_motor(floppy_ready))
  1667. return;
  1668. if (fdc_dtr())
  1669. return;
  1670. debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
  1671. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
  1672. disk_change(current_drive) && !DP->select_delay)
  1673. twaddle(); /* this clears the dcl on certain
  1674. * drive/controller combinations */
  1675. #ifdef fd_chose_dma_mode
  1676. if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
  1677. unsigned long flags = claim_dma_lock();
  1678. fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
  1679. release_dma_lock(flags);
  1680. }
  1681. #endif
  1682. if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
  1683. perpendicular_mode();
  1684. fdc_specify(); /* must be done here because of hut, hlt ... */
  1685. seek_floppy();
  1686. } else {
  1687. if ((raw_cmd->flags & FD_RAW_READ) ||
  1688. (raw_cmd->flags & FD_RAW_WRITE))
  1689. fdc_specify();
  1690. setup_rw_floppy();
  1691. }
  1692. }
  1693. static void floppy_start(void)
  1694. {
  1695. reschedule_timeout(current_reqD, "floppy start");
  1696. scandrives();
  1697. debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
  1698. set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
  1699. floppy_ready();
  1700. }
  1701. /*
  1702. * ========================================================================
  1703. * here ends the bottom half. Exported routines are:
  1704. * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
  1705. * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
  1706. * Initialization also uses output_byte, result, set_dor, floppy_interrupt
  1707. * and set_dor.
  1708. * ========================================================================
  1709. */
  1710. /*
  1711. * General purpose continuations.
  1712. * ==============================
  1713. */
  1714. static void do_wakeup(void)
  1715. {
  1716. reschedule_timeout(MAXTIMEOUT, "do wakeup");
  1717. cont = NULL;
  1718. command_status += 2;
  1719. wake_up(&command_done);
  1720. }
  1721. static const struct cont_t wakeup_cont = {
  1722. .interrupt = empty,
  1723. .redo = do_wakeup,
  1724. .error = empty,
  1725. .done = (done_f)empty
  1726. };
  1727. static const struct cont_t intr_cont = {
  1728. .interrupt = empty,
  1729. .redo = process_fd_request,
  1730. .error = empty,
  1731. .done = (done_f)empty
  1732. };
  1733. static int wait_til_done(void (*handler)(void), bool interruptible)
  1734. {
  1735. int ret;
  1736. schedule_bh(handler);
  1737. if (interruptible)
  1738. wait_event_interruptible(command_done, command_status >= 2);
  1739. else
  1740. wait_event(command_done, command_status >= 2);
  1741. if (command_status < 2) {
  1742. cancel_activity();
  1743. cont = &intr_cont;
  1744. reset_fdc();
  1745. return -EINTR;
  1746. }
  1747. if (FDCS->reset)
  1748. command_status = FD_COMMAND_ERROR;
  1749. if (command_status == FD_COMMAND_OKAY)
  1750. ret = 0;
  1751. else
  1752. ret = -EIO;
  1753. command_status = FD_COMMAND_NONE;
  1754. return ret;
  1755. }
  1756. static void generic_done(int result)
  1757. {
  1758. command_status = result;
  1759. cont = &wakeup_cont;
  1760. }
  1761. static void generic_success(void)
  1762. {
  1763. cont->done(1);
  1764. }
  1765. static void generic_failure(void)
  1766. {
  1767. cont->done(0);
  1768. }
  1769. static void success_and_wakeup(void)
  1770. {
  1771. generic_success();
  1772. cont->redo();
  1773. }
  1774. /*
  1775. * formatting and rw support.
  1776. * ==========================
  1777. */
  1778. static int next_valid_format(void)
  1779. {
  1780. int probed_format;
  1781. probed_format = DRS->probed_format;
  1782. while (1) {
  1783. if (probed_format >= 8 || !DP->autodetect[probed_format]) {
  1784. DRS->probed_format = 0;
  1785. return 1;
  1786. }
  1787. if (floppy_type[DP->autodetect[probed_format]].sect) {
  1788. DRS->probed_format = probed_format;
  1789. return 0;
  1790. }
  1791. probed_format++;
  1792. }
  1793. }
  1794. static void bad_flp_intr(void)
  1795. {
  1796. int err_count;
  1797. if (probing) {
  1798. DRS->probed_format++;
  1799. if (!next_valid_format())
  1800. return;
  1801. }
  1802. err_count = ++(*errors);
  1803. INFBOUND(DRWE->badness, err_count);
  1804. if (err_count > DP->max_errors.abort)
  1805. cont->done(0);
  1806. if (err_count > DP->max_errors.reset)
  1807. FDCS->reset = 1;
  1808. else if (err_count > DP->max_errors.recal)
  1809. DRS->track = NEED_2_RECAL;
  1810. }
  1811. static void set_floppy(int drive)
  1812. {
  1813. int type = ITYPE(UDRS->fd_device);
  1814. if (type)
  1815. _floppy = floppy_type + type;
  1816. else
  1817. _floppy = current_type[drive];
  1818. }
  1819. /*
  1820. * formatting support.
  1821. * ===================
  1822. */
  1823. static void format_interrupt(void)
  1824. {
  1825. switch (interpret_errors()) {
  1826. case 1:
  1827. cont->error();
  1828. case 2:
  1829. break;
  1830. case 0:
  1831. cont->done(1);
  1832. }
  1833. cont->redo();
  1834. }
  1835. #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
  1836. #define CT(x) ((x) | 0xc0)
  1837. static void setup_format_params(int track)
  1838. {
  1839. int n;
  1840. int il;
  1841. int count;
  1842. int head_shift;
  1843. int track_shift;
  1844. struct fparm {
  1845. unsigned char track, head, sect, size;
  1846. } *here = (struct fparm *)floppy_track_buffer;
  1847. raw_cmd = &default_raw_cmd;
  1848. raw_cmd->track = track;
  1849. raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
  1850. FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
  1851. raw_cmd->rate = _floppy->rate & 0x43;
  1852. raw_cmd->cmd_count = NR_F;
  1853. COMMAND = FM_MODE(_floppy, FD_FORMAT);
  1854. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
  1855. F_SIZECODE = FD_SIZECODE(_floppy);
  1856. F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
  1857. F_GAP = _floppy->fmt_gap;
  1858. F_FILL = FD_FILL_BYTE;
  1859. raw_cmd->kernel_data = floppy_track_buffer;
  1860. raw_cmd->length = 4 * F_SECT_PER_TRACK;
  1861. /* allow for about 30ms for data transport per track */
  1862. head_shift = (F_SECT_PER_TRACK + 5) / 6;
  1863. /* a ``cylinder'' is two tracks plus a little stepping time */
  1864. track_shift = 2 * head_shift + 3;
  1865. /* position of logical sector 1 on this track */
  1866. n = (track_shift * format_req.track + head_shift * format_req.head)
  1867. % F_SECT_PER_TRACK;
  1868. /* determine interleave */
  1869. il = 1;
  1870. if (_floppy->fmt_gap < 0x22)
  1871. il++;
  1872. /* initialize field */
  1873. for (count = 0; count < F_SECT_PER_TRACK; ++count) {
  1874. here[count].track = format_req.track;
  1875. here[count].head = format_req.head;
  1876. here[count].sect = 0;
  1877. here[count].size = F_SIZECODE;
  1878. }
  1879. /* place logical sectors */
  1880. for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
  1881. here[n].sect = count;
  1882. n = (n + il) % F_SECT_PER_TRACK;
  1883. if (here[n].sect) { /* sector busy, find next free sector */
  1884. ++n;
  1885. if (n >= F_SECT_PER_TRACK) {
  1886. n -= F_SECT_PER_TRACK;
  1887. while (here[n].sect)
  1888. ++n;
  1889. }
  1890. }
  1891. }
  1892. if (_floppy->stretch & FD_SECTBASEMASK) {
  1893. for (count = 0; count < F_SECT_PER_TRACK; count++)
  1894. here[count].sect += FD_SECTBASE(_floppy) - 1;
  1895. }
  1896. }
  1897. static void redo_format(void)
  1898. {
  1899. buffer_track = -1;
  1900. setup_format_params(format_req.track << STRETCH(_floppy));
  1901. floppy_start();
  1902. debugt(__func__, "queue format request");
  1903. }
  1904. static const struct cont_t format_cont = {
  1905. .interrupt = format_interrupt,
  1906. .redo = redo_format,
  1907. .error = bad_flp_intr,
  1908. .done = generic_done
  1909. };
  1910. static int do_format(int drive, struct format_descr *tmp_format_req)
  1911. {
  1912. int ret;
  1913. if (lock_fdc(drive, true))
  1914. return -EINTR;
  1915. set_floppy(drive);
  1916. if (!_floppy ||
  1917. _floppy->track > DP->tracks ||
  1918. tmp_format_req->track >= _floppy->track ||
  1919. tmp_format_req->head >= _floppy->head ||
  1920. (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
  1921. !_floppy->fmt_gap) {
  1922. process_fd_request();
  1923. return -EINVAL;
  1924. }
  1925. format_req = *tmp_format_req;
  1926. format_errors = 0;
  1927. cont = &format_cont;
  1928. errors = &format_errors;
  1929. ret = wait_til_done(redo_format, true);
  1930. if (ret == -EINTR)
  1931. return -EINTR;
  1932. process_fd_request();
  1933. return ret;
  1934. }
  1935. /*
  1936. * Buffer read/write and support
  1937. * =============================
  1938. */
  1939. static void floppy_end_request(struct request *req, int error)
  1940. {
  1941. unsigned int nr_sectors = current_count_sectors;
  1942. unsigned int drive = (unsigned long)req->rq_disk->private_data;
  1943. /* current_count_sectors can be zero if transfer failed */
  1944. if (error)
  1945. nr_sectors = blk_rq_cur_sectors(req);
  1946. if (__blk_end_request(req, error, nr_sectors << 9))
  1947. return;
  1948. /* We're done with the request */
  1949. floppy_off(drive);
  1950. current_req = NULL;
  1951. }
  1952. /* new request_done. Can handle physical sectors which are smaller than a
  1953. * logical buffer */
  1954. static void request_done(int uptodate)
  1955. {
  1956. struct request *req = current_req;
  1957. struct request_queue *q;
  1958. unsigned long flags;
  1959. int block;
  1960. char msg[sizeof("request done ") + sizeof(int) * 3];
  1961. probing = 0;
  1962. snprintf(msg, sizeof(msg), "request done %d", uptodate);
  1963. reschedule_timeout(MAXTIMEOUT, msg);
  1964. if (!req) {
  1965. pr_info("floppy.c: no request in request_done\n");
  1966. return;
  1967. }
  1968. q = req->q;
  1969. if (uptodate) {
  1970. /* maintain values for invalidation on geometry
  1971. * change */
  1972. block = current_count_sectors + blk_rq_pos(req);
  1973. INFBOUND(DRS->maxblock, block);
  1974. if (block > _floppy->sect)
  1975. DRS->maxtrack = 1;
  1976. /* unlock chained buffers */
  1977. spin_lock_irqsave(q->queue_lock, flags);
  1978. floppy_end_request(req, 0);
  1979. spin_unlock_irqrestore(q->queue_lock, flags);
  1980. } else {
  1981. if (rq_data_dir(req) == WRITE) {
  1982. /* record write error information */
  1983. DRWE->write_errors++;
  1984. if (DRWE->write_errors == 1) {
  1985. DRWE->first_error_sector = blk_rq_pos(req);
  1986. DRWE->first_error_generation = DRS->generation;
  1987. }
  1988. DRWE->last_error_sector = blk_rq_pos(req);
  1989. DRWE->last_error_generation = DRS->generation;
  1990. }
  1991. spin_lock_irqsave(q->queue_lock, flags);
  1992. floppy_end_request(req, -EIO);
  1993. spin_unlock_irqrestore(q->queue_lock, flags);
  1994. }
  1995. }
  1996. /* Interrupt handler evaluating the result of the r/w operation */
  1997. static void rw_interrupt(void)
  1998. {
  1999. int eoc;
  2000. int ssize;
  2001. int heads;
  2002. int nr_sectors;
  2003. if (R_HEAD >= 2) {
  2004. /* some Toshiba floppy controllers occasionnally seem to
  2005. * return bogus interrupts after read/write operations, which
  2006. * can be recognized by a bad head number (>= 2) */
  2007. return;
  2008. }
  2009. if (!DRS->first_read_date)
  2010. DRS->first_read_date = jiffies;
  2011. nr_sectors = 0;
  2012. ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
  2013. if (ST1 & ST1_EOC)
  2014. eoc = 1;
  2015. else
  2016. eoc = 0;
  2017. if (COMMAND & 0x80)
  2018. heads = 2;
  2019. else
  2020. heads = 1;
  2021. nr_sectors = (((R_TRACK - TRACK) * heads +
  2022. R_HEAD - HEAD) * SECT_PER_TRACK +
  2023. R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
  2024. if (nr_sectors / ssize >
  2025. DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
  2026. DPRINT("long rw: %x instead of %lx\n",
  2027. nr_sectors, current_count_sectors);
  2028. pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
  2029. pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
  2030. pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
  2031. pr_info("heads=%d eoc=%d\n", heads, eoc);
  2032. pr_info("spt=%d st=%d ss=%d\n",
  2033. SECT_PER_TRACK, fsector_t, ssize);
  2034. pr_info("in_sector_offset=%d\n", in_sector_offset);
  2035. }
  2036. nr_sectors -= in_sector_offset;
  2037. INFBOUND(nr_sectors, 0);
  2038. SUPBOUND(current_count_sectors, nr_sectors);
  2039. switch (interpret_errors()) {
  2040. case 2:
  2041. cont->redo();
  2042. return;
  2043. case 1:
  2044. if (!current_count_sectors) {
  2045. cont->error();
  2046. cont->redo();
  2047. return;
  2048. }
  2049. break;
  2050. case 0:
  2051. if (!current_count_sectors) {
  2052. cont->redo();
  2053. return;
  2054. }
  2055. current_type[current_drive] = _floppy;
  2056. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2057. break;
  2058. }
  2059. if (probing) {
  2060. if (DP->flags & FTD_MSG)
  2061. DPRINT("Auto-detected floppy type %s in fd%d\n",
  2062. _floppy->name, current_drive);
  2063. current_type[current_drive] = _floppy;
  2064. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2065. probing = 0;
  2066. }
  2067. if (CT(COMMAND) != FD_READ ||
  2068. raw_cmd->kernel_data == current_req->buffer) {
  2069. /* transfer directly from buffer */
  2070. cont->done(1);
  2071. } else if (CT(COMMAND) == FD_READ) {
  2072. buffer_track = raw_cmd->track;
  2073. buffer_drive = current_drive;
  2074. INFBOUND(buffer_max, nr_sectors + fsector_t);
  2075. }
  2076. cont->redo();
  2077. }
  2078. /* Compute maximal contiguous buffer size. */
  2079. static int buffer_chain_size(void)
  2080. {
  2081. struct bio_vec *bv;
  2082. int size;
  2083. struct req_iterator iter;
  2084. char *base;
  2085. base = bio_data(current_req->bio);
  2086. size = 0;
  2087. rq_for_each_segment(bv, current_req, iter) {
  2088. if (page_address(bv->bv_page) + bv->bv_offset != base + size)
  2089. break;
  2090. size += bv->bv_len;
  2091. }
  2092. return size >> 9;
  2093. }
  2094. /* Compute the maximal transfer size */
  2095. static int transfer_size(int ssize, int max_sector, int max_size)
  2096. {
  2097. SUPBOUND(max_sector, fsector_t + max_size);
  2098. /* alignment */
  2099. max_sector -= (max_sector % _floppy->sect) % ssize;
  2100. /* transfer size, beginning not aligned */
  2101. current_count_sectors = max_sector - fsector_t;
  2102. return max_sector;
  2103. }
  2104. /*
  2105. * Move data from/to the track buffer to/from the buffer cache.
  2106. */
  2107. static void copy_buffer(int ssize, int max_sector, int max_sector_2)
  2108. {
  2109. int remaining; /* number of transferred 512-byte sectors */
  2110. struct bio_vec *bv;
  2111. char *buffer;
  2112. char *dma_buffer;
  2113. int size;
  2114. struct req_iterator iter;
  2115. max_sector = transfer_size(ssize,
  2116. min(max_sector, max_sector_2),
  2117. blk_rq_sectors(current_req));
  2118. if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
  2119. buffer_max > fsector_t + blk_rq_sectors(current_req))
  2120. current_count_sectors = min_t(int, buffer_max - fsector_t,
  2121. blk_rq_sectors(current_req));
  2122. remaining = current_count_sectors << 9;
  2123. if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
  2124. DPRINT("in copy buffer\n");
  2125. pr_info("current_count_sectors=%ld\n", current_count_sectors);
  2126. pr_info("remaining=%d\n", remaining >> 9);
  2127. pr_info("current_req->nr_sectors=%u\n",
  2128. blk_rq_sectors(current_req));
  2129. pr_info("current_req->current_nr_sectors=%u\n",
  2130. blk_rq_cur_sectors(current_req));
  2131. pr_info("max_sector=%d\n", max_sector);
  2132. pr_info("ssize=%d\n", ssize);
  2133. }
  2134. buffer_max = max(max_sector, buffer_max);
  2135. dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
  2136. size = blk_rq_cur_bytes(current_req);
  2137. rq_for_each_segment(bv, current_req, iter) {
  2138. if (!remaining)
  2139. break;
  2140. size = bv->bv_len;
  2141. SUPBOUND(size, remaining);
  2142. buffer = page_address(bv->bv_page) + bv->bv_offset;
  2143. if (dma_buffer + size >
  2144. floppy_track_buffer + (max_buffer_sectors << 10) ||
  2145. dma_buffer < floppy_track_buffer) {
  2146. DPRINT("buffer overrun in copy buffer %d\n",
  2147. (int)((floppy_track_buffer - dma_buffer) >> 9));
  2148. pr_info("fsector_t=%d buffer_min=%d\n",
  2149. fsector_t, buffer_min);
  2150. pr_info("current_count_sectors=%ld\n",
  2151. current_count_sectors);
  2152. if (CT(COMMAND) == FD_READ)
  2153. pr_info("read\n");
  2154. if (CT(COMMAND) == FD_WRITE)
  2155. pr_info("write\n");
  2156. break;
  2157. }
  2158. if (((unsigned long)buffer) % 512)
  2159. DPRINT("%p buffer not aligned\n", buffer);
  2160. if (CT(COMMAND) == FD_READ)
  2161. memcpy(buffer, dma_buffer, size);
  2162. else
  2163. memcpy(dma_buffer, buffer, size);
  2164. remaining -= size;
  2165. dma_buffer += size;
  2166. }
  2167. if (remaining) {
  2168. if (remaining > 0)
  2169. max_sector -= remaining >> 9;
  2170. DPRINT("weirdness: remaining %d\n", remaining >> 9);
  2171. }
  2172. }
  2173. /* work around a bug in pseudo DMA
  2174. * (on some FDCs) pseudo DMA does not stop when the CPU stops
  2175. * sending data. Hence we need a different way to signal the
  2176. * transfer length: We use SECT_PER_TRACK. Unfortunately, this
  2177. * does not work with MT, hence we can only transfer one head at
  2178. * a time
  2179. */
  2180. static void virtualdmabug_workaround(void)
  2181. {
  2182. int hard_sectors;
  2183. int end_sector;
  2184. if (CT(COMMAND) == FD_WRITE) {
  2185. COMMAND &= ~0x80; /* switch off multiple track mode */
  2186. hard_sectors = raw_cmd->length >> (7 + SIZECODE);
  2187. end_sector = SECTOR + hard_sectors - 1;
  2188. if (end_sector > SECT_PER_TRACK) {
  2189. pr_info("too many sectors %d > %d\n",
  2190. end_sector, SECT_PER_TRACK);
  2191. return;
  2192. }
  2193. SECT_PER_TRACK = end_sector;
  2194. /* make sure SECT_PER_TRACK
  2195. * points to end of transfer */
  2196. }
  2197. }
  2198. /*
  2199. * Formulate a read/write request.
  2200. * this routine decides where to load the data (directly to buffer, or to
  2201. * tmp floppy area), how much data to load (the size of the buffer, the whole
  2202. * track, or a single sector)
  2203. * All floppy_track_buffer handling goes in here. If we ever add track buffer
  2204. * allocation on the fly, it should be done here. No other part should need
  2205. * modification.
  2206. */
  2207. static int make_raw_rw_request(void)
  2208. {
  2209. int aligned_sector_t;
  2210. int max_sector;
  2211. int max_size;
  2212. int tracksize;
  2213. int ssize;
  2214. if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
  2215. return 0;
  2216. set_fdc((long)current_req->rq_disk->private_data);
  2217. raw_cmd = &default_raw_cmd;
  2218. raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
  2219. FD_RAW_NEED_SEEK;
  2220. raw_cmd->cmd_count = NR_RW;
  2221. if (rq_data_dir(current_req) == READ) {
  2222. raw_cmd->flags |= FD_RAW_READ;
  2223. COMMAND = FM_MODE(_floppy, FD_READ);
  2224. } else if (rq_data_dir(current_req) == WRITE) {
  2225. raw_cmd->flags |= FD_RAW_WRITE;
  2226. COMMAND = FM_MODE(_floppy, FD_WRITE);
  2227. } else {
  2228. DPRINT("%s: unknown command\n", __func__);
  2229. return 0;
  2230. }
  2231. max_sector = _floppy->sect * _floppy->head;
  2232. TRACK = (int)blk_rq_pos(current_req) / max_sector;
  2233. fsector_t = (int)blk_rq_pos(current_req) % max_sector;
  2234. if (_floppy->track && TRACK >= _floppy->track) {
  2235. if (blk_rq_cur_sectors(current_req) & 1) {
  2236. current_count_sectors = 1;
  2237. return 1;
  2238. } else
  2239. return 0;
  2240. }
  2241. HEAD = fsector_t / _floppy->sect;
  2242. if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
  2243. test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
  2244. fsector_t < _floppy->sect)
  2245. max_sector = _floppy->sect;
  2246. /* 2M disks have phantom sectors on the first track */
  2247. if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
  2248. max_sector = 2 * _floppy->sect / 3;
  2249. if (fsector_t >= max_sector) {
  2250. current_count_sectors =
  2251. min_t(int, _floppy->sect - fsector_t,
  2252. blk_rq_sectors(current_req));
  2253. return 1;
  2254. }
  2255. SIZECODE = 2;
  2256. } else
  2257. SIZECODE = FD_SIZECODE(_floppy);
  2258. raw_cmd->rate = _floppy->rate & 0x43;
  2259. if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
  2260. raw_cmd->rate = 1;
  2261. if (SIZECODE)
  2262. SIZECODE2 = 0xff;
  2263. else
  2264. SIZECODE2 = 0x80;
  2265. raw_cmd->track = TRACK << STRETCH(_floppy);
  2266. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
  2267. GAP = _floppy->gap;
  2268. ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
  2269. SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
  2270. SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
  2271. FD_SECTBASE(_floppy);
  2272. /* tracksize describes the size which can be filled up with sectors
  2273. * of size ssize.
  2274. */
  2275. tracksize = _floppy->sect - _floppy->sect % ssize;
  2276. if (tracksize < _floppy->sect) {
  2277. SECT_PER_TRACK++;
  2278. if (tracksize <= fsector_t % _floppy->sect)
  2279. SECTOR--;
  2280. /* if we are beyond tracksize, fill up using smaller sectors */
  2281. while (tracksize <= fsector_t % _floppy->sect) {
  2282. while (tracksize + ssize > _floppy->sect) {
  2283. SIZECODE--;
  2284. ssize >>= 1;
  2285. }
  2286. SECTOR++;
  2287. SECT_PER_TRACK++;
  2288. tracksize += ssize;
  2289. }
  2290. max_sector = HEAD * _floppy->sect + tracksize;
  2291. } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
  2292. max_sector = _floppy->sect;
  2293. } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
  2294. /* for virtual DMA bug workaround */
  2295. max_sector = _floppy->sect;
  2296. }
  2297. in_sector_offset = (fsector_t % _floppy->sect) % ssize;
  2298. aligned_sector_t = fsector_t - in_sector_offset;
  2299. max_size = blk_rq_sectors(current_req);
  2300. if ((raw_cmd->track == buffer_track) &&
  2301. (current_drive == buffer_drive) &&
  2302. (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
  2303. /* data already in track buffer */
  2304. if (CT(COMMAND) == FD_READ) {
  2305. copy_buffer(1, max_sector, buffer_max);
  2306. return 1;
  2307. }
  2308. } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
  2309. if (CT(COMMAND) == FD_WRITE) {
  2310. unsigned int sectors;
  2311. sectors = fsector_t + blk_rq_sectors(current_req);
  2312. if (sectors > ssize && sectors < ssize + ssize)
  2313. max_size = ssize + ssize;
  2314. else
  2315. max_size = ssize;
  2316. }
  2317. raw_cmd->flags &= ~FD_RAW_WRITE;
  2318. raw_cmd->flags |= FD_RAW_READ;
  2319. COMMAND = FM_MODE(_floppy, FD_READ);
  2320. } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
  2321. unsigned long dma_limit;
  2322. int direct, indirect;
  2323. indirect =
  2324. transfer_size(ssize, max_sector,
  2325. max_buffer_sectors * 2) - fsector_t;
  2326. /*
  2327. * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
  2328. * on a 64 bit machine!
  2329. */
  2330. max_size = buffer_chain_size();
  2331. dma_limit = (MAX_DMA_ADDRESS -
  2332. ((unsigned long)current_req->buffer)) >> 9;
  2333. if ((unsigned long)max_size > dma_limit)
  2334. max_size = dma_limit;
  2335. /* 64 kb boundaries */
  2336. if (CROSS_64KB(current_req->buffer, max_size << 9))
  2337. max_size = (K_64 -
  2338. ((unsigned long)current_req->buffer) %
  2339. K_64) >> 9;
  2340. direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
  2341. /*
  2342. * We try to read tracks, but if we get too many errors, we
  2343. * go back to reading just one sector at a time.
  2344. *
  2345. * This means we should be able to read a sector even if there
  2346. * are other bad sectors on this track.
  2347. */
  2348. if (!direct ||
  2349. (indirect * 2 > direct * 3 &&
  2350. *errors < DP->max_errors.read_track &&
  2351. ((!probing ||
  2352. (DP->read_track & (1 << DRS->probed_format)))))) {
  2353. max_size = blk_rq_sectors(current_req);
  2354. } else {
  2355. raw_cmd->kernel_data = current_req->buffer;
  2356. raw_cmd->length = current_count_sectors << 9;
  2357. if (raw_cmd->length == 0) {
  2358. DPRINT("%s: zero dma transfer attempted\n", __func__);
  2359. DPRINT("indirect=%d direct=%d fsector_t=%d\n",
  2360. indirect, direct, fsector_t);
  2361. return 0;
  2362. }
  2363. virtualdmabug_workaround();
  2364. return 2;
  2365. }
  2366. }
  2367. if (CT(COMMAND) == FD_READ)
  2368. max_size = max_sector; /* unbounded */
  2369. /* claim buffer track if needed */
  2370. if (buffer_track != raw_cmd->track || /* bad track */
  2371. buffer_drive != current_drive || /* bad drive */
  2372. fsector_t > buffer_max ||
  2373. fsector_t < buffer_min ||
  2374. ((CT(COMMAND) == FD_READ ||
  2375. (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
  2376. max_sector > 2 * max_buffer_sectors + buffer_min &&
  2377. max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
  2378. /* not enough space */
  2379. buffer_track = -1;
  2380. buffer_drive = current_drive;
  2381. buffer_max = buffer_min = aligned_sector_t;
  2382. }
  2383. raw_cmd->kernel_data = floppy_track_buffer +
  2384. ((aligned_sector_t - buffer_min) << 9);
  2385. if (CT(COMMAND) == FD_WRITE) {
  2386. /* copy write buffer to track buffer.
  2387. * if we get here, we know that the write
  2388. * is either aligned or the data already in the buffer
  2389. * (buffer will be overwritten) */
  2390. if (in_sector_offset && buffer_track == -1)
  2391. DPRINT("internal error offset !=0 on write\n");
  2392. buffer_track = raw_cmd->track;
  2393. buffer_drive = current_drive;
  2394. copy_buffer(ssize, max_sector,
  2395. 2 * max_buffer_sectors + buffer_min);
  2396. } else
  2397. transfer_size(ssize, max_sector,
  2398. 2 * max_buffer_sectors + buffer_min -
  2399. aligned_sector_t);
  2400. /* round up current_count_sectors to get dma xfer size */
  2401. raw_cmd->length = in_sector_offset + current_count_sectors;
  2402. raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
  2403. raw_cmd->length <<= 9;
  2404. if ((raw_cmd->length < current_count_sectors << 9) ||
  2405. (raw_cmd->kernel_data != current_req->buffer &&
  2406. CT(COMMAND) == FD_WRITE &&
  2407. (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
  2408. aligned_sector_t < buffer_min)) ||
  2409. raw_cmd->length % (128 << SIZECODE) ||
  2410. raw_cmd->length <= 0 || current_count_sectors <= 0) {
  2411. DPRINT("fractionary current count b=%lx s=%lx\n",
  2412. raw_cmd->length, current_count_sectors);
  2413. if (raw_cmd->kernel_data != current_req->buffer)
  2414. pr_info("addr=%d, length=%ld\n",
  2415. (int)((raw_cmd->kernel_data -
  2416. floppy_track_buffer) >> 9),
  2417. current_count_sectors);
  2418. pr_info("st=%d ast=%d mse=%d msi=%d\n",
  2419. fsector_t, aligned_sector_t, max_sector, max_size);
  2420. pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
  2421. pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
  2422. COMMAND, SECTOR, HEAD, TRACK);
  2423. pr_info("buffer drive=%d\n", buffer_drive);
  2424. pr_info("buffer track=%d\n", buffer_track);
  2425. pr_info("buffer_min=%d\n", buffer_min);
  2426. pr_info("buffer_max=%d\n", buffer_max);
  2427. return 0;
  2428. }
  2429. if (raw_cmd->kernel_data != current_req->buffer) {
  2430. if (raw_cmd->kernel_data < floppy_track_buffer ||
  2431. current_count_sectors < 0 ||
  2432. raw_cmd->length < 0 ||
  2433. raw_cmd->kernel_data + raw_cmd->length >
  2434. floppy_track_buffer + (max_buffer_sectors << 10)) {
  2435. DPRINT("buffer overrun in schedule dma\n");
  2436. pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
  2437. fsector_t, buffer_min, raw_cmd->length >> 9);
  2438. pr_info("current_count_sectors=%ld\n",
  2439. current_count_sectors);
  2440. if (CT(COMMAND) == FD_READ)
  2441. pr_info("read\n");
  2442. if (CT(COMMAND) == FD_WRITE)
  2443. pr_info("write\n");
  2444. return 0;
  2445. }
  2446. } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
  2447. current_count_sectors > blk_rq_sectors(current_req)) {
  2448. DPRINT("buffer overrun in direct transfer\n");
  2449. return 0;
  2450. } else if (raw_cmd->length < current_count_sectors << 9) {
  2451. DPRINT("more sectors than bytes\n");
  2452. pr_info("bytes=%ld\n", raw_cmd->length >> 9);
  2453. pr_info("sectors=%ld\n", current_count_sectors);
  2454. }
  2455. if (raw_cmd->length == 0) {
  2456. DPRINT("zero dma transfer attempted from make_raw_request\n");
  2457. return 0;
  2458. }
  2459. virtualdmabug_workaround();
  2460. return 2;
  2461. }
  2462. /*
  2463. * Round-robin between our available drives, doing one request from each
  2464. */
  2465. static int set_next_request(void)
  2466. {
  2467. struct request_queue *q;
  2468. int old_pos = fdc_queue;
  2469. do {
  2470. q = disks[fdc_queue]->queue;
  2471. if (++fdc_queue == N_DRIVE)
  2472. fdc_queue = 0;
  2473. if (q) {
  2474. current_req = blk_fetch_request(q);
  2475. if (current_req)
  2476. break;
  2477. }
  2478. } while (fdc_queue != old_pos);
  2479. return current_req != NULL;
  2480. }
  2481. static void redo_fd_request(void)
  2482. {
  2483. int drive;
  2484. int tmp;
  2485. lastredo = jiffies;
  2486. if (current_drive < N_DRIVE)
  2487. floppy_off(current_drive);
  2488. do_request:
  2489. if (!current_req) {
  2490. int pending;
  2491. spin_lock_irq(&floppy_lock);
  2492. pending = set_next_request();
  2493. spin_unlock_irq(&floppy_lock);
  2494. if (!pending) {
  2495. do_floppy = NULL;
  2496. unlock_fdc();
  2497. return;
  2498. }
  2499. }
  2500. drive = (long)current_req->rq_disk->private_data;
  2501. set_fdc(drive);
  2502. reschedule_timeout(current_reqD, "redo fd request");
  2503. set_floppy(drive);
  2504. raw_cmd = &default_raw_cmd;
  2505. raw_cmd->flags = 0;
  2506. if (start_motor(redo_fd_request))
  2507. return;
  2508. disk_change(current_drive);
  2509. if (test_bit(current_drive, &fake_change) ||
  2510. test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
  2511. DPRINT("disk absent or changed during operation\n");
  2512. request_done(0);
  2513. goto do_request;
  2514. }
  2515. if (!_floppy) { /* Autodetection */
  2516. if (!probing) {
  2517. DRS->probed_format = 0;
  2518. if (next_valid_format()) {
  2519. DPRINT("no autodetectable formats\n");
  2520. _floppy = NULL;
  2521. request_done(0);
  2522. goto do_request;
  2523. }
  2524. }
  2525. probing = 1;
  2526. _floppy = floppy_type + DP->autodetect[DRS->probed_format];
  2527. } else
  2528. probing = 0;
  2529. errors = &(current_req->errors);
  2530. tmp = make_raw_rw_request();
  2531. if (tmp < 2) {
  2532. request_done(tmp);
  2533. goto do_request;
  2534. }
  2535. if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
  2536. twaddle();
  2537. schedule_bh(floppy_start);
  2538. debugt(__func__, "queue fd request");
  2539. return;
  2540. }
  2541. static const struct cont_t rw_cont = {
  2542. .interrupt = rw_interrupt,
  2543. .redo = redo_fd_request,
  2544. .error = bad_flp_intr,
  2545. .done = request_done
  2546. };
  2547. static void process_fd_request(void)
  2548. {
  2549. cont = &rw_cont;
  2550. schedule_bh(redo_fd_request);
  2551. }
  2552. static void do_fd_request(struct request_queue *q)
  2553. {
  2554. if (WARN(max_buffer_sectors == 0,
  2555. "VFS: %s called on non-open device\n", __func__))
  2556. return;
  2557. if (WARN(atomic_read(&usage_count) == 0,
  2558. "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
  2559. current_req, (long)blk_rq_pos(current_req), current_req->cmd_type,
  2560. current_req->cmd_flags))
  2561. return;
  2562. if (test_bit(0, &fdc_busy)) {
  2563. /* fdc busy, this new request will be treated when the
  2564. current one is done */
  2565. is_alive(__func__, "old request running");
  2566. return;
  2567. }
  2568. lock_fdc(MAXTIMEOUT, false);
  2569. process_fd_request();
  2570. is_alive(__func__, "");
  2571. }
  2572. static const struct cont_t poll_cont = {
  2573. .interrupt = success_and_wakeup,
  2574. .redo = floppy_ready,
  2575. .error = generic_failure,
  2576. .done = generic_done
  2577. };
  2578. static int poll_drive(bool interruptible, int flag)
  2579. {
  2580. /* no auto-sense, just clear dcl */
  2581. raw_cmd = &default_raw_cmd;
  2582. raw_cmd->flags = flag;
  2583. raw_cmd->track = 0;
  2584. raw_cmd->cmd_count = 0;
  2585. cont = &poll_cont;
  2586. debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
  2587. set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
  2588. return wait_til_done(floppy_ready, interruptible);
  2589. }
  2590. /*
  2591. * User triggered reset
  2592. * ====================
  2593. */
  2594. static void reset_intr(void)
  2595. {
  2596. pr_info("weird, reset interrupt called\n");
  2597. }
  2598. static const struct cont_t reset_cont = {
  2599. .interrupt = reset_intr,
  2600. .redo = success_and_wakeup,
  2601. .error = generic_failure,
  2602. .done = generic_done
  2603. };
  2604. static int user_reset_fdc(int drive, int arg, bool interruptible)
  2605. {
  2606. int ret;
  2607. if (lock_fdc(drive, interruptible))
  2608. return -EINTR;
  2609. if (arg == FD_RESET_ALWAYS)
  2610. FDCS->reset = 1;
  2611. if (FDCS->reset) {
  2612. cont = &reset_cont;
  2613. ret = wait_til_done(reset_fdc, interruptible);
  2614. if (ret == -EINTR)
  2615. return -EINTR;
  2616. }
  2617. process_fd_request();
  2618. return 0;
  2619. }
  2620. /*
  2621. * Misc Ioctl's and support
  2622. * ========================
  2623. */
  2624. static inline int fd_copyout(void __user *param, const void *address,
  2625. unsigned long size)
  2626. {
  2627. return copy_to_user(param, address, size) ? -EFAULT : 0;
  2628. }
  2629. static inline int fd_copyin(void __user *param, void *address,
  2630. unsigned long size)
  2631. {
  2632. return copy_from_user(address, param, size) ? -EFAULT : 0;
  2633. }
  2634. static const char *drive_name(int type, int drive)
  2635. {
  2636. struct floppy_struct *floppy;
  2637. if (type)
  2638. floppy = floppy_type + type;
  2639. else {
  2640. if (UDP->native_format)
  2641. floppy = floppy_type + UDP->native_format;
  2642. else
  2643. return "(null)";
  2644. }
  2645. if (floppy->name)
  2646. return floppy->name;
  2647. else
  2648. return "(null)";
  2649. }
  2650. /* raw commands */
  2651. static void raw_cmd_done(int flag)
  2652. {
  2653. int i;
  2654. if (!flag) {
  2655. raw_cmd->flags |= FD_RAW_FAILURE;
  2656. raw_cmd->flags |= FD_RAW_HARDFAILURE;
  2657. } else {
  2658. raw_cmd->reply_count = inr;
  2659. if (raw_cmd->reply_count > MAX_REPLIES)
  2660. raw_cmd->reply_count = 0;
  2661. for (i = 0; i < raw_cmd->reply_count; i++)
  2662. raw_cmd->reply[i] = reply_buffer[i];
  2663. if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2664. unsigned long flags;
  2665. flags = claim_dma_lock();
  2666. raw_cmd->length = fd_get_dma_residue();
  2667. release_dma_lock(flags);
  2668. }
  2669. if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
  2670. (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
  2671. raw_cmd->flags |= FD_RAW_FAILURE;
  2672. if (disk_change(current_drive))
  2673. raw_cmd->flags |= FD_RAW_DISK_CHANGE;
  2674. else
  2675. raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
  2676. if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
  2677. motor_off_callback(current_drive);
  2678. if (raw_cmd->next &&
  2679. (!(raw_cmd->flags & FD_RAW_FAILURE) ||
  2680. !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
  2681. ((raw_cmd->flags & FD_RAW_FAILURE) ||
  2682. !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
  2683. raw_cmd = raw_cmd->next;
  2684. return;
  2685. }
  2686. }
  2687. generic_done(flag);
  2688. }
  2689. static const struct cont_t raw_cmd_cont = {
  2690. .interrupt = success_and_wakeup,
  2691. .redo = floppy_start,
  2692. .error = generic_failure,
  2693. .done = raw_cmd_done
  2694. };
  2695. static int raw_cmd_copyout(int cmd, void __user *param,
  2696. struct floppy_raw_cmd *ptr)
  2697. {
  2698. int ret;
  2699. while (ptr) {
  2700. ret = copy_to_user(param, ptr, sizeof(*ptr));
  2701. if (ret)
  2702. return -EFAULT;
  2703. param += sizeof(struct floppy_raw_cmd);
  2704. if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
  2705. if (ptr->length >= 0 &&
  2706. ptr->length <= ptr->buffer_length) {
  2707. long length = ptr->buffer_length - ptr->length;
  2708. ret = fd_copyout(ptr->data, ptr->kernel_data,
  2709. length);
  2710. if (ret)
  2711. return ret;
  2712. }
  2713. }
  2714. ptr = ptr->next;
  2715. }
  2716. return 0;
  2717. }
  2718. static void raw_cmd_free(struct floppy_raw_cmd **ptr)
  2719. {
  2720. struct floppy_raw_cmd *next;
  2721. struct floppy_raw_cmd *this;
  2722. this = *ptr;
  2723. *ptr = NULL;
  2724. while (this) {
  2725. if (this->buffer_length) {
  2726. fd_dma_mem_free((unsigned long)this->kernel_data,
  2727. this->buffer_length);
  2728. this->buffer_length = 0;
  2729. }
  2730. next = this->next;
  2731. kfree(this);
  2732. this = next;
  2733. }
  2734. }
  2735. static int raw_cmd_copyin(int cmd, void __user *param,
  2736. struct floppy_raw_cmd **rcmd)
  2737. {
  2738. struct floppy_raw_cmd *ptr;
  2739. int ret;
  2740. int i;
  2741. *rcmd = NULL;
  2742. loop:
  2743. ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
  2744. if (!ptr)
  2745. return -ENOMEM;
  2746. *rcmd = ptr;
  2747. ret = copy_from_user(ptr, param, sizeof(*ptr));
  2748. if (ret)
  2749. return -EFAULT;
  2750. ptr->next = NULL;
  2751. ptr->buffer_length = 0;
  2752. param += sizeof(struct floppy_raw_cmd);
  2753. if (ptr->cmd_count > 33)
  2754. /* the command may now also take up the space
  2755. * initially intended for the reply & the
  2756. * reply count. Needed for long 82078 commands
  2757. * such as RESTORE, which takes ... 17 command
  2758. * bytes. Murphy's law #137: When you reserve
  2759. * 16 bytes for a structure, you'll one day
  2760. * discover that you really need 17...
  2761. */
  2762. return -EINVAL;
  2763. for (i = 0; i < 16; i++)
  2764. ptr->reply[i] = 0;
  2765. ptr->resultcode = 0;
  2766. ptr->kernel_data = NULL;
  2767. if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2768. if (ptr->length <= 0)
  2769. return -EINVAL;
  2770. ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
  2771. fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
  2772. if (!ptr->kernel_data)
  2773. return -ENOMEM;
  2774. ptr->buffer_length = ptr->length;
  2775. }
  2776. if (ptr->flags & FD_RAW_WRITE) {
  2777. ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
  2778. if (ret)
  2779. return ret;
  2780. }
  2781. if (ptr->flags & FD_RAW_MORE) {
  2782. rcmd = &(ptr->next);
  2783. ptr->rate &= 0x43;
  2784. goto loop;
  2785. }
  2786. return 0;
  2787. }
  2788. static int raw_cmd_ioctl(int cmd, void __user *param)
  2789. {
  2790. struct floppy_raw_cmd *my_raw_cmd;
  2791. int drive;
  2792. int ret2;
  2793. int ret;
  2794. if (FDCS->rawcmd <= 1)
  2795. FDCS->rawcmd = 1;
  2796. for (drive = 0; drive < N_DRIVE; drive++) {
  2797. if (FDC(drive) != fdc)
  2798. continue;
  2799. if (drive == current_drive) {
  2800. if (UDRS->fd_ref > 1) {
  2801. FDCS->rawcmd = 2;
  2802. break;
  2803. }
  2804. } else if (UDRS->fd_ref) {
  2805. FDCS->rawcmd = 2;
  2806. break;
  2807. }
  2808. }
  2809. if (FDCS->reset)
  2810. return -EIO;
  2811. ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
  2812. if (ret) {
  2813. raw_cmd_free(&my_raw_cmd);
  2814. return ret;
  2815. }
  2816. raw_cmd = my_raw_cmd;
  2817. cont = &raw_cmd_cont;
  2818. ret = wait_til_done(floppy_start, true);
  2819. debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl\n");
  2820. if (ret != -EINTR && FDCS->reset)
  2821. ret = -EIO;
  2822. DRS->track = NO_TRACK;
  2823. ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
  2824. if (!ret)
  2825. ret = ret2;
  2826. raw_cmd_free(&my_raw_cmd);
  2827. return ret;
  2828. }
  2829. static int invalidate_drive(struct block_device *bdev)
  2830. {
  2831. /* invalidate the buffer track to force a reread */
  2832. set_bit((long)bdev->bd_disk->private_data, &fake_change);
  2833. process_fd_request();
  2834. check_disk_change(bdev);
  2835. return 0;
  2836. }
  2837. static int set_geometry(unsigned int cmd, struct floppy_struct *g,
  2838. int drive, int type, struct block_device *bdev)
  2839. {
  2840. int cnt;
  2841. /* sanity checking for parameters. */
  2842. if (g->sect <= 0 ||
  2843. g->head <= 0 ||
  2844. g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
  2845. /* check if reserved bits are set */
  2846. (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
  2847. return -EINVAL;
  2848. if (type) {
  2849. if (!capable(CAP_SYS_ADMIN))
  2850. return -EPERM;
  2851. mutex_lock(&open_lock);
  2852. if (lock_fdc(drive, true)) {
  2853. mutex_unlock(&open_lock);
  2854. return -EINTR;
  2855. }
  2856. floppy_type[type] = *g;
  2857. floppy_type[type].name = "user format";
  2858. for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
  2859. floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
  2860. floppy_type[type].size + 1;
  2861. process_fd_request();
  2862. for (cnt = 0; cnt < N_DRIVE; cnt++) {
  2863. struct block_device *bdev = opened_bdev[cnt];
  2864. if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
  2865. continue;
  2866. __invalidate_device(bdev, true);
  2867. }
  2868. mutex_unlock(&open_lock);
  2869. } else {
  2870. int oldStretch;
  2871. if (lock_fdc(drive, true))
  2872. return -EINTR;
  2873. if (cmd != FDDEFPRM) {
  2874. /* notice a disk change immediately, else
  2875. * we lose our settings immediately*/
  2876. if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
  2877. return -EINTR;
  2878. }
  2879. oldStretch = g->stretch;
  2880. user_params[drive] = *g;
  2881. if (buffer_drive == drive)
  2882. SUPBOUND(buffer_max, user_params[drive].sect);
  2883. current_type[drive] = &user_params[drive];
  2884. floppy_sizes[drive] = user_params[drive].size;
  2885. if (cmd == FDDEFPRM)
  2886. DRS->keep_data = -1;
  2887. else
  2888. DRS->keep_data = 1;
  2889. /* invalidation. Invalidate only when needed, i.e.
  2890. * when there are already sectors in the buffer cache
  2891. * whose number will change. This is useful, because
  2892. * mtools often changes the geometry of the disk after
  2893. * looking at the boot block */
  2894. if (DRS->maxblock > user_params[drive].sect ||
  2895. DRS->maxtrack ||
  2896. ((user_params[drive].sect ^ oldStretch) &
  2897. (FD_SWAPSIDES | FD_SECTBASEMASK)))
  2898. invalidate_drive(bdev);
  2899. else
  2900. process_fd_request();
  2901. }
  2902. return 0;
  2903. }
  2904. /* handle obsolete ioctl's */
  2905. static unsigned int ioctl_table[] = {
  2906. FDCLRPRM,
  2907. FDSETPRM,
  2908. FDDEFPRM,
  2909. FDGETPRM,
  2910. FDMSGON,
  2911. FDMSGOFF,
  2912. FDFMTBEG,
  2913. FDFMTTRK,
  2914. FDFMTEND,
  2915. FDSETEMSGTRESH,
  2916. FDFLUSH,
  2917. FDSETMAXERRS,
  2918. FDGETMAXERRS,
  2919. FDGETDRVTYP,
  2920. FDSETDRVPRM,
  2921. FDGETDRVPRM,
  2922. FDGETDRVSTAT,
  2923. FDPOLLDRVSTAT,
  2924. FDRESET,
  2925. FDGETFDCSTAT,
  2926. FDWERRORCLR,
  2927. FDWERRORGET,
  2928. FDRAWCMD,
  2929. FDEJECT,
  2930. FDTWADDLE
  2931. };
  2932. static int normalize_ioctl(unsigned int *cmd, int *size)
  2933. {
  2934. int i;
  2935. for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
  2936. if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
  2937. *size = _IOC_SIZE(*cmd);
  2938. *cmd = ioctl_table[i];
  2939. if (*size > _IOC_SIZE(*cmd)) {
  2940. pr_info("ioctl not yet supported\n");
  2941. return -EFAULT;
  2942. }
  2943. return 0;
  2944. }
  2945. }
  2946. return -EINVAL;
  2947. }
  2948. static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
  2949. {
  2950. if (type)
  2951. *g = &floppy_type[type];
  2952. else {
  2953. if (lock_fdc(drive, false))
  2954. return -EINTR;
  2955. if (poll_drive(false, 0) == -EINTR)
  2956. return -EINTR;
  2957. process_fd_request();
  2958. *g = current_type[drive];
  2959. }
  2960. if (!*g)
  2961. return -ENODEV;
  2962. return 0;
  2963. }
  2964. static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  2965. {
  2966. int drive = (long)bdev->bd_disk->private_data;
  2967. int type = ITYPE(drive_state[drive].fd_device);
  2968. struct floppy_struct *g;
  2969. int ret;
  2970. ret = get_floppy_geometry(drive, type, &g);
  2971. if (ret)
  2972. return ret;
  2973. geo->heads = g->head;
  2974. geo->sectors = g->sect;
  2975. geo->cylinders = g->track;
  2976. return 0;
  2977. }
  2978. static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  2979. unsigned long param)
  2980. {
  2981. int drive = (long)bdev->bd_disk->private_data;
  2982. int type = ITYPE(UDRS->fd_device);
  2983. int i;
  2984. int ret;
  2985. int size;
  2986. union inparam {
  2987. struct floppy_struct g; /* geometry */
  2988. struct format_descr f;
  2989. struct floppy_max_errors max_errors;
  2990. struct floppy_drive_params dp;
  2991. } inparam; /* parameters coming from user space */
  2992. const void *outparam; /* parameters passed back to user space */
  2993. /* convert compatibility eject ioctls into floppy eject ioctl.
  2994. * We do this in order to provide a means to eject floppy disks before
  2995. * installing the new fdutils package */
  2996. if (cmd == CDROMEJECT || /* CD-ROM eject */
  2997. cmd == 0x6470) { /* SunOS floppy eject */
  2998. DPRINT("obsolete eject ioctl\n");
  2999. DPRINT("please use floppycontrol --eject\n");
  3000. cmd = FDEJECT;
  3001. }
  3002. if (!((cmd & 0xff00) == 0x0200))
  3003. return -EINVAL;
  3004. /* convert the old style command into a new style command */
  3005. ret = normalize_ioctl(&cmd, &size);
  3006. if (ret)
  3007. return ret;
  3008. /* permission checks */
  3009. if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
  3010. ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
  3011. return -EPERM;
  3012. if (WARN_ON(size < 0 || size > sizeof(inparam)))
  3013. return -EINVAL;
  3014. /* copyin */
  3015. memset(&inparam, 0, sizeof(inparam));
  3016. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  3017. ret = fd_copyin((void __user *)param, &inparam, size);
  3018. if (ret)
  3019. return ret;
  3020. }
  3021. switch (cmd) {
  3022. case FDEJECT:
  3023. if (UDRS->fd_ref != 1)
  3024. /* somebody else has this drive open */
  3025. return -EBUSY;
  3026. if (lock_fdc(drive, true))
  3027. return -EINTR;
  3028. /* do the actual eject. Fails on
  3029. * non-Sparc architectures */
  3030. ret = fd_eject(UNIT(drive));
  3031. set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  3032. set_bit(FD_VERIFY_BIT, &UDRS->flags);
  3033. process_fd_request();
  3034. return ret;
  3035. case FDCLRPRM:
  3036. if (lock_fdc(drive, true))
  3037. return -EINTR;
  3038. current_type[drive] = NULL;
  3039. floppy_sizes[drive] = MAX_DISK_SIZE << 1;
  3040. UDRS->keep_data = 0;
  3041. return invalidate_drive(bdev);
  3042. case FDSETPRM:
  3043. case FDDEFPRM:
  3044. return set_geometry(cmd, &inparam.g, drive, type, bdev);
  3045. case FDGETPRM:
  3046. ret = get_floppy_geometry(drive, type,
  3047. (struct floppy_struct **)&outparam);
  3048. if (ret)
  3049. return ret;
  3050. break;
  3051. case FDMSGON:
  3052. UDP->flags |= FTD_MSG;
  3053. return 0;
  3054. case FDMSGOFF:
  3055. UDP->flags &= ~FTD_MSG;
  3056. return 0;
  3057. case FDFMTBEG:
  3058. if (lock_fdc(drive, true))
  3059. return -EINTR;
  3060. if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
  3061. return -EINTR;
  3062. ret = UDRS->flags;
  3063. process_fd_request();
  3064. if (ret & FD_VERIFY)
  3065. return -ENODEV;
  3066. if (!(ret & FD_DISK_WRITABLE))
  3067. return -EROFS;
  3068. return 0;
  3069. case FDFMTTRK:
  3070. if (UDRS->fd_ref != 1)
  3071. return -EBUSY;
  3072. return do_format(drive, &inparam.f);
  3073. case FDFMTEND:
  3074. case FDFLUSH:
  3075. if (lock_fdc(drive, true))
  3076. return -EINTR;
  3077. return invalidate_drive(bdev);
  3078. case FDSETEMSGTRESH:
  3079. UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
  3080. return 0;
  3081. case FDGETMAXERRS:
  3082. outparam = &UDP->max_errors;
  3083. break;
  3084. case FDSETMAXERRS:
  3085. UDP->max_errors = inparam.max_errors;
  3086. break;
  3087. case FDGETDRVTYP:
  3088. outparam = drive_name(type, drive);
  3089. SUPBOUND(size, strlen((const char *)outparam) + 1);
  3090. break;
  3091. case FDSETDRVPRM:
  3092. *UDP = inparam.dp;
  3093. break;
  3094. case FDGETDRVPRM:
  3095. outparam = UDP;
  3096. break;
  3097. case FDPOLLDRVSTAT:
  3098. if (lock_fdc(drive, true))
  3099. return -EINTR;
  3100. if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
  3101. return -EINTR;
  3102. process_fd_request();
  3103. /* fall through */
  3104. case FDGETDRVSTAT:
  3105. outparam = UDRS;
  3106. break;
  3107. case FDRESET:
  3108. return user_reset_fdc(drive, (int)param, true);
  3109. case FDGETFDCSTAT:
  3110. outparam = UFDCS;
  3111. break;
  3112. case FDWERRORCLR:
  3113. memset(UDRWE, 0, sizeof(*UDRWE));
  3114. return 0;
  3115. case FDWERRORGET:
  3116. outparam = UDRWE;
  3117. break;
  3118. case FDRAWCMD:
  3119. if (type)
  3120. return -EINVAL;
  3121. if (lock_fdc(drive, true))
  3122. return -EINTR;
  3123. set_floppy(drive);
  3124. i = raw_cmd_ioctl(cmd, (void __user *)param);
  3125. if (i == -EINTR)
  3126. return -EINTR;
  3127. process_fd_request();
  3128. return i;
  3129. case FDTWADDLE:
  3130. if (lock_fdc(drive, true))
  3131. return -EINTR;
  3132. twaddle();
  3133. process_fd_request();
  3134. return 0;
  3135. default:
  3136. return -EINVAL;
  3137. }
  3138. if (_IOC_DIR(cmd) & _IOC_READ)
  3139. return fd_copyout((void __user *)param, outparam, size);
  3140. return 0;
  3141. }
  3142. static int fd_ioctl(struct block_device *bdev, fmode_t mode,
  3143. unsigned int cmd, unsigned long param)
  3144. {
  3145. int ret;
  3146. mutex_lock(&floppy_mutex);
  3147. ret = fd_locked_ioctl(bdev, mode, cmd, param);
  3148. mutex_unlock(&floppy_mutex);
  3149. return ret;
  3150. }
  3151. static void __init config_types(void)
  3152. {
  3153. bool has_drive = false;
  3154. int drive;
  3155. /* read drive info out of physical CMOS */
  3156. drive = 0;
  3157. if (!UDP->cmos)
  3158. UDP->cmos = FLOPPY0_TYPE;
  3159. drive = 1;
  3160. if (!UDP->cmos && FLOPPY1_TYPE)
  3161. UDP->cmos = FLOPPY1_TYPE;
  3162. /* FIXME: additional physical CMOS drive detection should go here */
  3163. for (drive = 0; drive < N_DRIVE; drive++) {
  3164. unsigned int type = UDP->cmos;
  3165. struct floppy_drive_params *params;
  3166. const char *name = NULL;
  3167. static char temparea[32];
  3168. if (type < ARRAY_SIZE(default_drive_params)) {
  3169. params = &default_drive_params[type].params;
  3170. if (type) {
  3171. name = default_drive_params[type].name;
  3172. allowed_drive_mask |= 1 << drive;
  3173. } else
  3174. allowed_drive_mask &= ~(1 << drive);
  3175. } else {
  3176. params = &default_drive_params[0].params;
  3177. sprintf(temparea, "unknown type %d (usb?)", type);
  3178. name = temparea;
  3179. }
  3180. if (name) {
  3181. const char *prepend;
  3182. if (!has_drive) {
  3183. prepend = "";
  3184. has_drive = true;
  3185. pr_info("Floppy drive(s):");
  3186. } else {
  3187. prepend = ",";
  3188. }
  3189. pr_cont("%s fd%d is %s", prepend, drive, name);
  3190. }
  3191. *UDP = *params;
  3192. }
  3193. if (has_drive)
  3194. pr_cont("\n");
  3195. }
  3196. static int floppy_release(struct gendisk *disk, fmode_t mode)
  3197. {
  3198. int drive = (long)disk->private_data;
  3199. mutex_lock(&floppy_mutex);
  3200. mutex_lock(&open_lock);
  3201. if (UDRS->fd_ref < 0)
  3202. UDRS->fd_ref = 0;
  3203. else if (!UDRS->fd_ref--) {
  3204. DPRINT("floppy_release with fd_ref == 0");
  3205. UDRS->fd_ref = 0;
  3206. }
  3207. if (!UDRS->fd_ref)
  3208. opened_bdev[drive] = NULL;
  3209. mutex_unlock(&open_lock);
  3210. mutex_unlock(&floppy_mutex);
  3211. return 0;
  3212. }
  3213. /*
  3214. * floppy_open check for aliasing (/dev/fd0 can be the same as
  3215. * /dev/PS0 etc), and disallows simultaneous access to the same
  3216. * drive with different device numbers.
  3217. */
  3218. static int floppy_open(struct block_device *bdev, fmode_t mode)
  3219. {
  3220. int drive = (long)bdev->bd_disk->private_data;
  3221. int old_dev, new_dev;
  3222. int try;
  3223. int res = -EBUSY;
  3224. char *tmp;
  3225. mutex_lock(&floppy_mutex);
  3226. mutex_lock(&open_lock);
  3227. old_dev = UDRS->fd_device;
  3228. if (opened_bdev[drive] && opened_bdev[drive] != bdev)
  3229. goto out2;
  3230. if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
  3231. set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  3232. set_bit(FD_VERIFY_BIT, &UDRS->flags);
  3233. }
  3234. if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (mode & FMODE_EXCL)))
  3235. goto out2;
  3236. if (mode & FMODE_EXCL)
  3237. UDRS->fd_ref = -1;
  3238. else
  3239. UDRS->fd_ref++;
  3240. opened_bdev[drive] = bdev;
  3241. res = -ENXIO;
  3242. if (!floppy_track_buffer) {
  3243. /* if opening an ED drive, reserve a big buffer,
  3244. * else reserve a small one */
  3245. if ((UDP->cmos == 6) || (UDP->cmos == 5))
  3246. try = 64; /* Only 48 actually useful */
  3247. else
  3248. try = 32; /* Only 24 actually useful */
  3249. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3250. if (!tmp && !floppy_track_buffer) {
  3251. try >>= 1; /* buffer only one side */
  3252. INFBOUND(try, 16);
  3253. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3254. }
  3255. if (!tmp && !floppy_track_buffer)
  3256. fallback_on_nodma_alloc(&tmp, 2048 * try);
  3257. if (!tmp && !floppy_track_buffer) {
  3258. DPRINT("Unable to allocate DMA memory\n");
  3259. goto out;
  3260. }
  3261. if (floppy_track_buffer) {
  3262. if (tmp)
  3263. fd_dma_mem_free((unsigned long)tmp, try * 1024);
  3264. } else {
  3265. buffer_min = buffer_max = -1;
  3266. floppy_track_buffer = tmp;
  3267. max_buffer_sectors = try;
  3268. }
  3269. }
  3270. new_dev = MINOR(bdev->bd_dev);
  3271. UDRS->fd_device = new_dev;
  3272. set_capacity(disks[drive], floppy_sizes[new_dev]);
  3273. if (old_dev != -1 && old_dev != new_dev) {
  3274. if (buffer_drive == drive)
  3275. buffer_track = -1;
  3276. }
  3277. if (UFDCS->rawcmd == 1)
  3278. UFDCS->rawcmd = 2;
  3279. if (!(mode & FMODE_NDELAY)) {
  3280. if (mode & (FMODE_READ|FMODE_WRITE)) {
  3281. UDRS->last_checked = 0;
  3282. check_disk_change(bdev);
  3283. if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
  3284. goto out;
  3285. }
  3286. res = -EROFS;
  3287. if ((mode & FMODE_WRITE) &&
  3288. !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
  3289. goto out;
  3290. }
  3291. mutex_unlock(&open_lock);
  3292. mutex_unlock(&floppy_mutex);
  3293. return 0;
  3294. out:
  3295. if (UDRS->fd_ref < 0)
  3296. UDRS->fd_ref = 0;
  3297. else
  3298. UDRS->fd_ref--;
  3299. if (!UDRS->fd_ref)
  3300. opened_bdev[drive] = NULL;
  3301. out2:
  3302. mutex_unlock(&open_lock);
  3303. mutex_unlock(&floppy_mutex);
  3304. return res;
  3305. }
  3306. /*
  3307. * Check if the disk has been changed or if a change has been faked.
  3308. */
  3309. static unsigned int floppy_check_events(struct gendisk *disk,
  3310. unsigned int clearing)
  3311. {
  3312. int drive = (long)disk->private_data;
  3313. if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  3314. test_bit(FD_VERIFY_BIT, &UDRS->flags))
  3315. return DISK_EVENT_MEDIA_CHANGE;
  3316. if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
  3317. lock_fdc(drive, false);
  3318. poll_drive(false, 0);
  3319. process_fd_request();
  3320. }
  3321. if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  3322. test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
  3323. test_bit(drive, &fake_change) ||
  3324. drive_no_geom(drive))
  3325. return DISK_EVENT_MEDIA_CHANGE;
  3326. return 0;
  3327. }
  3328. /*
  3329. * This implements "read block 0" for floppy_revalidate().
  3330. * Needed for format autodetection, checking whether there is
  3331. * a disk in the drive, and whether that disk is writable.
  3332. */
  3333. static void floppy_rb0_complete(struct bio *bio, int err)
  3334. {
  3335. complete((struct completion *)bio->bi_private);
  3336. }
  3337. static int __floppy_read_block_0(struct block_device *bdev)
  3338. {
  3339. struct bio bio;
  3340. struct bio_vec bio_vec;
  3341. struct completion complete;
  3342. struct page *page;
  3343. size_t size;
  3344. page = alloc_page(GFP_NOIO);
  3345. if (!page) {
  3346. process_fd_request();
  3347. return -ENOMEM;
  3348. }
  3349. size = bdev->bd_block_size;
  3350. if (!size)
  3351. size = 1024;
  3352. bio_init(&bio);
  3353. bio.bi_io_vec = &bio_vec;
  3354. bio_vec.bv_page = page;
  3355. bio_vec.bv_len = size;
  3356. bio_vec.bv_offset = 0;
  3357. bio.bi_vcnt = 1;
  3358. bio.bi_idx = 0;
  3359. bio.bi_size = size;
  3360. bio.bi_bdev = bdev;
  3361. bio.bi_sector = 0;
  3362. bio.bi_flags = BIO_QUIET;
  3363. init_completion(&complete);
  3364. bio.bi_private = &complete;
  3365. bio.bi_end_io = floppy_rb0_complete;
  3366. submit_bio(READ, &bio);
  3367. process_fd_request();
  3368. wait_for_completion(&complete);
  3369. __free_page(page);
  3370. return 0;
  3371. }
  3372. /* revalidate the floppy disk, i.e. trigger format autodetection by reading
  3373. * the bootblock (block 0). "Autodetection" is also needed to check whether
  3374. * there is a disk in the drive at all... Thus we also do it for fixed
  3375. * geometry formats */
  3376. static int floppy_revalidate(struct gendisk *disk)
  3377. {
  3378. int drive = (long)disk->private_data;
  3379. int cf;
  3380. int res = 0;
  3381. if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  3382. test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
  3383. test_bit(drive, &fake_change) ||
  3384. drive_no_geom(drive)) {
  3385. if (WARN(atomic_read(&usage_count) == 0,
  3386. "VFS: revalidate called on non-open device.\n"))
  3387. return -EFAULT;
  3388. lock_fdc(drive, false);
  3389. cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  3390. test_bit(FD_VERIFY_BIT, &UDRS->flags));
  3391. if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
  3392. process_fd_request(); /*already done by another thread */
  3393. return 0;
  3394. }
  3395. UDRS->maxblock = 0;
  3396. UDRS->maxtrack = 0;
  3397. if (buffer_drive == drive)
  3398. buffer_track = -1;
  3399. clear_bit(drive, &fake_change);
  3400. clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  3401. if (cf)
  3402. UDRS->generation++;
  3403. if (drive_no_geom(drive)) {
  3404. /* auto-sensing */
  3405. res = __floppy_read_block_0(opened_bdev[drive]);
  3406. } else {
  3407. if (cf)
  3408. poll_drive(false, FD_RAW_NEED_DISK);
  3409. process_fd_request();
  3410. }
  3411. }
  3412. set_capacity(disk, floppy_sizes[UDRS->fd_device]);
  3413. return res;
  3414. }
  3415. static const struct block_device_operations floppy_fops = {
  3416. .owner = THIS_MODULE,
  3417. .open = floppy_open,
  3418. .release = floppy_release,
  3419. .ioctl = fd_ioctl,
  3420. .getgeo = fd_getgeo,
  3421. .check_events = floppy_check_events,
  3422. .revalidate_disk = floppy_revalidate,
  3423. };
  3424. /*
  3425. * Floppy Driver initialization
  3426. * =============================
  3427. */
  3428. /* Determine the floppy disk controller type */
  3429. /* This routine was written by David C. Niemi */
  3430. static char __init get_fdc_version(void)
  3431. {
  3432. int r;
  3433. output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
  3434. if (FDCS->reset)
  3435. return FDC_NONE;
  3436. r = result();
  3437. if (r <= 0x00)
  3438. return FDC_NONE; /* No FDC present ??? */
  3439. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3440. pr_info("FDC %d is an 8272A\n", fdc);
  3441. return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
  3442. }
  3443. if (r != 10) {
  3444. pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
  3445. fdc, r);
  3446. return FDC_UNKNOWN;
  3447. }
  3448. if (!fdc_configure()) {
  3449. pr_info("FDC %d is an 82072\n", fdc);
  3450. return FDC_82072; /* 82072 doesn't know CONFIGURE */
  3451. }
  3452. output_byte(FD_PERPENDICULAR);
  3453. if (need_more_output() == MORE_OUTPUT) {
  3454. output_byte(0);
  3455. } else {
  3456. pr_info("FDC %d is an 82072A\n", fdc);
  3457. return FDC_82072A; /* 82072A as found on Sparcs. */
  3458. }
  3459. output_byte(FD_UNLOCK);
  3460. r = result();
  3461. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3462. pr_info("FDC %d is a pre-1991 82077\n", fdc);
  3463. return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
  3464. * LOCK/UNLOCK */
  3465. }
  3466. if ((r != 1) || (reply_buffer[0] != 0x00)) {
  3467. pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
  3468. fdc, r);
  3469. return FDC_UNKNOWN;
  3470. }
  3471. output_byte(FD_PARTID);
  3472. r = result();
  3473. if (r != 1) {
  3474. pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
  3475. fdc, r);
  3476. return FDC_UNKNOWN;
  3477. }
  3478. if (reply_buffer[0] == 0x80) {
  3479. pr_info("FDC %d is a post-1991 82077\n", fdc);
  3480. return FDC_82077; /* Revised 82077AA passes all the tests */
  3481. }
  3482. switch (reply_buffer[0] >> 5) {
  3483. case 0x0:
  3484. /* Either a 82078-1 or a 82078SL running at 5Volt */
  3485. pr_info("FDC %d is an 82078.\n", fdc);
  3486. return FDC_82078;
  3487. case 0x1:
  3488. pr_info("FDC %d is a 44pin 82078\n", fdc);
  3489. return FDC_82078;
  3490. case 0x2:
  3491. pr_info("FDC %d is a S82078B\n", fdc);
  3492. return FDC_S82078B;
  3493. case 0x3:
  3494. pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
  3495. return FDC_87306;
  3496. default:
  3497. pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
  3498. fdc, reply_buffer[0] >> 5);
  3499. return FDC_82078_UNKN;
  3500. }
  3501. } /* get_fdc_version */
  3502. /* lilo configuration */
  3503. static void __init floppy_set_flags(int *ints, int param, int param2)
  3504. {
  3505. int i;
  3506. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3507. if (param)
  3508. default_drive_params[i].params.flags |= param2;
  3509. else
  3510. default_drive_params[i].params.flags &= ~param2;
  3511. }
  3512. DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
  3513. }
  3514. static void __init daring(int *ints, int param, int param2)
  3515. {
  3516. int i;
  3517. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3518. if (param) {
  3519. default_drive_params[i].params.select_delay = 0;
  3520. default_drive_params[i].params.flags |=
  3521. FD_SILENT_DCL_CLEAR;
  3522. } else {
  3523. default_drive_params[i].params.select_delay =
  3524. 2 * HZ / 100;
  3525. default_drive_params[i].params.flags &=
  3526. ~FD_SILENT_DCL_CLEAR;
  3527. }
  3528. }
  3529. DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
  3530. }
  3531. static void __init set_cmos(int *ints, int dummy, int dummy2)
  3532. {
  3533. int current_drive = 0;
  3534. if (ints[0] != 2) {
  3535. DPRINT("wrong number of parameters for CMOS\n");
  3536. return;
  3537. }
  3538. current_drive = ints[1];
  3539. if (current_drive < 0 || current_drive >= 8) {
  3540. DPRINT("bad drive for set_cmos\n");
  3541. return;
  3542. }
  3543. #if N_FDC > 1
  3544. if (current_drive >= 4 && !FDC2)
  3545. FDC2 = 0x370;
  3546. #endif
  3547. DP->cmos = ints[2];
  3548. DPRINT("setting CMOS code to %d\n", ints[2]);
  3549. }
  3550. static struct param_table {
  3551. const char *name;
  3552. void (*fn) (int *ints, int param, int param2);
  3553. int *var;
  3554. int def_param;
  3555. int param2;
  3556. } config_params[] __initdata = {
  3557. {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3558. {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3559. {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
  3560. {"irq", NULL, &FLOPPY_IRQ, 6, 0},
  3561. {"dma", NULL, &FLOPPY_DMA, 2, 0},
  3562. {"daring", daring, NULL, 1, 0},
  3563. #if N_FDC > 1
  3564. {"two_fdc", NULL, &FDC2, 0x370, 0},
  3565. {"one_fdc", NULL, &FDC2, 0, 0},
  3566. #endif
  3567. {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
  3568. {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
  3569. {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
  3570. {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
  3571. {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
  3572. {"nodma", NULL, &can_use_virtual_dma, 1, 0},
  3573. {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
  3574. {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
  3575. {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
  3576. {"nofifo", NULL, &no_fifo, 0x20, 0},
  3577. {"usefifo", NULL, &no_fifo, 0, 0},
  3578. {"cmos", set_cmos, NULL, 0, 0},
  3579. {"slow", NULL, &slow_floppy, 1, 0},
  3580. {"unexpected_interrupts", NULL, &print_unex, 1, 0},
  3581. {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
  3582. {"L40SX", NULL, &print_unex, 0, 0}
  3583. EXTRA_FLOPPY_PARAMS
  3584. };
  3585. static int __init floppy_setup(char *str)
  3586. {
  3587. int i;
  3588. int param;
  3589. int ints[11];
  3590. str = get_options(str, ARRAY_SIZE(ints), ints);
  3591. if (str) {
  3592. for (i = 0; i < ARRAY_SIZE(config_params); i++) {
  3593. if (strcmp(str, config_params[i].name) == 0) {
  3594. if (ints[0])
  3595. param = ints[1];
  3596. else
  3597. param = config_params[i].def_param;
  3598. if (config_params[i].fn)
  3599. config_params[i].fn(ints, param,
  3600. config_params[i].
  3601. param2);
  3602. if (config_params[i].var) {
  3603. DPRINT("%s=%d\n", str, param);
  3604. *config_params[i].var = param;
  3605. }
  3606. return 1;
  3607. }
  3608. }
  3609. }
  3610. if (str) {
  3611. DPRINT("unknown floppy option [%s]\n", str);
  3612. DPRINT("allowed options are:");
  3613. for (i = 0; i < ARRAY_SIZE(config_params); i++)
  3614. pr_cont(" %s", config_params[i].name);
  3615. pr_cont("\n");
  3616. } else
  3617. DPRINT("botched floppy option\n");
  3618. DPRINT("Read Documentation/blockdev/floppy.txt\n");
  3619. return 0;
  3620. }
  3621. static int have_no_fdc = -ENODEV;
  3622. static ssize_t floppy_cmos_show(struct device *dev,
  3623. struct device_attribute *attr, char *buf)
  3624. {
  3625. struct platform_device *p = to_platform_device(dev);
  3626. int drive;
  3627. drive = p->id;
  3628. return sprintf(buf, "%X\n", UDP->cmos);
  3629. }
  3630. static DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
  3631. static void floppy_device_release(struct device *dev)
  3632. {
  3633. }
  3634. static int floppy_resume(struct device *dev)
  3635. {
  3636. int fdc;
  3637. for (fdc = 0; fdc < N_FDC; fdc++)
  3638. if (FDCS->address != -1)
  3639. user_reset_fdc(-1, FD_RESET_ALWAYS, false);
  3640. return 0;
  3641. }
  3642. static const struct dev_pm_ops floppy_pm_ops = {
  3643. .resume = floppy_resume,
  3644. .restore = floppy_resume,
  3645. };
  3646. static struct platform_driver floppy_driver = {
  3647. .driver = {
  3648. .name = "floppy",
  3649. .pm = &floppy_pm_ops,
  3650. },
  3651. };
  3652. static struct platform_device floppy_device[N_DRIVE];
  3653. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  3654. {
  3655. int drive = (*part & 3) | ((*part & 0x80) >> 5);
  3656. if (drive >= N_DRIVE ||
  3657. !(allowed_drive_mask & (1 << drive)) ||
  3658. fdc_state[FDC(drive)].version == FDC_NONE)
  3659. return NULL;
  3660. if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
  3661. return NULL;
  3662. *part = 0;
  3663. return get_disk(disks[drive]);
  3664. }
  3665. static int __init floppy_init(void)
  3666. {
  3667. int i, unit, drive;
  3668. int err, dr;
  3669. set_debugt();
  3670. interruptjiffies = resultjiffies = jiffies;
  3671. #if defined(CONFIG_PPC)
  3672. if (check_legacy_ioport(FDC1))
  3673. return -ENODEV;
  3674. #endif
  3675. raw_cmd = NULL;
  3676. for (dr = 0; dr < N_DRIVE; dr++) {
  3677. disks[dr] = alloc_disk(1);
  3678. if (!disks[dr]) {
  3679. err = -ENOMEM;
  3680. goto out_put_disk;
  3681. }
  3682. disks[dr]->queue = blk_init_queue(do_fd_request, &floppy_lock);
  3683. if (!disks[dr]->queue) {
  3684. err = -ENOMEM;
  3685. goto out_put_disk;
  3686. }
  3687. blk_queue_max_hw_sectors(disks[dr]->queue, 64);
  3688. disks[dr]->major = FLOPPY_MAJOR;
  3689. disks[dr]->first_minor = TOMINOR(dr);
  3690. disks[dr]->fops = &floppy_fops;
  3691. sprintf(disks[dr]->disk_name, "fd%d", dr);
  3692. init_timer(&motor_off_timer[dr]);
  3693. motor_off_timer[dr].data = dr;
  3694. motor_off_timer[dr].function = motor_off_callback;
  3695. }
  3696. err = register_blkdev(FLOPPY_MAJOR, "fd");
  3697. if (err)
  3698. goto out_put_disk;
  3699. err = platform_driver_register(&floppy_driver);
  3700. if (err)
  3701. goto out_unreg_blkdev;
  3702. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  3703. floppy_find, NULL, NULL);
  3704. for (i = 0; i < 256; i++)
  3705. if (ITYPE(i))
  3706. floppy_sizes[i] = floppy_type[ITYPE(i)].size;
  3707. else
  3708. floppy_sizes[i] = MAX_DISK_SIZE << 1;
  3709. reschedule_timeout(MAXTIMEOUT, "floppy init");
  3710. config_types();
  3711. for (i = 0; i < N_FDC; i++) {
  3712. fdc = i;
  3713. memset(FDCS, 0, sizeof(*FDCS));
  3714. FDCS->dtr = -1;
  3715. FDCS->dor = 0x4;
  3716. #if defined(__sparc__) || defined(__mc68000__)
  3717. /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
  3718. #ifdef __mc68000__
  3719. if (MACH_IS_SUN3X)
  3720. #endif
  3721. FDCS->version = FDC_82072A;
  3722. #endif
  3723. }
  3724. use_virtual_dma = can_use_virtual_dma & 1;
  3725. fdc_state[0].address = FDC1;
  3726. if (fdc_state[0].address == -1) {
  3727. del_timer_sync(&fd_timeout);
  3728. err = -ENODEV;
  3729. goto out_unreg_region;
  3730. }
  3731. #if N_FDC > 1
  3732. fdc_state[1].address = FDC2;
  3733. #endif
  3734. fdc = 0; /* reset fdc in case of unexpected interrupt */
  3735. err = floppy_grab_irq_and_dma();
  3736. if (err) {
  3737. del_timer_sync(&fd_timeout);
  3738. err = -EBUSY;
  3739. goto out_unreg_region;
  3740. }
  3741. /* initialise drive state */
  3742. for (drive = 0; drive < N_DRIVE; drive++) {
  3743. memset(UDRS, 0, sizeof(*UDRS));
  3744. memset(UDRWE, 0, sizeof(*UDRWE));
  3745. set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
  3746. set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  3747. set_bit(FD_VERIFY_BIT, &UDRS->flags);
  3748. UDRS->fd_device = -1;
  3749. floppy_track_buffer = NULL;
  3750. max_buffer_sectors = 0;
  3751. }
  3752. /*
  3753. * Small 10 msec delay to let through any interrupt that
  3754. * initialization might have triggered, to not
  3755. * confuse detection:
  3756. */
  3757. msleep(10);
  3758. for (i = 0; i < N_FDC; i++) {
  3759. fdc = i;
  3760. FDCS->driver_version = FD_DRIVER_VERSION;
  3761. for (unit = 0; unit < 4; unit++)
  3762. FDCS->track[unit] = 0;
  3763. if (FDCS->address == -1)
  3764. continue;
  3765. FDCS->rawcmd = 2;
  3766. if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
  3767. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3768. floppy_release_regions(fdc);
  3769. FDCS->address = -1;
  3770. FDCS->version = FDC_NONE;
  3771. continue;
  3772. }
  3773. /* Try to determine the floppy controller type */
  3774. FDCS->version = get_fdc_version();
  3775. if (FDCS->version == FDC_NONE) {
  3776. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3777. floppy_release_regions(fdc);
  3778. FDCS->address = -1;
  3779. continue;
  3780. }
  3781. if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
  3782. can_use_virtual_dma = 0;
  3783. have_no_fdc = 0;
  3784. /* Not all FDCs seem to be able to handle the version command
  3785. * properly, so force a reset for the standard FDC clones,
  3786. * to avoid interrupt garbage.
  3787. */
  3788. user_reset_fdc(-1, FD_RESET_ALWAYS, false);
  3789. }
  3790. fdc = 0;
  3791. del_timer_sync(&fd_timeout);
  3792. current_drive = 0;
  3793. initialized = true;
  3794. if (have_no_fdc) {
  3795. DPRINT("no floppy controllers found\n");
  3796. err = have_no_fdc;
  3797. goto out_flush_work;
  3798. }
  3799. for (drive = 0; drive < N_DRIVE; drive++) {
  3800. if (!(allowed_drive_mask & (1 << drive)))
  3801. continue;
  3802. if (fdc_state[FDC(drive)].version == FDC_NONE)
  3803. continue;
  3804. floppy_device[drive].name = floppy_device_name;
  3805. floppy_device[drive].id = drive;
  3806. floppy_device[drive].dev.release = floppy_device_release;
  3807. err = platform_device_register(&floppy_device[drive]);
  3808. if (err)
  3809. goto out_flush_work;
  3810. err = device_create_file(&floppy_device[drive].dev,
  3811. &dev_attr_cmos);
  3812. if (err)
  3813. goto out_unreg_platform_dev;
  3814. /* to be cleaned up... */
  3815. disks[drive]->private_data = (void *)(long)drive;
  3816. disks[drive]->flags |= GENHD_FL_REMOVABLE;
  3817. disks[drive]->driverfs_dev = &floppy_device[drive].dev;
  3818. add_disk(disks[drive]);
  3819. }
  3820. return 0;
  3821. out_unreg_platform_dev:
  3822. platform_device_unregister(&floppy_device[drive]);
  3823. out_flush_work:
  3824. flush_work_sync(&floppy_work);
  3825. if (atomic_read(&usage_count))
  3826. floppy_release_irq_and_dma();
  3827. out_unreg_region:
  3828. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  3829. platform_driver_unregister(&floppy_driver);
  3830. out_unreg_blkdev:
  3831. unregister_blkdev(FLOPPY_MAJOR, "fd");
  3832. out_put_disk:
  3833. while (dr--) {
  3834. del_timer_sync(&motor_off_timer[dr]);
  3835. if (disks[dr]->queue)
  3836. blk_cleanup_queue(disks[dr]->queue);
  3837. put_disk(disks[dr]);
  3838. }
  3839. return err;
  3840. }
  3841. static const struct io_region {
  3842. int offset;
  3843. int size;
  3844. } io_regions[] = {
  3845. { 2, 1 },
  3846. /* address + 3 is sometimes reserved by pnp bios for motherboard */
  3847. { 4, 2 },
  3848. /* address + 6 is reserved, and may be taken by IDE.
  3849. * Unfortunately, Adaptec doesn't know this :-(, */
  3850. { 7, 1 },
  3851. };
  3852. static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
  3853. {
  3854. while (p != io_regions) {
  3855. p--;
  3856. release_region(FDCS->address + p->offset, p->size);
  3857. }
  3858. }
  3859. #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
  3860. static int floppy_request_regions(int fdc)
  3861. {
  3862. const struct io_region *p;
  3863. for (p = io_regions; p < ARRAY_END(io_regions); p++) {
  3864. if (!request_region(FDCS->address + p->offset,
  3865. p->size, "floppy")) {
  3866. DPRINT("Floppy io-port 0x%04lx in use\n",
  3867. FDCS->address + p->offset);
  3868. floppy_release_allocated_regions(fdc, p);
  3869. return -EBUSY;
  3870. }
  3871. }
  3872. return 0;
  3873. }
  3874. static void floppy_release_regions(int fdc)
  3875. {
  3876. floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
  3877. }
  3878. static int floppy_grab_irq_and_dma(void)
  3879. {
  3880. if (atomic_inc_return(&usage_count) > 1)
  3881. return 0;
  3882. /*
  3883. * We might have scheduled a free_irq(), wait it to
  3884. * drain first:
  3885. */
  3886. flush_work_sync(&floppy_work);
  3887. if (fd_request_irq()) {
  3888. DPRINT("Unable to grab IRQ%d for the floppy driver\n",
  3889. FLOPPY_IRQ);
  3890. atomic_dec(&usage_count);
  3891. return -1;
  3892. }
  3893. if (fd_request_dma()) {
  3894. DPRINT("Unable to grab DMA%d for the floppy driver\n",
  3895. FLOPPY_DMA);
  3896. if (can_use_virtual_dma & 2)
  3897. use_virtual_dma = can_use_virtual_dma = 1;
  3898. if (!(can_use_virtual_dma & 1)) {
  3899. fd_free_irq();
  3900. atomic_dec(&usage_count);
  3901. return -1;
  3902. }
  3903. }
  3904. for (fdc = 0; fdc < N_FDC; fdc++) {
  3905. if (FDCS->address != -1) {
  3906. if (floppy_request_regions(fdc))
  3907. goto cleanup;
  3908. }
  3909. }
  3910. for (fdc = 0; fdc < N_FDC; fdc++) {
  3911. if (FDCS->address != -1) {
  3912. reset_fdc_info(1);
  3913. fd_outb(FDCS->dor, FD_DOR);
  3914. }
  3915. }
  3916. fdc = 0;
  3917. set_dor(0, ~0, 8); /* avoid immediate interrupt */
  3918. for (fdc = 0; fdc < N_FDC; fdc++)
  3919. if (FDCS->address != -1)
  3920. fd_outb(FDCS->dor, FD_DOR);
  3921. /*
  3922. * The driver will try and free resources and relies on us
  3923. * to know if they were allocated or not.
  3924. */
  3925. fdc = 0;
  3926. irqdma_allocated = 1;
  3927. return 0;
  3928. cleanup:
  3929. fd_free_irq();
  3930. fd_free_dma();
  3931. while (--fdc >= 0)
  3932. floppy_release_regions(fdc);
  3933. atomic_dec(&usage_count);
  3934. return -1;
  3935. }
  3936. static void floppy_release_irq_and_dma(void)
  3937. {
  3938. int old_fdc;
  3939. #ifndef __sparc__
  3940. int drive;
  3941. #endif
  3942. long tmpsize;
  3943. unsigned long tmpaddr;
  3944. if (!atomic_dec_and_test(&usage_count))
  3945. return;
  3946. if (irqdma_allocated) {
  3947. fd_disable_dma();
  3948. fd_free_dma();
  3949. fd_free_irq();
  3950. irqdma_allocated = 0;
  3951. }
  3952. set_dor(0, ~0, 8);
  3953. #if N_FDC > 1
  3954. set_dor(1, ~8, 0);
  3955. #endif
  3956. floppy_enable_hlt();
  3957. if (floppy_track_buffer && max_buffer_sectors) {
  3958. tmpsize = max_buffer_sectors * 1024;
  3959. tmpaddr = (unsigned long)floppy_track_buffer;
  3960. floppy_track_buffer = NULL;
  3961. max_buffer_sectors = 0;
  3962. buffer_min = buffer_max = -1;
  3963. fd_dma_mem_free(tmpaddr, tmpsize);
  3964. }
  3965. #ifndef __sparc__
  3966. for (drive = 0; drive < N_FDC * 4; drive++)
  3967. if (timer_pending(motor_off_timer + drive))
  3968. pr_info("motor off timer %d still active\n", drive);
  3969. #endif
  3970. if (timer_pending(&fd_timeout))
  3971. pr_info("floppy timer still active:%s\n", timeout_message);
  3972. if (timer_pending(&fd_timer))
  3973. pr_info("auxiliary floppy timer still active\n");
  3974. if (work_pending(&floppy_work))
  3975. pr_info("work still pending\n");
  3976. old_fdc = fdc;
  3977. for (fdc = 0; fdc < N_FDC; fdc++)
  3978. if (FDCS->address != -1)
  3979. floppy_release_regions(fdc);
  3980. fdc = old_fdc;
  3981. }
  3982. #ifdef MODULE
  3983. static char *floppy;
  3984. static void __init parse_floppy_cfg_string(char *cfg)
  3985. {
  3986. char *ptr;
  3987. while (*cfg) {
  3988. ptr = cfg;
  3989. while (*cfg && *cfg != ' ' && *cfg != '\t')
  3990. cfg++;
  3991. if (*cfg) {
  3992. *cfg = '\0';
  3993. cfg++;
  3994. }
  3995. if (*ptr)
  3996. floppy_setup(ptr);
  3997. }
  3998. }
  3999. static int __init floppy_module_init(void)
  4000. {
  4001. if (floppy)
  4002. parse_floppy_cfg_string(floppy);
  4003. return floppy_init();
  4004. }
  4005. module_init(floppy_module_init);
  4006. static void __exit floppy_module_exit(void)
  4007. {
  4008. int drive;
  4009. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  4010. unregister_blkdev(FLOPPY_MAJOR, "fd");
  4011. platform_driver_unregister(&floppy_driver);
  4012. for (drive = 0; drive < N_DRIVE; drive++) {
  4013. del_timer_sync(&motor_off_timer[drive]);
  4014. if ((allowed_drive_mask & (1 << drive)) &&
  4015. fdc_state[FDC(drive)].version != FDC_NONE) {
  4016. del_gendisk(disks[drive]);
  4017. device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
  4018. platform_device_unregister(&floppy_device[drive]);
  4019. }
  4020. blk_cleanup_queue(disks[drive]->queue);
  4021. put_disk(disks[drive]);
  4022. }
  4023. del_timer_sync(&fd_timeout);
  4024. del_timer_sync(&fd_timer);
  4025. if (atomic_read(&usage_count))
  4026. floppy_release_irq_and_dma();
  4027. /* eject disk, if any */
  4028. fd_eject(0);
  4029. }
  4030. module_exit(floppy_module_exit);
  4031. module_param(floppy, charp, 0);
  4032. module_param(FLOPPY_IRQ, int, 0);
  4033. module_param(FLOPPY_DMA, int, 0);
  4034. MODULE_AUTHOR("Alain L. Knaff");
  4035. MODULE_SUPPORTED_DEVICE("fd");
  4036. MODULE_LICENSE("GPL");
  4037. /* This doesn't actually get used other than for module information */
  4038. static const struct pnp_device_id floppy_pnpids[] = {
  4039. {"PNP0700", 0},
  4040. {}
  4041. };
  4042. MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
  4043. #else
  4044. __setup("floppy=", floppy_setup);
  4045. module_init(floppy_init)
  4046. #endif
  4047. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);