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