1 /*- 2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 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 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/module.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/ata.h> 35 #include <sys/bus.h> 36 #include <sys/endian.h> 37 #include <sys/malloc.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/sema.h> 41 #include <sys/taskqueue.h> 42 #include <vm/uma.h> 43 #include <machine/stdarg.h> 44 #include <machine/resource.h> 45 #include <machine/bus.h> 46 #include <sys/rman.h> 47 #include <dev/led/led.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pcireg.h> 50 #include "siis.h" 51 52 #include <cam/cam.h> 53 #include <cam/cam_ccb.h> 54 #include <cam/cam_sim.h> 55 #include <cam/cam_xpt_sim.h> 56 #include <cam/cam_debug.h> 57 58 /* local prototypes */ 59 static int siis_setup_interrupt(device_t dev); 60 static void siis_intr(void *data); 61 static int siis_suspend(device_t dev); 62 static int siis_resume(device_t dev); 63 static int siis_ch_init(device_t dev); 64 static int siis_ch_deinit(device_t dev); 65 static int siis_ch_suspend(device_t dev); 66 static int siis_ch_resume(device_t dev); 67 static void siis_ch_intr_locked(void *data); 68 static void siis_ch_intr(void *data); 69 static void siis_ch_led(void *priv, int onoff); 70 static void siis_begin_transaction(device_t dev, union ccb *ccb); 71 static void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 72 static void siis_execute_transaction(struct siis_slot *slot); 73 static void siis_timeout(struct siis_slot *slot); 74 static void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et); 75 static int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag); 76 static void siis_dmainit(device_t dev); 77 static void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 78 static void siis_dmafini(device_t dev); 79 static void siis_slotsalloc(device_t dev); 80 static void siis_slotsfree(device_t dev); 81 static void siis_reset(device_t dev); 82 static void siis_portinit(device_t dev); 83 static int siis_wait_ready(device_t dev, int t); 84 85 static int siis_sata_connect(struct siis_channel *ch); 86 87 static void siis_issue_recovery(device_t dev); 88 static void siis_process_read_log(device_t dev, union ccb *ccb); 89 static void siis_process_request_sense(device_t dev, union ccb *ccb); 90 91 static void siisaction(struct cam_sim *sim, union ccb *ccb); 92 static void siispoll(struct cam_sim *sim); 93 94 MALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers"); 95 96 static struct { 97 uint32_t id; 98 const char *name; 99 int ports; 100 int quirks; 101 #define SIIS_Q_SNTF 1 102 #define SIIS_Q_NOMSI 2 103 } siis_ids[] = { 104 {0x31241095, "SiI3124", 4, 0}, 105 {0x31248086, "SiI3124", 4, 0}, 106 {0x31321095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 107 {0x02421095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 108 {0x02441095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 109 {0x31311095, "SiI3131", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 110 {0x35311095, "SiI3531", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 111 {0, NULL, 0, 0} 112 }; 113 114 #define recovery_type spriv_field0 115 #define RECOVERY_NONE 0 116 #define RECOVERY_READ_LOG 1 117 #define RECOVERY_REQUEST_SENSE 2 118 #define recovery_slot spriv_field1 119 120 static int 121 siis_probe(device_t dev) 122 { 123 char buf[64]; 124 int i; 125 uint32_t devid = pci_get_devid(dev); 126 127 for (i = 0; siis_ids[i].id != 0; i++) { 128 if (siis_ids[i].id == devid) { 129 snprintf(buf, sizeof(buf), "%s SATA controller", 130 siis_ids[i].name); 131 device_set_desc_copy(dev, buf); 132 return (BUS_PROBE_VENDOR); 133 } 134 } 135 return (ENXIO); 136 } 137 138 static int 139 siis_attach(device_t dev) 140 { 141 struct siis_controller *ctlr = device_get_softc(dev); 142 uint32_t devid = pci_get_devid(dev); 143 device_t child; 144 int error, i, unit; 145 146 ctlr->dev = dev; 147 for (i = 0; siis_ids[i].id != 0; i++) { 148 if (siis_ids[i].id == devid) 149 break; 150 } 151 ctlr->quirks = siis_ids[i].quirks; 152 /* Global memory */ 153 ctlr->r_grid = PCIR_BAR(0); 154 if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 155 &ctlr->r_grid, RF_ACTIVE))) 156 return (ENXIO); 157 ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL); 158 /* Channels memory */ 159 ctlr->r_rid = PCIR_BAR(2); 160 if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 161 &ctlr->r_rid, RF_ACTIVE))) 162 return (ENXIO); 163 /* Setup our own memory management for channels. */ 164 ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem); 165 ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem); 166 ctlr->sc_iomem.rm_type = RMAN_ARRAY; 167 ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 168 if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 169 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 170 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 171 return (error); 172 } 173 if ((error = rman_manage_region(&ctlr->sc_iomem, 174 rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 175 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 176 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 177 rman_fini(&ctlr->sc_iomem); 178 return (error); 179 } 180 pci_enable_busmaster(dev); 181 /* Reset controller */ 182 siis_resume(dev); 183 /* Number of HW channels */ 184 ctlr->channels = siis_ids[i].ports; 185 /* Setup interrupts. */ 186 if (siis_setup_interrupt(dev)) { 187 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 188 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 189 rman_fini(&ctlr->sc_iomem); 190 return ENXIO; 191 } 192 /* Attach all channels on this controller */ 193 for (unit = 0; unit < ctlr->channels; unit++) { 194 child = device_add_child(dev, "siisch", -1); 195 if (child == NULL) 196 device_printf(dev, "failed to add channel device\n"); 197 else 198 device_set_ivars(child, (void *)(intptr_t)unit); 199 } 200 bus_generic_attach(dev); 201 return 0; 202 } 203 204 static int 205 siis_detach(device_t dev) 206 { 207 struct siis_controller *ctlr = device_get_softc(dev); 208 device_t *children; 209 int nchildren, i; 210 211 /* Detach & delete all children */ 212 if (!device_get_children(dev, &children, &nchildren)) { 213 for (i = 0; i < nchildren; i++) 214 device_delete_child(dev, children[i]); 215 free(children, M_TEMP); 216 } 217 /* Free interrupts. */ 218 if (ctlr->irq.r_irq) { 219 bus_teardown_intr(dev, ctlr->irq.r_irq, 220 ctlr->irq.handle); 221 bus_release_resource(dev, SYS_RES_IRQ, 222 ctlr->irq.r_irq_rid, ctlr->irq.r_irq); 223 } 224 pci_release_msi(dev); 225 /* Free memory. */ 226 rman_fini(&ctlr->sc_iomem); 227 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 228 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 229 return (0); 230 } 231 232 static int 233 siis_suspend(device_t dev) 234 { 235 struct siis_controller *ctlr = device_get_softc(dev); 236 237 bus_generic_suspend(dev); 238 /* Put controller into reset state. */ 239 ctlr->gctl |= SIIS_GCTL_GRESET; 240 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 241 return 0; 242 } 243 244 static int 245 siis_resume(device_t dev) 246 { 247 struct siis_controller *ctlr = device_get_softc(dev); 248 249 /* Set PCIe max read request size to at least 1024 bytes */ 250 if (pci_get_max_read_req(dev) < 1024) 251 pci_set_max_read_req(dev, 1024); 252 /* Put controller into reset state. */ 253 ctlr->gctl |= SIIS_GCTL_GRESET; 254 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 255 DELAY(10000); 256 /* Get controller out of reset state and enable port interrupts. */ 257 ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE); 258 ctlr->gctl |= 0x0000000f; 259 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 260 return (bus_generic_resume(dev)); 261 } 262 263 static int 264 siis_setup_interrupt(device_t dev) 265 { 266 struct siis_controller *ctlr = device_get_softc(dev); 267 int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1; 268 269 /* Process hints. */ 270 resource_int_value(device_get_name(dev), 271 device_get_unit(dev), "msi", &msi); 272 if (msi < 0) 273 msi = 0; 274 else if (msi > 0) 275 msi = min(1, pci_msi_count(dev)); 276 /* Allocate MSI if needed/present. */ 277 if (msi && pci_alloc_msi(dev, &msi) != 0) 278 msi = 0; 279 /* Allocate all IRQs. */ 280 ctlr->irq.r_irq_rid = msi ? 1 : 0; 281 if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 282 &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 283 device_printf(dev, "unable to map interrupt\n"); 284 return ENXIO; 285 } 286 if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL, 287 siis_intr, ctlr, &ctlr->irq.handle))) { 288 /* SOS XXX release r_irq */ 289 device_printf(dev, "unable to setup interrupt\n"); 290 return ENXIO; 291 } 292 return (0); 293 } 294 295 /* 296 * Common case interrupt handler. 297 */ 298 static void 299 siis_intr(void *data) 300 { 301 struct siis_controller *ctlr = (struct siis_controller *)data; 302 u_int32_t is; 303 void *arg; 304 int unit; 305 306 is = ATA_INL(ctlr->r_gmem, SIIS_IS); 307 for (unit = 0; unit < ctlr->channels; unit++) { 308 if ((is & SIIS_IS_PORT(unit)) != 0 && 309 (arg = ctlr->interrupt[unit].argument)) { 310 ctlr->interrupt[unit].function(arg); 311 } 312 } 313 /* Acknowledge interrupt, if MSI enabled. */ 314 if (ctlr->irq.r_irq_rid) { 315 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 316 ctlr->gctl | SIIS_GCTL_MSIACK); 317 } 318 } 319 320 static struct resource * 321 siis_alloc_resource(device_t dev, device_t child, int type, int *rid, 322 u_long start, u_long end, u_long count, u_int flags) 323 { 324 struct siis_controller *ctlr = device_get_softc(dev); 325 int unit = ((struct siis_channel *)device_get_softc(child))->unit; 326 struct resource *res = NULL; 327 int offset = unit << 13; 328 long st; 329 330 switch (type) { 331 case SYS_RES_MEMORY: 332 st = rman_get_start(ctlr->r_mem); 333 res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 334 st + offset + 0x2000, 0x2000, RF_ACTIVE, child); 335 if (res) { 336 bus_space_handle_t bsh; 337 bus_space_tag_t bst; 338 bsh = rman_get_bushandle(ctlr->r_mem); 339 bst = rman_get_bustag(ctlr->r_mem); 340 bus_space_subregion(bst, bsh, offset, 0x2000, &bsh); 341 rman_set_bushandle(res, bsh); 342 rman_set_bustag(res, bst); 343 } 344 break; 345 case SYS_RES_IRQ: 346 if (*rid == ATA_IRQ_RID) 347 res = ctlr->irq.r_irq; 348 break; 349 } 350 return (res); 351 } 352 353 static int 354 siis_release_resource(device_t dev, device_t child, int type, int rid, 355 struct resource *r) 356 { 357 358 switch (type) { 359 case SYS_RES_MEMORY: 360 rman_release_resource(r); 361 return (0); 362 case SYS_RES_IRQ: 363 if (rid != ATA_IRQ_RID) 364 return ENOENT; 365 return (0); 366 } 367 return (EINVAL); 368 } 369 370 static int 371 siis_setup_intr(device_t dev, device_t child, struct resource *irq, 372 int flags, driver_filter_t *filter, driver_intr_t *function, 373 void *argument, void **cookiep) 374 { 375 struct siis_controller *ctlr = device_get_softc(dev); 376 int unit = (intptr_t)device_get_ivars(child); 377 378 if (filter != NULL) { 379 printf("siis.c: we cannot use a filter here\n"); 380 return (EINVAL); 381 } 382 ctlr->interrupt[unit].function = function; 383 ctlr->interrupt[unit].argument = argument; 384 return (0); 385 } 386 387 static int 388 siis_teardown_intr(device_t dev, device_t child, struct resource *irq, 389 void *cookie) 390 { 391 struct siis_controller *ctlr = device_get_softc(dev); 392 int unit = (intptr_t)device_get_ivars(child); 393 394 ctlr->interrupt[unit].function = NULL; 395 ctlr->interrupt[unit].argument = NULL; 396 return (0); 397 } 398 399 static int 400 siis_print_child(device_t dev, device_t child) 401 { 402 int retval; 403 404 retval = bus_print_child_header(dev, child); 405 retval += printf(" at channel %d", 406 (int)(intptr_t)device_get_ivars(child)); 407 retval += bus_print_child_footer(dev, child); 408 409 return (retval); 410 } 411 412 static int 413 siis_child_location_str(device_t dev, device_t child, char *buf, 414 size_t buflen) 415 { 416 417 snprintf(buf, buflen, "channel=%d", 418 (int)(intptr_t)device_get_ivars(child)); 419 return (0); 420 } 421 422 devclass_t siis_devclass; 423 static device_method_t siis_methods[] = { 424 DEVMETHOD(device_probe, siis_probe), 425 DEVMETHOD(device_attach, siis_attach), 426 DEVMETHOD(device_detach, siis_detach), 427 DEVMETHOD(device_suspend, siis_suspend), 428 DEVMETHOD(device_resume, siis_resume), 429 DEVMETHOD(bus_print_child, siis_print_child), 430 DEVMETHOD(bus_alloc_resource, siis_alloc_resource), 431 DEVMETHOD(bus_release_resource, siis_release_resource), 432 DEVMETHOD(bus_setup_intr, siis_setup_intr), 433 DEVMETHOD(bus_teardown_intr,siis_teardown_intr), 434 DEVMETHOD(bus_child_location_str, siis_child_location_str), 435 { 0, 0 } 436 }; 437 static driver_t siis_driver = { 438 "siis", 439 siis_methods, 440 sizeof(struct siis_controller) 441 }; 442 DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0); 443 MODULE_VERSION(siis, 1); 444 MODULE_DEPEND(siis, cam, 1, 1, 1); 445 446 static int 447 siis_ch_probe(device_t dev) 448 { 449 450 device_set_desc_copy(dev, "SIIS channel"); 451 return (0); 452 } 453 454 static int 455 siis_ch_attach(device_t dev) 456 { 457 struct siis_controller *ctlr = device_get_softc(device_get_parent(dev)); 458 struct siis_channel *ch = device_get_softc(dev); 459 struct cam_devq *devq; 460 int rid, error, i, sata_rev = 0; 461 462 ch->dev = dev; 463 ch->unit = (intptr_t)device_get_ivars(dev); 464 ch->quirks = ctlr->quirks; 465 resource_int_value(device_get_name(dev), 466 device_get_unit(dev), "pm_level", &ch->pm_level); 467 resource_int_value(device_get_name(dev), 468 device_get_unit(dev), "sata_rev", &sata_rev); 469 for (i = 0; i < 16; i++) { 470 ch->user[i].revision = sata_rev; 471 ch->user[i].mode = 0; 472 ch->user[i].bytecount = 8192; 473 ch->user[i].tags = SIIS_MAX_SLOTS; 474 ch->curr[i] = ch->user[i]; 475 if (ch->pm_level) 476 ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ; 477 ch->user[i].caps |= CTS_SATA_CAPS_H_AN; 478 } 479 mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 480 rid = ch->unit; 481 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 482 &rid, RF_ACTIVE))) 483 return (ENXIO); 484 siis_dmainit(dev); 485 siis_slotsalloc(dev); 486 siis_ch_init(dev); 487 mtx_lock(&ch->mtx); 488 rid = ATA_IRQ_RID; 489 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 490 &rid, RF_SHAREABLE | RF_ACTIVE))) { 491 device_printf(dev, "Unable to map interrupt\n"); 492 error = ENXIO; 493 goto err0; 494 } 495 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 496 siis_ch_intr_locked, dev, &ch->ih))) { 497 device_printf(dev, "Unable to setup interrupt\n"); 498 error = ENXIO; 499 goto err1; 500 } 501 /* Create the device queue for our SIM. */ 502 devq = cam_simq_alloc(SIIS_MAX_SLOTS); 503 if (devq == NULL) { 504 device_printf(dev, "Unable to allocate simq\n"); 505 error = ENOMEM; 506 goto err1; 507 } 508 /* Construct SIM entry */ 509 ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 510 device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq); 511 if (ch->sim == NULL) { 512 cam_simq_free(devq); 513 device_printf(dev, "unable to allocate sim\n"); 514 error = ENOMEM; 515 goto err1; 516 } 517 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 518 device_printf(dev, "unable to register xpt bus\n"); 519 error = ENXIO; 520 goto err2; 521 } 522 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 523 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 524 device_printf(dev, "unable to create path\n"); 525 error = ENXIO; 526 goto err3; 527 } 528 mtx_unlock(&ch->mtx); 529 ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev)); 530 return (0); 531 532 err3: 533 xpt_bus_deregister(cam_sim_path(ch->sim)); 534 err2: 535 cam_sim_free(ch->sim, /*free_devq*/TRUE); 536 err1: 537 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 538 err0: 539 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 540 mtx_unlock(&ch->mtx); 541 mtx_destroy(&ch->mtx); 542 return (error); 543 } 544 545 static int 546 siis_ch_detach(device_t dev) 547 { 548 struct siis_channel *ch = device_get_softc(dev); 549 550 led_destroy(ch->led); 551 mtx_lock(&ch->mtx); 552 xpt_async(AC_LOST_DEVICE, ch->path, NULL); 553 xpt_free_path(ch->path); 554 xpt_bus_deregister(cam_sim_path(ch->sim)); 555 cam_sim_free(ch->sim, /*free_devq*/TRUE); 556 mtx_unlock(&ch->mtx); 557 558 bus_teardown_intr(dev, ch->r_irq, ch->ih); 559 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 560 561 siis_ch_deinit(dev); 562 siis_slotsfree(dev); 563 siis_dmafini(dev); 564 565 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 566 mtx_destroy(&ch->mtx); 567 return (0); 568 } 569 570 static int 571 siis_ch_init(device_t dev) 572 { 573 struct siis_channel *ch = device_get_softc(dev); 574 575 /* Get port out of reset state. */ 576 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 577 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 578 if (ch->pm_present) 579 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 580 else 581 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 582 /* Enable port interrupts */ 583 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 584 return (0); 585 } 586 587 static int 588 siis_ch_deinit(device_t dev) 589 { 590 struct siis_channel *ch = device_get_softc(dev); 591 592 /* Put port into reset state. */ 593 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 594 return (0); 595 } 596 597 static int 598 siis_ch_suspend(device_t dev) 599 { 600 struct siis_channel *ch = device_get_softc(dev); 601 602 mtx_lock(&ch->mtx); 603 xpt_freeze_simq(ch->sim, 1); 604 while (ch->oslots) 605 msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100); 606 siis_ch_deinit(dev); 607 mtx_unlock(&ch->mtx); 608 return (0); 609 } 610 611 static int 612 siis_ch_resume(device_t dev) 613 { 614 struct siis_channel *ch = device_get_softc(dev); 615 616 mtx_lock(&ch->mtx); 617 siis_ch_init(dev); 618 siis_reset(dev); 619 xpt_release_simq(ch->sim, TRUE); 620 mtx_unlock(&ch->mtx); 621 return (0); 622 } 623 624 devclass_t siisch_devclass; 625 static device_method_t siisch_methods[] = { 626 DEVMETHOD(device_probe, siis_ch_probe), 627 DEVMETHOD(device_attach, siis_ch_attach), 628 DEVMETHOD(device_detach, siis_ch_detach), 629 DEVMETHOD(device_suspend, siis_ch_suspend), 630 DEVMETHOD(device_resume, siis_ch_resume), 631 { 0, 0 } 632 }; 633 static driver_t siisch_driver = { 634 "siisch", 635 siisch_methods, 636 sizeof(struct siis_channel) 637 }; 638 DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 639 640 static void 641 siis_ch_led(void *priv, int onoff) 642 { 643 device_t dev; 644 struct siis_channel *ch; 645 646 dev = (device_t)priv; 647 ch = device_get_softc(dev); 648 649 if (onoff == 0) 650 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON); 651 else 652 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON); 653 } 654 655 struct siis_dc_cb_args { 656 bus_addr_t maddr; 657 int error; 658 }; 659 660 static void 661 siis_dmainit(device_t dev) 662 { 663 struct siis_channel *ch = device_get_softc(dev); 664 struct siis_dc_cb_args dcba; 665 666 /* Command area. */ 667 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 668 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 669 NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 670 0, NULL, NULL, &ch->dma.work_tag)) 671 goto error; 672 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 673 &ch->dma.work_map)) 674 goto error; 675 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 676 SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 677 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 678 goto error; 679 } 680 ch->dma.work_bus = dcba.maddr; 681 /* Data area. */ 682 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 683 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 684 NULL, NULL, 685 SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 686 SIIS_SG_ENTRIES, 0xFFFFFFFF, 687 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 688 goto error; 689 } 690 return; 691 692 error: 693 device_printf(dev, "WARNING - DMA initialization failed\n"); 694 siis_dmafini(dev); 695 } 696 697 static void 698 siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 699 { 700 struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 701 702 if (!(dcba->error = error)) 703 dcba->maddr = segs[0].ds_addr; 704 } 705 706 static void 707 siis_dmafini(device_t dev) 708 { 709 struct siis_channel *ch = device_get_softc(dev); 710 711 if (ch->dma.data_tag) { 712 bus_dma_tag_destroy(ch->dma.data_tag); 713 ch->dma.data_tag = NULL; 714 } 715 if (ch->dma.work_bus) { 716 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 717 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 718 ch->dma.work_bus = 0; 719 ch->dma.work_map = NULL; 720 ch->dma.work = NULL; 721 } 722 if (ch->dma.work_tag) { 723 bus_dma_tag_destroy(ch->dma.work_tag); 724 ch->dma.work_tag = NULL; 725 } 726 } 727 728 static void 729 siis_slotsalloc(device_t dev) 730 { 731 struct siis_channel *ch = device_get_softc(dev); 732 int i; 733 734 /* Alloc and setup command/dma slots */ 735 bzero(ch->slot, sizeof(ch->slot)); 736 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 737 struct siis_slot *slot = &ch->slot[i]; 738 739 slot->dev = dev; 740 slot->slot = i; 741 slot->state = SIIS_SLOT_EMPTY; 742 slot->ccb = NULL; 743 callout_init_mtx(&slot->timeout, &ch->mtx, 0); 744 745 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 746 device_printf(ch->dev, "FAILURE - create data_map\n"); 747 } 748 } 749 750 static void 751 siis_slotsfree(device_t dev) 752 { 753 struct siis_channel *ch = device_get_softc(dev); 754 int i; 755 756 /* Free all dma slots */ 757 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 758 struct siis_slot *slot = &ch->slot[i]; 759 760 callout_drain(&slot->timeout); 761 if (slot->dma.data_map) { 762 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 763 slot->dma.data_map = NULL; 764 } 765 } 766 } 767 768 static void 769 siis_notify_events(device_t dev) 770 { 771 struct siis_channel *ch = device_get_softc(dev); 772 struct cam_path *dpath; 773 u_int32_t status; 774 int i; 775 776 if (ch->quirks & SIIS_Q_SNTF) { 777 status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 778 ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 779 } else { 780 /* 781 * Without SNTF we have no idea which device sent notification. 782 * If PMP is connected, assume it, else - device. 783 */ 784 status = (ch->pm_present) ? 0x8000 : 0x0001; 785 } 786 if (bootverbose) 787 device_printf(dev, "SNTF 0x%04x\n", status); 788 for (i = 0; i < 16; i++) { 789 if ((status & (1 << i)) == 0) 790 continue; 791 if (xpt_create_path(&dpath, NULL, 792 xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 793 xpt_async(AC_SCSI_AEN, dpath, NULL); 794 xpt_free_path(dpath); 795 } 796 } 797 798 } 799 800 static void 801 siis_phy_check_events(device_t dev) 802 { 803 struct siis_channel *ch = device_get_softc(dev); 804 805 /* If we have a connection event, deal with it */ 806 if (ch->pm_level == 0) { 807 u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 808 union ccb *ccb; 809 810 if (bootverbose) { 811 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 812 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 813 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 814 device_printf(dev, "CONNECT requested\n"); 815 } else 816 device_printf(dev, "DISCONNECT requested\n"); 817 } 818 siis_reset(dev); 819 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 820 return; 821 if (xpt_create_path(&ccb->ccb_h.path, NULL, 822 cam_sim_path(ch->sim), 823 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 824 xpt_free_ccb(ccb); 825 return; 826 } 827 xpt_rescan(ccb); 828 } 829 } 830 831 static void 832 siis_ch_intr_locked(void *data) 833 { 834 device_t dev = (device_t)data; 835 struct siis_channel *ch = device_get_softc(dev); 836 837 mtx_lock(&ch->mtx); 838 siis_ch_intr(data); 839 mtx_unlock(&ch->mtx); 840 } 841 842 static void 843 siis_ch_intr(void *data) 844 { 845 device_t dev = (device_t)data; 846 struct siis_channel *ch = device_get_softc(dev); 847 uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 848 enum siis_err_type et; 849 int i, ccs, port, tslots; 850 851 mtx_assert(&ch->mtx, MA_OWNED); 852 /* Read command statuses. */ 853 sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 854 ok = ch->rslots & ~sstatus; 855 /* Complete all successfull commands. */ 856 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 857 if ((ok >> i) & 1) 858 siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 859 } 860 /* Do we have any other events? */ 861 if ((sstatus & SIIS_P_SS_ATTN) == 0) 862 return; 863 /* Read and clear interrupt statuses. */ 864 istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 865 (0xFFFF & ~SIIS_P_IX_COMMCOMP); 866 ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 867 /* Process PHY events */ 868 if (istatus & SIIS_P_IX_PHYRDYCHG) 869 siis_phy_check_events(dev); 870 /* Process NOTIFY events */ 871 if (istatus & SIIS_P_IX_SDBN) 872 siis_notify_events(dev); 873 /* Process command errors */ 874 if (istatus & SIIS_P_IX_COMMERR) { 875 estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 876 ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 877 ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 878 port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 879 err = ch->rslots & sstatus; 880 //device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 881 // __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 882 // ATA_INL(ch->r_mem, SIIS_P_SERR)); 883 884 if (!ch->recoverycmd && !ch->recovery) { 885 xpt_freeze_simq(ch->sim, ch->numrslots); 886 ch->recovery = 1; 887 } 888 if (ch->frozen) { 889 union ccb *fccb = ch->frozen; 890 ch->frozen = NULL; 891 fccb->ccb_h.status &= ~CAM_STATUS_MASK; 892 fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 893 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 894 xpt_freeze_devq(fccb->ccb_h.path, 1); 895 fccb->ccb_h.status |= CAM_DEV_QFRZN; 896 } 897 xpt_done(fccb); 898 } 899 if (estatus == SIIS_P_CMDERR_DEV || 900 estatus == SIIS_P_CMDERR_SDB || 901 estatus == SIIS_P_CMDERR_DATAFIS) { 902 tslots = ch->numtslots[port]; 903 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 904 /* XXX: requests in loading state. */ 905 if (((ch->rslots >> i) & 1) == 0) 906 continue; 907 if (ch->slot[i].ccb->ccb_h.target_id != port) 908 continue; 909 if (tslots == 0) { 910 /* Untagged operation. */ 911 if (i == ccs) 912 et = SIIS_ERR_TFE; 913 else 914 et = SIIS_ERR_INNOCENT; 915 } else { 916 /* Tagged operation. */ 917 et = SIIS_ERR_NCQ; 918 } 919 siis_end_transaction(&ch->slot[i], et); 920 } 921 /* 922 * We can't reinit port if there are some other 923 * commands active, use resume to complete them. 924 */ 925 if (ch->rslots != 0 && !ch->recoverycmd) 926 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 927 } else { 928 if (estatus == SIIS_P_CMDERR_SENDFIS || 929 estatus == SIIS_P_CMDERR_INCSTATE || 930 estatus == SIIS_P_CMDERR_PPE || 931 estatus == SIIS_P_CMDERR_SERVICE) { 932 et = SIIS_ERR_SATA; 933 } else 934 et = SIIS_ERR_INVALID; 935 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 936 /* XXX: requests in loading state. */ 937 if (((ch->rslots >> i) & 1) == 0) 938 continue; 939 siis_end_transaction(&ch->slot[i], et); 940 } 941 } 942 } 943 } 944 945 /* Must be called with channel locked. */ 946 static int 947 siis_check_collision(device_t dev, union ccb *ccb) 948 { 949 struct siis_channel *ch = device_get_softc(dev); 950 951 mtx_assert(&ch->mtx, MA_OWNED); 952 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 953 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 954 /* Tagged command while we have no supported tag free. */ 955 if (((~ch->oslots) & (0x7fffffff >> (31 - 956 ch->curr[ccb->ccb_h.target_id].tags))) == 0) 957 return (1); 958 } 959 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 960 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 961 /* Atomic command while anything active. */ 962 if (ch->numrslots != 0) 963 return (1); 964 } 965 /* We have some atomic command running. */ 966 if (ch->aslots != 0) 967 return (1); 968 return (0); 969 } 970 971 /* Must be called with channel locked. */ 972 static void 973 siis_begin_transaction(device_t dev, union ccb *ccb) 974 { 975 struct siis_channel *ch = device_get_softc(dev); 976 struct siis_slot *slot; 977 int tag, tags; 978 979 mtx_assert(&ch->mtx, MA_OWNED); 980 /* Choose empty slot. */ 981 tags = SIIS_MAX_SLOTS; 982 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 983 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) 984 tags = ch->curr[ccb->ccb_h.target_id].tags; 985 tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1; 986 /* Occupy chosen slot. */ 987 slot = &ch->slot[tag]; 988 slot->ccb = ccb; 989 /* Update channel stats. */ 990 ch->oslots |= (1 << slot->slot); 991 ch->numrslots++; 992 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 993 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 994 ch->numtslots[ccb->ccb_h.target_id]++; 995 } 996 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 997 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 998 ch->aslots |= (1 << slot->slot); 999 slot->dma.nsegs = 0; 1000 /* If request moves data, setup and load SG list */ 1001 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1002 void *buf; 1003 bus_size_t size; 1004 1005 slot->state = SIIS_SLOT_LOADING; 1006 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1007 buf = ccb->ataio.data_ptr; 1008 size = ccb->ataio.dxfer_len; 1009 } else { 1010 buf = ccb->csio.data_ptr; 1011 size = ccb->csio.dxfer_len; 1012 } 1013 bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 1014 buf, size, siis_dmasetprd, slot, 0); 1015 } else 1016 siis_execute_transaction(slot); 1017 } 1018 1019 /* Locked by busdma engine. */ 1020 static void 1021 siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1022 { 1023 struct siis_slot *slot = arg; 1024 struct siis_channel *ch = device_get_softc(slot->dev); 1025 struct siis_cmd *ctp; 1026 struct siis_dma_prd *prd; 1027 int i; 1028 1029 mtx_assert(&ch->mtx, MA_OWNED); 1030 if (error) { 1031 device_printf(slot->dev, "DMA load error\n"); 1032 if (!ch->recoverycmd) 1033 xpt_freeze_simq(ch->sim, 1); 1034 siis_end_transaction(slot, SIIS_ERR_INVALID); 1035 return; 1036 } 1037 KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 1038 /* Get a piece of the workspace for this request */ 1039 ctp = (struct siis_cmd *) 1040 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1041 /* Fill S/G table */ 1042 if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 1043 prd = &ctp->u.ata.prd[0]; 1044 else 1045 prd = &ctp->u.atapi.prd[0]; 1046 for (i = 0; i < nsegs; i++) { 1047 prd[i].dba = htole64(segs[i].ds_addr); 1048 prd[i].dbc = htole32(segs[i].ds_len); 1049 prd[i].control = 0; 1050 } 1051 prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 1052 slot->dma.nsegs = nsegs; 1053 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1054 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1055 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1056 siis_execute_transaction(slot); 1057 } 1058 1059 /* Must be called with channel locked. */ 1060 static void 1061 siis_execute_transaction(struct siis_slot *slot) 1062 { 1063 device_t dev = slot->dev; 1064 struct siis_channel *ch = device_get_softc(dev); 1065 struct siis_cmd *ctp; 1066 union ccb *ccb = slot->ccb; 1067 u_int64_t prb_bus; 1068 1069 mtx_assert(&ch->mtx, MA_OWNED); 1070 /* Get a piece of the workspace for this request */ 1071 ctp = (struct siis_cmd *) 1072 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1073 ctp->control = 0; 1074 ctp->protocol_override = 0; 1075 ctp->transfer_count = 0; 1076 /* Special handling for Soft Reset command. */ 1077 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1078 if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) { 1079 ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 1080 } else { 1081 ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE); 1082 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1083 ctp->protocol_override |= 1084 htole16(SIIS_PRB_PROTO_NCQ); 1085 } 1086 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1087 ctp->protocol_override |= 1088 htole16(SIIS_PRB_PROTO_READ); 1089 } else 1090 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1091 ctp->protocol_override |= 1092 htole16(SIIS_PRB_PROTO_WRITE); 1093 } 1094 } 1095 } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1096 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1097 ctp->control |= htole16(SIIS_PRB_PACKET_READ); 1098 else 1099 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1100 ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 1101 } 1102 /* Special handling for Soft Reset command. */ 1103 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1104 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1105 (ccb->ataio.cmd.control & ATA_A_RESET)) { 1106 /* Kick controller into sane state */ 1107 siis_portinit(dev); 1108 } 1109 /* Setup the FIS for this request */ 1110 if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) { 1111 device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1112 if (!ch->recoverycmd) 1113 xpt_freeze_simq(ch->sim, 1); 1114 siis_end_transaction(slot, SIIS_ERR_INVALID); 1115 return; 1116 } 1117 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1118 BUS_DMASYNC_PREWRITE); 1119 /* Issue command to the controller. */ 1120 slot->state = SIIS_SLOT_RUNNING; 1121 ch->rslots |= (1 << slot->slot); 1122 prb_bus = ch->dma.work_bus + 1123 SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 1124 ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 1125 ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 1126 /* Start command execution timeout */ 1127 callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000, 1128 (timeout_t*)siis_timeout, slot); 1129 return; 1130 } 1131 1132 /* Must be called with channel locked. */ 1133 static void 1134 siis_process_timeout(device_t dev) 1135 { 1136 struct siis_channel *ch = device_get_softc(dev); 1137 int i; 1138 1139 mtx_assert(&ch->mtx, MA_OWNED); 1140 if (!ch->recoverycmd && !ch->recovery) { 1141 xpt_freeze_simq(ch->sim, ch->numrslots); 1142 ch->recovery = 1; 1143 } 1144 /* Handle the rest of commands. */ 1145 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1146 /* Do we have a running request on slot? */ 1147 if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1148 continue; 1149 siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT); 1150 } 1151 } 1152 1153 /* Must be called with channel locked. */ 1154 static void 1155 siis_rearm_timeout(device_t dev) 1156 { 1157 struct siis_channel *ch = device_get_softc(dev); 1158 int i; 1159 1160 mtx_assert(&ch->mtx, MA_OWNED); 1161 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1162 struct siis_slot *slot = &ch->slot[i]; 1163 1164 /* Do we have a running request on slot? */ 1165 if (slot->state < SIIS_SLOT_RUNNING) 1166 continue; 1167 if ((ch->toslots & (1 << i)) == 0) 1168 continue; 1169 callout_reset(&slot->timeout, 1170 (int)slot->ccb->ccb_h.timeout * hz / 1000, 1171 (timeout_t*)siis_timeout, slot); 1172 } 1173 } 1174 1175 /* Locked by callout mechanism. */ 1176 static void 1177 siis_timeout(struct siis_slot *slot) 1178 { 1179 device_t dev = slot->dev; 1180 struct siis_channel *ch = device_get_softc(dev); 1181 union ccb *ccb = slot->ccb; 1182 1183 mtx_assert(&ch->mtx, MA_OWNED); 1184 /* Check for stale timeout. */ 1185 if (slot->state < SIIS_SLOT_RUNNING) 1186 return; 1187 1188 /* Handle soft-reset timeouts without doing hard-reset. */ 1189 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1190 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1191 (ccb->ataio.cmd.control & ATA_A_RESET)) { 1192 xpt_freeze_simq(ch->sim, ch->numrslots); 1193 siis_end_transaction(slot, SIIS_ERR_TFE); 1194 return; 1195 } 1196 1197 device_printf(dev, "Timeout on slot %d\n", slot->slot); 1198 device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 1199 __func__, ATA_INL(ch->r_mem, SIIS_P_IS), 1200 ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 1201 ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 1202 ATA_INL(ch->r_mem, SIIS_P_SERR)); 1203 1204 if (ch->toslots == 0) 1205 xpt_freeze_simq(ch->sim, 1); 1206 ch->toslots |= (1 << slot->slot); 1207 if ((ch->rslots & ~ch->toslots) == 0) 1208 siis_process_timeout(dev); 1209 else 1210 device_printf(dev, " ... waiting for slots %08x\n", 1211 ch->rslots & ~ch->toslots); 1212 } 1213 1214 /* Must be called with channel locked. */ 1215 static void 1216 siis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 1217 { 1218 device_t dev = slot->dev; 1219 struct siis_channel *ch = device_get_softc(dev); 1220 union ccb *ccb = slot->ccb; 1221 int lastto; 1222 1223 mtx_assert(&ch->mtx, MA_OWNED); 1224 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1225 BUS_DMASYNC_POSTWRITE); 1226 /* Read result registers to the result struct 1227 * May be incorrect if several commands finished same time, 1228 * so read only when sure or have to. 1229 */ 1230 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1231 struct ata_res *res = &ccb->ataio.res; 1232 if ((et == SIIS_ERR_TFE) || 1233 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1234 int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 1235 1236 res->status = ATA_INB(ch->r_mem, offs + 2); 1237 res->error = ATA_INB(ch->r_mem, offs + 3); 1238 res->lba_low = ATA_INB(ch->r_mem, offs + 4); 1239 res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 1240 res->lba_high = ATA_INB(ch->r_mem, offs + 6); 1241 res->device = ATA_INB(ch->r_mem, offs + 7); 1242 res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 1243 res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 1244 res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 1245 res->sector_count = ATA_INB(ch->r_mem, offs + 12); 1246 res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 1247 } else 1248 bzero(res, sizeof(*res)); 1249 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1250 ch->numrslots == 1) { 1251 ccb->ataio.resid = ccb->ataio.dxfer_len - 1252 ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1253 } 1254 } else { 1255 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1256 ch->numrslots == 1) { 1257 ccb->csio.resid = ccb->csio.dxfer_len - 1258 ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1259 } 1260 } 1261 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1262 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1263 (ccb->ccb_h.flags & CAM_DIR_IN) ? 1264 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1265 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1266 } 1267 /* Set proper result status. */ 1268 if (et != SIIS_ERR_NONE || ch->recovery) { 1269 ch->eslots |= (1 << slot->slot); 1270 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1271 } 1272 /* In case of error, freeze device for proper recovery. */ 1273 if (et != SIIS_ERR_NONE && (!ch->recoverycmd) && 1274 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1275 xpt_freeze_devq(ccb->ccb_h.path, 1); 1276 ccb->ccb_h.status |= CAM_DEV_QFRZN; 1277 } 1278 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1279 switch (et) { 1280 case SIIS_ERR_NONE: 1281 ccb->ccb_h.status |= CAM_REQ_CMP; 1282 if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1283 ccb->csio.scsi_status = SCSI_STATUS_OK; 1284 break; 1285 case SIIS_ERR_INVALID: 1286 ch->fatalerr = 1; 1287 ccb->ccb_h.status |= CAM_REQ_INVALID; 1288 break; 1289 case SIIS_ERR_INNOCENT: 1290 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1291 break; 1292 case SIIS_ERR_TFE: 1293 case SIIS_ERR_NCQ: 1294 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1295 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1296 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1297 } else { 1298 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1299 } 1300 break; 1301 case SIIS_ERR_SATA: 1302 ch->fatalerr = 1; 1303 ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1304 break; 1305 case SIIS_ERR_TIMEOUT: 1306 ch->fatalerr = 1; 1307 ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1308 break; 1309 default: 1310 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1311 } 1312 /* Free slot. */ 1313 ch->oslots &= ~(1 << slot->slot); 1314 ch->rslots &= ~(1 << slot->slot); 1315 ch->aslots &= ~(1 << slot->slot); 1316 slot->state = SIIS_SLOT_EMPTY; 1317 slot->ccb = NULL; 1318 /* Update channel stats. */ 1319 ch->numrslots--; 1320 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1321 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1322 ch->numtslots[ccb->ccb_h.target_id]--; 1323 } 1324 /* Cancel timeout state if request completed normally. */ 1325 if (et != SIIS_ERR_TIMEOUT) { 1326 lastto = (ch->toslots == (1 << slot->slot)); 1327 ch->toslots &= ~(1 << slot->slot); 1328 if (lastto) 1329 xpt_release_simq(ch->sim, TRUE); 1330 } 1331 /* If it was our READ LOG command - process it. */ 1332 if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 1333 siis_process_read_log(dev, ccb); 1334 /* If it was our REQUEST SENSE command - process it. */ 1335 } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 1336 siis_process_request_sense(dev, ccb); 1337 /* If it was NCQ or ATAPI command error, put result on hold. */ 1338 } else if (et == SIIS_ERR_NCQ || 1339 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 1340 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 1341 ch->hold[slot->slot] = ccb; 1342 ch->numhslots++; 1343 } else 1344 xpt_done(ccb); 1345 /* If we have no other active commands, ... */ 1346 if (ch->rslots == 0) { 1347 /* if there were timeouts or fatal error - reset port. */ 1348 if (ch->toslots != 0 || ch->fatalerr) { 1349 siis_reset(dev); 1350 } else { 1351 /* if we have slots in error, we can reinit port. */ 1352 if (ch->eslots != 0) 1353 siis_portinit(dev); 1354 /* if there commands on hold, we can do recovery. */ 1355 if (!ch->recoverycmd && ch->numhslots) 1356 siis_issue_recovery(dev); 1357 } 1358 /* If all the reset of commands are in timeout - abort them. */ 1359 } else if ((ch->rslots & ~ch->toslots) == 0 && 1360 et != SIIS_ERR_TIMEOUT) 1361 siis_rearm_timeout(dev); 1362 /* Unfreeze frozen command. */ 1363 if (ch->frozen && !siis_check_collision(dev, ch->frozen)) { 1364 union ccb *fccb = ch->frozen; 1365 ch->frozen = NULL; 1366 siis_begin_transaction(dev, fccb); 1367 xpt_release_simq(ch->sim, TRUE); 1368 } 1369 } 1370 1371 static void 1372 siis_issue_recovery(device_t dev) 1373 { 1374 struct siis_channel *ch = device_get_softc(dev); 1375 union ccb *ccb; 1376 struct ccb_ataio *ataio; 1377 struct ccb_scsiio *csio; 1378 int i; 1379 1380 /* Find some held command. */ 1381 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1382 if (ch->hold[i]) 1383 break; 1384 } 1385 if (i == SIIS_MAX_SLOTS) 1386 return; 1387 ccb = xpt_alloc_ccb_nowait(); 1388 if (ccb == NULL) { 1389 device_printf(dev, "Unable to allocate recovery command\n"); 1390 completeall: 1391 /* We can't do anything -- complete held commands. */ 1392 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1393 if (ch->hold[i] == NULL) 1394 continue; 1395 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1396 ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL; 1397 xpt_done(ch->hold[i]); 1398 ch->hold[i] = NULL; 1399 ch->numhslots--; 1400 } 1401 siis_reset(dev); 1402 return; 1403 } 1404 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1405 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1406 /* READ LOG */ 1407 ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 1408 ccb->ccb_h.func_code = XPT_ATA_IO; 1409 ccb->ccb_h.flags = CAM_DIR_IN; 1410 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1411 ataio = &ccb->ataio; 1412 ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 1413 if (ataio->data_ptr == NULL) { 1414 xpt_free_ccb(ccb); 1415 device_printf(dev, 1416 "Unable to allocate memory for READ LOG command\n"); 1417 goto completeall; 1418 } 1419 ataio->dxfer_len = 512; 1420 bzero(&ataio->cmd, sizeof(ataio->cmd)); 1421 ataio->cmd.flags = CAM_ATAIO_48BIT; 1422 ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1423 ataio->cmd.sector_count = 1; 1424 ataio->cmd.sector_count_exp = 0; 1425 ataio->cmd.lba_low = 0x10; 1426 ataio->cmd.lba_mid = 0; 1427 ataio->cmd.lba_mid_exp = 0; 1428 } else { 1429 /* REQUEST SENSE */ 1430 ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 1431 ccb->ccb_h.recovery_slot = i; 1432 ccb->ccb_h.func_code = XPT_SCSI_IO; 1433 ccb->ccb_h.flags = CAM_DIR_IN; 1434 ccb->ccb_h.status = 0; 1435 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1436 csio = &ccb->csio; 1437 csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 1438 csio->dxfer_len = ch->hold[i]->csio.sense_len; 1439 csio->cdb_len = 6; 1440 bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 1441 csio->cdb_io.cdb_bytes[0] = 0x03; 1442 csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 1443 } 1444 ch->recoverycmd = 1; 1445 siis_begin_transaction(dev, ccb); 1446 } 1447 1448 static void 1449 siis_process_read_log(device_t dev, union ccb *ccb) 1450 { 1451 struct siis_channel *ch = device_get_softc(dev); 1452 uint8_t *data; 1453 struct ata_res *res; 1454 int i; 1455 1456 ch->recoverycmd = 0; 1457 data = ccb->ataio.data_ptr; 1458 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1459 (data[0] & 0x80) == 0) { 1460 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1461 if (!ch->hold[i]) 1462 continue; 1463 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1464 continue; 1465 if ((data[0] & 0x1F) == i) { 1466 res = &ch->hold[i]->ataio.res; 1467 res->status = data[2]; 1468 res->error = data[3]; 1469 res->lba_low = data[4]; 1470 res->lba_mid = data[5]; 1471 res->lba_high = data[6]; 1472 res->device = data[7]; 1473 res->lba_low_exp = data[8]; 1474 res->lba_mid_exp = data[9]; 1475 res->lba_high_exp = data[10]; 1476 res->sector_count = data[12]; 1477 res->sector_count_exp = data[13]; 1478 } else { 1479 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1480 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1481 } 1482 xpt_done(ch->hold[i]); 1483 ch->hold[i] = NULL; 1484 ch->numhslots--; 1485 } 1486 } else { 1487 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1488 device_printf(dev, "Error while READ LOG EXT\n"); 1489 else if ((data[0] & 0x80) == 0) { 1490 device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1491 } 1492 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1493 if (!ch->hold[i]) 1494 continue; 1495 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1496 continue; 1497 xpt_done(ch->hold[i]); 1498 ch->hold[i] = NULL; 1499 ch->numhslots--; 1500 } 1501 } 1502 free(ccb->ataio.data_ptr, M_SIIS); 1503 xpt_free_ccb(ccb); 1504 } 1505 1506 static void 1507 siis_process_request_sense(device_t dev, union ccb *ccb) 1508 { 1509 struct siis_channel *ch = device_get_softc(dev); 1510 int i; 1511 1512 ch->recoverycmd = 0; 1513 1514 i = ccb->ccb_h.recovery_slot; 1515 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1516 ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 1517 } else { 1518 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1519 ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 1520 } 1521 xpt_done(ch->hold[i]); 1522 ch->hold[i] = NULL; 1523 ch->numhslots--; 1524 xpt_free_ccb(ccb); 1525 } 1526 1527 static void 1528 siis_portinit(device_t dev) 1529 { 1530 struct siis_channel *ch = device_get_softc(dev); 1531 int i; 1532 1533 ch->eslots = 0; 1534 ch->recovery = 0; 1535 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 1536 for (i = 0; i < 16; i++) { 1537 ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 1538 ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 1539 } 1540 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 1541 siis_wait_ready(dev, 1000); 1542 } 1543 1544 static int 1545 siis_devreset(device_t dev) 1546 { 1547 struct siis_channel *ch = device_get_softc(dev); 1548 int timeout = 0; 1549 uint32_t val; 1550 1551 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 1552 while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1553 SIIS_P_CTL_DEV_RESET) != 0) { 1554 DELAY(100); 1555 if (timeout++ > 1000) { 1556 device_printf(dev, "device reset stuck " 1557 "(timeout 100ms) status = %08x\n", val); 1558 return (EBUSY); 1559 } 1560 } 1561 return (0); 1562 } 1563 1564 static int 1565 siis_wait_ready(device_t dev, int t) 1566 { 1567 struct siis_channel *ch = device_get_softc(dev); 1568 int timeout = 0; 1569 uint32_t val; 1570 1571 while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1572 SIIS_P_CTL_READY) == 0) { 1573 DELAY(1000); 1574 if (timeout++ > t) { 1575 device_printf(dev, "port is not ready (timeout %dms) " 1576 "status = %08x\n", t, val); 1577 return (EBUSY); 1578 } 1579 } 1580 return (0); 1581 } 1582 1583 static void 1584 siis_reset(device_t dev) 1585 { 1586 struct siis_channel *ch = device_get_softc(dev); 1587 int i, retry = 0, sata_rev; 1588 uint32_t val; 1589 1590 xpt_freeze_simq(ch->sim, 1); 1591 if (bootverbose) 1592 device_printf(dev, "SIIS reset...\n"); 1593 if (!ch->recoverycmd && !ch->recovery) 1594 xpt_freeze_simq(ch->sim, ch->numrslots); 1595 /* Requeue frozen command. */ 1596 if (ch->frozen) { 1597 union ccb *fccb = ch->frozen; 1598 ch->frozen = NULL; 1599 fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1600 fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1601 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1602 xpt_freeze_devq(fccb->ccb_h.path, 1); 1603 fccb->ccb_h.status |= CAM_DEV_QFRZN; 1604 } 1605 xpt_done(fccb); 1606 } 1607 /* Requeue all running commands. */ 1608 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1609 /* Do we have a running request on slot? */ 1610 if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1611 continue; 1612 /* XXX; Commands in loading state. */ 1613 siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1614 } 1615 /* Finish all held commands as-is. */ 1616 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1617 if (!ch->hold[i]) 1618 continue; 1619 xpt_done(ch->hold[i]); 1620 ch->hold[i] = NULL; 1621 ch->numhslots--; 1622 } 1623 if (ch->toslots != 0) 1624 xpt_release_simq(ch->sim, TRUE); 1625 ch->eslots = 0; 1626 ch->recovery = 0; 1627 ch->toslots = 0; 1628 ch->fatalerr = 0; 1629 /* Disable port interrupts */ 1630 ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 1631 /* Set speed limit. */ 1632 sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 1633 if (sata_rev == 1) 1634 val = ATA_SC_SPD_SPEED_GEN1; 1635 else if (sata_rev == 2) 1636 val = ATA_SC_SPD_SPEED_GEN2; 1637 else if (sata_rev == 3) 1638 val = ATA_SC_SPD_SPEED_GEN3; 1639 else 1640 val = 0; 1641 ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 1642 ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 1643 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 1644 retry: 1645 siis_devreset(dev); 1646 /* Reset and reconnect PHY, */ 1647 if (!siis_sata_connect(ch)) { 1648 ch->devices = 0; 1649 /* Enable port interrupts */ 1650 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1651 if (bootverbose) 1652 device_printf(dev, 1653 "SIIS reset done: phy reset found no device\n"); 1654 /* Tell the XPT about the event */ 1655 xpt_async(AC_BUS_RESET, ch->path, NULL); 1656 xpt_release_simq(ch->sim, TRUE); 1657 return; 1658 } 1659 /* Wait for port ready status. */ 1660 if (siis_wait_ready(dev, 1000)) { 1661 device_printf(dev, "port ready timeout\n"); 1662 if (!retry) { 1663 device_printf(dev, "trying full port reset ...\n"); 1664 /* Get port to the reset state. */ 1665 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 1666 DELAY(10000); 1667 /* Get port out of reset state. */ 1668 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 1669 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 1670 if (ch->pm_present) 1671 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1672 else 1673 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1674 siis_wait_ready(dev, 5000); 1675 retry = 1; 1676 goto retry; 1677 } 1678 } 1679 ch->devices = 1; 1680 /* Enable port interrupts */ 1681 ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 1682 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1683 if (bootverbose) 1684 device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 1685 /* Tell the XPT about the event */ 1686 xpt_async(AC_BUS_RESET, ch->path, NULL); 1687 xpt_release_simq(ch->sim, TRUE); 1688 } 1689 1690 static int 1691 siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag) 1692 { 1693 struct siis_channel *ch = device_get_softc(dev); 1694 u_int8_t *fis = &ctp->fis[0]; 1695 1696 bzero(fis, 24); 1697 fis[0] = 0x27; /* host to device */ 1698 fis[1] = (ccb->ccb_h.target_id & 0x0f); 1699 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1700 fis[1] |= 0x80; 1701 fis[2] = ATA_PACKET_CMD; 1702 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1703 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 1704 fis[3] = ATA_F_DMA; 1705 else { 1706 fis[5] = ccb->csio.dxfer_len; 1707 fis[6] = ccb->csio.dxfer_len >> 8; 1708 } 1709 fis[7] = ATA_D_LBA; 1710 fis[15] = ATA_A_4BIT; 1711 bzero(ctp->u.atapi.ccb, 16); 1712 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1713 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1714 ctp->u.atapi.ccb, ccb->csio.cdb_len); 1715 } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1716 fis[1] |= 0x80; 1717 fis[2] = ccb->ataio.cmd.command; 1718 fis[3] = ccb->ataio.cmd.features; 1719 fis[4] = ccb->ataio.cmd.lba_low; 1720 fis[5] = ccb->ataio.cmd.lba_mid; 1721 fis[6] = ccb->ataio.cmd.lba_high; 1722 fis[7] = ccb->ataio.cmd.device; 1723 fis[8] = ccb->ataio.cmd.lba_low_exp; 1724 fis[9] = ccb->ataio.cmd.lba_mid_exp; 1725 fis[10] = ccb->ataio.cmd.lba_high_exp; 1726 fis[11] = ccb->ataio.cmd.features_exp; 1727 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1728 fis[12] = tag << 3; 1729 fis[13] = 0; 1730 } else { 1731 fis[12] = ccb->ataio.cmd.sector_count; 1732 fis[13] = ccb->ataio.cmd.sector_count_exp; 1733 } 1734 fis[15] = ATA_A_4BIT; 1735 } else { 1736 /* Soft reset. */ 1737 } 1738 return (20); 1739 } 1740 1741 static int 1742 siis_sata_connect(struct siis_channel *ch) 1743 { 1744 u_int32_t status; 1745 int timeout, found = 0; 1746 1747 /* Wait up to 100ms for "connect well" */ 1748 for (timeout = 0; timeout < 1000 ; timeout++) { 1749 status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 1750 if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE) 1751 found = 1; 1752 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1753 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1754 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 1755 break; 1756 if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 1757 if (bootverbose) { 1758 device_printf(ch->dev, "SATA offline status=%08x\n", 1759 status); 1760 } 1761 return (0); 1762 } 1763 if (found == 0 && timeout >= 100) 1764 break; 1765 DELAY(100); 1766 } 1767 if (timeout >= 1000 || !found) { 1768 if (bootverbose) { 1769 device_printf(ch->dev, 1770 "SATA connect timeout time=%dus status=%08x\n", 1771 timeout * 100, status); 1772 } 1773 return (0); 1774 } 1775 if (bootverbose) { 1776 device_printf(ch->dev, "SATA connect time=%dus status=%08x\n", 1777 timeout * 100, status); 1778 } 1779 /* Clear SATA error register */ 1780 ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 1781 return (1); 1782 } 1783 1784 static int 1785 siis_check_ids(device_t dev, union ccb *ccb) 1786 { 1787 1788 if (ccb->ccb_h.target_id > 15) { 1789 ccb->ccb_h.status = CAM_TID_INVALID; 1790 xpt_done(ccb); 1791 return (-1); 1792 } 1793 if (ccb->ccb_h.target_lun != 0) { 1794 ccb->ccb_h.status = CAM_LUN_INVALID; 1795 xpt_done(ccb); 1796 return (-1); 1797 } 1798 return (0); 1799 } 1800 1801 static void 1802 siisaction(struct cam_sim *sim, union ccb *ccb) 1803 { 1804 device_t dev, parent; 1805 struct siis_channel *ch; 1806 1807 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 1808 ccb->ccb_h.func_code)); 1809 1810 ch = (struct siis_channel *)cam_sim_softc(sim); 1811 dev = ch->dev; 1812 mtx_assert(&ch->mtx, MA_OWNED); 1813 switch (ccb->ccb_h.func_code) { 1814 /* Common cases first */ 1815 case XPT_ATA_IO: /* Execute the requested I/O operation */ 1816 case XPT_SCSI_IO: 1817 if (siis_check_ids(dev, ccb)) 1818 return; 1819 if (ch->devices == 0 || 1820 (ch->pm_present == 0 && 1821 ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 1822 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1823 break; 1824 } 1825 ccb->ccb_h.recovery_type = RECOVERY_NONE; 1826 /* Check for command collision. */ 1827 if (siis_check_collision(dev, ccb)) { 1828 /* Freeze command. */ 1829 ch->frozen = ccb; 1830 /* We have only one frozen slot, so freeze simq also. */ 1831 xpt_freeze_simq(ch->sim, 1); 1832 return; 1833 } 1834 siis_begin_transaction(dev, ccb); 1835 return; 1836 case XPT_EN_LUN: /* Enable LUN as a target */ 1837 case XPT_TARGET_IO: /* Execute target I/O request */ 1838 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1839 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1840 case XPT_ABORT: /* Abort the specified CCB */ 1841 /* XXX Implement */ 1842 ccb->ccb_h.status = CAM_REQ_INVALID; 1843 break; 1844 case XPT_SET_TRAN_SETTINGS: 1845 { 1846 struct ccb_trans_settings *cts = &ccb->cts; 1847 struct siis_device *d; 1848 1849 if (siis_check_ids(dev, ccb)) 1850 return; 1851 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1852 d = &ch->curr[ccb->ccb_h.target_id]; 1853 else 1854 d = &ch->user[ccb->ccb_h.target_id]; 1855 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 1856 d->revision = cts->xport_specific.sata.revision; 1857 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 1858 d->mode = cts->xport_specific.sata.mode; 1859 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 1860 d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 1861 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1862 d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags); 1863 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 1864 ch->pm_present = cts->xport_specific.sata.pm_present; 1865 if (ch->pm_present) 1866 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1867 else 1868 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1869 } 1870 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1871 d->atapi = cts->xport_specific.sata.atapi; 1872 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1873 d->caps = cts->xport_specific.sata.caps; 1874 ccb->ccb_h.status = CAM_REQ_CMP; 1875 break; 1876 } 1877 case XPT_GET_TRAN_SETTINGS: 1878 /* Get default/user set transfer settings for the target */ 1879 { 1880 struct ccb_trans_settings *cts = &ccb->cts; 1881 struct siis_device *d; 1882 uint32_t status; 1883 1884 if (siis_check_ids(dev, ccb)) 1885 return; 1886 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1887 d = &ch->curr[ccb->ccb_h.target_id]; 1888 else 1889 d = &ch->user[ccb->ccb_h.target_id]; 1890 cts->protocol = PROTO_ATA; 1891 cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1892 cts->transport = XPORT_SATA; 1893 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1894 cts->proto_specific.valid = 0; 1895 cts->xport_specific.sata.valid = 0; 1896 if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1897 (ccb->ccb_h.target_id == 15 || 1898 (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 1899 status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1900 if (status & 0x0f0) { 1901 cts->xport_specific.sata.revision = 1902 (status & 0x0f0) >> 4; 1903 cts->xport_specific.sata.valid |= 1904 CTS_SATA_VALID_REVISION; 1905 } 1906 cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 1907 if (ch->pm_level) 1908 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 1909 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN; 1910 cts->xport_specific.sata.caps &= 1911 ch->user[ccb->ccb_h.target_id].caps; 1912 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1913 } else { 1914 cts->xport_specific.sata.revision = d->revision; 1915 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 1916 cts->xport_specific.sata.caps = d->caps; 1917 if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1918 (ch->quirks & SIIS_Q_SNTF) == 0) 1919 cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN; 1920 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1921 } 1922 cts->xport_specific.sata.mode = d->mode; 1923 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 1924 cts->xport_specific.sata.bytecount = d->bytecount; 1925 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 1926 cts->xport_specific.sata.pm_present = ch->pm_present; 1927 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1928 cts->xport_specific.sata.tags = d->tags; 1929 cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 1930 cts->xport_specific.sata.atapi = d->atapi; 1931 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 1932 ccb->ccb_h.status = CAM_REQ_CMP; 1933 break; 1934 } 1935 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1936 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1937 siis_reset(dev); 1938 ccb->ccb_h.status = CAM_REQ_CMP; 1939 break; 1940 case XPT_TERM_IO: /* Terminate the I/O process */ 1941 /* XXX Implement */ 1942 ccb->ccb_h.status = CAM_REQ_INVALID; 1943 break; 1944 case XPT_PATH_INQ: /* Path routing inquiry */ 1945 { 1946 struct ccb_pathinq *cpi = &ccb->cpi; 1947 1948 parent = device_get_parent(dev); 1949 cpi->version_num = 1; /* XXX??? */ 1950 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 1951 cpi->hba_inquiry |= PI_SATAPM; 1952 cpi->target_sprt = 0; 1953 cpi->hba_misc = PIM_SEQSCAN; 1954 cpi->hba_eng_cnt = 0; 1955 cpi->max_target = 15; 1956 cpi->max_lun = 0; 1957 cpi->initiator_id = 0; 1958 cpi->bus_id = cam_sim_bus(sim); 1959 cpi->base_transfer_speed = 150000; 1960 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1961 strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 1962 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1963 cpi->unit_number = cam_sim_unit(sim); 1964 cpi->transport = XPORT_SATA; 1965 cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1966 cpi->protocol = PROTO_ATA; 1967 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1968 cpi->maxio = MAXPHYS; 1969 cpi->hba_vendor = pci_get_vendor(parent); 1970 cpi->hba_device = pci_get_device(parent); 1971 cpi->hba_subvendor = pci_get_subvendor(parent); 1972 cpi->hba_subdevice = pci_get_subdevice(parent); 1973 cpi->ccb_h.status = CAM_REQ_CMP; 1974 break; 1975 } 1976 default: 1977 ccb->ccb_h.status = CAM_REQ_INVALID; 1978 break; 1979 } 1980 xpt_done(ccb); 1981 } 1982 1983 static void 1984 siispoll(struct cam_sim *sim) 1985 { 1986 struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 1987 1988 siis_ch_intr(ch->dev); 1989 } 1990