st.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  1. /* $OpenBSD: st.c,v 1.129 2015/06/07 19:13:27 krw Exp $ */
  2. /* $NetBSD: st.c,v 1.71 1997/02/21 23:03:49 thorpej Exp $ */
  3. /*
  4. * Copyright (c) 1994 Charles Hannum. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Charles Hannum.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Originally written by Julian Elischer (julian@tfs.com)
  33. * for TRW Financial Systems for use under the MACH(2.5) operating system.
  34. *
  35. * TRW Financial Systems, in accordance with their agreement with Carnegie
  36. * Mellon University, makes this software available to CMU to distribute
  37. * or use in any manner that they see fit as long as this message is kept with
  38. * the software. For this reason TFS also grants any other persons or
  39. * organisations permission to use or modify this software.
  40. *
  41. * TFS supplies this software to be publicly redistributed
  42. * on the understanding that TFS is not responsible for the correct
  43. * functioning of this software in any circumstances.
  44. *
  45. * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
  46. * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
  47. */
  48. /*
  49. * To do:
  50. * work out some better way of guessing what a good timeout is going
  51. * to be depending on whether we expect to retension or not.
  52. */
  53. #include <sys/types.h>
  54. #include <sys/param.h>
  55. #include <sys/systm.h>
  56. #include <sys/timeout.h>
  57. #include <sys/fcntl.h>
  58. #include <sys/errno.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/stat.h>
  61. #include <sys/pool.h>
  62. #include <sys/buf.h>
  63. #include <sys/mtio.h>
  64. #include <sys/device.h>
  65. #include <sys/conf.h>
  66. #include <sys/vnode.h>
  67. #include <scsi/scsi_all.h>
  68. #include <scsi/scsi_tape.h>
  69. #include <scsi/scsiconf.h>
  70. /* Defines for device specific stuff */
  71. #define DEF_FIXED_BSIZE 512
  72. #define STMODE(z) ( minor(z) & 0x03)
  73. #define STUNIT(z) ((minor(z) >> 4) )
  74. #define STMINOR(unit, mode) (((unit) << 4) + (mode))
  75. #define MAXSTMODES 16
  76. #define ST_IO_TIME (3 * 60 * 1000) /* 3 minutes */
  77. #define ST_CTL_TIME (30 * 1000) /* 30 seconds */
  78. #define ST_SPC_TIME (4 * 60 * 60 * 1000) /* 4 hours */
  79. /*
  80. * Maximum density code allowed in SCSI spec (SSC2R08f, Section 8.3).
  81. */
  82. #define SCSI_MAX_DENSITY_CODE 0xff
  83. /*
  84. * Define various devices that we know mis-behave in some way,
  85. * and note how they are bad, so we can correct for them
  86. */
  87. struct modes {
  88. u_int quirks; /* same definitions as in quirkdata */
  89. int blksize;
  90. u_int8_t density;
  91. };
  92. struct quirkdata {
  93. u_int quirks;
  94. #define ST_Q_FORCE_BLKSIZE 0x0001
  95. #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */
  96. #define ST_Q_IGNORE_LOADS 0x0004
  97. #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */
  98. #define ST_Q_UNIMODAL 0x0010 /* unimode drive rejects mode select */
  99. struct modes modes;
  100. };
  101. struct st_quirk_inquiry_pattern {
  102. struct scsi_inquiry_pattern pattern;
  103. struct quirkdata quirkdata;
  104. };
  105. const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
  106. {{T_SEQUENTIAL, T_REMOV,
  107. " ", " ", " "}, {0,
  108. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  109. {{T_SEQUENTIAL, T_REMOV,
  110. "TANDBERG", " TDC 3600 ", ""}, {0,
  111. {0, 0, 0}}}, /* minor 0-3 */
  112. {{T_SEQUENTIAL, T_REMOV,
  113. "TANDBERG", " TDC 3800 ", ""}, {0,
  114. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  115. /*
  116. * At least -005 and -007 need this. I'll assume they all do unless I
  117. * hear otherwise. - mycroft, 31MAR1994
  118. */
  119. {{T_SEQUENTIAL, T_REMOV,
  120. "ARCHIVE ", "VIPER 2525 25462", ""}, {0,
  121. {ST_Q_SENSE_HELP, 0, 0}}}, /* minor 0-3 */
  122. /*
  123. * One user reports that this works for his tape drive. It probably
  124. * needs more work. - mycroft, 09APR1994
  125. */
  126. {{T_SEQUENTIAL, T_REMOV,
  127. "SANKYO ", "CP525 ", ""}, {0,
  128. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  129. {{T_SEQUENTIAL, T_REMOV,
  130. "ANRITSU ", "DMT780 ", ""}, {0,
  131. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  132. {{T_SEQUENTIAL, T_REMOV,
  133. "ARCHIVE ", "VIPER 150 21247", ""}, {0,
  134. {0, 0, 0}}}, /* minor 0-3 */
  135. {{T_SEQUENTIAL, T_REMOV,
  136. "ARCHIVE ", "VIPER 150 21531", ""}, {0,
  137. {ST_Q_SENSE_HELP, 0, 0}}}, /* minor 0-3 */
  138. {{T_SEQUENTIAL, T_REMOV,
  139. "WANGTEK ", "5099ES SCSI", ""}, {0,
  140. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  141. {{T_SEQUENTIAL, T_REMOV,
  142. "WANGTEK ", "5150ES SCSI", ""}, {0,
  143. {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */
  144. {{T_SEQUENTIAL, T_REMOV,
  145. "WANGTEK ", "5525ES SCSI REV7", ""}, {0,
  146. {0, 0, 0}}}, /* minor 0-3 */
  147. {{T_SEQUENTIAL, T_REMOV,
  148. "WangDAT ", "Model 1300 ", ""}, {0,
  149. {0, 0, 0}}}, /* minor 0-3 */
  150. {{T_SEQUENTIAL, T_REMOV,
  151. "EXABYTE ", "EXB-8200 ", "263H"}, {0,
  152. {0, 0, 0}}}, /* minor 0-3 */
  153. {{T_SEQUENTIAL, T_REMOV,
  154. "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL,
  155. {0, 0, QIC_3095}}}, /* minor 0-3 */
  156. #if 0
  157. {{T_SEQUENTIAL, T_REMOV,
  158. "EXABYTE ", "EXB-8200 ", ""}, {0,
  159. {0, 0, 0}}}, /* minor 0-3 */
  160. #endif
  161. {{T_SEQUENTIAL, T_REMOV,
  162. "WANGTEK ", "5150ES SCSI FA15\0""01 A", "????"}, {0,
  163. {ST_Q_IGNORE_LOADS, 0, 0}}}, /* minor 0-3 */
  164. {{T_SEQUENTIAL, T_REMOV,
  165. "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS,
  166. {0, 0, 0}}}, /* minor 0-3 */
  167. };
  168. #define NOEJECT 0
  169. #define EJECT 1
  170. #define NOREWIND 0
  171. #define DOREWIND 1
  172. struct st_softc {
  173. struct device sc_dev;
  174. int flags; /* see below */
  175. u_int quirks; /* quirks for the open mode */
  176. int blksize; /* blksize we are using */
  177. u_int8_t density; /* present density */
  178. short mt_resid; /* last (short) resid */
  179. short mt_erreg; /* last error (sense key) seen */
  180. struct scsi_link *sc_link; /* our link to the adpter etc. */
  181. int blkmin; /* min blk size */
  182. int blkmax; /* max blk size */
  183. const struct quirkdata *quirkdata; /* if we have a rogue entry */
  184. u_int64_t numblks; /* nominal blocks capacity */
  185. u_int32_t media_blksize; /* 0 if not ST_FIXEDBLOCKS */
  186. u_int32_t media_density; /* this is what it said when asked */
  187. int media_fileno; /* relative to BOT. -1 means unknown. */
  188. int media_blkno; /* relative to BOF. -1 means unknown. */
  189. int media_eom; /* relative to BOT. -1 means unknown. */
  190. u_int drive_quirks; /* quirks of this drive */
  191. struct modes modes; /* plus more for each mode */
  192. u_int8_t modeflags; /* flags for the modes */
  193. #define DENSITY_SET_BY_USER 0x01
  194. #define DENSITY_SET_BY_QUIRK 0x02
  195. #define BLKSIZE_SET_BY_USER 0x04
  196. #define BLKSIZE_SET_BY_QUIRK 0x08
  197. struct bufq sc_bufq;
  198. struct timeout sc_timeout;
  199. struct scsi_xshandler sc_xsh;
  200. };
  201. int stmatch(struct device *, void *, void *);
  202. void stattach(struct device *, struct device *, void *);
  203. int stactivate(struct device *, int);
  204. int stdetach(struct device *, int);
  205. void stminphys(struct buf *);
  206. void st_identify_drive(struct st_softc *, struct scsi_inquiry_data *);
  207. void st_loadquirks(struct st_softc *);
  208. int st_mount_tape(dev_t, int);
  209. void st_unmount(struct st_softc *, int, int);
  210. int st_decide_mode(struct st_softc *, int);
  211. void ststart(struct scsi_xfer *);
  212. void st_buf_done(struct scsi_xfer *);
  213. int st_read(struct st_softc *, char *, int, int);
  214. int st_read_block_limits(struct st_softc *, int);
  215. int st_mode_sense(struct st_softc *, int);
  216. int st_mode_select(struct st_softc *, int);
  217. int st_space(struct st_softc *, int, u_int, int);
  218. int st_write_filemarks(struct st_softc *, int, int);
  219. int st_check_eod(struct st_softc *, int, int *, int);
  220. int st_load(struct st_softc *, u_int, int);
  221. int st_rewind(struct st_softc *, u_int, int);
  222. int st_interpret_sense(struct scsi_xfer *);
  223. int st_touch_tape(struct st_softc *);
  224. int st_erase(struct st_softc *, int, int);
  225. struct cfattach st_ca = {
  226. sizeof(struct st_softc), stmatch, stattach,
  227. stdetach, stactivate
  228. };
  229. struct cfdriver st_cd = {
  230. NULL, "st", DV_TAPE
  231. };
  232. #define ST_INFO_VALID 0x0001
  233. #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */
  234. #define ST_WRITTEN 0x0004 /* data have been written, EOD needed */
  235. #define ST_FIXEDBLOCKS 0x0008
  236. #define ST_AT_FILEMARK 0x0010
  237. #define ST_EIO_PENDING 0x0020 /* we couldn't report it then (had data) */
  238. #define ST_EOM_PENDING 0x0040 /* we couldn't report it then (had data) */
  239. #define ST_EOD_DETECTED 0x0080
  240. #define ST_FM_WRITTEN 0x0100 /*
  241. * EOF file mark written -- used with
  242. * ~ST_WRITTEN to indicate that multiple file
  243. * marks have been written
  244. */
  245. #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */
  246. #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */
  247. #define ST_MOUNTED 0x0800 /* Device is presently mounted */
  248. #define ST_DONTBUFFER 0x1000 /* Disable buffering/caching */
  249. #define ST_WAITING 0x2000
  250. #define ST_DYING 0x4000 /* dying, when deactivated */
  251. #define ST_BOD_DETECTED 0x8000
  252. #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \
  253. ST_BLANK_READ)
  254. #define stlookup(unit) (struct st_softc *)device_lookup(&st_cd, (unit))
  255. const struct scsi_inquiry_pattern st_patterns[] = {
  256. {T_SEQUENTIAL, T_REMOV,
  257. "", "", ""},
  258. };
  259. int
  260. stmatch(struct device *parent, void *match, void *aux)
  261. {
  262. struct scsi_attach_args *sa = aux;
  263. int priority;
  264. (void)scsi_inqmatch(sa->sa_inqbuf,
  265. st_patterns, nitems(st_patterns),
  266. sizeof(st_patterns[0]), &priority);
  267. return (priority);
  268. }
  269. /*
  270. * The routine called by the low level scsi routine when it discovers
  271. * A device suitable for this driver
  272. */
  273. void
  274. stattach(struct device *parent, struct device *self, void *aux)
  275. {
  276. struct st_softc *st = (void *)self;
  277. struct scsi_attach_args *sa = aux;
  278. struct scsi_link *sc_link = sa->sa_sc_link;
  279. SC_DEBUG(sc_link, SDEV_DB2, ("stattach:\n"));
  280. /*
  281. * Store information needed to contact our base driver
  282. */
  283. st->sc_link = sc_link;
  284. sc_link->interpret_sense = st_interpret_sense;
  285. sc_link->device_softc = st;
  286. /*
  287. * Check if the drive is a known criminal and take
  288. * Any steps needed to bring it into line
  289. */
  290. st_identify_drive(st, sa->sa_inqbuf);
  291. printf("\n");
  292. scsi_xsh_set(&st->sc_xsh, sc_link, ststart);
  293. timeout_set(&st->sc_timeout, (void (*)(void *))scsi_xsh_set,
  294. &st->sc_xsh);
  295. /* Set up the buf queue for this device. */
  296. bufq_init(&st->sc_bufq, BUFQ_FIFO);
  297. /* Start up with media position unknown. */
  298. st->media_fileno = -1;
  299. st->media_blkno = -1;
  300. st->media_eom = -1;
  301. /*
  302. * Reset the media loaded flag, sometimes the data
  303. * acquired at boot time is not quite accurate. This
  304. * will be checked again at the first open.
  305. */
  306. sc_link->flags &= ~SDEV_MEDIA_LOADED;
  307. }
  308. int
  309. stactivate(struct device *self, int act)
  310. {
  311. struct st_softc *st = (struct st_softc *)self;
  312. int rv = 0;
  313. switch (act) {
  314. case DVACT_DEACTIVATE:
  315. st->flags |= ST_DYING;
  316. scsi_xsh_del(&st->sc_xsh);
  317. break;
  318. }
  319. return (rv);
  320. }
  321. int
  322. stdetach(struct device *self, int flags)
  323. {
  324. struct st_softc *st = (struct st_softc *)self;
  325. int bmaj, cmaj, mn;
  326. bufq_drain(&st->sc_bufq);
  327. /* Locate the lowest minor number to be detached. */
  328. mn = STMINOR(self->dv_unit, 0);
  329. for (bmaj = 0; bmaj < nblkdev; bmaj++)
  330. if (bdevsw[bmaj].d_open == stopen)
  331. vdevgone(bmaj, mn, mn + MAXSTMODES - 1, VBLK);
  332. for (cmaj = 0; cmaj < nchrdev; cmaj++)
  333. if (cdevsw[cmaj].d_open == stopen)
  334. vdevgone(cmaj, mn, mn + MAXSTMODES - 1, VCHR);
  335. bufq_destroy(&st->sc_bufq);
  336. return (0);
  337. }
  338. /*
  339. * Use the inquiry routine in 'scsi_base' to get drive info so we can
  340. * Further tailor our behaviour.
  341. */
  342. void
  343. st_identify_drive(struct st_softc *st, struct scsi_inquiry_data *inqbuf)
  344. {
  345. const struct st_quirk_inquiry_pattern *finger;
  346. int priority;
  347. finger = (const struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf,
  348. st_quirk_patterns,
  349. nitems(st_quirk_patterns),
  350. sizeof(st_quirk_patterns[0]), &priority);
  351. if (priority != 0) {
  352. st->quirkdata = &finger->quirkdata;
  353. st->drive_quirks = finger->quirkdata.quirks;
  354. st->quirks = finger->quirkdata.quirks; /* start value */
  355. st_loadquirks(st);
  356. }
  357. }
  358. /*
  359. * initialise the subdevices to the default (QUIRK) state.
  360. * this will remove any setting made by the system operator or previous
  361. * operations.
  362. */
  363. void
  364. st_loadquirks(struct st_softc *st)
  365. {
  366. const struct modes *mode;
  367. struct modes *mode2;
  368. mode = &st->quirkdata->modes;
  369. mode2 = &st->modes;
  370. bzero(mode2, sizeof(struct modes));
  371. st->modeflags &= ~(BLKSIZE_SET_BY_QUIRK |
  372. DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
  373. DENSITY_SET_BY_USER);
  374. if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
  375. mode2->blksize = mode->blksize;
  376. st->modeflags |= BLKSIZE_SET_BY_QUIRK;
  377. }
  378. if (mode->density) {
  379. mode2->density = mode->density;
  380. st->modeflags |= DENSITY_SET_BY_QUIRK;
  381. }
  382. }
  383. /*
  384. * open the device.
  385. */
  386. int
  387. stopen(dev_t dev, int flags, int fmt, struct proc *p)
  388. {
  389. struct scsi_link *sc_link;
  390. struct st_softc *st;
  391. int error = 0;
  392. st = stlookup(STUNIT(dev));
  393. if (st == NULL)
  394. return (ENXIO);
  395. sc_link = st->sc_link;
  396. if (st->flags & ST_DYING) {
  397. error = ENXIO;
  398. goto done;
  399. }
  400. if (ISSET(flags, FWRITE) && ISSET(sc_link->flags, SDEV_READONLY)) {
  401. error = EACCES;
  402. goto done;
  403. }
  404. SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
  405. STUNIT(dev), st_cd.cd_ndevs));
  406. /*
  407. * Tape is an exclusive media. Only one open at a time.
  408. */
  409. if (sc_link->flags & SDEV_OPEN) {
  410. SC_DEBUG(sc_link, SDEV_DB4, ("already open\n"));
  411. error = EBUSY;
  412. goto done;
  413. }
  414. /* Use st_interpret_sense() now. */
  415. sc_link->flags |= SDEV_OPEN;
  416. /*
  417. * Check the unit status. This clears any outstanding errors and
  418. * will ensure that media is present.
  419. */
  420. error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES,
  421. SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE |
  422. SCSI_IGNORE_ILLEGAL_REQUEST);
  423. /*
  424. * Terminate any exising mount session if there is no media.
  425. */
  426. if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0)
  427. st_unmount(st, NOEJECT, DOREWIND);
  428. if (error) {
  429. sc_link->flags &= ~SDEV_OPEN;
  430. goto done;
  431. }
  432. if ((st->flags & ST_MOUNTED) == 0) {
  433. error = st_mount_tape(dev, flags);
  434. if (error) {
  435. sc_link->flags &= ~SDEV_OPEN;
  436. goto done;
  437. }
  438. }
  439. /*
  440. * Make sure that a tape opened in write-only mode will have
  441. * file marks written on it when closed, even if not written to.
  442. * This is for SUN compatibility
  443. */
  444. if ((flags & O_ACCMODE) == FWRITE)
  445. st->flags |= ST_WRITTEN;
  446. done:
  447. SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
  448. device_unref(&st->sc_dev);
  449. return (error);
  450. }
  451. /*
  452. * close the device.. only called if we are the LAST
  453. * occurrence of an open device
  454. */
  455. int
  456. stclose(dev_t dev, int flags, int mode, struct proc *p)
  457. {
  458. struct scsi_link *sc_link;
  459. struct st_softc *st;
  460. int error = 0;
  461. st = stlookup(STUNIT(dev));
  462. if (st == NULL)
  463. return (ENXIO);
  464. if (st->flags & ST_DYING) {
  465. error = ENXIO;
  466. goto done;
  467. }
  468. sc_link = st->sc_link;
  469. SC_DEBUG(sc_link, SDEV_DB1, ("closing\n"));
  470. if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
  471. st_write_filemarks(st, 1, 0);
  472. switch (STMODE(dev)) {
  473. case 0: /* normal */
  474. st_unmount(st, NOEJECT, DOREWIND);
  475. break;
  476. case 3: /* eject, no rewind */
  477. st_unmount(st, EJECT, NOREWIND);
  478. break;
  479. case 1: /* no rewind */
  480. /* leave mounted unless media seems to have been removed */
  481. if (!(sc_link->flags & SDEV_MEDIA_LOADED))
  482. st_unmount(st, NOEJECT, NOREWIND);
  483. break;
  484. case 2: /* rewind, eject */
  485. st_unmount(st, EJECT, DOREWIND);
  486. break;
  487. }
  488. sc_link->flags &= ~SDEV_OPEN;
  489. timeout_del(&st->sc_timeout);
  490. scsi_xsh_del(&st->sc_xsh);
  491. done:
  492. device_unref(&st->sc_dev);
  493. return (error);
  494. }
  495. /*
  496. * Start a new mount session.
  497. * Copy in all the default parameters from the selected device mode.
  498. * and try guess any that seem to be defaulted.
  499. */
  500. int
  501. st_mount_tape(dev_t dev, int flags)
  502. {
  503. struct st_softc *st;
  504. struct scsi_link *sc_link;
  505. int error = 0;
  506. st = stlookup(STUNIT(dev));
  507. if (st == NULL)
  508. return (ENXIO);
  509. if (st->flags & ST_DYING) {
  510. error = ENXIO;
  511. goto done;
  512. }
  513. sc_link = st->sc_link;
  514. SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n"));
  515. if (st->flags & ST_MOUNTED)
  516. goto done;
  517. st->quirks = st->drive_quirks | st->modes.quirks;
  518. /*
  519. * If the media is new, then make sure we give it a chance to
  520. * to do a 'load' instruction. (We assume it is new.)
  521. */
  522. if ((error = st_load(st, LD_LOAD, 0)) != 0)
  523. goto done;
  524. /*
  525. * Throw another dummy instruction to catch
  526. * 'Unit attention' errors. Some drives appear to give
  527. * these after doing a Load instruction.
  528. * (noteably some DAT drives)
  529. */
  530. /* XXX */
  531. scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, SCSI_SILENT);
  532. /*
  533. * Some devices can't tell you much until they have been
  534. * asked to look at the media. This quirk does this.
  535. */
  536. if (st->quirks & ST_Q_SENSE_HELP)
  537. if ((error = st_touch_tape(st)) != 0)
  538. return error;
  539. /*
  540. * Load the physical device parameters
  541. * loads: blkmin, blkmax
  542. */
  543. if (!(sc_link->flags & SDEV_ATAPI) &&
  544. (error = st_read_block_limits(st, 0)) != 0)
  545. goto done;
  546. /*
  547. * Load the media dependent parameters
  548. * includes: media_blksize,media_density,numblks
  549. * As we have a tape in, it should be reflected here.
  550. * If not you may need the "quirk" above.
  551. */
  552. if ((error = st_mode_sense(st, 0)) != 0)
  553. goto done;
  554. /*
  555. * If we have gained a permanent density from somewhere,
  556. * then use it in preference to the one supplied by
  557. * default by the driver.
  558. */
  559. if (st->modeflags & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
  560. st->density = st->modes.density;
  561. else
  562. st->density = st->media_density;
  563. /*
  564. * If we have gained a permanent blocksize
  565. * then use it in preference to the one supplied by
  566. * default by the driver.
  567. */
  568. st->flags &= ~ST_FIXEDBLOCKS;
  569. if (st->modeflags & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
  570. st->blksize = st->modes.blksize;
  571. if (st->blksize)
  572. st->flags |= ST_FIXEDBLOCKS;
  573. } else {
  574. if ((error = st_decide_mode(st, 0)) != 0)
  575. goto done;
  576. }
  577. if ((error = st_mode_select(st, 0)) != 0) {
  578. printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
  579. goto done;
  580. }
  581. scsi_prevent(sc_link, PR_PREVENT,
  582. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
  583. st->flags |= ST_MOUNTED;
  584. sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */
  585. st->media_fileno = 0;
  586. st->media_blkno = 0;
  587. st->media_eom = -1;
  588. done:
  589. device_unref(&st->sc_dev);
  590. return (error);
  591. }
  592. /*
  593. * End the present mount session.
  594. * Rewind, and optionally eject the tape.
  595. * Reset various flags to indicate that all new
  596. * operations require another mount operation
  597. */
  598. void
  599. st_unmount(struct st_softc *st, int eject, int rewind)
  600. {
  601. struct scsi_link *sc_link = st->sc_link;
  602. int nmarks;
  603. st->media_fileno = -1;
  604. st->media_blkno = -1;
  605. if (!(st->flags & ST_MOUNTED))
  606. return;
  607. SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
  608. st_check_eod(st, 0, &nmarks, SCSI_IGNORE_NOT_READY);
  609. if (rewind)
  610. st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
  611. scsi_prevent(sc_link, PR_ALLOW,
  612. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
  613. if (eject)
  614. st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
  615. st->flags &= ~ST_MOUNTED;
  616. sc_link->flags &= ~SDEV_MEDIA_LOADED;
  617. }
  618. /*
  619. * Given all we know about the device, media, mode, 'quirks' and
  620. * initial operation, make a decision as to how we should be set
  621. * to run (regarding blocking and EOD marks)
  622. */
  623. int
  624. st_decide_mode(struct st_softc *st, int first_read)
  625. {
  626. struct scsi_link *sc_link = st->sc_link;
  627. SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
  628. /* ATAPI tapes are always fixed blocksize. */
  629. if (sc_link->flags & SDEV_ATAPI) {
  630. st->flags |= ST_FIXEDBLOCKS;
  631. if (st->media_blksize > 0)
  632. st->blksize = st->media_blksize;
  633. else
  634. st->blksize = DEF_FIXED_BSIZE;
  635. goto done;
  636. }
  637. /*
  638. * If the drive can only handle fixed-length blocks and only at
  639. * one size, perhaps we should just do that.
  640. */
  641. if (st->blkmin && (st->blkmin == st->blkmax)) {
  642. st->flags |= ST_FIXEDBLOCKS;
  643. st->blksize = st->blkmin;
  644. SC_DEBUG(sc_link, SDEV_DB3,
  645. ("blkmin == blkmax of %d\n", st->blkmin));
  646. goto done;
  647. }
  648. /*
  649. * If the tape density mandates (or even suggests) use of fixed
  650. * or variable-length blocks, comply.
  651. */
  652. switch (st->density) {
  653. case HALFINCH_800:
  654. case HALFINCH_1600:
  655. case HALFINCH_6250:
  656. case DDS:
  657. st->flags &= ~ST_FIXEDBLOCKS;
  658. st->blksize = 0;
  659. SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
  660. goto done;
  661. case QIC_11:
  662. case QIC_24:
  663. case QIC_120:
  664. case QIC_150:
  665. case QIC_525:
  666. case QIC_1320:
  667. st->flags |= ST_FIXEDBLOCKS;
  668. if (st->media_blksize > 0)
  669. st->blksize = st->media_blksize;
  670. else
  671. st->blksize = DEF_FIXED_BSIZE;
  672. SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
  673. goto done;
  674. }
  675. /*
  676. * If we're about to read the tape, perhaps we should choose
  677. * fixed or variable-length blocks and block size according to
  678. * what the drive found on the tape.
  679. */
  680. if (first_read &&
  681. (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
  682. (st->media_blksize == DEF_FIXED_BSIZE) ||
  683. (st->media_blksize == 1024))) {
  684. if (st->media_blksize > 0)
  685. st->flags |= ST_FIXEDBLOCKS;
  686. else
  687. st->flags &= ~ST_FIXEDBLOCKS;
  688. st->blksize = st->media_blksize;
  689. SC_DEBUG(sc_link, SDEV_DB3,
  690. ("Used media_blksize of %d\n", st->media_blksize));
  691. goto done;
  692. }
  693. /*
  694. * We're getting no hints from any direction. Choose variable-
  695. * length blocks arbitrarily.
  696. */
  697. st->flags &= ~ST_FIXEDBLOCKS;
  698. st->blksize = 0;
  699. SC_DEBUG(sc_link, SDEV_DB3,
  700. ("Give up and default to variable mode\n"));
  701. done:
  702. /*
  703. * Decide whether or not to write two file marks to signify end-
  704. * of-data. Make the decision as a function of density. If
  705. * the decision is not to use a second file mark, the SCSI BLANK
  706. * CHECK condition code will be recognized as end-of-data when
  707. * first read.
  708. * (I think this should be a by-product of fixed/variable..julian)
  709. */
  710. switch (st->density) {
  711. /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
  712. case QIC_11:
  713. case QIC_24:
  714. case QIC_120:
  715. case QIC_150:
  716. case QIC_525:
  717. case QIC_1320:
  718. st->flags &= ~ST_2FM_AT_EOD;
  719. break;
  720. default:
  721. st->flags |= ST_2FM_AT_EOD;
  722. }
  723. return 0;
  724. }
  725. /*
  726. * Actually translate the requested transfer into
  727. * one the physical driver can understand
  728. * The transfer is described by a buf and will include
  729. * only one physical transfer.
  730. */
  731. void
  732. ststrategy(struct buf *bp)
  733. {
  734. struct scsi_link *sc_link;
  735. struct st_softc *st;
  736. int s;
  737. st = stlookup(STUNIT(bp->b_dev));
  738. if (st == NULL) {
  739. bp->b_error = ENXIO;
  740. goto bad;
  741. }
  742. if (st->flags & ST_DYING) {
  743. bp->b_error = ENXIO;
  744. goto bad;
  745. }
  746. sc_link = st->sc_link;
  747. SC_DEBUG(sc_link, SDEV_DB2, ("ststrategy: %ld bytes @ blk %lld\n",
  748. bp->b_bcount, (long long)bp->b_blkno));
  749. /*
  750. * If it's a null transfer, return immediately.
  751. */
  752. if (bp->b_bcount == 0)
  753. goto done;
  754. /*
  755. * Odd sized request on fixed drives are verboten
  756. */
  757. if (st->flags & ST_FIXEDBLOCKS) {
  758. if (bp->b_bcount % st->blksize) {
  759. printf("%s: bad request, must be multiple of %d\n",
  760. st->sc_dev.dv_xname, st->blksize);
  761. bp->b_error = EIO;
  762. goto bad;
  763. }
  764. }
  765. /*
  766. * as are out-of-range requests on variable drives.
  767. */
  768. else if (bp->b_bcount < st->blkmin ||
  769. (st->blkmax && bp->b_bcount > st->blkmax)) {
  770. printf("%s: bad request, must be between %d and %d\n",
  771. st->sc_dev.dv_xname, st->blkmin, st->blkmax);
  772. bp->b_error = EIO;
  773. goto bad;
  774. }
  775. /*
  776. * Place it in the queue of activities for this tape
  777. * at the end (a bit silly because we only have on user..
  778. * (but it could fork()))
  779. */
  780. bufq_queue(&st->sc_bufq, bp);
  781. /*
  782. * Tell the device to get going on the transfer if it's
  783. * not doing anything, otherwise just wait for completion
  784. * (All a bit silly if we're only allowing 1 open but..)
  785. */
  786. scsi_xsh_add(&st->sc_xsh);
  787. device_unref(&st->sc_dev);
  788. return;
  789. bad:
  790. bp->b_flags |= B_ERROR;
  791. done:
  792. /* Set b_resid to indicate no xfer was done. */
  793. bp->b_resid = bp->b_bcount;
  794. s = splbio();
  795. biodone(bp);
  796. splx(s);
  797. if (st)
  798. device_unref(&st->sc_dev);
  799. }
  800. void
  801. ststart(struct scsi_xfer *xs)
  802. {
  803. struct scsi_link *sc_link = xs->sc_link;
  804. struct st_softc *st = sc_link->device_softc;
  805. struct buf *bp;
  806. struct scsi_rw_tape *cmd;
  807. int s;
  808. SC_DEBUG(sc_link, SDEV_DB2, ("ststart\n"));
  809. if (st->flags & ST_DYING) {
  810. scsi_xs_put(xs);
  811. return;
  812. }
  813. /*
  814. * if the device has been unmounted by the user
  815. * then throw away all requests until done
  816. */
  817. if (!(st->flags & ST_MOUNTED) ||
  818. !(sc_link->flags & SDEV_MEDIA_LOADED)) {
  819. /* make sure that one implies the other.. */
  820. sc_link->flags &= ~SDEV_MEDIA_LOADED;
  821. bufq_drain(&st->sc_bufq);
  822. scsi_xs_put(xs);
  823. return;
  824. }
  825. for (;;) {
  826. bp = bufq_dequeue(&st->sc_bufq);
  827. if (bp == NULL) {
  828. scsi_xs_put(xs);
  829. return;
  830. }
  831. /*
  832. * Only FIXEDBLOCK devices have pending I/O or space
  833. * operations.
  834. */
  835. if (st->flags & ST_FIXEDBLOCKS) {
  836. /*
  837. * If we are at a filemark but have not reported it yet
  838. * then we should report it now
  839. */
  840. if (st->flags & ST_AT_FILEMARK) {
  841. if ((bp->b_flags & B_READ) == B_WRITE) {
  842. /*
  843. * Handling of ST_AT_FILEMARK in
  844. * st_space will fill in the right file
  845. * mark count.
  846. * Back up over filemark
  847. */
  848. if (st_space(st, 0, SP_FILEMARKS, 0)) {
  849. bp->b_flags |= B_ERROR;
  850. bp->b_resid = bp->b_bcount;
  851. bp->b_error = EIO;
  852. s = splbio();
  853. biodone(bp);
  854. splx(s);
  855. continue;
  856. }
  857. } else {
  858. bp->b_resid = bp->b_bcount;
  859. bp->b_error = 0;
  860. bp->b_flags &= ~B_ERROR;
  861. st->flags &= ~ST_AT_FILEMARK;
  862. s = splbio();
  863. biodone(bp);
  864. splx(s);
  865. continue; /* seek more work */
  866. }
  867. }
  868. }
  869. /*
  870. * If we are at EIO or EOM but have not reported it
  871. * yet then we should report it now.
  872. */
  873. if (st->flags & (ST_EOM_PENDING | ST_EIO_PENDING)) {
  874. bp->b_resid = bp->b_bcount;
  875. if (st->flags & ST_EIO_PENDING) {
  876. bp->b_error = EIO;
  877. bp->b_flags |= B_ERROR;
  878. }
  879. st->flags &= ~(ST_EOM_PENDING | ST_EIO_PENDING);
  880. s = splbio();
  881. biodone(bp);
  882. splx(s);
  883. continue; /* seek more work */
  884. }
  885. break;
  886. }
  887. /*
  888. * Fill out the scsi command
  889. */
  890. cmd = (struct scsi_rw_tape *)xs->cmd;
  891. bzero(cmd, sizeof(*cmd));
  892. if ((bp->b_flags & B_READ) == B_WRITE) {
  893. cmd->opcode = WRITE;
  894. st->flags &= ~ST_FM_WRITTEN;
  895. st->flags |= ST_WRITTEN;
  896. xs->flags |= SCSI_DATA_OUT;
  897. } else {
  898. cmd->opcode = READ;
  899. xs->flags |= SCSI_DATA_IN;
  900. }
  901. /*
  902. * Handle "fixed-block-mode" tape drives by using the
  903. * block count instead of the length.
  904. */
  905. if (st->flags & ST_FIXEDBLOCKS) {
  906. cmd->byte2 |= SRW_FIXED;
  907. _lto3b(bp->b_bcount / st->blksize, cmd->len);
  908. } else
  909. _lto3b(bp->b_bcount, cmd->len);
  910. if (st->media_blkno != -1) {
  911. /* Update block count now, errors will set it to -1. */
  912. if (st->flags & ST_FIXEDBLOCKS)
  913. st->media_blkno += _3btol(cmd->len);
  914. else if (cmd->len != 0)
  915. st->media_blkno++;
  916. }
  917. xs->cmdlen = sizeof(*cmd);
  918. xs->timeout = ST_IO_TIME;
  919. xs->data = bp->b_data;
  920. xs->datalen = bp->b_bcount;
  921. xs->done = st_buf_done;
  922. xs->cookie = bp;
  923. xs->bp = bp;
  924. /*
  925. * go ask the adapter to do all this for us
  926. */
  927. scsi_xs_exec(xs);
  928. /*
  929. * should we try do more work now?
  930. */
  931. if (ISSET(st->flags, ST_WAITING))
  932. CLR(st->flags, ST_WAITING);
  933. else if (bufq_peek(&st->sc_bufq))
  934. scsi_xsh_add(&st->sc_xsh);
  935. }
  936. void
  937. st_buf_done(struct scsi_xfer *xs)
  938. {
  939. struct st_softc *st = xs->sc_link->device_softc;
  940. struct buf *bp = xs->cookie;
  941. int error, s;
  942. switch (xs->error) {
  943. case XS_NOERROR:
  944. bp->b_error = 0;
  945. bp->b_resid = xs->resid;
  946. break;
  947. case XS_NO_CCB:
  948. /* The adapter is busy, requeue the buf and try it later. */
  949. bufq_requeue(&st->sc_bufq, bp);
  950. scsi_xs_put(xs);
  951. SET(st->flags, ST_WAITING); /* dont let ststart xsh_add */
  952. timeout_add(&st->sc_timeout, 1);
  953. return;
  954. case XS_SENSE:
  955. case XS_SHORTSENSE:
  956. #ifdef SCSIDEBUG
  957. scsi_sense_print_debug(xs);
  958. #endif
  959. error = st_interpret_sense(xs);
  960. if (error == 0) {
  961. bp->b_error = 0;
  962. bp->b_resid = xs->resid;
  963. break;
  964. }
  965. if (error != ERESTART)
  966. xs->retries = 0;
  967. goto retry;
  968. case XS_BUSY:
  969. if (xs->retries) {
  970. if (scsi_delay(xs, 1) != ERESTART)
  971. xs->retries = 0;
  972. }
  973. goto retry;
  974. case XS_TIMEOUT:
  975. retry:
  976. if (xs->retries--) {
  977. scsi_xs_exec(xs);
  978. return;
  979. }
  980. /* FALLTHROUGH */
  981. default:
  982. bp->b_error = EIO;
  983. bp->b_flags |= B_ERROR;
  984. bp->b_resid = bp->b_bcount;
  985. break;
  986. }
  987. s = splbio();
  988. biodone(bp);
  989. splx(s);
  990. scsi_xs_put(xs);
  991. }
  992. void
  993. stminphys(struct buf *bp)
  994. {
  995. struct st_softc *st;
  996. st = stlookup(STUNIT(bp->b_dev));
  997. if (st == NULL)
  998. return;
  999. (*st->sc_link->adapter->scsi_minphys)(bp, st->sc_link);
  1000. device_unref(&st->sc_dev);
  1001. }
  1002. int
  1003. stread(dev_t dev, struct uio *uio, int iomode)
  1004. {
  1005. return (physio(ststrategy, dev, B_READ, stminphys, uio));
  1006. }
  1007. int
  1008. stwrite(dev_t dev, struct uio *uio, int iomode)
  1009. {
  1010. return (physio(ststrategy, dev, B_WRITE, stminphys, uio));
  1011. }
  1012. /*
  1013. * Perform special action on behalf of the user;
  1014. * knows about the internals of this device
  1015. */
  1016. int
  1017. stioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
  1018. {
  1019. int error = 0;
  1020. int nmarks;
  1021. int flags = 0;
  1022. struct st_softc *st;
  1023. int hold_blksize;
  1024. u_int8_t hold_density;
  1025. struct mtop *mt = (struct mtop *) arg;
  1026. int number;
  1027. /*
  1028. * Find the device that the user is talking about
  1029. */
  1030. st = stlookup(STUNIT(dev));
  1031. if (st == NULL)
  1032. return (ENXIO);
  1033. if (st->flags & ST_DYING) {
  1034. error = ENXIO;
  1035. goto done;
  1036. }
  1037. hold_blksize = st->blksize;
  1038. hold_density = st->density;
  1039. switch (cmd) {
  1040. case MTIOCGET: {
  1041. struct mtget *g = (struct mtget *) arg;
  1042. /*
  1043. * (to get the current state of READONLY)
  1044. */
  1045. error = st_mode_sense(st, SCSI_SILENT);
  1046. if (error)
  1047. break;
  1048. SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
  1049. bzero(g, sizeof(struct mtget));
  1050. g->mt_type = 0x7; /* Ultrix compat *//*? */
  1051. g->mt_blksiz = st->blksize;
  1052. g->mt_density = st->density;
  1053. g->mt_mblksiz = st->modes.blksize;
  1054. g->mt_mdensity = st->modes.density;
  1055. if (st->sc_link->flags & SDEV_READONLY)
  1056. g->mt_dsreg |= MT_DS_RDONLY;
  1057. if (st->flags & ST_MOUNTED)
  1058. g->mt_dsreg |= MT_DS_MOUNTED;
  1059. g->mt_resid = st->mt_resid;
  1060. g->mt_erreg = st->mt_erreg;
  1061. g->mt_fileno = st->media_fileno;
  1062. g->mt_blkno = st->media_blkno;
  1063. /*
  1064. * clear latched errors.
  1065. */
  1066. st->mt_resid = 0;
  1067. st->mt_erreg = 0;
  1068. break;
  1069. }
  1070. case MTIOCTOP: {
  1071. SC_DEBUG(st->sc_link, SDEV_DB1,
  1072. ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
  1073. number = mt->mt_count;
  1074. switch (mt->mt_op) {
  1075. case MTWEOF: /* write an end-of-file record */
  1076. error = st_write_filemarks(st, number, flags);
  1077. break;
  1078. case MTBSF: /* backward space file */
  1079. number = -number;
  1080. case MTFSF: /* forward space file */
  1081. error = st_check_eod(st, 0, &nmarks, flags);
  1082. if (!error)
  1083. error = st_space(st, number - nmarks,
  1084. SP_FILEMARKS, flags);
  1085. break;
  1086. case MTBSR: /* backward space record */
  1087. number = -number;
  1088. case MTFSR: /* forward space record */
  1089. error = st_check_eod(st, 1, &nmarks, flags);
  1090. if (!error)
  1091. error = st_space(st, number, SP_BLKS, flags);
  1092. break;
  1093. case MTREW: /* rewind */
  1094. error = st_rewind(st, 0, flags);
  1095. break;
  1096. case MTOFFL: /* rewind and put the drive offline */
  1097. st_unmount(st, EJECT, DOREWIND);
  1098. break;
  1099. case MTNOP: /* no operation, sets status only */
  1100. break;
  1101. case MTRETEN: /* retension the tape */
  1102. error = st_load(st, LD_RETENSION, flags);
  1103. if (!error)
  1104. error = st_load(st, LD_LOAD, flags);
  1105. break;
  1106. case MTEOM: /* forward space to end of media */
  1107. error = st_check_eod(st, 0, &nmarks, flags);
  1108. if (!error)
  1109. error = st_space(st, 1, SP_EOM, flags);
  1110. break;
  1111. case MTCACHE: /* enable controller cache */
  1112. st->flags &= ~ST_DONTBUFFER;
  1113. goto try_new_value;
  1114. case MTNOCACHE: /* disable controller cache */
  1115. st->flags |= ST_DONTBUFFER;
  1116. goto try_new_value;
  1117. case MTERASE: /* erase volume */
  1118. error = st_erase(st, number, flags);
  1119. break;
  1120. case MTSETBSIZ: /* Set block size for device */
  1121. if (number == 0) {
  1122. st->flags &= ~ST_FIXEDBLOCKS;
  1123. } else {
  1124. if ((st->blkmin || st->blkmax) &&
  1125. (number < st->blkmin ||
  1126. number > st->blkmax)) {
  1127. error = EINVAL;
  1128. break;
  1129. }
  1130. st->flags |= ST_FIXEDBLOCKS;
  1131. }
  1132. st->blksize = number;
  1133. st->flags |= ST_BLOCK_SET; /*XXX */
  1134. goto try_new_value;
  1135. case MTSETDNSTY: /* Set density for device and mode */
  1136. if (number < 0 || number > SCSI_MAX_DENSITY_CODE) {
  1137. error = EINVAL;
  1138. break;
  1139. } else
  1140. st->density = number;
  1141. goto try_new_value;
  1142. default:
  1143. error = EINVAL;
  1144. }
  1145. break;
  1146. }
  1147. case MTIOCIEOT:
  1148. case MTIOCEEOT:
  1149. break;
  1150. #if 0
  1151. case MTIOCRDSPOS:
  1152. error = st_rdpos(st, 0, (u_int32_t *) arg);
  1153. break;
  1154. case MTIOCRDHPOS:
  1155. error = st_rdpos(st, 1, (u_int32_t *) arg);
  1156. break;
  1157. case MTIOCSLOCATE:
  1158. error = st_setpos(st, 0, (u_int32_t *) arg);
  1159. break;
  1160. case MTIOCHLOCATE:
  1161. error = st_setpos(st, 1, (u_int32_t *) arg);
  1162. break;
  1163. #endif
  1164. default:
  1165. error = scsi_do_ioctl(st->sc_link, cmd, arg, flag);
  1166. break;
  1167. }
  1168. goto done;
  1169. try_new_value:
  1170. /*
  1171. * Check that the mode being asked for is aggreeable to the
  1172. * drive. If not, put it back the way it was.
  1173. */
  1174. if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
  1175. printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
  1176. st->density = hold_density;
  1177. st->blksize = hold_blksize;
  1178. if (st->blksize)
  1179. st->flags |= ST_FIXEDBLOCKS;
  1180. else
  1181. st->flags &= ~ST_FIXEDBLOCKS;
  1182. goto done;
  1183. }
  1184. /*
  1185. * As the drive liked it, if we are setting a new default,
  1186. * set it into the structures as such.
  1187. */
  1188. switch (mt->mt_op) {
  1189. case MTSETBSIZ:
  1190. st->modes.blksize = st->blksize;
  1191. st->modeflags |= BLKSIZE_SET_BY_USER;
  1192. break;
  1193. case MTSETDNSTY:
  1194. st->modes.density = st->density;
  1195. st->modeflags |= DENSITY_SET_BY_USER;
  1196. break;
  1197. }
  1198. done:
  1199. device_unref(&st->sc_dev);
  1200. return (error);
  1201. }
  1202. /*
  1203. * Do a synchronous read.
  1204. */
  1205. int
  1206. st_read(struct st_softc *st, char *buf, int size, int flags)
  1207. {
  1208. struct scsi_rw_tape *cmd;
  1209. struct scsi_xfer *xs;
  1210. int error;
  1211. if (size == 0)
  1212. return 0;
  1213. xs = scsi_xs_get(st->sc_link, flags | SCSI_DATA_IN);
  1214. if (xs == NULL)
  1215. return (ENOMEM);
  1216. xs->cmdlen = sizeof(*cmd);
  1217. xs->data = buf;
  1218. xs->datalen = size;
  1219. xs->retries = 0;
  1220. xs->timeout = ST_IO_TIME;
  1221. cmd = (struct scsi_rw_tape *)xs->cmd;
  1222. cmd->opcode = READ;
  1223. if (st->flags & ST_FIXEDBLOCKS) {
  1224. cmd->byte2 |= SRW_FIXED;
  1225. _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
  1226. cmd->len);
  1227. } else
  1228. _lto3b(size, cmd->len);
  1229. error = scsi_xs_sync(xs);
  1230. scsi_xs_put(xs);
  1231. return (error);
  1232. }
  1233. /*
  1234. * Ask the drive what its min and max blk sizes are.
  1235. */
  1236. int
  1237. st_read_block_limits(struct st_softc *st, int flags)
  1238. {
  1239. struct scsi_block_limits_data *block_limits = NULL;
  1240. struct scsi_block_limits *cmd;
  1241. struct scsi_link *sc_link = st->sc_link;
  1242. struct scsi_xfer *xs;
  1243. int error = 0;
  1244. if ((sc_link->flags & SDEV_MEDIA_LOADED))
  1245. return (0);
  1246. block_limits = dma_alloc(sizeof(*block_limits), PR_NOWAIT);
  1247. if (block_limits == NULL)
  1248. return (ENOMEM);
  1249. xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN);
  1250. if (xs == NULL) {
  1251. error = ENOMEM;
  1252. goto done;
  1253. }
  1254. xs->cmdlen = sizeof(*cmd);
  1255. xs->data = (void *)block_limits;
  1256. xs->datalen = sizeof(*block_limits);
  1257. xs->timeout = ST_CTL_TIME;
  1258. cmd = (struct scsi_block_limits *)xs->cmd;
  1259. cmd->opcode = READ_BLOCK_LIMITS;
  1260. error = scsi_xs_sync(xs);
  1261. scsi_xs_put(xs);
  1262. if (error == 0) {
  1263. st->blkmin = _2btol(block_limits->min_length);
  1264. st->blkmax = _3btol(block_limits->max_length);
  1265. SC_DEBUG(sc_link, SDEV_DB3,
  1266. ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
  1267. }
  1268. done:
  1269. if (block_limits)
  1270. dma_free(block_limits, sizeof(*block_limits));
  1271. return (error);
  1272. }
  1273. /*
  1274. * Get the scsi driver to send a full inquiry to the
  1275. * device and use the results to fill out the global
  1276. * parameter structure.
  1277. *
  1278. * called from:
  1279. * attach
  1280. * open
  1281. * ioctl (to reset original blksize)
  1282. */
  1283. int
  1284. st_mode_sense(struct st_softc *st, int flags)
  1285. {
  1286. union scsi_mode_sense_buf *data = NULL;
  1287. struct scsi_link *sc_link = st->sc_link;
  1288. u_int64_t block_count;
  1289. u_int32_t density, block_size;
  1290. u_char *page0 = NULL;
  1291. u_int8_t dev_spec;
  1292. int error = 0, big;
  1293. data = dma_alloc(sizeof(*data), PR_NOWAIT);
  1294. if (data == NULL)
  1295. return (ENOMEM);
  1296. /*
  1297. * Ask for page 0 (vendor specific) mode sense data.
  1298. */
  1299. error = scsi_do_mode_sense(sc_link, 0, data, (void **)&page0,
  1300. &density, &block_count, &block_size, 1, flags | SCSI_SILENT, &big);
  1301. if (error != 0)
  1302. goto done;
  1303. /* It is valid for no page0 to be available. */
  1304. if (big)
  1305. dev_spec = data->hdr_big.dev_spec;
  1306. else
  1307. dev_spec = data->hdr.dev_spec;
  1308. if (dev_spec & SMH_DSP_WRITE_PROT)
  1309. SET(sc_link->flags, SDEV_READONLY);
  1310. else
  1311. CLR(sc_link->flags, SDEV_READONLY);
  1312. st->numblks = block_count;
  1313. st->media_blksize = block_size;
  1314. st->media_density = density;
  1315. SC_DEBUG(sc_link, SDEV_DB3,
  1316. ("density code 0x%x, %d-byte blocks, write-%s, ",
  1317. st->media_density, st->media_blksize,
  1318. sc_link->flags & SDEV_READONLY ? "protected" : "enabled"));
  1319. SC_DEBUGN(sc_link, SDEV_DB3,
  1320. ("%sbuffered\n", dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
  1321. sc_link->flags |= SDEV_MEDIA_LOADED;
  1322. done:
  1323. if (data)
  1324. dma_free(data, sizeof(*data));
  1325. return (error);
  1326. }
  1327. /*
  1328. * Send a filled out parameter structure to the drive to
  1329. * set it into the desire modes etc.
  1330. */
  1331. int
  1332. st_mode_select(struct st_softc *st, int flags)
  1333. {
  1334. union scsi_mode_sense_buf *inbuf = NULL, *outbuf = NULL;
  1335. struct scsi_blk_desc general;
  1336. struct scsi_link *sc_link = st->sc_link;
  1337. u_int8_t *page0 = NULL;
  1338. int error = 0, big, page0_size;
  1339. inbuf = dma_alloc(sizeof(*inbuf), PR_NOWAIT);
  1340. if (inbuf == NULL) {
  1341. error = ENOMEM;
  1342. goto done;
  1343. }
  1344. outbuf = dma_alloc(sizeof(*outbuf), PR_NOWAIT | PR_ZERO);
  1345. if (outbuf == NULL) {
  1346. error = ENOMEM;
  1347. goto done;
  1348. }
  1349. /*
  1350. * This quirk deals with drives that have only one valid mode and think
  1351. * this gives them license to reject all mode selects, even if the
  1352. * selected mode is the one that is supported.
  1353. */
  1354. if (st->quirks & ST_Q_UNIMODAL) {
  1355. SC_DEBUG(sc_link, SDEV_DB3,
  1356. ("not setting density 0x%x blksize 0x%x\n",
  1357. st->density, st->blksize));
  1358. error = 0;
  1359. goto done;
  1360. }
  1361. if (sc_link->flags & SDEV_ATAPI) {
  1362. error = 0;
  1363. goto done;
  1364. }
  1365. bzero(&general, sizeof(general));
  1366. general.density = st->density;
  1367. if (st->flags & ST_FIXEDBLOCKS)
  1368. _lto3b(st->blksize, general.blklen);
  1369. /*
  1370. * Ask for page 0 (vendor specific) mode sense data.
  1371. */
  1372. error = scsi_do_mode_sense(sc_link, 0, inbuf, (void **)&page0, NULL,
  1373. NULL, NULL, 1, flags | SCSI_SILENT, &big);
  1374. if (error != 0)
  1375. goto done;
  1376. if (page0 == NULL) {
  1377. page0_size = 0;
  1378. } else if (big == 0) {
  1379. page0_size = inbuf->hdr.data_length +
  1380. sizeof(inbuf->hdr.data_length) - sizeof(inbuf->hdr) -
  1381. inbuf->hdr.blk_desc_len;
  1382. memcpy(&outbuf->buf[sizeof(outbuf->hdr)+ sizeof(general)],
  1383. page0, page0_size);
  1384. } else {
  1385. page0_size = _2btol(inbuf->hdr_big.data_length) +
  1386. sizeof(inbuf->hdr_big.data_length) -
  1387. sizeof(inbuf->hdr_big) -
  1388. _2btol(inbuf->hdr_big.blk_desc_len);
  1389. memcpy(&outbuf->buf[sizeof(outbuf->hdr_big) + sizeof(general)],
  1390. page0, page0_size);
  1391. }
  1392. /*
  1393. * Set up for a mode select.
  1394. */
  1395. if (big == 0) {
  1396. outbuf->hdr.data_length = sizeof(outbuf->hdr) +
  1397. sizeof(general) + page0_size -
  1398. sizeof(outbuf->hdr.data_length);
  1399. if ((st->flags & ST_DONTBUFFER) == 0)
  1400. outbuf->hdr.dev_spec = SMH_DSP_BUFF_MODE_ON;
  1401. outbuf->hdr.blk_desc_len = sizeof(general);
  1402. memcpy(&outbuf->buf[sizeof(outbuf->hdr)],
  1403. &general, sizeof(general));
  1404. error = scsi_mode_select(st->sc_link, 0, &outbuf->hdr,
  1405. flags, ST_CTL_TIME);
  1406. goto done;
  1407. }
  1408. /* MODE SENSE (10) header was returned, so use MODE SELECT (10). */
  1409. _lto2b((sizeof(outbuf->hdr_big) + sizeof(general) + page0_size -
  1410. sizeof(outbuf->hdr_big.data_length)), outbuf->hdr_big.data_length);
  1411. if ((st->flags & ST_DONTBUFFER) == 0)
  1412. outbuf->hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON;
  1413. _lto2b(sizeof(general), outbuf->hdr_big.blk_desc_len);
  1414. memcpy(&outbuf->buf[sizeof(outbuf->hdr_big)], &general,
  1415. sizeof(general));
  1416. error = scsi_mode_select_big(st->sc_link, 0, &outbuf->hdr_big,
  1417. flags, ST_CTL_TIME);
  1418. done:
  1419. if (inbuf)
  1420. dma_free(inbuf, sizeof(*inbuf));
  1421. if (outbuf)
  1422. dma_free(outbuf, sizeof(*outbuf));
  1423. return (error);
  1424. }
  1425. /*
  1426. * issue an erase command
  1427. */
  1428. int
  1429. st_erase(struct st_softc *st, int full, int flags)
  1430. {
  1431. struct scsi_erase *cmd;
  1432. struct scsi_xfer *xs;
  1433. int error;
  1434. xs = scsi_xs_get(st->sc_link, flags);
  1435. if (xs == NULL)
  1436. return (ENOMEM);
  1437. xs->cmdlen = sizeof(*cmd);
  1438. /*
  1439. * Full erase means set LONG bit in erase command, which asks
  1440. * the drive to erase the entire unit. Without this bit, we're
  1441. * asking the drive to write an erase gap.
  1442. */
  1443. cmd = (struct scsi_erase *)xs->cmd;
  1444. cmd->opcode = ERASE;
  1445. if (full) {
  1446. cmd->byte2 = SE_IMMED|SE_LONG;
  1447. xs->timeout = ST_SPC_TIME;
  1448. } else {
  1449. cmd->byte2 = SE_IMMED;
  1450. xs->timeout = ST_IO_TIME;
  1451. }
  1452. /*
  1453. * XXX We always do this asynchronously, for now. How long should
  1454. * we wait if we want to (eventually) to it synchronously?
  1455. */
  1456. error = scsi_xs_sync(xs);
  1457. scsi_xs_put(xs);
  1458. return (error);
  1459. }
  1460. /*
  1461. * skip N blocks/filemarks/seq filemarks/eom
  1462. */
  1463. int
  1464. st_space(struct st_softc *st, int number, u_int what, int flags)
  1465. {
  1466. struct scsi_space *cmd;
  1467. struct scsi_xfer *xs;
  1468. int error;
  1469. switch (what) {
  1470. case SP_BLKS:
  1471. if (st->flags & ST_PER_ACTION) {
  1472. if (number > 0) {
  1473. st->flags &= ~ST_PER_ACTION;
  1474. return EIO;
  1475. } else if (number < 0) {
  1476. if (st->flags & ST_AT_FILEMARK) {
  1477. /*
  1478. * Handling of ST_AT_FILEMARK
  1479. * in st_space will fill in the
  1480. * right file mark count.
  1481. */
  1482. error = st_space(st, 0, SP_FILEMARKS,
  1483. flags);
  1484. if (error)
  1485. return error;
  1486. }
  1487. if (st->flags & ST_BLANK_READ) {
  1488. st->flags &= ~ST_BLANK_READ;
  1489. return EIO;
  1490. }
  1491. st->flags &= ~(ST_EIO_PENDING | ST_EOM_PENDING);
  1492. }
  1493. }
  1494. break;
  1495. case SP_FILEMARKS:
  1496. if (st->flags & ST_EIO_PENDING) {
  1497. if (number > 0) {
  1498. /* pretend we just discovered the error */
  1499. st->flags &= ~ST_EIO_PENDING;
  1500. return EIO;
  1501. } else if (number < 0) {
  1502. /* back away from the error */
  1503. st->flags &= ~ST_EIO_PENDING;
  1504. }
  1505. }
  1506. if (st->flags & ST_AT_FILEMARK) {
  1507. st->flags &= ~ST_AT_FILEMARK;
  1508. number--;
  1509. }
  1510. if ((st->flags & ST_BLANK_READ) && (number < 0)) {
  1511. /* back away from unwritten tape */
  1512. st->flags &= ~ST_BLANK_READ;
  1513. number++; /* XXX dubious */
  1514. }
  1515. break;
  1516. case SP_EOM:
  1517. if (st->flags & ST_EOM_PENDING) {
  1518. /* We are already there. */
  1519. st->flags &= ~ST_EOM_PENDING;
  1520. return (0);
  1521. }
  1522. if (st->flags & ST_EIO_PENDING) {
  1523. /* pretend we just discovered the error */
  1524. st->flags &= ~ST_EIO_PENDING;
  1525. return EIO;
  1526. }
  1527. if (st->flags & ST_AT_FILEMARK)
  1528. st->flags &= ~ST_AT_FILEMARK;
  1529. break;
  1530. }
  1531. if (number == 0)
  1532. return 0;
  1533. xs = scsi_xs_get(st->sc_link, flags);
  1534. if (xs == NULL)
  1535. return (ENOMEM);
  1536. cmd = (struct scsi_space *)xs->cmd;
  1537. cmd->opcode = SPACE;
  1538. cmd->byte2 = what;
  1539. _lto3b(number, cmd->number);
  1540. xs->cmdlen = sizeof(*cmd);
  1541. xs->timeout = ST_SPC_TIME;
  1542. CLR(st->flags, ST_EOD_DETECTED);
  1543. error = scsi_xs_sync(xs);
  1544. scsi_xs_put(xs);
  1545. if (error != 0) {
  1546. st->media_fileno = -1;
  1547. st->media_blkno = -1;
  1548. } else {
  1549. switch (what) {
  1550. case SP_BLKS:
  1551. if (st->media_blkno != -1) {
  1552. st->media_blkno += number;
  1553. if (st->media_blkno < 0)
  1554. st->media_blkno = -1;
  1555. }
  1556. break;
  1557. case SP_FILEMARKS:
  1558. if (st->media_fileno != -1) {
  1559. if (!ISSET(st->flags, ST_EOD_DETECTED))
  1560. st->media_fileno += number;
  1561. if (st->media_fileno > st->media_eom)
  1562. st->media_eom = st->media_fileno;
  1563. st->media_blkno = 0;
  1564. }
  1565. break;
  1566. case SP_EOM:
  1567. if (st->media_eom != -1) {
  1568. st->media_fileno = st->media_eom;
  1569. st->media_blkno = 0;
  1570. } else {
  1571. st->media_fileno = -1;
  1572. st->media_blkno = -1;
  1573. }
  1574. break;
  1575. default:
  1576. st->media_fileno = -1;
  1577. st->media_blkno = -1;
  1578. break;
  1579. }
  1580. }
  1581. return (error);
  1582. }
  1583. /*
  1584. * write N filemarks
  1585. */
  1586. int
  1587. st_write_filemarks(struct st_softc *st, int number, int flags)
  1588. {
  1589. struct scsi_write_filemarks *cmd;
  1590. struct scsi_xfer *xs;
  1591. int error;
  1592. if (number < 0)
  1593. return (EINVAL);
  1594. xs = scsi_xs_get(st->sc_link, flags);
  1595. if (xs == NULL)
  1596. return (ENOMEM);
  1597. xs->cmdlen = sizeof(*cmd);
  1598. xs->timeout = ST_IO_TIME * 4;
  1599. switch (number) {
  1600. case 0: /* really a command to sync the drive's buffers */
  1601. break;
  1602. case 1:
  1603. if (st->flags & ST_FM_WRITTEN) /* already have one down */
  1604. st->flags &= ~ST_WRITTEN;
  1605. else
  1606. st->flags |= ST_FM_WRITTEN;
  1607. st->flags &= ~ST_PER_ACTION;
  1608. break;
  1609. default:
  1610. st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
  1611. break;
  1612. }
  1613. cmd = (struct scsi_write_filemarks *)xs->cmd;
  1614. cmd->opcode = WRITE_FILEMARKS;
  1615. _lto3b(number, cmd->number);
  1616. error = scsi_xs_sync(xs);
  1617. scsi_xs_put(xs);
  1618. if (error != 0) {
  1619. st->media_fileno = -1;
  1620. st->media_blkno = -1;
  1621. st->media_eom = -1;
  1622. } else if (st->media_fileno != -1) {
  1623. st->media_fileno += number;
  1624. st->media_eom = st->media_fileno;
  1625. st->media_blkno = 0;
  1626. }
  1627. return (error);
  1628. }
  1629. /*
  1630. * Make sure the right number of file marks is on tape if the
  1631. * tape has been written. If the position argument is true,
  1632. * leave the tape positioned where it was originally.
  1633. *
  1634. * nmarks returns the number of marks to skip (or, if position
  1635. * true, which were skipped) to get back original position.
  1636. */
  1637. int
  1638. st_check_eod(struct st_softc *st, int position, int *nmarks, int flags)
  1639. {
  1640. int error;
  1641. switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
  1642. default:
  1643. *nmarks = 0;
  1644. return 0;
  1645. case ST_WRITTEN:
  1646. case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
  1647. *nmarks = 1;
  1648. break;
  1649. case ST_WRITTEN | ST_2FM_AT_EOD:
  1650. *nmarks = 2;
  1651. }
  1652. error = st_write_filemarks(st, *nmarks, flags);
  1653. if (position && !error)
  1654. error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
  1655. return error;
  1656. }
  1657. /*
  1658. * load/unload/retension
  1659. */
  1660. int
  1661. st_load(struct st_softc *st, u_int type, int flags)
  1662. {
  1663. struct scsi_load *cmd;
  1664. struct scsi_xfer *xs;
  1665. int error, nmarks;
  1666. st->media_fileno = -1;
  1667. st->media_blkno = -1;
  1668. st->media_eom = -1;
  1669. if (type != LD_LOAD) {
  1670. error = st_check_eod(st, 0, &nmarks, flags);
  1671. if (error)
  1672. return (error);
  1673. }
  1674. if (st->quirks & ST_Q_IGNORE_LOADS) {
  1675. if (type == LD_LOAD) {
  1676. /*
  1677. * If we ignore loads, at least we should try a rewind.
  1678. */
  1679. return (st_rewind(st, 0, flags));
  1680. }
  1681. return (0);
  1682. }
  1683. xs = scsi_xs_get(st->sc_link, flags);
  1684. if (xs == NULL)
  1685. return (ENOMEM);
  1686. xs->cmdlen = sizeof(*cmd);
  1687. xs->timeout = ST_SPC_TIME;
  1688. cmd = (struct scsi_load *)xs->cmd;
  1689. cmd->opcode = LOAD;
  1690. cmd->how = type;
  1691. error = scsi_xs_sync(xs);
  1692. scsi_xs_put(xs);
  1693. return (error);
  1694. }
  1695. /*
  1696. * Rewind the device
  1697. */
  1698. int
  1699. st_rewind(struct st_softc *st, u_int immediate, int flags)
  1700. {
  1701. struct scsi_rewind *cmd;
  1702. struct scsi_xfer *xs;
  1703. int error, nmarks;
  1704. error = st_check_eod(st, 0, &nmarks, flags);
  1705. if (error)
  1706. return (error);
  1707. st->flags &= ~ST_PER_ACTION;
  1708. xs = scsi_xs_get(st->sc_link, flags);
  1709. if (xs == NULL)
  1710. return (ENOMEM);
  1711. xs->cmdlen = sizeof(*cmd);
  1712. xs->timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
  1713. cmd = (struct scsi_rewind *)xs->cmd;
  1714. cmd->opcode = REWIND;
  1715. cmd->byte2 = immediate;
  1716. error = scsi_xs_sync(xs);
  1717. scsi_xs_put(xs);
  1718. if (error == 0) {
  1719. st->media_fileno = 0;
  1720. st->media_blkno = 0;
  1721. }
  1722. return (error);
  1723. }
  1724. /*
  1725. * Look at the returned sense and act on the error and detirmine
  1726. * The unix error number to pass back... (0 = report no error)
  1727. * (-1 = continue processing)
  1728. */
  1729. int
  1730. st_interpret_sense(struct scsi_xfer *xs)
  1731. {
  1732. struct scsi_sense_data *sense = &xs->sense;
  1733. struct scsi_link *sc_link = xs->sc_link;
  1734. struct scsi_space *space;
  1735. struct st_softc *st = sc_link->device_softc;
  1736. u_int8_t serr = sense->error_code & SSD_ERRCODE;
  1737. u_int8_t skey = sense->flags & SSD_KEY;
  1738. int32_t resid, info, number;
  1739. int datalen;
  1740. if (((sc_link->flags & SDEV_OPEN) == 0) ||
  1741. (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED))
  1742. return (scsi_interpret_sense(xs));
  1743. info = (int32_t)_4btol(sense->info);
  1744. switch (skey) {
  1745. /*
  1746. * We do custom processing in st for the unit becoming ready case.
  1747. * in this case we do not allow xs->retries to be decremented
  1748. * only on the "Unit Becoming Ready" case. This is because tape
  1749. * drives report "Unit Becoming Ready" when loading media, etc.
  1750. * and can take a long time. Rather than having a massive timeout
  1751. * for all operations (which would cause other problems) we allow
  1752. * operations to wait (but be interruptable with Ctrl-C) forever
  1753. * as long as the drive is reporting that it is becoming ready.
  1754. * all other cases are handled as per the default.
  1755. */
  1756. case SKEY_NOT_READY:
  1757. if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
  1758. return (0);
  1759. switch (ASC_ASCQ(sense)) {
  1760. case SENSE_NOT_READY_BECOMING_READY:
  1761. SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n",
  1762. sense->add_sense_code_qual));
  1763. /* don't count this as a retry */
  1764. xs->retries++;
  1765. return (scsi_delay(xs, 1));
  1766. default:
  1767. return (scsi_interpret_sense(xs));
  1768. }
  1769. break;
  1770. case SKEY_BLANK_CHECK:
  1771. if (sense->error_code & SSD_ERRCODE_VALID &&
  1772. xs->cmd->opcode == SPACE) {
  1773. switch (ASC_ASCQ(sense)) {
  1774. case SENSE_END_OF_DATA_DETECTED:
  1775. st->flags |= ST_EOD_DETECTED;
  1776. space = (struct scsi_space *)xs->cmd;
  1777. number = _3btol(space->number);
  1778. st->media_fileno = number - info;
  1779. st->media_eom = st->media_fileno;
  1780. return (0);
  1781. case SENSE_BEGINNING_OF_MEDIUM_DETECTED:
  1782. /* Standard says: Position is undefined! */
  1783. st->flags |= ST_BOD_DETECTED;
  1784. st->media_fileno = -1;
  1785. st->media_blkno = -1;
  1786. return (0);
  1787. }
  1788. }
  1789. break;
  1790. case SKEY_NO_SENSE:
  1791. case SKEY_RECOVERED_ERROR:
  1792. case SKEY_MEDIUM_ERROR:
  1793. case SKEY_VOLUME_OVERFLOW:
  1794. break;
  1795. default:
  1796. return (scsi_interpret_sense(xs));
  1797. }
  1798. /*
  1799. * 'resid' can be in units of st->blksize or bytes. xs->resid and
  1800. * xs->datalen are always in units of bytes. So we need a variable
  1801. * to store datalen in the same units as resid and to adjust
  1802. * xs->resid to be in bytes.
  1803. */
  1804. if (sense->error_code & SSD_ERRCODE_VALID) {
  1805. if (st->flags & ST_FIXEDBLOCKS)
  1806. resid = info * st->blksize; /* XXXX overflow? */
  1807. else
  1808. resid = info;
  1809. } else {
  1810. resid = xs->datalen;
  1811. }
  1812. if (resid < 0 || resid > xs->datalen)
  1813. xs->resid = xs->datalen;
  1814. else
  1815. xs->resid = resid;
  1816. datalen = xs->datalen;
  1817. if (st->flags & ST_FIXEDBLOCKS) {
  1818. resid /= st->blksize;
  1819. datalen /= st->blksize;
  1820. }
  1821. if (sense->flags & SSD_FILEMARK) {
  1822. if (st->media_fileno != -1) {
  1823. st->media_fileno++;
  1824. if (st->media_fileno > st->media_eom)
  1825. st->media_eom = st->media_fileno;
  1826. st->media_blkno = 0;
  1827. }
  1828. if ((st->flags & ST_FIXEDBLOCKS) == 0)
  1829. return 0;
  1830. st->flags |= ST_AT_FILEMARK;
  1831. }
  1832. if (sense->flags & SSD_EOM) {
  1833. st->flags |= ST_EOM_PENDING;
  1834. xs->resid = 0;
  1835. if (st->flags & ST_FIXEDBLOCKS)
  1836. return (0);
  1837. }
  1838. if (sense->flags & SSD_ILI) {
  1839. if ((st->flags & ST_FIXEDBLOCKS) == 0) {
  1840. if (resid >= 0 && resid <= datalen)
  1841. return (0);
  1842. if ((xs->flags & SCSI_SILENT) == 0)
  1843. printf( "%s: bad residual %d out of "
  1844. "%d\n", st->sc_dev.dv_xname, resid,
  1845. datalen);
  1846. return (EIO);
  1847. }
  1848. /* Fixed size blocks. */
  1849. if (sense->error_code & SSD_ERRCODE_VALID)
  1850. if ((xs->flags & SCSI_SILENT) == 0)
  1851. printf("%s: block wrong size, %d blocks "
  1852. "residual\n", st->sc_dev.dv_xname, resid);
  1853. st->flags |= ST_EIO_PENDING;
  1854. /*
  1855. * This quirk code helps the drive read the first tape block,
  1856. * regardless of format. That is required for these drives to
  1857. * return proper MODE SENSE information.
  1858. */
  1859. if ((st->quirks & ST_Q_SENSE_HELP) &&
  1860. !(sc_link->flags & SDEV_MEDIA_LOADED))
  1861. st->blksize -= 512;
  1862. }
  1863. if ((st->flags & ST_FIXEDBLOCKS) && xs->resid == xs->datalen) {
  1864. if (st->flags & ST_EIO_PENDING)
  1865. return EIO;
  1866. if (st->flags & ST_AT_FILEMARK)
  1867. return 0;
  1868. }
  1869. if (skey == SKEY_BLANK_CHECK) {
  1870. /*
  1871. * This quirk code helps the drive read the first tape block,
  1872. * regardless of format. That is required for these drives to
  1873. * return proper MODE SENSE information.
  1874. */
  1875. if ((st->quirks & ST_Q_SENSE_HELP) &&
  1876. !(sc_link->flags & SDEV_MEDIA_LOADED)) {
  1877. /* still starting */
  1878. st->blksize -= 512;
  1879. } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
  1880. st->flags |= ST_BLANK_READ;
  1881. st->flags |= ST_EOM_PENDING;
  1882. xs->resid = xs->datalen;
  1883. return (0);
  1884. }
  1885. }
  1886. return (scsi_interpret_sense(xs));
  1887. }
  1888. /*
  1889. * The quirk here is that the drive returns some value to st_mode_sense
  1890. * incorrectly until the tape has actually passed by the head.
  1891. *
  1892. * The method is to set the drive to large fixed-block state (user-specified
  1893. * density and 1024-byte blocks), then read and rewind to get it to sense the
  1894. * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
  1895. * work, as a last resort, try variable- length blocks. The result will be
  1896. * the ability to do an accurate st_mode_sense.
  1897. *
  1898. * We know we can do a rewind because we just did a load, which implies rewind.
  1899. * Rewind seems preferable to space backward if we have a virgin tape.
  1900. *
  1901. * The rest of the code for this quirk is in ILI processing and BLANK CHECK
  1902. * error processing, both part of st_interpret_sense.
  1903. */
  1904. int
  1905. st_touch_tape(struct st_softc *st)
  1906. {
  1907. char *buf = NULL;
  1908. int readsize, maxblksize = 1024;
  1909. int error = 0;
  1910. if ((error = st_mode_sense(st, 0)) != 0)
  1911. goto done;
  1912. buf = dma_alloc(maxblksize, PR_NOWAIT);
  1913. if (!buf) {
  1914. error = ENOMEM;
  1915. goto done;
  1916. }
  1917. st->blksize = 1024;
  1918. do {
  1919. switch (st->blksize) {
  1920. case 512:
  1921. case 1024:
  1922. readsize = st->blksize;
  1923. st->flags |= ST_FIXEDBLOCKS;
  1924. break;
  1925. default:
  1926. readsize = 1;
  1927. st->flags &= ~ST_FIXEDBLOCKS;
  1928. }
  1929. if ((error = st_mode_select(st, 0)) != 0)
  1930. goto done;
  1931. st_read(st, buf, readsize, SCSI_SILENT); /* XXX */
  1932. if ((error = st_rewind(st, 0, 0)) != 0)
  1933. goto done;
  1934. } while (readsize != 1 && readsize > st->blksize);
  1935. done:
  1936. dma_free(buf, maxblksize);
  1937. return (error);
  1938. }
  1939. int
  1940. stdump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
  1941. {
  1942. /* Not implemented. */
  1943. return ENXIO;
  1944. }