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