1 1.6 riastrad /* $NetBSD: altmem.c,v 1.6 2017/10/28 04:53:55 riastradh Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products 13 1.1 jmcneill * derived from this software without specific prior written permission. 14 1.1 jmcneill * 15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 jmcneill * SUCH DAMAGE. 26 1.1 jmcneill */ 27 1.1 jmcneill 28 1.1 jmcneill #include <sys/cdefs.h> 29 1.6 riastrad __KERNEL_RCSID(0, "$NetBSD: altmem.c,v 1.6 2017/10/28 04:53:55 riastradh Exp $"); 30 1.1 jmcneill #include <sys/param.h> 31 1.1 jmcneill #include <sys/types.h> 32 1.1 jmcneill #include <sys/device.h> 33 1.1 jmcneill #include <sys/conf.h> 34 1.1 jmcneill #include <sys/buf.h> 35 1.1 jmcneill #include <sys/disklabel.h> 36 1.1 jmcneill #include <sys/disk.h> 37 1.1 jmcneill 38 1.1 jmcneill #include <dev/altmem/altmemvar.h> 39 1.1 jmcneill 40 1.6 riastrad #include "ioconf.h" 41 1.6 riastrad 42 1.1 jmcneill struct altmem_softc { 43 1.1 jmcneill device_t sc_dev; 44 1.1 jmcneill 45 1.1 jmcneill struct disk sc_dkdev; 46 1.1 jmcneill 47 1.1 jmcneill void *sc_cookie; 48 1.1 jmcneill const struct altmem_memops *sc_memops; 49 1.1 jmcneill 50 1.1 jmcneill size_t sc_size; 51 1.1 jmcneill }; 52 1.1 jmcneill 53 1.1 jmcneill static dev_type_open(altmemopen); 54 1.1 jmcneill static dev_type_close(altmemclose); 55 1.1 jmcneill static dev_type_read(altmemread); 56 1.1 jmcneill static dev_type_write(altmemwrite); 57 1.1 jmcneill static dev_type_ioctl(altmemioctl); 58 1.1 jmcneill static dev_type_strategy(altmemstrategy); 59 1.1 jmcneill static dev_type_size(altmemsize); 60 1.1 jmcneill 61 1.1 jmcneill static int altmem_match(device_t, cfdata_t, void *); 62 1.1 jmcneill static void altmem_attach(device_t, device_t, void *); 63 1.1 jmcneill 64 1.1 jmcneill const struct bdevsw altmem_bdevsw = { 65 1.2 dholland .d_open = altmemopen, 66 1.2 dholland .d_close = altmemclose, 67 1.2 dholland .d_strategy = altmemstrategy, 68 1.2 dholland .d_ioctl = altmemioctl, 69 1.2 dholland .d_dump = nodump, 70 1.2 dholland .d_psize = altmemsize, 71 1.3 dholland .d_discard = nodiscard, 72 1.2 dholland .d_flag = D_DISK 73 1.1 jmcneill }; 74 1.1 jmcneill const struct cdevsw altmem_cdevsw = { 75 1.2 dholland .d_open = altmemopen, 76 1.2 dholland .d_close = altmemclose, 77 1.2 dholland .d_read = altmemread, 78 1.2 dholland .d_write = altmemwrite, 79 1.2 dholland .d_ioctl = altmemioctl, 80 1.2 dholland .d_stop = nostop, 81 1.2 dholland .d_tty = notty, 82 1.2 dholland .d_poll = nopoll, 83 1.2 dholland .d_mmap = nommap, 84 1.2 dholland .d_kqfilter = nokqfilter, 85 1.4 dholland .d_discard = nodiscard, 86 1.2 dholland .d_flag = D_DISK 87 1.1 jmcneill }; 88 1.5 mlelstv static struct dkdriver altmemdkdriver = { 89 1.5 mlelstv .d_strategy = altmemstrategy, 90 1.5 mlelstv .d_minphys = minphys 91 1.5 mlelstv }; 92 1.1 jmcneill 93 1.1 jmcneill CFATTACH_DECL_NEW(altmem, sizeof(struct altmem_softc), altmem_match, 94 1.1 jmcneill altmem_attach, NULL, NULL); 95 1.1 jmcneill 96 1.1 jmcneill static int 97 1.1 jmcneill altmem_match(device_t parent, cfdata_t match, void *opaque) 98 1.1 jmcneill { 99 1.1 jmcneill return 1; 100 1.1 jmcneill } 101 1.1 jmcneill 102 1.1 jmcneill static void 103 1.1 jmcneill altmem_attach(device_t parent, device_t self, void *opaque) 104 1.1 jmcneill { 105 1.1 jmcneill struct altmem_softc *sc = device_private(self); 106 1.1 jmcneill struct altmem_attach_args *aaa = opaque; 107 1.1 jmcneill char pbuf[9]; 108 1.1 jmcneill 109 1.1 jmcneill sc->sc_dev = self; 110 1.1 jmcneill sc->sc_cookie = aaa->cookie; 111 1.1 jmcneill sc->sc_memops = aaa->memops; 112 1.1 jmcneill sc->sc_size = sc->sc_memops->getsize(sc->sc_cookie); 113 1.1 jmcneill 114 1.1 jmcneill format_bytes(pbuf, sizeof(pbuf), sc->sc_size); 115 1.1 jmcneill 116 1.1 jmcneill aprint_naive("\n"); 117 1.1 jmcneill aprint_normal(": %s\n", pbuf); 118 1.1 jmcneill 119 1.1 jmcneill disk_init(&sc->sc_dkdev, device_xname(self), &altmemdkdriver); 120 1.1 jmcneill disk_attach(&sc->sc_dkdev); 121 1.1 jmcneill } 122 1.1 jmcneill 123 1.1 jmcneill static int 124 1.1 jmcneill altmemsize(dev_t dev) 125 1.1 jmcneill { 126 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev)); 127 1.1 jmcneill if (sc == NULL) 128 1.1 jmcneill return 0; 129 1.1 jmcneill return sc->sc_size >> DEV_BSHIFT; 130 1.1 jmcneill } 131 1.1 jmcneill 132 1.1 jmcneill static int 133 1.1 jmcneill altmemopen(dev_t dev, int flag, int fmt, struct lwp *l) 134 1.1 jmcneill { 135 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev)); 136 1.1 jmcneill if (sc == NULL) 137 1.1 jmcneill return ENXIO; 138 1.1 jmcneill return 0; 139 1.1 jmcneill } 140 1.1 jmcneill 141 1.1 jmcneill static int 142 1.1 jmcneill altmemclose(dev_t dev, int flag, int fmt, struct lwp *l) 143 1.1 jmcneill { 144 1.1 jmcneill return 0; 145 1.1 jmcneill } 146 1.1 jmcneill 147 1.1 jmcneill static int 148 1.1 jmcneill altmemread(dev_t dev, struct uio *uio, int flags) 149 1.1 jmcneill { 150 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL) 151 1.1 jmcneill return ENXIO; 152 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_READ, minphys, uio); 153 1.1 jmcneill } 154 1.1 jmcneill 155 1.1 jmcneill static int 156 1.1 jmcneill altmemwrite(dev_t dev, struct uio *uio, int flags) 157 1.1 jmcneill { 158 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL) 159 1.1 jmcneill return ENXIO; 160 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_WRITE, minphys, uio); 161 1.1 jmcneill } 162 1.1 jmcneill 163 1.1 jmcneill static void 164 1.1 jmcneill altmemstrategy(struct buf *bp) 165 1.1 jmcneill { 166 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(bp->b_dev)); 167 1.1 jmcneill 168 1.1 jmcneill if (sc == NULL) { 169 1.1 jmcneill bp->b_error = ENXIO; 170 1.1 jmcneill biodone(bp); 171 1.1 jmcneill return; 172 1.1 jmcneill } 173 1.1 jmcneill if (bp->b_bcount == 0) { 174 1.1 jmcneill biodone(bp); 175 1.1 jmcneill return; 176 1.1 jmcneill } 177 1.1 jmcneill 178 1.1 jmcneill sc->sc_memops->strategy(sc->sc_cookie, bp); 179 1.1 jmcneill biodone(bp); 180 1.1 jmcneill } 181 1.1 jmcneill 182 1.1 jmcneill static int 183 1.1 jmcneill altmemioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 184 1.1 jmcneill { 185 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev)); 186 1.1 jmcneill struct dkwedge_info *dkw; 187 1.1 jmcneill 188 1.1 jmcneill switch (cmd) { 189 1.1 jmcneill case DIOCGWEDGEINFO: 190 1.1 jmcneill dkw = (void *)data; 191 1.1 jmcneill strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev), 192 1.1 jmcneill sizeof(dkw->dkw_devname)); 193 1.1 jmcneill strlcpy(dkw->dkw_wname, "altmem", sizeof(dkw->dkw_wname)); 194 1.1 jmcneill dkw->dkw_parent[0] = '\0'; 195 1.1 jmcneill dkw->dkw_offset = 0; 196 1.1 jmcneill dkw->dkw_size = sc->sc_size >> DEV_BSHIFT; 197 1.1 jmcneill strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED); 198 1.1 jmcneill break; 199 1.1 jmcneill default: 200 1.1 jmcneill return ENOTTY; 201 1.1 jmcneill } 202 1.1 jmcneill return 0; 203 1.1 jmcneill } 204