Merge from vendor branch PKGSRC:
[netbsd-mini2440.git] / sys / dev / scsipi / sd.c
blobebd34f3e7836c64a42ae47c30bafb6dcf0ba31e6
1 /* $NetBSD: sd.c,v 1.289 2009/08/03 09:40:45 jnemeth Exp $ */
3 /*-
4 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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.
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.
33 * Originally written by Julian Elischer (julian@dialix.oz.au)
34 * for TRW Financial Systems for use under the MACH(2.5) operating system.
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.
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.
46 * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.289 2009/08/03 09:40:45 jnemeth Exp $");
52 #include "opt_scsi.h"
53 #include "rnd.h"
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
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>
86 #include <prop/proplib.h>
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)
93 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
95 #define SD_DEFAULT_BLKSIZE 512
97 static void sdminphys(struct buf *);
98 static void sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
99 static int sdgetdisklabel(struct sd_softc *);
100 static void sdstart(struct scsipi_periph *);
101 static void sdrestart(void *);
102 static void sddone(struct scsipi_xfer *, int);
103 static bool sd_suspend(device_t PMF_FN_PROTO);
104 static bool sd_shutdown(device_t, int);
105 static int sd_interpret_sense(struct scsipi_xfer *);
106 static int sdlastclose(device_t);
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);
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);
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 *);
132 CFATTACH_DECL3_NEW(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
133 NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
135 extern struct cfdriver sd_cd;
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 "", "", ""},
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);
161 const struct bdevsw sd_bdevsw = {
162 sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
165 const struct cdevsw sd_cdevsw = {
166 sdopen, sdclose, sdread, sdwrite, sdioctl,
167 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
170 static struct dkdriver sddkdriver = { sdstrategy, sdminphys };
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 */
179 struct sd_mode_sense_data {
181 * XXX
182 * We are not going to parse this as-is -- it just has to be large
183 * enough.
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;
194 * The routine called by the low level scsi routine when it discovers
195 * A device suitable for this driver
197 static int
198 sdmatch(device_t parent, cfdata_t match,
199 void *aux)
201 struct scsipibus_attach_args *sa = aux;
202 int priority;
204 (void)scsipi_inqmatch(&sa->sa_inqbuf,
205 sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
206 sizeof(sd_patterns[0]), &priority);
208 return (priority);
212 * Attach routine common to atapi & scsi.
214 static void
215 sdattach(device_t parent, device_t self, void *aux)
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];
224 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
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;
232 if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
233 periph->periph_version == 0)
234 sd->flags |= SDF_ANCIENT;
236 bufq_alloc(&sd->buf_queue, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
238 callout_init(&sd->sc_callout, 0);
241 * Store information needed to contact our base driver
243 sd->sc_periph = periph;
245 periph->periph_dev = sd->sc_dev;
246 periph->periph_switch = &sd_switch;
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.
253 periph->periph_openings =
254 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
255 periph->periph_flags |= PERIPH_GROW_OPENINGS;
258 * Initialize and attach the disk structure.
260 disk_init(&sd->sc_dk, device_xname(sd->sc_dev), &sddkdriver);
261 disk_attach(&sd->sc_dk);
264 * Use the subdriver to request information regarding the drive.
266 aprint_naive("\n");
267 aprint_normal("\n");
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);
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;
288 case SDGP_RESULT_OFFLINE:
289 aprint_normal("drive offline");
290 break;
292 case SDGP_RESULT_UNFORMATTED:
293 aprint_normal("unformatted media");
294 break;
296 #ifdef DIAGNOSTIC
297 default:
298 panic("sdattach: unknown result from get_parms");
299 break;
300 #endif
302 aprint_normal("\n");
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').
312 if (!pmf_device_register1(self, sd_suspend, NULL, sd_shutdown))
313 aprint_error_dev(self, "couldn't establish power handler\n");
315 #if NRND > 0
317 * attach the device into the random source list
319 rnd_attach_source(&sd->rnd_source, device_xname(sd->sc_dev),
320 RND_TYPE_DISK, 0);
321 #endif
323 /* Discover wedges on this disk. */
324 dkwedge_discover(&sd->sc_dk);
327 static int
328 sddetach(device_t self, int flags)
330 struct sd_softc *sd = device_private(self);
331 int s, bmaj, cmaj, i, mn, rc;
333 if ((rc = disk_begindetach(&sd->sc_dk, sdlastclose, self, flags)) != 0)
334 return rc;
336 /* locate the major number */
337 bmaj = bdevsw_lookup_major(&sd_bdevsw);
338 cmaj = cdevsw_lookup_major(&sd_cdevsw);
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);
347 /* kill any pending restart */
348 callout_stop(&sd->sc_callout);
350 /* Delete all of our wedges. */
351 dkwedge_delall(&sd->sc_dk);
353 s = splbio();
355 /* Kill off any queued buffers. */
356 bufq_drain(sd->buf_queue);
358 bufq_free(sd->buf_queue);
360 /* Kill off any pending commands. */
361 scsipi_kill_pending(sd->sc_periph);
363 splx(s);
365 /* Detach from the disk list. */
366 disk_detach(&sd->sc_dk);
367 disk_destroy(&sd->sc_dk);
369 callout_destroy(&sd->sc_callout);
371 pmf_device_deregister(self);
373 #if NRND > 0
374 /* Unhook the entropy source. */
375 rnd_detach_source(&sd->rnd_source);
376 #endif
378 return (0);
382 * open the device. Make sure the partition info is a up-to-date as can be.
384 static int
385 sdopen(dev_t dev, int flag, int fmt, struct lwp *l)
387 struct sd_softc *sd;
388 struct scsipi_periph *periph;
389 struct scsipi_adapter *adapt;
390 int unit, part;
391 int error;
393 unit = SDUNIT(dev);
394 sd = device_lookup_private(&sd_cd, unit);
395 if (sd == NULL)
396 return (ENXIO);
398 if (!device_is_active(sd->sc_dev))
399 return (ENODEV);
401 part = SDPART(dev);
403 mutex_enter(&sd->sc_dk.dk_openlock);
406 * If there are wedges, and this is not RAW_PART, then we
407 * need to fail.
409 if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
410 error = EBUSY;
411 goto bad1;
414 periph = sd->sc_periph;
415 adapt = periph->periph_channel->chan_adapter;
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));
422 * If this is the first open of this device, add a reference
423 * to the adapter.
425 if (sd->sc_dk.dk_openmask == 0 &&
426 (error = scsipi_adapter_addref(adapt)) != 0)
427 goto bad1;
429 if ((periph->periph_flags & PERIPH_OPEN) != 0) {
431 * If any partition is open, but the disk has been invalidated,
432 * disallow further opens of non-raw partition
434 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
435 (part != RAW_PART || fmt != S_IFCHR)) {
436 error = EIO;
437 goto bad2;
439 } else {
440 int silent;
442 if (part == RAW_PART && fmt == S_IFCHR)
443 silent = XS_CTL_SILENT;
444 else
445 silent = 0;
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);
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.
457 if (error == EIO) {
458 int error2;
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;
473 if (error) {
474 if (silent)
475 goto out;
476 goto bad2;
479 periph->periph_flags |= PERIPH_OPEN;
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;
491 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
492 int param_error;
493 periph->periph_flags |= PERIPH_MEDIA_LOADED;
496 * Load the physical device parameters.
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.
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;
509 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
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;
517 SC_DEBUG(periph, SCSIPI_DB3,
518 ("Disklabel loaded "));
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;
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;
540 sd->sc_dk.dk_openmask =
541 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
543 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
544 mutex_exit(&sd->sc_dk.dk_openlock);
545 return (0);
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;
557 bad2:
558 if (sd->sc_dk.dk_openmask == 0)
559 scsipi_adapter_delref(adapt);
561 bad1:
562 mutex_exit(&sd->sc_dk.dk_openlock);
563 return (error);
567 * Caller must hold sd->sc_dk.dk_openlock.
569 static int
570 sdlastclose(device_t self)
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;
577 * If the disk cache needs flushing, and the disk supports
578 * it, do it now.
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);
589 scsipi_wait_drain(periph);
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;
598 scsipi_wait_drain(periph);
600 scsipi_adapter_delref(adapt);
602 return 0;
606 * close the device.. only called if we are the LAST occurence of an open
607 * device. Convenient now but usually a pain.
609 static int
610 sdclose(dev_t dev, int flag, int fmt, struct lwp *l)
612 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev));
613 int part = SDPART(dev);
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;
624 sd->sc_dk.dk_openmask =
625 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
627 if (sd->sc_dk.dk_openmask == 0)
628 sdlastclose(sd->sc_dev);
630 mutex_exit(&sd->sc_dk.dk_openlock);
631 return (0);
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.
639 static void
640 sdstrategy(struct buf *bp)
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;
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));
653 * If the device has been made invalid, error out
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;
664 lp = sd->sc_dk.dk_label;
667 * The transfer must be a whole number of blocks, offset must not be
668 * negative.
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;
675 if (!sector_aligned || bp->b_blkno < 0) {
676 bp->b_error = EINVAL;
677 goto done;
680 * If it's a null transfer, return immediatly
682 if (bp->b_bcount == 0)
683 goto done;
686 * Do bounds checking, adjust transfer. if error, process.
687 * If end of partition, just return.
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;
700 * Now convert the block number to absolute and put it in
701 * terms of the device's logical block size.
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);
710 if (SDPART(bp->b_dev) != RAW_PART)
711 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
713 bp->b_rawblkno = blkno;
715 s = splbio();
718 * Place it in the queue of disk activities for this disk.
720 * XXX Only do disksort() if the current operating mode does not
721 * XXX include tagged queueing.
723 bufq_put(sd->buf_queue, bp);
726 * Tell the device to get going on the transfer if it's
727 * not doing anything, otherwise just wait for completion
729 sdstart(sd->sc_periph);
731 splx(s);
732 return;
734 done:
736 * Correctly set the buf to indicate a completed xfer
738 bp->b_resid = bp->b_bcount;
739 biodone(bp);
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)
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.
755 * must be called at the correct (highish) spl level
756 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
758 static void
759 sdstart(struct scsipi_periph *periph)
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;
771 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
773 * Check if the device has room for another command
775 while (periph->periph_active < periph->periph_openings) {
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).
781 if (periph->periph_flags & PERIPH_WAITING) {
782 periph->periph_flags &= ~PERIPH_WAITING;
783 wakeup((void *)periph);
784 return;
788 * If the device has become invalid, abort all the
789 * reads and writes until all files have been closed and
790 * re-opened
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;
805 * See if there is a buf with work for us to do..
807 if ((bp = bufq_peek(sd->buf_queue)) == NULL)
808 return;
811 * We have a buf, now we should make a command.
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);
820 * Fill out the scsi command. Use the smallest CDB possible
821 * (6-byte, 10-byte, or 16-byte).
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;
854 /* Instrumentation. */
855 disk_busy(&sd->sc_dk);
858 * Mark the disk dirty so that the cache will be
859 * flushed on close.
861 if ((bp->b_flags & B_READ) == 0)
862 sd->flags |= SDF_DIRTY;
865 * Figure out what flags to use.
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;
874 * Call the routine that chats with the adapter.
875 * Note: we cannot sleep as we may be an interrupt
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)) {
882 * out of memory. Keep this buffer in the queue, and
883 * retry later.
885 callout_reset(&sd->sc_callout, hz / 2, sdrestart,
886 periph);
887 return;
890 * need to dequeue the buffer before queuing the command,
891 * because cdstart may be called recursively from the
892 * HBA driver
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);
906 static void
907 sdrestart(void *v)
909 int s = splbio();
910 sdstart((struct scsipi_periph *)v);
911 splx(s);
914 static void
915 sddone(struct scsipi_xfer *xs, int error)
917 struct sd_softc *sd = device_private(xs->xs_periph->periph_dev);
918 struct buf *bp = xs->bp;
920 if (sd->flags & SDF_FLUSHING) {
921 /* Flush completed, no longer dirty. */
922 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
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;
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
939 biodone(bp);
943 static void
944 sdminphys(struct buf *bp)
946 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev));
947 long xmax;
950 * If the device is ancient, we want to make sure that
951 * the transfer fits into a 6-byte cdb.
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.
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;
965 if (bp->b_bcount > xmax)
966 bp->b_bcount = xmax;
969 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
972 static int
973 sdread(dev_t dev, struct uio *uio, int ioflag)
976 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
979 static int
980 sdwrite(dev_t dev, struct uio *uio, int ioflag)
983 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
987 * Perform special action on behalf of the user
988 * Knows about the internals of this device
990 static int
991 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
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 #ifdef __HAVE_OLD_DISKLABEL
998 struct disklabel *newlabel = NULL;
999 #endif
1001 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
1004 * If the device is not valid, some IOCTLs can still be
1005 * handled on the raw partition. Check this here.
1007 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1008 switch (cmd) {
1009 case DIOCKLABEL:
1010 case DIOCWLABEL:
1011 case DIOCLOCK:
1012 case DIOCEJECT:
1013 case ODIOCEJECT:
1014 case DIOCGCACHE:
1015 case DIOCSCACHE:
1016 case SCIOCIDENTIFY:
1017 case OSCIOCIDENTIFY:
1018 case SCIOCCOMMAND:
1019 case SCIOCDEBUG:
1020 if (part == RAW_PART)
1021 break;
1022 /* FALLTHROUGH */
1023 default:
1024 if ((periph->periph_flags & PERIPH_OPEN) == 0)
1025 return (ENODEV);
1026 else
1027 return (EIO);
1031 error = disk_ioctl(&sd->sc_dk, cmd, addr, flag, l);
1032 if (error != EPASSTHROUGH)
1033 return (error);
1035 switch (cmd) {
1036 case DIOCGDINFO:
1037 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1038 return (0);
1040 #ifdef __HAVE_OLD_DISKLABEL
1041 case ODIOCGDINFO:
1042 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1043 if (newlabel == NULL)
1044 return EIO;
1045 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1046 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1047 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1048 else
1049 error = ENOTTY;
1050 free(newlabel, M_TEMP);
1051 return error;
1052 #endif
1054 case DIOCGPART:
1055 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1056 ((struct partinfo *)addr)->part =
1057 &sd->sc_dk.dk_label->d_partitions[part];
1058 return (0);
1060 case DIOCWDINFO:
1061 case DIOCSDINFO:
1062 #ifdef __HAVE_OLD_DISKLABEL
1063 case ODIOCWDINFO:
1064 case ODIOCSDINFO:
1065 #endif
1067 struct disklabel *lp;
1069 if ((flag & FWRITE) == 0)
1070 return (EBADF);
1072 #ifdef __HAVE_OLD_DISKLABEL
1073 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1074 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1075 if (newlabel == NULL)
1076 return EIO;
1077 memset(newlabel, 0, sizeof newlabel);
1078 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1079 lp = newlabel;
1080 } else
1081 #endif
1082 lp = (struct disklabel *)addr;
1084 mutex_enter(&sd->sc_dk.dk_openlock);
1085 sd->flags |= SDF_LABELLING;
1087 error = setdisklabel(sd->sc_dk.dk_label,
1088 lp, /*sd->sc_dk.dk_openmask : */0,
1089 sd->sc_dk.dk_cpulabel);
1090 if (error == 0) {
1091 if (cmd == DIOCWDINFO
1092 #ifdef __HAVE_OLD_DISKLABEL
1093 || cmd == ODIOCWDINFO
1094 #endif
1096 error = writedisklabel(SDLABELDEV(dev),
1097 sdstrategy, sd->sc_dk.dk_label,
1098 sd->sc_dk.dk_cpulabel);
1101 sd->flags &= ~SDF_LABELLING;
1102 mutex_exit(&sd->sc_dk.dk_openlock);
1103 #ifdef __HAVE_OLD_DISKLABEL
1104 if (newlabel != NULL)
1105 free(newlabel, M_TEMP);
1106 #endif
1107 return (error);
1110 case DIOCKLABEL:
1111 if (*(int *)addr)
1112 periph->periph_flags |= PERIPH_KEEP_LABEL;
1113 else
1114 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1115 return (0);
1117 case DIOCWLABEL:
1118 if ((flag & FWRITE) == 0)
1119 return (EBADF);
1120 if (*(int *)addr)
1121 sd->flags |= SDF_WLABEL;
1122 else
1123 sd->flags &= ~SDF_WLABEL;
1124 return (0);
1126 case DIOCLOCK:
1127 if (periph->periph_flags & PERIPH_REMOVABLE)
1128 return (scsipi_prevent(periph,
1129 (*(int *)addr) ?
1130 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
1131 else
1132 return (ENOTTY);
1134 case DIOCEJECT:
1135 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1136 return (ENOTTY);
1137 if (*(int *)addr == 0) {
1139 * Don't force eject: check that we are the only
1140 * partition open. If so, unlock it.
1142 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1143 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1144 sd->sc_dk.dk_openmask) {
1145 error = scsipi_prevent(periph, SPAMR_ALLOW,
1146 XS_CTL_IGNORE_NOT_READY);
1147 if (error)
1148 return (error);
1149 } else {
1150 return (EBUSY);
1153 /* FALLTHROUGH */
1154 case ODIOCEJECT:
1155 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1156 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1158 case DIOCGDEFLABEL:
1159 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1160 return (0);
1162 #ifdef __HAVE_OLD_DISKLABEL
1163 case ODIOCGDEFLABEL:
1164 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1165 if (newlabel == NULL)
1166 return EIO;
1167 sdgetdefaultlabel(sd, newlabel);
1168 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1169 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1170 else
1171 error = ENOTTY;
1172 free(newlabel, M_TEMP);
1173 return error;
1174 #endif
1176 case DIOCGCACHE:
1177 return (sd_getcache(sd, (int *) addr));
1179 case DIOCSCACHE:
1180 if ((flag & FWRITE) == 0)
1181 return (EBADF);
1182 return (sd_setcache(sd, *(int *) addr));
1184 case DIOCCACHESYNC:
1186 * XXX Do we really need to care about having a writable
1187 * file descriptor here?
1189 if ((flag & FWRITE) == 0)
1190 return (EBADF);
1191 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1192 error = sd_flush(sd, 0);
1193 if (error)
1194 sd->flags &= ~SDF_FLUSHING;
1195 else
1196 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1197 } else
1198 error = 0;
1199 return (error);
1201 case DIOCAWEDGE:
1203 struct dkwedge_info *dkw = (void *) addr;
1205 if ((flag & FWRITE) == 0)
1206 return (EBADF);
1208 /* If the ioctl happens here, the parent is us. */
1209 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev),
1210 sizeof(dkw->dkw_parent));
1211 return (dkwedge_add(dkw));
1214 case DIOCDWEDGE:
1216 struct dkwedge_info *dkw = (void *) addr;
1218 if ((flag & FWRITE) == 0)
1219 return (EBADF);
1221 /* If the ioctl happens here, the parent is us. */
1222 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev),
1223 sizeof(dkw->dkw_parent));
1224 return (dkwedge_del(dkw));
1227 case DIOCLWEDGES:
1229 struct dkwedge_list *dkwl = (void *) addr;
1231 return (dkwedge_list(&sd->sc_dk, dkwl, l));
1234 default:
1235 if (part != RAW_PART)
1236 return (ENOTTY);
1237 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
1240 #ifdef DIAGNOSTIC
1241 panic("sdioctl: impossible");
1242 #endif
1245 static void
1246 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1249 memset(lp, 0, sizeof(struct disklabel));
1251 lp->d_secsize = sd->params.blksize;
1252 lp->d_ntracks = sd->params.heads;
1253 lp->d_nsectors = sd->params.sectors;
1254 lp->d_ncylinders = sd->params.cyls;
1255 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1257 switch (scsipi_periph_bustype(sd->sc_periph)) {
1258 case SCSIPI_BUSTYPE_SCSI:
1259 lp->d_type = DTYPE_SCSI;
1260 break;
1261 case SCSIPI_BUSTYPE_ATAPI:
1262 lp->d_type = DTYPE_ATAPI;
1263 break;
1266 * XXX
1267 * We could probe the mode pages to figure out what kind of disc it is.
1268 * Is this worthwhile?
1270 strncpy(lp->d_typename, sd->name, 16);
1271 strncpy(lp->d_packname, "fictitious", 16);
1272 lp->d_secperunit = sd->params.disksize;
1273 lp->d_rpm = sd->params.rot_rate;
1274 lp->d_interleave = 1;
1275 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1276 D_REMOVABLE : 0;
1278 lp->d_partitions[RAW_PART].p_offset = 0;
1279 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1280 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1281 lp->d_npartitions = RAW_PART + 1;
1283 lp->d_magic = DISKMAGIC;
1284 lp->d_magic2 = DISKMAGIC;
1285 lp->d_checksum = dkcksum(lp);
1290 * Load the label information on the named device
1292 static int
1293 sdgetdisklabel(struct sd_softc *sd)
1295 struct disklabel *lp = sd->sc_dk.dk_label;
1296 const char *errstring;
1298 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1300 sdgetdefaultlabel(sd, lp);
1302 if (lp->d_secpercyl == 0) {
1303 lp->d_secpercyl = 100;
1304 /* as long as it's not 0 - readdisklabel divides by it (?) */
1308 * Call the generic disklabel extraction routine
1310 errstring = readdisklabel(MAKESDDEV(0, device_unit(sd->sc_dev),
1311 RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1312 if (errstring) {
1313 aprint_error_dev(sd->sc_dev, "%s\n", errstring);
1314 return EIO;
1316 return 0;
1319 static bool
1320 sd_shutdown(device_t self, int how)
1322 struct sd_softc *sd = device_private(self);
1325 * If the disk cache needs to be flushed, and the disk supports
1326 * it, flush it. We're cold at this point, so we poll for
1327 * completion.
1329 if ((sd->flags & SDF_DIRTY) != 0) {
1330 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1331 aprint_error_dev(sd->sc_dev,
1332 "cache synchronization failed\n");
1333 sd->flags &= ~SDF_FLUSHING;
1334 } else
1335 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1337 return true;
1340 static bool
1341 sd_suspend(device_t dv PMF_FN_ARGS)
1343 return sd_shutdown(dv, boothowto); /* XXX no need to poll */
1347 * Check Errors
1349 static int
1350 sd_interpret_sense(struct scsipi_xfer *xs)
1352 struct scsipi_periph *periph = xs->xs_periph;
1353 struct scsi_sense_data *sense = &xs->sense.scsi_sense;
1354 struct sd_softc *sd = device_private(periph->periph_dev);
1355 int s, error, retval = EJUSTRETURN;
1358 * If the periph is already recovering, just do the normal
1359 * error processing.
1361 if (periph->periph_flags & PERIPH_RECOVERING)
1362 return (retval);
1365 * Ignore errors from accessing illegal fields (e.g. trying to
1366 * lock the door of a digicam, which doesn't have a door that
1367 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
1369 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
1370 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
1371 sense->asc == 0x24 &&
1372 sense->ascq == 0x00) { /* Illegal field in CDB */
1373 if (!(xs->xs_control & XS_CTL_SILENT)) {
1374 scsipi_printaddr(periph);
1375 printf("no door lock\n");
1377 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
1378 return (retval);
1384 * If the device is not open yet, let the generic code handle it.
1386 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1387 return (retval);
1390 * If it isn't a extended or extended/deferred error, let
1391 * the generic code handle it.
1393 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
1394 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
1395 return (retval);
1397 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
1398 sense->asc == 0x4) {
1399 if (sense->ascq == 0x01) {
1401 * Unit In The Process Of Becoming Ready.
1403 printf("%s: waiting for pack to spin up...\n",
1404 device_xname(sd->sc_dev));
1405 if (!callout_pending(&periph->periph_callout))
1406 scsipi_periph_freeze(periph, 1);
1407 callout_reset(&periph->periph_callout,
1408 5 * hz, scsipi_periph_timed_thaw, periph);
1409 retval = ERESTART;
1410 } else if (sense->ascq == 0x02) {
1411 printf("%s: pack is stopped, restarting...\n",
1412 device_xname(sd->sc_dev));
1413 s = splbio();
1414 periph->periph_flags |= PERIPH_RECOVERING;
1415 splx(s);
1416 error = scsipi_start(periph, SSS_START,
1417 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1418 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1419 if (error) {
1420 aprint_error_dev(sd->sc_dev,
1421 "unable to restart pack\n");
1422 retval = error;
1423 } else
1424 retval = ERESTART;
1425 s = splbio();
1426 periph->periph_flags &= ~PERIPH_RECOVERING;
1427 splx(s);
1430 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
1431 sense->asc == 0x31 &&
1432 sense->ascq == 0x00) { /* maybe for any asq ? */
1433 /* Medium Format Corrupted */
1434 retval = EFTYPE;
1436 return (retval);
1440 static int
1441 sdsize(dev_t dev)
1443 struct sd_softc *sd;
1444 int part, unit, omask;
1445 int size;
1447 unit = SDUNIT(dev);
1448 sd = device_lookup_private(&sd_cd, unit);
1449 if (sd == NULL)
1450 return (-1);
1452 if (!device_is_active(sd->sc_dev))
1453 return (-1);
1455 part = SDPART(dev);
1456 omask = sd->sc_dk.dk_openmask & (1 << part);
1458 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1459 return (-1);
1460 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1461 size = -1;
1462 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1463 size = -1;
1464 else
1465 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1466 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1467 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1468 return (-1);
1469 return (size);
1472 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1473 static struct scsipi_xfer sx;
1474 static int sddoingadump;
1477 * dump all of physical memory into the partition specified, starting
1478 * at offset 'dumplo' into the partition.
1480 static int
1481 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1483 struct sd_softc *sd; /* disk unit to do the I/O */
1484 struct disklabel *lp; /* disk's disklabel */
1485 int unit, part;
1486 int sectorsize; /* size of a disk sector */
1487 int nsects; /* number of sectors in partition */
1488 int sectoff; /* sector offset of partition */
1489 int totwrt; /* total number of sectors left to write */
1490 int nwrt; /* current number of sectors to write */
1491 struct scsipi_rw_10 cmd; /* write command */
1492 struct scsipi_xfer *xs; /* ... convenience */
1493 struct scsipi_periph *periph;
1494 struct scsipi_channel *chan;
1496 /* Check if recursive dump; if so, punt. */
1497 if (sddoingadump)
1498 return (EFAULT);
1500 /* Mark as active early. */
1501 sddoingadump = 1;
1503 unit = SDUNIT(dev); /* Decompose unit & partition. */
1504 part = SDPART(dev);
1506 /* Check for acceptable drive number. */
1507 sd = device_lookup_private(&sd_cd, unit);
1508 if (sd == NULL)
1509 return (ENXIO);
1511 if (!device_is_active(sd->sc_dev))
1512 return (ENODEV);
1514 periph = sd->sc_periph;
1515 chan = periph->periph_channel;
1517 /* Make sure it was initialized. */
1518 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1519 return (ENXIO);
1521 /* Convert to disk sectors. Request must be a multiple of size. */
1522 lp = sd->sc_dk.dk_label;
1523 sectorsize = lp->d_secsize;
1524 if ((size % sectorsize) != 0)
1525 return (EFAULT);
1526 totwrt = size / sectorsize;
1527 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1529 nsects = lp->d_partitions[part].p_size;
1530 sectoff = lp->d_partitions[part].p_offset;
1532 /* Check transfer bounds against partition size. */
1533 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1534 return (EINVAL);
1536 /* Offset block number to start of partition. */
1537 blkno += sectoff;
1539 xs = &sx;
1541 while (totwrt > 0) {
1542 nwrt = totwrt; /* XXX */
1543 #ifndef SD_DUMP_NOT_TRUSTED
1545 * Fill out the scsi command
1547 memset(&cmd, 0, sizeof(cmd));
1548 cmd.opcode = WRITE_10;
1549 _lto4b(blkno, cmd.addr);
1550 _lto2b(nwrt, cmd.length);
1552 * Fill out the scsipi_xfer structure
1553 * Note: we cannot sleep as we may be an interrupt
1554 * don't use scsipi_command() as it may want to wait
1555 * for an xs.
1557 memset(xs, 0, sizeof(sx));
1558 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1559 XS_CTL_DATA_OUT;
1560 xs->xs_status = 0;
1561 xs->xs_periph = periph;
1562 xs->xs_retries = SDRETRIES;
1563 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1564 xs->cmd = (struct scsipi_generic *)&cmd;
1565 xs->cmdlen = sizeof(cmd);
1566 xs->resid = nwrt * sectorsize;
1567 xs->error = XS_NOERROR;
1568 xs->bp = 0;
1569 xs->data = va;
1570 xs->datalen = nwrt * sectorsize;
1571 callout_init(&xs->xs_callout, 0);
1574 * Pass all this info to the scsi driver.
1576 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1577 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1578 xs->error != XS_NOERROR)
1579 return (EIO);
1580 #else /* SD_DUMP_NOT_TRUSTED */
1581 /* Let's just talk about this first... */
1582 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1583 delay(500 * 1000); /* half a second */
1584 #endif /* SD_DUMP_NOT_TRUSTED */
1586 /* update block count */
1587 totwrt -= nwrt;
1588 blkno += nwrt;
1589 va = (char *)va + sectorsize * nwrt;
1591 sddoingadump = 0;
1592 return (0);
1595 static int
1596 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1597 int page, int flags, int *big)
1600 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1601 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1602 *big = 1;
1603 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1604 size + sizeof(struct scsi_mode_parameter_header_10),
1605 flags, SDRETRIES, 6000);
1606 } else {
1607 *big = 0;
1608 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1609 size + sizeof(struct scsi_mode_parameter_header_6),
1610 flags, SDRETRIES, 6000);
1614 static int
1615 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1616 int flags, int big)
1619 if (big) {
1620 struct scsi_mode_parameter_header_10 *header = sense;
1622 _lto2b(0, header->data_length);
1623 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1624 size + sizeof(struct scsi_mode_parameter_header_10),
1625 flags, SDRETRIES, 6000);
1626 } else {
1627 struct scsi_mode_parameter_header_6 *header = sense;
1629 header->data_length = 0;
1630 return scsipi_mode_select(sd->sc_periph, byte2, sense,
1631 size + sizeof(struct scsi_mode_parameter_header_6),
1632 flags, SDRETRIES, 6000);
1637 * sd_validate_blksize:
1639 * Validate the block size. Print error if periph is specified,
1641 static int
1642 sd_validate_blksize(struct scsipi_periph *periph, int len)
1645 switch (len) {
1646 case 256:
1647 case 512:
1648 case 1024:
1649 case 2048:
1650 case 4096:
1651 return 1;
1654 if (periph) {
1655 scsipi_printaddr(periph);
1656 printf("%s sector size: 0x%x. Defaulting to %d bytes.\n",
1657 (len ^ (1 << (ffs(len) - 1))) ?
1658 "preposterous" : "unsupported",
1659 len, SD_DEFAULT_BLKSIZE);
1662 return 0;
1666 * sd_read_capacity:
1668 * Find out from the device what its capacity is.
1670 static u_int64_t
1671 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
1673 union {
1674 struct scsipi_read_capacity_10 cmd;
1675 struct scsipi_read_capacity_16 cmd16;
1676 } cmd;
1677 union {
1678 struct scsipi_read_capacity_10_data data;
1679 struct scsipi_read_capacity_16_data data16;
1680 } *datap;
1681 uint64_t rv;
1683 memset(&cmd, 0, sizeof(cmd));
1684 cmd.cmd.opcode = READ_CAPACITY_10;
1687 * Don't allocate data buffer on stack;
1688 * The lower driver layer might use the same stack and
1689 * if it uses region which is in the same cacheline,
1690 * cache flush ops against the data buffer won't work properly.
1692 datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
1693 if (datap == NULL)
1694 return 0;
1697 * If the command works, interpret the result as a 4 byte
1698 * number of blocks
1700 rv = 0;
1701 memset(datap, 0, sizeof(datap->data));
1702 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
1703 (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
1704 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1705 goto out;
1707 if (_4btol(datap->data.addr) != 0xffffffff) {
1708 *blksize = _4btol(datap->data.length);
1709 rv = _4btol(datap->data.addr) + 1;
1710 goto out;
1714 * Device is larger than can be reflected by READ CAPACITY (10).
1715 * Try READ CAPACITY (16).
1718 memset(&cmd, 0, sizeof(cmd));
1719 cmd.cmd16.opcode = READ_CAPACITY_16;
1720 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
1721 _lto4b(sizeof(datap->data16), cmd.cmd16.len);
1723 memset(datap, 0, sizeof(datap->data16));
1724 if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
1725 (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
1726 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1727 goto out;
1729 *blksize = _4btol(datap->data16.length);
1730 rv = _8btol(datap->data16.addr) + 1;
1732 out:
1733 free(datap, M_TEMP);
1734 return rv;
1737 static int
1738 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1740 struct {
1741 struct scsi_mode_parameter_header_6 header;
1742 /* no block descriptor */
1743 u_int8_t pg_code; /* page code (should be 6) */
1744 u_int8_t pg_length; /* page length (should be 11) */
1745 u_int8_t wcd; /* bit0: cache disable */
1746 u_int8_t lbs[2]; /* logical block size */
1747 u_int8_t size[5]; /* number of log. blocks */
1748 u_int8_t pp; /* power/performance */
1749 u_int8_t flags;
1750 u_int8_t resvd;
1751 } scsipi_sense;
1752 u_int64_t blocks;
1753 int error, blksize;
1756 * sd_read_capacity (ie "read capacity") and mode sense page 6
1757 * give the same information. Do both for now, and check
1758 * for consistency.
1759 * XXX probably differs for removable media
1761 dp->blksize = SD_DEFAULT_BLKSIZE;
1762 if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
1763 return (SDGP_RESULT_OFFLINE); /* XXX? */
1765 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1766 &scsipi_sense.header, sizeof(scsipi_sense),
1767 flags, SDRETRIES, 6000);
1769 if (error != 0)
1770 return (SDGP_RESULT_OFFLINE); /* XXX? */
1772 dp->blksize = blksize;
1773 if (!sd_validate_blksize(NULL, dp->blksize))
1774 dp->blksize = _2btol(scsipi_sense.lbs);
1775 if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
1776 dp->blksize = SD_DEFAULT_BLKSIZE;
1779 * Create a pseudo-geometry.
1781 dp->heads = 64;
1782 dp->sectors = 32;
1783 dp->cyls = blocks / (dp->heads * dp->sectors);
1784 dp->disksize = _5btol(scsipi_sense.size);
1785 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
1786 printf("RBC size: mode sense=%llu, get cap=%llu\n",
1787 (unsigned long long)dp->disksize,
1788 (unsigned long long)blocks);
1789 dp->disksize = blocks;
1791 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1793 return (SDGP_RESULT_OK);
1797 * Get the scsi driver to send a full inquiry to the * device and use the
1798 * results to fill out the disk parameter structure.
1800 static int
1801 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1803 u_int64_t blocks;
1804 int error, blksize;
1805 #if 0
1806 int i;
1807 u_int8_t *p;
1808 #endif
1810 dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
1811 flags);
1812 if (blocks == 0) {
1813 struct scsipi_read_format_capacities cmd;
1814 struct {
1815 struct scsipi_capacity_list_header header;
1816 struct scsipi_capacity_descriptor desc;
1817 } __packed data;
1819 memset(&cmd, 0, sizeof(cmd));
1820 memset(&data, 0, sizeof(data));
1821 cmd.opcode = READ_FORMAT_CAPACITIES;
1822 _lto2b(sizeof(data), cmd.length);
1824 error = scsipi_command(sd->sc_periph,
1825 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1826 SDRETRIES, 20000, NULL,
1827 flags | XS_CTL_DATA_IN);
1828 if (error == EFTYPE) {
1829 /* Medium Format Corrupted, handle as not formatted */
1830 return (SDGP_RESULT_UNFORMATTED);
1832 if (error || data.header.length == 0)
1833 return (SDGP_RESULT_OFFLINE);
1835 #if 0
1836 printf("rfc: length=%d\n", data.header.length);
1837 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");
1838 #endif
1839 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1840 case SCSIPI_CAP_DESC_CODE_RESERVED:
1841 case SCSIPI_CAP_DESC_CODE_FORMATTED:
1842 break;
1844 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1845 return (SDGP_RESULT_UNFORMATTED);
1847 case SCSIPI_CAP_DESC_CODE_NONE:
1848 return (SDGP_RESULT_OFFLINE);
1851 dp->disksize = blocks = _4btol(data.desc.nblks);
1852 if (blocks == 0)
1853 return (SDGP_RESULT_OFFLINE); /* XXX? */
1855 blksize = _3btol(data.desc.blklen);
1857 } else if (!sd_validate_blksize(NULL, blksize)) {
1858 struct sd_mode_sense_data scsipi_sense;
1859 int big, bsize;
1860 struct scsi_general_block_descriptor *bdesc;
1862 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1863 error = sd_mode_sense(sd, 0, &scsipi_sense,
1864 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1865 if (!error) {
1866 if (big) {
1867 bdesc = (void *)(&scsipi_sense.header.big + 1);
1868 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1869 } else {
1870 bdesc = (void *)(&scsipi_sense.header.small + 1);
1871 bsize = scsipi_sense.header.small.blk_desc_len;
1874 #if 0
1875 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1876 printf("page 0 bsize=%d\n", bsize);
1877 printf("page 0 ok\n");
1878 #endif
1880 if (bsize >= 8) {
1881 blksize = _3btol(bdesc->blklen);
1886 if (!sd_validate_blksize(sd->sc_periph, blksize))
1887 blksize = SD_DEFAULT_BLKSIZE;
1889 dp->blksize = blksize;
1890 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
1891 return (0);
1894 static int
1895 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1897 struct sd_mode_sense_data scsipi_sense;
1898 int error;
1899 int big, byte2;
1900 size_t poffset;
1901 union scsi_disk_pages *pages;
1903 byte2 = SMS_DBD;
1904 again:
1905 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1906 error = sd_mode_sense(sd, byte2, &scsipi_sense,
1907 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1908 sizeof(scsipi_sense.pages.rigid_geometry), 4,
1909 flags | XS_CTL_SILENT, &big);
1910 if (error) {
1911 if (byte2 == SMS_DBD) {
1912 /* No result; try once more with DBD off */
1913 byte2 = 0;
1914 goto again;
1916 return (error);
1919 if (big) {
1920 poffset = sizeof scsipi_sense.header.big;
1921 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1922 } else {
1923 poffset = sizeof scsipi_sense.header.small;
1924 poffset += scsipi_sense.header.small.blk_desc_len;
1927 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
1928 return ERESTART;
1930 pages = (void *)((u_long)&scsipi_sense + poffset);
1931 #if 0
1933 size_t i;
1934 u_int8_t *p;
1936 printf("page 4 sense:");
1937 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
1938 i--, p++)
1939 printf(" %02x", *p);
1940 printf("\n");
1941 printf("page 4 pg_code=%d sense=%p/%p\n",
1942 pages->rigid_geometry.pg_code, &scsipi_sense, pages);
1944 #endif
1946 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
1947 return (ERESTART);
1949 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1950 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1951 _3btol(pages->rigid_geometry.ncyl),
1952 pages->rigid_geometry.nheads,
1953 _2btol(pages->rigid_geometry.st_cyl_wp),
1954 _2btol(pages->rigid_geometry.st_cyl_rwc),
1955 _2btol(pages->rigid_geometry.land_zone)));
1958 * KLUDGE!! (for zone recorded disks)
1959 * give a number of sectors so that sec * trks * cyls
1960 * is <= disk_size
1961 * can lead to wasted space! THINK ABOUT THIS !
1963 dp->heads = pages->rigid_geometry.nheads;
1964 dp->cyls = _3btol(pages->rigid_geometry.ncyl);
1965 if (dp->heads == 0 || dp->cyls == 0)
1966 return (ERESTART);
1967 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
1969 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1970 if (dp->rot_rate == 0)
1971 dp->rot_rate = 3600;
1973 #if 0
1974 printf("page 4 ok\n");
1975 #endif
1976 return (0);
1979 static int
1980 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
1982 struct sd_mode_sense_data scsipi_sense;
1983 int error;
1984 int big, byte2;
1985 size_t poffset;
1986 union scsi_disk_pages *pages;
1988 byte2 = SMS_DBD;
1989 again:
1990 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1991 error = sd_mode_sense(sd, 0, &scsipi_sense,
1992 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1993 sizeof(scsipi_sense.pages.flex_geometry), 5,
1994 flags | XS_CTL_SILENT, &big);
1995 if (error) {
1996 if (byte2 == SMS_DBD) {
1997 /* No result; try once more with DBD off */
1998 byte2 = 0;
1999 goto again;
2001 return (error);
2004 if (big) {
2005 poffset = sizeof scsipi_sense.header.big;
2006 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
2007 } else {
2008 poffset = sizeof scsipi_sense.header.small;
2009 poffset += scsipi_sense.header.small.blk_desc_len;
2012 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
2013 return ERESTART;
2015 pages = (void *)((u_long)&scsipi_sense + poffset);
2016 #if 0
2018 size_t i;
2019 u_int8_t *p;
2021 printf("page 5 sense:");
2022 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
2023 i--, p++)
2024 printf(" %02x", *p);
2025 printf("\n");
2026 printf("page 5 pg_code=%d sense=%p/%p\n",
2027 pages->flex_geometry.pg_code, &scsipi_sense, pages);
2029 #endif
2031 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
2032 return (ERESTART);
2034 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
2035 ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
2036 _3btol(pages->flex_geometry.ncyl),
2037 pages->flex_geometry.nheads,
2038 pages->flex_geometry.ph_sec_tr,
2039 _2btol(pages->flex_geometry.bytes_s)));
2041 dp->heads = pages->flex_geometry.nheads;
2042 dp->cyls = _2btol(pages->flex_geometry.ncyl);
2043 dp->sectors = pages->flex_geometry.ph_sec_tr;
2044 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
2045 return (ERESTART);
2047 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
2048 if (dp->rot_rate == 0)
2049 dp->rot_rate = 3600;
2051 #if 0
2052 printf("page 5 ok\n");
2053 #endif
2054 return (0);
2057 static int
2058 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
2060 int error;
2063 * If offline, the SDEV_MEDIA_LOADED flag will be
2064 * cleared by the caller if necessary.
2066 if (sd->type == T_SIMPLE_DIRECT) {
2067 error = sd_get_simplifiedparms(sd, dp, flags);
2068 if (!error)
2069 disk_blocksize(&sd->sc_dk, dp->blksize);
2070 return (error);
2073 error = sd_get_capacity(sd, dp, flags);
2074 if (error)
2075 return (error);
2077 disk_blocksize(&sd->sc_dk, dp->blksize);
2079 if (sd->type == T_OPTICAL)
2080 goto page0;
2082 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
2083 if (!sd_get_parms_page5(sd, dp, flags) ||
2084 !sd_get_parms_page4(sd, dp, flags))
2085 goto setprops;
2086 } else {
2087 if (!sd_get_parms_page4(sd, dp, flags) ||
2088 !sd_get_parms_page5(sd, dp, flags))
2089 goto setprops;
2092 page0:
2093 printf("%s: fabricating a geometry\n", device_xname(sd->sc_dev));
2094 /* Try calling driver's method for figuring out geometry. */
2095 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
2096 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
2097 (sd->sc_periph, dp, dp->disksize)) {
2099 * Use adaptec standard fictitious geometry
2100 * this depends on which controller (e.g. 1542C is
2101 * different. but we have to put SOMETHING here..)
2103 dp->heads = 64;
2104 dp->sectors = 32;
2105 dp->cyls = dp->disksize / (64 * 32);
2107 dp->rot_rate = 3600;
2109 setprops:
2110 sd_set_properties(sd);
2112 return (SDGP_RESULT_OK);
2115 static int
2116 sd_flush(struct sd_softc *sd, int flags)
2118 struct scsipi_periph *periph = sd->sc_periph;
2119 struct scsi_synchronize_cache_10 cmd;
2122 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
2123 * We issue with address 0 length 0, which should be
2124 * interpreted by the device as "all remaining blocks
2125 * starting at address 0". We ignore ILLEGAL REQUEST
2126 * in the event that the command is not supported by
2127 * the device, and poll for completion so that we know
2128 * that the cache has actually been flushed.
2130 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
2131 * command, as indicated by our quirks flags.
2133 * XXX What about older devices?
2135 if (periph->periph_version < 2 ||
2136 (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
2137 return (0);
2139 sd->flags |= SDF_FLUSHING;
2140 memset(&cmd, 0, sizeof(cmd));
2141 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
2143 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
2144 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
2147 static int
2148 sd_getcache(struct sd_softc *sd, int *bitsp)
2150 struct scsipi_periph *periph = sd->sc_periph;
2151 struct sd_mode_sense_data scsipi_sense;
2152 int error, bits = 0;
2153 int big;
2154 union scsi_disk_pages *pages;
2156 if (periph->periph_version < 2)
2157 return (EOPNOTSUPP);
2159 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2160 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2161 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2162 if (error)
2163 return (error);
2165 if (big)
2166 pages = (void *)(&scsipi_sense.header.big + 1);
2167 else
2168 pages = (void *)(&scsipi_sense.header.small + 1);
2170 if ((pages->caching_params.flags & CACHING_RCD) == 0)
2171 bits |= DKCACHE_READ;
2172 if (pages->caching_params.flags & CACHING_WCE)
2173 bits |= DKCACHE_WRITE;
2174 if (pages->caching_params.pg_code & PGCODE_PS)
2175 bits |= DKCACHE_SAVE;
2177 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2178 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2179 sizeof(scsipi_sense.pages.caching_params),
2180 SMS_PCTRL_CHANGEABLE|8, 0, &big);
2181 if (error == 0) {
2182 if (big)
2183 pages = (void *)(&scsipi_sense.header.big + 1);
2184 else
2185 pages = (void *)(&scsipi_sense.header.small + 1);
2187 if (pages->caching_params.flags & CACHING_RCD)
2188 bits |= DKCACHE_RCHANGE;
2189 if (pages->caching_params.flags & CACHING_WCE)
2190 bits |= DKCACHE_WCHANGE;
2193 *bitsp = bits;
2195 return (0);
2198 static int
2199 sd_setcache(struct sd_softc *sd, int bits)
2201 struct scsipi_periph *periph = sd->sc_periph;
2202 struct sd_mode_sense_data scsipi_sense;
2203 int error;
2204 uint8_t oflags, byte2 = 0;
2205 int big;
2206 union scsi_disk_pages *pages;
2208 if (periph->periph_version < 2)
2209 return (EOPNOTSUPP);
2211 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2212 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2213 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2214 if (error)
2215 return (error);
2217 if (big)
2218 pages = (void *)(&scsipi_sense.header.big + 1);
2219 else
2220 pages = (void *)(&scsipi_sense.header.small + 1);
2222 oflags = pages->caching_params.flags;
2224 if (bits & DKCACHE_READ)
2225 pages->caching_params.flags &= ~CACHING_RCD;
2226 else
2227 pages->caching_params.flags |= CACHING_RCD;
2229 if (bits & DKCACHE_WRITE)
2230 pages->caching_params.flags |= CACHING_WCE;
2231 else
2232 pages->caching_params.flags &= ~CACHING_WCE;
2234 if (oflags == pages->caching_params.flags)
2235 return (0);
2237 pages->caching_params.pg_code &= PGCODE_MASK;
2239 if (bits & DKCACHE_SAVE)
2240 byte2 |= SMS_SP;
2242 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2243 sizeof(struct scsi_mode_page_header) +
2244 pages->caching_params.pg_length, 0, big));
2247 static void
2248 sd_set_properties(struct sd_softc *sd)
2250 prop_dictionary_t disk_info, odisk_info, geom;
2252 disk_info = prop_dictionary_create();
2254 geom = prop_dictionary_create();
2256 prop_dictionary_set_uint64(geom, "sectors-per-unit",
2257 sd->params.disksize);
2259 prop_dictionary_set_uint32(geom, "sector-size",
2260 sd->params.blksize);
2262 prop_dictionary_set_uint16(geom, "sectors-per-track",
2263 sd->params.sectors);
2265 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
2266 sd->params.heads);
2268 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
2269 sd->params.cyls);
2271 prop_dictionary_set(disk_info, "geometry", geom);
2272 prop_object_release(geom);
2274 prop_dictionary_set(device_properties(sd->sc_dev),
2275 "disk-info", disk_info);
2278 * Don't release disk_info here; we keep a reference to it.
2279 * disk_detach() will release it when we go away.
2282 odisk_info = sd->sc_dk.dk_info;
2283 sd->sc_dk.dk_info = disk_info;
2284 if (odisk_info)
2285 prop_object_release(odisk_info);