osst.c 181 KB

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