osst.c 181 KB

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