bugdev.c revision 1.11 1 1.11 tsutsui /* $NetBSD: bugdev.c,v 1.11 2008/01/12 09:54:31 tsutsui Exp $ */
2 1.1 chuck
3 1.5 pk /*-
4 1.5 pk * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 chuck * All rights reserved.
6 1.1 chuck *
7 1.5 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.5 pk * by Paul Kranenburg.
9 1.5 pk *
10 1.1 chuck * Redistribution and use in source and binary forms, with or without
11 1.1 chuck * modification, are permitted provided that the following conditions
12 1.1 chuck * are met:
13 1.1 chuck * 1. Redistributions of source code must retain the above copyright
14 1.1 chuck * notice, this list of conditions and the following disclaimer.
15 1.1 chuck * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 chuck * notice, this list of conditions and the following disclaimer in the
17 1.1 chuck * documentation and/or other materials provided with the distribution.
18 1.1 chuck * 3. All advertising materials mentioning features or use of this software
19 1.1 chuck * must display the following acknowledgement:
20 1.5 pk * This product includes software developed by the NetBSD
21 1.5 pk * Foundation, Inc. and its contributors.
22 1.5 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.5 pk * contributors may be used to endorse or promote products derived
24 1.5 pk * from this software without specific prior written permission.
25 1.1 chuck *
26 1.5 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.5 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.5 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.5 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.5 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.5 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.5 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.5 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.5 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.5 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.5 pk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 chuck */
38 1.1 chuck
39 1.1 chuck #include <sys/param.h>
40 1.1 chuck #include <sys/disklabel.h>
41 1.1 chuck #include <machine/prom.h>
42 1.1 chuck
43 1.8 junyoung #include <lib/libsa/stand.h>
44 1.1 chuck #include "libsa.h"
45 1.1 chuck
46 1.7 junyoung void cputobsdlabel(struct disklabel *lp, struct cpu_disklabel *clp);
47 1.1 chuck
48 1.1 chuck int errno;
49 1.1 chuck
50 1.1 chuck struct bugsc_softc {
51 1.1 chuck int fd; /* Prom file descriptor */
52 1.1 chuck int poff; /* Partition offset */
53 1.1 chuck int psize; /* Partition size */
54 1.1 chuck short ctrl;
55 1.1 chuck short dev;
56 1.1 chuck } bugsc_softc[1];
57 1.1 chuck
58 1.1 chuck int
59 1.7 junyoung devopen(struct open_file *f, const char *fname, char **file)
60 1.1 chuck {
61 1.4 scw struct bugsc_softc *pp = &bugsc_softc[0];
62 1.4 scw int error, pn = 0;
63 1.1 chuck char *dev, *cp;
64 1.4 scw size_t nrd;
65 1.4 scw static int iobuf[DEV_BSIZE / sizeof(int)];
66 1.1 chuck struct disklabel sdlabel;
67 1.1 chuck
68 1.1 chuck dev = bugargs.arg_start;
69 1.1 chuck
70 1.1 chuck /*
71 1.1 chuck * Extract partition # from boot device string.
72 1.2 scw * The Bug command line format of this is:
73 1.2 scw *
74 1.2 scw * 147-Bug> bo [drive],,[<d>:][kernel_name] [options]
75 1.2 scw *
76 1.2 scw * Where:
77 1.2 scw * [drive] The bug LUN number, eg. 0
78 1.2 scw * [<d>:] <d> is partition # ('a' to 'h')
79 1.2 scw * [kernel_name] Eg. netbsd or /netbsd
80 1.2 scw * [options] Eg. -s
81 1.2 scw *
82 1.2 scw * At this time, all we need do is scan for a ':', and assume the
83 1.2 scw * preceding letter is a partition id.
84 1.1 chuck */
85 1.2 scw for (cp = dev + 1; *cp && cp <= bugargs.arg_end; cp++) {
86 1.2 scw if ( *cp == ':' ) {
87 1.2 scw pn = *(cp - 1) - 'a';
88 1.2 scw break;
89 1.2 scw }
90 1.2 scw }
91 1.2 scw
92 1.2 scw if ( pn < 0 || pn >= MAXPARTITIONS ) {
93 1.2 scw printf("Invalid partition number; defaulting to 'a'\n");
94 1.2 scw pn = 0;
95 1.1 chuck }
96 1.1 chuck
97 1.1 chuck pp->fd = bugscopen(f);
98 1.1 chuck
99 1.1 chuck if (pp->fd < 0) {
100 1.1 chuck printf("Can't open device `%s'\n", dev);
101 1.7 junyoung return ENXIO;
102 1.1 chuck }
103 1.4 scw error = bugscstrategy(pp, F_READ, LABELSECTOR, DEV_BSIZE, iobuf, &nrd);
104 1.1 chuck if (error)
105 1.7 junyoung return error;
106 1.4 scw if (nrd != DEV_BSIZE)
107 1.7 junyoung return EINVAL;
108 1.1 chuck
109 1.4 scw /*LINTED*/
110 1.4 scw cputobsdlabel(&sdlabel, (struct cpu_disklabel *)&(iobuf[0]));
111 1.1 chuck pp->poff = sdlabel.d_partitions[pn].p_offset;
112 1.1 chuck pp->psize = sdlabel.d_partitions[pn].p_size;
113 1.1 chuck
114 1.1 chuck f->f_dev = devsw;
115 1.1 chuck f->f_devdata = (void *)pp;
116 1.4 scw /*LINTED*/
117 1.1 chuck *file = (char *)fname;
118 1.7 junyoung return 0;
119 1.1 chuck }
120 1.1 chuck
121 1.1 chuck /* silly block scale factor */
122 1.1 chuck #define BUG_BLOCK_SIZE 256
123 1.1 chuck #define BUG_SCALE (512/BUG_BLOCK_SIZE)
124 1.4 scw /*ARGSUSED*/
125 1.1 chuck int
126 1.7 junyoung bugscstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *buf,
127 1.7 junyoung size_t *rsize)
128 1.1 chuck {
129 1.1 chuck struct mvmeprom_dskio dio;
130 1.4 scw struct bugsc_softc *pp = (struct bugsc_softc *)devdata;
131 1.1 chuck daddr_t blk = dblk + pp->poff;
132 1.1 chuck
133 1.1 chuck twiddle();
134 1.1 chuck
135 1.1 chuck dio.ctrl_lun = pp->ctrl;
136 1.1 chuck dio.dev_lun = pp->dev;
137 1.1 chuck dio.status = 0;
138 1.1 chuck dio.pbuffer = buf;
139 1.1 chuck dio.blk_num = blk * BUG_SCALE;
140 1.1 chuck dio.blk_cnt = size / BUG_BLOCK_SIZE; /* assumed size in bytes */
141 1.1 chuck dio.flag = 0;
142 1.1 chuck dio.addr_mod = 0;
143 1.1 chuck #ifdef DEBUG
144 1.1 chuck printf("bugscstrategy: size=%d blk=%d buf=%x\n", size, blk, buf);
145 1.1 chuck printf("ctrl %d dev %d\n", dio.ctrl_lun, dio.dev_lun);
146 1.1 chuck #endif
147 1.1 chuck mvmeprom_diskrd(&dio);
148 1.1 chuck
149 1.1 chuck *rsize = dio.blk_cnt * BUG_BLOCK_SIZE;
150 1.1 chuck #ifdef DEBUG
151 1.11 tsutsui printf("rsize %d status %x\n", *rsize, dio.status);
152 1.1 chuck #endif
153 1.1 chuck
154 1.1 chuck if (dio.status)
155 1.7 junyoung return EIO;
156 1.7 junyoung return 0;
157 1.1 chuck }
158 1.1 chuck
159 1.1 chuck int
160 1.9 he bugscopen(struct open_file *f, ...)
161 1.1 chuck {
162 1.11 tsutsui
163 1.1 chuck #ifdef DEBUG
164 1.1 chuck printf("bugscopen:\n");
165 1.1 chuck #endif
166 1.1 chuck
167 1.1 chuck f->f_devdata = (void *)bugsc_softc;
168 1.1 chuck bugsc_softc[0].ctrl = (short)bugargs.ctrl_lun;
169 1.1 chuck bugsc_softc[0].dev = (short)bugargs.dev_lun;
170 1.1 chuck #ifdef DEBUG
171 1.1 chuck printf("using mvmebug ctrl %d dev %d\n",
172 1.1 chuck bugsc_softc[0].ctrl, bugsc_softc[0].dev);
173 1.1 chuck #endif
174 1.7 junyoung return 0;
175 1.1 chuck }
176 1.1 chuck
177 1.4 scw /*ARGSUSED*/
178 1.1 chuck int
179 1.7 junyoung bugscclose(struct open_file *f)
180 1.1 chuck {
181 1.11 tsutsui
182 1.7 junyoung return EIO;
183 1.1 chuck }
184 1.1 chuck
185 1.4 scw /*ARGSUSED*/
186 1.1 chuck int
187 1.7 junyoung bugscioctl(struct open_file *f, u_long cmd, void *data)
188 1.1 chuck {
189 1.11 tsutsui
190 1.7 junyoung return EIO;
191 1.1 chuck }
192 1.1 chuck
193 1.1 chuck void
194 1.7 junyoung cputobsdlabel(struct disklabel *lp, struct cpu_disklabel *clp)
195 1.1 chuck {
196 1.1 chuck int i;
197 1.1 chuck
198 1.11 tsutsui lp->d_magic = (uint32_t)clp->magic1;
199 1.11 tsutsui lp->d_type = (uint16_t)clp->type;
200 1.11 tsutsui lp->d_subtype = (uint16_t)clp->subtype;
201 1.4 scw
202 1.6 scw memcpy(lp->d_typename, clp->vid_vd, 16);
203 1.6 scw memcpy(lp->d_packname, clp->packname, 16);
204 1.4 scw
205 1.11 tsutsui lp->d_secsize = (uint32_t)clp->cfg_psm;
206 1.11 tsutsui lp->d_nsectors = (uint32_t)clp->cfg_spt;
207 1.11 tsutsui lp->d_ncylinders = (uint32_t)clp->cfg_trk; /* trk is num of cyl! */
208 1.11 tsutsui lp->d_ntracks = (uint32_t)clp->cfg_hds;
209 1.11 tsutsui lp->d_secpercyl = (uint32_t)clp->secpercyl;
210 1.11 tsutsui lp->d_secperunit = (uint32_t)clp->secperunit;
211 1.11 tsutsui lp->d_sparespertrack = (uint16_t)clp->sparespertrack;
212 1.11 tsutsui lp->d_sparespercyl = (uint16_t)clp->sparespercyl;
213 1.11 tsutsui lp->d_acylinders = (uint32_t)clp->acylinders;
214 1.11 tsutsui lp->d_rpm = (uint16_t)clp->rpm;
215 1.11 tsutsui lp->d_interleave = (uint16_t)clp->cfg_ilv;
216 1.11 tsutsui lp->d_trackskew = (uint16_t)clp->cfg_sof;
217 1.11 tsutsui lp->d_cylskew = (uint16_t)clp->cylskew;
218 1.11 tsutsui lp->d_headswitch = (uint32_t)clp->headswitch;
219 1.1 chuck
220 1.1 chuck /* this silly table is for winchester drives */
221 1.1 chuck switch (clp->cfg_ssr) {
222 1.1 chuck case 0:
223 1.1 chuck lp->d_trkseek = 0;
224 1.1 chuck break;
225 1.1 chuck case 1:
226 1.1 chuck lp->d_trkseek = 6;
227 1.1 chuck break;
228 1.1 chuck case 2:
229 1.1 chuck lp->d_trkseek = 10;
230 1.1 chuck break;
231 1.1 chuck case 3:
232 1.1 chuck lp->d_trkseek = 15;
233 1.1 chuck break;
234 1.1 chuck case 4:
235 1.1 chuck lp->d_trkseek = 20;
236 1.1 chuck break;
237 1.1 chuck default:
238 1.1 chuck lp->d_trkseek = 0;
239 1.1 chuck break;
240 1.1 chuck }
241 1.11 tsutsui lp->d_flags = (uint32_t)clp->flags;
242 1.4 scw
243 1.1 chuck for (i = 0; i < NDDATA; i++)
244 1.11 tsutsui lp->d_drivedata[i] = (uint32_t)clp->drivedata[i];
245 1.4 scw
246 1.1 chuck for (i = 0; i < NSPARE; i++)
247 1.11 tsutsui lp->d_spare[i] = (uint32_t)clp->spare[i];
248 1.4 scw
249 1.11 tsutsui lp->d_magic2 = (uint32_t)clp->magic2;
250 1.11 tsutsui lp->d_checksum = (uint16_t)clp->checksum;
251 1.11 tsutsui lp->d_npartitions = (uint16_t)clp->partitions;
252 1.11 tsutsui lp->d_bbsize = (uint32_t)clp->bbsize;
253 1.11 tsutsui lp->d_sbsize = (uint32_t)clp->sbsize;
254 1.4 scw
255 1.6 scw memcpy(&(lp->d_partitions[0]), clp->vid_4,
256 1.7 junyoung sizeof(struct partition) * 4);
257 1.4 scw
258 1.4 scw /* CONSTCOND */
259 1.7 junyoung memcpy(&(lp->d_partitions[4]), clp->cfg_4, sizeof(struct partition)
260 1.11 tsutsui * ((MAXPARTITIONS < 16) ? (MAXPARTITIONS - 4) : 12));
261 1.1 chuck }
262