aic7xxx.c revision 1.17 1 /* $NetBSD: aic7xxx.c,v 1.17 1996/10/21 22:34:04 thorpej Exp $ */
2
3 /*
4 * Generic driver for the aic7xxx based adaptec SCSI controllers
5 * Product specific probe and attach routines can be found in:
6 * i386/eisa/aic7770.c 27/284X and aic7770 motherboard controllers
7 * pci/aic7870.c 3940, 2940, aic7880, aic7870 and aic7850 controllers
8 *
9 * Copyright (c) 1994, 1995, 1996 Justin T. Gibbs.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice immediately at the beginning of the file, without modification,
17 * this list of conditions, and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from Id: aic7xxx.c,v 1.75 1996/06/23 20:02:37 gibbs Exp
37 */
38 /*
39 * TODO:
40 * Implement Target Mode
41 *
42 * A few notes on how SCB paging works...
43 *
44 * SCB paging takes advantage of the fact that devices stay disconnected
45 * from the bus a relatively long time and that while they're disconnected,
46 * having the SCBs for that device down on the host adapter is of little use.
47 * Instead we copy the SCB back up into kernel memory and reuse the SCB slot
48 * on the card to schedule another transaction. This can be a real payoff
49 * when doing random I/O to tagged queueing devices since there are more
50 * transactions active at once for the device to sort for optimal seek
51 * reduction. The algorithm goes like this...
52 *
53 * At the sequencer level:
54 * 1) Disconnected SCBs are threaded onto a doubly linked list, headed by
55 * DISCONNECTED_SCBH using the SCB_NEXT and SCB_PREV fields. The most
56 * recently disconnected device is always at the head.
57 *
58 * 2) The SCB has an added field SCB_TAG that corresponds to the kernel
59 * SCB number (ie 0-254).
60 *
61 * 3) When a command is queued, the hardware index of the SCB it was downloaded
62 * into is placed into the QINFIFO for easy indexing by the sequencer.
63 *
64 * 4) The tag field is used as the tag for tagged-queueing, for determining
65 * the related kernel SCB, and is the value put into the QOUTFIFO
66 * so the kernel doesn't have to upload the SCB to determine the kernel SCB
67 * that completed on command completes.
68 *
69 * 5) When a reconnect occurs, the sequencer must scan the SCB array (even
70 * in the tag case) looking for the appropriate SCB and if it can't find
71 * it, it interrupts the kernel so it can page the SCB in.
72 *
73 * 6) If the sequencer is successful in finding the SCB, it removes it from
74 * the doubly linked list of disconnected SCBS.
75 *
76 * At the kernel level:
77 * 1) There are four queues that a kernel SCB may reside on:
78 * free_scbs - SCBs that are not in use and have a hardware slot assigned
79 * to them.
80 * page_scbs - SCBs that are not in use and need to have a hardware slot
81 * assigned to them (i.e. they will most likely cause a page
82 * out event).
83 * waiting_scbs - SCBs that are active, don't have an assigned hardware
84 * slot assigned to them and are waiting for either a
85 * disconnection or a command complete to free up a slot.
86 * assigned_scbs - SCBs that were in the waiting_scbs queue, but were
87 * assigned a slot by ahc_free_scb.
88 *
89 * 2) When a new request comes in, an SCB is allocated from the free_scbs or
90 * page_scbs queue with preference to SCBs on the free_scbs queue.
91 *
92 * 3) If there are no free slots (we retrieved the SCB off of the page_scbs
93 * queue), the SCB is inserted onto the tail of the waiting_scbs list and
94 * we attempt to run this queue down.
95 *
96 * 4) ahc_run_waiing_queues() looks at both the assigned_scbs and waiting_scbs
97 * queues. In the case of the assigned_scbs, the commands are immediately
98 * downloaded and started. For waiting_scbs, we page in all that we can
99 * ensuring we don't create a resource deadlock (see comments in
100 * ahc_run_waing_queues()).
101 *
102 * 5) After we handle a bunch of command completes, we also try running the
103 * queues since many SCBs may have disconnected since the last command
104 * was started and we have at least one free slot on the card.
105 *
106 * 6) ahc_free_scb looks at the waiting_scbs queue for a transaction
107 * requiring a slot and moves it to the assigned_scbs queue if it
108 * finds one. Otherwise it puts the current SCB onto the free_scbs
109 * queue for later use.
110 *
111 * 7) The driver handles page-in requests from the sequencer in response to
112 * the NO_MATCH sequencer interrupt. For tagged commands, the approprite
113 * SCB is easily found since the tag is a direct index into our kernel SCB
114 * array. For non-tagged commands, we keep a separate array of 16 pointers
115 * that point to the single possible SCB that was paged out for that target.
116 */
117
118 #include <sys/param.h>
119 #include <sys/systm.h>
120 #if defined(__NetBSD__)
121 #include <sys/device.h>
122 #include <machine/bus.h>
123 #include <machine/intr.h>
124 #endif /* defined(__NetBSD__) */
125
126 #include <sys/malloc.h>
127 #include <sys/buf.h>
128 #include <sys/proc.h>
129
130 #include <scsi/scsi_all.h>
131 #include <scsi/scsi_message.h>
132 #if defined(__NetBSD__)
133 #include <scsi/scsi_debug.h>
134 #endif
135 #include <scsi/scsiconf.h>
136
137 #if defined(__FreeBSD__)
138 #include <machine/clock.h>
139 #endif
140
141 #include <vm/vm.h>
142 #include <vm/vm_param.h>
143 #include <vm/pmap.h>
144
145 #if defined(__FreeBSD__)
146 #include <i386/scsi/aic7xxx.h>
147
148 #include <dev/aic7xxx/aic7xxx_reg.h>
149 #endif /* defined(__FreeBSD__) */
150
151 #if defined(__NetBSD__)
152 #include <dev/ic/aic7xxxreg.h>
153 #include <dev/ic/aic7xxxvar.h>
154
155 #define bootverbose 1
156
157 #define DEBUGTARG DEBUGTARGET
158 #if DEBUGTARG < 0 /* Negative numbrs for disabling cause warnings */
159 #undef DEBUGTARG
160 #define DEBUGTARG 17
161 #endif
162 #endif /* defined(__NetBSD__) */
163
164 #include <sys/kernel.h>
165 #define KVTOPHYS(x) vtophys(x)
166
167 #define MIN(a,b) ((a < b) ? a : b)
168 #define ALL_TARGETS -1
169
170 #if defined(__FreeBSD__)
171 u_long ahc_unit = 0;
172 #endif
173
174 #ifdef AHC_DEBUG
175 static int ahc_debug = AHC_DEBUG;
176 #endif
177
178 #ifdef AHC_BROKEN_CACHE
179 int ahc_broken_cache = 1;
180
181 /*
182 * "wbinvd" cause writing back whole cache (both CPU internal & external)
183 * to memory, so that the instruction takes a lot of time.
184 * This makes machine slow.
185 */
186 #define INVALIDATE_CACHE() __asm __volatile("wbinvd")
187 #endif
188
189 /**** bit definitions for SCSIDEF ****/
190 #define HSCSIID 0x07 /* our SCSI ID */
191 #define HWSCSIID 0x0f /* our SCSI ID if Wide Bus */
192
193 static void ahcminphys __P((struct buf *bp));
194 static int32_t ahc_scsi_cmd __P((struct scsi_xfer *xs));
195 static inline void pause_sequencer __P((struct ahc_data *ahc));
196 static inline void unpause_sequencer __P((struct ahc_data *ahc,
197 int unpause_always));
198 static inline void restart_sequencer __P((struct ahc_data *ahc));
199
200 static struct scsi_adapter ahc_switch =
201 {
202 ahc_scsi_cmd,
203 ahcminphys,
204 0,
205 0,
206 #if defined(__FreeBSD__)
207 0,
208 "ahc",
209 { 0, 0 }
210 #endif
211 };
212
213 /* the below structure is so we have a default dev struct for our link struct */
214 static struct scsi_device ahc_dev =
215 {
216 NULL, /* Use default error handler */
217 NULL, /* have a queue, served by this */
218 NULL, /* have no async handler */
219 NULL, /* Use default 'done' routine */
220 #if defined(__FreeBSD__)
221 "ahc",
222 0,
223 { 0, 0 }
224 #endif
225 };
226
227 static inline void
228 pause_sequencer(ahc)
229 struct ahc_data *ahc;
230 {
231 AHC_OUTB(ahc, HCNTRL, ahc->pause);
232
233 /*
234 * Since the sequencer can disable pausing in a critical section, we
235 * must loop until it actually stops.
236 */
237 while ((AHC_INB(ahc, HCNTRL) & PAUSE) == 0)
238 ;
239 }
240
241 static inline void
242 unpause_sequencer(ahc, unpause_always)
243 struct ahc_data *ahc;
244 int unpause_always;
245 {
246 if (unpause_always
247 ||(AHC_INB(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
248 AHC_OUTB(ahc, HCNTRL, ahc->unpause);
249 }
250
251 /*
252 * Restart the sequencer program from address zero
253 */
254 static inline void
255 restart_sequencer(ahc)
256 struct ahc_data *ahc;
257 {
258 do {
259 AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
260 } while((AHC_INB(ahc, SEQADDR0) != 0)
261 || (AHC_INB(ahc, SEQADDR1) != 0));
262
263 unpause_sequencer(ahc, /*unpause_always*/TRUE);
264 }
265
266 #if defined(__NetBSD__)
267 /*
268 * Is device which is pointed by sc_link connected on second scsi bus ?
269 */
270 #define IS_SCSIBUS_B(ahc, sc_link) \
271 ((sc_link)->scsibus == (ahc)->sc_link_b.scsibus)
272
273 /*
274 * convert FreeBSD's SCSI symbols to NetBSD's
275 */
276 #define SCSI_NOMASK SCSI_POLL
277 #define opennings openings
278 #endif
279
280 static u_char ahc_abort_wscb __P((struct ahc_data *ahc, struct scb *scbp,
281 u_char prev,
282 u_char timedout_scb, u_int32_t xs_error));
283 static void ahc_add_waiting_scb __P((struct ahc_data *ahc,
284 struct scb *scb));
285 static void ahc_done __P((struct ahc_data *ahc, struct scb *scbp));
286 static void ahc_free_scb __P((struct ahc_data *ahc, struct scb *scb,
287 int flags));
288 static inline void ahc_send_scb __P((struct ahc_data *ahc, struct scb *scb));
289 static inline void ahc_fetch_scb __P((struct ahc_data *ahc, struct scb *scb));
290 static inline void ahc_page_scb __P((struct ahc_data *ahc, struct scb *out_scb,
291 struct scb *in_scb));
292 static inline void ahc_run_waiting_queues __P((struct ahc_data *ahc));
293 static void ahc_handle_seqint __P((struct ahc_data *ahc, u_int8_t intstat));
294 static struct scb *
295 ahc_get_scb __P((struct ahc_data *ahc, int flags));
296 static void ahc_loadseq __P((struct ahc_data *ahc));
297 static int ahc_match_scb __P((struct scb *scb, int target, char channel));
298 static int ahc_poll __P((struct ahc_data *ahc, int wait));
299 #ifdef AHC_DEBUG
300 static void ahc_print_scb __P((struct scb *scb));
301 #endif
302 static int ahc_reset_channel __P((struct ahc_data *ahc, char channel,
303 u_char timedout_scb, u_int32_t xs_error,
304 u_char initiate_reset));
305 static int ahc_reset_device __P((struct ahc_data *ahc, int target,
306 char channel, u_char timedout_scb,
307 u_int32_t xs_error));
308 static void ahc_reset_current_bus __P((struct ahc_data *ahc));
309 static void ahc_run_done_queue __P((struct ahc_data *ahc));
310 static void ahc_scsirate __P((struct ahc_data* ahc, u_int8_t *scsirate,
311 u_int8_t *period, u_int8_t *offset,
312 char channel, int target));
313 #if defined(__FreeBSD__)
314 static timeout_t
315 ahc_timeout;
316 #elif defined(__NetBSD__)
317 static void ahc_timeout __P((void *));
318 #endif
319 static void ahc_busy_target __P((struct ahc_data *ahc,
320 int target, char channel));
321 static void ahc_unbusy_target __P((struct ahc_data *ahc,
322 int target, char channel));
323 static void ahc_construct_sdtr __P((struct ahc_data *ahc, int start_byte,
324 u_int8_t period, u_int8_t offset));
325 static void ahc_construct_wdtr __P((struct ahc_data *ahc, int start_byte,
326 u_int8_t bus_width));
327
328 #if defined(__FreeBSD__)
329
330 char *ahc_name(ahc)
331 struct ahc_data *ahc;
332 {
333 static char name[10];
334
335 sprintf(name, "ahc%d", ahc->unit);
336 return (name);
337 }
338
339 #elif defined(__NetBSD__)
340 struct cfdriver ahc_cd = {
341 NULL, "ahc", DV_DULL
342 };
343 #endif
344
345 #ifdef AHC_DEBUG
346 static void
347 ahc_print_scb(scb)
348 struct scb *scb;
349 {
350 printf("scb:%p control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n"
351 ,scb
352 ,scb->control
353 ,scb->tcl
354 ,scb->cmdlen
355 ,scb->cmdpointer );
356 printf(" datlen:%d data:0x%lx segs:0x%x segp:0x%lx\n"
357 ,scb->datalen
358 ,scb->data
359 ,scb->SG_segment_count
360 ,scb->SG_list_pointer);
361 printf(" sg_addr:%lx sg_len:%ld\n"
362 ,scb->ahc_dma[0].addr
363 ,scb->ahc_dma[0].len);
364 }
365
366 #endif
367
368 static struct {
369 u_char errno;
370 char *errmesg;
371 } hard_error[] = {
372 { ILLHADDR, "Illegal Host Access" },
373 { ILLSADDR, "Illegal Sequencer Address referrenced" },
374 { ILLOPCODE, "Illegal Opcode in sequencer program" },
375 { PARERR, "Sequencer Ram Parity Error" }
376 };
377
378
379 /*
380 * Valid SCSIRATE values. (p. 3-17)
381 * Provides a mapping of tranfer periods in ns to the proper value to
382 * stick in the scsiscfr reg to use that transfer rate.
383 */
384 static struct {
385 short sxfr;
386 /* Rates in Ultra mode have bit 8 of sxfr set */
387 #define ULTRA_SXFR 0x100
388 int period; /* in ns/4 */
389 char *rate;
390 } ahc_syncrates[] = {
391 { 0x100, 12, "20.0" },
392 { 0x110, 15, "16.0" },
393 { 0x120, 18, "13.4" },
394 { 0x000, 25, "10.0" },
395 { 0x010, 31, "8.0" },
396 { 0x020, 37, "6.67" },
397 { 0x030, 43, "5.7" },
398 { 0x040, 50, "5.0" },
399 { 0x050, 56, "4.4" },
400 { 0x060, 62, "4.0" },
401 { 0x070, 68, "3.6" }
402 };
403
404 static int ahc_num_syncrates =
405 sizeof(ahc_syncrates) / sizeof(ahc_syncrates[0]);
406
407 /*
408 * Allocate a controller structures for a new device and initialize it.
409 * ahc_reset should be called before now since we assume that the card
410 * is paused.
411 */
412 #if defined(__FreeBSD__)
413 struct ahc_data *
414 ahc_alloc(unit, iobase, type, flags)
415 int unit;
416 u_long iobase;
417 #elif defined(__NetBSD__)
418 void
419 ahc_construct(ahc, iot, ioh, type, flags)
420 struct ahc_data *ahc;
421 bus_space_tag_t iot;
422 bus_space_handle_t ioh;
423 #endif
424 ahc_type type;
425 ahc_flag flags;
426 {
427
428 /*
429 * find unit and check we have that many defined
430 */
431
432 #if defined(__FreeBSD__)
433 struct ahc_data *ahc;
434
435 /*
436 * Allocate a storage area for us
437 */
438
439 ahc = malloc(sizeof(struct ahc_data), M_TEMP, M_NOWAIT);
440 if (!ahc) {
441 printf("ahc%d: cannot malloc!\n", unit);
442 return NULL;
443 }
444 bzero(ahc, sizeof(struct ahc_data));
445 #endif
446 STAILQ_INIT(&ahc->free_scbs);
447 STAILQ_INIT(&ahc->page_scbs);
448 STAILQ_INIT(&ahc->waiting_scbs);
449 STAILQ_INIT(&ahc->assigned_scbs);
450 #if defined(__FreeBSD__)
451 ahc->unit = unit;
452 #endif
453 #if defined(__FreeBSD__)
454 ahc->baseport = iobase;
455 #elif defined(__NetBSD__)
456 ahc->sc_iot = iot;
457 ahc->sc_ioh = ioh;
458 #endif
459 ahc->type = type;
460 ahc->flags = flags;
461 ahc->unpause = (AHC_INB(ahc, HCNTRL) & IRQMS) | INTEN;
462 ahc->pause = ahc->unpause | PAUSE;
463
464 #if defined(__FreeBSD__)
465 return (ahc);
466 #endif
467 }
468
469 void
470 ahc_free(ahc)
471 struct ahc_data *ahc;
472 {
473 #if defined(__FreeBSD__)
474 free(ahc, M_DEVBUF);
475 return;
476 #endif
477 }
478
479 void
480 #if defined(__FreeBSD__)
481 ahc_reset(iobase)
482 u_long iobase;
483 #elif defined(__NetBSD__)
484 ahc_reset(devname, iot, ioh)
485 char *devname;
486 bus_space_tag_t iot;
487 bus_space_handle_t ioh;
488 #endif
489 {
490 u_char hcntrl;
491 int wait;
492
493 /* Retain the IRQ type accross the chip reset */
494 #if defined(__FreeBSD__)
495 hcntrl = (inb(HCNTRL + iobase) & IRQMS) | INTEN;
496
497 outb(HCNTRL + iobase, CHIPRST | PAUSE);
498 #elif defined(__NetBSD__)
499 hcntrl = (bus_space_read_1(iot, ioh, HCNTRL) & IRQMS) | INTEN;
500
501 bus_space_write_1(iot, ioh, HCNTRL, CHIPRST | PAUSE);
502 #endif
503 /*
504 * Ensure that the reset has finished
505 */
506 wait = 1000;
507 #if defined(__FreeBSD__)
508 while (--wait && !(inb(HCNTRL + iobase) & CHIPRSTACK))
509 #elif defined(__NetBSD__)
510 while (--wait && !(bus_space_read_1(iot, ioh, HCNTRL) & CHIPRSTACK))
511 #endif
512 DELAY(1000);
513 if(wait == 0) {
514 #if defined(__FreeBSD__)
515 printf("ahc at 0x%lx: WARNING - Failed chip reset! "
516 "Trying to initialize anyway.\n", iobase);
517 #elif defined(__NetBSD__)
518 printf("%s: WARNING - Failed chip reset! "
519 "Trying to initialize anyway.\n", devname);
520 #endif
521 }
522 #if defined(__FreeBSD__)
523 outb(HCNTRL + iobase, hcntrl | PAUSE);
524 #elif defined(__NetBSD__)
525 bus_space_write_1(iot, ioh, HCNTRL, hcntrl | PAUSE);
526 #endif
527 }
528
529 /*
530 * Look up the valid period to SCSIRATE conversion in our table.
531 */
532 static void
533 ahc_scsirate(ahc, scsirate, period, offset, channel, target )
534 struct ahc_data *ahc;
535 u_int8_t *scsirate;
536 u_int8_t *period;
537 u_int8_t *offset;
538 char channel;
539 int target;
540 {
541 int i;
542 u_int32_t ultra_enb_addr;
543 u_int8_t sxfrctl0;
544 u_int8_t ultra_enb;
545
546 i = ahc_num_syncrates; /* Default to async */
547
548 if (*period >= ahc_syncrates[0].period && *offset != 0) {
549 for (i = 0; i < ahc_num_syncrates; i++) {
550
551 if (*period <= ahc_syncrates[i].period) {
552 /*
553 * Watch out for Ultra speeds when ultra is not
554 * enabled and vice-versa.
555 */
556 if(!(ahc->type & AHC_ULTRA)
557 && (ahc_syncrates[i].sxfr & ULTRA_SXFR)) {
558 /*
559 * This should only happen if the
560 * drive is the first to negotiate
561 * and chooses a high rate. We'll
562 * just move down the table util
563 * we hit a non ultra speed.
564 */
565 continue;
566 }
567 *scsirate = (ahc_syncrates[i].sxfr & 0xF0)
568 | (*offset & 0x0f);
569 *period = ahc_syncrates[i].period;
570
571 if(bootverbose) {
572 printf("%s: target %d synchronous at %sMHz,"
573 " offset = 0x%x\n",
574 ahc_name(ahc), target,
575 ahc_syncrates[i].rate, *offset );
576 }
577 break;
578 }
579 }
580 }
581 if (i >= ahc_num_syncrates) {
582 /* Use asyncronous transfers. */
583 *scsirate = 0;
584 *period = 0;
585 *offset = 0;
586 if (bootverbose)
587 printf("%s: target %d using asyncronous transfers\n",
588 ahc_name(ahc), target );
589 }
590 /*
591 * Ensure Ultra mode is set properly for
592 * this target.
593 */
594 ultra_enb_addr = ULTRA_ENB;
595 if(channel == 'B' || target > 7)
596 ultra_enb_addr++;
597 ultra_enb = AHC_INB(ahc, ultra_enb_addr);
598 sxfrctl0 = AHC_INB(ahc, SXFRCTL0);
599 if (*scsirate != 0 && ahc_syncrates[i].sxfr & ULTRA_SXFR) {
600 ultra_enb |= 0x01 << (target & 0x07);
601 sxfrctl0 |= ULTRAEN;
602 }
603 else {
604 ultra_enb &= ~(0x01 << (target & 0x07));
605 sxfrctl0 &= ~ULTRAEN;
606 }
607 AHC_OUTB(ahc, ultra_enb_addr, ultra_enb);
608 AHC_OUTB(ahc, SXFRCTL0, sxfrctl0);
609 }
610
611 /*
612 * Attach all the sub-devices we can find
613 */
614 int
615 ahc_attach(ahc)
616 struct ahc_data *ahc;
617 {
618 #if defined(__FreeBSD__)
619 struct scsibus_data *scbus;
620 #endif
621
622 #ifdef AHC_BROKEN_CACHE
623 if (cpu_class == CPUCLASS_386) /* doesn't have "wbinvd" instruction */
624 ahc_broken_cache = 0;
625 #endif
626 /*
627 * fill in the prototype scsi_links.
628 */
629 #if defined(__FreeBSD__)
630 ahc->sc_link.adapter_unit = ahc->unit;
631 ahc->sc_link.adapter_targ = ahc->our_id;
632 ahc->sc_link.fordriver = 0;
633 #elif defined(__NetBSD__)
634 ahc->sc_link.adapter_target = ahc->our_id;
635 ahc->sc_link.channel = 0;
636 #endif
637 ahc->sc_link.adapter_softc = ahc;
638 ahc->sc_link.adapter = &ahc_switch;
639 ahc->sc_link.opennings = 2;
640 ahc->sc_link.device = &ahc_dev;
641 ahc->sc_link.flags = DEBUGLEVEL;
642
643 if(ahc->type & AHC_TWIN) {
644 /* Configure the second scsi bus */
645 ahc->sc_link_b = ahc->sc_link;
646 #if defined(__FreeBSD__)
647 ahc->sc_link_b.adapter_targ = ahc->our_id_b;
648 ahc->sc_link_b.adapter_bus = 1;
649 ahc->sc_link_b.fordriver = (void *)SELBUSB;
650 #elif defined(__NetBSD__)
651 ahc->sc_link_b.adapter_target = ahc->our_id_b;
652 ahc->sc_link_b.channel = 1;
653 #endif
654 }
655
656
657 #if defined(__FreeBSD__)
658 /*
659 * Prepare the scsibus_data area for the upperlevel
660 * scsi code.
661 */
662 scbus = scsi_alloc_bus();
663 if(!scbus)
664 return 0;
665 scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
666 &ahc->sc_link_b : &ahc->sc_link;
667 if(ahc->type & AHC_WIDE)
668 scbus->maxtarg = 15;
669
670 /*
671 * ask the adapter what subunits are present
672 */
673 if(bootverbose)
674 printf("ahc%d: Probing channel %c\n", ahc->unit,
675 (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'B' : 'A');
676 scsi_attachdevs(scbus);
677 scbus = NULL; /* Upper-level SCSI code owns this now */
678
679 if(ahc->type & AHC_TWIN) {
680 scbus = scsi_alloc_bus();
681 if(!scbus)
682 return 0;
683 scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
684 &ahc->sc_link : &ahc->sc_link_b;
685 if(ahc->type & AHC_WIDE)
686 scbus->maxtarg = 15;
687 if(bootverbose)
688 printf("ahc%d: Probing Channel %c\n", ahc->unit,
689 (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'A': 'B');
690 scsi_attachdevs(scbus);
691 scbus = NULL; /* Upper-level SCSI code owns this now */
692 }
693 #elif defined(__NetBSD__)
694 /*
695 * XXX - Update MI SCSI code
696 *
697 * if(ahc->type & AHC_WIDE)
698 * max target of both channel A and B = 15;
699 */
700
701 /*
702 * ask the adapter what subunits are present
703 */
704 if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) == 0) {
705 /* make IS_SCSIBUS_B() == false, while probing channel A */
706 ahc->sc_link_b.scsibus = 0xff;
707
708 config_found((void *)ahc, &ahc->sc_link, scsiprint);
709 if (ahc->type & AHC_TWIN)
710 config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
711 } else {
712 /*
713 * if implementation of IS_SCSIBUS_B() is changed to use
714 * ahc->sc_link.scsibus, then "ahc->sc_link.scsibus = 0xff;"
715 * is needed, here.
716 */
717
718 /* assert(ahc->type & AHC_TWIN); */
719 config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
720 config_found((void *)ahc, &ahc->sc_link, scsiprint);
721 }
722 #endif
723 return 1;
724 }
725
726 /*
727 * Send an SCB down to the card via PIO.
728 * We assume that the proper SCB is already selected in SCBPTR.
729 */
730 static inline void
731 ahc_send_scb(ahc, scb)
732 struct ahc_data *ahc;
733 struct scb *scb;
734 {
735 AHC_OUTB(ahc, SCBCNT, SCBAUTO);
736 if( ahc->type == AHC_284 )
737 /* Can only do 8bit PIO */
738 AHC_OUTSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
739 else
740 AHC_OUTSL(ahc, SCBARRAY, scb,
741 (SCB_PIO_TRANSFER_SIZE + 3) / 4);
742 AHC_OUTB(ahc, SCBCNT, 0);
743 }
744
745 /*
746 * Retrieve an SCB from the card via PIO.
747 * We assume that the proper SCB is already selected in SCBPTR.
748 */
749 static inline void
750 ahc_fetch_scb(ahc, scb)
751 struct ahc_data *ahc;
752 struct scb *scb;
753 {
754 AHC_OUTB(ahc, SCBCNT, 0x80); /* SCBAUTO */
755
756 /* Can only do 8bit PIO for reads */
757 AHC_INSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
758
759 AHC_OUTB(ahc, SCBCNT, 0);
760 }
761
762 /*
763 * Swap in_scbp for out_scbp down in the cards SCB array.
764 * We assume that the SCB for out_scbp is already selected in SCBPTR.
765 */
766 static inline void
767 ahc_page_scb(ahc, out_scbp, in_scbp)
768 struct ahc_data *ahc;
769 struct scb *out_scbp;
770 struct scb *in_scbp;
771 {
772 /* Page-out */
773 ahc_fetch_scb(ahc, out_scbp);
774 out_scbp->flags |= SCB_PAGED_OUT;
775 if(!(out_scbp->control & TAG_ENB))
776 {
777 /* Stick in non-tagged array */
778 int index = (out_scbp->tcl >> 4)
779 | (out_scbp->tcl & SELBUSB);
780 ahc->pagedout_ntscbs[index] = out_scbp;
781 }
782
783 /* Page-in */
784 in_scbp->position = out_scbp->position;
785 out_scbp->position = SCB_LIST_NULL;
786 ahc_send_scb(ahc, in_scbp);
787 in_scbp->flags &= ~SCB_PAGED_OUT;
788 }
789
790 static inline void
791 ahc_run_waiting_queues(ahc)
792 struct ahc_data *ahc;
793 {
794 struct scb* scb;
795 u_char cur_scb;
796
797 if(!(ahc->assigned_scbs.stqh_first || ahc->waiting_scbs.stqh_first))
798 return;
799
800 pause_sequencer(ahc);
801 cur_scb = AHC_INB(ahc, SCBPTR);
802
803 /*
804 * First handle SCBs that are waiting but have been
805 * assigned a slot.
806 */
807 while((scb = ahc->assigned_scbs.stqh_first) != NULL) {
808 STAILQ_REMOVE_HEAD(&ahc->assigned_scbs, links);
809 AHC_OUTB(ahc, SCBPTR, scb->position);
810 ahc_send_scb(ahc, scb);
811
812 /* Mark this as an active command */
813 scb->flags ^= SCB_ASSIGNEDQ|SCB_ACTIVE;
814
815 AHC_OUTB(ahc, QINFIFO, scb->position);
816 if (!(scb->xs->flags & SCSI_NOMASK)) {
817 timeout(ahc_timeout, (caddr_t)scb,
818 (scb->xs->timeout * hz) / 1000);
819 }
820 SC_DEBUG(scb->xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
821 }
822 /* Now deal with SCBs that require paging */
823 if((scb = ahc->waiting_scbs.stqh_first) != NULL) {
824 u_char disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
825 u_char active = AHC_INB(ahc, FLAGS) & (SELECTED|IDENTIFY_SEEN);
826 int count = 0;
827
828 do {
829 u_char next_scb;
830
831 /* Attempt to page this SCB in */
832 if(disc_scb == SCB_LIST_NULL)
833 break;
834
835 /*
836 * Check the next SCB on in the list.
837 */
838 AHC_OUTB(ahc, SCBPTR, disc_scb);
839 next_scb = AHC_INB(ahc, SCB_NEXT);
840
841 /*
842 * We have to be careful about when we allow
843 * an SCB to be paged out. There must always
844 * be at least one slot availible for a
845 * reconnecting target in case it references
846 * an SCB that has been paged out. Our
847 * heuristic is that either the disconnected
848 * list has at least two entries in it or
849 * there is one entry and the sequencer is
850 * activily working on an SCB which implies that
851 * it will either complete or disconnect before
852 * another reconnection can occur.
853 */
854 if((next_scb != SCB_LIST_NULL) || active)
855 {
856 u_char out_scbi;
857 struct scb* out_scbp;
858
859 STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
860
861 /*
862 * Find the in-core SCB for the one
863 * we're paging out.
864 */
865 out_scbi = AHC_INB(ahc, SCB_TAG);
866 out_scbp = ahc->scbarray[out_scbi];
867
868 /* Do the page out */
869 ahc_page_scb(ahc, out_scbp, scb);
870
871 /* Mark this as an active command */
872 scb->flags ^= SCB_WAITINGQ|SCB_ACTIVE;
873
874 /* Queue the command */
875 AHC_OUTB(ahc, QINFIFO, scb->position);
876 if (!(scb->xs->flags & SCSI_NOMASK)) {
877 timeout(ahc_timeout, (caddr_t)scb,
878 (scb->xs->timeout * hz) / 1000);
879 }
880 SC_DEBUG(scb->xs->sc_link, SDEV_DB3,
881 ("cmd_paged-in\n"));
882 count++;
883
884 /* Advance to the next disconnected SCB */
885 disc_scb = next_scb;
886 }
887 else
888 break;
889 } while((scb = ahc->waiting_scbs.stqh_first) != NULL);
890
891 if(count) {
892 /*
893 * Update the head of the disconnected list.
894 */
895 AHC_OUTB(ahc, DISCONNECTED_SCBH, disc_scb);
896 if(disc_scb != SCB_LIST_NULL) {
897 AHC_OUTB(ahc, SCBPTR, disc_scb);
898 AHC_OUTB(ahc, SCB_PREV, SCB_LIST_NULL);
899 }
900 }
901 }
902 /* Restore old position */
903 AHC_OUTB(ahc, SCBPTR, cur_scb);
904 unpause_sequencer(ahc, /*unpause_always*/FALSE);
905 }
906
907 /*
908 * Add this SCB to the head of the "waiting for selection" list.
909 */
910 static
911 void ahc_add_waiting_scb(ahc, scb)
912 struct ahc_data *ahc;
913 struct scb *scb;
914 {
915 u_char next;
916 u_char curscb;
917
918 curscb = AHC_INB(ahc, SCBPTR);
919 next = AHC_INB(ahc, WAITING_SCBH);
920
921 AHC_OUTB(ahc, SCBPTR, scb->position);
922 AHC_OUTB(ahc, SCB_NEXT, next);
923 AHC_OUTB(ahc, WAITING_SCBH, scb->position);
924
925 AHC_OUTB(ahc, SCBPTR, curscb);
926 }
927
928 /*
929 * Catch an interrupt from the adapter
930 */
931 #if defined(__FreeBSD__)
932 void
933 #elif defined (__NetBSD__)
934 int
935 #endif
936 ahc_intr(arg)
937 void *arg;
938 {
939 int intstat;
940 u_char status;
941 struct scb *scb;
942 struct scsi_xfer *xs;
943 struct ahc_data *ahc = (struct ahc_data *)arg;
944
945 intstat = AHC_INB(ahc, INTSTAT);
946 /*
947 * Is this interrupt for me? or for
948 * someone who is sharing my interrupt
949 */
950 if (!(intstat & INT_PEND))
951 #if defined(__FreeBSD__)
952 return;
953 #elif defined(__NetBSD__)
954 return 0;
955 #endif
956
957 if (intstat & BRKADRINT) {
958 /* We upset the sequencer :-( */
959
960 /* Lookup the error message */
961 int i, error = AHC_INB(ahc, ERROR);
962 int num_errors = sizeof(hard_error)/sizeof(hard_error[0]);
963 for(i = 0; error != 1 && i < num_errors; i++)
964 error >>= 1;
965 panic("%s: brkadrint, %s at seqaddr = 0x%x\n",
966 ahc_name(ahc), hard_error[i].errmesg,
967 (AHC_INB(ahc, SEQADDR1) << 8) |
968 AHC_INB(ahc, SEQADDR0));
969 }
970 if (intstat & SEQINT)
971 ahc_handle_seqint(ahc, intstat);
972
973 if (intstat & SCSIINT) {
974
975 int scb_index = AHC_INB(ahc, SCB_TAG);
976 status = AHC_INB(ahc, SSTAT1);
977 scb = ahc->scbarray[scb_index];
978
979 if (status & SCSIRSTI) {
980 char channel;
981 channel = AHC_INB(ahc, SBLKCTL);
982 channel = channel & SELBUSB ? 'B' : 'A';
983 printf("%s: Someone reset channel %c\n",
984 ahc_name(ahc), channel);
985 ahc_reset_channel(ahc,
986 channel,
987 SCB_LIST_NULL,
988 XS_BUSY,
989 /* Initiate Reset */FALSE);
990 scb = NULL;
991 }
992 else if (!(scb && (scb->flags & SCB_ACTIVE))){
993 printf("%s: ahc_intr - referenced scb not "
994 "valid during scsiint 0x%x scb(%d)\n",
995 ahc_name(ahc), status, scb_index);
996 AHC_OUTB(ahc, CLRSINT1, status);
997 unpause_sequencer(ahc, /*unpause_always*/TRUE);
998 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
999 scb = NULL;
1000 }
1001 else if (status & SCSIPERR) {
1002 /*
1003 * Determine the bus phase and
1004 * queue an appropriate message
1005 */
1006 char *phase;
1007 u_char mesg_out = MSG_NOOP;
1008 u_char lastphase = AHC_INB(ahc, LASTPHASE);
1009
1010 xs = scb->xs;
1011 sc_print_addr(xs->sc_link);
1012
1013 switch(lastphase) {
1014 case P_DATAOUT:
1015 phase = "Data-Out";
1016 break;
1017 case P_DATAIN:
1018 phase = "Data-In";
1019 mesg_out = MSG_INITIATOR_DET_ERR;
1020 break;
1021 case P_COMMAND:
1022 phase = "Command";
1023 break;
1024 case P_MESGOUT:
1025 phase = "Message-Out";
1026 break;
1027 case P_STATUS:
1028 phase = "Status";
1029 mesg_out = MSG_INITIATOR_DET_ERR;
1030 break;
1031 case P_MESGIN:
1032 phase = "Message-In";
1033 mesg_out = MSG_PARITY_ERROR;
1034 break;
1035 default:
1036 phase = "unknown";
1037 break;
1038 }
1039 printf("parity error during %s phase.\n", phase);
1040
1041 /*
1042 * We've set the hardware to assert ATN if we
1043 * get a parity error on "in" phases, so all we
1044 * need to do is stuff the message buffer with
1045 * the appropriate message. "In" phases have set
1046 * mesg_out to something other than MSG_NOP.
1047 */
1048 if(mesg_out != MSG_NOOP) {
1049 AHC_OUTB(ahc, MSG0, mesg_out);
1050 AHC_OUTB(ahc, MSG_LEN, 1);
1051 }
1052 else
1053 /*
1054 * Should we allow the target to make
1055 * this decision for us?
1056 */
1057 xs->error = XS_DRIVER_STUFFUP;
1058 }
1059 else if (status & SELTO) {
1060 u_char waiting;
1061 u_char flags;
1062
1063 xs = scb->xs;
1064 xs->error = XS_SELTIMEOUT;
1065 /*
1066 * Clear any pending messages for the timed out
1067 * target, and mark the target as free
1068 */
1069 flags = AHC_INB(ahc, FLAGS);
1070 AHC_OUTB(ahc, MSG_LEN, 0);
1071 ahc_unbusy_target(ahc, xs->sc_link->target,
1072 #if defined(__FreeBSD__)
1073 ((long)xs->sc_link->fordriver & SELBUSB)
1074 #elif defined(__NetBSD__)
1075 IS_SCSIBUS_B(ahc, xs->sc_link)
1076 #endif
1077 ? 'B' : 'A');
1078 /* Stop the selection */
1079 AHC_OUTB(ahc, SCSISEQ, 0);
1080
1081 AHC_OUTB(ahc, SCB_CONTROL, 0);
1082
1083 AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
1084
1085 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
1086
1087 /* Shift the waiting for selection queue forward */
1088 waiting = AHC_INB(ahc, WAITING_SCBH);
1089 AHC_OUTB(ahc, SCBPTR, waiting);
1090 waiting = AHC_INB(ahc, SCB_NEXT);
1091 AHC_OUTB(ahc, WAITING_SCBH, waiting);
1092
1093 restart_sequencer(ahc);
1094 }
1095 else if (!(status & BUSFREE)) {
1096 sc_print_addr(scb->xs->sc_link);
1097 printf("Unknown SCSIINT. Status = 0x%x\n", status);
1098 AHC_OUTB(ahc, CLRSINT1, status);
1099 unpause_sequencer(ahc, /*unpause_always*/TRUE);
1100 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
1101 scb = NULL;
1102 }
1103 if(scb != NULL) {
1104 /* We want to process the command */
1105 untimeout(ahc_timeout, (caddr_t)scb);
1106 ahc_done(ahc, scb);
1107 }
1108 }
1109 if (intstat & CMDCMPLT) {
1110 int scb_index;
1111
1112 do {
1113 scb_index = AHC_INB(ahc, QOUTFIFO);
1114 scb = ahc->scbarray[scb_index];
1115 if (!scb || !(scb->flags & SCB_ACTIVE)) {
1116 printf("%s: WARNING "
1117 "no command for scb %d (cmdcmplt)\n"
1118 "QOUTCNT == %d\n",
1119 ahc_name(ahc), scb_index,
1120 AHC_INB(ahc, QOUTCNT));
1121 AHC_OUTB(ahc, CLRINT, CLRCMDINT);
1122 continue;
1123 }
1124 AHC_OUTB(ahc, CLRINT, CLRCMDINT);
1125 untimeout(ahc_timeout, (caddr_t)scb);
1126 ahc_done(ahc, scb);
1127
1128 } while (AHC_INB(ahc, QOUTCNT) & ahc->qcntmask);
1129
1130 ahc_run_waiting_queues(ahc);
1131 }
1132 #if defined(__NetBSD__)
1133 return 1;
1134 #endif
1135 }
1136
1137 static void
1138 ahc_handle_seqint(ahc, intstat)
1139 struct ahc_data *ahc;
1140 u_int8_t intstat;
1141 {
1142 struct scb *scb;
1143 u_short targ_mask;
1144 u_char target = (AHC_INB(ahc, SCSIID) >> 4) & 0x0f;
1145 u_char scratch_offset = target;
1146 char channel = AHC_INB(ahc, SBLKCTL) & SELBUSB ? 'B': 'A';
1147
1148 if (channel == 'B')
1149 scratch_offset += 8;
1150 targ_mask = (0x01 << scratch_offset);
1151
1152 switch (intstat & SEQINT_MASK) {
1153 case NO_MATCH:
1154 if (ahc->flags & AHC_PAGESCBS) {
1155 /* SCB Page-in request */
1156 u_char tag;
1157 u_char next;
1158 u_char disc_scb;
1159 struct scb *outscb;
1160 u_char arg_1 = AHC_INB(ahc, ARG_1);
1161
1162 /*
1163 * We should succeed, so set this now.
1164 * If we don't, and one of the methods
1165 * we use to aquire an SCB calls ahc_done,
1166 * we may wind up in our start routine
1167 * and unpause the adapter without giving
1168 * it the correct return value, which will
1169 * cause a hang.
1170 */
1171 AHC_OUTB(ahc, RETURN_1, SCB_PAGEDIN);
1172
1173 if (arg_1 == SCB_LIST_NULL) {
1174 /* Non-tagged command */
1175 int index;
1176
1177 index = target|(channel == 'B' ? SELBUSB : 0);
1178 scb = ahc->pagedout_ntscbs[index];
1179 } else
1180 scb = ahc->scbarray[arg_1];
1181
1182 if (!(scb->flags & SCB_PAGED_OUT))
1183 panic("%s: Request to page in a non paged out "
1184 "SCB.", ahc_name(ahc));
1185 /*
1186 * Now to pick the SCB to page out.
1187 * Either take a free SCB, an assigned SCB,
1188 * an SCB that just completed, the first
1189 * one on the disconnected SCB list, or
1190 * as a last resort a queued SCB.
1191 */
1192 if (ahc->free_scbs.stqh_first) {
1193 outscb = ahc->free_scbs.stqh_first;
1194 STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
1195 scb->position = outscb->position;
1196 outscb->position = SCB_LIST_NULL;
1197 STAILQ_INSERT_HEAD(&ahc->page_scbs, outscb,
1198 links);
1199 AHC_OUTB(ahc, SCBPTR, scb->position);
1200 ahc_send_scb(ahc, scb);
1201 scb->flags &= ~SCB_PAGED_OUT;
1202 goto pagein_done;
1203 }
1204 if (intstat & CMDCMPLT) {
1205 int scb_index;
1206
1207 AHC_OUTB(ahc, CLRINT, CLRCMDINT);
1208 scb_index = AHC_INB(ahc, QOUTFIFO);
1209 if (!(AHC_INB(ahc, QOUTCNT) & ahc->qcntmask))
1210 intstat &= ~CMDCMPLT;
1211
1212 outscb = ahc->scbarray[scb_index];
1213 if (!outscb || !(outscb->flags & SCB_ACTIVE)) {
1214 printf("%s: WARNING no command for "
1215 "scb %d (cmdcmplt)\n",
1216 ahc_name(ahc),
1217 scb_index);
1218 /*
1219 * Fall through in hopes of finding
1220 * another SCB
1221 */
1222 } else {
1223 scb->position = outscb->position;
1224 outscb->position = SCB_LIST_NULL;
1225 AHC_OUTB(ahc, SCBPTR, scb->position);
1226 ahc_send_scb(ahc, scb);
1227 scb->flags &= ~SCB_PAGED_OUT;
1228 untimeout(ahc_timeout,
1229 (caddr_t)outscb);
1230 ahc_done(ahc, outscb);
1231 goto pagein_done;
1232 }
1233 }
1234 disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
1235 if (disc_scb != SCB_LIST_NULL) {
1236 AHC_OUTB(ahc, SCBPTR, disc_scb);
1237 tag = AHC_INB(ahc, SCB_TAG);
1238 outscb = ahc->scbarray[tag];
1239 next = AHC_INB(ahc, SCB_NEXT);
1240 if (next != SCB_LIST_NULL) {
1241 AHC_OUTB(ahc, SCBPTR, next);
1242 AHC_OUTB(ahc, SCB_PREV,
1243 SCB_LIST_NULL);
1244 AHC_OUTB(ahc, SCBPTR, disc_scb);
1245 }
1246 AHC_OUTB(ahc, DISCONNECTED_SCBH, next);
1247 ahc_page_scb(ahc, outscb, scb);
1248 } else if (AHC_INB(ahc, QINCNT) & ahc->qcntmask) {
1249 /*
1250 * Pull one of our queued commands
1251 * as a last resort
1252 */
1253 disc_scb = AHC_INB(ahc, QINFIFO);
1254 AHC_OUTB(ahc, SCBPTR, disc_scb);
1255 tag = AHC_INB(ahc, SCB_TAG);
1256 outscb = ahc->scbarray[tag];
1257 if ((outscb->control & 0x23) != TAG_ENB) {
1258 /*
1259 * This is not a simple tagged command
1260 * so its position in the queue
1261 * matters. Take the command at the
1262 * end of the queue instead.
1263 */
1264 int i;
1265 u_char saved_queue[AHC_SCB_MAX];
1266 u_char queued = AHC_INB(ahc, QINCNT)
1267 & ahc->qcntmask;
1268
1269 /*
1270 * Count the command we removed
1271 * already
1272 */
1273 saved_queue[0] = disc_scb;
1274 queued++;
1275
1276 /* Empty the input queue */
1277 for (i = 1; i < queued; i++)
1278 saved_queue[i] = AHC_INB(ahc, QINFIFO);
1279
1280 /*
1281 * Put everyone back but the
1282 * last entry
1283 */
1284 queued--;
1285 for (i = 0; i < queued; i++)
1286 AHC_OUTB(ahc, QINFIFO,
1287 saved_queue[i]);
1288
1289 AHC_OUTB(ahc, SCBPTR,
1290 saved_queue[queued]);
1291 tag = AHC_INB(ahc, SCB_TAG);
1292 outscb = ahc->scbarray[tag];
1293 }
1294 untimeout(ahc_timeout, (caddr_t)outscb);
1295 scb->position = outscb->position;
1296 outscb->position = SCB_LIST_NULL;
1297 STAILQ_INSERT_HEAD(&ahc->waiting_scbs,
1298 outscb, links);
1299 outscb->flags |= SCB_WAITINGQ;
1300 ahc_send_scb(ahc, scb);
1301 scb->flags &= ~SCB_PAGED_OUT;
1302 }
1303 else {
1304 panic("Page-in request with no candidates");
1305 AHC_OUTB(ahc, RETURN_1, 0);
1306 }
1307 pagein_done:
1308 } else {
1309 printf("%s:%c:%d: no active SCB for reconnecting "
1310 "target - issuing ABORT\n",
1311 ahc_name(ahc), channel, target);
1312 printf("SAVED_TCL == 0x%x\n",
1313 AHC_INB(ahc, SAVED_TCL));
1314 ahc_unbusy_target(ahc, target, channel);
1315 AHC_OUTB(ahc, SCB_CONTROL, 0);
1316 AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
1317 AHC_OUTB(ahc, RETURN_1, 0);
1318 }
1319 break;
1320 case SEND_REJECT:
1321 {
1322 u_char rejbyte = AHC_INB(ahc, REJBYTE);
1323 printf("%s:%c:%d: Warning - unknown message recieved from "
1324 "target (0x%x). Rejecting\n",
1325 ahc_name(ahc), channel, target, rejbyte);
1326 break;
1327 }
1328 case NO_IDENT:
1329 panic("%s:%c:%d: Target did not send an IDENTIFY message. "
1330 "SAVED_TCL == 0x%x\n",
1331 ahc_name(ahc), channel, target,
1332 AHC_INB(ahc, SAVED_TCL));
1333 break;
1334 case BAD_PHASE:
1335 printf("%s:%c:%d: unknown scsi bus phase. Attempting to "
1336 "continue\n", ahc_name(ahc), channel, target);
1337 break;
1338 case EXTENDED_MSG:
1339 {
1340 u_int8_t message_length;
1341 u_int8_t message_code;
1342
1343 message_length = AHC_INB(ahc, MSGIN_EXT_LEN);
1344 message_code = AHC_INB(ahc, MSGIN_EXT_OPCODE);
1345 switch(message_code) {
1346 case MSG_EXT_SDTR:
1347 {
1348 u_int8_t period;
1349 u_int8_t offset;
1350 u_int8_t saved_offset;
1351 u_int8_t targ_scratch;
1352 u_int8_t maxoffset;
1353 u_int8_t rate;
1354
1355 if (message_length != MSG_EXT_SDTR_LEN) {
1356 AHC_OUTB(ahc, RETURN_1, SEND_REJ);
1357 ahc->sdtrpending &= ~targ_mask;
1358 break;
1359 }
1360 period = AHC_INB(ahc, MSGIN_EXT_BYTE0);
1361 saved_offset = AHC_INB(ahc, MSGIN_EXT_BYTE1);
1362 targ_scratch = AHC_INB(ahc, TARG_SCRATCH
1363 + scratch_offset);
1364 if (targ_scratch & WIDEXFER)
1365 maxoffset = MAX_OFFSET_16BIT;
1366 else
1367 maxoffset = MAX_OFFSET_8BIT;
1368 offset = MIN(saved_offset, maxoffset);
1369 ahc_scsirate(ahc, &rate, &period, &offset,
1370 channel, target);
1371 /* Preserve the WideXfer flag */
1372 targ_scratch = rate | (targ_scratch & WIDEXFER);
1373
1374 /*
1375 * Update both the target scratch area and the
1376 * current SCSIRATE.
1377 */
1378 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
1379 targ_scratch);
1380 AHC_OUTB(ahc, SCSIRATE, targ_scratch);
1381
1382 /*
1383 * See if we initiated Sync Negotiation
1384 * and didn't have to fall down to async
1385 * transfers.
1386 */
1387 if ((ahc->sdtrpending & targ_mask) != 0
1388 && (saved_offset == offset)) {
1389 /*
1390 * Don't send an SDTR back to
1391 * the target
1392 */
1393 AHC_OUTB(ahc, RETURN_1, 0);
1394 ahc->needsdtr &= ~targ_mask;
1395 ahc->sdtrpending &= ~targ_mask;
1396 } else {
1397 /*
1398 * Send our own SDTR in reply
1399 */
1400 #ifdef AHC_DEBUG
1401 if(ahc_debug & AHC_SHOWMISC)
1402 printf("Sending SDTR!!\n");
1403 #endif
1404 ahc_construct_sdtr(ahc, /*start_byte*/0,
1405 period, offset);
1406 AHC_OUTB(ahc, RETURN_1, SEND_MSG);
1407
1408 /*
1409 * If we aren't starting a re-negotiation
1410 * because we had to go async in response
1411 * to a "too low" response from the target
1412 * clear the needsdtr flag for this target.
1413 */
1414 if ((ahc->sdtrpending & targ_mask) == 0)
1415 ahc->needsdtr &= ~targ_mask;
1416 else
1417 ahc->sdtrpending |= targ_mask;
1418 }
1419 break;
1420 }
1421 case MSG_EXT_WDTR:
1422 {
1423 u_int8_t scratch, bus_width;
1424
1425 if (message_length != MSG_EXT_WDTR_LEN) {
1426 AHC_OUTB(ahc, RETURN_1, SEND_REJ);
1427 ahc->wdtrpending &= ~targ_mask;
1428 break;
1429 }
1430
1431 bus_width = AHC_INB(ahc, MSGIN_EXT_BYTE0);
1432 scratch = AHC_INB(ahc, TARG_SCRATCH
1433 + scratch_offset);
1434
1435 if (ahc->wdtrpending & targ_mask) {
1436 /*
1437 * Don't send a WDTR back to the
1438 * target, since we asked first.
1439 */
1440 AHC_OUTB(ahc, RETURN_1, 0);
1441 switch(bus_width){
1442 case BUS_8_BIT:
1443 scratch &= 0x7f;
1444 break;
1445 case BUS_16_BIT:
1446 if(bootverbose)
1447 printf("%s: target %d using "
1448 "16Bit transfers\n",
1449 ahc_name(ahc), target);
1450 scratch |= WIDEXFER;
1451 break;
1452 case BUS_32_BIT:
1453 /*
1454 * How can we do 32bit transfers
1455 * on a 16bit bus?
1456 */
1457 AHC_OUTB(ahc, RETURN_1, SEND_REJ);
1458 printf("%s: target %d requested 32Bit "
1459 "transfers. Rejecting...\n",
1460 ahc_name(ahc), target);
1461 break;
1462 default:
1463 break;
1464 }
1465 } else {
1466 /*
1467 * Send our own WDTR in reply
1468 */
1469 switch(bus_width) {
1470 case BUS_8_BIT:
1471 scratch &= 0x7f;
1472 break;
1473 case BUS_32_BIT:
1474 case BUS_16_BIT:
1475 if(ahc->type & AHC_WIDE) {
1476 /* Negotiate 16_BITS */
1477 bus_width = BUS_16_BIT;
1478 if(bootverbose)
1479 printf("%s: target %d "
1480 "using 16Bit "
1481 "transfers\n",
1482 ahc_name(ahc),
1483 target);
1484 scratch |= WIDEXFER;
1485 } else
1486 bus_width = BUS_8_BIT;
1487 break;
1488 default:
1489 break;
1490 }
1491 ahc_construct_wdtr(ahc, /*start_byte*/0,
1492 bus_width);
1493 AHC_OUTB(ahc, RETURN_1, SEND_MSG);
1494 }
1495
1496 ahc->needwdtr &= ~targ_mask;
1497 ahc->wdtrpending &= ~targ_mask;
1498 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset, scratch);
1499 AHC_OUTB(ahc, SCSIRATE, scratch);
1500 break;
1501 }
1502 default:
1503 /* Unknown extended message. Reject it. */
1504 AHC_OUTB(ahc, RETURN_1, SEND_REJ);
1505 }
1506 }
1507 case REJECT_MSG:
1508 {
1509 /*
1510 * What we care about here is if we had an
1511 * outstanding SDTR or WDTR message for this
1512 * target. If we did, this is a signal that
1513 * the target is refusing negotiation.
1514 */
1515
1516 u_char targ_scratch;
1517
1518 targ_scratch = AHC_INB(ahc, TARG_SCRATCH
1519 + scratch_offset);
1520
1521 if (ahc->wdtrpending & targ_mask){
1522 /* note 8bit xfers and clear flag */
1523 targ_scratch &= 0x7f;
1524 ahc->needwdtr &= ~targ_mask;
1525 ahc->wdtrpending &= ~targ_mask;
1526 printf("%s:%c:%d: refuses WIDE negotiation. Using "
1527 "8bit transfers\n", ahc_name(ahc),
1528 channel, target);
1529 } else if(ahc->sdtrpending & targ_mask){
1530 /* note asynch xfers and clear flag */
1531 targ_scratch &= 0xf0;
1532 ahc->needsdtr &= ~targ_mask;
1533 ahc->sdtrpending &= ~targ_mask;
1534 printf("%s:%c:%d: refuses syncronous negotiation. "
1535 "Using asyncronous transfers\n",
1536 ahc_name(ahc),
1537 channel, target);
1538 } else {
1539 /*
1540 * Otherwise, we ignore it.
1541 */
1542 #ifdef AHC_DEBUG
1543 if(ahc_debug & AHC_SHOWMISC)
1544 printf("%s:%c:%d: Message reject -- ignored\n",
1545 ahc_name(ahc), channel, target);
1546 #endif
1547 break;
1548 }
1549 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset, targ_scratch);
1550 AHC_OUTB(ahc, SCSIRATE, targ_scratch);
1551 break;
1552 }
1553 case BAD_STATUS:
1554 {
1555 int scb_index;
1556 struct scsi_xfer *xs;
1557
1558 /* The sequencer will notify us when a command
1559 * has an error that would be of interest to
1560 * the kernel. This allows us to leave the sequencer
1561 * running in the common case of command completes
1562 * without error.
1563 */
1564
1565 scb_index = AHC_INB(ahc, SCB_TAG);
1566 scb = ahc->scbarray[scb_index];
1567
1568 /*
1569 * Set the default return value to 0 (don't
1570 * send sense). The sense code will change
1571 * this if needed and this reduces code
1572 * duplication.
1573 */
1574 AHC_OUTB(ahc, RETURN_1, 0);
1575 if (!(scb && (scb->flags & SCB_ACTIVE))) {
1576 printf("%s:%c:%d: ahc_intr - referenced scb "
1577 "not valid during seqint 0x%x scb(%d)\n",
1578 ahc_name(ahc),
1579 channel, target, intstat,
1580 scb_index);
1581 goto clear;
1582 }
1583
1584 xs = scb->xs;
1585
1586 scb->status = AHC_INB(ahc, SCB_TARGET_STATUS);
1587
1588 #ifdef AHC_DEBUG
1589 if((ahc_debug & AHC_SHOWSCBS)
1590 && xs->sc_link->target == DEBUGTARGET)
1591 ahc_print_scb(scb);
1592 #endif
1593 xs->status = scb->status;
1594 switch(scb->status){
1595 case SCSI_OK:
1596 printf("%s: Interrupted for staus of"
1597 " 0???\n", ahc_name(ahc));
1598 break;
1599 case SCSI_CHECK:
1600 #ifdef AHC_DEBUG
1601 if(ahc_debug & AHC_SHOWSENSE)
1602 {
1603 sc_print_addr(xs->sc_link);
1604 printf("requests Check Status\n");
1605 }
1606 #endif
1607
1608 if ((xs->error == XS_NOERROR)
1609 && !(scb->flags & SCB_SENSE)) {
1610 struct ahc_dma_seg *sg = scb->ahc_dma;
1611 struct scsi_sense *sc = &(scb->sense_cmd);
1612 #ifdef AHC_DEBUG
1613 if (ahc_debug & AHC_SHOWSENSE)
1614 {
1615 sc_print_addr(xs->sc_link);
1616 printf("Sending Sense\n");
1617 }
1618 #endif
1619 #if defined(__FreeBSD__)
1620 sc->op_code = REQUEST_SENSE;
1621 #elif defined(__NetBSD__)
1622 sc->opcode = REQUEST_SENSE;
1623 #endif
1624 sc->byte2 = xs->sc_link->lun << 5;
1625 sc->length = sizeof(struct scsi_sense_data);
1626 sc->control = 0;
1627
1628 sg->addr = KVTOPHYS(&xs->sense);
1629 sg->len = sizeof(struct scsi_sense_data);
1630
1631 scb->control &= DISCENB;
1632 scb->status = 0;
1633 scb->SG_segment_count = 1;
1634 scb->SG_list_pointer = KVTOPHYS(sg);
1635 scb->data = sg->addr;
1636 scb->datalen = sg->len;
1637 #ifdef AHC_BROKEN_CACHE
1638 if (ahc_broken_cache)
1639 INVALIDATE_CACHE();
1640 #endif
1641 scb->cmdpointer = KVTOPHYS(sc);
1642 scb->cmdlen = sizeof(*sc);
1643
1644 scb->flags |= SCB_SENSE;
1645 ahc_send_scb(ahc, scb);
1646 /*
1647 * Ensure that the target is "BUSY"
1648 * so we don't get overlapping
1649 * commands if we happen to be doing
1650 * tagged I/O.
1651 */
1652 ahc_busy_target(ahc, target, channel);
1653
1654 /*
1655 * Make us the next command to run
1656 */
1657 ahc_add_waiting_scb(ahc, scb);
1658 AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
1659 break;
1660 }
1661 /*
1662 * Clear the SCB_SENSE Flag and have
1663 * the sequencer do a normal command
1664 * complete with either a "DRIVER_STUFFUP"
1665 * error or whatever other error condition
1666 * we already had.
1667 */
1668 scb->flags &= ~SCB_SENSE;
1669 if (xs->error == XS_NOERROR)
1670 xs->error = XS_DRIVER_STUFFUP;
1671 break;
1672 case SCSI_BUSY:
1673 xs->error = XS_BUSY;
1674 sc_print_addr(xs->sc_link);
1675 printf("Target Busy\n");
1676 break;
1677 case SCSI_QUEUE_FULL:
1678 /*
1679 * The upper level SCSI code will someday
1680 * handle this properly.
1681 */
1682 sc_print_addr(xs->sc_link);
1683 printf("Queue Full\n");
1684 scb->flags |= SCB_ASSIGNEDQ;
1685 STAILQ_INSERT_TAIL(&ahc->assigned_scbs,scb, links);
1686 AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
1687 break;
1688 default:
1689 sc_print_addr(xs->sc_link);
1690 printf("unexpected targ_status: %x\n", scb->status);
1691 xs->error = XS_DRIVER_STUFFUP;
1692 break;
1693 }
1694 break;
1695 }
1696 case RESIDUAL:
1697 {
1698 int scb_index;
1699 struct scsi_xfer *xs;
1700
1701 scb_index = AHC_INB(ahc, SCB_TAG);
1702 scb = ahc->scbarray[scb_index];
1703 xs = scb->xs;
1704 /*
1705 * Don't clobber valid resid info with
1706 * a resid coming from a check sense
1707 * operation.
1708 */
1709 if (!(scb->flags & SCB_SENSE)) {
1710 int resid_sgs;
1711
1712 /*
1713 * Remainder of the SG where the transfer
1714 * stopped.
1715 */
1716 xs->resid = (AHC_INB(ahc, SCB_RESID_DCNT2)<<16) |
1717 (AHC_INB(ahc, SCB_RESID_DCNT1)<<8) |
1718 AHC_INB(ahc, SCB_RESID_DCNT0);
1719
1720 /*
1721 * Add up the contents of all residual
1722 * SG segments that are after the SG where
1723 * the transfer stopped.
1724 */
1725 resid_sgs = AHC_INB(ahc, SCB_RESID_SGCNT) - 1;
1726 while (resid_sgs > 0) {
1727 int sg;
1728
1729 sg = scb->SG_segment_count - resid_sgs;
1730 xs->resid += scb->ahc_dma[sg].len;
1731 resid_sgs--;
1732 }
1733
1734 #if defined(__FreeBSD__)
1735 xs->flags |= SCSI_RESID_VALID;
1736 #elif defined(__NetBSD__)
1737 /* XXX - Update to do this right */
1738 #endif
1739 #ifdef AHC_DEBUG
1740 if (ahc_debug & AHC_SHOWMISC) {
1741 sc_print_addr(xs->sc_link);
1742 printf("Handled Residual of %ld bytes\n"
1743 ,xs->resid);
1744 }
1745 #endif
1746 }
1747 break;
1748 }
1749 case ABORT_TAG:
1750 {
1751 int scb_index;
1752 struct scsi_xfer *xs;
1753
1754 scb_index = AHC_INB(ahc, SCB_TAG);
1755 scb = ahc->scbarray[scb_index];
1756 xs = scb->xs;
1757 /*
1758 * We didn't recieve a valid tag back from
1759 * the target on a reconnect.
1760 */
1761 sc_print_addr(xs->sc_link);
1762 printf("invalid tag recieved -- sending ABORT_TAG\n");
1763 xs->error = XS_DRIVER_STUFFUP;
1764 untimeout(ahc_timeout, (caddr_t)scb);
1765 ahc_done(ahc, scb);
1766 break;
1767 }
1768 case AWAITING_MSG:
1769 {
1770 int scb_index;
1771 scb_index = AHC_INB(ahc, SCB_TAG);
1772 scb = ahc->scbarray[scb_index];
1773 /*
1774 * This SCB had a zero length command, informing
1775 * the sequencer that we wanted to send a special
1776 * message to this target. We only do this for
1777 * BUS_DEVICE_RESET messages currently.
1778 */
1779 if (scb->flags & SCB_DEVICE_RESET) {
1780 AHC_OUTB(ahc, MSG0,
1781 MSG_BUS_DEV_RESET);
1782 AHC_OUTB(ahc, MSG_LEN, 1);
1783 printf("Bus Device Reset Message Sent\n");
1784 } else if (scb->flags & SCB_MSGOUT_WDTR) {
1785 ahc_construct_wdtr(ahc, AHC_INB(ahc, MSG_LEN),
1786 BUS_16_BIT);
1787 } else if (scb->flags & SCB_MSGOUT_SDTR) {
1788 u_int8_t target_scratch;
1789 u_int8_t ultraenable;
1790 int sxfr;
1791 int i;
1792
1793 /* Pull the user defined setting */
1794 target_scratch = AHC_INB(ahc, TARG_SCRATCH
1795 + scratch_offset);
1796
1797 sxfr = target_scratch & SXFR;
1798 if (scratch_offset < 8)
1799 ultraenable = AHC_INB(ahc, ULTRA_ENB);
1800 else
1801 ultraenable = AHC_INB(ahc, ULTRA_ENB + 1);
1802
1803 if (ultraenable & targ_mask)
1804 /* Want an ultra speed in the table */
1805 sxfr |= 0x100;
1806
1807 for (i = 0; i < ahc_num_syncrates; i++)
1808 if (sxfr == ahc_syncrates[i].sxfr)
1809 break;
1810
1811 ahc_construct_sdtr(ahc, AHC_INB(ahc, MSG_LEN),
1812 ahc_syncrates[i].period,
1813 target_scratch & WIDEXFER ?
1814 MAX_OFFSET_16BIT : MAX_OFFSET_8BIT);
1815 } else
1816 panic("ahc_intr: AWAITING_MSG for an SCB that "
1817 "does not have a waiting message");
1818 break;
1819 }
1820 case IMMEDDONE:
1821 {
1822 /*
1823 * Take care of device reset messages
1824 */
1825 u_char scbindex = AHC_INB(ahc, SCB_TAG);
1826 scb = ahc->scbarray[scbindex];
1827 if (scb->flags & SCB_DEVICE_RESET) {
1828 u_char targ_scratch;
1829 int found;
1830 /*
1831 * Go back to async/narrow transfers and
1832 * renegotiate.
1833 */
1834 ahc_unbusy_target(ahc, target, channel);
1835 ahc->needsdtr |= ahc->needsdtr_orig & targ_mask;
1836 ahc->needwdtr |= ahc->needwdtr_orig & targ_mask;
1837 ahc->sdtrpending &= ~targ_mask;
1838 ahc->wdtrpending &= ~targ_mask;
1839 targ_scratch = AHC_INB(ahc, TARG_SCRATCH
1840 + scratch_offset);
1841 targ_scratch &= SXFR;
1842 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
1843 targ_scratch);
1844 found = ahc_reset_device(ahc, target,
1845 channel, SCB_LIST_NULL,
1846 XS_NOERROR);
1847 sc_print_addr(scb->xs->sc_link);
1848 printf("Bus Device Reset delivered. "
1849 "%d SCBs aborted\n", found);
1850 ahc->in_timeout = FALSE;
1851 ahc_run_done_queue(ahc);
1852 } else
1853 panic("ahc_intr: Immediate complete for "
1854 "unknown operation.");
1855 break;
1856 }
1857 case DATA_OVERRUN:
1858 {
1859 /*
1860 * When the sequencer detects an overrun, it
1861 * sets STCNT to 0x00ffffff and allows the
1862 * target to complete its transfer in
1863 * BITBUCKET mode.
1864 */
1865 u_char scbindex = AHC_INB(ahc, SCB_TAG);
1866 u_int32_t overrun;
1867 scb = ahc->scbarray[scbindex];
1868 overrun = AHC_INB(ahc, STCNT0)
1869 | (AHC_INB(ahc, STCNT1) << 8)
1870 | (AHC_INB(ahc, STCNT2) << 16);
1871 overrun = 0x00ffffff - overrun;
1872 sc_print_addr(scb->xs->sc_link);
1873 printf("data overrun of %d bytes detected."
1874 " Forcing a retry.\n", overrun);
1875 /*
1876 * Set this and it will take affect when the
1877 * target does a command complete.
1878 */
1879 scb->xs->error = XS_DRIVER_STUFFUP;
1880 break;
1881 }
1882 #if NOT_YET
1883 /* XXX Fill these in later */
1884 case MESG_BUFFER_BUSY:
1885 break;
1886 case MSGIN_PHASEMIS:
1887 break;
1888 #endif
1889 default:
1890 printf("ahc_intr: seqint, "
1891 "intstat == 0x%x, scsisigi = 0x%x\n",
1892 intstat, AHC_INB(ahc, SCSISIGI));
1893 break;
1894 }
1895
1896 clear:
1897 /*
1898 * Clear the upper byte that holds SEQINT status
1899 * codes and clear the SEQINT bit.
1900 */
1901 AHC_OUTB(ahc, CLRINT, CLRSEQINT);
1902
1903 /*
1904 * The sequencer is paused immediately on
1905 * a SEQINT, so we should restart it when
1906 * we're done.
1907 */
1908 unpause_sequencer(ahc, /*unpause_always*/TRUE);
1909 }
1910
1911 /*
1912 * We have a scb which has been processed by the
1913 * adaptor, now we look to see how the operation
1914 * went.
1915 */
1916 static void
1917 ahc_done(ahc, scb)
1918 struct ahc_data *ahc;
1919 struct scb *scb;
1920 {
1921 struct scsi_xfer *xs = scb->xs;
1922
1923 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_done\n"));
1924 /*
1925 * Put the results of the operation
1926 * into the xfer and call whoever started it
1927 */
1928 #if defined(__NetBSD__)
1929 if (xs->error != XS_NOERROR) {
1930 /* Don't override the error value. */
1931 } else if (scb->flags & SCB_ABORTED) {
1932 xs->error = XS_DRIVER_STUFFUP;
1933 } else
1934 #endif
1935 if(scb->flags & SCB_SENSE)
1936 xs->error = XS_SENSE;
1937 if(scb->flags & SCB_SENTORDEREDTAG)
1938 ahc->in_timeout = FALSE;
1939 #if defined(__FreeBSD__)
1940 if ((xs->flags & SCSI_ERR_OK) && !(xs->error == XS_SENSE)) {
1941 /* All went correctly OR errors expected */
1942 xs->error = XS_NOERROR;
1943 }
1944 #elif defined(__NetBSD__)
1945 /*
1946 * Since NetBSD doesn't have error ignoring operation mode
1947 * (SCSI_ERR_OK in FreeBSD), we don't have to care this case.
1948 */
1949 #endif
1950 xs->flags |= ITSDONE;
1951 #ifdef AHC_TAGENABLE
1952 if(xs->cmd->opcode == INQUIRY && xs->error == XS_NOERROR)
1953 {
1954 struct scsi_inquiry_data *inq_data;
1955 u_short mask = 0x01 << (xs->sc_link->target |
1956 (scb->tcl & 0x08));
1957 /*
1958 * Sneak a look at the results of the SCSI Inquiry
1959 * command and see if we can do Tagged queing. This
1960 * should really be done by the higher level drivers.
1961 */
1962 inq_data = (struct scsi_inquiry_data *)xs->data;
1963 if((inq_data->flags & SID_CmdQue) && !(ahc->tagenable & mask))
1964 {
1965 printf("%s: target %d Tagged Queuing Device\n",
1966 ahc_name(ahc), xs->sc_link->target);
1967 ahc->tagenable |= mask;
1968 if(ahc->maxhscbs >= 16 || (ahc->flags & AHC_PAGESCBS)) {
1969 /* Default to 8 tags */
1970 xs->sc_link->opennings += 6;
1971 }
1972 else
1973 {
1974 /*
1975 * Default to 4 tags on whimpy
1976 * cards that don't have much SCB
1977 * space and can't page. This prevents
1978 * a single device from hogging all
1979 * slots. We should really have a better
1980 * way of providing fairness.
1981 */
1982 xs->sc_link->opennings += 2;
1983 }
1984 }
1985 }
1986 #endif
1987 ahc_free_scb(ahc, scb, xs->flags);
1988 scsi_done(xs);
1989 }
1990
1991 /*
1992 * Start the board, ready for normal operation
1993 */
1994 int
1995 ahc_init(ahc)
1996 struct ahc_data *ahc;
1997 {
1998 u_int8_t scsi_conf, sblkctl, i;
1999 u_int16_t ultraenable = 0;
2000 int max_targ = 15;
2001 /*
2002 * Assume we have a board at this stage and it has been reset.
2003 */
2004
2005 /* Handle the SCBPAGING option */
2006 #ifndef AHC_SCBPAGING_ENABLE
2007 ahc->flags &= ~AHC_PAGESCBS;
2008 #endif
2009
2010 /* Determine channel configuration and who we are on the scsi bus. */
2011 switch ( (sblkctl = AHC_INB(ahc, SBLKCTL) & 0x0a) ) {
2012 case 0:
2013 ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
2014 ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
2015 if(ahc->type == AHC_394)
2016 printf("Channel %c, SCSI Id=%d, ",
2017 ahc->flags & AHC_CHNLB ? 'B' : 'A',
2018 ahc->our_id);
2019 else
2020 printf("Single Channel, SCSI Id=%d, ", ahc->our_id);
2021 AHC_OUTB(ahc, FLAGS, SINGLE_BUS | (ahc->flags & AHC_PAGESCBS));
2022 break;
2023 case 2:
2024 ahc->our_id = (AHC_INB(ahc, SCSICONF + 1) & HWSCSIID);
2025 ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
2026 if(ahc->type == AHC_394)
2027 printf("Wide Channel %c, SCSI Id=%d, ",
2028 ahc->flags & AHC_CHNLB ? 'B' : 'A',
2029 ahc->our_id);
2030 else
2031 printf("Wide Channel, SCSI Id=%d, ", ahc->our_id);
2032 ahc->type |= AHC_WIDE;
2033 AHC_OUTB(ahc, FLAGS, WIDE_BUS | (ahc->flags & AHC_PAGESCBS));
2034 break;
2035 case 8:
2036 ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
2037 ahc->our_id_b = (AHC_INB(ahc, SCSICONF + 1) & HSCSIID);
2038 printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, ",
2039 ahc->our_id, ahc->our_id_b);
2040 ahc->type |= AHC_TWIN;
2041 AHC_OUTB(ahc, FLAGS, TWIN_BUS | (ahc->flags & AHC_PAGESCBS));
2042 break;
2043 default:
2044 printf(" Unsupported adapter type. Ignoring\n");
2045 return(-1);
2046 }
2047
2048 /* Determine the number of SCBs */
2049
2050 {
2051 AHC_OUTB(ahc, SCBPTR, 0);
2052 AHC_OUTB(ahc, SCB_CONTROL, 0);
2053 for(i = 1; i < AHC_SCB_MAX; i++) {
2054 AHC_OUTB(ahc, SCBPTR, i);
2055 AHC_OUTB(ahc, SCB_CONTROL, i);
2056 if(AHC_INB(ahc, SCB_CONTROL) != i)
2057 break;
2058 AHC_OUTB(ahc, SCBPTR, 0);
2059 if(AHC_INB(ahc, SCB_CONTROL) != 0)
2060 break;
2061 /* Clear the control byte. */
2062 AHC_OUTB(ahc, SCBPTR, i);
2063 AHC_OUTB(ahc, SCB_CONTROL, 0);
2064
2065 ahc->qcntmask |= i; /* Update the count mask. */
2066 }
2067
2068 /* Ensure we clear the 0 SCB's control byte. */
2069 AHC_OUTB(ahc, SCBPTR, 0);
2070 AHC_OUTB(ahc, SCB_CONTROL, 0);
2071
2072 ahc->qcntmask |= i;
2073 ahc->maxhscbs = i;
2074 }
2075
2076 if((ahc->maxhscbs < AHC_SCB_MAX) && (ahc->flags & AHC_PAGESCBS))
2077 ahc->maxscbs = AHC_SCB_MAX;
2078 else {
2079 ahc->maxscbs = ahc->maxhscbs;
2080 ahc->flags &= ~AHC_PAGESCBS;
2081 }
2082
2083 printf("%d SCBs\n", ahc->maxhscbs);
2084
2085 #ifdef AHC_DEBUG
2086 if(ahc_debug & AHC_SHOWMISC) {
2087 struct scb test;
2088 printf("%s: hardware scb %ld bytes; kernel scb; "
2089 "ahc_dma %d bytes\n",
2090 ahc_name(ahc),
2091 (u_long)&(test.next) - (u_long)(&test),
2092 sizeof(test),
2093 sizeof(struct ahc_dma_seg));
2094 }
2095 #endif /* AHC_DEBUG */
2096
2097 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
2098 if(ahc->type & AHC_TWIN)
2099 {
2100 /*
2101 * The device is gated to channel B after a chip reset,
2102 * so set those values first
2103 */
2104 AHC_OUTB(ahc, SCSIID, ahc->our_id_b);
2105 scsi_conf = AHC_INB(ahc, SCSICONF + 1);
2106 AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
2107 | ENSTIMER|ACTNEGEN|STPWEN);
2108 AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
2109 if(ahc->type & AHC_ULTRA)
2110 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
2111 else
2112 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
2113
2114 if(scsi_conf & RESET_SCSI) {
2115 /* Reset the bus */
2116 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2117 if(bootverbose)
2118 printf("%s: Reseting Channel B\n",
2119 ahc_name(ahc));
2120 #endif
2121 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
2122 DELAY(1000);
2123 AHC_OUTB(ahc, SCSISEQ, 0);
2124
2125 /* Ensure we don't get a RSTI interrupt from this */
2126 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
2127 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
2128 }
2129
2130 /* Select Channel A */
2131 AHC_OUTB(ahc, SBLKCTL, 0);
2132 }
2133 AHC_OUTB(ahc, SCSIID, ahc->our_id);
2134 scsi_conf = AHC_INB(ahc, SCSICONF);
2135 AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
2136 | ENSTIMER|ACTNEGEN|STPWEN);
2137 AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
2138 if(ahc->type & AHC_ULTRA)
2139 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
2140 else
2141 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
2142
2143 if(scsi_conf & RESET_SCSI) {
2144 /* Reset the bus */
2145 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2146 if(bootverbose)
2147 printf("%s: Reseting Channel A\n", ahc_name(ahc));
2148 #endif
2149
2150 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
2151 DELAY(1000);
2152 AHC_OUTB(ahc, SCSISEQ, 0);
2153
2154 /* Ensure we don't get a RSTI interrupt from this */
2155 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
2156 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
2157 }
2158
2159 /*
2160 * Look at the information that board initialization or
2161 * the board bios has left us. In the lower four bits of each
2162 * target's scratch space any value other than 0 indicates
2163 * that we should initiate syncronous transfers. If it's zero,
2164 * the user or the BIOS has decided to disable syncronous
2165 * negotiation to that target so we don't activate the needsdtr
2166 * flag.
2167 */
2168 ahc->needsdtr_orig = 0;
2169 ahc->needwdtr_orig = 0;
2170
2171 /* Grab the disconnection disable table and invert it for our needs */
2172 if(ahc->flags & AHC_USEDEFAULTS) {
2173 printf("%s: Host Adapter Bios disabled. Using default SCSI "
2174 "device parameters\n", ahc_name(ahc));
2175 ahc->discenable = 0xff;
2176 }
2177 else
2178 ahc->discenable = ~((AHC_INB(ahc, DISC_DSB + 1) << 8)
2179 | AHC_INB(ahc, DISC_DSB));
2180
2181 if(!(ahc->type & (AHC_WIDE|AHC_TWIN)))
2182 max_targ = 7;
2183
2184 for(i = 0; i <= max_targ; i++){
2185 u_char target_settings;
2186 if (ahc->flags & AHC_USEDEFAULTS) {
2187 target_settings = 0; /* 10MHz */
2188 ahc->needsdtr_orig |= (0x01 << i);
2189 ahc->needwdtr_orig |= (0x01 << i);
2190 }
2191 else {
2192 /* Take the settings leftover in scratch RAM. */
2193 target_settings = AHC_INB(ahc, TARG_SCRATCH + i);
2194
2195 if(target_settings & 0x0f){
2196 ahc->needsdtr_orig |= (0x01 << i);
2197 /*Default to a asyncronous transfers(0 offset)*/
2198 target_settings &= 0xf0;
2199 }
2200 if(target_settings & 0x80){
2201 ahc->needwdtr_orig |= (0x01 << i);
2202 /*
2203 * We'll set the Wide flag when we
2204 * are successful with Wide negotiation.
2205 * Turn it off for now so we aren't
2206 * confused.
2207 */
2208 target_settings &= 0x7f;
2209 }
2210 if(ahc->type & AHC_ULTRA) {
2211 /*
2212 * Enable Ultra for any target that
2213 * has a valid ultra syncrate setting.
2214 */
2215 u_char rate = target_settings & 0x70;
2216 if(rate == 0x00 || rate == 0x10 ||
2217 rate == 0x20 || rate == 0x40) {
2218 if(rate == 0x40) {
2219 /* Treat 10MHz specially */
2220 target_settings &= ~0x70;
2221 }
2222 else
2223 ultraenable |= (0x01 << i);
2224 }
2225 }
2226 }
2227 AHC_OUTB(ahc, TARG_SCRATCH+i,target_settings);
2228 }
2229 /*
2230 * If we are not a WIDE device, forget WDTR. This
2231 * makes the driver work on some cards that don't
2232 * leave these fields cleared when the BIOS is not
2233 * installed.
2234 */
2235 if(!(ahc->type & AHC_WIDE))
2236 ahc->needwdtr_orig = 0;
2237 ahc->needsdtr = ahc->needsdtr_orig;
2238 ahc->needwdtr = ahc->needwdtr_orig;
2239 ahc->sdtrpending = 0;
2240 ahc->wdtrpending = 0;
2241 ahc->tagenable = 0;
2242 ahc->orderedtag = 0;
2243
2244 AHC_OUTB(ahc, ULTRA_ENB, ultraenable & 0xff);
2245 AHC_OUTB(ahc, ULTRA_ENB + 1, (ultraenable >> 8) & 0xff);
2246
2247 #ifdef AHC_DEBUG
2248 /* How did we do? */
2249 if(ahc_debug & AHC_SHOWMISC)
2250 printf("NEEDSDTR == 0x%x\nNEEDWDTR == 0x%x\n"
2251 "DISCENABLE == 0x%x\n", ahc->needsdtr,
2252 ahc->needwdtr, ahc->discenable);
2253 #endif
2254 /*
2255 * Set the number of availible SCBs
2256 */
2257 AHC_OUTB(ahc, SCBCOUNT, ahc->maxhscbs);
2258
2259 /*
2260 * 2's compliment of maximum tag value
2261 */
2262 i = ahc->maxscbs;
2263 AHC_OUTB(ahc, COMP_SCBCOUNT, -i & 0xff);
2264
2265 /*
2266 * QCount mask to deal with broken aic7850s that
2267 * sporatically get garbage in the upper bits of
2268 * their QCount registers.
2269 */
2270 AHC_OUTB(ahc, QCNTMASK, ahc->qcntmask);
2271
2272 /* We don't have any busy targets right now */
2273 AHC_OUTB(ahc, ACTIVE_A, 0);
2274 AHC_OUTB(ahc, ACTIVE_B, 0);
2275
2276 /* We don't have any waiting selections */
2277 AHC_OUTB(ahc, WAITING_SCBH, SCB_LIST_NULL);
2278
2279 /* Our disconnection list is empty too */
2280 AHC_OUTB(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
2281
2282 /* Message out buffer starts empty */
2283 AHC_OUTB(ahc, MSG_LEN, 0x00);
2284
2285 /*
2286 * Load the Sequencer program and Enable the adapter
2287 * in "fast" mode.
2288 */
2289 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2290 if(bootverbose)
2291 printf("%s: Downloading Sequencer Program...",
2292 ahc_name(ahc));
2293 #endif
2294
2295 ahc_loadseq(ahc);
2296
2297 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2298 if(bootverbose)
2299 printf("Done\n");
2300 #endif
2301
2302 AHC_OUTB(ahc, SEQCTL, FASTMODE);
2303
2304 unpause_sequencer(ahc, /*unpause_always*/TRUE);
2305
2306 /*
2307 * Note that we are going and return (to probe)
2308 */
2309 ahc->flags |= AHC_INIT;
2310 return (0);
2311 }
2312
2313 static void
2314 ahcminphys(bp)
2315 struct buf *bp;
2316 {
2317 /*
2318 * Even though the card can transfer up to 16megs per command
2319 * we are limited by the number of segments in the dma segment
2320 * list that we can hold. The worst case is that all pages are
2321 * discontinuous physically, hense the "page per segment" limit
2322 * enforced here.
2323 */
2324 if (bp->b_bcount > ((AHC_NSEG - 1) * PAGE_SIZE)) {
2325 bp->b_bcount = ((AHC_NSEG - 1) * PAGE_SIZE);
2326 }
2327 #if defined(__NetBSD__)
2328 minphys(bp);
2329 #endif
2330 }
2331
2332 /*
2333 * start a scsi operation given the command and
2334 * the data address, target, and lun all of which
2335 * are stored in the scsi_xfer struct
2336 */
2337 static int32_t
2338 ahc_scsi_cmd(xs)
2339 struct scsi_xfer *xs;
2340 {
2341 struct scb *scb;
2342 struct ahc_dma_seg *sg;
2343 int seg; /* scatter gather seg being worked on */
2344 int thiskv;
2345 physaddr thisphys, nextphys;
2346 int bytes_this_seg, bytes_this_page, datalen, flags;
2347 struct ahc_data *ahc;
2348 u_short mask;
2349 int s;
2350
2351 ahc = (struct ahc_data *)xs->sc_link->adapter_softc;
2352 mask = (0x01 << (xs->sc_link->target
2353 #if defined(__FreeBSD__)
2354 | ((u_long)xs->sc_link->fordriver & 0x08)));
2355 #elif defined(__NetBSD__)
2356 | (IS_SCSIBUS_B(ahc, xs->sc_link) ? SELBUSB : 0) ));
2357 #endif
2358 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_scsi_cmd\n"));
2359 /*
2360 * get an scb to use. If the transfer
2361 * is from a buf (possibly from interrupt time)
2362 * then we can't allow it to sleep
2363 */
2364 flags = xs->flags;
2365 if (flags & ITSDONE) {
2366 printf("%s: Already done?", ahc_name(ahc));
2367 xs->flags &= ~ITSDONE;
2368 }
2369 if (!(flags & INUSE)) {
2370 printf("%s: Not in use?", ahc_name(ahc));
2371 xs->flags |= INUSE;
2372 }
2373 if (!(scb = ahc_get_scb(ahc, flags))) {
2374 xs->error = XS_DRIVER_STUFFUP;
2375 return (TRY_AGAIN_LATER);
2376 }
2377 SC_DEBUG(xs->sc_link, SDEV_DB3, ("start scb(%p)\n", scb));
2378 scb->xs = xs;
2379 if (flags & SCSI_RESET) {
2380 scb->flags |= SCB_DEVICE_RESET|SCB_IMMED;
2381 scb->control |= MK_MESSAGE;
2382 }
2383 /*
2384 * Put all the arguments for the xfer in the scb
2385 */
2386
2387 if(ahc->tagenable & mask) {
2388 scb->control |= TAG_ENB;
2389 if(ahc->orderedtag & mask) {
2390 printf("Ordered Tag sent\n");
2391 scb->control |= 0x02;
2392 ahc->orderedtag &= ~mask;
2393 }
2394 }
2395 if(ahc->discenable & mask)
2396 scb->control |= DISCENB;
2397 if((ahc->needwdtr & mask) && !(ahc->wdtrpending & mask))
2398 {
2399 scb->control |= MK_MESSAGE;
2400 scb->flags |= SCB_MSGOUT_WDTR;
2401 ahc->wdtrpending |= mask;
2402 }
2403 else if((ahc->needsdtr & mask) && !(ahc->sdtrpending & mask))
2404 {
2405 scb->control |= MK_MESSAGE;
2406 scb->flags |= SCB_MSGOUT_SDTR;
2407 ahc->sdtrpending |= mask;
2408 }
2409 scb->tcl = ((xs->sc_link->target << 4) & 0xF0) |
2410 #if defined(__FreeBSD__)
2411 ((u_long)xs->sc_link->fordriver & 0x08) |
2412 #elif defined(__NetBSD__)
2413 (IS_SCSIBUS_B(ahc,xs->sc_link)? SELBUSB : 0)|
2414 #endif
2415 (xs->sc_link->lun & 0x07);
2416 scb->cmdlen = xs->cmdlen;
2417 scb->cmdpointer = KVTOPHYS(xs->cmd);
2418 xs->resid = 0;
2419 xs->status = 0;
2420 if (xs->datalen) { /* should use S/G only if not zero length */
2421 scb->SG_list_pointer = KVTOPHYS(scb->ahc_dma);
2422 sg = scb->ahc_dma;
2423 seg = 0;
2424 /*
2425 * Set up the scatter gather block
2426 */
2427 SC_DEBUG(xs->sc_link, SDEV_DB4,
2428 ("%ld @%p:- ", xs->datalen, xs->data));
2429 datalen = xs->datalen;
2430 thiskv = (int) xs->data;
2431 thisphys = KVTOPHYS(thiskv);
2432
2433 while ((datalen) && (seg < AHC_NSEG)) {
2434 bytes_this_seg = 0;
2435
2436 /* put in the base address */
2437 sg->addr = thisphys;
2438
2439 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("0x%lx", thisphys));
2440
2441 /* do it at least once */
2442 nextphys = thisphys;
2443 while ((datalen) && (thisphys == nextphys)) {
2444 /*
2445 * This page is contiguous (physically)
2446 * with the the last, just extend the
2447 * length
2448 */
2449 /* how far to the end of the page */
2450 nextphys = (thisphys & (~(PAGE_SIZE- 1)))
2451 + PAGE_SIZE;
2452 bytes_this_page = nextphys - thisphys;
2453 /**** or the data ****/
2454 bytes_this_page = min(bytes_this_page ,datalen);
2455 bytes_this_seg += bytes_this_page;
2456 datalen -= bytes_this_page;
2457
2458 /* get more ready for the next page */
2459 thiskv = (thiskv & (~(PAGE_SIZE - 1)))
2460 + PAGE_SIZE;
2461 if (datalen)
2462 thisphys = KVTOPHYS(thiskv);
2463 }
2464 /*
2465 * next page isn't contiguous, finish the seg
2466 */
2467 SC_DEBUGN(xs->sc_link, SDEV_DB4,
2468 ("(0x%x)", bytes_this_seg));
2469 sg->len = bytes_this_seg;
2470 sg++;
2471 seg++;
2472 }
2473 scb->SG_segment_count = seg;
2474
2475 /* Copy the first SG into the data pointer area */
2476 scb->data = scb->ahc_dma->addr;
2477 scb->datalen = scb->ahc_dma->len;
2478 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("\n"));
2479 if (datalen) {
2480 /* there's still data, must have run out of segs! */
2481 printf("%s: ahc_scsi_cmd: more than %d DMA segs\n",
2482 ahc_name(ahc), AHC_NSEG);
2483 xs->error = XS_DRIVER_STUFFUP;
2484 ahc_free_scb(ahc, scb, flags);
2485 return (COMPLETE);
2486 }
2487 #ifdef AHC_BROKEN_CACHE
2488 if (ahc_broken_cache)
2489 INVALIDATE_CACHE();
2490 #endif
2491 }
2492 else {
2493 /*
2494 * No data xfer, use non S/G values
2495 */
2496 scb->SG_segment_count = 0;
2497 scb->SG_list_pointer = 0;
2498 scb->data = 0;
2499 scb->datalen = 0;
2500 }
2501
2502 #ifdef AHC_DEBUG
2503 if((ahc_debug & AHC_SHOWSCBS) && (xs->sc_link->target == DEBUGTARG))
2504 ahc_print_scb(scb);
2505 #endif
2506 s = splbio();
2507
2508 if( scb->position != SCB_LIST_NULL )
2509 {
2510 /* We already have a valid slot */
2511 u_char curscb;
2512
2513 pause_sequencer(ahc);
2514 curscb = AHC_INB(ahc, SCBPTR);
2515 AHC_OUTB(ahc, SCBPTR, scb->position);
2516 ahc_send_scb(ahc, scb);
2517 AHC_OUTB(ahc, SCBPTR, curscb);
2518 AHC_OUTB(ahc, QINFIFO, scb->position);
2519 unpause_sequencer(ahc, /*unpause_always*/FALSE);
2520 scb->flags |= SCB_ACTIVE;
2521 if (!(flags & SCSI_NOMASK)) {
2522 timeout(ahc_timeout, (caddr_t)scb,
2523 (xs->timeout * hz) / 1000);
2524 }
2525 SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
2526 }
2527 else {
2528 scb->flags |= SCB_WAITINGQ;
2529 STAILQ_INSERT_TAIL(&ahc->waiting_scbs, scb, links);
2530 ahc_run_waiting_queues(ahc);
2531 }
2532 if (!(flags & SCSI_NOMASK)) {
2533 splx(s);
2534 return (SUCCESSFULLY_QUEUED);
2535 }
2536 /*
2537 * If we can't use interrupts, poll for completion
2538 */
2539 SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
2540 do {
2541 if (ahc_poll(ahc, xs->timeout)) {
2542 if (!(xs->flags & SCSI_SILENT))
2543 printf("cmd fail\n");
2544 ahc_timeout(scb);
2545 break;
2546 }
2547 } while (!(xs->flags & ITSDONE)); /* a non command complete intr */
2548 splx(s);
2549 return (COMPLETE);
2550 }
2551
2552
2553 /*
2554 * A scb (and hence an scb entry on the board is put onto the
2555 * free list.
2556 */
2557 static void
2558 ahc_free_scb(ahc, scb, flags)
2559 struct ahc_data *ahc;
2560 int flags;
2561 struct scb *scb;
2562 {
2563 struct scb *wscb;
2564 unsigned int opri;
2565
2566 opri = splbio();
2567
2568 /* Clean up for the next user */
2569 scb->flags = SCB_FREE;
2570 scb->control = 0;
2571 scb->status = 0;
2572
2573 if(scb->position == SCB_LIST_NULL) {
2574 STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
2575 if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
2576 /*
2577 * If there were no SCBs availible, wake anybody waiting
2578 * for one to come free.
2579 */
2580 wakeup((caddr_t)&ahc->free_scbs);
2581 }
2582 /*
2583 * If there are any SCBS on the waiting queue,
2584 * assign the slot of this "freed" SCB to the first
2585 * one. We'll run the waiting queues after all command
2586 * completes for a particular interrupt are completed
2587 * or when we start another command.
2588 */
2589 else if((wscb = ahc->waiting_scbs.stqh_first) != NULL) {
2590 STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
2591 wscb->position = scb->position;
2592 STAILQ_INSERT_TAIL(&ahc->assigned_scbs, wscb, links);
2593 wscb->flags ^= SCB_WAITINGQ|SCB_ASSIGNEDQ;
2594
2595 /*
2596 * The "freed" SCB will need to be assigned a slot
2597 * before being used, so put it in the page_scbs
2598 * queue.
2599 */
2600 scb->position = SCB_LIST_NULL;
2601 STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
2602 if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
2603 /*
2604 * If there were no SCBs availible, wake anybody waiting
2605 * for one to come free.
2606 */
2607 wakeup((caddr_t)&ahc->free_scbs);
2608 }
2609 else {
2610 STAILQ_INSERT_HEAD(&ahc->free_scbs, scb, links);
2611 if(!scb->links.stqe_next && !ahc->page_scbs.stqh_first)
2612 /*
2613 * If there were no SCBs availible, wake anybody waiting
2614 * for one to come free.
2615 */
2616 wakeup((caddr_t)&ahc->free_scbs);
2617 }
2618 #ifdef AHC_DEBUG
2619 ahc->activescbs--;
2620 #endif
2621 splx(opri);
2622 }
2623
2624 /*
2625 * Get a free scb, either one already assigned to a hardware slot
2626 * on the adapter or one that will require an SCB to be paged out before
2627 * use. If there are none, see if we can allocate a new SCB. Otherwise
2628 * either return an error or sleep.
2629 */
2630 static struct scb *
2631 ahc_get_scb(ahc, flags)
2632 struct ahc_data *ahc;
2633 int flags;
2634 {
2635 unsigned opri;
2636 struct scb *scbp;
2637
2638 opri = splbio();
2639 /*
2640 * If we can and have to, sleep waiting for one to come free
2641 * but only if we can't allocate a new one.
2642 */
2643 while (1) {
2644 if((scbp = ahc->free_scbs.stqh_first)) {
2645 STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
2646 }
2647 else if((scbp = ahc->page_scbs.stqh_first)) {
2648 STAILQ_REMOVE_HEAD(&ahc->page_scbs, links);
2649 }
2650 else if(ahc->numscbs < ahc->maxscbs) {
2651 scbp = (struct scb *) malloc(sizeof(struct scb),
2652 M_TEMP, M_NOWAIT);
2653 if (scbp) {
2654 bzero(scbp, sizeof(struct scb));
2655 scbp->tag = ahc->numscbs;
2656 if( ahc->numscbs < ahc->maxhscbs )
2657 scbp->position = ahc->numscbs;
2658 else
2659 scbp->position = SCB_LIST_NULL;
2660 ahc->numscbs++;
2661 /*
2662 * Place in the scbarray
2663 * Never is removed.
2664 */
2665 ahc->scbarray[scbp->tag] = scbp;
2666 }
2667 else {
2668 printf("%s: Can't malloc SCB\n",
2669 ahc_name(ahc));
2670 }
2671 }
2672 else {
2673 if (!(flags & SCSI_NOSLEEP)) {
2674 tsleep((caddr_t)&ahc->free_scbs, PRIBIO,
2675 "ahcscb", 0);
2676 continue;
2677 }
2678 }
2679 break;
2680 }
2681
2682 #ifdef AHC_DEBUG
2683 if (scbp) {
2684 ahc->activescbs++;
2685 if((ahc_debug & AHC_SHOWSCBCNT)
2686 && (ahc->activescbs == ahc->maxhscbs))
2687 printf("%s: Max SCBs active\n", ahc_name(ahc));
2688 }
2689 #endif
2690
2691 splx(opri);
2692
2693 return (scbp);
2694 }
2695
2696 static void ahc_loadseq(ahc)
2697 struct ahc_data *ahc;
2698 {
2699 static u_char seqprog[] = {
2700 #if defined(__FreeBSD__)
2701 # include "aic7xxx_seq.h"
2702 #endif
2703 #if defined(__NetBSD__)
2704 # include <dev/microcode/aic7xxx/aic7xxx_seq.h>
2705 #endif
2706 };
2707
2708 AHC_OUTB(ahc, SEQCTL, PERRORDIS|SEQRESET|LOADRAM);
2709
2710 AHC_OUTSB(ahc, SEQRAM, seqprog, sizeof(seqprog));
2711
2712 do {
2713 AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
2714 } while((AHC_INB(ahc, SEQADDR0) != 0)
2715 || (AHC_INB(ahc, SEQADDR1) != 0));
2716 }
2717
2718 /*
2719 * Function to poll for command completion when
2720 * interrupts are disabled (crash dumps)
2721 */
2722 static int
2723 ahc_poll(ahc, wait)
2724 struct ahc_data *ahc;
2725 int wait; /* in msec */
2726 {
2727 while (--wait) {
2728 DELAY(1000);
2729 if (AHC_INB(ahc, INTSTAT) & INT_PEND)
2730 break;
2731 } if (wait == 0) {
2732 printf("%s: board is not responding\n", ahc_name(ahc));
2733 return (EIO);
2734 }
2735 ahc_intr((void *)ahc);
2736 return (0);
2737 }
2738
2739 static void
2740 ahc_timeout(arg)
2741 void *arg;
2742 {
2743 struct scb *scb = (struct scb *)arg;
2744 struct ahc_data *ahc;
2745 int s, found;
2746 u_char bus_state;
2747 char channel;
2748
2749 s = splbio();
2750
2751 if (!(scb->flags & SCB_ACTIVE)) {
2752 /* Previous timeout took care of me already */
2753 splx(s);
2754 return;
2755 }
2756
2757 ahc = (struct ahc_data *)scb->xs->sc_link->adapter_softc;
2758
2759 if (ahc->in_timeout) {
2760 /*
2761 * Some other SCB has started a recovery operation
2762 * and is still working on cleaning things up.
2763 */
2764 if (scb->flags & SCB_TIMEDOUT) {
2765 /*
2766 * This SCB has been here before and is not the
2767 * recovery SCB. Cut our losses and panic. Its
2768 * better to do this than trash a filesystem.
2769 */
2770 panic("%s: Timed-out command times out "
2771 "again\n", ahc_name(ahc));
2772 }
2773 else if (!(scb->flags & SCB_ABORTED))
2774 {
2775 /*
2776 * This is not the SCB that started this timeout
2777 * processing. Give this scb another lifetime so
2778 * that it can continue once we deal with the
2779 * timeout.
2780 */
2781 scb->flags |= SCB_TIMEDOUT;
2782 timeout(ahc_timeout, (caddr_t)scb,
2783 (scb->xs->timeout * hz) / 1000);
2784 splx(s);
2785 return;
2786 }
2787 }
2788 ahc->in_timeout = TRUE;
2789
2790 /*
2791 * Ensure that the card doesn't do anything
2792 * behind our back.
2793 */
2794 pause_sequencer(ahc);
2795
2796 sc_print_addr(scb->xs->sc_link);
2797 printf("timed out ");
2798 /*
2799 * Take a snapshot of the bus state and print out
2800 * some information so we can track down driver bugs.
2801 */
2802 bus_state = AHC_INB(ahc, LASTPHASE);
2803
2804 switch(bus_state & PHASE_MASK)
2805 {
2806 case P_DATAOUT:
2807 printf("in dataout phase");
2808 break;
2809 case P_DATAIN:
2810 printf("in datain phase");
2811 break;
2812 case P_COMMAND:
2813 printf("in command phase");
2814 break;
2815 case P_MESGOUT:
2816 printf("in message out phase");
2817 break;
2818 case P_STATUS:
2819 printf("in status phase");
2820 break;
2821 case P_MESGIN:
2822 printf("in message in phase");
2823 break;
2824 default:
2825 printf("while idle, LASTPHASE == 0x%x",
2826 bus_state);
2827 /*
2828 * We aren't in a valid phase, so assume we're
2829 * idle.
2830 */
2831 bus_state = 0;
2832 break;
2833 }
2834
2835 printf(", SCSISIGI == 0x%x\n", AHC_INB(ahc, SCSISIGI));
2836
2837 /* Decide our course of action */
2838
2839 if(scb->flags & SCB_ABORTED)
2840 {
2841 /*
2842 * Been down this road before.
2843 * Do a full bus reset.
2844 */
2845 char channel = (scb->tcl & SELBUSB)
2846 ? 'B': 'A';
2847 found = ahc_reset_channel(ahc, channel, scb->tag,
2848 XS_TIMEOUT, /*Initiate Reset*/TRUE);
2849 printf("%s: Issued Channel %c Bus Reset #1. "
2850 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
2851 ahc->in_timeout = FALSE;
2852 }
2853 else if(scb->control & TAG_ENB) {
2854 /*
2855 * We could be starving this command
2856 * try sending an ordered tag command
2857 * to the target we come from.
2858 */
2859 scb->flags |= SCB_ABORTED|SCB_SENTORDEREDTAG;
2860 ahc->orderedtag |= 0xFF;
2861 timeout(ahc_timeout, (caddr_t)scb, (5 * hz));
2862 unpause_sequencer(ahc, /*unpause_always*/FALSE);
2863 printf("Ordered Tag queued\n");
2864 goto done;
2865 }
2866 else {
2867 /*
2868 * Send a Bus Device Reset Message:
2869 * The target that is holding up the bus may not
2870 * be the same as the one that triggered this timeout
2871 * (different commands have different timeout lengths).
2872 * It is also impossible to get a message to a target
2873 * if we are in a "frozen" data transfer phase. Our
2874 * strategy here is to queue a bus device reset message
2875 * to the timed out target if it is disconnected.
2876 * Otherwise, if we have an active target we stuff the
2877 * message buffer with a bus device reset message and
2878 * assert ATN in the hopes that the target will let go
2879 * of the bus and finally disconnect. If this fails,
2880 * we'll get another timeout 2 seconds later which will
2881 * cause a bus reset.
2882 *
2883 * XXX If the SCB is paged out, we simply reset the
2884 * bus. We should probably queue a new command
2885 * instead.
2886 */
2887
2888 /* Test to see if scb is disconnected */
2889 if( !(scb->flags & SCB_PAGED_OUT ) ){
2890 u_char active_scb;
2891 struct scb *active_scbp;
2892
2893 active_scb = AHC_INB(ahc, SCBPTR);
2894 active_scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
2895 AHC_OUTB(ahc, SCBPTR, scb->position);
2896
2897 if(AHC_INB(ahc, SCB_CONTROL) & DISCONNECTED) {
2898 if(ahc->flags & AHC_PAGESCBS) {
2899 /*
2900 * Pull this SCB out of the
2901 * disconnected list.
2902 */
2903 u_char prev = AHC_INB(ahc, SCB_PREV);
2904 u_char next = AHC_INB(ahc, SCB_NEXT);
2905 if(prev == SCB_LIST_NULL) {
2906 /* At the head */
2907 AHC_OUTB(ahc, DISCONNECTED_SCBH,
2908 next );
2909 }
2910 else {
2911 AHC_OUTB(ahc, SCBPTR, prev);
2912 AHC_OUTB(ahc, SCB_NEXT, next);
2913 if(next != SCB_LIST_NULL) {
2914 AHC_OUTB(ahc, SCBPTR,
2915 next);
2916 AHC_OUTB(ahc, SCB_PREV,
2917 prev);
2918 }
2919 AHC_OUTB(ahc, SCBPTR,
2920 scb->position);
2921 }
2922 }
2923 scb->flags |= SCB_DEVICE_RESET|SCB_ABORTED;
2924 scb->control &= DISCENB;
2925 scb->control |= MK_MESSAGE;
2926 scb->cmdlen = 0;
2927 scb->SG_segment_count = 0;
2928 scb->SG_list_pointer = 0;
2929 scb->data = 0;
2930 scb->datalen = 0;
2931 ahc_send_scb(ahc, scb);
2932 ahc_add_waiting_scb(ahc, scb);
2933 timeout(ahc_timeout, (caddr_t)scb, (2 * hz));
2934 sc_print_addr(scb->xs->sc_link);
2935 printf("BUS DEVICE RESET message queued.\n");
2936 AHC_OUTB(ahc, SCBPTR, active_scb);
2937 unpause_sequencer(ahc, /*unpause_always*/FALSE);
2938 goto done;
2939 }
2940 /* Is the active SCB really active? */
2941 else if((active_scbp->flags & SCB_ACTIVE) && bus_state){
2942 AHC_OUTB(ahc, MSG_LEN, 1);
2943 AHC_OUTB(ahc, MSG0, MSG_BUS_DEV_RESET);
2944 AHC_OUTB(ahc, SCSISIGO, bus_state|ATNO);
2945 sc_print_addr(active_scbp->xs->sc_link);
2946 printf("asserted ATN - device reset in "
2947 "message buffer\n");
2948 active_scbp->flags |= SCB_DEVICE_RESET
2949 | SCB_ABORTED;
2950 if(active_scbp != scb) {
2951 untimeout(ahc_timeout,
2952 (caddr_t)active_scbp);
2953 /* Give scb a new lease on life */
2954 timeout(ahc_timeout, (caddr_t)scb,
2955 (scb->xs->timeout * hz) / 1000);
2956 }
2957 timeout(ahc_timeout, (caddr_t)active_scbp,
2958 (2 * hz));
2959 AHC_OUTB(ahc, SCBPTR, active_scb);
2960 unpause_sequencer(ahc, /*unpause_always*/FALSE);
2961 goto done;
2962 }
2963 }
2964 /*
2965 * No active target or a paged out SCB.
2966 * Try reseting the bus.
2967 */
2968 channel = (scb->tcl & SELBUSB) ? 'B': 'A';
2969 found = ahc_reset_channel(ahc, channel, scb->tag,
2970 XS_TIMEOUT,
2971 /*Initiate Reset*/TRUE);
2972 printf("%s: Issued Channel %c Bus Reset #2. "
2973 "%d SCBs aborted\n", ahc_name(ahc), channel,
2974 found);
2975 ahc->in_timeout = FALSE;
2976 }
2977 done:
2978 splx(s);
2979 }
2980
2981
2982 /*
2983 * The device at the given target/channel has been reset. Abort
2984 * all active and queued scbs for that target/channel.
2985 */
2986 static int
2987 ahc_reset_device(ahc, target, channel, timedout_scb, xs_error)
2988 struct ahc_data *ahc;
2989 int target;
2990 char channel;
2991 u_char timedout_scb;
2992 u_int32_t xs_error;
2993 {
2994 struct scb *scbp;
2995 u_char active_scb;
2996 int i = 0;
2997 int found = 0;
2998
2999 /* restore this when we're done */
3000 active_scb = AHC_INB(ahc, SCBPTR);
3001
3002 /*
3003 * Search the QINFIFO.
3004 */
3005 {
3006 u_char saved_queue[AHC_SCB_MAX];
3007 u_char queued = AHC_INB(ahc, QINCNT) & ahc->qcntmask;
3008
3009 for (i = 0; i < (queued - found); i++) {
3010 saved_queue[i] = AHC_INB(ahc, QINFIFO);
3011 AHC_OUTB(ahc, SCBPTR, saved_queue[i]);
3012 scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
3013 if (ahc_match_scb (scbp, target, channel)){
3014 /*
3015 * We found an scb that needs to be aborted.
3016 */
3017 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3018 scbp->xs->error |= xs_error;
3019 if(scbp->position != timedout_scb)
3020 untimeout(ahc_timeout, (caddr_t)scbp);
3021 AHC_OUTB(ahc, SCB_CONTROL, 0);
3022 i--;
3023 found++;
3024 }
3025 }
3026 /* Now put the saved scbs back. */
3027 for (queued = 0; queued < i; queued++) {
3028 AHC_OUTB(ahc, QINFIFO, saved_queue[queued]);
3029 }
3030 }
3031
3032 /*
3033 * Search waiting for selection list.
3034 */
3035 {
3036 u_char next, prev;
3037
3038 next = AHC_INB(ahc, WAITING_SCBH); /* Start at head of list. */
3039 prev = SCB_LIST_NULL;
3040
3041 while (next != SCB_LIST_NULL) {
3042 AHC_OUTB(ahc, SCBPTR, next);
3043 scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
3044 /*
3045 * Select the SCB.
3046 */
3047 if (ahc_match_scb(scbp, target, channel)) {
3048 next = ahc_abort_wscb(ahc, scbp, prev,
3049 timedout_scb, xs_error);
3050 found++;
3051 }
3052 else {
3053 prev = next;
3054 next = AHC_INB(ahc, SCB_NEXT);
3055 }
3056 }
3057 }
3058 /*
3059 * Go through the entire SCB array now and look for
3060 * commands for this target that are active. These
3061 * are other (most likely tagged) commands that
3062 * were disconnected when the reset occured.
3063 */
3064 for(i = 0; i < ahc->numscbs; i++) {
3065 scbp = ahc->scbarray[i];
3066 if((scbp->flags & SCB_ACTIVE)
3067 && ahc_match_scb(scbp, target, channel)) {
3068 /* Ensure the target is "free" */
3069 ahc_unbusy_target(ahc, target, channel);
3070 if( !(scbp->flags & SCB_PAGED_OUT) )
3071 {
3072 AHC_OUTB(ahc, SCBPTR, scbp->position);
3073 AHC_OUTB(ahc, SCB_CONTROL, 0);
3074 }
3075 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3076 scbp->xs->error |= xs_error;
3077 if(scbp->tag != timedout_scb)
3078 untimeout(ahc_timeout, (caddr_t)scbp);
3079 found++;
3080 }
3081 }
3082 AHC_OUTB(ahc, SCBPTR, active_scb);
3083 return found;
3084 }
3085
3086 /*
3087 * Manipulate the waiting for selection list and return the
3088 * scb that follows the one that we remove.
3089 */
3090 static u_char
3091 ahc_abort_wscb (ahc, scbp, prev, timedout_scb, xs_error)
3092 struct ahc_data *ahc;
3093 struct scb *scbp;
3094 u_char prev;
3095 u_char timedout_scb;
3096 u_int32_t xs_error;
3097 {
3098 u_char curscbp, next;
3099 int target = ((scbp->tcl >> 4) & 0x0f);
3100 char channel = (scbp->tcl & SELBUSB) ? 'B' : 'A';
3101 /*
3102 * Select the SCB we want to abort and
3103 * pull the next pointer out of it.
3104 */
3105 curscbp = AHC_INB(ahc, SCBPTR);
3106 AHC_OUTB(ahc, SCBPTR, scbp->position);
3107 next = AHC_INB(ahc, SCB_NEXT);
3108
3109 /* Clear the necessary fields */
3110 AHC_OUTB(ahc, SCB_CONTROL, 0);
3111 AHC_OUTB(ahc, SCB_NEXT, SCB_LIST_NULL);
3112 ahc_unbusy_target(ahc, target, channel);
3113
3114 /* update the waiting list */
3115 if( prev == SCB_LIST_NULL )
3116 /* First in the list */
3117 AHC_OUTB(ahc, WAITING_SCBH, next);
3118 else {
3119 /*
3120 * Select the scb that pointed to us
3121 * and update its next pointer.
3122 */
3123 AHC_OUTB(ahc, SCBPTR, prev);
3124 AHC_OUTB(ahc, SCB_NEXT, next);
3125 }
3126 /*
3127 * Point us back at the original scb position
3128 * and inform the SCSI system that the command
3129 * has been aborted.
3130 */
3131 AHC_OUTB(ahc, SCBPTR, curscbp);
3132 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3133 scbp->xs->error |= xs_error;
3134 if(scbp->tag != timedout_scb)
3135 untimeout(ahc_timeout, (caddr_t)scbp);
3136 return next;
3137 }
3138
3139 static void
3140 ahc_busy_target(ahc, target, channel)
3141 struct ahc_data *ahc;
3142 u_char target;
3143 char channel;
3144 {
3145 u_char active;
3146 u_long active_port = ACTIVE_A;
3147
3148 if(target > 0x07 || channel == 'B') {
3149 /*
3150 * targets on the Second channel or
3151 * above id 7 store info in byte two
3152 * of HA_ACTIVE
3153 */
3154 active_port++;
3155 }
3156 active = AHC_INB(ahc, active_port);
3157 active |= (0x01 << (target & 0x07));
3158 AHC_OUTB(ahc, active_port, active);
3159 }
3160
3161 static void
3162 ahc_unbusy_target(ahc, target, channel)
3163 struct ahc_data *ahc;
3164 u_char target;
3165 char channel;
3166 {
3167 u_char active;
3168 u_long active_port = ACTIVE_A;
3169
3170 if(target > 0x07 || channel == 'B') {
3171 /*
3172 * targets on the Second channel or
3173 * above id 7 store info in byte two
3174 * of HA_ACTIVE
3175 */
3176 active_port++;
3177 }
3178 active = AHC_INB(ahc, active_port);
3179 active &= ~(0x01 << (target & 0x07));
3180 AHC_OUTB(ahc, active_port, active);
3181 }
3182
3183 static void
3184 ahc_reset_current_bus(ahc)
3185 struct ahc_data *ahc;
3186 {
3187 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
3188 DELAY(1000);
3189 AHC_OUTB(ahc, SCSISEQ, 0);
3190 }
3191
3192 static int
3193 ahc_reset_channel(ahc, channel, timedout_scb, xs_error, initiate_reset)
3194 struct ahc_data *ahc;
3195 char channel;
3196 u_char timedout_scb;
3197 u_int32_t xs_error;
3198 u_char initiate_reset;
3199 {
3200 u_char sblkctl;
3201 char cur_channel;
3202 u_long offset, offset_max;
3203 int found;
3204
3205 /*
3206 * Clean up all the state information for the
3207 * pending transactions on this bus.
3208 */
3209 found = ahc_reset_device(ahc, ALL_TARGETS, channel,
3210 timedout_scb, xs_error);
3211 if(channel == 'B'){
3212 ahc->needsdtr |= (ahc->needsdtr_orig & 0xff00);
3213 ahc->sdtrpending &= 0x00ff;
3214 AHC_OUTB(ahc, ACTIVE_B, 0);
3215 offset = TARG_SCRATCH + 8;
3216 offset_max = TARG_SCRATCH + 16;
3217 }
3218 else if (ahc->type & AHC_WIDE){
3219 ahc->needsdtr = ahc->needsdtr_orig;
3220 ahc->needwdtr = ahc->needwdtr_orig;
3221 ahc->sdtrpending = 0;
3222 ahc->wdtrpending = 0;
3223 AHC_OUTB(ahc, ACTIVE_A, 0);
3224 AHC_OUTB(ahc, ACTIVE_B, 0);
3225 offset = TARG_SCRATCH;
3226 offset_max = TARG_SCRATCH + 16;
3227 }
3228 else{
3229 ahc->needsdtr |= (ahc->needsdtr_orig & 0x00ff);
3230 ahc->sdtrpending &= 0xff00;
3231 AHC_OUTB(ahc, ACTIVE_A, 0);
3232 offset = TARG_SCRATCH;
3233 offset_max = TARG_SCRATCH + 8;
3234 }
3235 for(;offset < offset_max;offset++) {
3236 /*
3237 * Revert to async/narrow transfers
3238 * until we renegotiate.
3239 */
3240 u_char targ_scratch;
3241
3242 targ_scratch = AHC_INB(ahc, offset);
3243 targ_scratch &= SXFR;
3244 AHC_OUTB(ahc, offset, targ_scratch);
3245 }
3246
3247 /*
3248 * Reset the bus if we are initiating this reset and
3249 * restart/unpause the sequencer
3250 */
3251 /* Case 1: Command for another bus is active */
3252 sblkctl = AHC_INB(ahc, SBLKCTL);
3253 cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
3254 if(cur_channel != channel)
3255 {
3256 /*
3257 * Stealthily reset the other bus
3258 * without upsetting the current bus
3259 */
3260 AHC_OUTB(ahc, SBLKCTL, sblkctl ^ SELBUSB);
3261 if( initiate_reset )
3262 {
3263 ahc_reset_current_bus(ahc);
3264 }
3265 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
3266 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
3267 AHC_OUTB(ahc, SBLKCTL, sblkctl);
3268 unpause_sequencer(ahc, /*unpause_always*/TRUE);
3269 }
3270 /* Case 2: A command from this bus is active or we're idle */
3271 else {
3272 if( initiate_reset )
3273 {
3274 ahc_reset_current_bus(ahc);
3275 }
3276 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
3277 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
3278 restart_sequencer(ahc);
3279 }
3280 ahc_run_done_queue(ahc);
3281 return found;
3282 }
3283
3284 void
3285 ahc_run_done_queue(ahc)
3286 struct ahc_data *ahc;
3287 {
3288 int i;
3289 struct scb *scbp;
3290
3291 for(i = 0; i < ahc->numscbs; i++) {
3292 scbp = ahc->scbarray[i];
3293 if(scbp->flags & SCB_QUEUED_FOR_DONE)
3294 ahc_done(ahc, scbp);
3295 }
3296 }
3297
3298 static int
3299 ahc_match_scb (scb, target, channel)
3300 struct scb *scb;
3301 int target;
3302 char channel;
3303 {
3304 int targ = (scb->tcl >> 4) & 0x0f;
3305 char chan = (scb->tcl & SELBUSB) ? 'B' : 'A';
3306
3307 if (target == ALL_TARGETS)
3308 return (chan == channel);
3309 else
3310 return ((chan == channel) && (targ == target));
3311 }
3312
3313
3314 static void
3315 ahc_construct_sdtr(ahc, start_byte, period, offset)
3316 struct ahc_data *ahc;
3317 int start_byte;
3318 u_int8_t period;
3319 u_int8_t offset;
3320 {
3321 AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
3322 AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_SDTR_LEN);
3323 AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_SDTR);
3324 AHC_OUTB(ahc, MSG3 + start_byte, period);
3325 AHC_OUTB(ahc, MSG4 + start_byte, offset);
3326 AHC_OUTB(ahc, MSG_LEN, start_byte + 5);
3327 }
3328
3329 static void
3330 ahc_construct_wdtr(ahc, start_byte, bus_width)
3331 struct ahc_data *ahc;
3332 int start_byte;
3333 u_int8_t bus_width;
3334 {
3335 AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
3336 AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_WDTR_LEN);
3337 AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_WDTR);
3338 AHC_OUTB(ahc, MSG3 + start_byte, bus_width);
3339 AHC_OUTB(ahc, MSG_LEN, start_byte + 4);
3340 }
3341