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