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