2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 * $OpenBSD: sili.c,v 1.147 2009/02/16 21:19:07 miod Exp $
56 void sili_port_interrupt_enable(struct sili_port *ap);
57 void sili_port_interrupt_redisable(struct sili_port *ap);
58 void sili_port_interrupt_reenable(struct sili_port *ap);
60 int sili_load_prb(struct sili_ccb *);
61 void sili_unload_prb(struct sili_ccb *);
62 static void sili_load_prb_callback(void *info, bus_dma_segment_t *segs,
63 int nsegs, int error);
64 void sili_start(struct sili_ccb *);
65 static void sili_port_reinit(struct sili_port *ap);
66 int sili_port_softreset(struct sili_port *ap);
67 int sili_port_hardreset(struct sili_port *ap);
68 void sili_port_hardstop(struct sili_port *ap);
69 void sili_port_listen(struct sili_port *ap);
71 static void sili_ata_cmd_timeout_unserialized(void *);
72 static int sili_core_timeout(struct sili_ccb *ccb, int really_error);
73 void sili_check_active_timeouts(struct sili_port *ap);
75 void sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb);
77 void sili_port_read_ncq_error(struct sili_port *, int);
79 struct sili_dmamem *sili_dmamem_alloc(struct sili_softc *, bus_dma_tag_t tag);
80 void sili_dmamem_free(struct sili_softc *, struct sili_dmamem *);
81 static void sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
83 static void sili_dummy_done(struct ata_xfer *xa);
84 static void sili_empty_done(struct sili_ccb *ccb);
85 static void sili_ata_cmd_done(struct sili_ccb *ccb);
88 * Initialize the global SILI hardware. This code does not set up any of
92 sili_init(struct sili_softc *sc)
94 DPRINTF(SILI_D_VERBOSE, " GHC 0x%b",
95 sili_read(sc, SILI_REG_GHC), SILI_FMT_GHC);
98 * Reset the entire chip. This also resets all ports.
100 * The spec doesn't say anything about how long we have to
101 * wait, so wait 10ms.
103 sili_write(sc, SILI_REG_GCTL, SILI_REG_GCTL_GRESET);
105 sili_write(sc, SILI_REG_GCTL, 0);
112 * Allocate and initialize an SILI port.
115 sili_port_alloc(struct sili_softc *sc, u_int port)
117 struct sili_port *ap;
119 struct sili_prb *prb;
120 struct sili_ccb *ccb;
125 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
126 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
128 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
129 device_get_name(sc->sc_dev),
130 device_get_unit(sc->sc_dev),
132 sc->sc_ports[port] = ap;
135 * Allocate enough so we never have to reallocate, it makes
138 * ap_pmcount will be reduced by the scan if we encounter the
139 * port multiplier port prior to target 15.
141 if (ap->ap_ata == NULL) {
142 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * SILI_MAX_PMPORTS,
143 M_DEVBUF, M_INTWAIT | M_ZERO);
144 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
146 at->at_sili_port = ap;
148 at->at_probe = ATA_PROBE_NEED_INIT;
149 at->at_features |= ATA_PORT_F_RESCAN;
150 ksnprintf(at->at_name, sizeof(at->at_name),
151 "%s.%d", ap->ap_name, i);
154 if (bus_space_subregion(sc->sc_piot, sc->sc_pioh,
155 SILI_PORT_REGION(port), SILI_PORT_SIZE,
157 device_printf(sc->sc_dev,
158 "unable to create register window for port %d\n",
165 ap->ap_probe = ATA_PROBE_NEED_INIT;
166 TAILQ_INIT(&ap->ap_ccb_free);
167 TAILQ_INIT(&ap->ap_ccb_pending);
168 lockinit(&ap->ap_ccb_lock, "silipo", 0, 0);
170 /* Disable port interrupts */
171 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
174 * Reset the port. This is similar to a Device Reset but far
175 * more invasive. We use Device Reset in our hardreset function.
176 * This function also does the same OOB initialization sequence
177 * that Device Reset does.
179 * NOTE: SILI_PREG_STATUS_READY will not be asserted unless and until
180 * a device is connected to the port, so we can't use it to
181 * verify that the port exists.
183 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
184 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
185 device_printf(sc->sc_dev,
186 "Port %d will not go into reset\n", port);
190 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
193 * Allocate the SGE Table
195 ap->ap_dmamem_prbs = sili_dmamem_alloc(sc, sc->sc_tag_prbs);
196 if (ap->ap_dmamem_prbs == NULL) {
197 kprintf("%s: NOSGET\n", PORTNAME(ap));
202 * Set up the SGE table base address
204 ap->ap_prbs = (struct sili_prb *)SILI_DMA_KVA(ap->ap_dmamem_prbs);
207 * Allocate a CCB for each command slot
209 ap->ap_ccbs = kmalloc(sizeof(struct sili_ccb) * sc->sc_ncmds, M_DEVBUF,
211 if (ap->ap_ccbs == NULL) {
212 device_printf(sc->sc_dev,
213 "unable to allocate command list for port %d\n",
219 * Most structures are in the port BAR. Assign convenient
220 * pointers in the CCBs
222 for (i = 0; i < sc->sc_ncmds; i++) {
223 ccb = &ap->ap_ccbs[i];
225 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
228 device_printf(sc->sc_dev,
229 "unable to create dmamap for port %d "
230 "ccb %d\n", port, i);
235 * WARNING!!! Access to the rfis is only allowed under very
236 * carefully controlled circumstances because it
237 * is located in the LRAM and reading from the
238 * LRAM has hardware issues which can blow the
239 * port up. I kid you not (from Linux, and
240 * verified by testing here).
242 callout_init(&ccb->ccb_timeout);
245 ccb->ccb_prb = &ap->ap_prbs[i];
246 ccb->ccb_prb_paddr = SILI_DMA_DVA(ap->ap_dmamem_prbs) +
247 sizeof(*ccb->ccb_prb) * i;
248 ccb->ccb_xa.fis = &ccb->ccb_prb->prb_h2d;
249 prb = bus_space_kva(ap->ap_sc->sc_iot, ap->ap_ioh,
250 SILI_PREG_LRAM_SLOT(i));
251 ccb->ccb_prb_lram = prb;
253 * Point our rfis to host-memory instead of the LRAM PRB.
254 * It will be copied back if ATA_F_AUTOSENSE is set. The
257 /*ccb->ccb_xa.rfis = &prb->prb_d2h;*/
258 ccb->ccb_xa.rfis = (void *)ccb->ccb_xa.fis;
260 ccb->ccb_xa.packetcmd = prb_packet(ccb->ccb_prb);
263 ccb->ccb_xa.state = ATA_S_COMPLETE;
266 * Reserve CCB[1] as the error CCB. It doesn't matter
267 * which one we use for the Sili controllers.
270 ap->ap_err_ccb = ccb;
275 * Do not call sili_port_init() here, the helper thread will
276 * call it for the parallel probe
278 sili_os_start_port(ap);
281 sili_port_free(sc, port);
286 * This is called once by the low level attach (from the helper thread)
287 * to get the port state machine rolling, and typically only called again
288 * on a hot-plug insertion event.
290 * This is called for PM attachments and hot-plug insertion events, and
291 * typically not called again until after an unplug/replug sequence.
293 * Returns 0 if a device is successfully detected.
296 sili_port_init(struct sili_port *ap)
299 * Do a very hard reset of the port
301 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
303 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
306 * Register initialization
308 sili_pwrite(ap, SILI_PREG_FIFO_CTL,
309 SILI_PREG_FIFO_CTL_ENCODE(1024, 1024));
310 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_32BITDMA |
312 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_NOAUTOCC);
313 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
314 sili_pwrite(ap, SILI_PREG_SNTF, -1);
315 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
317 sili_port_interrupt_enable(ap);
322 * Handle an errored port. This routine is called when the only
323 * commands left on the queue are expired, meaning we can safely
324 * go through a port init to clear its state.
326 * We complete the expired CCBs and then restart the queue.
330 sili_port_reinit(struct sili_port *ap)
332 struct sili_ccb *ccb;
338 if (bootverbose || 1) {
339 kprintf("%s: reiniting port after error reent=%d "
342 (ap->ap_flags & AP_F_REINIT_ACTIVE),
347 * Clear port resume, clear bits 16:13 in the port device status
348 * register. This is from the data sheet.
350 * Data sheet does not specify a delay but it seems prudent.
352 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
354 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
355 data = sili_pread(ap, SILI_PREG_PM_STATUS(target));
356 data &= ~(SILI_PREG_PM_STATUS_SERVICE |
357 SILI_PREG_PM_STATUS_LEGACY |
358 SILI_PREG_PM_STATUS_NATIVE |
359 SILI_PREG_PM_STATUS_VBSY);
360 sili_pwrite(ap, SILI_PREG_PM_STATUS(target), data);
361 sili_pwrite(ap, SILI_PREG_PM_QACTIVE(target), 0);
365 * Issue a Port Initialize and wait for it to clear. This flushes
366 * commands but does not reset the port. Then wait for port ready.
368 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
369 if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
370 kprintf("%s: Unable to reinit, port failed\n",
373 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
374 kprintf("%s: Unable to reinit, port will not come ready\n",
379 * If reentrant, stop here. Otherwise the state for the original
380 * ahci_port_reinit() will get ripped out from under it.
382 if (ap->ap_flags & AP_F_REINIT_ACTIVE)
384 ap->ap_flags |= AP_F_REINIT_ACTIVE;
387 * Read the LOG ERROR page for targets that returned a specific
388 * D2H FIS with ERR set.
390 * Don't bother if we are already using the error CCB.
392 if ((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0) {
393 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
394 at = &ap->ap_ata[target];
395 if (at->at_features & ATA_PORT_F_READLOG) {
396 at->at_features &= ~ATA_PORT_F_READLOG;
397 sili_port_read_ncq_error(ap, target);
403 * Finally clean out the expired commands, we've probed the error
404 * status (or hopefully probed the error status). Well, ok,
405 * we probably didn't XXX.
407 while (ap->ap_expired) {
408 slot = ffs(ap->ap_expired) - 1;
409 ap->ap_expired &= ~(1 << slot);
410 KKASSERT(ap->ap_active & (1 << slot));
411 ap->ap_active &= ~(1 << slot);
413 ccb = &ap->ap_ccbs[slot];
414 ccb->ccb_xa.state = ATA_S_TIMEOUT;
416 ccb->ccb_xa.complete(&ccb->ccb_xa);
418 ap->ap_flags &= ~AP_F_REINIT_ACTIVE;
421 * Wow. All done. We can get the port moving again.
423 if (ap->ap_probe == ATA_PROBE_FAILED) {
424 kprintf("%s: reinit failed, port is dead\n", PORTNAME(ap));
425 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
426 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
427 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
428 ccb->ccb_xa.state = ATA_S_TIMEOUT;
430 ccb->ccb_xa.complete(&ccb->ccb_xa);
433 sili_issue_pending_commands(ap, NULL);
438 * Enable or re-enable interrupts on a port.
440 * This routine is called from the port initialization code or from the
441 * helper thread as the real interrupt may be forced to turn off certain
445 sili_port_interrupt_enable(struct sili_port *ap)
449 data = SILI_PREG_INT_CCOMPLETE | SILI_PREG_INT_CERROR |
450 SILI_PREG_INT_PHYRDYCHG | SILI_PREG_INT_DEVEXCHG |
451 SILI_PREG_INT_DECODE | SILI_PREG_INT_CRC |
452 SILI_PREG_INT_HANDSHK | SILI_PREG_INT_PMCHANGE;
453 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
454 data |= SILI_PREG_INT_SDB;
455 sili_pwrite(ap, SILI_PREG_INT_ENABLE, data);
459 sili_port_interrupt_redisable(struct sili_port *ap)
463 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
464 data &= SILI_REG_GINT_PORTMASK;
465 data &= ~(1 << ap->ap_num);
466 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
470 sili_port_interrupt_reenable(struct sili_port *ap)
474 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
475 data &= SILI_REG_GINT_PORTMASK;
476 data |= (1 << ap->ap_num);
477 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
481 * Run the port / target state machine from a main context.
483 * The state machine for the port is always run.
485 * If atx is non-NULL run the state machine for a particular target.
486 * If atx is NULL run the state machine for all targets.
489 sili_port_state_machine(struct sili_port *ap, int initial)
498 * State machine for port. Note that CAM is not yet associated
499 * during the initial parallel probe and the port's probe state
500 * will not get past ATA_PROBE_NEED_IDENT.
503 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
504 kprintf("%s: Waiting 7 seconds on insertion\n",
509 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
511 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
512 sili_port_reset(ap, NULL, 1);
513 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
514 sili_port_reset(ap, NULL, 0);
515 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
516 sili_cam_probe(ap, NULL);
518 if (ap->ap_type != ATA_PORT_T_PM) {
519 if (ap->ap_probe == ATA_PROBE_FAILED) {
520 sili_cam_changed(ap, NULL, 0);
521 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
522 sili_cam_changed(ap, NULL, 1);
528 * Port Multiplier state machine.
530 * Get a mask of changed targets and combine with any runnable
531 * states already present.
533 for (loop = 0; ;++loop) {
534 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
535 kprintf("%s: PM unable to read hot-plug bitmap\n",
541 * Do at least one loop, then stop if no more state changes
542 * have occured. The PM might not generate a new
543 * notification until we clear the entire bitmap.
545 if (loop && data == 0)
549 * New devices showing up in the bitmap require some spin-up
550 * time before we start probing them. Reset didsleep. The
551 * first new device we detect will sleep before probing.
553 * This only applies to devices whos change bit is set in
554 * the data, and does not apply to the initial boot-time
559 for (target = 0; target < ap->ap_pmcount; ++target) {
560 at = &ap->ap_ata[target];
563 * Check the target state for targets behind the PM
564 * which have changed state. This will adjust
565 * at_probe and set ATA_PORT_F_RESCAN
567 * We want to wait at least 10 seconds before probing
568 * a newly inserted device. If the check status
569 * indicates a device is present and in need of a
570 * hard reset, we make sure we have slept before
573 * We also need to wait at least 1 second for the
574 * PHY state to change after insertion, if we
575 * haven't already waited the 10 seconds.
577 * NOTE: When pm_check_good finds a good port it
578 * typically starts us in probe state
579 * NEED_HARD_RESET rather than INIT.
581 if (data & (1 << target)) {
582 if (initial == 0 && didsleep == 0)
584 sili_pm_check_good(ap, target);
585 if (initial == 0 && didsleep == 0 &&
586 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
589 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
590 sili_os_sleep(10000);
595 * Report hot-plug events before the probe state
596 * really gets hot. Only actual events are reported
597 * here to reduce spew.
599 if (data & (1 << target)) {
600 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
601 switch(at->at_probe) {
602 case ATA_PROBE_NEED_INIT:
603 case ATA_PROBE_NEED_HARD_RESET:
604 kprintf("Device inserted\n");
606 case ATA_PROBE_FAILED:
607 kprintf("Device removed\n");
610 kprintf("Device probe in progress\n");
616 * Run through the state machine as necessary if
617 * the port is not marked failed.
619 * The state machine may stop at NEED_IDENT if
620 * CAM is not yet attached.
622 * Acquire exclusive access to the port while we
623 * are doing this. This prevents command-completion
624 * from queueing commands for non-polled targets
625 * inbetween our probe steps. We need to do this
626 * because the reset probes can generate severe PHY
627 * and protocol errors and soft-brick the port.
629 if (at->at_probe != ATA_PROBE_FAILED &&
630 at->at_probe != ATA_PROBE_GOOD) {
631 if (at->at_probe == ATA_PROBE_NEED_INIT)
632 sili_pm_port_init(ap, at);
633 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
634 sili_port_reset(ap, at, 1);
635 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
636 sili_port_reset(ap, at, 0);
637 if (at->at_probe == ATA_PROBE_NEED_IDENT)
638 sili_cam_probe(ap, at);
642 * Add or remove from CAM
644 if (at->at_features & ATA_PORT_F_RESCAN) {
645 at->at_features &= ~ATA_PORT_F_RESCAN;
646 if (at->at_probe == ATA_PROBE_FAILED) {
647 sili_cam_changed(ap, at, 0);
648 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
649 sili_cam_changed(ap, at, 1);
652 data &= ~(1 << target);
655 kprintf("%s: WARNING (PM): extra bits set in "
656 "EINFO: %08x\n", PORTNAME(ap), data);
657 while (target < SILI_MAX_PMPORTS) {
658 sili_pm_check_good(ap, target);
666 * De-initialize and detach a port.
669 sili_port_free(struct sili_softc *sc, u_int port)
671 struct sili_port *ap = sc->sc_ports[port];
672 struct sili_ccb *ccb;
675 * Ensure port is disabled and its interrupts are all flushed.
678 sili_os_stop_port(ap);
679 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
680 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
681 sili_write(ap->ap_sc, SILI_REG_GCTL,
682 sili_read(ap->ap_sc, SILI_REG_GCTL) &
683 ~SILI_REG_GINT_PORTST(ap->ap_num));
687 while ((ccb = sili_get_ccb(ap)) != NULL) {
688 if (ccb->ccb_dmamap) {
689 bus_dmamap_destroy(sc->sc_tag_data,
691 ccb->ccb_dmamap = NULL;
694 if ((ccb = ap->ap_err_ccb) != NULL) {
695 if (ccb->ccb_dmamap) {
696 bus_dmamap_destroy(sc->sc_tag_data,
698 ccb->ccb_dmamap = NULL;
700 ap->ap_err_ccb = NULL;
702 kfree(ap->ap_ccbs, M_DEVBUF);
706 if (ap->ap_dmamem_prbs) {
707 sili_dmamem_free(sc, ap->ap_dmamem_prbs);
708 ap->ap_dmamem_prbs = NULL;
711 kfree(ap->ap_ata, M_DEVBUF);
714 if (ap->ap_err_scratch) {
715 kfree(ap->ap_err_scratch, M_DEVBUF);
716 ap->ap_err_scratch = NULL;
719 /* bus_space(9) says we dont free the subregions handle */
722 sc->sc_ports[port] = NULL;
728 * If hard is 0 perform a softreset of the port.
729 * If hard is 1 perform a hard reset of the port.
730 * If hard is 2 perform a hard reset of the port and cycle the phy.
732 * If at is non-NULL an indirect port via a port-multiplier is being
733 * reset, otherwise a direct port is being reset.
735 * NOTE: Indirect ports can only be soft-reset.
738 sili_port_reset(struct sili_port *ap, struct ata_port *at, int hard)
744 rc = sili_pm_hardreset(ap, at->at_target, hard);
746 rc = sili_port_hardreset(ap);
749 rc = sili_pm_softreset(ap, at->at_target);
751 rc = sili_port_softreset(ap);
757 * SILI soft reset, Section 10.4.1
759 * (at) will be NULL when soft-resetting a directly-attached device, and
760 * non-NULL when soft-resetting a device through a port multiplier.
762 * This function keeps port communications intact and attempts to generate
763 * a reset to the connected device using device commands.
766 sili_port_softreset(struct sili_port *ap)
768 struct sili_ccb *ccb = NULL;
769 struct sili_prb *prb;
776 kprintf("%s: START SOFTRESET\n", PORTNAME(ap));
779 ap->ap_state = AP_S_NORMAL;
782 * Prep the special soft-reset SII command.
784 ccb = sili_get_err_ccb(ap);
785 ccb->ccb_done = sili_empty_done;
786 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
787 ccb->ccb_xa.complete = sili_dummy_done;
788 ccb->ccb_xa.at = NULL;
791 bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
792 prb->prb_h2d.flags = 0;
793 prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
794 prb->prb_override = 0;
795 prb->prb_xfer_count = 0;
797 ccb->ccb_xa.state = ATA_S_PENDING;
800 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
802 if (sili_poll(ccb, 8000, sili_quick_timeout) != ATA_S_COMPLETE) {
803 kprintf("%s: First FIS failed\n", PORTNAME(ap));
807 sig = (prb->prb_d2h.lba_high << 24) |
808 (prb->prb_d2h.lba_mid << 16) |
809 (prb->prb_d2h.lba_low << 8) |
810 (prb->prb_d2h.sector_count);
812 kprintf("%s: SOFTRESET SIGNATURE %08x\n", PORTNAME(ap), sig);
815 * If the softreset is trying to clear a BSY condition after a
816 * normal portreset we assign the port type.
818 * If the softreset is being run first as part of the ccb error
819 * processing code then report if the device signature changed
822 if (ap->ap_type == ATA_PORT_T_NONE) {
823 ap->ap_type = sili_port_signature(ap, NULL, sig);
825 if (sili_port_signature(ap, NULL, sig) != ap->ap_type) {
826 kprintf("%s: device signature unexpectedly "
827 "changed\n", PORTNAME(ap));
828 error = EBUSY; /* XXX */
834 sili_put_err_ccb(ccb);
838 * If we failed to softreset make the port quiescent, otherwise
839 * make sure the port's start/stop state matches what it was on
842 * Don't kill the port if the softreset is on a port multiplier
843 * target, that would kill all the targets!
846 kprintf("%s: END SOFTRESET %d prob=%d state=%d\n",
847 PORTNAME(ap), error, ap->ap_probe, ap->ap_state);
850 sili_port_hardstop(ap);
851 /* ap_probe set to failed */
853 ap->ap_probe = ATA_PROBE_NEED_IDENT;
858 sili_pwrite(ap, SILI_PREG_SERR, -1);
860 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
866 * This function does a hard reset of the port. Note that the device
867 * connected to the port could still end-up hung. Phy detection is
868 * used to short-cut longer operations.
871 sili_port_hardreset(struct sili_port *ap)
878 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
880 ap->ap_state = AP_S_NORMAL;
883 * Set SCTL up for any speed restrictions before issuing the
884 * device reset. This may also take us out of an INIT state
885 * (if we were previously in a continuous reset state from
886 * sili_port_listen()).
888 data = SILI_PREG_SCTL_SPM_NONE |
889 SILI_PREG_SCTL_IPM_NONE |
890 SILI_PREG_SCTL_SPD_NONE |
891 SILI_PREG_SCTL_DET_NONE;
892 if (SiliForceGen1 & (1 << ap->ap_num)) {
893 data &= ~SILI_PREG_SCTL_SPD_NONE;
894 data |= SILI_PREG_SCTL_SPD_GEN1;
896 sili_pwrite(ap, SILI_PREG_SCTL, data);
899 * The transition from a continuous COMRESET state from
900 * sili_port_listen() back to device detect can take a
901 * few seconds. It's quite non-deterministic. Most of
902 * the time it takes far less. Use a polling loop to
907 data = sili_pread(ap, SILI_PREG_SSTS);
908 if (data & SILI_PREG_SSTS_DET)
910 loop -= sili_os_softsleep();
915 * Issue Device Reset, give the phy a little time to settle down.
917 * NOTE: Unlike Port Reset, the port ready signal will not
918 * go active unless a device is established to be on
921 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
922 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
923 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
924 if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
925 kprintf("%s: hardreset failed to clear\n", PORTNAME(ap));
930 * Try to determine if there is a device on the port.
932 * Give the device 3/10 second to at least be detected.
936 data = sili_pread(ap, SILI_PREG_SSTS);
937 if (data & SILI_PREG_SSTS_DET)
939 loop -= sili_os_softsleep();
943 kprintf("%s: Port appears to be unplugged\n",
951 * There is something on the port. Give the device 3 seconds
954 if (sili_pwait_eq(ap, 3000, SILI_PREG_SSTS,
955 SILI_PREG_SSTS_DET, SILI_PREG_SSTS_DET_DEV)) {
957 kprintf("%s: Device may be powered down\n",
965 * We got something that definitely looks like a device. Give
966 * the device time to send us its first D2H FIS.
968 * This effectively waits for BSY to clear.
970 if (sili_pwait_set_to(ap, 3000, SILI_PREG_STATUS,
971 SILI_PREG_STATUS_READY)) {
979 * Do the PM port probe regardless of how things turned out above.
981 * If the PM port probe fails it will return the original error
984 if (ap->ap_sc->sc_flags & SILI_F_SPM) {
985 error = sili_pm_port_probe(ap, error);
994 if (ap->ap_type == ATA_PORT_T_PM)
995 ap->ap_probe = ATA_PROBE_GOOD;
997 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1001 * No device detected.
1003 data = sili_pread(ap, SILI_PREG_SSTS);
1005 switch(data & SATA_PM_SSTS_DET) {
1006 case SILI_PREG_SSTS_DET_DEV_NE:
1007 kprintf("%s: Device not communicating\n",
1010 case SILI_PREG_SSTS_DET_OFFLINE:
1011 kprintf("%s: PHY offline\n",
1015 kprintf("%s: No device detected\n",
1019 sili_port_hardstop(ap);
1025 kprintf("%s: Device on port is bricked\n",
1027 sili_port_hardstop(ap);
1030 sili_pwrite(ap, SILI_PREG_SERR, -1);
1033 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1038 * Hard-stop on hot-swap device removal. See 10.10.1
1040 * Place the port in a mode that will allow it to detect hot-swap insertions.
1041 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1042 * seem to do the job.
1045 sili_port_hardstop(struct sili_port *ap)
1047 struct sili_ccb *ccb;
1048 struct ata_port *at;
1052 ap->ap_state = AP_S_FATAL_ERROR;
1053 ap->ap_probe = ATA_PROBE_FAILED;
1054 ap->ap_type = ATA_PORT_T_NONE;
1057 * Clean up AT sub-ports on SATA port.
1059 for (i = 0; ap->ap_ata && i < SILI_MAX_PMPORTS; ++i) {
1060 at = &ap->ap_ata[i];
1061 at->at_type = ATA_PORT_T_NONE;
1062 at->at_probe = ATA_PROBE_FAILED;
1063 at->at_features &= ~ATA_PORT_F_READLOG;
1067 * Kill the port. Don't bother waiting for it to transition
1070 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
1071 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
1072 kprintf("%s: Port will not go into reset\n",
1076 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
1079 * Turn off port-multiplier control bit
1081 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1084 * Clean up the command list.
1086 while (ap->ap_active) {
1087 slot = ffs(ap->ap_active) - 1;
1088 ap->ap_active &= ~(1 << slot);
1089 ap->ap_expired &= ~(1 << slot);
1090 --ap->ap_active_cnt;
1091 ccb = &ap->ap_ccbs[slot];
1092 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1093 callout_stop(&ccb->ccb_timeout);
1094 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1096 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1097 ATA_F_TIMEOUT_EXPIRED);
1098 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1100 ccb->ccb_xa.complete(&ccb->ccb_xa);
1102 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1103 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1104 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1105 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1107 ccb->ccb_xa.complete(&ccb->ccb_xa);
1109 KKASSERT(ap->ap_active_cnt == 0);
1112 * Put the port into a listen mode, we want to get insertion/removal
1115 sili_port_listen(ap);
1119 * Place port into a listen mode for hotplug events only. The port has
1120 * already been reset and the command processor may not be ready due
1121 * to the lack of a device.
1124 sili_port_listen(struct sili_port *ap)
1129 data = SILI_PREG_SCTL_SPM_NONE |
1130 SILI_PREG_SCTL_IPM_NONE |
1131 SILI_PREG_SCTL_SPD_NONE |
1132 SILI_PREG_SCTL_DET_INIT;
1133 if (SiliForceGen1 & (1 << ap->ap_num)) {
1134 data &= ~SILI_PREG_SCTL_SPD_NONE;
1135 data |= SILI_PREG_SCTL_SPD_GEN1;
1139 sili_pwrite(ap, SILI_PREG_SERR, -1);
1140 sili_pwrite(ap, SILI_PREG_INT_ENABLE, SILI_PREG_INT_PHYRDYCHG |
1141 SILI_PREG_INT_DEVEXCHG);
1145 * Figure out what type of device is connected to the port, ATAPI or
1149 sili_port_signature(struct sili_port *ap, struct ata_port *at, u_int32_t sig)
1152 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1153 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1154 return(ATA_PORT_T_ATAPI);
1155 } else if ((sig & 0xffff0000) ==
1156 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1157 return(ATA_PORT_T_PM);
1159 return(ATA_PORT_T_DISK);
1164 * Load the DMA descriptor table for a CCB's buffer.
1166 * NOTE: ATA_F_PIO is auto-selected by sili part.
1169 sili_load_prb(struct sili_ccb *ccb)
1171 struct sili_port *ap = ccb->ccb_port;
1172 struct sili_softc *sc = ap->ap_sc;
1173 struct ata_xfer *xa = &ccb->ccb_xa;
1174 struct sili_prb *prb = ccb->ccb_prb;
1175 struct sili_sge *sge;
1176 bus_dmamap_t dmap = ccb->ccb_dmamap;
1180 * Set up the PRB. The PRB contains 2 SGE's (1 if it is an ATAPI
1181 * command). The SGE must be set up to link to the rest of our
1182 * SGE array, in blocks of four SGEs (a SGE table) starting at
1184 prb->prb_xfer_count = 0;
1185 prb->prb_control = 0;
1186 prb->prb_override = 0;
1187 sge = (ccb->ccb_xa.flags & ATA_F_PACKET) ?
1188 &prb->prb_sge_packet : &prb->prb_sge_normal;
1189 if (xa->datalen == 0) {
1190 sge->sge_flags = SILI_SGE_FLAGS_TRM | SILI_SGE_FLAGS_DRD;
1195 if (ccb->ccb_xa.flags & ATA_F_READ)
1196 prb->prb_control |= SILI_PRB_CTRL_READ;
1197 if (ccb->ccb_xa.flags & ATA_F_WRITE)
1198 prb->prb_control |= SILI_PRB_CTRL_WRITE;
1199 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1201 sge->sge_paddr = ccb->ccb_prb_paddr +
1202 offsetof(struct sili_prb, prb_sge[0]);
1205 * Load our sge array.
1207 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1208 xa->data, xa->datalen,
1209 sili_load_prb_callback,
1211 ((xa->flags & ATA_F_NOWAIT) ?
1212 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1214 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1218 bus_dmamap_sync(sc->sc_tag_data, dmap,
1219 (xa->flags & ATA_F_READ) ?
1220 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1226 * Callback from BUSDMA system to load the segment list.
1228 * The scatter/gather table is loaded by the sili chip in blocks of
1229 * four SGE's. If a continuance is required the last entry in each
1230 * block must point to the next block.
1234 sili_load_prb_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1237 struct sili_ccb *ccb = info;
1238 struct sili_sge *sge;
1241 KKASSERT(nsegs <= SILI_MAX_SGET);
1244 sge = &ccb->ccb_prb->prb_sge[0];
1246 if ((sgi & 3) == 3) {
1247 sge->sge_paddr = htole64(ccb->ccb_prb_paddr +
1248 offsetof(struct sili_prb,
1251 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1253 sge->sge_paddr = htole64(segs->ds_addr);
1254 sge->sge_count = htole32(segs->ds_len);
1263 sge->sge_flags |= SILI_SGE_FLAGS_TRM;
1267 sili_unload_prb(struct sili_ccb *ccb)
1269 struct sili_port *ap = ccb->ccb_port;
1270 struct sili_softc *sc = ap->ap_sc;
1271 struct ata_xfer *xa = &ccb->ccb_xa;
1272 bus_dmamap_t dmap = ccb->ccb_dmamap;
1274 if (xa->datalen != 0) {
1275 bus_dmamap_sync(sc->sc_tag_data, dmap,
1276 (xa->flags & ATA_F_READ) ?
1277 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1279 bus_dmamap_unload(sc->sc_tag_data, dmap);
1281 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1284 xa->resid = xa->datalen -
1285 le32toh(ccb->ccb_prb->prb_xfer_count);
1290 * Start a command and poll for completion.
1292 * timeout is in ms and only counts once the command gets on-chip.
1294 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1295 * that no error occured.
1297 * NOTE: If the caller specifies a NULL timeout function the caller is
1298 * responsible for clearing hardware state on failure, but we will
1299 * deal with removing the ccb from any pending queue.
1301 * NOTE: NCQ should never be used with this function.
1303 * NOTE: If the port is in a failed state and stopped we do not try
1304 * to activate the ccb.
1307 sili_poll(struct sili_ccb *ccb, int timeout,
1308 void (*timeout_fn)(struct sili_ccb *))
1310 struct sili_port *ap = ccb->ccb_port;
1312 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1313 ccb->ccb_xa.state = ATA_S_ERROR;
1314 return(ccb->ccb_xa.state);
1317 KKASSERT((ap->ap_expired & (1 << ccb->ccb_slot)) == 0);
1321 sili_port_intr(ap, 1);
1322 switch(ccb->ccb_xa.state) {
1324 timeout -= sili_os_softsleep();
1328 * The packet can get stuck on the pending queue
1329 * if the port refuses to come ready. XXX
1332 if (xxx AP_F_EXCLUSIVE_ACCESS)
1333 timeout -= sili_os_softsleep();
1336 sili_os_softsleep();
1337 sili_check_active_timeouts(ap);
1340 return (ccb->ccb_xa.state);
1342 } while (timeout > 0);
1345 * Don't spew if this is a probe during hard reset
1347 if (ap->ap_probe != ATA_PROBE_NEED_HARD_RESET) {
1348 kprintf("%s: Poll timeout slot %d\n",
1349 ATANAME(ap, ccb->ccb_xa.at),
1355 return(ccb->ccb_xa.state);
1359 * When polling we have to check if the currently active CCB(s)
1360 * have timed out as the callout will be deadlocked while we
1361 * hold the port lock.
1364 sili_check_active_timeouts(struct sili_port *ap)
1366 struct sili_ccb *ccb;
1370 mask = ap->ap_active;
1372 tag = ffs(mask) - 1;
1373 mask &= ~(1 << tag);
1374 ccb = &ap->ap_ccbs[tag];
1375 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1376 sili_core_timeout(ccb, 0);
1384 sili_start_timeout(struct sili_ccb *ccb)
1386 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1387 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1388 callout_reset(&ccb->ccb_timeout,
1389 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1390 sili_ata_cmd_timeout_unserialized, ccb);
1395 sili_start(struct sili_ccb *ccb)
1397 struct sili_port *ap = ccb->ccb_port;
1399 struct sili_softc *sc = ap->ap_sc;
1402 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1405 * Sync our SGE table and PRB
1407 bus_dmamap_sync(ap->ap_dmamem_prbs->adm_tag,
1408 ap->ap_dmamem_prbs->adm_map,
1409 BUS_DMASYNC_PREWRITE);
1412 * XXX dmamap for PRB XXX BUS_DMASYNC_PREWRITE
1416 * Controller will update shared memory!
1417 * XXX bus_dmamap_sync ... BUS_DMASYNC_PREREAD ...
1419 /* Prepare RFIS area for write by controller */
1422 * There's no point trying to optimize this, it only shaves a few
1423 * nanoseconds so just queue the command and call our generic issue.
1425 sili_issue_pending_commands(ap, ccb);
1429 * Wait for all commands to complete processing. We hold the lock so no
1430 * new commands will be queued.
1433 sili_exclusive_access(struct sili_port *ap)
1435 while (ap->ap_active) {
1436 sili_port_intr(ap, 1);
1437 sili_os_softsleep();
1442 * If ccb is not NULL enqueue and/or issue it.
1444 * If ccb is NULL issue whatever we can from the queue. However, nothing
1445 * new is issued if the exclusive access flag is set or expired ccb's are
1448 * If existing commands are still active (ap_active) we can only
1449 * issue matching new commands.
1452 sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb)
1457 * If just running the queue and in exclusive access mode we
1458 * just return. Also in this case if there are any expired ccb's
1459 * we want to clear the queue so the port can be safely stopped.
1461 * XXX sili chip - expiration needs to be per-target if PM supports
1465 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1466 } else if (ap->ap_expired) {
1471 * Pull the next ccb off the queue and run it if possible.
1472 * If the port is not ready to accept commands enable the
1473 * ready interrupt instead of starting a new command.
1475 * XXX limit ncqdepth for attached devices behind PM
1477 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1479 * Port may be wedged.
1481 if ((sili_pread(ap, SILI_PREG_STATUS) &
1482 SILI_PREG_STATUS_READY) == 0) {
1483 kprintf("%s: slot %d NOT READY\n",
1484 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
1485 sili_pwrite(ap, SILI_PREG_INT_ENABLE,
1486 SILI_PREG_INT_READY);
1491 * Handle exclusivity requirements. ATA_F_EXCLUSIVE is used
1492 * when we may have to access the rfis which is stored in
1493 * the LRAM PRB. Unfortunately reading the LRAM PRB is
1494 * highly problematic, so requests (like PM requests) which
1495 * need to access the rfis use exclusive mode and then
1496 * access the copy made by the port interrupt code back in
1499 if (ap->ap_active & ~ap->ap_expired) {
1501 * There may be multiple ccb's already running,
1502 * if any are running and ap_run_flags sets
1503 * one of these flags then we know only one is
1506 * XXX Current AUTOSENSE code forces exclusivity
1507 * to simplify the code.
1509 if (ap->ap_run_flags &
1510 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1515 * If the ccb we want to run is exclusive and ccb's
1516 * are still active on the port, we can't queue it
1519 * XXX Current AUTOSENSE code forces exclusivity
1520 * to simplify the code.
1522 if (ccb->ccb_xa.flags &
1523 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1528 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1529 ccb->ccb_xa.state = ATA_S_ONCHIP;
1530 ap->ap_active |= 1 << ccb->ccb_slot;
1531 ap->ap_active_cnt++;
1532 ap->ap_run_flags = ccb->ccb_xa.flags;
1535 * We can't use the CMD_FIFO method because it requires us
1536 * building the PRB in the LRAM, and the LRAM is buggy. So
1537 * we use host memory for the PRB.
1539 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot),
1540 (u_int32_t)ccb->ccb_prb_paddr);
1541 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot) + 4,
1542 (u_int32_t)(ccb->ccb_prb_paddr >> 32));
1543 /* sili_pwrite(ap, SILI_PREG_CMD_FIFO, ccb->ccb_slot); */
1544 sili_start_timeout(ccb);
1549 sili_intr(void *arg)
1551 struct sili_softc *sc = arg;
1552 struct sili_port *ap;
1557 * Check if the master enable is up, and whether any interrupts are
1560 * Clear the ints we got.
1562 if ((sc->sc_flags & SILI_F_INT_GOOD) == 0)
1564 gint = sili_read(sc, SILI_REG_GINT);
1565 if (gint == 0 || gint == 0xffffffff)
1567 sili_write(sc, SILI_REG_GINT, gint);
1570 * Process interrupts for each port in a non-blocking fashion.
1572 while (gint & SILI_REG_GINT_PORTMASK) {
1573 port = ffs(gint) - 1;
1574 ap = sc->sc_ports[port];
1576 if (sili_os_lock_port_nb(ap) == 0) {
1577 sili_port_intr(ap, 0);
1578 sili_os_unlock_port(ap);
1580 sili_port_interrupt_redisable(ap);
1581 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1584 gint &= ~(1 << port);
1589 * Core called from helper thread.
1592 sili_port_thread_core(struct sili_port *ap, int mask)
1595 * Process any expired timedouts.
1597 sili_os_lock_port(ap);
1598 if (mask & AP_SIGF_TIMEOUT) {
1599 sili_check_active_timeouts(ap);
1603 * Process port interrupts which require a higher level of
1606 if (mask & AP_SIGF_PORTINT) {
1607 sili_port_intr(ap, 1);
1608 sili_port_interrupt_reenable(ap);
1609 sili_os_unlock_port(ap);
1611 sili_os_unlock_port(ap);
1616 * Core per-port interrupt handler.
1618 * If blockable is 0 we cannot call sili_os_sleep() at all and we can only
1619 * deal with normal command completions which do not require blocking.
1622 sili_port_intr(struct sili_port *ap, int blockable)
1624 struct sili_softc *sc = ap->ap_sc;
1627 struct sili_ccb *ccb = NULL;
1628 struct ata_port *ccb_at = NULL;
1631 const u_int32_t blockable_mask = SILI_PREG_IST_PHYRDYCHG |
1632 SILI_PREG_IST_DEVEXCHG |
1633 SILI_PREG_IST_CERROR |
1634 SILI_PREG_IST_DECODE |
1636 SILI_PREG_IST_HANDSHK;
1637 const u_int32_t fatal_mask = SILI_PREG_IST_PHYRDYCHG |
1638 SILI_PREG_IST_DEVEXCHG |
1639 SILI_PREG_IST_DECODE |
1641 SILI_PREG_IST_HANDSHK;
1643 enum { NEED_NOTHING, NEED_HOTPLUG_INSERT,
1644 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1647 * NOTE: CCOMPLETE was automatically cleared when we read INT_STATUS.
1649 is = sili_pread(ap, SILI_PREG_INT_STATUS);
1650 is &= SILI_PREG_IST_MASK;
1651 if (is & SILI_PREG_IST_CCOMPLETE)
1652 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CCOMPLETE);
1655 * If we can't block then we can't handle these here. Disable
1656 * the interrupts in question so we don't live-lock, the helper
1657 * thread will re-enable them.
1659 * If the port is in a completely failed state we do not want
1660 * to drop through to failed-command-processing if blockable is 0,
1661 * just let the thread deal with it all.
1663 * Otherwise we fall through and still handle DHRS and any commands
1664 * which completed normally. Even if we are errored we haven't
1665 * stopped the port yet so CI/SACT are still good.
1667 if (blockable == 0) {
1668 if (ap->ap_state == AP_S_FATAL_ERROR) {
1669 sili_port_interrupt_redisable(ap);
1670 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1671 /*is &= ~blockable_mask;*/
1674 if (is & blockable_mask) {
1675 sili_port_interrupt_redisable(ap);
1676 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1677 /*is &= ~blockable_mask;*/
1682 if (is & SILI_PREG_IST_CERROR) {
1684 * Command failed (blockable).
1686 * This stops command processing. We can extract the PM
1687 * target from the PMP field in SILI_PREG_CONTEXT. The
1688 * tag is not necessarily valid so don't use that.
1690 * We must then expire all CCB's for that target and resume
1691 * processing if any other targets have active commands.
1692 * Particular error codes can be recovered by reading the LOG
1695 * The expire handling code will do the rest, which is
1696 * basically to reset the port once the only active
1697 * commands remaining are all expired.
1703 target = (sili_pread(ap, SILI_PREG_CONTEXT) >>
1704 SILI_PREG_CONTEXT_PMPORT_SHIFT) &
1705 SILI_PREG_CONTEXT_PMPORT_MASK;
1706 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CERROR);
1707 active = ap->ap_active & ~ap->ap_expired;
1708 error = sili_pread(ap, SILI_PREG_CERROR);
1709 kprintf("%s.%d target error %d active=%08x hactive=%08x "
1711 PORTNAME(ap), target, error,
1712 active, sili_pread(ap, SILI_PREG_SLOTST),
1713 sili_pread(ap, SILI_PREG_SERR), SILI_PFMT_SERR);
1716 slot = ffs(active) - 1;
1717 ccb = &ap->ap_ccbs[slot];
1718 if ((ccb_at = ccb->ccb_xa.at) == NULL)
1719 ccb_at = &ap->ap_ata[0];
1720 if (target == ccb_at->at_target) {
1721 if ((ccb->ccb_xa.flags & ATA_F_NCQ) &&
1722 (error == SILI_PREG_CERROR_DEVICE ||
1723 error == SILI_PREG_CERROR_SDBERROR)) {
1724 ccb_at->at_features |= ATA_PORT_F_READLOG;
1726 if (sili_core_timeout(ccb, 1) == 0)
1729 active &= ~(1 << slot);
1733 * Resume will be 0 if the timeout reinited and restarted
1734 * the port. Otherwise we resume the port to allow other
1735 * commands to complete.
1738 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESUME);
1742 * Device notification to us (non-blocking)
1744 * This is interrupt status SILIPREG_IST_SDB
1746 * NOTE! On some parts notification bits can get set without
1747 * generating an interrupt. It is unclear whether this is
1748 * a bug in the PM (sending a DTOH device setbits with 'N' set
1749 * and 'I' not set), or a bug in the host controller.
1751 * It only seems to occur under load.
1753 if (sc->sc_flags & SILI_F_SSNTF) {
1757 data = sili_pread(ap, SILI_PREG_SNTF);
1758 if (is & SILI_PREG_IST_SDB) {
1759 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1761 is &= ~SILI_PREG_IST_SDB;
1762 xstr = " (no SDBS!)";
1767 kprintf("%s: NOTIFY %08x%s\n",
1768 PORTNAME(ap), data, xstr);
1769 sili_pwrite(ap, SILI_PREG_SNTF, data);
1770 sili_cam_changed(ap, NULL, -1);
1775 * Port change (hot-plug) (blockable).
1777 * A PCS interrupt will occur on hot-plug once communication is
1780 * A PRCS interrupt will occur on hot-unplug (and possibly also
1783 * XXX We can then check the CPS (Cold Presence State) bit, if
1784 * supported, to determine if a device is plugged in or not and do
1787 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
1788 * can also occur if an unsolicited COMINIT is received.
1789 * If this occurs command processing is automatically
1790 * stopped (CR goes inactive) and the port must be stopped
1793 if (is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG)) {
1795 sili_pwrite(ap, SILI_PREG_SERR,
1796 (SILI_PREG_SERR_DIAG_N | SILI_PREG_SERR_DIAG_X));
1797 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1798 is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG));
1800 is &= ~(SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG);
1801 kprintf("%s: Port change\n", PORTNAME(ap));
1803 switch (sili_pread(ap, SILI_PREG_SSTS) & SILI_PREG_SSTS_DET) {
1804 case SILI_PREG_SSTS_DET_DEV:
1805 if (ap->ap_type == ATA_PORT_T_NONE &&
1806 ap->ap_probe == ATA_PROBE_FAILED) {
1807 need = NEED_HOTPLUG_INSERT;
1812 kprintf("%s: Device lost\n", PORTNAME(ap));
1813 if (ap->ap_type != ATA_PORT_T_NONE) {
1814 need = NEED_HOTPLUG_REMOVE;
1822 * Check for remaining errors - they are fatal. (blockable)
1824 if (is & fatal_mask) {
1827 sili_pwrite(ap, SILI_PREG_INT_STATUS, is & fatal_mask);
1829 serr = sili_pread(ap, SILI_PREG_SERR);
1830 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
1831 "disabling port.\n",
1833 is, SILI_PFMT_INT_STATUS,
1834 serr, SILI_PFMT_SERR
1837 /* XXX try recovery first */
1842 * Fail all outstanding commands if we know the port won't recover.
1844 * We may have a ccb_at if the failed command is known and was
1845 * being sent to a device over a port multiplier (PM). In this
1846 * case if the port itself has not completely failed we fail just
1847 * the commands related to that target.
1849 if (ap->ap_state == AP_S_FATAL_ERROR &&
1850 (ap->ap_active & ~ap->ap_expired)) {
1851 kprintf("%s: Fatal port error, expiring %08x\n",
1852 PORTNAME(ap), ap->ap_active & ~ap->ap_expired);
1854 ap->ap_state = AP_S_FATAL_ERROR;
1857 * Error all the active slots. If running across a PM
1858 * try to error out just the slots related to the target.
1860 active = ap->ap_active & ~ap->ap_expired;
1863 slot = ffs(active) - 1;
1864 active &= ~(1 << slot);
1865 ccb = &ap->ap_ccbs[slot];
1866 sili_core_timeout(ccb, 1);
1871 * CCB completion (non blocking).
1873 * CCB completion is detected by noticing the slot bit in
1874 * the port slot status register has cleared while the bit
1875 * is still set in our ap_active variable.
1877 * When completing expired events we must remember to reinit
1878 * the port once everything is clear.
1880 * Due to a single-level recursion when reading the log page,
1881 * it is possible for the slot to already have been cleared
1882 * for some expired tags, do not include expired tags in
1885 active = ap->ap_active & ~sili_pread(ap, SILI_PREG_SLOTST);
1886 active &= ~ap->ap_expired;
1890 slot = ffs(active) - 1;
1891 ccb = &ap->ap_ccbs[slot];
1893 DPRINTF(SILI_D_INTR, "%s: slot %d is complete%s\n",
1894 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
1897 active &= ~(1 << slot);
1900 * XXX sync POSTREAD for return data?
1902 ap->ap_active &= ~(1 << ccb->ccb_slot);
1903 --ap->ap_active_cnt;
1906 * Complete the ccb. If the ccb was marked expired it
1907 * may or may not have been cleared from the port,
1908 * make sure we mark it as having timed out.
1910 * In a normal completion if AUTOSENSE is set we copy
1911 * the PRB LRAM rfis back to the rfis in host-memory.
1913 * XXX Currently AUTOSENSE also forces exclusivity so we
1914 * can safely work around a hardware bug when reading
1917 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
1918 ap->ap_expired &= ~(1 << ccb->ccb_slot);
1919 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1921 ccb->ccb_xa.complete(&ccb->ccb_xa);
1923 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
1924 ccb->ccb_xa.state = ATA_S_COMPLETE;
1925 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
1926 memcpy(ccb->ccb_xa.rfis,
1927 &ccb->ccb_prb_lram->prb_d2h,
1928 sizeof(ccb->ccb_prb_lram->prb_d2h));
1929 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
1930 ccb->ccb_xa.state = ATA_S_ERROR;
1936 if (is & SILI_PREG_IST_READY) {
1937 is &= ~SILI_PREG_IST_READY;
1938 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_READY);
1939 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_READY);
1943 * If we had expired commands and were waiting for
1944 * remaining commands to complete, and they have now
1945 * completed, we can reinit the port.
1947 * This will also clean out the expired commands.
1948 * The timeout code also calls sili_port_reinit() if
1949 * the only commands remaining after a timeout are all
1950 * now expired commands.
1952 * Otherwise just reissue.
1954 if (ap->ap_expired && ap->ap_active == ap->ap_expired) {
1956 sili_port_reinit(ap);
1958 sili_issue_pending_commands(ap, NULL);
1962 * Cleanup. Will not be set if non-blocking.
1965 case NEED_HOTPLUG_INSERT:
1967 * A hot-plug insertion event has occured and all
1968 * outstanding commands have already been revoked.
1970 * Don't recurse if this occurs while we are
1971 * resetting the port.
1973 * Place the port in a continuous COMRESET state
1974 * until the INIT code gets to it.
1976 kprintf("%s: HOTPLUG - Device inserted\n",
1978 ap->ap_probe = ATA_PROBE_NEED_INIT;
1979 sili_cam_changed(ap, NULL, -1);
1981 case NEED_HOTPLUG_REMOVE:
1983 * A hot-plug removal event has occured and all
1984 * outstanding commands have already been revoked.
1986 * Don't recurse if this occurs while we are
1987 * resetting the port.
1989 kprintf("%s: HOTPLUG - Device removed\n",
1991 sili_port_hardstop(ap);
1992 /* ap_probe set to failed */
1993 sili_cam_changed(ap, NULL, -1);
2001 sili_get_ccb(struct sili_port *ap)
2003 struct sili_ccb *ccb;
2005 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2006 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2008 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2009 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2010 ccb->ccb_xa.state = ATA_S_SETUP;
2011 ccb->ccb_xa.at = NULL;
2013 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2019 sili_put_ccb(struct sili_ccb *ccb)
2021 struct sili_port *ap = ccb->ccb_port;
2023 ccb->ccb_xa.state = ATA_S_PUT;
2024 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2025 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2026 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2030 sili_get_err_ccb(struct sili_port *ap)
2032 struct sili_ccb *err_ccb;
2034 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2035 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2038 * Grab a CCB to use for error recovery. This should never fail, as
2039 * we ask atascsi to reserve one for us at init time.
2041 err_ccb = ap->ap_err_ccb;
2042 KKASSERT(err_ccb != NULL);
2043 err_ccb->ccb_xa.flags = 0;
2044 err_ccb->ccb_done = sili_empty_done;
2050 sili_put_err_ccb(struct sili_ccb *ccb)
2052 struct sili_port *ap = ccb->ccb_port;
2054 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2056 KKASSERT(ccb == ap->ap_err_ccb);
2058 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2062 * Read log page to get NCQ error.
2064 * Return 0 on success
2067 sili_port_read_ncq_error(struct sili_port *ap, int target)
2069 struct sili_ccb *ccb;
2070 struct ata_fis_h2d *fis;
2073 DPRINTF(SILI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2075 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2076 ccb = sili_get_err_ccb(ap);
2077 ccb->ccb_done = sili_empty_done;
2078 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2079 ccb->ccb_xa.data = ap->ap_err_scratch;
2080 ccb->ccb_xa.datalen = 512;
2081 ccb->ccb_xa.complete = sili_dummy_done;
2082 ccb->ccb_xa.at = &ap->ap_ata[target];
2083 fis = &ccb->ccb_prb->prb_h2d;
2084 bzero(fis, sizeof(*fis));
2086 fis->type = ATA_FIS_TYPE_H2D;
2087 fis->flags = ATA_H2D_FLAGS_CMD | target;
2088 fis->command = ATA_C_READ_LOG_EXT;
2089 fis->lba_low = 0x10; /* queued error log page (10h) */
2090 fis->sector_count = 1; /* number of sectors (1) */
2091 fis->sector_count_exp = 0;
2092 fis->lba_mid = 0; /* starting offset */
2093 fis->lba_mid_exp = 0;
2097 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
2099 if (sili_load_prb(ccb) != 0) {
2100 status = ATA_S_ERROR;
2102 ccb->ccb_xa.state = ATA_S_PENDING;
2103 status = sili_poll(ccb, 1000, sili_quick_timeout);
2107 * Just spew if it fails, there isn't much we can do at this point.
2109 if (status != ATA_S_COMPLETE) {
2110 kprintf("%s: log page read failed, slot %d was still active.\n",
2111 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
2114 /* Done with the error CCB now. */
2115 sili_unload_prb(ccb);
2116 sili_put_err_ccb(ccb);
2118 /* Extract failed register set and tags from the scratch space. */
2119 if (status == ATA_S_COMPLETE) {
2120 struct ata_log_page_10h *log;
2123 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2124 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2126 * Not queued bit was set - wasn't an NCQ error?
2128 * XXX This bit seems to be set a lot even for NCQ
2133 * Copy back the log record as a D2H register FIS.
2135 err_slot = log->err_regs.type &
2136 ATA_LOG_10H_TYPE_TAG_MASK;
2137 ccb = &ap->ap_ccbs[err_slot];
2138 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2139 kprintf("%s: read NCQ error page slot=%d\n",
2140 ATANAME(ap, ccb->ccb_xa.at), err_slot
2142 memcpy(&ccb->ccb_prb->prb_d2h, &log->err_regs,
2143 sizeof(struct ata_fis_d2h));
2144 ccb->ccb_prb->prb_d2h.type = ATA_FIS_TYPE_D2H;
2145 ccb->ccb_prb->prb_d2h.flags = 0;
2146 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
2147 ccb->ccb_xa.state = ATA_S_ERROR;
2149 kprintf("%s: read NCQ error page slot=%d, "
2150 "slot does not match any cmds\n",
2151 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2160 * Allocate memory for various structures DMAd by hardware. The maximum
2161 * number of segments for these tags is 1 so the DMA memory will have a
2162 * single physical base address.
2164 struct sili_dmamem *
2165 sili_dmamem_alloc(struct sili_softc *sc, bus_dma_tag_t tag)
2167 struct sili_dmamem *adm;
2170 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2172 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2173 BUS_DMA_ZERO, &adm->adm_map);
2176 error = bus_dmamap_load(tag, adm->adm_map,
2178 bus_dma_tag_getmaxsize(tag),
2179 sili_dmamem_saveseg, &adm->adm_busaddr,
2184 bus_dmamap_destroy(tag, adm->adm_map);
2185 adm->adm_map = NULL;
2186 adm->adm_tag = NULL;
2187 adm->adm_kva = NULL;
2189 kfree(adm, M_DEVBUF);
2197 sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2199 KKASSERT(error == 0);
2200 KKASSERT(nsegs == 1);
2201 *(bus_addr_t *)info = segs->ds_addr;
2206 sili_dmamem_free(struct sili_softc *sc, struct sili_dmamem *adm)
2209 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2210 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2211 adm->adm_map = NULL;
2212 adm->adm_tag = NULL;
2213 adm->adm_kva = NULL;
2215 kfree(adm, M_DEVBUF);
2219 sili_read(struct sili_softc *sc, bus_size_t r)
2221 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2222 BUS_SPACE_BARRIER_READ);
2223 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2227 sili_write(struct sili_softc *sc, bus_size_t r, u_int32_t v)
2229 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2230 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2231 BUS_SPACE_BARRIER_WRITE);
2235 sili_pread(struct sili_port *ap, bus_size_t r)
2237 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2238 BUS_SPACE_BARRIER_READ);
2239 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2243 sili_pwrite(struct sili_port *ap, bus_size_t r, u_int32_t v)
2245 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2246 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2247 BUS_SPACE_BARRIER_WRITE);
2251 * Wait up to (timeout) milliseconds for the masked port register to
2254 * Timeout is in milliseconds.
2257 sili_pwait_eq(struct sili_port *ap, int timeout,
2258 bus_size_t r, u_int32_t mask, u_int32_t target)
2263 * Loop hard up to 100uS
2265 for (t = 0; t < 100; ++t) {
2266 if ((sili_pread(ap, r) & mask) == target)
2268 sili_os_hardsleep(1); /* us */
2272 timeout -= sili_os_softsleep();
2273 if ((sili_pread(ap, r) & mask) == target)
2275 } while (timeout > 0);
2280 sili_wait_ne(struct sili_softc *sc, bus_size_t r, u_int32_t mask,
2286 * Loop hard up to 100uS
2288 for (t = 0; t < 100; ++t) {
2289 if ((sili_read(sc, r) & mask) != target)
2291 sili_os_hardsleep(1); /* us */
2295 * And one millisecond the slow way
2299 t -= sili_os_softsleep();
2300 if ((sili_read(sc, r) & mask) != target)
2309 * Acquire an ata transfer.
2311 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2312 * targets that go through the port multiplier.
2315 sili_ata_get_xfer(struct sili_port *ap, struct ata_port *at)
2317 struct sili_ccb *ccb;
2319 ccb = sili_get_ccb(ap);
2321 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer: NULL ccb\n",
2326 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer got slot %d\n",
2327 PORTNAME(ap), ccb->ccb_slot);
2329 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
2330 ccb->ccb_xa.at = at;
2331 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2333 return (&ccb->ccb_xa);
2337 sili_ata_put_xfer(struct ata_xfer *xa)
2339 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2341 DPRINTF(SILI_D_XFER, "sili_ata_put_xfer slot %d\n", ccb->ccb_slot);
2347 sili_ata_cmd(struct ata_xfer *xa)
2349 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2351 KKASSERT(xa->state == ATA_S_SETUP);
2353 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2356 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2357 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2358 sili_pread(ccb->ccb_port, SILI_PREG_CMD), SILI_PFMT_CMD,
2361 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2364 ccb->ccb_done = sili_ata_cmd_done;
2366 if (sili_load_prb(ccb) != 0)
2369 xa->state = ATA_S_PENDING;
2371 if (xa->flags & ATA_F_POLL)
2372 return (sili_poll(ccb, xa->timeout, sili_ata_cmd_timeout));
2375 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
2376 xa->flags |= ATA_F_TIMEOUT_DESIRED;
2383 xa->state = ATA_S_ERROR;
2386 return (ATA_S_ERROR);
2390 sili_ata_cmd_done(struct sili_ccb *ccb)
2392 struct ata_xfer *xa = &ccb->ccb_xa;
2395 * NOTE: callout does not lock port and may race us modifying
2396 * the flags, so make sure its stopped.
2398 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2399 callout_stop(&ccb->ccb_timeout);
2400 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2402 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
2404 KKASSERT(xa->state != ATA_S_ONCHIP);
2405 sili_unload_prb(ccb);
2407 if (xa->state != ATA_S_TIMEOUT)
2412 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
2413 * while the callout is runing.
2415 * We can't safely get the port lock here or delay, we could block
2416 * the callout thread.
2419 sili_ata_cmd_timeout_unserialized(void *arg)
2421 struct sili_ccb *ccb = arg;
2422 struct sili_port *ap = ccb->ccb_port;
2424 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
2425 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
2426 sili_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
2430 sili_ata_cmd_timeout(struct sili_ccb *ccb)
2432 sili_core_timeout(ccb, 0);
2436 * Timeout code, typically called when the port command processor is running.
2438 * Returns 0 if all timeout processing completed, non-zero if it is still
2443 sili_core_timeout(struct sili_ccb *ccb, int really_error)
2445 struct ata_xfer *xa = &ccb->ccb_xa;
2446 struct sili_port *ap = ccb->ccb_port;
2447 struct ata_port *at;
2449 at = ccb->ccb_xa.at;
2451 kprintf("%s: CMD %s state=%d slot=%d\n"
2456 (really_error ? "ERROR" : "TIMEOUT"),
2457 ccb->ccb_xa.state, ccb->ccb_slot,
2460 sili_pread(ap, SILI_PREG_SLOTST)
2464 * NOTE: Timeout will not be running if the command was polled.
2465 * If we got here at least one of these flags should be set.
2467 * However, it might be running if we are called from the
2468 * interrupt error handling code.
2470 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2471 ATA_F_TIMEOUT_RUNNING));
2472 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2473 callout_stop(&ccb->ccb_timeout);
2474 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2476 xa->flags &= ~ATA_F_TIMEOUT_EXPIRED;
2478 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2479 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2480 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2483 sili_issue_pending_commands(ap, NULL);
2486 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2487 kprintf("%s: Unexpected state during timeout: %d\n",
2488 ATANAME(ap, at), ccb->ccb_xa.state);
2493 * We can't process timeouts while other commands are running.
2495 ap->ap_expired |= 1 << ccb->ccb_slot;
2497 if (ap->ap_active != ap->ap_expired) {
2498 kprintf("%s: Deferred timeout until its safe, slot %d\n",
2499 ATANAME(ap, at), ccb->ccb_slot);
2504 * We have to issue a Port reinit. We don't read an error log
2505 * page for timeouts. Reiniting the port will clear all pending
2508 sili_port_reinit(ap);
2513 * Used by the softreset, pm_port_probe, and read_ncq_error only, in very
2514 * specialized, controlled circumstances.
2517 sili_quick_timeout(struct sili_ccb *ccb)
2519 struct sili_port *ap = ccb->ccb_port;
2521 switch (ccb->ccb_xa.state) {
2523 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2524 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2527 KKASSERT((ap->ap_active & ~ap->ap_expired) ==
2528 (1 << ccb->ccb_slot));
2529 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2530 ap->ap_active &= ~(1 << ccb->ccb_slot);
2531 KKASSERT(ap->ap_active_cnt > 0);
2532 --ap->ap_active_cnt;
2533 sili_port_reinit(ap);
2536 panic("%s: sili_quick_timeout: ccb in bad state %d",
2537 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
2542 sili_dummy_done(struct ata_xfer *xa)
2547 sili_empty_done(struct sili_ccb *ccb)