1157114Sscottl/*-
2157114Sscottl * Copyright (c) 2006 IronPort Systems
3157114Sscottl * All rights reserved.
4157114Sscottl *
5157114Sscottl * Redistribution and use in source and binary forms, with or without
6157114Sscottl * modification, are permitted provided that the following conditions
7157114Sscottl * are met:
8157114Sscottl * 1. Redistributions of source code must retain the above copyright
9157114Sscottl *    notice, this list of conditions and the following disclaimer.
10157114Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11157114Sscottl *    notice, this list of conditions and the following disclaimer in the
12157114Sscottl *    documentation and/or other materials provided with the distribution.
13157114Sscottl *
14157114Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15157114Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16157114Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17157114Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18157114Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19157114Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20157114Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21157114Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22157114Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23157114Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24157114Sscottl * SUCH DAMAGE.
25157114Sscottl */
26157114Sscottl
27157114Sscottl#include <sys/cdefs.h>
28157114Sscottl__FBSDID("$FreeBSD$");
29157114Sscottl
30157114Sscottl#include "opt_mfi.h"
31157114Sscottl
32157114Sscottl#include <sys/param.h>
33157114Sscottl#include <sys/systm.h>
34157114Sscottl#include <sys/kernel.h>
35158737Sambrisko#include <sys/selinfo.h>
36157114Sscottl#include <sys/module.h>
37157114Sscottl#include <sys/malloc.h>
38227562Sjhb#include <sys/sysctl.h>
39158737Sambrisko#include <sys/uio.h>
40157114Sscottl
41157114Sscottl#include <sys/bio.h>
42157114Sscottl#include <sys/bus.h>
43157114Sscottl#include <sys/conf.h>
44157114Sscottl#include <sys/disk.h>
45157114Sscottl#include <geom/geom_disk.h>
46157114Sscottl
47157114Sscottl#include <vm/vm.h>
48157114Sscottl#include <vm/pmap.h>
49157114Sscottl
50157114Sscottl#include <machine/md_var.h>
51157114Sscottl#include <machine/bus.h>
52157114Sscottl#include <sys/rman.h>
53157114Sscottl
54157114Sscottl#include <dev/mfi/mfireg.h>
55157114Sscottl#include <dev/mfi/mfi_ioctl.h>
56157114Sscottl#include <dev/mfi/mfivar.h>
57157114Sscottl
58157114Sscottlstatic int	mfi_disk_probe(device_t dev);
59157114Sscottlstatic int	mfi_disk_attach(device_t dev);
60157114Sscottlstatic int	mfi_disk_detach(device_t dev);
61157114Sscottl
62157114Sscottlstatic disk_open_t	mfi_disk_open;
63157114Sscottlstatic disk_close_t	mfi_disk_close;
64157114Sscottlstatic disk_strategy_t	mfi_disk_strategy;
65157114Sscottlstatic dumper_t		mfi_disk_dump;
66157114Sscottl
67157114Sscottlstatic devclass_t	mfi_disk_devclass;
68157114Sscottl
69157114Sscottlstatic device_method_t mfi_disk_methods[] = {
70157114Sscottl	DEVMETHOD(device_probe,		mfi_disk_probe),
71157114Sscottl	DEVMETHOD(device_attach,	mfi_disk_attach),
72157114Sscottl	DEVMETHOD(device_detach,	mfi_disk_detach),
73157114Sscottl	{ 0, 0 }
74157114Sscottl};
75157114Sscottl
76157114Sscottlstatic driver_t mfi_disk_driver = {
77157114Sscottl	"mfid",
78157114Sscottl	mfi_disk_methods,
79157114Sscottl	sizeof(struct mfi_disk)
80157114Sscottl};
81157114Sscottl
82157114SscottlDRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
83157114Sscottl
84157114Sscottlstatic int
85157114Sscottlmfi_disk_probe(device_t dev)
86157114Sscottl{
87157114Sscottl
88157114Sscottl	return (0);
89157114Sscottl}
90157114Sscottl
91157114Sscottlstatic int
92157114Sscottlmfi_disk_attach(device_t dev)
93157114Sscottl{
94157114Sscottl	struct mfi_disk *sc;
95169451Sscottl	struct mfi_ld_info *ld_info;
96242681Sambrisko	struct mfi_disk_pending *ld_pend;
97157114Sscottl	uint64_t sectors;
98157114Sscottl	uint32_t secsize;
99163398Sscottl	char *state;
100157114Sscottl
101157114Sscottl	sc = device_get_softc(dev);
102169451Sscottl	ld_info = device_get_ivars(dev);
103157114Sscottl
104157114Sscottl	sc->ld_dev = dev;
105169451Sscottl	sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
106157114Sscottl	sc->ld_unit = device_get_unit(dev);
107169451Sscottl	sc->ld_info = ld_info;
108157114Sscottl	sc->ld_controller = device_get_softc(device_get_parent(dev));
109169451Sscottl	sc->ld_flags = 0;
110157114Sscottl
111169451Sscottl	sectors = ld_info->size;
112159811Sps	secsize = MFI_SECTOR_LEN;
113171821Sjhb	mtx_lock(&sc->ld_controller->mfi_io_lock);
114169451Sscottl	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
115242681Sambrisko	TAILQ_FOREACH(ld_pend, &sc->ld_controller->mfi_ld_pend_tqh,
116242681Sambrisko	    ld_link) {
117242681Sambrisko		TAILQ_REMOVE(&sc->ld_controller->mfi_ld_pend_tqh,
118242681Sambrisko		    ld_pend, ld_link);
119242681Sambrisko		free(ld_pend, M_MFIBUF);
120242681Sambrisko		break;
121242681Sambrisko	}
122171821Sjhb	mtx_unlock(&sc->ld_controller->mfi_io_lock);
123157114Sscottl
124169451Sscottl	switch (ld_info->ld_config.params.state) {
125163398Sscottl	case MFI_LD_STATE_OFFLINE:
126163398Sscottl		state = "offline";
127163398Sscottl		break;
128163398Sscottl	case MFI_LD_STATE_PARTIALLY_DEGRADED:
129163398Sscottl		state = "partially degraded";
130163398Sscottl		break;
131163398Sscottl	case MFI_LD_STATE_DEGRADED:
132163398Sscottl		state = "degraded";
133163398Sscottl		break;
134163398Sscottl	case MFI_LD_STATE_OPTIMAL:
135163398Sscottl		state = "optimal";
136163398Sscottl		break;
137163398Sscottl	default:
138163398Sscottl		state = "unknown";
139163398Sscottl		break;
140163398Sscottl	}
141157114Sscottl
142242681Sambrisko	if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
143242681Sambrisko		device_printf(dev,
144242681Sambrisko		      "%juMB (%ju sectors) RAID volume (no label) is %s\n",
145242681Sambrisko		       sectors / (1024 * 1024 / secsize), sectors, state);
146242681Sambrisko	} else {
147242681Sambrisko		device_printf(dev,
148242681Sambrisko		      "%juMB (%ju sectors) RAID volume '%s' is %s\n",
149242681Sambrisko		      sectors / (1024 * 1024 / secsize), sectors,
150242681Sambrisko		      ld_info->ld_config.properties.name, state);
151242681Sambrisko	}
152236323Ssbruno
153157114Sscottl	sc->ld_disk = disk_alloc();
154157114Sscottl	sc->ld_disk->d_drv1 = sc;
155185035Sjhb	sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
156185035Sjhb	    (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
157157114Sscottl	sc->ld_disk->d_name = "mfid";
158157114Sscottl	sc->ld_disk->d_open = mfi_disk_open;
159157114Sscottl	sc->ld_disk->d_close = mfi_disk_close;
160157114Sscottl	sc->ld_disk->d_strategy = mfi_disk_strategy;
161157114Sscottl	sc->ld_disk->d_dump = mfi_disk_dump;
162157114Sscottl	sc->ld_disk->d_unit = sc->ld_unit;
163157114Sscottl	sc->ld_disk->d_sectorsize = secsize;
164157114Sscottl	sc->ld_disk->d_mediasize = sectors * secsize;
165157114Sscottl	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
166157114Sscottl		sc->ld_disk->d_fwheads = 255;
167157114Sscottl		sc->ld_disk->d_fwsectors = 63;
168157114Sscottl	} else {
169157114Sscottl		sc->ld_disk->d_fwheads = 64;
170157114Sscottl		sc->ld_disk->d_fwsectors = 32;
171157114Sscottl	}
172267084Skib	sc->ld_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
173157114Sscottl	disk_create(sc->ld_disk, DISK_VERSION);
174157114Sscottl
175157114Sscottl	return (0);
176157114Sscottl}
177157114Sscottl
178157114Sscottlstatic int
179157114Sscottlmfi_disk_detach(device_t dev)
180157114Sscottl{
181157114Sscottl	struct mfi_disk *sc;
182157114Sscottl
183157114Sscottl	sc = device_get_softc(dev);
184157114Sscottl
185171821Sjhb	mtx_lock(&sc->ld_controller->mfi_io_lock);
186171821Sjhb	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
187171821Sjhb	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
188171821Sjhb	    (sc->ld_controller->mfi_keep_deleted_volumes ||
189171821Sjhb	    sc->ld_controller->mfi_detaching)) {
190171821Sjhb		mtx_unlock(&sc->ld_controller->mfi_io_lock);
191157114Sscottl		return (EBUSY);
192171821Sjhb	}
193171821Sjhb	mtx_unlock(&sc->ld_controller->mfi_io_lock);
194157114Sscottl
195171821Sjhb	disk_destroy(sc->ld_disk);
196171821Sjhb	mtx_lock(&sc->ld_controller->mfi_io_lock);
197171821Sjhb	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
198171821Sjhb	mtx_unlock(&sc->ld_controller->mfi_io_lock);
199169451Sscottl	free(sc->ld_info, M_MFIBUF);
200157114Sscottl	return (0);
201157114Sscottl}
202157114Sscottl
203157114Sscottlstatic int
204157114Sscottlmfi_disk_open(struct disk *dp)
205157114Sscottl{
206169451Sscottl	struct mfi_disk *sc;
207171821Sjhb	int error;
208157114Sscottl
209169451Sscottl	sc = dp->d_drv1;
210169451Sscottl	mtx_lock(&sc->ld_controller->mfi_io_lock);
211171821Sjhb	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
212171821Sjhb		error = ENXIO;
213171821Sjhb	else {
214171821Sjhb		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
215171821Sjhb		error = 0;
216171821Sjhb	}
217169451Sscottl	mtx_unlock(&sc->ld_controller->mfi_io_lock);
218169451Sscottl
219171821Sjhb	return (error);
220157114Sscottl}
221157114Sscottl
222157114Sscottlstatic int
223157114Sscottlmfi_disk_close(struct disk *dp)
224157114Sscottl{
225169451Sscottl	struct mfi_disk *sc;
226157114Sscottl
227169451Sscottl	sc = dp->d_drv1;
228169451Sscottl	mtx_lock(&sc->ld_controller->mfi_io_lock);
229169451Sscottl	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
230169451Sscottl	mtx_unlock(&sc->ld_controller->mfi_io_lock);
231169451Sscottl
232157114Sscottl	return (0);
233157114Sscottl}
234157114Sscottl
235171821Sjhbint
236171821Sjhbmfi_disk_disable(struct mfi_disk *sc)
237171821Sjhb{
238171821Sjhb
239171821Sjhb	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
240171821Sjhb	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
241171821Sjhb		if (sc->ld_controller->mfi_delete_busy_volumes)
242171821Sjhb			return (0);
243233711Sambrisko		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
244171821Sjhb		return (EBUSY);
245171821Sjhb	}
246171821Sjhb	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
247171821Sjhb	return (0);
248171821Sjhb}
249171821Sjhb
250171821Sjhbvoid
251171821Sjhbmfi_disk_enable(struct mfi_disk *sc)
252171821Sjhb{
253171821Sjhb
254171821Sjhb	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
255171821Sjhb	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
256171821Sjhb}
257171821Sjhb
258157114Sscottlstatic void
259157114Sscottlmfi_disk_strategy(struct bio *bio)
260157114Sscottl{
261157114Sscottl	struct mfi_disk *sc;
262157114Sscottl	struct mfi_softc *controller;
263157114Sscottl
264157114Sscottl	sc = bio->bio_disk->d_drv1;
265157114Sscottl
266157114Sscottl	if (sc == NULL) {
267157114Sscottl		bio->bio_error = EINVAL;
268157114Sscottl		bio->bio_flags |= BIO_ERROR;
269157114Sscottl		bio->bio_resid = bio->bio_bcount;
270157114Sscottl		biodone(bio);
271157114Sscottl		return;
272157114Sscottl	}
273157114Sscottl
274248627Sdelphij	controller = sc->ld_controller;
275235016Sambrisko	if (controller->adpreset) {
276233711Sambrisko		bio->bio_error = EBUSY;
277233711Sambrisko		return;
278233711Sambrisko	}
279233711Sambrisko
280235016Sambrisko	if (controller->hw_crit_error) {
281233711Sambrisko		bio->bio_error = EBUSY;
282233711Sambrisko		return;
283233711Sambrisko	}
284233711Sambrisko
285235016Sambrisko	if (controller->issuepend_done == 0) {
286233711Sambrisko		bio->bio_error = EBUSY;
287233711Sambrisko		return;
288233711Sambrisko	}
289233711Sambrisko
290157114Sscottl	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
291233711Sambrisko	/* Mark it as LD IO */
292233711Sambrisko	bio->bio_driver2 = (void *)MFI_LD_IO;
293157114Sscottl	mtx_lock(&controller->mfi_io_lock);
294157114Sscottl	mfi_enqueue_bio(controller, bio);
295157114Sscottl	mfi_startio(controller);
296157114Sscottl	mtx_unlock(&controller->mfi_io_lock);
297157114Sscottl	return;
298157114Sscottl}
299157114Sscottl
300157114Sscottlvoid
301157114Sscottlmfi_disk_complete(struct bio *bio)
302157114Sscottl{
303157114Sscottl	struct mfi_disk *sc;
304157114Sscottl	struct mfi_frame_header *hdr;
305157114Sscottl
306157114Sscottl	sc = bio->bio_disk->d_drv1;
307157114Sscottl	hdr = bio->bio_driver1;
308157114Sscottl
309157114Sscottl	if (bio->bio_flags & BIO_ERROR) {
310238371Ssbruno		bio->bio_resid = bio->bio_bcount;
311157114Sscottl		if (bio->bio_error == 0)
312157114Sscottl			bio->bio_error = EIO;
313157114Sscottl		disk_err(bio, "hard error", -1, 1);
314157114Sscottl	} else {
315157114Sscottl		bio->bio_resid = 0;
316157114Sscottl	}
317157114Sscottl	biodone(bio);
318157114Sscottl}
319157114Sscottl
320157114Sscottlstatic int
321157114Sscottlmfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
322157114Sscottl{
323157114Sscottl	struct mfi_disk *sc;
324157114Sscottl	struct mfi_softc *parent_sc;
325157114Sscottl	struct disk *dp;
326157114Sscottl	int error;
327157114Sscottl
328157114Sscottl	dp = arg;
329157114Sscottl	sc = dp->d_drv1;
330157114Sscottl	parent_sc = sc->ld_controller;
331157114Sscottl
332157114Sscottl	if (len > 0) {
333157114Sscottl		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
334159811Sps		    MFI_SECTOR_LEN, virt, len)) != 0)
335157114Sscottl			return (error);
336157114Sscottl	} else {
337157114Sscottl		/* mfi_sync_cache(parent_sc, sc->ld_id); */
338157114Sscottl	}
339157114Sscottl
340157114Sscottl	return (0);
341157114Sscottl}
342