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