osst.c 181 KB

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