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