1/* $NetBSD: arcmsr.c,v 1.45 2024/02/09 17:57:03 andvar Exp $ */ 2/* $OpenBSD: arc.c,v 1.68 2007/10/27 03:28:27 dlg Exp $ */ 3 4/* 5 * Copyright (c) 2007, 2008 Juan Romero Pardines <xtraeme@netbsd.org> 6 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org> 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 USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21#include "bio.h" 22 23#include <sys/cdefs.h> 24__KERNEL_RCSID(0, "$NetBSD: arcmsr.c,v 1.45 2024/02/09 17:57:03 andvar Exp $"); 25 26#include <sys/param.h> 27#include <sys/buf.h> 28#include <sys/kernel.h> 29#include <sys/device.h> 30#include <sys/kmem.h> 31#include <sys/kthread.h> 32#include <sys/mutex.h> 33#include <sys/condvar.h> 34#include <sys/rwlock.h> 35 36#if NBIO > 0 37#include <sys/ioctl.h> 38#include <dev/biovar.h> 39#endif 40 41#include <dev/pci/pcireg.h> 42#include <dev/pci/pcivar.h> 43#include <dev/pci/pcidevs.h> 44 45#include <dev/scsipi/scsipi_all.h> 46#include <dev/scsipi/scsi_all.h> 47#include <dev/scsipi/scsiconf.h> 48 49#include <dev/sysmon/sysmonvar.h> 50 51#include <sys/bus.h> 52 53#include <dev/pci/arcmsrvar.h> 54 55/* #define ARC_DEBUG */ 56#ifdef ARC_DEBUG 57#define ARC_D_INIT (1<<0) 58#define ARC_D_RW (1<<1) 59#define ARC_D_DB (1<<2) 60 61int arcdebug = 0; 62 63#define DPRINTF(p...) do { if (arcdebug) printf(p); } while (0) 64#define DNPRINTF(n, p...) do { if ((n) & arcdebug) printf(p); } while (0) 65 66#else 67#define DPRINTF(p, ...) /* p */ 68#define DNPRINTF(n, p, ...) /* n, p */ 69#endif 70 71/* 72 * the fw header must always equal this. 73 */ 74#if NBIO > 0 75static struct arc_fw_hdr arc_fw_hdr = { 0x5e, 0x01, 0x61 }; 76#endif 77 78/* 79 * autoconf(9) glue. 80 */ 81static int arc_match(device_t, cfdata_t, void *); 82static void arc_attach(device_t, device_t, void *); 83static int arc_detach(device_t, int); 84static bool arc_shutdown(device_t, int); 85static int arc_intr(void *); 86static void arc_minphys(struct buf *); 87 88CFATTACH_DECL_NEW(arcmsr, sizeof(struct arc_softc), 89 arc_match, arc_attach, arc_detach, NULL); 90 91/* 92 * bio(4) and sysmon_envsys(9) glue. 93 */ 94#if NBIO > 0 95static int arc_bioctl(device_t, u_long, void *); 96static int arc_bio_inq(struct arc_softc *, struct bioc_inq *); 97static int arc_bio_vol(struct arc_softc *, struct bioc_vol *); 98static int arc_bio_disk_volume(struct arc_softc *, struct bioc_disk *); 99static int arc_bio_disk_novol(struct arc_softc *, struct bioc_disk *); 100static void arc_bio_disk_filldata(struct arc_softc *, struct bioc_disk *, 101 struct arc_fw_diskinfo *, int); 102static int arc_bio_alarm(struct arc_softc *, struct bioc_alarm *); 103static int arc_bio_alarm_state(struct arc_softc *, struct bioc_alarm *); 104static int arc_bio_getvol(struct arc_softc *, int, 105 struct arc_fw_volinfo *); 106static int arc_bio_setstate(struct arc_softc *, struct bioc_setstate *); 107static int arc_bio_volops(struct arc_softc *, struct bioc_volops *); 108static void arc_create_sensors(void *); 109static void arc_refresh_sensors(struct sysmon_envsys *, envsys_data_t *); 110static int arc_fw_parse_status_code(struct arc_softc *, uint8_t *); 111#endif 112 113/* 114 * interface for scsi midlayer to talk to. 115 */ 116static void arc_scsi_cmd(struct scsipi_channel *, scsipi_adapter_req_t, 117 void *); 118 119/* 120 * code to deal with getting bits in and out of the bus space. 121 */ 122static uint32_t arc_read(struct arc_softc *, bus_size_t); 123static void arc_read_region(struct arc_softc *, bus_size_t, void *, 124 size_t); 125static void arc_write(struct arc_softc *, bus_size_t, uint32_t); 126#if NBIO > 0 127static void arc_write_region(struct arc_softc *, bus_size_t, void *, 128 size_t); 129#endif 130static int arc_wait_eq(struct arc_softc *, bus_size_t, uint32_t, 131 uint32_t); 132#ifdef unused 133static int arc_wait_ne(struct arc_softc *, bus_size_t, uint32_t, 134 uint32_t); 135#endif 136static int arc_msg0(struct arc_softc *, uint32_t); 137static struct arc_dmamem *arc_dmamem_alloc(struct arc_softc *, size_t); 138static void arc_dmamem_free(struct arc_softc *, 139 struct arc_dmamem *); 140 141static int arc_alloc_ccbs(device_t); 142static struct arc_ccb *arc_get_ccb(struct arc_softc *); 143static void arc_put_ccb(struct arc_softc *, struct arc_ccb *); 144static int arc_load_xs(struct arc_ccb *); 145static int arc_complete(struct arc_softc *, struct arc_ccb *, int); 146static void arc_scsi_cmd_done(struct arc_softc *, struct arc_ccb *, 147 uint32_t); 148 149/* 150 * real stuff for dealing with the hardware. 151 */ 152static int arc_map_pci_resources(device_t, struct pci_attach_args *); 153static void arc_unmap_pci_resources(struct arc_softc *); 154static int arc_query_firmware(device_t); 155 156/* 157 * stuff to do messaging via the doorbells. 158 */ 159#if NBIO > 0 160static void arc_lock(struct arc_softc *); 161static void arc_unlock(struct arc_softc *); 162static void arc_wait(struct arc_softc *); 163static uint8_t arc_msg_cksum(void *, uint16_t); 164static int arc_msgbuf(struct arc_softc *, void *, size_t, void *, size_t); 165#endif 166 167#define arc_push(_s, _r) arc_write((_s), ARC_REG_POST_QUEUE, (_r)) 168#define arc_pop(_s) arc_read((_s), ARC_REG_REPLY_QUEUE) 169 170static int 171arc_match(device_t parent, cfdata_t match, void *aux) 172{ 173 struct pci_attach_args *pa = aux; 174 175 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ARECA) { 176 switch (PCI_PRODUCT(pa->pa_id)) { 177 case PCI_PRODUCT_ARECA_ARC1110: 178 case PCI_PRODUCT_ARECA_ARC1120: 179 case PCI_PRODUCT_ARECA_ARC1130: 180 case PCI_PRODUCT_ARECA_ARC1160: 181 case PCI_PRODUCT_ARECA_ARC1170: 182 case PCI_PRODUCT_ARECA_ARC1200: 183 case PCI_PRODUCT_ARECA_ARC1202: 184 case PCI_PRODUCT_ARECA_ARC1210: 185 case PCI_PRODUCT_ARECA_ARC1220: 186 case PCI_PRODUCT_ARECA_ARC1230: 187 case PCI_PRODUCT_ARECA_ARC1260: 188 case PCI_PRODUCT_ARECA_ARC1270: 189 case PCI_PRODUCT_ARECA_ARC1280: 190 case PCI_PRODUCT_ARECA_ARC1380: 191 case PCI_PRODUCT_ARECA_ARC1381: 192 case PCI_PRODUCT_ARECA_ARC1680: 193 case PCI_PRODUCT_ARECA_ARC1681: 194 return 1; 195 default: 196 break; 197 } 198 } 199 200 return 0; 201} 202 203static void 204arc_attach(device_t parent, device_t self, void *aux) 205{ 206 struct arc_softc *sc = device_private(self); 207 struct pci_attach_args *pa = aux; 208 struct scsipi_adapter *adapt = &sc->sc_adapter; 209 struct scsipi_channel *chan = &sc->sc_chan; 210 211 sc->sc_dev = self; 212 sc->sc_talking = 0; 213 rw_init(&sc->sc_rwlock); 214 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_BIO); 215 cv_init(&sc->sc_condvar, "arcdb"); 216 217 if (arc_map_pci_resources(self, pa) != 0) { 218 /* error message printed by arc_map_pci_resources */ 219 return; 220 } 221 222 if (arc_query_firmware(self) != 0) { 223 /* error message printed by arc_query_firmware */ 224 goto unmap_pci; 225 } 226 227 if (arc_alloc_ccbs(self) != 0) { 228 /* error message printed by arc_alloc_ccbs */ 229 goto unmap_pci; 230 } 231 232 if (!pmf_device_register1(self, NULL, NULL, arc_shutdown)) 233 panic("%s: couldn't establish shutdown handler\n", 234 device_xname(self)); 235 236 memset(adapt, 0, sizeof(*adapt)); 237 adapt->adapt_dev = self; 238 adapt->adapt_nchannels = 1; 239 adapt->adapt_openings = sc->sc_req_count / ARC_MAX_TARGET; 240 adapt->adapt_max_periph = adapt->adapt_openings; 241 adapt->adapt_minphys = arc_minphys; 242 adapt->adapt_request = arc_scsi_cmd; 243 adapt->adapt_flags = SCSIPI_ADAPT_MPSAFE; 244 245 memset(chan, 0, sizeof(*chan)); 246 chan->chan_adapter = adapt; 247 chan->chan_bustype = &scsi_bustype; 248 chan->chan_nluns = ARC_MAX_LUN; 249 chan->chan_ntargets = ARC_MAX_TARGET; 250 chan->chan_id = ARC_MAX_TARGET; 251 chan->chan_flags = SCSIPI_CHAN_NOSETTLE; 252 253 /* 254 * Save the device_t returned, because we could to attach 255 * devices via the management interface. 256 */ 257 sc->sc_scsibus_dv = config_found(self, &sc->sc_chan, scsiprint, 258 CFARGS_NONE); 259 260 /* enable interrupts */ 261 arc_write(sc, ARC_REG_INTRMASK, 262 ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRSTAT_DOORBELL)); 263 264#if NBIO > 0 265 /* 266 * Register the driver to bio(4) and setup the sensors. 267 */ 268 if (bio_register(self, arc_bioctl) != 0) 269 panic("%s: bioctl registration failed\n", device_xname(self)); 270 271 /* 272 * you need to talk to the firmware to get volume info. our firmware 273 * interface relies on being able to sleep, so we need to use a thread 274 * to do the work. 275 */ 276 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 277 arc_create_sensors, sc, &sc->sc_lwp, "arcmsr_sensors") != 0) 278 panic("%s: unable to create a kernel thread for sensors\n", 279 device_xname(self)); 280#endif 281 282 return; 283 284unmap_pci: 285 arc_unmap_pci_resources(sc); 286} 287 288static int 289arc_detach(device_t self, int flags) 290{ 291 struct arc_softc *sc = device_private(self); 292 293 if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0) 294 aprint_error_dev(self, "timeout waiting to stop bg rebuild\n"); 295 296 if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0) 297 aprint_error_dev(self, "timeout waiting to flush cache\n"); 298 299 if (sc->sc_sme != NULL) 300 sysmon_envsys_unregister(sc->sc_sme); 301 302 return 0; 303} 304 305static bool 306arc_shutdown(device_t self, int how) 307{ 308 struct arc_softc *sc = device_private(self); 309 310 if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0) 311 aprint_error_dev(self, "timeout waiting to stop bg rebuild\n"); 312 313 if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0) 314 aprint_error_dev(self, "timeout waiting to flush cache\n"); 315 316 return true; 317} 318 319static void 320arc_minphys(struct buf *bp) 321{ 322 if (bp->b_bcount > MAXPHYS) 323 bp->b_bcount = MAXPHYS; 324 minphys(bp); 325} 326 327static int 328arc_intr(void *arg) 329{ 330 struct arc_softc *sc = arg; 331 struct arc_ccb *ccb = NULL; 332 char *kva = ARC_DMA_KVA(sc->sc_requests); 333 struct arc_io_cmd *cmd; 334 uint32_t reg, intrstat; 335 336 mutex_spin_enter(&sc->sc_mutex); 337 intrstat = arc_read(sc, ARC_REG_INTRSTAT); 338 if (intrstat == 0x0) { 339 mutex_spin_exit(&sc->sc_mutex); 340 return 0; 341 } 342 343 intrstat &= ARC_REG_INTRSTAT_POSTQUEUE | ARC_REG_INTRSTAT_DOORBELL; 344 arc_write(sc, ARC_REG_INTRSTAT, intrstat); 345 346 if (intrstat & ARC_REG_INTRSTAT_DOORBELL) { 347 if (sc->sc_talking) { 348 arc_write(sc, ARC_REG_INTRMASK, 349 ~ARC_REG_INTRMASK_POSTQUEUE); 350 cv_broadcast(&sc->sc_condvar); 351 } else { 352 /* otherwise drop it */ 353 reg = arc_read(sc, ARC_REG_OUTB_DOORBELL); 354 arc_write(sc, ARC_REG_OUTB_DOORBELL, reg); 355 if (reg & ARC_REG_OUTB_DOORBELL_WRITE_OK) 356 arc_write(sc, ARC_REG_INB_DOORBELL, 357 ARC_REG_INB_DOORBELL_READ_OK); 358 } 359 } 360 mutex_spin_exit(&sc->sc_mutex); 361 362 while ((reg = arc_pop(sc)) != 0xffffffff) { 363 cmd = (struct arc_io_cmd *)(kva + 364 ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) - 365 (uint32_t)ARC_DMA_DVA(sc->sc_requests))); 366 ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)]; 367 368 bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests), 369 ccb->ccb_offset, ARC_MAX_IOCMDLEN, 370 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 371 372 arc_scsi_cmd_done(sc, ccb, reg); 373 } 374 375 376 return 1; 377} 378 379void 380arc_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg) 381{ 382 struct scsipi_periph *periph; 383 struct scsipi_xfer *xs; 384 struct scsipi_adapter *adapt = chan->chan_adapter; 385 struct arc_softc *sc = device_private(adapt->adapt_dev); 386 struct arc_ccb *ccb; 387 struct arc_msg_scsicmd *cmd; 388 uint32_t reg; 389 uint8_t target; 390 391 switch (req) { 392 case ADAPTER_REQ_GROW_RESOURCES: 393 /* Not supported. */ 394 return; 395 case ADAPTER_REQ_SET_XFER_MODE: 396 /* Not supported. */ 397 return; 398 case ADAPTER_REQ_RUN_XFER: 399 break; 400 } 401 402 mutex_spin_enter(&sc->sc_mutex); 403 404 xs = arg; 405 periph = xs->xs_periph; 406 target = periph->periph_target; 407 408 if (xs->cmdlen > ARC_MSG_CDBLEN) { 409 memset(&xs->sense, 0, sizeof(xs->sense)); 410 xs->sense.scsi_sense.response_code = SSD_RCODE_VALID | 0x70; 411 xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST; 412 xs->sense.scsi_sense.asc = 0x20; 413 xs->error = XS_SENSE; 414 xs->status = SCSI_CHECK; 415 mutex_spin_exit(&sc->sc_mutex); 416 scsipi_done(xs); 417 return; 418 } 419 420 ccb = arc_get_ccb(sc); 421 if (ccb == NULL) { 422 xs->error = XS_RESOURCE_SHORTAGE; 423 mutex_spin_exit(&sc->sc_mutex); 424 scsipi_done(xs); 425 return; 426 } 427 428 ccb->ccb_xs = xs; 429 430 if (arc_load_xs(ccb) != 0) { 431 xs->error = XS_DRIVER_STUFFUP; 432 arc_put_ccb(sc, ccb); 433 mutex_spin_exit(&sc->sc_mutex); 434 scsipi_done(xs); 435 return; 436 } 437 438 cmd = &ccb->ccb_cmd->cmd; 439 reg = ccb->ccb_cmd_post; 440 441 /* bus is always 0 */ 442 cmd->target = target; 443 cmd->lun = periph->periph_lun; 444 cmd->function = 1; /* XXX magic number */ 445 446 cmd->cdb_len = xs->cmdlen; 447 cmd->sgl_len = ccb->ccb_dmamap->dm_nsegs; 448 if (xs->xs_control & XS_CTL_DATA_OUT) 449 cmd->flags = ARC_MSG_SCSICMD_FLAG_WRITE; 450 if (ccb->ccb_dmamap->dm_nsegs > ARC_SGL_256LEN) { 451 cmd->flags |= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512; 452 reg |= ARC_REG_POST_QUEUE_BIGFRAME; 453 } 454 455 cmd->context = htole32(ccb->ccb_id); 456 cmd->data_len = htole32(xs->datalen); 457 458 memcpy(cmd->cdb, xs->cmd, xs->cmdlen); 459 460 /* we've built the command, let's put it on the hw */ 461 bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests), 462 ccb->ccb_offset, ARC_MAX_IOCMDLEN, 463 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 464 465 arc_push(sc, reg); 466 if (xs->xs_control & XS_CTL_POLL) { 467 if (arc_complete(sc, ccb, xs->timeout) != 0) { 468 xs->error = XS_DRIVER_STUFFUP; 469 mutex_spin_exit(&sc->sc_mutex); 470 scsipi_done(xs); 471 return; 472 } 473 } 474 475 mutex_spin_exit(&sc->sc_mutex); 476} 477 478int 479arc_load_xs(struct arc_ccb *ccb) 480{ 481 struct arc_softc *sc = ccb->ccb_sc; 482 struct scsipi_xfer *xs = ccb->ccb_xs; 483 bus_dmamap_t dmap = ccb->ccb_dmamap; 484 struct arc_sge *sgl = ccb->ccb_cmd->sgl, *sge; 485 uint64_t addr; 486 int i, error; 487 488 if (xs->datalen == 0) 489 return 0; 490 491 error = bus_dmamap_load(sc->sc_dmat, dmap, 492 xs->data, xs->datalen, NULL, 493 (xs->xs_control & XS_CTL_NOSLEEP) ? 494 BUS_DMA_NOWAIT : BUS_DMA_WAITOK); 495 if (error != 0) { 496 aprint_error("%s: error %d loading dmamap\n", 497 device_xname(sc->sc_dev), error); 498 return 1; 499 } 500 501 for (i = 0; i < dmap->dm_nsegs; i++) { 502 sge = &sgl[i]; 503 504 sge->sg_hdr = htole32(ARC_SGE_64BIT | dmap->dm_segs[i].ds_len); 505 addr = dmap->dm_segs[i].ds_addr; 506 sge->sg_hi_addr = htole32((uint32_t)(addr >> 32)); 507 sge->sg_lo_addr = htole32((uint32_t)addr); 508 } 509 510 bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize, 511 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD : 512 BUS_DMASYNC_PREWRITE); 513 514 return 0; 515} 516 517void 518arc_scsi_cmd_done(struct arc_softc *sc, struct arc_ccb *ccb, uint32_t reg) 519{ 520 struct scsipi_xfer *xs = ccb->ccb_xs; 521 struct arc_msg_scsicmd *cmd; 522 523 if (xs->datalen != 0) { 524 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0, 525 ccb->ccb_dmamap->dm_mapsize, 526 (xs->xs_control & XS_CTL_DATA_IN) ? 527 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 528 bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap); 529 } 530 531 /* timeout_del */ 532 xs->status |= XS_STS_DONE; 533 534 if (reg & ARC_REG_REPLY_QUEUE_ERR) { 535 cmd = &ccb->ccb_cmd->cmd; 536 537 switch (cmd->status) { 538 case ARC_MSG_STATUS_SELTIMEOUT: 539 case ARC_MSG_STATUS_ABORTED: 540 case ARC_MSG_STATUS_INIT_FAIL: 541 xs->status = SCSI_OK; 542 xs->error = XS_SELTIMEOUT; 543 break; 544 545 case SCSI_CHECK: 546 memset(&xs->sense, 0, sizeof(xs->sense)); 547 memcpy(&xs->sense, cmd->sense_data, 548 uimin(ARC_MSG_SENSELEN, sizeof(xs->sense))); 549 xs->sense.scsi_sense.response_code = 550 SSD_RCODE_VALID | 0x70; 551 xs->status = SCSI_CHECK; 552 xs->error = XS_SENSE; 553 xs->resid = 0; 554 break; 555 556 default: 557 /* unknown device status */ 558 xs->error = XS_BUSY; /* try again later? */ 559 xs->status = SCSI_BUSY; 560 break; 561 } 562 } else { 563 xs->status = SCSI_OK; 564 xs->error = XS_NOERROR; 565 xs->resid = 0; 566 } 567 568 arc_put_ccb(sc, ccb); 569 scsipi_done(xs); 570} 571 572int 573arc_complete(struct arc_softc *sc, struct arc_ccb *nccb, int timeout) 574{ 575 struct arc_ccb *ccb = NULL; 576 char *kva = ARC_DMA_KVA(sc->sc_requests); 577 struct arc_io_cmd *cmd; 578 uint32_t reg; 579 580 do { 581 reg = arc_pop(sc); 582 if (reg == 0xffffffff) { 583 if (timeout-- == 0) 584 return 1; 585 586 delay(1000); 587 continue; 588 } 589 590 cmd = (struct arc_io_cmd *)(kva + 591 ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) - 592 ARC_DMA_DVA(sc->sc_requests))); 593 ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)]; 594 595 bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests), 596 ccb->ccb_offset, ARC_MAX_IOCMDLEN, 597 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 598 599 arc_scsi_cmd_done(sc, ccb, reg); 600 } while (nccb != ccb); 601 602 return 0; 603} 604 605int 606arc_map_pci_resources(device_t self, struct pci_attach_args *pa) 607{ 608 struct arc_softc *sc = device_private(self); 609 pcireg_t memtype; 610 pci_intr_handle_t ih; 611 char intrbuf[PCI_INTRSTR_LEN]; 612 613 sc->sc_pc = pa->pa_pc; 614 sc->sc_tag = pa->pa_tag; 615 sc->sc_dmat = pa->pa_dmat; 616 617 memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, ARC_PCI_BAR); 618 if (pci_mapreg_map(pa, ARC_PCI_BAR, memtype, 0, &sc->sc_iot, 619 &sc->sc_ioh, NULL, &sc->sc_ios) != 0) { 620 aprint_error(": unable to map system interface register\n"); 621 return 1; 622 } 623 624 if (pci_intr_map(pa, &ih) != 0) { 625 aprint_error(": unable to map interrupt\n"); 626 goto unmap; 627 } 628 629 pci_intr_setattr(pa->pa_pc, &ih, PCI_INTR_MPSAFE, true); 630 631 sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_BIO, 632 arc_intr, sc, device_xname(self)); 633 if (sc->sc_ih == NULL) { 634 aprint_error(": unable to map interrupt [2]\n"); 635 goto unmap; 636 } 637 638 aprint_normal("\n"); 639 aprint_normal_dev(self, "interrupting at %s\n", 640 pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf))); 641 642 return 0; 643 644unmap: 645 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 646 sc->sc_ios = 0; 647 return 1; 648} 649 650void 651arc_unmap_pci_resources(struct arc_softc *sc) 652{ 653 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 654 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 655 sc->sc_ios = 0; 656} 657 658int 659arc_query_firmware(device_t self) 660{ 661 struct arc_softc *sc = device_private(self); 662 struct arc_msg_firmware_info fwinfo; 663 char string[81]; /* sizeof(vendor)*2+1 */ 664 665 if (arc_wait_eq(sc, ARC_REG_OUTB_ADDR1, ARC_REG_OUTB_ADDR1_FIRMWARE_OK, 666 ARC_REG_OUTB_ADDR1_FIRMWARE_OK) != 0) { 667 aprint_debug_dev(self, "timeout waiting for firmware ok\n"); 668 return 1; 669 } 670 671 if (arc_msg0(sc, ARC_REG_INB_MSG0_GET_CONFIG) != 0) { 672 aprint_debug_dev(self, "timeout waiting for get config\n"); 673 return 1; 674 } 675 676 if (arc_msg0(sc, ARC_REG_INB_MSG0_START_BGRB) != 0) { 677 aprint_debug_dev(self, "timeout waiting to start bg rebuild\n"); 678 return 1; 679 } 680 681 arc_read_region(sc, ARC_REG_MSGBUF, &fwinfo, sizeof(fwinfo)); 682 683 DNPRINTF(ARC_D_INIT, "%s: signature: 0x%08x\n", 684 device_xname(self), htole32(fwinfo.signature)); 685 686 if (htole32(fwinfo.signature) != ARC_FWINFO_SIGNATURE_GET_CONFIG) { 687 aprint_error_dev(self, "invalid firmware info from iop\n"); 688 return 1; 689 } 690 691 DNPRINTF(ARC_D_INIT, "%s: request_len: %d\n", 692 device_xname(self), htole32(fwinfo.request_len)); 693 DNPRINTF(ARC_D_INIT, "%s: queue_len: %d\n", 694 device_xname(self), htole32(fwinfo.queue_len)); 695 DNPRINTF(ARC_D_INIT, "%s: sdram_size: %d\n", 696 device_xname(self), htole32(fwinfo.sdram_size)); 697 DNPRINTF(ARC_D_INIT, "%s: sata_ports: %d\n", 698 device_xname(self), htole32(fwinfo.sata_ports)); 699 700 strnvisx(string, sizeof(string), fwinfo.vendor, sizeof(fwinfo.vendor), 701 VIS_TRIM|VIS_SAFE|VIS_OCTAL); 702 DNPRINTF(ARC_D_INIT, "%s: vendor: \"%s\"\n", 703 device_xname(self), string); 704 705 strnvisx(string, sizeof(string), fwinfo.model, sizeof(fwinfo.model), 706 VIS_TRIM|VIS_SAFE|VIS_OCTAL); 707 aprint_normal_dev(self, "Areca %s Host Adapter RAID controller\n", 708 string); 709 710 strnvisx(string, sizeof(string), fwinfo.fw_version, 711 sizeof(fwinfo.fw_version), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 712 DNPRINTF(ARC_D_INIT, "%s: version: \"%s\"\n", 713 device_xname(self), string); 714 715 aprint_normal_dev(self, "%d ports, %dMB SDRAM, firmware <%s>\n", 716 htole32(fwinfo.sata_ports), htole32(fwinfo.sdram_size), string); 717 718 if (htole32(fwinfo.request_len) != ARC_MAX_IOCMDLEN) { 719 aprint_error_dev(self, 720 "unexpected request frame size (%d != %d)\n", 721 htole32(fwinfo.request_len), ARC_MAX_IOCMDLEN); 722 return 1; 723 } 724 725 sc->sc_req_count = htole32(fwinfo.queue_len); 726 727 return 0; 728} 729 730#if NBIO > 0 731static int 732arc_bioctl(device_t self, u_long cmd, void *addr) 733{ 734 struct arc_softc *sc = device_private(self); 735 int error = 0; 736 737 switch (cmd) { 738 case BIOCINQ: 739 error = arc_bio_inq(sc, (struct bioc_inq *)addr); 740 break; 741 742 case BIOCVOL: 743 error = arc_bio_vol(sc, (struct bioc_vol *)addr); 744 break; 745 746 case BIOCDISK: 747 error = arc_bio_disk_volume(sc, (struct bioc_disk *)addr); 748 break; 749 750 case BIOCDISK_NOVOL: 751 error = arc_bio_disk_novol(sc, (struct bioc_disk *)addr); 752 break; 753 754 case BIOCALARM: 755 error = arc_bio_alarm(sc, (struct bioc_alarm *)addr); 756 break; 757 758 case BIOCSETSTATE: 759 error = arc_bio_setstate(sc, (struct bioc_setstate *)addr); 760 break; 761 762 case BIOCVOLOPS: 763 error = arc_bio_volops(sc, (struct bioc_volops *)addr); 764 break; 765 766 default: 767 error = ENOTTY; 768 break; 769 } 770 771 return error; 772} 773 774static int 775arc_fw_parse_status_code(struct arc_softc *sc, uint8_t *reply) 776{ 777 switch (*reply) { 778 case ARC_FW_CMD_RAIDINVAL: 779 printf("%s: firmware error (invalid raid set)\n", 780 device_xname(sc->sc_dev)); 781 return EINVAL; 782 case ARC_FW_CMD_VOLINVAL: 783 printf("%s: firmware error (invalid volume set)\n", 784 device_xname(sc->sc_dev)); 785 return EINVAL; 786 case ARC_FW_CMD_NORAID: 787 printf("%s: firmware error (unexistent raid set)\n", 788 device_xname(sc->sc_dev)); 789 return ENODEV; 790 case ARC_FW_CMD_NOVOLUME: 791 printf("%s: firmware error (unexistent volume set)\n", 792 device_xname(sc->sc_dev)); 793 return ENODEV; 794 case ARC_FW_CMD_NOPHYSDRV: 795 printf("%s: firmware error (unexistent physical drive)\n", 796 device_xname(sc->sc_dev)); 797 return ENODEV; 798 case ARC_FW_CMD_PARAM_ERR: 799 printf("%s: firmware error (parameter error)\n", 800 device_xname(sc->sc_dev)); 801 return EINVAL; 802 case ARC_FW_CMD_UNSUPPORTED: 803 printf("%s: firmware error (unsupported command)\n", 804 device_xname(sc->sc_dev)); 805 return EOPNOTSUPP; 806 case ARC_FW_CMD_DISKCFG_CHGD: 807 printf("%s: firmware error (disk configuration changed)\n", 808 device_xname(sc->sc_dev)); 809 return EINVAL; 810 case ARC_FW_CMD_PASS_INVAL: 811 printf("%s: firmware error (invalid password)\n", 812 device_xname(sc->sc_dev)); 813 return EINVAL; 814 case ARC_FW_CMD_NODISKSPACE: 815 printf("%s: firmware error (no disk space available)\n", 816 device_xname(sc->sc_dev)); 817 return EOPNOTSUPP; 818 case ARC_FW_CMD_CHECKSUM_ERR: 819 printf("%s: firmware error (checksum error)\n", 820 device_xname(sc->sc_dev)); 821 return EINVAL; 822 case ARC_FW_CMD_PASS_REQD: 823 printf("%s: firmware error (password required)\n", 824 device_xname(sc->sc_dev)); 825 return EPERM; 826 case ARC_FW_CMD_OK: 827 default: 828 return 0; 829 } 830} 831 832static int 833arc_bio_alarm(struct arc_softc *sc, struct bioc_alarm *ba) 834{ 835 uint8_t request[2], reply[1]; 836 size_t len; 837 int error = 0; 838 839 switch (ba->ba_opcode) { 840 case BIOC_SAENABLE: 841 case BIOC_SADISABLE: 842 request[0] = ARC_FW_SET_ALARM; 843 request[1] = (ba->ba_opcode == BIOC_SAENABLE) ? 844 ARC_FW_SET_ALARM_ENABLE : ARC_FW_SET_ALARM_DISABLE; 845 len = sizeof(request); 846 847 break; 848 849 case BIOC_SASILENCE: 850 request[0] = ARC_FW_MUTE_ALARM; 851 len = 1; 852 853 break; 854 855 case BIOC_GASTATUS: 856 /* system info is too big/ugly to deal with here */ 857 return arc_bio_alarm_state(sc, ba); 858 859 default: 860 return EOPNOTSUPP; 861 } 862 863 error = arc_msgbuf(sc, request, len, reply, sizeof(reply)); 864 if (error != 0) 865 return error; 866 867 return arc_fw_parse_status_code(sc, &reply[0]); 868} 869 870static int 871arc_bio_alarm_state(struct arc_softc *sc, struct bioc_alarm *ba) 872{ 873 struct arc_fw_sysinfo *sysinfo; 874 uint8_t request; 875 int error = 0; 876 877 sysinfo = kmem_zalloc(sizeof(*sysinfo), KM_SLEEP); 878 879 request = ARC_FW_SYSINFO; 880 error = arc_msgbuf(sc, &request, sizeof(request), 881 sysinfo, sizeof(struct arc_fw_sysinfo)); 882 883 if (error != 0) 884 goto out; 885 886 ba->ba_status = sysinfo->alarm; 887 888out: 889 kmem_free(sysinfo, sizeof(*sysinfo)); 890 return error; 891} 892 893static int 894arc_bio_volops(struct arc_softc *sc, struct bioc_volops *bc) 895{ 896 /* to create a raid set */ 897 struct req_craidset { 898 uint8_t cmdcode; 899 uint32_t devmask; 900 uint8_t raidset_name[16]; 901 } __packed; 902 903 /* to create a volume set */ 904 struct req_cvolset { 905 uint8_t cmdcode; 906 uint8_t raidset; 907 uint8_t volset_name[16]; 908 uint64_t capacity; 909 uint8_t raidlevel; 910 uint8_t stripe; 911 uint8_t scsi_chan; 912 uint8_t scsi_target; 913 uint8_t scsi_lun; 914 uint8_t tagqueue; 915 uint8_t cache; 916 uint8_t speed; 917 uint8_t quick_init; 918 } __packed; 919 920 struct scsibus_softc *scsibus_sc = NULL; 921 struct req_craidset req_craidset; 922 struct req_cvolset req_cvolset; 923 uint8_t request[2]; 924 uint8_t reply[1]; 925 int error = 0; 926 927 switch (bc->bc_opcode) { 928 case BIOC_VCREATE_VOLUME: 929 { 930 /* 931 * Zero out the structs so that we use some defaults 932 * in raid and volume sets. 933 */ 934 memset(&req_craidset, 0, sizeof(req_craidset)); 935 memset(&req_cvolset, 0, sizeof(req_cvolset)); 936 937 /* 938 * Firstly we have to create the raid set and 939 * use the default name for all them. 940 */ 941 req_craidset.cmdcode = ARC_FW_CREATE_RAIDSET; 942 req_craidset.devmask = bc->bc_devmask; 943 error = arc_msgbuf(sc, &req_craidset, sizeof(req_craidset), 944 reply, sizeof(reply)); 945 if (error != 0) 946 return error; 947 948 error = arc_fw_parse_status_code(sc, &reply[0]); 949 if (error) { 950 printf("%s: create raidset%d failed\n", 951 device_xname(sc->sc_dev), bc->bc_volid); 952 return error; 953 } 954 955 /* 956 * At this point the raid set was created, so it's 957 * time to create the volume set. 958 */ 959 req_cvolset.cmdcode = ARC_FW_CREATE_VOLUME; 960 req_cvolset.raidset = bc->bc_volid; 961 req_cvolset.capacity = bc->bc_size * ARC_BLOCKSIZE; 962 963 /* 964 * Set the RAID level. 965 */ 966 switch (bc->bc_level) { 967 case 0: 968 case 1: 969 req_cvolset.raidlevel = bc->bc_level; 970 break; 971 case BIOC_SVOL_RAID10: 972 req_cvolset.raidlevel = 1; 973 break; 974 case 3: 975 req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_3; 976 break; 977 case 5: 978 req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_5; 979 break; 980 case 6: 981 req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_6; 982 break; 983 default: 984 return EOPNOTSUPP; 985 } 986 987 /* 988 * Set the stripe size. 989 */ 990 switch (bc->bc_stripe) { 991 case 4: 992 req_cvolset.stripe = 0; 993 break; 994 case 8: 995 req_cvolset.stripe = 1; 996 break; 997 case 16: 998 req_cvolset.stripe = 2; 999 break; 1000 case 32: 1001 req_cvolset.stripe = 3; 1002 break; 1003 case 64: 1004 req_cvolset.stripe = 4; 1005 break; 1006 case 128: 1007 req_cvolset.stripe = 5; 1008 break; 1009 default: 1010 req_cvolset.stripe = 4; /* by default 64K */ 1011 break; 1012 } 1013 1014 req_cvolset.scsi_chan = bc->bc_channel; 1015 req_cvolset.scsi_target = bc->bc_target; 1016 req_cvolset.scsi_lun = bc->bc_lun; 1017 req_cvolset.tagqueue = 1; /* always enabled */ 1018 req_cvolset.cache = 1; /* always enabled */ 1019 req_cvolset.speed = 4; /* always max speed */ 1020 1021 /* RAID 1 and 1+0 levels need foreground initialization */ 1022 if (bc->bc_level == 1 || bc->bc_level == BIOC_SVOL_RAID10) 1023 req_cvolset.quick_init = 1; /* foreground init */ 1024 1025 error = arc_msgbuf(sc, &req_cvolset, sizeof(req_cvolset), 1026 reply, sizeof(reply)); 1027 if (error != 0) 1028 return error; 1029 1030 error = arc_fw_parse_status_code(sc, &reply[0]); 1031 if (error) { 1032 printf("%s: create volumeset%d failed\n", 1033 device_xname(sc->sc_dev), bc->bc_volid); 1034 return error; 1035 } 1036 1037 /* 1038 * If we are creating a RAID 1 or RAID 1+0 volume, 1039 * the volume will be created immediately but it won't 1040 * be available until the initialization is done... so 1041 * don't bother attaching the sd(4) device. 1042 */ 1043 if (bc->bc_level == 1 || bc->bc_level == BIOC_SVOL_RAID10) 1044 break; 1045 1046 /* 1047 * Do a rescan on the bus to attach the device associated 1048 * with the new volume. 1049 */ 1050 scsibus_sc = device_private(sc->sc_scsibus_dv); 1051 (void)scsi_probe_bus(scsibus_sc, bc->bc_target, bc->bc_lun); 1052 1053 break; 1054 } 1055 case BIOC_VREMOVE_VOLUME: 1056 { 1057 /* 1058 * Remove the volume set specified in bc_volid. 1059 */ 1060 request[0] = ARC_FW_DELETE_VOLUME; 1061 request[1] = bc->bc_volid; 1062 error = arc_msgbuf(sc, request, sizeof(request), 1063 reply, sizeof(reply)); 1064 if (error != 0) 1065 return error; 1066 1067 error = arc_fw_parse_status_code(sc, &reply[0]); 1068 if (error) { 1069 printf("%s: delete volumeset%d failed\n", 1070 device_xname(sc->sc_dev), bc->bc_volid); 1071 return error; 1072 } 1073 1074 /* 1075 * Detach the sd(4) device associated with the volume, 1076 * but if there's an error don't make it a priority. 1077 */ 1078 error = scsipi_target_detach(&sc->sc_chan, bc->bc_target, 1079 bc->bc_lun, 0); 1080 if (error) 1081 printf("%s: couldn't detach sd device for volume %d " 1082 "at %u:%u.%u (error=%d)\n", 1083 device_xname(sc->sc_dev), bc->bc_volid, 1084 bc->bc_channel, bc->bc_target, bc->bc_lun, error); 1085 1086 /* 1087 * and remove the raid set specified in bc_volid, 1088 * we only care about volumes. 1089 */ 1090 request[0] = ARC_FW_DELETE_RAIDSET; 1091 request[1] = bc->bc_volid; 1092 error = arc_msgbuf(sc, request, sizeof(request), 1093 reply, sizeof(reply)); 1094 if (error != 0) 1095 return error; 1096 1097 error = arc_fw_parse_status_code(sc, &reply[0]); 1098 if (error) { 1099 printf("%s: delete raidset%d failed\n", 1100 device_xname(sc->sc_dev), bc->bc_volid); 1101 return error; 1102 } 1103 1104 break; 1105 } 1106 default: 1107 return EOPNOTSUPP; 1108 } 1109 1110 return error; 1111} 1112 1113static int 1114arc_bio_setstate(struct arc_softc *sc, struct bioc_setstate *bs) 1115{ 1116 /* for a hotspare disk */ 1117 struct request_hs { 1118 uint8_t cmdcode; 1119 uint32_t devmask; 1120 } __packed; 1121 1122 /* for a pass-through disk */ 1123 struct request_pt { 1124 uint8_t cmdcode; 1125 uint8_t devid; 1126 uint8_t scsi_chan; 1127 uint8_t scsi_id; 1128 uint8_t scsi_lun; 1129 uint8_t tagged_queue; 1130 uint8_t cache_mode; 1131 uint8_t max_speed; 1132 } __packed; 1133 1134 struct scsibus_softc *scsibus_sc = NULL; 1135 struct request_hs req_hs; /* to add/remove hotspare */ 1136 struct request_pt req_pt; /* to add a pass-through */ 1137 uint8_t req_gen[2]; 1138 uint8_t reply[1]; 1139 int error = 0; 1140 1141 switch (bs->bs_status) { 1142 case BIOC_SSHOTSPARE: 1143 { 1144 req_hs.cmdcode = ARC_FW_CREATE_HOTSPARE; 1145 req_hs.devmask = (1 << bs->bs_target); 1146 goto hotspare; 1147 } 1148 case BIOC_SSDELHOTSPARE: 1149 { 1150 req_hs.cmdcode = ARC_FW_DELETE_HOTSPARE; 1151 req_hs.devmask = (1 << bs->bs_target); 1152 goto hotspare; 1153 } 1154 case BIOC_SSPASSTHRU: 1155 { 1156 req_pt.cmdcode = ARC_FW_CREATE_PASSTHRU; 1157 req_pt.devid = bs->bs_other_id; /* this wants device# */ 1158 req_pt.scsi_chan = bs->bs_channel; 1159 req_pt.scsi_id = bs->bs_target; 1160 req_pt.scsi_lun = bs->bs_lun; 1161 req_pt.tagged_queue = 1; /* always enabled */ 1162 req_pt.cache_mode = 1; /* always enabled */ 1163 req_pt.max_speed = 4; /* always max speed */ 1164 1165 error = arc_msgbuf(sc, &req_pt, sizeof(req_pt), 1166 reply, sizeof(reply)); 1167 if (error != 0) 1168 return error; 1169 1170 /* 1171 * Do a rescan on the bus to attach the new device 1172 * associated with the pass-through disk. 1173 */ 1174 scsibus_sc = device_private(sc->sc_scsibus_dv); 1175 (void)scsi_probe_bus(scsibus_sc, bs->bs_target, bs->bs_lun); 1176 1177 goto out; 1178 } 1179 case BIOC_SSDELPASSTHRU: 1180 { 1181 req_gen[0] = ARC_FW_DELETE_PASSTHRU; 1182 req_gen[1] = bs->bs_target; 1183 error = arc_msgbuf(sc, &req_gen, sizeof(req_gen), 1184 reply, sizeof(reply)); 1185 if (error != 0) 1186 return error; 1187 1188 /* 1189 * Detach the sd device associated with this pass-through disk. 1190 */ 1191 error = scsipi_target_detach(&sc->sc_chan, bs->bs_target, 1192 bs->bs_lun, 0); 1193 if (error) 1194 printf("%s: couldn't detach sd device for the " 1195 "pass-through disk at %u:%u.%u (error=%d)\n", 1196 device_xname(sc->sc_dev), 1197 bs->bs_channel, bs->bs_target, bs->bs_lun, error); 1198 1199 goto out; 1200 } 1201 case BIOC_SSCHECKSTART_VOL: 1202 { 1203 req_gen[0] = ARC_FW_START_CHECKVOL; 1204 req_gen[1] = bs->bs_volid; 1205 error = arc_msgbuf(sc, &req_gen, sizeof(req_gen), 1206 reply, sizeof(reply)); 1207 if (error != 0) 1208 return error; 1209 1210 goto out; 1211 } 1212 case BIOC_SSCHECKSTOP_VOL: 1213 { 1214 uint8_t req = ARC_FW_STOP_CHECKVOL; 1215 error = arc_msgbuf(sc, &req, 1, reply, sizeof(reply)); 1216 if (error != 0) 1217 return error; 1218 1219 goto out; 1220 } 1221 default: 1222 return EOPNOTSUPP; 1223 } 1224 1225hotspare: 1226 error = arc_msgbuf(sc, &req_hs, sizeof(req_hs), 1227 reply, sizeof(reply)); 1228 if (error != 0) 1229 return error; 1230 1231out: 1232 return arc_fw_parse_status_code(sc, &reply[0]); 1233} 1234 1235static int 1236arc_bio_inq(struct arc_softc *sc, struct bioc_inq *bi) 1237{ 1238 uint8_t request[2]; 1239 struct arc_fw_sysinfo *sysinfo = NULL; 1240 struct arc_fw_raidinfo *raidinfo; 1241 int nvols = 0, i; 1242 int error = 0; 1243 1244 raidinfo = kmem_zalloc(sizeof(*raidinfo), KM_SLEEP); 1245 1246 if (!sc->sc_maxraidset || !sc->sc_maxvolset || !sc->sc_cchans) { 1247 sysinfo = kmem_zalloc(sizeof(*sysinfo), KM_SLEEP); 1248 1249 request[0] = ARC_FW_SYSINFO; 1250 error = arc_msgbuf(sc, request, 1, sysinfo, 1251 sizeof(struct arc_fw_sysinfo)); 1252 if (error != 0) 1253 goto out; 1254 1255 sc->sc_maxraidset = sysinfo->max_raid_set; 1256 sc->sc_maxvolset = sysinfo->max_volume_set; 1257 sc->sc_cchans = sysinfo->ide_channels; 1258 } 1259 1260 request[0] = ARC_FW_RAIDINFO; 1261 for (i = 0; i < sc->sc_maxraidset; i++) { 1262 request[1] = i; 1263 error = arc_msgbuf(sc, request, sizeof(request), raidinfo, 1264 sizeof(struct arc_fw_raidinfo)); 1265 if (error != 0) 1266 goto out; 1267 1268 nvols += raidinfo->volumes; 1269 } 1270 1271 strlcpy(bi->bi_dev, device_xname(sc->sc_dev), sizeof(bi->bi_dev)); 1272 bi->bi_novol = nvols; 1273 bi->bi_nodisk = sc->sc_cchans; 1274 1275out: 1276 if (sysinfo) 1277 kmem_free(sysinfo, sizeof(*sysinfo)); 1278 kmem_free(raidinfo, sizeof(*raidinfo)); 1279 return error; 1280} 1281 1282static int 1283arc_bio_getvol(struct arc_softc *sc, int vol, struct arc_fw_volinfo *volinfo) 1284{ 1285 uint8_t request[2]; 1286 int error = 0; 1287 int nvols = 0, i; 1288 1289 request[0] = ARC_FW_VOLINFO; 1290 for (i = 0; i < sc->sc_maxvolset; i++) { 1291 request[1] = i; 1292 error = arc_msgbuf(sc, request, sizeof(request), volinfo, 1293 sizeof(struct arc_fw_volinfo)); 1294 if (error != 0) 1295 goto out; 1296 1297 if (volinfo->capacity == 0 && volinfo->capacity2 == 0) 1298 continue; 1299 1300 if (nvols == vol) 1301 break; 1302 1303 nvols++; 1304 } 1305 1306 if (nvols != vol || 1307 (volinfo->capacity == 0 && volinfo->capacity2 == 0)) { 1308 error = ENODEV; 1309 goto out; 1310 } 1311 1312out: 1313 return error; 1314} 1315 1316static int 1317arc_bio_vol(struct arc_softc *sc, struct bioc_vol *bv) 1318{ 1319 struct arc_fw_volinfo *volinfo; 1320 uint64_t blocks; 1321 uint32_t status; 1322 int error = 0; 1323 1324 volinfo = kmem_zalloc(sizeof(*volinfo), KM_SLEEP); 1325 1326 error = arc_bio_getvol(sc, bv->bv_volid, volinfo); 1327 if (error != 0) 1328 goto out; 1329 1330 bv->bv_percent = -1; 1331 bv->bv_seconds = 0; 1332 1333 status = htole32(volinfo->volume_status); 1334 if (status == 0x0) { 1335 if (htole32(volinfo->fail_mask) == 0x0) 1336 bv->bv_status = BIOC_SVONLINE; 1337 else 1338 bv->bv_status = BIOC_SVDEGRADED; 1339 } else if (status & ARC_FW_VOL_STATUS_NEED_REGEN) { 1340 bv->bv_status = BIOC_SVDEGRADED; 1341 } else if (status & ARC_FW_VOL_STATUS_FAILED) { 1342 bv->bv_status = BIOC_SVOFFLINE; 1343 } else if (status & ARC_FW_VOL_STATUS_INITTING) { 1344 bv->bv_status = BIOC_SVBUILDING; 1345 bv->bv_percent = htole32(volinfo->progress); 1346 } else if (status & ARC_FW_VOL_STATUS_REBUILDING) { 1347 bv->bv_status = BIOC_SVREBUILD; 1348 bv->bv_percent = htole32(volinfo->progress); 1349 } else if (status & ARC_FW_VOL_STATUS_MIGRATING) { 1350 bv->bv_status = BIOC_SVMIGRATING; 1351 bv->bv_percent = htole32(volinfo->progress); 1352 } else if (status & ARC_FW_VOL_STATUS_CHECKING) { 1353 bv->bv_status = BIOC_SVCHECKING; 1354 bv->bv_percent = htole32(volinfo->progress); 1355 } else if (status & ARC_FW_VOL_STATUS_NEED_INIT) { 1356 bv->bv_status = BIOC_SVOFFLINE; 1357 } else { 1358 printf("%s: volume %d status 0x%x\n", 1359 device_xname(sc->sc_dev), bv->bv_volid, status); 1360 } 1361 1362 blocks = (uint64_t)htole32(volinfo->capacity2) << 32; 1363 blocks += (uint64_t)htole32(volinfo->capacity); 1364 bv->bv_size = blocks * ARC_BLOCKSIZE; /* XXX */ 1365 1366 switch (volinfo->raid_level) { 1367 case ARC_FW_VOL_RAIDLEVEL_0: 1368 bv->bv_level = 0; 1369 break; 1370 case ARC_FW_VOL_RAIDLEVEL_1: 1371 if (volinfo->member_disks > 2) 1372 bv->bv_level = BIOC_SVOL_RAID10; 1373 else 1374 bv->bv_level = 1; 1375 break; 1376 case ARC_FW_VOL_RAIDLEVEL_3: 1377 bv->bv_level = 3; 1378 break; 1379 case ARC_FW_VOL_RAIDLEVEL_5: 1380 bv->bv_level = 5; 1381 break; 1382 case ARC_FW_VOL_RAIDLEVEL_6: 1383 bv->bv_level = 6; 1384 break; 1385 case ARC_FW_VOL_RAIDLEVEL_PASSTHRU: 1386 bv->bv_level = BIOC_SVOL_PASSTHRU; 1387 break; 1388 default: 1389 bv->bv_level = -1; 1390 break; 1391 } 1392 1393 bv->bv_nodisk = volinfo->member_disks; 1394 bv->bv_stripe_size = volinfo->stripe_size / 2; 1395 snprintf(bv->bv_dev, sizeof(bv->bv_dev), "sd%d", bv->bv_volid); 1396 strnvisx(bv->bv_vendor, sizeof(bv->bv_vendor), volinfo->set_name, 1397 sizeof(volinfo->set_name), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 1398 1399out: 1400 kmem_free(volinfo, sizeof(*volinfo)); 1401 return error; 1402} 1403 1404static int 1405arc_bio_disk_novol(struct arc_softc *sc, struct bioc_disk *bd) 1406{ 1407 struct arc_fw_diskinfo *diskinfo; 1408 uint8_t request[2]; 1409 int error = 0; 1410 1411 diskinfo = kmem_zalloc(sizeof(*diskinfo), KM_SLEEP); 1412 1413 if (bd->bd_diskid >= sc->sc_cchans) { 1414 error = ENODEV; 1415 goto out; 1416 } 1417 1418 request[0] = ARC_FW_DISKINFO; 1419 request[1] = bd->bd_diskid; 1420 error = arc_msgbuf(sc, request, sizeof(request), 1421 diskinfo, sizeof(struct arc_fw_diskinfo)); 1422 if (error != 0) 1423 goto out; 1424 1425 /* skip disks with no capacity */ 1426 if (htole32(diskinfo->capacity) == 0 && 1427 htole32(diskinfo->capacity2) == 0) 1428 goto out; 1429 1430 bd->bd_disknovol = true; 1431 arc_bio_disk_filldata(sc, bd, diskinfo, bd->bd_diskid); 1432 1433out: 1434 kmem_free(diskinfo, sizeof(*diskinfo)); 1435 return error; 1436} 1437 1438static void 1439arc_bio_disk_filldata(struct arc_softc *sc, struct bioc_disk *bd, 1440 struct arc_fw_diskinfo *diskinfo, int diskid) 1441{ 1442 uint64_t blocks; 1443 char model[81]; 1444 char serial[41]; 1445 char rev[17]; 1446 1447 /* Ignore bit zero for now, we don't know what it means */ 1448 diskinfo->device_state &= ~0x1; 1449 1450 switch (diskinfo->device_state) { 1451 case ARC_FW_DISK_FAILED: 1452 bd->bd_status = BIOC_SDFAILED; 1453 break; 1454 case ARC_FW_DISK_PASSTHRU: 1455 bd->bd_status = BIOC_SDPASSTHRU; 1456 break; 1457 case ARC_FW_DISK_NORMAL: 1458 bd->bd_status = BIOC_SDONLINE; 1459 break; 1460 case ARC_FW_DISK_HOTSPARE: 1461 bd->bd_status = BIOC_SDHOTSPARE; 1462 break; 1463 case ARC_FW_DISK_UNUSED: 1464 bd->bd_status = BIOC_SDUNUSED; 1465 break; 1466 case 0: 1467 /* disk has been disconnected */ 1468 bd->bd_status = BIOC_SDOFFLINE; 1469 bd->bd_channel = 1; 1470 bd->bd_target = 0; 1471 bd->bd_lun = 0; 1472 strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor)); 1473 break; 1474 default: 1475 printf("%s: unknown disk device_state: 0x%x\n", __func__, 1476 diskinfo->device_state); 1477 bd->bd_status = BIOC_SDINVALID; 1478 return; 1479 } 1480 1481 blocks = (uint64_t)htole32(diskinfo->capacity2) << 32; 1482 blocks += (uint64_t)htole32(diskinfo->capacity); 1483 bd->bd_size = blocks * ARC_BLOCKSIZE; /* XXX */ 1484 1485 strnvisx(model, sizeof(model), diskinfo->model, 1486 sizeof(diskinfo->model), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 1487 strnvisx(serial, sizeof(serial), diskinfo->serial, 1488 sizeof(diskinfo->serial), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 1489 strnvisx(rev, sizeof(rev), diskinfo->firmware_rev, 1490 sizeof(diskinfo->firmware_rev), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 1491 1492 snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s", model, rev); 1493 strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial)); 1494 1495#if 0 1496 bd->bd_channel = diskinfo->scsi_attr.channel; 1497 bd->bd_target = diskinfo->scsi_attr.target; 1498 bd->bd_lun = diskinfo->scsi_attr.lun; 1499#endif 1500 1501 /* 1502 * the firmware doesn't seem to fill scsi_attr in, so fake it with 1503 * the diskid. 1504 */ 1505 bd->bd_channel = 0; 1506 bd->bd_target = diskid; 1507 bd->bd_lun = 0; 1508} 1509 1510static int 1511arc_bio_disk_volume(struct arc_softc *sc, struct bioc_disk *bd) 1512{ 1513 struct arc_fw_raidinfo *raidinfo; 1514 struct arc_fw_volinfo *volinfo; 1515 struct arc_fw_diskinfo *diskinfo; 1516 uint8_t request[2]; 1517 int error = 0; 1518 1519 volinfo = kmem_zalloc(sizeof(*volinfo), KM_SLEEP); 1520 raidinfo = kmem_zalloc(sizeof(*raidinfo), KM_SLEEP); 1521 diskinfo = kmem_zalloc(sizeof(*diskinfo), KM_SLEEP); 1522 1523 error = arc_bio_getvol(sc, bd->bd_volid, volinfo); 1524 if (error != 0) 1525 goto out; 1526 1527 request[0] = ARC_FW_RAIDINFO; 1528 request[1] = volinfo->raid_set_number; 1529 1530 error = arc_msgbuf(sc, request, sizeof(request), raidinfo, 1531 sizeof(struct arc_fw_raidinfo)); 1532 if (error != 0) 1533 goto out; 1534 1535 if (bd->bd_diskid >= sc->sc_cchans || 1536 bd->bd_diskid >= raidinfo->member_devices) { 1537 error = ENODEV; 1538 goto out; 1539 } 1540 1541 if (raidinfo->device_array[bd->bd_diskid] == 0xff) { 1542 /* 1543 * The disk has been disconnected, mark it offline 1544 * and put it on another bus. 1545 */ 1546 bd->bd_channel = 1; 1547 bd->bd_target = 0; 1548 bd->bd_lun = 0; 1549 bd->bd_status = BIOC_SDOFFLINE; 1550 strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor)); 1551 goto out; 1552 } 1553 1554 request[0] = ARC_FW_DISKINFO; 1555 request[1] = raidinfo->device_array[bd->bd_diskid]; 1556 error = arc_msgbuf(sc, request, sizeof(request), diskinfo, 1557 sizeof(struct arc_fw_diskinfo)); 1558 if (error != 0) 1559 goto out; 1560 1561 /* now fill our bio disk with data from the firmware */ 1562 arc_bio_disk_filldata(sc, bd, diskinfo, 1563 raidinfo->device_array[bd->bd_diskid]); 1564 1565out: 1566 kmem_free(raidinfo, sizeof(*raidinfo)); 1567 kmem_free(volinfo, sizeof(*volinfo)); 1568 kmem_free(diskinfo, sizeof(*diskinfo)); 1569 return error; 1570} 1571 1572static uint8_t 1573arc_msg_cksum(void *cmd, uint16_t len) 1574{ 1575 uint8_t *buf = cmd; 1576 uint8_t cksum; 1577 int i; 1578 1579 cksum = (uint8_t)(len >> 8) + (uint8_t)len; 1580 for (i = 0; i < len; i++) 1581 cksum += buf[i]; 1582 1583 return cksum; 1584} 1585 1586 1587static int 1588arc_msgbuf(struct arc_softc *sc, void *wptr, size_t wbuflen, void *rptr, 1589 size_t rbuflen) 1590{ 1591 uint8_t rwbuf[ARC_REG_IOC_RWBUF_MAXLEN]; 1592 uint8_t *wbuf, *rbuf; 1593 int wlen, wdone = 0, rlen, rdone = 0; 1594 struct arc_fw_bufhdr *bufhdr; 1595 uint32_t reg, rwlen; 1596 int error = 0; 1597#ifdef ARC_DEBUG 1598 int i; 1599#endif 1600 1601 wbuf = rbuf = NULL; 1602 1603 DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n", 1604 device_xname(sc->sc_dev), wbuflen, rbuflen); 1605 1606 wlen = sizeof(struct arc_fw_bufhdr) + wbuflen + 1; /* 1 for cksum */ 1607 wbuf = kmem_alloc(wlen, KM_SLEEP); 1608 1609 rlen = sizeof(struct arc_fw_bufhdr) + rbuflen + 1; /* 1 for cksum */ 1610 rbuf = kmem_alloc(rlen, KM_SLEEP); 1611 1612 DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wlen: %d rlen: %d\n", 1613 device_xname(sc->sc_dev), wlen, rlen); 1614 1615 bufhdr = (struct arc_fw_bufhdr *)wbuf; 1616 bufhdr->hdr = arc_fw_hdr; 1617 bufhdr->len = htole16(wbuflen); 1618 memcpy(wbuf + sizeof(struct arc_fw_bufhdr), wptr, wbuflen); 1619 wbuf[wlen - 1] = arc_msg_cksum(wptr, wbuflen); 1620 1621 arc_lock(sc); 1622 if (arc_read(sc, ARC_REG_OUTB_DOORBELL) != 0) { 1623 error = EBUSY; 1624 goto out; 1625 } 1626 1627 reg = ARC_REG_OUTB_DOORBELL_READ_OK; 1628 1629 do { 1630 if ((reg & ARC_REG_OUTB_DOORBELL_READ_OK) && wdone < wlen) { 1631 memset(rwbuf, 0, sizeof(rwbuf)); 1632 rwlen = (wlen - wdone) % sizeof(rwbuf); 1633 memcpy(rwbuf, &wbuf[wdone], rwlen); 1634 1635#ifdef ARC_DEBUG 1636 if (arcdebug & ARC_D_DB) { 1637 printf("%s: write %d:", 1638 device_xname(sc->sc_dev), rwlen); 1639 for (i = 0; i < rwlen; i++) 1640 printf(" 0x%02x", rwbuf[i]); 1641 printf("\n"); 1642 } 1643#endif 1644 1645 /* copy the chunk to the hw */ 1646 arc_write(sc, ARC_REG_IOC_WBUF_LEN, rwlen); 1647 arc_write_region(sc, ARC_REG_IOC_WBUF, rwbuf, 1648 sizeof(rwbuf)); 1649 1650 /* say we have a buffer for the hw */ 1651 arc_write(sc, ARC_REG_INB_DOORBELL, 1652 ARC_REG_INB_DOORBELL_WRITE_OK); 1653 1654 wdone += rwlen; 1655 } 1656 1657 while ((reg = arc_read(sc, ARC_REG_OUTB_DOORBELL)) == 0) 1658 arc_wait(sc); 1659 1660 arc_write(sc, ARC_REG_OUTB_DOORBELL, reg); 1661 1662 DNPRINTF(ARC_D_DB, "%s: reg: 0x%08x\n", 1663 device_xname(sc->sc_dev), reg); 1664 1665 if ((reg & ARC_REG_OUTB_DOORBELL_WRITE_OK) && rdone < rlen) { 1666 rwlen = arc_read(sc, ARC_REG_IOC_RBUF_LEN); 1667 if (rwlen > sizeof(rwbuf)) { 1668 DNPRINTF(ARC_D_DB, "%s: rwlen too big\n", 1669 device_xname(sc->sc_dev)); 1670 error = EIO; 1671 goto out; 1672 } 1673 1674 arc_read_region(sc, ARC_REG_IOC_RBUF, rwbuf, 1675 sizeof(rwbuf)); 1676 1677 arc_write(sc, ARC_REG_INB_DOORBELL, 1678 ARC_REG_INB_DOORBELL_READ_OK); 1679 1680#ifdef ARC_DEBUG 1681 printf("%s: len: %d+%d=%d/%d\n", 1682 device_xname(sc->sc_dev), 1683 rwlen, rdone, rwlen + rdone, rlen); 1684 if (arcdebug & ARC_D_DB) { 1685 printf("%s: read:", 1686 device_xname(sc->sc_dev)); 1687 for (i = 0; i < rwlen; i++) 1688 printf(" 0x%02x", rwbuf[i]); 1689 printf("\n"); 1690 } 1691#endif 1692 1693 if ((rdone + rwlen) > rlen) { 1694 DNPRINTF(ARC_D_DB, "%s: rwbuf too big\n", 1695 device_xname(sc->sc_dev)); 1696 error = EIO; 1697 goto out; 1698 } 1699 1700 memcpy(&rbuf[rdone], rwbuf, rwlen); 1701 rdone += rwlen; 1702 } 1703 } while (rdone != rlen); 1704 1705 bufhdr = (struct arc_fw_bufhdr *)rbuf; 1706 if (memcmp(&bufhdr->hdr, &arc_fw_hdr, sizeof(bufhdr->hdr)) != 0 || 1707 bufhdr->len != htole16(rbuflen)) { 1708 DNPRINTF(ARC_D_DB, "%s: rbuf hdr is wrong\n", 1709 device_xname(sc->sc_dev)); 1710 error = EIO; 1711 goto out; 1712 } 1713 1714 memcpy(rptr, rbuf + sizeof(struct arc_fw_bufhdr), rbuflen); 1715 1716 if (rbuf[rlen - 1] != arc_msg_cksum(rptr, rbuflen)) { 1717 DNPRINTF(ARC_D_DB, "%s: invalid cksum\n", 1718 device_xname(sc->sc_dev)); 1719 error = EIO; 1720 goto out; 1721 } 1722 1723out: 1724 arc_unlock(sc); 1725 kmem_free(wbuf, wlen); 1726 kmem_free(rbuf, rlen); 1727 1728 return error; 1729} 1730 1731static void 1732arc_lock(struct arc_softc *sc) 1733{ 1734 rw_enter(&sc->sc_rwlock, RW_WRITER); 1735 mutex_spin_enter(&sc->sc_mutex); 1736 arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE); 1737 sc->sc_talking = 1; 1738} 1739 1740static void 1741arc_unlock(struct arc_softc *sc) 1742{ 1743 KASSERT(mutex_owned(&sc->sc_mutex)); 1744 1745 arc_write(sc, ARC_REG_INTRMASK, 1746 ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL)); 1747 sc->sc_talking = 0; 1748 mutex_spin_exit(&sc->sc_mutex); 1749 rw_exit(&sc->sc_rwlock); 1750} 1751 1752static void 1753arc_wait(struct arc_softc *sc) 1754{ 1755 KASSERT(mutex_owned(&sc->sc_mutex)); 1756 1757 arc_write(sc, ARC_REG_INTRMASK, 1758 ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL)); 1759 if (cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz) == EWOULDBLOCK) 1760 arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE); 1761} 1762 1763 1764static void 1765arc_create_sensors(void *arg) 1766{ 1767 struct arc_softc *sc = arg; 1768 struct bioc_inq bi; 1769 struct bioc_vol bv; 1770 int i, j; 1771 size_t slen, count = 0; 1772 1773 memset(&bi, 0, sizeof(bi)); 1774 if (arc_bio_inq(sc, &bi) != 0) { 1775 aprint_error("%s: unable to query firmware for sensor info\n", 1776 device_xname(sc->sc_dev)); 1777 kthread_exit(0); 1778 } 1779 1780 /* There's no point to continue if there are no volumes */ 1781 if (!bi.bi_novol) 1782 kthread_exit(0); 1783 1784 for (i = 0; i < bi.bi_novol; i++) { 1785 memset(&bv, 0, sizeof(bv)); 1786 bv.bv_volid = i; 1787 if (arc_bio_vol(sc, &bv) != 0) 1788 kthread_exit(0); 1789 1790 /* Skip passthrough volumes */ 1791 if (bv.bv_level == BIOC_SVOL_PASSTHRU) 1792 continue; 1793 1794 /* new volume found */ 1795 sc->sc_nsensors++; 1796 /* new disk in a volume found */ 1797 sc->sc_nsensors+= bv.bv_nodisk; 1798 } 1799 1800 /* No valid volumes */ 1801 if (!sc->sc_nsensors) 1802 kthread_exit(0); 1803 1804 sc->sc_sme = sysmon_envsys_create(); 1805 slen = sizeof(arc_edata_t) * sc->sc_nsensors; 1806 sc->sc_arc_sensors = kmem_zalloc(slen, KM_SLEEP); 1807 1808 /* Attach sensors for volumes and disks */ 1809 for (i = 0; i < bi.bi_novol; i++) { 1810 memset(&bv, 0, sizeof(bv)); 1811 bv.bv_volid = i; 1812 if (arc_bio_vol(sc, &bv) != 0) 1813 goto bad; 1814 1815 sc->sc_arc_sensors[count].arc_sensor.units = ENVSYS_DRIVE; 1816 sc->sc_arc_sensors[count].arc_sensor.state = ENVSYS_SINVALID; 1817 sc->sc_arc_sensors[count].arc_sensor.value_cur = 1818 ENVSYS_DRIVE_EMPTY; 1819 sc->sc_arc_sensors[count].arc_sensor.flags = 1820 ENVSYS_FMONSTCHANGED; 1821 1822 /* Skip passthrough volumes */ 1823 if (bv.bv_level == BIOC_SVOL_PASSTHRU) 1824 continue; 1825 1826 if (bv.bv_level == BIOC_SVOL_RAID10) 1827 snprintf(sc->sc_arc_sensors[count].arc_sensor.desc, 1828 sizeof(sc->sc_arc_sensors[count].arc_sensor.desc), 1829 "RAID 1+0 volume%d (%s)", i, bv.bv_dev); 1830 else 1831 snprintf(sc->sc_arc_sensors[count].arc_sensor.desc, 1832 sizeof(sc->sc_arc_sensors[count].arc_sensor.desc), 1833 "RAID %d volume%d (%s)", bv.bv_level, i, 1834 bv.bv_dev); 1835 1836 sc->sc_arc_sensors[count].arc_volid = i; 1837 1838 if (sysmon_envsys_sensor_attach(sc->sc_sme, 1839 &sc->sc_arc_sensors[count].arc_sensor)) 1840 goto bad; 1841 1842 count++; 1843 1844 /* Attach disk sensors for this volume */ 1845 for (j = 0; j < bv.bv_nodisk; j++) { 1846 sc->sc_arc_sensors[count].arc_sensor.state = 1847 ENVSYS_SINVALID; 1848 sc->sc_arc_sensors[count].arc_sensor.units = 1849 ENVSYS_DRIVE; 1850 sc->sc_arc_sensors[count].arc_sensor.value_cur = 1851 ENVSYS_DRIVE_EMPTY; 1852 sc->sc_arc_sensors[count].arc_sensor.flags = 1853 ENVSYS_FMONSTCHANGED; 1854 1855 snprintf(sc->sc_arc_sensors[count].arc_sensor.desc, 1856 sizeof(sc->sc_arc_sensors[count].arc_sensor.desc), 1857 "disk%d volume%d (%s)", j, i, bv.bv_dev); 1858 sc->sc_arc_sensors[count].arc_volid = i; 1859 sc->sc_arc_sensors[count].arc_diskid = j + 10; 1860 1861 if (sysmon_envsys_sensor_attach(sc->sc_sme, 1862 &sc->sc_arc_sensors[count].arc_sensor)) 1863 goto bad; 1864 1865 count++; 1866 } 1867 } 1868 1869 /* 1870 * Register our envsys driver with the framework now that the 1871 * sensors were all attached. 1872 */ 1873 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 1874 sc->sc_sme->sme_cookie = sc; 1875 sc->sc_sme->sme_refresh = arc_refresh_sensors; 1876 1877 if (sysmon_envsys_register(sc->sc_sme)) { 1878 aprint_debug("%s: unable to register with sysmon\n", 1879 device_xname(sc->sc_dev)); 1880 goto bad; 1881 } 1882 kthread_exit(0); 1883 1884bad: 1885 sysmon_envsys_destroy(sc->sc_sme); 1886 sc->sc_sme = NULL; 1887 1888 kmem_free(sc->sc_arc_sensors, slen); 1889 sc->sc_arc_sensors = NULL; 1890 1891 kthread_exit(0); 1892} 1893 1894static void 1895arc_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata) 1896{ 1897 struct arc_softc *sc = sme->sme_cookie; 1898 struct bioc_vol bv; 1899 struct bioc_disk bd; 1900 arc_edata_t *arcdata = (arc_edata_t *)edata; 1901 1902 /* sanity check */ 1903 if (edata->units != ENVSYS_DRIVE) 1904 return; 1905 1906 memset(&bv, 0, sizeof(bv)); 1907 bv.bv_volid = arcdata->arc_volid; 1908 1909 if (arc_bio_vol(sc, &bv)) { 1910 bv.bv_status = BIOC_SVINVALID; 1911 bio_vol_to_envsys(edata, &bv); 1912 return; 1913 } 1914 1915 if (arcdata->arc_diskid) { 1916 /* Current sensor is handling a disk volume member */ 1917 memset(&bd, 0, sizeof(bd)); 1918 bd.bd_volid = arcdata->arc_volid; 1919 bd.bd_diskid = arcdata->arc_diskid - 10; 1920 1921 if (arc_bio_disk_volume(sc, &bd)) 1922 bd.bd_status = BIOC_SDOFFLINE; 1923 bio_disk_to_envsys(edata, &bd); 1924 } else { 1925 /* Current sensor is handling a volume */ 1926 bio_vol_to_envsys(edata, &bv); 1927 } 1928} 1929#endif /* NBIO > 0 */ 1930 1931static uint32_t 1932arc_read(struct arc_softc *sc, bus_size_t r) 1933{ 1934 uint32_t v; 1935 1936 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 1937 BUS_SPACE_BARRIER_READ); 1938 v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r); 1939 1940 DNPRINTF(ARC_D_RW, "%s: arc_read 0x%lx 0x%08x\n", 1941 device_xname(sc->sc_dev), r, v); 1942 1943 return v; 1944} 1945 1946static void 1947arc_read_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len) 1948{ 1949 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len, 1950 BUS_SPACE_BARRIER_READ); 1951 bus_space_read_region_4(sc->sc_iot, sc->sc_ioh, r, 1952 (uint32_t *)buf, len >> 2); 1953} 1954 1955static void 1956arc_write(struct arc_softc *sc, bus_size_t r, uint32_t v) 1957{ 1958 DNPRINTF(ARC_D_RW, "%s: arc_write 0x%lx 0x%08x\n", 1959 device_xname(sc->sc_dev), r, v); 1960 1961 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 1962 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 1963 BUS_SPACE_BARRIER_WRITE); 1964} 1965 1966#if NBIO > 0 1967static void 1968arc_write_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len) 1969{ 1970 bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, r, 1971 (const uint32_t *)buf, len >> 2); 1972 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len, 1973 BUS_SPACE_BARRIER_WRITE); 1974} 1975#endif /* NBIO > 0 */ 1976 1977static int 1978arc_wait_eq(struct arc_softc *sc, bus_size_t r, uint32_t mask, 1979 uint32_t target) 1980{ 1981 int i; 1982 1983 DNPRINTF(ARC_D_RW, "%s: arc_wait_eq 0x%lx 0x%08x 0x%08x\n", 1984 device_xname(sc->sc_dev), r, mask, target); 1985 1986 for (i = 0; i < 10000; i++) { 1987 if ((arc_read(sc, r) & mask) == target) 1988 return 0; 1989 delay(1000); 1990 } 1991 1992 return 1; 1993} 1994 1995#if unused 1996static int 1997arc_wait_ne(struct arc_softc *sc, bus_size_t r, uint32_t mask, 1998 uint32_t target) 1999{ 2000 int i; 2001 2002 DNPRINTF(ARC_D_RW, "%s: arc_wait_ne 0x%lx 0x%08x 0x%08x\n", 2003 device_xname(sc->sc_dev), r, mask, target); 2004 2005 for (i = 0; i < 10000; i++) { 2006 if ((arc_read(sc, r) & mask) != target) 2007 return 0; 2008 delay(1000); 2009 } 2010 2011 return 1; 2012} 2013#endif 2014 2015static int 2016arc_msg0(struct arc_softc *sc, uint32_t m) 2017{ 2018 /* post message */ 2019 arc_write(sc, ARC_REG_INB_MSG0, m); 2020 /* wait for the fw to do it */ 2021 if (arc_wait_eq(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0, 2022 ARC_REG_INTRSTAT_MSG0) != 0) 2023 return 1; 2024 2025 /* ack it */ 2026 arc_write(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0); 2027 2028 return 0; 2029} 2030 2031static struct arc_dmamem * 2032arc_dmamem_alloc(struct arc_softc *sc, size_t size) 2033{ 2034 struct arc_dmamem *adm; 2035 int nsegs; 2036 2037 adm = kmem_zalloc(sizeof(*adm), KM_SLEEP); 2038 adm->adm_size = size; 2039 2040 if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 2041 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &adm->adm_map) != 0) 2042 goto admfree; 2043 2044 if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &adm->adm_seg, 2045 1, &nsegs, BUS_DMA_NOWAIT) != 0) 2046 goto destroy; 2047 2048 if (bus_dmamem_map(sc->sc_dmat, &adm->adm_seg, nsegs, size, 2049 &adm->adm_kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) 2050 goto free; 2051 2052 if (bus_dmamap_load(sc->sc_dmat, adm->adm_map, adm->adm_kva, size, 2053 NULL, BUS_DMA_NOWAIT) != 0) 2054 goto unmap; 2055 2056 memset(adm->adm_kva, 0, size); 2057 2058 return adm; 2059 2060unmap: 2061 bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, size); 2062free: 2063 bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1); 2064destroy: 2065 bus_dmamap_destroy(sc->sc_dmat, adm->adm_map); 2066admfree: 2067 kmem_free(adm, sizeof(*adm)); 2068 2069 return NULL; 2070} 2071 2072static void 2073arc_dmamem_free(struct arc_softc *sc, struct arc_dmamem *adm) 2074{ 2075 bus_dmamap_unload(sc->sc_dmat, adm->adm_map); 2076 bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, adm->adm_size); 2077 bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1); 2078 bus_dmamap_destroy(sc->sc_dmat, adm->adm_map); 2079 kmem_free(adm, sizeof(*adm)); 2080} 2081 2082static int 2083arc_alloc_ccbs(device_t self) 2084{ 2085 struct arc_softc *sc = device_private(self); 2086 struct arc_ccb *ccb; 2087 uint8_t *cmd; 2088 int i; 2089 size_t ccbslen; 2090 2091 TAILQ_INIT(&sc->sc_ccb_free); 2092 2093 ccbslen = sizeof(struct arc_ccb) * sc->sc_req_count; 2094 sc->sc_ccbs = kmem_zalloc(ccbslen, KM_SLEEP); 2095 2096 sc->sc_requests = arc_dmamem_alloc(sc, 2097 ARC_MAX_IOCMDLEN * sc->sc_req_count); 2098 if (sc->sc_requests == NULL) { 2099 aprint_error_dev(self, "unable to allocate ccb dmamem\n"); 2100 goto free_ccbs; 2101 } 2102 cmd = ARC_DMA_KVA(sc->sc_requests); 2103 2104 for (i = 0; i < sc->sc_req_count; i++) { 2105 ccb = &sc->sc_ccbs[i]; 2106 2107 if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, ARC_SGL_MAXLEN, 2108 MAXPHYS, 0, 0, &ccb->ccb_dmamap) != 0) { 2109 aprint_error_dev(self, 2110 "unable to create dmamap for ccb %d\n", i); 2111 goto free_maps; 2112 } 2113 2114 ccb->ccb_sc = sc; 2115 ccb->ccb_id = i; 2116 ccb->ccb_offset = ARC_MAX_IOCMDLEN * i; 2117 2118 ccb->ccb_cmd = (struct arc_io_cmd *)&cmd[ccb->ccb_offset]; 2119 ccb->ccb_cmd_post = (ARC_DMA_DVA(sc->sc_requests) + 2120 ccb->ccb_offset) >> ARC_REG_POST_QUEUE_ADDR_SHIFT; 2121 2122 arc_put_ccb(sc, ccb); 2123 } 2124 2125 return 0; 2126 2127free_maps: 2128 while ((ccb = arc_get_ccb(sc)) != NULL) 2129 bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap); 2130 arc_dmamem_free(sc, sc->sc_requests); 2131 2132free_ccbs: 2133 kmem_free(sc->sc_ccbs, ccbslen); 2134 2135 return 1; 2136} 2137 2138static struct arc_ccb * 2139arc_get_ccb(struct arc_softc *sc) 2140{ 2141 struct arc_ccb *ccb; 2142 2143 ccb = TAILQ_FIRST(&sc->sc_ccb_free); 2144 if (ccb != NULL) 2145 TAILQ_REMOVE(&sc->sc_ccb_free, ccb, ccb_link); 2146 2147 return ccb; 2148} 2149 2150static void 2151arc_put_ccb(struct arc_softc *sc, struct arc_ccb *ccb) 2152{ 2153 ccb->ccb_xs = NULL; 2154 memset(ccb->ccb_cmd, 0, ARC_MAX_IOCMDLEN); 2155 TAILQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_link); 2156} 2157