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