osst.c 181 KB

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