The cam_sim structure was being deallocated unconditionally by device
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx.c
1 /*
2  * Core routines and tables shareable across OS platforms.
3  *
4  * Copyright (c) 1994-2002 Justin T. Gibbs.
5  * Copyright (c) 2000-2003 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    substantially similar to the "NO WARRANTY" disclaimer below
16  *    ("Disclaimer") and any redistribution must be conditioned upon
17  *    including a substantially similar Disclaimer requirement for further
18  *    binary redistribution.
19  * 3. Neither the names of the above-listed copyright holders nor the names
20  *    of any contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * Alternatively, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") version 2 as published by the Free
25  * Software Foundation.
26  *
27  * NO WARRANTY
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGES.
39  *
40  * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#198 $
41  *
42  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.c,v 1.3.2.5 2003/06/10 03:26:07 gibbs Exp $
43  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx.c,v 1.5 2004/03/15 01:10:42 dillon Exp $
44  */
45
46 #include "aic79xx_osm.h"
47 #include "aic79xx_inline.h"
48 #include "aicasm/aicasm_insformat.h"
49
50 /******************************** Globals *************************************/
51 struct ahd_softc_tailq ahd_tailq = TAILQ_HEAD_INITIALIZER(ahd_tailq);
52
53 /***************************** Lookup Tables **********************************/
54 char *ahd_chip_names[] =
55 {
56         "NONE",
57         "aic7901",
58         "aic7902",
59         "aic7901A"
60 };
61 static const u_int num_chip_names = NUM_ELEMENTS(ahd_chip_names);
62
63 /*
64  * Hardware error codes.
65  */
66 struct ahd_hard_error_entry {
67         uint8_t errno;
68         char *errmesg;
69 };
70
71 static struct ahd_hard_error_entry ahd_hard_errors[] = {
72         { DSCTMOUT,     "Discard Timer has timed out" },
73         { ILLOPCODE,    "Illegal Opcode in sequencer program" },
74         { SQPARERR,     "Sequencer Parity Error" },
75         { DPARERR,      "Data-path Parity Error" },
76         { MPARERR,      "Scratch or SCB Memory Parity Error" },
77         { CIOPARERR,    "CIOBUS Parity Error" },
78 };
79 static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
80
81 static struct ahd_phase_table_entry ahd_phase_table[] =
82 {
83         { P_DATAOUT,    MSG_NOOP,               "in Data-out phase"     },
84         { P_DATAIN,     MSG_INITIATOR_DET_ERR,  "in Data-in phase"      },
85         { P_DATAOUT_DT, MSG_NOOP,               "in DT Data-out phase"  },
86         { P_DATAIN_DT,  MSG_INITIATOR_DET_ERR,  "in DT Data-in phase"   },
87         { P_COMMAND,    MSG_NOOP,               "in Command phase"      },
88         { P_MESGOUT,    MSG_NOOP,               "in Message-out phase"  },
89         { P_STATUS,     MSG_INITIATOR_DET_ERR,  "in Status phase"       },
90         { P_MESGIN,     MSG_PARITY_ERROR,       "in Message-in phase"   },
91         { P_BUSFREE,    MSG_NOOP,               "while idle"            },
92         { 0,            MSG_NOOP,               "in unknown phase"      }
93 };
94
95 /*
96  * In most cases we only wish to itterate over real phases, so
97  * exclude the last element from the count.
98  */
99 static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
100
101 /* Our Sequencer Program */
102 #include "aic79xx_seq.h"
103
104 /**************************** Function Declarations ***************************/
105 static void             ahd_handle_transmission_error(struct ahd_softc *ahd);
106 static void             ahd_handle_lqiphase_error(struct ahd_softc *ahd,
107                                                   u_int lqistat1);
108 static int              ahd_handle_pkt_busfree(struct ahd_softc *ahd,
109                                                u_int busfreetime);
110 static int              ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
111 static void             ahd_handle_proto_violation(struct ahd_softc *ahd);
112 static void             ahd_force_renegotiation(struct ahd_softc *ahd,
113                                                 struct ahd_devinfo *devinfo);
114
115 static struct ahd_tmode_tstate*
116                         ahd_alloc_tstate(struct ahd_softc *ahd,
117                                          u_int scsi_id, char channel);
118 #ifdef AHD_TARGET_MODE
119 static void             ahd_free_tstate(struct ahd_softc *ahd,
120                                         u_int scsi_id, char channel, int force);
121 #endif
122 static void             ahd_devlimited_syncrate(struct ahd_softc *ahd,
123                                                 struct ahd_initiator_tinfo *,
124                                                 u_int *period,
125                                                 u_int *ppr_options,
126                                                 role_t role);
127 static void             ahd_update_neg_table(struct ahd_softc *ahd,
128                                              struct ahd_devinfo *devinfo,
129                                              struct ahd_transinfo *tinfo);
130 static void             ahd_update_pending_scbs(struct ahd_softc *ahd);
131 static void             ahd_fetch_devinfo(struct ahd_softc *ahd,
132                                           struct ahd_devinfo *devinfo);
133 static void             ahd_scb_devinfo(struct ahd_softc *ahd,
134                                         struct ahd_devinfo *devinfo,
135                                         struct scb *scb);
136 static void             ahd_setup_initiator_msgout(struct ahd_softc *ahd,
137                                                    struct ahd_devinfo *devinfo,
138                                                    struct scb *scb);
139 static void             ahd_build_transfer_msg(struct ahd_softc *ahd,
140                                                struct ahd_devinfo *devinfo);
141 static void             ahd_construct_sdtr(struct ahd_softc *ahd,
142                                            struct ahd_devinfo *devinfo,
143                                            u_int period, u_int offset);
144 static void             ahd_construct_wdtr(struct ahd_softc *ahd,
145                                            struct ahd_devinfo *devinfo,
146                                            u_int bus_width);
147 static void             ahd_construct_ppr(struct ahd_softc *ahd,
148                                           struct ahd_devinfo *devinfo,
149                                           u_int period, u_int offset,
150                                           u_int bus_width, u_int ppr_options);
151 static void             ahd_clear_msg_state(struct ahd_softc *ahd);
152 static void             ahd_handle_message_phase(struct ahd_softc *ahd);
153 typedef enum {
154         AHDMSG_1B,
155         AHDMSG_2B,
156         AHDMSG_EXT
157 } ahd_msgtype;
158 static int              ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
159                                      u_int msgval, int full);
160 static int              ahd_parse_msg(struct ahd_softc *ahd,
161                                       struct ahd_devinfo *devinfo);
162 static int              ahd_handle_msg_reject(struct ahd_softc *ahd,
163                                               struct ahd_devinfo *devinfo);
164 static void             ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
165                                                 struct ahd_devinfo *devinfo);
166 static void             ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
167 static void             ahd_handle_devreset(struct ahd_softc *ahd,
168                                             struct ahd_devinfo *devinfo,
169                                             u_int lun, cam_status status,
170                                             char *message, int verbose_level);
171 #if AHD_TARGET_MODE
172 static void             ahd_setup_target_msgin(struct ahd_softc *ahd,
173                                                struct ahd_devinfo *devinfo,
174                                                struct scb *scb);
175 #endif
176
177 static u_int            ahd_sglist_size(struct ahd_softc *ahd);
178 static u_int            ahd_sglist_allocsize(struct ahd_softc *ahd);
179 static bus_dmamap_callback_t
180                         ahd_dmamap_cb; 
181 static void             ahd_initialize_hscbs(struct ahd_softc *ahd);
182 static int              ahd_init_scbdata(struct ahd_softc *ahd);
183 static void             ahd_fini_scbdata(struct ahd_softc *ahd);
184 static void             ahd_setup_iocell_workaround(struct ahd_softc *ahd);
185 static void             ahd_iocell_first_selection(struct ahd_softc *ahd);
186 static void             ahd_add_col_list(struct ahd_softc *ahd,
187                                          struct scb *scb, u_int col_idx);
188 static void             ahd_rem_col_list(struct ahd_softc *ahd,
189                                          struct scb *scb);
190 static void             ahd_chip_init(struct ahd_softc *ahd);
191 static void             ahd_qinfifo_requeue(struct ahd_softc *ahd,
192                                             struct scb *prev_scb,
193                                             struct scb *scb);
194 static int              ahd_qinfifo_count(struct ahd_softc *ahd);
195 static int              ahd_search_scb_list(struct ahd_softc *ahd, int target,
196                                             char channel, int lun, u_int tag,
197                                             role_t role, uint32_t status,
198                                             ahd_search_action action,
199                                             u_int *list_head, u_int tid);
200 static void             ahd_stitch_tid_list(struct ahd_softc *ahd,
201                                             u_int tid_prev, u_int tid_cur,
202                                             u_int tid_next);
203 static void             ahd_add_scb_to_free_list(struct ahd_softc *ahd,
204                                                  u_int scbid);
205 static u_int            ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
206                                      u_int prev, u_int next, u_int tid);
207 static void             ahd_reset_current_bus(struct ahd_softc *ahd);
208 static ahd_callback_t   ahd_reset_poll;
209 static ahd_callback_t   ahd_stat_timer;
210 #ifdef AHD_DUMP_SEQ
211 static void             ahd_dumpseq(struct ahd_softc *ahd);
212 #endif
213 static void             ahd_loadseq(struct ahd_softc *ahd);
214 static int              ahd_check_patch(struct ahd_softc *ahd,
215                                         struct patch **start_patch,
216                                         u_int start_instr, u_int *skip_addr);
217 static u_int            ahd_resolve_seqaddr(struct ahd_softc *ahd,
218                                             u_int address);
219 static void             ahd_download_instr(struct ahd_softc *ahd,
220                                            u_int instrptr, uint8_t *dconsts);
221 static int              ahd_probe_stack_size(struct ahd_softc *ahd);
222 #ifdef AHD_TARGET_MODE
223 static void             ahd_queue_lstate_event(struct ahd_softc *ahd,
224                                                struct ahd_tmode_lstate *lstate,
225                                                u_int initiator_id,
226                                                u_int event_type,
227                                                u_int event_arg);
228 static void             ahd_update_scsiid(struct ahd_softc *ahd,
229                                           u_int targid_mask);
230 static int              ahd_handle_target_cmd(struct ahd_softc *ahd,
231                                               struct target_cmd *cmd);
232 #endif
233
234 /******************************** Private Inlines *****************************/
235 static __inline void    ahd_assert_atn(struct ahd_softc *ahd);
236 static __inline int     ahd_currently_packetized(struct ahd_softc *ahd);
237 static __inline int     ahd_set_active_fifo(struct ahd_softc *ahd);
238
239 static __inline void
240 ahd_assert_atn(struct ahd_softc *ahd)
241 {
242         ahd_outb(ahd, SCSISIGO, ATNO);
243 }
244
245 /*
246  * Determine if the current connection has a packetized
247  * agreement.  This does not necessarily mean that we
248  * are currently in a packetized transfer.  We could
249  * just as easily be sending or receiving a message.
250  */
251 static __inline int
252 ahd_currently_packetized(struct ahd_softc *ahd)
253 {
254         ahd_mode_state   saved_modes;
255         int              packetized;
256
257         saved_modes = ahd_save_modes(ahd);
258         if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
259                 /*
260                  * The packetized bit refers to the last
261                  * connection, not the current one.  Check
262                  * for non-zero LQISTATE instead.
263                  */
264                 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
265                 packetized = ahd_inb(ahd, LQISTATE) != 0;
266         } else {
267                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
268                 packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
269         }
270         ahd_restore_modes(ahd, saved_modes);
271         return (packetized);
272 }
273
274 static __inline int
275 ahd_set_active_fifo(struct ahd_softc *ahd)
276 {
277         u_int active_fifo;
278
279         AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
280         active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
281         switch (active_fifo) {
282         case 0:
283         case 1:
284                 ahd_set_modes(ahd, active_fifo, active_fifo);
285                 return (1);
286         default:
287                 return (0);
288         }
289 }
290
291 /************************* Sequencer Execution Control ************************/
292 /*
293  * Restart the sequencer program from address zero
294  */
295 void
296 ahd_restart(struct ahd_softc *ahd)
297 {
298
299         ahd_pause(ahd);
300
301         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
302
303         /* No more pending messages */
304         ahd_clear_msg_state(ahd);
305         ahd_outb(ahd, SCSISIGO, 0);             /* De-assert BSY */
306         ahd_outb(ahd, MSG_OUT, MSG_NOOP);       /* No message to send */
307         ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
308         ahd_outb(ahd, SEQINTCTL, 0);
309         ahd_outb(ahd, LASTPHASE, P_BUSFREE);
310         ahd_outb(ahd, SEQ_FLAGS, 0);
311         ahd_outb(ahd, SAVED_SCSIID, 0xFF);
312         ahd_outb(ahd, SAVED_LUN, 0xFF);
313
314         /*
315          * Ensure that the sequencer's idea of TQINPOS
316          * matches our own.  The sequencer increments TQINPOS
317          * only after it sees a DMA complete and a reset could
318          * occur before the increment leaving the kernel to believe
319          * the command arrived but the sequencer to not.
320          */
321         ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
322
323         /* Always allow reselection */
324         ahd_outb(ahd, SCSISEQ1,
325                  ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
326         /* Ensure that no DMA operations are in progress */
327         ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
328         ahd_outb(ahd, SCBHCNT, 0);
329         ahd_outb(ahd, CCSCBCTL, CCSCBRESET);
330         ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
331         ahd_unpause(ahd);
332 }
333
334 void
335 ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
336 {
337         ahd_mode_state   saved_modes;
338
339 #ifdef AHD_DEBUG
340         if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
341                 printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
342 #endif
343         saved_modes = ahd_save_modes(ahd);
344         ahd_set_modes(ahd, fifo, fifo);
345         ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
346         if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
347                 ahd_outb(ahd, CCSGCTL, CCSGRESET);
348         ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
349         ahd_outb(ahd, SG_STATE, 0);
350         ahd_restore_modes(ahd, saved_modes);
351 }
352
353 /************************* Input/Output Queues ********************************/
354 /*
355  * Flush and completed commands that are sitting in the command
356  * complete queues down on the chip but have yet to be dma'ed back up.
357  */
358 void
359 ahd_flush_qoutfifo(struct ahd_softc *ahd)
360 {
361         struct          scb *scb;
362         ahd_mode_state  saved_modes;
363         u_int           saved_scbptr;
364         u_int           ccscbctl;
365         u_int           scbid;
366         u_int           next_scbid;
367
368         saved_modes = ahd_save_modes(ahd);
369         ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
370         saved_scbptr = ahd_get_scbptr(ahd);
371
372         /*
373          * Wait for any inprogress DMA to complete and clear DMA state
374          * if this if for an SCB in the qinfifo.
375          */
376         while ((ccscbctl = ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0) {
377
378                 if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
379                         if ((ccscbctl & ARRDONE) != 0)
380                                 break;
381                 } else if ((ccscbctl & CCSCBDONE) != 0)
382                         break;
383                 ahd_delay(200);
384         }
385         if ((ccscbctl & CCSCBDIR) != 0)
386                 ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
387
388         /*
389          * Complete any SCBs that just finished being
390          * DMA'ed into the qoutfifo.
391          */
392         ahd_run_qoutfifo(ahd);
393
394         /*
395          * Manually update/complete any completed SCBs that are waiting to be
396          * DMA'ed back up to the host.
397          */
398         scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
399         while (!SCBID_IS_NULL(scbid)) {
400                 uint8_t *hscb_ptr;
401                 u_int    i;
402                 
403                 ahd_set_scbptr(ahd, scbid);
404                 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
405                 scb = ahd_lookup_scb(ahd, scbid);
406                 if (scb == NULL) {
407                         printf("%s: Warning - DMA-up and complete "
408                                "SCB %d invalid\n", ahd_name(ahd), scbid);
409                         continue;
410                 }
411                 hscb_ptr = (uint8_t *)scb->hscb;
412                 for (i = 0; i < sizeof(struct hardware_scb); i++)
413                         *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
414
415                 ahd_complete_scb(ahd, scb);
416                 scbid = next_scbid;
417         }
418         ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
419
420         scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
421         while (!SCBID_IS_NULL(scbid)) {
422
423                 ahd_set_scbptr(ahd, scbid);
424                 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
425                 scb = ahd_lookup_scb(ahd, scbid);
426                 if (scb == NULL) {
427                         printf("%s: Warning - Complete SCB %d invalid\n",
428                                ahd_name(ahd), scbid);
429                         continue;
430                 }
431
432                 ahd_complete_scb(ahd, scb);
433                 scbid = next_scbid;
434         }
435         ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
436         ahd_set_scbptr(ahd, saved_scbptr);
437
438         /*
439          * Flush the good status FIFO for compelted packetized commands.
440          */
441         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
442         while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
443                 scbid = (ahd_inb(ahd, GSFIFO+1) << 8)
444                       | ahd_inb(ahd, GSFIFO);
445                 scb = ahd_lookup_scb(ahd, scbid);
446                 if (scb == NULL) {
447                         printf("%s: Warning - GSFIFO SCB %d invalid\n",
448                                ahd_name(ahd), scbid);
449                         continue;
450                 }
451                 ahd_complete_scb(ahd, scb);
452         }
453
454         /*
455          * Restore state.
456          */
457         ahd_restore_modes(ahd, saved_modes);
458         ahd->flags |= AHD_UPDATE_PEND_CMDS;
459 }
460
461 void
462 ahd_run_qoutfifo(struct ahd_softc *ahd)
463 {
464         struct scb *scb;
465         u_int  scb_index;
466
467         if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
468                 panic("ahd_run_qoutfifo recursion");
469         ahd->flags |= AHD_RUNNING_QOUTFIFO;
470         ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
471         while ((ahd->qoutfifo[ahd->qoutfifonext]
472              & QOUTFIFO_ENTRY_VALID_LE) == ahd->qoutfifonext_valid_tag) {
473
474                 scb_index = ahd_le16toh(ahd->qoutfifo[ahd->qoutfifonext]
475                                       & ~QOUTFIFO_ENTRY_VALID_LE);
476                 scb = ahd_lookup_scb(ahd, scb_index);
477                 if (scb == NULL) {
478                         printf("%s: WARNING no command for scb %d "
479                                "(cmdcmplt)\nQOUTPOS = %d\n",
480                                ahd_name(ahd), scb_index,
481                                ahd->qoutfifonext);
482                         ahd_dump_card_state(ahd);
483                 } else
484                         ahd_complete_scb(ahd, scb);
485
486                 ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
487                 if (ahd->qoutfifonext == 0)
488                         ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID_LE;
489         }
490         ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
491 }
492
493 /************************* Interrupt Handling *********************************/
494 void
495 ahd_handle_hwerrint(struct ahd_softc *ahd)
496 {
497         /*
498          * Some catastrophic hardware error has occurred.
499          * Print it for the user and disable the controller.
500          */
501         int i;
502         int error;
503
504         error = ahd_inb(ahd, ERROR);
505         for (i = 0; i < num_errors; i++) {
506                 if ((error & ahd_hard_errors[i].errno) != 0)
507                         printf("%s: hwerrint, %s\n",
508                                ahd_name(ahd), ahd_hard_errors[i].errmesg);
509         }
510
511         ahd_dump_card_state(ahd);
512         panic("BRKADRINT");
513
514         /* Tell everyone that this HBA is no longer available */
515         ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
516                        CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
517                        CAM_NO_HBA);
518
519         /* Tell the system that this controller has gone away. */
520         ahd_free(ahd);
521 }
522
523 void
524 ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
525 {
526         u_int seqintcode;
527
528         /*
529          * Save the sequencer interrupt code and clear the SEQINT
530          * bit. We will unpause the sequencer, if appropriate,
531          * after servicing the request.
532          */
533         seqintcode = ahd_inb(ahd, SEQINTCODE);
534         ahd_outb(ahd, CLRINT, CLRSEQINT);
535         if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
536                 /*
537                  * Unpause the sequencer and let it clear
538                  * SEQINT by writing NO_SEQINT to it.  This
539                  * will cause the sequencer to be paused again,
540                  * which is the expected state of this routine.
541                  */
542                 ahd_unpause(ahd);
543                 while (!ahd_is_paused(ahd))
544                         ;
545                 ahd_outb(ahd, CLRINT, CLRSEQINT);
546         }
547         ahd_update_modes(ahd);
548 #ifdef AHD_DEBUG
549         if ((ahd_debug & AHD_SHOW_MISC) != 0)
550                 printf("%s: Handle Seqint Called for code %d\n",
551                        ahd_name(ahd), seqintcode);
552 #endif
553         switch (seqintcode) {
554         case BAD_SCB_STATUS:
555         {
556                 struct  scb *scb;
557                 u_int   scbid;
558                 int     cmds_pending;
559
560                 scbid = ahd_get_scbptr(ahd);
561                 scb = ahd_lookup_scb(ahd, scbid);
562                 if (scb != NULL) {
563                         ahd_complete_scb(ahd, scb);
564                 } else {
565                         printf("%s: WARNING no command for scb %d "
566                                "(bad status)\n", ahd_name(ahd), scbid);
567                         ahd_dump_card_state(ahd);
568                 }
569                 cmds_pending = ahd_inw(ahd, CMDS_PENDING);
570                 if (cmds_pending > 0)
571                         ahd_outw(ahd, CMDS_PENDING, cmds_pending - 1);
572                 break;
573         }
574         case ENTERING_NONPACK:
575         {
576                 struct  scb *scb;
577                 u_int   scbid;
578
579                 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
580                                  ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
581                 scbid = ahd_get_scbptr(ahd);
582                 scb = ahd_lookup_scb(ahd, scbid);
583                 if (scb == NULL) {
584                         /*
585                          * Somehow need to know if this
586                          * is from a selection or reselection.
587                          * From that, we can termine target
588                          * ID so we at least have an I_T nexus.
589                          */
590                 } else {
591                         ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
592                         ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
593                         ahd_outb(ahd, SEQ_FLAGS, 0x0);
594                 }
595                 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
596                  && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
597                         /*
598                          * Phase change after read stream with
599                          * CRC error with P0 asserted on last
600                          * packet.
601                          */
602 #ifdef AHD_DEBUG
603                         if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
604                                 printf("%s: Assuming LQIPHASE_NLQ with "
605                                        "P0 assertion\n", ahd_name(ahd));
606 #endif
607                 }
608 #ifdef AHD_DEBUG
609                 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
610                         printf("%s: Entering NONPACK\n", ahd_name(ahd));
611 #endif
612                 break;
613         }
614         case INVALID_SEQINT:
615                 printf("%s: Invalid Sequencer interrupt occurred.\n",
616                        ahd_name(ahd));
617                 ahd_dump_card_state(ahd);
618                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
619                 break;
620         case STATUS_OVERRUN:
621         {
622                 struct  scb *scb;
623                 u_int   scbid;
624
625                 scbid = ahd_get_scbptr(ahd);
626                 scb = ahd_lookup_scb(ahd, scbid);
627                 if (scb != NULL)
628                         ahd_print_path(ahd, scb);
629                 else
630                         printf("%s: ", ahd_name(ahd));
631                 printf("SCB %d Packetized Status Overrun", scbid);
632                 ahd_dump_card_state(ahd);
633                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
634                 break;
635         }
636         case CFG4ISTAT_INTR:
637         {
638                 struct  scb *scb;
639                 u_int   scbid;
640
641                 scbid = ahd_get_scbptr(ahd);
642                 scb = ahd_lookup_scb(ahd, scbid);
643                 if (scb == NULL) {
644                         ahd_dump_card_state(ahd);
645                         printf("CFG4ISTAT: Free SCB %d referenced", scbid);
646                         panic("For safety");
647                 }
648                 ahd_outq(ahd, HADDR, scb->sense_busaddr);
649                 ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
650                 ahd_outb(ahd, HCNT + 2, 0);
651                 ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
652                 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
653                 break;
654         }
655         case ILLEGAL_PHASE:
656         {
657                 u_int bus_phase;
658
659                 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
660                 printf("%s: ILLEGAL_PHASE 0x%x\n",
661                        ahd_name(ahd), bus_phase);
662
663                 switch (bus_phase) {
664                 case P_DATAOUT:
665                 case P_DATAIN:
666                 case P_DATAOUT_DT:
667                 case P_DATAIN_DT:
668                 case P_MESGOUT:
669                 case P_STATUS:
670                 case P_MESGIN:
671                         ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
672                         printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
673                         break;
674                 case P_COMMAND:
675                 {
676                         struct  ahd_devinfo devinfo;
677                         struct  scb *scb;
678                         struct  ahd_initiator_tinfo *targ_info;
679                         struct  ahd_tmode_tstate *tstate;
680                         struct  ahd_transinfo *tinfo;
681                         u_int   scbid;
682
683                         /*
684                          * If a target takes us into the command phase
685                          * assume that it has been externally reset and
686                          * has thus lost our previous packetized negotiation
687                          * agreement.  Since we have not sent an identify
688                          * message and may not have fully qualified the
689                          * connection, we change our command to TUR, assert
690                          * ATN and ABORT the task when we go to message in
691                          * phase.  The OSM will see the REQUEUE_REQUEST
692                          * status and retry the command.
693                          */
694                         scbid = ahd_get_scbptr(ahd);
695                         scb = ahd_lookup_scb(ahd, scbid);
696                         if (scb == NULL) {
697                                 printf("Invalid phase with no valid SCB.  "
698                                        "Resetting bus.\n");
699                                 ahd_reset_channel(ahd, 'A',
700                                                   /*Initiate Reset*/TRUE);
701                                 break;
702                         }
703                         ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
704                                             SCB_GET_TARGET(ahd, scb),
705                                             SCB_GET_LUN(scb),
706                                             SCB_GET_CHANNEL(ahd, scb),
707                                             ROLE_INITIATOR);
708                         targ_info = ahd_fetch_transinfo(ahd,
709                                                         devinfo.channel,
710                                                         devinfo.our_scsiid,
711                                                         devinfo.target,
712                                                         &tstate);
713                         tinfo = &targ_info->curr;
714                         ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
715                                       AHD_TRANS_ACTIVE, /*paused*/TRUE);
716                         ahd_set_syncrate(ahd, &devinfo, /*period*/0,
717                                          /*offset*/0, /*ppr_options*/0,
718                                          AHD_TRANS_ACTIVE, /*paused*/TRUE);
719                         ahd_outb(ahd, SCB_CDB_STORE, 0);
720                         ahd_outb(ahd, SCB_CDB_STORE+1, 0);
721                         ahd_outb(ahd, SCB_CDB_STORE+2, 0);
722                         ahd_outb(ahd, SCB_CDB_STORE+3, 0);
723                         ahd_outb(ahd, SCB_CDB_STORE+4, 0);
724                         ahd_outb(ahd, SCB_CDB_STORE+5, 0);
725                         ahd_outb(ahd, SCB_CDB_LEN, 6);
726                         scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
727                         scb->hscb->control |= MK_MESSAGE;
728                         ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
729                         ahd_outb(ahd, MSG_OUT, HOST_MSG);
730                         ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
731                         /*
732                          * The lun is 0, regardless of the SCB's lun
733                          * as we have not sent an identify message.
734                          */
735                         ahd_outb(ahd, SAVED_LUN, 0);
736                         ahd_outb(ahd, SEQ_FLAGS, 0);
737                         ahd_assert_atn(ahd);
738                         scb->flags &= ~(SCB_PACKETIZED);
739                         scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
740                         ahd_freeze_devq(ahd, scb);
741                         ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
742                         ahd_freeze_scb(scb);
743
744                         /*
745                          * Allow the sequencer to continue with
746                          * non-pack processing.
747                          */
748                         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
749                         ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
750                         if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
751                                 ahd_outb(ahd, CLRLQOINT1, 0);
752                         }
753 #ifdef AHD_DEBUG
754                         if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
755                                 ahd_print_path(ahd, scb);
756                                 printf("Unexpected command phase from "
757                                        "packetized target\n");
758                         }
759 #endif
760                         break;
761                 }
762                 }
763                 break;
764         }
765         case CFG4OVERRUN:
766         {
767                 struct  scb *scb;
768                 u_int   scb_index;
769                 
770 #ifdef AHD_DEBUG
771                 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
772                         printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
773                                ahd_inb(ahd, MODE_PTR));
774                 }
775 #endif
776                 scb_index = ahd_get_scbptr(ahd);
777                 scb = ahd_lookup_scb(ahd, scb_index);
778                 if (scb == NULL) {
779                         /*
780                          * Attempt to transfer to an SCB that is
781                          * not outstanding.
782                          */
783                         ahd_assert_atn(ahd);
784                         ahd_outb(ahd, MSG_OUT, HOST_MSG);
785                         ahd->msgout_buf[0] = MSG_ABORT_TASK;
786                         ahd->msgout_len = 1;
787                         ahd->msgout_index = 0;
788                         ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
789                         /*
790                          * Clear status received flag to prevent any
791                          * attempt to complete this bogus SCB.
792                          */
793                         ahd_outb(ahd, SCB_CONTROL,
794                                  ahd_inb(ahd, SCB_CONTROL) & ~STATUS_RCVD);
795                 }
796                 break;
797         }
798         case DUMP_CARD_STATE:
799         {
800                 ahd_dump_card_state(ahd);
801                 break;
802         }
803         case PDATA_REINIT:
804         {
805 #ifdef AHD_DEBUG
806                 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
807                         printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
808                                "SG_CACHE_SHADOW = 0x%x\n",
809                                ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
810                                ahd_inb(ahd, SG_CACHE_SHADOW));
811                 }
812 #endif
813                 ahd_reinitialize_dataptrs(ahd);
814                 break;
815         }
816         case HOST_MSG_LOOP:
817         {
818                 struct ahd_devinfo devinfo;
819
820                 /*
821                  * The sequencer has encountered a message phase
822                  * that requires host assistance for completion.
823                  * While handling the message phase(s), we will be
824                  * notified by the sequencer after each byte is
825                  * transfered so we can track bus phase changes.
826                  *
827                  * If this is the first time we've seen a HOST_MSG_LOOP
828                  * interrupt, initialize the state of the host message
829                  * loop.
830                  */
831                 ahd_fetch_devinfo(ahd, &devinfo);
832                 if (ahd->msg_type == MSG_TYPE_NONE) {
833                         struct scb *scb;
834                         u_int scb_index;
835                         u_int bus_phase;
836
837                         bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
838                         if (bus_phase != P_MESGIN
839                          && bus_phase != P_MESGOUT) {
840                                 printf("ahd_intr: HOST_MSG_LOOP bad "
841                                        "phase 0x%x\n", bus_phase);
842                                 /*
843                                  * Probably transitioned to bus free before
844                                  * we got here.  Just punt the message.
845                                  */
846                                 ahd_dump_card_state(ahd);
847                                 ahd_clear_intstat(ahd);
848                                 ahd_restart(ahd);
849                                 return;
850                         }
851
852                         scb_index = ahd_get_scbptr(ahd);
853                         scb = ahd_lookup_scb(ahd, scb_index);
854                         if (devinfo.role == ROLE_INITIATOR) {
855                                 if (bus_phase == P_MESGOUT)
856                                         ahd_setup_initiator_msgout(ahd,
857                                                                    &devinfo,
858                                                                    scb);
859                                 else {
860                                         ahd->msg_type =
861                                             MSG_TYPE_INITIATOR_MSGIN;
862                                         ahd->msgin_index = 0;
863                                 }
864                         }
865 #if AHD_TARGET_MODE
866                         else {
867                                 if (bus_phase == P_MESGOUT) {
868                                         ahd->msg_type =
869                                             MSG_TYPE_TARGET_MSGOUT;
870                                         ahd->msgin_index = 0;
871                                 }
872                                 else 
873                                         ahd_setup_target_msgin(ahd,
874                                                                &devinfo,
875                                                                scb);
876                         }
877 #endif
878                 }
879
880                 ahd_handle_message_phase(ahd);
881                 break;
882         }
883         case NO_MATCH:
884         {
885                 /* Ensure we don't leave the selection hardware on */
886                 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
887                 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
888
889                 printf("%s:%c:%d: no active SCB for reconnecting "
890                        "target - issuing BUS DEVICE RESET\n",
891                        ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
892                 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
893                        "REG0 == 0x%x ACCUM = 0x%x\n",
894                        ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
895                        ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
896                 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
897                        "SINDEX == 0x%x\n",
898                        ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
899                        ahd_find_busy_tcl(ahd,
900                                          BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
901                                                    ahd_inb(ahd, SAVED_LUN))),
902                        ahd_inw(ahd, SINDEX));
903                 printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
904                        "SCB_CONTROL == 0x%x\n",
905                        ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
906                        ahd_inb_scbram(ahd, SCB_LUN),
907                        ahd_inb_scbram(ahd, SCB_CONTROL));
908                 printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
909                        ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
910                 printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
911                 printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
912                 ahd_dump_card_state(ahd);
913                 ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
914                 ahd->msgout_len = 1;
915                 ahd->msgout_index = 0;
916                 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
917                 ahd_outb(ahd, MSG_OUT, HOST_MSG);
918                 ahd_assert_atn(ahd);
919                 break;
920         }
921         case PROTO_VIOLATION:
922         {
923                 ahd_handle_proto_violation(ahd);
924                 break;
925         }
926         case IGN_WIDE_RES:
927         {
928                 struct ahd_devinfo devinfo;
929
930                 ahd_fetch_devinfo(ahd, &devinfo);
931                 ahd_handle_ign_wide_residue(ahd, &devinfo);
932                 break;
933         }
934         case BAD_PHASE:
935         {
936                 u_int lastphase;
937
938                 lastphase = ahd_inb(ahd, LASTPHASE);
939                 printf("%s:%c:%d: unknown scsi bus phase %x, "
940                        "lastphase = 0x%x.  Attempting to continue\n",
941                        ahd_name(ahd), 'A',
942                        SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
943                        lastphase, ahd_inb(ahd, SCSISIGI));
944                 break;
945         }
946         case MISSED_BUSFREE:
947         {
948                 u_int lastphase;
949
950                 lastphase = ahd_inb(ahd, LASTPHASE);
951                 printf("%s:%c:%d: Missed busfree. "
952                        "Lastphase = 0x%x, Curphase = 0x%x\n",
953                        ahd_name(ahd), 'A',
954                        SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
955                        lastphase, ahd_inb(ahd, SCSISIGI));
956                 ahd_restart(ahd);
957                 return;
958         }
959         case DATA_OVERRUN:
960         {
961                 /*
962                  * When the sequencer detects an overrun, it
963                  * places the controller in "BITBUCKET" mode
964                  * and allows the target to complete its transfer.
965                  * Unfortunately, none of the counters get updated
966                  * when the controller is in this mode, so we have
967                  * no way of knowing how large the overrun was.
968                  */
969                 struct  scb *scb;
970                 u_int   scbindex;
971 #ifdef AHD_DEBUG
972                 u_int   lastphase;
973 #endif
974
975                 scbindex = ahd_get_scbptr(ahd);
976                 scb = ahd_lookup_scb(ahd, scbindex);
977 #ifdef AHD_DEBUG
978                 lastphase = ahd_inb(ahd, LASTPHASE);
979                 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
980                         ahd_print_path(ahd, scb);
981                         printf("data overrun detected %s.  Tag == 0x%x.\n",
982                                ahd_lookup_phase_entry(lastphase)->phasemsg,
983                                SCB_GET_TAG(scb));
984                         ahd_print_path(ahd, scb);
985                         printf("%s seen Data Phase.  Length = %ld.  "
986                                "NumSGs = %d.\n",
987                                ahd_inb(ahd, SEQ_FLAGS) & DPHASE
988                                ? "Have" : "Haven't",
989                                ahd_get_transfer_length(scb), scb->sg_count);
990                         ahd_dump_sglist(scb);
991                 }
992 #endif
993
994                 /*
995                  * Set this and it will take effect when the
996                  * target does a command complete.
997                  */
998                 ahd_freeze_devq(ahd, scb);
999                 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1000                 ahd_freeze_scb(scb);
1001                 break;
1002         }
1003         case MKMSG_FAILED:
1004         {
1005                 struct ahd_devinfo devinfo;
1006                 struct scb *scb;
1007                 u_int scbid;
1008
1009                 ahd_fetch_devinfo(ahd, &devinfo);
1010                 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
1011                        ahd_name(ahd), devinfo.channel, devinfo.target,
1012                        devinfo.lun);
1013                 scbid = ahd_get_scbptr(ahd);
1014                 scb = ahd_lookup_scb(ahd, scbid);
1015                 if (scb != NULL
1016                  && (scb->flags & SCB_RECOVERY_SCB) != 0)
1017                         /*
1018                          * Ensure that we didn't put a second instance of this
1019                          * SCB into the QINFIFO.
1020                          */
1021                         ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1022                                            SCB_GET_CHANNEL(ahd, scb),
1023                                            SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1024                                            ROLE_INITIATOR, /*status*/0,
1025                                            SEARCH_REMOVE);
1026                 ahd_outb(ahd, SCB_CONTROL,
1027                          ahd_inb(ahd, SCB_CONTROL) & ~MK_MESSAGE);
1028                 break;
1029         }
1030         case TASKMGMT_FUNC_COMPLETE:
1031         {
1032                 u_int   scbid;
1033                 struct  scb *scb;
1034
1035                 scbid = ahd_get_scbptr(ahd);
1036                 scb = ahd_lookup_scb(ahd, scbid);
1037                 if (scb != NULL) {
1038                         u_int      lun;
1039                         u_int      tag;
1040                         cam_status error;
1041
1042                         ahd_print_path(ahd, scb);
1043                         printf("Task Management Func 0x%x Complete\n",
1044                                scb->hscb->task_management);
1045                         lun = CAM_LUN_WILDCARD;
1046                         tag = SCB_LIST_NULL;
1047
1048                         switch (scb->hscb->task_management) {
1049                         case SIU_TASKMGMT_ABORT_TASK:
1050                                 tag = SCB_GET_TAG(scb);
1051                         case SIU_TASKMGMT_ABORT_TASK_SET:
1052                         case SIU_TASKMGMT_CLEAR_TASK_SET:
1053                                 lun = scb->hscb->lun;
1054                                 error = CAM_REQ_ABORTED;
1055                                 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1056                                                'A', lun, tag, ROLE_INITIATOR,
1057                                                error);
1058                                 break;
1059                         case SIU_TASKMGMT_LUN_RESET:
1060                                 lun = scb->hscb->lun;
1061                         case SIU_TASKMGMT_TARGET_RESET:
1062                         {
1063                                 struct ahd_devinfo devinfo;
1064
1065                                 ahd_scb_devinfo(ahd, &devinfo, scb);
1066                                 error = CAM_BDR_SENT;
1067                                 ahd_handle_devreset(ahd, &devinfo, lun,
1068                                                     CAM_BDR_SENT,
1069                                                     lun != CAM_LUN_WILDCARD
1070                                                     ? "Lun Reset"
1071                                                     : "Target Reset",
1072                                                     /*verbose_level*/0);
1073                                 break;
1074                         }
1075                         default:
1076                                 panic("Unexpected TaskMgmt Func\n");
1077                                 break;
1078                         }
1079                 }
1080                 break;
1081         }
1082         case TASKMGMT_CMD_CMPLT_OKAY:
1083         {
1084                 u_int   scbid;
1085                 struct  scb *scb;
1086
1087                 /*
1088                  * An ABORT TASK TMF failed to be delivered before
1089                  * the targeted command completed normally.
1090                  */
1091                 scbid = ahd_get_scbptr(ahd);
1092                 scb = ahd_lookup_scb(ahd, scbid);
1093                 if (scb != NULL) {
1094                         /*
1095                          * Remove the second instance of this SCB from
1096                          * the QINFIFO if it is still there.
1097                          */
1098                         ahd_print_path(ahd, scb);
1099                         printf("SCB completes before TMF\n");
1100                         /*
1101                          * Handle losing the race.  Wait until any
1102                          * current selection completes.  We will then
1103                          * set the TMF back to zero in this SCB so that
1104                          * the sequencer doesn't bother to issue another
1105                          * sequencer interrupt for its completion.
1106                          */
1107                         while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
1108                             && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
1109                             && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
1110                                 ;
1111                         ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
1112                         ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1113                                            SCB_GET_CHANNEL(ahd, scb),  
1114                                            SCB_GET_LUN(scb), SCB_GET_TAG(scb), 
1115                                            ROLE_INITIATOR, /*status*/0,   
1116                                            SEARCH_REMOVE);
1117                 }
1118                 break;
1119         }
1120         case TRACEPOINT0:
1121         case TRACEPOINT1:
1122         case TRACEPOINT2:
1123         case TRACEPOINT3:
1124                 printf("%s: Tracepoint %d\n", ahd_name(ahd),
1125                        seqintcode - TRACEPOINT0);
1126                 break;
1127         case NO_SEQINT:
1128                 break;
1129         case SAW_HWERR:
1130                 ahd_handle_hwerrint(ahd);
1131                 break;
1132         default:
1133                 printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
1134                        seqintcode);
1135                 break;
1136         }
1137         /*
1138          *  The sequencer is paused immediately on
1139          *  a SEQINT, so we should restart it when
1140          *  we're done.
1141          */
1142         ahd_unpause(ahd);
1143 }
1144
1145 void
1146 ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
1147 {
1148         struct scb      *scb;
1149         u_int            status0;
1150         u_int            status3;
1151         u_int            status;
1152         u_int            lqistat1;
1153         u_int            lqostat0;
1154         u_int            scbid;
1155         u_int            busfreetime;
1156
1157         ahd_update_modes(ahd);
1158         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1159
1160         status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
1161         status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
1162         status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
1163         lqistat1 = ahd_inb(ahd, LQISTAT1);
1164         lqostat0 = ahd_inb(ahd, LQOSTAT0);
1165         busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1166         if ((status0 & (SELDI|SELDO)) != 0) {
1167                 u_int simode0;
1168
1169                 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1170                 simode0 = ahd_inb(ahd, SIMODE0);
1171                 status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
1172                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1173         }
1174         scbid = ahd_get_scbptr(ahd);
1175         scb = ahd_lookup_scb(ahd, scbid);
1176         if (scb != NULL
1177          && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1178                 scb = NULL;
1179
1180         /* Make sure the sequencer is in a safe location. */
1181         ahd_clear_critical_section(ahd);
1182
1183         if ((status0 & IOERR) != 0) {
1184                 u_int now_lvd;
1185
1186                 now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
1187                 printf("%s: Transceiver State Has Changed to %s mode\n",
1188                        ahd_name(ahd), now_lvd ? "LVD" : "SE");
1189                 ahd_outb(ahd, CLRSINT0, CLRIOERR);
1190                 /*
1191                  * A change in I/O mode is equivalent to a bus reset.
1192                  */
1193                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1194                 ahd_pause(ahd);
1195                 ahd_setup_iocell_workaround(ahd);
1196                 ahd_unpause(ahd);
1197         } else if ((status0 & OVERRUN) != 0) {
1198                 printf("%s: SCSI offset overrun detected.  Resetting bus.\n",
1199                        ahd_name(ahd));
1200                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1201         } else if ((status & SCSIRSTI) != 0) {
1202                 printf("%s: Someone reset channel A\n", ahd_name(ahd));
1203                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
1204         } else if ((status & SCSIPERR) != 0) {
1205                 ahd_handle_transmission_error(ahd);
1206         } else if (lqostat0 != 0) {
1207                 printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
1208                 ahd_outb(ahd, CLRLQOINT0, lqostat0);
1209                 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
1210                         ahd_outb(ahd, CLRLQOINT1, 0);
1211                 }
1212         } else if ((status & SELTO) != 0) {
1213                 u_int  scbid;
1214
1215                 /* Stop the selection */
1216                 ahd_outb(ahd, SCSISEQ0, 0);
1217
1218                 /* No more pending messages */
1219                 ahd_clear_msg_state(ahd);
1220
1221                 /* Clear interrupt state */
1222                 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1223
1224                 /*
1225                  * Although the driver does not care about the
1226                  * 'Selection in Progress' status bit, the busy
1227                  * LED does.  SELINGO is only cleared by a sucessfull
1228                  * selection, so we must manually clear it to insure
1229                  * the LED turns off just incase no future successful
1230                  * selections occur (e.g. no devices on the bus).
1231                  */
1232                 ahd_outb(ahd, CLRSINT0, CLRSELINGO);
1233
1234                 scbid = ahd_inw(ahd, WAITING_TID_HEAD);
1235                 scb = ahd_lookup_scb(ahd, scbid);
1236                 if (scb == NULL) {
1237                         printf("%s: ahd_intr - referenced scb not "
1238                                "valid during SELTO scb(0x%x)\n",
1239                                ahd_name(ahd), scbid);
1240                         ahd_dump_card_state(ahd);
1241                 } else {
1242                         struct ahd_devinfo devinfo;
1243 #ifdef AHD_DEBUG
1244                         if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
1245                                 ahd_print_path(ahd, scb);
1246                                 printf("Saw Selection Timeout for SCB 0x%x\n",
1247                                        scbid);
1248                         }
1249 #endif
1250                         /*
1251                          * Force a renegotiation with this target just in
1252                          * case the cable was pulled and will later be
1253                          * re-attached.  The target may forget its negotiation
1254                          * settings with us should it attempt to reselect
1255                          * during the interruption.  The target will not issue
1256                          * a unit attention in this case, so we must always
1257                          * renegotiate.
1258                          */
1259                         ahd_scb_devinfo(ahd, &devinfo, scb);
1260                         ahd_force_renegotiation(ahd, &devinfo);
1261                         ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1262                         ahd_freeze_devq(ahd, scb);
1263                 }
1264                 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1265                 ahd_iocell_first_selection(ahd);
1266                 ahd_unpause(ahd);
1267         } else if ((status0 & (SELDI|SELDO)) != 0) {
1268                 ahd_iocell_first_selection(ahd);
1269                 ahd_unpause(ahd);
1270         } else if (status3 != 0) {
1271                 printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
1272                        ahd_name(ahd), status3);
1273                 ahd_outb(ahd, CLRSINT3, status3);
1274         } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
1275                 ahd_handle_lqiphase_error(ahd, lqistat1);
1276         } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1277                 /*
1278                  * This status can be delayed during some
1279                  * streaming operations.  The SCSIPHASE
1280                  * handler has already dealt with this case
1281                  * so just clear the error.
1282                  */
1283                 ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
1284         } else if ((status & BUSFREE) != 0) {
1285                 u_int lqostat1;
1286                 int   restart;
1287                 int   clear_fifo;
1288                 int   packetized;
1289                 u_int mode;
1290
1291                 /*
1292                  * Clear our selection hardware as soon as possible.
1293                  * We may have an entry in the waiting Q for this target,
1294                  * that is affected by this busfree and we don't want to
1295                  * go about selecting the target while we handle the event.
1296                  */
1297                 ahd_outb(ahd, SCSISEQ0, 0);
1298
1299                 /*
1300                  * Determine what we were up to at the time of
1301                  * the busfree.
1302                  */
1303                 mode = AHD_MODE_SCSI;
1304                 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1305                 lqostat1 = ahd_inb(ahd, LQOSTAT1);
1306                 switch (busfreetime) {
1307                 case BUSFREE_DFF0:
1308                 case BUSFREE_DFF1:
1309                 {
1310                         u_int   scbid;
1311                         struct  scb *scb;
1312
1313                         mode = busfreetime == BUSFREE_DFF0
1314                              ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
1315                         ahd_set_modes(ahd, mode, mode);
1316                         scbid = ahd_get_scbptr(ahd);
1317                         scb = ahd_lookup_scb(ahd, scbid);
1318                         if (scb == NULL) {
1319                                 printf("%s: Invalid SCB %d in DFF%d "
1320                                        "during unexpected busfree\n",
1321                                        ahd_name(ahd), scbid, mode);
1322                                 packetized = 0;
1323                         } else
1324                                 packetized = (scb->flags & SCB_PACKETIZED) != 0;
1325                         clear_fifo = 1;
1326                         break;
1327                 }
1328                 case BUSFREE_LQO:
1329                         clear_fifo = 0;
1330                         packetized = 1;
1331                         break;
1332                 default:
1333                         clear_fifo = 0;
1334                         packetized =  (lqostat1 & LQOBUSFREE) != 0;
1335                         if (!packetized
1336                          && ahd_inb(ahd, LASTPHASE) == P_BUSFREE)
1337                                 packetized = 1;
1338                         break;
1339                 }
1340
1341 #ifdef AHD_DEBUG
1342                 if ((ahd_debug & AHD_SHOW_MISC) != 0)
1343                         printf("Saw Busfree.  Busfreetime = 0x%x.\n",
1344                                busfreetime);
1345 #endif
1346                 /*
1347                  * Busfrees that occur in non-packetized phases are
1348                  * handled by the nonpkt_busfree handler.
1349                  */
1350                 if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
1351                         restart = ahd_handle_pkt_busfree(ahd, busfreetime);
1352                 } else {
1353                         packetized = 0;
1354                         restart = ahd_handle_nonpkt_busfree(ahd);
1355                 }
1356                 /*
1357                  * Clear the busfree interrupt status.  The setting of
1358                  * the interrupt is a pulse, so in a perfect world, we
1359                  * would not need to muck with the ENBUSFREE logic.  This
1360                  * would ensure that if the bus moves on to another
1361                  * connection, busfree protection is still in force.  If
1362                  * BUSFREEREV is broken, however, we must manually clear
1363                  * the ENBUSFREE if the busfree occurred during a non-pack
1364                  * connection so that we don't get false positives during
1365                  * future, packetized, connections.
1366                  */
1367                 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
1368                 if (packetized == 0
1369                  && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
1370                         ahd_outb(ahd, SIMODE1,
1371                                  ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
1372
1373                 if (clear_fifo)
1374                         ahd_clear_fifo(ahd, mode);
1375
1376                 ahd_clear_msg_state(ahd);
1377                 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1378                 if (restart) {
1379                         ahd_restart(ahd);
1380                 } else {
1381                         ahd_unpause(ahd);
1382                 }
1383         } else {
1384                 printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
1385                        ahd_name(ahd), status);
1386                 ahd_dump_card_state(ahd);
1387                 ahd_clear_intstat(ahd);
1388                 ahd_unpause(ahd);
1389         }
1390 }
1391
1392 static void
1393 ahd_handle_transmission_error(struct ahd_softc *ahd)
1394 {
1395         struct  scb *scb;
1396         u_int   scbid;
1397         u_int   lqistat1;
1398         u_int   lqistat2;
1399         u_int   msg_out;
1400         u_int   curphase;
1401         u_int   lastphase;
1402         u_int   perrdiag;
1403         u_int   cur_col;
1404         int     silent;
1405
1406         scb = NULL;
1407         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1408         lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
1409         lqistat2 = ahd_inb(ahd, LQISTAT2);
1410         if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
1411          && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
1412                 u_int lqistate;
1413
1414                 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1415                 lqistate = ahd_inb(ahd, LQISTATE);
1416                 if ((lqistate >= 0x1E && lqistate <= 0x24)
1417                  || (lqistate == 0x29)) {
1418 #ifdef AHD_DEBUG
1419                         if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1420                                 printf("%s: NLQCRC found via LQISTATE\n",
1421                                        ahd_name(ahd));
1422                         }
1423 #endif
1424                         lqistat1 |= LQICRCI_NLQ;
1425                 }
1426                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1427         }
1428
1429         ahd_outb(ahd, CLRLQIINT1, lqistat1);
1430         lastphase = ahd_inb(ahd, LASTPHASE);
1431         curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1432         perrdiag = ahd_inb(ahd, PERRDIAG);
1433         msg_out = MSG_INITIATOR_DET_ERR;
1434         ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
1435         
1436         /*
1437          * Try to find the SCB associated with this error.
1438          */
1439         silent = FALSE;
1440         if (lqistat1 == 0
1441          || (lqistat1 & LQICRCI_NLQ) != 0) {
1442                 if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
1443                         ahd_set_active_fifo(ahd);
1444                 scbid = ahd_get_scbptr(ahd);
1445                 scb = ahd_lookup_scb(ahd, scbid);
1446                 if (scb != NULL && SCB_IS_SILENT(scb))
1447                         silent = TRUE;
1448         }
1449
1450         cur_col = 0;
1451         if (silent == FALSE) {
1452                 printf("%s: Transmission error detected\n", ahd_name(ahd));
1453                 ahd_lqistat1_print(lqistat1, &cur_col, 50);
1454                 ahd_lastphase_print(lastphase, &cur_col, 50);
1455                 ahd_scsisigi_print(curphase, &cur_col, 50);
1456                 ahd_perrdiag_print(perrdiag, &cur_col, 50);
1457                 printf("\n");
1458                 ahd_dump_card_state(ahd);
1459         }
1460
1461         if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
1462                 if (silent == FALSE) {
1463                         printf("%s: Gross protocol error during incoming "
1464                                "packet.  lqistat1 == 0x%x.  Resetting bus.\n",
1465                                ahd_name(ahd), lqistat1);
1466                 }
1467                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1468                 return;
1469         } else if ((lqistat1 & LQICRCI_LQ) != 0) {
1470                 /*
1471                  * A CRC error has been detected on an incoming LQ.
1472                  * The bus is currently hung on the last ACK.
1473                  * Hit LQIRETRY to release the last ack, and
1474                  * wait for the sequencer to determine that ATNO
1475                  * is asserted while in message out to take us
1476                  * to our host message loop.  No NONPACKREQ or
1477                  * LQIPHASE type errors will occur in this
1478                  * scenario.  After this first LQIRETRY, the LQI
1479                  * manager will be in ISELO where it will
1480                  * happily sit until another packet phase begins.
1481                  * Unexpected bus free detection is enabled
1482                  * through any phases that occur after we release
1483                  * this last ack until the LQI manager sees a
1484                  * packet phase.  This implies we may have to
1485                  * ignore a perfectly valid "unexected busfree"
1486                  * after our "initiator detected error" message is
1487                  * sent.  A busfree is the expected response after
1488                  * we tell the target that it's L_Q was corrupted.
1489                  * (SPI4R09 10.7.3.3.3)
1490                  */
1491                 ahd_outb(ahd, LQCTL2, LQIRETRY);
1492                 printf("LQIRetry for LQICRCI_LQ to release ACK\n");
1493         } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1494                 /*
1495                  * We detected a CRC error in a NON-LQ packet.
1496                  * The hardware has varying behavior in this situation
1497                  * depending on whether this packet was part of a
1498                  * stream or not.
1499                  *
1500                  * PKT by PKT mode:
1501                  * The hardware has already acked the complete packet.
1502                  * If the target honors our outstanding ATN condition,
1503                  * we should be (or soon will be) in MSGOUT phase.
1504                  * This will trigger the LQIPHASE_LQ status bit as the
1505                  * hardware was expecting another LQ.  Unexpected
1506                  * busfree detection is enabled.  Once LQIPHASE_LQ is
1507                  * true (first entry into host message loop is much
1508                  * the same), we must clear LQIPHASE_LQ and hit
1509                  * LQIRETRY so the hardware is ready to handle
1510                  * a future LQ.  NONPACKREQ will not be asserted again
1511                  * once we hit LQIRETRY until another packet is
1512                  * processed.  The target may either go busfree
1513                  * or start another packet in response to our message.
1514                  *
1515                  * Read Streaming P0 asserted:
1516                  * If we raise ATN and the target completes the entire
1517                  * stream (P0 asserted during the last packet), the
1518                  * hardware will ack all data and return to the ISTART
1519                  * state.  When the target reponds to our ATN condition,
1520                  * LQIPHASE_LQ will be asserted.  We should respond to
1521                  * this with an LQIRETRY to prepare for any future
1522                  * packets.  NONPACKREQ will not be asserted again
1523                  * once we hit LQIRETRY until another packet is
1524                  * processed.  The target may either go busfree or
1525                  * start another packet in response to our message.
1526                  * Busfree detection is enabled.
1527                  *
1528                  * Read Streaming P0 not asserted:
1529                  * If we raise ATN and the target transitions to
1530                  * MSGOUT in or after a packet where P0 is not
1531                  * asserted, the hardware will assert LQIPHASE_NLQ.
1532                  * We should respond to the LQIPHASE_NLQ with an
1533                  * LQIRETRY.  Should the target stay in a non-pkt
1534                  * phase after we send our message, the hardware
1535                  * will assert LQIPHASE_LQ.  Recovery is then just as
1536                  * listed above for the read streaming with P0 asserted.
1537                  * Busfree detection is enabled.
1538                  */
1539                 if (silent == FALSE)
1540                         printf("LQICRC_NLQ\n");
1541                 if (scb == NULL) {
1542                         printf("%s: No SCB valid for LQICRC_NLQ.  "
1543                                "Resetting bus\n", ahd_name(ahd));
1544                         ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1545                         return;
1546                 }
1547         } else if ((lqistat1 & LQIBADLQI) != 0) {
1548                 printf("Need to handle BADLQI!\n");
1549                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1550                 return;
1551         } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
1552                 if ((curphase & ~P_DATAIN_DT) != 0) {
1553                         /* Ack the byte.  So we can continue. */
1554                         if (silent == FALSE)
1555                                 printf("Acking %s to clear perror\n",
1556                                     ahd_lookup_phase_entry(curphase)->phasemsg);
1557                         ahd_inb(ahd, SCSIDAT);
1558                 }
1559         
1560                 if (curphase == P_MESGIN)
1561                         msg_out = MSG_PARITY_ERROR;
1562         }
1563
1564         /*
1565          * We've set the hardware to assert ATN if we 
1566          * get a parity error on "in" phases, so all we
1567          * need to do is stuff the message buffer with
1568          * the appropriate message.  "In" phases have set
1569          * mesg_out to something other than MSG_NOP.
1570          */
1571         ahd->send_msg_perror = msg_out;
1572         if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
1573                 scb->flags |= SCB_TRANSMISSION_ERROR;
1574         ahd_outb(ahd, MSG_OUT, HOST_MSG);
1575         ahd_outb(ahd, CLRINT, CLRSCSIINT);
1576         ahd_unpause(ahd);
1577 }
1578
1579 static void
1580 ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
1581 {
1582         /*
1583          * Clear the sources of the interrupts.
1584          */
1585         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1586         ahd_outb(ahd, CLRLQIINT1, lqistat1);
1587
1588         /*
1589          * If the "illegal" phase changes were in response
1590          * to our ATN to flag a CRC error, AND we ended up
1591          * on packet boundaries, clear the error, restart the
1592          * LQI manager as appropriate, and go on our merry
1593          * way toward sending the message.  Otherwise, reset
1594          * the bus to clear the error.
1595          */
1596         ahd_set_active_fifo(ahd);
1597         if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
1598          && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
1599                 if ((lqistat1 & LQIPHASE_LQ) != 0) {
1600                         printf("LQIRETRY for LQIPHASE_LQ\n");
1601                         ahd_outb(ahd, LQCTL2, LQIRETRY);
1602                 } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
1603                         printf("LQIRETRY for LQIPHASE_NLQ\n");
1604                         ahd_outb(ahd, LQCTL2, LQIRETRY);
1605                 } else
1606                         panic("ahd_handle_lqiphase_error: No phase errors\n");
1607                 ahd_dump_card_state(ahd);
1608                 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1609                 ahd_unpause(ahd);
1610         } else {
1611                 printf("Reseting Channel for LQI Phase error\n");
1612                 ahd_dump_card_state(ahd);
1613                 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1614         }
1615 }
1616
1617 /*
1618  * Packetized unexpected or expected busfree.
1619  * Entered in mode based on busfreetime.
1620  */
1621 static int
1622 ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
1623 {
1624         u_int lqostat1;
1625
1626         AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
1627                          ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
1628         lqostat1 = ahd_inb(ahd, LQOSTAT1);
1629         if ((lqostat1 & LQOBUSFREE) != 0) {
1630                 struct scb *scb;
1631                 u_int scbid;
1632                 u_int saved_scbptr;
1633                 u_int waiting_h;
1634                 u_int waiting_t;
1635                 u_int next;
1636
1637                 if ((busfreetime & BUSFREE_LQO) == 0)
1638                         printf("%s: Warning, BUSFREE time is 0x%x.  "
1639                                "Expected BUSFREE_LQO.\n",
1640                                ahd_name(ahd), busfreetime);
1641                 /*
1642                  * The LQO manager detected an unexpected busfree
1643                  * either:
1644                  *
1645                  * 1) During an outgoing LQ.
1646                  * 2) After an outgoing LQ but before the first
1647                  *    REQ of the command packet.
1648                  * 3) During an outgoing command packet.
1649                  *
1650                  * In all cases, CURRSCB is pointing to the
1651                  * SCB that encountered the failure.  Clean
1652                  * up the queue, clear SELDO and LQOBUSFREE,
1653                  * and allow the sequencer to restart the select
1654                  * out at its lesure.
1655                  */
1656                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1657                 scbid = ahd_inw(ahd, CURRSCB);
1658                 scb = ahd_lookup_scb(ahd, scbid);
1659                 if (scb == NULL)
1660                        panic("SCB not valid during LQOBUSFREE");
1661                 /*
1662                  * Clear the status.
1663                  */
1664                 ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
1665                 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
1666                         ahd_outb(ahd, CLRLQOINT1, 0);
1667                 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
1668                 ahd_flush_device_writes(ahd);
1669                 ahd_outb(ahd, CLRSINT0, CLRSELDO);
1670
1671                 /*
1672                  * Return the LQO manager to its idle loop.  It will
1673                  * not do this automatically if the busfree occurs
1674                  * after the first REQ of either the LQ or command
1675                  * packet or between the LQ and command packet.
1676                  */
1677                 ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
1678
1679                 /*
1680                  * Update the waiting for selection queue so
1681                  * we restart on the correct SCB.
1682                  */
1683                 waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
1684                 saved_scbptr = ahd_get_scbptr(ahd);
1685                 if (waiting_h != scbid) {
1686
1687                         ahd_outw(ahd, WAITING_TID_HEAD, scbid);
1688                         waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
1689                         if (waiting_t == waiting_h) {
1690                                 ahd_outw(ahd, WAITING_TID_TAIL, scbid);
1691                                 next = SCB_LIST_NULL;
1692                         } else {
1693                                 ahd_set_scbptr(ahd, waiting_h);
1694                                 next = ahd_inw_scbram(ahd, SCB_NEXT2);
1695                         }
1696                         ahd_set_scbptr(ahd, scbid);
1697                         ahd_outw(ahd, SCB_NEXT2, next);
1698                 }
1699                 ahd_set_scbptr(ahd, saved_scbptr);
1700                 if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
1701                         if (SCB_IS_SILENT(scb) == FALSE) {
1702                                 ahd_print_path(ahd, scb);
1703                                 printf("Probable outgoing LQ CRC error.  "
1704                                        "Retrying command\n");
1705                         }
1706                         scb->crc_retry_count++;
1707                 } else {
1708                         ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
1709                         ahd_freeze_scb(scb);
1710                         ahd_freeze_devq(ahd, scb);
1711                 }
1712                 /* Return unpausing the sequencer. */
1713                 return (0);
1714         } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
1715                 /*
1716                  * Ignore what are really parity errors that
1717                  * occur on the last REQ of a free running
1718                  * clock prior to going busfree.  Some drives
1719                  * do not properly active negate just before
1720                  * going busfree resulting in a parity glitch.
1721                  */
1722                 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
1723 #ifdef AHD_DEBUG
1724                 if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
1725                         printf("%s: Parity on last REQ detected "
1726                                "during busfree phase.\n",
1727                                ahd_name(ahd));
1728 #endif
1729                 /* Return unpausing the sequencer. */
1730                 return (0);
1731         }
1732         if (ahd->src_mode != AHD_MODE_SCSI) {
1733                 u_int   scbid;
1734                 struct  scb *scb;
1735
1736                 scbid = ahd_get_scbptr(ahd);
1737                 scb = ahd_lookup_scb(ahd, scbid);
1738                 ahd_print_path(ahd, scb);
1739                 printf("Unexpected PKT busfree condition\n");
1740                 ahd_dump_card_state(ahd);
1741                 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
1742                                SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1743                                ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
1744
1745                 /* Return restarting the sequencer. */
1746                 return (1);
1747         }
1748         printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
1749         ahd_dump_card_state(ahd);
1750         /* Restart the sequencer. */
1751         return (1);
1752 }
1753
1754 /*
1755  * Non-packetized unexpected or expected busfree.
1756  */
1757 static int
1758 ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
1759 {
1760         struct  ahd_devinfo devinfo;
1761         struct  scb *scb;
1762         u_int   lastphase;
1763         u_int   saved_scsiid;
1764         u_int   saved_lun;
1765         u_int   target;
1766         u_int   initiator_role_id;
1767         u_int   scbid;
1768         u_int   ppr_busfree;
1769         int     printerror;
1770
1771         /*
1772          * Look at what phase we were last in.  If its message out,
1773          * chances are pretty good that the busfree was in response
1774          * to one of our abort requests.
1775          */
1776         lastphase = ahd_inb(ahd, LASTPHASE);
1777         saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
1778         saved_lun = ahd_inb(ahd, SAVED_LUN);
1779         target = SCSIID_TARGET(ahd, saved_scsiid);
1780         initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
1781         ahd_compile_devinfo(&devinfo, initiator_role_id,
1782                             target, saved_lun, 'A', ROLE_INITIATOR);
1783         printerror = 1;
1784
1785         scbid = ahd_get_scbptr(ahd);
1786         scb = ahd_lookup_scb(ahd, scbid);
1787         if (scb != NULL
1788          && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1789                 scb = NULL;
1790
1791         ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
1792         if (lastphase == P_MESGOUT) {
1793                 u_int tag;
1794
1795                 tag = SCB_LIST_NULL;
1796                 if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
1797                  || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
1798                         int found;
1799                         int sent_msg;
1800
1801                         if (scb == NULL) {
1802                                 ahd_print_devinfo(ahd, &devinfo);
1803                                 printf("Abort for unidentified "
1804                                        "connection completed.\n");
1805                                 /* restart the sequencer. */
1806                                 return (1);
1807                         }
1808                         sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
1809                         ahd_print_path(ahd, scb);
1810                         printf("SCB %d - Abort%s Completed.\n",
1811                                SCB_GET_TAG(scb),
1812                                sent_msg == MSG_ABORT_TAG ? "" : " Tag");
1813
1814                         if (sent_msg == MSG_ABORT_TAG)
1815                                 tag = SCB_GET_TAG(scb);
1816
1817                         if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
1818                                 /*
1819                                  * This abort is in response to an
1820                                  * unexpected switch to command phase
1821                                  * for a packetized connection.  Since
1822                                  * the identify message was never sent,
1823                                  * "saved lun" is 0.  We really want to
1824                                  * abort only the SCB that encountered
1825                                  * this error, which could have a different
1826                                  * lun.  The SCB will be retried so the OS
1827                                  * will see the UA after renegotiating to
1828                                  * packetized.
1829                                  */
1830                                 tag = SCB_GET_TAG(scb);
1831                                 saved_lun = scb->hscb->lun;
1832                         }
1833                         found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
1834                                                tag, ROLE_INITIATOR,
1835                                                CAM_REQ_ABORTED);
1836                         printf("found == 0x%x\n", found);
1837                         printerror = 0;
1838                 } else if (ahd_sent_msg(ahd, AHDMSG_1B,
1839                                         MSG_BUS_DEV_RESET, TRUE)) {
1840 #if defined(__DragonFly__) || defined(__FreeBSD__)
1841                         /*
1842                          * Don't mark the user's request for this BDR
1843                          * as completing with CAM_BDR_SENT.  CAM3
1844                          * specifies CAM_REQ_CMP.
1845                          */
1846                         if (scb != NULL
1847                          && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
1848                          && ahd_match_scb(ahd, scb, target, 'A',
1849                                           CAM_LUN_WILDCARD, SCB_LIST_NULL,
1850                                           ROLE_INITIATOR))
1851                                 ahd_set_transaction_status(scb, CAM_REQ_CMP);
1852 #endif
1853                         ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
1854                                             CAM_BDR_SENT, "Bus Device Reset",
1855                                             /*verbose_level*/0);
1856                         printerror = 0;
1857                 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
1858                         && ppr_busfree == 0) {
1859                         struct ahd_initiator_tinfo *tinfo;
1860                         struct ahd_tmode_tstate *tstate;
1861
1862                         /*
1863                          * PPR Rejected.  Try non-ppr negotiation
1864                          * and retry command.
1865                          */
1866 #ifdef AHD_DEBUG
1867                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1868                                 printf("PPR negotiation rejected busfree.\n");
1869 #endif
1870                         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
1871                                                     devinfo.our_scsiid,
1872                                                     devinfo.target, &tstate);
1873                         tinfo->curr.transport_version = 2;
1874                         tinfo->goal.transport_version = 2;
1875                         tinfo->goal.ppr_options = 0;
1876                         ahd_qinfifo_requeue_tail(ahd, scb);
1877                         printerror = 0;
1878                 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
1879                         && ppr_busfree == 0) {
1880                         /*
1881                          * Negotiation Rejected.  Go-narrow and
1882                          * retry command.
1883                          */
1884 #ifdef AHD_DEBUG
1885                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1886                                 printf("WDTR negotiation rejected busfree.\n");
1887 #endif
1888                         ahd_set_width(ahd, &devinfo,
1889                                       MSG_EXT_WDTR_BUS_8_BIT,
1890                                       AHD_TRANS_CUR|AHD_TRANS_GOAL,
1891                                       /*paused*/TRUE);
1892                         ahd_qinfifo_requeue_tail(ahd, scb);
1893                         printerror = 0;
1894                 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
1895                         && ppr_busfree == 0) {
1896                         /*
1897                          * Negotiation Rejected.  Go-async and
1898                          * retry command.
1899                          */
1900 #ifdef AHD_DEBUG
1901                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1902                                 printf("SDTR negotiation rejected busfree.\n");
1903 #endif
1904                         ahd_set_syncrate(ahd, &devinfo,
1905                                         /*period*/0, /*offset*/0,
1906                                         /*ppr_options*/0,
1907                                         AHD_TRANS_CUR|AHD_TRANS_GOAL,
1908                                         /*paused*/TRUE);
1909                         ahd_qinfifo_requeue_tail(ahd, scb);
1910                         printerror = 0;
1911                 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
1912                         && ahd_sent_msg(ahd, AHDMSG_1B,
1913                                          MSG_INITIATOR_DET_ERR, TRUE)) {
1914
1915 #ifdef AHD_DEBUG
1916                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1917                                 printf("Expected IDE Busfree\n");
1918 #endif
1919                         printerror = 0;
1920                 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
1921                         && ahd_sent_msg(ahd, AHDMSG_1B,
1922                                         MSG_MESSAGE_REJECT, TRUE)) {
1923
1924 #ifdef AHD_DEBUG
1925                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1926                                 printf("Expected QAS Reject Busfree\n");
1927 #endif
1928                         printerror = 0;
1929                 }
1930         }
1931
1932         /*
1933          * The busfree required flag is honored at the end of
1934          * the message phases.  We check it last in case we
1935          * had to send some other message that caused a busfree.
1936          */
1937         if (printerror != 0
1938          && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
1939          && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
1940
1941                 ahd_freeze_devq(ahd, scb);
1942                 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1943                 ahd_freeze_scb(scb);
1944                 if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
1945                         ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1946                                        SCB_GET_CHANNEL(ahd, scb),
1947                                        SCB_GET_LUN(scb), SCB_LIST_NULL,
1948                                        ROLE_INITIATOR, CAM_REQ_ABORTED);
1949                 } else {
1950 #ifdef AHD_DEBUG
1951                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1952                                 printf("PPR Negotiation Busfree.\n");
1953 #endif
1954                         ahd_done(ahd, scb);
1955                 }
1956                 printerror = 0;
1957         }
1958         if (printerror != 0) {
1959                 int aborted;
1960
1961                 aborted = 0;
1962                 if (scb != NULL) {
1963                         u_int tag;
1964
1965                         if ((scb->hscb->control & TAG_ENB) != 0)
1966                                 tag = SCB_GET_TAG(scb);
1967                         else
1968                                 tag = SCB_LIST_NULL;
1969                         ahd_print_path(ahd, scb);
1970                         aborted = ahd_abort_scbs(ahd, target, 'A',
1971                                        SCB_GET_LUN(scb), tag,
1972                                        ROLE_INITIATOR,
1973                                        CAM_UNEXP_BUSFREE);
1974                 } else {
1975                         /*
1976                          * We had not fully identified this connection,
1977                          * so we cannot abort anything.
1978                          */
1979                         printf("%s: ", ahd_name(ahd));
1980                 }
1981                 if (lastphase != P_BUSFREE)
1982                         ahd_force_renegotiation(ahd, &devinfo);
1983                 printf("Unexpected busfree %s, %d SCBs aborted, "
1984                        "PRGMCNT == 0x%x\n",
1985                        ahd_lookup_phase_entry(lastphase)->phasemsg,
1986                        aborted,
1987                        ahd_inb(ahd, PRGMCNT)
1988                         | (ahd_inb(ahd, PRGMCNT+1) << 8));
1989                 ahd_dump_card_state(ahd);
1990         }
1991         /* Always restart the sequencer. */
1992         return (1);
1993 }
1994
1995 static void
1996 ahd_handle_proto_violation(struct ahd_softc *ahd)
1997 {
1998         struct  ahd_devinfo devinfo;
1999         struct  scb *scb;
2000         u_int   scbid;
2001         u_int   seq_flags;
2002         u_int   curphase;
2003         u_int   lastphase;
2004         int     found;
2005
2006         ahd_fetch_devinfo(ahd, &devinfo);
2007         scbid = ahd_get_scbptr(ahd);
2008         scb = ahd_lookup_scb(ahd, scbid);
2009         seq_flags = ahd_inb(ahd, SEQ_FLAGS);
2010         curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
2011         lastphase = ahd_inb(ahd, LASTPHASE);
2012         if ((seq_flags & NOT_IDENTIFIED) != 0) {
2013
2014                 /*
2015                  * The reconnecting target either did not send an
2016                  * identify message, or did, but we didn't find an SCB
2017                  * to match.
2018                  */
2019                 ahd_print_devinfo(ahd, &devinfo);
2020                 printf("Target did not send an IDENTIFY message. "
2021                        "LASTPHASE = 0x%x.\n", lastphase);
2022                 scb = NULL;
2023         } else if (scb == NULL) {
2024                 /*
2025                  * We don't seem to have an SCB active for this
2026                  * transaction.  Print an error and reset the bus.
2027                  */
2028                 ahd_print_devinfo(ahd, &devinfo);
2029                 printf("No SCB found during protocol violation\n");
2030                 goto proto_violation_reset;
2031         } else {
2032                 ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
2033                 if ((seq_flags & NO_CDB_SENT) != 0) {
2034                         ahd_print_path(ahd, scb);
2035                         printf("No or incomplete CDB sent to device.\n");
2036                 } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
2037                           & STATUS_RCVD) == 0) {
2038                         /*
2039                          * The target never bothered to provide status to
2040                          * us prior to completing the command.  Since we don't
2041                          * know the disposition of this command, we must attempt
2042                          * to abort it.  Assert ATN and prepare to send an abort
2043                          * message.
2044                          */
2045                         ahd_print_path(ahd, scb);
2046                         printf("Completed command without status.\n");
2047                 } else {
2048                         ahd_print_path(ahd, scb);
2049                         printf("Unknown protocol violation.\n");
2050                         ahd_dump_card_state(ahd);
2051                 }
2052         }
2053         if ((lastphase & ~P_DATAIN_DT) == 0
2054          || lastphase == P_COMMAND) {
2055 proto_violation_reset:
2056                 /*
2057                  * Target either went directly to data
2058                  * phase or didn't respond to our ATN.
2059                  * The only safe thing to do is to blow
2060                  * it away with a bus reset.
2061                  */
2062                 found = ahd_reset_channel(ahd, 'A', TRUE);
2063                 printf("%s: Issued Channel %c Bus Reset. "
2064                        "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
2065         } else {
2066                 /*
2067                  * Leave the selection hardware off in case
2068                  * this abort attempt will affect yet to
2069                  * be sent commands.
2070                  */
2071                 ahd_outb(ahd, SCSISEQ0,
2072                          ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2073                 ahd_assert_atn(ahd);
2074                 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2075                 if (scb == NULL) {
2076                         ahd_print_devinfo(ahd, &devinfo);
2077                         ahd->msgout_buf[0] = MSG_ABORT_TASK;
2078                         ahd->msgout_len = 1;
2079                         ahd->msgout_index = 0;
2080                         ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2081                 } else {
2082                         ahd_print_path(ahd, scb);
2083                         scb->flags |= SCB_ABORT;
2084                 }
2085                 printf("Protocol violation %s.  Attempting to abort.\n",
2086                        ahd_lookup_phase_entry(curphase)->phasemsg);
2087         }
2088 }
2089
2090 /*
2091  * Force renegotiation to occur the next time we initiate
2092  * a command to the current device.
2093  */
2094 static void
2095 ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
2096 {
2097         struct  ahd_initiator_tinfo *targ_info;
2098         struct  ahd_tmode_tstate *tstate;
2099
2100 #ifdef AHD_DEBUG
2101         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
2102                 ahd_print_devinfo(ahd, devinfo);
2103                 printf("Forcing renegotiation\n");
2104         }
2105 #endif
2106         targ_info = ahd_fetch_transinfo(ahd,
2107                                         devinfo->channel,
2108                                         devinfo->our_scsiid,
2109                                         devinfo->target,
2110                                         &tstate);
2111         ahd_update_neg_request(ahd, devinfo, tstate,
2112                                targ_info, AHD_NEG_IF_NON_ASYNC);
2113 }
2114
2115 #define AHD_MAX_STEPS 2000
2116 void
2117 ahd_clear_critical_section(struct ahd_softc *ahd)
2118 {
2119         ahd_mode_state  saved_modes;
2120         int             stepping;
2121         int             steps;
2122         int             first_instr;
2123         u_int           simode0;
2124         u_int           simode1;
2125         u_int           simode3;
2126         u_int           lqimode0;
2127         u_int           lqimode1;
2128         u_int           lqomode0;
2129         u_int           lqomode1;
2130
2131         if (ahd->num_critical_sections == 0)
2132                 return;
2133
2134         stepping = FALSE;
2135         steps = 0;
2136         first_instr = 0;
2137         simode0 = 0;
2138         simode1 = 0;
2139         simode3 = 0;
2140         lqimode0 = 0;
2141         lqimode1 = 0;
2142         lqomode0 = 0;
2143         lqomode1 = 0;
2144         saved_modes = ahd_save_modes(ahd);
2145         for (;;) {
2146                 struct  cs *cs;
2147                 u_int   seqaddr;
2148                 u_int   i;
2149
2150                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2151                 seqaddr = ahd_inb(ahd, CURADDR)
2152                         | (ahd_inb(ahd, CURADDR+1) << 8);
2153
2154                 cs = ahd->critical_sections;
2155                 for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
2156                         
2157                         if (cs->begin < seqaddr && cs->end >= seqaddr)
2158                                 break;
2159                 }
2160
2161                 if (i == ahd->num_critical_sections)
2162                         break;
2163
2164                 if (steps > AHD_MAX_STEPS) {
2165                         printf("%s: Infinite loop in critical section\n"
2166                                "%s: First Instruction 0x%x now 0x%x\n",
2167                                ahd_name(ahd), ahd_name(ahd), first_instr,
2168                                seqaddr);
2169                         ahd_dump_card_state(ahd);
2170                         panic("critical section loop");
2171                 }
2172
2173                 steps++;
2174 #ifdef AHD_DEBUG
2175                 if ((ahd_debug & AHD_SHOW_MISC) != 0)
2176                         printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
2177                                seqaddr);
2178 #endif
2179                 if (stepping == FALSE) {
2180
2181                         first_instr = seqaddr;
2182                         ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2183                         simode0 = ahd_inb(ahd, SIMODE0);
2184                         simode3 = ahd_inb(ahd, SIMODE3);
2185                         lqimode0 = ahd_inb(ahd, LQIMODE0);
2186                         lqimode1 = ahd_inb(ahd, LQIMODE1);
2187                         lqomode0 = ahd_inb(ahd, LQOMODE0);
2188                         lqomode1 = ahd_inb(ahd, LQOMODE1);
2189                         ahd_outb(ahd, SIMODE0, 0);
2190                         ahd_outb(ahd, SIMODE3, 0);
2191                         ahd_outb(ahd, LQIMODE0, 0);
2192                         ahd_outb(ahd, LQIMODE1, 0);
2193                         ahd_outb(ahd, LQOMODE0, 0);
2194                         ahd_outb(ahd, LQOMODE1, 0);
2195                         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2196                         simode1 = ahd_inb(ahd, SIMODE1);
2197                         ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
2198                         ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
2199                         stepping = TRUE;
2200                 }
2201                 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
2202                 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2203                 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
2204                 ahd_outb(ahd, HCNTRL, ahd->unpause);
2205                 do {
2206                         ahd_delay(200);
2207                 } while (!ahd_is_paused(ahd));
2208                 ahd_update_modes(ahd);
2209         }
2210         if (stepping) {
2211                 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2212                 ahd_outb(ahd, SIMODE0, simode0);
2213                 ahd_outb(ahd, SIMODE3, simode3);
2214                 ahd_outb(ahd, LQIMODE0, lqimode0);
2215                 ahd_outb(ahd, LQIMODE1, lqimode1);
2216                 ahd_outb(ahd, LQOMODE0, lqomode0);
2217                 ahd_outb(ahd, LQOMODE1, lqomode1);
2218                 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2219                 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
2220                 ahd_outb(ahd, SIMODE1, simode1);
2221                 /*
2222                  * SCSIINT seems to glitch occassionally when
2223                  * the interrupt masks are restored.  Clear SCSIINT
2224                  * one more time so that only persistent errors
2225                  * are seen as a real interrupt.
2226                  */
2227                 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2228         }
2229         ahd_restore_modes(ahd, saved_modes);
2230 }
2231
2232 /*
2233  * Clear any pending interrupt status.
2234  */
2235 void
2236 ahd_clear_intstat(struct ahd_softc *ahd)
2237 {
2238         AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
2239                          ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
2240         /* Clear any interrupt conditions this may have caused */
2241         ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
2242                                  |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
2243         ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
2244                                  |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
2245                                  |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
2246         ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
2247                                  |CLRLQOATNPKT|CLRLQOTCRC);
2248         ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
2249                                  |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
2250         if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
2251                 ahd_outb(ahd, CLRLQOINT0, 0);
2252                 ahd_outb(ahd, CLRLQOINT1, 0);
2253         }
2254         ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
2255         ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
2256                                 |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
2257         ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
2258                                 |CLRIOERR|CLROVERRUN);
2259         ahd_outb(ahd, CLRINT, CLRSCSIINT);
2260 }
2261
2262 /**************************** Debugging Routines ******************************/
2263 #ifdef AHD_DEBUG
2264 uint32_t ahd_debug = AHD_DEBUG_OPTS;
2265 #endif
2266 void
2267 ahd_print_scb(struct scb *scb)
2268 {
2269         struct hardware_scb *hscb;
2270         int i;
2271
2272         hscb = scb->hscb;
2273         printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
2274                (void *)scb,
2275                hscb->control,
2276                hscb->scsiid,
2277                hscb->lun,
2278                hscb->cdb_len);
2279         printf("Shared Data: ");
2280         for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
2281                 printf("%#02x", hscb->shared_data.idata.cdb[i]);
2282         printf("        dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
2283                (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
2284                (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
2285                ahd_le32toh(hscb->datacnt),
2286                ahd_le32toh(hscb->sgptr),
2287                SCB_GET_TAG(scb));
2288         ahd_dump_sglist(scb);
2289 }
2290
2291 void
2292 ahd_dump_sglist(struct scb *scb)
2293 {
2294         int i;
2295
2296         if (scb->sg_count > 0) {
2297                 if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
2298                         struct ahd_dma64_seg *sg_list;
2299
2300                         sg_list = (struct ahd_dma64_seg*)scb->sg_list;
2301                         for (i = 0; i < scb->sg_count; i++) {
2302                                 uint64_t addr;
2303                                 uint32_t len;
2304
2305                                 addr = ahd_le64toh(sg_list[i].addr);
2306                                 len = ahd_le32toh(sg_list[i].len);
2307                                 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2308                                        i,
2309                                        (uint32_t)((addr >> 32) & 0xFFFFFFFF),
2310                                        (uint32_t)(addr & 0xFFFFFFFF),
2311                                        sg_list[i].len & AHD_SG_LEN_MASK,
2312                                        (sg_list[i].len & AHD_DMA_LAST_SEG)
2313                                      ? " Last" : "");
2314                         }
2315                 } else {
2316                         struct ahd_dma_seg *sg_list;
2317
2318                         sg_list = (struct ahd_dma_seg*)scb->sg_list;
2319                         for (i = 0; i < scb->sg_count; i++) {
2320                                 uint32_t len;
2321
2322                                 len = ahd_le32toh(sg_list[i].len);
2323                                 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2324                                        i,
2325                                        (len >> 24) & SG_HIGH_ADDR_BITS,
2326                                        ahd_le32toh(sg_list[i].addr),
2327                                        len & AHD_SG_LEN_MASK,
2328                                        len & AHD_DMA_LAST_SEG ? " Last" : "");
2329                         }
2330                 }
2331         }
2332 }
2333
2334 /************************* Transfer Negotiation *******************************/
2335 /*
2336  * Allocate per target mode instance (ID we respond to as a target)
2337  * transfer negotiation data structures.
2338  */
2339 static struct ahd_tmode_tstate *
2340 ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
2341 {
2342         struct ahd_tmode_tstate *master_tstate;
2343         struct ahd_tmode_tstate *tstate;
2344         int i;
2345
2346         master_tstate = ahd->enabled_targets[ahd->our_id];
2347         if (ahd->enabled_targets[scsi_id] != NULL
2348          && ahd->enabled_targets[scsi_id] != master_tstate)
2349                 panic("%s: ahd_alloc_tstate - Target already allocated",
2350                       ahd_name(ahd));
2351         tstate = malloc(sizeof(*tstate), M_DEVBUF, M_WAITOK);
2352
2353         /*
2354          * If we have allocated a master tstate, copy user settings from
2355          * the master tstate (taken from SRAM or the EEPROM) for this
2356          * channel, but reset our current and goal settings to async/narrow
2357          * until an initiator talks to us.
2358          */
2359         if (master_tstate != NULL) {
2360                 memcpy(tstate, master_tstate, sizeof(*tstate));
2361                 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
2362                 for (i = 0; i < 16; i++) {
2363                         memset(&tstate->transinfo[i].curr, 0,
2364                               sizeof(tstate->transinfo[i].curr));
2365                         memset(&tstate->transinfo[i].goal, 0,
2366                               sizeof(tstate->transinfo[i].goal));
2367                 }
2368         } else
2369                 memset(tstate, 0, sizeof(*tstate));
2370         ahd->enabled_targets[scsi_id] = tstate;
2371         return (tstate);
2372 }
2373
2374 #ifdef AHD_TARGET_MODE
2375 /*
2376  * Free per target mode instance (ID we respond to as a target)
2377  * transfer negotiation data structures.
2378  */
2379 static void
2380 ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
2381 {
2382         struct ahd_tmode_tstate *tstate;
2383
2384         /*
2385          * Don't clean up our "master" tstate.
2386          * It has our default user settings.
2387          */
2388         if (scsi_id == ahd->our_id
2389          && force == FALSE)
2390                 return;
2391
2392         tstate = ahd->enabled_targets[scsi_id];
2393         if (tstate != NULL)
2394                 free(tstate, M_DEVBUF);
2395         ahd->enabled_targets[scsi_id] = NULL;
2396 }
2397 #endif
2398
2399 /*
2400  * Called when we have an active connection to a target on the bus,
2401  * this function finds the nearest period to the input period limited
2402  * by the capabilities of the bus connectivity of and sync settings for
2403  * the target.
2404  */
2405 void
2406 ahd_devlimited_syncrate(struct ahd_softc *ahd,
2407                         struct ahd_initiator_tinfo *tinfo,
2408                         u_int *period, u_int *ppr_options, role_t role)
2409 {
2410         struct  ahd_transinfo *transinfo;
2411         u_int   maxsync;
2412
2413         if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
2414          && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
2415                 maxsync = AHD_SYNCRATE_PACED;
2416         } else {
2417                 maxsync = AHD_SYNCRATE_ULTRA;
2418                 /* Can't do DT related options on an SE bus */
2419                 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2420         }
2421         /*
2422          * Never allow a value higher than our current goal
2423          * period otherwise we may allow a target initiated
2424          * negotiation to go above the limit as set by the
2425          * user.  In the case of an initiator initiated
2426          * sync negotiation, we limit based on the user
2427          * setting.  This allows the system to still accept
2428          * incoming negotiations even if target initiated
2429          * negotiation is not performed.
2430          */
2431         if (role == ROLE_TARGET)
2432                 transinfo = &tinfo->user;
2433         else 
2434                 transinfo = &tinfo->goal;
2435         *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
2436         if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
2437                 maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
2438                 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2439         }
2440         if (transinfo->period == 0) {
2441                 *period = 0;
2442                 *ppr_options = 0;
2443         } else {
2444                 *period = MAX(*period, transinfo->period);
2445                 ahd_find_syncrate(ahd, period, ppr_options, maxsync);
2446         }
2447 }
2448
2449 /*
2450  * Look up the valid period to SCSIRATE conversion in our table.
2451  * Return the period and offset that should be sent to the target
2452  * if this was the beginning of an SDTR.
2453  */
2454 void
2455 ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
2456                   u_int *ppr_options, u_int maxsync)
2457 {
2458         if (*period < maxsync)
2459                 *period = maxsync;
2460
2461         if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2462          && *period > AHD_SYNCRATE_MIN_DT)
2463                 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2464                 
2465         if (*period > AHD_SYNCRATE_MIN)
2466                 *period = 0;
2467
2468         /* Honor PPR option conformance rules. */
2469         if (*period > AHD_SYNCRATE_PACED)
2470                 *ppr_options &= ~MSG_EXT_PPR_RTI;
2471
2472         if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
2473                 *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
2474
2475         if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
2476                 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2477
2478         /* Skip all PACED only entries if IU is not available */
2479         if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
2480          && *period < AHD_SYNCRATE_DT)
2481                 *period = AHD_SYNCRATE_DT;
2482
2483         /* Skip all DT only entries if DT is not available */
2484         if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
2485          && *period < AHD_SYNCRATE_ULTRA2)
2486                 *period = AHD_SYNCRATE_ULTRA2;
2487 }
2488
2489 /*
2490  * Truncate the given synchronous offset to a value the
2491  * current adapter type and syncrate are capable of.
2492  */
2493 void
2494 ahd_validate_offset(struct ahd_softc *ahd,
2495                     struct ahd_initiator_tinfo *tinfo,
2496                     u_int period, u_int *offset, int wide,
2497                     role_t role)
2498 {
2499         u_int maxoffset;
2500
2501         /* Limit offset to what we can do */
2502         if (period == 0)
2503                 maxoffset = 0;
2504         else if (period <= AHD_SYNCRATE_PACED) {
2505                 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
2506                         maxoffset = MAX_OFFSET_PACED_BUG;
2507                 else
2508                         maxoffset = MAX_OFFSET_PACED;
2509         } else
2510                 maxoffset = MAX_OFFSET_NON_PACED;
2511         *offset = MIN(*offset, maxoffset);
2512         if (tinfo != NULL) {
2513                 if (role == ROLE_TARGET)
2514                         *offset = MIN(*offset, tinfo->user.offset);
2515                 else
2516                         *offset = MIN(*offset, tinfo->goal.offset);
2517         }
2518 }
2519
2520 /*
2521  * Truncate the given transfer width parameter to a value the
2522  * current adapter type is capable of.
2523  */
2524 void
2525 ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
2526                    u_int *bus_width, role_t role)
2527 {
2528         switch (*bus_width) {
2529         default:
2530                 if (ahd->features & AHD_WIDE) {
2531                         /* Respond Wide */
2532                         *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2533                         break;
2534                 }
2535                 /* FALLTHROUGH */
2536         case MSG_EXT_WDTR_BUS_8_BIT:
2537                 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2538                 break;
2539         }
2540         if (tinfo != NULL) {
2541                 if (role == ROLE_TARGET)
2542                         *bus_width = MIN(tinfo->user.width, *bus_width);
2543                 else
2544                         *bus_width = MIN(tinfo->goal.width, *bus_width);
2545         }
2546 }
2547
2548 /*
2549  * Update the bitmask of targets for which the controller should
2550  * negotiate with at the next convenient oportunity.  This currently
2551  * means the next time we send the initial identify messages for
2552  * a new transaction.
2553  */
2554 int
2555 ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2556                        struct ahd_tmode_tstate *tstate,
2557                        struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
2558 {
2559         u_int auto_negotiate_orig;
2560
2561         auto_negotiate_orig = tstate->auto_negotiate;
2562         if (neg_type == AHD_NEG_ALWAYS) {
2563                 /*
2564                  * Force our "current" settings to be
2565                  * unknown so that unless a bus reset
2566                  * occurs the need to renegotiate is
2567                  * recorded persistently.
2568                  */
2569                 if ((ahd->features & AHD_WIDE) != 0)
2570                         tinfo->curr.width = AHD_WIDTH_UNKNOWN;
2571                 tinfo->curr.period = AHD_PERIOD_UNKNOWN;
2572                 tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
2573         }
2574         if (tinfo->curr.period != tinfo->goal.period
2575          || tinfo->curr.width != tinfo->goal.width
2576          || tinfo->curr.offset != tinfo->goal.offset
2577          || tinfo->curr.ppr_options != tinfo->goal.ppr_options
2578          || (neg_type == AHD_NEG_IF_NON_ASYNC
2579           && (tinfo->goal.offset != 0
2580            || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
2581            || tinfo->goal.ppr_options != 0)))
2582                 tstate->auto_negotiate |= devinfo->target_mask;
2583         else
2584                 tstate->auto_negotiate &= ~devinfo->target_mask;
2585
2586         return (auto_negotiate_orig != tstate->auto_negotiate);
2587 }
2588
2589 /*
2590  * Update the user/goal/curr tables of synchronous negotiation
2591  * parameters as well as, in the case of a current or active update,
2592  * any data structures on the host controller.  In the case of an
2593  * active update, the specified target is currently talking to us on
2594  * the bus, so the transfer parameter update must take effect
2595  * immediately.
2596  */
2597 void
2598 ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2599                  u_int period, u_int offset, u_int ppr_options,
2600                  u_int type, int paused)
2601 {
2602         struct  ahd_initiator_tinfo *tinfo;
2603         struct  ahd_tmode_tstate *tstate;
2604         u_int   old_period;
2605         u_int   old_offset;
2606         u_int   old_ppr;
2607         int     active;
2608         int     update_needed;
2609
2610         active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
2611         update_needed = 0;
2612
2613         if (period == 0 || offset == 0) {
2614                 period = 0;
2615                 offset = 0;
2616         }
2617
2618         tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
2619                                     devinfo->target, &tstate);
2620
2621         if ((type & AHD_TRANS_USER) != 0) {
2622                 tinfo->user.period = period;
2623                 tinfo->user.offset = offset;
2624                 tinfo->user.ppr_options = ppr_options;
2625         }
2626
2627         if ((type & AHD_TRANS_GOAL) != 0) {
2628                 tinfo->goal.period = period;
2629                 tinfo->goal.offset = offset;
2630                 tinfo->goal.ppr_options = ppr_options;
2631         }
2632
2633         old_period = tinfo->curr.period;
2634         old_offset = tinfo->curr.offset;
2635         old_ppr    = tinfo->curr.ppr_options;
2636
2637         if ((type & AHD_TRANS_CUR) != 0
2638          && (old_period != period
2639           || old_offset != offset
2640           || old_ppr != ppr_options)) {
2641
2642                 update_needed++;
2643
2644                 tinfo->curr.period = period;
2645                 tinfo->curr.offset = offset;
2646                 tinfo->curr.ppr_options = ppr_options;
2647
2648                 ahd_send_async(ahd, devinfo->channel, devinfo->target,
2649                                CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
2650                 if (bootverbose) {
2651                         if (offset != 0) {
2652                                 int options;
2653
2654                                 printf("%s: target %d synchronous with "
2655                                        "period = 0x%x, offset = 0x%x",
2656                                        ahd_name(ahd), devinfo->target,
2657                                        period, offset);
2658                                 options = 0;
2659                                 if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
2660                                         printf("(RDSTRM");
2661                                         options++;
2662                                 }
2663                                 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
2664                                         printf("%s", options ? "|DT" : "(DT");
2665                                         options++;
2666                                 }
2667                                 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
2668                                         printf("%s", options ? "|IU" : "(IU");
2669                                         options++;
2670                                 }
2671                                 if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
2672                                         printf("%s", options ? "|RTI" : "(RTI");
2673                                         options++;
2674                                 }
2675                                 if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
2676                                         printf("%s", options ? "|QAS" : "(QAS");
2677                                         options++;
2678                                 }
2679                                 if (options != 0)
2680                                         printf(")\n");
2681                                 else
2682                                         printf("\n");
2683                         } else {
2684                                 printf("%s: target %d using "
2685                                        "asynchronous transfers%s\n",
2686                                        ahd_name(ahd), devinfo->target,
2687                                        (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
2688                                      ?  "(QAS)" : "");
2689                         }
2690                 }
2691         }
2692         /*
2693          * Always refresh the neg-table to handle the case of the
2694          * sequencer setting the ENATNO bit for a MK_MESSAGE request.
2695          * We will always renegotiate in that case if this is a
2696          * packetized request.  Also manage the busfree expected flag
2697          * from this common routine so that we catch changes due to
2698          * WDTR or SDTR messages.
2699          */
2700         if ((type & AHD_TRANS_CUR) != 0) {
2701                 if (!paused)
2702                         ahd_pause(ahd);
2703                 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
2704                 if (!paused)
2705                         ahd_unpause(ahd);
2706                 if (ahd->msg_type != MSG_TYPE_NONE) {
2707                         if ((old_ppr & MSG_EXT_PPR_IU_REQ)
2708                          != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
2709 #ifdef AHD_DEBUG
2710                                 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
2711                                         ahd_print_devinfo(ahd, devinfo);
2712                                         printf("Expecting IU Change busfree\n");
2713                                 }
2714 #endif
2715                                 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
2716                                                |  MSG_FLAG_IU_REQ_CHANGED;
2717                         }
2718                         if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
2719 #ifdef AHD_DEBUG
2720                                 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2721                                         printf("PPR with IU_REQ outstanding\n");
2722 #endif
2723                                 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
2724                         }
2725                 }
2726         }
2727
2728         update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
2729                                                 tinfo, AHD_NEG_TO_GOAL);
2730
2731         if (update_needed && active)
2732                 ahd_update_pending_scbs(ahd);
2733 }
2734
2735 /*
2736  * Update the user/goal/curr tables of wide negotiation
2737  * parameters as well as, in the case of a current or active update,
2738  * any data structures on the host controller.  In the case of an
2739  * active update, the specified target is currently talking to us on
2740  * the bus, so the transfer parameter update must take effect
2741  * immediately.
2742  */
2743 void
2744 ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2745               u_int width, u_int type, int paused)
2746 {
2747         struct  ahd_initiator_tinfo *tinfo;
2748         struct  ahd_tmode_tstate *tstate;
2749         u_int   oldwidth;
2750         int     active;
2751         int     update_needed;
2752
2753         active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
2754         update_needed = 0;
2755         tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
2756                                     devinfo->target, &tstate);
2757
2758         if ((type & AHD_TRANS_USER) != 0)
2759                 tinfo->user.width = width;
2760
2761         if ((type & AHD_TRANS_GOAL) != 0)
2762                 tinfo->goal.width = width;
2763
2764         oldwidth = tinfo->curr.width;
2765         if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
2766
2767                 update_needed++;
2768
2769                 tinfo->curr.width = width;
2770                 ahd_send_async(ahd, devinfo->channel, devinfo->target,
2771                                CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
2772                 if (bootverbose) {
2773                         printf("%s: target %d using %dbit transfers\n",
2774                                ahd_name(ahd), devinfo->target,
2775                                8 * (0x01 << width));
2776                 }
2777         }
2778
2779         if ((type & AHD_TRANS_CUR) != 0) {
2780                 if (!paused)
2781                         ahd_pause(ahd);
2782                 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
2783                 if (!paused)
2784                         ahd_unpause(ahd);
2785         }
2786
2787         update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
2788                                                 tinfo, AHD_NEG_TO_GOAL);
2789         if (update_needed && active)
2790                 ahd_update_pending_scbs(ahd);
2791
2792 }
2793
2794 /*
2795  * Update the current state of tagged queuing for a given target.
2796  */
2797 void
2798 ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2799              ahd_queue_alg alg)
2800 {
2801         ahd_platform_set_tags(ahd, devinfo, alg);
2802         ahd_send_async(ahd, devinfo->channel, devinfo->target,
2803                        devinfo->lun, AC_TRANSFER_NEG, &alg);
2804 }
2805
2806 static void
2807 ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2808                      struct ahd_transinfo *tinfo)
2809 {
2810         ahd_mode_state  saved_modes;
2811         u_int           period;
2812         u_int           ppr_opts;
2813         u_int           con_opts;
2814         u_int           offset;
2815         u_int           saved_negoaddr;
2816         uint8_t         iocell_opts[sizeof(ahd->iocell_opts)];
2817
2818         saved_modes = ahd_save_modes(ahd);
2819         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2820
2821         saved_negoaddr = ahd_inb(ahd, NEGOADDR);
2822         ahd_outb(ahd, NEGOADDR, devinfo->target);
2823         period = tinfo->period;
2824         offset = tinfo->offset;
2825         memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts)); 
2826         ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
2827                                         |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
2828         con_opts = 0;
2829         if (period == 0)
2830                 period = AHD_SYNCRATE_ASYNC;
2831         if (period == AHD_SYNCRATE_160) {
2832
2833                 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
2834                         /*
2835                          * When the SPI4 spec was finalized, PACE transfers
2836                          * was not made a configurable option in the PPR
2837                          * message.  Instead it is assumed to be enabled for
2838                          * any syncrate faster than 80MHz.  Nevertheless,
2839                          * Harpoon2A4 allows this to be configurable.
2840                          *
2841                          * Harpoon2A4 also assumes at most 2 data bytes per
2842                          * negotiated REQ/ACK offset.  Paced transfers take
2843                          * 4, so we must adjust our offset.
2844                          */
2845                         ppr_opts |= PPROPT_PACE;
2846                         offset *= 2;
2847
2848                         /*
2849                          * Harpoon2A assumed that there would be a
2850                          * fallback rate between 160MHz and 80Mhz,
2851                          * so 7 is used as the period factor rather
2852                          * than 8 for 160MHz.
2853                          */
2854                         period = AHD_SYNCRATE_REVA_160;
2855                 }
2856                 if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
2857                         iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
2858                             ~AHD_PRECOMP_MASK;
2859         } else {
2860                 /*
2861                  * Precomp should be disabled for non-paced transfers.
2862                  */
2863                 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
2864
2865                 if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
2866                  && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0) {
2867                         /*
2868                          * Slow down our CRC interval to be
2869                          * compatible with devices that can't
2870                          * handle a CRC at full speed.
2871                          */
2872                         con_opts |= ENSLOWCRC;
2873                 }
2874         }
2875
2876         ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
2877         ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
2878         ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
2879         ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
2880
2881         ahd_outb(ahd, NEGPERIOD, period);
2882         ahd_outb(ahd, NEGPPROPTS, ppr_opts);
2883         ahd_outb(ahd, NEGOFFSET, offset);
2884
2885         if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
2886                 con_opts |= WIDEXFER;
2887
2888         /*
2889          * During packetized transfers, the target will
2890          * give us the oportunity to send command packets
2891          * without us asserting attention.
2892          */
2893         if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
2894                 con_opts |= ENAUTOATNO;
2895         ahd_outb(ahd, NEGCONOPTS, con_opts);
2896         ahd_outb(ahd, NEGOADDR, saved_negoaddr);
2897         ahd_restore_modes(ahd, saved_modes);
2898 }
2899
2900 /*
2901  * When the transfer settings for a connection change, setup for
2902  * negotiation in pending SCBs to effect the change as quickly as
2903  * possible.  We also cancel any negotiations that are scheduled
2904  * for inflight SCBs that have not been started yet.
2905  */
2906 static void
2907 ahd_update_pending_scbs(struct ahd_softc *ahd)
2908 {
2909         struct          scb *pending_scb;
2910         int             pending_scb_count;
2911         int             i;
2912         int             paused;
2913         u_int           saved_scbptr;
2914         ahd_mode_state  saved_modes;
2915
2916         /*
2917          * Traverse the pending SCB list and ensure that all of the
2918          * SCBs there have the proper settings.  We can only safely
2919          * clear the negotiation required flag (setting requires the
2920          * execution queue to be modified) and this is only possible
2921          * if we are not already attempting to select out for this
2922          * SCB.  For this reason, all callers only call this routine
2923          * if we are changing the negotiation settings for the currently
2924          * active transaction on the bus.
2925          */
2926         pending_scb_count = 0;
2927         LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2928                 struct ahd_devinfo devinfo;
2929                 struct hardware_scb *pending_hscb;
2930                 struct ahd_initiator_tinfo *tinfo;
2931                 struct ahd_tmode_tstate *tstate;
2932
2933                 ahd_scb_devinfo(ahd, &devinfo, pending_scb);
2934                 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
2935                                             devinfo.our_scsiid,
2936                                             devinfo.target, &tstate);
2937                 pending_hscb = pending_scb->hscb;
2938                 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
2939                  && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
2940                         pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
2941                         pending_hscb->control &= ~MK_MESSAGE;
2942                 }
2943                 ahd_sync_scb(ahd, pending_scb,
2944                              BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2945                 pending_scb_count++;
2946         }
2947
2948         if (pending_scb_count == 0)
2949                 return;
2950
2951         if (ahd_is_paused(ahd)) {
2952                 paused = 1;
2953         } else {
2954                 paused = 0;
2955                 ahd_pause(ahd);
2956         }
2957
2958         /*
2959          * Force the sequencer to reinitialize the selection for
2960          * the command at the head of the execution queue if it
2961          * has already been setup.  The negotiation changes may
2962          * effect whether we select-out with ATN.
2963          */
2964         saved_modes = ahd_save_modes(ahd);
2965         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2966         ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2967         saved_scbptr = ahd_get_scbptr(ahd);
2968         /* Ensure that the hscbs down on the card match the new information */
2969         for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
2970                 struct  hardware_scb *pending_hscb;
2971                 u_int   control;
2972                 u_int   scb_tag;
2973
2974                 ahd_set_scbptr(ahd, i);
2975                 scb_tag = i;
2976                 pending_scb = ahd_lookup_scb(ahd, scb_tag);
2977                 if (pending_scb == NULL)
2978                         continue;
2979
2980                 pending_hscb = pending_scb->hscb;
2981                 control = ahd_inb_scbram(ahd, SCB_CONTROL);
2982                 control &= ~MK_MESSAGE;
2983                 control |= pending_hscb->control & MK_MESSAGE;
2984                 ahd_outb(ahd, SCB_CONTROL, control);
2985         }
2986         ahd_set_scbptr(ahd, saved_scbptr);
2987         ahd_restore_modes(ahd, saved_modes);
2988
2989         if (paused == 0)
2990                 ahd_unpause(ahd);
2991 }
2992
2993 /**************************** Pathing Information *****************************/
2994 static void
2995 ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
2996 {
2997         ahd_mode_state  saved_modes;
2998         u_int           saved_scsiid;
2999         role_t          role;
3000         int             our_id;
3001
3002         saved_modes = ahd_save_modes(ahd);
3003         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3004
3005         if (ahd_inb(ahd, SSTAT0) & TARGET)
3006                 role = ROLE_TARGET;
3007         else
3008                 role = ROLE_INITIATOR;
3009
3010         if (role == ROLE_TARGET
3011          && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
3012                 /* We were selected, so pull our id from TARGIDIN */
3013                 our_id = ahd_inb(ahd, TARGIDIN) & OID;
3014         } else if (role == ROLE_TARGET)
3015                 our_id = ahd_inb(ahd, TOWNID);
3016         else
3017                 our_id = ahd_inb(ahd, IOWNID);
3018
3019         saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
3020         ahd_compile_devinfo(devinfo,
3021                             our_id,
3022                             SCSIID_TARGET(ahd, saved_scsiid),
3023                             ahd_inb(ahd, SAVED_LUN),
3024                             SCSIID_CHANNEL(ahd, saved_scsiid),
3025                             role);
3026         ahd_restore_modes(ahd, saved_modes);
3027 }
3028
3029 void
3030 ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3031 {
3032         printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
3033                devinfo->target, devinfo->lun);
3034 }
3035
3036 struct ahd_phase_table_entry*
3037 ahd_lookup_phase_entry(int phase)
3038 {
3039         struct ahd_phase_table_entry *entry;
3040         struct ahd_phase_table_entry *last_entry;
3041
3042         /*
3043          * num_phases doesn't include the default entry which
3044          * will be returned if the phase doesn't match.
3045          */
3046         last_entry = &ahd_phase_table[num_phases];
3047         for (entry = ahd_phase_table; entry < last_entry; entry++) {
3048                 if (phase == entry->phase)
3049                         break;
3050         }
3051         return (entry);
3052 }
3053
3054 void
3055 ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
3056                     u_int lun, char channel, role_t role)
3057 {
3058         devinfo->our_scsiid = our_id;
3059         devinfo->target = target;
3060         devinfo->lun = lun;
3061         devinfo->target_offset = target;
3062         devinfo->channel = channel;
3063         devinfo->role = role;
3064         if (channel == 'B')
3065                 devinfo->target_offset += 8;
3066         devinfo->target_mask = (0x01 << devinfo->target_offset);
3067 }
3068
3069 static void
3070 ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3071                 struct scb *scb)
3072 {
3073         role_t  role;
3074         int     our_id;
3075
3076         our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
3077         role = ROLE_INITIATOR;
3078         if ((scb->hscb->control & TARGET_SCB) != 0)
3079                 role = ROLE_TARGET;
3080         ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
3081                             SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
3082 }
3083
3084
3085 /************************ Message Phase Processing ****************************/
3086 /*
3087  * When an initiator transaction with the MK_MESSAGE flag either reconnects
3088  * or enters the initial message out phase, we are interrupted.  Fill our
3089  * outgoing message buffer with the appropriate message and beging handing
3090  * the message phase(s) manually.
3091  */
3092 static void
3093 ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3094                            struct scb *scb)
3095 {
3096         /*
3097          * To facilitate adding multiple messages together,
3098          * each routine should increment the index and len
3099          * variables instead of setting them explicitly.
3100          */
3101         ahd->msgout_index = 0;
3102         ahd->msgout_len = 0;
3103
3104         if (ahd_currently_packetized(ahd))
3105                 ahd->msg_flags |= MSG_FLAG_PACKETIZED;
3106
3107         if (ahd->send_msg_perror
3108          && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
3109                 ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
3110                 ahd->msgout_len++;
3111                 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3112 #ifdef AHD_DEBUG
3113                 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3114                         printf("Setting up for Parity Error delivery\n");
3115 #endif
3116                 return;
3117         } else if (scb == NULL) {
3118                 printf("%s: WARNING. No pending message for "
3119                        "I_T msgin.  Issuing NO-OP\n", ahd_name(ahd));
3120                 ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
3121                 ahd->msgout_len++;
3122                 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3123                 return;
3124         }
3125
3126         if ((scb->flags & SCB_DEVICE_RESET) == 0
3127          && (scb->flags & SCB_PACKETIZED) == 0
3128          && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
3129                 u_int identify_msg;
3130
3131                 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
3132                 if ((scb->hscb->control & DISCENB) != 0)
3133                         identify_msg |= MSG_IDENTIFY_DISCFLAG;
3134                 ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
3135                 ahd->msgout_len++;
3136
3137                 if ((scb->hscb->control & TAG_ENB) != 0) {
3138                         ahd->msgout_buf[ahd->msgout_index++] =
3139                             scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
3140                         ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
3141                         ahd->msgout_len += 2;
3142                 }
3143         }
3144
3145         if (scb->flags & SCB_DEVICE_RESET) {
3146                 ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
3147                 ahd->msgout_len++;
3148                 ahd_print_path(ahd, scb);
3149                 printf("Bus Device Reset Message Sent\n");
3150                 /*
3151                  * Clear our selection hardware in advance of
3152                  * the busfree.  We may have an entry in the waiting
3153                  * Q for this target, and we don't want to go about
3154                  * selecting while we handle the busfree and blow it
3155                  * away.
3156                  */
3157                 ahd_outb(ahd, SCSISEQ0, 0);
3158         } else if ((scb->flags & SCB_ABORT) != 0) {
3159
3160                 if ((scb->hscb->control & TAG_ENB) != 0) {
3161                         ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
3162                 } else {
3163                         ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
3164                 }
3165                 ahd->msgout_len++;
3166                 ahd_print_path(ahd, scb);
3167                 printf("Abort%s Message Sent\n",
3168                        (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
3169                 /*
3170                  * Clear our selection hardware in advance of
3171                  * the busfree.  We may have an entry in the waiting
3172                  * Q for this target, and we don't want to go about
3173                  * selecting while we handle the busfree and blow it
3174                  * away.
3175                  */
3176                 ahd_outb(ahd, SCSISEQ0, 0);
3177         } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
3178                 ahd_build_transfer_msg(ahd, devinfo);
3179                 /*
3180                  * Clear our selection hardware in advance of potential
3181                  * PPR IU status change busfree.  We may have an entry in
3182                  * the waiting Q for this target, and we don't want to go
3183                  * about selecting while we handle the busfree and blow
3184                  * it away.
3185                  */
3186                 ahd_outb(ahd, SCSISEQ0, 0);
3187         } else {
3188                 printf("ahd_intr: AWAITING_MSG for an SCB that "
3189                        "does not have a waiting message\n");
3190                 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
3191                        devinfo->target_mask);
3192                 panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
3193                       "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
3194                       ahd_inb(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
3195                       scb->flags);
3196         }
3197
3198         /*
3199          * Clear the MK_MESSAGE flag from the SCB so we aren't
3200          * asked to send this message again.
3201          */
3202         ahd_outb(ahd, SCB_CONTROL,
3203                  ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
3204         scb->hscb->control &= ~MK_MESSAGE;
3205         ahd->msgout_index = 0;
3206         ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3207 }
3208
3209 /*
3210  * Build an appropriate transfer negotiation message for the
3211  * currently active target.
3212  */
3213 static void
3214 ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3215 {
3216         /*
3217          * We need to initiate transfer negotiations.
3218          * If our current and goal settings are identical,
3219          * we want to renegotiate due to a check condition.
3220          */
3221         struct  ahd_initiator_tinfo *tinfo;
3222         struct  ahd_tmode_tstate *tstate;
3223         int     dowide;
3224         int     dosync;
3225         int     doppr;
3226         u_int   period;
3227         u_int   ppr_options;
3228         u_int   offset;
3229
3230         tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3231                                     devinfo->target, &tstate);
3232         /*
3233          * Filter our period based on the current connection.
3234          * If we can't perform DT transfers on this segment (not in LVD
3235          * mode for instance), then our decision to issue a PPR message
3236          * may change.
3237          */
3238         period = tinfo->goal.period;
3239         offset = tinfo->goal.offset;
3240         ppr_options = tinfo->goal.ppr_options;
3241         /* Target initiated PPR is not allowed in the SCSI spec */
3242         if (devinfo->role == ROLE_TARGET)
3243                 ppr_options = 0;
3244         ahd_devlimited_syncrate(ahd, tinfo, &period,
3245                                 &ppr_options, devinfo->role);
3246         dowide = tinfo->curr.width != tinfo->goal.width;
3247         dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
3248         /*
3249          * Only use PPR if we have options that need it, even if the device
3250          * claims to support it.  There might be an expander in the way
3251          * that doesn't.
3252          */
3253         doppr = ppr_options != 0;
3254
3255         if (!dowide && !dosync && !doppr) {
3256                 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
3257                 dosync = tinfo->goal.offset != 0;
3258         }
3259
3260         if (!dowide && !dosync && !doppr) {
3261                 /*
3262                  * Force async with a WDTR message if we have a wide bus,
3263                  * or just issue an SDTR with a 0 offset.
3264                  */
3265                 if ((ahd->features & AHD_WIDE) != 0)
3266                         dowide = 1;
3267                 else
3268                         dosync = 1;
3269
3270                 if (bootverbose) {
3271                         ahd_print_devinfo(ahd, devinfo);
3272                         printf("Ensuring async\n");
3273                 }
3274         }
3275         /* Target initiated PPR is not allowed in the SCSI spec */
3276         if (devinfo->role == ROLE_TARGET)
3277                 doppr = 0;
3278
3279         /*
3280          * Both the PPR message and SDTR message require the
3281          * goal syncrate to be limited to what the target device
3282          * is capable of handling (based on whether an LVD->SE
3283          * expander is on the bus), so combine these two cases.
3284          * Regardless, guarantee that if we are using WDTR and SDTR
3285          * messages that WDTR comes first.
3286          */
3287         if (doppr || (dosync && !dowide)) {
3288
3289                 offset = tinfo->goal.offset;
3290                 ahd_validate_offset(ahd, tinfo, period, &offset,
3291                                     doppr ? tinfo->goal.width
3292                                           : tinfo->curr.width,
3293                                     devinfo->role);
3294                 if (doppr) {
3295                         ahd_construct_ppr(ahd, devinfo, period, offset,
3296                                           tinfo->goal.width, ppr_options);
3297                 } else {
3298                         ahd_construct_sdtr(ahd, devinfo, period, offset);
3299                 }
3300         } else {
3301                 ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
3302         }
3303 }
3304
3305 /*
3306  * Build a synchronous negotiation message in our message
3307  * buffer based on the input parameters.
3308  */
3309 static void
3310 ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3311                    u_int period, u_int offset)
3312 {
3313         if (offset == 0)
3314                 period = AHD_ASYNC_XFER_PERIOD;
3315         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3316         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
3317         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
3318         ahd->msgout_buf[ahd->msgout_index++] = period;
3319         ahd->msgout_buf[ahd->msgout_index++] = offset;
3320         ahd->msgout_len += 5;
3321         if (bootverbose) {
3322                 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3323                        ahd_name(ahd), devinfo->channel, devinfo->target,
3324                        devinfo->lun, period, offset);
3325         }
3326 }
3327
3328 /*
3329  * Build a wide negotiateion message in our message
3330  * buffer based on the input parameters.
3331  */
3332 static void
3333 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3334                    u_int bus_width)
3335 {
3336         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3337         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
3338         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
3339         ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3340         ahd->msgout_len += 4;
3341         if (bootverbose) {
3342                 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
3343                        ahd_name(ahd), devinfo->channel, devinfo->target,
3344                        devinfo->lun, bus_width);
3345         }
3346 }
3347
3348 /*
3349  * Build a parallel protocol request message in our message
3350  * buffer based on the input parameters.
3351  */
3352 static void
3353 ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3354                   u_int period, u_int offset, u_int bus_width,
3355                   u_int ppr_options)
3356 {
3357         /*
3358          * Always request precompensation from
3359          * the other target if we are running
3360          * at paced syncrates.
3361          */
3362         if (period <= AHD_SYNCRATE_PACED)
3363                 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
3364         if (offset == 0)
3365                 period = AHD_ASYNC_XFER_PERIOD;
3366         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3367         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
3368         ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
3369         ahd->msgout_buf[ahd->msgout_index++] = period;
3370         ahd->msgout_buf[ahd->msgout_index++] = 0;
3371         ahd->msgout_buf[ahd->msgout_index++] = offset;
3372         ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3373         ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
3374         ahd->msgout_len += 8;
3375         if (bootverbose) {
3376                 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
3377                        "offset %x, ppr_options %x\n", ahd_name(ahd),
3378                        devinfo->channel, devinfo->target, devinfo->lun,
3379                        bus_width, period, offset, ppr_options);
3380         }
3381 }
3382
3383 /*
3384  * Clear any active message state.
3385  */
3386 static void
3387 ahd_clear_msg_state(struct ahd_softc *ahd)
3388 {
3389         ahd_mode_state saved_modes;
3390
3391         saved_modes = ahd_save_modes(ahd);
3392         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3393         ahd->send_msg_perror = 0;
3394         ahd->msg_flags = MSG_FLAG_NONE;
3395         ahd->msgout_len = 0;
3396         ahd->msgin_index = 0;
3397         ahd->msg_type = MSG_TYPE_NONE;
3398         if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
3399                 /*
3400                  * The target didn't care to respond to our
3401                  * message request, so clear ATN.
3402                  */
3403                 ahd_outb(ahd, CLRSINT1, CLRATNO);
3404         }
3405         ahd_outb(ahd, MSG_OUT, MSG_NOOP);
3406         ahd_outb(ahd, SEQ_FLAGS2,
3407                  ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
3408         ahd_restore_modes(ahd, saved_modes);
3409 }
3410
3411 /*
3412  * Manual message loop handler.
3413  */
3414 static void
3415 ahd_handle_message_phase(struct ahd_softc *ahd)
3416
3417         struct  ahd_devinfo devinfo;
3418         u_int   bus_phase;
3419         int     end_session;
3420
3421         ahd_fetch_devinfo(ahd, &devinfo);
3422         end_session = FALSE;
3423         bus_phase = ahd_inb(ahd, LASTPHASE);
3424
3425         if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
3426                 printf("LQIRETRY for LQIPHASE_OUTPKT\n");
3427                 ahd_outb(ahd, LQCTL2, LQIRETRY);
3428         }
3429 reswitch:
3430         switch (ahd->msg_type) {
3431         case MSG_TYPE_INITIATOR_MSGOUT:
3432         {
3433                 int lastbyte;
3434                 int phasemis;
3435                 int msgdone;
3436
3437                 if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
3438                         panic("HOST_MSG_LOOP interrupt with no active message");
3439
3440 #ifdef AHD_DEBUG
3441                 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3442                         ahd_print_devinfo(ahd, &devinfo);
3443                         printf("INITIATOR_MSG_OUT");
3444                 }
3445 #endif
3446                 phasemis = bus_phase != P_MESGOUT;
3447                 if (phasemis) {
3448 #ifdef AHD_DEBUG
3449                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3450                                 printf(" PHASEMIS %s\n",
3451                                        ahd_lookup_phase_entry(bus_phase)
3452                                                              ->phasemsg);
3453                         }
3454 #endif
3455                         if (bus_phase == P_MESGIN) {
3456                                 /*
3457                                  * Change gears and see if
3458                                  * this messages is of interest to
3459                                  * us or should be passed back to
3460                                  * the sequencer.
3461                                  */
3462                                 ahd_outb(ahd, CLRSINT1, CLRATNO);
3463                                 ahd->send_msg_perror = 0;
3464                                 ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
3465                                 ahd->msgin_index = 0;
3466                                 goto reswitch;
3467                         }
3468                         end_session = TRUE;
3469                         break;
3470                 }
3471
3472                 if (ahd->send_msg_perror) {
3473                         ahd_outb(ahd, CLRSINT1, CLRATNO);
3474                         ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3475 #ifdef AHD_DEBUG
3476                         if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3477                                 printf(" byte 0x%x\n", ahd->send_msg_perror);
3478 #endif
3479                         /*
3480                          * If we are notifying the target of a CRC error
3481                          * during packetized operations, the target is
3482                          * within its rights to acknowledge our message
3483                          * with a busfree.
3484                          */
3485                         if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
3486                          && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
3487                                 ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
3488
3489                         ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
3490                         ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3491                         break;
3492                 }
3493
3494                 msgdone = ahd->msgout_index == ahd->msgout_len;
3495                 if (msgdone) {
3496                         /*
3497                          * The target has requested a retry.
3498                          * Re-assert ATN, reset our message index to
3499                          * 0, and try again.
3500                          */
3501                         ahd->msgout_index = 0;
3502                         ahd_assert_atn(ahd);
3503                 }
3504
3505                 lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
3506                 if (lastbyte) {
3507                         /* Last byte is signified by dropping ATN */
3508                         ahd_outb(ahd, CLRSINT1, CLRATNO);
3509                 }
3510
3511                 /*
3512                  * Clear our interrupt status and present
3513                  * the next byte on the bus.
3514                  */
3515                 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3516 #ifdef AHD_DEBUG
3517                 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3518                         printf(" byte 0x%x\n",
3519                                ahd->msgout_buf[ahd->msgout_index]);
3520 #endif
3521                 ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
3522                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3523                 break;
3524         }
3525         case MSG_TYP