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