1 1.1 tsutsui /* $NetBSD: iris_disk.c,v 1.1 2019/01/12 16:44:47 tsutsui Exp $ */ 2 1.1 tsutsui 3 1.1 tsutsui /* 4 1.1 tsutsui * Copyright (c) 2018 Naruaki Etomi 5 1.1 tsutsui * All rights reserved. 6 1.1 tsutsui * 7 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 8 1.1 tsutsui * modification, are permitted provided that the following conditions 9 1.1 tsutsui * are met: 10 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 11 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 12 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 14 1.1 tsutsui * documentation and/or other materials provided with the distribution. 15 1.1 tsutsui * 16 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 tsutsui */ 27 1.1 tsutsui 28 1.1 tsutsui /* 29 1.1 tsutsui * Copyright (c) 1988 University of Utah. 30 1.1 tsutsui * Copyright (c) 1990, 1993 31 1.1 tsutsui * The Regents of the University of California. All rights reserved. 32 1.1 tsutsui * 33 1.1 tsutsui * This code is derived from software contributed to Berkeley by 34 1.1 tsutsui * Van Jacobson of Lawrence Berkeley Laboratory and the Systems 35 1.1 tsutsui * Programming Group of the University of Utah Computer Science Department. 36 1.1 tsutsui * 37 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 38 1.1 tsutsui * modification, are permitted provided that the following conditions 39 1.1 tsutsui * are met: 40 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 41 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 42 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 43 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 44 1.1 tsutsui * documentation and/or other materials provided with the distribution. 45 1.1 tsutsui * 3. Neither the name of the University nor the names of its contributors 46 1.1 tsutsui * may be used to endorse or promote products derived from this software 47 1.1 tsutsui * without specific prior written permission. 48 1.1 tsutsui * 49 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 1.1 tsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 1.1 tsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 1.1 tsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 1.1 tsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 1.1 tsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 1.1 tsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 1.1 tsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 1.1 tsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 1.1 tsutsui * SUCH DAMAGE. 60 1.1 tsutsui * 61 1.1 tsutsui * from: Utah $Hdr: sd.c 1.9 92/12/21$ 62 1.1 tsutsui * 63 1.1 tsutsui * @(#)sd.c 8.1 (Berkeley) 6/10/93 64 1.1 tsutsui */ 65 1.1 tsutsui 66 1.1 tsutsui /* 67 1.1 tsutsui * Silicon Graphics "IRIS" series MIPS processors machine bootloader. 68 1.1 tsutsui * Disk I/O API routine. 69 1.1 tsutsui * Most of the following was adapted from /sys/arch/hp300/stand/common/sd.c. 70 1.1 tsutsui * NetBSD: sd.c,v 1.11 2011/07/17 20:54:40 joerg Exp 71 1.1 tsutsui */ 72 1.1 tsutsui 73 1.1 tsutsui #include <lib/libsa/stand.h> 74 1.1 tsutsui 75 1.1 tsutsui #include <sys/param.h> 76 1.1 tsutsui #include <sys/disklabel.h> 77 1.1 tsutsui 78 1.1 tsutsui #ifndef INDIGO_R3K_MODE 79 1.1 tsutsui #include <dev/arcbios/arcbios.h> 80 1.1 tsutsui #endif 81 1.1 tsutsui 82 1.1 tsutsui #include "disk.h" 83 1.1 tsutsui 84 1.1 tsutsui #include "iris_machdep.h" 85 1.1 tsutsui #include "iris_scsivar.h" 86 1.1 tsutsui 87 1.1 tsutsui struct disk_softc { 88 1.1 tsutsui int sc_part; /* disk partition number */ 89 1.1 tsutsui char sc_retry; 90 1.1 tsutsui char sc_alive; 91 1.1 tsutsui short sc_blkshift; 92 1.1 tsutsui struct disklabel sc_label; /* disk label for this disk */ 93 1.1 tsutsui }; 94 1.1 tsutsui 95 1.1 tsutsui static int diskinit(struct disk_softc *); 96 1.1 tsutsui 97 1.1 tsutsui #define DISKRETRY 2 98 1.1 tsutsui 99 1.1 tsutsui int 100 1.1 tsutsui diskstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr, 101 1.1 tsutsui size_t *cnt) 102 1.1 tsutsui { 103 1.1 tsutsui struct disk_softc *sc; 104 1.1 tsutsui struct disklabel *lp; 105 1.1 tsutsui uint8_t *buf; 106 1.1 tsutsui daddr_t blk; 107 1.1 tsutsui u_int nblk; 108 1.1 tsutsui int stat; 109 1.1 tsutsui 110 1.1 tsutsui sc = devdata; 111 1.1 tsutsui 112 1.1 tsutsui buf = addr; 113 1.1 tsutsui lp = &sc->sc_label; 114 1.1 tsutsui blk = bn + (lp->d_partitions[sc->sc_part].p_offset >> sc->sc_blkshift); 115 1.1 tsutsui nblk = reqcnt >> sc->sc_blkshift; 116 1.1 tsutsui 117 1.1 tsutsui sc->sc_retry = 0; 118 1.1 tsutsui 119 1.1 tsutsui retry: 120 1.1 tsutsui stat = scsi_read(buf, reqcnt, blk, nblk); 121 1.1 tsutsui 122 1.1 tsutsui if (stat) { 123 1.1 tsutsui DELAY(1000000); 124 1.1 tsutsui if (++sc->sc_retry > DISKRETRY) { 125 1.1 tsutsui return EIO; 126 1.1 tsutsui } 127 1.1 tsutsui printf("diskstrategy retry\n"); 128 1.1 tsutsui goto retry; 129 1.1 tsutsui } 130 1.1 tsutsui 131 1.1 tsutsui *cnt = reqcnt; 132 1.1 tsutsui 133 1.1 tsutsui return 0; 134 1.1 tsutsui } 135 1.1 tsutsui 136 1.1 tsutsui static int 137 1.1 tsutsui diskinit(struct disk_softc *sc) 138 1.1 tsutsui { 139 1.1 tsutsui uint8_t capbuf[2]; 140 1.1 tsutsui 141 1.1 tsutsui uint8_t stat; 142 1.1 tsutsui 143 1.1 tsutsui stat = scsi_test_unit_rdy(); 144 1.1 tsutsui 145 1.1 tsutsui if (stat != 0) { 146 1.1 tsutsui /* drive may be doing RTZ - wait a bit */ 147 1.1 tsutsui DELAY(1000000); 148 1.1 tsutsui stat = scsi_test_unit_rdy(); 149 1.1 tsutsui 150 1.1 tsutsui if (stat != 0) { 151 1.1 tsutsui printf("diskinit abort!\n"); 152 1.1 tsutsui printf("Boot failed! Halting...\n"); 153 1.1 tsutsui reboot(); 154 1.1 tsutsui } 155 1.1 tsutsui } 156 1.1 tsutsui 157 1.1 tsutsui /* 158 1.1 tsutsui * try to get the drive block size. 159 1.1 tsutsui */ 160 1.1 tsutsui capbuf[0] = 0; 161 1.1 tsutsui capbuf[1] = 0; 162 1.1 tsutsui 163 1.1 tsutsui stat = scsi_read_capacity((uint8_t *)capbuf, sizeof(capbuf)); 164 1.1 tsutsui 165 1.1 tsutsui if (stat == 0) { 166 1.1 tsutsui if (capbuf[1] > DEV_BSIZE) 167 1.1 tsutsui for (; capbuf[1] > DEV_BSIZE; capbuf[1] >>= 1) 168 1.1 tsutsui ++sc->sc_blkshift; 169 1.1 tsutsui } 170 1.1 tsutsui 171 1.1 tsutsui sc->sc_alive = 1; 172 1.1 tsutsui return 1; 173 1.1 tsutsui } 174 1.1 tsutsui 175 1.1 tsutsui int 176 1.1 tsutsui diskopen(struct open_file *f, ...) 177 1.1 tsutsui { 178 1.1 tsutsui struct disk_softc *sc; 179 1.1 tsutsui struct disklabel *lp; 180 1.1 tsutsui size_t cnt; 181 1.1 tsutsui char *msg, buf[DEV_BSIZE]; 182 1.1 tsutsui int error; 183 1.1 tsutsui 184 1.1 tsutsui cnt = 0; 185 1.1 tsutsui 186 1.1 tsutsui sc = alloc(sizeof(struct disk_softc)); 187 1.1 tsutsui memset(sc, 0, sizeof(struct disk_softc)); 188 1.1 tsutsui f->f_devdata = (void *)sc; 189 1.1 tsutsui 190 1.1 tsutsui sc->sc_part = scsi_part; 191 1.1 tsutsui 192 1.1 tsutsui if (sc->sc_alive == 0) { 193 1.1 tsutsui if (diskinit(sc) == 0) 194 1.1 tsutsui return ENXIO; 195 1.1 tsutsui } 196 1.1 tsutsui 197 1.1 tsutsui /* try to read disk label and partition table information */ 198 1.1 tsutsui lp = &sc->sc_label; 199 1.1 tsutsui lp->d_secsize = DEV_BSIZE; 200 1.1 tsutsui lp->d_secpercyl = 1; 201 1.1 tsutsui lp->d_npartitions = MAXPARTITIONS; 202 1.1 tsutsui lp->d_partitions[scsi_part].p_offset = 0; 203 1.1 tsutsui lp->d_partitions[scsi_part].p_size = 0x7fffffff; 204 1.1 tsutsui 205 1.1 tsutsui error = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, 206 1.1 tsutsui &cnt); 207 1.1 tsutsui 208 1.1 tsutsui if (error || cnt != DEV_BSIZE) { 209 1.1 tsutsui printf("diskstrategy error...\n"); 210 1.1 tsutsui dealloc(sc, sizeof(struct disk_softc)); 211 1.1 tsutsui return ENXIO; 212 1.1 tsutsui } 213 1.1 tsutsui 214 1.1 tsutsui msg = getdisklabel(buf, lp); 215 1.1 tsutsui 216 1.1 tsutsui if (msg) { 217 1.1 tsutsui /* If no label, just assume 0 and return */ 218 1.1 tsutsui printf("No disklabel...\n"); 219 1.1 tsutsui reboot(); 220 1.1 tsutsui } 221 1.1 tsutsui 222 1.1 tsutsui return 0; 223 1.1 tsutsui } 224 1.1 tsutsui 225 1.1 tsutsui int 226 1.1 tsutsui diskclose(struct open_file *f) 227 1.1 tsutsui { 228 1.1 tsutsui struct disk_softc *sc = f->f_devdata; 229 1.1 tsutsui 230 1.1 tsutsui dealloc(sc, sizeof *sc); 231 1.1 tsutsui f->f_devdata = NULL; 232 1.1 tsutsui 233 1.1 tsutsui return 0; 234 1.1 tsutsui } 235