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 int sili_port_softreset(struct sili_port *ap);
66 int sili_port_pmprobe(struct sili_port *ap);
67 int sili_port_hardreset(struct sili_port *ap, int hard);
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);
73 void sili_quick_timeout(struct sili_ccb *ccb);
74 void sili_check_active_timeouts(struct sili_port *ap);
76 void sili_beg_exclusive_access(struct sili_port *ap, struct ata_port *at);
77 void sili_end_exclusive_access(struct sili_port *ap, struct ata_port *at);
78 void sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb);
80 int sili_port_read_ncq_error(struct sili_port *, int);
82 struct sili_dmamem *sili_dmamem_alloc(struct sili_softc *, bus_dma_tag_t tag);
83 void sili_dmamem_free(struct sili_softc *, struct sili_dmamem *);
84 static void sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
86 static void sili_dummy_done(struct ata_xfer *xa);
87 static void sili_empty_done(struct sili_ccb *ccb);
88 static void sili_ata_cmd_done(struct sili_ccb *ccb);
90 /* Wait for all bits in _b to be cleared */
91 #define sili_pwait_clr(_ap, _r, _b) \
92 sili_pwait_eq((_ap), SILI_PWAIT_TIMEOUT, (_r), (_b), 0)
93 #define sili_pwait_clr_to(_ap, _to, _r, _b) \
94 sili_pwait_eq((_ap), _to, (_r), (_b), 0)
96 /* Wait for all bits in _b to be set */
97 #define sili_pwait_set(_ap, _r, _b) \
98 sili_pwait_eq((_ap), SILI_PWAIT_TIMEOUT, (_r), (_b), (_b))
99 #define sili_pwait_set_to(_ap, _to, _r, _b) \
100 sili_pwait_eq((_ap), _to, (_r), (_b), (_b))
102 #define SILI_PWAIT_TIMEOUT 1000
105 * Initialize the global SILI hardware. This code does not set up any of
109 sili_init(struct sili_softc *sc)
111 DPRINTF(SILI_D_VERBOSE, " GHC 0x%b",
112 sili_read(sc, SILI_REG_GHC), SILI_FMT_GHC);
115 * Reset the entire chip. This also resets all ports.
117 * The spec doesn't say anything about how long we have to
118 * wait, so wait 10ms.
120 sili_write(sc, SILI_REG_GCTL, SILI_REG_GCTL_GRESET);
122 sili_write(sc, SILI_REG_GCTL, 0);
129 * Allocate and initialize an SILI port.
132 sili_port_alloc(struct sili_softc *sc, u_int port)
134 struct sili_port *ap;
136 struct sili_ccb *ccb;
141 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
143 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
144 device_get_name(sc->sc_dev),
145 device_get_unit(sc->sc_dev),
147 sc->sc_ports[port] = ap;
148 kprintf("%s: allocate port\n", PORTNAME(ap));
151 * Allocate enough so we never have to reallocate, it makes
154 * ap_pmcount will be reduced by the scan if we encounter the
155 * port multiplier port prior to target 15.
157 if (ap->ap_ata == NULL) {
158 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * SILI_MAX_PMPORTS,
159 M_DEVBUF, M_INTWAIT | M_ZERO);
160 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
162 at->at_sili_port = ap;
164 at->at_probe = ATA_PROBE_NEED_INIT;
165 at->at_features |= ATA_PORT_F_RESCAN;
166 ksnprintf(at->at_name, sizeof(at->at_name),
167 "%s.%d", ap->ap_name, i);
170 if (bus_space_subregion(sc->sc_piot, sc->sc_pioh,
171 SILI_PORT_REGION(port), SILI_PORT_SIZE,
173 device_printf(sc->sc_dev,
174 "unable to create register window for port %d\n",
181 ap->ap_probe = ATA_PROBE_NEED_INIT;
182 TAILQ_INIT(&ap->ap_ccb_free);
183 TAILQ_INIT(&ap->ap_ccb_pending);
184 lockinit(&ap->ap_ccb_lock, "silipo", 0, 0);
186 /* Disable port interrupts */
187 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
190 * Reset the port. This is similar to a Device Reset but far
191 * more invasive. We use Device Reset in our hardreset function.
192 * This function also does the same OOB initialization sequence
193 * that Device Reset does.
195 * NOTE: SILI_PREG_STATUS_READY will not be asserted unless and until
196 * a device is connected to the port, so we can't use it to
197 * verify that the port exists.
199 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
200 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
201 device_printf(sc->sc_dev,
202 "Port %d will not go into reset\n", port);
206 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
207 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
210 * Allocate the SGE Table
212 ap->ap_dmamem_sget = sili_dmamem_alloc(sc, sc->sc_tag_sget);
213 if (ap->ap_dmamem_sget == NULL) {
214 kprintf("%s: NOSGET\n", PORTNAME(ap));
219 * Set up the SGE table base address
221 ap->ap_sget = (struct sili_sge *)SILI_DMA_KVA(ap->ap_dmamem_sget);
224 * Allocate a CCB for each command slot
226 ap->ap_ccbs = kmalloc(sizeof(struct sili_ccb) * sc->sc_ncmds, M_DEVBUF,
228 if (ap->ap_ccbs == NULL) {
229 device_printf(sc->sc_dev,
230 "unable to allocate command list for port %d\n",
236 * Port control register setup.
238 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_NOAUTOCC);
239 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_32BITDMA |
241 SILI_PREG_CTL_NOAUTOCC);
244 * Most structures are in the port BAR. Assign convenient
245 * pointers in the CCBs
248 for (i = 0; i < sc->sc_ncmds; i++) {
249 ccb = &ap->ap_ccbs[i];
251 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
254 device_printf(sc->sc_dev,
255 "unable to create dmamap for port %d "
256 "ccb %d\n", port, i);
261 * NOTE: fis and rfis overload the same structure.
263 callout_init(&ccb->ccb_timeout);
266 ccb->ccb_prb = bus_space_kva(sc->sc_piot, ap->ap_ioh,
267 SILI_PREG_LRAM_SLOT(i));
268 ccb->ccb_sge = &ap->ap_sget[i * SILI_MAX_SGET];
269 ccb->ccb_sge_paddr = SILI_DMA_DVA(ap->ap_dmamem_sget) +
270 sizeof(*ccb->ccb_sge) * SILI_MAX_SGET * i;
271 ccb->ccb_xa.fis = &ccb->ccb_prb->prb_h2d;
272 ccb->ccb_xa.rfis = &ccb->ccb_prb->prb_d2h;
273 ccb->ccb_xa.packetcmd = prb_packet(ccb->ccb_prb);
276 ccb->ccb_xa.state = ATA_S_COMPLETE;
279 * Reserve CCB[1] as the error CCB. It doesn't matter
280 * which one we use for the Sili controllers.
283 ap->ap_err_ccb = ccb;
287 kprintf("%s: start port\n", PORTNAME(ap));
288 sili_os_start_port(ap);
291 sili_port_free(sc, port);
296 * [re]initialize an idle port. No CCBs should be active.
298 * If at is NULL we are initializing a directly connected port, otherwise
299 * we are indirectly initializing a port multiplier port.
301 * This function is called during the initial port allocation sequence
302 * and is also called on hot-plug insertion. We take no chances and
303 * use a hardreset instead of a softreset.
305 * This function is the only way to move a failed port back to active
308 * Returns 0 if a device is successfully detected.
311 sili_port_init(struct sili_port *ap, struct ata_port *atx)
317 * Clear all notification bits
319 if (atx == NULL && (ap->ap_sc->sc_flags & SILI_F_SSNTF))
320 sili_pwrite(ap, SILI_PREG_SNTF, -1);
323 * Make sure the port is out of continuous COMRESET mode.
325 data = SILI_PREG_SCTL_SPM_NONE |
326 SILI_PREG_SCTL_IPM_NONE |
327 SILI_PREG_SCTL_SPD_NONE |
328 SILI_PREG_SCTL_DET_NONE;
329 if (SiliForceGen1 & (1 << ap->ap_num)) {
330 data &= ~SILI_PREG_SCTL_SPD_NONE;
331 data |= SILI_PREG_SCTL_SPD_GEN1;
333 sili_pwrite(ap, SILI_PREG_SCTL, data);
336 * Hard-reset the port. If a device is detected but it is busy
337 * we try a second time, this time cycling the phy as well.
339 * XXX note: hard reset mode 2 (cycling the PHY) is not reliable.
342 atx->at_probe = ATA_PROBE_NEED_HARD_RESET;
344 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
346 rc = sili_port_reset(ap, atx, 1);
348 rc = sili_port_reset(ap, atx, 1);
350 rc = sili_port_reset(ap, atx, 2);
357 * We had problems talking to the device on the port.
360 sili_pm_read(ap, atx->at_target,
361 SATA_PMREG_SSTS, &data);
363 switch(data & SATA_PM_SSTS_DET) {
364 case SATA_PM_SSTS_DET_DEV_NE:
365 kprintf("%s: Device not communicating\n",
368 case SATA_PM_SSTS_DET_PHYOFFLINE:
369 kprintf("%s: PHY offline\n",
373 kprintf("%s: No device detected\n",
378 data = sili_pread(ap, SILI_PREG_SSTS);
380 switch(data & SATA_PM_SSTS_DET) {
381 case SILI_PREG_SSTS_DET_DEV_NE:
382 kprintf("%s: Device not communicating\n",
385 case SILI_PREG_SSTS_DET_OFFLINE:
386 kprintf("%s: PHY offline\n",
390 kprintf("%s: No device detected\n",
399 * The device on the port is still telling us its busy,
400 * which means that it is not properly handling a SATA
403 * It may be possible to softreset the device using CLO
404 * and a device reset command.
407 kprintf("%s: Device on port is bricked, giving up\n",
410 kprintf("%s: Device on port is bricked, "
411 "trying softreset\n", PORTNAME(ap));
413 rc = sili_port_reset(ap, atx, 0);
415 kprintf("%s: Unable unbrick device\n",
418 kprintf("%s: Successfully unbricked\n",
429 * Command transfers can only be enabled if a device was successfully
432 * Allocate or deallocate the ap_ata array here too.
435 switch(ap->ap_type) {
436 case ATA_PORT_T_NONE:
449 * Flush interrupts on the port. XXX
451 * Enable interrupts on the port whether a device is sitting on
452 * it or not, to handle hot-plug events.
456 sili_pwrite(ap, SILI_PREG_IS, sili_pread(ap, SILI_PREG_IS));
457 sili_write(ap->ap_sc, SILI_REG_IS, 1 << ap->ap_num);
459 sili_port_interrupt_enable(ap);
465 * Handle an errored port. This routine is called when the only
466 * commands left on the queue are expired, meaning we can safely
467 * go through a port init to clear its state.
469 * We complete the expired CCBs and then restart the queue.
473 sili_port_reinit(struct sili_port *ap)
475 struct sili_ccb *ccb;
482 reentrant = (ap->ap_flags & AP_F_ERR_CCB_RESERVED) ? 1 : 0;
484 kprintf("%s: PORT REINIT AFTER ERROR reentrant=%d\n",
485 PORTNAME(ap), reentrant);
488 * Clear port resume, clear bits 16:13 in the port device status
489 * register. This is from the data sheet.
491 * Data sheet does not specify a delay but it seems prudent.
493 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
495 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
496 data = sili_pread(ap, SILI_PREG_PM_STATUS(target));
497 data &= ~(SILI_PREG_PM_STATUS_SERVICE |
498 SILI_PREG_PM_STATUS_LEGACY |
499 SILI_PREG_PM_STATUS_NATIVE |
500 SILI_PREG_PM_STATUS_VBSY);
501 sili_pwrite(ap, SILI_PREG_PM_STATUS(target), data);
502 sili_pwrite(ap, SILI_PREG_PM_QACTIVE(target), 0);
506 * Issue a Port Initialize and wait for it to clear. This flushes
507 * commands but does not reset the port. Then wait for port ready.
509 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
510 if (sili_pwait_clr(ap, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
511 kprintf("%s: Unable to reinit, port failed\n",
514 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
515 kprintf("%s: Unable to reinit, port will not come ready\n",
520 * Read the LOG ERROR page for targets that returned a specific
521 * D2H FIS with ERR set.
523 if (reentrant == 0) {
524 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
525 at = &ap->ap_ata[target];
526 if (at->at_features & ATA_PORT_F_READLOG) {
527 kprintf("%s: READ LOG ERROR PAGE\n",
529 at->at_features &= ~ATA_PORT_F_READLOG;
530 sili_port_read_ncq_error(ap, target);
536 * Finally clean out the expired commands, we've probed the error
537 * status (or hopefully probed the error status). Well, ok,
538 * we probably didn't XXX.
540 while (ap->ap_expired) {
541 slot = ffs(ap->ap_expired) - 1;
542 ap->ap_expired &= ~(1 << slot);
543 KKASSERT(ap->ap_active & (1 << slot));
544 ap->ap_active &= ~(1 << slot);
546 ccb = &ap->ap_ccbs[slot];
547 ccb->ccb_xa.state = ATA_S_TIMEOUT;
548 kprintf("%s: reinit: kill slot %d\n",
549 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
551 ccb->ccb_xa.complete(&ccb->ccb_xa);
555 * Wow. All done. We can get the port moving again.
558 kprintf("%s: reinit called reentrantly, skip end\n",
560 } else if (ap->ap_probe == ATA_PROBE_FAILED) {
561 kprintf("%s: reinit failed, port is dead\n", PORTNAME(ap));
562 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
563 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
564 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
565 ccb->ccb_xa.state = ATA_S_TIMEOUT;
567 ccb->ccb_xa.complete(&ccb->ccb_xa);
570 kprintf("%s: reinit succeeded probe=%d type=%d\n", PORTNAME(ap), ap->ap_probe, ap->ap_type);
571 sili_issue_pending_commands(ap, NULL);
577 * Enable or re-enable interrupts on a port.
579 * This routine is called from the port initialization code or from the
580 * helper thread as the real interrupt may be forced to turn off certain
584 sili_port_interrupt_enable(struct sili_port *ap)
588 data = SILI_PREG_INT_CCOMPLETE | SILI_PREG_INT_CERROR |
589 SILI_PREG_INT_PHYRDYCHG | SILI_PREG_INT_DEVEXCHG |
590 SILI_PREG_INT_DECODE | SILI_PREG_INT_CRC |
591 SILI_PREG_INT_HANDSHK | SILI_PREG_INT_PMCHANGE;
592 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
593 data |= SILI_PREG_INT_SDB;
594 sili_pwrite(ap, SILI_PREG_INT_ENABLE, data);
598 sili_port_interrupt_redisable(struct sili_port *ap)
602 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
603 data &= SILI_REG_GINT_PORTMASK;
604 data &= ~(1 << ap->ap_num);
605 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
609 sili_port_interrupt_reenable(struct sili_port *ap)
613 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
614 data &= SILI_REG_GINT_PORTMASK;
615 data |= (1 << ap->ap_num);
616 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
620 * Run the port / target state machine from a main context.
622 * The state machine for the port is always run.
624 * If atx is non-NULL run the state machine for a particular target.
625 * If atx is NULL run the state machine for all targets.
628 sili_port_state_machine(struct sili_port *ap, int initial)
637 * State machine for port. Note that CAM is not yet associated
638 * during the initial parallel probe and the port's probe state
639 * will not get past ATA_PROBE_NEED_IDENT.
642 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
643 kprintf("%s: Waiting 10 seconds on insertion\n",
645 sili_os_sleep(10000);
648 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
649 sili_port_init(ap, NULL);
650 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
651 sili_port_reset(ap, NULL, 1);
652 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
653 sili_port_reset(ap, NULL, 0);
654 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
655 sili_cam_probe(ap, NULL);
657 if (ap->ap_type != ATA_PORT_T_PM) {
658 if (ap->ap_probe == ATA_PROBE_FAILED) {
659 sili_cam_changed(ap, NULL, 0);
660 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
661 sili_cam_changed(ap, NULL, 1);
667 * Port Multiplier state machine.
669 * Get a mask of changed targets and combine with any runnable
670 * states already present.
672 for (loop = 0; ;++loop) {
673 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
674 kprintf("%s: PM unable to read hot-plug bitmap\n",
680 * Do at least one loop, then stop if no more state changes
681 * have occured. The PM might not generate a new
682 * notification until we clear the entire bitmap.
684 if (loop && data == 0)
688 * New devices showing up in the bitmap require some spin-up
689 * time before we start probing them. Reset didsleep. The
690 * first new device we detect will sleep before probing.
692 * This only applies to devices whos change bit is set in
693 * the data, and does not apply to the initial boot-time
698 for (target = 0; target < ap->ap_pmcount; ++target) {
699 at = &ap->ap_ata[target];
702 * Check the target state for targets behind the PM
703 * which have changed state. This will adjust
704 * at_probe and set ATA_PORT_F_RESCAN
706 * We want to wait at least 10 seconds before probing
707 * a newly inserted device. If the check status
708 * indicates a device is present and in need of a
709 * hard reset, we make sure we have slept before
712 * We also need to wait at least 1 second for the
713 * PHY state to change after insertion, if we
714 * haven't already waited the 10 seconds.
716 * NOTE: When pm_check_good finds a good port it
717 * typically starts us in probe state
718 * NEED_HARD_RESET rather than INIT.
720 if (data & (1 << target)) {
721 if (initial == 0 && didsleep == 0)
723 sili_pm_check_good(ap, target);
724 if (initial == 0 && didsleep == 0 &&
725 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
728 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
729 sili_os_sleep(10000);
734 * Report hot-plug events before the probe state
735 * really gets hot. Only actual events are reported
736 * here to reduce spew.
738 if (data & (1 << target)) {
739 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
740 switch(at->at_probe) {
741 case ATA_PROBE_NEED_INIT:
742 case ATA_PROBE_NEED_HARD_RESET:
743 kprintf("Device inserted\n");
745 case ATA_PROBE_FAILED:
746 kprintf("Device removed\n");
749 kprintf("Device probe in progress\n");
755 * Run through the state machine as necessary if
756 * the port is not marked failed.
758 * The state machine may stop at NEED_IDENT if
759 * CAM is not yet attached.
761 * Acquire exclusive access to the port while we
762 * are doing this. This prevents command-completion
763 * from queueing commands for non-polled targets
764 * inbetween our probe steps. We need to do this
765 * because the reset probes can generate severe PHY
766 * and protocol errors and soft-brick the port.
768 if (at->at_probe != ATA_PROBE_FAILED &&
769 at->at_probe != ATA_PROBE_GOOD) {
770 sili_beg_exclusive_access(ap, at);
771 if (at->at_probe == ATA_PROBE_NEED_INIT)
772 sili_port_init(ap, at);
773 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
774 sili_port_reset(ap, at, 1);
775 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
776 sili_port_reset(ap, at, 0);
777 if (at->at_probe == ATA_PROBE_NEED_IDENT)
778 sili_cam_probe(ap, at);
779 sili_end_exclusive_access(ap, at);
783 * Add or remove from CAM
785 if (at->at_features & ATA_PORT_F_RESCAN) {
786 at->at_features &= ~ATA_PORT_F_RESCAN;
787 if (at->at_probe == ATA_PROBE_FAILED) {
788 sili_cam_changed(ap, at, 0);
789 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
790 sili_cam_changed(ap, at, 1);
793 data &= ~(1 << target);
796 kprintf("%s: WARNING (PM): extra bits set in "
797 "EINFO: %08x\n", PORTNAME(ap), data);
798 while (target < SILI_MAX_PMPORTS) {
799 sili_pm_check_good(ap, target);
807 * De-initialize and detach a port.
810 sili_port_free(struct sili_softc *sc, u_int port)
812 struct sili_port *ap = sc->sc_ports[port];
813 struct sili_ccb *ccb;
816 * Ensure port is disabled and its interrupts are all flushed.
819 sili_os_stop_port(ap);
820 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
821 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
822 sili_write(ap->ap_sc, SILI_REG_GCTL,
823 sili_read(ap->ap_sc, SILI_REG_GCTL) &
824 ~SILI_REG_GINT_PORTST(ap->ap_num));
828 while ((ccb = sili_get_ccb(ap)) != NULL) {
829 if (ccb->ccb_dmamap) {
830 bus_dmamap_destroy(sc->sc_tag_data,
832 ccb->ccb_dmamap = NULL;
835 if ((ccb = ap->ap_err_ccb) != NULL) {
836 if (ccb->ccb_dmamap) {
837 bus_dmamap_destroy(sc->sc_tag_data,
839 ccb->ccb_dmamap = NULL;
841 ap->ap_err_ccb = NULL;
843 kfree(ap->ap_ccbs, M_DEVBUF);
847 if (ap->ap_dmamem_sget) {
848 sili_dmamem_free(sc, ap->ap_dmamem_sget);
849 ap->ap_dmamem_sget = NULL;
852 kfree(ap->ap_ata, M_DEVBUF);
856 /* bus_space(9) says we dont free the subregions handle */
859 sc->sc_ports[port] = NULL;
865 * If hard is 0 perform a softreset of the port.
866 * If hard is 1 perform a hard reset of the port.
867 * If hard is 2 perform a hard reset of the port and cycle the phy.
869 * If at is non-NULL an indirect port via a port-multiplier is being
870 * reset, otherwise a direct port is being reset.
872 * NOTE: Indirect ports can only be soft-reset.
875 sili_port_reset(struct sili_port *ap, struct ata_port *at, int hard)
881 rc = sili_pm_hardreset(ap, at->at_target, hard);
883 rc = sili_port_hardreset(ap, hard);
886 rc = sili_pm_softreset(ap, at->at_target);
888 rc = sili_port_softreset(ap);
894 * SILI soft reset, Section 10.4.1
896 * (at) will be NULL when soft-resetting a directly-attached device, and
897 * non-NULL when soft-resetting a device through a port multiplier.
899 * This function keeps port communications intact and attempts to generate
900 * a reset to the connected device using device commands.
903 sili_port_softreset(struct sili_port *ap)
905 struct sili_ccb *ccb = NULL;
906 struct sili_prb *prb;
912 kprintf("%s: START SOFTRESET\n", PORTNAME(ap));
914 DPRINTF(SILI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
917 ap->ap_state = AP_S_NORMAL;
920 * Prep the special soft-reset SII command.
922 ccb = sili_get_err_ccb(ap);
923 ccb->ccb_done = sili_empty_done;
924 ccb->ccb_xa.flags = ATA_F_POLL;
925 ccb->ccb_xa.complete = sili_dummy_done;
926 ccb->ccb_xa.at = NULL;
929 bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
930 prb->prb_h2d.flags = 0;
931 prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
932 prb->prb_override = 0;
934 ccb->ccb_xa.state = ATA_S_PENDING;
935 ccb->ccb_xa.flags = 0;
938 if (sili_poll(ccb, 8000, sili_quick_timeout) != ATA_S_COMPLETE) {
939 kprintf("%s: First FIS failed\n", PORTNAME(ap));
943 sig = (prb->prb_d2h.lba_high << 24) |
944 (prb->prb_d2h.lba_mid << 16) |
945 (prb->prb_d2h.lba_low << 8) |
946 (prb->prb_d2h.sector_count);
947 kprintf("%s: SOFTRESET SIGNATURE %08x\n", PORTNAME(ap), sig);
950 * If the softreset is trying to clear a BSY condition after a
951 * normal portreset we assign the port type.
953 * If the softreset is being run first as part of the ccb error
954 * processing code then report if the device signature changed
957 if (ap->ap_type == ATA_PORT_T_NONE) {
958 ap->ap_type = sili_port_signature(ap, NULL, sig);
960 if (sili_port_signature(ap, NULL, sig) != ap->ap_type) {
961 kprintf("%s: device signature unexpectedly "
962 "changed\n", PORTNAME(ap));
963 error = EBUSY; /* XXX */
969 sili_put_err_ccb(ccb);
973 * If we failed to softreset make the port quiescent, otherwise
974 * make sure the port's start/stop state matches what it was on
977 * Don't kill the port if the softreset is on a port multiplier
978 * target, that would kill all the targets!
980 kprintf("%s: END SOFTRESET %d prob=%d state=%d\n", PORTNAME(ap), error, ap->ap_probe, ap->ap_state);
982 sili_port_hardstop(ap);
983 /* ap_probe set to failed */
985 ap->ap_probe = ATA_PROBE_NEED_IDENT;
990 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
996 * SILI port reset, Section 10.4.2
998 * This function does a hard reset of the port. Note that the device
999 * connected to the port could still end-up hung.
1002 sili_port_hardreset(struct sili_port *ap, int hard)
1008 DPRINTF(SILI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
1010 ap->ap_state = AP_S_NORMAL;
1014 * Issue Device Reset.
1016 * NOTE: Unlike Port Reset, the port ready signal will not
1017 * go active unless a device is established to be on
1020 sili_pwrite(ap, SILI_PREG_SERR, -1);
1021 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1022 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
1023 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
1024 if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
1025 kprintf("%s: hardreset failed to clear\n", PORTNAME(ap));
1029 * Try to determine if there is a device on the port.
1031 * Give the device 3/10 second to at least be detected.
1035 r = sili_pread(ap, SILI_PREG_SSTS);
1036 if (r & SILI_PREG_SSTS_DET)
1038 loop -= sili_os_softsleep();
1042 kprintf("%s: Port appears to be unplugged\n",
1049 * There is something on the port. Give the device 3 seconds
1050 * to fully negotiate.
1053 sili_pwait_eq(ap, 3000, SILI_PREG_SSTS,
1054 SILI_PREG_SSTS_DET, SILI_PREG_SSTS_DET_DEV)) {
1056 kprintf("%s: Device may be powered down\n",
1063 * Wait for the port to become ready.
1065 * This can take more then a second, give it 3 seconds. If we
1066 * succeed give the device another 3ms after that.
1068 * NOTE: Port multipliers can do two things here. First they can
1069 * return device-ready if a device is on target 0 and also
1070 * return the signature for that device. If there is no
1071 * device on target 0 then BSY/DRQ is never cleared and
1072 * it never comes ready.
1074 if (error == 0 && sili_pwait_set_to(ap, 3000, SILI_PREG_STATUS,
1075 SILI_PREG_STATUS_READY)) {
1077 * The device is bricked or its a port multiplier and will
1078 * not unbusy until we do the pmprobe CLO softreset sequence.
1080 error = sili_port_pmprobe(ap);
1082 kprintf("%s: Device will not come ready\n",
1085 ap->ap_type = ATA_PORT_T_PM;
1087 } else if (error == 0) {
1089 * The sili's hardreset doesn't return a signature (does it)?
1090 * In anycase, set the type so the signature gets set by
1091 * the softreset stage.
1093 error = sili_port_pmprobe(ap);
1095 ap->ap_type = ATA_PORT_T_NONE;
1098 ap->ap_type = ATA_PORT_T_PM;
1099 kprintf("%s: Port multiplier detected\n",
1105 * hard-stop the port if we failed. This will set ap_probe
1109 sili_port_hardstop(ap);
1110 /* ap_probe set to failed */
1112 if (ap->ap_type == ATA_PORT_T_PM)
1113 ap->ap_probe = ATA_PROBE_GOOD;
1115 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1121 * SILI port multiplier probe. This routine is run by the hardreset code
1122 * if it gets past the device detect.
1124 * All we do here is call sili_pm_softreset(). The Sili chip does all the
1127 * Return 0 on success, non-zero on failure.
1130 sili_port_pmprobe(struct sili_port *ap)
1132 struct ata_port *at;
1137 * If we don't support port multipliers don't try to detect one.
1139 if ((ap->ap_sc->sc_flags & SILI_F_SPM) == 0)
1143 * The port may be unhappy from its hardreset if there's a PM
1144 * but no device at target 0. If we try to shove the softreset
1145 * for target 15 down its throat it will pop a gasket.
1147 * Reiniting the port.. kind of a soft reset of its command
1148 * processor which otherwise does not effect the port registers,
1149 * seems to fix the problem.
1151 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_PMA);
1152 sili_port_reinit(ap);
1153 ap->ap_state = AP_S_NORMAL;
1154 error = sili_pm_softreset(ap, 15);
1156 ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
1161 kprintf("PMPROBE3 %d\n", error);
1163 if (error == 0 && sili_pm_identify(ap)) {
1164 kprintf("%s: PM - cannot identify port multiplier\n",
1168 kprintf("PMPROBE3 %d %d %d\n", error, ap->ap_probe, ap->ap_state);
1171 * If we probed the PM reset the state for the targets behind
1172 * it so they get probed by the state machine.
1175 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
1176 at = &ap->ap_ata[i];
1177 at->at_probe = ATA_PROBE_NEED_INIT;
1178 at->at_features |= ATA_PORT_F_RESCAN;
1179 at->at_features &= ~ATA_PORT_F_READLOG;
1184 * If we failed turn off PMA, otherwise identify the port multiplier.
1185 * CAM will iterate the devices.
1188 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1193 * Hard-stop on hot-swap device removal. See 10.10.1
1195 * Place the port in a mode that will allow it to detect hot-swap insertions.
1196 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1197 * seem to do the job.
1200 sili_port_hardstop(struct sili_port *ap)
1202 struct sili_ccb *ccb;
1203 struct ata_port *at;
1207 ap->ap_state = AP_S_FATAL_ERROR;
1208 ap->ap_probe = ATA_PROBE_FAILED;
1209 ap->ap_type = ATA_PORT_T_NONE;
1212 * Clean up AT sub-ports on SATA port.
1214 for (i = 0; ap->ap_ata && i < SILI_MAX_PMPORTS; ++i) {
1215 at = &ap->ap_ata[i];
1216 at->at_type = ATA_PORT_T_NONE;
1217 at->at_probe = ATA_PROBE_FAILED;
1218 at->at_features &= ~ATA_PORT_F_READLOG;
1222 * Kill the port. Don't bother waiting for it to transition
1225 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
1226 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
1227 kprintf("%s: Port will not go into reset\n",
1231 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
1232 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
1235 * Turn off port-multiplier control bit
1237 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1240 * Clean up the command list.
1242 while (ap->ap_active) {
1243 slot = ffs(ap->ap_active) - 1;
1244 ap->ap_active &= ~(1 << slot);
1245 ap->ap_expired &= ~(1 << slot);
1246 --ap->ap_active_cnt;
1247 ccb = &ap->ap_ccbs[slot];
1248 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1249 callout_stop(&ccb->ccb_timeout);
1250 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1252 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1253 ATA_F_TIMEOUT_EXPIRED);
1254 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1256 ccb->ccb_xa.complete(&ccb->ccb_xa);
1258 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1259 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1260 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1261 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1263 ccb->ccb_xa.complete(&ccb->ccb_xa);
1265 KKASSERT(ap->ap_active_cnt == 0);
1268 * Put the port into a listen mode, we want to get insertion/removal
1271 sili_port_listen(ap);
1275 * Place port into a listen mode for hotplug events only. The port has
1276 * already been reset and the command processor may not be ready due
1277 * to the lack of a device.
1280 sili_port_listen(struct sili_port *ap)
1285 data = SILI_PREG_SCTL_SPM_NONE |
1286 SILI_PREG_SCTL_IPM_NONE |
1287 SILI_PREG_SCTL_SPD_NONE |
1288 SILI_PREG_SCTL_DET_INIT;
1289 if (SiliForceGen1 & (1 << ap->ap_num)) {
1290 data &= ~SILI_PREG_SCTL_SPD_NONE;
1291 data |= SILI_PREG_SCTL_SPD_GEN1;
1294 sili_pwrite(ap, SILI_PREG_SERR, -1);
1295 sili_pwrite(ap, SILI_PREG_INT_ENABLE, SILI_PREG_INT_PHYRDYCHG |
1296 SILI_PREG_INT_DEVEXCHG);
1300 * Figure out what type of device is connected to the port, ATAPI or
1304 sili_port_signature(struct sili_port *ap, struct ata_port *at, u_int32_t sig)
1307 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1308 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1309 return(ATA_PORT_T_ATAPI);
1310 } else if ((sig & 0xffff0000) ==
1311 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1312 return(ATA_PORT_T_PM);
1314 return(ATA_PORT_T_DISK);
1319 * Load the DMA descriptor table for a CCB's buffer.
1321 * NOTE: ATA_F_PIO is auto-selected by sili part.
1324 sili_load_prb(struct sili_ccb *ccb)
1326 struct sili_port *ap = ccb->ccb_port;
1327 struct sili_softc *sc = ap->ap_sc;
1328 struct ata_xfer *xa = &ccb->ccb_xa;
1329 struct sili_prb *prb = ccb->ccb_prb;
1330 struct sili_sge *sge;
1331 bus_dmamap_t dmap = ccb->ccb_dmamap;
1335 * Set up the PRB. Set up the SGE that links to our
1336 * SGE array. We do not use the limited number of SGE's
1337 * in the Sili's LRAM.
1339 prb->prb_xfer_count = 0;
1340 prb->prb_control = 0;
1341 prb->prb_override = 0;
1342 sge = (ccb->ccb_xa.flags & ATA_F_PACKET) ?
1343 &prb->prb_sge_packet : &prb->prb_sge_normal;
1344 if (xa->datalen == 0) {
1345 sge->sge_flags = SILI_SGE_FLAGS_TRM | SILI_SGE_FLAGS_DRD;
1350 if (ccb->ccb_xa.flags & ATA_F_READ)
1351 prb->prb_control |= SILI_PRB_CTRL_READ;
1352 if (ccb->ccb_xa.flags & ATA_F_WRITE)
1353 prb->prb_control |= SILI_PRB_CTRL_WRITE;
1354 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1356 sge->sge_paddr = ccb->ccb_sge_paddr;
1359 * Load our external sge array.
1361 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1362 xa->data, xa->datalen,
1363 sili_load_prb_callback,
1365 ((xa->flags & ATA_F_NOWAIT) ?
1366 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1368 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1372 bus_dmamap_sync(sc->sc_tag_data, dmap,
1373 (xa->flags & ATA_F_READ) ?
1374 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1380 bus_dmamap_unload(sc->sc_tag_data, dmap);
1386 * Callback from BUSDMA system to load the segment list.
1388 * The scatter/gather table is loaded by the sili chip in blocks of
1389 * four SGE's. If a continuance is required the last entry in each
1390 * block must point to the next block.
1394 sili_load_prb_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1397 struct sili_ccb *ccb = info;
1398 struct sili_sge *sge;
1401 KKASSERT(nsegs <= SILI_MAX_SGET);
1406 if ((sgi & 3) == 3) {
1407 sge->sge_paddr = htole64(ccb->ccb_sge_paddr +
1408 sizeof(*sge) * (sgi + 1));
1410 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1412 sge->sge_paddr = htole64(segs->ds_addr);
1413 sge->sge_count = htole32(segs->ds_len);
1422 sge->sge_flags |= SILI_SGE_FLAGS_TRM;
1426 sili_unload_prb(struct sili_ccb *ccb)
1428 struct sili_port *ap = ccb->ccb_port;
1429 struct sili_softc *sc = ap->ap_sc;
1430 struct ata_xfer *xa = &ccb->ccb_xa;
1431 bus_dmamap_t dmap = ccb->ccb_dmamap;
1433 if (xa->datalen != 0) {
1434 bus_dmamap_sync(sc->sc_tag_data, dmap,
1435 (xa->flags & ATA_F_READ) ?
1436 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1438 bus_dmamap_unload(sc->sc_tag_data, dmap);
1440 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1443 xa->resid = xa->datalen -
1444 le32toh(ccb->ccb_prb->prb_xfer_count);
1449 * Start a command and poll for completion.
1451 * timeout is in ms and only counts once the command gets on-chip.
1453 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1454 * that no error occured.
1456 * NOTE: If the caller specifies a NULL timeout function the caller is
1457 * responsible for clearing hardware state on failure, but we will
1458 * deal with removing the ccb from any pending queue.
1460 * NOTE: NCQ should never be used with this function.
1462 * NOTE: If the port is in a failed state and stopped we do not try
1463 * to activate the ccb.
1466 sili_poll(struct sili_ccb *ccb, int timeout,
1467 void (*timeout_fn)(struct sili_ccb *))
1469 struct sili_port *ap = ccb->ccb_port;
1471 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1472 ccb->ccb_xa.state = ATA_S_ERROR;
1473 return(ccb->ccb_xa.state);
1479 sili_port_intr(ap, 1);
1480 switch(ccb->ccb_xa.state) {
1482 timeout -= sili_os_softsleep();
1486 * The packet can get stuck on the pending queue
1487 * if the port refuses to come ready.
1489 if (AP_F_EXCLUSIVE_ACCESS)
1490 timeout -= sili_os_softsleep();
1492 sili_os_softsleep();
1493 sili_check_active_timeouts(ap);
1496 return (ccb->ccb_xa.state);
1498 } while (timeout > 0);
1500 kprintf("%s: Poll timeout slot %d\n",
1501 ATANAME(ap, ccb->ccb_xa.at),
1506 return(ccb->ccb_xa.state);
1510 * When polling we have to check if the currently active CCB(s)
1511 * have timed out as the callout will be deadlocked while we
1512 * hold the port lock.
1515 sili_check_active_timeouts(struct sili_port *ap)
1517 struct sili_ccb *ccb;
1521 mask = ap->ap_active;
1523 tag = ffs(mask) - 1;
1524 mask &= ~(1 << tag);
1525 ccb = &ap->ap_ccbs[tag];
1526 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1527 sili_core_timeout(ccb);
1535 sili_start_timeout(struct sili_ccb *ccb)
1537 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1538 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1539 callout_reset(&ccb->ccb_timeout,
1540 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1541 sili_ata_cmd_timeout_unserialized, ccb);
1546 sili_start(struct sili_ccb *ccb)
1548 struct sili_port *ap = ccb->ccb_port;
1550 struct sili_softc *sc = ap->ap_sc;
1553 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1556 * Sync our SGE table and PRB
1558 bus_dmamap_sync(ap->ap_dmamem_sget->adm_tag,
1559 ap->ap_dmamem_sget->adm_map,
1560 BUS_DMASYNC_PREWRITE);
1563 * XXX dmamap for PRB XXX BUS_DMASYNC_PREWRITE
1567 * Controller will update shared memory!
1568 * XXX bus_dmamap_sync ... BUS_DMASYNC_PREREAD ...
1570 /* Prepare RFIS area for write by controller */
1573 * There's no point trying to optimize this, it only shaves a few
1574 * nanoseconds so just queue the command and call our generic issue.
1576 sili_issue_pending_commands(ap, ccb);
1580 * While holding the port lock acquire exclusive access to the port.
1582 * This is used when running the state machine to initialize and identify
1583 * targets over a port multiplier. Setting exclusive access prevents
1584 * sili_port_intr() from activating any requests sitting on the pending
1588 sili_beg_exclusive_access(struct sili_port *ap, struct ata_port *at)
1590 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1591 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1592 while (ap->ap_active) {
1593 sili_port_intr(ap, 1);
1594 sili_os_softsleep();
1599 sili_end_exclusive_access(struct sili_port *ap, struct ata_port *at)
1601 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
1602 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
1603 sili_issue_pending_commands(ap, NULL);
1607 * If ccb is not NULL enqueue and/or issue it.
1609 * If ccb is NULL issue whatever we can from the queue. However, nothing
1610 * new is issued if the exclusive access flag is set or expired ccb's are
1613 * If existing commands are still active (ap_active) we can only
1614 * issue matching new commands.
1617 sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb)
1622 * If just running the queue and in exclusive access mode we
1623 * just return. Also in this case if there are any expired ccb's
1624 * we want to clear the queue so the port can be safely stopped.
1626 * XXX sili chip - expiration needs to be per-target if PM supports
1630 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1631 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
1636 * Pull the next ccb off the queue and run it if possible.
1637 * If the port is not ready to accept commands enable the
1638 * ready interrupt instead of starting a new command.
1640 * XXX limit ncqdepth for attached devices behind PM
1642 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1643 if ((sili_pread(ap, SILI_PREG_STATUS) &
1644 SILI_PREG_STATUS_READY) == 0) {
1645 kprintf("%s: slot %d NOT READY\n",
1646 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
1647 sili_pwrite(ap, SILI_PREG_INT_ENABLE,
1648 SILI_PREG_INT_READY);
1651 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1652 ccb->ccb_xa.state = ATA_S_ONCHIP;
1653 ap->ap_active |= 1 << ccb->ccb_slot;
1654 ap->ap_active_cnt++;
1655 sili_pwrite(ap, SILI_PREG_CMD_FIFO, ccb->ccb_slot);
1656 sili_start_timeout(ccb);
1661 sili_intr(void *arg)
1663 struct sili_softc *sc = arg;
1664 struct sili_port *ap;
1669 * Check if the master enable is up, and whether any interrupts are
1672 * Clear the ints we got.
1674 if ((sc->sc_flags & SILI_F_INT_GOOD) == 0)
1676 gint = sili_read(sc, SILI_REG_GINT);
1677 if (gint == 0 || gint == 0xffffffff)
1679 sili_write(sc, SILI_REG_GINT, gint);
1682 * Process interrupts for each port in a non-blocking fashion.
1684 while (gint & SILI_REG_GINT_PORTMASK) {
1685 port = ffs(gint) - 1;
1686 ap = sc->sc_ports[port];
1688 if (sili_os_lock_port_nb(ap) == 0) {
1689 sili_port_intr(ap, 0);
1690 sili_os_unlock_port(ap);
1692 sili_port_interrupt_redisable(ap);
1693 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1696 gint &= ~(1 << port);
1701 * Core called from helper thread.
1704 sili_port_thread_core(struct sili_port *ap, int mask)
1707 * Process any expired timedouts.
1709 sili_os_lock_port(ap);
1710 if (mask & AP_SIGF_TIMEOUT) {
1711 sili_check_active_timeouts(ap);
1715 * Process port interrupts which require a higher level of
1718 if (mask & AP_SIGF_PORTINT) {
1719 sili_port_intr(ap, 1);
1720 sili_port_interrupt_reenable(ap);
1721 sili_os_unlock_port(ap);
1723 sili_os_unlock_port(ap);
1728 * Core per-port interrupt handler.
1730 * If blockable is 0 we cannot call sili_os_sleep() at all and we can only
1731 * deal with normal command completions which do not require blocking.
1734 sili_port_intr(struct sili_port *ap, int blockable)
1736 struct sili_softc *sc = ap->ap_sc;
1739 struct sili_ccb *ccb = NULL;
1740 struct ata_port *ccb_at = NULL;
1745 const u_int32_t blockable_mask = SILI_PREG_IST_PHYRDYCHG |
1746 SILI_PREG_IST_DEVEXCHG |
1747 SILI_PREG_IST_CERROR |
1748 SILI_PREG_IST_DECODE |
1750 SILI_PREG_IST_HANDSHK;
1751 const u_int32_t fatal_mask = SILI_PREG_IST_PHYRDYCHG |
1752 SILI_PREG_IST_DEVEXCHG |
1753 SILI_PREG_IST_DECODE |
1755 SILI_PREG_IST_HANDSHK;
1757 enum { NEED_NOTHING, NEED_HOTPLUG_INSERT,
1758 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1761 * NOTE: CCOMPLETE was automatically cleared when we read INT_STATUS.
1763 is = sili_pread(ap, SILI_PREG_INT_STATUS);
1764 is &= SILI_PREG_IST_MASK;
1765 if (is & SILI_PREG_IST_CCOMPLETE)
1766 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CCOMPLETE);
1769 * If we can't block then we can't handle these here. Disable
1770 * the interrupts in question so we don't live-lock, the helper
1771 * thread will re-enable them.
1773 * If the port is in a completely failed state we do not want
1774 * to drop through to failed-command-processing if blockable is 0,
1775 * just let the thread deal with it all.
1777 * Otherwise we fall through and still handle DHRS and any commands
1778 * which completed normally. Even if we are errored we haven't
1779 * stopped the port yet so CI/SACT are still good.
1781 if (blockable == 0) {
1782 if (ap->ap_state == AP_S_FATAL_ERROR) {
1783 sili_port_interrupt_redisable(ap);
1784 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1785 /*is &= ~blockable_mask;*/
1788 if (is & blockable_mask) {
1789 sili_port_interrupt_redisable(ap);
1790 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1791 /*is &= ~blockable_mask;*/
1796 if (is & SILI_PREG_IST_CERROR) {
1798 * Command failed (blockable).
1800 * This stops command processing. We can extract the PM
1801 * target from the PMP field in SILI_PREG_CONTEXT. The
1802 * tag is not necessarily valid so don't use that.
1804 * We must then expire all CCB's for that target and resume
1805 * processing if any other targets have active commands.
1806 * Particular error codes can be recovered by reading the LOG
1809 * The expire handling code will do the rest, which is
1810 * basically to reset the port once the only active
1811 * commands remaining are all expired.
1817 target = (sili_pread(ap, SILI_PREG_CONTEXT) >>
1818 SILI_PREG_CONTEXT_PMPORT_SHIFT) &
1819 SILI_PREG_CONTEXT_PMPORT_MASK;
1820 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CERROR);
1821 active = ap->ap_active & ~ap->ap_expired;
1822 error = sili_pread(ap, SILI_PREG_CERROR);
1823 kprintf("%s.%d target error %d active=%08x hactive=%08x "
1825 PORTNAME(ap), target, error,
1826 active, sili_pread(ap, SILI_PREG_SLOTST),
1827 sili_pread(ap, SILI_PREG_SERR), SILI_PFMT_SERR);
1830 slot = ffs(active) - 1;
1831 ccb = &ap->ap_ccbs[slot];
1832 if ((ccb_at = ccb->ccb_xa.at) == NULL)
1833 ccb_at = &ap->ap_ata[0];
1834 if (target == ccb_at->at_target) {
1835 kprintf("%s kill ccb slot %d\n",
1836 ATANAME(ap, ccb->ccb_xa.at), slot);
1837 if (ccb->ccb_xa.flags & ATA_F_NCQ &&
1838 (error == SILI_PREG_CERROR_DEVICE ||
1839 error == SILI_PREG_CERROR_SDBERROR)) {
1840 ccb_at->at_features |= ATA_PORT_F_READLOG;
1842 if (sili_core_timeout(ccb) == 0)
1845 active &= ~(1 << slot);
1849 * Resume will be 0 if the timeout reinited and restarted
1850 * the port. Otherwise we resume the port to allow other
1851 * commands to complete.
1853 kprintf("%s.%d remain=%08x resume=%d\n",
1854 PORTNAME(ap), target,
1855 ap->ap_active & ~ap->ap_expired, resume);
1857 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESUME);
1861 * Device notification to us (non-blocking)
1863 * This is interrupt status SILIPREG_IST_SDB
1865 * NOTE! On some parts notification bits can get set without
1866 * generating an interrupt. It is unclear whether this is
1867 * a bug in the PM (sending a DTOH device setbits with 'N' set
1868 * and 'I' not set), or a bug in the host controller.
1870 * It only seems to occur under load.
1872 if (sc->sc_flags & SILI_F_SSNTF) {
1876 data = sili_pread(ap, SILI_PREG_SNTF);
1877 if (is & SILI_PREG_IST_SDB) {
1878 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1880 is &= ~SILI_PREG_IST_SDB;
1881 xstr = " (no SDBS!)";
1886 kprintf("%s: NOTIFY %08x%s\n",
1887 PORTNAME(ap), data, xstr);
1888 sili_pwrite(ap, SILI_PREG_SNTF, data);
1889 sili_cam_changed(ap, NULL, -1);
1894 * Port change (hot-plug) (blockable).
1896 * A PCS interrupt will occur on hot-plug once communication is
1899 * A PRCS interrupt will occur on hot-unplug (and possibly also
1902 * XXX We can then check the CPS (Cold Presence State) bit, if
1903 * supported, to determine if a device is plugged in or not and do
1906 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
1907 * can also occur if an unsolicited COMINIT is received.
1908 * If this occurs command processing is automatically
1909 * stopped (CR goes inactive) and the port must be stopped
1912 if (is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG)) {
1914 sili_pwrite(ap, SILI_PREG_SERR,
1915 (SILI_PREG_SERR_DIAG_N | SILI_PREG_SERR_DIAG_X));
1916 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1917 is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG));
1919 is &= ~(SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG);
1920 kprintf("%s: Port change\n", PORTNAME(ap));
1922 switch (sili_pread(ap, SILI_PREG_SSTS) & SILI_PREG_SSTS_DET) {
1923 case SILI_PREG_SSTS_DET_DEV:
1924 if (ap->ap_type == ATA_PORT_T_NONE &&
1925 ap->ap_probe == ATA_PROBE_FAILED) {
1926 need = NEED_HOTPLUG_INSERT;
1931 kprintf("%s: Device lost\n", PORTNAME(ap));
1932 if (ap->ap_type != ATA_PORT_T_NONE) {
1933 need = NEED_HOTPLUG_REMOVE;
1941 * Check for remaining errors - they are fatal. (blockable)
1943 if (is & fatal_mask) {
1946 sili_pwrite(ap, SILI_PREG_INT_STATUS, is & fatal_mask);
1948 serr = sili_pread(ap, SILI_PREG_SERR);
1949 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
1950 "disabling port.\n",
1952 is, SILI_PFMT_INT_STATUS,
1953 serr, SILI_PFMT_SERR
1956 /* XXX try recovery first */
1961 * Fail all outstanding commands if we know the port won't recover.
1963 * We may have a ccb_at if the failed command is known and was
1964 * being sent to a device over a port multiplier (PM). In this
1965 * case if the port itself has not completely failed we fail just
1966 * the commands related to that target.
1968 if (ap->ap_state == AP_S_FATAL_ERROR && ap->ap_active) {
1970 kprintf("%s: Interrupt, fatal error\n", PORTNAME(ap));
1971 ap->ap_state = AP_S_FATAL_ERROR;
1974 * Error all the active slots. If running across a PM
1975 * try to error out just the slots related to the target.
1977 active = ap->ap_active & ~ap->ap_expired;
1980 slot = ffs(active) - 1;
1981 kprintf("%s: Killing slot %d\n", PORTNAME(ap), slot);
1982 active &= ~(1 << slot);
1983 ccb = &ap->ap_ccbs[slot];
1984 sili_core_timeout(ccb);
1989 * CCB completion (non blocking).
1991 * CCB completion is detected by noticing the slot bit in
1992 * the port slot status register has cleared while the bit
1993 * is still set in our ap_active variable.
1995 * When completing expired events we must remember to reinit
1996 * the port once everything is clear.
1998 active = ap->ap_active & ~sili_pread(ap, SILI_PREG_SLOTST);
2001 slot = ffs(active) - 1;
2002 ccb = &ap->ap_ccbs[slot];
2004 DPRINTF(SILI_D_INTR, "%s: slot %d is complete%s\n",
2005 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2008 active &= ~(1 << slot);
2011 * XXX sync POSTREAD for return data?
2013 ap->ap_active &= ~(1 << ccb->ccb_slot);
2014 --ap->ap_active_cnt;
2017 * Complete the ccb. If the ccb was marked expired it
2018 * may or may not have been cleared from the port,
2019 * make sure we mark it as having timed out.
2021 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2022 ap->ap_expired &= ~(1 << ccb->ccb_slot);
2023 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2025 ccb->ccb_xa.complete(&ccb->ccb_xa);
2027 if (ccb->ccb_xa.state == ATA_S_ONCHIP)
2028 ccb->ccb_xa.state = ATA_S_COMPLETE;
2032 if (is & SILI_PREG_IST_READY) {
2033 is &= ~SILI_PREG_IST_READY;
2034 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_READY);
2035 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_READY);
2039 * If we had expired commands and were waiting for
2040 * remaining commands to complete, and they have now
2041 * completed, we can reinit the port.
2043 * This will also clean out the expired commands.
2044 * The timeout code also calls sili_port_reinit() if
2045 * the only commands remaining after a timeout are all
2046 * now expired commands.
2048 * Otherwise just reissue.
2050 if (ap->ap_expired && ap->ap_active == ap->ap_expired)
2051 sili_port_reinit(ap);
2053 sili_issue_pending_commands(ap, NULL);
2056 * Cleanup. Will not be set if non-blocking.
2059 case NEED_HOTPLUG_INSERT:
2061 * A hot-plug insertion event has occured and all
2062 * outstanding commands have already been revoked.
2064 * Don't recurse if this occurs while we are
2065 * resetting the port.
2067 * Place the port in a continuous COMRESET state
2068 * until the INIT code gets to it.
2070 kprintf("%s: HOTPLUG - Device inserted\n",
2072 ap->ap_probe = ATA_PROBE_NEED_INIT;
2073 sili_cam_changed(ap, NULL, -1);
2075 case NEED_HOTPLUG_REMOVE:
2077 * A hot-plug removal event has occured and all
2078 * outstanding commands have already been revoked.
2080 * Don't recurse if this occurs while we are
2081 * resetting the port.
2083 kprintf("%s: HOTPLUG - Device removed\n",
2085 sili_port_hardstop(ap);
2086 /* ap_probe set to failed */
2087 sili_cam_changed(ap, NULL, -1);
2095 sili_get_ccb(struct sili_port *ap)
2097 struct sili_ccb *ccb;
2099 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2100 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2102 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2103 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2104 ccb->ccb_xa.state = ATA_S_SETUP;
2105 ccb->ccb_xa.at = NULL;
2107 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2113 sili_put_ccb(struct sili_ccb *ccb)
2115 struct sili_port *ap = ccb->ccb_port;
2118 if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2119 ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2120 ccb->ccb_xa.state != ATA_S_ERROR) {
2121 kprintf("%s: invalid ata_xfer state %02x in sili_put_ccb, "
2123 PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2128 ccb->ccb_xa.state = ATA_S_PUT;
2129 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2130 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2131 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2135 sili_get_err_ccb(struct sili_port *ap)
2137 struct sili_ccb *err_ccb;
2139 KKASSERT(sili_pread(ap, SILI_PREG_CI) == 0);
2140 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2141 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2144 KKASSERT(ap->ap_err_busy == 0);
2145 ap->ap_err_busy = 1;
2148 * Grab a CCB to use for error recovery. This should never fail, as
2149 * we ask atascsi to reserve one for us at init time.
2151 err_ccb = ap->ap_err_ccb;
2152 KKASSERT(err_ccb != NULL);
2153 err_ccb->ccb_xa.flags = 0;
2154 err_ccb->ccb_done = sili_empty_done;
2160 sili_put_err_ccb(struct sili_ccb *ccb)
2162 struct sili_port *ap = ccb->ccb_port;
2165 KKASSERT(ap->ap_err_busy);
2167 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2169 KKASSERT(ccb == ap->ap_err_ccb);
2172 ap->ap_err_busy = 0;
2174 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2178 * Read log page to get NCQ error.
2181 sili_port_read_ncq_error(struct sili_port *ap, int target)
2183 struct sili_ccb *ccb;
2184 struct ata_fis_h2d *fis;
2187 DPRINTF(SILI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2189 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2190 ccb = sili_get_err_ccb(ap);
2191 ccb->ccb_done = sili_empty_done;
2192 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2193 ccb->ccb_xa.data = ap->ap_err_scratch;
2194 ccb->ccb_xa.datalen = 512;
2195 ccb->ccb_xa.complete = sili_dummy_done;
2196 ccb->ccb_xa.at = &ap->ap_ata[target];
2197 fis = &ccb->ccb_prb->prb_h2d;
2198 bzero(fis, sizeof(*fis));
2200 fis->type = ATA_FIS_TYPE_H2D;
2201 fis->flags = ATA_H2D_FLAGS_CMD | target;
2202 fis->command = ATA_C_READ_LOG_EXT;
2203 fis->lba_low = 0x10; /* queued error log page (10h) */
2204 fis->sector_count = 1; /* number of sectors (1) */
2205 fis->sector_count_exp = 0;
2206 fis->lba_mid = 0; /* starting offset */
2207 fis->lba_mid_exp = 0;
2210 if (sili_load_prb(ccb) != 0) {
2211 rc = ENOMEM; /* XXX caller must abort all commands */
2213 ccb->ccb_xa.state = ATA_S_PENDING;
2214 rc = sili_poll(ccb, 1000, sili_quick_timeout);
2217 /* Abort our command, if it failed, by stopping command DMA. */
2219 kprintf("%s: log page read failed, slot %d was still active.\n",
2220 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
2223 /* Done with the error CCB now. */
2224 sili_unload_prb(ccb);
2225 sili_put_err_ccb(ccb);
2227 /* Extract failed register set and tags from the scratch space. */
2229 struct ata_log_page_10h *log;
2232 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2233 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2234 /* Not queued bit was set - wasn't an NCQ error? */
2235 kprintf("%s: read NCQ error page, but not an NCQ "
2240 /* Copy back the log record as a D2H register FIS. */
2241 err_slot = log->err_regs.type &
2242 ATA_LOG_10H_TYPE_TAG_MASK;
2243 ccb = &ap->ap_ccbs[err_slot];
2244 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2245 kprintf("%s: read NCQ error page ok\n",
2246 ATANAME(ap, ccb->ccb_xa.at));
2247 memcpy(&ccb->ccb_prb->prb_d2h, &log->err_regs,
2248 sizeof(struct ata_fis_d2h));
2249 ccb->ccb_prb->prb_d2h.type = ATA_FIS_TYPE_D2H;
2250 ccb->ccb_prb->prb_d2h.flags = 0;
2252 kprintf("%s: error log slot %d did not match a failed ccb!\n", ATANAME(ccb->ccb_port, ccb->ccb_xa.at), err_slot);
2261 * Allocate memory for various structures DMAd by hardware. The maximum
2262 * number of segments for these tags is 1 so the DMA memory will have a
2263 * single physical base address.
2265 struct sili_dmamem *
2266 sili_dmamem_alloc(struct sili_softc *sc, bus_dma_tag_t tag)
2268 struct sili_dmamem *adm;
2271 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2273 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2274 BUS_DMA_ZERO, &adm->adm_map);
2277 error = bus_dmamap_load(tag, adm->adm_map,
2279 bus_dma_tag_getmaxsize(tag),
2280 sili_dmamem_saveseg, &adm->adm_busaddr,
2285 bus_dmamap_destroy(tag, adm->adm_map);
2286 adm->adm_map = NULL;
2287 adm->adm_tag = NULL;
2288 adm->adm_kva = NULL;
2290 kfree(adm, M_DEVBUF);
2298 sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2300 KKASSERT(error == 0);
2301 KKASSERT(nsegs == 1);
2302 *(bus_addr_t *)info = segs->ds_addr;
2307 sili_dmamem_free(struct sili_softc *sc, struct sili_dmamem *adm)
2310 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2311 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2312 adm->adm_map = NULL;
2313 adm->adm_tag = NULL;
2314 adm->adm_kva = NULL;
2316 kfree(adm, M_DEVBUF);
2320 sili_read(struct sili_softc *sc, bus_size_t r)
2322 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2323 BUS_SPACE_BARRIER_READ);
2324 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2328 sili_write(struct sili_softc *sc, bus_size_t r, u_int32_t v)
2330 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2331 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2332 BUS_SPACE_BARRIER_WRITE);
2336 sili_pread(struct sili_port *ap, bus_size_t r)
2338 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2339 BUS_SPACE_BARRIER_READ);
2340 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2344 sili_pwrite(struct sili_port *ap, bus_size_t r, u_int32_t v)
2346 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2347 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2348 BUS_SPACE_BARRIER_WRITE);
2352 * Wait up to (timeout) milliseconds for the masked port register to
2355 * Timeout is in milliseconds.
2358 sili_pwait_eq(struct sili_port *ap, int timeout,
2359 bus_size_t r, u_int32_t mask, u_int32_t target)
2364 * Loop hard up to 100uS
2366 for (t = 0; t < 100; ++t) {
2367 if ((sili_pread(ap, r) & mask) == target)
2369 sili_os_hardsleep(1); /* us */
2373 timeout -= sili_os_softsleep();
2374 if ((sili_pread(ap, r) & mask) == target)
2376 } while (timeout > 0);
2381 sili_wait_ne(struct sili_softc *sc, bus_size_t r, u_int32_t mask,
2387 * Loop hard up to 100uS
2389 for (t = 0; t < 100; ++t) {
2390 if ((sili_read(sc, r) & mask) != target)
2392 sili_os_hardsleep(1); /* us */
2396 * And one millisecond the slow way
2400 t -= sili_os_softsleep();
2401 if ((sili_read(sc, r) & mask) != target)
2410 * Acquire an ata transfer.
2412 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2413 * targets that go through the port multiplier.
2416 sili_ata_get_xfer(struct sili_port *ap, struct ata_port *at)
2418 struct sili_ccb *ccb;
2420 ccb = sili_get_ccb(ap);
2422 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer: NULL ccb\n",
2427 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer got slot %d\n",
2428 PORTNAME(ap), ccb->ccb_slot);
2430 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
2431 ccb->ccb_xa.at = at;
2432 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2434 return (&ccb->ccb_xa);
2438 sili_ata_put_xfer(struct ata_xfer *xa)
2440 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2442 DPRINTF(SILI_D_XFER, "sili_ata_put_xfer slot %d\n", ccb->ccb_slot);
2448 sili_ata_cmd(struct ata_xfer *xa)
2450 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2452 KKASSERT(xa->state == ATA_S_SETUP);
2454 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2457 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2458 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2459 sili_pread(ccb->ccb_port, SILI_PREG_CMD), SILI_PFMT_CMD,
2462 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2465 ccb->ccb_done = sili_ata_cmd_done;
2467 if (sili_load_prb(ccb) != 0)
2470 xa->state = ATA_S_PENDING;
2472 if (xa->flags & ATA_F_POLL)
2473 return (sili_poll(ccb, xa->timeout, sili_ata_cmd_timeout));
2476 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
2477 xa->flags |= ATA_F_TIMEOUT_DESIRED;
2484 xa->state = ATA_S_ERROR;
2487 return (ATA_S_ERROR);
2491 sili_ata_cmd_done(struct sili_ccb *ccb)
2493 struct ata_xfer *xa = &ccb->ccb_xa;
2496 * NOTE: callout does not lock port and may race us modifying
2497 * the flags, so make sure its stopped.
2499 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2500 callout_stop(&ccb->ccb_timeout);
2501 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2503 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
2505 KKASSERT(xa->state != ATA_S_ONCHIP);
2506 sili_unload_prb(ccb);
2509 else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
2510 kprintf("%s: invalid ata_xfer state %02x in sili_ata_cmd_done, "
2512 PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
2514 if (xa->state != ATA_S_TIMEOUT)
2519 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
2520 * while the callout is runing.
2522 * We can't safely get the port lock here or delay, we could block
2523 * the callout thread.
2526 sili_ata_cmd_timeout_unserialized(void *arg)
2528 struct sili_ccb *ccb = arg;
2529 struct sili_port *ap = ccb->ccb_port;
2531 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
2532 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
2533 sili_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
2537 sili_ata_cmd_timeout(struct sili_ccb *ccb)
2539 sili_core_timeout(ccb);
2543 * Timeout code, typically called when the port command processor is running.
2545 * Returns 0 if all timeout processing completed, non-zero if it is still
2550 sili_core_timeout(struct sili_ccb *ccb)
2552 struct ata_xfer *xa = &ccb->ccb_xa;
2553 struct sili_port *ap = ccb->ccb_port;
2554 struct ata_port *at;
2556 at = ccb->ccb_xa.at;
2558 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
2563 ccb->ccb_xa.state, ccb->ccb_slot,
2566 sili_pread(ap, SILI_PREG_SLOTST)
2570 * NOTE: Timeout will not be running if the command was polled.
2571 * If we got here at least one of these flags should be set.
2573 * However, it might be running if we are called from the
2574 * interrupt error handling code.
2576 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2577 ATA_F_TIMEOUT_RUNNING));
2578 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2579 callout_stop(&ccb->ccb_timeout);
2580 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2582 xa->flags &= ~ATA_F_TIMEOUT_EXPIRED;
2584 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2585 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2586 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2589 sili_issue_pending_commands(ap, NULL);
2592 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2593 kprintf("%s: Unexpected state during timeout: %d\n",
2594 ATANAME(ap, at), ccb->ccb_xa.state);
2599 * We can't process timeouts while other commands are running.
2601 ap->ap_expired |= 1 << ccb->ccb_slot;
2603 if (ap->ap_active != ap->ap_expired) {
2604 kprintf("%s: Deferred timeout until its safe, slot %d\n",
2605 ATANAME(ap, at), ccb->ccb_slot);
2610 * We have to issue a Port reinit. We don't read an error log
2611 * page for timeouts. Reiniting the port will clear all pending
2614 sili_port_reinit(ap);
2619 * Used by the softreset, pmprobe, and read_ncq_error only, in very
2620 * specialized, controlled circumstances.
2623 sili_quick_timeout(struct sili_ccb *ccb)
2625 struct sili_port *ap = ccb->ccb_port;
2627 switch (ccb->ccb_xa.state) {
2629 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2630 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2633 KKASSERT((ap->ap_active & ~ap->ap_expired) ==
2634 (1 << ccb->ccb_slot));
2635 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2636 ap->ap_active &= ~(1 << ccb->ccb_slot);
2637 KKASSERT(ap->ap_active_cnt > 0);
2638 --ap->ap_active_cnt;
2639 sili_port_reinit(ap);
2642 panic("%s: sili_quick_timeout: ccb in bad state %d",
2643 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
2648 sili_dummy_done(struct ata_xfer *xa)
2653 sili_empty_done(struct sili_ccb *ccb)