floppy.c 134 KB

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