aic7xxx.c revision 1.23 1 /* $NetBSD: aic7xxx.c,v 1.23 1997/04/10 02:48:38 cgd 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 printf("%s:%c:%d: refuses WIDE negotiation. Using "
1543 "8bit transfers\n", ahc_name(ahc),
1544 channel, target);
1545 } else if(ahc->sdtrpending & targ_mask){
1546 /* note asynch xfers and clear flag */
1547 targ_scratch &= 0xf0;
1548 ahc->needsdtr &= ~targ_mask;
1549 ahc->sdtrpending &= ~targ_mask;
1550 printf("%s:%c:%d: refuses synchronous negotiation. "
1551 "Using asynchronous transfers\n",
1552 ahc_name(ahc),
1553 channel, target);
1554 } else {
1555 /*
1556 * Otherwise, we ignore it.
1557 */
1558 #ifdef AHC_DEBUG
1559 if(ahc_debug & AHC_SHOWMISC)
1560 printf("%s:%c:%d: Message reject -- ignored\n",
1561 ahc_name(ahc), channel, target);
1562 #endif
1563 break;
1564 }
1565 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset, targ_scratch);
1566 AHC_OUTB(ahc, SCSIRATE, targ_scratch);
1567 break;
1568 }
1569 case BAD_STATUS:
1570 {
1571 int scb_index;
1572 struct scsi_xfer *xs;
1573
1574 /* The sequencer will notify us when a command
1575 * has an error that would be of interest to
1576 * the kernel. This allows us to leave the sequencer
1577 * running in the common case of command completes
1578 * without error.
1579 */
1580
1581 scb_index = AHC_INB(ahc, SCB_TAG);
1582 scb = ahc->scbarray[scb_index];
1583
1584 /*
1585 * Set the default return value to 0 (don't
1586 * send sense). The sense code will change
1587 * this if needed and this reduces code
1588 * duplication.
1589 */
1590 AHC_OUTB(ahc, RETURN_1, 0);
1591 if (!(scb && (scb->flags & SCB_ACTIVE))) {
1592 printf("%s:%c:%d: ahc_intr - referenced scb "
1593 "not valid during seqint 0x%x scb(%d)\n",
1594 ahc_name(ahc),
1595 channel, target, intstat,
1596 scb_index);
1597 goto clear;
1598 }
1599
1600 xs = scb->xs;
1601
1602 scb->status = AHC_INB(ahc, SCB_TARGET_STATUS);
1603
1604 #ifdef AHC_DEBUG
1605 if((ahc_debug & AHC_SHOWSCBS)
1606 && xs->sc_link->target == DEBUGTARGET)
1607 ahc_print_scb(scb);
1608 #endif
1609 xs->status = scb->status;
1610 switch(scb->status){
1611 case SCSI_OK:
1612 printf("%s: Interrupted for staus of"
1613 " 0???\n", ahc_name(ahc));
1614 break;
1615 case SCSI_CHECK:
1616 #ifdef AHC_DEBUG
1617 if(ahc_debug & AHC_SHOWSENSE)
1618 {
1619
1620 sc_print_addr(xs->sc_link);
1621 printf("requests Check Status\n");
1622 }
1623 #endif
1624
1625 if ((xs->error == XS_NOERROR)
1626 && !(scb->flags & SCB_SENSE)) {
1627 struct ahc_dma_seg *sg = scb->ahc_dma;
1628 struct scsi_sense *sc = &(scb->sense_cmd);
1629 #ifdef AHC_DEBUG
1630 if (ahc_debug & AHC_SHOWSENSE)
1631 {
1632 sc_print_addr(xs->sc_link);
1633 printf("Sending Sense\n");
1634 }
1635 #endif
1636 #if defined(__FreeBSD__)
1637 sc->op_code = REQUEST_SENSE;
1638 #elif defined(__NetBSD__)
1639 sc->opcode = REQUEST_SENSE;
1640 #endif
1641 sc->byte2 = xs->sc_link->lun << 5;
1642 sc->length = sizeof(struct scsi_sense_data);
1643 sc->control = 0;
1644 sg->addr = KVTOPHYS(&xs->sense);
1645 sg->len = sizeof(struct scsi_sense_data);
1646
1647 scb->control &= DISCENB;
1648 scb->status = 0;
1649 scb->SG_segment_count = 1;
1650 scb->SG_list_pointer = KVTOPHYS(sg);
1651 scb->data = sg->addr;
1652 scb->datalen = sg->len;
1653 #ifdef AHC_BROKEN_CACHE
1654 if (ahc_broken_cache)
1655 INVALIDATE_CACHE();
1656 #endif
1657 scb->cmdpointer = KVTOPHYS(sc);
1658 scb->cmdlen = sizeof(*sc);
1659
1660 scb->flags |= SCB_SENSE;
1661 ahc_send_scb(ahc, scb);
1662 /*
1663 * Ensure that the target is "BUSY"
1664 * so we don't get overlapping
1665 * commands if we happen to be doing
1666 * tagged I/O.
1667 */
1668 ahc_busy_target(ahc, target, channel);
1669
1670 /*
1671 * Make us the next command to run
1672 */
1673 ahc_add_waiting_scb(ahc, scb);
1674 AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
1675 break;
1676 }
1677 /*
1678 * Clear the SCB_SENSE Flag and have
1679 * the sequencer do a normal command
1680 * complete with either a "DRIVER_STUFFUP"
1681 * error or whatever other error condition
1682 * we already had.
1683 */
1684 scb->flags &= ~SCB_SENSE;
1685 if (xs->error == XS_NOERROR)
1686 xs->error = XS_DRIVER_STUFFUP;
1687 break;
1688 case SCSI_BUSY:
1689 xs->error = XS_BUSY;
1690 sc_print_addr(xs->sc_link);
1691 printf("Target Busy\n");
1692 break;
1693 case SCSI_QUEUE_FULL:
1694 /*
1695 * The upper level SCSI code will someday
1696 * handle this properly.
1697 */
1698 sc_print_addr(xs->sc_link);
1699 printf("Queue Full\n");
1700 scb->flags |= SCB_ASSIGNEDQ;
1701 STAILQ_INSERT_TAIL(&ahc->assigned_scbs,scb, links);
1702 AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
1703 break;
1704 default:
1705 sc_print_addr(xs->sc_link);
1706 printf("unexpected targ_status: %x\n", scb->status);
1707 xs->error = XS_DRIVER_STUFFUP;
1708 break;
1709 }
1710 break;
1711 }
1712 case RESIDUAL:
1713 {
1714 int scb_index;
1715 struct scsi_xfer *xs;
1716
1717 scb_index = AHC_INB(ahc, SCB_TAG);
1718 scb = ahc->scbarray[scb_index];
1719 xs = scb->xs;
1720 /*
1721 * Don't clobber valid resid info with
1722 * a resid coming from a check sense
1723 * operation.
1724 */
1725 if (!(scb->flags & SCB_SENSE)) {
1726 int resid_sgs;
1727
1728 /*
1729 * Remainder of the SG where the transfer
1730 * stopped.
1731 */
1732 xs->resid = (AHC_INB(ahc, SCB_RESID_DCNT2)<<16) |
1733 (AHC_INB(ahc, SCB_RESID_DCNT1)<<8) |
1734 AHC_INB(ahc, SCB_RESID_DCNT0);
1735
1736 /*
1737 * Add up the contents of all residual
1738 * SG segments that are after the SG where
1739 * the transfer stopped.
1740 */
1741 resid_sgs = AHC_INB(ahc, SCB_RESID_SGCNT) - 1;
1742 while (resid_sgs > 0) {
1743 int sg;
1744
1745 sg = scb->SG_segment_count - resid_sgs;
1746 xs->resid += scb->ahc_dma[sg].len;
1747 resid_sgs--;
1748 }
1749
1750 #if defined(__FreeBSD__)
1751 xs->flags |= SCSI_RESID_VALID;
1752 #elif defined(__NetBSD__)
1753 /* XXX - Update to do this right */
1754 #endif
1755 #ifdef AHC_DEBUG
1756 if (ahc_debug & AHC_SHOWMISC) {
1757 sc_print_addr(xs->sc_link);
1758 printf("Handled Residual of %ld bytes\n"
1759 ,xs->resid);
1760 }
1761 #endif
1762 }
1763 break;
1764 }
1765 case ABORT_TAG:
1766 {
1767 int scb_index;
1768 struct scsi_xfer *xs;
1769
1770 scb_index = AHC_INB(ahc, SCB_TAG);
1771 scb = ahc->scbarray[scb_index];
1772 xs = scb->xs;
1773 /*
1774 * We didn't recieve a valid tag back from
1775 * the target on a reconnect.
1776 */
1777 sc_print_addr(xs->sc_link);
1778 printf("invalid tag received -- sending ABORT_TAG\n");
1779 xs->error = XS_DRIVER_STUFFUP;
1780 untimeout(ahc_timeout, (caddr_t)scb);
1781 ahc_done(ahc, scb);
1782 break;
1783 }
1784 case AWAITING_MSG:
1785 {
1786 int scb_index;
1787 scb_index = AHC_INB(ahc, SCB_TAG);
1788 scb = ahc->scbarray[scb_index];
1789 /*
1790 * This SCB had a zero length command, informing
1791 * the sequencer that we wanted to send a special
1792 * message to this target. We only do this for
1793 * BUS_DEVICE_RESET messages currently.
1794 */
1795 if (scb->flags & SCB_DEVICE_RESET) {
1796 AHC_OUTB(ahc, MSG0,
1797 MSG_BUS_DEV_RESET);
1798 AHC_OUTB(ahc, MSG_LEN, 1);
1799 printf("Bus Device Reset Message Sent\n");
1800 } else if (scb->flags & SCB_MSGOUT_WDTR) {
1801 ahc_construct_wdtr(ahc, AHC_INB(ahc, MSG_LEN),
1802 BUS_16_BIT);
1803 } else if (scb->flags & SCB_MSGOUT_SDTR) {
1804 u_int8_t target_scratch;
1805 u_int8_t ultraenable;
1806 int sxfr;
1807 int i;
1808
1809 /* Pull the user defined setting */
1810 target_scratch = AHC_INB(ahc, TARG_SCRATCH
1811 + scratch_offset);
1812
1813 sxfr = target_scratch & SXFR;
1814 if (scratch_offset < 8)
1815 ultraenable = AHC_INB(ahc, ULTRA_ENB);
1816 else
1817 ultraenable = AHC_INB(ahc, ULTRA_ENB + 1);
1818
1819 if (ultraenable & targ_mask)
1820 /* Want an ultra speed in the table */
1821 sxfr |= 0x100;
1822
1823 for (i = 0; i < ahc_num_syncrates; i++)
1824 if (sxfr == ahc_syncrates[i].sxfr)
1825 break;
1826
1827 ahc_construct_sdtr(ahc, AHC_INB(ahc, MSG_LEN),
1828 ahc_syncrates[i].period,
1829 target_scratch & WIDEXFER ?
1830 MAX_OFFSET_16BIT : MAX_OFFSET_8BIT);
1831 } else
1832 panic("ahc_intr: AWAITING_MSG for an SCB that "
1833 "does not have a waiting message");
1834 break;
1835 }
1836 case IMMEDDONE:
1837 {
1838 /*
1839 * Take care of device reset messages
1840 */
1841 u_char scbindex = AHC_INB(ahc, SCB_TAG);
1842 scb = ahc->scbarray[scbindex];
1843 if (scb->flags & SCB_DEVICE_RESET) {
1844 u_char targ_scratch;
1845 int found;
1846 /*
1847 * Go back to async/narrow transfers and
1848 * renegotiate.
1849 */
1850 ahc_unbusy_target(ahc, target, channel);
1851 ahc->needsdtr |= ahc->needsdtr_orig & targ_mask;
1852 ahc->needwdtr |= ahc->needwdtr_orig & targ_mask;
1853 ahc->sdtrpending &= ~targ_mask;
1854 ahc->wdtrpending &= ~targ_mask;
1855 targ_scratch = AHC_INB(ahc, TARG_SCRATCH
1856 + scratch_offset);
1857 targ_scratch &= SXFR;
1858 AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
1859 targ_scratch);
1860 found = ahc_reset_device(ahc, target,
1861 channel, SCB_LIST_NULL,
1862 XS_NOERROR);
1863 sc_print_addr(scb->xs->sc_link);
1864 printf("Bus Device Reset delivered. "
1865 "%d SCBs aborted\n", found);
1866 ahc->in_timeout = FALSE;
1867 ahc_run_done_queue(ahc);
1868 } else
1869 panic("ahc_intr: Immediate complete for "
1870 "unknown operation.");
1871 break;
1872 }
1873 case DATA_OVERRUN:
1874 {
1875 /*
1876 * When the sequencer detects an overrun, it
1877 * sets STCNT to 0x00ffffff and allows the
1878 * target to complete its transfer in
1879 * BITBUCKET mode.
1880 */
1881 u_char scbindex = AHC_INB(ahc, SCB_TAG);
1882 u_int32_t overrun;
1883 scb = ahc->scbarray[scbindex];
1884 overrun = AHC_INB(ahc, STCNT0)
1885 | (AHC_INB(ahc, STCNT1) << 8)
1886 | (AHC_INB(ahc, STCNT2) << 16);
1887 overrun = 0x00ffffff - overrun;
1888 sc_print_addr(scb->xs->sc_link);
1889 printf("data overrun of %d bytes detected."
1890 " Forcing a retry.\n", overrun);
1891 /*
1892 * Set this and it will take affect when the
1893 * target does a command complete.
1894 */
1895 scb->xs->error = XS_DRIVER_STUFFUP;
1896 break;
1897 }
1898 #if NOT_YET
1899 /* XXX Fill these in later */
1900 case MESG_BUFFER_BUSY:
1901 break;
1902 case MSGIN_PHASEMIS:
1903 break;
1904 #endif
1905 default:
1906 printf("ahc_intr: seqint, "
1907 "intstat == 0x%x, scsisigi = 0x%x\n",
1908 intstat, AHC_INB(ahc, SCSISIGI));
1909 break;
1910 }
1911
1912 clear:
1913 /*
1914 * Clear the upper byte that holds SEQINT status
1915 * codes and clear the SEQINT bit.
1916 */
1917 AHC_OUTB(ahc, CLRINT, CLRSEQINT);
1918
1919 /*
1920 * The sequencer is paused immediately on
1921 * a SEQINT, so we should restart it when
1922 * we're done.
1923 */
1924 unpause_sequencer(ahc, /*unpause_always*/TRUE);
1925 }
1926
1927 /*
1928 * We have a scb which has been processed by the
1929 * adaptor, now we look to see how the operation
1930 * went.
1931 */
1932 static void
1933 ahc_done(ahc, scb)
1934 struct ahc_data *ahc;
1935 struct scb *scb;
1936 {
1937 struct scsi_xfer *xs = scb->xs;
1938
1939 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_done\n"));
1940 /*
1941 * Put the results of the operation
1942 * into the xfer and call whoever started it
1943 */
1944 #if defined(__NetBSD__)
1945 if (xs->error != XS_NOERROR) {
1946 /* Don't override the error value. */
1947 } else if (scb->flags & SCB_ABORTED) {
1948 xs->error = XS_DRIVER_STUFFUP;
1949 } else
1950 #endif
1951 if(scb->flags & SCB_SENSE)
1952 xs->error = XS_SENSE;
1953 if(scb->flags & SCB_SENTORDEREDTAG)
1954 ahc->in_timeout = FALSE;
1955 #if defined(__FreeBSD__)
1956 if ((xs->flags & SCSI_ERR_OK) && !(xs->error == XS_SENSE)) {
1957 /* All went correctly OR errors expected */
1958 xs->error = XS_NOERROR;
1959 }
1960 #elif defined(__NetBSD__)
1961 /*
1962 * Since NetBSD doesn't have error ignoring operation mode
1963 * (SCSI_ERR_OK in FreeBSD), we don't have to care this case.
1964 */
1965 #endif
1966 xs->flags |= ITSDONE;
1967 #ifdef AHC_TAGENABLE
1968 if(xs->cmd->opcode == INQUIRY && xs->error == XS_NOERROR)
1969 {
1970 struct scsi_inquiry_data *inq_data;
1971 u_short mask = 0x01 << (xs->sc_link->target |
1972 (scb->tcl & 0x08));
1973 /*
1974 * Sneak a look at the results of the SCSI Inquiry
1975 * command and see if we can do Tagged queing. This
1976 * should really be done by the higher level drivers.
1977 */
1978 inq_data = (struct scsi_inquiry_data *)xs->data;
1979 if((inq_data->flags & SID_CmdQue) && !(ahc->tagenable & mask))
1980 {
1981 printf("%s: target %d Tagged Queuing Device\n",
1982 ahc_name(ahc), xs->sc_link->target);
1983 ahc->tagenable |= mask;
1984 if(ahc->maxhscbs >= 16 || (ahc->flags & AHC_PAGESCBS)) {
1985 /* Default to 8 tags */
1986 xs->sc_link->opennings += 6;
1987 }
1988 else
1989 {
1990 /*
1991 * Default to 4 tags on whimpy
1992 * cards that don't have much SCB
1993 * space and can't page. This prevents
1994 * a single device from hogging all
1995 * slots. We should really have a better
1996 * way of providing fairness.
1997 */
1998 xs->sc_link->opennings += 2;
1999 }
2000 }
2001 }
2002 #endif
2003 ahc_free_scb(ahc, scb, xs->flags);
2004 scsi_done(xs);
2005
2006 #if defined(__NetBSD__) /* XXX */
2007 /*
2008 * If there are entries in the software queue, try to
2009 * run the first one. We should be more or less guaranteed
2010 * to succeed, since we just freed an SCB.
2011 *
2012 * NOTE: ahc_scsi_cmd() relies on our calling it with
2013 * the first entry in the queue.
2014 */
2015 if (ahc->sc_xxxq.lh_first != NULL)
2016 (void) ahc_scsi_cmd(ahc->sc_xxxq.lh_first);
2017 #endif /* __NetBSD__ */
2018 }
2019
2020 /*
2021 * Start the board, ready for normal operation
2022 */
2023 int
2024 ahc_init(ahc)
2025 struct ahc_data *ahc;
2026 {
2027 u_int8_t scsi_conf, sblkctl, i;
2028 u_int16_t ultraenable = 0;
2029 int max_targ = 15;
2030 /*
2031 * Assume we have a board at this stage and it has been reset.
2032 */
2033
2034 /* Handle the SCBPAGING option */
2035 #ifndef AHC_SCBPAGING_ENABLE
2036 ahc->flags &= ~AHC_PAGESCBS;
2037 #endif
2038
2039 /* Determine channel configuration and who we are on the scsi bus. */
2040 switch ( (sblkctl = AHC_INB(ahc, SBLKCTL) & 0x0a) ) {
2041 case 0:
2042 ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
2043 ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
2044 if(ahc->type == AHC_394)
2045 printf("Channel %c, SCSI Id=%d, ",
2046 ahc->flags & AHC_CHNLB ? 'B' : 'A',
2047 ahc->our_id);
2048 else
2049 printf("Single Channel, SCSI Id=%d, ", ahc->our_id);
2050 AHC_OUTB(ahc, FLAGS, SINGLE_BUS | (ahc->flags & AHC_PAGESCBS));
2051 break;
2052 case 2:
2053 ahc->our_id = (AHC_INB(ahc, SCSICONF + 1) & HWSCSIID);
2054 ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
2055 if(ahc->type == AHC_394)
2056 printf("Wide Channel %c, SCSI Id=%d, ",
2057 ahc->flags & AHC_CHNLB ? 'B' : 'A',
2058 ahc->our_id);
2059 else
2060 printf("Wide Channel, SCSI Id=%d, ", ahc->our_id);
2061 ahc->type |= AHC_WIDE;
2062 AHC_OUTB(ahc, FLAGS, WIDE_BUS | (ahc->flags & AHC_PAGESCBS));
2063 break;
2064 case 8:
2065 ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
2066 ahc->our_id_b = (AHC_INB(ahc, SCSICONF + 1) & HSCSIID);
2067 printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, ",
2068 ahc->our_id, ahc->our_id_b);
2069 ahc->type |= AHC_TWIN;
2070 AHC_OUTB(ahc, FLAGS, TWIN_BUS | (ahc->flags & AHC_PAGESCBS));
2071 break;
2072 default:
2073 printf(" Unsupported adapter type. Ignoring\n");
2074 return(-1);
2075 }
2076
2077 /* Determine the number of SCBs */
2078
2079 {
2080 AHC_OUTB(ahc, SCBPTR, 0);
2081 AHC_OUTB(ahc, SCB_CONTROL, 0);
2082 for(i = 1; i < AHC_SCB_MAX; i++) {
2083 AHC_OUTB(ahc, SCBPTR, i);
2084 AHC_OUTB(ahc, SCB_CONTROL, i);
2085 if(AHC_INB(ahc, SCB_CONTROL) != i)
2086 break;
2087 AHC_OUTB(ahc, SCBPTR, 0);
2088 if(AHC_INB(ahc, SCB_CONTROL) != 0)
2089 break;
2090 /* Clear the control byte. */
2091 AHC_OUTB(ahc, SCBPTR, i);
2092 AHC_OUTB(ahc, SCB_CONTROL, 0);
2093
2094 ahc->qcntmask |= i; /* Update the count mask. */
2095 }
2096
2097 /* Ensure we clear the 0 SCB's control byte. */
2098 AHC_OUTB(ahc, SCBPTR, 0);
2099 AHC_OUTB(ahc, SCB_CONTROL, 0);
2100
2101 ahc->qcntmask |= i;
2102 ahc->maxhscbs = i;
2103 }
2104
2105 if((ahc->maxhscbs < AHC_SCB_MAX) && (ahc->flags & AHC_PAGESCBS))
2106 ahc->maxscbs = AHC_SCB_MAX;
2107 else {
2108 ahc->maxscbs = ahc->maxhscbs;
2109 ahc->flags &= ~AHC_PAGESCBS;
2110 }
2111
2112 printf("%d SCBs\n", ahc->maxhscbs);
2113
2114 #ifdef AHC_DEBUG
2115 if(ahc_debug & AHC_SHOWMISC) {
2116 struct scb test;
2117 printf("%s: hardware scb %ld bytes; kernel scb; "
2118 "ahc_dma %d bytes\n",
2119 ahc_name(ahc),
2120 (u_long)&(test.next) - (u_long)(&test),
2121 sizeof(test),
2122 sizeof(struct ahc_dma_seg));
2123 }
2124 #endif /* AHC_DEBUG */
2125
2126 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
2127 if(ahc->type & AHC_TWIN)
2128 {
2129 /*
2130 * The device is gated to channel B after a chip reset,
2131 * so set those values first
2132 */
2133 AHC_OUTB(ahc, SCSIID, ahc->our_id_b);
2134 scsi_conf = AHC_INB(ahc, SCSICONF + 1);
2135 AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
2136 | ENSTIMER|ACTNEGEN|STPWEN);
2137 AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
2138 if(ahc->type & AHC_ULTRA)
2139 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
2140 else
2141 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
2142
2143 if(scsi_conf & RESET_SCSI) {
2144 /* Reset the bus */
2145 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2146 if(bootverbose)
2147 printf("%s: Resetting Channel B\n",
2148 ahc_name(ahc));
2149 #endif
2150 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
2151 DELAY(1000);
2152 AHC_OUTB(ahc, SCSISEQ, 0);
2153
2154 /* Ensure we don't get a RSTI interrupt from this */
2155 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
2156 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
2157 }
2158
2159 /* Select Channel A */
2160 AHC_OUTB(ahc, SBLKCTL, 0);
2161 }
2162 AHC_OUTB(ahc, SCSIID, ahc->our_id);
2163 scsi_conf = AHC_INB(ahc, SCSICONF);
2164 AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
2165 | ENSTIMER|ACTNEGEN|STPWEN);
2166 AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
2167 if(ahc->type & AHC_ULTRA)
2168 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
2169 else
2170 AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
2171
2172 if(scsi_conf & RESET_SCSI) {
2173 /* Reset the bus */
2174 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2175 if(bootverbose)
2176 printf("%s: Resetting Channel A\n", ahc_name(ahc));
2177 #endif
2178
2179 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
2180 DELAY(1000);
2181 AHC_OUTB(ahc, SCSISEQ, 0);
2182
2183 /* Ensure we don't get a RSTI interrupt from this */
2184 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
2185 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
2186 }
2187
2188 /*
2189 * Look at the information that board initialization or
2190 * the board bios has left us. In the lower four bits of each
2191 * target's scratch space any value other than 0 indicates
2192 * that we should initiate synchronous transfers. If it's zero,
2193 * the user or the BIOS has decided to disable synchronous
2194 * negotiation to that target so we don't activate the needsdtr
2195 * flag.
2196 */
2197 ahc->needsdtr_orig = 0;
2198 ahc->needwdtr_orig = 0;
2199
2200 /* Grab the disconnection disable table and invert it for our needs */
2201 if(ahc->flags & AHC_USEDEFAULTS) {
2202 printf("%s: Host Adapter Bios disabled. Using default SCSI "
2203 "device parameters\n", ahc_name(ahc));
2204 ahc->discenable = 0xff;
2205 }
2206 else
2207 ahc->discenable = ~((AHC_INB(ahc, DISC_DSB + 1) << 8)
2208 | AHC_INB(ahc, DISC_DSB));
2209
2210 if(!(ahc->type & (AHC_WIDE|AHC_TWIN)))
2211 max_targ = 7;
2212
2213 for(i = 0; i <= max_targ; i++){
2214 u_char target_settings;
2215 if (ahc->flags & AHC_USEDEFAULTS) {
2216 target_settings = 0; /* 10MHz */
2217 ahc->needsdtr_orig |= (0x01 << i);
2218 ahc->needwdtr_orig |= (0x01 << i);
2219 }
2220 else {
2221 /* Take the settings leftover in scratch RAM. */
2222 target_settings = AHC_INB(ahc, TARG_SCRATCH + i);
2223
2224 if(target_settings & 0x0f){
2225 ahc->needsdtr_orig |= (0x01 << i);
2226 /*Default to asynchronous transfers(0 offset)*/
2227 target_settings &= 0xf0;
2228 }
2229 if(target_settings & 0x80){
2230 ahc->needwdtr_orig |= (0x01 << i);
2231 /*
2232 * We'll set the Wide flag when we
2233 * are successful with Wide negotiation.
2234 * Turn it off for now so we aren't
2235 * confused.
2236 */
2237 target_settings &= 0x7f;
2238 }
2239 if(ahc->type & AHC_ULTRA) {
2240 /*
2241 * Enable Ultra for any target that
2242 * has a valid ultra syncrate setting.
2243 */
2244 u_char rate = target_settings & 0x70;
2245 if(rate == 0x00 || rate == 0x10 ||
2246 rate == 0x20 || rate == 0x40) {
2247 if(rate == 0x40) {
2248 /* Treat 10MHz specially */
2249 target_settings &= ~0x70;
2250 }
2251 else
2252 ultraenable |= (0x01 << i);
2253 }
2254 }
2255 }
2256 AHC_OUTB(ahc, TARG_SCRATCH+i,target_settings);
2257 }
2258 /*
2259 * If we are not a WIDE device, forget WDTR. This
2260 * makes the driver work on some cards that don't
2261 * leave these fields cleared when the BIOS is not
2262 * installed.
2263 */
2264 if(!(ahc->type & AHC_WIDE))
2265 ahc->needwdtr_orig = 0;
2266 ahc->needsdtr = ahc->needsdtr_orig;
2267 ahc->needwdtr = ahc->needwdtr_orig;
2268 ahc->sdtrpending = 0;
2269 ahc->wdtrpending = 0;
2270 ahc->tagenable = 0;
2271 ahc->orderedtag = 0;
2272
2273 AHC_OUTB(ahc, ULTRA_ENB, ultraenable & 0xff);
2274 AHC_OUTB(ahc, ULTRA_ENB + 1, (ultraenable >> 8) & 0xff);
2275
2276 #ifdef AHC_DEBUG
2277 /* How did we do? */
2278 if(ahc_debug & AHC_SHOWMISC)
2279 printf("NEEDSDTR == 0x%x\nNEEDWDTR == 0x%x\n"
2280 "DISCENABLE == 0x%x\n", ahc->needsdtr,
2281 ahc->needwdtr, ahc->discenable);
2282 #endif
2283 /*
2284 * Set the number of available SCBs
2285 */
2286 AHC_OUTB(ahc, SCBCOUNT, ahc->maxhscbs);
2287
2288 /*
2289 * 2's compliment of maximum tag value
2290 */
2291 i = ahc->maxscbs;
2292 AHC_OUTB(ahc, COMP_SCBCOUNT, -i & 0xff);
2293
2294 /*
2295 * QCount mask to deal with broken aic7850s that
2296 * sporadically get garbage in the upper bits of
2297 * their QCount registers.
2298 */
2299 AHC_OUTB(ahc, QCNTMASK, ahc->qcntmask);
2300
2301 /* We don't have any busy targets right now */
2302 AHC_OUTB(ahc, ACTIVE_A, 0);
2303 AHC_OUTB(ahc, ACTIVE_B, 0);
2304
2305 /* We don't have any waiting selections */
2306 AHC_OUTB(ahc, WAITING_SCBH, SCB_LIST_NULL);
2307
2308 /* Our disconnection list is empty too */
2309 AHC_OUTB(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
2310
2311 /* Message out buffer starts empty */
2312 AHC_OUTB(ahc, MSG_LEN, 0x00);
2313
2314 /*
2315 * Load the Sequencer program and Enable the adapter
2316 * in "fast" mode.
2317 */
2318 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2319 if(bootverbose)
2320 printf("%s: Downloading Sequencer Program...",
2321 ahc_name(ahc));
2322 #endif
2323
2324 ahc_loadseq(ahc);
2325
2326 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
2327 if(bootverbose)
2328 printf("Done\n");
2329 #endif
2330
2331 AHC_OUTB(ahc, SEQCTL, FASTMODE);
2332
2333 unpause_sequencer(ahc, /*unpause_always*/TRUE);
2334
2335 /*
2336 * Note that we are going and return (to probe)
2337 */
2338 ahc->flags |= AHC_INIT;
2339 return (0);
2340 }
2341
2342 static void
2343 ahcminphys(bp)
2344 struct buf *bp;
2345 {
2346 /*
2347 * Even though the card can transfer up to 16megs per command
2348 * we are limited by the number of segments in the dma segment
2349 * list that we can hold. The worst case is that all pages are
2350 * discontinuous physically, hense the "page per segment" limit
2351 * enforced here.
2352 */
2353 if (bp->b_bcount > ((AHC_NSEG - 1) * PAGE_SIZE)) {
2354 bp->b_bcount = ((AHC_NSEG - 1) * PAGE_SIZE);
2355 }
2356 #if defined(__NetBSD__)
2357 minphys(bp);
2358 #endif
2359 }
2360
2361 #if defined(__NetBSD__) /* XXX */
2362 /*
2363 * Insert a scsi_xfer into the software queue. We overload xs->free_list
2364 * to to ensure we don't run into a queue resource shortage, and keep
2365 * a pointer to the last entry around to make insertion O(C).
2366 */
2367 static void
2368 ahc_xxx_enqueue(ahc, xs, infront)
2369 struct ahc_data *ahc;
2370 struct scsi_xfer *xs;
2371 int infront;
2372 {
2373
2374 if (infront || ahc->sc_xxxq.lh_first == NULL) {
2375 if (ahc->sc_xxxq.lh_first == NULL)
2376 ahc->sc_xxxqlast = xs;
2377 LIST_INSERT_HEAD(&ahc->sc_xxxq, xs, free_list);
2378 return;
2379 }
2380
2381 LIST_INSERT_AFTER(ahc->sc_xxxqlast, xs, free_list);
2382 ahc->sc_xxxqlast = xs;
2383 }
2384
2385 /*
2386 * Pull a scsi_xfer off the front of the software queue. When we
2387 * pull the last one off, we need to clear the pointer to the last
2388 * entry.
2389 */
2390 static struct scsi_xfer *
2391 ahc_xxx_dequeue(ahc)
2392 struct ahc_data *ahc;
2393 {
2394 struct scsi_xfer *xs;
2395
2396 xs = ahc->sc_xxxq.lh_first;
2397 LIST_REMOVE(xs, free_list);
2398
2399 if (ahc->sc_xxxq.lh_first == NULL)
2400 ahc->sc_xxxqlast = NULL;
2401
2402 return (xs);
2403 }
2404 #endif
2405
2406 /*
2407 * start a scsi operation given the command and
2408 * the data address, target, and lun all of which
2409 * are stored in the scsi_xfer struct
2410 */
2411 static int32_t
2412 ahc_scsi_cmd(xs)
2413 struct scsi_xfer *xs;
2414 {
2415 struct scb *scb;
2416 struct ahc_dma_seg *sg;
2417 int seg; /* scatter gather seg being worked on */
2418 unsigned long thiskv, nextkv;
2419 physaddr thisphys, nextphys;
2420 int bytes_this_seg, bytes_this_page, datalen, flags;
2421 struct ahc_data *ahc;
2422 u_short mask;
2423 int s;
2424 #if defined(__NetBSD__) /* XXX */
2425 int dontqueue = 0, fromqueue = 0;
2426 #endif
2427
2428 ahc = (struct ahc_data *)xs->sc_link->adapter_softc;
2429 mask = (0x01 << (xs->sc_link->target
2430 #if defined(__FreeBSD__)
2431 | ((u_long)xs->sc_link->fordriver & 0x08)));
2432 #elif defined(__NetBSD__)
2433 | (IS_SCSIBUS_B(ahc, xs->sc_link) ? SELBUSB : 0) ));
2434 #endif
2435
2436 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_scsi_cmd\n"));
2437
2438 #if defined(__NetBSD__) /* XXX */
2439 /* must protect the queue */
2440 s = splbio();
2441
2442 /*
2443 * If we're running the queue from ahc_done(), we're called
2444 * with the first entry in the queue as our argument.
2445 * Pull it off; if we can't run the job, it will get placed
2446 * back at the front.
2447 */
2448 if (xs == ahc->sc_xxxq.lh_first) {
2449 xs = ahc_xxx_dequeue(ahc);
2450 fromqueue = 1;
2451 goto get_scb;
2452 }
2453
2454 /* determine safety of software queueing */
2455 dontqueue = xs->flags & SCSI_POLL;
2456
2457 /*
2458 * Handle situations where there's already entries in the
2459 * queue.
2460 */
2461 if (ahc->sc_xxxq.lh_first != NULL) {
2462 /*
2463 * If we can't queue, we have to abort, since
2464 * we have to preserve order.
2465 */
2466 if (dontqueue) {
2467 splx(s);
2468 xs->error = XS_DRIVER_STUFFUP;
2469 return (TRY_AGAIN_LATER);
2470 }
2471
2472 /*
2473 * Swap with the first queue entry.
2474 */
2475 ahc_xxx_enqueue(ahc, xs, 0);
2476 xs = ahc_xxx_dequeue(ahc);
2477 fromqueue = 1;
2478 }
2479
2480 get_scb:
2481 #endif /* __NetBSD__ */
2482 /*
2483 * get an scb to use. If the transfer
2484 * is from a buf (possibly from interrupt time)
2485 * then we can't allow it to sleep
2486 */
2487 flags = xs->flags;
2488 if (flags & ITSDONE) {
2489 printf("%s: Already done?", ahc_name(ahc));
2490 xs->flags &= ~ITSDONE;
2491 }
2492 if (!(flags & INUSE)) {
2493 printf("%s: Not in use?", ahc_name(ahc));
2494 xs->flags |= INUSE;
2495 }
2496 if (!(scb = ahc_get_scb(ahc, flags))) {
2497 #if defined(__NetBSD__) /* XXX */
2498 /*
2499 * If we can't queue, we lose.
2500 */
2501 if (dontqueue) {
2502 splx(s);
2503 xs->error = XS_DRIVER_STUFFUP;
2504 return (TRY_AGAIN_LATER);
2505 }
2506
2507 /*
2508 * If we were pulled off the queue, put ourselves
2509 * back in the front, otherwise tack ourselves onto
2510 * the end.
2511 */
2512 ahc_xxx_enqueue(ahc, xs, fromqueue);
2513
2514 splx(s);
2515 return (SUCCESSFULLY_QUEUED);
2516 #else
2517 xs->error = XS_DRIVER_STUFFUP;
2518 return (TRY_AGAIN_LATER);
2519 #endif /* __NetBSD__ */
2520 }
2521
2522 #if defined(__NetBSD__)
2523 /* we're done playing with the queue */
2524 splx(s);
2525 #endif
2526
2527 SC_DEBUG(xs->sc_link, SDEV_DB3, ("start scb(%p)\n", scb));
2528 scb->xs = xs;
2529 if (flags & SCSI_RESET) {
2530 scb->flags |= SCB_DEVICE_RESET|SCB_IMMED;
2531 scb->control |= MK_MESSAGE;
2532 }
2533 /*
2534 * Put all the arguments for the xfer in the scb
2535 */
2536
2537 if(ahc->tagenable & mask) {
2538 scb->control |= TAG_ENB;
2539 if(ahc->orderedtag & mask) {
2540 printf("Ordered Tag sent\n");
2541 scb->control |= 0x02;
2542 ahc->orderedtag &= ~mask;
2543 }
2544 }
2545 if(ahc->discenable & mask)
2546 scb->control |= DISCENB;
2547 if((ahc->needwdtr & mask) && !(ahc->wdtrpending & mask))
2548 {
2549 scb->control |= MK_MESSAGE;
2550 scb->flags |= SCB_MSGOUT_WDTR;
2551 ahc->wdtrpending |= mask;
2552 }
2553 else if((ahc->needsdtr & mask) && !(ahc->sdtrpending & mask))
2554 {
2555 scb->control |= MK_MESSAGE;
2556 scb->flags |= SCB_MSGOUT_SDTR;
2557 ahc->sdtrpending |= mask;
2558 }
2559 scb->tcl = ((xs->sc_link->target << 4) & 0xF0) |
2560 #if defined(__FreeBSD__)
2561 ((u_long)xs->sc_link->fordriver & 0x08) |
2562 #elif defined(__NetBSD__)
2563 (IS_SCSIBUS_B(ahc,xs->sc_link)? SELBUSB : 0)|
2564 #endif
2565 (xs->sc_link->lun & 0x07);
2566 scb->cmdlen = xs->cmdlen;
2567 scb->cmdpointer = KVTOPHYS(xs->cmd);
2568 xs->resid = 0;
2569 xs->status = 0;
2570 if (xs->datalen) { /* should use S/G only if not zero length */
2571 scb->SG_list_pointer = KVTOPHYS(scb->ahc_dma);
2572 sg = scb->ahc_dma;
2573 seg = 0;
2574 /*
2575 * Set up the scatter gather block
2576 */
2577 SC_DEBUG(xs->sc_link, SDEV_DB4,
2578 ("%ld @%p:- ", xs->datalen, xs->data));
2579 datalen = xs->datalen;
2580 thiskv = (unsigned long) xs->data;
2581 thisphys = KVTOPHYS(thiskv);
2582
2583 while ((datalen) && (seg < AHC_NSEG)) {
2584 bytes_this_seg = 0;
2585
2586 /* put in the base address */
2587 sg->addr = thisphys;
2588
2589 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("0x%lx", thisphys));
2590
2591 /* do it at least once */
2592 nextphys = thisphys;
2593 while ((datalen) && (thisphys == nextphys)) {
2594 /*
2595 * This page is contiguous (physically)
2596 * with the the last, just extend the
2597 * length
2598 */
2599 /* how far to the end of the page */
2600 nextphys = (thisphys & (~(PAGE_SIZE- 1)))
2601 + PAGE_SIZE;
2602 bytes_this_page = nextphys - thisphys;
2603 /**** or the data ****/
2604 bytes_this_page = min(bytes_this_page, datalen);
2605 bytes_this_seg += bytes_this_page;
2606 datalen -= bytes_this_page;
2607
2608 /* get more ready for the next page */
2609 nextkv = thiskv;
2610 nextkv &= ~((unsigned long) PAGE_SIZE - 1);
2611 nextkv += PAGE_SIZE;
2612 if (datalen)
2613 thisphys = KVTOPHYS(nextkv);
2614 thiskv = nextkv;
2615 }
2616 /*
2617 * next page isn't contiguous, finish the seg
2618 */
2619 SC_DEBUGN(xs->sc_link, SDEV_DB4,
2620 ("(0x%x)", bytes_this_seg));
2621 sg->len = bytes_this_seg;
2622 sg++;
2623 seg++;
2624 }
2625 scb->SG_segment_count = seg;
2626
2627 /* Copy the first SG into the data pointer area */
2628 scb->data = scb->ahc_dma->addr;
2629 scb->datalen = scb->ahc_dma->len;
2630 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("\n"));
2631 if (datalen) {
2632 /* there's still data, must have run out of segs! */
2633 printf("%s: ahc_scsi_cmd: more than %d DMA segs\n",
2634 ahc_name(ahc), AHC_NSEG);
2635 xs->error = XS_DRIVER_STUFFUP;
2636 ahc_free_scb(ahc, scb, flags);
2637 return (COMPLETE);
2638 }
2639 #ifdef AHC_BROKEN_CACHE
2640 if (ahc_broken_cache)
2641 INVALIDATE_CACHE();
2642 #endif
2643 }
2644 else {
2645 /*
2646 * No data xfer, use non S/G values
2647 */
2648 scb->SG_segment_count = 0;
2649 scb->SG_list_pointer = 0;
2650 scb->data = 0;
2651 scb->datalen = 0;
2652 }
2653
2654 #ifdef AHC_DEBUG
2655 if((ahc_debug & AHC_SHOWSCBS) && (xs->sc_link->target == DEBUGTARG))
2656 ahc_print_scb(scb);
2657 #endif
2658 s = splbio();
2659
2660 if( scb->position != SCB_LIST_NULL )
2661 {
2662 /* We already have a valid slot */
2663 u_char curscb;
2664
2665 pause_sequencer(ahc);
2666 curscb = AHC_INB(ahc, SCBPTR);
2667 AHC_OUTB(ahc, SCBPTR, scb->position);
2668 ahc_send_scb(ahc, scb);
2669 AHC_OUTB(ahc, SCBPTR, curscb);
2670 AHC_OUTB(ahc, QINFIFO, scb->position);
2671 unpause_sequencer(ahc, /*unpause_always*/FALSE);
2672 scb->flags |= SCB_ACTIVE;
2673 if (!(flags & SCSI_NOMASK)) {
2674 timeout(ahc_timeout, (caddr_t)scb,
2675 (xs->timeout * hz) / 1000);
2676 }
2677 SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
2678 }
2679 else {
2680 scb->flags |= SCB_WAITINGQ;
2681 STAILQ_INSERT_TAIL(&ahc->waiting_scbs, scb, links);
2682 ahc_run_waiting_queues(ahc);
2683 }
2684 if (!(flags & SCSI_NOMASK)) {
2685 splx(s);
2686 return (SUCCESSFULLY_QUEUED);
2687 }
2688 /*
2689 * If we can't use interrupts, poll for completion
2690 */
2691 SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
2692 do {
2693 if (ahc_poll(ahc, xs->timeout)) {
2694 if (!(xs->flags & SCSI_SILENT))
2695 printf("cmd fail\n");
2696 ahc_timeout(scb);
2697 break;
2698 }
2699 } while (!(xs->flags & ITSDONE)); /* a non command complete intr */
2700 splx(s);
2701 return (COMPLETE);
2702 }
2703
2704
2705 /*
2706 * A scb (and hence an scb entry on the board) is put onto the
2707 * free list.
2708 */
2709 static void
2710 ahc_free_scb(ahc, scb, flags)
2711 struct ahc_data *ahc;
2712 int flags;
2713 struct scb *scb;
2714 {
2715 struct scb *wscb;
2716 unsigned int opri;
2717
2718 opri = splbio();
2719
2720 /* Clean up for the next user */
2721 scb->flags = SCB_FREE;
2722 scb->control = 0;
2723 scb->status = 0;
2724
2725 if(scb->position == SCB_LIST_NULL) {
2726 STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
2727 if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
2728 /*
2729 * If there were no SCBs available, wake anybody waiting
2730 * for one to come free.
2731 */
2732 wakeup((caddr_t)&ahc->free_scbs);
2733 }
2734 /*
2735 * If there are any SCBS on the waiting queue,
2736 * assign the slot of this "freed" SCB to the first
2737 * one. We'll run the waiting queues after all command
2738 * completes for a particular interrupt are completed
2739 * or when we start another command.
2740 */
2741 else if((wscb = ahc->waiting_scbs.stqh_first) != NULL) {
2742 STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
2743 wscb->position = scb->position;
2744 STAILQ_INSERT_TAIL(&ahc->assigned_scbs, wscb, links);
2745 wscb->flags ^= SCB_WAITINGQ|SCB_ASSIGNEDQ;
2746
2747 /*
2748 * The "freed" SCB will need to be assigned a slot
2749 * before being used, so put it in the page_scbs
2750 * queue.
2751 */
2752 scb->position = SCB_LIST_NULL;
2753 STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
2754 if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
2755 /*
2756 * If there were no SCBs available, wake anybody waiting
2757 * for one to come free.
2758 */
2759 wakeup((caddr_t)&ahc->free_scbs);
2760 }
2761 else {
2762 STAILQ_INSERT_HEAD(&ahc->free_scbs, scb, links);
2763 if(!scb->links.stqe_next && !ahc->page_scbs.stqh_first)
2764 /*
2765 * If there were no SCBs available, wake anybody waiting
2766 * for one to come free.
2767 */
2768 wakeup((caddr_t)&ahc->free_scbs);
2769 }
2770 #ifdef AHC_DEBUG
2771 ahc->activescbs--;
2772 #endif
2773 splx(opri);
2774 }
2775
2776 /*
2777 * Get a free scb, either one already assigned to a hardware slot
2778 * on the adapter or one that will require an SCB to be paged out before
2779 * use. If there are none, see if we can allocate a new SCB. Otherwise
2780 * either return an error or sleep.
2781 */
2782 static struct scb *
2783 ahc_get_scb(ahc, flags)
2784 struct ahc_data *ahc;
2785 int flags;
2786 {
2787 unsigned opri;
2788 struct scb *scbp;
2789
2790 opri = splbio();
2791 /*
2792 * If we can and have to, sleep waiting for one to come free
2793 * but only if we can't allocate a new one.
2794 */
2795 while (1) {
2796 if((scbp = ahc->free_scbs.stqh_first)) {
2797 STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
2798 }
2799 else if((scbp = ahc->page_scbs.stqh_first)) {
2800 STAILQ_REMOVE_HEAD(&ahc->page_scbs, links);
2801 }
2802 else if(ahc->numscbs < ahc->maxscbs) {
2803 scbp = (struct scb *) malloc(sizeof(struct scb),
2804 M_TEMP, M_NOWAIT);
2805 if (scbp) {
2806 bzero(scbp, sizeof(struct scb));
2807 scbp->tag = ahc->numscbs;
2808 if( ahc->numscbs < ahc->maxhscbs )
2809 scbp->position = ahc->numscbs;
2810 else
2811 scbp->position = SCB_LIST_NULL;
2812 ahc->numscbs++;
2813 /*
2814 * Place in the scbarray
2815 * Never is removed.
2816 */
2817 ahc->scbarray[scbp->tag] = scbp;
2818 }
2819 else {
2820 printf("%s: Can't malloc SCB\n",
2821 ahc_name(ahc));
2822 }
2823 }
2824 else {
2825 if (!(flags & SCSI_NOSLEEP)) {
2826 tsleep((caddr_t)&ahc->free_scbs, PRIBIO,
2827 "ahcscb", 0);
2828 continue;
2829 }
2830 }
2831 break;
2832 }
2833
2834 #ifdef AHC_DEBUG
2835 if (scbp) {
2836 ahc->activescbs++;
2837 if((ahc_debug & AHC_SHOWSCBCNT)
2838 && (ahc->activescbs == ahc->maxhscbs))
2839 printf("%s: Max SCBs active\n", ahc_name(ahc));
2840 }
2841 #endif
2842
2843 splx(opri);
2844
2845 return (scbp);
2846 }
2847
2848 static void ahc_loadseq(ahc)
2849 struct ahc_data *ahc;
2850 {
2851 static u_char seqprog[] = {
2852 #if defined(__FreeBSD__)
2853 # include "aic7xxx_seq.h"
2854 #endif
2855 #if defined(__NetBSD__)
2856 # include <dev/microcode/aic7xxx/aic7xxx_seq.h>
2857 #endif
2858 };
2859
2860 AHC_OUTB(ahc, SEQCTL, PERRORDIS|SEQRESET|LOADRAM);
2861
2862 AHC_OUTSB(ahc, SEQRAM, seqprog, sizeof(seqprog));
2863
2864 do {
2865 AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
2866 } while((AHC_INB(ahc, SEQADDR0) != 0)
2867 || (AHC_INB(ahc, SEQADDR1) != 0));
2868 }
2869
2870 /*
2871 * Function to poll for command completion when
2872 * interrupts are disabled (crash dumps)
2873 */
2874 static int
2875 ahc_poll(ahc, wait)
2876 struct ahc_data *ahc;
2877 int wait; /* in msec */
2878 {
2879 while (--wait) {
2880 DELAY(1000);
2881 if (AHC_INB(ahc, INTSTAT) & INT_PEND)
2882 break;
2883 } if (wait == 0) {
2884 printf("%s: board is not responding\n", ahc_name(ahc));
2885 return (EIO);
2886 }
2887 ahc_intr((void *)ahc);
2888 return (0);
2889 }
2890
2891 static void
2892 ahc_timeout(arg)
2893 void *arg;
2894 {
2895 struct scb *scb = (struct scb *)arg;
2896 struct ahc_data *ahc;
2897 int s, found;
2898 u_char bus_state;
2899 char channel;
2900
2901 s = splbio();
2902
2903 if (!(scb->flags & SCB_ACTIVE)) {
2904 /* Previous timeout took care of me already */
2905 splx(s);
2906 return;
2907 }
2908
2909 ahc = (struct ahc_data *)scb->xs->sc_link->adapter_softc;
2910
2911 if (ahc->in_timeout) {
2912 /*
2913 * Some other SCB has started a recovery operation
2914 * and is still working on cleaning things up.
2915 */
2916 if (scb->flags & SCB_TIMEDOUT) {
2917 /*
2918 * This SCB has been here before and is not the
2919 * recovery SCB. Cut our losses and panic. Its
2920 * better to do this than trash a filesystem.
2921 */
2922 panic("%s: Timed-out command times out "
2923 "again\n", ahc_name(ahc));
2924 }
2925 else if (!(scb->flags & SCB_ABORTED))
2926 {
2927 /*
2928 * This is not the SCB that started this timeout
2929 * processing. Give this scb another lifetime so
2930 * that it can continue once we deal with the
2931 * timeout.
2932 */
2933 scb->flags |= SCB_TIMEDOUT;
2934 timeout(ahc_timeout, (caddr_t)scb,
2935 (scb->xs->timeout * hz) / 1000);
2936 splx(s);
2937 return;
2938 }
2939 }
2940 ahc->in_timeout = TRUE;
2941
2942 /*
2943 * Ensure that the card doesn't do anything
2944 * behind our back.
2945 */
2946 pause_sequencer(ahc);
2947
2948 sc_print_addr(scb->xs->sc_link);
2949 printf("timed out ");
2950 /*
2951 * Take a snapshot of the bus state and print out
2952 * some information so we can track down driver bugs.
2953 */
2954 bus_state = AHC_INB(ahc, LASTPHASE);
2955
2956 switch(bus_state & PHASE_MASK)
2957 {
2958 case P_DATAOUT:
2959 printf("in dataout phase");
2960 break;
2961 case P_DATAIN:
2962 printf("in datain phase");
2963 break;
2964 case P_COMMAND:
2965 printf("in command phase");
2966 break;
2967 case P_MESGOUT:
2968 printf("in message out phase");
2969 break;
2970 case P_STATUS:
2971 printf("in status phase");
2972 break;
2973 case P_MESGIN:
2974 printf("in message in phase");
2975 break;
2976 default:
2977 printf("while idle, LASTPHASE == 0x%x",
2978 bus_state);
2979 /*
2980 * We aren't in a valid phase, so assume we're
2981 * idle.
2982 */
2983 bus_state = 0;
2984 break;
2985 }
2986
2987 printf(", SCSISIGI == 0x%x\n", AHC_INB(ahc, SCSISIGI));
2988
2989 /* Decide our course of action */
2990
2991 if(scb->flags & SCB_ABORTED)
2992 {
2993 /*
2994 * Been down this road before.
2995 * Do a full bus reset.
2996 */
2997 char channel = (scb->tcl & SELBUSB)
2998 ? 'B': 'A';
2999 found = ahc_reset_channel(ahc, channel, scb->tag,
3000 XS_TIMEOUT, /*Initiate Reset*/TRUE);
3001 printf("%s: Issued Channel %c Bus Reset #1. "
3002 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
3003 ahc->in_timeout = FALSE;
3004 }
3005 else if(scb->control & TAG_ENB) {
3006 /*
3007 * We could be starving this command
3008 * try sending an ordered tag command
3009 * to the target we come from.
3010 */
3011 scb->flags |= SCB_ABORTED|SCB_SENTORDEREDTAG;
3012 ahc->orderedtag |= 0xFF;
3013 timeout(ahc_timeout, (caddr_t)scb, (5 * hz));
3014 unpause_sequencer(ahc, /*unpause_always*/FALSE);
3015 printf("Ordered Tag queued\n");
3016 goto done;
3017 }
3018 else {
3019 /*
3020 * Send a Bus Device Reset Message:
3021 * The target that is holding up the bus may not
3022 * be the same as the one that triggered this timeout
3023 * (different commands have different timeout lengths).
3024 * It is also impossible to get a message to a target
3025 * if we are in a "frozen" data transfer phase. Our
3026 * strategy here is to queue a bus device reset message
3027 * to the timed out target if it is disconnected.
3028 * Otherwise, if we have an active target we stuff the
3029 * message buffer with a bus device reset message and
3030 * assert ATN in the hopes that the target will let go
3031 * of the bus and finally disconnect. If this fails,
3032 * we'll get another timeout 2 seconds later which will
3033 * cause a bus reset.
3034 *
3035 * XXX If the SCB is paged out, we simply reset the
3036 * bus. We should probably queue a new command
3037 * instead.
3038 */
3039
3040 /* Test to see if scb is disconnected */
3041 if( !(scb->flags & SCB_PAGED_OUT ) ){
3042 u_char active_scb;
3043 struct scb *active_scbp;
3044
3045 active_scb = AHC_INB(ahc, SCBPTR);
3046 active_scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
3047 AHC_OUTB(ahc, SCBPTR, scb->position);
3048
3049 if(AHC_INB(ahc, SCB_CONTROL) & DISCONNECTED) {
3050 if(ahc->flags & AHC_PAGESCBS) {
3051 /*
3052 * Pull this SCB out of the
3053 * disconnected list.
3054 */
3055 u_char prev = AHC_INB(ahc, SCB_PREV);
3056 u_char next = AHC_INB(ahc, SCB_NEXT);
3057 if(prev == SCB_LIST_NULL) {
3058 /* At the head */
3059 AHC_OUTB(ahc, DISCONNECTED_SCBH,
3060 next );
3061 }
3062 else {
3063 AHC_OUTB(ahc, SCBPTR, prev);
3064 AHC_OUTB(ahc, SCB_NEXT, next);
3065 if(next != SCB_LIST_NULL) {
3066 AHC_OUTB(ahc, SCBPTR,
3067 next);
3068 AHC_OUTB(ahc, SCB_PREV,
3069 prev);
3070 }
3071 AHC_OUTB(ahc, SCBPTR,
3072 scb->position);
3073 }
3074 }
3075 scb->flags |= SCB_DEVICE_RESET|SCB_ABORTED;
3076 scb->control &= DISCENB;
3077 scb->control |= MK_MESSAGE;
3078 scb->cmdlen = 0;
3079 scb->SG_segment_count = 0;
3080 scb->SG_list_pointer = 0;
3081 scb->data = 0;
3082 scb->datalen = 0;
3083 ahc_send_scb(ahc, scb);
3084 ahc_add_waiting_scb(ahc, scb);
3085 timeout(ahc_timeout, (caddr_t)scb, (2 * hz));
3086 sc_print_addr(scb->xs->sc_link);
3087 printf("BUS DEVICE RESET message queued.\n");
3088 AHC_OUTB(ahc, SCBPTR, active_scb);
3089 unpause_sequencer(ahc, /*unpause_always*/FALSE);
3090 goto done;
3091 }
3092 /* Is the active SCB really active? */
3093 else if((active_scbp->flags & SCB_ACTIVE) && bus_state){
3094 AHC_OUTB(ahc, MSG_LEN, 1);
3095 AHC_OUTB(ahc, MSG0, MSG_BUS_DEV_RESET);
3096 AHC_OUTB(ahc, SCSISIGO, bus_state|ATNO);
3097 sc_print_addr(active_scbp->xs->sc_link);
3098 printf("asserted ATN - device reset in "
3099 "message buffer\n");
3100 active_scbp->flags |= SCB_DEVICE_RESET
3101 | SCB_ABORTED;
3102 if(active_scbp != scb) {
3103 untimeout(ahc_timeout,
3104 (caddr_t)active_scbp);
3105 /* Give scb a new lease on life */
3106 timeout(ahc_timeout, (caddr_t)scb,
3107 (scb->xs->timeout * hz) / 1000);
3108 }
3109 timeout(ahc_timeout, (caddr_t)active_scbp,
3110 (2 * hz));
3111 AHC_OUTB(ahc, SCBPTR, active_scb);
3112 unpause_sequencer(ahc, /*unpause_always*/FALSE);
3113 goto done;
3114 }
3115 }
3116 /*
3117 * No active target or a paged out SCB.
3118 * Try resetting the bus.
3119 */
3120 channel = (scb->tcl & SELBUSB) ? 'B': 'A';
3121 found = ahc_reset_channel(ahc, channel, scb->tag,
3122 XS_TIMEOUT,
3123 /*Initiate Reset*/TRUE);
3124 printf("%s: Issued Channel %c Bus Reset #2. "
3125 "%d SCBs aborted\n", ahc_name(ahc), channel,
3126 found);
3127 ahc->in_timeout = FALSE;
3128 }
3129 done:
3130 splx(s);
3131 }
3132
3133
3134 /*
3135 * The device at the given target/channel has been reset. Abort
3136 * all active and queued scbs for that target/channel.
3137 */
3138 static int
3139 ahc_reset_device(ahc, target, channel, timedout_scb, xs_error)
3140 struct ahc_data *ahc;
3141 int target;
3142 char channel;
3143 u_char timedout_scb;
3144 u_int32_t xs_error;
3145 {
3146 struct scb *scbp;
3147 u_char active_scb;
3148 int i = 0;
3149 int found = 0;
3150
3151 /* restore this when we're done */
3152 active_scb = AHC_INB(ahc, SCBPTR);
3153
3154 /*
3155 * Search the QINFIFO.
3156 */
3157 {
3158 u_char saved_queue[AHC_SCB_MAX];
3159 u_char queued = AHC_INB(ahc, QINCNT) & ahc->qcntmask;
3160
3161 for (i = 0; i < (queued - found); i++) {
3162 saved_queue[i] = AHC_INB(ahc, QINFIFO);
3163 AHC_OUTB(ahc, SCBPTR, saved_queue[i]);
3164 scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
3165 if (ahc_match_scb (scbp, target, channel)){
3166 /*
3167 * We found an scb that needs to be aborted.
3168 */
3169 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3170 scbp->xs->error |= xs_error;
3171 if(scbp->position != timedout_scb)
3172 untimeout(ahc_timeout, (caddr_t)scbp);
3173 AHC_OUTB(ahc, SCB_CONTROL, 0);
3174 i--;
3175 found++;
3176 }
3177 }
3178 /* Now put the saved scbs back. */
3179 for (queued = 0; queued < i; queued++) {
3180 AHC_OUTB(ahc, QINFIFO, saved_queue[queued]);
3181 }
3182 }
3183
3184 /*
3185 * Search waiting for selection list.
3186 */
3187 {
3188 u_char next, prev;
3189
3190 next = AHC_INB(ahc, WAITING_SCBH); /* Start at head of list. */
3191 prev = SCB_LIST_NULL;
3192
3193 while (next != SCB_LIST_NULL) {
3194 AHC_OUTB(ahc, SCBPTR, next);
3195 scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
3196 /*
3197 * Select the SCB.
3198 */
3199 if (ahc_match_scb(scbp, target, channel)) {
3200 next = ahc_abort_wscb(ahc, scbp, prev,
3201 timedout_scb, xs_error);
3202 found++;
3203 }
3204 else {
3205 prev = next;
3206 next = AHC_INB(ahc, SCB_NEXT);
3207 }
3208 }
3209 }
3210 /*
3211 * Go through the entire SCB array now and look for
3212 * commands for this target that are active. These
3213 * are other (most likely tagged) commands that
3214 * were disconnected when the reset occured.
3215 */
3216 for(i = 0; i < ahc->numscbs; i++) {
3217 scbp = ahc->scbarray[i];
3218 if((scbp->flags & SCB_ACTIVE)
3219 && ahc_match_scb(scbp, target, channel)) {
3220 /* Ensure the target is "free" */
3221 ahc_unbusy_target(ahc, target, channel);
3222 if( !(scbp->flags & SCB_PAGED_OUT) )
3223 {
3224 AHC_OUTB(ahc, SCBPTR, scbp->position);
3225 AHC_OUTB(ahc, SCB_CONTROL, 0);
3226 }
3227 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3228 scbp->xs->error |= xs_error;
3229 if(scbp->tag != timedout_scb)
3230 untimeout(ahc_timeout, (caddr_t)scbp);
3231 found++;
3232 }
3233 }
3234 AHC_OUTB(ahc, SCBPTR, active_scb);
3235 return found;
3236 }
3237
3238 /*
3239 * Manipulate the waiting for selection list and return the
3240 * scb that follows the one that we remove.
3241 */
3242 static u_char
3243 ahc_abort_wscb (ahc, scbp, prev, timedout_scb, xs_error)
3244 struct ahc_data *ahc;
3245 struct scb *scbp;
3246 u_char prev;
3247 u_char timedout_scb;
3248 u_int32_t xs_error;
3249 {
3250 u_char curscbp, next;
3251 int target = ((scbp->tcl >> 4) & 0x0f);
3252 char channel = (scbp->tcl & SELBUSB) ? 'B' : 'A';
3253 /*
3254 * Select the SCB we want to abort and
3255 * pull the next pointer out of it.
3256 */
3257 curscbp = AHC_INB(ahc, SCBPTR);
3258 AHC_OUTB(ahc, SCBPTR, scbp->position);
3259 next = AHC_INB(ahc, SCB_NEXT);
3260
3261 /* Clear the necessary fields */
3262 AHC_OUTB(ahc, SCB_CONTROL, 0);
3263 AHC_OUTB(ahc, SCB_NEXT, SCB_LIST_NULL);
3264 ahc_unbusy_target(ahc, target, channel);
3265
3266 /* update the waiting list */
3267 if( prev == SCB_LIST_NULL )
3268 /* First in the list */
3269 AHC_OUTB(ahc, WAITING_SCBH, next);
3270 else {
3271 /*
3272 * Select the scb that pointed to us
3273 * and update its next pointer.
3274 */
3275 AHC_OUTB(ahc, SCBPTR, prev);
3276 AHC_OUTB(ahc, SCB_NEXT, next);
3277 }
3278 /*
3279 * Point us back at the original scb position
3280 * and inform the SCSI system that the command
3281 * has been aborted.
3282 */
3283 AHC_OUTB(ahc, SCBPTR, curscbp);
3284 scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
3285 scbp->xs->error |= xs_error;
3286 if(scbp->tag != timedout_scb)
3287 untimeout(ahc_timeout, (caddr_t)scbp);
3288 return next;
3289 }
3290
3291 static void
3292 ahc_busy_target(ahc, target, channel)
3293 struct ahc_data *ahc;
3294 u_char target;
3295 char channel;
3296 {
3297 u_char active;
3298 u_long active_port = ACTIVE_A;
3299
3300 if(target > 0x07 || channel == 'B') {
3301 /*
3302 * targets on the Second channel or
3303 * above id 7 store info in byte two
3304 * of HA_ACTIVE
3305 */
3306 active_port++;
3307 }
3308 active = AHC_INB(ahc, active_port);
3309 active |= (0x01 << (target & 0x07));
3310 AHC_OUTB(ahc, active_port, active);
3311 }
3312
3313 static void
3314 ahc_unbusy_target(ahc, target, channel)
3315 struct ahc_data *ahc;
3316 u_char target;
3317 char channel;
3318 {
3319 u_char active;
3320 u_long active_port = ACTIVE_A;
3321
3322 if(target > 0x07 || channel == 'B') {
3323 /*
3324 * targets on the Second channel or
3325 * above id 7 store info in byte two
3326 * of HA_ACTIVE
3327 */
3328 active_port++;
3329 }
3330 active = AHC_INB(ahc, active_port);
3331 active &= ~(0x01 << (target & 0x07));
3332 AHC_OUTB(ahc, active_port, active);
3333 }
3334
3335 static void
3336 ahc_reset_current_bus(ahc)
3337 struct ahc_data *ahc;
3338 {
3339 AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
3340 DELAY(1000);
3341 AHC_OUTB(ahc, SCSISEQ, 0);
3342 }
3343
3344 static int
3345 ahc_reset_channel(ahc, channel, timedout_scb, xs_error, initiate_reset)
3346 struct ahc_data *ahc;
3347 char channel;
3348 u_char timedout_scb;
3349 u_int32_t xs_error;
3350 u_char initiate_reset;
3351 {
3352 u_char sblkctl;
3353 char cur_channel;
3354 u_long offset, offset_max;
3355 int found;
3356
3357 /*
3358 * Clean up all the state information for the
3359 * pending transactions on this bus.
3360 */
3361 found = ahc_reset_device(ahc, ALL_TARGETS, channel,
3362 timedout_scb, xs_error);
3363 if(channel == 'B'){
3364 ahc->needsdtr |= (ahc->needsdtr_orig & 0xff00);
3365 ahc->sdtrpending &= 0x00ff;
3366 AHC_OUTB(ahc, ACTIVE_B, 0);
3367 offset = TARG_SCRATCH + 8;
3368 offset_max = TARG_SCRATCH + 16;
3369 }
3370 else if (ahc->type & AHC_WIDE){
3371 ahc->needsdtr = ahc->needsdtr_orig;
3372 ahc->needwdtr = ahc->needwdtr_orig;
3373 ahc->sdtrpending = 0;
3374 ahc->wdtrpending = 0;
3375 AHC_OUTB(ahc, ACTIVE_A, 0);
3376 AHC_OUTB(ahc, ACTIVE_B, 0);
3377 offset = TARG_SCRATCH;
3378 offset_max = TARG_SCRATCH + 16;
3379 }
3380 else{
3381 ahc->needsdtr |= (ahc->needsdtr_orig & 0x00ff);
3382 ahc->sdtrpending &= 0xff00;
3383 AHC_OUTB(ahc, ACTIVE_A, 0);
3384 offset = TARG_SCRATCH;
3385 offset_max = TARG_SCRATCH + 8;
3386 }
3387 for(;offset < offset_max;offset++) {
3388 /*
3389 * Revert to async/narrow transfers
3390 * until we renegotiate.
3391 */
3392 u_char targ_scratch;
3393
3394 targ_scratch = AHC_INB(ahc, offset);
3395 targ_scratch &= SXFR;
3396 AHC_OUTB(ahc, offset, targ_scratch);
3397 }
3398
3399 /*
3400 * Reset the bus if we are initiating this reset and
3401 * restart/unpause the sequencer
3402 */
3403 /* Case 1: Command for another bus is active */
3404 sblkctl = AHC_INB(ahc, SBLKCTL);
3405 cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
3406 if(cur_channel != channel)
3407 {
3408 /*
3409 * Stealthily reset the other bus
3410 * without upsetting the current bus
3411 */
3412 AHC_OUTB(ahc, SBLKCTL, sblkctl ^ SELBUSB);
3413 if( initiate_reset )
3414 {
3415 ahc_reset_current_bus(ahc);
3416 }
3417 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
3418 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
3419 AHC_OUTB(ahc, SBLKCTL, sblkctl);
3420 unpause_sequencer(ahc, /*unpause_always*/TRUE);
3421 }
3422 /* Case 2: A command from this bus is active or we're idle */
3423 else {
3424 if( initiate_reset )
3425 {
3426 ahc_reset_current_bus(ahc);
3427 }
3428 AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
3429 AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
3430 restart_sequencer(ahc);
3431 }
3432 ahc_run_done_queue(ahc);
3433 return found;
3434 }
3435
3436 void
3437 ahc_run_done_queue(ahc)
3438 struct ahc_data *ahc;
3439 {
3440 int i;
3441 struct scb *scbp;
3442
3443 for(i = 0; i < ahc->numscbs; i++) {
3444 scbp = ahc->scbarray[i];
3445 if(scbp->flags & SCB_QUEUED_FOR_DONE)
3446 ahc_done(ahc, scbp);
3447 }
3448 }
3449
3450 static int
3451 ahc_match_scb (scb, target, channel)
3452 struct scb *scb;
3453 int target;
3454 char channel;
3455 {
3456 int targ = (scb->tcl >> 4) & 0x0f;
3457 char chan = (scb->tcl & SELBUSB) ? 'B' : 'A';
3458
3459 if (target == ALL_TARGETS)
3460 return (chan == channel);
3461 else
3462 return ((chan == channel) && (targ == target));
3463 }
3464
3465
3466 static void
3467 ahc_construct_sdtr(ahc, start_byte, period, offset)
3468 struct ahc_data *ahc;
3469 int start_byte;
3470 u_int8_t period;
3471 u_int8_t offset;
3472 {
3473 AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
3474 AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_SDTR_LEN);
3475 AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_SDTR);
3476 AHC_OUTB(ahc, MSG3 + start_byte, period);
3477 AHC_OUTB(ahc, MSG4 + start_byte, offset);
3478 AHC_OUTB(ahc, MSG_LEN, start_byte + 5);
3479 }
3480
3481 static void
3482 ahc_construct_wdtr(ahc, start_byte, bus_width)
3483 struct ahc_data *ahc;
3484 int start_byte;
3485 u_int8_t bus_width;
3486 {
3487 AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
3488 AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_WDTR_LEN);
3489 AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_WDTR);
3490 AHC_OUTB(ahc, MSG3 + start_byte, bus_width);
3491 AHC_OUTB(ahc, MSG_LEN, start_byte + 4);
3492 }
3493