Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx_osm.c
1 /*
2  * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2002 Justin T. Gibbs.
5  * Copyright (c) 2001-2002 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
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  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.c#27 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.3.2.4 2003/06/10 03:26:07 gibbs Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.10 2006/09/05 00:55:37 dillon Exp $
36  */
37
38 #include "aic79xx_osm.h"
39 #include "aic79xx_inline.h"
40
41 #include "opt_ddb.h"
42 #ifdef DDB
43 #include <ddb/ddb.h>
44 #endif
45
46 #ifndef AHD_TMODE_ENABLE
47 #define AHD_TMODE_ENABLE 0
48 #endif
49
50 #define ccb_scb_ptr spriv_ptr0
51
52 #if UNUSED
53 static void     ahd_dump_targcmd(struct target_cmd *cmd);
54 #endif
55 static int      ahd_modevent(module_t mod, int type, void *data);
56 static void     ahd_action(struct cam_sim *sim, union ccb *ccb);
57 static void     ahd_set_tran_settings(struct ahd_softc *ahd,
58                                       int our_id, char channel,
59                                       struct ccb_trans_settings *cts);
60 static void     ahd_get_tran_settings(struct ahd_softc *ahd,
61                                       int our_id, char channel,
62                                       struct ccb_trans_settings *cts);
63 static void     ahd_async(void *callback_arg, uint32_t code,
64                           struct cam_path *path, void *arg);
65 static void     ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
66                                 int nsegments, int error);
67 static void     ahd_poll(struct cam_sim *sim);
68 static void     ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
69                                struct ccb_scsiio *csio, struct scb *scb);
70 static void     ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
71                               union ccb *ccb);
72 static int      ahd_create_path(struct ahd_softc *ahd,
73                                 char channel, u_int target, u_int lun,
74                                 struct cam_path **path);
75
76 #if NOT_YET
77 static void     ahd_set_recoveryscb(struct ahd_softc *ahd, struct scb *scb);
78 #endif
79
80 static int
81 ahd_create_path(struct ahd_softc *ahd, char channel, u_int target,
82                 u_int lun, struct cam_path **path)
83 {
84         path_id_t path_id;
85
86         if (channel == 'B')
87                 path_id = cam_sim_path(ahd->platform_data->sim_b);
88         else 
89                 path_id = cam_sim_path(ahd->platform_data->sim);
90
91         return (xpt_create_path(path, /*periph*/NULL,
92                                 path_id, target, lun));
93 }
94
95 int
96 ahd_map_int(struct ahd_softc *ahd)
97 {
98         int error;
99
100         /* Hook up our interrupt handler */
101         error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
102                                0, ahd_platform_intr, ahd,
103                                &ahd->platform_data->ih, NULL);
104         if (error != 0)
105                 device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
106                               error);
107         return (error);
108 }
109
110 /*
111  * Attach all the sub-devices we can find
112  */
113 int
114 ahd_attach(struct ahd_softc *ahd)
115 {
116         char   ahd_info[256];
117         struct ccb_setasync csa;
118         struct cam_sim *sim;
119         struct cam_path *path;
120         long s;
121         int count;
122
123         count = 0;
124         sim = NULL;
125
126         ahd_controller_info(ahd, ahd_info);
127         printf("%s\n", ahd_info);
128         ahd_lock(ahd, &s);
129
130         /*
131          * Construct our SIM entry
132          */
133         sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
134                             device_get_unit(ahd->dev_softc),
135                             1, AHD_MAX_QUEUE, NULL);
136         if (sim == NULL)
137                 goto fail;
138
139         if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
140                 cam_sim_free(sim);
141                 sim = NULL;
142                 goto fail;
143         }
144         
145         if (xpt_create_path(&path, /*periph*/NULL,
146                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
147                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
148                 xpt_bus_deregister(cam_sim_path(sim));
149                 cam_sim_free(sim);
150                 sim = NULL;
151                 goto fail;
152         }
153                 
154         xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
155         csa.ccb_h.func_code = XPT_SASYNC_CB;
156         csa.event_enable = AC_LOST_DEVICE;
157         csa.callback = ahd_async;
158         csa.callback_arg = sim;
159         xpt_action((union ccb *)&csa);
160         count++;
161
162 fail:
163         ahd->platform_data->sim = sim;
164         ahd->platform_data->path = path;
165         if (count != 0) {
166                 /* We have to wait until after any system dumps... */
167                 ahd->platform_data->eh =
168                     EVENTHANDLER_REGISTER(shutdown_final, ahd_shutdown,
169                                           ahd, SHUTDOWN_PRI_DEFAULT);
170                 ahd_intr_enable(ahd, TRUE);
171         }
172
173         ahd_unlock(ahd, &s);
174
175         return (count);
176 }
177
178 /*
179  * Catch an interrupt from the adapter
180  */
181 void
182 ahd_platform_intr(void *arg)
183 {
184         struct  ahd_softc *ahd;
185
186         ahd = (struct ahd_softc *)arg; 
187         ahd_intr(ahd);
188 }
189
190 /*
191  * We have an scb which has been processed by the
192  * adaptor, now we look to see how the operation
193  * went.
194  */
195 void
196 ahd_done(struct ahd_softc *ahd, struct scb *scb)
197 {
198         union ccb *ccb;
199
200         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
201                   ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
202
203         ccb = scb->io_ctx;
204         LIST_REMOVE(scb, pending_links);
205
206         callout_stop(&ccb->ccb_h.timeout_ch);
207
208         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
209                 bus_dmasync_op_t op;
210
211                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
212                         op = BUS_DMASYNC_POSTREAD;
213                 else
214                         op = BUS_DMASYNC_POSTWRITE;
215                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
216                 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
217         }
218
219 #ifdef AHD_TARGET_MODE
220         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
221                 struct cam_path *ccb_path;
222
223                 /*
224                  * If we have finally disconnected, clean up our
225                  * pending device state.
226                  * XXX - There may be error states that cause where
227                  *       we will remain connected.
228                  */
229                 ccb_path = ccb->ccb_h.path;
230                 if (ahd->pending_device != NULL
231                  && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
232
233                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
234                                 ahd->pending_device = NULL;
235                         } else {
236                                 xpt_print_path(ccb->ccb_h.path);
237                                 printf("Still disconnected\n");
238                                 ahd_freeze_ccb(ccb);
239                         }
240                 }
241
242                 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG)
243                         ccb->ccb_h.status |= CAM_REQ_CMP;
244                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
245                 ahd_free_scb(ahd, scb);
246                 xpt_done(ccb);
247                 return;
248         }
249 #endif
250
251         /*
252          * If the recovery SCB completes, we have to be
253          * out of our timeout.
254          */
255         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
256                 struct  scb *list_scb;
257
258                 /*
259                  * We were able to complete the command successfully,
260                  * so reinstate the timeouts for all other pending
261                  * commands.
262                  */
263                 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
264                         union ccb *ccb;
265                         uint64_t time;
266
267                         ccb = list_scb->io_ctx;
268                         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
269                                 continue;
270
271                         time = ccb->ccb_h.timeout;
272                         time *= hz;
273                         time /= 1000;
274                         callout_reset(&ccb->ccb_h.timeout_ch, time,
275                             ahd_timeout, list_scb);
276                 }
277
278                 if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
279                  || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
280                         ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
281                 ahd_print_path(ahd, scb);
282                 printf("no longer in timeout, status = %x\n",
283                        ccb->ccb_h.status);
284         }
285
286         /* Don't clobber any existing error state */
287         if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
288                 ccb->ccb_h.status |= CAM_REQ_CMP;
289         } else if ((scb->flags & SCB_SENSE) != 0) {
290                 /*
291                  * We performed autosense retrieval.
292                  *
293                  * Zero any sense not transferred by the
294                  * device.  The SCSI spec mandates that any
295                  * untransfered data should be assumed to be
296                  * zero.  Complete the 'bounce' of sense information
297                  * through buffers accessible via bus-space by
298                  * copying it into the clients csio.
299                  */
300                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
301                 memcpy(&ccb->csio.sense_data,
302                        ahd_get_sense_buf(ahd, scb),
303 /* XXX What size do we want to use??? */
304                         sizeof(ccb->csio.sense_data)
305                        - ccb->csio.sense_resid);
306                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
307         } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
308                 struct scsi_status_iu_header *siu;
309                 u_int sense_len;
310                 int i;
311
312                 /*
313                  * Copy only the sense data into the provided buffer.
314                  */
315                 siu = (struct scsi_status_iu_header *)scb->sense_data;
316                 sense_len = MIN(scsi_4btoul(siu->sense_length),
317                                 sizeof(ccb->csio.sense_data));
318                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
319                 memcpy(&ccb->csio.sense_data,
320                        ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
321                        sense_len);
322                 printf("Copied %d bytes of sense data offset %d:", sense_len,
323                        SIU_SENSE_OFFSET(siu));
324                 for (i = 0; i < sense_len; i++)
325                         printf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
326                 printf("\n");
327                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
328         }
329         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
330         ahd_free_scb(ahd, scb);
331         xpt_done(ccb);
332 }
333
334 static void
335 ahd_action(struct cam_sim *sim, union ccb *ccb)
336 {
337         struct  ahd_softc *ahd;
338 #ifdef AHD_TARGET_MODE
339         struct  ahd_tmode_lstate *lstate;
340 #endif
341         u_int   target_id;
342         u_int   our_id;
343         long    s;
344
345         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
346         
347         ahd = (struct ahd_softc *)cam_sim_softc(sim);
348
349         target_id = ccb->ccb_h.target_id;
350         our_id = SIM_SCSI_ID(ahd, sim);
351         
352         switch (ccb->ccb_h.func_code) {
353         /* Common cases first */
354 #ifdef AHD_TARGET_MODE
355         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
356         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
357         {
358                 struct     ahd_tmode_tstate *tstate;
359                 cam_status status;
360
361                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
362                                              &lstate, TRUE);
363
364                 if (status != CAM_REQ_CMP) {
365                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
366                                 /* Response from the black hole device */
367                                 tstate = NULL;
368                                 lstate = ahd->black_hole;
369                         } else {
370                                 ccb->ccb_h.status = status;
371                                 xpt_done(ccb);
372                                 break;
373                         }
374                 }
375                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
376
377                         ahd_lock(ahd, &s);
378                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
379                                           sim_links.sle);
380                         ccb->ccb_h.status = CAM_REQ_INPROG;
381                         if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
382                                 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
383                         ahd_unlock(ahd, &s);
384                         break;
385                 }
386
387                 /*
388                  * The target_id represents the target we attempt to
389                  * select.  In target mode, this is the initiator of
390                  * the original command.
391                  */
392                 our_id = target_id;
393                 target_id = ccb->csio.init_id;
394                 /* FALLTHROUGH */
395         }
396 #endif
397         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
398         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
399         {
400                 struct  scb *scb;
401                 struct  hardware_scb *hscb;     
402                 struct  ahd_initiator_tinfo *tinfo;
403                 struct  ahd_tmode_tstate *tstate;
404                 u_int   col_idx;
405
406                 if ((ahd->flags & AHD_INITIATORROLE) == 0
407                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
408                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
409                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
410                         xpt_done(ccb);
411                         return;
412                 }
413
414                 /*
415                  * get an scb to use.
416                  */
417                 ahd_lock(ahd, &s);
418                 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
419                                             target_id, &tstate);
420                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
421                  || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
422                  || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
423                         col_idx = AHD_NEVER_COL_IDX;
424                 } else {
425                         col_idx = AHD_BUILD_COL_IDX(target_id,
426                                                     ccb->ccb_h.target_lun);
427                 }
428                 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
429         
430                         xpt_freeze_simq(sim, /*count*/1);
431                         ahd->flags |= AHD_RESOURCE_SHORTAGE;
432                         ahd_unlock(ahd, &s);
433                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
434                         xpt_done(ccb);
435                         return;
436                 }
437                 ahd_unlock(ahd, &s);
438                 
439                 hscb = scb->hscb;
440                 
441                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
442                           ("start scb(%p)\n", scb));
443                 scb->io_ctx = ccb;
444                 /*
445                  * So we can find the SCB when an abort is requested
446                  */
447                 ccb->ccb_h.ccb_scb_ptr = scb;
448
449                 /*
450                  * Put all the arguments for the xfer in the scb
451                  */
452                 hscb->control = 0;
453                 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
454                 hscb->lun = ccb->ccb_h.target_lun;
455                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
456                         hscb->cdb_len = 0;
457                         scb->flags |= SCB_DEVICE_RESET;
458                         hscb->control |= MK_MESSAGE;
459                         hscb->task_management = SIU_TASKMGMT_LUN_RESET;
460                         ahd_execute_scb(scb, NULL, 0, 0);
461                 } else {
462 #ifdef AHD_TARGET_MODE
463                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
464                                 struct target_data *tdata;
465
466                                 tdata = &hscb->shared_data.tdata;
467                                 if (ahd->pending_device == lstate)
468                                         scb->flags |= SCB_TARGET_IMMEDIATE;
469                                 hscb->control |= TARGET_SCB;
470                                 tdata->target_phases = 0;
471                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
472                                         tdata->target_phases |= SPHASE_PENDING;
473                                         tdata->scsi_status =
474                                             ccb->csio.scsi_status;
475                                 }
476                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
477                                         tdata->target_phases |= NO_DISCONNECT;
478
479                                 tdata->initiator_tag =
480                                     ahd_htole16(ccb->csio.tag_id);
481                         }
482 #endif
483                         hscb->task_management = 0;
484                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
485                                 hscb->control |= ccb->csio.tag_action;
486                         
487                         ahd_setup_data(ahd, sim, &ccb->csio, scb);
488                 }
489                 break;
490         }
491 #ifdef AHD_TARGET_MODE
492         case XPT_NOTIFY_ACK:
493         case XPT_IMMED_NOTIFY:
494         {
495                 struct     ahd_tmode_tstate *tstate;
496                 struct     ahd_tmode_lstate *lstate;
497                 cam_status status;
498
499                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
500                                              &lstate, TRUE);
501
502                 if (status != CAM_REQ_CMP) {
503                         ccb->ccb_h.status = status;
504                         xpt_done(ccb);
505                         break;
506                 }
507                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
508                                   sim_links.sle);
509                 ccb->ccb_h.status = CAM_REQ_INPROG;
510                 ahd_send_lstate_events(ahd, lstate);
511                 break;
512         }
513         case XPT_EN_LUN:                /* Enable LUN as a target */
514                 ahd_handle_en_lun(ahd, sim, ccb);
515                 xpt_done(ccb);
516                 break;
517 #endif
518         case XPT_ABORT:                 /* Abort the specified CCB */
519         {
520                 ahd_abort_ccb(ahd, sim, ccb);
521                 break;
522         }
523         case XPT_SET_TRAN_SETTINGS:
524         {
525                 ahd_lock(ahd, &s);
526                 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
527                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
528                 ahd_unlock(ahd, &s);
529                 xpt_done(ccb);
530                 break;
531         }
532         case XPT_GET_TRAN_SETTINGS:
533         /* Get default/user set transfer settings for the target */
534         {
535                 ahd_lock(ahd, &s);
536                 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
537                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
538                 ahd_unlock(ahd, &s);
539                 xpt_done(ccb);
540                 break;
541         }
542         case XPT_CALC_GEOMETRY:
543         {
544                 struct    ccb_calc_geometry *ccg;
545                 uint32_t size_mb;
546                 uint32_t secs_per_cylinder;
547                 int       extended;
548
549                 ccg = &ccb->ccg;
550                 size_mb = ccg->volume_size
551                         / ((1024L * 1024L) / ccg->block_size);
552                 extended = ahd->flags & AHD_EXTENDED_TRANS_A;
553                 
554                 if (size_mb > 1024 && extended) {
555                         ccg->heads = 255;
556                         ccg->secs_per_track = 63;
557                 } else {
558                         ccg->heads = 64;
559                         ccg->secs_per_track = 32;
560                 }
561                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
562                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
563                 ccb->ccb_h.status = CAM_REQ_CMP;
564                 xpt_done(ccb);
565                 break;
566         }
567         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
568         {
569                 int  found;
570                 
571                 ahd_lock(ahd, &s);
572                 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
573                                           /*initiate reset*/TRUE);
574                 ahd_unlock(ahd, &s);
575                 if (bootverbose) {
576                         xpt_print_path(SIM_PATH(ahd, sim));
577                         printf("SCSI bus reset delivered. "
578                                "%d SCBs aborted.\n", found);
579                 }
580                 ccb->ccb_h.status = CAM_REQ_CMP;
581                 xpt_done(ccb);
582                 break;
583         }
584         case XPT_TERM_IO:               /* Terminate the I/O process */
585                 /* XXX Implement */
586                 ccb->ccb_h.status = CAM_REQ_INVALID;
587                 xpt_done(ccb);
588                 break;
589         case XPT_PATH_INQ:              /* Path routing inquiry */
590         {
591                 struct ccb_pathinq *cpi = &ccb->cpi;
592                 
593                 cpi->version_num = 1; /* XXX??? */
594                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
595                 if ((ahd->features & AHD_WIDE) != 0)
596                         cpi->hba_inquiry |= PI_WIDE_16;
597                 if ((ahd->features & AHD_TARGETMODE) != 0) {
598                         cpi->target_sprt = PIT_PROCESSOR
599                                          | PIT_DISCONNECT
600                                          | PIT_TERM_IO;
601                 } else {
602                         cpi->target_sprt = 0;
603                 }
604                 cpi->hba_misc = 0;
605                 cpi->hba_eng_cnt = 0;
606                 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
607                 cpi->max_lun = AHD_NUM_LUNS - 1;
608                 cpi->initiator_id = ahd->our_id;
609                 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
610                         cpi->hba_misc |= PIM_NOBUSRESET;
611                 }
612                 cpi->bus_id = cam_sim_bus(sim);
613                 cpi->base_transfer_speed = 3300;
614                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
615                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
616                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
617                 cpi->unit_number = cam_sim_unit(sim);
618 #ifdef AHD_NEW_TRAN_SETTINGS
619                 cpi->protocol = PROTO_SCSI;
620                 cpi->protocol_version = SCSI_REV_2;
621                 cpi->transport = XPORT_SPI;
622                 cpi->transport_version = 2;
623                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
624                 cpi->transport_version = 4;
625                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
626 #endif
627                 cpi->ccb_h.status = CAM_REQ_CMP;
628                 xpt_done(ccb);
629                 break;
630         }
631         default:
632                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
633                 xpt_done(ccb);
634                 break;
635         }
636 }
637
638
639 static void
640 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
641                       struct ccb_trans_settings *cts)
642 {
643 #ifdef AHD_NEW_TRAN_SETTINGS
644         struct    ahd_devinfo devinfo;
645         struct    ccb_trans_settings_scsi *scsi;
646         struct    ccb_trans_settings_spi *spi;
647         struct    ahd_initiator_tinfo *tinfo;
648         struct    ahd_tmode_tstate *tstate;
649         uint16_t *discenable;
650         uint16_t *tagenable;
651         u_int     update_type;
652
653         scsi = &cts->proto_specific.scsi;
654         spi = &cts->xport_specific.spi;
655         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
656                             cts->ccb_h.target_id,
657                             cts->ccb_h.target_lun,
658                             SIM_CHANNEL(ahd, sim),
659                             ROLE_UNKNOWN);
660         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
661                                     devinfo.our_scsiid,
662                                     devinfo.target, &tstate);
663         update_type = 0;
664         if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
665                 update_type |= AHD_TRANS_GOAL;
666                 discenable = &tstate->discenable;
667                 tagenable = &tstate->tagenable;
668                 tinfo->curr.protocol_version = cts->protocol_version;
669                 tinfo->curr.transport_version = cts->transport_version;
670                 tinfo->goal.protocol_version = cts->protocol_version;
671                 tinfo->goal.transport_version = cts->transport_version;
672         } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
673                 update_type |= AHD_TRANS_USER;
674                 discenable = &ahd->user_discenable;
675                 tagenable = &ahd->user_tagenable;
676                 tinfo->user.protocol_version = cts->protocol_version;
677                 tinfo->user.transport_version = cts->transport_version;
678         } else {
679                 cts->ccb_h.status = CAM_REQ_INVALID;
680                 return;
681         }
682         
683         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
684                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
685                         *discenable |= devinfo.target_mask;
686                 else
687                         *discenable &= ~devinfo.target_mask;
688         }
689         
690         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
691                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
692                         *tagenable |= devinfo.target_mask;
693                 else
694                         *tagenable &= ~devinfo.target_mask;
695         }       
696
697         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
698                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
699                                    &spi->bus_width, ROLE_UNKNOWN);
700                 ahd_set_width(ahd, &devinfo, spi->bus_width,
701                               update_type, /*paused*/FALSE);
702         }
703
704         if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
705                 if (update_type == AHD_TRANS_USER)
706                         spi->ppr_options = tinfo->user.ppr_options;
707                 else
708                         spi->ppr_options = tinfo->goal.ppr_options;
709         }
710
711         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
712                 if (update_type == AHD_TRANS_USER)
713                         spi->sync_offset = tinfo->user.offset;
714                 else
715                         spi->sync_offset = tinfo->goal.offset;
716         }
717
718         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
719                 if (update_type == AHD_TRANS_USER)
720                         spi->sync_period = tinfo->user.period;
721                 else
722                         spi->sync_period = tinfo->goal.period;
723         }
724
725         if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
726          || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
727                 u_int   maxsync;
728
729                 maxsync = AHD_SYNCRATE_MAX;
730
731                 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
732                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
733
734                 if ((*discenable & devinfo.target_mask) == 0)
735                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
736
737                 ahd_find_syncrate(ahd, &spi->sync_period,
738                                   &spi->ppr_options, maxsync);
739                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
740                                     spi->sync_period, &spi->sync_offset,
741                                     spi->bus_width, ROLE_UNKNOWN);
742
743                 /* We use a period of 0 to represent async */
744                 if (spi->sync_offset == 0) {
745                         spi->sync_period = 0;
746                         spi->ppr_options = 0;
747                 }
748
749                 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
750                                  spi->sync_offset, spi->ppr_options,
751                                  update_type, /*paused*/FALSE);
752         }
753         cts->ccb_h.status = CAM_REQ_CMP;
754 #else
755         struct    ahd_devinfo devinfo;
756         struct    ahd_initiator_tinfo *tinfo;
757         struct    ahd_tmode_tstate *tstate;
758         uint16_t *discenable;
759         uint16_t *tagenable;
760         u_int     update_type;
761
762         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
763                             cts->ccb_h.target_id,
764                             cts->ccb_h.target_lun,
765                             SIM_CHANNEL(ahd, sim),
766                             ROLE_UNKNOWN);
767         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
768                                     devinfo.our_scsiid,
769                                     devinfo.target, &tstate);
770         update_type = 0;
771         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
772                 update_type |= AHD_TRANS_GOAL;
773                 discenable = &tstate->discenable;
774                 tagenable = &tstate->tagenable;
775         } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
776                 update_type |= AHD_TRANS_USER;
777                 discenable = &ahd->user_discenable;
778                 tagenable = &ahd->user_tagenable;
779         } else {
780                 cts->ccb_h.status = CAM_REQ_INVALID;
781                 return;
782         }
783         
784         if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
785                 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
786                         *discenable |= devinfo.target_mask;
787                 else
788                         *discenable &= ~devinfo.target_mask;
789         }
790         
791         if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
792                 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
793                         *tagenable |= devinfo.target_mask;
794                 else
795                         *tagenable &= ~devinfo.target_mask;
796         }       
797
798         if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
799                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
800                                    &cts->bus_width, ROLE_UNKNOWN);
801                 ahd_set_width(ahd, &devinfo, cts->bus_width,
802                               update_type, /*paused*/FALSE);
803         }
804
805         if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
806                 if (update_type == AHD_TRANS_USER)
807                         cts->sync_offset = tinfo->user.offset;
808                 else
809                         cts->sync_offset = tinfo->goal.offset;
810         }
811
812         if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
813                 if (update_type == AHD_TRANS_USER)
814                         cts->sync_period = tinfo->user.period;
815                 else
816                         cts->sync_period = tinfo->goal.period;
817         }
818
819         if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
820          || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
821          || ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
822          || ((cts->valid & CCB_TRANS_DISC_VALID) != 0)) {
823                 u_int ppr_options;
824                 u_int maxsync;
825
826                 maxsync = AHD_SYNCRATE_MAX;
827                 ppr_options = 0;
828                 if (cts->sync_period <= AHD_SYNCRATE_DT
829                  && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT) {
830                         ppr_options = tinfo->user.ppr_options
831                                     | MSG_EXT_PPR_DT_REQ;
832                 }
833
834                 if ((*tagenable & devinfo.target_mask) == 0
835                  || (*discenable & devinfo.target_mask) == 0)
836                         ppr_options &= ~MSG_EXT_PPR_IU_REQ;
837
838                 ahd_find_syncrate(ahd, &cts->sync_period,
839                                   &ppr_options, maxsync);
840                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
841                                     cts->sync_period, &cts->sync_offset,
842                                     MSG_EXT_WDTR_BUS_8_BIT,
843                                     ROLE_UNKNOWN);
844
845                 /* We use a period of 0 to represent async */
846                 if (cts->sync_offset == 0) {
847                         cts->sync_period = 0;
848                         ppr_options = 0;
849                 }
850
851                 if (ppr_options != 0
852                  && tinfo->user.transport_version >= 3) {
853                         tinfo->goal.transport_version =
854                             tinfo->user.transport_version;
855                         tinfo->curr.transport_version =
856                             tinfo->user.transport_version;
857                 }
858                 
859                 ahd_set_syncrate(ahd, &devinfo, cts->sync_period,
860                                  cts->sync_offset, ppr_options,
861                                  update_type, /*paused*/FALSE);
862         }
863         cts->ccb_h.status = CAM_REQ_CMP;
864 #endif
865 }
866
867 static void
868 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
869                       struct ccb_trans_settings *cts)
870 {
871 #ifdef AHD_NEW_TRAN_SETTINGS
872         struct  ahd_devinfo devinfo;
873         struct  ccb_trans_settings_scsi *scsi;
874         struct  ccb_trans_settings_spi *spi;
875         struct  ahd_initiator_tinfo *targ_info;
876         struct  ahd_tmode_tstate *tstate;
877         struct  ahd_transinfo *tinfo;
878
879         scsi = &cts->proto_specific.scsi;
880         spi = &cts->xport_specific.spi;
881         ahd_compile_devinfo(&devinfo, our_id,
882                             cts->ccb_h.target_id,
883                             cts->ccb_h.target_lun,
884                             channel, ROLE_UNKNOWN);
885         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
886                                         devinfo.our_scsiid,
887                                         devinfo.target, &tstate);
888         
889         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
890                 tinfo = &targ_info->curr;
891         else
892                 tinfo = &targ_info->user;
893         
894         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
895         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
896         if (cts->type == CTS_TYPE_USER_SETTINGS) {
897                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
898                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
899
900                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
901                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
902         } else {
903                 if ((tstate->discenable & devinfo.target_mask) != 0)
904                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
905
906                 if ((tstate->tagenable & devinfo.target_mask) != 0)
907                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
908         }
909         cts->protocol_version = tinfo->protocol_version;
910         cts->transport_version = tinfo->transport_version;
911
912         spi->sync_period = tinfo->period;
913         spi->sync_offset = tinfo->offset;
914         spi->bus_width = tinfo->width;
915         spi->ppr_options = tinfo->ppr_options;
916         
917         cts->protocol = PROTO_SCSI;
918         cts->transport = XPORT_SPI;
919         spi->valid = CTS_SPI_VALID_SYNC_RATE
920                    | CTS_SPI_VALID_SYNC_OFFSET
921                    | CTS_SPI_VALID_BUS_WIDTH
922                    | CTS_SPI_VALID_PPR_OPTIONS;
923
924         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
925                 scsi->valid = CTS_SCSI_VALID_TQ;
926                 spi->valid |= CTS_SPI_VALID_DISC;
927         } else {
928                 scsi->valid = 0;
929         }
930
931         cts->ccb_h.status = CAM_REQ_CMP;
932 #else
933         struct  ahd_devinfo devinfo;
934         struct  ahd_initiator_tinfo *targ_info;
935         struct  ahd_tmode_tstate *tstate;
936         struct  ahd_transinfo *tinfo;
937
938         ahd_compile_devinfo(&devinfo, our_id,
939                             cts->ccb_h.target_id,
940                             cts->ccb_h.target_lun,
941                             channel, ROLE_UNKNOWN);
942         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
943                                         devinfo.our_scsiid,
944                                         devinfo.target, &tstate);
945         
946         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
947                 tinfo = &targ_info->curr;
948         else
949                 tinfo = &targ_info->user;
950         
951         cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
952         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
953                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
954                         cts->flags |= CCB_TRANS_DISC_ENB;
955
956                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
957                         cts->flags |= CCB_TRANS_TAG_ENB;
958         } else {
959                 if ((tstate->discenable & devinfo.target_mask) != 0)
960                         cts->flags |= CCB_TRANS_DISC_ENB;
961
962                 if ((tstate->tagenable & devinfo.target_mask) != 0)
963                         cts->flags |= CCB_TRANS_TAG_ENB;
964         }
965         cts->sync_period = tinfo->period;
966         cts->sync_offset = tinfo->offset;
967         cts->bus_width = tinfo->width;
968         
969         cts->valid = CCB_TRANS_SYNC_RATE_VALID
970                    | CCB_TRANS_SYNC_OFFSET_VALID
971                    | CCB_TRANS_BUS_WIDTH_VALID;
972
973         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
974                 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
975
976         cts->ccb_h.status = CAM_REQ_CMP;
977 #endif
978 }
979
980 static void
981 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
982 {
983         struct ahd_softc *ahd;
984         struct cam_sim *sim;
985
986         sim = (struct cam_sim *)callback_arg;
987         ahd = (struct ahd_softc *)cam_sim_softc(sim);
988         switch (code) {
989         case AC_LOST_DEVICE:
990         {
991                 struct  ahd_devinfo devinfo;
992                 long    s;
993
994                 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
995                                     xpt_path_target_id(path),
996                                     xpt_path_lun_id(path),
997                                     SIM_CHANNEL(ahd, sim),
998                                     ROLE_UNKNOWN);
999
1000                 /*
1001                  * Revert to async/narrow transfers
1002                  * for the next device.
1003                  */
1004                 ahd_lock(ahd, &s);
1005                 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1006                               AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
1007                 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
1008                                  /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
1009                                  /*paused*/FALSE);
1010                 ahd_unlock(ahd, &s);
1011                 break;
1012         }
1013         default:
1014                 break;
1015         }
1016 }
1017
1018 static void
1019 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1020                 int error)
1021 {
1022         struct  scb *scb;
1023         union   ccb *ccb;
1024         struct  ahd_softc *ahd;
1025         struct  ahd_initiator_tinfo *tinfo;
1026         struct  ahd_tmode_tstate *tstate;
1027         u_int   mask;
1028         u_long  s;
1029
1030         scb = (struct scb *)arg;
1031         ccb = scb->io_ctx;
1032         ahd = scb->ahd_softc;
1033
1034         if (error != 0) {
1035                 if (error == EFBIG)
1036                         ahd_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1037                 else
1038                         ahd_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1039                 if (nsegments != 0)
1040                         bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
1041                 ahd_lock(ahd, &s);
1042                 ahd_free_scb(ahd, scb);
1043                 ahd_unlock(ahd, &s);
1044                 xpt_done(ccb);
1045                 return;
1046         }
1047         scb->sg_count = 0;
1048         if (nsegments != 0) {
1049                 void *sg;
1050                 bus_dmasync_op_t op;
1051                 u_int i;
1052
1053                 /* Copy the segments into our SG list */
1054                 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
1055
1056                         sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
1057                                           dm_segs->ds_len,
1058                                           /*last*/i == 1);
1059                         dm_segs++;
1060                 }
1061                 
1062                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1063                         op = BUS_DMASYNC_PREREAD;
1064                 else
1065                         op = BUS_DMASYNC_PREWRITE;
1066
1067                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
1068
1069                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1070                         struct target_data *tdata;
1071
1072                         tdata = &scb->hscb->shared_data.tdata;
1073                         tdata->target_phases |= DPHASE_PENDING;
1074                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1075                                 tdata->data_phase = P_DATAOUT;
1076                         else
1077                                 tdata->data_phase = P_DATAIN;
1078                 }
1079         }
1080
1081         ahd_lock(ahd, &s);
1082
1083         /*
1084          * Last time we need to check if this SCB needs to
1085          * be aborted.
1086          */
1087         if (ahd_get_transaction_status(scb) != CAM_REQ_INPROG) {
1088                 if (nsegments != 0)
1089                         bus_dmamap_unload(ahd->buffer_dmat,
1090                                           scb->dmamap);
1091                 ahd_free_scb(ahd, scb);
1092                 ahd_unlock(ahd, &s);
1093                 xpt_done(ccb);
1094                 return;
1095         }
1096
1097         tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1098                                     SCSIID_OUR_ID(scb->hscb->scsiid),
1099                                     SCSIID_TARGET(ahd, scb->hscb->scsiid),
1100                                     &tstate);
1101
1102         mask = SCB_GET_TARGET_MASK(ahd, scb);
1103
1104         if ((tstate->discenable & mask) != 0
1105          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1106                 scb->hscb->control |= DISCENB;
1107
1108         if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1109                 scb->flags |= SCB_PACKETIZED;
1110                 if (scb->hscb->task_management != 0)
1111                         scb->hscb->control &= ~MK_MESSAGE;
1112         }
1113
1114         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1115          && (tinfo->goal.width != 0
1116           || tinfo->goal.period != 0
1117           || tinfo->goal.ppr_options != 0)) {
1118                 scb->flags |= SCB_NEGOTIATE;
1119                 scb->hscb->control |= MK_MESSAGE;
1120         } else if ((tstate->auto_negotiate & mask) != 0) {
1121                 scb->flags |= SCB_AUTO_NEGOTIATE;
1122                 scb->hscb->control |= MK_MESSAGE;
1123         }
1124
1125         LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1126
1127         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1128
1129         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1130                 uint64_t time;
1131
1132                 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1133                         ccb->ccb_h.timeout = 5 * 1000;
1134
1135                 time = ccb->ccb_h.timeout;
1136                 time *= hz;
1137                 time /= 1000;
1138                 callout_reset(&ccb->ccb_h.timeout_ch, time, ahd_timeout, scb);
1139         }
1140
1141         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1142                 /* Define a mapping from our tag to the SCB. */
1143                 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1144                 ahd_pause(ahd);
1145                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1146                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1147                 ahd_unpause(ahd);
1148         } else {
1149                 ahd_queue_scb(ahd, scb);
1150         }
1151
1152         ahd_unlock(ahd, &s);
1153 }
1154
1155 static void
1156 ahd_poll(struct cam_sim *sim)
1157 {
1158         ahd_intr(cam_sim_softc(sim));
1159 }
1160
1161 static void
1162 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1163                struct ccb_scsiio *csio, struct scb *scb)
1164 {
1165         struct hardware_scb *hscb;
1166         struct ccb_hdr *ccb_h;
1167         
1168         hscb = scb->hscb;
1169         ccb_h = &csio->ccb_h;
1170         
1171         csio->resid = 0;
1172         csio->sense_resid = 0;
1173         if (ccb_h->func_code == XPT_SCSI_IO) {
1174                 hscb->cdb_len = csio->cdb_len;
1175                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1176
1177                         if (hscb->cdb_len > MAX_CDB_LEN
1178                          && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1179                                 u_long s;
1180
1181                                 /*
1182                                  * Should CAM start to support CDB sizes
1183                                  * greater than 16 bytes, we could use
1184                                  * the sense buffer to store the CDB.
1185                                  */
1186                                 ahd_set_transaction_status(scb,
1187                                                            CAM_REQ_INVALID);
1188                                 ahd_lock(ahd, &s);
1189                                 ahd_free_scb(ahd, scb);
1190                                 ahd_unlock(ahd, &s);
1191                                 xpt_done((union ccb *)csio);
1192                                 return;
1193                         }
1194                         if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1195                                 hscb->shared_data.idata.cdb_from_host.cdbptr =
1196                                    ahd_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1197                                 hscb->shared_data.idata.cdb_from_host.cdblen =
1198                                    csio->cdb_len;
1199                                 hscb->cdb_len |= SCB_CDB_LEN_PTR;
1200                         } else {
1201                                 memcpy(hscb->shared_data.idata.cdb, 
1202                                        csio->cdb_io.cdb_ptr,
1203                                        hscb->cdb_len);
1204                         }
1205                 } else {
1206                         if (hscb->cdb_len > MAX_CDB_LEN) {
1207                                 u_long s;
1208
1209                                 ahd_set_transaction_status(scb,
1210                                                            CAM_REQ_INVALID);
1211                                 ahd_lock(ahd, &s);
1212                                 ahd_free_scb(ahd, scb);
1213                                 ahd_unlock(ahd, &s);
1214                                 xpt_done((union ccb *)csio);
1215                                 return;
1216                         }
1217                         memcpy(hscb->shared_data.idata.cdb,
1218                                csio->cdb_io.cdb_bytes, hscb->cdb_len);
1219                 }
1220         }
1221                 
1222         /* Only use S/G if there is a transfer */
1223         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1224                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1225                         /* We've been given a pointer to a single buffer */
1226                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1227                                 int error;
1228
1229                                 crit_enter();
1230                                 error = bus_dmamap_load(ahd->buffer_dmat,
1231                                                         scb->dmamap,
1232                                                         csio->data_ptr,
1233                                                         csio->dxfer_len,
1234                                                         ahd_execute_scb,
1235                                                         scb, /*flags*/0);
1236                                 if (error == EINPROGRESS) {
1237                                         /*
1238                                          * So as to maintain ordering,
1239                                          * freeze the controller queue
1240                                          * until our mapping is
1241                                          * returned.
1242                                          */
1243                                         xpt_freeze_simq(sim,
1244                                                         /*count*/1);
1245                                         scb->io_ctx->ccb_h.status |=
1246                                             CAM_RELEASE_SIMQ;
1247                                 }
1248                                 crit_exit();
1249                         } else {
1250                                 struct bus_dma_segment seg;
1251
1252                                 /* Pointer to physical buffer */
1253                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1254                                         panic("ahd_setup_data - Transfer size "
1255                                               "larger than can device max");
1256
1257                                 seg.ds_addr =
1258                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1259                                 seg.ds_len = csio->dxfer_len;
1260                                 ahd_execute_scb(scb, &seg, 1, 0);
1261                         }
1262                 } else {
1263                         struct bus_dma_segment *segs;
1264
1265                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1266                                 panic("ahd_setup_data - Physical segment "
1267                                       "pointers unsupported");
1268
1269                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1270                                 panic("ahd_setup_data - Virtual segment "
1271                                       "addresses unsupported");
1272
1273                         /* Just use the segments provided */
1274                         segs = (struct bus_dma_segment *)csio->data_ptr;
1275                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1276                 }
1277         } else {
1278                 ahd_execute_scb(scb, NULL, 0, 0);
1279         }
1280 }
1281
1282 #if NOT_YET
1283 static void
1284 ahd_set_recoveryscb(struct ahd_softc *ahd, struct scb *scb) {
1285
1286         if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1287                 struct scb *list_scb;
1288
1289                 scb->flags |= SCB_RECOVERY_SCB;
1290
1291                 /*
1292                  * Take all queued, but not sent SCBs out of the equation.
1293                  * Also ensure that no new CCBs are queued to us while we
1294                  * try to fix this problem.
1295                  */
1296                 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1297                         xpt_freeze_simq(SCB_GET_SIM(ahd, scb), /*count*/1);
1298                         scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1299                 }
1300
1301                 /*
1302                  * Go through all of our pending SCBs and remove
1303                  * any scheduled timeouts for them.  We will reschedule
1304                  * them after we've successfully fixed this problem.
1305                  */
1306                 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
1307                         union ccb *ccb;
1308
1309                         ccb = list_scb->io_ctx;
1310                         callout_stop(&ccb->ccb_h.timeout_ch);
1311                 }
1312         }
1313 }
1314 #endif
1315
1316 void
1317 ahd_timeout(void *arg)
1318 {
1319         struct  scb       *scb;
1320         struct  ahd_softc *ahd;
1321         ahd_mode_state     saved_modes;
1322         long               s;
1323         int                target;
1324         int                lun;
1325         char               channel;
1326
1327 #if NOT_YET
1328         int                i;
1329         int                found;
1330         u_int              last_phase;
1331 #endif
1332
1333         scb = (struct scb *)arg; 
1334         ahd = (struct ahd_softc *)scb->ahd_softc;
1335
1336         ahd_lock(ahd, &s);
1337
1338         ahd_pause_and_flushwork(ahd);
1339
1340         saved_modes = ahd_save_modes(ahd);
1341 #if 0
1342         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1343         ahd_outb(ahd, SCSISIGO, ACKO);
1344         printf("set ACK\n");
1345         ahd_outb(ahd, SCSISIGO, 0);
1346         printf("clearing Ack\n");
1347         ahd_restore_modes(ahd, saved_modes);
1348 #endif
1349         if ((scb->flags & SCB_ACTIVE) == 0) {
1350                 /* Previous timeout took care of me already */
1351                 printf("%s: Timedout SCB already complete. "
1352                        "Interrupts may not be functioning.\n", ahd_name(ahd));
1353                 ahd_unpause(ahd);
1354                 ahd_unlock(ahd, &s);
1355                 return;
1356         }
1357
1358         target = SCB_GET_TARGET(ahd, scb);
1359         channel = SCB_GET_CHANNEL(ahd, scb);
1360         lun = SCB_GET_LUN(scb);
1361
1362         ahd_print_path(ahd, scb);
1363         printf("SCB 0x%x - timed out\n", SCB_GET_TAG(scb));
1364         ahd_dump_card_state(ahd);
1365         ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
1366                           /*initiate reset*/TRUE);
1367         ahd_unlock(ahd, &s);
1368         return;
1369 #if NOT_YET
1370         last_phase = ahd_inb(ahd, LASTPHASE);
1371         if (scb->sg_count > 0) {
1372                 for (i = 0; i < scb->sg_count; i++) {
1373                         printf("sg[%d] - Addr 0x%x : Length %d\n",
1374                                i,
1375                                ((struct ahd_dma_seg *)scb->sg_list)[i].addr,
1376                                ((struct ahd_dma_seg *)scb->sg_list)[i].len
1377                                 & AHD_SG_LEN_MASK);
1378                 }
1379         }
1380         if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1381                 /*
1382                  * Been down this road before.
1383                  * Do a full bus reset.
1384                  */
1385 bus_reset:
1386                 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1387                 found = ahd_reset_channel(ahd, channel, /*Initiate Reset*/TRUE);
1388                 printf("%s: Issued Channel %c Bus Reset. "
1389                        "%d SCBs aborted\n", ahd_name(ahd), channel, found);
1390         } else {
1391                 /*
1392                  * If we are a target, transition to bus free and report
1393                  * the timeout.
1394                  * 
1395                  * The target/initiator that is holding up the bus may not
1396                  * be the same as the one that triggered this timeout
1397                  * (different commands have different timeout lengths).
1398                  * If the bus is idle and we are actiing as the initiator
1399                  * for this request, queue a BDR message to the timed out
1400                  * target.  Otherwise, if the timed out transaction is
1401                  * active:
1402                  *   Initiator transaction:
1403                  *      Stuff the message buffer with a BDR message and assert
1404                  *      ATN in the hopes that the target will let go of the bus
1405                  *      and go to the mesgout phase.  If this fails, we'll
1406                  *      get another timeout 2 seconds later which will attempt
1407                  *      a bus reset.
1408                  *
1409                  *   Target transaction:
1410                  *      Transition to BUS FREE and report the error.
1411                  *      It's good to be the target!
1412                  */
1413                 u_int active_scb_index;
1414                 u_int saved_scbptr;
1415
1416                 saved_scbptr = ahd_get_scbptr(ahd);
1417                 active_scb_index = saved_scbptr;
1418
1419                 if (last_phase != P_BUSFREE 
1420                   && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1421                   && (active_scb_index < ahd->scb_data.numscbs)) {
1422                         struct scb *active_scb;
1423
1424                         /*
1425                          * If the active SCB is not us, assume that
1426                          * the active SCB has a longer timeout than
1427                          * the timedout SCB, and wait for the active
1428                          * SCB to timeout.
1429                          */ 
1430                         active_scb = ahd_lookup_scb(ahd, active_scb_index);
1431                         if (active_scb != scb) {
1432                                 struct   ccb_hdr *ccbh;
1433                                 uint64_t newtimeout;
1434
1435                                 ahd_print_path(ahd, scb);
1436                                 printf("Other SCB Timeout%s",
1437                                        (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1438                                        ? " again\n" : "\n");
1439                                 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1440                                 newtimeout =
1441                                     MAX(active_scb->io_ctx->ccb_h.timeout,
1442                                         scb->io_ctx->ccb_h.timeout);
1443                                 newtimeout *= hz;
1444                                 newtimeout /= 1000;
1445                                 ccbh = &scb->io_ctx->ccb_h;
1446                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1447                                     newtimeout, ahd_timeout, scb);
1448                                 ahd_unpause(ahd);
1449                                 ahd_unlock(ahd, &s);
1450                                 return;
1451                         } 
1452
1453                         /* It's us */
1454                         if ((scb->hscb->control & TARGET_SCB) != 0) {
1455
1456                                 /*
1457                                  * Send back any queued up transactions
1458                                  * and properly record the error condition.
1459                                  */
1460                                 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1461                                                SCB_GET_CHANNEL(ahd, scb),
1462                                                SCB_GET_LUN(scb),
1463                                                SCB_GET_TAG(scb),
1464                                                ROLE_TARGET,
1465                                                CAM_CMD_TIMEOUT);
1466
1467                                 /* Will clear us from the bus */
1468                                 ahd_restart(ahd);
1469                                 ahd_unlock(ahd, &s);
1470                                 return;
1471                         }
1472
1473                         ahd_set_recoveryscb(ahd, active_scb);
1474                         ahd_outb(ahd, MSG_OUT, HOST_MSG);
1475                         ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
1476                         ahd_print_path(ahd, active_scb);
1477                         printf("BDR message in message buffer\n");
1478                         active_scb->flags |= SCB_DEVICE_RESET;
1479                         callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1480                             2 * hz, ahd_timeout, active_scb);
1481                         ahd_unpause(ahd);
1482                 } else {
1483                         int      disconnected;
1484
1485                         /* XXX Shouldn't panic.  Just punt instead? */
1486                         if ((scb->hscb->control & TARGET_SCB) != 0)
1487                                 panic("Timed-out target SCB but bus idle");
1488
1489                         if (last_phase != P_BUSFREE
1490                          && (ahd_inb(ahd, SSTAT0) & TARGET) != 0) {
1491                                 /* XXX What happened to the SCB? */
1492                                 /* Hung target selection.  Goto busfree */
1493                                 printf("%s: Hung target selection\n",
1494                                        ahd_name(ahd));
1495                                 ahd_restart(ahd);
1496                                 ahd_unlock(ahd, &s);
1497                                 return;
1498                         }
1499
1500                         if (ahd_search_qinfifo(ahd, target, channel, lun,
1501                                                SCB_GET_TAG(scb), ROLE_INITIATOR,
1502                                                /*status*/0, SEARCH_COUNT) > 0) {
1503                                 disconnected = FALSE;
1504                         } else {
1505                                 disconnected = TRUE;
1506                         }
1507
1508                         if (disconnected) {
1509
1510                                 ahd_set_recoveryscb(ahd, scb);
1511                                 /*
1512                                  * Actually re-queue this SCB in an attempt
1513                                  * to select the device before it reconnects.
1514                                  * In either case (selection or reselection),
1515                                  * we will now issue a target reset to the
1516                                  * timed-out device.
1517                                  *
1518                                  * Set the MK_MESSAGE control bit indicating
1519                                  * that we desire to send a message.  We
1520                                  * also set the disconnected flag since
1521                                  * in the paging case there is no guarantee
1522                                  * that our SCB control byte matches the
1523                                  * version on the card.  We don't want the
1524                                  * sequencer to abort the command thinking
1525                                  * an unsolicited reselection occurred.
1526                                  */
1527                                 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1528                                 scb->flags |= SCB_DEVICE_RESET;
1529
1530                                 /*
1531                                  * The sequencer will never re-reference the
1532                                  * in-core SCB.  To make sure we are notified
1533                                  * during reslection, set the MK_MESSAGE flag
1534                                  * in the card's copy of the SCB.
1535                                  */
1536                                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1537                                 ahd_outb(ahd, SCB_CONTROL,
1538                                          ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
1539
1540                                 /*
1541                                  * Clear out any entries in the QINFIFO first
1542                                  * so we are the next SCB for this target
1543                                  * to run.
1544                                  */
1545                                 ahd_search_qinfifo(ahd,
1546                                                    SCB_GET_TARGET(ahd, scb),
1547                                                    channel, SCB_GET_LUN(scb),
1548                                                    SCB_LIST_NULL,
1549                                                    ROLE_INITIATOR,
1550                                                    CAM_REQUEUE_REQ,
1551                                                    SEARCH_COMPLETE);
1552                                 ahd_print_path(ahd, scb);
1553                                 printf("Queuing a BDR SCB\n");
1554                                 ahd_qinfifo_requeue_tail(ahd, scb);
1555                                 ahd_set_scbptr(ahd, saved_scbptr);
1556                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1557                                     2 * hz, ahd_timeout, scb);
1558                                 ahd_unpause(ahd);
1559                         } else {
1560                                 /* Go "immediatly" to the bus reset */
1561                                 /* This shouldn't happen */
1562                                 ahd_set_recoveryscb(ahd, scb);
1563                                 ahd_print_path(ahd, scb);
1564                                 printf("SCB %d: Immediate reset.  "
1565                                         "Flags = 0x%x\n", SCB_GET_TAG(scb),
1566                                         scb->flags);
1567                                 goto bus_reset;
1568                         }
1569                 }
1570         }
1571         ahd_unlock(ahd, &s);
1572 #endif
1573 }
1574
1575 static void
1576 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1577 {
1578         union ccb *abort_ccb;
1579
1580         abort_ccb = ccb->cab.abort_ccb;
1581         switch (abort_ccb->ccb_h.func_code) {
1582 #ifdef AHD_TARGET_MODE
1583         case XPT_ACCEPT_TARGET_IO:
1584         case XPT_IMMED_NOTIFY:
1585         case XPT_CONT_TARGET_IO:
1586         {
1587                 struct ahd_tmode_tstate *tstate;
1588                 struct ahd_tmode_lstate *lstate;
1589                 struct ccb_hdr_slist *list;
1590                 cam_status status;
1591
1592                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1593                                              &lstate, TRUE);
1594
1595                 if (status != CAM_REQ_CMP) {
1596                         ccb->ccb_h.status = status;
1597                         break;
1598                 }
1599
1600                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1601                         list = &lstate->accept_tios;
1602                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1603                         list = &lstate->immed_notifies;
1604                 else
1605                         list = NULL;
1606
1607                 if (list != NULL) {
1608                         struct ccb_hdr *curelm;
1609                         int found;
1610
1611                         curelm = SLIST_FIRST(list);
1612                         found = 0;
1613                         if (curelm == &abort_ccb->ccb_h) {
1614                                 found = 1;
1615                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1616                         } else {
1617                                 while(curelm != NULL) {
1618                                         struct ccb_hdr *nextelm;
1619
1620                                         nextelm =
1621                                             SLIST_NEXT(curelm, sim_links.sle);
1622
1623                                         if (nextelm == &abort_ccb->ccb_h) {
1624                                                 found = 1;
1625                                                 SLIST_NEXT(curelm,
1626                                                            sim_links.sle) =
1627                                                     SLIST_NEXT(nextelm,
1628                                                                sim_links.sle);
1629                                                 break;
1630                                         }
1631                                         curelm = nextelm;
1632                                 }
1633                         }
1634
1635                         if (found) {
1636                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1637                                 xpt_done(abort_ccb);
1638                                 ccb->ccb_h.status = CAM_REQ_CMP;
1639                         } else {
1640                                 xpt_print_path(abort_ccb->ccb_h.path);
1641                                 printf("Not found\n");
1642                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1643                         }
1644                         break;
1645                 }
1646                 /* FALLTHROUGH */
1647         }
1648 #endif
1649         case XPT_SCSI_IO:
1650                 /* XXX Fully implement the hard ones */
1651                 ccb->ccb_h.status = CAM_UA_ABORT;
1652                 break;
1653         default:
1654                 ccb->ccb_h.status = CAM_REQ_INVALID;
1655                 break;
1656         }
1657         xpt_done(ccb);
1658 }
1659
1660 void
1661 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1662                 u_int lun, ac_code code, void *opt_arg)
1663 {
1664         struct  ccb_trans_settings cts;
1665         struct cam_path *path;
1666         void *arg;
1667         int error;
1668
1669         arg = NULL;
1670         error = ahd_create_path(ahd, channel, target, lun, &path);
1671
1672         if (error != CAM_REQ_CMP)
1673                 return;
1674
1675         switch (code) {
1676         case AC_TRANSFER_NEG:
1677         {
1678 #ifdef AHD_NEW_TRAN_SETTINGS
1679                 struct  ccb_trans_settings_scsi *scsi;
1680         
1681                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1682                 scsi = &cts.proto_specific.scsi;
1683 #else
1684                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1685 #endif
1686                 cts.ccb_h.path = path;
1687                 cts.ccb_h.target_id = target;
1688                 cts.ccb_h.target_lun = lun;
1689                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1690                 arg = &cts;
1691 #ifdef AHD_NEW_TRAN_SETTINGS
1692                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1693                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1694 #else
1695                 cts.valid &= ~CCB_TRANS_TQ_VALID;
1696                 cts.flags &= ~CCB_TRANS_TAG_ENB;
1697 #endif
1698                 if (opt_arg == NULL)
1699                         break;
1700                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1701 #ifdef AHD_NEW_TRAN_SETTINGS
1702                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1703                 scsi->valid |= CTS_SCSI_VALID_TQ;
1704 #else
1705                         cts.flags |= CCB_TRANS_TAG_ENB;
1706                 cts.valid |= CCB_TRANS_TQ_VALID;
1707 #endif
1708                 break;
1709         }
1710         case AC_SENT_BDR:
1711         case AC_BUS_RESET:
1712                 break;
1713         default:
1714                 panic("ahd_send_async: Unexpected async event");
1715         }
1716         xpt_async(code, path, arg);
1717         xpt_free_path(path);
1718 }
1719
1720 void
1721 ahd_platform_set_tags(struct ahd_softc *ahd,
1722                       struct ahd_devinfo *devinfo, int enable)
1723 {
1724 }
1725
1726 int
1727 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1728 {
1729         ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1730                                     M_INTWAIT | M_ZERO);
1731         return (0);
1732 }
1733
1734 void
1735 ahd_platform_free(struct ahd_softc *ahd)
1736 {
1737         struct ahd_platform_data *pdata;
1738
1739         pdata = ahd->platform_data;
1740         if (pdata != NULL) {
1741                 if (pdata->regs[0] != NULL)
1742                         bus_release_resource(ahd->dev_softc,
1743                                              pdata->regs_res_type[0],
1744                                              pdata->regs_res_id[0],
1745                                              pdata->regs[0]);
1746
1747                 if (pdata->regs[1] != NULL)
1748                         bus_release_resource(ahd->dev_softc,
1749                                              pdata->regs_res_type[1],
1750                                              pdata->regs_res_id[1],
1751                                              pdata->regs[1]);
1752
1753                 if (pdata->irq != NULL)
1754                         bus_release_resource(ahd->dev_softc,
1755                                              pdata->irq_res_type,
1756                                              0, pdata->irq);
1757
1758                 if (pdata->sim_b != NULL) {
1759                         xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1760                         xpt_free_path(pdata->path_b);
1761                         xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1762                         cam_sim_free(pdata->sim_b);
1763                 }
1764                 if (pdata->sim != NULL) {
1765                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1766                         xpt_free_path(pdata->path);
1767                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1768                         cam_sim_free(pdata->sim);
1769                 }
1770                 if (pdata->eh != NULL)
1771                         EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1772                 kfree(ahd->platform_data, M_DEVBUF);
1773         }
1774 }
1775
1776 int
1777 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1778 {
1779         /* We don't sort softcs under FreeBSD so report equal always */
1780         return (0);
1781 }
1782
1783 int
1784 ahd_detach(device_t dev)
1785 {
1786         struct ahd_softc *ahd;
1787         u_long l;
1788         u_long s;
1789
1790         ahd_list_lock(&l);
1791         device_printf(dev, "detaching device\n");
1792         ahd = device_get_softc(dev);
1793         ahd = ahd_find_softc(ahd);
1794         if (ahd == NULL) {
1795                 device_printf(dev, "aic7xxx already detached\n");
1796                 ahd_list_unlock(&l);
1797                 return (ENOENT);
1798         }
1799         ahd_lock(ahd, &s);
1800         ahd_intr_enable(ahd, FALSE);
1801         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1802         ahd_unlock(ahd, &s);
1803         ahd_free(ahd);
1804         ahd_list_unlock(&l);
1805         return (0);
1806 }
1807
1808 #if UNUSED
1809 static void
1810 ahd_dump_targcmd(struct target_cmd *cmd)
1811 {
1812         uint8_t *byte;
1813         uint8_t *last_byte;
1814         int i;
1815
1816         byte = &cmd->initiator_channel;
1817         /* Debugging info for received commands */
1818         last_byte = &cmd[1].initiator_channel;
1819
1820         i = 0;
1821         while (byte < last_byte) {
1822                 if (i == 0)
1823                         printf("\t");
1824                 printf("%#x", *byte++);
1825                 i++;
1826                 if (i == 8) {
1827                         printf("\n");
1828                         i = 0;
1829                 } else {
1830                         printf(", ");
1831                 }
1832         }
1833 }
1834 #endif
1835
1836 static int
1837 ahd_modevent(module_t mod, int type, void *data)
1838 {
1839         /* XXX Deal with busy status on unload. */
1840         return 0;
1841 }
1842   
1843 static moduledata_t ahd_mod = {
1844         "ahd",
1845         ahd_modevent,
1846         NULL
1847 };
1848
1849 /********************************** DDB Hooks *********************************/
1850 #ifdef DDB
1851 static struct ahd_softc *ahd_ddb_softc;
1852 static int ahd_ddb_paused;
1853 static int ahd_ddb_paused_on_entry;
1854 DB_COMMAND(ahd_set_unit, ahd_ddb_set_unit)
1855 {
1856         struct ahd_softc *list_ahd;
1857
1858         ahd_ddb_softc = NULL;
1859         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1860                 if (list_ahd->unit == addr)
1861                         ahd_ddb_softc = list_ahd;
1862         }
1863         if (ahd_ddb_softc == NULL)
1864                 db_error("No matching softc found!\n");
1865 }
1866
1867 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1868 {
1869         if (ahd_ddb_softc == NULL) {
1870                 db_error("Must set unit with ahd_set_unit first!\n");
1871                 return;
1872         }
1873         if (ahd_ddb_paused == 0) {
1874                 ahd_ddb_paused++;
1875                 if (ahd_is_paused(ahd_ddb_softc)) {
1876                         ahd_ddb_paused_on_entry++;
1877                         return;
1878                 }
1879                 ahd_pause(ahd_ddb_softc);
1880         }
1881 }
1882
1883 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1884 {
1885         if (ahd_ddb_softc == NULL) {
1886                 db_error("Must set unit with ahd_set_unit first!\n");
1887                 return;
1888         }
1889         if (ahd_ddb_paused != 0) {
1890                 ahd_ddb_paused = 0;
1891                 if (ahd_ddb_paused_on_entry)
1892                         return;
1893                 ahd_unpause(ahd_ddb_softc);
1894         } else if (ahd_ddb_paused_on_entry != 0) {
1895                 /* Two unpauses to clear a paused on entry. */
1896                 ahd_ddb_paused_on_entry = 0;
1897                 ahd_unpause(ahd_ddb_softc);
1898         }
1899 }
1900
1901 DB_COMMAND(ahd_in, ahd_ddb_in)
1902 {
1903         int c;
1904         int size;
1905  
1906         if (ahd_ddb_softc == NULL) {
1907                 db_error("Must set unit with ahd_set_unit first!\n");
1908                 return;
1909         }
1910         if (have_addr == 0)
1911                 return;
1912
1913         size = 1;
1914         while ((c = *modif++) != '\0') {
1915                 switch (c) {
1916                 case 'b':
1917                         size = 1;
1918                         break;
1919                 case 'w':
1920                         size = 2;
1921                         break;
1922                 case 'l':
1923                         size = 4;
1924                 break;
1925                 }
1926         }
1927
1928         if (count <= 0)
1929                 count = 1;
1930         while (--count >= 0) {
1931                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1932                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1933                 switch (size) {
1934                 case 1:
1935                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1936                         break;
1937                 case 2:
1938                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1939                         break;
1940                 case 4:
1941                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1942                         break;
1943                 }
1944         }
1945 }
1946
1947 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1948 {
1949         db_expr_t old_value;
1950         db_expr_t new_value;
1951         int       size;
1952  
1953         if (ahd_ddb_softc == NULL) {
1954                 db_error("Must set unit with ahd_set_unit first!\n");
1955                 return;
1956         }
1957
1958         switch (modif[0]) {
1959         case '\0':
1960         case 'b':
1961                 size = 1;
1962                 break;
1963         case 'h':
1964                 size = 2;
1965                 break;
1966         case 'l':
1967                 size = 4;
1968                 break;
1969         default:
1970                 db_error("Unknown size\n");
1971                 return;
1972         }
1973  
1974         while (db_expression(&new_value)) {
1975                 switch (size) {
1976                 default:
1977                 case 1:
1978                         old_value = ahd_inb(ahd_ddb_softc, addr);
1979                         ahd_outb(ahd_ddb_softc, addr, new_value);
1980                         break;
1981                 case 2:
1982                         old_value = ahd_inw(ahd_ddb_softc, addr);
1983                         ahd_outw(ahd_ddb_softc, addr, new_value);
1984                         break;
1985                 case 4:
1986                         old_value = ahd_inl(ahd_ddb_softc, addr);
1987                         ahd_outl(ahd_ddb_softc, addr, new_value);
1988                         break;
1989                 }
1990                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1991                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1992                           (u_long)old_value, (u_long)new_value);
1993                 addr += size;
1994         }
1995         db_skip_to_eol();
1996 }
1997
1998 #endif
1999
2000
2001 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2002 MODULE_DEPEND(ahd, cam, 1, 1, 1);
2003 MODULE_VERSION(ahd, 1);