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