osst.c 180 KB

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