floppy.c 134 KB

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