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