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