xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*-
2  * Copyright (c) 2006 IronPort Systems
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_mfi.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/selinfo.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/sysctl.h>
39 #include <sys/uio.h>
40 
41 #include <sys/bio.h>
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/disk.h>
45 #include <geom/geom_disk.h>
46 
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 
50 #include <machine/md_var.h>
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 
54 #include <dev/mfi/mfireg.h>
55 #include <dev/mfi/mfi_ioctl.h>
56 #include <dev/mfi/mfivar.h>
57 
58 static int	mfi_disk_probe(device_t dev);
59 static int	mfi_disk_attach(device_t dev);
60 static int	mfi_disk_detach(device_t dev);
61 
62 static disk_open_t	mfi_disk_open;
63 static disk_close_t	mfi_disk_close;
64 static disk_strategy_t	mfi_disk_strategy;
65 static dumper_t		mfi_disk_dump;
66 
67 static devclass_t	mfi_disk_devclass;
68 
69 static device_method_t mfi_disk_methods[] = {
70 	DEVMETHOD(device_probe,		mfi_disk_probe),
71 	DEVMETHOD(device_attach,	mfi_disk_attach),
72 	DEVMETHOD(device_detach,	mfi_disk_detach),
73 	{ 0, 0 }
74 };
75 
76 static driver_t mfi_disk_driver = {
77 	"mfid",
78 	mfi_disk_methods,
79 	sizeof(struct mfi_disk)
80 };
81 
82 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
83 
84 static int
85 mfi_disk_probe(device_t dev)
86 {
87 
88 	return (0);
89 }
90 
91 static int
92 mfi_disk_attach(device_t dev)
93 {
94 	struct mfi_disk *sc;
95 	struct mfi_ld_info *ld_info;
96 	uint64_t sectors;
97 	uint32_t secsize;
98 	char *state;
99 
100 	sc = device_get_softc(dev);
101 	ld_info = device_get_ivars(dev);
102 
103 	sc->ld_dev = dev;
104 	sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
105 	sc->ld_unit = device_get_unit(dev);
106 	sc->ld_info = ld_info;
107 	sc->ld_controller = device_get_softc(device_get_parent(dev));
108 	sc->ld_flags = 0;
109 
110 	sectors = ld_info->size;
111 	secsize = MFI_SECTOR_LEN;
112 	mtx_lock(&sc->ld_controller->mfi_io_lock);
113 	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
114 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
115 
116 	switch (ld_info->ld_config.params.state) {
117 	case MFI_LD_STATE_OFFLINE:
118 		state = "offline";
119 		break;
120 	case MFI_LD_STATE_PARTIALLY_DEGRADED:
121 		state = "partially degraded";
122 		break;
123 	case MFI_LD_STATE_DEGRADED:
124 		state = "degraded";
125 		break;
126 	case MFI_LD_STATE_OPTIMAL:
127 		state = "optimal";
128 		break;
129 	default:
130 		state = "unknown";
131 		break;
132 	}
133 
134         if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
135                 device_printf(dev,
136                       "%juMB (%ju sectors) RAID volume (no label) is %s\n",
137                       sectors / (1024 * 1024 / secsize), sectors, state);
138         } else {
139                 device_printf(dev,
140                       "%juMB (%ju sectors) RAID volume '%s' is %s\n",
141                       sectors / (1024 * 1024 / secsize), sectors,
142                       ld_info->ld_config.properties.name, state);
143         }
144 
145 	sc->ld_disk = disk_alloc();
146 	sc->ld_disk->d_drv1 = sc;
147 	sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
148 	    (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
149 	sc->ld_disk->d_name = "mfid";
150 	sc->ld_disk->d_open = mfi_disk_open;
151 	sc->ld_disk->d_close = mfi_disk_close;
152 	sc->ld_disk->d_strategy = mfi_disk_strategy;
153 	sc->ld_disk->d_dump = mfi_disk_dump;
154 	sc->ld_disk->d_unit = sc->ld_unit;
155 	sc->ld_disk->d_sectorsize = secsize;
156 	sc->ld_disk->d_mediasize = sectors * secsize;
157 	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
158 		sc->ld_disk->d_fwheads = 255;
159 		sc->ld_disk->d_fwsectors = 63;
160 	} else {
161 		sc->ld_disk->d_fwheads = 64;
162 		sc->ld_disk->d_fwsectors = 32;
163 	}
164 	disk_create(sc->ld_disk, DISK_VERSION);
165 
166 	return (0);
167 }
168 
169 static int
170 mfi_disk_detach(device_t dev)
171 {
172 	struct mfi_disk *sc;
173 
174 	sc = device_get_softc(dev);
175 
176 	mtx_lock(&sc->ld_controller->mfi_io_lock);
177 	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
178 	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
179 	    (sc->ld_controller->mfi_keep_deleted_volumes ||
180 	    sc->ld_controller->mfi_detaching)) {
181 		mtx_unlock(&sc->ld_controller->mfi_io_lock);
182 		return (EBUSY);
183 	}
184 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
185 
186 	disk_destroy(sc->ld_disk);
187 	mtx_lock(&sc->ld_controller->mfi_io_lock);
188 	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
189 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
190 	free(sc->ld_info, M_MFIBUF);
191 	return (0);
192 }
193 
194 static int
195 mfi_disk_open(struct disk *dp)
196 {
197 	struct mfi_disk *sc;
198 	int error;
199 
200 	sc = dp->d_drv1;
201 	mtx_lock(&sc->ld_controller->mfi_io_lock);
202 	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
203 		error = ENXIO;
204 	else {
205 		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
206 		error = 0;
207 	}
208 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
209 
210 	return (error);
211 }
212 
213 static int
214 mfi_disk_close(struct disk *dp)
215 {
216 	struct mfi_disk *sc;
217 
218 	sc = dp->d_drv1;
219 	mtx_lock(&sc->ld_controller->mfi_io_lock);
220 	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
221 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
222 
223 	return (0);
224 }
225 
226 int
227 mfi_disk_disable(struct mfi_disk *sc)
228 {
229 
230 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
231 	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
232 		if (sc->ld_controller->mfi_delete_busy_volumes)
233 			return (0);
234 		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
235 		return (EBUSY);
236 	}
237 	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
238 	return (0);
239 }
240 
241 void
242 mfi_disk_enable(struct mfi_disk *sc)
243 {
244 
245 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
246 	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
247 }
248 
249 static void
250 mfi_disk_strategy(struct bio *bio)
251 {
252 	struct mfi_disk *sc;
253 	struct mfi_softc *controller;
254 
255 	sc = bio->bio_disk->d_drv1;
256 	controller = sc->ld_controller;
257 
258 	if (sc == NULL) {
259 		bio->bio_error = EINVAL;
260 		bio->bio_flags |= BIO_ERROR;
261 		bio->bio_resid = bio->bio_bcount;
262 		biodone(bio);
263 		return;
264 	}
265 
266 	if (controller->adpreset) {
267 		bio->bio_error = EBUSY;
268 		return;
269 	}
270 
271 	if (controller->hw_crit_error) {
272 		bio->bio_error = EBUSY;
273 		return;
274 	}
275 
276 	if (controller->issuepend_done == 0) {
277 		bio->bio_error = EBUSY;
278 		return;
279 	}
280 
281 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
282 	/* Mark it as LD IO */
283 	bio->bio_driver2 = (void *)MFI_LD_IO;
284 	mtx_lock(&controller->mfi_io_lock);
285 	mfi_enqueue_bio(controller, bio);
286 	mfi_startio(controller);
287 	mtx_unlock(&controller->mfi_io_lock);
288 	return;
289 }
290 
291 void
292 mfi_disk_complete(struct bio *bio)
293 {
294 	struct mfi_disk *sc;
295 	struct mfi_frame_header *hdr;
296 
297 	sc = bio->bio_disk->d_drv1;
298 	hdr = bio->bio_driver1;
299 
300 	if (bio->bio_flags & BIO_ERROR) {
301 		bio->bio_resid = bio->bio_bcount;
302 		if (bio->bio_error == 0)
303 			bio->bio_error = EIO;
304 		disk_err(bio, "hard error", -1, 1);
305 	} else {
306 		bio->bio_resid = 0;
307 	}
308 	biodone(bio);
309 }
310 
311 static int
312 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
313 {
314 	struct mfi_disk *sc;
315 	struct mfi_softc *parent_sc;
316 	struct disk *dp;
317 	int error;
318 
319 	dp = arg;
320 	sc = dp->d_drv1;
321 	parent_sc = sc->ld_controller;
322 
323 	if (len > 0) {
324 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
325 		    MFI_SECTOR_LEN, virt, len)) != 0)
326 			return (error);
327 	} else {
328 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
329 	}
330 
331 	return (0);
332 }
333