| 1 | /* $NetBSD: st.c,v 1.230 2016/11/20 15:37:19 mlelstv Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Charles M. Hannum. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Originally written by Julian Elischer (julian@tfs.com) |
| 34 | * for TRW Financial Systems for use under the MACH(2.5) operating system. |
| 35 | * |
| 36 | * TRW Financial Systems, in accordance with their agreement with Carnegie |
| 37 | * Mellon University, makes this software available to CMU to distribute |
| 38 | * or use in any manner that they see fit as long as this message is kept with |
| 39 | * the software. For this reason TFS also grants any other persons or |
| 40 | * organisations permission to use or modify this software. |
| 41 | * |
| 42 | * TFS supplies this software to be publicly redistributed |
| 43 | * on the understanding that TFS is not responsible for the correct |
| 44 | * functioning of this software in any circumstances. |
| 45 | * |
| 46 | * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 |
| 47 | * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993 |
| 48 | * |
| 49 | * A lot of rewhacking done by mjacob (mjacob@nas.nasa.gov). |
| 50 | */ |
| 51 | |
| 52 | #include <sys/cdefs.h> |
| 53 | __KERNEL_RCSID(0, "$NetBSD: st.c,v 1.230 2016/11/20 15:37:19 mlelstv Exp $" ); |
| 54 | |
| 55 | #ifdef _KERNEL_OPT |
| 56 | #include "opt_scsi.h" |
| 57 | #endif |
| 58 | |
| 59 | #include <sys/param.h> |
| 60 | #include <sys/systm.h> |
| 61 | #include <sys/fcntl.h> |
| 62 | #include <sys/errno.h> |
| 63 | #include <sys/ioctl.h> |
| 64 | #include <sys/malloc.h> |
| 65 | #include <sys/buf.h> |
| 66 | #include <sys/bufq.h> |
| 67 | #include <sys/proc.h> |
| 68 | #include <sys/mtio.h> |
| 69 | #include <sys/device.h> |
| 70 | #include <sys/conf.h> |
| 71 | #include <sys/kernel.h> |
| 72 | #include <sys/vnode.h> |
| 73 | #include <sys/iostat.h> |
| 74 | #include <sys/sysctl.h> |
| 75 | |
| 76 | #include <dev/scsipi/scsi_spc.h> |
| 77 | #include <dev/scsipi/scsipi_all.h> |
| 78 | #include <dev/scsipi/scsi_all.h> |
| 79 | #include <dev/scsipi/scsi_tape.h> |
| 80 | #include <dev/scsipi/scsipiconf.h> |
| 81 | #include <dev/scsipi/scsipi_base.h> |
| 82 | #include <dev/scsipi/stvar.h> |
| 83 | |
| 84 | /* Defines for device specific stuff */ |
| 85 | #define DEF_FIXED_BSIZE 512 |
| 86 | |
| 87 | #define STMODE(z) ( minor(z) & 0x03) |
| 88 | #define STDSTY(z) ((minor(z) >> 2) & 0x03) |
| 89 | #define STUNIT(z) ((minor(z) >> 4) ) |
| 90 | #define STNMINOR 16 |
| 91 | |
| 92 | #define NORMAL_MODE 0 |
| 93 | #define NOREW_MODE 1 |
| 94 | #define EJECT_MODE 2 |
| 95 | #define CTRL_MODE 3 |
| 96 | |
| 97 | #ifndef ST_MOUNT_DELAY |
| 98 | #define ST_MOUNT_DELAY 0 |
| 99 | #endif |
| 100 | |
| 101 | static dev_type_open(stopen); |
| 102 | static dev_type_close(stclose); |
| 103 | static dev_type_read(stread); |
| 104 | static dev_type_write(stwrite); |
| 105 | static dev_type_ioctl(stioctl); |
| 106 | static dev_type_strategy(ststrategy); |
| 107 | static dev_type_dump(stdump); |
| 108 | |
| 109 | const struct bdevsw st_bdevsw = { |
| 110 | .d_open = stopen, |
| 111 | .d_close = stclose, |
| 112 | .d_strategy = ststrategy, |
| 113 | .d_ioctl = stioctl, |
| 114 | .d_dump = stdump, |
| 115 | .d_psize = nosize, |
| 116 | .d_discard = nodiscard, |
| 117 | .d_flag = D_TAPE | D_MPSAFE |
| 118 | }; |
| 119 | |
| 120 | const struct cdevsw st_cdevsw = { |
| 121 | .d_open = stopen, |
| 122 | .d_close = stclose, |
| 123 | .d_read = stread, |
| 124 | .d_write = stwrite, |
| 125 | .d_ioctl = stioctl, |
| 126 | .d_stop = nostop, |
| 127 | .d_tty = notty, |
| 128 | .d_poll = nopoll, |
| 129 | .d_mmap = nommap, |
| 130 | .d_kqfilter = nokqfilter, |
| 131 | .d_discard = nodiscard, |
| 132 | .d_flag = D_TAPE | D_MPSAFE |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * Define various devices that we know mis-behave in some way, |
| 137 | * and note how they are bad, so we can correct for them |
| 138 | */ |
| 139 | |
| 140 | static const struct st_quirk_inquiry_pattern st_quirk_patterns[] = { |
| 141 | {{T_SEQUENTIAL, T_REMOV, |
| 142 | " " , " " , " " }, {0, 0, { |
| 143 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 144 | {ST_Q_FORCE_BLKSIZE, 512, QIC_24}, /* minor 4-7 */ |
| 145 | {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600}, /* minor 8-11 */ |
| 146 | {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250} /* minor 12-15 */ |
| 147 | }}}, |
| 148 | {{T_SEQUENTIAL, T_REMOV, |
| 149 | "TANDBERG" , " TDC 3600 " , "" }, {0, 12, { |
| 150 | {0, 0, 0}, /* minor 0-3 */ |
| 151 | {ST_Q_FORCE_BLKSIZE, 0, QIC_525}, /* minor 4-7 */ |
| 152 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 153 | {0, 0, QIC_120} /* minor 12-15 */ |
| 154 | }}}, |
| 155 | {{T_SEQUENTIAL, T_REMOV, |
| 156 | "TANDBERG" , " TDC 3800 " , "" }, {0, 0, { |
| 157 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 158 | {0, 0, QIC_525}, /* minor 4-7 */ |
| 159 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 160 | {0, 0, QIC_120} /* minor 12-15 */ |
| 161 | }}}, |
| 162 | {{T_SEQUENTIAL, T_REMOV, |
| 163 | "TANDBERG" , " SLR5 4/8GB " , "" }, {0, 0, { |
| 164 | {ST_Q_FORCE_BLKSIZE, 1024, 0}, /* minor 0-3 */ |
| 165 | {0, 0, 0}, /* minor 4-7 */ |
| 166 | {0, 0, 0}, /* minor 8-11 */ |
| 167 | {0, 0, 0} /* minor 12-15 */ |
| 168 | }}}, |
| 169 | /* |
| 170 | * lacking a manual for the 4200, it's not clear what the |
| 171 | * specific density codes should be- the device is a 2.5GB |
| 172 | * capable QIC drive, those density codes aren't readily |
| 173 | * availabel. The 'default' will just have to do. |
| 174 | */ |
| 175 | {{T_SEQUENTIAL, T_REMOV, |
| 176 | "TANDBERG" , " TDC 4200 " , "" }, {0, 0, { |
| 177 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 178 | {0, 0, QIC_525}, /* minor 4-7 */ |
| 179 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 180 | {0, 0, QIC_120} /* minor 12-15 */ |
| 181 | }}}, |
| 182 | /* |
| 183 | * At least -005 and -007 need this. I'll assume they all do unless I |
| 184 | * hear otherwise. - mycroft, 31MAR1994 |
| 185 | */ |
| 186 | {{T_SEQUENTIAL, T_REMOV, |
| 187 | "ARCHIVE " , "VIPER 2525 25462" , "" }, {0, 0, { |
| 188 | {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ |
| 189 | {ST_Q_SENSE_HELP, 0, QIC_525}, /* minor 4-7 */ |
| 190 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 191 | {0, 0, QIC_120} /* minor 12-15 */ |
| 192 | }}}, |
| 193 | /* |
| 194 | * One user reports that this works for his tape drive. It probably |
| 195 | * needs more work. - mycroft, 09APR1994 |
| 196 | */ |
| 197 | {{T_SEQUENTIAL, T_REMOV, |
| 198 | "SANKYO " , "CP525 " , "" }, {0, 0, { |
| 199 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 200 | {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */ |
| 201 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 202 | {0, 0, QIC_120} /* minor 12-15 */ |
| 203 | }}}, |
| 204 | {{T_SEQUENTIAL, T_REMOV, |
| 205 | "ANRITSU " , "DMT780 " , "" }, {0, 0, { |
| 206 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 207 | {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */ |
| 208 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 209 | {0, 0, QIC_120} /* minor 12-15 */ |
| 210 | }}}, |
| 211 | {{T_SEQUENTIAL, T_REMOV, |
| 212 | "ARCHIVE " , "VIPER 150 21247" , "" }, {ST_Q_ERASE_NOIMM, 12, { |
| 213 | {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ |
| 214 | {0, 0, QIC_150}, /* minor 4-7 */ |
| 215 | {0, 0, QIC_120}, /* minor 8-11 */ |
| 216 | {0, 0, QIC_24} /* minor 12-15 */ |
| 217 | }}}, |
| 218 | {{T_SEQUENTIAL, T_REMOV, |
| 219 | "ARCHIVE " , "VIPER 150 21531" , "" }, {ST_Q_ERASE_NOIMM, 12, { |
| 220 | {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ |
| 221 | {0, 0, QIC_150}, /* minor 4-7 */ |
| 222 | {0, 0, QIC_120}, /* minor 8-11 */ |
| 223 | {0, 0, QIC_24} /* minor 12-15 */ |
| 224 | }}}, |
| 225 | {{T_SEQUENTIAL, T_REMOV, |
| 226 | "WANGTEK " , "5099ES SCSI" , "" }, {0, 0, { |
| 227 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 228 | {0, 0, QIC_11}, /* minor 4-7 */ |
| 229 | {0, 0, QIC_24}, /* minor 8-11 */ |
| 230 | {0, 0, QIC_24} /* minor 12-15 */ |
| 231 | }}}, |
| 232 | {{T_SEQUENTIAL, T_REMOV, |
| 233 | "WANGTEK " , "5150ES SCSI" , "" }, {0, 0, { |
| 234 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 235 | {0, 0, QIC_24}, /* minor 4-7 */ |
| 236 | {0, 0, QIC_120}, /* minor 8-11 */ |
| 237 | {0, 0, QIC_150} /* minor 12-15 */ |
| 238 | }}}, |
| 239 | {{T_SEQUENTIAL, T_REMOV, |
| 240 | "WANGTEK " , "5525ES SCSI REV7" , "" }, {0, 0, { |
| 241 | {0, 0, 0}, /* minor 0-3 */ |
| 242 | {ST_Q_BLKSIZE, 0, QIC_525}, /* minor 4-7 */ |
| 243 | {0, 0, QIC_150}, /* minor 8-11 */ |
| 244 | {0, 0, QIC_120} /* minor 12-15 */ |
| 245 | }}}, |
| 246 | {{T_SEQUENTIAL, T_REMOV, |
| 247 | "WangDAT " , "Model 1300 " , "" }, {0, 0, { |
| 248 | {0, 0, 0}, /* minor 0-3 */ |
| 249 | {ST_Q_FORCE_BLKSIZE, 512, DDS}, /* minor 4-7 */ |
| 250 | {ST_Q_FORCE_BLKSIZE, 1024, DDS}, /* minor 8-11 */ |
| 251 | {ST_Q_FORCE_BLKSIZE, 0, DDS} /* minor 12-15 */ |
| 252 | }}}, |
| 253 | {{T_SEQUENTIAL, T_REMOV, |
| 254 | "EXABYTE " , "EXB-8200 " , "263H" }, {0, 5, { |
| 255 | {0, 0, 0}, /* minor 0-3 */ |
| 256 | {0, 0, 0}, /* minor 4-7 */ |
| 257 | {0, 0, 0}, /* minor 8-11 */ |
| 258 | {0, 0, 0} /* minor 12-15 */ |
| 259 | }}}, |
| 260 | {{T_SEQUENTIAL, T_REMOV, |
| 261 | "STK" , "9490" , "" }, |
| 262 | {ST_Q_FORCE_BLKSIZE, 0, { |
| 263 | {0, 0, 0}, /* minor 0-3 */ |
| 264 | {0, 0, 0}, /* minor 4-7 */ |
| 265 | {0, 0, 0}, /* minor 8-11 */ |
| 266 | {0, 0, 0} /* minor 12-15 */ |
| 267 | }}}, |
| 268 | {{T_SEQUENTIAL, T_REMOV, |
| 269 | "STK" , "SD-3" , "" }, |
| 270 | {ST_Q_FORCE_BLKSIZE, 0, { |
| 271 | {0, 0, 0}, /* minor 0-3 */ |
| 272 | {0, 0, 0}, /* minor 4-7 */ |
| 273 | {0, 0, 0}, /* minor 8-11 */ |
| 274 | {0, 0, 0} /* minor 12-15 */ |
| 275 | }}}, |
| 276 | {{T_SEQUENTIAL, T_REMOV, |
| 277 | "IBM" , "03590" , "" }, {ST_Q_IGNORE_LOADS, 0, { |
| 278 | {0, 0, 0}, /* minor 0-3 */ |
| 279 | {0, 0, 0}, /* minor 4-7 */ |
| 280 | {0, 0, 0}, /* minor 8-11 */ |
| 281 | {0, 0, 0} /* minor 12-15 */ |
| 282 | }}}, |
| 283 | {{T_SEQUENTIAL, T_REMOV, |
| 284 | "HP " , "T4000s " , "" }, {ST_Q_UNIMODAL, 0, { |
| 285 | {0, 0, QIC_3095}, /* minor 0-3 */ |
| 286 | {0, 0, QIC_3095}, /* minor 4-7 */ |
| 287 | {0, 0, QIC_3095}, /* minor 8-11 */ |
| 288 | {0, 0, QIC_3095}, /* minor 12-15 */ |
| 289 | }}}, |
| 290 | #if 0 |
| 291 | {{T_SEQUENTIAL, T_REMOV, |
| 292 | "EXABYTE " , "EXB-8200 " , "" }, {0, 12, { |
| 293 | {0, 0, 0}, /* minor 0-3 */ |
| 294 | {0, 0, 0}, /* minor 4-7 */ |
| 295 | {0, 0, 0}, /* minor 8-11 */ |
| 296 | {0, 0, 0} /* minor 12-15 */ |
| 297 | }}}, |
| 298 | #endif |
| 299 | {{T_SEQUENTIAL, T_REMOV, |
| 300 | "TEAC " , "MT-2ST/N50 " , "" }, {ST_Q_IGNORE_LOADS, 0, { |
| 301 | {0, 0, 0}, /* minor 0-3 */ |
| 302 | {0, 0, 0}, /* minor 4-7 */ |
| 303 | {0, 0, 0}, /* minor 8-11 */ |
| 304 | {0, 0, 0} /* minor 12-15 */ |
| 305 | }}}, |
| 306 | {{T_SEQUENTIAL, T_REMOV, |
| 307 | "OnStream" , "ADR50 Drive" , "" }, {ST_Q_UNIMODAL, 0, { |
| 308 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 309 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 4-7 */ |
| 310 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 8-11 */ |
| 311 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 12-15 */ |
| 312 | }}}, |
| 313 | {{T_SEQUENTIAL, T_REMOV, |
| 314 | "OnStream DI-30" , "" , "1.0" }, {ST_Q_NOFILEMARKS, 0, { |
| 315 | {0, 0, 0}, /* minor 0-3 */ |
| 316 | {0, 0, 0}, /* minor 4-7 */ |
| 317 | {0, 0, 0}, /* minor 8-11 */ |
| 318 | {0, 0, 0} /* minor 12-15 */ |
| 319 | }}}, |
| 320 | {{T_SEQUENTIAL, T_REMOV, |
| 321 | "NCR H621" , "0-STD-03-46F880 " , "" }, {ST_Q_NOPREVENT, 0, { |
| 322 | {0, 0, 0}, /* minor 0-3 */ |
| 323 | {0, 0, 0}, /* minor 4-7 */ |
| 324 | {0, 0, 0}, /* minor 8-11 */ |
| 325 | {0, 0, 0} /* minor 12-15 */ |
| 326 | }}}, |
| 327 | {{T_SEQUENTIAL, T_REMOV, |
| 328 | "Seagate STT3401A" , "hp0atxa" , "" }, {0, 0, { |
| 329 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ |
| 330 | {ST_Q_FORCE_BLKSIZE, 1024, 0}, /* minor 4-7 */ |
| 331 | {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 8-11 */ |
| 332 | {ST_Q_FORCE_BLKSIZE, 512, 0} /* minor 12-15 */ |
| 333 | }}}, |
| 334 | }; |
| 335 | |
| 336 | #define NOEJECT 0 |
| 337 | #define EJECT 1 |
| 338 | |
| 339 | static void st_identify_drive(struct st_softc *, |
| 340 | struct scsipi_inquiry_pattern *); |
| 341 | static void st_loadquirks(struct st_softc *); |
| 342 | static int st_mount_tape(dev_t, int); |
| 343 | static void st_unmount(struct st_softc *, boolean); |
| 344 | static int st_decide_mode(struct st_softc *, boolean); |
| 345 | static void ststart(struct scsipi_periph *); |
| 346 | static void strestart(void *); |
| 347 | static void stdone(struct scsipi_xfer *, int); |
| 348 | static int st_read(struct st_softc *, char *, int, int); |
| 349 | static int st_space(struct st_softc *, int, u_int, int); |
| 350 | static int st_write_filemarks(struct st_softc *, int, int); |
| 351 | static int st_check_eod(struct st_softc *, boolean, int *, int); |
| 352 | static int st_load(struct st_softc *, u_int, int); |
| 353 | static int st_rewind(struct st_softc *, u_int, int); |
| 354 | static int st_interpret_sense(struct scsipi_xfer *); |
| 355 | static int st_touch_tape(struct st_softc *); |
| 356 | static int st_erase(struct st_softc *, int full, int flags); |
| 357 | static int st_rdpos(struct st_softc *, int, uint32_t *); |
| 358 | static int st_setpos(struct st_softc *, int, uint32_t *); |
| 359 | |
| 360 | static const struct scsipi_periphsw st_switch = { |
| 361 | st_interpret_sense, |
| 362 | ststart, |
| 363 | NULL, |
| 364 | stdone |
| 365 | }; |
| 366 | |
| 367 | #if defined(ST_ENABLE_EARLYWARN) |
| 368 | #define ST_INIT_FLAGS ST_EARLYWARN |
| 369 | #else |
| 370 | #define ST_INIT_FLAGS 0 |
| 371 | #endif |
| 372 | |
| 373 | /* |
| 374 | * The routine called by the low level scsi routine when it discovers |
| 375 | * A device suitable for this driver |
| 376 | */ |
| 377 | void |
| 378 | stattach(device_t parent, device_t self, void *aux) |
| 379 | { |
| 380 | struct st_softc *st = device_private(self); |
| 381 | struct scsipibus_attach_args *sa = aux; |
| 382 | struct scsipi_periph *periph = sa->sa_periph; |
| 383 | |
| 384 | SC_DEBUG(periph, SCSIPI_DB2, ("stattach: " )); |
| 385 | st->sc_dev = self; |
| 386 | |
| 387 | /* Store information needed to contact our base driver */ |
| 388 | st->sc_periph = periph; |
| 389 | periph->periph_dev = st->sc_dev; |
| 390 | periph->periph_switch = &st_switch; |
| 391 | |
| 392 | /* Set initial flags */ |
| 393 | st->flags = ST_INIT_FLAGS; |
| 394 | |
| 395 | /* Set up the buf queue for this device */ |
| 396 | bufq_alloc(&st->buf_queue, "fcfs" , 0); |
| 397 | callout_init(&st->sc_callout, 0); |
| 398 | |
| 399 | /* |
| 400 | * Check if the drive is a known criminal and take |
| 401 | * Any steps needed to bring it into line |
| 402 | */ |
| 403 | st_identify_drive(st, &sa->sa_inqbuf); |
| 404 | aprint_naive("\n" ); |
| 405 | aprint_normal("\n" ); |
| 406 | /* Use the subdriver to request information regarding the drive. */ |
| 407 | aprint_normal_dev(self, "%s" , st->quirkdata ? "quirks apply, " : "" ); |
| 408 | if (scsipi_test_unit_ready(periph, |
| 409 | XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) || |
| 410 | st->ops(st, ST_OPS_MODESENSE, |
| 411 | XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE)) |
| 412 | aprint_normal("drive empty\n" ); |
| 413 | else { |
| 414 | aprint_normal("density code %d, " , st->media_density); |
| 415 | if (st->media_blksize > 0) |
| 416 | aprint_normal("%d-byte" , st->media_blksize); |
| 417 | else |
| 418 | aprint_normal("variable" ); |
| 419 | aprint_normal(" blocks, write-%s\n" , |
| 420 | (st->flags & ST_READONLY) ? "protected" : "enabled" ); |
| 421 | } |
| 422 | |
| 423 | st->stats = iostat_alloc(IOSTAT_TAPE, parent, |
| 424 | device_xname(st->sc_dev)); |
| 425 | |
| 426 | rnd_attach_source(&st->rnd_source, device_xname(st->sc_dev), |
| 427 | RND_TYPE_TAPE, RND_FLAG_DEFAULT); |
| 428 | } |
| 429 | |
| 430 | int |
| 431 | stdetach(device_t self, int flags) |
| 432 | { |
| 433 | struct st_softc *st = device_private(self); |
| 434 | struct scsipi_periph *periph = st->sc_periph; |
| 435 | struct scsipi_channel *chan = periph->periph_channel; |
| 436 | int bmaj, cmaj, mn; |
| 437 | |
| 438 | /* locate the major number */ |
| 439 | bmaj = bdevsw_lookup_major(&st_bdevsw); |
| 440 | cmaj = cdevsw_lookup_major(&st_cdevsw); |
| 441 | |
| 442 | /* kill any pending restart */ |
| 443 | callout_stop(&st->sc_callout); |
| 444 | |
| 445 | mutex_enter(chan_mtx(chan)); |
| 446 | |
| 447 | /* Kill off any queued buffers. */ |
| 448 | bufq_drain(st->buf_queue); |
| 449 | |
| 450 | /* Kill off any pending commands. */ |
| 451 | scsipi_kill_pending(st->sc_periph); |
| 452 | |
| 453 | mutex_exit(chan_mtx(chan)); |
| 454 | |
| 455 | bufq_free(st->buf_queue); |
| 456 | |
| 457 | /* Nuke the vnodes for any open instances */ |
| 458 | mn = STUNIT(device_unit(self)); |
| 459 | vdevgone(bmaj, mn, mn+STNMINOR-1, VBLK); |
| 460 | vdevgone(cmaj, mn, mn+STNMINOR-1, VCHR); |
| 461 | |
| 462 | iostat_free(st->stats); |
| 463 | |
| 464 | /* Unhook the entropy source. */ |
| 465 | rnd_detach_source(&st->rnd_source); |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Use the inquiry routine in 'scsi_base' to get drive info so we can |
| 472 | * Further tailor our behaviour. |
| 473 | */ |
| 474 | static void |
| 475 | st_identify_drive(struct st_softc *st, struct scsipi_inquiry_pattern *inqbuf) |
| 476 | { |
| 477 | const struct st_quirk_inquiry_pattern *finger; |
| 478 | int priority; |
| 479 | |
| 480 | finger = scsipi_inqmatch(inqbuf, |
| 481 | st_quirk_patterns, |
| 482 | sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]), |
| 483 | sizeof(st_quirk_patterns[0]), &priority); |
| 484 | if (priority != 0) { |
| 485 | st->quirkdata = &finger->quirkdata; |
| 486 | st->drive_quirks = finger->quirkdata.quirks; |
| 487 | st->quirks = finger->quirkdata.quirks; /* start value */ |
| 488 | st->page_0_size = finger->quirkdata.page_0_size; |
| 489 | KASSERT(st->page_0_size <= MAX_PAGE_0_SIZE); |
| 490 | st_loadquirks(st); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * initialise the subdevices to the default (QUIRK) state. |
| 496 | * this will remove any setting made by the system operator or previous |
| 497 | * operations. |
| 498 | */ |
| 499 | static void |
| 500 | st_loadquirks(struct st_softc *st) |
| 501 | { |
| 502 | const struct modes *mode; |
| 503 | struct modes *mode2; |
| 504 | int i; |
| 505 | |
| 506 | mode = st->quirkdata->modes; |
| 507 | mode2 = st->modes; |
| 508 | for (i = 0; i < 4; i++) { |
| 509 | memset(mode2, 0, sizeof(struct modes)); |
| 510 | st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK | |
| 511 | DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER | |
| 512 | DENSITY_SET_BY_USER); |
| 513 | if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) { |
| 514 | mode2->blksize = mode->blksize; |
| 515 | st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK; |
| 516 | } |
| 517 | if (mode->density) { |
| 518 | mode2->density = mode->density; |
| 519 | st->modeflags[i] |= DENSITY_SET_BY_QUIRK; |
| 520 | } |
| 521 | mode2->quirks |= mode->quirks; |
| 522 | mode++; |
| 523 | mode2++; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* open the device. */ |
| 528 | static int |
| 529 | stopen(dev_t dev, int flags, int mode, struct lwp *l) |
| 530 | { |
| 531 | u_int stmode, dsty; |
| 532 | int error, sflags, unit, tries, ntries; |
| 533 | struct st_softc *st; |
| 534 | struct scsipi_periph *periph; |
| 535 | struct scsipi_adapter *adapt; |
| 536 | |
| 537 | unit = STUNIT(dev); |
| 538 | st = device_lookup_private(&st_cd, unit); |
| 539 | if (st == NULL) |
| 540 | return ENXIO; |
| 541 | |
| 542 | stmode = STMODE(dev); |
| 543 | dsty = STDSTY(dev); |
| 544 | |
| 545 | periph = st->sc_periph; |
| 546 | adapt = periph->periph_channel->chan_adapter; |
| 547 | |
| 548 | SC_DEBUG(periph, SCSIPI_DB1, |
| 549 | ("open: dev=0x%" PRIx64" (unit %d (of %d))\n" , dev, unit, |
| 550 | st_cd.cd_ndevs)); |
| 551 | |
| 552 | /* Only allow one at a time */ |
| 553 | if (periph->periph_flags & PERIPH_OPEN) { |
| 554 | aprint_error_dev(st->sc_dev, "already open\n" ); |
| 555 | return EBUSY; |
| 556 | } |
| 557 | |
| 558 | if ((error = scsipi_adapter_addref(adapt)) != 0) |
| 559 | return error; |
| 560 | |
| 561 | /* clear any latched errors. */ |
| 562 | st->mt_resid = 0; |
| 563 | st->mt_erreg = 0; |
| 564 | st->asc = 0; |
| 565 | st->ascq = 0; |
| 566 | |
| 567 | /* |
| 568 | * Catch any unit attention errors. Be silent about this |
| 569 | * unless we're already mounted. We ignore media change |
| 570 | * if we're in control mode or not mounted yet. |
| 571 | */ |
| 572 | if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) { |
| 573 | #ifdef SCSIDEBUG |
| 574 | sflags = XS_CTL_IGNORE_MEDIA_CHANGE; |
| 575 | #else |
| 576 | sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE; |
| 577 | #endif |
| 578 | } else |
| 579 | sflags = 0; |
| 580 | |
| 581 | /* |
| 582 | * If we're already mounted or we aren't configured for |
| 583 | * a mount delay, only try a test unit ready once. Otherwise, |
| 584 | * try up to ST_MOUNT_DELAY times with a rest interval of |
| 585 | * one second between each try. |
| 586 | */ |
| 587 | if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) |
| 588 | ntries = 1; |
| 589 | else |
| 590 | ntries = ST_MOUNT_DELAY; |
| 591 | |
| 592 | for (error = tries = 0; tries < ntries; tries++) { |
| 593 | int slpintr, oflags; |
| 594 | |
| 595 | /* |
| 596 | * If we had no error, or we're opening the control mode |
| 597 | * device, we jump out right away. |
| 598 | */ |
| 599 | error = scsipi_test_unit_ready(periph, sflags); |
| 600 | if (error == 0 || stmode == CTRL_MODE) |
| 601 | break; |
| 602 | |
| 603 | /* |
| 604 | * We had an error. |
| 605 | * |
| 606 | * If we're already mounted or we aren't configured for |
| 607 | * a mount delay, or the error isn't a NOT READY error, |
| 608 | * skip to the error exit now. |
| 609 | */ |
| 610 | if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 || |
| 611 | (st->mt_key != SKEY_NOT_READY)) { |
| 612 | goto bad; |
| 613 | } |
| 614 | |
| 615 | /* clear any latched errors. */ |
| 616 | st->mt_resid = 0; |
| 617 | st->mt_erreg = 0; |
| 618 | st->asc = 0; |
| 619 | st->ascq = 0; |
| 620 | |
| 621 | /* |
| 622 | * Fake that we have the device open so |
| 623 | * we block other apps from getting in. |
| 624 | */ |
| 625 | oflags = periph->periph_flags; |
| 626 | periph->periph_flags |= PERIPH_OPEN; |
| 627 | |
| 628 | slpintr = kpause("stload" , true, hz, NULL); |
| 629 | |
| 630 | periph->periph_flags = oflags; /* restore flags */ |
| 631 | if (slpintr != 0 && slpintr != EWOULDBLOCK) { |
| 632 | goto bad; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | |
| 637 | /* |
| 638 | * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has |
| 639 | * been opened to set defaults and perform other, usually non-I/O |
| 640 | * related, operations. In this case, do a quick check to see |
| 641 | * whether the unit actually had a tape loaded (this will be known |
| 642 | * as to whether or not we got a NOT READY for the above |
| 643 | * unit attention). If a tape is there, go do a mount sequence. |
| 644 | */ |
| 645 | if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) { |
| 646 | periph->periph_flags |= PERIPH_OPEN; |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * If we get this far and had an error set, that means we failed |
| 652 | * to pass the 'test unit ready' test for the non-controlmode device, |
| 653 | * so we bounce the open. |
| 654 | */ |
| 655 | if (error) |
| 656 | return error; |
| 657 | |
| 658 | /* Else, we're now committed to saying we're open. */ |
| 659 | periph->periph_flags |= PERIPH_OPEN; /* unit attn are now errors */ |
| 660 | |
| 661 | /* |
| 662 | * If it's a different mode, or if the media has been |
| 663 | * invalidated, unmount the tape from the previous |
| 664 | * session but continue with open processing |
| 665 | */ |
| 666 | if (st->last_dsty != dsty || |
| 667 | (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) |
| 668 | st_unmount(st, NOEJECT); |
| 669 | |
| 670 | /* |
| 671 | * If we are not mounted, then we should start a new |
| 672 | * mount session. |
| 673 | */ |
| 674 | if (!(st->flags & ST_MOUNTED)) { |
| 675 | if ((error = st_mount_tape(dev, flags)) != 0) |
| 676 | goto bad; |
| 677 | st->last_dsty = dsty; |
| 678 | } |
| 679 | if (!(st->quirks & ST_Q_NOPREVENT)) { |
| 680 | scsipi_prevent(periph, SPAMR_PREVENT_DT, |
| 681 | XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY); |
| 682 | } |
| 683 | |
| 684 | SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n" )); |
| 685 | return 0; |
| 686 | |
| 687 | bad: |
| 688 | st_unmount(st, NOEJECT); |
| 689 | scsipi_adapter_delref(adapt); |
| 690 | periph->periph_flags &= ~PERIPH_OPEN; |
| 691 | return error; |
| 692 | } |
| 693 | |
| 694 | static int |
| 695 | stclose(dev_t dev, int flags, int mode, struct lwp *l) |
| 696 | { |
| 697 | int stxx, error = 0; |
| 698 | struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev)); |
| 699 | struct scsipi_periph *periph = st->sc_periph; |
| 700 | struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; |
| 701 | |
| 702 | SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n" )); |
| 703 | |
| 704 | /* |
| 705 | * Make sure that a tape opened in write-only mode will have |
| 706 | * file marks written on it when closed, even if not written to. |
| 707 | * |
| 708 | * This is for SUN compatibility. Actually, the Sun way of |
| 709 | * things is to: |
| 710 | * |
| 711 | * only write filemarks if there are fmks to be written and |
| 712 | * - open for write (possibly read/write) |
| 713 | * - the last operation was a write |
| 714 | * or: |
| 715 | * - opened for wronly |
| 716 | * - no data was written (including filemarks) |
| 717 | */ |
| 718 | |
| 719 | stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN); |
| 720 | if (((flags & FWRITE) && stxx == ST_WRITTEN) || |
| 721 | ((flags & O_ACCMODE) == FWRITE && stxx == 0)) { |
| 722 | int nm; |
| 723 | error = st_check_eod(st, FALSE, &nm, 0); |
| 724 | } |
| 725 | |
| 726 | /* Allow robots to eject tape if needed. */ |
| 727 | scsipi_prevent(periph, SPAMR_ALLOW, |
| 728 | XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY); |
| 729 | |
| 730 | switch (STMODE(dev)) { |
| 731 | case NORMAL_MODE: |
| 732 | st_unmount(st, NOEJECT); |
| 733 | break; |
| 734 | case NOREW_MODE: |
| 735 | case CTRL_MODE: |
| 736 | /* |
| 737 | * Leave mounted unless media seems to have been removed. |
| 738 | * |
| 739 | * Otherwise, if we're to terminate a tape with more than one |
| 740 | * filemark [ and because we're not rewinding here ], backspace |
| 741 | * one filemark so that later appends will see an unbroken |
| 742 | * sequence of: |
| 743 | * |
| 744 | * file - FMK - file - FMK ... file - FMK FMK (EOM) |
| 745 | */ |
| 746 | if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { |
| 747 | st_unmount(st, NOEJECT); |
| 748 | } else if (error == 0) { |
| 749 | /* |
| 750 | * ST_WRITTEN was preserved from above. |
| 751 | * |
| 752 | * All we need to know here is: |
| 753 | * |
| 754 | * Were we writing this tape and was the last |
| 755 | * operation a write? |
| 756 | * |
| 757 | * Are there supposed to be 2FM at EOD? |
| 758 | * |
| 759 | * If both statements are true, then we backspace |
| 760 | * one filemark. |
| 761 | */ |
| 762 | stxx |= (st->flags & ST_2FM_AT_EOD); |
| 763 | if ((flags & FWRITE) != 0 && |
| 764 | (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) { |
| 765 | error = st_space(st, -1, SP_FILEMARKS, 0); |
| 766 | } |
| 767 | } |
| 768 | break; |
| 769 | case EJECT_MODE: |
| 770 | st_unmount(st, EJECT); |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | scsipi_wait_drain(periph); |
| 775 | |
| 776 | scsipi_adapter_delref(adapt); |
| 777 | periph->periph_flags &= ~PERIPH_OPEN; |
| 778 | |
| 779 | return error; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Start a new mount session. |
| 784 | * Copy in all the default parameters from the selected device mode. |
| 785 | * and try guess any that seem to be defaulted. |
| 786 | */ |
| 787 | static int |
| 788 | st_mount_tape(dev_t dev, int flags) |
| 789 | { |
| 790 | int unit; |
| 791 | u_int dsty; |
| 792 | struct st_softc *st; |
| 793 | struct scsipi_periph *periph; |
| 794 | int error = 0; |
| 795 | |
| 796 | unit = STUNIT(dev); |
| 797 | dsty = STDSTY(dev); |
| 798 | st = device_lookup_private(&st_cd, unit); |
| 799 | periph = st->sc_periph; |
| 800 | |
| 801 | if (st->flags & ST_MOUNTED) |
| 802 | return 0; |
| 803 | |
| 804 | SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n " )); |
| 805 | st->flags |= ST_NEW_MOUNT; |
| 806 | st->quirks = st->drive_quirks | st->modes[dsty].quirks; |
| 807 | /* |
| 808 | * If the media is new, then make sure we give it a chance to |
| 809 | * to do a 'load' instruction. (We assume it is new.) |
| 810 | */ |
| 811 | if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0) |
| 812 | return error; |
| 813 | /* |
| 814 | * Throw another dummy instruction to catch |
| 815 | * 'Unit attention' errors. Many drives give |
| 816 | * these after doing a Load instruction (with |
| 817 | * the MEDIUM MAY HAVE CHANGED asc/ascq). |
| 818 | */ |
| 819 | scsipi_test_unit_ready(periph, XS_CTL_SILENT); /* XXX */ |
| 820 | |
| 821 | /* |
| 822 | * Some devices can't tell you much until they have been |
| 823 | * asked to look at the media. This quirk does this. |
| 824 | */ |
| 825 | if (st->quirks & ST_Q_SENSE_HELP) |
| 826 | if ((error = st_touch_tape(st)) != 0) |
| 827 | return error; |
| 828 | /* |
| 829 | * Load the physical device parameters |
| 830 | * loads: blkmin, blkmax |
| 831 | */ |
| 832 | if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0) |
| 833 | return error; |
| 834 | /* |
| 835 | * Load the media dependent parameters |
| 836 | * includes: media_blksize,media_density,numblks |
| 837 | * As we have a tape in, it should be reflected here. |
| 838 | * If not you may need the "quirk" above. |
| 839 | */ |
| 840 | if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0) |
| 841 | return error; |
| 842 | /* |
| 843 | * If we have gained a permanent density from somewhere, |
| 844 | * then use it in preference to the one supplied by |
| 845 | * default by the driver. |
| 846 | */ |
| 847 | if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) |
| 848 | st->density = st->modes[dsty].density; |
| 849 | else |
| 850 | st->density = st->media_density; |
| 851 | /* |
| 852 | * If we have gained a permanent blocksize |
| 853 | * then use it in preference to the one supplied by |
| 854 | * default by the driver. |
| 855 | */ |
| 856 | st->flags &= ~ST_FIXEDBLOCKS; |
| 857 | if (st->modeflags[dsty] & |
| 858 | (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) { |
| 859 | st->blksize = st->modes[dsty].blksize; |
| 860 | if (st->blksize) |
| 861 | st->flags |= ST_FIXEDBLOCKS; |
| 862 | } else { |
| 863 | if ((error = st_decide_mode(st, FALSE)) != 0) |
| 864 | return error; |
| 865 | } |
| 866 | if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) { |
| 867 | /* ATAPI will return ENODEV for this, and this may be OK */ |
| 868 | if (error != ENODEV) { |
| 869 | aprint_error_dev(st->sc_dev, |
| 870 | "cannot set selected mode\n" ); |
| 871 | return error; |
| 872 | } |
| 873 | } |
| 874 | st->flags &= ~ST_NEW_MOUNT; |
| 875 | st->flags |= ST_MOUNTED; |
| 876 | periph->periph_flags |= PERIPH_MEDIA_LOADED; /* move earlier? */ |
| 877 | st->blkno = st->fileno = (daddr_t) 0; |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * End the present mount session. |
| 883 | * Rewind, and optionally eject the tape. |
| 884 | * Reset various flags to indicate that all new |
| 885 | * operations require another mount operation |
| 886 | */ |
| 887 | static void |
| 888 | st_unmount(struct st_softc *st, boolean eject) |
| 889 | { |
| 890 | struct scsipi_periph *periph = st->sc_periph; |
| 891 | int nmarks; |
| 892 | |
| 893 | if ((st->flags & ST_MOUNTED) == 0) |
| 894 | return; |
| 895 | SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n" )); |
| 896 | st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY); |
| 897 | st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY); |
| 898 | |
| 899 | /* |
| 900 | * Section 9.3.3 of the SCSI specs states that a device shall return |
| 901 | * the density value specified in the last succesfull MODE SELECT |
| 902 | * after an unload operation, in case it is not able to |
| 903 | * automatically determine the density of the new medium. |
| 904 | * |
| 905 | * So we instruct the device to use the default density, which will |
| 906 | * prevent the use of stale density values (in particular, |
| 907 | * in st_touch_tape(). |
| 908 | */ |
| 909 | st->density = 0; |
| 910 | if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) { |
| 911 | aprint_error_dev(st->sc_dev, |
| 912 | "WARNING: cannot revert to default density\n" ); |
| 913 | } |
| 914 | |
| 915 | if (eject) { |
| 916 | if (!(st->quirks & ST_Q_NOPREVENT)) { |
| 917 | scsipi_prevent(periph, SPAMR_ALLOW, |
| 918 | XS_CTL_IGNORE_ILLEGAL_REQUEST | |
| 919 | XS_CTL_IGNORE_NOT_READY); |
| 920 | } |
| 921 | st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY); |
| 922 | st->blkno = st->fileno = (daddr_t) -1; |
| 923 | } else { |
| 924 | st->blkno = st->fileno = (daddr_t) 0; |
| 925 | } |
| 926 | st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT); |
| 927 | periph->periph_flags &= ~PERIPH_MEDIA_LOADED; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Given all we know about the device, media, mode, 'quirks' and |
| 932 | * initial operation, make a decision as to how we should be set |
| 933 | * to run (regarding blocking and EOD marks) |
| 934 | */ |
| 935 | int |
| 936 | st_decide_mode(struct st_softc *st, boolean first_read) |
| 937 | { |
| 938 | |
| 939 | SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n" )); |
| 940 | |
| 941 | /* |
| 942 | * If the drive can only handle fixed-length blocks and only at |
| 943 | * one size, perhaps we should just do that. |
| 944 | */ |
| 945 | if (st->blkmin && (st->blkmin == st->blkmax)) { |
| 946 | st->flags |= ST_FIXEDBLOCKS; |
| 947 | st->blksize = st->blkmin; |
| 948 | SC_DEBUG(st->sc_periph, SCSIPI_DB3, |
| 949 | ("blkmin == blkmax of %d\n" , st->blkmin)); |
| 950 | goto done; |
| 951 | } |
| 952 | /* |
| 953 | * If the tape density mandates (or even suggests) use of fixed |
| 954 | * or variable-length blocks, comply. |
| 955 | */ |
| 956 | switch (st->density) { |
| 957 | case HALFINCH_800: |
| 958 | case HALFINCH_1600: |
| 959 | case HALFINCH_6250: |
| 960 | case DDS: |
| 961 | st->flags &= ~ST_FIXEDBLOCKS; |
| 962 | st->blksize = 0; |
| 963 | SC_DEBUG(st->sc_periph, SCSIPI_DB3, |
| 964 | ("density specified variable\n" )); |
| 965 | goto done; |
| 966 | case QIC_11: |
| 967 | case QIC_24: |
| 968 | case QIC_120: |
| 969 | case QIC_150: |
| 970 | case QIC_525: |
| 971 | case QIC_1320: |
| 972 | case QIC_3095: |
| 973 | case QIC_3220: |
| 974 | st->flags |= ST_FIXEDBLOCKS; |
| 975 | if (st->media_blksize > 0) |
| 976 | st->blksize = st->media_blksize; |
| 977 | else |
| 978 | st->blksize = DEF_FIXED_BSIZE; |
| 979 | SC_DEBUG(st->sc_periph, SCSIPI_DB3, |
| 980 | ("density specified fixed\n" )); |
| 981 | goto done; |
| 982 | } |
| 983 | /* |
| 984 | * If we're about to read the tape, perhaps we should choose |
| 985 | * fixed or variable-length blocks and block size according to |
| 986 | * what the drive found on the tape. |
| 987 | */ |
| 988 | if (first_read && |
| 989 | (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) || |
| 990 | (st->media_blksize == DEF_FIXED_BSIZE) || |
| 991 | (st->media_blksize == 1024))) { |
| 992 | if (st->media_blksize > 0) |
| 993 | st->flags |= ST_FIXEDBLOCKS; |
| 994 | else |
| 995 | st->flags &= ~ST_FIXEDBLOCKS; |
| 996 | st->blksize = st->media_blksize; |
| 997 | SC_DEBUG(st->sc_periph, SCSIPI_DB3, |
| 998 | ("Used media_blksize of %d\n" , st->media_blksize)); |
| 999 | goto done; |
| 1000 | } |
| 1001 | /* |
| 1002 | * We're getting no hints from any direction. Choose variable- |
| 1003 | * length blocks arbitrarily. |
| 1004 | */ |
| 1005 | st->flags &= ~ST_FIXEDBLOCKS; |
| 1006 | st->blksize = 0; |
| 1007 | SC_DEBUG(st->sc_periph, SCSIPI_DB3, |
| 1008 | ("Give up and default to variable mode\n" )); |
| 1009 | |
| 1010 | done: |
| 1011 | /* |
| 1012 | * Decide whether or not to write two file marks to signify end- |
| 1013 | * of-data. Make the decision as a function of density. If |
| 1014 | * the decision is not to use a second file mark, the SCSI BLANK |
| 1015 | * CHECK condition code will be recognized as end-of-data when |
| 1016 | * first read. |
| 1017 | * (I think this should be a by-product of fixed/variable..julian) |
| 1018 | */ |
| 1019 | switch (st->density) { |
| 1020 | /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */ |
| 1021 | case QIC_11: |
| 1022 | case QIC_24: |
| 1023 | case QIC_120: |
| 1024 | case QIC_150: |
| 1025 | case QIC_525: |
| 1026 | case QIC_1320: |
| 1027 | case QIC_3095: |
| 1028 | case QIC_3220: |
| 1029 | st->flags &= ~ST_2FM_AT_EOD; |
| 1030 | break; |
| 1031 | default: |
| 1032 | st->flags |= ST_2FM_AT_EOD; |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Actually translate the requested transfer into |
| 1039 | * one the physical driver can understand |
| 1040 | * The transfer is described by a buf and will include |
| 1041 | * only one physical transfer. |
| 1042 | */ |
| 1043 | static void |
| 1044 | ststrategy(struct buf *bp) |
| 1045 | { |
| 1046 | struct st_softc *st = device_lookup_private(&st_cd, STUNIT(bp->b_dev)); |
| 1047 | struct scsipi_periph *periph = st->sc_periph; |
| 1048 | struct scsipi_channel *chan = periph->periph_channel; |
| 1049 | |
| 1050 | SC_DEBUG(periph, SCSIPI_DB1, |
| 1051 | ("ststrategy %d bytes @ blk %" PRId64 "\n" , bp->b_bcount, |
| 1052 | bp->b_blkno)); |
| 1053 | /* If it's a null transfer, return immediately */ |
| 1054 | if (bp->b_bcount == 0) |
| 1055 | goto abort; |
| 1056 | |
| 1057 | /* If offset is negative, error */ |
| 1058 | if (bp->b_blkno < 0) { |
| 1059 | bp->b_error = EINVAL; |
| 1060 | goto abort; |
| 1061 | } |
| 1062 | |
| 1063 | /* Odd sized request on fixed drives are verboten */ |
| 1064 | if (st->flags & ST_FIXEDBLOCKS) { |
| 1065 | if (bp->b_bcount % st->blksize) { |
| 1066 | aprint_error_dev(st->sc_dev, "bad request, must be multiple of %d\n" , |
| 1067 | st->blksize); |
| 1068 | bp->b_error = EIO; |
| 1069 | goto abort; |
| 1070 | } |
| 1071 | } |
| 1072 | /* as are out-of-range requests on variable drives. */ |
| 1073 | else if (bp->b_bcount < st->blkmin || |
| 1074 | (st->blkmax && bp->b_bcount > st->blkmax)) { |
| 1075 | aprint_error_dev(st->sc_dev, "bad request, must be between %d and %d\n" , |
| 1076 | st->blkmin, st->blkmax); |
| 1077 | bp->b_error = EIO; |
| 1078 | goto abort; |
| 1079 | } |
| 1080 | mutex_enter(chan_mtx(chan)); |
| 1081 | |
| 1082 | /* |
| 1083 | * Place it in the queue of activities for this tape |
| 1084 | * at the end (a bit silly because we only have on user.. |
| 1085 | * (but it could fork())) |
| 1086 | */ |
| 1087 | bufq_put(st->buf_queue, bp); |
| 1088 | |
| 1089 | /* |
| 1090 | * Tell the device to get going on the transfer if it's |
| 1091 | * not doing anything, otherwise just wait for completion |
| 1092 | * (All a bit silly if we're only allowing 1 open but..) |
| 1093 | */ |
| 1094 | ststart(periph); |
| 1095 | |
| 1096 | mutex_exit(chan_mtx(chan)); |
| 1097 | return; |
| 1098 | abort: |
| 1099 | /* |
| 1100 | * Reset the residue because we didn't do anything, |
| 1101 | * and send the buffer back as done. |
| 1102 | */ |
| 1103 | bp->b_resid = bp->b_bcount; |
| 1104 | biodone(bp); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * ststart looks to see if there is a buf waiting for the device |
| 1110 | * and that the device is not already busy. If both are true, |
| 1111 | * It dequeues the buf and creates a scsi command to perform the |
| 1112 | * transfer required. The transfer request will call scsipi_done |
| 1113 | * on completion, which will in turn call this routine again |
| 1114 | * so that the next queued transfer is performed. |
| 1115 | * The bufs are queued by the strategy routine (ststrategy) |
| 1116 | * |
| 1117 | * This routine is also called after other non-queued requests |
| 1118 | * have been made of the scsi driver, to ensure that the queue |
| 1119 | * continues to be drained. |
| 1120 | * ststart() is called with channel lock held |
| 1121 | */ |
| 1122 | static void |
| 1123 | ststart(struct scsipi_periph *periph) |
| 1124 | { |
| 1125 | struct st_softc *st = device_private(periph->periph_dev); |
| 1126 | struct buf *bp; |
| 1127 | struct scsi_rw_tape cmd; |
| 1128 | struct scsipi_xfer *xs; |
| 1129 | int flags, error __diagused; |
| 1130 | |
| 1131 | SC_DEBUG(periph, SCSIPI_DB2, ("ststart " )); |
| 1132 | /* See if there is a buf to do and we are not already doing one */ |
| 1133 | while (periph->periph_active < periph->periph_openings) { |
| 1134 | /* if a special awaits, let it proceed first */ |
| 1135 | if (periph->periph_flags & PERIPH_WAITING) { |
| 1136 | periph->periph_flags &= ~PERIPH_WAITING; |
| 1137 | cv_broadcast(periph_cv_periph(periph)); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * If the device has been unmounted by the user |
| 1143 | * then throw away all requests until done. |
| 1144 | */ |
| 1145 | if (__predict_false((st->flags & ST_MOUNTED) == 0 || |
| 1146 | (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { |
| 1147 | if ((bp = bufq_get(st->buf_queue)) != NULL) { |
| 1148 | /* make sure that one implies the other.. */ |
| 1149 | periph->periph_flags &= ~PERIPH_MEDIA_LOADED; |
| 1150 | bp->b_error = EIO; |
| 1151 | bp->b_resid = bp->b_bcount; |
| 1152 | biodone(bp); |
| 1153 | continue; |
| 1154 | } else |
| 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | if ((bp = bufq_peek(st->buf_queue)) == NULL) |
| 1159 | return; |
| 1160 | |
| 1161 | iostat_busy(st->stats); |
| 1162 | |
| 1163 | /* |
| 1164 | * only FIXEDBLOCK devices have pending I/O or space operations. |
| 1165 | */ |
| 1166 | if (st->flags & ST_FIXEDBLOCKS) { |
| 1167 | /* |
| 1168 | * If we are at a filemark but have not reported it yet |
| 1169 | * then we should report it now |
| 1170 | */ |
| 1171 | if (st->flags & ST_AT_FILEMARK) { |
| 1172 | if ((bp->b_flags & B_READ) == B_WRITE) { |
| 1173 | /* |
| 1174 | * Handling of ST_AT_FILEMARK in |
| 1175 | * st_space will fill in the right file |
| 1176 | * mark count. |
| 1177 | * Back up over filemark |
| 1178 | */ |
| 1179 | if (st_space(st, 0, SP_FILEMARKS, 0)) { |
| 1180 | bufq_get(st->buf_queue); |
| 1181 | bp->b_error = EIO; |
| 1182 | bp->b_resid = bp->b_bcount; |
| 1183 | biodone(bp); |
| 1184 | continue; |
| 1185 | } |
| 1186 | } else { |
| 1187 | bufq_get(st->buf_queue); |
| 1188 | bp->b_resid = bp->b_bcount; |
| 1189 | bp->b_error = 0; |
| 1190 | st->flags &= ~ST_AT_FILEMARK; |
| 1191 | biodone(bp); |
| 1192 | continue; /* seek more work */ |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | /* |
| 1197 | * If we are at EOM but have not reported it |
| 1198 | * yet then we should report it now. |
| 1199 | */ |
| 1200 | if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) { |
| 1201 | bufq_get(st->buf_queue); |
| 1202 | bp->b_resid = bp->b_bcount; |
| 1203 | if (st->flags & ST_EIO_PENDING) |
| 1204 | bp->b_error = EIO; |
| 1205 | st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING); |
| 1206 | biodone(bp); |
| 1207 | continue; /* seek more work */ |
| 1208 | } |
| 1209 | |
| 1210 | /* Fill out the scsi command */ |
| 1211 | memset(&cmd, 0, sizeof(cmd)); |
| 1212 | flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC; |
| 1213 | if ((bp->b_flags & B_READ) == B_WRITE) { |
| 1214 | cmd.opcode = WRITE; |
| 1215 | st->flags &= ~ST_FM_WRITTEN; |
| 1216 | flags |= XS_CTL_DATA_OUT; |
| 1217 | } else { |
| 1218 | cmd.opcode = READ; |
| 1219 | flags |= XS_CTL_DATA_IN; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Handle "fixed-block-mode" tape drives by using the |
| 1224 | * block count instead of the length. |
| 1225 | */ |
| 1226 | if (st->flags & ST_FIXEDBLOCKS) { |
| 1227 | cmd.byte2 |= SRW_FIXED; |
| 1228 | _lto3b(bp->b_bcount / st->blksize, cmd.len); |
| 1229 | } else |
| 1230 | _lto3b(bp->b_bcount, cmd.len); |
| 1231 | |
| 1232 | /* Clear 'position updated' indicator */ |
| 1233 | st->flags &= ~ST_POSUPDATED; |
| 1234 | |
| 1235 | /* go ask the adapter to do all this for us */ |
| 1236 | xs = scsipi_make_xs_locked(periph, |
| 1237 | (struct scsipi_generic *)&cmd, sizeof(cmd), |
| 1238 | (u_char *)bp->b_data, bp->b_bcount, |
| 1239 | 0, ST_IO_TIME, bp, flags); |
| 1240 | if (__predict_false(xs == NULL)) { |
| 1241 | /* |
| 1242 | * out of memory. Keep this buffer in the queue, and |
| 1243 | * retry later. |
| 1244 | */ |
| 1245 | callout_reset(&st->sc_callout, hz / 2, strestart, |
| 1246 | periph); |
| 1247 | return; |
| 1248 | } |
| 1249 | /* |
| 1250 | * need to dequeue the buffer before queuing the command, |
| 1251 | * because cdstart may be called recursively from the |
| 1252 | * HBA driver |
| 1253 | */ |
| 1254 | #ifdef DIAGNOSTIC |
| 1255 | if (bufq_get(st->buf_queue) != bp) |
| 1256 | panic("ststart(): dequeued wrong buf" ); |
| 1257 | #else |
| 1258 | bufq_get(st->buf_queue); |
| 1259 | #endif |
| 1260 | error = scsipi_execute_xs(xs); |
| 1261 | /* with a scsipi_xfer preallocated, scsipi_command can't fail */ |
| 1262 | KASSERT(error == 0); |
| 1263 | } /* go back and see if we can cram more work in.. */ |
| 1264 | } |
| 1265 | |
| 1266 | static void |
| 1267 | strestart(void *v) |
| 1268 | { |
| 1269 | struct scsipi_periph *periph = (struct scsipi_periph *)v; |
| 1270 | struct scsipi_channel *chan = periph->periph_channel; |
| 1271 | |
| 1272 | mutex_enter(chan_mtx(chan)); |
| 1273 | ststart((struct scsipi_periph *)v); |
| 1274 | mutex_exit(chan_mtx(chan)); |
| 1275 | } |
| 1276 | |
| 1277 | static void |
| 1278 | stdone(struct scsipi_xfer *xs, int error) |
| 1279 | { |
| 1280 | struct st_softc *st = device_private(xs->xs_periph->periph_dev); |
| 1281 | struct buf *bp = xs->bp; |
| 1282 | |
| 1283 | if (bp) { |
| 1284 | bp->b_error = error; |
| 1285 | bp->b_resid = xs->resid; |
| 1286 | /* |
| 1287 | * buggy device ? A SDLT320 can report an info |
| 1288 | * field of 0x3de8000 on a Media Error/Write Error |
| 1289 | * for this CBD: 0x0a 00 00 80 00 00 |
| 1290 | */ |
| 1291 | if (bp->b_resid > bp->b_bcount || bp->b_resid < 0) |
| 1292 | bp->b_resid = bp->b_bcount; |
| 1293 | |
| 1294 | if ((bp->b_flags & B_READ) == B_WRITE) |
| 1295 | st->flags |= ST_WRITTEN; |
| 1296 | else |
| 1297 | st->flags &= ~ST_WRITTEN; |
| 1298 | |
| 1299 | iostat_unbusy(st->stats, bp->b_bcount, |
| 1300 | ((bp->b_flags & B_READ) == B_READ)); |
| 1301 | |
| 1302 | rnd_add_uint32(&st->rnd_source, bp->b_blkno); |
| 1303 | |
| 1304 | if ((st->flags & ST_POSUPDATED) == 0) { |
| 1305 | if (error) { |
| 1306 | st->fileno = st->blkno = -1; |
| 1307 | } else if (st->blkno != -1) { |
| 1308 | if (st->flags & ST_FIXEDBLOCKS) |
| 1309 | st->blkno += |
| 1310 | (bp->b_bcount / st->blksize); |
| 1311 | else |
| 1312 | st->blkno++; |
| 1313 | } |
| 1314 | } |
| 1315 | biodone(bp); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static int |
| 1320 | stread(dev_t dev, struct uio *uio, int iomode) |
| 1321 | { |
| 1322 | struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev)); |
| 1323 | |
| 1324 | return physio(ststrategy, NULL, dev, B_READ, |
| 1325 | st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio); |
| 1326 | } |
| 1327 | |
| 1328 | static int |
| 1329 | stwrite(dev_t dev, struct uio *uio, int iomode) |
| 1330 | { |
| 1331 | struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev)); |
| 1332 | |
| 1333 | return physio(ststrategy, NULL, dev, B_WRITE, |
| 1334 | st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio); |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Perform special action on behalf of the user; |
| 1339 | * knows about the internals of this device |
| 1340 | */ |
| 1341 | static int |
| 1342 | stioctl(dev_t dev, u_long cmd, void *arg, int flag, struct lwp *l) |
| 1343 | { |
| 1344 | int error = 0; |
| 1345 | int unit; |
| 1346 | int number, nmarks, dsty; |
| 1347 | int flags; |
| 1348 | struct st_softc *st; |
| 1349 | int hold_blksize; |
| 1350 | uint8_t hold_density; |
| 1351 | struct mtop *mt = (struct mtop *) arg; |
| 1352 | |
| 1353 | /* Find the device that the user is talking about */ |
| 1354 | flags = 0; /* give error messages, act on errors etc. */ |
| 1355 | unit = STUNIT(dev); |
| 1356 | dsty = STDSTY(dev); |
| 1357 | st = device_lookup_private(&st_cd, unit); |
| 1358 | hold_blksize = st->blksize; |
| 1359 | hold_density = st->density; |
| 1360 | |
| 1361 | switch ((u_int)cmd) { |
| 1362 | case MTIOCGET: { |
| 1363 | struct mtget *g = (struct mtget *) arg; |
| 1364 | /* |
| 1365 | * (to get the current state of READONLY) |
| 1366 | */ |
| 1367 | error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT); |
| 1368 | if (error) { |
| 1369 | /* |
| 1370 | * Ignore the error if in control mode; |
| 1371 | * this is mandated by st(4). |
| 1372 | */ |
| 1373 | if (STMODE(dev) != CTRL_MODE) |
| 1374 | break; |
| 1375 | error = 0; |
| 1376 | } |
| 1377 | SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n" )); |
| 1378 | memset(g, 0, sizeof(struct mtget)); |
| 1379 | g->mt_type = MT_ISAR; /* Ultrix compat *//*? */ |
| 1380 | g->mt_blksiz = st->blksize; |
| 1381 | g->mt_density = st->density; |
| 1382 | g->mt_mblksiz[0] = st->modes[0].blksize; |
| 1383 | g->mt_mblksiz[1] = st->modes[1].blksize; |
| 1384 | g->mt_mblksiz[2] = st->modes[2].blksize; |
| 1385 | g->mt_mblksiz[3] = st->modes[3].blksize; |
| 1386 | g->mt_mdensity[0] = st->modes[0].density; |
| 1387 | g->mt_mdensity[1] = st->modes[1].density; |
| 1388 | g->mt_mdensity[2] = st->modes[2].density; |
| 1389 | g->mt_mdensity[3] = st->modes[3].density; |
| 1390 | g->mt_fileno = st->fileno; |
| 1391 | g->mt_blkno = st->blkno; |
| 1392 | if (st->flags & ST_READONLY) |
| 1393 | g->mt_dsreg |= MT_DS_RDONLY; |
| 1394 | if (st->flags & ST_MOUNTED) |
| 1395 | g->mt_dsreg |= MT_DS_MOUNTED; |
| 1396 | g->mt_resid = st->mt_resid; |
| 1397 | g->mt_erreg = st->mt_erreg; |
| 1398 | /* |
| 1399 | * clear latched errors. |
| 1400 | */ |
| 1401 | st->mt_resid = 0; |
| 1402 | st->mt_erreg = 0; |
| 1403 | st->asc = 0; |
| 1404 | st->ascq = 0; |
| 1405 | break; |
| 1406 | } |
| 1407 | case MTIOCTOP: { |
| 1408 | SC_DEBUG(st->sc_periph, SCSIPI_DB1, |
| 1409 | ("[ioctl: op=0x%x count=0x%x]\n" , mt->mt_op, |
| 1410 | mt->mt_count)); |
| 1411 | |
| 1412 | /* compat: in U*x it is a short */ |
| 1413 | number = mt->mt_count; |
| 1414 | switch ((short) (mt->mt_op)) { |
| 1415 | case MTWEOF: /* write an end-of-file record */ |
| 1416 | error = st_write_filemarks(st, number, flags); |
| 1417 | break; |
| 1418 | case MTBSF: /* backward space file */ |
| 1419 | number = -number; |
| 1420 | case MTFSF: /* forward space file */ |
| 1421 | error = st_check_eod(st, FALSE, &nmarks, flags); |
| 1422 | if (!error) |
| 1423 | error = st_space(st, number - nmarks, |
| 1424 | SP_FILEMARKS, flags); |
| 1425 | break; |
| 1426 | case MTBSR: /* backward space record */ |
| 1427 | number = -number; |
| 1428 | case MTFSR: /* forward space record */ |
| 1429 | error = st_check_eod(st, true, &nmarks, flags); |
| 1430 | if (!error) |
| 1431 | error = st_space(st, number, SP_BLKS, flags); |
| 1432 | break; |
| 1433 | case MTREW: /* rewind */ |
| 1434 | error = st_rewind(st, 0, flags); |
| 1435 | break; |
| 1436 | case MTOFFL: /* rewind and put the drive offline */ |
| 1437 | st_unmount(st, EJECT); |
| 1438 | break; |
| 1439 | case MTNOP: /* no operation, sets status only */ |
| 1440 | break; |
| 1441 | case MTRETEN: /* retension the tape */ |
| 1442 | error = st_load(st, LD_RETENSION, flags); |
| 1443 | if (!error) |
| 1444 | error = st_load(st, LD_LOAD, flags); |
| 1445 | break; |
| 1446 | case MTEOM: /* forward space to end of media */ |
| 1447 | error = st_check_eod(st, FALSE, &nmarks, flags); |
| 1448 | if (!error) |
| 1449 | error = st_space(st, 1, SP_EOM, flags); |
| 1450 | break; |
| 1451 | case MTCACHE: /* enable controller cache */ |
| 1452 | st->flags &= ~ST_DONTBUFFER; |
| 1453 | goto try_new_value; |
| 1454 | case MTNOCACHE: /* disable controller cache */ |
| 1455 | st->flags |= ST_DONTBUFFER; |
| 1456 | goto try_new_value; |
| 1457 | case MTERASE: /* erase volume */ |
| 1458 | error = st_erase(st, number, flags); |
| 1459 | break; |
| 1460 | case MTSETBSIZ: /* Set block size for device */ |
| 1461 | #ifdef NOTYET |
| 1462 | if (!(st->flags & ST_NEW_MOUNT)) { |
| 1463 | uprintf("re-mount tape before changing " |
| 1464 | "blocksize" ); |
| 1465 | error = EINVAL; |
| 1466 | break; |
| 1467 | } |
| 1468 | #endif |
| 1469 | if (number == 0) |
| 1470 | st->flags &= ~ST_FIXEDBLOCKS; |
| 1471 | else { |
| 1472 | if ((st->blkmin || st->blkmax) && |
| 1473 | (number < st->blkmin || |
| 1474 | number > st->blkmax)) { |
| 1475 | error = EINVAL; |
| 1476 | break; |
| 1477 | } |
| 1478 | st->flags |= ST_FIXEDBLOCKS; |
| 1479 | } |
| 1480 | st->blksize = number; |
| 1481 | st->flags |= ST_BLOCK_SET; /*XXX */ |
| 1482 | goto try_new_value; |
| 1483 | case MTSETDNSTY: /* Set density for device and mode */ |
| 1484 | /* |
| 1485 | * Any number >= 0 and <= 0xff is legal. Numbers |
| 1486 | * above 0x80 are 'vendor unique'. |
| 1487 | */ |
| 1488 | if (number < 0 || number > 255) { |
| 1489 | error = EINVAL; |
| 1490 | break; |
| 1491 | } else |
| 1492 | st->density = number; |
| 1493 | goto try_new_value; |
| 1494 | case MTCMPRESS: |
| 1495 | error = st->ops(st, (number == 0) ? |
| 1496 | ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON, |
| 1497 | XS_CTL_SILENT); |
| 1498 | break; |
| 1499 | case MTEWARN: |
| 1500 | if (number) |
| 1501 | st->flags |= ST_EARLYWARN; |
| 1502 | else |
| 1503 | st->flags &= ~ST_EARLYWARN; |
| 1504 | break; |
| 1505 | |
| 1506 | default: |
| 1507 | error = EINVAL; |
| 1508 | } |
| 1509 | break; |
| 1510 | } |
| 1511 | case MTIOCIEOT: |
| 1512 | case MTIOCEEOT: |
| 1513 | break; |
| 1514 | case MTIOCRDSPOS: |
| 1515 | error = st_rdpos(st, 0, (uint32_t *)arg); |
| 1516 | break; |
| 1517 | case MTIOCRDHPOS: |
| 1518 | error = st_rdpos(st, 1, (uint32_t *)arg); |
| 1519 | break; |
| 1520 | case MTIOCSLOCATE: |
| 1521 | error = st_setpos(st, 0, (uint32_t *)arg); |
| 1522 | break; |
| 1523 | case MTIOCHLOCATE: |
| 1524 | error = st_setpos(st, 1, (uint32_t *)arg); |
| 1525 | break; |
| 1526 | default: |
| 1527 | error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg, flag, l); |
| 1528 | break; |
| 1529 | } |
| 1530 | return error; |
| 1531 | |
| 1532 | try_new_value: |
| 1533 | /* |
| 1534 | * Check that the mode being asked for is aggreeable to the |
| 1535 | * drive. If not, put it back the way it was. |
| 1536 | * |
| 1537 | * If in control mode, we can make (persistent) mode changes |
| 1538 | * even if no medium is loaded (see st(4)). |
| 1539 | */ |
| 1540 | if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) && |
| 1541 | (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) { |
| 1542 | /* put it back as it was */ |
| 1543 | aprint_error_dev(st->sc_dev, "cannot set selected mode\n" ); |
| 1544 | st->density = hold_density; |
| 1545 | st->blksize = hold_blksize; |
| 1546 | if (st->blksize) |
| 1547 | st->flags |= ST_FIXEDBLOCKS; |
| 1548 | else |
| 1549 | st->flags &= ~ST_FIXEDBLOCKS; |
| 1550 | return error; |
| 1551 | } |
| 1552 | /* |
| 1553 | * As the drive liked it, if we are setting a new default, |
| 1554 | * set it into the structures as such. |
| 1555 | * |
| 1556 | * The means for deciding this are not finalised yet- but |
| 1557 | * if the device was opened in Control Mode, the values |
| 1558 | * are persistent now across mounts. |
| 1559 | */ |
| 1560 | if (STMODE(dev) == CTRL_MODE) { |
| 1561 | switch ((short) (mt->mt_op)) { |
| 1562 | case MTSETBSIZ: |
| 1563 | st->modes[dsty].blksize = st->blksize; |
| 1564 | st->modeflags[dsty] |= BLKSIZE_SET_BY_USER; |
| 1565 | break; |
| 1566 | case MTSETDNSTY: |
| 1567 | st->modes[dsty].density = st->density; |
| 1568 | st->modeflags[dsty] |= DENSITY_SET_BY_USER; |
| 1569 | break; |
| 1570 | } |
| 1571 | } |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | /* Do a synchronous read. */ |
| 1576 | static int |
| 1577 | st_read(struct st_softc *st, char *bf, int size, int flags) |
| 1578 | { |
| 1579 | struct scsi_rw_tape cmd; |
| 1580 | |
| 1581 | /* If it's a null transfer, return immediatly */ |
| 1582 | if (size == 0) |
| 1583 | return 0; |
| 1584 | memset(&cmd, 0, sizeof(cmd)); |
| 1585 | cmd.opcode = READ; |
| 1586 | if (st->flags & ST_FIXEDBLOCKS) { |
| 1587 | cmd.byte2 |= SRW_FIXED; |
| 1588 | _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE), |
| 1589 | cmd.len); |
| 1590 | } else |
| 1591 | _lto3b(size, cmd.len); |
| 1592 | return scsipi_command(st->sc_periph, |
| 1593 | (void *)&cmd, sizeof(cmd), (void *)bf, size, 0, ST_IO_TIME, NULL, |
| 1594 | flags | XS_CTL_DATA_IN); |
| 1595 | } |
| 1596 | |
| 1597 | /* issue an erase command */ |
| 1598 | static int |
| 1599 | st_erase(struct st_softc *st, int full, int flags) |
| 1600 | { |
| 1601 | int tmo; |
| 1602 | struct scsi_erase cmd; |
| 1603 | |
| 1604 | /* |
| 1605 | * Full erase means set LONG bit in erase command, which asks |
| 1606 | * the drive to erase the entire unit. Without this bit, we're |
| 1607 | * asking the drive to write an erase gap. |
| 1608 | */ |
| 1609 | memset(&cmd, 0, sizeof(cmd)); |
| 1610 | cmd.opcode = ERASE; |
| 1611 | if (full) { |
| 1612 | cmd.byte2 = SE_LONG; |
| 1613 | tmo = ST_SPC_TIME; |
| 1614 | } else |
| 1615 | tmo = ST_IO_TIME; |
| 1616 | |
| 1617 | /* |
| 1618 | * XXX We always do this asynchronously, for now, unless the device |
| 1619 | * has the ST_Q_ERASE_NOIMM quirk. How long should we wait if we |
| 1620 | * want to (eventually) to it synchronously? |
| 1621 | */ |
| 1622 | if ((st->quirks & ST_Q_ERASE_NOIMM) == 0) |
| 1623 | cmd.byte2 |= SE_IMMED; |
| 1624 | |
| 1625 | return scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1626 | ST_RETRIES, tmo, NULL, flags); |
| 1627 | } |
| 1628 | |
| 1629 | /* skip N blocks/filemarks/seq filemarks/eom */ |
| 1630 | static int |
| 1631 | st_space(struct st_softc *st, int number, u_int what, int flags) |
| 1632 | { |
| 1633 | struct scsi_space cmd; |
| 1634 | int error; |
| 1635 | |
| 1636 | switch (what) { |
| 1637 | case SP_BLKS: |
| 1638 | if (st->flags & ST_PER_ACTION) { |
| 1639 | if (number > 0) { |
| 1640 | st->flags &= ~ST_PER_ACTION; |
| 1641 | return EIO; |
| 1642 | } else if (number < 0) { |
| 1643 | if (st->flags & ST_AT_FILEMARK) { |
| 1644 | /* |
| 1645 | * Handling of ST_AT_FILEMARK |
| 1646 | * in st_space will fill in the |
| 1647 | * right file mark count. |
| 1648 | */ |
| 1649 | error = st_space(st, 0, SP_FILEMARKS, |
| 1650 | flags); |
| 1651 | if (error) |
| 1652 | return error; |
| 1653 | } |
| 1654 | if (st->flags & ST_BLANK_READ) { |
| 1655 | st->flags &= ~ST_BLANK_READ; |
| 1656 | return EIO; |
| 1657 | } |
| 1658 | st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING); |
| 1659 | } |
| 1660 | } |
| 1661 | break; |
| 1662 | case SP_FILEMARKS: |
| 1663 | if (st->flags & ST_EIO_PENDING) { |
| 1664 | if (number > 0) { |
| 1665 | /* pretend we just discovered the error */ |
| 1666 | st->flags &= ~ST_EIO_PENDING; |
| 1667 | return EIO; |
| 1668 | } else if (number < 0) { |
| 1669 | /* back away from the error */ |
| 1670 | st->flags &= ~ST_EIO_PENDING; |
| 1671 | } |
| 1672 | } |
| 1673 | if (st->flags & ST_AT_FILEMARK) { |
| 1674 | st->flags &= ~ST_AT_FILEMARK; |
| 1675 | number--; |
| 1676 | } |
| 1677 | if ((st->flags & ST_BLANK_READ) && (number < 0)) { |
| 1678 | /* back away from unwritten tape */ |
| 1679 | st->flags &= ~ST_BLANK_READ; |
| 1680 | number++; /* XXX dubious */ |
| 1681 | } |
| 1682 | break; |
| 1683 | case SP_EOM: |
| 1684 | if (st->flags & ST_EOM_PENDING) { |
| 1685 | /* we're already there */ |
| 1686 | st->flags &= ~ST_EOM_PENDING; |
| 1687 | return 0; |
| 1688 | } |
| 1689 | if (st->flags & ST_EIO_PENDING) { |
| 1690 | /* pretend we just discovered the error */ |
| 1691 | st->flags &= ~ST_EIO_PENDING; |
| 1692 | return EIO; |
| 1693 | } |
| 1694 | if (st->flags & ST_AT_FILEMARK) |
| 1695 | st->flags &= ~ST_AT_FILEMARK; |
| 1696 | break; |
| 1697 | } |
| 1698 | if (number == 0) |
| 1699 | return 0; |
| 1700 | |
| 1701 | memset(&cmd, 0, sizeof(cmd)); |
| 1702 | cmd.opcode = SPACE; |
| 1703 | cmd.byte2 = what; |
| 1704 | _lto3b(number, cmd.number); |
| 1705 | |
| 1706 | st->flags &= ~ST_POSUPDATED; |
| 1707 | st->last_ctl_resid = 0; |
| 1708 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1709 | 0, ST_SPC_TIME, NULL, flags); |
| 1710 | |
| 1711 | if (error == 0 && (st->flags & ST_POSUPDATED) == 0) { |
| 1712 | number = number - st->last_ctl_resid; |
| 1713 | if (what == SP_BLKS) { |
| 1714 | if (st->blkno != -1) |
| 1715 | st->blkno += number; |
| 1716 | } else if (what == SP_FILEMARKS) { |
| 1717 | if (st->fileno != -1) { |
| 1718 | st->fileno += number; |
| 1719 | if (number > 0) |
| 1720 | st->blkno = 0; |
| 1721 | else if (number < 0) |
| 1722 | st->blkno = -1; |
| 1723 | } |
| 1724 | } else if (what == SP_EOM) { |
| 1725 | /* This loses us relative position. */ |
| 1726 | st->fileno = st->blkno = -1; |
| 1727 | } |
| 1728 | } |
| 1729 | return error; |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * write N filemarks |
| 1734 | */ |
| 1735 | static int |
| 1736 | st_write_filemarks(struct st_softc *st, int number, int flags) |
| 1737 | { |
| 1738 | int error; |
| 1739 | struct scsi_write_filemarks cmd; |
| 1740 | |
| 1741 | /* |
| 1742 | * It's hard to write a negative number of file marks. |
| 1743 | * Don't try. |
| 1744 | */ |
| 1745 | if (number < 0) |
| 1746 | return EINVAL; |
| 1747 | switch (number) { |
| 1748 | case 0: /* really a command to sync the drive's buffers */ |
| 1749 | break; |
| 1750 | case 1: |
| 1751 | if (st->flags & ST_FM_WRITTEN) /* already have one down */ |
| 1752 | st->flags &= ~ST_WRITTEN; |
| 1753 | else |
| 1754 | st->flags |= ST_FM_WRITTEN; |
| 1755 | st->flags &= ~ST_PER_ACTION; |
| 1756 | break; |
| 1757 | default: |
| 1758 | st->flags &= ~(ST_PER_ACTION | ST_WRITTEN); |
| 1759 | } |
| 1760 | |
| 1761 | memset(&cmd, 0, sizeof(cmd)); |
| 1762 | cmd.opcode = WRITE_FILEMARKS; |
| 1763 | if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(st->sc_periph)) == |
| 1764 | SCSIPI_BUSTYPE_ATAPI) |
| 1765 | cmd.byte2 = SR_IMMED; |
| 1766 | /* |
| 1767 | * The ATAPI Onstream DI-30 doesn't support writing filemarks, but |
| 1768 | * WRITE_FILEMARKS is still used to flush the buffer |
| 1769 | */ |
| 1770 | if ((st->quirks & ST_Q_NOFILEMARKS) == 0) |
| 1771 | _lto3b(number, cmd.number); |
| 1772 | |
| 1773 | /* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */ |
| 1774 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1775 | 0, ST_IO_TIME * 4, NULL, flags); |
| 1776 | if (error == 0 && st->fileno != -1) |
| 1777 | st->fileno += number; |
| 1778 | return error; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * Make sure the right number of file marks is on tape if the |
| 1783 | * tape has been written. If the position argument is true, |
| 1784 | * leave the tape positioned where it was originally. |
| 1785 | * |
| 1786 | * nmarks returns the number of marks to skip (or, if position |
| 1787 | * true, which were skipped) to get back original position. |
| 1788 | */ |
| 1789 | static int |
| 1790 | st_check_eod(struct st_softc *st, boolean position, int *nmarks, int flags) |
| 1791 | { |
| 1792 | int error; |
| 1793 | |
| 1794 | switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) { |
| 1795 | default: |
| 1796 | *nmarks = 0; |
| 1797 | return 0; |
| 1798 | case ST_WRITTEN: |
| 1799 | case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD: |
| 1800 | *nmarks = 1; |
| 1801 | break; |
| 1802 | case ST_WRITTEN | ST_2FM_AT_EOD: |
| 1803 | *nmarks = 2; |
| 1804 | } |
| 1805 | error = st_write_filemarks(st, *nmarks, flags); |
| 1806 | if (position && !error) |
| 1807 | error = st_space(st, -*nmarks, SP_FILEMARKS, flags); |
| 1808 | return error; |
| 1809 | } |
| 1810 | |
| 1811 | /* load/unload/retension */ |
| 1812 | static int |
| 1813 | st_load(struct st_softc *st, u_int type, int flags) |
| 1814 | { |
| 1815 | int error; |
| 1816 | struct scsi_load cmd; |
| 1817 | |
| 1818 | if (type != LD_LOAD) { |
| 1819 | int nmarks; |
| 1820 | |
| 1821 | error = st_check_eod(st, FALSE, &nmarks, flags); |
| 1822 | if (error) { |
| 1823 | aprint_error_dev(st->sc_dev, |
| 1824 | "failed to write closing filemarks at " |
| 1825 | "unload, errno=%d\n" , error); |
| 1826 | return error; |
| 1827 | } |
| 1828 | } |
| 1829 | if (st->quirks & ST_Q_IGNORE_LOADS) { |
| 1830 | if (type == LD_LOAD) |
| 1831 | /* |
| 1832 | * If we ignore loads, at least we should try a rewind. |
| 1833 | */ |
| 1834 | return st_rewind(st, 0, flags); |
| 1835 | /* otherwise, we should do what's asked of us */ |
| 1836 | } |
| 1837 | |
| 1838 | memset(&cmd, 0, sizeof(cmd)); |
| 1839 | cmd.opcode = LOAD; |
| 1840 | if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(st->sc_periph)) == |
| 1841 | SCSIPI_BUSTYPE_ATAPI) |
| 1842 | cmd.byte2 = SR_IMMED; |
| 1843 | cmd.how = type; |
| 1844 | |
| 1845 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1846 | ST_RETRIES, ST_SPC_TIME, NULL, flags); |
| 1847 | if (error) { |
| 1848 | aprint_error_dev(st->sc_dev, "error %d in st_load (op %d)\n" , |
| 1849 | error, type); |
| 1850 | } |
| 1851 | return error; |
| 1852 | } |
| 1853 | |
| 1854 | /* Rewind the device */ |
| 1855 | static int |
| 1856 | st_rewind(struct st_softc *st, u_int immediate, int flags) |
| 1857 | { |
| 1858 | struct scsi_rewind cmd; |
| 1859 | int error; |
| 1860 | int nmarks; |
| 1861 | int timeout; |
| 1862 | |
| 1863 | error = st_check_eod(st, FALSE, &nmarks, flags); |
| 1864 | if (error) { |
| 1865 | aprint_error_dev(st->sc_dev, |
| 1866 | "failed to write closing filemarks at " |
| 1867 | "rewind, errno=%d\n" , error); |
| 1868 | return error; |
| 1869 | } |
| 1870 | st->flags &= ~ST_PER_ACTION; |
| 1871 | |
| 1872 | /* If requestor asked for immediate response, set a short timeout */ |
| 1873 | timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME; |
| 1874 | |
| 1875 | /* ATAPI tapes always need immediate to be set */ |
| 1876 | if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI) |
| 1877 | immediate = SR_IMMED; |
| 1878 | |
| 1879 | memset(&cmd, 0, sizeof(cmd)); |
| 1880 | cmd.opcode = REWIND; |
| 1881 | cmd.byte2 = immediate; |
| 1882 | |
| 1883 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1884 | ST_RETRIES, timeout, NULL, flags); |
| 1885 | if (error) { |
| 1886 | aprint_error_dev(st->sc_dev, "error %d trying to rewind\n" , |
| 1887 | error); |
| 1888 | /* lost position */ |
| 1889 | st->fileno = st->blkno = -1; |
| 1890 | } else |
| 1891 | st->fileno = st->blkno = 0; |
| 1892 | return error; |
| 1893 | } |
| 1894 | |
| 1895 | static int |
| 1896 | st_rdpos(struct st_softc *st, int hard, uint32_t *blkptr) |
| 1897 | { |
| 1898 | int error; |
| 1899 | uint8_t posdata[20]; |
| 1900 | struct scsi_tape_read_position cmd; |
| 1901 | |
| 1902 | /* |
| 1903 | * We try and flush any buffered writes here if we were writing |
| 1904 | * and we're trying to get hardware block position. It eats |
| 1905 | * up performance substantially, but I'm wary of drive firmware. |
| 1906 | * |
| 1907 | * I think that *logical* block position is probably okay- |
| 1908 | * but hardware block position might have to wait for data |
| 1909 | * to hit media to be valid. Caveat Emptor. |
| 1910 | */ |
| 1911 | |
| 1912 | if (hard && (st->flags & ST_WRITTEN)) { |
| 1913 | /* First flush any pending writes... */ |
| 1914 | error = st_write_filemarks(st, 0, XS_CTL_SILENT); |
| 1915 | /* |
| 1916 | * The latter case is for 'write protected' tapes |
| 1917 | * which are too stupid to recognize a zero count |
| 1918 | * for writing filemarks as a no-op. |
| 1919 | */ |
| 1920 | if (error != 0 && error != EACCES && error != EROFS) |
| 1921 | return error; |
| 1922 | } |
| 1923 | |
| 1924 | memset(&cmd, 0, sizeof(cmd)); |
| 1925 | memset(&posdata, 0, sizeof(posdata)); |
| 1926 | cmd.opcode = READ_POSITION; |
| 1927 | if (hard) |
| 1928 | cmd.byte1 = 1; |
| 1929 | |
| 1930 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), |
| 1931 | (void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL, |
| 1932 | XS_CTL_SILENT | XS_CTL_DATA_IN); |
| 1933 | |
| 1934 | if (error == 0) { |
| 1935 | #if 0 |
| 1936 | printf("posdata:" ); |
| 1937 | for (hard = 0; hard < sizeof(posdata); hard++) |
| 1938 | printf("%02x " , posdata[hard] & 0xff); |
| 1939 | printf("\n" ); |
| 1940 | #endif |
| 1941 | if (posdata[0] & 0x4) /* Block Position Unknown */ |
| 1942 | error = EINVAL; |
| 1943 | else |
| 1944 | *blkptr = _4btol(&posdata[4]); |
| 1945 | } |
| 1946 | return error; |
| 1947 | } |
| 1948 | |
| 1949 | static int |
| 1950 | st_setpos(struct st_softc *st, int hard, uint32_t *blkptr) |
| 1951 | { |
| 1952 | int error; |
| 1953 | struct scsi_tape_locate cmd; |
| 1954 | |
| 1955 | /* |
| 1956 | * We used to try and flush any buffered writes here. |
| 1957 | * Now we push this onto user applications to either |
| 1958 | * flush the pending writes themselves (via a zero count |
| 1959 | * WRITE FILEMARKS command) or they can trust their tape |
| 1960 | * drive to do this correctly for them. |
| 1961 | * |
| 1962 | * There are very ugly performance limitations otherwise. |
| 1963 | */ |
| 1964 | |
| 1965 | memset(&cmd, 0, sizeof(cmd)); |
| 1966 | cmd.opcode = LOCATE; |
| 1967 | if (hard) |
| 1968 | cmd.byte2 = 1 << 2; |
| 1969 | _lto4b(*blkptr, cmd.blkaddr); |
| 1970 | error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, |
| 1971 | ST_RETRIES, ST_SPC_TIME, NULL, 0); |
| 1972 | /* |
| 1973 | * Note file && block number position now unknown (if |
| 1974 | * these things ever start being maintained in this driver) |
| 1975 | */ |
| 1976 | st->fileno = st->blkno = -1; |
| 1977 | return error; |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | /* |
| 1982 | * Look at the returned sense and act on the error and determine |
| 1983 | * the unix error number to pass back..., 0 (== report no error), |
| 1984 | * -1 = retry the operation, -2 continue error processing. |
| 1985 | */ |
| 1986 | static int |
| 1987 | st_interpret_sense(struct scsipi_xfer *xs) |
| 1988 | { |
| 1989 | struct scsipi_periph *periph = xs->xs_periph; |
| 1990 | struct scsi_sense_data *sense = &xs->sense.scsi_sense; |
| 1991 | struct buf *bp = xs->bp; |
| 1992 | struct st_softc *st = device_private(periph->periph_dev); |
| 1993 | int retval = EJUSTRETURN; |
| 1994 | int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0); |
| 1995 | uint8_t key; |
| 1996 | int32_t info; |
| 1997 | |
| 1998 | /* |
| 1999 | * If it isn't a extended or extended/deferred error, let |
| 2000 | * the generic code handle it. |
| 2001 | */ |
| 2002 | if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && |
| 2003 | SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) |
| 2004 | return retval; |
| 2005 | |
| 2006 | if (sense->response_code & SSD_RCODE_VALID) |
| 2007 | info = _4btol(sense->info); |
| 2008 | else |
| 2009 | info = (st->flags & ST_FIXEDBLOCKS) ? |
| 2010 | xs->datalen / st->blksize : xs->datalen; |
| 2011 | key = SSD_SENSE_KEY(sense->flags); |
| 2012 | st->mt_erreg = key; |
| 2013 | st->asc = sense->asc; |
| 2014 | st->ascq = sense->ascq; |
| 2015 | st->mt_resid = (short) info; |
| 2016 | |
| 2017 | if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) { |
| 2018 | /* Not Ready, Logical Unit Is in Process Of Becoming Ready */ |
| 2019 | if (!callout_pending(&periph->periph_callout)) |
| 2020 | scsipi_periph_freeze(periph, 1); |
| 2021 | callout_reset(&periph->periph_callout, |
| 2022 | hz, scsipi_periph_timed_thaw, periph); |
| 2023 | return ERESTART; |
| 2024 | } |
| 2025 | |
| 2026 | /* If the device is not open yet, let generic handle */ |
| 2027 | if ((periph->periph_flags & PERIPH_OPEN) == 0) |
| 2028 | return retval; |
| 2029 | |
| 2030 | xs->resid = info; |
| 2031 | if (st->flags & ST_FIXEDBLOCKS) { |
| 2032 | if (bp) { |
| 2033 | xs->resid *= st->blksize; |
| 2034 | st->last_io_resid = xs->resid; |
| 2035 | } else |
| 2036 | st->last_ctl_resid = xs->resid; |
| 2037 | if (key == SKEY_VOLUME_OVERFLOW) { |
| 2038 | st->flags |= ST_EIO_PENDING; |
| 2039 | if (bp) |
| 2040 | bp->b_resid = xs->resid; |
| 2041 | } else if (sense->flags & SSD_EOM) { |
| 2042 | if ((st->flags & ST_EARLYWARN) == 0) |
| 2043 | st->flags |= ST_EIO_PENDING; |
| 2044 | st->flags |= ST_EOM_PENDING; |
| 2045 | if (bp) { |
| 2046 | #if 0 |
| 2047 | bp->b_resid = xs->resid; |
| 2048 | #else |
| 2049 | /* |
| 2050 | * Grotesque as it seems, the few times |
| 2051 | * I've actually seen a non-zero resid, |
| 2052 | * the tape drive actually lied and had |
| 2053 | * written all the data! |
| 2054 | */ |
| 2055 | bp->b_resid = 0; |
| 2056 | #endif |
| 2057 | } |
| 2058 | } |
| 2059 | if (sense->flags & SSD_FILEMARK) { |
| 2060 | st->flags |= ST_AT_FILEMARK; |
| 2061 | if (bp) |
| 2062 | bp->b_resid = xs->resid; |
| 2063 | if (st->fileno != (daddr_t) -1) { |
| 2064 | st->fileno++; |
| 2065 | st->blkno = 0; |
| 2066 | st->flags |= ST_POSUPDATED; |
| 2067 | } |
| 2068 | } |
| 2069 | if (sense->flags & SSD_ILI) { |
| 2070 | st->flags |= ST_EIO_PENDING; |
| 2071 | if (bp) |
| 2072 | bp->b_resid = xs->resid; |
| 2073 | if (sense->response_code & SSD_RCODE_VALID && |
| 2074 | (xs->xs_control & XS_CTL_SILENT) == 0) |
| 2075 | aprint_error_dev(st->sc_dev, |
| 2076 | "block wrong size, %d blocks residual\n" , |
| 2077 | info); |
| 2078 | |
| 2079 | /* |
| 2080 | * This quirk code helps the drive read |
| 2081 | * the first tape block, regardless of |
| 2082 | * format. That is required for these |
| 2083 | * drives to return proper MODE SENSE |
| 2084 | * information. |
| 2085 | */ |
| 2086 | if ((st->quirks & ST_Q_SENSE_HELP) && |
| 2087 | (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) |
| 2088 | st->blksize -= 512; |
| 2089 | else if ((st->flags & ST_POSUPDATED) == 0) { |
| 2090 | if (st->blkno != (daddr_t) -1) { |
| 2091 | st->blkno += |
| 2092 | (xs->datalen / st->blksize); |
| 2093 | st->flags |= ST_POSUPDATED; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | /* |
| 2098 | * If data wanted and no data was transferred, do it immediately |
| 2099 | */ |
| 2100 | if (xs->datalen && xs->resid >= xs->datalen) { |
| 2101 | if (st->flags & ST_EIO_PENDING) |
| 2102 | return EIO; |
| 2103 | if (st->flags & ST_AT_FILEMARK) { |
| 2104 | if (bp) |
| 2105 | bp->b_resid = xs->resid; |
| 2106 | return 0; |
| 2107 | } |
| 2108 | } |
| 2109 | } else { /* must be variable mode */ |
| 2110 | if (bp) |
| 2111 | st->last_io_resid = xs->resid; |
| 2112 | else |
| 2113 | st->last_ctl_resid = xs->resid; |
| 2114 | if (sense->flags & SSD_EOM) { |
| 2115 | /* |
| 2116 | * The current semantics of this |
| 2117 | * driver requires EOM detection |
| 2118 | * to return EIO unless early |
| 2119 | * warning detection is enabled |
| 2120 | * for variable mode (this is always |
| 2121 | * on for fixed block mode). |
| 2122 | */ |
| 2123 | if (st->flags & ST_EARLYWARN) { |
| 2124 | st->flags |= ST_EOM_PENDING; |
| 2125 | retval = 0; |
| 2126 | } else { |
| 2127 | retval = EIO; |
| 2128 | /* |
| 2129 | * If we return an error we can't claim to |
| 2130 | * have transfered all data. |
| 2131 | */ |
| 2132 | if (xs->resid == 0) |
| 2133 | xs->resid = xs->datalen; |
| 2134 | } |
| 2135 | |
| 2136 | /* |
| 2137 | * If it's an unadorned EOM detection, |
| 2138 | * suppress printing an error. |
| 2139 | */ |
| 2140 | if (key == SKEY_NO_SENSE) { |
| 2141 | doprint = 0; |
| 2142 | } |
| 2143 | } else if (sense->flags & SSD_FILEMARK) { |
| 2144 | retval = 0; |
| 2145 | if (st->fileno != (daddr_t) -1) { |
| 2146 | st->fileno++; |
| 2147 | st->blkno = 0; |
| 2148 | st->flags |= ST_POSUPDATED; |
| 2149 | } |
| 2150 | } else if (sense->flags & SSD_ILI) { |
| 2151 | if (info < 0) { |
| 2152 | /* |
| 2153 | * The tape record was bigger than the read |
| 2154 | * we issued. |
| 2155 | */ |
| 2156 | if ((xs->xs_control & XS_CTL_SILENT) == 0) { |
| 2157 | aprint_error_dev(st->sc_dev, |
| 2158 | "%d-byte tape record too big" |
| 2159 | " for %d-byte user buffer\n" , |
| 2160 | xs->datalen - info, xs->datalen); |
| 2161 | } |
| 2162 | retval = EIO; |
| 2163 | } else { |
| 2164 | retval = 0; |
| 2165 | if (st->blkno != (daddr_t) -1) { |
| 2166 | st->blkno++; |
| 2167 | st->flags |= ST_POSUPDATED; |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | if (bp) |
| 2172 | bp->b_resid = xs->resid; |
| 2173 | } |
| 2174 | |
| 2175 | #ifndef SCSIPI_DEBUG |
| 2176 | if (retval == 0 && key == SKEY_NO_SENSE) |
| 2177 | doprint = 0; |
| 2178 | #endif |
| 2179 | if (key == SKEY_BLANK_CHECK) { |
| 2180 | /* |
| 2181 | * This quirk code helps the drive read the |
| 2182 | * first tape block, regardless of format. That |
| 2183 | * is required for these drives to return proper |
| 2184 | * MODE SENSE information. |
| 2185 | */ |
| 2186 | if ((st->quirks & ST_Q_SENSE_HELP) && |
| 2187 | (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { |
| 2188 | /* still starting */ |
| 2189 | st->blksize -= 512; |
| 2190 | } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) { |
| 2191 | st->flags |= ST_BLANK_READ; |
| 2192 | xs->resid = xs->datalen; |
| 2193 | if (bp) { |
| 2194 | bp->b_resid = xs->resid; |
| 2195 | /* return an EOF */ |
| 2196 | } |
| 2197 | retval = 0; |
| 2198 | /* lost position */ |
| 2199 | st->fileno = st->blkno = -1; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * If generic sense processing will continue, we should not |
| 2205 | * print sense info here. |
| 2206 | */ |
| 2207 | if (retval == EJUSTRETURN) |
| 2208 | doprint = 0; |
| 2209 | |
| 2210 | if (doprint) { |
| 2211 | /* Print verbose sense info if possible */ |
| 2212 | if (scsipi_print_sense(xs, 0) != 0) |
| 2213 | return retval; |
| 2214 | |
| 2215 | /* Print less-verbose sense info */ |
| 2216 | scsipi_printaddr(periph); |
| 2217 | printf("Sense Key 0x%02x" , key); |
| 2218 | if ((sense->response_code & SSD_RCODE_VALID) != 0) { |
| 2219 | switch (key) { |
| 2220 | case SKEY_NOT_READY: |
| 2221 | case SKEY_ILLEGAL_REQUEST: |
| 2222 | case SKEY_UNIT_ATTENTION: |
| 2223 | case SKEY_DATA_PROTECT: |
| 2224 | break; |
| 2225 | case SKEY_VOLUME_OVERFLOW: |
| 2226 | case SKEY_BLANK_CHECK: |
| 2227 | printf(", requested size: %d (decimal)" , info); |
| 2228 | break; |
| 2229 | case SKEY_ABORTED_COMMAND: |
| 2230 | if (xs->xs_retries) |
| 2231 | printf(", retrying" ); |
| 2232 | printf(", cmd 0x%x, info 0x%x" , |
| 2233 | xs->cmd->opcode, info); |
| 2234 | break; |
| 2235 | default: |
| 2236 | printf(", info = %d (decimal)" , info); |
| 2237 | } |
| 2238 | } |
| 2239 | if (sense->extra_len != 0) { |
| 2240 | int n; |
| 2241 | printf(", data =" ); |
| 2242 | for (n = 0; n < sense->extra_len; n++) |
| 2243 | printf(" %02x" , sense->csi[n]); |
| 2244 | } |
| 2245 | printf("\n" ); |
| 2246 | } |
| 2247 | return retval; |
| 2248 | } |
| 2249 | |
| 2250 | /* |
| 2251 | * The quirk here is that the drive returns some value to st_mode_sense |
| 2252 | * incorrectly until the tape has actually passed by the head. |
| 2253 | * |
| 2254 | * The method is to set the drive to large fixed-block state (user-specified |
| 2255 | * density and 1024-byte blocks), then read and rewind to get it to sense the |
| 2256 | * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't |
| 2257 | * work, as a last resort, try variable- length blocks. The result will be |
| 2258 | * the ability to do an accurate st_mode_sense. |
| 2259 | * |
| 2260 | * We know we can do a rewind because we just did a load, which implies rewind. |
| 2261 | * Rewind seems preferable to space backward if we have a virgin tape. |
| 2262 | * |
| 2263 | * The rest of the code for this quirk is in ILI processing and BLANK CHECK |
| 2264 | * error processing, both part of st_interpret_sense. |
| 2265 | */ |
| 2266 | static int |
| 2267 | st_touch_tape(struct st_softc *st) |
| 2268 | { |
| 2269 | char *bf; |
| 2270 | int readsize; |
| 2271 | int error; |
| 2272 | |
| 2273 | bf = malloc(1024, M_TEMP, M_NOWAIT); |
| 2274 | if (bf == NULL) |
| 2275 | return ENOMEM; |
| 2276 | |
| 2277 | if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0) |
| 2278 | goto bad; |
| 2279 | |
| 2280 | /* |
| 2281 | * If the block size is already known from the |
| 2282 | * sense data, use it. Else start probing at 1024. |
| 2283 | */ |
| 2284 | if (st->media_blksize > 0) |
| 2285 | st->blksize = st->media_blksize; |
| 2286 | else |
| 2287 | st->blksize = 1024; |
| 2288 | |
| 2289 | do { |
| 2290 | switch (st->blksize) { |
| 2291 | case 512: |
| 2292 | case 1024: |
| 2293 | readsize = st->blksize; |
| 2294 | st->flags |= ST_FIXEDBLOCKS; |
| 2295 | break; |
| 2296 | default: |
| 2297 | readsize = 1; |
| 2298 | st->flags &= ~ST_FIXEDBLOCKS; |
| 2299 | } |
| 2300 | if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT)) |
| 2301 | != 0) { |
| 2302 | /* |
| 2303 | * The device did not agree with the proposed |
| 2304 | * block size. If we exhausted our options, |
| 2305 | * return failure, else try another. |
| 2306 | */ |
| 2307 | if (readsize == 1) |
| 2308 | goto bad; |
| 2309 | st->blksize -= 512; |
| 2310 | continue; |
| 2311 | } |
| 2312 | st_read(st, bf, readsize, XS_CTL_SILENT); /* XXX */ |
| 2313 | if ((error = st_rewind(st, 0, 0)) != 0) { |
| 2314 | bad: free(bf, M_TEMP); |
| 2315 | return error; |
| 2316 | } |
| 2317 | } while (readsize != 1 && readsize > st->blksize); |
| 2318 | |
| 2319 | free(bf, M_TEMP); |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | static int |
| 2324 | stdump(dev_t dev, daddr_t blkno, void *va, size_t size) |
| 2325 | { |
| 2326 | /* Not implemented. */ |
| 2327 | return ENXIO; |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * Send a filled out parameter structure to the drive to |
| 2332 | * set it into the desire modes etc. |
| 2333 | */ |
| 2334 | int |
| 2335 | st_mode_select(struct st_softc *st, int flags) |
| 2336 | { |
| 2337 | u_int select_len; |
| 2338 | struct select { |
| 2339 | struct scsi_mode_parameter_header_6 ; |
| 2340 | struct scsi_general_block_descriptor blk_desc; |
| 2341 | u_char sense_data[MAX_PAGE_0_SIZE]; |
| 2342 | } select; |
| 2343 | struct scsipi_periph *periph = st->sc_periph; |
| 2344 | |
| 2345 | select_len = sizeof(select.header) + sizeof(select.blk_desc) + |
| 2346 | st->page_0_size; |
| 2347 | |
| 2348 | /* |
| 2349 | * This quirk deals with drives that have only one valid mode |
| 2350 | * and think this gives them license to reject all mode selects, |
| 2351 | * even if the selected mode is the one that is supported. |
| 2352 | */ |
| 2353 | if (st->quirks & ST_Q_UNIMODAL) { |
| 2354 | SC_DEBUG(periph, SCSIPI_DB3, |
| 2355 | ("not setting density 0x%x blksize 0x%x\n" , |
| 2356 | st->density, st->blksize)); |
| 2357 | return 0; |
| 2358 | } |
| 2359 | |
| 2360 | /* Set up for a mode select */ |
| 2361 | memset(&select, 0, sizeof(select)); |
| 2362 | select.header.blk_desc_len = sizeof(struct |
| 2363 | scsi_general_block_descriptor); |
| 2364 | select.header.dev_spec &= ~SMH_DSP_BUFF_MODE; |
| 2365 | select.blk_desc.density = st->density; |
| 2366 | if (st->flags & ST_DONTBUFFER) |
| 2367 | select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF; |
| 2368 | else |
| 2369 | select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON; |
| 2370 | if (st->flags & ST_FIXEDBLOCKS) |
| 2371 | _lto3b(st->blksize, select.blk_desc.blklen); |
| 2372 | if (st->page_0_size) |
| 2373 | memcpy(select.sense_data, st->sense_data, st->page_0_size); |
| 2374 | |
| 2375 | /* do the command */ |
| 2376 | return scsipi_mode_select(periph, 0, &select.header, select_len, |
| 2377 | flags, ST_RETRIES, ST_CTL_TIME); |
| 2378 | } |
| 2379 | |