mfi_disk.c revision 236323
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: head/sys/dev/mfi/mfi_disk.c 236323 2012-05-30 17:07:50Z sbruno $");
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/sysctl.h>
39#include <sys/uio.h>
40
41#include <sys/bio.h>
42#include <sys/bus.h>
43#include <sys/conf.h>
44#include <sys/disk.h>
45#include <geom/geom_disk.h>
46
47#include <vm/vm.h>
48#include <vm/pmap.h>
49
50#include <machine/md_var.h>
51#include <machine/bus.h>
52#include <sys/rman.h>
53
54#include <dev/mfi/mfireg.h>
55#include <dev/mfi/mfi_ioctl.h>
56#include <dev/mfi/mfivar.h>
57
58static int	mfi_disk_probe(device_t dev);
59static int	mfi_disk_attach(device_t dev);
60static int	mfi_disk_detach(device_t dev);
61
62static disk_open_t	mfi_disk_open;
63static disk_close_t	mfi_disk_close;
64static disk_strategy_t	mfi_disk_strategy;
65static dumper_t		mfi_disk_dump;
66
67static devclass_t	mfi_disk_devclass;
68
69static device_method_t mfi_disk_methods[] = {
70	DEVMETHOD(device_probe,		mfi_disk_probe),
71	DEVMETHOD(device_attach,	mfi_disk_attach),
72	DEVMETHOD(device_detach,	mfi_disk_detach),
73	{ 0, 0 }
74};
75
76static driver_t mfi_disk_driver = {
77	"mfid",
78	mfi_disk_methods,
79	sizeof(struct mfi_disk)
80};
81
82DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
83
84static int
85mfi_disk_probe(device_t dev)
86{
87
88	return (0);
89}
90
91static int
92mfi_disk_attach(device_t dev)
93{
94	struct mfi_disk *sc;
95	struct mfi_ld_info *ld_info;
96	uint64_t sectors;
97	uint32_t secsize;
98	char *state;
99
100	sc = device_get_softc(dev);
101	ld_info = device_get_ivars(dev);
102
103	sc->ld_dev = dev;
104	sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
105	sc->ld_unit = device_get_unit(dev);
106	sc->ld_info = ld_info;
107	sc->ld_controller = device_get_softc(device_get_parent(dev));
108	sc->ld_flags = 0;
109
110	sectors = ld_info->size;
111	secsize = MFI_SECTOR_LEN;
112	mtx_lock(&sc->ld_controller->mfi_io_lock);
113	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
114	mtx_unlock(&sc->ld_controller->mfi_io_lock);
115
116	switch (ld_info->ld_config.params.state) {
117	case MFI_LD_STATE_OFFLINE:
118		state = "offline";
119		break;
120	case MFI_LD_STATE_PARTIALLY_DEGRADED:
121		state = "partially degraded";
122		break;
123	case MFI_LD_STATE_DEGRADED:
124		state = "degraded";
125		break;
126	case MFI_LD_STATE_OPTIMAL:
127		state = "optimal";
128		break;
129	default:
130		state = "unknown";
131		break;
132	}
133
134        if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
135                device_printf(dev,
136                      "%juMB (%ju sectors) RAID volume (no label) is %s\n",
137                      sectors / (1024 * 1024 / secsize), sectors, state);
138        } else {
139                device_printf(dev,
140                      "%juMB (%ju sectors) RAID volume '%s' is %s\n",
141                      sectors / (1024 * 1024 / secsize), sectors,
142                      ld_info->ld_config.properties.name, state);
143        }
144
145	sc->ld_disk = disk_alloc();
146	sc->ld_disk->d_drv1 = sc;
147	sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
148	    (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
149	sc->ld_disk->d_name = "mfid";
150	sc->ld_disk->d_open = mfi_disk_open;
151	sc->ld_disk->d_close = mfi_disk_close;
152	sc->ld_disk->d_strategy = mfi_disk_strategy;
153	sc->ld_disk->d_dump = mfi_disk_dump;
154	sc->ld_disk->d_unit = sc->ld_unit;
155	sc->ld_disk->d_sectorsize = secsize;
156	sc->ld_disk->d_mediasize = sectors * secsize;
157	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
158		sc->ld_disk->d_fwheads = 255;
159		sc->ld_disk->d_fwsectors = 63;
160	} else {
161		sc->ld_disk->d_fwheads = 64;
162		sc->ld_disk->d_fwsectors = 32;
163	}
164	disk_create(sc->ld_disk, DISK_VERSION);
165
166	return (0);
167}
168
169static int
170mfi_disk_detach(device_t dev)
171{
172	struct mfi_disk *sc;
173
174	sc = device_get_softc(dev);
175
176	mtx_lock(&sc->ld_controller->mfi_io_lock);
177	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
178	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
179	    (sc->ld_controller->mfi_keep_deleted_volumes ||
180	    sc->ld_controller->mfi_detaching)) {
181		mtx_unlock(&sc->ld_controller->mfi_io_lock);
182		return (EBUSY);
183	}
184	mtx_unlock(&sc->ld_controller->mfi_io_lock);
185
186	disk_destroy(sc->ld_disk);
187	mtx_lock(&sc->ld_controller->mfi_io_lock);
188	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
189	mtx_unlock(&sc->ld_controller->mfi_io_lock);
190	free(sc->ld_info, M_MFIBUF);
191	return (0);
192}
193
194static int
195mfi_disk_open(struct disk *dp)
196{
197	struct mfi_disk *sc;
198	int error;
199
200	sc = dp->d_drv1;
201	mtx_lock(&sc->ld_controller->mfi_io_lock);
202	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
203		error = ENXIO;
204	else {
205		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
206		error = 0;
207	}
208	mtx_unlock(&sc->ld_controller->mfi_io_lock);
209
210	return (error);
211}
212
213static int
214mfi_disk_close(struct disk *dp)
215{
216	struct mfi_disk *sc;
217
218	sc = dp->d_drv1;
219	mtx_lock(&sc->ld_controller->mfi_io_lock);
220	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
221	mtx_unlock(&sc->ld_controller->mfi_io_lock);
222
223	return (0);
224}
225
226int
227mfi_disk_disable(struct mfi_disk *sc)
228{
229
230	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
231	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
232		if (sc->ld_controller->mfi_delete_busy_volumes)
233			return (0);
234		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
235		return (EBUSY);
236	}
237	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
238	return (0);
239}
240
241void
242mfi_disk_enable(struct mfi_disk *sc)
243{
244
245	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
246	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
247}
248
249static void
250mfi_disk_strategy(struct bio *bio)
251{
252	struct mfi_disk *sc;
253	struct mfi_softc *controller;
254
255	sc = bio->bio_disk->d_drv1;
256	controller = sc->ld_controller;
257
258	if (sc == NULL) {
259		bio->bio_error = EINVAL;
260		bio->bio_flags |= BIO_ERROR;
261		bio->bio_resid = bio->bio_bcount;
262		biodone(bio);
263		return;
264	}
265
266	if (controller->adpreset) {
267		bio->bio_error = EBUSY;
268		return;
269	}
270
271	if (controller->hw_crit_error) {
272		bio->bio_error = EBUSY;
273		return;
274	}
275
276	if (controller->issuepend_done == 0) {
277		bio->bio_error = EBUSY;
278		return;
279	}
280
281	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
282	/* Mark it as LD IO */
283	bio->bio_driver2 = (void *)MFI_LD_IO;
284	mtx_lock(&controller->mfi_io_lock);
285	mfi_enqueue_bio(controller, bio);
286	mfi_startio(controller);
287	mtx_unlock(&controller->mfi_io_lock);
288	return;
289}
290
291void
292mfi_disk_complete(struct bio *bio)
293{
294	struct mfi_disk *sc;
295	struct mfi_frame_header *hdr;
296
297	sc = bio->bio_disk->d_drv1;
298	hdr = bio->bio_driver1;
299
300	if (bio->bio_flags & BIO_ERROR) {
301		if (bio->bio_error == 0)
302			bio->bio_error = EIO;
303		disk_err(bio, "hard error", -1, 1);
304	} else {
305		bio->bio_resid = 0;
306	}
307	biodone(bio);
308}
309
310static int
311mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
312{
313	struct mfi_disk *sc;
314	struct mfi_softc *parent_sc;
315	struct disk *dp;
316	int error;
317
318	dp = arg;
319	sc = dp->d_drv1;
320	parent_sc = sc->ld_controller;
321
322	if (len > 0) {
323		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
324		    MFI_SECTOR_LEN, virt, len)) != 0)
325			return (error);
326	} else {
327		/* mfi_sync_cache(parent_sc, sc->ld_id); */
328	}
329
330	return (0);
331}
332