xref: /freebsd/sys/dev/ida/ida_disk.c (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999,2000 Jonathan Lemon
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 /*
32  * Disk driver for Compaq SMART RAID adapters.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 
40 #include <sys/bio.h>
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/cons.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <machine/md_var.h>
53 
54 #include <geom/geom_disk.h>
55 
56 #include <dev/ida/idareg.h>
57 #include <dev/ida/idavar.h>
58 
59 /* prototypes */
60 static int idad_probe(device_t dev);
61 static int idad_attach(device_t dev);
62 static int idad_detach(device_t dev);
63 
64 static	d_strategy_t	idad_strategy;
65 static	dumper_t	idad_dump;
66 
67 static devclass_t	idad_devclass;
68 
69 static device_method_t idad_methods[] = {
70 	DEVMETHOD(device_probe,		idad_probe),
71 	DEVMETHOD(device_attach,	idad_attach),
72 	DEVMETHOD(device_detach,	idad_detach),
73 	{ 0, 0 }
74 };
75 
76 static driver_t idad_driver = {
77 	"idad",
78 	idad_methods,
79 	sizeof(struct idad_softc)
80 };
81 
82 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
83 
84 /*
85  * Read/write routine for a buffer.  Finds the proper unit, range checks
86  * arguments, and schedules the transfer.  Does not wait for the transfer
87  * to complete.  Multi-page transfers are supported.  All I/O requests must
88  * be a multiple of a sector in length.
89  */
90 static void
91 idad_strategy(struct bio *bp)
92 {
93 	struct idad_softc *drv;
94 
95 	drv = bp->bio_disk->d_drv1;
96 	if (drv == NULL) {
97     		bp->bio_error = EINVAL;
98 		goto bad;
99 	}
100 
101 	/*
102 	 * software write protect check
103 	 */
104 	if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) {
105 		bp->bio_error = EROFS;
106 		goto bad;
107 	}
108 
109 	if ((bp->bio_cmd != BIO_READ) && (bp->bio_cmd != BIO_WRITE)) {
110 		bp->bio_error = EOPNOTSUPP;
111 		goto bad;
112 	}
113 
114 	bp->bio_driver1 = drv;
115 	ida_submit_buf(drv->controller, bp);
116 	return;
117 
118 bad:
119 	bp->bio_flags |= BIO_ERROR;
120 
121 	/*
122 	 * Correctly set the buf to indicate a completed transfer
123 	 */
124 	bp->bio_resid = bp->bio_bcount;
125 	biodone(bp);
126 	return;
127 }
128 
129 static int
130 idad_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
131 {
132 
133 	struct idad_softc *drv;
134 	int error = 0;
135 	struct disk *dp;
136 
137 	dp = arg;
138 	drv = dp->d_drv1;
139 	if (drv == NULL)
140 		return (ENXIO);
141 
142 	drv->controller->flags &= ~IDA_INTERRUPTS;
143 
144 	if (length > 0) {
145 		error = ida_command(drv->controller, CMD_WRITE, virtual,
146 		    length, drv->drive, offset / DEV_BSIZE, DMA_DATA_OUT);
147 	}
148 	drv->controller->flags |= IDA_INTERRUPTS;
149 	return (error);
150 }
151 
152 void
153 idad_intr(struct bio *bp)
154 {
155 
156 	if (bp->bio_flags & BIO_ERROR)
157 		bp->bio_error = EIO;
158 	else
159 		bp->bio_resid = 0;
160 
161 	biodone(bp);
162 }
163 
164 static int
165 idad_probe(device_t dev)
166 {
167 
168 	device_set_desc(dev, "Compaq Logical Drive");
169 	return (0);
170 }
171 
172 static int
173 idad_attach(device_t dev)
174 {
175 	struct ida_drive_info dinfo;
176 	struct idad_softc *drv;
177 	device_t parent;
178 	int error;
179 
180 	drv = (struct idad_softc *)device_get_softc(dev);
181 	parent = device_get_parent(dev);
182 	drv->dev = dev;
183 	drv->controller = (struct ida_softc *)device_get_softc(parent);
184 	drv->unit = device_get_unit(dev);
185 	drv->drive = (intptr_t)device_get_ivars(dev);
186 
187 	mtx_lock(&drv->controller->lock);
188 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
189 	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
190 	mtx_unlock(&drv->controller->lock);
191 	if (error) {
192 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
193 		return (ENXIO);
194 	}
195 
196 	drv->cylinders = dinfo.dp.ncylinders;
197 	drv->heads = dinfo.dp.nheads;
198 	drv->sectors = dinfo.dp.nsectors;
199 	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
200 	drv->secperunit = dinfo.secperunit;
201 
202 	/* XXX
203 	 * other initialization
204 	 */
205 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
206 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
207 	    drv->secperunit, drv->secsize);
208 
209 	drv->disk = disk_alloc();
210 	drv->disk->d_strategy = idad_strategy;
211 	drv->disk->d_name = "idad";
212 	drv->disk->d_dump = idad_dump;
213 	drv->disk->d_sectorsize = drv->secsize;
214 	drv->disk->d_mediasize = (off_t)drv->secperunit * drv->secsize;
215 	drv->disk->d_fwsectors = drv->sectors;
216 	drv->disk->d_fwheads = drv->heads;
217 	drv->disk->d_drv1 = drv;
218 	drv->disk->d_maxsize = DFLTPHYS;		/* XXX guess? */
219 	drv->disk->d_unit = drv->unit;
220 	disk_create(drv->disk, DISK_VERSION);
221 
222 	return (0);
223 }
224 
225 static int
226 idad_detach(device_t dev)
227 {
228 	struct idad_softc *drv;
229 
230 	drv = (struct idad_softc *)device_get_softc(dev);
231 	disk_destroy(drv->disk);
232 	return (0);
233 }
234