xref: /netbsd-src/sys/arch/sgimips/stand/common/disk.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: disk.c,v 1.10 2011/02/20 07:59:52 matt 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)disk.c	8.1 (Berkeley) 6/10/93
35  */
36 
37 #include <lib/libsa/stand.h>
38 #include <machine/stdarg.h>
39 
40 #include <sys/param.h>
41 #include <sys/disklabel.h>
42 
43 #include <dev/arcbios/arcbios.h>
44 
45 #include "common.h"
46 #include "disk.h"
47 
48 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
49 
50 struct	disk_softc {
51 	u_long	sc_fd;			/* ARCBIOS file id */
52 	int	sc_part;		/* disk partition number */
53 	struct	disklabel sc_label;	/* disk label for this disk */
54 };
55 
56 int
57 diskstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr,
58     size_t *cnt)
59 {
60 	struct disk_softc *sc = (struct disk_softc *)devdata;
61 	int part = sc->sc_part;
62 	struct partition *pp = &sc->sc_label.d_partitions[part];
63 	long error;
64 	int64_t offset;
65 	u_long count;
66 
67 	offset = bn;
68 
69 	/*
70 	 * Partial-block transfers not handled.
71 	 */
72 	if (reqcnt & (DEV_BSIZE - 1)) {
73 		*cnt = 0;
74 		return EINVAL;
75 	}
76 
77 	offset += pp->p_offset;
78 
79 	if (pp->p_fstype == FS_RAID)
80 		offset += RF_PROTECTED_SECTORS;
81 
82 	/*
83 	 * Convert from blocks to bytes.
84 	 */
85 	offset *= DEV_BSIZE;
86 
87 	error = arcbios_Seek(sc->sc_fd, &offset, 0);
88 	if (error != ARCBIOS_ESUCCESS)
89 		return EIO;
90 	error = arcbios_Read(sc->sc_fd, addr, reqcnt, &count);
91 	if (error != ARCBIOS_ESUCCESS)
92 		return EIO;
93 
94 	*cnt = count;
95 	return 0;
96 }
97 
98 int
99 diskopen(struct open_file *f, ...)
100 {
101 	int part;
102 
103 	struct disk_softc *sc;
104 	struct disklabel *lp;
105 #ifdef arc
106 	char *msg, buf[DEV_BSIZE];
107 	size_t cnt;
108 	int mbrp_off, i;
109 #endif
110 	int error;
111 	u_long fd;
112 	char *device;
113 	va_list ap;
114 
115 	va_start(ap, f);
116 
117 	device = va_arg(ap, char *);
118 
119 	/*
120 	 * For NetBSD/sgimips, since we use the SGI partition map directly,
121 	 * we fake an in-core NetBSD disklabel with offset of 0.
122 	 *
123 	 * For NetBSD/arc, there is a MBR partition map on the disk, which we
124 	 * then expect to find a NetBSD disklabel within the MBR partition.
125 	 * We require that the kernel be located in first partition in the
126 	 * NetBSD disklabel, because we have not other way to represent the
127 	 * root partition.
128 	 */
129 	part = 0;
130 
131 	if (part >= MAXPARTITIONS)
132 		return ENXIO;
133 
134 	error = arcbios_Open(device, 0, &fd);
135 	if (error) {
136 		printf("diskopen: open failed, errno = %d\n", error);
137 		return ENXIO;
138 	}
139 
140 	sc = alloc(sizeof(struct disk_softc));
141 	memset(sc, 0, sizeof(struct disk_softc));
142 	f->f_devdata = (void *)sc;
143 
144 	sc->sc_fd = fd;
145 	sc->sc_part = part;
146 
147 	/* try to read disk label and partition table information */
148 	lp = &sc->sc_label;
149 	lp->d_secsize = DEV_BSIZE;
150 	lp->d_secpercyl = 1;
151 	lp->d_npartitions = MAXPARTITIONS;
152 	lp->d_partitions[part].p_offset = 0;
153 	lp->d_partitions[part].p_size = 0x7fffffff;
154 
155 #ifdef arc
156 	error = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
157 	    buf, &cnt);
158 	if (error || cnt != DEV_BSIZE) {
159 		printf("%s: can't read disklabel, errno = %d\n",
160 		    device, error);
161 		dealloc(sc, sizeof(struct disk_softc));
162 		return ENXIO;
163 	}
164 	msg = getdisklabel(buf, lp);
165 	if (msg) {
166 		/* If no label, just assume 0 and return */
167 		return 0;
168 	}
169 
170 	/*
171 	 * On arc, we can't open whole disk, but can open each partition with
172 	 * OSLOADPARTITION like scsi(0)disk(0)rdisk()partition(1) etc.
173 	 * Thus, we don't have to add offset of the MBR partition.
174 	 */
175 	/* XXX magic: partition 2 is whole NetBSD partition */
176 	mbrp_off = lp->d_partitions[2].p_offset;
177 	for (i = 0; i < MAXPARTITIONS; i++) {
178 		if (lp->d_partitions[i].p_fstype != FS_UNUSED &&
179 		    lp->d_partitions[i].p_offset >= mbrp_off)
180 			lp->d_partitions[i].p_offset -= mbrp_off;
181 	}
182 
183 	if (part >= lp->d_npartitions ||
184 	    lp->d_partitions[part].p_fstype == FS_UNUSED ||
185 	    lp->d_partitions[part].p_size == 0) {
186 		dealloc(sc, sizeof(struct disk_softc));
187 		return ENXIO;
188 	}
189 #endif
190 	return 0;
191 }
192 
193 #ifndef LIBSA_NO_DEV_CLOSE
194 int
195 diskclose(struct open_file *f)
196 {
197 
198 	arcbios_Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
199 	dealloc(f->f_devdata, sizeof(struct disk_softc));
200 	f->f_devdata = NULL;
201 	return 0;
202 }
203 #endif
204