Home | History | Annotate | Line # | Download | only in boot
sd.c revision 1.3.6.3
      1  1.3.6.3     skrll /*      $NetBSD: sd.c,v 1.3.6.3 2004/09/21 13:19:44 skrll 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.1       dbj #include <dev/scsipi/scsipi_all.h>
     35      1.1       dbj #include <dev/scsipi/scsi_all.h>
     36      1.1       dbj #include <dev/scsipi/scsipi_disk.h>
     37      1.1       dbj #include <lib/libsa/stand.h>
     38      1.1       dbj #include <lib/libkern/libkern.h> /* for bzero() */
     39      1.3  christos #include "dmareg.h"
     40      1.1       dbj 
     41      1.3  christos #ifdef xSD_DEBUG
     42      1.1       dbj #define DPRINTF(x) printf x;
     43      1.1       dbj #else
     44      1.1       dbj #define DPRINTF(x)
     45      1.1       dbj #endif
     46      1.1       dbj 
     47      1.1       dbj struct sdminilabel {
     48      1.1       dbj     u_short npart;
     49      1.3  christos     long offset[MAXPARTITIONS+1]; /* offset from absolute block 0! */
     50      1.1       dbj };
     51      1.1       dbj 
     52      1.1       dbj struct  sd_softc {
     53      1.1       dbj     int sc_unit;
     54      1.1       dbj     int sc_lun;
     55      1.1       dbj     int sc_part;
     56      1.1       dbj     int sc_dev_bsize;
     57      1.1       dbj     struct sdminilabel sc_pinfo;
     58      1.1       dbj };
     59      1.1       dbj 
     60      1.1       dbj #define NSD 7
     61      1.1       dbj #define MAXRETRIES 5	/* must be at least one */
     62      1.1       dbj 
     63      1.1       dbj void	scsi_init(void);
     64      1.3  christos int	scsiicmd(char, char, u_char *, int, char *, int *);
     65      1.1       dbj int	sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
     66      1.1       dbj 		   void *buf, size_t *rsize);
     67      1.2       dbj int sdprobe(char target, char lun);
     68      1.2       dbj int sdgetinfo(struct sd_softc *ss);
     69      1.2       dbj int sdopen(struct open_file *f, char count, char lun, char part);
     70      1.2       dbj int sdclose(struct open_file *f);
     71      1.1       dbj 
     72      1.1       dbj int
     73      1.1       dbj sdprobe(char target, char lun)
     74      1.1       dbj {
     75      1.1       dbj     struct scsipi_test_unit_ready cdb1;
     76      1.1       dbj     struct scsipi_inquiry cdb2;
     77      1.1       dbj     struct scsipi_inquiry_data inq;
     78      1.1       dbj     int error, retries;
     79      1.3  christos     int count;
     80      1.1       dbj 
     81      1.1       dbj     bzero(&cdb1, sizeof(cdb1));
     82      1.1       dbj     cdb1.opcode =  TEST_UNIT_READY;
     83      1.1       dbj 
     84      1.1       dbj     retries = 0;
     85      1.1       dbj     do {
     86      1.3  christos 	    count = 0;
     87      1.3  christos 	error = scsiicmd(target, lun, (u_char *)&cdb1, sizeof(cdb1), NULL, &count);
     88      1.1       dbj 	if (error == -SCSI_BUSY) {
     89      1.1       dbj 		register int N = 10000000; while (--N > 0);
     90      1.1       dbj 	}
     91      1.1       dbj     } while ((error == -SCSI_CHECK || error == -SCSI_BUSY)
     92      1.1       dbj 	     && retries++ < MAXRETRIES);
     93      1.1       dbj 
     94      1.1       dbj     if (error)
     95      1.1       dbj 	return error<0 ? ENODEV : error;
     96      1.1       dbj 
     97      1.1       dbj     bzero(&cdb2, sizeof(cdb2));
     98      1.1       dbj     cdb2.opcode = INQUIRY;
     99      1.1       dbj     cdb2.length = sizeof(inq);
    100      1.3  christos     count = sizeof (inq);
    101      1.1       dbj     error = scsiicmd(target, lun, (u_char *)&cdb2, sizeof(cdb2),
    102      1.3  christos 		     (char *)&inq, &count);
    103      1.1       dbj     if (error != 0)
    104      1.1       dbj       return error<0 ? EHER : error;
    105      1.1       dbj 
    106      1.1       dbj     if ((inq.device & SID_TYPE) != T_DIRECT
    107      1.1       dbj 	&& (inq.device & SID_TYPE) != T_CDROM)
    108      1.1       dbj       return EUNIT;	/* not a disk */
    109      1.1       dbj 
    110      1.1       dbj     DPRINTF(("booting disk %s.\n", inq.vendor));
    111      1.1       dbj 
    112      1.1       dbj     return 0;
    113      1.1       dbj }
    114      1.1       dbj 
    115      1.1       dbj int
    116      1.1       dbj sdgetinfo(struct sd_softc *ss)
    117      1.1       dbj {
    118      1.1       dbj     struct scsipi_read_capacity cdb;
    119      1.1       dbj     struct scsipi_read_cap_data cap;
    120      1.1       dbj     struct sdminilabel *pi = &ss->sc_pinfo;
    121  1.3.6.1     skrll     struct next68k_disklabel *label;
    122      1.1       dbj     int error, i, blklen;
    123  1.3.6.1     skrll     char io_buf[NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET];
    124      1.3  christos     int count;
    125      1.3  christos     int sc_blkshift = 0;
    126      1.1       dbj 
    127      1.1       dbj     bzero(&cdb, sizeof(cdb));
    128      1.1       dbj     cdb.opcode = READ_CAPACITY;
    129      1.3  christos     count = sizeof(cap);
    130      1.1       dbj     error = scsiicmd(ss->sc_unit, ss->sc_lun, (u_char *)&cdb, sizeof(cdb),
    131      1.3  christos 		     (char *)&cap, &count);
    132      1.1       dbj     if (error != 0)
    133      1.1       dbj 	return error<0 ? EHER : error;
    134      1.1       dbj     blklen = (cap.length[0]<<24) + (cap.length[1]<<16)
    135      1.1       dbj 	     + (cap.length[2]<<8) + cap.length[3];
    136      1.1       dbj     ss->sc_dev_bsize = blklen;
    137      1.3  christos 
    138      1.1       dbj     ss->sc_pinfo.offset[ss->sc_part] = 0; /* read absolute sector */
    139  1.3.6.1     skrll     error = sdstrategy(ss, F_READ, NEXT68K_LABEL_SECTOR,
    140  1.3.6.1     skrll 		       NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET, io_buf, &i);
    141      1.1       dbj     if (error != 0) {
    142      1.1       dbj 	DPRINTF(("sdgetinfo: sdstrategy error %d\n", error));
    143      1.1       dbj         return(ERDLAB);
    144      1.1       dbj     }
    145  1.3.6.1     skrll     label = (struct next68k_disklabel *)(io_buf+NEXT68K_LABEL_OFFSET);
    146      1.1       dbj 
    147      1.3  christos     if (!IS_DISKLABEL(label)) /*  || (label->cd_flags & CD_UNINIT)!=0) */
    148      1.1       dbj 	return EUNLAB;		/* bad magic */
    149      1.1       dbj 
    150      1.1       dbj     /* XXX calculate checksum ... for now we rely on the magic number */
    151      1.1       dbj     DPRINTF(("Disk is %s (%s, %s).\n",
    152      1.1       dbj 	     label->cd_label,label->cd_name, label->cd_type));
    153      1.1       dbj 
    154      1.1       dbj     while (label->cd_secsize > blklen)
    155      1.1       dbj     {
    156      1.1       dbj 	blklen <<= 1;
    157      1.3  christos 	++sc_blkshift;
    158      1.1       dbj     }
    159      1.1       dbj     if (label->cd_secsize < blklen)
    160      1.1       dbj     {
    161      1.1       dbj 	printf("bad label sectorsize (%d) or device blocksize (%d).\n",
    162      1.3  christos 	       label->cd_secsize, blklen>>sc_blkshift);
    163      1.1       dbj 	return ENXIO;
    164      1.1       dbj     }
    165      1.3  christos     pi->npart = 0;
    166      1.1       dbj     for(i=0; i<MAXPARTITIONS; i++) {
    167      1.3  christos 	if (label->cd_partitions[i].cp_size > 0) {
    168      1.3  christos 	    pi->offset[pi->npart] = (label->cd_partitions[i].cp_offset
    169      1.3  christos 				     + label->cd_front) << sc_blkshift;
    170      1.3  christos 	}
    171      1.1       dbj 	else
    172      1.3  christos 	    pi->offset[pi->npart] = -1;
    173      1.3  christos 	DPRINTF (("%d: [%d]=%ld\n", i, pi->npart, pi->offset[pi->npart]));
    174      1.3  christos 	pi->npart++;
    175      1.3  christos 	if (pi->npart == RAW_PART)
    176      1.3  christos 	    pi->npart++;
    177      1.1       dbj     }
    178      1.3  christos     pi->offset[RAW_PART] = -1;
    179      1.3  christos 
    180      1.1       dbj     return 0;
    181      1.1       dbj }
    182      1.1       dbj 
    183      1.1       dbj int
    184      1.1       dbj sdopen(struct open_file *f, char count, char lun, char part)
    185      1.1       dbj {
    186      1.1       dbj     register struct sd_softc *ss;
    187      1.1       dbj     char unit, cnt;
    188      1.1       dbj     int error;
    189      1.1       dbj 
    190      1.1       dbj     DPRINTF(("open: sd(%d,%d,%d)\n", count, lun, part));
    191      1.1       dbj 
    192      1.1       dbj     if (lun >= NSD)
    193      1.1       dbj 	return EUNIT;
    194      1.1       dbj 
    195      1.1       dbj     scsi_init();
    196      1.1       dbj 
    197      1.1       dbj     for(cnt=0, unit=0; unit < NSD; unit++)
    198      1.1       dbj     {
    199      1.1       dbj 	DPRINTF(("trying target %d lun %d.\n", unit, lun));
    200      1.1       dbj 	error = sdprobe(unit, lun);
    201      1.1       dbj 	if (error == 0)
    202      1.1       dbj 	{
    203      1.1       dbj 	    if (cnt++ == count)
    204      1.1       dbj 		break;
    205      1.1       dbj 	}
    206      1.1       dbj 	else if (error != EUNIT)
    207      1.1       dbj 	    return error;
    208      1.1       dbj     }
    209      1.1       dbj 
    210      1.1       dbj     if (unit >= NSD)
    211      1.1       dbj 	return EUNIT;
    212      1.1       dbj 
    213      1.1       dbj     ss = alloc(sizeof(struct sd_softc));
    214      1.1       dbj     ss->sc_unit = unit;
    215      1.1       dbj     ss->sc_lun = lun;
    216      1.1       dbj     ss->sc_part = part;
    217      1.1       dbj 
    218      1.1       dbj     if ((error = sdgetinfo(ss)) != 0)
    219      1.1       dbj 	return error;
    220      1.1       dbj 
    221      1.1       dbj     if ((unsigned char)part >= ss->sc_pinfo.npart
    222      1.1       dbj 	|| ss->sc_pinfo.offset[(int)part] == -1)
    223      1.1       dbj 	return EPART;
    224      1.1       dbj 
    225      1.1       dbj     f->f_devdata = ss;
    226      1.1       dbj     return 0;
    227      1.1       dbj }
    228      1.1       dbj 
    229      1.1       dbj int
    230      1.1       dbj sdclose(struct open_file *f)
    231      1.1       dbj {
    232      1.1       dbj     register struct sd_softc *ss = f->f_devdata;
    233      1.1       dbj 
    234      1.1       dbj     free(ss, sizeof(struct sd_softc));
    235      1.1       dbj     return 0;
    236      1.1       dbj }
    237      1.1       dbj 
    238      1.1       dbj int
    239      1.1       dbj sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
    240      1.1       dbj 	   void *buf, size_t *rsize)
    241      1.1       dbj {
    242      1.3  christos     u_long blk = dblk + ss->sc_pinfo.offset[ss->sc_part];
    243      1.1       dbj     struct scsipi_rw_big cdb;
    244      1.1       dbj     int error;
    245      1.1       dbj 
    246      1.1       dbj     if (size == 0)
    247      1.1       dbj 	return 0;
    248      1.1       dbj 
    249      1.1       dbj     if (rw != F_READ)
    250      1.1       dbj     {
    251      1.1       dbj 	printf("sdstrategy: write not implemented.\n");
    252      1.1       dbj 	return EOPNOTSUPP;
    253      1.1       dbj     }
    254      1.1       dbj 
    255      1.3  christos     *rsize = 0;
    256      1.3  christos     while (size > 0) {
    257      1.3  christos 	    u_long nblks;
    258      1.3  christos 	    int tsize;
    259      1.3  christos 	    if (size > MAX_DMASIZE)
    260      1.3  christos 		    tsize = MAX_DMASIZE;
    261      1.3  christos 	    else
    262      1.3  christos 		    tsize = size;
    263      1.3  christos 
    264      1.3  christos 	    nblks = howmany(tsize, ss->sc_dev_bsize);
    265      1.3  christos 
    266      1.3  christos 	    DPRINTF(("sdstrategy: read block %ld, %d bytes (%ld blks a %d bytes).\n",
    267      1.3  christos 		     blk, tsize, nblks, ss->sc_dev_bsize));
    268      1.3  christos 
    269      1.3  christos 	    bzero(&cdb, sizeof(cdb));
    270      1.3  christos 	    cdb.opcode = READ_BIG;
    271      1.3  christos 	    cdb.addr[0] = (blk & 0xff000000) >> 24;
    272      1.3  christos 	    cdb.addr[1] = (blk & 0xff0000) >> 16;
    273      1.3  christos 	    cdb.addr[2] = (blk & 0xff00) >> 8;
    274      1.3  christos 	    cdb.addr[3] = blk & 0xff;
    275      1.3  christos 	    cdb.length[0] = (nblks & 0xff00) >> 8;
    276      1.3  christos 	    cdb.length[1] = nblks & 0xff;
    277      1.3  christos 
    278      1.3  christos 	    error = scsiicmd(ss->sc_unit, ss->sc_lun,
    279      1.3  christos 			     (u_char *)&cdb, sizeof(cdb), (unsigned char *)buf + *rsize, &tsize);
    280      1.3  christos 	    if (error != 0)
    281      1.3  christos 	    {
    282      1.3  christos 		    DPRINTF(("sdstrategy: scsiicmd failed: %d = %s.\n", error, strerror(error)));
    283      1.3  christos 		    return error<0 ? EIO : error;
    284      1.3  christos 	    }
    285      1.3  christos 	    *rsize += tsize;
    286      1.3  christos 	    size -= tsize;
    287      1.3  christos 	    blk += nblks;
    288      1.1       dbj     }
    289      1.3  christos     DPRINTF(("sdstrategy: read %d bytes\n", *rsize));
    290      1.1       dbj     return 0;
    291      1.1       dbj }
    292      1.3  christos 
    293