1fa47e104b90043bf5ec6e4e6cd9d68c589d441d
[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.27.2.6 2003/06/10 03:26:09 gibbs Exp $
34  * $DragonFly: src/sys/dev/disk/aic7xxx/aic7xxx_osm.c,v 1.13 2007/06/04 17:21:55 dillon 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                 struct    ccb_calc_geometry *ccg;
811                 uint32_t size_mb;
812                 uint32_t secs_per_cylinder;
813                 int       extended;
814
815                 ccg = &ccb->ccg;
816                 size_mb = ccg->volume_size
817                         / ((1024L * 1024L) / ccg->block_size);
818                 extended = SIM_IS_SCSIBUS_B(ahc, sim)
819                         ? ahc->flags & AHC_EXTENDED_TRANS_B
820                         : ahc->flags & AHC_EXTENDED_TRANS_A;
821                 
822                 if (size_mb > 1024 && extended) {
823                         ccg->heads = 255;
824                         ccg->secs_per_track = 63;
825                 } else {
826                         ccg->heads = 64;
827                         ccg->secs_per_track = 32;
828                 }
829                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
830                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
831                 ccb->ccb_h.status = CAM_REQ_CMP;
832                 xpt_done(ccb);
833                 break;
834         }
835         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
836         {
837                 int  found;
838                 
839                 ahc_lock();
840                 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
841                                           /*initiate reset*/TRUE);
842                 ahc_unlock();
843                 if (bootverbose) {
844                         xpt_print_path(SIM_PATH(ahc, sim));
845                         kprintf("SCSI bus reset delivered. "
846                                "%d SCBs aborted.\n", found);
847                 }
848                 ccb->ccb_h.status = CAM_REQ_CMP;
849                 xpt_done(ccb);
850                 break;
851         }
852         case XPT_TERM_IO:               /* Terminate the I/O process */
853                 /* XXX Implement */
854                 ccb->ccb_h.status = CAM_REQ_INVALID;
855                 xpt_done(ccb);
856                 break;
857         case XPT_PATH_INQ:              /* Path routing inquiry */
858         {
859                 struct ccb_pathinq *cpi = &ccb->cpi;
860                 
861                 cpi->version_num = 1; /* XXX??? */
862                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
863                 if ((ahc->features & AHC_WIDE) != 0)
864                         cpi->hba_inquiry |= PI_WIDE_16;
865                 if ((ahc->features & AHC_TARGETMODE) != 0) {
866                         cpi->target_sprt = PIT_PROCESSOR
867                                          | PIT_DISCONNECT
868                                          | PIT_TERM_IO;
869                 } else {
870                         cpi->target_sprt = 0;
871                 }
872                 cpi->hba_misc = 0;
873                 cpi->hba_eng_cnt = 0;
874                 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
875                 cpi->max_lun = AHC_NUM_LUNS - 1;
876                 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
877                         cpi->initiator_id = ahc->our_id_b;
878                         if ((ahc->flags & AHC_RESET_BUS_B) == 0)
879                                 cpi->hba_misc |= PIM_NOBUSRESET;
880                 } else {
881                         cpi->initiator_id = ahc->our_id;
882                         if ((ahc->flags & AHC_RESET_BUS_A) == 0)
883                                 cpi->hba_misc |= PIM_NOBUSRESET;
884                 }
885                 cpi->bus_id = cam_sim_bus(sim);
886                 cpi->base_transfer_speed = 3300;
887                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
888                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
889                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
890                 cpi->unit_number = cam_sim_unit(sim);
891 #ifdef AHC_NEW_TRAN_SETTINGS
892                 cpi->protocol = PROTO_SCSI;
893                 cpi->protocol_version = SCSI_REV_2;
894                 cpi->transport = XPORT_SPI;
895                 cpi->transport_version = 2;
896                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
897                 if ((ahc->features & AHC_DT) != 0) {
898                         cpi->transport_version = 3;
899                         cpi->xport_specific.spi.ppr_options =
900                             SID_SPI_CLOCK_DT_ST;
901                 }
902 #endif
903                 cpi->ccb_h.status = CAM_REQ_CMP;
904                 xpt_done(ccb);
905                 break;
906         }
907         default:
908                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
909                 xpt_done(ccb);
910                 break;
911         }
912 }
913
914 static void
915 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
916                       struct ccb_trans_settings *cts)
917 {
918 #ifdef AHC_NEW_TRAN_SETTINGS
919         struct  ahc_devinfo devinfo;
920         struct  ccb_trans_settings_scsi *scsi;
921         struct  ccb_trans_settings_spi *spi;
922         struct  ahc_initiator_tinfo *targ_info;
923         struct  ahc_tmode_tstate *tstate;
924         struct  ahc_transinfo *tinfo;
925
926         scsi = &cts->proto_specific.scsi;
927         spi = &cts->xport_specific.spi;
928         ahc_compile_devinfo(&devinfo, our_id,
929                             cts->ccb_h.target_id,
930                             cts->ccb_h.target_lun,
931                             channel, ROLE_UNKNOWN);
932         targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
933                                         devinfo.our_scsiid,
934                                         devinfo.target, &tstate);
935         
936         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
937                 tinfo = &targ_info->curr;
938         else
939                 tinfo = &targ_info->user;
940         
941         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
942         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
943         if (cts->type == CTS_TYPE_USER_SETTINGS) {
944                 if ((ahc->user_discenable & devinfo.target_mask) != 0)
945                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
946
947                 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
948                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
949         } else {
950                 if ((tstate->discenable & devinfo.target_mask) != 0)
951                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
952
953                 if ((tstate->tagenable & devinfo.target_mask) != 0)
954                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
955         }
956         cts->protocol_version = tinfo->protocol_version;
957         cts->transport_version = tinfo->transport_version;
958
959         spi->sync_period = tinfo->period;
960         spi->sync_offset = tinfo->offset;
961         spi->bus_width = tinfo->width;
962         spi->ppr_options = tinfo->ppr_options;
963         
964         cts->protocol = PROTO_SCSI;
965         cts->transport = XPORT_SPI;
966         spi->valid = CTS_SPI_VALID_SYNC_RATE
967                    | CTS_SPI_VALID_SYNC_OFFSET
968                    | CTS_SPI_VALID_BUS_WIDTH
969                    | CTS_SPI_VALID_PPR_OPTIONS;
970
971         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
972                 scsi->valid = CTS_SCSI_VALID_TQ;
973                 spi->valid |= CTS_SPI_VALID_DISC;
974         } else {
975                 scsi->valid = 0;
976         }
977
978         cts->ccb_h.status = CAM_REQ_CMP;
979 #else
980         struct  ahc_devinfo devinfo;
981         struct  ahc_initiator_tinfo *targ_info;
982         struct  ahc_tmode_tstate *tstate;
983         struct  ahc_transinfo *tinfo;
984
985         ahc_compile_devinfo(&devinfo, our_id,
986                             cts->ccb_h.target_id,
987                             cts->ccb_h.target_lun,
988                             channel, ROLE_UNKNOWN);
989         targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
990                                         devinfo.our_scsiid,
991                                         devinfo.target, &tstate);
992         
993         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
994                 tinfo = &targ_info->curr;
995         else
996                 tinfo = &targ_info->user;
997         
998         cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
999         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
1000                 if ((ahc->user_discenable & devinfo.target_mask) != 0)
1001                         cts->flags |= CCB_TRANS_DISC_ENB;
1002
1003                 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1004                         cts->flags |= CCB_TRANS_TAG_ENB;
1005         } else {
1006                 if ((tstate->discenable & devinfo.target_mask) != 0)
1007                         cts->flags |= CCB_TRANS_DISC_ENB;
1008
1009                 if ((tstate->tagenable & devinfo.target_mask) != 0)
1010                         cts->flags |= CCB_TRANS_TAG_ENB;
1011         }
1012         cts->sync_period = tinfo->period;
1013         cts->sync_offset = tinfo->offset;
1014         cts->bus_width = tinfo->width;
1015         
1016         cts->valid = CCB_TRANS_SYNC_RATE_VALID
1017                    | CCB_TRANS_SYNC_OFFSET_VALID
1018                    | CCB_TRANS_BUS_WIDTH_VALID;
1019
1020         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1021                 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1022
1023         cts->ccb_h.status = CAM_REQ_CMP;
1024 #endif
1025 }
1026
1027 static void
1028 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1029 {
1030         struct ahc_softc *ahc;
1031         struct cam_sim *sim;
1032
1033         sim = (struct cam_sim *)callback_arg;
1034         ahc = (struct ahc_softc *)cam_sim_softc(sim);
1035         switch (code) {
1036         case AC_LOST_DEVICE:
1037         {
1038                 struct  ahc_devinfo devinfo;
1039
1040                 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1041                                     xpt_path_target_id(path),
1042                                     xpt_path_lun_id(path),
1043                                     SIM_CHANNEL(ahc, sim),
1044                                     ROLE_UNKNOWN);
1045
1046                 /*
1047                  * Revert to async/narrow transfers
1048                  * for the next device.
1049                  */
1050                 ahc_lock();
1051                 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1052                               AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1053                 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1054                                  /*period*/0, /*offset*/0, /*ppr_options*/0,
1055                                  AHC_TRANS_GOAL|AHC_TRANS_CUR,
1056                                  /*paused*/FALSE);
1057                 ahc_unlock();
1058                 break;
1059         }
1060         default:
1061                 break;
1062         }
1063 }
1064
1065 static void
1066 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1067                 int error)
1068 {
1069         struct  scb *scb;
1070         union   ccb *ccb;
1071         struct  ahc_softc *ahc;
1072         struct  ahc_initiator_tinfo *tinfo;
1073         struct  ahc_tmode_tstate *tstate;
1074         u_int   mask;
1075
1076         scb = (struct scb *)arg;
1077         ccb = scb->io_ctx;
1078         ahc = scb->ahc_softc;
1079
1080         if (error != 0) {
1081                 if (error == EFBIG)
1082                         ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1083                 else
1084                         ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1085                 if (nsegments != 0)
1086                         bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1087                 ahc_lock();
1088                 ahc_free_scb(ahc, scb);
1089                 ahc_unlock();
1090                 xpt_done(ccb);
1091                 return;
1092         }
1093         if (nsegments != 0) {
1094                 struct    ahc_dma_seg *sg;
1095                 bus_dma_segment_t *end_seg;
1096                 bus_dmasync_op_t op;
1097
1098                 end_seg = dm_segs + nsegments;
1099
1100                 /* Copy the segments into our SG list */
1101                 sg = scb->sg_list;
1102                 while (dm_segs < end_seg) {
1103                         uint32_t len;
1104
1105                         sg->addr = ahc_htole32(dm_segs->ds_addr);
1106                         len = dm_segs->ds_len
1107                             | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1108                         sg->len = ahc_htole32(len);
1109                         sg++;
1110                         dm_segs++;
1111                 }
1112                 
1113                 /*
1114                  * Note where to find the SG entries in bus space.
1115                  * We also set the full residual flag which the 
1116                  * sequencer will clear as soon as a data transfer
1117                  * occurs.
1118                  */
1119                 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1120
1121                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1122                         op = BUS_DMASYNC_PREREAD;
1123                 else
1124                         op = BUS_DMASYNC_PREWRITE;
1125
1126                 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1127
1128                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1129                         struct target_data *tdata;
1130
1131                         tdata = &scb->hscb->shared_data.tdata;
1132                         tdata->target_phases |= DPHASE_PENDING;
1133                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1134                                 tdata->data_phase = P_DATAOUT;
1135                         else
1136                                 tdata->data_phase = P_DATAIN;
1137
1138                         /*
1139                          * If the transfer is of an odd length and in the
1140                          * "in" direction (scsi->HostBus), then it may
1141                          * trigger a bug in the 'WideODD' feature of
1142                          * non-Ultra2 chips.  Force the total data-length
1143                          * to be even by adding an extra, 1 byte, SG,
1144                          * element.  We do this even if we are not currently
1145                          * negotiated wide as negotiation could occur before
1146                          * this command is executed.
1147                          */
1148                         if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1149                          && (ccb->csio.dxfer_len & 0x1) != 0
1150                          && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1151
1152                                 nsegments++;
1153                                 if (nsegments > AHC_NSEG) {
1154
1155                                         ahc_set_transaction_status(scb,
1156                                             CAM_REQ_TOO_BIG);
1157                                         bus_dmamap_unload(ahc->buffer_dmat,
1158                                                           scb->dmamap);
1159                                         ahc_lock();
1160                                         ahc_free_scb(ahc, scb);
1161                                         ahc_unlock();
1162                                         xpt_done(ccb);
1163                                         return;
1164                                 }
1165                                 sg->addr = ahc_htole32(ahc->dma_bug_buf);
1166                                 sg->len = ahc_htole32(1);
1167                                 sg++;
1168                         }
1169                 }
1170                 sg--;
1171                 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1172
1173                 /* Copy the first SG into the "current" data pointer area */
1174                 scb->hscb->dataptr = scb->sg_list->addr;
1175                 scb->hscb->datacnt = scb->sg_list->len;
1176         } else {
1177                 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1178                 scb->hscb->dataptr = 0;
1179                 scb->hscb->datacnt = 0;
1180         }
1181         
1182         scb->sg_count = nsegments;
1183
1184         ahc_lock();
1185
1186         /*
1187          * Last time we need to check if this SCB needs to
1188          * be aborted.
1189          */
1190         if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1191                 if (nsegments != 0)
1192                         bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1193                 ahc_free_scb(ahc, scb);
1194                 ahc_unlock();
1195                 xpt_done(ccb);
1196                 return;
1197         }
1198
1199         tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1200                                     SCSIID_OUR_ID(scb->hscb->scsiid),
1201                                     SCSIID_TARGET(ahc, scb->hscb->scsiid),
1202                                     &tstate);
1203
1204         mask = SCB_GET_TARGET_MASK(ahc, scb);
1205         scb->hscb->scsirate = tinfo->scsirate;
1206         scb->hscb->scsioffset = tinfo->curr.offset;
1207         if ((tstate->ultraenb & mask) != 0)
1208                 scb->hscb->control |= ULTRAENB;
1209
1210         if ((tstate->discenable & mask) != 0
1211          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1212                 scb->hscb->control |= DISCENB;
1213
1214         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1215          && (tinfo->goal.width != 0
1216           || tinfo->goal.offset != 0
1217           || tinfo->goal.ppr_options != 0)) {
1218                 scb->flags |= SCB_NEGOTIATE;
1219                 scb->hscb->control |= MK_MESSAGE;
1220         } else if ((tstate->auto_negotiate & mask) != 0) {
1221                 scb->flags |= SCB_AUTO_NEGOTIATE;
1222                 scb->hscb->control |= MK_MESSAGE;
1223         }
1224
1225         LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1226
1227         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1228
1229         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1230                 uint64_t time;
1231
1232                 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1233                         ccb->ccb_h.timeout = 5 * 1000;
1234
1235                 time = ccb->ccb_h.timeout;
1236                 time *= hz;
1237                 time /= 1000;
1238                 callout_reset(&ccb->ccb_h.timeout_ch, time, ahc_timeout, scb);
1239         }
1240
1241         /*
1242          * We only allow one untagged transaction
1243          * per target in the initiator role unless
1244          * we are storing a full busy target *lun*
1245          * table in SCB space.
1246          */
1247         if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1248          && (ahc->flags & AHC_SCB_BTT) == 0) {
1249                 struct scb_tailq *untagged_q;
1250                 int target_offset;
1251
1252                 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1253                 untagged_q = &(ahc->untagged_queues[target_offset]);
1254                 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1255                 scb->flags |= SCB_UNTAGGEDQ;
1256                 if (TAILQ_FIRST(untagged_q) != scb) {
1257                         ahc_unlock();
1258                         return;
1259                 }
1260         }
1261         scb->flags |= SCB_ACTIVE;
1262
1263         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1264                 /* Define a mapping from our tag to the SCB. */
1265                 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1266                 ahc_pause(ahc);
1267                 if ((ahc->flags & AHC_PAGESCBS) == 0)
1268                         ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1269                 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1270                 ahc_unpause(ahc);
1271         } else {
1272                 ahc_queue_scb(ahc, scb);
1273         }
1274
1275         ahc_unlock();
1276 }
1277
1278 static void
1279 ahc_poll(struct cam_sim *sim)
1280 {
1281         struct ahc_softc *ahc;
1282
1283         ahc = (struct ahc_softc *)cam_sim_softc(sim);
1284         ahc_intr(ahc);
1285 }
1286
1287 static void
1288 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1289                struct ccb_scsiio *csio, struct scb *scb)
1290 {
1291         struct hardware_scb *hscb;
1292         struct ccb_hdr *ccb_h;
1293         
1294         hscb = scb->hscb;
1295         ccb_h = &csio->ccb_h;
1296         
1297         csio->resid = 0;
1298         csio->sense_resid = 0;
1299         if (ccb_h->func_code == XPT_SCSI_IO) {
1300                 hscb->cdb_len = csio->cdb_len;
1301                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1302
1303                         if (hscb->cdb_len > sizeof(hscb->cdb32)
1304                          || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1305                                 ahc_set_transaction_status(scb,
1306                                                            CAM_REQ_INVALID);
1307                                 ahc_lock();
1308                                 ahc_free_scb(ahc, scb);
1309                                 ahc_unlock();
1310                                 xpt_done((union ccb *)csio);
1311                                 return;
1312                         }
1313                         if (hscb->cdb_len > 12) {
1314                                 memcpy(hscb->cdb32, 
1315                                        csio->cdb_io.cdb_ptr,
1316                                        hscb->cdb_len);
1317                                 scb->flags |= SCB_CDB32_PTR;
1318                         } else {
1319                                 memcpy(hscb->shared_data.cdb, 
1320                                        csio->cdb_io.cdb_ptr,
1321                                        hscb->cdb_len);
1322                         }
1323                 } else {
1324                         if (hscb->cdb_len > 12) {
1325                                 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1326                                        hscb->cdb_len);
1327                                 scb->flags |= SCB_CDB32_PTR;
1328                         } else {
1329                                 memcpy(hscb->shared_data.cdb,
1330                                        csio->cdb_io.cdb_bytes,
1331                                        hscb->cdb_len);
1332                         }
1333                 }
1334         }
1335                 
1336         /* Only use S/G if there is a transfer */
1337         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1338                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1339                         /* We've been given a pointer to a single buffer */
1340                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1341                                 int error;
1342
1343                                 crit_enter();
1344                                 error = bus_dmamap_load(ahc->buffer_dmat,
1345                                                         scb->dmamap,
1346                                                         csio->data_ptr,
1347                                                         csio->dxfer_len,
1348                                                         ahc_execute_scb,
1349                                                         scb, /*flags*/0);
1350                                 if (error == EINPROGRESS) {
1351                                         /*
1352                                          * So as to maintain ordering,
1353                                          * freeze the controller queue
1354                                          * until our mapping is
1355                                          * returned.
1356                                          */
1357                                         xpt_freeze_simq(sim,
1358                                                         /*count*/1);
1359                                         scb->io_ctx->ccb_h.status |=
1360                                             CAM_RELEASE_SIMQ;
1361                                 }
1362                                 crit_exit();
1363                         } else {
1364                                 struct bus_dma_segment seg;
1365
1366                                 /* Pointer to physical buffer */
1367                                 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1368                                         panic("ahc_setup_data - Transfer size "
1369                                               "larger than can device max");
1370
1371                                 seg.ds_addr =
1372                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1373                                 seg.ds_len = csio->dxfer_len;
1374                                 ahc_execute_scb(scb, &seg, 1, 0);
1375                         }
1376                 } else {
1377                         struct bus_dma_segment *segs;
1378
1379                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1380                                 panic("ahc_setup_data - Physical segment "
1381                                       "pointers unsupported");
1382
1383                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1384                                 panic("ahc_setup_data - Virtual segment "
1385                                       "addresses unsupported");
1386
1387                         /* Just use the segments provided */
1388                         segs = (struct bus_dma_segment *)csio->data_ptr;
1389                         ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1390                 }
1391         } else {
1392                 ahc_execute_scb(scb, NULL, 0, 0);
1393         }
1394 }
1395
1396 static void
1397 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1398
1399         if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1400                 struct scb *list_scb;
1401
1402                 scb->flags |= SCB_RECOVERY_SCB;
1403
1404                 /*
1405                  * Take all queued, but not sent SCBs out of the equation.
1406                  * Also ensure that no new CCBs are queued to us while we
1407                  * try to fix this problem.
1408                  */
1409                 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1410                         xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1411                         scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1412                 }
1413
1414                 /*
1415                  * Go through all of our pending SCBs and remove
1416                  * any scheduled timeouts for them.  We will reschedule
1417                  * them after we've successfully fixed this problem.
1418                  */
1419                 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1420                         union ccb *ccb;
1421
1422                         ccb = list_scb->io_ctx;
1423                         callout_stop(&ccb->ccb_h.timeout_ch);
1424                 }
1425         }
1426 }
1427
1428 void
1429 ahc_timeout(void *arg)
1430 {
1431         struct  scb *scb;
1432         struct  ahc_softc *ahc;
1433         int     found;
1434         u_int   last_phase;
1435         int     target;
1436         int     lun;
1437         int     i;
1438         char    channel;
1439
1440         scb = (struct scb *)arg; 
1441         ahc = (struct ahc_softc *)scb->ahc_softc;
1442
1443         ahc_lock();
1444
1445         ahc_pause_and_flushwork(ahc);
1446
1447         if ((scb->flags & SCB_ACTIVE) == 0) {
1448                 /* Previous timeout took care of me already */
1449                 kprintf("%s: Timedout SCB already complete. "
1450                        "Interrupts may not be functioning.\n", ahc_name(ahc));
1451                 ahc_unpause(ahc);
1452                 ahc_unlock();
1453                 return;
1454         }
1455
1456         target = SCB_GET_TARGET(ahc, scb);
1457         channel = SCB_GET_CHANNEL(ahc, scb);
1458         lun = SCB_GET_LUN(scb);
1459
1460         ahc_print_path(ahc, scb);
1461         kprintf("SCB 0x%x - timed out\n", scb->hscb->tag);
1462         ahc_dump_card_state(ahc);
1463         last_phase = ahc_inb(ahc, LASTPHASE);
1464         if (scb->sg_count > 0) {
1465                 for (i = 0; i < scb->sg_count; i++) {
1466                         kprintf("sg[%d] - Addr 0x%x : Length %d\n",
1467                                i,
1468                                scb->sg_list[i].addr,
1469                                scb->sg_list[i].len & AHC_SG_LEN_MASK);
1470                 }
1471         }
1472         if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1473                 /*
1474                  * Been down this road before.
1475                  * Do a full bus reset.
1476                  */
1477 bus_reset:
1478                 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1479                 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1480                 kprintf("%s: Issued Channel %c Bus Reset. "
1481                        "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1482         } else {
1483                 /*
1484                  * If we are a target, transition to bus free and report
1485                  * the timeout.
1486                  * 
1487                  * The target/initiator that is holding up the bus may not
1488                  * be the same as the one that triggered this timeout
1489                  * (different commands have different timeout lengths).
1490                  * If the bus is idle and we are actiing as the initiator
1491                  * for this request, queue a BDR message to the timed out
1492                  * target.  Otherwise, if the timed out transaction is
1493                  * active:
1494                  *   Initiator transaction:
1495                  *      Stuff the message buffer with a BDR message and assert
1496                  *      ATN in the hopes that the target will let go of the bus
1497                  *      and go to the mesgout phase.  If this fails, we'll
1498                  *      get another timeout 2 seconds later which will attempt
1499                  *      a bus reset.
1500                  *
1501                  *   Target transaction:
1502                  *      Transition to BUS FREE and report the error.
1503                  *      It's good to be the target!
1504                  */
1505                 u_int active_scb_index;
1506                 u_int saved_scbptr;
1507
1508                 saved_scbptr = ahc_inb(ahc, SCBPTR);
1509                 active_scb_index = ahc_inb(ahc, SCB_TAG);
1510
1511                 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1512                   && (active_scb_index < ahc->scb_data->numscbs)) {
1513                         struct scb *active_scb;
1514
1515                         /*
1516                          * If the active SCB is not us, assume that
1517                          * the active SCB has a longer timeout than
1518                          * the timedout SCB, and wait for the active
1519                          * SCB to timeout.
1520                          */ 
1521                         active_scb = ahc_lookup_scb(ahc, active_scb_index);
1522                         if (active_scb != scb) {
1523                                 struct   ccb_hdr *ccbh;
1524                                 uint64_t newtimeout;
1525
1526                                 ahc_print_path(ahc, scb);
1527                                 kprintf("Other SCB Timeout%s",
1528                                        (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1529                                        ? " again\n" : "\n");
1530                                 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1531                                 newtimeout =
1532                                     MAX(active_scb->io_ctx->ccb_h.timeout,
1533                                         scb->io_ctx->ccb_h.timeout);
1534                                 newtimeout *= hz;
1535                                 newtimeout /= 1000;
1536                                 ccbh = &scb->io_ctx->ccb_h;
1537                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1538                                     newtimeout, ahc_timeout, scb);
1539                                 ahc_unpause(ahc);
1540                                 ahc_unlock();
1541                                 return;
1542                         } 
1543
1544                         /* It's us */
1545                         if ((scb->flags & SCB_TARGET_SCB) != 0) {
1546
1547                                 /*
1548                                  * Send back any queued up transactions
1549                                  * and properly record the error condition.
1550                                  */
1551                                 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1552                                                SCB_GET_CHANNEL(ahc, scb),
1553                                                SCB_GET_LUN(scb),
1554                                                scb->hscb->tag,
1555                                                ROLE_TARGET,
1556                                                CAM_CMD_TIMEOUT);
1557
1558                                 /* Will clear us from the bus */
1559                                 ahc_restart(ahc);
1560                                 ahc_unlock();
1561                                 return;
1562                         }
1563
1564                         ahc_set_recoveryscb(ahc, active_scb);
1565                         ahc_outb(ahc, MSG_OUT, HOST_MSG);
1566                         ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1567                         ahc_print_path(ahc, active_scb);
1568                         kprintf("BDR message in message buffer\n");
1569                         active_scb->flags |= SCB_DEVICE_RESET;
1570                         callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1571                             2 * hz, ahc_timeout, active_scb);
1572                         ahc_unpause(ahc);
1573                 } else {
1574                         int      disconnected;
1575
1576                         /* XXX Shouldn't panic.  Just punt instead? */
1577                         if ((scb->flags & SCB_TARGET_SCB) != 0)
1578                                 panic("Timed-out target SCB but bus idle");
1579
1580                         if (last_phase != P_BUSFREE
1581                          && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1582                                 /* XXX What happened to the SCB? */
1583                                 /* Hung target selection.  Goto busfree */
1584                                 kprintf("%s: Hung target selection\n",
1585                                        ahc_name(ahc));
1586                                 ahc_restart(ahc);
1587                                 ahc_unlock();
1588                                 return;
1589                         }
1590
1591                         if (ahc_search_qinfifo(ahc, target, channel, lun,
1592                                                scb->hscb->tag, ROLE_INITIATOR,
1593                                                /*status*/0, SEARCH_COUNT) > 0) {
1594                                 disconnected = FALSE;
1595                         } else {
1596                                 disconnected = TRUE;
1597                         }
1598
1599                         if (disconnected) {
1600
1601                                 ahc_set_recoveryscb(ahc, scb);
1602                                 /*
1603                                  * Actually re-queue this SCB in an attempt
1604                                  * to select the device before it reconnects.
1605                                  * In either case (selection or reselection),
1606                                  * we will now issue a target reset to the
1607                                  * timed-out device.
1608                                  *
1609                                  * Set the MK_MESSAGE control bit indicating
1610                                  * that we desire to send a message.  We
1611                                  * also set the disconnected flag since
1612                                  * in the paging case there is no guarantee
1613                                  * that our SCB control byte matches the
1614                                  * version on the card.  We don't want the
1615                                  * sequencer to abort the command thinking
1616                                  * an unsolicited reselection occurred.
1617                                  */
1618                                 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1619                                 scb->flags |= SCB_DEVICE_RESET;
1620
1621                                 /*
1622                                  * Remove any cached copy of this SCB in the
1623                                  * disconnected list in preparation for the
1624                                  * queuing of our abort SCB.  We use the
1625                                  * same element in the SCB, SCB_NEXT, for
1626                                  * both the qinfifo and the disconnected list.
1627                                  */
1628                                 ahc_search_disc_list(ahc, target, channel,
1629                                                      lun, scb->hscb->tag,
1630                                                      /*stop_on_first*/TRUE,
1631                                                      /*remove*/TRUE,
1632                                                      /*save_state*/FALSE);
1633
1634                                 /*
1635                                  * In the non-paging case, the sequencer will
1636                                  * never re-reference the in-core SCB.
1637                                  * To make sure we are notified during
1638                                  * reslection, set the MK_MESSAGE flag in
1639                                  * the card's copy of the SCB.
1640                                  */
1641                                 if ((ahc->flags & AHC_PAGESCBS) == 0) {
1642                                         ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1643                                         ahc_outb(ahc, SCB_CONTROL,
1644                                                  ahc_inb(ahc, SCB_CONTROL)
1645                                                 | MK_MESSAGE);
1646                                 }
1647
1648                                 /*
1649                                  * Clear out any entries in the QINFIFO first
1650                                  * so we are the next SCB for this target
1651                                  * to run.
1652                                  */
1653                                 ahc_search_qinfifo(ahc,
1654                                                    SCB_GET_TARGET(ahc, scb),
1655                                                    channel, SCB_GET_LUN(scb),
1656                                                    SCB_LIST_NULL,
1657                                                    ROLE_INITIATOR,
1658                                                    CAM_REQUEUE_REQ,
1659                                                    SEARCH_COMPLETE);
1660                                 ahc_print_path(ahc, scb);
1661                                 kprintf("Queuing a BDR SCB\n");
1662                                 ahc_qinfifo_requeue_tail(ahc, scb);
1663                                 ahc_outb(ahc, SCBPTR, saved_scbptr);
1664                                 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1665                                     2 * hz, ahc_timeout, scb);
1666                                 ahc_unpause(ahc);
1667                         } else {
1668                                 /* Go "immediatly" to the bus reset */
1669                                 /* This shouldn't happen */
1670                                 ahc_set_recoveryscb(ahc, scb);
1671                                 ahc_print_path(ahc, scb);
1672                                 kprintf("SCB %d: Immediate reset.  "
1673                                         "Flags = 0x%x\n", scb->hscb->tag,
1674                                         scb->flags);
1675                                 goto bus_reset;
1676                         }
1677                 }
1678         }
1679         ahc_unlock();
1680 }
1681
1682 static void
1683 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1684 {
1685         union ccb *abort_ccb;
1686
1687         abort_ccb = ccb->cab.abort_ccb;
1688         switch (abort_ccb->ccb_h.func_code) {
1689         case XPT_ACCEPT_TARGET_IO:
1690         case XPT_IMMED_NOTIFY:
1691         case XPT_CONT_TARGET_IO:
1692         {
1693                 struct ahc_tmode_tstate *tstate;
1694                 struct ahc_tmode_lstate *lstate;
1695                 struct ccb_hdr_slist *list;
1696                 cam_status status;
1697
1698                 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1699                                              &lstate, TRUE);
1700
1701                 if (status != CAM_REQ_CMP) {
1702                         ccb->ccb_h.status = status;
1703                         break;
1704                 }
1705
1706                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1707                         list = &lstate->accept_tios;
1708                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1709                         list = &lstate->immed_notifies;
1710                 else
1711                         list = NULL;
1712
1713                 if (list != NULL) {
1714                         struct ccb_hdr *curelm;
1715                         int found;
1716
1717                         curelm = SLIST_FIRST(list);
1718                         found = 0;
1719                         if (curelm == &abort_ccb->ccb_h) {
1720                                 found = 1;
1721                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1722                         } else {
1723                                 while(curelm != NULL) {
1724                                         struct ccb_hdr *nextelm;
1725
1726                                         nextelm =
1727                                             SLIST_NEXT(curelm, sim_links.sle);
1728
1729                                         if (nextelm == &abort_ccb->ccb_h) {
1730                                                 found = 1;
1731                                                 SLIST_NEXT(curelm,
1732                                                            sim_links.sle) =
1733                                                     SLIST_NEXT(nextelm,
1734                                                                sim_links.sle);
1735                                                 break;
1736                                         }
1737                                         curelm = nextelm;
1738                                 }
1739                         }
1740
1741                         if (found) {
1742                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1743                                 xpt_done(abort_ccb);
1744                                 ccb->ccb_h.status = CAM_REQ_CMP;
1745                         } else {
1746                                 xpt_print_path(abort_ccb->ccb_h.path);
1747                                 kprintf("Not found\n");
1748                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1749                         }
1750                         break;
1751                 }
1752                 /* FALLTHROUGH */
1753         }
1754         case XPT_SCSI_IO:
1755                 /* XXX Fully implement the hard ones */
1756                 ccb->ccb_h.status = CAM_UA_ABORT;
1757                 break;
1758         default:
1759                 ccb->ccb_h.status = CAM_REQ_INVALID;
1760                 break;
1761         }
1762         xpt_done(ccb);
1763 }
1764
1765 void
1766 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1767                 u_int lun, ac_code code, void *opt_arg)
1768 {
1769         struct  ccb_trans_settings cts;
1770         struct cam_path *path;
1771         void *arg;
1772         int error;
1773
1774         arg = NULL;
1775         error = ahc_create_path(ahc, channel, target, lun, &path);
1776
1777         if (error != CAM_REQ_CMP)
1778                 return;
1779
1780         switch (code) {
1781         case AC_TRANSFER_NEG:
1782         {
1783 #ifdef AHC_NEW_TRAN_SETTINGS
1784                 struct  ccb_trans_settings_scsi *scsi;
1785         
1786                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1787                 scsi = &cts.proto_specific.scsi;
1788 #else
1789                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1790 #endif
1791                 cts.ccb_h.path = path;
1792                 cts.ccb_h.target_id = target;
1793                 cts.ccb_h.target_lun = lun;
1794                 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1795                                                           : ahc->our_id_b,
1796                                       channel, &cts);
1797                 arg = &cts;
1798 #ifdef AHC_NEW_TRAN_SETTINGS
1799                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1800                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1801 #else
1802                 cts.valid &= ~CCB_TRANS_TQ_VALID;
1803                 cts.flags &= ~CCB_TRANS_TAG_ENB;
1804 #endif
1805                 if (opt_arg == NULL)
1806                         break;
1807                 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1808 #ifdef AHC_NEW_TRAN_SETTINGS
1809                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1810                 scsi->valid |= CTS_SCSI_VALID_TQ;
1811 #else
1812                         cts.flags |= CCB_TRANS_TAG_ENB;
1813                 cts.valid |= CCB_TRANS_TQ_VALID;
1814 #endif
1815                 break;
1816         }
1817         case AC_SENT_BDR:
1818         case AC_BUS_RESET:
1819                 break;
1820         default:
1821                 panic("ahc_send_async: Unexpected async event");
1822         }
1823         xpt_async(code, path, arg);
1824         xpt_free_path(path);
1825 }
1826
1827 void
1828 ahc_platform_set_tags(struct ahc_softc *ahc,
1829                       struct ahc_devinfo *devinfo, int enable)
1830 {
1831 }
1832
1833 int
1834 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1835 {
1836         ahc->platform_data = kmalloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1837                                     M_INTWAIT | M_ZERO);
1838         return (0);
1839 }
1840
1841 void
1842 ahc_platform_free(struct ahc_softc *ahc)
1843 {
1844         struct ahc_platform_data *pdata;
1845
1846         pdata = ahc->platform_data;
1847         if (pdata != NULL) {
1848                 if (pdata->regs != NULL)
1849                         bus_release_resource(ahc->dev_softc,
1850                                              pdata->regs_res_type,
1851                                              pdata->regs_res_id,
1852                                              pdata->regs);
1853
1854                 if (pdata->irq != NULL)
1855                         bus_release_resource(ahc->dev_softc,
1856                                              pdata->irq_res_type,
1857                                              0, pdata->irq);
1858
1859                 if (pdata->sim_b != NULL) {
1860                         xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1861                         xpt_free_path(pdata->path_b);
1862                         xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1863                         cam_sim_free(pdata->sim_b);
1864                 }
1865                 if (pdata->sim != NULL) {
1866                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1867                         xpt_free_path(pdata->path);
1868                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1869                         cam_sim_free(pdata->sim);
1870                 }
1871                 if (pdata->eh != NULL)
1872                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1873                 kfree(ahc->platform_data, M_DEVBUF);
1874         }
1875 }
1876
1877 int
1878 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1879 {
1880         /* We don't sort softcs under FreeBSD so report equal always */
1881         return (0);
1882 }
1883
1884 int
1885 ahc_detach(device_t dev)
1886 {
1887         struct ahc_softc *ahc;
1888
1889         device_printf(dev, "detaching device\n");
1890         ahc = device_get_softc(dev);
1891         ahc = ahc_find_softc(ahc);
1892         if (ahc == NULL) {
1893                 device_printf(dev, "aic7xxx already detached\n");
1894                 return (ENOENT);
1895         }
1896         ahc_lock();
1897         ahc_intr_enable(ahc, FALSE);
1898         bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1899         ahc_unlock();
1900         ahc_free(ahc);
1901         return (0);
1902 }
1903
1904 #if UNUSED
1905 static void
1906 ahc_dump_targcmd(struct target_cmd *cmd)
1907 {
1908         uint8_t *byte;
1909         uint8_t *last_byte;
1910         int i;
1911
1912         byte = &cmd->initiator_channel;
1913         /* Debugging info for received commands */
1914         last_byte = &cmd[1].initiator_channel;
1915
1916         i = 0;
1917         while (byte < last_byte) {
1918                 if (i == 0)
1919                         kprintf("\t");
1920                 kprintf("%#x", *byte++);
1921                 i++;
1922                 if (i == 8) {
1923                         kprintf("\n");
1924                         i = 0;
1925                 } else {
1926                         kprintf(", ");
1927                 }
1928         }
1929 }
1930 #endif
1931
1932 static int
1933 ahc_modevent(module_t mod, int type, void *data)
1934 {
1935         /* XXX Deal with busy status on unload. */
1936         return 0;
1937 }
1938   
1939 static moduledata_t ahc_mod = {
1940         "ahc",
1941         ahc_modevent,
1942         NULL
1943 };
1944
1945 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1946 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1947 MODULE_VERSION(ahc, 1);