Home | History | Annotate | Line # | Download | only in boot
sd.c revision 1.1.1.1
      1 /*      $NetBSD: sd.c,v 1.1.1.1 1998/06/09 07:53:06 dbj Exp $        */
      2 /*
      3  * Copyright (c) 1994 Rolf Grossmann
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Rolf Grossmann.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/disklabel.h>
     34 #include <dev/scsipi/scsipi_all.h>
     35 #include <dev/scsipi/scsi_all.h>
     36 #include <dev/scsipi/scsipi_disk.h>
     37 #include <lib/libsa/stand.h>
     38 #include <lib/libkern/libkern.h> /* for bzero() */
     39 
     40 #ifdef SD_DEBUG
     41 #define DPRINTF(x) printf x;
     42 #else
     43 #define DPRINTF(x)
     44 #endif
     45 
     46 struct sdminilabel {
     47     u_short npart;
     48     long offset[MAXPARTITIONS]; /* offset from absolute block 0! */
     49 };
     50 
     51 struct  sd_softc {
     52     int sc_unit;
     53     int sc_lun;
     54     int sc_part;
     55     short sc_blkshift;
     56     int sc_dev_bsize;
     57     struct sdminilabel sc_pinfo;
     58 };
     59 
     60 #define NSD 7
     61 #define MAXRETRIES 5	/* must be at least one */
     62 
     63 void	scsi_init(void);
     64 int	scsiicmd(char, char, u_char *, int, char *, int);
     65 int	sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
     66 		   void *buf, size_t *rsize);
     67 
     68 int
     69 sdprobe(char target, char lun)
     70 {
     71     struct scsipi_test_unit_ready cdb1;
     72     struct scsipi_inquiry cdb2;
     73     struct scsipi_inquiry_data inq;
     74     int error, retries;
     75 
     76     bzero(&cdb1, sizeof(cdb1));
     77     cdb1.opcode =  TEST_UNIT_READY;
     78 
     79     retries = 0;
     80     do {
     81 	error = scsiicmd(target, lun, (u_char *)&cdb1, sizeof(cdb1), NULL, 0);
     82 	if (error == -SCSI_BUSY) {
     83 		register int N = 10000000; while (--N > 0);
     84 	}
     85     } while ((error == -SCSI_CHECK || error == -SCSI_BUSY)
     86 	     && retries++ < MAXRETRIES);
     87 
     88     if (error)
     89 	return error<0 ? ENODEV : error;
     90 
     91     bzero(&cdb2, sizeof(cdb2));
     92     cdb2.opcode = INQUIRY;
     93     cdb2.length = sizeof(inq);
     94     error = scsiicmd(target, lun, (u_char *)&cdb2, sizeof(cdb2),
     95 		     (char *)&inq, sizeof(inq));
     96     if (error != 0)
     97       return error<0 ? EHER : error;
     98 
     99     if ((inq.device & SID_TYPE) != T_DIRECT
    100 	&& (inq.device & SID_TYPE) != T_CDROM)
    101       return EUNIT;	/* not a disk */
    102 
    103     DPRINTF(("booting disk %s.\n", inq.vendor));
    104 
    105     return 0;
    106 }
    107 
    108 int
    109 sdgetinfo(struct sd_softc *ss)
    110 {
    111     struct scsipi_read_capacity cdb;
    112     struct scsipi_read_cap_data cap;
    113     struct sdminilabel *pi = &ss->sc_pinfo;
    114     struct cpu_disklabel *label;
    115     int error, i, blklen;
    116     char io_buf[LABELSIZE+LABELOFFSET];
    117 
    118     bzero(&cdb, sizeof(cdb));
    119     cdb.opcode = READ_CAPACITY;
    120     error = scsiicmd(ss->sc_unit, ss->sc_lun, (u_char *)&cdb, sizeof(cdb),
    121 		     (char *)&cap, sizeof(cap));
    122     if (error != 0)
    123 	return error<0 ? EHER : error;
    124 
    125     blklen = (cap.length[0]<<24) + (cap.length[1]<<16)
    126 	     + (cap.length[2]<<8) + cap.length[3];
    127     ss->sc_dev_bsize = blklen;
    128     ss->sc_blkshift = 0;
    129     ss->sc_pinfo.offset[ss->sc_part] = 0; /* read absolute sector */
    130     error = sdstrategy(ss, F_READ, LABELSECTOR,
    131 		       LABELSIZE+LABELOFFSET, io_buf, &i);
    132     if (error != 0) {
    133 	DPRINTF(("sdgetinfo: sdstrategy error %d\n", error));
    134         return(ERDLAB);
    135     }
    136     label = (struct cpu_disklabel *)(io_buf+LABELOFFSET);
    137 
    138     if (!IS_DISKLABEL(label) || (label->cd_flags & CD_UNINIT)!=0)
    139 	return EUNLAB;		/* bad magic */
    140 
    141     /* XXX calculate checksum ... for now we rely on the magic number */
    142     DPRINTF(("Disk is %s (%s, %s).\n",
    143 	     label->cd_label,label->cd_name, label->cd_type));
    144 
    145     while (label->cd_secsize > blklen)
    146     {
    147 	blklen <<= 1;
    148 	++ss->sc_blkshift;
    149     }
    150     if (label->cd_secsize < blklen)
    151     {
    152 	printf("bad label sectorsize (%d) or device blocksize (%d).\n",
    153 	       label->cd_secsize, blklen>>ss->sc_blkshift);
    154 	return ENXIO;
    155     }
    156     pi->npart = MAXPARTITIONS;
    157     for(i=0; i<MAXPARTITIONS; i++) {
    158 	if (label->cd_partitions[i].cp_size > 0)
    159 	    pi->offset[i] = label->cd_partitions[i].cp_offset
    160 			    + label->cd_front;
    161 	else
    162 	    pi->offset[i] = -1;
    163     }
    164 
    165     return 0;
    166 }
    167 
    168 int
    169 sdopen(struct open_file *f, char count, char lun, char part)
    170 {
    171     register struct sd_softc *ss;
    172     char unit, cnt;
    173     int error;
    174 
    175     DPRINTF(("open: sd(%d,%d,%d)\n", count, lun, part));
    176 
    177     if (lun >= NSD)
    178 	return EUNIT;
    179 
    180     scsi_init();
    181 
    182     for(cnt=0, unit=0; unit < NSD; unit++)
    183     {
    184 	DPRINTF(("trying target %d lun %d.\n", unit, lun));
    185 	error = sdprobe(unit, lun);
    186 	if (error == 0)
    187 	{
    188 	    if (cnt++ == count)
    189 		break;
    190 	}
    191 	else if (error != EUNIT)
    192 	    return error;
    193     }
    194 
    195     if (unit >= NSD)
    196 	return EUNIT;
    197 
    198     ss = alloc(sizeof(struct sd_softc));
    199     ss->sc_unit = unit;
    200     ss->sc_lun = lun;
    201     ss->sc_part = part;
    202 
    203     if ((error = sdgetinfo(ss)) != 0)
    204 	return error;
    205 
    206     if ((unsigned char)part >= ss->sc_pinfo.npart
    207 	|| ss->sc_pinfo.offset[(int)part] == -1)
    208 	return EPART;
    209 
    210     f->f_devdata = ss;
    211     return 0;
    212 }
    213 
    214 int
    215 sdclose(struct open_file *f)
    216 {
    217     register struct sd_softc *ss = f->f_devdata;
    218 
    219     free(ss, sizeof(struct sd_softc));
    220     return 0;
    221 }
    222 
    223 int
    224 sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
    225 	   void *buf, size_t *rsize)
    226 {
    227     u_long blk = (dblk + ss->sc_pinfo.offset[ss->sc_part]) << ss->sc_blkshift;
    228     u_long nblks = howmany(size, ss->sc_dev_bsize);
    229     struct scsipi_rw_big cdb;
    230     int error;
    231 
    232     if (size == 0)
    233 	return 0;
    234 
    235     if (rw != F_READ)
    236     {
    237 	printf("sdstrategy: write not implemented.\n");
    238 	return EOPNOTSUPP;
    239     }
    240 
    241     DPRINTF(("sdstrategy: read block %ld, %d bytes (%ld blks).\n",
    242 	     blk, size, nblks));
    243 
    244     bzero(&cdb, sizeof(cdb));
    245     cdb.opcode = READ_BIG;
    246     cdb.addr[0] = (blk & 0xff000000) >> 24;
    247     cdb.addr[1] = (blk & 0xff0000) >> 16;
    248     cdb.addr[2] = (blk & 0xff00) >> 8;
    249     cdb.addr[3] = blk & 0xff;
    250     cdb.length[0] = (nblks & 0xff00) >> 8;
    251     cdb.length[1] = nblks & 0xff;
    252 
    253     error = scsiicmd(ss->sc_unit, ss->sc_lun,
    254 		     (u_char *)&cdb, sizeof(cdb), buf, size);
    255     if (error != 0)
    256     {
    257 	DPRINTF(("sdstrategy: scsiicmd failed: %d = %s.\n", error, strerror(error)));
    258 	return error<0 ? EIO : error;
    259     }
    260     *rsize = size;
    261     return 0;
    262 }
    263