st.c 125 KB

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