osst.c 181 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913
  1. /*
  2. SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
  3. file Documentation/scsi/st.txt for more information.
  4. History:
  5. OnStream SCSI Tape support (osst) cloned from st.c by
  6. Willem Riede (osst@riede.org) Feb 2000
  7. Fixes ... Kurt Garloff <garloff@suse.de> Mar 2000
  8. Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
  9. Contribution and ideas from several people including (in alphabetical
  10. order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
  11. Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
  12. Copyright 1992 - 2002 Kai Makisara / 2000 - 2004 Willem Riede
  13. email osst@riede.org
  14. $Header: /cvsroot/osst/Driver/osst.c,v 1.73 2005/01/01 21:13:34 wriede Exp $
  15. Microscopic alterations - Rik Ling, 2000/12/21
  16. Last st.c sync: Tue Oct 15 22:01:04 2002 by makisara
  17. Some small formal changes - aeb, 950809
  18. */
  19. static const char * cvsid = "$Id: osst.c,v 1.73 2005/01/01 21:13:34 wriede Exp $";
  20. static const char * osst_version = "0.99.3";
  21. /* The "failure to reconnect" firmware bug */
  22. #define OSST_FW_NEED_POLL_MIN 10601 /*(107A)*/
  23. #define OSST_FW_NEED_POLL_MAX 10704 /*(108D)*/
  24. #define OSST_FW_NEED_POLL(x,d) ((x) >= OSST_FW_NEED_POLL_MIN && (x) <= OSST_FW_NEED_POLL_MAX && d->host->this_id != 7)
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/kernel.h>
  28. #include <linux/sched.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/mm.h>
  31. #include <linux/init.h>
  32. #include <linux/string.h>
  33. #include <linux/errno.h>
  34. #include <linux/mtio.h>
  35. #include <linux/ioctl.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/devfs_fs_kernel.h>
  42. #include <linux/delay.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/dma.h>
  45. #include <asm/system.h>
  46. /* The driver prints some debugging information on the console if DEBUG
  47. is defined and non-zero. */
  48. #define DEBUG 0
  49. /* The message level for the debug messages is currently set to KERN_NOTICE
  50. so that people can easily see the messages. Later when the debugging messages
  51. in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
  52. #define OSST_DEB_MSG KERN_NOTICE
  53. #include <scsi/scsi.h>
  54. #include <scsi/scsi_dbg.h>
  55. #include <scsi/scsi_device.h>
  56. #include <scsi/scsi_driver.h>
  57. #include <scsi/scsi_eh.h>
  58. #include <scsi/scsi_host.h>
  59. #include <scsi/scsi_ioctl.h>
  60. #include <scsi/scsi_request.h>
  61. #define ST_KILOBYTE 1024
  62. #include "st.h"
  63. #include "osst.h"
  64. #include "osst_options.h"
  65. #include "osst_detect.h"
  66. static int max_dev = 0;
  67. static int write_threshold_kbs = 0;
  68. static int max_sg_segs = 0;
  69. #ifdef MODULE
  70. MODULE_AUTHOR("Willem Riede");
  71. MODULE_DESCRIPTION("OnStream {DI-|FW-|SC-|USB}{30|50} Tape Driver");
  72. MODULE_LICENSE("GPL");
  73. module_param(max_dev, int, 0444);
  74. MODULE_PARM_DESC(max_dev, "Maximum number of OnStream Tape Drives to attach (4)");
  75. module_param(write_threshold_kbs, int, 0644);
  76. MODULE_PARM_DESC(write_threshold_kbs, "Asynchronous write threshold (KB; 32)");
  77. module_param(max_sg_segs, int, 0644);
  78. MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (9)");
  79. #else
  80. static struct osst_dev_parm {
  81. char *name;
  82. int *val;
  83. } parms[] __initdata = {
  84. { "max_dev", &max_dev },
  85. { "write_threshold_kbs", &write_threshold_kbs },
  86. { "max_sg_segs", &max_sg_segs }
  87. };
  88. #endif
  89. static char *osst_formats[ST_NBR_MODES] ={"", "l", "m", "a"};
  90. /* Some default definitions have been moved to osst_options.h */
  91. #define OSST_BUFFER_SIZE (OSST_BUFFER_BLOCKS * ST_KILOBYTE)
  92. #define OSST_WRITE_THRESHOLD (OSST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
  93. /* The buffer size should fit into the 24 bits for length in the
  94. 6-byte SCSI read and write commands. */
  95. #if OSST_BUFFER_SIZE >= (2 << 24 - 1)
  96. #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
  97. #endif
  98. #if DEBUG
  99. static int debugging = 1;
  100. /* uncomment define below to test error recovery */
  101. // #define OSST_INJECT_ERRORS 1
  102. #endif
  103. /* Do not retry! The drive firmware already retries when appropriate,
  104. and when it tries to tell us something, we had better listen... */
  105. #define MAX_RETRIES 0
  106. #define NO_TAPE NOT_READY
  107. #define OSST_WAIT_POSITION_COMPLETE (HZ > 200 ? HZ / 200 : 1)
  108. #define OSST_WAIT_WRITE_COMPLETE (HZ / 12)
  109. #define OSST_WAIT_LONG_WRITE_COMPLETE (HZ / 2)
  110. #define OSST_TIMEOUT (200 * HZ)
  111. #define OSST_LONG_TIMEOUT (1800 * HZ)
  112. #define TAPE_NR(x) (iminor(x) & ~(-1 << ST_MODE_SHIFT))
  113. #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
  114. #define TAPE_REWIND(x) ((iminor(x) & 0x80) == 0)
  115. #define TAPE_IS_RAW(x) (TAPE_MODE(x) & (ST_NBR_MODES >> 1))
  116. /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
  117. 24 bits) */
  118. #define SET_DENS_AND_BLK 0x10001
  119. static int osst_buffer_size = OSST_BUFFER_SIZE;
  120. static int osst_write_threshold = OSST_WRITE_THRESHOLD;
  121. static int osst_max_sg_segs = OSST_MAX_SG;
  122. static int osst_max_dev = OSST_MAX_TAPES;
  123. static int osst_nr_dev;
  124. static struct osst_tape **os_scsi_tapes = NULL;
  125. static DEFINE_RWLOCK(os_scsi_tapes_lock);
  126. static int modes_defined = 0;
  127. static struct osst_buffer *new_tape_buffer(int, int, int);
  128. static int enlarge_buffer(struct osst_buffer *, int);
  129. static void normalize_buffer(struct osst_buffer *);
  130. static int append_to_buffer(const char __user *, struct osst_buffer *, int);
  131. static int from_buffer(struct osst_buffer *, char __user *, int);
  132. static int osst_zero_buffer_tail(struct osst_buffer *);
  133. static int osst_copy_to_buffer(struct osst_buffer *, unsigned char *);
  134. static int osst_copy_from_buffer(struct osst_buffer *, unsigned char *);
  135. static int osst_probe(struct device *);
  136. static int osst_remove(struct device *);
  137. static struct scsi_driver osst_template = {
  138. .owner = THIS_MODULE,
  139. .gendrv = {
  140. .name = "osst",
  141. .probe = osst_probe,
  142. .remove = osst_remove,
  143. }
  144. };
  145. static int osst_int_ioctl(struct osst_tape *STp, struct scsi_request ** aSRpnt,
  146. unsigned int cmd_in, unsigned long arg);
  147. static int osst_set_frame_position(struct osst_tape *STp, struct scsi_request ** aSRpnt, int frame, int skip);
  148. static int osst_get_frame_position(struct osst_tape *STp, struct scsi_request ** aSRpnt);
  149. static int osst_flush_write_buffer(struct osst_tape *STp, struct scsi_request ** aSRpnt);
  150. static int osst_write_error_recovery(struct osst_tape * STp, struct scsi_request ** aSRpnt, int pending);
  151. static inline char *tape_name(struct osst_tape *tape)
  152. {
  153. return tape->drive->disk_name;
  154. }
  155. /* Routines that handle the interaction with mid-layer SCSI routines */
  156. /* Convert the result to success code */
  157. static int osst_chk_result(struct osst_tape * STp, struct scsi_request * SRpnt)
  158. {
  159. char *name = tape_name(STp);
  160. int result = SRpnt->sr_result;
  161. unsigned char * sense = SRpnt->sr_sense_buffer, scode;
  162. #if DEBUG
  163. const char *stp;
  164. #endif
  165. if (!result) {
  166. sense[0] = 0; /* We don't have sense data if this byte is zero */
  167. return 0;
  168. }
  169. if ((driver_byte(result) & DRIVER_MASK) == DRIVER_SENSE)
  170. scode = sense[2] & 0x0f;
  171. else {
  172. sense[0] = 0; /* We don't have sense data if this byte is zero */
  173. scode = 0;
  174. }
  175. #if DEBUG
  176. if (debugging) {
  177. printk(OSST_DEB_MSG "%s:D: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
  178. name, result,
  179. SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],
  180. SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],
  181. SRpnt->sr_bufflen);
  182. if (scode) printk(OSST_DEB_MSG "%s:D: Sense: %02x, ASC: %02x, ASCQ: %02x\n",
  183. name, scode, sense[12], sense[13]);
  184. if (driver_byte(result) & DRIVER_SENSE)
  185. scsi_print_req_sense("osst ", SRpnt);
  186. }
  187. else
  188. #endif
  189. if (!(driver_byte(result) & DRIVER_SENSE) ||
  190. ((sense[0] & 0x70) == 0x70 &&
  191. scode != NO_SENSE &&
  192. scode != RECOVERED_ERROR &&
  193. /* scode != UNIT_ATTENTION && */
  194. scode != BLANK_CHECK &&
  195. scode != VOLUME_OVERFLOW &&
  196. SRpnt->sr_cmnd[0] != MODE_SENSE &&
  197. SRpnt->sr_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
  198. if (driver_byte(result) & DRIVER_SENSE) {
  199. printk(KERN_WARNING "%s:W: Command with sense data:\n", name);
  200. scsi_print_req_sense("osst:", SRpnt);
  201. }
  202. else {
  203. static int notyetprinted = 1;
  204. printk(KERN_WARNING
  205. "%s:W: Warning %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
  206. name, result, suggestion(result), driver_byte(result) & DRIVER_MASK,
  207. host_byte(result));
  208. if (notyetprinted) {
  209. notyetprinted = 0;
  210. printk(KERN_INFO
  211. "%s:I: This warning may be caused by your scsi controller,\n", name);
  212. printk(KERN_INFO
  213. "%s:I: it has been reported with some Buslogic cards.\n", name);
  214. }
  215. }
  216. }
  217. STp->pos_unknown |= STp->device->was_reset;
  218. if ((sense[0] & 0x70) == 0x70 &&
  219. scode == RECOVERED_ERROR) {
  220. STp->recover_count++;
  221. STp->recover_erreg++;
  222. #if DEBUG
  223. if (debugging) {
  224. if (SRpnt->sr_cmnd[0] == READ_6)
  225. stp = "read";
  226. else if (SRpnt->sr_cmnd[0] == WRITE_6)
  227. stp = "write";
  228. else
  229. stp = "ioctl";
  230. printk(OSST_DEB_MSG "%s:D: Recovered %s error (%d).\n", name, stp,
  231. STp->recover_count);
  232. }
  233. #endif
  234. if ((sense[2] & 0xe0) == 0)
  235. return 0;
  236. }
  237. return (-EIO);
  238. }
  239. /* Wakeup from interrupt */
  240. static void osst_sleep_done (struct scsi_cmnd * SCpnt)
  241. {
  242. struct osst_tape * STp = container_of(SCpnt->request->rq_disk->private_data, struct osst_tape, driver);
  243. if ((STp->buffer)->writing &&
  244. (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
  245. (SCpnt->sense_buffer[2] & 0x40)) {
  246. /* EOM at write-behind, has all been written? */
  247. if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
  248. STp->buffer->midlevel_result = SCpnt->result; /* Error */
  249. else
  250. STp->buffer->midlevel_result = INT_MAX; /* OK */
  251. }
  252. else
  253. STp->buffer->midlevel_result = SCpnt->result;
  254. SCpnt->request->rq_status = RQ_SCSI_DONE;
  255. STp->buffer->last_SRpnt = SCpnt->sc_request;
  256. #if DEBUG
  257. STp->write_pending = 0;
  258. #endif
  259. complete(SCpnt->request->waiting);
  260. }
  261. /* Do the scsi command. Waits until command performed if do_wait is true.
  262. Otherwise osst_write_behind_check() is used to check that the command
  263. has finished. */
  264. static struct scsi_request * osst_do_scsi(struct scsi_request *SRpnt, struct osst_tape *STp,
  265. unsigned char *cmd, int bytes, int direction, int timeout, int retries, int do_wait)
  266. {
  267. unsigned char *bp;
  268. #ifdef OSST_INJECT_ERRORS
  269. static int inject = 0;
  270. static int repeat = 0;
  271. #endif
  272. if (SRpnt == NULL) {
  273. if ((SRpnt = scsi_allocate_request(STp->device, GFP_ATOMIC)) == NULL) {
  274. printk(KERN_ERR "%s:E: Can't get SCSI request.\n", tape_name(STp));
  275. if (signal_pending(current))
  276. (STp->buffer)->syscall_result = (-EINTR);
  277. else
  278. (STp->buffer)->syscall_result = (-EBUSY);
  279. return NULL;
  280. }
  281. }
  282. init_completion(&STp->wait);
  283. SRpnt->sr_use_sg = (bytes > (STp->buffer)->sg[0].length) ?
  284. (STp->buffer)->use_sg : 0;
  285. if (SRpnt->sr_use_sg) {
  286. bp = (char *)&(STp->buffer->sg[0]);
  287. if (STp->buffer->sg_segs < SRpnt->sr_use_sg)
  288. SRpnt->sr_use_sg = STp->buffer->sg_segs;
  289. }
  290. else
  291. bp = (STp->buffer)->b_data;
  292. SRpnt->sr_data_direction = direction;
  293. SRpnt->sr_cmd_len = 0;
  294. SRpnt->sr_request->waiting = &(STp->wait);
  295. SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
  296. SRpnt->sr_request->rq_disk = STp->drive;
  297. scsi_do_req(SRpnt, (void *)cmd, bp, bytes, osst_sleep_done, timeout, retries);
  298. if (do_wait) {
  299. wait_for_completion(SRpnt->sr_request->waiting);
  300. SRpnt->sr_request->waiting = NULL;
  301. STp->buffer->syscall_result = osst_chk_result(STp, SRpnt);
  302. #ifdef OSST_INJECT_ERRORS
  303. if (STp->buffer->syscall_result == 0 &&
  304. cmd[0] == READ_6 &&
  305. cmd[4] &&
  306. ( (++ inject % 83) == 29 ||
  307. (STp->first_frame_position == 240
  308. /* or STp->read_error_frame to fail again on the block calculated above */ &&
  309. ++repeat < 3))) {
  310. printk(OSST_DEB_MSG "%s:D: Injecting read error\n", tape_name(STp));
  311. STp->buffer->last_result_fatal = 1;
  312. }
  313. #endif
  314. }
  315. return SRpnt;
  316. }
  317. /* Handle the write-behind checking (downs the semaphore) */
  318. static void osst_write_behind_check(struct osst_tape *STp)
  319. {
  320. struct osst_buffer * STbuffer;
  321. STbuffer = STp->buffer;
  322. #if DEBUG
  323. if (STp->write_pending)
  324. STp->nbr_waits++;
  325. else
  326. STp->nbr_finished++;
  327. #endif
  328. wait_for_completion(&(STp->wait));
  329. (STp->buffer)->last_SRpnt->sr_request->waiting = NULL;
  330. STp->buffer->syscall_result = osst_chk_result(STp, STp->buffer->last_SRpnt);
  331. if ((STp->buffer)->syscall_result)
  332. (STp->buffer)->syscall_result =
  333. osst_write_error_recovery(STp, &((STp->buffer)->last_SRpnt), 1);
  334. else
  335. STp->first_frame_position++;
  336. scsi_release_request((STp->buffer)->last_SRpnt);
  337. if (STbuffer->writing < STbuffer->buffer_bytes)
  338. printk(KERN_WARNING "osst :A: write_behind_check: something left in buffer!\n");
  339. STbuffer->buffer_bytes -= STbuffer->writing;
  340. STbuffer->writing = 0;
  341. return;
  342. }
  343. /* Onstream specific Routines */
  344. /*
  345. * Initialize the OnStream AUX
  346. */
  347. static void osst_init_aux(struct osst_tape * STp, int frame_type, int frame_seq_number,
  348. int logical_blk_num, int blk_sz, int blk_cnt)
  349. {
  350. os_aux_t *aux = STp->buffer->aux;
  351. os_partition_t *par = &aux->partition;
  352. os_dat_t *dat = &aux->dat;
  353. if (STp->raw) return;
  354. memset(aux, 0, sizeof(*aux));
  355. aux->format_id = htonl(0);
  356. memcpy(aux->application_sig, "LIN4", 4);
  357. aux->hdwr = htonl(0);
  358. aux->frame_type = frame_type;
  359. switch (frame_type) {
  360. case OS_FRAME_TYPE_HEADER:
  361. aux->update_frame_cntr = htonl(STp->update_frame_cntr);
  362. par->partition_num = OS_CONFIG_PARTITION;
  363. par->par_desc_ver = OS_PARTITION_VERSION;
  364. par->wrt_pass_cntr = htons(0xffff);
  365. /* 0-4 = reserved, 5-9 = header, 2990-2994 = header, 2995-2999 = reserved */
  366. par->first_frame_ppos = htonl(0);
  367. par->last_frame_ppos = htonl(0xbb7);
  368. aux->frame_seq_num = htonl(0);
  369. aux->logical_blk_num_high = htonl(0);
  370. aux->logical_blk_num = htonl(0);
  371. aux->next_mark_ppos = htonl(STp->first_mark_ppos);
  372. break;
  373. case OS_FRAME_TYPE_DATA:
  374. case OS_FRAME_TYPE_MARKER:
  375. dat->dat_sz = 8;
  376. dat->reserved1 = 0;
  377. dat->entry_cnt = 1;
  378. dat->reserved3 = 0;
  379. dat->dat_list[0].blk_sz = htonl(blk_sz);
  380. dat->dat_list[0].blk_cnt = htons(blk_cnt);
  381. dat->dat_list[0].flags = frame_type==OS_FRAME_TYPE_MARKER?
  382. OS_DAT_FLAGS_MARK:OS_DAT_FLAGS_DATA;
  383. dat->dat_list[0].reserved = 0;
  384. case OS_FRAME_TYPE_EOD:
  385. aux->update_frame_cntr = htonl(0);
  386. par->partition_num = OS_DATA_PARTITION;
  387. par->par_desc_ver = OS_PARTITION_VERSION;
  388. par->wrt_pass_cntr = htons(STp->wrt_pass_cntr);
  389. par->first_frame_ppos = htonl(STp->first_data_ppos);
  390. par->last_frame_ppos = htonl(STp->capacity);
  391. aux->frame_seq_num = htonl(frame_seq_number);
  392. aux->logical_blk_num_high = htonl(0);
  393. aux->logical_blk_num = htonl(logical_blk_num);
  394. break;
  395. default: ; /* probably FILL */
  396. }
  397. aux->filemark_cnt = ntohl(STp->filemark_cnt);
  398. aux->phys_fm = ntohl(0xffffffff);
  399. aux->last_mark_ppos = ntohl(STp->last_mark_ppos);
  400. aux->last_mark_lbn = ntohl(STp->last_mark_lbn);
  401. }
  402. /*
  403. * Verify that we have the correct tape frame
  404. */
  405. static int osst_verify_frame(struct osst_tape * STp, int frame_seq_number, int quiet)
  406. {
  407. char * name = tape_name(STp);
  408. os_aux_t * aux = STp->buffer->aux;
  409. os_partition_t * par = &(aux->partition);
  410. struct st_partstat * STps = &(STp->ps[STp->partition]);
  411. int blk_cnt, blk_sz, i;
  412. if (STp->raw) {
  413. if (STp->buffer->syscall_result) {
  414. for (i=0; i < STp->buffer->sg_segs; i++)
  415. memset(page_address(STp->buffer->sg[i].page),
  416. 0, STp->buffer->sg[i].length);
  417. strcpy(STp->buffer->b_data, "READ ERROR ON FRAME");
  418. } else
  419. STp->buffer->buffer_bytes = OS_FRAME_SIZE;
  420. return 1;
  421. }
  422. if (STp->buffer->syscall_result) {
  423. #if DEBUG
  424. printk(OSST_DEB_MSG "%s:D: Skipping frame, read error\n", name);
  425. #endif
  426. return 0;
  427. }
  428. if (ntohl(aux->format_id) != 0) {
  429. #if DEBUG
  430. printk(OSST_DEB_MSG "%s:D: Skipping frame, format_id %u\n", name, ntohl(aux->format_id));
  431. #endif
  432. goto err_out;
  433. }
  434. if (memcmp(aux->application_sig, STp->application_sig, 4) != 0 &&
  435. (memcmp(aux->application_sig, "LIN3", 4) != 0 || STp->linux_media_version != 4)) {
  436. #if DEBUG
  437. printk(OSST_DEB_MSG "%s:D: Skipping frame, incorrect application signature\n", name);
  438. #endif
  439. goto err_out;
  440. }
  441. if (par->partition_num != OS_DATA_PARTITION) {
  442. if (!STp->linux_media || STp->linux_media_version != 2) {
  443. #if DEBUG
  444. printk(OSST_DEB_MSG "%s:D: Skipping frame, partition num %d\n",
  445. name, par->partition_num);
  446. #endif
  447. goto err_out;
  448. }
  449. }
  450. if (par->par_desc_ver != OS_PARTITION_VERSION) {
  451. #if DEBUG
  452. printk(OSST_DEB_MSG "%s:D: Skipping frame, partition version %d\n", name, par->par_desc_ver);
  453. #endif
  454. goto err_out;
  455. }
  456. if (ntohs(par->wrt_pass_cntr) != STp->wrt_pass_cntr) {
  457. #if DEBUG
  458. printk(OSST_DEB_MSG "%s:D: Skipping frame, wrt_pass_cntr %d (expected %d)\n",
  459. name, ntohs(par->wrt_pass_cntr), STp->wrt_pass_cntr);
  460. #endif
  461. goto err_out;
  462. }
  463. if (aux->frame_type != OS_FRAME_TYPE_DATA &&
  464. aux->frame_type != OS_FRAME_TYPE_EOD &&
  465. aux->frame_type != OS_FRAME_TYPE_MARKER) {
  466. if (!quiet)
  467. #if DEBUG
  468. printk(OSST_DEB_MSG "%s:D: Skipping frame, frame type %x\n", name, aux->frame_type);
  469. #endif
  470. goto err_out;
  471. }
  472. if (aux->frame_type == OS_FRAME_TYPE_EOD &&
  473. STp->first_frame_position < STp->eod_frame_ppos) {
  474. printk(KERN_INFO "%s:I: Skipping premature EOD frame %d\n", name,
  475. STp->first_frame_position);
  476. goto err_out;
  477. }
  478. if (frame_seq_number != -1 && ntohl(aux->frame_seq_num) != frame_seq_number) {
  479. if (!quiet)
  480. #if DEBUG
  481. printk(OSST_DEB_MSG "%s:D: Skipping frame, sequence number %u (expected %d)\n",
  482. name, ntohl(aux->frame_seq_num), frame_seq_number);
  483. #endif
  484. goto err_out;
  485. }
  486. if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
  487. STps->eof = ST_FM_HIT;
  488. i = ntohl(aux->filemark_cnt);
  489. if (STp->header_cache != NULL && i < OS_FM_TAB_MAX && (i > STp->filemark_cnt ||
  490. STp->first_frame_position - 1 != ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[i]))) {
  491. #if DEBUG
  492. printk(OSST_DEB_MSG "%s:D: %s filemark %d at frame pos %d\n", name,
  493. STp->header_cache->dat_fm_tab.fm_tab_ent[i] == 0?"Learned":"Corrected",
  494. i, STp->first_frame_position - 1);
  495. #endif
  496. STp->header_cache->dat_fm_tab.fm_tab_ent[i] = htonl(STp->first_frame_position - 1);
  497. if (i >= STp->filemark_cnt)
  498. STp->filemark_cnt = i+1;
  499. }
  500. }
  501. if (aux->frame_type == OS_FRAME_TYPE_EOD) {
  502. STps->eof = ST_EOD_1;
  503. STp->frame_in_buffer = 1;
  504. }
  505. if (aux->frame_type == OS_FRAME_TYPE_DATA) {
  506. blk_cnt = ntohs(aux->dat.dat_list[0].blk_cnt);
  507. blk_sz = ntohl(aux->dat.dat_list[0].blk_sz);
  508. STp->buffer->buffer_bytes = blk_cnt * blk_sz;
  509. STp->buffer->read_pointer = 0;
  510. STp->frame_in_buffer = 1;
  511. /* See what block size was used to write file */
  512. if (STp->block_size != blk_sz && blk_sz > 0) {
  513. printk(KERN_INFO
  514. "%s:I: File was written with block size %d%c, currently %d%c, adjusted to match.\n",
  515. name, blk_sz<1024?blk_sz:blk_sz/1024,blk_sz<1024?'b':'k',
  516. STp->block_size<1024?STp->block_size:STp->block_size/1024,
  517. STp->block_size<1024?'b':'k');
  518. STp->block_size = blk_sz;
  519. STp->buffer->buffer_blocks = OS_DATA_SIZE / blk_sz;
  520. }
  521. STps->eof = ST_NOEOF;
  522. }
  523. STp->frame_seq_number = ntohl(aux->frame_seq_num);
  524. STp->logical_blk_num = ntohl(aux->logical_blk_num);
  525. return 1;
  526. err_out:
  527. if (STp->read_error_frame == 0)
  528. STp->read_error_frame = STp->first_frame_position - 1;
  529. return 0;
  530. }
  531. /*
  532. * Wait for the unit to become Ready
  533. */
  534. static int osst_wait_ready(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  535. unsigned timeout, int initial_delay)
  536. {
  537. unsigned char cmd[MAX_COMMAND_SIZE];
  538. struct scsi_request * SRpnt;
  539. unsigned long startwait = jiffies;
  540. #if DEBUG
  541. int dbg = debugging;
  542. char * name = tape_name(STp);
  543. printk(OSST_DEB_MSG "%s:D: Reached onstream wait ready\n", name);
  544. #endif
  545. if (initial_delay > 0)
  546. msleep(jiffies_to_msecs(initial_delay));
  547. memset(cmd, 0, MAX_COMMAND_SIZE);
  548. cmd[0] = TEST_UNIT_READY;
  549. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  550. *aSRpnt = SRpnt;
  551. if (!SRpnt) return (-EBUSY);
  552. while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
  553. (( SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 4 &&
  554. (SRpnt->sr_sense_buffer[13] == 1 || SRpnt->sr_sense_buffer[13] == 8) ) ||
  555. ( SRpnt->sr_sense_buffer[2] == 6 && SRpnt->sr_sense_buffer[12] == 0x28 &&
  556. SRpnt->sr_sense_buffer[13] == 0 ) )) {
  557. #if DEBUG
  558. if (debugging) {
  559. printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait ready\n", name);
  560. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  561. debugging = 0;
  562. }
  563. #endif
  564. msleep(100);
  565. memset(cmd, 0, MAX_COMMAND_SIZE);
  566. cmd[0] = TEST_UNIT_READY;
  567. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  568. }
  569. *aSRpnt = SRpnt;
  570. #if DEBUG
  571. debugging = dbg;
  572. #endif
  573. if ( STp->buffer->syscall_result &&
  574. osst_write_error_recovery(STp, aSRpnt, 0) ) {
  575. #if DEBUG
  576. printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait ready\n", name);
  577. printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
  578. STp->buffer->syscall_result, SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[2],
  579. SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
  580. #endif
  581. return (-EIO);
  582. }
  583. #if DEBUG
  584. printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait ready\n", name);
  585. #endif
  586. return 0;
  587. }
  588. /*
  589. * Wait for a tape to be inserted in the unit
  590. */
  591. static int osst_wait_for_medium(struct osst_tape * STp, struct scsi_request ** aSRpnt, unsigned timeout)
  592. {
  593. unsigned char cmd[MAX_COMMAND_SIZE];
  594. struct scsi_request * SRpnt;
  595. unsigned long startwait = jiffies;
  596. #if DEBUG
  597. int dbg = debugging;
  598. char * name = tape_name(STp);
  599. printk(OSST_DEB_MSG "%s:D: Reached onstream wait for medium\n", name);
  600. #endif
  601. memset(cmd, 0, MAX_COMMAND_SIZE);
  602. cmd[0] = TEST_UNIT_READY;
  603. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  604. *aSRpnt = SRpnt;
  605. if (!SRpnt) return (-EBUSY);
  606. while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
  607. SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 0x3a &&
  608. SRpnt->sr_sense_buffer[13] == 0 ) {
  609. #if DEBUG
  610. if (debugging) {
  611. printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait medium\n", name);
  612. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  613. debugging = 0;
  614. }
  615. #endif
  616. msleep(100);
  617. memset(cmd, 0, MAX_COMMAND_SIZE);
  618. cmd[0] = TEST_UNIT_READY;
  619. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  620. }
  621. *aSRpnt = SRpnt;
  622. #if DEBUG
  623. debugging = dbg;
  624. #endif
  625. if ( STp->buffer->syscall_result && SRpnt->sr_sense_buffer[2] != 2 &&
  626. SRpnt->sr_sense_buffer[12] != 4 && SRpnt->sr_sense_buffer[13] == 1) {
  627. #if DEBUG
  628. printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait medium\n", name);
  629. printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
  630. STp->buffer->syscall_result, SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[2],
  631. SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
  632. #endif
  633. return 0;
  634. }
  635. #if DEBUG
  636. printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait medium\n", name);
  637. #endif
  638. return 1;
  639. }
  640. static int osst_position_tape_and_confirm(struct osst_tape * STp, struct scsi_request ** aSRpnt, int frame)
  641. {
  642. int retval;
  643. osst_wait_ready(STp, aSRpnt, 15 * 60, 0); /* TODO - can this catch a write error? */
  644. retval = osst_set_frame_position(STp, aSRpnt, frame, 0);
  645. if (retval) return (retval);
  646. osst_wait_ready(STp, aSRpnt, 15 * 60, OSST_WAIT_POSITION_COMPLETE);
  647. return (osst_get_frame_position(STp, aSRpnt));
  648. }
  649. /*
  650. * Wait for write(s) to complete
  651. */
  652. static int osst_flush_drive_buffer(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  653. {
  654. unsigned char cmd[MAX_COMMAND_SIZE];
  655. struct scsi_request * SRpnt;
  656. int result = 0;
  657. int delay = OSST_WAIT_WRITE_COMPLETE;
  658. #if DEBUG
  659. char * name = tape_name(STp);
  660. printk(OSST_DEB_MSG "%s:D: Reached onstream flush drive buffer (write filemark)\n", name);
  661. #endif
  662. memset(cmd, 0, MAX_COMMAND_SIZE);
  663. cmd[0] = WRITE_FILEMARKS;
  664. cmd[1] = 1;
  665. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  666. *aSRpnt = SRpnt;
  667. if (!SRpnt) return (-EBUSY);
  668. if (STp->buffer->syscall_result) {
  669. if ((SRpnt->sr_sense_buffer[2] & 0x0f) == 2 && SRpnt->sr_sense_buffer[12] == 4) {
  670. if (SRpnt->sr_sense_buffer[13] == 8) {
  671. delay = OSST_WAIT_LONG_WRITE_COMPLETE;
  672. }
  673. } else
  674. result = osst_write_error_recovery(STp, aSRpnt, 0);
  675. }
  676. result |= osst_wait_ready(STp, aSRpnt, 5 * 60, delay);
  677. STp->ps[STp->partition].rw = OS_WRITING_COMPLETE;
  678. return (result);
  679. }
  680. #define OSST_POLL_PER_SEC 10
  681. static int osst_wait_frame(struct osst_tape * STp, struct scsi_request ** aSRpnt, int curr, int minlast, int to)
  682. {
  683. unsigned long startwait = jiffies;
  684. char * name = tape_name(STp);
  685. #if DEBUG
  686. char notyetprinted = 1;
  687. #endif
  688. if (minlast >= 0 && STp->ps[STp->partition].rw != ST_READING)
  689. printk(KERN_ERR "%s:A: Waiting for frame without having initialized read!\n", name);
  690. while (time_before (jiffies, startwait + to*HZ))
  691. {
  692. int result;
  693. result = osst_get_frame_position(STp, aSRpnt);
  694. if (result == -EIO)
  695. if ((result = osst_write_error_recovery(STp, aSRpnt, 0)) == 0)
  696. return 0; /* successful recovery leaves drive ready for frame */
  697. if (result < 0) break;
  698. if (STp->first_frame_position == curr &&
  699. ((minlast < 0 &&
  700. (signed)STp->last_frame_position > (signed)curr + minlast) ||
  701. (minlast >= 0 && STp->cur_frames > minlast)
  702. ) && result >= 0)
  703. {
  704. #if DEBUG
  705. if (debugging || jiffies - startwait >= 2*HZ/OSST_POLL_PER_SEC)
  706. printk (OSST_DEB_MSG
  707. "%s:D: Succ wait f fr %i (>%i): %i-%i %i (%i): %3li.%li s\n",
  708. name, curr, curr+minlast, STp->first_frame_position,
  709. STp->last_frame_position, STp->cur_frames,
  710. result, (jiffies-startwait)/HZ,
  711. (((jiffies-startwait)%HZ)*10)/HZ);
  712. #endif
  713. return 0;
  714. }
  715. #if DEBUG
  716. if (jiffies - startwait >= 2*HZ/OSST_POLL_PER_SEC && notyetprinted)
  717. {
  718. printk (OSST_DEB_MSG "%s:D: Wait for frame %i (>%i): %i-%i %i (%i)\n",
  719. name, curr, curr+minlast, STp->first_frame_position,
  720. STp->last_frame_position, STp->cur_frames, result);
  721. notyetprinted--;
  722. }
  723. #endif
  724. msleep(1000 / OSST_POLL_PER_SEC);
  725. }
  726. #if DEBUG
  727. printk (OSST_DEB_MSG "%s:D: Fail wait f fr %i (>%i): %i-%i %i: %3li.%li s\n",
  728. name, curr, curr+minlast, STp->first_frame_position,
  729. STp->last_frame_position, STp->cur_frames,
  730. (jiffies-startwait)/HZ, (((jiffies-startwait)%HZ)*10)/HZ);
  731. #endif
  732. return -EBUSY;
  733. }
  734. static int osst_recover_wait_frame(struct osst_tape * STp, struct scsi_request ** aSRpnt, int writing)
  735. {
  736. struct scsi_request * SRpnt;
  737. unsigned char cmd[MAX_COMMAND_SIZE];
  738. unsigned long startwait = jiffies;
  739. int retval = 1;
  740. char * name = tape_name(STp);
  741. if (writing) {
  742. char mybuf[24];
  743. char * olddata = STp->buffer->b_data;
  744. int oldsize = STp->buffer->buffer_size;
  745. /* write zero fm then read pos - if shows write error, try to recover - if no progress, wait */
  746. memset(cmd, 0, MAX_COMMAND_SIZE);
  747. cmd[0] = WRITE_FILEMARKS;
  748. cmd[1] = 1;
  749. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout,
  750. MAX_RETRIES, 1);
  751. while (retval && time_before (jiffies, startwait + 5*60*HZ)) {
  752. if (STp->buffer->syscall_result && (SRpnt->sr_sense_buffer[2] & 0x0f) != 2) {
  753. /* some failure - not just not-ready */
  754. retval = osst_write_error_recovery(STp, aSRpnt, 0);
  755. break;
  756. }
  757. set_current_state(TASK_INTERRUPTIBLE);
  758. schedule_timeout (HZ / OSST_POLL_PER_SEC);
  759. STp->buffer->b_data = mybuf; STp->buffer->buffer_size = 24;
  760. memset(cmd, 0, MAX_COMMAND_SIZE);
  761. cmd[0] = READ_POSITION;
  762. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 20, DMA_FROM_DEVICE, STp->timeout,
  763. MAX_RETRIES, 1);
  764. retval = ( STp->buffer->syscall_result || (STp->buffer)->b_data[15] > 25 );
  765. STp->buffer->b_data = olddata; STp->buffer->buffer_size = oldsize;
  766. }
  767. if (retval)
  768. printk(KERN_ERR "%s:E: Device did not succeed to write buffered data\n", name);
  769. } else
  770. /* TODO - figure out which error conditions can be handled */
  771. if (STp->buffer->syscall_result)
  772. printk(KERN_WARNING
  773. "%s:W: Recover_wait_frame(read) cannot handle %02x:%02x:%02x\n", name,
  774. (*aSRpnt)->sr_sense_buffer[ 2] & 0x0f,
  775. (*aSRpnt)->sr_sense_buffer[12],
  776. (*aSRpnt)->sr_sense_buffer[13]);
  777. return retval;
  778. }
  779. /*
  780. * Read the next OnStream tape frame at the current location
  781. */
  782. static int osst_read_frame(struct osst_tape * STp, struct scsi_request ** aSRpnt, int timeout)
  783. {
  784. unsigned char cmd[MAX_COMMAND_SIZE];
  785. struct scsi_request * SRpnt;
  786. int retval = 0;
  787. #if DEBUG
  788. os_aux_t * aux = STp->buffer->aux;
  789. char * name = tape_name(STp);
  790. #endif
  791. if (STp->poll)
  792. if (osst_wait_frame (STp, aSRpnt, STp->first_frame_position, 0, timeout))
  793. retval = osst_recover_wait_frame(STp, aSRpnt, 0);
  794. memset(cmd, 0, MAX_COMMAND_SIZE);
  795. cmd[0] = READ_6;
  796. cmd[1] = 1;
  797. cmd[4] = 1;
  798. #if DEBUG
  799. if (debugging)
  800. printk(OSST_DEB_MSG "%s:D: Reading frame from OnStream tape\n", name);
  801. #endif
  802. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, DMA_FROM_DEVICE,
  803. STp->timeout, MAX_RETRIES, 1);
  804. *aSRpnt = SRpnt;
  805. if (!SRpnt)
  806. return (-EBUSY);
  807. if ((STp->buffer)->syscall_result) {
  808. retval = 1;
  809. if (STp->read_error_frame == 0) {
  810. STp->read_error_frame = STp->first_frame_position;
  811. #if DEBUG
  812. printk(OSST_DEB_MSG "%s:D: Recording read error at %d\n", name, STp->read_error_frame);
  813. #endif
  814. }
  815. #if DEBUG
  816. if (debugging)
  817. printk(OSST_DEB_MSG "%s:D: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
  818. name,
  819. SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
  820. SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
  821. SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
  822. SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]);
  823. #endif
  824. }
  825. else
  826. STp->first_frame_position++;
  827. #if DEBUG
  828. if (debugging) {
  829. char sig[8]; int i;
  830. for (i=0;i<4;i++)
  831. sig[i] = aux->application_sig[i]<32?'^':aux->application_sig[i];
  832. sig[4] = '\0';
  833. printk(OSST_DEB_MSG
  834. "%s:D: AUX: %s UpdFrCt#%d Wpass#%d %s FrSeq#%d LogBlk#%d Qty=%d Sz=%d\n", name, sig,
  835. ntohl(aux->update_frame_cntr), ntohs(aux->partition.wrt_pass_cntr),
  836. aux->frame_type==1?"EOD":aux->frame_type==2?"MARK":
  837. aux->frame_type==8?"HEADR":aux->frame_type==0x80?"DATA":"FILL",
  838. ntohl(aux->frame_seq_num), ntohl(aux->logical_blk_num),
  839. ntohs(aux->dat.dat_list[0].blk_cnt), ntohl(aux->dat.dat_list[0].blk_sz) );
  840. if (aux->frame_type==2)
  841. printk(OSST_DEB_MSG "%s:D: mark_cnt=%d, last_mark_ppos=%d, last_mark_lbn=%d\n", name,
  842. ntohl(aux->filemark_cnt), ntohl(aux->last_mark_ppos), ntohl(aux->last_mark_lbn));
  843. printk(OSST_DEB_MSG "%s:D: Exit read frame from OnStream tape with code %d\n", name, retval);
  844. }
  845. #endif
  846. return (retval);
  847. }
  848. static int osst_initiate_read(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  849. {
  850. struct st_partstat * STps = &(STp->ps[STp->partition]);
  851. struct scsi_request * SRpnt ;
  852. unsigned char cmd[MAX_COMMAND_SIZE];
  853. int retval = 0;
  854. char * name = tape_name(STp);
  855. if (STps->rw != ST_READING) { /* Initialize read operation */
  856. if (STps->rw == ST_WRITING || STp->dirty) {
  857. STp->write_type = OS_WRITE_DATA;
  858. osst_flush_write_buffer(STp, aSRpnt);
  859. osst_flush_drive_buffer(STp, aSRpnt);
  860. }
  861. STps->rw = ST_READING;
  862. STp->frame_in_buffer = 0;
  863. /*
  864. * Issue a read 0 command to get the OnStream drive
  865. * read frames into its buffer.
  866. */
  867. memset(cmd, 0, MAX_COMMAND_SIZE);
  868. cmd[0] = READ_6;
  869. cmd[1] = 1;
  870. #if DEBUG
  871. printk(OSST_DEB_MSG "%s:D: Start Read Ahead on OnStream tape\n", name);
  872. #endif
  873. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  874. *aSRpnt = SRpnt;
  875. if ((retval = STp->buffer->syscall_result))
  876. printk(KERN_WARNING "%s:W: Error starting read ahead\n", name);
  877. }
  878. return retval;
  879. }
  880. static int osst_get_logical_frame(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  881. int frame_seq_number, int quiet)
  882. {
  883. struct st_partstat * STps = &(STp->ps[STp->partition]);
  884. char * name = tape_name(STp);
  885. int cnt = 0,
  886. bad = 0,
  887. past = 0,
  888. x,
  889. position;
  890. /*
  891. * If we want just any frame (-1) and there is a frame in the buffer, return it
  892. */
  893. if (frame_seq_number == -1 && STp->frame_in_buffer) {
  894. #if DEBUG
  895. printk(OSST_DEB_MSG "%s:D: Frame %d still in buffer\n", name, STp->frame_seq_number);
  896. #endif
  897. return (STps->eof);
  898. }
  899. /*
  900. * Search and wait for the next logical tape frame
  901. */
  902. while (1) {
  903. if (cnt++ > 400) {
  904. printk(KERN_ERR "%s:E: Couldn't find logical frame %d, aborting\n",
  905. name, frame_seq_number);
  906. if (STp->read_error_frame) {
  907. osst_set_frame_position(STp, aSRpnt, STp->read_error_frame, 0);
  908. #if DEBUG
  909. printk(OSST_DEB_MSG "%s:D: Repositioning tape to bad frame %d\n",
  910. name, STp->read_error_frame);
  911. #endif
  912. STp->read_error_frame = 0;
  913. STp->abort_count++;
  914. }
  915. return (-EIO);
  916. }
  917. #if DEBUG
  918. if (debugging)
  919. printk(OSST_DEB_MSG "%s:D: Looking for frame %d, attempt %d\n",
  920. name, frame_seq_number, cnt);
  921. #endif
  922. if ( osst_initiate_read(STp, aSRpnt)
  923. || ( (!STp->frame_in_buffer) && osst_read_frame(STp, aSRpnt, 30) ) ) {
  924. if (STp->raw)
  925. return (-EIO);
  926. position = osst_get_frame_position(STp, aSRpnt);
  927. if (position >= 0xbae && position < 0xbb8)
  928. position = 0xbb8;
  929. else if (position > STp->eod_frame_ppos || ++bad == 10) {
  930. position = STp->read_error_frame - 1;
  931. bad = 0;
  932. }
  933. else {
  934. position += 29;
  935. cnt += 19;
  936. }
  937. #if DEBUG
  938. printk(OSST_DEB_MSG "%s:D: Bad frame detected, positioning tape to block %d\n",
  939. name, position);
  940. #endif
  941. osst_set_frame_position(STp, aSRpnt, position, 0);
  942. continue;
  943. }
  944. if (osst_verify_frame(STp, frame_seq_number, quiet))
  945. break;
  946. if (osst_verify_frame(STp, -1, quiet)) {
  947. x = ntohl(STp->buffer->aux->frame_seq_num);
  948. if (STp->fast_open) {
  949. printk(KERN_WARNING
  950. "%s:W: Found logical frame %d instead of %d after fast open\n",
  951. name, x, frame_seq_number);
  952. STp->header_ok = 0;
  953. STp->read_error_frame = 0;
  954. return (-EIO);
  955. }
  956. if (x > frame_seq_number) {
  957. if (++past > 3) {
  958. /* positioning backwards did not bring us to the desired frame */
  959. position = STp->read_error_frame - 1;
  960. }
  961. else {
  962. position = osst_get_frame_position(STp, aSRpnt)
  963. + frame_seq_number - x - 1;
  964. if (STp->first_frame_position >= 3000 && position < 3000)
  965. position -= 10;
  966. }
  967. #if DEBUG
  968. printk(OSST_DEB_MSG
  969. "%s:D: Found logical frame %d while looking for %d: back up %d\n",
  970. name, x, frame_seq_number,
  971. STp->first_frame_position - position);
  972. #endif
  973. osst_set_frame_position(STp, aSRpnt, position, 0);
  974. cnt += 10;
  975. }
  976. else
  977. past = 0;
  978. }
  979. if (osst_get_frame_position(STp, aSRpnt) == 0xbaf) {
  980. #if DEBUG
  981. printk(OSST_DEB_MSG "%s:D: Skipping config partition\n", name);
  982. #endif
  983. osst_set_frame_position(STp, aSRpnt, 0xbb8, 0);
  984. cnt--;
  985. }
  986. STp->frame_in_buffer = 0;
  987. }
  988. if (cnt > 1) {
  989. STp->recover_count++;
  990. STp->recover_erreg++;
  991. printk(KERN_WARNING "%s:I: Don't worry, Read error at position %d recovered\n",
  992. name, STp->read_error_frame);
  993. }
  994. STp->read_count++;
  995. #if DEBUG
  996. if (debugging || STps->eof)
  997. printk(OSST_DEB_MSG
  998. "%s:D: Exit get logical frame (%d=>%d) from OnStream tape with code %d\n",
  999. name, frame_seq_number, STp->frame_seq_number, STps->eof);
  1000. #endif
  1001. STp->fast_open = 0;
  1002. STp->read_error_frame = 0;
  1003. return (STps->eof);
  1004. }
  1005. static int osst_seek_logical_blk(struct osst_tape * STp, struct scsi_request ** aSRpnt, int logical_blk_num)
  1006. {
  1007. struct st_partstat * STps = &(STp->ps[STp->partition]);
  1008. char * name = tape_name(STp);
  1009. int retries = 0;
  1010. int frame_seq_estimate, ppos_estimate, move;
  1011. if (logical_blk_num < 0) logical_blk_num = 0;
  1012. #if DEBUG
  1013. printk(OSST_DEB_MSG "%s:D: Seeking logical block %d (now at %d, size %d%c)\n",
  1014. name, logical_blk_num, STp->logical_blk_num,
  1015. STp->block_size<1024?STp->block_size:STp->block_size/1024,
  1016. STp->block_size<1024?'b':'k');
  1017. #endif
  1018. /* Do we know where we are? */
  1019. if (STps->drv_block >= 0) {
  1020. move = logical_blk_num - STp->logical_blk_num;
  1021. if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
  1022. move /= (OS_DATA_SIZE / STp->block_size);
  1023. frame_seq_estimate = STp->frame_seq_number + move;
  1024. } else
  1025. frame_seq_estimate = logical_blk_num * STp->block_size / OS_DATA_SIZE;
  1026. if (frame_seq_estimate < 2980) ppos_estimate = frame_seq_estimate + 10;
  1027. else ppos_estimate = frame_seq_estimate + 20;
  1028. while (++retries < 10) {
  1029. if (ppos_estimate > STp->eod_frame_ppos-2) {
  1030. frame_seq_estimate += STp->eod_frame_ppos - 2 - ppos_estimate;
  1031. ppos_estimate = STp->eod_frame_ppos - 2;
  1032. }
  1033. if (frame_seq_estimate < 0) {
  1034. frame_seq_estimate = 0;
  1035. ppos_estimate = 10;
  1036. }
  1037. osst_set_frame_position(STp, aSRpnt, ppos_estimate, 0);
  1038. if (osst_get_logical_frame(STp, aSRpnt, frame_seq_estimate, 1) >= 0) {
  1039. /* we've located the estimated frame, now does it have our block? */
  1040. if (logical_blk_num < STp->logical_blk_num ||
  1041. logical_blk_num >= STp->logical_blk_num + ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt)) {
  1042. if (STps->eof == ST_FM_HIT)
  1043. move = logical_blk_num < STp->logical_blk_num? -2 : 1;
  1044. else {
  1045. move = logical_blk_num - STp->logical_blk_num;
  1046. if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
  1047. move /= (OS_DATA_SIZE / STp->block_size);
  1048. }
  1049. if (!move) move = logical_blk_num > STp->logical_blk_num ? 1 : -1;
  1050. #if DEBUG
  1051. printk(OSST_DEB_MSG
  1052. "%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d) move %d\n",
  1053. name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
  1054. STp->logical_blk_num, logical_blk_num, move);
  1055. #endif
  1056. frame_seq_estimate += move;
  1057. ppos_estimate += move;
  1058. continue;
  1059. } else {
  1060. STp->buffer->read_pointer = (logical_blk_num - STp->logical_blk_num) * STp->block_size;
  1061. STp->buffer->buffer_bytes -= STp->buffer->read_pointer;
  1062. STp->logical_blk_num = logical_blk_num;
  1063. #if DEBUG
  1064. printk(OSST_DEB_MSG
  1065. "%s:D: Seek success at ppos %d fsq %d in_buf %d, bytes %d, ptr %d*%d\n",
  1066. name, ppos_estimate, STp->frame_seq_number, STp->frame_in_buffer,
  1067. STp->buffer->buffer_bytes, STp->buffer->read_pointer / STp->block_size,
  1068. STp->block_size);
  1069. #endif
  1070. STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
  1071. if (STps->eof == ST_FM_HIT) {
  1072. STps->drv_file++;
  1073. STps->drv_block = 0;
  1074. } else {
  1075. STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
  1076. STp->logical_blk_num -
  1077. (STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
  1078. -1;
  1079. }
  1080. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
  1081. return 0;
  1082. }
  1083. }
  1084. if (osst_get_logical_frame(STp, aSRpnt, -1, 1) < 0)
  1085. goto error;
  1086. /* we are not yet at the estimated frame, adjust our estimate of its physical position */
  1087. #if DEBUG
  1088. printk(OSST_DEB_MSG "%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d)\n",
  1089. name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
  1090. STp->logical_blk_num, logical_blk_num);
  1091. #endif
  1092. if (frame_seq_estimate != STp->frame_seq_number)
  1093. ppos_estimate += frame_seq_estimate - STp->frame_seq_number;
  1094. else
  1095. break;
  1096. }
  1097. error:
  1098. printk(KERN_ERR "%s:E: Couldn't seek to logical block %d (at %d), %d retries\n",
  1099. name, logical_blk_num, STp->logical_blk_num, retries);
  1100. return (-EIO);
  1101. }
  1102. /* The values below are based on the OnStream frame payload size of 32K == 2**15,
  1103. * that is, OSST_FRAME_SHIFT + OSST_SECTOR_SHIFT must be 15. With a minimum block
  1104. * size of 512 bytes, we need to be able to resolve 32K/512 == 64 == 2**6 positions
  1105. * inside each frame. Finaly, OSST_SECTOR_MASK == 2**OSST_FRAME_SHIFT - 1.
  1106. */
  1107. #define OSST_FRAME_SHIFT 6
  1108. #define OSST_SECTOR_SHIFT 9
  1109. #define OSST_SECTOR_MASK 0x03F
  1110. static int osst_get_sector(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  1111. {
  1112. int sector;
  1113. #if DEBUG
  1114. char * name = tape_name(STp);
  1115. printk(OSST_DEB_MSG
  1116. "%s:D: Positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, %cptr %d, eof %d\n",
  1117. name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
  1118. STp->ps[STp->partition].drv_file, STp->ps[STp->partition].drv_block,
  1119. STp->ps[STp->partition].rw == ST_WRITING?'w':'r',
  1120. STp->ps[STp->partition].rw == ST_WRITING?STp->buffer->buffer_bytes:
  1121. STp->buffer->read_pointer, STp->ps[STp->partition].eof);
  1122. #endif
  1123. /* do we know where we are inside a file? */
  1124. if (STp->ps[STp->partition].drv_block >= 0) {
  1125. sector = (STp->frame_in_buffer ? STp->first_frame_position-1 :
  1126. STp->first_frame_position) << OSST_FRAME_SHIFT;
  1127. if (STp->ps[STp->partition].rw == ST_WRITING)
  1128. sector |= (STp->buffer->buffer_bytes >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
  1129. else
  1130. sector |= (STp->buffer->read_pointer >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
  1131. } else {
  1132. sector = osst_get_frame_position(STp, aSRpnt);
  1133. if (sector > 0)
  1134. sector <<= OSST_FRAME_SHIFT;
  1135. }
  1136. return sector;
  1137. }
  1138. static int osst_seek_sector(struct osst_tape * STp, struct scsi_request ** aSRpnt, int sector)
  1139. {
  1140. struct st_partstat * STps = &(STp->ps[STp->partition]);
  1141. int frame = sector >> OSST_FRAME_SHIFT,
  1142. offset = (sector & OSST_SECTOR_MASK) << OSST_SECTOR_SHIFT,
  1143. r;
  1144. #if DEBUG
  1145. char * name = tape_name(STp);
  1146. printk(OSST_DEB_MSG "%s:D: Seeking sector %d in frame %d at offset %d\n",
  1147. name, sector, frame, offset);
  1148. #endif
  1149. if (frame < 0 || frame >= STp->capacity) return (-ENXIO);
  1150. if (frame <= STp->first_data_ppos) {
  1151. STp->frame_seq_number = STp->logical_blk_num = STps->drv_file = STps->drv_block = 0;
  1152. return (osst_set_frame_position(STp, aSRpnt, frame, 0));
  1153. }
  1154. r = osst_set_frame_position(STp, aSRpnt, offset?frame:frame-1, 0);
  1155. if (r < 0) return r;
  1156. r = osst_get_logical_frame(STp, aSRpnt, -1, 1);
  1157. if (r < 0) return r;
  1158. if (osst_get_frame_position(STp, aSRpnt) != (offset?frame+1:frame)) return (-EIO);
  1159. if (offset) {
  1160. STp->logical_blk_num += offset / STp->block_size;
  1161. STp->buffer->read_pointer = offset;
  1162. STp->buffer->buffer_bytes -= offset;
  1163. } else {
  1164. STp->frame_seq_number++;
  1165. STp->frame_in_buffer = 0;
  1166. STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1167. STp->buffer->buffer_bytes = STp->buffer->read_pointer = 0;
  1168. }
  1169. STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
  1170. if (STps->eof == ST_FM_HIT) {
  1171. STps->drv_file++;
  1172. STps->drv_block = 0;
  1173. } else {
  1174. STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
  1175. STp->logical_blk_num -
  1176. (STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
  1177. -1;
  1178. }
  1179. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
  1180. #if DEBUG
  1181. printk(OSST_DEB_MSG
  1182. "%s:D: Now positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, rptr %d, eof %d\n",
  1183. name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
  1184. STps->drv_file, STps->drv_block, STp->buffer->read_pointer, STps->eof);
  1185. #endif
  1186. return 0;
  1187. }
  1188. /*
  1189. * Read back the drive's internal buffer contents, as a part
  1190. * of the write error recovery mechanism for old OnStream
  1191. * firmware revisions.
  1192. * Precondition for this function to work: all frames in the
  1193. * drive's buffer must be of one type (DATA, MARK or EOD)!
  1194. */
  1195. static int osst_read_back_buffer_and_rewrite(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  1196. unsigned int frame, unsigned int skip, int pending)
  1197. {
  1198. struct scsi_request * SRpnt = * aSRpnt;
  1199. unsigned char * buffer, * p;
  1200. unsigned char cmd[MAX_COMMAND_SIZE];
  1201. int flag, new_frame, i;
  1202. int nframes = STp->cur_frames;
  1203. int blks_per_frame = ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1204. int frame_seq_number = ntohl(STp->buffer->aux->frame_seq_num)
  1205. - (nframes + pending - 1);
  1206. int logical_blk_num = ntohl(STp->buffer->aux->logical_blk_num)
  1207. - (nframes + pending - 1) * blks_per_frame;
  1208. char * name = tape_name(STp);
  1209. unsigned long startwait = jiffies;
  1210. #if DEBUG
  1211. int dbg = debugging;
  1212. #endif
  1213. if ((buffer = (unsigned char *)vmalloc((nframes + 1) * OS_DATA_SIZE)) == NULL)
  1214. return (-EIO);
  1215. printk(KERN_INFO "%s:I: Reading back %d frames from drive buffer%s\n",
  1216. name, nframes, pending?" and one that was pending":"");
  1217. osst_copy_from_buffer(STp->buffer, (p = &buffer[nframes * OS_DATA_SIZE]));
  1218. #if DEBUG
  1219. if (pending && debugging)
  1220. printk(OSST_DEB_MSG "%s:D: Pending frame %d (lblk %d), data %02x %02x %02x %02x\n",
  1221. name, frame_seq_number + nframes,
  1222. logical_blk_num + nframes * blks_per_frame,
  1223. p[0], p[1], p[2], p[3]);
  1224. #endif
  1225. for (i = 0, p = buffer; i < nframes; i++, p += OS_DATA_SIZE) {
  1226. memset(cmd, 0, MAX_COMMAND_SIZE);
  1227. cmd[0] = 0x3C; /* Buffer Read */
  1228. cmd[1] = 6; /* Retrieve Faulty Block */
  1229. cmd[7] = 32768 >> 8;
  1230. cmd[8] = 32768 & 0xff;
  1231. SRpnt = osst_do_scsi(SRpnt, STp, cmd, OS_FRAME_SIZE, DMA_FROM_DEVICE,
  1232. STp->timeout, MAX_RETRIES, 1);
  1233. if ((STp->buffer)->syscall_result || !SRpnt) {
  1234. printk(KERN_ERR "%s:E: Failed to read frame back from OnStream buffer\n", name);
  1235. vfree(buffer);
  1236. *aSRpnt = SRpnt;
  1237. return (-EIO);
  1238. }
  1239. osst_copy_from_buffer(STp->buffer, p);
  1240. #if DEBUG
  1241. if (debugging)
  1242. printk(OSST_DEB_MSG "%s:D: Read back logical frame %d, data %02x %02x %02x %02x\n",
  1243. name, frame_seq_number + i, p[0], p[1], p[2], p[3]);
  1244. #endif
  1245. }
  1246. *aSRpnt = SRpnt;
  1247. osst_get_frame_position(STp, aSRpnt);
  1248. #if DEBUG
  1249. printk(OSST_DEB_MSG "%s:D: Frames left in buffer: %d\n", name, STp->cur_frames);
  1250. #endif
  1251. /* Write synchronously so we can be sure we're OK again and don't have to recover recursively */
  1252. /* In the header we don't actually re-write the frames that fail, just the ones after them */
  1253. for (flag=1, new_frame=frame, p=buffer, i=0; i < nframes + pending; ) {
  1254. if (flag) {
  1255. if (STp->write_type == OS_WRITE_HEADER) {
  1256. i += skip;
  1257. p += skip * OS_DATA_SIZE;
  1258. }
  1259. else if (new_frame < 2990 && new_frame+skip+nframes+pending >= 2990)
  1260. new_frame = 3000-i;
  1261. else
  1262. new_frame += skip;
  1263. #if DEBUG
  1264. printk(OSST_DEB_MSG "%s:D: Position to frame %d, write fseq %d\n",
  1265. name, new_frame+i, frame_seq_number+i);
  1266. #endif
  1267. osst_set_frame_position(STp, aSRpnt, new_frame + i, 0);
  1268. osst_wait_ready(STp, aSRpnt, 60, OSST_WAIT_POSITION_COMPLETE);
  1269. osst_get_frame_position(STp, aSRpnt);
  1270. SRpnt = * aSRpnt;
  1271. if (new_frame > frame + 1000) {
  1272. printk(KERN_ERR "%s:E: Failed to find writable tape media\n", name);
  1273. vfree(buffer);
  1274. return (-EIO);
  1275. }
  1276. if ( i >= nframes + pending ) break;
  1277. flag = 0;
  1278. }
  1279. osst_copy_to_buffer(STp->buffer, p);
  1280. /*
  1281. * IMPORTANT: for error recovery to work, _never_ queue frames with mixed frame type!
  1282. */
  1283. osst_init_aux(STp, STp->buffer->aux->frame_type, frame_seq_number+i,
  1284. logical_blk_num + i*blks_per_frame,
  1285. ntohl(STp->buffer->aux->dat.dat_list[0].blk_sz), blks_per_frame);
  1286. memset(cmd, 0, MAX_COMMAND_SIZE);
  1287. cmd[0] = WRITE_6;
  1288. cmd[1] = 1;
  1289. cmd[4] = 1;
  1290. #if DEBUG
  1291. if (debugging)
  1292. printk(OSST_DEB_MSG
  1293. "%s:D: About to write frame %d, seq %d, lbn %d, data %02x %02x %02x %02x\n",
  1294. name, new_frame+i, frame_seq_number+i, logical_blk_num + i*blks_per_frame,
  1295. p[0], p[1], p[2], p[3]);
  1296. #endif
  1297. SRpnt = osst_do_scsi(SRpnt, STp, cmd, OS_FRAME_SIZE, DMA_TO_DEVICE,
  1298. STp->timeout, MAX_RETRIES, 1);
  1299. if (STp->buffer->syscall_result)
  1300. flag = 1;
  1301. else {
  1302. p += OS_DATA_SIZE; i++;
  1303. /* if we just sent the last frame, wait till all successfully written */
  1304. if ( i == nframes + pending ) {
  1305. #if DEBUG
  1306. printk(OSST_DEB_MSG "%s:D: Check re-write successful\n", name);
  1307. #endif
  1308. memset(cmd, 0, MAX_COMMAND_SIZE);
  1309. cmd[0] = WRITE_FILEMARKS;
  1310. cmd[1] = 1;
  1311. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  1312. STp->timeout, MAX_RETRIES, 1);
  1313. #if DEBUG
  1314. if (debugging) {
  1315. printk(OSST_DEB_MSG "%s:D: Sleeping in re-write wait ready\n", name);
  1316. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  1317. debugging = 0;
  1318. }
  1319. #endif
  1320. flag = STp->buffer->syscall_result;
  1321. while ( !flag && time_before(jiffies, startwait + 60*HZ) ) {
  1322. memset(cmd, 0, MAX_COMMAND_SIZE);
  1323. cmd[0] = TEST_UNIT_READY;
  1324. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, STp->timeout,
  1325. MAX_RETRIES, 1);
  1326. if (SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 4 &&
  1327. (SRpnt->sr_sense_buffer[13] == 1 || SRpnt->sr_sense_buffer[13] == 8)) {
  1328. /* in the process of becoming ready */
  1329. msleep(100);
  1330. continue;
  1331. }
  1332. if (STp->buffer->syscall_result)
  1333. flag = 1;
  1334. break;
  1335. }
  1336. #if DEBUG
  1337. debugging = dbg;
  1338. printk(OSST_DEB_MSG "%s:D: Wait re-write finished\n", name);
  1339. #endif
  1340. }
  1341. }
  1342. *aSRpnt = SRpnt;
  1343. if (flag) {
  1344. if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) == 13 &&
  1345. SRpnt->sr_sense_buffer[12] == 0 &&
  1346. SRpnt->sr_sense_buffer[13] == 2) {
  1347. printk(KERN_ERR "%s:E: Volume overflow in write error recovery\n", name);
  1348. vfree(buffer);
  1349. return (-EIO); /* hit end of tape = fail */
  1350. }
  1351. i = ((SRpnt->sr_sense_buffer[3] << 24) |
  1352. (SRpnt->sr_sense_buffer[4] << 16) |
  1353. (SRpnt->sr_sense_buffer[5] << 8) |
  1354. SRpnt->sr_sense_buffer[6] ) - new_frame;
  1355. p = &buffer[i * OS_DATA_SIZE];
  1356. #if DEBUG
  1357. printk(OSST_DEB_MSG "%s:D: Additional write error at %d\n", name, new_frame+i);
  1358. #endif
  1359. osst_get_frame_position(STp, aSRpnt);
  1360. #if DEBUG
  1361. printk(OSST_DEB_MSG "%s:D: reported frame positions: host = %d, tape = %d, buffer = %d\n",
  1362. name, STp->first_frame_position, STp->last_frame_position, STp->cur_frames);
  1363. #endif
  1364. }
  1365. }
  1366. if (flag) {
  1367. /* error recovery did not successfully complete */
  1368. printk(KERN_ERR "%s:D: Write error recovery failed in %s\n", name,
  1369. STp->write_type == OS_WRITE_HEADER?"header":"body");
  1370. }
  1371. if (!pending)
  1372. osst_copy_to_buffer(STp->buffer, p); /* so buffer content == at entry in all cases */
  1373. vfree(buffer);
  1374. return 0;
  1375. }
  1376. static int osst_reposition_and_retry(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  1377. unsigned int frame, unsigned int skip, int pending)
  1378. {
  1379. unsigned char cmd[MAX_COMMAND_SIZE];
  1380. struct scsi_request * SRpnt;
  1381. char * name = tape_name(STp);
  1382. int expected = 0;
  1383. int attempts = 1000 / skip;
  1384. int flag = 1;
  1385. unsigned long startwait = jiffies;
  1386. #if DEBUG
  1387. int dbg = debugging;
  1388. #endif
  1389. while (attempts && time_before(jiffies, startwait + 60*HZ)) {
  1390. if (flag) {
  1391. #if DEBUG
  1392. debugging = dbg;
  1393. #endif
  1394. if (frame < 2990 && frame+skip+STp->cur_frames+pending >= 2990)
  1395. frame = 3000-skip;
  1396. expected = frame+skip+STp->cur_frames+pending;
  1397. #if DEBUG
  1398. printk(OSST_DEB_MSG "%s:D: Position to fppos %d, re-write from fseq %d\n",
  1399. name, frame+skip, STp->frame_seq_number-STp->cur_frames-pending);
  1400. #endif
  1401. osst_set_frame_position(STp, aSRpnt, frame + skip, 1);
  1402. flag = 0;
  1403. attempts--;
  1404. set_current_state(TASK_INTERRUPTIBLE);
  1405. schedule_timeout(HZ / 10);
  1406. }
  1407. if (osst_get_frame_position(STp, aSRpnt) < 0) { /* additional write error */
  1408. #if DEBUG
  1409. printk(OSST_DEB_MSG "%s:D: Addl error, host %d, tape %d, buffer %d\n",
  1410. name, STp->first_frame_position,
  1411. STp->last_frame_position, STp->cur_frames);
  1412. #endif
  1413. frame = STp->last_frame_position;
  1414. flag = 1;
  1415. continue;
  1416. }
  1417. if (pending && STp->cur_frames < 50) {
  1418. memset(cmd, 0, MAX_COMMAND_SIZE);
  1419. cmd[0] = WRITE_6;
  1420. cmd[1] = 1;
  1421. cmd[4] = 1;
  1422. #if DEBUG
  1423. printk(OSST_DEB_MSG "%s:D: About to write pending fseq %d at fppos %d\n",
  1424. name, STp->frame_seq_number-1, STp->first_frame_position);
  1425. #endif
  1426. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, DMA_TO_DEVICE,
  1427. STp->timeout, MAX_RETRIES, 1);
  1428. *aSRpnt = SRpnt;
  1429. if (STp->buffer->syscall_result) { /* additional write error */
  1430. if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) == 13 &&
  1431. SRpnt->sr_sense_buffer[12] == 0 &&
  1432. SRpnt->sr_sense_buffer[13] == 2) {
  1433. printk(KERN_ERR
  1434. "%s:E: Volume overflow in write error recovery\n",
  1435. name);
  1436. break; /* hit end of tape = fail */
  1437. }
  1438. flag = 1;
  1439. }
  1440. else
  1441. pending = 0;
  1442. continue;
  1443. }
  1444. if (STp->cur_frames == 0) {
  1445. #if DEBUG
  1446. debugging = dbg;
  1447. printk(OSST_DEB_MSG "%s:D: Wait re-write finished\n", name);
  1448. #endif
  1449. if (STp->first_frame_position != expected) {
  1450. printk(KERN_ERR "%s:A: Actual position %d - expected %d\n",
  1451. name, STp->first_frame_position, expected);
  1452. return (-EIO);
  1453. }
  1454. return 0;
  1455. }
  1456. #if DEBUG
  1457. if (debugging) {
  1458. printk(OSST_DEB_MSG "%s:D: Sleeping in re-write wait ready\n", name);
  1459. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  1460. debugging = 0;
  1461. }
  1462. #endif
  1463. set_current_state(TASK_INTERRUPTIBLE);
  1464. schedule_timeout(HZ / 10);
  1465. }
  1466. printk(KERN_ERR "%s:E: Failed to find valid tape media\n", name);
  1467. #if DEBUG
  1468. debugging = dbg;
  1469. #endif
  1470. return (-EIO);
  1471. }
  1472. /*
  1473. * Error recovery algorithm for the OnStream tape.
  1474. */
  1475. static int osst_write_error_recovery(struct osst_tape * STp, struct scsi_request ** aSRpnt, int pending)
  1476. {
  1477. struct scsi_request * SRpnt = * aSRpnt;
  1478. struct st_partstat * STps = & STp->ps[STp->partition];
  1479. char * name = tape_name(STp);
  1480. int retval = 0;
  1481. int rw_state;
  1482. unsigned int frame, skip;
  1483. rw_state = STps->rw;
  1484. if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) != 3
  1485. || SRpnt->sr_sense_buffer[12] != 12
  1486. || SRpnt->sr_sense_buffer[13] != 0) {
  1487. #if DEBUG
  1488. printk(OSST_DEB_MSG "%s:D: Write error recovery cannot handle %02x:%02x:%02x\n", name,
  1489. SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
  1490. #endif
  1491. return (-EIO);
  1492. }
  1493. frame = (SRpnt->sr_sense_buffer[3] << 24) |
  1494. (SRpnt->sr_sense_buffer[4] << 16) |
  1495. (SRpnt->sr_sense_buffer[5] << 8) |
  1496. SRpnt->sr_sense_buffer[6];
  1497. skip = SRpnt->sr_sense_buffer[9];
  1498. #if DEBUG
  1499. printk(OSST_DEB_MSG "%s:D: Detected physical bad frame at %u, advised to skip %d\n", name, frame, skip);
  1500. #endif
  1501. osst_get_frame_position(STp, aSRpnt);
  1502. #if DEBUG
  1503. printk(OSST_DEB_MSG "%s:D: reported frame positions: host = %d, tape = %d\n",
  1504. name, STp->first_frame_position, STp->last_frame_position);
  1505. #endif
  1506. switch (STp->write_type) {
  1507. case OS_WRITE_DATA:
  1508. case OS_WRITE_EOD:
  1509. case OS_WRITE_NEW_MARK:
  1510. printk(KERN_WARNING
  1511. "%s:I: Relocating %d buffered logical frames from position %u to %u\n",
  1512. name, STp->cur_frames, frame, (frame + skip > 3000 && frame < 3000)?3000:frame + skip);
  1513. if (STp->os_fw_rev >= 10600)
  1514. retval = osst_reposition_and_retry(STp, aSRpnt, frame, skip, pending);
  1515. else
  1516. retval = osst_read_back_buffer_and_rewrite(STp, aSRpnt, frame, skip, pending);
  1517. printk(KERN_WARNING "%s:%s: %sWrite error%srecovered\n", name,
  1518. retval?"E" :"I",
  1519. retval?"" :"Don't worry, ",
  1520. retval?" not ":" ");
  1521. break;
  1522. case OS_WRITE_LAST_MARK:
  1523. printk(KERN_ERR "%s:E: Bad frame in update last marker, fatal\n", name);
  1524. osst_set_frame_position(STp, aSRpnt, frame + STp->cur_frames + pending, 0);
  1525. retval = -EIO;
  1526. break;
  1527. case OS_WRITE_HEADER:
  1528. printk(KERN_WARNING "%s:I: Bad frame in header partition, skipped\n", name);
  1529. retval = osst_read_back_buffer_and_rewrite(STp, aSRpnt, frame, 1, pending);
  1530. break;
  1531. default:
  1532. printk(KERN_INFO "%s:I: Bad frame in filler, ignored\n", name);
  1533. osst_set_frame_position(STp, aSRpnt, frame + STp->cur_frames + pending, 0);
  1534. }
  1535. osst_get_frame_position(STp, aSRpnt);
  1536. #if DEBUG
  1537. printk(OSST_DEB_MSG "%s:D: Positioning complete, cur_frames %d, pos %d, tape pos %d\n",
  1538. name, STp->cur_frames, STp->first_frame_position, STp->last_frame_position);
  1539. printk(OSST_DEB_MSG "%s:D: next logical frame to write: %d\n", name, STp->logical_blk_num);
  1540. #endif
  1541. if (retval == 0) {
  1542. STp->recover_count++;
  1543. STp->recover_erreg++;
  1544. } else
  1545. STp->abort_count++;
  1546. STps->rw = rw_state;
  1547. return retval;
  1548. }
  1549. static int osst_space_over_filemarks_backward(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  1550. int mt_op, int mt_count)
  1551. {
  1552. char * name = tape_name(STp);
  1553. int cnt;
  1554. int last_mark_ppos = -1;
  1555. #if DEBUG
  1556. printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_backwards %d %d\n", name, mt_op, mt_count);
  1557. #endif
  1558. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1559. #if DEBUG
  1560. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_bwd\n", name);
  1561. #endif
  1562. return -EIO;
  1563. }
  1564. if (STp->linux_media_version >= 4) {
  1565. /*
  1566. * direct lookup in header filemark list
  1567. */
  1568. cnt = ntohl(STp->buffer->aux->filemark_cnt);
  1569. if (STp->header_ok &&
  1570. STp->header_cache != NULL &&
  1571. (cnt - mt_count) >= 0 &&
  1572. (cnt - mt_count) < OS_FM_TAB_MAX &&
  1573. (cnt - mt_count) < STp->filemark_cnt &&
  1574. STp->header_cache->dat_fm_tab.fm_tab_ent[cnt-1] == STp->buffer->aux->last_mark_ppos)
  1575. last_mark_ppos = ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt - mt_count]);
  1576. #if DEBUG
  1577. if (STp->header_cache == NULL || (cnt - mt_count) < 0 || (cnt - mt_count) >= OS_FM_TAB_MAX)
  1578. printk(OSST_DEB_MSG "%s:D: Filemark lookup fail due to %s\n", name,
  1579. STp->header_cache == NULL?"lack of header cache":"count out of range");
  1580. else
  1581. printk(OSST_DEB_MSG "%s:D: Filemark lookup: prev mark %d (%s), skip %d to %d\n",
  1582. name, cnt,
  1583. ((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
  1584. (STp->header_cache->dat_fm_tab.fm_tab_ent[cnt-1] ==
  1585. STp->buffer->aux->last_mark_ppos))?"match":"error",
  1586. mt_count, last_mark_ppos);
  1587. #endif
  1588. if (last_mark_ppos > 10 && last_mark_ppos < STp->eod_frame_ppos) {
  1589. osst_position_tape_and_confirm(STp, aSRpnt, last_mark_ppos);
  1590. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1591. #if DEBUG
  1592. printk(OSST_DEB_MSG
  1593. "%s:D: Couldn't get logical blk num in space_filemarks\n", name);
  1594. #endif
  1595. return (-EIO);
  1596. }
  1597. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1598. printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
  1599. name, last_mark_ppos);
  1600. return (-EIO);
  1601. }
  1602. goto found;
  1603. }
  1604. #if DEBUG
  1605. printk(OSST_DEB_MSG "%s:D: Reverting to scan filemark backwards\n", name);
  1606. #endif
  1607. }
  1608. cnt = 0;
  1609. while (cnt != mt_count) {
  1610. last_mark_ppos = ntohl(STp->buffer->aux->last_mark_ppos);
  1611. if (last_mark_ppos == -1)
  1612. return (-EIO);
  1613. #if DEBUG
  1614. printk(OSST_DEB_MSG "%s:D: Positioning to last mark at %d\n", name, last_mark_ppos);
  1615. #endif
  1616. osst_position_tape_and_confirm(STp, aSRpnt, last_mark_ppos);
  1617. cnt++;
  1618. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1619. #if DEBUG
  1620. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n", name);
  1621. #endif
  1622. return (-EIO);
  1623. }
  1624. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1625. printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
  1626. name, last_mark_ppos);
  1627. return (-EIO);
  1628. }
  1629. }
  1630. found:
  1631. if (mt_op == MTBSFM) {
  1632. STp->frame_seq_number++;
  1633. STp->frame_in_buffer = 0;
  1634. STp->buffer->buffer_bytes = 0;
  1635. STp->buffer->read_pointer = 0;
  1636. STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1637. }
  1638. return 0;
  1639. }
  1640. /*
  1641. * ADRL 1.1 compatible "slow" space filemarks fwd version
  1642. *
  1643. * Just scans for the filemark sequentially.
  1644. */
  1645. static int osst_space_over_filemarks_forward_slow(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  1646. int mt_op, int mt_count)
  1647. {
  1648. int cnt = 0;
  1649. #if DEBUG
  1650. char * name = tape_name(STp);
  1651. printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_forward_slow %d %d\n", name, mt_op, mt_count);
  1652. #endif
  1653. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1654. #if DEBUG
  1655. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_fwd\n", name);
  1656. #endif
  1657. return (-EIO);
  1658. }
  1659. while (1) {
  1660. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1661. #if DEBUG
  1662. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n", name);
  1663. #endif
  1664. return (-EIO);
  1665. }
  1666. if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_MARKER)
  1667. cnt++;
  1668. if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_EOD) {
  1669. #if DEBUG
  1670. printk(OSST_DEB_MSG "%s:D: space_fwd: EOD reached\n", name);
  1671. #endif
  1672. if (STp->first_frame_position > STp->eod_frame_ppos+1) {
  1673. #if DEBUG
  1674. printk(OSST_DEB_MSG "%s:D: EOD position corrected (%d=>%d)\n",
  1675. name, STp->eod_frame_ppos, STp->first_frame_position-1);
  1676. #endif
  1677. STp->eod_frame_ppos = STp->first_frame_position-1;
  1678. }
  1679. return (-EIO);
  1680. }
  1681. if (cnt == mt_count)
  1682. break;
  1683. STp->frame_in_buffer = 0;
  1684. }
  1685. if (mt_op == MTFSF) {
  1686. STp->frame_seq_number++;
  1687. STp->frame_in_buffer = 0;
  1688. STp->buffer->buffer_bytes = 0;
  1689. STp->buffer->read_pointer = 0;
  1690. STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1691. }
  1692. return 0;
  1693. }
  1694. /*
  1695. * Fast linux specific version of OnStream FSF
  1696. */
  1697. static int osst_space_over_filemarks_forward_fast(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  1698. int mt_op, int mt_count)
  1699. {
  1700. char * name = tape_name(STp);
  1701. int cnt = 0,
  1702. next_mark_ppos = -1;
  1703. #if DEBUG
  1704. printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_forward_fast %d %d\n", name, mt_op, mt_count);
  1705. #endif
  1706. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1707. #if DEBUG
  1708. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_fwd\n", name);
  1709. #endif
  1710. return (-EIO);
  1711. }
  1712. if (STp->linux_media_version >= 4) {
  1713. /*
  1714. * direct lookup in header filemark list
  1715. */
  1716. cnt = ntohl(STp->buffer->aux->filemark_cnt) - 1;
  1717. if (STp->header_ok &&
  1718. STp->header_cache != NULL &&
  1719. (cnt + mt_count) < OS_FM_TAB_MAX &&
  1720. (cnt + mt_count) < STp->filemark_cnt &&
  1721. ((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
  1722. (STp->header_cache->dat_fm_tab.fm_tab_ent[cnt] == STp->buffer->aux->last_mark_ppos)))
  1723. next_mark_ppos = ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt + mt_count]);
  1724. #if DEBUG
  1725. if (STp->header_cache == NULL || (cnt + mt_count) >= OS_FM_TAB_MAX)
  1726. printk(OSST_DEB_MSG "%s:D: Filemark lookup fail due to %s\n", name,
  1727. STp->header_cache == NULL?"lack of header cache":"count out of range");
  1728. else
  1729. printk(OSST_DEB_MSG "%s:D: Filemark lookup: prev mark %d (%s), skip %d to %d\n",
  1730. name, cnt,
  1731. ((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
  1732. (STp->header_cache->dat_fm_tab.fm_tab_ent[cnt] ==
  1733. STp->buffer->aux->last_mark_ppos))?"match":"error",
  1734. mt_count, next_mark_ppos);
  1735. #endif
  1736. if (next_mark_ppos <= 10 || next_mark_ppos > STp->eod_frame_ppos) {
  1737. #if DEBUG
  1738. printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
  1739. #endif
  1740. return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count);
  1741. } else {
  1742. osst_position_tape_and_confirm(STp, aSRpnt, next_mark_ppos);
  1743. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1744. #if DEBUG
  1745. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n",
  1746. name);
  1747. #endif
  1748. return (-EIO);
  1749. }
  1750. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1751. printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
  1752. name, next_mark_ppos);
  1753. return (-EIO);
  1754. }
  1755. if (ntohl(STp->buffer->aux->filemark_cnt) != cnt + mt_count) {
  1756. printk(KERN_WARNING "%s:W: Expected to find marker %d at ppos %d, not %d\n",
  1757. name, cnt+mt_count, next_mark_ppos,
  1758. ntohl(STp->buffer->aux->filemark_cnt));
  1759. return (-EIO);
  1760. }
  1761. }
  1762. } else {
  1763. /*
  1764. * Find nearest (usually previous) marker, then jump from marker to marker
  1765. */
  1766. while (1) {
  1767. if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_MARKER)
  1768. break;
  1769. if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_EOD) {
  1770. #if DEBUG
  1771. printk(OSST_DEB_MSG "%s:D: space_fwd: EOD reached\n", name);
  1772. #endif
  1773. return (-EIO);
  1774. }
  1775. if (ntohl(STp->buffer->aux->filemark_cnt) == 0) {
  1776. if (STp->first_mark_ppos == -1) {
  1777. #if DEBUG
  1778. printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
  1779. #endif
  1780. return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count);
  1781. }
  1782. osst_position_tape_and_confirm(STp, aSRpnt, STp->first_mark_ppos);
  1783. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1784. #if DEBUG
  1785. printk(OSST_DEB_MSG
  1786. "%s:D: Couldn't get logical blk num in space_filemarks_fwd_fast\n",
  1787. name);
  1788. #endif
  1789. return (-EIO);
  1790. }
  1791. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1792. printk(KERN_WARNING "%s:W: Expected to find filemark at %d\n",
  1793. name, STp->first_mark_ppos);
  1794. return (-EIO);
  1795. }
  1796. } else {
  1797. if (osst_space_over_filemarks_backward(STp, aSRpnt, MTBSF, 1) < 0)
  1798. return (-EIO);
  1799. mt_count++;
  1800. }
  1801. }
  1802. cnt++;
  1803. while (cnt != mt_count) {
  1804. next_mark_ppos = ntohl(STp->buffer->aux->next_mark_ppos);
  1805. if (!next_mark_ppos || next_mark_ppos > STp->eod_frame_ppos) {
  1806. #if DEBUG
  1807. printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
  1808. #endif
  1809. return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count - cnt);
  1810. }
  1811. #if DEBUG
  1812. else printk(OSST_DEB_MSG "%s:D: Positioning to next mark at %d\n", name, next_mark_ppos);
  1813. #endif
  1814. osst_position_tape_and_confirm(STp, aSRpnt, next_mark_ppos);
  1815. cnt++;
  1816. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  1817. #if DEBUG
  1818. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n",
  1819. name);
  1820. #endif
  1821. return (-EIO);
  1822. }
  1823. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1824. printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
  1825. name, next_mark_ppos);
  1826. return (-EIO);
  1827. }
  1828. }
  1829. }
  1830. if (mt_op == MTFSF) {
  1831. STp->frame_seq_number++;
  1832. STp->frame_in_buffer = 0;
  1833. STp->buffer->buffer_bytes = 0;
  1834. STp->buffer->read_pointer = 0;
  1835. STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1836. }
  1837. return 0;
  1838. }
  1839. /*
  1840. * In debug mode, we want to see as many errors as possible
  1841. * to test the error recovery mechanism.
  1842. */
  1843. #if DEBUG
  1844. static void osst_set_retries(struct osst_tape * STp, struct scsi_request ** aSRpnt, int retries)
  1845. {
  1846. unsigned char cmd[MAX_COMMAND_SIZE];
  1847. struct scsi_request * SRpnt = * aSRpnt;
  1848. char * name = tape_name(STp);
  1849. memset(cmd, 0, MAX_COMMAND_SIZE);
  1850. cmd[0] = MODE_SELECT;
  1851. cmd[1] = 0x10;
  1852. cmd[4] = NUMBER_RETRIES_PAGE_LENGTH + MODE_HEADER_LENGTH;
  1853. (STp->buffer)->b_data[0] = cmd[4] - 1;
  1854. (STp->buffer)->b_data[1] = 0; /* Medium Type - ignoring */
  1855. (STp->buffer)->b_data[2] = 0; /* Reserved */
  1856. (STp->buffer)->b_data[3] = 0; /* Block Descriptor Length */
  1857. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 0] = NUMBER_RETRIES_PAGE | (1 << 7);
  1858. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 1] = 2;
  1859. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] = 4;
  1860. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] = retries;
  1861. if (debugging)
  1862. printk(OSST_DEB_MSG "%s:D: Setting number of retries on OnStream tape to %d\n", name, retries);
  1863. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, STp->timeout, 0, 1);
  1864. *aSRpnt = SRpnt;
  1865. if ((STp->buffer)->syscall_result)
  1866. printk (KERN_ERR "%s:D: Couldn't set retries to %d\n", name, retries);
  1867. }
  1868. #endif
  1869. static int osst_write_filemark(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  1870. {
  1871. int result;
  1872. int this_mark_ppos = STp->first_frame_position;
  1873. int this_mark_lbn = STp->logical_blk_num;
  1874. #if DEBUG
  1875. char * name = tape_name(STp);
  1876. #endif
  1877. if (STp->raw) return 0;
  1878. STp->write_type = OS_WRITE_NEW_MARK;
  1879. #if DEBUG
  1880. printk(OSST_DEB_MSG "%s:D: Writing Filemark %i at fppos %d (fseq %d, lblk %d)\n",
  1881. name, STp->filemark_cnt, this_mark_ppos, STp->frame_seq_number, this_mark_lbn);
  1882. #endif
  1883. STp->dirty = 1;
  1884. result = osst_flush_write_buffer(STp, aSRpnt);
  1885. result |= osst_flush_drive_buffer(STp, aSRpnt);
  1886. STp->last_mark_ppos = this_mark_ppos;
  1887. STp->last_mark_lbn = this_mark_lbn;
  1888. if (STp->header_cache != NULL && STp->filemark_cnt < OS_FM_TAB_MAX)
  1889. STp->header_cache->dat_fm_tab.fm_tab_ent[STp->filemark_cnt] = htonl(this_mark_ppos);
  1890. if (STp->filemark_cnt++ == 0)
  1891. STp->first_mark_ppos = this_mark_ppos;
  1892. return result;
  1893. }
  1894. static int osst_write_eod(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  1895. {
  1896. int result;
  1897. #if DEBUG
  1898. char * name = tape_name(STp);
  1899. #endif
  1900. if (STp->raw) return 0;
  1901. STp->write_type = OS_WRITE_EOD;
  1902. STp->eod_frame_ppos = STp->first_frame_position;
  1903. #if DEBUG
  1904. printk(OSST_DEB_MSG "%s:D: Writing EOD at fppos %d (fseq %d, lblk %d)\n", name,
  1905. STp->eod_frame_ppos, STp->frame_seq_number, STp->logical_blk_num);
  1906. #endif
  1907. STp->dirty = 1;
  1908. result = osst_flush_write_buffer(STp, aSRpnt);
  1909. result |= osst_flush_drive_buffer(STp, aSRpnt);
  1910. STp->eod_frame_lfa = --(STp->frame_seq_number);
  1911. return result;
  1912. }
  1913. static int osst_write_filler(struct osst_tape * STp, struct scsi_request ** aSRpnt, int where, int count)
  1914. {
  1915. char * name = tape_name(STp);
  1916. #if DEBUG
  1917. printk(OSST_DEB_MSG "%s:D: Reached onstream write filler group %d\n", name, where);
  1918. #endif
  1919. osst_wait_ready(STp, aSRpnt, 60 * 5, 0);
  1920. osst_set_frame_position(STp, aSRpnt, where, 0);
  1921. STp->write_type = OS_WRITE_FILLER;
  1922. while (count--) {
  1923. memcpy(STp->buffer->b_data, "Filler", 6);
  1924. STp->buffer->buffer_bytes = 6;
  1925. STp->dirty = 1;
  1926. if (osst_flush_write_buffer(STp, aSRpnt)) {
  1927. printk(KERN_INFO "%s:I: Couldn't write filler frame\n", name);
  1928. return (-EIO);
  1929. }
  1930. }
  1931. #if DEBUG
  1932. printk(OSST_DEB_MSG "%s:D: Exiting onstream write filler group\n", name);
  1933. #endif
  1934. return osst_flush_drive_buffer(STp, aSRpnt);
  1935. }
  1936. static int __osst_write_header(struct osst_tape * STp, struct scsi_request ** aSRpnt, int where, int count)
  1937. {
  1938. char * name = tape_name(STp);
  1939. int result;
  1940. #if DEBUG
  1941. printk(OSST_DEB_MSG "%s:D: Reached onstream write header group %d\n", name, where);
  1942. #endif
  1943. osst_wait_ready(STp, aSRpnt, 60 * 5, 0);
  1944. osst_set_frame_position(STp, aSRpnt, where, 0);
  1945. STp->write_type = OS_WRITE_HEADER;
  1946. while (count--) {
  1947. osst_copy_to_buffer(STp->buffer, (unsigned char *)STp->header_cache);
  1948. STp->buffer->buffer_bytes = sizeof(os_header_t);
  1949. STp->dirty = 1;
  1950. if (osst_flush_write_buffer(STp, aSRpnt)) {
  1951. printk(KERN_INFO "%s:I: Couldn't write header frame\n", name);
  1952. return (-EIO);
  1953. }
  1954. }
  1955. result = osst_flush_drive_buffer(STp, aSRpnt);
  1956. #if DEBUG
  1957. printk(OSST_DEB_MSG "%s:D: Write onstream header group %s\n", name, result?"failed":"done");
  1958. #endif
  1959. return result;
  1960. }
  1961. static int osst_write_header(struct osst_tape * STp, struct scsi_request ** aSRpnt, int locate_eod)
  1962. {
  1963. os_header_t * header;
  1964. int result;
  1965. char * name = tape_name(STp);
  1966. #if DEBUG
  1967. printk(OSST_DEB_MSG "%s:D: Writing tape header\n", name);
  1968. #endif
  1969. if (STp->raw) return 0;
  1970. if (STp->header_cache == NULL) {
  1971. if ((STp->header_cache = (os_header_t *)vmalloc(sizeof(os_header_t))) == NULL) {
  1972. printk(KERN_ERR "%s:E: Failed to allocate header cache\n", name);
  1973. return (-ENOMEM);
  1974. }
  1975. memset(STp->header_cache, 0, sizeof(os_header_t));
  1976. #if DEBUG
  1977. printk(OSST_DEB_MSG "%s:D: Allocated and cleared memory for header cache\n", name);
  1978. #endif
  1979. }
  1980. if (STp->header_ok) STp->update_frame_cntr++;
  1981. else STp->update_frame_cntr = 0;
  1982. header = STp->header_cache;
  1983. strcpy(header->ident_str, "ADR_SEQ");
  1984. header->major_rev = 1;
  1985. header->minor_rev = 4;
  1986. header->ext_trk_tb_off = htons(17192);
  1987. header->pt_par_num = 1;
  1988. header->partition[0].partition_num = OS_DATA_PARTITION;
  1989. header->partition[0].par_desc_ver = OS_PARTITION_VERSION;
  1990. header->partition[0].wrt_pass_cntr = htons(STp->wrt_pass_cntr);
  1991. header->partition[0].first_frame_ppos = htonl(STp->first_data_ppos);
  1992. header->partition[0].last_frame_ppos = htonl(STp->capacity);
  1993. header->partition[0].eod_frame_ppos = htonl(STp->eod_frame_ppos);
  1994. header->cfg_col_width = htonl(20);
  1995. header->dat_col_width = htonl(1500);
  1996. header->qfa_col_width = htonl(0);
  1997. header->ext_track_tb.nr_stream_part = 1;
  1998. header->ext_track_tb.et_ent_sz = 32;
  1999. header->ext_track_tb.dat_ext_trk_ey.et_part_num = 0;
  2000. header->ext_track_tb.dat_ext_trk_ey.fmt = 1;
  2001. header->ext_track_tb.dat_ext_trk_ey.fm_tab_off = htons(17736);
  2002. header->ext_track_tb.dat_ext_trk_ey.last_hlb_hi = 0;
  2003. header->ext_track_tb.dat_ext_trk_ey.last_hlb = htonl(STp->eod_frame_lfa);
  2004. header->ext_track_tb.dat_ext_trk_ey.last_pp = htonl(STp->eod_frame_ppos);
  2005. header->dat_fm_tab.fm_part_num = 0;
  2006. header->dat_fm_tab.fm_tab_ent_sz = 4;
  2007. header->dat_fm_tab.fm_tab_ent_cnt = htons(STp->filemark_cnt<OS_FM_TAB_MAX?
  2008. STp->filemark_cnt:OS_FM_TAB_MAX);
  2009. result = __osst_write_header(STp, aSRpnt, 0xbae, 5);
  2010. if (STp->update_frame_cntr == 0)
  2011. osst_write_filler(STp, aSRpnt, 0xbb3, 5);
  2012. result &= __osst_write_header(STp, aSRpnt, 5, 5);
  2013. if (locate_eod) {
  2014. #if DEBUG
  2015. printk(OSST_DEB_MSG "%s:D: Locating back to eod frame addr %d\n", name, STp->eod_frame_ppos);
  2016. #endif
  2017. osst_set_frame_position(STp, aSRpnt, STp->eod_frame_ppos, 0);
  2018. }
  2019. if (result)
  2020. printk(KERN_ERR "%s:E: Write header failed\n", name);
  2021. else {
  2022. memcpy(STp->application_sig, "LIN4", 4);
  2023. STp->linux_media = 1;
  2024. STp->linux_media_version = 4;
  2025. STp->header_ok = 1;
  2026. }
  2027. return result;
  2028. }
  2029. static int osst_reset_header(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  2030. {
  2031. if (STp->header_cache != NULL)
  2032. memset(STp->header_cache, 0, sizeof(os_header_t));
  2033. STp->logical_blk_num = STp->frame_seq_number = 0;
  2034. STp->frame_in_buffer = 0;
  2035. STp->eod_frame_ppos = STp->first_data_ppos = 0x0000000A;
  2036. STp->filemark_cnt = 0;
  2037. STp->first_mark_ppos = STp->last_mark_ppos = STp->last_mark_lbn = -1;
  2038. return osst_write_header(STp, aSRpnt, 1);
  2039. }
  2040. static int __osst_analyze_headers(struct osst_tape * STp, struct scsi_request ** aSRpnt, int ppos)
  2041. {
  2042. char * name = tape_name(STp);
  2043. os_header_t * header;
  2044. os_aux_t * aux;
  2045. char id_string[8];
  2046. int linux_media_version,
  2047. update_frame_cntr;
  2048. if (STp->raw)
  2049. return 1;
  2050. if (ppos == 5 || ppos == 0xbae || STp->buffer->syscall_result) {
  2051. if (osst_set_frame_position(STp, aSRpnt, ppos, 0))
  2052. printk(KERN_WARNING "%s:W: Couldn't position tape\n", name);
  2053. osst_wait_ready(STp, aSRpnt, 60 * 15, 0);
  2054. if (osst_initiate_read (STp, aSRpnt)) {
  2055. printk(KERN_WARNING "%s:W: Couldn't initiate read\n", name);
  2056. return 0;
  2057. }
  2058. }
  2059. if (osst_read_frame(STp, aSRpnt, 180)) {
  2060. #if DEBUG
  2061. printk(OSST_DEB_MSG "%s:D: Couldn't read header frame\n", name);
  2062. #endif
  2063. return 0;
  2064. }
  2065. header = (os_header_t *) STp->buffer->b_data; /* warning: only first segment addressable */
  2066. aux = STp->buffer->aux;
  2067. if (aux->frame_type != OS_FRAME_TYPE_HEADER) {
  2068. #if DEBUG
  2069. printk(OSST_DEB_MSG "%s:D: Skipping non-header frame (%d)\n", name, ppos);
  2070. #endif
  2071. return 0;
  2072. }
  2073. if (ntohl(aux->frame_seq_num) != 0 ||
  2074. ntohl(aux->logical_blk_num) != 0 ||
  2075. aux->partition.partition_num != OS_CONFIG_PARTITION ||
  2076. ntohl(aux->partition.first_frame_ppos) != 0 ||
  2077. ntohl(aux->partition.last_frame_ppos) != 0xbb7 ) {
  2078. #if DEBUG
  2079. printk(OSST_DEB_MSG "%s:D: Invalid header frame (%d,%d,%d,%d,%d)\n", name,
  2080. ntohl(aux->frame_seq_num), ntohl(aux->logical_blk_num),
  2081. aux->partition.partition_num, ntohl(aux->partition.first_frame_ppos),
  2082. ntohl(aux->partition.last_frame_ppos));
  2083. #endif
  2084. return 0;
  2085. }
  2086. if (strncmp(header->ident_str, "ADR_SEQ", 7) != 0 &&
  2087. strncmp(header->ident_str, "ADR-SEQ", 7) != 0) {
  2088. strlcpy(id_string, header->ident_str, 8);
  2089. #if DEBUG
  2090. printk(OSST_DEB_MSG "%s:D: Invalid header identification string %s\n", name, id_string);
  2091. #endif
  2092. return 0;
  2093. }
  2094. update_frame_cntr = ntohl(aux->update_frame_cntr);
  2095. if (update_frame_cntr < STp->update_frame_cntr) {
  2096. #if DEBUG
  2097. printk(OSST_DEB_MSG "%s:D: Skipping frame %d with update_frame_counter %d<%d\n",
  2098. name, ppos, update_frame_cntr, STp->update_frame_cntr);
  2099. #endif
  2100. return 0;
  2101. }
  2102. if (header->major_rev != 1 || header->minor_rev != 4 ) {
  2103. #if DEBUG
  2104. printk(OSST_DEB_MSG "%s:D: %s revision %d.%d detected (1.4 supported)\n",
  2105. name, (header->major_rev != 1 || header->minor_rev < 2 ||
  2106. header->minor_rev > 4 )? "Invalid" : "Warning:",
  2107. header->major_rev, header->minor_rev);
  2108. #endif
  2109. if (header->major_rev != 1 || header->minor_rev < 2 || header->minor_rev > 4)
  2110. return 0;
  2111. }
  2112. #if DEBUG
  2113. if (header->pt_par_num != 1)
  2114. printk(KERN_INFO "%s:W: %d partitions defined, only one supported\n",
  2115. name, header->pt_par_num);
  2116. #endif
  2117. memcpy(id_string, aux->application_sig, 4);
  2118. id_string[4] = 0;
  2119. if (memcmp(id_string, "LIN", 3) == 0) {
  2120. STp->linux_media = 1;
  2121. linux_media_version = id_string[3] - '0';
  2122. if (linux_media_version != 4)
  2123. printk(KERN_INFO "%s:I: Linux media version %d detected (current 4)\n",
  2124. name, linux_media_version);
  2125. } else {
  2126. printk(KERN_WARNING "%s:W: Non Linux media detected (%s)\n", name, id_string);
  2127. return 0;
  2128. }
  2129. if (linux_media_version < STp->linux_media_version) {
  2130. #if DEBUG
  2131. printk(OSST_DEB_MSG "%s:D: Skipping frame %d with linux_media_version %d\n",
  2132. name, ppos, linux_media_version);
  2133. #endif
  2134. return 0;
  2135. }
  2136. if (linux_media_version > STp->linux_media_version) {
  2137. #if DEBUG
  2138. printk(OSST_DEB_MSG "%s:D: Frame %d sets linux_media_version to %d\n",
  2139. name, ppos, linux_media_version);
  2140. #endif
  2141. memcpy(STp->application_sig, id_string, 5);
  2142. STp->linux_media_version = linux_media_version;
  2143. STp->update_frame_cntr = -1;
  2144. }
  2145. if (update_frame_cntr > STp->update_frame_cntr) {
  2146. #if DEBUG
  2147. printk(OSST_DEB_MSG "%s:D: Frame %d sets update_frame_counter to %d\n",
  2148. name, ppos, update_frame_cntr);
  2149. #endif
  2150. if (STp->header_cache == NULL) {
  2151. if ((STp->header_cache = (os_header_t *)vmalloc(sizeof(os_header_t))) == NULL) {
  2152. printk(KERN_ERR "%s:E: Failed to allocate header cache\n", name);
  2153. return 0;
  2154. }
  2155. #if DEBUG
  2156. printk(OSST_DEB_MSG "%s:D: Allocated memory for header cache\n", name);
  2157. #endif
  2158. }
  2159. osst_copy_from_buffer(STp->buffer, (unsigned char *)STp->header_cache);
  2160. header = STp->header_cache; /* further accesses from cached (full) copy */
  2161. STp->wrt_pass_cntr = ntohs(header->partition[0].wrt_pass_cntr);
  2162. STp->first_data_ppos = ntohl(header->partition[0].first_frame_ppos);
  2163. STp->eod_frame_ppos = ntohl(header->partition[0].eod_frame_ppos);
  2164. STp->eod_frame_lfa = ntohl(header->ext_track_tb.dat_ext_trk_ey.last_hlb);
  2165. STp->filemark_cnt = ntohl(aux->filemark_cnt);
  2166. STp->first_mark_ppos = ntohl(aux->next_mark_ppos);
  2167. STp->last_mark_ppos = ntohl(aux->last_mark_ppos);
  2168. STp->last_mark_lbn = ntohl(aux->last_mark_lbn);
  2169. STp->update_frame_cntr = update_frame_cntr;
  2170. #if DEBUG
  2171. printk(OSST_DEB_MSG "%s:D: Detected write pass %d, update frame counter %d, filemark counter %d\n",
  2172. name, STp->wrt_pass_cntr, STp->update_frame_cntr, STp->filemark_cnt);
  2173. printk(OSST_DEB_MSG "%s:D: first data frame on tape = %d, last = %d, eod frame = %d\n", name,
  2174. STp->first_data_ppos,
  2175. ntohl(header->partition[0].last_frame_ppos),
  2176. ntohl(header->partition[0].eod_frame_ppos));
  2177. printk(OSST_DEB_MSG "%s:D: first mark on tape = %d, last = %d, eod frame = %d\n",
  2178. name, STp->first_mark_ppos, STp->last_mark_ppos, STp->eod_frame_ppos);
  2179. #endif
  2180. if (header->minor_rev < 4 && STp->linux_media_version == 4) {
  2181. #if DEBUG
  2182. printk(OSST_DEB_MSG "%s:D: Moving filemark list to ADR 1.4 location\n", name);
  2183. #endif
  2184. memcpy((void *)header->dat_fm_tab.fm_tab_ent,
  2185. (void *)header->old_filemark_list, sizeof(header->dat_fm_tab.fm_tab_ent));
  2186. memset((void *)header->old_filemark_list, 0, sizeof(header->old_filemark_list));
  2187. }
  2188. if (header->minor_rev == 4 &&
  2189. (header->ext_trk_tb_off != htons(17192) ||
  2190. header->partition[0].partition_num != OS_DATA_PARTITION ||
  2191. header->partition[0].par_desc_ver != OS_PARTITION_VERSION ||
  2192. header->partition[0].last_frame_ppos != htonl(STp->capacity) ||
  2193. header->cfg_col_width != htonl(20) ||
  2194. header->dat_col_width != htonl(1500) ||
  2195. header->qfa_col_width != htonl(0) ||
  2196. header->ext_track_tb.nr_stream_part != 1 ||
  2197. header->ext_track_tb.et_ent_sz != 32 ||
  2198. header->ext_track_tb.dat_ext_trk_ey.et_part_num != OS_DATA_PARTITION ||
  2199. header->ext_track_tb.dat_ext_trk_ey.fmt != 1 ||
  2200. header->ext_track_tb.dat_ext_trk_ey.fm_tab_off != htons(17736) ||
  2201. header->ext_track_tb.dat_ext_trk_ey.last_hlb_hi != 0 ||
  2202. header->ext_track_tb.dat_ext_trk_ey.last_pp != htonl(STp->eod_frame_ppos) ||
  2203. header->dat_fm_tab.fm_part_num != OS_DATA_PARTITION ||
  2204. header->dat_fm_tab.fm_tab_ent_sz != 4 ||
  2205. header->dat_fm_tab.fm_tab_ent_cnt !=
  2206. htons(STp->filemark_cnt<OS_FM_TAB_MAX?STp->filemark_cnt:OS_FM_TAB_MAX)))
  2207. printk(KERN_WARNING "%s:W: Failed consistency check ADR 1.4 format\n", name);
  2208. }
  2209. return 1;
  2210. }
  2211. static int osst_analyze_headers(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  2212. {
  2213. int position, ppos;
  2214. int first, last;
  2215. int valid = 0;
  2216. char * name = tape_name(STp);
  2217. position = osst_get_frame_position(STp, aSRpnt);
  2218. if (STp->raw) {
  2219. STp->header_ok = STp->linux_media = 1;
  2220. STp->linux_media_version = 0;
  2221. return 1;
  2222. }
  2223. STp->header_ok = STp->linux_media = STp->linux_media_version = 0;
  2224. STp->wrt_pass_cntr = STp->update_frame_cntr = -1;
  2225. STp->eod_frame_ppos = STp->first_data_ppos = -1;
  2226. STp->first_mark_ppos = STp->last_mark_ppos = STp->last_mark_lbn = -1;
  2227. #if DEBUG
  2228. printk(OSST_DEB_MSG "%s:D: Reading header\n", name);
  2229. #endif
  2230. /* optimization for speed - if we are positioned at ppos 10, read second group first */
  2231. /* TODO try the ADR 1.1 locations for the second group if we have no valid one yet... */
  2232. first = position==10?0xbae: 5;
  2233. last = position==10?0xbb3:10;
  2234. for (ppos = first; ppos < last; ppos++)
  2235. if (__osst_analyze_headers(STp, aSRpnt, ppos))
  2236. valid = 1;
  2237. first = position==10? 5:0xbae;
  2238. last = position==10?10:0xbb3;
  2239. for (ppos = first; ppos < last; ppos++)
  2240. if (__osst_analyze_headers(STp, aSRpnt, ppos))
  2241. valid = 1;
  2242. if (!valid) {
  2243. printk(KERN_ERR "%s:E: Failed to find valid ADRL header, new media?\n", name);
  2244. STp->eod_frame_ppos = STp->first_data_ppos = 0;
  2245. osst_set_frame_position(STp, aSRpnt, 10, 0);
  2246. return 0;
  2247. }
  2248. if (position <= STp->first_data_ppos) {
  2249. position = STp->first_data_ppos;
  2250. STp->ps[0].drv_file = STp->ps[0].drv_block = STp->frame_seq_number = STp->logical_blk_num = 0;
  2251. }
  2252. osst_set_frame_position(STp, aSRpnt, position, 0);
  2253. STp->header_ok = 1;
  2254. return 1;
  2255. }
  2256. static int osst_verify_position(struct osst_tape * STp, struct scsi_request ** aSRpnt)
  2257. {
  2258. int frame_position = STp->first_frame_position;
  2259. int frame_seq_numbr = STp->frame_seq_number;
  2260. int logical_blk_num = STp->logical_blk_num;
  2261. int halfway_frame = STp->frame_in_buffer;
  2262. int read_pointer = STp->buffer->read_pointer;
  2263. int prev_mark_ppos = -1;
  2264. int actual_mark_ppos, i, n;
  2265. #if DEBUG
  2266. char * name = tape_name(STp);
  2267. printk(OSST_DEB_MSG "%s:D: Verify that the tape is really the one we think before writing\n", name);
  2268. #endif
  2269. osst_set_frame_position(STp, aSRpnt, frame_position - 1, 0);
  2270. if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
  2271. #if DEBUG
  2272. printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in verify_position\n", name);
  2273. #endif
  2274. return (-EIO);
  2275. }
  2276. if (STp->linux_media_version >= 4) {
  2277. for (i=0; i<STp->filemark_cnt; i++)
  2278. if ((n=ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[i])) < frame_position)
  2279. prev_mark_ppos = n;
  2280. } else
  2281. prev_mark_ppos = frame_position - 1; /* usually - we don't really know */
  2282. actual_mark_ppos = STp->buffer->aux->frame_type == OS_FRAME_TYPE_MARKER ?
  2283. frame_position - 1 : ntohl(STp->buffer->aux->last_mark_ppos);
  2284. if (frame_position != STp->first_frame_position ||
  2285. frame_seq_numbr != STp->frame_seq_number + (halfway_frame?0:1) ||
  2286. prev_mark_ppos != actual_mark_ppos ) {
  2287. #if DEBUG
  2288. printk(OSST_DEB_MSG "%s:D: Block mismatch: fppos %d-%d, fseq %d-%d, mark %d-%d\n", name,
  2289. STp->first_frame_position, frame_position,
  2290. STp->frame_seq_number + (halfway_frame?0:1),
  2291. frame_seq_numbr, actual_mark_ppos, prev_mark_ppos);
  2292. #endif
  2293. return (-EIO);
  2294. }
  2295. if (halfway_frame) {
  2296. /* prepare buffer for append and rewrite on top of original */
  2297. osst_set_frame_position(STp, aSRpnt, frame_position - 1, 0);
  2298. STp->buffer->buffer_bytes = read_pointer;
  2299. STp->ps[STp->partition].rw = ST_WRITING;
  2300. STp->dirty = 1;
  2301. }
  2302. STp->frame_in_buffer = halfway_frame;
  2303. STp->frame_seq_number = frame_seq_numbr;
  2304. STp->logical_blk_num = logical_blk_num;
  2305. return 0;
  2306. }
  2307. /* Acc. to OnStream, the vers. numbering is the following:
  2308. * X.XX for released versions (X=digit),
  2309. * XXXY for unreleased versions (Y=letter)
  2310. * Ordering 1.05 < 106A < 106B < ... < 106a < ... < 1.06
  2311. * This fn makes monoton numbers out of this scheme ...
  2312. */
  2313. static unsigned int osst_parse_firmware_rev (const char * str)
  2314. {
  2315. if (str[1] == '.') {
  2316. return (str[0]-'0')*10000
  2317. +(str[2]-'0')*1000
  2318. +(str[3]-'0')*100;
  2319. } else {
  2320. return (str[0]-'0')*10000
  2321. +(str[1]-'0')*1000
  2322. +(str[2]-'0')*100 - 100
  2323. +(str[3]-'@');
  2324. }
  2325. }
  2326. /*
  2327. * Configure the OnStream SCII tape drive for default operation
  2328. */
  2329. static int osst_configure_onstream(struct osst_tape *STp, struct scsi_request ** aSRpnt)
  2330. {
  2331. unsigned char cmd[MAX_COMMAND_SIZE];
  2332. char * name = tape_name(STp);
  2333. struct scsi_request * SRpnt = * aSRpnt;
  2334. osst_mode_parameter_header_t * header;
  2335. osst_block_size_page_t * bs;
  2336. osst_capabilities_page_t * cp;
  2337. osst_tape_paramtr_page_t * prm;
  2338. int drive_buffer_size;
  2339. if (STp->ready != ST_READY) {
  2340. #if DEBUG
  2341. printk(OSST_DEB_MSG "%s:D: Not Ready\n", name);
  2342. #endif
  2343. return (-EIO);
  2344. }
  2345. if (STp->os_fw_rev < 10600) {
  2346. printk(KERN_INFO "%s:I: Old OnStream firmware revision detected (%s),\n", name, STp->device->rev);
  2347. printk(KERN_INFO "%s:I: an upgrade to version 1.06 or above is recommended\n", name);
  2348. }
  2349. /*
  2350. * Configure 32.5KB (data+aux) frame size.
  2351. * Get the current frame size from the block size mode page
  2352. */
  2353. memset(cmd, 0, MAX_COMMAND_SIZE);
  2354. cmd[0] = MODE_SENSE;
  2355. cmd[1] = 8;
  2356. cmd[2] = BLOCK_SIZE_PAGE;
  2357. cmd[4] = BLOCK_SIZE_PAGE_LENGTH + MODE_HEADER_LENGTH;
  2358. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, STp->timeout, 0, 1);
  2359. if (SRpnt == NULL) {
  2360. #if DEBUG
  2361. printk(OSST_DEB_MSG "osst :D: Busy\n");
  2362. #endif
  2363. return (-EBUSY);
  2364. }
  2365. *aSRpnt = SRpnt;
  2366. if ((STp->buffer)->syscall_result != 0) {
  2367. printk (KERN_ERR "%s:E: Can't get tape block size mode page\n", name);
  2368. return (-EIO);
  2369. }
  2370. header = (osst_mode_parameter_header_t *) (STp->buffer)->b_data;
  2371. bs = (osst_block_size_page_t *) ((STp->buffer)->b_data + sizeof(osst_mode_parameter_header_t) + header->bdl);
  2372. #if DEBUG
  2373. printk(OSST_DEB_MSG "%s:D: 32KB play back: %s\n", name, bs->play32 ? "Yes" : "No");
  2374. printk(OSST_DEB_MSG "%s:D: 32.5KB play back: %s\n", name, bs->play32_5 ? "Yes" : "No");
  2375. printk(OSST_DEB_MSG "%s:D: 32KB record: %s\n", name, bs->record32 ? "Yes" : "No");
  2376. printk(OSST_DEB_MSG "%s:D: 32.5KB record: %s\n", name, bs->record32_5 ? "Yes" : "No");
  2377. #endif
  2378. /*
  2379. * Configure default auto columns mode, 32.5KB transfer mode
  2380. */
  2381. bs->one = 1;
  2382. bs->play32 = 0;
  2383. bs->play32_5 = 1;
  2384. bs->record32 = 0;
  2385. bs->record32_5 = 1;
  2386. memset(cmd, 0, MAX_COMMAND_SIZE);
  2387. cmd[0] = MODE_SELECT;
  2388. cmd[1] = 0x10;
  2389. cmd[4] = BLOCK_SIZE_PAGE_LENGTH + MODE_HEADER_LENGTH;
  2390. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, STp->timeout, 0, 1);
  2391. *aSRpnt = SRpnt;
  2392. if ((STp->buffer)->syscall_result != 0) {
  2393. printk (KERN_ERR "%s:E: Couldn't set tape block size mode page\n", name);
  2394. return (-EIO);
  2395. }
  2396. #if DEBUG
  2397. printk(KERN_INFO "%s:D: Drive Block Size changed to 32.5K\n", name);
  2398. /*
  2399. * In debug mode, we want to see as many errors as possible
  2400. * to test the error recovery mechanism.
  2401. */
  2402. osst_set_retries(STp, aSRpnt, 0);
  2403. SRpnt = * aSRpnt;
  2404. #endif
  2405. /*
  2406. * Set vendor name to 'LIN4' for "Linux support version 4".
  2407. */
  2408. memset(cmd, 0, MAX_COMMAND_SIZE);
  2409. cmd[0] = MODE_SELECT;
  2410. cmd[1] = 0x10;
  2411. cmd[4] = VENDOR_IDENT_PAGE_LENGTH + MODE_HEADER_LENGTH;
  2412. header->mode_data_length = VENDOR_IDENT_PAGE_LENGTH + MODE_HEADER_LENGTH - 1;
  2413. header->medium_type = 0; /* Medium Type - ignoring */
  2414. header->dsp = 0; /* Reserved */
  2415. header->bdl = 0; /* Block Descriptor Length */
  2416. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 0] = VENDOR_IDENT_PAGE | (1 << 7);
  2417. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 1] = 6;
  2418. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] = 'L';
  2419. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] = 'I';
  2420. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 4] = 'N';
  2421. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 5] = '4';
  2422. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 6] = 0;
  2423. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 7] = 0;
  2424. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, STp->timeout, 0, 1);
  2425. *aSRpnt = SRpnt;
  2426. if ((STp->buffer)->syscall_result != 0) {
  2427. printk (KERN_ERR "%s:E: Couldn't set vendor name to %s\n", name,
  2428. (char *) ((STp->buffer)->b_data + MODE_HEADER_LENGTH + 2));
  2429. return (-EIO);
  2430. }
  2431. memset(cmd, 0, MAX_COMMAND_SIZE);
  2432. cmd[0] = MODE_SENSE;
  2433. cmd[1] = 8;
  2434. cmd[2] = CAPABILITIES_PAGE;
  2435. cmd[4] = CAPABILITIES_PAGE_LENGTH + MODE_HEADER_LENGTH;
  2436. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, STp->timeout, 0, 1);
  2437. *aSRpnt = SRpnt;
  2438. if ((STp->buffer)->syscall_result != 0) {
  2439. printk (KERN_ERR "%s:E: Can't get capabilities page\n", name);
  2440. return (-EIO);
  2441. }
  2442. header = (osst_mode_parameter_header_t *) (STp->buffer)->b_data;
  2443. cp = (osst_capabilities_page_t *) ((STp->buffer)->b_data +
  2444. sizeof(osst_mode_parameter_header_t) + header->bdl);
  2445. drive_buffer_size = ntohs(cp->buffer_size) / 2;
  2446. memset(cmd, 0, MAX_COMMAND_SIZE);
  2447. cmd[0] = MODE_SENSE;
  2448. cmd[1] = 8;
  2449. cmd[2] = TAPE_PARAMTR_PAGE;
  2450. cmd[4] = TAPE_PARAMTR_PAGE_LENGTH + MODE_HEADER_LENGTH;
  2451. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, STp->timeout, 0, 1);
  2452. *aSRpnt = SRpnt;
  2453. if ((STp->buffer)->syscall_result != 0) {
  2454. printk (KERN_ERR "%s:E: Can't get tape parameter page\n", name);
  2455. return (-EIO);
  2456. }
  2457. header = (osst_mode_parameter_header_t *) (STp->buffer)->b_data;
  2458. prm = (osst_tape_paramtr_page_t *) ((STp->buffer)->b_data +
  2459. sizeof(osst_mode_parameter_header_t) + header->bdl);
  2460. STp->density = prm->density;
  2461. STp->capacity = ntohs(prm->segtrk) * ntohs(prm->trks);
  2462. #if DEBUG
  2463. printk(OSST_DEB_MSG "%s:D: Density %d, tape length: %dMB, drive buffer size: %dKB\n",
  2464. name, STp->density, STp->capacity / 32, drive_buffer_size);
  2465. #endif
  2466. return 0;
  2467. }
  2468. /* Step over EOF if it has been inadvertently crossed (ioctl not used because
  2469. it messes up the block number). */
  2470. static int cross_eof(struct osst_tape *STp, struct scsi_request ** aSRpnt, int forward)
  2471. {
  2472. int result;
  2473. char * name = tape_name(STp);
  2474. #if DEBUG
  2475. if (debugging)
  2476. printk(OSST_DEB_MSG "%s:D: Stepping over filemark %s.\n",
  2477. name, forward ? "forward" : "backward");
  2478. #endif
  2479. if (forward) {
  2480. /* assumes that the filemark is already read by the drive, so this is low cost */
  2481. result = osst_space_over_filemarks_forward_slow(STp, aSRpnt, MTFSF, 1);
  2482. }
  2483. else
  2484. /* assumes this is only called if we just read the filemark! */
  2485. result = osst_seek_logical_blk(STp, aSRpnt, STp->logical_blk_num - 1);
  2486. if (result < 0)
  2487. printk(KERN_WARNING "%s:W: Stepping over filemark %s failed.\n",
  2488. name, forward ? "forward" : "backward");
  2489. return result;
  2490. }
  2491. /* Get the tape position. */
  2492. static int osst_get_frame_position(struct osst_tape *STp, struct scsi_request ** aSRpnt)
  2493. {
  2494. unsigned char scmd[MAX_COMMAND_SIZE];
  2495. struct scsi_request * SRpnt;
  2496. int result = 0;
  2497. char * name = tape_name(STp);
  2498. /* KG: We want to be able to use it for checking Write Buffer availability
  2499. * and thus don't want to risk to overwrite anything. Exchange buffers ... */
  2500. char mybuf[24];
  2501. char * olddata = STp->buffer->b_data;
  2502. int oldsize = STp->buffer->buffer_size;
  2503. if (STp->ready != ST_READY) return (-EIO);
  2504. memset (scmd, 0, MAX_COMMAND_SIZE);
  2505. scmd[0] = READ_POSITION;
  2506. STp->buffer->b_data = mybuf; STp->buffer->buffer_size = 24;
  2507. SRpnt = osst_do_scsi(*aSRpnt, STp, scmd, 20, DMA_FROM_DEVICE,
  2508. STp->timeout, MAX_RETRIES, 1);
  2509. if (!SRpnt) {
  2510. STp->buffer->b_data = olddata; STp->buffer->buffer_size = oldsize;
  2511. return (-EBUSY);
  2512. }
  2513. *aSRpnt = SRpnt;
  2514. if (STp->buffer->syscall_result)
  2515. result = ((SRpnt->sr_sense_buffer[2] & 0x0f) == 3) ? -EIO : -EINVAL; /* 3: Write Error */
  2516. if (result == -EINVAL)
  2517. printk(KERN_ERR "%s:E: Can't read tape position.\n", name);
  2518. else {
  2519. if (result == -EIO) { /* re-read position - this needs to preserve media errors */
  2520. unsigned char mysense[16];
  2521. memcpy (mysense, SRpnt->sr_sense_buffer, 16);
  2522. memset (scmd, 0, MAX_COMMAND_SIZE);
  2523. scmd[0] = READ_POSITION;
  2524. STp->buffer->b_data = mybuf; STp->buffer->buffer_size = 24;
  2525. SRpnt = osst_do_scsi(SRpnt, STp, scmd, 20, DMA_FROM_DEVICE,
  2526. STp->timeout, MAX_RETRIES, 1);
  2527. #if DEBUG
  2528. printk(OSST_DEB_MSG "%s:D: Reread position, reason=[%02x:%02x:%02x], result=[%s%02x:%02x:%02x]\n",
  2529. name, mysense[2], mysense[12], mysense[13], STp->buffer->syscall_result?"":"ok:",
  2530. SRpnt->sr_sense_buffer[2],SRpnt->sr_sense_buffer[12],SRpnt->sr_sense_buffer[13]);
  2531. #endif
  2532. if (!STp->buffer->syscall_result)
  2533. memcpy (SRpnt->sr_sense_buffer, mysense, 16);
  2534. else
  2535. printk(KERN_WARNING "%s:W: Double error in get position\n", name);
  2536. }
  2537. STp->first_frame_position = ((STp->buffer)->b_data[4] << 24)
  2538. + ((STp->buffer)->b_data[5] << 16)
  2539. + ((STp->buffer)->b_data[6] << 8)
  2540. + (STp->buffer)->b_data[7];
  2541. STp->last_frame_position = ((STp->buffer)->b_data[ 8] << 24)
  2542. + ((STp->buffer)->b_data[ 9] << 16)
  2543. + ((STp->buffer)->b_data[10] << 8)
  2544. + (STp->buffer)->b_data[11];
  2545. STp->cur_frames = (STp->buffer)->b_data[15];
  2546. #if DEBUG
  2547. if (debugging) {
  2548. printk(OSST_DEB_MSG "%s:D: Drive Positions: host %d, tape %d%s, buffer %d\n", name,
  2549. STp->first_frame_position, STp->last_frame_position,
  2550. ((STp->buffer)->b_data[0]&0x80)?" (BOP)":
  2551. ((STp->buffer)->b_data[0]&0x40)?" (EOP)":"",
  2552. STp->cur_frames);
  2553. }
  2554. #endif
  2555. if (STp->cur_frames == 0 && STp->first_frame_position != STp->last_frame_position) {
  2556. #if DEBUG
  2557. printk(OSST_DEB_MSG "%s:D: Correcting read position %d, %d, %d\n", name,
  2558. STp->first_frame_position, STp->last_frame_position, STp->cur_frames);
  2559. #endif
  2560. STp->first_frame_position = STp->last_frame_position;
  2561. }
  2562. }
  2563. STp->buffer->b_data = olddata; STp->buffer->buffer_size = oldsize;
  2564. return (result == 0 ? STp->first_frame_position : result);
  2565. }
  2566. /* Set the tape block */
  2567. static int osst_set_frame_position(struct osst_tape *STp, struct scsi_request ** aSRpnt, int ppos, int skip)
  2568. {
  2569. unsigned char scmd[MAX_COMMAND_SIZE];
  2570. struct scsi_request * SRpnt;
  2571. struct st_partstat * STps;
  2572. int result = 0;
  2573. int pp = (ppos == 3000 && !skip)? 0 : ppos;
  2574. char * name = tape_name(STp);
  2575. if (STp->ready != ST_READY) return (-EIO);
  2576. STps = &(STp->ps[STp->partition]);
  2577. if (ppos < 0 || ppos > STp->capacity) {
  2578. printk(KERN_WARNING "%s:W: Reposition request %d out of range\n", name, ppos);
  2579. pp = ppos = ppos < 0 ? 0 : (STp->capacity - 1);
  2580. result = (-EINVAL);
  2581. }
  2582. do {
  2583. #if DEBUG
  2584. if (debugging)
  2585. printk(OSST_DEB_MSG "%s:D: Setting ppos to %d.\n", name, pp);
  2586. #endif
  2587. memset (scmd, 0, MAX_COMMAND_SIZE);
  2588. scmd[0] = SEEK_10;
  2589. scmd[1] = 1;
  2590. scmd[3] = (pp >> 24);
  2591. scmd[4] = (pp >> 16);
  2592. scmd[5] = (pp >> 8);
  2593. scmd[6] = pp;
  2594. if (skip)
  2595. scmd[9] = 0x80;
  2596. SRpnt = osst_do_scsi(*aSRpnt, STp, scmd, 0, DMA_NONE, STp->long_timeout,
  2597. MAX_RETRIES, 1);
  2598. if (!SRpnt)
  2599. return (-EBUSY);
  2600. *aSRpnt = SRpnt;
  2601. if ((STp->buffer)->syscall_result != 0) {
  2602. #if DEBUG
  2603. printk(OSST_DEB_MSG "%s:D: SEEK command from %d to %d failed.\n",
  2604. name, STp->first_frame_position, pp);
  2605. #endif
  2606. result = (-EIO);
  2607. }
  2608. if (pp != ppos)
  2609. osst_wait_ready(STp, aSRpnt, 5 * 60, OSST_WAIT_POSITION_COMPLETE);
  2610. } while ((pp != ppos) && (pp = ppos));
  2611. STp->first_frame_position = STp->last_frame_position = ppos;
  2612. STps->eof = ST_NOEOF;
  2613. STps->at_sm = 0;
  2614. STps->rw = ST_IDLE;
  2615. STp->frame_in_buffer = 0;
  2616. return result;
  2617. }
  2618. static int osst_write_trailer(struct osst_tape *STp, struct scsi_request ** aSRpnt, int leave_at_EOT)
  2619. {
  2620. struct st_partstat * STps = &(STp->ps[STp->partition]);
  2621. int result = 0;
  2622. if (STp->write_type != OS_WRITE_NEW_MARK) {
  2623. /* true unless the user wrote the filemark for us */
  2624. result = osst_flush_drive_buffer(STp, aSRpnt);
  2625. if (result < 0) goto out;
  2626. result = osst_write_filemark(STp, aSRpnt);
  2627. if (result < 0) goto out;
  2628. if (STps->drv_file >= 0)
  2629. STps->drv_file++ ;
  2630. STps->drv_block = 0;
  2631. }
  2632. result = osst_write_eod(STp, aSRpnt);
  2633. osst_write_header(STp, aSRpnt, leave_at_EOT);
  2634. STps->eof = ST_FM;
  2635. out:
  2636. return result;
  2637. }
  2638. /* osst versions of st functions - augmented and stripped to suit OnStream only */
  2639. /* Flush the write buffer (never need to write if variable blocksize). */
  2640. static int osst_flush_write_buffer(struct osst_tape *STp, struct scsi_request ** aSRpnt)
  2641. {
  2642. int offset, transfer, blks = 0;
  2643. int result = 0;
  2644. unsigned char cmd[MAX_COMMAND_SIZE];
  2645. struct scsi_request * SRpnt = *aSRpnt;
  2646. struct st_partstat * STps;
  2647. char * name = tape_name(STp);
  2648. if ((STp->buffer)->writing) {
  2649. if (SRpnt == (STp->buffer)->last_SRpnt)
  2650. #if DEBUG
  2651. { printk(OSST_DEB_MSG
  2652. "%s:D: aSRpnt points to scsi_request that write_behind_check will release -- cleared\n", name);
  2653. #endif
  2654. *aSRpnt = SRpnt = NULL;
  2655. #if DEBUG
  2656. } else if (SRpnt)
  2657. printk(OSST_DEB_MSG
  2658. "%s:D: aSRpnt does not point to scsi_request that write_behind_check will release -- strange\n", name);
  2659. #endif
  2660. osst_write_behind_check(STp);
  2661. if ((STp->buffer)->syscall_result) {
  2662. #if DEBUG
  2663. if (debugging)
  2664. printk(OSST_DEB_MSG "%s:D: Async write error (flush) %x.\n",
  2665. name, (STp->buffer)->midlevel_result);
  2666. #endif
  2667. if ((STp->buffer)->midlevel_result == INT_MAX)
  2668. return (-ENOSPC);
  2669. return (-EIO);
  2670. }
  2671. }
  2672. result = 0;
  2673. if (STp->dirty == 1) {
  2674. STp->write_count++;
  2675. STps = &(STp->ps[STp->partition]);
  2676. STps->rw = ST_WRITING;
  2677. offset = STp->buffer->buffer_bytes;
  2678. blks = (offset + STp->block_size - 1) / STp->block_size;
  2679. transfer = OS_FRAME_SIZE;
  2680. if (offset < OS_DATA_SIZE)
  2681. osst_zero_buffer_tail(STp->buffer);
  2682. if (STp->poll)
  2683. if (osst_wait_frame (STp, aSRpnt, STp->first_frame_position, -50, 120))
  2684. result = osst_recover_wait_frame(STp, aSRpnt, 1);
  2685. memset(cmd, 0, MAX_COMMAND_SIZE);
  2686. cmd[0] = WRITE_6;
  2687. cmd[1] = 1;
  2688. cmd[4] = 1;
  2689. switch (STp->write_type) {
  2690. case OS_WRITE_DATA:
  2691. #if DEBUG
  2692. if (debugging)
  2693. printk(OSST_DEB_MSG "%s:D: Writing %d blocks to frame %d, lblks %d-%d\n",
  2694. name, blks, STp->frame_seq_number,
  2695. STp->logical_blk_num - blks, STp->logical_blk_num - 1);
  2696. #endif
  2697. osst_init_aux(STp, OS_FRAME_TYPE_DATA, STp->frame_seq_number++,
  2698. STp->logical_blk_num - blks, STp->block_size, blks);
  2699. break;
  2700. case OS_WRITE_EOD:
  2701. osst_init_aux(STp, OS_FRAME_TYPE_EOD, STp->frame_seq_number++,
  2702. STp->logical_blk_num, 0, 0);
  2703. break;
  2704. case OS_WRITE_NEW_MARK:
  2705. osst_init_aux(STp, OS_FRAME_TYPE_MARKER, STp->frame_seq_number++,
  2706. STp->logical_blk_num++, 0, blks=1);
  2707. break;
  2708. case OS_WRITE_HEADER:
  2709. osst_init_aux(STp, OS_FRAME_TYPE_HEADER, 0, 0, 0, blks=0);
  2710. break;
  2711. default: /* probably FILLER */
  2712. osst_init_aux(STp, OS_FRAME_TYPE_FILL, 0, 0, 0, 0);
  2713. }
  2714. #if DEBUG
  2715. if (debugging)
  2716. printk(OSST_DEB_MSG "%s:D: Flushing %d bytes, Transfering %d bytes in %d lblocks.\n",
  2717. name, offset, transfer, blks);
  2718. #endif
  2719. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
  2720. STp->timeout, MAX_RETRIES, 1);
  2721. *aSRpnt = SRpnt;
  2722. if (!SRpnt)
  2723. return (-EBUSY);
  2724. if ((STp->buffer)->syscall_result != 0) {
  2725. #if DEBUG
  2726. printk(OSST_DEB_MSG
  2727. "%s:D: write sense [0]=0x%02x [2]=%02x [12]=%02x [13]=%02x\n",
  2728. name, SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[2],
  2729. SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
  2730. #endif
  2731. if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
  2732. (SRpnt->sr_sense_buffer[2] & 0x40) && /* FIXME - SC-30 drive doesn't assert EOM bit */
  2733. (SRpnt->sr_sense_buffer[2] & 0x0f) == NO_SENSE) {
  2734. STp->dirty = 0;
  2735. (STp->buffer)->buffer_bytes = 0;
  2736. result = (-ENOSPC);
  2737. }
  2738. else {
  2739. if (osst_write_error_recovery(STp, aSRpnt, 1)) {
  2740. printk(KERN_ERR "%s:E: Error on flush write.\n", name);
  2741. result = (-EIO);
  2742. }
  2743. }
  2744. STps->drv_block = (-1); /* FIXME - even if write recovery succeeds? */
  2745. }
  2746. else {
  2747. STp->first_frame_position++;
  2748. STp->dirty = 0;
  2749. (STp->buffer)->buffer_bytes = 0;
  2750. }
  2751. }
  2752. #if DEBUG
  2753. printk(OSST_DEB_MSG "%s:D: Exit flush write buffer with code %d\n", name, result);
  2754. #endif
  2755. return result;
  2756. }
  2757. /* Flush the tape buffer. The tape will be positioned correctly unless
  2758. seek_next is true. */
  2759. static int osst_flush_buffer(struct osst_tape * STp, struct scsi_request ** aSRpnt, int seek_next)
  2760. {
  2761. struct st_partstat * STps;
  2762. int backspace = 0, result = 0;
  2763. #if DEBUG
  2764. char * name = tape_name(STp);
  2765. #endif
  2766. /*
  2767. * If there was a bus reset, block further access
  2768. * to this device.
  2769. */
  2770. if( STp->pos_unknown)
  2771. return (-EIO);
  2772. if (STp->ready != ST_READY)
  2773. return 0;
  2774. STps = &(STp->ps[STp->partition]);
  2775. if (STps->rw == ST_WRITING || STp->dirty) { /* Writing */
  2776. STp->write_type = OS_WRITE_DATA;
  2777. return osst_flush_write_buffer(STp, aSRpnt);
  2778. }
  2779. if (STp->block_size == 0)
  2780. return 0;
  2781. #if DEBUG
  2782. printk(OSST_DEB_MSG "%s:D: Reached flush (read) buffer\n", name);
  2783. #endif
  2784. if (!STp->can_bsr) {
  2785. backspace = ((STp->buffer)->buffer_bytes + (STp->buffer)->read_pointer) / STp->block_size -
  2786. ((STp->buffer)->read_pointer + STp->block_size - 1 ) / STp->block_size ;
  2787. (STp->buffer)->buffer_bytes = 0;
  2788. (STp->buffer)->read_pointer = 0;
  2789. STp->frame_in_buffer = 0; /* FIXME is this relevant w. OSST? */
  2790. }
  2791. if (!seek_next) {
  2792. if (STps->eof == ST_FM_HIT) {
  2793. result = cross_eof(STp, aSRpnt, 0); /* Back over the EOF hit */
  2794. if (!result)
  2795. STps->eof = ST_NOEOF;
  2796. else {
  2797. if (STps->drv_file >= 0)
  2798. STps->drv_file++;
  2799. STps->drv_block = 0;
  2800. }
  2801. }
  2802. if (!result && backspace > 0) /* TODO -- design and run a test case for this */
  2803. result = osst_seek_logical_blk(STp, aSRpnt, STp->logical_blk_num - backspace);
  2804. }
  2805. else if (STps->eof == ST_FM_HIT) {
  2806. if (STps->drv_file >= 0)
  2807. STps->drv_file++;
  2808. STps->drv_block = 0;
  2809. STps->eof = ST_NOEOF;
  2810. }
  2811. return result;
  2812. }
  2813. static int osst_write_frame(struct osst_tape * STp, struct scsi_request ** aSRpnt, int synchronous)
  2814. {
  2815. unsigned char cmd[MAX_COMMAND_SIZE];
  2816. struct scsi_request * SRpnt;
  2817. int blks;
  2818. #if DEBUG
  2819. char * name = tape_name(STp);
  2820. #endif
  2821. if ((!STp-> raw) && (STp->first_frame_position == 0xbae)) { /* _must_ preserve buffer! */
  2822. #if DEBUG
  2823. printk(OSST_DEB_MSG "%s:D: Reaching config partition.\n", name);
  2824. #endif
  2825. if (osst_flush_drive_buffer(STp, aSRpnt) < 0) {
  2826. return (-EIO);
  2827. }
  2828. /* error recovery may have bumped us past the header partition */
  2829. if (osst_get_frame_position(STp, aSRpnt) < 0xbb8) {
  2830. #if DEBUG
  2831. printk(OSST_DEB_MSG "%s:D: Skipping over config partition.\n", name);
  2832. #endif
  2833. osst_position_tape_and_confirm(STp, aSRpnt, 0xbb8);
  2834. }
  2835. }
  2836. if (STp->poll)
  2837. if (osst_wait_frame (STp, aSRpnt, STp->first_frame_position, -48, 120))
  2838. if (osst_recover_wait_frame(STp, aSRpnt, 1))
  2839. return (-EIO);
  2840. // osst_build_stats(STp, &SRpnt);
  2841. STp->ps[STp->partition].rw = ST_WRITING;
  2842. STp->write_type = OS_WRITE_DATA;
  2843. memset(cmd, 0, MAX_COMMAND_SIZE);
  2844. cmd[0] = WRITE_6;
  2845. cmd[1] = 1;
  2846. cmd[4] = 1; /* one frame at a time... */
  2847. blks = STp->buffer->buffer_bytes / STp->block_size;
  2848. #if DEBUG
  2849. if (debugging)
  2850. printk(OSST_DEB_MSG "%s:D: Writing %d blocks to frame %d, lblks %d-%d\n", name, blks,
  2851. STp->frame_seq_number, STp->logical_blk_num - blks, STp->logical_blk_num - 1);
  2852. #endif
  2853. osst_init_aux(STp, OS_FRAME_TYPE_DATA, STp->frame_seq_number++,
  2854. STp->logical_blk_num - blks, STp->block_size, blks);
  2855. #if DEBUG
  2856. if (!synchronous)
  2857. STp->write_pending = 1;
  2858. #endif
  2859. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, DMA_TO_DEVICE, STp->timeout,
  2860. MAX_RETRIES, synchronous);
  2861. if (!SRpnt)
  2862. return (-EBUSY);
  2863. *aSRpnt = SRpnt;
  2864. if (synchronous) {
  2865. if (STp->buffer->syscall_result != 0) {
  2866. #if DEBUG
  2867. if (debugging)
  2868. printk(OSST_DEB_MSG "%s:D: Error on write:\n", name);
  2869. #endif
  2870. if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
  2871. (SRpnt->sr_sense_buffer[2] & 0x40)) {
  2872. if ((SRpnt->sr_sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
  2873. return (-ENOSPC);
  2874. }
  2875. else {
  2876. if (osst_write_error_recovery(STp, aSRpnt, 1))
  2877. return (-EIO);
  2878. }
  2879. }
  2880. else
  2881. STp->first_frame_position++;
  2882. }
  2883. STp->write_count++;
  2884. return 0;
  2885. }
  2886. /* Lock or unlock the drive door. Don't use when struct scsi_request allocated. */
  2887. static int do_door_lock(struct osst_tape * STp, int do_lock)
  2888. {
  2889. int retval, cmd;
  2890. cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
  2891. #if DEBUG
  2892. printk(OSST_DEB_MSG "%s:D: %socking drive door.\n", tape_name(STp), do_lock ? "L" : "Unl");
  2893. #endif
  2894. retval = scsi_ioctl(STp->device, cmd, NULL);
  2895. if (!retval) {
  2896. STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
  2897. }
  2898. else {
  2899. STp->door_locked = ST_LOCK_FAILS;
  2900. }
  2901. return retval;
  2902. }
  2903. /* Set the internal state after reset */
  2904. static void reset_state(struct osst_tape *STp)
  2905. {
  2906. int i;
  2907. struct st_partstat *STps;
  2908. STp->pos_unknown = 0;
  2909. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  2910. STps = &(STp->ps[i]);
  2911. STps->rw = ST_IDLE;
  2912. STps->eof = ST_NOEOF;
  2913. STps->at_sm = 0;
  2914. STps->last_block_valid = 0;
  2915. STps->drv_block = -1;
  2916. STps->drv_file = -1;
  2917. }
  2918. }
  2919. /* Entry points to osst */
  2920. /* Write command */
  2921. static ssize_t osst_write(struct file * filp, const char __user * buf, size_t count, loff_t *ppos)
  2922. {
  2923. ssize_t total, retval = 0;
  2924. ssize_t i, do_count, blks, transfer;
  2925. int write_threshold;
  2926. int doing_write = 0;
  2927. const char __user * b_point;
  2928. struct scsi_request * SRpnt = NULL;
  2929. struct st_modedef * STm;
  2930. struct st_partstat * STps;
  2931. struct osst_tape * STp = filp->private_data;
  2932. char * name = tape_name(STp);
  2933. if (down_interruptible(&STp->lock))
  2934. return (-ERESTARTSYS);
  2935. /*
  2936. * If we are in the middle of error recovery, don't let anyone
  2937. * else try and use this device. Also, if error recovery fails, it
  2938. * may try and take the device offline, in which case all further
  2939. * access to the device is prohibited.
  2940. */
  2941. if( !scsi_block_when_processing_errors(STp->device) ) {
  2942. retval = (-ENXIO);
  2943. goto out;
  2944. }
  2945. if (STp->ready != ST_READY) {
  2946. if (STp->ready == ST_NO_TAPE)
  2947. retval = (-ENOMEDIUM);
  2948. else
  2949. retval = (-EIO);
  2950. goto out;
  2951. }
  2952. STm = &(STp->modes[STp->current_mode]);
  2953. if (!STm->defined) {
  2954. retval = (-ENXIO);
  2955. goto out;
  2956. }
  2957. if (count == 0)
  2958. goto out;
  2959. /*
  2960. * If there was a bus reset, block further access
  2961. * to this device.
  2962. */
  2963. if (STp->pos_unknown) {
  2964. retval = (-EIO);
  2965. goto out;
  2966. }
  2967. #if DEBUG
  2968. if (!STp->in_use) {
  2969. printk(OSST_DEB_MSG "%s:D: Incorrect device.\n", name);
  2970. retval = (-EIO);
  2971. goto out;
  2972. }
  2973. #endif
  2974. if (STp->write_prot) {
  2975. retval = (-EACCES);
  2976. goto out;
  2977. }
  2978. /* Write must be integral number of blocks */
  2979. if (STp->block_size != 0 && (count % STp->block_size) != 0) {
  2980. printk(KERN_ERR "%s:E: Write (%Zd bytes) not multiple of tape block size (%d%c).\n",
  2981. name, count, STp->block_size<1024?
  2982. STp->block_size:STp->block_size/1024, STp->block_size<1024?'b':'k');
  2983. retval = (-EINVAL);
  2984. goto out;
  2985. }
  2986. if (STp->first_frame_position >= STp->capacity - OSST_EOM_RESERVE) {
  2987. printk(KERN_ERR "%s:E: Write truncated at EOM early warning (frame %d).\n",
  2988. name, STp->first_frame_position);
  2989. retval = (-ENOSPC);
  2990. goto out;
  2991. }
  2992. if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED && !do_door_lock(STp, 1))
  2993. STp->door_locked = ST_LOCKED_AUTO;
  2994. STps = &(STp->ps[STp->partition]);
  2995. if (STps->rw == ST_READING) {
  2996. #if DEBUG
  2997. printk(OSST_DEB_MSG "%s:D: Switching from read to write at file %d, block %d\n", name,
  2998. STps->drv_file, STps->drv_block);
  2999. #endif
  3000. retval = osst_flush_buffer(STp, &SRpnt, 0);
  3001. if (retval)
  3002. goto out;
  3003. STps->rw = ST_IDLE;
  3004. }
  3005. if (STps->rw != ST_WRITING) {
  3006. /* Are we totally rewriting this tape? */
  3007. if (!STp->header_ok ||
  3008. (STp->first_frame_position == STp->first_data_ppos && STps->drv_block < 0) ||
  3009. (STps->drv_file == 0 && STps->drv_block == 0)) {
  3010. STp->wrt_pass_cntr++;
  3011. #if DEBUG
  3012. printk(OSST_DEB_MSG "%s:D: Allocating next write pass counter: %d\n",
  3013. name, STp->wrt_pass_cntr);
  3014. #endif
  3015. osst_reset_header(STp, &SRpnt);
  3016. STps->drv_file = STps->drv_block = 0;
  3017. }
  3018. /* Do we know where we'll be writing on the tape? */
  3019. else {
  3020. if ((STp->fast_open && osst_verify_position(STp, &SRpnt)) ||
  3021. STps->drv_file < 0 || STps->drv_block < 0) {
  3022. if (STp->first_frame_position == STp->eod_frame_ppos) { /* at EOD */
  3023. STps->drv_file = STp->filemark_cnt;
  3024. STps->drv_block = 0;
  3025. }
  3026. else {
  3027. /* We have no idea where the tape is positioned - give up */
  3028. #if DEBUG
  3029. printk(OSST_DEB_MSG
  3030. "%s:D: Cannot write at indeterminate position.\n", name);
  3031. #endif
  3032. retval = (-EIO);
  3033. goto out;
  3034. }
  3035. }
  3036. if ((STps->drv_file + STps->drv_block) > 0 && STps->drv_file < STp->filemark_cnt) {
  3037. STp->filemark_cnt = STps->drv_file;
  3038. STp->last_mark_ppos =
  3039. ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[STp->filemark_cnt-1]);
  3040. printk(KERN_WARNING
  3041. "%s:W: Overwriting file %d with old write pass counter %d\n",
  3042. name, STps->drv_file, STp->wrt_pass_cntr);
  3043. printk(KERN_WARNING
  3044. "%s:W: may lead to stale data being accepted on reading back!\n",
  3045. name);
  3046. #if DEBUG
  3047. printk(OSST_DEB_MSG
  3048. "%s:D: resetting filemark count to %d and last mark ppos,lbn to %d,%d\n",
  3049. name, STp->filemark_cnt, STp->last_mark_ppos, STp->last_mark_lbn);
  3050. #endif
  3051. }
  3052. }
  3053. STp->fast_open = 0;
  3054. }
  3055. if (!STp->header_ok) {
  3056. #if DEBUG
  3057. printk(OSST_DEB_MSG "%s:D: Write cannot proceed without valid headers\n", name);
  3058. #endif
  3059. retval = (-EIO);
  3060. goto out;
  3061. }
  3062. if ((STp->buffer)->writing) {
  3063. if (SRpnt) printk(KERN_ERR "%s:A: Not supposed to have SRpnt at line %d\n", name, __LINE__);
  3064. osst_write_behind_check(STp);
  3065. if ((STp->buffer)->syscall_result) {
  3066. #if DEBUG
  3067. if (debugging)
  3068. printk(OSST_DEB_MSG "%s:D: Async write error (write) %x.\n", name,
  3069. (STp->buffer)->midlevel_result);
  3070. #endif
  3071. if ((STp->buffer)->midlevel_result == INT_MAX)
  3072. STps->eof = ST_EOM_OK;
  3073. else
  3074. STps->eof = ST_EOM_ERROR;
  3075. }
  3076. }
  3077. if (STps->eof == ST_EOM_OK) {
  3078. retval = (-ENOSPC);
  3079. goto out;
  3080. }
  3081. else if (STps->eof == ST_EOM_ERROR) {
  3082. retval = (-EIO);
  3083. goto out;
  3084. }
  3085. /* Check the buffer readability in cases where copy_user might catch
  3086. the problems after some tape movement. */
  3087. if ((copy_from_user(&i, buf, 1) != 0 ||
  3088. copy_from_user(&i, buf + count - 1, 1) != 0)) {
  3089. retval = (-EFAULT);
  3090. goto out;
  3091. }
  3092. if (!STm->do_buffer_writes) {
  3093. write_threshold = 1;
  3094. }
  3095. else
  3096. write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
  3097. if (!STm->do_async_writes)
  3098. write_threshold--;
  3099. total = count;
  3100. #if DEBUG
  3101. if (debugging)
  3102. printk(OSST_DEB_MSG "%s:D: Writing %d bytes to file %d block %d lblk %d fseq %d fppos %d\n",
  3103. name, count, STps->drv_file, STps->drv_block,
  3104. STp->logical_blk_num, STp->frame_seq_number, STp->first_frame_position);
  3105. #endif
  3106. b_point = buf;
  3107. while ((STp->buffer)->buffer_bytes + count > write_threshold)
  3108. {
  3109. doing_write = 1;
  3110. do_count = (STp->buffer)->buffer_blocks * STp->block_size -
  3111. (STp->buffer)->buffer_bytes;
  3112. if (do_count > count)
  3113. do_count = count;
  3114. i = append_to_buffer(b_point, STp->buffer, do_count);
  3115. if (i) {
  3116. retval = i;
  3117. goto out;
  3118. }
  3119. blks = do_count / STp->block_size;
  3120. STp->logical_blk_num += blks; /* logical_blk_num is incremented as data is moved from user */
  3121. i = osst_write_frame(STp, &SRpnt, 1);
  3122. if (i == (-ENOSPC)) {
  3123. transfer = STp->buffer->writing; /* FIXME -- check this logic */
  3124. if (transfer <= do_count) {
  3125. filp->f_pos += do_count - transfer;
  3126. count -= do_count - transfer;
  3127. if (STps->drv_block >= 0) {
  3128. STps->drv_block += (do_count - transfer) / STp->block_size;
  3129. }
  3130. STps->eof = ST_EOM_OK;
  3131. retval = (-ENOSPC); /* EOM within current request */
  3132. #if DEBUG
  3133. if (debugging)
  3134. printk(OSST_DEB_MSG "%s:D: EOM with %d bytes unwritten.\n",
  3135. name, transfer);
  3136. #endif
  3137. }
  3138. else {
  3139. STps->eof = ST_EOM_ERROR;
  3140. STps->drv_block = (-1); /* Too cautious? */
  3141. retval = (-EIO); /* EOM for old data */
  3142. #if DEBUG
  3143. if (debugging)
  3144. printk(OSST_DEB_MSG "%s:D: EOM with lost data.\n", name);
  3145. #endif
  3146. }
  3147. }
  3148. else
  3149. retval = i;
  3150. if (retval < 0) {
  3151. if (SRpnt != NULL) {
  3152. scsi_release_request(SRpnt);
  3153. SRpnt = NULL;
  3154. }
  3155. STp->buffer->buffer_bytes = 0;
  3156. STp->dirty = 0;
  3157. if (count < total)
  3158. retval = total - count;
  3159. goto out;
  3160. }
  3161. filp->f_pos += do_count;
  3162. b_point += do_count;
  3163. count -= do_count;
  3164. if (STps->drv_block >= 0) {
  3165. STps->drv_block += blks;
  3166. }
  3167. STp->buffer->buffer_bytes = 0;
  3168. STp->dirty = 0;
  3169. } /* end while write threshold exceeded */
  3170. if (count != 0) {
  3171. STp->dirty = 1;
  3172. i = append_to_buffer(b_point, STp->buffer, count);
  3173. if (i) {
  3174. retval = i;
  3175. goto out;
  3176. }
  3177. blks = count / STp->block_size;
  3178. STp->logical_blk_num += blks;
  3179. if (STps->drv_block >= 0) {
  3180. STps->drv_block += blks;
  3181. }
  3182. filp->f_pos += count;
  3183. count = 0;
  3184. }
  3185. if (doing_write && (STp->buffer)->syscall_result != 0) {
  3186. retval = (STp->buffer)->syscall_result;
  3187. goto out;
  3188. }
  3189. if (STm->do_async_writes && ((STp->buffer)->buffer_bytes >= STp->write_threshold)) {
  3190. /* Schedule an asynchronous write */
  3191. (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
  3192. STp->block_size) * STp->block_size;
  3193. STp->dirty = !((STp->buffer)->writing ==
  3194. (STp->buffer)->buffer_bytes);
  3195. i = osst_write_frame(STp, &SRpnt, 0);
  3196. if (i < 0) {
  3197. retval = (-EIO);
  3198. goto out;
  3199. }
  3200. SRpnt = NULL; /* Prevent releasing this request! */
  3201. }
  3202. STps->at_sm &= (total == 0);
  3203. if (total > 0)
  3204. STps->eof = ST_NOEOF;
  3205. retval = total;
  3206. out:
  3207. if (SRpnt != NULL) scsi_release_request(SRpnt);
  3208. up(&STp->lock);
  3209. return retval;
  3210. }
  3211. /* Read command */
  3212. static ssize_t osst_read(struct file * filp, char __user * buf, size_t count, loff_t *ppos)
  3213. {
  3214. ssize_t total, retval = 0;
  3215. ssize_t i, transfer;
  3216. int special;
  3217. struct st_modedef * STm;
  3218. struct st_partstat * STps;
  3219. struct scsi_request * SRpnt = NULL;
  3220. struct osst_tape * STp = filp->private_data;
  3221. char * name = tape_name(STp);
  3222. if (down_interruptible(&STp->lock))
  3223. return (-ERESTARTSYS);
  3224. /*
  3225. * If we are in the middle of error recovery, don't let anyone
  3226. * else try and use this device. Also, if error recovery fails, it
  3227. * may try and take the device offline, in which case all further
  3228. * access to the device is prohibited.
  3229. */
  3230. if( !scsi_block_when_processing_errors(STp->device) ) {
  3231. retval = (-ENXIO);
  3232. goto out;
  3233. }
  3234. if (STp->ready != ST_READY) {
  3235. if (STp->ready == ST_NO_TAPE)
  3236. retval = (-ENOMEDIUM);
  3237. else
  3238. retval = (-EIO);
  3239. goto out;
  3240. }
  3241. STm = &(STp->modes[STp->current_mode]);
  3242. if (!STm->defined) {
  3243. retval = (-ENXIO);
  3244. goto out;
  3245. }
  3246. #if DEBUG
  3247. if (!STp->in_use) {
  3248. printk(OSST_DEB_MSG "%s:D: Incorrect device.\n", name);
  3249. retval = (-EIO);
  3250. goto out;
  3251. }
  3252. #endif
  3253. /* Must have initialized medium */
  3254. if (!STp->header_ok) {
  3255. retval = (-EIO);
  3256. goto out;
  3257. }
  3258. if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED && !do_door_lock(STp, 1))
  3259. STp->door_locked = ST_LOCKED_AUTO;
  3260. STps = &(STp->ps[STp->partition]);
  3261. if (STps->rw == ST_WRITING) {
  3262. retval = osst_flush_buffer(STp, &SRpnt, 0);
  3263. if (retval)
  3264. goto out;
  3265. STps->rw = ST_IDLE;
  3266. /* FIXME -- this may leave the tape without EOD and up2date headers */
  3267. }
  3268. if ((count % STp->block_size) != 0) {
  3269. printk(KERN_WARNING
  3270. "%s:W: Read (%Zd bytes) not multiple of tape block size (%d%c).\n", name, count,
  3271. STp->block_size<1024?STp->block_size:STp->block_size/1024, STp->block_size<1024?'b':'k');
  3272. }
  3273. #if DEBUG
  3274. if (debugging && STps->eof != ST_NOEOF)
  3275. printk(OSST_DEB_MSG "%s:D: EOF/EOM flag up (%d). Bytes %d\n", name,
  3276. STps->eof, (STp->buffer)->buffer_bytes);
  3277. #endif
  3278. if ((STp->buffer)->buffer_bytes == 0 &&
  3279. STps->eof >= ST_EOD_1) {
  3280. if (STps->eof < ST_EOD) {
  3281. STps->eof += 1;
  3282. retval = 0;
  3283. goto out;
  3284. }
  3285. retval = (-EIO); /* EOM or Blank Check */
  3286. goto out;
  3287. }
  3288. /* Check the buffer writability before any tape movement. Don't alter
  3289. buffer data. */
  3290. if (copy_from_user(&i, buf, 1) != 0 ||
  3291. copy_to_user (buf, &i, 1) != 0 ||
  3292. copy_from_user(&i, buf + count - 1, 1) != 0 ||
  3293. copy_to_user (buf + count - 1, &i, 1) != 0) {
  3294. retval = (-EFAULT);
  3295. goto out;
  3296. }
  3297. /* Loop until enough data in buffer or a special condition found */
  3298. for (total = 0, special = 0; total < count - STp->block_size + 1 && !special; ) {
  3299. /* Get new data if the buffer is empty */
  3300. if ((STp->buffer)->buffer_bytes == 0) {
  3301. if (STps->eof == ST_FM_HIT)
  3302. break;
  3303. special = osst_get_logical_frame(STp, &SRpnt, STp->frame_seq_number, 0);
  3304. if (special < 0) { /* No need to continue read */
  3305. STp->frame_in_buffer = 0;
  3306. retval = special;
  3307. goto out;
  3308. }
  3309. }
  3310. /* Move the data from driver buffer to user buffer */
  3311. if ((STp->buffer)->buffer_bytes > 0) {
  3312. #if DEBUG
  3313. if (debugging && STps->eof != ST_NOEOF)
  3314. printk(OSST_DEB_MSG "%s:D: EOF up (%d). Left %d, needed %d.\n", name,
  3315. STps->eof, (STp->buffer)->buffer_bytes, count - total);
  3316. #endif
  3317. /* force multiple of block size, note block_size may have been adjusted */
  3318. transfer = (((STp->buffer)->buffer_bytes < count - total ?
  3319. (STp->buffer)->buffer_bytes : count - total)/
  3320. STp->block_size) * STp->block_size;
  3321. if (transfer == 0) {
  3322. printk(KERN_WARNING
  3323. "%s:W: Nothing can be transfered, requested %Zd, tape block size (%d%c).\n",
  3324. name, count, STp->block_size < 1024?
  3325. STp->block_size:STp->block_size/1024,
  3326. STp->block_size<1024?'b':'k');
  3327. break;
  3328. }
  3329. i = from_buffer(STp->buffer, buf, transfer);
  3330. if (i) {
  3331. retval = i;
  3332. goto out;
  3333. }
  3334. STp->logical_blk_num += transfer / STp->block_size;
  3335. STps->drv_block += transfer / STp->block_size;
  3336. filp->f_pos += transfer;
  3337. buf += transfer;
  3338. total += transfer;
  3339. }
  3340. if ((STp->buffer)->buffer_bytes == 0) {
  3341. #if DEBUG
  3342. if (debugging)
  3343. printk(OSST_DEB_MSG "%s:D: Finished with frame %d\n",
  3344. name, STp->frame_seq_number);
  3345. #endif
  3346. STp->frame_in_buffer = 0;
  3347. STp->frame_seq_number++; /* frame to look for next time */
  3348. }
  3349. } /* for (total = 0, special = 0; total < count && !special; ) */
  3350. /* Change the eof state if no data from tape or buffer */
  3351. if (total == 0) {
  3352. if (STps->eof == ST_FM_HIT) {
  3353. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD_2:ST_FM;
  3354. STps->drv_block = 0;
  3355. if (STps->drv_file >= 0)
  3356. STps->drv_file++;
  3357. }
  3358. else if (STps->eof == ST_EOD_1) {
  3359. STps->eof = ST_EOD_2;
  3360. if (STps->drv_block > 0 && STps->drv_file >= 0)
  3361. STps->drv_file++;
  3362. STps->drv_block = 0;
  3363. }
  3364. else if (STps->eof == ST_EOD_2)
  3365. STps->eof = ST_EOD;
  3366. }
  3367. else if (STps->eof == ST_FM)
  3368. STps->eof = ST_NOEOF;
  3369. retval = total;
  3370. out:
  3371. if (SRpnt != NULL) scsi_release_request(SRpnt);
  3372. up(&STp->lock);
  3373. return retval;
  3374. }
  3375. /* Set the driver options */
  3376. static void osst_log_options(struct osst_tape *STp, struct st_modedef *STm, char *name)
  3377. {
  3378. printk(KERN_INFO
  3379. "%s:I: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
  3380. name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
  3381. STm->do_read_ahead);
  3382. printk(KERN_INFO
  3383. "%s:I: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
  3384. name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
  3385. printk(KERN_INFO
  3386. "%s:I: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
  3387. name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
  3388. STp->scsi2_logical);
  3389. printk(KERN_INFO
  3390. "%s:I: sysv: %d\n", name, STm->sysv);
  3391. #if DEBUG
  3392. printk(KERN_INFO
  3393. "%s:D: debugging: %d\n",
  3394. name, debugging);
  3395. #endif
  3396. }
  3397. static int osst_set_options(struct osst_tape *STp, long options)
  3398. {
  3399. int value;
  3400. long code;
  3401. struct st_modedef * STm;
  3402. char * name = tape_name(STp);
  3403. STm = &(STp->modes[STp->current_mode]);
  3404. if (!STm->defined) {
  3405. memcpy(STm, &(STp->modes[0]), sizeof(*STm));
  3406. modes_defined = 1;
  3407. #if DEBUG
  3408. if (debugging)
  3409. printk(OSST_DEB_MSG "%s:D: Initialized mode %d definition from mode 0\n",
  3410. name, STp->current_mode);
  3411. #endif
  3412. }
  3413. code = options & MT_ST_OPTIONS;
  3414. if (code == MT_ST_BOOLEANS) {
  3415. STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
  3416. STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
  3417. STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
  3418. STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
  3419. STp->two_fm = (options & MT_ST_TWO_FM) != 0;
  3420. STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
  3421. STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
  3422. STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
  3423. STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
  3424. if ((STp->device)->scsi_level >= SCSI_2)
  3425. STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
  3426. STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
  3427. STm->sysv = (options & MT_ST_SYSV) != 0;
  3428. #if DEBUG
  3429. debugging = (options & MT_ST_DEBUGGING) != 0;
  3430. #endif
  3431. osst_log_options(STp, STm, name);
  3432. }
  3433. else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
  3434. value = (code == MT_ST_SETBOOLEANS);
  3435. if ((options & MT_ST_BUFFER_WRITES) != 0)
  3436. STm->do_buffer_writes = value;
  3437. if ((options & MT_ST_ASYNC_WRITES) != 0)
  3438. STm->do_async_writes = value;
  3439. if ((options & MT_ST_DEF_WRITES) != 0)
  3440. STm->defaults_for_writes = value;
  3441. if ((options & MT_ST_READ_AHEAD) != 0)
  3442. STm->do_read_ahead = value;
  3443. if ((options & MT_ST_TWO_FM) != 0)
  3444. STp->two_fm = value;
  3445. if ((options & MT_ST_FAST_MTEOM) != 0)
  3446. STp->fast_mteom = value;
  3447. if ((options & MT_ST_AUTO_LOCK) != 0)
  3448. STp->do_auto_lock = value;
  3449. if ((options & MT_ST_CAN_BSR) != 0)
  3450. STp->can_bsr = value;
  3451. if ((options & MT_ST_NO_BLKLIMS) != 0)
  3452. STp->omit_blklims = value;
  3453. if ((STp->device)->scsi_level >= SCSI_2 &&
  3454. (options & MT_ST_CAN_PARTITIONS) != 0)
  3455. STp->can_partitions = value;
  3456. if ((options & MT_ST_SCSI2LOGICAL) != 0)
  3457. STp->scsi2_logical = value;
  3458. if ((options & MT_ST_SYSV) != 0)
  3459. STm->sysv = value;
  3460. #if DEBUG
  3461. if ((options & MT_ST_DEBUGGING) != 0)
  3462. debugging = value;
  3463. #endif
  3464. osst_log_options(STp, STm, name);
  3465. }
  3466. else if (code == MT_ST_WRITE_THRESHOLD) {
  3467. value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
  3468. if (value < 1 || value > osst_buffer_size) {
  3469. printk(KERN_WARNING "%s:W: Write threshold %d too small or too large.\n",
  3470. name, value);
  3471. return (-EIO);
  3472. }
  3473. STp->write_threshold = value;
  3474. printk(KERN_INFO "%s:I: Write threshold set to %d bytes.\n",
  3475. name, value);
  3476. }
  3477. else if (code == MT_ST_DEF_BLKSIZE) {
  3478. value = (options & ~MT_ST_OPTIONS);
  3479. if (value == ~MT_ST_OPTIONS) {
  3480. STm->default_blksize = (-1);
  3481. printk(KERN_INFO "%s:I: Default block size disabled.\n", name);
  3482. }
  3483. else {
  3484. if (value < 512 || value > OS_DATA_SIZE || OS_DATA_SIZE % value) {
  3485. printk(KERN_WARNING "%s:W: Default block size cannot be set to %d.\n",
  3486. name, value);
  3487. return (-EINVAL);
  3488. }
  3489. STm->default_blksize = value;
  3490. printk(KERN_INFO "%s:I: Default block size set to %d bytes.\n",
  3491. name, STm->default_blksize);
  3492. }
  3493. }
  3494. else if (code == MT_ST_TIMEOUTS) {
  3495. value = (options & ~MT_ST_OPTIONS);
  3496. if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
  3497. STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
  3498. printk(KERN_INFO "%s:I: Long timeout set to %d seconds.\n", name,
  3499. (value & ~MT_ST_SET_LONG_TIMEOUT));
  3500. }
  3501. else {
  3502. STp->timeout = value * HZ;
  3503. printk(KERN_INFO "%s:I: Normal timeout set to %d seconds.\n", name, value);
  3504. }
  3505. }
  3506. else if (code == MT_ST_DEF_OPTIONS) {
  3507. code = (options & ~MT_ST_CLEAR_DEFAULT);
  3508. value = (options & MT_ST_CLEAR_DEFAULT);
  3509. if (code == MT_ST_DEF_DENSITY) {
  3510. if (value == MT_ST_CLEAR_DEFAULT) {
  3511. STm->default_density = (-1);
  3512. printk(KERN_INFO "%s:I: Density default disabled.\n", name);
  3513. }
  3514. else {
  3515. STm->default_density = value & 0xff;
  3516. printk(KERN_INFO "%s:I: Density default set to %x\n",
  3517. name, STm->default_density);
  3518. }
  3519. }
  3520. else if (code == MT_ST_DEF_DRVBUFFER) {
  3521. if (value == MT_ST_CLEAR_DEFAULT) {
  3522. STp->default_drvbuffer = 0xff;
  3523. printk(KERN_INFO "%s:I: Drive buffer default disabled.\n", name);
  3524. }
  3525. else {
  3526. STp->default_drvbuffer = value & 7;
  3527. printk(KERN_INFO "%s:I: Drive buffer default set to %x\n",
  3528. name, STp->default_drvbuffer);
  3529. }
  3530. }
  3531. else if (code == MT_ST_DEF_COMPRESSION) {
  3532. if (value == MT_ST_CLEAR_DEFAULT) {
  3533. STm->default_compression = ST_DONT_TOUCH;
  3534. printk(KERN_INFO "%s:I: Compression default disabled.\n", name);
  3535. }
  3536. else {
  3537. STm->default_compression = (value & 1 ? ST_YES : ST_NO);
  3538. printk(KERN_INFO "%s:I: Compression default set to %x\n",
  3539. name, (value & 1));
  3540. }
  3541. }
  3542. }
  3543. else
  3544. return (-EIO);
  3545. return 0;
  3546. }
  3547. /* Internal ioctl function */
  3548. static int osst_int_ioctl(struct osst_tape * STp, struct scsi_request ** aSRpnt,
  3549. unsigned int cmd_in, unsigned long arg)
  3550. {
  3551. int timeout;
  3552. long ltmp;
  3553. int i, ioctl_result;
  3554. int chg_eof = 1;
  3555. unsigned char cmd[MAX_COMMAND_SIZE];
  3556. struct scsi_request * SRpnt = * aSRpnt;
  3557. struct st_partstat * STps;
  3558. int fileno, blkno, at_sm, frame_seq_numbr, logical_blk_num;
  3559. int datalen = 0, direction = DMA_NONE;
  3560. char * name = tape_name(STp);
  3561. if (STp->ready != ST_READY && cmd_in != MTLOAD) {
  3562. if (STp->ready == ST_NO_TAPE)
  3563. return (-ENOMEDIUM);
  3564. else
  3565. return (-EIO);
  3566. }
  3567. timeout = STp->long_timeout;
  3568. STps = &(STp->ps[STp->partition]);
  3569. fileno = STps->drv_file;
  3570. blkno = STps->drv_block;
  3571. at_sm = STps->at_sm;
  3572. frame_seq_numbr = STp->frame_seq_number;
  3573. logical_blk_num = STp->logical_blk_num;
  3574. memset(cmd, 0, MAX_COMMAND_SIZE);
  3575. switch (cmd_in) {
  3576. case MTFSFM:
  3577. chg_eof = 0; /* Changed from the FSF after this */
  3578. case MTFSF:
  3579. if (STp->raw)
  3580. return (-EIO);
  3581. if (STp->linux_media)
  3582. ioctl_result = osst_space_over_filemarks_forward_fast(STp, &SRpnt, cmd_in, arg);
  3583. else
  3584. ioctl_result = osst_space_over_filemarks_forward_slow(STp, &SRpnt, cmd_in, arg);
  3585. if (fileno >= 0)
  3586. fileno += arg;
  3587. blkno = 0;
  3588. at_sm &= (arg == 0);
  3589. goto os_bypass;
  3590. case MTBSF:
  3591. chg_eof = 0; /* Changed from the FSF after this */
  3592. case MTBSFM:
  3593. if (STp->raw)
  3594. return (-EIO);
  3595. ioctl_result = osst_space_over_filemarks_backward(STp, &SRpnt, cmd_in, arg);
  3596. if (fileno >= 0)
  3597. fileno -= arg;
  3598. blkno = (-1); /* We can't know the block number */
  3599. at_sm &= (arg == 0);
  3600. goto os_bypass;
  3601. case MTFSR:
  3602. case MTBSR:
  3603. #if DEBUG
  3604. if (debugging)
  3605. printk(OSST_DEB_MSG "%s:D: Skipping %lu blocks %s from logical block %d\n",
  3606. name, arg, cmd_in==MTFSR?"forward":"backward", logical_blk_num);
  3607. #endif
  3608. if (cmd_in == MTFSR) {
  3609. logical_blk_num += arg;
  3610. if (blkno >= 0) blkno += arg;
  3611. }
  3612. else {
  3613. logical_blk_num -= arg;
  3614. if (blkno >= 0) blkno -= arg;
  3615. }
  3616. ioctl_result = osst_seek_logical_blk(STp, &SRpnt, logical_blk_num);
  3617. fileno = STps->drv_file;
  3618. blkno = STps->drv_block;
  3619. at_sm &= (arg == 0);
  3620. goto os_bypass;
  3621. case MTFSS:
  3622. cmd[0] = SPACE;
  3623. cmd[1] = 0x04; /* Space Setmarks */ /* FIXME -- OS can't do this? */
  3624. cmd[2] = (arg >> 16);
  3625. cmd[3] = (arg >> 8);
  3626. cmd[4] = arg;
  3627. #if DEBUG
  3628. if (debugging)
  3629. printk(OSST_DEB_MSG "%s:D: Spacing tape forward %d setmarks.\n", name,
  3630. cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
  3631. #endif
  3632. if (arg != 0) {
  3633. blkno = fileno = (-1);
  3634. at_sm = 1;
  3635. }
  3636. break;
  3637. case MTBSS:
  3638. cmd[0] = SPACE;
  3639. cmd[1] = 0x04; /* Space Setmarks */ /* FIXME -- OS can't do this? */
  3640. ltmp = (-arg);
  3641. cmd[2] = (ltmp >> 16);
  3642. cmd[3] = (ltmp >> 8);
  3643. cmd[4] = ltmp;
  3644. #if DEBUG
  3645. if (debugging) {
  3646. if (cmd[2] & 0x80)
  3647. ltmp = 0xff000000;
  3648. ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
  3649. printk(OSST_DEB_MSG "%s:D: Spacing tape backward %ld setmarks.\n",
  3650. name, (-ltmp));
  3651. }
  3652. #endif
  3653. if (arg != 0) {
  3654. blkno = fileno = (-1);
  3655. at_sm = 1;
  3656. }
  3657. break;
  3658. case MTWEOF:
  3659. if ((STps->rw == ST_WRITING || STp->dirty) && !STp->pos_unknown) {
  3660. STp->write_type = OS_WRITE_DATA;
  3661. ioctl_result = osst_flush_write_buffer(STp, &SRpnt);
  3662. } else
  3663. ioctl_result = 0;
  3664. #if DEBUG
  3665. if (debugging)
  3666. printk(OSST_DEB_MSG "%s:D: Writing %ld filemark(s).\n", name, arg);
  3667. #endif
  3668. for (i=0; i<arg; i++)
  3669. ioctl_result |= osst_write_filemark(STp, &SRpnt);
  3670. if (fileno >= 0) fileno += arg;
  3671. if (blkno >= 0) blkno = 0;
  3672. goto os_bypass;
  3673. case MTWSM:
  3674. if (STp->write_prot)
  3675. return (-EACCES);
  3676. if (!STp->raw)
  3677. return 0;
  3678. cmd[0] = WRITE_FILEMARKS; /* FIXME -- need OS version */
  3679. if (cmd_in == MTWSM)
  3680. cmd[1] = 2;
  3681. cmd[2] = (arg >> 16);
  3682. cmd[3] = (arg >> 8);
  3683. cmd[4] = arg;
  3684. timeout = STp->timeout;
  3685. #if DEBUG
  3686. if (debugging)
  3687. printk(OSST_DEB_MSG "%s:D: Writing %d setmark(s).\n", name,
  3688. cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
  3689. #endif
  3690. if (fileno >= 0)
  3691. fileno += arg;
  3692. blkno = 0;
  3693. at_sm = (cmd_in == MTWSM);
  3694. break;
  3695. case MTOFFL:
  3696. case MTLOAD:
  3697. case MTUNLOAD:
  3698. case MTRETEN:
  3699. cmd[0] = START_STOP;
  3700. cmd[1] = 1; /* Don't wait for completion */
  3701. if (cmd_in == MTLOAD) {
  3702. if (STp->ready == ST_NO_TAPE)
  3703. cmd[4] = 4; /* open tray */
  3704. else
  3705. cmd[4] = 1; /* load */
  3706. }
  3707. if (cmd_in == MTRETEN)
  3708. cmd[4] = 3; /* retension then mount */
  3709. if (cmd_in == MTOFFL)
  3710. cmd[4] = 4; /* rewind then eject */
  3711. timeout = STp->timeout;
  3712. #if DEBUG
  3713. if (debugging) {
  3714. switch (cmd_in) {
  3715. case MTUNLOAD:
  3716. printk(OSST_DEB_MSG "%s:D: Unloading tape.\n", name);
  3717. break;
  3718. case MTLOAD:
  3719. printk(OSST_DEB_MSG "%s:D: Loading tape.\n", name);
  3720. break;
  3721. case MTRETEN:
  3722. printk(OSST_DEB_MSG "%s:D: Retensioning tape.\n", name);
  3723. break;
  3724. case MTOFFL:
  3725. printk(OSST_DEB_MSG "%s:D: Ejecting tape.\n", name);
  3726. break;
  3727. }
  3728. }
  3729. #endif
  3730. fileno = blkno = at_sm = frame_seq_numbr = logical_blk_num = 0 ;
  3731. break;
  3732. case MTNOP:
  3733. #if DEBUG
  3734. if (debugging)
  3735. printk(OSST_DEB_MSG "%s:D: No-op on tape.\n", name);
  3736. #endif
  3737. return 0; /* Should do something ? */
  3738. break;
  3739. case MTEOM:
  3740. #if DEBUG
  3741. if (debugging)
  3742. printk(OSST_DEB_MSG "%s:D: Spacing to end of recorded medium.\n", name);
  3743. #endif
  3744. if ((osst_position_tape_and_confirm(STp, &SRpnt, STp->eod_frame_ppos) < 0) ||
  3745. (osst_get_logical_frame(STp, &SRpnt, -1, 0) < 0)) {
  3746. ioctl_result = -EIO;
  3747. goto os_bypass;
  3748. }
  3749. if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_EOD) {
  3750. #if DEBUG
  3751. printk(OSST_DEB_MSG "%s:D: No EOD frame found where expected.\n", name);
  3752. #endif
  3753. ioctl_result = -EIO;
  3754. goto os_bypass;
  3755. }
  3756. ioctl_result = osst_set_frame_position(STp, &SRpnt, STp->eod_frame_ppos, 0);
  3757. fileno = STp->filemark_cnt;
  3758. blkno = at_sm = 0;
  3759. goto os_bypass;
  3760. case MTERASE:
  3761. if (STp->write_prot)
  3762. return (-EACCES);
  3763. ioctl_result = osst_reset_header(STp, &SRpnt);
  3764. i = osst_write_eod(STp, &SRpnt);
  3765. if (i < ioctl_result) ioctl_result = i;
  3766. i = osst_position_tape_and_confirm(STp, &SRpnt, STp->eod_frame_ppos);
  3767. if (i < ioctl_result) ioctl_result = i;
  3768. fileno = blkno = at_sm = 0 ;
  3769. goto os_bypass;
  3770. case MTREW:
  3771. cmd[0] = REZERO_UNIT; /* rewind */
  3772. cmd[1] = 1;
  3773. #if DEBUG
  3774. if (debugging)
  3775. printk(OSST_DEB_MSG "%s:D: Rewinding tape, Immed=%d.\n", name, cmd[1]);
  3776. #endif
  3777. fileno = blkno = at_sm = frame_seq_numbr = logical_blk_num = 0 ;
  3778. break;
  3779. case MTSETBLK: /* Set block length */
  3780. if ((STps->drv_block == 0 ) &&
  3781. !STp->dirty &&
  3782. ((STp->buffer)->buffer_bytes == 0) &&
  3783. ((arg & MT_ST_BLKSIZE_MASK) >= 512 ) &&
  3784. ((arg & MT_ST_BLKSIZE_MASK) <= OS_DATA_SIZE) &&
  3785. !(OS_DATA_SIZE % (arg & MT_ST_BLKSIZE_MASK)) ) {
  3786. /*
  3787. * Only allowed to change the block size if you opened the
  3788. * device at the beginning of a file before writing anything.
  3789. * Note, that when reading, changing block_size is futile,
  3790. * as the size used when writing overrides it.
  3791. */
  3792. STp->block_size = (arg & MT_ST_BLKSIZE_MASK);
  3793. printk(KERN_INFO "%s:I: Block size set to %d bytes.\n",
  3794. name, STp->block_size);
  3795. return 0;
  3796. }
  3797. case MTSETDENSITY: /* Set tape density */
  3798. case MTSETDRVBUFFER: /* Set drive buffering */
  3799. case SET_DENS_AND_BLK: /* Set density and block size */
  3800. chg_eof = 0;
  3801. if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
  3802. return (-EIO); /* Not allowed if data in buffer */
  3803. if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
  3804. (arg & MT_ST_BLKSIZE_MASK) != 0 &&
  3805. (arg & MT_ST_BLKSIZE_MASK) != STp->block_size ) {
  3806. printk(KERN_WARNING "%s:W: Illegal to set block size to %d%s.\n",
  3807. name, (int)(arg & MT_ST_BLKSIZE_MASK),
  3808. (OS_DATA_SIZE % (arg & MT_ST_BLKSIZE_MASK))?"":" now");
  3809. return (-EINVAL);
  3810. }
  3811. return 0; /* FIXME silently ignore if block size didn't change */
  3812. default:
  3813. return (-ENOSYS);
  3814. }
  3815. SRpnt = osst_do_scsi(SRpnt, STp, cmd, datalen, direction, timeout, MAX_RETRIES, 1);
  3816. ioctl_result = (STp->buffer)->syscall_result;
  3817. if (!SRpnt) {
  3818. #if DEBUG
  3819. printk(OSST_DEB_MSG "%s:D: Couldn't exec scsi cmd for IOCTL\n", name);
  3820. #endif
  3821. return ioctl_result;
  3822. }
  3823. if (!ioctl_result) { /* SCSI command successful */
  3824. STp->frame_seq_number = frame_seq_numbr;
  3825. STp->logical_blk_num = logical_blk_num;
  3826. }
  3827. os_bypass:
  3828. #if DEBUG
  3829. if (debugging)
  3830. printk(OSST_DEB_MSG "%s:D: IOCTL (%d) Result=%d\n", name, cmd_in, ioctl_result);
  3831. #endif
  3832. if (!ioctl_result) { /* success */
  3833. if (cmd_in == MTFSFM) {
  3834. fileno--;
  3835. blkno--;
  3836. }
  3837. if (cmd_in == MTBSFM) {
  3838. fileno++;
  3839. blkno++;
  3840. }
  3841. STps->drv_block = blkno;
  3842. STps->drv_file = fileno;
  3843. STps->at_sm = at_sm;
  3844. if (cmd_in == MTEOM)
  3845. STps->eof = ST_EOD;
  3846. else if ((cmd_in == MTFSFM || cmd_in == MTBSF) && STps->eof == ST_FM_HIT) {
  3847. ioctl_result = osst_seek_logical_blk(STp, &SRpnt, STp->logical_blk_num-1);
  3848. STps->drv_block++;
  3849. STp->logical_blk_num++;
  3850. STp->frame_seq_number++;
  3851. STp->frame_in_buffer = 0;
  3852. STp->buffer->read_pointer = 0;
  3853. }
  3854. else if (cmd_in == MTFSF)
  3855. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_FM;
  3856. else if (chg_eof)
  3857. STps->eof = ST_NOEOF;
  3858. if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
  3859. STp->rew_at_close = 0;
  3860. else if (cmd_in == MTLOAD) {
  3861. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  3862. STp->ps[i].rw = ST_IDLE;
  3863. STp->ps[i].last_block_valid = 0;/* FIXME - where else is this field maintained? */
  3864. }
  3865. STp->partition = 0;
  3866. }
  3867. if (cmd_in == MTREW) {
  3868. ioctl_result = osst_position_tape_and_confirm(STp, &SRpnt, STp->first_data_ppos);
  3869. if (ioctl_result > 0)
  3870. ioctl_result = 0;
  3871. }
  3872. } else if (cmd_in == MTBSF || cmd_in == MTBSFM ) {
  3873. if (osst_position_tape_and_confirm(STp, &SRpnt, STp->first_data_ppos) < 0)
  3874. STps->drv_file = STps->drv_block = -1;
  3875. else
  3876. STps->drv_file = STps->drv_block = 0;
  3877. STps->eof = ST_NOEOF;
  3878. } else if (cmd_in == MTFSF || cmd_in == MTFSFM) {
  3879. if (osst_position_tape_and_confirm(STp, &SRpnt, STp->eod_frame_ppos) < 0)
  3880. STps->drv_file = STps->drv_block = -1;
  3881. else {
  3882. STps->drv_file = STp->filemark_cnt;
  3883. STps->drv_block = 0;
  3884. }
  3885. STps->eof = ST_EOD;
  3886. } else if (cmd_in == MTBSR || cmd_in == MTFSR || cmd_in == MTWEOF || cmd_in == MTEOM) {
  3887. STps->drv_file = STps->drv_block = (-1);
  3888. STps->eof = ST_NOEOF;
  3889. STp->header_ok = 0;
  3890. } else if (cmd_in == MTERASE) {
  3891. STp->header_ok = 0;
  3892. } else if (SRpnt) { /* SCSI command was not completely successful. */
  3893. if (SRpnt->sr_sense_buffer[2] & 0x40) {
  3894. STps->eof = ST_EOM_OK;
  3895. STps->drv_block = 0;
  3896. }
  3897. if (chg_eof)
  3898. STps->eof = ST_NOEOF;
  3899. if ((SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK)
  3900. STps->eof = ST_EOD;
  3901. if (cmd_in == MTLOAD && osst_wait_for_medium(STp, &SRpnt, 60))
  3902. ioctl_result = osst_wait_ready(STp, &SRpnt, 5 * 60, OSST_WAIT_POSITION_COMPLETE);
  3903. }
  3904. *aSRpnt = SRpnt;
  3905. return ioctl_result;
  3906. }
  3907. /* Open the device */
  3908. static int os_scsi_tape_open(struct inode * inode, struct file * filp)
  3909. {
  3910. unsigned short flags;
  3911. int i, b_size, new_session = 0, retval = 0;
  3912. unsigned char cmd[MAX_COMMAND_SIZE];
  3913. struct scsi_request * SRpnt = NULL;
  3914. struct osst_tape * STp;
  3915. struct st_modedef * STm;
  3916. struct st_partstat * STps;
  3917. char * name;
  3918. int dev = TAPE_NR(inode);
  3919. int mode = TAPE_MODE(inode);
  3920. /*
  3921. * We really want to do nonseekable_open(inode, filp); here, but some
  3922. * versions of tar incorrectly call lseek on tapes and bail out if that
  3923. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  3924. */
  3925. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  3926. write_lock(&os_scsi_tapes_lock);
  3927. if (dev >= osst_max_dev || os_scsi_tapes == NULL ||
  3928. (STp = os_scsi_tapes[dev]) == NULL || !STp->device) {
  3929. write_unlock(&os_scsi_tapes_lock);
  3930. return (-ENXIO);
  3931. }
  3932. name = tape_name(STp);
  3933. if (STp->in_use) {
  3934. write_unlock(&os_scsi_tapes_lock);
  3935. #if DEBUG
  3936. printk(OSST_DEB_MSG "%s:D: Device already in use.\n", name);
  3937. #endif
  3938. return (-EBUSY);
  3939. }
  3940. if (scsi_device_get(STp->device)) {
  3941. write_unlock(&os_scsi_tapes_lock);
  3942. #if DEBUG
  3943. printk(OSST_DEB_MSG "%s:D: Failed scsi_device_get.\n", name);
  3944. #endif
  3945. return (-ENXIO);
  3946. }
  3947. filp->private_data = STp;
  3948. STp->in_use = 1;
  3949. write_unlock(&os_scsi_tapes_lock);
  3950. STp->rew_at_close = TAPE_REWIND(inode);
  3951. if( !scsi_block_when_processing_errors(STp->device) ) {
  3952. return -ENXIO;
  3953. }
  3954. if (mode != STp->current_mode) {
  3955. #if DEBUG
  3956. if (debugging)
  3957. printk(OSST_DEB_MSG "%s:D: Mode change from %d to %d.\n",
  3958. name, STp->current_mode, mode);
  3959. #endif
  3960. new_session = 1;
  3961. STp->current_mode = mode;
  3962. }
  3963. STm = &(STp->modes[STp->current_mode]);
  3964. flags = filp->f_flags;
  3965. STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
  3966. STp->raw = TAPE_IS_RAW(inode);
  3967. if (STp->raw)
  3968. STp->header_ok = 0;
  3969. /* Allocate data segments for this device's tape buffer */
  3970. if (!enlarge_buffer(STp->buffer, STp->restr_dma)) {
  3971. printk(KERN_ERR "%s:E: Unable to allocate memory segments for tape buffer.\n", name);
  3972. retval = (-EOVERFLOW);
  3973. goto err_out;
  3974. }
  3975. if (STp->buffer->buffer_size >= OS_FRAME_SIZE) {
  3976. for (i = 0, b_size = 0;
  3977. (i < STp->buffer->sg_segs) && ((b_size + STp->buffer->sg[i].length) <= OS_DATA_SIZE);
  3978. b_size += STp->buffer->sg[i++].length);
  3979. STp->buffer->aux = (os_aux_t *) (page_address(STp->buffer->sg[i].page) + OS_DATA_SIZE - b_size);
  3980. #if DEBUG
  3981. printk(OSST_DEB_MSG "%s:D: b_data points to %p in segment 0 at %p\n", name,
  3982. STp->buffer->b_data, page_address(STp->buffer->sg[0].page));
  3983. printk(OSST_DEB_MSG "%s:D: AUX points to %p in segment %d at %p\n", name,
  3984. STp->buffer->aux, i, page_address(STp->buffer->sg[i].page));
  3985. #endif
  3986. } else {
  3987. STp->buffer->aux = NULL; /* this had better never happen! */
  3988. printk(KERN_NOTICE "%s:A: Framesize %d too large for buffer.\n", name, OS_FRAME_SIZE);
  3989. retval = (-EIO);
  3990. goto err_out;
  3991. }
  3992. STp->buffer->writing = 0;
  3993. STp->buffer->syscall_result = 0;
  3994. STp->dirty = 0;
  3995. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  3996. STps = &(STp->ps[i]);
  3997. STps->rw = ST_IDLE;
  3998. }
  3999. STp->ready = ST_READY;
  4000. #if DEBUG
  4001. STp->nbr_waits = STp->nbr_finished = 0;
  4002. #endif
  4003. memset (cmd, 0, MAX_COMMAND_SIZE);
  4004. cmd[0] = TEST_UNIT_READY;
  4005. SRpnt = osst_do_scsi(NULL, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  4006. if (!SRpnt) {
  4007. retval = (STp->buffer)->syscall_result; /* FIXME - valid? */
  4008. goto err_out;
  4009. }
  4010. if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
  4011. (SRpnt->sr_sense_buffer[2] & 0x0f) == NOT_READY &&
  4012. SRpnt->sr_sense_buffer[12] == 4 ) {
  4013. #if DEBUG
  4014. printk(OSST_DEB_MSG "%s:D: Unit not ready, cause %x\n", name, SRpnt->sr_sense_buffer[13]);
  4015. #endif
  4016. if (filp->f_flags & O_NONBLOCK) {
  4017. retval = -EAGAIN;
  4018. goto err_out;
  4019. }
  4020. if (SRpnt->sr_sense_buffer[13] == 2) { /* initialize command required (LOAD) */
  4021. memset (cmd, 0, MAX_COMMAND_SIZE);
  4022. cmd[0] = START_STOP;
  4023. cmd[1] = 1;
  4024. cmd[4] = 1;
  4025. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  4026. STp->timeout, MAX_RETRIES, 1);
  4027. }
  4028. osst_wait_ready(STp, &SRpnt, (SRpnt->sr_sense_buffer[13]==1?15:3) * 60, 0);
  4029. }
  4030. if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
  4031. (SRpnt->sr_sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
  4032. #if DEBUG
  4033. printk(OSST_DEB_MSG "%s:D: Unit wants attention\n", name);
  4034. #endif
  4035. STp->header_ok = 0;
  4036. for (i=0; i < 10; i++) {
  4037. memset (cmd, 0, MAX_COMMAND_SIZE);
  4038. cmd[0] = TEST_UNIT_READY;
  4039. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  4040. STp->timeout, MAX_RETRIES, 1);
  4041. if ((SRpnt->sr_sense_buffer[0] & 0x70) != 0x70 ||
  4042. (SRpnt->sr_sense_buffer[2] & 0x0f) != UNIT_ATTENTION)
  4043. break;
  4044. }
  4045. STp->pos_unknown = 0;
  4046. STp->partition = STp->new_partition = 0;
  4047. if (STp->can_partitions)
  4048. STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
  4049. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  4050. STps = &(STp->ps[i]);
  4051. STps->rw = ST_IDLE; /* FIXME - seems to be redundant... */
  4052. STps->eof = ST_NOEOF;
  4053. STps->at_sm = 0;
  4054. STps->last_block_valid = 0;
  4055. STps->drv_block = 0;
  4056. STps->drv_file = 0 ;
  4057. }
  4058. new_session = 1;
  4059. STp->recover_count = 0;
  4060. STp->abort_count = 0;
  4061. }
  4062. /*
  4063. * if we have valid headers from before, and the drive/tape seem untouched,
  4064. * open without reconfiguring and re-reading the headers
  4065. */
  4066. if (!STp->buffer->syscall_result && STp->header_ok &&
  4067. !SRpnt->sr_result && SRpnt->sr_sense_buffer[0] == 0) {
  4068. memset(cmd, 0, MAX_COMMAND_SIZE);
  4069. cmd[0] = MODE_SENSE;
  4070. cmd[1] = 8;
  4071. cmd[2] = VENDOR_IDENT_PAGE;
  4072. cmd[4] = VENDOR_IDENT_PAGE_LENGTH + MODE_HEADER_LENGTH;
  4073. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, STp->timeout, 0, 1);
  4074. if (STp->buffer->syscall_result ||
  4075. STp->buffer->b_data[MODE_HEADER_LENGTH + 2] != 'L' ||
  4076. STp->buffer->b_data[MODE_HEADER_LENGTH + 3] != 'I' ||
  4077. STp->buffer->b_data[MODE_HEADER_LENGTH + 4] != 'N' ||
  4078. STp->buffer->b_data[MODE_HEADER_LENGTH + 5] != '4' ) {
  4079. #if DEBUG
  4080. printk(OSST_DEB_MSG "%s:D: Signature was changed to %c%c%c%c\n", name,
  4081. STp->buffer->b_data[MODE_HEADER_LENGTH + 2],
  4082. STp->buffer->b_data[MODE_HEADER_LENGTH + 3],
  4083. STp->buffer->b_data[MODE_HEADER_LENGTH + 4],
  4084. STp->buffer->b_data[MODE_HEADER_LENGTH + 5]);
  4085. #endif
  4086. STp->header_ok = 0;
  4087. }
  4088. i = STp->first_frame_position;
  4089. if (STp->header_ok && i == osst_get_frame_position(STp, &SRpnt)) {
  4090. if (STp->door_locked == ST_UNLOCKED) {
  4091. if (do_door_lock(STp, 1))
  4092. printk(KERN_INFO "%s:I: Can't lock drive door\n", name);
  4093. else
  4094. STp->door_locked = ST_LOCKED_AUTO;
  4095. }
  4096. if (!STp->frame_in_buffer) {
  4097. STp->block_size = (STm->default_blksize > 0) ?
  4098. STm->default_blksize : OS_DATA_SIZE;
  4099. STp->buffer->buffer_bytes = STp->buffer->read_pointer = 0;
  4100. }
  4101. STp->buffer->buffer_blocks = OS_DATA_SIZE / STp->block_size;
  4102. STp->fast_open = 1;
  4103. scsi_release_request(SRpnt);
  4104. return 0;
  4105. }
  4106. #if DEBUG
  4107. if (i != STp->first_frame_position)
  4108. printk(OSST_DEB_MSG "%s:D: Tape position changed from %d to %d\n",
  4109. name, i, STp->first_frame_position);
  4110. #endif
  4111. STp->header_ok = 0;
  4112. }
  4113. STp->fast_open = 0;
  4114. if ((STp->buffer)->syscall_result != 0 && /* in all error conditions except no medium */
  4115. (SRpnt->sr_sense_buffer[2] != 2 || SRpnt->sr_sense_buffer[12] != 0x3A) ) {
  4116. memset(cmd, 0, MAX_COMMAND_SIZE);
  4117. cmd[0] = MODE_SELECT;
  4118. cmd[1] = 0x10;
  4119. cmd[4] = 4 + MODE_HEADER_LENGTH;
  4120. (STp->buffer)->b_data[0] = cmd[4] - 1;
  4121. (STp->buffer)->b_data[1] = 0; /* Medium Type - ignoring */
  4122. (STp->buffer)->b_data[2] = 0; /* Reserved */
  4123. (STp->buffer)->b_data[3] = 0; /* Block Descriptor Length */
  4124. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 0] = 0x3f;
  4125. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 1] = 1;
  4126. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] = 2;
  4127. (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] = 3;
  4128. #if DEBUG
  4129. printk(OSST_DEB_MSG "%s:D: Applying soft reset\n", name);
  4130. #endif
  4131. SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, STp->timeout, 0, 1);
  4132. STp->header_ok = 0;
  4133. for (i=0; i < 10; i++) {
  4134. memset (cmd, 0, MAX_COMMAND_SIZE);
  4135. cmd[0] = TEST_UNIT_READY;
  4136. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  4137. STp->timeout, MAX_RETRIES, 1);
  4138. if ((SRpnt->sr_sense_buffer[0] & 0x70) != 0x70 ||
  4139. (SRpnt->sr_sense_buffer[2] & 0x0f) == NOT_READY)
  4140. break;
  4141. if ((SRpnt->sr_sense_buffer[2] & 0x0f) == UNIT_ATTENTION) {
  4142. STp->pos_unknown = 0;
  4143. STp->partition = STp->new_partition = 0;
  4144. if (STp->can_partitions)
  4145. STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
  4146. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  4147. STps = &(STp->ps[i]);
  4148. STps->rw = ST_IDLE;
  4149. STps->eof = ST_NOEOF;
  4150. STps->at_sm = 0;
  4151. STps->last_block_valid = 0;
  4152. STps->drv_block = 0;
  4153. STps->drv_file = 0 ;
  4154. }
  4155. new_session = 1;
  4156. }
  4157. }
  4158. }
  4159. if (osst_wait_ready(STp, &SRpnt, 15 * 60, 0)) /* FIXME - not allowed with NOBLOCK */
  4160. printk(KERN_INFO "%s:I: Device did not become Ready in open\n", name);
  4161. if ((STp->buffer)->syscall_result != 0) {
  4162. if ((STp->device)->scsi_level >= SCSI_2 &&
  4163. (SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
  4164. (SRpnt->sr_sense_buffer[2] & 0x0f) == NOT_READY &&
  4165. SRpnt->sr_sense_buffer[12] == 0x3a) { /* Check ASC */
  4166. STp->ready = ST_NO_TAPE;
  4167. } else
  4168. STp->ready = ST_NOT_READY;
  4169. scsi_release_request(SRpnt);
  4170. SRpnt = NULL;
  4171. STp->density = 0; /* Clear the erroneous "residue" */
  4172. STp->write_prot = 0;
  4173. STp->block_size = 0;
  4174. STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
  4175. STp->partition = STp->new_partition = 0;
  4176. STp->door_locked = ST_UNLOCKED;
  4177. return 0;
  4178. }
  4179. osst_configure_onstream(STp, &SRpnt);
  4180. STp->block_size = STp->raw ? OS_FRAME_SIZE : (
  4181. (STm->default_blksize > 0) ? STm->default_blksize : OS_DATA_SIZE);
  4182. STp->buffer->buffer_blocks = STp->raw ? 1 : OS_DATA_SIZE / STp->block_size;
  4183. STp->buffer->buffer_bytes =
  4184. STp->buffer->read_pointer =
  4185. STp->frame_in_buffer = 0;
  4186. #if DEBUG
  4187. if (debugging)
  4188. printk(OSST_DEB_MSG "%s:D: Block size: %d, frame size: %d, buffer size: %d (%d blocks).\n",
  4189. name, STp->block_size, OS_FRAME_SIZE, (STp->buffer)->buffer_size,
  4190. (STp->buffer)->buffer_blocks);
  4191. #endif
  4192. if (STp->drv_write_prot) {
  4193. STp->write_prot = 1;
  4194. #if DEBUG
  4195. if (debugging)
  4196. printk(OSST_DEB_MSG "%s:D: Write protected\n", name);
  4197. #endif
  4198. if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
  4199. retval = (-EROFS);
  4200. goto err_out;
  4201. }
  4202. }
  4203. if (new_session) { /* Change the drive parameters for the new mode */
  4204. #if DEBUG
  4205. if (debugging)
  4206. printk(OSST_DEB_MSG "%s:D: New Session\n", name);
  4207. #endif
  4208. STp->density_changed = STp->blksize_changed = 0;
  4209. STp->compression_changed = 0;
  4210. }
  4211. /*
  4212. * properly position the tape and check the ADR headers
  4213. */
  4214. if (STp->door_locked == ST_UNLOCKED) {
  4215. if (do_door_lock(STp, 1))
  4216. printk(KERN_INFO "%s:I: Can't lock drive door\n", name);
  4217. else
  4218. STp->door_locked = ST_LOCKED_AUTO;
  4219. }
  4220. osst_analyze_headers(STp, &SRpnt);
  4221. scsi_release_request(SRpnt);
  4222. SRpnt = NULL;
  4223. return 0;
  4224. err_out:
  4225. if (SRpnt != NULL)
  4226. scsi_release_request(SRpnt);
  4227. normalize_buffer(STp->buffer);
  4228. STp->header_ok = 0;
  4229. STp->in_use = 0;
  4230. scsi_device_put(STp->device);
  4231. return retval;
  4232. }
  4233. /* Flush the tape buffer before close */
  4234. static int os_scsi_tape_flush(struct file * filp)
  4235. {
  4236. int result = 0, result2;
  4237. struct osst_tape * STp = filp->private_data;
  4238. struct st_modedef * STm = &(STp->modes[STp->current_mode]);
  4239. struct st_partstat * STps = &(STp->ps[STp->partition]);
  4240. struct scsi_request * SRpnt = NULL;
  4241. char * name = tape_name(STp);
  4242. if (file_count(filp) > 1)
  4243. return 0;
  4244. if ((STps->rw == ST_WRITING || STp->dirty) && !STp->pos_unknown) {
  4245. STp->write_type = OS_WRITE_DATA;
  4246. result = osst_flush_write_buffer(STp, &SRpnt);
  4247. if (result != 0 && result != (-ENOSPC))
  4248. goto out;
  4249. }
  4250. if ( STps->rw >= ST_WRITING && !STp->pos_unknown) {
  4251. #if DEBUG
  4252. if (debugging) {
  4253. printk(OSST_DEB_MSG "%s:D: File length %ld bytes.\n",
  4254. name, (long)(filp->f_pos));
  4255. printk(OSST_DEB_MSG "%s:D: Async write waits %d, finished %d.\n",
  4256. name, STp->nbr_waits, STp->nbr_finished);
  4257. }
  4258. #endif
  4259. result = osst_write_trailer(STp, &SRpnt, !(STp->rew_at_close));
  4260. #if DEBUG
  4261. if (debugging)
  4262. printk(OSST_DEB_MSG "%s:D: Buffer flushed, %d EOF(s) written\n",
  4263. name, 1+STp->two_fm);
  4264. #endif
  4265. }
  4266. else if (!STp->rew_at_close) {
  4267. STps = &(STp->ps[STp->partition]);
  4268. if (!STm->sysv || STps->rw != ST_READING) {
  4269. if (STp->can_bsr)
  4270. result = osst_flush_buffer(STp, &SRpnt, 0); /* this is the default path */
  4271. else if (STps->eof == ST_FM_HIT) {
  4272. result = cross_eof(STp, &SRpnt, 0);
  4273. if (result) {
  4274. if (STps->drv_file >= 0)
  4275. STps->drv_file++;
  4276. STps->drv_block = 0;
  4277. STps->eof = ST_FM;
  4278. }
  4279. else
  4280. STps->eof = ST_NOEOF;
  4281. }
  4282. }
  4283. else if ((STps->eof == ST_NOEOF &&
  4284. !(result = cross_eof(STp, &SRpnt, 1))) ||
  4285. STps->eof == ST_FM_HIT) {
  4286. if (STps->drv_file >= 0)
  4287. STps->drv_file++;
  4288. STps->drv_block = 0;
  4289. STps->eof = ST_FM;
  4290. }
  4291. }
  4292. out:
  4293. if (STp->rew_at_close) {
  4294. result2 = osst_position_tape_and_confirm(STp, &SRpnt, STp->first_data_ppos);
  4295. STps->drv_file = STps->drv_block = STp->frame_seq_number = STp->logical_blk_num = 0;
  4296. if (result == 0 && result2 < 0)
  4297. result = result2;
  4298. }
  4299. if (SRpnt) scsi_release_request(SRpnt);
  4300. if (STp->abort_count || STp->recover_count) {
  4301. printk(KERN_INFO "%s:I:", name);
  4302. if (STp->abort_count)
  4303. printk(" %d unrecovered errors", STp->abort_count);
  4304. if (STp->recover_count)
  4305. printk(" %d recovered errors", STp->recover_count);
  4306. if (STp->write_count)
  4307. printk(" in %d frames written", STp->write_count);
  4308. if (STp->read_count)
  4309. printk(" in %d frames read", STp->read_count);
  4310. printk("\n");
  4311. STp->recover_count = 0;
  4312. STp->abort_count = 0;
  4313. }
  4314. STp->write_count = 0;
  4315. STp->read_count = 0;
  4316. return result;
  4317. }
  4318. /* Close the device and release it */
  4319. static int os_scsi_tape_close(struct inode * inode, struct file * filp)
  4320. {
  4321. int result = 0;
  4322. struct osst_tape * STp = filp->private_data;
  4323. if (STp->door_locked == ST_LOCKED_AUTO)
  4324. do_door_lock(STp, 0);
  4325. if (STp->raw)
  4326. STp->header_ok = 0;
  4327. normalize_buffer(STp->buffer);
  4328. write_lock(&os_scsi_tapes_lock);
  4329. STp->in_use = 0;
  4330. write_unlock(&os_scsi_tapes_lock);
  4331. scsi_device_put(STp->device);
  4332. return result;
  4333. }
  4334. /* The ioctl command */
  4335. static int osst_ioctl(struct inode * inode,struct file * file,
  4336. unsigned int cmd_in, unsigned long arg)
  4337. {
  4338. int i, cmd_nr, cmd_type, retval = 0;
  4339. unsigned int blk;
  4340. struct st_modedef * STm;
  4341. struct st_partstat * STps;
  4342. struct scsi_request * SRpnt = NULL;
  4343. struct osst_tape * STp = file->private_data;
  4344. char * name = tape_name(STp);
  4345. void __user * p = (void __user *)arg;
  4346. if (down_interruptible(&STp->lock))
  4347. return -ERESTARTSYS;
  4348. #if DEBUG
  4349. if (debugging && !STp->in_use) {
  4350. printk(OSST_DEB_MSG "%s:D: Incorrect device.\n", name);
  4351. retval = (-EIO);
  4352. goto out;
  4353. }
  4354. #endif
  4355. STm = &(STp->modes[STp->current_mode]);
  4356. STps = &(STp->ps[STp->partition]);
  4357. /*
  4358. * If we are in the middle of error recovery, don't let anyone
  4359. * else try and use this device. Also, if error recovery fails, it
  4360. * may try and take the device offline, in which case all further
  4361. * access to the device is prohibited.
  4362. */
  4363. if( !scsi_block_when_processing_errors(STp->device) ) {
  4364. retval = (-ENXIO);
  4365. goto out;
  4366. }
  4367. cmd_type = _IOC_TYPE(cmd_in);
  4368. cmd_nr = _IOC_NR(cmd_in);
  4369. #if DEBUG
  4370. printk(OSST_DEB_MSG "%s:D: Ioctl %d,%d in %s mode\n", name,
  4371. cmd_type, cmd_nr, STp->raw?"raw":"normal");
  4372. #endif
  4373. if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
  4374. struct mtop mtc;
  4375. int auto_weof = 0;
  4376. if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
  4377. retval = (-EINVAL);
  4378. goto out;
  4379. }
  4380. i = copy_from_user((char *) &mtc, p, sizeof(struct mtop));
  4381. if (i) {
  4382. retval = (-EFAULT);
  4383. goto out;
  4384. }
  4385. if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
  4386. printk(KERN_WARNING "%s:W: MTSETDRVBUFFER only allowed for root.\n", name);
  4387. retval = (-EPERM);
  4388. goto out;
  4389. }
  4390. if (!STm->defined && (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
  4391. retval = (-ENXIO);
  4392. goto out;
  4393. }
  4394. if (!STp->pos_unknown) {
  4395. if (STps->eof == ST_FM_HIT) {
  4396. if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
  4397. mtc.mt_count -= 1;
  4398. if (STps->drv_file >= 0)
  4399. STps->drv_file += 1;
  4400. }
  4401. else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
  4402. mtc.mt_count += 1;
  4403. if (STps->drv_file >= 0)
  4404. STps->drv_file += 1;
  4405. }
  4406. }
  4407. if (mtc.mt_op == MTSEEK) {
  4408. /* Old position must be restored if partition will be changed */
  4409. i = !STp->can_partitions || (STp->new_partition != STp->partition);
  4410. }
  4411. else {
  4412. i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
  4413. mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
  4414. mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
  4415. mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
  4416. mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
  4417. mtc.mt_op == MTCOMPRESSION;
  4418. }
  4419. i = osst_flush_buffer(STp, &SRpnt, i);
  4420. if (i < 0) {
  4421. retval = i;
  4422. goto out;
  4423. }
  4424. }
  4425. else {
  4426. /*
  4427. * If there was a bus reset, block further access
  4428. * to this device. If the user wants to rewind the tape,
  4429. * then reset the flag and allow access again.
  4430. */
  4431. if(mtc.mt_op != MTREW &&
  4432. mtc.mt_op != MTOFFL &&
  4433. mtc.mt_op != MTRETEN &&
  4434. mtc.mt_op != MTERASE &&
  4435. mtc.mt_op != MTSEEK &&
  4436. mtc.mt_op != MTEOM) {
  4437. retval = (-EIO);
  4438. goto out;
  4439. }
  4440. reset_state(STp);
  4441. /* remove this when the midlevel properly clears was_reset */
  4442. STp->device->was_reset = 0;
  4443. }
  4444. if (mtc.mt_op != MTCOMPRESSION && mtc.mt_op != MTLOCK &&
  4445. mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
  4446. mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTSETDRVBUFFER &&
  4447. mtc.mt_op != MTMKPART && mtc.mt_op != MTSETPART &&
  4448. mtc.mt_op != MTWEOF && mtc.mt_op != MTWSM ) {
  4449. /*
  4450. * The user tells us to move to another position on the tape.
  4451. * If we were appending to the tape content, that would leave
  4452. * the tape without proper end, in that case write EOD and
  4453. * update the header to reflect its position.
  4454. */
  4455. #if DEBUG
  4456. printk(KERN_WARNING "%s:D: auto_weod %s at ffp=%d,efp=%d,fsn=%d,lbn=%d,fn=%d,bn=%d\n", name,
  4457. STps->rw >= ST_WRITING ? "write" : STps->rw == ST_READING ? "read" : "idle",
  4458. STp->first_frame_position, STp->eod_frame_ppos, STp->frame_seq_number,
  4459. STp->logical_blk_num, STps->drv_file, STps->drv_block );
  4460. #endif
  4461. if (STps->rw >= ST_WRITING && STp->first_frame_position >= STp->eod_frame_ppos) {
  4462. auto_weof = ((STp->write_type != OS_WRITE_NEW_MARK) &&
  4463. !(mtc.mt_op == MTREW || mtc.mt_op == MTOFFL));
  4464. i = osst_write_trailer(STp, &SRpnt,
  4465. !(mtc.mt_op == MTREW || mtc.mt_op == MTOFFL));
  4466. #if DEBUG
  4467. printk(KERN_WARNING "%s:D: post trailer xeof=%d,ffp=%d,efp=%d,fsn=%d,lbn=%d,fn=%d,bn=%d\n",
  4468. name, auto_weof, STp->first_frame_position, STp->eod_frame_ppos,
  4469. STp->frame_seq_number, STp->logical_blk_num, STps->drv_file, STps->drv_block );
  4470. #endif
  4471. if (i < 0) {
  4472. retval = i;
  4473. goto out;
  4474. }
  4475. }
  4476. STps->rw = ST_IDLE;
  4477. }
  4478. if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
  4479. do_door_lock(STp, 0); /* Ignore result! */
  4480. if (mtc.mt_op == MTSETDRVBUFFER &&
  4481. (mtc.mt_count & MT_ST_OPTIONS) != 0) {
  4482. retval = osst_set_options(STp, mtc.mt_count);
  4483. goto out;
  4484. }
  4485. if (mtc.mt_op == MTSETPART) {
  4486. if (mtc.mt_count >= STp->nbr_partitions)
  4487. retval = -EINVAL;
  4488. else {
  4489. STp->new_partition = mtc.mt_count;
  4490. retval = 0;
  4491. }
  4492. goto out;
  4493. }
  4494. if (mtc.mt_op == MTMKPART) {
  4495. if (!STp->can_partitions) {
  4496. retval = (-EINVAL);
  4497. goto out;
  4498. }
  4499. if ((i = osst_int_ioctl(STp, &SRpnt, MTREW, 0)) < 0 /*||
  4500. (i = partition_tape(inode, mtc.mt_count)) < 0*/) {
  4501. retval = i;
  4502. goto out;
  4503. }
  4504. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  4505. STp->ps[i].rw = ST_IDLE;
  4506. STp->ps[i].at_sm = 0;
  4507. STp->ps[i].last_block_valid = 0;
  4508. }
  4509. STp->partition = STp->new_partition = 0;
  4510. STp->nbr_partitions = 1; /* Bad guess ?-) */
  4511. STps->drv_block = STps->drv_file = 0;
  4512. retval = 0;
  4513. goto out;
  4514. }
  4515. if (mtc.mt_op == MTSEEK) {
  4516. if (STp->raw)
  4517. i = osst_set_frame_position(STp, &SRpnt, mtc.mt_count, 0);
  4518. else
  4519. i = osst_seek_sector(STp, &SRpnt, mtc.mt_count);
  4520. if (!STp->can_partitions)
  4521. STp->ps[0].rw = ST_IDLE;
  4522. retval = i;
  4523. goto out;
  4524. }
  4525. if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
  4526. retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
  4527. goto out;
  4528. }
  4529. if (auto_weof)
  4530. cross_eof(STp, &SRpnt, 0);
  4531. if (mtc.mt_op == MTCOMPRESSION)
  4532. retval = -EINVAL; /* OnStream drives don't have compression hardware */
  4533. else
  4534. /* MTBSF MTBSFM MTBSR MTBSS MTEOM MTERASE MTFSF MTFSFB MTFSR MTFSS
  4535. * MTLOAD MTOFFL MTRESET MTRETEN MTREW MTUNLOAD MTWEOF MTWSM */
  4536. retval = osst_int_ioctl(STp, &SRpnt, mtc.mt_op, mtc.mt_count);
  4537. goto out;
  4538. }
  4539. if (!STm->defined) {
  4540. retval = (-ENXIO);
  4541. goto out;
  4542. }
  4543. if ((i = osst_flush_buffer(STp, &SRpnt, 0)) < 0) {
  4544. retval = i;
  4545. goto out;
  4546. }
  4547. if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
  4548. struct mtget mt_status;
  4549. if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
  4550. retval = (-EINVAL);
  4551. goto out;
  4552. }
  4553. mt_status.mt_type = MT_ISONSTREAM_SC;
  4554. mt_status.mt_erreg = STp->recover_erreg << MT_ST_SOFTERR_SHIFT;
  4555. mt_status.mt_dsreg =
  4556. ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
  4557. ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
  4558. mt_status.mt_blkno = STps->drv_block;
  4559. mt_status.mt_fileno = STps->drv_file;
  4560. if (STp->block_size != 0) {
  4561. if (STps->rw == ST_WRITING)
  4562. mt_status.mt_blkno += (STp->buffer)->buffer_bytes / STp->block_size;
  4563. else if (STps->rw == ST_READING)
  4564. mt_status.mt_blkno -= ((STp->buffer)->buffer_bytes +
  4565. STp->block_size - 1) / STp->block_size;
  4566. }
  4567. mt_status.mt_gstat = 0;
  4568. if (STp->drv_write_prot)
  4569. mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
  4570. if (mt_status.mt_blkno == 0) {
  4571. if (mt_status.mt_fileno == 0)
  4572. mt_status.mt_gstat |= GMT_BOT(0xffffffff);
  4573. else
  4574. mt_status.mt_gstat |= GMT_EOF(0xffffffff);
  4575. }
  4576. mt_status.mt_resid = STp->partition;
  4577. if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
  4578. mt_status.mt_gstat |= GMT_EOT(0xffffffff);
  4579. else if (STps->eof >= ST_EOM_OK)
  4580. mt_status.mt_gstat |= GMT_EOD(0xffffffff);
  4581. if (STp->density == 1)
  4582. mt_status.mt_gstat |= GMT_D_800(0xffffffff);
  4583. else if (STp->density == 2)
  4584. mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
  4585. else if (STp->density == 3)
  4586. mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
  4587. if (STp->ready == ST_READY)
  4588. mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
  4589. if (STp->ready == ST_NO_TAPE)
  4590. mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
  4591. if (STps->at_sm)
  4592. mt_status.mt_gstat |= GMT_SM(0xffffffff);
  4593. if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
  4594. STp->drv_buffer != 0)
  4595. mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
  4596. i = copy_to_user(p, &mt_status, sizeof(struct mtget));
  4597. if (i) {
  4598. retval = (-EFAULT);
  4599. goto out;
  4600. }
  4601. STp->recover_erreg = 0; /* Clear after read */
  4602. retval = 0;
  4603. goto out;
  4604. } /* End of MTIOCGET */
  4605. if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
  4606. struct mtpos mt_pos;
  4607. if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
  4608. retval = (-EINVAL);
  4609. goto out;
  4610. }
  4611. if (STp->raw)
  4612. blk = osst_get_frame_position(STp, &SRpnt);
  4613. else
  4614. blk = osst_get_sector(STp, &SRpnt);
  4615. if (blk < 0) {
  4616. retval = blk;
  4617. goto out;
  4618. }
  4619. mt_pos.mt_blkno = blk;
  4620. i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
  4621. if (i)
  4622. retval = -EFAULT;
  4623. goto out;
  4624. }
  4625. if (SRpnt) scsi_release_request(SRpnt);
  4626. up(&STp->lock);
  4627. return scsi_ioctl(STp->device, cmd_in, p);
  4628. out:
  4629. if (SRpnt) scsi_release_request(SRpnt);
  4630. up(&STp->lock);
  4631. return retval;
  4632. }
  4633. #ifdef CONFIG_COMPAT
  4634. static long osst_compat_ioctl(struct file * file, unsigned int cmd_in, unsigned long arg)
  4635. {
  4636. struct osst_tape *STp = file->private_data;
  4637. struct scsi_device *sdev = STp->device;
  4638. int ret = -ENOIOCTLCMD;
  4639. if (sdev->host->hostt->compat_ioctl) {
  4640. ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
  4641. }
  4642. return ret;
  4643. }
  4644. #endif
  4645. /* Memory handling routines */
  4646. /* Try to allocate a new tape buffer skeleton. Caller must not hold os_scsi_tapes_lock */
  4647. static struct osst_buffer * new_tape_buffer( int from_initialization, int need_dma, int max_sg )
  4648. {
  4649. int i, priority;
  4650. struct osst_buffer *tb;
  4651. if (from_initialization)
  4652. priority = GFP_ATOMIC;
  4653. else
  4654. priority = GFP_KERNEL;
  4655. i = sizeof(struct osst_buffer) + (osst_max_sg_segs - 1) * sizeof(struct scatterlist);
  4656. tb = (struct osst_buffer *)kmalloc(i, priority);
  4657. if (!tb) {
  4658. printk(KERN_NOTICE "osst :I: Can't allocate new tape buffer.\n");
  4659. return NULL;
  4660. }
  4661. memset(tb, 0, i);
  4662. tb->sg_segs = tb->orig_sg_segs = 0;
  4663. tb->use_sg = max_sg;
  4664. tb->in_use = 1;
  4665. tb->dma = need_dma;
  4666. tb->buffer_size = 0;
  4667. #if DEBUG
  4668. if (debugging)
  4669. printk(OSST_DEB_MSG
  4670. "osst :D: Allocated tape buffer skeleton (%d bytes, %d segments, dma: %d).\n",
  4671. i, max_sg, need_dma);
  4672. #endif
  4673. return tb;
  4674. }
  4675. /* Try to allocate a temporary (while a user has the device open) enlarged tape buffer */
  4676. static int enlarge_buffer(struct osst_buffer *STbuffer, int need_dma)
  4677. {
  4678. int segs, nbr, max_segs, b_size, priority, order, got;
  4679. if (STbuffer->buffer_size >= OS_FRAME_SIZE)
  4680. return 1;
  4681. if (STbuffer->sg_segs) {
  4682. printk(KERN_WARNING "osst :A: Buffer not previously normalized.\n");
  4683. normalize_buffer(STbuffer);
  4684. }
  4685. /* See how many segments we can use -- need at least two */
  4686. nbr = max_segs = STbuffer->use_sg;
  4687. if (nbr <= 2)
  4688. return 0;
  4689. priority = GFP_KERNEL /* | __GFP_NOWARN */;
  4690. if (need_dma)
  4691. priority |= GFP_DMA;
  4692. /* Try to allocate the first segment up to OS_DATA_SIZE and the others
  4693. big enough to reach the goal (code assumes no segments in place) */
  4694. for (b_size = OS_DATA_SIZE, order = OSST_FIRST_ORDER; b_size >= PAGE_SIZE; order--, b_size /= 2) {
  4695. STbuffer->sg[0].page = alloc_pages(priority, order);
  4696. STbuffer->sg[0].offset = 0;
  4697. if (STbuffer->sg[0].page != NULL) {
  4698. STbuffer->sg[0].length = b_size;
  4699. STbuffer->b_data = page_address(STbuffer->sg[0].page);
  4700. break;
  4701. }
  4702. }
  4703. if (STbuffer->sg[0].page == NULL) {
  4704. printk(KERN_NOTICE "osst :I: Can't allocate tape buffer main segment.\n");
  4705. return 0;
  4706. }
  4707. /* Got initial segment of 'bsize,order', continue with same size if possible, except for AUX */
  4708. for (segs=STbuffer->sg_segs=1, got=b_size;
  4709. segs < max_segs && got < OS_FRAME_SIZE; ) {
  4710. STbuffer->sg[segs].page =
  4711. alloc_pages(priority, (OS_FRAME_SIZE - got <= PAGE_SIZE) ? 0 : order);
  4712. STbuffer->sg[segs].offset = 0;
  4713. if (STbuffer->sg[segs].page == NULL) {
  4714. if (OS_FRAME_SIZE - got <= (max_segs - segs) * b_size / 2 && order) {
  4715. b_size /= 2; /* Large enough for the rest of the buffers */
  4716. order--;
  4717. continue;
  4718. }
  4719. printk(KERN_WARNING "osst :W: Failed to enlarge buffer to %d bytes.\n",
  4720. OS_FRAME_SIZE);
  4721. #if DEBUG
  4722. STbuffer->buffer_size = got;
  4723. #endif
  4724. normalize_buffer(STbuffer);
  4725. return 0;
  4726. }
  4727. STbuffer->sg[segs].length = (OS_FRAME_SIZE - got <= PAGE_SIZE / 2) ? (OS_FRAME_SIZE - got) : b_size;
  4728. got += STbuffer->sg[segs].length;
  4729. STbuffer->buffer_size = got;
  4730. STbuffer->sg_segs = ++segs;
  4731. }
  4732. #if DEBUG
  4733. if (debugging) {
  4734. printk(OSST_DEB_MSG
  4735. "osst :D: Expanded tape buffer (%d bytes, %d->%d segments, dma: %d, at: %p).\n",
  4736. got, STbuffer->orig_sg_segs, STbuffer->sg_segs, need_dma, STbuffer->b_data);
  4737. printk(OSST_DEB_MSG
  4738. "osst :D: segment sizes: first %d at %p, last %d bytes at %p.\n",
  4739. STbuffer->sg[0].length, page_address(STbuffer->sg[0].page),
  4740. STbuffer->sg[segs-1].length, page_address(STbuffer->sg[segs-1].page));
  4741. }
  4742. #endif
  4743. return 1;
  4744. }
  4745. /* Release the segments */
  4746. static void normalize_buffer(struct osst_buffer *STbuffer)
  4747. {
  4748. int i, order, b_size;
  4749. for (i=0; i < STbuffer->sg_segs; i++) {
  4750. for (b_size = PAGE_SIZE, order = 0;
  4751. b_size < STbuffer->sg[i].length;
  4752. b_size *= 2, order++);
  4753. __free_pages(STbuffer->sg[i].page, order);
  4754. STbuffer->buffer_size -= STbuffer->sg[i].length;
  4755. }
  4756. #if DEBUG
  4757. if (debugging && STbuffer->orig_sg_segs < STbuffer->sg_segs)
  4758. printk(OSST_DEB_MSG "osst :D: Buffer at %p normalized to %d bytes (segs %d).\n",
  4759. STbuffer->b_data, STbuffer->buffer_size, STbuffer->sg_segs);
  4760. #endif
  4761. STbuffer->sg_segs = STbuffer->orig_sg_segs = 0;
  4762. }
  4763. /* Move data from the user buffer to the tape buffer. Returns zero (success) or
  4764. negative error code. */
  4765. static int append_to_buffer(const char __user *ubp, struct osst_buffer *st_bp, int do_count)
  4766. {
  4767. int i, cnt, res, offset;
  4768. for (i=0, offset=st_bp->buffer_bytes;
  4769. i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
  4770. offset -= st_bp->sg[i].length;
  4771. if (i == st_bp->sg_segs) { /* Should never happen */
  4772. printk(KERN_WARNING "osst :A: Append_to_buffer offset overflow.\n");
  4773. return (-EIO);
  4774. }
  4775. for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
  4776. cnt = st_bp->sg[i].length - offset < do_count ?
  4777. st_bp->sg[i].length - offset : do_count;
  4778. res = copy_from_user(page_address(st_bp->sg[i].page) + offset, ubp, cnt);
  4779. if (res)
  4780. return (-EFAULT);
  4781. do_count -= cnt;
  4782. st_bp->buffer_bytes += cnt;
  4783. ubp += cnt;
  4784. offset = 0;
  4785. }
  4786. if (do_count) { /* Should never happen */
  4787. printk(KERN_WARNING "osst :A: Append_to_buffer overflow (left %d).\n",
  4788. do_count);
  4789. return (-EIO);
  4790. }
  4791. return 0;
  4792. }
  4793. /* Move data from the tape buffer to the user buffer. Returns zero (success) or
  4794. negative error code. */
  4795. static int from_buffer(struct osst_buffer *st_bp, char __user *ubp, int do_count)
  4796. {
  4797. int i, cnt, res, offset;
  4798. for (i=0, offset=st_bp->read_pointer;
  4799. i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
  4800. offset -= st_bp->sg[i].length;
  4801. if (i == st_bp->sg_segs) { /* Should never happen */
  4802. printk(KERN_WARNING "osst :A: From_buffer offset overflow.\n");
  4803. return (-EIO);
  4804. }
  4805. for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
  4806. cnt = st_bp->sg[i].length - offset < do_count ?
  4807. st_bp->sg[i].length - offset : do_count;
  4808. res = copy_to_user(ubp, page_address(st_bp->sg[i].page) + offset, cnt);
  4809. if (res)
  4810. return (-EFAULT);
  4811. do_count -= cnt;
  4812. st_bp->buffer_bytes -= cnt;
  4813. st_bp->read_pointer += cnt;
  4814. ubp += cnt;
  4815. offset = 0;
  4816. }
  4817. if (do_count) { /* Should never happen */
  4818. printk(KERN_WARNING "osst :A: From_buffer overflow (left %d).\n", do_count);
  4819. return (-EIO);
  4820. }
  4821. return 0;
  4822. }
  4823. /* Sets the tail of the buffer after fill point to zero.
  4824. Returns zero (success) or negative error code. */
  4825. static int osst_zero_buffer_tail(struct osst_buffer *st_bp)
  4826. {
  4827. int i, offset, do_count, cnt;
  4828. for (i = 0, offset = st_bp->buffer_bytes;
  4829. i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
  4830. offset -= st_bp->sg[i].length;
  4831. if (i == st_bp->sg_segs) { /* Should never happen */
  4832. printk(KERN_WARNING "osst :A: Zero_buffer offset overflow.\n");
  4833. return (-EIO);
  4834. }
  4835. for (do_count = OS_DATA_SIZE - st_bp->buffer_bytes;
  4836. i < st_bp->sg_segs && do_count > 0; i++) {
  4837. cnt = st_bp->sg[i].length - offset < do_count ?
  4838. st_bp->sg[i].length - offset : do_count ;
  4839. memset(page_address(st_bp->sg[i].page) + offset, 0, cnt);
  4840. do_count -= cnt;
  4841. offset = 0;
  4842. }
  4843. if (do_count) { /* Should never happen */
  4844. printk(KERN_WARNING "osst :A: Zero_buffer overflow (left %d).\n", do_count);
  4845. return (-EIO);
  4846. }
  4847. return 0;
  4848. }
  4849. /* Copy a osst 32K chunk of memory into the buffer.
  4850. Returns zero (success) or negative error code. */
  4851. static int osst_copy_to_buffer(struct osst_buffer *st_bp, unsigned char *ptr)
  4852. {
  4853. int i, cnt, do_count = OS_DATA_SIZE;
  4854. for (i = 0; i < st_bp->sg_segs && do_count > 0; i++) {
  4855. cnt = st_bp->sg[i].length < do_count ?
  4856. st_bp->sg[i].length : do_count ;
  4857. memcpy(page_address(st_bp->sg[i].page), ptr, cnt);
  4858. do_count -= cnt;
  4859. ptr += cnt;
  4860. }
  4861. if (do_count || i != st_bp->sg_segs-1) { /* Should never happen */
  4862. printk(KERN_WARNING "osst :A: Copy_to_buffer overflow (left %d at sg %d).\n",
  4863. do_count, i);
  4864. return (-EIO);
  4865. }
  4866. return 0;
  4867. }
  4868. /* Copy a osst 32K chunk of memory from the buffer.
  4869. Returns zero (success) or negative error code. */
  4870. static int osst_copy_from_buffer(struct osst_buffer *st_bp, unsigned char *ptr)
  4871. {
  4872. int i, cnt, do_count = OS_DATA_SIZE;
  4873. for (i = 0; i < st_bp->sg_segs && do_count > 0; i++) {
  4874. cnt = st_bp->sg[i].length < do_count ?
  4875. st_bp->sg[i].length : do_count ;
  4876. memcpy(ptr, page_address(st_bp->sg[i].page), cnt);
  4877. do_count -= cnt;
  4878. ptr += cnt;
  4879. }
  4880. if (do_count || i != st_bp->sg_segs-1) { /* Should never happen */
  4881. printk(KERN_WARNING "osst :A: Copy_from_buffer overflow (left %d at sg %d).\n",
  4882. do_count, i);
  4883. return (-EIO);
  4884. }
  4885. return 0;
  4886. }
  4887. /* Module housekeeping */
  4888. static void validate_options (void)
  4889. {
  4890. if (max_dev > 0)
  4891. osst_max_dev = max_dev;
  4892. if (write_threshold_kbs > 0)
  4893. osst_write_threshold = write_threshold_kbs * ST_KILOBYTE;
  4894. if (osst_write_threshold > osst_buffer_size)
  4895. osst_write_threshold = osst_buffer_size;
  4896. if (max_sg_segs >= OSST_FIRST_SG)
  4897. osst_max_sg_segs = max_sg_segs;
  4898. #if DEBUG
  4899. printk(OSST_DEB_MSG "osst :D: max tapes %d, write threshold %d, max s/g segs %d.\n",
  4900. osst_max_dev, osst_write_threshold, osst_max_sg_segs);
  4901. #endif
  4902. }
  4903. #ifndef MODULE
  4904. /* Set the boot options. Syntax: osst=xxx,yyy,...
  4905. where xxx is write threshold in 1024 byte blocks,
  4906. and yyy is number of s/g segments to use. */
  4907. static int __init osst_setup (char *str)
  4908. {
  4909. int i, ints[5];
  4910. char *stp;
  4911. stp = get_options(str, ARRAY_SIZE(ints), ints);
  4912. if (ints[0] > 0) {
  4913. for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
  4914. *parms[i].val = ints[i + 1];
  4915. } else {
  4916. while (stp != NULL) {
  4917. for (i = 0; i < ARRAY_SIZE(parms); i++) {
  4918. int len = strlen(parms[i].name);
  4919. if (!strncmp(stp, parms[i].name, len) &&
  4920. (*(stp + len) == ':' || *(stp + len) == '=')) {
  4921. *parms[i].val =
  4922. simple_strtoul(stp + len + 1, NULL, 0);
  4923. break;
  4924. }
  4925. }
  4926. if (i >= sizeof(parms) / sizeof(struct osst_dev_parm))
  4927. printk(KERN_INFO "osst :I: Illegal parameter in '%s'\n",
  4928. stp);
  4929. stp = strchr(stp, ',');
  4930. if (stp)
  4931. stp++;
  4932. }
  4933. }
  4934. return 1;
  4935. }
  4936. __setup("osst=", osst_setup);
  4937. #endif
  4938. static struct file_operations osst_fops = {
  4939. .owner = THIS_MODULE,
  4940. .read = osst_read,
  4941. .write = osst_write,
  4942. .ioctl = osst_ioctl,
  4943. #ifdef CONFIG_COMPAT
  4944. .compat_ioctl = osst_compat_ioctl,
  4945. #endif
  4946. .open = os_scsi_tape_open,
  4947. .flush = os_scsi_tape_flush,
  4948. .release = os_scsi_tape_close,
  4949. };
  4950. static int osst_supports(struct scsi_device * SDp)
  4951. {
  4952. struct osst_support_data {
  4953. char *vendor;
  4954. char *model;
  4955. char *rev;
  4956. char *driver_hint; /* Name of the correct driver, NULL if unknown */
  4957. };
  4958. static struct osst_support_data support_list[] = {
  4959. /* {"XXX", "Yy-", "", NULL}, example */
  4960. SIGS_FROM_OSST,
  4961. {NULL, }};
  4962. struct osst_support_data *rp;
  4963. /* We are willing to drive OnStream SC-x0 as well as the
  4964. * * IDE, ParPort, FireWire, USB variants, if accessible by
  4965. * * emulation layer (ide-scsi, usb-storage, ...) */
  4966. for (rp=&(support_list[0]); rp->vendor != NULL; rp++)
  4967. if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
  4968. !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
  4969. !strncmp(rp->rev, SDp->rev, strlen(rp->rev)))
  4970. return 1;
  4971. return 0;
  4972. }
  4973. /*
  4974. * sysfs support for osst driver parameter information
  4975. */
  4976. static ssize_t osst_version_show(struct device_driver *ddd, char *buf)
  4977. {
  4978. return snprintf(buf, PAGE_SIZE, "%s\n", osst_version);
  4979. }
  4980. static DRIVER_ATTR(version, S_IRUGO, osst_version_show, NULL);
  4981. static void osst_create_driverfs_files(struct device_driver *driverfs)
  4982. {
  4983. driver_create_file(driverfs, &driver_attr_version);
  4984. }
  4985. static void osst_remove_driverfs_files(struct device_driver *driverfs)
  4986. {
  4987. driver_remove_file(driverfs, &driver_attr_version);
  4988. }
  4989. /*
  4990. * sysfs support for accessing ADR header information
  4991. */
  4992. static ssize_t osst_adr_rev_show(struct class_device *class_dev, char *buf)
  4993. {
  4994. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  4995. ssize_t l = 0;
  4996. if (STp && STp->header_ok && STp->linux_media)
  4997. l = snprintf(buf, PAGE_SIZE, "%d.%d\n", STp->header_cache->major_rev, STp->header_cache->minor_rev);
  4998. return l;
  4999. }
  5000. CLASS_DEVICE_ATTR(ADR_rev, S_IRUGO, osst_adr_rev_show, NULL);
  5001. static ssize_t osst_linux_media_version_show(struct class_device *class_dev, char *buf)
  5002. {
  5003. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  5004. ssize_t l = 0;
  5005. if (STp && STp->header_ok && STp->linux_media)
  5006. l = snprintf(buf, PAGE_SIZE, "LIN%d\n", STp->linux_media_version);
  5007. return l;
  5008. }
  5009. CLASS_DEVICE_ATTR(media_version, S_IRUGO, osst_linux_media_version_show, NULL);
  5010. static ssize_t osst_capacity_show(struct class_device *class_dev, char *buf)
  5011. {
  5012. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  5013. ssize_t l = 0;
  5014. if (STp && STp->header_ok && STp->linux_media)
  5015. l = snprintf(buf, PAGE_SIZE, "%d\n", STp->capacity);
  5016. return l;
  5017. }
  5018. CLASS_DEVICE_ATTR(capacity, S_IRUGO, osst_capacity_show, NULL);
  5019. static ssize_t osst_first_data_ppos_show(struct class_device *class_dev, char *buf)
  5020. {
  5021. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  5022. ssize_t l = 0;
  5023. if (STp && STp->header_ok && STp->linux_media)
  5024. l = snprintf(buf, PAGE_SIZE, "%d\n", STp->first_data_ppos);
  5025. return l;
  5026. }
  5027. CLASS_DEVICE_ATTR(BOT_frame, S_IRUGO, osst_first_data_ppos_show, NULL);
  5028. static ssize_t osst_eod_frame_ppos_show(struct class_device *class_dev, char *buf)
  5029. {
  5030. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  5031. ssize_t l = 0;
  5032. if (STp && STp->header_ok && STp->linux_media)
  5033. l = snprintf(buf, PAGE_SIZE, "%d\n", STp->eod_frame_ppos);
  5034. return l;
  5035. }
  5036. CLASS_DEVICE_ATTR(EOD_frame, S_IRUGO, osst_eod_frame_ppos_show, NULL);
  5037. static ssize_t osst_filemark_cnt_show(struct class_device *class_dev, char *buf)
  5038. {
  5039. struct osst_tape * STp = (struct osst_tape *) class_get_devdata (class_dev);
  5040. ssize_t l = 0;
  5041. if (STp && STp->header_ok && STp->linux_media)
  5042. l = snprintf(buf, PAGE_SIZE, "%d\n", STp->filemark_cnt);
  5043. return l;
  5044. }
  5045. CLASS_DEVICE_ATTR(file_count, S_IRUGO, osst_filemark_cnt_show, NULL);
  5046. static struct class *osst_sysfs_class;
  5047. static int osst_sysfs_valid = 0;
  5048. static void osst_sysfs_init(void)
  5049. {
  5050. osst_sysfs_class = class_create(THIS_MODULE, "onstream_tape");
  5051. if ( IS_ERR(osst_sysfs_class) )
  5052. printk(KERN_WARNING "osst :W: Unable to register sysfs class\n");
  5053. else
  5054. osst_sysfs_valid = 1;
  5055. }
  5056. static void osst_sysfs_add(dev_t dev, struct device *device, struct osst_tape * STp, char * name)
  5057. {
  5058. struct class_device *osst_class_member;
  5059. if (!osst_sysfs_valid) return;
  5060. osst_class_member = class_device_create(osst_sysfs_class, dev, device, "%s", name);
  5061. if (IS_ERR(osst_class_member)) {
  5062. printk(KERN_WARNING "osst :W: Unable to add sysfs class member %s\n", name);
  5063. return;
  5064. }
  5065. class_set_devdata(osst_class_member, STp);
  5066. class_device_create_file(osst_class_member, &class_device_attr_ADR_rev);
  5067. class_device_create_file(osst_class_member, &class_device_attr_media_version);
  5068. class_device_create_file(osst_class_member, &class_device_attr_capacity);
  5069. class_device_create_file(osst_class_member, &class_device_attr_BOT_frame);
  5070. class_device_create_file(osst_class_member, &class_device_attr_EOD_frame);
  5071. class_device_create_file(osst_class_member, &class_device_attr_file_count);
  5072. }
  5073. static void osst_sysfs_destroy(dev_t dev)
  5074. {
  5075. if (!osst_sysfs_valid) return;
  5076. class_device_destroy(osst_sysfs_class, dev);
  5077. }
  5078. static void osst_sysfs_cleanup(void)
  5079. {
  5080. if (osst_sysfs_valid) {
  5081. class_destroy(osst_sysfs_class);
  5082. osst_sysfs_valid = 0;
  5083. }
  5084. }
  5085. /*
  5086. * osst startup / cleanup code
  5087. */
  5088. static int osst_probe(struct device *dev)
  5089. {
  5090. struct scsi_device * SDp = to_scsi_device(dev);
  5091. struct osst_tape * tpnt;
  5092. struct st_modedef * STm;
  5093. struct st_partstat * STps;
  5094. struct osst_buffer * buffer;
  5095. struct gendisk * drive;
  5096. int i, mode, dev_num;
  5097. if (SDp->type != TYPE_TAPE || !osst_supports(SDp))
  5098. return -ENODEV;
  5099. drive = alloc_disk(1);
  5100. if (!drive) {
  5101. printk(KERN_ERR "osst :E: Out of memory. Device not attached.\n");
  5102. return -ENODEV;
  5103. }
  5104. /* if this is the first attach, build the infrastructure */
  5105. write_lock(&os_scsi_tapes_lock);
  5106. if (os_scsi_tapes == NULL) {
  5107. os_scsi_tapes =
  5108. (struct osst_tape **)kmalloc(osst_max_dev * sizeof(struct osst_tape *),
  5109. GFP_ATOMIC);
  5110. if (os_scsi_tapes == NULL) {
  5111. write_unlock(&os_scsi_tapes_lock);
  5112. printk(KERN_ERR "osst :E: Unable to allocate array for OnStream SCSI tapes.\n");
  5113. goto out_put_disk;
  5114. }
  5115. for (i=0; i < osst_max_dev; ++i) os_scsi_tapes[i] = NULL;
  5116. }
  5117. if (osst_nr_dev >= osst_max_dev) {
  5118. write_unlock(&os_scsi_tapes_lock);
  5119. printk(KERN_ERR "osst :E: Too many tape devices (max. %d).\n", osst_max_dev);
  5120. goto out_put_disk;
  5121. }
  5122. /* find a free minor number */
  5123. for (i=0; os_scsi_tapes[i] && i<osst_max_dev; i++);
  5124. if(i >= osst_max_dev) panic ("Scsi_devices corrupt (osst)");
  5125. dev_num = i;
  5126. /* allocate a struct osst_tape for this device */
  5127. tpnt = (struct osst_tape *)kmalloc(sizeof(struct osst_tape), GFP_ATOMIC);
  5128. if (tpnt == NULL) {
  5129. write_unlock(&os_scsi_tapes_lock);
  5130. printk(KERN_ERR "osst :E: Can't allocate device descriptor, device not attached.\n");
  5131. goto out_put_disk;
  5132. }
  5133. memset(tpnt, 0, sizeof(struct osst_tape));
  5134. /* allocate a buffer for this device */
  5135. i = SDp->host->sg_tablesize;
  5136. if (osst_max_sg_segs < i)
  5137. i = osst_max_sg_segs;
  5138. buffer = new_tape_buffer(1, SDp->host->unchecked_isa_dma, i);
  5139. if (buffer == NULL) {
  5140. write_unlock(&os_scsi_tapes_lock);
  5141. printk(KERN_ERR "osst :E: Unable to allocate a tape buffer, device not attached.\n");
  5142. kfree(tpnt);
  5143. goto out_put_disk;
  5144. }
  5145. os_scsi_tapes[dev_num] = tpnt;
  5146. tpnt->buffer = buffer;
  5147. tpnt->device = SDp;
  5148. drive->private_data = &tpnt->driver;
  5149. sprintf(drive->disk_name, "osst%d", dev_num);
  5150. tpnt->driver = &osst_template;
  5151. tpnt->drive = drive;
  5152. tpnt->in_use = 0;
  5153. tpnt->capacity = 0xfffff;
  5154. tpnt->dirty = 0;
  5155. tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
  5156. tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
  5157. tpnt->density = 0;
  5158. tpnt->do_auto_lock = OSST_AUTO_LOCK;
  5159. tpnt->can_bsr = OSST_IN_FILE_POS;
  5160. tpnt->can_partitions = 0;
  5161. tpnt->two_fm = OSST_TWO_FM;
  5162. tpnt->fast_mteom = OSST_FAST_MTEOM;
  5163. tpnt->scsi2_logical = OSST_SCSI2LOGICAL; /* FIXME */
  5164. tpnt->write_threshold = osst_write_threshold;
  5165. tpnt->default_drvbuffer = 0xff; /* No forced buffering */
  5166. tpnt->partition = 0;
  5167. tpnt->new_partition = 0;
  5168. tpnt->nbr_partitions = 0;
  5169. tpnt->min_block = 512;
  5170. tpnt->max_block = OS_DATA_SIZE;
  5171. tpnt->timeout = OSST_TIMEOUT;
  5172. tpnt->long_timeout = OSST_LONG_TIMEOUT;
  5173. /* Recognize OnStream tapes */
  5174. /* We don't need to test for OnStream, as this has been done in detect () */
  5175. tpnt->os_fw_rev = osst_parse_firmware_rev (SDp->rev);
  5176. tpnt->omit_blklims = 1;
  5177. tpnt->poll = (strncmp(SDp->model, "DI-", 3) == 0) ||
  5178. (strncmp(SDp->model, "FW-", 3) == 0) || OSST_FW_NEED_POLL(tpnt->os_fw_rev,SDp);
  5179. tpnt->frame_in_buffer = 0;
  5180. tpnt->header_ok = 0;
  5181. tpnt->linux_media = 0;
  5182. tpnt->header_cache = NULL;
  5183. for (i=0; i < ST_NBR_MODES; i++) {
  5184. STm = &(tpnt->modes[i]);
  5185. STm->defined = 0;
  5186. STm->sysv = OSST_SYSV;
  5187. STm->defaults_for_writes = 0;
  5188. STm->do_async_writes = OSST_ASYNC_WRITES;
  5189. STm->do_buffer_writes = OSST_BUFFER_WRITES;
  5190. STm->do_read_ahead = OSST_READ_AHEAD;
  5191. STm->default_compression = ST_DONT_TOUCH;
  5192. STm->default_blksize = 512;
  5193. STm->default_density = (-1); /* No forced density */
  5194. }
  5195. for (i=0; i < ST_NBR_PARTITIONS; i++) {
  5196. STps = &(tpnt->ps[i]);
  5197. STps->rw = ST_IDLE;
  5198. STps->eof = ST_NOEOF;
  5199. STps->at_sm = 0;
  5200. STps->last_block_valid = 0;
  5201. STps->drv_block = (-1);
  5202. STps->drv_file = (-1);
  5203. }
  5204. tpnt->current_mode = 0;
  5205. tpnt->modes[0].defined = 1;
  5206. tpnt->modes[2].defined = 1;
  5207. tpnt->density_changed = tpnt->compression_changed = tpnt->blksize_changed = 0;
  5208. init_MUTEX(&tpnt->lock);
  5209. osst_nr_dev++;
  5210. write_unlock(&os_scsi_tapes_lock);
  5211. {
  5212. char name[8];
  5213. /* Rewind entry */
  5214. osst_sysfs_add(MKDEV(OSST_MAJOR, dev_num), dev, tpnt, tape_name(tpnt));
  5215. /* No-rewind entry */
  5216. snprintf(name, 8, "%s%s", "n", tape_name(tpnt));
  5217. osst_sysfs_add(MKDEV(OSST_MAJOR, dev_num + 128), dev, tpnt, name);
  5218. }
  5219. for (mode = 0; mode < ST_NBR_MODES; ++mode) {
  5220. /* Rewind entry */
  5221. devfs_mk_cdev(MKDEV(OSST_MAJOR, dev_num + (mode << 5)),
  5222. S_IFCHR | S_IRUGO | S_IWUGO,
  5223. "%s/ot%s", SDp->devfs_name, osst_formats[mode]);
  5224. /* No-rewind entry */
  5225. devfs_mk_cdev(MKDEV(OSST_MAJOR, dev_num + (mode << 5) + 128),
  5226. S_IFCHR | S_IRUGO | S_IWUGO,
  5227. "%s/ot%sn", SDp->devfs_name, osst_formats[mode]);
  5228. }
  5229. drive->number = devfs_register_tape(SDp->devfs_name);
  5230. printk(KERN_INFO
  5231. "osst :I: Attached OnStream %.5s tape at scsi%d, channel %d, id %d, lun %d as %s\n",
  5232. SDp->model, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun, tape_name(tpnt));
  5233. return 0;
  5234. out_put_disk:
  5235. put_disk(drive);
  5236. return -ENODEV;
  5237. };
  5238. static int osst_remove(struct device *dev)
  5239. {
  5240. struct scsi_device * SDp = to_scsi_device(dev);
  5241. struct osst_tape * tpnt;
  5242. int i, mode;
  5243. if ((SDp->type != TYPE_TAPE) || (osst_nr_dev <= 0))
  5244. return 0;
  5245. write_lock(&os_scsi_tapes_lock);
  5246. for(i=0; i < osst_max_dev; i++) {
  5247. if((tpnt = os_scsi_tapes[i]) && (tpnt->device == SDp)) {
  5248. osst_sysfs_destroy(MKDEV(OSST_MAJOR, i));
  5249. osst_sysfs_destroy(MKDEV(OSST_MAJOR, i+128));
  5250. tpnt->device = NULL;
  5251. for (mode = 0; mode < ST_NBR_MODES; ++mode) {
  5252. devfs_remove("%s/ot%s", SDp->devfs_name, osst_formats[mode]);
  5253. devfs_remove("%s/ot%sn", SDp->devfs_name, osst_formats[mode]);
  5254. }
  5255. devfs_unregister_tape(tpnt->drive->number);
  5256. put_disk(tpnt->drive);
  5257. os_scsi_tapes[i] = NULL;
  5258. osst_nr_dev--;
  5259. write_unlock(&os_scsi_tapes_lock);
  5260. vfree(tpnt->header_cache);
  5261. if (tpnt->buffer) {
  5262. normalize_buffer(tpnt->buffer);
  5263. kfree(tpnt->buffer);
  5264. }
  5265. kfree(tpnt);
  5266. return 0;
  5267. }
  5268. }
  5269. write_unlock(&os_scsi_tapes_lock);
  5270. return 0;
  5271. }
  5272. static int __init init_osst(void)
  5273. {
  5274. printk(KERN_INFO "osst :I: Tape driver with OnStream support version %s\nosst :I: %s\n", osst_version, cvsid);
  5275. validate_options();
  5276. osst_sysfs_init();
  5277. if ((register_chrdev(OSST_MAJOR,"osst", &osst_fops) < 0) || scsi_register_driver(&osst_template.gendrv)) {
  5278. printk(KERN_ERR "osst :E: Unable to register major %d for OnStream tapes\n", OSST_MAJOR);
  5279. osst_sysfs_cleanup();
  5280. return 1;
  5281. }
  5282. osst_create_driverfs_files(&osst_template.gendrv);
  5283. return 0;
  5284. }
  5285. static void __exit exit_osst (void)
  5286. {
  5287. int i;
  5288. struct osst_tape * STp;
  5289. osst_remove_driverfs_files(&osst_template.gendrv);
  5290. scsi_unregister_driver(&osst_template.gendrv);
  5291. unregister_chrdev(OSST_MAJOR, "osst");
  5292. osst_sysfs_cleanup();
  5293. if (os_scsi_tapes) {
  5294. for (i=0; i < osst_max_dev; ++i) {
  5295. if (!(STp = os_scsi_tapes[i])) continue;
  5296. /* This is defensive, supposed to happen during detach */
  5297. vfree(STp->header_cache);
  5298. if (STp->buffer) {
  5299. normalize_buffer(STp->buffer);
  5300. kfree(STp->buffer);
  5301. }
  5302. put_disk(STp->drive);
  5303. kfree(STp);
  5304. }
  5305. kfree(os_scsi_tapes);
  5306. }
  5307. printk(KERN_INFO "osst :I: Unloaded.\n");
  5308. }
  5309. module_init(init_osst);
  5310. module_exit(exit_osst);