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