xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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 struct mfi_disk {
69 	device_t	ld_dev;
70 	int		ld_id;
71 	int		ld_unit;
72 	struct mfi_softc *ld_controller;
73 	struct mfi_ld	*ld_ld;
74 	struct disk	*ld_disk;
75 };
76 
77 static device_method_t mfi_disk_methods[] = {
78 	DEVMETHOD(device_probe,		mfi_disk_probe),
79 	DEVMETHOD(device_attach,	mfi_disk_attach),
80 	DEVMETHOD(device_detach,	mfi_disk_detach),
81 	{ 0, 0 }
82 };
83 
84 static driver_t mfi_disk_driver = {
85 	"mfid",
86 	mfi_disk_methods,
87 	sizeof(struct mfi_disk)
88 };
89 
90 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
91 
92 static int
93 mfi_disk_probe(device_t dev)
94 {
95 
96 	return (0);
97 }
98 
99 static int
100 mfi_disk_attach(device_t dev)
101 {
102 	struct mfi_disk *sc;
103 	struct mfi_ld *ld;
104 	uint64_t sectors;
105 	uint32_t secsize;
106 	char *state;
107 
108 	sc = device_get_softc(dev);
109 	ld = device_get_ivars(dev);
110 
111 	sc->ld_dev = dev;
112 	sc->ld_id = ld->ld_id;
113 	sc->ld_unit = device_get_unit(dev);
114 	sc->ld_ld = device_get_ivars(dev);
115 	sc->ld_controller = device_get_softc(device_get_parent(dev));
116 
117 	sectors = ld->ld_info->size;
118 	secsize = MFI_SECTOR_LEN;
119 	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, ld, ld_link);
120 
121 	switch (ld->ld_info->ld_config.params.state) {
122 	case MFI_LD_STATE_OFFLINE:
123 		state = "offline";
124 		break;
125 	case MFI_LD_STATE_PARTIALLY_DEGRADED:
126 		state = "partially degraded";
127 		break;
128 	case MFI_LD_STATE_DEGRADED:
129 		state = "degraded";
130 		break;
131 	case MFI_LD_STATE_OPTIMAL:
132 		state = "optimal";
133 		break;
134 	default:
135 		state = "unknown";
136 		break;
137 	}
138 	device_printf(dev, "%juMB (%ju sectors) RAID volume '%s' is %s\n",
139 		      sectors / (1024 * 1024 / secsize), sectors,
140 		      ld->ld_info->ld_config.properties.name,
141 		      state);
142 
143 	sc->ld_disk = disk_alloc();
144 	sc->ld_disk->d_drv1 = sc;
145 	sc->ld_disk->d_maxsize = sc->ld_controller->mfi_max_io * secsize;
146 	sc->ld_disk->d_name = "mfid";
147 	sc->ld_disk->d_open = mfi_disk_open;
148 	sc->ld_disk->d_close = mfi_disk_close;
149 	sc->ld_disk->d_strategy = mfi_disk_strategy;
150 	sc->ld_disk->d_dump = mfi_disk_dump;
151 	sc->ld_disk->d_unit = sc->ld_unit;
152 	sc->ld_disk->d_sectorsize = secsize;
153 	sc->ld_disk->d_mediasize = sectors * secsize;
154 	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
155 		sc->ld_disk->d_fwheads = 255;
156 		sc->ld_disk->d_fwsectors = 63;
157 	} else {
158 		sc->ld_disk->d_fwheads = 64;
159 		sc->ld_disk->d_fwsectors = 32;
160 	}
161 	disk_create(sc->ld_disk, DISK_VERSION);
162 
163 	return (0);
164 }
165 
166 static int
167 mfi_disk_detach(device_t dev)
168 {
169 	struct mfi_disk *sc;
170 
171 	sc = device_get_softc(dev);
172 
173 	if (sc->ld_disk->d_flags & DISKFLAG_OPEN)
174 		return (EBUSY);
175 
176 	disk_destroy(sc->ld_disk);
177 	return (0);
178 }
179 
180 static int
181 mfi_disk_open(struct disk *dp)
182 {
183 
184 	return (0);
185 }
186 
187 static int
188 mfi_disk_close(struct disk *dp)
189 {
190 
191 	return (0);
192 }
193 
194 static void
195 mfi_disk_strategy(struct bio *bio)
196 {
197 	struct mfi_disk *sc;
198 	struct mfi_softc *controller;
199 
200 	sc = bio->bio_disk->d_drv1;
201 
202 	if (sc == NULL) {
203 		bio->bio_error = EINVAL;
204 		bio->bio_flags |= BIO_ERROR;
205 		bio->bio_resid = bio->bio_bcount;
206 		biodone(bio);
207 		return;
208 	}
209 
210 	controller = sc->ld_controller;
211 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
212 	mtx_lock(&controller->mfi_io_lock);
213 	mfi_enqueue_bio(controller, bio);
214 	mfi_startio(controller);
215 	mtx_unlock(&controller->mfi_io_lock);
216 	return;
217 }
218 
219 void
220 mfi_disk_complete(struct bio *bio)
221 {
222 	struct mfi_disk *sc;
223 	struct mfi_frame_header *hdr;
224 
225 	sc = bio->bio_disk->d_drv1;
226 	hdr = bio->bio_driver1;
227 
228 	if (bio->bio_flags & BIO_ERROR) {
229 		if (bio->bio_error == 0)
230 			bio->bio_error = EIO;
231 		disk_err(bio, "hard error", -1, 1);
232 	} else {
233 		bio->bio_resid = 0;
234 	}
235 	biodone(bio);
236 }
237 
238 static int
239 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
240 {
241 	struct mfi_disk *sc;
242 	struct mfi_softc *parent_sc;
243 	struct disk *dp;
244 	int error;
245 
246 	dp = arg;
247 	sc = dp->d_drv1;
248 	parent_sc = sc->ld_controller;
249 
250 	if (len > 0) {
251 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
252 		    MFI_SECTOR_LEN, virt, len)) != 0)
253 			return (error);
254 	} else {
255 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
256 	}
257 
258 	return (0);
259 }
260