| 1 | /* $NetBSD: ciss.c,v 1.36 2016/07/14 04:00:45 msaitoh Exp $ */ |
| 2 | /* $OpenBSD: ciss.c,v 1.68 2013/05/30 16:15:02 deraadt Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2005,2006 Michael Shalayeff |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN |
| 17 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 18 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | */ |
| 20 | |
| 21 | #include <sys/cdefs.h> |
| 22 | __KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.36 2016/07/14 04:00:45 msaitoh Exp $" ); |
| 23 | |
| 24 | #include "bio.h" |
| 25 | |
| 26 | /* #define CISS_DEBUG */ |
| 27 | |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/systm.h> |
| 30 | #include <sys/buf.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/device.h> |
| 33 | #include <sys/kernel.h> |
| 34 | #include <sys/malloc.h> |
| 35 | #include <sys/proc.h> |
| 36 | |
| 37 | #include <sys/bus.h> |
| 38 | |
| 39 | #include <dev/scsipi/scsi_all.h> |
| 40 | #include <dev/scsipi/scsi_disk.h> |
| 41 | #include <dev/scsipi/scsiconf.h> |
| 42 | #include <dev/scsipi/scsipi_all.h> |
| 43 | |
| 44 | #include <dev/ic/cissreg.h> |
| 45 | #include <dev/ic/cissvar.h> |
| 46 | |
| 47 | #if NBIO > 0 |
| 48 | #include <dev/biovar.h> |
| 49 | #endif /* NBIO > 0 */ |
| 50 | |
| 51 | #ifdef CISS_DEBUG |
| 52 | #define CISS_DPRINTF(m,a) if (ciss_debug & (m)) printf a |
| 53 | #define CISS_D_CMD 0x0001 |
| 54 | #define CISS_D_INTR 0x0002 |
| 55 | #define CISS_D_MISC 0x0004 |
| 56 | #define CISS_D_DMA 0x0008 |
| 57 | #define CISS_D_IOCTL 0x0010 |
| 58 | #define CISS_D_ERR 0x0020 |
| 59 | int ciss_debug = 0 |
| 60 | | CISS_D_CMD |
| 61 | | CISS_D_INTR |
| 62 | | CISS_D_MISC |
| 63 | | CISS_D_DMA |
| 64 | | CISS_D_IOCTL |
| 65 | | CISS_D_ERR |
| 66 | ; |
| 67 | #else |
| 68 | #define CISS_DPRINTF(m,a) /* m, a */ |
| 69 | #endif |
| 70 | |
| 71 | static void ciss_scsi_cmd(struct scsipi_channel *chan, |
| 72 | scsipi_adapter_req_t req, void *arg); |
| 73 | static int ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd, |
| 74 | void *addr, int flag, struct proc *p); |
| 75 | static void cissminphys(struct buf *bp); |
| 76 | |
| 77 | #if 0 |
| 78 | static void ciss_scsi_raw_cmd(struct scsipi_channel *chan, |
| 79 | scsipi_adapter_req_t req, void *arg); |
| 80 | #endif |
| 81 | |
| 82 | static int ciss_sync(struct ciss_softc *sc); |
| 83 | static void ciss_heartbeat(void *v); |
| 84 | static void ciss_shutdown(void *v); |
| 85 | |
| 86 | static struct ciss_ccb *ciss_get_ccb(struct ciss_softc *sc); |
| 87 | static void ciss_put_ccb(struct ciss_ccb *ccb); |
| 88 | static int ciss_cmd(struct ciss_ccb *ccb, int flags, int wait); |
| 89 | static int ciss_done(struct ciss_ccb *ccb); |
| 90 | static int ciss_error(struct ciss_ccb *ccb); |
| 91 | struct ciss_ld *ciss_pdscan(struct ciss_softc *sc, int ld); |
| 92 | static int ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq); |
| 93 | int ciss_ldid(struct ciss_softc *, int, struct ciss_ldid *); |
| 94 | int ciss_ldstat(struct ciss_softc *, int, struct ciss_ldstat *); |
| 95 | static int ciss_ldmap(struct ciss_softc *sc); |
| 96 | int ciss_pdid(struct ciss_softc *, u_int8_t, struct ciss_pdid *, int); |
| 97 | |
| 98 | #if NBIO > 0 |
| 99 | int ciss_ioctl(device_t, u_long, void *); |
| 100 | int ciss_ioctl_vol(struct ciss_softc *, struct bioc_vol *); |
| 101 | int ciss_blink(struct ciss_softc *, int, int, int, struct ciss_blink *); |
| 102 | int ciss_create_sensors(struct ciss_softc *); |
| 103 | void ciss_sensor_refresh(struct sysmon_envsys *, envsys_data_t *); |
| 104 | #endif /* NBIO > 0 */ |
| 105 | |
| 106 | static struct ciss_ccb * |
| 107 | ciss_get_ccb(struct ciss_softc *sc) |
| 108 | { |
| 109 | struct ciss_ccb *ccb; |
| 110 | |
| 111 | mutex_enter(&sc->sc_mutex); |
| 112 | if ((ccb = TAILQ_LAST(&sc->sc_free_ccb, ciss_queue_head))) { |
| 113 | TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_link); |
| 114 | ccb->ccb_state = CISS_CCB_READY; |
| 115 | } |
| 116 | mutex_exit(&sc->sc_mutex); |
| 117 | return ccb; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | ciss_put_ccb(struct ciss_ccb *ccb) |
| 122 | { |
| 123 | struct ciss_softc *sc = ccb->ccb_sc; |
| 124 | |
| 125 | ccb->ccb_state = CISS_CCB_FREE; |
| 126 | mutex_enter(&sc->sc_mutex); |
| 127 | TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link); |
| 128 | mutex_exit(&sc->sc_mutex); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | ciss_attach(struct ciss_softc *sc) |
| 133 | { |
| 134 | struct ciss_ccb *ccb; |
| 135 | struct ciss_cmd *cmd; |
| 136 | struct ciss_inquiry *inq; |
| 137 | bus_dma_segment_t seg[1]; |
| 138 | int error, i, total, rseg, maxfer; |
| 139 | paddr_t pa; |
| 140 | |
| 141 | bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, |
| 142 | (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4); |
| 143 | |
| 144 | if (sc->cfg.signature != CISS_SIGNATURE) { |
| 145 | aprint_error(": bad sign 0x%08x\n" , sc->cfg.signature); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | if (!(sc->cfg.methods & CISS_METH_SIMPL)) { |
| 150 | aprint_error(": not simple 0x%08x\n" , sc->cfg.methods); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | sc->cfg.rmethod = CISS_METH_SIMPL; |
| 155 | sc->cfg.paddr_lim = 0; /* 32bit addrs */ |
| 156 | sc->cfg.int_delay = 0; /* disable coalescing */ |
| 157 | sc->cfg.int_count = 0; |
| 158 | strlcpy(sc->cfg.hostname, "HUMPPA" , sizeof(sc->cfg.hostname)); |
| 159 | sc->cfg.driverf |= CISS_DRV_PRF; /* enable prefetch */ |
| 160 | if (!sc->cfg.maxsg) |
| 161 | sc->cfg.maxsg = MAXPHYS / PAGE_SIZE + 1; |
| 162 | |
| 163 | bus_space_write_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, |
| 164 | (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4); |
| 165 | bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, sizeof(sc->cfg), |
| 166 | BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); |
| 167 | |
| 168 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IDB, CISS_IDB_CFG); |
| 169 | bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4, |
| 170 | BUS_SPACE_BARRIER_WRITE); |
| 171 | for (i = 1000; i--; DELAY(1000)) { |
| 172 | /* XXX maybe IDB is really 64bit? - hp dl380 needs this */ |
| 173 | (void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB + 4); |
| 174 | if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG)) |
| 175 | break; |
| 176 | bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4, |
| 177 | BUS_SPACE_BARRIER_READ); |
| 178 | } |
| 179 | |
| 180 | if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG) { |
| 181 | printf(": cannot set config\n" ); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, |
| 186 | (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4); |
| 187 | |
| 188 | if (!(sc->cfg.amethod & CISS_METH_SIMPL)) { |
| 189 | printf(": cannot simplify 0x%08x\n" , sc->cfg.amethod); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | /* i'm ready for you and i hope you're ready for me */ |
| 194 | for (i = 30000; i--; DELAY(1000)) { |
| 195 | if (bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff + |
| 196 | offsetof(struct ciss_config, amethod)) & CISS_METH_READY) |
| 197 | break; |
| 198 | bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff + |
| 199 | offsetof(struct ciss_config, amethod), 4, |
| 200 | BUS_SPACE_BARRIER_READ); |
| 201 | } |
| 202 | |
| 203 | if (!(bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff + |
| 204 | offsetof(struct ciss_config, amethod)) & CISS_METH_READY)) { |
| 205 | aprint_error(": she never came ready for me 0x%08x\n" , |
| 206 | sc->cfg.amethod); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM); |
| 211 | mutex_init(&sc->sc_mutex_scratch, MUTEX_DEFAULT, IPL_VM); |
| 212 | cv_init(&sc->sc_condvar, "ciss_cmd" ); |
| 213 | sc->maxcmd = sc->cfg.maxcmd; |
| 214 | sc->maxsg = sc->cfg.maxsg; |
| 215 | if (sc->maxsg > MAXPHYS / PAGE_SIZE + 1) |
| 216 | sc->maxsg = MAXPHYS / PAGE_SIZE + 1; |
| 217 | i = sizeof(struct ciss_ccb) + |
| 218 | sizeof(ccb->ccb_cmd.sgl[0]) * (sc->maxsg - 1); |
| 219 | for (sc->ccblen = 0x10; sc->ccblen < i; sc->ccblen <<= 1); |
| 220 | |
| 221 | total = sc->ccblen * sc->maxcmd; |
| 222 | if ((error = bus_dmamem_alloc(sc->sc_dmat, total, PAGE_SIZE, 0, |
| 223 | sc->cmdseg, 1, &rseg, BUS_DMA_NOWAIT))) { |
| 224 | aprint_error(": cannot allocate CCBs (%d)\n" , error); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if ((error = bus_dmamem_map(sc->sc_dmat, sc->cmdseg, rseg, total, |
| 229 | (void **)&sc->ccbs, BUS_DMA_NOWAIT))) { |
| 230 | aprint_error(": cannot map CCBs (%d)\n" , error); |
| 231 | return -1; |
| 232 | } |
| 233 | memset(sc->ccbs, 0, total); |
| 234 | |
| 235 | if ((error = bus_dmamap_create(sc->sc_dmat, total, 1, |
| 236 | total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->cmdmap))) { |
| 237 | aprint_error(": cannot create CCBs dmamap (%d)\n" , error); |
| 238 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | if ((error = bus_dmamap_load(sc->sc_dmat, sc->cmdmap, sc->ccbs, total, |
| 243 | NULL, BUS_DMA_NOWAIT))) { |
| 244 | aprint_error(": cannot load CCBs dmamap (%d)\n" , error); |
| 245 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 246 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | TAILQ_INIT(&sc->sc_ccbq); |
| 251 | TAILQ_INIT(&sc->sc_ccbdone); |
| 252 | TAILQ_INIT(&sc->sc_free_ccb); |
| 253 | |
| 254 | maxfer = sc->maxsg * PAGE_SIZE; |
| 255 | for (i = 0; total > 0 && i < sc->maxcmd; i++, total -= sc->ccblen) { |
| 256 | ccb = (struct ciss_ccb *) ((char *)sc->ccbs + i * sc->ccblen); |
| 257 | cmd = &ccb->ccb_cmd; |
| 258 | pa = sc->cmdseg[0].ds_addr + i * sc->ccblen; |
| 259 | |
| 260 | ccb->ccb_sc = sc; |
| 261 | ccb->ccb_cmdpa = pa + offsetof(struct ciss_ccb, ccb_cmd); |
| 262 | ccb->ccb_state = CISS_CCB_FREE; |
| 263 | |
| 264 | cmd->id = htole32(i << 2); |
| 265 | cmd->id_hi = htole32(0); |
| 266 | cmd->sgin = sc->maxsg; |
| 267 | cmd->sglen = htole16((u_int16_t)cmd->sgin); |
| 268 | cmd->err_len = htole32(sizeof(ccb->ccb_err)); |
| 269 | pa += offsetof(struct ciss_ccb, ccb_err); |
| 270 | cmd->err_pa = htole64((u_int64_t)pa); |
| 271 | |
| 272 | if ((error = bus_dmamap_create(sc->sc_dmat, maxfer, sc->maxsg, |
| 273 | maxfer, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, |
| 274 | &ccb->ccb_dmamap))) |
| 275 | break; |
| 276 | |
| 277 | TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link); |
| 278 | } |
| 279 | |
| 280 | if (i < sc->maxcmd) { |
| 281 | aprint_error(": cannot create ccb#%d dmamap (%d)\n" , i, error); |
| 282 | if (i == 0) { |
| 283 | /* TODO leaking cmd's dmamaps and shitz */ |
| 284 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 285 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 286 | return -1; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, |
| 291 | seg, 1, &rseg, BUS_DMA_NOWAIT))) { |
| 292 | aprint_error(": cannot allocate scratch buffer (%d)\n" , error); |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | if ((error = bus_dmamem_map(sc->sc_dmat, seg, rseg, PAGE_SIZE, |
| 297 | (void **)&sc->scratch, BUS_DMA_NOWAIT))) { |
| 298 | aprint_error(": cannot map scratch buffer (%d)\n" , error); |
| 299 | return -1; |
| 300 | } |
| 301 | memset(sc->scratch, 0, PAGE_SIZE); |
| 302 | sc->sc_waitflag = XS_CTL_NOSLEEP; /* can't sleep yet */ |
| 303 | |
| 304 | mutex_enter(&sc->sc_mutex_scratch); /* is this really needed? */ |
| 305 | inq = sc->scratch; |
| 306 | if (ciss_inq(sc, inq)) { |
| 307 | aprint_error(": adapter inquiry failed\n" ); |
| 308 | mutex_exit(&sc->sc_mutex_scratch); |
| 309 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 310 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | if (!(inq->flags & CISS_INQ_BIGMAP)) { |
| 315 | aprint_error(": big map is not supported, flags=0x%x\n" , |
| 316 | inq->flags); |
| 317 | mutex_exit(&sc->sc_mutex_scratch); |
| 318 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 319 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | sc->maxunits = inq->numld; |
| 324 | sc->nbus = inq->nscsi_bus; |
| 325 | sc->ndrives = inq->buswidth ? inq->buswidth : 256; |
| 326 | aprint_normal(": %d LD%s, HW rev %d, FW %4.4s/%4.4s" , |
| 327 | inq->numld, inq->numld == 1? "" : "s" , |
| 328 | inq->hw_rev, inq->fw_running, inq->fw_stored); |
| 329 | |
| 330 | if (sc->cfg.methods & CISS_METH_FIFO64) |
| 331 | aprint_normal(", 64bit fifo" ); |
| 332 | else if (sc->cfg.methods & CISS_METH_FIFO64_RRO) |
| 333 | aprint_normal(", 64bit fifo rro" ); |
| 334 | aprint_normal("\n" ); |
| 335 | |
| 336 | mutex_exit(&sc->sc_mutex_scratch); |
| 337 | |
| 338 | callout_init(&sc->sc_hb, 0); |
| 339 | callout_setfunc(&sc->sc_hb, ciss_heartbeat, sc); |
| 340 | callout_schedule(&sc->sc_hb, hz * 3); |
| 341 | |
| 342 | /* map LDs */ |
| 343 | if (ciss_ldmap(sc)) { |
| 344 | aprint_error_dev(sc->sc_dev, "adapter LD map failed\n" ); |
| 345 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 346 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | if (!(sc->sc_lds = malloc(sc->maxunits * sizeof(*sc->sc_lds), |
| 351 | M_DEVBUF, M_NOWAIT))) { |
| 352 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 353 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 354 | return -1; |
| 355 | } |
| 356 | memset(sc->sc_lds, 0, sc->maxunits * sizeof(*sc->sc_lds)); |
| 357 | |
| 358 | sc->sc_flush = CISS_FLUSH_ENABLE; |
| 359 | if (!(sc->sc_sh = shutdownhook_establish(ciss_shutdown, sc))) { |
| 360 | aprint_error_dev(sc->sc_dev, |
| 361 | "unable to establish shutdown hook\n" ); |
| 362 | bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1); |
| 363 | bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap); |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | sc->sc_channel.chan_adapter = &sc->sc_adapter; |
| 368 | sc->sc_channel.chan_bustype = &scsi_bustype; |
| 369 | sc->sc_channel.chan_channel = 0; |
| 370 | sc->sc_channel.chan_ntargets = sc->maxunits; |
| 371 | sc->sc_channel.chan_nluns = 1; /* ciss doesn't really have SCSI luns */ |
| 372 | sc->sc_channel.chan_openings = sc->maxcmd; |
| 373 | #if NBIO > 0 |
| 374 | /* XXX Reserve some ccb's for sensor and bioctl. */ |
| 375 | if (sc->sc_channel.chan_openings > 2) |
| 376 | sc->sc_channel.chan_openings -= 2; |
| 377 | #endif |
| 378 | sc->sc_channel.chan_flags = 0; |
| 379 | sc->sc_channel.chan_id = sc->maxunits; |
| 380 | |
| 381 | sc->sc_adapter.adapt_dev = sc->sc_dev; |
| 382 | sc->sc_adapter.adapt_openings = sc->sc_channel.chan_openings; |
| 383 | sc->sc_adapter.adapt_max_periph = min(sc->sc_adapter.adapt_openings, 256); |
| 384 | sc->sc_adapter.adapt_request = ciss_scsi_cmd; |
| 385 | sc->sc_adapter.adapt_minphys = cissminphys; |
| 386 | sc->sc_adapter.adapt_ioctl = ciss_scsi_ioctl; |
| 387 | sc->sc_adapter.adapt_nchannels = 1; |
| 388 | config_found(sc->sc_dev, &sc->sc_channel, scsiprint); |
| 389 | |
| 390 | #if 0 |
| 391 | sc->sc_link_raw.adapter_softc = sc; |
| 392 | sc->sc_link.openings = sc->sc_channel.chan_openings; |
| 393 | sc->sc_link_raw.adapter = &ciss_raw_switch; |
| 394 | sc->sc_link_raw.adapter_target = sc->ndrives; |
| 395 | sc->sc_link_raw.adapter_buswidth = sc->ndrives; |
| 396 | config_found(sc->sc_dev, &sc->sc_channel, scsiprint); |
| 397 | #endif |
| 398 | |
| 399 | #if NBIO > 0 |
| 400 | /* now map all the physdevs into their lds */ |
| 401 | /* XXX currently we assign all of them into ld0 */ |
| 402 | for (i = 0; i < sc->maxunits && i < 1; i++) |
| 403 | if (!(sc->sc_lds[i] = ciss_pdscan(sc, i))) { |
| 404 | sc->sc_waitflag = 0; /* we can sleep now */ |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | if (bio_register(sc->sc_dev, ciss_ioctl) != 0) |
| 409 | aprint_error_dev(sc->sc_dev, "controller registration failed" ); |
| 410 | else |
| 411 | sc->sc_ioctl = ciss_ioctl; |
| 412 | if (ciss_create_sensors(sc) != 0) |
| 413 | aprint_error_dev(sc->sc_dev, "unable to create sensors" ); |
| 414 | #endif |
| 415 | sc->sc_waitflag = 0; /* we can sleep now */ |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | ciss_shutdown(void *v) |
| 422 | { |
| 423 | struct ciss_softc *sc = v; |
| 424 | |
| 425 | sc->sc_flush = CISS_FLUSH_DISABLE; |
| 426 | /* timeout_del(&sc->sc_hb); */ |
| 427 | ciss_sync(sc); |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | cissminphys(struct buf *bp) |
| 432 | { |
| 433 | #if 0 /* TODO */ |
| 434 | #define CISS_MAXFER (PAGE_SIZE * (sc->maxsg + 1)) |
| 435 | if (bp->b_bcount > CISS_MAXFER) |
| 436 | bp->b_bcount = CISS_MAXFER; |
| 437 | #endif |
| 438 | minphys(bp); |
| 439 | } |
| 440 | |
| 441 | static struct ciss_ccb * |
| 442 | ciss_poll1(struct ciss_softc *sc) |
| 443 | { |
| 444 | struct ciss_ccb *ccb; |
| 445 | uint32_t id; |
| 446 | |
| 447 | if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem)) { |
| 448 | CISS_DPRINTF(CISS_D_CMD, ("N" )); |
| 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | if (sc->cfg.methods & CISS_METH_FIFO64) { |
| 453 | if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ64_HI) == |
| 454 | 0xffffffff) { |
| 455 | CISS_DPRINTF(CISS_D_CMD, ("Q" )); |
| 456 | return NULL; |
| 457 | } |
| 458 | id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ64_LO); |
| 459 | } else if (sc->cfg.methods & CISS_METH_FIFO64_RRO) { |
| 460 | id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ64_LO); |
| 461 | if (id == 0xffffffff) { |
| 462 | CISS_DPRINTF(CISS_D_CMD, ("Q" )); |
| 463 | return NULL; |
| 464 | } |
| 465 | (void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ64_HI); |
| 466 | } else { |
| 467 | id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ); |
| 468 | if (id == 0xffffffff) { |
| 469 | CISS_DPRINTF(CISS_D_CMD, ("Q" )); |
| 470 | return NULL; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | CISS_DPRINTF(CISS_D_CMD, ("got=0x%x " , id)); |
| 475 | ccb = (struct ciss_ccb *) ((char *)sc->ccbs + (id >> 2) * sc->ccblen); |
| 476 | ccb->ccb_cmd.id = htole32(id); |
| 477 | ccb->ccb_cmd.id_hi = htole32(0); |
| 478 | return ccb; |
| 479 | } |
| 480 | |
| 481 | static int |
| 482 | ciss_poll(struct ciss_softc *sc, struct ciss_ccb *ccb, int ms) |
| 483 | { |
| 484 | struct ciss_ccb *ccb1; |
| 485 | |
| 486 | ms /= 10; |
| 487 | |
| 488 | while (ms-- > 0) { |
| 489 | DELAY(10); |
| 490 | ccb1 = ciss_poll1(sc); |
| 491 | if (ccb1 == NULL) |
| 492 | continue; |
| 493 | ciss_done(ccb1); |
| 494 | if (ccb1 == ccb) |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | return ETIMEDOUT; |
| 499 | } |
| 500 | |
| 501 | static int |
| 502 | ciss_wait(struct ciss_softc *sc, struct ciss_ccb *ccb, int ms) |
| 503 | { |
| 504 | int tohz, etick; |
| 505 | |
| 506 | tohz = mstohz(ms); |
| 507 | if (tohz == 0) |
| 508 | tohz = 1; |
| 509 | etick = hardclock_ticks + tohz; |
| 510 | |
| 511 | for (;;) { |
| 512 | ccb->ccb_state = CISS_CCB_POLL; |
| 513 | CISS_DPRINTF(CISS_D_CMD, ("cv_timedwait(%d) " , tohz)); |
| 514 | mutex_enter(&sc->sc_mutex); |
| 515 | if (cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, tohz) |
| 516 | == EWOULDBLOCK) { |
| 517 | mutex_exit(&sc->sc_mutex); |
| 518 | return EWOULDBLOCK; |
| 519 | } |
| 520 | mutex_exit(&sc->sc_mutex); |
| 521 | if (ccb->ccb_state == CISS_CCB_ONQ) { |
| 522 | ciss_done(ccb); |
| 523 | return 0; |
| 524 | } |
| 525 | tohz = etick - hardclock_ticks; |
| 526 | if (tohz <= 0) |
| 527 | return EWOULDBLOCK; |
| 528 | CISS_DPRINTF(CISS_D_CMD, ("T" )); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * submit a command and optionally wait for completition. |
| 534 | * wait arg abuses XS_CTL_POLL|XS_CTL_NOSLEEP flags to request |
| 535 | * to wait (XS_CTL_POLL) and to allow tsleep() (!XS_CTL_NOSLEEP) |
| 536 | * instead of busy loop waiting |
| 537 | */ |
| 538 | static int |
| 539 | ciss_cmd(struct ciss_ccb *ccb, int flags, int wait) |
| 540 | { |
| 541 | struct ciss_softc *sc = ccb->ccb_sc; |
| 542 | struct ciss_cmd *cmd = &ccb->ccb_cmd; |
| 543 | bus_dmamap_t dmap = ccb->ccb_dmamap; |
| 544 | u_int64_t addr; |
| 545 | int i, error = 0; |
| 546 | |
| 547 | if (ccb->ccb_state != CISS_CCB_READY) { |
| 548 | printf("%s: ccb %d not ready state=0x%x\n" , device_xname(sc->sc_dev), |
| 549 | cmd->id, ccb->ccb_state); |
| 550 | return (EINVAL); |
| 551 | } |
| 552 | |
| 553 | if (ccb->ccb_data) { |
| 554 | bus_dma_segment_t *sgd; |
| 555 | |
| 556 | if ((error = bus_dmamap_load(sc->sc_dmat, dmap, ccb->ccb_data, |
| 557 | ccb->ccb_len, NULL, flags))) { |
| 558 | if (error == EFBIG) |
| 559 | printf("more than %d dma segs\n" , sc->maxsg); |
| 560 | else |
| 561 | printf("error %d loading dma map\n" , error); |
| 562 | ciss_put_ccb(ccb); |
| 563 | return (error); |
| 564 | } |
| 565 | cmd->sgin = dmap->dm_nsegs; |
| 566 | |
| 567 | sgd = dmap->dm_segs; |
| 568 | CISS_DPRINTF(CISS_D_DMA, ("data=%p/%zu<%#" PRIxPADDR "/%zu" , |
| 569 | ccb->ccb_data, ccb->ccb_len, sgd->ds_addr, sgd->ds_len)); |
| 570 | |
| 571 | for (i = 0; i < dmap->dm_nsegs; sgd++, i++) { |
| 572 | cmd->sgl[i].addr_lo = htole32(sgd->ds_addr); |
| 573 | cmd->sgl[i].addr_hi = |
| 574 | htole32((u_int64_t)sgd->ds_addr >> 32); |
| 575 | cmd->sgl[i].len = htole32(sgd->ds_len); |
| 576 | cmd->sgl[i].flags = htole32(0); |
| 577 | if (i) { |
| 578 | CISS_DPRINTF(CISS_D_DMA, |
| 579 | (",%#" PRIxPADDR "/%zu" , sgd->ds_addr, |
| 580 | sgd->ds_len)); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | CISS_DPRINTF(CISS_D_DMA, ("> " )); |
| 585 | |
| 586 | bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize, |
| 587 | BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); |
| 588 | } else |
| 589 | cmd->sgin = 0; |
| 590 | cmd->sglen = htole16((u_int16_t)cmd->sgin); |
| 591 | memset(&ccb->ccb_err, 0, sizeof(ccb->ccb_err)); |
| 592 | |
| 593 | bus_dmamap_sync(sc->sc_dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize, |
| 594 | BUS_DMASYNC_PREWRITE); |
| 595 | |
| 596 | if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP)) |
| 597 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR, |
| 598 | bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem); |
| 599 | |
| 600 | mutex_enter(&sc->sc_mutex); |
| 601 | TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link); |
| 602 | mutex_exit(&sc->sc_mutex); |
| 603 | ccb->ccb_state = CISS_CCB_ONQ; |
| 604 | CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x " , cmd->id)); |
| 605 | if (sc->cfg.methods & (CISS_METH_FIFO64|CISS_METH_FIFO64_RRO)) { |
| 606 | /* |
| 607 | * Write the upper 32bits immediately before the lower |
| 608 | * 32bits and set bit 63 to indicate 64bit FIFO mode. |
| 609 | */ |
| 610 | addr = (u_int64_t)ccb->ccb_cmdpa; |
| 611 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ64_HI, |
| 612 | (addr >> 32) | 0x80000000); |
| 613 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ64_LO, |
| 614 | addr & 0x00000000ffffffffULL); |
| 615 | } else |
| 616 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ, |
| 617 | ccb->ccb_cmdpa); |
| 618 | |
| 619 | if (wait & XS_CTL_POLL) { |
| 620 | int ms; |
| 621 | CISS_DPRINTF(CISS_D_CMD, ("waiting " )); |
| 622 | |
| 623 | ms = ccb->ccb_xs ? ccb->ccb_xs->timeout : 60000; |
| 624 | if (wait & XS_CTL_NOSLEEP) |
| 625 | error = ciss_poll(sc, ccb, ms); |
| 626 | else |
| 627 | error = ciss_wait(sc, ccb, ms); |
| 628 | |
| 629 | /* if never got a chance to be done above... */ |
| 630 | if (ccb->ccb_state != CISS_CCB_FREE) { |
| 631 | KASSERT(error); |
| 632 | ccb->ccb_err.cmd_stat = CISS_ERR_TMO; |
| 633 | error = ciss_done(ccb); |
| 634 | } |
| 635 | |
| 636 | CISS_DPRINTF(CISS_D_CMD, ("done %d:%d" , |
| 637 | ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat)); |
| 638 | } |
| 639 | |
| 640 | if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP)) |
| 641 | bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR, |
| 642 | bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem); |
| 643 | |
| 644 | return (error); |
| 645 | } |
| 646 | |
| 647 | static int |
| 648 | ciss_done(struct ciss_ccb *ccb) |
| 649 | { |
| 650 | struct ciss_softc *sc = ccb->ccb_sc; |
| 651 | struct scsipi_xfer *xs = ccb->ccb_xs; |
| 652 | struct ciss_cmd *cmd; |
| 653 | int error = 0; |
| 654 | |
| 655 | CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) " , ccb)); |
| 656 | |
| 657 | if (ccb->ccb_state != CISS_CCB_ONQ) { |
| 658 | printf("%s: unqueued ccb %p ready, state=0x%x\n" , |
| 659 | device_xname(sc->sc_dev), ccb, ccb->ccb_state); |
| 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | ccb->ccb_state = CISS_CCB_READY; |
| 664 | mutex_enter(&sc->sc_mutex); |
| 665 | TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link); |
| 666 | mutex_exit(&sc->sc_mutex); |
| 667 | |
| 668 | if (ccb->ccb_cmd.id & CISS_CMD_ERR) |
| 669 | error = ciss_error(ccb); |
| 670 | |
| 671 | cmd = &ccb->ccb_cmd; |
| 672 | if (ccb->ccb_data) { |
| 673 | bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0, |
| 674 | ccb->ccb_dmamap->dm_mapsize, (cmd->flags & CISS_CDB_IN) ? |
| 675 | BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); |
| 676 | bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap); |
| 677 | ccb->ccb_xs = NULL; |
| 678 | ccb->ccb_data = NULL; |
| 679 | } |
| 680 | |
| 681 | ciss_put_ccb(ccb); |
| 682 | |
| 683 | if (xs) { |
| 684 | xs->resid = 0; |
| 685 | CISS_DPRINTF(CISS_D_CMD, ("scsipi_done(%p) " , xs)); |
| 686 | if (xs->cmd->opcode == INQUIRY) { |
| 687 | struct scsipi_inquiry_data *inq; |
| 688 | inq = (struct scsipi_inquiry_data *)xs->data; |
| 689 | if ((inq->version & SID_ANSII) == 0 && |
| 690 | (inq->flags3 & SID_CmdQue) != 0) { |
| 691 | inq->version |= 2; |
| 692 | } |
| 693 | } |
| 694 | scsipi_done(xs); |
| 695 | } |
| 696 | |
| 697 | return error; |
| 698 | } |
| 699 | |
| 700 | static int |
| 701 | ciss_error(struct ciss_ccb *ccb) |
| 702 | { |
| 703 | struct ciss_softc *sc = ccb->ccb_sc; |
| 704 | struct ciss_error *err = &ccb->ccb_err; |
| 705 | struct scsipi_xfer *xs = ccb->ccb_xs; |
| 706 | int rv; |
| 707 | |
| 708 | switch ((rv = le16toh(err->cmd_stat))) { |
| 709 | case CISS_ERR_OK: |
| 710 | rv = 0; |
| 711 | break; |
| 712 | |
| 713 | case CISS_ERR_INVCMD: |
| 714 | if (xs == NULL || |
| 715 | xs->cmd->opcode != SCSI_SYNCHRONIZE_CACHE_10) |
| 716 | printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n" , |
| 717 | device_xname(sc->sc_dev), ccb->ccb_cmd.id, |
| 718 | err->err_info, err->err_type[3], err->err_type[2]); |
| 719 | if (xs) { |
| 720 | memset(&xs->sense, 0, sizeof(xs->sense)); |
| 721 | xs->sense.scsi_sense.response_code = |
| 722 | SSD_RCODE_CURRENT | SSD_RCODE_VALID; |
| 723 | xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST; |
| 724 | xs->sense.scsi_sense.asc = 0x24; /* ill field */ |
| 725 | xs->sense.scsi_sense.ascq = 0x0; |
| 726 | xs->error = XS_SENSE; |
| 727 | } |
| 728 | rv = EIO; |
| 729 | break; |
| 730 | |
| 731 | case CISS_ERR_TMO: |
| 732 | xs->error = XS_TIMEOUT; |
| 733 | rv = ETIMEDOUT; |
| 734 | break; |
| 735 | |
| 736 | case CISS_ERR_UNRUN: |
| 737 | /* Underrun */ |
| 738 | xs->resid = le32toh(err->resid); |
| 739 | CISS_DPRINTF(CISS_D_CMD, (" underrun resid=0x%x " , |
| 740 | xs->resid)); |
| 741 | rv = EIO; |
| 742 | break; |
| 743 | default: |
| 744 | if (xs) { |
| 745 | CISS_DPRINTF(CISS_D_CMD, ("scsi_stat=%x " , err->scsi_stat)); |
| 746 | switch (err->scsi_stat) { |
| 747 | case SCSI_CHECK: |
| 748 | xs->error = XS_SENSE; |
| 749 | memcpy(&xs->sense, &err->sense[0], |
| 750 | sizeof(xs->sense)); |
| 751 | CISS_DPRINTF(CISS_D_CMD, (" sense=%02x %02x %02x %02x " , |
| 752 | err->sense[0], err->sense[1], err->sense[2], err->sense[3])); |
| 753 | rv = EIO; |
| 754 | break; |
| 755 | |
| 756 | case XS_BUSY: |
| 757 | xs->error = XS_BUSY; |
| 758 | rv = EBUSY; |
| 759 | break; |
| 760 | |
| 761 | default: |
| 762 | CISS_DPRINTF(CISS_D_ERR, ("%s: " |
| 763 | "cmd_stat=%x scsi_stat=0x%x resid=0x%x\n" , |
| 764 | device_xname(sc->sc_dev), rv, err->scsi_stat, |
| 765 | le32toh(err->resid))); |
| 766 | printf("ciss driver stuffup in %s:%d: %s()\n" , |
| 767 | __FILE__, __LINE__, __func__); |
| 768 | xs->error = XS_DRIVER_STUFFUP; |
| 769 | rv = EIO; |
| 770 | break; |
| 771 | } |
| 772 | xs->resid = le32toh(err->resid); |
| 773 | } else |
| 774 | rv = EIO; |
| 775 | } |
| 776 | ccb->ccb_cmd.id &= htole32(~3); |
| 777 | |
| 778 | return rv; |
| 779 | } |
| 780 | |
| 781 | static int |
| 782 | ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq) |
| 783 | { |
| 784 | struct ciss_ccb *ccb; |
| 785 | struct ciss_cmd *cmd; |
| 786 | |
| 787 | ccb = ciss_get_ccb(sc); |
| 788 | ccb->ccb_len = sizeof(*inq); |
| 789 | ccb->ccb_data = inq; |
| 790 | ccb->ccb_xs = NULL; |
| 791 | cmd = &ccb->ccb_cmd; |
| 792 | cmd->tgt = htole32(CISS_CMD_MODE_PERIPH); |
| 793 | cmd->tgt2 = 0; |
| 794 | cmd->cdblen = 10; |
| 795 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN; |
| 796 | cmd->tmo = htole16(0); |
| 797 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 798 | cmd->cdb[0] = CISS_CMD_CTRL_GET; |
| 799 | cmd->cdb[6] = CISS_CMS_CTRL_CTRL; |
| 800 | cmd->cdb[7] = sizeof(*inq) >> 8; /* biiiig endian */ |
| 801 | cmd->cdb[8] = sizeof(*inq) & 0xff; |
| 802 | |
| 803 | return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP); |
| 804 | } |
| 805 | |
| 806 | static int |
| 807 | ciss_ldmap(struct ciss_softc *sc) |
| 808 | { |
| 809 | struct ciss_ccb *ccb; |
| 810 | struct ciss_cmd *cmd; |
| 811 | struct ciss_ldmap *lmap; |
| 812 | int total, rv; |
| 813 | |
| 814 | mutex_enter(&sc->sc_mutex_scratch); |
| 815 | lmap = sc->scratch; |
| 816 | lmap->size = htobe32(sc->maxunits * sizeof(lmap->map)); |
| 817 | total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map); |
| 818 | |
| 819 | ccb = ciss_get_ccb(sc); |
| 820 | ccb->ccb_len = total; |
| 821 | ccb->ccb_data = lmap; |
| 822 | ccb->ccb_xs = NULL; |
| 823 | cmd = &ccb->ccb_cmd; |
| 824 | cmd->tgt = CISS_CMD_MODE_PERIPH; |
| 825 | cmd->tgt2 = 0; |
| 826 | cmd->cdblen = 12; |
| 827 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN; |
| 828 | cmd->tmo = htole16(30); |
| 829 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 830 | cmd->cdb[0] = CISS_CMD_LDMAP; |
| 831 | cmd->cdb[8] = total >> 8; /* biiiig endian */ |
| 832 | cmd->cdb[9] = total & 0xff; |
| 833 | |
| 834 | rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP); |
| 835 | |
| 836 | if (rv) { |
| 837 | mutex_exit(&sc->sc_mutex_scratch); |
| 838 | return rv; |
| 839 | } |
| 840 | |
| 841 | CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n" , |
| 842 | lmap->map[0].tgt, lmap->map[0].tgt2)); |
| 843 | |
| 844 | mutex_exit(&sc->sc_mutex_scratch); |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int |
| 849 | ciss_sync(struct ciss_softc *sc) |
| 850 | { |
| 851 | struct ciss_ccb *ccb; |
| 852 | struct ciss_cmd *cmd; |
| 853 | struct ciss_flush *flush; |
| 854 | int rv; |
| 855 | |
| 856 | mutex_enter(&sc->sc_mutex_scratch); |
| 857 | flush = sc->scratch; |
| 858 | memset(flush, 0, sizeof(*flush)); |
| 859 | flush->flush = sc->sc_flush; |
| 860 | |
| 861 | ccb = ciss_get_ccb(sc); |
| 862 | ccb->ccb_len = sizeof(*flush); |
| 863 | ccb->ccb_data = flush; |
| 864 | ccb->ccb_xs = NULL; |
| 865 | cmd = &ccb->ccb_cmd; |
| 866 | cmd->tgt = CISS_CMD_MODE_PERIPH; |
| 867 | cmd->tgt2 = 0; |
| 868 | cmd->cdblen = 10; |
| 869 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT; |
| 870 | cmd->tmo = 0; |
| 871 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 872 | cmd->cdb[0] = CISS_CMD_CTRL_SET; |
| 873 | cmd->cdb[6] = CISS_CMS_CTRL_FLUSH; |
| 874 | cmd->cdb[7] = sizeof(*flush) >> 8; /* biiiig endian */ |
| 875 | cmd->cdb[8] = sizeof(*flush) & 0xff; |
| 876 | |
| 877 | rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP); |
| 878 | mutex_exit(&sc->sc_mutex_scratch); |
| 879 | |
| 880 | return rv; |
| 881 | } |
| 882 | |
| 883 | int |
| 884 | ciss_ldid(struct ciss_softc *sc, int target, struct ciss_ldid *id) |
| 885 | { |
| 886 | struct ciss_ccb *ccb; |
| 887 | struct ciss_cmd *cmd; |
| 888 | |
| 889 | ccb = ciss_get_ccb(sc); |
| 890 | if (ccb == NULL) |
| 891 | return ENOMEM; |
| 892 | ccb->ccb_len = sizeof(*id); |
| 893 | ccb->ccb_data = id; |
| 894 | ccb->ccb_xs = NULL; |
| 895 | cmd = &ccb->ccb_cmd; |
| 896 | cmd->tgt = htole32(CISS_CMD_MODE_PERIPH); |
| 897 | cmd->tgt2 = 0; |
| 898 | cmd->cdblen = 10; |
| 899 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN; |
| 900 | cmd->tmo = htole16(0); |
| 901 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 902 | cmd->cdb[0] = CISS_CMD_CTRL_GET; |
| 903 | cmd->cdb[1] = target; |
| 904 | cmd->cdb[6] = CISS_CMS_CTRL_LDIDEXT; |
| 905 | cmd->cdb[7] = sizeof(*id) >> 8; /* biiiig endian */ |
| 906 | cmd->cdb[8] = sizeof(*id) & 0xff; |
| 907 | |
| 908 | return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL | sc->sc_waitflag); |
| 909 | } |
| 910 | |
| 911 | int |
| 912 | ciss_ldstat(struct ciss_softc *sc, int target, struct ciss_ldstat *stat) |
| 913 | { |
| 914 | struct ciss_ccb *ccb; |
| 915 | struct ciss_cmd *cmd; |
| 916 | |
| 917 | ccb = ciss_get_ccb(sc); |
| 918 | if (ccb == NULL) |
| 919 | return ENOMEM; |
| 920 | ccb->ccb_len = sizeof(*stat); |
| 921 | ccb->ccb_data = stat; |
| 922 | ccb->ccb_xs = NULL; |
| 923 | cmd = &ccb->ccb_cmd; |
| 924 | cmd->tgt = htole32(CISS_CMD_MODE_PERIPH); |
| 925 | cmd->tgt2 = 0; |
| 926 | cmd->cdblen = 10; |
| 927 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN; |
| 928 | cmd->tmo = htole16(0); |
| 929 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 930 | cmd->cdb[0] = CISS_CMD_CTRL_GET; |
| 931 | cmd->cdb[1] = target; |
| 932 | cmd->cdb[6] = CISS_CMS_CTRL_LDSTAT; |
| 933 | cmd->cdb[7] = sizeof(*stat) >> 8; /* biiiig endian */ |
| 934 | cmd->cdb[8] = sizeof(*stat) & 0xff; |
| 935 | |
| 936 | return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL | sc->sc_waitflag); |
| 937 | } |
| 938 | |
| 939 | int |
| 940 | ciss_pdid(struct ciss_softc *sc, u_int8_t drv, struct ciss_pdid *id, int wait) |
| 941 | { |
| 942 | struct ciss_ccb *ccb; |
| 943 | struct ciss_cmd *cmd; |
| 944 | |
| 945 | ccb = ciss_get_ccb(sc); |
| 946 | if (ccb == NULL) |
| 947 | return ENOMEM; |
| 948 | ccb->ccb_len = sizeof(*id); |
| 949 | ccb->ccb_data = id; |
| 950 | ccb->ccb_xs = NULL; |
| 951 | cmd = &ccb->ccb_cmd; |
| 952 | cmd->tgt = htole32(CISS_CMD_MODE_PERIPH); |
| 953 | cmd->tgt2 = 0; |
| 954 | cmd->cdblen = 10; |
| 955 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN; |
| 956 | cmd->tmo = htole16(0); |
| 957 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 958 | cmd->cdb[0] = CISS_CMD_CTRL_GET; |
| 959 | cmd->cdb[2] = drv; |
| 960 | cmd->cdb[6] = CISS_CMS_CTRL_PDID; |
| 961 | cmd->cdb[7] = sizeof(*id) >> 8; /* biiiig endian */ |
| 962 | cmd->cdb[8] = sizeof(*id) & 0xff; |
| 963 | |
| 964 | return ciss_cmd(ccb, BUS_DMA_NOWAIT, wait); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | struct ciss_ld * |
| 969 | ciss_pdscan(struct ciss_softc *sc, int ld) |
| 970 | { |
| 971 | struct ciss_pdid *pdid; |
| 972 | struct ciss_ld *ldp; |
| 973 | u_int8_t drv, buf[128]; |
| 974 | int i, j, k = 0; |
| 975 | |
| 976 | mutex_enter(&sc->sc_mutex_scratch); |
| 977 | pdid = sc->scratch; |
| 978 | if (sc->ndrives == 256) { |
| 979 | for (i = 0; i < CISS_BIGBIT; i++) |
| 980 | if (!ciss_pdid(sc, i, pdid, |
| 981 | XS_CTL_POLL|XS_CTL_NOSLEEP) && |
| 982 | (pdid->present & CISS_PD_PRESENT)) |
| 983 | buf[k++] = i; |
| 984 | } else |
| 985 | for (i = 0; i < sc->nbus; i++) |
| 986 | for (j = 0; j < sc->ndrives; j++) { |
| 987 | drv = CISS_BIGBIT + i * sc->ndrives + j; |
| 988 | if (!ciss_pdid(sc, drv, pdid, |
| 989 | XS_CTL_POLL|XS_CTL_NOSLEEP)) |
| 990 | buf[k++] = drv; |
| 991 | } |
| 992 | mutex_exit(&sc->sc_mutex_scratch); |
| 993 | |
| 994 | if (!k) |
| 995 | return NULL; |
| 996 | |
| 997 | ldp = malloc(sizeof(*ldp) + (k-1), M_DEVBUF, M_NOWAIT); |
| 998 | if (!ldp) |
| 999 | return NULL; |
| 1000 | |
| 1001 | memset(&ldp->bling, 0, sizeof(ldp->bling)); |
| 1002 | ldp->ndrives = k; |
| 1003 | ldp->xname[0] = 0; |
| 1004 | memcpy(ldp->tgts, buf, k); |
| 1005 | return ldp; |
| 1006 | } |
| 1007 | |
| 1008 | #if 0 |
| 1009 | static void |
| 1010 | ciss_scsi_raw_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, |
| 1011 | void *arg) /* TODO */ |
| 1012 | { |
| 1013 | struct scsipi_xfer *xs = (struct scsipi_xfer *) arg; |
| 1014 | struct ciss_rawsoftc *rsc = device_private( |
| 1015 | chan->chan_adapter->adapt_dev); |
| 1016 | struct ciss_softc *sc = rsc->sc_softc; |
| 1017 | struct ciss_ccb *ccb; |
| 1018 | struct ciss_cmd *cmd; |
| 1019 | int error; |
| 1020 | |
| 1021 | CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_raw_cmd " )); |
| 1022 | |
| 1023 | switch (req) |
| 1024 | { |
| 1025 | case ADAPTER_REQ_RUN_XFER: |
| 1026 | if (xs->cmdlen > CISS_MAX_CDB) { |
| 1027 | CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p " , xs)); |
| 1028 | memset(&xs->sense, 0, sizeof(xs->sense)); |
| 1029 | printf("ciss driver stuffup in %s:%d: %s()\n" , |
| 1030 | __FILE__, __LINE__, __func__); |
| 1031 | xs->error = XS_DRIVER_STUFFUP; |
| 1032 | scsipi_done(xs); |
| 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | error = 0; |
| 1037 | xs->error = XS_NOERROR; |
| 1038 | |
| 1039 | /* TODO check this target has not yet employed w/ any volume */ |
| 1040 | |
| 1041 | ccb = ciss_get_ccb(sc); |
| 1042 | cmd = &ccb->ccb_cmd; |
| 1043 | ccb->ccb_len = xs->datalen; |
| 1044 | ccb->ccb_data = xs->data; |
| 1045 | ccb->ccb_xs = xs; |
| 1046 | |
| 1047 | cmd->cdblen = xs->cmdlen; |
| 1048 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL; |
| 1049 | if (xs->xs_control & XS_CTL_DATA_IN) |
| 1050 | cmd->flags |= CISS_CDB_IN; |
| 1051 | else if (xs->xs_control & XS_CTL_DATA_OUT) |
| 1052 | cmd->flags |= CISS_CDB_OUT; |
| 1053 | cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000; |
| 1054 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 1055 | memcpy(&cmd->cdb[0], xs->cmd, CISS_MAX_CDB); |
| 1056 | |
| 1057 | if (ciss_cmd(ccb, BUS_DMA_WAITOK, |
| 1058 | xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) { |
| 1059 | printf("ciss driver stuffup in %s:%d: %s()\n" , |
| 1060 | __FILE__, __LINE__, __func__); |
| 1061 | xs->error = XS_DRIVER_STUFFUP; |
| 1062 | scsipi_done(xs); |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | break; |
| 1067 | |
| 1068 | case ADAPTER_REQ_GROW_RESOURCES: |
| 1069 | /* |
| 1070 | * Not supported. |
| 1071 | */ |
| 1072 | break; |
| 1073 | |
| 1074 | case ADAPTER_REQ_SET_XFER_MODE: |
| 1075 | /* |
| 1076 | * We can't change the transfer mode, but at least let |
| 1077 | * scsipi know what the adapter has negociated. |
| 1078 | */ |
| 1079 | /* Get xfer mode and return it */ |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
| 1085 | static void |
| 1086 | ciss_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, |
| 1087 | void *arg) |
| 1088 | { |
| 1089 | struct scsipi_xfer *xs; |
| 1090 | struct scsipi_xfer_mode *xm; |
| 1091 | struct ciss_softc *sc = device_private(chan->chan_adapter->adapt_dev); |
| 1092 | u_int8_t target; |
| 1093 | struct ciss_ccb *ccb; |
| 1094 | struct ciss_cmd *cmd; |
| 1095 | |
| 1096 | CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd " )); |
| 1097 | |
| 1098 | switch (req) |
| 1099 | { |
| 1100 | case ADAPTER_REQ_RUN_XFER: |
| 1101 | xs = (struct scsipi_xfer *) arg; |
| 1102 | target = xs->xs_periph->periph_target; |
| 1103 | CISS_DPRINTF(CISS_D_CMD, ("targ=%d " , target)); |
| 1104 | if (xs->cmdlen > CISS_MAX_CDB) { |
| 1105 | CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p " , xs)); |
| 1106 | memset(&xs->sense, 0, sizeof(xs->sense)); |
| 1107 | xs->error = XS_SENSE; |
| 1108 | printf("ciss driver stuffup in %s:%d: %s()\n" , |
| 1109 | __FILE__, __LINE__, __func__); |
| 1110 | scsipi_done(xs); |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | xs->error = XS_NOERROR; |
| 1115 | |
| 1116 | /* XXX emulate SYNCHRONIZE_CACHE ??? */ |
| 1117 | |
| 1118 | ccb = ciss_get_ccb(sc); |
| 1119 | cmd = &ccb->ccb_cmd; |
| 1120 | ccb->ccb_len = xs->datalen; |
| 1121 | ccb->ccb_data = xs->data; |
| 1122 | ccb->ccb_xs = xs; |
| 1123 | cmd->tgt = CISS_CMD_MODE_LD | target; |
| 1124 | cmd->tgt2 = 0; |
| 1125 | cmd->cdblen = xs->cmdlen; |
| 1126 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL; |
| 1127 | if (xs->xs_control & XS_CTL_DATA_IN) |
| 1128 | cmd->flags |= CISS_CDB_IN; |
| 1129 | else if (xs->xs_control & XS_CTL_DATA_OUT) |
| 1130 | cmd->flags |= CISS_CDB_OUT; |
| 1131 | cmd->tmo = htole16(xs->timeout < 1000? 1 : xs->timeout / 1000); |
| 1132 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 1133 | memcpy(&cmd->cdb[0], xs->cmd, CISS_MAX_CDB); |
| 1134 | CISS_DPRINTF(CISS_D_CMD, ("cmd=%02x %02x %02x %02x %02x %02x " , |
| 1135 | cmd->cdb[0], cmd->cdb[1], cmd->cdb[2], |
| 1136 | cmd->cdb[3], cmd->cdb[4], cmd->cdb[5])); |
| 1137 | |
| 1138 | if (ciss_cmd(ccb, BUS_DMA_WAITOK, |
| 1139 | xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) { |
| 1140 | printf("ciss driver stuffup in %s:%d: %s()\n" , |
| 1141 | __FILE__, __LINE__, __func__); |
| 1142 | xs->error = XS_DRIVER_STUFFUP; |
| 1143 | scsipi_done(xs); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | break; |
| 1148 | case ADAPTER_REQ_GROW_RESOURCES: |
| 1149 | /* |
| 1150 | * Not supported. |
| 1151 | */ |
| 1152 | break; |
| 1153 | case ADAPTER_REQ_SET_XFER_MODE: |
| 1154 | /* |
| 1155 | * We can't change the transfer mode, but at least let |
| 1156 | * scsipi know what the adapter has negociated. |
| 1157 | */ |
| 1158 | xm = (struct scsipi_xfer_mode *)arg; |
| 1159 | xm->xm_mode |= PERIPH_CAP_TQING; |
| 1160 | scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm); |
| 1161 | break; |
| 1162 | default: |
| 1163 | printf("%s: %d %d unsupported\n" , __func__, __LINE__, req); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | int |
| 1168 | ciss_intr(void *v) |
| 1169 | { |
| 1170 | struct ciss_softc *sc = v; |
| 1171 | struct ciss_ccb *ccb; |
| 1172 | u_int32_t id; |
| 1173 | bus_size_t reg; |
| 1174 | int hit = 0; |
| 1175 | |
| 1176 | CISS_DPRINTF(CISS_D_INTR, ("intr " )); |
| 1177 | |
| 1178 | if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem)) |
| 1179 | return 0; |
| 1180 | |
| 1181 | if (sc->cfg.methods & CISS_METH_FIFO64) |
| 1182 | reg = CISS_OUTQ64_HI; |
| 1183 | else if (sc->cfg.methods & CISS_METH_FIFO64_RRO) |
| 1184 | reg = CISS_OUTQ64_LO; |
| 1185 | else |
| 1186 | reg = CISS_OUTQ; |
| 1187 | while ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg)) != |
| 1188 | 0xffffffff) { |
| 1189 | if (reg == CISS_OUTQ64_HI) |
| 1190 | id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, |
| 1191 | CISS_OUTQ64_LO); |
| 1192 | else if (reg == CISS_OUTQ64_LO) |
| 1193 | (void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, |
| 1194 | CISS_OUTQ64_HI); |
| 1195 | |
| 1196 | ccb = (struct ciss_ccb *) ((char *)sc->ccbs + (id >> 2) * sc->ccblen); |
| 1197 | ccb->ccb_cmd.id = htole32(id); |
| 1198 | ccb->ccb_cmd.id_hi = htole32(0); /* ignore the upper 32bits */ |
| 1199 | if (ccb->ccb_state == CISS_CCB_POLL) { |
| 1200 | ccb->ccb_state = CISS_CCB_ONQ; |
| 1201 | mutex_enter(&sc->sc_mutex); |
| 1202 | cv_broadcast(&sc->sc_condvar); |
| 1203 | mutex_exit(&sc->sc_mutex); |
| 1204 | } else |
| 1205 | ciss_done(ccb); |
| 1206 | |
| 1207 | hit = 1; |
| 1208 | } |
| 1209 | |
| 1210 | CISS_DPRINTF(CISS_D_INTR, ("exit\n" )); |
| 1211 | return hit; |
| 1212 | } |
| 1213 | |
| 1214 | static void |
| 1215 | ciss_heartbeat(void *v) |
| 1216 | { |
| 1217 | struct ciss_softc *sc = v; |
| 1218 | u_int32_t hb; |
| 1219 | |
| 1220 | hb = bus_space_read_4(sc->sc_iot, sc->cfg_ioh, |
| 1221 | sc->cfgoff + offsetof(struct ciss_config, heartbeat)); |
| 1222 | if (hb == sc->heartbeat) { |
| 1223 | sc->fibrillation++; |
| 1224 | CISS_DPRINTF(CISS_D_ERR, ("%s: fibrillation #%d (value=%d)\n" , |
| 1225 | device_xname(sc->sc_dev), sc->fibrillation, hb)); |
| 1226 | if (sc->fibrillation >= 11) { |
| 1227 | /* No heartbeat for 33 seconds */ |
| 1228 | panic("%s: dead" , device_xname(sc->sc_dev)); /* XXX reset! */ |
| 1229 | } |
| 1230 | } else { |
| 1231 | sc->heartbeat = hb; |
| 1232 | if (sc->fibrillation) { |
| 1233 | CISS_DPRINTF(CISS_D_ERR, ("%s: " |
| 1234 | "fibrillation ended (value=%d)\n" , |
| 1235 | device_xname(sc->sc_dev), hb)); |
| 1236 | } |
| 1237 | sc->fibrillation = 0; |
| 1238 | } |
| 1239 | |
| 1240 | callout_schedule(&sc->sc_hb, hz * 3); |
| 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd, |
| 1245 | void *addr, int flag, struct proc *p) |
| 1246 | { |
| 1247 | #if NBIO > 0 |
| 1248 | return ciss_ioctl(chan->chan_adapter->adapt_dev, cmd, addr); |
| 1249 | #else |
| 1250 | return ENOTTY; |
| 1251 | #endif |
| 1252 | } |
| 1253 | |
| 1254 | #if NBIO > 0 |
| 1255 | const int ciss_level[] = { 0, 4, 1, 5, 51, 7 }; |
| 1256 | const int ciss_stat[] = { BIOC_SVONLINE, BIOC_SVOFFLINE, BIOC_SVOFFLINE, |
| 1257 | BIOC_SVDEGRADED, BIOC_SVREBUILD, BIOC_SVREBUILD, BIOC_SVDEGRADED, |
| 1258 | BIOC_SVDEGRADED, BIOC_SVINVALID, BIOC_SVINVALID, BIOC_SVBUILDING, |
| 1259 | BIOC_SVOFFLINE, BIOC_SVBUILDING }; |
| 1260 | |
| 1261 | int |
| 1262 | ciss_ioctl(device_t dev, u_long cmd, void *addr) |
| 1263 | { |
| 1264 | struct ciss_softc *sc = device_private(dev); |
| 1265 | struct bioc_inq *bi; |
| 1266 | struct bioc_disk *bd; |
| 1267 | struct bioc_blink *bb; |
| 1268 | struct ciss_ldstat *ldstat; |
| 1269 | struct ciss_pdid *pdid; |
| 1270 | struct ciss_blink *blink; |
| 1271 | struct ciss_ld *ldp; |
| 1272 | u_int8_t drv; |
| 1273 | int ld, pd, error = 0; |
| 1274 | |
| 1275 | switch (cmd) { |
| 1276 | case BIOCINQ: |
| 1277 | bi = (struct bioc_inq *)addr; |
| 1278 | strlcpy(bi->bi_dev, device_xname(sc->sc_dev), sizeof(bi->bi_dev)); |
| 1279 | bi->bi_novol = sc->maxunits; |
| 1280 | bi->bi_nodisk = sc->sc_lds[0]->ndrives; |
| 1281 | break; |
| 1282 | |
| 1283 | case BIOCVOL: |
| 1284 | error = ciss_ioctl_vol(sc, (struct bioc_vol *)addr); |
| 1285 | break; |
| 1286 | |
| 1287 | case BIOCDISK_NOVOL: |
| 1288 | /* |
| 1289 | * XXX since we don't know how to associate physical drives with logical drives |
| 1290 | * yet, BIOCDISK_NOVOL is equivalent to BIOCDISK to the volume that we've |
| 1291 | * associated all physical drives to. |
| 1292 | * Maybe assoicate all physical drives to all logical volumes, but only return |
| 1293 | * physical drives on one logical volume. Which one? Either 1st volume that |
| 1294 | * is degraded, rebuilding, or failed? |
| 1295 | */ |
| 1296 | bd = (struct bioc_disk *)addr; |
| 1297 | bd->bd_volid = 0; |
| 1298 | bd->bd_disknovol = true; |
| 1299 | /* FALLTHROUGH */ |
| 1300 | case BIOCDISK: |
| 1301 | bd = (struct bioc_disk *)addr; |
| 1302 | if (bd->bd_volid > sc->maxunits) { |
| 1303 | error = EINVAL; |
| 1304 | break; |
| 1305 | } |
| 1306 | ldp = sc->sc_lds[0]; |
| 1307 | if (!ldp || (pd = bd->bd_diskid) > ldp->ndrives) { |
| 1308 | error = EINVAL; |
| 1309 | break; |
| 1310 | } |
| 1311 | ldstat = sc->scratch; |
| 1312 | if ((error = ciss_ldstat(sc, bd->bd_volid, ldstat))) { |
| 1313 | break; |
| 1314 | } |
| 1315 | bd->bd_status = -1; |
| 1316 | if (ldstat->stat == CISS_LD_REBLD && |
| 1317 | ldstat->bigrebuild == ldp->tgts[pd]) |
| 1318 | bd->bd_status = BIOC_SDREBUILD; |
| 1319 | if (ciss_bitset(ldp->tgts[pd] & (~CISS_BIGBIT), |
| 1320 | ldstat->bigfailed)) { |
| 1321 | bd->bd_status = BIOC_SDFAILED; |
| 1322 | bd->bd_size = 0; |
| 1323 | bd->bd_channel = (ldp->tgts[pd] & (~CISS_BIGBIT)) / |
| 1324 | sc->ndrives; |
| 1325 | bd->bd_target = ldp->tgts[pd] % sc->ndrives; |
| 1326 | bd->bd_lun = 0; |
| 1327 | bd->bd_vendor[0] = '\0'; |
| 1328 | bd->bd_serial[0] = '\0'; |
| 1329 | bd->bd_procdev[0] = '\0'; |
| 1330 | } else { |
| 1331 | pdid = sc->scratch; |
| 1332 | if ((error = ciss_pdid(sc, ldp->tgts[pd], pdid, |
| 1333 | XS_CTL_POLL))) { |
| 1334 | bd->bd_status = BIOC_SDFAILED; |
| 1335 | bd->bd_size = 0; |
| 1336 | bd->bd_channel = (ldp->tgts[pd] & (~CISS_BIGBIT)) / |
| 1337 | sc->ndrives; |
| 1338 | bd->bd_target = ldp->tgts[pd] % sc->ndrives; |
| 1339 | bd->bd_lun = 0; |
| 1340 | bd->bd_vendor[0] = '\0'; |
| 1341 | bd->bd_serial[0] = '\0'; |
| 1342 | bd->bd_procdev[0] = '\0'; |
| 1343 | error = 0; |
| 1344 | break; |
| 1345 | } |
| 1346 | if (bd->bd_status < 0) { |
| 1347 | if (pdid->config & CISS_PD_SPARE) |
| 1348 | bd->bd_status = BIOC_SDHOTSPARE; |
| 1349 | else if (pdid->present & CISS_PD_PRESENT) |
| 1350 | bd->bd_status = BIOC_SDONLINE; |
| 1351 | else |
| 1352 | bd->bd_status = BIOC_SDINVALID; |
| 1353 | } |
| 1354 | bd->bd_size = (u_int64_t)le32toh(pdid->nblocks) * |
| 1355 | le16toh(pdid->blksz); |
| 1356 | bd->bd_channel = pdid->bus; |
| 1357 | bd->bd_target = pdid->target; |
| 1358 | bd->bd_lun = 0; |
| 1359 | strlcpy(bd->bd_vendor, pdid->model, |
| 1360 | sizeof(bd->bd_vendor)); |
| 1361 | strlcpy(bd->bd_serial, pdid->serial, |
| 1362 | sizeof(bd->bd_serial)); |
| 1363 | bd->bd_procdev[0] = '\0'; |
| 1364 | } |
| 1365 | break; |
| 1366 | |
| 1367 | case BIOCBLINK: |
| 1368 | bb = (struct bioc_blink *)addr; |
| 1369 | blink = sc->scratch; |
| 1370 | error = EINVAL; |
| 1371 | /* XXX workaround completely dumb scsi addressing */ |
| 1372 | for (ld = 0; ld < sc->maxunits; ld++) { |
| 1373 | ldp = sc->sc_lds[ld]; |
| 1374 | if (!ldp) |
| 1375 | continue; |
| 1376 | if (sc->ndrives == 256) |
| 1377 | drv = bb->bb_target; |
| 1378 | else |
| 1379 | drv = CISS_BIGBIT + |
| 1380 | bb->bb_channel * sc->ndrives + |
| 1381 | bb->bb_target; |
| 1382 | for (pd = 0; pd < ldp->ndrives; pd++) |
| 1383 | if (ldp->tgts[pd] == drv) |
| 1384 | error = ciss_blink(sc, ld, pd, |
| 1385 | bb->bb_status, blink); |
| 1386 | } |
| 1387 | break; |
| 1388 | |
| 1389 | case BIOCALARM: |
| 1390 | case BIOCSETSTATE: |
| 1391 | default: |
| 1392 | error = EINVAL; |
| 1393 | } |
| 1394 | |
| 1395 | return (error); |
| 1396 | } |
| 1397 | |
| 1398 | int |
| 1399 | ciss_ioctl_vol(struct ciss_softc *sc, struct bioc_vol *bv) |
| 1400 | { |
| 1401 | struct ciss_ldid *ldid; |
| 1402 | struct ciss_ld *ldp; |
| 1403 | struct ciss_ldstat *ldstat; |
| 1404 | struct ciss_pdid *pdid; |
| 1405 | int error = 0; |
| 1406 | u_int blks; |
| 1407 | |
| 1408 | if (bv->bv_volid > sc->maxunits) { |
| 1409 | return EINVAL; |
| 1410 | } |
| 1411 | ldp = sc->sc_lds[bv->bv_volid]; |
| 1412 | ldid = sc->scratch; |
| 1413 | if ((error = ciss_ldid(sc, bv->bv_volid, ldid))) { |
| 1414 | return error; |
| 1415 | } |
| 1416 | bv->bv_status = BIOC_SVINVALID; |
| 1417 | blks = (u_int)le16toh(ldid->nblocks[1]) << 16 | |
| 1418 | le16toh(ldid->nblocks[0]); |
| 1419 | bv->bv_size = blks * (u_quad_t)le16toh(ldid->blksize); |
| 1420 | bv->bv_level = ciss_level[ldid->type]; |
| 1421 | /* |
| 1422 | * XXX Should only return bv_nodisk for logigal volume that we've associated |
| 1423 | * the physical drives to: either the 1st degraded, rebuilding, or failed |
| 1424 | * volume else volume 0? |
| 1425 | */ |
| 1426 | if (ldp) { |
| 1427 | bv->bv_nodisk = ldp->ndrives; |
| 1428 | strlcpy(bv->bv_dev, ldp->xname, sizeof(bv->bv_dev)); |
| 1429 | } |
| 1430 | strlcpy(bv->bv_vendor, "CISS" , sizeof(bv->bv_vendor)); |
| 1431 | ldstat = sc->scratch; |
| 1432 | memset(ldstat, 0, sizeof(*ldstat)); |
| 1433 | if ((error = ciss_ldstat(sc, bv->bv_volid, ldstat))) { |
| 1434 | return error; |
| 1435 | } |
| 1436 | bv->bv_percent = -1; |
| 1437 | bv->bv_seconds = 0; |
| 1438 | if (ldstat->stat < sizeof(ciss_stat)/sizeof(ciss_stat[0])) |
| 1439 | bv->bv_status = ciss_stat[ldstat->stat]; |
| 1440 | if (bv->bv_status == BIOC_SVREBUILD || |
| 1441 | bv->bv_status == BIOC_SVBUILDING) { |
| 1442 | u_int64_t prog; |
| 1443 | |
| 1444 | ldp = sc->sc_lds[0]; |
| 1445 | if (ldp) { |
| 1446 | bv->bv_nodisk = ldp->ndrives; |
| 1447 | strlcpy(bv->bv_dev, ldp->xname, sizeof(bv->bv_dev)); |
| 1448 | } |
| 1449 | /* |
| 1450 | * XXX ldstat->prog is blocks remaining on physical drive being rebuilt |
| 1451 | * blks is only correct for a RAID1 set; RAID5 needs to determine the |
| 1452 | * size of the physical device - which we don't yet know. |
| 1453 | * ldstat->bigrebuild has physical device target, so could be used with |
| 1454 | * pdid to get size. Another way is to save pd information in sc so it's |
| 1455 | * easy to reference. |
| 1456 | */ |
| 1457 | prog = (u_int64_t)((ldstat->prog[3] << 24) | |
| 1458 | (ldstat->prog[2] << 16) | (ldstat->prog[1] << 8) | |
| 1459 | ldstat->prog[0]); |
| 1460 | pdid = sc->scratch; |
| 1461 | if (!ciss_pdid(sc, ldstat->bigrebuild, pdid, XS_CTL_POLL)) { |
| 1462 | blks = le32toh(pdid->nblocks); |
| 1463 | bv->bv_percent = (blks - prog) * 1000ULL / blks; |
| 1464 | } |
| 1465 | } |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | int |
| 1470 | ciss_blink(struct ciss_softc *sc, int ld, int pd, int stat, |
| 1471 | struct ciss_blink *blink) |
| 1472 | { |
| 1473 | struct ciss_ccb *ccb; |
| 1474 | struct ciss_cmd *cmd; |
| 1475 | struct ciss_ld *ldp; |
| 1476 | |
| 1477 | if (ld > sc->maxunits) |
| 1478 | return EINVAL; |
| 1479 | |
| 1480 | ldp = sc->sc_lds[ld]; |
| 1481 | if (!ldp || pd > ldp->ndrives) |
| 1482 | return EINVAL; |
| 1483 | |
| 1484 | ldp->bling.pdtab[ldp->tgts[pd]] = stat == BIOC_SBUNBLINK? 0 : |
| 1485 | CISS_BLINK_ALL; |
| 1486 | memcpy(blink, &ldp->bling, sizeof(*blink)); |
| 1487 | |
| 1488 | ccb = ciss_get_ccb(sc); |
| 1489 | if (ccb == NULL) |
| 1490 | return ENOMEM; |
| 1491 | ccb->ccb_len = sizeof(*blink); |
| 1492 | ccb->ccb_data = blink; |
| 1493 | ccb->ccb_xs = NULL; |
| 1494 | cmd = &ccb->ccb_cmd; |
| 1495 | cmd->tgt = htole32(CISS_CMD_MODE_PERIPH); |
| 1496 | cmd->tgt2 = 0; |
| 1497 | cmd->cdblen = 10; |
| 1498 | cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT; |
| 1499 | cmd->tmo = htole16(0); |
| 1500 | memset(&cmd->cdb[0], 0, sizeof(cmd->cdb)); |
| 1501 | cmd->cdb[0] = CISS_CMD_CTRL_SET; |
| 1502 | cmd->cdb[6] = CISS_CMS_CTRL_PDBLINK; |
| 1503 | cmd->cdb[7] = sizeof(*blink) >> 8; /* biiiig endian */ |
| 1504 | cmd->cdb[8] = sizeof(*blink) & 0xff; |
| 1505 | |
| 1506 | return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL); |
| 1507 | } |
| 1508 | |
| 1509 | int |
| 1510 | ciss_create_sensors(struct ciss_softc *sc) |
| 1511 | { |
| 1512 | int i; |
| 1513 | int nsensors = sc->maxunits; |
| 1514 | |
| 1515 | if (nsensors == 0) { |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | sc->sc_sme = sysmon_envsys_create(); |
| 1520 | sc->sc_sensor = malloc(sizeof(envsys_data_t) * nsensors, |
| 1521 | M_DEVBUF, M_NOWAIT | M_ZERO); |
| 1522 | if (sc->sc_sensor == NULL) { |
| 1523 | aprint_error_dev(sc->sc_dev, "can't allocate envsys_data" ); |
| 1524 | return(ENOMEM); |
| 1525 | } |
| 1526 | |
| 1527 | for (i = 0; i < nsensors; i++) { |
| 1528 | sc->sc_sensor[i].units = ENVSYS_DRIVE; |
| 1529 | sc->sc_sensor[i].state = ENVSYS_SINVALID; |
| 1530 | sc->sc_sensor[i].value_cur = ENVSYS_DRIVE_EMPTY; |
| 1531 | /* Enable monitoring for drive state changes */ |
| 1532 | sc->sc_sensor[i].flags |= ENVSYS_FMONSTCHANGED; |
| 1533 | /* logical drives */ |
| 1534 | snprintf(sc->sc_sensor[i].desc, |
| 1535 | sizeof(sc->sc_sensor[i].desc), "%s:%d" , |
| 1536 | device_xname(sc->sc_dev), i); |
| 1537 | if (sysmon_envsys_sensor_attach(sc->sc_sme, |
| 1538 | &sc->sc_sensor[i])) |
| 1539 | goto out; |
| 1540 | } |
| 1541 | |
| 1542 | sc->sc_sme->sme_name = device_xname(sc->sc_dev); |
| 1543 | sc->sc_sme->sme_cookie = sc; |
| 1544 | sc->sc_sme->sme_refresh = ciss_sensor_refresh; |
| 1545 | if (sysmon_envsys_register(sc->sc_sme)) { |
| 1546 | printf("%s: unable to register with sysmon\n" , |
| 1547 | device_xname(sc->sc_dev)); |
| 1548 | return(1); |
| 1549 | } |
| 1550 | return (0); |
| 1551 | |
| 1552 | out: |
| 1553 | free(sc->sc_sensor, M_DEVBUF); |
| 1554 | sysmon_envsys_destroy(sc->sc_sme); |
| 1555 | return EINVAL; |
| 1556 | } |
| 1557 | |
| 1558 | void |
| 1559 | ciss_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) |
| 1560 | { |
| 1561 | struct ciss_softc *sc = sme->sme_cookie; |
| 1562 | struct bioc_vol bv; |
| 1563 | |
| 1564 | if (edata->sensor >= sc->maxunits) |
| 1565 | return; |
| 1566 | |
| 1567 | memset(&bv, 0, sizeof(bv)); |
| 1568 | bv.bv_volid = edata->sensor; |
| 1569 | if (ciss_ioctl_vol(sc, &bv)) |
| 1570 | bv.bv_status = BIOC_SVINVALID; |
| 1571 | |
| 1572 | bio_vol_to_envsys(edata, &bv); |
| 1573 | } |
| 1574 | #endif /* NBIO > 0 */ |
| 1575 | |