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