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