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