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