1 /*- 2 * Copyright (c) 2001 Michael Smith 3 * Copyright (c) 2004 Paul Saab 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Common Interface for SCSI-3 Support driver. 32 * 33 * CISS claims to provide a common interface between a generic SCSI 34 * transport and an intelligent host adapter. 35 * 36 * This driver supports CISS as defined in the document "CISS Command 37 * Interface for SCSI-3 Support Open Specification", Version 1.04, 38 * Valence Number 1, dated 20001127, produced by Compaq Computer 39 * Corporation. This document appears to be a hastily and somewhat 40 * arbitrarlily cut-down version of a larger (and probably even more 41 * chaotic and inconsistent) Compaq internal document. Various 42 * details were also gleaned from Compaq's "cciss" driver for Linux. 43 * 44 * We provide a shim layer between the CISS interface and CAM, 45 * offloading most of the queueing and being-a-disk chores onto CAM. 46 * Entry to the driver is via the PCI bus attachment (ciss_probe, 47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action, 48 * ciss_cam_poll). The Compaq CISS adapters are, however, poor SCSI 49 * citizens and we have to fake up some responses to get reasonable 50 * behaviour out of them. In addition, the CISS command set is by no 51 * means adequate to support the functionality of a RAID controller, 52 * and thus the supported Compaq adapters utilise portions of the 53 * control protocol from earlier Compaq adapter families. 54 * 55 * Note that we only support the "simple" transport layer over PCI. 56 * This interface (ab)uses the I2O register set (specifically the post 57 * queues) to exchange commands with the adapter. Other interfaces 58 * are available, but we aren't supposed to know about them, and it is 59 * dubious whether they would provide major performance improvements 60 * except under extreme load. 61 * 62 * Currently the only supported CISS adapters are the Compaq Smart 63 * Array 5* series (5300, 5i, 532). Even with only three adapters, 64 * Compaq still manage to have interface variations. 65 * 66 * 67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as 68 * well as Paul Saab at Yahoo! for their assistance in making this 69 * driver happen. 70 * 71 * More thanks must go to John Cagle at HP for the countless hours 72 * spent making this driver "work" with the MSA* series storage 73 * enclosures. Without his help (and nagging), this driver could not 74 * be used with these enclosures. 75 */ 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/malloc.h> 80 #include <sys/kernel.h> 81 #include <sys/bus.h> 82 #include <sys/conf.h> 83 #include <sys/stat.h> 84 #include <sys/kthread.h> 85 #include <sys/queue.h> 86 #include <sys/sysctl.h> 87 88 #include <cam/cam.h> 89 #include <cam/cam_ccb.h> 90 #include <cam/cam_periph.h> 91 #include <cam/cam_sim.h> 92 #include <cam/cam_xpt_sim.h> 93 #include <cam/scsi/scsi_all.h> 94 #include <cam/scsi/scsi_message.h> 95 96 #include <machine/bus.h> 97 #include <machine/endian.h> 98 #include <machine/resource.h> 99 #include <sys/rman.h> 100 101 #include <dev/pci/pcireg.h> 102 #include <dev/pci/pcivar.h> 103 104 #include <dev/ciss/cissreg.h> 105 #include <dev/ciss/cissvar.h> 106 #include <dev/ciss/cissio.h> 107 108 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers"); 109 110 /* pci interface */ 111 static int ciss_lookup(device_t dev); 112 static int ciss_probe(device_t dev); 113 static int ciss_attach(device_t dev); 114 static int ciss_detach(device_t dev); 115 static int ciss_shutdown(device_t dev); 116 117 /* (de)initialisation functions, control wrappers */ 118 static int ciss_init_pci(struct ciss_softc *sc); 119 static int ciss_wait_adapter(struct ciss_softc *sc); 120 static int ciss_flush_adapter(struct ciss_softc *sc); 121 static int ciss_init_requests(struct ciss_softc *sc); 122 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, 123 int nseg, int error); 124 static int ciss_identify_adapter(struct ciss_softc *sc); 125 static int ciss_init_logical(struct ciss_softc *sc); 126 static int ciss_init_physical(struct ciss_softc *sc); 127 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll); 128 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld); 129 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld); 130 static int ciss_update_config(struct ciss_softc *sc); 131 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld); 132 static void ciss_init_sysctl(struct ciss_softc *sc); 133 static void ciss_soft_reset(struct ciss_softc *sc); 134 static void ciss_free(struct ciss_softc *sc); 135 static void ciss_spawn_notify_thread(struct ciss_softc *sc); 136 static void ciss_kill_notify_thread(struct ciss_softc *sc); 137 138 /* request submission/completion */ 139 static int ciss_start(struct ciss_request *cr); 140 static void ciss_done(struct ciss_softc *sc); 141 static void ciss_intr(void *arg); 142 static void ciss_complete(struct ciss_softc *sc); 143 static int ciss_report_request(struct ciss_request *cr, int *command_status, 144 int *scsi_status); 145 static int ciss_synch_request(struct ciss_request *cr, int timeout); 146 static int ciss_poll_request(struct ciss_request *cr, int timeout); 147 static int ciss_wait_request(struct ciss_request *cr, int timeout); 148 #if 0 149 static int ciss_abort_request(struct ciss_request *cr); 150 #endif 151 152 /* request queueing */ 153 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp); 154 static void ciss_preen_command(struct ciss_request *cr); 155 static void ciss_release_request(struct ciss_request *cr); 156 157 /* request helpers */ 158 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp, 159 int opcode, void **bufp, size_t bufsize); 160 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc); 161 162 /* DMA map/unmap */ 163 static int ciss_map_request(struct ciss_request *cr); 164 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, 165 int nseg, int error); 166 static void ciss_unmap_request(struct ciss_request *cr); 167 168 /* CAM interface */ 169 static int ciss_cam_init(struct ciss_softc *sc); 170 static void ciss_cam_rescan_target(struct ciss_softc *sc, 171 int bus, int target); 172 static void ciss_cam_rescan_all(struct ciss_softc *sc); 173 static void ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb); 174 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb); 175 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio); 176 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio); 177 static void ciss_cam_poll(struct cam_sim *sim); 178 static void ciss_cam_complete(struct ciss_request *cr); 179 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio); 180 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc, 181 int bus, int target); 182 static int ciss_name_device(struct ciss_softc *sc, int bus, int target); 183 184 /* periodic status monitoring */ 185 static void ciss_periodic(void *arg); 186 static void ciss_disable_adapter(struct ciss_softc *sc); 187 static void ciss_notify_event(struct ciss_softc *sc); 188 static void ciss_notify_complete(struct ciss_request *cr); 189 static int ciss_notify_abort(struct ciss_softc *sc); 190 static int ciss_notify_abort_bmic(struct ciss_softc *sc); 191 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn); 192 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn); 193 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn); 194 195 /* debugging output */ 196 static void ciss_print_request(struct ciss_request *cr); 197 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld); 198 static const char *ciss_name_ldrive_status(int status); 199 static int ciss_decode_ldrive_status(int status); 200 static const char *ciss_name_ldrive_org(int org); 201 static const char *ciss_name_command_status(int status); 202 203 /* 204 * PCI bus interface. 205 */ 206 static device_method_t ciss_methods[] = { 207 /* Device interface */ 208 DEVMETHOD(device_probe, ciss_probe), 209 DEVMETHOD(device_attach, ciss_attach), 210 DEVMETHOD(device_detach, ciss_detach), 211 DEVMETHOD(device_shutdown, ciss_shutdown), 212 { 0, 0 } 213 }; 214 215 static driver_t ciss_pci_driver = { 216 "ciss", 217 ciss_methods, 218 sizeof(struct ciss_softc) 219 }; 220 221 static devclass_t ciss_devclass; 222 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0); 223 MODULE_DEPEND(ciss, cam, 1, 1, 1); 224 MODULE_DEPEND(ciss, pci, 1, 1, 1); 225 226 /* 227 * Control device interface. 228 */ 229 static d_open_t ciss_open; 230 static d_close_t ciss_close; 231 static d_ioctl_t ciss_ioctl; 232 233 static struct cdevsw ciss_cdevsw = { 234 .d_version = D_VERSION, 235 .d_flags = 0, 236 .d_open = ciss_open, 237 .d_close = ciss_close, 238 .d_ioctl = ciss_ioctl, 239 .d_name = "ciss", 240 }; 241 242 /* 243 * This tunable can be set at boot time and controls whether physical devices 244 * that are marked hidden by the firmware should be exposed anyways. 245 */ 246 static unsigned int ciss_expose_hidden_physical = 0; 247 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical); 248 249 /************************************************************************ 250 * CISS adapters amazingly don't have a defined programming interface 251 * value. (One could say some very despairing things about PCI and 252 * people just not getting the general idea.) So we are forced to 253 * stick with matching against subvendor/subdevice, and thus have to 254 * be updated for every new CISS adapter that appears. 255 */ 256 #define CISS_BOARD_SA5 (1<<0) 257 #define CISS_BOARD_SA5B (1<<1) 258 259 static struct 260 { 261 u_int16_t subvendor; 262 u_int16_t subdevice; 263 int flags; 264 char *desc; 265 } ciss_vendor_data[] = { 266 { 0x0e11, 0x4070, CISS_BOARD_SA5, "Compaq Smart Array 5300" }, 267 { 0x0e11, 0x4080, CISS_BOARD_SA5B, "Compaq Smart Array 5i" }, 268 { 0x0e11, 0x4082, CISS_BOARD_SA5B, "Compaq Smart Array 532" }, 269 { 0x0e11, 0x4083, CISS_BOARD_SA5B, "HP Smart Array 5312" }, 270 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" }, 271 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" }, 272 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" }, 273 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" }, 274 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" }, 275 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" }, 276 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" }, 277 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" }, 278 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" }, 279 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" }, 280 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" }, 281 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" }, 282 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" }, 283 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" }, 284 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" }, 285 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" }, 286 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" }, 287 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" }, 288 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" }, 289 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" }, 290 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" }, 291 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array" }, 292 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" }, 293 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" }, 294 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" }, 295 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" }, 296 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" }, 297 { 0, 0, 0, NULL } 298 }; 299 300 /************************************************************************ 301 * Find a match for the device in our list of known adapters. 302 */ 303 static int 304 ciss_lookup(device_t dev) 305 { 306 int i; 307 308 for (i = 0; ciss_vendor_data[i].desc != NULL; i++) 309 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) && 310 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) { 311 return(i); 312 } 313 return(-1); 314 } 315 316 /************************************************************************ 317 * Match a known CISS adapter. 318 */ 319 static int 320 ciss_probe(device_t dev) 321 { 322 int i; 323 324 i = ciss_lookup(dev); 325 if (i != -1) { 326 device_set_desc(dev, ciss_vendor_data[i].desc); 327 return(BUS_PROBE_DEFAULT); 328 } 329 return(ENOENT); 330 } 331 332 /************************************************************************ 333 * Attach the driver to this adapter. 334 */ 335 static int 336 ciss_attach(device_t dev) 337 { 338 struct ciss_softc *sc; 339 int i, error; 340 341 debug_called(1); 342 343 #ifdef CISS_DEBUG 344 /* print structure/union sizes */ 345 debug_struct(ciss_command); 346 debug_struct(ciss_header); 347 debug_union(ciss_device_address); 348 debug_struct(ciss_cdb); 349 debug_struct(ciss_report_cdb); 350 debug_struct(ciss_notify_cdb); 351 debug_struct(ciss_notify); 352 debug_struct(ciss_message_cdb); 353 debug_struct(ciss_error_info_pointer); 354 debug_struct(ciss_error_info); 355 debug_struct(ciss_sg_entry); 356 debug_struct(ciss_config_table); 357 debug_struct(ciss_bmic_cdb); 358 debug_struct(ciss_bmic_id_ldrive); 359 debug_struct(ciss_bmic_id_lstatus); 360 debug_struct(ciss_bmic_id_table); 361 debug_struct(ciss_bmic_id_pdrive); 362 debug_struct(ciss_bmic_blink_pdrive); 363 debug_struct(ciss_bmic_flush_cache); 364 debug_const(CISS_MAX_REQUESTS); 365 debug_const(CISS_MAX_LOGICAL); 366 debug_const(CISS_INTERRUPT_COALESCE_DELAY); 367 debug_const(CISS_INTERRUPT_COALESCE_COUNT); 368 debug_const(CISS_COMMAND_ALLOC_SIZE); 369 debug_const(CISS_COMMAND_SG_LENGTH); 370 371 debug_type(cciss_pci_info_struct); 372 debug_type(cciss_coalint_struct); 373 debug_type(cciss_coalint_struct); 374 debug_type(NodeName_type); 375 debug_type(NodeName_type); 376 debug_type(Heartbeat_type); 377 debug_type(BusTypes_type); 378 debug_type(FirmwareVer_type); 379 debug_type(DriverVer_type); 380 debug_type(IOCTL_Command_struct); 381 #endif 382 383 sc = device_get_softc(dev); 384 sc->ciss_dev = dev; 385 386 /* 387 * Work out adapter type. 388 */ 389 i = ciss_lookup(dev); 390 if (i < 0) { 391 ciss_printf(sc, "unknown adapter type\n"); 392 error = ENXIO; 393 goto out; 394 } 395 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) { 396 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5; 397 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) { 398 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B; 399 } else { 400 /* really an error on our part */ 401 ciss_printf(sc, "unable to determine hardware type\n"); 402 error = ENXIO; 403 goto out; 404 } 405 406 /* 407 * Do PCI-specific init. 408 */ 409 if ((error = ciss_init_pci(sc)) != 0) 410 goto out; 411 412 /* 413 * Initialise driver queues. 414 */ 415 ciss_initq_free(sc); 416 ciss_initq_busy(sc); 417 ciss_initq_complete(sc); 418 ciss_initq_notify(sc); 419 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF); 420 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0); 421 422 /* 423 * Initalize device sysctls. 424 */ 425 ciss_init_sysctl(sc); 426 427 /* 428 * Initialise command/request pool. 429 */ 430 if ((error = ciss_init_requests(sc)) != 0) 431 goto out; 432 433 /* 434 * Get adapter information. 435 */ 436 if ((error = ciss_identify_adapter(sc)) != 0) 437 goto out; 438 439 /* 440 * Find all the physical devices. 441 */ 442 if ((error = ciss_init_physical(sc)) != 0) 443 goto out; 444 445 /* 446 * Build our private table of logical devices. 447 */ 448 if ((error = ciss_init_logical(sc)) != 0) 449 goto out; 450 451 /* 452 * Enable interrupts so that the CAM scan can complete. 453 */ 454 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc); 455 456 /* 457 * Initialise the CAM interface. 458 */ 459 if ((error = ciss_cam_init(sc)) != 0) 460 goto out; 461 462 /* 463 * Start the heartbeat routine and event chain. 464 */ 465 ciss_periodic(sc); 466 467 /* 468 * Create the control device. 469 */ 470 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev), 471 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, 472 "ciss%d", device_get_unit(sc->ciss_dev)); 473 sc->ciss_dev_t->si_drv1 = sc; 474 475 /* 476 * The adapter is running; synchronous commands can now sleep 477 * waiting for an interrupt to signal completion. 478 */ 479 sc->ciss_flags |= CISS_FLAG_RUNNING; 480 481 ciss_spawn_notify_thread(sc); 482 483 error = 0; 484 out: 485 if (error != 0) 486 ciss_free(sc); 487 return(error); 488 } 489 490 /************************************************************************ 491 * Detach the driver from this adapter. 492 */ 493 static int 494 ciss_detach(device_t dev) 495 { 496 struct ciss_softc *sc = device_get_softc(dev); 497 498 debug_called(1); 499 500 mtx_lock(&sc->ciss_mtx); 501 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) { 502 mtx_unlock(&sc->ciss_mtx); 503 return (EBUSY); 504 } 505 506 /* flush adapter cache */ 507 ciss_flush_adapter(sc); 508 509 /* release all resources. The mutex is released and freed here too. */ 510 ciss_free(sc); 511 512 return(0); 513 } 514 515 /************************************************************************ 516 * Prepare adapter for system shutdown. 517 */ 518 static int 519 ciss_shutdown(device_t dev) 520 { 521 struct ciss_softc *sc = device_get_softc(dev); 522 523 debug_called(1); 524 525 mtx_lock(&sc->ciss_mtx); 526 /* flush adapter cache */ 527 ciss_flush_adapter(sc); 528 529 if (sc->ciss_soft_reset) 530 ciss_soft_reset(sc); 531 mtx_unlock(&sc->ciss_mtx); 532 533 return(0); 534 } 535 536 static void 537 ciss_init_sysctl(struct ciss_softc *sc) 538 { 539 540 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev), 541 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)), 542 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, ""); 543 } 544 545 /************************************************************************ 546 * Perform PCI-specific attachment actions. 547 */ 548 static int 549 ciss_init_pci(struct ciss_softc *sc) 550 { 551 uintptr_t cbase, csize, cofs; 552 int error; 553 554 debug_called(1); 555 556 /* 557 * Allocate register window first (we need this to find the config 558 * struct). 559 */ 560 error = ENXIO; 561 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS; 562 if ((sc->ciss_regs_resource = 563 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY, 564 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) { 565 ciss_printf(sc, "can't allocate register window\n"); 566 return(ENXIO); 567 } 568 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource); 569 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource); 570 571 /* 572 * Find the BAR holding the config structure. If it's not the one 573 * we already mapped for registers, map it too. 574 */ 575 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff; 576 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) { 577 if ((sc->ciss_cfg_resource = 578 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY, 579 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) { 580 ciss_printf(sc, "can't allocate config window\n"); 581 return(ENXIO); 582 } 583 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource); 584 csize = rman_get_end(sc->ciss_cfg_resource) - 585 rman_get_start(sc->ciss_cfg_resource) + 1; 586 } else { 587 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource); 588 csize = rman_get_end(sc->ciss_regs_resource) - 589 rman_get_start(sc->ciss_regs_resource) + 1; 590 } 591 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF); 592 593 /* 594 * Use the base/size/offset values we just calculated to 595 * sanity-check the config structure. If it's OK, point to it. 596 */ 597 if ((cofs + sizeof(struct ciss_config_table)) > csize) { 598 ciss_printf(sc, "config table outside window\n"); 599 return(ENXIO); 600 } 601 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs); 602 debug(1, "config struct at %p", sc->ciss_cfg); 603 604 /* 605 * Validate the config structure. If we supported other transport 606 * methods, we could select amongst them at this point in time. 607 */ 608 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) { 609 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n", 610 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1], 611 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]); 612 return(ENXIO); 613 } 614 615 /* 616 * Put the board into simple mode, and tell it we're using the low 617 * 4GB of RAM. Set the default interrupt coalescing options. 618 */ 619 if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) { 620 ciss_printf(sc, "adapter does not support 'simple' transport layer\n"); 621 return(ENXIO); 622 } 623 sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE; 624 sc->ciss_cfg->command_physlimit = 0; 625 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY; 626 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT; 627 628 #ifdef __i386__ 629 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH; 630 #endif 631 632 if (ciss_update_config(sc)) { 633 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n", 634 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR)); 635 return(ENXIO); 636 } 637 if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) { 638 ciss_printf(sc, 639 "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n", 640 sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method); 641 return(ENXIO); 642 } 643 644 /* 645 * Wait for the adapter to come ready. 646 */ 647 if ((error = ciss_wait_adapter(sc)) != 0) 648 return(error); 649 650 /* 651 * Turn off interrupts before we go routing anything. 652 */ 653 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc); 654 655 /* 656 * Allocate and set up our interrupt. 657 */ 658 sc->ciss_irq_rid = 0; 659 if ((sc->ciss_irq_resource = 660 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid, 661 RF_ACTIVE | RF_SHAREABLE)) == NULL) { 662 ciss_printf(sc, "can't allocate interrupt\n"); 663 return(ENXIO); 664 } 665 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource, 666 INTR_TYPE_CAM|INTR_MPSAFE, NULL, ciss_intr, sc, 667 &sc->ciss_intr)) { 668 ciss_printf(sc, "can't set up interrupt\n"); 669 return(ENXIO); 670 } 671 672 /* 673 * Allocate the parent bus DMA tag appropriate for our PCI 674 * interface. 675 * 676 * Note that "simple" adapters can only address within a 32-bit 677 * span. 678 */ 679 if (bus_dma_tag_create(NULL, /* parent */ 680 1, 0, /* alignment, boundary */ 681 BUS_SPACE_MAXADDR, /* lowaddr */ 682 BUS_SPACE_MAXADDR, /* highaddr */ 683 NULL, NULL, /* filter, filterarg */ 684 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 685 CISS_COMMAND_SG_LENGTH, /* nsegments */ 686 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 687 BUS_DMA_ALLOCNOW, /* flags */ 688 NULL, NULL, /* lockfunc, lockarg */ 689 &sc->ciss_parent_dmat)) { 690 ciss_printf(sc, "can't allocate parent DMA tag\n"); 691 return(ENOMEM); 692 } 693 694 /* 695 * Create DMA tag for mapping buffers into adapter-addressable 696 * space. 697 */ 698 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */ 699 1, 0, /* alignment, boundary */ 700 BUS_SPACE_MAXADDR, /* lowaddr */ 701 BUS_SPACE_MAXADDR, /* highaddr */ 702 NULL, NULL, /* filter, filterarg */ 703 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */ 704 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 705 0, /* flags */ 706 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */ 707 &sc->ciss_buffer_dmat)) { 708 ciss_printf(sc, "can't allocate buffer DMA tag\n"); 709 return(ENOMEM); 710 } 711 return(0); 712 } 713 714 /************************************************************************ 715 * Wait for the adapter to come ready. 716 */ 717 static int 718 ciss_wait_adapter(struct ciss_softc *sc) 719 { 720 int i; 721 722 debug_called(1); 723 724 /* 725 * Wait for the adapter to come ready. 726 */ 727 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) { 728 ciss_printf(sc, "waiting for adapter to come ready...\n"); 729 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) { 730 DELAY(1000000); /* one second */ 731 if (i > 30) { 732 ciss_printf(sc, "timed out waiting for adapter to come ready\n"); 733 return(EIO); 734 } 735 } 736 } 737 return(0); 738 } 739 740 /************************************************************************ 741 * Flush the adapter cache. 742 */ 743 static int 744 ciss_flush_adapter(struct ciss_softc *sc) 745 { 746 struct ciss_request *cr; 747 struct ciss_bmic_flush_cache *cbfc; 748 int error, command_status; 749 750 debug_called(1); 751 752 cr = NULL; 753 cbfc = NULL; 754 755 /* 756 * Build a BMIC request to flush the cache. We don't disable 757 * it, as we may be going to do more I/O (eg. we are emulating 758 * the Synchronise Cache command). 759 */ 760 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) { 761 error = ENOMEM; 762 goto out; 763 } 764 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE, 765 (void **)&cbfc, sizeof(*cbfc))) != 0) 766 goto out; 767 768 /* 769 * Submit the request and wait for it to complete. 770 */ 771 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 772 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error); 773 goto out; 774 } 775 776 /* 777 * Check response. 778 */ 779 ciss_report_request(cr, &command_status, NULL); 780 switch(command_status) { 781 case CISS_CMD_STATUS_SUCCESS: 782 break; 783 default: 784 ciss_printf(sc, "error flushing cache (%s)\n", 785 ciss_name_command_status(command_status)); 786 error = EIO; 787 goto out; 788 } 789 790 out: 791 if (cbfc != NULL) 792 free(cbfc, CISS_MALLOC_CLASS); 793 if (cr != NULL) 794 ciss_release_request(cr); 795 return(error); 796 } 797 798 static void 799 ciss_soft_reset(struct ciss_softc *sc) 800 { 801 struct ciss_request *cr = NULL; 802 struct ciss_command *cc; 803 int i, error = 0; 804 805 for (i = 0; i < sc->ciss_max_logical_bus; i++) { 806 /* only reset proxy controllers */ 807 if (sc->ciss_controllers[i].physical.bus == 0) 808 continue; 809 810 if ((error = ciss_get_request(sc, &cr)) != 0) 811 break; 812 813 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET, 814 NULL, 0)) != 0) 815 break; 816 817 cc = CISS_FIND_COMMAND(cr); 818 cc->header.address = sc->ciss_controllers[i]; 819 820 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) 821 break; 822 823 ciss_release_request(cr); 824 } 825 826 if (error) 827 ciss_printf(sc, "error resetting controller (%d)\n", error); 828 829 if (cr != NULL) 830 ciss_release_request(cr); 831 } 832 833 /************************************************************************ 834 * Allocate memory for the adapter command structures, initialise 835 * the request structures. 836 * 837 * Note that the entire set of commands are allocated in a single 838 * contiguous slab. 839 */ 840 static int 841 ciss_init_requests(struct ciss_softc *sc) 842 { 843 struct ciss_request *cr; 844 int i; 845 846 debug_called(1); 847 848 /* 849 * Calculate the number of request structures/commands we are 850 * going to provide for this adapter. 851 */ 852 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands); 853 854 if (bootverbose) 855 ciss_printf(sc, "using %d of %d available commands\n", 856 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands); 857 858 /* 859 * Create the DMA tag for commands. 860 */ 861 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */ 862 1, 0, /* alignment, boundary */ 863 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 864 BUS_SPACE_MAXADDR, /* highaddr */ 865 NULL, NULL, /* filter, filterarg */ 866 CISS_COMMAND_ALLOC_SIZE * 867 sc->ciss_max_requests, 1, /* maxsize, nsegments */ 868 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 869 BUS_DMA_ALLOCNOW, /* flags */ 870 NULL, NULL, /* lockfunc, lockarg */ 871 &sc->ciss_command_dmat)) { 872 ciss_printf(sc, "can't allocate command DMA tag\n"); 873 return(ENOMEM); 874 } 875 /* 876 * Allocate memory and make it available for DMA. 877 */ 878 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command, 879 BUS_DMA_NOWAIT, &sc->ciss_command_map)) { 880 ciss_printf(sc, "can't allocate command memory\n"); 881 return(ENOMEM); 882 } 883 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command, 884 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests, 885 ciss_command_map_helper, sc, 0); 886 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests); 887 888 /* 889 * Set up the request and command structures, push requests onto 890 * the free queue. 891 */ 892 for (i = 1; i < sc->ciss_max_requests; i++) { 893 cr = &sc->ciss_request[i]; 894 cr->cr_sc = sc; 895 cr->cr_tag = i; 896 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap); 897 ciss_enqueue_free(cr); 898 } 899 return(0); 900 } 901 902 static void 903 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) 904 { 905 struct ciss_softc *sc = (struct ciss_softc *)arg; 906 907 sc->ciss_command_phys = segs->ds_addr; 908 } 909 910 /************************************************************************ 911 * Identify the adapter, print some information about it. 912 */ 913 static int 914 ciss_identify_adapter(struct ciss_softc *sc) 915 { 916 struct ciss_request *cr; 917 int error, command_status; 918 919 debug_called(1); 920 921 cr = NULL; 922 923 /* 924 * Get a request, allocate storage for the adapter data. 925 */ 926 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR, 927 (void **)&sc->ciss_id, 928 sizeof(*sc->ciss_id))) != 0) 929 goto out; 930 931 /* 932 * Submit the request and wait for it to complete. 933 */ 934 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 935 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error); 936 goto out; 937 } 938 939 /* 940 * Check response. 941 */ 942 ciss_report_request(cr, &command_status, NULL); 943 switch(command_status) { 944 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */ 945 break; 946 case CISS_CMD_STATUS_DATA_UNDERRUN: 947 case CISS_CMD_STATUS_DATA_OVERRUN: 948 ciss_printf(sc, "data over/underrun reading adapter information\n"); 949 default: 950 ciss_printf(sc, "error reading adapter information (%s)\n", 951 ciss_name_command_status(command_status)); 952 error = EIO; 953 goto out; 954 } 955 956 /* sanity-check reply */ 957 if (!sc->ciss_id->big_map_supported) { 958 ciss_printf(sc, "adapter does not support BIG_MAP\n"); 959 error = ENXIO; 960 goto out; 961 } 962 963 #if 0 964 /* XXX later revisions may not need this */ 965 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH; 966 #endif 967 968 /* XXX only really required for old 5300 adapters? */ 969 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT; 970 971 /* print information */ 972 if (bootverbose) { 973 #if 0 /* XXX proxy volumes??? */ 974 ciss_printf(sc, " %d logical drive%s configured\n", 975 sc->ciss_id->configured_logical_drives, 976 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s"); 977 #endif 978 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision); 979 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count); 980 981 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature); 982 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence); 983 ciss_printf(sc, " supported I/O methods 0x%b\n", 984 sc->ciss_cfg->supported_methods, 985 "\20\1READY\2simple\3performant\4MEMQ\n"); 986 ciss_printf(sc, " active I/O method 0x%b\n", 987 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n"); 988 ciss_printf(sc, " 4G page base 0x%08x\n", 989 sc->ciss_cfg->command_physlimit); 990 ciss_printf(sc, " interrupt coalesce delay %dus\n", 991 sc->ciss_cfg->interrupt_coalesce_delay); 992 ciss_printf(sc, " interrupt coalesce count %d\n", 993 sc->ciss_cfg->interrupt_coalesce_count); 994 ciss_printf(sc, " max outstanding commands %d\n", 995 sc->ciss_cfg->max_outstanding_commands); 996 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types, 997 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n"); 998 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name); 999 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat); 1000 } 1001 1002 out: 1003 if (error) { 1004 if (sc->ciss_id != NULL) { 1005 free(sc->ciss_id, CISS_MALLOC_CLASS); 1006 sc->ciss_id = NULL; 1007 } 1008 } 1009 if (cr != NULL) 1010 ciss_release_request(cr); 1011 return(error); 1012 } 1013 1014 /************************************************************************ 1015 * Helper routine for generating a list of logical and physical luns. 1016 */ 1017 static struct ciss_lun_report * 1018 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits) 1019 { 1020 struct ciss_request *cr; 1021 struct ciss_command *cc; 1022 struct ciss_report_cdb *crc; 1023 struct ciss_lun_report *cll; 1024 int command_status; 1025 int report_size; 1026 int error = 0; 1027 1028 debug_called(1); 1029 1030 cr = NULL; 1031 cll = NULL; 1032 1033 /* 1034 * Get a request, allocate storage for the address list. 1035 */ 1036 if ((error = ciss_get_request(sc, &cr)) != 0) 1037 goto out; 1038 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address); 1039 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) { 1040 ciss_printf(sc, "can't allocate memory for lun report\n"); 1041 error = ENOMEM; 1042 goto out; 1043 } 1044 1045 /* 1046 * Build the Report Logical/Physical LUNs command. 1047 */ 1048 cc = CISS_FIND_COMMAND(cr); 1049 cr->cr_data = cll; 1050 cr->cr_length = report_size; 1051 cr->cr_flags = CISS_REQ_DATAIN; 1052 1053 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; 1054 cc->header.address.physical.bus = 0; 1055 cc->header.address.physical.target = 0; 1056 cc->cdb.cdb_length = sizeof(*crc); 1057 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 1058 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 1059 cc->cdb.direction = CISS_CDB_DIRECTION_READ; 1060 cc->cdb.timeout = 30; /* XXX better suggestions? */ 1061 1062 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]); 1063 bzero(crc, sizeof(*crc)); 1064 crc->opcode = opcode; 1065 crc->length = htonl(report_size); /* big-endian field */ 1066 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */ 1067 1068 /* 1069 * Submit the request and wait for it to complete. (timeout 1070 * here should be much greater than above) 1071 */ 1072 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 1073 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error); 1074 goto out; 1075 } 1076 1077 /* 1078 * Check response. Note that data over/underrun is OK. 1079 */ 1080 ciss_report_request(cr, &command_status, NULL); 1081 switch(command_status) { 1082 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */ 1083 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */ 1084 break; 1085 case CISS_CMD_STATUS_DATA_OVERRUN: 1086 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n", 1087 CISS_MAX_LOGICAL); 1088 break; 1089 default: 1090 ciss_printf(sc, "error detecting logical drive configuration (%s)\n", 1091 ciss_name_command_status(command_status)); 1092 error = EIO; 1093 goto out; 1094 } 1095 ciss_release_request(cr); 1096 cr = NULL; 1097 1098 out: 1099 if (cr != NULL) 1100 ciss_release_request(cr); 1101 if (error && cll != NULL) { 1102 free(cll, CISS_MALLOC_CLASS); 1103 cll = NULL; 1104 } 1105 return(cll); 1106 } 1107 1108 /************************************************************************ 1109 * Find logical drives on the adapter. 1110 */ 1111 static int 1112 ciss_init_logical(struct ciss_softc *sc) 1113 { 1114 struct ciss_lun_report *cll; 1115 int error = 0, i, j; 1116 int ndrives; 1117 1118 debug_called(1); 1119 1120 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS, 1121 CISS_MAX_LOGICAL); 1122 if (cll == NULL) { 1123 error = ENXIO; 1124 goto out; 1125 } 1126 1127 /* sanity-check reply */ 1128 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address)); 1129 if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) { 1130 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n", 1131 ndrives, CISS_MAX_LOGICAL); 1132 error = ENXIO; 1133 goto out; 1134 } 1135 1136 /* 1137 * Save logical drive information. 1138 */ 1139 if (bootverbose) { 1140 ciss_printf(sc, "%d logical drive%s\n", 1141 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : ""); 1142 } 1143 1144 sc->ciss_logical = 1145 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *), 1146 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 1147 if (sc->ciss_logical == NULL) { 1148 error = ENXIO; 1149 goto out; 1150 } 1151 1152 for (i = 0; i <= sc->ciss_max_logical_bus; i++) { 1153 sc->ciss_logical[i] = 1154 malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive), 1155 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 1156 if (sc->ciss_logical[i] == NULL) { 1157 error = ENXIO; 1158 goto out; 1159 } 1160 1161 for (j = 0; j < CISS_MAX_LOGICAL; j++) 1162 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT; 1163 } 1164 1165 1166 for (i = 0; i < CISS_MAX_LOGICAL; i++) { 1167 if (i < ndrives) { 1168 struct ciss_ldrive *ld; 1169 int bus, target; 1170 1171 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun); 1172 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun); 1173 ld = &sc->ciss_logical[bus][target]; 1174 1175 ld->cl_address = cll->lun[i]; 1176 ld->cl_controller = &sc->ciss_controllers[bus]; 1177 if (ciss_identify_logical(sc, ld) != 0) 1178 continue; 1179 /* 1180 * If the drive has had media exchanged, we should bring it online. 1181 */ 1182 if (ld->cl_lstatus->media_exchanged) 1183 ciss_accept_media(sc, ld); 1184 1185 } 1186 } 1187 1188 out: 1189 if (cll != NULL) 1190 free(cll, CISS_MALLOC_CLASS); 1191 return(error); 1192 } 1193 1194 static int 1195 ciss_init_physical(struct ciss_softc *sc) 1196 { 1197 struct ciss_lun_report *cll; 1198 int error = 0, i; 1199 int nphys; 1200 int bus, target; 1201 1202 debug_called(1); 1203 1204 bus = 0; 1205 target = 0; 1206 1207 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS, 1208 CISS_MAX_PHYSICAL); 1209 if (cll == NULL) { 1210 error = ENXIO; 1211 goto out; 1212 } 1213 1214 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address)); 1215 1216 if (bootverbose) { 1217 ciss_printf(sc, "%d physical device%s\n", 1218 nphys, (nphys > 1 || nphys == 0) ? "s" : ""); 1219 } 1220 1221 /* 1222 * Figure out the bus mapping. 1223 * Logical buses include both the local logical bus for local arrays and 1224 * proxy buses for remote arrays. Physical buses are numbered by the 1225 * controller and represent physical buses that hold physical devices. 1226 * We shift these bus numbers so that everything fits into a single flat 1227 * numbering space for CAM. Logical buses occupy the first 32 CAM bus 1228 * numbers, and the physical bus numbers are shifted to be above that. 1229 * This results in the various driver arrays being indexed as follows: 1230 * 1231 * ciss_controllers[] - indexed by logical bus 1232 * ciss_cam_sim[] - indexed by both logical and physical, with physical 1233 * being shifted by 32. 1234 * ciss_logical[][] - indexed by logical bus 1235 * ciss_physical[][] - indexed by physical bus 1236 * 1237 * XXX This is getting more and more hackish. CISS really doesn't play 1238 * well with a standard SCSI model; devices are addressed via magic 1239 * cookies, not via b/t/l addresses. Since there is no way to store 1240 * the cookie in the CAM device object, we have to keep these lookup 1241 * tables handy so that the devices can be found quickly at the cost 1242 * of wasting memory and having a convoluted lookup scheme. This 1243 * driver should probably be converted to block interface. 1244 */ 1245 /* 1246 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy 1247 * controller. A proxy controller is another physical controller 1248 * behind the primary PCI controller. We need to know about this 1249 * so that BMIC commands can be properly targeted. There can be 1250 * proxy controllers attached to a single PCI controller, so 1251 * find the highest numbered one so the array can be properly 1252 * sized. 1253 */ 1254 sc->ciss_max_logical_bus = 1; 1255 for (i = 0; i < nphys; i++) { 1256 if (cll->lun[i].physical.extra_address == 0) { 1257 bus = cll->lun[i].physical.bus; 1258 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1; 1259 } else { 1260 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address); 1261 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus); 1262 } 1263 } 1264 1265 sc->ciss_controllers = 1266 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address), 1267 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 1268 1269 if (sc->ciss_controllers == NULL) { 1270 ciss_printf(sc, "Could not allocate memory for controller map\n"); 1271 error = ENOMEM; 1272 goto out; 1273 } 1274 1275 /* setup a map of controller addresses */ 1276 for (i = 0; i < nphys; i++) { 1277 if (cll->lun[i].physical.extra_address == 0) { 1278 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i]; 1279 } 1280 } 1281 1282 sc->ciss_physical = 1283 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *), 1284 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 1285 if (sc->ciss_physical == NULL) { 1286 ciss_printf(sc, "Could not allocate memory for physical device map\n"); 1287 error = ENOMEM; 1288 goto out; 1289 } 1290 1291 for (i = 0; i < sc->ciss_max_physical_bus; i++) { 1292 sc->ciss_physical[i] = 1293 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT, 1294 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 1295 if (sc->ciss_physical[i] == NULL) { 1296 ciss_printf(sc, "Could not allocate memory for target map\n"); 1297 error = ENOMEM; 1298 goto out; 1299 } 1300 } 1301 1302 ciss_filter_physical(sc, cll); 1303 1304 out: 1305 if (cll != NULL) 1306 free(cll, CISS_MALLOC_CLASS); 1307 1308 return(error); 1309 } 1310 1311 static int 1312 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll) 1313 { 1314 u_int32_t ea; 1315 int i, nphys; 1316 int bus, target; 1317 1318 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address)); 1319 for (i = 0; i < nphys; i++) { 1320 if (cll->lun[i].physical.extra_address == 0) 1321 continue; 1322 1323 /* 1324 * Filter out devices that we don't want. Level 3 LUNs could 1325 * probably be supported, but the docs don't give enough of a 1326 * hint to know how. 1327 * 1328 * The mode field of the physical address is likely set to have 1329 * hard disks masked out. Honor it unless the user has overridden 1330 * us with the tunable. We also munge the inquiry data for these 1331 * disks so that they only show up as passthrough devices. Keeping 1332 * them visible in this fashion is useful for doing things like 1333 * flashing firmware. 1334 */ 1335 ea = cll->lun[i].physical.extra_address; 1336 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) || 1337 (CISS_EXTRA_MODE2(ea) == 0x3)) 1338 continue; 1339 if ((ciss_expose_hidden_physical == 0) && 1340 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL)) 1341 continue; 1342 1343 /* 1344 * Note: CISS firmware numbers physical busses starting at '1', not 1345 * '0'. This numbering is internal to the firmware and is only 1346 * used as a hint here. 1347 */ 1348 bus = CISS_EXTRA_BUS2(ea) - 1; 1349 target = CISS_EXTRA_TARGET2(ea); 1350 sc->ciss_physical[bus][target].cp_address = cll->lun[i]; 1351 sc->ciss_physical[bus][target].cp_online = 1; 1352 } 1353 1354 return (0); 1355 } 1356 1357 static int 1358 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld) 1359 { 1360 struct ciss_request *cr; 1361 struct ciss_command *cc; 1362 struct scsi_inquiry *inq; 1363 int error; 1364 int command_status; 1365 1366 cr = NULL; 1367 1368 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry)); 1369 1370 if ((error = ciss_get_request(sc, &cr)) != 0) 1371 goto out; 1372 1373 cc = CISS_FIND_COMMAND(cr); 1374 cr->cr_data = &ld->cl_geometry; 1375 cr->cr_length = sizeof(ld->cl_geometry); 1376 cr->cr_flags = CISS_REQ_DATAIN; 1377 1378 cc->header.address = ld->cl_address; 1379 cc->cdb.cdb_length = 6; 1380 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 1381 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 1382 cc->cdb.direction = CISS_CDB_DIRECTION_READ; 1383 cc->cdb.timeout = 30; 1384 1385 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]); 1386 inq->opcode = INQUIRY; 1387 inq->byte2 = SI_EVPD; 1388 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY; 1389 inq->length = sizeof(ld->cl_geometry); 1390 1391 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 1392 ciss_printf(sc, "error getting geometry (%d)\n", error); 1393 goto out; 1394 } 1395 1396 ciss_report_request(cr, &command_status, NULL); 1397 switch(command_status) { 1398 case CISS_CMD_STATUS_SUCCESS: 1399 case CISS_CMD_STATUS_DATA_UNDERRUN: 1400 break; 1401 case CISS_CMD_STATUS_DATA_OVERRUN: 1402 ciss_printf(sc, "WARNING: Data overrun\n"); 1403 break; 1404 default: 1405 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n", 1406 ciss_name_command_status(command_status)); 1407 break; 1408 } 1409 1410 out: 1411 if (cr != NULL) 1412 ciss_release_request(cr); 1413 return(error); 1414 } 1415 /************************************************************************ 1416 * Identify a logical drive, initialise state related to it. 1417 */ 1418 static int 1419 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld) 1420 { 1421 struct ciss_request *cr; 1422 struct ciss_command *cc; 1423 struct ciss_bmic_cdb *cbc; 1424 int error, command_status; 1425 1426 debug_called(1); 1427 1428 cr = NULL; 1429 1430 /* 1431 * Build a BMIC request to fetch the drive ID. 1432 */ 1433 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE, 1434 (void **)&ld->cl_ldrive, 1435 sizeof(*ld->cl_ldrive))) != 0) 1436 goto out; 1437 cc = CISS_FIND_COMMAND(cr); 1438 cc->header.address = *ld->cl_controller; /* target controller */ 1439 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]); 1440 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun); 1441 1442 /* 1443 * Submit the request and wait for it to complete. 1444 */ 1445 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 1446 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error); 1447 goto out; 1448 } 1449 1450 /* 1451 * Check response. 1452 */ 1453 ciss_report_request(cr, &command_status, NULL); 1454 switch(command_status) { 1455 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */ 1456 break; 1457 case CISS_CMD_STATUS_DATA_UNDERRUN: 1458 case CISS_CMD_STATUS_DATA_OVERRUN: 1459 ciss_printf(sc, "data over/underrun reading logical drive ID\n"); 1460 default: 1461 ciss_printf(sc, "error reading logical drive ID (%s)\n", 1462 ciss_name_command_status(command_status)); 1463 error = EIO; 1464 goto out; 1465 } 1466 ciss_release_request(cr); 1467 cr = NULL; 1468 1469 /* 1470 * Build a CISS BMIC command to get the logical drive status. 1471 */ 1472 if ((error = ciss_get_ldrive_status(sc, ld)) != 0) 1473 goto out; 1474 1475 /* 1476 * Get the logical drive geometry. 1477 */ 1478 if ((error = ciss_inquiry_logical(sc, ld)) != 0) 1479 goto out; 1480 1481 /* 1482 * Print the drive's basic characteristics. 1483 */ 1484 if (bootverbose) { 1485 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ", 1486 CISS_LUN_TO_BUS(ld->cl_address.logical.lun), 1487 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun), 1488 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance), 1489 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) * 1490 ld->cl_ldrive->block_size)); 1491 1492 ciss_print_ldrive(sc, ld); 1493 } 1494 out: 1495 if (error != 0) { 1496 /* make the drive not-exist */ 1497 ld->cl_status = CISS_LD_NONEXISTENT; 1498 if (ld->cl_ldrive != NULL) { 1499 free(ld->cl_ldrive, CISS_MALLOC_CLASS); 1500 ld->cl_ldrive = NULL; 1501 } 1502 if (ld->cl_lstatus != NULL) { 1503 free(ld->cl_lstatus, CISS_MALLOC_CLASS); 1504 ld->cl_lstatus = NULL; 1505 } 1506 } 1507 if (cr != NULL) 1508 ciss_release_request(cr); 1509 1510 return(error); 1511 } 1512 1513 /************************************************************************ 1514 * Get status for a logical drive. 1515 * 1516 * XXX should we also do this in response to Test Unit Ready? 1517 */ 1518 static int 1519 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld) 1520 { 1521 struct ciss_request *cr; 1522 struct ciss_command *cc; 1523 struct ciss_bmic_cdb *cbc; 1524 int error, command_status; 1525 1526 /* 1527 * Build a CISS BMIC command to get the logical drive status. 1528 */ 1529 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS, 1530 (void **)&ld->cl_lstatus, 1531 sizeof(*ld->cl_lstatus))) != 0) 1532 goto out; 1533 cc = CISS_FIND_COMMAND(cr); 1534 cc->header.address = *ld->cl_controller; /* target controller */ 1535 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]); 1536 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun); 1537 1538 /* 1539 * Submit the request and wait for it to complete. 1540 */ 1541 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 1542 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error); 1543 goto out; 1544 } 1545 1546 /* 1547 * Check response. 1548 */ 1549 ciss_report_request(cr, &command_status, NULL); 1550 switch(command_status) { 1551 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */ 1552 break; 1553 case CISS_CMD_STATUS_DATA_UNDERRUN: 1554 case CISS_CMD_STATUS_DATA_OVERRUN: 1555 ciss_printf(sc, "data over/underrun reading logical drive status\n"); 1556 default: 1557 ciss_printf(sc, "error reading logical drive status (%s)\n", 1558 ciss_name_command_status(command_status)); 1559 error = EIO; 1560 goto out; 1561 } 1562 1563 /* 1564 * Set the drive's summary status based on the returned status. 1565 * 1566 * XXX testing shows that a failed JBOD drive comes back at next 1567 * boot in "queued for expansion" mode. WTF? 1568 */ 1569 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status); 1570 1571 out: 1572 if (cr != NULL) 1573 ciss_release_request(cr); 1574 return(error); 1575 } 1576 1577 /************************************************************************ 1578 * Notify the adapter of a config update. 1579 */ 1580 static int 1581 ciss_update_config(struct ciss_softc *sc) 1582 { 1583 int i; 1584 1585 debug_called(1); 1586 1587 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE); 1588 for (i = 0; i < 1000; i++) { 1589 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) & 1590 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) { 1591 return(0); 1592 } 1593 DELAY(1000); 1594 } 1595 return(1); 1596 } 1597 1598 /************************************************************************ 1599 * Accept new media into a logical drive. 1600 * 1601 * XXX The drive has previously been offline; it would be good if we 1602 * could make sure it's not open right now. 1603 */ 1604 static int 1605 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld) 1606 { 1607 struct ciss_request *cr; 1608 struct ciss_command *cc; 1609 struct ciss_bmic_cdb *cbc; 1610 int command_status; 1611 int error = 0, ldrive; 1612 1613 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun); 1614 1615 debug(0, "bringing logical drive %d back online"); 1616 1617 /* 1618 * Build a CISS BMIC command to bring the drive back online. 1619 */ 1620 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA, 1621 NULL, 0)) != 0) 1622 goto out; 1623 cc = CISS_FIND_COMMAND(cr); 1624 cc->header.address = *ld->cl_controller; /* target controller */ 1625 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]); 1626 cbc->log_drive = ldrive; 1627 1628 /* 1629 * Submit the request and wait for it to complete. 1630 */ 1631 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 1632 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error); 1633 goto out; 1634 } 1635 1636 /* 1637 * Check response. 1638 */ 1639 ciss_report_request(cr, &command_status, NULL); 1640 switch(command_status) { 1641 case CISS_CMD_STATUS_SUCCESS: /* all OK */ 1642 /* we should get a logical drive status changed event here */ 1643 break; 1644 default: 1645 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n", 1646 ciss_name_command_status(command_status)); 1647 break; 1648 } 1649 1650 out: 1651 if (cr != NULL) 1652 ciss_release_request(cr); 1653 return(error); 1654 } 1655 1656 /************************************************************************ 1657 * Release adapter resources. 1658 */ 1659 static void 1660 ciss_free(struct ciss_softc *sc) 1661 { 1662 struct ciss_request *cr; 1663 int i, j; 1664 1665 debug_called(1); 1666 1667 /* we're going away */ 1668 sc->ciss_flags |= CISS_FLAG_ABORTING; 1669 1670 /* terminate the periodic heartbeat routine */ 1671 callout_stop(&sc->ciss_periodic); 1672 1673 /* cancel the Event Notify chain */ 1674 ciss_notify_abort(sc); 1675 1676 ciss_kill_notify_thread(sc); 1677 1678 /* disconnect from CAM */ 1679 if (sc->ciss_cam_sim) { 1680 for (i = 0; i < sc->ciss_max_logical_bus; i++) { 1681 if (sc->ciss_cam_sim[i]) { 1682 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i])); 1683 cam_sim_free(sc->ciss_cam_sim[i], 0); 1684 } 1685 } 1686 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus + 1687 CISS_PHYSICAL_BASE; i++) { 1688 if (sc->ciss_cam_sim[i]) { 1689 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i])); 1690 cam_sim_free(sc->ciss_cam_sim[i], 0); 1691 } 1692 } 1693 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS); 1694 } 1695 if (sc->ciss_cam_devq) 1696 cam_simq_free(sc->ciss_cam_devq); 1697 1698 /* remove the control device */ 1699 mtx_unlock(&sc->ciss_mtx); 1700 if (sc->ciss_dev_t != NULL) 1701 destroy_dev(sc->ciss_dev_t); 1702 1703 /* Final cleanup of the callout. */ 1704 callout_drain(&sc->ciss_periodic); 1705 mtx_destroy(&sc->ciss_mtx); 1706 1707 /* free the controller data */ 1708 if (sc->ciss_id != NULL) 1709 free(sc->ciss_id, CISS_MALLOC_CLASS); 1710 1711 /* release I/O resources */ 1712 if (sc->ciss_regs_resource != NULL) 1713 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY, 1714 sc->ciss_regs_rid, sc->ciss_regs_resource); 1715 if (sc->ciss_cfg_resource != NULL) 1716 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY, 1717 sc->ciss_cfg_rid, sc->ciss_cfg_resource); 1718 if (sc->ciss_intr != NULL) 1719 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr); 1720 if (sc->ciss_irq_resource != NULL) 1721 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ, 1722 sc->ciss_irq_rid, sc->ciss_irq_resource); 1723 1724 /* destroy DMA tags */ 1725 if (sc->ciss_parent_dmat) 1726 bus_dma_tag_destroy(sc->ciss_parent_dmat); 1727 1728 while ((cr = ciss_dequeue_free(sc)) != NULL) 1729 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap); 1730 if (sc->ciss_buffer_dmat) 1731 bus_dma_tag_destroy(sc->ciss_buffer_dmat); 1732 1733 /* destroy command memory and DMA tag */ 1734 if (sc->ciss_command != NULL) { 1735 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map); 1736 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map); 1737 } 1738 if (sc->ciss_command_dmat) 1739 bus_dma_tag_destroy(sc->ciss_command_dmat); 1740 1741 if (sc->ciss_logical) { 1742 for (i = 0; i <= sc->ciss_max_logical_bus; i++) { 1743 for (j = 0; j < CISS_MAX_LOGICAL; j++) { 1744 if (sc->ciss_logical[i][j].cl_ldrive) 1745 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS); 1746 if (sc->ciss_logical[i][j].cl_lstatus) 1747 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS); 1748 } 1749 free(sc->ciss_logical[i], CISS_MALLOC_CLASS); 1750 } 1751 free(sc->ciss_logical, CISS_MALLOC_CLASS); 1752 } 1753 1754 if (sc->ciss_physical) { 1755 for (i = 0; i < sc->ciss_max_physical_bus; i++) 1756 free(sc->ciss_physical[i], CISS_MALLOC_CLASS); 1757 free(sc->ciss_physical, CISS_MALLOC_CLASS); 1758 } 1759 1760 if (sc->ciss_controllers) 1761 free(sc->ciss_controllers, CISS_MALLOC_CLASS); 1762 1763 } 1764 1765 /************************************************************************ 1766 * Give a command to the adapter. 1767 * 1768 * Note that this uses the simple transport layer directly. If we 1769 * want to add support for other layers, we'll need a switch of some 1770 * sort. 1771 * 1772 * Note that the simple transport layer has no way of refusing a 1773 * command; we only have as many request structures as the adapter 1774 * supports commands, so we don't have to check (this presumes that 1775 * the adapter can handle commands as fast as we throw them at it). 1776 */ 1777 static int 1778 ciss_start(struct ciss_request *cr) 1779 { 1780 struct ciss_command *cc; /* XXX debugging only */ 1781 int error; 1782 1783 cc = CISS_FIND_COMMAND(cr); 1784 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag); 1785 1786 /* 1787 * Map the request's data. 1788 */ 1789 if ((error = ciss_map_request(cr))) 1790 return(error); 1791 1792 #if 0 1793 ciss_print_request(cr); 1794 #endif 1795 1796 return(0); 1797 } 1798 1799 /************************************************************************ 1800 * Fetch completed request(s) from the adapter, queue them for 1801 * completion handling. 1802 * 1803 * Note that this uses the simple transport layer directly. If we 1804 * want to add support for other layers, we'll need a switch of some 1805 * sort. 1806 * 1807 * Note that the simple transport mechanism does not require any 1808 * reentrancy protection; the OPQ read is atomic. If there is a 1809 * chance of a race with something else that might move the request 1810 * off the busy list, then we will have to lock against that 1811 * (eg. timeouts, etc.) 1812 */ 1813 static void 1814 ciss_done(struct ciss_softc *sc) 1815 { 1816 struct ciss_request *cr; 1817 struct ciss_command *cc; 1818 u_int32_t tag, index; 1819 int complete; 1820 1821 debug_called(3); 1822 1823 /* 1824 * Loop quickly taking requests from the adapter and moving them 1825 * from the busy queue to the completed queue. 1826 */ 1827 complete = 0; 1828 for (;;) { 1829 1830 /* see if the OPQ contains anything */ 1831 if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc)) 1832 break; 1833 1834 tag = CISS_TL_SIMPLE_FETCH_CMD(sc); 1835 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY) 1836 break; 1837 index = tag >> 2; 1838 debug(2, "completed command %d%s", index, 1839 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : ""); 1840 if (index >= sc->ciss_max_requests) { 1841 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag); 1842 continue; 1843 } 1844 cr = &(sc->ciss_request[index]); 1845 cc = CISS_FIND_COMMAND(cr); 1846 cc->header.host_tag = tag; /* not updated by adapter */ 1847 if (ciss_remove_busy(cr)) { 1848 /* assume this is garbage out of the adapter */ 1849 ciss_printf(sc, "completed nonbusy request %d\n", index); 1850 } else { 1851 ciss_enqueue_complete(cr); 1852 } 1853 complete = 1; 1854 } 1855 1856 /* 1857 * Invoke completion processing. If we can defer this out of 1858 * interrupt context, that'd be good. 1859 */ 1860 if (complete) 1861 ciss_complete(sc); 1862 } 1863 1864 /************************************************************************ 1865 * Take an interrupt from the adapter. 1866 */ 1867 static void 1868 ciss_intr(void *arg) 1869 { 1870 struct ciss_softc *sc = (struct ciss_softc *)arg; 1871 1872 /* 1873 * The only interrupt we recognise indicates that there are 1874 * entries in the outbound post queue. 1875 */ 1876 mtx_lock(&sc->ciss_mtx); 1877 ciss_done(sc); 1878 mtx_unlock(&sc->ciss_mtx); 1879 } 1880 1881 /************************************************************************ 1882 * Process completed requests. 1883 * 1884 * Requests can be completed in three fashions: 1885 * 1886 * - by invoking a callback function (cr_complete is non-null) 1887 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set) 1888 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context 1889 */ 1890 static void 1891 ciss_complete(struct ciss_softc *sc) 1892 { 1893 struct ciss_request *cr; 1894 1895 debug_called(2); 1896 1897 /* 1898 * Loop taking requests off the completed queue and performing 1899 * completion processing on them. 1900 */ 1901 for (;;) { 1902 if ((cr = ciss_dequeue_complete(sc)) == NULL) 1903 break; 1904 ciss_unmap_request(cr); 1905 1906 /* 1907 * If the request has a callback, invoke it. 1908 */ 1909 if (cr->cr_complete != NULL) { 1910 cr->cr_complete(cr); 1911 continue; 1912 } 1913 1914 /* 1915 * If someone is sleeping on this request, wake them up. 1916 */ 1917 if (cr->cr_flags & CISS_REQ_SLEEP) { 1918 cr->cr_flags &= ~CISS_REQ_SLEEP; 1919 wakeup(cr); 1920 continue; 1921 } 1922 1923 /* 1924 * If someone is polling this request for completion, signal. 1925 */ 1926 if (cr->cr_flags & CISS_REQ_POLL) { 1927 cr->cr_flags &= ~CISS_REQ_POLL; 1928 continue; 1929 } 1930 1931 /* 1932 * Give up and throw the request back on the free queue. This 1933 * should never happen; resources will probably be lost. 1934 */ 1935 ciss_printf(sc, "WARNING: completed command with no submitter\n"); 1936 ciss_enqueue_free(cr); 1937 } 1938 } 1939 1940 /************************************************************************ 1941 * Report on the completion status of a request, and pass back SCSI 1942 * and command status values. 1943 */ 1944 static int 1945 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status) 1946 { 1947 struct ciss_command *cc; 1948 struct ciss_error_info *ce; 1949 1950 debug_called(2); 1951 1952 cc = CISS_FIND_COMMAND(cr); 1953 ce = (struct ciss_error_info *)&(cc->sg[0]); 1954 1955 /* 1956 * We don't consider data under/overrun an error for the Report 1957 * Logical/Physical LUNs commands. 1958 */ 1959 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) && 1960 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) || 1961 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) && 1962 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) || 1963 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) || 1964 (cc->cdb.cdb[0] == INQUIRY))) { 1965 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR; 1966 debug(2, "ignoring irrelevant under/overrun error"); 1967 } 1968 1969 /* 1970 * Check the command's error bit, if clear, there's no status and 1971 * everything is OK. 1972 */ 1973 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) { 1974 if (scsi_status != NULL) 1975 *scsi_status = SCSI_STATUS_OK; 1976 if (command_status != NULL) 1977 *command_status = CISS_CMD_STATUS_SUCCESS; 1978 return(0); 1979 } else { 1980 if (command_status != NULL) 1981 *command_status = ce->command_status; 1982 if (scsi_status != NULL) { 1983 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) { 1984 *scsi_status = ce->scsi_status; 1985 } else { 1986 *scsi_status = -1; 1987 } 1988 } 1989 if (bootverbose) 1990 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n", 1991 ce->command_status, ciss_name_command_status(ce->command_status), 1992 ce->scsi_status); 1993 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) { 1994 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n", 1995 ce->additional_error_info.invalid_command.offense_size, 1996 ce->additional_error_info.invalid_command.offense_offset, 1997 ce->additional_error_info.invalid_command.offense_value); 1998 } 1999 } 2000 #if 0 2001 ciss_print_request(cr); 2002 #endif 2003 return(1); 2004 } 2005 2006 /************************************************************************ 2007 * Issue a request and don't return until it's completed. 2008 * 2009 * Depending on adapter status, we may poll or sleep waiting for 2010 * completion. 2011 */ 2012 static int 2013 ciss_synch_request(struct ciss_request *cr, int timeout) 2014 { 2015 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) { 2016 return(ciss_wait_request(cr, timeout)); 2017 } else { 2018 return(ciss_poll_request(cr, timeout)); 2019 } 2020 } 2021 2022 /************************************************************************ 2023 * Issue a request and poll for completion. 2024 * 2025 * Timeout in milliseconds. 2026 */ 2027 static int 2028 ciss_poll_request(struct ciss_request *cr, int timeout) 2029 { 2030 int error; 2031 2032 debug_called(2); 2033 2034 cr->cr_flags |= CISS_REQ_POLL; 2035 if ((error = ciss_start(cr)) != 0) 2036 return(error); 2037 2038 do { 2039 ciss_done(cr->cr_sc); 2040 if (!(cr->cr_flags & CISS_REQ_POLL)) 2041 return(0); 2042 DELAY(1000); 2043 } while (timeout-- >= 0); 2044 return(EWOULDBLOCK); 2045 } 2046 2047 /************************************************************************ 2048 * Issue a request and sleep waiting for completion. 2049 * 2050 * Timeout in milliseconds. Note that a spurious wakeup will reset 2051 * the timeout. 2052 */ 2053 static int 2054 ciss_wait_request(struct ciss_request *cr, int timeout) 2055 { 2056 int s, error; 2057 2058 debug_called(2); 2059 2060 cr->cr_flags |= CISS_REQ_SLEEP; 2061 if ((error = ciss_start(cr)) != 0) 2062 return(error); 2063 2064 s = splcam(); 2065 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) { 2066 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000); 2067 } 2068 splx(s); 2069 return(error); 2070 } 2071 2072 #if 0 2073 /************************************************************************ 2074 * Abort a request. Note that a potential exists here to race the 2075 * request being completed; the caller must deal with this. 2076 */ 2077 static int 2078 ciss_abort_request(struct ciss_request *ar) 2079 { 2080 struct ciss_request *cr; 2081 struct ciss_command *cc; 2082 struct ciss_message_cdb *cmc; 2083 int error; 2084 2085 debug_called(1); 2086 2087 /* get a request */ 2088 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0) 2089 return(error); 2090 2091 /* build the abort command */ 2092 cc = CISS_FIND_COMMAND(cr); 2093 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */ 2094 cc->header.address.physical.target = 0; 2095 cc->header.address.physical.bus = 0; 2096 cc->cdb.cdb_length = sizeof(*cmc); 2097 cc->cdb.type = CISS_CDB_TYPE_MESSAGE; 2098 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 2099 cc->cdb.direction = CISS_CDB_DIRECTION_NONE; 2100 cc->cdb.timeout = 30; 2101 2102 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]); 2103 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT; 2104 cmc->type = CISS_MESSAGE_ABORT_TASK; 2105 cmc->abort_tag = ar->cr_tag; /* endianness?? */ 2106 2107 /* 2108 * Send the request and wait for a response. If we believe we 2109 * aborted the request OK, clear the flag that indicates it's 2110 * running. 2111 */ 2112 error = ciss_synch_request(cr, 35 * 1000); 2113 if (!error) 2114 error = ciss_report_request(cr, NULL, NULL); 2115 ciss_release_request(cr); 2116 2117 return(error); 2118 } 2119 #endif 2120 2121 2122 /************************************************************************ 2123 * Fetch and initialise a request 2124 */ 2125 static int 2126 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp) 2127 { 2128 struct ciss_request *cr; 2129 2130 debug_called(2); 2131 2132 /* 2133 * Get a request and clean it up. 2134 */ 2135 if ((cr = ciss_dequeue_free(sc)) == NULL) 2136 return(ENOMEM); 2137 2138 cr->cr_data = NULL; 2139 cr->cr_flags = 0; 2140 cr->cr_complete = NULL; 2141 cr->cr_private = NULL; 2142 2143 ciss_preen_command(cr); 2144 *crp = cr; 2145 return(0); 2146 } 2147 2148 static void 2149 ciss_preen_command(struct ciss_request *cr) 2150 { 2151 struct ciss_command *cc; 2152 u_int32_t cmdphys; 2153 2154 /* 2155 * Clean up the command structure. 2156 * 2157 * Note that we set up the error_info structure here, since the 2158 * length can be overwritten by any command. 2159 */ 2160 cc = CISS_FIND_COMMAND(cr); 2161 cc->header.sg_in_list = 0; /* kinda inefficient this way */ 2162 cc->header.sg_total = 0; 2163 cc->header.host_tag = cr->cr_tag << 2; 2164 cc->header.host_tag_zeroes = 0; 2165 cmdphys = CISS_FIND_COMMANDPHYS(cr); 2166 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command); 2167 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command); 2168 } 2169 2170 /************************************************************************ 2171 * Release a request to the free list. 2172 */ 2173 static void 2174 ciss_release_request(struct ciss_request *cr) 2175 { 2176 struct ciss_softc *sc; 2177 2178 debug_called(2); 2179 2180 sc = cr->cr_sc; 2181 2182 /* release the request to the free queue */ 2183 ciss_requeue_free(cr); 2184 } 2185 2186 /************************************************************************ 2187 * Allocate a request that will be used to send a BMIC command. Do some 2188 * of the common setup here to avoid duplicating it everywhere else. 2189 */ 2190 static int 2191 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp, 2192 int opcode, void **bufp, size_t bufsize) 2193 { 2194 struct ciss_request *cr; 2195 struct ciss_command *cc; 2196 struct ciss_bmic_cdb *cbc; 2197 void *buf; 2198 int error; 2199 int dataout; 2200 2201 debug_called(2); 2202 2203 cr = NULL; 2204 buf = NULL; 2205 2206 /* 2207 * Get a request. 2208 */ 2209 if ((error = ciss_get_request(sc, &cr)) != 0) 2210 goto out; 2211 2212 /* 2213 * Allocate data storage if requested, determine the data direction. 2214 */ 2215 dataout = 0; 2216 if ((bufsize > 0) && (bufp != NULL)) { 2217 if (*bufp == NULL) { 2218 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) { 2219 error = ENOMEM; 2220 goto out; 2221 } 2222 } else { 2223 buf = *bufp; 2224 dataout = 1; /* we are given a buffer, so we are writing */ 2225 } 2226 } 2227 2228 /* 2229 * Build a CISS BMIC command to get the logical drive ID. 2230 */ 2231 cr->cr_data = buf; 2232 cr->cr_length = bufsize; 2233 if (!dataout) 2234 cr->cr_flags = CISS_REQ_DATAIN; 2235 2236 cc = CISS_FIND_COMMAND(cr); 2237 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; 2238 cc->header.address.physical.bus = 0; 2239 cc->header.address.physical.target = 0; 2240 cc->cdb.cdb_length = sizeof(*cbc); 2241 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 2242 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 2243 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ; 2244 cc->cdb.timeout = 0; 2245 2246 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]); 2247 bzero(cbc, sizeof(*cbc)); 2248 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ; 2249 cbc->bmic_opcode = opcode; 2250 cbc->size = htons((u_int16_t)bufsize); 2251 2252 out: 2253 if (error) { 2254 if (cr != NULL) 2255 ciss_release_request(cr); 2256 } else { 2257 *crp = cr; 2258 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL)) 2259 *bufp = buf; 2260 } 2261 return(error); 2262 } 2263 2264 /************************************************************************ 2265 * Handle a command passed in from userspace. 2266 */ 2267 static int 2268 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc) 2269 { 2270 struct ciss_request *cr; 2271 struct ciss_command *cc; 2272 struct ciss_error_info *ce; 2273 int error = 0; 2274 2275 debug_called(1); 2276 2277 cr = NULL; 2278 2279 /* 2280 * Get a request. 2281 */ 2282 while (ciss_get_request(sc, &cr) != 0) 2283 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz); 2284 cc = CISS_FIND_COMMAND(cr); 2285 2286 /* 2287 * Allocate an in-kernel databuffer if required, copy in user data. 2288 */ 2289 cr->cr_length = ioc->buf_size; 2290 if (ioc->buf_size > 0) { 2291 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) { 2292 error = ENOMEM; 2293 goto out; 2294 } 2295 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) { 2296 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size); 2297 goto out; 2298 } 2299 } 2300 2301 /* 2302 * Build the request based on the user command. 2303 */ 2304 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address)); 2305 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb)); 2306 2307 /* XXX anything else to populate here? */ 2308 2309 /* 2310 * Run the command. 2311 */ 2312 if ((error = ciss_synch_request(cr, 60 * 1000))) { 2313 debug(0, "request failed - %d", error); 2314 goto out; 2315 } 2316 2317 /* 2318 * Check to see if the command succeeded. 2319 */ 2320 ce = (struct ciss_error_info *)&(cc->sg[0]); 2321 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0) 2322 bzero(ce, sizeof(*ce)); 2323 2324 /* 2325 * Copy the results back to the user. 2326 */ 2327 bcopy(ce, &ioc->error_info, sizeof(*ce)); 2328 if ((ioc->buf_size > 0) && 2329 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) { 2330 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size); 2331 goto out; 2332 } 2333 2334 /* done OK */ 2335 error = 0; 2336 2337 out: 2338 if ((cr != NULL) && (cr->cr_data != NULL)) 2339 free(cr->cr_data, CISS_MALLOC_CLASS); 2340 if (cr != NULL) 2341 ciss_release_request(cr); 2342 return(error); 2343 } 2344 2345 /************************************************************************ 2346 * Map a request into bus-visible space, initialise the scatter/gather 2347 * list. 2348 */ 2349 static int 2350 ciss_map_request(struct ciss_request *cr) 2351 { 2352 struct ciss_softc *sc; 2353 int error = 0; 2354 2355 debug_called(2); 2356 2357 sc = cr->cr_sc; 2358 2359 /* check that mapping is necessary */ 2360 if (cr->cr_flags & CISS_REQ_MAPPED) 2361 return(0); 2362 2363 cr->cr_flags |= CISS_REQ_MAPPED; 2364 2365 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map, 2366 BUS_DMASYNC_PREWRITE); 2367 2368 if (cr->cr_data != NULL) { 2369 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap, 2370 cr->cr_data, cr->cr_length, 2371 ciss_request_map_helper, cr, 0); 2372 if (error != 0) 2373 return (error); 2374 } else { 2375 /* 2376 * Post the command to the adapter. 2377 */ 2378 ciss_enqueue_busy(cr); 2379 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr)); 2380 } 2381 2382 return(0); 2383 } 2384 2385 static void 2386 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2387 { 2388 struct ciss_command *cc; 2389 struct ciss_request *cr; 2390 struct ciss_softc *sc; 2391 int i; 2392 2393 debug_called(2); 2394 2395 cr = (struct ciss_request *)arg; 2396 sc = cr->cr_sc; 2397 cc = CISS_FIND_COMMAND(cr); 2398 2399 for (i = 0; i < nseg; i++) { 2400 cc->sg[i].address = segs[i].ds_addr; 2401 cc->sg[i].length = segs[i].ds_len; 2402 cc->sg[i].extension = 0; 2403 } 2404 /* we leave the s/g table entirely within the command */ 2405 cc->header.sg_in_list = nseg; 2406 cc->header.sg_total = nseg; 2407 2408 if (cr->cr_flags & CISS_REQ_DATAIN) 2409 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD); 2410 if (cr->cr_flags & CISS_REQ_DATAOUT) 2411 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE); 2412 2413 /* 2414 * Post the command to the adapter. 2415 */ 2416 ciss_enqueue_busy(cr); 2417 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr)); 2418 } 2419 2420 /************************************************************************ 2421 * Unmap a request from bus-visible space. 2422 */ 2423 static void 2424 ciss_unmap_request(struct ciss_request *cr) 2425 { 2426 struct ciss_softc *sc; 2427 2428 debug_called(2); 2429 2430 sc = cr->cr_sc; 2431 2432 /* check that unmapping is necessary */ 2433 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0) 2434 return; 2435 2436 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map, 2437 BUS_DMASYNC_POSTWRITE); 2438 2439 if (cr->cr_data == NULL) 2440 goto out; 2441 2442 if (cr->cr_flags & CISS_REQ_DATAIN) 2443 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD); 2444 if (cr->cr_flags & CISS_REQ_DATAOUT) 2445 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE); 2446 2447 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap); 2448 out: 2449 cr->cr_flags &= ~CISS_REQ_MAPPED; 2450 } 2451 2452 /************************************************************************ 2453 * Attach the driver to CAM. 2454 * 2455 * We put all the logical drives on a single SCSI bus. 2456 */ 2457 static int 2458 ciss_cam_init(struct ciss_softc *sc) 2459 { 2460 int i, maxbus; 2461 2462 debug_called(1); 2463 2464 /* 2465 * Allocate a devq. We can reuse this for the masked physical 2466 * devices if we decide to export these as well. 2467 */ 2468 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) { 2469 ciss_printf(sc, "can't allocate CAM SIM queue\n"); 2470 return(ENOMEM); 2471 } 2472 2473 /* 2474 * Create a SIM. 2475 * 2476 * This naturally wastes a bit of memory. The alternative is to allocate 2477 * and register each bus as it is found, and then track them on a linked 2478 * list. Unfortunately, the driver has a few places where it needs to 2479 * look up the SIM based solely on bus number, and it's unclear whether 2480 * a list traversal would work for these situations. 2481 */ 2482 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus + 2483 CISS_PHYSICAL_BASE); 2484 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*), 2485 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO); 2486 if (sc->ciss_cam_sim == NULL) { 2487 ciss_printf(sc, "can't allocate memory for controller SIM\n"); 2488 return(ENOMEM); 2489 } 2490 2491 for (i = 0; i < sc->ciss_max_logical_bus; i++) { 2492 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, 2493 "ciss", sc, 2494 device_get_unit(sc->ciss_dev), 2495 &sc->ciss_mtx, 2496 sc->ciss_max_requests - 2, 2497 sc->ciss_max_requests - 2, 2498 sc->ciss_cam_devq)) == NULL) { 2499 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i); 2500 return(ENOMEM); 2501 } 2502 2503 /* 2504 * Register bus with this SIM. 2505 */ 2506 mtx_lock(&sc->ciss_mtx); 2507 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) { 2508 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) { 2509 ciss_printf(sc, "can't register SCSI bus %d\n", i); 2510 mtx_unlock(&sc->ciss_mtx); 2511 return (ENXIO); 2512 } 2513 } 2514 mtx_unlock(&sc->ciss_mtx); 2515 } 2516 2517 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus + 2518 CISS_PHYSICAL_BASE; i++) { 2519 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, 2520 "ciss", sc, 2521 device_get_unit(sc->ciss_dev), 2522 &sc->ciss_mtx, 1, 2523 sc->ciss_max_requests - 2, 2524 sc->ciss_cam_devq)) == NULL) { 2525 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i); 2526 return (ENOMEM); 2527 } 2528 2529 mtx_lock(&sc->ciss_mtx); 2530 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) { 2531 ciss_printf(sc, "can't register SCSI bus %d\n", i); 2532 mtx_unlock(&sc->ciss_mtx); 2533 return (ENXIO); 2534 } 2535 mtx_unlock(&sc->ciss_mtx); 2536 } 2537 2538 /* 2539 * Initiate a rescan of the bus. 2540 */ 2541 mtx_lock(&sc->ciss_mtx); 2542 ciss_cam_rescan_all(sc); 2543 mtx_unlock(&sc->ciss_mtx); 2544 2545 return(0); 2546 } 2547 2548 /************************************************************************ 2549 * Initiate a rescan of the 'logical devices' SIM 2550 */ 2551 static void 2552 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target) 2553 { 2554 struct cam_path *path; 2555 union ccb *ccb; 2556 2557 debug_called(1); 2558 2559 if ((ccb = malloc(sizeof(union ccb), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) { 2560 ciss_printf(sc, "rescan failed (can't allocate CCB)\n"); 2561 return; 2562 } 2563 2564 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->ciss_cam_sim[bus]), 2565 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 2566 ciss_printf(sc, "rescan failed (can't create path)\n"); 2567 free(ccb, CISS_MALLOC_CLASS); 2568 return; 2569 } 2570 2571 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/); 2572 ccb->ccb_h.func_code = XPT_SCAN_BUS; 2573 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback; 2574 ccb->crcn.flags = CAM_FLAG_NONE; 2575 xpt_action(ccb); 2576 2577 /* scan is now in progress */ 2578 } 2579 2580 static void 2581 ciss_cam_rescan_all(struct ciss_softc *sc) 2582 { 2583 int i; 2584 2585 /* Rescan the logical buses */ 2586 for (i = 0; i < sc->ciss_max_logical_bus; i++) 2587 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD); 2588 /* Rescan the physical buses */ 2589 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus + 2590 CISS_PHYSICAL_BASE; i++) 2591 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD); 2592 } 2593 2594 static void 2595 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 2596 { 2597 xpt_free_path(ccb->ccb_h.path); 2598 free(ccb, CISS_MALLOC_CLASS); 2599 } 2600 2601 /************************************************************************ 2602 * Handle requests coming from CAM 2603 */ 2604 static void 2605 ciss_cam_action(struct cam_sim *sim, union ccb *ccb) 2606 { 2607 struct ciss_softc *sc; 2608 struct ccb_scsiio *csio; 2609 int bus, target; 2610 int physical; 2611 2612 sc = cam_sim_softc(sim); 2613 bus = cam_sim_bus(sim); 2614 csio = (struct ccb_scsiio *)&ccb->csio; 2615 target = csio->ccb_h.target_id; 2616 physical = CISS_IS_PHYSICAL(bus); 2617 2618 switch (ccb->ccb_h.func_code) { 2619 2620 /* perform SCSI I/O */ 2621 case XPT_SCSI_IO: 2622 if (!ciss_cam_action_io(sim, csio)) 2623 return; 2624 break; 2625 2626 /* perform geometry calculations */ 2627 case XPT_CALC_GEOMETRY: 2628 { 2629 struct ccb_calc_geometry *ccg = &ccb->ccg; 2630 struct ciss_ldrive *ld; 2631 2632 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2633 2634 ld = NULL; 2635 if (!physical) 2636 ld = &sc->ciss_logical[bus][target]; 2637 2638 /* 2639 * Use the cached geometry settings unless the fault tolerance 2640 * is invalid. 2641 */ 2642 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) { 2643 u_int32_t secs_per_cylinder; 2644 2645 ccg->heads = 255; 2646 ccg->secs_per_track = 32; 2647 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2648 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2649 } else { 2650 ccg->heads = ld->cl_geometry.heads; 2651 ccg->secs_per_track = ld->cl_geometry.sectors; 2652 ccg->cylinders = ntohs(ld->cl_geometry.cylinders); 2653 } 2654 ccb->ccb_h.status = CAM_REQ_CMP; 2655 break; 2656 } 2657 2658 /* handle path attribute inquiry */ 2659 case XPT_PATH_INQ: 2660 { 2661 struct ccb_pathinq *cpi = &ccb->cpi; 2662 2663 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2664 2665 cpi->version_num = 1; 2666 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */ 2667 cpi->target_sprt = 0; 2668 cpi->hba_misc = 0; 2669 cpi->max_target = CISS_MAX_LOGICAL; 2670 cpi->max_lun = 0; /* 'logical drive' channel only */ 2671 cpi->initiator_id = CISS_MAX_LOGICAL; 2672 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2673 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN); 2674 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2675 cpi->unit_number = cam_sim_unit(sim); 2676 cpi->bus_id = cam_sim_bus(sim); 2677 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */ 2678 cpi->transport = XPORT_SPI; 2679 cpi->transport_version = 2; 2680 cpi->protocol = PROTO_SCSI; 2681 cpi->protocol_version = SCSI_REV_2; 2682 ccb->ccb_h.status = CAM_REQ_CMP; 2683 break; 2684 } 2685 2686 case XPT_GET_TRAN_SETTINGS: 2687 { 2688 struct ccb_trans_settings *cts = &ccb->cts; 2689 int bus, target; 2690 struct ccb_trans_settings_spi *spi = 2691 &cts->xport_specific.spi; 2692 2693 bus = cam_sim_bus(sim); 2694 target = cts->ccb_h.target_id; 2695 2696 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target); 2697 /* disconnect always OK */ 2698 cts->protocol = PROTO_SCSI; 2699 cts->protocol_version = SCSI_REV_2; 2700 cts->transport = XPORT_SPI; 2701 cts->transport_version = 2; 2702 2703 spi->valid = CTS_SPI_VALID_DISC; 2704 spi->flags = CTS_SPI_FLAGS_DISC_ENB; 2705 2706 cts->ccb_h.status = CAM_REQ_CMP; 2707 break; 2708 } 2709 2710 default: /* we can't do this */ 2711 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code); 2712 ccb->ccb_h.status = CAM_REQ_INVALID; 2713 break; 2714 } 2715 2716 xpt_done(ccb); 2717 } 2718 2719 /************************************************************************ 2720 * Handle a CAM SCSI I/O request. 2721 */ 2722 static int 2723 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio) 2724 { 2725 struct ciss_softc *sc; 2726 int bus, target; 2727 struct ciss_request *cr; 2728 struct ciss_command *cc; 2729 int error; 2730 2731 sc = cam_sim_softc(sim); 2732 bus = cam_sim_bus(sim); 2733 target = csio->ccb_h.target_id; 2734 2735 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun); 2736 2737 /* firmware does not support commands > 10 bytes */ 2738 if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) { 2739 debug(3, " command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE); 2740 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2741 } 2742 2743 /* check that the CDB pointer is not to a physical address */ 2744 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) { 2745 debug(3, " CDB pointer is to physical address"); 2746 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2747 } 2748 2749 /* if there is data transfer, it must be to/from a virtual address */ 2750 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 2751 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */ 2752 debug(3, " data pointer is to physical address"); 2753 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2754 } 2755 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */ 2756 debug(3, " data has premature s/g setup"); 2757 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2758 } 2759 } 2760 2761 /* abandon aborted ccbs or those that have failed validation */ 2762 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 2763 debug(3, "abandoning CCB due to abort/validation failure"); 2764 return(EINVAL); 2765 } 2766 2767 /* handle emulation of some SCSI commands ourself */ 2768 if (ciss_cam_emulate(sc, csio)) 2769 return(0); 2770 2771 /* 2772 * Get a request to manage this command. If we can't, return the 2773 * ccb, freeze the queue and flag so that we unfreeze it when a 2774 * request completes. 2775 */ 2776 if ((error = ciss_get_request(sc, &cr)) != 0) { 2777 xpt_freeze_simq(sim, 1); 2778 csio->ccb_h.status |= CAM_REQUEUE_REQ; 2779 return(error); 2780 } 2781 2782 /* 2783 * Build the command. 2784 */ 2785 cc = CISS_FIND_COMMAND(cr); 2786 cr->cr_data = csio->data_ptr; 2787 cr->cr_length = csio->dxfer_len; 2788 cr->cr_complete = ciss_cam_complete; 2789 cr->cr_private = csio; 2790 2791 /* 2792 * Target the right logical volume. 2793 */ 2794 if (CISS_IS_PHYSICAL(bus)) 2795 cc->header.address = 2796 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address; 2797 else 2798 cc->header.address = 2799 sc->ciss_logical[bus][target].cl_address; 2800 cc->cdb.cdb_length = csio->cdb_len; 2801 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 2802 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */ 2803 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 2804 cr->cr_flags = CISS_REQ_DATAOUT; 2805 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE; 2806 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 2807 cr->cr_flags = CISS_REQ_DATAIN; 2808 cc->cdb.direction = CISS_CDB_DIRECTION_READ; 2809 } else { 2810 cr->cr_flags = 0; 2811 cc->cdb.direction = CISS_CDB_DIRECTION_NONE; 2812 } 2813 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1; 2814 if (csio->ccb_h.flags & CAM_CDB_POINTER) { 2815 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len); 2816 } else { 2817 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len); 2818 } 2819 2820 /* 2821 * Submit the request to the adapter. 2822 * 2823 * Note that this may fail if we're unable to map the request (and 2824 * if we ever learn a transport layer other than simple, may fail 2825 * if the adapter rejects the command). 2826 */ 2827 if ((error = ciss_start(cr)) != 0) { 2828 xpt_freeze_simq(sim, 1); 2829 if (error == EINPROGRESS) { 2830 csio->ccb_h.status |= CAM_RELEASE_SIMQ; 2831 error = 0; 2832 } else { 2833 csio->ccb_h.status |= CAM_REQUEUE_REQ; 2834 ciss_release_request(cr); 2835 } 2836 return(error); 2837 } 2838 2839 return(0); 2840 } 2841 2842 /************************************************************************ 2843 * Emulate SCSI commands the adapter doesn't handle as we might like. 2844 */ 2845 static int 2846 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio) 2847 { 2848 int bus, target; 2849 u_int8_t opcode; 2850 2851 target = csio->ccb_h.target_id; 2852 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path)); 2853 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ? 2854 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]; 2855 2856 if (CISS_IS_PHYSICAL(bus)) { 2857 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) { 2858 csio->ccb_h.status = CAM_SEL_TIMEOUT; 2859 xpt_done((union ccb *)csio); 2860 return(1); 2861 } else 2862 return(0); 2863 } 2864 2865 /* 2866 * Handle requests for volumes that don't exist or are not online. 2867 * A selection timeout is slightly better than an illegal request. 2868 * Other errors might be better. 2869 */ 2870 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) { 2871 csio->ccb_h.status = CAM_SEL_TIMEOUT; 2872 xpt_done((union ccb *)csio); 2873 return(1); 2874 } 2875 2876 /* if we have to fake Synchronise Cache */ 2877 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) { 2878 /* 2879 * If this is a Synchronise Cache command, typically issued when 2880 * a device is closed, flush the adapter and complete now. 2881 */ 2882 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ? 2883 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) { 2884 ciss_flush_adapter(sc); 2885 csio->ccb_h.status = CAM_REQ_CMP; 2886 xpt_done((union ccb *)csio); 2887 return(1); 2888 } 2889 } 2890 2891 return(0); 2892 } 2893 2894 /************************************************************************ 2895 * Check for possibly-completed commands. 2896 */ 2897 static void 2898 ciss_cam_poll(struct cam_sim *sim) 2899 { 2900 struct ciss_softc *sc = cam_sim_softc(sim); 2901 2902 debug_called(2); 2903 2904 ciss_done(sc); 2905 } 2906 2907 /************************************************************************ 2908 * Handle completion of a command - pass results back through the CCB 2909 */ 2910 static void 2911 ciss_cam_complete(struct ciss_request *cr) 2912 { 2913 struct ciss_softc *sc; 2914 struct ciss_command *cc; 2915 struct ciss_error_info *ce; 2916 struct ccb_scsiio *csio; 2917 int scsi_status; 2918 int command_status; 2919 2920 debug_called(2); 2921 2922 sc = cr->cr_sc; 2923 cc = CISS_FIND_COMMAND(cr); 2924 ce = (struct ciss_error_info *)&(cc->sg[0]); 2925 csio = (struct ccb_scsiio *)cr->cr_private; 2926 2927 /* 2928 * Extract status values from request. 2929 */ 2930 ciss_report_request(cr, &command_status, &scsi_status); 2931 csio->scsi_status = scsi_status; 2932 2933 /* 2934 * Handle specific SCSI status values. 2935 */ 2936 switch(scsi_status) { 2937 /* no status due to adapter error */ 2938 case -1: 2939 debug(0, "adapter error"); 2940 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2941 break; 2942 2943 /* no status due to command completed OK */ 2944 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */ 2945 debug(2, "SCSI_STATUS_OK"); 2946 csio->ccb_h.status = CAM_REQ_CMP; 2947 break; 2948 2949 /* check condition, sense data included */ 2950 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */ 2951 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n", 2952 ce->sense_length, ce->residual_count); 2953 bzero(&csio->sense_data, SSD_FULL_SIZE); 2954 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length); 2955 csio->sense_len = ce->sense_length; 2956 csio->resid = ce->residual_count; 2957 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 2958 #ifdef CISS_DEBUG 2959 { 2960 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0]; 2961 debug(0, "sense key %x", sns->flags & SSD_KEY); 2962 } 2963 #endif 2964 break; 2965 2966 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */ 2967 debug(0, "SCSI_STATUS_BUSY"); 2968 csio->ccb_h.status = CAM_SCSI_BUSY; 2969 break; 2970 2971 default: 2972 debug(0, "unknown status 0x%x", csio->scsi_status); 2973 csio->ccb_h.status = CAM_REQ_CMP_ERR; 2974 break; 2975 } 2976 2977 /* handle post-command fixup */ 2978 ciss_cam_complete_fixup(sc, csio); 2979 2980 /* tell CAM we're ready for more commands */ 2981 csio->ccb_h.status |= CAM_RELEASE_SIMQ; 2982 2983 xpt_done((union ccb *)csio); 2984 ciss_release_request(cr); 2985 } 2986 2987 /******************************************************************************** 2988 * Fix up the result of some commands here. 2989 */ 2990 static void 2991 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio) 2992 { 2993 struct scsi_inquiry_data *inq; 2994 struct ciss_ldrive *cl; 2995 int bus, target; 2996 2997 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ? 2998 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) { 2999 3000 inq = (struct scsi_inquiry_data *)csio->data_ptr; 3001 target = csio->ccb_h.target_id; 3002 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path)); 3003 3004 /* 3005 * Don't let hard drives be seen by the DA driver. They will still be 3006 * attached by the PASS driver. 3007 */ 3008 if (CISS_IS_PHYSICAL(bus)) { 3009 if (SID_TYPE(inq) == T_DIRECT) 3010 inq->device = (inq->device & 0xe0) | T_NODEVICE; 3011 return; 3012 } 3013 3014 cl = &sc->ciss_logical[bus][target]; 3015 3016 padstr(inq->vendor, "COMPAQ", 8); 3017 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8); 3018 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16); 3019 } 3020 } 3021 3022 3023 /******************************************************************************** 3024 * Find a peripheral attached at (target) 3025 */ 3026 static struct cam_periph * 3027 ciss_find_periph(struct ciss_softc *sc, int bus, int target) 3028 { 3029 struct cam_periph *periph; 3030 struct cam_path *path; 3031 int status; 3032 3033 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]), 3034 target, 0); 3035 if (status == CAM_REQ_CMP) { 3036 periph = cam_periph_find(path, NULL); 3037 xpt_free_path(path); 3038 } else { 3039 periph = NULL; 3040 } 3041 return(periph); 3042 } 3043 3044 /******************************************************************************** 3045 * Name the device at (target) 3046 * 3047 * XXX is this strictly correct? 3048 */ 3049 static int 3050 ciss_name_device(struct ciss_softc *sc, int bus, int target) 3051 { 3052 struct cam_periph *periph; 3053 3054 if (CISS_IS_PHYSICAL(bus)) 3055 return (0); 3056 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) { 3057 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d", 3058 periph->periph_name, periph->unit_number); 3059 return(0); 3060 } 3061 sc->ciss_logical[bus][target].cl_name[0] = 0; 3062 return(ENOENT); 3063 } 3064 3065 /************************************************************************ 3066 * Periodic status monitoring. 3067 */ 3068 static void 3069 ciss_periodic(void *arg) 3070 { 3071 struct ciss_softc *sc; 3072 struct ciss_request *cr = NULL; 3073 struct ciss_command *cc = NULL; 3074 int error = 0; 3075 3076 debug_called(1); 3077 3078 sc = (struct ciss_softc *)arg; 3079 3080 /* 3081 * Check the adapter heartbeat. 3082 */ 3083 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) { 3084 sc->ciss_heart_attack++; 3085 debug(0, "adapter heart attack in progress 0x%x/%d", 3086 sc->ciss_heartbeat, sc->ciss_heart_attack); 3087 if (sc->ciss_heart_attack == 3) { 3088 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n"); 3089 ciss_disable_adapter(sc); 3090 return; 3091 } 3092 } else { 3093 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat; 3094 sc->ciss_heart_attack = 0; 3095 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat); 3096 } 3097 3098 /* 3099 * Send the NOP message and wait for a response. 3100 */ 3101 if ((error = ciss_get_request(sc, &cr)) == 0) { 3102 cc = CISS_FIND_COMMAND(cr); 3103 cc->cdb.cdb_length = 1; 3104 cc->cdb.type = CISS_CDB_TYPE_MESSAGE; 3105 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 3106 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE; 3107 cc->cdb.timeout = 0; 3108 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP; 3109 3110 if ((error = ciss_synch_request(cr, 10 * 1000)) != 0) { 3111 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n"); 3112 } 3113 3114 ciss_release_request(cr); 3115 } 3116 3117 /* 3118 * If the notify event request has died for some reason, or has 3119 * not started yet, restart it. 3120 */ 3121 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) { 3122 debug(0, "(re)starting Event Notify chain"); 3123 ciss_notify_event(sc); 3124 } 3125 3126 /* 3127 * Reschedule. 3128 */ 3129 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc); 3130 } 3131 3132 /************************************************************************ 3133 * Disable the adapter. 3134 * 3135 * The all requests in completed queue is failed with hardware error. 3136 * This will cause failover in a multipath configuration. 3137 */ 3138 static void 3139 ciss_disable_adapter(struct ciss_softc *sc) 3140 { 3141 struct ciss_request *cr; 3142 struct ciss_command *cc; 3143 struct ciss_error_info *ce; 3144 int s; 3145 3146 s = splcam(); 3147 3148 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc); 3149 pci_disable_busmaster(sc->ciss_dev); 3150 sc->ciss_flags &= ~CISS_FLAG_RUNNING; 3151 3152 for (;;) { 3153 if ((cr = ciss_dequeue_busy(sc)) == NULL) 3154 break; 3155 3156 cc = CISS_FIND_COMMAND(cr); 3157 ce = (struct ciss_error_info *)&(cc->sg[0]); 3158 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR; 3159 ciss_enqueue_complete(cr); 3160 } 3161 3162 for (;;) { 3163 if ((cr = ciss_dequeue_complete(sc)) == NULL) 3164 break; 3165 3166 /* 3167 * If the request has a callback, invoke it. 3168 */ 3169 if (cr->cr_complete != NULL) { 3170 cr->cr_complete(cr); 3171 continue; 3172 } 3173 3174 /* 3175 * If someone is sleeping on this request, wake them up. 3176 */ 3177 if (cr->cr_flags & CISS_REQ_SLEEP) { 3178 cr->cr_flags &= ~CISS_REQ_SLEEP; 3179 wakeup(cr); 3180 continue; 3181 } 3182 } 3183 3184 splx(s); 3185 } 3186 3187 /************************************************************************ 3188 * Request a notification response from the adapter. 3189 * 3190 * If (cr) is NULL, this is the first request of the adapter, so 3191 * reset the adapter's message pointer and start with the oldest 3192 * message available. 3193 */ 3194 static void 3195 ciss_notify_event(struct ciss_softc *sc) 3196 { 3197 struct ciss_request *cr; 3198 struct ciss_command *cc; 3199 struct ciss_notify_cdb *cnc; 3200 int error; 3201 3202 debug_called(1); 3203 3204 cr = sc->ciss_periodic_notify; 3205 3206 /* get a request if we don't already have one */ 3207 if (cr == NULL) { 3208 if ((error = ciss_get_request(sc, &cr)) != 0) { 3209 debug(0, "can't get notify event request"); 3210 goto out; 3211 } 3212 sc->ciss_periodic_notify = cr; 3213 cr->cr_complete = ciss_notify_complete; 3214 debug(1, "acquired request %d", cr->cr_tag); 3215 } 3216 3217 /* 3218 * Get a databuffer if we don't already have one, note that the 3219 * adapter command wants a larger buffer than the actual 3220 * structure. 3221 */ 3222 if (cr->cr_data == NULL) { 3223 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) { 3224 debug(0, "can't get notify event request buffer"); 3225 error = ENOMEM; 3226 goto out; 3227 } 3228 cr->cr_length = CISS_NOTIFY_DATA_SIZE; 3229 } 3230 3231 /* re-setup the request's command (since we never release it) XXX overkill*/ 3232 ciss_preen_command(cr); 3233 3234 /* (re)build the notify event command */ 3235 cc = CISS_FIND_COMMAND(cr); 3236 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; 3237 cc->header.address.physical.bus = 0; 3238 cc->header.address.physical.target = 0; 3239 3240 cc->cdb.cdb_length = sizeof(*cnc); 3241 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 3242 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 3243 cc->cdb.direction = CISS_CDB_DIRECTION_READ; 3244 cc->cdb.timeout = 0; /* no timeout, we hope */ 3245 3246 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]); 3247 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE); 3248 cnc->opcode = CISS_OPCODE_READ; 3249 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT; 3250 cnc->timeout = 0; /* no timeout, we hope */ 3251 cnc->synchronous = 0; 3252 cnc->ordered = 0; 3253 cnc->seek_to_oldest = 0; 3254 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0) 3255 cnc->new_only = 1; 3256 else 3257 cnc->new_only = 0; 3258 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE); 3259 3260 /* submit the request */ 3261 error = ciss_start(cr); 3262 3263 out: 3264 if (error) { 3265 if (cr != NULL) { 3266 if (cr->cr_data != NULL) 3267 free(cr->cr_data, CISS_MALLOC_CLASS); 3268 ciss_release_request(cr); 3269 } 3270 sc->ciss_periodic_notify = NULL; 3271 debug(0, "can't submit notify event request"); 3272 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK; 3273 } else { 3274 debug(1, "notify event submitted"); 3275 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK; 3276 } 3277 } 3278 3279 static void 3280 ciss_notify_complete(struct ciss_request *cr) 3281 { 3282 struct ciss_command *cc; 3283 struct ciss_notify *cn; 3284 struct ciss_softc *sc; 3285 int scsi_status; 3286 int command_status; 3287 debug_called(1); 3288 3289 cc = CISS_FIND_COMMAND(cr); 3290 cn = (struct ciss_notify *)cr->cr_data; 3291 sc = cr->cr_sc; 3292 3293 /* 3294 * Report request results, decode status. 3295 */ 3296 ciss_report_request(cr, &command_status, &scsi_status); 3297 3298 /* 3299 * Abort the chain on a fatal error. 3300 * 3301 * XXX which of these are actually errors? 3302 */ 3303 if ((command_status != CISS_CMD_STATUS_SUCCESS) && 3304 (command_status != CISS_CMD_STATUS_TARGET_STATUS) && 3305 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */ 3306 ciss_printf(sc, "fatal error in Notify Event request (%s)\n", 3307 ciss_name_command_status(command_status)); 3308 ciss_release_request(cr); 3309 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK; 3310 return; 3311 } 3312 3313 /* 3314 * If the adapter gave us a text message, print it. 3315 */ 3316 if (cn->message[0] != 0) 3317 ciss_printf(sc, "*** %.80s\n", cn->message); 3318 3319 debug(0, "notify event class %d subclass %d detail %d", 3320 cn->class, cn->subclass, cn->detail); 3321 3322 /* 3323 * If the response indicates that the notifier has been aborted, 3324 * release the notifier command. 3325 */ 3326 if ((cn->class == CISS_NOTIFY_NOTIFIER) && 3327 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) && 3328 (cn->detail == 1)) { 3329 debug(0, "notifier exiting"); 3330 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK; 3331 ciss_release_request(cr); 3332 sc->ciss_periodic_notify = NULL; 3333 wakeup(&sc->ciss_periodic_notify); 3334 } else { 3335 /* Handle notify events in a kernel thread */ 3336 ciss_enqueue_notify(cr); 3337 sc->ciss_periodic_notify = NULL; 3338 wakeup(&sc->ciss_periodic_notify); 3339 wakeup(&sc->ciss_notify); 3340 } 3341 /* 3342 * Send a new notify event command, if we're not aborting. 3343 */ 3344 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) { 3345 ciss_notify_event(sc); 3346 } 3347 } 3348 3349 /************************************************************************ 3350 * Abort the Notify Event chain. 3351 * 3352 * Note that we can't just abort the command in progress; we have to 3353 * explicitly issue an Abort Notify Event command in order for the 3354 * adapter to clean up correctly. 3355 * 3356 * If we are called with CISS_FLAG_ABORTING set in the adapter softc, 3357 * the chain will not restart itself. 3358 */ 3359 static int 3360 ciss_notify_abort(struct ciss_softc *sc) 3361 { 3362 struct ciss_request *cr; 3363 struct ciss_command *cc; 3364 struct ciss_notify_cdb *cnc; 3365 int error, s, command_status, scsi_status; 3366 3367 debug_called(1); 3368 3369 cr = NULL; 3370 error = 0; 3371 3372 /* verify that there's an outstanding command */ 3373 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) 3374 goto out; 3375 3376 /* get a command to issue the abort with */ 3377 if ((error = ciss_get_request(sc, &cr))) 3378 goto out; 3379 3380 /* get a buffer for the result */ 3381 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) { 3382 debug(0, "can't get notify event request buffer"); 3383 error = ENOMEM; 3384 goto out; 3385 } 3386 cr->cr_length = CISS_NOTIFY_DATA_SIZE; 3387 3388 /* build the CDB */ 3389 cc = CISS_FIND_COMMAND(cr); 3390 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; 3391 cc->header.address.physical.bus = 0; 3392 cc->header.address.physical.target = 0; 3393 cc->cdb.cdb_length = sizeof(*cnc); 3394 cc->cdb.type = CISS_CDB_TYPE_COMMAND; 3395 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; 3396 cc->cdb.direction = CISS_CDB_DIRECTION_READ; 3397 cc->cdb.timeout = 0; /* no timeout, we hope */ 3398 3399 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]); 3400 bzero(cnc, sizeof(*cnc)); 3401 cnc->opcode = CISS_OPCODE_WRITE; 3402 cnc->command = CISS_COMMAND_ABORT_NOTIFY; 3403 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE); 3404 3405 ciss_print_request(cr); 3406 3407 /* 3408 * Submit the request and wait for it to complete. 3409 */ 3410 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 3411 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error); 3412 goto out; 3413 } 3414 3415 /* 3416 * Check response. 3417 */ 3418 ciss_report_request(cr, &command_status, &scsi_status); 3419 switch(command_status) { 3420 case CISS_CMD_STATUS_SUCCESS: 3421 break; 3422 case CISS_CMD_STATUS_INVALID_COMMAND: 3423 /* 3424 * Some older adapters don't support the CISS version of this 3425 * command. Fall back to using the BMIC version. 3426 */ 3427 error = ciss_notify_abort_bmic(sc); 3428 if (error != 0) 3429 goto out; 3430 break; 3431 3432 case CISS_CMD_STATUS_TARGET_STATUS: 3433 /* 3434 * This can happen if the adapter thinks there wasn't an outstanding 3435 * Notify Event command but we did. We clean up here. 3436 */ 3437 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) { 3438 if (sc->ciss_periodic_notify != NULL) 3439 ciss_release_request(sc->ciss_periodic_notify); 3440 error = 0; 3441 goto out; 3442 } 3443 /* FALLTHROUGH */ 3444 3445 default: 3446 ciss_printf(sc, "Abort Notify Event command failed (%s)\n", 3447 ciss_name_command_status(command_status)); 3448 error = EIO; 3449 goto out; 3450 } 3451 3452 /* 3453 * Sleep waiting for the notifier command to complete. Note 3454 * that if it doesn't, we may end up in a bad situation, since 3455 * the adapter may deliver it later. Also note that the adapter 3456 * requires the Notify Event command to be cancelled in order to 3457 * maintain internal bookkeeping. 3458 */ 3459 s = splcam(); 3460 while (sc->ciss_periodic_notify != NULL) { 3461 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5); 3462 if (error == EWOULDBLOCK) { 3463 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n"); 3464 break; 3465 } 3466 } 3467 splx(s); 3468 3469 out: 3470 /* release the cancel request */ 3471 if (cr != NULL) { 3472 if (cr->cr_data != NULL) 3473 free(cr->cr_data, CISS_MALLOC_CLASS); 3474 ciss_release_request(cr); 3475 } 3476 if (error == 0) 3477 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK; 3478 return(error); 3479 } 3480 3481 /************************************************************************ 3482 * Abort the Notify Event chain using a BMIC command. 3483 */ 3484 static int 3485 ciss_notify_abort_bmic(struct ciss_softc *sc) 3486 { 3487 struct ciss_request *cr; 3488 int error, command_status; 3489 3490 debug_called(1); 3491 3492 cr = NULL; 3493 error = 0; 3494 3495 /* verify that there's an outstanding command */ 3496 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) 3497 goto out; 3498 3499 /* 3500 * Build a BMIC command to cancel the Notify on Event command. 3501 * 3502 * Note that we are sending a CISS opcode here. Odd. 3503 */ 3504 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY, 3505 NULL, 0)) != 0) 3506 goto out; 3507 3508 /* 3509 * Submit the request and wait for it to complete. 3510 */ 3511 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) { 3512 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error); 3513 goto out; 3514 } 3515 3516 /* 3517 * Check response. 3518 */ 3519 ciss_report_request(cr, &command_status, NULL); 3520 switch(command_status) { 3521 case CISS_CMD_STATUS_SUCCESS: 3522 break; 3523 default: 3524 ciss_printf(sc, "error cancelling Notify on Event (%s)\n", 3525 ciss_name_command_status(command_status)); 3526 error = EIO; 3527 goto out; 3528 } 3529 3530 out: 3531 if (cr != NULL) 3532 ciss_release_request(cr); 3533 return(error); 3534 } 3535 3536 /************************************************************************ 3537 * Handle rescanning all the logical volumes when a notify event 3538 * causes the drives to come online or offline. 3539 */ 3540 static void 3541 ciss_notify_rescan_logical(struct ciss_softc *sc) 3542 { 3543 struct ciss_lun_report *cll; 3544 struct ciss_ldrive *ld; 3545 int i, j, ndrives; 3546 3547 /* 3548 * We must rescan all logical volumes to get the right logical 3549 * drive address. 3550 */ 3551 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS, 3552 CISS_MAX_LOGICAL); 3553 if (cll == NULL) 3554 return; 3555 3556 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address)); 3557 3558 /* 3559 * Delete any of the drives which were destroyed by the 3560 * firmware. 3561 */ 3562 for (i = 0; i < sc->ciss_max_logical_bus; i++) { 3563 for (j = 0; j < CISS_MAX_LOGICAL; j++) { 3564 ld = &sc->ciss_logical[i][j]; 3565 3566 if (ld->cl_update == 0) 3567 continue; 3568 3569 if (ld->cl_status != CISS_LD_ONLINE) { 3570 ciss_cam_rescan_target(sc, i, j); 3571 ld->cl_update = 0; 3572 if (ld->cl_ldrive) 3573 free(ld->cl_ldrive, CISS_MALLOC_CLASS); 3574 if (ld->cl_lstatus) 3575 free(ld->cl_lstatus, CISS_MALLOC_CLASS); 3576 3577 ld->cl_ldrive = NULL; 3578 ld->cl_lstatus = NULL; 3579 } 3580 } 3581 } 3582 3583 /* 3584 * Scan for new drives. 3585 */ 3586 for (i = 0; i < ndrives; i++) { 3587 int bus, target; 3588 3589 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun); 3590 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun); 3591 ld = &sc->ciss_logical[bus][target]; 3592 3593 if (ld->cl_update == 0) 3594 continue; 3595 3596 ld->cl_update = 0; 3597 ld->cl_address = cll->lun[i]; 3598 ld->cl_controller = &sc->ciss_controllers[bus]; 3599 if (ciss_identify_logical(sc, ld) == 0) { 3600 ciss_cam_rescan_target(sc, bus, target); 3601 } 3602 } 3603 free(cll, CISS_MALLOC_CLASS); 3604 } 3605 3606 /************************************************************************ 3607 * Handle a notify event relating to the status of a logical drive. 3608 * 3609 * XXX need to be able to defer some of these to properly handle 3610 * calling the "ID Physical drive" command, unless the 'extended' 3611 * drive IDs are always in BIG_MAP format. 3612 */ 3613 static void 3614 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn) 3615 { 3616 struct ciss_ldrive *ld; 3617 int ostatus, bus, target; 3618 3619 debug_called(2); 3620 3621 bus = cn->device.physical.bus; 3622 target = cn->data.logical_status.logical_drive; 3623 ld = &sc->ciss_logical[bus][target]; 3624 3625 switch (cn->subclass) { 3626 case CISS_NOTIFY_LOGICAL_STATUS: 3627 switch (cn->detail) { 3628 case 0: 3629 ciss_name_device(sc, bus, target); 3630 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n", 3631 cn->data.logical_status.logical_drive, ld->cl_name, 3632 ciss_name_ldrive_status(cn->data.logical_status.previous_state), 3633 ciss_name_ldrive_status(cn->data.logical_status.new_state), 3634 cn->data.logical_status.spare_state, 3635 "\20\1configured\2rebuilding\3failed\4in use\5available\n"); 3636 3637 /* 3638 * Update our idea of the drive's status. 3639 */ 3640 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state); 3641 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state); 3642 if (ld->cl_lstatus != NULL) 3643 ld->cl_lstatus->status = cn->data.logical_status.new_state; 3644 3645 /* 3646 * Have CAM rescan the drive if its status has changed. 3647 */ 3648 if (ostatus != ld->cl_status) { 3649 ld->cl_update = 1; 3650 ciss_notify_rescan_logical(sc); 3651 } 3652 3653 break; 3654 3655 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */ 3656 ciss_name_device(sc, bus, target); 3657 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n", 3658 cn->data.logical_status.logical_drive, ld->cl_name); 3659 ciss_accept_media(sc, ld); 3660 3661 ld->cl_update = 1; 3662 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state); 3663 ciss_notify_rescan_logical(sc); 3664 break; 3665 3666 case 2: 3667 case 3: 3668 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n", 3669 cn->data.rebuild_aborted.logical_drive, 3670 ld->cl_name, 3671 (cn->detail == 2) ? "read" : "write"); 3672 break; 3673 } 3674 break; 3675 3676 case CISS_NOTIFY_LOGICAL_ERROR: 3677 if (cn->detail == 0) { 3678 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n", 3679 cn->data.io_error.logical_drive, 3680 ld->cl_name, 3681 cn->data.io_error.failure_bus, 3682 cn->data.io_error.failure_drive); 3683 /* XXX should we take the drive down at this point, or will we be told? */ 3684 } 3685 break; 3686 3687 case CISS_NOTIFY_LOGICAL_SURFACE: 3688 if (cn->detail == 0) 3689 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n", 3690 cn->data.consistency_completed.logical_drive, 3691 ld->cl_name); 3692 break; 3693 } 3694 } 3695 3696 /************************************************************************ 3697 * Handle a notify event relating to the status of a physical drive. 3698 */ 3699 static void 3700 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn) 3701 { 3702 } 3703 3704 /************************************************************************ 3705 * Handle a notify event relating to the status of a physical drive. 3706 */ 3707 static void 3708 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn) 3709 { 3710 struct ciss_lun_report *cll = NULL; 3711 int bus, target; 3712 int s; 3713 3714 switch (cn->subclass) { 3715 case CISS_NOTIFY_HOTPLUG_PHYSICAL: 3716 case CISS_NOTIFY_HOTPLUG_NONDISK: 3717 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number); 3718 target = 3719 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number); 3720 3721 s = splcam(); 3722 if (cn->detail == 0) { 3723 /* 3724 * Mark the device offline so that it'll start producing selection 3725 * timeouts to the upper layer. 3726 */ 3727 if ((bus >= 0) && (target >= 0)) 3728 sc->ciss_physical[bus][target].cp_online = 0; 3729 } else { 3730 /* 3731 * Rescan the physical lun list for new items 3732 */ 3733 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS, 3734 CISS_MAX_PHYSICAL); 3735 if (cll == NULL) { 3736 ciss_printf(sc, "Warning, cannot get physical lun list\n"); 3737 break; 3738 } 3739 ciss_filter_physical(sc, cll); 3740 } 3741 splx(s); 3742 break; 3743 3744 default: 3745 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass); 3746 return; 3747 } 3748 3749 if (cll != NULL) 3750 free(cll, CISS_MALLOC_CLASS); 3751 } 3752 3753 /************************************************************************ 3754 * Handle deferred processing of notify events. Notify events may need 3755 * sleep which is unsafe during an interrupt. 3756 */ 3757 static void 3758 ciss_notify_thread(void *arg) 3759 { 3760 struct ciss_softc *sc; 3761 struct ciss_request *cr; 3762 struct ciss_notify *cn; 3763 int s; 3764 3765 sc = (struct ciss_softc *)arg; 3766 #if __FreeBSD_version >= 500000 3767 mtx_lock(&sc->ciss_mtx); 3768 #endif 3769 3770 s = splcam(); 3771 for (;;) { 3772 if (TAILQ_EMPTY(&sc->ciss_notify) != 0 && 3773 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) { 3774 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0); 3775 } 3776 3777 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) 3778 break; 3779 3780 cr = ciss_dequeue_notify(sc); 3781 splx(s); 3782 3783 if (cr == NULL) 3784 panic("cr null"); 3785 cn = (struct ciss_notify *)cr->cr_data; 3786 3787 switch (cn->class) { 3788 case CISS_NOTIFY_HOTPLUG: 3789 ciss_notify_hotplug(sc, cn); 3790 break; 3791 case CISS_NOTIFY_LOGICAL: 3792 ciss_notify_logical(sc, cn); 3793 break; 3794 case CISS_NOTIFY_PHYSICAL: 3795 ciss_notify_physical(sc, cn); 3796 break; 3797 } 3798 3799 ciss_release_request(cr); 3800 3801 s = splcam(); 3802 } 3803 sc->ciss_notify_thread = NULL; 3804 wakeup(&sc->ciss_notify_thread); 3805 splx(s); 3806 3807 #if __FreeBSD_version >= 500000 3808 mtx_unlock(&sc->ciss_mtx); 3809 #endif 3810 kproc_exit(0); 3811 } 3812 3813 /************************************************************************ 3814 * Start the notification kernel thread. 3815 */ 3816 static void 3817 ciss_spawn_notify_thread(struct ciss_softc *sc) 3818 { 3819 3820 #if __FreeBSD_version > 500005 3821 if (kproc_create((void(*)(void *))ciss_notify_thread, sc, 3822 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d", 3823 device_get_unit(sc->ciss_dev))) 3824 #else 3825 if (kproc_create((void(*)(void *))ciss_notify_thread, sc, 3826 &sc->ciss_notify_thread, "ciss_notify%d", 3827 device_get_unit(sc->ciss_dev))) 3828 #endif 3829 panic("Could not create notify thread\n"); 3830 } 3831 3832 /************************************************************************ 3833 * Kill the notification kernel thread. 3834 */ 3835 static void 3836 ciss_kill_notify_thread(struct ciss_softc *sc) 3837 { 3838 3839 if (sc->ciss_notify_thread == NULL) 3840 return; 3841 3842 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT; 3843 wakeup(&sc->ciss_notify); 3844 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0); 3845 } 3846 3847 /************************************************************************ 3848 * Print a request. 3849 */ 3850 static void 3851 ciss_print_request(struct ciss_request *cr) 3852 { 3853 struct ciss_softc *sc; 3854 struct ciss_command *cc; 3855 int i; 3856 3857 sc = cr->cr_sc; 3858 cc = CISS_FIND_COMMAND(cr); 3859 3860 ciss_printf(sc, "REQUEST @ %p\n", cr); 3861 ciss_printf(sc, " data %p/%d tag %d flags %b\n", 3862 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags, 3863 "\20\1mapped\2sleep\3poll\4dataout\5datain\n"); 3864 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n", 3865 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag); 3866 switch(cc->header.address.mode.mode) { 3867 case CISS_HDR_ADDRESS_MODE_PERIPHERAL: 3868 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL: 3869 ciss_printf(sc, " physical bus %d target %d\n", 3870 cc->header.address.physical.bus, cc->header.address.physical.target); 3871 break; 3872 case CISS_HDR_ADDRESS_MODE_LOGICAL: 3873 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun); 3874 break; 3875 } 3876 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n", 3877 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" : 3878 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" : 3879 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??", 3880 cc->cdb.cdb_length, 3881 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" : 3882 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??", 3883 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" : 3884 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" : 3885 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" : 3886 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" : 3887 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??"); 3888 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " "); 3889 3890 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) { 3891 /* XXX print error info */ 3892 } else { 3893 /* since we don't use chained s/g, don't support it here */ 3894 for (i = 0; i < cc->header.sg_in_list; i++) { 3895 if ((i % 4) == 0) 3896 ciss_printf(sc, " "); 3897 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length); 3898 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1))) 3899 printf("\n"); 3900 } 3901 } 3902 } 3903 3904 /************************************************************************ 3905 * Print information about the status of a logical drive. 3906 */ 3907 static void 3908 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld) 3909 { 3910 int bus, target, i; 3911 3912 if (ld->cl_lstatus == NULL) { 3913 printf("does not exist\n"); 3914 return; 3915 } 3916 3917 /* print drive status */ 3918 switch(ld->cl_lstatus->status) { 3919 case CISS_LSTATUS_OK: 3920 printf("online\n"); 3921 break; 3922 case CISS_LSTATUS_INTERIM_RECOVERY: 3923 printf("in interim recovery mode\n"); 3924 break; 3925 case CISS_LSTATUS_READY_RECOVERY: 3926 printf("ready to begin recovery\n"); 3927 break; 3928 case CISS_LSTATUS_RECOVERING: 3929 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding); 3930 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding); 3931 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n", 3932 bus, target, ld->cl_lstatus->blocks_to_recover); 3933 break; 3934 case CISS_LSTATUS_EXPANDING: 3935 printf("being expanded, %u blocks remaining\n", 3936 ld->cl_lstatus->blocks_to_recover); 3937 break; 3938 case CISS_LSTATUS_QUEUED_FOR_EXPANSION: 3939 printf("queued for expansion\n"); 3940 break; 3941 case CISS_LSTATUS_FAILED: 3942 printf("queued for expansion\n"); 3943 break; 3944 case CISS_LSTATUS_WRONG_PDRIVE: 3945 printf("wrong physical drive inserted\n"); 3946 break; 3947 case CISS_LSTATUS_MISSING_PDRIVE: 3948 printf("missing a needed physical drive\n"); 3949 break; 3950 case CISS_LSTATUS_BECOMING_READY: 3951 printf("becoming ready\n"); 3952 break; 3953 } 3954 3955 /* print failed physical drives */ 3956 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) { 3957 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]); 3958 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]); 3959 if (bus == -1) 3960 continue; 3961 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target, 3962 ld->cl_lstatus->drive_failure_map[i]); 3963 } 3964 } 3965 3966 #ifdef CISS_DEBUG 3967 /************************************************************************ 3968 * Print information about the controller/driver. 3969 */ 3970 static void 3971 ciss_print_adapter(struct ciss_softc *sc) 3972 { 3973 int i, j; 3974 3975 ciss_printf(sc, "ADAPTER:\n"); 3976 for (i = 0; i < CISSQ_COUNT; i++) { 3977 ciss_printf(sc, "%s %d/%d\n", 3978 i == 0 ? "free" : 3979 i == 1 ? "busy" : "complete", 3980 sc->ciss_qstat[i].q_length, 3981 sc->ciss_qstat[i].q_max); 3982 } 3983 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests); 3984 ciss_printf(sc, "flags %b\n", sc->ciss_flags, 3985 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n"); 3986 3987 for (i = 0; i < sc->ciss_max_logical_bus; i++) { 3988 for (j = 0; j < CISS_MAX_LOGICAL; j++) { 3989 ciss_printf(sc, "LOGICAL DRIVE %d: ", i); 3990 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]); 3991 } 3992 } 3993 3994 /* XXX Should physical drives be printed out here? */ 3995 3996 for (i = 1; i < sc->ciss_max_requests; i++) 3997 ciss_print_request(sc->ciss_request + i); 3998 } 3999 4000 /* DDB hook */ 4001 static void 4002 ciss_print0(void) 4003 { 4004 struct ciss_softc *sc; 4005 4006 sc = devclass_get_softc(devclass_find("ciss"), 0); 4007 if (sc == NULL) { 4008 printf("no ciss controllers\n"); 4009 } else { 4010 ciss_print_adapter(sc); 4011 } 4012 } 4013 #endif 4014 4015 /************************************************************************ 4016 * Return a name for a logical drive status value. 4017 */ 4018 static const char * 4019 ciss_name_ldrive_status(int status) 4020 { 4021 switch (status) { 4022 case CISS_LSTATUS_OK: 4023 return("OK"); 4024 case CISS_LSTATUS_FAILED: 4025 return("failed"); 4026 case CISS_LSTATUS_NOT_CONFIGURED: 4027 return("not configured"); 4028 case CISS_LSTATUS_INTERIM_RECOVERY: 4029 return("interim recovery"); 4030 case CISS_LSTATUS_READY_RECOVERY: 4031 return("ready for recovery"); 4032 case CISS_LSTATUS_RECOVERING: 4033 return("recovering"); 4034 case CISS_LSTATUS_WRONG_PDRIVE: 4035 return("wrong physical drive inserted"); 4036 case CISS_LSTATUS_MISSING_PDRIVE: 4037 return("missing physical drive"); 4038 case CISS_LSTATUS_EXPANDING: 4039 return("expanding"); 4040 case CISS_LSTATUS_BECOMING_READY: 4041 return("becoming ready"); 4042 case CISS_LSTATUS_QUEUED_FOR_EXPANSION: 4043 return("queued for expansion"); 4044 } 4045 return("unknown status"); 4046 } 4047 4048 /************************************************************************ 4049 * Return an online/offline/nonexistent value for a logical drive 4050 * status value. 4051 */ 4052 static int 4053 ciss_decode_ldrive_status(int status) 4054 { 4055 switch(status) { 4056 case CISS_LSTATUS_NOT_CONFIGURED: 4057 return(CISS_LD_NONEXISTENT); 4058 4059 case CISS_LSTATUS_OK: 4060 case CISS_LSTATUS_INTERIM_RECOVERY: 4061 case CISS_LSTATUS_READY_RECOVERY: 4062 case CISS_LSTATUS_RECOVERING: 4063 case CISS_LSTATUS_EXPANDING: 4064 case CISS_LSTATUS_QUEUED_FOR_EXPANSION: 4065 return(CISS_LD_ONLINE); 4066 4067 case CISS_LSTATUS_FAILED: 4068 case CISS_LSTATUS_WRONG_PDRIVE: 4069 case CISS_LSTATUS_MISSING_PDRIVE: 4070 case CISS_LSTATUS_BECOMING_READY: 4071 default: 4072 return(CISS_LD_OFFLINE); 4073 } 4074 } 4075 4076 4077 /************************************************************************ 4078 * Return a name for a logical drive's organisation. 4079 */ 4080 static const char * 4081 ciss_name_ldrive_org(int org) 4082 { 4083 switch(org) { 4084 case CISS_LDRIVE_RAID0: 4085 return("RAID 0"); 4086 case CISS_LDRIVE_RAID1: 4087 return("RAID 1"); 4088 case CISS_LDRIVE_RAID4: 4089 return("RAID 4"); 4090 case CISS_LDRIVE_RAID5: 4091 return("RAID 5"); 4092 case CISS_LDRIVE_RAID51: 4093 return("RAID 5+1"); 4094 case CISS_LDRIVE_RAIDADG: 4095 return("RAID ADG"); 4096 } 4097 return("unkown"); 4098 } 4099 4100 /************************************************************************ 4101 * Return a name for a command status value. 4102 */ 4103 static const char * 4104 ciss_name_command_status(int status) 4105 { 4106 switch(status) { 4107 case CISS_CMD_STATUS_SUCCESS: 4108 return("success"); 4109 case CISS_CMD_STATUS_TARGET_STATUS: 4110 return("target status"); 4111 case CISS_CMD_STATUS_DATA_UNDERRUN: 4112 return("data underrun"); 4113 case CISS_CMD_STATUS_DATA_OVERRUN: 4114 return("data overrun"); 4115 case CISS_CMD_STATUS_INVALID_COMMAND: 4116 return("invalid command"); 4117 case CISS_CMD_STATUS_PROTOCOL_ERROR: 4118 return("protocol error"); 4119 case CISS_CMD_STATUS_HARDWARE_ERROR: 4120 return("hardware error"); 4121 case CISS_CMD_STATUS_CONNECTION_LOST: 4122 return("connection lost"); 4123 case CISS_CMD_STATUS_ABORTED: 4124 return("aborted"); 4125 case CISS_CMD_STATUS_ABORT_FAILED: 4126 return("abort failed"); 4127 case CISS_CMD_STATUS_UNSOLICITED_ABORT: 4128 return("unsolicited abort"); 4129 case CISS_CMD_STATUS_TIMEOUT: 4130 return("timeout"); 4131 case CISS_CMD_STATUS_UNABORTABLE: 4132 return("unabortable"); 4133 } 4134 return("unknown status"); 4135 } 4136 4137 /************************************************************************ 4138 * Handle an open on the control device. 4139 */ 4140 static int 4141 ciss_open(struct cdev *dev, int flags, int fmt, d_thread_t *p) 4142 { 4143 struct ciss_softc *sc; 4144 4145 debug_called(1); 4146 4147 sc = (struct ciss_softc *)dev->si_drv1; 4148 4149 /* we might want to veto if someone already has us open */ 4150 4151 mtx_lock(&sc->ciss_mtx); 4152 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN; 4153 mtx_unlock(&sc->ciss_mtx); 4154 return(0); 4155 } 4156 4157 /************************************************************************ 4158 * Handle the last close on the control device. 4159 */ 4160 static int 4161 ciss_close(struct cdev *dev, int flags, int fmt, d_thread_t *p) 4162 { 4163 struct ciss_softc *sc; 4164 4165 debug_called(1); 4166 4167 sc = (struct ciss_softc *)dev->si_drv1; 4168 4169 mtx_lock(&sc->ciss_mtx); 4170 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN; 4171 mtx_unlock(&sc->ciss_mtx); 4172 return (0); 4173 } 4174 4175 /******************************************************************************** 4176 * Handle adapter-specific control operations. 4177 * 4178 * Note that the API here is compatible with the Linux driver, in order to 4179 * simplify the porting of Compaq's userland tools. 4180 */ 4181 static int 4182 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p) 4183 { 4184 struct ciss_softc *sc; 4185 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr; 4186 #ifdef __amd64__ 4187 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr; 4188 IOCTL_Command_struct ioc_swab; 4189 #endif 4190 int error; 4191 4192 debug_called(1); 4193 4194 sc = (struct ciss_softc *)dev->si_drv1; 4195 error = 0; 4196 mtx_lock(&sc->ciss_mtx); 4197 4198 switch(cmd) { 4199 case CCISS_GETPCIINFO: 4200 { 4201 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr; 4202 4203 pis->bus = pci_get_bus(sc->ciss_dev); 4204 pis->dev_fn = pci_get_slot(sc->ciss_dev); 4205 pis->board_id = pci_get_devid(sc->ciss_dev); 4206 4207 break; 4208 } 4209 4210 case CCISS_GETINTINFO: 4211 { 4212 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr; 4213 4214 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay; 4215 cis->count = sc->ciss_cfg->interrupt_coalesce_count; 4216 4217 break; 4218 } 4219 4220 case CCISS_SETINTINFO: 4221 { 4222 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr; 4223 4224 if ((cis->delay == 0) && (cis->count == 0)) { 4225 error = EINVAL; 4226 break; 4227 } 4228 4229 /* 4230 * XXX apparently this is only safe if the controller is idle, 4231 * we should suspend it before doing this. 4232 */ 4233 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay; 4234 sc->ciss_cfg->interrupt_coalesce_count = cis->count; 4235 4236 if (ciss_update_config(sc)) 4237 error = EIO; 4238 4239 /* XXX resume the controller here */ 4240 break; 4241 } 4242 4243 case CCISS_GETNODENAME: 4244 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr, 4245 sizeof(NodeName_type)); 4246 break; 4247 4248 case CCISS_SETNODENAME: 4249 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name, 4250 sizeof(NodeName_type)); 4251 if (ciss_update_config(sc)) 4252 error = EIO; 4253 break; 4254 4255 case CCISS_GETHEARTBEAT: 4256 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat; 4257 break; 4258 4259 case CCISS_GETBUSTYPES: 4260 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types; 4261 break; 4262 4263 case CCISS_GETFIRMVER: 4264 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr, 4265 sizeof(FirmwareVer_type)); 4266 break; 4267 4268 case CCISS_GETDRIVERVER: 4269 *(DriverVer_type *)addr = CISS_DRIVER_VERSION; 4270 break; 4271 4272 case CCISS_REVALIDVOLS: 4273 /* 4274 * This is a bit ugly; to do it "right" we really need 4275 * to find any disks that have changed, kick CAM off them, 4276 * then rescan only these disks. It'd be nice if they 4277 * a) told us which disk(s) they were going to play with, 4278 * and b) which ones had arrived. 8( 4279 */ 4280 break; 4281 4282 #ifdef __amd64__ 4283 case CCISS_PASSTHRU32: 4284 ioc_swab.LUN_info = ioc32->LUN_info; 4285 ioc_swab.Request = ioc32->Request; 4286 ioc_swab.error_info = ioc32->error_info; 4287 ioc_swab.buf_size = ioc32->buf_size; 4288 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf; 4289 ioc = &ioc_swab; 4290 /* FALLTHROUGH */ 4291 #endif 4292 4293 case CCISS_PASSTHRU: 4294 error = ciss_user_command(sc, ioc); 4295 break; 4296 4297 default: 4298 debug(0, "unknown ioctl 0x%lx", cmd); 4299 4300 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO); 4301 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO); 4302 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO); 4303 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME); 4304 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME); 4305 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT); 4306 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES); 4307 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER); 4308 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER); 4309 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS); 4310 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU); 4311 4312 error = ENOIOCTL; 4313 break; 4314 } 4315 4316 mtx_unlock(&sc->ciss_mtx); 4317 return(error); 4318 } 4319