st.c 122 KB

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