SILI - Cleanup, remove broken KKASSERT and remove DIAGNOSTICs
[dragonfly.git] / sys / dev / disk / sili / sili.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  *
34  *
35  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
36  *
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.
40  *
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.
48  *
49  *
50  *
51  * $OpenBSD: sili.c,v 1.147 2009/02/16 21:19:07 miod Exp $
52  */
53
54 #include "sili.h"
55
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);
59
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_hardreset(struct sili_port *ap);
67 void    sili_port_hardstop(struct sili_port *ap);
68 void    sili_port_listen(struct sili_port *ap);
69
70 static void sili_ata_cmd_timeout_unserialized(void *);
71 static int sili_core_timeout(struct sili_ccb *ccb, int really_error);
72 void    sili_check_active_timeouts(struct sili_port *ap);
73
74 #if 0
75 void    sili_beg_exclusive_access(struct sili_port *ap, struct ata_port *at);
76 void    sili_end_exclusive_access(struct sili_port *ap, struct ata_port *at);
77 #endif
78 void    sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb);
79
80 void    sili_port_read_ncq_error(struct sili_port *, int);
81
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);
85
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);
89
90 /*
91  * Initialize the global SILI hardware.  This code does not set up any of
92  * its ports.
93  */
94 int
95 sili_init(struct sili_softc *sc)
96 {
97         DPRINTF(SILI_D_VERBOSE, " GHC 0x%b",
98                 sili_read(sc, SILI_REG_GHC), SILI_FMT_GHC);
99
100         /*
101          * Reset the entire chip.  This also resets all ports.
102          *
103          * The spec doesn't say anything about how long we have to
104          * wait, so wait 10ms.
105          */
106         sili_write(sc, SILI_REG_GCTL, SILI_REG_GCTL_GRESET);
107         sili_os_sleep(10);
108         sili_write(sc, SILI_REG_GCTL, 0);
109         sili_os_sleep(10);
110
111         return (0);
112 }
113
114 /*
115  * Allocate and initialize an SILI port.
116  */
117 int
118 sili_port_alloc(struct sili_softc *sc, u_int port)
119 {
120         struct sili_port        *ap;
121         struct ata_port         *at;
122         struct sili_prb         *prb;
123         struct sili_ccb         *ccb;
124         int     rc = ENOMEM;
125         int     error;
126         int     i;
127
128         ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
129         ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
130
131         ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
132                   device_get_name(sc->sc_dev),
133                   device_get_unit(sc->sc_dev),
134                   port);
135         sc->sc_ports[port] = ap;
136
137         /*
138          * Allocate enough so we never have to reallocate, it makes
139          * it easier.
140          *
141          * ap_pmcount will be reduced by the scan if we encounter the
142          * port multiplier port prior to target 15.
143          */
144         if (ap->ap_ata == NULL) {
145                 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * SILI_MAX_PMPORTS,
146                                      M_DEVBUF, M_INTWAIT | M_ZERO);
147                 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
148                         at = &ap->ap_ata[i];
149                         at->at_sili_port = ap;
150                         at->at_target = i;
151                         at->at_probe = ATA_PROBE_NEED_INIT;
152                         at->at_features |= ATA_PORT_F_RESCAN;
153                         ksnprintf(at->at_name, sizeof(at->at_name),
154                                   "%s.%d", ap->ap_name, i);
155                 }
156         }
157         if (bus_space_subregion(sc->sc_piot, sc->sc_pioh,
158                                 SILI_PORT_REGION(port), SILI_PORT_SIZE,
159                                 &ap->ap_ioh) != 0) {
160                 device_printf(sc->sc_dev,
161                               "unable to create register window for port %d\n",
162                               port);
163                 goto freeport;
164         }
165
166         ap->ap_sc = sc;
167         ap->ap_num = port;
168         ap->ap_probe = ATA_PROBE_NEED_INIT;
169         TAILQ_INIT(&ap->ap_ccb_free);
170         TAILQ_INIT(&ap->ap_ccb_pending);
171         lockinit(&ap->ap_ccb_lock, "silipo", 0, 0);
172
173         /* Disable port interrupts */
174         sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
175
176         /*
177          * Reset the port.  This is similar to a Device Reset but far
178          * more invasive.  We use Device Reset in our hardreset function.
179          * This function also does the same OOB initialization sequence
180          * that Device Reset does.
181          *
182          * NOTE: SILI_PREG_STATUS_READY will not be asserted unless and until
183          *       a device is connected to the port, so we can't use it to
184          *       verify that the port exists.
185          */
186         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
187         if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
188                 device_printf(sc->sc_dev,
189                               "Port %d will not go into reset\n", port);
190                 goto freeport;
191         }
192         sili_os_sleep(10);
193         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
194
195         /*
196          * Allocate the SGE Table
197          */
198         ap->ap_dmamem_prbs = sili_dmamem_alloc(sc, sc->sc_tag_prbs);
199         if (ap->ap_dmamem_prbs == NULL) {
200                 kprintf("%s: NOSGET\n", PORTNAME(ap));
201                 goto freeport;
202         }
203
204         /*
205          * Set up the SGE table base address
206          */
207         ap->ap_prbs = (struct sili_prb *)SILI_DMA_KVA(ap->ap_dmamem_prbs);
208
209         /*
210          * Allocate a CCB for each command slot
211          */
212         ap->ap_ccbs = kmalloc(sizeof(struct sili_ccb) * sc->sc_ncmds, M_DEVBUF,
213                               M_WAITOK | M_ZERO);
214         if (ap->ap_ccbs == NULL) {
215                 device_printf(sc->sc_dev,
216                               "unable to allocate command list for port %d\n",
217                               port);
218                 goto freeport;
219         }
220
221         /*
222          * Most structures are in the port BAR.  Assign convenient
223          * pointers in the CCBs
224          */
225         for (i = 0; i < sc->sc_ncmds; i++) {
226                 ccb = &ap->ap_ccbs[i];
227
228                 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
229                                           &ccb->ccb_dmamap);
230                 if (error) {
231                         device_printf(sc->sc_dev,
232                                       "unable to create dmamap for port %d "
233                                       "ccb %d\n", port, i);
234                         goto freeport;
235                 }
236
237                 /*
238                  * WARNING!!!  Access to the rfis is only allowed under very
239                  *             carefully controlled circumstances because it
240                  *             is located in the LRAM and reading from the
241                  *             LRAM has hardware issues which can blow the
242                  *             port up.  I kid you not (from Linux, and
243                  *             verified by testing here).
244                  */
245                 callout_init(&ccb->ccb_timeout);
246                 ccb->ccb_slot = i;
247                 ccb->ccb_port = ap;
248                 ccb->ccb_prb = &ap->ap_prbs[i];
249                 ccb->ccb_prb_paddr = SILI_DMA_DVA(ap->ap_dmamem_prbs) +
250                                      sizeof(*ccb->ccb_prb) * i;
251                 ccb->ccb_xa.fis = &ccb->ccb_prb->prb_h2d;
252                 prb = bus_space_kva(ap->ap_sc->sc_iot, ap->ap_ioh,
253                                     SILI_PREG_LRAM_SLOT(i));
254                 ccb->ccb_prb_lram = prb;
255                 /*
256                  * Point our rfis to host-memory instead of the LRAM PRB.
257                  * It will be copied back if ATA_F_AUTOSENSE is set.  The
258                  * LRAM PRB is buggy.
259                  */
260                 /*ccb->ccb_xa.rfis = &prb->prb_d2h;*/
261                 ccb->ccb_xa.rfis = (void *)ccb->ccb_xa.fis;
262
263                 ccb->ccb_xa.packetcmd = prb_packet(ccb->ccb_prb);
264                 ccb->ccb_xa.tag = i;
265
266                 ccb->ccb_xa.state = ATA_S_COMPLETE;
267
268                 /*
269                  * Reserve CCB[1] as the error CCB.  It doesn't matter
270                  * which one we use for the Sili controllers.
271                  */
272                 if (i == 1)
273                         ap->ap_err_ccb = ccb;
274                 else
275                         sili_put_ccb(ccb);
276         }
277         /*
278          * Do not call sili_port_init() here, the helper thread will
279          * call it for the parallel probe
280          */
281         sili_os_start_port(ap);
282         return(0);
283 freeport:
284         sili_port_free(sc, port);
285         return (rc);
286 }
287
288 /*
289  * This is called once by the low level attach (from the helper thread)
290  * to get the port state machine rolling, and typically only called again
291  * on a hot-plug insertion event.
292  *
293  * This is called for PM attachments and hot-plug insertion events, and
294  * typically not called again until after an unplug/replug sequence.
295  *
296  * Returns 0 if a device is successfully detected.
297  */
298 int
299 sili_port_init(struct sili_port *ap)
300 {
301         /*
302          * Do a very hard reset of the port
303          */
304         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
305         sili_os_sleep(10);
306         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
307
308         /*
309          * Register initialization
310          */
311         sili_pwrite(ap, SILI_PREG_FIFO_CTL,
312                     SILI_PREG_FIFO_CTL_ENCODE(1024, 1024));
313         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_32BITDMA |
314                                            SILI_PREG_CTL_PMA);
315         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_NOAUTOCC);
316         if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
317                 sili_pwrite(ap, SILI_PREG_SNTF, -1);
318         ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
319         ap->ap_pmcount = 0;
320         sili_port_interrupt_enable(ap);
321         return (0);
322 }
323
324 /*
325  * Handle an errored port.  This routine is called when the only
326  * commands left on the queue are expired, meaning we can safely
327  * go through a port init to clear its state.
328  *
329  * We complete the expired CCBs and then restart the queue.
330  */
331 static
332 void
333 sili_port_reinit(struct sili_port *ap)
334 {
335         struct sili_ccb *ccb;
336         struct ata_port *at;
337         int slot;
338         int target;
339         u_int32_t data;
340         int reentrant;
341
342         reentrant = (ap->ap_flags & AP_F_ERR_CCB_RESERVED) ? 1 : 0;
343
344         if (bootverbose || 1) {
345                 kprintf("%s: reiniting port after error reent=%d "
346                         "expired=%08x\n",
347                         PORTNAME(ap), reentrant, ap->ap_expired);
348         }
349
350         /*
351          * Clear port resume, clear bits 16:13 in the port device status
352          * register.  This is from the data sheet.
353          *
354          * Data sheet does not specify a delay but it seems prudent.
355          */
356         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
357         sili_os_sleep(10);
358         for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
359                 data = sili_pread(ap, SILI_PREG_PM_STATUS(target));
360                 data &= ~(SILI_PREG_PM_STATUS_SERVICE |
361                           SILI_PREG_PM_STATUS_LEGACY |
362                           SILI_PREG_PM_STATUS_NATIVE |
363                           SILI_PREG_PM_STATUS_VBSY);
364                 sili_pwrite(ap, SILI_PREG_PM_STATUS(target), data);
365                 sili_pwrite(ap, SILI_PREG_PM_QACTIVE(target), 0);
366         }
367
368         /*
369          * Issue a Port Initialize and wait for it to clear.  This flushes
370          * commands but does not reset the port.  Then wait for port ready.
371          */
372         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
373         if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
374                 kprintf("%s: Unable to reinit, port failed\n",
375                         PORTNAME(ap));
376         }
377         if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
378                 kprintf("%s: Unable to reinit, port will not come ready\n",
379                         PORTNAME(ap));
380         }
381
382         /*
383          * If reentrant, stop here.  Otherwise the state for the original
384          * ahci_port_reinit() will get ripped out from under it.
385          */
386         if (reentrant)
387                 return;
388
389         /*
390          * Read the LOG ERROR page for targets that returned a specific
391          * D2H FIS with ERR set.
392          */
393         for (target = 0; target < SILI_MAX_PMPORTS; ++target) {
394                 at = &ap->ap_ata[target];
395                 if (at->at_features & ATA_PORT_F_READLOG) {
396                         at->at_features &= ~ATA_PORT_F_READLOG;
397                         sili_port_read_ncq_error(ap, target);
398                 }
399         }
400
401         /*
402          * Finally clean out the expired commands, we've probed the error
403          * status (or hopefully probed the error status).  Well, ok,
404          * we probably didn't XXX.
405          */
406         while (ap->ap_expired) {
407                 slot = ffs(ap->ap_expired) - 1;
408                 ap->ap_expired &= ~(1 << slot);
409                 KKASSERT(ap->ap_active & (1 << slot));
410                 ap->ap_active &= ~(1 << slot);
411                 --ap->ap_active_cnt;
412                 ccb = &ap->ap_ccbs[slot];
413                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
414                 ccb->ccb_done(ccb);
415                 ccb->ccb_xa.complete(&ccb->ccb_xa);
416         }
417
418         /*
419          * Wow.  All done.  We can get the port moving again.
420          */
421         if (ap->ap_probe == ATA_PROBE_FAILED) {
422                 kprintf("%s: reinit failed, port is dead\n", PORTNAME(ap));
423                 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
424                         TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
425                         ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
426                         ccb->ccb_xa.state = ATA_S_TIMEOUT;
427                         ccb->ccb_done(ccb);
428                         ccb->ccb_xa.complete(&ccb->ccb_xa);
429                 }
430         } else {
431                 sili_issue_pending_commands(ap, NULL);
432         }
433 }
434
435 /*
436  * Enable or re-enable interrupts on a port.
437  *
438  * This routine is called from the port initialization code or from the
439  * helper thread as the real interrupt may be forced to turn off certain
440  * interrupt sources.
441  */
442 void
443 sili_port_interrupt_enable(struct sili_port *ap)
444 {
445         u_int32_t data;
446
447         data =  SILI_PREG_INT_CCOMPLETE | SILI_PREG_INT_CERROR |
448                 SILI_PREG_INT_PHYRDYCHG | SILI_PREG_INT_DEVEXCHG |
449                 SILI_PREG_INT_DECODE | SILI_PREG_INT_CRC |
450                 SILI_PREG_INT_HANDSHK | SILI_PREG_INT_PMCHANGE;
451         if (ap->ap_sc->sc_flags & SILI_F_SSNTF)
452                 data |= SILI_PREG_INT_SDB;
453         sili_pwrite(ap, SILI_PREG_INT_ENABLE, data);
454 }
455
456 void
457 sili_port_interrupt_redisable(struct sili_port *ap)
458 {
459         u_int32_t data;
460
461         data = sili_read(ap->ap_sc, SILI_REG_GCTL);
462         data &= SILI_REG_GINT_PORTMASK;
463         data &= ~(1 << ap->ap_num);
464         sili_write(ap->ap_sc, SILI_REG_GCTL, data);
465 }
466
467 void
468 sili_port_interrupt_reenable(struct sili_port *ap)
469 {
470         u_int32_t data;
471
472         data = sili_read(ap->ap_sc, SILI_REG_GCTL);
473         data &= SILI_REG_GINT_PORTMASK;
474         data |= (1 << ap->ap_num);
475         sili_write(ap->ap_sc, SILI_REG_GCTL, data);
476 }
477
478 /*
479  * Run the port / target state machine from a main context.
480  *
481  * The state machine for the port is always run.
482  *
483  * If atx is non-NULL run the state machine for a particular target.
484  * If atx is NULL run the state machine for all targets.
485  */
486 void
487 sili_port_state_machine(struct sili_port *ap, int initial)
488 {
489         struct ata_port *at;
490         u_int32_t data;
491         int target;
492         int didsleep;
493         int loop;
494
495         /*
496          * State machine for port.  Note that CAM is not yet associated
497          * during the initial parallel probe and the port's probe state
498          * will not get past ATA_PROBE_NEED_IDENT.
499          */
500         {
501                 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
502                         kprintf("%s: Waiting 7 seconds on insertion\n",
503                                 PORTNAME(ap));
504                         sili_os_sleep(7000);
505                         initial = 1;
506                 }
507                 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
508                         sili_port_init(ap);
509                 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
510                         sili_port_reset(ap, NULL, 1);
511                 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
512                         sili_port_reset(ap, NULL, 0);
513                 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
514                         sili_cam_probe(ap, NULL);
515         }
516         if (ap->ap_type != ATA_PORT_T_PM) {
517                 if (ap->ap_probe == ATA_PROBE_FAILED) {
518                         sili_cam_changed(ap, NULL, 0);
519                 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
520                         sili_cam_changed(ap, NULL, 1);
521                 }
522                 return;
523         }
524
525         /*
526          * Port Multiplier state machine.
527          *
528          * Get a mask of changed targets and combine with any runnable
529          * states already present.
530          */
531         for (loop = 0; ;++loop) {
532                 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
533                         kprintf("%s: PM unable to read hot-plug bitmap\n",
534                                 PORTNAME(ap));
535                         break;
536                 }
537
538                 /*
539                  * Do at least one loop, then stop if no more state changes
540                  * have occured.  The PM might not generate a new
541                  * notification until we clear the entire bitmap.
542                  */
543                 if (loop && data == 0)
544                         break;
545
546                 /*
547                  * New devices showing up in the bitmap require some spin-up
548                  * time before we start probing them.  Reset didsleep.  The
549                  * first new device we detect will sleep before probing.
550                  *
551                  * This only applies to devices whos change bit is set in
552                  * the data, and does not apply to the initial boot-time
553                  * probe.
554                  */
555                 didsleep = 0;
556
557                 for (target = 0; target < ap->ap_pmcount; ++target) {
558                         at = &ap->ap_ata[target];
559
560                         /*
561                          * Check the target state for targets behind the PM
562                          * which have changed state.  This will adjust
563                          * at_probe and set ATA_PORT_F_RESCAN
564                          *
565                          * We want to wait at least 10 seconds before probing
566                          * a newly inserted device.  If the check status
567                          * indicates a device is present and in need of a
568                          * hard reset, we make sure we have slept before
569                          * continuing.
570                          *
571                          * We also need to wait at least 1 second for the
572                          * PHY state to change after insertion, if we
573                          * haven't already waited the 10 seconds.
574                          *
575                          * NOTE: When pm_check_good finds a good port it
576                          *       typically starts us in probe state
577                          *       NEED_HARD_RESET rather than INIT.
578                          */
579                         if (data & (1 << target)) {
580                                 if (initial == 0 && didsleep == 0)
581                                         sili_os_sleep(1000);
582                                 sili_pm_check_good(ap, target);
583                                 if (initial == 0 && didsleep == 0 &&
584                                     at->at_probe <= ATA_PROBE_NEED_HARD_RESET
585                                 ) {
586                                         didsleep = 1;
587                                         kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
588                                         sili_os_sleep(10000);
589                                 }
590                         }
591
592                         /*
593                          * Report hot-plug events before the probe state
594                          * really gets hot.  Only actual events are reported
595                          * here to reduce spew.
596                          */
597                         if (data & (1 << target)) {
598                                 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
599                                 switch(at->at_probe) {
600                                 case ATA_PROBE_NEED_INIT:
601                                 case ATA_PROBE_NEED_HARD_RESET:
602                                         kprintf("Device inserted\n");
603                                         break;
604                                 case ATA_PROBE_FAILED:
605                                         kprintf("Device removed\n");
606                                         break;
607                                 default:
608                                         kprintf("Device probe in progress\n");
609                                         break;
610                                 }
611                         }
612
613                         /*
614                          * Run through the state machine as necessary if
615                          * the port is not marked failed.
616                          *
617                          * The state machine may stop at NEED_IDENT if
618                          * CAM is not yet attached.
619                          *
620                          * Acquire exclusive access to the port while we
621                          * are doing this.  This prevents command-completion
622                          * from queueing commands for non-polled targets
623                          * inbetween our probe steps.  We need to do this
624                          * because the reset probes can generate severe PHY
625                          * and protocol errors and soft-brick the port.
626                          */
627                         if (at->at_probe != ATA_PROBE_FAILED &&
628                             at->at_probe != ATA_PROBE_GOOD) {
629                                 if (at->at_probe == ATA_PROBE_NEED_INIT)
630                                         sili_pm_port_init(ap, at);
631                                 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
632                                         sili_port_reset(ap, at, 1);
633                                 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
634                                         sili_port_reset(ap, at, 0);
635                                 if (at->at_probe == ATA_PROBE_NEED_IDENT)
636                                         sili_cam_probe(ap, at);
637                         }
638
639                         /*
640                          * Add or remove from CAM
641                          */
642                         if (at->at_features & ATA_PORT_F_RESCAN) {
643                                 at->at_features &= ~ATA_PORT_F_RESCAN;
644                                 if (at->at_probe == ATA_PROBE_FAILED) {
645                                         sili_cam_changed(ap, at, 0);
646                                 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
647                                         sili_cam_changed(ap, at, 1);
648                                 }
649                         }
650                         data &= ~(1 << target);
651                 }
652                 if (data) {
653                         kprintf("%s: WARNING (PM): extra bits set in "
654                                 "EINFO: %08x\n", PORTNAME(ap), data);
655                         while (target < SILI_MAX_PMPORTS) {
656                                 sili_pm_check_good(ap, target);
657                                 ++target;
658                         }
659                 }
660         }
661 }
662
663 /*
664  * De-initialize and detach a port.
665  */
666 void
667 sili_port_free(struct sili_softc *sc, u_int port)
668 {
669         struct sili_port                *ap = sc->sc_ports[port];
670         struct sili_ccb                 *ccb;
671
672         /*
673          * Ensure port is disabled and its interrupts are all flushed.
674          */
675         if (ap->ap_sc) {
676                 sili_os_stop_port(ap);
677                 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_MASK);
678                 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
679                 sili_write(ap->ap_sc, SILI_REG_GCTL,
680                         sili_read(ap->ap_sc, SILI_REG_GCTL) &
681                         ~SILI_REG_GINT_PORTST(ap->ap_num));
682         }
683
684         if (ap->ap_ccbs) {
685                 while ((ccb = sili_get_ccb(ap)) != NULL) {
686                         if (ccb->ccb_dmamap) {
687                                 bus_dmamap_destroy(sc->sc_tag_data,
688                                                    ccb->ccb_dmamap);
689                                 ccb->ccb_dmamap = NULL;
690                         }
691                 }
692                 if ((ccb = ap->ap_err_ccb) != NULL) {
693                         if (ccb->ccb_dmamap) {
694                                 bus_dmamap_destroy(sc->sc_tag_data,
695                                                    ccb->ccb_dmamap);
696                                 ccb->ccb_dmamap = NULL;
697                         }
698                         ap->ap_err_ccb = NULL;
699                 }
700                 kfree(ap->ap_ccbs, M_DEVBUF);
701                 ap->ap_ccbs = NULL;
702         }
703
704         if (ap->ap_dmamem_prbs) {
705                 sili_dmamem_free(sc, ap->ap_dmamem_prbs);
706                 ap->ap_dmamem_prbs = NULL;
707         }
708         if (ap->ap_ata) {
709                 kfree(ap->ap_ata, M_DEVBUF);
710                 ap->ap_ata = NULL;
711         }
712         if (ap->ap_err_scratch) {
713                 kfree(ap->ap_err_scratch, M_DEVBUF);
714                 ap->ap_err_scratch = NULL;
715         }
716
717         /* bus_space(9) says we dont free the subregions handle */
718
719         kfree(ap, M_DEVBUF);
720         sc->sc_ports[port] = NULL;
721 }
722
723 /*
724  * Reset a port.
725  *
726  * If hard is 0 perform a softreset of the port.
727  * If hard is 1 perform a hard reset of the port.
728  * If hard is 2 perform a hard reset of the port and cycle the phy.
729  *
730  * If at is non-NULL an indirect port via a port-multiplier is being
731  * reset, otherwise a direct port is being reset.
732  *
733  * NOTE: Indirect ports can only be soft-reset.
734  */
735 int
736 sili_port_reset(struct sili_port *ap, struct ata_port *at, int hard)
737 {
738         int rc;
739
740         if (hard) {
741                 if (at)
742                         rc = sili_pm_hardreset(ap, at->at_target, hard);
743                 else
744                         rc = sili_port_hardreset(ap);
745         } else {
746                 if (at)
747                         rc = sili_pm_softreset(ap, at->at_target);
748                 else
749                         rc = sili_port_softreset(ap);
750         }
751         return(rc);
752 }
753
754 /*
755  * SILI soft reset, Section 10.4.1
756  *
757  * (at) will be NULL when soft-resetting a directly-attached device, and
758  * non-NULL when soft-resetting a device through a port multiplier.
759  *
760  * This function keeps port communications intact and attempts to generate
761  * a reset to the connected device using device commands.
762  */
763 int
764 sili_port_softreset(struct sili_port *ap)
765 {
766         struct sili_ccb         *ccb = NULL;
767         struct sili_prb         *prb;
768         int                     error;
769         u_int32_t               sig;
770
771         error = EIO;
772
773         if (bootverbose)
774                 kprintf("%s: START SOFTRESET\n", PORTNAME(ap));
775
776         crit_enter();
777         ap->ap_state = AP_S_NORMAL;
778
779         /*
780          * Prep the special soft-reset SII command.
781          */
782         ccb = sili_get_err_ccb(ap);
783         ccb->ccb_done = sili_empty_done;
784         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
785         ccb->ccb_xa.complete = sili_dummy_done;
786         ccb->ccb_xa.at = NULL;
787
788         prb = ccb->ccb_prb;
789         bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
790         prb->prb_h2d.flags = 0;
791         prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
792         prb->prb_override = 0;
793         prb->prb_xfer_count = 0;
794
795         ccb->ccb_xa.state = ATA_S_PENDING;
796
797         /*
798          * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
799          */
800         if (sili_poll(ccb, 8000, sili_quick_timeout) != ATA_S_COMPLETE) {
801                 kprintf("%s: First FIS failed\n", PORTNAME(ap));
802                 goto err;
803         }
804
805         sig = (prb->prb_d2h.lba_high << 24) |
806               (prb->prb_d2h.lba_mid << 16) |
807               (prb->prb_d2h.lba_low << 8) |
808               (prb->prb_d2h.sector_count);
809         if (bootverbose)
810                 kprintf("%s: SOFTRESET SIGNATURE %08x\n", PORTNAME(ap), sig);
811
812         /*
813          * If the softreset is trying to clear a BSY condition after a
814          * normal portreset we assign the port type.
815          *
816          * If the softreset is being run first as part of the ccb error
817          * processing code then report if the device signature changed
818          * unexpectedly.
819          */
820         if (ap->ap_type == ATA_PORT_T_NONE) {
821                 ap->ap_type = sili_port_signature(ap, NULL, sig);
822         } else {
823                 if (sili_port_signature(ap, NULL, sig) != ap->ap_type) {
824                         kprintf("%s: device signature unexpectedly "
825                                 "changed\n", PORTNAME(ap));
826                         error = EBUSY; /* XXX */
827                 }
828         }
829         error = 0;
830 err:
831         if (ccb != NULL) {
832                 sili_put_err_ccb(ccb);
833         }
834
835         /*
836          * If we failed to softreset make the port quiescent, otherwise
837          * make sure the port's start/stop state matches what it was on
838          * entry.
839          *
840          * Don't kill the port if the softreset is on a port multiplier
841          * target, that would kill all the targets!
842          */
843         if (bootverbose) {
844                 kprintf("%s: END SOFTRESET %d prob=%d state=%d\n",
845                         PORTNAME(ap), error, ap->ap_probe, ap->ap_state);
846         }
847         if (error) {
848                 sili_port_hardstop(ap);
849                 /* ap_probe set to failed */
850         } else {
851                 ap->ap_probe = ATA_PROBE_NEED_IDENT;
852                 ap->ap_pmcount = 1;
853         }
854         crit_exit();
855
856         sili_pwrite(ap, SILI_PREG_SERR, -1);
857         if (bootverbose)
858                 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
859
860         return (error);
861 }
862
863 /*
864  * This function does a hard reset of the port.  Note that the device
865  * connected to the port could still end-up hung.  Phy detection is
866  * used to short-cut longer operations.
867  */
868 int
869 sili_port_hardreset(struct sili_port *ap)
870 {
871         u_int32_t data;
872         int     error;
873         int     loop;
874
875         if (bootverbose)
876                 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
877
878         ap->ap_state = AP_S_NORMAL;
879
880         /*
881          * Set SCTL up for any speed restrictions before issuing the
882          * device reset.   This may also take us out of an INIT state
883          * (if we were previously in a continuous reset state from
884          * sili_port_listen()).
885          */
886         data = SILI_PREG_SCTL_SPM_NONE |
887                SILI_PREG_SCTL_IPM_NONE |
888                SILI_PREG_SCTL_SPD_NONE |
889                SILI_PREG_SCTL_DET_NONE;
890         if (SiliForceGen1 & (1 << ap->ap_num)) {
891                 data &= ~SILI_PREG_SCTL_SPD_NONE;
892                 data |= SILI_PREG_SCTL_SPD_GEN1;
893         }
894         sili_pwrite(ap, SILI_PREG_SCTL, data);
895
896         /*
897          * The transition from a continuous COMRESET state from
898          * sili_port_listen() back to device detect can take a
899          * few seconds.  It's quite non-deterministic.  Most of
900          * the time it takes far less.  Use a polling loop to
901          * wait.
902          */
903         loop = 4000;
904         while (loop > 0) {
905                 data = sili_pread(ap, SILI_PREG_SSTS);
906                 if (data & SILI_PREG_SSTS_DET)
907                         break;
908                 loop -= sili_os_softsleep();
909         }
910         sili_os_sleep(100);
911
912         /*
913          * Issue Device Reset, give the phy a little time to settle down.
914          *
915          * NOTE:  Unlike Port Reset, the port ready signal will not
916          *        go active unless a device is established to be on
917          *        the port.
918          */
919         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
920         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
921         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
922         if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
923                 kprintf("%s: hardreset failed to clear\n", PORTNAME(ap));
924         }
925         sili_os_sleep(20);
926
927         /*
928          * Try to determine if there is a device on the port.
929          *
930          * Give the device 3/10 second to at least be detected.
931          */
932         loop = 300;
933         while (loop > 0) {
934                 data = sili_pread(ap, SILI_PREG_SSTS);
935                 if (data & SILI_PREG_SSTS_DET)
936                         break;
937                 loop -= sili_os_softsleep();
938         }
939         if (loop <= 0) {
940                 if (bootverbose) {
941                         kprintf("%s: Port appears to be unplugged\n",
942                                 PORTNAME(ap));
943                 }
944                 error = ENODEV;
945                 goto done;
946         }
947
948         /*
949          * There is something on the port.  Give the device 3 seconds
950          * to detect.
951          */
952         if (sili_pwait_eq(ap, 3000, SILI_PREG_SSTS,
953                           SILI_PREG_SSTS_DET, SILI_PREG_SSTS_DET_DEV)) {
954                 if (bootverbose) {
955                         kprintf("%s: Device may be powered down\n",
956                                 PORTNAME(ap));
957                 }
958                 error = ENODEV;
959                 goto pmdetect;
960         }
961
962         /*
963          * We got something that definitely looks like a device.  Give
964          * the device time to send us its first D2H FIS.
965          *
966          * This effectively waits for BSY to clear.
967          */
968         if (sili_pwait_set_to(ap, 3000, SILI_PREG_STATUS,
969                               SILI_PREG_STATUS_READY)) {
970                 error = EBUSY;
971         } else {
972                 error = 0;
973         }
974
975 pmdetect:
976         /*
977          * Do the PM port probe regardless of how things turned out above.
978          *
979          * If the PM port probe fails it will return the original error
980          * from above.
981          */
982         if (ap->ap_sc->sc_flags & SILI_F_SPM) {
983                 error = sili_pm_port_probe(ap, error);
984         }
985
986 done:
987         /*
988          * Finish up
989          */
990         switch(error) {
991         case 0:
992                 if (ap->ap_type == ATA_PORT_T_PM)
993                         ap->ap_probe = ATA_PROBE_GOOD;
994                 else
995                         ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
996                 break;
997         case ENODEV:
998                 /*
999                  * No device detected.
1000                  */
1001                 data = sili_pread(ap, SILI_PREG_SSTS);
1002
1003                 switch(data & SATA_PM_SSTS_DET) {
1004                 case SILI_PREG_SSTS_DET_DEV_NE:
1005                         kprintf("%s: Device not communicating\n",
1006                                 PORTNAME(ap));
1007                         break;
1008                 case SILI_PREG_SSTS_DET_OFFLINE:
1009                         kprintf("%s: PHY offline\n",
1010                                 PORTNAME(ap));
1011                         break;
1012                 default:
1013                         kprintf("%s: No device detected\n",
1014                                 PORTNAME(ap));
1015                         break;
1016                 }
1017                 sili_port_hardstop(ap);
1018                 break;
1019         default:
1020                 /*
1021                  * (EBUSY)
1022                  */
1023                 kprintf("%s: Device on port is bricked\n",
1024                         PORTNAME(ap));
1025                 sili_port_hardstop(ap);
1026                 break;
1027         }
1028         sili_pwrite(ap, SILI_PREG_SERR, -1);
1029
1030         if (bootverbose)
1031                 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1032         return (error);
1033 }
1034
1035 /*
1036  * Hard-stop on hot-swap device removal.  See 10.10.1
1037  *
1038  * Place the port in a mode that will allow it to detect hot-swap insertions.
1039  * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1040  * seem to do the job.
1041  */
1042 void
1043 sili_port_hardstop(struct sili_port *ap)
1044 {
1045         struct sili_ccb *ccb;
1046         struct ata_port *at;
1047         int i;
1048         int slot;
1049
1050         ap->ap_state = AP_S_FATAL_ERROR;
1051         ap->ap_probe = ATA_PROBE_FAILED;
1052         ap->ap_type = ATA_PORT_T_NONE;
1053
1054         /*
1055          * Clean up AT sub-ports on SATA port.
1056          */
1057         for (i = 0; ap->ap_ata && i < SILI_MAX_PMPORTS; ++i) {
1058                 at = &ap->ap_ata[i];
1059                 at->at_type = ATA_PORT_T_NONE;
1060                 at->at_probe = ATA_PROBE_FAILED;
1061                 at->at_features &= ~ATA_PORT_F_READLOG;
1062         }
1063
1064         /*
1065          * Kill the port.  Don't bother waiting for it to transition
1066          * back up.
1067          */
1068         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESET);
1069         if (sili_pread(ap, SILI_PREG_STATUS) & SILI_PREG_STATUS_READY) {
1070                 kprintf("%s: Port will not go into reset\n",
1071                         PORTNAME(ap));
1072         }
1073         sili_os_sleep(10);
1074         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESET);
1075
1076         /*
1077          * Turn off port-multiplier control bit
1078          */
1079         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
1080
1081         /*
1082          * Clean up the command list.
1083          */
1084         while (ap->ap_active) {
1085                 slot = ffs(ap->ap_active) - 1;
1086                 ap->ap_active &= ~(1 << slot);
1087                 ap->ap_expired &= ~(1 << slot);
1088                 --ap->ap_active_cnt;
1089                 ccb = &ap->ap_ccbs[slot];
1090                 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1091                         callout_stop(&ccb->ccb_timeout);
1092                         ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1093                 }
1094                 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1095                                        ATA_F_TIMEOUT_EXPIRED);
1096                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1097                 ccb->ccb_done(ccb);
1098                 ccb->ccb_xa.complete(&ccb->ccb_xa);
1099         }
1100         while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1101                 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1102                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1103                 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1104                 ccb->ccb_done(ccb);
1105                 ccb->ccb_xa.complete(&ccb->ccb_xa);
1106         }
1107         KKASSERT(ap->ap_active_cnt == 0);
1108
1109         /*
1110          * Put the port into a listen mode, we want to get insertion/removal
1111          * events.
1112          */
1113         sili_port_listen(ap);
1114 }
1115
1116 /*
1117  * Place port into a listen mode for hotplug events only.  The port has
1118  * already been reset and the command processor may not be ready due
1119  * to the lack of a device.
1120  */
1121 void
1122 sili_port_listen(struct sili_port *ap)
1123 {
1124         u_int32_t data;
1125
1126 #if 1
1127         data = SILI_PREG_SCTL_SPM_NONE |
1128                SILI_PREG_SCTL_IPM_NONE |
1129                SILI_PREG_SCTL_SPD_NONE |
1130                SILI_PREG_SCTL_DET_INIT;
1131         if (SiliForceGen1 & (1 << ap->ap_num)) {
1132                 data &= ~SILI_PREG_SCTL_SPD_NONE;
1133                 data |= SILI_PREG_SCTL_SPD_GEN1;
1134         }
1135 #endif
1136         sili_os_sleep(20);
1137         sili_pwrite(ap, SILI_PREG_SERR, -1);
1138         sili_pwrite(ap, SILI_PREG_INT_ENABLE, SILI_PREG_INT_PHYRDYCHG |
1139                                               SILI_PREG_INT_DEVEXCHG);
1140 }
1141
1142 /*
1143  * Figure out what type of device is connected to the port, ATAPI or
1144  * DISK.
1145  */
1146 int
1147 sili_port_signature(struct sili_port *ap, struct ata_port *at, u_int32_t sig)
1148 {
1149         if (bootverbose)
1150                 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1151         if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1152                 return(ATA_PORT_T_ATAPI);
1153         } else if ((sig & 0xffff0000) ==
1154                  (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1155                 return(ATA_PORT_T_PM);
1156         } else {
1157                 return(ATA_PORT_T_DISK);
1158         }
1159 }
1160
1161 /*
1162  * Load the DMA descriptor table for a CCB's buffer.
1163  *
1164  * NOTE: ATA_F_PIO is auto-selected by sili part.
1165  */
1166 int
1167 sili_load_prb(struct sili_ccb *ccb)
1168 {
1169         struct sili_port                *ap = ccb->ccb_port;
1170         struct sili_softc               *sc = ap->ap_sc;
1171         struct ata_xfer                 *xa = &ccb->ccb_xa;
1172         struct sili_prb                 *prb = ccb->ccb_prb;
1173         struct sili_sge                 *sge;
1174         bus_dmamap_t                    dmap = ccb->ccb_dmamap;
1175         int                             error;
1176
1177         /*
1178          * Set up the PRB.  The PRB contains 2 SGE's (1 if it is an ATAPI
1179          * command).  The SGE must be set up to link to the rest of our
1180          * SGE array, in blocks of four SGEs (a SGE table) starting at
1181          */
1182         prb->prb_xfer_count = 0;
1183         prb->prb_control = 0;
1184         prb->prb_override = 0;
1185         sge = (ccb->ccb_xa.flags & ATA_F_PACKET) ?
1186                 &prb->prb_sge_packet : &prb->prb_sge_normal;
1187         if (xa->datalen == 0) {
1188                 sge->sge_flags = SILI_SGE_FLAGS_TRM | SILI_SGE_FLAGS_DRD;
1189                 sge->sge_count = 0;
1190                 return (0);
1191         }
1192
1193         if (ccb->ccb_xa.flags & ATA_F_READ)
1194                 prb->prb_control |= SILI_PRB_CTRL_READ;
1195         if (ccb->ccb_xa.flags & ATA_F_WRITE)
1196                 prb->prb_control |= SILI_PRB_CTRL_WRITE;
1197         sge->sge_flags = SILI_SGE_FLAGS_LNK;
1198         sge->sge_count = 0;
1199         sge->sge_paddr = ccb->ccb_prb_paddr +
1200                          offsetof(struct sili_prb, prb_sge[0]);
1201
1202         /*
1203          * Load our sge array.
1204          */
1205         error = bus_dmamap_load(sc->sc_tag_data, dmap,
1206                                 xa->data, xa->datalen,
1207                                 sili_load_prb_callback,
1208                                 ccb,
1209                                 ((xa->flags & ATA_F_NOWAIT) ?
1210                                     BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1211         if (error != 0) {
1212                 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1213                 return (1);
1214         }
1215
1216         bus_dmamap_sync(sc->sc_tag_data, dmap,
1217                         (xa->flags & ATA_F_READ) ?
1218                             BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1219
1220         return (0);
1221 }
1222
1223 /*
1224  * Callback from BUSDMA system to load the segment list.
1225  *
1226  * The scatter/gather table is loaded by the sili chip in blocks of
1227  * four SGE's.  If a continuance is required the last entry in each
1228  * block must point to the next block.
1229  */
1230 static
1231 void
1232 sili_load_prb_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1233                         int error)
1234 {
1235         struct sili_ccb *ccb = info;
1236         struct sili_sge *sge;
1237         int sgi;
1238
1239         KKASSERT(nsegs <= SILI_MAX_SGET);
1240
1241         sgi = 0;
1242         sge = &ccb->ccb_prb->prb_sge[0];
1243         while (nsegs) {
1244                 if ((sgi & 3) == 3) {
1245                         sge->sge_paddr = htole64(ccb->ccb_prb_paddr +
1246                                                  offsetof(struct sili_prb,
1247                                                         prb_sge[sgi + 1]));
1248                         sge->sge_count = 0;
1249                         sge->sge_flags = SILI_SGE_FLAGS_LNK;
1250                 } else {
1251                         sge->sge_paddr = htole64(segs->ds_addr);
1252                         sge->sge_count = htole32(segs->ds_len);
1253                         sge->sge_flags = 0;
1254                         --nsegs;
1255                         ++segs;
1256                 }
1257                 ++sge;
1258                 ++sgi;
1259         }
1260         --sge;
1261         sge->sge_flags |= SILI_SGE_FLAGS_TRM;
1262 }
1263
1264 void
1265 sili_unload_prb(struct sili_ccb *ccb)
1266 {
1267         struct sili_port                *ap = ccb->ccb_port;
1268         struct sili_softc               *sc = ap->ap_sc;
1269         struct ata_xfer                 *xa = &ccb->ccb_xa;
1270         bus_dmamap_t                    dmap = ccb->ccb_dmamap;
1271
1272         if (xa->datalen != 0) {
1273                 bus_dmamap_sync(sc->sc_tag_data, dmap,
1274                                 (xa->flags & ATA_F_READ) ?
1275                                 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1276
1277                 bus_dmamap_unload(sc->sc_tag_data, dmap);
1278
1279                 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1280                         xa->resid = 0;
1281                 else
1282                         xa->resid = xa->datalen -
1283                                     le32toh(ccb->ccb_prb->prb_xfer_count);
1284         }
1285 }
1286
1287 /*
1288  * Start a command and poll for completion.
1289  *
1290  * timeout is in ms and only counts once the command gets on-chip.
1291  *
1292  * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1293  * that no error occured.
1294  *
1295  * NOTE: If the caller specifies a NULL timeout function the caller is
1296  *       responsible for clearing hardware state on failure, but we will
1297  *       deal with removing the ccb from any pending queue.
1298  *
1299  * NOTE: NCQ should never be used with this function.
1300  *
1301  * NOTE: If the port is in a failed state and stopped we do not try
1302  *       to activate the ccb.
1303  */
1304 int
1305 sili_poll(struct sili_ccb *ccb, int timeout,
1306           void (*timeout_fn)(struct sili_ccb *))
1307 {
1308         struct sili_port *ap = ccb->ccb_port;
1309
1310         if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1311                 ccb->ccb_xa.state = ATA_S_ERROR;
1312                 return(ccb->ccb_xa.state);
1313         }
1314
1315         sili_start(ccb);
1316
1317         do {
1318                 sili_port_intr(ap, 1);
1319                 switch(ccb->ccb_xa.state) {
1320                 case ATA_S_ONCHIP:
1321                         timeout -= sili_os_softsleep();
1322                         break;
1323                 case ATA_S_PENDING:
1324                         /*
1325                          * The packet can get stuck on the pending queue
1326                          * if the port refuses to come ready.  XXX
1327                          */
1328 #if 0
1329                         if (xxx AP_F_EXCLUSIVE_ACCESS)
1330                                 timeout -= sili_os_softsleep();
1331                         else
1332 #endif
1333                                 sili_os_softsleep();
1334                         sili_check_active_timeouts(ap);
1335                         break;
1336                 default:
1337                         return (ccb->ccb_xa.state);
1338                 }
1339         } while (timeout > 0);
1340
1341         /*
1342          * Don't spew if this is a probe during hard reset
1343          */
1344         if (ap->ap_probe != ATA_PROBE_NEED_HARD_RESET) {
1345                 kprintf("%s: Poll timeout slot %d\n",
1346                         ATANAME(ap, ccb->ccb_xa.at),
1347                         ccb->ccb_slot);
1348         }
1349
1350         timeout_fn(ccb);
1351
1352         return(ccb->ccb_xa.state);
1353 }
1354
1355 /*
1356  * When polling we have to check if the currently active CCB(s)
1357  * have timed out as the callout will be deadlocked while we
1358  * hold the port lock.
1359  */
1360 void
1361 sili_check_active_timeouts(struct sili_port *ap)
1362 {
1363         struct sili_ccb *ccb;
1364         u_int32_t mask;
1365         int tag;
1366
1367         mask = ap->ap_active;
1368         while (mask) {
1369                 tag = ffs(mask) - 1;
1370                 mask &= ~(1 << tag);
1371                 ccb = &ap->ap_ccbs[tag];
1372                 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1373                         sili_core_timeout(ccb, 0);
1374                 }
1375         }
1376 }
1377
1378 static
1379 __inline
1380 void
1381 sili_start_timeout(struct sili_ccb *ccb)
1382 {
1383         if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1384                 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1385                 callout_reset(&ccb->ccb_timeout,
1386                               (ccb->ccb_xa.timeout * hz + 999) / 1000,
1387                               sili_ata_cmd_timeout_unserialized, ccb);
1388         }
1389 }
1390
1391 void
1392 sili_start(struct sili_ccb *ccb)
1393 {
1394         struct sili_port                *ap = ccb->ccb_port;
1395 #if 0
1396         struct sili_softc               *sc = ap->ap_sc;
1397 #endif
1398
1399         KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1400
1401         /*
1402          * Sync our SGE table and PRB
1403          */
1404         bus_dmamap_sync(ap->ap_dmamem_prbs->adm_tag,
1405                         ap->ap_dmamem_prbs->adm_map,
1406                         BUS_DMASYNC_PREWRITE);
1407
1408         /*
1409          * XXX dmamap for PRB XXX  BUS_DMASYNC_PREWRITE
1410          */
1411
1412         /*
1413          * Controller will update shared memory!
1414          * XXX bus_dmamap_sync ... BUS_DMASYNC_PREREAD ...
1415          */
1416         /* Prepare RFIS area for write by controller */
1417
1418         /*
1419          * There's no point trying to optimize this, it only shaves a few
1420          * nanoseconds so just queue the command and call our generic issue.
1421          */
1422         sili_issue_pending_commands(ap, ccb);
1423 }
1424
1425 #if 0
1426 /*
1427  * While holding the port lock acquire exclusive access to the port.
1428  *
1429  * This is used when running the state machine to initialize and identify
1430  * targets over a port multiplier.  Setting exclusive access prevents
1431  * sili_port_intr() from activating any requests sitting on the pending
1432  * queue.
1433  */
1434 void
1435 sili_beg_exclusive_access(struct sili_port *ap, struct ata_port *at)
1436 {
1437         KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1438         ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1439         while (ap->ap_active) {
1440                 sili_port_intr(ap, 1);
1441                 sili_os_softsleep();
1442         }
1443 }
1444
1445 void
1446 sili_end_exclusive_access(struct sili_port *ap, struct ata_port *at)
1447 {
1448         KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
1449         ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
1450         sili_issue_pending_commands(ap, NULL);
1451 }
1452 #endif
1453
1454 /*
1455  * If ccb is not NULL enqueue and/or issue it.
1456  *
1457  * If ccb is NULL issue whatever we can from the queue.  However, nothing
1458  * new is issued if the exclusive access flag is set or expired ccb's are
1459  * present.
1460  *
1461  * If existing commands are still active (ap_active) we can only
1462  * issue matching new commands.
1463  */
1464 void
1465 sili_issue_pending_commands(struct sili_port *ap, struct sili_ccb *ccb)
1466 {
1467         /*
1468          * Enqueue the ccb.
1469          *
1470          * If just running the queue and in exclusive access mode we
1471          * just return.  Also in this case if there are any expired ccb's
1472          * we want to clear the queue so the port can be safely stopped.
1473          *
1474          * XXX sili chip - expiration needs to be per-target if PM supports
1475          *      FBSS?
1476          */
1477         if (ccb) {
1478                 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1479         } else if (ap->ap_expired) {
1480                 return;
1481         }
1482
1483         /*
1484          * Pull the next ccb off the queue and run it if possible.
1485          * If the port is not ready to accept commands enable the
1486          * ready interrupt instead of starting a new command.
1487          *
1488          * XXX limit ncqdepth for attached devices behind PM
1489          */
1490         while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1491                 /*
1492                  * Port may be wedged.
1493                  */
1494                 if ((sili_pread(ap, SILI_PREG_STATUS) &
1495                     SILI_PREG_STATUS_READY) == 0) {
1496                         kprintf("%s: slot %d NOT READY\n",
1497                                 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
1498                         sili_pwrite(ap, SILI_PREG_INT_ENABLE,
1499                                     SILI_PREG_INT_READY);
1500                         break;
1501                 }
1502
1503                 /*
1504                  * Handle exclusivity requirements.  ATA_F_EXCLUSIVE is used
1505                  * when we may have to access the rfis which is stored in
1506                  * the LRAM PRB.  Unfortunately reading the LRAM PRB is
1507                  * highly problematic, so requests (like PM requests) which
1508                  * need to access the rfis use exclusive mode and then
1509                  * access the copy made by the port interrupt code back in
1510                  * host memory.
1511                  */
1512                 if (ap->ap_active & ~ap->ap_expired) {
1513                         /*
1514                          * There may be multiple ccb's already running,
1515                          * if any are running and ap_run_flags sets
1516                          * one of these flags then we know only one is
1517                          * running.
1518                          *
1519                          * XXX Current AUTOSENSE code forces exclusivity
1520                          *     to simplify the code.
1521                          */
1522                         if (ap->ap_run_flags &
1523                             (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1524                                 break;
1525                         }
1526
1527                         /*
1528                          * If the ccb we want to run is exclusive and ccb's
1529                          * are still active on the port, we can't queue it
1530                          * yet.
1531                          *
1532                          * XXX Current AUTOSENSE code forces exclusivity
1533                          *     to simplify the code.
1534                          */
1535                         if (ccb->ccb_xa.flags &
1536                             (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
1537                                 break;
1538                         }
1539                 }
1540
1541                 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1542                 ccb->ccb_xa.state = ATA_S_ONCHIP;
1543                 ap->ap_active |= 1 << ccb->ccb_slot;
1544                 ap->ap_active_cnt++;
1545                 ap->ap_run_flags = ccb->ccb_xa.flags;
1546
1547                 /*
1548                  * We can't use the CMD_FIFO method because it requires us
1549                  * building the PRB in the LRAM, and the LRAM is buggy.  So
1550                  * we use host memory for the PRB.
1551                  */
1552                 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot),
1553                             (u_int32_t)ccb->ccb_prb_paddr);
1554                 sili_pwrite(ap, SILI_PREG_CMDACT(ccb->ccb_slot) + 4,
1555                             (u_int32_t)(ccb->ccb_prb_paddr >> 32));
1556                 /* sili_pwrite(ap, SILI_PREG_CMD_FIFO, ccb->ccb_slot); */
1557                 sili_start_timeout(ccb);
1558         }
1559 }
1560
1561 void
1562 sili_intr(void *arg)
1563 {
1564         struct sili_softc       *sc = arg;
1565         struct sili_port        *ap;
1566         u_int32_t               gint;
1567         int                     port;
1568
1569         /*
1570          * Check if the master enable is up, and whether any interrupts are
1571          * pending.
1572          *
1573          * Clear the ints we got.
1574          */
1575         if ((sc->sc_flags & SILI_F_INT_GOOD) == 0)
1576                 return;
1577         gint = sili_read(sc, SILI_REG_GINT);
1578         if (gint == 0 || gint == 0xffffffff)
1579                 return;
1580         sili_write(sc, SILI_REG_GINT, gint);
1581
1582         /*
1583          * Process interrupts for each port in a non-blocking fashion.
1584          */
1585         while (gint & SILI_REG_GINT_PORTMASK) {
1586                 port = ffs(gint) - 1;
1587                 ap = sc->sc_ports[port];
1588                 if (ap) {
1589                         if (sili_os_lock_port_nb(ap) == 0) {
1590                                 sili_port_intr(ap, 0);
1591                                 sili_os_unlock_port(ap);
1592                         } else {
1593                                 sili_port_interrupt_redisable(ap);
1594                                 sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1595                         }
1596                 }
1597                 gint &= ~(1 << port);
1598         }
1599 }
1600
1601 /*
1602  * Core called from helper thread.
1603  */
1604 void
1605 sili_port_thread_core(struct sili_port *ap, int mask)
1606 {
1607         /*
1608          * Process any expired timedouts.
1609          */
1610         sili_os_lock_port(ap);
1611         if (mask & AP_SIGF_TIMEOUT) {
1612                 sili_check_active_timeouts(ap);
1613         }
1614
1615         /*
1616          * Process port interrupts which require a higher level of
1617          * intervention.
1618          */
1619         if (mask & AP_SIGF_PORTINT) {
1620                 sili_port_intr(ap, 1);
1621                 sili_port_interrupt_reenable(ap);
1622                 sili_os_unlock_port(ap);
1623         } else {
1624                 sili_os_unlock_port(ap);
1625         }
1626 }
1627
1628 /*
1629  * Core per-port interrupt handler.
1630  *
1631  * If blockable is 0 we cannot call sili_os_sleep() at all and we can only
1632  * deal with normal command completions which do not require blocking.
1633  */
1634 void
1635 sili_port_intr(struct sili_port *ap, int blockable)
1636 {
1637         struct sili_softc       *sc = ap->ap_sc;
1638         u_int32_t               is;
1639         int                     slot;
1640         struct sili_ccb         *ccb = NULL;
1641         struct ata_port         *ccb_at = NULL;
1642         u_int32_t               active;
1643         u_int32_t               finished;
1644         const u_int32_t         blockable_mask = SILI_PREG_IST_PHYRDYCHG |
1645                                                  SILI_PREG_IST_DEVEXCHG |
1646                                                  SILI_PREG_IST_CERROR |
1647                                                  SILI_PREG_IST_DECODE |
1648                                                  SILI_PREG_IST_CRC |
1649                                                  SILI_PREG_IST_HANDSHK;
1650         const u_int32_t         fatal_mask     = SILI_PREG_IST_PHYRDYCHG |
1651                                                  SILI_PREG_IST_DEVEXCHG |
1652                                                  SILI_PREG_IST_DECODE |
1653                                                  SILI_PREG_IST_CRC |
1654                                                  SILI_PREG_IST_HANDSHK;
1655
1656         enum { NEED_NOTHING, NEED_HOTPLUG_INSERT,
1657                NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1658
1659         /*
1660          * NOTE: CCOMPLETE was automatically cleared when we read INT_STATUS.
1661          */
1662         is = sili_pread(ap, SILI_PREG_INT_STATUS);
1663         is &= SILI_PREG_IST_MASK;
1664         if (is & SILI_PREG_IST_CCOMPLETE)
1665                 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CCOMPLETE);
1666
1667         /*
1668          * If we can't block then we can't handle these here.  Disable
1669          * the interrupts in question so we don't live-lock, the helper
1670          * thread will re-enable them.
1671          *
1672          * If the port is in a completely failed state we do not want
1673          * to drop through to failed-command-processing if blockable is 0,
1674          * just let the thread deal with it all.
1675          *
1676          * Otherwise we fall through and still handle DHRS and any commands
1677          * which completed normally.  Even if we are errored we haven't
1678          * stopped the port yet so CI/SACT are still good.
1679          */
1680         if (blockable == 0) {
1681                 if (ap->ap_state == AP_S_FATAL_ERROR) {
1682                         sili_port_interrupt_redisable(ap);
1683                         sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1684                         /*is &= ~blockable_mask;*/
1685                         return;
1686                 }
1687                 if (is & blockable_mask) {
1688                         sili_port_interrupt_redisable(ap);
1689                         sili_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1690                         /*is &= ~blockable_mask;*/
1691                         return;
1692                 }
1693         }
1694
1695         if (is & SILI_PREG_IST_CERROR) {
1696                 /*
1697                  * Command failed (blockable).
1698                  *
1699                  * This stops command processing.  We can extract the PM
1700                  * target from the PMP field in SILI_PREG_CONTEXT.  The
1701                  * tag is not necessarily valid so don't use that.
1702                  *
1703                  * We must then expire all CCB's for that target and resume
1704                  * processing if any other targets have active commands.
1705                  * Particular error codes can be recovered by reading the LOG
1706                  * page.
1707                  *
1708                  * The expire handling code will do the rest, which is
1709                  * basically to reset the port once the only active
1710                  * commands remaining are all expired.
1711                  */
1712                 u_int32_t error;
1713                 int       target;
1714                 int       resume = 1;
1715
1716                 target = (sili_pread(ap, SILI_PREG_CONTEXT) >>
1717                           SILI_PREG_CONTEXT_PMPORT_SHIFT) &
1718                           SILI_PREG_CONTEXT_PMPORT_MASK;
1719                 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_CERROR);
1720                 active = ap->ap_active & ~ap->ap_expired;
1721                 error = sili_pread(ap, SILI_PREG_CERROR);
1722                 kprintf("%s.%d target error %d active=%08x hactive=%08x "
1723                         "SERR=%b\n",
1724                         PORTNAME(ap), target, error,
1725                         active, sili_pread(ap, SILI_PREG_SLOTST),
1726                         sili_pread(ap, SILI_PREG_SERR), SILI_PFMT_SERR);
1727
1728                 while (active) {
1729                         slot = ffs(active) - 1;
1730                         ccb = &ap->ap_ccbs[slot];
1731                         if ((ccb_at = ccb->ccb_xa.at) == NULL)
1732                                 ccb_at = &ap->ap_ata[0];
1733                         if (target == ccb_at->at_target) {
1734                                 if (ccb->ccb_xa.flags & ATA_F_NCQ &&
1735                                     (error == SILI_PREG_CERROR_DEVICE ||
1736                                      error == SILI_PREG_CERROR_SDBERROR)) {
1737                                         ccb_at->at_features |= ATA_PORT_F_READLOG;
1738                                 }
1739                                 if (sili_core_timeout(ccb, 1) == 0)
1740                                         resume = 0;
1741                         }
1742                         active &= ~(1 << slot);
1743                 }
1744
1745                 /*
1746                  * Resume will be 0 if the timeout reinited and restarted
1747                  * the port.  Otherwise we resume the port to allow other
1748                  * commands to complete.
1749                  */
1750                 if (resume)
1751                         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_RESUME);
1752         }
1753
1754         /*
1755          * Device notification to us (non-blocking)
1756          *
1757          * This is interrupt status SILIPREG_IST_SDB
1758          *
1759          * NOTE!  On some parts notification bits can get set without
1760          *        generating an interrupt.  It is unclear whether this is
1761          *        a bug in the PM (sending a DTOH device setbits with 'N' set
1762          *        and 'I' not set), or a bug in the host controller.
1763          *
1764          *        It only seems to occur under load.
1765          */
1766         if (sc->sc_flags & SILI_F_SSNTF) {
1767                 u_int32_t data;
1768                 const char *xstr;
1769
1770                 data = sili_pread(ap, SILI_PREG_SNTF);
1771                 if (is & SILI_PREG_IST_SDB) {
1772                         sili_pwrite(ap, SILI_PREG_INT_STATUS,
1773                                     SILI_PREG_IST_SDB);
1774                         is &= ~SILI_PREG_IST_SDB;
1775                         xstr = " (no SDBS!)";
1776                 } else {
1777                         xstr = "";
1778                 }
1779                 if (data) {
1780                         kprintf("%s: NOTIFY %08x%s\n",
1781                                 PORTNAME(ap), data, xstr);
1782                         sili_pwrite(ap, SILI_PREG_SNTF, data);
1783                         sili_cam_changed(ap, NULL, -1);
1784                 }
1785         }
1786
1787         /*
1788          * Port change (hot-plug) (blockable).
1789          *
1790          * A PCS interrupt will occur on hot-plug once communication is
1791          * established.
1792          *
1793          * A PRCS interrupt will occur on hot-unplug (and possibly also
1794          * on hot-plug).
1795          *
1796          * XXX We can then check the CPS (Cold Presence State) bit, if
1797          * supported, to determine if a device is plugged in or not and do
1798          * the right thing.
1799          *
1800          * WARNING:  A PCS interrupt is cleared by clearing DIAG_X, and
1801          *           can also occur if an unsolicited COMINIT is received.
1802          *           If this occurs command processing is automatically
1803          *           stopped (CR goes inactive) and the port must be stopped
1804          *           and restarted.
1805          */
1806         if (is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG)) {
1807                 /* XXX */
1808                 sili_pwrite(ap, SILI_PREG_SERR,
1809                         (SILI_PREG_SERR_DIAG_N | SILI_PREG_SERR_DIAG_X));
1810                 sili_pwrite(ap, SILI_PREG_INT_STATUS,
1811                     is & (SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG));
1812
1813                 is &= ~(SILI_PREG_IST_PHYRDYCHG | SILI_PREG_IST_DEVEXCHG);
1814                 kprintf("%s: Port change\n", PORTNAME(ap));
1815
1816                 switch (sili_pread(ap, SILI_PREG_SSTS) & SILI_PREG_SSTS_DET) {
1817                 case SILI_PREG_SSTS_DET_DEV:
1818                         if (ap->ap_type == ATA_PORT_T_NONE &&
1819                             ap->ap_probe == ATA_PROBE_FAILED) {
1820                                 need = NEED_HOTPLUG_INSERT;
1821                                 goto fatal;
1822                         }
1823                         break;
1824                 default:
1825                         kprintf("%s: Device lost\n", PORTNAME(ap));
1826                         if (ap->ap_type != ATA_PORT_T_NONE) {
1827                                 need = NEED_HOTPLUG_REMOVE;
1828                                 goto fatal;
1829                         }
1830                         break;
1831                 }
1832         }
1833
1834         /*
1835          * Check for remaining errors - they are fatal. (blockable)
1836          */
1837         if (is & fatal_mask) {
1838                 u_int32_t serr;
1839
1840                 sili_pwrite(ap, SILI_PREG_INT_STATUS, is & fatal_mask);
1841
1842                 serr = sili_pread(ap, SILI_PREG_SERR);
1843                 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
1844                         "disabling port.\n",
1845                         PORTNAME(ap),
1846                         is, SILI_PFMT_INT_STATUS,
1847                         serr, SILI_PFMT_SERR
1848                 );
1849                 is &= ~fatal_mask;
1850                 /* XXX try recovery first */
1851                 goto fatal;
1852         }
1853
1854         /*
1855          * Fail all outstanding commands if we know the port won't recover.
1856          *
1857          * We may have a ccb_at if the failed command is known and was
1858          * being sent to a device over a port multiplier (PM).  In this
1859          * case if the port itself has not completely failed we fail just
1860          * the commands related to that target.
1861          */
1862         if (ap->ap_state == AP_S_FATAL_ERROR &&
1863             (ap->ap_active & ~ap->ap_expired)) {
1864                 kprintf("%s: Fatal port error, expiring %08x\n",
1865                         PORTNAME(ap), ap->ap_active & ~ap->ap_expired);
1866 fatal:
1867                 ap->ap_state = AP_S_FATAL_ERROR;
1868
1869                 /*
1870                  * Error all the active slots.  If running across a PM
1871                  * try to error out just the slots related to the target.
1872                  */
1873                 active = ap->ap_active & ~ap->ap_expired;
1874
1875                 while (active) {
1876                         slot = ffs(active) - 1;
1877                         active &= ~(1 << slot);
1878                         ccb = &ap->ap_ccbs[slot];
1879                         sili_core_timeout(ccb, 1);
1880                 }
1881         }
1882
1883         /*
1884          * CCB completion (non blocking).
1885          *
1886          * CCB completion is detected by noticing the slot bit in
1887          * the port slot status register has cleared while the bit
1888          * is still set in our ap_active variable.
1889          *
1890          * When completing expired events we must remember to reinit
1891          * the port once everything is clear.
1892          *
1893          * Due to a single-level recursion when reading the log page,
1894          * it is possible for the slot to already have been cleared
1895          * for some expired tags, do not include expired tags in
1896          * the list.
1897          */
1898         active = ap->ap_active & ~sili_pread(ap, SILI_PREG_SLOTST);
1899         active &= ~ap->ap_expired;
1900
1901         finished = active;
1902         while (active) {
1903                 slot = ffs(active) - 1;
1904                 ccb = &ap->ap_ccbs[slot];
1905
1906                 DPRINTF(SILI_D_INTR, "%s: slot %d is complete%s\n",
1907                     PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
1908                     " (error)" : "");
1909
1910                 active &= ~(1 << slot);
1911
1912                 /*
1913                  * XXX sync POSTREAD for return data?
1914                  */
1915                 ap->ap_active &= ~(1 << ccb->ccb_slot);
1916                 --ap->ap_active_cnt;
1917
1918                 /*
1919                  * Complete the ccb.  If the ccb was marked expired it
1920                  * may or may not have been cleared from the port,
1921                  * make sure we mark it as having timed out.
1922                  *
1923                  * In a normal completion if AUTOSENSE is set we copy
1924                  * the PRB LRAM rfis back to the rfis in host-memory.
1925                  *
1926                  * XXX Currently AUTOSENSE also forces exclusivity so we
1927                  *     can safely work around a hardware bug when reading
1928                  *     the LRAM.
1929                  */
1930                 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
1931                         ap->ap_expired &= ~(1 << ccb->ccb_slot);
1932                         ccb->ccb_xa.state = ATA_S_TIMEOUT;
1933                         ccb->ccb_done(ccb);
1934                         ccb->ccb_xa.complete(&ccb->ccb_xa);
1935                 } else {
1936                         if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
1937                                 ccb->ccb_xa.state = ATA_S_COMPLETE;
1938                                 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
1939                                         memcpy(ccb->ccb_xa.rfis,
1940                                                &ccb->ccb_prb_lram->prb_d2h,
1941                                                sizeof(ccb->ccb_prb_lram->prb_d2h));
1942                                         if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
1943                                                 ccb->ccb_xa.state = ATA_S_ERROR;
1944                                 }
1945                         }
1946                         ccb->ccb_done(ccb);
1947                 }
1948         }
1949         if (is & SILI_PREG_IST_READY) {
1950                 is &= ~SILI_PREG_IST_READY;
1951                 sili_pwrite(ap, SILI_PREG_INT_DISABLE, SILI_PREG_INT_READY);
1952                 sili_pwrite(ap, SILI_PREG_INT_STATUS, SILI_PREG_IST_READY);
1953         }
1954
1955         /*
1956          * If we had expired commands and were waiting for
1957          * remaining commands to complete, and they have now
1958          * completed, we can reinit the port.
1959          *
1960          * This will also clean out the expired commands.
1961          * The timeout code also calls sili_port_reinit() if
1962          * the only commands remaining after a timeout are all
1963          * now expired commands.
1964          *
1965          * Otherwise just reissue.
1966          */
1967         if (ap->ap_expired && ap->ap_active == ap->ap_expired) {
1968                 if (finished)
1969                         sili_port_reinit(ap);
1970         } else {
1971                 sili_issue_pending_commands(ap, NULL);
1972         }
1973
1974         /*
1975          * Cleanup.  Will not be set if non-blocking.
1976          */
1977         switch(need) {
1978         case NEED_HOTPLUG_INSERT:
1979                 /*
1980                  * A hot-plug insertion event has occured and all
1981                  * outstanding commands have already been revoked.
1982                  *
1983                  * Don't recurse if this occurs while we are
1984                  * resetting the port.
1985                  *
1986                  * Place the port in a continuous COMRESET state
1987                  * until the INIT code gets to it.
1988                  */
1989                 kprintf("%s: HOTPLUG - Device inserted\n",
1990                         PORTNAME(ap));
1991                 ap->ap_probe = ATA_PROBE_NEED_INIT;
1992                 sili_cam_changed(ap, NULL, -1);
1993                 break;
1994         case NEED_HOTPLUG_REMOVE:
1995                 /*
1996                  * A hot-plug removal event has occured and all
1997                  * outstanding commands have already been revoked.
1998                  *
1999                  * Don't recurse if this occurs while we are
2000                  * resetting the port.
2001                  */
2002                 kprintf("%s: HOTPLUG - Device removed\n",
2003                         PORTNAME(ap));
2004                 sili_port_hardstop(ap);
2005                 /* ap_probe set to failed */
2006                 sili_cam_changed(ap, NULL, -1);
2007                 break;
2008         default:
2009                 break;
2010         }
2011 }
2012
2013 struct sili_ccb *
2014 sili_get_ccb(struct sili_port *ap)
2015 {
2016         struct sili_ccb                 *ccb;
2017
2018         lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2019         ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2020         if (ccb != NULL) {
2021                 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2022                 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2023                 ccb->ccb_xa.state = ATA_S_SETUP;
2024                 ccb->ccb_xa.at = NULL;
2025         }
2026         lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2027
2028         return (ccb);
2029 }
2030
2031 void
2032 sili_put_ccb(struct sili_ccb *ccb)
2033 {
2034         struct sili_port                *ap = ccb->ccb_port;
2035
2036         ccb->ccb_xa.state = ATA_S_PUT;
2037         lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2038         TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2039         lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2040 }
2041
2042 struct sili_ccb *
2043 sili_get_err_ccb(struct sili_port *ap)
2044 {
2045         struct sili_ccb *err_ccb;
2046
2047         KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2048         ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2049
2050         /*
2051          * Grab a CCB to use for error recovery.  This should never fail, as
2052          * we ask atascsi to reserve one for us at init time.
2053          */
2054         err_ccb = ap->ap_err_ccb;
2055         KKASSERT(err_ccb != NULL);
2056         err_ccb->ccb_xa.flags = 0;
2057         err_ccb->ccb_done = sili_empty_done;
2058
2059         return err_ccb;
2060 }
2061
2062 void
2063 sili_put_err_ccb(struct sili_ccb *ccb)
2064 {
2065         struct sili_port *ap = ccb->ccb_port;
2066
2067         KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2068
2069         KKASSERT(ccb == ap->ap_err_ccb);
2070
2071         ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2072 }
2073
2074 /*
2075  * Read log page to get NCQ error.
2076  *
2077  * Return 0 on success
2078  */
2079 void
2080 sili_port_read_ncq_error(struct sili_port *ap, int target)
2081 {
2082         struct sili_ccb         *ccb;
2083         struct ata_fis_h2d      *fis;
2084         int                     status;
2085
2086         DPRINTF(SILI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2087
2088         /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2089         ccb = sili_get_err_ccb(ap);
2090         ccb->ccb_done = sili_empty_done;
2091         ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2092         ccb->ccb_xa.data = ap->ap_err_scratch;
2093         ccb->ccb_xa.datalen = 512;
2094         ccb->ccb_xa.complete = sili_dummy_done;
2095         ccb->ccb_xa.at = &ap->ap_ata[target];
2096         fis = &ccb->ccb_prb->prb_h2d;
2097         bzero(fis, sizeof(*fis));
2098
2099         fis->type = ATA_FIS_TYPE_H2D;
2100         fis->flags = ATA_H2D_FLAGS_CMD | target;
2101         fis->command = ATA_C_READ_LOG_EXT;
2102         fis->lba_low = 0x10;            /* queued error log page (10h) */
2103         fis->sector_count = 1;          /* number of sectors (1) */
2104         fis->sector_count_exp = 0;
2105         fis->lba_mid = 0;               /* starting offset */
2106         fis->lba_mid_exp = 0;
2107         fis->device = 0;
2108
2109         /*
2110          * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
2111          */
2112         if (sili_load_prb(ccb) != 0) {
2113                 status = ATA_S_ERROR;
2114         } else {
2115                 ccb->ccb_xa.state = ATA_S_PENDING;
2116                 status = sili_poll(ccb, 1000, sili_quick_timeout);
2117         }
2118
2119         /*
2120          * Just spew if it fails, there isn't much we can do at this point.
2121          */
2122         if (status != ATA_S_COMPLETE) {
2123                 kprintf("%s: log page read failed, slot %d was still active.\n",
2124                         ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot);
2125         }
2126
2127         /* Done with the error CCB now. */
2128         sili_unload_prb(ccb);
2129         sili_put_err_ccb(ccb);
2130
2131         /* Extract failed register set and tags from the scratch space. */
2132         if (status == ATA_S_COMPLETE) {
2133                 struct ata_log_page_10h         *log;
2134                 int                             err_slot;
2135
2136                 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2137                 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2138                         /*
2139                          * Not queued bit was set - wasn't an NCQ error?
2140                          *
2141                          * XXX This bit seems to be set a lot even for NCQ
2142                          *     errors?
2143                          */
2144                 } else {
2145                         /*
2146                          * Copy back the log record as a D2H register FIS.
2147                          */
2148                         err_slot = log->err_regs.type &
2149                                    ATA_LOG_10H_TYPE_TAG_MASK;
2150                         ccb = &ap->ap_ccbs[err_slot];
2151                         if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2152                                 kprintf("%s: read NCQ error page slot=%d\n",
2153                                         ATANAME(ap, ccb->ccb_xa.at), err_slot
2154                                 );
2155                                 memcpy(&ccb->ccb_prb->prb_d2h, &log->err_regs,
2156                                         sizeof(struct ata_fis_d2h));
2157                                 ccb->ccb_prb->prb_d2h.type = ATA_FIS_TYPE_D2H;
2158                                 ccb->ccb_prb->prb_d2h.flags = 0;
2159                                 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
2160                                         ccb->ccb_xa.state = ATA_S_ERROR;
2161                         } else {
2162                                 kprintf("%s: read NCQ error page slot=%d, "
2163                                         "slot does not match any cmds\n",
2164                                         ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2165                                         err_slot
2166                                 );
2167                         }
2168                 }
2169         }
2170 }
2171
2172 /*
2173  * Allocate memory for various structures DMAd by hardware.  The maximum
2174  * number of segments for these tags is 1 so the DMA memory will have a
2175  * single physical base address.
2176  */
2177 struct sili_dmamem *
2178 sili_dmamem_alloc(struct sili_softc *sc, bus_dma_tag_t tag)
2179 {
2180         struct sili_dmamem *adm;
2181         int     error;
2182
2183         adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2184
2185         error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2186                                  BUS_DMA_ZERO, &adm->adm_map);
2187         if (error == 0) {
2188                 adm->adm_tag = tag;
2189                 error = bus_dmamap_load(tag, adm->adm_map,
2190                                         adm->adm_kva,
2191                                         bus_dma_tag_getmaxsize(tag),
2192                                         sili_dmamem_saveseg, &adm->adm_busaddr,
2193                                         0);
2194         }
2195         if (error) {
2196                 if (adm->adm_map) {
2197                         bus_dmamap_destroy(tag, adm->adm_map);
2198                         adm->adm_map = NULL;
2199                         adm->adm_tag = NULL;
2200                         adm->adm_kva = NULL;
2201                 }
2202                 kfree(adm, M_DEVBUF);
2203                 adm = NULL;
2204         }
2205         return (adm);
2206 }
2207
2208 static
2209 void
2210 sili_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2211 {
2212         KKASSERT(error == 0);
2213         KKASSERT(nsegs == 1);
2214         *(bus_addr_t *)info = segs->ds_addr;
2215 }
2216
2217
2218 void
2219 sili_dmamem_free(struct sili_softc *sc, struct sili_dmamem *adm)
2220 {
2221         if (adm->adm_map) {
2222                 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2223                 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2224                 adm->adm_map = NULL;
2225                 adm->adm_tag = NULL;
2226                 adm->adm_kva = NULL;
2227         }
2228         kfree(adm, M_DEVBUF);
2229 }
2230
2231 u_int32_t
2232 sili_read(struct sili_softc *sc, bus_size_t r)
2233 {
2234         bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2235                           BUS_SPACE_BARRIER_READ);
2236         return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2237 }
2238
2239 void
2240 sili_write(struct sili_softc *sc, bus_size_t r, u_int32_t v)
2241 {
2242         bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2243         bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2244                           BUS_SPACE_BARRIER_WRITE);
2245 }
2246
2247 u_int32_t
2248 sili_pread(struct sili_port *ap, bus_size_t r)
2249 {
2250         bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2251                           BUS_SPACE_BARRIER_READ);
2252         return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2253 }
2254
2255 void
2256 sili_pwrite(struct sili_port *ap, bus_size_t r, u_int32_t v)
2257 {
2258         bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2259         bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2260                           BUS_SPACE_BARRIER_WRITE);
2261 }
2262
2263 /*
2264  * Wait up to (timeout) milliseconds for the masked port register to
2265  * match the target.
2266  *
2267  * Timeout is in milliseconds.
2268  */
2269 int
2270 sili_pwait_eq(struct sili_port *ap, int timeout,
2271               bus_size_t r, u_int32_t mask, u_int32_t target)
2272 {
2273         int     t;
2274
2275         /*
2276          * Loop hard up to 100uS
2277          */
2278         for (t = 0; t < 100; ++t) {
2279                 if ((sili_pread(ap, r) & mask) == target)
2280                         return (0);
2281                 sili_os_hardsleep(1);   /* us */
2282         }
2283
2284         do {
2285                 timeout -= sili_os_softsleep();
2286                 if ((sili_pread(ap, r) & mask) == target)
2287                         return (0);
2288         } while (timeout > 0);
2289         return (1);
2290 }
2291
2292 int
2293 sili_wait_ne(struct sili_softc *sc, bus_size_t r, u_int32_t mask,
2294              u_int32_t target)
2295 {
2296         int     t;
2297
2298         /*
2299          * Loop hard up to 100uS
2300          */
2301         for (t = 0; t < 100; ++t) {
2302                 if ((sili_read(sc, r) & mask) != target)
2303                         return (0);
2304                 sili_os_hardsleep(1);   /* us */
2305         }
2306
2307         /*
2308          * And one millisecond the slow way
2309          */
2310         t = 1000;
2311         do {
2312                 t -= sili_os_softsleep();
2313                 if ((sili_read(sc, r) & mask) != target)
2314                         return (0);
2315         } while (t > 0);
2316
2317         return (1);
2318 }
2319
2320
2321 /*
2322  * Acquire an ata transfer.
2323  *
2324  * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2325  * targets that go through the port multiplier.
2326  */
2327 struct ata_xfer *
2328 sili_ata_get_xfer(struct sili_port *ap, struct ata_port *at)
2329 {
2330         struct sili_ccb         *ccb;
2331
2332         ccb = sili_get_ccb(ap);
2333         if (ccb == NULL) {
2334                 DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer: NULL ccb\n",
2335                     PORTNAME(ap));
2336                 return (NULL);
2337         }
2338
2339         DPRINTF(SILI_D_XFER, "%s: sili_ata_get_xfer got slot %d\n",
2340             PORTNAME(ap), ccb->ccb_slot);
2341
2342         bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
2343         ccb->ccb_xa.at = at;
2344         ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2345
2346         return (&ccb->ccb_xa);
2347 }
2348
2349 void
2350 sili_ata_put_xfer(struct ata_xfer *xa)
2351 {
2352         struct sili_ccb                 *ccb = (struct sili_ccb *)xa;
2353
2354         DPRINTF(SILI_D_XFER, "sili_ata_put_xfer slot %d\n", ccb->ccb_slot);
2355
2356         sili_put_ccb(ccb);
2357 }
2358
2359 int
2360 sili_ata_cmd(struct ata_xfer *xa)
2361 {
2362         struct sili_ccb                 *ccb = (struct sili_ccb *)xa;
2363
2364         KKASSERT(xa->state == ATA_S_SETUP);
2365
2366         if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2367                 goto failcmd;
2368 #if 0
2369         kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2370                 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2371                 sili_pread(ccb->ccb_port, SILI_PREG_CMD), SILI_PFMT_CMD,
2372                 ccb->ccb_slot,
2373                 ccb->ccb_xa.at,
2374                 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2375 #endif
2376
2377         ccb->ccb_done = sili_ata_cmd_done;
2378
2379         if (sili_load_prb(ccb) != 0)
2380                 goto failcmd;
2381
2382         xa->state = ATA_S_PENDING;
2383
2384         if (xa->flags & ATA_F_POLL)
2385                 return (sili_poll(ccb, xa->timeout, sili_ata_cmd_timeout));
2386
2387         crit_enter();
2388         KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
2389         xa->flags |= ATA_F_TIMEOUT_DESIRED;
2390         sili_start(ccb);
2391         crit_exit();
2392         return (xa->state);
2393
2394 failcmd:
2395         crit_enter();
2396         xa->state = ATA_S_ERROR;
2397         xa->complete(xa);
2398         crit_exit();
2399         return (ATA_S_ERROR);
2400 }
2401
2402 static void
2403 sili_ata_cmd_done(struct sili_ccb *ccb)
2404 {
2405         struct ata_xfer                 *xa = &ccb->ccb_xa;
2406
2407         /*
2408          * NOTE: callout does not lock port and may race us modifying
2409          * the flags, so make sure its stopped.
2410          */
2411         if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2412                 callout_stop(&ccb->ccb_timeout);
2413                 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2414         }
2415         xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
2416
2417         KKASSERT(xa->state != ATA_S_ONCHIP);
2418         sili_unload_prb(ccb);
2419
2420         if (xa->state != ATA_S_TIMEOUT)
2421                 xa->complete(xa);
2422 }
2423
2424 /*
2425  * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
2426  * while the callout is runing.
2427  *
2428  * We can't safely get the port lock here or delay, we could block
2429  * the callout thread.
2430  */
2431 static void
2432 sili_ata_cmd_timeout_unserialized(void *arg)
2433 {
2434         struct sili_ccb         *ccb = arg;
2435         struct sili_port        *ap = ccb->ccb_port;
2436
2437         ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
2438         ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
2439         sili_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
2440 }
2441
2442 void
2443 sili_ata_cmd_timeout(struct sili_ccb *ccb)
2444 {
2445         sili_core_timeout(ccb, 0);
2446 }
2447
2448 /*
2449  * Timeout code, typically called when the port command processor is running.
2450  *
2451  * Returns 0 if all timeout processing completed, non-zero if it is still
2452  * in progress.
2453  */
2454 static
2455 int
2456 sili_core_timeout(struct sili_ccb *ccb, int really_error)
2457 {
2458         struct ata_xfer         *xa = &ccb->ccb_xa;
2459         struct sili_port        *ap = ccb->ccb_port;
2460         struct ata_port         *at;
2461
2462         at = ccb->ccb_xa.at;
2463
2464         kprintf("%s: CMD %s state=%d slot=%d\n"
2465                 "\t active=%08x\n"
2466                 "\texpired=%08x\n"
2467                 "\thactive=%08x\n",
2468                 ATANAME(ap, at),
2469                 (really_error ? "ERROR" : "TIMEOUT"),
2470                 ccb->ccb_xa.state, ccb->ccb_slot,
2471                 ap->ap_active,
2472                 ap->ap_expired,
2473                 sili_pread(ap, SILI_PREG_SLOTST)
2474         );
2475
2476         /*
2477          * NOTE: Timeout will not be running if the command was polled.
2478          *       If we got here at least one of these flags should be set.
2479          *
2480          *       However, it might be running if we are called from the
2481          *       interrupt error handling code.
2482          */
2483         KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2484                               ATA_F_TIMEOUT_RUNNING));
2485         if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2486                 callout_stop(&ccb->ccb_timeout);
2487                 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2488         }
2489         xa->flags &= ~ATA_F_TIMEOUT_EXPIRED;
2490
2491         if (ccb->ccb_xa.state == ATA_S_PENDING) {
2492                 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2493                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2494                 ccb->ccb_done(ccb);
2495                 xa->complete(xa);
2496                 sili_issue_pending_commands(ap, NULL);
2497                 return(1);
2498         }
2499         if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2500                 kprintf("%s: Unexpected state during timeout: %d\n",
2501                         ATANAME(ap, at), ccb->ccb_xa.state);
2502                 return(1);
2503         }
2504
2505         /*
2506          * We can't process timeouts while other commands are running.
2507          */
2508         ap->ap_expired |= 1 << ccb->ccb_slot;
2509
2510         if (ap->ap_active != ap->ap_expired) {
2511                 kprintf("%s: Deferred timeout until its safe, slot %d\n",
2512                         ATANAME(ap, at), ccb->ccb_slot);
2513                 return(1);
2514         }
2515
2516         /*
2517          * We have to issue a Port reinit.  We don't read an error log
2518          * page for timeouts.  Reiniting the port will clear all pending
2519          * commands.
2520          */
2521         sili_port_reinit(ap);
2522         return(0);
2523 }
2524
2525 /*
2526  * Used by the softreset, pm_port_probe, and read_ncq_error only, in very
2527  * specialized, controlled circumstances.
2528  */
2529 void
2530 sili_quick_timeout(struct sili_ccb *ccb)
2531 {
2532         struct sili_port *ap = ccb->ccb_port;
2533
2534         switch (ccb->ccb_xa.state) {
2535         case ATA_S_PENDING:
2536                 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2537                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2538                 break;
2539         case ATA_S_ONCHIP:
2540                 KKASSERT((ap->ap_active & ~ap->ap_expired) ==
2541                          (1 << ccb->ccb_slot));
2542                 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2543                 ap->ap_active &= ~(1 << ccb->ccb_slot);
2544                 KKASSERT(ap->ap_active_cnt > 0);
2545                 --ap->ap_active_cnt;
2546                 sili_port_reinit(ap);
2547                 break;
2548         default:
2549                 panic("%s: sili_quick_timeout: ccb in bad state %d",
2550                       ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
2551         }
2552 }
2553
2554 static void
2555 sili_dummy_done(struct ata_xfer *xa)
2556 {
2557 }
2558
2559 static void
2560 sili_empty_done(struct sili_ccb *ccb)
2561 {
2562 }