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