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