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