]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/aha/aha.c
Import tzdata 2017c
[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_ABORT:                 /* Abort the specified CCB */
835                 /* XXX Implement */
836                 ccb->ccb_h.status = CAM_REQ_INVALID;
837                 xpt_done(ccb);
838                 break;
839         case XPT_SET_TRAN_SETTINGS:
840                 /* XXX Implement */
841                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
842                 xpt_done(ccb);
843                 break;
844         case XPT_GET_TRAN_SETTINGS:
845         /* Get default/user set transfer settings for the target */
846         {
847                 struct  ccb_trans_settings *cts = &ccb->cts;
848                 u_int   target_mask = 0x01 << ccb->ccb_h.target_id;
849                 struct ccb_trans_settings_scsi *scsi =
850                     &cts->proto_specific.scsi;
851                 struct ccb_trans_settings_spi *spi =
852                     &cts->xport_specific.spi;
853
854                 cts->protocol = PROTO_SCSI;
855                 cts->protocol_version = SCSI_REV_2;
856                 cts->transport = XPORT_SPI;
857                 cts->transport_version = 2;
858                 if (cts->type == CTS_TYPE_USER_SETTINGS) {
859                         spi->flags = 0;
860                         if ((aha->disc_permitted & target_mask) != 0)
861                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
862                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
863                         if ((aha->sync_permitted & target_mask) != 0) {
864                                 if (aha->boardid >= BOARD_1542CF)
865                                         spi->sync_period = 25;
866                                 else
867                                         spi->sync_period = 50;
868                         } else {
869                                 spi->sync_period = 0;
870                         }
871
872                         if (spi->sync_period != 0)
873                                 spi->sync_offset = 15;
874
875                         spi->valid = CTS_SPI_VALID_SYNC_RATE
876                                    | CTS_SPI_VALID_SYNC_OFFSET
877                                    | CTS_SPI_VALID_BUS_WIDTH
878                                    | CTS_SPI_VALID_DISC;
879                         scsi->valid = CTS_SCSI_VALID_TQ;
880                 } else {
881                         ahafetchtransinfo(aha, cts);
882                 }
883
884                 ccb->ccb_h.status = CAM_REQ_CMP;
885                 xpt_done(ccb);
886                 break;
887         }
888         case XPT_CALC_GEOMETRY:
889         {
890                 struct    ccb_calc_geometry *ccg;
891                 uint32_t size_mb;
892                 uint32_t secs_per_cylinder;
893
894                 ccg = &ccb->ccg;
895                 size_mb = ccg->volume_size
896                         / ((1024L * 1024L) / ccg->block_size);
897                 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
898                         if (size_mb >= 2048) {
899                                 ccg->heads = 255;
900                                 ccg->secs_per_track = 63;
901                         } else {
902                                 ccg->heads = 128;
903                                 ccg->secs_per_track = 32;
904                         }
905                 } else {
906                         ccg->heads = 64;
907                         ccg->secs_per_track = 32;
908                 }
909                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
910                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
911                 ccb->ccb_h.status = CAM_REQ_CMP;
912                 xpt_done(ccb);
913                 break;
914         }
915         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
916                 ahareset(aha, /*hardreset*/TRUE);
917                 ccb->ccb_h.status = CAM_REQ_CMP;
918                 xpt_done(ccb);
919                 break;
920         case XPT_TERM_IO:               /* Terminate the I/O process */
921                 /* XXX Implement */
922                 ccb->ccb_h.status = CAM_REQ_INVALID;
923                 xpt_done(ccb);
924                 break;
925         case XPT_PATH_INQ:              /* Path routing inquiry */
926         {
927                 struct ccb_pathinq *cpi = &ccb->cpi;
928
929                 cpi->version_num = 1; /* XXX??? */
930                 cpi->hba_inquiry = PI_SDTR_ABLE;
931                 cpi->target_sprt = 0;
932                 cpi->hba_misc = 0;
933                 cpi->hba_eng_cnt = 0;
934                 cpi->max_target = 7;
935                 cpi->max_lun = 7;
936                 cpi->initiator_id = aha->scsi_id;
937                 cpi->bus_id = cam_sim_bus(sim);
938                 cpi->base_transfer_speed = 3300;
939                 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
940                 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
941                 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
942                 cpi->unit_number = cam_sim_unit(sim);
943                 cpi->transport = XPORT_SPI;
944                 cpi->transport_version = 2;
945                 cpi->protocol = PROTO_SCSI;
946                 cpi->protocol_version = SCSI_REV_2;
947                 cpi->ccb_h.status = CAM_REQ_CMP;
948                 xpt_done(ccb);
949                 break;
950         }
951         default:
952                 ccb->ccb_h.status = CAM_REQ_INVALID;
953                 xpt_done(ccb);
954                 break;
955         }
956 }
957
958 static void
959 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
960 {
961         struct   aha_ccb *accb;
962         union    ccb *ccb;
963         struct   aha_softc *aha;
964         uint32_t paddr;
965
966         accb = (struct aha_ccb *)arg;
967         ccb = accb->ccb;
968         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
969
970         if (error != 0) {
971                 if (error != EFBIG)
972                         device_printf(aha->dev,
973                             "Unexepected error 0x%x returned from "
974                             "bus_dmamap_load\n", error);
975                 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
976                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
977                         ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
978                 }
979                 ahafreeccb(aha, accb);
980                 xpt_done(ccb);
981                 return;
982         }
983
984         if (nseg != 0) {
985                 aha_sg_t *sg;
986                 bus_dma_segment_t *end_seg;
987                 bus_dmasync_op_t op;
988
989                 end_seg = dm_segs + nseg;
990
991                 /* Copy the segments into our SG list */
992                 sg = accb->sg_list;
993                 while (dm_segs < end_seg) {
994                         ahautoa24(dm_segs->ds_len, sg->len);
995                         ahautoa24(dm_segs->ds_addr, sg->addr);
996                         sg++;
997                         dm_segs++;
998                 }
999
1000                 if (nseg > 1) {
1001                         accb->hccb.opcode = aha->ccb_sg_opcode;
1002                         ahautoa24((sizeof(aha_sg_t) * nseg),
1003                             accb->hccb.data_len);
1004                         ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1005                 } else {
1006                         bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1007                         bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1008                 }
1009
1010                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1011                         op = BUS_DMASYNC_PREREAD;
1012                 else
1013                         op = BUS_DMASYNC_PREWRITE;
1014
1015                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1016
1017         } else {
1018                 accb->hccb.opcode = INITIATOR_CCB;
1019                 ahautoa24(0, accb->hccb.data_len);
1020                 ahautoa24(0, accb->hccb.data_addr);
1021         }
1022
1023         /*
1024          * Last time we need to check if this CCB needs to
1025          * be aborted.
1026          */
1027         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1028                 if (nseg != 0)
1029                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1030                 ahafreeccb(aha, accb);
1031                 xpt_done(ccb);
1032                 return;
1033         }
1034
1035         accb->flags = ACCB_ACTIVE;
1036         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1037         LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1038
1039         callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1040             ahatimeout, accb, 0);
1041
1042         /* Tell the adapter about this command */
1043         if (aha->cur_outbox->action_code != AMBO_FREE) {
1044                 /*
1045                  * We should never encounter a busy mailbox.
1046                  * If we do, warn the user, and treat it as
1047                  * a resource shortage.  If the controller is
1048                  * hung, one of the pending transactions will
1049                  * timeout causing us to start recovery operations.
1050                  */
1051                 device_printf(aha->dev,
1052                     "Encountered busy mailbox with %d out of %d "
1053                     "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1054                 callout_stop(&accb->timer);
1055                 if (nseg != 0)
1056                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1057                 ahafreeccb(aha, accb);
1058                 aha->resource_shortage = TRUE;
1059                 xpt_freeze_simq(aha->sim, /*count*/1);
1060                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1061                 xpt_done(ccb);
1062                 return;
1063         }
1064         paddr = ahaccbvtop(aha, accb);
1065         ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1066         aha->cur_outbox->action_code = AMBO_START;
1067         aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1068
1069         ahanextoutbox(aha);
1070 }
1071
1072 void
1073 aha_intr(void *arg)
1074 {
1075         struct  aha_softc *aha;
1076
1077         aha = arg;
1078         mtx_lock(&aha->lock);
1079         aha_intr_locked(aha);
1080         mtx_unlock(&aha->lock);
1081 }
1082
1083 void
1084 aha_intr_locked(struct aha_softc *aha)
1085 {
1086         u_int   intstat;
1087         uint32_t paddr;
1088
1089         while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1090                 if ((intstat & CMD_COMPLETE) != 0) {
1091                         aha->latched_status = aha_inb(aha, STATUS_REG);
1092                         aha->command_cmp = TRUE;
1093                 }
1094
1095                 aha_outb(aha, CONTROL_REG, RESET_INTR);
1096
1097                 if ((intstat & IMB_LOADED) != 0) {
1098                         while (aha->cur_inbox->comp_code != AMBI_FREE) {
1099                                 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1100                                 ahadone(aha, ahaccbptov(aha, paddr),
1101                                     aha->cur_inbox->comp_code);
1102                                 aha->cur_inbox->comp_code = AMBI_FREE;
1103                                 ahanextinbox(aha);
1104                         }
1105                 }
1106
1107                 if ((intstat & SCSI_BUS_RESET) != 0) {
1108                         ahareset(aha, /*hardreset*/FALSE);
1109                 }
1110         }
1111 }
1112
1113 static void
1114 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1115 {
1116         union  ccb        *ccb;
1117         struct ccb_scsiio *csio;
1118
1119         ccb = accb->ccb;
1120         csio = &accb->ccb->csio;
1121
1122         if ((accb->flags & ACCB_ACTIVE) == 0) {
1123                 device_printf(aha->dev, 
1124                     "ahadone - Attempt to free non-active ACCB %p\n",
1125                     (void *)accb);
1126                 return;
1127         }
1128
1129         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1130                 bus_dmasync_op_t op;
1131
1132                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1133                         op = BUS_DMASYNC_POSTREAD;
1134                 else
1135                         op = BUS_DMASYNC_POSTWRITE;
1136                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1137                 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1138         }
1139
1140         if (accb == aha->recovery_accb) {
1141                 /*
1142                  * The recovery ACCB does not have a CCB associated
1143                  * with it, so short circuit the normal error handling.
1144                  * We now traverse our list of pending CCBs and process
1145                  * any that were terminated by the recovery CCBs action.
1146                  * We also reinstate timeouts for all remaining, pending,
1147                  * CCBs.
1148                  */
1149                 struct cam_path *path;
1150                 struct ccb_hdr *ccb_h;
1151                 cam_status error;
1152
1153                 /* Notify all clients that a BDR occurred */
1154                 error = xpt_create_path(&path, /*periph*/NULL,
1155                     cam_sim_path(aha->sim), accb->hccb.target,
1156                     CAM_LUN_WILDCARD);
1157
1158                 if (error == CAM_REQ_CMP) {
1159                         xpt_async(AC_SENT_BDR, path, NULL);
1160                         xpt_free_path(path);
1161                 }
1162
1163                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1164                 while (ccb_h != NULL) {
1165                         struct aha_ccb *pending_accb;
1166
1167                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1168                         if (pending_accb->hccb.target == accb->hccb.target) {
1169                                 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1170                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1171                                 ahadone(aha, pending_accb, AMBI_ERROR);
1172                         } else {
1173                                 callout_reset_sbt(&pending_accb->timer,
1174                                     SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1175                                     pending_accb, 0);
1176                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1177                         }
1178                 }
1179                 device_printf(aha->dev, "No longer in timeout\n");
1180                 return;
1181         }
1182
1183         callout_stop(&accb->timer);
1184
1185         switch (comp_code) {
1186         case AMBI_FREE:
1187                 device_printf(aha->dev,
1188                     "ahadone - CCB completed with free status!\n");
1189                 break;
1190         case AMBI_NOT_FOUND:
1191                 device_printf(aha->dev,
1192                     "ahadone - CCB Abort failed to find CCB\n");
1193                 break;
1194         case AMBI_ABORT:
1195         case AMBI_ERROR:
1196                 /* An error occurred */
1197                 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1198                         csio->resid = 0;
1199                 else
1200                         csio->resid = aha_a24tou(accb->hccb.data_len);
1201                 switch(accb->hccb.ahastat) {
1202                 case AHASTAT_DATARUN_ERROR:
1203                 {
1204                         if (csio->resid <= 0) {
1205                                 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1206                                 break;
1207                         }
1208                         /* FALLTHROUGH */
1209                 }
1210                 case AHASTAT_NOERROR:
1211                         csio->scsi_status = accb->hccb.sdstat;
1212                         csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1213                         switch(csio->scsi_status) {
1214                         case SCSI_STATUS_CHECK_COND:
1215                         case SCSI_STATUS_CMD_TERMINATED:
1216                                 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1217                                 /*
1218                                  * The aha writes the sense data at different
1219                                  * offsets based on the scsi cmd len
1220                                  */
1221                                 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1222                                     accb->hccb.cmd_len,
1223                                     (caddr_t) &csio->sense_data,
1224                                     accb->hccb.sense_len);
1225                                 break;
1226                         default:
1227                                 break;
1228                         case SCSI_STATUS_OK:
1229                                 csio->ccb_h.status = CAM_REQ_CMP;
1230                                 break;
1231                         }
1232                         break;
1233                 case AHASTAT_SELTIMEOUT:
1234                         csio->ccb_h.status = CAM_SEL_TIMEOUT;
1235                         break;
1236                 case AHASTAT_UNEXPECTED_BUSFREE:
1237                         csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1238                         break;
1239                 case AHASTAT_INVALID_PHASE:
1240                         csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1241                         break;
1242                 case AHASTAT_INVALID_ACTION_CODE:
1243                         panic("%s: Inavlid Action code", aha_name(aha));
1244                         break;
1245                 case AHASTAT_INVALID_OPCODE:
1246                         if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1247                                 panic("%s: Invalid CCB Opcode %x hccb = %p",
1248                                     aha_name(aha), accb->hccb.opcode,
1249                                     &accb->hccb);
1250                         device_printf(aha->dev,
1251                             "AHA-1540A compensation failed\n");
1252                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1253                         csio->ccb_h.status = CAM_REQUEUE_REQ;
1254                         break;
1255                 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1256                         /* We don't even support linked commands... */
1257                         panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1258                         break;
1259                 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1260                         panic("%s: Invalid CCB or SG list", aha_name(aha));
1261                         break;
1262                 case AHASTAT_HA_SCSI_BUS_RESET:
1263                         if ((csio->ccb_h.status & CAM_STATUS_MASK)
1264                             != CAM_CMD_TIMEOUT)
1265                                 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1266                         break;
1267                 case AHASTAT_HA_BDR:
1268                         if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1269                                 csio->ccb_h.status = CAM_BDR_SENT;
1270                         else
1271                                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1272                         break;
1273                 }
1274                 if (csio->ccb_h.status != CAM_REQ_CMP) {
1275                         xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1276                         csio->ccb_h.status |= CAM_DEV_QFRZN;
1277                 }
1278                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1279                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1280                 ahafreeccb(aha, accb);
1281                 xpt_done(ccb);
1282                 break;
1283         case AMBI_OK:
1284                 /* All completed without incident */
1285                 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1286                 /* I don't think so since it works???? */
1287                 ccb->ccb_h.status |= CAM_REQ_CMP;
1288                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1289                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1290                 ahafreeccb(aha, accb);
1291                 xpt_done(ccb);
1292                 break;
1293         }
1294 }
1295
1296 static int
1297 ahareset(struct aha_softc* aha, int hard_reset)
1298 {
1299         struct   ccb_hdr *ccb_h;
1300         u_int    status;
1301         u_int    timeout;
1302         uint8_t reset_type;
1303
1304         if (hard_reset != 0)
1305                 reset_type = HARD_RESET;
1306         else
1307                 reset_type = SOFT_RESET;
1308         aha_outb(aha, CONTROL_REG, reset_type);
1309
1310         /* Wait 5sec. for Diagnostic start */
1311         timeout = 5 * 10000;
1312         while (--timeout) {
1313                 status = aha_inb(aha, STATUS_REG);
1314                 if ((status & DIAG_ACTIVE) != 0)
1315                         break;
1316                 DELAY(100);
1317         }
1318         if (timeout == 0) {
1319                 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1320                     "assert. status = %#x\n", status));
1321                 return (ETIMEDOUT);
1322         }
1323
1324         /* Wait 10sec. for Diagnostic end */
1325         timeout = 10 * 10000;
1326         while (--timeout) {
1327                 status = aha_inb(aha, STATUS_REG);
1328                 if ((status & DIAG_ACTIVE) == 0)
1329                         break;
1330                 DELAY(100);
1331         }
1332         if (timeout == 0) {
1333                 panic("%s: ahareset - Diagnostic Active failed to drop. "
1334                     "status = 0x%x\n", aha_name(aha), status);
1335                 return (ETIMEDOUT);
1336         }
1337
1338         /* Wait for the host adapter to become ready or report a failure */
1339         timeout = 10000;
1340         while (--timeout) {
1341                 status = aha_inb(aha, STATUS_REG);
1342                 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1343                         break;
1344                 DELAY(100);
1345         }
1346         if (timeout == 0) {
1347                 device_printf(aha->dev, "ahareset - Host adapter failed to "
1348                     "come ready. status = 0x%x\n", status);
1349                 return (ETIMEDOUT);
1350         }
1351
1352         /* If the diagnostics failed, tell the user */
1353         if ((status & DIAG_FAIL) != 0
1354          || (status & HA_READY) == 0) {
1355                 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1356
1357                 if ((status & DATAIN_REG_READY) != 0)
1358                         device_printf(aha->dev, "ahareset - Host Adapter "
1359                             "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1360                 return (ENXIO);
1361         }
1362
1363         /* If we've attached to the XPT, tell it about the event */
1364         if (aha->path != NULL)
1365                 xpt_async(AC_BUS_RESET, aha->path, NULL);
1366
1367         /*
1368          * Perform completion processing for all outstanding CCBs.
1369          */
1370         while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1371                 struct aha_ccb *pending_accb;
1372
1373                 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1374                 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1375                 ahadone(aha, pending_accb, AMBI_ERROR);
1376         }
1377
1378         /* If we've allocated mailboxes, initialize them */
1379         /* Must be done after we've aborted our queue, or aha_cmd fails */
1380         if (aha->init_level > 4)
1381                 ahainitmboxes(aha);
1382
1383         return (0);
1384 }
1385
1386 /*
1387  * Send a command to the adapter.
1388  */
1389 int
1390 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1391         u_int param_len, uint8_t *reply_data, u_int reply_len,
1392         u_int cmd_timeout)
1393 {
1394         u_int   timeout;
1395         u_int   status;
1396         u_int   saved_status;
1397         u_int   intstat;
1398         u_int   reply_buf_size;
1399         int     cmd_complete;
1400         int     error;
1401
1402         /* No data returned to start */
1403         reply_buf_size = reply_len;
1404         reply_len = 0;
1405         intstat = 0;
1406         cmd_complete = 0;
1407         saved_status = 0;
1408         error = 0;
1409
1410         /*
1411          * All commands except for the "start mailbox" and the "enable
1412          * outgoing mailbox read interrupt" commands cannot be issued
1413          * while there are pending transactions.  Freeze our SIMQ
1414          * and wait for all completions to occur if necessary.
1415          */
1416         timeout = 10000;
1417         while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1418                 /* Fire the interrupt handler in case interrupts are blocked */
1419                 aha_intr(aha);
1420                 DELAY(10);
1421         }
1422
1423         if (timeout == 0) {
1424                 device_printf(aha->dev, 
1425                     "aha_cmd: Timeout waiting for adapter idle\n");
1426                 return (ETIMEDOUT);
1427         }
1428         aha->command_cmp = 0;
1429         /*
1430          * Wait up to 10 sec. for the adapter to become
1431          * ready to accept commands.
1432          */
1433         timeout = 100000;
1434         while (--timeout) {
1435                 status = aha_inb(aha, STATUS_REG);
1436                 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1437                         break;
1438                 /*
1439                  * Throw away any pending data which may be
1440                  * left over from earlier commands that we
1441                  * timedout on.
1442                  */
1443                 if ((status & DATAIN_REG_READY) != 0)
1444                         (void)aha_inb(aha, DATAIN_REG);
1445                 DELAY(100);
1446         }
1447         if (timeout == 0) {
1448                 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1449                     " ready, status = 0x%x\n", status);
1450                 return (ETIMEDOUT);
1451         }
1452
1453         /*
1454          * Send the opcode followed by any necessary parameter bytes.
1455          */
1456         aha_outb(aha, COMMAND_REG, opcode);
1457
1458         /*
1459          * Wait for up to 1sec to get the parameter list sent
1460          */
1461         timeout = 10000;
1462         while (param_len && --timeout) {
1463                 DELAY(100);
1464                 status = aha_inb(aha, STATUS_REG);
1465                 intstat = aha_inb(aha, INTSTAT_REG);
1466
1467                 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1468                  == (INTR_PENDING|CMD_COMPLETE)) {
1469                         saved_status = status;
1470                         cmd_complete = 1;
1471                         break;
1472                 }
1473
1474                 if (aha->command_cmp != 0) {
1475                         saved_status = aha->latched_status;
1476                         cmd_complete = 1;
1477                         break;
1478                 }
1479                 if ((status & DATAIN_REG_READY) != 0)
1480                         break;
1481                 if ((status & CMD_REG_BUSY) == 0) {
1482                         aha_outb(aha, COMMAND_REG, *params++);
1483                         param_len--;
1484                         timeout = 10000;
1485                 }
1486         }
1487         if (timeout == 0) {
1488                 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1489                     "status = 0x%x\n", status);
1490                 error = ETIMEDOUT;
1491         }
1492
1493         /*
1494          * For all other commands, we wait for any output data
1495          * and the final comand completion interrupt.
1496          */
1497         while (cmd_complete == 0 && --cmd_timeout) {
1498
1499                 status = aha_inb(aha, STATUS_REG);
1500                 intstat = aha_inb(aha, INTSTAT_REG);
1501
1502                 if (aha->command_cmp != 0) {
1503                         cmd_complete = 1;
1504                         saved_status = aha->latched_status;
1505                 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1506                         == (INTR_PENDING|CMD_COMPLETE)) {
1507                         /*
1508                          * Our poll (in case interrupts are blocked)
1509                          * saw the CMD_COMPLETE interrupt.
1510                          */
1511                         cmd_complete = 1;
1512                         saved_status = status;
1513                 }
1514                 if ((status & DATAIN_REG_READY) != 0) {
1515                         uint8_t data;
1516
1517                         data = aha_inb(aha, DATAIN_REG);
1518                         if (reply_len < reply_buf_size) {
1519                                 *reply_data++ = data;
1520                         } else {
1521                                 device_printf(aha->dev, 
1522                                     "aha_cmd - Discarded reply data "
1523                                     "byte for opcode 0x%x\n", opcode);
1524                         }
1525                         /*
1526                          * Reset timeout to ensure at least a second
1527                          * between response bytes.
1528                          */
1529                         cmd_timeout = MAX(cmd_timeout, 10000);
1530                         reply_len++;
1531                 }
1532                 DELAY(100);
1533         }
1534         if (cmd_timeout == 0) {
1535                 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1536                     "intstat = 0x%x, reply_len = %d\n", status, intstat,
1537                     reply_len);
1538                 return (ETIMEDOUT);
1539         }
1540
1541         /*
1542          * Clear any pending interrupts.  Block interrupts so our
1543          * interrupt handler is not re-entered.
1544          */
1545         aha_intr(aha);
1546
1547         if (error != 0)
1548                 return (error);
1549
1550         /*
1551          * If the command was rejected by the controller, tell the caller.
1552          */
1553         if ((saved_status & CMD_INVALID) != 0) {
1554                 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1555                 /*
1556                  * Some early adapters may not recover properly from
1557                  * an invalid command.  If it appears that the controller
1558                  * has wedged (i.e. status was not cleared by our interrupt
1559                  * reset above), perform a soft reset.
1560                  */
1561                 DELAY(1000);
1562                 status = aha_inb(aha, STATUS_REG);
1563                 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1564                               CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1565                  || (status & (HA_READY|INIT_REQUIRED))
1566                   != (HA_READY|INIT_REQUIRED))
1567                         ahareset(aha, /*hard_reset*/FALSE);
1568                 return (EINVAL);
1569         }
1570
1571         if (param_len > 0) {
1572                 /* The controller did not accept the full argument list */
1573                 PRVERB((aha->dev, "Controller did not accept full argument "
1574                     "list (%d > 0)\n", param_len));
1575                 return (E2BIG);
1576         }
1577
1578         if (reply_len != reply_buf_size) {
1579                 /* Too much or too little data received */
1580                 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1581                     reply_len, reply_buf_size));
1582                 return (EMSGSIZE);
1583         }
1584
1585         /* We were successful */
1586         return (0);
1587 }
1588
1589 static int
1590 ahainitmboxes(struct aha_softc *aha)
1591 {
1592         int error;
1593         init_24b_mbox_params_t init_mbox;
1594
1595         bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1596         bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1597         aha->cur_inbox = aha->in_boxes;
1598         aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1599         aha->cur_outbox = aha->out_boxes;
1600         aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1601
1602         /* Tell the adapter about them */
1603         init_mbox.num_mboxes = aha->num_boxes;
1604         ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1605         error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1606             /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1607             /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1608
1609         if (error != 0)
1610                 printf("ahainitmboxes: Initialization command failed\n");
1611         return (error);
1612 }
1613
1614 /*
1615  * Update the XPT's idea of the negotiated transfer
1616  * parameters for a particular target.
1617  */
1618 static void
1619 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1620 {
1621         setup_data_t    setup_info;
1622         u_int           target;
1623         u_int           targ_offset;
1624         u_int           sync_period;
1625         int             error;
1626         uint8_t param;
1627         targ_syncinfo_t sync_info;
1628         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1629
1630         target = cts->ccb_h.target_id;
1631         targ_offset = (target & 0x7);
1632
1633         /*
1634          * Inquire Setup Information.  This command retreives
1635          * the sync info for older models.
1636          */
1637         param = sizeof(setup_info);
1638         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
1639             (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1640
1641         if (error != 0) {
1642                 device_printf(aha->dev,
1643                     "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1644                     error);
1645                 return;
1646         }
1647
1648         sync_info = setup_info.syncinfo[targ_offset];
1649
1650         if (sync_info.sync == 0)
1651                 spi->sync_offset = 0;
1652         else
1653                 spi->sync_offset = sync_info.offset;
1654
1655         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1656
1657         if (aha->boardid >= BOARD_1542CF)
1658                 sync_period = 1000;
1659         else
1660                 sync_period = 2000;
1661         sync_period += 500 * sync_info.period;
1662
1663         /* Convert ns value to standard SCSI sync rate */
1664         if (spi->sync_offset != 0)
1665                 spi->sync_period = scsi_calc_syncparam(sync_period);
1666         else
1667                 spi->sync_period = 0;
1668
1669         spi->valid = CTS_SPI_VALID_SYNC_RATE
1670                    | CTS_SPI_VALID_SYNC_OFFSET
1671                    | CTS_SPI_VALID_BUS_WIDTH;
1672         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1673 }
1674
1675 static void
1676 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1677 {
1678         struct aha_softc* aha;
1679
1680         aha = (struct aha_softc*)arg;
1681         aha->mailbox_physbase = segs->ds_addr;
1682 }
1683
1684 static void
1685 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1686 {
1687         struct aha_softc* aha;
1688
1689         aha = (struct aha_softc*)arg;
1690         aha->aha_ccb_physbase = segs->ds_addr;
1691 }
1692
1693 static void
1694 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1695 {
1696
1697         struct aha_softc* aha;
1698
1699         aha = (struct aha_softc*)arg;
1700         SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1701 }
1702
1703 static void
1704 ahapoll(struct cam_sim *sim)
1705 {
1706         aha_intr_locked(cam_sim_softc(sim));
1707 }
1708
1709 static void
1710 ahatimeout(void *arg)
1711 {
1712         struct aha_ccb  *accb;
1713         union  ccb      *ccb;
1714         struct aha_softc *aha;
1715         uint32_t        paddr;
1716         struct ccb_hdr *ccb_h;
1717
1718         accb = (struct aha_ccb *)arg;
1719         ccb = accb->ccb;
1720         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1721         mtx_assert(&aha->lock, MA_OWNED);
1722         xpt_print_path(ccb->ccb_h.path);
1723         printf("CCB %p - timed out\n", (void *)accb);
1724
1725         if ((accb->flags & ACCB_ACTIVE) == 0) {
1726                 xpt_print_path(ccb->ccb_h.path);
1727                 printf("CCB %p - timed out CCB already completed\n",
1728                     (void *)accb);
1729                 return;
1730         }
1731
1732         /*
1733          * In order to simplify the recovery process, we ask the XPT
1734          * layer to halt the queue of new transactions and we traverse
1735          * the list of pending CCBs and remove their timeouts. This
1736          * means that the driver attempts to clear only one error
1737          * condition at a time.  In general, timeouts that occur
1738          * close together are related anyway, so there is no benefit
1739          * in attempting to handle errors in parallel.  Timeouts will
1740          * be reinstated when the recovery process ends.
1741          */
1742         if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1743                 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1744                         xpt_freeze_simq(aha->sim, /*count*/1);
1745                         accb->flags |= ACCB_RELEASE_SIMQ;
1746                 }
1747
1748                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1749                 while (ccb_h != NULL) {
1750                         struct aha_ccb *pending_accb;
1751
1752                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1753                         callout_stop(&pending_accb->timer);
1754                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1755                 }
1756         }
1757
1758         if ((accb->flags & ACCB_DEVICE_RESET) != 0
1759          || aha->cur_outbox->action_code != AMBO_FREE) {
1760                 /*
1761                  * Try a full host adapter/SCSI bus reset.
1762                  * We do this only if we have already attempted
1763                  * to clear the condition with a BDR, or we cannot
1764                  * attempt a BDR for lack of mailbox resources.
1765                  */
1766                 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1767                 ahareset(aha, /*hardreset*/TRUE);
1768                 device_printf(aha->dev, "No longer in timeout\n");
1769         } else {
1770                 /*
1771                  * Send a Bus Device Reset message:
1772                  * The target that is holding up the bus may not
1773                  * be the same as the one that triggered this timeout
1774                  * (different commands have different timeout lengths),
1775                  * but we have no way of determining this from our
1776                  * timeout handler.  Our strategy here is to queue a
1777                  * BDR message to the target of the timed out command.
1778                  * If this fails, we'll get another timeout 2 seconds
1779                  * later which will attempt a bus reset.
1780                  */
1781                 accb->flags |= ACCB_DEVICE_RESET;
1782                 callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1783                 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1784
1785                 /* No Data Transfer */
1786                 aha->recovery_accb->hccb.datain = TRUE;
1787                 aha->recovery_accb->hccb.dataout = TRUE;
1788                 aha->recovery_accb->hccb.ahastat = 0;
1789                 aha->recovery_accb->hccb.sdstat = 0;
1790                 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1791
1792                 /* Tell the adapter about this command */
1793                 paddr = ahaccbvtop(aha, aha->recovery_accb);
1794                 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1795                 aha->cur_outbox->action_code = AMBO_START;
1796                 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1797                 ahanextoutbox(aha);
1798         }
1799 }
1800
1801 int
1802 aha_detach(struct aha_softc *aha)
1803 {
1804         mtx_lock(&aha->lock);
1805         xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1806         xpt_free_path(aha->path);
1807         xpt_bus_deregister(cam_sim_path(aha->sim));
1808         cam_sim_free(aha->sim, /*free_devq*/TRUE);
1809         mtx_unlock(&aha->lock);
1810         /* XXX: Drain all timers? */
1811         return (0);
1812 }
1813 MODULE_DEPEND(aha, cam, 1, 1, 1);