aic7xxx.c revision 1.76 1 /* $NetBSD: aic7xxx.c,v 1.76 2001/07/07 15:53:15 thorpej Exp $ */
2
3 /*
4 * Generic driver for the aic7xxx based adaptec SCSI controllers
5 * Product specific probe and attach routines can be found in:
6 * i386/eisa/ahc_eisa.c 27/284X and aic7770 motherboard controllers
7 * pci/ahc_pci.c 3985, 3980, 3940, 2940, aic7895, aic7890,
8 * aic7880, aic7870, aic7860, and aic7850 controllers
9 *
10 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * Alternatively, this software may be distributed under the terms of the
23 * the GNU Public License ("GPL").
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx.c,v 1.42 2000/03/18 22:28:18 gibbs Exp $
38 */
39 /*
40 * A few notes on features of the driver.
41 *
42 * SCB paging takes advantage of the fact that devices stay disconnected
43 * from the bus a relatively long time and that while they're disconnected,
44 * having the SCBs for these transactions down on the host adapter is of
45 * little use. Instead of leaving this idle SCB down on the card we copy
46 * it back up into kernel memory and reuse the SCB slot on the card to
47 * schedule another transaction. This can be a real payoff when doing random
48 * I/O to tagged queueing devices since there are more transactions active at
49 * once for the device to sort for optimal seek reduction. The algorithm goes
50 * like this...
51 *
52 * The sequencer maintains two lists of its hardware SCBs. The first is the
53 * singly linked free list which tracks all SCBs that are not currently in
54 * use. The second is the doubly linked disconnected list which holds the
55 * SCBs of transactions that are in the disconnected state sorted most
56 * recently disconnected first. When the kernel queues a transaction to
57 * the card, a hardware SCB to "house" this transaction is retrieved from
58 * either of these two lists. If the SCB came from the disconnected list,
59 * a check is made to see if any data transfer or SCB linking (more on linking
60 * in a bit) information has been changed since it was copied from the host
61 * and if so, DMAs the SCB back up before it can be used. Once a hardware
62 * SCB has been obtained, the SCB is DMAed from the host. Before any work
63 * can begin on this SCB, the sequencer must ensure that either the SCB is
64 * for a tagged transaction or the target is not already working on another
65 * non-tagged transaction. If a conflict arises in the non-tagged case, the
66 * sequencer finds the SCB for the active transactions and sets the SCB_LINKED
67 * field in that SCB to this next SCB to execute. To facilitate finding
68 * active non-tagged SCBs, the last four bytes of up to the first four hardware
69 * SCBs serve as a storage area for the currently active SCB ID for each
70 * target.
71 *
72 * When a device reconnects, a search is made of the hardware SCBs to find
73 * the SCB for this transaction. If the search fails, a hardware SCB is
74 * pulled from either the free or disconnected SCB list and the proper
75 * SCB is DMAed from the host. If the MK_MESSAGE control bit is set
76 * in the control byte of the SCB while it was disconnected, the sequencer
77 * will assert ATN and attempt to issue a message to the host.
78 *
79 * When a command completes, a check for non-zero status and residuals is
80 * made. If either of these conditions exists, the SCB is DMAed back up to
81 * the host so that it can interpret this information. Additionally, in the
82 * case of bad status, the sequencer generates a special interrupt and pauses
83 * itself. This allows the host to setup a request sense command if it
84 * chooses for this target synchronously with the error so that sense
85 * information isn't lost.
86 *
87 */
88
89 #include "opt_ddb.h"
90 #include "opt_ahc.h"
91
92 #include <sys/param.h>
93 #include <sys/kernel.h>
94 #include <sys/systm.h>
95 #include <sys/device.h>
96 #include <sys/malloc.h>
97 #include <sys/buf.h>
98 #include <sys/proc.h>
99 #include <sys/scsiio.h>
100
101 #include <machine/bus.h>
102 #include <machine/intr.h>
103
104 #include <dev/scsipi/scsi_all.h>
105 #include <dev/scsipi/scsipi_all.h>
106 #include <dev/scsipi/scsi_message.h>
107 #include <dev/scsipi/scsipi_debug.h>
108 #include <dev/scsipi/scsiconf.h>
109
110 #include <uvm/uvm_extern.h>
111
112 #include <dev/ic/aic7xxxvar.h>
113 #include <dev/microcode/aic7xxx/sequencer.h>
114 #include <dev/microcode/aic7xxx/aic7xxx_reg.h>
115 #include <dev/microcode/aic7xxx/aic7xxx_seq.h>
116
117 #define ALL_CHANNELS '\0'
118 #define ALL_TARGETS_MASK 0xFFFF
119 #define INITIATOR_WILDCARD (~0)
120
121 #define SIM_IS_SCSIBUS_B(ahc, periph) \
122 ((periph)->periph_channel->chan_channel == 1)
123 #define SIM_CHANNEL(ahc, periph) \
124 (SIM_IS_SCSIBUS_B(ahc, periph) ? 'B' : 'A')
125 #define SIM_SCSI_ID(ahc, periph) \
126 (SIM_IS_SCSIBUS_B(ahc, periph) ? ahc->our_id_b : ahc->our_id)
127 #define SCB_IS_SCSIBUS_B(scb) \
128 (((scb)->hscb->tcl & SELBUSB) != 0)
129 #define SCB_TARGET(scb) \
130 (((scb)->hscb->tcl & TID) >> 4)
131 #define SCB_CHANNEL(scb) \
132 (SCB_IS_SCSIBUS_B(scb) ? 'B' : 'A')
133 #define SCB_LUN(scb) \
134 ((scb)->hscb->tcl & LID)
135 #define SCB_TARGET_OFFSET(scb) \
136 (SCB_TARGET(scb) + (SCB_IS_SCSIBUS_B(scb) ? 8 : 0))
137 #define SCB_TARGET_MASK(scb) \
138 (0x01 << (SCB_TARGET_OFFSET(scb)))
139 #define TCL_CHANNEL(ahc, tcl) \
140 ((((ahc)->features & AHC_TWIN) && ((tcl) & SELBUSB)) ? 'B' : 'A')
141 #define TCL_SCSI_ID(ahc, tcl) \
142 (TCL_CHANNEL((ahc), (tcl)) == 'B' ? (ahc)->our_id_b : (ahc)->our_id)
143 #define TCL_TARGET(tcl) (((tcl) & TID) >> TCL_TARGET_SHIFT)
144 #define TCL_LUN(tcl) ((tcl) & LID)
145
146 #define XS_TCL(ahc, xs) \
147 ((((xs)->xs_periph->periph_target << 4) & 0xF0) \
148 | (SIM_IS_SCSIBUS_B((ahc), (xs)->xs_periph) ? SELBUSB : 0) \
149 | ((xs)->xs_periph->periph_lun & 0x07))
150
151 const char * const ahc_chip_names[] =
152 {
153 "NONE",
154 "aic7770",
155 "aic7850",
156 "aic7855",
157 "aic7859",
158 "aic7860",
159 "aic7870",
160 "aic7880",
161 "aic7890/91",
162 "aic7892",
163 "aic7895",
164 "aic7896/97",
165 "aic7899"
166 };
167
168 typedef enum {
169 ROLE_UNKNOWN,
170 ROLE_INITIATOR,
171 ROLE_TARGET
172 } role_t;
173
174 struct ahc_devinfo {
175 int our_scsiid;
176 int target_offset;
177 u_int16_t target_mask;
178 u_int8_t target;
179 u_int8_t lun;
180 char channel;
181 role_t role; /*
182 * Only guaranteed to be correct if not
183 * in the busfree state.
184 */
185 };
186
187 typedef enum {
188 SEARCH_COMPLETE,
189 SEARCH_COUNT,
190 SEARCH_REMOVE
191 } ahc_search_action;
192
193 #ifdef AHC_DEBUG
194 static int ahc_debug = AHC_DEBUG;
195 #endif
196
197 static int ahcinitscbdata(struct ahc_softc *);
198 static void ahcfiniscbdata(struct ahc_softc *);
199
200 #if UNUSED
201 static void ahc_dump_targcmd(struct target_cmd *);
202 #endif
203 static void ahc_shutdown(void *arg);
204 static void ahc_action(struct scsipi_channel *,
205 scsipi_adapter_req_t, void *);
206 static int ahc_ioctl(struct scsipi_channel *, u_long, caddr_t, int,
207 struct proc *);
208 static void ahc_execute_scb(void *, bus_dma_segment_t *, int);
209 static int ahc_poll(struct ahc_softc *, int);
210 static void ahc_setup_data(struct ahc_softc *, struct scsipi_xfer *,
211 struct scb *);
212 static void ahc_freeze_devq(struct ahc_softc *, struct scsipi_periph *);
213 static void ahcallocscbs(struct ahc_softc *);
214 #if UNUSED
215 static void ahc_scb_devinfo(struct ahc_softc *, struct ahc_devinfo *,
216 struct scb *);
217 #endif
218 static void ahc_fetch_devinfo(struct ahc_softc *, struct ahc_devinfo *);
219 static void ahc_compile_devinfo(struct ahc_devinfo *, u_int, u_int, u_int,
220 char, role_t);
221 static u_int ahc_abort_wscb(struct ahc_softc *, u_int, u_int);
222 static void ahc_done(struct ahc_softc *, struct scb *);
223 static struct tmode_tstate *
224 ahc_alloc_tstate(struct ahc_softc *, u_int, char);
225 #if UNUSED
226 static void ahc_free_tstate(struct ahc_softc *, u_int, char, int);
227 #endif
228 static void ahc_handle_seqint(struct ahc_softc *, u_int);
229 static void ahc_handle_scsiint(struct ahc_softc *, u_int);
230 static void ahc_build_transfer_msg(struct ahc_softc *,
231 struct ahc_devinfo *);
232 static void ahc_setup_initiator_msgout(struct ahc_softc *,
233 struct ahc_devinfo *,
234 struct scb *);
235 static void ahc_setup_target_msgin(struct ahc_softc *,
236 struct ahc_devinfo *);
237 static void ahc_clear_msg_state(struct ahc_softc *);
238 static void ahc_handle_message_phase(struct ahc_softc *,
239 struct scsipi_periph *);
240 static int ahc_sent_msg(struct ahc_softc *, u_int, int);
241
242 static int ahc_parse_msg(struct ahc_softc *, struct scsipi_periph *,
243 struct ahc_devinfo *);
244 static void ahc_handle_ign_wide_residue(struct ahc_softc *,
245 struct ahc_devinfo *);
246 static void ahc_handle_devreset(struct ahc_softc *, struct ahc_devinfo *,
247 int, char *, int);
248 #ifdef AHC_DUMP_SEQ
249 static void ahc_dumpseq(struct ahc_softc *);
250 #endif
251 static void ahc_loadseq(struct ahc_softc *);
252 static int ahc_check_patch(struct ahc_softc *, const struct patch **,
253 int, int *);
254 static void ahc_download_instr(struct ahc_softc *, int, u_int8_t *);
255 static int ahc_match_scb(struct scb *, int, char, int, u_int, role_t);
256 #if defined(AHC_DEBUG)
257 static void ahc_print_scb(struct scb *);
258 #endif
259 static int ahc_search_qinfifo(struct ahc_softc *, int, char, int, u_int,
260 role_t, scb_flag, ahc_search_action);
261 static int ahc_reset_channel(struct ahc_softc *, char, int);
262 static int ahc_abort_scbs(struct ahc_softc *, int, char, int, u_int,
263 role_t, int);
264 static int ahc_search_disc_list(struct ahc_softc *, int,
265 char, int, u_int, int, int, int);
266 static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *, u_int, u_int);
267 static void ahc_add_curscb_to_free_list(struct ahc_softc *);
268 static void ahc_clear_intstat(struct ahc_softc *);
269 static void ahc_reset_current_bus(struct ahc_softc *);
270 static const struct ahc_syncrate *
271 ahc_devlimited_syncrate(struct ahc_softc *, u_int *);
272 static const struct ahc_syncrate *
273 ahc_find_syncrate(struct ahc_softc *, u_int *, u_int);
274 static u_int ahc_find_period(struct ahc_softc *, u_int, u_int);
275 static void ahc_validate_offset(struct ahc_softc *,
276 const struct ahc_syncrate *, u_int *, int);
277 static void ahc_update_target_msg_request(struct ahc_softc *,
278 struct ahc_devinfo *,
279 struct ahc_initiator_tinfo *,
280 int, int);
281 static void ahc_set_syncrate(struct ahc_softc *, struct ahc_devinfo *,
282 const struct ahc_syncrate *, u_int, u_int,
283 u_int, int, int);
284 static void ahc_set_width(struct ahc_softc *, struct ahc_devinfo *,
285 u_int, u_int, int, int);
286 static void ahc_set_tags(struct ahc_softc *, struct ahc_devinfo *, int);
287 static void ahc_update_xfer_mode(struct ahc_softc *, struct ahc_devinfo *);
288 static void ahc_construct_sdtr(struct ahc_softc *, u_int, u_int);
289
290 static void ahc_construct_wdtr(struct ahc_softc *, u_int);
291
292 static void ahc_calc_residual(struct scb *);
293
294 static void ahc_update_pending_syncrates(struct ahc_softc *);
295
296 static void ahc_set_recoveryscb(struct ahc_softc *, struct scb *);
297
298 static void ahc_timeout (void *);
299 static __inline int sequencer_paused(struct ahc_softc *);
300 static __inline void pause_sequencer(struct ahc_softc *);
301 static __inline void unpause_sequencer(struct ahc_softc *);
302 static void restart_sequencer(struct ahc_softc *);
303 static __inline u_int ahc_index_busy_tcl(struct ahc_softc *, u_int, int);
304
305 static __inline void ahc_busy_tcl(struct ahc_softc *, struct scb *);
306 static __inline int ahc_isbusy_tcl(struct ahc_softc *, struct scb *);
307
308 static __inline void ahc_freeze_ccb(struct scb *);
309 static __inline void ahcsetccbstatus(struct scsipi_xfer *, int);
310 static void ahc_run_qoutfifo(struct ahc_softc *);
311
312 static __inline struct ahc_initiator_tinfo *
313 ahc_fetch_transinfo(struct ahc_softc *,
314 char, u_int, u_int,
315 struct tmode_tstate **);
316 static void ahcfreescb(struct ahc_softc *, struct scb *);
317 static __inline struct scb *ahcgetscb(struct ahc_softc *);
318
319 static int ahc_createdmamem(bus_dma_tag_t, int, int, bus_dmamap_t *,
320 caddr_t *, bus_addr_t *, bus_dma_segment_t *,
321 int *, const char *, const char *);
322 static void ahc_freedmamem(bus_dma_tag_t, int, bus_dmamap_t,
323 caddr_t, bus_dma_segment_t *, int);
324 static void ahcminphys(struct buf *);
325
326 static __inline void ahc_swap_hscb(struct hardware_scb *);
327 static __inline void ahc_swap_sg(struct ahc_dma_seg *);
328 static int ahc_istagged_device(struct ahc_softc *, struct scsipi_xfer *, int);
329
330 #if defined(AHC_DEBUG) && 0
331 static void ahc_dumptinfo(struct ahc_softc *, struct ahc_initiator_tinfo *);
332 #endif
333
334 static __inline void
335 ahc_swap_hscb(struct hardware_scb *hscb)
336 {
337 hscb->SG_pointer = htole32(hscb->SG_pointer);
338 hscb->data = htole32(hscb->data);
339 hscb->datalen = htole32(hscb->datalen);
340 /*
341 * No need to swap cmdpointer; it's either 0 or set to
342 * cmdstore_busaddr, which is already swapped.
343 */
344 }
345
346 static __inline void
347 ahc_swap_sg(struct ahc_dma_seg *sg)
348 {
349 sg->addr = htole32(sg->addr);
350 sg->len = htole32(sg->len);
351 }
352
353 static void
354 ahcminphys(bp)
355 struct buf *bp;
356 {
357 /*
358 * Even though the card can transfer up to 16megs per command
359 * we are limited by the number of segments in the dma segment
360 * list that we can hold. The worst case is that all pages are
361 * discontinuous physically, hense the "page per segment" limit
362 * enforced here.
363 */
364 if (bp->b_bcount > AHC_MAXTRANSFER_SIZE) {
365 bp->b_bcount = AHC_MAXTRANSFER_SIZE;
366 }
367 minphys(bp);
368 }
369
370
371 static __inline u_int32_t
372 ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index)
373 {
374 return (ahc->scb_data->hscb_busaddr
375 + (sizeof(struct hardware_scb) * index));
376 }
377
378 #define AHC_BUSRESET_DELAY 25 /* Reset delay in us */
379
380 static __inline int
381 sequencer_paused(struct ahc_softc *ahc)
382 {
383 return ((ahc_inb(ahc, HCNTRL) & PAUSE) != 0);
384 }
385
386 static __inline void
387 pause_sequencer(struct ahc_softc *ahc)
388 {
389 ahc_outb(ahc, HCNTRL, ahc->pause);
390
391 /*
392 * Since the sequencer can disable pausing in a critical section, we
393 * must loop until it actually stops.
394 */
395 while (sequencer_paused(ahc) == 0)
396 ;
397 }
398
399 static __inline void
400 unpause_sequencer(struct ahc_softc *ahc)
401 {
402 if ((ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
403 ahc_outb(ahc, HCNTRL, ahc->unpause);
404 }
405
406 /*
407 * Restart the sequencer program from address zero
408 */
409 static void
410 restart_sequencer(struct ahc_softc *ahc)
411 {
412 u_int i;
413
414 pause_sequencer(ahc);
415
416 /*
417 * Everytime we restart the sequencer, there
418 * is the possiblitity that we have restarted
419 * within a three instruction window where an
420 * SCB has been marked free but has not made it
421 * onto the free list. Since SCSI events(bus reset,
422 * unexpected bus free) will always freeze the
423 * sequencer, we cannot close this window. To
424 * avoid losing an SCB, we reconsitute the free
425 * list every time we restart the sequencer.
426 */
427 ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
428 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
429
430 ahc_outb(ahc, SCBPTR, i);
431 if (ahc_inb(ahc, SCB_TAG) == SCB_LIST_NULL)
432 ahc_add_curscb_to_free_list(ahc);
433 }
434 ahc_outb(ahc, SEQCTL, FASTMODE|SEQRESET);
435 unpause_sequencer(ahc);
436 }
437
438 static __inline u_int
439 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl, int unbusy)
440 {
441 u_int scbid;
442
443 scbid = ahc->untagged_scbs[tcl];
444 if (unbusy) {
445 ahc->untagged_scbs[tcl] = SCB_LIST_NULL;
446 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
447 UNTAGGEDSCB_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
448 }
449
450 return (scbid);
451 }
452
453 static __inline void
454 ahc_busy_tcl(struct ahc_softc *ahc, struct scb *scb)
455 {
456 ahc->untagged_scbs[scb->hscb->tcl] = scb->hscb->tag;
457 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
458 UNTAGGEDSCB_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
459 }
460
461 static __inline int
462 ahc_isbusy_tcl(struct ahc_softc *ahc, struct scb *scb)
463 {
464 return ahc->untagged_scbs[scb->hscb->tcl] != SCB_LIST_NULL;
465 }
466
467 static __inline void
468 ahc_freeze_ccb(struct scb *scb)
469 {
470 struct scsipi_xfer *xs = scb->xs;
471
472 if (!(scb->flags & SCB_FREEZE_QUEUE)) {
473 scsipi_periph_freeze(xs->xs_periph, 1);
474 scb->flags |= SCB_FREEZE_QUEUE;
475 }
476 }
477
478 static __inline void
479 ahcsetccbstatus(struct scsipi_xfer *xs, int status)
480 {
481 xs->error = status;
482 }
483
484 static __inline struct ahc_initiator_tinfo *
485 ahc_fetch_transinfo(struct ahc_softc *ahc, char channel, u_int our_id,
486 u_int remote_id, struct tmode_tstate **tstate)
487 {
488 /*
489 * Transfer data structures are stored from the perspective
490 * of the target role. Since the parameters for a connection
491 * in the initiator role to a given target are the same as
492 * when the roles are reversed, we pretend we are the target.
493 */
494 if (channel == 'B')
495 our_id += 8;
496 *tstate = ahc->enabled_targets[our_id];
497 return (&(*tstate)->transinfo[remote_id]);
498 }
499
500 static void
501 ahc_run_qoutfifo(struct ahc_softc *ahc)
502 {
503 struct scb *scb;
504 u_int scb_index;
505
506 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap, 0,
507 256, BUS_DMASYNC_POSTREAD);
508
509 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
510 scb_index = ahc->qoutfifo[ahc->qoutfifonext];
511 ahc->qoutfifo[ahc->qoutfifonext++] = SCB_LIST_NULL;
512
513 scb = &ahc->scb_data->scbarray[scb_index];
514 if (scb_index >= ahc->scb_data->numscbs
515 || (scb->flags & SCB_ACTIVE) == 0) {
516 printf("%s: WARNING no command for scb %d "
517 "(cmdcmplt)\nQOUTPOS = %d\n",
518 ahc_name(ahc), scb_index,
519 ahc->qoutfifonext - 1);
520 continue;
521 }
522
523 /*
524 * Save off the residual
525 * if there is one.
526 */
527 if (scb->hscb->residual_SG_count != 0)
528 ahc_calc_residual(scb);
529 else
530 scb->xs->resid = 0;
531 #ifdef AHC_DEBUG
532 if (ahc_debug & AHC_SHOWSCBS) {
533 scsipi_printaddr(scb->xs->xs_periph);
534 printf("run_qoutfifo: SCB %x complete\n",
535 scb->hscb->tag);
536 }
537 #endif
538 ahc_done(ahc, scb);
539 }
540 }
541
542
543 /*
544 * An scb (and hence an scb entry on the board) is put onto the
545 * free list.
546 */
547 static void
548 ahcfreescb(struct ahc_softc *ahc, struct scb *scb)
549 {
550 struct hardware_scb *hscb;
551 int opri;
552
553 hscb = scb->hscb;
554
555 #ifdef AHC_DEBUG
556 if (ahc_debug & AHC_SHOWSCBALLOC)
557 printf("%s: free SCB tag %x\n", ahc_name(ahc), hscb->tag);
558 #endif
559
560 opri = splbio();
561
562 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 ||
563 (scb->flags & SCB_RECOVERY_SCB) != 0) {
564 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
565 scsipi_channel_thaw(&ahc->sc_channel, 1);
566 if (ahc->features & AHC_TWIN)
567 scsipi_channel_thaw(&ahc->sc_channel_b, 1);
568 }
569
570 /* Clean up for the next user */
571 scb->flags = SCB_FREE;
572 hscb->control = 0;
573 hscb->status = 0;
574
575 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links);
576
577 splx(opri);
578 }
579
580 /*
581 * Get a free scb, either one already assigned to a hardware slot
582 * on the adapter or one that will require an SCB to be paged out before
583 * use. If there are none, see if we can allocate a new SCB. Otherwise
584 * either return an error or sleep.
585 */
586 static __inline struct scb *
587 ahcgetscb(struct ahc_softc *ahc)
588 {
589 struct scb *scbp;
590 int opri;;
591
592 opri = splbio();
593 if ((scbp = SLIST_FIRST(&ahc->scb_data->free_scbs))) {
594 SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links);
595 } else {
596 ahcallocscbs(ahc);
597 scbp = SLIST_FIRST(&ahc->scb_data->free_scbs);
598 if (scbp != NULL)
599 SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links);
600 }
601
602 splx(opri);
603
604 #ifdef AHC_DEBUG
605 if (ahc_debug & AHC_SHOWSCBALLOC) {
606 if (scbp != NULL)
607 printf("%s: new SCB, tag %x\n", ahc_name(ahc),
608 scbp->hscb->tag);
609 else
610 printf("%s: failed to allocate new SCB\n",
611 ahc_name(ahc));
612 }
613 #endif
614
615 return (scbp);
616 }
617
618 static int
619 ahc_createdmamem(tag, size, flags, mapp, vaddr, baddr, seg, nseg, myname, what)
620 bus_dma_tag_t tag;
621 int size;
622 int flags;
623 bus_dmamap_t *mapp;
624 caddr_t *vaddr;
625 bus_addr_t *baddr;
626 bus_dma_segment_t *seg;
627 int *nseg;
628 const char *myname, *what;
629 {
630 int error, level = 0;
631
632 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
633 seg, 1, nseg, BUS_DMA_NOWAIT)) != 0) {
634 printf("%s: failed to allocate DMA mem for %s, error = %d\n",
635 myname, what, error);
636 goto out;
637 }
638 level++;
639
640 if ((error = bus_dmamem_map(tag, seg, *nseg, size, vaddr,
641 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
642 printf("%s: failed to map DMA mem for %s, error = %d\n",
643 myname, what, error);
644 goto out;
645 }
646 level++;
647
648 if ((error = bus_dmamap_create(tag, size, 1, size, 0,
649 BUS_DMA_NOWAIT | flags, mapp)) != 0) {
650 printf("%s: failed to create DMA map for %s, error = %d\n",
651 myname, what, error);
652 goto out;
653 }
654 level++;
655
656 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
657 BUS_DMA_NOWAIT)) != 0) {
658 printf("%s: failed to load DMA map for %s, error = %d\n",
659 myname, what, error);
660 goto out;
661 }
662
663 *baddr = (*mapp)->dm_segs[0].ds_addr;
664
665 #ifdef AHC_DEBUG
666 printf("%s: dmamem for %s at busaddr %lx virt %lx nseg %d size %d\n",
667 myname, what, (unsigned long)*baddr, (unsigned long)*vaddr,
668 *nseg, size);
669 #endif
670
671 return 0;
672 out:
673 switch (level) {
674 case 3:
675 bus_dmamap_destroy(tag, *mapp);
676 /* FALLTHROUGH */
677 case 2:
678 bus_dmamem_unmap(tag, *vaddr, size);
679 /* FALLTHROUGH */
680 case 1:
681 bus_dmamem_free(tag, seg, *nseg);
682 break;
683 default:
684 break;
685 }
686
687 return error;
688 }
689
690 static void
691 ahc_freedmamem(tag, size, map, vaddr, seg, nseg)
692 bus_dma_tag_t tag;
693 int size;
694 bus_dmamap_t map;
695 caddr_t vaddr;
696 bus_dma_segment_t *seg;
697 int nseg;
698 {
699
700 bus_dmamap_unload(tag, map);
701 bus_dmamap_destroy(tag, map);
702 bus_dmamem_unmap(tag, vaddr, size);
703 bus_dmamem_free(tag, seg, nseg);
704 }
705
706 char *
707 ahc_name(struct ahc_softc *ahc)
708 {
709 return (ahc->sc_dev.dv_xname);
710 }
711
712 #ifdef AHC_DEBUG
713 static void
714 ahc_print_scb(struct scb *scb)
715 {
716 struct hardware_scb *hscb = scb->hscb;
717
718 printf("scb:%p tag %x control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n",
719 scb,
720 hscb->tag,
721 hscb->control,
722 hscb->tcl,
723 hscb->cmdlen,
724 (unsigned long)le32toh(hscb->cmdpointer));
725 printf(" datlen:%u data:0x%lx segs:0x%x segp:0x%lx\n",
726 le32toh(hscb->datalen),
727 (unsigned long)(le32toh(hscb->data)),
728 hscb->SG_count,
729 (unsigned long)(le32toh(hscb->SG_pointer)));
730 printf(" sg_addr:%lx sg_len:%lu\n",
731 (unsigned long)(le32toh(scb->sg_list[0].addr)),
732 (unsigned long)(le32toh(scb->sg_list[0].len)));
733 printf(" cdb:%x %x %x %x %x %x %x %x %x %x %x %x\n",
734 hscb->cmdstore[0], hscb->cmdstore[1], hscb->cmdstore[2],
735 hscb->cmdstore[3], hscb->cmdstore[4], hscb->cmdstore[5],
736 hscb->cmdstore[6], hscb->cmdstore[7], hscb->cmdstore[8],
737 hscb->cmdstore[9], hscb->cmdstore[10], hscb->cmdstore[11]);
738 }
739 #endif
740
741 static const struct {
742 u_int8_t errno;
743 const char *errmesg;
744 } hard_error[] = {
745 { ILLHADDR, "Illegal Host Access" },
746 { ILLSADDR, "Illegal Sequencer Address referrenced" },
747 { ILLOPCODE, "Illegal Opcode in sequencer program" },
748 { SQPARERR, "Sequencer Parity Error" },
749 { DPARERR, "Data-path Parity Error" },
750 { MPARERR, "Scratch or SCB Memory Parity Error" },
751 { PCIERRSTAT, "PCI Error detected" },
752 { CIOPARERR, "CIOBUS Parity Error" },
753 };
754 static const int num_errors = sizeof(hard_error)/sizeof(hard_error[0]);
755
756 static const struct {
757 u_int8_t phase;
758 u_int8_t mesg_out; /* Message response to parity errors */
759 const char *phasemsg;
760 } phase_table[] = {
761 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
762 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
763 { P_COMMAND, MSG_NOOP, "in Command phase" },
764 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
765 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
766 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
767 { P_BUSFREE, MSG_NOOP, "while idle" },
768 { 0, MSG_NOOP, "in unknown phase" }
769 };
770 static const int num_phases = (sizeof(phase_table)/sizeof(phase_table[0])) - 1;
771
772 /*
773 * Valid SCSIRATE values. (p. 3-17)
774 * Provides a mapping of tranfer periods in ns to the proper value to
775 * stick in the scsiscfr reg to use that transfer rate.
776 */
777 #define AHC_SYNCRATE_DT 0
778 #define AHC_SYNCRATE_ULTRA2 1
779 #define AHC_SYNCRATE_ULTRA 3
780 #define AHC_SYNCRATE_FAST 6
781 static const struct ahc_syncrate ahc_syncrates[] = {
782 /* ultra2 fast/ultra period rate */
783 { 0x42, 0x000, 9, "80.0" },
784 { 0x03, 0x000, 10, "40.0" },
785 { 0x04, 0x000, 11, "33.0" },
786 { 0x05, 0x100, 12, "20.0" },
787 { 0x06, 0x110, 15, "16.0" },
788 { 0x07, 0x120, 18, "13.4" },
789 { 0x08, 0x000, 25, "10.0" },
790 { 0x19, 0x010, 31, "8.0" },
791 { 0x1a, 0x020, 37, "6.67" },
792 { 0x1b, 0x030, 43, "5.7" },
793 { 0x1c, 0x040, 50, "5.0" },
794 { 0x00, 0x050, 56, "4.4" },
795 { 0x00, 0x060, 62, "4.0" },
796 { 0x00, 0x070, 68, "3.6" },
797 { 0x00, 0x000, 0, NULL }
798 };
799
800 /*
801 * Allocate a controller structure for a new device and initialize it.
802 */
803 int
804 ahc_alloc(struct ahc_softc *ahc, bus_space_handle_t sh, bus_space_tag_t st,
805 bus_dma_tag_t parent_dmat, ahc_chip chip, ahc_feature features,
806 ahc_flag flags)
807 {
808 struct scb_data *scb_data;
809
810 scb_data = malloc(sizeof (struct scb_data), M_DEVBUF, M_NOWAIT);
811 if (scb_data == NULL) {
812 printf("%s: cannot malloc softc!\n", ahc_name(ahc));
813 return -1;
814 }
815 bzero(scb_data, sizeof (struct scb_data));
816 LIST_INIT(&ahc->pending_ccbs);
817 ahc->tag = st;
818 ahc->bsh = sh;
819 ahc->parent_dmat = parent_dmat;
820 ahc->chip = chip;
821 ahc->features = features;
822 ahc->flags = flags;
823 ahc->scb_data = scb_data;
824
825 ahc->unpause = (ahc_inb(ahc, HCNTRL) & IRQMS) | INTEN;
826 /* The IRQMS bit is only valid on VL and EISA chips */
827 if ((ahc->chip & AHC_PCI) != 0)
828 ahc->unpause &= ~IRQMS;
829 ahc->pause = ahc->unpause | PAUSE;
830 return (0);
831 }
832
833 void
834 ahc_free(ahc)
835 struct ahc_softc *ahc;
836 {
837 ahcfiniscbdata(ahc);
838 if (ahc->init_level != 0)
839 ahc_freedmamem(ahc->parent_dmat, ahc->shared_data_size,
840 ahc->shared_data_dmamap, ahc->qoutfifo,
841 &ahc->shared_data_seg, ahc->shared_data_nseg);
842
843 if (ahc->scb_data != NULL)
844 free(ahc->scb_data, M_DEVBUF);
845 if (ahc->bus_data != NULL)
846 free(ahc->bus_data, M_DEVBUF);
847 return;
848 }
849
850 static int
851 ahcinitscbdata(struct ahc_softc *ahc)
852 {
853 struct scb_data *scb_data;
854 int i;
855
856 scb_data = ahc->scb_data;
857 SLIST_INIT(&scb_data->free_scbs);
858 SLIST_INIT(&scb_data->sg_maps);
859
860 /* Allocate SCB resources */
861 scb_data->scbarray =
862 (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX,
863 M_DEVBUF, M_NOWAIT);
864 if (scb_data->scbarray == NULL)
865 return (ENOMEM);
866 bzero(scb_data->scbarray, sizeof(struct scb) * AHC_SCB_MAX);
867
868 /* Determine the number of hardware SCBs and initialize them */
869
870 scb_data->maxhscbs = ahc_probe_scbs(ahc);
871 /* SCB 0 heads the free list */
872 ahc_outb(ahc, FREE_SCBH, 0);
873 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
874 ahc_outb(ahc, SCBPTR, i);
875
876 /* Clear the control byte. */
877 ahc_outb(ahc, SCB_CONTROL, 0);
878
879 /* Set the next pointer */
880 ahc_outb(ahc, SCB_NEXT, i+1);
881
882 /* Make the tag number invalid */
883 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
884 }
885
886 /* Make sure that the last SCB terminates the free list */
887 ahc_outb(ahc, SCBPTR, i-1);
888 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
889
890 /* Ensure we clear the 0 SCB's control byte. */
891 ahc_outb(ahc, SCBPTR, 0);
892 ahc_outb(ahc, SCB_CONTROL, 0);
893
894 scb_data->maxhscbs = i;
895
896 if (ahc->scb_data->maxhscbs == 0)
897 panic("%s: No SCB space found", ahc_name(ahc));
898
899 /*
900 * Create our DMA tags. These tags define the kinds of device
901 * accessible memory allocations and memory mappings we will
902 * need to perform during normal operation.
903 *
904 * Unless we need to further restrict the allocation, we rely
905 * on the restrictions of the parent dmat, hence the common
906 * use of MAXADDR and MAXSIZE.
907 */
908
909 if (ahc_createdmamem(ahc->parent_dmat,
910 AHC_SCB_MAX * sizeof(struct hardware_scb), ahc->sc_dmaflags,
911 &scb_data->hscb_dmamap,
912 (caddr_t *)&scb_data->hscbs, &scb_data->hscb_busaddr,
913 &scb_data->hscb_seg, &scb_data->hscb_nseg, ahc_name(ahc),
914 "hardware SCB structures") < 0)
915 goto error_exit;
916
917 scb_data->init_level++;
918
919 if (ahc_createdmamem(ahc->parent_dmat,
920 AHC_SCB_MAX * sizeof(struct scsipi_sense_data), ahc->sc_dmaflags,
921 &scb_data->sense_dmamap, (caddr_t *)&scb_data->sense,
922 &scb_data->sense_busaddr, &scb_data->sense_seg,
923 &scb_data->sense_nseg, ahc_name(ahc), "sense buffers") < 0)
924 goto error_exit;
925
926 scb_data->init_level++;
927
928 /* Perform initial CCB allocation */
929 bzero(scb_data->hscbs, AHC_SCB_MAX * sizeof(struct hardware_scb));
930 ahcallocscbs(ahc);
931
932 if (scb_data->numscbs == 0) {
933 printf("%s: ahc_init_scb_data - "
934 "Unable to allocate initial scbs\n",
935 ahc_name(ahc));
936 goto error_exit;
937 }
938
939 scb_data->init_level++;
940
941 /*
942 * Note that we were successfull
943 */
944 return 0;
945
946 error_exit:
947
948 return ENOMEM;
949 }
950
951 static void
952 ahcfiniscbdata(struct ahc_softc *ahc)
953 {
954 struct scb_data *scb_data;
955
956 scb_data = ahc->scb_data;
957
958 switch (scb_data->init_level) {
959 default:
960 case 3:
961 {
962 struct sg_map_node *sg_map;
963
964 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
965 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
966 ahc_freedmamem(ahc->parent_dmat, PAGE_SIZE,
967 sg_map->sg_dmamap, (caddr_t)sg_map->sg_vaddr,
968 &sg_map->sg_dmasegs, sg_map->sg_nseg);
969 free(sg_map, M_DEVBUF);
970 }
971 }
972 /*FALLTHROUGH*/
973 case 2:
974 ahc_freedmamem(ahc->parent_dmat,
975 AHC_SCB_MAX * sizeof(struct scsipi_sense_data),
976 scb_data->sense_dmamap, (caddr_t)scb_data->sense,
977 &scb_data->sense_seg, scb_data->sense_nseg);
978 /*FALLTHROUGH*/
979 case 1:
980 ahc_freedmamem(ahc->parent_dmat,
981 AHC_SCB_MAX * sizeof(struct hardware_scb),
982 scb_data->hscb_dmamap, (caddr_t)scb_data->hscbs,
983 &scb_data->hscb_seg, scb_data->hscb_nseg);
984 /*FALLTHROUGH*/
985 }
986 if (scb_data->scbarray != NULL)
987 free(scb_data->scbarray, M_DEVBUF);
988 }
989
990 int
991 ahc_reset(struct ahc_softc *ahc)
992 {
993 u_int sblkctl;
994 int wait;
995
996 #ifdef AHC_DUMP_SEQ
997 if (ahc->init_level == 0)
998 ahc_dumpseq(ahc);
999 #endif
1000 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
1001 /*
1002 * Ensure that the reset has finished
1003 */
1004 wait = 1000;
1005 do {
1006 DELAY(1000);
1007 } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
1008
1009 if (wait == 0) {
1010 printf("%s: WARNING - Failed chip reset! "
1011 "Trying to initialize anyway.\n", ahc_name(ahc));
1012 }
1013 ahc_outb(ahc, HCNTRL, ahc->pause);
1014
1015 /* Determine channel configuration */
1016 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
1017 /* No Twin Channel PCI cards */
1018 if ((ahc->chip & AHC_PCI) != 0)
1019 sblkctl &= ~SELBUSB;
1020 switch (sblkctl) {
1021 case 0:
1022 /* Single Narrow Channel */
1023 break;
1024 case 2:
1025 /* Wide Channel */
1026 ahc->features |= AHC_WIDE;
1027 break;
1028 case 8:
1029 /* Twin Channel */
1030 ahc->features |= AHC_TWIN;
1031 break;
1032 default:
1033 printf(" Unsupported adapter type. Ignoring\n");
1034 return(-1);
1035 }
1036
1037 return (0);
1038 }
1039
1040 /*
1041 * Called when we have an active connection to a target on the bus,
1042 * this function finds the nearest syncrate to the input period limited
1043 * by the capabilities of the bus connectivity of the target.
1044 */
1045 static const struct ahc_syncrate *
1046 ahc_devlimited_syncrate(struct ahc_softc *ahc, u_int *period) {
1047 u_int maxsync;
1048
1049 if ((ahc->features & AHC_ULTRA2) != 0) {
1050 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
1051 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
1052 maxsync = AHC_SYNCRATE_ULTRA2;
1053 } else {
1054 maxsync = AHC_SYNCRATE_ULTRA;
1055 }
1056 } else if ((ahc->features & AHC_ULTRA) != 0) {
1057 maxsync = AHC_SYNCRATE_ULTRA;
1058 } else {
1059 maxsync = AHC_SYNCRATE_FAST;
1060 }
1061 return (ahc_find_syncrate(ahc, period, maxsync));
1062 }
1063
1064 /*
1065 * Look up the valid period to SCSIRATE conversion in our table.
1066 * Return the period and offset that should be sent to the target
1067 * if this was the beginning of an SDTR.
1068 */
1069 static const struct ahc_syncrate *
1070 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period, u_int maxsync)
1071 {
1072 const struct ahc_syncrate *syncrate;
1073
1074 syncrate = &ahc_syncrates[maxsync];
1075 while ((syncrate->rate != NULL)
1076 && ((ahc->features & AHC_ULTRA2) == 0
1077 || (syncrate->sxfr_u2 != 0))) {
1078
1079 if (*period <= syncrate->period) {
1080 /*
1081 * When responding to a target that requests
1082 * sync, the requested rate may fall between
1083 * two rates that we can output, but still be
1084 * a rate that we can receive. Because of this,
1085 * we want to respond to the target with
1086 * the same rate that it sent to us even
1087 * if the period we use to send data to it
1088 * is lower. Only lower the response period
1089 * if we must.
1090 */
1091 if (syncrate == &ahc_syncrates[maxsync])
1092 *period = syncrate->period;
1093 break;
1094 }
1095 syncrate++;
1096 }
1097
1098 if ((*period == 0)
1099 || (syncrate->rate == NULL)
1100 || ((ahc->features & AHC_ULTRA2) != 0
1101 && (syncrate->sxfr_u2 == 0))) {
1102 /* Use asynchronous transfers. */
1103 *period = 0;
1104 syncrate = NULL;
1105 }
1106 return (syncrate);
1107 }
1108
1109 static u_int
1110 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
1111 {
1112 const struct ahc_syncrate *syncrate;
1113
1114 if ((ahc->features & AHC_ULTRA2) != 0)
1115 scsirate &= SXFR_ULTRA2;
1116 else
1117 scsirate &= SXFR;
1118
1119 syncrate = &ahc_syncrates[maxsync];
1120 while (syncrate->rate != NULL) {
1121
1122 if ((ahc->features & AHC_ULTRA2) != 0) {
1123 if (syncrate->sxfr_u2 == 0)
1124 break;
1125 else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
1126 return (syncrate->period);
1127 } else if (scsirate == (syncrate->sxfr & SXFR)) {
1128 return (syncrate->period);
1129 }
1130 syncrate++;
1131 }
1132 return (0); /* async */
1133 }
1134
1135 static void
1136 ahc_validate_offset(struct ahc_softc *ahc, const struct ahc_syncrate *syncrate,
1137 u_int *offset, int wide)
1138 {
1139 u_int maxoffset;
1140
1141 /* Limit offset to what we can do */
1142 if (syncrate == NULL) {
1143 maxoffset = 0;
1144 } else if ((ahc->features & AHC_ULTRA2) != 0) {
1145 maxoffset = MAX_OFFSET_ULTRA2;
1146 } else {
1147 if (wide)
1148 maxoffset = MAX_OFFSET_16BIT;
1149 else
1150 maxoffset = MAX_OFFSET_8BIT;
1151 }
1152 *offset = MIN(*offset, maxoffset);
1153 }
1154
1155 static void
1156 ahc_update_target_msg_request(struct ahc_softc *ahc,
1157 struct ahc_devinfo *devinfo,
1158 struct ahc_initiator_tinfo *tinfo,
1159 int force, int paused)
1160 {
1161 u_int targ_msg_req_orig;
1162
1163 targ_msg_req_orig = ahc->targ_msg_req;
1164 if (tinfo->current.period != tinfo->goal.period
1165 || tinfo->current.width != tinfo->goal.width
1166 || tinfo->current.offset != tinfo->goal.offset
1167 || (force
1168 && (tinfo->goal.period != 0
1169 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT))) {
1170 ahc->targ_msg_req |= devinfo->target_mask;
1171 } else {
1172 ahc->targ_msg_req &= ~devinfo->target_mask;
1173 }
1174
1175 if (ahc->targ_msg_req != targ_msg_req_orig) {
1176 /* Update the message request bit for this target */
1177 if ((ahc->features & AHC_HS_MAILBOX) != 0) {
1178 if (paused) {
1179 ahc_outb(ahc, TARGET_MSG_REQUEST,
1180 ahc->targ_msg_req & 0xFF);
1181 ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
1182 (ahc->targ_msg_req >> 8) & 0xFF);
1183 } else {
1184 ahc_outb(ahc, HS_MAILBOX,
1185 0x01 << HOST_MAILBOX_SHIFT);
1186 }
1187 } else {
1188 if (!paused)
1189 pause_sequencer(ahc);
1190
1191 ahc_outb(ahc, TARGET_MSG_REQUEST,
1192 ahc->targ_msg_req & 0xFF);
1193 ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
1194 (ahc->targ_msg_req >> 8) & 0xFF);
1195
1196 if (!paused)
1197 unpause_sequencer(ahc);
1198 }
1199 }
1200 }
1201
1202 static void
1203 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1204 const struct ahc_syncrate *syncrate,
1205 u_int period, u_int offset, u_int type, int paused, int done)
1206 {
1207 struct ahc_initiator_tinfo *tinfo;
1208 struct tmode_tstate *tstate;
1209 u_int old_period;
1210 u_int old_offset;
1211 int active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
1212
1213 if (syncrate == NULL) {
1214 period = 0;
1215 offset = 0;
1216 }
1217
1218 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1219 devinfo->target, &tstate);
1220 old_period = tinfo->current.period;
1221 old_offset = tinfo->current.offset;
1222
1223 if ((type & AHC_TRANS_CUR) != 0
1224 && (old_period != period || old_offset != offset)) {
1225 u_int scsirate;
1226
1227 scsirate = tinfo->scsirate;
1228 if ((ahc->features & AHC_ULTRA2) != 0) {
1229
1230 /* XXX */
1231 /* Force single edge until DT is fully implemented */
1232 scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
1233 if (syncrate != NULL)
1234 scsirate |= syncrate->sxfr_u2|SINGLE_EDGE;
1235
1236 if (active)
1237 ahc_outb(ahc, SCSIOFFSET, offset);
1238 } else {
1239
1240 scsirate &= ~(SXFR|SOFS);
1241 /*
1242 * Ensure Ultra mode is set properly for
1243 * this target.
1244 */
1245 tstate->ultraenb &= ~devinfo->target_mask;
1246 if (syncrate != NULL) {
1247 if (syncrate->sxfr & ULTRA_SXFR) {
1248 tstate->ultraenb |=
1249 devinfo->target_mask;
1250 }
1251 scsirate |= syncrate->sxfr & SXFR;
1252 scsirate |= offset & SOFS;
1253 }
1254 if (active) {
1255 u_int sxfrctl0;
1256
1257 sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
1258 sxfrctl0 &= ~FAST20;
1259 if (tstate->ultraenb & devinfo->target_mask)
1260 sxfrctl0 |= FAST20;
1261 ahc_outb(ahc, SXFRCTL0, sxfrctl0);
1262 }
1263 }
1264 if (active)
1265 ahc_outb(ahc, SCSIRATE, scsirate);
1266
1267 tinfo->scsirate = scsirate;
1268 tinfo->current.period = period;
1269 tinfo->current.offset = offset;
1270
1271 /* Update the syncrates in any pending scbs */
1272 ahc_update_pending_syncrates(ahc);
1273 }
1274
1275 if ((type & AHC_TRANS_GOAL) != 0) {
1276 tinfo->goal.period = period;
1277 tinfo->goal.offset = offset;
1278 }
1279
1280 if ((type & AHC_TRANS_USER) != 0) {
1281 tinfo->user.period = period;
1282 tinfo->user.offset = offset;
1283 }
1284
1285 ahc_update_target_msg_request(ahc, devinfo, tinfo,
1286 /*force*/FALSE,
1287 paused);
1288 }
1289
1290 static void
1291 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1292 u_int width, u_int type, int paused, int done)
1293 {
1294 struct ahc_initiator_tinfo *tinfo;
1295 struct tmode_tstate *tstate;
1296 u_int oldwidth;
1297 int active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
1298
1299 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1300 devinfo->target, &tstate);
1301 oldwidth = tinfo->current.width;
1302
1303 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
1304 u_int scsirate;
1305
1306 scsirate = tinfo->scsirate;
1307 scsirate &= ~WIDEXFER;
1308 if (width == MSG_EXT_WDTR_BUS_16_BIT)
1309 scsirate |= WIDEXFER;
1310
1311 tinfo->scsirate = scsirate;
1312
1313 if (active)
1314 ahc_outb(ahc, SCSIRATE, scsirate);
1315
1316 tinfo->current.width = width;
1317 }
1318
1319 if ((type & AHC_TRANS_GOAL) != 0)
1320 tinfo->goal.width = width;
1321 if ((type & AHC_TRANS_USER) != 0)
1322 tinfo->user.width = width;
1323
1324 ahc_update_target_msg_request(ahc, devinfo, tinfo,
1325 /*force*/FALSE, paused);
1326 }
1327
1328 static void
1329 ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, int enable)
1330 {
1331 struct ahc_initiator_tinfo *tinfo;
1332 struct tmode_tstate *tstate;
1333
1334 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1335 devinfo->target, &tstate);
1336
1337 if (enable) {
1338 tstate->tagenable |= devinfo->target_mask;
1339 } else {
1340 tstate->tagenable &= ~devinfo->target_mask;
1341 tstate->tagdisable |= devinfo->target_mask;
1342 }
1343 }
1344
1345 static void
1346 ahc_update_xfer_mode(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1347 {
1348 struct scsipi_xfer_mode xm;
1349 struct ahc_initiator_tinfo *tinfo;
1350 struct tmode_tstate *tstate;
1351
1352 if (ahc->inited_targets[devinfo->target] != 2)
1353 return;
1354
1355 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1356 devinfo->target, &tstate);
1357
1358 xm.xm_target = devinfo->target;
1359 xm.xm_mode = 0;
1360 xm.xm_period = tinfo->current.period;
1361 xm.xm_offset = tinfo->current.offset;
1362 if (tinfo->current.width == 1)
1363 xm.xm_mode |= PERIPH_CAP_WIDE16;
1364 if (tinfo->current.period)
1365 xm.xm_mode |= PERIPH_CAP_SYNC;
1366 if (tstate->tagenable & devinfo->target_mask)
1367 xm.xm_mode |= PERIPH_CAP_TQING;
1368 scsipi_async_event(
1369 devinfo->channel == 'B' ? &ahc->sc_channel_b : &ahc->sc_channel,
1370 ASYNC_EVENT_XFER_MODE, &xm);
1371 }
1372
1373 /*
1374 * Attach all the sub-devices we can find
1375 */
1376 int
1377 ahc_attach(struct ahc_softc *ahc)
1378 {
1379 ahc->sc_adapter.adapt_dev = &ahc->sc_dev;
1380 ahc->sc_adapter.adapt_nchannels = (ahc->features & AHC_TWIN) ? 2 : 1;
1381 if (ahc->flags & AHC_PAGESCBS) {
1382 ahc->sc_adapter.adapt_openings = AHC_SCB_MAX;
1383 ahc->sc_adapter.adapt_max_periph = 16;
1384 } else {
1385 ahc->sc_adapter.adapt_openings = ahc->scb_data->maxhscbs;
1386 if (ahc->scb_data->maxhscbs >= 16)
1387 ahc->sc_adapter.adapt_max_periph = 16;
1388 else
1389 ahc->sc_adapter.adapt_max_periph = 4;
1390 }
1391 ahc->sc_adapter.adapt_ioctl = ahc_ioctl;
1392 ahc->sc_adapter.adapt_minphys = ahcminphys;
1393 ahc->sc_adapter.adapt_request = ahc_action;
1394
1395 ahc->sc_channel.chan_adapter = &ahc->sc_adapter;
1396 ahc->sc_channel.chan_bustype = &scsi_bustype;
1397 ahc->sc_channel.chan_channel = 0;
1398 ahc->sc_channel.chan_ntargets = (ahc->features & AHC_WIDE) ? 16 : 8;
1399 ahc->sc_channel.chan_nluns = 8;
1400 ahc->sc_channel.chan_id = ahc->our_id;
1401
1402 if (ahc->features & AHC_TWIN) {
1403 ahc->sc_channel_b = ahc->sc_channel;
1404 ahc->sc_channel_b.chan_id = ahc->our_id_b;
1405 ahc->sc_channel_b.chan_channel = 1;
1406 }
1407
1408 if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) == 0) {
1409 config_found((void *)ahc, &ahc->sc_channel, scsiprint);
1410 if (ahc->features & AHC_TWIN)
1411 config_found((void *)ahc, &ahc->sc_channel_b,
1412 scsiprint);
1413 } else {
1414 config_found((void *)ahc, &ahc->sc_channel_b, scsiprint);
1415 config_found((void *)ahc, &ahc->sc_channel, scsiprint);
1416 }
1417 return 1;
1418 }
1419
1420 static void
1421 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1422 {
1423 u_int saved_tcl;
1424 role_t role;
1425 int our_id;
1426
1427 if (ahc_inb(ahc, SSTAT0) & TARGET)
1428 role = ROLE_TARGET;
1429 else
1430 role = ROLE_INITIATOR;
1431
1432 if (role == ROLE_TARGET
1433 && (ahc->features & AHC_MULTI_TID) != 0
1434 && (ahc_inb(ahc, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
1435 /* We were selected, so pull our id from TARGIDIN */
1436 our_id = ahc_inb(ahc, TARGIDIN) & OID;
1437 } else if ((ahc->features & AHC_ULTRA2) != 0)
1438 our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
1439 else
1440 our_id = ahc_inb(ahc, SCSIID) & OID;
1441
1442 saved_tcl = ahc_inb(ahc, SAVED_TCL);
1443 ahc_compile_devinfo(devinfo, our_id, TCL_TARGET(saved_tcl),
1444 TCL_LUN(saved_tcl), TCL_CHANNEL(ahc, saved_tcl),
1445 role);
1446 }
1447
1448 static void
1449 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
1450 u_int lun, char channel, role_t role)
1451 {
1452 devinfo->our_scsiid = our_id;
1453 devinfo->target = target;
1454 devinfo->lun = lun;
1455 devinfo->target_offset = target;
1456 devinfo->channel = channel;
1457 devinfo->role = role;
1458 if (channel == 'B')
1459 devinfo->target_offset += 8;
1460 devinfo->target_mask = (0x01 << devinfo->target_offset);
1461 }
1462
1463 /*
1464 * Catch an interrupt from the adapter
1465 */
1466 int
1467 ahc_intr(void *arg)
1468 {
1469 struct ahc_softc *ahc;
1470 u_int intstat;
1471
1472 ahc = (struct ahc_softc *)arg;
1473
1474 intstat = ahc_inb(ahc, INTSTAT);
1475
1476 /*
1477 * Any interrupts to process?
1478 */
1479 if ((intstat & INT_PEND) == 0) {
1480 if (ahc->bus_intr && ahc->bus_intr(ahc)) {
1481 #ifdef AHC_DEBUG
1482 printf("%s: bus intr: CCHADDR %x HADDR %x SEQADDR %x\n",
1483 ahc_name(ahc),
1484 ahc_inb(ahc, CCHADDR) |
1485 (ahc_inb(ahc, CCHADDR+1) << 8)
1486 | (ahc_inb(ahc, CCHADDR+2) << 16)
1487 | (ahc_inb(ahc, CCHADDR+3) << 24),
1488 ahc_inb(ahc, HADDR) | (ahc_inb(ahc, HADDR+1) << 8)
1489 | (ahc_inb(ahc, HADDR+2) << 16)
1490 | (ahc_inb(ahc, HADDR+3) << 24),
1491 ahc_inb(ahc, SEQADDR0) |
1492 (ahc_inb(ahc, SEQADDR1) << 8));
1493 #endif
1494 return 1;
1495 }
1496 return 0;
1497 }
1498
1499 #ifdef AHC_DEBUG
1500 if (ahc_debug & AHC_SHOWINTR) {
1501 printf("%s: intstat %x\n", ahc_name(ahc), intstat);
1502 }
1503 #endif
1504
1505 if (intstat & CMDCMPLT) {
1506 ahc_outb(ahc, CLRINT, CLRCMDINT);
1507 ahc_run_qoutfifo(ahc);
1508 }
1509 if (intstat & BRKADRINT) {
1510 /*
1511 * We upset the sequencer :-(
1512 * Lookup the error message
1513 */
1514 int i, error, num_errors;
1515
1516 error = ahc_inb(ahc, ERROR);
1517 num_errors = sizeof(hard_error)/sizeof(hard_error[0]);
1518 for (i = 0; error != 1 && i < num_errors; i++)
1519 error >>= 1;
1520 panic("%s: brkadrint, %s at seqaddr = 0x%x\n",
1521 ahc_name(ahc), hard_error[i].errmesg,
1522 ahc_inb(ahc, SEQADDR0) |
1523 (ahc_inb(ahc, SEQADDR1) << 8));
1524
1525 /* Tell everyone that this HBA is no longer available */
1526 ahc_abort_scbs(ahc, AHC_TARGET_WILDCARD, ALL_CHANNELS,
1527 AHC_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
1528 XS_DRIVER_STUFFUP);
1529 }
1530 if (intstat & SEQINT)
1531 ahc_handle_seqint(ahc, intstat);
1532
1533 if (intstat & SCSIINT)
1534 ahc_handle_scsiint(ahc, intstat);
1535
1536 return 1;
1537 }
1538
1539 static struct tmode_tstate *
1540 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
1541 {
1542 struct tmode_tstate *master_tstate;
1543 struct tmode_tstate *tstate;
1544 int i, s;
1545
1546 master_tstate = ahc->enabled_targets[ahc->our_id];
1547 if (channel == 'B') {
1548 scsi_id += 8;
1549 master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
1550 }
1551 if (ahc->enabled_targets[scsi_id] != NULL
1552 && ahc->enabled_targets[scsi_id] != master_tstate)
1553 panic("%s: ahc_alloc_tstate - Target already allocated",
1554 ahc_name(ahc));
1555 tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
1556 if (tstate == NULL)
1557 return (NULL);
1558
1559 /*
1560 * If we have allocated a master tstate, copy user settings from
1561 * the master tstate (taken from SRAM or the EEPROM) for this
1562 * channel, but reset our current and goal settings to async/narrow
1563 * until an initiator talks to us.
1564 */
1565 if (master_tstate != NULL) {
1566 memcpy(tstate, master_tstate, sizeof(*tstate));
1567 tstate->ultraenb = 0;
1568 for (i = 0; i < 16; i++) {
1569 bzero(&tstate->transinfo[i].current,
1570 sizeof(tstate->transinfo[i].current));
1571 bzero(&tstate->transinfo[i].goal,
1572 sizeof(tstate->transinfo[i].goal));
1573 }
1574 } else
1575 bzero(tstate, sizeof(*tstate));
1576 s = splbio();
1577 ahc->enabled_targets[scsi_id] = tstate;
1578 splx(s);
1579 return (tstate);
1580 }
1581
1582 #if UNUSED
1583 static void
1584 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
1585 {
1586 struct tmode_tstate *tstate;
1587
1588 /* Don't clean up the entry for our initiator role */
1589 if ((ahc->flags & AHC_INITIATORMODE) != 0
1590 && ((channel == 'B' && scsi_id == ahc->our_id_b)
1591 || (channel == 'A' && scsi_id == ahc->our_id))
1592 && force == FALSE)
1593 return;
1594
1595 if (channel == 'B')
1596 scsi_id += 8;
1597 tstate = ahc->enabled_targets[scsi_id];
1598 if (tstate != NULL)
1599 free(tstate, M_DEVBUF);
1600 ahc->enabled_targets[scsi_id] = NULL;
1601 }
1602 #endif
1603
1604 static void
1605 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
1606 {
1607 struct scb *scb;
1608 struct ahc_devinfo devinfo;
1609
1610 ahc_fetch_devinfo(ahc, &devinfo);
1611
1612 /*
1613 * Clear the upper byte that holds SEQINT status
1614 * codes and clear the SEQINT bit. We will unpause
1615 * the sequencer, if appropriate, after servicing
1616 * the request.
1617 */
1618 ahc_outb(ahc, CLRINT, CLRSEQINT);
1619 switch (intstat & SEQINT_MASK) {
1620 case NO_MATCH:
1621 {
1622 /* Ensure we don't leave the selection hardware on */
1623 ahc_outb(ahc, SCSISEQ,
1624 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
1625
1626 printf("%s:%c:%d: no active SCB for reconnecting "
1627 "target - issuing BUS DEVICE RESET\n",
1628 ahc_name(ahc), devinfo.channel, devinfo.target);
1629 printf("SAVED_TCL == 0x%x, ARG_1 == 0x%x, SEQ_FLAGS == 0x%x\n",
1630 ahc_inb(ahc, SAVED_TCL), ahc_inb(ahc, ARG_1),
1631 ahc_inb(ahc, SEQ_FLAGS));
1632 ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
1633 ahc->msgout_len = 1;
1634 ahc->msgout_index = 0;
1635 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
1636 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1637 ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, LASTPHASE) | ATNO);
1638 break;
1639 }
1640 case UPDATE_TMSG_REQ:
1641 ahc_outb(ahc, TARGET_MSG_REQUEST, ahc->targ_msg_req & 0xFF);
1642 ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
1643 (ahc->targ_msg_req >> 8) & 0xFF);
1644 ahc_outb(ahc, HS_MAILBOX, 0);
1645 break;
1646 case SEND_REJECT:
1647 {
1648 u_int rejbyte = ahc_inb(ahc, ACCUM);
1649 printf("%s:%c:%d: Warning - unknown message received from "
1650 "target (0x%x). Rejecting\n",
1651 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
1652 break;
1653 }
1654 case NO_IDENT:
1655 {
1656 /*
1657 * The reconnecting target either did not send an identify
1658 * message, or did, but we didn't find and SCB to match and
1659 * before it could respond to our ATN/abort, it hit a dataphase.
1660 * The only safe thing to do is to blow it away with a bus
1661 * reset.
1662 */
1663 int found;
1664
1665 printf("%s:%c:%d: Target did not send an IDENTIFY message. "
1666 "LASTPHASE = 0x%x, SAVED_TCL == 0x%x\n",
1667 ahc_name(ahc), devinfo.channel, devinfo.target,
1668 ahc_inb(ahc, LASTPHASE), ahc_inb(ahc, SAVED_TCL));
1669 found = ahc_reset_channel(ahc, devinfo.channel,
1670 /*initiate reset*/TRUE);
1671 printf("%s: Issued Channel %c Bus Reset. "
1672 "%d SCBs aborted\n", ahc_name(ahc), devinfo.channel,
1673 found);
1674 return;
1675 }
1676 case BAD_PHASE:
1677 {
1678 u_int lastphase;
1679
1680 lastphase = ahc_inb(ahc, LASTPHASE);
1681 if (lastphase == P_BUSFREE) {
1682 printf("%s:%c:%d: Missed busfree. Curphase = 0x%x\n",
1683 ahc_name(ahc), devinfo.channel, devinfo.target,
1684 ahc_inb(ahc, SCSISIGI));
1685 restart_sequencer(ahc);
1686 return;
1687 } else {
1688 printf("%s:%c:%d: unknown scsi bus phase %x. "
1689 "Attempting to continue\n",
1690 ahc_name(ahc), devinfo.channel, devinfo.target,
1691 ahc_inb(ahc, SCSISIGI));
1692 }
1693 break;
1694 }
1695 case BAD_STATUS:
1696 {
1697 u_int scb_index;
1698 struct hardware_scb *hscb;
1699 struct scsipi_xfer *xs;
1700 /*
1701 * The sequencer will notify us when a command
1702 * has an error that would be of interest to
1703 * the kernel. This allows us to leave the sequencer
1704 * running in the common case of command completes
1705 * without error. The sequencer will already have
1706 * dma'd the SCB back up to us, so we can reference
1707 * the in kernel copy directly.
1708 */
1709 scb_index = ahc_inb(ahc, SCB_TAG);
1710 scb = &ahc->scb_data->scbarray[scb_index];
1711
1712 /* ahc_print_scb(scb); */
1713
1714 /*
1715 * Set the default return value to 0 (don't
1716 * send sense). The sense code will change
1717 * this if needed.
1718 */
1719 ahc_outb(ahc, RETURN_1, 0);
1720 if (!(scb_index < ahc->scb_data->numscbs
1721 && (scb->flags & SCB_ACTIVE) != 0)) {
1722 printf("%s:%c:%d: ahc_intr - referenced scb "
1723 "not valid during seqint 0x%x scb(%d)\n",
1724 ahc_name(ahc), devinfo.channel,
1725 devinfo.target, intstat, scb_index);
1726 goto unpause;
1727 }
1728
1729 hscb = scb->hscb;
1730 xs = scb->xs;
1731
1732 /* Don't want to clobber the original sense code */
1733 if ((scb->flags & SCB_SENSE) != 0) {
1734 /*
1735 * Clear the SCB_SENSE Flag and have
1736 * the sequencer do a normal command
1737 * complete.
1738 */
1739 scb->flags &= ~SCB_SENSE;
1740 ahcsetccbstatus(xs, XS_DRIVER_STUFFUP);
1741 break;
1742 }
1743 /* Freeze the queue unit the client sees the error. */
1744 ahc_freeze_devq(ahc, xs->xs_periph);
1745 ahc_freeze_ccb(scb);
1746 xs->status = hscb->status;
1747 switch (hscb->status) {
1748 case SCSI_STATUS_OK:
1749 printf("%s: Interrupted for status of 0???\n",
1750 ahc_name(ahc));
1751 break;
1752 case SCSI_STATUS_CMD_TERMINATED:
1753 case SCSI_STATUS_CHECK_COND:
1754 #if defined(AHC_DEBUG)
1755 if (ahc_debug & AHC_SHOWSENSE) {
1756 scsipi_printaddr(xs->xs_periph);
1757 printf("Check Status, resid %d datalen %d\n",
1758 xs->resid, xs->datalen);
1759 }
1760 #endif
1761
1762 if (xs->error == XS_NOERROR &&
1763 !(scb->flags & SCB_SENSE)) {
1764 struct ahc_dma_seg *sg;
1765 struct scsipi_sense *sc;
1766 struct ahc_initiator_tinfo *tinfo;
1767 struct tmode_tstate *tstate;
1768
1769 sg = scb->sg_list;
1770 sc = (struct scsipi_sense *)(&hscb->cmdstore);
1771 /*
1772 * Save off the residual if there is one.
1773 */
1774 if (hscb->residual_SG_count != 0)
1775 ahc_calc_residual(scb);
1776 else
1777 xs->resid = 0;
1778
1779 #ifdef AHC_DEBUG
1780 if (ahc_debug & AHC_SHOWSENSE) {
1781 scsipi_printaddr(xs->xs_periph);
1782 printf("Sending Sense\n");
1783 }
1784 #endif
1785 sg->addr = ahc->scb_data->sense_busaddr +
1786 (hscb->tag*sizeof(struct scsipi_sense_data));
1787 sg->len = sizeof (struct scsipi_sense_data);
1788
1789 sc->opcode = REQUEST_SENSE;
1790 sc->byte2 = SCB_LUN(scb) << 5;
1791 sc->unused[0] = 0;
1792 sc->unused[1] = 0;
1793 sc->length = sg->len;
1794 sc->control = 0;
1795
1796 /*
1797 * Would be nice to preserve DISCENB here,
1798 * but due to the way we page SCBs, we can't.
1799 */
1800 hscb->control = 0;
1801
1802 /*
1803 * This request sense could be because the
1804 * the device lost power or in some other
1805 * way has lost our transfer negotiations.
1806 * Renegotiate if appropriate. Unit attention
1807 * errors will be reported before any data
1808 * phases occur.
1809 */
1810 ahc_calc_residual(scb);
1811 #if defined(AHC_DEBUG)
1812 if (ahc_debug & AHC_SHOWSENSE) {
1813 scsipi_printaddr(xs->xs_periph);
1814 printf("Sense: datalen %d resid %d"
1815 "chan %d id %d targ %d\n",
1816 xs->datalen, xs->resid,
1817 devinfo.channel, devinfo.our_scsiid,
1818 devinfo.target);
1819 }
1820 #endif
1821 if (xs->datalen > 0 &&
1822 xs->resid == xs->datalen) {
1823 tinfo = ahc_fetch_transinfo(ahc,
1824 devinfo.channel,
1825 devinfo.our_scsiid,
1826 devinfo.target,
1827 &tstate);
1828 ahc_update_target_msg_request(ahc,
1829 &devinfo,
1830 tinfo,
1831 /*force*/TRUE,
1832 /*paused*/TRUE);
1833 }
1834 hscb->status = 0;
1835 hscb->SG_count = 1;
1836 hscb->SG_pointer = scb->sg_list_phys;
1837 hscb->data = sg->addr;
1838 hscb->datalen = sg->len;
1839 hscb->cmdpointer = hscb->cmdstore_busaddr;
1840 hscb->cmdlen = sizeof(*sc);
1841 scb->sg_count = hscb->SG_count;
1842 ahc_swap_hscb(hscb);
1843 ahc_swap_sg(scb->sg_list);
1844 scb->flags |= SCB_SENSE;
1845 /*
1846 * Ensure the target is busy since this
1847 * will be an untagged request.
1848 */
1849 ahc_busy_tcl(ahc, scb);
1850 ahc_outb(ahc, RETURN_1, SEND_SENSE);
1851
1852 /*
1853 * Ensure we have enough time to actually
1854 * retrieve the sense.
1855 */
1856 if (!(scb->xs->xs_control & XS_CTL_POLL)) {
1857 callout_reset(&scb->xs->xs_callout,
1858 5 * hz, ahc_timeout, scb);
1859 }
1860 }
1861 break;
1862 case SCSI_STATUS_QUEUE_FULL:
1863 case SCSI_STATUS_BUSY:
1864 xs->error = XS_BUSY;
1865 break;
1866 }
1867 break;
1868 }
1869 case TRACE_POINT:
1870 {
1871 printf("SSTAT2 = 0x%x DFCNTRL = 0x%x\n", ahc_inb(ahc, SSTAT2),
1872 ahc_inb(ahc, DFCNTRL));
1873 printf("SSTAT3 = 0x%x DSTATUS = 0x%x\n", ahc_inb(ahc, SSTAT3),
1874 ahc_inb(ahc, DFSTATUS));
1875 printf("SSTAT0 = 0x%x, SCB_DATACNT = 0x%x\n",
1876 ahc_inb(ahc, SSTAT0),
1877 ahc_inb(ahc, SCB_DATACNT));
1878 break;
1879 }
1880 case HOST_MSG_LOOP:
1881 {
1882 /*
1883 * The sequencer has encountered a message phase
1884 * that requires host assistance for completion.
1885 * While handling the message phase(s), we will be
1886 * notified by the sequencer after each byte is
1887 * transfered so we can track bus phases.
1888 *
1889 * If this is the first time we've seen a HOST_MSG_LOOP,
1890 * initialize the state of the host message loop.
1891 */
1892 if (ahc->msg_type == MSG_TYPE_NONE) {
1893 u_int bus_phase;
1894
1895 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1896 if (bus_phase != P_MESGIN
1897 && bus_phase != P_MESGOUT) {
1898 printf("ahc_intr: HOST_MSG_LOOP bad "
1899 "phase 0x%x\n",
1900 bus_phase);
1901 /*
1902 * Probably transitioned to bus free before
1903 * we got here. Just punt the message.
1904 */
1905 ahc_clear_intstat(ahc);
1906 restart_sequencer(ahc);
1907 }
1908
1909 if (devinfo.role == ROLE_INITIATOR) {
1910 struct scb *scb;
1911 u_int scb_index;
1912
1913 scb_index = ahc_inb(ahc, SCB_TAG);
1914 scb = &ahc->scb_data->scbarray[scb_index];
1915
1916 if (bus_phase == P_MESGOUT)
1917 ahc_setup_initiator_msgout(ahc,
1918 &devinfo,
1919 scb);
1920 else {
1921 ahc->msg_type =
1922 MSG_TYPE_INITIATOR_MSGIN;
1923 ahc->msgin_index = 0;
1924 }
1925 } else {
1926 if (bus_phase == P_MESGOUT) {
1927 ahc->msg_type =
1928 MSG_TYPE_TARGET_MSGOUT;
1929 ahc->msgin_index = 0;
1930 } else
1931 /* XXX Ever executed??? */
1932 ahc_setup_target_msgin(ahc, &devinfo);
1933 }
1934 }
1935
1936 /* Pass a NULL path so that handlers generate their own */
1937 ahc_handle_message_phase(ahc, /*path*/NULL);
1938 break;
1939 }
1940 case PERR_DETECTED:
1941 {
1942 /*
1943 * If we've cleared the parity error interrupt
1944 * but the sequencer still believes that SCSIPERR
1945 * is true, it must be that the parity error is
1946 * for the currently presented byte on the bus,
1947 * and we are not in a phase (data-in) where we will
1948 * eventually ack this byte. Ack the byte and
1949 * throw it away in the hope that the target will
1950 * take us to message out to deliver the appropriate
1951 * error message.
1952 */
1953 if ((intstat & SCSIINT) == 0
1954 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
1955 u_int curphase;
1956
1957 /*
1958 * The hardware will only let you ack bytes
1959 * if the expected phase in SCSISIGO matches
1960 * the current phase. Make sure this is
1961 * currently the case.
1962 */
1963 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1964 ahc_outb(ahc, LASTPHASE, curphase);
1965 ahc_outb(ahc, SCSISIGO, curphase);
1966 ahc_inb(ahc, SCSIDATL);
1967 }
1968 break;
1969 }
1970 case DATA_OVERRUN:
1971 {
1972 /*
1973 * When the sequencer detects an overrun, it
1974 * places the controller in "BITBUCKET" mode
1975 * and allows the target to complete its transfer.
1976 * Unfortunately, none of the counters get updated
1977 * when the controller is in this mode, so we have
1978 * no way of knowing how large the overrun was.
1979 */
1980 u_int scbindex = ahc_inb(ahc, SCB_TAG);
1981 u_int lastphase = ahc_inb(ahc, LASTPHASE);
1982 int i;
1983
1984 scb = &ahc->scb_data->scbarray[scbindex];
1985 for (i = 0; i < num_phases; i++) {
1986 if (lastphase == phase_table[i].phase)
1987 break;
1988 }
1989 scsipi_printaddr(scb->xs->xs_periph);
1990 printf("data overrun detected %s."
1991 " Tag == 0x%x.\n",
1992 phase_table[i].phasemsg,
1993 scb->hscb->tag);
1994 scsipi_printaddr(scb->xs->xs_periph);
1995 printf("%s seen Data Phase. Length = %d. NumSGs = %d.\n",
1996 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
1997 scb->xs->datalen, scb->sg_count);
1998 if (scb->sg_count > 0) {
1999 for (i = 0; i < scb->sg_count; i++) {
2000 printf("sg[%d] - Addr 0x%x : Length %d\n",
2001 i,
2002 le32toh(scb->sg_list[i].addr),
2003 le32toh(scb->sg_list[i].len));
2004 }
2005 }
2006 /*
2007 * Set this and it will take affect when the
2008 * target does a command complete.
2009 */
2010 ahc_freeze_devq(ahc, scb->xs->xs_periph);
2011 ahcsetccbstatus(scb->xs, XS_DRIVER_STUFFUP);
2012 ahc_freeze_ccb(scb);
2013 break;
2014 }
2015 case TRACEPOINT:
2016 {
2017 printf("TRACEPOINT: RETURN_1 = %d\n", ahc_inb(ahc, RETURN_1));
2018 printf("TRACEPOINT: RETURN_2 = %d\n", ahc_inb(ahc, RETURN_2));
2019 printf("TRACEPOINT: ARG_1 = %d\n", ahc_inb(ahc, ARG_1));
2020 printf("TRACEPOINT: ARG_2 = %d\n", ahc_inb(ahc, ARG_2));
2021 printf("TRACEPOINT: CCHADDR = %x\n",
2022 ahc_inb(ahc, CCHADDR) | (ahc_inb(ahc, CCHADDR+1) << 8)
2023 | (ahc_inb(ahc, CCHADDR+2) << 16)
2024 | (ahc_inb(ahc, CCHADDR+3) << 24));
2025 #if 0
2026 printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1));
2027 printf("SSTAT0 == 0x%x\n", ahc_inb(ahc, SSTAT0));
2028 printf(", SCSISIGI == 0x%x\n", ahc_inb(ahc, SCSISIGI));
2029 printf("TRACEPOINT: CCHCNT = %d, SG_COUNT = %d\n",
2030 ahc_inb(ahc, CCHCNT), ahc_inb(ahc, SG_COUNT));
2031 printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG));
2032 printf("TRACEPOINT1: CCHADDR = %d, CCHCNT = %d, SCBPTR = %d\n",
2033 ahc_inb(ahc, CCHADDR)
2034 | (ahc_inb(ahc, CCHADDR+1) << 8)
2035 | (ahc_inb(ahc, CCHADDR+2) << 16)
2036 | (ahc_inb(ahc, CCHADDR+3) << 24),
2037 ahc_inb(ahc, CCHCNT)
2038 | (ahc_inb(ahc, CCHCNT+1) << 8)
2039 | (ahc_inb(ahc, CCHCNT+2) << 16),
2040 ahc_inb(ahc, SCBPTR));
2041 printf("TRACEPOINT: WAITING_SCBH = %d\n", ahc_inb(ahc, WAITING_SCBH));
2042 printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG));
2043 #if DDB > 0
2044 cpu_Debugger();
2045 #endif
2046 #endif
2047 break;
2048 }
2049 #if NOT_YET
2050 /* XXX Fill these in later */
2051 case MESG_BUFFER_BUSY:
2052 break;
2053 case MSGIN_PHASEMIS:
2054 break;
2055 #endif
2056 default:
2057 printf("ahc_intr: seqint, "
2058 "intstat == 0x%x, scsisigi = 0x%x\n",
2059 intstat, ahc_inb(ahc, SCSISIGI));
2060 break;
2061 }
2062
2063 unpause:
2064 /*
2065 * The sequencer is paused immediately on
2066 * a SEQINT, so we should restart it when
2067 * we're done.
2068 */
2069 unpause_sequencer(ahc);
2070 }
2071
2072 static void
2073 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
2074 {
2075 u_int scb_index;
2076 u_int status;
2077 struct scb *scb;
2078 char cur_channel;
2079 char intr_channel;
2080
2081 if ((ahc->features & AHC_TWIN) != 0
2082 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
2083 cur_channel = 'B';
2084 else
2085 cur_channel = 'A';
2086 intr_channel = cur_channel;
2087
2088 status = ahc_inb(ahc, SSTAT1);
2089 if (status == 0) {
2090 if ((ahc->features & AHC_TWIN) != 0) {
2091 /* Try the other channel */
2092 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
2093 status = ahc_inb(ahc, SSTAT1);
2094 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
2095 intr_channel = (cur_channel == 'A') ? 'B' : 'A';
2096 }
2097 if (status == 0) {
2098 printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
2099 return;
2100 }
2101 }
2102
2103 scb_index = ahc_inb(ahc, SCB_TAG);
2104 if (scb_index < ahc->scb_data->numscbs) {
2105 scb = &ahc->scb_data->scbarray[scb_index];
2106 if ((scb->flags & SCB_ACTIVE) == 0
2107 || (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) == 0)
2108 scb = NULL;
2109 } else
2110 scb = NULL;
2111
2112 if ((status & SCSIRSTI) != 0) {
2113 printf("%s: Someone reset channel %c\n",
2114 ahc_name(ahc), intr_channel);
2115 ahc_reset_channel(ahc, intr_channel, /* Initiate Reset */FALSE);
2116 } else if ((status & SCSIPERR) != 0) {
2117 /*
2118 * Determine the bus phase and queue an appropriate message.
2119 * SCSIPERR is latched true as soon as a parity error
2120 * occurs. If the sequencer acked the transfer that
2121 * caused the parity error and the currently presented
2122 * transfer on the bus has correct parity, SCSIPERR will
2123 * be cleared by CLRSCSIPERR. Use this to determine if
2124 * we should look at the last phase the sequencer recorded,
2125 * or the current phase presented on the bus.
2126 */
2127 u_int mesg_out;
2128 u_int curphase;
2129 u_int errorphase;
2130 u_int lastphase;
2131 int i;
2132
2133 lastphase = ahc_inb(ahc, LASTPHASE);
2134 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2135 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
2136 /*
2137 * For all phases save DATA, the sequencer won't
2138 * automatically ack a byte that has a parity error
2139 * in it. So the only way that the current phase
2140 * could be 'data-in' is if the parity error is for
2141 * an already acked byte in the data phase. During
2142 * synchronous data-in transfers, we may actually
2143 * ack bytes before latching the current phase in
2144 * LASTPHASE, leading to the discrepancy between
2145 * curphase and lastphase.
2146 */
2147 if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
2148 || curphase == P_DATAIN)
2149 errorphase = curphase;
2150 else
2151 errorphase = lastphase;
2152
2153 for (i = 0; i < num_phases; i++) {
2154 if (errorphase == phase_table[i].phase)
2155 break;
2156 }
2157 mesg_out = phase_table[i].mesg_out;
2158 if (scb != NULL)
2159 scsipi_printaddr(scb->xs->xs_periph);
2160 else
2161 printf("%s:%c:%d: ", ahc_name(ahc),
2162 intr_channel,
2163 TCL_TARGET(ahc_inb(ahc, SAVED_TCL)));
2164
2165 printf("parity error detected %s. "
2166 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
2167 phase_table[i].phasemsg,
2168 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8),
2169 ahc_inb(ahc, SCSIRATE));
2170
2171 /*
2172 * We've set the hardware to assert ATN if we
2173 * get a parity error on "in" phases, so all we
2174 * need to do is stuff the message buffer with
2175 * the appropriate message. "In" phases have set
2176 * mesg_out to something other than MSG_NOP.
2177 */
2178 if (mesg_out != MSG_NOOP) {
2179 if (ahc->msg_type != MSG_TYPE_NONE)
2180 ahc->send_msg_perror = TRUE;
2181 else
2182 ahc_outb(ahc, MSG_OUT, mesg_out);
2183 }
2184 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2185 unpause_sequencer(ahc);
2186 } else if ((status & BUSFREE) != 0
2187 && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
2188 /*
2189 * First look at what phase we were last in.
2190 * If its message out, chances are pretty good
2191 * that the busfree was in response to one of
2192 * our abort requests.
2193 */
2194 u_int lastphase = ahc_inb(ahc, LASTPHASE);
2195 u_int saved_tcl = ahc_inb(ahc, SAVED_TCL);
2196 u_int target = TCL_TARGET(saved_tcl);
2197 u_int initiator_role_id = TCL_SCSI_ID(ahc, saved_tcl);
2198 char channel = TCL_CHANNEL(ahc, saved_tcl);
2199 int printerror = 1;
2200
2201 ahc_outb(ahc, SCSISEQ,
2202 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
2203 if (lastphase == P_MESGOUT) {
2204 u_int message;
2205 u_int tag;
2206
2207 message = ahc->msgout_buf[ahc->msgout_index - 1];
2208 tag = SCB_LIST_NULL;
2209 switch (message) {
2210 case MSG_ABORT_TAG:
2211 tag = scb->hscb->tag;
2212 /* FALLTRHOUGH */
2213 case MSG_ABORT:
2214 scsipi_printaddr(scb->xs->xs_periph);
2215 printf("SCB %x - Abort %s Completed.\n",
2216 scb->hscb->tag, tag == SCB_LIST_NULL ?
2217 "" : "Tag");
2218 ahc_abort_scbs(ahc, target, channel,
2219 TCL_LUN(saved_tcl), tag,
2220 ROLE_INITIATOR,
2221 XS_DRIVER_STUFFUP);
2222 printerror = 0;
2223 break;
2224 case MSG_BUS_DEV_RESET:
2225 {
2226 struct ahc_devinfo devinfo;
2227
2228 if (scb != NULL &&
2229 (scb->xs->xs_control & XS_CTL_RESET)
2230 && ahc_match_scb(scb, target, channel,
2231 TCL_LUN(saved_tcl),
2232 SCB_LIST_NULL,
2233 ROLE_INITIATOR)) {
2234 ahcsetccbstatus(scb->xs, XS_NOERROR);
2235 }
2236 ahc_compile_devinfo(&devinfo,
2237 initiator_role_id,
2238 target,
2239 TCL_LUN(saved_tcl),
2240 channel,
2241 ROLE_INITIATOR);
2242 ahc_handle_devreset(ahc, &devinfo,
2243 XS_RESET,
2244 "Bus Device Reset",
2245 /*verbose_level*/0);
2246 printerror = 0;
2247 break;
2248 }
2249 default:
2250 break;
2251 }
2252 }
2253 if (printerror != 0) {
2254 int i;
2255
2256 if (scb != NULL) {
2257 u_int tag;
2258
2259 if ((scb->hscb->control & TAG_ENB) != 0)
2260 tag = scb->hscb->tag;
2261 else
2262 tag = SCB_LIST_NULL;
2263 ahc_abort_scbs(ahc, target, channel,
2264 SCB_LUN(scb), tag,
2265 ROLE_INITIATOR,
2266 XS_DRIVER_STUFFUP);
2267 scsipi_printaddr(scb->xs->xs_periph);
2268 } else {
2269 /*
2270 * We had not fully identified this connection,
2271 * so we cannot abort anything.
2272 */
2273 printf("%s: ", ahc_name(ahc));
2274 }
2275 for (i = 0; i < num_phases; i++) {
2276 if (lastphase == phase_table[i].phase)
2277 break;
2278 }
2279 printf("Unexpected busfree %s\n"
2280 "SEQADDR == 0x%x\n",
2281 phase_table[i].phasemsg, ahc_inb(ahc, SEQADDR0)
2282 | (ahc_inb(ahc, SEQADDR1) << 8));
2283 }
2284 ahc_clear_msg_state(ahc);
2285 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
2286 ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
2287 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2288 restart_sequencer(ahc);
2289 } else if ((status & SELTO) != 0) {
2290 u_int scbptr;
2291
2292 scbptr = ahc_inb(ahc, WAITING_SCBH);
2293 ahc_outb(ahc, SCBPTR, scbptr);
2294 scb_index = ahc_inb(ahc, SCB_TAG);
2295
2296 if (scb_index < ahc->scb_data->numscbs) {
2297 scb = &ahc->scb_data->scbarray[scb_index];
2298 if ((scb->flags & SCB_ACTIVE) == 0)
2299 scb = NULL;
2300 } else
2301 scb = NULL;
2302
2303 if (scb == NULL) {
2304 printf("%s: ahc_intr - referenced scb not "
2305 "valid during SELTO scb(%d, %d)\n",
2306 ahc_name(ahc), scbptr, scb_index);
2307 } else {
2308 u_int tag;
2309
2310 tag = SCB_LIST_NULL;
2311 if ((scb->hscb->control & TAG_ENB) != 0)
2312 tag = scb->hscb->tag;
2313
2314 ahc_abort_scbs(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
2315 SCB_LUN(scb), tag,
2316 ROLE_INITIATOR, XS_SELTIMEOUT);
2317 }
2318 /* Stop the selection */
2319 ahc_outb(ahc, SCSISEQ, 0);
2320
2321 /* No more pending messages */
2322 ahc_clear_msg_state(ahc);
2323
2324 /*
2325 * Although the driver does not care about the
2326 * 'Selection in Progress' status bit, the busy
2327 * LED does. SELINGO is only cleared by a sucessful
2328 * selection, so we must manually clear it to ensure
2329 * the LED turns off just incase no future successful
2330 * selections occur (e.g. no devices on the bus).
2331 */
2332 ahc_outb(ahc, CLRSINT0, CLRSELINGO);
2333
2334 /* Clear interrupt state */
2335 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
2336 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2337 restart_sequencer(ahc);
2338 } else {
2339 scsipi_printaddr(scb->xs->xs_periph);
2340 printf("Unknown SCSIINT. Status = 0x%x\n", status);
2341 ahc_outb(ahc, CLRSINT1, status);
2342 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2343 unpause_sequencer(ahc);
2344 }
2345 }
2346
2347 static void
2348 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2349 {
2350 /*
2351 * We need to initiate transfer negotiations.
2352 * If our current and goal settings are identical,
2353 * we want to renegotiate due to a check condition.
2354 */
2355 struct ahc_initiator_tinfo *tinfo;
2356 struct tmode_tstate *tstate;
2357 int dowide;
2358 int dosync;
2359
2360 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2361 devinfo->target, &tstate);
2362 dowide = tinfo->current.width != tinfo->goal.width;
2363 dosync = tinfo->current.period != tinfo->goal.period;
2364
2365 if (!dowide && !dosync) {
2366 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
2367 dosync = tinfo->goal.period != 0;
2368 }
2369
2370 if (dowide) {
2371 ahc_construct_wdtr(ahc, tinfo->goal.width);
2372 } else if (dosync) {
2373 const struct ahc_syncrate *rate;
2374 u_int period;
2375 u_int offset;
2376
2377 period = tinfo->goal.period;
2378 rate = ahc_devlimited_syncrate(ahc, &period);
2379 offset = tinfo->goal.offset;
2380 ahc_validate_offset(ahc, rate, &offset,
2381 tinfo->current.width);
2382 ahc_construct_sdtr(ahc, period, offset);
2383 } else {
2384 panic("ahc_intr: AWAITING_MSG for negotiation, "
2385 "but no negotiation needed\n");
2386 }
2387 }
2388
2389 static void
2390 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2391 struct scb *scb)
2392 {
2393 /*
2394 * To facilitate adding multiple messages together,
2395 * each routine should increment the index and len
2396 * variables instead of setting them explicitly.
2397 */
2398 ahc->msgout_index = 0;
2399 ahc->msgout_len = 0;
2400
2401 if ((scb->flags & SCB_DEVICE_RESET) == 0
2402 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
2403 u_int identify_msg;
2404
2405 identify_msg = MSG_IDENTIFYFLAG | SCB_LUN(scb);
2406 if ((scb->hscb->control & DISCENB) != 0)
2407 identify_msg |= MSG_IDENTIFY_DISCFLAG;
2408 ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
2409 ahc->msgout_len++;
2410
2411 if ((scb->hscb->control & TAG_ENB) != 0) {
2412 ahc->msgout_buf[ahc->msgout_index++] =
2413 scb->xs->xs_tag_type;
2414 ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
2415 ahc->msgout_len += 2;
2416 }
2417 }
2418
2419 if (scb->flags & SCB_DEVICE_RESET) {
2420 ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
2421 ahc->msgout_len++;
2422 scsipi_printaddr(scb->xs->xs_periph);
2423 printf("Bus Device Reset Message Sent\n");
2424 } else if (scb->flags & SCB_ABORT) {
2425 if ((scb->hscb->control & TAG_ENB) != 0)
2426 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
2427 else
2428 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
2429 ahc->msgout_len++;
2430 scsipi_printaddr(scb->xs->xs_periph);
2431 printf("Abort Message Sent\n");
2432 } else if ((ahc->targ_msg_req & devinfo->target_mask) != 0) {
2433 ahc_build_transfer_msg(ahc, devinfo);
2434 } else {
2435 printf("ahc_intr: AWAITING_MSG for an SCB that "
2436 "does not have a waiting message");
2437 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2438 "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
2439 ahc_inb(ahc, MSG_OUT), scb->flags);
2440 }
2441
2442 /*
2443 * Clear the MK_MESSAGE flag from the SCB so we aren't
2444 * asked to send this message again.
2445 */
2446 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
2447 ahc->msgout_index = 0;
2448 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2449 }
2450
2451 static void
2452 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2453 {
2454 /*
2455 * To facilitate adding multiple messages together,
2456 * each routine should increment the index and len
2457 * variables instead of setting them explicitly.
2458 */
2459 ahc->msgout_index = 0;
2460 ahc->msgout_len = 0;
2461
2462 if ((ahc->targ_msg_req & devinfo->target_mask) != 0)
2463 ahc_build_transfer_msg(ahc, devinfo);
2464 else
2465 panic("ahc_intr: AWAITING target message with no message");
2466
2467 ahc->msgout_index = 0;
2468 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
2469 }
2470
2471 static int
2472 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2473 {
2474 /*
2475 * What we care about here is if we had an
2476 * outstanding SDTR or WDTR message for this
2477 * target. If we did, this is a signal that
2478 * the target is refusing negotiation.
2479 */
2480 struct scb *scb;
2481 u_int scb_index;
2482 u_int last_msg;
2483 int response = 0;
2484
2485 scb_index = ahc_inb(ahc, SCB_TAG);
2486 scb = &ahc->scb_data->scbarray[scb_index];
2487
2488 /* Might be necessary */
2489 last_msg = ahc_inb(ahc, LAST_MSG);
2490
2491 if (ahc_sent_msg(ahc, MSG_EXT_WDTR, /*full*/FALSE)) {
2492 struct ahc_initiator_tinfo *tinfo;
2493 struct tmode_tstate *tstate;
2494
2495 #ifdef AHC_DEBUG_NEG
2496 /* note 8bit xfers */
2497 printf("%s:%c:%d: refuses WIDE negotiation. Using "
2498 "8bit transfers\n", ahc_name(ahc),
2499 devinfo->channel, devinfo->target);
2500 #endif
2501 ahc_set_width(ahc, devinfo,
2502 MSG_EXT_WDTR_BUS_8_BIT,
2503 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
2504 /*paused*/TRUE, /*done*/TRUE);
2505 /*
2506 * No need to clear the sync rate. If the target
2507 * did not accept the command, our syncrate is
2508 * unaffected. If the target started the negotiation,
2509 * but rejected our response, we already cleared the
2510 * sync rate before sending our WDTR.
2511 */
2512 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
2513 devinfo->our_scsiid,
2514 devinfo->target, &tstate);
2515 if (tinfo->goal.period) {
2516 u_int period;
2517
2518 /* Start the sync negotiation */
2519 period = tinfo->goal.period;
2520 ahc_devlimited_syncrate(ahc, &period);
2521 ahc->msgout_index = 0;
2522 ahc->msgout_len = 0;
2523 ahc_construct_sdtr(ahc, period, tinfo->goal.offset);
2524 ahc->msgout_index = 0;
2525 response = 1;
2526 } else
2527 ahc_update_xfer_mode(ahc, devinfo);
2528 } else if (ahc_sent_msg(ahc, MSG_EXT_SDTR, /*full*/FALSE)) {
2529 /* note asynch xfers and clear flag */
2530 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
2531 /*offset*/0, AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
2532 /*paused*/TRUE, /*done*/TRUE);
2533 #ifdef AHC_DEBUG_NEG
2534 printf("%s:%c:%d: refuses synchronous negotiation. "
2535 "Using asynchronous transfers\n",
2536 ahc_name(ahc),
2537 devinfo->channel, devinfo->target);
2538 #endif
2539 ahc_update_xfer_mode(ahc, devinfo);
2540 } else if ((scb->hscb->control & TAG_ENB) != 0) {
2541 printf("%s:%c:%d: refuses tagged commands. Performing "
2542 "non-tagged I/O\n", ahc_name(ahc),
2543 devinfo->channel, devinfo->target);
2544
2545 ahc_set_tags(ahc, devinfo, FALSE);
2546 ahc_update_xfer_mode(ahc, devinfo);
2547
2548 /*
2549 * Resend the identify for this CCB as the target
2550 * may believe that the selection is invalid otherwise.
2551 */
2552 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL)
2553 & ~MSG_SIMPLE_Q_TAG);
2554 scb->hscb->control &= ~MSG_SIMPLE_Q_TAG;
2555 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
2556 ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO);
2557
2558 /*
2559 * Requeue all tagged commands for this target
2560 * currently in our posession so they can be
2561 * converted to untagged commands.
2562 */
2563 ahc_search_qinfifo(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
2564 SCB_LUN(scb), /*tag*/SCB_LIST_NULL,
2565 ROLE_INITIATOR, SCB_REQUEUE,
2566 SEARCH_COMPLETE);
2567 } else {
2568 /*
2569 * Otherwise, we ignore it.
2570 */
2571 printf("%s:%c:%d: Message reject for %x -- ignored\n",
2572 ahc_name(ahc), devinfo->channel, devinfo->target,
2573 last_msg);
2574 }
2575 return (response);
2576 }
2577
2578 static void
2579 ahc_clear_msg_state(struct ahc_softc *ahc)
2580 {
2581 ahc->msgout_len = 0;
2582 ahc->msgin_index = 0;
2583 ahc->msg_type = MSG_TYPE_NONE;
2584 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
2585 }
2586
2587 static void
2588 ahc_handle_message_phase(struct ahc_softc *ahc, struct scsipi_periph *periph)
2589 {
2590 struct ahc_devinfo devinfo;
2591 u_int bus_phase;
2592 int end_session;
2593
2594 ahc_fetch_devinfo(ahc, &devinfo);
2595 end_session = FALSE;
2596 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2597
2598 reswitch:
2599 switch (ahc->msg_type) {
2600 case MSG_TYPE_INITIATOR_MSGOUT:
2601 {
2602 int lastbyte;
2603 int phasemis;
2604 int msgdone;
2605
2606 if (ahc->msgout_len == 0)
2607 panic("REQINIT interrupt with no active message");
2608
2609 phasemis = bus_phase != P_MESGOUT;
2610 if (phasemis) {
2611 if (bus_phase == P_MESGIN) {
2612 /*
2613 * Change gears and see if
2614 * this messages is of interest to
2615 * us or should be passed back to
2616 * the sequencer.
2617 */
2618 ahc_outb(ahc, CLRSINT1, CLRATNO);
2619 ahc->send_msg_perror = FALSE;
2620 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
2621 ahc->msgin_index = 0;
2622 goto reswitch;
2623 }
2624 end_session = TRUE;
2625 break;
2626 }
2627
2628 if (ahc->send_msg_perror) {
2629 ahc_outb(ahc, CLRSINT1, CLRATNO);
2630 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2631 ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
2632 break;
2633 }
2634
2635 msgdone = ahc->msgout_index == ahc->msgout_len;
2636 if (msgdone) {
2637 /*
2638 * The target has requested a retry.
2639 * Re-assert ATN, reset our message index to
2640 * 0, and try again.
2641 */
2642 ahc->msgout_index = 0;
2643 ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO);
2644 }
2645
2646 lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
2647 if (lastbyte) {
2648 /* Last byte is signified by dropping ATN */
2649 ahc_outb(ahc, CLRSINT1, CLRATNO);
2650 }
2651
2652 /*
2653 * Clear our interrupt status and present
2654 * the next byte on the bus.
2655 */
2656 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2657 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2658 break;
2659 }
2660 case MSG_TYPE_INITIATOR_MSGIN:
2661 {
2662 int phasemis;
2663 int message_done;
2664
2665 phasemis = bus_phase != P_MESGIN;
2666
2667 if (phasemis) {
2668 ahc->msgin_index = 0;
2669 if (bus_phase == P_MESGOUT
2670 && (ahc->send_msg_perror == TRUE
2671 || (ahc->msgout_len != 0
2672 && ahc->msgout_index == 0))) {
2673 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2674 goto reswitch;
2675 }
2676 end_session = TRUE;
2677 break;
2678 }
2679
2680 /* Pull the byte in without acking it */
2681 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
2682
2683 message_done = ahc_parse_msg(ahc, periph, &devinfo);
2684
2685 if (message_done) {
2686 /*
2687 * Clear our incoming message buffer in case there
2688 * is another message following this one.
2689 */
2690 ahc->msgin_index = 0;
2691
2692 /*
2693 * If this message illicited a response,
2694 * assert ATN so the target takes us to the
2695 * message out phase.
2696 */
2697 if (ahc->msgout_len != 0)
2698 ahc_outb(ahc, SCSISIGO,
2699 ahc_inb(ahc, SCSISIGO) | ATNO);
2700 } else
2701 ahc->msgin_index++;
2702
2703 /* Ack the byte */
2704 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2705 ahc_inb(ahc, SCSIDATL);
2706 break;
2707 }
2708 case MSG_TYPE_TARGET_MSGIN:
2709 {
2710 int msgdone;
2711 int msgout_request;
2712
2713 if (ahc->msgout_len == 0)
2714 panic("Target MSGIN with no active message");
2715
2716 /*
2717 * If we interrupted a mesgout session, the initiator
2718 * will not know this until our first REQ. So, we
2719 * only honor mesgout requests after we've sent our
2720 * first byte.
2721 */
2722 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
2723 && ahc->msgout_index > 0)
2724 msgout_request = TRUE;
2725 else
2726 msgout_request = FALSE;
2727
2728 if (msgout_request) {
2729
2730 /*
2731 * Change gears and see if
2732 * this messages is of interest to
2733 * us or should be passed back to
2734 * the sequencer.
2735 */
2736 ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
2737 ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
2738 ahc->msgin_index = 0;
2739 /* Dummy read to REQ for first byte */
2740 ahc_inb(ahc, SCSIDATL);
2741 ahc_outb(ahc, SXFRCTL0,
2742 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2743 break;
2744 }
2745
2746 msgdone = ahc->msgout_index == ahc->msgout_len;
2747 if (msgdone) {
2748 ahc_outb(ahc, SXFRCTL0,
2749 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2750 end_session = TRUE;
2751 break;
2752 }
2753
2754 /*
2755 * Present the next byte on the bus.
2756 */
2757 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2758 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2759 break;
2760 }
2761 case MSG_TYPE_TARGET_MSGOUT:
2762 {
2763 int lastbyte;
2764 int msgdone;
2765
2766 /*
2767 * The initiator signals that this is
2768 * the last byte by dropping ATN.
2769 */
2770 lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
2771
2772 /*
2773 * Read the latched byte, but turn off SPIOEN first
2774 * so that we don't inadvertantly cause a REQ for the
2775 * next byte.
2776 */
2777 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2778 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
2779 msgdone = ahc_parse_msg(ahc, periph, &devinfo);
2780 if (msgdone == MSGLOOP_TERMINATED) {
2781 /*
2782 * The message is *really* done in that it caused
2783 * us to go to bus free. The sequencer has already
2784 * been reset at this point, so pull the ejection
2785 * handle.
2786 */
2787 return;
2788 }
2789
2790 ahc->msgin_index++;
2791
2792 /*
2793 * XXX Read spec about initiator dropping ATN too soon
2794 * and use msgdone to detect it.
2795 */
2796 if (msgdone == MSGLOOP_MSGCOMPLETE) {
2797 ahc->msgin_index = 0;
2798
2799 /*
2800 * If this message illicited a response, transition
2801 * to the Message in phase and send it.
2802 */
2803 if (ahc->msgout_len != 0) {
2804 ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
2805 ahc_outb(ahc, SXFRCTL0,
2806 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2807 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
2808 ahc->msgin_index = 0;
2809 break;
2810 }
2811 }
2812
2813 if (lastbyte)
2814 end_session = TRUE;
2815 else {
2816 /* Ask for the next byte. */
2817 ahc_outb(ahc, SXFRCTL0,
2818 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2819 }
2820
2821 break;
2822 }
2823 default:
2824 panic("Unknown REQINIT message type");
2825 }
2826
2827 if (end_session) {
2828 ahc_clear_msg_state(ahc);
2829 ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
2830 } else
2831 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
2832 }
2833
2834 /*
2835 * See if we sent a particular extended message to the target.
2836 * If "full" is true, the target saw the full message.
2837 * If "full" is false, the target saw at least the first
2838 * byte of the message.
2839 */
2840 static int
2841 ahc_sent_msg(struct ahc_softc *ahc, u_int msgtype, int full)
2842 {
2843 int found;
2844 int index;
2845
2846 found = FALSE;
2847 index = 0;
2848
2849 while (index < ahc->msgout_len) {
2850 if (ahc->msgout_buf[index] == MSG_EXTENDED) {
2851
2852 /* Found a candidate */
2853 if (ahc->msgout_buf[index+2] == msgtype) {
2854 u_int end_index;
2855
2856 end_index = index + 1
2857 + ahc->msgout_buf[index + 1];
2858 if (full) {
2859 if (ahc->msgout_index > end_index)
2860 found = TRUE;
2861 } else if (ahc->msgout_index > index)
2862 found = TRUE;
2863 }
2864 break;
2865 } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_Q_TAG
2866 && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
2867
2868 /* Skip tag type and tag id or residue param*/
2869 index += 2;
2870 } else {
2871 /* Single byte message */
2872 index++;
2873 }
2874 }
2875 return (found);
2876 }
2877
2878 static int
2879 ahc_parse_msg(struct ahc_softc *ahc, struct scsipi_periph *periph,
2880 struct ahc_devinfo *devinfo)
2881 {
2882 struct ahc_initiator_tinfo *tinfo;
2883 struct tmode_tstate *tstate;
2884 int reject;
2885 int done;
2886 int response;
2887 u_int targ_scsirate;
2888
2889 done = MSGLOOP_IN_PROG;
2890 response = FALSE;
2891 reject = FALSE;
2892 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2893 devinfo->target, &tstate);
2894 targ_scsirate = tinfo->scsirate;
2895
2896 /*
2897 * Parse as much of the message as is available,
2898 * rejecting it if we don't support it. When
2899 * the entire message is available and has been
2900 * handled, return MSGLOOP_MSGCOMPLETE, indicating
2901 * that we have parsed an entire message.
2902 *
2903 * In the case of extended messages, we accept the length
2904 * byte outright and perform more checking once we know the
2905 * extended message type.
2906 */
2907 switch (ahc->msgin_buf[0]) {
2908 case MSG_MESSAGE_REJECT:
2909 response = ahc_handle_msg_reject(ahc, devinfo);
2910 /* FALLTHROUGH */
2911 case MSG_NOOP:
2912 done = MSGLOOP_MSGCOMPLETE;
2913 break;
2914 case MSG_IGN_WIDE_RESIDUE:
2915 {
2916 /* Wait for the whole message */
2917 if (ahc->msgin_index >= 1) {
2918 if (ahc->msgin_buf[1] != 1
2919 || tinfo->current.width == MSG_EXT_WDTR_BUS_8_BIT) {
2920 reject = TRUE;
2921 done = MSGLOOP_MSGCOMPLETE;
2922 } else
2923 ahc_handle_ign_wide_residue(ahc, devinfo);
2924 }
2925 break;
2926 }
2927 case MSG_EXTENDED:
2928 {
2929 /* Wait for enough of the message to begin validation */
2930 if (ahc->msgin_index < 2)
2931 break;
2932 switch (ahc->msgin_buf[2]) {
2933 case MSG_EXT_SDTR:
2934 {
2935 const struct ahc_syncrate *syncrate;
2936 u_int period;
2937 u_int offset;
2938 u_int saved_offset;
2939
2940 if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
2941 reject = TRUE;
2942 break;
2943 }
2944
2945 /*
2946 * Wait until we have both args before validating
2947 * and acting on this message.
2948 *
2949 * Add one to MSG_EXT_SDTR_LEN to account for
2950 * the extended message preamble.
2951 */
2952 if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
2953 break;
2954
2955 period = ahc->msgin_buf[3];
2956 saved_offset = offset = ahc->msgin_buf[4];
2957 syncrate = ahc_devlimited_syncrate(ahc, &period);
2958 ahc_validate_offset(ahc, syncrate, &offset,
2959 targ_scsirate & WIDEXFER);
2960 ahc_set_syncrate(ahc, devinfo,
2961 syncrate, period, offset,
2962 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
2963 /*paused*/TRUE, /*done*/TRUE);
2964 ahc_update_xfer_mode(ahc, devinfo);
2965
2966 /*
2967 * See if we initiated Sync Negotiation
2968 * and didn't have to fall down to async
2969 * transfers.
2970 */
2971 if (ahc_sent_msg(ahc, MSG_EXT_SDTR, /*full*/TRUE)) {
2972 /* We started it */
2973 if (saved_offset != offset) {
2974 /* Went too low - force async */
2975 reject = TRUE;
2976 }
2977 } else {
2978 /*
2979 * Send our own SDTR in reply
2980 */
2981 ahc->msgout_index = 0;
2982 ahc->msgout_len = 0;
2983 ahc_construct_sdtr(ahc, period, offset);
2984 ahc->msgout_index = 0;
2985 response = TRUE;
2986 }
2987 done = MSGLOOP_MSGCOMPLETE;
2988 break;
2989 }
2990 case MSG_EXT_WDTR:
2991 {
2992 u_int bus_width;
2993 u_int sending_reply;
2994
2995 sending_reply = FALSE;
2996 if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
2997 reject = TRUE;
2998 break;
2999 }
3000
3001 /*
3002 * Wait until we have our arg before validating
3003 * and acting on this message.
3004 *
3005 * Add one to MSG_EXT_WDTR_LEN to account for
3006 * the extended message preamble.
3007 */
3008 if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
3009 break;
3010
3011 bus_width = ahc->msgin_buf[3];
3012 if (ahc_sent_msg(ahc, MSG_EXT_WDTR, /*full*/TRUE)) {
3013 /*
3014 * Don't send a WDTR back to the
3015 * target, since we asked first.
3016 */
3017 switch (bus_width){
3018 default:
3019 /*
3020 * How can we do anything greater
3021 * than 16bit transfers on a 16bit
3022 * bus?
3023 */
3024 reject = TRUE;
3025 printf("%s: target %d requested %dBit "
3026 "transfers. Rejecting...\n",
3027 ahc_name(ahc), devinfo->target,
3028 8 * (0x01 << bus_width));
3029 /* FALLTHROUGH */
3030 case MSG_EXT_WDTR_BUS_8_BIT:
3031 bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3032 break;
3033 case MSG_EXT_WDTR_BUS_16_BIT:
3034 break;
3035 }
3036 } else {
3037 /*
3038 * Send our own WDTR in reply
3039 */
3040 switch (bus_width) {
3041 default:
3042 if (ahc->features & AHC_WIDE) {
3043 /* Respond Wide */
3044 bus_width =
3045 MSG_EXT_WDTR_BUS_16_BIT;
3046 break;
3047 }
3048 /* FALLTHROUGH */
3049 case MSG_EXT_WDTR_BUS_8_BIT:
3050 bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3051 break;
3052 }
3053 ahc->msgout_index = 0;
3054 ahc->msgout_len = 0;
3055 ahc_construct_wdtr(ahc, bus_width);
3056 ahc->msgout_index = 0;
3057 response = TRUE;
3058 sending_reply = TRUE;
3059 }
3060 ahc_set_width(ahc, devinfo, bus_width,
3061 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3062 /*paused*/TRUE, /*done*/TRUE);
3063
3064 /* After a wide message, we are async */
3065 ahc_set_syncrate(ahc, devinfo,
3066 /*syncrate*/NULL, /*period*/0,
3067 /*offset*/0, AHC_TRANS_ACTIVE,
3068 /*paused*/TRUE, /*done*/FALSE);
3069 if (sending_reply == FALSE && reject == FALSE) {
3070
3071 if (tinfo->goal.period) {
3072 const struct ahc_syncrate *rate;
3073 u_int period;
3074 u_int offset;
3075
3076 /* Start the sync negotiation */
3077 period = tinfo->goal.period;
3078 rate = ahc_devlimited_syncrate(ahc,
3079 &period);
3080 offset = tinfo->goal.offset;
3081 ahc_validate_offset(ahc, rate, &offset,
3082 tinfo->current.width);
3083 ahc->msgout_index = 0;
3084 ahc->msgout_len = 0;
3085 ahc_construct_sdtr(ahc, period, offset);
3086 ahc->msgout_index = 0;
3087 response = TRUE;
3088 } else
3089 ahc_update_xfer_mode(ahc, devinfo);
3090 }
3091 done = MSGLOOP_MSGCOMPLETE;
3092 break;
3093 }
3094 default:
3095 /* Unknown extended message. Reject it. */
3096 reject = TRUE;
3097 break;
3098 }
3099 break;
3100 }
3101 case MSG_BUS_DEV_RESET:
3102 ahc_handle_devreset(ahc, devinfo,
3103 XS_RESET, "Bus Device Reset Received",
3104 /*verbose_level*/0);
3105 restart_sequencer(ahc);
3106 done = MSGLOOP_TERMINATED;
3107 break;
3108 case MSG_ABORT_TAG:
3109 case MSG_ABORT:
3110 case MSG_CLEAR_QUEUE:
3111 /* Target mode messages */
3112 if (devinfo->role != ROLE_TARGET) {
3113 reject = TRUE;
3114 break;
3115 }
3116 #if AHC_TARGET_MODE
3117 ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3118 devinfo->lun,
3119 ahc->msgin_buf[0] == MSG_ABORT_TAG
3120 ? SCB_LIST_NULL
3121 : ahc_inb(ahc, INITIATOR_TAG),
3122 ROLE_TARGET, XS_DRIVER_STUFFUP);
3123
3124 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3125 if (tstate != NULL) {
3126 struct tmode_lstate* lstate;
3127
3128 lstate = tstate->enabled_luns[devinfo->lun];
3129 if (lstate != NULL) {
3130 ahc_queue_lstate_event(ahc, lstate,
3131 devinfo->our_scsiid,
3132 ahc->msgin_buf[0],
3133 /*arg*/0);
3134 ahc_send_lstate_events(ahc, lstate);
3135 }
3136 }
3137 done = MSGLOOP_MSGCOMPLETE;
3138 #else
3139 panic("ahc: got target mode message");
3140 #endif
3141 break;
3142 case MSG_TERM_IO_PROC:
3143 default:
3144 reject = TRUE;
3145 break;
3146 }
3147
3148 if (reject) {
3149 /*
3150 * Setup to reject the message.
3151 */
3152 ahc->msgout_index = 0;
3153 ahc->msgout_len = 1;
3154 ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
3155 done = MSGLOOP_MSGCOMPLETE;
3156 response = TRUE;
3157 }
3158
3159 if (done != MSGLOOP_IN_PROG && !response)
3160 /* Clear the outgoing message buffer */
3161 ahc->msgout_len = 0;
3162
3163 return (done);
3164 }
3165
3166 static void
3167 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3168 {
3169 u_int scb_index;
3170 struct scb *scb;
3171
3172 scb_index = ahc_inb(ahc, SCB_TAG);
3173 scb = &ahc->scb_data->scbarray[scb_index];
3174 if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
3175 || !(scb->xs->xs_control & XS_CTL_DATA_IN)) {
3176 /*
3177 * Ignore the message if we haven't
3178 * seen an appropriate data phase yet.
3179 */
3180 } else {
3181 /*
3182 * If the residual occurred on the last
3183 * transfer and the transfer request was
3184 * expected to end on an odd count, do
3185 * nothing. Otherwise, subtract a byte
3186 * and update the residual count accordingly.
3187 */
3188 u_int resid_sgcnt;
3189
3190 resid_sgcnt = ahc_inb(ahc, SCB_RESID_SGCNT);
3191 if (resid_sgcnt == 0
3192 && ahc_inb(ahc, DATA_COUNT_ODD) == 1) {
3193 /*
3194 * If the residual occurred on the last
3195 * transfer and the transfer request was
3196 * expected to end on an odd count, do
3197 * nothing.
3198 */
3199 } else {
3200 u_int data_cnt;
3201 u_int32_t data_addr;
3202 u_int sg_index;
3203
3204 data_cnt = (ahc_inb(ahc, SCB_RESID_DCNT + 2) << 16)
3205 | (ahc_inb(ahc, SCB_RESID_DCNT + 1) << 8)
3206 | (ahc_inb(ahc, SCB_RESID_DCNT));
3207
3208 data_addr = (ahc_inb(ahc, SHADDR + 3) << 24)
3209 | (ahc_inb(ahc, SHADDR + 2) << 16)
3210 | (ahc_inb(ahc, SHADDR + 1) << 8)
3211 | (ahc_inb(ahc, SHADDR));
3212
3213 data_cnt += 1;
3214 data_addr -= 1;
3215
3216 sg_index = scb->sg_count - resid_sgcnt;
3217
3218 if (sg_index != 0
3219 && (le32toh(scb->sg_list[sg_index].len) < data_cnt)) {
3220 u_int32_t sg_addr;
3221
3222 sg_index--;
3223 data_cnt = 1;
3224 data_addr = le32toh(scb->sg_list[sg_index].addr)
3225 + le32toh(scb->sg_list[sg_index].len)
3226 - 1;
3227
3228 /*
3229 * The physical address base points to the
3230 * second entry as it is always used for
3231 * calculating the "next S/G pointer".
3232 */
3233 sg_addr = scb->sg_list_phys
3234 + (sg_index* sizeof(*scb->sg_list));
3235 ahc_outb(ahc, SG_NEXT + 3, sg_addr >> 24);
3236 ahc_outb(ahc, SG_NEXT + 2, sg_addr >> 16);
3237 ahc_outb(ahc, SG_NEXT + 1, sg_addr >> 8);
3238 ahc_outb(ahc, SG_NEXT, sg_addr);
3239 }
3240
3241 ahc_outb(ahc, SCB_RESID_DCNT + 2, data_cnt >> 16);
3242 ahc_outb(ahc, SCB_RESID_DCNT + 1, data_cnt >> 8);
3243 ahc_outb(ahc, SCB_RESID_DCNT, data_cnt);
3244
3245 ahc_outb(ahc, SHADDR + 3, data_addr >> 24);
3246 ahc_outb(ahc, SHADDR + 2, data_addr >> 16);
3247 ahc_outb(ahc, SHADDR + 1, data_addr >> 8);
3248 ahc_outb(ahc, SHADDR, data_addr);
3249 }
3250 }
3251 }
3252
3253 static void
3254 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3255 int status, char *message,
3256 int verbose_level)
3257 {
3258 int found;
3259
3260 found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3261 AHC_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
3262 status);
3263
3264 /*
3265 * Go back to async/narrow transfers and renegotiate.
3266 * ahc_set_width and ahc_set_syncrate can cope with NULL
3267 * paths.
3268 */
3269 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3270 AHC_TRANS_CUR, /*paused*/TRUE, /*done*/FALSE);
3271 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
3272 /*period*/0, /*offset*/0, AHC_TRANS_CUR,
3273 /*paused*/TRUE, /*done*/FALSE);
3274 ahc_update_xfer_mode(ahc, devinfo);
3275
3276 if (message != NULL && (verbose_level <= 0))
3277 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
3278 message, devinfo->channel, devinfo->target, found);
3279 }
3280
3281 /*
3282 * We have an scb which has been processed by the
3283 * adaptor, now we look to see how the operation
3284 * went.
3285 */
3286 static void
3287 ahc_done(struct ahc_softc *ahc, struct scb *scb)
3288 {
3289 struct scsipi_xfer *xs;
3290 struct scsipi_periph *periph;
3291 int requeue = 0;
3292 int target;
3293
3294
3295 xs = scb->xs;
3296 periph = xs->xs_periph;
3297 LIST_REMOVE(scb, plinks);
3298
3299 callout_stop(&scb->xs->xs_callout);
3300
3301 #ifdef AHC_DEBUG
3302 if (ahc_debug & AHC_SHOWCMDS) {
3303 scsipi_printaddr(periph);
3304 printf("ahc_done opcode %d tag %x\n", xs->cmdstore.opcode,
3305 scb->hscb->tag);
3306 }
3307 #endif
3308
3309 target = periph->periph_target;
3310
3311 if (xs->datalen) {
3312 int op;
3313
3314 if (xs->xs_control & XS_CTL_DATA_IN)
3315 op = BUS_DMASYNC_POSTREAD;
3316 else
3317 op = BUS_DMASYNC_POSTWRITE;
3318 bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
3319 scb->dmamap->dm_mapsize, op);
3320 bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
3321 }
3322
3323 /*
3324 * Unbusy this target/channel/lun.
3325 * XXX if we are holding two commands per lun,
3326 * send the next command.
3327 */
3328 if (!(scb->hscb->control & TAG_ENB))
3329 ahc_index_busy_tcl(ahc, scb->hscb->tcl, /*unbusy*/TRUE);
3330
3331 /*
3332 * If the recovery SCB completes, we have to be
3333 * out of our timeout.
3334 */
3335 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
3336
3337 struct scb *scbp;
3338
3339 /*
3340 * We were able to complete the command successfully,
3341 * so reinstate the timeouts for all other pending
3342 * commands.
3343 */
3344 scbp = ahc->pending_ccbs.lh_first;
3345 while (scbp != NULL) {
3346 struct scsipi_xfer *txs = scbp->xs;
3347
3348 if (!(txs->xs_control & XS_CTL_POLL)) {
3349 callout_reset(&scbp->xs->xs_callout,
3350 (scbp->xs->timeout * hz) / 1000,
3351 ahc_timeout, scbp);
3352 }
3353 scbp = LIST_NEXT(scbp, plinks);
3354 }
3355
3356 /*
3357 * Ensure that we didn't put a second instance of this
3358 * SCB into the QINFIFO.
3359 */
3360 ahc_search_qinfifo(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
3361 SCB_LUN(scb), scb->hscb->tag,
3362 ROLE_INITIATOR, /*status*/0,
3363 SEARCH_REMOVE);
3364 if (xs->error != XS_NOERROR)
3365 ahcsetccbstatus(xs, XS_TIMEOUT);
3366 scsipi_printaddr(xs->xs_periph);
3367 printf("no longer in timeout, status = %x\n", xs->status);
3368 }
3369
3370 if (xs->error != XS_NOERROR) {
3371 /* Don't clobber any existing error state */
3372 } else if ((scb->flags & SCB_SENSE) != 0) {
3373 /*
3374 * We performed autosense retrieval.
3375 *
3376 * bzero the sense data before having
3377 * the drive fill it. The SCSI spec mandates
3378 * that any untransfered data should be
3379 * assumed to be zero. Complete the 'bounce'
3380 * of sense information through buffers accessible
3381 * via bus-space by copying it into the clients
3382 * csio.
3383 */
3384 bzero(&xs->sense.scsi_sense, sizeof(xs->sense.scsi_sense));
3385 memcpy(&xs->sense.scsi_sense,
3386 &ahc->scb_data->sense[scb->hscb->tag],
3387 le32toh(scb->sg_list->len));
3388 xs->error = XS_SENSE;
3389 }
3390 if (scb->flags & SCB_FREEZE_QUEUE) {
3391 scsipi_periph_thaw(periph, 1);
3392 scb->flags &= ~SCB_FREEZE_QUEUE;
3393 }
3394
3395 requeue = scb->flags & SCB_REQUEUE;
3396 ahcfreescb(ahc, scb);
3397
3398 if (requeue) {
3399 xs->error = XS_REQUEUE;
3400 }
3401 scsipi_done(xs);
3402 }
3403
3404 /*
3405 * Determine the number of SCBs available on the controller
3406 */
3407 int
3408 ahc_probe_scbs(struct ahc_softc *ahc) {
3409 int i;
3410
3411 for (i = 0; i < AHC_SCB_MAX; i++) {
3412 ahc_outb(ahc, SCBPTR, i);
3413 ahc_outb(ahc, SCB_CONTROL, i);
3414 if (ahc_inb(ahc, SCB_CONTROL) != i)
3415 break;
3416 ahc_outb(ahc, SCBPTR, 0);
3417 if (ahc_inb(ahc, SCB_CONTROL) != 0)
3418 break;
3419 }
3420 return (i);
3421 }
3422
3423 /*
3424 * Start the board, ready for normal operation
3425 */
3426 int
3427 ahc_init(struct ahc_softc *ahc)
3428 {
3429 int max_targ = 15;
3430 int i;
3431 int term;
3432 u_int scsi_conf;
3433 u_int scsiseq_template;
3434 u_int ultraenb;
3435 u_int discenable;
3436 u_int tagenable;
3437 size_t driver_data_size;
3438 u_int32_t physaddr;
3439
3440 #ifdef AHC_PRINT_SRAM
3441 printf("Scratch Ram:");
3442 for (i = 0x20; i < 0x5f; i++) {
3443 if (((i % 8) == 0) && (i != 0)) {
3444 printf ("\n ");
3445 }
3446 printf (" 0x%x", ahc_inb(ahc, i));
3447 }
3448 if ((ahc->features & AHC_MORE_SRAM) != 0) {
3449 for (i = 0x70; i < 0x7f; i++) {
3450 if (((i % 8) == 0) && (i != 0)) {
3451 printf ("\n ");
3452 }
3453 printf (" 0x%x", ahc_inb(ahc, i));
3454 }
3455 }
3456 printf ("\n");
3457 #endif
3458
3459 /*
3460 * Assume we have a board at this stage and it has been reset.
3461 */
3462 if ((ahc->flags & AHC_USEDEFAULTS) != 0)
3463 ahc->our_id = ahc->our_id_b = 7;
3464
3465 /*
3466 * Default to allowing initiator operations.
3467 */
3468 ahc->flags |= AHC_INITIATORMODE;
3469
3470 /*
3471 * DMA tag for our command fifos and other data in system memory
3472 * the card's sequencer must be able to access. For initiator
3473 * roles, we need to allocate space for the qinfifo, qoutfifo,
3474 * and untagged_scb arrays each of which are composed of 256
3475 * 1 byte elements. When providing for the target mode role,
3476 * we additionally must provide space for the incoming target
3477 * command fifo.
3478 */
3479 driver_data_size = 3 * 256 * sizeof(u_int8_t);
3480
3481 if (ahc_createdmamem(ahc->parent_dmat, driver_data_size,
3482 ahc->sc_dmaflags,
3483 &ahc->shared_data_dmamap, (caddr_t *)&ahc->qoutfifo,
3484 &ahc->shared_data_busaddr, &ahc->shared_data_seg,
3485 &ahc->shared_data_nseg, ahc_name(ahc), "shared data") < 0)
3486 return (ENOMEM);
3487
3488 ahc->init_level++;
3489
3490 /* Allocate SCB data now that parent_dmat is initialized */
3491 if (ahc->scb_data->maxhscbs == 0)
3492 if (ahcinitscbdata(ahc) != 0)
3493 return (ENOMEM);
3494
3495 ahc->qinfifo = &ahc->qoutfifo[256];
3496 ahc->untagged_scbs = &ahc->qinfifo[256];
3497 /* There are no untagged SCBs active yet. */
3498 for (i = 0; i < 256; i++)
3499 ahc->untagged_scbs[i] = SCB_LIST_NULL;
3500
3501 /* All of our queues are empty */
3502 for (i = 0; i < 256; i++)
3503 ahc->qoutfifo[i] = SCB_LIST_NULL;
3504
3505 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap, 0,
3506 driver_data_size, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3507
3508 /*
3509 * Allocate a tstate to house information for our
3510 * initiator presence on the bus as well as the user
3511 * data for any target mode initiator.
3512 */
3513 if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
3514 printf("%s: unable to allocate tmode_tstate. "
3515 "Failing attach\n", ahc_name(ahc));
3516 return (-1);
3517 }
3518
3519 if ((ahc->features & AHC_TWIN) != 0) {
3520 if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
3521 printf("%s: unable to allocate tmode_tstate. "
3522 "Failing attach\n", ahc_name(ahc));
3523 return (-1);
3524 }
3525 printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, primary %c, ",
3526 ahc->our_id, ahc->our_id_b,
3527 ahc->flags & AHC_CHANNEL_B_PRIMARY? 'B': 'A');
3528 } else {
3529 if ((ahc->features & AHC_WIDE) != 0) {
3530 printf("Wide ");
3531 } else {
3532 printf("Single ");
3533 }
3534 printf("Channel %c, SCSI Id=%d, ", ahc->channel, ahc->our_id);
3535 }
3536
3537 ahc_outb(ahc, SEQ_FLAGS, 0);
3538
3539 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX) {
3540 ahc->flags |= AHC_PAGESCBS;
3541 printf("%d/%d SCBs\n", ahc->scb_data->maxhscbs, AHC_SCB_MAX);
3542 } else {
3543 ahc->flags &= ~AHC_PAGESCBS;
3544 printf("%d SCBs\n", ahc->scb_data->maxhscbs);
3545 }
3546
3547 #ifdef AHC_DEBUG
3548 if (ahc_debug & AHC_SHOWMISC) {
3549 printf("%s: hardware scb %d bytes; kernel scb %d bytes; "
3550 "ahc_dma %d bytes\n",
3551 ahc_name(ahc),
3552 sizeof(struct hardware_scb),
3553 sizeof(struct scb),
3554 sizeof(struct ahc_dma_seg));
3555 }
3556 #endif /* AHC_DEBUG */
3557
3558 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
3559 if (ahc->features & AHC_TWIN) {
3560
3561 /*
3562 * The device is gated to channel B after a chip reset,
3563 * so set those values first
3564 */
3565 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
3566 if ((ahc->features & AHC_ULTRA2) != 0)
3567 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id_b);
3568 else
3569 ahc_outb(ahc, SCSIID, ahc->our_id_b);
3570 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
3571 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
3572 |term|ENSTIMER|ACTNEGEN);
3573 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
3574 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
3575
3576 if ((scsi_conf & RESET_SCSI) != 0
3577 && (ahc->flags & AHC_INITIATORMODE) != 0)
3578 ahc->flags |= AHC_RESET_BUS_B;
3579
3580 /* Select Channel A */
3581 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
3582 }
3583 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
3584 if ((ahc->features & AHC_ULTRA2) != 0)
3585 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
3586 else
3587 ahc_outb(ahc, SCSIID, ahc->our_id);
3588 scsi_conf = ahc_inb(ahc, SCSICONF);
3589 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
3590 |term
3591 |ENSTIMER|ACTNEGEN);
3592 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
3593 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
3594
3595 if ((scsi_conf & RESET_SCSI) != 0
3596 && (ahc->flags & AHC_INITIATORMODE) != 0)
3597 ahc->flags |= AHC_RESET_BUS_A;
3598
3599 /*
3600 * Look at the information that board initialization or
3601 * the board bios has left us.
3602 */
3603 ultraenb = 0;
3604 tagenable = ALL_TARGETS_MASK;
3605
3606 /* Grab the disconnection disable table and invert it for our needs */
3607 if (ahc->flags & AHC_USEDEFAULTS) {
3608 printf("%s: Host Adapter Bios disabled. Using default SCSI "
3609 "device parameters\n", ahc_name(ahc));
3610 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
3611 AHC_TERM_ENB_A|AHC_TERM_ENB_B;
3612 discenable = ALL_TARGETS_MASK;
3613 if ((ahc->features & AHC_ULTRA) != 0)
3614 ultraenb = ALL_TARGETS_MASK;
3615 } else {
3616 discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
3617 | ahc_inb(ahc, DISC_DSB));
3618 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
3619 ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
3620 | ahc_inb(ahc, ULTRA_ENB);
3621 }
3622
3623 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
3624 max_targ = 7;
3625
3626 for (i = 0; i <= max_targ; i++) {
3627 struct ahc_initiator_tinfo *tinfo;
3628 struct tmode_tstate *tstate;
3629 u_int our_id;
3630 u_int target_id;
3631 char channel;
3632
3633 channel = 'A';
3634 our_id = ahc->our_id;
3635 target_id = i;
3636 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
3637 channel = 'B';
3638 our_id = ahc->our_id_b;
3639 target_id = i % 8;
3640 }
3641 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
3642 target_id, &tstate);
3643 /* Default to async narrow across the board */
3644 bzero(tinfo, sizeof(*tinfo));
3645 if (ahc->flags & AHC_USEDEFAULTS) {
3646 if ((ahc->features & AHC_WIDE) != 0)
3647 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
3648
3649 /*
3650 * These will be truncated when we determine the
3651 * connection type we have with the target.
3652 */
3653 tinfo->user.period = ahc_syncrates->period;
3654 tinfo->user.offset = ~0;
3655 } else {
3656 u_int scsirate;
3657 u_int16_t mask;
3658
3659 /* Take the settings leftover in scratch RAM. */
3660 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
3661 mask = (0x01 << i);
3662 if ((ahc->features & AHC_ULTRA2) != 0) {
3663 u_int offset;
3664 u_int maxsync;
3665
3666 if ((scsirate & SOFS) == 0x0F) {
3667 /*
3668 * Haven't negotiated yet,
3669 * so the format is different.
3670 */
3671 scsirate = (scsirate & SXFR) >> 4
3672 | (ultraenb & mask)
3673 ? 0x08 : 0x0
3674 | (scsirate & WIDEXFER);
3675 offset = MAX_OFFSET_ULTRA2;
3676 } else
3677 offset = ahc_inb(ahc, TARG_OFFSET + i);
3678 maxsync = AHC_SYNCRATE_ULTRA2;
3679 if ((ahc->features & AHC_DT) != 0)
3680 maxsync = AHC_SYNCRATE_DT;
3681 tinfo->user.period =
3682 ahc_find_period(ahc, scsirate, maxsync);
3683 if (offset == 0)
3684 tinfo->user.period = 0;
3685 else
3686 tinfo->user.offset = ~0;
3687 } else if ((scsirate & SOFS) != 0) {
3688 tinfo->user.period =
3689 ahc_find_period(ahc, scsirate,
3690 (ultraenb & mask)
3691 ? AHC_SYNCRATE_ULTRA
3692 : AHC_SYNCRATE_FAST);
3693 if (tinfo->user.period != 0)
3694 tinfo->user.offset = ~0;
3695 }
3696 if ((scsirate & WIDEXFER) != 0
3697 && (ahc->features & AHC_WIDE) != 0)
3698 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
3699 }
3700 tinfo->goal = tinfo->user; /* force negotiation */
3701 tstate->ultraenb = ultraenb;
3702 tstate->discenable = discenable;
3703 tstate->tagenable = 0; /* Wait until the XPT says its okay */
3704 tstate->tagdisable = 0;
3705 }
3706 ahc->user_discenable = discenable;
3707 ahc->user_tagenable = tagenable;
3708
3709 /*
3710 * Tell the sequencer where it can find our arrays in memory.
3711 */
3712 physaddr = ahc->scb_data->hscb_busaddr;
3713 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
3714 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
3715 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
3716 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
3717
3718 physaddr = ahc->shared_data_busaddr;
3719 ahc_outb(ahc, SCBID_ADDR, physaddr & 0xFF);
3720 ahc_outb(ahc, SCBID_ADDR + 1, (physaddr >> 8) & 0xFF);
3721 ahc_outb(ahc, SCBID_ADDR + 2, (physaddr >> 16) & 0xFF);
3722 ahc_outb(ahc, SCBID_ADDR + 3, (physaddr >> 24) & 0xFF);
3723
3724 /* Target mode incomding command fifo */
3725 physaddr += 3 * 256 * sizeof(u_int8_t);
3726 ahc_outb(ahc, TMODE_CMDADDR, physaddr & 0xFF);
3727 ahc_outb(ahc, TMODE_CMDADDR + 1, (physaddr >> 8) & 0xFF);
3728 ahc_outb(ahc, TMODE_CMDADDR + 2, (physaddr >> 16) & 0xFF);
3729 ahc_outb(ahc, TMODE_CMDADDR + 3, (physaddr >> 24) & 0xFF);
3730
3731 /*
3732 * Initialize the group code to command length table.
3733 * This overrides the values in TARG_SCSIRATE, so only
3734 * setup the table after we have processed that information.
3735 */
3736 ahc_outb(ahc, CMDSIZE_TABLE, 5);
3737 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
3738 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
3739 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
3740 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
3741 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
3742 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
3743 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
3744
3745 /* Tell the sequencer of our initial queue positions */
3746 ahc_outb(ahc, KERNEL_QINPOS, 0);
3747 ahc_outb(ahc, QINPOS, 0);
3748 ahc_outb(ahc, QOUTPOS, 0);
3749
3750 #ifdef AHC_DEBUG
3751 if (ahc_debug & AHC_SHOWMISC)
3752 printf("DISCENABLE == 0x%x\nULTRAENB == 0x%x\n",
3753 discenable, ultraenb);
3754 #endif
3755
3756 /* Don't have any special messages to send to targets */
3757 ahc_outb(ahc, TARGET_MSG_REQUEST, 0);
3758 ahc_outb(ahc, TARGET_MSG_REQUEST + 1, 0);
3759
3760 /*
3761 * Use the built in queue management registers
3762 * if they are available.
3763 */
3764 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
3765 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
3766 ahc_outb(ahc, SDSCB_QOFF, 0);
3767 ahc_outb(ahc, SNSCB_QOFF, 0);
3768 ahc_outb(ahc, HNSCB_QOFF, 0);
3769 }
3770
3771
3772 /* We don't have any waiting selections */
3773 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
3774
3775 /* Our disconnection list is empty too */
3776 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
3777
3778 /* Message out buffer starts empty */
3779 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
3780
3781 /*
3782 * Setup the allowed SCSI Sequences based on operational mode.
3783 * If we are a target, we'll enable select in operations once
3784 * we've had a lun enabled.
3785 */
3786 scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
3787 if ((ahc->flags & AHC_INITIATORMODE) != 0)
3788 scsiseq_template |= ENRSELI;
3789 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
3790
3791 /*
3792 * Load the Sequencer program and Enable the adapter
3793 * in "fast" mode.
3794 */
3795 #ifdef AHC_DEBUG
3796 printf("%s: Downloading Sequencer Program...",
3797 ahc_name(ahc));
3798 #endif
3799
3800 ahc_loadseq(ahc);
3801
3802 /* We have to wait until after any system dumps... */
3803 shutdownhook_establish(ahc_shutdown, ahc);
3804
3805 return (0);
3806 }
3807
3808 static int
3809 ahc_ioctl(struct scsipi_channel *channel, u_long cmd, caddr_t addr, int flag,
3810 struct proc *p)
3811 {
3812 struct ahc_softc *ahc = (void *)channel->chan_adapter->adapt_dev;
3813 int s, ret = ENOTTY;
3814
3815 switch (cmd) {
3816 case SCBUSIORESET:
3817 s = splbio();
3818 ahc_reset_channel(ahc, channel->chan_channel == 1 ? 'B' : 'A',
3819 TRUE);
3820 splx(s);
3821 ret = 0;
3822 break;
3823 default:
3824 break;
3825 }
3826
3827 return ret;
3828 }
3829
3830
3831 /*
3832 * XXX fvdl the busy_tcl checks and settings should only be done
3833 * for the non-tagged queueing case, but we don't do tagged queueing
3834 * yet, so..
3835 */
3836 static void
3837 ahc_action(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
3838 {
3839 struct scsipi_xfer *xs;
3840 struct scsipi_periph *periph;
3841 struct ahc_softc *ahc = (void *)chan->chan_adapter->adapt_dev;
3842 struct scb *scb;
3843 struct hardware_scb *hscb;
3844 struct ahc_initiator_tinfo *tinfo;
3845 struct tmode_tstate *tstate;
3846 u_int target_id;
3847 u_int our_id;
3848 int s, tcl;
3849 u_int16_t mask;
3850 char channel;
3851
3852 switch (req) {
3853 case ADAPTER_REQ_RUN_XFER:
3854 xs = arg;
3855 periph = xs->xs_periph;
3856
3857 SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("ahc_action\n"));
3858
3859 /* must protect the queue */
3860 s = splbio();
3861
3862 tcl = XS_TCL(ahc, xs);
3863
3864 if (!ahc_istagged_device(ahc, xs, 0) &&
3865 ahc_index_busy_tcl(ahc, tcl, FALSE) != SCB_LIST_NULL) {
3866 panic("ahc_action: not tagged and device busy");
3867 }
3868
3869
3870 target_id = periph->periph_target;
3871 our_id = SIM_SCSI_ID(ahc, periph);
3872
3873 /*
3874 * get an scb to use.
3875 */
3876 if ((scb = ahcgetscb(ahc)) == NULL) {
3877 xs->error = XS_RESOURCE_SHORTAGE;
3878 scsipi_done(xs);
3879 splx(s);
3880 return;
3881 }
3882
3883 tcl = XS_TCL(ahc, xs);
3884
3885 #ifdef DIAGNOSTIC
3886 if (!ahc_istagged_device(ahc, xs, 0) &&
3887 ahc_index_busy_tcl(ahc, tcl, FALSE) != SCB_LIST_NULL)
3888 panic("ahc: queuing for busy target");
3889 #endif
3890
3891 scb->xs = xs;
3892 hscb = scb->hscb;
3893 hscb->tcl = tcl;
3894
3895 if (xs->xs_tag_type) {
3896 #ifdef DIAGNOSTIC
3897 if (ahc_istagged_device(ahc, xs, 0) == 0)
3898 panic("ahc_action: taggged command for untagged device");
3899 #endif
3900 scb->hscb->control |= TAG_ENB;
3901 } else
3902 ahc_busy_tcl(ahc, scb);
3903
3904 splx(s);
3905
3906 channel = SIM_CHANNEL(ahc, periph);
3907 if (ahc->inited_channels[channel - 'A'] == 0) {
3908 if ((channel == 'A' &&
3909 (ahc->flags & AHC_RESET_BUS_A)) ||
3910 (channel == 'B' &&
3911 (ahc->flags & AHC_RESET_BUS_B))) {
3912 s = splbio();
3913 ahc_reset_channel(ahc, channel, TRUE);
3914 splx(s);
3915 }
3916 ahc->inited_channels[channel - 'A'] = 1;
3917 }
3918
3919 /*
3920 * Put all the arguments for the xfer in the scb
3921 */
3922
3923 mask = SCB_TARGET_MASK(scb);
3924 tinfo = ahc_fetch_transinfo(ahc,
3925 SIM_CHANNEL(ahc, periph),
3926 our_id, target_id, &tstate);
3927 if (ahc->inited_targets[target_id] == 0) {
3928 struct ahc_devinfo devinfo;
3929 s = splbio();
3930 ahc_compile_devinfo(&devinfo, our_id, target_id,
3931 periph->periph_lun,
3932 SIM_CHANNEL(ahc, periph),
3933 ROLE_INITIATOR);
3934 ahc_update_target_msg_request(ahc, &devinfo, tinfo,
3935 TRUE, FALSE);
3936 ahc->inited_targets[target_id] = 1;
3937 splx(s);
3938 }
3939 hscb->scsirate = tinfo->scsirate;
3940 hscb->scsioffset = tinfo->current.offset;
3941 if ((tstate->ultraenb & mask) != 0)
3942 hscb->control |= ULTRAENB;
3943
3944 if ((tstate->discenable & mask) != 0)
3945 hscb->control |= DISCENB;
3946
3947 if (xs->xs_control & XS_CTL_RESET) {
3948 hscb->cmdpointer = 0;
3949 scb->flags |= SCB_DEVICE_RESET;
3950 hscb->control |= MK_MESSAGE;
3951 ahc_execute_scb(scb, NULL, 0);
3952 }
3953
3954 ahc_setup_data(ahc, xs, scb);
3955 return;
3956 case ADAPTER_REQ_GROW_RESOURCES:
3957 /* XXX not supported */
3958 return;
3959 case ADAPTER_REQ_SET_XFER_MODE:
3960 {
3961 struct scsipi_xfer_mode *xm = arg;
3962 struct ahc_devinfo devinfo;
3963 int target_id, our_id;
3964 char channel;
3965
3966 target_id = xm->xm_target;
3967 our_id = chan->chan_id;
3968 channel = (chan->chan_channel == 1) ? 'B' : 'A';
3969 s = splbio();
3970 tinfo = ahc_fetch_transinfo(ahc, channel, our_id, target_id,
3971 &tstate);
3972 ahc_compile_devinfo(&devinfo, our_id, target_id,
3973 0, channel, ROLE_INITIATOR);
3974 ahc->inited_targets[target_id] = 2;
3975 if (xm->xm_mode & PERIPH_CAP_TQING &&
3976 (tstate->tagdisable & devinfo.target_mask) == 0) {
3977 ahc_set_tags(ahc, &devinfo, TRUE);
3978 }
3979 splx(s);
3980 ahc_update_xfer_mode(ahc, &devinfo);
3981 }
3982 }
3983 }
3984
3985 static void
3986 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
3987 {
3988 struct scb *scb;
3989 struct scsipi_xfer *xs;
3990 struct ahc_softc *ahc;
3991 int s;
3992
3993 scb = (struct scb *)arg;
3994 xs = scb->xs;
3995 ahc = (void *)xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
3996
3997
3998 if (nsegments != 0) {
3999 struct ahc_dma_seg *sg;
4000 bus_dma_segment_t *end_seg;
4001 int op;
4002
4003 end_seg = dm_segs + nsegments;
4004
4005 /* Copy the first SG into the data pointer area */
4006 scb->hscb->data = dm_segs->ds_addr;
4007 scb->hscb->datalen = dm_segs->ds_len;
4008
4009 /* Copy the segments into our SG list */
4010 sg = scb->sg_list;
4011 while (dm_segs < end_seg) {
4012 sg->addr = dm_segs->ds_addr;
4013 sg->len = dm_segs->ds_len;
4014 ahc_swap_sg(sg);
4015 sg++;
4016 dm_segs++;
4017 }
4018
4019 /* Note where to find the SG entries in bus space */
4020 scb->hscb->SG_pointer = scb->sg_list_phys;
4021
4022 if (xs->xs_control & XS_CTL_DATA_IN)
4023 op = BUS_DMASYNC_PREREAD;
4024 else
4025 op = BUS_DMASYNC_PREWRITE;
4026
4027 bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
4028 scb->dmamap->dm_mapsize, op);
4029
4030 } else {
4031 scb->hscb->SG_pointer = 0;
4032 scb->hscb->data = 0;
4033 scb->hscb->datalen = 0;
4034 }
4035
4036 scb->sg_count = scb->hscb->SG_count = nsegments;
4037
4038 s = splbio();
4039
4040 /*
4041 * Last time we need to check if this SCB needs to
4042 * be aborted.
4043 */
4044 if (xs->xs_status & XS_STS_DONE) {
4045 if (!ahc_istagged_device(ahc, xs, 0))
4046 ahc_index_busy_tcl(ahc, scb->hscb->tcl, TRUE);
4047 if (nsegments != 0)
4048 bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
4049 ahcfreescb(ahc, scb);
4050 splx(s);
4051 return;
4052 }
4053
4054 #ifdef DIAGNOSTIC
4055 if (scb->sg_count > 255)
4056 panic("ahc bad sg_count");
4057 #endif
4058
4059 ahc_swap_hscb(scb->hscb);
4060
4061 LIST_INSERT_HEAD(&ahc->pending_ccbs, scb, plinks);
4062
4063 scb->flags |= SCB_ACTIVE;
4064
4065 if (!(xs->xs_control & XS_CTL_POLL))
4066 callout_reset(&scb->xs->xs_callout, (xs->timeout * hz) / 1000,
4067 ahc_timeout, scb);
4068
4069 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
4070 #if 0
4071 printf("Continueing Immediate Command %d:%d\n",
4072 xs->xs_periph->periph_target,
4073 xs->xs_periph->periph_lun);
4074 #endif
4075 pause_sequencer(ahc);
4076 if ((ahc->flags & AHC_PAGESCBS) == 0)
4077 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
4078 ahc_outb(ahc, SCB_TAG, scb->hscb->tag);
4079 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
4080 unpause_sequencer(ahc);
4081 } else {
4082
4083 #if 0
4084 printf("tag %x at qpos %u vaddr %p paddr 0x%lx\n",
4085 scb->hscb->tag, ahc->qinfifonext,
4086 &ahc->qinfifo[ahc->qinfifonext],
4087 ahc->shared_data_busaddr + 1024 + ahc->qinfifonext);
4088 #endif
4089
4090 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
4091
4092 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
4093 QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
4094
4095 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
4096 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
4097 } else {
4098 pause_sequencer(ahc);
4099 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
4100 unpause_sequencer(ahc);
4101 }
4102 }
4103
4104 #ifdef AHC_DEBUG
4105 if (ahc_debug & AHC_SHOWCMDS) {
4106 scsi_print_addr(xs->xs_periph);
4107 printf("opcode %d tag %x len %d flags %x control %x fpos %u"
4108 " rate %x\n",
4109 xs->cmdstore.opcode, scb->hscb->tag, scb->hscb->datalen,
4110 scb->flags, scb->hscb->control, ahc->qinfifonext,
4111 scb->hscb->scsirate);
4112 }
4113 #endif
4114
4115 if (!(xs->xs_control & XS_CTL_POLL)) {
4116 splx(s);
4117 return;
4118 }
4119 /*
4120 * If we can't use interrupts, poll for completion
4121 */
4122 SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("cmd_poll\n"));
4123 do {
4124 if (ahc_poll(ahc, xs->timeout)) {
4125 if (!(xs->xs_control & XS_CTL_SILENT))
4126 printf("cmd fail\n");
4127 ahc_timeout(scb);
4128 break;
4129 }
4130 } while (!(xs->xs_status & XS_STS_DONE));
4131 splx(s);
4132 return;
4133 }
4134
4135 static int
4136 ahc_poll(struct ahc_softc *ahc, int wait)
4137 {
4138 while (--wait) {
4139 DELAY(1000);
4140 if (ahc_inb(ahc, INTSTAT) & INT_PEND)
4141 break;
4142 }
4143
4144 if (wait == 0) {
4145 printf("%s: board is not responding\n", ahc_name(ahc));
4146 return (EIO);
4147 }
4148
4149 ahc_intr((void *)ahc);
4150 return (0);
4151 }
4152
4153 static void
4154 ahc_setup_data(struct ahc_softc *ahc, struct scsipi_xfer *xs,
4155 struct scb *scb)
4156 {
4157 struct hardware_scb *hscb;
4158
4159 hscb = scb->hscb;
4160 xs->resid = xs->status = 0;
4161
4162 hscb->cmdlen = xs->cmdlen;
4163 memcpy(hscb->cmdstore, xs->cmd, xs->cmdlen);
4164 hscb->cmdpointer = hscb->cmdstore_busaddr;
4165
4166 /* Only use S/G if there is a transfer */
4167 if (xs->datalen) {
4168 int error;
4169
4170 error = bus_dmamap_load(ahc->parent_dmat,
4171 scb->dmamap, xs->data,
4172 xs->datalen, NULL,
4173 ((xs->xs_control & XS_CTL_NOSLEEP) ?
4174 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
4175 BUS_DMA_STREAMING);
4176 if (error) {
4177 if (!ahc_istagged_device(ahc, xs, 0))
4178 ahc_index_busy_tcl(ahc, hscb->tcl, TRUE);
4179 xs->error = XS_RESOURCE_SHORTAGE; /* XXX fvdl */
4180 scsipi_done(xs);
4181 return;
4182 }
4183 ahc_execute_scb(scb,
4184 scb->dmamap->dm_segs,
4185 scb->dmamap->dm_nsegs);
4186 } else {
4187 ahc_execute_scb(scb, NULL, 0);
4188 }
4189 }
4190
4191 static void
4192 ahc_freeze_devq(struct ahc_softc *ahc, struct scsipi_periph *periph)
4193 {
4194 int target;
4195 char channel;
4196 int lun;
4197
4198 target = periph->periph_target;
4199 lun = periph->periph_lun;
4200 channel = periph->periph_channel->chan_channel;
4201
4202 ahc_search_qinfifo(ahc, target, channel, lun,
4203 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
4204 SCB_REQUEUE, SEARCH_COMPLETE);
4205 }
4206
4207 static void
4208 ahcallocscbs(struct ahc_softc *ahc)
4209 {
4210 struct scb_data *scb_data;
4211 struct scb *next_scb;
4212 struct sg_map_node *sg_map;
4213 bus_addr_t physaddr;
4214 struct ahc_dma_seg *segs;
4215 int newcount;
4216 int i;
4217
4218 scb_data = ahc->scb_data;
4219 if (scb_data->numscbs >= AHC_SCB_MAX)
4220 /* Can't allocate any more */
4221 return;
4222
4223 next_scb = &scb_data->scbarray[scb_data->numscbs];
4224
4225 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
4226
4227 if (sg_map == NULL)
4228 return;
4229
4230 if (ahc_createdmamem(ahc->parent_dmat, PAGE_SIZE, ahc->sc_dmaflags,
4231 &sg_map->sg_dmamap,
4232 (caddr_t *)&sg_map->sg_vaddr, &sg_map->sg_physaddr,
4233 &sg_map->sg_dmasegs, &sg_map->sg_nseg, ahc_name(ahc),
4234 "SG space") < 0) {
4235 free(sg_map, M_DEVBUF);
4236 return;
4237 }
4238
4239 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
4240
4241 segs = sg_map->sg_vaddr;
4242 physaddr = sg_map->sg_physaddr;
4243
4244 newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
4245 for (i = 0; scb_data->numscbs < AHC_SCB_MAX && i < newcount; i++) {
4246 int error;
4247
4248 next_scb->sg_list = segs;
4249 /*
4250 * The sequencer always starts with the second entry.
4251 * The first entry is embedded in the scb.
4252 */
4253 next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
4254 next_scb->flags = SCB_FREE;
4255 error = bus_dmamap_create(ahc->parent_dmat,
4256 AHC_MAXTRANSFER_SIZE, AHC_NSEG, MAXBSIZE, 0,
4257 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW|ahc->sc_dmaflags,
4258 &next_scb->dmamap);
4259 if (error != 0)
4260 break;
4261 next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
4262 next_scb->hscb->tag = ahc->scb_data->numscbs;
4263 next_scb->hscb->cmdstore_busaddr =
4264 ahc_hscb_busaddr(ahc, next_scb->hscb->tag)
4265 + offsetof(struct hardware_scb, cmdstore);
4266 next_scb->hscb->cmdstore_busaddr =
4267 htole32(next_scb->hscb->cmdstore_busaddr);
4268 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, next_scb, links);
4269 segs += AHC_NSEG;
4270 physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
4271 next_scb++;
4272 ahc->scb_data->numscbs++;
4273 }
4274 #ifdef AHC_DEBUG
4275 if (ahc_debug & AHC_SHOWSCBALLOC)
4276 printf("%s: allocated %d new SCBs count now %d\n",
4277 ahc_name(ahc), i - 1, ahc->scb_data->numscbs);
4278 #endif
4279 }
4280
4281 #ifdef AHC_DUMP_SEQ
4282 static void
4283 ahc_dumpseq(struct ahc_softc* ahc)
4284 {
4285 int i;
4286 int max_prog;
4287
4288 if ((ahc->chip & AHC_BUS_MASK) < AHC_PCI)
4289 max_prog = 448;
4290 else if ((ahc->features & AHC_ULTRA2) != 0)
4291 max_prog = 768;
4292 else
4293 max_prog = 512;
4294
4295 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
4296 ahc_outb(ahc, SEQADDR0, 0);
4297 ahc_outb(ahc, SEQADDR1, 0);
4298 for (i = 0; i < max_prog; i++) {
4299 u_int8_t ins_bytes[4];
4300
4301 ahc_insb(ahc, SEQRAM, ins_bytes, 4);
4302 printf("0x%08x\n", ins_bytes[0] << 24
4303 | ins_bytes[1] << 16
4304 | ins_bytes[2] << 8
4305 | ins_bytes[3]);
4306 }
4307 }
4308 #endif
4309
4310 static void
4311 ahc_loadseq(struct ahc_softc *ahc)
4312 {
4313 const struct patch *cur_patch;
4314 int i;
4315 int downloaded;
4316 int skip_addr;
4317 u_int8_t download_consts[4];
4318
4319 /* Setup downloadable constant table */
4320 #if 0
4321 /* No downloaded constants are currently defined. */
4322 download_consts[TMODE_NUMCMDS] = ahc->num_targetcmds;
4323 #endif
4324
4325 cur_patch = patches;
4326 downloaded = 0;
4327 skip_addr = 0;
4328 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
4329 ahc_outb(ahc, SEQADDR0, 0);
4330 ahc_outb(ahc, SEQADDR1, 0);
4331
4332 for (i = 0; i < sizeof(seqprog)/4; i++) {
4333 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
4334 /*
4335 * Don't download this instruction as it
4336 * is in a patch that was removed.
4337 */
4338 continue;
4339 }
4340 ahc_download_instr(ahc, i, download_consts);
4341 downloaded++;
4342 }
4343 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
4344 restart_sequencer(ahc);
4345
4346 #ifdef AHC_DEBUG
4347 printf(" %d instructions downloaded\n", downloaded);
4348 #endif
4349 }
4350
4351 static int
4352 ahc_check_patch(struct ahc_softc *ahc, const struct patch **start_patch,
4353 int start_instr, int *skip_addr)
4354 {
4355 const struct patch *cur_patch;
4356 const struct patch *last_patch;
4357 int num_patches;
4358
4359 num_patches = sizeof(patches)/sizeof(struct patch);
4360 last_patch = &patches[num_patches];
4361 cur_patch = *start_patch;
4362
4363 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
4364
4365 if (cur_patch->patch_func(ahc) == 0) {
4366
4367 /* Start rejecting code */
4368 *skip_addr = start_instr + cur_patch->skip_instr;
4369 cur_patch += cur_patch->skip_patch;
4370 } else {
4371 /* Accepted this patch. Advance to the next
4372 * one and wait for our intruction pointer to
4373 * hit this point.
4374 */
4375 cur_patch++;
4376 }
4377 }
4378
4379 *start_patch = cur_patch;
4380 if (start_instr < *skip_addr)
4381 /* Still skipping */
4382 return (0);
4383
4384 return (1);
4385 }
4386
4387 static void
4388 ahc_download_instr(struct ahc_softc *ahc, int instrptr, u_int8_t *dconsts)
4389 {
4390 union ins_formats instr;
4391 struct ins_format1 *fmt1_ins;
4392 struct ins_format3 *fmt3_ins;
4393 u_int opcode;
4394
4395 /* Structure copy */
4396 memcpy(&instr, &seqprog[instrptr * 4], sizeof instr);
4397
4398 instr.integer = le32toh(instr.integer);
4399
4400 fmt1_ins = &instr.format1;
4401 fmt3_ins = NULL;
4402
4403 /* Pull the opcode */
4404 opcode = instr.format1.opcode;
4405 switch (opcode) {
4406 case AIC_OP_JMP:
4407 case AIC_OP_JC:
4408 case AIC_OP_JNC:
4409 case AIC_OP_CALL:
4410 case AIC_OP_JNE:
4411 case AIC_OP_JNZ:
4412 case AIC_OP_JE:
4413 case AIC_OP_JZ:
4414 {
4415 const struct patch *cur_patch;
4416 int address_offset;
4417 u_int address;
4418 int skip_addr;
4419 int i;
4420
4421 fmt3_ins = &instr.format3;
4422 address_offset = 0;
4423 address = fmt3_ins->address;
4424 cur_patch = patches;
4425 skip_addr = 0;
4426
4427 for (i = 0; i < address;) {
4428
4429 ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
4430
4431 if (skip_addr > i) {
4432 int end_addr;
4433
4434 end_addr = MIN(address, skip_addr);
4435 address_offset += end_addr - i;
4436 i = skip_addr;
4437 } else {
4438 i++;
4439 }
4440 }
4441 address -= address_offset;
4442 fmt3_ins->address = address;
4443 /* FALLTHROUGH */
4444 }
4445 case AIC_OP_OR:
4446 case AIC_OP_AND:
4447 case AIC_OP_XOR:
4448 case AIC_OP_ADD:
4449 case AIC_OP_ADC:
4450 case AIC_OP_BMOV:
4451 if (fmt1_ins->parity != 0) {
4452 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
4453 }
4454 fmt1_ins->parity = 0;
4455 /* FALLTHROUGH */
4456 case AIC_OP_ROL:
4457 if ((ahc->features & AHC_ULTRA2) != 0) {
4458 int i, count;
4459
4460 /* Calculate odd parity for the instruction */
4461 for (i = 0, count = 0; i < 31; i++) {
4462 u_int32_t mask;
4463
4464 mask = 0x01 << i;
4465 if ((instr.integer & mask) != 0)
4466 count++;
4467 }
4468 if ((count & 0x01) == 0)
4469 instr.format1.parity = 1;
4470 } else {
4471 /* Compress the instruction for older sequencers */
4472 if (fmt3_ins != NULL) {
4473 instr.integer =
4474 fmt3_ins->immediate
4475 | (fmt3_ins->source << 8)
4476 | (fmt3_ins->address << 16)
4477 | (fmt3_ins->opcode << 25);
4478 } else {
4479 instr.integer =
4480 fmt1_ins->immediate
4481 | (fmt1_ins->source << 8)
4482 | (fmt1_ins->destination << 16)
4483 | (fmt1_ins->ret << 24)
4484 | (fmt1_ins->opcode << 25);
4485 }
4486 }
4487 instr.integer = htole32(instr.integer);
4488 ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
4489 break;
4490 default:
4491 panic("Unknown opcode encountered in seq program");
4492 break;
4493 }
4494 }
4495
4496 static void
4497 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb)
4498 {
4499
4500 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
4501 struct scb *scbp;
4502
4503 scb->flags |= SCB_RECOVERY_SCB;
4504
4505 /*
4506 * Take all queued, but not sent SCBs out of the equation.
4507 * Also ensure that no new CCBs are queued to us while we
4508 * try to fix this problem.
4509 */
4510 scsipi_channel_freeze(&ahc->sc_channel, 1);
4511 if (ahc->features & AHC_TWIN)
4512 scsipi_channel_freeze(&ahc->sc_channel_b, 1);
4513
4514 /*
4515 * Go through all of our pending SCBs and remove
4516 * any scheduled timeouts for them. We will reschedule
4517 * them after we've successfully fixed this problem.
4518 */
4519 scbp = ahc->pending_ccbs.lh_first;
4520 while (scbp != NULL) {
4521 callout_stop(&scbp->xs->xs_callout);
4522 scbp = scbp->plinks.le_next;
4523 }
4524 }
4525 }
4526
4527 static void
4528 ahc_timeout(void *arg)
4529 {
4530 struct scb *scb;
4531 struct ahc_softc *ahc;
4532 int s, found;
4533 u_int last_phase;
4534 int target;
4535 int lun;
4536 int i;
4537 char channel;
4538
4539 scb = (struct scb *)arg;
4540 ahc =
4541 (void *)scb->xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
4542
4543 s = splbio();
4544
4545 /*
4546 * Ensure that the card doesn't do anything
4547 * behind our back. Also make sure that we
4548 * didn't "just" miss an interrupt that would
4549 * affect this timeout.
4550 */
4551 do {
4552 ahc_intr(ahc);
4553 pause_sequencer(ahc);
4554 } while (ahc_inb(ahc, INTSTAT) & INT_PEND);
4555
4556 if ((scb->flags & SCB_ACTIVE) == 0) {
4557 /* Previous timeout took care of me already */
4558 printf("Timedout SCB handled by another timeout\n");
4559 unpause_sequencer(ahc);
4560 splx(s);
4561 return;
4562 }
4563
4564 target = SCB_TARGET(scb);
4565 channel = SCB_CHANNEL(scb);
4566 lun = SCB_LUN(scb);
4567
4568 scsipi_printaddr(scb->xs->xs_periph);
4569 printf("SCB %x - timed out ", scb->hscb->tag);
4570 /*
4571 * Take a snapshot of the bus state and print out
4572 * some information so we can track down driver bugs.
4573 */
4574 last_phase = ahc_inb(ahc, LASTPHASE);
4575
4576 for (i = 0; i < num_phases; i++) {
4577 if (last_phase == phase_table[i].phase)
4578 break;
4579 }
4580 printf("%s", phase_table[i].phasemsg);
4581
4582 printf(", SEQADDR == 0x%x\n",
4583 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
4584 printf("SCSIRATE == 0x%x\n", ahc_inb(ahc, SCSIRATE));
4585
4586 #ifdef AHC_DEBUG
4587 ahc_print_scb(scb);
4588 #endif
4589
4590 #if 0
4591 printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1));
4592 printf("SSTAT3 == 0x%x\n", ahc_inb(ahc, SSTAT3));
4593 printf("SCSIPHASE == 0x%x\n", ahc_inb(ahc, SCSIPHASE));
4594 printf("SCSIOFFSET == 0x%x\n", ahc_inb(ahc, SCSIOFFSET));
4595 printf("SEQ_FLAGS == 0x%x\n", ahc_inb(ahc, SEQ_FLAGS));
4596 printf("SCB_DATAPTR == 0x%x\n", ahc_inb(ahc, SCB_DATAPTR)
4597 | ahc_inb(ahc, SCB_DATAPTR + 1) << 8
4598 | ahc_inb(ahc, SCB_DATAPTR + 2) << 16
4599 | ahc_inb(ahc, SCB_DATAPTR + 3) << 24);
4600 printf("SCB_DATACNT == 0x%x\n", ahc_inb(ahc, SCB_DATACNT)
4601 | ahc_inb(ahc, SCB_DATACNT + 1) << 8
4602 | ahc_inb(ahc, SCB_DATACNT + 2) << 16);
4603 printf("SCB_SGCOUNT == 0x%x\n", ahc_inb(ahc, SCB_SGCOUNT));
4604 printf("CCSCBCTL == 0x%x\n", ahc_inb(ahc, CCSCBCTL));
4605 printf("CCSCBCNT == 0x%x\n", ahc_inb(ahc, CCSCBCNT));
4606 printf("DFCNTRL == 0x%x\n", ahc_inb(ahc, DFCNTRL));
4607 printf("DFSTATUS == 0x%x\n", ahc_inb(ahc, DFSTATUS));
4608 printf("CCHCNT == 0x%x\n", ahc_inb(ahc, CCHCNT));
4609 if (scb->sg_count > 0) {
4610 for (i = 0; i < scb->sg_count; i++) {
4611 printf("sg[%d] - Addr 0x%x : Length %d\n",
4612 i,
4613 le32toh(scb->sg_list[i].addr),
4614 le32toh(scb->sg_list[i].len));
4615 }
4616 }
4617 #endif
4618 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
4619 /*
4620 * Been down this road before.
4621 * Do a full bus reset.
4622 */
4623 bus_reset:
4624 ahcsetccbstatus(scb->xs, XS_TIMEOUT);
4625 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
4626 printf("%s: Issued Channel %c Bus Reset. "
4627 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
4628 } else {
4629 /*
4630 * If we are a target, transition to bus free and report
4631 * the timeout.
4632 *
4633 * The target/initiator that is holding up the bus may not
4634 * be the same as the one that triggered this timeout
4635 * (different commands have different timeout lengths).
4636 * If the bus is idle and we are actiing as the initiator
4637 * for this request, queue a BDR message to the timed out
4638 * target. Otherwise, if the timed out transaction is
4639 * active:
4640 * Initiator transaction:
4641 * Stuff the message buffer with a BDR message and assert
4642 * ATN in the hopes that the target will let go of the bus
4643 * and go to the mesgout phase. If this fails, we'll
4644 * get another timeout 2 seconds later which will attempt
4645 * a bus reset.
4646 *
4647 * Target transaction:
4648 * Transition to BUS FREE and report the error.
4649 * It's good to be the target!
4650 */
4651 u_int active_scb_index;
4652
4653 active_scb_index = ahc_inb(ahc, SCB_TAG);
4654
4655 if (last_phase != P_BUSFREE
4656 && (active_scb_index < ahc->scb_data->numscbs)) {
4657 struct scb *active_scb;
4658
4659 /*
4660 * If the active SCB is not from our device,
4661 * assume that another device is hogging the bus
4662 * and wait for it's timeout to expire before
4663 * taking additional action.
4664 */
4665 active_scb = &ahc->scb_data->scbarray[active_scb_index];
4666 if (active_scb->hscb->tcl != scb->hscb->tcl) {
4667 u_int newtimeout;
4668
4669 scsipi_printaddr(scb->xs->xs_periph);
4670 printf("Other SCB Timeout%s",
4671 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
4672 ? " again\n" : "\n");
4673 scb->flags |= SCB_OTHERTCL_TIMEOUT;
4674 newtimeout = MAX(active_scb->xs->timeout,
4675 scb->xs->timeout);
4676 callout_reset(&scb->xs->xs_callout,
4677 (newtimeout * hz) / 1000,
4678 ahc_timeout, scb);
4679 splx(s);
4680 return;
4681 }
4682
4683 /* It's us */
4684 if ((scb->hscb->control & TARGET_SCB) != 0) {
4685
4686 /*
4687 * Send back any queued up transactions
4688 * and properly record the error condition.
4689 */
4690 ahc_freeze_devq(ahc, scb->xs->xs_periph);
4691 ahcsetccbstatus(scb->xs, XS_TIMEOUT);
4692 ahc_freeze_ccb(scb);
4693 ahc_done(ahc, scb);
4694
4695 /* Will clear us from the bus */
4696 restart_sequencer(ahc);
4697 splx(s);
4698 return;
4699 }
4700
4701 ahc_set_recoveryscb(ahc, active_scb);
4702 ahc_outb(ahc, MSG_OUT, MSG_BUS_DEV_RESET);
4703 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
4704 scsipi_printaddr(active_scb->xs->xs_periph);
4705 printf("BDR message in message buffer\n");
4706 active_scb->flags |= SCB_DEVICE_RESET;
4707 callout_reset(&active_scb->xs->xs_callout,
4708 2 * hz, ahc_timeout, active_scb);
4709 unpause_sequencer(ahc);
4710 } else {
4711 int disconnected;
4712
4713 /* XXX Shouldn't panic. Just punt instead */
4714 if ((scb->hscb->control & TARGET_SCB) != 0)
4715 panic("Timed-out target SCB but bus idle");
4716
4717 if (last_phase != P_BUSFREE
4718 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
4719 /* XXX What happened to the SCB? */
4720 /* Hung target selection. Goto busfree */
4721 printf("%s: Hung target selection\n",
4722 ahc_name(ahc));
4723 restart_sequencer(ahc);
4724 splx(s);
4725 return;
4726 }
4727
4728 if (ahc_search_qinfifo(ahc, target, channel, lun,
4729 scb->hscb->tag, ROLE_INITIATOR,
4730 /*status*/0, SEARCH_COUNT) > 0) {
4731 disconnected = FALSE;
4732 } else {
4733 disconnected = TRUE;
4734 }
4735
4736 if (disconnected) {
4737 u_int active_scb;
4738
4739 ahc_set_recoveryscb(ahc, scb);
4740 /*
4741 * Simply set the MK_MESSAGE control bit.
4742 */
4743 scb->hscb->control |= MK_MESSAGE;
4744 scb->flags |= SCB_QUEUED_MSG
4745 | SCB_DEVICE_RESET;
4746
4747 /*
4748 * Mark the cached copy of this SCB in the
4749 * disconnected list too, so that a reconnect
4750 * at this point causes a BDR or abort.
4751 */
4752 active_scb = ahc_inb(ahc, SCBPTR);
4753 if (ahc_search_disc_list(ahc, target,
4754 channel, lun,
4755 scb->hscb->tag,
4756 /*stop_on_first*/TRUE,
4757 /*remove*/FALSE,
4758 /*save_state*/FALSE)) {
4759 u_int scb_control;
4760
4761 scb_control = ahc_inb(ahc, SCB_CONTROL);
4762 scb_control |= MK_MESSAGE;
4763 ahc_outb(ahc, SCB_CONTROL, scb_control);
4764 }
4765 ahc_outb(ahc, SCBPTR, active_scb);
4766 ahc_index_busy_tcl(ahc, scb->hscb->tcl,
4767 /*unbusy*/TRUE);
4768
4769 /*
4770 * Actually re-queue this SCB in case we can
4771 * select the device before it reconnects.
4772 * Clear out any entries in the QINFIFO first
4773 * so we are the next SCB for this target
4774 * to run.
4775 */
4776 ahc_search_qinfifo(ahc, SCB_TARGET(scb),
4777 channel, SCB_LUN(scb),
4778 SCB_LIST_NULL,
4779 ROLE_INITIATOR,
4780 SCB_REQUEUE,
4781 SEARCH_COMPLETE);
4782 scsipi_printaddr(scb->xs->xs_periph);
4783 printf("Queuing a BDR SCB\n");
4784 ahc->qinfifo[ahc->qinfifonext++] =
4785 scb->hscb->tag;
4786
4787 bus_dmamap_sync(ahc->parent_dmat,
4788 ahc->shared_data_dmamap,
4789 QINFIFO_OFFSET * 256, 256,
4790 BUS_DMASYNC_PREWRITE);
4791
4792 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
4793 ahc_outb(ahc, HNSCB_QOFF,
4794 ahc->qinfifonext);
4795 } else {
4796 ahc_outb(ahc, KERNEL_QINPOS,
4797 ahc->qinfifonext);
4798 }
4799 callout_reset(&scb->xs->xs_callout, 2 * hz,
4800 ahc_timeout, scb);
4801 unpause_sequencer(ahc);
4802 } else {
4803 /* Go "immediatly" to the bus reset */
4804 /* This shouldn't happen */
4805 ahc_set_recoveryscb(ahc, scb);
4806 scsipi_printaddr(scb->xs->xs_periph);
4807 printf("SCB %x: Immediate reset. "
4808 "Flags = 0x%x\n", scb->hscb->tag,
4809 scb->flags);
4810 goto bus_reset;
4811 }
4812 }
4813 }
4814 splx(s);
4815 }
4816
4817 static int
4818 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
4819 int lun, u_int tag, role_t role, scb_flag status,
4820 ahc_search_action action)
4821 {
4822 struct scb *scbp;
4823 u_int8_t qinpos;
4824 u_int8_t qintail;
4825 int found;
4826
4827 qinpos = ahc_inb(ahc, QINPOS);
4828 qintail = ahc->qinfifonext;
4829 found = 0;
4830
4831 /*
4832 * Start with an empty queue. Entries that are not chosen
4833 * for removal will be re-added to the queue as we go.
4834 */
4835 ahc->qinfifonext = qinpos;
4836
4837 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
4838 QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_POSTREAD);
4839
4840 while (qinpos != qintail) {
4841 scbp = &ahc->scb_data->scbarray[ahc->qinfifo[qinpos]];
4842 if (ahc_match_scb(scbp, target, channel, lun, tag, role)) {
4843 /*
4844 * We found an scb that needs to be removed.
4845 */
4846 switch (action) {
4847 case SEARCH_COMPLETE:
4848 if (!(scbp->xs->xs_status & XS_STS_DONE)) {
4849 scbp->flags |= status;
4850 scbp->xs->error = XS_NOERROR;
4851 }
4852 ahc_freeze_ccb(scbp);
4853 ahc_done(ahc, scbp);
4854 break;
4855 case SEARCH_COUNT:
4856 ahc->qinfifo[ahc->qinfifonext++] =
4857 scbp->hscb->tag;
4858 break;
4859 case SEARCH_REMOVE:
4860 break;
4861 }
4862 found++;
4863 } else {
4864 ahc->qinfifo[ahc->qinfifonext++] = scbp->hscb->tag;
4865 }
4866 qinpos++;
4867 }
4868
4869 bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
4870 QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
4871
4872 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
4873 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
4874 } else {
4875 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
4876 }
4877
4878 return (found);
4879 }
4880
4881 /*
4882 * Abort all SCBs that match the given description (target/channel/lun/tag),
4883 * setting their status to the passed in status if the status has not already
4884 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
4885 * is paused before it is called.
4886 */
4887 static int
4888 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
4889 int lun, u_int tag, role_t role, int status)
4890 {
4891 struct scb *scbp;
4892 u_int active_scb;
4893 int i;
4894 int found;
4895
4896 /* restore this when we're done */
4897 active_scb = ahc_inb(ahc, SCBPTR);
4898
4899 found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
4900 role, SCB_REQUEUE, SEARCH_COMPLETE);
4901
4902 /*
4903 * Search waiting for selection list.
4904 */
4905 {
4906 u_int8_t next, prev;
4907
4908 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
4909 prev = SCB_LIST_NULL;
4910
4911 while (next != SCB_LIST_NULL) {
4912 u_int8_t scb_index;
4913
4914 ahc_outb(ahc, SCBPTR, next);
4915 scb_index = ahc_inb(ahc, SCB_TAG);
4916 if (scb_index >= ahc->scb_data->numscbs) {
4917 panic("Waiting List inconsistency. "
4918 "SCB index == %d, yet numscbs == %d.",
4919 scb_index, ahc->scb_data->numscbs);
4920 }
4921 scbp = &ahc->scb_data->scbarray[scb_index];
4922 if (ahc_match_scb(scbp, target, channel,
4923 lun, SCB_LIST_NULL, role)) {
4924
4925 next = ahc_abort_wscb(ahc, next, prev);
4926 } else {
4927
4928 prev = next;
4929 next = ahc_inb(ahc, SCB_NEXT);
4930 }
4931 }
4932 }
4933 /*
4934 * Go through the disconnected list and remove any entries we
4935 * have queued for completion, 0'ing their control byte too.
4936 * We save the active SCB and restore it ourselves, so there
4937 * is no reason for this search to restore it too.
4938 */
4939 ahc_search_disc_list(ahc, target, channel, lun, tag,
4940 /*stop_on_first*/FALSE, /*remove*/TRUE,
4941 /*save_state*/FALSE);
4942
4943 /*
4944 * Go through the hardware SCB array looking for commands that
4945 * were active but not on any list.
4946 */
4947 for(i = 0; i < ahc->scb_data->maxhscbs; i++) {
4948 u_int scbid;
4949
4950 ahc_outb(ahc, SCBPTR, i);
4951 scbid = ahc_inb(ahc, SCB_TAG);
4952 scbp = &ahc->scb_data->scbarray[scbid];
4953 if (scbid < ahc->scb_data->numscbs
4954 && ahc_match_scb(scbp, target, channel, lun, tag, role))
4955 ahc_add_curscb_to_free_list(ahc);
4956 }
4957
4958 /*
4959 * Go through the pending CCB list and look for
4960 * commands for this target that are still active.
4961 * These are other tagged commands that were
4962 * disconnected when the reset occured.
4963 */
4964 {
4965 struct scb *scb;
4966
4967 scb = ahc->pending_ccbs.lh_first;
4968 while (scb != NULL) {
4969 scbp = scb;
4970 scb = scb->plinks.le_next;
4971 if (ahc_match_scb(scbp, target, channel,
4972 lun, tag, role)) {
4973 if (!(scbp->xs->xs_status & XS_STS_DONE))
4974 ahcsetccbstatus(scbp->xs, status);
4975 ahc_freeze_ccb(scbp);
4976 ahc_done(ahc, scbp);
4977 found++;
4978 }
4979 }
4980 }
4981 ahc_outb(ahc, SCBPTR, active_scb);
4982 return found;
4983 }
4984
4985 static int
4986 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
4987 int lun, u_int tag, int stop_on_first, int remove,
4988 int save_state)
4989 {
4990 struct scb *scbp;
4991 u_int next;
4992 u_int prev;
4993 u_int count;
4994 u_int active_scb;
4995
4996 count = 0;
4997 next = ahc_inb(ahc, DISCONNECTED_SCBH);
4998 prev = SCB_LIST_NULL;
4999
5000 if (save_state) {
5001 /* restore this when we're done */
5002 active_scb = ahc_inb(ahc, SCBPTR);
5003 } else
5004 /* Silence compiler */
5005 active_scb = SCB_LIST_NULL;
5006
5007 while (next != SCB_LIST_NULL) {
5008 u_int scb_index;
5009
5010 ahc_outb(ahc, SCBPTR, next);
5011 scb_index = ahc_inb(ahc, SCB_TAG);
5012 if (scb_index >= ahc->scb_data->numscbs) {
5013 panic("Disconnected List inconsistency. "
5014 "SCB index == %d, yet numscbs == %d.",
5015 scb_index, ahc->scb_data->numscbs);
5016 }
5017 scbp = &ahc->scb_data->scbarray[scb_index];
5018 if (ahc_match_scb(scbp, target, channel, lun,
5019 tag, ROLE_INITIATOR)) {
5020 count++;
5021 if (remove) {
5022 next =
5023 ahc_rem_scb_from_disc_list(ahc, prev, next);
5024 } else {
5025 prev = next;
5026 next = ahc_inb(ahc, SCB_NEXT);
5027 }
5028 if (stop_on_first)
5029 break;
5030 } else {
5031 prev = next;
5032 next = ahc_inb(ahc, SCB_NEXT);
5033 }
5034 }
5035 if (save_state)
5036 ahc_outb(ahc, SCBPTR, active_scb);
5037 return (count);
5038 }
5039
5040 static u_int
5041 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
5042 {
5043 u_int next;
5044
5045 ahc_outb(ahc, SCBPTR, scbptr);
5046 next = ahc_inb(ahc, SCB_NEXT);
5047
5048 ahc_outb(ahc, SCB_CONTROL, 0);
5049
5050 ahc_add_curscb_to_free_list(ahc);
5051
5052 if (prev != SCB_LIST_NULL) {
5053 ahc_outb(ahc, SCBPTR, prev);
5054 ahc_outb(ahc, SCB_NEXT, next);
5055 } else
5056 ahc_outb(ahc, DISCONNECTED_SCBH, next);
5057
5058 return (next);
5059 }
5060
5061 static void
5062 ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
5063 {
5064 /* Invalidate the tag so that ahc_find_scb doesn't think it's active */
5065 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
5066
5067 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
5068 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
5069 }
5070
5071 /*
5072 * Manipulate the waiting for selection list and return the
5073 * scb that follows the one that we remove.
5074 */
5075 static u_int
5076 ahc_abort_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
5077 {
5078 u_int curscb, next;
5079
5080 /*
5081 * Select the SCB we want to abort and
5082 * pull the next pointer out of it.
5083 */
5084 curscb = ahc_inb(ahc, SCBPTR);
5085 ahc_outb(ahc, SCBPTR, scbpos);
5086 next = ahc_inb(ahc, SCB_NEXT);
5087
5088 /* Clear the necessary fields */
5089 ahc_outb(ahc, SCB_CONTROL, 0);
5090
5091 ahc_add_curscb_to_free_list(ahc);
5092
5093 /* update the waiting list */
5094 if (prev == SCB_LIST_NULL) {
5095 /* First in the list */
5096 ahc_outb(ahc, WAITING_SCBH, next);
5097
5098 /*
5099 * Ensure we aren't attempting to perform
5100 * selection for this entry.
5101 */
5102 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
5103 } else {
5104 /*
5105 * Select the scb that pointed to us
5106 * and update its next pointer.
5107 */
5108 ahc_outb(ahc, SCBPTR, prev);
5109 ahc_outb(ahc, SCB_NEXT, next);
5110 }
5111
5112 /*
5113 * Point us back at the original scb position.
5114 */
5115 ahc_outb(ahc, SCBPTR, curscb);
5116 return next;
5117 }
5118
5119 static void
5120 ahc_clear_intstat(struct ahc_softc *ahc)
5121 {
5122 /* Clear any interrupt conditions this may have caused */
5123 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
5124 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
5125 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
5126 CLRREQINIT);
5127 ahc_outb(ahc, CLRINT, CLRSCSIINT);
5128 }
5129
5130 static void
5131 ahc_reset_current_bus(struct ahc_softc *ahc)
5132 {
5133 u_int8_t scsiseq;
5134
5135 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
5136 scsiseq = ahc_inb(ahc, SCSISEQ);
5137 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
5138 DELAY(AHC_BUSRESET_DELAY);
5139 /* Turn off the bus reset */
5140 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
5141
5142 ahc_clear_intstat(ahc);
5143
5144 /* Re-enable reset interrupts */
5145 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
5146 }
5147
5148 static int
5149 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
5150 {
5151 u_int initiator, target, max_scsiid;
5152 u_int sblkctl;
5153 u_int our_id;
5154 int found;
5155 int restart_needed;
5156 char cur_channel;
5157
5158 ahc->pending_device = NULL;
5159
5160 pause_sequencer(ahc);
5161
5162 /*
5163 * Run our command complete fifos to ensure that we perform
5164 * completion processing on any commands that 'completed'
5165 * before the reset occurred.
5166 */
5167 ahc_run_qoutfifo(ahc);
5168
5169 /*
5170 * Reset the bus if we are initiating this reset
5171 */
5172 sblkctl = ahc_inb(ahc, SBLKCTL);
5173 cur_channel = 'A';
5174 if ((ahc->features & AHC_TWIN) != 0
5175 && ((sblkctl & SELBUSB) != 0))
5176 cur_channel = 'B';
5177 if (cur_channel != channel) {
5178 /* Case 1: Command for another bus is active
5179 * Stealthily reset the other bus without
5180 * upsetting the current bus.
5181 */
5182 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
5183 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
5184 ahc_outb(ahc, SCSISEQ,
5185 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
5186 if (initiate_reset)
5187 ahc_reset_current_bus(ahc);
5188 ahc_clear_intstat(ahc);
5189 ahc_outb(ahc, SBLKCTL, sblkctl);
5190 restart_needed = FALSE;
5191 } else {
5192 /* Case 2: A command from this bus is active or we're idle */
5193 ahc_clear_msg_state(ahc);
5194 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
5195 ahc_outb(ahc, SCSISEQ,
5196 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
5197 if (initiate_reset)
5198 ahc_reset_current_bus(ahc);
5199 ahc_clear_intstat(ahc);
5200
5201 /*
5202 * Since we are going to restart the sequencer, avoid
5203 * a race in the sequencer that could cause corruption
5204 * of our Q pointers by starting over from index 0.
5205 */
5206 ahc->qoutfifonext = 0;
5207 if ((ahc->features & AHC_QUEUE_REGS) != 0)
5208 ahc_outb(ahc, SDSCB_QOFF, 0);
5209 else
5210 ahc_outb(ahc, QOUTPOS, 0);
5211 restart_needed = TRUE;
5212 }
5213
5214 /*
5215 * Clean up all the state information for the
5216 * pending transactions on this bus.
5217 */
5218 found = ahc_abort_scbs(ahc, AHC_TARGET_WILDCARD, channel,
5219 AHC_LUN_WILDCARD, SCB_LIST_NULL,
5220 ROLE_UNKNOWN, XS_RESET);
5221 if (channel == 'B') {
5222 our_id = ahc->our_id_b;
5223 } else {
5224 our_id = ahc->our_id;
5225 }
5226
5227 max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
5228
5229 /*
5230 * Revert to async/narrow transfers until we renegotiate.
5231 */
5232 for (target = 0; target <= max_scsiid; target++) {
5233
5234 if (ahc->enabled_targets[target] == NULL)
5235 continue;
5236 for (initiator = 0; initiator <= max_scsiid; initiator++) {
5237 struct ahc_devinfo devinfo;
5238
5239 ahc_compile_devinfo(&devinfo, target, initiator,
5240 AHC_LUN_WILDCARD,
5241 channel, ROLE_UNKNOWN);
5242 ahc_set_width(ahc, &devinfo,
5243 MSG_EXT_WDTR_BUS_8_BIT,
5244 AHC_TRANS_CUR, /*paused*/TRUE, FALSE);
5245 ahc_set_syncrate(ahc, &devinfo,
5246 /*syncrate*/NULL, /*period*/0,
5247 /*offset*/0, AHC_TRANS_CUR,
5248 /*paused*/TRUE, FALSE);
5249 ahc_update_xfer_mode(ahc, &devinfo);
5250 }
5251 }
5252
5253 if (restart_needed)
5254 restart_sequencer(ahc);
5255 else
5256 unpause_sequencer(ahc);
5257 return found;
5258 }
5259
5260 static int
5261 ahc_match_scb(struct scb *scb, int target, char channel,
5262 int lun, u_int tag, role_t role)
5263 {
5264 int targ = SCB_TARGET(scb);
5265 char chan = SCB_CHANNEL(scb);
5266 int slun = SCB_LUN(scb);
5267 int match;
5268
5269 match = ((chan == channel) || (channel == ALL_CHANNELS));
5270 if (match != 0)
5271 match = ((targ == target) || (target == AHC_TARGET_WILDCARD));
5272 if (match != 0)
5273 match = ((lun == slun) || (lun == AHC_LUN_WILDCARD));
5274
5275 return match;
5276 }
5277
5278 static void
5279 ahc_construct_sdtr(struct ahc_softc *ahc, u_int period, u_int offset)
5280 {
5281 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
5282 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN;
5283 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR;
5284 ahc->msgout_buf[ahc->msgout_index++] = period;
5285 ahc->msgout_buf[ahc->msgout_index++] = offset;
5286 ahc->msgout_len += 5;
5287 }
5288
5289 static void
5290 ahc_construct_wdtr(struct ahc_softc *ahc, u_int bus_width)
5291 {
5292 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
5293 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN;
5294 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR;
5295 ahc->msgout_buf[ahc->msgout_index++] = bus_width;
5296 ahc->msgout_len += 4;
5297 }
5298
5299 static void
5300 ahc_calc_residual(struct scb *scb)
5301 {
5302 struct hardware_scb *hscb;
5303
5304 hscb = scb->hscb;
5305
5306 /*
5307 * If the disconnected flag is still set, this is bogus
5308 * residual information left over from a sequencer
5309 * pagin/pageout, so ignore this case.
5310 */
5311 if ((scb->hscb->control & DISCONNECTED) == 0) {
5312 u_int32_t resid;
5313 int resid_sgs;
5314 int sg;
5315
5316 /*
5317 * Remainder of the SG where the transfer
5318 * stopped.
5319 */
5320 resid = (hscb->residual_data_count[2] << 16)
5321 | (hscb->residual_data_count[1] <<8)
5322 | (hscb->residual_data_count[0]);
5323
5324 /*
5325 * Add up the contents of all residual
5326 * SG segments that are after the SG where
5327 * the transfer stopped.
5328 */
5329 resid_sgs = scb->hscb->residual_SG_count - 1/*current*/;
5330 sg = scb->sg_count - resid_sgs;
5331 while (resid_sgs > 0) {
5332
5333 resid += le32toh(scb->sg_list[sg].len);
5334 sg++;
5335 resid_sgs--;
5336 }
5337 scb->xs->resid = resid;
5338 }
5339
5340 /*
5341 * Clean out the residual information in this SCB for its
5342 * next consumer.
5343 */
5344 hscb->residual_SG_count = 0;
5345
5346 #ifdef AHC_DEBUG
5347 if (ahc_debug & AHC_SHOWMISC) {
5348 scsipi_printaddr(scb->xs->xs_periph);
5349 printf("Handled Residual of %ld bytes\n" ,(long)scb->xs->resid);
5350 }
5351 #endif
5352 }
5353
5354 static void
5355 ahc_update_pending_syncrates(struct ahc_softc *ahc)
5356 {
5357 struct scb *scb;
5358 int pending_ccb_count;
5359 int i;
5360 u_int saved_scbptr;
5361
5362 /*
5363 * Traverse the pending SCB list and ensure that all of the
5364 * SCBs there have the proper settings.
5365 */
5366 scb = LIST_FIRST(&ahc->pending_ccbs);
5367 pending_ccb_count = 0;
5368 while (scb != NULL) {
5369 struct ahc_devinfo devinfo;
5370 struct scsipi_xfer *xs;
5371 struct scb *pending_scb;
5372 struct hardware_scb *pending_hscb;
5373 struct ahc_initiator_tinfo *tinfo;
5374 struct tmode_tstate *tstate;
5375 u_int our_id, remote_id;
5376
5377 xs = scb->xs;
5378 pending_scb = scb;
5379 pending_hscb = pending_scb->hscb;
5380 our_id = SCB_IS_SCSIBUS_B(pending_scb)
5381 ? ahc->our_id_b : ahc->our_id;
5382 remote_id = xs->xs_periph->periph_target;
5383 ahc_compile_devinfo(&devinfo, our_id, remote_id,
5384 SCB_LUN(pending_scb),
5385 SCB_CHANNEL(pending_scb),
5386 ROLE_UNKNOWN);
5387 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
5388 our_id, remote_id, &tstate);
5389 pending_hscb->control &= ~ULTRAENB;
5390 if ((tstate->ultraenb & devinfo.target_mask) != 0)
5391 pending_hscb->control |= ULTRAENB;
5392 pending_hscb->scsirate = tinfo->scsirate;
5393 pending_hscb->scsioffset = tinfo->current.offset;
5394 pending_ccb_count++;
5395 scb = LIST_NEXT(scb, plinks);
5396 }
5397
5398 if (pending_ccb_count == 0)
5399 return;
5400
5401 saved_scbptr = ahc_inb(ahc, SCBPTR);
5402 /* Ensure that the hscbs down on the card match the new information */
5403 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
5404 u_int scb_tag;
5405
5406 ahc_outb(ahc, SCBPTR, i);
5407 scb_tag = ahc_inb(ahc, SCB_TAG);
5408 if (scb_tag != SCB_LIST_NULL) {
5409 struct ahc_devinfo devinfo;
5410 struct scb *pending_scb;
5411 struct scsipi_xfer *xs;
5412 struct hardware_scb *pending_hscb;
5413 struct ahc_initiator_tinfo *tinfo;
5414 struct tmode_tstate *tstate;
5415 u_int our_id, remote_id;
5416 u_int control;
5417
5418 pending_scb = &ahc->scb_data->scbarray[scb_tag];
5419 if (pending_scb->flags == SCB_FREE)
5420 continue;
5421 pending_hscb = pending_scb->hscb;
5422 xs = pending_scb->xs;
5423 our_id = SCB_IS_SCSIBUS_B(pending_scb)
5424 ? ahc->our_id_b : ahc->our_id;
5425 remote_id = xs->xs_periph->periph_target;
5426 ahc_compile_devinfo(&devinfo, our_id, remote_id,
5427 SCB_LUN(pending_scb),
5428 SCB_CHANNEL(pending_scb),
5429 ROLE_UNKNOWN);
5430 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
5431 our_id, remote_id, &tstate);
5432 control = ahc_inb(ahc, SCB_CONTROL);
5433 control &= ~ULTRAENB;
5434 if ((tstate->ultraenb & devinfo.target_mask) != 0)
5435 control |= ULTRAENB;
5436 ahc_outb(ahc, SCB_CONTROL, control);
5437 ahc_outb(ahc, SCB_SCSIRATE, tinfo->scsirate);
5438 ahc_outb(ahc, SCB_SCSIOFFSET, tinfo->current.offset);
5439 }
5440 }
5441 ahc_outb(ahc, SCBPTR, saved_scbptr);
5442 }
5443
5444 #if UNUSED
5445 static void
5446 ahc_dump_targcmd(struct target_cmd *cmd)
5447 {
5448 u_int8_t *byte;
5449 u_int8_t *last_byte;
5450 int i;
5451
5452 byte = &cmd->initiator_channel;
5453 /* Debugging info for received commands */
5454 last_byte = &cmd[1].initiator_channel;
5455
5456 i = 0;
5457 while (byte < last_byte) {
5458 if (i == 0)
5459 printf("\t");
5460 printf("%#x", *byte++);
5461 i++;
5462 if (i == 8) {
5463 printf("\n");
5464 i = 0;
5465 } else {
5466 printf(", ");
5467 }
5468 }
5469 }
5470 #endif
5471
5472 static void
5473 ahc_shutdown(void *arg)
5474 {
5475 struct ahc_softc *ahc;
5476 int i;
5477 u_int sxfrctl1_a, sxfrctl1_b;
5478
5479 ahc = (struct ahc_softc *)arg;
5480
5481 pause_sequencer(ahc);
5482
5483 /*
5484 * Preserve the value of the SXFRCTL1 register for all channels.
5485 * It contains settings that affect termination and we don't want
5486 * to disturb the integrity of the bus during shutdown in case
5487 * we are in a multi-initiator setup.
5488 */
5489 sxfrctl1_b = 0;
5490 if ((ahc->features & AHC_TWIN) != 0) {
5491 u_int sblkctl;
5492
5493 sblkctl = ahc_inb(ahc, SBLKCTL);
5494 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
5495 sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
5496 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
5497 }
5498
5499 sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
5500
5501 /* This will reset most registers to 0, but not all */
5502 ahc_reset(ahc);
5503
5504 if ((ahc->features & AHC_TWIN) != 0) {
5505 u_int sblkctl;
5506
5507 sblkctl = ahc_inb(ahc, SBLKCTL);
5508 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
5509 ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
5510 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
5511 }
5512 ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
5513
5514 ahc_outb(ahc, SCSISEQ, 0);
5515 ahc_outb(ahc, SXFRCTL0, 0);
5516 ahc_outb(ahc, DSPCISTATUS, 0);
5517
5518 for (i = TARG_SCSIRATE; i < HA_274_BIOSCTRL; i++)
5519 ahc_outb(ahc, i, 0);
5520 }
5521
5522 #if defined(AHC_DEBUG) && 0
5523 static void
5524 ahc_dumptinfo(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo)
5525 {
5526 printf("%s: tinfo: rate %u\n", ahc_name(ahc), tinfo->scsirate);
5527
5528 printf("\tcurrent:\n");
5529 printf("\t\twidth %u period %u offset %u flags %x\n",
5530 tinfo->current.width, tinfo->current.period,
5531 tinfo->current.offset, tinfo->current.ppr_flags);
5532
5533 printf("\tgoal:\n");
5534 printf("\t\twidth %u period %u offset %u flags %x\n",
5535 tinfo->goal.width, tinfo->goal.period,
5536 tinfo->goal.offset, tinfo->goal.ppr_flags);
5537
5538 printf("\tuser:\n");
5539 printf("\t\twidth %u period %u offset %u flags %x\n",
5540 tinfo->user.width, tinfo->user.period,
5541 tinfo->user.offset, tinfo->user.ppr_flags);
5542 }
5543 #endif
5544
5545 static int
5546 ahc_istagged_device(struct ahc_softc *ahc, struct scsipi_xfer *xs,
5547 int nocmdcheck)
5548 {
5549 #ifdef AHC_NO_TAGS
5550 return 0;
5551 #else
5552 char channel;
5553 u_int our_id, target;
5554 struct tmode_tstate *tstate;
5555 struct ahc_devinfo devinfo;
5556
5557 channel = SIM_CHANNEL(ahc, xs->xs_periph);
5558 our_id = SIM_SCSI_ID(ahc, xs->xs_periph);
5559 target = xs->xs_periph->periph_target;
5560 (void)ahc_fetch_transinfo(ahc, channel, our_id, target, &tstate);
5561
5562 ahc_compile_devinfo(&devinfo, our_id, target,
5563 xs->xs_periph->periph_lun, channel, ROLE_INITIATOR);
5564
5565 return (tstate->tagenable & devinfo.target_mask);
5566 #endif
5567 }
5568