st.c 125 KB

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