1 /* $NetBSD: sd.c,v 1.289 2009/08/03 09:40:45 jnemeth Exp $ */
4 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
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
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 $");
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
60 #include <sys/ioctl.h>
61 #include <sys/scsiio.h>
65 #include <sys/malloc.h>
66 #include <sys/errno.h>
67 #include <sys/device.h>
68 #include <sys/disklabel.h>
72 #include <sys/vnode.h>
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,
110 static int sd_mode_select(struct sd_softc
*, u_int8_t
, void *, size_t, 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
*,
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
*,
120 static int sd_get_parms_page5(struct sd_softc
*, struct disk_parms
*,
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
[] = {
146 {T_SIMPLE_DIRECT
, T_FIXED
,
148 {T_SIMPLE_DIRECT
, T_REMOV
,
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
{
182 * We are not going to parse this as-is -- it just has to be large
186 struct scsi_mode_parameter_header_6 small
;
187 struct scsi_mode_parameter_header_10 big
;
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
198 sdmatch(device_t parent
, cfdata_t match
,
201 struct scsipibus_attach_args
*sa
= aux
;
204 (void)scsipi_inqmatch(&sa
->sa_inqbuf
,
205 sd_patterns
, sizeof(sd_patterns
) / sizeof(sd_patterns
[0]),
206 sizeof(sd_patterns
[0]), &priority
);
212 * Attach routine common to atapi & scsi.
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
;
221 struct disk_parms
*dp
= &sd
->params
;
224 SC_DEBUG(periph
, SCSIPI_DB2
, ("sdattach: "));
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.
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
);
274 result
= SDGP_RESULT_OFFLINE
;
276 result
= sd_get_parms(sd
, &sd
->params
, XS_CTL_DISCOVERY
);
277 aprint_normal_dev(sd
->sc_dev
, "");
280 format_bytes(pbuf
, sizeof(pbuf
),
281 (u_int64_t
)dp
->disksize
* dp
->blksize
);
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
);
288 case SDGP_RESULT_OFFLINE
:
289 aprint_normal("drive offline");
292 case SDGP_RESULT_UNFORMATTED
:
293 aprint_normal("unformatted media");
298 panic("sdattach: unknown result from get_parms");
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
312 if (!pmf_device_register1(self
, sd_suspend
, NULL
, sd_shutdown
))
313 aprint_error_dev(self
, "couldn't establish power handler\n");
317 * attach the device into the random source list
319 rnd_attach_source(&sd
->rnd_source
, device_xname(sd
->sc_dev
),
323 /* Discover wedges on this disk. */
324 dkwedge_discover(&sd
->sc_dk
);
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)
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
);
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
);
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
);
374 /* Unhook the entropy source. */
375 rnd_detach_source(&sd
->rnd_source
);
382 * open the device. Make sure the partition info is a up-to-date as can be.
385 sdopen(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
388 struct scsipi_periph
*periph
;
389 struct scsipi_adapter
*adapt
;
394 sd
= device_lookup_private(&sd_cd
, unit
);
398 if (!device_is_active(sd
->sc_dev
))
403 mutex_enter(&sd
->sc_dk
.dk_openlock
);
406 * If there are wedges, and this is not RAW_PART, then we
409 if (sd
->sc_dk
.dk_nwedges
!= 0 && part
!= RAW_PART
) {
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
425 if (sd
->sc_dk
.dk_openmask
== 0 &&
426 (error
= scsipi_adapter_addref(adapt
)) != 0)
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
)) {
442 if (part
== RAW_PART
&& fmt
== S_IFCHR
)
443 silent
= XS_CTL_SILENT
;
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
|
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.
460 error2
= scsipi_start(periph
, SSS_START
, silent
);
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
|
491 if ((periph
->periph_flags
& PERIPH_MEDIA_LOADED
) == 0) {
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
503 if ((param_error
= sd_get_parms(sd
, &sd
->params
, 0))
504 == SDGP_RESULT_OFFLINE
) {
506 periph
->periph_flags
&= ~PERIPH_MEDIA_LOADED
;
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
)) {
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
)) {
531 out
: /* Insure only one open at a time. */
534 sd
->sc_dk
.dk_copenmask
|= (1 << part
);
537 sd
->sc_dk
.dk_bopenmask
|= (1 << part
);
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
);
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
|
554 periph
->periph_flags
&= ~PERIPH_OPEN
;
558 if (sd
->sc_dk
.dk_openmask
== 0)
559 scsipi_adapter_delref(adapt
);
562 mutex_exit(&sd
->sc_dk
.dk_openlock
);
567 * Caller must hold sd->sc_dk.dk_openlock.
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
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
;
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
|
596 periph
->periph_flags
&= ~PERIPH_OPEN
;
598 scsipi_wait_drain(periph
);
600 scsipi_adapter_delref(adapt
);
606 * close the device.. only called if we are the LAST occurence of an open
607 * device. Convenient now but usually a pain.
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
);
618 sd
->sc_dk
.dk_copenmask
&= ~(1 << part
);
621 sd
->sc_dk
.dk_bopenmask
&= ~(1 << part
);
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
);
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.
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
;
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
)
660 bp
->b_error
= ENODEV
;
664 lp
= sd
->sc_dk
.dk_label
;
667 * The transfer must be a whole number of blocks, offset must not be
670 if (lp
->d_secsize
== DEV_BSIZE
) {
671 sector_aligned
= (bp
->b_bcount
& (DEV_BSIZE
- 1)) == 0;
673 sector_aligned
= (bp
->b_bcount
% lp
->d_secsize
) == 0;
675 if (!sector_aligned
|| bp
->b_blkno
< 0) {
676 bp
->b_error
= EINVAL
;
680 * If it's a null transfer, return immediatly
682 if (bp
->b_bcount
== 0)
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)
694 if (bounds_check_with_label(&sd
->sc_dk
, bp
,
695 (sd
->flags
& (SDF_WLABEL
|SDF_LABELLING
)) != 0) <= 0)
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
)
705 else if (lp
->d_secsize
> DEV_BSIZE
)
706 blkno
= bp
->b_blkno
/ (lp
->d_secsize
/ DEV_BSIZE
);
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
;
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
);
736 * Correctly set the buf to indicate a completed xfer
738 bp
->b_resid
= bp
->b_bcount
;
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
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
;
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
);
788 * If the device has become invalid, abort all the
789 * reads and writes until all files have been closed and
793 (periph
->periph_flags
& PERIPH_MEDIA_LOADED
) == 0)) {
794 if ((bp
= bufq_get(sd
->buf_queue
)) != NULL
) {
796 bp
->b_resid
= bp
->b_bcount
;
805 * See if there is a buf with work for us to do..
807 if ((bp
= bufq_peek(sd
->buf_queue
)) == NULL
)
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
;
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
)) {
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
) {
836 memset(&cmd_big
, 0, sizeof(cmd_big
));
837 cmd_big
.opcode
= (bp
->b_flags
& B_READ
) ?
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
;
845 memset(&cmd16
, 0, sizeof(cmd16
));
846 cmd16
.opcode
= (bp
->b_flags
& B_READ
) ?
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
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
;
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
885 callout_reset(&sd
->sc_callout
, hz
/ 2, sdrestart
,
890 * need to dequeue the buffer before queuing the command,
891 * because cdstart may be called recursively from the
895 if (bufq_get(sd
->buf_queue
) != bp
)
896 panic("sdstart(): dequeued wrong buf");
898 bufq_get(sd
->buf_queue
);
900 error
= scsipi_execute_xs(xs
);
901 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
910 sdstart((struct scsipi_periph
*)v
);
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
);
927 bp
->b_resid
= xs
->resid
;
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
));
936 rnd_add_uint32(&sd
->rnd_source
, bp
->b_rawblkno
);
944 sdminphys(struct buf
*bp
)
946 struct sd_softc
*sd
= device_lookup_private(&sd_cd
, SDUNIT(bp
->b_dev
));
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
)
969 scsipi_adapter_minphys(sd
->sc_periph
->periph_channel
, bp
);
973 sdread(dev_t dev
, struct uio
*uio
, int ioflag
)
976 return (physio(sdstrategy
, NULL
, dev
, B_READ
, sdminphys
, uio
));
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
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
);
997 #ifdef __HAVE_OLD_DISKLABEL
998 struct disklabel
*newlabel
= NULL
;
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) {
1017 case OSCIOCIDENTIFY
:
1020 if (part
== RAW_PART
)
1024 if ((periph
->periph_flags
& PERIPH_OPEN
) == 0)
1031 error
= disk_ioctl(&sd
->sc_dk
, cmd
, addr
, flag
, l
);
1032 if (error
!= EPASSTHROUGH
)
1037 *(struct disklabel
*)addr
= *(sd
->sc_dk
.dk_label
);
1040 #ifdef __HAVE_OLD_DISKLABEL
1042 newlabel
= malloc(sizeof *newlabel
, M_TEMP
, M_WAITOK
);
1043 if (newlabel
== NULL
)
1045 memcpy(newlabel
, sd
->sc_dk
.dk_label
, sizeof (*newlabel
));
1046 if (newlabel
->d_npartitions
<= OLDMAXPARTITIONS
)
1047 memcpy(addr
, newlabel
, sizeof (struct olddisklabel
));
1050 free(newlabel
, M_TEMP
);
1055 ((struct partinfo
*)addr
)->disklab
= sd
->sc_dk
.dk_label
;
1056 ((struct partinfo
*)addr
)->part
=
1057 &sd
->sc_dk
.dk_label
->d_partitions
[part
];
1062 #ifdef __HAVE_OLD_DISKLABEL
1067 struct disklabel
*lp
;
1069 if ((flag
& FWRITE
) == 0)
1072 #ifdef __HAVE_OLD_DISKLABEL
1073 if (cmd
== ODIOCSDINFO
|| cmd
== ODIOCWDINFO
) {
1074 newlabel
= malloc(sizeof *newlabel
, M_TEMP
, M_WAITOK
);
1075 if (newlabel
== NULL
)
1077 memset(newlabel
, 0, sizeof newlabel
);
1078 memcpy(newlabel
, addr
, sizeof (struct olddisklabel
));
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
);
1091 if (cmd
== DIOCWDINFO
1092 #ifdef __HAVE_OLD_DISKLABEL
1093 || cmd
== ODIOCWDINFO
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
);
1112 periph
->periph_flags
|= PERIPH_KEEP_LABEL
;
1114 periph
->periph_flags
&= ~PERIPH_KEEP_LABEL
;
1118 if ((flag
& FWRITE
) == 0)
1121 sd
->flags
|= SDF_WLABEL
;
1123 sd
->flags
&= ~SDF_WLABEL
;
1127 if (periph
->periph_flags
& PERIPH_REMOVABLE
)
1128 return (scsipi_prevent(periph
,
1130 SPAMR_PREVENT_DT
: SPAMR_ALLOW
, 0));
1135 if ((periph
->periph_flags
& PERIPH_REMOVABLE
) == 0)
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
);
1155 return ((periph
->periph_flags
& PERIPH_REMOVABLE
) == 0 ?
1156 ENOTTY
: scsipi_start(periph
, SSS_STOP
|SSS_LOEJ
, 0));
1159 sdgetdefaultlabel(sd
, (struct disklabel
*)addr
);
1162 #ifdef __HAVE_OLD_DISKLABEL
1163 case ODIOCGDEFLABEL
:
1164 newlabel
= malloc(sizeof *newlabel
, M_TEMP
, M_WAITOK
);
1165 if (newlabel
== NULL
)
1167 sdgetdefaultlabel(sd
, newlabel
);
1168 if (newlabel
->d_npartitions
<= OLDMAXPARTITIONS
)
1169 memcpy(addr
, newlabel
, sizeof (struct olddisklabel
));
1172 free(newlabel
, M_TEMP
);
1177 return (sd_getcache(sd
, (int *) addr
));
1180 if ((flag
& FWRITE
) == 0)
1182 return (sd_setcache(sd
, *(int *) addr
));
1186 * XXX Do we really need to care about having a writable
1187 * file descriptor here?
1189 if ((flag
& FWRITE
) == 0)
1191 if (((sd
->flags
& SDF_DIRTY
) != 0 || *(int *)addr
!= 0)) {
1192 error
= sd_flush(sd
, 0);
1194 sd
->flags
&= ~SDF_FLUSHING
;
1196 sd
->flags
&= ~(SDF_FLUSHING
|SDF_DIRTY
);
1203 struct dkwedge_info
*dkw
= (void *) addr
;
1205 if ((flag
& FWRITE
) == 0)
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
));
1216 struct dkwedge_info
*dkw
= (void *) addr
;
1218 if ((flag
& FWRITE
) == 0)
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
));
1229 struct dkwedge_list
*dkwl
= (void *) addr
;
1231 return (dkwedge_list(&sd
->sc_dk
, dkwl
, l
));
1235 if (part
!= RAW_PART
)
1237 return (scsipi_do_ioctl(periph
, dev
, cmd
, addr
, flag
, l
));
1241 panic("sdioctl: impossible");
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
;
1261 case SCSIPI_BUSTYPE_ATAPI
:
1262 lp
->d_type
= DTYPE_ATAPI
;
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
?
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
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
);
1313 aprint_error_dev(sd
->sc_dev
, "%s\n", errstring
);
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
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
;
1335 sd
->flags
&= ~(SDF_FLUSHING
|SDF_DIRTY
);
1341 sd_suspend(device_t dv PMF_FN_ARGS
)
1343 return sd_shutdown(dv
, boothowto
); /* XXX no need to poll */
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
1361 if (periph
->periph_flags
& PERIPH_RECOVERING
)
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
;
1384 * If the device is not open yet, let the generic code handle it.
1386 if ((periph
->periph_flags
& PERIPH_MEDIA_LOADED
) == 0)
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
)
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
);
1410 } else if (sense
->ascq
== 0x02) {
1411 printf("%s: pack is stopped, restarting...\n",
1412 device_xname(sd
->sc_dev
));
1414 periph
->periph_flags
|= PERIPH_RECOVERING
;
1416 error
= scsipi_start(periph
, SSS_START
,
1417 XS_CTL_URGENT
|XS_CTL_HEAD_TAG
|
1418 XS_CTL_THAW_PERIPH
|XS_CTL_FREEZE_PERIPH
);
1420 aprint_error_dev(sd
->sc_dev
,
1421 "unable to restart pack\n");
1426 periph
->periph_flags
&= ~PERIPH_RECOVERING
;
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 */
1443 struct sd_softc
*sd
;
1444 int part
, unit
, omask
;
1448 sd
= device_lookup_private(&sd_cd
, unit
);
1452 if (!device_is_active(sd
->sc_dev
))
1456 omask
= sd
->sc_dk
.dk_openmask
& (1 << part
);
1458 if (omask
== 0 && sdopen(dev
, 0, S_IFBLK
, NULL
) != 0)
1460 if ((sd
->sc_periph
->periph_flags
& PERIPH_MEDIA_LOADED
) == 0)
1462 else if (sd
->sc_dk
.dk_label
->d_partitions
[part
].p_fstype
!= FS_SWAP
)
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)
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.
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 */
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. */
1500 /* Mark as active early. */
1503 unit
= SDUNIT(dev
); /* Decompose unit & partition. */
1506 /* Check for acceptable drive number. */
1507 sd
= device_lookup_private(&sd_cd
, unit
);
1511 if (!device_is_active(sd
->sc_dev
))
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)
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)
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
))
1536 /* Offset block number to start of partition. */
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
1557 memset(xs
, 0, sizeof(sx
));
1558 xs
->xs_control
|= XS_CTL_NOSLEEP
| XS_CTL_POLL
|
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
;
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
)
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 */
1589 va
= (char *)va
+ sectorsize
* nwrt
;
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
)) {
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);
1608 return scsipi_mode_sense(sd
->sc_periph
, byte2
, page
, sense
,
1609 size
+ sizeof(struct scsi_mode_parameter_header_6
),
1610 flags
, SDRETRIES
, 6000);
1615 sd_mode_select(struct sd_softc
*sd
, u_int8_t byte2
, void *sense
, size_t size
,
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);
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,
1642 sd_validate_blksize(struct scsipi_periph
*periph
, int len
)
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
);
1668 * Find out from the device what its capacity is.
1671 sd_read_capacity(struct scsipi_periph
*periph
, int *blksize
, int flags
)
1674 struct scsipi_read_capacity_10 cmd
;
1675 struct scsipi_read_capacity_16 cmd16
;
1678 struct scsipi_read_capacity_10_data data
;
1679 struct scsipi_read_capacity_16_data data16
;
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
);
1697 * If the command works, interpret the result as a 4 byte
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)
1707 if (_4btol(datap
->data
.addr
) != 0xffffffff) {
1708 *blksize
= _4btol(datap
->data
.length
);
1709 rv
= _4btol(datap
->data
.addr
) + 1;
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)
1729 *blksize
= _4btol(datap
->data16
.length
);
1730 rv
= _8btol(datap
->data16
.addr
) + 1;
1733 free(datap
, M_TEMP
);
1738 sd_get_simplifiedparms(struct sd_softc
*sd
, struct disk_parms
*dp
, int flags
)
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 */
1756 * sd_read_capacity (ie "read capacity") and mode sense page 6
1757 * give the same information. Do both for now, and check
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);
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.
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.
1801 sd_get_capacity(struct sd_softc
*sd
, struct disk_parms
*dp
, int flags
)
1810 dp
->disksize
= blocks
= sd_read_capacity(sd
->sc_periph
, &blksize
,
1813 struct scsipi_read_format_capacities cmd
;
1815 struct scsipi_capacity_list_header header
;
1816 struct scsipi_capacity_descriptor desc
;
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
);
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");
1839 switch (data
.desc
.byte5
& SCSIPI_CAP_DESC_CODE_MASK
) {
1840 case SCSIPI_CAP_DESC_CODE_RESERVED
:
1841 case SCSIPI_CAP_DESC_CODE_FORMATTED
:
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
);
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
;
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
);
1867 bdesc
= (void *)(&scsipi_sense
.header
.big
+ 1);
1868 bsize
= _2btol(scsipi_sense
.header
.big
.blk_desc_len
);
1870 bdesc
= (void *)(&scsipi_sense
.header
.small
+ 1);
1871 bsize
= scsipi_sense
.header
.small
.blk_desc_len
;
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");
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
;
1895 sd_get_parms_page4(struct sd_softc
*sd
, struct disk_parms
*dp
, int flags
)
1897 struct sd_mode_sense_data scsipi_sense
;
1901 union scsi_disk_pages
*pages
;
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
);
1911 if (byte2
== SMS_DBD
) {
1912 /* No result; try once more with DBD off */
1920 poffset
= sizeof scsipi_sense
.header
.big
;
1921 poffset
+= _2btol(scsipi_sense
.header
.big
.blk_desc_len
);
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
))
1930 pages
= (void *)((u_long
)&scsipi_sense
+ poffset
);
1936 printf("page 4 sense:");
1937 for (i
= sizeof(scsipi_sense
), p
= (void *)&scsipi_sense
; i
;
1939 printf(" %02x", *p
);
1941 printf("page 4 pg_code=%d sense=%p/%p\n",
1942 pages
->rigid_geometry
.pg_code
, &scsipi_sense
, pages
);
1946 if ((pages
->rigid_geometry
.pg_code
& PGCODE_MASK
) != 4)
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
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)
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;
1974 printf("page 4 ok\n");
1980 sd_get_parms_page5(struct sd_softc
*sd
, struct disk_parms
*dp
, int flags
)
1982 struct sd_mode_sense_data scsipi_sense
;
1986 union scsi_disk_pages
*pages
;
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
);
1996 if (byte2
== SMS_DBD
) {
1997 /* No result; try once more with DBD off */
2005 poffset
= sizeof scsipi_sense
.header
.big
;
2006 poffset
+= _2btol(scsipi_sense
.header
.big
.blk_desc_len
);
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
))
2015 pages
= (void *)((u_long
)&scsipi_sense
+ poffset
);
2021 printf("page 5 sense:");
2022 for (i
= sizeof(scsipi_sense
), p
= (void *)&scsipi_sense
; i
;
2024 printf(" %02x", *p
);
2026 printf("page 5 pg_code=%d sense=%p/%p\n",
2027 pages
->flex_geometry
.pg_code
, &scsipi_sense
, pages
);
2031 if ((pages
->flex_geometry
.pg_code
& PGCODE_MASK
) != 5)
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)
2047 dp
->rot_rate
= _2btol(pages
->rigid_geometry
.rpm
);
2048 if (dp
->rot_rate
== 0)
2049 dp
->rot_rate
= 3600;
2052 printf("page 5 ok\n");
2058 sd_get_parms(struct sd_softc
*sd
, struct disk_parms
*dp
, int flags
)
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
);
2069 disk_blocksize(&sd
->sc_dk
, dp
->blksize
);
2073 error
= sd_get_capacity(sd
, dp
, flags
);
2077 disk_blocksize(&sd
->sc_dk
, dp
->blksize
);
2079 if (sd
->type
== T_OPTICAL
)
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
))
2087 if (!sd_get_parms_page4(sd
, dp
, flags
) ||
2088 !sd_get_parms_page5(sd
, dp
, flags
))
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..)
2105 dp
->cyls
= dp
->disksize
/ (64 * 32);
2107 dp
->rot_rate
= 3600;
2110 sd_set_properties(sd
);
2112 return (SDGP_RESULT_OK
);
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
))
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
));
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;
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
);
2166 pages
= (void *)(&scsipi_sense
.header
.big
+ 1);
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
);
2183 pages
= (void *)(&scsipi_sense
.header
.big
+ 1);
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
;
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
;
2204 uint8_t oflags
, byte2
= 0;
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
);
2218 pages
= (void *)(&scsipi_sense
.header
.big
+ 1);
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
;
2227 pages
->caching_params
.flags
|= CACHING_RCD
;
2229 if (bits
& DKCACHE_WRITE
)
2230 pages
->caching_params
.flags
|= CACHING_WCE
;
2232 pages
->caching_params
.flags
&= ~CACHING_WCE
;
2234 if (oflags
== pages
->caching_params
.flags
)
2237 pages
->caching_params
.pg_code
&= PGCODE_MASK
;
2239 if (bits
& DKCACHE_SAVE
)
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
));
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",
2268 prop_dictionary_set_uint64(geom
, "cylinders-per-unit",
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
;
2285 prop_object_release(odisk_info
);