/* $NetBSD: sd.c,v 1.6 2024/07/02 05:34:08 rin Exp $ */
/*
* Copyright (c) 2010 KIYOHARA Takashi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static int sd_validate_blksize(int);
static uint64_t sd_read_capacity(struct sd_softc *, int *);
static int sd_get_simplifiedparms(struct sd_softc *);
static int sd_get_capacity(struct sd_softc *);
static int sd_get_parms_page4(struct sd_softc *, struct disk_parms *);
static int sd_get_parms_page5(struct sd_softc *, struct disk_parms *);
static int sd_get_parms(struct sd_softc *);
static void sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
static int sdgetdisklabel(struct sd_softc *);
int sdopen(struct open_file *, ...);
int sdclose(struct open_file *);
int sdstrategy(void *, int, daddr_t, size_t, void *, size_t *);
static int
sd_validate_blksize(int len)
{
switch (len) {
case 256:
case 512:
case 1024:
case 2048:
case 4096:
return 1;
}
return 0;
}
/*
* sd_read_capacity:
*
* Find out from the device what its capacity is.
*/
static uint64_t
sd_read_capacity(struct sd_softc *sd, int *blksize)
{
union {
struct scsipi_read_capacity_10 cmd;
struct scsipi_read_capacity_16 cmd16;
} cmd;
union {
struct scsipi_read_capacity_10_data data;
struct scsipi_read_capacity_16_data data16;
} data;
uint64_t rv;
/*
* If the command works, interpret the result as a 4 byte
* number of blocks
*/
rv = 0;
memset(&data, 0, sizeof(data.data));
if (scsi_command(sd, (void *)&cmd.cmd, sizeof(cmd.cmd),
(void *)&data, sizeof(data.data)) != 0)
goto out;
/*
* sd_read_capacity (ie "read capacity") and mode sense page 6
* give the same information. Do both for now, and check
* for consistency.
* XXX probably differs for removable media
*/
dp->blksize = SD_DEFAULT_BLKSIZE;
if ((blocks = sd_read_capacity(sd, &blksize)) == 0)
return SDGP_RESULT_OFFLINE; /* XXX? */
/*
* Get the scsi driver to send a full inquiry to the * device and use the
* results to fill out the disk parameter structure.
*/
static int
sd_get_capacity(struct sd_softc *sd)
{
struct disk_parms *dp = &sd->sc_params;
uint64_t blocks;
int error, blksize;
printf("page 4 sense:");
for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
i--, p++)
printf(" %02x", *p);
printf("\n");
printf("page 4 pg_code=%d sense=%p/%p\n",
pages->rigid_geometry.pg_code, &scsipi_sense, pages);
}
#endif
if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
return ERESTART;
/*
* KLUDGE!! (for zone recorded disks)
* give a number of sectors so that sec * trks * cyls
* is <= disk_size
* can lead to wasted space! THINK ABOUT THIS !
*/
dp->heads = pages->rigid_geometry.nheads;
dp->cyls = _3btol(pages->rigid_geometry.ncyl);
if (dp->heads == 0 || dp->cyls == 0)
return ERESTART;
dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
if (dp->rot_rate == 0)
dp->rot_rate = 3600;
#if 0
printf("page 4 ok\n");
#endif
return 0;
}
static int
sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp)
{
struct sd_mode_sense_data scsipi_sense;
union scsi_disk_pages *pages;
size_t poffset;
int byte2, error;
byte2 = SMS_DBD;
again:
memset(&scsipi_sense, 0, sizeof(scsipi_sense));
error = scsi_mode_sense(sd, 0, 5, &scsipi_sense.header,
(byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
sizeof(scsipi_sense.pages.flex_geometry));
if (error) {
if (byte2 == SMS_DBD) {
/* No result; try once more with DBD off */
byte2 = 0;
goto again;
}
return error;
}
dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
if (dp->rot_rate == 0)
dp->rot_rate = 3600;
#if 0
printf("page 5 ok\n");
#endif
return 0;
}
static int
sd_get_parms(struct sd_softc *sd)
{
struct disk_parms *dp = &sd->sc_params;
int error;
/*
* If offline, the SDEV_MEDIA_LOADED flag will be
* cleared by the caller if necessary.
*/
if (sd->sc_type == T_SIMPLE_DIRECT) {
error = sd_get_simplifiedparms(sd);
if (error)
return error;
goto ok;
}
error = sd_get_capacity(sd);
if (error)
return error;
if (sd->sc_type == T_OPTICAL)
goto page0;
if (sd->sc_flags & FLAGS_REMOVABLE) {
if (!sd_get_parms_page5(sd, dp) ||
!sd_get_parms_page4(sd, dp))
goto ok;
} else {
if (!sd_get_parms_page4(sd, dp) ||
!sd_get_parms_page5(sd, dp))
goto ok;
}
page0:
printf("fabricating a geometry\n");
/* Try calling driver's method for figuring out geometry. */
/*
* Use adaptec standard fictitious geometry
* this depends on which controller (e.g. 1542C is
* different. but we have to put SOMETHING here..)
*/
dp->heads = 64;
dp->sectors = 32;
dp->cyls = dp->disksize / (64 * 32);
dp->rot_rate = 3600;
/*
* Load the label information on the named device.
*/
static int
sdgetdisklabel(struct sd_softc *sd)
{
struct mbr_sector *mbr;
struct mbr_partition *mp;
struct disklabel *lp = &sd->sc_label;
size_t rsize;
int sector, i;
char *msg;
uint8_t buf[DEV_BSIZE];
sdgetdefaultlabel(sd, lp);
if (lp->d_secpercyl == 0) {
lp->d_secpercyl = 100;
/* as long as it's not 0 - readdisklabel divides by it (?) */
}
/*
* Find NetBSD Partition in DOS partition table.
*/
sector = 0;
if (sdstrategy(sd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
return EOFFSET;
mbr = (struct mbr_sector *)buf;
if (mbr->mbr_magic == htole16(MBR_MAGIC)) {
/*
* Lookup NetBSD slice. If there is none, go ahead
* and try to read the disklabel off sector #0.
*/
mp = mbr->mbr_parts;
for (i = 0; i < MBR_PART_COUNT; i++) {
if (mp[i].mbrp_type == MBR_PTYPE_NETBSD) {
sector = le32toh(mp[i].mbrp_start);
break;
}
}
}
if (sd->sc_flags & FLAGS_REMOVABLE) {
printf("XXXXX: removable device found. will not support\n");
}
if (!(sd->sc_flags & FLAGS_MEDIA_LOADED))
sd->sc_flags |= FLAGS_MEDIA_LOADED;
if ((error = sd_get_parms(sd)) != 0)
return error;