]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mfi/mfi_syspd.c
Import DTS files from Linux 5.0
[FreeBSD/FreeBSD.git] / sys / dev / mfi / mfi_syspd.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *            Copyright 1994-2009 The FreeBSD Project.
9  *            All rights reserved.
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  *    THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FREEBSD PROJECT OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation
30  * are those of the authors and should not be interpreted as representing
31  * official policies,either expressed or implied, of the FreeBSD Project.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_mfi.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/selinfo.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
46 #include <sys/uio.h>
47
48 #include <sys/bio.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/disk.h>
52 #include <geom/geom_disk.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56
57 #include <machine/md_var.h>
58 #include <machine/bus.h>
59 #include <sys/rman.h>
60
61 #include <dev/mfi/mfireg.h>
62 #include <dev/mfi/mfi_ioctl.h>
63 #include <dev/mfi/mfivar.h>
64
65 static int      mfi_syspd_probe(device_t dev);
66 static int      mfi_syspd_attach(device_t dev);
67 static int      mfi_syspd_detach(device_t dev);
68
69 static disk_open_t      mfi_syspd_open;
70 static disk_close_t     mfi_syspd_close;
71 static disk_strategy_t  mfi_syspd_strategy;
72 static dumper_t         mfi_syspd_dump;
73
74 static devclass_t       mfi_syspd_devclass;
75
76 static device_method_t mfi_syspd_methods[] = {
77         DEVMETHOD(device_probe,         mfi_syspd_probe),
78         DEVMETHOD(device_attach,        mfi_syspd_attach),
79         DEVMETHOD(device_detach,        mfi_syspd_detach),
80         { 0, 0 }
81 };
82
83 static driver_t mfi_syspd_driver = {
84         "mfisyspd",
85         mfi_syspd_methods,
86         sizeof(struct mfi_system_pd)
87 };
88
89 DRIVER_MODULE(mfisyspd, mfi, mfi_syspd_driver, mfi_syspd_devclass, 0, 0);
90
91 static int
92 mfi_syspd_probe(device_t dev)
93 {
94         return (0);
95 }
96
97 static int
98 mfi_syspd_attach(device_t dev)
99 {
100         struct mfi_system_pd *sc;
101         struct mfi_pd_info *pd_info;
102         struct mfi_system_pending *syspd_pend;
103         uint64_t sectors;
104         uint32_t secsize;
105
106         sc = device_get_softc(dev);
107         pd_info = device_get_ivars(dev);
108         sc->pd_dev = dev;
109         sc->pd_id = pd_info->ref.v.device_id;
110         sc->pd_unit = device_get_unit(dev);
111         sc->pd_info = pd_info;
112         sc->pd_controller = device_get_softc(device_get_parent(dev));
113         sc->pd_flags = 0;
114
115         sectors = pd_info->raw_size;
116         secsize = MFI_SECTOR_LEN;
117         mtx_lock(&sc->pd_controller->mfi_io_lock);
118         TAILQ_INSERT_TAIL(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
119         TAILQ_FOREACH(syspd_pend, &sc->pd_controller->mfi_syspd_pend_tqh,
120             pd_link) {
121                 TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_pend_tqh,
122                     syspd_pend, pd_link);
123                 free(syspd_pend, M_MFIBUF);
124                 break;
125         }
126         mtx_unlock(&sc->pd_controller->mfi_io_lock);
127         device_printf(dev, "%juMB (%ju sectors) SYSPD volume (deviceid: %d)\n",
128                       sectors / (1024 * 1024 / secsize), sectors, sc->pd_id);
129         sc->pd_disk = disk_alloc();
130         sc->pd_disk->d_drv1 = sc;
131         sc->pd_disk->d_maxsize = min(sc->pd_controller->mfi_max_io * secsize,
132                 (sc->pd_controller->mfi_max_sge - 1) * PAGE_SIZE);
133         sc->pd_disk->d_name = "mfisyspd";
134         sc->pd_disk->d_open = mfi_syspd_open;
135         sc->pd_disk->d_close = mfi_syspd_close;
136         sc->pd_disk->d_strategy = mfi_syspd_strategy;
137         sc->pd_disk->d_dump = mfi_syspd_dump;
138         sc->pd_disk->d_unit = sc->pd_unit;
139         sc->pd_disk->d_sectorsize = secsize;
140         sc->pd_disk->d_mediasize = sectors * secsize;
141         if (sc->pd_disk->d_mediasize >= (1 * 1024 * 1024)) {
142                 sc->pd_disk->d_fwheads = 255;
143                 sc->pd_disk->d_fwsectors = 63;
144         } else {
145                 sc->pd_disk->d_fwheads = 64;
146                 sc->pd_disk->d_fwsectors = 32;
147         }
148         sc->pd_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
149         disk_create(sc->pd_disk, DISK_VERSION);
150
151         device_printf(dev, " SYSPD volume attached\n");
152
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         mtx_lock(&sc->pd_controller->mfi_io_lock);
164         if (((sc->pd_disk->d_flags & DISKFLAG_OPEN) ||
165             (sc->pd_flags & MFI_DISK_FLAGS_OPEN)) &&
166             (sc->pd_controller->mfi_keep_deleted_volumes ||
167             sc->pd_controller->mfi_detaching)) {
168                 mtx_unlock(&sc->pd_controller->mfi_io_lock);
169                 device_printf(dev, "Cant detach syspd\n");
170                 return (EBUSY);
171         }
172         mtx_unlock(&sc->pd_controller->mfi_io_lock);
173
174         disk_destroy(sc->pd_disk);
175         mtx_lock(&sc->pd_controller->mfi_io_lock);
176         TAILQ_REMOVE(&sc->pd_controller->mfi_syspd_tqh, sc, pd_link);
177         mtx_unlock(&sc->pd_controller->mfi_io_lock);
178         free(sc->pd_info, M_MFIBUF);
179         return (0);
180 }
181
182 static int
183 mfi_syspd_open(struct disk *dp)
184 {
185         struct mfi_system_pd *sc;
186         int error;
187
188         sc = dp->d_drv1;
189         mtx_lock(&sc->pd_controller->mfi_io_lock);
190         if (sc->pd_flags & MFI_DISK_FLAGS_DISABLED)
191                 error = ENXIO;
192         else {
193                 sc->pd_flags |= MFI_DISK_FLAGS_OPEN;
194                 error = 0;
195         }
196         mtx_unlock(&sc->pd_controller->mfi_io_lock);
197         return (error);
198 }
199
200 static int
201 mfi_syspd_close(struct disk *dp)
202 {
203         struct mfi_system_pd *sc;
204
205         sc = dp->d_drv1;
206         mtx_lock(&sc->pd_controller->mfi_io_lock);
207         sc->pd_flags &= ~MFI_DISK_FLAGS_OPEN;
208         mtx_unlock(&sc->pd_controller->mfi_io_lock);
209
210         return (0);
211 }
212
213 int
214 mfi_syspd_disable(struct mfi_system_pd *sc)
215 {
216
217         device_printf(sc->pd_dev, "syspd disable \n");
218         mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
219         if (sc->pd_flags & MFI_DISK_FLAGS_OPEN) {
220                 if (sc->pd_controller->mfi_delete_busy_volumes)
221                         return (0);
222                 device_printf(sc->pd_dev,
223                     "Unable to delete busy syspd device\n");
224                 return (EBUSY);
225         }
226         sc->pd_flags |= MFI_DISK_FLAGS_DISABLED;
227         return (0);
228 }
229
230 void
231 mfi_syspd_enable(struct mfi_system_pd *sc)
232 {
233
234         device_printf(sc->pd_dev, "syspd enable \n");
235         mtx_assert(&sc->pd_controller->mfi_io_lock, MA_OWNED);
236         sc->pd_flags &= ~MFI_DISK_FLAGS_DISABLED;
237 }
238
239 static void
240 mfi_syspd_strategy(struct bio *bio)
241 {
242         struct mfi_system_pd *sc;
243         struct mfi_softc *controller;
244
245         sc = bio->bio_disk->d_drv1;
246
247         if (sc == NULL) {
248                 bio->bio_error = EINVAL;
249                 bio->bio_flags |= BIO_ERROR;
250                 bio->bio_resid = bio->bio_bcount;
251                 biodone(bio);
252                 return;
253         }
254
255         controller = sc->pd_controller;
256         bio->bio_driver1 = (void *)(uintptr_t)sc->pd_id;
257         /* Mark it as system PD IO */
258         bio->bio_driver2 = (void *)MFI_SYS_PD_IO;
259         mtx_lock(&controller->mfi_io_lock);
260         mfi_enqueue_bio(controller, bio);
261         mfi_startio(controller);
262         mtx_unlock(&controller->mfi_io_lock);
263         return;
264 }
265
266 static int
267 mfi_syspd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset,
268     size_t len)
269 {
270         struct mfi_system_pd *sc;
271         struct mfi_softc *parent_sc;
272         struct disk *dp;
273         int error;
274
275         dp = arg;
276         sc = dp->d_drv1;
277         parent_sc = sc->pd_controller;
278
279         if (len > 0) {
280                 if ((error = mfi_dump_syspd_blocks(parent_sc,
281                     sc->pd_id, offset / MFI_SECTOR_LEN, virt, len)) != 0)
282                         return (error);
283         } else {
284                 /* mfi_sync_cache(parent_sc, sc->ld_id); */
285         }
286         return (0);
287 }