xref: /netbsd/sys/arch/sgimips/stand/common/disk.c (revision bf9ec67e)
1 /*	$NetBSD: disk.c,v 1.2 2001/11/21 23:33:19 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)disk.c	8.1 (Berkeley) 6/10/93
39  */
40 
41 #include <lib/libsa/stand.h>
42 #include <machine/stdarg.h>
43 
44 #include <sys/param.h>
45 #include <sys/disklabel.h>
46 
47 #include <dev/arcbios/arcbios.h>
48 
49 #include "common.h"
50 #include "disk.h"
51 
52 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
53 
54 extern const struct arcbios_fv *ARCBIOS;
55 
56 struct	disk_softc {
57 	int	sc_fd;			/* PROM file id */
58 	int	sc_part;		/* disk partition number */
59 	struct	disklabel sc_label;	/* disk label for this disk */
60 };
61 
62 int
63 diskstrategy(devdata, rw, bn, reqcnt, addr, cnt)
64 	void *devdata;
65 	int rw;
66 	daddr_t bn;
67 	size_t reqcnt;
68 	void *addr;
69 	size_t *cnt;	/* out: number of bytes transfered */
70 {
71 	struct disk_softc *sc = (struct disk_softc *)devdata;
72 	int part = sc->sc_part;
73 	struct partition *pp = &sc->sc_label.d_partitions[part];
74 	int s;
75 	int64_t offset;
76 	int count;
77 
78 	offset = bn;
79 
80 	/*
81 	 * Partial-block transfers not handled.
82 	 */
83 	if (reqcnt & (DEV_BSIZE - 1)) {
84 		*cnt = 0;
85 		return (EINVAL);
86 	}
87 
88 	offset += pp->p_offset;
89 
90 	if (pp->p_fstype == FS_RAID)
91 		offset += RF_PROTECTED_SECTORS;
92 
93 	/*
94 	 * Convert from blocks to bytes.
95 	 */
96 	offset *= DEV_BSIZE;
97 
98 	s = ARCBIOS->Seek(sc->sc_fd, &offset, 0);
99 	s = ARCBIOS->Read(sc->sc_fd, addr, reqcnt, &count);
100 
101 	if (s < 0)
102 		return (EIO);
103 
104 	*cnt = count;
105 	return (0);
106 }
107 
108 int
109 diskopen(struct open_file *f, ...)
110 {
111 	int ctlr, unit, part;
112 
113 	struct disk_softc *sc;
114 	struct disklabel *lp;
115 #ifdef arc
116 	char *msg, buf[DEV_BSIZE];
117 #endif
118 	int i, cnt;
119 	char *device;
120 	va_list ap;
121 
122 	va_start(ap, f);
123 
124 	device = va_arg(ap, char *);
125 
126 	/*
127 	 * For NetBSD/sgimips, since we use the SGI partition map directly,
128 	 * we fake an in-core NetBSD disklabel with offset of 0.
129 	 *
130 	 * For NetBSD/arc, there is a MBR partition map on the disk, which we
131 	 * then expect to find a NetBSD disklabel within the MBR partition.
132 	 * We require that the kernel be located in first partition in the
133 	 * NetBSD disklabel, because we have not other way to represent the
134 	 * root partition.
135 	 */
136 	part = 0;
137 
138 	if (part >= 16)
139 		return (ENXIO);
140 
141 	if (ARCBIOS->Open(device, 0, &i)) {
142 		printf("open failed\n");
143 		return (ENXIO);
144 	}
145 
146 	sc = alloc(sizeof(struct disk_softc));
147 	memset(sc, 0, sizeof(struct disk_softc));
148 	f->f_devdata = (void *)sc;
149 
150 	sc->sc_fd = i;
151 	sc->sc_part = part;
152 
153 	/* try to read disk label and partition table information */
154 	lp = &sc->sc_label;
155 	lp->d_secsize = DEV_BSIZE;
156 	lp->d_secpercyl = 1;
157 	lp->d_npartitions = MAXPARTITIONS;
158 	lp->d_partitions[part].p_offset = 0;
159 	lp->d_partitions[part].p_size = 0x7fffffff;
160 
161 #ifdef arc
162 	i = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
163 	    buf, &cnt);
164 	if (i || cnt != DEV_BSIZE) {
165 		printf("%s: error reading disk label\n", device);
166 		free(sc, sizeof(struct disk_softc));
167 		return (ENXIO);
168 	}
169 	msg = getdisklabel(buf, lp);
170 	if (msg) {
171 		/* If no label, just assume 0 and return */
172 		return (0);
173 	}
174 #endif
175 
176 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
177 		free(sc, sizeof(struct disk_softc));
178 		return (ENXIO);
179 	}
180 	return (0);
181 }
182 
183 #ifndef LIBSA_NO_DEV_CLOSE
184 int
185 diskclose(f)
186 	struct open_file *f;
187 {
188 	ARCBIOS->Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
189 	free(f->f_devdata, sizeof(struct disk_softc));
190 	f->f_devdata = (void *)0;
191 	return (0);
192 }
193 #endif
194