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