Merge from vendor branch HEIMDAL:
[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.6 2004/09/17 03:39:39 joerg 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                                INTR_TYPE_CAM, ahd_platform_intr, ahd,
103                                &ahd->platform_data->ih);
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 s;
1228                                 int error;
1229
1230                                 s = splsoftvm();
1231                                 error = bus_dmamap_load(ahd->buffer_dmat,
1232                                                         scb->dmamap,
1233                                                         csio->data_ptr,
1234                                                         csio->dxfer_len,
1235                                                         ahd_execute_scb,
1236                                                         scb, /*flags*/0);
1237                                 if (error == EINPROGRESS) {
1238                                         /*
1239                                          * So as to maintain ordering,
1240                                          * freeze the controller queue
1241                                          * until our mapping is
1242                                          * returned.
1243                                          */
1244                                         xpt_freeze_simq(sim,
1245                                                         /*count*/1);
1246                                         scb->io_ctx->ccb_h.status |=
1247                                             CAM_RELEASE_SIMQ;
1248                                 }
1249                                 splx(s);
1250                         } else {
1251                                 struct bus_dma_segment seg;
1252
1253                                 /* Pointer to physical buffer */
1254                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1255                                         panic("ahd_setup_data - Transfer size "
1256                                               "larger than can device max");
1257
1258                                 seg.ds_addr =
1259                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1260                                 seg.ds_len = csio->dxfer_len;
1261                                 ahd_execute_scb(scb, &seg, 1, 0);
1262                         }
1263                 } else {
1264                         struct bus_dma_segment *segs;
1265
1266                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1267                                 panic("ahd_setup_data - Physical segment "
1268                                       "pointers unsupported");
1269
1270                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1271                                 panic("ahd_setup_data - Virtual segment "
1272                                       "addresses unsupported");
1273
1274                         /* Just use the segments provided */
1275                         segs = (struct bus_dma_segment *)csio->data_ptr;
1276                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1277                 }
1278         } else {
1279                 ahd_execute_scb(scb, NULL, 0, 0);
1280         }
1281 }
1282
1283 #if NOT_YET
1284 static void
1285 ahd_set_recoveryscb(struct ahd_softc *ahd, struct scb *scb) {
1286
1287         if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1288                 struct scb *list_scb;
1289
1290                 scb->flags |= SCB_RECOVERY_SCB;
1291
1292                 /*
1293                  * Take all queued, but not sent SCBs out of the equation.
1294                  * Also ensure that no new CCBs are queued to us while we
1295                  * try to fix this problem.
1296                  */
1297                 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1298                         xpt_freeze_simq(SCB_GET_SIM(ahd, scb), /*count*/1);
1299                         scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1300                 }
1301
1302                 /*
1303                  * Go through all of our pending SCBs and remove
1304                  * any scheduled timeouts for them.  We will reschedule
1305                  * them after we've successfully fixed this problem.
1306                  */
1307                 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
1308                         union ccb *ccb;
1309
1310                         ccb = list_scb->io_ctx;
1311                         callout_stop(&ccb->ccb_h.timeout_ch);
1312                 }
1313         }
1314 }
1315 #endif
1316
1317 void
1318 ahd_timeout(void *arg)
1319 {
1320         struct  scb       *scb;
1321         struct  ahd_softc *ahd;
1322         ahd_mode_state     saved_modes;
1323         long               s;
1324         int                target;
1325         int                lun;
1326         char               channel;
1327
1328 #if NOT_YET
1329         int                i;
1330         int                found;
1331         u_int              last_phase;
1332 #endif
1333
1334         scb = (struct scb *)arg; 
1335         ahd = (struct ahd_softc *)scb->ahd_softc;
1336
1337         ahd_lock(ahd, &s);
1338
1339         ahd_pause_and_flushwork(ahd);
1340
1341         saved_modes = ahd_save_modes(ahd);
1342 #if 0
1343         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1344         ahd_outb(ahd, SCSISIGO, ACKO);
1345         printf("set ACK\n");
1346         ahd_outb(ahd, SCSISIGO, 0);
1347         printf("clearing Ack\n");
1348         ahd_restore_modes(ahd, saved_modes);
1349 #endif
1350         if ((scb->flags & SCB_ACTIVE) == 0) {
1351                 /* Previous timeout took care of me already */
1352                 printf("%s: Timedout SCB already complete. "
1353                        "Interrupts may not be functioning.\n", ahd_name(ahd));
1354                 ahd_unpause(ahd);
1355                 ahd_unlock(ahd, &s);
1356                 return;
1357         }
1358
1359         target = SCB_GET_TARGET(ahd, scb);
1360         channel = SCB_GET_CHANNEL(ahd, scb);
1361         lun = SCB_GET_LUN(scb);
1362
1363         ahd_print_path(ahd, scb);
1364         printf("SCB 0x%x - timed out\n", SCB_GET_TAG(scb));
1365         ahd_dump_card_state(ahd);
1366         ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
1367                           /*initiate reset*/TRUE);
1368         ahd_unlock(ahd, &s);
1369         return;
1370 #if NOT_YET
1371         last_phase = ahd_inb(ahd, LASTPHASE);
1372         if (scb->sg_count > 0) {
1373                 for (i = 0; i < scb->sg_count; i++) {
1374                         printf("sg[%d] - Addr 0x%x : Length %d\n",
1375                                i,
1376                                ((struct ahd_dma_seg *)scb->sg_list)[i].addr,
1377                                ((struct ahd_dma_seg *)scb->sg_list)[i].len
1378                                 & AHD_SG_LEN_MASK);
1379                 }
1380         }
1381         if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1382                 /*
1383                  * Been down this road before.
1384                  * Do a full bus reset.
1385                  */
1386 bus_reset:
1387                 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1388                 found = ahd_reset_channel(ahd, channel, /*Initiate Reset*/TRUE);
1389                 printf("%s: Issued Channel %c Bus Reset. "
1390                        "%d SCBs aborted\n", ahd_name(ahd), channel, found);
1391         } else {
1392                 /*
1393                  * If we are a target, transition to bus free and report
1394                  * the timeout.
1395                  * 
1396                  * The target/initiator that is holding up the bus may not
1397                  * be the same as the one that triggered this timeout
1398                  * (different commands have different timeout lengths).
1399                  * If the bus is idle and we are actiing as the initiator
1400                  * for this request, queue a BDR message to the timed out
1401                  * target.  Otherwise, if the timed out transaction is
1402                  * active:
1403                  *   Initiator transaction:
1404                  *      Stuff the message buffer with a BDR message and assert
1405                  *      ATN in the hopes that the target will let go of the bus
1406                  *      and go to the mesgout phase.  If this fails, we'll
1407                  *      get another timeout 2 seconds later which will attempt
1408                  *      a bus reset.
1409                  *
1410                  *   Target transaction:
1411                  *      Transition to BUS FREE and report the error.
1412                  *      It's good to be the target!
1413                  */
1414                 u_int active_scb_index;
1415                 u_int saved_scbptr;
1416
1417                 saved_scbptr = ahd_get_scbptr(ahd);
1418                 active_scb_index = saved_scbptr;
1419
1420                 if (last_phase != P_BUSFREE 
1421                   && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1422                   && (active_scb_index < ahd->scb_data.numscbs)) {
1423                         struct scb *active_scb;
1424
1425                         /*
1426                          * If the active SCB is not us, assume that
1427                          * the active SCB has a longer timeout than
1428                          * the timedout SCB, and wait for the active
1429                          * SCB to timeout.
1430                          */ 
1431                         active_scb = ahd_lookup_scb(ahd, active_scb_index);
1432                         if (active_scb != scb) {
1433                                 struct   ccb_hdr *ccbh;
1434                                 uint64_t newtimeout;
1435
1436                                 ahd_print_path(ahd, scb);
1437                                 printf("Other SCB Timeout%s",
1438                                        (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1439                                        ? " again\n" : "\n");
1440                                 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1441                                 newtimeout =
1442                                     MAX(active_scb->io_ctx->ccb_h.timeout,
1443                                         scb->io_ctx->ccb_h.timeout);
1444                                 newtimeout *= hz;
1445                                 newtimeout /= 1000;
1446                                 ccbh = &scb->io_ctx->ccb_h;
1447                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1448                                     newtimeout, ahd_timeout, scb);
1449                                 ahd_unpause(ahd);
1450                                 ahd_unlock(ahd, &s);
1451                                 return;
1452                         } 
1453
1454                         /* It's us */
1455                         if ((scb->hscb->control & TARGET_SCB) != 0) {
1456
1457                                 /*
1458                                  * Send back any queued up transactions
1459                                  * and properly record the error condition.
1460                                  */
1461                                 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1462                                                SCB_GET_CHANNEL(ahd, scb),
1463                                                SCB_GET_LUN(scb),
1464                                                SCB_GET_TAG(scb),
1465                                                ROLE_TARGET,
1466                                                CAM_CMD_TIMEOUT);
1467
1468                                 /* Will clear us from the bus */
1469                                 ahd_restart(ahd);
1470                                 ahd_unlock(ahd, &s);
1471                                 return;
1472                         }
1473
1474                         ahd_set_recoveryscb(ahd, active_scb);
1475                         ahd_outb(ahd, MSG_OUT, HOST_MSG);
1476                         ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
1477                         ahd_print_path(ahd, active_scb);
1478                         printf("BDR message in message buffer\n");
1479                         active_scb->flags |= SCB_DEVICE_RESET;
1480                         callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1481                             2 * hz, ahd_timeout, active_scb);
1482                         ahd_unpause(ahd);
1483                 } else {
1484                         int      disconnected;
1485
1486                         /* XXX Shouldn't panic.  Just punt instead? */
1487                         if ((scb->hscb->control & TARGET_SCB) != 0)
1488                                 panic("Timed-out target SCB but bus idle");
1489
1490                         if (last_phase != P_BUSFREE
1491                          && (ahd_inb(ahd, SSTAT0) & TARGET) != 0) {
1492                                 /* XXX What happened to the SCB? */
1493                                 /* Hung target selection.  Goto busfree */
1494                                 printf("%s: Hung target selection\n",
1495                                        ahd_name(ahd));
1496                                 ahd_restart(ahd);
1497                                 ahd_unlock(ahd, &s);
1498                                 return;
1499                         }
1500
1501                         if (ahd_search_qinfifo(ahd, target, channel, lun,
1502                                                SCB_GET_TAG(scb), ROLE_INITIATOR,
1503                                                /*status*/0, SEARCH_COUNT) > 0) {
1504                                 disconnected = FALSE;
1505                         } else {
1506                                 disconnected = TRUE;
1507                         }
1508
1509                         if (disconnected) {
1510
1511                                 ahd_set_recoveryscb(ahd, scb);
1512                                 /*
1513                                  * Actually re-queue this SCB in an attempt
1514                                  * to select the device before it reconnects.
1515                                  * In either case (selection or reselection),
1516                                  * we will now issue a target reset to the
1517                                  * timed-out device.
1518                                  *
1519                                  * Set the MK_MESSAGE control bit indicating
1520                                  * that we desire to send a message.  We
1521                                  * also set the disconnected flag since
1522                                  * in the paging case there is no guarantee
1523                                  * that our SCB control byte matches the
1524                                  * version on the card.  We don't want the
1525                                  * sequencer to abort the command thinking
1526                                  * an unsolicited reselection occurred.
1527                                  */
1528                                 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1529                                 scb->flags |= SCB_DEVICE_RESET;
1530
1531                                 /*
1532                                  * The sequencer will never re-reference the
1533                                  * in-core SCB.  To make sure we are notified
1534                                  * during reslection, set the MK_MESSAGE flag
1535                                  * in the card's copy of the SCB.
1536                                  */
1537                                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1538                                 ahd_outb(ahd, SCB_CONTROL,
1539                                          ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
1540
1541                                 /*
1542                                  * Clear out any entries in the QINFIFO first
1543                                  * so we are the next SCB for this target
1544                                  * to run.
1545                                  */
1546                                 ahd_search_qinfifo(ahd,
1547                                                    SCB_GET_TARGET(ahd, scb),
1548                                                    channel, SCB_GET_LUN(scb),
1549                                                    SCB_LIST_NULL,
1550                                                    ROLE_INITIATOR,
1551                                                    CAM_REQUEUE_REQ,
1552                                                    SEARCH_COMPLETE);
1553                                 ahd_print_path(ahd, scb);
1554                                 printf("Queuing a BDR SCB\n");
1555                                 ahd_qinfifo_requeue_tail(ahd, scb);
1556                                 ahd_set_scbptr(ahd, saved_scbptr);
1557                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1558                                     2 * hz, ahd_timeout, scb);
1559                                 ahd_unpause(ahd);
1560                         } else {
1561                                 /* Go "immediatly" to the bus reset */
1562                                 /* This shouldn't happen */
1563                                 ahd_set_recoveryscb(ahd, scb);
1564                                 ahd_print_path(ahd, scb);
1565                                 printf("SCB %d: Immediate reset.  "
1566                                         "Flags = 0x%x\n", SCB_GET_TAG(scb),
1567                                         scb->flags);
1568                                 goto bus_reset;
1569                         }
1570                 }
1571         }
1572         ahd_unlock(ahd, &s);
1573 #endif
1574 }
1575
1576 static void
1577 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1578 {
1579         union ccb *abort_ccb;
1580
1581         abort_ccb = ccb->cab.abort_ccb;
1582         switch (abort_ccb->ccb_h.func_code) {
1583 #ifdef AHD_TARGET_MODE
1584         case XPT_ACCEPT_TARGET_IO:
1585         case XPT_IMMED_NOTIFY:
1586         case XPT_CONT_TARGET_IO:
1587         {
1588                 struct ahd_tmode_tstate *tstate;
1589                 struct ahd_tmode_lstate *lstate;
1590                 struct ccb_hdr_slist *list;
1591                 cam_status status;
1592
1593                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1594                                              &lstate, TRUE);
1595
1596                 if (status != CAM_REQ_CMP) {
1597                         ccb->ccb_h.status = status;
1598                         break;
1599                 }
1600
1601                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1602                         list = &lstate->accept_tios;
1603                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1604                         list = &lstate->immed_notifies;
1605                 else
1606                         list = NULL;
1607
1608                 if (list != NULL) {
1609                         struct ccb_hdr *curelm;
1610                         int found;
1611
1612                         curelm = SLIST_FIRST(list);
1613                         found = 0;
1614                         if (curelm == &abort_ccb->ccb_h) {
1615                                 found = 1;
1616                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1617                         } else {
1618                                 while(curelm != NULL) {
1619                                         struct ccb_hdr *nextelm;
1620
1621                                         nextelm =
1622                                             SLIST_NEXT(curelm, sim_links.sle);
1623
1624                                         if (nextelm == &abort_ccb->ccb_h) {
1625                                                 found = 1;
1626                                                 SLIST_NEXT(curelm,
1627                                                            sim_links.sle) =
1628                                                     SLIST_NEXT(nextelm,
1629                                                                sim_links.sle);
1630                                                 break;
1631                                         }
1632                                         curelm = nextelm;
1633                                 }
1634                         }
1635
1636                         if (found) {
1637                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1638                                 xpt_done(abort_ccb);
1639                                 ccb->ccb_h.status = CAM_REQ_CMP;
1640                         } else {
1641                                 xpt_print_path(abort_ccb->ccb_h.path);
1642                                 printf("Not found\n");
1643                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1644                         }
1645                         break;
1646                 }
1647                 /* FALLTHROUGH */
1648         }
1649 #endif
1650         case XPT_SCSI_IO:
1651                 /* XXX Fully implement the hard ones */
1652                 ccb->ccb_h.status = CAM_UA_ABORT;
1653                 break;
1654         default:
1655                 ccb->ccb_h.status = CAM_REQ_INVALID;
1656                 break;
1657         }
1658         xpt_done(ccb);
1659 }
1660
1661 void
1662 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1663                 u_int lun, ac_code code, void *opt_arg)
1664 {
1665         struct  ccb_trans_settings cts;
1666         struct cam_path *path;
1667         void *arg;
1668         int error;
1669
1670         arg = NULL;
1671         error = ahd_create_path(ahd, channel, target, lun, &path);
1672
1673         if (error != CAM_REQ_CMP)
1674                 return;
1675
1676         switch (code) {
1677         case AC_TRANSFER_NEG:
1678         {
1679 #ifdef AHD_NEW_TRAN_SETTINGS
1680                 struct  ccb_trans_settings_scsi *scsi;
1681         
1682                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1683                 scsi = &cts.proto_specific.scsi;
1684 #else
1685                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1686 #endif
1687                 cts.ccb_h.path = path;
1688                 cts.ccb_h.target_id = target;
1689                 cts.ccb_h.target_lun = lun;
1690                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1691                 arg = &cts;
1692 #ifdef AHD_NEW_TRAN_SETTINGS
1693                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1694                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1695 #else
1696                 cts.valid &= ~CCB_TRANS_TQ_VALID;
1697                 cts.flags &= ~CCB_TRANS_TAG_ENB;
1698 #endif
1699                 if (opt_arg == NULL)
1700                         break;
1701                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1702 #ifdef AHD_NEW_TRAN_SETTINGS
1703                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1704                 scsi->valid |= CTS_SCSI_VALID_TQ;
1705 #else
1706                         cts.flags |= CCB_TRANS_TAG_ENB;
1707                 cts.valid |= CCB_TRANS_TQ_VALID;
1708 #endif
1709                 break;
1710         }
1711         case AC_SENT_BDR:
1712         case AC_BUS_RESET:
1713                 break;
1714         default:
1715                 panic("ahd_send_async: Unexpected async event");
1716         }
1717         xpt_async(code, path, arg);
1718         xpt_free_path(path);
1719 }
1720
1721 void
1722 ahd_platform_set_tags(struct ahd_softc *ahd,
1723                       struct ahd_devinfo *devinfo, int enable)
1724 {
1725 }
1726
1727 int
1728 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1729 {
1730         ahd->platform_data = malloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1731                                     M_INTWAIT | M_ZERO);
1732         return (0);
1733 }
1734
1735 void
1736 ahd_platform_free(struct ahd_softc *ahd)
1737 {
1738         struct ahd_platform_data *pdata;
1739
1740         pdata = ahd->platform_data;
1741         if (pdata != NULL) {
1742                 if (pdata->regs[0] != NULL)
1743                         bus_release_resource(ahd->dev_softc,
1744                                              pdata->regs_res_type[0],
1745                                              pdata->regs_res_id[0],
1746                                              pdata->regs[0]);
1747
1748                 if (pdata->regs[1] != NULL)
1749                         bus_release_resource(ahd->dev_softc,
1750                                              pdata->regs_res_type[1],
1751                                              pdata->regs_res_id[1],
1752                                              pdata->regs[1]);
1753
1754                 if (pdata->irq != NULL)
1755                         bus_release_resource(ahd->dev_softc,
1756                                              pdata->irq_res_type,
1757                                              0, pdata->irq);
1758
1759                 if (pdata->sim_b != NULL) {
1760                         xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1761                         xpt_free_path(pdata->path_b);
1762                         xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1763                         cam_sim_free(pdata->sim_b);
1764                 }
1765                 if (pdata->sim != NULL) {
1766                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1767                         xpt_free_path(pdata->path);
1768                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1769                         cam_sim_free(pdata->sim);
1770                 }
1771                 if (pdata->eh != NULL)
1772                         EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1773                 free(ahd->platform_data, M_DEVBUF);
1774         }
1775 }
1776
1777 int
1778 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1779 {
1780         /* We don't sort softcs under FreeBSD so report equal always */
1781         return (0);
1782 }
1783
1784 int
1785 ahd_detach(device_t dev)
1786 {
1787         struct ahd_softc *ahd;
1788         u_long l;
1789         u_long s;
1790
1791         ahd_list_lock(&l);
1792         device_printf(dev, "detaching device\n");
1793         ahd = device_get_softc(dev);
1794         ahd = ahd_find_softc(ahd);
1795         if (ahd == NULL) {
1796                 device_printf(dev, "aic7xxx already detached\n");
1797                 ahd_list_unlock(&l);
1798                 return (ENOENT);
1799         }
1800         ahd_lock(ahd, &s);
1801         ahd_intr_enable(ahd, FALSE);
1802         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1803         ahd_unlock(ahd, &s);
1804         ahd_free(ahd);
1805         ahd_list_unlock(&l);
1806         return (0);
1807 }
1808
1809 #if UNUSED
1810 static void
1811 ahd_dump_targcmd(struct target_cmd *cmd)
1812 {
1813         uint8_t *byte;
1814         uint8_t *last_byte;
1815         int i;
1816
1817         byte = &cmd->initiator_channel;
1818         /* Debugging info for received commands */
1819         last_byte = &cmd[1].initiator_channel;
1820
1821         i = 0;
1822         while (byte < last_byte) {
1823                 if (i == 0)
1824                         printf("\t");
1825                 printf("%#x", *byte++);
1826                 i++;
1827                 if (i == 8) {
1828                         printf("\n");
1829                         i = 0;
1830                 } else {
1831                         printf(", ");
1832                 }
1833         }
1834 }
1835 #endif
1836
1837 static int
1838 ahd_modevent(module_t mod, int type, void *data)
1839 {
1840         /* XXX Deal with busy status on unload. */
1841         return 0;
1842 }
1843   
1844 static moduledata_t ahd_mod = {
1845         "ahd",
1846         ahd_modevent,
1847         NULL
1848 };
1849
1850 /********************************** DDB Hooks *********************************/
1851 #ifdef DDB
1852 static struct ahd_softc *ahd_ddb_softc;
1853 static int ahd_ddb_paused;
1854 static int ahd_ddb_paused_on_entry;
1855 DB_COMMAND(ahd_set_unit, ahd_ddb_set_unit)
1856 {
1857         struct ahd_softc *list_ahd;
1858
1859         ahd_ddb_softc = NULL;
1860         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1861                 if (list_ahd->unit == addr)
1862                         ahd_ddb_softc = list_ahd;
1863         }
1864         if (ahd_ddb_softc == NULL)
1865                 db_error("No matching softc found!\n");
1866 }
1867
1868 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1869 {
1870         if (ahd_ddb_softc == NULL) {
1871                 db_error("Must set unit with ahd_set_unit first!\n");
1872                 return;
1873         }
1874         if (ahd_ddb_paused == 0) {
1875                 ahd_ddb_paused++;
1876                 if (ahd_is_paused(ahd_ddb_softc)) {
1877                         ahd_ddb_paused_on_entry++;
1878                         return;
1879                 }
1880                 ahd_pause(ahd_ddb_softc);
1881         }
1882 }
1883
1884 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1885 {
1886         if (ahd_ddb_softc == NULL) {
1887                 db_error("Must set unit with ahd_set_unit first!\n");
1888                 return;
1889         }
1890         if (ahd_ddb_paused != 0) {
1891                 ahd_ddb_paused = 0;
1892                 if (ahd_ddb_paused_on_entry)
1893                         return;
1894                 ahd_unpause(ahd_ddb_softc);
1895         } else if (ahd_ddb_paused_on_entry != 0) {
1896                 /* Two unpauses to clear a paused on entry. */
1897                 ahd_ddb_paused_on_entry = 0;
1898                 ahd_unpause(ahd_ddb_softc);
1899         }
1900 }
1901
1902 DB_COMMAND(ahd_in, ahd_ddb_in)
1903 {
1904         int c;
1905         int size;
1906  
1907         if (ahd_ddb_softc == NULL) {
1908                 db_error("Must set unit with ahd_set_unit first!\n");
1909                 return;
1910         }
1911         if (have_addr == 0)
1912                 return;
1913
1914         size = 1;
1915         while ((c = *modif++) != '\0') {
1916                 switch (c) {
1917                 case 'b':
1918                         size = 1;
1919                         break;
1920                 case 'w':
1921                         size = 2;
1922                         break;
1923                 case 'l':
1924                         size = 4;
1925                 break;
1926                 }
1927         }
1928
1929         if (count <= 0)
1930                 count = 1;
1931         while (--count >= 0) {
1932                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1933                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1934                 switch (size) {
1935                 case 1:
1936                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1937                         break;
1938                 case 2:
1939                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1940                         break;
1941                 case 4:
1942                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1943                         break;
1944                 }
1945         }
1946 }
1947
1948 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1949 {
1950         db_expr_t old_value;
1951         db_expr_t new_value;
1952         int       size;
1953  
1954         if (ahd_ddb_softc == NULL) {
1955                 db_error("Must set unit with ahd_set_unit first!\n");
1956                 return;
1957         }
1958
1959         switch (modif[0]) {
1960         case '\0':
1961         case 'b':
1962                 size = 1;
1963                 break;
1964         case 'h':
1965                 size = 2;
1966                 break;
1967         case 'l':
1968                 size = 4;
1969                 break;
1970         default:
1971                 db_error("Unknown size\n");
1972                 return;
1973         }
1974  
1975         while (db_expression(&new_value)) {
1976                 switch (size) {
1977                 default:
1978                 case 1:
1979                         old_value = ahd_inb(ahd_ddb_softc, addr);
1980                         ahd_outb(ahd_ddb_softc, addr, new_value);
1981                         break;
1982                 case 2:
1983                         old_value = ahd_inw(ahd_ddb_softc, addr);
1984                         ahd_outw(ahd_ddb_softc, addr, new_value);
1985                         break;
1986                 case 4:
1987                         old_value = ahd_inl(ahd_ddb_softc, addr);
1988                         ahd_outl(ahd_ddb_softc, addr, new_value);
1989                         break;
1990                 }
1991                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1992                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1993                           (u_long)old_value, (u_long)new_value);
1994                 addr += size;
1995         }
1996         db_skip_to_eol();
1997 }
1998
1999 #endif
2000
2001
2002 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2003 MODULE_DEPEND(ahd, cam, 1, 1, 1);
2004 MODULE_VERSION(ahd, 1);