1 /*- 2 * Copyright (c) 1999 Michael Smith 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Driver for the Mylex DAC960 family of RAID controllers. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bio.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/kernel.h> 40 #include <sys/sx.h> 41 42 #include <sys/bus.h> 43 #include <sys/conf.h> 44 #include <sys/stat.h> 45 46 #include <machine/resource.h> 47 #include <machine/bus.h> 48 #include <machine/clock.h> 49 #include <sys/rman.h> 50 51 #include <geom/geom_disk.h> 52 53 #include <dev/mlx/mlxio.h> 54 #include <dev/mlx/mlxvar.h> 55 #include <dev/mlx/mlxreg.h> 56 57 static struct cdevsw mlx_cdevsw = { 58 .d_version = D_VERSION, 59 .d_open = mlx_open, 60 .d_close = mlx_close, 61 .d_ioctl = mlx_ioctl, 62 .d_name = "mlx", 63 }; 64 65 devclass_t mlx_devclass; 66 67 /* 68 * Per-interface accessor methods 69 */ 70 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 71 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 72 static void mlx_v3_intaction(struct mlx_softc *sc, int action); 73 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first); 74 75 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 76 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 77 static void mlx_v4_intaction(struct mlx_softc *sc, int action); 78 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first); 79 80 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 81 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 82 static void mlx_v5_intaction(struct mlx_softc *sc, int action); 83 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first); 84 85 /* 86 * Status monitoring 87 */ 88 static void mlx_periodic(void *data); 89 static void mlx_periodic_enquiry(struct mlx_command *mc); 90 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc); 91 static void mlx_periodic_eventlog_respond(struct mlx_command *mc); 92 static void mlx_periodic_rebuild(struct mlx_command *mc); 93 94 /* 95 * Channel Pause 96 */ 97 static void mlx_pause_action(struct mlx_softc *sc); 98 static void mlx_pause_done(struct mlx_command *mc); 99 100 /* 101 * Command submission. 102 */ 103 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, 104 void (*complete)(struct mlx_command *mc)); 105 static int mlx_flush(struct mlx_softc *sc); 106 static int mlx_check(struct mlx_softc *sc, int drive); 107 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target); 108 static int mlx_wait_command(struct mlx_command *mc); 109 static int mlx_poll_command(struct mlx_command *mc); 110 void mlx_startio_cb(void *arg, 111 bus_dma_segment_t *segs, 112 int nsegments, int error); 113 static void mlx_startio(struct mlx_softc *sc); 114 static void mlx_completeio(struct mlx_command *mc); 115 static int mlx_user_command(struct mlx_softc *sc, 116 struct mlx_usercommand *mu); 117 void mlx_user_cb(void *arg, bus_dma_segment_t *segs, 118 int nsegments, int error); 119 120 /* 121 * Command buffer allocation. 122 */ 123 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc); 124 static void mlx_releasecmd(struct mlx_command *mc); 125 static void mlx_freecmd(struct mlx_command *mc); 126 127 /* 128 * Command management. 129 */ 130 static int mlx_getslot(struct mlx_command *mc); 131 static void mlx_setup_dmamap(struct mlx_command *mc, 132 bus_dma_segment_t *segs, 133 int nsegments, int error); 134 static void mlx_unmapcmd(struct mlx_command *mc); 135 static int mlx_shutdown_locked(struct mlx_softc *sc); 136 static int mlx_start(struct mlx_command *mc); 137 static int mlx_done(struct mlx_softc *sc, int startio); 138 static void mlx_complete(struct mlx_softc *sc); 139 140 /* 141 * Debugging. 142 */ 143 static char *mlx_diagnose_command(struct mlx_command *mc); 144 static void mlx_describe_controller(struct mlx_softc *sc); 145 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2); 146 147 /* 148 * Utility functions. 149 */ 150 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit); 151 152 /******************************************************************************** 153 ******************************************************************************** 154 Public Interfaces 155 ******************************************************************************** 156 ********************************************************************************/ 157 158 /******************************************************************************** 159 * Free all of the resources associated with (sc) 160 * 161 * Should not be called if the controller is active. 162 */ 163 void 164 mlx_free(struct mlx_softc *sc) 165 { 166 struct mlx_command *mc; 167 168 debug_called(1); 169 170 /* destroy control device */ 171 if (sc->mlx_dev_t != NULL) 172 destroy_dev(sc->mlx_dev_t); 173 174 if (sc->mlx_intr) 175 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr); 176 177 /* cancel status timeout */ 178 MLX_IO_LOCK(sc); 179 callout_stop(&sc->mlx_timeout); 180 181 /* throw away any command buffers */ 182 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) { 183 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link); 184 mlx_freecmd(mc); 185 } 186 MLX_IO_UNLOCK(sc); 187 callout_drain(&sc->mlx_timeout); 188 189 /* destroy data-transfer DMA tag */ 190 if (sc->mlx_buffer_dmat) 191 bus_dma_tag_destroy(sc->mlx_buffer_dmat); 192 193 /* free and destroy DMA memory and tag for s/g lists */ 194 if (sc->mlx_sgbusaddr) 195 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap); 196 if (sc->mlx_sgtable) 197 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap); 198 if (sc->mlx_sg_dmat) 199 bus_dma_tag_destroy(sc->mlx_sg_dmat); 200 201 /* disconnect the interrupt handler */ 202 if (sc->mlx_irq != NULL) 203 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq); 204 205 /* destroy the parent DMA tag */ 206 if (sc->mlx_parent_dmat) 207 bus_dma_tag_destroy(sc->mlx_parent_dmat); 208 209 /* release the register window mapping */ 210 if (sc->mlx_mem != NULL) 211 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem); 212 213 /* free controller enquiry data */ 214 if (sc->mlx_enq2 != NULL) 215 free(sc->mlx_enq2, M_DEVBUF); 216 217 sx_destroy(&sc->mlx_config_lock); 218 mtx_destroy(&sc->mlx_io_lock); 219 } 220 221 /******************************************************************************** 222 * Map the scatter/gather table into bus space 223 */ 224 static void 225 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) 226 { 227 struct mlx_softc *sc = (struct mlx_softc *)arg; 228 229 debug_called(1); 230 231 /* save base of s/g table's address in bus space */ 232 sc->mlx_sgbusaddr = segs->ds_addr; 233 } 234 235 static int 236 mlx_sglist_map(struct mlx_softc *sc) 237 { 238 size_t segsize; 239 int error, ncmd; 240 241 debug_called(1); 242 243 /* destroy any existing mappings */ 244 if (sc->mlx_sgbusaddr) 245 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap); 246 if (sc->mlx_sgtable) 247 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap); 248 if (sc->mlx_sg_dmat) 249 bus_dma_tag_destroy(sc->mlx_sg_dmat); 250 sc->mlx_sgbusaddr = 0; 251 sc->mlx_sgtable = NULL; 252 sc->mlx_sg_dmat = NULL; 253 254 /* 255 * Create a single tag describing a region large enough to hold all of 256 * the s/g lists we will need. If we're called early on, we don't know how 257 * many commands we're going to be asked to support, so only allocate enough 258 * for a couple. 259 */ 260 if (sc->mlx_enq2 == NULL) { 261 ncmd = 2; 262 } else { 263 ncmd = sc->mlx_enq2->me_max_commands; 264 } 265 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd; 266 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */ 267 1, 0, /* alignment,boundary */ 268 BUS_SPACE_MAXADDR, /* lowaddr */ 269 BUS_SPACE_MAXADDR, /* highaddr */ 270 NULL, NULL, /* filter, filterarg */ 271 segsize, 1, /* maxsize, nsegments */ 272 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 273 0, /* flags */ 274 NULL, NULL, /* lockfunc, lockarg */ 275 &sc->mlx_sg_dmat); 276 if (error != 0) { 277 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n"); 278 return(ENOMEM); 279 } 280 281 /* 282 * Allocate enough s/g maps for all commands and permanently map them into 283 * controller-visible space. 284 * 285 * XXX this assumes we can get enough space for all the s/g maps in one 286 * contiguous slab. We may need to switch to a more complex arrangement 287 * where we allocate in smaller chunks and keep a lookup table from slot 288 * to bus address. 289 */ 290 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable, 291 BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap); 292 if (error) { 293 device_printf(sc->mlx_dev, "can't allocate s/g table\n"); 294 return(ENOMEM); 295 } 296 (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, 297 segsize, mlx_dma_map_sg, sc, 0); 298 return(0); 299 } 300 301 /******************************************************************************** 302 * Initialise the controller and softc 303 */ 304 int 305 mlx_attach(struct mlx_softc *sc) 306 { 307 struct mlx_enquiry_old *meo; 308 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg; 309 310 debug_called(1); 311 312 /* 313 * Initialise per-controller queues. 314 */ 315 TAILQ_INIT(&sc->mlx_work); 316 TAILQ_INIT(&sc->mlx_freecmds); 317 bioq_init(&sc->mlx_bioq); 318 319 /* 320 * Select accessor methods based on controller interface type. 321 */ 322 switch(sc->mlx_iftype) { 323 case MLX_IFTYPE_2: 324 case MLX_IFTYPE_3: 325 sc->mlx_tryqueue = mlx_v3_tryqueue; 326 sc->mlx_findcomplete = mlx_v3_findcomplete; 327 sc->mlx_intaction = mlx_v3_intaction; 328 sc->mlx_fw_handshake = mlx_v3_fw_handshake; 329 break; 330 case MLX_IFTYPE_4: 331 sc->mlx_tryqueue = mlx_v4_tryqueue; 332 sc->mlx_findcomplete = mlx_v4_findcomplete; 333 sc->mlx_intaction = mlx_v4_intaction; 334 sc->mlx_fw_handshake = mlx_v4_fw_handshake; 335 break; 336 case MLX_IFTYPE_5: 337 sc->mlx_tryqueue = mlx_v5_tryqueue; 338 sc->mlx_findcomplete = mlx_v5_findcomplete; 339 sc->mlx_intaction = mlx_v5_intaction; 340 sc->mlx_fw_handshake = mlx_v5_fw_handshake; 341 break; 342 default: 343 return(ENXIO); /* should never happen */ 344 } 345 346 /* disable interrupts before we start talking to the controller */ 347 MLX_IO_LOCK(sc); 348 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 349 MLX_IO_UNLOCK(sc); 350 351 /* 352 * Wait for the controller to come ready, handshake with the firmware if required. 353 * This is typically only necessary on platforms where the controller BIOS does not 354 * run. 355 */ 356 hsmsg = 0; 357 DELAY(1000); 358 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2, 359 hsmsg == 0)) != 0) { 360 /* report first time around... */ 361 if (hsmsg == 0) { 362 device_printf(sc->mlx_dev, "controller initialisation in progress...\n"); 363 hsmsg = 1; 364 } 365 /* did we get a real message? */ 366 if (hscode == 2) { 367 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2); 368 /* fatal initialisation error? */ 369 if (hscode != 0) { 370 return(ENXIO); 371 } 372 } 373 } 374 if (hsmsg == 1) 375 device_printf(sc->mlx_dev, "initialisation complete.\n"); 376 377 /* 378 * Allocate and connect our interrupt. 379 */ 380 rid = 0; 381 sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid, 382 RF_SHAREABLE | RF_ACTIVE); 383 if (sc->mlx_irq == NULL) { 384 device_printf(sc->mlx_dev, "can't allocate interrupt\n"); 385 return(ENXIO); 386 } 387 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO | 388 INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr); 389 if (error) { 390 device_printf(sc->mlx_dev, "can't set up interrupt\n"); 391 return(ENXIO); 392 } 393 394 /* 395 * Create DMA tag for mapping buffers into controller-addressable space. 396 */ 397 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */ 398 1, 0, /* align, boundary */ 399 BUS_SPACE_MAXADDR, /* lowaddr */ 400 BUS_SPACE_MAXADDR, /* highaddr */ 401 NULL, NULL, /* filter, filterarg */ 402 MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */ 403 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 404 0, /* flags */ 405 busdma_lock_mutex, /* lockfunc */ 406 &sc->mlx_io_lock, /* lockarg */ 407 &sc->mlx_buffer_dmat); 408 if (error != 0) { 409 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n"); 410 return(ENOMEM); 411 } 412 413 /* 414 * Create some initial scatter/gather mappings so we can run the probe 415 * commands. 416 */ 417 error = mlx_sglist_map(sc); 418 if (error != 0) { 419 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n"); 420 return(error); 421 } 422 423 /* 424 * We don't (yet) know where the event log is up to. 425 */ 426 sc->mlx_currevent = -1; 427 428 /* 429 * Obtain controller feature information 430 */ 431 MLX_IO_LOCK(sc); 432 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) { 433 MLX_IO_UNLOCK(sc); 434 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n"); 435 return(ENXIO); 436 } 437 438 /* 439 * Do quirk/feature related things. 440 */ 441 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff; 442 switch(sc->mlx_iftype) { 443 case MLX_IFTYPE_2: 444 /* These controllers don't report the firmware version in the ENQUIRY2 response */ 445 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) { 446 MLX_IO_UNLOCK(sc); 447 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n"); 448 return(ENXIO); 449 } 450 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor; 451 452 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */ 453 if (meo->me_fwminor < 42) { 454 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 455 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n"); 456 } 457 free(meo, M_DEVBUF); 458 break; 459 case MLX_IFTYPE_3: 460 /* XXX certify 3.52? */ 461 if (fwminor < 51) { 462 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 463 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n"); 464 } 465 break; 466 case MLX_IFTYPE_4: 467 /* XXX certify firmware versions? */ 468 if (fwminor < 6) { 469 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 470 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n"); 471 } 472 break; 473 case MLX_IFTYPE_5: 474 if (fwminor < 7) { 475 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 476 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n"); 477 } 478 break; 479 default: 480 MLX_IO_UNLOCK(sc); 481 return(ENXIO); /* should never happen */ 482 } 483 MLX_IO_UNLOCK(sc); 484 485 /* 486 * Create the final scatter/gather mappings now that we have characterised the controller. 487 */ 488 error = mlx_sglist_map(sc); 489 if (error != 0) { 490 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n"); 491 return(error); 492 } 493 494 /* 495 * No user-requested background operation is in progress. 496 */ 497 sc->mlx_background = 0; 498 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE; 499 500 /* 501 * Create the control device. 502 */ 503 sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR, 504 S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev)); 505 sc->mlx_dev_t->si_drv1 = sc; 506 507 /* 508 * Start the timeout routine. 509 */ 510 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc); 511 512 /* print a little information about the controller */ 513 mlx_describe_controller(sc); 514 515 return(0); 516 } 517 518 /******************************************************************************** 519 * Locate disk resources and attach children to them. 520 */ 521 void 522 mlx_startup(struct mlx_softc *sc) 523 { 524 struct mlx_enq_sys_drive *mes; 525 struct mlx_sysdrive *dr; 526 int i, error; 527 528 debug_called(1); 529 530 /* 531 * Scan all the system drives and attach children for those that 532 * don't currently have them. 533 */ 534 MLX_IO_LOCK(sc); 535 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL); 536 MLX_IO_UNLOCK(sc); 537 if (mes == NULL) { 538 device_printf(sc->mlx_dev, "error fetching drive status\n"); 539 return; 540 } 541 542 /* iterate over drives returned */ 543 MLX_CONFIG_LOCK(sc); 544 for (i = 0, dr = &sc->mlx_sysdrive[0]; 545 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff); 546 i++, dr++) { 547 /* are we already attached to this drive? */ 548 if (dr->ms_disk == 0) { 549 /* pick up drive information */ 550 dr->ms_size = mes[i].sd_size; 551 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf; 552 dr->ms_state = mes[i].sd_state; 553 554 /* generate geometry information */ 555 if (sc->mlx_geom == MLX_GEOM_128_32) { 556 dr->ms_heads = 128; 557 dr->ms_sectors = 32; 558 dr->ms_cylinders = dr->ms_size / (128 * 32); 559 } else { /* MLX_GEOM_255/63 */ 560 dr->ms_heads = 255; 561 dr->ms_sectors = 63; 562 dr->ms_cylinders = dr->ms_size / (255 * 63); 563 } 564 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1); 565 if (dr->ms_disk == 0) 566 device_printf(sc->mlx_dev, "device_add_child failed\n"); 567 device_set_ivars(dr->ms_disk, dr); 568 } 569 } 570 free(mes, M_DEVBUF); 571 if ((error = bus_generic_attach(sc->mlx_dev)) != 0) 572 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error); 573 574 /* mark controller back up */ 575 MLX_IO_LOCK(sc); 576 sc->mlx_state &= ~MLX_STATE_SHUTDOWN; 577 578 /* enable interrupts */ 579 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE); 580 MLX_IO_UNLOCK(sc); 581 MLX_CONFIG_UNLOCK(sc); 582 } 583 584 /******************************************************************************** 585 * Disconnect from the controller completely, in preparation for unload. 586 */ 587 int 588 mlx_detach(device_t dev) 589 { 590 struct mlx_softc *sc = device_get_softc(dev); 591 struct mlxd_softc *mlxd; 592 int i, error; 593 594 debug_called(1); 595 596 error = EBUSY; 597 MLX_CONFIG_LOCK(sc); 598 if (sc->mlx_state & MLX_STATE_OPEN) 599 goto out; 600 601 for (i = 0; i < MLX_MAXDRIVES; i++) { 602 if (sc->mlx_sysdrive[i].ms_disk != 0) { 603 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk); 604 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */ 605 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n"); 606 goto out; 607 } 608 } 609 } 610 if ((error = mlx_shutdown(dev))) 611 goto out; 612 MLX_CONFIG_UNLOCK(sc); 613 614 mlx_free(sc); 615 616 return (0); 617 out: 618 MLX_CONFIG_UNLOCK(sc); 619 return(error); 620 } 621 622 /******************************************************************************** 623 * Bring the controller down to a dormant state and detach all child devices. 624 * 625 * This function is called before detach, system shutdown, or before performing 626 * an operation which may add or delete system disks. (Call mlx_startup to 627 * resume normal operation.) 628 * 629 * Note that we can assume that the bioq on the controller is empty, as we won't 630 * allow shutdown if any device is open. 631 */ 632 int 633 mlx_shutdown(device_t dev) 634 { 635 struct mlx_softc *sc = device_get_softc(dev); 636 int error; 637 638 MLX_CONFIG_LOCK(sc); 639 error = mlx_shutdown_locked(sc); 640 MLX_CONFIG_UNLOCK(sc); 641 return (error); 642 } 643 644 static int 645 mlx_shutdown_locked(struct mlx_softc *sc) 646 { 647 int i, error; 648 649 debug_called(1); 650 651 MLX_CONFIG_ASSERT_LOCKED(sc); 652 653 MLX_IO_LOCK(sc); 654 sc->mlx_state |= MLX_STATE_SHUTDOWN; 655 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 656 657 /* flush controller */ 658 device_printf(sc->mlx_dev, "flushing cache..."); 659 if (mlx_flush(sc)) { 660 printf("failed\n"); 661 } else { 662 printf("done\n"); 663 } 664 MLX_IO_UNLOCK(sc); 665 666 /* delete all our child devices */ 667 for (i = 0; i < MLX_MAXDRIVES; i++) { 668 if (sc->mlx_sysdrive[i].ms_disk != 0) { 669 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0) 670 return (error); 671 sc->mlx_sysdrive[i].ms_disk = 0; 672 } 673 } 674 675 return (0); 676 } 677 678 /******************************************************************************** 679 * Bring the controller to a quiescent state, ready for system suspend. 680 */ 681 int 682 mlx_suspend(device_t dev) 683 { 684 struct mlx_softc *sc = device_get_softc(dev); 685 686 debug_called(1); 687 688 MLX_IO_LOCK(sc); 689 sc->mlx_state |= MLX_STATE_SUSPEND; 690 691 /* flush controller */ 692 device_printf(sc->mlx_dev, "flushing cache..."); 693 printf("%s\n", mlx_flush(sc) ? "failed" : "done"); 694 695 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 696 MLX_IO_UNLOCK(sc); 697 698 return(0); 699 } 700 701 /******************************************************************************** 702 * Bring the controller back to a state ready for operation. 703 */ 704 int 705 mlx_resume(device_t dev) 706 { 707 struct mlx_softc *sc = device_get_softc(dev); 708 709 debug_called(1); 710 711 MLX_IO_LOCK(sc); 712 sc->mlx_state &= ~MLX_STATE_SUSPEND; 713 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE); 714 MLX_IO_UNLOCK(sc); 715 716 return(0); 717 } 718 719 /******************************************************************************* 720 * Take an interrupt, or be poked by other code to look for interrupt-worthy 721 * status. 722 */ 723 void 724 mlx_intr(void *arg) 725 { 726 struct mlx_softc *sc = (struct mlx_softc *)arg; 727 728 debug_called(1); 729 730 /* collect finished commands, queue anything waiting */ 731 MLX_IO_LOCK(sc); 732 mlx_done(sc, 1); 733 MLX_IO_UNLOCK(sc); 734 }; 735 736 /******************************************************************************* 737 * Receive a buf structure from a child device and queue it on a particular 738 * disk resource, then poke the disk resource to start as much work as it can. 739 */ 740 int 741 mlx_submit_buf(struct mlx_softc *sc, struct bio *bp) 742 { 743 744 debug_called(1); 745 746 MLX_IO_ASSERT_LOCKED(sc); 747 bioq_insert_tail(&sc->mlx_bioq, bp); 748 sc->mlx_waitbufs++; 749 mlx_startio(sc); 750 return(0); 751 } 752 753 /******************************************************************************** 754 * Accept an open operation on the control device. 755 */ 756 int 757 mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td) 758 { 759 struct mlx_softc *sc = dev->si_drv1; 760 761 MLX_CONFIG_LOCK(sc); 762 MLX_IO_LOCK(sc); 763 sc->mlx_state |= MLX_STATE_OPEN; 764 MLX_IO_UNLOCK(sc); 765 MLX_CONFIG_UNLOCK(sc); 766 return(0); 767 } 768 769 /******************************************************************************** 770 * Accept the last close on the control device. 771 */ 772 int 773 mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td) 774 { 775 struct mlx_softc *sc = dev->si_drv1; 776 777 MLX_CONFIG_LOCK(sc); 778 MLX_IO_LOCK(sc); 779 sc->mlx_state &= ~MLX_STATE_OPEN; 780 MLX_IO_UNLOCK(sc); 781 MLX_CONFIG_UNLOCK(sc); 782 return (0); 783 } 784 785 /******************************************************************************** 786 * Handle controller-specific control operations. 787 */ 788 int 789 mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 790 { 791 struct mlx_softc *sc = dev->si_drv1; 792 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr; 793 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr; 794 int *arg = (int *)addr; 795 struct mlx_pause *mp; 796 struct mlx_sysdrive *dr; 797 struct mlxd_softc *mlxd; 798 int i, error; 799 800 switch(cmd) { 801 /* 802 * Enumerate connected system drives; returns the first system drive's 803 * unit number if *arg is -1, or the next unit after *arg if it's 804 * a valid unit on this controller. 805 */ 806 case MLX_NEXT_CHILD: 807 /* search system drives */ 808 MLX_CONFIG_LOCK(sc); 809 for (i = 0; i < MLX_MAXDRIVES; i++) { 810 /* is this one attached? */ 811 if (sc->mlx_sysdrive[i].ms_disk != 0) { 812 /* looking for the next one we come across? */ 813 if (*arg == -1) { 814 *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk); 815 MLX_CONFIG_UNLOCK(sc); 816 return(0); 817 } 818 /* we want the one after this one */ 819 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk)) 820 *arg = -1; 821 } 822 } 823 MLX_CONFIG_UNLOCK(sc); 824 return(ENOENT); 825 826 /* 827 * Scan the controller to see whether new drives have appeared. 828 */ 829 case MLX_RESCAN_DRIVES: 830 mtx_lock(&Giant); 831 mlx_startup(sc); 832 mtx_unlock(&Giant); 833 return(0); 834 835 /* 836 * Disconnect from the specified drive; it may be about to go 837 * away. 838 */ 839 case MLX_DETACH_DRIVE: /* detach one drive */ 840 MLX_CONFIG_LOCK(sc); 841 if (((dr = mlx_findunit(sc, *arg)) == NULL) || 842 ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) { 843 MLX_CONFIG_UNLOCK(sc); 844 return(ENOENT); 845 } 846 847 device_printf(dr->ms_disk, "detaching..."); 848 error = 0; 849 if (mlxd->mlxd_flags & MLXD_OPEN) { 850 error = EBUSY; 851 goto detach_out; 852 } 853 854 /* flush controller */ 855 MLX_IO_LOCK(sc); 856 if (mlx_flush(sc)) { 857 MLX_IO_UNLOCK(sc); 858 error = EBUSY; 859 goto detach_out; 860 } 861 MLX_IO_UNLOCK(sc); 862 863 /* nuke drive */ 864 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0) 865 goto detach_out; 866 dr->ms_disk = 0; 867 868 detach_out: 869 MLX_CONFIG_UNLOCK(sc); 870 if (error) { 871 printf("failed\n"); 872 } else { 873 printf("done\n"); 874 } 875 return(error); 876 877 /* 878 * Pause one or more SCSI channels for a period of time, to assist 879 * in the process of hot-swapping devices. 880 * 881 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem 882 * to do this right. 883 */ 884 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */ 885 /* Does this command work on this firmware? */ 886 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS)) 887 return(EOPNOTSUPP); 888 889 /* check time values */ 890 mp = (struct mlx_pause *)addr; 891 if ((mp->mp_when < 0) || (mp->mp_when > 3600)) 892 return(EINVAL); 893 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30))) 894 return(EINVAL); 895 896 MLX_IO_LOCK(sc); 897 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) { 898 /* cancel a pending pause operation */ 899 sc->mlx_pause.mp_which = 0; 900 } else { 901 /* fix for legal channels */ 902 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1); 903 904 /* check for a pause currently running */ 905 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) { 906 MLX_IO_UNLOCK(sc); 907 return(EBUSY); 908 } 909 910 /* looks ok, go with it */ 911 sc->mlx_pause.mp_which = mp->mp_which; 912 sc->mlx_pause.mp_when = time_second + mp->mp_when; 913 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong; 914 } 915 MLX_IO_UNLOCK(sc); 916 return(0); 917 918 /* 919 * Accept a command passthrough-style. 920 */ 921 case MLX_COMMAND: 922 return(mlx_user_command(sc, (struct mlx_usercommand *)addr)); 923 924 /* 925 * Start a rebuild on a given SCSI disk 926 */ 927 case MLX_REBUILDASYNC: 928 MLX_IO_LOCK(sc); 929 if (sc->mlx_background != 0) { 930 MLX_IO_UNLOCK(sc); 931 rb->rr_status = 0x0106; 932 return(EBUSY); 933 } 934 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target); 935 switch (rb->rr_status) { 936 case 0: 937 error = 0; 938 break; 939 case 0x10000: 940 error = ENOMEM; /* couldn't set up the command */ 941 break; 942 case 0x0002: 943 error = EBUSY; 944 break; 945 case 0x0104: 946 error = EIO; 947 break; 948 case 0x0105: 949 error = ERANGE; 950 break; 951 case 0x0106: 952 error = EBUSY; 953 break; 954 default: 955 error = EINVAL; 956 break; 957 } 958 if (error == 0) 959 sc->mlx_background = MLX_BACKGROUND_REBUILD; 960 MLX_IO_UNLOCK(sc); 961 return(error); 962 963 /* 964 * Get the status of the current rebuild or consistency check. 965 */ 966 case MLX_REBUILDSTAT: 967 MLX_IO_LOCK(sc); 968 *rs = sc->mlx_rebuildstat; 969 MLX_IO_UNLOCK(sc); 970 return(0); 971 972 /* 973 * Return the per-controller system drive number matching the 974 * disk device number in (arg), if it happens to belong to us. 975 */ 976 case MLX_GET_SYSDRIVE: 977 error = ENOENT; 978 MLX_CONFIG_LOCK(sc); 979 mtx_lock(&Giant); 980 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg); 981 mtx_unlock(&Giant); 982 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) && 983 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) { 984 error = 0; 985 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive; 986 } 987 MLX_CONFIG_UNLOCK(sc); 988 return(error); 989 990 default: 991 return(ENOTTY); 992 } 993 } 994 995 /******************************************************************************** 996 * Handle operations requested by a System Drive connected to this controller. 997 */ 998 int 999 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd, 1000 caddr_t addr, int32_t flag, struct thread *td) 1001 { 1002 int *arg = (int *)addr; 1003 int error, result; 1004 1005 switch(cmd) { 1006 /* 1007 * Return the current status of this drive. 1008 */ 1009 case MLXD_STATUS: 1010 MLX_IO_LOCK(sc); 1011 *arg = drive->ms_state; 1012 MLX_IO_UNLOCK(sc); 1013 return(0); 1014 1015 /* 1016 * Start a background consistency check on this drive. 1017 */ 1018 case MLXD_CHECKASYNC: /* start a background consistency check */ 1019 MLX_IO_LOCK(sc); 1020 if (sc->mlx_background != 0) { 1021 MLX_IO_UNLOCK(sc); 1022 *arg = 0x0106; 1023 return(EBUSY); 1024 } 1025 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]); 1026 switch (result) { 1027 case 0: 1028 error = 0; 1029 break; 1030 case 0x10000: 1031 error = ENOMEM; /* couldn't set up the command */ 1032 break; 1033 case 0x0002: 1034 error = EIO; 1035 break; 1036 case 0x0105: 1037 error = ERANGE; 1038 break; 1039 case 0x0106: 1040 error = EBUSY; 1041 break; 1042 default: 1043 error = EINVAL; 1044 break; 1045 } 1046 if (error == 0) 1047 sc->mlx_background = MLX_BACKGROUND_CHECK; 1048 MLX_IO_UNLOCK(sc); 1049 *arg = result; 1050 return(error); 1051 1052 } 1053 return(ENOIOCTL); 1054 } 1055 1056 1057 /******************************************************************************** 1058 ******************************************************************************** 1059 Status Monitoring 1060 ******************************************************************************** 1061 ********************************************************************************/ 1062 1063 /******************************************************************************** 1064 * Fire off commands to periodically check the status of connected drives. 1065 */ 1066 static void 1067 mlx_periodic(void *data) 1068 { 1069 struct mlx_softc *sc = (struct mlx_softc *)data; 1070 1071 debug_called(1); 1072 MLX_IO_ASSERT_LOCKED(sc); 1073 1074 /* 1075 * Run a bus pause? 1076 */ 1077 if ((sc->mlx_pause.mp_which != 0) && 1078 (sc->mlx_pause.mp_when > 0) && 1079 (time_second >= sc->mlx_pause.mp_when)){ 1080 1081 mlx_pause_action(sc); /* pause is running */ 1082 sc->mlx_pause.mp_when = 0; 1083 sysbeep(500, hz); 1084 1085 /* 1086 * Bus pause still running? 1087 */ 1088 } else if ((sc->mlx_pause.mp_which != 0) && 1089 (sc->mlx_pause.mp_when == 0)) { 1090 1091 /* time to stop bus pause? */ 1092 if (time_second >= sc->mlx_pause.mp_howlong) { 1093 mlx_pause_action(sc); 1094 sc->mlx_pause.mp_which = 0; /* pause is complete */ 1095 sysbeep(500, hz); 1096 } else { 1097 sysbeep((time_second % 5) * 100 + 500, hz/8); 1098 } 1099 1100 /* 1101 * Run normal periodic activities? 1102 */ 1103 } else if (time_second > (sc->mlx_lastpoll + 10)) { 1104 sc->mlx_lastpoll = time_second; 1105 1106 /* 1107 * Check controller status. 1108 * 1109 * XXX Note that this may not actually launch a command in situations of high load. 1110 */ 1111 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY, 1112 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry); 1113 1114 /* 1115 * Check system drive status. 1116 * 1117 * XXX This might be better left to event-driven detection, eg. I/O to an offline 1118 * drive will detect it's offline, rebuilds etc. should detect the drive is back 1119 * online. 1120 */ 1121 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES, 1122 mlx_periodic_enquiry); 1123 1124 } 1125 1126 /* get drive rebuild/check status */ 1127 /* XXX should check sc->mlx_background if this is only valid while in progress */ 1128 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild); 1129 1130 /* deal with possibly-missed interrupts and timed-out commands */ 1131 mlx_done(sc, 1); 1132 1133 /* reschedule another poll next second or so */ 1134 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc); 1135 } 1136 1137 /******************************************************************************** 1138 * Handle the result of an ENQUIRY command instigated by periodic status polling. 1139 */ 1140 static void 1141 mlx_periodic_enquiry(struct mlx_command *mc) 1142 { 1143 struct mlx_softc *sc = mc->mc_sc; 1144 1145 debug_called(1); 1146 MLX_IO_ASSERT_LOCKED(sc); 1147 1148 /* Command completed OK? */ 1149 if (mc->mc_status != 0) { 1150 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc)); 1151 goto out; 1152 } 1153 1154 /* respond to command */ 1155 switch(mc->mc_mailbox[0]) { 1156 /* 1157 * This is currently a bit fruitless, as we don't know how to extract the eventlog 1158 * pointer yet. 1159 */ 1160 case MLX_CMD_ENQUIRY_OLD: 1161 { 1162 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data; 1163 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data; 1164 int i; 1165 1166 /* convert data in-place to new format */ 1167 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) { 1168 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan; 1169 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ; 1170 } 1171 me->me_misc_flags = 0; 1172 me->me_rebuild_count = meo->me_rebuild_count; 1173 me->me_dead_count = meo->me_dead_count; 1174 me->me_critical_sd_count = meo->me_critical_sd_count; 1175 me->me_event_log_seq_num = 0; 1176 me->me_offline_sd_count = meo->me_offline_sd_count; 1177 me->me_max_commands = meo->me_max_commands; 1178 me->me_rebuild_flag = meo->me_rebuild_flag; 1179 me->me_fwmajor = meo->me_fwmajor; 1180 me->me_fwminor = meo->me_fwminor; 1181 me->me_status_flags = meo->me_status_flags; 1182 me->me_flash_age = meo->me_flash_age; 1183 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) { 1184 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) { 1185 me->me_drvsize[i] = 0; /* drive beyond supported range */ 1186 } else { 1187 me->me_drvsize[i] = meo->me_drvsize[i]; 1188 } 1189 } 1190 me->me_num_sys_drvs = meo->me_num_sys_drvs; 1191 } 1192 /* FALLTHROUGH */ 1193 1194 /* 1195 * Generic controller status update. We could do more with this than just 1196 * checking the event log. 1197 */ 1198 case MLX_CMD_ENQUIRY: 1199 { 1200 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data; 1201 1202 if (sc->mlx_currevent == -1) { 1203 /* initialise our view of the event log */ 1204 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num; 1205 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) { 1206 /* record where current events are up to */ 1207 sc->mlx_currevent = me->me_event_log_seq_num; 1208 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent); 1209 1210 /* mark the event log as busy */ 1211 sc->mlx_flags |= MLX_EVENTLOG_BUSY; 1212 1213 /* drain new eventlog entries */ 1214 mlx_periodic_eventlog_poll(sc); 1215 } 1216 break; 1217 } 1218 case MLX_CMD_ENQSYSDRIVE: 1219 { 1220 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data; 1221 struct mlx_sysdrive *dr; 1222 int i; 1223 1224 for (i = 0, dr = &sc->mlx_sysdrive[0]; 1225 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff); 1226 i++) { 1227 1228 /* has state been changed by controller? */ 1229 if (dr->ms_state != mes[i].sd_state) { 1230 switch(mes[i].sd_state) { 1231 case MLX_SYSD_OFFLINE: 1232 device_printf(dr->ms_disk, "drive offline\n"); 1233 break; 1234 case MLX_SYSD_ONLINE: 1235 device_printf(dr->ms_disk, "drive online\n"); 1236 break; 1237 case MLX_SYSD_CRITICAL: 1238 device_printf(dr->ms_disk, "drive critical\n"); 1239 break; 1240 } 1241 /* save new state */ 1242 dr->ms_state = mes[i].sd_state; 1243 } 1244 } 1245 break; 1246 } 1247 default: 1248 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]); 1249 break; 1250 } 1251 1252 out: 1253 free(mc->mc_data, M_DEVBUF); 1254 mlx_releasecmd(mc); 1255 } 1256 1257 static void 1258 mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 1259 { 1260 struct mlx_command *mc; 1261 1262 mc = (struct mlx_command *)arg; 1263 mlx_setup_dmamap(mc, segs, nsegments, error); 1264 1265 /* build the command to get one entry */ 1266 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, 1267 mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0); 1268 mc->mc_complete = mlx_periodic_eventlog_respond; 1269 mc->mc_private = mc; 1270 1271 /* start the command */ 1272 if (mlx_start(mc) != 0) { 1273 mlx_releasecmd(mc); 1274 free(mc->mc_data, M_DEVBUF); 1275 mc->mc_data = NULL; 1276 } 1277 1278 } 1279 1280 /******************************************************************************** 1281 * Instigate a poll for one event log message on (sc). 1282 * We only poll for one message at a time, to keep our command usage down. 1283 */ 1284 static void 1285 mlx_periodic_eventlog_poll(struct mlx_softc *sc) 1286 { 1287 struct mlx_command *mc; 1288 void *result = NULL; 1289 int error = 0; 1290 1291 debug_called(1); 1292 MLX_IO_ASSERT_LOCKED(sc); 1293 1294 /* get ourselves a command buffer */ 1295 error = 1; 1296 if ((mc = mlx_alloccmd(sc)) == NULL) 1297 goto out; 1298 1299 /* allocate the response structure */ 1300 if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF, 1301 M_NOWAIT)) == NULL) 1302 goto out; 1303 1304 /* get a command slot */ 1305 if (mlx_getslot(mc)) 1306 goto out; 1307 1308 /* map the command so the controller can see it */ 1309 mc->mc_data = result; 1310 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024; 1311 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 1312 mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT); 1313 1314 out: 1315 if (error != 0) { 1316 if (mc != NULL) 1317 mlx_releasecmd(mc); 1318 if ((result != NULL) && (mc->mc_data != NULL)) 1319 free(result, M_DEVBUF); 1320 } 1321 } 1322 1323 /******************************************************************************** 1324 * Handle the result of polling for a log message, generate diagnostic output. 1325 * If this wasn't the last message waiting for us, we'll go collect another. 1326 */ 1327 static char *mlx_sense_messages[] = { 1328 "because write recovery failed", 1329 "because of SCSI bus reset failure", 1330 "because of double check condition", 1331 "because it was removed", 1332 "because of gross error on SCSI chip", 1333 "because of bad tag returned from drive", 1334 "because of timeout on SCSI command", 1335 "because of reset SCSI command issued from system", 1336 "because busy or parity error count exceeded limit", 1337 "because of 'kill drive' command from system", 1338 "because of selection timeout", 1339 "due to SCSI phase sequence error", 1340 "due to unknown status" 1341 }; 1342 1343 static void 1344 mlx_periodic_eventlog_respond(struct mlx_command *mc) 1345 { 1346 struct mlx_softc *sc = mc->mc_sc; 1347 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data; 1348 char *reason; 1349 1350 debug_called(1); 1351 MLX_IO_ASSERT_LOCKED(sc); 1352 1353 sc->mlx_lastevent++; /* next message... */ 1354 if (mc->mc_status == 0) { 1355 1356 /* handle event log message */ 1357 switch(el->el_type) { 1358 /* 1359 * This is the only sort of message we understand at the moment. 1360 * The tests here are probably incomplete. 1361 */ 1362 case MLX_LOGMSG_SENSE: /* sense data */ 1363 /* Mylex vendor-specific message indicating a drive was killed? */ 1364 if ((el->el_sensekey == 9) && 1365 (el->el_asc == 0x80)) { 1366 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) { 1367 reason = mlx_sense_messages[el->el_asq]; 1368 } else { 1369 reason = "for unknown reason"; 1370 } 1371 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n", 1372 el->el_channel, el->el_target, reason); 1373 } 1374 /* SCSI drive was reset? */ 1375 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) { 1376 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n", 1377 el->el_channel, el->el_target); 1378 } 1379 /* SCSI drive error? */ 1380 if (!((el->el_sensekey == 0) || 1381 ((el->el_sensekey == 2) && 1382 (el->el_asc == 0x04) && 1383 ((el->el_asq == 0x01) || 1384 (el->el_asq == 0x02))))) { 1385 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n", 1386 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq); 1387 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":"); 1388 } 1389 break; 1390 1391 default: 1392 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type); 1393 break; 1394 } 1395 } else { 1396 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc)); 1397 /* give up on all the outstanding messages, as we may have come unsynched */ 1398 sc->mlx_lastevent = sc->mlx_currevent; 1399 } 1400 1401 /* dispose of command and data */ 1402 free(mc->mc_data, M_DEVBUF); 1403 mlx_releasecmd(mc); 1404 1405 /* is there another message to obtain? */ 1406 if (sc->mlx_lastevent != sc->mlx_currevent) { 1407 mlx_periodic_eventlog_poll(sc); 1408 } else { 1409 /* clear log-busy status */ 1410 sc->mlx_flags &= ~MLX_EVENTLOG_BUSY; 1411 } 1412 } 1413 1414 /******************************************************************************** 1415 * Handle check/rebuild operations in progress. 1416 */ 1417 static void 1418 mlx_periodic_rebuild(struct mlx_command *mc) 1419 { 1420 struct mlx_softc *sc = mc->mc_sc; 1421 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data; 1422 1423 MLX_IO_ASSERT_LOCKED(sc); 1424 switch(mc->mc_status) { 1425 case 0: /* operation running, update stats */ 1426 sc->mlx_rebuildstat = *mr; 1427 1428 /* spontaneous rebuild/check? */ 1429 if (sc->mlx_background == 0) { 1430 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS; 1431 device_printf(sc->mlx_dev, "background check/rebuild operation started\n"); 1432 } 1433 break; 1434 1435 case 0x0105: /* nothing running, finalise stats and report */ 1436 switch(sc->mlx_background) { 1437 case MLX_BACKGROUND_CHECK: 1438 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */ 1439 break; 1440 case MLX_BACKGROUND_REBUILD: 1441 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */ 1442 break; 1443 case MLX_BACKGROUND_SPONTANEOUS: 1444 default: 1445 /* if we have previously been non-idle, report the transition */ 1446 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) { 1447 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n"); 1448 } 1449 } 1450 sc->mlx_background = 0; 1451 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE; 1452 break; 1453 } 1454 free(mc->mc_data, M_DEVBUF); 1455 mlx_releasecmd(mc); 1456 } 1457 1458 /******************************************************************************** 1459 ******************************************************************************** 1460 Channel Pause 1461 ******************************************************************************** 1462 ********************************************************************************/ 1463 1464 /******************************************************************************** 1465 * It's time to perform a channel pause action for (sc), either start or stop 1466 * the pause. 1467 */ 1468 static void 1469 mlx_pause_action(struct mlx_softc *sc) 1470 { 1471 struct mlx_command *mc; 1472 int failsafe, i, command; 1473 1474 MLX_IO_ASSERT_LOCKED(sc); 1475 1476 /* What are we doing here? */ 1477 if (sc->mlx_pause.mp_when == 0) { 1478 command = MLX_CMD_STARTCHANNEL; 1479 failsafe = 0; 1480 1481 } else { 1482 command = MLX_CMD_STOPCHANNEL; 1483 1484 /* 1485 * Channels will always start again after the failsafe period, 1486 * which is specified in multiples of 30 seconds. 1487 * This constrains us to a maximum pause of 450 seconds. 1488 */ 1489 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30; 1490 if (failsafe > 0xf) { 1491 failsafe = 0xf; 1492 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5; 1493 } 1494 } 1495 1496 /* build commands for every channel requested */ 1497 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) { 1498 if ((1 << i) & sc->mlx_pause.mp_which) { 1499 1500 /* get ourselves a command buffer */ 1501 if ((mc = mlx_alloccmd(sc)) == NULL) 1502 goto fail; 1503 /* get a command slot */ 1504 mc->mc_flags |= MLX_CMD_PRIORITY; 1505 if (mlx_getslot(mc)) 1506 goto fail; 1507 1508 /* build the command */ 1509 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0); 1510 mc->mc_complete = mlx_pause_done; 1511 mc->mc_private = sc; /* XXX not needed */ 1512 if (mlx_start(mc)) 1513 goto fail; 1514 /* command submitted OK */ 1515 return; 1516 1517 fail: 1518 device_printf(sc->mlx_dev, "%s failed for channel %d\n", 1519 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i); 1520 if (mc != NULL) 1521 mlx_releasecmd(mc); 1522 } 1523 } 1524 } 1525 1526 static void 1527 mlx_pause_done(struct mlx_command *mc) 1528 { 1529 struct mlx_softc *sc = mc->mc_sc; 1530 int command = mc->mc_mailbox[0]; 1531 int channel = mc->mc_mailbox[2] & 0xf; 1532 1533 MLX_IO_ASSERT_LOCKED(sc); 1534 if (mc->mc_status != 0) { 1535 device_printf(sc->mlx_dev, "%s command failed - %s\n", 1536 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc)); 1537 } else if (command == MLX_CMD_STOPCHANNEL) { 1538 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n", 1539 channel, (long)(sc->mlx_pause.mp_howlong - time_second)); 1540 } else { 1541 device_printf(sc->mlx_dev, "channel %d resuming\n", channel); 1542 } 1543 mlx_releasecmd(mc); 1544 } 1545 1546 /******************************************************************************** 1547 ******************************************************************************** 1548 Command Submission 1549 ******************************************************************************** 1550 ********************************************************************************/ 1551 1552 static void 1553 mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 1554 { 1555 struct mlx_softc *sc; 1556 struct mlx_command *mc; 1557 1558 mc = (struct mlx_command *)arg; 1559 if (error) 1560 return; 1561 1562 mlx_setup_dmamap(mc, segs, nsegments, error); 1563 1564 /* build an enquiry command */ 1565 sc = mc->mc_sc; 1566 mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0); 1567 1568 /* do we want a completion callback? */ 1569 if (mc->mc_complete != NULL) { 1570 if ((error = mlx_start(mc)) != 0) 1571 return; 1572 } else { 1573 /* run the command in either polled or wait mode */ 1574 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : 1575 mlx_poll_command(mc)) 1576 return; 1577 1578 /* command completed OK? */ 1579 if (mc->mc_status != 0) { 1580 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", 1581 mlx_diagnose_command(mc)); 1582 return; 1583 } 1584 } 1585 } 1586 1587 /******************************************************************************** 1588 * Perform an Enquiry command using a type-3 command buffer and a return a single 1589 * linear result buffer. If the completion function is specified, it will 1590 * be called with the completed command (and the result response will not be 1591 * valid until that point). Otherwise, the command will either be busy-waited 1592 * for (interrupts not enabled), or slept for. 1593 */ 1594 static void * 1595 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc)) 1596 { 1597 struct mlx_command *mc; 1598 void *result; 1599 int error; 1600 1601 debug_called(1); 1602 MLX_IO_ASSERT_LOCKED(sc); 1603 1604 /* get ourselves a command buffer */ 1605 error = 1; 1606 result = NULL; 1607 if ((mc = mlx_alloccmd(sc)) == NULL) 1608 goto out; 1609 /* allocate the response structure */ 1610 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL) 1611 goto out; 1612 /* get a command slot */ 1613 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT; 1614 if (mlx_getslot(mc)) 1615 goto out; 1616 1617 /* map the command so the controller can see it */ 1618 mc->mc_data = result; 1619 mc->mc_length = bufsize; 1620 mc->mc_command = command; 1621 1622 if (complete != NULL) { 1623 mc->mc_complete = complete; 1624 mc->mc_private = mc; 1625 } 1626 1627 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 1628 mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT); 1629 1630 out: 1631 /* we got a command, but nobody else will free it */ 1632 if ((mc != NULL) && (mc->mc_complete == NULL)) 1633 mlx_releasecmd(mc); 1634 /* we got an error, and we allocated a result */ 1635 if ((error != 0) && (result != NULL)) { 1636 free(result, M_DEVBUF); 1637 result = NULL; 1638 } 1639 return(result); 1640 } 1641 1642 1643 /******************************************************************************** 1644 * Perform a Flush command on the nominated controller. 1645 * 1646 * May be called with interrupts enabled or disabled; will not return until 1647 * the flush operation completes or fails. 1648 */ 1649 static int 1650 mlx_flush(struct mlx_softc *sc) 1651 { 1652 struct mlx_command *mc; 1653 int error; 1654 1655 debug_called(1); 1656 MLX_IO_ASSERT_LOCKED(sc); 1657 1658 /* get ourselves a command buffer */ 1659 error = 1; 1660 if ((mc = mlx_alloccmd(sc)) == NULL) 1661 goto out; 1662 /* get a command slot */ 1663 if (mlx_getslot(mc)) 1664 goto out; 1665 1666 /* build a flush command */ 1667 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0); 1668 1669 /* can't assume that interrupts are going to work here, so play it safe */ 1670 if (mlx_poll_command(mc)) 1671 goto out; 1672 1673 /* command completed OK? */ 1674 if (mc->mc_status != 0) { 1675 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc)); 1676 goto out; 1677 } 1678 1679 error = 0; /* success */ 1680 out: 1681 if (mc != NULL) 1682 mlx_releasecmd(mc); 1683 return(error); 1684 } 1685 1686 /******************************************************************************** 1687 * Start a background consistency check on (drive). 1688 * 1689 * May be called with interrupts enabled or disabled; will return as soon as the 1690 * operation has started or been refused. 1691 */ 1692 static int 1693 mlx_check(struct mlx_softc *sc, int drive) 1694 { 1695 struct mlx_command *mc; 1696 int error; 1697 1698 debug_called(1); 1699 MLX_IO_ASSERT_LOCKED(sc); 1700 1701 /* get ourselves a command buffer */ 1702 error = 0x10000; 1703 if ((mc = mlx_alloccmd(sc)) == NULL) 1704 goto out; 1705 /* get a command slot */ 1706 if (mlx_getslot(mc)) 1707 goto out; 1708 1709 /* build a checkasync command, set the "fix it" flag */ 1710 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0); 1711 1712 /* start the command and wait for it to be returned */ 1713 if (mlx_wait_command(mc)) 1714 goto out; 1715 1716 /* command completed OK? */ 1717 if (mc->mc_status != 0) { 1718 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc)); 1719 } else { 1720 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started"); 1721 } 1722 error = mc->mc_status; 1723 1724 out: 1725 if (mc != NULL) 1726 mlx_releasecmd(mc); 1727 return(error); 1728 } 1729 1730 /******************************************************************************** 1731 * Start a background rebuild of the physical drive at (channel),(target). 1732 * 1733 * May be called with interrupts enabled or disabled; will return as soon as the 1734 * operation has started or been refused. 1735 */ 1736 static int 1737 mlx_rebuild(struct mlx_softc *sc, int channel, int target) 1738 { 1739 struct mlx_command *mc; 1740 int error; 1741 1742 debug_called(1); 1743 MLX_IO_ASSERT_LOCKED(sc); 1744 1745 /* get ourselves a command buffer */ 1746 error = 0x10000; 1747 if ((mc = mlx_alloccmd(sc)) == NULL) 1748 goto out; 1749 /* get a command slot */ 1750 if (mlx_getslot(mc)) 1751 goto out; 1752 1753 /* build a checkasync command, set the "fix it" flag */ 1754 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0); 1755 1756 /* start the command and wait for it to be returned */ 1757 if (mlx_wait_command(mc)) 1758 goto out; 1759 1760 /* command completed OK? */ 1761 if (mc->mc_status != 0) { 1762 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc)); 1763 } else { 1764 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target); 1765 } 1766 error = mc->mc_status; 1767 1768 out: 1769 if (mc != NULL) 1770 mlx_releasecmd(mc); 1771 return(error); 1772 } 1773 1774 /******************************************************************************** 1775 * Run the command (mc) and return when it completes. 1776 * 1777 * Interrupts need to be enabled; returns nonzero on error. 1778 */ 1779 static int 1780 mlx_wait_command(struct mlx_command *mc) 1781 { 1782 struct mlx_softc *sc = mc->mc_sc; 1783 int error, count; 1784 1785 debug_called(1); 1786 MLX_IO_ASSERT_LOCKED(sc); 1787 1788 mc->mc_complete = NULL; 1789 mc->mc_private = mc; /* wake us when you're done */ 1790 if ((error = mlx_start(mc)) != 0) 1791 return(error); 1792 1793 count = 0; 1794 /* XXX better timeout? */ 1795 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) { 1796 mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz); 1797 } 1798 1799 if (mc->mc_status != 0) { 1800 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc)); 1801 return(EIO); 1802 } 1803 return(0); 1804 } 1805 1806 1807 /******************************************************************************** 1808 * Start the command (mc) and busy-wait for it to complete. 1809 * 1810 * Should only be used when interrupts can't be relied upon. Returns 0 on 1811 * success, nonzero on error. 1812 * Successfully completed commands are dequeued. 1813 */ 1814 static int 1815 mlx_poll_command(struct mlx_command *mc) 1816 { 1817 struct mlx_softc *sc = mc->mc_sc; 1818 int error, count; 1819 1820 debug_called(1); 1821 MLX_IO_ASSERT_LOCKED(sc); 1822 1823 mc->mc_complete = NULL; 1824 mc->mc_private = NULL; /* we will poll for it */ 1825 if ((error = mlx_start(mc)) != 0) 1826 return(error); 1827 1828 count = 0; 1829 do { 1830 /* poll for completion */ 1831 mlx_done(mc->mc_sc, 1); 1832 1833 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000)); 1834 if (mc->mc_status != MLX_STATUS_BUSY) { 1835 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 1836 return(0); 1837 } 1838 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc)); 1839 return(EIO); 1840 } 1841 1842 void 1843 mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 1844 { 1845 struct mlx_command *mc; 1846 struct mlxd_softc *mlxd; 1847 struct mlx_softc *sc; 1848 struct bio *bp; 1849 int blkcount; 1850 int driveno; 1851 int cmd; 1852 1853 mc = (struct mlx_command *)arg; 1854 mlx_setup_dmamap(mc, segs, nsegments, error); 1855 1856 sc = mc->mc_sc; 1857 bp = mc->mc_private; 1858 1859 if (bp->bio_cmd == BIO_READ) { 1860 mc->mc_flags |= MLX_CMD_DATAIN; 1861 cmd = MLX_CMD_READSG; 1862 } else { 1863 mc->mc_flags |= MLX_CMD_DATAOUT; 1864 cmd = MLX_CMD_WRITESG; 1865 } 1866 1867 /* build a suitable I/O command (assumes 512-byte rounded transfers) */ 1868 mlxd = bp->bio_disk->d_drv1; 1869 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive; 1870 blkcount = (bp->bio_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE; 1871 1872 if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size) 1873 device_printf(sc->mlx_dev, 1874 "I/O beyond end of unit (%lld,%d > %lu)\n", 1875 (long long)bp->bio_pblkno, blkcount, 1876 (u_long)sc->mlx_sysdrive[driveno].ms_size); 1877 1878 /* 1879 * Build the I/O command. Note that the SG list type bits are set to zero, 1880 * denoting the format of SG list that we are using. 1881 */ 1882 if (sc->mlx_iftype == MLX_IFTYPE_2) { 1883 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : 1884 MLX_CMD_READSG_OLD, 1885 blkcount & 0xff, /* xfer length low byte */ 1886 bp->bio_pblkno, /* physical block number */ 1887 driveno, /* target drive number */ 1888 mc->mc_sgphys, /* location of SG list */ 1889 mc->mc_nsgent & 0x3f); /* size of SG list */ 1890 } else { 1891 mlx_make_type5(mc, cmd, 1892 blkcount & 0xff, /* xfer length low byte */ 1893 (driveno << 3) | ((blkcount >> 8) & 0x07), 1894 /* target+length high 3 bits */ 1895 bp->bio_pblkno, /* physical block number */ 1896 mc->mc_sgphys, /* location of SG list */ 1897 mc->mc_nsgent & 0x3f); /* size of SG list */ 1898 } 1899 1900 /* try to give command to controller */ 1901 if (mlx_start(mc) != 0) { 1902 /* fail the command */ 1903 mc->mc_status = MLX_STATUS_WEDGED; 1904 mlx_completeio(mc); 1905 } 1906 1907 sc->mlx_state &= ~MLX_STATE_QFROZEN; 1908 } 1909 1910 /******************************************************************************** 1911 * Pull as much work off the softc's work queue as possible and give it to the 1912 * controller. Leave a couple of slots free for emergencies. 1913 */ 1914 static void 1915 mlx_startio(struct mlx_softc *sc) 1916 { 1917 struct mlx_command *mc; 1918 struct bio *bp; 1919 int error; 1920 1921 MLX_IO_ASSERT_LOCKED(sc); 1922 1923 /* spin until something prevents us from doing any work */ 1924 for (;;) { 1925 if (sc->mlx_state & MLX_STATE_QFROZEN) 1926 break; 1927 1928 /* see if there's work to be done */ 1929 if ((bp = bioq_first(&sc->mlx_bioq)) == NULL) 1930 break; 1931 /* get a command */ 1932 if ((mc = mlx_alloccmd(sc)) == NULL) 1933 break; 1934 /* get a slot for the command */ 1935 if (mlx_getslot(mc) != 0) { 1936 mlx_releasecmd(mc); 1937 break; 1938 } 1939 /* get the buf containing our work */ 1940 bioq_remove(&sc->mlx_bioq, bp); 1941 sc->mlx_waitbufs--; 1942 1943 /* connect the buf to the command */ 1944 mc->mc_complete = mlx_completeio; 1945 mc->mc_private = bp; 1946 mc->mc_data = bp->bio_data; 1947 mc->mc_length = bp->bio_bcount; 1948 1949 /* map the command so the controller can work with it */ 1950 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 1951 mc->mc_length, mlx_startio_cb, mc, 0); 1952 if (error == EINPROGRESS) { 1953 sc->mlx_state |= MLX_STATE_QFROZEN; 1954 break; 1955 } 1956 } 1957 } 1958 1959 /******************************************************************************** 1960 * Handle completion of an I/O command. 1961 */ 1962 static void 1963 mlx_completeio(struct mlx_command *mc) 1964 { 1965 struct mlx_softc *sc = mc->mc_sc; 1966 struct bio *bp = mc->mc_private; 1967 struct mlxd_softc *mlxd = bp->bio_disk->d_drv1; 1968 1969 MLX_IO_ASSERT_LOCKED(sc); 1970 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */ 1971 bp->bio_error = EIO; 1972 bp->bio_flags |= BIO_ERROR; 1973 1974 switch(mc->mc_status) { 1975 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */ 1976 device_printf(mlxd->mlxd_dev, "drive offline\n"); 1977 /* should signal this with a return code */ 1978 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE; 1979 break; 1980 1981 default: /* other I/O error */ 1982 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc)); 1983 #if 0 1984 device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n", 1985 bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno); 1986 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " "); 1987 #endif 1988 break; 1989 } 1990 } 1991 mlx_releasecmd(mc); 1992 mlxd_intr(bp); 1993 } 1994 1995 void 1996 mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 1997 { 1998 struct mlx_usercommand *mu; 1999 struct mlx_command *mc; 2000 struct mlx_dcdb *dcdb; 2001 2002 mc = (struct mlx_command *)arg; 2003 if (error) 2004 return; 2005 2006 mlx_setup_dmamap(mc, segs, nsegments, error); 2007 2008 mu = (struct mlx_usercommand *)mc->mc_private; 2009 dcdb = NULL; 2010 2011 /* 2012 * If this is a passthrough SCSI command, the DCDB is packed at the 2013 * beginning of the data area. Fix up the DCDB to point to the correct 2014 * physical address and override any bufptr supplied by the caller since 2015 * we know what it's meant to be. 2016 */ 2017 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) { 2018 dcdb = (struct mlx_dcdb *)mc->mc_data; 2019 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb); 2020 mu->mu_bufptr = 8; 2021 } 2022 2023 /* 2024 * If there's a data buffer, fix up the command's buffer pointer. 2025 */ 2026 if (mu->mu_datasize > 0) { 2027 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff; 2028 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff; 2029 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff; 2030 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff; 2031 } 2032 debug(0, "command fixup"); 2033 2034 /* submit the command and wait */ 2035 if (mlx_wait_command(mc) != 0) 2036 return; 2037 2038 } 2039 2040 /******************************************************************************** 2041 * Take a command from user-space and try to run it. 2042 * 2043 * XXX Note that this can't perform very much in the way of error checking, and 2044 * as such, applications _must_ be considered trustworthy. 2045 * XXX Commands using S/G for data are not supported. 2046 */ 2047 static int 2048 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu) 2049 { 2050 struct mlx_command *mc; 2051 void *kbuf; 2052 int error; 2053 2054 debug_called(0); 2055 2056 kbuf = NULL; 2057 mc = NULL; 2058 error = ENOMEM; 2059 2060 /* get ourselves a command and copy in from user space */ 2061 MLX_IO_LOCK(sc); 2062 if ((mc = mlx_alloccmd(sc)) == NULL) { 2063 MLX_IO_UNLOCK(sc); 2064 return(error); 2065 } 2066 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox)); 2067 debug(0, "got command buffer"); 2068 2069 /* 2070 * if we need a buffer for data transfer, allocate one and copy in its 2071 * initial contents 2072 */ 2073 if (mu->mu_datasize > 0) { 2074 if (mu->mu_datasize > MLX_MAXPHYS) { 2075 error = EINVAL; 2076 goto out; 2077 } 2078 MLX_IO_UNLOCK(sc); 2079 if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) || 2080 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) { 2081 MLX_IO_LOCK(sc); 2082 goto out; 2083 } 2084 MLX_IO_LOCK(sc); 2085 debug(0, "got kernel buffer"); 2086 } 2087 2088 /* get a command slot */ 2089 if (mlx_getslot(mc)) 2090 goto out; 2091 debug(0, "got a slot"); 2092 2093 if (mu->mu_datasize > 0) { 2094 2095 /* range check the pointer to physical buffer address */ 2096 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - 2097 sizeof(u_int32_t)))) { 2098 error = EINVAL; 2099 goto out; 2100 } 2101 } 2102 2103 /* map the command so the controller can see it */ 2104 mc->mc_data = kbuf; 2105 mc->mc_length = mu->mu_datasize; 2106 mc->mc_private = mu; 2107 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 2108 mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT); 2109 if (error) 2110 goto out; 2111 2112 /* copy out status and data */ 2113 mu->mu_status = mc->mc_status; 2114 if (mu->mu_datasize > 0) { 2115 MLX_IO_UNLOCK(sc); 2116 error = copyout(kbuf, mu->mu_buf, mu->mu_datasize); 2117 MLX_IO_LOCK(sc); 2118 } 2119 2120 out: 2121 mlx_releasecmd(mc); 2122 MLX_IO_UNLOCK(sc); 2123 if (kbuf != NULL) 2124 free(kbuf, M_DEVBUF); 2125 return(error); 2126 } 2127 2128 /******************************************************************************** 2129 ******************************************************************************** 2130 Command I/O to Controller 2131 ******************************************************************************** 2132 ********************************************************************************/ 2133 2134 /******************************************************************************** 2135 * Find a free command slot for (mc). 2136 * 2137 * Don't hand out a slot to a normal-priority command unless there are at least 2138 * 4 slots free for priority commands. 2139 */ 2140 static int 2141 mlx_getslot(struct mlx_command *mc) 2142 { 2143 struct mlx_softc *sc = mc->mc_sc; 2144 int slot, limit; 2145 2146 debug_called(1); 2147 2148 MLX_IO_ASSERT_LOCKED(sc); 2149 2150 /* 2151 * Enforce slot-usage limit, if we have the required information. 2152 */ 2153 if (sc->mlx_enq2 != NULL) { 2154 limit = sc->mlx_enq2->me_max_commands; 2155 } else { 2156 limit = 2; 2157 } 2158 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4)) 2159 return(EBUSY); 2160 2161 /* 2162 * Allocate an outstanding command slot 2163 * 2164 * XXX linear search is slow 2165 */ 2166 for (slot = 0; slot < limit; slot++) { 2167 debug(2, "try slot %d", slot); 2168 if (sc->mlx_busycmd[slot] == NULL) 2169 break; 2170 } 2171 if (slot < limit) { 2172 sc->mlx_busycmd[slot] = mc; 2173 sc->mlx_busycmds++; 2174 } 2175 2176 /* out of slots? */ 2177 if (slot >= limit) 2178 return(EBUSY); 2179 2180 debug(2, "got slot %d", slot); 2181 mc->mc_slot = slot; 2182 return(0); 2183 } 2184 2185 /******************************************************************************** 2186 * Map/unmap (mc)'s data in the controller's addressable space. 2187 */ 2188 static void 2189 mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments, 2190 int error) 2191 { 2192 struct mlx_softc *sc = mc->mc_sc; 2193 struct mlx_sgentry *sg; 2194 int i; 2195 2196 debug_called(1); 2197 2198 /* XXX should be unnecessary */ 2199 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg)) 2200 panic("MLX: too many s/g segments (%d, max %d)", nsegments, 2201 sc->mlx_enq2->me_max_sg); 2202 2203 /* get base address of s/g table */ 2204 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG); 2205 2206 /* save s/g table information in command */ 2207 mc->mc_nsgent = nsegments; 2208 mc->mc_sgphys = sc->mlx_sgbusaddr + 2209 (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry)); 2210 mc->mc_dataphys = segs[0].ds_addr; 2211 2212 /* populate s/g table */ 2213 for (i = 0; i < nsegments; i++, sg++) { 2214 sg->sg_addr = segs[i].ds_addr; 2215 sg->sg_count = segs[i].ds_len; 2216 } 2217 2218 /* Make sure the buffers are visible on the bus. */ 2219 if (mc->mc_flags & MLX_CMD_DATAIN) 2220 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, 2221 BUS_DMASYNC_PREREAD); 2222 if (mc->mc_flags & MLX_CMD_DATAOUT) 2223 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, 2224 BUS_DMASYNC_PREWRITE); 2225 } 2226 2227 static void 2228 mlx_unmapcmd(struct mlx_command *mc) 2229 { 2230 struct mlx_softc *sc = mc->mc_sc; 2231 2232 debug_called(1); 2233 2234 /* if the command involved data at all */ 2235 if (mc->mc_data != NULL) { 2236 2237 if (mc->mc_flags & MLX_CMD_DATAIN) 2238 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD); 2239 if (mc->mc_flags & MLX_CMD_DATAOUT) 2240 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE); 2241 2242 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap); 2243 } 2244 } 2245 2246 /******************************************************************************** 2247 * Try to deliver (mc) to the controller. 2248 * 2249 * Can be called at any interrupt level, with or without interrupts enabled. 2250 */ 2251 static int 2252 mlx_start(struct mlx_command *mc) 2253 { 2254 struct mlx_softc *sc = mc->mc_sc; 2255 int i; 2256 2257 debug_called(1); 2258 2259 /* save the slot number as ident so we can handle this command when complete */ 2260 mc->mc_mailbox[0x1] = mc->mc_slot; 2261 2262 /* mark the command as currently being processed */ 2263 mc->mc_status = MLX_STATUS_BUSY; 2264 2265 /* set a default 60-second timeout XXX tunable? XXX not currently used */ 2266 mc->mc_timeout = time_second + 60; 2267 2268 /* spin waiting for the mailbox */ 2269 for (i = 100000; i > 0; i--) { 2270 if (sc->mlx_tryqueue(sc, mc)) { 2271 /* move command to work queue */ 2272 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link); 2273 return (0); 2274 } else if (i > 1) 2275 mlx_done(sc, 0); 2276 } 2277 2278 /* 2279 * We couldn't get the controller to take the command. Revoke the slot 2280 * that the command was given and return it with a bad status. 2281 */ 2282 sc->mlx_busycmd[mc->mc_slot] = NULL; 2283 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n"); 2284 mc->mc_status = MLX_STATUS_WEDGED; 2285 mlx_complete(sc); 2286 return(EIO); 2287 } 2288 2289 /******************************************************************************** 2290 * Poll the controller (sc) for completed commands. 2291 * Update command status and free slots for reuse. If any slots were freed, 2292 * new commands may be posted. 2293 * 2294 * Returns nonzero if one or more commands were completed. 2295 */ 2296 static int 2297 mlx_done(struct mlx_softc *sc, int startio) 2298 { 2299 struct mlx_command *mc; 2300 int result; 2301 u_int8_t slot; 2302 u_int16_t status; 2303 2304 debug_called(2); 2305 MLX_IO_ASSERT_LOCKED(sc); 2306 2307 result = 0; 2308 2309 /* loop collecting completed commands */ 2310 for (;;) { 2311 /* poll for a completed command's identifier and status */ 2312 if (sc->mlx_findcomplete(sc, &slot, &status)) { 2313 result = 1; 2314 mc = sc->mlx_busycmd[slot]; /* find command */ 2315 if (mc != NULL) { /* paranoia */ 2316 if (mc->mc_status == MLX_STATUS_BUSY) { 2317 mc->mc_status = status; /* save status */ 2318 2319 /* free slot for reuse */ 2320 sc->mlx_busycmd[slot] = NULL; 2321 sc->mlx_busycmds--; 2322 } else { 2323 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot); 2324 } 2325 } else { 2326 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot); 2327 } 2328 } else { 2329 break; 2330 } 2331 } 2332 2333 /* if we've completed any commands, try posting some more */ 2334 if (result && startio) 2335 mlx_startio(sc); 2336 2337 /* handle completion and timeouts */ 2338 mlx_complete(sc); 2339 2340 return(result); 2341 } 2342 2343 /******************************************************************************** 2344 * Perform post-completion processing for commands on (sc). 2345 */ 2346 static void 2347 mlx_complete(struct mlx_softc *sc) 2348 { 2349 struct mlx_command *mc, *nc; 2350 2351 debug_called(2); 2352 MLX_IO_ASSERT_LOCKED(sc); 2353 2354 /* scan the list of busy/done commands */ 2355 mc = TAILQ_FIRST(&sc->mlx_work); 2356 while (mc != NULL) { 2357 nc = TAILQ_NEXT(mc, mc_link); 2358 2359 /* Command has been completed in some fashion */ 2360 if (mc->mc_status != MLX_STATUS_BUSY) { 2361 2362 /* unmap the command's data buffer */ 2363 mlx_unmapcmd(mc); 2364 /* 2365 * Does the command have a completion handler? 2366 */ 2367 if (mc->mc_complete != NULL) { 2368 /* remove from list and give to handler */ 2369 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 2370 mc->mc_complete(mc); 2371 2372 /* 2373 * Is there a sleeper waiting on this command? 2374 */ 2375 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */ 2376 2377 /* remove from list and wake up sleeper */ 2378 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 2379 wakeup_one(mc->mc_private); 2380 2381 /* 2382 * Leave the command for a caller that's polling for it. 2383 */ 2384 } else { 2385 } 2386 } 2387 mc = nc; 2388 } 2389 } 2390 2391 /******************************************************************************** 2392 ******************************************************************************** 2393 Command Buffer Management 2394 ******************************************************************************** 2395 ********************************************************************************/ 2396 2397 /******************************************************************************** 2398 * Get a new command buffer. 2399 * 2400 * This may return NULL in low-memory cases. 2401 * 2402 * Note that using malloc() is expensive (the command buffer is << 1 page) but 2403 * necessary if we are to be a loadable module before the zone allocator is fixed. 2404 * 2405 * If possible, we recycle a command buffer that's been used before. 2406 * 2407 * XXX Note that command buffers are not cleaned out - it is the caller's 2408 * responsibility to ensure that all required fields are filled in before 2409 * using a buffer. 2410 */ 2411 static struct mlx_command * 2412 mlx_alloccmd(struct mlx_softc *sc) 2413 { 2414 struct mlx_command *mc; 2415 int error; 2416 2417 debug_called(1); 2418 2419 MLX_IO_ASSERT_LOCKED(sc); 2420 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) 2421 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link); 2422 2423 /* allocate a new command buffer? */ 2424 if (mc == NULL) { 2425 mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO); 2426 if (mc != NULL) { 2427 mc->mc_sc = sc; 2428 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap); 2429 if (error) { 2430 free(mc, M_DEVBUF); 2431 return(NULL); 2432 } 2433 } 2434 } 2435 return(mc); 2436 } 2437 2438 /******************************************************************************** 2439 * Release a command buffer for recycling. 2440 * 2441 * XXX It might be a good idea to limit the number of commands we save for reuse 2442 * if it's shown that this list bloats out massively. 2443 */ 2444 static void 2445 mlx_releasecmd(struct mlx_command *mc) 2446 { 2447 2448 debug_called(1); 2449 2450 MLX_IO_ASSERT_LOCKED(mc->mc_sc); 2451 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link); 2452 } 2453 2454 /******************************************************************************** 2455 * Permanently discard a command buffer. 2456 */ 2457 static void 2458 mlx_freecmd(struct mlx_command *mc) 2459 { 2460 struct mlx_softc *sc = mc->mc_sc; 2461 2462 debug_called(1); 2463 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap); 2464 free(mc, M_DEVBUF); 2465 } 2466 2467 2468 /******************************************************************************** 2469 ******************************************************************************** 2470 Type 3 interface accessor methods 2471 ******************************************************************************** 2472 ********************************************************************************/ 2473 2474 /******************************************************************************** 2475 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 2476 * (the controller is not ready to take a command). 2477 */ 2478 static int 2479 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 2480 { 2481 int i; 2482 2483 debug_called(2); 2484 MLX_IO_ASSERT_LOCKED(sc); 2485 2486 /* ready for our command? */ 2487 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) { 2488 /* copy mailbox data to window */ 2489 for (i = 0; i < 13; i++) 2490 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 2491 2492 /* post command */ 2493 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL); 2494 return(1); 2495 } 2496 return(0); 2497 } 2498 2499 /******************************************************************************** 2500 * See if a command has been completed, if so acknowledge its completion 2501 * and recover the slot number and status code. 2502 */ 2503 static int 2504 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 2505 { 2506 2507 debug_called(2); 2508 MLX_IO_ASSERT_LOCKED(sc); 2509 2510 /* status available? */ 2511 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) { 2512 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */ 2513 *status = MLX_V3_GET_STATUS(sc); /* get status */ 2514 2515 /* acknowledge completion */ 2516 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL); 2517 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK); 2518 return(1); 2519 } 2520 return(0); 2521 } 2522 2523 /******************************************************************************** 2524 * Enable/disable interrupts as requested. (No acknowledge required) 2525 */ 2526 static void 2527 mlx_v3_intaction(struct mlx_softc *sc, int action) 2528 { 2529 debug_called(1); 2530 MLX_IO_ASSERT_LOCKED(sc); 2531 2532 switch(action) { 2533 case MLX_INTACTION_DISABLE: 2534 MLX_V3_PUT_IER(sc, 0); 2535 sc->mlx_state &= ~MLX_STATE_INTEN; 2536 break; 2537 case MLX_INTACTION_ENABLE: 2538 MLX_V3_PUT_IER(sc, 1); 2539 sc->mlx_state |= MLX_STATE_INTEN; 2540 break; 2541 } 2542 } 2543 2544 /******************************************************************************** 2545 * Poll for firmware error codes during controller initialisation. 2546 * Returns 0 if initialisation is complete, 1 if still in progress but no 2547 * error has been fetched, 2 if an error has been retrieved. 2548 */ 2549 static int 2550 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, 2551 int first) 2552 { 2553 u_int8_t fwerror; 2554 2555 debug_called(2); 2556 2557 /* first time around, clear any hardware completion status */ 2558 if (first) { 2559 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK); 2560 DELAY(1000); 2561 } 2562 2563 /* init in progress? */ 2564 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY)) 2565 return(0); 2566 2567 /* test error value */ 2568 fwerror = MLX_V3_GET_FWERROR(sc); 2569 if (!(fwerror & MLX_V3_FWERROR_PEND)) 2570 return(1); 2571 2572 /* mask status pending bit, fetch status */ 2573 *error = fwerror & ~MLX_V3_FWERROR_PEND; 2574 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc); 2575 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc); 2576 2577 /* acknowledge */ 2578 MLX_V3_PUT_FWERROR(sc, 0); 2579 2580 return(2); 2581 } 2582 2583 /******************************************************************************** 2584 ******************************************************************************** 2585 Type 4 interface accessor methods 2586 ******************************************************************************** 2587 ********************************************************************************/ 2588 2589 /******************************************************************************** 2590 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 2591 * (the controller is not ready to take a command). 2592 */ 2593 static int 2594 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 2595 { 2596 int i; 2597 2598 debug_called(2); 2599 MLX_IO_ASSERT_LOCKED(sc); 2600 2601 /* ready for our command? */ 2602 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) { 2603 /* copy mailbox data to window */ 2604 for (i = 0; i < 13; i++) 2605 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 2606 2607 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */ 2608 bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH, 2609 BUS_SPACE_BARRIER_WRITE); 2610 2611 /* post command */ 2612 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD); 2613 return(1); 2614 } 2615 return(0); 2616 } 2617 2618 /******************************************************************************** 2619 * See if a command has been completed, if so acknowledge its completion 2620 * and recover the slot number and status code. 2621 */ 2622 static int 2623 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 2624 { 2625 2626 debug_called(2); 2627 MLX_IO_ASSERT_LOCKED(sc); 2628 2629 /* status available? */ 2630 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) { 2631 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */ 2632 *status = MLX_V4_GET_STATUS(sc); /* get status */ 2633 2634 /* acknowledge completion */ 2635 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK); 2636 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK); 2637 return(1); 2638 } 2639 return(0); 2640 } 2641 2642 /******************************************************************************** 2643 * Enable/disable interrupts as requested. 2644 */ 2645 static void 2646 mlx_v4_intaction(struct mlx_softc *sc, int action) 2647 { 2648 debug_called(1); 2649 MLX_IO_ASSERT_LOCKED(sc); 2650 2651 switch(action) { 2652 case MLX_INTACTION_DISABLE: 2653 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT); 2654 sc->mlx_state &= ~MLX_STATE_INTEN; 2655 break; 2656 case MLX_INTACTION_ENABLE: 2657 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT); 2658 sc->mlx_state |= MLX_STATE_INTEN; 2659 break; 2660 } 2661 } 2662 2663 /******************************************************************************** 2664 * Poll for firmware error codes during controller initialisation. 2665 * Returns 0 if initialisation is complete, 1 if still in progress but no 2666 * error has been fetched, 2 if an error has been retrieved. 2667 */ 2668 static int 2669 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, 2670 int first) 2671 { 2672 u_int8_t fwerror; 2673 2674 debug_called(2); 2675 2676 /* first time around, clear any hardware completion status */ 2677 if (first) { 2678 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK); 2679 DELAY(1000); 2680 } 2681 2682 /* init in progress? */ 2683 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY)) 2684 return(0); 2685 2686 /* test error value */ 2687 fwerror = MLX_V4_GET_FWERROR(sc); 2688 if (!(fwerror & MLX_V4_FWERROR_PEND)) 2689 return(1); 2690 2691 /* mask status pending bit, fetch status */ 2692 *error = fwerror & ~MLX_V4_FWERROR_PEND; 2693 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc); 2694 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc); 2695 2696 /* acknowledge */ 2697 MLX_V4_PUT_FWERROR(sc, 0); 2698 2699 return(2); 2700 } 2701 2702 /******************************************************************************** 2703 ******************************************************************************** 2704 Type 5 interface accessor methods 2705 ******************************************************************************** 2706 ********************************************************************************/ 2707 2708 /******************************************************************************** 2709 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 2710 * (the controller is not ready to take a command). 2711 */ 2712 static int 2713 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 2714 { 2715 int i; 2716 2717 debug_called(2); 2718 MLX_IO_ASSERT_LOCKED(sc); 2719 2720 /* ready for our command? */ 2721 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) { 2722 /* copy mailbox data to window */ 2723 for (i = 0; i < 13; i++) 2724 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 2725 2726 /* post command */ 2727 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD); 2728 return(1); 2729 } 2730 return(0); 2731 } 2732 2733 /******************************************************************************** 2734 * See if a command has been completed, if so acknowledge its completion 2735 * and recover the slot number and status code. 2736 */ 2737 static int 2738 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 2739 { 2740 2741 debug_called(2); 2742 MLX_IO_ASSERT_LOCKED(sc); 2743 2744 /* status available? */ 2745 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) { 2746 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */ 2747 *status = MLX_V5_GET_STATUS(sc); /* get status */ 2748 2749 /* acknowledge completion */ 2750 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK); 2751 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK); 2752 return(1); 2753 } 2754 return(0); 2755 } 2756 2757 /******************************************************************************** 2758 * Enable/disable interrupts as requested. 2759 */ 2760 static void 2761 mlx_v5_intaction(struct mlx_softc *sc, int action) 2762 { 2763 debug_called(1); 2764 MLX_IO_ASSERT_LOCKED(sc); 2765 2766 switch(action) { 2767 case MLX_INTACTION_DISABLE: 2768 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT); 2769 sc->mlx_state &= ~MLX_STATE_INTEN; 2770 break; 2771 case MLX_INTACTION_ENABLE: 2772 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT); 2773 sc->mlx_state |= MLX_STATE_INTEN; 2774 break; 2775 } 2776 } 2777 2778 /******************************************************************************** 2779 * Poll for firmware error codes during controller initialisation. 2780 * Returns 0 if initialisation is complete, 1 if still in progress but no 2781 * error has been fetched, 2 if an error has been retrieved. 2782 */ 2783 static int 2784 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, 2785 int first) 2786 { 2787 u_int8_t fwerror; 2788 2789 debug_called(2); 2790 2791 /* first time around, clear any hardware completion status */ 2792 if (first) { 2793 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK); 2794 DELAY(1000); 2795 } 2796 2797 /* init in progress? */ 2798 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE) 2799 return(0); 2800 2801 /* test for error value */ 2802 fwerror = MLX_V5_GET_FWERROR(sc); 2803 if (!(fwerror & MLX_V5_FWERROR_PEND)) 2804 return(1); 2805 2806 /* mask status pending bit, fetch status */ 2807 *error = fwerror & ~MLX_V5_FWERROR_PEND; 2808 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc); 2809 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc); 2810 2811 /* acknowledge */ 2812 MLX_V5_PUT_FWERROR(sc, 0xff); 2813 2814 return(2); 2815 } 2816 2817 /******************************************************************************** 2818 ******************************************************************************** 2819 Debugging 2820 ******************************************************************************** 2821 ********************************************************************************/ 2822 2823 /******************************************************************************** 2824 * Return a status message describing (mc) 2825 */ 2826 static char *mlx_status_messages[] = { 2827 "normal completion", /* 00 */ 2828 "irrecoverable data error", /* 01 */ 2829 "drive does not exist, or is offline", /* 02 */ 2830 "attempt to write beyond end of drive", /* 03 */ 2831 "bad data encountered", /* 04 */ 2832 "invalid log entry request", /* 05 */ 2833 "attempt to rebuild online drive", /* 06 */ 2834 "new disk failed during rebuild", /* 07 */ 2835 "invalid channel/target", /* 08 */ 2836 "rebuild/check already in progress", /* 09 */ 2837 "one or more disks are dead", /* 10 */ 2838 "invalid or non-redundant drive", /* 11 */ 2839 "channel is busy", /* 12 */ 2840 "channel is not stopped", /* 13 */ 2841 "rebuild successfully terminated", /* 14 */ 2842 "unsupported command", /* 15 */ 2843 "check condition received", /* 16 */ 2844 "device is busy", /* 17 */ 2845 "selection or command timeout", /* 18 */ 2846 "command terminated abnormally", /* 19 */ 2847 "" 2848 }; 2849 2850 static struct 2851 { 2852 int command; 2853 u_int16_t status; 2854 int msg; 2855 } mlx_messages[] = { 2856 {MLX_CMD_READSG, 0x0001, 1}, 2857 {MLX_CMD_READSG, 0x0002, 1}, 2858 {MLX_CMD_READSG, 0x0105, 3}, 2859 {MLX_CMD_READSG, 0x010c, 4}, 2860 {MLX_CMD_WRITESG, 0x0001, 1}, 2861 {MLX_CMD_WRITESG, 0x0002, 1}, 2862 {MLX_CMD_WRITESG, 0x0105, 3}, 2863 {MLX_CMD_READSG_OLD, 0x0001, 1}, 2864 {MLX_CMD_READSG_OLD, 0x0002, 1}, 2865 {MLX_CMD_READSG_OLD, 0x0105, 3}, 2866 {MLX_CMD_WRITESG_OLD, 0x0001, 1}, 2867 {MLX_CMD_WRITESG_OLD, 0x0002, 1}, 2868 {MLX_CMD_WRITESG_OLD, 0x0105, 3}, 2869 {MLX_CMD_LOGOP, 0x0105, 5}, 2870 {MLX_CMD_REBUILDASYNC, 0x0002, 6}, 2871 {MLX_CMD_REBUILDASYNC, 0x0004, 7}, 2872 {MLX_CMD_REBUILDASYNC, 0x0105, 8}, 2873 {MLX_CMD_REBUILDASYNC, 0x0106, 9}, 2874 {MLX_CMD_REBUILDASYNC, 0x0107, 14}, 2875 {MLX_CMD_CHECKASYNC, 0x0002, 10}, 2876 {MLX_CMD_CHECKASYNC, 0x0105, 11}, 2877 {MLX_CMD_CHECKASYNC, 0x0106, 9}, 2878 {MLX_CMD_STOPCHANNEL, 0x0106, 12}, 2879 {MLX_CMD_STOPCHANNEL, 0x0105, 8}, 2880 {MLX_CMD_STARTCHANNEL, 0x0005, 13}, 2881 {MLX_CMD_STARTCHANNEL, 0x0105, 8}, 2882 {MLX_CMD_DIRECT_CDB, 0x0002, 16}, 2883 {MLX_CMD_DIRECT_CDB, 0x0008, 17}, 2884 {MLX_CMD_DIRECT_CDB, 0x000e, 18}, 2885 {MLX_CMD_DIRECT_CDB, 0x000f, 19}, 2886 {MLX_CMD_DIRECT_CDB, 0x0105, 8}, 2887 2888 {0, 0x0104, 14}, 2889 {-1, 0, 0} 2890 }; 2891 2892 static char * 2893 mlx_diagnose_command(struct mlx_command *mc) 2894 { 2895 static char unkmsg[80]; 2896 int i; 2897 2898 /* look up message in table */ 2899 for (i = 0; mlx_messages[i].command != -1; i++) 2900 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) && 2901 (mc->mc_status == mlx_messages[i].status)) 2902 return(mlx_status_messages[mlx_messages[i].msg]); 2903 2904 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]); 2905 return(unkmsg); 2906 } 2907 2908 /******************************************************************************* 2909 * Print a string describing the controller (sc) 2910 */ 2911 static struct 2912 { 2913 int hwid; 2914 char *name; 2915 } mlx_controller_names[] = { 2916 {0x01, "960P/PD"}, 2917 {0x02, "960PL"}, 2918 {0x10, "960PG"}, 2919 {0x11, "960PJ"}, 2920 {0x12, "960PR"}, 2921 {0x13, "960PT"}, 2922 {0x14, "960PTL0"}, 2923 {0x15, "960PRL"}, 2924 {0x16, "960PTL1"}, 2925 {0x20, "1164PVX"}, 2926 {-1, NULL} 2927 }; 2928 2929 static void 2930 mlx_describe_controller(struct mlx_softc *sc) 2931 { 2932 static char buf[80]; 2933 char *model; 2934 int i; 2935 2936 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) { 2937 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) { 2938 model = mlx_controller_names[i].name; 2939 break; 2940 } 2941 } 2942 if (model == NULL) { 2943 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff); 2944 model = buf; 2945 } 2946 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n", 2947 model, 2948 sc->mlx_enq2->me_actual_channels, 2949 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "", 2950 sc->mlx_enq2->me_firmware_id & 0xff, 2951 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff, 2952 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff, 2953 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff, 2954 sc->mlx_enq2->me_mem_size / (1024 * 1024)); 2955 2956 if (bootverbose) { 2957 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id); 2958 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id); 2959 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels, 2960 sc->mlx_enq2->me_actual_channels); 2961 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets); 2962 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags); 2963 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives); 2964 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms); 2965 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans); 2966 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size, 2967 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size); 2968 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type); 2969 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed); 2970 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed); 2971 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands); 2972 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg); 2973 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp); 2974 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod); 2975 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb); 2976 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency); 2977 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout); 2978 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines); 2979 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const); 2980 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk); 2981 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor); 2982 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline); 2983 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n", 2984 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "", 2985 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10, 2986 8 << (sc->mlx_enq2->me_scsi_cap & 0x3)); 2987 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build); 2988 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type); 2989 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features, 2990 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n"); 2991 2992 } 2993 } 2994 2995 /******************************************************************************* 2996 * Emit a string describing the firmware handshake status code, and return a flag 2997 * indicating whether the code represents a fatal error. 2998 * 2999 * Error code interpretations are from the Linux driver, and don't directly match 3000 * the messages printed by Mylex's BIOS. This may change if documentation on the 3001 * codes is forthcoming. 3002 */ 3003 static int 3004 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2) 3005 { 3006 switch(error) { 3007 case 0x00: 3008 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1); 3009 break; 3010 case 0x08: 3011 /* we could be neater about this and give some indication when we receive more of them */ 3012 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) { 3013 device_printf(sc->mlx_dev, "spinning up drives...\n"); 3014 sc->mlx_flags |= MLX_SPINUP_REPORTED; 3015 } 3016 break; 3017 case 0x30: 3018 device_printf(sc->mlx_dev, "configuration checksum error\n"); 3019 break; 3020 case 0x60: 3021 device_printf(sc->mlx_dev, "mirror race recovery failed\n"); 3022 break; 3023 case 0x70: 3024 device_printf(sc->mlx_dev, "mirror race recovery in progress\n"); 3025 break; 3026 case 0x90: 3027 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1); 3028 break; 3029 case 0xa0: 3030 device_printf(sc->mlx_dev, "logical drive installation aborted\n"); 3031 break; 3032 case 0xb0: 3033 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n"); 3034 break; 3035 case 0xd0: 3036 device_printf(sc->mlx_dev, "new controller configuration found\n"); 3037 break; 3038 case 0xf0: 3039 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n"); 3040 return(1); 3041 default: 3042 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2); 3043 break; 3044 } 3045 return(0); 3046 } 3047 3048 /******************************************************************************** 3049 ******************************************************************************** 3050 Utility Functions 3051 ******************************************************************************** 3052 ********************************************************************************/ 3053 3054 /******************************************************************************** 3055 * Find the disk whose unit number is (unit) on this controller 3056 */ 3057 static struct mlx_sysdrive * 3058 mlx_findunit(struct mlx_softc *sc, int unit) 3059 { 3060 int i; 3061 3062 /* search system drives */ 3063 MLX_CONFIG_ASSERT_LOCKED(sc); 3064 for (i = 0; i < MLX_MAXDRIVES; i++) { 3065 /* is this one attached? */ 3066 if (sc->mlx_sysdrive[i].ms_disk != 0) { 3067 /* is this the one? */ 3068 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk)) 3069 return(&sc->mlx_sysdrive[i]); 3070 } 3071 } 3072 return(NULL); 3073 } 3074