mfi(4): Merge LSI's latest driver changes (updates us to version 3.981).
[dragonfly.git] / sys / dev / raid / mfi / mfi_syspd.c
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  * $FreeBSD: src/sys/dev/mfi/mfi_pddisk.c,v 1.2.2.6 2007/08/24 17:29:18 jhb Exp $
32  */
33
34 #include "opt_mfi.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/uio.h>
42
43 #include <sys/bio.h>
44 #include <sys/buf2.h>
45 #include <sys/bus.h>
46 #include <sys/conf.h>
47 #include <sys/disk.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51
52 #include <machine/md_var.h>
53 #include <sys/rman.h>
54
55 #include <dev/raid/mfi/mfireg.h>
56 #include <dev/raid/mfi/mfi_ioctl.h>
57 #include <dev/raid/mfi/mfivar.h>
58
59 static int      mfi_syspd_probe(device_t dev);
60 static int      mfi_syspd_attach(device_t dev);
61 static int      mfi_syspd_detach(device_t dev);
62
63 static d_open_t         mfi_syspd_open;
64 static d_close_t        mfi_syspd_close;
65 static d_strategy_t     mfi_syspd_strategy;
66 static d_dump_t         mfi_syspd_dump;
67
68 static struct dev_ops mfi_syspd_ops = {
69         { "mfisyspd", 0, D_DISK },
70         .d_open = mfi_syspd_open,
71         .d_close = mfi_syspd_close,
72         .d_strategy = mfi_syspd_strategy,
73         .d_dump = mfi_syspd_dump,
74 };
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
97         return (0);
98 }
99
100 static int
101 mfi_syspd_attach(device_t dev)
102 {
103         struct mfi_system_pd *sc;
104         struct mfi_pd_info *pd_info;
105         struct disk_info info;
106         uint64_t sectors;
107         uint32_t secsize;
108
109         sc = device_get_softc(dev);
110         pd_info = device_get_ivars(dev);
111
112         sc->pd_dev = dev;
113         sc->pd_id = pd_info->ref.v.device_id;
114         sc->pd_unit = device_get_unit(dev);
115         sc->pd_info = pd_info;
116         sc->pd_controller = device_get_softc(device_get_parent(dev));
117         sc->pd_flags = MFI_DISK_FLAGS_SYSPD;
118
119         sectors = pd_info->raw_size;
120         secsize = MFI_SECTOR_LEN;
121         lockmgr(&sc->pd_controller->mfi_io_lock, LK_EXCLUSIVE);
122         TAILQ_INSERT_TAIL(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
123         lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
124         device_printf(dev, "%juMB (%ju sectors) SYSPD volume\n",
125                       sectors / (1024 * 1024 / secsize), sectors);
126
127         devstat_add_entry(&sc->pd_devstat, "mfisyspd", device_get_unit(dev),
128             MFI_SECTOR_LEN, DEVSTAT_NO_ORDERED_TAGS,
129             DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
130             DEVSTAT_PRIORITY_ARRAY);
131
132         sc->pd_disk.d_cdev = disk_create(sc->pd_unit, &sc->pd_disk,
133             &mfi_syspd_ops);
134         sc->pd_disk.d_cdev->si_drv1 = sc;
135         sc->pd_disk.d_cdev->si_iosize_max = sc->pd_controller->mfi_max_io *
136             secsize;
137
138         bzero(&info, sizeof(info));
139         info.d_media_blksize = secsize; /* mandatory */
140         info.d_media_blocks = sectors;
141
142         if (info.d_media_blocks >= (1 * 1024 * 1024)) {
143                 info.d_nheads = 255;
144                 info.d_secpertrack = 63;
145         } else {
146                 info.d_nheads = 64;
147                 info.d_secpertrack = 32;
148         }
149
150         disk_setdiskinfo(&sc->pd_disk, &info);
151
152         device_printf(dev, " SYSPD volume attached\n");
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         lockmgr(&sc->pd_controller->mfi_io_lock, LK_EXCLUSIVE);
164         if ((sc->pd_flags & MFI_DISK_FLAGS_OPEN) &&
165             (sc->pd_controller->mfi_keep_deleted_volumes ||
166             sc->pd_controller->mfi_detaching)) {
167                 lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
168                 device_printf(dev,"Cant detach syspd\n");
169                 return (EBUSY);
170         }
171         lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
172
173         disk_destroy(&sc->pd_disk);
174         devstat_remove_entry(&sc->pd_devstat);
175         lockmgr(&sc->pd_controller->mfi_io_lock, LK_EXCLUSIVE);
176         TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
177         lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
178         kfree(sc->pd_info, M_MFIBUF);
179         return (0);
180 }
181
182 static int
183 mfi_syspd_open(struct dev_open_args *ap)
184 {
185         struct mfi_system_pd *sc = ap->a_head.a_dev->si_drv1;
186         int error;
187
188         lockmgr(&sc->pd_controller->mfi_io_lock, LK_EXCLUSIVE);
189         if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED)
190                 error = ENXIO;
191         else {
192                 sc->pd_flags |= MFI_DISK_FLAGS_OPEN;
193                 error = 0;
194         }
195         lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
196         return (error);
197 }
198
199 static int
200 mfi_syspd_close(struct dev_close_args *ap)
201 {
202         struct mfi_system_pd *sc = ap->a_head.a_dev->si_drv1;
203
204         lockmgr(&sc->pd_controller->mfi_io_lock, LK_EXCLUSIVE);
205         sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN;
206         lockmgr(&sc->pd_controller->mfi_io_lock, LK_RELEASE);
207
208         return (0);
209 }
210
211 int
212 mfi_syspd_disable(struct mfi_system_pd *sc)
213 {
214
215         device_printf(sc->pd_dev,"syspd disable \n");
216         KKASSERT(lockstatus(&sc->pd_controller->mfi_io_lock, curthread) != 0);
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, "Unable to delete busy syspd device\n");
221                 return (EBUSY);
222         }
223         sc->pd_flags |= MFI_DISK_FLAGS_DISABLED;
224         return (0);
225 }
226
227 void
228 mfi_syspd_enable(struct mfi_system_pd *sc)
229 {
230
231         device_printf(sc->pd_dev,"syspd enable \n");
232         KKASSERT(lockstatus(&sc->pd_controller->mfi_io_lock, curthread) != 0);
233         sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED;
234 }
235
236 static int
237 mfi_syspd_strategy(struct dev_strategy_args *ap)
238 {
239         struct bio *bio = ap->a_bio;
240         struct buf *bp = bio->bio_buf;
241         struct mfi_system_pd *sc = ap->a_head.a_dev->si_drv1;
242         struct mfi_softc *controller;
243
244         if (sc == NULL) {
245                 bp->b_error = EINVAL;
246                 bp->b_flags |= B_ERROR;
247                 bp->b_resid = bp->b_bcount;
248                 biodone(bio);
249                 return (0);
250         }
251
252         /*
253          * XXX swildner
254          *
255          * If it's a null transfer, do nothing. FreeBSD's original driver
256          * doesn't have this, but that caused hard error messages (even
257          * though everything else continued to work fine). Interestingly,
258          * only when HAMMER was used.
259          *
260          * Several others of our RAID drivers have this check, such as
261          * aac(4) and ida(4), so we insert it here, too.
262          *
263          * The cause of null transfers is yet unknown.
264          */
265         if (bp->b_bcount == 0) {
266                 bp->b_resid = bp->b_bcount;
267                 biodone(bio);
268                 return (0);
269         }
270
271         controller = sc->pd_controller;
272         bio->bio_driver_info = sc;
273         lockmgr(&controller->mfi_io_lock, LK_EXCLUSIVE);
274         mfi_enqueue_bio(controller, bio);
275         devstat_start_transaction(&sc->pd_devstat);
276         mfi_startio(controller);
277         lockmgr(&controller->mfi_io_lock, LK_RELEASE);
278         return (0);
279 }
280
281 #if 0
282 void
283 mfi_disk_complete(struct bio *bio)
284 {
285         struct mfi_system_pd *sc = bio->bio_driver_info;
286         struct buf *bp = bio->bio_buf;
287
288         devstat_end_transaction_buf(&sc->pd_devstat, bp);
289         if (bio->b_flags & B_ERROR) {
290                 if (bp->b_error == 0)
291                         bp->b_error = EIO;
292                 diskerr(bio, sc->pd_disk.d_cdev, "hard error", -1, 1);
293                 kprintf("\n");
294         } else {
295                 bp->b_resid = 0;
296         }
297         biodone(bio);
298 }
299 #endif
300
301 static int
302 mfi_syspd_dump(struct dev_dump_args *ap)
303 {
304         cdev_t dev = ap->a_head.a_dev;
305         off_t offset = ap->a_offset;
306         void *virt = ap->a_virtual;
307         size_t len = ap->a_length;
308         struct mfi_system_pd *sc;
309         struct mfi_softc *parent_sc;
310         int error;
311
312         sc = dev->si_drv1;
313         parent_sc = sc->pd_controller;
314
315         if (len > 0) {
316                 if ((error = mfi_dump_syspd_blocks(parent_sc, sc->pd_id, offset /
317                     MFI_SECTOR_LEN, virt, len)) != 0)
318                         return (error);
319         } else {
320                 /* mfi_sync_cache(parent_sc, sc->ld_id); */
321         }
322         return (0);
323 }