osst.c 183 KB

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