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