xref: /openbsd-src/sys/scsi/sd.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: sd.c,v 1.255 2014/07/12 18:50:25 tedu Exp $	*/
2 /*	$NetBSD: sd.c,v 1.111 1997/04/02 02:29:41 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Originally written by Julian Elischer (julian@dialix.oz.au)
35  * for TRW Financial Systems for use under the MACH(2.5) operating system.
36  *
37  * TRW Financial Systems, in accordance with their agreement with Carnegie
38  * Mellon University, makes this software available to CMU to distribute
39  * or use in any manner that they see fit as long as this message is kept with
40  * the software. For this reason TFS also grants any other persons or
41  * organisations permission to use or modify this software.
42  *
43  * TFS supplies this software to be publicly redistributed
44  * on the understanding that TFS is not responsible for the correct
45  * functioning of this software in any circumstances.
46  *
47  * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992
48  */
49 
50 #include <sys/stdint.h>
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/timeout.h>
55 #include <sys/file.h>
56 #include <sys/stat.h>
57 #include <sys/ioctl.h>
58 #include <sys/mtio.h>
59 #include <sys/mutex.h>
60 #include <sys/buf.h>
61 #include <sys/uio.h>
62 #include <sys/malloc.h>
63 #include <sys/pool.h>
64 #include <sys/errno.h>
65 #include <sys/device.h>
66 #include <sys/disklabel.h>
67 #include <sys/disk.h>
68 #include <sys/proc.h>
69 #include <sys/conf.h>
70 #include <sys/scsiio.h>
71 #include <sys/dkio.h>
72 #include <sys/reboot.h>
73 
74 #include <scsi/scsi_all.h>
75 #include <scsi/scsi_disk.h>
76 #include <scsi/scsiconf.h>
77 #include <scsi/sdvar.h>
78 
79 #include <ufs/ffs/fs.h>			/* for BBSIZE and SBSIZE */
80 
81 #include <sys/vnode.h>
82 
83 int	sdmatch(struct device *, void *, void *);
84 void	sdattach(struct device *, struct device *, void *);
85 int	sdactivate(struct device *, int);
86 int	sddetach(struct device *, int);
87 
88 void	sdminphys(struct buf *);
89 int	sdgetdisklabel(dev_t, struct sd_softc *, struct disklabel *, int);
90 void	sdstart(struct scsi_xfer *);
91 int	sd_interpret_sense(struct scsi_xfer *);
92 int	sd_read_cap_10(struct sd_softc *, int);
93 int	sd_read_cap_16(struct sd_softc *, int);
94 int	sd_size(struct sd_softc *, int);
95 int	sd_thin_pages(struct sd_softc *, int);
96 int	sd_vpd_block_limits(struct sd_softc *, int);
97 int	sd_vpd_thin(struct sd_softc *, int);
98 int	sd_thin_params(struct sd_softc *, int);
99 int	sd_get_parms(struct sd_softc *, struct disk_parms *, int);
100 void	sd_flush(struct sd_softc *, int);
101 
102 void	viscpy(u_char *, u_char *, int);
103 
104 int	sd_ioctl_inquiry(struct sd_softc *, struct dk_inquiry *);
105 int	sd_ioctl_cache(struct sd_softc *, long, struct dk_cache *);
106 
107 void	sd_cmd_rw6(struct scsi_xfer *, int, u_int64_t, u_int);
108 void	sd_cmd_rw10(struct scsi_xfer *, int, u_int64_t, u_int);
109 void	sd_cmd_rw12(struct scsi_xfer *, int, u_int64_t, u_int);
110 void	sd_cmd_rw16(struct scsi_xfer *, int, u_int64_t, u_int);
111 
112 void	sd_buf_done(struct scsi_xfer *);
113 
114 struct cfattach sd_ca = {
115 	sizeof(struct sd_softc), sdmatch, sdattach,
116 	sddetach, sdactivate
117 };
118 
119 struct cfdriver sd_cd = {
120 	NULL, "sd", DV_DISK
121 };
122 
123 const struct scsi_inquiry_pattern sd_patterns[] = {
124 	{T_DIRECT, T_FIXED,
125 	 "",         "",                 ""},
126 	{T_DIRECT, T_REMOV,
127 	 "",         "",                 ""},
128 	{T_RDIRECT, T_FIXED,
129 	 "",         "",                 ""},
130 	{T_RDIRECT, T_REMOV,
131 	 "",         "",                 ""},
132 	{T_OPTICAL, T_FIXED,
133 	 "",         "",                 ""},
134 	{T_OPTICAL, T_REMOV,
135 	 "",         "",                 ""},
136 };
137 
138 #define sdlookup(unit) (struct sd_softc *)disk_lookup(&sd_cd, (unit))
139 
140 int
141 sdmatch(struct device *parent, void *match, void *aux)
142 {
143 	struct scsi_attach_args *sa = aux;
144 	int priority;
145 
146 	(void)scsi_inqmatch(sa->sa_inqbuf,
147 	    sd_patterns, nitems(sd_patterns),
148 	    sizeof(sd_patterns[0]), &priority);
149 
150 	return (priority);
151 }
152 
153 /*
154  * The routine called by the low level scsi routine when it discovers
155  * a device suitable for this driver.
156  */
157 void
158 sdattach(struct device *parent, struct device *self, void *aux)
159 {
160 	struct sd_softc *sc = (struct sd_softc *)self;
161 	struct scsi_attach_args *sa = aux;
162 	struct disk_parms *dp = &sc->params;
163 	struct scsi_link *sc_link = sa->sa_sc_link;
164 	int sd_autoconf = scsi_autoconf | SCSI_SILENT |
165 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE;
166 	struct dk_cache dkc;
167 	int error, result, sortby = BUFQ_DEFAULT;
168 
169 	SC_DEBUG(sc_link, SDEV_DB2, ("sdattach:\n"));
170 
171 	/*
172 	 * Store information needed to contact our base driver
173 	 */
174 	sc->sc_link = sc_link;
175 	sc_link->interpret_sense = sd_interpret_sense;
176 	sc_link->device_softc = sc;
177 
178 	if ((sc_link->flags & SDEV_ATAPI) && (sc_link->flags & SDEV_REMOVABLE))
179 		sc_link->quirks |= SDEV_NOSYNCCACHE;
180 
181 	if (!(sc_link->inqdata.flags & SID_RelAdr))
182 		sc_link->quirks |= SDEV_ONLYBIG;
183 
184 	/*
185 	 * Note if this device is ancient.  This is used in sdminphys().
186 	 */
187 	if (!(sc_link->flags & SDEV_ATAPI) &&
188 	    SCSISPC(sa->sa_inqbuf->version) == 0)
189 		sc->flags |= SDF_ANCIENT;
190 
191 	/*
192 	 * Use the subdriver to request information regarding
193 	 * the drive. We cannot use interrupts yet, so the
194 	 * request must specify this.
195 	 */
196 	printf("\n");
197 
198 	scsi_xsh_set(&sc->sc_xsh, sc_link, sdstart);
199 	timeout_set(&sc->sc_timeout, (void (*)(void *))scsi_xsh_add,
200 	    &sc->sc_xsh);
201 
202 	/* Spin up non-UMASS devices ready or not. */
203 	if ((sc->sc_link->flags & SDEV_UMASS) == 0)
204 		scsi_start(sc_link, SSS_START, sd_autoconf);
205 
206 	/*
207 	 * Some devices (e.g. Blackberry Pearl) won't admit they have
208 	 * media loaded unless its been locked in.
209 	 */
210 	if ((sc_link->flags & SDEV_REMOVABLE) != 0)
211 		scsi_prevent(sc_link, PR_PREVENT, sd_autoconf);
212 
213 	/* Check that it is still responding and ok. */
214 	error = scsi_test_unit_ready(sc->sc_link, TEST_READY_RETRIES * 3,
215 	    sd_autoconf);
216 
217 	if (error)
218 		result = SDGP_RESULT_OFFLINE;
219 	else
220 		result = sd_get_parms(sc, &sc->params, sd_autoconf);
221 
222 	if ((sc_link->flags & SDEV_REMOVABLE) != 0)
223 		scsi_prevent(sc_link, PR_ALLOW, sd_autoconf);
224 
225 	switch (result) {
226 	case SDGP_RESULT_OK:
227 		printf("%s: %lluMB, %lu bytes/sector, %llu sectors",
228 		    sc->sc_dev.dv_xname,
229 		    dp->disksize / (1048576 / dp->secsize), dp->secsize,
230 		    dp->disksize);
231 		if (ISSET(sc->flags, SDF_THIN)) {
232 			sortby = BUFQ_FIFO;
233 			printf(", thin");
234 		}
235 		if (ISSET(sc_link->flags, SDEV_READONLY)) {
236 			printf(", readonly");
237 		}
238 		printf("\n");
239 		break;
240 
241 	case SDGP_RESULT_OFFLINE:
242 		break;
243 
244 #ifdef DIAGNOSTIC
245 	default:
246 		panic("sdattach: unknown result (%#x) from get_parms", result);
247 		break;
248 #endif
249 	}
250 
251 	/*
252 	 * Initialize disk structures.
253 	 */
254 	sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
255 	bufq_init(&sc->sc_bufq, sortby);
256 
257 	/*
258 	 * Enable write cache by default.
259 	 */
260 	memset(&dkc, 0, sizeof(dkc));
261 	if (sd_ioctl_cache(sc, DIOCGCACHE, &dkc) == 0 && dkc.wrcache == 0) {
262 		dkc.wrcache = 1;
263 		sd_ioctl_cache(sc, DIOCSCACHE, &dkc);
264 	}
265 
266 	/* Attach disk. */
267 	disk_attach(&sc->sc_dev, &sc->sc_dk);
268 }
269 
270 int
271 sdactivate(struct device *self, int act)
272 {
273 	struct sd_softc *sc = (struct sd_softc *)self;
274 	int rv = 0;
275 
276 	switch (act) {
277 	case DVACT_SUSPEND:
278 		/*
279 		 * We flush the cache, since we our next step before
280 		 * DVACT_POWERDOWN might be a hibernate operation.
281 		 */
282 		if ((sc->flags & SDF_DIRTY) != 0)
283 			sd_flush(sc, SCSI_AUTOCONF);
284 		break;
285 	case DVACT_POWERDOWN:
286 		/*
287 		 * Stop the disk.  Stopping the disk should flush the
288 		 * cache, but we are paranoid so we flush the cache
289 		 * first.  We're cold at this point, so we poll for
290 	 	 * completion.
291 		 */
292 		if ((sc->flags & SDF_DIRTY) != 0)
293 			sd_flush(sc, SCSI_AUTOCONF);
294 		if (boothowto & RB_POWERDOWN)
295 			scsi_start(sc->sc_link, SSS_STOP,
296 			    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_AUTOCONF);
297 		break;
298 	case DVACT_RESUME:
299 		scsi_start(sc->sc_link, SSS_START,
300 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_AUTOCONF);
301 		break;
302 	case DVACT_DEACTIVATE:
303 		sc->flags |= SDF_DYING;
304 		scsi_xsh_del(&sc->sc_xsh);
305 		break;
306 	}
307 	return (rv);
308 }
309 
310 int
311 sddetach(struct device *self, int flags)
312 {
313 	struct sd_softc *sc = (struct sd_softc *)self;
314 
315 	bufq_drain(&sc->sc_bufq);
316 
317 	disk_gone(sdopen, self->dv_unit);
318 
319 	/* Detach disk. */
320 	bufq_destroy(&sc->sc_bufq);
321 	disk_detach(&sc->sc_dk);
322 
323 	return (0);
324 }
325 
326 /*
327  * Open the device. Make sure the partition info is as up-to-date as can be.
328  */
329 int
330 sdopen(dev_t dev, int flag, int fmt, struct proc *p)
331 {
332 	struct scsi_link *sc_link;
333 	struct sd_softc *sc;
334 	int error = 0, part, rawopen, unit;
335 
336 	unit = DISKUNIT(dev);
337 	part = DISKPART(dev);
338 
339 	rawopen = (part == RAW_PART) && (fmt == S_IFCHR);
340 
341 	sc = sdlookup(unit);
342 	if (sc == NULL)
343 		return (ENXIO);
344 	sc_link = sc->sc_link;
345 
346 	if (sc->flags & SDF_DYING) {
347 		device_unref(&sc->sc_dev);
348 		return (ENXIO);
349 	}
350 	if (ISSET(flag, FWRITE) && ISSET(sc_link->flags, SDEV_READONLY)) {
351 		device_unref(&sc->sc_dev);
352 		return (EACCES);
353 	}
354 
355 	SC_DEBUG(sc_link, SDEV_DB1,
356 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
357 	    sd_cd.cd_ndevs, part));
358 
359 	if ((error = disk_lock(&sc->sc_dk)) != 0) {
360 		device_unref(&sc->sc_dev);
361 		return (error);
362 	}
363 
364 	if (sc->sc_dk.dk_openmask != 0) {
365 		/*
366 		 * If any partition is open, but the disk has been invalidated,
367 		 * disallow further opens of non-raw partition.
368 		 */
369 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
370 			if (rawopen)
371 				goto out;
372 			error = EIO;
373 			goto bad;
374 		}
375 	} else {
376 		/* Spin up non-UMASS devices ready or not. */
377 		if ((sc->sc_link->flags & SDEV_UMASS) == 0)
378 			scsi_start(sc_link, SSS_START, (rawopen ? SCSI_SILENT :
379 			    0) | SCSI_IGNORE_ILLEGAL_REQUEST |
380 			    SCSI_IGNORE_MEDIA_CHANGE);
381 
382 		/* Use sd_interpret_sense() for sense errors.
383 		 *
384 		 * But only after spinning the disk up! Just in case a broken
385 		 * device returns "Initialization command required." and causes
386 		 * a loop of scsi_start() calls.
387 		 */
388 		sc_link->flags |= SDEV_OPEN;
389 
390 		/*
391 		 * Try to prevent the unloading of a removable device while
392 		 * it's open. But allow the open to proceed if the device can't
393 		 * be locked in.
394 		 */
395 		if ((sc_link->flags & SDEV_REMOVABLE) != 0) {
396 			scsi_prevent(sc_link, PR_PREVENT, SCSI_SILENT |
397 			    SCSI_IGNORE_ILLEGAL_REQUEST |
398 			    SCSI_IGNORE_MEDIA_CHANGE);
399 		}
400 
401 		/* Check that it is still responding and ok. */
402 		error = scsi_test_unit_ready(sc_link,
403 		    TEST_READY_RETRIES, SCSI_SILENT |
404 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
405 
406 		if (error) {
407 			if (rawopen) {
408 				error = 0;
409 				goto out;
410 			} else
411 				goto bad;
412 		}
413 
414 		/* Load the physical device parameters. */
415 		sc_link->flags |= SDEV_MEDIA_LOADED;
416 		if (sd_get_parms(sc, &sc->params, (rawopen ? SCSI_SILENT : 0))
417 		    == SDGP_RESULT_OFFLINE) {
418 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
419 			error = ENXIO;
420 			goto bad;
421 		}
422 		SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded\n"));
423 
424 		/* Load the partition info if not already loaded. */
425 		if (sdgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0) == EIO) {
426 			error = EIO;
427 			goto bad;
428 		}
429 		SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded\n"));
430 	}
431 
432 out:
433 	if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0)
434 		goto bad;
435 
436 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
437 
438 	/* It's OK to fall through because dk_openmask is now non-zero. */
439 bad:
440 	if (sc->sc_dk.dk_openmask == 0) {
441 		if ((sc->sc_link->flags & SDEV_REMOVABLE) != 0)
442 			scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT |
443 			    SCSI_IGNORE_ILLEGAL_REQUEST |
444 			    SCSI_IGNORE_MEDIA_CHANGE);
445 		sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
446 	}
447 
448 	disk_unlock(&sc->sc_dk);
449 	device_unref(&sc->sc_dev);
450 	return (error);
451 }
452 
453 /*
454  * Close the device. Only called if we are the last occurrence of an open
455  * device.  Convenient now but usually a pain.
456  */
457 int
458 sdclose(dev_t dev, int flag, int fmt, struct proc *p)
459 {
460 	struct sd_softc *sc;
461 	int part = DISKPART(dev);
462 
463 	sc = sdlookup(DISKUNIT(dev));
464 	if (sc == NULL)
465 		return (ENXIO);
466 	if (sc->flags & SDF_DYING) {
467 		device_unref(&sc->sc_dev);
468 		return (ENXIO);
469 	}
470 
471 	disk_lock_nointr(&sc->sc_dk);
472 
473 	disk_closepart(&sc->sc_dk, part, fmt);
474 
475 	if (sc->sc_dk.dk_openmask == 0) {
476 		if ((sc->flags & SDF_DIRTY) != 0)
477 			sd_flush(sc, 0);
478 
479 		if ((sc->sc_link->flags & SDEV_REMOVABLE) != 0)
480 			scsi_prevent(sc->sc_link, PR_ALLOW,
481 			    SCSI_IGNORE_ILLEGAL_REQUEST |
482 			    SCSI_IGNORE_NOT_READY | SCSI_SILENT);
483 		sc->sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
484 
485 		if (sc->sc_link->flags & SDEV_EJECTING) {
486 			scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0);
487 			sc->sc_link->flags &= ~SDEV_EJECTING;
488 		}
489 
490 		timeout_del(&sc->sc_timeout);
491 		scsi_xsh_del(&sc->sc_xsh);
492 	}
493 
494 	disk_unlock(&sc->sc_dk);
495 	device_unref(&sc->sc_dev);
496 	return 0;
497 }
498 
499 /*
500  * Actually translate the requested transfer into one the physical driver
501  * can understand.  The transfer is described by a buf and will include
502  * only one physical transfer.
503  */
504 void
505 sdstrategy(struct buf *bp)
506 {
507 	struct sd_softc *sc;
508 	int s;
509 
510 	sc = sdlookup(DISKUNIT(bp->b_dev));
511 	if (sc == NULL) {
512 		bp->b_error = ENXIO;
513 		goto bad;
514 	}
515 	if (sc->flags & SDF_DYING) {
516 		bp->b_error = ENXIO;
517 		goto bad;
518 	}
519 
520 	SC_DEBUG(sc->sc_link, SDEV_DB2, ("sdstrategy: %ld bytes @ blk %lld\n",
521 	    bp->b_bcount, (long long)bp->b_blkno));
522 	/*
523 	 * If the device has been made invalid, error out
524 	 */
525 	if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
526 		if (sc->sc_link->flags & SDEV_OPEN)
527 			bp->b_error = EIO;
528 		else
529 			bp->b_error = ENODEV;
530 		goto bad;
531 	}
532 
533 	/* Validate the request. */
534 	if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1)
535 		goto done;
536 
537 	/* Place it in the queue of disk activities for this disk. */
538 	bufq_queue(&sc->sc_bufq, bp);
539 
540 	/*
541 	 * Tell the device to get going on the transfer if it's
542 	 * not doing anything, otherwise just wait for completion
543 	 */
544 	scsi_xsh_add(&sc->sc_xsh);
545 
546 	device_unref(&sc->sc_dev);
547 	return;
548 
549  bad:
550 	bp->b_flags |= B_ERROR;
551 	bp->b_resid = bp->b_bcount;
552  done:
553 	s = splbio();
554 	biodone(bp);
555 	splx(s);
556 	if (sc != NULL)
557 		device_unref(&sc->sc_dev);
558 }
559 
560 void
561 sd_cmd_rw6(struct scsi_xfer *xs, int read, u_int64_t secno, u_int nsecs)
562 {
563 	struct scsi_rw *cmd = (struct scsi_rw *)xs->cmd;
564 
565 	cmd->opcode = read ? READ_COMMAND : WRITE_COMMAND;
566 	_lto3b(secno, cmd->addr);
567 	cmd->length = nsecs;
568 
569 	xs->cmdlen = sizeof(*cmd);
570 }
571 
572 void
573 sd_cmd_rw10(struct scsi_xfer *xs, int read, u_int64_t secno, u_int nsecs)
574 {
575 	struct scsi_rw_big *cmd = (struct scsi_rw_big *)xs->cmd;
576 
577 	cmd->opcode = read ? READ_BIG : WRITE_BIG;
578 	_lto4b(secno, cmd->addr);
579 	_lto2b(nsecs, cmd->length);
580 
581 	xs->cmdlen = sizeof(*cmd);
582 }
583 
584 void
585 sd_cmd_rw12(struct scsi_xfer *xs, int read, u_int64_t secno, u_int nsecs)
586 {
587 	struct scsi_rw_12 *cmd = (struct scsi_rw_12 *)xs->cmd;
588 
589 	cmd->opcode = read ? READ_12 : WRITE_12;
590 	_lto4b(secno, cmd->addr);
591 	_lto4b(nsecs, cmd->length);
592 
593 	xs->cmdlen = sizeof(*cmd);
594 }
595 
596 void
597 sd_cmd_rw16(struct scsi_xfer *xs, int read, u_int64_t secno, u_int nsecs)
598 {
599 	struct scsi_rw_16 *cmd = (struct scsi_rw_16 *)xs->cmd;
600 
601 	cmd->opcode = read ? READ_16 : WRITE_16;
602 	_lto8b(secno, cmd->addr);
603 	_lto4b(nsecs, cmd->length);
604 
605 	xs->cmdlen = sizeof(*cmd);
606 }
607 
608 /*
609  * sdstart looks to see if there is a buf waiting for the device
610  * and that the device is not already busy. If both are true,
611  * It dequeues the buf and creates a scsi command to perform the
612  * transfer in the buf. The transfer request will call scsi_done
613  * on completion, which will in turn call this routine again
614  * so that the next queued transfer is performed.
615  * The bufs are queued by the strategy routine (sdstrategy)
616  *
617  * This routine is also called after other non-queued requests
618  * have been made of the scsi driver, to ensure that the queue
619  * continues to be drained.
620  */
621 void
622 sdstart(struct scsi_xfer *xs)
623 {
624 	struct scsi_link *link = xs->sc_link;
625 	struct sd_softc *sc = link->device_softc;
626 	struct buf *bp;
627 	u_int64_t secno;
628 	int nsecs;
629 	int read;
630 	struct partition *p;
631 
632 	if (sc->flags & SDF_DYING) {
633 		scsi_xs_put(xs);
634 		return;
635 	}
636 	if ((link->flags & SDEV_MEDIA_LOADED) == 0) {
637 		bufq_drain(&sc->sc_bufq);
638 		scsi_xs_put(xs);
639 		return;
640 	}
641 
642 	bp = bufq_dequeue(&sc->sc_bufq);
643 	if (bp == NULL) {
644 		scsi_xs_put(xs);
645 		return;
646 	}
647 
648 	secno = DL_BLKTOSEC(sc->sc_dk.dk_label, bp->b_blkno);
649 
650 	p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
651 	secno += DL_GETPOFFSET(p);
652 	nsecs = howmany(bp->b_bcount, sc->sc_dk.dk_label->d_secsize);
653 	read = bp->b_flags & B_READ;
654 
655 	/*
656 	 *  Fill out the scsi command.  If the transfer will
657 	 *  fit in a "small" cdb, use it.
658 	 */
659 	if (!(link->flags & SDEV_ATAPI) &&
660 	    !(link->quirks & SDEV_ONLYBIG) &&
661 	    ((secno & 0x1fffff) == secno) &&
662 	    ((nsecs & 0xff) == nsecs))
663 		sd_cmd_rw6(xs, read, secno, nsecs);
664 	else if (((secno & 0xffffffff) == secno) &&
665 	    ((nsecs & 0xffff) == nsecs))
666 		sd_cmd_rw10(xs, read, secno, nsecs);
667 	else if (((secno & 0xffffffff) == secno) &&
668 	    ((nsecs & 0xffffffff) == nsecs))
669 		sd_cmd_rw12(xs, read, secno, nsecs);
670 	else
671 		sd_cmd_rw16(xs, read, secno, nsecs);
672 
673 	xs->flags |= (read ? SCSI_DATA_IN : SCSI_DATA_OUT);
674 	xs->timeout = 60000;
675 	xs->data = bp->b_data;
676 	xs->datalen = bp->b_bcount;
677 
678 	xs->done = sd_buf_done;
679 	xs->cookie = bp;
680 	xs->bp = bp;
681 
682 	/* Instrumentation. */
683 	disk_busy(&sc->sc_dk);
684 
685 	/* Mark disk as dirty. */
686 	if (!read)
687 		sc->flags |= SDF_DIRTY;
688 
689 	scsi_xs_exec(xs);
690 
691 	/* move onto the next io */
692 	if (ISSET(sc->flags, SDF_WAITING))
693 		CLR(sc->flags, SDF_WAITING);
694 	else if (bufq_peek(&sc->sc_bufq))
695 		scsi_xsh_add(&sc->sc_xsh);
696 }
697 
698 void
699 sd_buf_done(struct scsi_xfer *xs)
700 {
701 	struct sd_softc *sc = xs->sc_link->device_softc;
702 	struct buf *bp = xs->cookie;
703 	int error, s;
704 
705 	switch (xs->error) {
706 	case XS_NOERROR:
707 		bp->b_error = 0;
708 		bp->b_resid = xs->resid;
709 		break;
710 
711 	case XS_NO_CCB:
712 		/* The adapter is busy, requeue the buf and try it later. */
713 		disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid,
714 		    bp->b_flags & B_READ);
715 		bufq_requeue(&sc->sc_bufq, bp);
716 		scsi_xs_put(xs);
717 		SET(sc->flags, SDF_WAITING);
718 		timeout_add(&sc->sc_timeout, 1);
719 		return;
720 
721 	case XS_SENSE:
722 	case XS_SHORTSENSE:
723 #ifdef SCSIDEBUG
724 		scsi_sense_print_debug(xs);
725 #endif
726 		error = sd_interpret_sense(xs);
727 		if (error == 0) {
728 			bp->b_error = 0;
729 			bp->b_resid = xs->resid;
730 			break;
731 		}
732 		if (error != ERESTART) {
733 			bp->b_error = error;
734 			xs->retries = 0;
735 		}
736 		goto retry;
737 
738 	case XS_BUSY:
739 		if (xs->retries) {
740 			if (scsi_delay(xs, 1) != ERESTART)
741 				xs->retries = 0;
742 		}
743 		goto retry;
744 
745 	case XS_TIMEOUT:
746 retry:
747 		if (xs->retries--) {
748 			scsi_xs_exec(xs);
749 			return;
750 		}
751 		/* FALLTHROUGH */
752 
753 	default:
754 		if (bp->b_error == 0)
755 			bp->b_error = EIO;
756 		bp->b_flags |= B_ERROR;
757 		bp->b_resid = bp->b_bcount;
758 		break;
759 	}
760 
761 	disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid,
762 	    bp->b_flags & B_READ);
763 
764 	s = splbio();
765 	biodone(bp);
766 	splx(s);
767 	scsi_xs_put(xs);
768 }
769 
770 void
771 sdminphys(struct buf *bp)
772 {
773 	struct sd_softc *sc;
774 	long max;
775 
776 	sc = sdlookup(DISKUNIT(bp->b_dev));
777 	if (sc == NULL)
778 		return;  /* XXX - right way to fail this? */
779 
780 	/*
781 	 * If the device is ancient, we want to make sure that
782 	 * the transfer fits into a 6-byte cdb.
783 	 *
784 	 * XXX Note that the SCSI-I spec says that 256-block transfers
785 	 * are allowed in a 6-byte read/write, and are specified
786 	 * by setting the "length" to 0.  However, we're conservative
787 	 * here, allowing only 255-block transfers in case an
788 	 * ancient device gets confused by length == 0.  A length of 0
789 	 * in a 10-byte read/write actually means 0 blocks.
790 	 */
791 	if (sc->flags & SDF_ANCIENT) {
792 		max = sc->sc_dk.dk_label->d_secsize * 0xff;
793 
794 		if (bp->b_bcount > max)
795 			bp->b_bcount = max;
796 	}
797 
798 	(*sc->sc_link->adapter->scsi_minphys)(bp, sc->sc_link);
799 
800 	device_unref(&sc->sc_dev);
801 }
802 
803 int
804 sdread(dev_t dev, struct uio *uio, int ioflag)
805 {
806 	return (physio(sdstrategy, dev, B_READ, sdminphys, uio));
807 }
808 
809 int
810 sdwrite(dev_t dev, struct uio *uio, int ioflag)
811 {
812 	return (physio(sdstrategy, dev, B_WRITE, sdminphys, uio));
813 }
814 
815 /*
816  * Perform special action on behalf of the user
817  * Knows about the internals of this device
818  */
819 int
820 sdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
821 {
822 	struct sd_softc *sc;
823 	struct disklabel *lp;
824 	int error = 0;
825 	int part = DISKPART(dev);
826 
827 	sc = sdlookup(DISKUNIT(dev));
828 	if (sc == NULL)
829 		return (ENXIO);
830 	if (sc->flags & SDF_DYING) {
831 		device_unref(&sc->sc_dev);
832 		return (ENXIO);
833 	}
834 
835 	SC_DEBUG(sc->sc_link, SDEV_DB2, ("sdioctl 0x%lx\n", cmd));
836 
837 	/*
838 	 * If the device is not valid.. abandon ship
839 	 */
840 	if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
841 		switch (cmd) {
842 		case DIOCLOCK:
843 		case DIOCEJECT:
844 		case SCIOCIDENTIFY:
845 		case SCIOCCOMMAND:
846 		case SCIOCDEBUG:
847 			if (part == RAW_PART)
848 				break;
849 		/* FALLTHROUGH */
850 		default:
851 			if ((sc->sc_link->flags & SDEV_OPEN) == 0) {
852 				error = ENODEV;
853 				goto exit;
854 			} else {
855 				error = EIO;
856 				goto exit;
857 			}
858 		}
859 	}
860 
861 	switch (cmd) {
862 	case DIOCRLDINFO:
863 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
864 		sdgetdisklabel(dev, sc, lp, 0);
865 		bcopy(lp, sc->sc_dk.dk_label, sizeof(*lp));
866 		free(lp, M_TEMP, 0);
867 		goto exit;
868 
869 	case DIOCGPDINFO:
870 		sdgetdisklabel(dev, sc, (struct disklabel *)addr, 1);
871 		goto exit;
872 
873 	case DIOCGDINFO:
874 		*(struct disklabel *)addr = *(sc->sc_dk.dk_label);
875 		goto exit;
876 
877 	case DIOCGPART:
878 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
879 		((struct partinfo *)addr)->part =
880 		    &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)];
881 		goto exit;
882 
883 	case DIOCWDINFO:
884 	case DIOCSDINFO:
885 		if ((flag & FWRITE) == 0) {
886 			error = EBADF;
887 			goto exit;
888 		}
889 
890 		if ((error = disk_lock(&sc->sc_dk)) != 0)
891 			goto exit;
892 
893 		error = setdisklabel(sc->sc_dk.dk_label,
894 		    (struct disklabel *)addr, sc->sc_dk.dk_openmask);
895 		if (error == 0) {
896 			if (cmd == DIOCWDINFO)
897 				error = writedisklabel(DISKLABELDEV(dev),
898 				    sdstrategy, sc->sc_dk.dk_label);
899 		}
900 
901 		disk_unlock(&sc->sc_dk);
902 		goto exit;
903 
904 	case DIOCLOCK:
905 		error = scsi_prevent(sc->sc_link,
906 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
907 		goto exit;
908 
909 	case MTIOCTOP:
910 		if (((struct mtop *)addr)->mt_op != MTOFFL) {
911 			error = EIO;
912 			goto exit;
913 		}
914 		/* FALLTHROUGH */
915 	case DIOCEJECT:
916 		if ((sc->sc_link->flags & SDEV_REMOVABLE) == 0) {
917 			error = ENOTTY;
918 			goto exit;
919 		}
920 		sc->sc_link->flags |= SDEV_EJECTING;
921 		goto exit;
922 
923 	case DIOCINQ:
924 		error = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
925 		if (error == ENOTTY)
926 			error = sd_ioctl_inquiry(sc,
927 			    (struct dk_inquiry *)addr);
928 		goto exit;
929 
930 	case DIOCSCACHE:
931 		if (!ISSET(flag, FWRITE)) {
932 			error = EBADF;
933 			goto exit;
934 		}
935 		/* FALLTHROUGH */
936 	case DIOCGCACHE:
937 		error = sd_ioctl_cache(sc, cmd, (struct dk_cache *)addr);
938 		goto exit;
939 
940 	default:
941 		if (part != RAW_PART) {
942 			error = ENOTTY;
943 			goto exit;
944 		}
945 		error = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
946 	}
947 
948  exit:
949 	device_unref(&sc->sc_dev);
950 	return (error);
951 }
952 
953 int
954 sd_ioctl_inquiry(struct sd_softc *sc, struct dk_inquiry *di)
955 {
956 	struct scsi_vpd_serial *vpd;
957 
958 	vpd = dma_alloc(sizeof(*vpd), PR_WAITOK | PR_ZERO);
959 
960 	bzero(di, sizeof(struct dk_inquiry));
961 	scsi_strvis(di->vendor, sc->sc_link->inqdata.vendor,
962 	    sizeof(sc->sc_link->inqdata.vendor));
963 	scsi_strvis(di->product, sc->sc_link->inqdata.product,
964 	    sizeof(sc->sc_link->inqdata.product));
965 	scsi_strvis(di->revision, sc->sc_link->inqdata.revision,
966 	    sizeof(sc->sc_link->inqdata.revision));
967 
968 	/* the serial vpd page is optional */
969 	if (scsi_inquire_vpd(sc->sc_link, vpd, sizeof(*vpd),
970 	    SI_PG_SERIAL, 0) == 0)
971 		scsi_strvis(di->serial, vpd->serial, sizeof(vpd->serial));
972 	else
973 		strlcpy(di->serial, "(unknown)", sizeof(vpd->serial));
974 
975 	dma_free(vpd, sizeof(*vpd));
976 	return (0);
977 }
978 
979 int
980 sd_ioctl_cache(struct sd_softc *sc, long cmd, struct dk_cache *dkc)
981 {
982 	union scsi_mode_sense_buf *buf;
983 	struct page_caching_mode *mode = NULL;
984 	u_int wrcache, rdcache;
985 	int big;
986 	int rv;
987 
988 	if (ISSET(sc->sc_link->flags, SDEV_UMASS))
989 		return (EOPNOTSUPP);
990 
991 	/* see if the adapter has special handling */
992 	rv = scsi_do_ioctl(sc->sc_link, cmd, (caddr_t)dkc, 0);
993 	if (rv != ENOTTY)
994 		return (rv);
995 
996 	buf = dma_alloc(sizeof(*buf), PR_WAITOK);
997 	if (buf == NULL)
998 		return (ENOMEM);
999 
1000 	rv = scsi_do_mode_sense(sc->sc_link, PAGE_CACHING_MODE,
1001 	    buf, (void **)&mode, NULL, NULL, NULL,
1002 	    sizeof(*mode) - 4, scsi_autoconf | SCSI_SILENT, &big);
1003 	if (rv != 0)
1004 		goto done;
1005 
1006 	if ((mode == NULL) || (!DISK_PGCODE(mode, PAGE_CACHING_MODE))) {
1007 		rv = EIO;
1008 		goto done;
1009 	}
1010 
1011 	wrcache = (ISSET(mode->flags, PG_CACHE_FL_WCE) ? 1 : 0);
1012 	rdcache = (ISSET(mode->flags, PG_CACHE_FL_RCD) ? 0 : 1);
1013 
1014 	switch (cmd) {
1015 	case DIOCGCACHE:
1016 		dkc->wrcache = wrcache;
1017 		dkc->rdcache = rdcache;
1018 		break;
1019 
1020 	case DIOCSCACHE:
1021 		if (dkc->wrcache == wrcache && dkc->rdcache == rdcache)
1022 			break;
1023 
1024 		if (dkc->wrcache)
1025 			SET(mode->flags, PG_CACHE_FL_WCE);
1026 		else
1027 			CLR(mode->flags, PG_CACHE_FL_WCE);
1028 
1029 		if (dkc->rdcache)
1030 			CLR(mode->flags, PG_CACHE_FL_RCD);
1031 		else
1032 			SET(mode->flags, PG_CACHE_FL_RCD);
1033 
1034 		if (big) {
1035 			rv = scsi_mode_select_big(sc->sc_link, SMS_PF,
1036 			    &buf->hdr_big, scsi_autoconf | SCSI_SILENT, 20000);
1037 		} else {
1038 			rv = scsi_mode_select(sc->sc_link, SMS_PF,
1039 			    &buf->hdr, scsi_autoconf | SCSI_SILENT, 20000);
1040 		}
1041 		break;
1042 	}
1043 
1044 done:
1045 	dma_free(buf, sizeof(*buf));
1046 	return (rv);
1047 }
1048 
1049 /*
1050  * Load the label information on the named device
1051  */
1052 int
1053 sdgetdisklabel(dev_t dev, struct sd_softc *sc, struct disklabel *lp,
1054     int spoofonly)
1055 {
1056 	size_t len;
1057 	char packname[sizeof(lp->d_packname) + 1];
1058 	char product[17], vendor[9];
1059 
1060 	bzero(lp, sizeof(struct disklabel));
1061 
1062 	lp->d_secsize = sc->params.secsize;
1063 	lp->d_ntracks = sc->params.heads;
1064 	lp->d_nsectors = sc->params.sectors;
1065 	lp->d_ncylinders = sc->params.cyls;
1066 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1067 	if (lp->d_secpercyl == 0) {
1068 		lp->d_secpercyl = 100;
1069 		/* as long as it's not 0 - readdisklabel divides by it */
1070 	}
1071 
1072 	lp->d_type = DTYPE_SCSI;
1073 	if ((sc->sc_link->inqdata.device & SID_TYPE) == T_OPTICAL)
1074 		strncpy(lp->d_typename, "SCSI optical",
1075 		    sizeof(lp->d_typename));
1076 	else
1077 		strncpy(lp->d_typename, "SCSI disk",
1078 		    sizeof(lp->d_typename));
1079 
1080 	/*
1081 	 * Try to fit '<vendor> <product>' into d_packname. If that doesn't fit
1082 	 * then leave out '<vendor> ' and use only as much of '<product>' as
1083 	 * does fit.
1084 	 */
1085 	viscpy(vendor, sc->sc_link->inqdata.vendor, 8);
1086 	viscpy(product, sc->sc_link->inqdata.product, 16);
1087 	len = snprintf(packname, sizeof(packname), "%s %s", vendor, product);
1088 	if (len > sizeof(lp->d_packname)) {
1089 		strlcpy(packname, product, sizeof(packname));
1090 		len = strlen(packname);
1091 	}
1092 	/*
1093 	 * It is safe to use len as the count of characters to copy because
1094 	 * packname is sizeof(lp->d_packname)+1, the string in packname is
1095 	 * always null terminated and len does not count the terminating null.
1096 	 * d_packname is not a null terminated string.
1097 	 */
1098 	bcopy(packname, lp->d_packname, len);
1099 
1100 	DL_SETDSIZE(lp, sc->params.disksize);
1101 	lp->d_version = 1;
1102 	lp->d_flags = 0;
1103 
1104 	/* XXX - these values for BBSIZE and SBSIZE assume ffs */
1105 	lp->d_bbsize = BBSIZE;
1106 	lp->d_sbsize = SBSIZE;
1107 
1108 	lp->d_magic = DISKMAGIC;
1109 	lp->d_magic2 = DISKMAGIC;
1110 	lp->d_checksum = dkcksum(lp);
1111 
1112 	/*
1113 	 * Call the generic disklabel extraction routine
1114 	 */
1115 	return readdisklabel(DISKLABELDEV(dev), sdstrategy, lp, spoofonly);
1116 }
1117 
1118 
1119 /*
1120  * Check Errors
1121  */
1122 int
1123 sd_interpret_sense(struct scsi_xfer *xs)
1124 {
1125 	struct scsi_sense_data *sense = &xs->sense;
1126 	struct scsi_link *sc_link = xs->sc_link;
1127 	struct sd_softc *sc = sc_link->device_softc;
1128 	u_int8_t serr = sense->error_code & SSD_ERRCODE;
1129 	int retval;
1130 
1131 	/*
1132 	 * Let the generic code handle everything except a few categories of
1133 	 * LUN not ready errors on open devices.
1134 	 */
1135 	if (((sc_link->flags & SDEV_OPEN) == 0) ||
1136 	    (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED) ||
1137 	    ((sense->flags & SSD_KEY) != SKEY_NOT_READY) ||
1138 	    (sense->extra_len < 6))
1139 		return (scsi_interpret_sense(xs));
1140 
1141 	switch (ASC_ASCQ(sense)) {
1142 	case SENSE_NOT_READY_BECOMING_READY:
1143 		SC_DEBUG(sc_link, SDEV_DB1, ("becoming ready.\n"));
1144 		retval = scsi_delay(xs, 5);
1145 		break;
1146 
1147 	case SENSE_NOT_READY_INIT_REQUIRED:
1148 		SC_DEBUG(sc_link, SDEV_DB1, ("spinning up\n"));
1149 		retval = scsi_start(sc->sc_link, SSS_START,
1150 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_NOSLEEP);
1151 		if (retval == 0)
1152 			retval = ERESTART;
1153 		else if (retval == ENOMEM)
1154 			/* Can't issue the command. Fall back on a delay. */
1155 			retval = scsi_delay(xs, 5);
1156 		else
1157 			SC_DEBUG(sc_link, SDEV_DB1, ("spin up failed (%#x)\n",
1158 			    retval));
1159 		break;
1160 
1161 	default:
1162 		retval = scsi_interpret_sense(xs);
1163 		break;
1164 	}
1165 
1166 	return (retval);
1167 }
1168 
1169 daddr_t
1170 sdsize(dev_t dev)
1171 {
1172 	struct disklabel *lp;
1173 	struct sd_softc *sc;
1174 	int part, omask;
1175 	daddr_t size;
1176 
1177 	sc = sdlookup(DISKUNIT(dev));
1178 	if (sc == NULL)
1179 		return -1;
1180 	if (sc->flags & SDF_DYING) {
1181 		size = -1;
1182 		goto exit;
1183 	}
1184 
1185 	part = DISKPART(dev);
1186 	omask = sc->sc_dk.dk_openmask & (1 << part);
1187 
1188 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) {
1189 		size = -1;
1190 		goto exit;
1191 	}
1192 
1193 	lp = sc->sc_dk.dk_label;
1194 	if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
1195 		size = -1;
1196 	else if (lp->d_partitions[part].p_fstype != FS_SWAP)
1197 		size = -1;
1198 	else
1199 		size = DL_SECTOBLK(lp, DL_GETPSIZE(&lp->d_partitions[part]));
1200 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1201 		size = -1;
1202 
1203  exit:
1204 	device_unref(&sc->sc_dev);
1205 	return size;
1206 }
1207 
1208 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1209 static int sddoingadump;
1210 
1211 /*
1212  * dump all of physical memory into the partition specified, starting
1213  * at offset 'dumplo' into the partition.
1214  */
1215 int
1216 sddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1217 {
1218 	struct sd_softc *sc;	/* disk unit to do the I/O */
1219 	struct disklabel *lp;	/* disk's disklabel */
1220 	int	unit, part;
1221 	u_int32_t sectorsize;	/* size of a disk sector */
1222 	u_int64_t nsects;	/* number of sectors in partition */
1223 	u_int64_t sectoff;	/* sector offset of partition */
1224 	u_int64_t totwrt;	/* total number of sectors left to write */
1225 	u_int32_t nwrt;		/* current number of sectors to write */
1226 	struct scsi_xfer *xs;	/* ... convenience */
1227 	int rv;
1228 
1229 	/* Check if recursive dump; if so, punt. */
1230 	if (sddoingadump)
1231 		return EFAULT;
1232 	if (blkno < 0)
1233 		return EINVAL;
1234 
1235 	/* Mark as active early. */
1236 	sddoingadump = 1;
1237 
1238 	unit = DISKUNIT(dev);	/* Decompose unit & partition. */
1239 	part = DISKPART(dev);
1240 
1241 	/* Check for acceptable drive number. */
1242 	if (unit >= sd_cd.cd_ndevs || (sc = sd_cd.cd_devs[unit]) == NULL)
1243 		return ENXIO;
1244 
1245 	/*
1246 	 * XXX Can't do this check, since the media might have been
1247 	 * XXX marked `invalid' by successful unmounting of all
1248 	 * XXX filesystems.
1249 	 */
1250 #if 0
1251 	/* Make sure it was initialized. */
1252 	if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1253 		return ENXIO;
1254 #endif
1255 
1256 	/* Convert to disk sectors.  Request must be a multiple of size. */
1257 	lp = sc->sc_dk.dk_label;
1258 	sectorsize = lp->d_secsize;
1259 	if ((size % sectorsize) != 0)
1260 		return EFAULT;
1261 	if ((blkno % DL_BLKSPERSEC(lp)) != 0)
1262 		return EFAULT;
1263 	totwrt = size / sectorsize;
1264 	blkno = DL_BLKTOSEC(lp, blkno);
1265 
1266 	nsects = DL_GETPSIZE(&lp->d_partitions[part]);
1267 	sectoff = DL_GETPOFFSET(&lp->d_partitions[part]);
1268 
1269 	/* Check transfer bounds against partition size. */
1270 	if ((blkno + totwrt) > nsects)
1271 		return EINVAL;
1272 
1273 	/* Offset block number to start of partition. */
1274 	blkno += sectoff;
1275 
1276 	while (totwrt > 0) {
1277 		if (totwrt > UINT32_MAX)
1278 			nwrt = UINT32_MAX;
1279 		else
1280 			nwrt = totwrt;
1281 
1282 #ifndef	SD_DUMP_NOT_TRUSTED
1283 		xs = scsi_xs_get(sc->sc_link, SCSI_NOSLEEP);
1284 		if (xs == NULL)
1285 			return (ENOMEM);
1286 
1287 		xs->timeout = 10000;
1288 		xs->flags |= SCSI_DATA_OUT;
1289 		xs->data = va;
1290 		xs->datalen = nwrt * sectorsize;
1291 
1292 		sd_cmd_rw10(xs, 0, blkno, nwrt); /* XXX */
1293 
1294 		rv = scsi_xs_sync(xs);
1295 		scsi_xs_put(xs);
1296 		if (rv != 0)
1297 			return (ENXIO);
1298 #else	/* SD_DUMP_NOT_TRUSTED */
1299 		/* Let's just talk about this first... */
1300 		printf("sd%d: dump addr 0x%x, blk %lld\n", unit, va,
1301 		    (long long)blkno);
1302 		delay(500 * 1000);	/* half a second */
1303 #endif	/* SD_DUMP_NOT_TRUSTED */
1304 
1305 		/* update block count */
1306 		totwrt -= nwrt;
1307 		blkno += nwrt;
1308 		va += sectorsize * nwrt;
1309 	}
1310 
1311 	sddoingadump = 0;
1312 
1313 	return (0);
1314 }
1315 
1316 /*
1317  * Copy up to len chars from src to dst, ignoring non-printables.
1318  * Must be room for len+1 chars in dst so we can write the NUL.
1319  * Does not assume src is NUL-terminated.
1320  */
1321 void
1322 viscpy(u_char *dst, u_char *src, int len)
1323 {
1324 	while (len > 0 && *src != '\0') {
1325 		if (*src < 0x20 || *src >= 0x80) {
1326 			src++;
1327 			continue;
1328 		}
1329 		*dst++ = *src++;
1330 		len--;
1331 	}
1332 	*dst = '\0';
1333 }
1334 
1335 int
1336 sd_read_cap_10(struct sd_softc *sc, int flags)
1337 {
1338 	struct scsi_read_capacity cdb;
1339 	struct scsi_read_cap_data *rdcap;
1340 	struct scsi_xfer *xs;
1341 	int rv = ENOMEM;
1342 
1343 	CLR(flags, SCSI_IGNORE_ILLEGAL_REQUEST);
1344 
1345 	rdcap = dma_alloc(sizeof(*rdcap), (ISSET(flags, SCSI_NOSLEEP) ?
1346 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1347 	if (rdcap == NULL)
1348 		return (ENOMEM);
1349 
1350 	xs = scsi_xs_get(sc->sc_link, flags | SCSI_DATA_IN | SCSI_SILENT);
1351 	if (xs == NULL)
1352 		goto done;
1353 
1354 	bzero(&cdb, sizeof(cdb));
1355 	cdb.opcode = READ_CAPACITY;
1356 
1357 	memcpy(xs->cmd, &cdb, sizeof(cdb));
1358 	xs->cmdlen = sizeof(cdb);
1359 	xs->data = (void *)rdcap;
1360 	xs->datalen = sizeof(*rdcap);
1361 	xs->timeout = 20000;
1362 
1363 	rv = scsi_xs_sync(xs);
1364 	scsi_xs_put(xs);
1365 
1366 	if (rv == 0) {
1367 		sc->params.disksize = _4btol(rdcap->addr) + 1ll;
1368 		sc->params.secsize = _4btol(rdcap->length);
1369 		CLR(sc->flags, SDF_THIN);
1370 	}
1371 
1372  done:
1373 	dma_free(rdcap, sizeof(*rdcap));
1374 	return (rv);
1375 }
1376 
1377 int
1378 sd_read_cap_16(struct sd_softc *sc, int flags)
1379 {
1380 	struct scsi_read_capacity_16 cdb;
1381 	struct scsi_read_cap_data_16 *rdcap;
1382 	struct scsi_xfer *xs;
1383 	int rv = ENOMEM;
1384 
1385 	CLR(flags, SCSI_IGNORE_ILLEGAL_REQUEST);
1386 
1387 	rdcap = dma_alloc(sizeof(*rdcap), (ISSET(flags, SCSI_NOSLEEP) ?
1388 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1389 	if (rdcap == NULL)
1390 		return (ENOMEM);
1391 
1392 	xs = scsi_xs_get(sc->sc_link, flags | SCSI_DATA_IN | SCSI_SILENT);
1393 	if (xs == NULL)
1394 		goto done;
1395 
1396 	bzero(&cdb, sizeof(cdb));
1397 	cdb.opcode = READ_CAPACITY_16;
1398 	cdb.byte2 = SRC16_SERVICE_ACTION;
1399 	_lto4b(sizeof(*rdcap), cdb.length);
1400 
1401 	memcpy(xs->cmd, &cdb, sizeof(cdb));
1402 	xs->cmdlen = sizeof(cdb);
1403 	xs->data = (void *)rdcap;
1404 	xs->datalen = sizeof(*rdcap);
1405 	xs->timeout = 20000;
1406 
1407 	rv = scsi_xs_sync(xs);
1408 	scsi_xs_put(xs);
1409 
1410 	if (rv == 0) {
1411 		if (_8btol(rdcap->addr) == 0) {
1412 			rv = EIO;
1413 			goto done;
1414 		}
1415 
1416 		sc->params.disksize = _8btol(rdcap->addr) + 1;
1417 		sc->params.secsize = _4btol(rdcap->length);
1418 		if (ISSET(_2btol(rdcap->lowest_aligned), READ_CAP_16_TPE))
1419 			SET(sc->flags, SDF_THIN);
1420 		else
1421 			CLR(sc->flags, SDF_THIN);
1422 	}
1423 
1424  done:
1425 	dma_free(rdcap, sizeof(*rdcap));
1426 	return (rv);
1427 }
1428 
1429 int
1430 sd_size(struct sd_softc *sc, int flags)
1431 {
1432 	int rv;
1433 
1434 	if (SCSISPC(sc->sc_link->inqdata.version) >= 3) {
1435 		rv = sd_read_cap_16(sc, flags);
1436 		if (rv != 0)
1437 			rv = sd_read_cap_10(sc, flags);
1438 	} else {
1439 		rv = sd_read_cap_10(sc, flags);
1440 		if (rv == 0 && sc->params.disksize == 0x100000000ll)
1441 			rv = sd_read_cap_16(sc, flags);
1442 	}
1443 
1444 	return (rv);
1445 }
1446 
1447 int
1448 sd_thin_pages(struct sd_softc *sc, int flags)
1449 {
1450 	struct scsi_vpd_hdr *pg;
1451 	size_t len = 0;
1452 	u_int8_t *pages;
1453 	int i, score = 0;
1454 	int rv;
1455 
1456 	pg = dma_alloc(sizeof(*pg), (ISSET(flags, SCSI_NOSLEEP) ?
1457 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1458 	if (pg == NULL)
1459 		return (ENOMEM);
1460 
1461 	rv = scsi_inquire_vpd(sc->sc_link, pg, sizeof(*pg),
1462 	    SI_PG_SUPPORTED, flags);
1463 	if (rv != 0)
1464 		goto done;
1465 
1466 	len = _2btol(pg->page_length);
1467 
1468 	dma_free(pg, sizeof(*pg));
1469 	pg = dma_alloc(sizeof(*pg) + len, (ISSET(flags, SCSI_NOSLEEP) ?
1470 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1471 	if (pg == NULL)
1472 		return (ENOMEM);
1473 
1474 	rv = scsi_inquire_vpd(sc->sc_link, pg, sizeof(*pg) + len,
1475 	    SI_PG_SUPPORTED, flags);
1476 	if (rv != 0)
1477 		goto done;
1478 
1479 	pages = (u_int8_t *)(pg + 1);
1480 	if (pages[0] != SI_PG_SUPPORTED) {
1481 		rv = EIO;
1482 		goto done;
1483 	}
1484 
1485 	for (i = 1; i < len; i++) {
1486 		switch (pages[i]) {
1487 		case SI_PG_DISK_LIMITS:
1488 		case SI_PG_DISK_THIN:
1489 			score++;
1490 			break;
1491 		}
1492 	}
1493 
1494 	if (score < 2)
1495 		rv = EOPNOTSUPP;
1496 
1497  done:
1498 	dma_free(pg, sizeof(*pg) + len);
1499 	return (rv);
1500 }
1501 
1502 int
1503 sd_vpd_block_limits(struct sd_softc *sc, int flags)
1504 {
1505 	struct scsi_vpd_disk_limits *pg;
1506 	int rv;
1507 
1508 	pg = dma_alloc(sizeof(*pg), (ISSET(flags, SCSI_NOSLEEP) ?
1509 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1510 	if (pg == NULL)
1511 		return (ENOMEM);
1512 
1513 	rv = scsi_inquire_vpd(sc->sc_link, pg, sizeof(*pg),
1514 	    SI_PG_DISK_LIMITS, flags);
1515 	if (rv != 0)
1516 		goto done;
1517 
1518 	if (_2btol(pg->hdr.page_length) == SI_PG_DISK_LIMITS_LEN_THIN) {
1519 		sc->params.unmap_sectors = _4btol(pg->max_unmap_lba_count);
1520 		sc->params.unmap_descs = _4btol(pg->max_unmap_desc_count);
1521 	} else
1522 		rv = EOPNOTSUPP;
1523 
1524  done:
1525 	dma_free(pg, sizeof(*pg));
1526 	return (rv);
1527 }
1528 
1529 int
1530 sd_vpd_thin(struct sd_softc *sc, int flags)
1531 {
1532 	struct scsi_vpd_disk_thin *pg;
1533 	int rv;
1534 
1535 	pg = dma_alloc(sizeof(*pg), (ISSET(flags, SCSI_NOSLEEP) ?
1536 	    PR_NOWAIT : PR_WAITOK) | PR_ZERO);
1537 	if (pg == NULL)
1538 		return (ENOMEM);
1539 
1540 	rv = scsi_inquire_vpd(sc->sc_link, pg, sizeof(*pg),
1541 	    SI_PG_DISK_THIN, flags);
1542 	if (rv != 0)
1543 		goto done;
1544 
1545 #ifdef notyet
1546 	if (ISSET(pg->flags, VPD_DISK_THIN_TPU))
1547 		sc->sc_delete = sd_unmap;
1548 	else if (ISSET(pg->flags, VPD_DISK_THIN_TPWS)) {
1549 		sc->sc_delete = sd_write_same_16;
1550 		sc->params.unmap_descs = 1; /* WRITE SAME 16 only does one */
1551 	} else
1552 		rv = EOPNOTSUPP;
1553 #endif
1554 
1555  done:
1556 	dma_free(pg, sizeof(*pg));
1557 	return (rv);
1558 }
1559 
1560 int
1561 sd_thin_params(struct sd_softc *sc, int flags)
1562 {
1563 	int rv;
1564 
1565 	rv = sd_thin_pages(sc, flags);
1566 	if (rv != 0)
1567 		return (rv);
1568 
1569 	rv = sd_vpd_block_limits(sc, flags);
1570 	if (rv != 0)
1571 		return (rv);
1572 
1573 	rv = sd_vpd_thin(sc, flags);
1574 	if (rv != 0)
1575 		return (rv);
1576 
1577 	return (0);
1578 }
1579 
1580 /*
1581  * Fill out the disk parameter structure. Return SDGP_RESULT_OK if the
1582  * structure is correctly filled in, SDGP_RESULT_OFFLINE otherwise. The caller
1583  * is responsible for clearing the SDEV_MEDIA_LOADED flag if the structure
1584  * cannot be completed.
1585  */
1586 int
1587 sd_get_parms(struct sd_softc *sc, struct disk_parms *dp, int flags)
1588 {
1589 	union scsi_mode_sense_buf *buf = NULL;
1590 	struct page_rigid_geometry *rigid = NULL;
1591 	struct page_flex_geometry *flex = NULL;
1592 	struct page_reduced_geometry *reduced = NULL;
1593 	u_char *page0 = NULL;
1594 	u_int32_t heads = 0, sectors = 0, cyls = 0, secsize = 0;
1595 	int err = 0, big;
1596 
1597 	if (sd_size(sc, flags) != 0)
1598 		return (SDGP_RESULT_OFFLINE);
1599 
1600 	if (ISSET(sc->flags, SDF_THIN) && sd_thin_params(sc, flags) != 0) {
1601 		/* we dont know the unmap limits, so we cant use thin shizz */
1602 		CLR(sc->flags, SDF_THIN);
1603 	}
1604 
1605 	buf = dma_alloc(sizeof(*buf), PR_NOWAIT);
1606 	if (buf == NULL)
1607 		goto validate;
1608 
1609 	/*
1610 	 * Ask for page 0 (vendor specific) mode sense data to find
1611 	 * READONLY info. The only thing USB devices will ask for.
1612 	 */
1613 	err = scsi_do_mode_sense(sc->sc_link, 0, buf, (void **)&page0,
1614 	    NULL, NULL, NULL, 1, flags | SCSI_SILENT, &big);
1615 	if (err == 0) {
1616 		if (big && buf->hdr_big.dev_spec & SMH_DSP_WRITE_PROT)
1617 			SET(sc->sc_link->flags, SDEV_READONLY);
1618 		else if (!big && buf->hdr.dev_spec & SMH_DSP_WRITE_PROT)
1619 			SET(sc->sc_link->flags, SDEV_READONLY);
1620 		else
1621 			CLR(sc->sc_link->flags, SDEV_READONLY);
1622 	}
1623 
1624 	/*
1625 	 * Many UMASS devices choke when asked about their geometry. Most
1626 	 * don't have a meaningful geometry anyway, so just fake it if
1627 	 * scsi_size() worked.
1628 	 */
1629 	if ((sc->sc_link->flags & SDEV_UMASS) && (dp->disksize > 0))
1630 		goto validate;
1631 
1632 	switch (sc->sc_link->inqdata.device & SID_TYPE) {
1633 	case T_OPTICAL:
1634 		/* No more information needed or available. */
1635 		break;
1636 
1637 	case T_RDIRECT:
1638 		/* T_RDIRECT supports only PAGE_REDUCED_GEOMETRY (6). */
1639 		err = scsi_do_mode_sense(sc->sc_link, PAGE_REDUCED_GEOMETRY,
1640 		    buf, (void **)&reduced, NULL, NULL, &secsize,
1641 		    sizeof(*reduced), flags | SCSI_SILENT, NULL);
1642 		if (!err && reduced &&
1643 		    DISK_PGCODE(reduced, PAGE_REDUCED_GEOMETRY)) {
1644 			if (dp->disksize == 0)
1645 				dp->disksize = _5btol(reduced->sectors);
1646 			if (secsize == 0)
1647 				secsize = _2btol(reduced->bytes_s);
1648 		}
1649 		break;
1650 
1651 	default:
1652 		/*
1653 		 * NOTE: Some devices leave off the last four bytes of
1654 		 * PAGE_RIGID_GEOMETRY and PAGE_FLEX_GEOMETRY mode sense pages.
1655 		 * The only information in those four bytes is RPM information
1656 		 * so accept the page. The extra bytes will be zero and RPM will
1657 		 * end up with the default value of 3600.
1658 		 */
1659 		if (((sc->sc_link->flags & SDEV_ATAPI) == 0) ||
1660 		    ((sc->sc_link->flags & SDEV_REMOVABLE) == 0))
1661 			err = scsi_do_mode_sense(sc->sc_link,
1662 			    PAGE_RIGID_GEOMETRY, buf, (void **)&rigid, NULL,
1663 			    NULL, &secsize, sizeof(*rigid) - 4,
1664 			    flags | SCSI_SILENT, NULL);
1665 		if (!err && rigid && DISK_PGCODE(rigid, PAGE_RIGID_GEOMETRY)) {
1666 			heads = rigid->nheads;
1667 			cyls = _3btol(rigid->ncyl);
1668 			if (heads * cyls > 0)
1669 				sectors = dp->disksize / (heads * cyls);
1670 		} else {
1671 			err = scsi_do_mode_sense(sc->sc_link,
1672 			    PAGE_FLEX_GEOMETRY, buf, (void **)&flex, NULL, NULL,
1673 			    &secsize, sizeof(*flex) - 4,
1674 			    flags | SCSI_SILENT, NULL);
1675 			if (!err && flex &&
1676 			    DISK_PGCODE(flex, PAGE_FLEX_GEOMETRY)) {
1677 				sectors = flex->ph_sec_tr;
1678 				heads = flex->nheads;
1679 				cyls = _2btol(flex->ncyl);
1680 				if (secsize == 0)
1681 					secsize = _2btol(flex->bytes_s);
1682 				if (dp->disksize == 0)
1683 					dp->disksize = heads * cyls * sectors;
1684 			}
1685 		}
1686 		break;
1687 	}
1688 
1689 validate:
1690 	if (buf)
1691 		dma_free(buf, sizeof(*buf));
1692 
1693 	if (dp->disksize == 0)
1694 		return (SDGP_RESULT_OFFLINE);
1695 
1696 	if (dp->secsize == 0)
1697 		dp->secsize = (secsize == 0) ? 512 : secsize;
1698 
1699 	/*
1700 	 * Restrict secsize values to powers of two between 512 and 64k.
1701 	 */
1702 	switch (dp->secsize) {
1703 	case 0x200:	/* == 512, == DEV_BSIZE on all architectures. */
1704 	case 0x400:
1705 	case 0x800:
1706 	case 0x1000:
1707 	case 0x2000:
1708 	case 0x4000:
1709 	case 0x8000:
1710 	case 0x10000:
1711 		break;
1712 	default:
1713 		SC_DEBUG(sc->sc_link, SDEV_DB1,
1714 		    ("sd_get_parms: bad secsize: %#x\n", dp->secsize));
1715 		return (SDGP_RESULT_OFFLINE);
1716 	}
1717 
1718 	/*
1719 	 * XXX THINK ABOUT THIS!!  Using values such that sectors * heads *
1720 	 * cyls is <= disk_size can lead to wasted space. We need a more
1721 	 * careful calculation/validation to make everything work out
1722 	 * optimally.
1723 	 */
1724 	if (dp->disksize > 0xffffffff && (dp->heads * dp->sectors) < 0xffff) {
1725 		dp->heads = 511;
1726 		dp->sectors = 255;
1727 		cyls = 0;
1728 	} else {
1729 		/*
1730 		 * Use standard geometry values for anything we still don't
1731 		 * know.
1732 		 */
1733 		dp->heads = (heads == 0) ? 255 : heads;
1734 		dp->sectors = (sectors == 0) ? 63 : sectors;
1735 	}
1736 
1737 	dp->cyls = (cyls == 0) ? dp->disksize / (dp->heads * dp->sectors) :
1738 	    cyls;
1739 
1740 	if (dp->cyls == 0) {
1741 		dp->heads = dp->cyls = 1;
1742 		dp->sectors = dp->disksize;
1743 	}
1744 
1745 	return (SDGP_RESULT_OK);
1746 }
1747 
1748 void
1749 sd_flush(struct sd_softc *sc, int flags)
1750 {
1751 	struct scsi_link *link = sc->sc_link;
1752 	struct scsi_xfer *xs;
1753 	struct scsi_synchronize_cache *cmd;
1754 
1755 	if (link->quirks & SDEV_NOSYNCCACHE)
1756 		return;
1757 
1758 	/*
1759 	 * Issue a SYNCHRONIZE CACHE. Address 0, length 0 means "all remaining
1760 	 * blocks starting at address 0". Ignore ILLEGAL REQUEST in the event
1761 	 * that the command is not supported by the device.
1762 	 */
1763 
1764 	xs = scsi_xs_get(link, flags);
1765 	if (xs == NULL) {
1766 		SC_DEBUG(link, SDEV_DB1, ("cache sync failed to get xs\n"));
1767 		return;
1768 	}
1769 
1770 	cmd = (struct scsi_synchronize_cache *)xs->cmd;
1771 	cmd->opcode = SYNCHRONIZE_CACHE;
1772 
1773 	xs->cmdlen = sizeof(*cmd);
1774 	xs->timeout = 100000;
1775 	xs->flags |= SCSI_IGNORE_ILLEGAL_REQUEST;
1776 
1777 	if (scsi_xs_sync(xs) == 0)
1778 		sc->flags &= ~SDF_DIRTY;
1779 	else
1780 		SC_DEBUG(link, SDEV_DB1, ("cache sync failed\n"));
1781 
1782 	scsi_xs_put(xs);
1783 }
1784