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