xref: /dflybsd-src/sys/dev/raid/ida/ida_disk.c (revision fabb8cebee0ef2460612b33883e1aeb538add25e)
1 /*-
2  * Copyright (c) 1999,2000 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  * $FreeBSD: src/sys/dev/ida/ida_disk.c,v 1.12.2.6 2001/11/27 20:21:02 ps Exp $
27  * $DragonFly: src/sys/dev/raid/ida/ida_disk.c,v 1.4 2003/07/21 05:50:31 dillon Exp $
28  */
29 
30 /*
31  * Disk driver for Compaq SMART RAID adapters.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 
39 #include <sys/buf.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/cons.h>
43 #include <sys/devicestat.h>
44 #include <sys/disk.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 <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <machine/md_var.h>
57 
58 #include <dev/ida/idareg.h>
59 #include <dev/ida/idavar.h>
60 
61 /* prototypes */
62 static int idad_probe(device_t dev);
63 static int idad_attach(device_t dev);
64 static int idad_detach(device_t dev);
65 
66 static	d_open_t	idad_open;
67 static	d_close_t	idad_close;
68 static	d_strategy_t	idad_strategy;
69 static	d_dump_t	idad_dump;
70 
71 #define IDAD_BDEV_MAJOR	29
72 #define IDAD_CDEV_MAJOR	109
73 
74 static struct cdevsw id_cdevsw = {
75 	/* name */ 	"idad",
76 	/* maj */	IDAD_CDEV_MAJOR,
77 	/* flags */	D_DISK,
78 	/* port */	NULL,
79 	/* autoq */	0,
80 
81 	/* open */	idad_open,
82 	/* close */	idad_close,
83 	/* read */	physread,
84 	/* write */	physwrite,
85 	/* ioctl */	noioctl,
86 	/* poll */	nopoll,
87 	/* mmap */	nommap,
88 	/* strategy */	idad_strategy,
89 	/* dump */	idad_dump,
90 	/* psize */ 	nopsize
91 };
92 
93 static devclass_t	idad_devclass;
94 static struct cdevsw 	idaddisk_cdevsw;
95 static int		disks_registered = 0;
96 
97 static device_method_t idad_methods[] = {
98 	DEVMETHOD(device_probe,		idad_probe),
99 	DEVMETHOD(device_attach,	idad_attach),
100 	DEVMETHOD(device_detach,	idad_detach),
101 	{ 0, 0 }
102 };
103 
104 static driver_t idad_driver = {
105 	"idad",
106 	idad_methods,
107 	sizeof(struct idad_softc)
108 };
109 
110 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
111 
112 static __inline struct idad_softc *
113 idad_getsoftc(dev_t dev)
114 {
115 
116 	return ((struct idad_softc *)dev->si_drv1);
117 }
118 
119 static int
120 idad_open(dev_t dev, int flags, int fmt, d_thread_t *td)
121 {
122 	struct idad_softc *drv;
123 	struct disklabel *label;
124 
125 	drv = idad_getsoftc(dev);
126 	if (drv == NULL)
127 		return (ENXIO);
128 
129 	label = &drv->disk.d_label;
130 	bzero(label, sizeof(*label));
131 	label->d_type = DTYPE_SCSI;
132 	label->d_secsize = drv->secsize;
133 	label->d_nsectors = drv->sectors;
134 	label->d_ntracks = drv->heads;
135 	label->d_ncylinders = drv->cylinders;
136 	label->d_secpercyl = drv->sectors * drv->heads;
137 	label->d_secperunit = drv->secperunit;
138 
139 	return (0);
140 }
141 
142 static int
143 idad_close(dev_t dev, int flags, int fmt, d_thread_t *td)
144 {
145 	struct idad_softc *drv;
146 
147 	drv = idad_getsoftc(dev);
148 	if (drv == NULL)
149 		return (ENXIO);
150 	return (0);
151 }
152 
153 /*
154  * Read/write routine for a buffer.  Finds the proper unit, range checks
155  * arguments, and schedules the transfer.  Does not wait for the transfer
156  * to complete.  Multi-page transfers are supported.  All I/O requests must
157  * be a multiple of a sector in length.
158  */
159 static void
160 idad_strategy(struct buf *bp)
161 {
162 	struct idad_softc *drv;
163 	int s;
164 
165 	drv = idad_getsoftc(bp->b_dev);
166 	if (drv == NULL) {
167     		bp->b_error = EINVAL;
168 		goto bad;
169 	}
170 
171 	/*
172 	 * software write protect check
173 	 */
174 	if (drv->flags & DRV_WRITEPROT && (bp->b_flags & B_READ) == 0) {
175 		bp->b_error = EROFS;
176 		goto bad;
177 	}
178 
179 	/*
180 	 * If it's a null transfer, return immediately
181 	 */
182 	if (bp->b_bcount == 0)
183 		goto done;
184 
185 	bp->b_driver1 = drv;
186 	s = splbio();
187 	devstat_start_transaction(&drv->stats);
188 	ida_submit_buf(drv->controller, bp);
189 	splx(s);
190 	return;
191 
192 bad:
193 	bp->b_flags |= B_ERROR;
194 
195 done:
196 	/*
197 	 * Correctly set the buf to indicate a completed transfer
198 	 */
199 	bp->b_resid = bp->b_bcount;
200 	biodone(bp);
201 	return;
202 }
203 
204 static int
205 idad_dump(dev_t dev)
206 {
207 	struct idad_softc *drv;
208 	u_int count, blkno, secsize;
209 	long blkcnt;
210 	int i, error, dumppages;
211         caddr_t va;
212 	vm_offset_t addr, a;
213 
214 	if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
215 		return (error);
216 
217 	drv = idad_getsoftc(dev);
218 	if (drv == NULL)
219 		return (ENXIO);
220 
221 	addr = 0;
222 	blkcnt = howmany(PAGE_SIZE, secsize);
223 
224 	while (count > 0) {
225 		va = NULL;
226 
227 		dumppages = imin(count / blkcnt, MAXDUMPPGS);
228 
229 		for (i = 0; i < dumppages; i++) {
230 			a = addr + (i * PAGE_SIZE);
231 			if (is_physical_memory(a))
232 				va = pmap_kenter_temporary(trunc_page(a), i);
233 			else
234 				va = pmap_kenter_temporary(trunc_page(0), i);
235 		}
236 
237 		error = ida_command(drv->controller, CMD_WRITE, va,
238 		    PAGE_SIZE * dumppages, drv->drive, blkno, DMA_DATA_OUT);
239 		if (error)
240 			return (error);
241 
242 		if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
243 			return (EINTR);
244 
245 		blkno += blkcnt * dumppages;
246 		count -= blkcnt * dumppages;
247 		addr += PAGE_SIZE * dumppages;
248 	}
249 	return (0);
250 }
251 
252 void
253 idad_intr(struct buf *bp)
254 {
255 	struct idad_softc *drv = (struct idad_softc *)bp->b_driver1;
256 
257 	if (bp->b_flags & B_ERROR)
258 		bp->b_error = EIO;
259 	else
260 		bp->b_resid = 0;
261 
262 	devstat_end_transaction_buf(&drv->stats, bp);
263 	biodone(bp);
264 }
265 
266 static int
267 idad_probe(device_t dev)
268 {
269 
270 	device_set_desc(dev, "Compaq Logical Drive");
271 	return (0);
272 }
273 
274 static int
275 idad_attach(device_t dev)
276 {
277 	struct ida_drive_info dinfo;
278 	struct idad_softc *drv;
279 	device_t parent;
280 	dev_t dsk;
281 	int error;
282 
283 	drv = (struct idad_softc *)device_get_softc(dev);
284 	parent = device_get_parent(dev);
285 	drv->controller = (struct ida_softc *)device_get_softc(parent);
286 	drv->unit = device_get_unit(dev);
287 	drv->drive = drv->controller->num_drives;
288 	drv->controller->num_drives++;
289 
290 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
291 	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
292 	if (error) {
293 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
294 		return (ENXIO);
295 	}
296 
297 	drv->cylinders = dinfo.ncylinders;
298 	drv->heads = dinfo.nheads;
299 	drv->sectors = dinfo.nsectors;
300 	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
301 	drv->secperunit = dinfo.secperunit;
302 
303 	/* XXX
304 	 * other initialization
305 	 */
306 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
307 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
308 	    drv->secperunit, drv->secsize);
309 
310 	devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize,
311 	    DEVSTAT_NO_ORDERED_TAGS,
312 	    DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER,
313 	    DEVSTAT_PRIORITY_ARRAY);
314 
315 	dsk = disk_create(drv->unit, &drv->disk, 0,
316 	    &id_cdevsw, &idaddisk_cdevsw);
317 
318 	dsk->si_drv1 = drv;
319 	dsk->si_iosize_max = DFLTPHYS;		/* XXX guess? */
320 	disks_registered++;
321 
322 	return (0);
323 }
324 
325 static int
326 idad_detach(device_t dev)
327 {
328 	struct idad_softc *drv;
329 
330 	drv = (struct idad_softc *)device_get_softc(dev);
331 	devstat_remove_entry(&drv->stats);
332 
333 	if (--disks_registered == 0)
334 		cdevsw_remove(&idaddisk_cdevsw);
335 	return (0);
336 }
337