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