2a39ef79c031fbcbd5926981b7a6386fdefc2003
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx_osm.c
1 /*
2  * Bus independent FreeBSD shim for the aic79xx based Adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2002, 2004 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#35 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.23 2005/12/04 02:12:40 ru Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.23 2008/02/10 00:01:02 pavalos Exp $
36  */
37
38 #include "aic79xx_osm.h"
39 #include "aic79xx_inline.h"
40
41 #include <sys/kthread.h>
42
43 #include "opt_ddb.h"
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #endif
47
48 #ifndef AHD_TMODE_ENABLE
49 #define AHD_TMODE_ENABLE 0
50 #endif
51
52 #include "aic_osm_lib.c"
53
54 #define ccb_scb_ptr spriv_ptr0
55
56 #if 0
57 static void     ahd_dump_targcmd(struct target_cmd *cmd);
58 #endif
59 static int      ahd_modevent(module_t mod, int type, void *data);
60 static void     ahd_action(struct cam_sim *sim, union ccb *ccb);
61 static void     ahd_set_tran_settings(struct ahd_softc *ahd,
62                                       int our_id, char channel,
63                                       struct ccb_trans_settings *cts);
64 static void     ahd_get_tran_settings(struct ahd_softc *ahd,
65                                       int our_id, char channel,
66                                       struct ccb_trans_settings *cts);
67 static void     ahd_async(void *callback_arg, uint32_t code,
68                           struct cam_path *path, void *arg);
69 static void     ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
70                                 int nsegments, int error);
71 static void     ahd_poll(struct cam_sim *sim);
72 static void     ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
73                                struct ccb_scsiio *csio, struct scb *scb);
74 static void     ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
75                               union ccb *ccb);
76 static int      ahd_create_path(struct ahd_softc *ahd,
77                                 char channel, u_int target, u_int lun,
78                                 struct cam_path **path);
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         path_id = cam_sim_path(ahd->platform_data->sim);
87         return (xpt_create_path(path, /*periph*/NULL,
88                                 path_id, target, lun));
89 }
90
91 int
92 ahd_map_int(struct ahd_softc *ahd)
93 {
94         int error;
95
96         /* Hook up our interrupt handler */
97         error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
98                                INTR_MPSAFE, ahd_platform_intr, ahd,
99                                &ahd->platform_data->ih, NULL);
100         if (error != 0)
101                 device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
102                               error);
103         return (error);
104 }
105
106 /*
107  * Attach all the sub-devices we can find
108  */
109 int
110 ahd_attach(struct ahd_softc *ahd)
111 {
112         char   ahd_info[256];
113         struct ccb_setasync csa;
114         struct cam_sim *sim;
115         struct cam_path *path;
116         int count;
117
118         count = 0;
119         sim = NULL;
120
121         /*
122          * Create a thread to perform all recovery.
123          */
124         if (ahd_spawn_recovery_thread(ahd) != 0)
125                 goto fail;
126
127         ahd_controller_info(ahd, ahd_info);
128         kprintf("%s\n", ahd_info);
129         ahd_lock(ahd);
130
131         /*
132          * Construct our SIM entry
133          */
134         sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
135                             device_get_unit(ahd->dev_softc),
136                             1, AHD_MAX_QUEUE, NULL);
137         if (sim == NULL)
138                 goto fail;
139
140         if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
141                 cam_sim_free(sim);
142                 sim = NULL;
143                 goto fail;
144         }
145         
146         if (xpt_create_path(&path, /*periph*/NULL,
147                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
148                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
149                 xpt_bus_deregister(cam_sim_path(sim));
150                 cam_sim_free(sim);
151                 sim = NULL;
152                 goto fail;
153         }
154                 
155         xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
156         csa.ccb_h.func_code = XPT_SASYNC_CB;
157         csa.event_enable = AC_LOST_DEVICE;
158         csa.callback = ahd_async;
159         csa.callback_arg = sim;
160         xpt_action((union ccb *)&csa);
161         count++;
162
163 fail:
164         ahd->platform_data->sim = sim;
165         ahd->platform_data->path = path;
166         ahd_unlock(ahd);
167         if (count != 0) {
168                 /* We have to wait until after any system dumps... */
169                 ahd->platform_data->eh =
170                     EVENTHANDLER_REGISTER(shutdown_post_sync, ahd_shutdown,
171                                           ahd, SHUTDOWN_PRI_DRIVER);
172                 ahd_intr_enable(ahd, TRUE);
173         }
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_lock(ahd);
188         ahd_intr(ahd);
189         ahd_unlock(ahd);
190 }
191
192 /*
193  * We have an scb which has been processed by the
194  * adaptor, now we look to see how the operation
195  * went.
196  */
197 void
198 ahd_done(struct ahd_softc *ahd, struct scb *scb)
199 {
200         union ccb *ccb;
201
202         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
203                   ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
204
205         ccb = scb->io_ctx;
206         LIST_REMOVE(scb, pending_links);
207         if ((scb->flags & SCB_TIMEDOUT) != 0)
208                 LIST_REMOVE(scb, timedout_links);
209
210         callout_stop(&scb->io_timer);
211
212         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
213                 bus_dmasync_op_t op;
214
215                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
216                         op = BUS_DMASYNC_POSTREAD;
217                 else
218                         op = BUS_DMASYNC_POSTWRITE;
219                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
220                 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
221         }
222
223 #ifdef AHD_TARGET_MODE
224         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
225                 struct cam_path *ccb_path;
226
227                 /*
228                  * If we have finally disconnected, clean up our
229                  * pending device state.
230                  * XXX - There may be error states that cause where
231                  *       we will remain connected.
232                  */
233                 ccb_path = ccb->ccb_h.path;
234                 if (ahd->pending_device != NULL
235                  && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
236
237                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
238                                 ahd->pending_device = NULL;
239                         } else {
240                                 xpt_print_path(ccb->ccb_h.path);
241                                 kprintf("Still disconnected\n");
242                                 ahd_freeze_ccb(ccb);
243                         }
244                 }
245
246                 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
247                         ccb->ccb_h.status |= CAM_REQ_CMP;
248                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
249                 ahd_free_scb(ahd, scb);
250                 xpt_done(ccb);
251                 return;
252         }
253 #endif
254
255         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
256                 struct  scb *list_scb;
257
258                 ahd->scb_data.recovery_scbs--;
259
260                 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
261                  || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
262                         aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
263
264                 if (ahd->scb_data.recovery_scbs == 0) {
265                         /*
266                          * All recovery actions have completed successfully,
267                          * so reinstate the timeouts for all other pending
268                          * commands.
269                          */
270                         LIST_FOREACH(list_scb,
271                                      &ahd->pending_scbs, pending_links) {
272
273                                 aic_scb_timer_reset(list_scb,
274                                                     aic_get_timeout(scb));
275                         }
276
277                         ahd_print_path(ahd, scb);
278                         kprintf("no longer in timeout, status = %x\n",
279                                ccb->ccb_h.status);
280                 }
281         }
282
283         /* Don't clobber any existing error state */
284         if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
285                 ccb->ccb_h.status |= CAM_REQ_CMP;
286         } else if ((scb->flags & SCB_SENSE) != 0) {
287                 /*
288                  * We performed autosense retrieval.
289                  *
290                  * Zero any sense not transferred by the
291                  * device.  The SCSI spec mandates that any
292                  * untransfered data should be assumed to be
293                  * zero.  Complete the 'bounce' of sense information
294                  * through buffers accessible via bus-space by
295                  * copying it into the clients csio.
296                  */
297                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
298                 memcpy(&ccb->csio.sense_data,
299                        ahd_get_sense_buf(ahd, scb),
300 /* XXX What size do we want to use??? */
301                         sizeof(ccb->csio.sense_data)
302                        - ccb->csio.sense_resid);
303                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
304         } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
305                 struct scsi_status_iu_header *siu;
306                 u_int sense_len;
307                 int i;
308
309                 /*
310                  * Copy only the sense data into the provided buffer.
311                  */
312                 siu = (struct scsi_status_iu_header *)scb->sense_data;
313                 sense_len = MIN(scsi_4btoul(siu->sense_length),
314                                 sizeof(ccb->csio.sense_data));
315                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
316                 memcpy(&ccb->csio.sense_data,
317                        ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
318                        sense_len);
319                 kprintf("Copied %d bytes of sense data offset %d:", sense_len,
320                        SIU_SENSE_OFFSET(siu));
321                 for (i = 0; i < sense_len; i++)
322                         kprintf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
323                 kprintf("\n");
324                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
325         }
326         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
327         ahd_free_scb(ahd, scb);
328         xpt_done(ccb);
329 }
330
331 static void
332 ahd_action(struct cam_sim *sim, union ccb *ccb)
333 {
334         struct  ahd_softc *ahd;
335 #ifdef AHD_TARGET_MODE
336         struct  ahd_tmode_lstate *lstate;
337 #endif
338         u_int   target_id;
339         u_int   our_id;
340
341         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
342         
343         ahd = (struct ahd_softc *)cam_sim_softc(sim);
344
345         target_id = ccb->ccb_h.target_id;
346         our_id = SIM_SCSI_ID(ahd, sim);
347         
348         switch (ccb->ccb_h.func_code) {
349         /* Common cases first */
350 #ifdef AHD_TARGET_MODE
351         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
352         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
353         {
354                 struct     ahd_tmode_tstate *tstate;
355                 cam_status status;
356
357                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
358                                              &lstate, TRUE);
359
360                 if (status != CAM_REQ_CMP) {
361                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
362                                 /* Response from the black hole device */
363                                 tstate = NULL;
364                                 lstate = ahd->black_hole;
365                         } else {
366                                 ccb->ccb_h.status = status;
367                                 xpt_done(ccb);
368                                 break;
369                         }
370                 }
371                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
372
373                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
374                                           sim_links.sle);
375                         ccb->ccb_h.status = CAM_REQ_INPROG;
376                         if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
377                                 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
378                         break;
379                 }
380
381                 /*
382                  * The target_id represents the target we attempt to
383                  * select.  In target mode, this is the initiator of
384                  * the original command.
385                  */
386                 our_id = target_id;
387                 target_id = ccb->csio.init_id;
388                 /* FALLTHROUGH */
389         }
390 #endif
391         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
392         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
393         {
394                 struct  scb *scb;
395                 struct  hardware_scb *hscb;     
396                 struct  ahd_initiator_tinfo *tinfo;
397                 struct  ahd_tmode_tstate *tstate;
398                 u_int   col_idx;
399
400                 if ((ahd->flags & AHD_INITIATORROLE) == 0
401                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
402                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
403                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
404                         xpt_done(ccb);
405                         return;
406                 }
407
408                 /*
409                  * get an scb to use.
410                  */
411                 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
412                                             target_id, &tstate);
413                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
414                  || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
415                  || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
416                         col_idx = AHD_NEVER_COL_IDX;
417                 } else {
418                         col_idx = AHD_BUILD_COL_IDX(target_id,
419                                                     ccb->ccb_h.target_lun);
420                 }
421                 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
422         
423                         xpt_freeze_simq(sim, /*count*/1);
424                         ahd->flags |= AHD_RESOURCE_SHORTAGE;
425                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
426                         xpt_done(ccb);
427                         return;
428                 }
429                 
430                 hscb = scb->hscb;
431                 
432                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
433                           ("start scb(%p)\n", scb));
434                 scb->io_ctx = ccb;
435                 /*
436                  * So we can find the SCB when an abort is requested
437                  */
438                 ccb->ccb_h.ccb_scb_ptr = scb;
439
440                 /*
441                  * Put all the arguments for the xfer in the scb
442                  */
443                 hscb->control = 0;
444                 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
445                 hscb->lun = ccb->ccb_h.target_lun;
446                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
447                         hscb->cdb_len = 0;
448                         scb->flags |= SCB_DEVICE_RESET;
449                         hscb->control |= MK_MESSAGE;
450                         hscb->task_management = SIU_TASKMGMT_LUN_RESET;
451                         ahd_execute_scb(scb, NULL, 0, 0);
452                 } else {
453 #ifdef AHD_TARGET_MODE
454                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
455                                 struct target_data *tdata;
456
457                                 tdata = &hscb->shared_data.tdata;
458                                 if (ahd->pending_device == lstate)
459                                         scb->flags |= SCB_TARGET_IMMEDIATE;
460                                 hscb->control |= TARGET_SCB;
461                                 tdata->target_phases = 0;
462                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
463                                         tdata->target_phases |= SPHASE_PENDING;
464                                         tdata->scsi_status =
465                                             ccb->csio.scsi_status;
466                                 }
467                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
468                                         tdata->target_phases |= NO_DISCONNECT;
469
470                                 tdata->initiator_tag =
471                                     ahd_htole16(ccb->csio.tag_id);
472                         }
473 #endif
474                         hscb->task_management = 0;
475                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
476                                 hscb->control |= ccb->csio.tag_action;
477                         
478                         ahd_setup_data(ahd, sim, &ccb->csio, scb);
479                 }
480                 break;
481         }
482 #ifdef AHD_TARGET_MODE
483         case XPT_NOTIFY_ACK:
484         case XPT_IMMED_NOTIFY:
485         {
486                 struct     ahd_tmode_tstate *tstate;
487                 struct     ahd_tmode_lstate *lstate;
488                 cam_status status;
489
490                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
491                                              &lstate, TRUE);
492
493                 if (status != CAM_REQ_CMP) {
494                         ccb->ccb_h.status = status;
495                         xpt_done(ccb);
496                         break;
497                 }
498                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
499                                   sim_links.sle);
500                 ccb->ccb_h.status = CAM_REQ_INPROG;
501                 ahd_send_lstate_events(ahd, lstate);
502                 break;
503         }
504         case XPT_EN_LUN:                /* Enable LUN as a target */
505                 ahd_handle_en_lun(ahd, sim, ccb);
506                 xpt_done(ccb);
507                 break;
508 #endif
509         case XPT_ABORT:                 /* Abort the specified CCB */
510         {
511                 ahd_abort_ccb(ahd, sim, ccb);
512                 break;
513         }
514         case XPT_SET_TRAN_SETTINGS:
515         {
516                 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
517                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
518                 xpt_done(ccb);
519                 break;
520         }
521         case XPT_GET_TRAN_SETTINGS:
522         /* Get default/user set transfer settings for the target */
523         {
524                 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
525                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
526                 xpt_done(ccb);
527                 break;
528         }
529         case XPT_CALC_GEOMETRY:
530         {
531                 cam_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
532                 xpt_done(ccb);
533                 break;
534         }
535         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
536         {
537                 int  found;
538                 
539                 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
540                                           /*initiate reset*/TRUE);
541                 if (bootverbose) {
542                         xpt_print_path(SIM_PATH(ahd, sim));
543                         kprintf("SCSI bus reset delivered. "
544                                "%d SCBs aborted.\n", found);
545                 }
546                 ccb->ccb_h.status = CAM_REQ_CMP;
547                 xpt_done(ccb);
548                 break;
549         }
550         case XPT_TERM_IO:               /* Terminate the I/O process */
551                 /* XXX Implement */
552                 ccb->ccb_h.status = CAM_REQ_INVALID;
553                 xpt_done(ccb);
554                 break;
555         case XPT_PATH_INQ:              /* Path routing inquiry */
556         {
557                 struct ccb_pathinq *cpi = &ccb->cpi;
558                 
559                 cpi->version_num = 1; /* XXX??? */
560                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
561                 if ((ahd->features & AHD_WIDE) != 0)
562                         cpi->hba_inquiry |= PI_WIDE_16;
563                 if ((ahd->features & AHD_TARGETMODE) != 0) {
564                         cpi->target_sprt = PIT_PROCESSOR
565                                          | PIT_DISCONNECT
566                                          | PIT_TERM_IO;
567                 } else {
568                         cpi->target_sprt = 0;
569                 }
570                 cpi->hba_misc = 0;
571                 cpi->hba_eng_cnt = 0;
572                 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
573                 cpi->max_lun = AHD_NUM_LUNS_NONPKT - 1;
574                 cpi->initiator_id = ahd->our_id;
575                 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
576                         cpi->hba_misc |= PIM_NOBUSRESET;
577                 }
578                 cpi->bus_id = cam_sim_bus(sim);
579                 cpi->base_transfer_speed = 3300;
580                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
581                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
582                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
583                 cpi->unit_number = cam_sim_unit(sim);
584                 cpi->protocol = PROTO_SCSI;
585                 cpi->protocol_version = SCSI_REV_2;
586                 cpi->transport = XPORT_SPI;
587                 cpi->transport_version = 2;
588                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
589                 cpi->transport_version = 4;
590                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
591                 cpi->ccb_h.status = CAM_REQ_CMP;
592                 xpt_done(ccb);
593                 break;
594         }
595         default:
596                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
597                 xpt_done(ccb);
598                 break;
599         }
600 }
601
602
603 static void
604 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
605                       struct ccb_trans_settings *cts)
606 {
607         struct    ahd_devinfo devinfo;
608         struct    ccb_trans_settings_scsi *scsi;
609         struct    ccb_trans_settings_spi *spi;
610         struct    ahd_initiator_tinfo *tinfo;
611         struct    ahd_tmode_tstate *tstate;
612         uint16_t *discenable;
613         uint16_t *tagenable;
614         u_int     update_type;
615
616         scsi = &cts->proto_specific.scsi;
617         spi = &cts->xport_specific.spi;
618         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
619                             cts->ccb_h.target_id,
620                             cts->ccb_h.target_lun,
621                             SIM_CHANNEL(ahd, sim),
622                             ROLE_UNKNOWN);
623         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
624                                     devinfo.our_scsiid,
625                                     devinfo.target, &tstate);
626         update_type = 0;
627         if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
628                 update_type |= AHD_TRANS_GOAL;
629                 discenable = &tstate->discenable;
630                 tagenable = &tstate->tagenable;
631                 tinfo->curr.protocol_version = cts->protocol_version;
632                 tinfo->curr.transport_version = cts->transport_version;
633                 tinfo->goal.protocol_version = cts->protocol_version;
634                 tinfo->goal.transport_version = cts->transport_version;
635         } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
636                 update_type |= AHD_TRANS_USER;
637                 discenable = &ahd->user_discenable;
638                 tagenable = &ahd->user_tagenable;
639                 tinfo->user.protocol_version = cts->protocol_version;
640                 tinfo->user.transport_version = cts->transport_version;
641         } else {
642                 cts->ccb_h.status = CAM_REQ_INVALID;
643                 return;
644         }
645         
646         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
647                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
648                         *discenable |= devinfo.target_mask;
649                 else
650                         *discenable &= ~devinfo.target_mask;
651         }
652         
653         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
654                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
655                         *tagenable |= devinfo.target_mask;
656                 else
657                         *tagenable &= ~devinfo.target_mask;
658         }       
659
660         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
661                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
662                                    &spi->bus_width, ROLE_UNKNOWN);
663                 ahd_set_width(ahd, &devinfo, spi->bus_width,
664                               update_type, /*paused*/FALSE);
665         }
666
667         if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
668                 if (update_type == AHD_TRANS_USER)
669                         spi->ppr_options = tinfo->user.ppr_options;
670                 else
671                         spi->ppr_options = tinfo->goal.ppr_options;
672         }
673
674         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
675                 if (update_type == AHD_TRANS_USER)
676                         spi->sync_offset = tinfo->user.offset;
677                 else
678                         spi->sync_offset = tinfo->goal.offset;
679         }
680
681         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
682                 if (update_type == AHD_TRANS_USER)
683                         spi->sync_period = tinfo->user.period;
684                 else
685                         spi->sync_period = tinfo->goal.period;
686         }
687
688         if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
689          || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
690                 u_int   maxsync;
691
692                 maxsync = AHD_SYNCRATE_MAX;
693
694                 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
695                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
696
697                 if ((*discenable & devinfo.target_mask) == 0)
698                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
699
700                 ahd_find_syncrate(ahd, &spi->sync_period,
701                                   &spi->ppr_options, maxsync);
702                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
703                                     spi->sync_period, &spi->sync_offset,
704                                     spi->bus_width, ROLE_UNKNOWN);
705
706                 /* We use a period of 0 to represent async */
707                 if (spi->sync_offset == 0) {
708                         spi->sync_period = 0;
709                         spi->ppr_options = 0;
710                 }
711
712                 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
713                                  spi->sync_offset, spi->ppr_options,
714                                  update_type, /*paused*/FALSE);
715         }
716         cts->ccb_h.status = CAM_REQ_CMP;
717 }
718
719 static void
720 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
721                       struct ccb_trans_settings *cts)
722 {
723         struct  ahd_devinfo devinfo;
724         struct  ccb_trans_settings_scsi *scsi;
725         struct  ccb_trans_settings_spi *spi;
726         struct  ahd_initiator_tinfo *targ_info;
727         struct  ahd_tmode_tstate *tstate;
728         struct  ahd_transinfo *tinfo;
729
730         scsi = &cts->proto_specific.scsi;
731         spi = &cts->xport_specific.spi;
732         ahd_compile_devinfo(&devinfo, our_id,
733                             cts->ccb_h.target_id,
734                             cts->ccb_h.target_lun,
735                             channel, ROLE_UNKNOWN);
736         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
737                                         devinfo.our_scsiid,
738                                         devinfo.target, &tstate);
739         
740         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
741                 tinfo = &targ_info->curr;
742         else
743                 tinfo = &targ_info->user;
744         
745         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
746         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
747         if (cts->type == CTS_TYPE_USER_SETTINGS) {
748                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
749                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
750
751                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
752                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
753         } else {
754                 if ((tstate->discenable & devinfo.target_mask) != 0)
755                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
756
757                 if ((tstate->tagenable & devinfo.target_mask) != 0)
758                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
759         }
760         cts->protocol_version = tinfo->protocol_version;
761         cts->transport_version = tinfo->transport_version;
762
763         spi->sync_period = tinfo->period;
764         spi->sync_offset = tinfo->offset;
765         spi->bus_width = tinfo->width;
766         spi->ppr_options = tinfo->ppr_options;
767         
768         cts->protocol = PROTO_SCSI;
769         cts->transport = XPORT_SPI;
770         spi->valid = CTS_SPI_VALID_SYNC_RATE
771                    | CTS_SPI_VALID_SYNC_OFFSET
772                    | CTS_SPI_VALID_BUS_WIDTH
773                    | CTS_SPI_VALID_PPR_OPTIONS;
774
775         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
776                 scsi->valid = CTS_SCSI_VALID_TQ;
777                 spi->valid |= CTS_SPI_VALID_DISC;
778         } else {
779                 scsi->valid = 0;
780         }
781
782         cts->ccb_h.status = CAM_REQ_CMP;
783 }
784
785 static void
786 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
787 {
788         struct ahd_softc *ahd;
789         struct cam_sim *sim;
790
791         sim = (struct cam_sim *)callback_arg;
792         ahd = (struct ahd_softc *)cam_sim_softc(sim);
793         switch (code) {
794         case AC_LOST_DEVICE:
795         {
796                 struct  ahd_devinfo devinfo;
797
798                 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
799                                     xpt_path_target_id(path),
800                                     xpt_path_lun_id(path),
801                                     SIM_CHANNEL(ahd, sim),
802                                     ROLE_UNKNOWN);
803
804                 /*
805                  * Revert to async/narrow transfers
806                  * for the next device.
807                  */
808                 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
809                               AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
810                 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
811                                  /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
812                                  /*paused*/FALSE);
813                 break;
814         }
815         default:
816                 break;
817         }
818 }
819
820 static void
821 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
822                 int error)
823 {
824         struct  scb *scb;
825         union   ccb *ccb;
826         struct  ahd_softc *ahd;
827         struct  ahd_initiator_tinfo *tinfo;
828         struct  ahd_tmode_tstate *tstate;
829         u_int   mask;
830
831         scb = (struct scb *)arg;
832         ccb = scb->io_ctx;
833         ahd = scb->ahd_softc;
834
835         if (error != 0) {
836                 if (error == EFBIG)
837                         aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
838                 else
839                         aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
840                 if (nsegments != 0)
841                         bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
842                 ahd_free_scb(ahd, scb);
843                 xpt_done(ccb);
844                 return;
845         }
846         scb->sg_count = 0;
847         if (nsegments != 0) {
848                 void *sg;
849                 bus_dmasync_op_t op;
850                 u_int i;
851
852                 /* Copy the segments into our SG list */
853                 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
854
855                         sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
856                                           dm_segs->ds_len,
857                                           /*last*/i == 1);
858                         dm_segs++;
859                 }
860                 
861                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
862                         op = BUS_DMASYNC_PREREAD;
863                 else
864                         op = BUS_DMASYNC_PREWRITE;
865
866                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
867
868                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
869                         struct target_data *tdata;
870
871                         tdata = &scb->hscb->shared_data.tdata;
872                         tdata->target_phases |= DPHASE_PENDING;
873                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
874                                 tdata->data_phase = P_DATAOUT;
875                         else
876                                 tdata->data_phase = P_DATAIN;
877                 }
878         }
879
880         /*
881          * Last time we need to check if this SCB needs to
882          * be aborted.
883          */
884         if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
885                 if (nsegments != 0)
886                         bus_dmamap_unload(ahd->buffer_dmat,
887                                           scb->dmamap);
888                 ahd_free_scb(ahd, scb);
889                 xpt_done(ccb);
890                 return;
891         }
892
893         tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
894                                     SCSIID_OUR_ID(scb->hscb->scsiid),
895                                     SCSIID_TARGET(ahd, scb->hscb->scsiid),
896                                     &tstate);
897
898         mask = SCB_GET_TARGET_MASK(ahd, scb);
899
900         if ((tstate->discenable & mask) != 0
901          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
902                 scb->hscb->control |= DISCENB;
903
904         if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
905                 scb->flags |= SCB_PACKETIZED;
906                 if (scb->hscb->task_management != 0)
907                         scb->hscb->control &= ~MK_MESSAGE;
908         }
909
910         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
911          && (tinfo->goal.width != 0
912           || tinfo->goal.period != 0
913           || tinfo->goal.ppr_options != 0)) {
914                 scb->flags |= SCB_NEGOTIATE;
915                 scb->hscb->control |= MK_MESSAGE;
916         } else if ((tstate->auto_negotiate & mask) != 0) {
917                 scb->flags |= SCB_AUTO_NEGOTIATE;
918                 scb->hscb->control |= MK_MESSAGE;
919         }
920
921         LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
922
923         ccb->ccb_h.status |= CAM_SIM_QUEUED;
924
925         aic_scb_timer_start(scb);
926
927         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
928                 /* Define a mapping from our tag to the SCB. */
929                 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
930                 ahd_pause(ahd);
931                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
932                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
933                 ahd_unpause(ahd);
934         } else {
935                 ahd_queue_scb(ahd, scb);
936         }
937 }
938
939 static void
940 ahd_poll(struct cam_sim *sim)
941 {
942         ahd_intr(cam_sim_softc(sim));
943 }
944
945 static void
946 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
947                struct ccb_scsiio *csio, struct scb *scb)
948 {
949         struct hardware_scb *hscb;
950         struct ccb_hdr *ccb_h;
951         
952         hscb = scb->hscb;
953         ccb_h = &csio->ccb_h;
954         
955         csio->resid = 0;
956         csio->sense_resid = 0;
957         if (ccb_h->func_code == XPT_SCSI_IO) {
958                 hscb->cdb_len = csio->cdb_len;
959                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
960
961                         if (hscb->cdb_len > MAX_CDB_LEN
962                          && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
963
964                                 /*
965                                  * Should CAM start to support CDB sizes
966                                  * greater than 16 bytes, we could use
967                                  * the sense buffer to store the CDB.
968                                  */
969                                 aic_set_transaction_status(scb,
970                                                            CAM_REQ_INVALID);
971                                 ahd_free_scb(ahd, scb);
972                                 xpt_done((union ccb *)csio);
973                                 return;
974                         }
975                         if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
976                                 hscb->shared_data.idata.cdb_from_host.cdbptr =
977                                    aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
978                                 hscb->shared_data.idata.cdb_from_host.cdblen =
979                                    csio->cdb_len;
980                                 hscb->cdb_len |= SCB_CDB_LEN_PTR;
981                         } else {
982                                 memcpy(hscb->shared_data.idata.cdb, 
983                                        csio->cdb_io.cdb_ptr,
984                                        hscb->cdb_len);
985                         }
986                 } else {
987                         if (hscb->cdb_len > MAX_CDB_LEN) {
988
989                                 aic_set_transaction_status(scb,
990                                                            CAM_REQ_INVALID);
991                                 ahd_free_scb(ahd, scb);
992                                 xpt_done((union ccb *)csio);
993                                 return;
994                         }
995                         memcpy(hscb->shared_data.idata.cdb,
996                                csio->cdb_io.cdb_bytes, hscb->cdb_len);
997                 }
998         }
999                 
1000         /* Only use S/G if there is a transfer */
1001         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1002                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1003                         /* We've been given a pointer to a single buffer */
1004                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1005                                 int error;
1006
1007                                 crit_enter();
1008                                 error = bus_dmamap_load(ahd->buffer_dmat,
1009                                                         scb->dmamap,
1010                                                         csio->data_ptr,
1011                                                         csio->dxfer_len,
1012                                                         ahd_execute_scb,
1013                                                         scb, /*flags*/0);
1014                                 if (error == EINPROGRESS) {
1015                                         /*
1016                                          * So as to maintain ordering,
1017                                          * freeze the controller queue
1018                                          * until our mapping is
1019                                          * returned.
1020                                          */
1021                                         xpt_freeze_simq(sim,
1022                                                         /*count*/1);
1023                                         scb->io_ctx->ccb_h.status |=
1024                                             CAM_RELEASE_SIMQ;
1025                                 }
1026                                 crit_exit();
1027                         } else {
1028                                 struct bus_dma_segment seg;
1029
1030                                 /* Pointer to physical buffer */
1031                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1032                                         panic("ahd_setup_data - Transfer size "
1033                                               "larger than can device max");
1034
1035                                 seg.ds_addr =
1036                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1037                                 seg.ds_len = csio->dxfer_len;
1038                                 ahd_execute_scb(scb, &seg, 1, 0);
1039                         }
1040                 } else {
1041                         struct bus_dma_segment *segs;
1042
1043                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1044                                 panic("ahd_setup_data - Physical segment "
1045                                       "pointers unsupported");
1046
1047                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1048                                 panic("ahd_setup_data - Virtual segment "
1049                                       "addresses unsupported");
1050
1051                         /* Just use the segments provided */
1052                         segs = (struct bus_dma_segment *)csio->data_ptr;
1053                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1054                 }
1055         } else {
1056                 ahd_execute_scb(scb, NULL, 0, 0);
1057         }
1058 }
1059
1060 static void
1061 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1062 {
1063         union ccb *abort_ccb;
1064
1065         abort_ccb = ccb->cab.abort_ccb;
1066         switch (abort_ccb->ccb_h.func_code) {
1067 #ifdef AHD_TARGET_MODE
1068         case XPT_ACCEPT_TARGET_IO:
1069         case XPT_IMMED_NOTIFY:
1070         case XPT_CONT_TARGET_IO:
1071         {
1072                 struct ahd_tmode_tstate *tstate;
1073                 struct ahd_tmode_lstate *lstate;
1074                 struct ccb_hdr_slist *list;
1075                 cam_status status;
1076
1077                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1078                                              &lstate, TRUE);
1079
1080                 if (status != CAM_REQ_CMP) {
1081                         ccb->ccb_h.status = status;
1082                         break;
1083                 }
1084
1085                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1086                         list = &lstate->accept_tios;
1087                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1088                         list = &lstate->immed_notifies;
1089                 else
1090                         list = NULL;
1091
1092                 if (list != NULL) {
1093                         struct ccb_hdr *curelm;
1094                         int found;
1095
1096                         curelm = SLIST_FIRST(list);
1097                         found = 0;
1098                         if (curelm == &abort_ccb->ccb_h) {
1099                                 found = 1;
1100                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1101                         } else {
1102                                 while(curelm != NULL) {
1103                                         struct ccb_hdr *nextelm;
1104
1105                                         nextelm =
1106                                             SLIST_NEXT(curelm, sim_links.sle);
1107
1108                                         if (nextelm == &abort_ccb->ccb_h) {
1109                                                 found = 1;
1110                                                 SLIST_NEXT(curelm,
1111                                                            sim_links.sle) =
1112                                                     SLIST_NEXT(nextelm,
1113                                                                sim_links.sle);
1114                                                 break;
1115                                         }
1116                                         curelm = nextelm;
1117                                 }
1118                         }
1119
1120                         if (found) {
1121                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1122                                 xpt_done(abort_ccb);
1123                                 ccb->ccb_h.status = CAM_REQ_CMP;
1124                         } else {
1125                                 xpt_print_path(abort_ccb->ccb_h.path);
1126                                 kprintf("Not found\n");
1127                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1128                         }
1129                         break;
1130                 }
1131                 /* FALLTHROUGH */
1132         }
1133 #endif
1134         case XPT_SCSI_IO:
1135                 /* XXX Fully implement the hard ones */
1136                 ccb->ccb_h.status = CAM_UA_ABORT;
1137                 break;
1138         default:
1139                 ccb->ccb_h.status = CAM_REQ_INVALID;
1140                 break;
1141         }
1142         xpt_done(ccb);
1143 }
1144
1145 void
1146 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1147                 u_int lun, ac_code code, void *opt_arg)
1148 {
1149         struct  ccb_trans_settings cts;
1150         struct cam_path *path;
1151         void *arg;
1152         int error;
1153
1154         arg = NULL;
1155         error = ahd_create_path(ahd, channel, target, lun, &path);
1156
1157         if (error != CAM_REQ_CMP)
1158                 return;
1159
1160         switch (code) {
1161         case AC_TRANSFER_NEG:
1162         {
1163                 struct  ccb_trans_settings_scsi *scsi;
1164         
1165                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1166                 scsi = &cts.proto_specific.scsi;
1167                 cts.ccb_h.path = path;
1168                 cts.ccb_h.target_id = target;
1169                 cts.ccb_h.target_lun = lun;
1170                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1171                 arg = &cts;
1172                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1173                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1174                 if (opt_arg == NULL)
1175                         break;
1176                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1177                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1178                 scsi->valid |= CTS_SCSI_VALID_TQ;
1179                 break;
1180         }
1181         case AC_SENT_BDR:
1182         case AC_BUS_RESET:
1183                 break;
1184         default:
1185                 panic("ahd_send_async: Unexpected async event");
1186         }
1187         xpt_async(code, path, arg);
1188         xpt_free_path(path);
1189 }
1190
1191 void
1192 ahd_platform_set_tags(struct ahd_softc *ahd,
1193                       struct ahd_devinfo *devinfo, int enable)
1194 {
1195 }
1196
1197 int
1198 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1199 {
1200         ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1201                                     M_INTWAIT | M_ZERO);
1202         return (0);
1203 }
1204
1205 void
1206 ahd_platform_free(struct ahd_softc *ahd)
1207 {
1208         struct ahd_platform_data *pdata;
1209
1210         pdata = ahd->platform_data;
1211         if (pdata != NULL) {
1212                 if (pdata->regs[0] != NULL)
1213                         bus_release_resource(ahd->dev_softc,
1214                                              pdata->regs_res_type[0],
1215                                              pdata->regs_res_id[0],
1216                                              pdata->regs[0]);
1217
1218                 if (pdata->regs[1] != NULL)
1219                         bus_release_resource(ahd->dev_softc,
1220                                              pdata->regs_res_type[1],
1221                                              pdata->regs_res_id[1],
1222                                              pdata->regs[1]);
1223
1224                 if (pdata->irq != NULL)
1225                         bus_release_resource(ahd->dev_softc,
1226                                              pdata->irq_res_type,
1227                                              0, pdata->irq);
1228
1229                 if (pdata->sim != NULL) {
1230                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1231                         xpt_free_path(pdata->path);
1232                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1233                         cam_sim_free(pdata->sim);
1234                 }
1235                 if (pdata->eh != NULL)
1236                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1237                 kfree(ahd->platform_data, M_DEVBUF);
1238         }
1239 }
1240
1241 int
1242 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1243 {
1244         /* We don't sort softcs under FreeBSD so report equal always */
1245         return (0);
1246 }
1247
1248 int
1249 ahd_detach(device_t dev)
1250 {
1251         struct ahd_softc *ahd;
1252
1253         device_printf(dev, "detaching device\n");
1254         ahd = device_get_softc(dev);
1255         ahd_lock(ahd);
1256         TAILQ_REMOVE(&ahd_tailq, ahd, links);
1257         ahd_intr_enable(ahd, FALSE);
1258         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1259         ahd_unlock(ahd);
1260         ahd_free(ahd);
1261         return (0);
1262 }
1263
1264 #if 0
1265 static void
1266 ahd_dump_targcmd(struct target_cmd *cmd)
1267 {
1268         uint8_t *byte;
1269         uint8_t *last_byte;
1270         int i;
1271
1272         byte = &cmd->initiator_channel;
1273         /* Debugging info for received commands */
1274         last_byte = &cmd[1].initiator_channel;
1275
1276         i = 0;
1277         while (byte < last_byte) {
1278                 if (i == 0)
1279                         kprintf("\t");
1280                 kprintf("%#x", *byte++);
1281                 i++;
1282                 if (i == 8) {
1283                         kprintf("\n");
1284                         i = 0;
1285                 } else {
1286                         kprintf(", ");
1287                 }
1288         }
1289 }
1290 #endif
1291
1292 static int
1293 ahd_modevent(module_t mod, int type, void *data)
1294 {
1295         /* XXX Deal with busy status on unload. */
1296         /* XXX Deal with unknown events */
1297         return 0;
1298 }
1299   
1300 static moduledata_t ahd_mod = {
1301         "ahd",
1302         ahd_modevent,
1303         NULL
1304 };
1305
1306 /********************************** DDB Hooks *********************************/
1307 #ifdef DDB
1308 static struct ahd_softc *ahd_ddb_softc;
1309 static int ahd_ddb_paused;
1310 static int ahd_ddb_paused_on_entry;
1311 DB_COMMAND(ahd_sunit, ahd_ddb_sunit)
1312 {
1313         struct ahd_softc *list_ahd;
1314
1315         ahd_ddb_softc = NULL;
1316         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1317                 if (list_ahd->unit == addr)
1318                         ahd_ddb_softc = list_ahd;
1319         }
1320         if (ahd_ddb_softc == NULL)
1321                 db_error("No matching softc found!\n");
1322 }
1323
1324 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1325 {
1326         if (ahd_ddb_softc == NULL) {
1327                 db_error("Must set unit with ahd_sunit first!\n");
1328                 return;
1329         }
1330         if (ahd_ddb_paused == 0) {
1331                 ahd_ddb_paused++;
1332                 if (ahd_is_paused(ahd_ddb_softc)) {
1333                         ahd_ddb_paused_on_entry++;
1334                         return;
1335                 }
1336                 ahd_pause(ahd_ddb_softc);
1337         }
1338 }
1339
1340 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1341 {
1342         if (ahd_ddb_softc == NULL) {
1343                 db_error("Must set unit with ahd_sunit first!\n");
1344                 return;
1345         }
1346         if (ahd_ddb_paused != 0) {
1347                 ahd_ddb_paused = 0;
1348                 if (ahd_ddb_paused_on_entry)
1349                         return;
1350                 ahd_unpause(ahd_ddb_softc);
1351         } else if (ahd_ddb_paused_on_entry != 0) {
1352                 /* Two unpauses to clear a paused on entry. */
1353                 ahd_ddb_paused_on_entry = 0;
1354                 ahd_unpause(ahd_ddb_softc);
1355         }
1356 }
1357
1358 DB_COMMAND(ahd_in, ahd_ddb_in)
1359 {
1360         int c;
1361         int size;
1362  
1363         if (ahd_ddb_softc == NULL) {
1364                 db_error("Must set unit with ahd_sunit first!\n");
1365                 return;
1366         }
1367         if (have_addr == 0)
1368                 return;
1369
1370         size = 1;
1371         while ((c = *modif++) != '\0') {
1372                 switch (c) {
1373                 case 'b':
1374                         size = 1;
1375                         break;
1376                 case 'w':
1377                         size = 2;
1378                         break;
1379                 case 'l':
1380                         size = 4;
1381                 break;
1382                 }
1383         }
1384
1385         if (count <= 0)
1386                 count = 1;
1387         while (--count >= 0) {
1388                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1389                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1390                 switch (size) {
1391                 case 1:
1392                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1393                         break;
1394                 case 2:
1395                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1396                         break;
1397                 case 4:
1398                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1399                         break;
1400                 }
1401         }
1402 }
1403
1404 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1405 {
1406         db_expr_t old_value;
1407         db_expr_t new_value;
1408         int       size;
1409  
1410         if (ahd_ddb_softc == NULL) {
1411                 db_error("Must set unit with ahd_sunit first!\n");
1412                 return;
1413         }
1414
1415         switch (modif[0]) {
1416         case '\0':
1417         case 'b':
1418                 size = 1;
1419                 break;
1420         case 'h':
1421                 size = 2;
1422                 break;
1423         case 'l':
1424                 size = 4;
1425                 break;
1426         default:
1427                 db_error("Unknown size\n");
1428                 return;
1429         }
1430  
1431         while (db_expression(&new_value)) {
1432                 switch (size) {
1433                 default:
1434                 case 1:
1435                         old_value = ahd_inb(ahd_ddb_softc, addr);
1436                         ahd_outb(ahd_ddb_softc, addr, new_value);
1437                         break;
1438                 case 2:
1439                         old_value = ahd_inw(ahd_ddb_softc, addr);
1440                         ahd_outw(ahd_ddb_softc, addr, new_value);
1441                         break;
1442                 case 4:
1443                         old_value = ahd_inl(ahd_ddb_softc, addr);
1444                         ahd_outl(ahd_ddb_softc, addr, new_value);
1445                         break;
1446                 }
1447                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1448                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1449                           (u_long)old_value, (u_long)new_value);
1450                 addr += size;
1451         }
1452         db_skip_to_eol();
1453 }
1454
1455 DB_COMMAND(ahd_dump, ahd_ddb_dump)
1456 {
1457         if (ahd_ddb_softc == NULL) {
1458                 db_error("Must set unit with ahd_sunit first!\n");
1459                 return;
1460         }
1461         ahd_dump_card_state(ahd_ddb_softc);
1462 }
1463
1464 #endif
1465
1466
1467 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1468 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1469 MODULE_VERSION(ahd, 1);