1 /* $NetBSD: ld_icp.c,v 1.31 2017/02/27 21:32:33 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * ICP-Vortex "GDT" front-end for ld(4) driver. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ld_icp.c,v 1.31 2017/02/27 21:32:33 jdolecek Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/buf.h> 44 #include <sys/bufq.h> 45 #include <sys/endian.h> 46 #include <sys/dkio.h> 47 #include <sys/disk.h> 48 #include <sys/module.h> 49 #include <sys/bus.h> 50 51 #include <dev/ldvar.h> 52 53 #include <dev/ic/icpreg.h> 54 #include <dev/ic/icpvar.h> 55 56 #include "ioconf.h" 57 58 struct ld_icp_softc { 59 struct ld_softc sc_ld; 60 int sc_hwunit; 61 }; 62 63 static void ld_icp_attach(device_t, device_t, void *); 64 static int ld_icp_detach(device_t, int); 65 static int ld_icp_dobio(struct ld_icp_softc *, void *, int, int, int, 66 struct buf *); 67 static int ld_icp_dump(struct ld_softc *, void *, int, int); 68 static int ld_icp_flush(struct ld_softc *, bool); 69 static int ld_icp_ioctl(struct ld_softc *, u_long, void *, int32_t, bool); 70 static void ld_icp_intr(struct icp_ccb *); 71 static int ld_icp_match(device_t, cfdata_t, void *); 72 static int ld_icp_start(struct ld_softc *, struct buf *); 73 74 static void ld_icp_adjqparam(device_t, int); 75 76 CFATTACH_DECL_NEW(ld_icp, sizeof(struct ld_icp_softc), 77 ld_icp_match, ld_icp_attach, ld_icp_detach, NULL); 78 79 static const struct icp_servicecb ld_icp_servicecb = { 80 ld_icp_adjqparam, 81 }; 82 83 static int 84 ld_icp_match(device_t parent, cfdata_t match, void *aux) 85 { 86 struct icp_attach_args *icpa; 87 88 icpa = aux; 89 90 return (icpa->icpa_unit < ICPA_UNIT_SCSI); 91 } 92 93 static void 94 ld_icp_attach(device_t parent, device_t self, void *aux) 95 { 96 struct icp_attach_args *icpa = aux; 97 struct ld_icp_softc *sc = device_private(self); 98 struct ld_softc *ld = &sc->sc_ld; 99 struct icp_softc *icp = device_private(parent); 100 struct icp_cachedrv *cd = &icp->icp_cdr[icpa->icpa_unit]; 101 struct icp_cdevinfo *cdi; 102 const char *str; 103 int t; 104 105 ld->sc_dv = self; 106 107 icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb); 108 109 sc->sc_hwunit = icpa->icpa_unit; 110 ld->sc_maxxfer = ICP_MAX_XFER; 111 ld->sc_secsize = ICP_SECTOR_SIZE; 112 ld->sc_start = ld_icp_start; 113 ld->sc_dump = ld_icp_dump; 114 ld->sc_ioctl = ld_icp_ioctl; 115 ld->sc_secperunit = cd->cd_size; 116 ld->sc_flags = LDF_ENABLED; 117 ld->sc_maxqueuecnt = icp->icp_openings; 118 119 if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO, 120 sc->sc_hwunit, sizeof(struct icp_cdevinfo))) { 121 aprint_error(": unable to retrieve device info\n"); 122 ld->sc_flags = LDF_ENABLED; 123 goto out; 124 } 125 cdi = (struct icp_cdevinfo *)icp->icp_scr; 126 127 aprint_normal(": <%.8s>, ", cdi->ld_name); 128 t = le32toh(cdi->ld_dtype) >> 16; 129 130 /* 131 * Print device type. 132 */ 133 if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1) 134 str = "RAID-1"; 135 else if (t == 0) 136 str = "JBOD"; 137 else if (t == 1) 138 str = "RAID-0"; 139 else if (t == 2) 140 str = "Chain"; 141 else 142 str = "unknown type"; 143 144 aprint_normal("type: %s, ", str); 145 146 /* 147 * Print device status. 148 */ 149 if (t > 2) 150 str = "missing"; 151 else if ((cdi->ld_error & 1) != 0) { 152 str = "fault"; 153 ld->sc_flags = LDF_ENABLED; 154 } else if ((cdi->ld_error & 2) != 0) 155 str = "invalid"; 156 else { 157 str = "optimal"; 158 ld->sc_flags = LDF_ENABLED; 159 } 160 161 aprint_normal("status: %s\n", str); 162 163 out: 164 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT); 165 } 166 167 static int 168 ld_icp_detach(device_t dv, int flags) 169 { 170 int rv; 171 172 if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0) 173 return (rv); 174 ldenddetach((struct ld_softc *) dv); 175 176 return (0); 177 } 178 179 static int 180 ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno, 181 int dowrite, struct buf *bp) 182 { 183 struct icp_cachecmd *cc; 184 struct icp_ccb *ic; 185 struct icp_softc *icp; 186 int s, rv; 187 188 icp = device_private(device_parent(sc->sc_ld.sc_dv)); 189 190 /* 191 * Allocate a command control block. 192 */ 193 if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) 194 return (EAGAIN); 195 196 /* 197 * Map the data transfer. 198 */ 199 cc = &ic->ic_cmd.cmd_packet.cc; 200 ic->ic_sg = cc->cc_sg; 201 ic->ic_service = ICP_CACHESERVICE; 202 203 rv = icp_ccb_map(icp, ic, data, datasize, 204 dowrite ? IC_XFER_OUT : IC_XFER_IN); 205 if (rv != 0) { 206 icp_ccb_free(icp, ic); 207 return (rv); 208 } 209 210 /* 211 * Build the command. 212 */ 213 ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ)); 214 cc->cc_deviceno = htole16(sc->sc_hwunit); 215 cc->cc_blockno = htole32(blkno); 216 cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE); 217 cc->cc_addr = ~0; /* scatter gather */ 218 cc->cc_nsgent = htole32(ic->ic_nsgent); 219 220 ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd + 221 ic->ic_nsgent * sizeof(*ic->ic_sg); 222 223 /* 224 * Fire it off to the controller. 225 */ 226 if (bp == NULL) { 227 s = splbio(); 228 rv = icp_ccb_poll(icp, ic, 10000); 229 icp_ccb_unmap(icp, ic); 230 icp_ccb_free(icp, ic); 231 splx(s); 232 } else { 233 ic->ic_intr = ld_icp_intr; 234 ic->ic_context = bp; 235 ic->ic_dv = sc->sc_ld.sc_dv; 236 icp_ccb_enqueue(icp, ic); 237 } 238 239 return (rv); 240 } 241 242 static int 243 ld_icp_start(struct ld_softc *ld, struct buf *bp) 244 { 245 246 return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data, 247 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp)); 248 } 249 250 static int 251 ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 252 { 253 254 return (ld_icp_dobio((struct ld_icp_softc *)ld, data, 255 blkcnt * ld->sc_secsize, blkno, 1, NULL)); 256 } 257 258 /* ARGSUSED */ 259 static int 260 ld_icp_flush(struct ld_softc *ld, bool poll) 261 { 262 struct ld_icp_softc *sc; 263 struct icp_softc *icp; 264 struct icp_cachecmd *cc; 265 struct icp_ccb *ic; 266 int rv; 267 268 sc = (struct ld_icp_softc *)ld; 269 icp = device_private(device_parent(ld->sc_dv)); 270 271 ic = icp_ccb_alloc_wait(icp); 272 ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH); 273 274 cc = &ic->ic_cmd.cmd_packet.cc; 275 cc->cc_deviceno = htole16(sc->sc_hwunit); 276 cc->cc_blockno = htole32(1); 277 cc->cc_blockcnt = 0; 278 cc->cc_addr = 0; 279 cc->cc_nsgent = 0; 280 281 ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd; 282 ic->ic_service = ICP_CACHESERVICE; 283 284 rv = icp_ccb_wait(icp, ic, 30000); 285 icp_ccb_free(icp, ic); 286 287 return (rv); 288 } 289 290 static int 291 ld_icp_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll) 292 { 293 int error; 294 295 switch (cmd) { 296 case DIOCCACHESYNC: 297 error = ld_icp_flush(ld, poll); 298 break; 299 300 default: 301 error = EPASSTHROUGH; 302 break; 303 } 304 305 return error; 306 } 307 308 static void 309 ld_icp_intr(struct icp_ccb *ic) 310 { 311 struct buf *bp; 312 struct ld_icp_softc *sc; 313 struct icp_softc *icp; 314 315 bp = ic->ic_context; 316 sc = device_private(ic->ic_dv); 317 icp = device_private(device_parent(sc->sc_ld.sc_dv)); 318 319 if (ic->ic_status != ICP_S_OK) { 320 aprint_error_dev(ic->ic_dv, "request failed; status=0x%04x\n", 321 ic->ic_status); 322 bp->b_error = EIO; 323 bp->b_resid = bp->b_bcount; 324 325 icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync); 326 icp->icp_evt.eu.sync.ionode = device_unit(icp->icp_dv); 327 icp->icp_evt.eu.sync.service = icp->icp_service; 328 icp->icp_evt.eu.sync.status = icp->icp_status; 329 icp->icp_evt.eu.sync.info = icp->icp_info; 330 icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit; 331 if (icp->icp_status >= 0x8000) 332 icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt); 333 else 334 icp_store_event(icp, GDT_ES_SYNC, icp->icp_service, 335 &icp->icp_evt); 336 } else 337 bp->b_resid = 0; 338 339 icp_ccb_unmap(icp, ic); 340 icp_ccb_free(icp, ic); 341 lddone(&sc->sc_ld, bp); 342 } 343 344 static void 345 ld_icp_adjqparam(device_t dv, int openings) 346 { 347 348 ldadjqparam((struct ld_softc *) dv, openings); 349 } 350 351 MODULE(MODULE_CLASS_DRIVER, ld_icp, "ld"); /* no icp module yet */ 352 353 #ifdef _MODULE 354 /* 355 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd" 356 * XXX it will be defined in the common-code module 357 */ 358 #undef CFDRIVER_DECL 359 #define CFDRIVER_DECL(name, class, attr) 360 #include "ioconf.c" 361 #endif 362 363 static int 364 ld_icp_modcmd(modcmd_t cmd, void *opaque) 365 { 366 #ifdef _MODULE 367 /* 368 * We ignore the cfdriver_vec[] that ioconf provides, since 369 * the cfdrivers are attached already. 370 */ 371 static struct cfdriver * const no_cfdriver_vec[] = { NULL }; 372 #endif 373 int error = 0; 374 375 #ifdef _MODULE 376 switch (cmd) { 377 case MODULE_CMD_INIT: 378 error = config_init_component(no_cfdriver_vec, 379 cfattach_ioconf_ld_icp, cfdata_ioconf_ld_icp); 380 break; 381 case MODULE_CMD_FINI: 382 error = config_fini_component(no_cfdriver_vec, 383 cfattach_ioconf_ld_icp, cfdata_ioconf_ld_icp); 384 break; 385 default: 386 error = ENOTTY; 387 break; 388 } 389 #endif 390 391 return error; 392 } 393