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