Merge from vendor branch CVS:
[dragonfly.git] / sys / dev / disk / ahb / ahb.c
1 /*
2  * CAM SCSI device driver for the Adaptec 174X SCSI Host adapter
3  *
4  * Copyright (c) 1998 Justin T. Gibbs
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ahb/ahb.c,v 1.18.2.3 2001/03/05 13:08:55 obrien Exp $
29  * $DragonFly: src/sys/dev/disk/ahb/ahb.c,v 1.11 2005/10/12 17:35:49 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/thread2.h>
39
40 #include <machine/bus_pio.h>
41 #include <machine/bus.h>
42 #include <machine/clock.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45
46 #include <bus/cam/cam.h>
47 #include <bus/cam/cam_ccb.h>
48 #include <bus/cam/cam_sim.h>
49 #include <bus/cam/cam_xpt_sim.h>
50 #include <bus/cam/cam_debug.h>
51
52 #include <bus/cam/scsi/scsi_message.h>
53
54 #include <bus/eisa/eisaconf.h>
55
56 #include "ahbreg.h"
57
58 #define ccb_ecb_ptr spriv_ptr0
59 #define ccb_ahb_ptr spriv_ptr1
60
61 #define ahb_inb(ahb, port)                              \
62         bus_space_read_1((ahb)->tag, (ahb)->bsh, port)
63
64 #define ahb_inl(ahb, port)                              \
65         bus_space_read_4((ahb)->tag, (ahb)->bsh, port)
66
67 #define ahb_outb(ahb, port, value)                      \
68         bus_space_write_1((ahb)->tag, (ahb)->bsh, port, value)
69
70 #define ahb_outl(ahb, port, value)                      \
71         bus_space_write_4((ahb)->tag, (ahb)->bsh, port, value)
72
73 static const char               *ahbmatch(eisa_id_t type);
74 static struct ahb_softc         *ahballoc(u_long unit, struct resource *res);
75 static void                      ahbfree(struct ahb_softc *ahb);
76 static int                       ahbreset(struct ahb_softc *ahb);
77 static void                      ahbmapecbs(void *arg, bus_dma_segment_t *segs,
78                                             int nseg, int error);
79 static int                       ahbxptattach(struct ahb_softc *ahb);
80 static void                      ahbhandleimmed(struct ahb_softc *ahb,
81                                                 u_int32_t mbox, u_int intstat);
82 static void                      ahbcalcresid(struct ahb_softc *ahb,
83                                               struct ecb *ecb, union ccb *ccb);
84 static __inline void             ahbdone(struct ahb_softc *ahb, u_int32_t mbox,
85                                          u_int intstat);
86 static void                      ahbintr(void *arg);
87 static bus_dmamap_callback_t     ahbexecuteecb;
88 static void                      ahbaction(struct cam_sim *sim, union ccb *ccb);
89 static void                      ahbpoll(struct cam_sim *sim);
90
91 /* Our timeout handler */
92 static timeout_t ahbtimeout;
93
94 static __inline struct ecb*     ahbecbget(struct ahb_softc *ahb);
95 static __inline void            ahbecbfree(struct ahb_softc* ahb,
96                                            struct ecb* ecb);
97 static __inline u_int32_t       ahbecbvtop(struct ahb_softc *ahb,
98                                            struct ecb *ecb);
99 static __inline struct ecb*     ahbecbptov(struct ahb_softc *ahb,
100                                            u_int32_t ecb_addr);
101 static __inline u_int32_t       ahbstatuspaddr(u_int32_t ecb_paddr);
102 static __inline u_int32_t       ahbsensepaddr(u_int32_t ecb_paddr);
103 static __inline u_int32_t       ahbsgpaddr(u_int32_t ecb_paddr);
104 static __inline void            ahbqueuembox(struct ahb_softc *ahb,
105                                              u_int32_t mboxval,
106                                              u_int attn_code);
107
108 static __inline struct ecb*
109 ahbecbget(struct ahb_softc *ahb)
110 {
111         struct  ecb* ecb;
112
113         crit_enter();
114         if ((ecb = SLIST_FIRST(&ahb->free_ecbs)) != NULL)
115                 SLIST_REMOVE_HEAD(&ahb->free_ecbs, links);
116         crit_exit();
117
118         return (ecb);
119 }
120
121 static __inline void
122 ahbecbfree(struct ahb_softc* ahb, struct ecb* ecb)
123 {
124         crit_enter();
125         ecb->state = ECB_FREE;
126         SLIST_INSERT_HEAD(&ahb->free_ecbs, ecb, links);
127         crit_exit();
128 }
129
130 static __inline u_int32_t
131 ahbecbvtop(struct ahb_softc *ahb, struct ecb *ecb)
132 {
133         return (ahb->ecb_physbase
134               + (u_int32_t)((caddr_t)ecb - (caddr_t)ahb->ecb_array));
135 }
136
137 static __inline struct ecb*
138 ahbecbptov(struct ahb_softc *ahb, u_int32_t ecb_addr)
139 {
140         return (ahb->ecb_array
141               + ((struct ecb*)ecb_addr - (struct ecb*)ahb->ecb_physbase));
142 }
143
144 static __inline u_int32_t
145 ahbstatuspaddr(u_int32_t ecb_paddr)
146 {
147         return (ecb_paddr + offsetof(struct ecb, status));
148 }
149
150 static __inline u_int32_t
151 ahbsensepaddr(u_int32_t ecb_paddr)
152 {
153         return (ecb_paddr + offsetof(struct ecb, sense));
154 }
155
156 static __inline u_int32_t
157 ahbsgpaddr(u_int32_t ecb_paddr)
158 {
159         return (ecb_paddr + offsetof(struct ecb, sg_list));
160 }
161
162 static __inline void
163 ahbqueuembox(struct ahb_softc *ahb, u_int32_t mboxval, u_int attn_code)
164 {
165         u_int loopmax = 300;
166         while (--loopmax) {
167                 u_int status;
168
169                 status = ahb_inb(ahb, HOSTSTAT);
170                 if ((status & (HOSTSTAT_MBOX_EMPTY|HOSTSTAT_BUSY))
171                    == HOSTSTAT_MBOX_EMPTY)
172                         break;
173                 DELAY(20);
174         }
175         if (loopmax == 0)
176                 panic("ahb%ld: adapter not taking commands\n", ahb->unit);
177
178         ahb_outl(ahb, MBOXOUT0, mboxval);
179         ahb_outb(ahb, ATTN, attn_code);
180 }
181
182 static const char *
183 ahbmatch(eisa_id_t type)
184 {                         
185         switch(type & 0xfffffe00) {
186                 case EISA_DEVICE_ID_ADAPTEC_1740:
187                         return ("Adaptec 174x SCSI host adapter");
188                         break;
189                 default:
190                         break;
191         }
192         return (NULL);
193
194
195 static int
196 ahbprobe(device_t dev)      
197 {       
198         const char *desc;
199         u_int32_t iobase;
200         u_int32_t irq;
201         u_int8_t  intdef;      
202         int shared;
203                 
204         desc = ahbmatch(eisa_get_id(dev));
205         if (!desc)
206             return (ENXIO);
207         device_set_desc(dev, desc);
208
209         iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) +
210             AHB_EISA_SLOT_OFFSET;
211                         
212         eisa_add_iospace(dev, iobase, AHB_EISA_IOSIZE, RESVADDR_NONE);
213                 
214         intdef = inb(INTDEF + iobase);
215         switch (intdef & 0x7) {
216         case INT9:  
217             irq = 9;
218             break;
219         case INT10: 
220             irq = 10;
221             break;
222         case INT11:
223             irq = 11;
224             break;
225         case INT12:
226             irq = 12; 
227             break;
228         case INT14:
229             irq = 14;
230             break;
231         case INT15:
232             irq = 15;
233             break;
234         default:
235             printf("Adaptec 174X at slot %d: illegal "
236                    "irq setting %d\n", eisa_get_slot(dev),
237                    (intdef & 0x7));
238             irq = 0;
239             break;
240         }               
241         if (irq == 0)
242             return ENXIO;
243
244         shared = (inb(INTDEF + iobase) & INTLEVEL) ?
245                  EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
246
247         eisa_add_intr(dev, irq, shared);
248
249         return 0;   
250 }
251
252 static int
253 ahbattach(device_t dev)
254 {
255         /*
256          * find unit and check we have that many defined
257          */
258         struct      ahb_softc *ahb;
259         struct      ecb* next_ecb;
260         struct      resource *io = 0;
261         struct      resource *irq = 0;
262         int         rid;
263         void        *ih;
264
265         rid = 0;
266         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
267                                 0, ~0, 1, RF_ACTIVE);
268         if (!io) {
269                 device_printf(dev, "No I/O space?!\n");
270                 return ENOMEM;
271         }
272
273         if ((ahb = ahballoc(device_get_unit(dev), io)) == NULL) {
274                 goto error_exit2;
275         }
276
277         if (ahbreset(ahb) != 0)
278                 goto error_exit;
279
280         rid = 0;
281         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
282                                  0, ~0, 1, RF_ACTIVE);
283         if (!irq) {
284                 device_printf(dev, "Can't allocate interrupt\n");
285                 goto error_exit;
286         }
287
288         /*
289          * Create our DMA tags.  These tags define the kinds of device
290          * accessible memory allocations and memory mappings we will 
291          * need to perform during normal operation.
292          */
293         /* DMA tag for mapping buffers into device visible space. */
294         /* XXX Should be a child of the EISA bus dma tag */
295         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
296                                /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
297                                /*highaddr*/BUS_SPACE_MAXADDR,
298                                /*filter*/NULL, /*filterarg*/NULL,
299                                /*maxsize*/MAXBSIZE, /*nsegments*/AHB_NSEG,
300                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
301                                /*flags*/BUS_DMA_ALLOCNOW,
302                                &ahb->buffer_dmat) != 0)
303                 goto error_exit;
304
305         ahb->init_level++;
306
307         /* DMA tag for our ccb structures and ha inquiry data */
308         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
309                                /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
310                                /*highaddr*/BUS_SPACE_MAXADDR,
311                                /*filter*/NULL, /*filterarg*/NULL,
312                                (AHB_NECB * sizeof(struct ecb))
313                                + sizeof(*ahb->ha_inq_data),
314                                /*nsegments*/1,
315                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
316                                /*flags*/0, &ahb->ecb_dmat) != 0)
317                 goto error_exit;
318
319         ahb->init_level++;
320
321         /* Allocation for our ccbs */
322         if (bus_dmamem_alloc(ahb->ecb_dmat, (void **)&ahb->ecb_array,
323                              BUS_DMA_NOWAIT, &ahb->ecb_dmamap) != 0)
324                 goto error_exit;
325
326         ahb->ha_inq_data = (struct ha_inquiry_data *)&ahb->ecb_array[AHB_NECB];
327
328         ahb->init_level++;
329
330         /* And permanently map them */
331         bus_dmamap_load(ahb->ecb_dmat, ahb->ecb_dmamap,
332                         ahb->ecb_array, AHB_NSEG * sizeof(struct ecb),
333                         ahbmapecbs, ahb, /*flags*/0);
334
335         ahb->init_level++;
336
337         /* Allocate the buffer dmamaps for each of our ECBs */
338         bzero(ahb->ecb_array, (AHB_NECB * sizeof(struct ecb))
339               + sizeof(*ahb->ha_inq_data));
340         next_ecb = ahb->ecb_array;
341         while (ahb->num_ecbs < AHB_NECB) {
342                 u_int32_t ecb_paddr;
343
344                 if (bus_dmamap_create(ahb->buffer_dmat, /*flags*/0,
345                                       &next_ecb->dmamap))
346                         break;
347                 ecb_paddr = ahbecbvtop(ahb, next_ecb);
348                 next_ecb->hecb.status_ptr = ahbstatuspaddr(ecb_paddr);
349                 next_ecb->hecb.sense_ptr = ahbsensepaddr(ecb_paddr);
350                 ahb->num_ecbs++;
351                 ahbecbfree(ahb, next_ecb);
352                 next_ecb++;
353         }
354
355         if (ahb->num_ecbs == 0)
356                 goto error_exit;
357
358         ahb->init_level++;
359
360         /*
361          * Now that we know we own the resources we need, register
362          * our bus with the XPT.
363          */
364         if (ahbxptattach(ahb))
365                 goto error_exit;
366
367         /* Enable our interrupt */
368         bus_setup_intr(dev, irq, 0, ahbintr, ahb, &ih, NULL);
369         return (0);
370
371 error_exit:
372         /*
373          * The board's IRQ line will not be left enabled
374          * if we can't intialize correctly, so its safe
375          * to release the irq.
376          */
377         ahbfree(ahb);
378 error_exit2:
379         if (io)
380                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
381         if (irq)
382                 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
383         return (-1);
384 }
385
386 static struct ahb_softc *
387 ahballoc(u_long unit, struct resource *res)
388 {
389         struct  ahb_softc *ahb;
390
391         /*
392          * Allocate a storage area for us
393          */
394         ahb = malloc(sizeof(struct ahb_softc), M_DEVBUF, M_INTWAIT | M_ZERO);
395         SLIST_INIT(&ahb->free_ecbs);
396         LIST_INIT(&ahb->pending_ccbs);
397         ahb->unit = unit;
398         ahb->tag = rman_get_bustag(res);
399         ahb->bsh = rman_get_bushandle(res);
400         ahb->disc_permitted = ~0;
401         ahb->tags_permitted = ~0;
402
403         return (ahb);
404 }
405
406 static void    
407 ahbfree(struct ahb_softc *ahb)
408 {
409         switch (ahb->init_level) {
410         default:
411         case 4:
412                 bus_dmamap_unload(ahb->ecb_dmat, ahb->ecb_dmamap);
413                 /* fall through */
414         case 3:
415                 bus_dmamem_free(ahb->ecb_dmat, ahb->ecb_array,
416                                 ahb->ecb_dmamap);
417                 bus_dmamap_destroy(ahb->ecb_dmat, ahb->ecb_dmamap);
418                 /* fall through */
419         case 2:
420                 bus_dma_tag_destroy(ahb->ecb_dmat);
421                 /* fall through */
422         case 1:
423                 bus_dma_tag_destroy(ahb->buffer_dmat);
424                 /* fall through */
425         case 0:
426                 break;
427         }
428         free(ahb, M_DEVBUF);
429 }
430
431 /*
432  * reset board, If it doesn't respond, return failure
433  */
434 static int
435 ahbreset(struct ahb_softc *ahb)
436 {
437         int     wait = 1000;    /* 1 sec enough? */
438         int     test;
439
440         if ((ahb_inb(ahb, PORTADDR) & PORTADDR_ENHANCED) == 0) {
441                 printf("ahb_reset: Controller not in enhanced mode\n");
442                 return (-1);
443         }
444
445         ahb_outb(ahb, CONTROL, CNTRL_HARD_RST);
446         DELAY(1000);
447         ahb_outb(ahb, CONTROL, 0);
448         while (--wait) {
449                 DELAY(1000);
450                 if ((ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_BUSY) == 0)
451                         break;
452         }
453
454         if (wait == 0) {
455                 printf("ahbreset: No answer from aha1742 board\n");
456                 return (-1);
457         }
458         if ((test = ahb_inb(ahb, MBOXIN0)) != 0) {
459                 printf("ahb_reset: self test failed, val = 0x%x\n", test);
460                 return (-1);
461         }
462         while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
463                 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
464                 DELAY(10000);
465         }
466         return (0);
467 }
468
469 static void
470 ahbmapecbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
471 {
472         struct ahb_softc* ahb;
473
474         ahb = (struct ahb_softc*)arg;
475         ahb->ecb_physbase = segs->ds_addr;
476         /*
477          * Space for adapter inquiry information is on the
478          * tail of the ecb array.
479          */
480         ahb->ha_inq_physbase = ahbecbvtop(ahb, &ahb->ecb_array[AHB_NECB]);
481 }
482
483 static int
484 ahbxptattach(struct ahb_softc *ahb)
485 {
486         struct ecb *ecb;
487         u_int  i;
488
489         /* Remeber who are we on the scsi bus */
490         ahb->scsi_id = ahb_inb(ahb, SCSIDEF) & HSCSIID;
491
492         /* Use extended translation?? */
493         ahb->extended_trans = ahb_inb(ahb, RESV1) & EXTENDED_TRANS;
494
495         /* Fetch adapter inquiry data */
496         ecb = ahbecbget(ahb);   /* Always succeeds - no outstanding commands */
497         ecb->hecb.opcode = ECBOP_READ_HA_INQDATA;
498         ecb->hecb.flag_word1 = FW1_SUPPRESS_URUN_ERR|FW1_ERR_STATUS_BLK_ONLY;
499         ecb->hecb.data_ptr = ahb->ha_inq_physbase;
500         ecb->hecb.data_len = sizeof(struct ha_inquiry_data);
501         ecb->hecb.sense_ptr = 0;
502         ecb->state = ECB_ACTIVE;
503         
504         /* Tell the adapter about this command */
505         ahbqueuembox(ahb, ahbecbvtop(ahb, ecb),
506                      ATTN_STARTECB|ahb->scsi_id);
507
508         /* Poll for interrupt completion */
509         for (i = 1000; ecb->state != ECB_FREE && i != 0; i--) {
510                 ahbintr(ahb);
511                 DELAY(1000);
512         }
513
514         ahb->num_ecbs = MIN(ahb->num_ecbs,
515                             ahb->ha_inq_data->scsi_data.reserved[1]);
516         printf("ahb%ld: %.8s %s SCSI Adapter, FW Rev. %.4s, ID=%d, %d ECBs\n",
517                ahb->unit, ahb->ha_inq_data->scsi_data.product,
518                (ahb->ha_inq_data->scsi_data.flags & 0x4) ? "Differential"
519                                                          : "Single Ended",
520                ahb->ha_inq_data->scsi_data.revision,
521                ahb->scsi_id, ahb->num_ecbs);
522
523         /* Restore sense paddr for future CCB clients */
524         ecb->hecb.sense_ptr = ahbsensepaddr(ahbecbvtop(ahb, ecb));
525
526         ahbecbfree(ahb, ecb);
527
528         /*
529          * Construct our SIM entry
530          */
531         ahb->sim = cam_sim_alloc(ahbaction, ahbpoll, "ahb", ahb, ahb->unit,
532                                  2, ahb->num_ecbs, NULL);
533         if (ahb->sim == NULL)
534                 return (ENOMEM);
535
536         if (xpt_bus_register(ahb->sim, 0) != CAM_SUCCESS) {
537                 cam_sim_free(ahb->sim);
538                 return (ENXIO);
539         }
540         
541         if (xpt_create_path(&ahb->path, /*periph*/NULL,
542                             cam_sim_path(ahb->sim), CAM_TARGET_WILDCARD,
543                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
544                 xpt_bus_deregister(cam_sim_path(ahb->sim));
545                 cam_sim_free(ahb->sim);
546                 return (ENXIO);
547         }
548                 
549         /*
550          * Allow the board to generate interrupts.
551          */
552         ahb_outb(ahb, INTDEF, ahb_inb(ahb, INTDEF) | INTEN);
553
554         return (0);
555 }
556
557 static void
558 ahbhandleimmed(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
559 {
560         struct ccb_hdr *ccb_h;
561         u_int target_id;
562
563         if (ahb->immed_cmd == 0) {
564                 printf("ahb%ld: Immediate Command complete with no "
565                        " pending command\n", ahb->unit);
566                 return;
567         }
568
569         target_id = intstat & INTSTAT_TARGET_MASK;
570
571         ccb_h = LIST_FIRST(&ahb->pending_ccbs);
572         while (ccb_h != NULL) {
573                 struct ecb *pending_ecb;
574                 union ccb *ccb;
575
576                 pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
577                 ccb = pending_ecb->ccb;
578                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
579                 if (ccb->ccb_h.target_id == target_id
580                  || target_id == ahb->scsi_id) {
581                         callout_stop(&ccb->ccb_h.timeout_ch);
582                         LIST_REMOVE(&ccb->ccb_h, sim_links.le);
583                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
584                                 bus_dmamap_unload(ahb->buffer_dmat,
585                                                   pending_ecb->dmamap);
586                         if (pending_ecb == ahb->immed_ecb)
587                                 ccb->ccb_h.status =
588                                     CAM_CMD_TIMEOUT|CAM_RELEASE_SIMQ;
589                         else if (target_id == ahb->scsi_id)
590                                 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
591                         else
592                                 ccb->ccb_h.status = CAM_BDR_SENT;
593                         ahbecbfree(ahb, pending_ecb);
594                         xpt_done(ccb);
595                 } else if (ahb->immed_ecb != NULL) {
596                         /* Re-instate timeout */
597                         callout_reset(&ccb->ccb_h.timeout_ch,
598                             (ccb->ccb_h.timeout * hz) / 1000,
599                             ahbtimeout, pending_ecb);
600                 }
601         }
602
603         if (ahb->immed_ecb != NULL) {
604                 ahb->immed_ecb = NULL;
605                 printf("ahb%ld: No longer in timeout\n", ahb->unit);
606         } else if (target_id == ahb->scsi_id)
607                 printf("ahb%ld: SCSI Bus Reset Delivered\n", ahb->unit);
608         else
609                 printf("ahb%ld:  Bus Device Reset Delibered to target %d\n",
610                        ahb->unit, target_id);
611
612         ahb->immed_cmd = 0;
613 }
614
615 static void
616 ahbcalcresid(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
617 {
618         if (ecb->status.data_overrun != 0) {
619                 /*
620                  * Overrun Condition.  The hardware doesn't
621                  * provide a meaningful byte count in this case
622                  * (the residual is always 0).  Tell the XPT
623                  * layer about the error.
624                  */
625                 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
626         } else {
627                 ccb->csio.resid = ecb->status.resid_count;
628
629                 if ((ecb->hecb.flag_word1 & FW1_SG_ECB) != 0) {
630                         /*
631                          * For S/G transfers, the adapter provides a pointer
632                          * to the address in the last S/G element used and a
633                          * residual for that element.  So, we need to sum up
634                          * the elements that follow it in order to get a real
635                          * residual number.  If we have an overrun, the residual
636                          * reported will be 0 and we already know that all S/G
637                          * segments have been exhausted, so we can skip this
638                          * step.
639                          */
640                         ahb_sg_t *sg;
641                         int       num_sg;
642
643                         num_sg = ecb->hecb.data_len / sizeof(ahb_sg_t);
644
645                         /* Find the S/G the adapter was working on */
646                         for (sg = ecb->sg_list;
647                              num_sg != 0 && sg->addr != ecb->status.resid_addr;
648                              num_sg--, sg++)
649                                 ;
650
651                         /* Skip it */
652                         num_sg--;
653                         sg++;
654
655                         /* Sum the rest */
656                         for (; num_sg != 0; num_sg--, sg++)
657                                 ccb->csio.resid += sg->len;
658                 }
659                 /* Underruns are not errors */
660                 ccb->ccb_h.status = CAM_REQ_CMP;
661         }
662 }
663
664 static void
665 ahbprocesserror(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
666 {
667         struct hardware_ecb *hecb;
668         struct ecb_status *status;
669
670         hecb = &ecb->hecb;
671         status = &ecb->status;
672         switch (status->ha_status) {
673         case HS_OK:
674                 ccb->csio.scsi_status = status->scsi_status;
675                 if (status->scsi_status != 0) {
676                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
677                         if (status->sense_stored) {
678                                 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
679                                 ccb->csio.sense_resid =
680                                     ccb->csio.sense_len - status->sense_len;
681                                 bcopy(&ecb->sense, &ccb->csio.sense_data,
682                                       status->sense_len);
683                         }
684                 }
685                 break;
686         case HS_TARGET_NOT_ASSIGNED:
687                 ccb->ccb_h.status = CAM_PATH_INVALID;
688                 break;
689         case HS_SEL_TIMEOUT:
690                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
691                 break;
692         case HS_DATA_RUN_ERR:
693                 ahbcalcresid(ahb, ecb, ccb);
694                 break;
695         case HS_UNEXPECTED_BUSFREE:
696                 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
697                 break;
698         case HS_INVALID_PHASE:
699                 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
700                 break;
701         case HS_REQUEST_SENSE_FAILED:
702                 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
703                 break;
704         case HS_TAG_MSG_REJECTED:
705         {
706                 struct ccb_trans_settings neg; 
707
708                 xpt_print_path(ccb->ccb_h.path);
709                 printf("refuses tagged commands.  Performing "
710                        "non-tagged I/O\n");
711                 neg.flags = 0;
712                 neg.valid = CCB_TRANS_TQ_VALID;
713                 xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path, /*priority*/1); 
714                 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
715                 ahb->tags_permitted &= ~(0x01 << ccb->ccb_h.target_id);
716                 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
717                 break;
718         }
719         case HS_FIRMWARE_LOAD_REQ:
720         case HS_HARDWARE_ERR:
721                 /*
722                  * Tell the system that the Adapter
723                  * is no longer functional.
724                  */
725                 ccb->ccb_h.status = CAM_NO_HBA;
726                 break;
727         case HS_CMD_ABORTED_HOST:
728         case HS_CMD_ABORTED_ADAPTER:
729         case HS_ATN_TARGET_FAILED:
730         case HS_SCSI_RESET_ADAPTER:
731         case HS_SCSI_RESET_INCOMING:
732                 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
733                 break;
734         case HS_INVALID_ECB_PARAM:
735                 printf("ahb%ld: opcode 0x%02x, flag_word1 0x%02x, flag_word2 0x%02x\n",
736                         ahb->unit, hecb->opcode, hecb->flag_word1, hecb->flag_word2);   
737                 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
738                 break;
739         case HS_DUP_TCB_RECEIVED:
740         case HS_INVALID_OPCODE:
741         case HS_INVALID_CMD_LINK:
742         case HS_PROGRAM_CKSUM_ERROR:
743                 panic("ahb%ld: Can't happen host status %x occurred",
744                       ahb->unit, status->ha_status);
745                 break;
746         }
747         if (ccb->ccb_h.status != CAM_REQ_CMP) {
748                 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
749                 ccb->ccb_h.status |= CAM_DEV_QFRZN;
750         }
751 }
752
753 static void
754 ahbdone(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
755 {
756         struct ecb *ecb;
757         union ccb *ccb;
758
759         ecb = ahbecbptov(ahb, mbox);
760
761         if ((ecb->state & ECB_ACTIVE) == 0)
762                 panic("ecb not active");
763
764         ccb = ecb->ccb;
765
766         if (ccb != NULL) {
767                 callout_stop(&ccb->ccb_h.timeout_ch);
768                 LIST_REMOVE(&ccb->ccb_h, sim_links.le);
769
770                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
771                         bus_dmasync_op_t op;
772
773                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
774                                 op = BUS_DMASYNC_POSTREAD;
775                         else
776                                 op = BUS_DMASYNC_POSTWRITE;
777                         bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
778                         bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
779                 }
780
781                 if ((intstat & INTSTAT_MASK) == INTSTAT_ECB_OK) {
782                         ccb->ccb_h.status = CAM_REQ_CMP;
783                         ccb->csio.resid = 0;
784                 } else {
785                         ahbprocesserror(ahb, ecb, ccb);
786                 }
787                 ahbecbfree(ahb, ecb);
788                 xpt_done(ccb);
789         } else {
790                 /* Non CCB Command */
791                 if ((intstat & INTSTAT_MASK) != INTSTAT_ECB_OK) {
792                         printf("ahb%ld: Command 0%x Failed %x:%x:%x\n",
793                                ahb->unit, ecb->hecb.opcode,
794                                *((u_int16_t*)&ecb->status),
795                                ecb->status.ha_status, ecb->status.resid_count);
796                 }
797                 /* Client owns this ECB and will release it. */
798         }
799 }
800
801 /*
802  * Catch an interrupt from the adaptor
803  */
804 static void
805 ahbintr(void *arg)
806 {
807         struct    ahb_softc *ahb;
808         u_int     intstat;
809         u_int32_t mbox;
810
811         ahb = (struct ahb_softc *)arg;
812
813         while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
814                 /*
815                  * Fetch information about this interrupt.
816                  */
817                 intstat = ahb_inb(ahb, INTSTAT);
818                 mbox = ahb_inl(ahb, MBOXIN0);
819
820                 /*
821                  * Reset interrupt latch.
822                  */
823                 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
824
825                 /*
826                  * Process the completed operation
827                  */
828                 switch (intstat & INTSTAT_MASK) {
829                 case INTSTAT_ECB_OK:
830                 case INTSTAT_ECB_CMPWRETRY:
831                 case INTSTAT_ECB_CMPWERR:
832                         ahbdone(ahb, mbox, intstat);
833                         break;
834                 case INTSTAT_AEN_OCCURED:
835                         if ((intstat & INTSTAT_TARGET_MASK) == ahb->scsi_id) {
836                                 /* Bus Reset */
837                                 xpt_print_path(ahb->path);
838                                 switch (mbox) {
839                                 case HS_SCSI_RESET_ADAPTER:
840                                         printf("Host Adapter Initiated "
841                                                "Bus Reset occurred\n");
842                                         break;
843                                 case HS_SCSI_RESET_INCOMING:
844                                         printf("Bus Reset Initiated "
845                                                "by another device occurred\n");
846                                         break;
847                                 }
848                                 /* Notify the XPT */
849                                 xpt_async(AC_BUS_RESET, ahb->path, NULL);
850                                 break;
851                         }
852                         printf("Unsupported initiator selection AEN occured\n");
853                         break;
854                 case INTSTAT_IMMED_OK:
855                 case INTSTAT_IMMED_ERR:
856                         ahbhandleimmed(ahb, mbox, intstat);
857                         break;
858                 case INTSTAT_HW_ERR:
859                         panic("Unrecoverable hardware Error Occurred\n");
860                 }
861         }
862 }
863
864 static void
865 ahbexecuteecb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
866 {
867         struct    ecb *ecb;
868         union     ccb *ccb;
869         struct    ahb_softc *ahb;
870         u_int32_t ecb_paddr;
871
872         ecb = (struct ecb *)arg;
873         ccb = ecb->ccb;
874         ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
875
876         if (error != 0) {
877                 if (error != EFBIG)
878                         printf("ahb%ld: Unexepected error 0x%x returned from "
879                                "bus_dmamap_load\n", ahb->unit, error);
880                 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
881                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
882                         ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
883                 }
884                 ahbecbfree(ahb, ecb);
885                 xpt_done(ccb);
886                 return;
887         }
888                 
889         ecb_paddr = ahbecbvtop(ahb, ecb);
890
891         if (nseg != 0) {
892                 ahb_sg_t *sg;
893                 bus_dma_segment_t *end_seg;
894                 bus_dmasync_op_t op;
895
896                 end_seg = dm_segs + nseg;
897
898                 /* Copy the segments into our SG list */
899                 sg = ecb->sg_list;
900                 while (dm_segs < end_seg) {
901                         sg->addr = dm_segs->ds_addr;
902                         sg->len = dm_segs->ds_len;
903                         sg++;
904                         dm_segs++;
905                 }
906
907                 if (nseg > 1) {
908                         ecb->hecb.flag_word1 |= FW1_SG_ECB;
909                         ecb->hecb.data_ptr = ahbsgpaddr(ecb_paddr);
910                         ecb->hecb.data_len = sizeof(ahb_sg_t) * nseg;
911                 } else {
912                         ecb->hecb.data_ptr = ecb->sg_list->addr;
913                         ecb->hecb.data_len = ecb->sg_list->len;
914                 }
915
916                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
917 /*                      ecb->hecb.flag_word2 |= FW2_DATA_DIR_IN; */
918                         op = BUS_DMASYNC_PREREAD;
919                 } else {
920                         op = BUS_DMASYNC_PREWRITE;
921                 }
922                 /* ecb->hecb.flag_word2 |= FW2_CHECK_DATA_DIR; */
923
924                 bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
925
926         } else {
927                 ecb->hecb.data_ptr = 0;
928                 ecb->hecb.data_len = 0;
929         }
930
931         crit_enter();
932
933         /*
934          * Last time we need to check if this CCB needs to
935          * be aborted.
936          */
937         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
938                 if (nseg != 0)
939                         bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
940                 ahbecbfree(ahb, ecb);
941                 xpt_done(ccb);
942                 crit_exit();
943                 return;
944         }
945                 
946         ecb->state = ECB_ACTIVE;
947         ccb->ccb_h.status |= CAM_SIM_QUEUED;
948         LIST_INSERT_HEAD(&ahb->pending_ccbs, &ccb->ccb_h, sim_links.le);
949
950         /* Tell the adapter about this command */
951         ahbqueuembox(ahb, ecb_paddr, ATTN_STARTECB|ccb->ccb_h.target_id);
952
953         callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
954                       ahbtimeout, ecb);
955         crit_exit();
956 }
957
958 static void
959 ahbaction(struct cam_sim *sim, union ccb *ccb)
960 {
961         struct  ahb_softc *ahb;
962
963         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahbaction\n"));
964         
965         ahb = (struct ahb_softc *)cam_sim_softc(sim);
966         
967         switch (ccb->ccb_h.func_code) {
968         /* Common cases first */
969         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
970         {
971                 struct ecb *ecb;
972                 struct hardware_ecb *hecb;
973
974                 /*
975                  * get an ecb to use.
976                  */
977                 if ((ecb = ahbecbget(ahb)) == NULL) {
978                         /* Should never occur */
979                         panic("Failed to get an ecb");
980                 }
981
982                 /*
983                  * So we can find the ECB when an abort is requested
984                  */
985                 ecb->ccb = ccb;
986                 ccb->ccb_h.ccb_ecb_ptr = ecb;
987                 ccb->ccb_h.ccb_ahb_ptr = ahb;
988
989                 /*
990                  * Put all the arguments for the xfer in the ecb
991                  */
992                 hecb = &ecb->hecb;
993                 hecb->opcode = ECBOP_INITIATOR_SCSI_CMD;
994                 hecb->flag_word1 = FW1_AUTO_REQUEST_SENSE
995                                  | FW1_ERR_STATUS_BLK_ONLY;
996                 hecb->flag_word2 = ccb->ccb_h.target_lun
997                                  | FW2_NO_RETRY_ON_BUSY;
998                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
999                         hecb->flag_word2 |= FW2_TAG_ENB
1000                                          | ((ccb->csio.tag_action & 0x3)
1001                                             << FW2_TAG_TYPE_SHIFT);
1002                 }
1003                 if ((ccb->ccb_h.flags & CAM_DIS_DISCONNECT) != 0)
1004                         hecb->flag_word2 |= FW2_DISABLE_DISC;
1005                 hecb->sense_len = ccb->csio.sense_len;
1006                 hecb->cdb_len = ccb->csio.cdb_len;
1007                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1008                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
1009                                 bcopy(ccb->csio.cdb_io.cdb_ptr,
1010                                       hecb->cdb, hecb->cdb_len);
1011                         } else {
1012                                 /* I guess I could map it in... */
1013                                 ccb->ccb_h.status = CAM_REQ_INVALID;
1014                                 ahbecbfree(ahb, ecb);
1015                                 xpt_done(ccb);
1016                                 return;
1017                         }
1018                 } else {
1019                         bcopy(ccb->csio.cdb_io.cdb_bytes,
1020                               hecb->cdb, hecb->cdb_len);
1021                 }
1022
1023                 /*
1024                  * If we have any data to send with this command,
1025                  * map it into bus space.
1026                  */
1027                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1028                         if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1029                                 /*
1030                                  * We've been given a pointer
1031                                  * to a single buffer.
1032                                  */
1033                                 if ((ccb->ccb_h.flags & CAM_DATA_PHYS)==0) {
1034                                         int error;
1035
1036                                         crit_enter();
1037                                         error = bus_dmamap_load(
1038                                             ahb->buffer_dmat,
1039                                             ecb->dmamap,
1040                                             ccb->csio.data_ptr,
1041                                             ccb->csio.dxfer_len,
1042                                             ahbexecuteecb,
1043                                             ecb, /*flags*/0);
1044                                         if (error == EINPROGRESS) {
1045                                                 /*
1046                                                  * So as to maintain ordering,
1047                                                  * freeze the controller queue
1048                                                  * until our mapping is
1049                                                  * returned.
1050                                                  */
1051                                                 xpt_freeze_simq(ahb->sim, 1);
1052                                                 ccb->ccb_h.status |=
1053                                                     CAM_RELEASE_SIMQ;
1054                                         }
1055                                         crit_exit();
1056                                 } else {
1057                                         struct bus_dma_segment seg; 
1058
1059                                         /* Pointer to physical buffer */
1060                                         seg.ds_addr =
1061                                             (bus_addr_t)ccb->csio.data_ptr;
1062                                         seg.ds_len = ccb->csio.dxfer_len;
1063                                         ahbexecuteecb(ecb, &seg, 1, 0);
1064                                 }
1065                         } else {
1066                                 struct bus_dma_segment *segs;
1067
1068                                 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
1069                                         panic("ahbaction - Physical segment "
1070                                               "pointers unsupported");
1071
1072                                 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0)
1073                                         panic("btaction - Virtual segment "
1074                                               "addresses unsupported");
1075
1076                                 /* Just use the segments provided */
1077                                 segs = (struct bus_dma_segment *)
1078                                     ccb->csio.data_ptr;
1079                                 ahbexecuteecb(ecb, segs, ccb->csio.sglist_cnt,
1080                                              0);
1081                         }
1082                 } else {
1083                         ahbexecuteecb(ecb, NULL, 0, 0);
1084                 }
1085                 break;
1086         }
1087         case XPT_EN_LUN:                /* Enable LUN as a target */
1088         case XPT_TARGET_IO:             /* Execute target I/O request */
1089         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
1090         case XPT_CONT_TARGET_IO:        /* Continue Host Target I/O Connection*/
1091         case XPT_ABORT:                 /* Abort the specified CCB */
1092                 /* XXX Implement */
1093                 ccb->ccb_h.status = CAM_REQ_INVALID;
1094                 xpt_done(ccb);
1095                 break;
1096         case XPT_SET_TRAN_SETTINGS:
1097         {
1098                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1099                 xpt_done(ccb);
1100                 break;
1101         }
1102         case XPT_GET_TRAN_SETTINGS:
1103         /* Get default/user set transfer settings for the target */
1104         {
1105                 struct  ccb_trans_settings *cts;
1106                 u_int   target_mask;
1107
1108                 cts = &ccb->cts;
1109                 target_mask = 0x01 << ccb->ccb_h.target_id;
1110                 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1111                         cts->flags = 0;
1112                         if ((ahb->disc_permitted & target_mask) != 0)
1113                                 cts->flags |= CCB_TRANS_DISC_ENB;
1114                         if ((ahb->tags_permitted & target_mask) != 0)
1115                                 cts->flags |= CCB_TRANS_TAG_ENB;
1116                         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1117                         cts->sync_period = 25; /* 10MHz */
1118
1119                         if (cts->sync_period != 0)
1120                                 cts->sync_offset = 15;
1121
1122                         cts->valid = CCB_TRANS_SYNC_RATE_VALID
1123                                    | CCB_TRANS_SYNC_OFFSET_VALID
1124                                    | CCB_TRANS_BUS_WIDTH_VALID
1125                                    | CCB_TRANS_DISC_VALID
1126                                    | CCB_TRANS_TQ_VALID;
1127                         ccb->ccb_h.status = CAM_REQ_CMP;
1128                 } else {
1129                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1130                 }
1131                 xpt_done(ccb);
1132                 break;
1133         }
1134         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
1135         {
1136                 int i;
1137
1138                 crit_enter();
1139                 ahb->immed_cmd = IMMED_RESET;
1140                 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1141                 /* Poll for interrupt completion */
1142                 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--) {
1143                         DELAY(1000);
1144                         ahbintr(cam_sim_softc(sim));
1145                 }
1146                 crit_exit();
1147                 break;
1148         }
1149         case XPT_CALC_GEOMETRY:
1150         {
1151                 struct    ccb_calc_geometry *ccg;
1152                 u_int32_t size_mb;
1153                 u_int32_t secs_per_cylinder;
1154
1155                 ccg = &ccb->ccg;
1156                 size_mb = ccg->volume_size
1157                         / ((1024L * 1024L) / ccg->block_size);
1158                 
1159                 if (size_mb > 1024 && (ahb->extended_trans != 0)) {
1160                         ccg->heads = 255;
1161                         ccg->secs_per_track = 63;
1162                 } else {
1163                         ccg->heads = 64;
1164                         ccg->secs_per_track = 32;
1165                 }
1166                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1167                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1168                 ccb->ccb_h.status = CAM_REQ_CMP;
1169                 xpt_done(ccb);
1170                 break;
1171         }
1172         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
1173         {
1174                 int i;
1175
1176                 ahb->immed_cmd = IMMED_RESET;
1177                 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1178                 /* Poll for interrupt completion */
1179                 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--)
1180                         DELAY(1000);
1181                 ccb->ccb_h.status = CAM_REQ_CMP;
1182                 xpt_done(ccb);
1183                 break;
1184         }
1185         case XPT_TERM_IO:               /* Terminate the I/O process */
1186                 /* XXX Implement */
1187                 ccb->ccb_h.status = CAM_REQ_INVALID;
1188                 xpt_done(ccb);
1189                 break;
1190         case XPT_PATH_INQ:              /* Path routing inquiry */
1191         {
1192                 struct ccb_pathinq *cpi = &ccb->cpi;
1193                 
1194                 cpi->version_num = 1; /* XXX??? */
1195                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1196                 cpi->target_sprt = 0;
1197                 cpi->hba_misc = 0;
1198                 cpi->hba_eng_cnt = 0;
1199                 cpi->max_target = 7;
1200                 cpi->max_lun = 7;
1201                 cpi->initiator_id = ahb->scsi_id;
1202                 cpi->bus_id = cam_sim_bus(sim);
1203                 cpi->base_transfer_speed = 3300;
1204                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1205                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1206                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1207                 cpi->unit_number = cam_sim_unit(sim);
1208                 cpi->ccb_h.status = CAM_REQ_CMP;
1209                 xpt_done(ccb);
1210                 break;
1211         }
1212 #if 0
1213         /* Need these??? */
1214         case XPT_IMMED_NOTIFY:          /* Notify Host Target driver of event */
1215         case XPT_NOTIFY_ACK:            /* Acknowledgement of event */
1216 #endif
1217         default:
1218                 ccb->ccb_h.status = CAM_REQ_INVALID;
1219                 xpt_done(ccb);
1220                 break;
1221         }
1222 }
1223
1224 static void
1225 ahbpoll(struct cam_sim *sim)
1226 {
1227         ahbintr(cam_sim_softc(sim));
1228 }
1229
1230 static void
1231 ahbtimeout(void *arg)
1232 {
1233         struct ecb       *ecb;
1234         union  ccb       *ccb;
1235         struct ahb_softc *ahb;
1236
1237         ecb = (struct ecb *)arg;
1238         ccb = ecb->ccb;
1239         ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
1240         xpt_print_path(ccb->ccb_h.path);
1241         printf("ECB %p - timed out\n", (void *)ecb);
1242
1243         crit_enter();
1244
1245         if ((ecb->state & ECB_ACTIVE) == 0) {
1246                 xpt_print_path(ccb->ccb_h.path);
1247                 printf("ECB %p - timed out ECB already completed\n",
1248                        (void *)ecb);
1249                 crit_exit();
1250                 return;
1251         }
1252         /*
1253          * In order to simplify the recovery process, we ask the XPT
1254          * layer to halt the queue of new transactions and we traverse
1255          * the list of pending CCBs and remove their timeouts. This
1256          * means that the driver attempts to clear only one error
1257          * condition at a time.  In general, timeouts that occur
1258          * close together are related anyway, so there is no benefit
1259          * in attempting to handle errors in parrallel.  Timeouts will
1260          * be reinstated when the recovery process ends.
1261          */
1262         if ((ecb->state & ECB_DEVICE_RESET) == 0) {
1263                 struct ccb_hdr *ccb_h;
1264
1265                 if ((ecb->state & ECB_RELEASE_SIMQ) == 0) {
1266                         xpt_freeze_simq(ahb->sim, /*count*/1);
1267                         ecb->state |= ECB_RELEASE_SIMQ;
1268                 }
1269
1270                 ccb_h = LIST_FIRST(&ahb->pending_ccbs);
1271                 while (ccb_h != NULL) {
1272                         struct ecb *pending_ecb;
1273
1274                         pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
1275                         callout_stop(&ccb_h->timeout_ch);
1276                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1277                 }
1278
1279                 /* Store for our interrupt handler */
1280                 ahb->immed_ecb = ecb;
1281
1282                 /*    
1283                  * Send a Bus Device Reset message:
1284                  * The target that is holding up the bus may not
1285                  * be the same as the one that triggered this timeout
1286                  * (different commands have different timeout lengths),
1287                  * but we have no way of determining this from our
1288                  * timeout handler.  Our strategy here is to queue a
1289                  * BDR message to the target of the timed out command.
1290                  * If this fails, we'll get another timeout 2 seconds
1291                  * later which will attempt a bus reset.
1292                  */
1293                 xpt_print_path(ccb->ccb_h.path);
1294                 printf("Queuing BDR\n");
1295                 ecb->state |= ECB_DEVICE_RESET;
1296                 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahbtimeout, ecb);
1297
1298                 ahb->immed_cmd = IMMED_RESET;
1299                 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1300         } else if ((ecb->state & ECB_SCSIBUS_RESET) != 0) {
1301                 /*
1302                  * Try a SCSI bus reset.  We do this only if we
1303                  * have already attempted to clear the condition with a BDR.
1304                  */
1305                 xpt_print_path(ccb->ccb_h.path);
1306                 printf("Attempting SCSI Bus reset\n");
1307                 ecb->state |= ECB_SCSIBUS_RESET;
1308                 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahbtimeout, ecb);
1309                 ahb->immed_cmd = IMMED_RESET;
1310                 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1311         } else {
1312                 /* Bring out the hammer... */
1313                 ahbreset(ahb);
1314
1315                 /* Simulate the reset complete interrupt */
1316                 ahbhandleimmed(ahb, 0, ahb->scsi_id|INTSTAT_IMMED_OK);
1317         }
1318
1319         crit_exit();
1320 }
1321
1322 static device_method_t ahb_eisa_methods[] = {
1323         /* Device interface */
1324         DEVMETHOD(device_probe,         ahbprobe),
1325         DEVMETHOD(device_attach,        ahbattach),
1326
1327         { 0, 0 }
1328 };
1329
1330 static driver_t ahb_eisa_driver = {
1331         "ahb",
1332         ahb_eisa_methods,
1333         1,                      /* unused */
1334 };
1335
1336 static devclass_t ahb_devclass;
1337
1338 DRIVER_MODULE(ahb, eisa, ahb_eisa_driver, ahb_devclass, 0, 0);