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