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