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