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