osst.c 183 KB

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