xref: /freebsd/sys/dev/mfi/mfi_syspd.c (revision 6486b015fc84e96725fef22b0e3363351399ae83)
1 /*-
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that the following conditions
4  * are met:
5  *
6  *            Copyright 1994-2009 The FreeBSD Project.
7  *            All rights reserved.
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  *    THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FREEBSD PROJECT OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation
28  * are those of the authors and should not be interpreted as representing
29  * official policies,either expressed or implied, of the FreeBSD Project.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mfi.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/selinfo.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/uio.h>
45 
46 #include <sys/bio.h>
47 #include <sys/bus.h>
48 #include <sys/conf.h>
49 #include <sys/disk.h>
50 #include <geom/geom_disk.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 
55 #include <machine/md_var.h>
56 #include <machine/bus.h>
57 #include <sys/rman.h>
58 
59 #include <dev/mfi/mfireg.h>
60 #include <dev/mfi/mfi_ioctl.h>
61 #include <dev/mfi/mfivar.h>
62 
63 static int	mfi_syspd_probe(device_t dev);
64 static int	mfi_syspd_attach(device_t dev);
65 static int	mfi_syspd_detach(device_t dev);
66 
67 static disk_open_t	mfi_syspd_open;
68 static disk_close_t	mfi_syspd_close;
69 static disk_strategy_t	mfi_syspd_strategy;
70 static dumper_t		mfi_syspd_dump;
71 
72 static devclass_t	mfi_syspd_devclass;
73 
74 static device_method_t mfi_syspd_methods[] = {
75 	DEVMETHOD(device_probe,		mfi_syspd_probe),
76 	DEVMETHOD(device_attach,	mfi_syspd_attach),
77 	DEVMETHOD(device_detach,	mfi_syspd_detach),
78 	{ 0, 0 }
79 };
80 
81 static driver_t mfi_syspd_driver = {
82 	"mfisyspd",
83 	mfi_syspd_methods,
84 	sizeof(struct mfi_system_pd)
85 };
86 
87 DRIVER_MODULE(mfisyspd, mfi, mfi_syspd_driver, mfi_syspd_devclass, 0, 0);
88 
89 static int
90 mfi_syspd_probe(device_t dev)
91 {
92 
93 	return (0);
94 }
95 
96 static int
97 mfi_syspd_attach(device_t dev)
98 {
99 	struct mfi_system_pd *sc;
100 	struct mfi_pd_info *pd_info;
101 	uint64_t sectors;
102 	uint32_t secsize;
103 
104 	sc = device_get_softc(dev);
105 	pd_info = device_get_ivars(dev);
106 
107 	sc->pd_dev = dev;
108 	sc->pd_id = pd_info->ref.v.device_id;
109 	sc->pd_unit = device_get_unit(dev);
110 	sc->pd_info = pd_info;
111 	sc->pd_controller = device_get_softc(device_get_parent(dev));
112 	sc->pd_flags = 0;
113 
114 	sectors = pd_info->raw_size;
115 	secsize = MFI_SECTOR_LEN;
116 	mtx_lock(&sc->pd_controller->mfi_io_lock);
117 	TAILQ_INSERT_TAIL(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
118 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
119 	device_printf(dev, "%juMB (%ju sectors) SYSPD volume\n",
120 		      sectors / (1024 * 1024 / secsize), sectors);
121 	sc->pd_disk = disk_alloc();
122 	sc->pd_disk->d_drv1 = sc;
123 	sc->pd_disk->d_maxsize = sc->pd_controller->mfi_max_io * secsize;
124 	sc->pd_disk->d_name = "mfisyspd";
125 	sc->pd_disk->d_open = mfi_syspd_open;
126 	sc->pd_disk->d_close = mfi_syspd_close;
127 	sc->pd_disk->d_strategy = mfi_syspd_strategy;
128 	sc->pd_disk->d_dump = mfi_syspd_dump;
129 	sc->pd_disk->d_unit = sc->pd_unit;
130 	sc->pd_disk->d_sectorsize = secsize;
131 	sc->pd_disk->d_mediasize = sectors * secsize;
132 	if (sc->pd_disk->d_mediasize >= (1 * 1024 * 1024)) {
133 		sc->pd_disk->d_fwheads = 255;
134 		sc->pd_disk->d_fwsectors = 63;
135 	} else {
136 		sc->pd_disk->d_fwheads = 64;
137 		sc->pd_disk->d_fwsectors = 32;
138 	}
139 	disk_create(sc->pd_disk, DISK_VERSION);
140 
141 	device_printf(dev, " SYSPD volume attached\n");
142 	return (0);
143 }
144 
145 static int
146 mfi_syspd_detach(device_t dev)
147 {
148 	struct mfi_system_pd *sc;
149 
150 	sc = device_get_softc(dev);
151 	device_printf(dev, "Detaching syspd\n");
152 	mtx_lock(&sc->pd_controller->mfi_io_lock);
153 	if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) ||
154 	    (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) &&
155 	    (sc->pd_controller->mfi_keep_deleted_volumes ||
156 	    sc->pd_controller->mfi_detaching)) {
157 		mtx_unlock(&sc->pd_controller->mfi_io_lock);
158 		device_printf(dev, "Cant detach syspd\n");
159 		return (EBUSY);
160 	}
161 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
162 
163 	disk_destroy(sc->pd_disk);
164 	mtx_lock(&sc->pd_controller->mfi_io_lock);
165 	TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
166 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
167 	free(sc->pd_info, M_MFIBUF);
168 	return (0);
169 }
170 
171 static int
172 mfi_syspd_open(struct disk *dp)
173 {
174 	struct mfi_system_pd *sc;
175 	int error;
176 
177 	sc = dp->d_drv1;
178 	mtx_lock(&sc->pd_controller->mfi_io_lock);
179 	if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED)
180 		error = ENXIO;
181 	else {
182 		sc->pd_flags |= MFI_DISK_FLAGS_OPEN;
183 		error = 0;
184 	}
185 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
186 	return (error);
187 }
188 
189 static int
190 mfi_syspd_close(struct disk *dp)
191 {
192 	struct mfi_system_pd *sc;
193 
194 	sc = dp->d_drv1;
195 	mtx_lock(&sc->pd_controller->mfi_io_lock);
196 	sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN;
197 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
198 
199 	return (0);
200 }
201 
202 int
203 mfi_syspd_disable(struct mfi_system_pd *sc)
204 {
205 
206 	device_printf(sc->pd_dev, "syspd disable \n");
207 	mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
208 	if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) {
209 		if (sc->pd_controller->mfi_delete_busy_volumes)
210 			return (0);
211 		device_printf(sc->pd_dev,
212 		    "Unable to delete busy syspd device\n");
213 		return (EBUSY);
214 	}
215 	sc->pd_flags |= MFI_DISK_FLAGS_DISABLED;
216 	return (0);
217 }
218 
219 void
220 mfi_syspd_enable(struct mfi_system_pd *sc)
221 {
222 
223 	device_printf(sc->pd_dev, "syspd enable \n");
224 	mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
225 	sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED;
226 }
227 
228 static void
229 mfi_syspd_strategy(struct bio *bio)
230 {
231 	struct mfi_system_pd *sc;
232 	struct mfi_softc *controller;
233 
234 	sc = bio->bio_disk->d_drv1;
235 
236 	if (sc == NULL) {
237 		bio->bio_error = EINVAL;
238 		bio->bio_flags |= BIO_ERROR;
239 		bio->bio_resid = bio->bio_bcount;
240 		biodone(bio);
241 		return;
242 	}
243 
244 	controller = sc->pd_controller;
245 	bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id;
246 	/* Mark it as system PD IO */
247 	bio->bio_driver2 = (void *)MFI_SYS_PD_IO;
248 	mtx_lock(&controller->mfi_io_lock);
249 	mfi_enqueue_bio(controller, bio);
250 	mfi_startio(controller);
251 	mtx_unlock(&controller->mfi_io_lock);
252 	return;
253 }
254 
255 static int
256 mfi_syspd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset,
257     size_t len)
258 {
259 	struct mfi_system_pd *sc;
260 	struct mfi_softc *parent_sc;
261 	struct disk *dp;
262 	int error;
263 
264 	dp = arg;
265 	sc = dp->d_drv1;
266 	parent_sc = sc->pd_controller;
267 
268 	if (len > 0) {
269 		if ((error = mfi_dump_syspd_blocks(parent_sc,
270 		    sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0)
271 			return (error);
272 	} else {
273 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
274 	}
275 	return (0);
276 }
277