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