1*4fbed232Sdholland /* $NetBSD: disk.c,v 1.12 2016/02/14 18:09:51 dholland Exp $ */
24e63f44fSthorpej
34e63f44fSthorpej /*
44e63f44fSthorpej * Copyright (c) 1992, 1993
54e63f44fSthorpej * The Regents of the University of California. All rights reserved.
64e63f44fSthorpej *
74e63f44fSthorpej * This code is derived from software contributed to Berkeley by
84e63f44fSthorpej * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
94e63f44fSthorpej *
104e63f44fSthorpej * Redistribution and use in source and binary forms, with or without
114e63f44fSthorpej * modification, are permitted provided that the following conditions
124e63f44fSthorpej * are met:
134e63f44fSthorpej * 1. Redistributions of source code must retain the above copyright
144e63f44fSthorpej * notice, this list of conditions and the following disclaimer.
154e63f44fSthorpej * 2. Redistributions in binary form must reproduce the above copyright
164e63f44fSthorpej * notice, this list of conditions and the following disclaimer in the
174e63f44fSthorpej * documentation and/or other materials provided with the distribution.
18aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
194e63f44fSthorpej * may be used to endorse or promote products derived from this software
204e63f44fSthorpej * without specific prior written permission.
214e63f44fSthorpej *
224e63f44fSthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234e63f44fSthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244e63f44fSthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254e63f44fSthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264e63f44fSthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274e63f44fSthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284e63f44fSthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294e63f44fSthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304e63f44fSthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314e63f44fSthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324e63f44fSthorpej * SUCH DAMAGE.
334e63f44fSthorpej *
344e63f44fSthorpej * @(#)disk.c 8.1 (Berkeley) 6/10/93
354e63f44fSthorpej */
364e63f44fSthorpej
374e63f44fSthorpej #include <lib/libsa/stand.h>
384e63f44fSthorpej
394e63f44fSthorpej #include <sys/param.h>
404e63f44fSthorpej #include <sys/disklabel.h>
414e63f44fSthorpej
424e63f44fSthorpej #include <dev/arcbios/arcbios.h>
434e63f44fSthorpej
444e63f44fSthorpej #include "common.h"
454e63f44fSthorpej #include "disk.h"
464e63f44fSthorpej
474e63f44fSthorpej #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
484e63f44fSthorpej
494e63f44fSthorpej struct disk_softc {
50fa657c9cStsutsui u_long sc_fd; /* ARCBIOS file id */
514e63f44fSthorpej int sc_part; /* disk partition number */
524e63f44fSthorpej struct disklabel sc_label; /* disk label for this disk */
534e63f44fSthorpej };
544e63f44fSthorpej
554e63f44fSthorpej int
diskstrategy(void * devdata,int rw,daddr_t bn,size_t reqcnt,void * addr,size_t * cnt)56fa657c9cStsutsui diskstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr,
57fa657c9cStsutsui size_t *cnt)
584e63f44fSthorpej {
594e63f44fSthorpej struct disk_softc *sc = (struct disk_softc *)devdata;
604e63f44fSthorpej int part = sc->sc_part;
614e63f44fSthorpej struct partition *pp = &sc->sc_label.d_partitions[part];
62fa657c9cStsutsui long error;
634e63f44fSthorpej int64_t offset;
64d0112ff3Stsutsui u_long count;
654e63f44fSthorpej
664e63f44fSthorpej offset = bn;
674e63f44fSthorpej
684e63f44fSthorpej /*
694e63f44fSthorpej * Partial-block transfers not handled.
704e63f44fSthorpej */
714e63f44fSthorpej if (reqcnt & (DEV_BSIZE - 1)) {
724e63f44fSthorpej *cnt = 0;
73fa657c9cStsutsui return EINVAL;
744e63f44fSthorpej }
754e63f44fSthorpej
764e63f44fSthorpej offset += pp->p_offset;
774e63f44fSthorpej
784e63f44fSthorpej if (pp->p_fstype == FS_RAID)
794e63f44fSthorpej offset += RF_PROTECTED_SECTORS;
804e63f44fSthorpej
814e63f44fSthorpej /*
824e63f44fSthorpej * Convert from blocks to bytes.
834e63f44fSthorpej */
844e63f44fSthorpej offset *= DEV_BSIZE;
854e63f44fSthorpej
8623347d39Smatt error = arcbios_Seek(sc->sc_fd, &offset, 0);
87fa657c9cStsutsui if (error != ARCBIOS_ESUCCESS)
88fa657c9cStsutsui return EIO;
8923347d39Smatt error = arcbios_Read(sc->sc_fd, addr, reqcnt, &count);
90fa657c9cStsutsui if (error != ARCBIOS_ESUCCESS)
91fa657c9cStsutsui return EIO;
924e63f44fSthorpej
934e63f44fSthorpej *cnt = count;
94fa657c9cStsutsui return 0;
954e63f44fSthorpej }
964e63f44fSthorpej
974e63f44fSthorpej int
diskopen(struct open_file * f,...)984e63f44fSthorpej diskopen(struct open_file *f, ...)
994e63f44fSthorpej {
100fdf5374eSsekiya int part;
1014e63f44fSthorpej
1024e63f44fSthorpej struct disk_softc *sc;
1034e63f44fSthorpej struct disklabel *lp;
1043975ceceSthorpej #ifdef arc
1053975ceceSthorpej char *msg, buf[DEV_BSIZE];
106fa657c9cStsutsui size_t cnt;
107fa657c9cStsutsui int mbrp_off, i;
1083975ceceSthorpej #endif
109fa657c9cStsutsui int error;
110d0112ff3Stsutsui u_long fd;
1114e63f44fSthorpej char *device;
1124e63f44fSthorpej va_list ap;
1134e63f44fSthorpej
1144e63f44fSthorpej va_start(ap, f);
1154e63f44fSthorpej device = va_arg(ap, char *);
116*4fbed232Sdholland va_end(ap);
1173975ceceSthorpej
1183975ceceSthorpej /*
1193975ceceSthorpej * For NetBSD/sgimips, since we use the SGI partition map directly,
1203975ceceSthorpej * we fake an in-core NetBSD disklabel with offset of 0.
1213975ceceSthorpej *
1223975ceceSthorpej * For NetBSD/arc, there is a MBR partition map on the disk, which we
1233975ceceSthorpej * then expect to find a NetBSD disklabel within the MBR partition.
1243975ceceSthorpej * We require that the kernel be located in first partition in the
1253975ceceSthorpej * NetBSD disklabel, because we have not other way to represent the
1263975ceceSthorpej * root partition.
1273975ceceSthorpej */
1284e63f44fSthorpej part = 0;
1293975ceceSthorpej
130fa657c9cStsutsui if (part >= MAXPARTITIONS)
131fa657c9cStsutsui return ENXIO;
1324e63f44fSthorpej
13323347d39Smatt error = arcbios_Open(device, 0, &fd);
134fa657c9cStsutsui if (error) {
135fa657c9cStsutsui printf("diskopen: open failed, errno = %d\n", error);
136fa657c9cStsutsui return ENXIO;
1374e63f44fSthorpej }
1384e63f44fSthorpej
1394e63f44fSthorpej sc = alloc(sizeof(struct disk_softc));
1404e63f44fSthorpej memset(sc, 0, sizeof(struct disk_softc));
1414e63f44fSthorpej f->f_devdata = (void *)sc;
1424e63f44fSthorpej
143d0112ff3Stsutsui sc->sc_fd = fd;
1444e63f44fSthorpej sc->sc_part = part;
1454e63f44fSthorpej
1464e63f44fSthorpej /* try to read disk label and partition table information */
1474e63f44fSthorpej lp = &sc->sc_label;
1484e63f44fSthorpej lp->d_secsize = DEV_BSIZE;
1494e63f44fSthorpej lp->d_secpercyl = 1;
1504e63f44fSthorpej lp->d_npartitions = MAXPARTITIONS;
1514e63f44fSthorpej lp->d_partitions[part].p_offset = 0;
1524e63f44fSthorpej lp->d_partitions[part].p_size = 0x7fffffff;
1534e63f44fSthorpej
1543975ceceSthorpej #ifdef arc
155fa657c9cStsutsui error = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
1563975ceceSthorpej buf, &cnt);
157fa657c9cStsutsui if (error || cnt != DEV_BSIZE) {
158fa657c9cStsutsui printf("%s: can't read disklabel, errno = %d\n",
159fa657c9cStsutsui device, error);
160606bb2caSchristos dealloc(sc, sizeof(struct disk_softc));
161fa657c9cStsutsui return ENXIO;
1624e63f44fSthorpej }
1634e63f44fSthorpej msg = getdisklabel(buf, lp);
1644e63f44fSthorpej if (msg) {
1654e63f44fSthorpej /* If no label, just assume 0 and return */
166fa657c9cStsutsui return 0;
167fa657c9cStsutsui }
168fa657c9cStsutsui
169fa657c9cStsutsui /*
170fa657c9cStsutsui * On arc, we can't open whole disk, but can open each partition with
171fa657c9cStsutsui * OSLOADPARTITION like scsi(0)disk(0)rdisk()partition(1) etc.
172fa657c9cStsutsui * Thus, we don't have to add offset of the MBR partition.
173fa657c9cStsutsui */
174fa657c9cStsutsui /* XXX magic: partition 2 is whole NetBSD partition */
175fa657c9cStsutsui mbrp_off = lp->d_partitions[2].p_offset;
176fa657c9cStsutsui for (i = 0; i < MAXPARTITIONS; i++) {
177fa657c9cStsutsui if (lp->d_partitions[i].p_fstype != FS_UNUSED &&
178fa657c9cStsutsui lp->d_partitions[i].p_offset >= mbrp_off)
179fa657c9cStsutsui lp->d_partitions[i].p_offset -= mbrp_off;
180fa657c9cStsutsui }
181fa657c9cStsutsui
182fa657c9cStsutsui if (part >= lp->d_npartitions ||
183fa657c9cStsutsui lp->d_partitions[part].p_fstype == FS_UNUSED ||
184fa657c9cStsutsui lp->d_partitions[part].p_size == 0) {
185606bb2caSchristos dealloc(sc, sizeof(struct disk_softc));
186fa657c9cStsutsui return ENXIO;
1874e63f44fSthorpej }
1883975ceceSthorpej #endif
189fa657c9cStsutsui return 0;
1904e63f44fSthorpej }
1914e63f44fSthorpej
1924e63f44fSthorpej #ifndef LIBSA_NO_DEV_CLOSE
1934e63f44fSthorpej int
diskclose(struct open_file * f)194fa657c9cStsutsui diskclose(struct open_file *f)
1954e63f44fSthorpej {
196fa657c9cStsutsui
19723347d39Smatt arcbios_Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
198606bb2caSchristos dealloc(f->f_devdata, sizeof(struct disk_softc));
199fa657c9cStsutsui f->f_devdata = NULL;
200fa657c9cStsutsui return 0;
2014e63f44fSthorpej }
2024e63f44fSthorpej #endif
203