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