siis.c revision 198426
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: head/sys/dev/siis/siis.c 198426 2009-10-23 21:33:26Z mav $"); 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/pci/pcivar.h> 48#include <dev/pci/pcireg.h> 49#include "siis.h" 50 51#include <cam/cam.h> 52#include <cam/cam_ccb.h> 53#include <cam/cam_sim.h> 54#include <cam/cam_xpt_sim.h> 55#include <cam/cam_xpt_periph.h> 56#include <cam/cam_debug.h> 57 58/* local prototypes */ 59static int siis_setup_interrupt(device_t dev); 60static void siis_intr(void *data); 61static int siis_suspend(device_t dev); 62static int siis_resume(device_t dev); 63static int siis_ch_suspend(device_t dev); 64static int siis_ch_resume(device_t dev); 65static void siis_ch_intr_locked(void *data); 66static void siis_ch_intr(void *data); 67static void siis_begin_transaction(device_t dev, union ccb *ccb); 68static void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 69static void siis_execute_transaction(struct siis_slot *slot); 70static void siis_timeout(struct siis_slot *slot); 71static void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et); 72static int siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag); 73static void siis_dmainit(device_t dev); 74static void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 75static void siis_dmafini(device_t dev); 76static void siis_slotsalloc(device_t dev); 77static void siis_slotsfree(device_t dev); 78static void siis_reset(device_t dev); 79static void siis_portinit(device_t dev); 80static int siis_wait_ready(device_t dev, int t); 81 82static int siis_sata_connect(struct siis_channel *ch); 83 84static void siis_issue_read_log(device_t dev); 85static void siis_process_read_log(device_t dev, union ccb *ccb); 86 87static void siisaction(struct cam_sim *sim, union ccb *ccb); 88static void siispoll(struct cam_sim *sim); 89 90MALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers"); 91 92static int 93siis_probe(device_t dev) 94{ 95 uint32_t devid = pci_get_devid(dev); 96 97 if (devid == SIIS_SII3124) { 98 device_set_desc_copy(dev, "SiI3124 SATA2 controller"); 99 } else if (devid == SIIS_SII3132 || 100 devid == SIIS_SII3132_1 || 101 devid == SIIS_SII3132_2) { 102 device_set_desc_copy(dev, "SiI3132 SATA2 controller"); 103 } else if (devid == SIIS_SII3531) { 104 device_set_desc_copy(dev, "SiI3531 SATA2 controller"); 105 } else { 106 return (ENXIO); 107 } 108 109 return (BUS_PROBE_VENDOR); 110} 111 112static int 113siis_attach(device_t dev) 114{ 115 struct siis_controller *ctlr = device_get_softc(dev); 116 uint32_t devid = pci_get_devid(dev); 117 device_t child; 118 int error, unit; 119 120 ctlr->dev = dev; 121 /* Global memory */ 122 ctlr->r_grid = PCIR_BAR(0); 123 if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 124 &ctlr->r_grid, RF_ACTIVE))) 125 return (ENXIO); 126 /* Channels memory */ 127 ctlr->r_rid = PCIR_BAR(2); 128 if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 129 &ctlr->r_rid, RF_ACTIVE))) 130 return (ENXIO); 131 /* Setup our own memory management for channels. */ 132 ctlr->sc_iomem.rm_type = RMAN_ARRAY; 133 ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 134 if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 135 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 136 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 137 return (error); 138 } 139 if ((error = rman_manage_region(&ctlr->sc_iomem, 140 rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 141 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 142 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 143 rman_fini(&ctlr->sc_iomem); 144 return (error); 145 } 146 /* Reset controller */ 147 siis_resume(dev); 148 /* Number of HW channels */ 149 ctlr->channels = (devid == SIIS_SII3124) ? 4 : 150 (devid == SIIS_SII3531 ? 1 : 2); 151 /* Setup interrupts. */ 152 if (siis_setup_interrupt(dev)) { 153 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 154 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 155 rman_fini(&ctlr->sc_iomem); 156 return ENXIO; 157 } 158 /* Attach all channels on this controller */ 159 for (unit = 0; unit < ctlr->channels; unit++) { 160 child = device_add_child(dev, "siisch", -1); 161 if (child == NULL) 162 device_printf(dev, "failed to add channel device\n"); 163 else 164 device_set_ivars(child, (void *)(intptr_t)unit); 165 } 166 bus_generic_attach(dev); 167 return 0; 168} 169 170static int 171siis_detach(device_t dev) 172{ 173 struct siis_controller *ctlr = device_get_softc(dev); 174 device_t *children; 175 int nchildren, i; 176 177 /* Detach & delete all children */ 178 if (!device_get_children(dev, &children, &nchildren)) { 179 for (i = 0; i < nchildren; i++) 180 device_delete_child(dev, children[i]); 181 free(children, M_TEMP); 182 } 183 /* Free interrupts. */ 184 if (ctlr->irq.r_irq) { 185 bus_teardown_intr(dev, ctlr->irq.r_irq, 186 ctlr->irq.handle); 187 bus_release_resource(dev, SYS_RES_IRQ, 188 ctlr->irq.r_irq_rid, ctlr->irq.r_irq); 189 } 190 pci_release_msi(dev); 191 /* Free memory. */ 192 rman_fini(&ctlr->sc_iomem); 193 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 194 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 195 return (0); 196} 197 198static int 199siis_suspend(device_t dev) 200{ 201 struct siis_controller *ctlr = device_get_softc(dev); 202 203 bus_generic_suspend(dev); 204 /* Put controller into reset state. */ 205 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET); 206 return 0; 207} 208 209static int 210siis_resume(device_t dev) 211{ 212 struct siis_controller *ctlr = device_get_softc(dev); 213 214 /* Put controller into reset state. */ 215 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET); 216 DELAY(10000); 217 /* Get controller out of reset state and enable port interrupts. */ 218 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 0x0000000f); 219 return (bus_generic_resume(dev)); 220} 221 222static int 223siis_setup_interrupt(device_t dev) 224{ 225 struct siis_controller *ctlr = device_get_softc(dev); 226 int msi = 0; 227 228 /* Process hints. */ 229 resource_int_value(device_get_name(dev), 230 device_get_unit(dev), "msi", &msi); 231 if (msi < 0) 232 msi = 0; 233 else if (msi > 0) 234 msi = min(1, pci_msi_count(dev)); 235 /* Allocate MSI if needed/present. */ 236 if (msi && pci_alloc_msi(dev, &msi) != 0) 237 msi = 0; 238 /* Allocate all IRQs. */ 239 ctlr->irq.r_irq_rid = msi ? 1 : 0; 240 if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 241 &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 242 device_printf(dev, "unable to map interrupt\n"); 243 return ENXIO; 244 } 245 if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL, 246 siis_intr, ctlr, &ctlr->irq.handle))) { 247 /* SOS XXX release r_irq */ 248 device_printf(dev, "unable to setup interrupt\n"); 249 return ENXIO; 250 } 251 return (0); 252} 253 254/* 255 * Common case interrupt handler. 256 */ 257static void 258siis_intr(void *data) 259{ 260 struct siis_controller *ctlr = (struct siis_controller *)data; 261 u_int32_t is; 262 void *arg; 263 int unit; 264 265 is = ATA_INL(ctlr->r_gmem, SIIS_IS); 266 for (unit = 0; unit < ctlr->channels; unit++) { 267 if ((is & SIIS_IS_PORT(unit)) != 0 && 268 (arg = ctlr->interrupt[unit].argument)) { 269 ctlr->interrupt[unit].function(arg); 270 } 271 } 272} 273 274static struct resource * 275siis_alloc_resource(device_t dev, device_t child, int type, int *rid, 276 u_long start, u_long end, u_long count, u_int flags) 277{ 278 struct siis_controller *ctlr = device_get_softc(dev); 279 int unit = ((struct siis_channel *)device_get_softc(child))->unit; 280 struct resource *res = NULL; 281 int offset = unit << 13; 282 long st; 283 284 switch (type) { 285 case SYS_RES_MEMORY: 286 st = rman_get_start(ctlr->r_mem); 287 res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 288 st + offset + 0x2000, 0x2000, RF_ACTIVE, child); 289 if (res) { 290 bus_space_handle_t bsh; 291 bus_space_tag_t bst; 292 bsh = rman_get_bushandle(ctlr->r_mem); 293 bst = rman_get_bustag(ctlr->r_mem); 294 bus_space_subregion(bst, bsh, offset, 0x2000, &bsh); 295 rman_set_bushandle(res, bsh); 296 rman_set_bustag(res, bst); 297 } 298 break; 299 case SYS_RES_IRQ: 300 if (*rid == ATA_IRQ_RID) 301 res = ctlr->irq.r_irq; 302 break; 303 } 304 return (res); 305} 306 307static int 308siis_release_resource(device_t dev, device_t child, int type, int rid, 309 struct resource *r) 310{ 311 312 switch (type) { 313 case SYS_RES_MEMORY: 314 rman_release_resource(r); 315 return (0); 316 case SYS_RES_IRQ: 317 if (rid != ATA_IRQ_RID) 318 return ENOENT; 319 return (0); 320 } 321 return (EINVAL); 322} 323 324static int 325siis_setup_intr(device_t dev, device_t child, struct resource *irq, 326 int flags, driver_filter_t *filter, driver_intr_t *function, 327 void *argument, void **cookiep) 328{ 329 struct siis_controller *ctlr = device_get_softc(dev); 330 int unit = (intptr_t)device_get_ivars(child); 331 332 if (filter != NULL) { 333 printf("siis.c: we cannot use a filter here\n"); 334 return (EINVAL); 335 } 336 ctlr->interrupt[unit].function = function; 337 ctlr->interrupt[unit].argument = argument; 338 return (0); 339} 340 341static int 342siis_teardown_intr(device_t dev, device_t child, struct resource *irq, 343 void *cookie) 344{ 345 struct siis_controller *ctlr = device_get_softc(dev); 346 int unit = (intptr_t)device_get_ivars(child); 347 348 ctlr->interrupt[unit].function = NULL; 349 ctlr->interrupt[unit].argument = NULL; 350 return (0); 351} 352 353static int 354siis_print_child(device_t dev, device_t child) 355{ 356 int retval; 357 358 retval = bus_print_child_header(dev, child); 359 retval += printf(" at channel %d", 360 (int)(intptr_t)device_get_ivars(child)); 361 retval += bus_print_child_footer(dev, child); 362 363 return (retval); 364} 365 366devclass_t siis_devclass; 367static device_method_t siis_methods[] = { 368 DEVMETHOD(device_probe, siis_probe), 369 DEVMETHOD(device_attach, siis_attach), 370 DEVMETHOD(device_detach, siis_detach), 371 DEVMETHOD(device_suspend, siis_suspend), 372 DEVMETHOD(device_resume, siis_resume), 373 DEVMETHOD(bus_print_child, siis_print_child), 374 DEVMETHOD(bus_alloc_resource, siis_alloc_resource), 375 DEVMETHOD(bus_release_resource, siis_release_resource), 376 DEVMETHOD(bus_setup_intr, siis_setup_intr), 377 DEVMETHOD(bus_teardown_intr,siis_teardown_intr), 378 { 0, 0 } 379}; 380static driver_t siis_driver = { 381 "siis", 382 siis_methods, 383 sizeof(struct siis_controller) 384}; 385DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0); 386MODULE_VERSION(siis, 1); 387MODULE_DEPEND(siis, cam, 1, 1, 1); 388 389static int 390siis_ch_probe(device_t dev) 391{ 392 393 device_set_desc_copy(dev, "SIIS channel"); 394 return (0); 395} 396 397static int 398siis_ch_attach(device_t dev) 399{ 400 struct siis_channel *ch = device_get_softc(dev); 401 struct cam_devq *devq; 402 int rid, error; 403 404 ch->dev = dev; 405 ch->unit = (intptr_t)device_get_ivars(dev); 406 resource_int_value(device_get_name(dev), 407 device_get_unit(dev), "pm_level", &ch->pm_level); 408 resource_int_value(device_get_name(dev), 409 device_get_unit(dev), "sata_rev", &ch->sata_rev); 410 mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 411 rid = ch->unit; 412 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 413 &rid, RF_ACTIVE))) 414 return (ENXIO); 415 siis_dmainit(dev); 416 siis_slotsalloc(dev); 417 siis_ch_resume(dev); 418 mtx_lock(&ch->mtx); 419 rid = ATA_IRQ_RID; 420 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 421 &rid, RF_SHAREABLE | RF_ACTIVE))) { 422 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 423 device_printf(dev, "Unable to map interrupt\n"); 424 return (ENXIO); 425 } 426 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 427 siis_ch_intr_locked, dev, &ch->ih))) { 428 device_printf(dev, "Unable to setup interrupt\n"); 429 error = ENXIO; 430 goto err1; 431 } 432 /* Create the device queue for our SIM. */ 433 devq = cam_simq_alloc(SIIS_MAX_SLOTS); 434 if (devq == NULL) { 435 device_printf(dev, "Unable to allocate simq\n"); 436 error = ENOMEM; 437 goto err1; 438 } 439 /* Construct SIM entry */ 440 ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 441 device_get_unit(dev), &ch->mtx, SIIS_MAX_SLOTS, 0, devq); 442 if (ch->sim == NULL) { 443 device_printf(dev, "unable to allocate sim\n"); 444 error = ENOMEM; 445 goto err2; 446 } 447 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 448 device_printf(dev, "unable to register xpt bus\n"); 449 error = ENXIO; 450 goto err2; 451 } 452 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 453 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 454 device_printf(dev, "unable to create path\n"); 455 error = ENXIO; 456 goto err3; 457 } 458 mtx_unlock(&ch->mtx); 459 return (0); 460 461err3: 462 xpt_bus_deregister(cam_sim_path(ch->sim)); 463err2: 464 cam_sim_free(ch->sim, /*free_devq*/TRUE); 465err1: 466 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 467 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 468 mtx_unlock(&ch->mtx); 469 return (error); 470} 471 472static int 473siis_ch_detach(device_t dev) 474{ 475 struct siis_channel *ch = device_get_softc(dev); 476 477 mtx_lock(&ch->mtx); 478 xpt_async(AC_LOST_DEVICE, ch->path, NULL); 479 xpt_free_path(ch->path); 480 xpt_bus_deregister(cam_sim_path(ch->sim)); 481 cam_sim_free(ch->sim, /*free_devq*/TRUE); 482 mtx_unlock(&ch->mtx); 483 484 bus_teardown_intr(dev, ch->r_irq, ch->ih); 485 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 486 487 siis_ch_suspend(dev); 488 siis_slotsfree(dev); 489 siis_dmafini(dev); 490 491 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 492 mtx_destroy(&ch->mtx); 493 return (0); 494} 495 496static int 497siis_ch_suspend(device_t dev) 498{ 499 struct siis_channel *ch = device_get_softc(dev); 500 501 /* Put port into reset state. */ 502 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 503 return (0); 504} 505 506static int 507siis_ch_resume(device_t dev) 508{ 509 struct siis_channel *ch = device_get_softc(dev); 510 511 /* Get port out of reset state. */ 512 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 513 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 514 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 515 /* Enable port interrupts */ 516 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 517 return (0); 518} 519 520devclass_t siisch_devclass; 521static device_method_t siisch_methods[] = { 522 DEVMETHOD(device_probe, siis_ch_probe), 523 DEVMETHOD(device_attach, siis_ch_attach), 524 DEVMETHOD(device_detach, siis_ch_detach), 525 DEVMETHOD(device_suspend, siis_ch_suspend), 526 DEVMETHOD(device_resume, siis_ch_resume), 527 { 0, 0 } 528}; 529static driver_t siisch_driver = { 530 "siisch", 531 siisch_methods, 532 sizeof(struct siis_channel) 533}; 534DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 535 536struct siis_dc_cb_args { 537 bus_addr_t maddr; 538 int error; 539}; 540 541static void 542siis_dmainit(device_t dev) 543{ 544 struct siis_channel *ch = device_get_softc(dev); 545 struct siis_dc_cb_args dcba; 546 547 /* Command area. */ 548 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 549 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 550 NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 551 0, NULL, NULL, &ch->dma.work_tag)) 552 goto error; 553 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 554 &ch->dma.work_map)) 555 goto error; 556 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 557 SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 558 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 559 goto error; 560 } 561 ch->dma.work_bus = dcba.maddr; 562 /* Data area. */ 563 if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 564 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 565 NULL, NULL, 566 SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 567 SIIS_SG_ENTRIES, 0xFFFFFFFF, 568 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 569 goto error; 570 } 571 return; 572 573error: 574 device_printf(dev, "WARNING - DMA initialization failed\n"); 575 siis_dmafini(dev); 576} 577 578static void 579siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 580{ 581 struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 582 583 if (!(dcba->error = error)) 584 dcba->maddr = segs[0].ds_addr; 585} 586 587static void 588siis_dmafini(device_t dev) 589{ 590 struct siis_channel *ch = device_get_softc(dev); 591 592 if (ch->dma.data_tag) { 593 bus_dma_tag_destroy(ch->dma.data_tag); 594 ch->dma.data_tag = NULL; 595 } 596 if (ch->dma.work_bus) { 597 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 598 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 599 ch->dma.work_bus = 0; 600 ch->dma.work_map = NULL; 601 ch->dma.work = NULL; 602 } 603 if (ch->dma.work_tag) { 604 bus_dma_tag_destroy(ch->dma.work_tag); 605 ch->dma.work_tag = NULL; 606 } 607} 608 609static void 610siis_slotsalloc(device_t dev) 611{ 612 struct siis_channel *ch = device_get_softc(dev); 613 int i; 614 615 /* Alloc and setup command/dma slots */ 616 bzero(ch->slot, sizeof(ch->slot)); 617 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 618 struct siis_slot *slot = &ch->slot[i]; 619 620 slot->dev = dev; 621 slot->slot = i; 622 slot->state = SIIS_SLOT_EMPTY; 623 slot->ccb = NULL; 624 callout_init_mtx(&slot->timeout, &ch->mtx, 0); 625 626 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 627 device_printf(ch->dev, "FAILURE - create data_map\n"); 628 } 629} 630 631static void 632siis_slotsfree(device_t dev) 633{ 634 struct siis_channel *ch = device_get_softc(dev); 635 int i; 636 637 /* Free all dma slots */ 638 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 639 struct siis_slot *slot = &ch->slot[i]; 640 641 if (slot->dma.data_map) { 642 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 643 slot->dma.data_map = NULL; 644 } 645 } 646} 647 648static void 649siis_notify_events(device_t dev) 650{ 651 struct siis_channel *ch = device_get_softc(dev); 652 struct cam_path *dpath; 653 u_int32_t status; 654 int i; 655 656 status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 657 ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 658 if (bootverbose) 659 device_printf(dev, "SNTF 0x%04x\n", status); 660 for (i = 0; i < 16; i++) { 661 if ((status & (1 << i)) == 0) 662 continue; 663 if (xpt_create_path(&dpath, NULL, 664 xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 665 xpt_async(AC_SCSI_AEN, dpath, NULL); 666 xpt_free_path(dpath); 667 } 668 } 669 670} 671 672static void 673siis_phy_check_events(device_t dev) 674{ 675 struct siis_channel *ch = device_get_softc(dev); 676 677 /* If we have a connection event, deal with it */ 678 if (ch->pm_level == 0) { 679 u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 680 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 681 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 682 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 683 if (bootverbose) 684 device_printf(dev, "CONNECT requested\n"); 685 siis_reset(dev); 686 } else { 687 if (bootverbose) 688 device_printf(dev, "DISCONNECT requested\n"); 689 ch->devices = 0; 690 } 691 } 692} 693 694static void 695siis_ch_intr_locked(void *data) 696{ 697 device_t dev = (device_t)data; 698 struct siis_channel *ch = device_get_softc(dev); 699 700 mtx_lock(&ch->mtx); 701 siis_ch_intr(data); 702 mtx_unlock(&ch->mtx); 703} 704 705static void 706siis_ch_intr(void *data) 707{ 708 device_t dev = (device_t)data; 709 struct siis_channel *ch = device_get_softc(dev); 710 uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 711 enum siis_err_type et; 712 int i, ccs, port, tslots; 713 714 mtx_assert(&ch->mtx, MA_OWNED); 715 /* Read command statuses. */ 716 sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 717 ok = ch->rslots & ~sstatus; 718 /* Complete all successfull commands. */ 719 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 720 if ((ok >> i) & 1) 721 siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 722 } 723 /* Do we have any other events? */ 724 if ((sstatus & SIIS_P_SS_ATTN) == 0) 725 return; 726 /* Read and clear interrupt statuses. */ 727 istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 728 (0xFFFF & ~SIIS_P_IX_COMMCOMP); 729 ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 730 /* Process PHY events */ 731 if (istatus & SIIS_P_IX_PHYRDYCHG) 732 siis_phy_check_events(dev); 733 /* Process NOTIFY events */ 734 if (istatus & SIIS_P_IX_SDBN) 735 siis_notify_events(dev); 736 /* Process command errors */ 737 if (istatus & SIIS_P_IX_COMMERR) { 738 estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 739 ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 740 ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 741 port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 742 err = ch->rslots & sstatus; 743//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 744// __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 745// ATA_INL(ch->r_mem, SIIS_P_SERR)); 746 747 if (!ch->readlog && !ch->recovery) { 748 xpt_freeze_simq(ch->sim, ch->numrslots); 749 ch->recovery = 1; 750 } 751 if (ch->frozen) { 752 union ccb *fccb = ch->frozen; 753 ch->frozen = NULL; 754 fccb->ccb_h.status &= ~CAM_STATUS_MASK; 755 fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 756 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 757 xpt_freeze_devq(fccb->ccb_h.path, 1); 758 fccb->ccb_h.status |= CAM_DEV_QFRZN; 759 } 760 xpt_done(fccb); 761 } 762 if (estatus == SIIS_P_CMDERR_DEV || 763 estatus == SIIS_P_CMDERR_SDB || 764 estatus == SIIS_P_CMDERR_DATAFIS) { 765 tslots = ch->numtslots[port]; 766 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 767 /* XXX: reqests in loading state. */ 768 if (((ch->rslots >> i) & 1) == 0) 769 continue; 770 if (ch->slot[i].ccb->ccb_h.target_id != port) 771 continue; 772 if (tslots == 0) { 773 /* Untagged operation. */ 774 if (i == ccs) 775 et = SIIS_ERR_TFE; 776 else 777 et = SIIS_ERR_INNOCENT; 778 } else { 779 /* Tagged operation. */ 780 et = SIIS_ERR_NCQ; 781 } 782 siis_end_transaction(&ch->slot[i], et); 783 } 784 /* 785 * We can't reinit port if there are some other 786 * commands active, use resume to complete them. 787 */ 788 if (ch->rslots != 0) 789 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 790 } else { 791 if (estatus == SIIS_P_CMDERR_SENDFIS || 792 estatus == SIIS_P_CMDERR_INCSTATE || 793 estatus == SIIS_P_CMDERR_PPE || 794 estatus == SIIS_P_CMDERR_SERVICE) { 795 et = SIIS_ERR_SATA; 796 } else 797 et = SIIS_ERR_INVALID; 798 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 799 /* XXX: reqests in loading state. */ 800 if (((ch->rslots >> i) & 1) == 0) 801 continue; 802 siis_end_transaction(&ch->slot[i], et); 803 } 804 } 805 } 806} 807 808/* Must be called with channel locked. */ 809static int 810siis_check_collision(device_t dev, union ccb *ccb) 811{ 812 struct siis_channel *ch = device_get_softc(dev); 813 814 mtx_assert(&ch->mtx, MA_OWNED); 815 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 816 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 817 /* Atomic command while anything active. */ 818 if (ch->numrslots != 0) 819 return (1); 820 } 821 /* We have some atomic command running. */ 822 if (ch->aslots != 0) 823 return (1); 824 return (0); 825} 826 827/* Must be called with channel locked. */ 828static void 829siis_begin_transaction(device_t dev, union ccb *ccb) 830{ 831 struct siis_channel *ch = device_get_softc(dev); 832 struct siis_slot *slot; 833 int tag; 834 835 mtx_assert(&ch->mtx, MA_OWNED); 836 /* Choose empty slot. */ 837 tag = ch->lastslot; 838 do { 839 tag++; 840 if (tag >= SIIS_MAX_SLOTS) 841 tag = 0; 842 if (ch->slot[tag].state == SIIS_SLOT_EMPTY) 843 break; 844 } while (tag != ch->lastslot); 845 if (ch->slot[tag].state != SIIS_SLOT_EMPTY) 846 device_printf(ch->dev, "ALL SLOTS BUSY!\n"); 847 ch->lastslot = tag; 848 /* Occupy chosen slot. */ 849 slot = &ch->slot[tag]; 850 slot->ccb = ccb; 851 /* Update channel stats. */ 852 ch->numrslots++; 853 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 854 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 855 ch->numtslots[ccb->ccb_h.target_id]++; 856 } 857 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 858 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 859 ch->aslots |= (1 << slot->slot); 860 slot->dma.nsegs = 0; 861 /* If request moves data, setup and load SG list */ 862 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 863 void *buf; 864 bus_size_t size; 865 866 slot->state = SIIS_SLOT_LOADING; 867 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 868 buf = ccb->ataio.data_ptr; 869 size = ccb->ataio.dxfer_len; 870 } else { 871 buf = ccb->csio.data_ptr; 872 size = ccb->csio.dxfer_len; 873 } 874 bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 875 buf, size, siis_dmasetprd, slot, 0); 876 } else 877 siis_execute_transaction(slot); 878} 879 880/* Locked by busdma engine. */ 881static void 882siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 883{ 884 struct siis_slot *slot = arg; 885 struct siis_channel *ch = device_get_softc(slot->dev); 886 struct siis_cmd *ctp; 887 struct siis_dma_prd *prd; 888 int i; 889 890 mtx_assert(&ch->mtx, MA_OWNED); 891 if (error) { 892 device_printf(slot->dev, "DMA load error\n"); 893 if (!ch->readlog) 894 xpt_freeze_simq(ch->sim, 1); 895 siis_end_transaction(slot, SIIS_ERR_INVALID); 896 return; 897 } 898 KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 899 /* Get a piece of the workspace for this request */ 900 ctp = (struct siis_cmd *) 901 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 902 /* Fill S/G table */ 903 if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 904 prd = &ctp->u.ata.prd[0]; 905 else 906 prd = &ctp->u.atapi.prd[0]; 907 for (i = 0; i < nsegs; i++) { 908 prd[i].dba = htole64(segs[i].ds_addr); 909 prd[i].dbc = htole32(segs[i].ds_len); 910 prd[i].control = 0; 911 } 912 prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 913 slot->dma.nsegs = nsegs; 914 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 915 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 916 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 917 siis_execute_transaction(slot); 918} 919 920/* Must be called with channel locked. */ 921static void 922siis_execute_transaction(struct siis_slot *slot) 923{ 924 device_t dev = slot->dev; 925 struct siis_channel *ch = device_get_softc(dev); 926 struct siis_cmd *ctp; 927 union ccb *ccb = slot->ccb; 928 u_int64_t prb_bus; 929 930 mtx_assert(&ch->mtx, MA_OWNED); 931 /* Get a piece of the workspace for this request */ 932 ctp = (struct siis_cmd *) 933 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 934 ctp->control = 0; 935 ctp->protocol_override = 0; 936 ctp->transfer_count = 0; 937 /* Special handling for Soft Reset command. */ 938 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 939 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) { 940 ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 941 } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 942 if (ccb->ccb_h.flags & CAM_DIR_IN) 943 ctp->control |= htole16(SIIS_PRB_PACKET_READ); 944 if (ccb->ccb_h.flags & CAM_DIR_OUT) 945 ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 946 } 947 /* Setup the FIS for this request */ 948 if (!siis_setup_fis(ctp, ccb, slot->slot)) { 949 device_printf(ch->dev, "Setting up SATA FIS failed\n"); 950 if (!ch->readlog) 951 xpt_freeze_simq(ch->sim, 1); 952 siis_end_transaction(slot, SIIS_ERR_INVALID); 953 return; 954 } 955 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 956 BUS_DMASYNC_PREWRITE); 957 /* Issue command to the controller. */ 958 slot->state = SIIS_SLOT_RUNNING; 959 ch->rslots |= (1 << slot->slot); 960 prb_bus = ch->dma.work_bus + 961 SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 962 ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 963 ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 964 /* Start command execution timeout */ 965 callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000, 966 (timeout_t*)siis_timeout, slot); 967 return; 968} 969 970/* Locked by callout mechanism. */ 971static void 972siis_timeout(struct siis_slot *slot) 973{ 974 device_t dev = slot->dev; 975 struct siis_channel *ch = device_get_softc(dev); 976 int i; 977 978 mtx_assert(&ch->mtx, MA_OWNED); 979 device_printf(dev, "Timeout on slot %d\n", slot->slot); 980device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 981 __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 982 ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 983 ATA_INL(ch->r_mem, SIIS_P_SERR)); 984 /* Kick controller into sane state. */ 985 siis_portinit(ch->dev); 986 987 if (!ch->readlog) 988 xpt_freeze_simq(ch->sim, ch->numrslots); 989 /* Handle frozen command. */ 990 if (ch->frozen) { 991 union ccb *fccb = ch->frozen; 992 ch->frozen = NULL; 993 fccb->ccb_h.status &= ~CAM_STATUS_MASK; 994 fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 995 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 996 xpt_freeze_devq(fccb->ccb_h.path, 1); 997 fccb->ccb_h.status |= CAM_DEV_QFRZN; 998 } 999 xpt_done(fccb); 1000 } 1001 /* Handle command with timeout. */ 1002 siis_end_transaction(&ch->slot[slot->slot], SIIS_ERR_TIMEOUT); 1003 /* Handle the rest of commands. */ 1004 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1005 /* Do we have a running request on slot? */ 1006 if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1007 continue; 1008 siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1009 } 1010} 1011 1012/* Must be called with channel locked. */ 1013static void 1014siis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 1015{ 1016 device_t dev = slot->dev; 1017 struct siis_channel *ch = device_get_softc(dev); 1018 union ccb *ccb = slot->ccb; 1019 1020 mtx_assert(&ch->mtx, MA_OWNED); 1021 /* Cancel command execution timeout */ 1022 callout_stop(&slot->timeout); 1023 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1024 BUS_DMASYNC_POSTWRITE); 1025 /* Read result registers to the result struct 1026 * May be incorrect if several commands finished same time, 1027 * so read only when sure or have to. 1028 */ 1029 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1030 struct ata_res *res = &ccb->ataio.res; 1031 if ((et == SIIS_ERR_TFE) || 1032 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1033 int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 1034 1035 res->status = ATA_INB(ch->r_mem, offs + 2); 1036 res->error = ATA_INB(ch->r_mem, offs + 3); 1037 res->lba_low = ATA_INB(ch->r_mem, offs + 4); 1038 res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 1039 res->lba_high = ATA_INB(ch->r_mem, offs + 6); 1040 res->device = ATA_INB(ch->r_mem, offs + 7); 1041 res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 1042 res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 1043 res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 1044 res->sector_count = ATA_INB(ch->r_mem, offs + 12); 1045 res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 1046 } else 1047 bzero(res, sizeof(*res)); 1048 } 1049 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1050 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1051 (ccb->ccb_h.flags & CAM_DIR_IN) ? 1052 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1053 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1054 } 1055 /* Set proper result status. */ 1056 if (et != SIIS_ERR_NONE || ch->recovery) { 1057 ch->eslots |= (1 << slot->slot); 1058 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1059 } 1060 /* In case of error, freeze device for proper recovery. */ 1061 if (et != SIIS_ERR_NONE && 1062 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1063 xpt_freeze_devq(ccb->ccb_h.path, 1); 1064 ccb->ccb_h.status |= CAM_DEV_QFRZN; 1065 } 1066 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1067 switch (et) { 1068 case SIIS_ERR_NONE: 1069 ccb->ccb_h.status |= CAM_REQ_CMP; 1070 if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1071 ccb->csio.scsi_status = SCSI_STATUS_OK; 1072 break; 1073 case SIIS_ERR_INVALID: 1074 ccb->ccb_h.status |= CAM_REQ_INVALID; 1075 break; 1076 case SIIS_ERR_INNOCENT: 1077 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1078 break; 1079 case SIIS_ERR_TFE: 1080 case SIIS_ERR_NCQ: 1081 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1082 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1083 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1084 } else { 1085 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1086 } 1087 break; 1088 case SIIS_ERR_SATA: 1089 ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1090 break; 1091 case SIIS_ERR_TIMEOUT: 1092 ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1093 break; 1094 default: 1095 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1096 } 1097 /* Free slot. */ 1098 ch->rslots &= ~(1 << slot->slot); 1099 ch->aslots &= ~(1 << slot->slot); 1100 slot->state = SIIS_SLOT_EMPTY; 1101 slot->ccb = NULL; 1102 /* Update channel stats. */ 1103 ch->numrslots--; 1104 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1105 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1106 ch->numtslots[ccb->ccb_h.target_id]--; 1107 } 1108 /* If it was NCQ command error, put result on hold. */ 1109 if (et == SIIS_ERR_NCQ) { 1110 ch->hold[slot->slot] = ccb; 1111 ch->numhslots++; 1112 } else if (ch->readlog) /* If it was our READ LOG command - process it. */ 1113 siis_process_read_log(dev, ccb); 1114 else 1115 xpt_done(ccb); 1116 /* Unfreeze frozen command. */ 1117 if (ch->frozen && ch->numrslots == 0) { 1118 union ccb *fccb = ch->frozen; 1119 ch->frozen = NULL; 1120 siis_begin_transaction(dev, fccb); 1121 xpt_release_simq(ch->sim, TRUE); 1122 } 1123 /* If we have no other active commands, ... */ 1124 if (ch->rslots == 0) { 1125 /* if we have slots in error, we can reinit port. */ 1126 if (ch->eslots != 0) 1127 siis_portinit(dev); 1128 /* if there commands on hold, we can do READ LOG. */ 1129 if (!ch->readlog && ch->numhslots) 1130 siis_issue_read_log(dev); 1131 } 1132} 1133 1134static void 1135siis_issue_read_log(device_t dev) 1136{ 1137 struct siis_channel *ch = device_get_softc(dev); 1138 union ccb *ccb; 1139 struct ccb_ataio *ataio; 1140 int i; 1141 1142 /* Find some holden command. */ 1143 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1144 if (ch->hold[i]) 1145 break; 1146 } 1147 if (i == SIIS_MAX_SLOTS) 1148 return; 1149 ch->readlog = 1; 1150 ccb = xpt_alloc_ccb_nowait(); 1151 if (ccb == NULL) { 1152 device_printf(dev, "Unable allocate READ LOG command"); 1153 return; /* XXX */ 1154 } 1155 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1156 ccb->ccb_h.func_code = XPT_ATA_IO; 1157 ccb->ccb_h.flags = CAM_DIR_IN; 1158 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1159 ataio = &ccb->ataio; 1160 ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 1161 if (ataio->data_ptr == NULL) { 1162 device_printf(dev, "Unable allocate memory for READ LOG command"); 1163 return; /* XXX */ 1164 } 1165 ataio->dxfer_len = 512; 1166 bzero(&ataio->cmd, sizeof(ataio->cmd)); 1167 ataio->cmd.flags = CAM_ATAIO_48BIT; 1168 ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1169 ataio->cmd.sector_count = 1; 1170 ataio->cmd.sector_count_exp = 0; 1171 ataio->cmd.lba_low = 0x10; 1172 ataio->cmd.lba_mid = 0; 1173 ataio->cmd.lba_mid_exp = 0; 1174 siis_begin_transaction(dev, ccb); 1175} 1176 1177static void 1178siis_process_read_log(device_t dev, union ccb *ccb) 1179{ 1180 struct siis_channel *ch = device_get_softc(dev); 1181 uint8_t *data; 1182 struct ata_res *res; 1183 int i; 1184 1185 ch->readlog = 0; 1186 data = ccb->ataio.data_ptr; 1187 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1188 (data[0] & 0x80) == 0) { 1189 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1190 if (!ch->hold[i]) 1191 continue; 1192 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1193 continue; 1194 if ((data[0] & 0x1F) == i) { 1195 res = &ch->hold[i]->ataio.res; 1196 res->status = data[2]; 1197 res->error = data[3]; 1198 res->lba_low = data[4]; 1199 res->lba_mid = data[5]; 1200 res->lba_high = data[6]; 1201 res->device = data[7]; 1202 res->lba_low_exp = data[8]; 1203 res->lba_mid_exp = data[9]; 1204 res->lba_high_exp = data[10]; 1205 res->sector_count = data[12]; 1206 res->sector_count_exp = data[13]; 1207 } else { 1208 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1209 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1210 } 1211 xpt_done(ch->hold[i]); 1212 ch->hold[i] = NULL; 1213 ch->numhslots--; 1214 } 1215 } else { 1216 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1217 device_printf(dev, "Error while READ LOG EXT\n"); 1218 else if ((data[0] & 0x80) == 0) { 1219 device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1220 } 1221 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1222 if (!ch->hold[i]) 1223 continue; 1224 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1225 continue; 1226 xpt_done(ch->hold[i]); 1227 ch->hold[i] = NULL; 1228 ch->numhslots--; 1229 } 1230 } 1231 free(ccb->ataio.data_ptr, M_SIIS); 1232 xpt_free_ccb(ccb); 1233} 1234 1235static void 1236siis_portinit(device_t dev) 1237{ 1238 struct siis_channel *ch = device_get_softc(dev); 1239 int i; 1240 1241 ch->eslots = 0; 1242 ch->recovery = 0; 1243 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 1244 for (i = 0; i < 16; i++) { 1245 ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 1246 ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 1247 } 1248 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 1249 siis_wait_ready(dev, 1000); 1250} 1251 1252static int 1253siis_devreset(device_t dev) 1254{ 1255 struct siis_channel *ch = device_get_softc(dev); 1256 int timeout = 0; 1257 uint32_t val; 1258 1259 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 1260 while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1261 SIIS_P_CTL_DEV_RESET) != 0) { 1262 DELAY(1000); 1263 if (timeout++ > 100) { 1264 device_printf(dev, "device reset stuck (timeout %dms) " 1265 "status = %08x\n", timeout, val); 1266 return (EBUSY); 1267 } 1268 } 1269 if (bootverbose) 1270 device_printf(dev, "device reset time=%dms\n", timeout); 1271 return (0); 1272} 1273 1274static int 1275siis_wait_ready(device_t dev, int t) 1276{ 1277 struct siis_channel *ch = device_get_softc(dev); 1278 int timeout = 0; 1279 uint32_t val; 1280 1281 while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1282 SIIS_P_CTL_READY) == 0) { 1283 DELAY(1000); 1284 if (timeout++ > t) { 1285 device_printf(dev, "port is not ready (timeout %dms) " 1286 "status = %08x\n", t, val); 1287 return (EBUSY); 1288 } 1289 } 1290 if (bootverbose) 1291 device_printf(dev, "ready wait time=%dms\n", timeout); 1292 return (0); 1293} 1294 1295static void 1296siis_reset(device_t dev) 1297{ 1298 struct siis_channel *ch = device_get_softc(dev); 1299 int i; 1300 uint32_t val; 1301 1302 if (bootverbose) 1303 device_printf(dev, "SIIS reset...\n"); 1304 xpt_freeze_simq(ch->sim, ch->numrslots); 1305 /* Requeue freezed command. */ 1306 if (ch->frozen) { 1307 union ccb *fccb = ch->frozen; 1308 ch->frozen = NULL; 1309 fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1310 fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1311 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1312 xpt_freeze_devq(fccb->ccb_h.path, 1); 1313 fccb->ccb_h.status |= CAM_DEV_QFRZN; 1314 } 1315 xpt_done(fccb); 1316 } 1317 /* Requeue all running commands. */ 1318 for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1319 /* Do we have a running request on slot? */ 1320 if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1321 continue; 1322 /* XXX; Commands in loading state. */ 1323 siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1324 } 1325 /* Disable port interrupts */ 1326 ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 1327 /* Set speed limit. */ 1328 if (ch->sata_rev == 1) 1329 val = ATA_SC_SPD_SPEED_GEN1; 1330 else if (ch->sata_rev == 2) 1331 val = ATA_SC_SPD_SPEED_GEN2; 1332 else if (ch->sata_rev == 3) 1333 val = ATA_SC_SPD_SPEED_GEN3; 1334 else 1335 val = 0; 1336 ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 1337 ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 1338 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 1339 siis_devreset(dev); 1340 /* Reset and reconnect PHY, */ 1341 if (!siis_sata_connect(ch)) { 1342 ch->devices = 0; 1343 /* Enable port interrupts */ 1344 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1345 if (bootverbose) 1346 device_printf(dev, 1347 "SIIS reset done: phy reset found no device\n"); 1348 /* Tell the XPT about the event */ 1349 xpt_async(AC_BUS_RESET, ch->path, NULL); 1350 return; 1351 } 1352 /* Wait for clearing busy status. */ 1353 if (siis_wait_ready(dev, 10000)) 1354 device_printf(dev, "device ready timeout\n"); 1355 ch->devices = 1; 1356 /* Enable port interrupts */ 1357 ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 1358 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1359 if (bootverbose) 1360 device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 1361 /* Tell the XPT about the event */ 1362 xpt_async(AC_BUS_RESET, ch->path, NULL); 1363} 1364 1365static int 1366siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag) 1367{ 1368 u_int8_t *fis = &ctp->fis[0]; 1369 1370 bzero(fis, 24); 1371 fis[0] = 0x27; /* host to device */ 1372 fis[1] = (ccb->ccb_h.target_id & 0x0f); 1373 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1374 fis[1] |= 0x80; 1375 fis[2] = ATA_PACKET_CMD; 1376 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 1377 fis[3] = ATA_F_DMA; 1378 else { 1379 fis[5] = ccb->csio.dxfer_len; 1380 fis[6] = ccb->csio.dxfer_len >> 8; 1381 } 1382 fis[7] = ATA_D_LBA; 1383 fis[15] = ATA_A_4BIT; 1384 bzero(ctp->u.atapi.ccb, 16); 1385 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1386 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1387 ctp->u.atapi.ccb, ccb->csio.cdb_len); 1388 } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1389 fis[1] |= 0x80; 1390 fis[2] = ccb->ataio.cmd.command; 1391 fis[3] = ccb->ataio.cmd.features; 1392 fis[4] = ccb->ataio.cmd.lba_low; 1393 fis[5] = ccb->ataio.cmd.lba_mid; 1394 fis[6] = ccb->ataio.cmd.lba_high; 1395 fis[7] = ccb->ataio.cmd.device; 1396 fis[8] = ccb->ataio.cmd.lba_low_exp; 1397 fis[9] = ccb->ataio.cmd.lba_mid_exp; 1398 fis[10] = ccb->ataio.cmd.lba_high_exp; 1399 fis[11] = ccb->ataio.cmd.features_exp; 1400 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1401 fis[12] = tag << 3; 1402 fis[13] = 0; 1403 } else { 1404 fis[12] = ccb->ataio.cmd.sector_count; 1405 fis[13] = ccb->ataio.cmd.sector_count_exp; 1406 } 1407 fis[15] = ATA_A_4BIT; 1408 } else { 1409 /* Soft reset. */ 1410 } 1411 return (20); 1412} 1413 1414static int 1415siis_sata_connect(struct siis_channel *ch) 1416{ 1417 u_int32_t status; 1418 int timeout; 1419 1420 /* Wait up to 100ms for "connect well" */ 1421 for (timeout = 0; timeout < 100 ; timeout++) { 1422 status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 1423 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1424 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1425 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 1426 break; 1427 DELAY(1000); 1428 } 1429 if (timeout >= 100) { 1430 if (bootverbose) { 1431 device_printf(ch->dev, "SATA connect timeout status=%08x\n", 1432 status); 1433 } 1434 return (0); 1435 } 1436 if (bootverbose) { 1437 device_printf(ch->dev, "SATA connect time=%dms status=%08x\n", 1438 timeout, status); 1439 } 1440 /* Clear SATA error register */ 1441 ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 1442 return (1); 1443} 1444 1445static void 1446siisaction(struct cam_sim *sim, union ccb *ccb) 1447{ 1448 device_t dev; 1449 struct siis_channel *ch; 1450 1451 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 1452 ccb->ccb_h.func_code)); 1453 1454 ch = (struct siis_channel *)cam_sim_softc(sim); 1455 dev = ch->dev; 1456 mtx_assert(&ch->mtx, MA_OWNED); 1457 switch (ccb->ccb_h.func_code) { 1458 /* Common cases first */ 1459 case XPT_ATA_IO: /* Execute the requested I/O operation */ 1460 case XPT_SCSI_IO: 1461 if (ch->devices == 0) { 1462 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1463 xpt_done(ccb); 1464 break; 1465 } 1466 /* Check for command collision. */ 1467 if (siis_check_collision(dev, ccb)) { 1468 /* Freeze command. */ 1469 ch->frozen = ccb; 1470 /* We have only one frozen slot, so freeze simq also. */ 1471 xpt_freeze_simq(ch->sim, 1); 1472 return; 1473 } 1474 siis_begin_transaction(dev, ccb); 1475 break; 1476 case XPT_EN_LUN: /* Enable LUN as a target */ 1477 case XPT_TARGET_IO: /* Execute target I/O request */ 1478 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1479 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1480 case XPT_ABORT: /* Abort the specified CCB */ 1481 /* XXX Implement */ 1482 ccb->ccb_h.status = CAM_REQ_INVALID; 1483 xpt_done(ccb); 1484 break; 1485 case XPT_SET_TRAN_SETTINGS: 1486 { 1487 struct ccb_trans_settings *cts = &ccb->cts; 1488 1489 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 1490 if (cts->xport_specific.sata.pm_present) 1491 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1492 else 1493 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1494 } 1495 ccb->ccb_h.status = CAM_REQ_CMP; 1496 xpt_done(ccb); 1497 break; 1498 } 1499 case XPT_GET_TRAN_SETTINGS: 1500 /* Get default/user set transfer settings for the target */ 1501 { 1502 struct ccb_trans_settings *cts = &ccb->cts; 1503 uint32_t status; 1504 1505 cts->protocol = PROTO_ATA; 1506 cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1507 cts->transport = XPORT_SATA; 1508 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1509 cts->proto_specific.valid = 0; 1510 cts->xport_specific.sata.valid = 0; 1511 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1512 status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1513 else 1514 status = ATA_INL(ch->r_mem, SIIS_P_SCTL) & ATA_SC_SPD_MASK; 1515 if (status & ATA_SS_SPD_GEN3) { 1516 cts->xport_specific.sata.bitrate = 600000; 1517 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1518 } else if (status & ATA_SS_SPD_GEN2) { 1519 cts->xport_specific.sata.bitrate = 300000; 1520 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1521 } else if (status & ATA_SS_SPD_GEN1) { 1522 cts->xport_specific.sata.bitrate = 150000; 1523 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1524 } 1525 cts->xport_specific.sata.pm_present = 1526 (ATA_INL(ch->r_mem, SIIS_P_STS) & SIIS_P_CTL_PME) ? 1527 1 : 0; 1528 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1529 ccb->ccb_h.status = CAM_REQ_CMP; 1530 xpt_done(ccb); 1531 break; 1532 } 1533#if 0 1534 case XPT_CALC_GEOMETRY: 1535 { 1536 struct ccb_calc_geometry *ccg; 1537 uint32_t size_mb; 1538 uint32_t secs_per_cylinder; 1539 1540 ccg = &ccb->ccg; 1541 size_mb = ccg->volume_size 1542 / ((1024L * 1024L) / ccg->block_size); 1543 if (size_mb >= 1024 && (aha->extended_trans != 0)) { 1544 if (size_mb >= 2048) { 1545 ccg->heads = 255; 1546 ccg->secs_per_track = 63; 1547 } else { 1548 ccg->heads = 128; 1549 ccg->secs_per_track = 32; 1550 } 1551 } else { 1552 ccg->heads = 64; 1553 ccg->secs_per_track = 32; 1554 } 1555 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 1556 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 1557 ccb->ccb_h.status = CAM_REQ_CMP; 1558 xpt_done(ccb); 1559 break; 1560 } 1561#endif 1562 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1563 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1564 siis_reset(dev); 1565 ccb->ccb_h.status = CAM_REQ_CMP; 1566 xpt_done(ccb); 1567 break; 1568 case XPT_TERM_IO: /* Terminate the I/O process */ 1569 /* XXX Implement */ 1570 ccb->ccb_h.status = CAM_REQ_INVALID; 1571 xpt_done(ccb); 1572 break; 1573 case XPT_PATH_INQ: /* Path routing inquiry */ 1574 { 1575 struct ccb_pathinq *cpi = &ccb->cpi; 1576 1577 cpi->version_num = 1; /* XXX??? */ 1578 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 1579 cpi->hba_inquiry |= PI_SATAPM; 1580 cpi->target_sprt = 0; 1581 cpi->hba_misc = PIM_SEQSCAN; 1582 cpi->hba_eng_cnt = 0; 1583 cpi->max_target = 15; 1584 cpi->max_lun = 0; 1585 cpi->initiator_id = 0; 1586 cpi->bus_id = cam_sim_bus(sim); 1587 cpi->base_transfer_speed = 150000; 1588 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1589 strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 1590 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1591 cpi->unit_number = cam_sim_unit(sim); 1592 cpi->transport = XPORT_SATA; 1593 cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1594 cpi->protocol = PROTO_ATA; 1595 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1596 cpi->ccb_h.status = CAM_REQ_CMP; 1597 cpi->maxio = MAXPHYS; 1598 xpt_done(ccb); 1599 break; 1600 } 1601 default: 1602 ccb->ccb_h.status = CAM_REQ_INVALID; 1603 xpt_done(ccb); 1604 break; 1605 } 1606} 1607 1608static void 1609siispoll(struct cam_sim *sim) 1610{ 1611 struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 1612 1613 siis_ch_intr(ch->dev); 1614} 1615