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