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