]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/ofw/libofw/ofw_disk.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / boot / ofw / libofw / ofw_disk.c
1 /*-
2  * Copyright (C) 2000 Benno Rice.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 /*
30  * Disk I/O routines using Open Firmware
31  */
32
33 #include <sys/param.h>
34 #include <sys/queue.h>
35
36 #include <netinet/in.h>
37
38 #include <machine/stdarg.h>
39
40 #include <stand.h>
41
42 #include "bootstrap.h"
43 #include "libofw.h"
44
45 static int      ofwd_init(void);
46 static int      ofwd_strategy(void *devdata, int flag, daddr_t dblk, 
47                                 size_t size, char *buf, size_t *rsize);
48 static int      ofwd_open(struct open_file *f, ...);
49 static int      ofwd_close(struct open_file *f);
50 static int      ofwd_ioctl(struct open_file *f, u_long cmd, void *data);
51 static void     ofwd_print(int verbose);
52
53 struct devsw ofwdisk = {
54         "block",
55         DEVT_DISK,
56         ofwd_init,
57         ofwd_strategy,
58         ofwd_open,
59         ofwd_close,
60         ofwd_ioctl,
61         ofwd_print
62 };
63
64 struct opened_dev {
65         ihandle_t               handle;
66         u_int                   count;
67         SLIST_ENTRY(opened_dev) link;
68 };
69
70 SLIST_HEAD(, opened_dev) opened_devs = SLIST_HEAD_INITIALIZER(opened_devs);
71
72 static int
73 ofwd_init(void)
74 {
75
76         return 0;
77 }
78
79 static int
80 ofwd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf,
81     size_t *rsize)
82 {
83         struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
84         daddr_t pos;
85         int n;
86
87         pos = dblk * 512;
88         do {
89                 if (OF_seek(dp->d_handle, pos) < 0)
90                         return EIO;
91                 n = OF_read(dp->d_handle, buf, size);
92                 if (n < 0 && n != -2)
93                         return EIO;
94         } while (n == -2);
95         *rsize = size;
96         return 0;
97 }
98
99 static int
100 ofwd_open(struct open_file *f, ...)
101 {
102         char path[256];
103         struct ofw_devdesc *dp;
104         struct opened_dev *odp;
105         va_list vl;
106
107         va_start(vl, f);
108         dp = va_arg(vl, struct ofw_devdesc *);
109         va_end(vl);
110         /*
111          * We're not guaranteed to be able to open a device more than once
112          * simultaneously and there is no OFW standard method to determine
113          * whether a device is already opened. Opening a device more than
114          * once happens to work with most OFW block device drivers but
115          * triggers a trap with at least the driver for the on-board SCSI
116          * controller in Sun Ultra 1. Upper layers and MI code expect to
117          * be able to open a device more than once however. As a workaround
118          * keep track of the opened devices and reuse the instance handle
119          * when asked to open an already opened device.
120          */
121         SLIST_FOREACH(odp, &opened_devs, link) {
122                 if (OF_instance_to_path(odp->handle, path, sizeof(path)) == -1)
123                         continue;
124                 if (strcmp(path, dp->d_path) == 0) {
125                         odp->count++;
126                         dp->d_handle = odp->handle;
127                         return 0;
128                 }
129         }
130         odp = malloc(sizeof(struct opened_dev));
131         if (odp == NULL) {
132                 printf("ofwd_open: malloc failed\n");
133                 return ENOMEM;
134         }
135         if ((odp->handle = OF_open(dp->d_path)) == -1) {
136                 printf("ofwd_open: Could not open %s\n", dp->d_path);
137                 free(odp);
138                 return ENOENT;
139         }
140         odp->count = 1;
141         SLIST_INSERT_HEAD(&opened_devs, odp, link);
142         dp->d_handle = odp->handle;
143         return 0;
144 }
145
146 static int
147 ofwd_close(struct open_file *f)
148 {
149         struct ofw_devdesc *dev = f->f_devdata;
150         struct opened_dev *odp;
151
152         SLIST_FOREACH(odp, &opened_devs, link) {
153                 if (odp->handle == dev->d_handle) {
154                         odp->count--;
155                         if (odp->count == 0) {
156                                 SLIST_REMOVE(&opened_devs, odp, opened_dev,
157                                     link);
158                         #if !defined(__powerpc__)
159                                 OF_close(odp->handle);
160                         #endif
161                                 free(odp);
162                         }
163                         break;
164                 }
165         }
166         return 0;
167 }
168
169 static int
170 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
171 {
172
173         return (EINVAL);
174 }
175
176 static void
177 ofwd_print(int verbose)
178 {
179
180 }