st.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
  4. file Documentation/scsi/st.rst for more information.
  5. History:
  6. Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
  7. Contribution and ideas from several people including (in alphabetical
  8. order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
  9. Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
  10. Michael Schaefer, J"org Weule, and Eric Youngdale.
  11. Copyright 1992 - 2016 Kai Makisara
  12. email Kai.Makisara@kolumbus.fi
  13. Some small formal changes - aeb, 950809
  14. Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
  15. */
  16. static const char *verstr = "20160209";
  17. #include <linux/module.h>
  18. #include <linux/compat.h>
  19. #include <linux/fs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include <linux/errno.h>
  27. #include <linux/mtio.h>
  28. #include <linux/cdrom.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/cdev.h>
  35. #include <linux/idr.h>
  36. #include <linux/delay.h>
  37. #include <linux/mutex.h>
  38. #include <linux/uaccess.h>
  39. #include <asm/dma.h>
  40. #include <asm/unaligned.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_dbg.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_driver.h>
  45. #include <scsi/scsi_eh.h>
  46. #include <scsi/scsi_host.h>
  47. #include <scsi/scsi_ioctl.h>
  48. #include <scsi/sg.h>
  49. /* The driver prints some debugging information on the console if DEBUG
  50. is defined and non-zero. */
  51. #define DEBUG 1
  52. #define NO_DEBUG 0
  53. #define ST_DEB_MSG KERN_NOTICE
  54. #if DEBUG
  55. /* The message level for the debug messages is currently set to KERN_NOTICE
  56. so that people can easily see the messages. Later when the debugging messages
  57. in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
  58. #define DEB(a) a
  59. #define DEBC(a) if (debugging) { a ; }
  60. #else
  61. #define DEB(a)
  62. #define DEBC(a)
  63. #endif
  64. #define ST_KILOBYTE 1024
  65. #include "st_options.h"
  66. #include "st.h"
  67. static int buffer_kbs;
  68. static int max_sg_segs;
  69. static int try_direct_io = TRY_DIRECT_IO;
  70. static int try_rdio = 1;
  71. static int try_wdio = 1;
  72. static int debug_flag;
  73. static struct class st_sysfs_class;
  74. static const struct attribute_group *st_dev_groups[];
  75. static const struct attribute_group *st_drv_groups[];
  76. MODULE_AUTHOR("Kai Makisara");
  77. MODULE_DESCRIPTION("SCSI tape (st) driver");
  78. MODULE_LICENSE("GPL");
  79. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
  80. MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
  81. /* Set 'perm' (4th argument) to 0 to disable module_param's definition
  82. * of sysfs parameters (which module_param doesn't yet support).
  83. * Sysfs parameters defined explicitly later.
  84. */
  85. module_param_named(buffer_kbs, buffer_kbs, int, 0);
  86. MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
  87. module_param_named(max_sg_segs, max_sg_segs, int, 0);
  88. MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
  89. module_param_named(try_direct_io, try_direct_io, int, 0);
  90. MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
  91. module_param_named(debug_flag, debug_flag, int, 0);
  92. MODULE_PARM_DESC(debug_flag, "Enable DEBUG, same as setting debugging=1");
  93. /* Extra parameters for testing */
  94. module_param_named(try_rdio, try_rdio, int, 0);
  95. MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
  96. module_param_named(try_wdio, try_wdio, int, 0);
  97. MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
  98. #ifndef MODULE
  99. static int write_threshold_kbs; /* retained for compatibility */
  100. static struct st_dev_parm {
  101. char *name;
  102. int *val;
  103. } parms[] __initdata = {
  104. {
  105. "buffer_kbs", &buffer_kbs
  106. },
  107. { /* Retained for compatibility with 2.4 */
  108. "write_threshold_kbs", &write_threshold_kbs
  109. },
  110. {
  111. "max_sg_segs", NULL
  112. },
  113. {
  114. "try_direct_io", &try_direct_io
  115. },
  116. {
  117. "debug_flag", &debug_flag
  118. }
  119. };
  120. #endif
  121. /* Restrict the number of modes so that names for all are assigned */
  122. #if ST_NBR_MODES > 16
  123. #error "Maximum number of modes is 16"
  124. #endif
  125. /* Bit reversed order to get same names for same minors with all
  126. mode counts */
  127. static const char *st_formats[] = {
  128. "", "r", "k", "s", "l", "t", "o", "u",
  129. "m", "v", "p", "x", "a", "y", "q", "z"};
  130. /* The default definitions have been moved to st_options.h */
  131. #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
  132. /* The buffer size should fit into the 24 bits for length in the
  133. 6-byte SCSI read and write commands. */
  134. #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
  135. #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
  136. #endif
  137. static int debugging = DEBUG;
  138. #define MAX_RETRIES 0
  139. #define MAX_WRITE_RETRIES 0
  140. #define MAX_READY_RETRIES 0
  141. #define NO_TAPE NOT_READY
  142. #define ST_TIMEOUT (900 * HZ)
  143. #define ST_LONG_TIMEOUT (14000 * HZ)
  144. /* Remove mode bits and auto-rewind bit (7) */
  145. #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
  146. (iminor(x) & ((1 << ST_MODE_SHIFT)-1)))
  147. #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
  148. /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
  149. #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
  150. (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
  151. /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
  152. 24 bits) */
  153. #define SET_DENS_AND_BLK 0x10001
  154. static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
  155. static int st_max_sg_segs = ST_MAX_SG;
  156. static int modes_defined;
  157. static int enlarge_buffer(struct st_buffer *, int, int);
  158. static void clear_buffer(struct st_buffer *);
  159. static void normalize_buffer(struct st_buffer *);
  160. static int append_to_buffer(const char __user *, struct st_buffer *, int);
  161. static int from_buffer(struct st_buffer *, char __user *, int);
  162. static void move_buffer_data(struct st_buffer *, int);
  163. static int sgl_map_user_pages(struct st_buffer *, const unsigned int,
  164. unsigned long, size_t, int);
  165. static int sgl_unmap_user_pages(struct st_buffer *, const unsigned int, int);
  166. static int st_probe(struct device *);
  167. static int st_remove(struct device *);
  168. static struct scsi_driver st_template = {
  169. .gendrv = {
  170. .name = "st",
  171. .owner = THIS_MODULE,
  172. .probe = st_probe,
  173. .remove = st_remove,
  174. .groups = st_drv_groups,
  175. },
  176. };
  177. static int st_compression(struct scsi_tape *, int);
  178. static int find_partition(struct scsi_tape *);
  179. static int switch_partition(struct scsi_tape *);
  180. static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
  181. static void scsi_tape_release(struct kref *);
  182. #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
  183. static DEFINE_MUTEX(st_ref_mutex);
  184. static DEFINE_SPINLOCK(st_index_lock);
  185. static DEFINE_SPINLOCK(st_use_lock);
  186. static DEFINE_IDR(st_index_idr);
  187. #ifndef SIGS_FROM_OSST
  188. #define SIGS_FROM_OSST \
  189. {"OnStream", "SC-", "", "osst"}, \
  190. {"OnStream", "DI-", "", "osst"}, \
  191. {"OnStream", "DP-", "", "osst"}, \
  192. {"OnStream", "USB", "", "osst"}, \
  193. {"OnStream", "FW-", "", "osst"}
  194. #endif
  195. static struct scsi_tape *scsi_tape_get(int dev)
  196. {
  197. struct scsi_tape *STp = NULL;
  198. mutex_lock(&st_ref_mutex);
  199. spin_lock(&st_index_lock);
  200. STp = idr_find(&st_index_idr, dev);
  201. if (!STp) goto out;
  202. kref_get(&STp->kref);
  203. if (!STp->device)
  204. goto out_put;
  205. if (scsi_device_get(STp->device))
  206. goto out_put;
  207. goto out;
  208. out_put:
  209. kref_put(&STp->kref, scsi_tape_release);
  210. STp = NULL;
  211. out:
  212. spin_unlock(&st_index_lock);
  213. mutex_unlock(&st_ref_mutex);
  214. return STp;
  215. }
  216. static void scsi_tape_put(struct scsi_tape *STp)
  217. {
  218. struct scsi_device *sdev = STp->device;
  219. mutex_lock(&st_ref_mutex);
  220. kref_put(&STp->kref, scsi_tape_release);
  221. scsi_device_put(sdev);
  222. mutex_unlock(&st_ref_mutex);
  223. }
  224. struct st_reject_data {
  225. char *vendor;
  226. char *model;
  227. char *rev;
  228. char *driver_hint; /* Name of the correct driver, NULL if unknown */
  229. };
  230. static struct st_reject_data reject_list[] = {
  231. /* {"XXX", "Yy-", "", NULL}, example */
  232. SIGS_FROM_OSST,
  233. {NULL, }};
  234. /* If the device signature is on the list of incompatible drives, the
  235. function returns a pointer to the name of the correct driver (if known) */
  236. static char * st_incompatible(struct scsi_device* SDp)
  237. {
  238. struct st_reject_data *rp;
  239. for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
  240. if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
  241. !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
  242. !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
  243. if (rp->driver_hint)
  244. return rp->driver_hint;
  245. else
  246. return "unknown";
  247. }
  248. return NULL;
  249. }
  250. static inline char *tape_name(struct scsi_tape *tape)
  251. {
  252. return tape->disk->disk_name;
  253. }
  254. #define st_printk(prefix, t, fmt, a...) \
  255. sdev_prefix_printk(prefix, (t)->device, tape_name(t), fmt, ##a)
  256. #ifdef DEBUG
  257. #define DEBC_printk(t, fmt, a...) \
  258. if (debugging) { st_printk(ST_DEB_MSG, t, fmt, ##a ); }
  259. #else
  260. #define DEBC_printk(t, fmt, a...)
  261. #endif
  262. static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
  263. {
  264. const u8 *ucp;
  265. const u8 *sense = SRpnt->sense;
  266. s->have_sense = scsi_normalize_sense(SRpnt->sense,
  267. SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
  268. s->flags = 0;
  269. if (s->have_sense) {
  270. s->deferred = 0;
  271. s->remainder_valid =
  272. scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
  273. switch (sense[0] & 0x7f) {
  274. case 0x71:
  275. s->deferred = 1;
  276. fallthrough;
  277. case 0x70:
  278. s->fixed_format = 1;
  279. s->flags = sense[2] & 0xe0;
  280. break;
  281. case 0x73:
  282. s->deferred = 1;
  283. fallthrough;
  284. case 0x72:
  285. s->fixed_format = 0;
  286. ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
  287. s->flags = ucp ? (ucp[3] & 0xe0) : 0;
  288. break;
  289. }
  290. }
  291. }
  292. /* Convert the result to success code */
  293. static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
  294. {
  295. int result = SRpnt->result;
  296. u8 scode;
  297. DEB(const char *stp;)
  298. char *name = tape_name(STp);
  299. struct st_cmdstatus *cmdstatp;
  300. if (!result)
  301. return 0;
  302. cmdstatp = &STp->buffer->cmdstat;
  303. st_analyze_sense(SRpnt, cmdstatp);
  304. if (cmdstatp->have_sense)
  305. scode = STp->buffer->cmdstat.sense_hdr.sense_key;
  306. else
  307. scode = 0;
  308. DEB(
  309. if (debugging) {
  310. st_printk(ST_DEB_MSG, STp,
  311. "Error: %x, cmd: %x %x %x %x %x %x\n", result,
  312. SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
  313. SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
  314. if (cmdstatp->have_sense)
  315. __scsi_print_sense(STp->device, name,
  316. SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
  317. } ) /* end DEB */
  318. if (!debugging) { /* Abnormal conditions for tape */
  319. if (!cmdstatp->have_sense)
  320. st_printk(KERN_WARNING, STp,
  321. "Error %x (driver bt 0x%x, host bt 0x%x).\n",
  322. result, driver_byte(result), host_byte(result));
  323. else if (cmdstatp->have_sense &&
  324. scode != NO_SENSE &&
  325. scode != RECOVERED_ERROR &&
  326. /* scode != UNIT_ATTENTION && */
  327. scode != BLANK_CHECK &&
  328. scode != VOLUME_OVERFLOW &&
  329. SRpnt->cmd[0] != MODE_SENSE &&
  330. SRpnt->cmd[0] != TEST_UNIT_READY) {
  331. __scsi_print_sense(STp->device, name,
  332. SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
  333. }
  334. }
  335. if (cmdstatp->fixed_format &&
  336. STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
  337. if (STp->cln_sense_value)
  338. STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
  339. STp->cln_sense_mask) == STp->cln_sense_value);
  340. else
  341. STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
  342. STp->cln_sense_mask) != 0);
  343. }
  344. if (cmdstatp->have_sense &&
  345. cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
  346. STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
  347. STp->pos_unknown |= STp->device->was_reset;
  348. if (cmdstatp->have_sense &&
  349. scode == RECOVERED_ERROR
  350. #if ST_RECOVERED_WRITE_FATAL
  351. && SRpnt->cmd[0] != WRITE_6
  352. && SRpnt->cmd[0] != WRITE_FILEMARKS
  353. #endif
  354. ) {
  355. STp->recover_count++;
  356. STp->recover_reg++;
  357. DEB(
  358. if (debugging) {
  359. if (SRpnt->cmd[0] == READ_6)
  360. stp = "read";
  361. else if (SRpnt->cmd[0] == WRITE_6)
  362. stp = "write";
  363. else
  364. stp = "ioctl";
  365. st_printk(ST_DEB_MSG, STp,
  366. "Recovered %s error (%d).\n",
  367. stp, STp->recover_count);
  368. } ) /* end DEB */
  369. if (cmdstatp->flags == 0)
  370. return 0;
  371. }
  372. return (-EIO);
  373. }
  374. static struct st_request *st_allocate_request(struct scsi_tape *stp)
  375. {
  376. struct st_request *streq;
  377. streq = kzalloc(sizeof(*streq), GFP_KERNEL);
  378. if (streq)
  379. streq->stp = stp;
  380. else {
  381. st_printk(KERN_ERR, stp,
  382. "Can't get SCSI request.\n");
  383. if (signal_pending(current))
  384. stp->buffer->syscall_result = -EINTR;
  385. else
  386. stp->buffer->syscall_result = -EBUSY;
  387. }
  388. return streq;
  389. }
  390. static void st_release_request(struct st_request *streq)
  391. {
  392. kfree(streq);
  393. }
  394. static void st_do_stats(struct scsi_tape *STp, struct request *req)
  395. {
  396. ktime_t now;
  397. now = ktime_get();
  398. if (scsi_req(req)->cmd[0] == WRITE_6) {
  399. now = ktime_sub(now, STp->stats->write_time);
  400. atomic64_add(ktime_to_ns(now), &STp->stats->tot_write_time);
  401. atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
  402. atomic64_inc(&STp->stats->write_cnt);
  403. if (scsi_req(req)->result) {
  404. atomic64_add(atomic_read(&STp->stats->last_write_size)
  405. - STp->buffer->cmdstat.residual,
  406. &STp->stats->write_byte_cnt);
  407. if (STp->buffer->cmdstat.residual > 0)
  408. atomic64_inc(&STp->stats->resid_cnt);
  409. } else
  410. atomic64_add(atomic_read(&STp->stats->last_write_size),
  411. &STp->stats->write_byte_cnt);
  412. } else if (scsi_req(req)->cmd[0] == READ_6) {
  413. now = ktime_sub(now, STp->stats->read_time);
  414. atomic64_add(ktime_to_ns(now), &STp->stats->tot_read_time);
  415. atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
  416. atomic64_inc(&STp->stats->read_cnt);
  417. if (scsi_req(req)->result) {
  418. atomic64_add(atomic_read(&STp->stats->last_read_size)
  419. - STp->buffer->cmdstat.residual,
  420. &STp->stats->read_byte_cnt);
  421. if (STp->buffer->cmdstat.residual > 0)
  422. atomic64_inc(&STp->stats->resid_cnt);
  423. } else
  424. atomic64_add(atomic_read(&STp->stats->last_read_size),
  425. &STp->stats->read_byte_cnt);
  426. } else {
  427. now = ktime_sub(now, STp->stats->other_time);
  428. atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
  429. atomic64_inc(&STp->stats->other_cnt);
  430. }
  431. atomic64_dec(&STp->stats->in_flight);
  432. }
  433. static void st_scsi_execute_end(struct request *req, blk_status_t status)
  434. {
  435. struct st_request *SRpnt = req->end_io_data;
  436. struct scsi_request *rq = scsi_req(req);
  437. struct scsi_tape *STp = SRpnt->stp;
  438. struct bio *tmp;
  439. STp->buffer->cmdstat.midlevel_result = SRpnt->result = rq->result;
  440. STp->buffer->cmdstat.residual = rq->resid_len;
  441. st_do_stats(STp, req);
  442. tmp = SRpnt->bio;
  443. if (rq->sense_len)
  444. memcpy(SRpnt->sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
  445. if (SRpnt->waiting)
  446. complete(SRpnt->waiting);
  447. blk_rq_unmap_user(tmp);
  448. blk_put_request(req);
  449. }
  450. static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd,
  451. int data_direction, void *buffer, unsigned bufflen,
  452. int timeout, int retries)
  453. {
  454. struct request *req;
  455. struct scsi_request *rq;
  456. struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
  457. int err = 0;
  458. struct scsi_tape *STp = SRpnt->stp;
  459. req = blk_get_request(SRpnt->stp->device->request_queue,
  460. data_direction == DMA_TO_DEVICE ?
  461. REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
  462. if (IS_ERR(req))
  463. return DRIVER_ERROR << 24;
  464. rq = scsi_req(req);
  465. req->rq_flags |= RQF_QUIET;
  466. mdata->null_mapped = 1;
  467. if (bufflen) {
  468. err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen,
  469. GFP_KERNEL);
  470. if (err) {
  471. blk_put_request(req);
  472. return DRIVER_ERROR << 24;
  473. }
  474. }
  475. atomic64_inc(&STp->stats->in_flight);
  476. if (cmd[0] == WRITE_6) {
  477. atomic_set(&STp->stats->last_write_size, bufflen);
  478. STp->stats->write_time = ktime_get();
  479. } else if (cmd[0] == READ_6) {
  480. atomic_set(&STp->stats->last_read_size, bufflen);
  481. STp->stats->read_time = ktime_get();
  482. } else {
  483. STp->stats->other_time = ktime_get();
  484. }
  485. SRpnt->bio = req->bio;
  486. rq->cmd_len = COMMAND_SIZE(cmd[0]);
  487. memset(rq->cmd, 0, BLK_MAX_CDB);
  488. memcpy(rq->cmd, cmd, rq->cmd_len);
  489. req->timeout = timeout;
  490. rq->retries = retries;
  491. req->end_io_data = SRpnt;
  492. blk_execute_rq_nowait(req->q, NULL, req, 1, st_scsi_execute_end);
  493. return 0;
  494. }
  495. /* Do the scsi command. Waits until command performed if do_wait is true.
  496. Otherwise write_behind_check() is used to check that the command
  497. has finished. */
  498. static struct st_request *
  499. st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
  500. int bytes, int direction, int timeout, int retries, int do_wait)
  501. {
  502. struct completion *waiting;
  503. struct rq_map_data *mdata = &STp->buffer->map_data;
  504. int ret;
  505. /* if async, make sure there's no command outstanding */
  506. if (!do_wait && ((STp->buffer)->last_SRpnt)) {
  507. st_printk(KERN_ERR, STp,
  508. "Async command already active.\n");
  509. if (signal_pending(current))
  510. (STp->buffer)->syscall_result = (-EINTR);
  511. else
  512. (STp->buffer)->syscall_result = (-EBUSY);
  513. return NULL;
  514. }
  515. if (!SRpnt) {
  516. SRpnt = st_allocate_request(STp);
  517. if (!SRpnt)
  518. return NULL;
  519. }
  520. /* If async IO, set last_SRpnt. This ptr tells write_behind_check
  521. which IO is outstanding. It's nulled out when the IO completes. */
  522. if (!do_wait)
  523. (STp->buffer)->last_SRpnt = SRpnt;
  524. waiting = &STp->wait;
  525. init_completion(waiting);
  526. SRpnt->waiting = waiting;
  527. if (STp->buffer->do_dio) {
  528. mdata->page_order = 0;
  529. mdata->nr_entries = STp->buffer->sg_segs;
  530. mdata->pages = STp->buffer->mapped_pages;
  531. } else {
  532. mdata->page_order = STp->buffer->reserved_page_order;
  533. mdata->nr_entries =
  534. DIV_ROUND_UP(bytes, PAGE_SIZE << mdata->page_order);
  535. mdata->pages = STp->buffer->reserved_pages;
  536. mdata->offset = 0;
  537. }
  538. memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
  539. STp->buffer->cmdstat.have_sense = 0;
  540. STp->buffer->syscall_result = 0;
  541. ret = st_scsi_execute(SRpnt, cmd, direction, NULL, bytes, timeout,
  542. retries);
  543. if (ret) {
  544. /* could not allocate the buffer or request was too large */
  545. (STp->buffer)->syscall_result = (-EBUSY);
  546. (STp->buffer)->last_SRpnt = NULL;
  547. } else if (do_wait) {
  548. wait_for_completion(waiting);
  549. SRpnt->waiting = NULL;
  550. (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
  551. }
  552. return SRpnt;
  553. }
  554. /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
  555. write has been correct but EOM early warning reached, -EIO if write ended in
  556. error or zero if write successful. Asynchronous writes are used only in
  557. variable block mode. */
  558. static int write_behind_check(struct scsi_tape * STp)
  559. {
  560. int retval = 0;
  561. struct st_buffer *STbuffer;
  562. struct st_partstat *STps;
  563. struct st_cmdstatus *cmdstatp;
  564. struct st_request *SRpnt;
  565. STbuffer = STp->buffer;
  566. if (!STbuffer->writing)
  567. return 0;
  568. DEB(
  569. if (STp->write_pending)
  570. STp->nbr_waits++;
  571. else
  572. STp->nbr_finished++;
  573. ) /* end DEB */
  574. wait_for_completion(&(STp->wait));
  575. SRpnt = STbuffer->last_SRpnt;
  576. STbuffer->last_SRpnt = NULL;
  577. SRpnt->waiting = NULL;
  578. (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
  579. st_release_request(SRpnt);
  580. STbuffer->buffer_bytes -= STbuffer->writing;
  581. STps = &(STp->ps[STp->partition]);
  582. if (STps->drv_block >= 0) {
  583. if (STp->block_size == 0)
  584. STps->drv_block++;
  585. else
  586. STps->drv_block += STbuffer->writing / STp->block_size;
  587. }
  588. cmdstatp = &STbuffer->cmdstat;
  589. if (STbuffer->syscall_result) {
  590. retval = -EIO;
  591. if (cmdstatp->have_sense && !cmdstatp->deferred &&
  592. (cmdstatp->flags & SENSE_EOM) &&
  593. (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  594. cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
  595. /* EOM at write-behind, has all data been written? */
  596. if (!cmdstatp->remainder_valid ||
  597. cmdstatp->uremainder64 == 0)
  598. retval = -ENOSPC;
  599. }
  600. if (retval == -EIO)
  601. STps->drv_block = -1;
  602. }
  603. STbuffer->writing = 0;
  604. DEB(if (debugging && retval)
  605. st_printk(ST_DEB_MSG, STp,
  606. "Async write error %x, return value %d.\n",
  607. STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
  608. return retval;
  609. }
  610. /* Step over EOF if it has been inadvertently crossed (ioctl not used because
  611. it messes up the block number). */
  612. static int cross_eof(struct scsi_tape * STp, int forward)
  613. {
  614. struct st_request *SRpnt;
  615. unsigned char cmd[MAX_COMMAND_SIZE];
  616. cmd[0] = SPACE;
  617. cmd[1] = 0x01; /* Space FileMarks */
  618. if (forward) {
  619. cmd[2] = cmd[3] = 0;
  620. cmd[4] = 1;
  621. } else
  622. cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
  623. cmd[5] = 0;
  624. DEBC_printk(STp, "Stepping over filemark %s.\n",
  625. forward ? "forward" : "backward");
  626. SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  627. STp->device->request_queue->rq_timeout,
  628. MAX_RETRIES, 1);
  629. if (!SRpnt)
  630. return (STp->buffer)->syscall_result;
  631. st_release_request(SRpnt);
  632. SRpnt = NULL;
  633. if ((STp->buffer)->cmdstat.midlevel_result != 0)
  634. st_printk(KERN_ERR, STp,
  635. "Stepping over filemark %s failed.\n",
  636. forward ? "forward" : "backward");
  637. return (STp->buffer)->syscall_result;
  638. }
  639. /* Flush the write buffer (never need to write if variable blocksize). */
  640. static int st_flush_write_buffer(struct scsi_tape * STp)
  641. {
  642. int transfer, blks;
  643. int result;
  644. unsigned char cmd[MAX_COMMAND_SIZE];
  645. struct st_request *SRpnt;
  646. struct st_partstat *STps;
  647. result = write_behind_check(STp);
  648. if (result)
  649. return result;
  650. result = 0;
  651. if (STp->dirty == 1) {
  652. transfer = STp->buffer->buffer_bytes;
  653. DEBC_printk(STp, "Flushing %d bytes.\n", transfer);
  654. memset(cmd, 0, MAX_COMMAND_SIZE);
  655. cmd[0] = WRITE_6;
  656. cmd[1] = 1;
  657. blks = transfer / STp->block_size;
  658. cmd[2] = blks >> 16;
  659. cmd[3] = blks >> 8;
  660. cmd[4] = blks;
  661. SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
  662. STp->device->request_queue->rq_timeout,
  663. MAX_WRITE_RETRIES, 1);
  664. if (!SRpnt)
  665. return (STp->buffer)->syscall_result;
  666. STps = &(STp->ps[STp->partition]);
  667. if ((STp->buffer)->syscall_result != 0) {
  668. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  669. if (cmdstatp->have_sense && !cmdstatp->deferred &&
  670. (cmdstatp->flags & SENSE_EOM) &&
  671. (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  672. cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
  673. (!cmdstatp->remainder_valid ||
  674. cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
  675. STp->dirty = 0;
  676. (STp->buffer)->buffer_bytes = 0;
  677. if (STps->drv_block >= 0)
  678. STps->drv_block += blks;
  679. result = (-ENOSPC);
  680. } else {
  681. st_printk(KERN_ERR, STp, "Error on flush.\n");
  682. STps->drv_block = (-1);
  683. result = (-EIO);
  684. }
  685. } else {
  686. if (STps->drv_block >= 0)
  687. STps->drv_block += blks;
  688. STp->dirty = 0;
  689. (STp->buffer)->buffer_bytes = 0;
  690. }
  691. st_release_request(SRpnt);
  692. SRpnt = NULL;
  693. }
  694. return result;
  695. }
  696. /* Flush the tape buffer. The tape will be positioned correctly unless
  697. seek_next is true. */
  698. static int flush_buffer(struct scsi_tape *STp, int seek_next)
  699. {
  700. int backspace, result;
  701. struct st_partstat *STps;
  702. /*
  703. * If there was a bus reset, block further access
  704. * to this device.
  705. */
  706. if (STp->pos_unknown)
  707. return (-EIO);
  708. if (STp->ready != ST_READY)
  709. return 0;
  710. STps = &(STp->ps[STp->partition]);
  711. if (STps->rw == ST_WRITING) /* Writing */
  712. return st_flush_write_buffer(STp);
  713. if (STp->block_size == 0)
  714. return 0;
  715. backspace = ((STp->buffer)->buffer_bytes +
  716. (STp->buffer)->read_pointer) / STp->block_size -
  717. ((STp->buffer)->read_pointer + STp->block_size - 1) /
  718. STp->block_size;
  719. (STp->buffer)->buffer_bytes = 0;
  720. (STp->buffer)->read_pointer = 0;
  721. result = 0;
  722. if (!seek_next) {
  723. if (STps->eof == ST_FM_HIT) {
  724. result = cross_eof(STp, 0); /* Back over the EOF hit */
  725. if (!result)
  726. STps->eof = ST_NOEOF;
  727. else {
  728. if (STps->drv_file >= 0)
  729. STps->drv_file++;
  730. STps->drv_block = 0;
  731. }
  732. }
  733. if (!result && backspace > 0)
  734. result = st_int_ioctl(STp, MTBSR, backspace);
  735. } else if (STps->eof == ST_FM_HIT) {
  736. if (STps->drv_file >= 0)
  737. STps->drv_file++;
  738. STps->drv_block = 0;
  739. STps->eof = ST_NOEOF;
  740. }
  741. return result;
  742. }
  743. /* Set the mode parameters */
  744. static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
  745. {
  746. int set_it = 0;
  747. unsigned long arg;
  748. if (!STp->density_changed &&
  749. STm->default_density >= 0 &&
  750. STm->default_density != STp->density) {
  751. arg = STm->default_density;
  752. set_it = 1;
  753. } else
  754. arg = STp->density;
  755. arg <<= MT_ST_DENSITY_SHIFT;
  756. if (!STp->blksize_changed &&
  757. STm->default_blksize >= 0 &&
  758. STm->default_blksize != STp->block_size) {
  759. arg |= STm->default_blksize;
  760. set_it = 1;
  761. } else
  762. arg |= STp->block_size;
  763. if (set_it &&
  764. st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
  765. st_printk(KERN_WARNING, STp,
  766. "Can't set default block size to %d bytes "
  767. "and density %x.\n",
  768. STm->default_blksize, STm->default_density);
  769. if (modes_defined)
  770. return (-EINVAL);
  771. }
  772. return 0;
  773. }
  774. /* Lock or unlock the drive door. Don't use when st_request allocated. */
  775. static int do_door_lock(struct scsi_tape * STp, int do_lock)
  776. {
  777. int retval;
  778. DEBC_printk(STp, "%socking drive door.\n", do_lock ? "L" : "Unl");
  779. retval = scsi_set_medium_removal(STp->device,
  780. do_lock ? SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
  781. if (!retval)
  782. STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
  783. else
  784. STp->door_locked = ST_LOCK_FAILS;
  785. return retval;
  786. }
  787. /* Set the internal state after reset */
  788. static void reset_state(struct scsi_tape *STp)
  789. {
  790. int i;
  791. struct st_partstat *STps;
  792. STp->pos_unknown = 0;
  793. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  794. STps = &(STp->ps[i]);
  795. STps->rw = ST_IDLE;
  796. STps->eof = ST_NOEOF;
  797. STps->at_sm = 0;
  798. STps->last_block_valid = 0;
  799. STps->drv_block = -1;
  800. STps->drv_file = -1;
  801. }
  802. if (STp->can_partitions) {
  803. STp->partition = find_partition(STp);
  804. if (STp->partition < 0)
  805. STp->partition = 0;
  806. STp->new_partition = STp->partition;
  807. }
  808. }
  809. /* Test if the drive is ready. Returns either one of the codes below or a negative system
  810. error code. */
  811. #define CHKRES_READY 0
  812. #define CHKRES_NEW_SESSION 1
  813. #define CHKRES_NOT_READY 2
  814. #define CHKRES_NO_TAPE 3
  815. #define MAX_ATTENTIONS 10
  816. static int test_ready(struct scsi_tape *STp, int do_wait)
  817. {
  818. int attentions, waits, max_wait, scode;
  819. int retval = CHKRES_READY, new_session = 0;
  820. unsigned char cmd[MAX_COMMAND_SIZE];
  821. struct st_request *SRpnt = NULL;
  822. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  823. max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
  824. for (attentions=waits=0; ; ) {
  825. memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  826. cmd[0] = TEST_UNIT_READY;
  827. SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  828. STp->long_timeout, MAX_READY_RETRIES, 1);
  829. if (!SRpnt) {
  830. retval = (STp->buffer)->syscall_result;
  831. break;
  832. }
  833. if (cmdstatp->have_sense) {
  834. scode = cmdstatp->sense_hdr.sense_key;
  835. if (scode == UNIT_ATTENTION) { /* New media? */
  836. new_session = 1;
  837. if (attentions < MAX_ATTENTIONS) {
  838. attentions++;
  839. continue;
  840. }
  841. else {
  842. retval = (-EIO);
  843. break;
  844. }
  845. }
  846. if (scode == NOT_READY) {
  847. if (waits < max_wait) {
  848. if (msleep_interruptible(1000)) {
  849. retval = (-EINTR);
  850. break;
  851. }
  852. waits++;
  853. continue;
  854. }
  855. else {
  856. if ((STp->device)->scsi_level >= SCSI_2 &&
  857. cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
  858. retval = CHKRES_NO_TAPE;
  859. else
  860. retval = CHKRES_NOT_READY;
  861. break;
  862. }
  863. }
  864. }
  865. retval = (STp->buffer)->syscall_result;
  866. if (!retval)
  867. retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
  868. break;
  869. }
  870. if (SRpnt != NULL)
  871. st_release_request(SRpnt);
  872. return retval;
  873. }
  874. /* See if the drive is ready and gather information about the tape. Return values:
  875. < 0 negative error code from errno.h
  876. 0 drive ready
  877. 1 drive not ready (possibly no tape)
  878. */
  879. static int check_tape(struct scsi_tape *STp, struct file *filp)
  880. {
  881. int i, retval, new_session = 0, do_wait;
  882. unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
  883. unsigned short st_flags = filp->f_flags;
  884. struct st_request *SRpnt = NULL;
  885. struct st_modedef *STm;
  886. struct st_partstat *STps;
  887. struct inode *inode = file_inode(filp);
  888. int mode = TAPE_MODE(inode);
  889. STp->ready = ST_READY;
  890. if (mode != STp->current_mode) {
  891. DEBC_printk(STp, "Mode change from %d to %d.\n",
  892. STp->current_mode, mode);
  893. new_session = 1;
  894. STp->current_mode = mode;
  895. }
  896. STm = &(STp->modes[STp->current_mode]);
  897. saved_cleaning = STp->cleaning_req;
  898. STp->cleaning_req = 0;
  899. do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
  900. retval = test_ready(STp, do_wait);
  901. if (retval < 0)
  902. goto err_out;
  903. if (retval == CHKRES_NEW_SESSION) {
  904. STp->pos_unknown = 0;
  905. STp->partition = STp->new_partition = 0;
  906. if (STp->can_partitions)
  907. STp->nbr_partitions = 1; /* This guess will be updated later
  908. if necessary */
  909. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  910. STps = &(STp->ps[i]);
  911. STps->rw = ST_IDLE;
  912. STps->eof = ST_NOEOF;
  913. STps->at_sm = 0;
  914. STps->last_block_valid = 0;
  915. STps->drv_block = 0;
  916. STps->drv_file = 0;
  917. }
  918. new_session = 1;
  919. }
  920. else {
  921. STp->cleaning_req |= saved_cleaning;
  922. if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
  923. if (retval == CHKRES_NO_TAPE)
  924. STp->ready = ST_NO_TAPE;
  925. else
  926. STp->ready = ST_NOT_READY;
  927. STp->density = 0; /* Clear the erroneous "residue" */
  928. STp->write_prot = 0;
  929. STp->block_size = 0;
  930. STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
  931. STp->partition = STp->new_partition = 0;
  932. STp->door_locked = ST_UNLOCKED;
  933. return CHKRES_NOT_READY;
  934. }
  935. }
  936. if (STp->omit_blklims)
  937. STp->min_block = STp->max_block = (-1);
  938. else {
  939. memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  940. cmd[0] = READ_BLOCK_LIMITS;
  941. SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
  942. STp->device->request_queue->rq_timeout,
  943. MAX_READY_RETRIES, 1);
  944. if (!SRpnt) {
  945. retval = (STp->buffer)->syscall_result;
  946. goto err_out;
  947. }
  948. if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
  949. STp->max_block = ((STp->buffer)->b_data[1] << 16) |
  950. ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
  951. STp->min_block = ((STp->buffer)->b_data[4] << 8) |
  952. (STp->buffer)->b_data[5];
  953. if ( DEB( debugging || ) !STp->inited)
  954. st_printk(KERN_INFO, STp,
  955. "Block limits %d - %d bytes.\n",
  956. STp->min_block, STp->max_block);
  957. } else {
  958. STp->min_block = STp->max_block = (-1);
  959. DEBC_printk(STp, "Can't read block limits.\n");
  960. }
  961. }
  962. memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  963. cmd[0] = MODE_SENSE;
  964. cmd[4] = 12;
  965. SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
  966. STp->device->request_queue->rq_timeout,
  967. MAX_READY_RETRIES, 1);
  968. if (!SRpnt) {
  969. retval = (STp->buffer)->syscall_result;
  970. goto err_out;
  971. }
  972. if ((STp->buffer)->syscall_result != 0) {
  973. DEBC_printk(STp, "No Mode Sense.\n");
  974. STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
  975. (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
  976. STp->drv_write_prot = 0;
  977. } else {
  978. DEBC_printk(STp,"Mode sense. Length %d, "
  979. "medium %x, WBS %x, BLL %d\n",
  980. (STp->buffer)->b_data[0],
  981. (STp->buffer)->b_data[1],
  982. (STp->buffer)->b_data[2],
  983. (STp->buffer)->b_data[3]);
  984. if ((STp->buffer)->b_data[3] >= 8) {
  985. STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
  986. STp->density = (STp->buffer)->b_data[4];
  987. STp->block_size = (STp->buffer)->b_data[9] * 65536 +
  988. (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
  989. DEBC_printk(STp, "Density %x, tape length: %x, "
  990. "drv buffer: %d\n",
  991. STp->density,
  992. (STp->buffer)->b_data[5] * 65536 +
  993. (STp->buffer)->b_data[6] * 256 +
  994. (STp->buffer)->b_data[7],
  995. STp->drv_buffer);
  996. }
  997. STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
  998. if (!STp->drv_buffer && STp->immediate_filemark) {
  999. st_printk(KERN_WARNING, STp,
  1000. "non-buffered tape: disabling "
  1001. "writing immediate filemarks\n");
  1002. STp->immediate_filemark = 0;
  1003. }
  1004. }
  1005. st_release_request(SRpnt);
  1006. SRpnt = NULL;
  1007. STp->inited = 1;
  1008. if (STp->block_size > 0)
  1009. (STp->buffer)->buffer_blocks =
  1010. (STp->buffer)->buffer_size / STp->block_size;
  1011. else
  1012. (STp->buffer)->buffer_blocks = 1;
  1013. (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
  1014. DEBC_printk(STp, "Block size: %d, buffer size: %d (%d blocks).\n",
  1015. STp->block_size, (STp->buffer)->buffer_size,
  1016. (STp->buffer)->buffer_blocks);
  1017. if (STp->drv_write_prot) {
  1018. STp->write_prot = 1;
  1019. DEBC_printk(STp, "Write protected\n");
  1020. if (do_wait &&
  1021. ((st_flags & O_ACCMODE) == O_WRONLY ||
  1022. (st_flags & O_ACCMODE) == O_RDWR)) {
  1023. retval = (-EROFS);
  1024. goto err_out;
  1025. }
  1026. }
  1027. if (STp->can_partitions && STp->nbr_partitions < 1) {
  1028. /* This code is reached when the device is opened for the first time
  1029. after the driver has been initialized with tape in the drive and the
  1030. partition support has been enabled. */
  1031. DEBC_printk(STp, "Updating partition number in status.\n");
  1032. if ((STp->partition = find_partition(STp)) < 0) {
  1033. retval = STp->partition;
  1034. goto err_out;
  1035. }
  1036. STp->new_partition = STp->partition;
  1037. STp->nbr_partitions = 1; /* This guess will be updated when necessary */
  1038. }
  1039. if (new_session) { /* Change the drive parameters for the new mode */
  1040. STp->density_changed = STp->blksize_changed = 0;
  1041. STp->compression_changed = 0;
  1042. if (!(STm->defaults_for_writes) &&
  1043. (retval = set_mode_densblk(STp, STm)) < 0)
  1044. goto err_out;
  1045. if (STp->default_drvbuffer != 0xff) {
  1046. if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
  1047. st_printk(KERN_WARNING, STp,
  1048. "Can't set default drive "
  1049. "buffering to %d.\n",
  1050. STp->default_drvbuffer);
  1051. }
  1052. }
  1053. return CHKRES_READY;
  1054. err_out:
  1055. return retval;
  1056. }
  1057. /* Open the device. Needs to take the BKL only because of incrementing the SCSI host
  1058. module count. */
  1059. static int st_open(struct inode *inode, struct file *filp)
  1060. {
  1061. int i, retval = (-EIO);
  1062. int resumed = 0;
  1063. struct scsi_tape *STp;
  1064. struct st_partstat *STps;
  1065. int dev = TAPE_NR(inode);
  1066. /*
  1067. * We really want to do nonseekable_open(inode, filp); here, but some
  1068. * versions of tar incorrectly call lseek on tapes and bail out if that
  1069. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1070. */
  1071. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1072. if (!(STp = scsi_tape_get(dev))) {
  1073. return -ENXIO;
  1074. }
  1075. filp->private_data = STp;
  1076. spin_lock(&st_use_lock);
  1077. if (STp->in_use) {
  1078. spin_unlock(&st_use_lock);
  1079. DEBC_printk(STp, "Device already in use.\n");
  1080. scsi_tape_put(STp);
  1081. return (-EBUSY);
  1082. }
  1083. STp->in_use = 1;
  1084. spin_unlock(&st_use_lock);
  1085. STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
  1086. if (scsi_autopm_get_device(STp->device) < 0) {
  1087. retval = -EIO;
  1088. goto err_out;
  1089. }
  1090. resumed = 1;
  1091. if (!scsi_block_when_processing_errors(STp->device)) {
  1092. retval = (-ENXIO);
  1093. goto err_out;
  1094. }
  1095. /* See that we have at least a one page buffer available */
  1096. if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
  1097. st_printk(KERN_WARNING, STp,
  1098. "Can't allocate one page tape buffer.\n");
  1099. retval = (-EOVERFLOW);
  1100. goto err_out;
  1101. }
  1102. (STp->buffer)->cleared = 0;
  1103. (STp->buffer)->writing = 0;
  1104. (STp->buffer)->syscall_result = 0;
  1105. STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
  1106. STp->dirty = 0;
  1107. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  1108. STps = &(STp->ps[i]);
  1109. STps->rw = ST_IDLE;
  1110. }
  1111. STp->try_dio_now = STp->try_dio;
  1112. STp->recover_count = 0;
  1113. DEB( STp->nbr_waits = STp->nbr_finished = 0;
  1114. STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
  1115. retval = check_tape(STp, filp);
  1116. if (retval < 0)
  1117. goto err_out;
  1118. if ((filp->f_flags & O_NONBLOCK) == 0 &&
  1119. retval != CHKRES_READY) {
  1120. if (STp->ready == NO_TAPE)
  1121. retval = (-ENOMEDIUM);
  1122. else
  1123. retval = (-EIO);
  1124. goto err_out;
  1125. }
  1126. return 0;
  1127. err_out:
  1128. normalize_buffer(STp->buffer);
  1129. spin_lock(&st_use_lock);
  1130. STp->in_use = 0;
  1131. spin_unlock(&st_use_lock);
  1132. if (resumed)
  1133. scsi_autopm_put_device(STp->device);
  1134. scsi_tape_put(STp);
  1135. return retval;
  1136. }
  1137. /* Flush the tape buffer before close */
  1138. static int st_flush(struct file *filp, fl_owner_t id)
  1139. {
  1140. int result = 0, result2;
  1141. unsigned char cmd[MAX_COMMAND_SIZE];
  1142. struct st_request *SRpnt;
  1143. struct scsi_tape *STp = filp->private_data;
  1144. struct st_modedef *STm = &(STp->modes[STp->current_mode]);
  1145. struct st_partstat *STps = &(STp->ps[STp->partition]);
  1146. if (file_count(filp) > 1)
  1147. return 0;
  1148. if (STps->rw == ST_WRITING && !STp->pos_unknown) {
  1149. result = st_flush_write_buffer(STp);
  1150. if (result != 0 && result != (-ENOSPC))
  1151. goto out;
  1152. }
  1153. if (STp->can_partitions &&
  1154. (result2 = switch_partition(STp)) < 0) {
  1155. DEBC_printk(STp, "switch_partition at close failed.\n");
  1156. if (result == 0)
  1157. result = result2;
  1158. goto out;
  1159. }
  1160. DEBC( if (STp->nbr_requests)
  1161. st_printk(KERN_DEBUG, STp,
  1162. "Number of r/w requests %d, dio used in %d, "
  1163. "pages %d.\n", STp->nbr_requests, STp->nbr_dio,
  1164. STp->nbr_pages));
  1165. if (STps->rw == ST_WRITING && !STp->pos_unknown) {
  1166. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  1167. #if DEBUG
  1168. DEBC_printk(STp, "Async write waits %d, finished %d.\n",
  1169. STp->nbr_waits, STp->nbr_finished);
  1170. #endif
  1171. memset(cmd, 0, MAX_COMMAND_SIZE);
  1172. cmd[0] = WRITE_FILEMARKS;
  1173. if (STp->immediate_filemark)
  1174. cmd[1] = 1;
  1175. cmd[4] = 1 + STp->two_fm;
  1176. SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  1177. STp->device->request_queue->rq_timeout,
  1178. MAX_WRITE_RETRIES, 1);
  1179. if (!SRpnt) {
  1180. result = (STp->buffer)->syscall_result;
  1181. goto out;
  1182. }
  1183. if (STp->buffer->syscall_result == 0 ||
  1184. (cmdstatp->have_sense && !cmdstatp->deferred &&
  1185. (cmdstatp->flags & SENSE_EOM) &&
  1186. (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  1187. cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
  1188. (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
  1189. /* Write successful at EOM */
  1190. st_release_request(SRpnt);
  1191. SRpnt = NULL;
  1192. if (STps->drv_file >= 0)
  1193. STps->drv_file++;
  1194. STps->drv_block = 0;
  1195. if (STp->two_fm)
  1196. cross_eof(STp, 0);
  1197. STps->eof = ST_FM;
  1198. }
  1199. else { /* Write error */
  1200. st_release_request(SRpnt);
  1201. SRpnt = NULL;
  1202. st_printk(KERN_ERR, STp,
  1203. "Error on write filemark.\n");
  1204. if (result == 0)
  1205. result = (-EIO);
  1206. }
  1207. DEBC_printk(STp, "Buffer flushed, %d EOF(s) written\n", cmd[4]);
  1208. } else if (!STp->rew_at_close) {
  1209. STps = &(STp->ps[STp->partition]);
  1210. if (!STm->sysv || STps->rw != ST_READING) {
  1211. if (STp->can_bsr)
  1212. result = flush_buffer(STp, 0);
  1213. else if (STps->eof == ST_FM_HIT) {
  1214. result = cross_eof(STp, 0);
  1215. if (result) {
  1216. if (STps->drv_file >= 0)
  1217. STps->drv_file++;
  1218. STps->drv_block = 0;
  1219. STps->eof = ST_FM;
  1220. } else
  1221. STps->eof = ST_NOEOF;
  1222. }
  1223. } else if ((STps->eof == ST_NOEOF &&
  1224. !(result = cross_eof(STp, 1))) ||
  1225. STps->eof == ST_FM_HIT) {
  1226. if (STps->drv_file >= 0)
  1227. STps->drv_file++;
  1228. STps->drv_block = 0;
  1229. STps->eof = ST_FM;
  1230. }
  1231. }
  1232. out:
  1233. if (STp->rew_at_close) {
  1234. result2 = st_int_ioctl(STp, MTREW, 1);
  1235. if (result == 0)
  1236. result = result2;
  1237. }
  1238. return result;
  1239. }
  1240. /* Close the device and release it. BKL is not needed: this is the only thread
  1241. accessing this tape. */
  1242. static int st_release(struct inode *inode, struct file *filp)
  1243. {
  1244. struct scsi_tape *STp = filp->private_data;
  1245. if (STp->door_locked == ST_LOCKED_AUTO)
  1246. do_door_lock(STp, 0);
  1247. normalize_buffer(STp->buffer);
  1248. spin_lock(&st_use_lock);
  1249. STp->in_use = 0;
  1250. spin_unlock(&st_use_lock);
  1251. scsi_autopm_put_device(STp->device);
  1252. scsi_tape_put(STp);
  1253. return 0;
  1254. }
  1255. /* The checks common to both reading and writing */
  1256. static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
  1257. {
  1258. ssize_t retval = 0;
  1259. /*
  1260. * If we are in the middle of error recovery, don't let anyone
  1261. * else try and use this device. Also, if error recovery fails, it
  1262. * may try and take the device offline, in which case all further
  1263. * access to the device is prohibited.
  1264. */
  1265. if (!scsi_block_when_processing_errors(STp->device)) {
  1266. retval = (-ENXIO);
  1267. goto out;
  1268. }
  1269. if (STp->ready != ST_READY) {
  1270. if (STp->ready == ST_NO_TAPE)
  1271. retval = (-ENOMEDIUM);
  1272. else
  1273. retval = (-EIO);
  1274. goto out;
  1275. }
  1276. if (! STp->modes[STp->current_mode].defined) {
  1277. retval = (-ENXIO);
  1278. goto out;
  1279. }
  1280. /*
  1281. * If there was a bus reset, block further access
  1282. * to this device.
  1283. */
  1284. if (STp->pos_unknown) {
  1285. retval = (-EIO);
  1286. goto out;
  1287. }
  1288. if (count == 0)
  1289. goto out;
  1290. DEB(
  1291. if (!STp->in_use) {
  1292. st_printk(ST_DEB_MSG, STp,
  1293. "Incorrect device.\n");
  1294. retval = (-EIO);
  1295. goto out;
  1296. } ) /* end DEB */
  1297. if (STp->can_partitions &&
  1298. (retval = switch_partition(STp)) < 0)
  1299. goto out;
  1300. if (STp->block_size == 0 && STp->max_block > 0 &&
  1301. (count < STp->min_block || count > STp->max_block)) {
  1302. retval = (-EINVAL);
  1303. goto out;
  1304. }
  1305. if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
  1306. !do_door_lock(STp, 1))
  1307. STp->door_locked = ST_LOCKED_AUTO;
  1308. out:
  1309. return retval;
  1310. }
  1311. static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
  1312. size_t count, int is_read)
  1313. {
  1314. int i, bufsize, retval = 0;
  1315. struct st_buffer *STbp = STp->buffer;
  1316. if (is_read)
  1317. i = STp->try_dio_now && try_rdio;
  1318. else
  1319. i = STp->try_dio_now && try_wdio;
  1320. if (i && ((unsigned long)buf & queue_dma_alignment(
  1321. STp->device->request_queue)) == 0) {
  1322. i = sgl_map_user_pages(STbp, STbp->use_sg, (unsigned long)buf,
  1323. count, (is_read ? READ : WRITE));
  1324. if (i > 0) {
  1325. STbp->do_dio = i;
  1326. STbp->buffer_bytes = 0; /* can be used as transfer counter */
  1327. }
  1328. else
  1329. STbp->do_dio = 0; /* fall back to buffering with any error */
  1330. STbp->sg_segs = STbp->do_dio;
  1331. DEB(
  1332. if (STbp->do_dio) {
  1333. STp->nbr_dio++;
  1334. STp->nbr_pages += STbp->do_dio;
  1335. }
  1336. )
  1337. } else
  1338. STbp->do_dio = 0;
  1339. DEB( STp->nbr_requests++; )
  1340. if (!STbp->do_dio) {
  1341. if (STp->block_size)
  1342. bufsize = STp->block_size > st_fixed_buffer_size ?
  1343. STp->block_size : st_fixed_buffer_size;
  1344. else {
  1345. bufsize = count;
  1346. /* Make sure that data from previous user is not leaked even if
  1347. HBA does not return correct residual */
  1348. if (is_read && STp->sili && !STbp->cleared)
  1349. clear_buffer(STbp);
  1350. }
  1351. if (bufsize > STbp->buffer_size &&
  1352. !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
  1353. st_printk(KERN_WARNING, STp,
  1354. "Can't allocate %d byte tape buffer.\n",
  1355. bufsize);
  1356. retval = (-EOVERFLOW);
  1357. goto out;
  1358. }
  1359. if (STp->block_size)
  1360. STbp->buffer_blocks = bufsize / STp->block_size;
  1361. }
  1362. out:
  1363. return retval;
  1364. }
  1365. /* Can be called more than once after each setup_buffer() */
  1366. static void release_buffering(struct scsi_tape *STp, int is_read)
  1367. {
  1368. struct st_buffer *STbp;
  1369. STbp = STp->buffer;
  1370. if (STbp->do_dio) {
  1371. sgl_unmap_user_pages(STbp, STbp->do_dio, is_read);
  1372. STbp->do_dio = 0;
  1373. STbp->sg_segs = 0;
  1374. }
  1375. }
  1376. /* Write command */
  1377. static ssize_t
  1378. st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
  1379. {
  1380. ssize_t total;
  1381. ssize_t i, do_count, blks, transfer;
  1382. ssize_t retval;
  1383. int undone, retry_eot = 0, scode;
  1384. int async_write;
  1385. unsigned char cmd[MAX_COMMAND_SIZE];
  1386. const char __user *b_point;
  1387. struct st_request *SRpnt = NULL;
  1388. struct scsi_tape *STp = filp->private_data;
  1389. struct st_modedef *STm;
  1390. struct st_partstat *STps;
  1391. struct st_buffer *STbp;
  1392. if (mutex_lock_interruptible(&STp->lock))
  1393. return -ERESTARTSYS;
  1394. retval = rw_checks(STp, filp, count);
  1395. if (retval || count == 0)
  1396. goto out;
  1397. /* Write must be integral number of blocks */
  1398. if (STp->block_size != 0 && (count % STp->block_size) != 0) {
  1399. st_printk(KERN_WARNING, STp,
  1400. "Write not multiple of tape block size.\n");
  1401. retval = (-EINVAL);
  1402. goto out;
  1403. }
  1404. STm = &(STp->modes[STp->current_mode]);
  1405. STps = &(STp->ps[STp->partition]);
  1406. if (STp->write_prot) {
  1407. retval = (-EACCES);
  1408. goto out;
  1409. }
  1410. if (STps->rw == ST_READING) {
  1411. retval = flush_buffer(STp, 0);
  1412. if (retval)
  1413. goto out;
  1414. STps->rw = ST_WRITING;
  1415. } else if (STps->rw != ST_WRITING &&
  1416. STps->drv_file == 0 && STps->drv_block == 0) {
  1417. if ((retval = set_mode_densblk(STp, STm)) < 0)
  1418. goto out;
  1419. if (STm->default_compression != ST_DONT_TOUCH &&
  1420. !(STp->compression_changed)) {
  1421. if (st_compression(STp, (STm->default_compression == ST_YES))) {
  1422. st_printk(KERN_WARNING, STp,
  1423. "Can't set default compression.\n");
  1424. if (modes_defined) {
  1425. retval = (-EINVAL);
  1426. goto out;
  1427. }
  1428. }
  1429. }
  1430. }
  1431. STbp = STp->buffer;
  1432. i = write_behind_check(STp);
  1433. if (i) {
  1434. if (i == -ENOSPC)
  1435. STps->eof = ST_EOM_OK;
  1436. else
  1437. STps->eof = ST_EOM_ERROR;
  1438. }
  1439. if (STps->eof == ST_EOM_OK) {
  1440. STps->eof = ST_EOD_1; /* allow next write */
  1441. retval = (-ENOSPC);
  1442. goto out;
  1443. }
  1444. else if (STps->eof == ST_EOM_ERROR) {
  1445. retval = (-EIO);
  1446. goto out;
  1447. }
  1448. /* Check the buffer readability in cases where copy_user might catch
  1449. the problems after some tape movement. */
  1450. if (STp->block_size != 0 &&
  1451. !STbp->do_dio &&
  1452. (copy_from_user(&i, buf, 1) != 0 ||
  1453. copy_from_user(&i, buf + count - 1, 1) != 0)) {
  1454. retval = (-EFAULT);
  1455. goto out;
  1456. }
  1457. retval = setup_buffering(STp, buf, count, 0);
  1458. if (retval)
  1459. goto out;
  1460. total = count;
  1461. memset(cmd, 0, MAX_COMMAND_SIZE);
  1462. cmd[0] = WRITE_6;
  1463. cmd[1] = (STp->block_size != 0);
  1464. STps->rw = ST_WRITING;
  1465. b_point = buf;
  1466. while (count > 0 && !retry_eot) {
  1467. if (STbp->do_dio) {
  1468. do_count = count;
  1469. }
  1470. else {
  1471. if (STp->block_size == 0)
  1472. do_count = count;
  1473. else {
  1474. do_count = STbp->buffer_blocks * STp->block_size -
  1475. STbp->buffer_bytes;
  1476. if (do_count > count)
  1477. do_count = count;
  1478. }
  1479. i = append_to_buffer(b_point, STbp, do_count);
  1480. if (i) {
  1481. retval = i;
  1482. goto out;
  1483. }
  1484. }
  1485. count -= do_count;
  1486. b_point += do_count;
  1487. async_write = STp->block_size == 0 && !STbp->do_dio &&
  1488. STm->do_async_writes && STps->eof < ST_EOM_OK;
  1489. if (STp->block_size != 0 && STm->do_buffer_writes &&
  1490. !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
  1491. STbp->buffer_bytes < STbp->buffer_size) {
  1492. STp->dirty = 1;
  1493. /* Don't write a buffer that is not full enough. */
  1494. if (!async_write && count == 0)
  1495. break;
  1496. }
  1497. retry_write:
  1498. if (STp->block_size == 0)
  1499. blks = transfer = do_count;
  1500. else {
  1501. if (!STbp->do_dio)
  1502. blks = STbp->buffer_bytes;
  1503. else
  1504. blks = do_count;
  1505. blks /= STp->block_size;
  1506. transfer = blks * STp->block_size;
  1507. }
  1508. cmd[2] = blks >> 16;
  1509. cmd[3] = blks >> 8;
  1510. cmd[4] = blks;
  1511. SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
  1512. STp->device->request_queue->rq_timeout,
  1513. MAX_WRITE_RETRIES, !async_write);
  1514. if (!SRpnt) {
  1515. retval = STbp->syscall_result;
  1516. goto out;
  1517. }
  1518. if (async_write && !STbp->syscall_result) {
  1519. STbp->writing = transfer;
  1520. STp->dirty = !(STbp->writing ==
  1521. STbp->buffer_bytes);
  1522. SRpnt = NULL; /* Prevent releasing this request! */
  1523. DEB( STp->write_pending = 1; )
  1524. break;
  1525. }
  1526. if (STbp->syscall_result != 0) {
  1527. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  1528. DEBC_printk(STp, "Error on write:\n");
  1529. if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
  1530. scode = cmdstatp->sense_hdr.sense_key;
  1531. if (cmdstatp->remainder_valid)
  1532. undone = (int)cmdstatp->uremainder64;
  1533. else if (STp->block_size == 0 &&
  1534. scode == VOLUME_OVERFLOW)
  1535. undone = transfer;
  1536. else
  1537. undone = 0;
  1538. if (STp->block_size != 0)
  1539. undone *= STp->block_size;
  1540. if (undone <= do_count) {
  1541. /* Only data from this write is not written */
  1542. count += undone;
  1543. b_point -= undone;
  1544. do_count -= undone;
  1545. if (STp->block_size)
  1546. blks = (transfer - undone) / STp->block_size;
  1547. STps->eof = ST_EOM_OK;
  1548. /* Continue in fixed block mode if all written
  1549. in this request but still something left to write
  1550. (retval left to zero)
  1551. */
  1552. if (STp->block_size == 0 ||
  1553. undone > 0 || count == 0)
  1554. retval = (-ENOSPC); /* EOM within current request */
  1555. DEBC_printk(STp, "EOM with %d "
  1556. "bytes unwritten.\n",
  1557. (int)count);
  1558. } else {
  1559. /* EOT within data buffered earlier (possible only
  1560. in fixed block mode without direct i/o) */
  1561. if (!retry_eot && !cmdstatp->deferred &&
  1562. (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
  1563. move_buffer_data(STp->buffer, transfer - undone);
  1564. retry_eot = 1;
  1565. if (STps->drv_block >= 0) {
  1566. STps->drv_block += (transfer - undone) /
  1567. STp->block_size;
  1568. }
  1569. STps->eof = ST_EOM_OK;
  1570. DEBC_printk(STp, "Retry "
  1571. "write of %d "
  1572. "bytes at EOM.\n",
  1573. STp->buffer->buffer_bytes);
  1574. goto retry_write;
  1575. }
  1576. else {
  1577. /* Either error within data buffered by driver or
  1578. failed retry */
  1579. count -= do_count;
  1580. blks = do_count = 0;
  1581. STps->eof = ST_EOM_ERROR;
  1582. STps->drv_block = (-1); /* Too cautious? */
  1583. retval = (-EIO); /* EOM for old data */
  1584. DEBC_printk(STp, "EOM with "
  1585. "lost data.\n");
  1586. }
  1587. }
  1588. } else {
  1589. count += do_count;
  1590. STps->drv_block = (-1); /* Too cautious? */
  1591. retval = STbp->syscall_result;
  1592. }
  1593. }
  1594. if (STps->drv_block >= 0) {
  1595. if (STp->block_size == 0)
  1596. STps->drv_block += (do_count > 0);
  1597. else
  1598. STps->drv_block += blks;
  1599. }
  1600. STbp->buffer_bytes = 0;
  1601. STp->dirty = 0;
  1602. if (retval || retry_eot) {
  1603. if (count < total)
  1604. retval = total - count;
  1605. goto out;
  1606. }
  1607. }
  1608. if (STps->eof == ST_EOD_1)
  1609. STps->eof = ST_EOM_OK;
  1610. else if (STps->eof != ST_EOM_OK)
  1611. STps->eof = ST_NOEOF;
  1612. retval = total - count;
  1613. out:
  1614. if (SRpnt != NULL)
  1615. st_release_request(SRpnt);
  1616. release_buffering(STp, 0);
  1617. mutex_unlock(&STp->lock);
  1618. return retval;
  1619. }
  1620. /* Read data from the tape. Returns zero in the normal case, one if the
  1621. eof status has changed, and the negative error code in case of a
  1622. fatal error. Otherwise updates the buffer and the eof state.
  1623. Does release user buffer mapping if it is set.
  1624. */
  1625. static long read_tape(struct scsi_tape *STp, long count,
  1626. struct st_request ** aSRpnt)
  1627. {
  1628. int transfer, blks, bytes;
  1629. unsigned char cmd[MAX_COMMAND_SIZE];
  1630. struct st_request *SRpnt;
  1631. struct st_modedef *STm;
  1632. struct st_partstat *STps;
  1633. struct st_buffer *STbp;
  1634. int retval = 0;
  1635. if (count == 0)
  1636. return 0;
  1637. STm = &(STp->modes[STp->current_mode]);
  1638. STps = &(STp->ps[STp->partition]);
  1639. if (STps->eof == ST_FM_HIT)
  1640. return 1;
  1641. STbp = STp->buffer;
  1642. if (STp->block_size == 0)
  1643. blks = bytes = count;
  1644. else {
  1645. if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
  1646. blks = (STp->buffer)->buffer_blocks;
  1647. bytes = blks * STp->block_size;
  1648. } else {
  1649. bytes = count;
  1650. if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
  1651. bytes = (STp->buffer)->buffer_size;
  1652. blks = bytes / STp->block_size;
  1653. bytes = blks * STp->block_size;
  1654. }
  1655. }
  1656. memset(cmd, 0, MAX_COMMAND_SIZE);
  1657. cmd[0] = READ_6;
  1658. cmd[1] = (STp->block_size != 0);
  1659. if (!cmd[1] && STp->sili)
  1660. cmd[1] |= 2;
  1661. cmd[2] = blks >> 16;
  1662. cmd[3] = blks >> 8;
  1663. cmd[4] = blks;
  1664. SRpnt = *aSRpnt;
  1665. SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
  1666. STp->device->request_queue->rq_timeout,
  1667. MAX_RETRIES, 1);
  1668. release_buffering(STp, 1);
  1669. *aSRpnt = SRpnt;
  1670. if (!SRpnt)
  1671. return STbp->syscall_result;
  1672. STbp->read_pointer = 0;
  1673. STps->at_sm = 0;
  1674. /* Something to check */
  1675. if (STbp->syscall_result) {
  1676. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  1677. retval = 1;
  1678. DEBC_printk(STp,
  1679. "Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
  1680. SRpnt->sense[0], SRpnt->sense[1],
  1681. SRpnt->sense[2], SRpnt->sense[3],
  1682. SRpnt->sense[4], SRpnt->sense[5],
  1683. SRpnt->sense[6], SRpnt->sense[7]);
  1684. if (cmdstatp->have_sense) {
  1685. if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
  1686. cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
  1687. if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
  1688. /* Compute the residual count */
  1689. if (cmdstatp->remainder_valid)
  1690. transfer = (int)cmdstatp->uremainder64;
  1691. else
  1692. transfer = 0;
  1693. if (cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR) {
  1694. if (STp->block_size == 0)
  1695. transfer = bytes;
  1696. /* Some drives set ILI with MEDIUM ERROR */
  1697. cmdstatp->flags &= ~SENSE_ILI;
  1698. }
  1699. if (cmdstatp->flags & SENSE_ILI) { /* ILI */
  1700. if (STp->block_size == 0 &&
  1701. transfer < 0) {
  1702. st_printk(KERN_NOTICE, STp,
  1703. "Failed to read %d "
  1704. "byte block with %d "
  1705. "byte transfer.\n",
  1706. bytes - transfer,
  1707. bytes);
  1708. if (STps->drv_block >= 0)
  1709. STps->drv_block += 1;
  1710. STbp->buffer_bytes = 0;
  1711. return (-ENOMEM);
  1712. } else if (STp->block_size == 0) {
  1713. STbp->buffer_bytes = bytes - transfer;
  1714. } else {
  1715. st_release_request(SRpnt);
  1716. SRpnt = *aSRpnt = NULL;
  1717. if (transfer == blks) { /* We did not get anything, error */
  1718. st_printk(KERN_NOTICE, STp,
  1719. "Incorrect "
  1720. "block size.\n");
  1721. if (STps->drv_block >= 0)
  1722. STps->drv_block += blks - transfer + 1;
  1723. st_int_ioctl(STp, MTBSR, 1);
  1724. return (-EIO);
  1725. }
  1726. /* We have some data, deliver it */
  1727. STbp->buffer_bytes = (blks - transfer) *
  1728. STp->block_size;
  1729. DEBC_printk(STp, "ILI but "
  1730. "enough data "
  1731. "received %ld "
  1732. "%d.\n", count,
  1733. STbp->buffer_bytes);
  1734. if (STps->drv_block >= 0)
  1735. STps->drv_block += 1;
  1736. if (st_int_ioctl(STp, MTBSR, 1))
  1737. return (-EIO);
  1738. }
  1739. } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
  1740. if (STps->eof != ST_FM_HIT)
  1741. STps->eof = ST_FM_HIT;
  1742. else
  1743. STps->eof = ST_EOD_2;
  1744. if (STp->block_size == 0)
  1745. STbp->buffer_bytes = 0;
  1746. else
  1747. STbp->buffer_bytes =
  1748. bytes - transfer * STp->block_size;
  1749. DEBC_printk(STp, "EOF detected (%d "
  1750. "bytes read).\n",
  1751. STbp->buffer_bytes);
  1752. } else if (cmdstatp->flags & SENSE_EOM) {
  1753. if (STps->eof == ST_FM)
  1754. STps->eof = ST_EOD_1;
  1755. else
  1756. STps->eof = ST_EOM_OK;
  1757. if (STp->block_size == 0)
  1758. STbp->buffer_bytes = bytes - transfer;
  1759. else
  1760. STbp->buffer_bytes =
  1761. bytes - transfer * STp->block_size;
  1762. DEBC_printk(STp, "EOM detected (%d "
  1763. "bytes read).\n",
  1764. STbp->buffer_bytes);
  1765. }
  1766. }
  1767. /* end of EOF, EOM, ILI test */
  1768. else { /* nonzero sense key */
  1769. DEBC_printk(STp, "Tape error while reading.\n");
  1770. STps->drv_block = (-1);
  1771. if (STps->eof == ST_FM &&
  1772. cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
  1773. DEBC_printk(STp, "Zero returned for "
  1774. "first BLANK CHECK "
  1775. "after EOF.\n");
  1776. STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
  1777. } else /* Some other extended sense code */
  1778. retval = (-EIO);
  1779. }
  1780. if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
  1781. STbp->buffer_bytes = 0;
  1782. }
  1783. /* End of extended sense test */
  1784. else { /* Non-extended sense */
  1785. retval = STbp->syscall_result;
  1786. }
  1787. }
  1788. /* End of error handling */
  1789. else { /* Read successful */
  1790. STbp->buffer_bytes = bytes;
  1791. if (STp->sili) /* In fixed block mode residual is always zero here */
  1792. STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
  1793. }
  1794. if (STps->drv_block >= 0) {
  1795. if (STp->block_size == 0)
  1796. STps->drv_block++;
  1797. else
  1798. STps->drv_block += STbp->buffer_bytes / STp->block_size;
  1799. }
  1800. return retval;
  1801. }
  1802. /* Read command */
  1803. static ssize_t
  1804. st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
  1805. {
  1806. ssize_t total;
  1807. ssize_t retval = 0;
  1808. ssize_t i, transfer;
  1809. int special, do_dio = 0;
  1810. struct st_request *SRpnt = NULL;
  1811. struct scsi_tape *STp = filp->private_data;
  1812. struct st_modedef *STm;
  1813. struct st_partstat *STps;
  1814. struct st_buffer *STbp = STp->buffer;
  1815. if (mutex_lock_interruptible(&STp->lock))
  1816. return -ERESTARTSYS;
  1817. retval = rw_checks(STp, filp, count);
  1818. if (retval || count == 0)
  1819. goto out;
  1820. STm = &(STp->modes[STp->current_mode]);
  1821. if (STp->block_size != 0 && (count % STp->block_size) != 0) {
  1822. if (!STm->do_read_ahead) {
  1823. retval = (-EINVAL); /* Read must be integral number of blocks */
  1824. goto out;
  1825. }
  1826. STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
  1827. }
  1828. STps = &(STp->ps[STp->partition]);
  1829. if (STps->rw == ST_WRITING) {
  1830. retval = flush_buffer(STp, 0);
  1831. if (retval)
  1832. goto out;
  1833. STps->rw = ST_READING;
  1834. }
  1835. DEB(
  1836. if (debugging && STps->eof != ST_NOEOF)
  1837. st_printk(ST_DEB_MSG, STp,
  1838. "EOF/EOM flag up (%d). Bytes %d\n",
  1839. STps->eof, STbp->buffer_bytes);
  1840. ) /* end DEB */
  1841. retval = setup_buffering(STp, buf, count, 1);
  1842. if (retval)
  1843. goto out;
  1844. do_dio = STbp->do_dio;
  1845. if (STbp->buffer_bytes == 0 &&
  1846. STps->eof >= ST_EOD_1) {
  1847. if (STps->eof < ST_EOD) {
  1848. STps->eof += 1;
  1849. retval = 0;
  1850. goto out;
  1851. }
  1852. retval = (-EIO); /* EOM or Blank Check */
  1853. goto out;
  1854. }
  1855. if (do_dio) {
  1856. /* Check the buffer writability before any tape movement. Don't alter
  1857. buffer data. */
  1858. if (copy_from_user(&i, buf, 1) != 0 ||
  1859. copy_to_user(buf, &i, 1) != 0 ||
  1860. copy_from_user(&i, buf + count - 1, 1) != 0 ||
  1861. copy_to_user(buf + count - 1, &i, 1) != 0) {
  1862. retval = (-EFAULT);
  1863. goto out;
  1864. }
  1865. }
  1866. STps->rw = ST_READING;
  1867. /* Loop until enough data in buffer or a special condition found */
  1868. for (total = 0, special = 0; total < count && !special;) {
  1869. /* Get new data if the buffer is empty */
  1870. if (STbp->buffer_bytes == 0) {
  1871. special = read_tape(STp, count - total, &SRpnt);
  1872. if (special < 0) { /* No need to continue read */
  1873. retval = special;
  1874. goto out;
  1875. }
  1876. }
  1877. /* Move the data from driver buffer to user buffer */
  1878. if (STbp->buffer_bytes > 0) {
  1879. DEB(
  1880. if (debugging && STps->eof != ST_NOEOF)
  1881. st_printk(ST_DEB_MSG, STp,
  1882. "EOF up (%d). Left %d, needed %d.\n",
  1883. STps->eof, STbp->buffer_bytes,
  1884. (int)(count - total));
  1885. ) /* end DEB */
  1886. transfer = STbp->buffer_bytes < count - total ?
  1887. STbp->buffer_bytes : count - total;
  1888. if (!do_dio) {
  1889. i = from_buffer(STbp, buf, transfer);
  1890. if (i) {
  1891. retval = i;
  1892. goto out;
  1893. }
  1894. }
  1895. buf += transfer;
  1896. total += transfer;
  1897. }
  1898. if (STp->block_size == 0)
  1899. break; /* Read only one variable length block */
  1900. } /* for (total = 0, special = 0;
  1901. total < count && !special; ) */
  1902. /* Change the eof state if no data from tape or buffer */
  1903. if (total == 0) {
  1904. if (STps->eof == ST_FM_HIT) {
  1905. STps->eof = ST_FM;
  1906. STps->drv_block = 0;
  1907. if (STps->drv_file >= 0)
  1908. STps->drv_file++;
  1909. } else if (STps->eof == ST_EOD_1) {
  1910. STps->eof = ST_EOD_2;
  1911. STps->drv_block = 0;
  1912. if (STps->drv_file >= 0)
  1913. STps->drv_file++;
  1914. } else if (STps->eof == ST_EOD_2)
  1915. STps->eof = ST_EOD;
  1916. } else if (STps->eof == ST_FM)
  1917. STps->eof = ST_NOEOF;
  1918. retval = total;
  1919. out:
  1920. if (SRpnt != NULL) {
  1921. st_release_request(SRpnt);
  1922. SRpnt = NULL;
  1923. }
  1924. if (do_dio) {
  1925. release_buffering(STp, 1);
  1926. STbp->buffer_bytes = 0;
  1927. }
  1928. mutex_unlock(&STp->lock);
  1929. return retval;
  1930. }
  1931. DEB(
  1932. /* Set the driver options */
  1933. static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm)
  1934. {
  1935. if (debugging) {
  1936. st_printk(KERN_INFO, STp,
  1937. "Mode %d options: buffer writes: %d, "
  1938. "async writes: %d, read ahead: %d\n",
  1939. STp->current_mode, STm->do_buffer_writes,
  1940. STm->do_async_writes, STm->do_read_ahead);
  1941. st_printk(KERN_INFO, STp,
  1942. " can bsr: %d, two FMs: %d, "
  1943. "fast mteom: %d, auto lock: %d,\n",
  1944. STp->can_bsr, STp->two_fm, STp->fast_mteom,
  1945. STp->do_auto_lock);
  1946. st_printk(KERN_INFO, STp,
  1947. " defs for wr: %d, no block limits: %d, "
  1948. "partitions: %d, s2 log: %d\n",
  1949. STm->defaults_for_writes, STp->omit_blklims,
  1950. STp->can_partitions, STp->scsi2_logical);
  1951. st_printk(KERN_INFO, STp,
  1952. " sysv: %d nowait: %d sili: %d "
  1953. "nowait_filemark: %d\n",
  1954. STm->sysv, STp->immediate, STp->sili,
  1955. STp->immediate_filemark);
  1956. st_printk(KERN_INFO, STp, " debugging: %d\n", debugging);
  1957. }
  1958. }
  1959. )
  1960. static int st_set_options(struct scsi_tape *STp, long options)
  1961. {
  1962. int value;
  1963. long code;
  1964. struct st_modedef *STm;
  1965. struct cdev *cd0, *cd1;
  1966. struct device *d0, *d1;
  1967. STm = &(STp->modes[STp->current_mode]);
  1968. if (!STm->defined) {
  1969. cd0 = STm->cdevs[0];
  1970. cd1 = STm->cdevs[1];
  1971. d0 = STm->devs[0];
  1972. d1 = STm->devs[1];
  1973. memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
  1974. STm->cdevs[0] = cd0;
  1975. STm->cdevs[1] = cd1;
  1976. STm->devs[0] = d0;
  1977. STm->devs[1] = d1;
  1978. modes_defined = 1;
  1979. DEBC_printk(STp, "Initialized mode %d definition from mode 0\n",
  1980. STp->current_mode);
  1981. }
  1982. code = options & MT_ST_OPTIONS;
  1983. if (code == MT_ST_BOOLEANS) {
  1984. STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
  1985. STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
  1986. STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
  1987. STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
  1988. STp->two_fm = (options & MT_ST_TWO_FM) != 0;
  1989. STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
  1990. STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
  1991. STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
  1992. STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
  1993. if ((STp->device)->scsi_level >= SCSI_2)
  1994. STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
  1995. STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
  1996. STp->immediate = (options & MT_ST_NOWAIT) != 0;
  1997. STp->immediate_filemark = (options & MT_ST_NOWAIT_EOF) != 0;
  1998. STm->sysv = (options & MT_ST_SYSV) != 0;
  1999. STp->sili = (options & MT_ST_SILI) != 0;
  2000. DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
  2001. st_log_options(STp, STm); )
  2002. } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
  2003. value = (code == MT_ST_SETBOOLEANS);
  2004. if ((options & MT_ST_BUFFER_WRITES) != 0)
  2005. STm->do_buffer_writes = value;
  2006. if ((options & MT_ST_ASYNC_WRITES) != 0)
  2007. STm->do_async_writes = value;
  2008. if ((options & MT_ST_DEF_WRITES) != 0)
  2009. STm->defaults_for_writes = value;
  2010. if ((options & MT_ST_READ_AHEAD) != 0)
  2011. STm->do_read_ahead = value;
  2012. if ((options & MT_ST_TWO_FM) != 0)
  2013. STp->two_fm = value;
  2014. if ((options & MT_ST_FAST_MTEOM) != 0)
  2015. STp->fast_mteom = value;
  2016. if ((options & MT_ST_AUTO_LOCK) != 0)
  2017. STp->do_auto_lock = value;
  2018. if ((options & MT_ST_CAN_BSR) != 0)
  2019. STp->can_bsr = value;
  2020. if ((options & MT_ST_NO_BLKLIMS) != 0)
  2021. STp->omit_blklims = value;
  2022. if ((STp->device)->scsi_level >= SCSI_2 &&
  2023. (options & MT_ST_CAN_PARTITIONS) != 0)
  2024. STp->can_partitions = value;
  2025. if ((options & MT_ST_SCSI2LOGICAL) != 0)
  2026. STp->scsi2_logical = value;
  2027. if ((options & MT_ST_NOWAIT) != 0)
  2028. STp->immediate = value;
  2029. if ((options & MT_ST_NOWAIT_EOF) != 0)
  2030. STp->immediate_filemark = value;
  2031. if ((options & MT_ST_SYSV) != 0)
  2032. STm->sysv = value;
  2033. if ((options & MT_ST_SILI) != 0)
  2034. STp->sili = value;
  2035. DEB(
  2036. if ((options & MT_ST_DEBUGGING) != 0)
  2037. debugging = value;
  2038. st_log_options(STp, STm); )
  2039. } else if (code == MT_ST_WRITE_THRESHOLD) {
  2040. /* Retained for compatibility */
  2041. } else if (code == MT_ST_DEF_BLKSIZE) {
  2042. value = (options & ~MT_ST_OPTIONS);
  2043. if (value == ~MT_ST_OPTIONS) {
  2044. STm->default_blksize = (-1);
  2045. DEBC_printk(STp, "Default block size disabled.\n");
  2046. } else {
  2047. STm->default_blksize = value;
  2048. DEBC_printk(STp,"Default block size set to "
  2049. "%d bytes.\n", STm->default_blksize);
  2050. if (STp->ready == ST_READY) {
  2051. STp->blksize_changed = 0;
  2052. set_mode_densblk(STp, STm);
  2053. }
  2054. }
  2055. } else if (code == MT_ST_TIMEOUTS) {
  2056. value = (options & ~MT_ST_OPTIONS);
  2057. if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
  2058. STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
  2059. DEBC_printk(STp, "Long timeout set to %d seconds.\n",
  2060. (value & ~MT_ST_SET_LONG_TIMEOUT));
  2061. } else {
  2062. blk_queue_rq_timeout(STp->device->request_queue,
  2063. value * HZ);
  2064. DEBC_printk(STp, "Normal timeout set to %d seconds.\n",
  2065. value);
  2066. }
  2067. } else if (code == MT_ST_SET_CLN) {
  2068. value = (options & ~MT_ST_OPTIONS) & 0xff;
  2069. if (value != 0 &&
  2070. (value < EXTENDED_SENSE_START ||
  2071. value >= SCSI_SENSE_BUFFERSIZE))
  2072. return (-EINVAL);
  2073. STp->cln_mode = value;
  2074. STp->cln_sense_mask = (options >> 8) & 0xff;
  2075. STp->cln_sense_value = (options >> 16) & 0xff;
  2076. st_printk(KERN_INFO, STp,
  2077. "Cleaning request mode %d, mask %02x, value %02x\n",
  2078. value, STp->cln_sense_mask, STp->cln_sense_value);
  2079. } else if (code == MT_ST_DEF_OPTIONS) {
  2080. code = (options & ~MT_ST_CLEAR_DEFAULT);
  2081. value = (options & MT_ST_CLEAR_DEFAULT);
  2082. if (code == MT_ST_DEF_DENSITY) {
  2083. if (value == MT_ST_CLEAR_DEFAULT) {
  2084. STm->default_density = (-1);
  2085. DEBC_printk(STp,
  2086. "Density default disabled.\n");
  2087. } else {
  2088. STm->default_density = value & 0xff;
  2089. DEBC_printk(STp, "Density default set to %x\n",
  2090. STm->default_density);
  2091. if (STp->ready == ST_READY) {
  2092. STp->density_changed = 0;
  2093. set_mode_densblk(STp, STm);
  2094. }
  2095. }
  2096. } else if (code == MT_ST_DEF_DRVBUFFER) {
  2097. if (value == MT_ST_CLEAR_DEFAULT) {
  2098. STp->default_drvbuffer = 0xff;
  2099. DEBC_printk(STp,
  2100. "Drive buffer default disabled.\n");
  2101. } else {
  2102. STp->default_drvbuffer = value & 7;
  2103. DEBC_printk(STp,
  2104. "Drive buffer default set to %x\n",
  2105. STp->default_drvbuffer);
  2106. if (STp->ready == ST_READY)
  2107. st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
  2108. }
  2109. } else if (code == MT_ST_DEF_COMPRESSION) {
  2110. if (value == MT_ST_CLEAR_DEFAULT) {
  2111. STm->default_compression = ST_DONT_TOUCH;
  2112. DEBC_printk(STp,
  2113. "Compression default disabled.\n");
  2114. } else {
  2115. if ((value & 0xff00) != 0) {
  2116. STp->c_algo = (value & 0xff00) >> 8;
  2117. DEBC_printk(STp, "Compression "
  2118. "algorithm set to 0x%x.\n",
  2119. STp->c_algo);
  2120. }
  2121. if ((value & 0xff) != 0xff) {
  2122. STm->default_compression = (value & 1 ? ST_YES : ST_NO);
  2123. DEBC_printk(STp, "Compression default "
  2124. "set to %x\n",
  2125. (value & 1));
  2126. if (STp->ready == ST_READY) {
  2127. STp->compression_changed = 0;
  2128. st_compression(STp, (STm->default_compression == ST_YES));
  2129. }
  2130. }
  2131. }
  2132. }
  2133. } else
  2134. return (-EIO);
  2135. return 0;
  2136. }
  2137. #define MODE_HEADER_LENGTH 4
  2138. /* Mode header and page byte offsets */
  2139. #define MH_OFF_DATA_LENGTH 0
  2140. #define MH_OFF_MEDIUM_TYPE 1
  2141. #define MH_OFF_DEV_SPECIFIC 2
  2142. #define MH_OFF_BDESCS_LENGTH 3
  2143. #define MP_OFF_PAGE_NBR 0
  2144. #define MP_OFF_PAGE_LENGTH 1
  2145. /* Mode header and page bit masks */
  2146. #define MH_BIT_WP 0x80
  2147. #define MP_MSK_PAGE_NBR 0x3f
  2148. /* Don't return block descriptors */
  2149. #define MODE_SENSE_OMIT_BDESCS 0x08
  2150. #define MODE_SELECT_PAGE_FORMAT 0x10
  2151. /* Read a mode page into the tape buffer. The block descriptors are included
  2152. if incl_block_descs is true. The page control is ored to the page number
  2153. parameter, if necessary. */
  2154. static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
  2155. {
  2156. unsigned char cmd[MAX_COMMAND_SIZE];
  2157. struct st_request *SRpnt;
  2158. memset(cmd, 0, MAX_COMMAND_SIZE);
  2159. cmd[0] = MODE_SENSE;
  2160. if (omit_block_descs)
  2161. cmd[1] = MODE_SENSE_OMIT_BDESCS;
  2162. cmd[2] = page;
  2163. cmd[4] = 255;
  2164. SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_FROM_DEVICE,
  2165. STp->device->request_queue->rq_timeout, 0, 1);
  2166. if (SRpnt == NULL)
  2167. return (STp->buffer)->syscall_result;
  2168. st_release_request(SRpnt);
  2169. return STp->buffer->syscall_result;
  2170. }
  2171. /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
  2172. in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
  2173. static int write_mode_page(struct scsi_tape *STp, int page, int slow)
  2174. {
  2175. int pgo;
  2176. unsigned char cmd[MAX_COMMAND_SIZE];
  2177. struct st_request *SRpnt;
  2178. int timeout;
  2179. memset(cmd, 0, MAX_COMMAND_SIZE);
  2180. cmd[0] = MODE_SELECT;
  2181. cmd[1] = MODE_SELECT_PAGE_FORMAT;
  2182. pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
  2183. cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
  2184. /* Clear reserved fields */
  2185. (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
  2186. (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
  2187. (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
  2188. (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
  2189. timeout = slow ?
  2190. STp->long_timeout : STp->device->request_queue->rq_timeout;
  2191. SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_TO_DEVICE,
  2192. timeout, 0, 1);
  2193. if (SRpnt == NULL)
  2194. return (STp->buffer)->syscall_result;
  2195. st_release_request(SRpnt);
  2196. return STp->buffer->syscall_result;
  2197. }
  2198. #define COMPRESSION_PAGE 0x0f
  2199. #define COMPRESSION_PAGE_LENGTH 16
  2200. #define CP_OFF_DCE_DCC 2
  2201. #define CP_OFF_C_ALGO 7
  2202. #define DCE_MASK 0x80
  2203. #define DCC_MASK 0x40
  2204. #define RED_MASK 0x60
  2205. /* Control the compression with mode page 15. Algorithm not changed if zero.
  2206. The block descriptors are read and written because Sony SDT-7000 does not
  2207. work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
  2208. Including block descriptors should not cause any harm to other drives. */
  2209. static int st_compression(struct scsi_tape * STp, int state)
  2210. {
  2211. int retval;
  2212. int mpoffs; /* Offset to mode page start */
  2213. unsigned char *b_data = (STp->buffer)->b_data;
  2214. if (STp->ready != ST_READY)
  2215. return (-EIO);
  2216. /* Read the current page contents */
  2217. retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
  2218. if (retval) {
  2219. DEBC_printk(STp, "Compression mode page not supported.\n");
  2220. return (-EIO);
  2221. }
  2222. mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
  2223. DEBC_printk(STp, "Compression state is %d.\n",
  2224. (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0));
  2225. /* Check if compression can be changed */
  2226. if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
  2227. DEBC_printk(STp, "Compression not supported.\n");
  2228. return (-EIO);
  2229. }
  2230. /* Do the change */
  2231. if (state) {
  2232. b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
  2233. if (STp->c_algo != 0)
  2234. b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
  2235. }
  2236. else {
  2237. b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
  2238. if (STp->c_algo != 0)
  2239. b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
  2240. }
  2241. retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
  2242. if (retval) {
  2243. DEBC_printk(STp, "Compression change failed.\n");
  2244. return (-EIO);
  2245. }
  2246. DEBC_printk(STp, "Compression state changed to %d.\n", state);
  2247. STp->compression_changed = 1;
  2248. return 0;
  2249. }
  2250. /* Process the load and unload commands (does unload if the load code is zero) */
  2251. static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
  2252. {
  2253. int retval = (-EIO), timeout;
  2254. unsigned char cmd[MAX_COMMAND_SIZE];
  2255. struct st_partstat *STps;
  2256. struct st_request *SRpnt;
  2257. if (STp->ready != ST_READY && !load_code) {
  2258. if (STp->ready == ST_NO_TAPE)
  2259. return (-ENOMEDIUM);
  2260. else
  2261. return (-EIO);
  2262. }
  2263. memset(cmd, 0, MAX_COMMAND_SIZE);
  2264. cmd[0] = START_STOP;
  2265. if (load_code)
  2266. cmd[4] |= 1;
  2267. /*
  2268. * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
  2269. */
  2270. if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
  2271. && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
  2272. DEBC_printk(STp, " Enhanced %sload slot %2d.\n",
  2273. (cmd[4]) ? "" : "un",
  2274. load_code - MT_ST_HPLOADER_OFFSET);
  2275. cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
  2276. }
  2277. if (STp->immediate) {
  2278. cmd[1] = 1; /* Don't wait for completion */
  2279. timeout = STp->device->request_queue->rq_timeout;
  2280. }
  2281. else
  2282. timeout = STp->long_timeout;
  2283. DEBC(
  2284. if (!load_code)
  2285. st_printk(ST_DEB_MSG, STp, "Unloading tape.\n");
  2286. else
  2287. st_printk(ST_DEB_MSG, STp, "Loading tape.\n");
  2288. );
  2289. SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  2290. timeout, MAX_RETRIES, 1);
  2291. if (!SRpnt)
  2292. return (STp->buffer)->syscall_result;
  2293. retval = (STp->buffer)->syscall_result;
  2294. st_release_request(SRpnt);
  2295. if (!retval) { /* SCSI command successful */
  2296. if (!load_code) {
  2297. STp->rew_at_close = 0;
  2298. STp->ready = ST_NO_TAPE;
  2299. }
  2300. else {
  2301. STp->rew_at_close = STp->autorew_dev;
  2302. retval = check_tape(STp, filp);
  2303. if (retval > 0)
  2304. retval = 0;
  2305. }
  2306. }
  2307. else {
  2308. STps = &(STp->ps[STp->partition]);
  2309. STps->drv_file = STps->drv_block = (-1);
  2310. }
  2311. return retval;
  2312. }
  2313. #if DEBUG
  2314. #define ST_DEB_FORWARD 0
  2315. #define ST_DEB_BACKWARD 1
  2316. static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd)
  2317. {
  2318. s32 sc;
  2319. if (!debugging)
  2320. return;
  2321. sc = sign_extend32(get_unaligned_be24(&cmd[2]), 23);
  2322. if (direction)
  2323. sc = -sc;
  2324. st_printk(ST_DEB_MSG, STp, "Spacing tape %s over %d %s.\n",
  2325. direction ? "backward" : "forward", sc, units);
  2326. }
  2327. #else
  2328. #define ST_DEB_FORWARD 0
  2329. #define ST_DEB_BACKWARD 1
  2330. static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd) {}
  2331. #endif
  2332. /* Internal ioctl function */
  2333. static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
  2334. {
  2335. int timeout;
  2336. long ltmp;
  2337. int ioctl_result;
  2338. int chg_eof = 1;
  2339. unsigned char cmd[MAX_COMMAND_SIZE];
  2340. struct st_request *SRpnt;
  2341. struct st_partstat *STps;
  2342. int fileno, blkno, at_sm, undone;
  2343. int datalen = 0, direction = DMA_NONE;
  2344. WARN_ON(STp->buffer->do_dio != 0);
  2345. if (STp->ready != ST_READY) {
  2346. if (STp->ready == ST_NO_TAPE)
  2347. return (-ENOMEDIUM);
  2348. else
  2349. return (-EIO);
  2350. }
  2351. timeout = STp->long_timeout;
  2352. STps = &(STp->ps[STp->partition]);
  2353. fileno = STps->drv_file;
  2354. blkno = STps->drv_block;
  2355. at_sm = STps->at_sm;
  2356. memset(cmd, 0, MAX_COMMAND_SIZE);
  2357. switch (cmd_in) {
  2358. case MTFSFM:
  2359. chg_eof = 0; /* Changed from the FSF after this */
  2360. fallthrough;
  2361. case MTFSF:
  2362. cmd[0] = SPACE;
  2363. cmd[1] = 0x01; /* Space FileMarks */
  2364. cmd[2] = (arg >> 16);
  2365. cmd[3] = (arg >> 8);
  2366. cmd[4] = arg;
  2367. deb_space_print(STp, ST_DEB_FORWARD, "filemarks", cmd);
  2368. if (fileno >= 0)
  2369. fileno += arg;
  2370. blkno = 0;
  2371. at_sm &= (arg == 0);
  2372. break;
  2373. case MTBSFM:
  2374. chg_eof = 0; /* Changed from the FSF after this */
  2375. fallthrough;
  2376. case MTBSF:
  2377. cmd[0] = SPACE;
  2378. cmd[1] = 0x01; /* Space FileMarks */
  2379. ltmp = (-arg);
  2380. cmd[2] = (ltmp >> 16);
  2381. cmd[3] = (ltmp >> 8);
  2382. cmd[4] = ltmp;
  2383. deb_space_print(STp, ST_DEB_BACKWARD, "filemarks", cmd);
  2384. if (fileno >= 0)
  2385. fileno -= arg;
  2386. blkno = (-1); /* We can't know the block number */
  2387. at_sm &= (arg == 0);
  2388. break;
  2389. case MTFSR:
  2390. cmd[0] = SPACE;
  2391. cmd[1] = 0x00; /* Space Blocks */
  2392. cmd[2] = (arg >> 16);
  2393. cmd[3] = (arg >> 8);
  2394. cmd[4] = arg;
  2395. deb_space_print(STp, ST_DEB_FORWARD, "blocks", cmd);
  2396. if (blkno >= 0)
  2397. blkno += arg;
  2398. at_sm &= (arg == 0);
  2399. break;
  2400. case MTBSR:
  2401. cmd[0] = SPACE;
  2402. cmd[1] = 0x00; /* Space Blocks */
  2403. ltmp = (-arg);
  2404. cmd[2] = (ltmp >> 16);
  2405. cmd[3] = (ltmp >> 8);
  2406. cmd[4] = ltmp;
  2407. deb_space_print(STp, ST_DEB_BACKWARD, "blocks", cmd);
  2408. if (blkno >= 0)
  2409. blkno -= arg;
  2410. at_sm &= (arg == 0);
  2411. break;
  2412. case MTFSS:
  2413. cmd[0] = SPACE;
  2414. cmd[1] = 0x04; /* Space Setmarks */
  2415. cmd[2] = (arg >> 16);
  2416. cmd[3] = (arg >> 8);
  2417. cmd[4] = arg;
  2418. deb_space_print(STp, ST_DEB_FORWARD, "setmarks", cmd);
  2419. if (arg != 0) {
  2420. blkno = fileno = (-1);
  2421. at_sm = 1;
  2422. }
  2423. break;
  2424. case MTBSS:
  2425. cmd[0] = SPACE;
  2426. cmd[1] = 0x04; /* Space Setmarks */
  2427. ltmp = (-arg);
  2428. cmd[2] = (ltmp >> 16);
  2429. cmd[3] = (ltmp >> 8);
  2430. cmd[4] = ltmp;
  2431. deb_space_print(STp, ST_DEB_BACKWARD, "setmarks", cmd);
  2432. if (arg != 0) {
  2433. blkno = fileno = (-1);
  2434. at_sm = 1;
  2435. }
  2436. break;
  2437. case MTWEOF:
  2438. case MTWEOFI:
  2439. case MTWSM:
  2440. if (STp->write_prot)
  2441. return (-EACCES);
  2442. cmd[0] = WRITE_FILEMARKS;
  2443. if (cmd_in == MTWSM)
  2444. cmd[1] = 2;
  2445. if (cmd_in == MTWEOFI ||
  2446. (cmd_in == MTWEOF && STp->immediate_filemark))
  2447. cmd[1] |= 1;
  2448. cmd[2] = (arg >> 16);
  2449. cmd[3] = (arg >> 8);
  2450. cmd[4] = arg;
  2451. timeout = STp->device->request_queue->rq_timeout;
  2452. DEBC(
  2453. if (cmd_in != MTWSM)
  2454. st_printk(ST_DEB_MSG, STp,
  2455. "Writing %d filemarks.\n",
  2456. cmd[2] * 65536 +
  2457. cmd[3] * 256 +
  2458. cmd[4]);
  2459. else
  2460. st_printk(ST_DEB_MSG, STp,
  2461. "Writing %d setmarks.\n",
  2462. cmd[2] * 65536 +
  2463. cmd[3] * 256 +
  2464. cmd[4]);
  2465. )
  2466. if (fileno >= 0)
  2467. fileno += arg;
  2468. blkno = 0;
  2469. at_sm = (cmd_in == MTWSM);
  2470. break;
  2471. case MTREW:
  2472. cmd[0] = REZERO_UNIT;
  2473. if (STp->immediate) {
  2474. cmd[1] = 1; /* Don't wait for completion */
  2475. timeout = STp->device->request_queue->rq_timeout;
  2476. }
  2477. DEBC_printk(STp, "Rewinding tape.\n");
  2478. fileno = blkno = at_sm = 0;
  2479. break;
  2480. case MTNOP:
  2481. DEBC_printk(STp, "No op on tape.\n");
  2482. return 0; /* Should do something ? */
  2483. break;
  2484. case MTRETEN:
  2485. cmd[0] = START_STOP;
  2486. if (STp->immediate) {
  2487. cmd[1] = 1; /* Don't wait for completion */
  2488. timeout = STp->device->request_queue->rq_timeout;
  2489. }
  2490. cmd[4] = 3;
  2491. DEBC_printk(STp, "Retensioning tape.\n");
  2492. fileno = blkno = at_sm = 0;
  2493. break;
  2494. case MTEOM:
  2495. if (!STp->fast_mteom) {
  2496. /* space to the end of tape */
  2497. ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
  2498. fileno = STps->drv_file;
  2499. if (STps->eof >= ST_EOD_1)
  2500. return 0;
  2501. /* The next lines would hide the number of spaced FileMarks
  2502. That's why I inserted the previous lines. I had no luck
  2503. with detecting EOM with FSF, so we go now to EOM.
  2504. Joerg Weule */
  2505. } else
  2506. fileno = (-1);
  2507. cmd[0] = SPACE;
  2508. cmd[1] = 3;
  2509. DEBC_printk(STp, "Spacing to end of recorded medium.\n");
  2510. blkno = -1;
  2511. at_sm = 0;
  2512. break;
  2513. case MTERASE:
  2514. if (STp->write_prot)
  2515. return (-EACCES);
  2516. cmd[0] = ERASE;
  2517. cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
  2518. if (STp->immediate) {
  2519. cmd[1] |= 2; /* Don't wait for completion */
  2520. timeout = STp->device->request_queue->rq_timeout;
  2521. }
  2522. else
  2523. timeout = STp->long_timeout * 8;
  2524. DEBC_printk(STp, "Erasing tape.\n");
  2525. fileno = blkno = at_sm = 0;
  2526. break;
  2527. case MTSETBLK: /* Set block length */
  2528. case MTSETDENSITY: /* Set tape density */
  2529. case MTSETDRVBUFFER: /* Set drive buffering */
  2530. case SET_DENS_AND_BLK: /* Set density and block size */
  2531. chg_eof = 0;
  2532. if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
  2533. return (-EIO); /* Not allowed if data in buffer */
  2534. if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
  2535. (arg & MT_ST_BLKSIZE_MASK) != 0 &&
  2536. STp->max_block > 0 &&
  2537. ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
  2538. (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
  2539. st_printk(KERN_WARNING, STp, "Illegal block size.\n");
  2540. return (-EINVAL);
  2541. }
  2542. cmd[0] = MODE_SELECT;
  2543. if ((STp->use_pf & USE_PF))
  2544. cmd[1] = MODE_SELECT_PAGE_FORMAT;
  2545. cmd[4] = datalen = 12;
  2546. direction = DMA_TO_DEVICE;
  2547. memset((STp->buffer)->b_data, 0, 12);
  2548. if (cmd_in == MTSETDRVBUFFER)
  2549. (STp->buffer)->b_data[2] = (arg & 7) << 4;
  2550. else
  2551. (STp->buffer)->b_data[2] =
  2552. STp->drv_buffer << 4;
  2553. (STp->buffer)->b_data[3] = 8; /* block descriptor length */
  2554. if (cmd_in == MTSETDENSITY) {
  2555. (STp->buffer)->b_data[4] = arg;
  2556. STp->density_changed = 1; /* At least we tried ;-) */
  2557. } else if (cmd_in == SET_DENS_AND_BLK)
  2558. (STp->buffer)->b_data[4] = arg >> 24;
  2559. else
  2560. (STp->buffer)->b_data[4] = STp->density;
  2561. if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
  2562. ltmp = arg & MT_ST_BLKSIZE_MASK;
  2563. if (cmd_in == MTSETBLK)
  2564. STp->blksize_changed = 1; /* At least we tried ;-) */
  2565. } else
  2566. ltmp = STp->block_size;
  2567. (STp->buffer)->b_data[9] = (ltmp >> 16);
  2568. (STp->buffer)->b_data[10] = (ltmp >> 8);
  2569. (STp->buffer)->b_data[11] = ltmp;
  2570. timeout = STp->device->request_queue->rq_timeout;
  2571. DEBC(
  2572. if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
  2573. st_printk(ST_DEB_MSG, STp,
  2574. "Setting block size to %d bytes.\n",
  2575. (STp->buffer)->b_data[9] * 65536 +
  2576. (STp->buffer)->b_data[10] * 256 +
  2577. (STp->buffer)->b_data[11]);
  2578. if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
  2579. st_printk(ST_DEB_MSG, STp,
  2580. "Setting density code to %x.\n",
  2581. (STp->buffer)->b_data[4]);
  2582. if (cmd_in == MTSETDRVBUFFER)
  2583. st_printk(ST_DEB_MSG, STp,
  2584. "Setting drive buffer code to %d.\n",
  2585. ((STp->buffer)->b_data[2] >> 4) & 7);
  2586. )
  2587. break;
  2588. default:
  2589. return (-ENOSYS);
  2590. }
  2591. SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
  2592. timeout, MAX_RETRIES, 1);
  2593. if (!SRpnt)
  2594. return (STp->buffer)->syscall_result;
  2595. ioctl_result = (STp->buffer)->syscall_result;
  2596. if (!ioctl_result) { /* SCSI command successful */
  2597. st_release_request(SRpnt);
  2598. SRpnt = NULL;
  2599. STps->drv_block = blkno;
  2600. STps->drv_file = fileno;
  2601. STps->at_sm = at_sm;
  2602. if (cmd_in == MTBSFM)
  2603. ioctl_result = st_int_ioctl(STp, MTFSF, 1);
  2604. else if (cmd_in == MTFSFM)
  2605. ioctl_result = st_int_ioctl(STp, MTBSF, 1);
  2606. if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
  2607. STp->block_size = arg & MT_ST_BLKSIZE_MASK;
  2608. if (STp->block_size != 0) {
  2609. (STp->buffer)->buffer_blocks =
  2610. (STp->buffer)->buffer_size / STp->block_size;
  2611. }
  2612. (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
  2613. if (cmd_in == SET_DENS_AND_BLK)
  2614. STp->density = arg >> MT_ST_DENSITY_SHIFT;
  2615. } else if (cmd_in == MTSETDRVBUFFER)
  2616. STp->drv_buffer = (arg & 7);
  2617. else if (cmd_in == MTSETDENSITY)
  2618. STp->density = arg;
  2619. if (cmd_in == MTEOM)
  2620. STps->eof = ST_EOD;
  2621. else if (cmd_in == MTFSF)
  2622. STps->eof = ST_FM;
  2623. else if (chg_eof)
  2624. STps->eof = ST_NOEOF;
  2625. if (cmd_in == MTWEOF || cmd_in == MTWEOFI)
  2626. STps->rw = ST_IDLE; /* prevent automatic WEOF at close */
  2627. } else { /* SCSI command was not completely successful. Don't return
  2628. from this block without releasing the SCSI command block! */
  2629. struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  2630. if (cmdstatp->flags & SENSE_EOM) {
  2631. if (cmd_in != MTBSF && cmd_in != MTBSFM &&
  2632. cmd_in != MTBSR && cmd_in != MTBSS)
  2633. STps->eof = ST_EOM_OK;
  2634. STps->drv_block = 0;
  2635. }
  2636. if (cmdstatp->remainder_valid)
  2637. undone = (int)cmdstatp->uremainder64;
  2638. else
  2639. undone = 0;
  2640. if ((cmd_in == MTWEOF || cmd_in == MTWEOFI) &&
  2641. cmdstatp->have_sense &&
  2642. (cmdstatp->flags & SENSE_EOM)) {
  2643. if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  2644. cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
  2645. ioctl_result = 0; /* EOF(s) written successfully at EOM */
  2646. STps->eof = ST_NOEOF;
  2647. } else { /* Writing EOF(s) failed */
  2648. if (fileno >= 0)
  2649. fileno -= undone;
  2650. if (undone < arg)
  2651. STps->eof = ST_NOEOF;
  2652. }
  2653. STps->drv_file = fileno;
  2654. } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
  2655. if (fileno >= 0)
  2656. STps->drv_file = fileno - undone;
  2657. else
  2658. STps->drv_file = fileno;
  2659. STps->drv_block = -1;
  2660. STps->eof = ST_NOEOF;
  2661. } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
  2662. if (arg > 0 && undone < 0) /* Some drives get this wrong */
  2663. undone = (-undone);
  2664. if (STps->drv_file >= 0)
  2665. STps->drv_file = fileno + undone;
  2666. STps->drv_block = 0;
  2667. STps->eof = ST_NOEOF;
  2668. } else if (cmd_in == MTFSR) {
  2669. if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
  2670. if (STps->drv_file >= 0)
  2671. STps->drv_file++;
  2672. STps->drv_block = 0;
  2673. STps->eof = ST_FM;
  2674. } else {
  2675. if (blkno >= undone)
  2676. STps->drv_block = blkno - undone;
  2677. else
  2678. STps->drv_block = (-1);
  2679. STps->eof = ST_NOEOF;
  2680. }
  2681. } else if (cmd_in == MTBSR) {
  2682. if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
  2683. STps->drv_file--;
  2684. STps->drv_block = (-1);
  2685. } else {
  2686. if (arg > 0 && undone < 0) /* Some drives get this wrong */
  2687. undone = (-undone);
  2688. if (STps->drv_block >= 0)
  2689. STps->drv_block = blkno + undone;
  2690. }
  2691. STps->eof = ST_NOEOF;
  2692. } else if (cmd_in == MTEOM) {
  2693. STps->drv_file = (-1);
  2694. STps->drv_block = (-1);
  2695. STps->eof = ST_EOD;
  2696. } else if (cmd_in == MTSETBLK ||
  2697. cmd_in == MTSETDENSITY ||
  2698. cmd_in == MTSETDRVBUFFER ||
  2699. cmd_in == SET_DENS_AND_BLK) {
  2700. if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
  2701. !(STp->use_pf & PF_TESTED)) {
  2702. /* Try the other possible state of Page Format if not
  2703. already tried */
  2704. STp->use_pf = (STp->use_pf ^ USE_PF) | PF_TESTED;
  2705. st_release_request(SRpnt);
  2706. SRpnt = NULL;
  2707. return st_int_ioctl(STp, cmd_in, arg);
  2708. }
  2709. } else if (chg_eof)
  2710. STps->eof = ST_NOEOF;
  2711. if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
  2712. STps->eof = ST_EOD;
  2713. st_release_request(SRpnt);
  2714. SRpnt = NULL;
  2715. }
  2716. return ioctl_result;
  2717. }
  2718. /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
  2719. structure. */
  2720. static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
  2721. int logical)
  2722. {
  2723. int result;
  2724. unsigned char scmd[MAX_COMMAND_SIZE];
  2725. struct st_request *SRpnt;
  2726. if (STp->ready != ST_READY)
  2727. return (-EIO);
  2728. memset(scmd, 0, MAX_COMMAND_SIZE);
  2729. if ((STp->device)->scsi_level < SCSI_2) {
  2730. scmd[0] = QFA_REQUEST_BLOCK;
  2731. scmd[4] = 3;
  2732. } else {
  2733. scmd[0] = READ_POSITION;
  2734. if (!logical && !STp->scsi2_logical)
  2735. scmd[1] = 1;
  2736. }
  2737. SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
  2738. STp->device->request_queue->rq_timeout,
  2739. MAX_READY_RETRIES, 1);
  2740. if (!SRpnt)
  2741. return (STp->buffer)->syscall_result;
  2742. if ((STp->buffer)->syscall_result != 0 ||
  2743. (STp->device->scsi_level >= SCSI_2 &&
  2744. ((STp->buffer)->b_data[0] & 4) != 0)) {
  2745. *block = *partition = 0;
  2746. DEBC_printk(STp, " Can't read tape position.\n");
  2747. result = (-EIO);
  2748. } else {
  2749. result = 0;
  2750. if ((STp->device)->scsi_level < SCSI_2) {
  2751. *block = ((STp->buffer)->b_data[0] << 16)
  2752. + ((STp->buffer)->b_data[1] << 8)
  2753. + (STp->buffer)->b_data[2];
  2754. *partition = 0;
  2755. } else {
  2756. *block = ((STp->buffer)->b_data[4] << 24)
  2757. + ((STp->buffer)->b_data[5] << 16)
  2758. + ((STp->buffer)->b_data[6] << 8)
  2759. + (STp->buffer)->b_data[7];
  2760. *partition = (STp->buffer)->b_data[1];
  2761. if (((STp->buffer)->b_data[0] & 0x80) &&
  2762. (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
  2763. STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
  2764. }
  2765. DEBC_printk(STp, "Got tape pos. blk %d part %d.\n",
  2766. *block, *partition);
  2767. }
  2768. st_release_request(SRpnt);
  2769. SRpnt = NULL;
  2770. return result;
  2771. }
  2772. /* Set the tape block and partition. Negative partition means that only the
  2773. block should be set in vendor specific way. */
  2774. static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
  2775. int logical)
  2776. {
  2777. struct st_partstat *STps;
  2778. int result, p;
  2779. unsigned int blk;
  2780. int timeout;
  2781. unsigned char scmd[MAX_COMMAND_SIZE];
  2782. struct st_request *SRpnt;
  2783. if (STp->ready != ST_READY)
  2784. return (-EIO);
  2785. timeout = STp->long_timeout;
  2786. STps = &(STp->ps[STp->partition]);
  2787. DEBC_printk(STp, "Setting block to %d and partition to %d.\n",
  2788. block, partition);
  2789. DEB(if (partition < 0)
  2790. return (-EIO); )
  2791. /* Update the location at the partition we are leaving */
  2792. if ((!STp->can_partitions && partition != 0) ||
  2793. partition >= ST_NBR_PARTITIONS)
  2794. return (-EINVAL);
  2795. if (partition != STp->partition) {
  2796. if (get_location(STp, &blk, &p, 1))
  2797. STps->last_block_valid = 0;
  2798. else {
  2799. STps->last_block_valid = 1;
  2800. STps->last_block_visited = blk;
  2801. DEBC_printk(STp, "Visited block %d for "
  2802. "partition %d saved.\n",
  2803. blk, STp->partition);
  2804. }
  2805. }
  2806. memset(scmd, 0, MAX_COMMAND_SIZE);
  2807. if ((STp->device)->scsi_level < SCSI_2) {
  2808. scmd[0] = QFA_SEEK_BLOCK;
  2809. scmd[2] = (block >> 16);
  2810. scmd[3] = (block >> 8);
  2811. scmd[4] = block;
  2812. scmd[5] = 0;
  2813. } else {
  2814. scmd[0] = SEEK_10;
  2815. scmd[3] = (block >> 24);
  2816. scmd[4] = (block >> 16);
  2817. scmd[5] = (block >> 8);
  2818. scmd[6] = block;
  2819. if (!logical && !STp->scsi2_logical)
  2820. scmd[1] = 4;
  2821. if (STp->partition != partition) {
  2822. scmd[1] |= 2;
  2823. scmd[8] = partition;
  2824. DEBC_printk(STp, "Trying to change partition "
  2825. "from %d to %d\n", STp->partition,
  2826. partition);
  2827. }
  2828. }
  2829. if (STp->immediate) {
  2830. scmd[1] |= 1; /* Don't wait for completion */
  2831. timeout = STp->device->request_queue->rq_timeout;
  2832. }
  2833. SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
  2834. timeout, MAX_READY_RETRIES, 1);
  2835. if (!SRpnt)
  2836. return (STp->buffer)->syscall_result;
  2837. STps->drv_block = STps->drv_file = (-1);
  2838. STps->eof = ST_NOEOF;
  2839. if ((STp->buffer)->syscall_result != 0) {
  2840. result = (-EIO);
  2841. if (STp->can_partitions &&
  2842. (STp->device)->scsi_level >= SCSI_2 &&
  2843. (p = find_partition(STp)) >= 0)
  2844. STp->partition = p;
  2845. } else {
  2846. if (STp->can_partitions) {
  2847. STp->partition = partition;
  2848. STps = &(STp->ps[partition]);
  2849. if (!STps->last_block_valid ||
  2850. STps->last_block_visited != block) {
  2851. STps->at_sm = 0;
  2852. STps->rw = ST_IDLE;
  2853. }
  2854. } else
  2855. STps->at_sm = 0;
  2856. if (block == 0)
  2857. STps->drv_block = STps->drv_file = 0;
  2858. result = 0;
  2859. }
  2860. st_release_request(SRpnt);
  2861. SRpnt = NULL;
  2862. return result;
  2863. }
  2864. /* Find the current partition number for the drive status. Called from open and
  2865. returns either partition number of negative error code. */
  2866. static int find_partition(struct scsi_tape *STp)
  2867. {
  2868. int i, partition;
  2869. unsigned int block;
  2870. if ((i = get_location(STp, &block, &partition, 1)) < 0)
  2871. return i;
  2872. if (partition >= ST_NBR_PARTITIONS)
  2873. return (-EIO);
  2874. return partition;
  2875. }
  2876. /* Change the partition if necessary */
  2877. static int switch_partition(struct scsi_tape *STp)
  2878. {
  2879. struct st_partstat *STps;
  2880. if (STp->partition == STp->new_partition)
  2881. return 0;
  2882. STps = &(STp->ps[STp->new_partition]);
  2883. if (!STps->last_block_valid)
  2884. STps->last_block_visited = 0;
  2885. return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
  2886. }
  2887. /* Functions for reading and writing the medium partition mode page. */
  2888. #define PART_PAGE 0x11
  2889. #define PART_PAGE_FIXED_LENGTH 8
  2890. #define PP_OFF_MAX_ADD_PARTS 2
  2891. #define PP_OFF_NBR_ADD_PARTS 3
  2892. #define PP_OFF_FLAGS 4
  2893. #define PP_OFF_PART_UNITS 6
  2894. #define PP_OFF_RESERVED 7
  2895. #define PP_BIT_IDP 0x20
  2896. #define PP_BIT_FDP 0x80
  2897. #define PP_MSK_PSUM_MB 0x10
  2898. #define PP_MSK_PSUM_UNITS 0x18
  2899. #define PP_MSK_POFM 0x04
  2900. /* Get the number of partitions on the tape. As a side effect reads the
  2901. mode page into the tape buffer. */
  2902. static int nbr_partitions(struct scsi_tape *STp)
  2903. {
  2904. int result;
  2905. if (STp->ready != ST_READY)
  2906. return (-EIO);
  2907. result = read_mode_page(STp, PART_PAGE, 1);
  2908. if (result) {
  2909. DEBC_printk(STp, "Can't read medium partition page.\n");
  2910. result = (-EIO);
  2911. } else {
  2912. result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
  2913. PP_OFF_NBR_ADD_PARTS] + 1;
  2914. DEBC_printk(STp, "Number of partitions %d.\n", result);
  2915. }
  2916. return result;
  2917. }
  2918. static int format_medium(struct scsi_tape *STp, int format)
  2919. {
  2920. int result = 0;
  2921. int timeout = STp->long_timeout;
  2922. unsigned char scmd[MAX_COMMAND_SIZE];
  2923. struct st_request *SRpnt;
  2924. memset(scmd, 0, MAX_COMMAND_SIZE);
  2925. scmd[0] = FORMAT_UNIT;
  2926. scmd[2] = format;
  2927. if (STp->immediate) {
  2928. scmd[1] |= 1; /* Don't wait for completion */
  2929. timeout = STp->device->request_queue->rq_timeout;
  2930. }
  2931. DEBC_printk(STp, "Sending FORMAT MEDIUM\n");
  2932. SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
  2933. timeout, MAX_RETRIES, 1);
  2934. if (!SRpnt)
  2935. result = STp->buffer->syscall_result;
  2936. return result;
  2937. }
  2938. /* Partition the tape into two partitions if size > 0 or one partition if
  2939. size == 0.
  2940. The block descriptors are read and written because Sony SDT-7000 does not
  2941. work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
  2942. My HP C1533A drive returns only one partition size field. This is used to
  2943. set the size of partition 1. There is no size field for the default partition.
  2944. Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
  2945. used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
  2946. The following algorithm is used to accommodate both drives: if the number of
  2947. partition size fields is greater than the maximum number of additional partitions
  2948. in the mode page, the second field is used. Otherwise the first field is used.
  2949. For Seagate DDS drives the page length must be 8 when no partitions is defined
  2950. and 10 when 1 partition is defined (information from Eric Lee Green). This is
  2951. is acceptable also to some other old drives and enforced if the first partition
  2952. size field is used for the first additional partition size.
  2953. For drives that advertize SCSI-3 or newer, use the SSC-3 methods.
  2954. */
  2955. static int partition_tape(struct scsi_tape *STp, int size)
  2956. {
  2957. int result;
  2958. int target_partition;
  2959. bool scsi3 = STp->device->scsi_level >= SCSI_3, needs_format = false;
  2960. int pgo, psd_cnt, psdo;
  2961. int psum = PP_MSK_PSUM_MB, units = 0;
  2962. unsigned char *bp;
  2963. result = read_mode_page(STp, PART_PAGE, 0);
  2964. if (result) {
  2965. DEBC_printk(STp, "Can't read partition mode page.\n");
  2966. return result;
  2967. }
  2968. target_partition = 1;
  2969. if (size < 0) {
  2970. target_partition = 0;
  2971. size = -size;
  2972. }
  2973. /* The mode page is in the buffer. Let's modify it and write it. */
  2974. bp = (STp->buffer)->b_data;
  2975. pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
  2976. DEBC_printk(STp, "Partition page length is %d bytes.\n",
  2977. bp[pgo + MP_OFF_PAGE_LENGTH] + 2);
  2978. psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
  2979. if (scsi3) {
  2980. needs_format = (bp[pgo + PP_OFF_FLAGS] & PP_MSK_POFM) != 0;
  2981. if (needs_format && size == 0) {
  2982. /* No need to write the mode page when clearing
  2983. * partitioning
  2984. */
  2985. DEBC_printk(STp, "Formatting tape with one partition.\n");
  2986. result = format_medium(STp, 0);
  2987. goto out;
  2988. }
  2989. if (needs_format) /* Leave the old value for HP DATs claiming SCSI_3 */
  2990. psd_cnt = 2;
  2991. if ((bp[pgo + PP_OFF_FLAGS] & PP_MSK_PSUM_UNITS) == PP_MSK_PSUM_UNITS) {
  2992. /* Use units scaling for large partitions if the device
  2993. * suggests it and no precision lost. Required for IBM
  2994. * TS1140/50 drives that don't support MB units.
  2995. */
  2996. if (size >= 1000 && (size % 1000) == 0) {
  2997. size /= 1000;
  2998. psum = PP_MSK_PSUM_UNITS;
  2999. units = 9; /* GB */
  3000. }
  3001. }
  3002. /* Try it anyway if too large to specify in MB */
  3003. if (psum == PP_MSK_PSUM_MB && size >= 65534) {
  3004. size /= 1000;
  3005. psum = PP_MSK_PSUM_UNITS;
  3006. units = 9; /* GB */
  3007. }
  3008. }
  3009. if (size >= 65535 || /* Does not fit into two bytes */
  3010. (target_partition == 0 && psd_cnt < 2)) {
  3011. result = -EINVAL;
  3012. goto out;
  3013. }
  3014. psdo = pgo + PART_PAGE_FIXED_LENGTH;
  3015. /* The second condition is for HP DDS which use only one partition size
  3016. * descriptor
  3017. */
  3018. if (target_partition > 0 &&
  3019. (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS] ||
  3020. bp[pgo + PP_OFF_MAX_ADD_PARTS] != 1)) {
  3021. bp[psdo] = bp[psdo + 1] = 0xff; /* Rest to partition 0 */
  3022. psdo += 2;
  3023. }
  3024. memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
  3025. DEBC_printk(STp, "psd_cnt %d, max.parts %d, nbr_parts %d\n",
  3026. psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
  3027. bp[pgo + PP_OFF_NBR_ADD_PARTS]);
  3028. if (size == 0) {
  3029. bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
  3030. if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
  3031. bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
  3032. DEBC_printk(STp, "Formatting tape with one partition.\n");
  3033. } else {
  3034. bp[psdo] = (size >> 8) & 0xff;
  3035. bp[psdo + 1] = size & 0xff;
  3036. if (target_partition == 0)
  3037. bp[psdo + 2] = bp[psdo + 3] = 0xff;
  3038. bp[pgo + 3] = 1;
  3039. if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
  3040. bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
  3041. DEBC_printk(STp,
  3042. "Formatting tape with two partitions (%i = %d MB).\n",
  3043. target_partition, units > 0 ? size * 1000 : size);
  3044. }
  3045. bp[pgo + PP_OFF_PART_UNITS] = 0;
  3046. bp[pgo + PP_OFF_RESERVED] = 0;
  3047. if (size != 1 || units != 0) {
  3048. bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | psum |
  3049. (bp[pgo + PP_OFF_FLAGS] & 0x07);
  3050. bp[pgo + PP_OFF_PART_UNITS] = units;
  3051. } else
  3052. bp[pgo + PP_OFF_FLAGS] = PP_BIT_FDP |
  3053. (bp[pgo + PP_OFF_FLAGS] & 0x1f);
  3054. bp[pgo + MP_OFF_PAGE_LENGTH] = 6 + psd_cnt * 2;
  3055. result = write_mode_page(STp, PART_PAGE, 1);
  3056. if (!result && needs_format)
  3057. result = format_medium(STp, 1);
  3058. if (result) {
  3059. st_printk(KERN_INFO, STp, "Partitioning of tape failed.\n");
  3060. result = (-EIO);
  3061. }
  3062. out:
  3063. return result;
  3064. }
  3065. /* The ioctl command */
  3066. static long st_ioctl_common(struct file *file, unsigned int cmd_in, void __user *p)
  3067. {
  3068. int i, cmd_nr, cmd_type, bt;
  3069. int retval = 0;
  3070. unsigned int blk;
  3071. struct scsi_tape *STp = file->private_data;
  3072. struct st_modedef *STm;
  3073. struct st_partstat *STps;
  3074. if (mutex_lock_interruptible(&STp->lock))
  3075. return -ERESTARTSYS;
  3076. DEB(
  3077. if (debugging && !STp->in_use) {
  3078. st_printk(ST_DEB_MSG, STp, "Incorrect device.\n");
  3079. retval = (-EIO);
  3080. goto out;
  3081. } ) /* end DEB */
  3082. STm = &(STp->modes[STp->current_mode]);
  3083. STps = &(STp->ps[STp->partition]);
  3084. /*
  3085. * If we are in the middle of error recovery, don't let anyone
  3086. * else try and use this device. Also, if error recovery fails, it
  3087. * may try and take the device offline, in which case all further
  3088. * access to the device is prohibited.
  3089. */
  3090. retval = scsi_ioctl_block_when_processing_errors(STp->device, cmd_in,
  3091. file->f_flags & O_NDELAY);
  3092. if (retval)
  3093. goto out;
  3094. cmd_type = _IOC_TYPE(cmd_in);
  3095. cmd_nr = _IOC_NR(cmd_in);
  3096. if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
  3097. struct mtop mtc;
  3098. if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
  3099. retval = (-EINVAL);
  3100. goto out;
  3101. }
  3102. i = copy_from_user(&mtc, p, sizeof(struct mtop));
  3103. if (i) {
  3104. retval = (-EFAULT);
  3105. goto out;
  3106. }
  3107. if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
  3108. st_printk(KERN_WARNING, STp,
  3109. "MTSETDRVBUFFER only allowed for root.\n");
  3110. retval = (-EPERM);
  3111. goto out;
  3112. }
  3113. if (!STm->defined &&
  3114. (mtc.mt_op != MTSETDRVBUFFER &&
  3115. (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
  3116. retval = (-ENXIO);
  3117. goto out;
  3118. }
  3119. if (!STp->pos_unknown) {
  3120. if (STps->eof == ST_FM_HIT) {
  3121. if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
  3122. mtc.mt_op == MTEOM) {
  3123. mtc.mt_count -= 1;
  3124. if (STps->drv_file >= 0)
  3125. STps->drv_file += 1;
  3126. } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
  3127. mtc.mt_count += 1;
  3128. if (STps->drv_file >= 0)
  3129. STps->drv_file += 1;
  3130. }
  3131. }
  3132. if (mtc.mt_op == MTSEEK) {
  3133. /* Old position must be restored if partition will be
  3134. changed */
  3135. i = !STp->can_partitions ||
  3136. (STp->new_partition != STp->partition);
  3137. } else {
  3138. i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
  3139. mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
  3140. mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
  3141. mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
  3142. mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
  3143. mtc.mt_op == MTCOMPRESSION;
  3144. }
  3145. i = flush_buffer(STp, i);
  3146. if (i < 0) {
  3147. retval = i;
  3148. goto out;
  3149. }
  3150. if (STps->rw == ST_WRITING &&
  3151. (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
  3152. mtc.mt_op == MTSEEK ||
  3153. mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
  3154. i = st_int_ioctl(STp, MTWEOF, 1);
  3155. if (i < 0) {
  3156. retval = i;
  3157. goto out;
  3158. }
  3159. if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
  3160. mtc.mt_count++;
  3161. STps->rw = ST_IDLE;
  3162. }
  3163. } else {
  3164. /*
  3165. * If there was a bus reset, block further access
  3166. * to this device. If the user wants to rewind the tape,
  3167. * then reset the flag and allow access again.
  3168. */
  3169. if (mtc.mt_op != MTREW &&
  3170. mtc.mt_op != MTOFFL &&
  3171. mtc.mt_op != MTRETEN &&
  3172. mtc.mt_op != MTERASE &&
  3173. mtc.mt_op != MTSEEK &&
  3174. mtc.mt_op != MTEOM) {
  3175. retval = (-EIO);
  3176. goto out;
  3177. }
  3178. reset_state(STp);
  3179. /* remove this when the midlevel properly clears was_reset */
  3180. STp->device->was_reset = 0;
  3181. }
  3182. if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
  3183. mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
  3184. mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
  3185. STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
  3186. if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
  3187. do_door_lock(STp, 0); /* Ignore result! */
  3188. if (mtc.mt_op == MTSETDRVBUFFER &&
  3189. (mtc.mt_count & MT_ST_OPTIONS) != 0) {
  3190. retval = st_set_options(STp, mtc.mt_count);
  3191. goto out;
  3192. }
  3193. if (mtc.mt_op == MTSETPART) {
  3194. if (!STp->can_partitions ||
  3195. mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
  3196. retval = (-EINVAL);
  3197. goto out;
  3198. }
  3199. if (mtc.mt_count >= STp->nbr_partitions &&
  3200. (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
  3201. retval = (-EIO);
  3202. goto out;
  3203. }
  3204. if (mtc.mt_count >= STp->nbr_partitions) {
  3205. retval = (-EINVAL);
  3206. goto out;
  3207. }
  3208. STp->new_partition = mtc.mt_count;
  3209. retval = 0;
  3210. goto out;
  3211. }
  3212. if (mtc.mt_op == MTMKPART) {
  3213. if (!STp->can_partitions) {
  3214. retval = (-EINVAL);
  3215. goto out;
  3216. }
  3217. i = do_load_unload(STp, file, 1);
  3218. if (i < 0) {
  3219. retval = i;
  3220. goto out;
  3221. }
  3222. i = partition_tape(STp, mtc.mt_count);
  3223. if (i < 0) {
  3224. retval = i;
  3225. goto out;
  3226. }
  3227. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  3228. STp->ps[i].rw = ST_IDLE;
  3229. STp->ps[i].at_sm = 0;
  3230. STp->ps[i].last_block_valid = 0;
  3231. }
  3232. STp->partition = STp->new_partition = 0;
  3233. STp->nbr_partitions = mtc.mt_count != 0 ? 2 : 1;
  3234. STps->drv_block = STps->drv_file = 0;
  3235. retval = 0;
  3236. goto out;
  3237. }
  3238. if (mtc.mt_op == MTSEEK) {
  3239. i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
  3240. if (!STp->can_partitions)
  3241. STp->ps[0].rw = ST_IDLE;
  3242. retval = i;
  3243. goto out;
  3244. }
  3245. if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
  3246. retval = do_load_unload(STp, file, 0);
  3247. goto out;
  3248. }
  3249. if (mtc.mt_op == MTLOAD) {
  3250. retval = do_load_unload(STp, file, max(1, mtc.mt_count));
  3251. goto out;
  3252. }
  3253. if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
  3254. retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
  3255. goto out;
  3256. }
  3257. if (STp->can_partitions && STp->ready == ST_READY &&
  3258. (i = switch_partition(STp)) < 0) {
  3259. retval = i;
  3260. goto out;
  3261. }
  3262. if (mtc.mt_op == MTCOMPRESSION)
  3263. retval = st_compression(STp, (mtc.mt_count & 1));
  3264. else
  3265. retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
  3266. goto out;
  3267. }
  3268. if (!STm->defined) {
  3269. retval = (-ENXIO);
  3270. goto out;
  3271. }
  3272. if ((i = flush_buffer(STp, 0)) < 0) {
  3273. retval = i;
  3274. goto out;
  3275. }
  3276. if (STp->can_partitions &&
  3277. (i = switch_partition(STp)) < 0) {
  3278. retval = i;
  3279. goto out;
  3280. }
  3281. if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
  3282. struct mtget mt_status;
  3283. if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
  3284. retval = (-EINVAL);
  3285. goto out;
  3286. }
  3287. mt_status.mt_type = STp->tape_type;
  3288. mt_status.mt_dsreg =
  3289. ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
  3290. ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
  3291. mt_status.mt_blkno = STps->drv_block;
  3292. mt_status.mt_fileno = STps->drv_file;
  3293. if (STp->block_size != 0) {
  3294. if (STps->rw == ST_WRITING)
  3295. mt_status.mt_blkno +=
  3296. (STp->buffer)->buffer_bytes / STp->block_size;
  3297. else if (STps->rw == ST_READING)
  3298. mt_status.mt_blkno -=
  3299. ((STp->buffer)->buffer_bytes +
  3300. STp->block_size - 1) / STp->block_size;
  3301. }
  3302. mt_status.mt_gstat = 0;
  3303. if (STp->drv_write_prot)
  3304. mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
  3305. if (mt_status.mt_blkno == 0) {
  3306. if (mt_status.mt_fileno == 0)
  3307. mt_status.mt_gstat |= GMT_BOT(0xffffffff);
  3308. else
  3309. mt_status.mt_gstat |= GMT_EOF(0xffffffff);
  3310. }
  3311. mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
  3312. mt_status.mt_resid = STp->partition;
  3313. if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
  3314. mt_status.mt_gstat |= GMT_EOT(0xffffffff);
  3315. else if (STps->eof >= ST_EOM_OK)
  3316. mt_status.mt_gstat |= GMT_EOD(0xffffffff);
  3317. if (STp->density == 1)
  3318. mt_status.mt_gstat |= GMT_D_800(0xffffffff);
  3319. else if (STp->density == 2)
  3320. mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
  3321. else if (STp->density == 3)
  3322. mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
  3323. if (STp->ready == ST_READY)
  3324. mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
  3325. if (STp->ready == ST_NO_TAPE)
  3326. mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
  3327. if (STps->at_sm)
  3328. mt_status.mt_gstat |= GMT_SM(0xffffffff);
  3329. if (STm->do_async_writes ||
  3330. (STm->do_buffer_writes && STp->block_size != 0) ||
  3331. STp->drv_buffer != 0)
  3332. mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
  3333. if (STp->cleaning_req)
  3334. mt_status.mt_gstat |= GMT_CLN(0xffffffff);
  3335. retval = put_user_mtget(p, &mt_status);
  3336. if (retval)
  3337. goto out;
  3338. STp->recover_reg = 0; /* Clear after read */
  3339. goto out;
  3340. } /* End of MTIOCGET */
  3341. if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
  3342. struct mtpos mt_pos;
  3343. if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
  3344. retval = (-EINVAL);
  3345. goto out;
  3346. }
  3347. if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
  3348. retval = i;
  3349. goto out;
  3350. }
  3351. mt_pos.mt_blkno = blk;
  3352. retval = put_user_mtpos(p, &mt_pos);
  3353. goto out;
  3354. }
  3355. mutex_unlock(&STp->lock);
  3356. switch (cmd_in) {
  3357. case SCSI_IOCTL_STOP_UNIT:
  3358. /* unload */
  3359. retval = scsi_ioctl(STp->device, cmd_in, p);
  3360. if (!retval) {
  3361. STp->rew_at_close = 0;
  3362. STp->ready = ST_NO_TAPE;
  3363. }
  3364. return retval;
  3365. case SCSI_IOCTL_GET_IDLUN:
  3366. case SCSI_IOCTL_GET_BUS_NUMBER:
  3367. break;
  3368. default:
  3369. if ((cmd_in == SG_IO ||
  3370. cmd_in == SCSI_IOCTL_SEND_COMMAND ||
  3371. cmd_in == CDROM_SEND_PACKET) &&
  3372. !capable(CAP_SYS_RAWIO))
  3373. i = -EPERM;
  3374. else
  3375. i = scsi_cmd_ioctl(STp->disk->queue, STp->disk,
  3376. file->f_mode, cmd_in, p);
  3377. if (i != -ENOTTY)
  3378. return i;
  3379. break;
  3380. }
  3381. return -ENOTTY;
  3382. out:
  3383. mutex_unlock(&STp->lock);
  3384. return retval;
  3385. }
  3386. static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
  3387. {
  3388. void __user *p = (void __user *)arg;
  3389. struct scsi_tape *STp = file->private_data;
  3390. int ret;
  3391. ret = st_ioctl_common(file, cmd_in, p);
  3392. if (ret != -ENOTTY)
  3393. return ret;
  3394. return scsi_ioctl(STp->device, cmd_in, p);
  3395. }
  3396. #ifdef CONFIG_COMPAT
  3397. static long st_compat_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
  3398. {
  3399. void __user *p = compat_ptr(arg);
  3400. struct scsi_tape *STp = file->private_data;
  3401. int ret;
  3402. /* argument conversion is handled using put_user_mtpos/put_user_mtget */
  3403. switch (cmd_in) {
  3404. case MTIOCPOS32:
  3405. return st_ioctl_common(file, MTIOCPOS, p);
  3406. case MTIOCGET32:
  3407. return st_ioctl_common(file, MTIOCGET, p);
  3408. }
  3409. ret = st_ioctl_common(file, cmd_in, p);
  3410. if (ret != -ENOTTY)
  3411. return ret;
  3412. return scsi_compat_ioctl(STp->device, cmd_in, p);
  3413. }
  3414. #endif
  3415. /* Try to allocate a new tape buffer. Calling function must not hold
  3416. dev_arr_lock. */
  3417. static struct st_buffer *new_tape_buffer(int need_dma, int max_sg)
  3418. {
  3419. struct st_buffer *tb;
  3420. tb = kzalloc(sizeof(struct st_buffer), GFP_KERNEL);
  3421. if (!tb) {
  3422. printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
  3423. return NULL;
  3424. }
  3425. tb->frp_segs = 0;
  3426. tb->use_sg = max_sg;
  3427. tb->dma = need_dma;
  3428. tb->buffer_size = 0;
  3429. tb->reserved_pages = kcalloc(max_sg, sizeof(struct page *),
  3430. GFP_KERNEL);
  3431. if (!tb->reserved_pages) {
  3432. kfree(tb);
  3433. return NULL;
  3434. }
  3435. return tb;
  3436. }
  3437. /* Try to allocate enough space in the tape buffer */
  3438. #define ST_MAX_ORDER 6
  3439. static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
  3440. {
  3441. int segs, max_segs, b_size, order, got;
  3442. gfp_t priority;
  3443. if (new_size <= STbuffer->buffer_size)
  3444. return 1;
  3445. if (STbuffer->buffer_size <= PAGE_SIZE)
  3446. normalize_buffer(STbuffer); /* Avoid extra segment */
  3447. max_segs = STbuffer->use_sg;
  3448. priority = GFP_KERNEL | __GFP_NOWARN;
  3449. if (need_dma)
  3450. priority |= GFP_DMA;
  3451. if (STbuffer->cleared)
  3452. priority |= __GFP_ZERO;
  3453. if (STbuffer->frp_segs) {
  3454. order = STbuffer->reserved_page_order;
  3455. b_size = PAGE_SIZE << order;
  3456. } else {
  3457. for (b_size = PAGE_SIZE, order = 0;
  3458. order < ST_MAX_ORDER &&
  3459. max_segs * (PAGE_SIZE << order) < new_size;
  3460. order++, b_size *= 2)
  3461. ; /* empty */
  3462. STbuffer->reserved_page_order = order;
  3463. }
  3464. if (max_segs * (PAGE_SIZE << order) < new_size) {
  3465. if (order == ST_MAX_ORDER)
  3466. return 0;
  3467. normalize_buffer(STbuffer);
  3468. return enlarge_buffer(STbuffer, new_size, need_dma);
  3469. }
  3470. for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
  3471. segs < max_segs && got < new_size;) {
  3472. struct page *page;
  3473. page = alloc_pages(priority, order);
  3474. if (!page) {
  3475. DEB(STbuffer->buffer_size = got);
  3476. normalize_buffer(STbuffer);
  3477. return 0;
  3478. }
  3479. STbuffer->frp_segs += 1;
  3480. got += b_size;
  3481. STbuffer->buffer_size = got;
  3482. STbuffer->reserved_pages[segs] = page;
  3483. segs++;
  3484. }
  3485. STbuffer->b_data = page_address(STbuffer->reserved_pages[0]);
  3486. return 1;
  3487. }
  3488. /* Make sure that no data from previous user is in the internal buffer */
  3489. static void clear_buffer(struct st_buffer * st_bp)
  3490. {
  3491. int i;
  3492. for (i=0; i < st_bp->frp_segs; i++)
  3493. memset(page_address(st_bp->reserved_pages[i]), 0,
  3494. PAGE_SIZE << st_bp->reserved_page_order);
  3495. st_bp->cleared = 1;
  3496. }
  3497. /* Release the extra buffer */
  3498. static void normalize_buffer(struct st_buffer * STbuffer)
  3499. {
  3500. int i, order = STbuffer->reserved_page_order;
  3501. for (i = 0; i < STbuffer->frp_segs; i++) {
  3502. __free_pages(STbuffer->reserved_pages[i], order);
  3503. STbuffer->buffer_size -= (PAGE_SIZE << order);
  3504. }
  3505. STbuffer->frp_segs = 0;
  3506. STbuffer->sg_segs = 0;
  3507. STbuffer->reserved_page_order = 0;
  3508. STbuffer->map_data.offset = 0;
  3509. }
  3510. /* Move data from the user buffer to the tape buffer. Returns zero (success) or
  3511. negative error code. */
  3512. static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
  3513. {
  3514. int i, cnt, res, offset;
  3515. int length = PAGE_SIZE << st_bp->reserved_page_order;
  3516. for (i = 0, offset = st_bp->buffer_bytes;
  3517. i < st_bp->frp_segs && offset >= length; i++)
  3518. offset -= length;
  3519. if (i == st_bp->frp_segs) { /* Should never happen */
  3520. printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
  3521. return (-EIO);
  3522. }
  3523. for (; i < st_bp->frp_segs && do_count > 0; i++) {
  3524. struct page *page = st_bp->reserved_pages[i];
  3525. cnt = length - offset < do_count ? length - offset : do_count;
  3526. res = copy_from_user(page_address(page) + offset, ubp, cnt);
  3527. if (res)
  3528. return (-EFAULT);
  3529. do_count -= cnt;
  3530. st_bp->buffer_bytes += cnt;
  3531. ubp += cnt;
  3532. offset = 0;
  3533. }
  3534. if (do_count) /* Should never happen */
  3535. return (-EIO);
  3536. return 0;
  3537. }
  3538. /* Move data from the tape buffer to the user buffer. Returns zero (success) or
  3539. negative error code. */
  3540. static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
  3541. {
  3542. int i, cnt, res, offset;
  3543. int length = PAGE_SIZE << st_bp->reserved_page_order;
  3544. for (i = 0, offset = st_bp->read_pointer;
  3545. i < st_bp->frp_segs && offset >= length; i++)
  3546. offset -= length;
  3547. if (i == st_bp->frp_segs) { /* Should never happen */
  3548. printk(KERN_WARNING "st: from_buffer offset overflow.\n");
  3549. return (-EIO);
  3550. }
  3551. for (; i < st_bp->frp_segs && do_count > 0; i++) {
  3552. struct page *page = st_bp->reserved_pages[i];
  3553. cnt = length - offset < do_count ? length - offset : do_count;
  3554. res = copy_to_user(ubp, page_address(page) + offset, cnt);
  3555. if (res)
  3556. return (-EFAULT);
  3557. do_count -= cnt;
  3558. st_bp->buffer_bytes -= cnt;
  3559. st_bp->read_pointer += cnt;
  3560. ubp += cnt;
  3561. offset = 0;
  3562. }
  3563. if (do_count) /* Should never happen */
  3564. return (-EIO);
  3565. return 0;
  3566. }
  3567. /* Move data towards start of buffer */
  3568. static void move_buffer_data(struct st_buffer * st_bp, int offset)
  3569. {
  3570. int src_seg, dst_seg, src_offset = 0, dst_offset;
  3571. int count, total;
  3572. int length = PAGE_SIZE << st_bp->reserved_page_order;
  3573. if (offset == 0)
  3574. return;
  3575. total=st_bp->buffer_bytes - offset;
  3576. for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
  3577. src_offset = offset;
  3578. if (src_offset < length)
  3579. break;
  3580. offset -= length;
  3581. }
  3582. st_bp->buffer_bytes = st_bp->read_pointer = total;
  3583. for (dst_seg=dst_offset=0; total > 0; ) {
  3584. struct page *dpage = st_bp->reserved_pages[dst_seg];
  3585. struct page *spage = st_bp->reserved_pages[src_seg];
  3586. count = min(length - dst_offset, length - src_offset);
  3587. memmove(page_address(dpage) + dst_offset,
  3588. page_address(spage) + src_offset, count);
  3589. src_offset += count;
  3590. if (src_offset >= length) {
  3591. src_seg++;
  3592. src_offset = 0;
  3593. }
  3594. dst_offset += count;
  3595. if (dst_offset >= length) {
  3596. dst_seg++;
  3597. dst_offset = 0;
  3598. }
  3599. total -= count;
  3600. }
  3601. }
  3602. /* Validate the options from command line or module parameters */
  3603. static void validate_options(void)
  3604. {
  3605. if (buffer_kbs > 0)
  3606. st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
  3607. if (max_sg_segs >= ST_FIRST_SG)
  3608. st_max_sg_segs = max_sg_segs;
  3609. }
  3610. #ifndef MODULE
  3611. /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
  3612. */
  3613. static int __init st_setup(char *str)
  3614. {
  3615. int i, len, ints[5];
  3616. char *stp;
  3617. stp = get_options(str, ARRAY_SIZE(ints), ints);
  3618. if (ints[0] > 0) {
  3619. for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
  3620. if (parms[i].val)
  3621. *parms[i].val = ints[i + 1];
  3622. } else {
  3623. while (stp != NULL) {
  3624. for (i = 0; i < ARRAY_SIZE(parms); i++) {
  3625. len = strlen(parms[i].name);
  3626. if (!strncmp(stp, parms[i].name, len) &&
  3627. (*(stp + len) == ':' || *(stp + len) == '=')) {
  3628. if (parms[i].val)
  3629. *parms[i].val =
  3630. simple_strtoul(stp + len + 1, NULL, 0);
  3631. else
  3632. printk(KERN_WARNING "st: Obsolete parameter %s\n",
  3633. parms[i].name);
  3634. break;
  3635. }
  3636. }
  3637. if (i >= ARRAY_SIZE(parms))
  3638. printk(KERN_WARNING "st: invalid parameter in '%s'\n",
  3639. stp);
  3640. stp = strchr(stp, ',');
  3641. if (stp)
  3642. stp++;
  3643. }
  3644. }
  3645. validate_options();
  3646. return 1;
  3647. }
  3648. __setup("st=", st_setup);
  3649. #endif
  3650. static const struct file_operations st_fops =
  3651. {
  3652. .owner = THIS_MODULE,
  3653. .read = st_read,
  3654. .write = st_write,
  3655. .unlocked_ioctl = st_ioctl,
  3656. #ifdef CONFIG_COMPAT
  3657. .compat_ioctl = st_compat_ioctl,
  3658. #endif
  3659. .open = st_open,
  3660. .flush = st_flush,
  3661. .release = st_release,
  3662. .llseek = noop_llseek,
  3663. };
  3664. static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
  3665. {
  3666. int i, error;
  3667. dev_t cdev_devno;
  3668. struct cdev *cdev;
  3669. struct device *dev;
  3670. struct st_modedef *STm = &(tape->modes[mode]);
  3671. char name[10];
  3672. int dev_num = tape->index;
  3673. cdev_devno = MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, rew));
  3674. cdev = cdev_alloc();
  3675. if (!cdev) {
  3676. pr_err("st%d: out of memory. Device not attached.\n", dev_num);
  3677. error = -ENOMEM;
  3678. goto out;
  3679. }
  3680. cdev->owner = THIS_MODULE;
  3681. cdev->ops = &st_fops;
  3682. STm->cdevs[rew] = cdev;
  3683. error = cdev_add(cdev, cdev_devno, 1);
  3684. if (error) {
  3685. pr_err("st%d: Can't add %s-rewind mode %d\n", dev_num,
  3686. rew ? "non" : "auto", mode);
  3687. pr_err("st%d: Device not attached.\n", dev_num);
  3688. goto out_free;
  3689. }
  3690. i = mode << (4 - ST_NBR_MODE_BITS);
  3691. snprintf(name, 10, "%s%s%s", rew ? "n" : "",
  3692. tape->disk->disk_name, st_formats[i]);
  3693. dev = device_create(&st_sysfs_class, &tape->device->sdev_gendev,
  3694. cdev_devno, &tape->modes[mode], "%s", name);
  3695. if (IS_ERR(dev)) {
  3696. pr_err("st%d: device_create failed\n", dev_num);
  3697. error = PTR_ERR(dev);
  3698. goto out_free;
  3699. }
  3700. STm->devs[rew] = dev;
  3701. return 0;
  3702. out_free:
  3703. cdev_del(STm->cdevs[rew]);
  3704. out:
  3705. STm->cdevs[rew] = NULL;
  3706. STm->devs[rew] = NULL;
  3707. return error;
  3708. }
  3709. static int create_cdevs(struct scsi_tape *tape)
  3710. {
  3711. int mode, error;
  3712. for (mode = 0; mode < ST_NBR_MODES; ++mode) {
  3713. error = create_one_cdev(tape, mode, 0);
  3714. if (error)
  3715. return error;
  3716. error = create_one_cdev(tape, mode, 1);
  3717. if (error)
  3718. return error;
  3719. }
  3720. return sysfs_create_link(&tape->device->sdev_gendev.kobj,
  3721. &tape->modes[0].devs[0]->kobj, "tape");
  3722. }
  3723. static void remove_cdevs(struct scsi_tape *tape)
  3724. {
  3725. int mode, rew;
  3726. sysfs_remove_link(&tape->device->sdev_gendev.kobj, "tape");
  3727. for (mode = 0; mode < ST_NBR_MODES; mode++) {
  3728. struct st_modedef *STm = &(tape->modes[mode]);
  3729. for (rew = 0; rew < 2; rew++) {
  3730. if (STm->cdevs[rew])
  3731. cdev_del(STm->cdevs[rew]);
  3732. if (STm->devs[rew])
  3733. device_unregister(STm->devs[rew]);
  3734. }
  3735. }
  3736. }
  3737. static int st_probe(struct device *dev)
  3738. {
  3739. struct scsi_device *SDp = to_scsi_device(dev);
  3740. struct gendisk *disk = NULL;
  3741. struct scsi_tape *tpnt = NULL;
  3742. struct st_modedef *STm;
  3743. struct st_partstat *STps;
  3744. struct st_buffer *buffer;
  3745. int i, error;
  3746. char *stp;
  3747. if (SDp->type != TYPE_TAPE)
  3748. return -ENODEV;
  3749. if ((stp = st_incompatible(SDp))) {
  3750. sdev_printk(KERN_INFO, SDp,
  3751. "OnStream tapes are no longer supported;\n");
  3752. sdev_printk(KERN_INFO, SDp,
  3753. "please mail to linux-scsi@vger.kernel.org.\n");
  3754. return -ENODEV;
  3755. }
  3756. scsi_autopm_get_device(SDp);
  3757. i = queue_max_segments(SDp->request_queue);
  3758. if (st_max_sg_segs < i)
  3759. i = st_max_sg_segs;
  3760. buffer = new_tape_buffer((SDp->host)->unchecked_isa_dma, i);
  3761. if (buffer == NULL) {
  3762. sdev_printk(KERN_ERR, SDp,
  3763. "st: Can't allocate new tape buffer. "
  3764. "Device not attached.\n");
  3765. goto out;
  3766. }
  3767. disk = alloc_disk(1);
  3768. if (!disk) {
  3769. sdev_printk(KERN_ERR, SDp,
  3770. "st: out of memory. Device not attached.\n");
  3771. goto out_buffer_free;
  3772. }
  3773. tpnt = kzalloc(sizeof(struct scsi_tape), GFP_KERNEL);
  3774. if (tpnt == NULL) {
  3775. sdev_printk(KERN_ERR, SDp,
  3776. "st: Can't allocate device descriptor.\n");
  3777. goto out_put_disk;
  3778. }
  3779. kref_init(&tpnt->kref);
  3780. tpnt->disk = disk;
  3781. disk->private_data = &tpnt->driver;
  3782. /* SCSI tape doesn't register this gendisk via add_disk(). Manually
  3783. * take queue reference that release_disk() expects. */
  3784. if (!blk_get_queue(SDp->request_queue))
  3785. goto out_put_disk;
  3786. disk->queue = SDp->request_queue;
  3787. tpnt->driver = &st_template;
  3788. tpnt->device = SDp;
  3789. if (SDp->scsi_level <= 2)
  3790. tpnt->tape_type = MT_ISSCSI1;
  3791. else
  3792. tpnt->tape_type = MT_ISSCSI2;
  3793. tpnt->buffer = buffer;
  3794. tpnt->buffer->last_SRpnt = NULL;
  3795. tpnt->inited = 0;
  3796. tpnt->dirty = 0;
  3797. tpnt->in_use = 0;
  3798. tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
  3799. tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
  3800. tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
  3801. tpnt->density = 0;
  3802. tpnt->do_auto_lock = ST_AUTO_LOCK;
  3803. tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
  3804. tpnt->can_partitions = 0;
  3805. tpnt->two_fm = ST_TWO_FM;
  3806. tpnt->fast_mteom = ST_FAST_MTEOM;
  3807. tpnt->scsi2_logical = ST_SCSI2LOGICAL;
  3808. tpnt->sili = ST_SILI;
  3809. tpnt->immediate = ST_NOWAIT;
  3810. tpnt->immediate_filemark = 0;
  3811. tpnt->default_drvbuffer = 0xff; /* No forced buffering */
  3812. tpnt->partition = 0;
  3813. tpnt->new_partition = 0;
  3814. tpnt->nbr_partitions = 0;
  3815. blk_queue_rq_timeout(tpnt->device->request_queue, ST_TIMEOUT);
  3816. tpnt->long_timeout = ST_LONG_TIMEOUT;
  3817. tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
  3818. for (i = 0; i < ST_NBR_MODES; i++) {
  3819. STm = &(tpnt->modes[i]);
  3820. STm->defined = 0;
  3821. STm->sysv = ST_SYSV;
  3822. STm->defaults_for_writes = 0;
  3823. STm->do_async_writes = ST_ASYNC_WRITES;
  3824. STm->do_buffer_writes = ST_BUFFER_WRITES;
  3825. STm->do_read_ahead = ST_READ_AHEAD;
  3826. STm->default_compression = ST_DONT_TOUCH;
  3827. STm->default_blksize = (-1); /* No forced size */
  3828. STm->default_density = (-1); /* No forced density */
  3829. STm->tape = tpnt;
  3830. }
  3831. for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  3832. STps = &(tpnt->ps[i]);
  3833. STps->rw = ST_IDLE;
  3834. STps->eof = ST_NOEOF;
  3835. STps->at_sm = 0;
  3836. STps->last_block_valid = 0;
  3837. STps->drv_block = (-1);
  3838. STps->drv_file = (-1);
  3839. }
  3840. tpnt->current_mode = 0;
  3841. tpnt->modes[0].defined = 1;
  3842. tpnt->density_changed = tpnt->compression_changed =
  3843. tpnt->blksize_changed = 0;
  3844. mutex_init(&tpnt->lock);
  3845. idr_preload(GFP_KERNEL);
  3846. spin_lock(&st_index_lock);
  3847. error = idr_alloc(&st_index_idr, tpnt, 0, ST_MAX_TAPES + 1, GFP_NOWAIT);
  3848. spin_unlock(&st_index_lock);
  3849. idr_preload_end();
  3850. if (error < 0) {
  3851. pr_warn("st: idr allocation failed: %d\n", error);
  3852. goto out_put_queue;
  3853. }
  3854. tpnt->index = error;
  3855. sprintf(disk->disk_name, "st%d", tpnt->index);
  3856. tpnt->stats = kzalloc(sizeof(struct scsi_tape_stats), GFP_KERNEL);
  3857. if (tpnt->stats == NULL) {
  3858. sdev_printk(KERN_ERR, SDp,
  3859. "st: Can't allocate statistics.\n");
  3860. goto out_idr_remove;
  3861. }
  3862. dev_set_drvdata(dev, tpnt);
  3863. error = create_cdevs(tpnt);
  3864. if (error)
  3865. goto out_remove_devs;
  3866. scsi_autopm_put_device(SDp);
  3867. sdev_printk(KERN_NOTICE, SDp,
  3868. "Attached scsi tape %s\n", tape_name(tpnt));
  3869. sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)\n",
  3870. tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
  3871. queue_dma_alignment(SDp->request_queue) + 1);
  3872. return 0;
  3873. out_remove_devs:
  3874. remove_cdevs(tpnt);
  3875. kfree(tpnt->stats);
  3876. out_idr_remove:
  3877. spin_lock(&st_index_lock);
  3878. idr_remove(&st_index_idr, tpnt->index);
  3879. spin_unlock(&st_index_lock);
  3880. out_put_queue:
  3881. blk_put_queue(disk->queue);
  3882. out_put_disk:
  3883. put_disk(disk);
  3884. kfree(tpnt);
  3885. out_buffer_free:
  3886. kfree(buffer);
  3887. out:
  3888. scsi_autopm_put_device(SDp);
  3889. return -ENODEV;
  3890. };
  3891. static int st_remove(struct device *dev)
  3892. {
  3893. struct scsi_tape *tpnt = dev_get_drvdata(dev);
  3894. int index = tpnt->index;
  3895. scsi_autopm_get_device(to_scsi_device(dev));
  3896. remove_cdevs(tpnt);
  3897. mutex_lock(&st_ref_mutex);
  3898. kref_put(&tpnt->kref, scsi_tape_release);
  3899. mutex_unlock(&st_ref_mutex);
  3900. spin_lock(&st_index_lock);
  3901. idr_remove(&st_index_idr, index);
  3902. spin_unlock(&st_index_lock);
  3903. return 0;
  3904. }
  3905. /**
  3906. * scsi_tape_release - Called to free the Scsi_Tape structure
  3907. * @kref: pointer to embedded kref
  3908. *
  3909. * st_ref_mutex must be held entering this routine. Because it is
  3910. * called on last put, you should always use the scsi_tape_get()
  3911. * scsi_tape_put() helpers which manipulate the semaphore directly
  3912. * and never do a direct kref_put().
  3913. **/
  3914. static void scsi_tape_release(struct kref *kref)
  3915. {
  3916. struct scsi_tape *tpnt = to_scsi_tape(kref);
  3917. struct gendisk *disk = tpnt->disk;
  3918. tpnt->device = NULL;
  3919. if (tpnt->buffer) {
  3920. normalize_buffer(tpnt->buffer);
  3921. kfree(tpnt->buffer->reserved_pages);
  3922. kfree(tpnt->buffer);
  3923. }
  3924. disk->private_data = NULL;
  3925. put_disk(disk);
  3926. kfree(tpnt->stats);
  3927. kfree(tpnt);
  3928. return;
  3929. }
  3930. static struct class st_sysfs_class = {
  3931. .name = "scsi_tape",
  3932. .dev_groups = st_dev_groups,
  3933. };
  3934. static int __init init_st(void)
  3935. {
  3936. int err;
  3937. validate_options();
  3938. printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d\n",
  3939. verstr, st_fixed_buffer_size, st_max_sg_segs);
  3940. debugging = (debug_flag > 0) ? debug_flag : NO_DEBUG;
  3941. if (debugging) {
  3942. printk(KERN_INFO "st: Debugging enabled debug_flag = %d\n",
  3943. debugging);
  3944. }
  3945. err = class_register(&st_sysfs_class);
  3946. if (err) {
  3947. pr_err("Unable register sysfs class for SCSI tapes\n");
  3948. return err;
  3949. }
  3950. err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  3951. ST_MAX_TAPE_ENTRIES, "st");
  3952. if (err) {
  3953. printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
  3954. SCSI_TAPE_MAJOR);
  3955. goto err_class;
  3956. }
  3957. err = scsi_register_driver(&st_template.gendrv);
  3958. if (err)
  3959. goto err_chrdev;
  3960. return 0;
  3961. err_chrdev:
  3962. unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  3963. ST_MAX_TAPE_ENTRIES);
  3964. err_class:
  3965. class_unregister(&st_sysfs_class);
  3966. return err;
  3967. }
  3968. static void __exit exit_st(void)
  3969. {
  3970. scsi_unregister_driver(&st_template.gendrv);
  3971. unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  3972. ST_MAX_TAPE_ENTRIES);
  3973. class_unregister(&st_sysfs_class);
  3974. idr_destroy(&st_index_idr);
  3975. printk(KERN_INFO "st: Unloaded.\n");
  3976. }
  3977. module_init(init_st);
  3978. module_exit(exit_st);
  3979. /* The sysfs driver interface. Read-only at the moment */
  3980. static ssize_t try_direct_io_show(struct device_driver *ddp, char *buf)
  3981. {
  3982. return scnprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
  3983. }
  3984. static DRIVER_ATTR_RO(try_direct_io);
  3985. static ssize_t fixed_buffer_size_show(struct device_driver *ddp, char *buf)
  3986. {
  3987. return scnprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
  3988. }
  3989. static DRIVER_ATTR_RO(fixed_buffer_size);
  3990. static ssize_t max_sg_segs_show(struct device_driver *ddp, char *buf)
  3991. {
  3992. return scnprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
  3993. }
  3994. static DRIVER_ATTR_RO(max_sg_segs);
  3995. static ssize_t version_show(struct device_driver *ddd, char *buf)
  3996. {
  3997. return scnprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
  3998. }
  3999. static DRIVER_ATTR_RO(version);
  4000. #if DEBUG
  4001. static ssize_t debug_flag_store(struct device_driver *ddp,
  4002. const char *buf, size_t count)
  4003. {
  4004. /* We only care what the first byte of the data is the rest is unused.
  4005. * if it's a '1' we turn on debug and if it's a '0' we disable it. All
  4006. * other values have -EINVAL returned if they are passed in.
  4007. */
  4008. if (count > 0) {
  4009. if (buf[0] == '0') {
  4010. debugging = NO_DEBUG;
  4011. return count;
  4012. } else if (buf[0] == '1') {
  4013. debugging = 1;
  4014. return count;
  4015. }
  4016. }
  4017. return -EINVAL;
  4018. }
  4019. static ssize_t debug_flag_show(struct device_driver *ddp, char *buf)
  4020. {
  4021. return scnprintf(buf, PAGE_SIZE, "%d\n", debugging);
  4022. }
  4023. static DRIVER_ATTR_RW(debug_flag);
  4024. #endif
  4025. static struct attribute *st_drv_attrs[] = {
  4026. &driver_attr_try_direct_io.attr,
  4027. &driver_attr_fixed_buffer_size.attr,
  4028. &driver_attr_max_sg_segs.attr,
  4029. &driver_attr_version.attr,
  4030. #if DEBUG
  4031. &driver_attr_debug_flag.attr,
  4032. #endif
  4033. NULL,
  4034. };
  4035. ATTRIBUTE_GROUPS(st_drv);
  4036. /* The sysfs simple class interface */
  4037. static ssize_t
  4038. defined_show(struct device *dev, struct device_attribute *attr, char *buf)
  4039. {
  4040. struct st_modedef *STm = dev_get_drvdata(dev);
  4041. ssize_t l = 0;
  4042. l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
  4043. return l;
  4044. }
  4045. static DEVICE_ATTR_RO(defined);
  4046. static ssize_t
  4047. default_blksize_show(struct device *dev, struct device_attribute *attr,
  4048. char *buf)
  4049. {
  4050. struct st_modedef *STm = dev_get_drvdata(dev);
  4051. ssize_t l = 0;
  4052. l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
  4053. return l;
  4054. }
  4055. static DEVICE_ATTR_RO(default_blksize);
  4056. static ssize_t
  4057. default_density_show(struct device *dev, struct device_attribute *attr,
  4058. char *buf)
  4059. {
  4060. struct st_modedef *STm = dev_get_drvdata(dev);
  4061. ssize_t l = 0;
  4062. char *fmt;
  4063. fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
  4064. l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
  4065. return l;
  4066. }
  4067. static DEVICE_ATTR_RO(default_density);
  4068. static ssize_t
  4069. default_compression_show(struct device *dev, struct device_attribute *attr,
  4070. char *buf)
  4071. {
  4072. struct st_modedef *STm = dev_get_drvdata(dev);
  4073. ssize_t l = 0;
  4074. l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
  4075. return l;
  4076. }
  4077. static DEVICE_ATTR_RO(default_compression);
  4078. static ssize_t
  4079. options_show(struct device *dev, struct device_attribute *attr, char *buf)
  4080. {
  4081. struct st_modedef *STm = dev_get_drvdata(dev);
  4082. struct scsi_tape *STp = STm->tape;
  4083. int options;
  4084. ssize_t l = 0;
  4085. options = STm->do_buffer_writes ? MT_ST_BUFFER_WRITES : 0;
  4086. options |= STm->do_async_writes ? MT_ST_ASYNC_WRITES : 0;
  4087. options |= STm->do_read_ahead ? MT_ST_READ_AHEAD : 0;
  4088. DEB( options |= debugging ? MT_ST_DEBUGGING : 0 );
  4089. options |= STp->two_fm ? MT_ST_TWO_FM : 0;
  4090. options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
  4091. options |= STm->defaults_for_writes ? MT_ST_DEF_WRITES : 0;
  4092. options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
  4093. options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
  4094. options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
  4095. options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
  4096. options |= STm->sysv ? MT_ST_SYSV : 0;
  4097. options |= STp->immediate ? MT_ST_NOWAIT : 0;
  4098. options |= STp->immediate_filemark ? MT_ST_NOWAIT_EOF : 0;
  4099. options |= STp->sili ? MT_ST_SILI : 0;
  4100. l = snprintf(buf, PAGE_SIZE, "0x%08x\n", options);
  4101. return l;
  4102. }
  4103. static DEVICE_ATTR_RO(options);
  4104. /* Support for tape stats */
  4105. /**
  4106. * read_cnt_show - return read count - count of reads made from tape drive
  4107. * @dev: struct device
  4108. * @attr: attribute structure
  4109. * @buf: buffer to return formatted data in
  4110. */
  4111. static ssize_t read_cnt_show(struct device *dev,
  4112. struct device_attribute *attr, char *buf)
  4113. {
  4114. struct st_modedef *STm = dev_get_drvdata(dev);
  4115. return sprintf(buf, "%lld",
  4116. (long long)atomic64_read(&STm->tape->stats->read_cnt));
  4117. }
  4118. static DEVICE_ATTR_RO(read_cnt);
  4119. /**
  4120. * read_byte_cnt_show - return read byte count - tape drives
  4121. * may use blocks less than 512 bytes this gives the raw byte count of
  4122. * of data read from the tape drive.
  4123. * @dev: struct device
  4124. * @attr: attribute structure
  4125. * @buf: buffer to return formatted data in
  4126. */
  4127. static ssize_t read_byte_cnt_show(struct device *dev,
  4128. struct device_attribute *attr, char *buf)
  4129. {
  4130. struct st_modedef *STm = dev_get_drvdata(dev);
  4131. return sprintf(buf, "%lld",
  4132. (long long)atomic64_read(&STm->tape->stats->read_byte_cnt));
  4133. }
  4134. static DEVICE_ATTR_RO(read_byte_cnt);
  4135. /**
  4136. * read_ns_show - return read ns - overall time spent waiting on reads in ns.
  4137. * @dev: struct device
  4138. * @attr: attribute structure
  4139. * @buf: buffer to return formatted data in
  4140. */
  4141. static ssize_t read_ns_show(struct device *dev,
  4142. struct device_attribute *attr, char *buf)
  4143. {
  4144. struct st_modedef *STm = dev_get_drvdata(dev);
  4145. return sprintf(buf, "%lld",
  4146. (long long)atomic64_read(&STm->tape->stats->tot_read_time));
  4147. }
  4148. static DEVICE_ATTR_RO(read_ns);
  4149. /**
  4150. * write_cnt_show - write count - number of user calls
  4151. * to write(2) that have written data to tape.
  4152. * @dev: struct device
  4153. * @attr: attribute structure
  4154. * @buf: buffer to return formatted data in
  4155. */
  4156. static ssize_t write_cnt_show(struct device *dev,
  4157. struct device_attribute *attr, char *buf)
  4158. {
  4159. struct st_modedef *STm = dev_get_drvdata(dev);
  4160. return sprintf(buf, "%lld",
  4161. (long long)atomic64_read(&STm->tape->stats->write_cnt));
  4162. }
  4163. static DEVICE_ATTR_RO(write_cnt);
  4164. /**
  4165. * write_byte_cnt_show - write byte count - raw count of
  4166. * bytes written to tape.
  4167. * @dev: struct device
  4168. * @attr: attribute structure
  4169. * @buf: buffer to return formatted data in
  4170. */
  4171. static ssize_t write_byte_cnt_show(struct device *dev,
  4172. struct device_attribute *attr, char *buf)
  4173. {
  4174. struct st_modedef *STm = dev_get_drvdata(dev);
  4175. return sprintf(buf, "%lld",
  4176. (long long)atomic64_read(&STm->tape->stats->write_byte_cnt));
  4177. }
  4178. static DEVICE_ATTR_RO(write_byte_cnt);
  4179. /**
  4180. * write_ns_show - write ns - number of nanoseconds waiting on write
  4181. * requests to complete.
  4182. * @dev: struct device
  4183. * @attr: attribute structure
  4184. * @buf: buffer to return formatted data in
  4185. */
  4186. static ssize_t write_ns_show(struct device *dev,
  4187. struct device_attribute *attr, char *buf)
  4188. {
  4189. struct st_modedef *STm = dev_get_drvdata(dev);
  4190. return sprintf(buf, "%lld",
  4191. (long long)atomic64_read(&STm->tape->stats->tot_write_time));
  4192. }
  4193. static DEVICE_ATTR_RO(write_ns);
  4194. /**
  4195. * in_flight_show - number of I/Os currently in flight -
  4196. * in most cases this will be either 0 or 1. It may be higher if someone
  4197. * has also issued other SCSI commands such as via an ioctl.
  4198. * @dev: struct device
  4199. * @attr: attribute structure
  4200. * @buf: buffer to return formatted data in
  4201. */
  4202. static ssize_t in_flight_show(struct device *dev,
  4203. struct device_attribute *attr, char *buf)
  4204. {
  4205. struct st_modedef *STm = dev_get_drvdata(dev);
  4206. return sprintf(buf, "%lld",
  4207. (long long)atomic64_read(&STm->tape->stats->in_flight));
  4208. }
  4209. static DEVICE_ATTR_RO(in_flight);
  4210. /**
  4211. * io_ns_show - io wait ns - this is the number of ns spent
  4212. * waiting on all I/O to complete. This includes tape movement commands
  4213. * such as rewinding, seeking to end of file or tape, it also includes
  4214. * read and write. To determine the time spent on tape movement
  4215. * subtract the read and write ns from this value.
  4216. * @dev: struct device
  4217. * @attr: attribute structure
  4218. * @buf: buffer to return formatted data in
  4219. */
  4220. static ssize_t io_ns_show(struct device *dev,
  4221. struct device_attribute *attr, char *buf)
  4222. {
  4223. struct st_modedef *STm = dev_get_drvdata(dev);
  4224. return sprintf(buf, "%lld",
  4225. (long long)atomic64_read(&STm->tape->stats->tot_io_time));
  4226. }
  4227. static DEVICE_ATTR_RO(io_ns);
  4228. /**
  4229. * other_cnt_show - other io count - this is the number of
  4230. * I/O requests other than read and write requests.
  4231. * Typically these are tape movement requests but will include driver
  4232. * tape movement. This includes only requests issued by the st driver.
  4233. * @dev: struct device
  4234. * @attr: attribute structure
  4235. * @buf: buffer to return formatted data in
  4236. */
  4237. static ssize_t other_cnt_show(struct device *dev,
  4238. struct device_attribute *attr, char *buf)
  4239. {
  4240. struct st_modedef *STm = dev_get_drvdata(dev);
  4241. return sprintf(buf, "%lld",
  4242. (long long)atomic64_read(&STm->tape->stats->other_cnt));
  4243. }
  4244. static DEVICE_ATTR_RO(other_cnt);
  4245. /**
  4246. * resid_cnt_show - A count of the number of times we get a residual
  4247. * count - this should indicate someone issuing reads larger than the
  4248. * block size on tape.
  4249. * @dev: struct device
  4250. * @attr: attribute structure
  4251. * @buf: buffer to return formatted data in
  4252. */
  4253. static ssize_t resid_cnt_show(struct device *dev,
  4254. struct device_attribute *attr, char *buf)
  4255. {
  4256. struct st_modedef *STm = dev_get_drvdata(dev);
  4257. return sprintf(buf, "%lld",
  4258. (long long)atomic64_read(&STm->tape->stats->resid_cnt));
  4259. }
  4260. static DEVICE_ATTR_RO(resid_cnt);
  4261. static struct attribute *st_dev_attrs[] = {
  4262. &dev_attr_defined.attr,
  4263. &dev_attr_default_blksize.attr,
  4264. &dev_attr_default_density.attr,
  4265. &dev_attr_default_compression.attr,
  4266. &dev_attr_options.attr,
  4267. NULL,
  4268. };
  4269. static struct attribute *st_stats_attrs[] = {
  4270. &dev_attr_read_cnt.attr,
  4271. &dev_attr_read_byte_cnt.attr,
  4272. &dev_attr_read_ns.attr,
  4273. &dev_attr_write_cnt.attr,
  4274. &dev_attr_write_byte_cnt.attr,
  4275. &dev_attr_write_ns.attr,
  4276. &dev_attr_in_flight.attr,
  4277. &dev_attr_io_ns.attr,
  4278. &dev_attr_other_cnt.attr,
  4279. &dev_attr_resid_cnt.attr,
  4280. NULL,
  4281. };
  4282. static struct attribute_group stats_group = {
  4283. .name = "stats",
  4284. .attrs = st_stats_attrs,
  4285. };
  4286. static struct attribute_group st_group = {
  4287. .attrs = st_dev_attrs,
  4288. };
  4289. static const struct attribute_group *st_dev_groups[] = {
  4290. &st_group,
  4291. &stats_group,
  4292. NULL,
  4293. };
  4294. /* The following functions may be useful for a larger audience. */
  4295. static int sgl_map_user_pages(struct st_buffer *STbp,
  4296. const unsigned int max_pages, unsigned long uaddr,
  4297. size_t count, int rw)
  4298. {
  4299. unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
  4300. unsigned long start = uaddr >> PAGE_SHIFT;
  4301. const int nr_pages = end - start;
  4302. int res, i;
  4303. struct page **pages;
  4304. struct rq_map_data *mdata = &STbp->map_data;
  4305. /* User attempted Overflow! */
  4306. if ((uaddr + count) < uaddr)
  4307. return -EINVAL;
  4308. /* Too big */
  4309. if (nr_pages > max_pages)
  4310. return -ENOMEM;
  4311. /* Hmm? */
  4312. if (count == 0)
  4313. return 0;
  4314. pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
  4315. if (pages == NULL)
  4316. return -ENOMEM;
  4317. /* Try to fault in all of the necessary pages */
  4318. /* rw==READ means read from drive, write into memory area */
  4319. res = pin_user_pages_fast(uaddr, nr_pages, rw == READ ? FOLL_WRITE : 0,
  4320. pages);
  4321. /* Errors and no page mapped should return here */
  4322. if (res < nr_pages)
  4323. goto out_unmap;
  4324. for (i=0; i < nr_pages; i++) {
  4325. /* FIXME: flush superflous for rw==READ,
  4326. * probably wrong function for rw==WRITE
  4327. */
  4328. flush_dcache_page(pages[i]);
  4329. }
  4330. mdata->offset = uaddr & ~PAGE_MASK;
  4331. STbp->mapped_pages = pages;
  4332. return nr_pages;
  4333. out_unmap:
  4334. if (res > 0) {
  4335. unpin_user_pages(pages, res);
  4336. res = 0;
  4337. }
  4338. kfree(pages);
  4339. return res;
  4340. }
  4341. /* And unmap them... */
  4342. static int sgl_unmap_user_pages(struct st_buffer *STbp,
  4343. const unsigned int nr_pages, int dirtied)
  4344. {
  4345. /* FIXME: cache flush missing for rw==READ */
  4346. unpin_user_pages_dirty_lock(STbp->mapped_pages, nr_pages, dirtied);
  4347. kfree(STbp->mapped_pages);
  4348. STbp->mapped_pages = NULL;
  4349. return 0;
  4350. }