xref: /netbsd-src/sys/dev/scsipi/sd.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: sd.c,v 1.273 2008/04/28 20:23:58 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Originally written by Julian Elischer (julian@dialix.oz.au)
34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
35  *
36  * TRW Financial Systems, in accordance with their agreement with Carnegie
37  * Mellon University, makes this software available to CMU to distribute
38  * or use in any manner that they see fit as long as this message is kept with
39  * the software. For this reason TFS also grants any other persons or
40  * organisations permission to use or modify this software.
41  *
42  * TFS supplies this software to be publicly redistributed
43  * on the understanding that TFS is not responsible for the correct
44  * functioning of this software in any circumstances.
45  *
46  * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.273 2008/04/28 20:23:58 martin Exp $");
51 
52 #include "opt_scsi.h"
53 #include "rnd.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/file.h>
59 #include <sys/stat.h>
60 #include <sys/ioctl.h>
61 #include <sys/scsiio.h>
62 #include <sys/buf.h>
63 #include <sys/bufq.h>
64 #include <sys/uio.h>
65 #include <sys/malloc.h>
66 #include <sys/errno.h>
67 #include <sys/device.h>
68 #include <sys/disklabel.h>
69 #include <sys/disk.h>
70 #include <sys/proc.h>
71 #include <sys/conf.h>
72 #include <sys/vnode.h>
73 #if NRND > 0
74 #include <sys/rnd.h>
75 #endif
76 
77 #include <dev/scsipi/scsi_spc.h>
78 #include <dev/scsipi/scsipi_all.h>
79 #include <dev/scsipi/scsi_all.h>
80 #include <dev/scsipi/scsipi_disk.h>
81 #include <dev/scsipi/scsi_disk.h>
82 #include <dev/scsipi/scsiconf.h>
83 #include <dev/scsipi/scsipi_base.h>
84 #include <dev/scsipi/sdvar.h>
85 
86 #include <prop/proplib.h>
87 
88 #define	SDUNIT(dev)			DISKUNIT(dev)
89 #define	SDPART(dev)			DISKPART(dev)
90 #define	SDMINOR(unit, part)		DISKMINOR(unit, part)
91 #define	MAKESDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
92 
93 #define	SDLABELDEV(dev)	(MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
94 
95 #define	SD_DEFAULT_BLKSIZE	512
96 
97 static void	sdminphys(struct buf *);
98 static void	sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
99 static int	sdgetdisklabel(struct sd_softc *);
100 static void	sdstart(struct scsipi_periph *);
101 static void	sdrestart(void *);
102 static void	sddone(struct scsipi_xfer *, int);
103 static bool	sd_suspend(device_t PMF_FN_PROTO);
104 static void	sd_shutdown(void *);
105 static int	sd_interpret_sense(struct scsipi_xfer *);
106 
107 static int	sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int,
108 		    int, int *);
109 static int	sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int,
110 		    int);
111 static int	sd_validate_blksize(struct scsipi_periph *, int);
112 static u_int64_t sd_read_capacity(struct scsipi_periph *, int *, int flags);
113 static int	sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *,
114 		    int);
115 static int	sd_get_capacity(struct sd_softc *, struct disk_parms *, int);
116 static int	sd_get_parms(struct sd_softc *, struct disk_parms *, int);
117 static int	sd_get_parms_page4(struct sd_softc *, struct disk_parms *,
118 		    int);
119 static int	sd_get_parms_page5(struct sd_softc *, struct disk_parms *,
120 		    int);
121 
122 static int	sd_flush(struct sd_softc *, int);
123 static int	sd_getcache(struct sd_softc *, int *);
124 static int	sd_setcache(struct sd_softc *, int);
125 
126 static int	sdmatch(struct device *, struct cfdata *, void *);
127 static void	sdattach(struct device *, struct device *, void *);
128 static int	sdactivate(struct device *, enum devact);
129 static int	sddetach(struct device *, int);
130 static void	sd_set_properties(struct sd_softc *);
131 
132 CFATTACH_DECL(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
133     sdactivate);
134 
135 extern struct cfdriver sd_cd;
136 
137 static const struct scsipi_inquiry_pattern sd_patterns[] = {
138 	{T_DIRECT, T_FIXED,
139 	 "",         "",                 ""},
140 	{T_DIRECT, T_REMOV,
141 	 "",         "",                 ""},
142 	{T_OPTICAL, T_FIXED,
143 	 "",         "",                 ""},
144 	{T_OPTICAL, T_REMOV,
145 	 "",         "",                 ""},
146 	{T_SIMPLE_DIRECT, T_FIXED,
147 	 "",         "",                 ""},
148 	{T_SIMPLE_DIRECT, T_REMOV,
149 	 "",         "",                 ""},
150 };
151 
152 static dev_type_open(sdopen);
153 static dev_type_close(sdclose);
154 static dev_type_read(sdread);
155 static dev_type_write(sdwrite);
156 static dev_type_ioctl(sdioctl);
157 static dev_type_strategy(sdstrategy);
158 static dev_type_dump(sddump);
159 static dev_type_size(sdsize);
160 
161 const struct bdevsw sd_bdevsw = {
162 	sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
163 };
164 
165 const struct cdevsw sd_cdevsw = {
166 	sdopen, sdclose, sdread, sdwrite, sdioctl,
167 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
168 };
169 
170 static struct dkdriver sddkdriver = { sdstrategy, sdminphys };
171 
172 static const struct scsipi_periphsw sd_switch = {
173 	sd_interpret_sense,	/* check our error handler first */
174 	sdstart,		/* have a queue, served by this */
175 	NULL,			/* have no async handler */
176 	sddone,			/* deal with stats at interrupt time */
177 };
178 
179 struct sd_mode_sense_data {
180 	/*
181 	 * XXX
182 	 * We are not going to parse this as-is -- it just has to be large
183 	 * enough.
184 	 */
185 	union {
186 		struct scsi_mode_parameter_header_6 small;
187 		struct scsi_mode_parameter_header_10 big;
188 	} header;
189 	struct scsi_general_block_descriptor blk_desc;
190 	union scsi_disk_pages pages;
191 };
192 
193 /*
194  * The routine called by the low level scsi routine when it discovers
195  * A device suitable for this driver
196  */
197 static int
198 sdmatch(struct device *parent, struct cfdata *match,
199     void *aux)
200 {
201 	struct scsipibus_attach_args *sa = aux;
202 	int priority;
203 
204 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
205 	    sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
206 	    sizeof(sd_patterns[0]), &priority);
207 
208 	return (priority);
209 }
210 
211 /*
212  * Attach routine common to atapi & scsi.
213  */
214 static void
215 sdattach(struct device *parent, struct device *self, void *aux)
216 {
217 	struct sd_softc *sd = device_private(self);
218 	struct scsipibus_attach_args *sa = aux;
219 	struct scsipi_periph *periph = sa->sa_periph;
220 	int error, result;
221 	struct disk_parms *dp = &sd->params;
222 	char pbuf[9];
223 
224 	SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
225 
226 	sd->type = (sa->sa_inqbuf.type & SID_TYPE);
227 	strncpy(sd->name, sa->sa_inqbuf.product, sizeof(sd->name));
228 	if (sd->type == T_SIMPLE_DIRECT)
229 		periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE;
230 
231 	if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
232 	    periph->periph_version == 0)
233 		sd->flags |= SDF_ANCIENT;
234 
235 	bufq_alloc(&sd->buf_queue, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
236 
237 	callout_init(&sd->sc_callout, 0);
238 
239 	/*
240 	 * Store information needed to contact our base driver
241 	 */
242 	sd->sc_periph = periph;
243 
244 	periph->periph_dev = &sd->sc_dev;
245 	periph->periph_switch = &sd_switch;
246 
247         /*
248          * Increase our openings to the maximum-per-periph
249          * supported by the adapter.  This will either be
250          * clamped down or grown by the adapter if necessary.
251          */
252 	periph->periph_openings =
253 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
254 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
255 
256 	/*
257 	 * Initialize and attach the disk structure.
258 	 */
259 	disk_init(&sd->sc_dk, device_xname(&sd->sc_dev), &sddkdriver);
260 	disk_attach(&sd->sc_dk);
261 
262 	/*
263 	 * Use the subdriver to request information regarding the drive.
264 	 */
265 	aprint_naive("\n");
266 	aprint_normal("\n");
267 
268 	error = scsipi_test_unit_ready(periph,
269 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
270 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
271 
272 	if (error)
273 		result = SDGP_RESULT_OFFLINE;
274 	else
275 		result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY);
276 	aprint_normal_dev(&sd->sc_dev, "");
277 	switch (result) {
278 	case SDGP_RESULT_OK:
279 		format_bytes(pbuf, sizeof(pbuf),
280 		    (u_int64_t)dp->disksize * dp->blksize);
281 	        aprint_normal(
282 		"%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors",
283 		    pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
284 		    (unsigned long long)dp->disksize);
285 		break;
286 
287 	case SDGP_RESULT_OFFLINE:
288 		aprint_normal("drive offline");
289 		break;
290 
291 	case SDGP_RESULT_UNFORMATTED:
292 		aprint_normal("unformatted media");
293 		break;
294 
295 #ifdef DIAGNOSTIC
296 	default:
297 		panic("sdattach: unknown result from get_parms");
298 		break;
299 #endif
300 	}
301 	aprint_normal("\n");
302 
303 	/*
304 	 * Establish a shutdown hook so that we can ensure that
305 	 * our data has actually made it onto the platter at
306 	 * shutdown time.  Note that this relies on the fact
307 	 * that the shutdown hook code puts us at the head of
308 	 * the list (thus guaranteeing that our hook runs before
309 	 * our ancestors').
310 	 */
311 	if ((sd->sc_sdhook =
312 	    shutdownhook_establish(sd_shutdown, sd)) == NULL)
313 		aprint_error_dev(&sd->sc_dev, "WARNING: unable to establish shutdown hook\n");
314 
315 	if (!pmf_device_register(self, sd_suspend, NULL))
316 		aprint_error_dev(self, "couldn't establish power handler\n");
317 
318 #if NRND > 0
319 	/*
320 	 * attach the device into the random source list
321 	 */
322 	rnd_attach_source(&sd->rnd_source, device_xname(&sd->sc_dev),
323 			  RND_TYPE_DISK, 0);
324 #endif
325 
326 	/* Discover wedges on this disk. */
327 	dkwedge_discover(&sd->sc_dk);
328 
329 	sd_set_properties(sd);
330 }
331 
332 static int
333 sdactivate(struct device *self, enum devact act)
334 {
335 	int rv = 0;
336 
337 	switch (act) {
338 	case DVACT_ACTIVATE:
339 		rv = EOPNOTSUPP;
340 		break;
341 
342 	case DVACT_DEACTIVATE:
343 		/*
344 		 * Nothing to do; we key off the device's DVF_ACTIVE.
345 		 */
346 		break;
347 	}
348 	return (rv);
349 }
350 
351 static int
352 sddetach(struct device *self, int flags)
353 {
354 	struct sd_softc *sd = device_private(self);
355 	int s, bmaj, cmaj, i, mn;
356 
357 	/* locate the major number */
358 	bmaj = bdevsw_lookup_major(&sd_bdevsw);
359 	cmaj = cdevsw_lookup_major(&sd_cdevsw);
360 
361 	/* Nuke the vnodes for any open instances */
362 	for (i = 0; i < MAXPARTITIONS; i++) {
363 		mn = SDMINOR(device_unit(self), i);
364 		vdevgone(bmaj, mn, mn, VBLK);
365 		vdevgone(cmaj, mn, mn, VCHR);
366 	}
367 
368 	/* kill any pending restart */
369 	callout_stop(&sd->sc_callout);
370 
371 	/* Delete all of our wedges. */
372 	dkwedge_delall(&sd->sc_dk);
373 
374 	s = splbio();
375 
376 	/* Kill off any queued buffers. */
377 	bufq_drain(sd->buf_queue);
378 
379 	bufq_free(sd->buf_queue);
380 
381 	/* Kill off any pending commands. */
382 	scsipi_kill_pending(sd->sc_periph);
383 
384 	splx(s);
385 
386 	/* Detach from the disk list. */
387 	disk_detach(&sd->sc_dk);
388 	disk_destroy(&sd->sc_dk);
389 
390 	pmf_device_deregister(self);
391 	shutdownhook_disestablish(sd->sc_sdhook);
392 
393 #if NRND > 0
394 	/* Unhook the entropy source. */
395 	rnd_detach_source(&sd->rnd_source);
396 #endif
397 
398 	return (0);
399 }
400 
401 /*
402  * open the device. Make sure the partition info is a up-to-date as can be.
403  */
404 static int
405 sdopen(dev_t dev, int flag, int fmt, struct lwp *l)
406 {
407 	struct sd_softc *sd;
408 	struct scsipi_periph *periph;
409 	struct scsipi_adapter *adapt;
410 	int unit, part;
411 	int error;
412 
413 	unit = SDUNIT(dev);
414 	if (unit >= sd_cd.cd_ndevs)
415 		return (ENXIO);
416 	sd = sd_cd.cd_devs[unit];
417 	if (sd == NULL)
418 		return (ENXIO);
419 
420 	if (!device_is_active(&sd->sc_dev))
421 		return (ENODEV);
422 
423 	part = SDPART(dev);
424 
425 	mutex_enter(&sd->sc_dk.dk_openlock);
426 
427 	/*
428 	 * If there are wedges, and this is not RAW_PART, then we
429 	 * need to fail.
430 	 */
431 	if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
432 		error = EBUSY;
433 		goto bad1;
434 	}
435 
436 	periph = sd->sc_periph;
437 	adapt = periph->periph_channel->chan_adapter;
438 
439 	SC_DEBUG(periph, SCSIPI_DB1,
440 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
441 	    sd_cd.cd_ndevs, part));
442 
443 	/*
444 	 * If this is the first open of this device, add a reference
445 	 * to the adapter.
446 	 */
447 	if (sd->sc_dk.dk_openmask == 0 &&
448 	    (error = scsipi_adapter_addref(adapt)) != 0)
449 		goto bad1;
450 
451 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
452 		/*
453 		 * If any partition is open, but the disk has been invalidated,
454 		 * disallow further opens of non-raw partition
455 		 */
456 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
457 		    (part != RAW_PART || fmt != S_IFCHR)) {
458 			error = EIO;
459 			goto bad2;
460 		}
461 	} else {
462 		int silent;
463 
464 		if (part == RAW_PART && fmt == S_IFCHR)
465 			silent = XS_CTL_SILENT;
466 		else
467 			silent = 0;
468 
469 		/* Check that it is still responding and ok. */
470 		error = scsipi_test_unit_ready(periph,
471 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
472 		    silent);
473 
474 		/*
475 		 * Start the pack spinning if necessary. Always allow the
476 		 * raw parition to be opened, for raw IOCTLs. Data transfers
477 		 * will check for SDEV_MEDIA_LOADED.
478 		 */
479 		if (error == EIO) {
480 			int error2;
481 
482 			error2 = scsipi_start(periph, SSS_START, silent);
483 			switch (error2) {
484 			case 0:
485 				error = 0;
486 				break;
487 			case EIO:
488 			case EINVAL:
489 				break;
490 			default:
491 				error = error2;
492 				break;
493 			}
494 		}
495 		if (error) {
496 			if (silent)
497 				goto out;
498 			goto bad2;
499 		}
500 
501 		periph->periph_flags |= PERIPH_OPEN;
502 
503 		if (periph->periph_flags & PERIPH_REMOVABLE) {
504 			/* Lock the pack in. */
505 			error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
506 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
507 			    XS_CTL_IGNORE_MEDIA_CHANGE |
508 			    XS_CTL_SILENT);
509 			if (error)
510 				goto bad3;
511 		}
512 
513 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
514 			int param_error;
515 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
516 
517 			/*
518 			 * Load the physical device parameters.
519 			 *
520 			 * Note that if media is present but unformatted,
521 			 * we allow the open (so that it can be formatted!).
522 			 * The drive should refuse real I/O, if the media is
523 			 * unformatted.
524 			 */
525 			if ((param_error = sd_get_parms(sd, &sd->params, 0))
526 			     == SDGP_RESULT_OFFLINE) {
527 				error = ENXIO;
528 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
529 				goto bad3;
530 			}
531 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
532 
533 			/* Load the partition info if not already loaded. */
534 			if (param_error == 0) {
535 				if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) {
536 					error = EIO;
537 					goto bad3;
538 				}
539 				SC_DEBUG(periph, SCSIPI_DB3,
540 				     ("Disklabel loaded "));
541 			}
542 		}
543 	}
544 
545 	/* Check that the partition exists. */
546 	if (part != RAW_PART &&
547 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
548 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
549 		error = ENXIO;
550 		goto bad3;
551 	}
552 
553  out:	/* Insure only one open at a time. */
554 	switch (fmt) {
555 	case S_IFCHR:
556 		sd->sc_dk.dk_copenmask |= (1 << part);
557 		break;
558 	case S_IFBLK:
559 		sd->sc_dk.dk_bopenmask |= (1 << part);
560 		break;
561 	}
562 	sd->sc_dk.dk_openmask =
563 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
564 
565 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
566 	mutex_exit(&sd->sc_dk.dk_openlock);
567 	return (0);
568 
569  bad3:
570 	if (sd->sc_dk.dk_openmask == 0) {
571 		if (periph->periph_flags & PERIPH_REMOVABLE)
572 			scsipi_prevent(periph, SPAMR_ALLOW,
573 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
574 			    XS_CTL_IGNORE_MEDIA_CHANGE |
575 			    XS_CTL_SILENT);
576 		periph->periph_flags &= ~PERIPH_OPEN;
577 	}
578 
579  bad2:
580 	if (sd->sc_dk.dk_openmask == 0)
581 		scsipi_adapter_delref(adapt);
582 
583  bad1:
584 	mutex_exit(&sd->sc_dk.dk_openlock);
585 	return (error);
586 }
587 
588 /*
589  * close the device.. only called if we are the LAST occurence of an open
590  * device.  Convenient now but usually a pain.
591  */
592 static int
593 sdclose(dev_t dev, int flag, int fmt, struct lwp *l)
594 {
595 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
596 	struct scsipi_periph *periph = sd->sc_periph;
597 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
598 	int part = SDPART(dev);
599 
600 	mutex_enter(&sd->sc_dk.dk_openlock);
601 	switch (fmt) {
602 	case S_IFCHR:
603 		sd->sc_dk.dk_copenmask &= ~(1 << part);
604 		break;
605 	case S_IFBLK:
606 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
607 		break;
608 	}
609 	sd->sc_dk.dk_openmask =
610 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
611 
612 	if (sd->sc_dk.dk_openmask == 0) {
613 		/*
614 		 * If the disk cache needs flushing, and the disk supports
615 		 * it, do it now.
616 		 */
617 		if ((sd->flags & SDF_DIRTY) != 0) {
618 			if (sd_flush(sd, 0)) {
619 				aprint_error_dev(&sd->sc_dev, "cache synchronization failed\n");
620 				sd->flags &= ~SDF_FLUSHING;
621 			} else
622 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
623 		}
624 
625 		scsipi_wait_drain(periph);
626 
627 		if (periph->periph_flags & PERIPH_REMOVABLE)
628 			scsipi_prevent(periph, SPAMR_ALLOW,
629 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
630 			    XS_CTL_IGNORE_NOT_READY |
631 			    XS_CTL_SILENT);
632 		periph->periph_flags &= ~PERIPH_OPEN;
633 
634 		scsipi_wait_drain(periph);
635 
636 		scsipi_adapter_delref(adapt);
637 	}
638 
639 	mutex_exit(&sd->sc_dk.dk_openlock);
640 	return (0);
641 }
642 
643 /*
644  * Actually translate the requested transfer into one the physical driver
645  * can understand.  The transfer is described by a buf and will include
646  * only one physical transfer.
647  */
648 static void
649 sdstrategy(struct buf *bp)
650 {
651 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
652 	struct scsipi_periph *periph = sd->sc_periph;
653 	struct disklabel *lp;
654 	daddr_t blkno;
655 	int s;
656 	bool sector_aligned;
657 
658 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
659 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
660 	    ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
661 	/*
662 	 * If the device has been made invalid, error out
663 	 */
664 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
665 	    !device_is_active(&sd->sc_dev)) {
666 		if (periph->periph_flags & PERIPH_OPEN)
667 			bp->b_error = EIO;
668 		else
669 			bp->b_error = ENODEV;
670 		goto done;
671 	}
672 
673 	lp = sd->sc_dk.dk_label;
674 
675 	/*
676 	 * The transfer must be a whole number of blocks, offset must not be
677 	 * negative.
678 	 */
679 	if (lp->d_secsize == DEV_BSIZE) {
680 		sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
681 	} else {
682 		sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
683 	}
684 	if (!sector_aligned || bp->b_blkno < 0) {
685 		bp->b_error = EINVAL;
686 		goto done;
687 	}
688 	/*
689 	 * If it's a null transfer, return immediatly
690 	 */
691 	if (bp->b_bcount == 0)
692 		goto done;
693 
694 	/*
695 	 * Do bounds checking, adjust transfer. if error, process.
696 	 * If end of partition, just return.
697 	 */
698 	if (SDPART(bp->b_dev) == RAW_PART) {
699 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
700 		    sd->params.disksize512) <= 0)
701 			goto done;
702 	} else {
703 		if (bounds_check_with_label(&sd->sc_dk, bp,
704 		    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
705 			goto done;
706 	}
707 
708 	/*
709 	 * Now convert the block number to absolute and put it in
710 	 * terms of the device's logical block size.
711 	 */
712 	if (lp->d_secsize == DEV_BSIZE)
713 		blkno = bp->b_blkno;
714 	else if (lp->d_secsize > DEV_BSIZE)
715 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
716 	else
717 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
718 
719 	if (SDPART(bp->b_dev) != RAW_PART)
720 		blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
721 
722 	bp->b_rawblkno = blkno;
723 
724 	s = splbio();
725 
726 	/*
727 	 * Place it in the queue of disk activities for this disk.
728 	 *
729 	 * XXX Only do disksort() if the current operating mode does not
730 	 * XXX include tagged queueing.
731 	 */
732 	BUFQ_PUT(sd->buf_queue, bp);
733 
734 	/*
735 	 * Tell the device to get going on the transfer if it's
736 	 * not doing anything, otherwise just wait for completion
737 	 */
738 	sdstart(sd->sc_periph);
739 
740 	splx(s);
741 	return;
742 
743 done:
744 	/*
745 	 * Correctly set the buf to indicate a completed xfer
746 	 */
747 	bp->b_resid = bp->b_bcount;
748 	biodone(bp);
749 }
750 
751 /*
752  * sdstart looks to see if there is a buf waiting for the device
753  * and that the device is not already busy. If both are true,
754  * It dequeues the buf and creates a scsi command to perform the
755  * transfer in the buf. The transfer request will call scsipi_done
756  * on completion, which will in turn call this routine again
757  * so that the next queued transfer is performed.
758  * The bufs are queued by the strategy routine (sdstrategy)
759  *
760  * This routine is also called after other non-queued requests
761  * have been made of the scsi driver, to ensure that the queue
762  * continues to be drained.
763  *
764  * must be called at the correct (highish) spl level
765  * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
766  */
767 static void
768 sdstart(struct scsipi_periph *periph)
769 {
770 	struct sd_softc *sd = (void *)periph->periph_dev;
771 	struct disklabel *lp = sd->sc_dk.dk_label;
772 	struct buf *bp = 0;
773 	struct scsipi_rw_16 cmd16;
774 	struct scsipi_rw_10 cmd_big;
775 	struct scsi_rw_6 cmd_small;
776 	struct scsipi_generic *cmdp;
777 	struct scsipi_xfer *xs;
778 	int nblks, cmdlen, error, flags;
779 
780 	SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
781 	/*
782 	 * Check if the device has room for another command
783 	 */
784 	while (periph->periph_active < periph->periph_openings) {
785 		/*
786 		 * there is excess capacity, but a special waits
787 		 * It'll need the adapter as soon as we clear out of the
788 		 * way and let it run (user level wait).
789 		 */
790 		if (periph->periph_flags & PERIPH_WAITING) {
791 			periph->periph_flags &= ~PERIPH_WAITING;
792 			wakeup((void *)periph);
793 			return;
794 		}
795 
796 		/*
797 		 * If the device has become invalid, abort all the
798 		 * reads and writes until all files have been closed and
799 		 * re-opened
800 		 */
801 		if (__predict_false(
802 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
803 			if ((bp = BUFQ_GET(sd->buf_queue)) != NULL) {
804 				bp->b_error = EIO;
805 				bp->b_resid = bp->b_bcount;
806 				biodone(bp);
807 				continue;
808 			} else {
809 				return;
810 			}
811 		}
812 
813 		/*
814 		 * See if there is a buf with work for us to do..
815 		 */
816 		if ((bp = BUFQ_PEEK(sd->buf_queue)) == NULL)
817 			return;
818 
819 		/*
820 		 * We have a buf, now we should make a command.
821 		 */
822 
823 		if (lp->d_secsize == DEV_BSIZE)
824 			nblks = bp->b_bcount >> DEV_BSHIFT;
825 		else
826 			nblks = howmany(bp->b_bcount, lp->d_secsize);
827 
828 		/*
829 		 * Fill out the scsi command.  Use the smallest CDB possible
830 		 * (6-byte, 10-byte, or 16-byte).
831 		 */
832 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
833 		    ((nblks & 0xff) == nblks) &&
834 		    !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
835 			/* 6-byte CDB */
836 			memset(&cmd_small, 0, sizeof(cmd_small));
837 			cmd_small.opcode = (bp->b_flags & B_READ) ?
838 			    SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
839 			_lto3b(bp->b_rawblkno, cmd_small.addr);
840 			cmd_small.length = nblks & 0xff;
841 			cmdlen = sizeof(cmd_small);
842 			cmdp = (struct scsipi_generic *)&cmd_small;
843 		} else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
844 			/* 10-byte CDB */
845 			memset(&cmd_big, 0, sizeof(cmd_big));
846 			cmd_big.opcode = (bp->b_flags & B_READ) ?
847 			    READ_10 : WRITE_10;
848 			_lto4b(bp->b_rawblkno, cmd_big.addr);
849 			_lto2b(nblks, cmd_big.length);
850 			cmdlen = sizeof(cmd_big);
851 			cmdp = (struct scsipi_generic *)&cmd_big;
852 		} else {
853 			/* 16-byte CDB */
854 			memset(&cmd16, 0, sizeof(cmd16));
855 			cmd16.opcode = (bp->b_flags & B_READ) ?
856 			    READ_16 : WRITE_16;
857 			_lto8b(bp->b_rawblkno, cmd16.addr);
858 			_lto4b(nblks, cmd16.length);
859 			cmdlen = sizeof(cmd16);
860 			cmdp = (struct scsipi_generic *)&cmd16;
861 		}
862 
863 		/* Instrumentation. */
864 		disk_busy(&sd->sc_dk);
865 
866 		/*
867 		 * Mark the disk dirty so that the cache will be
868 		 * flushed on close.
869 		 */
870 		if ((bp->b_flags & B_READ) == 0)
871 			sd->flags |= SDF_DIRTY;
872 
873 		/*
874 		 * Figure out what flags to use.
875 		 */
876 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
877 		if (bp->b_flags & B_READ)
878 			flags |= XS_CTL_DATA_IN;
879 		else
880 			flags |= XS_CTL_DATA_OUT;
881 
882 		/*
883 		 * Call the routine that chats with the adapter.
884 		 * Note: we cannot sleep as we may be an interrupt
885 		 */
886 		xs = scsipi_make_xs(periph, cmdp, cmdlen,
887 		    (u_char *)bp->b_data, bp->b_bcount,
888 		    SDRETRIES, SD_IO_TIMEOUT, bp, flags);
889 		if (__predict_false(xs == NULL)) {
890 			/*
891 			 * out of memory. Keep this buffer in the queue, and
892 			 * retry later.
893 			 */
894 			callout_reset(&sd->sc_callout, hz / 2, sdrestart,
895 			    periph);
896 			return;
897 		}
898 		/*
899 		 * need to dequeue the buffer before queuing the command,
900 		 * because cdstart may be called recursively from the
901 		 * HBA driver
902 		 */
903 #ifdef DIAGNOSTIC
904 		if (BUFQ_GET(sd->buf_queue) != bp)
905 			panic("sdstart(): dequeued wrong buf");
906 #else
907 		BUFQ_GET(sd->buf_queue);
908 #endif
909 		error = scsipi_execute_xs(xs);
910 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
911 		KASSERT(error == 0);
912 	}
913 }
914 
915 static void
916 sdrestart(void *v)
917 {
918 	int s = splbio();
919 	sdstart((struct scsipi_periph *)v);
920 	splx(s);
921 }
922 
923 static void
924 sddone(struct scsipi_xfer *xs, int error)
925 {
926 	struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
927 	struct buf *bp = xs->bp;
928 
929 	if (sd->flags & SDF_FLUSHING) {
930 		/* Flush completed, no longer dirty. */
931 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
932 	}
933 
934 	if (bp) {
935 		bp->b_error = error;
936 		bp->b_resid = xs->resid;
937 		if (error) {
938 			/* on a read/write error bp->b_resid is zero, so fix */
939 			bp->b_resid = bp->b_bcount;
940 		}
941 
942 		disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
943 		    (bp->b_flags & B_READ));
944 #if NRND > 0
945 		rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
946 #endif
947 
948 		biodone(bp);
949 	}
950 }
951 
952 static void
953 sdminphys(struct buf *bp)
954 {
955 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
956 	long xmax;
957 
958 	/*
959 	 * If the device is ancient, we want to make sure that
960 	 * the transfer fits into a 6-byte cdb.
961 	 *
962 	 * XXX Note that the SCSI-I spec says that 256-block transfers
963 	 * are allowed in a 6-byte read/write, and are specified
964 	 * by settng the "length" to 0.  However, we're conservative
965 	 * here, allowing only 255-block transfers in case an
966 	 * ancient device gets confused by length == 0.  A length of 0
967 	 * in a 10-byte read/write actually means 0 blocks.
968 	 */
969 	if ((sd->flags & SDF_ANCIENT) &&
970 	    ((sd->sc_periph->periph_flags &
971 	    (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
972 		xmax = sd->sc_dk.dk_label->d_secsize * 0xff;
973 
974 		if (bp->b_bcount > xmax)
975 			bp->b_bcount = xmax;
976 	}
977 
978 	scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
979 }
980 
981 static int
982 sdread(dev_t dev, struct uio *uio, int ioflag)
983 {
984 
985 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
986 }
987 
988 static int
989 sdwrite(dev_t dev, struct uio *uio, int ioflag)
990 {
991 
992 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
993 }
994 
995 /*
996  * Perform special action on behalf of the user
997  * Knows about the internals of this device
998  */
999 static int
1000 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1001 {
1002 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
1003 	struct scsipi_periph *periph = sd->sc_periph;
1004 	int part = SDPART(dev);
1005 	int error = 0;
1006 #ifdef __HAVE_OLD_DISKLABEL
1007 	struct disklabel *newlabel = NULL;
1008 #endif
1009 
1010 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
1011 
1012 	/*
1013 	 * If the device is not valid, some IOCTLs can still be
1014 	 * handled on the raw partition. Check this here.
1015 	 */
1016 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1017 		switch (cmd) {
1018 		case DIOCKLABEL:
1019 		case DIOCWLABEL:
1020 		case DIOCLOCK:
1021 		case DIOCEJECT:
1022 		case ODIOCEJECT:
1023 		case DIOCGCACHE:
1024 		case DIOCSCACHE:
1025 		case SCIOCIDENTIFY:
1026 		case OSCIOCIDENTIFY:
1027 		case SCIOCCOMMAND:
1028 		case SCIOCDEBUG:
1029 			if (part == RAW_PART)
1030 				break;
1031 		/* FALLTHROUGH */
1032 		default:
1033 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
1034 				return (ENODEV);
1035 			else
1036 				return (EIO);
1037 		}
1038 	}
1039 
1040 	switch (cmd) {
1041 	case DIOCGDINFO:
1042 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1043 		return (0);
1044 
1045 #ifdef __HAVE_OLD_DISKLABEL
1046 	case ODIOCGDINFO:
1047 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1048 		if (newlabel == NULL)
1049 			return EIO;
1050 		memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1051 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1052 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
1053 		else
1054 			error = ENOTTY;
1055 		free(newlabel, M_TEMP);
1056 		return error;
1057 #endif
1058 
1059 	case DIOCGPART:
1060 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1061 		((struct partinfo *)addr)->part =
1062 		    &sd->sc_dk.dk_label->d_partitions[part];
1063 		return (0);
1064 
1065 	case DIOCWDINFO:
1066 	case DIOCSDINFO:
1067 #ifdef __HAVE_OLD_DISKLABEL
1068 	case ODIOCWDINFO:
1069 	case ODIOCSDINFO:
1070 #endif
1071 	{
1072 		struct disklabel *lp;
1073 
1074 		if ((flag & FWRITE) == 0)
1075 			return (EBADF);
1076 
1077 #ifdef __HAVE_OLD_DISKLABEL
1078  		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1079 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1080 			if (newlabel == NULL)
1081 				return EIO;
1082 			memset(newlabel, 0, sizeof newlabel);
1083 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
1084 			lp = newlabel;
1085 		} else
1086 #endif
1087 		lp = (struct disklabel *)addr;
1088 
1089 		mutex_enter(&sd->sc_dk.dk_openlock);
1090 		sd->flags |= SDF_LABELLING;
1091 
1092 		error = setdisklabel(sd->sc_dk.dk_label,
1093 		    lp, /*sd->sc_dk.dk_openmask : */0,
1094 		    sd->sc_dk.dk_cpulabel);
1095 		if (error == 0) {
1096 			if (cmd == DIOCWDINFO
1097 #ifdef __HAVE_OLD_DISKLABEL
1098 			    || cmd == ODIOCWDINFO
1099 #endif
1100 			   )
1101 				error = writedisklabel(SDLABELDEV(dev),
1102 				    sdstrategy, sd->sc_dk.dk_label,
1103 				    sd->sc_dk.dk_cpulabel);
1104 		}
1105 
1106 		sd->flags &= ~SDF_LABELLING;
1107 		mutex_exit(&sd->sc_dk.dk_openlock);
1108 #ifdef __HAVE_OLD_DISKLABEL
1109 		if (newlabel != NULL)
1110 			free(newlabel, M_TEMP);
1111 #endif
1112 		return (error);
1113 	}
1114 
1115 	case DIOCKLABEL:
1116 		if (*(int *)addr)
1117 			periph->periph_flags |= PERIPH_KEEP_LABEL;
1118 		else
1119 			periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1120 		return (0);
1121 
1122 	case DIOCWLABEL:
1123 		if ((flag & FWRITE) == 0)
1124 			return (EBADF);
1125 		if (*(int *)addr)
1126 			sd->flags |= SDF_WLABEL;
1127 		else
1128 			sd->flags &= ~SDF_WLABEL;
1129 		return (0);
1130 
1131 	case DIOCLOCK:
1132 		if (periph->periph_flags & PERIPH_REMOVABLE)
1133 			return (scsipi_prevent(periph,
1134 			    (*(int *)addr) ?
1135 			    SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
1136 		else
1137 			return (ENOTTY);
1138 
1139 	case DIOCEJECT:
1140 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1141 			return (ENOTTY);
1142 		if (*(int *)addr == 0) {
1143 			/*
1144 			 * Don't force eject: check that we are the only
1145 			 * partition open. If so, unlock it.
1146 			 */
1147 			if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1148 			    sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1149 			    sd->sc_dk.dk_openmask) {
1150 				error = scsipi_prevent(periph, SPAMR_ALLOW,
1151 				    XS_CTL_IGNORE_NOT_READY);
1152 				if (error)
1153 					return (error);
1154 			} else {
1155 				return (EBUSY);
1156 			}
1157 		}
1158 		/* FALLTHROUGH */
1159 	case ODIOCEJECT:
1160 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1161 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1162 
1163 	case DIOCGDEFLABEL:
1164 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
1165 		return (0);
1166 
1167 #ifdef __HAVE_OLD_DISKLABEL
1168 	case ODIOCGDEFLABEL:
1169 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1170 		if (newlabel == NULL)
1171 			return EIO;
1172 		sdgetdefaultlabel(sd, newlabel);
1173 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1174 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
1175 		else
1176 			error = ENOTTY;
1177 		free(newlabel, M_TEMP);
1178 		return error;
1179 #endif
1180 
1181 	case DIOCGCACHE:
1182 		return (sd_getcache(sd, (int *) addr));
1183 
1184 	case DIOCSCACHE:
1185 		if ((flag & FWRITE) == 0)
1186 			return (EBADF);
1187 		return (sd_setcache(sd, *(int *) addr));
1188 
1189 	case DIOCCACHESYNC:
1190 		/*
1191 		 * XXX Do we really need to care about having a writable
1192 		 * file descriptor here?
1193 		 */
1194 		if ((flag & FWRITE) == 0)
1195 			return (EBADF);
1196 		if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1197 			error = sd_flush(sd, 0);
1198 			if (error)
1199 				sd->flags &= ~SDF_FLUSHING;
1200 			else
1201 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1202 		} else
1203 			error = 0;
1204 		return (error);
1205 
1206 	case DIOCAWEDGE:
1207 	    {
1208 	    	struct dkwedge_info *dkw = (void *) addr;
1209 
1210 		if ((flag & FWRITE) == 0)
1211 			return (EBADF);
1212 
1213 		/* If the ioctl happens here, the parent is us. */
1214 		strlcpy(dkw->dkw_parent, device_xname(&sd->sc_dev), sizeof(dkw->dkw_parent));
1215 		return (dkwedge_add(dkw));
1216 	    }
1217 
1218 	case DIOCDWEDGE:
1219 	    {
1220 	    	struct dkwedge_info *dkw = (void *) addr;
1221 
1222 		if ((flag & FWRITE) == 0)
1223 			return (EBADF);
1224 
1225 		/* If the ioctl happens here, the parent is us. */
1226 		strlcpy(dkw->dkw_parent, device_xname(&sd->sc_dev), sizeof(dkw->dkw_parent));
1227 		return (dkwedge_del(dkw));
1228 	    }
1229 
1230 	case DIOCLWEDGES:
1231 	    {
1232 	    	struct dkwedge_list *dkwl = (void *) addr;
1233 
1234 		return (dkwedge_list(&sd->sc_dk, dkwl, l));
1235 	    }
1236 
1237 	default:
1238 		if (part != RAW_PART)
1239 			return (ENOTTY);
1240 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
1241 	}
1242 
1243 #ifdef DIAGNOSTIC
1244 	panic("sdioctl: impossible");
1245 #endif
1246 }
1247 
1248 static void
1249 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1250 {
1251 
1252 	memset(lp, 0, sizeof(struct disklabel));
1253 
1254 	lp->d_secsize = sd->params.blksize;
1255 	lp->d_ntracks = sd->params.heads;
1256 	lp->d_nsectors = sd->params.sectors;
1257 	lp->d_ncylinders = sd->params.cyls;
1258 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1259 
1260 	switch (scsipi_periph_bustype(sd->sc_periph)) {
1261 	case SCSIPI_BUSTYPE_SCSI:
1262 		lp->d_type = DTYPE_SCSI;
1263 		break;
1264 	case SCSIPI_BUSTYPE_ATAPI:
1265 		lp->d_type = DTYPE_ATAPI;
1266 		break;
1267 	}
1268 	/*
1269 	 * XXX
1270 	 * We could probe the mode pages to figure out what kind of disc it is.
1271 	 * Is this worthwhile?
1272 	 */
1273 	strncpy(lp->d_typename, sd->name, 16);
1274 	strncpy(lp->d_packname, "fictitious", 16);
1275 	lp->d_secperunit = sd->params.disksize;
1276 	lp->d_rpm = sd->params.rot_rate;
1277 	lp->d_interleave = 1;
1278 	lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1279 	    D_REMOVABLE : 0;
1280 
1281 	lp->d_partitions[RAW_PART].p_offset = 0;
1282 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1283 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1284 	lp->d_npartitions = RAW_PART + 1;
1285 
1286 	lp->d_magic = DISKMAGIC;
1287 	lp->d_magic2 = DISKMAGIC;
1288 	lp->d_checksum = dkcksum(lp);
1289 }
1290 
1291 
1292 /*
1293  * Load the label information on the named device
1294  */
1295 static int
1296 sdgetdisklabel(struct sd_softc *sd)
1297 {
1298 	struct disklabel *lp = sd->sc_dk.dk_label;
1299 	const char *errstring;
1300 
1301 	memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1302 
1303 	sdgetdefaultlabel(sd, lp);
1304 
1305 	if (lp->d_secpercyl == 0) {
1306 		lp->d_secpercyl = 100;
1307 		/* as long as it's not 0 - readdisklabel divides by it (?) */
1308 	}
1309 
1310 	/*
1311 	 * Call the generic disklabel extraction routine
1312 	 */
1313 	errstring = readdisklabel(MAKESDDEV(0, device_unit(&sd->sc_dev),
1314 	    RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1315 	if (errstring) {
1316 		aprint_error_dev(&sd->sc_dev, "%s\n", errstring);
1317 		return EIO;
1318 	}
1319 	return 0;
1320 }
1321 
1322 static void
1323 sd_shutdown(void *arg)
1324 {
1325 	struct sd_softc *sd = arg;
1326 
1327 	/*
1328 	 * If the disk cache needs to be flushed, and the disk supports
1329 	 * it, flush it.  We're cold at this point, so we poll for
1330 	 * completion.
1331 	 */
1332 	if ((sd->flags & SDF_DIRTY) != 0) {
1333 		if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1334 			aprint_error_dev(&sd->sc_dev, "cache synchronization failed\n");
1335 			sd->flags &= ~SDF_FLUSHING;
1336 		} else
1337 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1338 	}
1339 }
1340 
1341 static bool
1342 sd_suspend(device_t dv PMF_FN_ARGS)
1343 {
1344 	struct sd_softc *sd = device_private(dv);
1345 
1346 	sd_shutdown(sd); /* XXX no need to poll */
1347 	return true;
1348 }
1349 
1350 /*
1351  * Check Errors
1352  */
1353 static int
1354 sd_interpret_sense(struct scsipi_xfer *xs)
1355 {
1356 	struct scsipi_periph *periph = xs->xs_periph;
1357 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
1358 	struct sd_softc *sd = (void *)periph->periph_dev;
1359 	int s, error, retval = EJUSTRETURN;
1360 
1361 	/*
1362 	 * If the periph is already recovering, just do the normal
1363 	 * error processing.
1364 	 */
1365 	if (periph->periph_flags & PERIPH_RECOVERING)
1366 		return (retval);
1367 
1368 	/*
1369 	 * Ignore errors from accessing illegal fields (e.g. trying to
1370 	 * lock the door of a digicam, which doesn't have a door that
1371 	 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
1372 	 */
1373 	if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
1374 	    SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
1375 	    sense->asc == 0x24 &&
1376 	    sense->ascq == 0x00) { /* Illegal field in CDB */
1377 		if (!(xs->xs_control & XS_CTL_SILENT)) {
1378 			scsipi_printaddr(periph);
1379 			printf("no door lock\n");
1380 		}
1381 		xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
1382 		return (retval);
1383 	}
1384 
1385 
1386 
1387 	/*
1388 	 * If the device is not open yet, let the generic code handle it.
1389 	 */
1390 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1391 		return (retval);
1392 
1393 	/*
1394 	 * If it isn't a extended or extended/deferred error, let
1395 	 * the generic code handle it.
1396 	 */
1397 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
1398 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
1399 		return (retval);
1400 
1401 	if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
1402 	    sense->asc == 0x4) {
1403 		if (sense->ascq == 0x01)	{
1404 			/*
1405 			 * Unit In The Process Of Becoming Ready.
1406 			 */
1407 			printf("%s: waiting for pack to spin up...\n",
1408 			    device_xname(&sd->sc_dev));
1409 			if (!callout_pending(&periph->periph_callout))
1410 				scsipi_periph_freeze(periph, 1);
1411 			callout_reset(&periph->periph_callout,
1412 			    5 * hz, scsipi_periph_timed_thaw, periph);
1413 			retval = ERESTART;
1414 		} else if (sense->ascq == 0x02) {
1415 			printf("%s: pack is stopped, restarting...\n",
1416 			    device_xname(&sd->sc_dev));
1417 			s = splbio();
1418 			periph->periph_flags |= PERIPH_RECOVERING;
1419 			splx(s);
1420 			error = scsipi_start(periph, SSS_START,
1421 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1422 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1423 			if (error) {
1424 				aprint_error_dev(&sd->sc_dev, "unable to restart pack\n");
1425 				retval = error;
1426 			} else
1427 				retval = ERESTART;
1428 			s = splbio();
1429 			periph->periph_flags &= ~PERIPH_RECOVERING;
1430 			splx(s);
1431 		}
1432 	}
1433 	if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
1434 	    sense->asc == 0x31 &&
1435 	    sense->ascq == 0x00)	{ /* maybe for any asq ? */
1436 		/* Medium Format Corrupted */
1437 		retval = EFTYPE;
1438 	}
1439 	return (retval);
1440 }
1441 
1442 
1443 static int
1444 sdsize(dev_t dev)
1445 {
1446 	struct sd_softc *sd;
1447 	int part, unit, omask;
1448 	int size;
1449 
1450 	unit = SDUNIT(dev);
1451 	if (unit >= sd_cd.cd_ndevs)
1452 		return (-1);
1453 	sd = sd_cd.cd_devs[unit];
1454 	if (sd == NULL)
1455 		return (-1);
1456 
1457 	if (!device_is_active(&sd->sc_dev))
1458 		return (-1);
1459 
1460 	part = SDPART(dev);
1461 	omask = sd->sc_dk.dk_openmask & (1 << part);
1462 
1463 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1464 		return (-1);
1465 	if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1466 		size = -1;
1467 	else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1468 		size = -1;
1469 	else
1470 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1471 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1472 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1473 		return (-1);
1474 	return (size);
1475 }
1476 
1477 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1478 static struct scsipi_xfer sx;
1479 static int sddoingadump;
1480 
1481 /*
1482  * dump all of physical memory into the partition specified, starting
1483  * at offset 'dumplo' into the partition.
1484  */
1485 static int
1486 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1487 {
1488 	struct sd_softc *sd;	/* disk unit to do the I/O */
1489 	struct disklabel *lp;	/* disk's disklabel */
1490 	int	unit, part;
1491 	int	sectorsize;	/* size of a disk sector */
1492 	int	nsects;		/* number of sectors in partition */
1493 	int	sectoff;	/* sector offset of partition */
1494 	int	totwrt;		/* total number of sectors left to write */
1495 	int	nwrt;		/* current number of sectors to write */
1496 	struct scsipi_rw_10 cmd;	/* write command */
1497 	struct scsipi_xfer *xs;	/* ... convenience */
1498 	struct scsipi_periph *periph;
1499 	struct scsipi_channel *chan;
1500 
1501 	/* Check if recursive dump; if so, punt. */
1502 	if (sddoingadump)
1503 		return (EFAULT);
1504 
1505 	/* Mark as active early. */
1506 	sddoingadump = 1;
1507 
1508 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
1509 	part = SDPART(dev);
1510 
1511 	/* Check for acceptable drive number. */
1512 	if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1513 		return (ENXIO);
1514 
1515 	if (!device_is_active(&sd->sc_dev))
1516 		return (ENODEV);
1517 
1518 	periph = sd->sc_periph;
1519 	chan = periph->periph_channel;
1520 
1521 	/* Make sure it was initialized. */
1522 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1523 		return (ENXIO);
1524 
1525 	/* Convert to disk sectors.  Request must be a multiple of size. */
1526 	lp = sd->sc_dk.dk_label;
1527 	sectorsize = lp->d_secsize;
1528 	if ((size % sectorsize) != 0)
1529 		return (EFAULT);
1530 	totwrt = size / sectorsize;
1531 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
1532 
1533 	nsects = lp->d_partitions[part].p_size;
1534 	sectoff = lp->d_partitions[part].p_offset;
1535 
1536 	/* Check transfer bounds against partition size. */
1537 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
1538 		return (EINVAL);
1539 
1540 	/* Offset block number to start of partition. */
1541 	blkno += sectoff;
1542 
1543 	xs = &sx;
1544 
1545 	while (totwrt > 0) {
1546 		nwrt = totwrt;		/* XXX */
1547 #ifndef	SD_DUMP_NOT_TRUSTED
1548 		/*
1549 		 *  Fill out the scsi command
1550 		 */
1551 		memset(&cmd, 0, sizeof(cmd));
1552 		cmd.opcode = WRITE_10;
1553 		_lto4b(blkno, cmd.addr);
1554 		_lto2b(nwrt, cmd.length);
1555 		/*
1556 		 * Fill out the scsipi_xfer structure
1557 		 *    Note: we cannot sleep as we may be an interrupt
1558 		 * don't use scsipi_command() as it may want to wait
1559 		 * for an xs.
1560 		 */
1561 		memset(xs, 0, sizeof(sx));
1562 		xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1563 		    XS_CTL_DATA_OUT;
1564 		xs->xs_status = 0;
1565 		xs->xs_periph = periph;
1566 		xs->xs_retries = SDRETRIES;
1567 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
1568 		xs->cmd = (struct scsipi_generic *)&cmd;
1569 		xs->cmdlen = sizeof(cmd);
1570 		xs->resid = nwrt * sectorsize;
1571 		xs->error = XS_NOERROR;
1572 		xs->bp = 0;
1573 		xs->data = va;
1574 		xs->datalen = nwrt * sectorsize;
1575 		callout_init(&xs->xs_callout, 0);
1576 
1577 		/*
1578 		 * Pass all this info to the scsi driver.
1579 		 */
1580 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1581 		if ((xs->xs_status & XS_STS_DONE) == 0 ||
1582 		    xs->error != XS_NOERROR)
1583 			return (EIO);
1584 #else	/* SD_DUMP_NOT_TRUSTED */
1585 		/* Let's just talk about this first... */
1586 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1587 		delay(500 * 1000);	/* half a second */
1588 #endif	/* SD_DUMP_NOT_TRUSTED */
1589 
1590 		/* update block count */
1591 		totwrt -= nwrt;
1592 		blkno += nwrt;
1593 		va = (char *)va + sectorsize * nwrt;
1594 	}
1595 	sddoingadump = 0;
1596 	return (0);
1597 }
1598 
1599 static int
1600 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1601     int page, int flags, int *big)
1602 {
1603 
1604 	if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1605 	    !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1606 		*big = 1;
1607 		return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1608 		    size + sizeof(struct scsi_mode_parameter_header_10),
1609 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1610 	} else {
1611 		*big = 0;
1612 		return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1613 		    size + sizeof(struct scsi_mode_parameter_header_6),
1614 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1615 	}
1616 }
1617 
1618 static int
1619 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1620     int flags, int big)
1621 {
1622 
1623 	if (big) {
1624 		struct scsi_mode_parameter_header_10 *header = sense;
1625 
1626 		_lto2b(0, header->data_length);
1627 		return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1628 		    size + sizeof(struct scsi_mode_parameter_header_10),
1629 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1630 	} else {
1631 		struct scsi_mode_parameter_header_6 *header = sense;
1632 
1633 		header->data_length = 0;
1634 		return scsipi_mode_select(sd->sc_periph, byte2, sense,
1635 		    size + sizeof(struct scsi_mode_parameter_header_6),
1636 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1637 	}
1638 }
1639 
1640 /*
1641  * sd_validate_blksize:
1642  *
1643  *	Validate the block size.  Print error if periph is specified,
1644  */
1645 static int
1646 sd_validate_blksize(struct scsipi_periph *periph, int len)
1647 {
1648 
1649 	switch (len) {
1650 	case 256:
1651 	case 512:
1652 	case 1024:
1653 	case 2048:
1654 	case 4096:
1655 		return 1;
1656 	}
1657 
1658 	if (periph) {
1659 		scsipi_printaddr(periph);
1660 		printf("%s sector size: 0x%x.  Defaulting to %d bytes.\n",
1661 		    (len ^ (1 << (ffs(len) - 1))) ?
1662 		    "preposterous" : "unsupported",
1663 		    len, SD_DEFAULT_BLKSIZE);
1664 	}
1665 
1666 	return 0;
1667 }
1668 
1669 /*
1670  * sd_read_capacity:
1671  *
1672  *	Find out from the device what its capacity is.
1673  */
1674 static u_int64_t
1675 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
1676 {
1677 	union {
1678 		struct scsipi_read_capacity_10 cmd;
1679 		struct scsipi_read_capacity_16 cmd16;
1680 	} cmd;
1681 	union {
1682 		struct scsipi_read_capacity_10_data data;
1683 		struct scsipi_read_capacity_16_data data16;
1684 	} *datap;
1685 	uint64_t rv;
1686 
1687 	memset(&cmd, 0, sizeof(cmd));
1688 	cmd.cmd.opcode = READ_CAPACITY_10;
1689 
1690 	/*
1691 	 * Don't allocate data buffer on stack;
1692 	 * The lower driver layer might use the same stack and
1693 	 * if it uses region which is in the same cacheline,
1694 	 * cache flush ops against the data buffer won't work properly.
1695 	 */
1696 	datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
1697 	if (datap == NULL)
1698 		return 0;
1699 
1700 	/*
1701 	 * If the command works, interpret the result as a 4 byte
1702 	 * number of blocks
1703 	 */
1704 	rv = 0;
1705 	memset(datap, 0, sizeof(datap->data));
1706 	if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
1707 	    (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
1708 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1709 		goto out;
1710 
1711 	if (_4btol(datap->data.addr) != 0xffffffff) {
1712 		*blksize = _4btol(datap->data.length);
1713 		rv = _4btol(datap->data.addr) + 1;
1714 		goto out;
1715 	}
1716 
1717 	/*
1718 	 * Device is larger than can be reflected by READ CAPACITY (10).
1719 	 * Try READ CAPACITY (16).
1720 	 */
1721 
1722 	memset(&cmd, 0, sizeof(cmd));
1723 	cmd.cmd16.opcode = READ_CAPACITY_16;
1724 	cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
1725 	_lto4b(sizeof(datap->data16), cmd.cmd16.len);
1726 
1727 	memset(datap, 0, sizeof(datap->data16));
1728 	if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
1729 	    (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
1730 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1731 		goto out;
1732 
1733 	*blksize = _4btol(datap->data16.length);
1734 	rv = _8btol(datap->data16.addr) + 1;
1735 
1736  out:
1737 	free(datap, M_TEMP);
1738 	return rv;
1739 }
1740 
1741 static int
1742 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1743 {
1744 	struct {
1745 		struct scsi_mode_parameter_header_6 header;
1746 		/* no block descriptor */
1747 		u_int8_t pg_code; /* page code (should be 6) */
1748 		u_int8_t pg_length; /* page length (should be 11) */
1749 		u_int8_t wcd; /* bit0: cache disable */
1750 		u_int8_t lbs[2]; /* logical block size */
1751 		u_int8_t size[5]; /* number of log. blocks */
1752 		u_int8_t pp; /* power/performance */
1753 		u_int8_t flags;
1754 		u_int8_t resvd;
1755 	} scsipi_sense;
1756 	u_int64_t blocks;
1757 	int error, blksize;
1758 
1759 	/*
1760 	 * sd_read_capacity (ie "read capacity") and mode sense page 6
1761 	 * give the same information. Do both for now, and check
1762 	 * for consistency.
1763 	 * XXX probably differs for removable media
1764 	 */
1765 	dp->blksize = SD_DEFAULT_BLKSIZE;
1766 	if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
1767 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
1768 
1769 	error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1770 	    &scsipi_sense.header, sizeof(scsipi_sense),
1771 	    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1772 
1773 	if (error != 0)
1774 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
1775 
1776 	dp->blksize = blksize;
1777 	if (!sd_validate_blksize(NULL, dp->blksize))
1778 		dp->blksize = _2btol(scsipi_sense.lbs);
1779 	if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
1780 		dp->blksize = SD_DEFAULT_BLKSIZE;
1781 
1782 	/*
1783 	 * Create a pseudo-geometry.
1784 	 */
1785 	dp->heads = 64;
1786 	dp->sectors = 32;
1787 	dp->cyls = blocks / (dp->heads * dp->sectors);
1788 	dp->disksize = _5btol(scsipi_sense.size);
1789 	if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
1790 		printf("RBC size: mode sense=%llu, get cap=%llu\n",
1791 		       (unsigned long long)dp->disksize,
1792 		       (unsigned long long)blocks);
1793 		dp->disksize = blocks;
1794 	}
1795 	dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1796 
1797 	return (SDGP_RESULT_OK);
1798 }
1799 
1800 /*
1801  * Get the scsi driver to send a full inquiry to the * device and use the
1802  * results to fill out the disk parameter structure.
1803  */
1804 static int
1805 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1806 {
1807 	u_int64_t blocks;
1808 	int error, blksize;
1809 #if 0
1810 	int i;
1811 	u_int8_t *p;
1812 #endif
1813 
1814 	dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
1815 	    flags);
1816 	if (blocks == 0) {
1817 		struct scsipi_read_format_capacities cmd;
1818 		struct {
1819 			struct scsipi_capacity_list_header header;
1820 			struct scsipi_capacity_descriptor desc;
1821 		} __packed data;
1822 
1823 		memset(&cmd, 0, sizeof(cmd));
1824 		memset(&data, 0, sizeof(data));
1825 		cmd.opcode = READ_FORMAT_CAPACITIES;
1826 		_lto2b(sizeof(data), cmd.length);
1827 
1828 		error = scsipi_command(sd->sc_periph,
1829 		    (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1830 		    SDRETRIES, 20000, NULL,
1831 		    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1832 		if (error == EFTYPE) {
1833 			/* Medium Format Corrupted, handle as not formatted */
1834 			return (SDGP_RESULT_UNFORMATTED);
1835 		}
1836 		if (error || data.header.length == 0)
1837 			return (SDGP_RESULT_OFFLINE);
1838 
1839 #if 0
1840 printf("rfc: length=%d\n", data.header.length);
1841 printf("rfc result:"); for (i = sizeof(struct scsipi_capacity_list_header) + data.header.length, p = (void *)&data; i; i--, p++) printf(" %02x", *p); printf("\n");
1842 #endif
1843 		switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1844 		case SCSIPI_CAP_DESC_CODE_RESERVED:
1845 		case SCSIPI_CAP_DESC_CODE_FORMATTED:
1846 			break;
1847 
1848 		case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1849 			return (SDGP_RESULT_UNFORMATTED);
1850 
1851 		case SCSIPI_CAP_DESC_CODE_NONE:
1852 			return (SDGP_RESULT_OFFLINE);
1853 		}
1854 
1855 		dp->disksize = blocks = _4btol(data.desc.nblks);
1856 		if (blocks == 0)
1857 			return (SDGP_RESULT_OFFLINE);		/* XXX? */
1858 
1859 		blksize = _3btol(data.desc.blklen);
1860 
1861 	} else if (!sd_validate_blksize(NULL, blksize)) {
1862 		struct sd_mode_sense_data scsipi_sense;
1863 		int big, bsize;
1864 		struct scsi_general_block_descriptor *bdesc;
1865 
1866 		memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1867 		error = sd_mode_sense(sd, 0, &scsipi_sense,
1868 		    sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1869 		if (!error) {
1870 			if (big) {
1871 				bdesc = (void *)(&scsipi_sense.header.big + 1);
1872 				bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1873 			} else {
1874 				bdesc = (void *)(&scsipi_sense.header.small + 1);
1875 				bsize = scsipi_sense.header.small.blk_desc_len;
1876 			}
1877 
1878 #if 0
1879 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1880 printf("page 0 bsize=%d\n", bsize);
1881 printf("page 0 ok\n");
1882 #endif
1883 
1884 			if (bsize >= 8) {
1885 				blksize = _3btol(bdesc->blklen);
1886 			}
1887 		}
1888 	}
1889 
1890 	if (!sd_validate_blksize(sd->sc_periph, blksize))
1891 		blksize = SD_DEFAULT_BLKSIZE;
1892 
1893 	dp->blksize = blksize;
1894 	dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
1895 	return (0);
1896 }
1897 
1898 static int
1899 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1900 {
1901 	struct sd_mode_sense_data scsipi_sense;
1902 	int error;
1903 	int big, byte2;
1904 	size_t poffset;
1905 	union scsi_disk_pages *pages;
1906 
1907 	byte2 = SMS_DBD;
1908 again:
1909 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1910 	error = sd_mode_sense(sd, byte2, &scsipi_sense,
1911 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1912 	    sizeof(scsipi_sense.pages.rigid_geometry), 4,
1913 	    flags | XS_CTL_SILENT, &big);
1914 	if (error) {
1915 		if (byte2 == SMS_DBD) {
1916 			/* No result; try once more with DBD off */
1917 			byte2 = 0;
1918 			goto again;
1919 		}
1920 		return (error);
1921 	}
1922 
1923 	if (big) {
1924 		poffset = sizeof scsipi_sense.header.big;
1925 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1926 	} else {
1927 		poffset = sizeof scsipi_sense.header.small;
1928 		poffset += scsipi_sense.header.small.blk_desc_len;
1929 	}
1930 
1931 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
1932 		return ERESTART;
1933 
1934 	pages = (void *)((u_long)&scsipi_sense + poffset);
1935 #if 0
1936 	{
1937 		size_t i;
1938 		u_int8_t *p;
1939 
1940 		printf("page 4 sense:");
1941 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
1942 		    i--, p++)
1943 			printf(" %02x", *p);
1944 		printf("\n");
1945 		printf("page 4 pg_code=%d sense=%p/%p\n",
1946 		    pages->rigid_geometry.pg_code, &scsipi_sense, pages);
1947 	}
1948 #endif
1949 
1950 	if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
1951 		return (ERESTART);
1952 
1953 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1954 	    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1955 	    _3btol(pages->rigid_geometry.ncyl),
1956 	    pages->rigid_geometry.nheads,
1957 	    _2btol(pages->rigid_geometry.st_cyl_wp),
1958 	    _2btol(pages->rigid_geometry.st_cyl_rwc),
1959 	    _2btol(pages->rigid_geometry.land_zone)));
1960 
1961 	/*
1962 	 * KLUDGE!! (for zone recorded disks)
1963 	 * give a number of sectors so that sec * trks * cyls
1964 	 * is <= disk_size
1965 	 * can lead to wasted space! THINK ABOUT THIS !
1966 	 */
1967 	dp->heads = pages->rigid_geometry.nheads;
1968 	dp->cyls = _3btol(pages->rigid_geometry.ncyl);
1969 	if (dp->heads == 0 || dp->cyls == 0)
1970 		return (ERESTART);
1971 	dp->sectors = dp->disksize / (dp->heads * dp->cyls);	/* XXX */
1972 
1973 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1974 	if (dp->rot_rate == 0)
1975 		dp->rot_rate = 3600;
1976 
1977 #if 0
1978 printf("page 4 ok\n");
1979 #endif
1980 	return (0);
1981 }
1982 
1983 static int
1984 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
1985 {
1986 	struct sd_mode_sense_data scsipi_sense;
1987 	int error;
1988 	int big, byte2;
1989 	size_t poffset;
1990 	union scsi_disk_pages *pages;
1991 
1992 	byte2 = SMS_DBD;
1993 again:
1994 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1995 	error = sd_mode_sense(sd, 0, &scsipi_sense,
1996 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1997 	    sizeof(scsipi_sense.pages.flex_geometry), 5,
1998 	    flags | XS_CTL_SILENT, &big);
1999 	if (error) {
2000 		if (byte2 == SMS_DBD) {
2001 			/* No result; try once more with DBD off */
2002 			byte2 = 0;
2003 			goto again;
2004 		}
2005 		return (error);
2006 	}
2007 
2008 	if (big) {
2009 		poffset = sizeof scsipi_sense.header.big;
2010 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
2011 	} else {
2012 		poffset = sizeof scsipi_sense.header.small;
2013 		poffset += scsipi_sense.header.small.blk_desc_len;
2014 	}
2015 
2016 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
2017 		return ERESTART;
2018 
2019 	pages = (void *)((u_long)&scsipi_sense + poffset);
2020 #if 0
2021 	{
2022 		size_t i;
2023 		u_int8_t *p;
2024 
2025 		printf("page 5 sense:");
2026 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
2027 		    i--, p++)
2028 			printf(" %02x", *p);
2029 		printf("\n");
2030 		printf("page 5 pg_code=%d sense=%p/%p\n",
2031 		    pages->flex_geometry.pg_code, &scsipi_sense, pages);
2032 	}
2033 #endif
2034 
2035 	if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
2036 		return (ERESTART);
2037 
2038 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
2039 	    ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
2040 	    _3btol(pages->flex_geometry.ncyl),
2041 	    pages->flex_geometry.nheads,
2042 	    pages->flex_geometry.ph_sec_tr,
2043 	    _2btol(pages->flex_geometry.bytes_s)));
2044 
2045 	dp->heads = pages->flex_geometry.nheads;
2046 	dp->cyls = _2btol(pages->flex_geometry.ncyl);
2047 	dp->sectors = pages->flex_geometry.ph_sec_tr;
2048 	if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
2049 		return (ERESTART);
2050 
2051 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
2052 	if (dp->rot_rate == 0)
2053 		dp->rot_rate = 3600;
2054 
2055 #if 0
2056 printf("page 5 ok\n");
2057 #endif
2058 	return (0);
2059 }
2060 
2061 static int
2062 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
2063 {
2064 	int error;
2065 
2066 	/*
2067 	 * If offline, the SDEV_MEDIA_LOADED flag will be
2068 	 * cleared by the caller if necessary.
2069 	 */
2070 	if (sd->type == T_SIMPLE_DIRECT) {
2071 		error = sd_get_simplifiedparms(sd, dp, flags);
2072 		if (!error)
2073 			disk_blocksize(&sd->sc_dk, dp->blksize);
2074 		return (error);
2075 	}
2076 
2077 	error = sd_get_capacity(sd, dp, flags);
2078 	if (error)
2079 		return (error);
2080 
2081 	disk_blocksize(&sd->sc_dk, dp->blksize);
2082 
2083 	if (sd->type == T_OPTICAL)
2084 		goto page0;
2085 
2086 	if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
2087 		if (!sd_get_parms_page5(sd, dp, flags) ||
2088 		    !sd_get_parms_page4(sd, dp, flags))
2089 			return (SDGP_RESULT_OK);
2090 	} else {
2091 		if (!sd_get_parms_page4(sd, dp, flags) ||
2092 		    !sd_get_parms_page5(sd, dp, flags))
2093 			return (SDGP_RESULT_OK);
2094 	}
2095 
2096 page0:
2097 	printf("%s: fabricating a geometry\n", device_xname(&sd->sc_dev));
2098 	/* Try calling driver's method for figuring out geometry. */
2099 	if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
2100 	    !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
2101 		(sd->sc_periph, dp, dp->disksize)) {
2102 		/*
2103 		 * Use adaptec standard fictitious geometry
2104 		 * this depends on which controller (e.g. 1542C is
2105 		 * different. but we have to put SOMETHING here..)
2106 		 */
2107 		dp->heads = 64;
2108 		dp->sectors = 32;
2109 		dp->cyls = dp->disksize / (64 * 32);
2110 	}
2111 	dp->rot_rate = 3600;
2112 	return (SDGP_RESULT_OK);
2113 }
2114 
2115 static int
2116 sd_flush(struct sd_softc *sd, int flags)
2117 {
2118 	struct scsipi_periph *periph = sd->sc_periph;
2119 	struct scsi_synchronize_cache_10 cmd;
2120 
2121 	/*
2122 	 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
2123 	 * We issue with address 0 length 0, which should be
2124 	 * interpreted by the device as "all remaining blocks
2125 	 * starting at address 0".  We ignore ILLEGAL REQUEST
2126 	 * in the event that the command is not supported by
2127 	 * the device, and poll for completion so that we know
2128 	 * that the cache has actually been flushed.
2129 	 *
2130 	 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
2131 	 * command, as indicated by our quirks flags.
2132 	 *
2133 	 * XXX What about older devices?
2134 	 */
2135 	if (periph->periph_version < 2 ||
2136 	    (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
2137 		return (0);
2138 
2139 	sd->flags |= SDF_FLUSHING;
2140 	memset(&cmd, 0, sizeof(cmd));
2141 	cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
2142 
2143 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
2144 	    SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
2145 }
2146 
2147 static int
2148 sd_getcache(struct sd_softc *sd, int *bitsp)
2149 {
2150 	struct scsipi_periph *periph = sd->sc_periph;
2151 	struct sd_mode_sense_data scsipi_sense;
2152 	int error, bits = 0;
2153 	int big;
2154 	union scsi_disk_pages *pages;
2155 
2156 	if (periph->periph_version < 2)
2157 		return (EOPNOTSUPP);
2158 
2159 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2160 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2161 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2162 	if (error)
2163 		return (error);
2164 
2165 	if (big)
2166 		pages = (void *)(&scsipi_sense.header.big + 1);
2167 	else
2168 		pages = (void *)(&scsipi_sense.header.small + 1);
2169 
2170 	if ((pages->caching_params.flags & CACHING_RCD) == 0)
2171 		bits |= DKCACHE_READ;
2172 	if (pages->caching_params.flags & CACHING_WCE)
2173 		bits |= DKCACHE_WRITE;
2174 	if (pages->caching_params.pg_code & PGCODE_PS)
2175 		bits |= DKCACHE_SAVE;
2176 
2177 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2178 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2179 	    sizeof(scsipi_sense.pages.caching_params),
2180 	    SMS_PCTRL_CHANGEABLE|8, 0, &big);
2181 	if (error == 0) {
2182 		if (big)
2183 			pages = (void *)(&scsipi_sense.header.big + 1);
2184 		else
2185 			pages = (void *)(&scsipi_sense.header.small + 1);
2186 
2187 		if (pages->caching_params.flags & CACHING_RCD)
2188 			bits |= DKCACHE_RCHANGE;
2189 		if (pages->caching_params.flags & CACHING_WCE)
2190 			bits |= DKCACHE_WCHANGE;
2191 	}
2192 
2193 	*bitsp = bits;
2194 
2195 	return (0);
2196 }
2197 
2198 static int
2199 sd_setcache(struct sd_softc *sd, int bits)
2200 {
2201 	struct scsipi_periph *periph = sd->sc_periph;
2202 	struct sd_mode_sense_data scsipi_sense;
2203 	int error;
2204 	uint8_t oflags, byte2 = 0;
2205 	int big;
2206 	union scsi_disk_pages *pages;
2207 
2208 	if (periph->periph_version < 2)
2209 		return (EOPNOTSUPP);
2210 
2211 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2212 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2213 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2214 	if (error)
2215 		return (error);
2216 
2217 	if (big)
2218 		pages = (void *)(&scsipi_sense.header.big + 1);
2219 	else
2220 		pages = (void *)(&scsipi_sense.header.small + 1);
2221 
2222 	oflags = pages->caching_params.flags;
2223 
2224 	if (bits & DKCACHE_READ)
2225 		pages->caching_params.flags &= ~CACHING_RCD;
2226 	else
2227 		pages->caching_params.flags |= CACHING_RCD;
2228 
2229 	if (bits & DKCACHE_WRITE)
2230 		pages->caching_params.flags |= CACHING_WCE;
2231 	else
2232 		pages->caching_params.flags &= ~CACHING_WCE;
2233 
2234 	if (oflags == pages->caching_params.flags)
2235 		return (0);
2236 
2237 	pages->caching_params.pg_code &= PGCODE_MASK;
2238 
2239 	if (bits & DKCACHE_SAVE)
2240 		byte2 |= SMS_SP;
2241 
2242 	return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2243 	    sizeof(struct scsi_mode_page_header) +
2244 	    pages->caching_params.pg_length, 0, big));
2245 }
2246 
2247 static void
2248 sd_set_properties(struct sd_softc *sd)
2249 {
2250 	prop_dictionary_t disk_info, odisk_info, geom;
2251 
2252 	disk_info = prop_dictionary_create();
2253 
2254 	geom = prop_dictionary_create();
2255 
2256 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
2257 	    sd->params.disksize);
2258 
2259 	prop_dictionary_set_uint32(geom, "sector-size",
2260 	    sd->params.blksize);
2261 
2262 	prop_dictionary_set_uint16(geom, "sectors-per-track",
2263 	    sd->params.sectors);
2264 
2265 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
2266 	    sd->params.heads);
2267 
2268 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
2269 	    sd->params.cyls);
2270 
2271 	prop_dictionary_set(disk_info, "geometry", geom);
2272 	prop_object_release(geom);
2273 
2274 	prop_dictionary_set(device_properties(&sd->sc_dev),
2275 	    "disk-info", disk_info);
2276 
2277 	/*
2278 	 * Don't release disk_info here; we keep a reference to it.
2279 	 * disk_detach() will release it when we go away.
2280 	 */
2281 
2282 	odisk_info = sd->sc_dk.dk_info;
2283 	sd->sc_dk.dk_info = disk_info;
2284 	if (odisk_info)
2285 		prop_object_release(odisk_info);
2286 }
2287