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