osst.c 183 KB

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