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