1 1.42 rin /* $NetBSD: ld_iop.c,v 1.42 2025/04/13 02:34:03 rin Exp $ */ 2 1.1 ad 3 1.1 ad /*- 4 1.6 ad * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc. 5 1.1 ad * All rights reserved. 6 1.1 ad * 7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 ad * by Andrew Doran. 9 1.1 ad * 10 1.1 ad * Redistribution and use in source and binary forms, with or without 11 1.1 ad * modification, are permitted provided that the following conditions 12 1.1 ad * are met: 13 1.1 ad * 1. Redistributions of source code must retain the above copyright 14 1.1 ad * notice, this list of conditions and the following disclaimer. 15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 ad * notice, this list of conditions and the following disclaimer in the 17 1.1 ad * documentation and/or other materials provided with the distribution. 18 1.1 ad * 19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 ad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 ad */ 31 1.1 ad 32 1.1 ad /* 33 1.1 ad * I2O front-end for ld(4) driver, supporting random block storage class 34 1.2 ad * devices. Currently, this doesn't handle anything more complex than 35 1.2 ad * fixed direct-access devices. 36 1.1 ad */ 37 1.10 lukem 38 1.10 lukem #include <sys/cdefs.h> 39 1.42 rin __KERNEL_RCSID(0, "$NetBSD: ld_iop.c,v 1.42 2025/04/13 02:34:03 rin Exp $"); 40 1.1 ad 41 1.1 ad #include <sys/param.h> 42 1.1 ad #include <sys/systm.h> 43 1.1 ad #include <sys/kernel.h> 44 1.1 ad #include <sys/device.h> 45 1.1 ad #include <sys/buf.h> 46 1.15 yamt #include <sys/bufq.h> 47 1.1 ad #include <sys/endian.h> 48 1.1 ad #include <sys/dkio.h> 49 1.1 ad #include <sys/disk.h> 50 1.1 ad #include <sys/proc.h> 51 1.1 ad 52 1.26 ad #include <sys/bus.h> 53 1.1 ad 54 1.1 ad #include <dev/ldvar.h> 55 1.1 ad 56 1.1 ad #include <dev/i2o/i2o.h> 57 1.6 ad #include <dev/i2o/iopio.h> 58 1.1 ad #include <dev/i2o/iopvar.h> 59 1.1 ad 60 1.6 ad #define LD_IOP_TIMEOUT 30*1000 61 1.6 ad 62 1.6 ad #define LD_IOP_CLAIMED 0x01 63 1.6 ad #define LD_IOP_NEW_EVTMASK 0x02 64 1.1 ad 65 1.1 ad struct ld_iop_softc { 66 1.1 ad struct ld_softc sc_ld; 67 1.1 ad struct iop_initiator sc_ii; 68 1.2 ad struct iop_initiator sc_eventii; 69 1.6 ad int sc_flags; 70 1.1 ad }; 71 1.1 ad 72 1.32 tron static void ld_iop_adjqparam(device_t, int); 73 1.32 tron static void ld_iop_attach(device_t, device_t, void *); 74 1.32 tron static int ld_iop_detach(device_t, int); 75 1.42 rin static int ld_iop_dump(struct ld_softc *, void *, daddr_t, int); 76 1.37 jdolecek static int ld_iop_flush(struct ld_softc *, bool); 77 1.37 jdolecek static int ld_iop_ioctl(struct ld_softc *, u_long, void *, int32_t, bool); 78 1.32 tron static void ld_iop_intr(device_t, struct iop_msg *, void *); 79 1.32 tron static void ld_iop_intr_event(device_t, struct iop_msg *, void *); 80 1.32 tron static int ld_iop_match(device_t, cfdata_t, void *); 81 1.1 ad static int ld_iop_start(struct ld_softc *, struct buf *); 82 1.6 ad static void ld_iop_unconfig(struct ld_iop_softc *, int); 83 1.1 ad 84 1.32 tron CFATTACH_DECL_NEW(ld_iop, sizeof(struct ld_iop_softc), 85 1.13 thorpej ld_iop_match, ld_iop_attach, ld_iop_detach, NULL); 86 1.1 ad 87 1.16 perry static const char * const ld_iop_errors[] = { 88 1.16 perry "success", 89 1.16 perry "media error", 90 1.6 ad "access error", 91 1.1 ad "device failure", 92 1.6 ad "device not ready", 93 1.1 ad "media not present", 94 1.6 ad "media locked", 95 1.1 ad "media failure", 96 1.6 ad "protocol failure", 97 1.6 ad "bus failure", 98 1.6 ad "access violation", 99 1.6 ad "media write protected", 100 1.1 ad "device reset", 101 1.6 ad "volume changed, waiting for acknowledgement", 102 1.6 ad "timeout", 103 1.1 ad }; 104 1.1 ad 105 1.1 ad static int 106 1.32 tron ld_iop_match(device_t parent, cfdata_t match, void *aux) 107 1.1 ad { 108 1.1 ad struct iop_attach_args *ia; 109 1.1 ad 110 1.1 ad ia = aux; 111 1.1 ad 112 1.3 ad return (ia->ia_class == I2O_CLASS_RANDOM_BLOCK_STORAGE); 113 1.1 ad } 114 1.1 ad 115 1.1 ad static void 116 1.32 tron ld_iop_attach(device_t parent, device_t self, void *aux) 117 1.1 ad { 118 1.32 tron struct iop_attach_args *ia = aux; 119 1.32 tron struct ld_iop_softc *sc = device_private(self); 120 1.32 tron struct iop_softc *iop = device_private(parent); 121 1.32 tron struct ld_softc *ld = &sc->sc_ld; 122 1.2 ad int rv, evreg, enable; 123 1.17 christos const char *typestr, *fixedstr; 124 1.1 ad u_int cachesz; 125 1.9 ad u_int32_t timeoutbase, rwvtimeoutbase, rwvtimeout; 126 1.1 ad struct { 127 1.1 ad struct i2o_param_op_results pr; 128 1.1 ad struct i2o_param_read_results prr; 129 1.1 ad union { 130 1.1 ad struct i2o_param_rbs_cache_control cc; 131 1.1 ad struct i2o_param_rbs_device_info bdi; 132 1.1 ad } p; 133 1.31 gmcgarry } __packed param; 134 1.1 ad 135 1.32 tron ld->sc_dv = self; 136 1.2 ad evreg = 0; 137 1.1 ad 138 1.1 ad /* Register us as an initiator. */ 139 1.1 ad sc->sc_ii.ii_dv = self; 140 1.1 ad sc->sc_ii.ii_intr = ld_iop_intr; 141 1.6 ad sc->sc_ii.ii_adjqparam = ld_iop_adjqparam; 142 1.1 ad sc->sc_ii.ii_flags = 0; 143 1.2 ad sc->sc_ii.ii_tid = ia->ia_tid; 144 1.6 ad iop_initiator_register(iop, &sc->sc_ii); 145 1.1 ad 146 1.2 ad /* Register another initiator to handle events from the device. */ 147 1.2 ad sc->sc_eventii.ii_dv = self; 148 1.2 ad sc->sc_eventii.ii_intr = ld_iop_intr_event; 149 1.8 ad sc->sc_eventii.ii_flags = II_NOTCTX | II_UTILITY; 150 1.2 ad sc->sc_eventii.ii_tid = ia->ia_tid; 151 1.6 ad iop_initiator_register(iop, &sc->sc_eventii); 152 1.6 ad 153 1.6 ad rv = iop_util_eventreg(iop, &sc->sc_eventii, 154 1.16 perry I2O_EVENT_GEN_EVENT_MASK_MODIFIED | 155 1.6 ad I2O_EVENT_GEN_DEVICE_RESET | 156 1.6 ad I2O_EVENT_GEN_STATE_CHANGE | 157 1.6 ad I2O_EVENT_GEN_GENERAL_WARNING); 158 1.6 ad if (rv != 0) { 159 1.27 cegger aprint_error_dev(self, "unable to register for events"); 160 1.2 ad goto bad; 161 1.2 ad } 162 1.2 ad evreg = 1; 163 1.2 ad 164 1.6 ad /* 165 1.6 ad * Start out with one queued command. The `iop' driver will adjust 166 1.6 ad * the queue parameters once we're up and running. 167 1.6 ad */ 168 1.6 ad ld->sc_maxqueuecnt = 1; 169 1.6 ad 170 1.1 ad ld->sc_maxxfer = IOP_MAX_XFER; 171 1.1 ad ld->sc_dump = ld_iop_dump; 172 1.37 jdolecek ld->sc_ioctl = ld_iop_ioctl; 173 1.1 ad ld->sc_start = ld_iop_start; 174 1.39 mlelstv ld->sc_flags = LDF_MPSAFE; 175 1.1 ad 176 1.1 ad /* Say what the device is. */ 177 1.6 ad printf(":"); 178 1.6 ad iop_print_ident(iop, ia->ia_tid); 179 1.1 ad 180 1.2 ad /* 181 1.2 ad * Claim the device so that we don't get any nasty surprises. Allow 182 1.2 ad * failure. 183 1.2 ad */ 184 1.6 ad rv = iop_util_claim(iop, &sc->sc_ii, 0, 185 1.1 ad I2O_UTIL_CLAIM_CAPACITY_SENSITIVE | 186 1.1 ad I2O_UTIL_CLAIM_NO_PEER_SERVICE | 187 1.1 ad I2O_UTIL_CLAIM_NO_MANAGEMENT_SERVICE | 188 1.1 ad I2O_UTIL_CLAIM_PRIMARY_USER); 189 1.6 ad sc->sc_flags = rv ? 0 : LD_IOP_CLAIMED; 190 1.1 ad 191 1.9 ad rv = iop_field_get_all(iop, ia->ia_tid, I2O_PARAM_RBS_DEVICE_INFO, 192 1.9 ad ¶m, sizeof(param), NULL); 193 1.9 ad if (rv != 0) 194 1.1 ad goto bad; 195 1.1 ad 196 1.1 ad ld->sc_secsize = le32toh(param.p.bdi.blocksize); 197 1.1 ad ld->sc_secperunit = (int) 198 1.1 ad (le64toh(param.p.bdi.capacity) / ld->sc_secsize); 199 1.1 ad 200 1.1 ad switch (param.p.bdi.type) { 201 1.1 ad case I2O_RBS_TYPE_DIRECT: 202 1.1 ad typestr = "direct access"; 203 1.2 ad enable = 1; 204 1.1 ad break; 205 1.1 ad case I2O_RBS_TYPE_WORM: 206 1.1 ad typestr = "WORM"; 207 1.2 ad enable = 0; 208 1.1 ad break; 209 1.1 ad case I2O_RBS_TYPE_CDROM: 210 1.6 ad typestr = "CD-ROM"; 211 1.2 ad enable = 0; 212 1.1 ad break; 213 1.1 ad case I2O_RBS_TYPE_OPTICAL: 214 1.1 ad typestr = "optical"; 215 1.2 ad enable = 0; 216 1.1 ad break; 217 1.1 ad default: 218 1.1 ad typestr = "unknown"; 219 1.2 ad enable = 0; 220 1.1 ad break; 221 1.1 ad } 222 1.1 ad 223 1.14 wiz if ((le32toh(param.p.bdi.capabilities) & I2O_RBS_CAP_REMOVABLE_MEDIA) 224 1.1 ad != 0) { 225 1.39 mlelstv /* ld->sc_flags |= LDF_REMOVABLE; */ 226 1.14 wiz fixedstr = "removable"; 227 1.2 ad enable = 0; 228 1.2 ad } else 229 1.1 ad fixedstr = "fixed"; 230 1.2 ad 231 1.6 ad printf(" %s, %s", typestr, fixedstr); 232 1.1 ad 233 1.1 ad /* 234 1.41 rillig * Determine if the device has a private cache. If so, print the 235 1.1 ad * cache size. Even if the device doesn't appear to have a cache, 236 1.6 ad * we perform a flush at shutdown. 237 1.1 ad */ 238 1.9 ad rv = iop_field_get_all(iop, ia->ia_tid, I2O_PARAM_RBS_CACHE_CONTROL, 239 1.9 ad ¶m, sizeof(param), NULL); 240 1.9 ad if (rv != 0) 241 1.1 ad goto bad; 242 1.1 ad 243 1.1 ad if ((cachesz = le32toh(param.p.cc.totalcachesize)) != 0) 244 1.1 ad printf(", %dkB cache", cachesz >> 10); 245 1.1 ad 246 1.2 ad printf("\n"); 247 1.2 ad 248 1.2 ad /* 249 1.2 ad * Configure the DDM's timeout functions to time out all commands 250 1.6 ad * after 30 seconds. 251 1.2 ad */ 252 1.16 perry timeoutbase = htole32(LD_IOP_TIMEOUT * 1000); 253 1.16 perry rwvtimeoutbase = htole32(LD_IOP_TIMEOUT * 1000); 254 1.9 ad rwvtimeout = 0; 255 1.9 ad 256 1.9 ad iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION, 257 1.9 ad &timeoutbase, sizeof(timeoutbase), 258 1.9 ad I2O_PARAM_RBS_OPERATION_timeoutbase); 259 1.9 ad iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION, 260 1.9 ad &rwvtimeoutbase, sizeof(rwvtimeoutbase), 261 1.9 ad I2O_PARAM_RBS_OPERATION_rwvtimeoutbase); 262 1.9 ad iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION, 263 1.9 ad &rwvtimeout, sizeof(rwvtimeout), 264 1.9 ad I2O_PARAM_RBS_OPERATION_rwvtimeoutbase); 265 1.2 ad 266 1.2 ad if (enable) 267 1.2 ad ld->sc_flags |= LDF_ENABLED; 268 1.2 ad else 269 1.27 cegger aprint_error_dev(self, "device not yet supported\n"); 270 1.2 ad 271 1.36 jdolecek ldattach(ld, BUFQ_DISK_DEFAULT_STRAT); 272 1.1 ad return; 273 1.1 ad 274 1.6 ad bad: 275 1.6 ad ld_iop_unconfig(sc, evreg); 276 1.6 ad } 277 1.6 ad 278 1.6 ad static void 279 1.6 ad ld_iop_unconfig(struct ld_iop_softc *sc, int evreg) 280 1.6 ad { 281 1.6 ad struct iop_softc *iop; 282 1.6 ad 283 1.32 tron iop = device_private(device_parent(sc->sc_ld.sc_dv)); 284 1.6 ad 285 1.6 ad if ((sc->sc_flags & LD_IOP_CLAIMED) != 0) 286 1.2 ad iop_util_claim(iop, &sc->sc_ii, 1, 287 1.2 ad I2O_UTIL_CLAIM_PRIMARY_USER); 288 1.6 ad 289 1.6 ad if (evreg) { 290 1.6 ad /* 291 1.16 perry * Mask off events, and wait up to 5 seconds for a reply. 292 1.6 ad * Note that some adapters won't reply to this (XXX We 293 1.6 ad * should check the event capabilities). 294 1.6 ad */ 295 1.24 ad mutex_spin_enter(&iop->sc_intrlock); 296 1.6 ad sc->sc_flags &= ~LD_IOP_NEW_EVTMASK; 297 1.24 ad mutex_spin_exit(&iop->sc_intrlock); 298 1.24 ad 299 1.6 ad iop_util_eventreg(iop, &sc->sc_eventii, 300 1.6 ad I2O_EVENT_GEN_EVENT_MASK_MODIFIED); 301 1.24 ad 302 1.24 ad mutex_spin_enter(&iop->sc_intrlock); 303 1.6 ad if ((sc->sc_flags & LD_IOP_NEW_EVTMASK) == 0) 304 1.24 ad cv_timedwait(&sc->sc_eventii.ii_cv, 305 1.24 ad &iop->sc_intrlock, hz * 5); 306 1.24 ad mutex_spin_exit(&iop->sc_intrlock); 307 1.6 ad } 308 1.6 ad 309 1.6 ad iop_initiator_unregister(iop, &sc->sc_eventii); 310 1.1 ad iop_initiator_unregister(iop, &sc->sc_ii); 311 1.1 ad } 312 1.1 ad 313 1.1 ad static int 314 1.32 tron ld_iop_detach(device_t self, int flags) 315 1.2 ad { 316 1.2 ad struct ld_iop_softc *sc; 317 1.2 ad struct iop_softc *iop; 318 1.4 ad int rv; 319 1.2 ad 320 1.20 thorpej sc = device_private(self); 321 1.20 thorpej iop = device_private(device_parent(self)); 322 1.2 ad 323 1.5 ad if ((rv = ldbegindetach(&sc->sc_ld, flags)) != 0) 324 1.4 ad return (rv); 325 1.2 ad 326 1.2 ad /* 327 1.2 ad * Abort any requests queued with the IOP, but allow requests that 328 1.2 ad * are already in progress to complete. 329 1.2 ad */ 330 1.2 ad if ((sc->sc_ld.sc_flags & LDF_ENABLED) != 0) 331 1.2 ad iop_util_abort(iop, &sc->sc_ii, 0, 0, 332 1.2 ad I2O_UTIL_ABORT_WILD | I2O_UTIL_ABORT_CLEAN); 333 1.2 ad 334 1.5 ad ldenddetach(&sc->sc_ld); 335 1.2 ad 336 1.6 ad /* Un-claim the target, and un-register our initiators. */ 337 1.6 ad if ((sc->sc_ld.sc_flags & LDF_ENABLED) != 0) 338 1.6 ad ld_iop_unconfig(sc, 1); 339 1.2 ad 340 1.2 ad return (0); 341 1.2 ad } 342 1.2 ad 343 1.2 ad static int 344 1.1 ad ld_iop_start(struct ld_softc *ld, struct buf *bp) 345 1.1 ad { 346 1.1 ad struct iop_msg *im; 347 1.1 ad struct iop_softc *iop; 348 1.1 ad struct ld_iop_softc *sc; 349 1.6 ad struct i2o_rbs_block_read *mf; 350 1.6 ad u_int rv, flags, write; 351 1.1 ad u_int64_t ba; 352 1.6 ad u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)]; 353 1.1 ad 354 1.33 mhitch sc = device_private(ld->sc_dv); 355 1.32 tron iop = device_private(device_parent(ld->sc_dv)); 356 1.1 ad 357 1.8 ad im = iop_msg_alloc(iop, 0); 358 1.1 ad im->im_dvcontext = bp; 359 1.1 ad 360 1.1 ad write = ((bp->b_flags & B_READ) == 0); 361 1.1 ad ba = (u_int64_t)bp->b_rawblkno * ld->sc_secsize; 362 1.1 ad 363 1.2 ad /* 364 1.2 ad * Write through the cache when performing synchronous writes. When 365 1.2 ad * performing a read, we don't request that the DDM cache the data, 366 1.2 ad * as there's little advantage to it. 367 1.2 ad */ 368 1.1 ad if (write) { 369 1.2 ad if ((bp->b_flags & B_ASYNC) == 0) 370 1.1 ad flags = I2O_RBS_BLOCK_WRITE_CACHE_WT; 371 1.1 ad else 372 1.1 ad flags = I2O_RBS_BLOCK_WRITE_CACHE_WB; 373 1.2 ad } else 374 1.2 ad flags = 0; 375 1.1 ad 376 1.1 ad /* 377 1.1 ad * Fill the message frame. We can use the block_read structure for 378 1.1 ad * both reads and writes, as it's almost identical to the 379 1.1 ad * block_write structure. 380 1.1 ad */ 381 1.6 ad mf = (struct i2o_rbs_block_read *)mb; 382 1.6 ad mf->msgflags = I2O_MSGFLAGS(i2o_rbs_block_read); 383 1.6 ad mf->msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, 384 1.1 ad write ? I2O_RBS_BLOCK_WRITE : I2O_RBS_BLOCK_READ); 385 1.6 ad mf->msgictx = sc->sc_ii.ii_ictx; 386 1.6 ad mf->msgtctx = im->im_tctx; 387 1.6 ad mf->flags = flags | (1 << 16); /* flags & time multiplier */ 388 1.6 ad mf->datasize = bp->b_bcount; 389 1.6 ad mf->lowoffset = (u_int32_t)ba; 390 1.6 ad mf->highoffset = (u_int32_t)(ba >> 32); 391 1.6 ad 392 1.6 ad /* Map the data transfer and enqueue the command. */ 393 1.6 ad rv = iop_msg_map_bio(iop, im, mb, bp->b_data, bp->b_bcount, write); 394 1.6 ad if (rv == 0) { 395 1.8 ad if ((rv = iop_post(iop, mb)) != 0) { 396 1.6 ad iop_msg_unmap(iop, im); 397 1.6 ad iop_msg_free(iop, im); 398 1.6 ad } 399 1.6 ad } 400 1.1 ad return (rv); 401 1.1 ad } 402 1.1 ad 403 1.1 ad static int 404 1.42 rin ld_iop_dump(struct ld_softc *ld, void *data, daddr_t blkno, int blkcnt) 405 1.1 ad { 406 1.1 ad struct iop_msg *im; 407 1.1 ad struct iop_softc *iop; 408 1.1 ad struct ld_iop_softc *sc; 409 1.6 ad struct i2o_rbs_block_write *mf; 410 1.1 ad int rv, bcount; 411 1.1 ad u_int64_t ba; 412 1.6 ad u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)]; 413 1.1 ad 414 1.33 mhitch sc = device_private(ld->sc_dv); 415 1.32 tron iop = device_private(device_parent(ld->sc_dv)); 416 1.1 ad bcount = blkcnt * ld->sc_secsize; 417 1.1 ad ba = (u_int64_t)blkno * ld->sc_secsize; 418 1.8 ad im = iop_msg_alloc(iop, IM_POLL); 419 1.1 ad 420 1.6 ad mf = (struct i2o_rbs_block_write *)mb; 421 1.6 ad mf->msgflags = I2O_MSGFLAGS(i2o_rbs_block_write); 422 1.6 ad mf->msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_RBS_BLOCK_WRITE); 423 1.6 ad mf->msgictx = sc->sc_ii.ii_ictx; 424 1.6 ad mf->msgtctx = im->im_tctx; 425 1.6 ad mf->flags = I2O_RBS_BLOCK_WRITE_CACHE_WT | (1 << 16); 426 1.6 ad mf->datasize = bcount; 427 1.6 ad mf->lowoffset = (u_int32_t)ba; 428 1.6 ad mf->highoffset = (u_int32_t)(ba >> 32); 429 1.1 ad 430 1.8 ad if ((rv = iop_msg_map(iop, im, mb, data, bcount, 1, NULL)) != 0) { 431 1.6 ad iop_msg_free(iop, im); 432 1.1 ad return (rv); 433 1.1 ad } 434 1.1 ad 435 1.6 ad rv = iop_msg_post(iop, im, mb, LD_IOP_TIMEOUT * 2); 436 1.1 ad iop_msg_unmap(iop, im); 437 1.6 ad iop_msg_free(iop, im); 438 1.1 ad return (rv); 439 1.1 ad } 440 1.1 ad 441 1.1 ad static int 442 1.37 jdolecek ld_iop_flush(struct ld_softc *ld, bool poll) 443 1.1 ad { 444 1.1 ad struct iop_msg *im; 445 1.1 ad struct iop_softc *iop; 446 1.1 ad struct ld_iop_softc *sc; 447 1.6 ad struct i2o_rbs_cache_flush mf; 448 1.1 ad int rv; 449 1.1 ad 450 1.33 mhitch sc = device_private(ld->sc_dv); 451 1.32 tron iop = device_private(device_parent(ld->sc_dv)); 452 1.38 mlelstv im = iop_msg_alloc(iop, poll ? IM_POLL : IM_WAIT); 453 1.1 ad 454 1.6 ad mf.msgflags = I2O_MSGFLAGS(i2o_rbs_cache_flush); 455 1.6 ad mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_RBS_CACHE_FLUSH); 456 1.6 ad mf.msgictx = sc->sc_ii.ii_ictx; 457 1.6 ad mf.msgtctx = im->im_tctx; 458 1.6 ad mf.flags = 1 << 16; /* time multiplier */ 459 1.1 ad 460 1.29 simonb /* Ancient disks will return an error here. */ 461 1.6 ad rv = iop_msg_post(iop, im, &mf, LD_IOP_TIMEOUT * 2); 462 1.6 ad iop_msg_free(iop, im); 463 1.1 ad return (rv); 464 1.1 ad } 465 1.1 ad 466 1.37 jdolecek static int 467 1.37 jdolecek ld_iop_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll) 468 1.37 jdolecek { 469 1.37 jdolecek int error; 470 1.37 jdolecek 471 1.37 jdolecek switch (cmd) { 472 1.37 jdolecek case DIOCCACHESYNC: 473 1.37 jdolecek error = ld_iop_flush(ld, poll); 474 1.37 jdolecek break; 475 1.37 jdolecek 476 1.37 jdolecek default: 477 1.37 jdolecek error = EPASSTHROUGH; 478 1.37 jdolecek break; 479 1.37 jdolecek } 480 1.37 jdolecek 481 1.37 jdolecek return error; 482 1.37 jdolecek } 483 1.37 jdolecek 484 1.37 jdolecek static void 485 1.32 tron ld_iop_intr(device_t dv, struct iop_msg *im, void *reply) 486 1.1 ad { 487 1.1 ad struct i2o_rbs_reply *rb; 488 1.1 ad struct buf *bp; 489 1.1 ad struct ld_iop_softc *sc; 490 1.1 ad struct iop_softc *iop; 491 1.6 ad int err, detail; 492 1.1 ad const char *errstr; 493 1.1 ad 494 1.1 ad rb = reply; 495 1.1 ad bp = im->im_dvcontext; 496 1.33 mhitch sc = device_private(dv); 497 1.32 tron iop = device_private(device_parent(dv)); 498 1.1 ad 499 1.6 ad err = ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0); 500 1.6 ad 501 1.6 ad if (!err && rb->reqstatus != I2O_STATUS_SUCCESS) { 502 1.6 ad detail = le16toh(rb->detail); 503 1.21 christos if (detail >= __arraycount(ld_iop_errors)) 504 1.6 ad errstr = "<unknown>"; 505 1.1 ad else 506 1.1 ad errstr = ld_iop_errors[detail]; 507 1.27 cegger aprint_error_dev(dv, "error 0x%04x: %s\n", detail, errstr); 508 1.6 ad err = 1; 509 1.6 ad } 510 1.6 ad 511 1.6 ad if (err) { 512 1.1 ad bp->b_error = EIO; 513 1.1 ad bp->b_resid = bp->b_bcount; 514 1.1 ad } else 515 1.6 ad bp->b_resid = bp->b_bcount - le32toh(rb->transfercount); 516 1.1 ad 517 1.1 ad iop_msg_unmap(iop, im); 518 1.6 ad iop_msg_free(iop, im); 519 1.1 ad lddone(&sc->sc_ld, bp); 520 1.2 ad } 521 1.2 ad 522 1.2 ad static void 523 1.32 tron ld_iop_intr_event(device_t dv, struct iop_msg *im, void *reply) 524 1.2 ad { 525 1.2 ad struct i2o_util_event_register_reply *rb; 526 1.6 ad struct ld_iop_softc *sc; 527 1.24 ad struct iop_softc *iop; 528 1.2 ad u_int event; 529 1.2 ad 530 1.2 ad rb = reply; 531 1.6 ad 532 1.6 ad if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) 533 1.6 ad return; 534 1.6 ad 535 1.2 ad event = le32toh(rb->event); 536 1.33 mhitch sc = device_private(dv); 537 1.2 ad 538 1.6 ad if (event == I2O_EVENT_GEN_EVENT_MASK_MODIFIED) { 539 1.24 ad iop = device_private(device_parent(dv)); 540 1.24 ad mutex_spin_enter(&iop->sc_intrlock); 541 1.6 ad sc->sc_flags |= LD_IOP_NEW_EVTMASK; 542 1.24 ad cv_broadcast(&sc->sc_eventii.ii_cv); 543 1.24 ad mutex_spin_exit(&iop->sc_intrlock); 544 1.2 ad return; 545 1.6 ad } 546 1.2 ad 547 1.27 cegger printf("%s: event 0x%08x received\n", device_xname(dv), event); 548 1.6 ad } 549 1.6 ad 550 1.6 ad static void 551 1.32 tron ld_iop_adjqparam(device_t dv, int mpi) 552 1.6 ad { 553 1.32 tron struct ld_iop_softc *sc = device_private(dv); 554 1.32 tron struct iop_softc *iop = device_private(device_parent(dv)); 555 1.32 tron struct ld_softc *ld = &sc->sc_ld; 556 1.6 ad 557 1.6 ad /* 558 1.40 ad * AMI controllers seem to lose the plot if you hand off lots of 559 1.6 ad * queued commands. 560 1.6 ad */ 561 1.6 ad if (le16toh(I2O_ORG_AMI) == iop->sc_status.orgid && mpi > 64) 562 1.6 ad mpi = 64; 563 1.6 ad 564 1.32 tron ldadjqparam(ld, mpi); 565 1.1 ad } 566