]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/aha/aha.c
Merge ^/head r313644 through r313895.
[FreeBSD/FreeBSD.git] / sys / dev / aha / aha.c
1 /*
2  * Generic register and struct definitions for the Adaptech 154x
3  * SCSI host adapters. Product specific probe and attach routines can
4  * be found in:
5  *      aha 1542A/1542B/1542C/1542CF/1542CP     aha_isa.c
6  */
7 /*-
8  * Copyright (c) 1998 M. Warner Losh.
9  * All Rights Reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Derived from bt.c written by:
33  *
34  * Copyright (c) 1998 Justin T. Gibbs.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions, and the following disclaimer,
42  *    without modification, immediately at the beginning of the file.
43  * 2. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
50  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 #include <sys/param.h>
63 #include <sys/conf.h>
64 #include <sys/bus.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
68 #include <sys/lock.h>
69 #include <sys/module.h>
70 #include <sys/mutex.h>
71 #include <sys/rman.h>
72
73 #include <machine/bus.h>
74
75 #include <cam/cam.h>
76 #include <cam/cam_ccb.h>
77 #include <cam/cam_sim.h>
78 #include <cam/cam_xpt_sim.h>
79 #include <cam/cam_debug.h>
80
81 #include <cam/scsi/scsi_message.h>
82
83 #include <dev/aha/ahareg.h>
84
85 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
86
87 /* Macro to determine that a rev is potentially a new valid one
88  * so that the driver doesn't keep breaking on new revs as it
89  * did for the CF and CP.
90  */
91 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
92
93 /* MailBox Management functions */
94 static __inline void    ahanextinbox(struct aha_softc *aha);
95 static __inline void    ahanextoutbox(struct aha_softc *aha);
96
97 #define aha_name(aha)   device_get_nameunit(aha->dev)
98
99 static __inline void
100 ahanextinbox(struct aha_softc *aha)
101 {
102         if (aha->cur_inbox == aha->last_inbox)
103                 aha->cur_inbox = aha->in_boxes;
104         else
105                 aha->cur_inbox++;
106 }
107
108 static __inline void
109 ahanextoutbox(struct aha_softc *aha)
110 {
111         if (aha->cur_outbox == aha->last_outbox)
112                 aha->cur_outbox = aha->out_boxes;
113         else
114                 aha->cur_outbox++;
115 }
116
117 #define ahautoa24(u,s3)                 \
118         (s3)[0] = ((u) >> 16) & 0xff;   \
119         (s3)[1] = ((u) >> 8) & 0xff;    \
120         (s3)[2] = (u) & 0xff;
121
122 #define aha_a24tou(s3) \
123         (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
124
125 /* CCB Management functions */
126 static __inline uint32_t                ahaccbvtop(struct aha_softc *aha,
127                                                   struct aha_ccb *accb);
128 static __inline struct aha_ccb*         ahaccbptov(struct aha_softc *aha,
129                                                   uint32_t ccb_addr);
130
131 static __inline uint32_t
132 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
133 {
134         return (aha->aha_ccb_physbase
135               + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
136 }
137 static __inline struct aha_ccb *
138 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
139 {
140         return (aha->aha_ccb_array +
141               + ((struct aha_ccb*)(uintptr_t)ccb_addr -
142                  (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
143 }
144
145 static struct aha_ccb*  ahagetccb(struct aha_softc *aha);
146 static __inline void    ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
147 static void             ahaallocccbs(struct aha_softc *aha);
148 static bus_dmamap_callback_t ahaexecuteccb;
149 static void             ahadone(struct aha_softc *aha, struct aha_ccb *accb,
150                                aha_mbi_comp_code_t comp_code);
151 static void             aha_intr_locked(struct aha_softc *aha);
152
153 /* Host adapter command functions */
154 static int      ahareset(struct aha_softc* aha, int hard_reset);
155
156 /* Initialization functions */
157 static int                      ahainitmboxes(struct aha_softc *aha);
158 static bus_dmamap_callback_t    ahamapmboxes;
159 static bus_dmamap_callback_t    ahamapccbs;
160 static bus_dmamap_callback_t    ahamapsgs;
161
162 /* Transfer Negotiation Functions */
163 static void ahafetchtransinfo(struct aha_softc *aha,
164                              struct ccb_trans_settings *cts);
165
166 /* CAM SIM entry points */
167 #define ccb_accb_ptr spriv_ptr0
168 #define ccb_aha_ptr spriv_ptr1
169 static void     ahaaction(struct cam_sim *sim, union ccb *ccb);
170 static void     ahapoll(struct cam_sim *sim);
171
172 /* Our timeout handler */
173 static void     ahatimeout(void *arg);
174
175 /* Exported functions */
176 void
177 aha_alloc(struct aha_softc *aha)
178 {
179
180         SLIST_INIT(&aha->free_aha_ccbs);
181         LIST_INIT(&aha->pending_ccbs);
182         SLIST_INIT(&aha->sg_maps);
183         aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
184         aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
185         mtx_init(&aha->lock, "aha", NULL, MTX_DEF);
186 }
187
188 void
189 aha_free(struct aha_softc *aha)
190 {
191         switch (aha->init_level) {
192         default:
193         case 8:
194         {
195                 struct sg_map_node *sg_map;
196
197                 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
198                         SLIST_REMOVE_HEAD(&aha->sg_maps, links);
199                         bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
200                         bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
201                             sg_map->sg_dmamap);
202                         free(sg_map, M_DEVBUF);
203                 }
204                 bus_dma_tag_destroy(aha->sg_dmat);
205         }
206         case 7:
207                 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
208         case 6:
209                 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
210                     aha->ccb_dmamap);
211         case 5:
212                 bus_dma_tag_destroy(aha->ccb_dmat);
213         case 4:
214                 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
215         case 3:
216                 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
217                     aha->mailbox_dmamap);
218         case 2:
219                 bus_dma_tag_destroy(aha->buffer_dmat);
220         case 1:
221                 bus_dma_tag_destroy(aha->mailbox_dmat);
222         case 0:
223                 break;
224         }
225         mtx_destroy(&aha->lock);
226 }
227
228 /*
229  * Probe the adapter and verify that the card is an Adaptec.
230  */
231 int
232 aha_probe(struct aha_softc* aha)
233 {
234         u_int    status;
235         u_int    intstat;
236         int      error;
237         board_id_data_t board_id;
238
239         /*
240          * See if the three I/O ports look reasonable.
241          * Touch the minimal number of registers in the
242          * failure case.
243          */
244         status = aha_inb(aha, STATUS_REG);
245         if ((status == 0) ||
246             (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
247                 PRVERB((aha->dev, "status reg test failed %x\n", status));
248                 return (ENXIO);
249         }
250
251         intstat = aha_inb(aha, INTSTAT_REG);
252         if ((intstat & INTSTAT_REG_RSVD) != 0) {
253                 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
254                 return (ENXIO);
255         }
256
257         /*
258          * Looking good so far.  Final test is to reset the
259          * adapter and fetch the board ID and ensure we aren't
260          * looking at a BusLogic.
261          */
262         if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
263                 PRVERB((aha->dev, "Failed Reset\n"));
264                 return (ENXIO);
265         }
266
267         /*
268          * Get the board ID.  We use this to see if we're dealing with
269          * a buslogic card or an aha card (or clone).
270          */
271         error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
272             (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
273         if (error != 0) {
274                 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
275                 return (ENXIO);
276         }
277         aha->fw_major = board_id.firmware_rev_major;
278         aha->fw_minor = board_id.firmware_rev_minor;
279         aha->boardid = board_id.board_type;
280
281         /*
282          * The Buslogic cards have an id of either 0x41 or 0x42.  So
283          * if those come up in the probe, we test the geometry register
284          * of the board.  Adaptec boards that are this old will not have
285          * this register, and return 0xff, while buslogic cards will return
286          * something different.
287          *
288          * It appears that for reasons unknow, for the for the
289          * aha-1542B cards, we need to wait a little bit before trying
290          * to read the geometry register.  I picked 10ms since we have
291          * reports that a for loop to 1000 did the trick, and this
292          * errs on the side of conservatism.  Besides, no one will
293          * notice a 10mS delay here, even the 1542B card users :-)
294          *
295          * Some compatible cards return 0 here.  Some cards also
296          * seem to return 0x7f.
297          *
298          * XXX I'm not sure how this will impact other cloned cards
299          *
300          * This really should be replaced with the esetup command, since
301          * that appears to be more reliable.  This becomes more and more
302          * true over time as we discover more cards that don't read the
303          * geometry register consistently.
304          */
305         if (aha->boardid <= 0x42) {
306                 /* Wait 10ms before reading */
307                 DELAY(10000);
308                 status = aha_inb(aha, GEOMETRY_REG);
309                 if (status != 0xff && status != 0x00 && status != 0x7f) {
310                         PRVERB((aha->dev, "Geometry Register test failed %#x\n",
311                                 status));
312                         return (ENXIO);
313                 }
314         }
315
316         return (0);
317 }
318
319 /*
320  * Pull the boards setup information and record it in our softc.
321  */
322 int
323 aha_fetch_adapter_info(struct aha_softc *aha)
324 {
325         setup_data_t    setup_info;
326         config_data_t config_data;
327         uint8_t length_param;
328         int      error;
329         struct  aha_extbios extbios;
330
331         switch (aha->boardid) {
332         case BOARD_1540_16HEAD_BIOS:
333                 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
334                 break;
335         case BOARD_1540_64HEAD_BIOS:
336                 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
337                 break;
338         case BOARD_1542:
339                 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
340                 break;
341         case BOARD_1542C:
342                 snprintf(aha->model, sizeof(aha->model), "1542C");
343                 break;
344         case BOARD_1542CF:
345                 snprintf(aha->model, sizeof(aha->model), "1542CF");
346                 break;
347         case BOARD_1542CP:
348                 snprintf(aha->model, sizeof(aha->model), "1542CP");
349                 break;
350         default:
351                 snprintf(aha->model, sizeof(aha->model), "Unknown");
352                 break;
353         }
354         /*
355          * If we are a new type of 1542 board (anything newer than a 1542C)
356          * then disable the extended bios so that the
357          * mailbox interface is unlocked.
358          * This is also true for the 1542B Version 3.20. First Adaptec
359          * board that supports >1Gb drives.
360          * No need to check the extended bios flags as some of the
361          * extensions that cause us problems are not flagged in that byte.
362          */
363         if (PROBABLY_NEW_BOARD(aha->boardid) ||
364                 (aha->boardid == 0x41
365                 && aha->fw_major == 0x31 &&
366                 aha->fw_minor >= 0x34)) {
367                 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
368                     /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
369                     DEFAULT_CMD_TIMEOUT);
370                 if (error != 0) {
371                         device_printf(aha->dev,
372                             "AOP_RETURN_EXT_BIOS_INFO - Failed.");
373                         return (error);
374                 }
375                 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
376                     /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
377                 if (error != 0) {
378                         device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
379                         return (error);
380                 }
381         }
382         if (aha->boardid < 0x41)
383                 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
384
385         aha->max_sg = 17;               /* Need >= 17 to do 64k I/O */
386         aha->diff_bus = 0;
387         aha->extended_lun = 0;
388         aha->extended_trans = 0;
389         aha->max_ccbs = 16;
390         /* Determine Sync/Wide/Disc settings */
391         length_param = sizeof(setup_info);
392         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
393             /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
394             DEFAULT_CMD_TIMEOUT);
395         if (error != 0) {
396                 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
397                     "Get Setup Info\n");
398                 return (error);
399         }
400         if (setup_info.initiate_sync != 0) {
401                 aha->sync_permitted = ALL_TARGETS;
402         }
403         aha->disc_permitted = ALL_TARGETS;
404
405         /* We need as many mailboxes as we can have ccbs */
406         aha->num_boxes = aha->max_ccbs;
407
408         /* Determine our SCSI ID */
409         error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
410             (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
411         if (error != 0) {
412                 device_printf(aha->dev,
413                     "aha_fetch_adapter_info - Failed Get Config\n");
414                 return (error);
415         }
416         aha->scsi_id = config_data.scsi_id;
417         return (0);
418 }
419
420 /*
421  * Start the board, ready for normal operation
422  */
423 int
424 aha_init(struct aha_softc* aha)
425 {
426         /* Announce the Adapter */
427         device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
428             aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
429
430         if (aha->diff_bus != 0)
431                 printf("Diff ");
432
433         printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
434             aha->max_ccbs);
435
436         /*
437          * Create our DMA tags.  These tags define the kinds of device
438          * accessible memory allocations and memory mappings we will
439          * need to perform during normal operation.
440          *
441          * Unless we need to further restrict the allocation, we rely
442          * on the restrictions of the parent dmat, hence the common
443          * use of MAXADDR and MAXSIZE.
444          */
445
446         /* DMA tag for mapping buffers into device visible space. */
447         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
448                                 /* alignment    */ 1,
449                                 /* boundary     */ 0,
450                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
451                                 /* highaddr     */ BUS_SPACE_MAXADDR,
452                                 /* filter       */ NULL,
453                                 /* filterarg    */ NULL,
454                                 /* maxsize      */ DFLTPHYS,
455                                 /* nsegments    */ AHA_NSEG,
456                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
457                                 /* flags        */ BUS_DMA_ALLOCNOW,
458                                 /* lockfunc     */ busdma_lock_mutex,
459                                 /* lockarg      */ &aha->lock,
460                                 &aha->buffer_dmat) != 0) {
461                 goto error_exit;
462         }
463
464         aha->init_level++;
465         /* DMA tag for our mailboxes */
466         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
467                                 /* alignment    */ 1,
468                                 /* boundary     */ 0,
469                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
470                                 /* highaddr     */ BUS_SPACE_MAXADDR,
471                                 /* filter       */ NULL,
472                                 /* filterarg    */ NULL,
473                                 /* maxsize      */ aha->num_boxes *
474                                                    (sizeof(aha_mbox_in_t) +
475                                                     sizeof(aha_mbox_out_t)),
476                                 /* nsegments    */ 1,
477                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
478                                 /* flags        */ 0,
479                                 /* lockfunc     */ NULL,
480                                 /* lockarg      */ NULL,
481                                 &aha->mailbox_dmat) != 0) {
482                 goto error_exit;
483         }
484
485         aha->init_level++;
486
487         /* Allocation for our mailboxes */
488         if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
489             BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
490                 goto error_exit;
491
492         aha->init_level++;
493
494         /* And permanently map them */
495         bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
496             aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
497             sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
498
499         aha->init_level++;
500
501         aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
502
503         ahainitmboxes(aha);
504
505         /* DMA tag for our ccb structures */
506         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
507                                 /* alignment    */ 1,
508                                 /* boundary     */ 0,
509                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
510                                 /* highaddr     */ BUS_SPACE_MAXADDR,
511                                 /* filter       */ NULL,
512                                 /* filterarg    */ NULL,
513                                 /* maxsize      */ aha->max_ccbs *
514                                                    sizeof(struct aha_ccb),
515                                 /* nsegments    */ 1,
516                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
517                                 /* flags        */ 0,
518                                 /* lockfunc     */ NULL,
519                                 /* lockarg      */ NULL,
520                                 &aha->ccb_dmat) != 0) {
521                 goto error_exit;
522         }
523
524         aha->init_level++;
525
526         /* Allocation for our ccbs */
527         if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
528             BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
529                 goto error_exit;
530
531         aha->init_level++;
532
533         /* And permanently map them */
534         bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
535             aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
536
537         aha->init_level++;
538
539         /* DMA tag for our S/G structures.  We allocate in page sized chunks */
540         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
541                                 /* alignment    */ 1,
542                                 /* boundary     */ 0,
543                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
544                                 /* highaddr     */ BUS_SPACE_MAXADDR,
545                                 /* filter       */ NULL,
546                                 /* filterarg    */ NULL,
547                                 /* maxsize      */ PAGE_SIZE,
548                                 /* nsegments    */ 1,
549                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
550                                 /* flags        */ 0,
551                                 /* lockfunc     */ NULL,
552                                 /* lockarg      */ NULL,
553                                 &aha->sg_dmat) != 0)
554                 goto error_exit;
555
556         aha->init_level++;
557
558         /* Perform initial CCB allocation */
559         bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
560         ahaallocccbs(aha);
561
562         if (aha->num_ccbs == 0) {
563                 device_printf(aha->dev,
564                     "aha_init - Unable to allocate initial ccbs\n");
565                 goto error_exit;
566         }
567
568         /*
569          * Note that we are going and return (to probe)
570          */
571         return (0);
572
573 error_exit:
574
575         return (ENXIO);
576 }
577
578 int
579 aha_attach(struct aha_softc *aha)
580 {
581         int tagged_dev_openings;
582         struct cam_devq *devq;
583
584         /*
585          * We don't do tagged queueing, since the aha cards don't
586          * support it.
587          */
588         tagged_dev_openings = 0;
589
590         /*
591          * Create the device queue for our SIM.
592          */
593         devq = cam_simq_alloc(aha->max_ccbs - 1);
594         if (devq == NULL)
595                 return (ENOMEM);
596
597         /*
598          * Construct our SIM entry
599          */
600         aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha,
601             device_get_unit(aha->dev), &aha->lock, 2, tagged_dev_openings,
602             devq);
603         if (aha->sim == NULL) {
604                 cam_simq_free(devq);
605                 return (ENOMEM);
606         }
607         mtx_lock(&aha->lock);
608         if (xpt_bus_register(aha->sim, aha->dev, 0) != CAM_SUCCESS) {
609                 cam_sim_free(aha->sim, /*free_devq*/TRUE);
610                 mtx_unlock(&aha->lock);
611                 return (ENXIO);
612         }
613         if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
614             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
615                 xpt_bus_deregister(cam_sim_path(aha->sim));
616                 cam_sim_free(aha->sim, /*free_devq*/TRUE);
617                 mtx_unlock(&aha->lock);
618                 return (ENXIO);
619         }
620         mtx_unlock(&aha->lock);
621
622         return (0);
623 }
624
625 static void
626 ahaallocccbs(struct aha_softc *aha)
627 {
628         struct aha_ccb *next_ccb;
629         struct sg_map_node *sg_map;
630         bus_addr_t physaddr;
631         aha_sg_t *segs;
632         int newcount;
633         int i;
634
635         next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
636
637         sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
638
639         if (sg_map == NULL)
640                 return;
641
642         /* Allocate S/G space for the next batch of CCBS */
643         if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
644             BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
645                 free(sg_map, M_DEVBUF);
646                 return;
647         }
648
649         SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
650
651         bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
652             PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
653
654         segs = sg_map->sg_vaddr;
655         physaddr = sg_map->sg_physaddr;
656
657         newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
658         for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
659                 int error;
660
661                 next_ccb->sg_list = segs;
662                 next_ccb->sg_list_phys = physaddr;
663                 next_ccb->flags = ACCB_FREE;
664                 callout_init_mtx(&next_ccb->timer, &aha->lock, 0);
665                 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
666                     &next_ccb->dmamap);
667                 if (error != 0)
668                         break;
669                 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
670                 segs += AHA_NSEG;
671                 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
672                 next_ccb++;
673                 aha->num_ccbs++;
674         }
675
676         /* Reserve a CCB for error recovery */
677         if (aha->recovery_accb == NULL) {
678                 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
679                 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
680         }
681 }
682
683 static __inline void
684 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
685 {
686
687         if (!dumping)
688                 mtx_assert(&aha->lock, MA_OWNED);
689         if ((accb->flags & ACCB_ACTIVE) != 0)
690                 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
691         if (aha->resource_shortage != 0
692             && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
693                 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
694                 aha->resource_shortage = FALSE;
695         }
696         accb->flags = ACCB_FREE;
697         SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
698         aha->active_ccbs--;
699 }
700
701 static struct aha_ccb*
702 ahagetccb(struct aha_softc *aha)
703 {
704         struct  aha_ccb* accb;
705
706         if (!dumping)
707                 mtx_assert(&aha->lock, MA_OWNED);
708         if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
709                 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
710                 aha->active_ccbs++;
711         } else if (aha->num_ccbs < aha->max_ccbs) {
712                 ahaallocccbs(aha);
713                 accb = SLIST_FIRST(&aha->free_aha_ccbs);
714                 if (accb == NULL)
715                         device_printf(aha->dev, "Can't malloc ACCB\n");
716                 else {
717                         SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
718                         aha->active_ccbs++;
719                 }
720         }
721
722         return (accb);
723 }
724
725 static void
726 ahaaction(struct cam_sim *sim, union ccb *ccb)
727 {
728         struct  aha_softc *aha;
729
730         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
731
732         aha = (struct aha_softc *)cam_sim_softc(sim);
733         mtx_assert(&aha->lock, MA_OWNED);
734
735         switch (ccb->ccb_h.func_code) {
736         /* Common cases first */
737         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
738         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */        {
739                 struct  aha_ccb *accb;
740                 struct  aha_hccb *hccb;
741
742                 /*
743                  * Get an accb to use.
744                  */
745                 if ((accb = ahagetccb(aha)) == NULL) {
746                         aha->resource_shortage = TRUE;
747                         xpt_freeze_simq(aha->sim, /*count*/1);
748                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
749                         xpt_done(ccb);
750                         return;
751                 }
752                 hccb = &accb->hccb;
753
754                 /*
755                  * So we can find the ACCB when an abort is requested
756                  */
757                 accb->ccb = ccb;
758                 ccb->ccb_h.ccb_accb_ptr = accb;
759                 ccb->ccb_h.ccb_aha_ptr = aha;
760
761                 /*
762                  * Put all the arguments for the xfer in the accb
763                  */
764                 hccb->target = ccb->ccb_h.target_id;
765                 hccb->lun = ccb->ccb_h.target_lun;
766                 hccb->ahastat = 0;
767                 hccb->sdstat = 0;
768
769                 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
770                         struct ccb_scsiio *csio;
771                         struct ccb_hdr *ccbh;
772                         int error;
773
774                         csio = &ccb->csio;
775                         ccbh = &csio->ccb_h;
776                         hccb->opcode = aha->ccb_ccb_opcode;
777                         hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
778                         hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
779                         hccb->cmd_len = csio->cdb_len;
780                         if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
781                                 ccb->ccb_h.status = CAM_REQ_INVALID;
782                                 ahafreeccb(aha, accb);
783                                 xpt_done(ccb);
784                                 return;
785                         }
786                         hccb->sense_len = csio->sense_len;
787                         if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
788                                 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
789                                         bcopy(csio->cdb_io.cdb_ptr,
790                                               hccb->scsi_cdb, hccb->cmd_len);
791                                 } else {
792                                         /* I guess I could map it in... */
793                                         ccbh->status = CAM_REQ_INVALID;
794                                         ahafreeccb(aha, accb);
795                                         xpt_done(ccb);
796                                         return;
797                                 }
798                         } else {
799                                 bcopy(csio->cdb_io.cdb_bytes,
800                                       hccb->scsi_cdb, hccb->cmd_len);
801                         }
802                         /*
803                          * If we have any data to send with this command,
804                          * map it into bus space.
805                          */
806
807                         error = bus_dmamap_load_ccb(
808                             aha->buffer_dmat,
809                             accb->dmamap,
810                             ccb,
811                             ahaexecuteccb,
812                             accb,
813                             /*flags*/0);
814                         if (error == EINPROGRESS) {
815                                 /*
816                                  * So as to maintain ordering, freeze the
817                                  * controller queue until our mapping is
818                                  * returned.
819                                  */
820                                 xpt_freeze_simq(aha->sim, 1);
821                                 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
822                         }
823                 } else {
824                         hccb->opcode = INITIATOR_BUS_DEV_RESET;
825                         /* No data transfer */
826                         hccb->datain = TRUE;
827                         hccb->dataout = TRUE;
828                         hccb->cmd_len = 0;
829                         hccb->sense_len = 0;
830                         ahaexecuteccb(accb, NULL, 0, 0);
831                 }
832                 break;
833         }
834         case XPT_EN_LUN:                /* Enable LUN as a target */
835         case XPT_TARGET_IO:             /* Execute target I/O request */
836         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
837         case XPT_CONT_TARGET_IO:        /* Continue Host Target I/O Connection*/
838         case XPT_ABORT:                 /* Abort the specified CCB */
839                 /* XXX Implement */
840                 ccb->ccb_h.status = CAM_REQ_INVALID;
841                 xpt_done(ccb);
842                 break;
843         case XPT_SET_TRAN_SETTINGS:
844                 /* XXX Implement */
845                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
846                 xpt_done(ccb);
847                 break;
848         case XPT_GET_TRAN_SETTINGS:
849         /* Get default/user set transfer settings for the target */
850         {
851                 struct  ccb_trans_settings *cts = &ccb->cts;
852                 u_int   target_mask = 0x01 << ccb->ccb_h.target_id;
853                 struct ccb_trans_settings_scsi *scsi =
854                     &cts->proto_specific.scsi;
855                 struct ccb_trans_settings_spi *spi =
856                     &cts->xport_specific.spi;
857
858                 cts->protocol = PROTO_SCSI;
859                 cts->protocol_version = SCSI_REV_2;
860                 cts->transport = XPORT_SPI;
861                 cts->transport_version = 2;
862                 if (cts->type == CTS_TYPE_USER_SETTINGS) {
863                         spi->flags = 0;
864                         if ((aha->disc_permitted & target_mask) != 0)
865                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
866                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
867                         if ((aha->sync_permitted & target_mask) != 0) {
868                                 if (aha->boardid >= BOARD_1542CF)
869                                         spi->sync_period = 25;
870                                 else
871                                         spi->sync_period = 50;
872                         } else {
873                                 spi->sync_period = 0;
874                         }
875
876                         if (spi->sync_period != 0)
877                                 spi->sync_offset = 15;
878
879                         spi->valid = CTS_SPI_VALID_SYNC_RATE
880                                    | CTS_SPI_VALID_SYNC_OFFSET
881                                    | CTS_SPI_VALID_BUS_WIDTH
882                                    | CTS_SPI_VALID_DISC;
883                         scsi->valid = CTS_SCSI_VALID_TQ;
884                 } else {
885                         ahafetchtransinfo(aha, cts);
886                 }
887
888                 ccb->ccb_h.status = CAM_REQ_CMP;
889                 xpt_done(ccb);
890                 break;
891         }
892         case XPT_CALC_GEOMETRY:
893         {
894                 struct    ccb_calc_geometry *ccg;
895                 uint32_t size_mb;
896                 uint32_t secs_per_cylinder;
897
898                 ccg = &ccb->ccg;
899                 size_mb = ccg->volume_size
900                         / ((1024L * 1024L) / ccg->block_size);
901                 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
902                         if (size_mb >= 2048) {
903                                 ccg->heads = 255;
904                                 ccg->secs_per_track = 63;
905                         } else {
906                                 ccg->heads = 128;
907                                 ccg->secs_per_track = 32;
908                         }
909                 } else {
910                         ccg->heads = 64;
911                         ccg->secs_per_track = 32;
912                 }
913                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
914                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
915                 ccb->ccb_h.status = CAM_REQ_CMP;
916                 xpt_done(ccb);
917                 break;
918         }
919         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
920                 ahareset(aha, /*hardreset*/TRUE);
921                 ccb->ccb_h.status = CAM_REQ_CMP;
922                 xpt_done(ccb);
923                 break;
924         case XPT_TERM_IO:               /* Terminate the I/O process */
925                 /* XXX Implement */
926                 ccb->ccb_h.status = CAM_REQ_INVALID;
927                 xpt_done(ccb);
928                 break;
929         case XPT_PATH_INQ:              /* Path routing inquiry */
930         {
931                 struct ccb_pathinq *cpi = &ccb->cpi;
932
933                 cpi->version_num = 1; /* XXX??? */
934                 cpi->hba_inquiry = PI_SDTR_ABLE;
935                 cpi->target_sprt = 0;
936                 cpi->hba_misc = 0;
937                 cpi->hba_eng_cnt = 0;
938                 cpi->max_target = 7;
939                 cpi->max_lun = 7;
940                 cpi->initiator_id = aha->scsi_id;
941                 cpi->bus_id = cam_sim_bus(sim);
942                 cpi->base_transfer_speed = 3300;
943                 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
944                 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
945                 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
946                 cpi->unit_number = cam_sim_unit(sim);
947                 cpi->transport = XPORT_SPI;
948                 cpi->transport_version = 2;
949                 cpi->protocol = PROTO_SCSI;
950                 cpi->protocol_version = SCSI_REV_2;
951                 cpi->ccb_h.status = CAM_REQ_CMP;
952                 xpt_done(ccb);
953                 break;
954         }
955         default:
956                 ccb->ccb_h.status = CAM_REQ_INVALID;
957                 xpt_done(ccb);
958                 break;
959         }
960 }
961
962 static void
963 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
964 {
965         struct   aha_ccb *accb;
966         union    ccb *ccb;
967         struct   aha_softc *aha;
968         uint32_t paddr;
969
970         accb = (struct aha_ccb *)arg;
971         ccb = accb->ccb;
972         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
973
974         if (error != 0) {
975                 if (error != EFBIG)
976                         device_printf(aha->dev,
977                             "Unexepected error 0x%x returned from "
978                             "bus_dmamap_load\n", error);
979                 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
980                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
981                         ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
982                 }
983                 ahafreeccb(aha, accb);
984                 xpt_done(ccb);
985                 return;
986         }
987
988         if (nseg != 0) {
989                 aha_sg_t *sg;
990                 bus_dma_segment_t *end_seg;
991                 bus_dmasync_op_t op;
992
993                 end_seg = dm_segs + nseg;
994
995                 /* Copy the segments into our SG list */
996                 sg = accb->sg_list;
997                 while (dm_segs < end_seg) {
998                         ahautoa24(dm_segs->ds_len, sg->len);
999                         ahautoa24(dm_segs->ds_addr, sg->addr);
1000                         sg++;
1001                         dm_segs++;
1002                 }
1003
1004                 if (nseg > 1) {
1005                         accb->hccb.opcode = aha->ccb_sg_opcode;
1006                         ahautoa24((sizeof(aha_sg_t) * nseg),
1007                             accb->hccb.data_len);
1008                         ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1009                 } else {
1010                         bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1011                         bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1012                 }
1013
1014                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1015                         op = BUS_DMASYNC_PREREAD;
1016                 else
1017                         op = BUS_DMASYNC_PREWRITE;
1018
1019                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1020
1021         } else {
1022                 accb->hccb.opcode = INITIATOR_CCB;
1023                 ahautoa24(0, accb->hccb.data_len);
1024                 ahautoa24(0, accb->hccb.data_addr);
1025         }
1026
1027         /*
1028          * Last time we need to check if this CCB needs to
1029          * be aborted.
1030          */
1031         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1032                 if (nseg != 0)
1033                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1034                 ahafreeccb(aha, accb);
1035                 xpt_done(ccb);
1036                 return;
1037         }
1038
1039         accb->flags = ACCB_ACTIVE;
1040         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1041         LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1042
1043         callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1044             ahatimeout, accb, 0);
1045
1046         /* Tell the adapter about this command */
1047         if (aha->cur_outbox->action_code != AMBO_FREE) {
1048                 /*
1049                  * We should never encounter a busy mailbox.
1050                  * If we do, warn the user, and treat it as
1051                  * a resource shortage.  If the controller is
1052                  * hung, one of the pending transactions will
1053                  * timeout causing us to start recovery operations.
1054                  */
1055                 device_printf(aha->dev,
1056                     "Encountered busy mailbox with %d out of %d "
1057                     "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1058                 callout_stop(&accb->timer);
1059                 if (nseg != 0)
1060                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1061                 ahafreeccb(aha, accb);
1062                 aha->resource_shortage = TRUE;
1063                 xpt_freeze_simq(aha->sim, /*count*/1);
1064                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1065                 xpt_done(ccb);
1066                 return;
1067         }
1068         paddr = ahaccbvtop(aha, accb);
1069         ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1070         aha->cur_outbox->action_code = AMBO_START;
1071         aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1072
1073         ahanextoutbox(aha);
1074 }
1075
1076 void
1077 aha_intr(void *arg)
1078 {
1079         struct  aha_softc *aha;
1080
1081         aha = arg;
1082         mtx_lock(&aha->lock);
1083         aha_intr_locked(aha);
1084         mtx_unlock(&aha->lock);
1085 }
1086
1087 void
1088 aha_intr_locked(struct aha_softc *aha)
1089 {
1090         u_int   intstat;
1091         uint32_t paddr;
1092
1093         while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1094                 if ((intstat & CMD_COMPLETE) != 0) {
1095                         aha->latched_status = aha_inb(aha, STATUS_REG);
1096                         aha->command_cmp = TRUE;
1097                 }
1098
1099                 aha_outb(aha, CONTROL_REG, RESET_INTR);
1100
1101                 if ((intstat & IMB_LOADED) != 0) {
1102                         while (aha->cur_inbox->comp_code != AMBI_FREE) {
1103                                 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1104                                 ahadone(aha, ahaccbptov(aha, paddr),
1105                                     aha->cur_inbox->comp_code);
1106                                 aha->cur_inbox->comp_code = AMBI_FREE;
1107                                 ahanextinbox(aha);
1108                         }
1109                 }
1110
1111                 if ((intstat & SCSI_BUS_RESET) != 0) {
1112                         ahareset(aha, /*hardreset*/FALSE);
1113                 }
1114         }
1115 }
1116
1117 static void
1118 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1119 {
1120         union  ccb        *ccb;
1121         struct ccb_scsiio *csio;
1122
1123         ccb = accb->ccb;
1124         csio = &accb->ccb->csio;
1125
1126         if ((accb->flags & ACCB_ACTIVE) == 0) {
1127                 device_printf(aha->dev, 
1128                     "ahadone - Attempt to free non-active ACCB %p\n",
1129                     (void *)accb);
1130                 return;
1131         }
1132
1133         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1134                 bus_dmasync_op_t op;
1135
1136                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1137                         op = BUS_DMASYNC_POSTREAD;
1138                 else
1139                         op = BUS_DMASYNC_POSTWRITE;
1140                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1141                 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1142         }
1143
1144         if (accb == aha->recovery_accb) {
1145                 /*
1146                  * The recovery ACCB does not have a CCB associated
1147                  * with it, so short circuit the normal error handling.
1148                  * We now traverse our list of pending CCBs and process
1149                  * any that were terminated by the recovery CCBs action.
1150                  * We also reinstate timeouts for all remaining, pending,
1151                  * CCBs.
1152                  */
1153                 struct cam_path *path;
1154                 struct ccb_hdr *ccb_h;
1155                 cam_status error;
1156
1157                 /* Notify all clients that a BDR occurred */
1158                 error = xpt_create_path(&path, /*periph*/NULL,
1159                     cam_sim_path(aha->sim), accb->hccb.target,
1160                     CAM_LUN_WILDCARD);
1161
1162                 if (error == CAM_REQ_CMP) {
1163                         xpt_async(AC_SENT_BDR, path, NULL);
1164                         xpt_free_path(path);
1165                 }
1166
1167                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1168                 while (ccb_h != NULL) {
1169                         struct aha_ccb *pending_accb;
1170
1171                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1172                         if (pending_accb->hccb.target == accb->hccb.target) {
1173                                 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1174                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1175                                 ahadone(aha, pending_accb, AMBI_ERROR);
1176                         } else {
1177                                 callout_reset_sbt(&pending_accb->timer,
1178                                     SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1179                                     pending_accb, 0);
1180                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1181                         }
1182                 }
1183                 device_printf(aha->dev, "No longer in timeout\n");
1184                 return;
1185         }
1186
1187         callout_stop(&accb->timer);
1188
1189         switch (comp_code) {
1190         case AMBI_FREE:
1191                 device_printf(aha->dev,
1192                     "ahadone - CCB completed with free status!\n");
1193                 break;
1194         case AMBI_NOT_FOUND:
1195                 device_printf(aha->dev,
1196                     "ahadone - CCB Abort failed to find CCB\n");
1197                 break;
1198         case AMBI_ABORT:
1199         case AMBI_ERROR:
1200                 /* An error occurred */
1201                 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1202                         csio->resid = 0;
1203                 else
1204                         csio->resid = aha_a24tou(accb->hccb.data_len);
1205                 switch(accb->hccb.ahastat) {
1206                 case AHASTAT_DATARUN_ERROR:
1207                 {
1208                         if (csio->resid <= 0) {
1209                                 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1210                                 break;
1211                         }
1212                         /* FALLTHROUGH */
1213                 }
1214                 case AHASTAT_NOERROR:
1215                         csio->scsi_status = accb->hccb.sdstat;
1216                         csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1217                         switch(csio->scsi_status) {
1218                         case SCSI_STATUS_CHECK_COND:
1219                         case SCSI_STATUS_CMD_TERMINATED:
1220                                 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1221                                 /*
1222                                  * The aha writes the sense data at different
1223                                  * offsets based on the scsi cmd len
1224                                  */
1225                                 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1226                                     accb->hccb.cmd_len,
1227                                     (caddr_t) &csio->sense_data,
1228                                     accb->hccb.sense_len);
1229                                 break;
1230                         default:
1231                                 break;
1232                         case SCSI_STATUS_OK:
1233                                 csio->ccb_h.status = CAM_REQ_CMP;
1234                                 break;
1235                         }
1236                         break;
1237                 case AHASTAT_SELTIMEOUT:
1238                         csio->ccb_h.status = CAM_SEL_TIMEOUT;
1239                         break;
1240                 case AHASTAT_UNEXPECTED_BUSFREE:
1241                         csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1242                         break;
1243                 case AHASTAT_INVALID_PHASE:
1244                         csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1245                         break;
1246                 case AHASTAT_INVALID_ACTION_CODE:
1247                         panic("%s: Inavlid Action code", aha_name(aha));
1248                         break;
1249                 case AHASTAT_INVALID_OPCODE:
1250                         if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1251                                 panic("%s: Invalid CCB Opcode %x hccb = %p",
1252                                     aha_name(aha), accb->hccb.opcode,
1253                                     &accb->hccb);
1254                         device_printf(aha->dev,
1255                             "AHA-1540A compensation failed\n");
1256                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1257                         csio->ccb_h.status = CAM_REQUEUE_REQ;
1258                         break;
1259                 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1260                         /* We don't even support linked commands... */
1261                         panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1262                         break;
1263                 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1264                         panic("%s: Invalid CCB or SG list", aha_name(aha));
1265                         break;
1266                 case AHASTAT_HA_SCSI_BUS_RESET:
1267                         if ((csio->ccb_h.status & CAM_STATUS_MASK)
1268                             != CAM_CMD_TIMEOUT)
1269                                 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1270                         break;
1271                 case AHASTAT_HA_BDR:
1272                         if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1273                                 csio->ccb_h.status = CAM_BDR_SENT;
1274                         else
1275                                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1276                         break;
1277                 }
1278                 if (csio->ccb_h.status != CAM_REQ_CMP) {
1279                         xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1280                         csio->ccb_h.status |= CAM_DEV_QFRZN;
1281                 }
1282                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1283                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1284                 ahafreeccb(aha, accb);
1285                 xpt_done(ccb);
1286                 break;
1287         case AMBI_OK:
1288                 /* All completed without incident */
1289                 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1290                 /* I don't think so since it works???? */
1291                 ccb->ccb_h.status |= CAM_REQ_CMP;
1292                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1293                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1294                 ahafreeccb(aha, accb);
1295                 xpt_done(ccb);
1296                 break;
1297         }
1298 }
1299
1300 static int
1301 ahareset(struct aha_softc* aha, int hard_reset)
1302 {
1303         struct   ccb_hdr *ccb_h;
1304         u_int    status;
1305         u_int    timeout;
1306         uint8_t reset_type;
1307
1308         if (hard_reset != 0)
1309                 reset_type = HARD_RESET;
1310         else
1311                 reset_type = SOFT_RESET;
1312         aha_outb(aha, CONTROL_REG, reset_type);
1313
1314         /* Wait 5sec. for Diagnostic start */
1315         timeout = 5 * 10000;
1316         while (--timeout) {
1317                 status = aha_inb(aha, STATUS_REG);
1318                 if ((status & DIAG_ACTIVE) != 0)
1319                         break;
1320                 DELAY(100);
1321         }
1322         if (timeout == 0) {
1323                 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1324                     "assert. status = %#x\n", status));
1325                 return (ETIMEDOUT);
1326         }
1327
1328         /* Wait 10sec. for Diagnostic end */
1329         timeout = 10 * 10000;
1330         while (--timeout) {
1331                 status = aha_inb(aha, STATUS_REG);
1332                 if ((status & DIAG_ACTIVE) == 0)
1333                         break;
1334                 DELAY(100);
1335         }
1336         if (timeout == 0) {
1337                 panic("%s: ahareset - Diagnostic Active failed to drop. "
1338                     "status = 0x%x\n", aha_name(aha), status);
1339                 return (ETIMEDOUT);
1340         }
1341
1342         /* Wait for the host adapter to become ready or report a failure */
1343         timeout = 10000;
1344         while (--timeout) {
1345                 status = aha_inb(aha, STATUS_REG);
1346                 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1347                         break;
1348                 DELAY(100);
1349         }
1350         if (timeout == 0) {
1351                 device_printf(aha->dev, "ahareset - Host adapter failed to "
1352                     "come ready. status = 0x%x\n", status);
1353                 return (ETIMEDOUT);
1354         }
1355
1356         /* If the diagnostics failed, tell the user */
1357         if ((status & DIAG_FAIL) != 0
1358          || (status & HA_READY) == 0) {
1359                 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1360
1361                 if ((status & DATAIN_REG_READY) != 0)
1362                         device_printf(aha->dev, "ahareset - Host Adapter "
1363                             "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1364                 return (ENXIO);
1365         }
1366
1367         /* If we've attached to the XPT, tell it about the event */
1368         if (aha->path != NULL)
1369                 xpt_async(AC_BUS_RESET, aha->path, NULL);
1370
1371         /*
1372          * Perform completion processing for all outstanding CCBs.
1373          */
1374         while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1375                 struct aha_ccb *pending_accb;
1376
1377                 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1378                 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1379                 ahadone(aha, pending_accb, AMBI_ERROR);
1380         }
1381
1382         /* If we've allocated mailboxes, initialize them */
1383         /* Must be done after we've aborted our queue, or aha_cmd fails */
1384         if (aha->init_level > 4)
1385                 ahainitmboxes(aha);
1386
1387         return (0);
1388 }
1389
1390 /*
1391  * Send a command to the adapter.
1392  */
1393 int
1394 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1395         u_int param_len, uint8_t *reply_data, u_int reply_len,
1396         u_int cmd_timeout)
1397 {
1398         u_int   timeout;
1399         u_int   status;
1400         u_int   saved_status;
1401         u_int   intstat;
1402         u_int   reply_buf_size;
1403         int     cmd_complete;
1404         int     error;
1405
1406         /* No data returned to start */
1407         reply_buf_size = reply_len;
1408         reply_len = 0;
1409         intstat = 0;
1410         cmd_complete = 0;
1411         saved_status = 0;
1412         error = 0;
1413
1414         /*
1415          * All commands except for the "start mailbox" and the "enable
1416          * outgoing mailbox read interrupt" commands cannot be issued
1417          * while there are pending transactions.  Freeze our SIMQ
1418          * and wait for all completions to occur if necessary.
1419          */
1420         timeout = 10000;
1421         while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1422                 /* Fire the interrupt handler in case interrupts are blocked */
1423                 aha_intr(aha);
1424                 DELAY(10);
1425         }
1426
1427         if (timeout == 0) {
1428                 device_printf(aha->dev, 
1429                     "aha_cmd: Timeout waiting for adapter idle\n");
1430                 return (ETIMEDOUT);
1431         }
1432         aha->command_cmp = 0;
1433         /*
1434          * Wait up to 10 sec. for the adapter to become
1435          * ready to accept commands.
1436          */
1437         timeout = 100000;
1438         while (--timeout) {
1439                 status = aha_inb(aha, STATUS_REG);
1440                 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1441                         break;
1442                 /*
1443                  * Throw away any pending data which may be
1444                  * left over from earlier commands that we
1445                  * timedout on.
1446                  */
1447                 if ((status & DATAIN_REG_READY) != 0)
1448                         (void)aha_inb(aha, DATAIN_REG);
1449                 DELAY(100);
1450         }
1451         if (timeout == 0) {
1452                 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1453                     " ready, status = 0x%x\n", status);
1454                 return (ETIMEDOUT);
1455         }
1456
1457         /*
1458          * Send the opcode followed by any necessary parameter bytes.
1459          */
1460         aha_outb(aha, COMMAND_REG, opcode);
1461
1462         /*
1463          * Wait for up to 1sec to get the parameter list sent
1464          */
1465         timeout = 10000;
1466         while (param_len && --timeout) {
1467                 DELAY(100);
1468                 status = aha_inb(aha, STATUS_REG);
1469                 intstat = aha_inb(aha, INTSTAT_REG);
1470
1471                 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1472                  == (INTR_PENDING|CMD_COMPLETE)) {
1473                         saved_status = status;
1474                         cmd_complete = 1;
1475                         break;
1476                 }
1477
1478                 if (aha->command_cmp != 0) {
1479                         saved_status = aha->latched_status;
1480                         cmd_complete = 1;
1481                         break;
1482                 }
1483                 if ((status & DATAIN_REG_READY) != 0)
1484                         break;
1485                 if ((status & CMD_REG_BUSY) == 0) {
1486                         aha_outb(aha, COMMAND_REG, *params++);
1487                         param_len--;
1488                         timeout = 10000;
1489                 }
1490         }
1491         if (timeout == 0) {
1492                 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1493                     "status = 0x%x\n", status);
1494                 error = ETIMEDOUT;
1495         }
1496
1497         /*
1498          * For all other commands, we wait for any output data
1499          * and the final comand completion interrupt.
1500          */
1501         while (cmd_complete == 0 && --cmd_timeout) {
1502
1503                 status = aha_inb(aha, STATUS_REG);
1504                 intstat = aha_inb(aha, INTSTAT_REG);
1505
1506                 if (aha->command_cmp != 0) {
1507                         cmd_complete = 1;
1508                         saved_status = aha->latched_status;
1509                 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1510                         == (INTR_PENDING|CMD_COMPLETE)) {
1511                         /*
1512                          * Our poll (in case interrupts are blocked)
1513                          * saw the CMD_COMPLETE interrupt.
1514                          */
1515                         cmd_complete = 1;
1516                         saved_status = status;
1517                 }
1518                 if ((status & DATAIN_REG_READY) != 0) {
1519                         uint8_t data;
1520
1521                         data = aha_inb(aha, DATAIN_REG);
1522                         if (reply_len < reply_buf_size) {
1523                                 *reply_data++ = data;
1524                         } else {
1525                                 device_printf(aha->dev, 
1526                                     "aha_cmd - Discarded reply data "
1527                                     "byte for opcode 0x%x\n", opcode);
1528                         }
1529                         /*
1530                          * Reset timeout to ensure at least a second
1531                          * between response bytes.
1532                          */
1533                         cmd_timeout = MAX(cmd_timeout, 10000);
1534                         reply_len++;
1535                 }
1536                 DELAY(100);
1537         }
1538         if (cmd_timeout == 0) {
1539                 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1540                     "intstat = 0x%x, reply_len = %d\n", status, intstat,
1541                     reply_len);
1542                 return (ETIMEDOUT);
1543         }
1544
1545         /*
1546          * Clear any pending interrupts.  Block interrupts so our
1547          * interrupt handler is not re-entered.
1548          */
1549         aha_intr(aha);
1550
1551         if (error != 0)
1552                 return (error);
1553
1554         /*
1555          * If the command was rejected by the controller, tell the caller.
1556          */
1557         if ((saved_status & CMD_INVALID) != 0) {
1558                 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1559                 /*
1560                  * Some early adapters may not recover properly from
1561                  * an invalid command.  If it appears that the controller
1562                  * has wedged (i.e. status was not cleared by our interrupt
1563                  * reset above), perform a soft reset.
1564                  */
1565                 DELAY(1000);
1566                 status = aha_inb(aha, STATUS_REG);
1567                 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1568                               CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1569                  || (status & (HA_READY|INIT_REQUIRED))
1570                   != (HA_READY|INIT_REQUIRED))
1571                         ahareset(aha, /*hard_reset*/FALSE);
1572                 return (EINVAL);
1573         }
1574
1575         if (param_len > 0) {
1576                 /* The controller did not accept the full argument list */
1577                 PRVERB((aha->dev, "Controller did not accept full argument "
1578                     "list (%d > 0)\n", param_len));
1579                 return (E2BIG);
1580         }
1581
1582         if (reply_len != reply_buf_size) {
1583                 /* Too much or too little data received */
1584                 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1585                     reply_len, reply_buf_size));
1586                 return (EMSGSIZE);
1587         }
1588
1589         /* We were successful */
1590         return (0);
1591 }
1592
1593 static int
1594 ahainitmboxes(struct aha_softc *aha)
1595 {
1596         int error;
1597         init_24b_mbox_params_t init_mbox;
1598
1599         bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1600         bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1601         aha->cur_inbox = aha->in_boxes;
1602         aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1603         aha->cur_outbox = aha->out_boxes;
1604         aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1605
1606         /* Tell the adapter about them */
1607         init_mbox.num_mboxes = aha->num_boxes;
1608         ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1609         error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1610             /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1611             /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1612
1613         if (error != 0)
1614                 printf("ahainitmboxes: Initialization command failed\n");
1615         return (error);
1616 }
1617
1618 /*
1619  * Update the XPT's idea of the negotiated transfer
1620  * parameters for a particular target.
1621  */
1622 static void
1623 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1624 {
1625         setup_data_t    setup_info;
1626         u_int           target;
1627         u_int           targ_offset;
1628         u_int           sync_period;
1629         int             error;
1630         uint8_t param;
1631         targ_syncinfo_t sync_info;
1632         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1633
1634         target = cts->ccb_h.target_id;
1635         targ_offset = (target & 0x7);
1636
1637         /*
1638          * Inquire Setup Information.  This command retreives
1639          * the sync info for older models.
1640          */
1641         param = sizeof(setup_info);
1642         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
1643             (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1644
1645         if (error != 0) {
1646                 device_printf(aha->dev,
1647                     "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1648                     error);
1649                 return;
1650         }
1651
1652         sync_info = setup_info.syncinfo[targ_offset];
1653
1654         if (sync_info.sync == 0)
1655                 spi->sync_offset = 0;
1656         else
1657                 spi->sync_offset = sync_info.offset;
1658
1659         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1660
1661         if (aha->boardid >= BOARD_1542CF)
1662                 sync_period = 1000;
1663         else
1664                 sync_period = 2000;
1665         sync_period += 500 * sync_info.period;
1666
1667         /* Convert ns value to standard SCSI sync rate */
1668         if (spi->sync_offset != 0)
1669                 spi->sync_period = scsi_calc_syncparam(sync_period);
1670         else
1671                 spi->sync_period = 0;
1672
1673         spi->valid = CTS_SPI_VALID_SYNC_RATE
1674                    | CTS_SPI_VALID_SYNC_OFFSET
1675                    | CTS_SPI_VALID_BUS_WIDTH;
1676         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1677 }
1678
1679 static void
1680 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1681 {
1682         struct aha_softc* aha;
1683
1684         aha = (struct aha_softc*)arg;
1685         aha->mailbox_physbase = segs->ds_addr;
1686 }
1687
1688 static void
1689 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1690 {
1691         struct aha_softc* aha;
1692
1693         aha = (struct aha_softc*)arg;
1694         aha->aha_ccb_physbase = segs->ds_addr;
1695 }
1696
1697 static void
1698 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1699 {
1700
1701         struct aha_softc* aha;
1702
1703         aha = (struct aha_softc*)arg;
1704         SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1705 }
1706
1707 static void
1708 ahapoll(struct cam_sim *sim)
1709 {
1710         aha_intr_locked(cam_sim_softc(sim));
1711 }
1712
1713 static void
1714 ahatimeout(void *arg)
1715 {
1716         struct aha_ccb  *accb;
1717         union  ccb      *ccb;
1718         struct aha_softc *aha;
1719         uint32_t        paddr;
1720         struct ccb_hdr *ccb_h;
1721
1722         accb = (struct aha_ccb *)arg;
1723         ccb = accb->ccb;
1724         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1725         mtx_assert(&aha->lock, MA_OWNED);
1726         xpt_print_path(ccb->ccb_h.path);
1727         printf("CCB %p - timed out\n", (void *)accb);
1728
1729         if ((accb->flags & ACCB_ACTIVE) == 0) {
1730                 xpt_print_path(ccb->ccb_h.path);
1731                 printf("CCB %p - timed out CCB already completed\n",
1732                     (void *)accb);
1733                 return;
1734         }
1735
1736         /*
1737          * In order to simplify the recovery process, we ask the XPT
1738          * layer to halt the queue of new transactions and we traverse
1739          * the list of pending CCBs and remove their timeouts. This
1740          * means that the driver attempts to clear only one error
1741          * condition at a time.  In general, timeouts that occur
1742          * close together are related anyway, so there is no benefit
1743          * in attempting to handle errors in parallel.  Timeouts will
1744          * be reinstated when the recovery process ends.
1745          */
1746         if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1747                 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1748                         xpt_freeze_simq(aha->sim, /*count*/1);
1749                         accb->flags |= ACCB_RELEASE_SIMQ;
1750                 }
1751
1752                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1753                 while (ccb_h != NULL) {
1754                         struct aha_ccb *pending_accb;
1755
1756                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1757                         callout_stop(&pending_accb->timer);
1758                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1759                 }
1760         }
1761
1762         if ((accb->flags & ACCB_DEVICE_RESET) != 0
1763          || aha->cur_outbox->action_code != AMBO_FREE) {
1764                 /*
1765                  * Try a full host adapter/SCSI bus reset.
1766                  * We do this only if we have already attempted
1767                  * to clear the condition with a BDR, or we cannot
1768                  * attempt a BDR for lack of mailbox resources.
1769                  */
1770                 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1771                 ahareset(aha, /*hardreset*/TRUE);
1772                 device_printf(aha->dev, "No longer in timeout\n");
1773         } else {
1774                 /*
1775                  * Send a Bus Device Reset message:
1776                  * The target that is holding up the bus may not
1777                  * be the same as the one that triggered this timeout
1778                  * (different commands have different timeout lengths),
1779                  * but we have no way of determining this from our
1780                  * timeout handler.  Our strategy here is to queue a
1781                  * BDR message to the target of the timed out command.
1782                  * If this fails, we'll get another timeout 2 seconds
1783                  * later which will attempt a bus reset.
1784                  */
1785                 accb->flags |= ACCB_DEVICE_RESET;
1786                 callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1787                 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1788
1789                 /* No Data Transfer */
1790                 aha->recovery_accb->hccb.datain = TRUE;
1791                 aha->recovery_accb->hccb.dataout = TRUE;
1792                 aha->recovery_accb->hccb.ahastat = 0;
1793                 aha->recovery_accb->hccb.sdstat = 0;
1794                 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1795
1796                 /* Tell the adapter about this command */
1797                 paddr = ahaccbvtop(aha, aha->recovery_accb);
1798                 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1799                 aha->cur_outbox->action_code = AMBO_START;
1800                 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1801                 ahanextoutbox(aha);
1802         }
1803 }
1804
1805 int
1806 aha_detach(struct aha_softc *aha)
1807 {
1808         mtx_lock(&aha->lock);
1809         xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1810         xpt_free_path(aha->path);
1811         xpt_bus_deregister(cam_sim_path(aha->sim));
1812         cam_sim_free(aha->sim, /*free_devq*/TRUE);
1813         mtx_unlock(&aha->lock);
1814         /* XXX: Drain all timers? */
1815         return (0);
1816 }
1817 MODULE_DEPEND(aha, cam, 1, 1, 1);