ide-tape.c 163 KB

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