xref: /freebsd/sys/dev/mfi/mfi_syspd.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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 device_method_t mfi_syspd_methods[] = {
77 	DEVMETHOD(device_probe,		mfi_syspd_probe),
78 	DEVMETHOD(device_attach,	mfi_syspd_attach),
79 	DEVMETHOD(device_detach,	mfi_syspd_detach),
80 	{ 0, 0 }
81 };
82 
83 static driver_t mfi_syspd_driver = {
84 	"mfisyspd",
85 	mfi_syspd_methods,
86 	sizeof(struct mfi_system_pd)
87 };
88 
89 DRIVER_MODULE(mfisyspd, mfi, mfi_syspd_driver, 0, 0);
90 
91 static int
92 mfi_syspd_probe(device_t dev)
93 {
94 	return (0);
95 }
96 
97 static int
98 mfi_syspd_attach(device_t dev)
99 {
100 	struct mfi_system_pd *sc;
101 	struct mfi_pd_info *pd_info;
102 	struct mfi_system_pending *syspd_pend;
103 	uint64_t sectors;
104 	uint32_t secsize;
105 
106 	sc = device_get_softc(dev);
107 	pd_info = device_get_ivars(dev);
108 	sc->pd_dev = dev;
109 	sc->pd_id = pd_info->ref.v.device_id;
110 	sc->pd_unit = device_get_unit(dev);
111 	sc->pd_info = pd_info;
112 	sc->pd_controller = device_get_softc(device_get_parent(dev));
113 	sc->pd_flags = 0;
114 
115 	sectors = pd_info->raw_size;
116 	secsize = MFI_SECTOR_LEN;
117 	mtx_lock(&sc->pd_controller->mfi_io_lock);
118 	TAILQ_INSERT_TAIL(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
119 	TAILQ_FOREACH(syspd_pend, &sc->pd_controller->mfi_syspd_pend_tqh,
120 	    pd_link) {
121 		TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_pend_tqh,
122 		    syspd_pend, pd_link);
123 		free(syspd_pend, M_MFIBUF);
124 		break;
125 	}
126 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
127 	device_printf(dev, "%juMB (%ju sectors) SYSPD volume (deviceid: %d)\n",
128 		      sectors / (1024 * 1024 / secsize), sectors, sc->pd_id);
129 	sc->pd_disk = disk_alloc();
130 	sc->pd_disk->d_drv1 = sc;
131 	sc->pd_disk->d_maxsize = min(sc->pd_controller->mfi_max_io * secsize,
132 		(sc->pd_controller->mfi_max_sge - 1) * PAGE_SIZE);
133 	sc->pd_disk->d_name = "mfisyspd";
134 	sc->pd_disk->d_open = mfi_syspd_open;
135 	sc->pd_disk->d_close = mfi_syspd_close;
136 	sc->pd_disk->d_strategy = mfi_syspd_strategy;
137 	sc->pd_disk->d_dump = mfi_syspd_dump;
138 	sc->pd_disk->d_unit = sc->pd_unit;
139 	sc->pd_disk->d_sectorsize = secsize;
140 	sc->pd_disk->d_mediasize = sectors * secsize;
141 	if (sc->pd_disk->d_mediasize >= (1 * 1024 * 1024)) {
142 		sc->pd_disk->d_fwheads = 255;
143 		sc->pd_disk->d_fwsectors = 63;
144 	} else {
145 		sc->pd_disk->d_fwheads = 64;
146 		sc->pd_disk->d_fwsectors = 32;
147 	}
148 	sc->pd_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
149 	disk_create(sc->pd_disk, DISK_VERSION);
150 
151 	device_printf(dev, " SYSPD volume attached\n");
152 
153 	return (0);
154 }
155 
156 static int
157 mfi_syspd_detach(device_t dev)
158 {
159 	struct mfi_system_pd *sc;
160 
161 	sc = device_get_softc(dev);
162 	device_printf(dev, "Detaching syspd\n");
163 	mtx_lock(&sc->pd_controller->mfi_io_lock);
164 	if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) ||
165 	    (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) &&
166 	    (sc->pd_controller->mfi_keep_deleted_volumes ||
167 	    sc->pd_controller->mfi_detaching)) {
168 		mtx_unlock(&sc->pd_controller->mfi_io_lock);
169 		device_printf(dev, "Cant detach syspd\n");
170 		return (EBUSY);
171 	}
172 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
173 
174 	disk_destroy(sc->pd_disk);
175 	mtx_lock(&sc->pd_controller->mfi_io_lock);
176 	TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
177 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
178 	free(sc->pd_info, M_MFIBUF);
179 	return (0);
180 }
181 
182 static int
183 mfi_syspd_open(struct disk *dp)
184 {
185 	struct mfi_system_pd *sc;
186 	int error;
187 
188 	sc = dp->d_drv1;
189 	mtx_lock(&sc->pd_controller->mfi_io_lock);
190 	if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED)
191 		error = ENXIO;
192 	else {
193 		sc->pd_flags |= MFI_DISK_FLAGS_OPEN;
194 		error = 0;
195 	}
196 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
197 	return (error);
198 }
199 
200 static int
201 mfi_syspd_close(struct disk *dp)
202 {
203 	struct mfi_system_pd *sc;
204 
205 	sc = dp->d_drv1;
206 	mtx_lock(&sc->pd_controller->mfi_io_lock);
207 	sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN;
208 	mtx_unlock(&sc->pd_controller->mfi_io_lock);
209 
210 	return (0);
211 }
212 
213 int
214 mfi_syspd_disable(struct mfi_system_pd *sc)
215 {
216 
217 	device_printf(sc->pd_dev, "syspd disable \n");
218 	mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
219 	if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) {
220 		if (sc->pd_controller->mfi_delete_busy_volumes)
221 			return (0);
222 		device_printf(sc->pd_dev,
223 		    "Unable to delete busy syspd device\n");
224 		return (EBUSY);
225 	}
226 	sc->pd_flags |= MFI_DISK_FLAGS_DISABLED;
227 	return (0);
228 }
229 
230 void
231 mfi_syspd_enable(struct mfi_system_pd *sc)
232 {
233 
234 	device_printf(sc->pd_dev, "syspd enable \n");
235 	mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
236 	sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED;
237 }
238 
239 static void
240 mfi_syspd_strategy(struct bio *bio)
241 {
242 	struct mfi_system_pd *sc;
243 	struct mfi_softc *controller;
244 
245 	sc = bio->bio_disk->d_drv1;
246 
247 	if (sc == NULL) {
248 		bio->bio_error = EINVAL;
249 		bio->bio_flags |= BIO_ERROR;
250 		bio->bio_resid = bio->bio_bcount;
251 		biodone(bio);
252 		return;
253 	}
254 
255 	controller = sc->pd_controller;
256 	bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id;
257 	/* Mark it as system PD IO */
258 	bio->bio_driver2 = (void *)MFI_SYS_PD_IO;
259 	mtx_lock(&controller->mfi_io_lock);
260 	mfi_enqueue_bio(controller, bio);
261 	mfi_startio(controller);
262 	mtx_unlock(&controller->mfi_io_lock);
263 	return;
264 }
265 
266 static int
267 mfi_syspd_dump(void *arg, void *virt, off_t offset, size_t len)
268 {
269 	struct mfi_system_pd *sc;
270 	struct mfi_softc *parent_sc;
271 	struct disk *dp;
272 	int error;
273 
274 	dp = arg;
275 	sc = dp->d_drv1;
276 	parent_sc = sc->pd_controller;
277 
278 	if (len > 0) {
279 		if ((error = mfi_dump_syspd_blocks(parent_sc,
280 		    sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0)
281 			return (error);
282 	} else {
283 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
284 	}
285 	return (0);
286 }
287