]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mfi/mfi_syspd.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / dev / 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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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
63 static int      mfi_syspd_probe(device_t dev);
64 static int      mfi_syspd_attach(device_t dev);
65 static int      mfi_syspd_detach(device_t dev);
66
67 static disk_open_t      mfi_syspd_open;
68 static disk_close_t     mfi_syspd_close;
69 static disk_strategy_t  mfi_syspd_strategy;
70 static dumper_t         mfi_syspd_dump;
71
72 static devclass_t       mfi_syspd_devclass;
73
74 static 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
81 static driver_t mfi_syspd_driver = {
82         "mfisyspd",
83         mfi_syspd_methods,
84         sizeof(struct mfi_system_pd)
85 };
86
87 DRIVER_MODULE(mfisyspd, mfi, mfi_syspd_driver, mfi_syspd_devclass, 0, 0);
88
89 static int
90 mfi_syspd_probe(device_t dev)
91 {
92         return (0);
93 }
94
95 static int
96 mfi_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         disk_create(sc->pd_disk, DISK_VERSION);
147
148         device_printf(dev, " SYSPD volume attached\n");
149
150         return (0);
151 }
152
153 static int
154 mfi_syspd_detach(device_t dev)
155 {
156         struct mfi_system_pd *sc;
157
158         sc = device_get_softc(dev);
159         device_printf(dev, "Detaching syspd\n");
160         mtx_lock(&sc->pd_controller->mfi_io_lock);
161         if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) ||
162             (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) &&
163             (sc->pd_controller->mfi_keep_deleted_volumes ||
164             sc->pd_controller->mfi_detaching)) {
165                 mtx_unlock(&sc->pd_controller->mfi_io_lock);
166                 device_printf(dev, "Cant detach syspd\n");
167                 return (EBUSY);
168         }
169         mtx_unlock(&sc->pd_controller->mfi_io_lock);
170
171         disk_destroy(sc->pd_disk);
172         mtx_lock(&sc->pd_controller->mfi_io_lock);
173         TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
174         mtx_unlock(&sc->pd_controller->mfi_io_lock);
175         free(sc->pd_info, M_MFIBUF);
176         return (0);
177 }
178
179 static int
180 mfi_syspd_open(struct disk *dp)
181 {
182         struct mfi_system_pd *sc;
183         int error;
184
185         sc = dp->d_drv1;
186         mtx_lock(&sc->pd_controller->mfi_io_lock);
187         if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED)
188                 error = ENXIO;
189         else {
190                 sc->pd_flags |= MFI_DISK_FLAGS_OPEN;
191                 error = 0;
192         }
193         mtx_unlock(&sc->pd_controller->mfi_io_lock);
194         return (error);
195 }
196
197 static int
198 mfi_syspd_close(struct disk *dp)
199 {
200         struct mfi_system_pd *sc;
201
202         sc = dp->d_drv1;
203         mtx_lock(&sc->pd_controller->mfi_io_lock);
204         sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN;
205         mtx_unlock(&sc->pd_controller->mfi_io_lock);
206
207         return (0);
208 }
209
210 int
211 mfi_syspd_disable(struct mfi_system_pd *sc)
212 {
213
214         device_printf(sc->pd_dev, "syspd disable \n");
215         mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
216         if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) {
217                 if (sc->pd_controller->mfi_delete_busy_volumes)
218                         return (0);
219                 device_printf(sc->pd_dev,
220                     "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         mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
233         sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED;
234 }
235
236 static void
237 mfi_syspd_strategy(struct bio *bio)
238 {
239         struct mfi_system_pd *sc;
240         struct mfi_softc *controller;
241
242         sc = bio->bio_disk->d_drv1;
243
244         if (sc == NULL) {
245                 bio->bio_error = EINVAL;
246                 bio->bio_flags |= BIO_ERROR;
247                 bio->bio_resid = bio->bio_bcount;
248                 biodone(bio);
249                 return;
250         }
251
252         controller = sc->pd_controller;
253         bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id;
254         /* Mark it as system PD IO */
255         bio->bio_driver2 = (void *)MFI_SYS_PD_IO;
256         mtx_lock(&controller->mfi_io_lock);
257         mfi_enqueue_bio(controller, bio);
258         mfi_startio(controller);
259         mtx_unlock(&controller->mfi_io_lock);
260         return;
261 }
262
263 static int
264 mfi_syspd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset,
265     size_t len)
266 {
267         struct mfi_system_pd *sc;
268         struct mfi_softc *parent_sc;
269         struct disk *dp;
270         int error;
271
272         dp = arg;
273         sc = dp->d_drv1;
274         parent_sc = sc->pd_controller;
275
276         if (len > 0) {
277                 if ((error = mfi_dump_syspd_blocks(parent_sc,
278                     sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0)
279                         return (error);
280         } else {
281                 /* mfi_sync_cache(parent_sc, sc->ld_id); */
282         }
283         return (0);
284 }