2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/taskqueue.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.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>
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_suspend(device_t dev);
64 static int siis_ch_resume(device_t dev);
65 static void siis_ch_intr_locked(void *data);
66 static void siis_ch_intr(void *data);
67 static void siis_begin_transaction(device_t dev, union ccb *ccb);
68 static void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
69 static void siis_execute_transaction(struct siis_slot *slot);
70 static void siis_timeout(struct siis_slot *slot);
71 static void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
72 static int siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag);
73 static void siis_dmainit(device_t dev);
74 static void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
75 static void siis_dmafini(device_t dev);
76 static void siis_slotsalloc(device_t dev);
77 static void siis_slotsfree(device_t dev);
78 static void siis_reset(device_t dev);
79 static void siis_portinit(device_t dev);
80 static int siis_wait_ready(device_t dev, int t);
82 static int siis_sata_connect(struct siis_channel *ch);
83 static int siis_sata_phy_reset(device_t dev);
85 static void siis_issue_read_log(device_t dev);
86 static void siis_process_read_log(device_t dev, union ccb *ccb);
88 static void siisaction(struct cam_sim *sim, union ccb *ccb);
89 static void siispoll(struct cam_sim *sim);
91 MALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
94 siis_probe(device_t dev)
96 uint32_t devid = pci_get_devid(dev);
98 if (devid == SIIS_SII3124) {
99 device_set_desc_copy(dev, "SiI3124 SATA2 controller");
100 } else if (devid == SIIS_SII3132 ||
101 devid == SIIS_SII3132_1 ||
102 devid == SIIS_SII3132_2) {
103 device_set_desc_copy(dev, "SiI3132 SATA2 controller");
104 } else if (devid == SIIS_SII3531) {
105 device_set_desc_copy(dev, "SiI3531 SATA2 controller");
110 return (BUS_PROBE_VENDOR);
114 siis_attach(device_t dev)
116 struct siis_controller *ctlr = device_get_softc(dev);
117 uint32_t devid = pci_get_devid(dev);
123 ctlr->r_grid = PCIR_BAR(0);
124 if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
125 &ctlr->r_grid, RF_ACTIVE)))
127 /* Channels memory */
128 ctlr->r_rid = PCIR_BAR(2);
129 if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
130 &ctlr->r_rid, RF_ACTIVE)))
132 /* Setup our own memory management for channels. */
133 ctlr->sc_iomem.rm_type = RMAN_ARRAY;
134 ctlr->sc_iomem.rm_descr = "I/O memory addresses";
135 if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
136 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
137 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
140 if ((error = rman_manage_region(&ctlr->sc_iomem,
141 rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
142 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
143 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
144 rman_fini(&ctlr->sc_iomem);
147 /* Reset controller */
149 /* Number of HW channels */
150 ctlr->channels = (devid == SIIS_SII3124) ? 4 :
151 (devid == SIIS_SII3531 ? 1 : 2);
152 /* Setup interrupts. */
153 if (siis_setup_interrupt(dev)) {
154 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
155 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
156 rman_fini(&ctlr->sc_iomem);
159 /* Attach all channels on this controller */
160 for (unit = 0; unit < ctlr->channels; unit++) {
161 child = device_add_child(dev, "siisch", -1);
163 device_printf(dev, "failed to add channel device\n");
165 device_set_ivars(child, (void *)(intptr_t)unit);
167 bus_generic_attach(dev);
172 siis_detach(device_t dev)
174 struct siis_controller *ctlr = device_get_softc(dev);
178 /* Detach & delete all children */
179 if (!device_get_children(dev, &children, &nchildren)) {
180 for (i = 0; i < nchildren; i++)
181 device_delete_child(dev, children[i]);
182 free(children, M_TEMP);
184 /* Free interrupts. */
185 if (ctlr->irq.r_irq) {
186 bus_teardown_intr(dev, ctlr->irq.r_irq,
188 bus_release_resource(dev, SYS_RES_IRQ,
189 ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
191 pci_release_msi(dev);
193 rman_fini(&ctlr->sc_iomem);
194 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
195 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
200 siis_suspend(device_t dev)
202 struct siis_controller *ctlr = device_get_softc(dev);
204 bus_generic_suspend(dev);
205 /* Put controller into reset state. */
206 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
211 siis_resume(device_t dev)
213 struct siis_controller *ctlr = device_get_softc(dev);
215 /* Put controller into reset state. */
216 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
218 /* Get controller out of reset state and enable port interrupts. */
219 ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 0x0000000f);
220 return (bus_generic_resume(dev));
224 siis_setup_interrupt(device_t dev)
226 struct siis_controller *ctlr = device_get_softc(dev);
230 resource_int_value(device_get_name(dev),
231 device_get_unit(dev), "msi", &msi);
235 msi = min(1, pci_msi_count(dev));
236 /* Allocate MSI if needed/present. */
237 if (msi && pci_alloc_msi(dev, &msi) != 0)
239 /* Allocate all IRQs. */
240 ctlr->irq.r_irq_rid = msi ? 1 : 0;
241 if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
242 &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
243 device_printf(dev, "unable to map interrupt\n");
246 if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
247 siis_intr, ctlr, &ctlr->irq.handle))) {
248 /* SOS XXX release r_irq */
249 device_printf(dev, "unable to setup interrupt\n");
256 * Common case interrupt handler.
259 siis_intr(void *data)
261 struct siis_controller *ctlr = (struct siis_controller *)data;
266 is = ATA_INL(ctlr->r_gmem, SIIS_IS);
267 for (unit = 0; unit < ctlr->channels; unit++) {
268 if ((is & SIIS_IS_PORT(unit)) != 0 &&
269 (arg = ctlr->interrupt[unit].argument)) {
270 ctlr->interrupt[unit].function(arg);
275 static struct resource *
276 siis_alloc_resource(device_t dev, device_t child, int type, int *rid,
277 u_long start, u_long end, u_long count, u_int flags)
279 struct siis_controller *ctlr = device_get_softc(dev);
280 int unit = ((struct siis_channel *)device_get_softc(child))->unit;
281 struct resource *res = NULL;
282 int offset = unit << 13;
287 st = rman_get_start(ctlr->r_mem);
288 res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
289 st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
291 bus_space_handle_t bsh;
293 bsh = rman_get_bushandle(ctlr->r_mem);
294 bst = rman_get_bustag(ctlr->r_mem);
295 bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
296 rman_set_bushandle(res, bsh);
297 rman_set_bustag(res, bst);
301 if (*rid == ATA_IRQ_RID)
302 res = ctlr->irq.r_irq;
309 siis_release_resource(device_t dev, device_t child, int type, int rid,
315 rman_release_resource(r);
318 if (rid != ATA_IRQ_RID)
326 siis_setup_intr(device_t dev, device_t child, struct resource *irq,
327 int flags, driver_filter_t *filter, driver_intr_t *function,
328 void *argument, void **cookiep)
330 struct siis_controller *ctlr = device_get_softc(dev);
331 int unit = (intptr_t)device_get_ivars(child);
333 if (filter != NULL) {
334 printf("siis.c: we cannot use a filter here\n");
337 ctlr->interrupt[unit].function = function;
338 ctlr->interrupt[unit].argument = argument;
343 siis_teardown_intr(device_t dev, device_t child, struct resource *irq,
346 struct siis_controller *ctlr = device_get_softc(dev);
347 int unit = (intptr_t)device_get_ivars(child);
349 ctlr->interrupt[unit].function = NULL;
350 ctlr->interrupt[unit].argument = NULL;
355 siis_print_child(device_t dev, device_t child)
359 retval = bus_print_child_header(dev, child);
360 retval += printf(" at channel %d",
361 (int)(intptr_t)device_get_ivars(child));
362 retval += bus_print_child_footer(dev, child);
367 devclass_t siis_devclass;
368 static device_method_t siis_methods[] = {
369 DEVMETHOD(device_probe, siis_probe),
370 DEVMETHOD(device_attach, siis_attach),
371 DEVMETHOD(device_detach, siis_detach),
372 DEVMETHOD(device_suspend, siis_suspend),
373 DEVMETHOD(device_resume, siis_resume),
374 DEVMETHOD(bus_print_child, siis_print_child),
375 DEVMETHOD(bus_alloc_resource, siis_alloc_resource),
376 DEVMETHOD(bus_release_resource, siis_release_resource),
377 DEVMETHOD(bus_setup_intr, siis_setup_intr),
378 DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
381 static driver_t siis_driver = {
384 sizeof(struct siis_controller)
386 DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
387 MODULE_VERSION(siis, 1);
388 MODULE_DEPEND(siis, cam, 1, 1, 1);
391 siis_ch_probe(device_t dev)
394 device_set_desc_copy(dev, "SIIS channel");
399 siis_ch_attach(device_t dev)
401 struct siis_channel *ch = device_get_softc(dev);
402 struct cam_devq *devq;
406 ch->unit = (intptr_t)device_get_ivars(dev);
407 resource_int_value(device_get_name(dev),
408 device_get_unit(dev), "pm_level", &ch->pm_level);
409 resource_int_value(device_get_name(dev),
410 device_get_unit(dev), "sata_rev", &ch->sata_rev);
411 mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
413 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
417 siis_slotsalloc(dev);
421 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
422 &rid, RF_SHAREABLE | RF_ACTIVE))) {
423 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
424 device_printf(dev, "Unable to map interrupt\n");
427 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
428 siis_ch_intr_locked, dev, &ch->ih))) {
429 device_printf(dev, "Unable to setup interrupt\n");
433 /* Create the device queue for our SIM. */
434 devq = cam_simq_alloc(SIIS_MAX_SLOTS);
436 device_printf(dev, "Unable to allocate simq\n");
440 /* Construct SIM entry */
441 ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
442 device_get_unit(dev), &ch->mtx, SIIS_MAX_SLOTS, 0, devq);
443 if (ch->sim == NULL) {
444 device_printf(dev, "unable to allocate sim\n");
448 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
449 device_printf(dev, "unable to register xpt bus\n");
453 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
454 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
455 device_printf(dev, "unable to create path\n");
459 mtx_unlock(&ch->mtx);
463 xpt_bus_deregister(cam_sim_path(ch->sim));
465 cam_sim_free(ch->sim, /*free_devq*/TRUE);
467 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
468 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
469 mtx_unlock(&ch->mtx);
474 siis_ch_detach(device_t dev)
476 struct siis_channel *ch = device_get_softc(dev);
479 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
480 xpt_free_path(ch->path);
481 xpt_bus_deregister(cam_sim_path(ch->sim));
482 cam_sim_free(ch->sim, /*free_devq*/TRUE);
483 mtx_unlock(&ch->mtx);
485 bus_teardown_intr(dev, ch->r_irq, ch->ih);
486 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
488 siis_ch_suspend(dev);
492 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
493 mtx_destroy(&ch->mtx);
498 siis_ch_suspend(device_t dev)
500 struct siis_channel *ch = device_get_softc(dev);
502 /* Put port into reset state. */
503 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
508 siis_ch_resume(device_t dev)
510 struct siis_channel *ch = device_get_softc(dev);
512 /* Get port out of reset state. */
513 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
514 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
515 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
516 /* Enable port interrupts */
517 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
521 devclass_t siisch_devclass;
522 static device_method_t siisch_methods[] = {
523 DEVMETHOD(device_probe, siis_ch_probe),
524 DEVMETHOD(device_attach, siis_ch_attach),
525 DEVMETHOD(device_detach, siis_ch_detach),
526 DEVMETHOD(device_suspend, siis_ch_suspend),
527 DEVMETHOD(device_resume, siis_ch_resume),
530 static driver_t siisch_driver = {
533 sizeof(struct siis_channel)
535 DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
537 struct siis_dc_cb_args {
543 siis_dmainit(device_t dev)
545 struct siis_channel *ch = device_get_softc(dev);
546 struct siis_dc_cb_args dcba;
549 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
550 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
551 NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
552 0, NULL, NULL, &ch->dma.work_tag))
554 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
557 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
558 SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
559 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
562 ch->dma.work_bus = dcba.maddr;
564 if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
565 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
567 SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
568 SIIS_SG_ENTRIES, 0xFFFFFFFF,
569 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
575 device_printf(dev, "WARNING - DMA initialization failed\n");
580 siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
582 struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
584 if (!(dcba->error = error))
585 dcba->maddr = segs[0].ds_addr;
589 siis_dmafini(device_t dev)
591 struct siis_channel *ch = device_get_softc(dev);
593 if (ch->dma.data_tag) {
594 bus_dma_tag_destroy(ch->dma.data_tag);
595 ch->dma.data_tag = NULL;
597 if (ch->dma.work_bus) {
598 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
599 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
600 ch->dma.work_bus = 0;
601 ch->dma.work_map = NULL;
604 if (ch->dma.work_tag) {
605 bus_dma_tag_destroy(ch->dma.work_tag);
606 ch->dma.work_tag = NULL;
611 siis_slotsalloc(device_t dev)
613 struct siis_channel *ch = device_get_softc(dev);
616 /* Alloc and setup command/dma slots */
617 bzero(ch->slot, sizeof(ch->slot));
618 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
619 struct siis_slot *slot = &ch->slot[i];
623 slot->state = SIIS_SLOT_EMPTY;
625 callout_init_mtx(&slot->timeout, &ch->mtx, 0);
627 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
628 device_printf(ch->dev, "FAILURE - create data_map\n");
633 siis_slotsfree(device_t dev)
635 struct siis_channel *ch = device_get_softc(dev);
638 /* Free all dma slots */
639 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
640 struct siis_slot *slot = &ch->slot[i];
642 if (slot->dma.data_map) {
643 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
644 slot->dma.data_map = NULL;
650 siis_phy_check_events(device_t dev)
652 struct siis_channel *ch = device_get_softc(dev);
654 /* If we have a connection event, deal with it */
655 if (ch->pm_level == 0) {
656 u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
657 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
658 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
659 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
661 device_printf(dev, "CONNECT requested\n");
665 device_printf(dev, "DISCONNECT requested\n");
672 siis_ch_intr_locked(void *data)
674 device_t dev = (device_t)data;
675 struct siis_channel *ch = device_get_softc(dev);
679 mtx_unlock(&ch->mtx);
683 siis_ch_intr(void *data)
685 device_t dev = (device_t)data;
686 struct siis_channel *ch = device_get_softc(dev);
687 uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
688 enum siis_err_type et;
689 int i, ccs, port, tslots;
691 mtx_assert(&ch->mtx, MA_OWNED);
692 /* Read command statuses. */
693 sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
694 ok = ch->rslots & ~sstatus;
695 /* Complete all successfull commands. */
696 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
698 siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
700 /* Do we have any other events? */
701 if ((sstatus & SIIS_P_SS_ATTN) == 0)
703 /* Read and clear interrupt statuses. */
704 istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
705 (0xFFFF & ~SIIS_P_IX_COMMCOMP);
706 ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
707 /* Process PHY events */
708 if (istatus & SIIS_P_IX_PHYRDYCHG)
709 siis_phy_check_events(dev);
710 /* Process command errors */
711 if (istatus & SIIS_P_IX_COMMERR) {
712 estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
713 ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
714 ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
715 port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
716 err = ch->rslots & sstatus;
717 //device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
718 // __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
719 // ATA_INL(ch->r_mem, SIIS_P_SERR));
721 if (!ch->readlog && !ch->recovery) {
722 xpt_freeze_simq(ch->sim, ch->numrslots);
726 union ccb *fccb = ch->frozen;
728 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
731 if (estatus == SIIS_P_CMDERR_DEV ||
732 estatus == SIIS_P_CMDERR_SDB ||
733 estatus == SIIS_P_CMDERR_DATAFIS) {
734 tslots = ch->numtslots[port];
735 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
736 /* XXX: reqests in loading state. */
737 if (((ch->rslots >> i) & 1) == 0)
739 if (ch->slot[i].ccb->ccb_h.target_id != port)
742 /* Untagged operation. */
746 et = SIIS_ERR_INNOCENT;
748 /* Tagged operation. */
751 siis_end_transaction(&ch->slot[i], et);
754 * We can't reinit port if there are some other
755 * commands active, use resume to complete them.
758 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
760 if (estatus == SIIS_P_CMDERR_SENDFIS ||
761 estatus == SIIS_P_CMDERR_INCSTATE ||
762 estatus == SIIS_P_CMDERR_PPE ||
763 estatus == SIIS_P_CMDERR_SERVICE) {
766 et = SIIS_ERR_INVALID;
767 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
768 /* XXX: reqests in loading state. */
769 if (((ch->rslots >> i) & 1) == 0)
771 siis_end_transaction(&ch->slot[i], et);
777 /* Must be called with channel locked. */
779 siis_check_collision(device_t dev, union ccb *ccb)
781 struct siis_channel *ch = device_get_softc(dev);
783 mtx_assert(&ch->mtx, MA_OWNED);
784 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
785 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
786 /* Atomic command while anything active. */
787 if (ch->numrslots != 0)
790 /* We have some atomic command running. */
796 /* Must be called with channel locked. */
798 siis_begin_transaction(device_t dev, union ccb *ccb)
800 struct siis_channel *ch = device_get_softc(dev);
801 struct siis_slot *slot;
804 mtx_assert(&ch->mtx, MA_OWNED);
805 /* Choose empty slot. */
809 if (tag >= SIIS_MAX_SLOTS)
811 if (ch->slot[tag].state == SIIS_SLOT_EMPTY)
813 } while (tag != ch->lastslot);
814 if (ch->slot[tag].state != SIIS_SLOT_EMPTY)
815 device_printf(ch->dev, "ALL SLOTS BUSY!\n");
817 /* Occupy chosen slot. */
818 slot = &ch->slot[tag];
820 /* Update channel stats. */
822 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
823 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
824 ch->numtslots[ccb->ccb_h.target_id]++;
826 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
827 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
828 ch->aslots |= (1 << slot->slot);
830 /* If request moves data, setup and load SG list */
831 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
835 slot->state = SIIS_SLOT_LOADING;
836 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
837 buf = ccb->ataio.data_ptr;
838 size = ccb->ataio.dxfer_len;
840 buf = ccb->csio.data_ptr;
841 size = ccb->csio.dxfer_len;
843 bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
844 buf, size, siis_dmasetprd, slot, 0);
846 siis_execute_transaction(slot);
849 /* Locked by busdma engine. */
851 siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
853 struct siis_slot *slot = arg;
854 struct siis_channel *ch = device_get_softc(slot->dev);
855 struct siis_cmd *ctp;
856 struct siis_dma_prd *prd;
859 mtx_assert(&ch->mtx, MA_OWNED);
861 device_printf(slot->dev, "DMA load error\n");
863 xpt_freeze_simq(ch->sim, 1);
864 siis_end_transaction(slot, SIIS_ERR_INVALID);
867 KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
868 /* Get a piece of the workspace for this request */
869 ctp = (struct siis_cmd *)
870 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
872 if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
873 prd = &ctp->u.ata.prd[0];
875 prd = &ctp->u.atapi.prd[0];
876 for (i = 0; i < nsegs; i++) {
877 prd[i].dba = htole64(segs[i].ds_addr);
878 prd[i].dbc = htole32(segs[i].ds_len);
881 prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
882 slot->dma.nsegs = nsegs;
883 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
884 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
885 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
886 siis_execute_transaction(slot);
889 /* Must be called with channel locked. */
891 siis_execute_transaction(struct siis_slot *slot)
893 device_t dev = slot->dev;
894 struct siis_channel *ch = device_get_softc(dev);
895 struct siis_cmd *ctp;
896 union ccb *ccb = slot->ccb;
899 mtx_assert(&ch->mtx, MA_OWNED);
900 /* Get a piece of the workspace for this request */
901 ctp = (struct siis_cmd *)
902 (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
904 ctp->protocol_override = 0;
905 ctp->transfer_count = 0;
906 /* Special handling for Soft Reset command. */
907 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
908 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
909 ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
910 } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
911 if (ccb->ccb_h.flags & CAM_DIR_IN)
912 ctp->control |= htole16(SIIS_PRB_PACKET_READ);
913 if (ccb->ccb_h.flags & CAM_DIR_OUT)
914 ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
916 /* Setup the FIS for this request */
917 if (!siis_setup_fis(ctp, ccb, slot->slot)) {
918 device_printf(ch->dev, "Setting up SATA FIS failed\n");
920 xpt_freeze_simq(ch->sim, 1);
921 siis_end_transaction(slot, SIIS_ERR_INVALID);
924 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
925 BUS_DMASYNC_PREWRITE);
926 /* Issue command to the controller. */
927 slot->state = SIIS_SLOT_RUNNING;
928 ch->rslots |= (1 << slot->slot);
929 prb_bus = ch->dma.work_bus +
930 SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
931 ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
932 ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
933 /* Start command execution timeout */
934 callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
935 (timeout_t*)siis_timeout, slot);
939 /* Locked by callout mechanism. */
941 siis_timeout(struct siis_slot *slot)
943 device_t dev = slot->dev;
944 struct siis_channel *ch = device_get_softc(dev);
947 mtx_assert(&ch->mtx, MA_OWNED);
948 device_printf(dev, "Timeout on slot %d\n", slot->slot);
949 device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
950 __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
951 ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
952 ATA_INL(ch->r_mem, SIIS_P_SERR));
953 /* Kick controller into sane state. */
954 siis_portinit(ch->dev);
957 xpt_freeze_simq(ch->sim, ch->numrslots);
958 /* Handle command with timeout. */
959 siis_end_transaction(&ch->slot[slot->slot], SIIS_ERR_TIMEOUT);
960 /* Handle the rest of commands. */
962 union ccb *fccb = ch->frozen;
964 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
967 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
968 /* Do we have a running request on slot? */
969 if (ch->slot[i].state < SIIS_SLOT_RUNNING)
971 siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
975 /* Must be called with channel locked. */
977 siis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
979 device_t dev = slot->dev;
980 struct siis_channel *ch = device_get_softc(dev);
981 union ccb *ccb = slot->ccb;
983 mtx_assert(&ch->mtx, MA_OWNED);
984 /* Cancel command execution timeout */
985 callout_stop(&slot->timeout);
986 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
987 BUS_DMASYNC_POSTWRITE);
988 /* Read result registers to the result struct
989 * May be incorrect if several commands finished same time,
990 * so read only when sure or have to.
992 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
993 struct ata_res *res = &ccb->ataio.res;
994 if ((et == SIIS_ERR_TFE) ||
995 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
996 int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
998 res->status = ATA_INB(ch->r_mem, offs + 2);
999 res->error = ATA_INB(ch->r_mem, offs + 3);
1000 res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1001 res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1002 res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1003 res->device = ATA_INB(ch->r_mem, offs + 7);
1004 res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1005 res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1006 res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1007 res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1008 res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1010 bzero(res, sizeof(*res));
1012 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1013 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1014 (ccb->ccb_h.flags & CAM_DIR_IN) ?
1015 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1016 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1018 /* Set proper result status. */
1019 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1020 if (et != SIIS_ERR_NONE || ch->recovery) {
1021 ch->eslots |= (1 << slot->slot);
1022 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1026 ccb->ccb_h.status |= CAM_REQ_CMP;
1027 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1028 ccb->csio.scsi_status = SCSI_STATUS_OK;
1030 case SIIS_ERR_INVALID:
1031 ccb->ccb_h.status |= CAM_REQ_INVALID;
1033 case SIIS_ERR_INNOCENT:
1034 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1037 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1038 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1039 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1041 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1045 ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1047 case SIIS_ERR_TIMEOUT:
1048 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1051 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1054 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1057 ch->rslots &= ~(1 << slot->slot);
1058 ch->aslots &= ~(1 << slot->slot);
1059 slot->state = SIIS_SLOT_EMPTY;
1061 /* Update channel stats. */
1063 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1064 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1065 ch->numtslots[ccb->ccb_h.target_id]--;
1067 /* If it was NCQ command error, put result on hold. */
1068 if (et == SIIS_ERR_NCQ) {
1069 ch->hold[slot->slot] = ccb;
1071 } else if (ch->readlog) /* If it was our READ LOG command - process it. */
1072 siis_process_read_log(dev, ccb);
1075 /* Unfreeze frozen command. */
1076 if (ch->frozen && ch->numrslots == 0) {
1077 union ccb *fccb = ch->frozen;
1079 siis_begin_transaction(dev, fccb);
1080 xpt_release_simq(ch->sim, TRUE);
1082 /* If we have no other active commands, ... */
1083 if (ch->rslots == 0) {
1084 /* if we have slots in error, we can reinit port. */
1085 if (ch->eslots != 0)
1087 /* if there commands on hold, we can do READ LOG. */
1088 if (!ch->readlog && ch->numhslots)
1089 siis_issue_read_log(dev);
1094 siis_issue_read_log(device_t dev)
1096 struct siis_channel *ch = device_get_softc(dev);
1098 struct ccb_ataio *ataio;
1101 /* Find some holden command. */
1102 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1106 if (i == SIIS_MAX_SLOTS)
1109 ccb = xpt_alloc_ccb_nowait();
1111 device_printf(dev, "Unable allocate READ LOG command");
1114 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */
1115 ccb->ccb_h.func_code = XPT_ATA_IO;
1116 ccb->ccb_h.flags = CAM_DIR_IN;
1117 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
1118 ataio = &ccb->ataio;
1119 ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1120 if (ataio->data_ptr == NULL) {
1121 device_printf(dev, "Unable allocate memory for READ LOG command");
1124 ataio->dxfer_len = 512;
1125 bzero(&ataio->cmd, sizeof(ataio->cmd));
1126 ataio->cmd.flags = CAM_ATAIO_48BIT;
1127 ataio->cmd.command = 0x2F; /* READ LOG EXT */
1128 ataio->cmd.sector_count = 1;
1129 ataio->cmd.sector_count_exp = 0;
1130 ataio->cmd.lba_low = 0x10;
1131 ataio->cmd.lba_mid = 0;
1132 ataio->cmd.lba_mid_exp = 0;
1133 siis_begin_transaction(dev, ccb);
1137 siis_process_read_log(device_t dev, union ccb *ccb)
1139 struct siis_channel *ch = device_get_softc(dev);
1141 struct ata_res *res;
1145 data = ccb->ataio.data_ptr;
1146 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1147 (data[0] & 0x80) == 0) {
1148 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1151 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1153 if ((data[0] & 0x1F) == i) {
1154 res = &ch->hold[i]->ataio.res;
1155 res->status = data[2];
1156 res->error = data[3];
1157 res->lba_low = data[4];
1158 res->lba_mid = data[5];
1159 res->lba_high = data[6];
1160 res->device = data[7];
1161 res->lba_low_exp = data[8];
1162 res->lba_mid_exp = data[9];
1163 res->lba_high_exp = data[10];
1164 res->sector_count = data[12];
1165 res->sector_count_exp = data[13];
1167 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1168 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1170 xpt_done(ch->hold[i]);
1175 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1176 device_printf(dev, "Error while READ LOG EXT\n");
1177 else if ((data[0] & 0x80) == 0) {
1178 device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1180 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1183 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1185 xpt_done(ch->hold[i]);
1190 free(ccb->ataio.data_ptr, M_SIIS);
1195 siis_portinit(device_t dev)
1197 struct siis_channel *ch = device_get_softc(dev);
1202 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1203 for (i = 0; i < 16; i++) {
1204 ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1205 ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1207 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1208 siis_wait_ready(dev, 1000);
1213 siis_devreset(device_t dev)
1215 struct siis_channel *ch = device_get_softc(dev);
1217 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1218 siis_wait_ready(dev, 1000);
1223 siis_wait_ready(device_t dev, int t)
1225 struct siis_channel *ch = device_get_softc(dev);
1229 while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1230 SIIS_P_CTL_READY) == 0) {
1232 if (timeout++ > t) {
1233 device_printf(dev, "port is not ready (timeout %dms) "
1234 "status = %08x\n", t, val);
1239 device_printf(dev, "ready wait time=%dms\n", timeout);
1244 siis_reset(device_t dev)
1246 struct siis_channel *ch = device_get_softc(dev);
1250 device_printf(dev, "SIIS reset...\n");
1251 xpt_freeze_simq(ch->sim, ch->numrslots);
1252 /* Requeue freezed command. */
1254 union ccb *fccb = ch->frozen;
1256 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1259 /* Disable port interrupts */
1260 ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1261 /* Kill the engine and requeue all running commands. */
1263 for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1264 /* Do we have a running request on slot? */
1265 if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1267 /* XXX; Commands in loading state. */
1268 siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1270 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1271 /* Reset and reconnect PHY, */
1272 if (!siis_sata_phy_reset(dev)) {
1274 /* Enable port interrupts */
1275 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1278 "SIIS reset done: phy reset found no device\n");
1279 /* Tell the XPT about the event */
1280 xpt_async(AC_BUS_RESET, ch->path, NULL);
1283 /* Wait for clearing busy status. */
1284 if (siis_wait_ready(dev, 10000)) {
1285 device_printf(dev, "device ready timeout\n");
1288 /* Enable port interrupts */
1289 ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1290 ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1292 device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1293 /* Tell the XPT about the event */
1294 xpt_async(AC_BUS_RESET, ch->path, NULL);
1298 siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag)
1300 u_int8_t *fis = &ctp->fis[0];
1303 fis[0] = 0x27; /* host to device */
1304 fis[1] = (ccb->ccb_h.target_id & 0x0f);
1305 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1307 fis[2] = ATA_PACKET_CMD;
1308 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1311 fis[5] = ccb->csio.dxfer_len;
1312 fis[6] = ccb->csio.dxfer_len >> 8;
1315 fis[15] = ATA_A_4BIT;
1316 bzero(ctp->u.atapi.ccb, 16);
1317 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1318 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1319 ctp->u.atapi.ccb, ccb->csio.cdb_len);
1320 } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1322 fis[2] = ccb->ataio.cmd.command;
1323 fis[3] = ccb->ataio.cmd.features;
1324 fis[4] = ccb->ataio.cmd.lba_low;
1325 fis[5] = ccb->ataio.cmd.lba_mid;
1326 fis[6] = ccb->ataio.cmd.lba_high;
1327 fis[7] = ccb->ataio.cmd.device;
1328 fis[8] = ccb->ataio.cmd.lba_low_exp;
1329 fis[9] = ccb->ataio.cmd.lba_mid_exp;
1330 fis[10] = ccb->ataio.cmd.lba_high_exp;
1331 fis[11] = ccb->ataio.cmd.features_exp;
1332 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1336 fis[12] = ccb->ataio.cmd.sector_count;
1337 fis[13] = ccb->ataio.cmd.sector_count_exp;
1339 fis[15] = ATA_A_4BIT;
1347 siis_sata_connect(struct siis_channel *ch)
1352 /* Wait up to 100ms for "connect well" */
1353 for (timeout = 0; timeout < 100 ; timeout++) {
1354 status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1355 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1356 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1357 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1361 if (timeout >= 100) {
1363 device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1369 device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1372 /* Clear SATA error register */
1373 ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1378 siis_sata_phy_reset(device_t dev)
1380 struct siis_channel *ch = device_get_softc(dev);
1384 device_printf(dev, "hardware reset ...\n");
1385 ATA_OUTL(ch->r_mem, SIIS_P_SCTL, ATA_SC_IPM_DIS_PARTIAL |
1386 ATA_SC_IPM_DIS_SLUMBER | ATA_SC_DET_RESET);
1388 if (ch->sata_rev == 1)
1389 val = ATA_SC_SPD_SPEED_GEN1;
1390 else if (ch->sata_rev == 2)
1391 val = ATA_SC_SPD_SPEED_GEN2;
1392 else if (ch->sata_rev == 3)
1393 val = ATA_SC_SPD_SPEED_GEN3;
1396 ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1397 ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1398 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1400 return (siis_sata_connect(ch));
1404 siisaction(struct cam_sim *sim, union ccb *ccb)
1407 struct siis_channel *ch;
1409 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1410 ccb->ccb_h.func_code));
1412 ch = (struct siis_channel *)cam_sim_softc(sim);
1414 mtx_assert(&ch->mtx, MA_OWNED);
1415 switch (ccb->ccb_h.func_code) {
1416 /* Common cases first */
1417 case XPT_ATA_IO: /* Execute the requested I/O operation */
1419 if (ch->devices == 0) {
1420 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1424 /* Check for command collision. */
1425 if (siis_check_collision(dev, ccb)) {
1426 /* Freeze command. */
1428 /* We have only one frozen slot, so freeze simq also. */
1429 xpt_freeze_simq(ch->sim, 1);
1432 siis_begin_transaction(dev, ccb);
1434 case XPT_EN_LUN: /* Enable LUN as a target */
1435 case XPT_TARGET_IO: /* Execute target I/O request */
1436 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1437 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1438 case XPT_ABORT: /* Abort the specified CCB */
1440 ccb->ccb_h.status = CAM_REQ_INVALID;
1443 case XPT_SET_TRAN_SETTINGS:
1445 struct ccb_trans_settings *cts = &ccb->cts;
1447 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1448 if (cts->xport_specific.sata.pm_present)
1449 ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1451 ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1453 ccb->ccb_h.status = CAM_REQ_CMP;
1457 case XPT_GET_TRAN_SETTINGS:
1458 /* Get default/user set transfer settings for the target */
1460 struct ccb_trans_settings *cts = &ccb->cts;
1463 cts->protocol = PROTO_ATA;
1464 cts->protocol_version = SCSI_REV_2;
1465 cts->transport = XPORT_SATA;
1466 cts->transport_version = 2;
1467 cts->proto_specific.valid = 0;
1468 cts->xport_specific.sata.valid = 0;
1469 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1470 status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1472 status = ATA_INL(ch->r_mem, SIIS_P_SCTL) & ATA_SC_SPD_MASK;
1473 if (status & ATA_SS_SPD_GEN3) {
1474 cts->xport_specific.sata.bitrate = 600000;
1475 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1476 } else if (status & ATA_SS_SPD_GEN2) {
1477 cts->xport_specific.sata.bitrate = 300000;
1478 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1479 } else if (status & ATA_SS_SPD_GEN1) {
1480 cts->xport_specific.sata.bitrate = 150000;
1481 cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1483 cts->xport_specific.sata.pm_present =
1484 (ATA_INL(ch->r_mem, SIIS_P_STS) & SIIS_P_CTL_PME) ?
1486 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1487 ccb->ccb_h.status = CAM_REQ_CMP;
1492 case XPT_CALC_GEOMETRY:
1494 struct ccb_calc_geometry *ccg;
1496 uint32_t secs_per_cylinder;
1499 size_mb = ccg->volume_size
1500 / ((1024L * 1024L) / ccg->block_size);
1501 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1502 if (size_mb >= 2048) {
1504 ccg->secs_per_track = 63;
1507 ccg->secs_per_track = 32;
1511 ccg->secs_per_track = 32;
1513 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1514 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1515 ccb->ccb_h.status = CAM_REQ_CMP;
1520 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1521 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1523 ccb->ccb_h.status = CAM_REQ_CMP;
1526 case XPT_TERM_IO: /* Terminate the I/O process */
1528 ccb->ccb_h.status = CAM_REQ_INVALID;
1531 case XPT_PATH_INQ: /* Path routing inquiry */
1533 struct ccb_pathinq *cpi = &ccb->cpi;
1535 cpi->version_num = 1; /* XXX??? */
1536 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1537 cpi->hba_inquiry |= PI_SATAPM;
1538 cpi->target_sprt = 0;
1539 cpi->hba_misc = PIM_SEQSCAN;
1540 cpi->hba_eng_cnt = 0;
1541 cpi->max_target = 14;
1543 cpi->initiator_id = 0;
1544 cpi->bus_id = cam_sim_bus(sim);
1545 cpi->base_transfer_speed = 150000;
1546 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1547 strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1548 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1549 cpi->unit_number = cam_sim_unit(sim);
1550 cpi->transport = XPORT_SATA;
1551 cpi->transport_version = 2;
1552 cpi->protocol = PROTO_ATA;
1553 cpi->protocol_version = SCSI_REV_2;
1554 cpi->ccb_h.status = CAM_REQ_CMP;
1555 cpi->maxio = MAXPHYS;
1560 ccb->ccb_h.status = CAM_REQ_INVALID;
1567 siispoll(struct cam_sim *sim)
1569 struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1571 siis_ch_intr(ch->dev);