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_prb *prb;
137 struct sili_ccb *ccb;
142 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
144 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
145 device_get_name(sc->sc_dev),
146 device_get_unit(sc->sc_dev),
148 sc->sc_ports[port] = ap;
149 kprintf("%s: allocate port\n", PORTNAME(ap));
152 * Allocate enough so we never have to reallocate, it makes
155 * ap_pmcount will be reduced by the scan if we encounter the
156 * port multiplier port prior to target 15.
158 if (ap->ap_ata == NULL) {
159 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * SILI_MAX_PMPORTS,
160 M_DEVBUF, M_INTWAIT | M_ZERO);
161 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
163 at->at_sili_port = ap;
165 at->at_probe = ATA_PROBE_NEED_INIT;
166 at->at_features |= ATA_PORT_F_RESCAN;
167 ksnprintf(at->at_name, sizeof(at->at_name),
168 "%s.%d", ap->ap_name, i);
171 if (bus_space_subregion(sc->sc_piot, sc->sc_pioh,
172 SILI_PORT_REGION(port), SILI_PORT_SIZE,
174 device_printf(sc->sc_dev,
175 "unable to create register window for port %d\n",
182 ap->ap_probe = ATA_PROBE_NEED_INIT;
183 TAILQ_INIT(&ap->ap_ccb_free);
184 TAILQ_INIT(&ap->ap_ccb_pending);
185 lockinit(&ap->ap_ccb_lock, "silipo", 0, 0);
187 /* Disable port interrupts */
188 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
191 * Reset the port. This is similar to a Device Reset but far
192 * more invasive. We use Device Reset in our hardreset function.
193 * This function also does the same OOB initialization sequence
194 * that Device Reset does.
196 * NOTE: SILI_PREG_STATUS_READY will not be asserted unless and until
197 * a device is connected to the port, so we can't use it to
198 * verify that the port exists.
200 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
201 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
202 device_printf(sc->sc_dev,
203 "Port %d will not go into reset\n", port);
207 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
208 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
211 * Adjust FIFO thresholds to improve PCI-e use.
213 sili_pwrite(ap, SILI_PREG_FIFO_CTL,
214 SILI_PREG_FIFO_CTL_ENCODE(1024, 1024));
217 * Allocate the SGE Table
219 ap->ap_dmamem_prbs = sili_dmamem_alloc(sc, sc->sc_tag_prbs);
220 if (ap->ap_dmamem_prbs == NULL) {
221 kprintf("%s: NOSGET\n", PORTNAME(ap));
226 * Set up the SGE table base address
228 ap->ap_prbs = (struct sili_prb *)SILI_DMA_KVA(ap->ap_dmamem_prbs);
231 * Allocate a CCB for each command slot
233 ap->ap_ccbs = kmalloc(sizeof(struct sili_ccb) * sc->sc_ncmds, M_DEVBUF,
235 if (ap->ap_ccbs == NULL) {
236 device_printf(sc->sc_dev,
237 "unable to allocate command list for port %d\n",
243 * Port control register setup.
245 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_NOAUTOCC);
246 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_32BITDMA |
248 SILI_PREG_CTL_NOAUTOCC);
251 * Most structures are in the port BAR. Assign convenient
252 * pointers in the CCBs
255 for (i = 0; i < sc->sc_ncmds; i++) {
256 ccb = &ap->ap_ccbs[i];
258 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
261 device_printf(sc->sc_dev,
262 "unable to create dmamap for port %d "
263 "ccb %d\n", port, i);
268 * WARNING!!! Access to the rfis is only allowed under very
269 * carefully controlled circumstances because it
270 * is located in the LRAM and reading from the
271 * LRAM has hardware issues which can blow the
272 * port up. I kid you not (from Linux, and
273 * verified by testing here).
275 callout_init(&ccb->ccb_timeout);
278 ccb->ccb_prb = &ap->ap_prbs[i];
279 ccb->ccb_prb_paddr = SILI_DMA_DVA(ap->ap_dmamem_prbs) +
280 sizeof(*ccb->ccb_prb) * i;
281 ccb->ccb_xa.fis = &ccb->ccb_prb->prb_h2d;
282 prb = bus_space_kva(ap->ap_sc->sc_iot, ap->ap_ioh,
283 SILI_PREG_LRAM_SLOT(i));
284 ccb->ccb_xa.rfis = &prb->prb_d2h;
285 ccb->ccb_xa.packetcmd = prb_packet(ccb->ccb_prb);
288 ccb->ccb_xa.state = ATA_S_COMPLETE;
291 * Reserve CCB[1] as the error CCB. It doesn't matter
292 * which one we use for the Sili controllers.
295 ap->ap_err_ccb = ccb;
299 kprintf("%s: start port\n", PORTNAME(ap));
300 sili_os_start_port(ap);
303 sili_port_free(sc, port);
308 * [re]initialize an idle port. No CCBs should be active.
310 * If at is NULL we are initializing a directly connected port, otherwise
311 * we are indirectly initializing a port multiplier port.
313 * This function is called during the initial port allocation sequence
314 * and is also called on hot-plug insertion. We take no chances and
315 * use a hardreset instead of a softreset.
317 * This function is the only way to move a failed port back to active
320 * Returns 0 if a device is successfully detected.
323 sili_port_init(struct sili_port *ap, struct ata_port *atx)
329 * Clear all notification bits
331 if (atx == NULL && (ap->ap_sc->sc_flags & SILI_F_SSNTF))
332 sili_pwrite(ap, SILI_PREG_SNTF, -1);
335 * Make sure the port is out of continuous COMRESET mode.
337 data = SILI_PREG_SCTL_SPM_NONE |
338 SILI_PREG_SCTL_IPM_NONE |
339 SILI_PREG_SCTL_SPD_NONE |
340 SILI_PREG_SCTL_DET_NONE;
341 if (SiliForceGen1 & (1 << ap->ap_num)) {
342 data &= ~SILI_PREG_SCTL_SPD_NONE;
343 data |= SILI_PREG_SCTL_SPD_GEN1;
345 sili_pwrite(ap, SILI_PREG_SCTL, data);
348 * Hard-reset the port. If a device is detected but it is busy
349 * we try a second time, this time cycling the phy as well.
351 * XXX note: hard reset mode 2 (cycling the PHY) is not reliable.
354 atx->at_probe = ATA_PROBE_NEED_HARD_RESET;
356 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
358 rc = sili_port_reset(ap, atx, 1);
360 rc = sili_port_reset(ap, atx, 1);
362 rc = sili_port_reset(ap, atx, 2);
369 * We had problems talking to the device on the port.
372 sili_pm_read(ap, atx->at_target,
373 SATA_PMREG_SSTS, &data);
375 switch(data & SATA_PM_SSTS_DET) {
376 case SATA_PM_SSTS_DET_DEV_NE:
377 kprintf("%s: Device not communicating\n",
380 case SATA_PM_SSTS_DET_PHYOFFLINE:
381 kprintf("%s: PHY offline\n",
385 kprintf("%s: No device detected\n",
390 data = sili_pread(ap, SILI_PREG_SSTS);
392 switch(data & SATA_PM_SSTS_DET) {
393 case SILI_PREG_SSTS_DET_DEV_NE:
394 kprintf("%s: Device not communicating\n",
397 case SILI_PREG_SSTS_DET_OFFLINE:
398 kprintf("%s: PHY offline\n",
402 kprintf("%s: No device detected\n",
411 * The device on the port is still telling us its busy,
412 * which means that it is not properly handling a SATA
415 * It may be possible to softreset the device using CLO
416 * and a device reset command.
419 kprintf("%s: Device on port is bricked, giving up\n",
422 kprintf("%s: Device on port is bricked, "
423 "trying softreset\n", PORTNAME(ap));
425 rc = sili_port_reset(ap, atx, 0);
427 kprintf("%s: Unable unbrick device\n",
430 kprintf("%s: Successfully unbricked\n",
441 * Command transfers can only be enabled if a device was successfully
444 * Allocate or deallocate the ap_ata array here too.
447 switch(ap->ap_type) {
448 case ATA_PORT_T_NONE:
461 * Flush interrupts on the port. XXX
463 * Enable interrupts on the port whether a device is sitting on
464 * it or not, to handle hot-plug events.
468 sili_pwrite(ap, SILI_PREG_IS, sili_pread(ap, SILI_PREG_IS));
469 sili_write(ap->ap_sc, SILI_REG_IS, 1 << ap->ap_num);
471 sili_port_interrupt_enable(ap);
477 * Handle an errored port. This routine is called when the only
478 * commands left on the queue are expired, meaning we can safely
479 * go through a port init to clear its state.
481 * We complete the expired CCBs and then restart the queue.
485 sili_port_reinit(struct sili_port *ap)
487 struct sili_ccb *ccb;
494 reentrant = (ap->ap_flags & AP_F_ERR_CCB_RESERVED) ? 1 : 0;
496 kprintf("%s: PORT REINIT AFTER ERROR reentrant=%d\n",
497 PORTNAME(ap), reentrant);
500 * Clear port resume, clear bits 16:13 in the port device status
501 * register. This is from the data sheet.
503 * Data sheet does not specify a delay but it seems prudent.
505 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
507 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
508 data = sili_pread(ap, SILI_PREG_PM_STATUS(target));
509 data &= ~(SILI_PREG_PM_STATUS_SERVICE |
510 SILI_PREG_PM_STATUS_LEGACY |
511 SILI_PREG_PM_STATUS_NATIVE |
512 SILI_PREG_PM_STATUS_VBSY);
513 sili_pwrite(ap, SILI_PREG_PM_STATUS(target), data);
514 sili_pwrite(ap, SILI_PREG_PM_QACTIVE(target), 0);
518 * Issue a Port Initialize and wait for it to clear. This flushes
519 * commands but does not reset the port. Then wait for port ready.
521 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
522 if (sili_pwait_clr(ap, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
523 kprintf("%s: Unable to reinit, port failed\n",
526 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
527 kprintf("%s: Unable to reinit, port will not come ready\n",
532 * Read the LOG ERROR page for targets that returned a specific
533 * D2H FIS with ERR set.
535 if (reentrant == 0) {
536 for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
537 at = &ap->ap_ata[target];
538 if (at->at_features & ATA_PORT_F_READLOG) {
539 kprintf("%s: READ LOG ERROR PAGE\n",
541 at->at_features &= ~ATA_PORT_F_READLOG;
542 sili_port_read_ncq_error(ap, target);
548 * Finally clean out the expired commands, we've probed the error
549 * status (or hopefully probed the error status). Well, ok,
550 * we probably didn't XXX.
552 while (ap->ap_expired) {
553 slot = ffs(ap->ap_expired) - 1;
554 ap->ap_expired &= ~(1 << slot);
555 KKASSERT(ap->ap_active & (1 << slot));
556 ap->ap_active &= ~(1 << slot);
558 ccb = &ap->ap_ccbs[slot];
559 ccb->ccb_xa.state = ATA_S_TIMEOUT;
560 kprintf("%s: reinit: kill slot %d\n",
561 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
563 ccb->ccb_xa.complete(&ccb->ccb_xa);
567 * Wow. All done. We can get the port moving again.
570 kprintf("%s: reinit called reentrantly, skip end\n",
572 } else if (ap->ap_probe == ATA_PROBE_FAILED) {
573 kprintf("%s: reinit failed, port is dead\n", PORTNAME(ap));
574 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
575 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
576 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
577 ccb->ccb_xa.state = ATA_S_TIMEOUT;
579 ccb->ccb_xa.complete(&ccb->ccb_xa);
582 kprintf("%s: reinit succeeded probe=%d type=%d\n", PORTNAME(ap), ap->ap_probe, ap->ap_type);
583 sili_issue_pending_commands(ap, NULL);
589 * Enable or re-enable interrupts on a port.
591 * This routine is called from the port initialization code or from the
592 * helper thread as the real interrupt may be forced to turn off certain
596 sili_port_interrupt_enable(struct sili_port *ap)
600 data = SILI_PREG_INT_CCOMPLETE | SILI_PREG_INT_CERROR |
601 SILI_PREG_INT_PHYRDYCHG | SILI_PREG_INT_DEVEXCHG |
602 SILI_PREG_INT_DECODE | SILI_PREG_INT_CRC |
603 SILI_PREG_INT_HANDSHK | SILI_PREG_INT_PMCHANGE;
604 if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
605 data |= SILI_PREG_INT_SDB;
606 sili_pwrite(ap, SILI_PREG_INT_ENABLE, data);
610 sili_port_interrupt_redisable(struct sili_port *ap)
614 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
615 data &= SILI_REG_GINT_PORTMASK;
616 data &= ~(1 << ap->ap_num);
617 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
621 sili_port_interrupt_reenable(struct sili_port *ap)
625 data = sili_read(ap->ap_sc, SILI_REG_GCTL);
626 data &= SILI_REG_GINT_PORTMASK;
627 data |= (1 << ap->ap_num);
628 sili_write(ap->ap_sc, SILI_REG_GCTL, data);
632 * Run the port / target state machine from a main context.
634 * The state machine for the port is always run.
636 * If atx is non-NULL run the state machine for a particular target.
637 * If atx is NULL run the state machine for all targets.
640 sili_port_state_machine(struct sili_port *ap, int initial)
649 * State machine for port. Note that CAM is not yet associated
650 * during the initial parallel probe and the port's probe state
651 * will not get past ATA_PROBE_NEED_IDENT.
654 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
655 kprintf("%s: Waiting 10 seconds on insertion\n",
657 sili_os_sleep(10000);
660 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
661 sili_port_init(ap, NULL);
662 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
663 sili_port_reset(ap, NULL, 1);
664 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
665 sili_port_reset(ap, NULL, 0);
666 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
667 sili_cam_probe(ap, NULL);
669 if (ap->ap_type != ATA_PORT_T_PM) {
670 if (ap->ap_probe == ATA_PROBE_FAILED) {
671 sili_cam_changed(ap, NULL, 0);
672 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
673 sili_cam_changed(ap, NULL, 1);
679 * Port Multiplier state machine.
681 * Get a mask of changed targets and combine with any runnable
682 * states already present.
684 for (loop = 0; ;++loop) {
685 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
686 kprintf("%s: PM unable to read hot-plug bitmap\n",
692 * Do at least one loop, then stop if no more state changes
693 * have occured. The PM might not generate a new
694 * notification until we clear the entire bitmap.
696 if (loop && data == 0)
700 * New devices showing up in the bitmap require some spin-up
701 * time before we start probing them. Reset didsleep. The
702 * first new device we detect will sleep before probing.
704 * This only applies to devices whos change bit is set in
705 * the data, and does not apply to the initial boot-time
710 for (target = 0; target < ap->ap_pmcount; ++target) {
711 at = &ap->ap_ata[target];
714 * Check the target state for targets behind the PM
715 * which have changed state. This will adjust
716 * at_probe and set ATA_PORT_F_RESCAN
718 * We want to wait at least 10 seconds before probing
719 * a newly inserted device. If the check status
720 * indicates a device is present and in need of a
721 * hard reset, we make sure we have slept before
724 * We also need to wait at least 1 second for the
725 * PHY state to change after insertion, if we
726 * haven't already waited the 10 seconds.
728 * NOTE: When pm_check_good finds a good port it
729 * typically starts us in probe state
730 * NEED_HARD_RESET rather than INIT.
732 if (data & (1 << target)) {
733 if (initial == 0 && didsleep == 0)
735 sili_pm_check_good(ap, target);
736 if (initial == 0 && didsleep == 0 &&
737 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
740 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
741 sili_os_sleep(10000);
746 * Report hot-plug events before the probe state
747 * really gets hot. Only actual events are reported
748 * here to reduce spew.
750 if (data & (1 << target)) {
751 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
752 switch(at->at_probe) {
753 case ATA_PROBE_NEED_INIT:
754 case ATA_PROBE_NEED_HARD_RESET:
755 kprintf("Device inserted\n");
757 case ATA_PROBE_FAILED:
758 kprintf("Device removed\n");
761 kprintf("Device probe in progress\n");
767 * Run through the state machine as necessary if
768 * the port is not marked failed.
770 * The state machine may stop at NEED_IDENT if
771 * CAM is not yet attached.
773 * Acquire exclusive access to the port while we
774 * are doing this. This prevents command-completion
775 * from queueing commands for non-polled targets
776 * inbetween our probe steps. We need to do this
777 * because the reset probes can generate severe PHY
778 * and protocol errors and soft-brick the port.
780 if (at->at_probe != ATA_PROBE_FAILED &&
781 at->at_probe != ATA_PROBE_GOOD) {
782 sili_beg_exclusive_access(ap, at);
783 if (at->at_probe == ATA_PROBE_NEED_INIT)
784 sili_port_init(ap, at);
785 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
786 sili_port_reset(ap, at, 1);
787 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
788 sili_port_reset(ap, at, 0);
789 if (at->at_probe == ATA_PROBE_NEED_IDENT)
790 sili_cam_probe(ap, at);
791 sili_end_exclusive_access(ap, at);
795 * Add or remove from CAM
797 if (at->at_features & ATA_PORT_F_RESCAN) {
798 at->at_features &= ~ATA_PORT_F_RESCAN;
799 if (at->at_probe == ATA_PROBE_FAILED) {
800 sili_cam_changed(ap, at, 0);
801 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
802 sili_cam_changed(ap, at, 1);
805 data &= ~(1 << target);
808 kprintf("%s: WARNING (PM): extra bits set in "
809 "EINFO: %08x\n", PORTNAME(ap), data);
810 while (target < SILI_MAX_PMPORTS) {
811 sili_pm_check_good(ap, target);
819 * De-initialize and detach a port.
822 sili_port_free(struct sili_softc *sc, u_int port)
824 struct sili_port *ap = sc->sc_ports[port];
825 struct sili_ccb *ccb;
828 * Ensure port is disabled and its interrupts are all flushed.
831 sili_os_stop_port(ap);
832 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
833 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
834 sili_write(ap->ap_sc, SILI_REG_GCTL,
835 sili_read(ap->ap_sc, SILI_REG_GCTL) &
836 ~SILI_REG_GINT_PORTST(ap->ap_num));
840 while ((ccb = sili_get_ccb(ap)) != NULL) {
841 if (ccb->ccb_dmamap) {
842 bus_dmamap_destroy(sc->sc_tag_data,
844 ccb->ccb_dmamap = NULL;
847 if ((ccb = ap->ap_err_ccb) != NULL) {
848 if (ccb->ccb_dmamap) {
849 bus_dmamap_destroy(sc->sc_tag_data,
851 ccb->ccb_dmamap = NULL;
853 ap->ap_err_ccb = NULL;
855 kfree(ap->ap_ccbs, M_DEVBUF);
859 if (ap->ap_dmamem_prbs) {
860 sili_dmamem_free(sc, ap->ap_dmamem_prbs);
861 ap->ap_dmamem_prbs = NULL;
864 kfree(ap->ap_ata, M_DEVBUF);
868 /* bus_space(9) says we dont free the subregions handle */
871 sc->sc_ports[port] = NULL;
877 * If hard is 0 perform a softreset of the port.
878 * If hard is 1 perform a hard reset of the port.
879 * If hard is 2 perform a hard reset of the port and cycle the phy.
881 * If at is non-NULL an indirect port via a port-multiplier is being
882 * reset, otherwise a direct port is being reset.
884 * NOTE: Indirect ports can only be soft-reset.
887 sili_port_reset(struct sili_port *ap, struct ata_port *at, int hard)
893 rc = sili_pm_hardreset(ap, at->at_target, hard);
895 rc = sili_port_hardreset(ap, hard);
898 rc = sili_pm_softreset(ap, at->at_target);
900 rc = sili_port_softreset(ap);
906 * SILI soft reset, Section 10.4.1
908 * (at) will be NULL when soft-resetting a directly-attached device, and
909 * non-NULL when soft-resetting a device through a port multiplier.
911 * This function keeps port communications intact and attempts to generate
912 * a reset to the connected device using device commands.
915 sili_port_softreset(struct sili_port *ap)
917 struct sili_ccb *ccb = NULL;
918 struct sili_prb *prb;
924 kprintf("%s: START SOFTRESET\n", PORTNAME(ap));
926 DPRINTF(SILI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
929 ap->ap_state = AP_S_NORMAL;
932 * Prep the special soft-reset SII command.
934 ccb = sili_get_err_ccb(ap);
935 ccb->ccb_done = sili_empty_done;
936 ccb->ccb_xa.flags = ATA_F_POLL;
937 ccb->ccb_xa.complete = sili_dummy_done;
938 ccb->ccb_xa.at = NULL;
941 bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
942 prb->prb_h2d.flags = 0;
943 prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
944 prb->prb_override = 0;
946 ccb->ccb_xa.state = ATA_S_PENDING;
947 ccb->ccb_xa.flags = 0;
950 if (sili_poll(ccb, 8000, sili_quick_timeout) != ATA_S_COMPLETE) {
951 kprintf("%s: First FIS failed\n", PORTNAME(ap));
955 sig = (prb->prb_d2h.lba_high << 24) |
956 (prb->prb_d2h.lba_mid << 16) |
957 (prb->prb_d2h.lba_low << 8) |
958 (prb->prb_d2h.sector_count);
959 kprintf("%s: SOFTRESET SIGNATURE %08x\n", PORTNAME(ap), sig);
962 * If the softreset is trying to clear a BSY condition after a
963 * normal portreset we assign the port type.
965 * If the softreset is being run first as part of the ccb error
966 * processing code then report if the device signature changed
969 if (ap->ap_type == ATA_PORT_T_NONE) {
970 ap->ap_type = sili_port_signature(ap, NULL, sig);
972 if (sili_port_signature(ap, NULL, sig) != ap->ap_type) {
973 kprintf("%s: device signature unexpectedly "
974 "changed\n", PORTNAME(ap));
975 error = EBUSY; /* XXX */
981 sili_put_err_ccb(ccb);
985 * If we failed to softreset make the port quiescent, otherwise
986 * make sure the port's start/stop state matches what it was on
989 * Don't kill the port if the softreset is on a port multiplier
990 * target, that would kill all the targets!
992 kprintf("%s: END SOFTRESET %d prob=%d state=%d\n", PORTNAME(ap), error, ap->ap_probe, ap->ap_state);
994 sili_port_hardstop(ap);
995 /* ap_probe set to failed */
997 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1002 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1008 * SILI port reset, Section 10.4.2
1010 * This function does a hard reset of the port. Note that the device
1011 * connected to the port could still end-up hung.
1014 sili_port_hardreset(struct sili_port *ap, int hard)
1020 DPRINTF(SILI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
1022 ap->ap_state = AP_S_NORMAL;
1026 * Issue Device Reset.
1028 * NOTE: Unlike Port Reset, the port ready signal will not
1029 * go active unless a device is established to be on
1032 sili_pwrite(ap, SILI_PREG_SERR, -1);
1033 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1034 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
1035 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
1036 if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
1037 kprintf("%s: hardreset failed to clear\n", PORTNAME(ap));
1041 * Try to determine if there is a device on the port.
1043 * Give the device 3/10 second to at least be detected.
1047 r = sili_pread(ap, SILI_PREG_SSTS);
1048 if (r & SILI_PREG_SSTS_DET)
1050 loop -= sili_os_softsleep();
1054 kprintf("%s: Port appears to be unplugged\n",
1061 * There is something on the port. Give the device 3 seconds
1062 * to fully negotiate.
1065 sili_pwait_eq(ap, 3000, SILI_PREG_SSTS,
1066 SILI_PREG_SSTS_DET, SILI_PREG_SSTS_DET_DEV)) {
1068 kprintf("%s: Device may be powered down\n",
1075 * Wait for the port to become ready.
1077 * This can take more then a second, give it 3 seconds. If we
1078 * succeed give the device another 3ms after that.
1080 * NOTE: Port multipliers can do two things here. First they can
1081 * return device-ready if a device is on target 0 and also
1082 * return the signature for that device. If there is no
1083 * device on target 0 then BSY/DRQ is never cleared and
1084 * it never comes ready.
1086 if (error == 0 && sili_pwait_set_to(ap, 3000, SILI_PREG_STATUS,
1087 SILI_PREG_STATUS_READY)) {
1089 * The device is bricked or its a port multiplier and will
1090 * not unbusy until we do the pmprobe CLO softreset sequence.
1092 error = sili_port_pmprobe(ap);
1094 kprintf("%s: Device will not come ready\n",
1097 ap->ap_type = ATA_PORT_T_PM;
1099 } else if (error == 0) {
1101 * The sili's hardreset doesn't return a signature (does it)?
1102 * In anycase, set the type so the signature gets set by
1103 * the softreset stage.
1105 error = sili_port_pmprobe(ap);
1107 ap->ap_type = ATA_PORT_T_NONE;
1110 ap->ap_type = ATA_PORT_T_PM;
1111 kprintf("%s: Port multiplier detected\n",
1117 * hard-stop the port if we failed. This will set ap_probe
1121 sili_port_hardstop(ap);
1122 /* ap_probe set to failed */
1124 if (ap->ap_type == ATA_PORT_T_PM)
1125 ap->ap_probe = ATA_PROBE_GOOD;
1127 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1133 * SILI port multiplier probe. This routine is run by the hardreset code
1134 * if it gets past the device detect.
1136 * All we do here is call sili_pm_softreset(). The Sili chip does all the
1139 * Return 0 on success, non-zero on failure.
1142 sili_port_pmprobe(struct sili_port *ap)
1144 struct ata_port *at;
1149 * If we don't support port multipliers don't try to detect one.
1151 if ((ap->ap_sc->sc_flags & SILI_F_SPM) == 0)
1155 * The port may be unhappy from its hardreset if there's a PM
1156 * but no device at target 0. If we try to shove the softreset
1157 * for target 15 down its throat it will pop a gasket.
1159 * Reiniting the port.. kind of a soft reset of its command
1160 * processor which otherwise does not effect the port registers,
1161 * seems to fix the problem.
1163 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_PMA);
1164 sili_port_reinit(ap);
1165 ap->ap_state = AP_S_NORMAL;
1166 error = sili_pm_softreset(ap, 15);
1168 ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
1173 kprintf("PMPROBE3 %d\n", error);
1175 if (error == 0 && sili_pm_identify(ap)) {
1176 kprintf("%s: PM - cannot identify port multiplier\n",
1180 kprintf("PMPROBE3 %d %d %d\n", error, ap->ap_probe, ap->ap_state);
1183 * If we probed the PM reset the state for the targets behind
1184 * it so they get probed by the state machine.
1187 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
1188 at = &ap->ap_ata[i];
1189 at->at_probe = ATA_PROBE_NEED_INIT;
1190 at->at_features |= ATA_PORT_F_RESCAN;
1191 at->at_features &= ~ATA_PORT_F_READLOG;
1196 * If we failed turn off PMA, otherwise identify the port multiplier.
1197 * CAM will iterate the devices.
1200 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1205 * Hard-stop on hot-swap device removal. See 10.10.1
1207 * Place the port in a mode that will allow it to detect hot-swap insertions.
1208 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1209 * seem to do the job.
1212 sili_port_hardstop(struct sili_port *ap)
1214 struct sili_ccb *ccb;
1215 struct ata_port *at;
1219 ap->ap_state = AP_S_FATAL_ERROR;
1220 ap->ap_probe = ATA_PROBE_FAILED;
1221 ap->ap_type = ATA_PORT_T_NONE;
1224 * Clean up AT sub-ports on SATA port.
1226 for (i = 0; ap->ap_ata && i < SILI_MAX_PMPORTS; ++i) {
1227 at = &ap->ap_ata[i];
1228 at->at_type = ATA_PORT_T_NONE;
1229 at->at_probe = ATA_PROBE_FAILED;
1230 at->at_features &= ~ATA_PORT_F_READLOG;
1234 * Kill the port. Don't bother waiting for it to transition
1237 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
1238 if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
1239 kprintf("%s: Port will not go into reset\n",
1243 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
1244 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
1247 * Turn off port-multiplier control bit
1249 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1252 * Clean up the command list.
1254 while (ap->ap_active) {
1255 slot = ffs(ap->ap_active) - 1;
1256 ap->ap_active &= ~(1 << slot);
1257 ap->ap_expired &= ~(1 << slot);
1258 --ap->ap_active_cnt;
1259 ccb = &ap->ap_ccbs[slot];
1260 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1261 callout_stop(&ccb->ccb_timeout);
1262 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1264 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1265 ATA_F_TIMEOUT_EXPIRED);
1266 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1268 ccb->ccb_xa.complete(&ccb->ccb_xa);
1270 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1271 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1272 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1273 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1275 ccb->ccb_xa.complete(&ccb->ccb_xa);
1277 KKASSERT(ap->ap_active_cnt == 0);
1280 * Put the port into a listen mode, we want to get insertion/removal
1283 sili_port_listen(ap);
1287 * Place port into a listen mode for hotplug events only. The port has
1288 * already been reset and the command processor may not be ready due
1289 * to the lack of a device.
1292 sili_port_listen(struct sili_port *ap)
1297 data = SILI_PREG_SCTL_SPM_NONE |
1298 SILI_PREG_SCTL_IPM_NONE |
1299 SILI_PREG_SCTL_SPD_NONE |
1300 SILI_PREG_SCTL_DET_INIT;
1301 if (SiliForceGen1 & (1 << ap->ap_num)) {
1302 data &= ~SILI_PREG_SCTL_SPD_NONE;
1303 data |= SILI_PREG_SCTL_SPD_GEN1;
1306 sili_pwrite(ap, SILI_PREG_SERR, -1);
1307 sili_pwrite(ap, SILI_PREG_INT_ENABLE, SILI_PREG_INT_PHYRDYCHG |
1308 SILI_PREG_INT_DEVEXCHG);
1312 * Figure out what type of device is connected to the port, ATAPI or
1316 sili_port_signature(struct sili_port *ap, struct ata_port *at, u_int32_t sig)
1319 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1320 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1321 return(ATA_PORT_T_ATAPI);
1322 } else if ((sig & 0xffff0000) ==
1323 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1324 return(ATA_PORT_T_PM);
1326 return(ATA_PORT_T_DISK);
1331 * Load the DMA descriptor table for a CCB's buffer.
1333 * NOTE: ATA_F_PIO is auto-selected by sili part.
1336 sili_load_prb(struct sili_ccb *ccb)
1338 struct sili_port *ap = ccb->ccb_port;
1339 struct sili_softc *sc = ap->ap_sc;
1340 struct ata_xfer *xa = &ccb->ccb_xa;
1341 struct sili_prb *prb = ccb->ccb_prb;
1342 struct sili_sge *sge;
1343 bus_dmamap_t dmap = ccb->ccb_dmamap;
1347 * Set up the PRB. The PRB contains 2 SGE's (1 if it is an ATAPI
1348 * command). The SGE must be set up to link to the rest of our
1349 * SGE array, in blocks of four SGEs (a SGE table) starting at
1351 prb->prb_xfer_count = 0;
1352 prb->prb_control = 0;
1353 prb->prb_override = 0;
1354 sge = (ccb->ccb_xa.flags & ATA_F_PACKET) ?
1355 &prb->prb_sge_packet : &prb->prb_sge_normal;
1356 if (xa->datalen == 0) {
1357 sge->sge_flags = SILI_SGE_FLAGS_TRM | SILI_SGE_FLAGS_DRD;
1362 if (ccb->ccb_xa.flags & ATA_F_READ)
1363 prb->prb_control |= SILI_PRB_CTRL_READ;
1364 if (ccb->ccb_xa.flags & ATA_F_WRITE)
1365 prb->prb_control |= SILI_PRB_CTRL_WRITE;
1366 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1368 sge->sge_paddr = ccb->ccb_prb_paddr +
1369 offsetof(struct sili_prb, prb_sge[0]);
1372 * Load our sge array.
1374 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1375 xa->data, xa->datalen,
1376 sili_load_prb_callback,
1378 ((xa->flags & ATA_F_NOWAIT) ?
1379 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1381 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1385 bus_dmamap_sync(sc->sc_tag_data, dmap,
1386 (xa->flags & ATA_F_READ) ?
1387 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1393 bus_dmamap_unload(sc->sc_tag_data, dmap);
1399 * Callback from BUSDMA system to load the segment list.
1401 * The scatter/gather table is loaded by the sili chip in blocks of
1402 * four SGE's. If a continuance is required the last entry in each
1403 * block must point to the next block.
1407 sili_load_prb_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1410 struct sili_ccb *ccb = info;
1411 struct sili_sge *sge;
1414 KKASSERT(nsegs <= SILI_MAX_SGET);
1417 sge = &ccb->ccb_prb->prb_sge[0];
1419 if ((sgi & 3) == 3) {
1420 sge->sge_paddr = htole64(ccb->ccb_prb_paddr +
1421 offsetof(struct sili_prb,
1424 sge->sge_flags = SILI_SGE_FLAGS_LNK;
1426 sge->sge_paddr = htole64(segs->ds_addr);
1427 sge->sge_count = htole32(segs->ds_len);
1436 sge->sge_flags |= SILI_SGE_FLAGS_TRM;
1440 sili_unload_prb(struct sili_ccb *ccb)
1442 struct sili_port *ap = ccb->ccb_port;
1443 struct sili_softc *sc = ap->ap_sc;
1444 struct ata_xfer *xa = &ccb->ccb_xa;
1445 bus_dmamap_t dmap = ccb->ccb_dmamap;
1447 if (xa->datalen != 0) {
1448 bus_dmamap_sync(sc->sc_tag_data, dmap,
1449 (xa->flags & ATA_F_READ) ?
1450 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1452 bus_dmamap_unload(sc->sc_tag_data, dmap);
1454 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1457 xa->resid = xa->datalen -
1458 le32toh(ccb->ccb_prb->prb_xfer_count);
1463 * Start a command and poll for completion.
1465 * timeout is in ms and only counts once the command gets on-chip.
1467 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1468 * that no error occured.
1470 * NOTE: If the caller specifies a NULL timeout function the caller is
1471 * responsible for clearing hardware state on failure, but we will
1472 * deal with removing the ccb from any pending queue.
1474 * NOTE: NCQ should never be used with this function.
1476 * NOTE: If the port is in a failed state and stopped we do not try
1477 * to activate the ccb.
1480 sili_poll(struct sili_ccb *ccb, int timeout,
1481 void (*timeout_fn)(struct sili_ccb *))
1483 struct sili_port *ap = ccb->ccb_port;
1485 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1486 ccb->ccb_xa.state = ATA_S_ERROR;
1487 return(ccb->ccb_xa.state);
1493 sili_port_intr(ap, 1);
1494 switch(ccb->ccb_xa.state) {
1496 timeout -= sili_os_softsleep();
1500 * The packet can get stuck on the pending queue
1501 * if the port refuses to come ready.
1503 if (AP_F_EXCLUSIVE_ACCESS)
1504 timeout -= sili_os_softsleep();
1506 sili_os_softsleep();
1507 sili_check_active_timeouts(ap);
1510 return (ccb->ccb_xa.state);
1512 } while (timeout > 0);
1514 kprintf("%s: Poll timeout slot %d\n",
1515 ATANAME(ap, ccb->ccb_xa.at),
1520 return(ccb->ccb_xa.state);
1524 * When polling we have to check if the currently active CCB(s)
1525 * have timed out as the callout will be deadlocked while we
1526 * hold the port lock.
1529 sili_check_active_timeouts(struct sili_port *ap)
1531 struct sili_ccb *ccb;
1535 mask = ap->ap_active;
1537 tag = ffs(mask) - 1;
1538 mask &= ~(1 << tag);
1539 ccb = &ap->ap_ccbs[tag];
1540 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1541 sili_core_timeout(ccb);
1549 sili_start_timeout(struct sili_ccb *ccb)
1551 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1552 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1553 callout_reset(&ccb->ccb_timeout,
1554 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1555 sili_ata_cmd_timeout_unserialized, ccb);
1560 sili_start(struct sili_ccb *ccb)
1562 struct sili_port *ap = ccb->ccb_port;
1564 struct sili_softc *sc = ap->ap_sc;
1567 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1570 * Sync our SGE table and PRB
1572 bus_dmamap_sync(ap->ap_dmamem_prbs->adm_tag,
1573 ap->ap_dmamem_prbs->adm_map,
1574 BUS_DMASYNC_PREWRITE);
1577 * XXX dmamap for PRB XXX BUS_DMASYNC_PREWRITE
1581 * Controller will update shared memory!
1582 * XXX bus_dmamap_sync ... BUS_DMASYNC_PREREAD ...
1584 /* Prepare RFIS area for write by controller */
1587 * There's no point trying to optimize this, it only shaves a few
1588 * nanoseconds so just queue the command and call our generic issue.
1590 sili_issue_pending_commands(ap, ccb);
1594 * While holding the port lock acquire exclusive access to the port.
1596 * This is used when running the state machine to initialize and identify
1597 * targets over a port multiplier. Setting exclusive access prevents
1598 * sili_port_intr() from activating any requests sitting on the pending
1602 sili_beg_exclusive_access(struct sili_port *ap, struct ata_port *at)
1604 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1605 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1606 while (ap->ap_active) {
1607 sili_port_intr(ap, 1);
1608 sili_os_softsleep();
1613 sili_end_exclusive_access(struct sili_port *ap, struct ata_port *at)
1615 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
1616 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
1617 sili_issue_pending_commands(ap, NULL);
1621 * If ccb is not NULL enqueue and/or issue it.
1623 * If ccb is NULL issue whatever we can from the queue. However, nothing
1624 * new is issued if the exclusive access flag is set or expired ccb's are
1627 * If existing commands are still active (ap_active) we can only
1628 * issue matching new commands.
1631 sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb)
1636 * If just running the queue and in exclusive access mode we
1637 * just return. Also in this case if there are any expired ccb's
1638 * we want to clear the queue so the port can be safely stopped.
1640 * XXX sili chip - expiration needs to be per-target if PM supports
1644 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1645 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
1650 * Pull the next ccb off the queue and run it if possible.
1651 * If the port is not ready to accept commands enable the
1652 * ready interrupt instead of starting a new command.
1654 * XXX limit ncqdepth for attached devices behind PM
1656 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1657 if ((sili_pread(ap, SILI_PREG_STATUS) &
1658 SILI_PREG_STATUS_READY) == 0) {
1659 kprintf("%s: slot %d NOT READY\n",
1660 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
1661 sili_pwrite(ap, SILI_PREG_INT_ENABLE,
1662 SILI_PREG_INT_READY);
1665 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1666 ccb->ccb_xa.state = ATA_S_ONCHIP;
1667 ap->ap_active |= 1 << ccb->ccb_slot;
1668 ap->ap_active_cnt++;
1671 * We can't use the CMD_FIFO method because it requires us
1672 * building the PRB in the LRAM, and the LRAM is buggy. So
1673 * we use host memory for the PRB.
1675 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot),
1676 (u_int32_t)ccb->ccb_prb_paddr);
1677 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot) + 4,
1678 (u_int32_t)(ccb->ccb_prb_paddr >> 32));
1679 /* sili_pwrite(ap, SILI_PREG_CMD_FIFO, ccb->ccb_slot); */
1680 sili_start_timeout(ccb);
1685 sili_intr(void *arg)
1687 struct sili_softc *sc = arg;
1688 struct sili_port *ap;
1693 * Check if the master enable is up, and whether any interrupts are
1696 * Clear the ints we got.
1698 if ((sc->sc_flags & SILI_F_INT_GOOD) == 0)
1700 gint = sili_read(sc, SILI_REG_GINT);
1701 if (gint == 0 || gint == 0xffffffff)
1703 sili_write(sc, SILI_REG_GINT, gint);
1706 * Process interrupts for each port in a non-blocking fashion.
1708 while (gint & SILI_REG_GINT_PORTMASK) {
1709 port = ffs(gint) - 1;
1710 ap = sc->sc_ports[port];
1712 if (sili_os_lock_port_nb(ap) == 0) {
1713 sili_port_intr(ap, 0);
1714 sili_os_unlock_port(ap);
1716 sili_port_interrupt_redisable(ap);
1717 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1720 gint &= ~(1 << port);
1725 * Core called from helper thread.
1728 sili_port_thread_core(struct sili_port *ap, int mask)
1731 * Process any expired timedouts.
1733 sili_os_lock_port(ap);
1734 if (mask & AP_SIGF_TIMEOUT) {
1735 sili_check_active_timeouts(ap);
1739 * Process port interrupts which require a higher level of
1742 if (mask & AP_SIGF_PORTINT) {
1743 sili_port_intr(ap, 1);
1744 sili_port_interrupt_reenable(ap);
1745 sili_os_unlock_port(ap);
1747 sili_os_unlock_port(ap);
1752 * Core per-port interrupt handler.
1754 * If blockable is 0 we cannot call sili_os_sleep() at all and we can only
1755 * deal with normal command completions which do not require blocking.
1758 sili_port_intr(struct sili_port *ap, int blockable)
1760 struct sili_softc *sc = ap->ap_sc;
1763 struct sili_ccb *ccb = NULL;
1764 struct ata_port *ccb_at = NULL;
1769 const u_int32_t blockable_mask = SILI_PREG_IST_PHYRDYCHG |
1770 SILI_PREG_IST_DEVEXCHG |
1771 SILI_PREG_IST_CERROR |
1772 SILI_PREG_IST_DECODE |
1774 SILI_PREG_IST_HANDSHK;
1775 const u_int32_t fatal_mask = SILI_PREG_IST_PHYRDYCHG |
1776 SILI_PREG_IST_DEVEXCHG |
1777 SILI_PREG_IST_DECODE |
1779 SILI_PREG_IST_HANDSHK;
1781 enum { NEED_NOTHING, NEED_HOTPLUG_INSERT,
1782 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1785 * NOTE: CCOMPLETE was automatically cleared when we read INT_STATUS.
1787 is = sili_pread(ap, SILI_PREG_INT_STATUS);
1788 is &= SILI_PREG_IST_MASK;
1789 if (is & SILI_PREG_IST_CCOMPLETE)
1790 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CCOMPLETE);
1793 * If we can't block then we can't handle these here. Disable
1794 * the interrupts in question so we don't live-lock, the helper
1795 * thread will re-enable them.
1797 * If the port is in a completely failed state we do not want
1798 * to drop through to failed-command-processing if blockable is 0,
1799 * just let the thread deal with it all.
1801 * Otherwise we fall through and still handle DHRS and any commands
1802 * which completed normally. Even if we are errored we haven't
1803 * stopped the port yet so CI/SACT are still good.
1805 if (blockable == 0) {
1806 if (ap->ap_state == AP_S_FATAL_ERROR) {
1807 sili_port_interrupt_redisable(ap);
1808 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1809 /*is &= ~blockable_mask;*/
1812 if (is & blockable_mask) {
1813 sili_port_interrupt_redisable(ap);
1814 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1815 /*is &= ~blockable_mask;*/
1820 if (is & SILI_PREG_IST_CERROR) {
1822 * Command failed (blockable).
1824 * This stops command processing. We can extract the PM
1825 * target from the PMP field in SILI_PREG_CONTEXT. The
1826 * tag is not necessarily valid so don't use that.
1828 * We must then expire all CCB's for that target and resume
1829 * processing if any other targets have active commands.
1830 * Particular error codes can be recovered by reading the LOG
1833 * The expire handling code will do the rest, which is
1834 * basically to reset the port once the only active
1835 * commands remaining are all expired.
1841 target = (sili_pread(ap, SILI_PREG_CONTEXT) >>
1842 SILI_PREG_CONTEXT_PMPORT_SHIFT) &
1843 SILI_PREG_CONTEXT_PMPORT_MASK;
1844 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CERROR);
1845 active = ap->ap_active & ~ap->ap_expired;
1846 error = sili_pread(ap, SILI_PREG_CERROR);
1847 kprintf("%s.%d target error %d active=%08x hactive=%08x "
1849 PORTNAME(ap), target, error,
1850 active, sili_pread(ap, SILI_PREG_SLOTST),
1851 sili_pread(ap, SILI_PREG_SERR), SILI_PFMT_SERR);
1854 slot = ffs(active) - 1;
1855 ccb = &ap->ap_ccbs[slot];
1856 if ((ccb_at = ccb->ccb_xa.at) == NULL)
1857 ccb_at = &ap->ap_ata[0];
1858 if (target == ccb_at->at_target) {
1859 kprintf("%s kill ccb slot %d\n",
1860 ATANAME(ap, ccb->ccb_xa.at), slot);
1861 if (ccb->ccb_xa.flags & ATA_F_NCQ &&
1862 (error == SILI_PREG_CERROR_DEVICE ||
1863 error == SILI_PREG_CERROR_SDBERROR)) {
1864 ccb_at->at_features |= ATA_PORT_F_READLOG;
1866 if (sili_core_timeout(ccb) == 0)
1869 active &= ~(1 << slot);
1873 * Resume will be 0 if the timeout reinited and restarted
1874 * the port. Otherwise we resume the port to allow other
1875 * commands to complete.
1877 kprintf("%s.%d remain=%08x resume=%d\n",
1878 PORTNAME(ap), target,
1879 ap->ap_active & ~ap->ap_expired, resume);
1881 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESUME);
1885 * Device notification to us (non-blocking)
1887 * This is interrupt status SILIPREG_IST_SDB
1889 * NOTE! On some parts notification bits can get set without
1890 * generating an interrupt. It is unclear whether this is
1891 * a bug in the PM (sending a DTOH device setbits with 'N' set
1892 * and 'I' not set), or a bug in the host controller.
1894 * It only seems to occur under load.
1896 if (sc->sc_flags & SILI_F_SSNTF) {
1900 data = sili_pread(ap, SILI_PREG_SNTF);
1901 if (is & SILI_PREG_IST_SDB) {
1902 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1904 is &= ~SILI_PREG_IST_SDB;
1905 xstr = " (no SDBS!)";
1910 kprintf("%s: NOTIFY %08x%s\n",
1911 PORTNAME(ap), data, xstr);
1912 sili_pwrite(ap, SILI_PREG_SNTF, data);
1913 sili_cam_changed(ap, NULL, -1);
1918 * Port change (hot-plug) (blockable).
1920 * A PCS interrupt will occur on hot-plug once communication is
1923 * A PRCS interrupt will occur on hot-unplug (and possibly also
1926 * XXX We can then check the CPS (Cold Presence State) bit, if
1927 * supported, to determine if a device is plugged in or not and do
1930 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
1931 * can also occur if an unsolicited COMINIT is received.
1932 * If this occurs command processing is automatically
1933 * stopped (CR goes inactive) and the port must be stopped
1936 if (is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG)) {
1938 sili_pwrite(ap, SILI_PREG_SERR,
1939 (SILI_PREG_SERR_DIAG_N | SILI_PREG_SERR_DIAG_X));
1940 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1941 is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG));
1943 is &= ~(SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG);
1944 kprintf("%s: Port change\n", PORTNAME(ap));
1946 switch (sili_pread(ap, SILI_PREG_SSTS) & SILI_PREG_SSTS_DET) {
1947 case SILI_PREG_SSTS_DET_DEV:
1948 if (ap->ap_type == ATA_PORT_T_NONE &&
1949 ap->ap_probe == ATA_PROBE_FAILED) {
1950 need = NEED_HOTPLUG_INSERT;
1955 kprintf("%s: Device lost\n", PORTNAME(ap));
1956 if (ap->ap_type != ATA_PORT_T_NONE) {
1957 need = NEED_HOTPLUG_REMOVE;
1965 * Check for remaining errors - they are fatal. (blockable)
1967 if (is & fatal_mask) {
1970 sili_pwrite(ap, SILI_PREG_INT_STATUS, is & fatal_mask);
1972 serr = sili_pread(ap, SILI_PREG_SERR);
1973 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
1974 "disabling port.\n",
1976 is, SILI_PFMT_INT_STATUS,
1977 serr, SILI_PFMT_SERR
1980 /* XXX try recovery first */
1985 * Fail all outstanding commands if we know the port won't recover.
1987 * We may have a ccb_at if the failed command is known and was
1988 * being sent to a device over a port multiplier (PM). In this
1989 * case if the port itself has not completely failed we fail just
1990 * the commands related to that target.
1992 if (ap->ap_state == AP_S_FATAL_ERROR && ap->ap_active) {
1994 kprintf("%s: Interrupt, fatal error\n", PORTNAME(ap));
1995 ap->ap_state = AP_S_FATAL_ERROR;
1998 * Error all the active slots. If running across a PM
1999 * try to error out just the slots related to the target.
2001 active = ap->ap_active & ~ap->ap_expired;
2004 slot = ffs(active) - 1;
2005 kprintf("%s: Killing slot %d\n", PORTNAME(ap), slot);
2006 active &= ~(1 << slot);
2007 ccb = &ap->ap_ccbs[slot];
2008 sili_core_timeout(ccb);
2013 * CCB completion (non blocking).
2015 * CCB completion is detected by noticing the slot bit in
2016 * the port slot status register has cleared while the bit
2017 * is still set in our ap_active variable.
2019 * When completing expired events we must remember to reinit
2020 * the port once everything is clear.
2022 active = ap->ap_active & ~sili_pread(ap, SILI_PREG_SLOTST);
2025 slot = ffs(active) - 1;
2026 ccb = &ap->ap_ccbs[slot];
2028 DPRINTF(SILI_D_INTR, "%s: slot %d is complete%s\n",
2029 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2032 active &= ~(1 << slot);
2035 * XXX sync POSTREAD for return data?
2037 ap->ap_active &= ~(1 << ccb->ccb_slot);
2038 --ap->ap_active_cnt;
2041 * Complete the ccb. If the ccb was marked expired it
2042 * may or may not have been cleared from the port,
2043 * make sure we mark it as having timed out.
2045 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2046 ap->ap_expired &= ~(1 << ccb->ccb_slot);
2047 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2049 ccb->ccb_xa.complete(&ccb->ccb_xa);
2051 if (ccb->ccb_xa.state == ATA_S_ONCHIP)
2052 ccb->ccb_xa.state = ATA_S_COMPLETE;
2056 if (is & SILI_PREG_IST_READY) {
2057 is &= ~SILI_PREG_IST_READY;
2058 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_READY);
2059 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_READY);
2063 * If we had expired commands and were waiting for
2064 * remaining commands to complete, and they have now
2065 * completed, we can reinit the port.
2067 * This will also clean out the expired commands.
2068 * The timeout code also calls sili_port_reinit() if
2069 * the only commands remaining after a timeout are all
2070 * now expired commands.
2072 * Otherwise just reissue.
2074 if (ap->ap_expired && ap->ap_active == ap->ap_expired)
2075 sili_port_reinit(ap);
2077 sili_issue_pending_commands(ap, NULL);
2080 * Cleanup. Will not be set if non-blocking.
2083 case NEED_HOTPLUG_INSERT:
2085 * A hot-plug insertion event has occured and all
2086 * outstanding commands have already been revoked.
2088 * Don't recurse if this occurs while we are
2089 * resetting the port.
2091 * Place the port in a continuous COMRESET state
2092 * until the INIT code gets to it.
2094 kprintf("%s: HOTPLUG - Device inserted\n",
2096 ap->ap_probe = ATA_PROBE_NEED_INIT;
2097 sili_cam_changed(ap, NULL, -1);
2099 case NEED_HOTPLUG_REMOVE:
2101 * A hot-plug removal event has occured and all
2102 * outstanding commands have already been revoked.
2104 * Don't recurse if this occurs while we are
2105 * resetting the port.
2107 kprintf("%s: HOTPLUG - Device removed\n",
2109 sili_port_hardstop(ap);
2110 /* ap_probe set to failed */
2111 sili_cam_changed(ap, NULL, -1);
2119 sili_get_ccb(struct sili_port *ap)
2121 struct sili_ccb *ccb;
2123 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2124 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2126 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2127 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2128 ccb->ccb_xa.state = ATA_S_SETUP;
2129 ccb->ccb_xa.at = NULL;
2131 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2137 sili_put_ccb(struct sili_ccb *ccb)
2139 struct sili_port *ap = ccb->ccb_port;
2142 if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2143 ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2144 ccb->ccb_xa.state != ATA_S_ERROR) {
2145 kprintf("%s: invalid ata_xfer state %02x in sili_put_ccb, "
2147 PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2152 ccb->ccb_xa.state = ATA_S_PUT;
2153 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2154 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2155 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2159 sili_get_err_ccb(struct sili_port *ap)
2161 struct sili_ccb *err_ccb;
2163 KKASSERT(sili_pread(ap, SILI_PREG_CI) == 0);
2164 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2165 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2168 KKASSERT(ap->ap_err_busy == 0);
2169 ap->ap_err_busy = 1;
2172 * Grab a CCB to use for error recovery. This should never fail, as
2173 * we ask atascsi to reserve one for us at init time.
2175 err_ccb = ap->ap_err_ccb;
2176 KKASSERT(err_ccb != NULL);
2177 err_ccb->ccb_xa.flags = 0;
2178 err_ccb->ccb_done = sili_empty_done;
2184 sili_put_err_ccb(struct sili_ccb *ccb)
2186 struct sili_port *ap = ccb->ccb_port;
2189 KKASSERT(ap->ap_err_busy);
2191 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2193 KKASSERT(ccb == ap->ap_err_ccb);
2196 ap->ap_err_busy = 0;
2198 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2202 * Read log page to get NCQ error.
2205 sili_port_read_ncq_error(struct sili_port *ap, int target)
2207 struct sili_ccb *ccb;
2208 struct ata_fis_h2d *fis;
2211 DPRINTF(SILI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2213 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2214 ccb = sili_get_err_ccb(ap);
2215 ccb->ccb_done = sili_empty_done;
2216 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2217 ccb->ccb_xa.data = ap->ap_err_scratch;
2218 ccb->ccb_xa.datalen = 512;
2219 ccb->ccb_xa.complete = sili_dummy_done;
2220 ccb->ccb_xa.at = &ap->ap_ata[target];
2221 fis = &ccb->ccb_prb->prb_h2d;
2222 bzero(fis, sizeof(*fis));
2224 fis->type = ATA_FIS_TYPE_H2D;
2225 fis->flags = ATA_H2D_FLAGS_CMD | target;
2226 fis->command = ATA_C_READ_LOG_EXT;
2227 fis->lba_low = 0x10; /* queued error log page (10h) */
2228 fis->sector_count = 1; /* number of sectors (1) */
2229 fis->sector_count_exp = 0;
2230 fis->lba_mid = 0; /* starting offset */
2231 fis->lba_mid_exp = 0;
2234 if (sili_load_prb(ccb) != 0) {
2235 rc = ENOMEM; /* XXX caller must abort all commands */
2237 ccb->ccb_xa.state = ATA_S_PENDING;
2238 rc = sili_poll(ccb, 1000, sili_quick_timeout);
2241 /* Abort our command, if it failed, by stopping command DMA. */
2243 kprintf("%s: log page read failed, slot %d was still active.\n",
2244 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
2247 /* Done with the error CCB now. */
2248 sili_unload_prb(ccb);
2249 sili_put_err_ccb(ccb);
2251 /* Extract failed register set and tags from the scratch space. */
2253 struct ata_log_page_10h *log;
2256 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2257 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2258 /* Not queued bit was set - wasn't an NCQ error? */
2259 kprintf("%s: read NCQ error page, but not an NCQ "
2264 /* Copy back the log record as a D2H register FIS. */
2265 err_slot = log->err_regs.type &
2266 ATA_LOG_10H_TYPE_TAG_MASK;
2267 ccb = &ap->ap_ccbs[err_slot];
2268 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2269 kprintf("%s: read NCQ error page ok\n",
2270 ATANAME(ap, ccb->ccb_xa.at));
2271 memcpy(&ccb->ccb_prb->prb_d2h, &log->err_regs,
2272 sizeof(struct ata_fis_d2h));
2273 ccb->ccb_prb->prb_d2h.type = ATA_FIS_TYPE_D2H;
2274 ccb->ccb_prb->prb_d2h.flags = 0;
2276 kprintf("%s: error log slot %d did not match a failed ccb!\n", ATANAME(ccb->ccb_port, ccb->ccb_xa.at), err_slot);
2285 * Allocate memory for various structures DMAd by hardware. The maximum
2286 * number of segments for these tags is 1 so the DMA memory will have a
2287 * single physical base address.
2289 struct sili_dmamem *
2290 sili_dmamem_alloc(struct sili_softc *sc, bus_dma_tag_t tag)
2292 struct sili_dmamem *adm;
2295 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2297 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2298 BUS_DMA_ZERO, &adm->adm_map);
2301 error = bus_dmamap_load(tag, adm->adm_map,
2303 bus_dma_tag_getmaxsize(tag),
2304 sili_dmamem_saveseg, &adm->adm_busaddr,
2309 bus_dmamap_destroy(tag, adm->adm_map);
2310 adm->adm_map = NULL;
2311 adm->adm_tag = NULL;
2312 adm->adm_kva = NULL;
2314 kfree(adm, M_DEVBUF);
2322 sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2324 KKASSERT(error == 0);
2325 KKASSERT(nsegs == 1);
2326 *(bus_addr_t *)info = segs->ds_addr;
2331 sili_dmamem_free(struct sili_softc *sc, struct sili_dmamem *adm)
2334 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2335 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2336 adm->adm_map = NULL;
2337 adm->adm_tag = NULL;
2338 adm->adm_kva = NULL;
2340 kfree(adm, M_DEVBUF);
2344 sili_read(struct sili_softc *sc, bus_size_t r)
2346 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2347 BUS_SPACE_BARRIER_READ);
2348 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2352 sili_write(struct sili_softc *sc, bus_size_t r, u_int32_t v)
2354 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2355 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2356 BUS_SPACE_BARRIER_WRITE);
2360 sili_pread(struct sili_port *ap, bus_size_t r)
2362 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2363 BUS_SPACE_BARRIER_READ);
2364 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2368 sili_pwrite(struct sili_port *ap, bus_size_t r, u_int32_t v)
2370 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2371 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2372 BUS_SPACE_BARRIER_WRITE);
2376 * Wait up to (timeout) milliseconds for the masked port register to
2379 * Timeout is in milliseconds.
2382 sili_pwait_eq(struct sili_port *ap, int timeout,
2383 bus_size_t r, u_int32_t mask, u_int32_t target)
2388 * Loop hard up to 100uS
2390 for (t = 0; t < 100; ++t) {
2391 if ((sili_pread(ap, r) & mask) == target)
2393 sili_os_hardsleep(1); /* us */
2397 timeout -= sili_os_softsleep();
2398 if ((sili_pread(ap, r) & mask) == target)
2400 } while (timeout > 0);
2405 sili_wait_ne(struct sili_softc *sc, bus_size_t r, u_int32_t mask,
2411 * Loop hard up to 100uS
2413 for (t = 0; t < 100; ++t) {
2414 if ((sili_read(sc, r) & mask) != target)
2416 sili_os_hardsleep(1); /* us */
2420 * And one millisecond the slow way
2424 t -= sili_os_softsleep();
2425 if ((sili_read(sc, r) & mask) != target)
2434 * Acquire an ata transfer.
2436 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2437 * targets that go through the port multiplier.
2440 sili_ata_get_xfer(struct sili_port *ap, struct ata_port *at)
2442 struct sili_ccb *ccb;
2444 ccb = sili_get_ccb(ap);
2446 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer: NULL ccb\n",
2451 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer got slot %d\n",
2452 PORTNAME(ap), ccb->ccb_slot);
2454 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
2455 ccb->ccb_xa.at = at;
2456 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2458 return (&ccb->ccb_xa);
2462 sili_ata_put_xfer(struct ata_xfer *xa)
2464 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2466 DPRINTF(SILI_D_XFER, "sili_ata_put_xfer slot %d\n", ccb->ccb_slot);
2472 sili_ata_cmd(struct ata_xfer *xa)
2474 struct sili_ccb *ccb = (struct sili_ccb *)xa;
2476 KKASSERT(xa->state == ATA_S_SETUP);
2478 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2481 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2482 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2483 sili_pread(ccb->ccb_port, SILI_PREG_CMD), SILI_PFMT_CMD,
2486 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2489 ccb->ccb_done = sili_ata_cmd_done;
2491 if (sili_load_prb(ccb) != 0)
2494 xa->state = ATA_S_PENDING;
2496 if (xa->flags & ATA_F_POLL)
2497 return (sili_poll(ccb, xa->timeout, sili_ata_cmd_timeout));
2500 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
2501 xa->flags |= ATA_F_TIMEOUT_DESIRED;
2508 xa->state = ATA_S_ERROR;
2511 return (ATA_S_ERROR);
2515 sili_ata_cmd_done(struct sili_ccb *ccb)
2517 struct ata_xfer *xa = &ccb->ccb_xa;
2520 * NOTE: callout does not lock port and may race us modifying
2521 * the flags, so make sure its stopped.
2523 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2524 callout_stop(&ccb->ccb_timeout);
2525 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2527 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
2529 KKASSERT(xa->state != ATA_S_ONCHIP);
2530 sili_unload_prb(ccb);
2533 else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
2534 kprintf("%s: invalid ata_xfer state %02x in sili_ata_cmd_done, "
2536 PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
2538 if (xa->state != ATA_S_TIMEOUT)
2543 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
2544 * while the callout is runing.
2546 * We can't safely get the port lock here or delay, we could block
2547 * the callout thread.
2550 sili_ata_cmd_timeout_unserialized(void *arg)
2552 struct sili_ccb *ccb = arg;
2553 struct sili_port *ap = ccb->ccb_port;
2555 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
2556 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
2557 sili_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
2561 sili_ata_cmd_timeout(struct sili_ccb *ccb)
2563 sili_core_timeout(ccb);
2567 * Timeout code, typically called when the port command processor is running.
2569 * Returns 0 if all timeout processing completed, non-zero if it is still
2574 sili_core_timeout(struct sili_ccb *ccb)
2576 struct ata_xfer *xa = &ccb->ccb_xa;
2577 struct sili_port *ap = ccb->ccb_port;
2578 struct ata_port *at;
2580 at = ccb->ccb_xa.at;
2582 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
2587 ccb->ccb_xa.state, ccb->ccb_slot,
2590 sili_pread(ap, SILI_PREG_SLOTST)
2594 * NOTE: Timeout will not be running if the command was polled.
2595 * If we got here at least one of these flags should be set.
2597 * However, it might be running if we are called from the
2598 * interrupt error handling code.
2600 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2601 ATA_F_TIMEOUT_RUNNING));
2602 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2603 callout_stop(&ccb->ccb_timeout);
2604 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2606 xa->flags &= ~ATA_F_TIMEOUT_EXPIRED;
2608 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2609 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2610 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2613 sili_issue_pending_commands(ap, NULL);
2616 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2617 kprintf("%s: Unexpected state during timeout: %d\n",
2618 ATANAME(ap, at), ccb->ccb_xa.state);
2623 * We can't process timeouts while other commands are running.
2625 ap->ap_expired |= 1 << ccb->ccb_slot;
2627 if (ap->ap_active != ap->ap_expired) {
2628 kprintf("%s: Deferred timeout until its safe, slot %d\n",
2629 ATANAME(ap, at), ccb->ccb_slot);
2634 * We have to issue a Port reinit. We don't read an error log
2635 * page for timeouts. Reiniting the port will clear all pending
2638 sili_port_reinit(ap);
2643 * Used by the softreset, pmprobe, and read_ncq_error only, in very
2644 * specialized, controlled circumstances.
2647 sili_quick_timeout(struct sili_ccb *ccb)
2649 struct sili_port *ap = ccb->ccb_port;
2651 switch (ccb->ccb_xa.state) {
2653 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2654 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2657 KKASSERT((ap->ap_active & ~ap->ap_expired) ==
2658 (1 << ccb->ccb_slot));
2659 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2660 ap->ap_active &= ~(1 << ccb->ccb_slot);
2661 KKASSERT(ap->ap_active_cnt > 0);
2662 --ap->ap_active_cnt;
2663 sili_port_reinit(ap);
2666 panic("%s: sili_quick_timeout: ccb in bad state %d",
2667 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
2672 sili_dummy_done(struct ata_xfer *xa)
2677 sili_empty_done(struct sili_ccb *ccb)