floppy.c 134 KB

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