xref: /freebsd/sys/dev/ida/ida_disk.c (revision 03016f421b93072568df5ea924df8d3bb503566a)
1 /*-
2  * Copyright (c) 1999 Jonathan Lemon
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 THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$Id: ida_disk.c,v 1.1 1999/06/24 03:31:57 jlemon Exp $
27  */
28 
29 /*
30  * Disk driver for Compaq SMART RAID adapters.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 
38 #include <sys/buf.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/devicestat.h>
43 #include <sys/disklabel.h>
44 #include <sys/diskslice.h>
45 
46 #if NPCI > 0
47 #include <machine/bus_memio.h>
48 #endif
49 #include <machine/bus_pio.h>
50 #include <machine/bus.h>
51 #include <machine/clock.h>
52 #include <sys/rman.h>
53 
54 #include <dev/ida/idareg.h>
55 #include <dev/ida/idavar.h>
56 
57 /* prototypes */
58 static void id_drvinit(void);
59 static int idprobe(device_t dev);
60 static int idattach(device_t dev);
61 
62 static	d_open_t	idopen;
63 static	d_close_t	idclose;
64 static	d_strategy_t	idstrategy;
65 static	d_ioctl_t	idioctl;
66 static	d_psize_t	idsize;
67 
68 #define ID_BDEV_MAJOR	29
69 #define ID_CDEV_MAJOR	109
70 
71 #define WD_BDEV_MAJOR	0
72 #define WD_CDEV_MAJOR	3
73 
74 static struct cdevsw id_cdevsw = {
75 	/* open */	idopen,
76 	/* close */	idclose,
77 	/* read */	physread,
78 	/* write */	physwrite,
79 	/* ioctl */	idioctl,
80 	/* stop */	nostop,
81 	/* reset */	noreset,
82 	/* devtotty */	nodevtotty,
83 	/* poll */	nopoll,
84 	/* mmap */	nommap,
85 	/* strategy */	idstrategy,
86 	/* name */ 	"id",
87 	/* parms */	noparms,
88 	/* maj */	ID_CDEV_MAJOR,
89 	/* dump */	nodump,
90 	/* psize */ 	idsize,
91 	/* flags */	D_DISK,
92 	/* maxio */	0,
93 	/* bmaj */	ID_BDEV_MAJOR
94 };
95 static struct cdevsw stolen_cdevsw;
96 
97 static devclass_t	id_devclass;
98 
99 static device_method_t id_methods[] = {
100 	DEVMETHOD(device_probe,		idprobe),
101 	DEVMETHOD(device_attach,	idattach),
102 	{ 0, 0 }
103 };
104 
105 static driver_t id_driver = {
106 	"id",
107 	id_methods,
108 	sizeof(struct id_softc)
109 };
110 
111 static __inline struct id_softc *
112 idgetsoftc(dev_t dev)
113 {
114 	int unit;
115 
116 	unit = dkunit(dev);
117 	return ((struct id_softc *)devclass_get_softc(id_devclass, unit));
118 }
119 
120 static int
121 idopen(dev_t dev, int flags, int fmt, struct proc *p)
122 {
123 	struct id_softc *drv;
124 	struct disklabel label;
125 	int error;
126 
127 	drv = idgetsoftc(dev);
128 	if (drv == NULL)
129 		return (ENXIO);
130 
131 	/* XXX block against race where > 1 person is reading label? */
132 
133 	bzero(&label, sizeof(label));
134 	label.d_type = DTYPE_SCSI;	/* XXX should this be DTYPE_RAID? */
135 /*
136 	strncpy(label.d_typename, ...
137 	strncpy(label.d_packname, ...
138 */
139 	label.d_secsize = drv->secsize;
140 	label.d_nsectors = drv->sectors;
141 	label.d_ntracks = drv->heads;
142 	label.d_ncylinders = drv->cylinders;
143 	label.d_secpercyl = drv->sectors * drv->heads;
144 	label.d_secperunit = drv->secperunit;
145 
146 	/* Initialize slice tables. */
147 	error = dsopen("id", dev, fmt, 0, &drv->slices, &label,
148 	    idstrategy, (ds_setgeom_t *)NULL, &id_cdevsw);
149 
150 	return (error);
151 }
152 
153 static int
154 idclose(dev_t dev, int flags, int fmt, struct proc *p)
155 {
156 	struct id_softc *drv;
157 
158 	drv = idgetsoftc(dev);
159 	if (drv == NULL)
160 		return (ENXIO);
161 	dsclose(dev, fmt, drv->slices);
162 	return (0);
163 }
164 
165 static int
166 idioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
167 {
168 	struct id_softc *drv;
169 	int error;
170 
171 	drv = idgetsoftc(dev);
172 	if (drv == NULL)
173 		return (ENXIO);
174 
175 	error = dsioctl("id", dev, cmd, addr, flag, &drv->slices,
176 	    idstrategy, (ds_setgeom_t *)NULL);
177 
178 	if (error == ENOIOCTL)
179 		return (ENOTTY);
180 
181 	return (error);
182 }
183 
184 static int
185 idsize(dev_t dev)
186 {
187 	struct id_softc *drv;
188 
189 	drv = idgetsoftc(dev);
190 	if (drv == NULL)
191 		return (ENXIO);
192 	return (dssize(dev, &drv->slices, idopen, idclose));
193 }
194 
195 /*
196  * Read/write routine for a buffer.  Finds the proper unit, range checks
197  * arguments, and schedules the transfer.  Does not wait for the transfer
198  * to complete.  Multi-page transfers are supported.  All I/O requests must
199  * be a multiple of a sector in length.
200  */
201 static void
202 idstrategy(struct buf *bp)
203 {
204 	struct id_softc *drv;
205 	int s;
206 
207 	drv = idgetsoftc(bp->b_dev);
208 	if (drv == NULL) {
209     		bp->b_error = EINVAL;
210 		goto bad;
211 	}
212 
213 	if (dscheck(bp, drv->slices) <= 0)
214 		goto done;
215 
216 	/*
217 	 * software write protect check
218 	 */
219 	if (drv->flags & DRV_WRITEPROT && (bp->b_flags & B_READ) == 0) {
220 		bp->b_error = EROFS;
221 		goto bad;
222 	}
223 
224 	/*
225 	 * If it's a null transfer, return immediately
226 	 */
227 	if (bp->b_bcount == 0)
228 		goto done;
229 
230 	bp->b_driver1 = drv;
231 	s = splbio();
232 	devstat_start_transaction(&drv->stats);
233 	ida_submit_buf(drv->controller, bp);
234 	splx(s);
235 	return;
236 
237 bad:
238 	bp->b_flags |= B_ERROR;
239 
240 done:
241 	/*
242 	 * Correctly set the buf to indicate a completed transfer
243 	 */
244 	bp->b_resid = bp->b_bcount;
245 	biodone(bp);
246 	return;
247 }
248 
249 void
250 id_intr(struct buf *bp)
251 {
252 	struct id_softc *drv = (struct id_softc *)bp->b_driver1;
253 
254 	if (bp->b_flags & B_ERROR)
255 		bp->b_error = EIO;
256 	else
257 		bp->b_resid = 0;
258 
259 	biodone(bp);
260 	devstat_end_transaction(&drv->stats,
261 	     bp->b_bcount - bp->b_resid, DEVSTAT_TAG_NONE,
262 	     (bp->b_flags & B_READ) ? DEVSTAT_READ : DEVSTAT_WRITE);
263 }
264 
265 static void
266 id_drvinit(void)
267 {
268 	static int devsw_installed = 0;
269 
270 	if (devsw_installed)
271 		return;				/* XXX is this needed? */
272 
273 	cdevsw_add(&id_cdevsw);
274 	stolen_cdevsw = id_cdevsw;
275 	stolen_cdevsw.d_maj = WD_CDEV_MAJOR;
276 	stolen_cdevsw.d_bmaj = WD_BDEV_MAJOR;
277 	cdevsw_add(&stolen_cdevsw);
278 	devsw_installed = 1;
279 }
280 
281 static int
282 idprobe(device_t dev)
283 {
284 
285 	id_drvinit();
286 	device_set_desc(dev, "Compaq Logical Drive");
287 	return (0);
288 }
289 
290 static int
291 idattach(device_t dev)
292 {
293 	struct ida_drive_info dinfo;
294 	struct id_softc *drv;
295 	device_t parent;
296 	int error;
297 
298 	drv = (struct id_softc *)device_get_softc(dev);
299 	parent = device_get_parent(dev);
300 	drv->controller = (struct ida_softc *)device_get_softc(parent);
301 	drv->unit = device_get_unit(dev);
302 
303 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
304 	    &dinfo, sizeof(dinfo), drv->unit, DMA_DATA_IN);
305 	if (error) {
306 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
307 		return (ENXIO);
308 	}
309 
310 	drv->cylinders = dinfo.ncylinders;
311 	drv->heads = dinfo.nheads;
312 	drv->sectors = dinfo.nsectors;
313 	drv->secsize = dinfo.secsize;
314 	drv->secperunit = dinfo.secperunit;
315 
316 	/* XXX
317 	 * other initialization
318 	 */
319 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
320 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
321 	    drv->secperunit, drv->secsize);
322 
323 	devstat_add_entry(&drv->stats, "id", drv->unit, drv->secsize,
324 	    DEVSTAT_NO_ORDERED_TAGS,
325 	    DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, DEVSTAT_PRIORITY_DA);
326 
327 	return (0);
328 }
329 
330 DEV_DRIVER_MODULE(id, ida, id_driver, id_devclass, id_cdevsw, 0, 0);
331