4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies.
43 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
44 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
46 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
47 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
48 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
49 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 * $OpenBSD: sili.c,v 1.147 2009/02/16 21:19:07 miod Exp $
58 void sili_port_interrupt_enable(struct sili_port *ap);
59 void sili_port_interrupt_redisable(struct sili_port *ap);
60 void sili_port_interrupt_reenable(struct sili_port *ap);
62 int sili_load_prb(struct sili_ccb *);
63 void sili_unload_prb(struct sili_ccb *);
64 static void sili_load_prb_callback(void *info, bus_dma_segment_t *segs,
65 int nsegs, int error);
66 void sili_start(struct sili_ccb *);
67 static void sili_port_reinit(struct sili_port *ap);
68 int sili_port_softreset(struct sili_port *ap);
69 int sili_port_hardreset(struct sili_port *ap);
70 void sili_port_hardstop(struct sili_port *ap);
71 void sili_port_listen(struct sili_port *ap);
73 static void sili_ata_cmd_timeout_unserialized(void *);
74 static int sili_core_timeout(struct sili_ccb *ccb, int really_error);
75 void sili_check_active_timeouts(struct sili_port *ap);
77 void sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb);
79 void sili_port_read_ncq_error(struct sili_port *, int);
81 struct sili_dmamem *sili_dmamem_alloc(struct sili_softc *, bus_dma_tag_t tag);
82 void sili_dmamem_free(struct sili_softc *, struct sili_dmamem *);
83 static void sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
85 static void sili_dummy_done(struct ata_xfer *xa);
86 static void sili_empty_done(struct sili_ccb *ccb);
87 static void sili_ata_cmd_done(struct sili_ccb *ccb);
90 * Initialize the global SILI hardware. This code does not set up any of
94 sili_init(struct sili_softc *sc)
96 DPRINTF(SILI_D_VERBOSE, " GHC 0x%b",
97 sili_read(sc, SILI_REG_GHC), SILI_FMT_GHC);
100 * Reset the entire chip. This also resets all ports.
102 * The spec doesn't say anything about how long we have to
103 * wait, so wait 10ms.
105 sili_write(sc, SILI_REG_GCTL, SILI_REG_GCTL_GRESET);
107 sili_write(sc, SILI_REG_GCTL, 0);
114 * Allocate and initialize an SILI port.
117 sili_port_alloc(struct sili_softc *sc, u_int port)
119 struct sili_port *ap;
121 struct sili_prb *prb;
122 struct sili_ccb *ccb;
127 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
128 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
130 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
131 device_get_name(sc->sc_dev),
132 device_get_unit(sc->sc_dev),
134 sc->sc_ports[port] = ap;
137 * Allocate enough so we never have to reallocate, it makes
140 * ap_pmcount will be reduced by the scan if we encounter the
141 * port multiplier port prior to target 15.
143 if (ap->ap_ata == NULL) {
144 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * SILI_MAX_PMPORTS,
145 M_DEVBUF, M_INTWAIT | M_ZERO);
146 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
148 at->at_sili_port = ap;
150 at->at_probe = ATA_PROBE_NEED_INIT;
151 at->at_features |= ATA_PORT_F_RESCAN;
152 ksnprintf(at->at_name, sizeof(at->at_name),
153 "%s.%d", ap->ap_name, i);
156 if (bus_space_subregion(sc->sc_piot, sc->sc_pioh,
157 SILI_PORT_REGION(port), SILI_PORT_SIZE,
159 device_printf(sc->sc_dev,
160 "unable to create register window for port %d\n",
167 ap->ap_probe = ATA_PROBE_NEED_INIT;
168 TAILQ_INIT(&ap->ap_ccb_free);
169 TAILQ_INIT(&ap->ap_ccb_pending);
170 lockinit(&ap->ap_ccb_lock, "silipo", 0, 0);
172 /* Disable port interrupts */
173 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
176 * Reset the port. This is similar to a Device Reset but far
177 * more invasive. We use Device Reset in our hardreset function.
178 * This function also does the same OOB initialization sequence
179 * that Device Reset does.
181 * NOTE: SILI_PREG_STATUS_READY will not be asserted unless and until
182 * a device is connected to the port, so we can't use it to
183 * verify that the port exists.
185 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
186 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
187 device_printf(sc->sc_dev,
188 "Port %d will not go into reset\n", port);
192 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
195 * Allocate the SGE Table
197 ap->ap_dmamem_prbs = sili_dmamem_alloc(sc, sc->sc_tag_prbs);
198 if (ap->ap_dmamem_prbs == NULL) {
199 kprintf("%s: NOSGET\n", PORTNAME(ap));
204 * Set up the SGE table base address
206 ap->ap_prbs = (struct sili_prb *)SILI_DMA_KVA(ap->ap_dmamem_prbs);
209 * Allocate a CCB for each command slot
211 ap->ap_ccbs = kmalloc(sizeof(struct sili_ccb) * sc->sc_ncmds, M_DEVBUF,
213 if (ap->ap_ccbs == NULL) {
214 device_printf(sc->sc_dev,
215 "unable to allocate command list for port %d\n",
221 * Most structures are in the port BAR. Assign convenient
222 * pointers in the CCBs
224 for (i = 0; i < sc->sc_ncmds; i++) {
225 ccb = &ap->ap_ccbs[i];
227 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
230 device_printf(sc->sc_dev,
231 "unable to create dmamap for port %d "
232 "ccb %d\n", port, i);
237 * WARNING!!! Access to the rfis is only allowed under very
238 * carefully controlled circumstances because it
239 * is located in the LRAM and reading from the
240 * LRAM has hardware issues which can blow the
241 * port up. I kid you not (from Linux, and
242 * verified by testing here).
244 callout_init(&ccb->ccb_timeout);
247 ccb->ccb_prb = &ap->ap_prbs[i];
248 ccb->ccb_prb_paddr = SILI_DMA_DVA(ap->ap_dmamem_prbs) +
249 sizeof(*ccb->ccb_prb) * i;
250 ccb->ccb_xa.fis = &ccb->ccb_prb->prb_h2d;
251 prb = bus_space_kva(ap->ap_sc->sc_iot, ap->ap_ioh,
252 SILI_PREG_LRAM_SLOT(i));
253 ccb->ccb_prb_lram = prb;
255 * Point our rfis to host-memory instead of the LRAM PRB.
256 * It will be copied back if ATA_F_AUTOSENSE is set. The
259 /*ccb->ccb_xa.rfis = &prb->prb_d2h;*/
260 ccb->ccb_xa.rfis = (void *)ccb->ccb_xa.fis;
262 ccb->ccb_xa.packetcmd = prb_packet(ccb->ccb_prb);
265 ccb->ccb_xa.state = ATA_S_COMPLETE;
268 * Reserve CCB[1] as the error CCB. It doesn't matter
269 * which one we use for the Sili controllers.
272 ap->ap_err_ccb = ccb;
277 * Do not call sili_port_init() here, the helper thread will
278 * call it for the parallel probe
280 sili_os_start_port(ap);
283 sili_port_free(sc, port);
288 * This is called once by the low level attach (from the helper thread)
289 * to get the port state machine rolling, and typically only called again
290 * on a hot-plug insertion event.
292 * This is called for PM attachments and hot-plug insertion events, and
293 * typically not called again until after an unplug/replug sequence.
295 * Returns 0 if a device is successfully detected.
298 sili_port_init(struct sili_port *ap)
301 * Do a very hard reset of the port
303 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
305 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
308 * Register initialization
310 sili_pwrite(ap, SILI_PREG_FIFO_CTL,
311 SILI_PREG_FIFO_CTL_ENCODE(1024, 1024));
312 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_32BITDMA |
314 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_NOAUTOCC);
315 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
316 sili_pwrite(ap, SILI_PREG_SNTF, -1);
317 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
319 sili_port_interrupt_enable(ap);
324 * Handle an errored port. This routine is called when the only
325 * commands left on the queue are expired, meaning we can safely
326 * go through a port init to clear its state.
328 * We complete the expired CCBs and then restart the queue.
332 sili_port_reinit(struct sili_port *ap)
334 struct sili_ccb *ccb;
340 if (bootverbose || 1) {
341 kprintf("%s: reiniting port after error reent=%d "
344 (ap->ap_flags & AP_F_REINIT_ACTIVE),
349 * Clear port resume, clear bits 16:13 in the port device status
350 * register. This is from the data sheet.
352 * Data sheet does not specify a delay but it seems prudent.
354 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
356 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
357 data = sili_pread(ap, SILI_PREG_PM_STATUS(target));
358 data &= ~(SILI_PREG_PM_STATUS_SERVICE |
359 SILI_PREG_PM_STATUS_LEGACY |
360 SILI_PREG_PM_STATUS_NATIVE |
361 SILI_PREG_PM_STATUS_VBSY);
362 sili_pwrite(ap, SILI_PREG_PM_STATUS(target), data);
363 sili_pwrite(ap, SILI_PREG_PM_QACTIVE(target), 0);
367 * Issue a Port Initialize and wait for it to clear. This flushes
368 * commands but does not reset the port. Then wait for port ready.
370 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
371 if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
372 kprintf("%s: Unable to reinit, port failed\n",
375 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
376 kprintf("%s: Unable to reinit, port will not come ready\n",
381 * If reentrant, stop here. Otherwise the state for the original
382 * ahci_port_reinit() will get ripped out from under it.
384 if (ap->ap_flags & AP_F_REINIT_ACTIVE)
386 ap->ap_flags |= AP_F_REINIT_ACTIVE;
389 * Read the LOG ERROR page for targets that returned a specific
390 * D2H FIS with ERR set.
392 * Don't bother if we are already using the error CCB.
394 if ((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0) {
395 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
396 at = &ap->ap_ata[target];
397 if (at->at_features & ATA_PORT_F_READLOG) {
398 at->at_features &= ~ATA_PORT_F_READLOG;
399 sili_port_read_ncq_error(ap, target);
405 * Finally clean out the expired commands, we've probed the error
406 * status (or hopefully probed the error status). Well, ok,
407 * we probably didn't XXX.
409 while (ap->ap_expired) {
410 slot = ffs(ap->ap_expired) - 1;
411 ap->ap_expired &= ~(1 << slot);
412 KKASSERT(ap->ap_active & (1 << slot));
413 ap->ap_active &= ~(1 << slot);
415 ccb = &ap->ap_ccbs[slot];
416 ccb->ccb_xa.state = ATA_S_TIMEOUT;
418 ccb->ccb_xa.complete(&ccb->ccb_xa);
420 ap->ap_flags &= ~AP_F_REINIT_ACTIVE;
423 * Wow. All done. We can get the port moving again.
425 if (ap->ap_probe == ATA_PROBE_FAILED) {
426 kprintf("%s: reinit failed, port is dead\n", PORTNAME(ap));
427 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
428 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
429 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
430 ccb->ccb_xa.state = ATA_S_TIMEOUT;
432 ccb->ccb_xa.complete(&ccb->ccb_xa);
435 sili_issue_pending_commands(ap, NULL);
440 * Enable or re-enable interrupts on a port.
442 * This routine is called from the port initialization code or from the
443 * helper thread as the real interrupt may be forced to turn off certain
447 sili_port_interrupt_enable(struct sili_port *ap)
451 data = SILI_PREG_INT_CCOMPLETE | SILI_PREG_INT_CERROR |
452 SILI_PREG_INT_PHYRDYCHG | SILI_PREG_INT_DEVEXCHG |
453 SILI_PREG_INT_DECODE | SILI_PREG_INT_CRC |
454 SILI_PREG_INT_HANDSHK | SILI_PREG_INT_PMCHANGE;
455 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
456 data |= SILI_PREG_INT_SDB;
457 sili_pwrite(ap, SILI_PREG_INT_ENABLE, data);
461 sili_port_interrupt_redisable(struct sili_port *ap)
465 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
466 data &= SILI_REG_GINT_PORTMASK;
467 data &= ~(1 << ap->ap_num);
468 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
472 sili_port_interrupt_reenable(struct sili_port *ap)
476 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
477 data &= SILI_REG_GINT_PORTMASK;
478 data |= (1 << ap->ap_num);
479 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
483 * Run the port / target state machine from a main context.
485 * The state machine for the port is always run.
487 * If atx is non-NULL run the state machine for a particular target.
488 * If atx is NULL run the state machine for all targets.
491 sili_port_state_machine(struct sili_port *ap, int initial)
500 * State machine for port. Note that CAM is not yet associated
501 * during the initial parallel probe and the port's probe state
502 * will not get past ATA_PROBE_NEED_IDENT.
505 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
506 kprintf("%s: Waiting 7 seconds on insertion\n",
511 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
513 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
514 sili_port_reset(ap, NULL, 1);
515 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
516 sili_port_reset(ap, NULL, 0);
517 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
518 sili_cam_probe(ap, NULL);
520 if (ap->ap_type != ATA_PORT_T_PM) {
521 if (ap->ap_probe == ATA_PROBE_FAILED) {
522 sili_cam_changed(ap, NULL, 0);
523 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
524 sili_cam_changed(ap, NULL, 1);
530 * Port Multiplier state machine.
532 * Get a mask of changed targets and combine with any runnable
533 * states already present.
535 for (loop = 0; ;++loop) {
536 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
537 kprintf("%s: PM unable to read hot-plug bitmap\n",
543 * Do at least one loop, then stop if no more state changes
544 * have occured. The PM might not generate a new
545 * notification until we clear the entire bitmap.
547 if (loop && data == 0)
551 * New devices showing up in the bitmap require some spin-up
552 * time before we start probing them. Reset didsleep. The
553 * first new device we detect will sleep before probing.
555 * This only applies to devices whos change bit is set in
556 * the data, and does not apply to the initial boot-time
561 for (target = 0; target < ap->ap_pmcount; ++target) {
562 at = &ap->ap_ata[target];
565 * Check the target state for targets behind the PM
566 * which have changed state. This will adjust
567 * at_probe and set ATA_PORT_F_RESCAN
569 * We want to wait at least 10 seconds before probing
570 * a newly inserted device. If the check status
571 * indicates a device is present and in need of a
572 * hard reset, we make sure we have slept before
575 * We also need to wait at least 1 second for the
576 * PHY state to change after insertion, if we
577 * haven't already waited the 10 seconds.
579 * NOTE: When pm_check_good finds a good port it
580 * typically starts us in probe state
581 * NEED_HARD_RESET rather than INIT.
583 if (data & (1 << target)) {
584 if (initial == 0 && didsleep == 0)
586 sili_pm_check_good(ap, target);
587 if (initial == 0 && didsleep == 0 &&
588 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
591 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
592 sili_os_sleep(10000);
597 * Report hot-plug events before the probe state
598 * really gets hot. Only actual events are reported
599 * here to reduce spew.
601 if (data & (1 << target)) {
602 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
603 switch(at->at_probe) {
604 case ATA_PROBE_NEED_INIT:
605 case ATA_PROBE_NEED_HARD_RESET:
606 kprintf("Device inserted\n");
608 case ATA_PROBE_FAILED:
609 kprintf("Device removed\n");
612 kprintf("Device probe in progress\n");
618 * Run through the state machine as necessary if
619 * the port is not marked failed.
621 * The state machine may stop at NEED_IDENT if
622 * CAM is not yet attached.
624 * Acquire exclusive access to the port while we
625 * are doing this. This prevents command-completion
626 * from queueing commands for non-polled targets
627 * inbetween our probe steps. We need to do this
628 * because the reset probes can generate severe PHY
629 * and protocol errors and soft-brick the port.
631 if (at->at_probe != ATA_PROBE_FAILED &&
632 at->at_probe != ATA_PROBE_GOOD) {
633 if (at->at_probe == ATA_PROBE_NEED_INIT)
634 sili_pm_port_init(ap, at);
635 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
636 sili_port_reset(ap, at, 1);
637 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
638 sili_port_reset(ap, at, 0);
639 if (at->at_probe == ATA_PROBE_NEED_IDENT)
640 sili_cam_probe(ap, at);
644 * Add or remove from CAM
646 if (at->at_features & ATA_PORT_F_RESCAN) {
647 at->at_features &= ~ATA_PORT_F_RESCAN;
648 if (at->at_probe == ATA_PROBE_FAILED) {
649 sili_cam_changed(ap, at, 0);
650 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
651 sili_cam_changed(ap, at, 1);
654 data &= ~(1 << target);
657 kprintf("%s: WARNING (PM): extra bits set in "
658 "EINFO: %08x\n", PORTNAME(ap), data);
659 while (target < SILI_MAX_PMPORTS) {
660 sili_pm_check_good(ap, target);
668 * De-initialize and detach a port.
671 sili_port_free(struct sili_softc *sc, u_int port)
673 struct sili_port *ap = sc->sc_ports[port];
674 struct sili_ccb *ccb;
677 * Ensure port is disabled and its interrupts are all flushed.
680 sili_os_stop_port(ap);
681 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
682 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
683 sili_write(ap->ap_sc, SILI_REG_GCTL,
684 sili_read(ap->ap_sc, SILI_REG_GCTL) &
685 ~SILI_REG_GINT_PORTST(ap->ap_num));
689 while ((ccb = sili_get_ccb(ap)) != NULL) {
690 if (ccb->ccb_dmamap) {
691 bus_dmamap_destroy(sc->sc_tag_data,
693 ccb->ccb_dmamap = NULL;
696 if ((ccb = ap->ap_err_ccb) != NULL) {
697 if (ccb->ccb_dmamap) {
698 bus_dmamap_destroy(sc->sc_tag_data,
700 ccb->ccb_dmamap = NULL;
702 ap->ap_err_ccb = NULL;
704 kfree(ap->ap_ccbs, M_DEVBUF);
708 if (ap->ap_dmamem_prbs) {
709 sili_dmamem_free(sc, ap->ap_dmamem_prbs);
710 ap->ap_dmamem_prbs = NULL;
713 kfree(ap->ap_ata, M_DEVBUF);
716 if (ap->ap_err_scratch) {
717 kfree(ap->ap_err_scratch, M_DEVBUF);
718 ap->ap_err_scratch = NULL;
721 /* bus_space(9) says we dont free the subregions handle */
724 sc->sc_ports[port] = NULL;
730 * If hard is 0 perform a softreset of the port.
731 * If hard is 1 perform a hard reset of the port.
732 * If hard is 2 perform a hard reset of the port and cycle the phy.
734 * If at is non-NULL an indirect port via a port-multiplier is being
735 * reset, otherwise a direct port is being reset.
737 * NOTE: Indirect ports can only be soft-reset.
740 sili_port_reset(struct sili_port *ap, struct ata_port *at, int hard)
746 rc = sili_pm_hardreset(ap, at->at_target, hard);
748 rc = sili_port_hardreset(ap);
751 rc = sili_pm_softreset(ap, at->at_target);
753 rc = sili_port_softreset(ap);
759 * SILI soft reset, Section 10.4.1
761 * (at) will be NULL when soft-resetting a directly-attached device, and
762 * non-NULL when soft-resetting a device through a port multiplier.
764 * This function keeps port communications intact and attempts to generate
765 * a reset to the connected device using device commands.
768 sili_port_softreset(struct sili_port *ap)
770 struct sili_ccb *ccb = NULL;
771 struct sili_prb *prb;
778 kprintf("%s: START SOFTRESET\n", PORTNAME(ap));
781 ap->ap_state = AP_S_NORMAL;
784 * Prep the special soft-reset SII command.
786 ccb = sili_get_err_ccb(ap);
787 ccb->ccb_done = sili_empty_done;
788 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
789 ccb->ccb_xa.complete = sili_dummy_done;
790 ccb->ccb_xa.at = NULL;
793 bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
794 prb->prb_h2d.flags = 0;
795 prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
796 prb->prb_override = 0;
797 prb->prb_xfer_count = 0;
799 ccb->ccb_xa.state = ATA_S_PENDING;
802 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
804 if (sili_poll(ccb, 8000, sili_quick_timeout) != ATA_S_COMPLETE) {
805 kprintf("%s: First FIS failed\n", PORTNAME(ap));
809 sig = (prb->prb_d2h.lba_high << 24) |
810 (prb->prb_d2h.lba_mid << 16) |
811 (prb->prb_d2h.lba_low << 8) |
812 (prb->prb_d2h.sector_count);
814 kprintf("%s: SOFTRESET SIGNATURE %08x\n", PORTNAME(ap), sig);
817 * If the softreset is trying to clear a BSY condition after a
818 * normal portreset we assign the port type.
820 * If the softreset is being run first as part of the ccb error
821 * processing code then report if the device signature changed
824 if (ap->ap_type == ATA_PORT_T_NONE) {
825 ap->ap_type = sili_port_signature(ap, NULL, sig);
827 if (sili_port_signature(ap, NULL, sig) != ap->ap_type) {
828 kprintf("%s: device signature unexpectedly "
829 "changed\n", PORTNAME(ap));
830 error = EBUSY; /* XXX */
836 sili_put_err_ccb(ccb);
840 * If we failed to softreset make the port quiescent, otherwise
841 * make sure the port's start/stop state matches what it was on
844 * Don't kill the port if the softreset is on a port multiplier
845 * target, that would kill all the targets!
848 kprintf("%s: END SOFTRESET %d prob=%d state=%d\n",
849 PORTNAME(ap), error, ap->ap_probe, ap->ap_state);
852 sili_port_hardstop(ap);
853 /* ap_probe set to failed */
855 ap->ap_probe = ATA_PROBE_NEED_IDENT;
860 sili_pwrite(ap, SILI_PREG_SERR, -1);
862 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
868 * This function does a hard reset of the port. Note that the device
869 * connected to the port could still end-up hung. Phy detection is
870 * used to short-cut longer operations.
873 sili_port_hardreset(struct sili_port *ap)
880 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
882 ap->ap_state = AP_S_NORMAL;
885 * Set SCTL up for any speed restrictions before issuing the
886 * device reset. This may also take us out of an INIT state
887 * (if we were previously in a continuous reset state from
888 * sili_port_listen()).
890 data = SILI_PREG_SCTL_SPM_NONE |
891 SILI_PREG_SCTL_IPM_NONE |
892 SILI_PREG_SCTL_SPD_NONE |
893 SILI_PREG_SCTL_DET_NONE;
894 if (SiliForceGen1 & (1 << ap->ap_num)) {
895 data &= ~SILI_PREG_SCTL_SPD_NONE;
896 data |= SILI_PREG_SCTL_SPD_GEN1;
898 sili_pwrite(ap, SILI_PREG_SCTL, data);
901 * The transition from a continuous COMRESET state from
902 * sili_port_listen() back to device detect can take a
903 * few seconds. It's quite non-deterministic. Most of
904 * the time it takes far less. Use a polling loop to
909 data = sili_pread(ap, SILI_PREG_SSTS);
910 if (data & SILI_PREG_SSTS_DET)
912 loop -= sili_os_softsleep();
917 * Issue Device Reset, give the phy a little time to settle down.
919 * NOTE: Unlike Port Reset, the port ready signal will not
920 * go active unless a device is established to be on
923 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
924 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
925 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
926 if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
927 kprintf("%s: hardreset failed to clear\n", PORTNAME(ap));
932 * Try to determine if there is a device on the port.
934 * Give the device 3/10 second to at least be detected.
938 data = sili_pread(ap, SILI_PREG_SSTS);
939 if (data & SILI_PREG_SSTS_DET)
941 loop -= sili_os_softsleep();
945 kprintf("%s: Port appears to be unplugged\n",
953 * There is something on the port. Give the device 3 seconds
956 if (sili_pwait_eq(ap, 3000, SILI_PREG_SSTS,
957 SILI_PREG_SSTS_DET, SILI_PREG_SSTS_DET_DEV)) {
959 kprintf("%s: Device may be powered down\n",
967 * We got something that definitely looks like a device. Give
968 * the device time to send us its first D2H FIS.
970 * This effectively waits for BSY to clear.
972 if (sili_pwait_set_to(ap, 3000, SILI_PREG_STATUS,
973 SILI_PREG_STATUS_READY)) {
981 * Do the PM port probe regardless of how things turned out above.
983 * If the PM port probe fails it will return the original error
986 if (ap->ap_sc->sc_flags & SILI_F_SPM) {
987 error = sili_pm_port_probe(ap, error);
996 if (ap->ap_type == ATA_PORT_T_PM)
997 ap->ap_probe = ATA_PROBE_GOOD;
999 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1003 * No device detected.
1005 data = sili_pread(ap, SILI_PREG_SSTS);
1007 switch(data & SATA_PM_SSTS_DET) {
1008 case SILI_PREG_SSTS_DET_DEV_NE:
1009 kprintf("%s: Device not communicating\n",
1012 case SILI_PREG_SSTS_DET_OFFLINE:
1013 kprintf("%s: PHY offline\n",
1017 kprintf("%s: No device detected\n",
1021 sili_port_hardstop(ap);
1027 kprintf("%s: Device on port is bricked\n",
1029 sili_port_hardstop(ap);
1032 sili_pwrite(ap, SILI_PREG_SERR, -1);
1035 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1040 * Hard-stop on hot-swap device removal. See 10.10.1
1042 * Place the port in a mode that will allow it to detect hot-swap insertions.
1043 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1044 * seem to do the job.
1047 sili_port_hardstop(struct sili_port *ap)
1049 struct sili_ccb *ccb;
1050 struct ata_port *at;
1055 ap->ap_state = AP_S_FATAL_ERROR;
1056 ap->ap_probe = ATA_PROBE_FAILED;
1057 ap->ap_type = ATA_PORT_T_NONE;
1060 * Clean up AT sub-ports on SATA port.
1062 for (i = 0; ap->ap_ata && i < SILI_MAX_PMPORTS; ++i) {
1063 at = &ap->ap_ata[i];
1064 at->at_type = ATA_PORT_T_NONE;
1065 at->at_probe = ATA_PROBE_FAILED;
1066 at->at_features &= ~ATA_PORT_F_READLOG;
1070 * Kill the port. Don't bother waiting for it to transition
1073 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
1074 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
1075 kprintf("%s: Port will not go into reset\n",
1079 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
1082 * Turn off port-multiplier control bit
1084 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1087 * Clean up the command list.
1090 while (ap->ap_active) {
1091 slot = ffs(ap->ap_active) - 1;
1092 ap->ap_active &= ~(1 << slot);
1093 ap->ap_expired &= ~(1 << slot);
1094 --ap->ap_active_cnt;
1095 ccb = &ap->ap_ccbs[slot];
1096 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1097 serial = ccb->ccb_xa.serial;
1098 callout_stop_sync(&ccb->ccb_timeout);
1099 if (serial != ccb->ccb_xa.serial) {
1100 kprintf("%s: Warning: timeout race ccb %p\n",
1104 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1106 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1107 ATA_F_TIMEOUT_EXPIRED);
1108 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1110 ccb->ccb_xa.complete(&ccb->ccb_xa);
1112 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1113 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1114 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1115 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1117 ccb->ccb_xa.complete(&ccb->ccb_xa);
1119 KKASSERT(ap->ap_active_cnt == 0);
1122 * Put the port into a listen mode, we want to get insertion/removal
1125 sili_port_listen(ap);
1129 * Place port into a listen mode for hotplug events only. The port has
1130 * already been reset and the command processor may not be ready due
1131 * to the lack of a device.
1134 sili_port_listen(struct sili_port *ap)
1139 data = SILI_PREG_SCTL_SPM_NONE |
1140 SILI_PREG_SCTL_IPM_NONE |
1141 SILI_PREG_SCTL_SPD_NONE |
1142 SILI_PREG_SCTL_DET_INIT;
1143 if (SiliForceGen1 & (1 << ap->ap_num)) {
1144 data &= ~SILI_PREG_SCTL_SPD_NONE;
1145 data |= SILI_PREG_SCTL_SPD_GEN1;
1149 sili_pwrite(ap, SILI_PREG_SERR, -1);
1150 sili_pwrite(ap, SILI_PREG_INT_ENABLE, SILI_PREG_INT_PHYRDYCHG |
1151 SILI_PREG_INT_DEVEXCHG);
1155 * Figure out what type of device is connected to the port, ATAPI or
1159 sili_port_signature(struct sili_port *ap, struct ata_port *at, u_int32_t sig)
1162 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1163 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1164 return(ATA_PORT_T_ATAPI);
1165 } else if ((sig & 0xffff0000) ==
1166 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1167 return(ATA_PORT_T_PM);
1169 return(ATA_PORT_T_DISK);
1174 * Load the DMA descriptor table for a CCB's buffer.
1176 * NOTE: ATA_F_PIO is auto-selected by sili part.
1179 sili_load_prb(struct sili_ccb *ccb)
1181 struct sili_port *ap = ccb->ccb_port;
1182 struct sili_softc *sc = ap->ap_sc;
1183 struct ata_xfer *xa = &ccb->ccb_xa;
1184 struct sili_prb *prb = ccb->ccb_prb;
1185 struct sili_sge *sge;
1186 bus_dmamap_t dmap = ccb->ccb_dmamap;
1190 * Set up the PRB. The PRB contains 2 SGE's (1 if it is an ATAPI
1191 * command). The SGE must be set up to link to the rest of our
1192 * SGE array, in blocks of four SGEs (a SGE table) starting at
1194 prb->prb_xfer_count = 0;
1195 prb->prb_control = 0;
1196 prb->prb_override = 0;
1197 sge = (ccb->ccb_xa.flags & ATA_F_PACKET) ?
1198 &prb->prb_sge_packet : &prb->prb_sge_normal;
1199 if (xa->datalen == 0) {
1200 sge->sge_flags = SILI_SGE_FLAGS_TRM | SILI_SGE_FLAGS_DRD;
1205 if (ccb->ccb_xa.flags & ATA_F_READ)
1206 prb->prb_control |= SILI_PRB_CTRL_READ;
1207 if (ccb->ccb_xa.flags & ATA_F_WRITE)
1208 prb->prb_control |= SILI_PRB_CTRL_WRITE;
1209 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1211 sge->sge_paddr = ccb->ccb_prb_paddr +
1212 offsetof(struct sili_prb, prb_sge[0]);
1215 * Load our sge array.
1217 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1218 xa->data, xa->datalen,
1219 sili_load_prb_callback,
1221 ((xa->flags & ATA_F_NOWAIT) ?
1222 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1224 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1228 bus_dmamap_sync(sc->sc_tag_data, dmap,
1229 (xa->flags & ATA_F_READ) ?
1230 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1236 * Callback from BUSDMA system to load the segment list.
1238 * The scatter/gather table is loaded by the sili chip in blocks of
1239 * four SGE's. If a continuance is required the last entry in each
1240 * block must point to the next block.
1244 sili_load_prb_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1247 struct sili_ccb *ccb = info;
1248 struct sili_sge *sge;
1251 KKASSERT(nsegs <= SILI_MAX_SGET);
1254 sge = &ccb->ccb_prb->prb_sge[0];
1256 if ((sgi & 3) == 3) {
1257 sge->sge_paddr = htole64(ccb->ccb_prb_paddr +
1258 offsetof(struct sili_prb,
1261 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1263 sge->sge_paddr = htole64(segs->ds_addr);
1264 sge->sge_count = htole32(segs->ds_len);
1273 sge->sge_flags |= SILI_SGE_FLAGS_TRM;
1277 sili_unload_prb(struct sili_ccb *ccb)
1279 struct sili_port *ap = ccb->ccb_port;
1280 struct sili_softc *sc = ap->ap_sc;
1281 struct ata_xfer *xa = &ccb->ccb_xa;
1282 bus_dmamap_t dmap = ccb->ccb_dmamap;
1284 if (xa->datalen != 0) {
1285 bus_dmamap_sync(sc->sc_tag_data, dmap,
1286 (xa->flags & ATA_F_READ) ?
1287 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1289 bus_dmamap_unload(sc->sc_tag_data, dmap);
1291 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1294 xa->resid = xa->datalen -
1295 le32toh(ccb->ccb_prb->prb_xfer_count);
1300 * Start a command and poll for completion.
1302 * timeout is in ms and only counts once the command gets on-chip.
1304 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1305 * that no error occured.
1307 * NOTE: If the caller specifies a NULL timeout function the caller is
1308 * responsible for clearing hardware state on failure, but we will
1309 * deal with removing the ccb from any pending queue.
1311 * NOTE: NCQ should never be used with this function.
1313 * NOTE: If the port is in a failed state and stopped we do not try
1314 * to activate the ccb.
1317 sili_poll(struct sili_ccb *ccb, int timeout,
1318 void (*timeout_fn)(struct sili_ccb *))
1320 struct sili_port *ap = ccb->ccb_port;
1322 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1323 ccb->ccb_xa.state = ATA_S_ERROR;
1324 return(ccb->ccb_xa.state);
1327 KKASSERT((ap->ap_expired & (1 << ccb->ccb_slot)) == 0);
1331 sili_port_intr(ap, 1);
1332 switch(ccb->ccb_xa.state) {
1334 timeout -= sili_os_softsleep();
1338 * The packet can get stuck on the pending queue
1339 * if the port refuses to come ready. XXX
1342 if (xxx AP_F_EXCLUSIVE_ACCESS)
1343 timeout -= sili_os_softsleep();
1346 sili_os_softsleep();
1347 sili_check_active_timeouts(ap);
1350 return (ccb->ccb_xa.state);
1352 } while (timeout > 0);
1355 * Don't spew if this is a probe during hard reset
1357 if (ap->ap_probe != ATA_PROBE_NEED_HARD_RESET) {
1358 kprintf("%s: Poll timeout slot %d\n",
1359 ATANAME(ap, ccb->ccb_xa.at),
1365 return(ccb->ccb_xa.state);
1369 * When polling we have to check if the currently active CCB(s)
1370 * have timed out as the callout will be deadlocked while we
1371 * hold the port lock.
1374 sili_check_active_timeouts(struct sili_port *ap)
1376 struct sili_ccb *ccb;
1380 mask = ap->ap_active;
1382 tag = ffs(mask) - 1;
1383 mask &= ~(1 << tag);
1384 ccb = &ap->ap_ccbs[tag];
1385 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1386 sili_core_timeout(ccb, 0);
1394 sili_start_timeout(struct sili_ccb *ccb)
1396 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1397 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1398 callout_reset(&ccb->ccb_timeout,
1399 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1400 sili_ata_cmd_timeout_unserialized, ccb);
1405 sili_start(struct sili_ccb *ccb)
1407 struct sili_port *ap = ccb->ccb_port;
1409 struct sili_softc *sc = ap->ap_sc;
1412 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1415 * Sync our SGE table and PRB
1417 bus_dmamap_sync(ap->ap_dmamem_prbs->adm_tag,
1418 ap->ap_dmamem_prbs->adm_map,
1419 BUS_DMASYNC_PREWRITE);
1422 * XXX dmamap for PRB XXX BUS_DMASYNC_PREWRITE
1426 * Controller will update shared memory!
1427 * XXX bus_dmamap_sync ... BUS_DMASYNC_PREREAD ...
1429 /* Prepare RFIS area for write by controller */
1432 * There's no point trying to optimize this, it only shaves a few
1433 * nanoseconds so just queue the command and call our generic issue.
1435 sili_issue_pending_commands(ap, ccb);
1439 * Wait for all commands to complete processing. We hold the lock so no
1440 * new commands will be queued.
1443 sili_exclusive_access(struct sili_port *ap)
1445 while (ap->ap_active) {
1446 sili_port_intr(ap, 1);
1447 sili_os_softsleep();
1452 * If ccb is not NULL enqueue and/or issue it.
1454 * If ccb is NULL issue whatever we can from the queue. However, nothing
1455 * new is issued if the exclusive access flag is set or expired ccb's are
1458 * If existing commands are still active (ap_active) we can only
1459 * issue matching new commands.
1462 sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb)
1467 * If just running the queue and in exclusive access mode we
1468 * just return. Also in this case if there are any expired ccb's
1469 * we want to clear the queue so the port can be safely stopped.
1471 * XXX sili chip - expiration needs to be per-target if PM supports
1475 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1476 } else if (ap->ap_expired) {
1481 * Pull the next ccb off the queue and run it if possible.
1482 * If the port is not ready to accept commands enable the
1483 * ready interrupt instead of starting a new command.
1485 * XXX limit ncqdepth for attached devices behind PM
1487 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1489 * Port may be wedged.
1491 if ((sili_pread(ap, SILI_PREG_STATUS) &
1492 SILI_PREG_STATUS_READY) == 0) {
1493 kprintf("%s: slot %d NOT READY\n",
1494 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
1495 sili_pwrite(ap, SILI_PREG_INT_ENABLE,
1496 SILI_PREG_INT_READY);
1501 * Handle exclusivity requirements. ATA_F_EXCLUSIVE is used
1502 * when we may have to access the rfis which is stored in
1503 * the LRAM PRB. Unfortunately reading the LRAM PRB is
1504 * highly problematic, so requests (like PM requests) which
1505 * need to access the rfis use exclusive mode and then
1506 * access the copy made by the port interrupt code back in
1509 if (ap->ap_active & ~ap->ap_expired) {
1511 * There may be multiple ccb's already running,
1512 * if any are running and ap_run_flags sets
1513 * one of these flags then we know only one is
1516 * XXX Current AUTOSENSE code forces exclusivity
1517 * to simplify the code.
1519 if (ap->ap_run_flags &
1520 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1525 * If the ccb we want to run is exclusive and ccb's
1526 * are still active on the port, we can't queue it
1529 * XXX Current AUTOSENSE code forces exclusivity
1530 * to simplify the code.
1532 if (ccb->ccb_xa.flags &
1533 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1538 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1539 ccb->ccb_xa.state = ATA_S_ONCHIP;
1540 ap->ap_active |= 1 << ccb->ccb_slot;
1541 ap->ap_active_cnt++;
1542 ap->ap_run_flags = ccb->ccb_xa.flags;
1545 * We can't use the CMD_FIFO method because it requires us
1546 * building the PRB in the LRAM, and the LRAM is buggy. So
1547 * we use host memory for the PRB.
1549 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot),
1550 (u_int32_t)ccb->ccb_prb_paddr);
1551 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot) + 4,
1552 (u_int32_t)(ccb->ccb_prb_paddr >> 32));
1553 /* sili_pwrite(ap, SILI_PREG_CMD_FIFO, ccb->ccb_slot); */
1554 sili_start_timeout(ccb);
1559 sili_intr(void *arg)
1561 struct sili_softc *sc = arg;
1562 struct sili_port *ap;
1567 * Check if the master enable is up, and whether any interrupts are
1570 * Clear the ints we got.
1572 if ((sc->sc_flags & SILI_F_INT_GOOD) == 0)
1574 gint = sili_read(sc, SILI_REG_GINT);
1575 if (gint == 0 || gint == 0xffffffff)
1577 sili_write(sc, SILI_REG_GINT, gint);
1580 * Process interrupts for each port in a non-blocking fashion.
1582 while (gint & SILI_REG_GINT_PORTMASK) {
1583 port = ffs(gint) - 1;
1584 ap = sc->sc_ports[port];
1586 if (sili_os_lock_port_nb(ap) == 0) {
1587 sili_port_intr(ap, 0);
1588 sili_os_unlock_port(ap);
1590 sili_port_interrupt_redisable(ap);
1591 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1594 gint &= ~(1 << port);
1599 * Core called from helper thread.
1602 sili_port_thread_core(struct sili_port *ap, int mask)
1605 * Process any expired timedouts.
1607 sili_os_lock_port(ap);
1608 if (mask & AP_SIGF_TIMEOUT) {
1609 sili_check_active_timeouts(ap);
1613 * Process port interrupts which require a higher level of
1616 if (mask & AP_SIGF_PORTINT) {
1617 sili_port_intr(ap, 1);
1618 sili_port_interrupt_reenable(ap);
1620 sili_os_unlock_port(ap);
1624 * Core per-port interrupt handler.
1626 * If blockable is 0 we cannot call sili_os_sleep() at all and we can only
1627 * deal with normal command completions which do not require blocking.
1630 sili_port_intr(struct sili_port *ap, int blockable)
1632 struct sili_softc *sc = ap->ap_sc;
1635 struct sili_ccb *ccb = NULL;
1636 struct ata_port *ccb_at = NULL;
1639 const u_int32_t blockable_mask = SILI_PREG_IST_PHYRDYCHG |
1640 SILI_PREG_IST_DEVEXCHG |
1641 SILI_PREG_IST_CERROR |
1642 SILI_PREG_IST_DECODE |
1644 SILI_PREG_IST_HANDSHK;
1645 const u_int32_t fatal_mask = SILI_PREG_IST_PHYRDYCHG |
1646 SILI_PREG_IST_DEVEXCHG |
1647 SILI_PREG_IST_DECODE |
1649 SILI_PREG_IST_HANDSHK;
1651 enum { NEED_NOTHING, NEED_HOTPLUG_INSERT,
1652 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1655 * NOTE: CCOMPLETE was automatically cleared when we read INT_STATUS.
1657 is = sili_pread(ap, SILI_PREG_INT_STATUS);
1658 is &= SILI_PREG_IST_MASK;
1659 if (is & SILI_PREG_IST_CCOMPLETE)
1660 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CCOMPLETE);
1663 * If we can't block then we can't handle these here. Disable
1664 * the interrupts in question so we don't live-lock, the helper
1665 * thread will re-enable them.
1667 * If the port is in a completely failed state we do not want
1668 * to drop through to failed-command-processing if blockable is 0,
1669 * just let the thread deal with it all.
1671 * Otherwise we fall through and still handle DHRS and any commands
1672 * which completed normally. Even if we are errored we haven't
1673 * stopped the port yet so CI/SACT are still good.
1675 if (blockable == 0) {
1676 if (ap->ap_state == AP_S_FATAL_ERROR) {
1677 sili_port_interrupt_redisable(ap);
1678 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1679 /*is &= ~blockable_mask;*/
1682 if (is & blockable_mask) {
1683 sili_port_interrupt_redisable(ap);
1684 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1685 /*is &= ~blockable_mask;*/
1690 if (is & SILI_PREG_IST_CERROR) {
1692 * Command failed (blockable).
1694 * This stops command processing. We can extract the PM
1695 * target from the PMP field in SILI_PREG_CONTEXT. The
1696 * tag is not necessarily valid so don't use that.
1698 * We must then expire all CCB's for that target and resume
1699 * processing if any other targets have active commands.
1700 * Particular error codes can be recovered by reading the LOG
1703 * The expire handling code will do the rest, which is
1704 * basically to reset the port once the only active
1705 * commands remaining are all expired.
1711 target = (sili_pread(ap, SILI_PREG_CONTEXT) >>
1712 SILI_PREG_CONTEXT_PMPORT_SHIFT) &
1713 SILI_PREG_CONTEXT_PMPORT_MASK;
1714 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CERROR);
1715 active = ap->ap_active & ~ap->ap_expired;
1716 error = sili_pread(ap, SILI_PREG_CERROR);
1717 kprintf("%s.%d target error %d active=%08x hactive=%08x "
1719 PORTNAME(ap), target, error,
1720 active, sili_pread(ap, SILI_PREG_SLOTST),
1721 sili_pread(ap, SILI_PREG_SERR), SILI_PFMT_SERR);
1724 slot = ffs(active) - 1;
1725 ccb = &ap->ap_ccbs[slot];
1726 if ((ccb_at = ccb->ccb_xa.at) == NULL)
1727 ccb_at = &ap->ap_ata[0];
1728 if (target == ccb_at->at_target) {
1729 if ((ccb->ccb_xa.flags & ATA_F_NCQ) &&
1730 (error == SILI_PREG_CERROR_DEVICE ||
1731 error == SILI_PREG_CERROR_SDBERROR)) {
1732 ccb_at->at_features |= ATA_PORT_F_READLOG;
1734 if (sili_core_timeout(ccb, 1) == 0)
1737 active &= ~(1 << slot);
1741 * Resume will be 0 if the timeout reinited and restarted
1742 * the port. Otherwise we resume the port to allow other
1743 * commands to complete.
1746 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESUME);
1750 * Device notification to us (non-blocking)
1752 * This is interrupt status SILIPREG_IST_SDB
1754 * NOTE! On some parts notification bits can get set without
1755 * generating an interrupt. It is unclear whether this is
1756 * a bug in the PM (sending a DTOH device setbits with 'N' set
1757 * and 'I' not set), or a bug in the host controller.
1759 * It only seems to occur under load.
1761 if (sc->sc_flags & SILI_F_SSNTF) {
1765 data = sili_pread(ap, SILI_PREG_SNTF);
1766 if (is & SILI_PREG_IST_SDB) {
1767 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1769 is &= ~SILI_PREG_IST_SDB;
1770 xstr = " (no SDBS!)";
1775 kprintf("%s: NOTIFY %08x%s\n",
1776 PORTNAME(ap), data, xstr);
1777 sili_pwrite(ap, SILI_PREG_SNTF, data);
1778 sili_cam_changed(ap, NULL, -1);
1783 * Port change (hot-plug) (blockable).
1785 * A PCS interrupt will occur on hot-plug once communication is
1788 * A PRCS interrupt will occur on hot-unplug (and possibly also
1791 * XXX We can then check the CPS (Cold Presence State) bit, if
1792 * supported, to determine if a device is plugged in or not and do
1795 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
1796 * can also occur if an unsolicited COMINIT is received.
1797 * If this occurs command processing is automatically
1798 * stopped (CR goes inactive) and the port must be stopped
1801 if (is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG)) {
1803 sili_pwrite(ap, SILI_PREG_SERR,
1804 (SILI_PREG_SERR_DIAG_N | SILI_PREG_SERR_DIAG_X));
1805 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1806 is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG));
1808 is &= ~(SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG);
1809 kprintf("%s: Port change\n", PORTNAME(ap));
1811 switch (sili_pread(ap, SILI_PREG_SSTS) & SILI_PREG_SSTS_DET) {
1812 case SILI_PREG_SSTS_DET_DEV:
1813 if (ap->ap_type == ATA_PORT_T_NONE &&
1814 ap->ap_probe == ATA_PROBE_FAILED) {
1815 need = NEED_HOTPLUG_INSERT;
1820 kprintf("%s: Device lost\n", PORTNAME(ap));
1821 if (ap->ap_type != ATA_PORT_T_NONE) {
1822 need = NEED_HOTPLUG_REMOVE;
1830 * Check for remaining errors - they are fatal. (blockable)
1832 if (is & fatal_mask) {
1835 sili_pwrite(ap, SILI_PREG_INT_STATUS, is & fatal_mask);
1837 serr = sili_pread(ap, SILI_PREG_SERR);
1838 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
1839 "disabling port.\n",
1841 is, SILI_PFMT_INT_STATUS,
1842 serr, SILI_PFMT_SERR
1845 /* XXX try recovery first */
1850 * Fail all outstanding commands if we know the port won't recover.
1852 * We may have a ccb_at if the failed command is known and was
1853 * being sent to a device over a port multiplier (PM). In this
1854 * case if the port itself has not completely failed we fail just
1855 * the commands related to that target.
1857 if (ap->ap_state == AP_S_FATAL_ERROR &&
1858 (ap->ap_active & ~ap->ap_expired)) {
1859 kprintf("%s: Fatal port error, expiring %08x\n",
1860 PORTNAME(ap), ap->ap_active & ~ap->ap_expired);
1862 ap->ap_state = AP_S_FATAL_ERROR;
1865 * Error all the active slots. If running across a PM
1866 * try to error out just the slots related to the target.
1868 active = ap->ap_active & ~ap->ap_expired;
1871 slot = ffs(active) - 1;
1872 active &= ~(1 << slot);
1873 ccb = &ap->ap_ccbs[slot];
1874 sili_core_timeout(ccb, 1);
1879 * CCB completion (non blocking).
1881 * CCB completion is detected by noticing the slot bit in
1882 * the port slot status register has cleared while the bit
1883 * is still set in our ap_active variable.
1885 * When completing expired events we must remember to reinit
1886 * the port once everything is clear.
1888 * Due to a single-level recursion when reading the log page,
1889 * it is possible for the slot to already have been cleared
1890 * for some expired tags, do not include expired tags in
1893 active = ap->ap_active & ~sili_pread(ap, SILI_PREG_SLOTST);
1894 active &= ~ap->ap_expired;
1898 slot = ffs(active) - 1;
1899 ccb = &ap->ap_ccbs[slot];
1901 DPRINTF(SILI_D_INTR, "%s: slot %d is complete%s\n",
1902 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
1905 active &= ~(1 << slot);
1908 * XXX sync POSTREAD for return data?
1910 ap->ap_active &= ~(1 << ccb->ccb_slot);
1911 --ap->ap_active_cnt;
1914 * Complete the ccb. If the ccb was marked expired it
1915 * may or may not have been cleared from the port,
1916 * make sure we mark it as having timed out.
1918 * In a normal completion if AUTOSENSE is set we copy
1919 * the PRB LRAM rfis back to the rfis in host-memory.
1921 * XXX Currently AUTOSENSE also forces exclusivity so we
1922 * can safely work around a hardware bug when reading
1925 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
1926 ap->ap_expired &= ~(1 << ccb->ccb_slot);
1927 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1929 ccb->ccb_xa.complete(&ccb->ccb_xa);
1931 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
1932 ccb->ccb_xa.state = ATA_S_COMPLETE;
1933 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
1934 memcpy(ccb->ccb_xa.rfis,
1935 &ccb->ccb_prb_lram->prb_d2h,
1936 sizeof(ccb->ccb_prb_lram->prb_d2h));
1937 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
1938 ccb->ccb_xa.state = ATA_S_ERROR;
1944 if (is & SILI_PREG_IST_READY) {
1945 is &= ~SILI_PREG_IST_READY;
1946 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_READY);
1947 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_READY);
1951 * If we had expired commands and were waiting for
1952 * remaining commands to complete, and they have now
1953 * completed, we can reinit the port.
1955 * This will also clean out the expired commands.
1956 * The timeout code also calls sili_port_reinit() if
1957 * the only commands remaining after a timeout are all
1958 * now expired commands.
1960 * Otherwise just reissue.
1962 if (ap->ap_expired && ap->ap_active == ap->ap_expired) {
1964 sili_port_reinit(ap);
1966 sili_issue_pending_commands(ap, NULL);
1970 * Cleanup. Will not be set if non-blocking.
1973 case NEED_HOTPLUG_INSERT:
1975 * A hot-plug insertion event has occured and all
1976 * outstanding commands have already been revoked.
1978 * Don't recurse if this occurs while we are
1979 * resetting the port.
1981 * Place the port in a continuous COMRESET state
1982 * until the INIT code gets to it.
1984 kprintf("%s: HOTPLUG - Device inserted\n",
1986 ap->ap_probe = ATA_PROBE_NEED_INIT;
1987 sili_cam_changed(ap, NULL, -1);
1989 case NEED_HOTPLUG_REMOVE:
1991 * A hot-plug removal event has occured and all
1992 * outstanding commands have already been revoked.
1994 * Don't recurse if this occurs while we are
1995 * resetting the port.
1997 kprintf("%s: HOTPLUG - Device removed\n",
1999 sili_port_hardstop(ap);
2000 /* ap_probe set to failed */
2001 sili_cam_changed(ap, NULL, -1);
2009 sili_get_ccb(struct sili_port *ap)
2011 struct sili_ccb *ccb;
2013 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2014 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2016 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2017 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2018 ccb->ccb_xa.state = ATA_S_SETUP;
2019 ccb->ccb_xa.at = NULL;
2021 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2027 sili_put_ccb(struct sili_ccb *ccb)
2029 struct sili_port *ap = ccb->ccb_port;
2031 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2032 ccb->ccb_xa.state = ATA_S_PUT;
2033 ++ccb->ccb_xa.serial;
2034 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2035 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2039 sili_get_err_ccb(struct sili_port *ap)
2041 struct sili_ccb *err_ccb;
2043 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2044 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2047 * Grab a CCB to use for error recovery. This should never fail, as
2048 * we ask atascsi to reserve one for us at init time.
2050 err_ccb = ap->ap_err_ccb;
2051 KKASSERT(err_ccb != NULL);
2052 err_ccb->ccb_xa.flags = 0;
2053 err_ccb->ccb_done = sili_empty_done;
2059 sili_put_err_ccb(struct sili_ccb *ccb)
2061 struct sili_port *ap = ccb->ccb_port;
2063 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2065 KKASSERT(ccb == ap->ap_err_ccb);
2067 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2071 * Read log page to get NCQ error.
2073 * Return 0 on success
2076 sili_port_read_ncq_error(struct sili_port *ap, int target)
2078 struct sili_ccb *ccb;
2079 struct ata_fis_h2d *fis;
2082 DPRINTF(SILI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2084 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2085 ccb = sili_get_err_ccb(ap);
2086 ccb->ccb_done = sili_empty_done;
2087 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2088 ccb->ccb_xa.data = ap->ap_err_scratch;
2089 ccb->ccb_xa.datalen = 512;
2090 ccb->ccb_xa.complete = sili_dummy_done;
2091 ccb->ccb_xa.at = &ap->ap_ata[target];
2092 fis = &ccb->ccb_prb->prb_h2d;
2093 bzero(fis, sizeof(*fis));
2095 fis->type = ATA_FIS_TYPE_H2D;
2096 fis->flags = ATA_H2D_FLAGS_CMD | target;
2097 fis->command = ATA_C_READ_LOG_EXT;
2098 fis->lba_low = 0x10; /* queued error log page (10h) */
2099 fis->sector_count = 1; /* number of sectors (1) */
2100 fis->sector_count_exp = 0;
2101 fis->lba_mid = 0; /* starting offset */
2102 fis->lba_mid_exp = 0;
2106 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
2108 if (sili_load_prb(ccb) != 0) {
2109 status = ATA_S_ERROR;
2111 ccb->ccb_xa.state = ATA_S_PENDING;
2112 status = sili_poll(ccb, 1000, sili_quick_timeout);
2116 * Just spew if it fails, there isn't much we can do at this point.
2118 if (status != ATA_S_COMPLETE) {
2119 kprintf("%s: log page read failed, slot %d was still active.\n",
2120 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
2123 /* Done with the error CCB now. */
2124 sili_unload_prb(ccb);
2125 sili_put_err_ccb(ccb);
2127 /* Extract failed register set and tags from the scratch space. */
2128 if (status == ATA_S_COMPLETE) {
2129 struct ata_log_page_10h *log;
2132 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2133 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2135 * Not queued bit was set - wasn't an NCQ error?
2137 * XXX This bit seems to be set a lot even for NCQ
2142 * Copy back the log record as a D2H register FIS.
2144 err_slot = log->err_regs.type &
2145 ATA_LOG_10H_TYPE_TAG_MASK;
2146 ccb = &ap->ap_ccbs[err_slot];
2147 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2148 kprintf("%s: read NCQ error page slot=%d\n",
2149 ATANAME(ap, ccb->ccb_xa.at), err_slot
2151 memcpy(&ccb->ccb_prb->prb_d2h, &log->err_regs,
2152 sizeof(struct ata_fis_d2h));
2153 ccb->ccb_prb->prb_d2h.type = ATA_FIS_TYPE_D2H;
2154 ccb->ccb_prb->prb_d2h.flags = 0;
2155 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
2156 ccb->ccb_xa.state = ATA_S_ERROR;
2158 kprintf("%s: read NCQ error page slot=%d, "
2159 "slot does not match any cmds\n",
2160 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2169 * Allocate memory for various structures DMAd by hardware. The maximum
2170 * number of segments for these tags is 1 so the DMA memory will have a
2171 * single physical base address.
2173 struct sili_dmamem *
2174 sili_dmamem_alloc(struct sili_softc *sc, bus_dma_tag_t tag)
2176 struct sili_dmamem *adm;
2179 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2181 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2182 BUS_DMA_ZERO, &adm->adm_map);
2185 error = bus_dmamap_load(tag, adm->adm_map,
2187 bus_dma_tag_getmaxsize(tag),
2188 sili_dmamem_saveseg, &adm->adm_busaddr,
2193 bus_dmamap_destroy(tag, adm->adm_map);
2194 adm->adm_map = NULL;
2195 adm->adm_tag = NULL;
2196 adm->adm_kva = NULL;
2198 kfree(adm, M_DEVBUF);
2206 sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2208 KKASSERT(error == 0);
2209 KKASSERT(nsegs == 1);
2210 *(bus_addr_t *)info = segs->ds_addr;
2215 sili_dmamem_free(struct sili_softc *sc, struct sili_dmamem *adm)
2218 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2219 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2220 adm->adm_map = NULL;
2221 adm->adm_tag = NULL;
2222 adm->adm_kva = NULL;
2224 kfree(adm, M_DEVBUF);
2228 sili_read(struct sili_softc *sc, bus_size_t r)
2230 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2231 BUS_SPACE_BARRIER_READ);
2232 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2236 sili_write(struct sili_softc *sc, bus_size_t r, u_int32_t v)
2238 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2239 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2240 BUS_SPACE_BARRIER_WRITE);
2244 sili_pread(struct sili_port *ap, bus_size_t r)
2246 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2247 BUS_SPACE_BARRIER_READ);
2248 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2252 sili_pwrite(struct sili_port *ap, bus_size_t r, u_int32_t v)
2254 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2255 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2256 BUS_SPACE_BARRIER_WRITE);
2260 * Wait up to (timeout) milliseconds for the masked port register to
2263 * Timeout is in milliseconds.
2266 sili_pwait_eq(struct sili_port *ap, int timeout,
2267 bus_size_t r, u_int32_t mask, u_int32_t target)
2272 * Loop hard up to 100uS
2274 for (t = 0; t < 100; ++t) {
2275 if ((sili_pread(ap, r) & mask) == target)
2277 sili_os_hardsleep(1); /* us */
2281 timeout -= sili_os_softsleep();
2282 if ((sili_pread(ap, r) & mask) == target)
2284 } while (timeout > 0);
2289 sili_wait_ne(struct sili_softc *sc, bus_size_t r, u_int32_t mask,
2295 * Loop hard up to 100uS
2297 for (t = 0; t < 100; ++t) {
2298 if ((sili_read(sc, r) & mask) != target)
2300 sili_os_hardsleep(1); /* us */
2304 * And one millisecond the slow way
2308 t -= sili_os_softsleep();
2309 if ((sili_read(sc, r) & mask) != target)
2318 * Acquire an ata transfer.
2320 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2321 * targets that go through the port multiplier.
2324 sili_ata_get_xfer(struct sili_port *ap, struct ata_port *at)
2326 struct sili_ccb *ccb;
2328 ccb = sili_get_ccb(ap);
2330 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer: NULL ccb\n",
2335 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer got slot %d\n",
2336 PORTNAME(ap), ccb->ccb_slot);
2338 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
2339 ccb->ccb_xa.at = at;
2340 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2342 return (&ccb->ccb_xa);
2346 sili_ata_put_xfer(struct ata_xfer *xa)
2348 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2350 DPRINTF(SILI_D_XFER, "sili_ata_put_xfer slot %d\n", ccb->ccb_slot);
2356 sili_ata_cmd(struct ata_xfer *xa)
2358 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2360 KKASSERT(xa->state == ATA_S_SETUP);
2362 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2365 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2366 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2367 sili_pread(ccb->ccb_port, SILI_PREG_CMD), SILI_PFMT_CMD,
2370 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2373 ccb->ccb_done = sili_ata_cmd_done;
2375 if (sili_load_prb(ccb) != 0)
2378 xa->state = ATA_S_PENDING;
2380 if (xa->flags & ATA_F_POLL)
2381 return (sili_poll(ccb, xa->timeout, sili_ata_cmd_timeout));
2384 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
2385 xa->flags |= ATA_F_TIMEOUT_DESIRED;
2392 xa->state = ATA_S_ERROR;
2395 return (ATA_S_ERROR);
2399 sili_ata_cmd_done(struct sili_ccb *ccb)
2401 struct ata_xfer *xa = &ccb->ccb_xa;
2405 * NOTE: callout does not lock port and may race us modifying
2406 * the flags, so make sure its stopped.
2408 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2409 serial = ccb->ccb_xa.serial;
2410 callout_stop_sync(&ccb->ccb_timeout);
2411 if (serial != ccb->ccb_xa.serial) {
2412 kprintf("%s: Warning: timeout race ccb %p\n",
2413 PORTNAME(ccb->ccb_port), ccb);
2416 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2418 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
2420 KKASSERT(xa->state != ATA_S_ONCHIP);
2421 sili_unload_prb(ccb);
2423 if (xa->state != ATA_S_TIMEOUT)
2428 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
2429 * while the callout is runing.
2431 * We can't safely get the port lock here or delay, we could block
2432 * the callout thread.
2435 sili_ata_cmd_timeout_unserialized(void *arg)
2437 struct sili_ccb *ccb = arg;
2438 struct sili_port *ap = ccb->ccb_port;
2440 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
2441 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
2442 sili_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
2446 sili_ata_cmd_timeout(struct sili_ccb *ccb)
2448 sili_core_timeout(ccb, 0);
2452 * Timeout code, typically called when the port command processor is running.
2454 * Returns 0 if all timeout processing completed, non-zero if it is still
2459 sili_core_timeout(struct sili_ccb *ccb, int really_error)
2461 struct ata_xfer *xa = &ccb->ccb_xa;
2462 struct sili_port *ap = ccb->ccb_port;
2463 struct ata_port *at;
2465 at = ccb->ccb_xa.at;
2467 kprintf("%s: CMD %s state=%d slot=%d\n"
2472 (really_error ? "ERROR" : "TIMEOUT"),
2473 ccb->ccb_xa.state, ccb->ccb_slot,
2476 sili_pread(ap, SILI_PREG_SLOTST)
2480 * NOTE: Timeout will not be running if the command was polled.
2481 * If we got here at least one of these flags should be set.
2483 * However, it might be running if we are called from the
2484 * interrupt error handling code.
2486 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2487 ATA_F_TIMEOUT_RUNNING));
2488 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2489 callout_stop(&ccb->ccb_timeout);
2490 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2492 xa->flags &= ~ATA_F_TIMEOUT_EXPIRED;
2494 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2495 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2496 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2499 sili_issue_pending_commands(ap, NULL);
2502 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2503 kprintf("%s: Unexpected state during timeout: %d\n",
2504 ATANAME(ap, at), ccb->ccb_xa.state);
2509 * We can't process timeouts while other commands are running.
2511 ap->ap_expired |= 1 << ccb->ccb_slot;
2513 if (ap->ap_active != ap->ap_expired) {
2514 kprintf("%s: Deferred timeout until its safe, slot %d\n",
2515 ATANAME(ap, at), ccb->ccb_slot);
2520 * We have to issue a Port reinit. We don't read an error log
2521 * page for timeouts. Reiniting the port will clear all pending
2524 sili_port_reinit(ap);
2529 * Used by the softreset, pm_port_probe, and read_ncq_error only, in very
2530 * specialized, controlled circumstances.
2533 sili_quick_timeout(struct sili_ccb *ccb)
2535 struct sili_port *ap = ccb->ccb_port;
2537 switch (ccb->ccb_xa.state) {
2539 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2540 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2543 KKASSERT((ap->ap_active & ~ap->ap_expired) ==
2544 (1 << ccb->ccb_slot));
2545 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2546 ap->ap_active &= ~(1 << ccb->ccb_slot);
2547 KKASSERT(ap->ap_active_cnt > 0);
2548 --ap->ap_active_cnt;
2549 sili_port_reinit(ap);
2552 panic("%s: sili_quick_timeout: ccb in bad state %d",
2553 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
2558 sili_dummy_done(struct ata_xfer *xa)
2563 sili_empty_done(struct sili_ccb *ccb)