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