ide-tape.c 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949
  1. /*
  2. * linux/drivers/ide/ide-tape.c Version 1.19 Nov, 2003
  3. *
  4. * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
  5. *
  6. * $Header$
  7. *
  8. * This driver was constructed as a student project in the software laboratory
  9. * of the faculty of electrical engineering in the Technion - Israel's
  10. * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
  11. *
  12. * It is hereby placed under the terms of the GNU general public license.
  13. * (See linux/COPYING).
  14. */
  15. /*
  16. * IDE ATAPI streaming tape driver.
  17. *
  18. * This driver is a part of the Linux ide driver and works in co-operation
  19. * with linux/drivers/block/ide.c.
  20. *
  21. * The driver, in co-operation with ide.c, basically traverses the
  22. * request-list for the block device interface. The character device
  23. * interface, on the other hand, creates new requests, adds them
  24. * to the request-list of the block device, and waits for their completion.
  25. *
  26. * Pipelined operation mode is now supported on both reads and writes.
  27. *
  28. * The block device major and minor numbers are determined from the
  29. * tape's relative position in the ide interfaces, as explained in ide.c.
  30. *
  31. * The character device interface consists of the following devices:
  32. *
  33. * ht0 major 37, minor 0 first IDE tape, rewind on close.
  34. * ht1 major 37, minor 1 second IDE tape, rewind on close.
  35. * ...
  36. * nht0 major 37, minor 128 first IDE tape, no rewind on close.
  37. * nht1 major 37, minor 129 second IDE tape, no rewind on close.
  38. * ...
  39. *
  40. * Run linux/scripts/MAKEDEV.ide to create the above entries.
  41. *
  42. * The general magnetic tape commands compatible interface, as defined by
  43. * include/linux/mtio.h, is accessible through the character device.
  44. *
  45. * General ide driver configuration options, such as the interrupt-unmask
  46. * flag, can be configured by issuing an ioctl to the block device interface,
  47. * as any other ide device.
  48. *
  49. * Our own ide-tape ioctl's can be issued to either the block device or
  50. * the character device interface.
  51. *
  52. * Maximal throughput with minimal bus load will usually be achieved in the
  53. * following scenario:
  54. *
  55. * 1. ide-tape is operating in the pipelined operation mode.
  56. * 2. No buffering is performed by the user backup program.
  57. *
  58. * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
  59. *
  60. * Ver 0.1 Nov 1 95 Pre-working code :-)
  61. * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
  62. * was successful ! (Using tar cvf ... on the block
  63. * device interface).
  64. * A longer backup resulted in major swapping, bad
  65. * overall Linux performance and eventually failed as
  66. * we received non serial read-ahead requests from the
  67. * buffer cache.
  68. * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
  69. * character device interface. Linux's responsiveness
  70. * and performance doesn't seem to be much affected
  71. * from the background backup procedure.
  72. * Some general mtio.h magnetic tape operations are
  73. * now supported by our character device. As a result,
  74. * popular tape utilities are starting to work with
  75. * ide tapes :-)
  76. * The following configurations were tested:
  77. * 1. An IDE ATAPI TAPE shares the same interface
  78. * and irq with an IDE ATAPI CDROM.
  79. * 2. An IDE ATAPI TAPE shares the same interface
  80. * and irq with a normal IDE disk.
  81. * Both configurations seemed to work just fine !
  82. * However, to be on the safe side, it is meanwhile
  83. * recommended to give the IDE TAPE its own interface
  84. * and irq.
  85. * The one thing which needs to be done here is to
  86. * add a "request postpone" feature to ide.c,
  87. * so that we won't have to wait for the tape to finish
  88. * performing a long media access (DSC) request (such
  89. * as a rewind) before we can access the other device
  90. * on the same interface. This effect doesn't disturb
  91. * normal operation most of the time because read/write
  92. * requests are relatively fast, and once we are
  93. * performing one tape r/w request, a lot of requests
  94. * from the other device can be queued and ide.c will
  95. * service all of them after this single tape request.
  96. * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
  97. * On each read / write request, we now ask the drive
  98. * if we can transfer a constant number of bytes
  99. * (a parameter of the drive) only to its buffers,
  100. * without causing actual media access. If we can't,
  101. * we just wait until we can by polling the DSC bit.
  102. * This ensures that while we are not transferring
  103. * more bytes than the constant referred to above, the
  104. * interrupt latency will not become too high and
  105. * we won't cause an interrupt timeout, as happened
  106. * occasionally in the previous version.
  107. * While polling for DSC, the current request is
  108. * postponed and ide.c is free to handle requests from
  109. * the other device. This is handled transparently to
  110. * ide.c. The hwgroup locking method which was used
  111. * in the previous version was removed.
  112. * Use of new general features which are provided by
  113. * ide.c for use with atapi devices.
  114. * (Programming done by Mark Lord)
  115. * Few potential bug fixes (Again, suggested by Mark)
  116. * Single character device data transfers are now
  117. * not limited in size, as they were before.
  118. * We are asking the tape about its recommended
  119. * transfer unit and send a larger data transfer
  120. * as several transfers of the above size.
  121. * For best results, use an integral number of this
  122. * basic unit (which is shown during driver
  123. * initialization). I will soon add an ioctl to get
  124. * this important parameter.
  125. * Our data transfer buffer is allocated on startup,
  126. * rather than before each data transfer. This should
  127. * ensure that we will indeed have a data buffer.
  128. * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
  129. * shared an interface with another device.
  130. * (poll_for_dsc was a complete mess).
  131. * Removed some old (non-active) code which had
  132. * to do with supporting buffer cache originated
  133. * requests.
  134. * The block device interface can now be opened, so
  135. * that general ide driver features like the unmask
  136. * interrupts flag can be selected with an ioctl.
  137. * This is the only use of the block device interface.
  138. * New fast pipelined operation mode (currently only on
  139. * writes). When using the pipelined mode, the
  140. * throughput can potentially reach the maximum
  141. * tape supported throughput, regardless of the
  142. * user backup program. On my tape drive, it sometimes
  143. * boosted performance by a factor of 2. Pipelined
  144. * mode is enabled by default, but since it has a few
  145. * downfalls as well, you may want to disable it.
  146. * A short explanation of the pipelined operation mode
  147. * is available below.
  148. * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
  149. * Added pipeline read mode. As a result, restores
  150. * are now as fast as backups.
  151. * Optimized shared interface behavior. The new behavior
  152. * typically results in better IDE bus efficiency and
  153. * higher tape throughput.
  154. * Pre-calculation of the expected read/write request
  155. * service time, based on the tape's parameters. In
  156. * the pipelined operation mode, this allows us to
  157. * adjust our polling frequency to a much lower value,
  158. * and thus to dramatically reduce our load on Linux,
  159. * without any decrease in performance.
  160. * Implemented additional mtio.h operations.
  161. * The recommended user block size is returned by
  162. * the MTIOCGET ioctl.
  163. * Additional minor changes.
  164. * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
  165. * use of some block sizes during a restore procedure.
  166. * The character device interface will now present a
  167. * continuous view of the media - any mix of block sizes
  168. * during a backup/restore procedure is supported. The
  169. * driver will buffer the requests internally and
  170. * convert them to the tape's recommended transfer
  171. * unit, making performance almost independent of the
  172. * chosen user block size.
  173. * Some improvements in error recovery.
  174. * By cooperating with ide-dma.c, bus mastering DMA can
  175. * now sometimes be used with IDE tape drives as well.
  176. * Bus mastering DMA has the potential to dramatically
  177. * reduce the CPU's overhead when accessing the device,
  178. * and can be enabled by using hdparm -d1 on the tape's
  179. * block device interface. For more info, read the
  180. * comments in ide-dma.c.
  181. * Ver 1.4 Mar 13 96 Fixed serialize support.
  182. * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
  183. * Fixed pipelined read mode inefficiency.
  184. * Fixed nasty null dereferencing bug.
  185. * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
  186. * Fixed end of media bug.
  187. * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
  188. * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
  189. * interactive response and high system throughput.
  190. * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
  191. * than requiring an explicit FSF command.
  192. * Abort pending requests at end of media.
  193. * MTTELL was sometimes returning incorrect results.
  194. * Return the real block size in the MTIOCGET ioctl.
  195. * Some error recovery bug fixes.
  196. * Ver 1.10 Nov 5 96 Major reorganization.
  197. * Reduced CPU overhead a bit by eliminating internal
  198. * bounce buffers.
  199. * Added module support.
  200. * Added multiple tape drives support.
  201. * Added partition support.
  202. * Rewrote DSC handling.
  203. * Some portability fixes.
  204. * Removed ide-tape.h.
  205. * Additional minor changes.
  206. * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
  207. * Use ide_stall_queue() for DSC overlap.
  208. * Use the maximum speed rather than the current speed
  209. * to compute the request service time.
  210. * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
  211. * corruption, which could occur if the total number
  212. * of bytes written to the tape was not an integral
  213. * number of tape blocks.
  214. * Add support for INTERRUPT DRQ devices.
  215. * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
  216. * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
  217. * Replace cli()/sti() with hwgroup spinlocks.
  218. * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
  219. * spinlock with private per-tape spinlock.
  220. * Ver 1.16 Sep 1 99 Add OnStream tape support.
  221. * Abort read pipeline on EOD.
  222. * Wait for the tape to become ready in case it returns
  223. * "in the process of becoming ready" on open().
  224. * Fix zero padding of the last written block in
  225. * case the tape block size is larger than PAGE_SIZE.
  226. * Decrease the default disconnection time to tn.
  227. * Ver 1.16e Oct 3 99 Minor fixes.
  228. * Ver 1.16e1 Oct 13 99 Patches by Arnold Niessen,
  229. * niessen@iae.nl / arnold.niessen@philips.com
  230. * GO-1) Undefined code in idetape_read_position
  231. * according to Gadi's email
  232. * AJN-1) Minor fix asc == 11 should be asc == 0x11
  233. * in idetape_issue_packet_command (did effect
  234. * debugging output only)
  235. * AJN-2) Added more debugging output, and
  236. * added ide-tape: where missing. I would also
  237. * like to add tape->name where possible
  238. * AJN-3) Added different debug_level's
  239. * via /proc/ide/hdc/settings
  240. * "debug_level" determines amount of debugging output;
  241. * can be changed using /proc/ide/hdx/settings
  242. * 0 : almost no debugging output
  243. * 1 : 0+output errors only
  244. * 2 : 1+output all sensekey/asc
  245. * 3 : 2+follow all chrdev related procedures
  246. * 4 : 3+follow all procedures
  247. * 5 : 4+include pc_stack rq_stack info
  248. * 6 : 5+USE_COUNT updates
  249. * AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
  250. * from 5 to 10 minutes
  251. * AJN-5) Changed maximum number of blocks to skip when
  252. * reading tapes with multiple consecutive write
  253. * errors from 100 to 1000 in idetape_get_logical_blk
  254. * Proposed changes to code:
  255. * 1) output "logical_blk_num" via /proc
  256. * 2) output "current_operation" via /proc
  257. * 3) Either solve or document the fact that `mt rewind' is
  258. * required after reading from /dev/nhtx to be
  259. * able to rmmod the idetape module;
  260. * Also, sometimes an application finishes but the
  261. * device remains `busy' for some time. Same cause ?
  262. * Proposed changes to release-notes:
  263. * 4) write a simple `quickstart' section in the
  264. * release notes; I volunteer if you don't want to
  265. * 5) include a pointer to video4linux in the doc
  266. * to stimulate video applications
  267. * 6) release notes lines 331 and 362: explain what happens
  268. * if the application data rate is higher than 1100 KB/s;
  269. * similar approach to lower-than-500 kB/s ?
  270. * 7) 6.6 Comparison; wouldn't it be better to allow different
  271. * strategies for read and write ?
  272. * Wouldn't it be better to control the tape buffer
  273. * contents instead of the bandwidth ?
  274. * 8) line 536: replace will by would (if I understand
  275. * this section correctly, a hypothetical and unwanted situation
  276. * is being described)
  277. * Ver 1.16f Dec 15 99 Change place of the secondary OnStream header frames.
  278. * Ver 1.17 Nov 2000 / Jan 2001 Marcel Mol, marcel@mesa.nl
  279. * - Add idetape_onstream_mode_sense_tape_parameter_page
  280. * function to get tape capacity in frames: tape->capacity.
  281. * - Add support for DI-50 drives( or any DI- drive).
  282. * - 'workaround' for read error/blank block around block 3000.
  283. * - Implement Early warning for end of media for Onstream.
  284. * - Cosmetic code changes for readability.
  285. * - Idetape_position_tape should not use SKIP bit during
  286. * Onstream read recovery.
  287. * - Add capacity, logical_blk_num and first/last_frame_position
  288. * to /proc/ide/hd?/settings.
  289. * - Module use count was gone in the Linux 2.4 driver.
  290. * Ver 1.17a Apr 2001 Willem Riede osst@riede.org
  291. * - Get drive's actual block size from mode sense block descriptor
  292. * - Limit size of pipeline
  293. * Ver 1.17b Oct 2002 Alan Stern <stern@rowland.harvard.edu>
  294. * Changed IDETAPE_MIN_PIPELINE_STAGES to 1 and actually used
  295. * it in the code!
  296. * Actually removed aborted stages in idetape_abort_pipeline
  297. * instead of just changing the command code.
  298. * Made the transfer byte count for Request Sense equal to the
  299. * actual length of the data transfer.
  300. * Changed handling of partial data transfers: they do not
  301. * cause DMA errors.
  302. * Moved initiation of DMA transfers to the correct place.
  303. * Removed reference to unallocated memory.
  304. * Made __idetape_discard_read_pipeline return the number of
  305. * sectors skipped, not the number of stages.
  306. * Replaced errant kfree() calls with __idetape_kfree_stage().
  307. * Fixed off-by-one error in testing the pipeline length.
  308. * Fixed handling of filemarks in the read pipeline.
  309. * Small code optimization for MTBSF and MTBSFM ioctls.
  310. * Don't try to unlock the door during device close if is
  311. * already unlocked!
  312. * Cosmetic fixes to miscellaneous debugging output messages.
  313. * Set the minimum /proc/ide/hd?/settings values for "pipeline",
  314. * "pipeline_min", and "pipeline_max" to 1.
  315. *
  316. * Here are some words from the first releases of hd.c, which are quoted
  317. * in ide.c and apply here as well:
  318. *
  319. * | Special care is recommended. Have Fun!
  320. *
  321. */
  322. /*
  323. * An overview of the pipelined operation mode.
  324. *
  325. * In the pipelined write mode, we will usually just add requests to our
  326. * pipeline and return immediately, before we even start to service them. The
  327. * user program will then have enough time to prepare the next request while
  328. * we are still busy servicing previous requests. In the pipelined read mode,
  329. * the situation is similar - we add read-ahead requests into the pipeline,
  330. * before the user even requested them.
  331. *
  332. * The pipeline can be viewed as a "safety net" which will be activated when
  333. * the system load is high and prevents the user backup program from keeping up
  334. * with the current tape speed. At this point, the pipeline will get
  335. * shorter and shorter but the tape will still be streaming at the same speed.
  336. * Assuming we have enough pipeline stages, the system load will hopefully
  337. * decrease before the pipeline is completely empty, and the backup program
  338. * will be able to "catch up" and refill the pipeline again.
  339. *
  340. * When using the pipelined mode, it would be best to disable any type of
  341. * buffering done by the user program, as ide-tape already provides all the
  342. * benefits in the kernel, where it can be done in a more efficient way.
  343. * As we will usually not block the user program on a request, the most
  344. * efficient user code will then be a simple read-write-read-... cycle.
  345. * Any additional logic will usually just slow down the backup process.
  346. *
  347. * Using the pipelined mode, I get a constant over 400 KBps throughput,
  348. * which seems to be the maximum throughput supported by my tape.
  349. *
  350. * However, there are some downfalls:
  351. *
  352. * 1. We use memory (for data buffers) in proportional to the number
  353. * of pipeline stages (each stage is about 26 KB with my tape).
  354. * 2. In the pipelined write mode, we cheat and postpone error codes
  355. * to the user task. In read mode, the actual tape position
  356. * will be a bit further than the last requested block.
  357. *
  358. * Concerning (1):
  359. *
  360. * 1. We allocate stages dynamically only when we need them. When
  361. * we don't need them, we don't consume additional memory. In
  362. * case we can't allocate stages, we just manage without them
  363. * (at the expense of decreased throughput) so when Linux is
  364. * tight in memory, we will not pose additional difficulties.
  365. *
  366. * 2. The maximum number of stages (which is, in fact, the maximum
  367. * amount of memory) which we allocate is limited by the compile
  368. * time parameter IDETAPE_MAX_PIPELINE_STAGES.
  369. *
  370. * 3. The maximum number of stages is a controlled parameter - We
  371. * don't start from the user defined maximum number of stages
  372. * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
  373. * will not even allocate this amount of stages if the user
  374. * program can't handle the speed). We then implement a feedback
  375. * loop which checks if the pipeline is empty, and if it is, we
  376. * increase the maximum number of stages as necessary until we
  377. * reach the optimum value which just manages to keep the tape
  378. * busy with minimum allocated memory or until we reach
  379. * IDETAPE_MAX_PIPELINE_STAGES.
  380. *
  381. * Concerning (2):
  382. *
  383. * In pipelined write mode, ide-tape can not return accurate error codes
  384. * to the user program since we usually just add the request to the
  385. * pipeline without waiting for it to be serviced. In case an error
  386. * occurs, I will report it on the next user request.
  387. *
  388. * In the pipelined read mode, subsequent read requests or forward
  389. * filemark spacing will perform correctly, as we preserve all blocks
  390. * and filemarks which we encountered during our excess read-ahead.
  391. *
  392. * For accurate tape positioning and error reporting, disabling
  393. * pipelined mode might be the best option.
  394. *
  395. * You can enable/disable/tune the pipelined operation mode by adjusting
  396. * the compile time parameters below.
  397. */
  398. /*
  399. * Possible improvements.
  400. *
  401. * 1. Support for the ATAPI overlap protocol.
  402. *
  403. * In order to maximize bus throughput, we currently use the DSC
  404. * overlap method which enables ide.c to service requests from the
  405. * other device while the tape is busy executing a command. The
  406. * DSC overlap method involves polling the tape's status register
  407. * for the DSC bit, and servicing the other device while the tape
  408. * isn't ready.
  409. *
  410. * In the current QIC development standard (December 1995),
  411. * it is recommended that new tape drives will *in addition*
  412. * implement the ATAPI overlap protocol, which is used for the
  413. * same purpose - efficient use of the IDE bus, but is interrupt
  414. * driven and thus has much less CPU overhead.
  415. *
  416. * ATAPI overlap is likely to be supported in most new ATAPI
  417. * devices, including new ATAPI cdroms, and thus provides us
  418. * a method by which we can achieve higher throughput when
  419. * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
  420. */
  421. #define IDETAPE_VERSION "1.19"
  422. #include <linux/config.h>
  423. #include <linux/module.h>
  424. #include <linux/types.h>
  425. #include <linux/string.h>
  426. #include <linux/kernel.h>
  427. #include <linux/delay.h>
  428. #include <linux/timer.h>
  429. #include <linux/mm.h>
  430. #include <linux/interrupt.h>
  431. #include <linux/major.h>
  432. #include <linux/devfs_fs_kernel.h>
  433. #include <linux/errno.h>
  434. #include <linux/genhd.h>
  435. #include <linux/slab.h>
  436. #include <linux/pci.h>
  437. #include <linux/ide.h>
  438. #include <linux/smp_lock.h>
  439. #include <linux/completion.h>
  440. #include <linux/bitops.h>
  441. #include <asm/byteorder.h>
  442. #include <asm/irq.h>
  443. #include <asm/uaccess.h>
  444. #include <asm/io.h>
  445. #include <asm/unaligned.h>
  446. /*
  447. * partition
  448. */
  449. typedef struct os_partition_s {
  450. __u8 partition_num;
  451. __u8 par_desc_ver;
  452. __u16 wrt_pass_cntr;
  453. __u32 first_frame_addr;
  454. __u32 last_frame_addr;
  455. __u32 eod_frame_addr;
  456. } os_partition_t;
  457. /*
  458. * DAT entry
  459. */
  460. typedef struct os_dat_entry_s {
  461. __u32 blk_sz;
  462. __u16 blk_cnt;
  463. __u8 flags;
  464. __u8 reserved;
  465. } os_dat_entry_t;
  466. /*
  467. * DAT
  468. */
  469. #define OS_DAT_FLAGS_DATA (0xc)
  470. #define OS_DAT_FLAGS_MARK (0x1)
  471. typedef struct os_dat_s {
  472. __u8 dat_sz;
  473. __u8 reserved1;
  474. __u8 entry_cnt;
  475. __u8 reserved3;
  476. os_dat_entry_t dat_list[16];
  477. } os_dat_t;
  478. #include <linux/mtio.h>
  479. /**************************** Tunable parameters *****************************/
  480. /*
  481. * Pipelined mode parameters.
  482. *
  483. * We try to use the minimum number of stages which is enough to
  484. * keep the tape constantly streaming. To accomplish that, we implement
  485. * a feedback loop around the maximum number of stages:
  486. *
  487. * We start from MIN maximum stages (we will not even use MIN stages
  488. * if we don't need them), increment it by RATE*(MAX-MIN)
  489. * whenever we sense that the pipeline is empty, until we reach
  490. * the optimum value or until we reach MAX.
  491. *
  492. * Setting the following parameter to 0 is illegal: the pipelined mode
  493. * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
  494. */
  495. #define IDETAPE_MIN_PIPELINE_STAGES 1
  496. #define IDETAPE_MAX_PIPELINE_STAGES 400
  497. #define IDETAPE_INCREASE_STAGES_RATE 20
  498. /*
  499. * The following are used to debug the driver:
  500. *
  501. * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
  502. * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
  503. * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
  504. * some places.
  505. *
  506. * Setting them to 0 will restore normal operation mode:
  507. *
  508. * 1. Disable logging normal successful operations.
  509. * 2. Disable self-sanity checks.
  510. * 3. Errors will still be logged, of course.
  511. *
  512. * All the #if DEBUG code will be removed some day, when the driver
  513. * is verified to be stable enough. This will make it much more
  514. * esthetic.
  515. */
  516. #define IDETAPE_DEBUG_INFO 0
  517. #define IDETAPE_DEBUG_LOG 0
  518. #define IDETAPE_DEBUG_BUGS 1
  519. /*
  520. * After each failed packet command we issue a request sense command
  521. * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
  522. *
  523. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  524. */
  525. #define IDETAPE_MAX_PC_RETRIES 3
  526. /*
  527. * With each packet command, we allocate a buffer of
  528. * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
  529. * commands (Not for READ/WRITE commands).
  530. */
  531. #define IDETAPE_PC_BUFFER_SIZE 256
  532. /*
  533. * In various places in the driver, we need to allocate storage
  534. * for packet commands and requests, which will remain valid while
  535. * we leave the driver to wait for an interrupt or a timeout event.
  536. */
  537. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  538. /*
  539. * Some drives (for example, Seagate STT3401A Travan) require a very long
  540. * timeout, because they don't return an interrupt or clear their busy bit
  541. * until after the command completes (even retension commands).
  542. */
  543. #define IDETAPE_WAIT_CMD (900*HZ)
  544. /*
  545. * The following parameter is used to select the point in the internal
  546. * tape fifo in which we will start to refill the buffer. Decreasing
  547. * the following parameter will improve the system's latency and
  548. * interactive response, while using a high value might improve sytem
  549. * throughput.
  550. */
  551. #define IDETAPE_FIFO_THRESHOLD 2
  552. /*
  553. * DSC polling parameters.
  554. *
  555. * Polling for DSC (a single bit in the status register) is a very
  556. * important function in ide-tape. There are two cases in which we
  557. * poll for DSC:
  558. *
  559. * 1. Before a read/write packet command, to ensure that we
  560. * can transfer data from/to the tape's data buffers, without
  561. * causing an actual media access. In case the tape is not
  562. * ready yet, we take out our request from the device
  563. * request queue, so that ide.c will service requests from
  564. * the other device on the same interface meanwhile.
  565. *
  566. * 2. After the successful initialization of a "media access
  567. * packet command", which is a command which can take a long
  568. * time to complete (it can be several seconds or even an hour).
  569. *
  570. * Again, we postpone our request in the middle to free the bus
  571. * for the other device. The polling frequency here should be
  572. * lower than the read/write frequency since those media access
  573. * commands are slow. We start from a "fast" frequency -
  574. * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
  575. * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
  576. * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
  577. *
  578. * We also set a timeout for the timer, in case something goes wrong.
  579. * The timeout should be longer then the maximum execution time of a
  580. * tape operation.
  581. */
  582. /*
  583. * DSC timings.
  584. */
  585. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  586. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  587. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  588. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  589. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  590. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  591. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  592. /*************************** End of tunable parameters ***********************/
  593. /*
  594. * Debugging/Performance analysis
  595. *
  596. * I/O trace support
  597. */
  598. #define USE_IOTRACE 0
  599. #if USE_IOTRACE
  600. #include <linux/io_trace.h>
  601. #define IO_IDETAPE_FIFO 500
  602. #endif
  603. /*
  604. * Read/Write error simulation
  605. */
  606. #define SIMULATE_ERRORS 0
  607. /*
  608. * For general magnetic tape device compatibility.
  609. */
  610. typedef enum {
  611. idetape_direction_none,
  612. idetape_direction_read,
  613. idetape_direction_write
  614. } idetape_chrdev_direction_t;
  615. struct idetape_bh {
  616. unsigned short b_size;
  617. atomic_t b_count;
  618. struct idetape_bh *b_reqnext;
  619. char *b_data;
  620. };
  621. /*
  622. * Our view of a packet command.
  623. */
  624. typedef struct idetape_packet_command_s {
  625. u8 c[12]; /* Actual packet bytes */
  626. int retries; /* On each retry, we increment retries */
  627. int error; /* Error code */
  628. int request_transfer; /* Bytes to transfer */
  629. int actually_transferred; /* Bytes actually transferred */
  630. int buffer_size; /* Size of our data buffer */
  631. struct idetape_bh *bh;
  632. char *b_data;
  633. int b_count;
  634. u8 *buffer; /* Data buffer */
  635. u8 *current_position; /* Pointer into the above buffer */
  636. ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  637. u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
  638. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  639. } idetape_pc_t;
  640. /*
  641. * Packet command flag bits.
  642. */
  643. /* Set when an error is considered normal - We won't retry */
  644. #define PC_ABORT 0
  645. /* 1 When polling for DSC on a media access command */
  646. #define PC_WAIT_FOR_DSC 1
  647. /* 1 when we prefer to use DMA if possible */
  648. #define PC_DMA_RECOMMENDED 2
  649. /* 1 while DMA in progress */
  650. #define PC_DMA_IN_PROGRESS 3
  651. /* 1 when encountered problem during DMA */
  652. #define PC_DMA_ERROR 4
  653. /* Data direction */
  654. #define PC_WRITING 5
  655. /*
  656. * Capabilities and Mechanical Status Page
  657. */
  658. typedef struct {
  659. unsigned page_code :6; /* Page code - Should be 0x2a */
  660. __u8 reserved0_6 :1;
  661. __u8 ps :1; /* parameters saveable */
  662. __u8 page_length; /* Page Length - Should be 0x12 */
  663. __u8 reserved2, reserved3;
  664. unsigned ro :1; /* Read Only Mode */
  665. unsigned reserved4_1234 :4;
  666. unsigned sprev :1; /* Supports SPACE in the reverse direction */
  667. unsigned reserved4_67 :2;
  668. unsigned reserved5_012 :3;
  669. unsigned efmt :1; /* Supports ERASE command initiated formatting */
  670. unsigned reserved5_4 :1;
  671. unsigned qfa :1; /* Supports the QFA two partition formats */
  672. unsigned reserved5_67 :2;
  673. unsigned lock :1; /* Supports locking the volume */
  674. unsigned locked :1; /* The volume is locked */
  675. unsigned prevent :1; /* The device defaults in the prevent state after power up */
  676. unsigned eject :1; /* The device can eject the volume */
  677. __u8 disconnect :1; /* The device can break request > ctl */
  678. __u8 reserved6_5 :1;
  679. unsigned ecc :1; /* Supports error correction */
  680. unsigned cmprs :1; /* Supports data compression */
  681. unsigned reserved7_0 :1;
  682. unsigned blk512 :1; /* Supports 512 bytes block size */
  683. unsigned blk1024 :1; /* Supports 1024 bytes block size */
  684. unsigned reserved7_3_6 :4;
  685. unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
  686. /* transfers for slow buffer memory ??? */
  687. /* Also 32768 block size in some cases */
  688. __u16 max_speed; /* Maximum speed supported in KBps */
  689. __u8 reserved10, reserved11;
  690. __u16 ctl; /* Continuous Transfer Limit in blocks */
  691. __u16 speed; /* Current Speed, in KBps */
  692. __u16 buffer_size; /* Buffer Size, in 512 bytes */
  693. __u8 reserved18, reserved19;
  694. } idetape_capabilities_page_t;
  695. /*
  696. * Block Size Page
  697. */
  698. typedef struct {
  699. unsigned page_code :6; /* Page code - Should be 0x30 */
  700. unsigned reserved1_6 :1;
  701. unsigned ps :1;
  702. __u8 page_length; /* Page Length - Should be 2 */
  703. __u8 reserved2;
  704. unsigned play32 :1;
  705. unsigned play32_5 :1;
  706. unsigned reserved2_23 :2;
  707. unsigned record32 :1;
  708. unsigned record32_5 :1;
  709. unsigned reserved2_6 :1;
  710. unsigned one :1;
  711. } idetape_block_size_page_t;
  712. /*
  713. * A pipeline stage.
  714. */
  715. typedef struct idetape_stage_s {
  716. struct request rq; /* The corresponding request */
  717. struct idetape_bh *bh; /* The data buffers */
  718. struct idetape_stage_s *next; /* Pointer to the next stage */
  719. } idetape_stage_t;
  720. /*
  721. * REQUEST SENSE packet command result - Data Format.
  722. */
  723. typedef struct {
  724. unsigned error_code :7; /* Current of deferred errors */
  725. unsigned valid :1; /* The information field conforms to QIC-157C */
  726. __u8 reserved1 :8; /* Segment Number - Reserved */
  727. unsigned sense_key :4; /* Sense Key */
  728. unsigned reserved2_4 :1; /* Reserved */
  729. unsigned ili :1; /* Incorrect Length Indicator */
  730. unsigned eom :1; /* End Of Medium */
  731. unsigned filemark :1; /* Filemark */
  732. __u32 information __attribute__ ((packed));
  733. __u8 asl; /* Additional sense length (n-7) */
  734. __u32 command_specific; /* Additional command specific information */
  735. __u8 asc; /* Additional Sense Code */
  736. __u8 ascq; /* Additional Sense Code Qualifier */
  737. __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
  738. unsigned sk_specific1 :7; /* Sense Key Specific */
  739. unsigned sksv :1; /* Sense Key Specific information is valid */
  740. __u8 sk_specific2; /* Sense Key Specific */
  741. __u8 sk_specific3; /* Sense Key Specific */
  742. __u8 pad[2]; /* Padding to 20 bytes */
  743. } idetape_request_sense_result_t;
  744. /*
  745. * Most of our global data which we need to save even as we leave the
  746. * driver due to an interrupt or a timer event is stored in a variable
  747. * of type idetape_tape_t, defined below.
  748. */
  749. typedef struct ide_tape_obj {
  750. ide_drive_t *drive;
  751. ide_driver_t *driver;
  752. struct gendisk *disk;
  753. struct kref kref;
  754. /*
  755. * Since a typical character device operation requires more
  756. * than one packet command, we provide here enough memory
  757. * for the maximum of interconnected packet commands.
  758. * The packet commands are stored in the circular array pc_stack.
  759. * pc_stack_index points to the last used entry, and warps around
  760. * to the start when we get to the last array entry.
  761. *
  762. * pc points to the current processed packet command.
  763. *
  764. * failed_pc points to the last failed packet command, or contains
  765. * NULL if we do not need to retry any packet command. This is
  766. * required since an additional packet command is needed before the
  767. * retry, to get detailed information on what went wrong.
  768. */
  769. /* Current packet command */
  770. idetape_pc_t *pc;
  771. /* Last failed packet command */
  772. idetape_pc_t *failed_pc;
  773. /* Packet command stack */
  774. idetape_pc_t pc_stack[IDETAPE_PC_STACK];
  775. /* Next free packet command storage space */
  776. int pc_stack_index;
  777. struct request rq_stack[IDETAPE_PC_STACK];
  778. /* We implement a circular array */
  779. int rq_stack_index;
  780. /*
  781. * DSC polling variables.
  782. *
  783. * While polling for DSC we use postponed_rq to postpone the
  784. * current request so that ide.c will be able to service
  785. * pending requests on the other device. Note that at most
  786. * we will have only one DSC (usually data transfer) request
  787. * in the device request queue. Additional requests can be
  788. * queued in our internal pipeline, but they will be visible
  789. * to ide.c only one at a time.
  790. */
  791. struct request *postponed_rq;
  792. /* The time in which we started polling for DSC */
  793. unsigned long dsc_polling_start;
  794. /* Timer used to poll for dsc */
  795. struct timer_list dsc_timer;
  796. /* Read/Write dsc polling frequency */
  797. unsigned long best_dsc_rw_frequency;
  798. /* The current polling frequency */
  799. unsigned long dsc_polling_frequency;
  800. /* Maximum waiting time */
  801. unsigned long dsc_timeout;
  802. /*
  803. * Read position information
  804. */
  805. u8 partition;
  806. /* Current block */
  807. unsigned int first_frame_position;
  808. unsigned int last_frame_position;
  809. unsigned int blocks_in_buffer;
  810. /*
  811. * Last error information
  812. */
  813. u8 sense_key, asc, ascq;
  814. /*
  815. * Character device operation
  816. */
  817. unsigned int minor;
  818. /* device name */
  819. char name[4];
  820. /* Current character device data transfer direction */
  821. idetape_chrdev_direction_t chrdev_direction;
  822. /*
  823. * Device information
  824. */
  825. /* Usually 512 or 1024 bytes */
  826. unsigned short tape_block_size;
  827. int user_bs_factor;
  828. /* Copy of the tape's Capabilities and Mechanical Page */
  829. idetape_capabilities_page_t capabilities;
  830. /*
  831. * Active data transfer request parameters.
  832. *
  833. * At most, there is only one ide-tape originated data transfer
  834. * request in the device request queue. This allows ide.c to
  835. * easily service requests from the other device when we
  836. * postpone our active request. In the pipelined operation
  837. * mode, we use our internal pipeline structure to hold
  838. * more data requests.
  839. *
  840. * The data buffer size is chosen based on the tape's
  841. * recommendation.
  842. */
  843. /* Pointer to the request which is waiting in the device request queue */
  844. struct request *active_data_request;
  845. /* Data buffer size (chosen based on the tape's recommendation */
  846. int stage_size;
  847. idetape_stage_t *merge_stage;
  848. int merge_stage_size;
  849. struct idetape_bh *bh;
  850. char *b_data;
  851. int b_count;
  852. /*
  853. * Pipeline parameters.
  854. *
  855. * To accomplish non-pipelined mode, we simply set the following
  856. * variables to zero (or NULL, where appropriate).
  857. */
  858. /* Number of currently used stages */
  859. int nr_stages;
  860. /* Number of pending stages */
  861. int nr_pending_stages;
  862. /* We will not allocate more than this number of stages */
  863. int max_stages, min_pipeline, max_pipeline;
  864. /* The first stage which will be removed from the pipeline */
  865. idetape_stage_t *first_stage;
  866. /* The currently active stage */
  867. idetape_stage_t *active_stage;
  868. /* Will be serviced after the currently active request */
  869. idetape_stage_t *next_stage;
  870. /* New requests will be added to the pipeline here */
  871. idetape_stage_t *last_stage;
  872. /* Optional free stage which we can use */
  873. idetape_stage_t *cache_stage;
  874. int pages_per_stage;
  875. /* Wasted space in each stage */
  876. int excess_bh_size;
  877. /* Status/Action flags: long for set_bit */
  878. unsigned long flags;
  879. /* protects the ide-tape queue */
  880. spinlock_t spinlock;
  881. /*
  882. * Measures average tape speed
  883. */
  884. unsigned long avg_time;
  885. int avg_size;
  886. int avg_speed;
  887. /* last sense information */
  888. idetape_request_sense_result_t sense;
  889. char vendor_id[10];
  890. char product_id[18];
  891. char firmware_revision[6];
  892. int firmware_revision_num;
  893. /* the door is currently locked */
  894. int door_locked;
  895. /* the tape hardware is write protected */
  896. char drv_write_prot;
  897. /* the tape is write protected (hardware or opened as read-only) */
  898. char write_prot;
  899. /*
  900. * Limit the number of times a request can
  901. * be postponed, to avoid an infinite postpone
  902. * deadlock.
  903. */
  904. /* request postpone count limit */
  905. int postpone_cnt;
  906. /*
  907. * Measures number of frames:
  908. *
  909. * 1. written/read to/from the driver pipeline (pipeline_head).
  910. * 2. written/read to/from the tape buffers (idetape_bh).
  911. * 3. written/read by the tape to/from the media (tape_head).
  912. */
  913. int pipeline_head;
  914. int buffer_head;
  915. int tape_head;
  916. int last_tape_head;
  917. /*
  918. * Speed control at the tape buffers input/output
  919. */
  920. unsigned long insert_time;
  921. int insert_size;
  922. int insert_speed;
  923. int max_insert_speed;
  924. int measure_insert_time;
  925. /*
  926. * Measure tape still time, in milliseconds
  927. */
  928. unsigned long tape_still_time_begin;
  929. int tape_still_time;
  930. /*
  931. * Speed regulation negative feedback loop
  932. */
  933. int speed_control;
  934. int pipeline_head_speed;
  935. int controlled_pipeline_head_speed;
  936. int uncontrolled_pipeline_head_speed;
  937. int controlled_last_pipeline_head;
  938. int uncontrolled_last_pipeline_head;
  939. unsigned long uncontrolled_pipeline_head_time;
  940. unsigned long controlled_pipeline_head_time;
  941. int controlled_previous_pipeline_head;
  942. int uncontrolled_previous_pipeline_head;
  943. unsigned long controlled_previous_head_time;
  944. unsigned long uncontrolled_previous_head_time;
  945. int restart_speed_control_req;
  946. /*
  947. * Debug_level determines amount of debugging output;
  948. * can be changed using /proc/ide/hdx/settings
  949. * 0 : almost no debugging output
  950. * 1 : 0+output errors only
  951. * 2 : 1+output all sensekey/asc
  952. * 3 : 2+follow all chrdev related procedures
  953. * 4 : 3+follow all procedures
  954. * 5 : 4+include pc_stack rq_stack info
  955. * 6 : 5+USE_COUNT updates
  956. */
  957. int debug_level;
  958. } idetape_tape_t;
  959. static DECLARE_MUTEX(idetape_ref_sem);
  960. static struct class *idetape_sysfs_class;
  961. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  962. #define ide_tape_g(disk) \
  963. container_of((disk)->private_data, struct ide_tape_obj, driver)
  964. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  965. {
  966. struct ide_tape_obj *tape = NULL;
  967. down(&idetape_ref_sem);
  968. tape = ide_tape_g(disk);
  969. if (tape)
  970. kref_get(&tape->kref);
  971. up(&idetape_ref_sem);
  972. return tape;
  973. }
  974. static void ide_tape_release(struct kref *);
  975. static void ide_tape_put(struct ide_tape_obj *tape)
  976. {
  977. down(&idetape_ref_sem);
  978. kref_put(&tape->kref, ide_tape_release);
  979. up(&idetape_ref_sem);
  980. }
  981. /*
  982. * Tape door status
  983. */
  984. #define DOOR_UNLOCKED 0
  985. #define DOOR_LOCKED 1
  986. #define DOOR_EXPLICITLY_LOCKED 2
  987. /*
  988. * Tape flag bits values.
  989. */
  990. #define IDETAPE_IGNORE_DSC 0
  991. #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
  992. #define IDETAPE_BUSY 2 /* Device already opened */
  993. #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
  994. #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
  995. #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
  996. #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
  997. #define IDETAPE_READ_ERROR 7
  998. #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
  999. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  1000. #define IDETAPE_MEDIUM_PRESENT 9
  1001. /*
  1002. * Supported ATAPI tape drives packet commands
  1003. */
  1004. #define IDETAPE_TEST_UNIT_READY_CMD 0x00
  1005. #define IDETAPE_REWIND_CMD 0x01
  1006. #define IDETAPE_REQUEST_SENSE_CMD 0x03
  1007. #define IDETAPE_READ_CMD 0x08
  1008. #define IDETAPE_WRITE_CMD 0x0a
  1009. #define IDETAPE_WRITE_FILEMARK_CMD 0x10
  1010. #define IDETAPE_SPACE_CMD 0x11
  1011. #define IDETAPE_INQUIRY_CMD 0x12
  1012. #define IDETAPE_ERASE_CMD 0x19
  1013. #define IDETAPE_MODE_SENSE_CMD 0x1a
  1014. #define IDETAPE_MODE_SELECT_CMD 0x15
  1015. #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
  1016. #define IDETAPE_PREVENT_CMD 0x1e
  1017. #define IDETAPE_LOCATE_CMD 0x2b
  1018. #define IDETAPE_READ_POSITION_CMD 0x34
  1019. #define IDETAPE_READ_BUFFER_CMD 0x3c
  1020. #define IDETAPE_SET_SPEED_CMD 0xbb
  1021. /*
  1022. * Some defines for the READ BUFFER command
  1023. */
  1024. #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
  1025. /*
  1026. * Some defines for the SPACE command
  1027. */
  1028. #define IDETAPE_SPACE_OVER_FILEMARK 1
  1029. #define IDETAPE_SPACE_TO_EOD 3
  1030. /*
  1031. * Some defines for the LOAD UNLOAD command
  1032. */
  1033. #define IDETAPE_LU_LOAD_MASK 1
  1034. #define IDETAPE_LU_RETENSION_MASK 2
  1035. #define IDETAPE_LU_EOT_MASK 4
  1036. /*
  1037. * Special requests for our block device strategy routine.
  1038. *
  1039. * In order to service a character device command, we add special
  1040. * requests to the tail of our block device request queue and wait
  1041. * for their completion.
  1042. */
  1043. enum {
  1044. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  1045. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  1046. REQ_IDETAPE_READ = (1 << 2),
  1047. REQ_IDETAPE_WRITE = (1 << 3),
  1048. REQ_IDETAPE_READ_BUFFER = (1 << 4),
  1049. };
  1050. /*
  1051. * Error codes which are returned in rq->errors to the higher part
  1052. * of the driver.
  1053. */
  1054. #define IDETAPE_ERROR_GENERAL 101
  1055. #define IDETAPE_ERROR_FILEMARK 102
  1056. #define IDETAPE_ERROR_EOD 103
  1057. /*
  1058. * The following is used to format the general configuration word of
  1059. * the ATAPI IDENTIFY DEVICE command.
  1060. */
  1061. struct idetape_id_gcw {
  1062. unsigned packet_size :2; /* Packet Size */
  1063. unsigned reserved234 :3; /* Reserved */
  1064. unsigned drq_type :2; /* Command packet DRQ type */
  1065. unsigned removable :1; /* Removable media */
  1066. unsigned device_type :5; /* Device type */
  1067. unsigned reserved13 :1; /* Reserved */
  1068. unsigned protocol :2; /* Protocol type */
  1069. };
  1070. /*
  1071. * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
  1072. */
  1073. typedef struct {
  1074. unsigned device_type :5; /* Peripheral Device Type */
  1075. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  1076. unsigned reserved1_6t0 :7; /* Reserved */
  1077. unsigned rmb :1; /* Removable Medium Bit */
  1078. unsigned ansi_version :3; /* ANSI Version */
  1079. unsigned ecma_version :3; /* ECMA Version */
  1080. unsigned iso_version :2; /* ISO Version */
  1081. unsigned response_format :4; /* Response Data Format */
  1082. unsigned reserved3_45 :2; /* Reserved */
  1083. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  1084. unsigned reserved3_7 :1; /* AENC - Reserved */
  1085. __u8 additional_length; /* Additional Length (total_length-4) */
  1086. __u8 rsv5, rsv6, rsv7; /* Reserved */
  1087. __u8 vendor_id[8]; /* Vendor Identification */
  1088. __u8 product_id[16]; /* Product Identification */
  1089. __u8 revision_level[4]; /* Revision Level */
  1090. __u8 vendor_specific[20]; /* Vendor Specific - Optional */
  1091. __u8 reserved56t95[40]; /* Reserved - Optional */
  1092. /* Additional information may be returned */
  1093. } idetape_inquiry_result_t;
  1094. /*
  1095. * READ POSITION packet command - Data Format (From Table 6-57)
  1096. */
  1097. typedef struct {
  1098. unsigned reserved0_10 :2; /* Reserved */
  1099. unsigned bpu :1; /* Block Position Unknown */
  1100. unsigned reserved0_543 :3; /* Reserved */
  1101. unsigned eop :1; /* End Of Partition */
  1102. unsigned bop :1; /* Beginning Of Partition */
  1103. u8 partition; /* Partition Number */
  1104. u8 reserved2, reserved3; /* Reserved */
  1105. u32 first_block; /* First Block Location */
  1106. u32 last_block; /* Last Block Location (Optional) */
  1107. u8 reserved12; /* Reserved */
  1108. u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
  1109. u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
  1110. } idetape_read_position_result_t;
  1111. /*
  1112. * Follows structures which are related to the SELECT SENSE / MODE SENSE
  1113. * packet commands. Those packet commands are still not supported
  1114. * by ide-tape.
  1115. */
  1116. #define IDETAPE_BLOCK_DESCRIPTOR 0
  1117. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  1118. #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
  1119. #define IDETAPE_BLOCK_SIZE_PAGE 0x30
  1120. #define IDETAPE_BUFFER_FILLING_PAGE 0x33
  1121. /*
  1122. * Mode Parameter Header for the MODE SENSE packet command
  1123. */
  1124. typedef struct {
  1125. __u8 mode_data_length; /* Length of the following data transfer */
  1126. __u8 medium_type; /* Medium Type */
  1127. __u8 dsp; /* Device Specific Parameter */
  1128. __u8 bdl; /* Block Descriptor Length */
  1129. #if 0
  1130. /* data transfer page */
  1131. __u8 page_code :6;
  1132. __u8 reserved0_6 :1;
  1133. __u8 ps :1; /* parameters saveable */
  1134. __u8 page_length; /* page Length == 0x02 */
  1135. __u8 reserved2;
  1136. __u8 read32k :1; /* 32k blk size (data only) */
  1137. __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
  1138. __u8 reserved3_23 :2;
  1139. __u8 write32k :1; /* 32k blk size (data only) */
  1140. __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
  1141. __u8 reserved3_6 :1;
  1142. __u8 streaming :1; /* streaming mode enable */
  1143. #endif
  1144. } idetape_mode_parameter_header_t;
  1145. /*
  1146. * Mode Parameter Block Descriptor the MODE SENSE packet command
  1147. *
  1148. * Support for block descriptors is optional.
  1149. */
  1150. typedef struct {
  1151. __u8 density_code; /* Medium density code */
  1152. __u8 blocks[3]; /* Number of blocks */
  1153. __u8 reserved4; /* Reserved */
  1154. __u8 length[3]; /* Block Length */
  1155. } idetape_parameter_block_descriptor_t;
  1156. /*
  1157. * The Data Compression Page, as returned by the MODE SENSE packet command.
  1158. */
  1159. typedef struct {
  1160. unsigned page_code :6; /* Page Code - Should be 0xf */
  1161. unsigned reserved0 :1; /* Reserved */
  1162. unsigned ps :1;
  1163. __u8 page_length; /* Page Length - Should be 14 */
  1164. unsigned reserved2 :6; /* Reserved */
  1165. unsigned dcc :1; /* Data Compression Capable */
  1166. unsigned dce :1; /* Data Compression Enable */
  1167. unsigned reserved3 :5; /* Reserved */
  1168. unsigned red :2; /* Report Exception on Decompression */
  1169. unsigned dde :1; /* Data Decompression Enable */
  1170. __u32 ca; /* Compression Algorithm */
  1171. __u32 da; /* Decompression Algorithm */
  1172. __u8 reserved[4]; /* Reserved */
  1173. } idetape_data_compression_page_t;
  1174. /*
  1175. * The Medium Partition Page, as returned by the MODE SENSE packet command.
  1176. */
  1177. typedef struct {
  1178. unsigned page_code :6; /* Page Code - Should be 0x11 */
  1179. unsigned reserved1_6 :1; /* Reserved */
  1180. unsigned ps :1;
  1181. __u8 page_length; /* Page Length - Should be 6 */
  1182. __u8 map; /* Maximum Additional Partitions - Should be 0 */
  1183. __u8 apd; /* Additional Partitions Defined - Should be 0 */
  1184. unsigned reserved4_012 :3; /* Reserved */
  1185. unsigned psum :2; /* Should be 0 */
  1186. unsigned idp :1; /* Should be 0 */
  1187. unsigned sdp :1; /* Should be 0 */
  1188. unsigned fdp :1; /* Fixed Data Partitions */
  1189. __u8 mfr; /* Medium Format Recognition */
  1190. __u8 reserved[2]; /* Reserved */
  1191. } idetape_medium_partition_page_t;
  1192. /*
  1193. * Run time configurable parameters.
  1194. */
  1195. typedef struct {
  1196. int dsc_rw_frequency;
  1197. int dsc_media_access_frequency;
  1198. int nr_stages;
  1199. } idetape_config_t;
  1200. /*
  1201. * The variables below are used for the character device interface.
  1202. * Additional state variables are defined in our ide_drive_t structure.
  1203. */
  1204. static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
  1205. #define ide_tape_f(file) ((file)->private_data)
  1206. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  1207. {
  1208. struct ide_tape_obj *tape = NULL;
  1209. down(&idetape_ref_sem);
  1210. tape = idetape_devs[i];
  1211. if (tape)
  1212. kref_get(&tape->kref);
  1213. up(&idetape_ref_sem);
  1214. return tape;
  1215. }
  1216. /*
  1217. * Function declarations
  1218. *
  1219. */
  1220. static int idetape_chrdev_release (struct inode *inode, struct file *filp);
  1221. static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
  1222. /*
  1223. * Too bad. The drive wants to send us data which we are not ready to accept.
  1224. * Just throw it away.
  1225. */
  1226. static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
  1227. {
  1228. while (bcount--)
  1229. (void) HWIF(drive)->INB(IDE_DATA_REG);
  1230. }
  1231. static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1232. {
  1233. struct idetape_bh *bh = pc->bh;
  1234. int count;
  1235. while (bcount) {
  1236. #if IDETAPE_DEBUG_BUGS
  1237. if (bh == NULL) {
  1238. printk(KERN_ERR "ide-tape: bh == NULL in "
  1239. "idetape_input_buffers\n");
  1240. idetape_discard_data(drive, bcount);
  1241. return;
  1242. }
  1243. #endif /* IDETAPE_DEBUG_BUGS */
  1244. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
  1245. HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
  1246. bcount -= count;
  1247. atomic_add(count, &bh->b_count);
  1248. if (atomic_read(&bh->b_count) == bh->b_size) {
  1249. bh = bh->b_reqnext;
  1250. if (bh)
  1251. atomic_set(&bh->b_count, 0);
  1252. }
  1253. }
  1254. pc->bh = bh;
  1255. }
  1256. static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1257. {
  1258. struct idetape_bh *bh = pc->bh;
  1259. int count;
  1260. while (bcount) {
  1261. #if IDETAPE_DEBUG_BUGS
  1262. if (bh == NULL) {
  1263. printk(KERN_ERR "ide-tape: bh == NULL in "
  1264. "idetape_output_buffers\n");
  1265. return;
  1266. }
  1267. #endif /* IDETAPE_DEBUG_BUGS */
  1268. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  1269. HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
  1270. bcount -= count;
  1271. pc->b_data += count;
  1272. pc->b_count -= count;
  1273. if (!pc->b_count) {
  1274. pc->bh = bh = bh->b_reqnext;
  1275. if (bh) {
  1276. pc->b_data = bh->b_data;
  1277. pc->b_count = atomic_read(&bh->b_count);
  1278. }
  1279. }
  1280. }
  1281. }
  1282. static void idetape_update_buffers (idetape_pc_t *pc)
  1283. {
  1284. struct idetape_bh *bh = pc->bh;
  1285. int count;
  1286. unsigned int bcount = pc->actually_transferred;
  1287. if (test_bit(PC_WRITING, &pc->flags))
  1288. return;
  1289. while (bcount) {
  1290. #if IDETAPE_DEBUG_BUGS
  1291. if (bh == NULL) {
  1292. printk(KERN_ERR "ide-tape: bh == NULL in "
  1293. "idetape_update_buffers\n");
  1294. return;
  1295. }
  1296. #endif /* IDETAPE_DEBUG_BUGS */
  1297. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  1298. atomic_set(&bh->b_count, count);
  1299. if (atomic_read(&bh->b_count) == bh->b_size)
  1300. bh = bh->b_reqnext;
  1301. bcount -= count;
  1302. }
  1303. pc->bh = bh;
  1304. }
  1305. /*
  1306. * idetape_next_pc_storage returns a pointer to a place in which we can
  1307. * safely store a packet command, even though we intend to leave the
  1308. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  1309. * commands is allocated at initialization time.
  1310. */
  1311. static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
  1312. {
  1313. idetape_tape_t *tape = drive->driver_data;
  1314. #if IDETAPE_DEBUG_LOG
  1315. if (tape->debug_level >= 5)
  1316. printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
  1317. tape->pc_stack_index);
  1318. #endif /* IDETAPE_DEBUG_LOG */
  1319. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  1320. tape->pc_stack_index=0;
  1321. return (&tape->pc_stack[tape->pc_stack_index++]);
  1322. }
  1323. /*
  1324. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  1325. * Since we queue packet commands in the request queue, we need to
  1326. * allocate a request, along with the allocation of a packet command.
  1327. */
  1328. /**************************************************************
  1329. * *
  1330. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  1331. * followed later on by kfree(). -ml *
  1332. * *
  1333. **************************************************************/
  1334. static struct request *idetape_next_rq_storage (ide_drive_t *drive)
  1335. {
  1336. idetape_tape_t *tape = drive->driver_data;
  1337. #if IDETAPE_DEBUG_LOG
  1338. if (tape->debug_level >= 5)
  1339. printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
  1340. tape->rq_stack_index);
  1341. #endif /* IDETAPE_DEBUG_LOG */
  1342. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  1343. tape->rq_stack_index=0;
  1344. return (&tape->rq_stack[tape->rq_stack_index++]);
  1345. }
  1346. /*
  1347. * idetape_init_pc initializes a packet command.
  1348. */
  1349. static void idetape_init_pc (idetape_pc_t *pc)
  1350. {
  1351. memset(pc->c, 0, 12);
  1352. pc->retries = 0;
  1353. pc->flags = 0;
  1354. pc->request_transfer = 0;
  1355. pc->buffer = pc->pc_buffer;
  1356. pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
  1357. pc->bh = NULL;
  1358. pc->b_data = NULL;
  1359. }
  1360. /*
  1361. * idetape_analyze_error is called on each failed packet command retry
  1362. * to analyze the request sense. We currently do not utilize this
  1363. * information.
  1364. */
  1365. static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
  1366. {
  1367. idetape_tape_t *tape = drive->driver_data;
  1368. idetape_pc_t *pc = tape->failed_pc;
  1369. tape->sense = *result;
  1370. tape->sense_key = result->sense_key;
  1371. tape->asc = result->asc;
  1372. tape->ascq = result->ascq;
  1373. #if IDETAPE_DEBUG_LOG
  1374. /*
  1375. * Without debugging, we only log an error if we decided to
  1376. * give up retrying.
  1377. */
  1378. if (tape->debug_level >= 1)
  1379. printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
  1380. "asc = %x, ascq = %x\n",
  1381. pc->c[0], result->sense_key,
  1382. result->asc, result->ascq);
  1383. #endif /* IDETAPE_DEBUG_LOG */
  1384. /*
  1385. * Correct pc->actually_transferred by asking the tape.
  1386. */
  1387. if (test_bit(PC_DMA_ERROR, &pc->flags)) {
  1388. pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
  1389. idetape_update_buffers(pc);
  1390. }
  1391. /*
  1392. * If error was the result of a zero-length read or write command,
  1393. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  1394. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  1395. */
  1396. if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
  1397. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */
  1398. if (result->sense_key == 5) {
  1399. /* don't report an error, everything's ok */
  1400. pc->error = 0;
  1401. /* don't retry read/write */
  1402. set_bit(PC_ABORT, &pc->flags);
  1403. }
  1404. }
  1405. if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
  1406. pc->error = IDETAPE_ERROR_FILEMARK;
  1407. set_bit(PC_ABORT, &pc->flags);
  1408. }
  1409. if (pc->c[0] == IDETAPE_WRITE_CMD) {
  1410. if (result->eom ||
  1411. (result->sense_key == 0xd && result->asc == 0x0 &&
  1412. result->ascq == 0x2)) {
  1413. pc->error = IDETAPE_ERROR_EOD;
  1414. set_bit(PC_ABORT, &pc->flags);
  1415. }
  1416. }
  1417. if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
  1418. if (result->sense_key == 8) {
  1419. pc->error = IDETAPE_ERROR_EOD;
  1420. set_bit(PC_ABORT, &pc->flags);
  1421. }
  1422. if (!test_bit(PC_ABORT, &pc->flags) &&
  1423. pc->actually_transferred)
  1424. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  1425. }
  1426. }
  1427. /*
  1428. * idetape_active_next_stage will declare the next stage as "active".
  1429. */
  1430. static void idetape_active_next_stage (ide_drive_t *drive)
  1431. {
  1432. idetape_tape_t *tape = drive->driver_data;
  1433. idetape_stage_t *stage = tape->next_stage;
  1434. struct request *rq = &stage->rq;
  1435. #if IDETAPE_DEBUG_LOG
  1436. if (tape->debug_level >= 4)
  1437. printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
  1438. #endif /* IDETAPE_DEBUG_LOG */
  1439. #if IDETAPE_DEBUG_BUGS
  1440. if (stage == NULL) {
  1441. printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
  1442. return;
  1443. }
  1444. #endif /* IDETAPE_DEBUG_BUGS */
  1445. rq->rq_disk = tape->disk;
  1446. rq->buffer = NULL;
  1447. rq->special = (void *)stage->bh;
  1448. tape->active_data_request = rq;
  1449. tape->active_stage = stage;
  1450. tape->next_stage = stage->next;
  1451. }
  1452. /*
  1453. * idetape_increase_max_pipeline_stages is a part of the feedback
  1454. * loop which tries to find the optimum number of stages. In the
  1455. * feedback loop, we are starting from a minimum maximum number of
  1456. * stages, and if we sense that the pipeline is empty, we try to
  1457. * increase it, until we reach the user compile time memory limit.
  1458. */
  1459. static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
  1460. {
  1461. idetape_tape_t *tape = drive->driver_data;
  1462. int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
  1463. #if IDETAPE_DEBUG_LOG
  1464. if (tape->debug_level >= 4)
  1465. printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
  1466. #endif /* IDETAPE_DEBUG_LOG */
  1467. tape->max_stages += max(increase, 1);
  1468. tape->max_stages = max(tape->max_stages, tape->min_pipeline);
  1469. tape->max_stages = min(tape->max_stages, tape->max_pipeline);
  1470. }
  1471. /*
  1472. * idetape_kfree_stage calls kfree to completely free a stage, along with
  1473. * its related buffers.
  1474. */
  1475. static void __idetape_kfree_stage (idetape_stage_t *stage)
  1476. {
  1477. struct idetape_bh *prev_bh, *bh = stage->bh;
  1478. int size;
  1479. while (bh != NULL) {
  1480. if (bh->b_data != NULL) {
  1481. size = (int) bh->b_size;
  1482. while (size > 0) {
  1483. free_page((unsigned long) bh->b_data);
  1484. size -= PAGE_SIZE;
  1485. bh->b_data += PAGE_SIZE;
  1486. }
  1487. }
  1488. prev_bh = bh;
  1489. bh = bh->b_reqnext;
  1490. kfree(prev_bh);
  1491. }
  1492. kfree(stage);
  1493. }
  1494. static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
  1495. {
  1496. __idetape_kfree_stage(stage);
  1497. }
  1498. /*
  1499. * idetape_remove_stage_head removes tape->first_stage from the pipeline.
  1500. * The caller should avoid race conditions.
  1501. */
  1502. static void idetape_remove_stage_head (ide_drive_t *drive)
  1503. {
  1504. idetape_tape_t *tape = drive->driver_data;
  1505. idetape_stage_t *stage;
  1506. #if IDETAPE_DEBUG_LOG
  1507. if (tape->debug_level >= 4)
  1508. printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
  1509. #endif /* IDETAPE_DEBUG_LOG */
  1510. #if IDETAPE_DEBUG_BUGS
  1511. if (tape->first_stage == NULL) {
  1512. printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
  1513. return;
  1514. }
  1515. if (tape->active_stage == tape->first_stage) {
  1516. printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
  1517. return;
  1518. }
  1519. #endif /* IDETAPE_DEBUG_BUGS */
  1520. stage = tape->first_stage;
  1521. tape->first_stage = stage->next;
  1522. idetape_kfree_stage(tape, stage);
  1523. tape->nr_stages--;
  1524. if (tape->first_stage == NULL) {
  1525. tape->last_stage = NULL;
  1526. #if IDETAPE_DEBUG_BUGS
  1527. if (tape->next_stage != NULL)
  1528. printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
  1529. if (tape->nr_stages)
  1530. printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
  1531. #endif /* IDETAPE_DEBUG_BUGS */
  1532. }
  1533. }
  1534. /*
  1535. * This will free all the pipeline stages starting from new_last_stage->next
  1536. * to the end of the list, and point tape->last_stage to new_last_stage.
  1537. */
  1538. static void idetape_abort_pipeline(ide_drive_t *drive,
  1539. idetape_stage_t *new_last_stage)
  1540. {
  1541. idetape_tape_t *tape = drive->driver_data;
  1542. idetape_stage_t *stage = new_last_stage->next;
  1543. idetape_stage_t *nstage;
  1544. #if IDETAPE_DEBUG_LOG
  1545. if (tape->debug_level >= 4)
  1546. printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
  1547. #endif
  1548. while (stage) {
  1549. nstage = stage->next;
  1550. idetape_kfree_stage(tape, stage);
  1551. --tape->nr_stages;
  1552. --tape->nr_pending_stages;
  1553. stage = nstage;
  1554. }
  1555. if (new_last_stage)
  1556. new_last_stage->next = NULL;
  1557. tape->last_stage = new_last_stage;
  1558. tape->next_stage = NULL;
  1559. }
  1560. /*
  1561. * idetape_end_request is used to finish servicing a request, and to
  1562. * insert a pending pipeline request into the main device queue.
  1563. */
  1564. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  1565. {
  1566. struct request *rq = HWGROUP(drive)->rq;
  1567. idetape_tape_t *tape = drive->driver_data;
  1568. unsigned long flags;
  1569. int error;
  1570. int remove_stage = 0;
  1571. idetape_stage_t *active_stage;
  1572. #if IDETAPE_DEBUG_LOG
  1573. if (tape->debug_level >= 4)
  1574. printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
  1575. #endif /* IDETAPE_DEBUG_LOG */
  1576. switch (uptodate) {
  1577. case 0: error = IDETAPE_ERROR_GENERAL; break;
  1578. case 1: error = 0; break;
  1579. default: error = uptodate;
  1580. }
  1581. rq->errors = error;
  1582. if (error)
  1583. tape->failed_pc = NULL;
  1584. spin_lock_irqsave(&tape->spinlock, flags);
  1585. /* The request was a pipelined data transfer request */
  1586. if (tape->active_data_request == rq) {
  1587. active_stage = tape->active_stage;
  1588. tape->active_stage = NULL;
  1589. tape->active_data_request = NULL;
  1590. tape->nr_pending_stages--;
  1591. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1592. remove_stage = 1;
  1593. if (error) {
  1594. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1595. if (error == IDETAPE_ERROR_EOD)
  1596. idetape_abort_pipeline(drive, active_stage);
  1597. }
  1598. } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1599. if (error == IDETAPE_ERROR_EOD) {
  1600. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1601. idetape_abort_pipeline(drive, active_stage);
  1602. }
  1603. }
  1604. if (tape->next_stage != NULL) {
  1605. idetape_active_next_stage(drive);
  1606. /*
  1607. * Insert the next request into the request queue.
  1608. */
  1609. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  1610. } else if (!error) {
  1611. idetape_increase_max_pipeline_stages(drive);
  1612. }
  1613. }
  1614. ide_end_drive_cmd(drive, 0, 0);
  1615. // blkdev_dequeue_request(rq);
  1616. // drive->rq = NULL;
  1617. // end_that_request_last(rq);
  1618. if (remove_stage)
  1619. idetape_remove_stage_head(drive);
  1620. if (tape->active_data_request == NULL)
  1621. clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1622. spin_unlock_irqrestore(&tape->spinlock, flags);
  1623. return 0;
  1624. }
  1625. static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
  1626. {
  1627. idetape_tape_t *tape = drive->driver_data;
  1628. #if IDETAPE_DEBUG_LOG
  1629. if (tape->debug_level >= 4)
  1630. printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
  1631. #endif /* IDETAPE_DEBUG_LOG */
  1632. if (!tape->pc->error) {
  1633. idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
  1634. idetape_end_request(drive, 1, 0);
  1635. } else {
  1636. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
  1637. idetape_end_request(drive, 0, 0);
  1638. }
  1639. return ide_stopped;
  1640. }
  1641. static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
  1642. {
  1643. idetape_init_pc(pc);
  1644. pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
  1645. pc->c[4] = 20;
  1646. pc->request_transfer = 20;
  1647. pc->callback = &idetape_request_sense_callback;
  1648. }
  1649. static void idetape_init_rq(struct request *rq, u8 cmd)
  1650. {
  1651. memset(rq, 0, sizeof(*rq));
  1652. rq->flags = REQ_SPECIAL;
  1653. rq->cmd[0] = cmd;
  1654. }
  1655. /*
  1656. * idetape_queue_pc_head generates a new packet command request in front
  1657. * of the request queue, before the current request, so that it will be
  1658. * processed immediately, on the next pass through the driver.
  1659. *
  1660. * idetape_queue_pc_head is called from the request handling part of
  1661. * the driver (the "bottom" part). Safe storage for the request should
  1662. * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
  1663. * before calling idetape_queue_pc_head.
  1664. *
  1665. * Memory for those requests is pre-allocated at initialization time, and
  1666. * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
  1667. * space for the maximum possible number of inter-dependent packet commands.
  1668. *
  1669. * The higher level of the driver - The ioctl handler and the character
  1670. * device handling functions should queue request to the lower level part
  1671. * and wait for their completion using idetape_queue_pc_tail or
  1672. * idetape_queue_rw_tail.
  1673. */
  1674. static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
  1675. {
  1676. struct ide_tape_obj *tape = drive->driver_data;
  1677. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  1678. rq->buffer = (char *) pc;
  1679. rq->rq_disk = tape->disk;
  1680. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  1681. }
  1682. /*
  1683. * idetape_retry_pc is called when an error was detected during the
  1684. * last packet command. We queue a request sense packet command in
  1685. * the head of the request list.
  1686. */
  1687. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  1688. {
  1689. idetape_tape_t *tape = drive->driver_data;
  1690. idetape_pc_t *pc;
  1691. struct request *rq;
  1692. atapi_error_t error;
  1693. error.all = HWIF(drive)->INB(IDE_ERROR_REG);
  1694. pc = idetape_next_pc_storage(drive);
  1695. rq = idetape_next_rq_storage(drive);
  1696. idetape_create_request_sense_cmd(pc);
  1697. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1698. idetape_queue_pc_head(drive, pc, rq);
  1699. return ide_stopped;
  1700. }
  1701. /*
  1702. * idetape_postpone_request postpones the current request so that
  1703. * ide.c will be able to service requests from another device on
  1704. * the same hwgroup while we are polling for DSC.
  1705. */
  1706. static void idetape_postpone_request (ide_drive_t *drive)
  1707. {
  1708. idetape_tape_t *tape = drive->driver_data;
  1709. #if IDETAPE_DEBUG_LOG
  1710. if (tape->debug_level >= 4)
  1711. printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
  1712. #endif
  1713. tape->postponed_rq = HWGROUP(drive)->rq;
  1714. ide_stall_queue(drive, tape->dsc_polling_frequency);
  1715. }
  1716. /*
  1717. * idetape_pc_intr is the usual interrupt handler which will be called
  1718. * during a packet command. We will transfer some of the data (as
  1719. * requested by the drive) and will re-point interrupt handler to us.
  1720. * When data transfer is finished, we will act according to the
  1721. * algorithm described before idetape_issue_packet_command.
  1722. *
  1723. */
  1724. static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
  1725. {
  1726. ide_hwif_t *hwif = drive->hwif;
  1727. idetape_tape_t *tape = drive->driver_data;
  1728. atapi_status_t status;
  1729. atapi_bcount_t bcount;
  1730. atapi_ireason_t ireason;
  1731. idetape_pc_t *pc = tape->pc;
  1732. unsigned int temp;
  1733. #if SIMULATE_ERRORS
  1734. static int error_sim_count = 0;
  1735. #endif
  1736. #if IDETAPE_DEBUG_LOG
  1737. if (tape->debug_level >= 4)
  1738. printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
  1739. "interrupt handler\n");
  1740. #endif /* IDETAPE_DEBUG_LOG */
  1741. /* Clear the interrupt */
  1742. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  1743. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1744. if (HWIF(drive)->ide_dma_end(drive) || status.b.check) {
  1745. /*
  1746. * A DMA error is sometimes expected. For example,
  1747. * if the tape is crossing a filemark during a
  1748. * READ command, it will issue an irq and position
  1749. * itself before the filemark, so that only a partial
  1750. * data transfer will occur (which causes the DMA
  1751. * error). In that case, we will later ask the tape
  1752. * how much bytes of the original request were
  1753. * actually transferred (we can't receive that
  1754. * information from the DMA engine on most chipsets).
  1755. */
  1756. /*
  1757. * On the contrary, a DMA error is never expected;
  1758. * it usually indicates a hardware error or abort.
  1759. * If the tape crosses a filemark during a READ
  1760. * command, it will issue an irq and position itself
  1761. * after the filemark (not before). Only a partial
  1762. * data transfer will occur, but no DMA error.
  1763. * (AS, 19 Apr 2001)
  1764. */
  1765. set_bit(PC_DMA_ERROR, &pc->flags);
  1766. } else {
  1767. pc->actually_transferred = pc->request_transfer;
  1768. idetape_update_buffers(pc);
  1769. }
  1770. #if IDETAPE_DEBUG_LOG
  1771. if (tape->debug_level >= 4)
  1772. printk(KERN_INFO "ide-tape: DMA finished\n");
  1773. #endif /* IDETAPE_DEBUG_LOG */
  1774. }
  1775. /* No more interrupts */
  1776. if (!status.b.drq) {
  1777. #if IDETAPE_DEBUG_LOG
  1778. if (tape->debug_level >= 2)
  1779. printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
  1780. #endif /* IDETAPE_DEBUG_LOG */
  1781. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1782. local_irq_enable();
  1783. #if SIMULATE_ERRORS
  1784. if ((pc->c[0] == IDETAPE_WRITE_CMD ||
  1785. pc->c[0] == IDETAPE_READ_CMD) &&
  1786. (++error_sim_count % 100) == 0) {
  1787. printk(KERN_INFO "ide-tape: %s: simulating error\n",
  1788. tape->name);
  1789. status.b.check = 1;
  1790. }
  1791. #endif
  1792. if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
  1793. status.b.check = 0;
  1794. if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) { /* Error detected */
  1795. #if IDETAPE_DEBUG_LOG
  1796. if (tape->debug_level >= 1)
  1797. printk(KERN_INFO "ide-tape: %s: I/O error\n",
  1798. tape->name);
  1799. #endif /* IDETAPE_DEBUG_LOG */
  1800. if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1801. printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
  1802. return ide_do_reset(drive);
  1803. }
  1804. #if IDETAPE_DEBUG_LOG
  1805. if (tape->debug_level >= 1)
  1806. printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
  1807. #endif
  1808. /* Retry operation */
  1809. return idetape_retry_pc(drive);
  1810. }
  1811. pc->error = 0;
  1812. if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
  1813. !status.b.dsc) {
  1814. /* Media access command */
  1815. tape->dsc_polling_start = jiffies;
  1816. tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
  1817. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  1818. /* Allow ide.c to handle other requests */
  1819. idetape_postpone_request(drive);
  1820. return ide_stopped;
  1821. }
  1822. if (tape->failed_pc == pc)
  1823. tape->failed_pc = NULL;
  1824. /* Command finished - Call the callback function */
  1825. return pc->callback(drive);
  1826. }
  1827. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1828. printk(KERN_ERR "ide-tape: The tape wants to issue more "
  1829. "interrupts in DMA mode\n");
  1830. printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
  1831. (void)__ide_dma_off(drive);
  1832. return ide_do_reset(drive);
  1833. }
  1834. /* Get the number of bytes to transfer on this interrupt. */
  1835. bcount.b.high = hwif->INB(IDE_BCOUNTH_REG);
  1836. bcount.b.low = hwif->INB(IDE_BCOUNTL_REG);
  1837. ireason.all = hwif->INB(IDE_IREASON_REG);
  1838. if (ireason.b.cod) {
  1839. printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
  1840. return ide_do_reset(drive);
  1841. }
  1842. if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
  1843. /* Hopefully, we will never get here */
  1844. printk(KERN_ERR "ide-tape: We wanted to %s, ",
  1845. ireason.b.io ? "Write":"Read");
  1846. printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
  1847. ireason.b.io ? "Read":"Write");
  1848. return ide_do_reset(drive);
  1849. }
  1850. if (!test_bit(PC_WRITING, &pc->flags)) {
  1851. /* Reading - Check that we have enough space */
  1852. temp = pc->actually_transferred + bcount.all;
  1853. if (temp > pc->request_transfer) {
  1854. if (temp > pc->buffer_size) {
  1855. printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
  1856. idetape_discard_data(drive, bcount.all);
  1857. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1858. return ide_started;
  1859. }
  1860. #if IDETAPE_DEBUG_LOG
  1861. if (tape->debug_level >= 2)
  1862. printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
  1863. #endif /* IDETAPE_DEBUG_LOG */
  1864. }
  1865. }
  1866. if (test_bit(PC_WRITING, &pc->flags)) {
  1867. if (pc->bh != NULL)
  1868. idetape_output_buffers(drive, pc, bcount.all);
  1869. else
  1870. /* Write the current buffer */
  1871. HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
  1872. } else {
  1873. if (pc->bh != NULL)
  1874. idetape_input_buffers(drive, pc, bcount.all);
  1875. else
  1876. /* Read the current buffer */
  1877. HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
  1878. }
  1879. /* Update the current position */
  1880. pc->actually_transferred += bcount.all;
  1881. pc->current_position += bcount.all;
  1882. #if IDETAPE_DEBUG_LOG
  1883. if (tape->debug_level >= 2)
  1884. printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all);
  1885. #endif
  1886. /* And set the interrupt handler again */
  1887. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1888. return ide_started;
  1889. }
  1890. /*
  1891. * Packet Command Interface
  1892. *
  1893. * The current Packet Command is available in tape->pc, and will not
  1894. * change until we finish handling it. Each packet command is associated
  1895. * with a callback function that will be called when the command is
  1896. * finished.
  1897. *
  1898. * The handling will be done in three stages:
  1899. *
  1900. * 1. idetape_issue_packet_command will send the packet command to the
  1901. * drive, and will set the interrupt handler to idetape_pc_intr.
  1902. *
  1903. * 2. On each interrupt, idetape_pc_intr will be called. This step
  1904. * will be repeated until the device signals us that no more
  1905. * interrupts will be issued.
  1906. *
  1907. * 3. ATAPI Tape media access commands have immediate status with a
  1908. * delayed process. In case of a successful initiation of a
  1909. * media access packet command, the DSC bit will be set when the
  1910. * actual execution of the command is finished.
  1911. * Since the tape drive will not issue an interrupt, we have to
  1912. * poll for this event. In this case, we define the request as
  1913. * "low priority request" by setting rq_status to
  1914. * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
  1915. * the driver.
  1916. *
  1917. * ide.c will then give higher priority to requests which
  1918. * originate from the other device, until will change rq_status
  1919. * to RQ_ACTIVE.
  1920. *
  1921. * 4. When the packet command is finished, it will be checked for errors.
  1922. *
  1923. * 5. In case an error was found, we queue a request sense packet
  1924. * command in front of the request queue and retry the operation
  1925. * up to IDETAPE_MAX_PC_RETRIES times.
  1926. *
  1927. * 6. In case no error was found, or we decided to give up and not
  1928. * to retry again, the callback function will be called and then
  1929. * we will handle the next request.
  1930. *
  1931. */
  1932. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  1933. {
  1934. ide_hwif_t *hwif = drive->hwif;
  1935. idetape_tape_t *tape = drive->driver_data;
  1936. idetape_pc_t *pc = tape->pc;
  1937. atapi_ireason_t ireason;
  1938. int retries = 100;
  1939. ide_startstop_t startstop;
  1940. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  1941. printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
  1942. return startstop;
  1943. }
  1944. ireason.all = hwif->INB(IDE_IREASON_REG);
  1945. while (retries-- && (!ireason.b.cod || ireason.b.io)) {
  1946. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
  1947. "a packet command, retrying\n");
  1948. udelay(100);
  1949. ireason.all = hwif->INB(IDE_IREASON_REG);
  1950. if (retries == 0) {
  1951. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
  1952. "issuing a packet command, ignoring\n");
  1953. ireason.b.cod = 1;
  1954. ireason.b.io = 0;
  1955. }
  1956. }
  1957. if (!ireason.b.cod || ireason.b.io) {
  1958. printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
  1959. "a packet command\n");
  1960. return ide_do_reset(drive);
  1961. }
  1962. /* Set the interrupt routine */
  1963. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1964. #ifdef CONFIG_BLK_DEV_IDEDMA
  1965. /* Begin DMA, if necessary */
  1966. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
  1967. hwif->dma_start(drive);
  1968. #endif
  1969. /* Send the actual packet */
  1970. HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
  1971. return ide_started;
  1972. }
  1973. static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
  1974. {
  1975. ide_hwif_t *hwif = drive->hwif;
  1976. idetape_tape_t *tape = drive->driver_data;
  1977. atapi_bcount_t bcount;
  1978. int dma_ok = 0;
  1979. #if IDETAPE_DEBUG_BUGS
  1980. if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
  1981. pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1982. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  1983. "Two request sense in serial were issued\n");
  1984. }
  1985. #endif /* IDETAPE_DEBUG_BUGS */
  1986. if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
  1987. tape->failed_pc = pc;
  1988. /* Set the current packet command */
  1989. tape->pc = pc;
  1990. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  1991. test_bit(PC_ABORT, &pc->flags)) {
  1992. /*
  1993. * We will "abort" retrying a packet command in case
  1994. * a legitimate error code was received (crossing a
  1995. * filemark, or end of the media, for example).
  1996. */
  1997. if (!test_bit(PC_ABORT, &pc->flags)) {
  1998. if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
  1999. tape->sense_key == 2 && tape->asc == 4 &&
  2000. (tape->ascq == 1 || tape->ascq == 8))) {
  2001. printk(KERN_ERR "ide-tape: %s: I/O error, "
  2002. "pc = %2x, key = %2x, "
  2003. "asc = %2x, ascq = %2x\n",
  2004. tape->name, pc->c[0],
  2005. tape->sense_key, tape->asc,
  2006. tape->ascq);
  2007. }
  2008. /* Giving up */
  2009. pc->error = IDETAPE_ERROR_GENERAL;
  2010. }
  2011. tape->failed_pc = NULL;
  2012. return pc->callback(drive);
  2013. }
  2014. #if IDETAPE_DEBUG_LOG
  2015. if (tape->debug_level >= 2)
  2016. printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
  2017. #endif /* IDETAPE_DEBUG_LOG */
  2018. pc->retries++;
  2019. /* We haven't transferred any data yet */
  2020. pc->actually_transferred = 0;
  2021. pc->current_position = pc->buffer;
  2022. /* Request to transfer the entire buffer at once */
  2023. bcount.all = pc->request_transfer;
  2024. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
  2025. printk(KERN_WARNING "ide-tape: DMA disabled, "
  2026. "reverting to PIO\n");
  2027. (void)__ide_dma_off(drive);
  2028. }
  2029. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  2030. dma_ok = !hwif->dma_setup(drive);
  2031. if (IDE_CONTROL_REG)
  2032. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  2033. hwif->OUTB(dma_ok ? 1 : 0, IDE_FEATURE_REG); /* Use PIO/DMA */
  2034. hwif->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
  2035. hwif->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
  2036. hwif->OUTB(drive->select.all, IDE_SELECT_REG);
  2037. if (dma_ok) /* Will begin DMA later */
  2038. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  2039. if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
  2040. ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
  2041. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  2042. return ide_started;
  2043. } else {
  2044. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  2045. return idetape_transfer_pc(drive);
  2046. }
  2047. }
  2048. /*
  2049. * General packet command callback function.
  2050. */
  2051. static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
  2052. {
  2053. idetape_tape_t *tape = drive->driver_data;
  2054. #if IDETAPE_DEBUG_LOG
  2055. if (tape->debug_level >= 4)
  2056. printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
  2057. #endif /* IDETAPE_DEBUG_LOG */
  2058. idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
  2059. return ide_stopped;
  2060. }
  2061. /*
  2062. * A mode sense command is used to "sense" tape parameters.
  2063. */
  2064. static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
  2065. {
  2066. idetape_init_pc(pc);
  2067. pc->c[0] = IDETAPE_MODE_SENSE_CMD;
  2068. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  2069. pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
  2070. pc->c[2] = page_code;
  2071. /*
  2072. * Changed pc->c[3] to 0 (255 will at best return unused info).
  2073. *
  2074. * For SCSI this byte is defined as subpage instead of high byte
  2075. * of length and some IDE drives seem to interpret it this way
  2076. * and return an error when 255 is used.
  2077. */
  2078. pc->c[3] = 0;
  2079. pc->c[4] = 255; /* (We will just discard data in that case) */
  2080. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  2081. pc->request_transfer = 12;
  2082. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  2083. pc->request_transfer = 24;
  2084. else
  2085. pc->request_transfer = 50;
  2086. pc->callback = &idetape_pc_callback;
  2087. }
  2088. static void calculate_speeds(ide_drive_t *drive)
  2089. {
  2090. idetape_tape_t *tape = drive->driver_data;
  2091. int full = 125, empty = 75;
  2092. if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
  2093. tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
  2094. tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
  2095. tape->controlled_last_pipeline_head = tape->pipeline_head;
  2096. tape->controlled_pipeline_head_time = jiffies;
  2097. }
  2098. if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
  2099. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
  2100. else if (time_after(jiffies, tape->controlled_previous_head_time))
  2101. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
  2102. if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
  2103. /* -1 for read mode error recovery */
  2104. if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
  2105. tape->uncontrolled_pipeline_head_time = jiffies;
  2106. tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
  2107. }
  2108. } else {
  2109. tape->uncontrolled_previous_head_time = jiffies;
  2110. tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
  2111. if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
  2112. tape->uncontrolled_pipeline_head_time = jiffies;
  2113. }
  2114. }
  2115. tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
  2116. if (tape->speed_control == 0) {
  2117. tape->max_insert_speed = 5000;
  2118. } else if (tape->speed_control == 1) {
  2119. if (tape->nr_pending_stages >= tape->max_stages / 2)
  2120. tape->max_insert_speed = tape->pipeline_head_speed +
  2121. (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
  2122. else
  2123. tape->max_insert_speed = 500 +
  2124. (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
  2125. if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
  2126. tape->max_insert_speed = 5000;
  2127. } else if (tape->speed_control == 2) {
  2128. tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
  2129. (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
  2130. } else
  2131. tape->max_insert_speed = tape->speed_control;
  2132. tape->max_insert_speed = max(tape->max_insert_speed, 500);
  2133. }
  2134. static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
  2135. {
  2136. idetape_tape_t *tape = drive->driver_data;
  2137. idetape_pc_t *pc = tape->pc;
  2138. atapi_status_t status;
  2139. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  2140. if (status.b.dsc) {
  2141. if (status.b.check) {
  2142. /* Error detected */
  2143. if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
  2144. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  2145. tape->name);
  2146. /* Retry operation */
  2147. return idetape_retry_pc(drive);
  2148. }
  2149. pc->error = 0;
  2150. if (tape->failed_pc == pc)
  2151. tape->failed_pc = NULL;
  2152. } else {
  2153. pc->error = IDETAPE_ERROR_GENERAL;
  2154. tape->failed_pc = NULL;
  2155. }
  2156. return pc->callback(drive);
  2157. }
  2158. static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
  2159. {
  2160. idetape_tape_t *tape = drive->driver_data;
  2161. struct request *rq = HWGROUP(drive)->rq;
  2162. int blocks = tape->pc->actually_transferred / tape->tape_block_size;
  2163. tape->avg_size += blocks * tape->tape_block_size;
  2164. tape->insert_size += blocks * tape->tape_block_size;
  2165. if (tape->insert_size > 1024 * 1024)
  2166. tape->measure_insert_time = 1;
  2167. if (tape->measure_insert_time) {
  2168. tape->measure_insert_time = 0;
  2169. tape->insert_time = jiffies;
  2170. tape->insert_size = 0;
  2171. }
  2172. if (time_after(jiffies, tape->insert_time))
  2173. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2174. if (jiffies - tape->avg_time >= HZ) {
  2175. tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
  2176. tape->avg_size = 0;
  2177. tape->avg_time = jiffies;
  2178. }
  2179. #if IDETAPE_DEBUG_LOG
  2180. if (tape->debug_level >= 4)
  2181. printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
  2182. #endif /* IDETAPE_DEBUG_LOG */
  2183. tape->first_frame_position += blocks;
  2184. rq->current_nr_sectors -= blocks;
  2185. if (!tape->pc->error)
  2186. idetape_end_request(drive, 1, 0);
  2187. else
  2188. idetape_end_request(drive, tape->pc->error, 0);
  2189. return ide_stopped;
  2190. }
  2191. static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2192. {
  2193. idetape_init_pc(pc);
  2194. pc->c[0] = IDETAPE_READ_CMD;
  2195. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  2196. pc->c[1] = 1;
  2197. pc->callback = &idetape_rw_callback;
  2198. pc->bh = bh;
  2199. atomic_set(&bh->b_count, 0);
  2200. pc->buffer = NULL;
  2201. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2202. if (pc->request_transfer == tape->stage_size)
  2203. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  2204. }
  2205. static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2206. {
  2207. int size = 32768;
  2208. struct idetape_bh *p = bh;
  2209. idetape_init_pc(pc);
  2210. pc->c[0] = IDETAPE_READ_BUFFER_CMD;
  2211. pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
  2212. pc->c[7] = size >> 8;
  2213. pc->c[8] = size & 0xff;
  2214. pc->callback = &idetape_pc_callback;
  2215. pc->bh = bh;
  2216. atomic_set(&bh->b_count, 0);
  2217. pc->buffer = NULL;
  2218. while (p) {
  2219. atomic_set(&p->b_count, 0);
  2220. p = p->b_reqnext;
  2221. }
  2222. pc->request_transfer = pc->buffer_size = size;
  2223. }
  2224. static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2225. {
  2226. idetape_init_pc(pc);
  2227. pc->c[0] = IDETAPE_WRITE_CMD;
  2228. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  2229. pc->c[1] = 1;
  2230. pc->callback = &idetape_rw_callback;
  2231. set_bit(PC_WRITING, &pc->flags);
  2232. pc->bh = bh;
  2233. pc->b_data = bh->b_data;
  2234. pc->b_count = atomic_read(&bh->b_count);
  2235. pc->buffer = NULL;
  2236. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2237. if (pc->request_transfer == tape->stage_size)
  2238. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  2239. }
  2240. /*
  2241. * idetape_do_request is our request handling function.
  2242. */
  2243. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  2244. struct request *rq, sector_t block)
  2245. {
  2246. idetape_tape_t *tape = drive->driver_data;
  2247. idetape_pc_t *pc = NULL;
  2248. struct request *postponed_rq = tape->postponed_rq;
  2249. atapi_status_t status;
  2250. #if IDETAPE_DEBUG_LOG
  2251. #if 0
  2252. if (tape->debug_level >= 5)
  2253. printk(KERN_INFO "ide-tape: rq_status: %d, "
  2254. "dev: %s, cmd: %ld, errors: %d\n", rq->rq_status,
  2255. rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
  2256. #endif
  2257. if (tape->debug_level >= 2)
  2258. printk(KERN_INFO "ide-tape: sector: %ld, "
  2259. "nr_sectors: %ld, current_nr_sectors: %d\n",
  2260. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  2261. #endif /* IDETAPE_DEBUG_LOG */
  2262. if ((rq->flags & REQ_SPECIAL) == 0) {
  2263. /*
  2264. * We do not support buffer cache originated requests.
  2265. */
  2266. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  2267. "request queue (%ld)\n", drive->name, rq->flags);
  2268. ide_end_request(drive, 0, 0);
  2269. return ide_stopped;
  2270. }
  2271. /*
  2272. * Retry a failed packet command
  2273. */
  2274. if (tape->failed_pc != NULL &&
  2275. tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  2276. return idetape_issue_packet_command(drive, tape->failed_pc);
  2277. }
  2278. #if IDETAPE_DEBUG_BUGS
  2279. if (postponed_rq != NULL)
  2280. if (rq != postponed_rq) {
  2281. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  2282. "Two DSC requests were queued\n");
  2283. idetape_end_request(drive, 0, 0);
  2284. return ide_stopped;
  2285. }
  2286. #endif /* IDETAPE_DEBUG_BUGS */
  2287. tape->postponed_rq = NULL;
  2288. /*
  2289. * If the tape is still busy, postpone our request and service
  2290. * the other device meanwhile.
  2291. */
  2292. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  2293. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  2294. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  2295. if (drive->post_reset == 1) {
  2296. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  2297. drive->post_reset = 0;
  2298. }
  2299. if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
  2300. tape->measure_insert_time = 1;
  2301. if (time_after(jiffies, tape->insert_time))
  2302. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2303. calculate_speeds(drive);
  2304. if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
  2305. !status.b.dsc) {
  2306. if (postponed_rq == NULL) {
  2307. tape->dsc_polling_start = jiffies;
  2308. tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
  2309. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  2310. } else if (time_after(jiffies, tape->dsc_timeout)) {
  2311. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  2312. tape->name);
  2313. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  2314. idetape_media_access_finished(drive);
  2315. return ide_stopped;
  2316. } else {
  2317. return ide_do_reset(drive);
  2318. }
  2319. } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
  2320. tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
  2321. idetape_postpone_request(drive);
  2322. return ide_stopped;
  2323. }
  2324. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  2325. tape->buffer_head++;
  2326. #if USE_IOTRACE
  2327. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2328. #endif
  2329. tape->postpone_cnt = 0;
  2330. pc = idetape_next_pc_storage(drive);
  2331. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2332. goto out;
  2333. }
  2334. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  2335. tape->buffer_head++;
  2336. #if USE_IOTRACE
  2337. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2338. #endif
  2339. tape->postpone_cnt = 0;
  2340. pc = idetape_next_pc_storage(drive);
  2341. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2342. goto out;
  2343. }
  2344. if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
  2345. tape->postpone_cnt = 0;
  2346. pc = idetape_next_pc_storage(drive);
  2347. idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2348. goto out;
  2349. }
  2350. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  2351. pc = (idetape_pc_t *) rq->buffer;
  2352. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  2353. rq->cmd[0] |= REQ_IDETAPE_PC2;
  2354. goto out;
  2355. }
  2356. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  2357. idetape_media_access_finished(drive);
  2358. return ide_stopped;
  2359. }
  2360. BUG();
  2361. out:
  2362. return idetape_issue_packet_command(drive, pc);
  2363. }
  2364. /*
  2365. * Pipeline related functions
  2366. */
  2367. static inline int idetape_pipeline_active (idetape_tape_t *tape)
  2368. {
  2369. int rc1, rc2;
  2370. rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2371. rc2 = (tape->active_data_request != NULL);
  2372. return rc1;
  2373. }
  2374. /*
  2375. * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
  2376. * stage, along with all the necessary small buffers which together make
  2377. * a buffer of size tape->stage_size (or a bit more). We attempt to
  2378. * combine sequential pages as much as possible.
  2379. *
  2380. * Returns a pointer to the new allocated stage, or NULL if we
  2381. * can't (or don't want to) allocate a stage.
  2382. *
  2383. * Pipeline stages are optional and are used to increase performance.
  2384. * If we can't allocate them, we'll manage without them.
  2385. */
  2386. static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
  2387. {
  2388. idetape_stage_t *stage;
  2389. struct idetape_bh *prev_bh, *bh;
  2390. int pages = tape->pages_per_stage;
  2391. char *b_data = NULL;
  2392. if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
  2393. return NULL;
  2394. stage->next = NULL;
  2395. bh = stage->bh = (struct idetape_bh *)kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  2396. if (bh == NULL)
  2397. goto abort;
  2398. bh->b_reqnext = NULL;
  2399. if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2400. goto abort;
  2401. if (clear)
  2402. memset(bh->b_data, 0, PAGE_SIZE);
  2403. bh->b_size = PAGE_SIZE;
  2404. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2405. while (--pages) {
  2406. if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2407. goto abort;
  2408. if (clear)
  2409. memset(b_data, 0, PAGE_SIZE);
  2410. if (bh->b_data == b_data + PAGE_SIZE) {
  2411. bh->b_size += PAGE_SIZE;
  2412. bh->b_data -= PAGE_SIZE;
  2413. if (full)
  2414. atomic_add(PAGE_SIZE, &bh->b_count);
  2415. continue;
  2416. }
  2417. if (b_data == bh->b_data + bh->b_size) {
  2418. bh->b_size += PAGE_SIZE;
  2419. if (full)
  2420. atomic_add(PAGE_SIZE, &bh->b_count);
  2421. continue;
  2422. }
  2423. prev_bh = bh;
  2424. if ((bh = (struct idetape_bh *)kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
  2425. free_page((unsigned long) b_data);
  2426. goto abort;
  2427. }
  2428. bh->b_reqnext = NULL;
  2429. bh->b_data = b_data;
  2430. bh->b_size = PAGE_SIZE;
  2431. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2432. prev_bh->b_reqnext = bh;
  2433. }
  2434. bh->b_size -= tape->excess_bh_size;
  2435. if (full)
  2436. atomic_sub(tape->excess_bh_size, &bh->b_count);
  2437. return stage;
  2438. abort:
  2439. __idetape_kfree_stage(stage);
  2440. return NULL;
  2441. }
  2442. static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
  2443. {
  2444. idetape_stage_t *cache_stage = tape->cache_stage;
  2445. #if IDETAPE_DEBUG_LOG
  2446. if (tape->debug_level >= 4)
  2447. printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
  2448. #endif /* IDETAPE_DEBUG_LOG */
  2449. if (tape->nr_stages >= tape->max_stages)
  2450. return NULL;
  2451. if (cache_stage != NULL) {
  2452. tape->cache_stage = NULL;
  2453. return cache_stage;
  2454. }
  2455. return __idetape_kmalloc_stage(tape, 0, 0);
  2456. }
  2457. static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
  2458. {
  2459. struct idetape_bh *bh = tape->bh;
  2460. int count;
  2461. while (n) {
  2462. #if IDETAPE_DEBUG_BUGS
  2463. if (bh == NULL) {
  2464. printk(KERN_ERR "ide-tape: bh == NULL in "
  2465. "idetape_copy_stage_from_user\n");
  2466. return;
  2467. }
  2468. #endif /* IDETAPE_DEBUG_BUGS */
  2469. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
  2470. copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count);
  2471. n -= count;
  2472. atomic_add(count, &bh->b_count);
  2473. buf += count;
  2474. if (atomic_read(&bh->b_count) == bh->b_size) {
  2475. bh = bh->b_reqnext;
  2476. if (bh)
  2477. atomic_set(&bh->b_count, 0);
  2478. }
  2479. }
  2480. tape->bh = bh;
  2481. }
  2482. static void idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
  2483. {
  2484. struct idetape_bh *bh = tape->bh;
  2485. int count;
  2486. while (n) {
  2487. #if IDETAPE_DEBUG_BUGS
  2488. if (bh == NULL) {
  2489. printk(KERN_ERR "ide-tape: bh == NULL in "
  2490. "idetape_copy_stage_to_user\n");
  2491. return;
  2492. }
  2493. #endif /* IDETAPE_DEBUG_BUGS */
  2494. count = min(tape->b_count, n);
  2495. copy_to_user(buf, tape->b_data, count);
  2496. n -= count;
  2497. tape->b_data += count;
  2498. tape->b_count -= count;
  2499. buf += count;
  2500. if (!tape->b_count) {
  2501. tape->bh = bh = bh->b_reqnext;
  2502. if (bh) {
  2503. tape->b_data = bh->b_data;
  2504. tape->b_count = atomic_read(&bh->b_count);
  2505. }
  2506. }
  2507. }
  2508. }
  2509. static void idetape_init_merge_stage (idetape_tape_t *tape)
  2510. {
  2511. struct idetape_bh *bh = tape->merge_stage->bh;
  2512. tape->bh = bh;
  2513. if (tape->chrdev_direction == idetape_direction_write)
  2514. atomic_set(&bh->b_count, 0);
  2515. else {
  2516. tape->b_data = bh->b_data;
  2517. tape->b_count = atomic_read(&bh->b_count);
  2518. }
  2519. }
  2520. static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
  2521. {
  2522. struct idetape_bh *tmp;
  2523. tmp = stage->bh;
  2524. stage->bh = tape->merge_stage->bh;
  2525. tape->merge_stage->bh = tmp;
  2526. idetape_init_merge_stage(tape);
  2527. }
  2528. /*
  2529. * idetape_add_stage_tail adds a new stage at the end of the pipeline.
  2530. */
  2531. static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
  2532. {
  2533. idetape_tape_t *tape = drive->driver_data;
  2534. unsigned long flags;
  2535. #if IDETAPE_DEBUG_LOG
  2536. if (tape->debug_level >= 4)
  2537. printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
  2538. #endif /* IDETAPE_DEBUG_LOG */
  2539. spin_lock_irqsave(&tape->spinlock, flags);
  2540. stage->next = NULL;
  2541. if (tape->last_stage != NULL)
  2542. tape->last_stage->next=stage;
  2543. else
  2544. tape->first_stage = tape->next_stage=stage;
  2545. tape->last_stage = stage;
  2546. if (tape->next_stage == NULL)
  2547. tape->next_stage = tape->last_stage;
  2548. tape->nr_stages++;
  2549. tape->nr_pending_stages++;
  2550. spin_unlock_irqrestore(&tape->spinlock, flags);
  2551. }
  2552. /*
  2553. * idetape_wait_for_request installs a completion in a pending request
  2554. * and sleeps until it is serviced.
  2555. *
  2556. * The caller should ensure that the request will not be serviced
  2557. * before we install the completion (usually by disabling interrupts).
  2558. */
  2559. static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
  2560. {
  2561. DECLARE_COMPLETION(wait);
  2562. idetape_tape_t *tape = drive->driver_data;
  2563. #if IDETAPE_DEBUG_BUGS
  2564. if (rq == NULL || (rq->flags & REQ_SPECIAL) == 0) {
  2565. printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
  2566. return;
  2567. }
  2568. #endif /* IDETAPE_DEBUG_BUGS */
  2569. rq->waiting = &wait;
  2570. rq->end_io = blk_end_sync_rq;
  2571. spin_unlock_irq(&tape->spinlock);
  2572. wait_for_completion(&wait);
  2573. /* The stage and its struct request have been deallocated */
  2574. spin_lock_irq(&tape->spinlock);
  2575. }
  2576. static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
  2577. {
  2578. idetape_tape_t *tape = drive->driver_data;
  2579. idetape_read_position_result_t *result;
  2580. #if IDETAPE_DEBUG_LOG
  2581. if (tape->debug_level >= 4)
  2582. printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
  2583. #endif /* IDETAPE_DEBUG_LOG */
  2584. if (!tape->pc->error) {
  2585. result = (idetape_read_position_result_t *) tape->pc->buffer;
  2586. #if IDETAPE_DEBUG_LOG
  2587. if (tape->debug_level >= 2)
  2588. printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
  2589. if (tape->debug_level >= 2)
  2590. printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
  2591. #endif /* IDETAPE_DEBUG_LOG */
  2592. if (result->bpu) {
  2593. printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
  2594. clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2595. idetape_end_request(drive, 0, 0);
  2596. } else {
  2597. #if IDETAPE_DEBUG_LOG
  2598. if (tape->debug_level >= 2)
  2599. printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
  2600. #endif /* IDETAPE_DEBUG_LOG */
  2601. tape->partition = result->partition;
  2602. tape->first_frame_position = ntohl(result->first_block);
  2603. tape->last_frame_position = ntohl(result->last_block);
  2604. tape->blocks_in_buffer = result->blocks_in_buffer[2];
  2605. set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2606. idetape_end_request(drive, 1, 0);
  2607. }
  2608. } else {
  2609. idetape_end_request(drive, 0, 0);
  2610. }
  2611. return ide_stopped;
  2612. }
  2613. /*
  2614. * idetape_create_write_filemark_cmd will:
  2615. *
  2616. * 1. Write a filemark if write_filemark=1.
  2617. * 2. Flush the device buffers without writing a filemark
  2618. * if write_filemark=0.
  2619. *
  2620. */
  2621. static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
  2622. {
  2623. idetape_init_pc(pc);
  2624. pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
  2625. pc->c[4] = write_filemark;
  2626. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2627. pc->callback = &idetape_pc_callback;
  2628. }
  2629. static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
  2630. {
  2631. idetape_init_pc(pc);
  2632. pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
  2633. pc->callback = &idetape_pc_callback;
  2634. }
  2635. /*
  2636. * idetape_queue_pc_tail is based on the following functions:
  2637. *
  2638. * ide_do_drive_cmd from ide.c
  2639. * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
  2640. *
  2641. * We add a special packet command request to the tail of the request
  2642. * queue, and wait for it to be serviced.
  2643. *
  2644. * This is not to be called from within the request handling part
  2645. * of the driver ! We allocate here data in the stack, and it is valid
  2646. * until the request is finished. This is not the case for the bottom
  2647. * part of the driver, where we are always leaving the functions to wait
  2648. * for an interrupt or a timer event.
  2649. *
  2650. * From the bottom part of the driver, we should allocate safe memory
  2651. * using idetape_next_pc_storage and idetape_next_rq_storage, and add
  2652. * the request to the request list without waiting for it to be serviced !
  2653. * In that case, we usually use idetape_queue_pc_head.
  2654. */
  2655. static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
  2656. {
  2657. struct ide_tape_obj *tape = drive->driver_data;
  2658. struct request rq;
  2659. idetape_init_rq(&rq, REQ_IDETAPE_PC1);
  2660. rq.buffer = (char *) pc;
  2661. rq.rq_disk = tape->disk;
  2662. return ide_do_drive_cmd(drive, &rq, ide_wait);
  2663. }
  2664. static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
  2665. {
  2666. idetape_init_pc(pc);
  2667. pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
  2668. pc->c[4] = cmd;
  2669. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2670. pc->callback = &idetape_pc_callback;
  2671. }
  2672. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  2673. {
  2674. idetape_tape_t *tape = drive->driver_data;
  2675. idetape_pc_t pc;
  2676. int load_attempted = 0;
  2677. /*
  2678. * Wait for the tape to become ready
  2679. */
  2680. set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  2681. timeout += jiffies;
  2682. while (time_before(jiffies, timeout)) {
  2683. idetape_create_test_unit_ready_cmd(&pc);
  2684. if (!__idetape_queue_pc_tail(drive, &pc))
  2685. return 0;
  2686. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  2687. || (tape->asc == 0x3A)) { /* no media */
  2688. if (load_attempted)
  2689. return -ENOMEDIUM;
  2690. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  2691. __idetape_queue_pc_tail(drive, &pc);
  2692. load_attempted = 1;
  2693. /* not about to be ready */
  2694. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  2695. (tape->ascq == 1 || tape->ascq == 8)))
  2696. return -EIO;
  2697. msleep(100);
  2698. }
  2699. return -EIO;
  2700. }
  2701. static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
  2702. {
  2703. return __idetape_queue_pc_tail(drive, pc);
  2704. }
  2705. static int idetape_flush_tape_buffers (ide_drive_t *drive)
  2706. {
  2707. idetape_pc_t pc;
  2708. int rc;
  2709. idetape_create_write_filemark_cmd(drive, &pc, 0);
  2710. if ((rc = idetape_queue_pc_tail(drive, &pc)))
  2711. return rc;
  2712. idetape_wait_ready(drive, 60 * 5 * HZ);
  2713. return 0;
  2714. }
  2715. static void idetape_create_read_position_cmd (idetape_pc_t *pc)
  2716. {
  2717. idetape_init_pc(pc);
  2718. pc->c[0] = IDETAPE_READ_POSITION_CMD;
  2719. pc->request_transfer = 20;
  2720. pc->callback = &idetape_read_position_callback;
  2721. }
  2722. static int idetape_read_position (ide_drive_t *drive)
  2723. {
  2724. idetape_tape_t *tape = drive->driver_data;
  2725. idetape_pc_t pc;
  2726. int position;
  2727. #if IDETAPE_DEBUG_LOG
  2728. if (tape->debug_level >= 4)
  2729. printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
  2730. #endif /* IDETAPE_DEBUG_LOG */
  2731. idetape_create_read_position_cmd(&pc);
  2732. if (idetape_queue_pc_tail(drive, &pc))
  2733. return -1;
  2734. position = tape->first_frame_position;
  2735. return position;
  2736. }
  2737. static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
  2738. {
  2739. idetape_init_pc(pc);
  2740. pc->c[0] = IDETAPE_LOCATE_CMD;
  2741. pc->c[1] = 2;
  2742. put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
  2743. pc->c[8] = partition;
  2744. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2745. pc->callback = &idetape_pc_callback;
  2746. }
  2747. static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
  2748. {
  2749. idetape_tape_t *tape = drive->driver_data;
  2750. if (!tape->capabilities.lock)
  2751. return 0;
  2752. idetape_init_pc(pc);
  2753. pc->c[0] = IDETAPE_PREVENT_CMD;
  2754. pc->c[4] = prevent;
  2755. pc->callback = &idetape_pc_callback;
  2756. return 1;
  2757. }
  2758. static int __idetape_discard_read_pipeline (ide_drive_t *drive)
  2759. {
  2760. idetape_tape_t *tape = drive->driver_data;
  2761. unsigned long flags;
  2762. int cnt;
  2763. if (tape->chrdev_direction != idetape_direction_read)
  2764. return 0;
  2765. /* Remove merge stage. */
  2766. cnt = tape->merge_stage_size / tape->tape_block_size;
  2767. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  2768. ++cnt; /* Filemarks count as 1 sector */
  2769. tape->merge_stage_size = 0;
  2770. if (tape->merge_stage != NULL) {
  2771. __idetape_kfree_stage(tape->merge_stage);
  2772. tape->merge_stage = NULL;
  2773. }
  2774. /* Clear pipeline flags. */
  2775. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2776. tape->chrdev_direction = idetape_direction_none;
  2777. /* Remove pipeline stages. */
  2778. if (tape->first_stage == NULL)
  2779. return 0;
  2780. spin_lock_irqsave(&tape->spinlock, flags);
  2781. tape->next_stage = NULL;
  2782. if (idetape_pipeline_active(tape))
  2783. idetape_wait_for_request(drive, tape->active_data_request);
  2784. spin_unlock_irqrestore(&tape->spinlock, flags);
  2785. while (tape->first_stage != NULL) {
  2786. struct request *rq_ptr = &tape->first_stage->rq;
  2787. cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
  2788. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  2789. ++cnt;
  2790. idetape_remove_stage_head(drive);
  2791. }
  2792. tape->nr_pending_stages = 0;
  2793. tape->max_stages = tape->min_pipeline;
  2794. return cnt;
  2795. }
  2796. /*
  2797. * idetape_position_tape positions the tape to the requested block
  2798. * using the LOCATE packet command. A READ POSITION command is then
  2799. * issued to check where we are positioned.
  2800. *
  2801. * Like all higher level operations, we queue the commands at the tail
  2802. * of the request queue and wait for their completion.
  2803. *
  2804. */
  2805. static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
  2806. {
  2807. idetape_tape_t *tape = drive->driver_data;
  2808. int retval;
  2809. idetape_pc_t pc;
  2810. if (tape->chrdev_direction == idetape_direction_read)
  2811. __idetape_discard_read_pipeline(drive);
  2812. idetape_wait_ready(drive, 60 * 5 * HZ);
  2813. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  2814. retval = idetape_queue_pc_tail(drive, &pc);
  2815. if (retval)
  2816. return (retval);
  2817. idetape_create_read_position_cmd(&pc);
  2818. return (idetape_queue_pc_tail(drive, &pc));
  2819. }
  2820. static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
  2821. {
  2822. idetape_tape_t *tape = drive->driver_data;
  2823. int cnt;
  2824. int seek, position;
  2825. cnt = __idetape_discard_read_pipeline(drive);
  2826. if (restore_position) {
  2827. position = idetape_read_position(drive);
  2828. seek = position > cnt ? position - cnt : 0;
  2829. if (idetape_position_tape(drive, seek, 0, 0)) {
  2830. printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
  2831. return;
  2832. }
  2833. }
  2834. }
  2835. /*
  2836. * idetape_queue_rw_tail generates a read/write request for the block
  2837. * device interface and wait for it to be serviced.
  2838. */
  2839. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
  2840. {
  2841. idetape_tape_t *tape = drive->driver_data;
  2842. struct request rq;
  2843. #if IDETAPE_DEBUG_LOG
  2844. if (tape->debug_level >= 2)
  2845. printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
  2846. #endif /* IDETAPE_DEBUG_LOG */
  2847. #if IDETAPE_DEBUG_BUGS
  2848. if (idetape_pipeline_active(tape)) {
  2849. printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
  2850. return (0);
  2851. }
  2852. #endif /* IDETAPE_DEBUG_BUGS */
  2853. idetape_init_rq(&rq, cmd);
  2854. rq.rq_disk = tape->disk;
  2855. rq.special = (void *)bh;
  2856. rq.sector = tape->first_frame_position;
  2857. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2858. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  2859. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  2860. return 0;
  2861. if (tape->merge_stage)
  2862. idetape_init_merge_stage(tape);
  2863. if (rq.errors == IDETAPE_ERROR_GENERAL)
  2864. return -EIO;
  2865. return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
  2866. }
  2867. /*
  2868. * idetape_insert_pipeline_into_queue is used to start servicing the
  2869. * pipeline stages, starting from tape->next_stage.
  2870. */
  2871. static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
  2872. {
  2873. idetape_tape_t *tape = drive->driver_data;
  2874. if (tape->next_stage == NULL)
  2875. return;
  2876. if (!idetape_pipeline_active(tape)) {
  2877. set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2878. idetape_active_next_stage(drive);
  2879. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  2880. }
  2881. }
  2882. static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
  2883. {
  2884. idetape_init_pc(pc);
  2885. pc->c[0] = IDETAPE_INQUIRY_CMD;
  2886. pc->c[4] = pc->request_transfer = 254;
  2887. pc->callback = &idetape_pc_callback;
  2888. }
  2889. static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
  2890. {
  2891. idetape_init_pc(pc);
  2892. pc->c[0] = IDETAPE_REWIND_CMD;
  2893. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2894. pc->callback = &idetape_pc_callback;
  2895. }
  2896. #if 0
  2897. static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
  2898. {
  2899. idetape_init_pc(pc);
  2900. set_bit(PC_WRITING, &pc->flags);
  2901. pc->c[0] = IDETAPE_MODE_SELECT_CMD;
  2902. pc->c[1] = 0x10;
  2903. put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
  2904. pc->request_transfer = 255;
  2905. pc->callback = &idetape_pc_callback;
  2906. }
  2907. #endif
  2908. static void idetape_create_erase_cmd (idetape_pc_t *pc)
  2909. {
  2910. idetape_init_pc(pc);
  2911. pc->c[0] = IDETAPE_ERASE_CMD;
  2912. pc->c[1] = 1;
  2913. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2914. pc->callback = &idetape_pc_callback;
  2915. }
  2916. static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
  2917. {
  2918. idetape_init_pc(pc);
  2919. pc->c[0] = IDETAPE_SPACE_CMD;
  2920. put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
  2921. pc->c[1] = cmd;
  2922. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2923. pc->callback = &idetape_pc_callback;
  2924. }
  2925. static void idetape_wait_first_stage (ide_drive_t *drive)
  2926. {
  2927. idetape_tape_t *tape = drive->driver_data;
  2928. unsigned long flags;
  2929. if (tape->first_stage == NULL)
  2930. return;
  2931. spin_lock_irqsave(&tape->spinlock, flags);
  2932. if (tape->active_stage == tape->first_stage)
  2933. idetape_wait_for_request(drive, tape->active_data_request);
  2934. spin_unlock_irqrestore(&tape->spinlock, flags);
  2935. }
  2936. /*
  2937. * idetape_add_chrdev_write_request tries to add a character device
  2938. * originated write request to our pipeline. In case we don't succeed,
  2939. * we revert to non-pipelined operation mode for this request.
  2940. *
  2941. * 1. Try to allocate a new pipeline stage.
  2942. * 2. If we can't, wait for more and more requests to be serviced
  2943. * and try again each time.
  2944. * 3. If we still can't allocate a stage, fallback to
  2945. * non-pipelined operation mode for this request.
  2946. */
  2947. static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
  2948. {
  2949. idetape_tape_t *tape = drive->driver_data;
  2950. idetape_stage_t *new_stage;
  2951. unsigned long flags;
  2952. struct request *rq;
  2953. #if IDETAPE_DEBUG_LOG
  2954. if (tape->debug_level >= 3)
  2955. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
  2956. #endif /* IDETAPE_DEBUG_LOG */
  2957. /*
  2958. * Attempt to allocate a new stage.
  2959. * Pay special attention to possible race conditions.
  2960. */
  2961. while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
  2962. spin_lock_irqsave(&tape->spinlock, flags);
  2963. if (idetape_pipeline_active(tape)) {
  2964. idetape_wait_for_request(drive, tape->active_data_request);
  2965. spin_unlock_irqrestore(&tape->spinlock, flags);
  2966. } else {
  2967. spin_unlock_irqrestore(&tape->spinlock, flags);
  2968. idetape_insert_pipeline_into_queue(drive);
  2969. if (idetape_pipeline_active(tape))
  2970. continue;
  2971. /*
  2972. * Linux is short on memory. Fallback to
  2973. * non-pipelined operation mode for this request.
  2974. */
  2975. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2976. }
  2977. }
  2978. rq = &new_stage->rq;
  2979. idetape_init_rq(rq, REQ_IDETAPE_WRITE);
  2980. /* Doesn't actually matter - We always assume sequential access */
  2981. rq->sector = tape->first_frame_position;
  2982. rq->nr_sectors = rq->current_nr_sectors = blocks;
  2983. idetape_switch_buffers(tape, new_stage);
  2984. idetape_add_stage_tail(drive, new_stage);
  2985. tape->pipeline_head++;
  2986. #if USE_IOTRACE
  2987. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2988. #endif
  2989. calculate_speeds(drive);
  2990. /*
  2991. * Estimate whether the tape has stopped writing by checking
  2992. * if our write pipeline is currently empty. If we are not
  2993. * writing anymore, wait for the pipeline to be full enough
  2994. * (90%) before starting to service requests, so that we will
  2995. * be able to keep up with the higher speeds of the tape.
  2996. */
  2997. if (!idetape_pipeline_active(tape)) {
  2998. if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
  2999. tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
  3000. tape->measure_insert_time = 1;
  3001. tape->insert_time = jiffies;
  3002. tape->insert_size = 0;
  3003. tape->insert_speed = 0;
  3004. idetape_insert_pipeline_into_queue(drive);
  3005. }
  3006. }
  3007. if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  3008. /* Return a deferred error */
  3009. return -EIO;
  3010. return blocks;
  3011. }
  3012. /*
  3013. * idetape_wait_for_pipeline will wait until all pending pipeline
  3014. * requests are serviced. Typically called on device close.
  3015. */
  3016. static void idetape_wait_for_pipeline (ide_drive_t *drive)
  3017. {
  3018. idetape_tape_t *tape = drive->driver_data;
  3019. unsigned long flags;
  3020. while (tape->next_stage || idetape_pipeline_active(tape)) {
  3021. idetape_insert_pipeline_into_queue(drive);
  3022. spin_lock_irqsave(&tape->spinlock, flags);
  3023. if (idetape_pipeline_active(tape))
  3024. idetape_wait_for_request(drive, tape->active_data_request);
  3025. spin_unlock_irqrestore(&tape->spinlock, flags);
  3026. }
  3027. }
  3028. static void idetape_empty_write_pipeline (ide_drive_t *drive)
  3029. {
  3030. idetape_tape_t *tape = drive->driver_data;
  3031. int blocks, min;
  3032. struct idetape_bh *bh;
  3033. #if IDETAPE_DEBUG_BUGS
  3034. if (tape->chrdev_direction != idetape_direction_write) {
  3035. printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
  3036. return;
  3037. }
  3038. if (tape->merge_stage_size > tape->stage_size) {
  3039. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  3040. tape->merge_stage_size = tape->stage_size;
  3041. }
  3042. #endif /* IDETAPE_DEBUG_BUGS */
  3043. if (tape->merge_stage_size) {
  3044. blocks = tape->merge_stage_size / tape->tape_block_size;
  3045. if (tape->merge_stage_size % tape->tape_block_size) {
  3046. unsigned int i;
  3047. blocks++;
  3048. i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
  3049. bh = tape->bh->b_reqnext;
  3050. while (bh) {
  3051. atomic_set(&bh->b_count, 0);
  3052. bh = bh->b_reqnext;
  3053. }
  3054. bh = tape->bh;
  3055. while (i) {
  3056. if (bh == NULL) {
  3057. printk(KERN_INFO "ide-tape: bug, bh NULL\n");
  3058. break;
  3059. }
  3060. min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
  3061. memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
  3062. atomic_add(min, &bh->b_count);
  3063. i -= min;
  3064. bh = bh->b_reqnext;
  3065. }
  3066. }
  3067. (void) idetape_add_chrdev_write_request(drive, blocks);
  3068. tape->merge_stage_size = 0;
  3069. }
  3070. idetape_wait_for_pipeline(drive);
  3071. if (tape->merge_stage != NULL) {
  3072. __idetape_kfree_stage(tape->merge_stage);
  3073. tape->merge_stage = NULL;
  3074. }
  3075. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  3076. tape->chrdev_direction = idetape_direction_none;
  3077. /*
  3078. * On the next backup, perform the feedback loop again.
  3079. * (I don't want to keep sense information between backups,
  3080. * as some systems are constantly on, and the system load
  3081. * can be totally different on the next backup).
  3082. */
  3083. tape->max_stages = tape->min_pipeline;
  3084. #if IDETAPE_DEBUG_BUGS
  3085. if (tape->first_stage != NULL ||
  3086. tape->next_stage != NULL ||
  3087. tape->last_stage != NULL ||
  3088. tape->nr_stages != 0) {
  3089. printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
  3090. "first_stage %p, next_stage %p, "
  3091. "last_stage %p, nr_stages %d\n",
  3092. tape->first_stage, tape->next_stage,
  3093. tape->last_stage, tape->nr_stages);
  3094. }
  3095. #endif /* IDETAPE_DEBUG_BUGS */
  3096. }
  3097. static void idetape_restart_speed_control (ide_drive_t *drive)
  3098. {
  3099. idetape_tape_t *tape = drive->driver_data;
  3100. tape->restart_speed_control_req = 0;
  3101. tape->pipeline_head = 0;
  3102. tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
  3103. tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
  3104. tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
  3105. tape->uncontrolled_pipeline_head_speed = 0;
  3106. tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
  3107. tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
  3108. }
  3109. static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
  3110. {
  3111. idetape_tape_t *tape = drive->driver_data;
  3112. idetape_stage_t *new_stage;
  3113. struct request rq;
  3114. int bytes_read;
  3115. int blocks = tape->capabilities.ctl;
  3116. /* Initialize read operation */
  3117. if (tape->chrdev_direction != idetape_direction_read) {
  3118. if (tape->chrdev_direction == idetape_direction_write) {
  3119. idetape_empty_write_pipeline(drive);
  3120. idetape_flush_tape_buffers(drive);
  3121. }
  3122. #if IDETAPE_DEBUG_BUGS
  3123. if (tape->merge_stage || tape->merge_stage_size) {
  3124. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
  3125. tape->merge_stage_size = 0;
  3126. }
  3127. #endif /* IDETAPE_DEBUG_BUGS */
  3128. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  3129. return -ENOMEM;
  3130. tape->chrdev_direction = idetape_direction_read;
  3131. /*
  3132. * Issue a read 0 command to ensure that DSC handshake
  3133. * is switched from completion mode to buffer available
  3134. * mode.
  3135. * No point in issuing this if DSC overlap isn't supported,
  3136. * some drives (Seagate STT3401A) will return an error.
  3137. */
  3138. if (drive->dsc_overlap) {
  3139. bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
  3140. if (bytes_read < 0) {
  3141. __idetape_kfree_stage(tape->merge_stage);
  3142. tape->merge_stage = NULL;
  3143. tape->chrdev_direction = idetape_direction_none;
  3144. return bytes_read;
  3145. }
  3146. }
  3147. }
  3148. if (tape->restart_speed_control_req)
  3149. idetape_restart_speed_control(drive);
  3150. idetape_init_rq(&rq, REQ_IDETAPE_READ);
  3151. rq.sector = tape->first_frame_position;
  3152. rq.nr_sectors = rq.current_nr_sectors = blocks;
  3153. if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
  3154. tape->nr_stages < max_stages) {
  3155. new_stage = idetape_kmalloc_stage(tape);
  3156. while (new_stage != NULL) {
  3157. new_stage->rq = rq;
  3158. idetape_add_stage_tail(drive, new_stage);
  3159. if (tape->nr_stages >= max_stages)
  3160. break;
  3161. new_stage = idetape_kmalloc_stage(tape);
  3162. }
  3163. }
  3164. if (!idetape_pipeline_active(tape)) {
  3165. if (tape->nr_pending_stages >= 3 * max_stages / 4) {
  3166. tape->measure_insert_time = 1;
  3167. tape->insert_time = jiffies;
  3168. tape->insert_size = 0;
  3169. tape->insert_speed = 0;
  3170. idetape_insert_pipeline_into_queue(drive);
  3171. }
  3172. }
  3173. return 0;
  3174. }
  3175. /*
  3176. * idetape_add_chrdev_read_request is called from idetape_chrdev_read
  3177. * to service a character device read request and add read-ahead
  3178. * requests to our pipeline.
  3179. */
  3180. static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
  3181. {
  3182. idetape_tape_t *tape = drive->driver_data;
  3183. unsigned long flags;
  3184. struct request *rq_ptr;
  3185. int bytes_read;
  3186. #if IDETAPE_DEBUG_LOG
  3187. if (tape->debug_level >= 4)
  3188. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
  3189. #endif /* IDETAPE_DEBUG_LOG */
  3190. /*
  3191. * If we are at a filemark, return a read length of 0
  3192. */
  3193. if (test_bit(IDETAPE_FILEMARK, &tape->flags))
  3194. return 0;
  3195. /*
  3196. * Wait for the next block to be available at the head
  3197. * of the pipeline
  3198. */
  3199. idetape_initiate_read(drive, tape->max_stages);
  3200. if (tape->first_stage == NULL) {
  3201. if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  3202. return 0;
  3203. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
  3204. }
  3205. idetape_wait_first_stage(drive);
  3206. rq_ptr = &tape->first_stage->rq;
  3207. bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
  3208. rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
  3209. if (rq_ptr->errors == IDETAPE_ERROR_EOD)
  3210. return 0;
  3211. else {
  3212. idetape_switch_buffers(tape, tape->first_stage);
  3213. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  3214. set_bit(IDETAPE_FILEMARK, &tape->flags);
  3215. spin_lock_irqsave(&tape->spinlock, flags);
  3216. idetape_remove_stage_head(drive);
  3217. spin_unlock_irqrestore(&tape->spinlock, flags);
  3218. tape->pipeline_head++;
  3219. #if USE_IOTRACE
  3220. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  3221. #endif
  3222. calculate_speeds(drive);
  3223. }
  3224. #if IDETAPE_DEBUG_BUGS
  3225. if (bytes_read > blocks * tape->tape_block_size) {
  3226. printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
  3227. bytes_read = blocks * tape->tape_block_size;
  3228. }
  3229. #endif /* IDETAPE_DEBUG_BUGS */
  3230. return (bytes_read);
  3231. }
  3232. static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
  3233. {
  3234. idetape_tape_t *tape = drive->driver_data;
  3235. struct idetape_bh *bh;
  3236. int blocks;
  3237. while (bcount) {
  3238. unsigned int count;
  3239. bh = tape->merge_stage->bh;
  3240. count = min(tape->stage_size, bcount);
  3241. bcount -= count;
  3242. blocks = count / tape->tape_block_size;
  3243. while (count) {
  3244. atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
  3245. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  3246. count -= atomic_read(&bh->b_count);
  3247. bh = bh->b_reqnext;
  3248. }
  3249. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  3250. }
  3251. }
  3252. static int idetape_pipeline_size (ide_drive_t *drive)
  3253. {
  3254. idetape_tape_t *tape = drive->driver_data;
  3255. idetape_stage_t *stage;
  3256. struct request *rq;
  3257. int size = 0;
  3258. idetape_wait_for_pipeline(drive);
  3259. stage = tape->first_stage;
  3260. while (stage != NULL) {
  3261. rq = &stage->rq;
  3262. size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
  3263. if (rq->errors == IDETAPE_ERROR_FILEMARK)
  3264. size += tape->tape_block_size;
  3265. stage = stage->next;
  3266. }
  3267. size += tape->merge_stage_size;
  3268. return size;
  3269. }
  3270. /*
  3271. * Rewinds the tape to the Beginning Of the current Partition (BOP).
  3272. *
  3273. * We currently support only one partition.
  3274. */
  3275. static int idetape_rewind_tape (ide_drive_t *drive)
  3276. {
  3277. int retval;
  3278. idetape_pc_t pc;
  3279. #if IDETAPE_DEBUG_LOG
  3280. idetape_tape_t *tape = drive->driver_data;
  3281. if (tape->debug_level >= 2)
  3282. printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
  3283. #endif /* IDETAPE_DEBUG_LOG */
  3284. idetape_create_rewind_cmd(drive, &pc);
  3285. retval = idetape_queue_pc_tail(drive, &pc);
  3286. if (retval)
  3287. return retval;
  3288. idetape_create_read_position_cmd(&pc);
  3289. retval = idetape_queue_pc_tail(drive, &pc);
  3290. if (retval)
  3291. return retval;
  3292. return 0;
  3293. }
  3294. /*
  3295. * Our special ide-tape ioctl's.
  3296. *
  3297. * Currently there aren't any ioctl's.
  3298. * mtio.h compatible commands should be issued to the character device
  3299. * interface.
  3300. */
  3301. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  3302. {
  3303. idetape_tape_t *tape = drive->driver_data;
  3304. idetape_config_t config;
  3305. void __user *argp = (void __user *)arg;
  3306. #if IDETAPE_DEBUG_LOG
  3307. if (tape->debug_level >= 4)
  3308. printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
  3309. #endif /* IDETAPE_DEBUG_LOG */
  3310. switch (cmd) {
  3311. case 0x0340:
  3312. if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
  3313. return -EFAULT;
  3314. tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
  3315. tape->max_stages = config.nr_stages;
  3316. break;
  3317. case 0x0350:
  3318. config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
  3319. config.nr_stages = tape->max_stages;
  3320. if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
  3321. return -EFAULT;
  3322. break;
  3323. default:
  3324. return -EIO;
  3325. }
  3326. return 0;
  3327. }
  3328. /*
  3329. * idetape_space_over_filemarks is now a bit more complicated than just
  3330. * passing the command to the tape since we may have crossed some
  3331. * filemarks during our pipelined read-ahead mode.
  3332. *
  3333. * As a minor side effect, the pipeline enables us to support MTFSFM when
  3334. * the filemark is in our internal pipeline even if the tape doesn't
  3335. * support spacing over filemarks in the reverse direction.
  3336. */
  3337. static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
  3338. {
  3339. idetape_tape_t *tape = drive->driver_data;
  3340. idetape_pc_t pc;
  3341. unsigned long flags;
  3342. int retval,count=0;
  3343. if (mt_count == 0)
  3344. return 0;
  3345. if (MTBSF == mt_op || MTBSFM == mt_op) {
  3346. if (!tape->capabilities.sprev)
  3347. return -EIO;
  3348. mt_count = - mt_count;
  3349. }
  3350. if (tape->chrdev_direction == idetape_direction_read) {
  3351. /*
  3352. * We have a read-ahead buffer. Scan it for crossed
  3353. * filemarks.
  3354. */
  3355. tape->merge_stage_size = 0;
  3356. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  3357. ++count;
  3358. while (tape->first_stage != NULL) {
  3359. if (count == mt_count) {
  3360. if (mt_op == MTFSFM)
  3361. set_bit(IDETAPE_FILEMARK, &tape->flags);
  3362. return 0;
  3363. }
  3364. spin_lock_irqsave(&tape->spinlock, flags);
  3365. if (tape->first_stage == tape->active_stage) {
  3366. /*
  3367. * We have reached the active stage in the read pipeline.
  3368. * There is no point in allowing the drive to continue
  3369. * reading any farther, so we stop the pipeline.
  3370. *
  3371. * This section should be moved to a separate subroutine,
  3372. * because a similar function is performed in
  3373. * __idetape_discard_read_pipeline(), for example.
  3374. */
  3375. tape->next_stage = NULL;
  3376. spin_unlock_irqrestore(&tape->spinlock, flags);
  3377. idetape_wait_first_stage(drive);
  3378. tape->next_stage = tape->first_stage->next;
  3379. } else
  3380. spin_unlock_irqrestore(&tape->spinlock, flags);
  3381. if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
  3382. ++count;
  3383. idetape_remove_stage_head(drive);
  3384. }
  3385. idetape_discard_read_pipeline(drive, 0);
  3386. }
  3387. /*
  3388. * The filemark was not found in our internal pipeline.
  3389. * Now we can issue the space command.
  3390. */
  3391. switch (mt_op) {
  3392. case MTFSF:
  3393. case MTBSF:
  3394. idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
  3395. return (idetape_queue_pc_tail(drive, &pc));
  3396. case MTFSFM:
  3397. case MTBSFM:
  3398. if (!tape->capabilities.sprev)
  3399. return (-EIO);
  3400. retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
  3401. if (retval) return (retval);
  3402. count = (MTBSFM == mt_op ? 1 : -1);
  3403. return (idetape_space_over_filemarks(drive, MTFSF, count));
  3404. default:
  3405. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
  3406. return (-EIO);
  3407. }
  3408. }
  3409. /*
  3410. * Our character device read / write functions.
  3411. *
  3412. * The tape is optimized to maximize throughput when it is transferring
  3413. * an integral number of the "continuous transfer limit", which is
  3414. * a parameter of the specific tape (26 KB on my particular tape).
  3415. * (32 kB for Onstream)
  3416. *
  3417. * As of version 1.3 of the driver, the character device provides an
  3418. * abstract continuous view of the media - any mix of block sizes (even 1
  3419. * byte) on the same backup/restore procedure is supported. The driver
  3420. * will internally convert the requests to the recommended transfer unit,
  3421. * so that an unmatch between the user's block size to the recommended
  3422. * size will only result in a (slightly) increased driver overhead, but
  3423. * will no longer hit performance.
  3424. * This is not applicable to Onstream.
  3425. */
  3426. static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
  3427. size_t count, loff_t *ppos)
  3428. {
  3429. struct ide_tape_obj *tape = ide_tape_f(file);
  3430. ide_drive_t *drive = tape->drive;
  3431. ssize_t bytes_read,temp, actually_read = 0, rc;
  3432. #if IDETAPE_DEBUG_LOG
  3433. if (tape->debug_level >= 3)
  3434. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
  3435. #endif /* IDETAPE_DEBUG_LOG */
  3436. if (tape->chrdev_direction != idetape_direction_read) {
  3437. if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
  3438. if (count > tape->tape_block_size &&
  3439. (count % tape->tape_block_size) == 0)
  3440. tape->user_bs_factor = count / tape->tape_block_size;
  3441. }
  3442. if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
  3443. return rc;
  3444. if (count == 0)
  3445. return (0);
  3446. if (tape->merge_stage_size) {
  3447. actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
  3448. idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read);
  3449. buf += actually_read;
  3450. tape->merge_stage_size -= actually_read;
  3451. count -= actually_read;
  3452. }
  3453. while (count >= tape->stage_size) {
  3454. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  3455. if (bytes_read <= 0)
  3456. goto finish;
  3457. idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read);
  3458. buf += bytes_read;
  3459. count -= bytes_read;
  3460. actually_read += bytes_read;
  3461. }
  3462. if (count) {
  3463. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  3464. if (bytes_read <= 0)
  3465. goto finish;
  3466. temp = min((unsigned long)count, (unsigned long)bytes_read);
  3467. idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp);
  3468. actually_read += temp;
  3469. tape->merge_stage_size = bytes_read-temp;
  3470. }
  3471. finish:
  3472. if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
  3473. #if IDETAPE_DEBUG_LOG
  3474. if (tape->debug_level >= 2)
  3475. printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
  3476. #endif
  3477. idetape_space_over_filemarks(drive, MTFSF, 1);
  3478. return 0;
  3479. }
  3480. return actually_read;
  3481. }
  3482. static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
  3483. size_t count, loff_t *ppos)
  3484. {
  3485. struct ide_tape_obj *tape = ide_tape_f(file);
  3486. ide_drive_t *drive = tape->drive;
  3487. ssize_t retval, actually_written = 0;
  3488. /* The drive is write protected. */
  3489. if (tape->write_prot)
  3490. return -EACCES;
  3491. #if IDETAPE_DEBUG_LOG
  3492. if (tape->debug_level >= 3)
  3493. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
  3494. "count %Zd\n", count);
  3495. #endif /* IDETAPE_DEBUG_LOG */
  3496. /* Initialize write operation */
  3497. if (tape->chrdev_direction != idetape_direction_write) {
  3498. if (tape->chrdev_direction == idetape_direction_read)
  3499. idetape_discard_read_pipeline(drive, 1);
  3500. #if IDETAPE_DEBUG_BUGS
  3501. if (tape->merge_stage || tape->merge_stage_size) {
  3502. printk(KERN_ERR "ide-tape: merge_stage_size "
  3503. "should be 0 now\n");
  3504. tape->merge_stage_size = 0;
  3505. }
  3506. #endif /* IDETAPE_DEBUG_BUGS */
  3507. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  3508. return -ENOMEM;
  3509. tape->chrdev_direction = idetape_direction_write;
  3510. idetape_init_merge_stage(tape);
  3511. /*
  3512. * Issue a write 0 command to ensure that DSC handshake
  3513. * is switched from completion mode to buffer available
  3514. * mode.
  3515. * No point in issuing this if DSC overlap isn't supported,
  3516. * some drives (Seagate STT3401A) will return an error.
  3517. */
  3518. if (drive->dsc_overlap) {
  3519. retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
  3520. if (retval < 0) {
  3521. __idetape_kfree_stage(tape->merge_stage);
  3522. tape->merge_stage = NULL;
  3523. tape->chrdev_direction = idetape_direction_none;
  3524. return retval;
  3525. }
  3526. }
  3527. }
  3528. if (count == 0)
  3529. return (0);
  3530. if (tape->restart_speed_control_req)
  3531. idetape_restart_speed_control(drive);
  3532. if (tape->merge_stage_size) {
  3533. #if IDETAPE_DEBUG_BUGS
  3534. if (tape->merge_stage_size >= tape->stage_size) {
  3535. printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
  3536. tape->merge_stage_size = 0;
  3537. }
  3538. #endif /* IDETAPE_DEBUG_BUGS */
  3539. actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
  3540. idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written);
  3541. buf += actually_written;
  3542. tape->merge_stage_size += actually_written;
  3543. count -= actually_written;
  3544. if (tape->merge_stage_size == tape->stage_size) {
  3545. tape->merge_stage_size = 0;
  3546. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3547. if (retval <= 0)
  3548. return (retval);
  3549. }
  3550. }
  3551. while (count >= tape->stage_size) {
  3552. idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size);
  3553. buf += tape->stage_size;
  3554. count -= tape->stage_size;
  3555. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3556. actually_written += tape->stage_size;
  3557. if (retval <= 0)
  3558. return (retval);
  3559. }
  3560. if (count) {
  3561. actually_written += count;
  3562. idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count);
  3563. tape->merge_stage_size += count;
  3564. }
  3565. return (actually_written);
  3566. }
  3567. static int idetape_write_filemark (ide_drive_t *drive)
  3568. {
  3569. idetape_pc_t pc;
  3570. /* Write a filemark */
  3571. idetape_create_write_filemark_cmd(drive, &pc, 1);
  3572. if (idetape_queue_pc_tail(drive, &pc)) {
  3573. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  3574. return -EIO;
  3575. }
  3576. return 0;
  3577. }
  3578. /*
  3579. * idetape_mtioctop is called from idetape_chrdev_ioctl when
  3580. * the general mtio MTIOCTOP ioctl is requested.
  3581. *
  3582. * We currently support the following mtio.h operations:
  3583. *
  3584. * MTFSF - Space over mt_count filemarks in the positive direction.
  3585. * The tape is positioned after the last spaced filemark.
  3586. *
  3587. * MTFSFM - Same as MTFSF, but the tape is positioned before the
  3588. * last filemark.
  3589. *
  3590. * MTBSF - Steps background over mt_count filemarks, tape is
  3591. * positioned before the last filemark.
  3592. *
  3593. * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
  3594. *
  3595. * Note:
  3596. *
  3597. * MTBSF and MTBSFM are not supported when the tape doesn't
  3598. * support spacing over filemarks in the reverse direction.
  3599. * In this case, MTFSFM is also usually not supported (it is
  3600. * supported in the rare case in which we crossed the filemark
  3601. * during our read-ahead pipelined operation mode).
  3602. *
  3603. * MTWEOF - Writes mt_count filemarks. Tape is positioned after
  3604. * the last written filemark.
  3605. *
  3606. * MTREW - Rewinds tape.
  3607. *
  3608. * MTLOAD - Loads the tape.
  3609. *
  3610. * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
  3611. * MTUNLOAD prevents further access until the media is replaced.
  3612. *
  3613. * MTNOP - Flushes tape buffers.
  3614. *
  3615. * MTRETEN - Retension media. This typically consists of one end
  3616. * to end pass on the media.
  3617. *
  3618. * MTEOM - Moves to the end of recorded data.
  3619. *
  3620. * MTERASE - Erases tape.
  3621. *
  3622. * MTSETBLK - Sets the user block size to mt_count bytes. If
  3623. * mt_count is 0, we will attempt to autodetect
  3624. * the block size.
  3625. *
  3626. * MTSEEK - Positions the tape in a specific block number, where
  3627. * each block is assumed to contain which user_block_size
  3628. * bytes.
  3629. *
  3630. * MTSETPART - Switches to another tape partition.
  3631. *
  3632. * MTLOCK - Locks the tape door.
  3633. *
  3634. * MTUNLOCK - Unlocks the tape door.
  3635. *
  3636. * The following commands are currently not supported:
  3637. *
  3638. * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
  3639. * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
  3640. */
  3641. static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
  3642. {
  3643. idetape_tape_t *tape = drive->driver_data;
  3644. idetape_pc_t pc;
  3645. int i,retval;
  3646. #if IDETAPE_DEBUG_LOG
  3647. if (tape->debug_level >= 1)
  3648. printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
  3649. "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
  3650. #endif /* IDETAPE_DEBUG_LOG */
  3651. /*
  3652. * Commands which need our pipelined read-ahead stages.
  3653. */
  3654. switch (mt_op) {
  3655. case MTFSF:
  3656. case MTFSFM:
  3657. case MTBSF:
  3658. case MTBSFM:
  3659. if (!mt_count)
  3660. return (0);
  3661. return (idetape_space_over_filemarks(drive,mt_op,mt_count));
  3662. default:
  3663. break;
  3664. }
  3665. switch (mt_op) {
  3666. case MTWEOF:
  3667. if (tape->write_prot)
  3668. return -EACCES;
  3669. idetape_discard_read_pipeline(drive, 1);
  3670. for (i = 0; i < mt_count; i++) {
  3671. retval = idetape_write_filemark(drive);
  3672. if (retval)
  3673. return retval;
  3674. }
  3675. return (0);
  3676. case MTREW:
  3677. idetape_discard_read_pipeline(drive, 0);
  3678. if (idetape_rewind_tape(drive))
  3679. return -EIO;
  3680. return 0;
  3681. case MTLOAD:
  3682. idetape_discard_read_pipeline(drive, 0);
  3683. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  3684. return (idetape_queue_pc_tail(drive, &pc));
  3685. case MTUNLOAD:
  3686. case MTOFFL:
  3687. /*
  3688. * If door is locked, attempt to unlock before
  3689. * attempting to eject.
  3690. */
  3691. if (tape->door_locked) {
  3692. if (idetape_create_prevent_cmd(drive, &pc, 0))
  3693. if (!idetape_queue_pc_tail(drive, &pc))
  3694. tape->door_locked = DOOR_UNLOCKED;
  3695. }
  3696. idetape_discard_read_pipeline(drive, 0);
  3697. idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
  3698. retval = idetape_queue_pc_tail(drive, &pc);
  3699. if (!retval)
  3700. clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  3701. return retval;
  3702. case MTNOP:
  3703. idetape_discard_read_pipeline(drive, 0);
  3704. return (idetape_flush_tape_buffers(drive));
  3705. case MTRETEN:
  3706. idetape_discard_read_pipeline(drive, 0);
  3707. idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  3708. return (idetape_queue_pc_tail(drive, &pc));
  3709. case MTEOM:
  3710. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  3711. return (idetape_queue_pc_tail(drive, &pc));
  3712. case MTERASE:
  3713. (void) idetape_rewind_tape(drive);
  3714. idetape_create_erase_cmd(&pc);
  3715. return (idetape_queue_pc_tail(drive, &pc));
  3716. case MTSETBLK:
  3717. if (mt_count) {
  3718. if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
  3719. return -EIO;
  3720. tape->user_bs_factor = mt_count / tape->tape_block_size;
  3721. clear_bit(IDETAPE_DETECT_BS, &tape->flags);
  3722. } else
  3723. set_bit(IDETAPE_DETECT_BS, &tape->flags);
  3724. return 0;
  3725. case MTSEEK:
  3726. idetape_discard_read_pipeline(drive, 0);
  3727. return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
  3728. case MTSETPART:
  3729. idetape_discard_read_pipeline(drive, 0);
  3730. return (idetape_position_tape(drive, 0, mt_count, 0));
  3731. case MTFSR:
  3732. case MTBSR:
  3733. case MTLOCK:
  3734. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  3735. return 0;
  3736. retval = idetape_queue_pc_tail(drive, &pc);
  3737. if (retval) return retval;
  3738. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  3739. return 0;
  3740. case MTUNLOCK:
  3741. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  3742. return 0;
  3743. retval = idetape_queue_pc_tail(drive, &pc);
  3744. if (retval) return retval;
  3745. tape->door_locked = DOOR_UNLOCKED;
  3746. return 0;
  3747. default:
  3748. printk(KERN_ERR "ide-tape: MTIO operation %d not "
  3749. "supported\n", mt_op);
  3750. return (-EIO);
  3751. }
  3752. }
  3753. /*
  3754. * Our character device ioctls.
  3755. *
  3756. * General mtio.h magnetic io commands are supported here, and not in
  3757. * the corresponding block interface.
  3758. *
  3759. * The following ioctls are supported:
  3760. *
  3761. * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
  3762. *
  3763. * MTIOCGET - The mt_dsreg field in the returned mtget structure
  3764. * will be set to (user block size in bytes <<
  3765. * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
  3766. *
  3767. * The mt_blkno is set to the current user block number.
  3768. * The other mtget fields are not supported.
  3769. *
  3770. * MTIOCPOS - The current tape "block position" is returned. We
  3771. * assume that each block contains user_block_size
  3772. * bytes.
  3773. *
  3774. * Our own ide-tape ioctls are supported on both interfaces.
  3775. */
  3776. static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  3777. {
  3778. struct ide_tape_obj *tape = ide_tape_f(file);
  3779. ide_drive_t *drive = tape->drive;
  3780. struct mtop mtop;
  3781. struct mtget mtget;
  3782. struct mtpos mtpos;
  3783. int block_offset = 0, position = tape->first_frame_position;
  3784. void __user *argp = (void __user *)arg;
  3785. #if IDETAPE_DEBUG_LOG
  3786. if (tape->debug_level >= 3)
  3787. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
  3788. "cmd=%u\n", cmd);
  3789. #endif /* IDETAPE_DEBUG_LOG */
  3790. tape->restart_speed_control_req = 1;
  3791. if (tape->chrdev_direction == idetape_direction_write) {
  3792. idetape_empty_write_pipeline(drive);
  3793. idetape_flush_tape_buffers(drive);
  3794. }
  3795. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  3796. block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
  3797. if ((position = idetape_read_position(drive)) < 0)
  3798. return -EIO;
  3799. }
  3800. switch (cmd) {
  3801. case MTIOCTOP:
  3802. if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
  3803. return -EFAULT;
  3804. return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
  3805. case MTIOCGET:
  3806. memset(&mtget, 0, sizeof (struct mtget));
  3807. mtget.mt_type = MT_ISSCSI2;
  3808. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  3809. mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  3810. if (tape->drv_write_prot) {
  3811. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  3812. }
  3813. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  3814. return -EFAULT;
  3815. return 0;
  3816. case MTIOCPOS:
  3817. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  3818. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  3819. return -EFAULT;
  3820. return 0;
  3821. default:
  3822. if (tape->chrdev_direction == idetape_direction_read)
  3823. idetape_discard_read_pipeline(drive, 1);
  3824. return idetape_blkdev_ioctl(drive, cmd, arg);
  3825. }
  3826. }
  3827. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
  3828. /*
  3829. * Our character device open function.
  3830. */
  3831. static int idetape_chrdev_open (struct inode *inode, struct file *filp)
  3832. {
  3833. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  3834. ide_drive_t *drive;
  3835. idetape_tape_t *tape;
  3836. idetape_pc_t pc;
  3837. int retval;
  3838. /*
  3839. * We really want to do nonseekable_open(inode, filp); here, but some
  3840. * versions of tar incorrectly call lseek on tapes and bail out if that
  3841. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  3842. */
  3843. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  3844. #if IDETAPE_DEBUG_LOG
  3845. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
  3846. #endif /* IDETAPE_DEBUG_LOG */
  3847. if (i >= MAX_HWIFS * MAX_DRIVES)
  3848. return -ENXIO;
  3849. if (!(tape = ide_tape_chrdev_get(i)))
  3850. return -ENXIO;
  3851. drive = tape->drive;
  3852. filp->private_data = tape;
  3853. if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
  3854. retval = -EBUSY;
  3855. goto out_put_tape;
  3856. }
  3857. retval = idetape_wait_ready(drive, 60 * HZ);
  3858. if (retval) {
  3859. clear_bit(IDETAPE_BUSY, &tape->flags);
  3860. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  3861. goto out_put_tape;
  3862. }
  3863. idetape_read_position(drive);
  3864. if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
  3865. (void)idetape_rewind_tape(drive);
  3866. if (tape->chrdev_direction != idetape_direction_read)
  3867. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  3868. /* Read block size and write protect status from drive. */
  3869. idetape_get_blocksize_from_block_descriptor(drive);
  3870. /* Set write protect flag if device is opened as read-only. */
  3871. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  3872. tape->write_prot = 1;
  3873. else
  3874. tape->write_prot = tape->drv_write_prot;
  3875. /* Make sure drive isn't write protected if user wants to write. */
  3876. if (tape->write_prot) {
  3877. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  3878. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  3879. clear_bit(IDETAPE_BUSY, &tape->flags);
  3880. retval = -EROFS;
  3881. goto out_put_tape;
  3882. }
  3883. }
  3884. /*
  3885. * Lock the tape drive door so user can't eject.
  3886. */
  3887. if (tape->chrdev_direction == idetape_direction_none) {
  3888. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  3889. if (!idetape_queue_pc_tail(drive, &pc)) {
  3890. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  3891. tape->door_locked = DOOR_LOCKED;
  3892. }
  3893. }
  3894. }
  3895. idetape_restart_speed_control(drive);
  3896. tape->restart_speed_control_req = 0;
  3897. return 0;
  3898. out_put_tape:
  3899. ide_tape_put(tape);
  3900. return retval;
  3901. }
  3902. static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
  3903. {
  3904. idetape_tape_t *tape = drive->driver_data;
  3905. idetape_empty_write_pipeline(drive);
  3906. tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
  3907. if (tape->merge_stage != NULL) {
  3908. idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
  3909. __idetape_kfree_stage(tape->merge_stage);
  3910. tape->merge_stage = NULL;
  3911. }
  3912. idetape_write_filemark(drive);
  3913. idetape_flush_tape_buffers(drive);
  3914. idetape_flush_tape_buffers(drive);
  3915. }
  3916. /*
  3917. * Our character device release function.
  3918. */
  3919. static int idetape_chrdev_release (struct inode *inode, struct file *filp)
  3920. {
  3921. struct ide_tape_obj *tape = ide_tape_f(filp);
  3922. ide_drive_t *drive = tape->drive;
  3923. idetape_pc_t pc;
  3924. unsigned int minor = iminor(inode);
  3925. lock_kernel();
  3926. tape = drive->driver_data;
  3927. #if IDETAPE_DEBUG_LOG
  3928. if (tape->debug_level >= 3)
  3929. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
  3930. #endif /* IDETAPE_DEBUG_LOG */
  3931. if (tape->chrdev_direction == idetape_direction_write)
  3932. idetape_write_release(drive, minor);
  3933. if (tape->chrdev_direction == idetape_direction_read) {
  3934. if (minor < 128)
  3935. idetape_discard_read_pipeline(drive, 1);
  3936. else
  3937. idetape_wait_for_pipeline(drive);
  3938. }
  3939. if (tape->cache_stage != NULL) {
  3940. __idetape_kfree_stage(tape->cache_stage);
  3941. tape->cache_stage = NULL;
  3942. }
  3943. if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
  3944. (void) idetape_rewind_tape(drive);
  3945. if (tape->chrdev_direction == idetape_direction_none) {
  3946. if (tape->door_locked == DOOR_LOCKED) {
  3947. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  3948. if (!idetape_queue_pc_tail(drive, &pc))
  3949. tape->door_locked = DOOR_UNLOCKED;
  3950. }
  3951. }
  3952. }
  3953. clear_bit(IDETAPE_BUSY, &tape->flags);
  3954. ide_tape_put(tape);
  3955. unlock_kernel();
  3956. return 0;
  3957. }
  3958. /*
  3959. * idetape_identify_device is called to check the contents of the
  3960. * ATAPI IDENTIFY command results. We return:
  3961. *
  3962. * 1 If the tape can be supported by us, based on the information
  3963. * we have so far.
  3964. *
  3965. * 0 If this tape driver is not currently supported by us.
  3966. */
  3967. static int idetape_identify_device (ide_drive_t *drive)
  3968. {
  3969. struct idetape_id_gcw gcw;
  3970. struct hd_driveid *id = drive->id;
  3971. #if IDETAPE_DEBUG_INFO
  3972. unsigned short mask,i;
  3973. #endif /* IDETAPE_DEBUG_INFO */
  3974. if (drive->id_read == 0)
  3975. return 1;
  3976. *((unsigned short *) &gcw) = id->config;
  3977. #if IDETAPE_DEBUG_INFO
  3978. printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
  3979. printk(KERN_INFO "ide-tape: Protocol Type: ");
  3980. switch (gcw.protocol) {
  3981. case 0: case 1: printk("ATA\n");break;
  3982. case 2: printk("ATAPI\n");break;
  3983. case 3: printk("Reserved (Unknown to ide-tape)\n");break;
  3984. }
  3985. printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
  3986. switch (gcw.device_type) {
  3987. case 0: printk("Direct-access Device\n");break;
  3988. case 1: printk("Streaming Tape Device\n");break;
  3989. case 2: case 3: case 4: printk("Reserved\n");break;
  3990. case 5: printk("CD-ROM Device\n");break;
  3991. case 6: printk("Reserved\n");
  3992. case 7: printk("Optical memory Device\n");break;
  3993. case 0x1f: printk("Unknown or no Device type\n");break;
  3994. default: printk("Reserved\n");
  3995. }
  3996. printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
  3997. printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
  3998. switch (gcw.drq_type) {
  3999. case 0: printk("Microprocessor DRQ\n");break;
  4000. case 1: printk("Interrupt DRQ\n");break;
  4001. case 2: printk("Accelerated DRQ\n");break;
  4002. case 3: printk("Reserved\n");break;
  4003. }
  4004. printk(KERN_INFO "ide-tape: Command Packet Size: ");
  4005. switch (gcw.packet_size) {
  4006. case 0: printk("12 bytes\n");break;
  4007. case 1: printk("16 bytes\n");break;
  4008. default: printk("Reserved\n");break;
  4009. }
  4010. printk(KERN_INFO "ide-tape: Model: %.40s\n",id->model);
  4011. printk(KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev);
  4012. printk(KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no);
  4013. printk(KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512);
  4014. printk(KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
  4015. printk(KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
  4016. printk(KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
  4017. printk(KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
  4018. printk(KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
  4019. printk(KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO);
  4020. printk(KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA);
  4021. printk(KERN_INFO "ide-tape: Single Word DMA supported modes: ");
  4022. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  4023. if (id->dma_1word & mask)
  4024. printk("%d ",i);
  4025. if (id->dma_1word & (mask << 8))
  4026. printk("(active) ");
  4027. }
  4028. printk("\n");
  4029. printk(KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
  4030. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  4031. if (id->dma_mword & mask)
  4032. printk("%d ",i);
  4033. if (id->dma_mword & (mask << 8))
  4034. printk("(active) ");
  4035. }
  4036. printk("\n");
  4037. if (id->field_valid & 0x0002) {
  4038. printk(KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n",
  4039. id->eide_pio_modes & 1 ? "Mode 3":"None");
  4040. printk(KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
  4041. if (id->eide_dma_min == 0)
  4042. printk("Not supported\n");
  4043. else
  4044. printk("%d ns\n",id->eide_dma_min);
  4045. printk(KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: ");
  4046. if (id->eide_dma_time == 0)
  4047. printk("Not supported\n");
  4048. else
  4049. printk("%d ns\n",id->eide_dma_time);
  4050. printk(KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
  4051. if (id->eide_pio == 0)
  4052. printk("Not supported\n");
  4053. else
  4054. printk("%d ns\n",id->eide_pio);
  4055. printk(KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
  4056. if (id->eide_pio_iordy == 0)
  4057. printk("Not supported\n");
  4058. else
  4059. printk("%d ns\n",id->eide_pio_iordy);
  4060. } else
  4061. printk(KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n");
  4062. #endif /* IDETAPE_DEBUG_INFO */
  4063. /* Check that we can support this device */
  4064. if (gcw.protocol !=2 )
  4065. printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
  4066. else if (gcw.device_type != 1)
  4067. printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
  4068. else if (!gcw.removable)
  4069. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  4070. else if (gcw.packet_size != 0) {
  4071. printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
  4072. if (gcw.packet_size == 1)
  4073. printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
  4074. } else
  4075. return 1;
  4076. return 0;
  4077. }
  4078. /*
  4079. * Use INQUIRY to get the firmware revision
  4080. */
  4081. static void idetape_get_inquiry_results (ide_drive_t *drive)
  4082. {
  4083. char *r;
  4084. idetape_tape_t *tape = drive->driver_data;
  4085. idetape_pc_t pc;
  4086. idetape_inquiry_result_t *inquiry;
  4087. idetape_create_inquiry_cmd(&pc);
  4088. if (idetape_queue_pc_tail(drive, &pc)) {
  4089. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
  4090. return;
  4091. }
  4092. inquiry = (idetape_inquiry_result_t *) pc.buffer;
  4093. memcpy(tape->vendor_id, inquiry->vendor_id, 8);
  4094. memcpy(tape->product_id, inquiry->product_id, 16);
  4095. memcpy(tape->firmware_revision, inquiry->revision_level, 4);
  4096. ide_fixstring(tape->vendor_id, 10, 0);
  4097. ide_fixstring(tape->product_id, 18, 0);
  4098. ide_fixstring(tape->firmware_revision, 6, 0);
  4099. r = tape->firmware_revision;
  4100. if (*(r + 1) == '.')
  4101. tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
  4102. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
  4103. }
  4104. /*
  4105. * idetape_get_mode_sense_results asks the tape about its various
  4106. * parameters. In particular, we will adjust our data transfer buffer
  4107. * size to the recommended value as returned by the tape.
  4108. */
  4109. static void idetape_get_mode_sense_results (ide_drive_t *drive)
  4110. {
  4111. idetape_tape_t *tape = drive->driver_data;
  4112. idetape_pc_t pc;
  4113. idetape_mode_parameter_header_t *header;
  4114. idetape_capabilities_page_t *capabilities;
  4115. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  4116. if (idetape_queue_pc_tail(drive, &pc)) {
  4117. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
  4118. tape->tape_block_size = 512;
  4119. tape->capabilities.ctl = 52;
  4120. tape->capabilities.speed = 450;
  4121. tape->capabilities.buffer_size = 6 * 52;
  4122. return;
  4123. }
  4124. header = (idetape_mode_parameter_header_t *) pc.buffer;
  4125. capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
  4126. capabilities->max_speed = ntohs(capabilities->max_speed);
  4127. capabilities->ctl = ntohs(capabilities->ctl);
  4128. capabilities->speed = ntohs(capabilities->speed);
  4129. capabilities->buffer_size = ntohs(capabilities->buffer_size);
  4130. if (!capabilities->speed) {
  4131. printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
  4132. capabilities->speed = 650;
  4133. }
  4134. if (!capabilities->max_speed) {
  4135. printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
  4136. capabilities->max_speed = 650;
  4137. }
  4138. tape->capabilities = *capabilities; /* Save us a copy */
  4139. if (capabilities->blk512)
  4140. tape->tape_block_size = 512;
  4141. else if (capabilities->blk1024)
  4142. tape->tape_block_size = 1024;
  4143. #if IDETAPE_DEBUG_INFO
  4144. printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
  4145. printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
  4146. printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
  4147. printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
  4148. printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
  4149. printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
  4150. printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
  4151. printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
  4152. printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
  4153. printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
  4154. printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
  4155. printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
  4156. printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
  4157. printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
  4158. printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
  4159. printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
  4160. printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
  4161. printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
  4162. printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
  4163. printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
  4164. printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
  4165. printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
  4166. printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
  4167. printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
  4168. printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
  4169. printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
  4170. #endif /* IDETAPE_DEBUG_INFO */
  4171. }
  4172. /*
  4173. * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
  4174. * and if it succeeds sets the tape block size with the reported value
  4175. */
  4176. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
  4177. {
  4178. idetape_tape_t *tape = drive->driver_data;
  4179. idetape_pc_t pc;
  4180. idetape_mode_parameter_header_t *header;
  4181. idetape_parameter_block_descriptor_t *block_descrp;
  4182. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  4183. if (idetape_queue_pc_tail(drive, &pc)) {
  4184. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  4185. if (tape->tape_block_size == 0) {
  4186. printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
  4187. tape->tape_block_size = 32768;
  4188. }
  4189. return;
  4190. }
  4191. header = (idetape_mode_parameter_header_t *) pc.buffer;
  4192. block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
  4193. tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
  4194. tape->drv_write_prot = (header->dsp & 0x80) >> 7;
  4195. #if IDETAPE_DEBUG_INFO
  4196. printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
  4197. #endif /* IDETAPE_DEBUG_INFO */
  4198. }
  4199. static void idetape_add_settings (ide_drive_t *drive)
  4200. {
  4201. idetape_tape_t *tape = drive->driver_data;
  4202. /*
  4203. * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
  4204. */
  4205. ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
  4206. ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
  4207. ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
  4208. ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
  4209. ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
  4210. ide_add_setting(drive, "pipeline_pending",SETTING_READ,-1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
  4211. ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
  4212. ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
  4213. ide_add_setting(drive, "tdsc", SETTING_RW, -1, -1, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
  4214. ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  4215. ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
  4216. ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed, NULL);
  4217. ide_add_setting(drive, "avg_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
  4218. ide_add_setting(drive, "debug_level",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
  4219. }
  4220. /*
  4221. * ide_setup is called to:
  4222. *
  4223. * 1. Initialize our various state variables.
  4224. * 2. Ask the tape for its capabilities.
  4225. * 3. Allocate a buffer which will be used for data
  4226. * transfer. The buffer size is chosen based on
  4227. * the recommendation which we received in step (2).
  4228. *
  4229. * Note that at this point ide.c already assigned us an irq, so that
  4230. * we can queue requests here and wait for their completion.
  4231. */
  4232. static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
  4233. {
  4234. unsigned long t1, tmid, tn, t;
  4235. int speed;
  4236. struct idetape_id_gcw gcw;
  4237. int stage_size;
  4238. struct sysinfo si;
  4239. spin_lock_init(&tape->spinlock);
  4240. drive->dsc_overlap = 1;
  4241. #ifdef CONFIG_BLK_DEV_IDEPCI
  4242. if (HWIF(drive)->pci_dev != NULL) {
  4243. /*
  4244. * These two ide-pci host adapters appear to need DSC overlap disabled.
  4245. * This probably needs further analysis.
  4246. */
  4247. if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
  4248. (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
  4249. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name);
  4250. drive->dsc_overlap = 0;
  4251. }
  4252. }
  4253. #endif /* CONFIG_BLK_DEV_IDEPCI */
  4254. /* Seagate Travan drives do not support DSC overlap. */
  4255. if (strstr(drive->id->model, "Seagate STT3401"))
  4256. drive->dsc_overlap = 0;
  4257. tape->minor = minor;
  4258. tape->name[0] = 'h';
  4259. tape->name[1] = 't';
  4260. tape->name[2] = '0' + minor;
  4261. tape->chrdev_direction = idetape_direction_none;
  4262. tape->pc = tape->pc_stack;
  4263. tape->max_insert_speed = 10000;
  4264. tape->speed_control = 1;
  4265. *((unsigned short *) &gcw) = drive->id->config;
  4266. if (gcw.drq_type == 1)
  4267. set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
  4268. tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
  4269. idetape_get_inquiry_results(drive);
  4270. idetape_get_mode_sense_results(drive);
  4271. idetape_get_blocksize_from_block_descriptor(drive);
  4272. tape->user_bs_factor = 1;
  4273. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  4274. while (tape->stage_size > 0xffff) {
  4275. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  4276. tape->capabilities.ctl /= 2;
  4277. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  4278. }
  4279. stage_size = tape->stage_size;
  4280. tape->pages_per_stage = stage_size / PAGE_SIZE;
  4281. if (stage_size % PAGE_SIZE) {
  4282. tape->pages_per_stage++;
  4283. tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
  4284. }
  4285. /*
  4286. * Select the "best" DSC read/write polling frequency
  4287. * and pipeline size.
  4288. */
  4289. speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
  4290. tape->max_stages = speed * 1000 * 10 / tape->stage_size;
  4291. /*
  4292. * Limit memory use for pipeline to 10% of physical memory
  4293. */
  4294. si_meminfo(&si);
  4295. if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
  4296. tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
  4297. tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
  4298. tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
  4299. tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
  4300. if (tape->max_stages == 0)
  4301. tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
  4302. t1 = (tape->stage_size * HZ) / (speed * 1000);
  4303. tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
  4304. tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
  4305. if (tape->max_stages)
  4306. t = tn;
  4307. else
  4308. t = t1;
  4309. /*
  4310. * Ensure that the number we got makes sense; limit
  4311. * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  4312. */
  4313. tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
  4314. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  4315. "%dkB pipeline, %lums tDSC%s\n",
  4316. drive->name, tape->name, tape->capabilities.speed,
  4317. (tape->capabilities.buffer_size * 512) / tape->stage_size,
  4318. tape->stage_size / 1024,
  4319. tape->max_stages * tape->stage_size / 1024,
  4320. tape->best_dsc_rw_frequency * 1000 / HZ,
  4321. drive->using_dma ? ", DMA":"");
  4322. idetape_add_settings(drive);
  4323. }
  4324. static void ide_tape_remove(ide_drive_t *drive)
  4325. {
  4326. idetape_tape_t *tape = drive->driver_data;
  4327. ide_unregister_subdriver(drive, tape->driver);
  4328. ide_unregister_region(tape->disk);
  4329. ide_tape_put(tape);
  4330. }
  4331. static void ide_tape_release(struct kref *kref)
  4332. {
  4333. struct ide_tape_obj *tape = to_ide_tape(kref);
  4334. ide_drive_t *drive = tape->drive;
  4335. struct gendisk *g = tape->disk;
  4336. BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
  4337. drive->dsc_overlap = 0;
  4338. drive->driver_data = NULL;
  4339. class_device_destroy(idetape_sysfs_class,
  4340. MKDEV(IDETAPE_MAJOR, tape->minor));
  4341. class_device_destroy(idetape_sysfs_class,
  4342. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  4343. devfs_remove("%s/mt", drive->devfs_name);
  4344. devfs_remove("%s/mtn", drive->devfs_name);
  4345. devfs_unregister_tape(g->number);
  4346. idetape_devs[tape->minor] = NULL;
  4347. g->private_data = NULL;
  4348. put_disk(g);
  4349. kfree(tape);
  4350. }
  4351. #ifdef CONFIG_PROC_FS
  4352. static int proc_idetape_read_name
  4353. (char *page, char **start, off_t off, int count, int *eof, void *data)
  4354. {
  4355. ide_drive_t *drive = (ide_drive_t *) data;
  4356. idetape_tape_t *tape = drive->driver_data;
  4357. char *out = page;
  4358. int len;
  4359. len = sprintf(out, "%s\n", tape->name);
  4360. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  4361. }
  4362. static ide_proc_entry_t idetape_proc[] = {
  4363. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  4364. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  4365. { NULL, 0, NULL, NULL }
  4366. };
  4367. #else
  4368. #define idetape_proc NULL
  4369. #endif
  4370. static int ide_tape_probe(ide_drive_t *);
  4371. static ide_driver_t idetape_driver = {
  4372. .gen_driver = {
  4373. .owner = THIS_MODULE,
  4374. .name = "ide-tape",
  4375. .bus = &ide_bus_type,
  4376. },
  4377. .probe = ide_tape_probe,
  4378. .remove = ide_tape_remove,
  4379. .version = IDETAPE_VERSION,
  4380. .media = ide_tape,
  4381. .supports_dsc_overlap = 1,
  4382. .do_request = idetape_do_request,
  4383. .end_request = idetape_end_request,
  4384. .error = __ide_error,
  4385. .abort = __ide_abort,
  4386. .proc = idetape_proc,
  4387. };
  4388. /*
  4389. * Our character device supporting functions, passed to register_chrdev.
  4390. */
  4391. static struct file_operations idetape_fops = {
  4392. .owner = THIS_MODULE,
  4393. .read = idetape_chrdev_read,
  4394. .write = idetape_chrdev_write,
  4395. .ioctl = idetape_chrdev_ioctl,
  4396. .open = idetape_chrdev_open,
  4397. .release = idetape_chrdev_release,
  4398. };
  4399. static int idetape_open(struct inode *inode, struct file *filp)
  4400. {
  4401. struct gendisk *disk = inode->i_bdev->bd_disk;
  4402. struct ide_tape_obj *tape;
  4403. ide_drive_t *drive;
  4404. if (!(tape = ide_tape_get(disk)))
  4405. return -ENXIO;
  4406. drive = tape->drive;
  4407. drive->usage++;
  4408. return 0;
  4409. }
  4410. static int idetape_release(struct inode *inode, struct file *filp)
  4411. {
  4412. struct gendisk *disk = inode->i_bdev->bd_disk;
  4413. struct ide_tape_obj *tape = ide_tape_g(disk);
  4414. ide_drive_t *drive = tape->drive;
  4415. drive->usage--;
  4416. ide_tape_put(tape);
  4417. return 0;
  4418. }
  4419. static int idetape_ioctl(struct inode *inode, struct file *file,
  4420. unsigned int cmd, unsigned long arg)
  4421. {
  4422. struct block_device *bdev = inode->i_bdev;
  4423. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  4424. ide_drive_t *drive = tape->drive;
  4425. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  4426. if (err == -EINVAL)
  4427. err = idetape_blkdev_ioctl(drive, cmd, arg);
  4428. return err;
  4429. }
  4430. static struct block_device_operations idetape_block_ops = {
  4431. .owner = THIS_MODULE,
  4432. .open = idetape_open,
  4433. .release = idetape_release,
  4434. .ioctl = idetape_ioctl,
  4435. };
  4436. static int ide_tape_probe(ide_drive_t *drive)
  4437. {
  4438. idetape_tape_t *tape;
  4439. struct gendisk *g;
  4440. int minor;
  4441. if (!strstr("ide-tape", drive->driver_req))
  4442. goto failed;
  4443. if (!drive->present)
  4444. goto failed;
  4445. if (drive->media != ide_tape)
  4446. goto failed;
  4447. if (!idetape_identify_device (drive)) {
  4448. printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
  4449. goto failed;
  4450. }
  4451. if (drive->scsi) {
  4452. printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
  4453. goto failed;
  4454. }
  4455. if (strstr(drive->id->model, "OnStream DI-")) {
  4456. printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
  4457. printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
  4458. }
  4459. tape = (idetape_tape_t *) kzalloc (sizeof (idetape_tape_t), GFP_KERNEL);
  4460. if (tape == NULL) {
  4461. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
  4462. goto failed;
  4463. }
  4464. g = alloc_disk(1 << PARTN_BITS);
  4465. if (!g)
  4466. goto out_free_tape;
  4467. ide_init_disk(g, drive);
  4468. ide_register_subdriver(drive, &idetape_driver);
  4469. kref_init(&tape->kref);
  4470. tape->drive = drive;
  4471. tape->driver = &idetape_driver;
  4472. tape->disk = g;
  4473. g->private_data = &tape->driver;
  4474. drive->driver_data = tape;
  4475. down(&idetape_ref_sem);
  4476. for (minor = 0; idetape_devs[minor]; minor++)
  4477. ;
  4478. idetape_devs[minor] = tape;
  4479. up(&idetape_ref_sem);
  4480. idetape_setup(drive, tape, minor);
  4481. class_device_create(idetape_sysfs_class, NULL,
  4482. MKDEV(IDETAPE_MAJOR, minor), &drive->gendev, "%s", tape->name);
  4483. class_device_create(idetape_sysfs_class, NULL,
  4484. MKDEV(IDETAPE_MAJOR, minor + 128), &drive->gendev, "n%s", tape->name);
  4485. devfs_mk_cdev(MKDEV(HWIF(drive)->major, minor),
  4486. S_IFCHR | S_IRUGO | S_IWUGO,
  4487. "%s/mt", drive->devfs_name);
  4488. devfs_mk_cdev(MKDEV(HWIF(drive)->major, minor + 128),
  4489. S_IFCHR | S_IRUGO | S_IWUGO,
  4490. "%s/mtn", drive->devfs_name);
  4491. g->number = devfs_register_tape(drive->devfs_name);
  4492. g->fops = &idetape_block_ops;
  4493. ide_register_region(g);
  4494. return 0;
  4495. out_free_tape:
  4496. kfree(tape);
  4497. failed:
  4498. return -ENODEV;
  4499. }
  4500. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  4501. MODULE_LICENSE("GPL");
  4502. static void __exit idetape_exit (void)
  4503. {
  4504. driver_unregister(&idetape_driver.gen_driver);
  4505. class_destroy(idetape_sysfs_class);
  4506. unregister_chrdev(IDETAPE_MAJOR, "ht");
  4507. }
  4508. static int __init idetape_init(void)
  4509. {
  4510. int error = 1;
  4511. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  4512. if (IS_ERR(idetape_sysfs_class)) {
  4513. idetape_sysfs_class = NULL;
  4514. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  4515. error = -EBUSY;
  4516. goto out;
  4517. }
  4518. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  4519. printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
  4520. error = -EBUSY;
  4521. goto out_free_class;
  4522. }
  4523. error = driver_register(&idetape_driver.gen_driver);
  4524. if (error)
  4525. goto out_free_driver;
  4526. return 0;
  4527. out_free_driver:
  4528. driver_unregister(&idetape_driver.gen_driver);
  4529. out_free_class:
  4530. class_destroy(idetape_sysfs_class);
  4531. out:
  4532. return error;
  4533. }
  4534. MODULE_ALIAS("ide:*m-tape*");
  4535. module_init(idetape_init);
  4536. module_exit(idetape_exit);
  4537. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);