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