ahc_eisa.c:
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx_osm.c
1 /*
2  * Bus independent FreeBSD shim for the aic79xx 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#35 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.16 2003/12/17 00:02:09 gibbs Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.15 2007/07/06 00:01:16 pavalos Exp $
36  */
37
38 #include "aic79xx_osm.h"
39 #include "aic79xx_inline.h"
40
41 #include <sys/kthread.h>
42
43 #include "opt_ddb.h"
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #endif
47
48 #ifndef AHD_TMODE_ENABLE
49 #define AHD_TMODE_ENABLE 0
50 #endif
51
52 #include "aic_osm_lib.c"
53
54 #define ccb_scb_ptr spriv_ptr0
55
56 #if UNUSED
57 static void     ahd_dump_targcmd(struct target_cmd *cmd);
58 #endif
59 static int      ahd_modevent(module_t mod, int type, void *data);
60 static void     ahd_action(struct cam_sim *sim, union ccb *ccb);
61 static void     ahd_set_tran_settings(struct ahd_softc *ahd,
62                                       int our_id, char channel,
63                                       struct ccb_trans_settings *cts);
64 static void     ahd_get_tran_settings(struct ahd_softc *ahd,
65                                       int our_id, char channel,
66                                       struct ccb_trans_settings *cts);
67 static void     ahd_async(void *callback_arg, uint32_t code,
68                           struct cam_path *path, void *arg);
69 static void     ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
70                                 int nsegments, int error);
71 static void     ahd_poll(struct cam_sim *sim);
72 static void     ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
73                                struct ccb_scsiio *csio, struct scb *scb);
74 static void     ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
75                               union ccb *ccb);
76 static int      ahd_create_path(struct ahd_softc *ahd,
77                                 char channel, u_int target, u_int lun,
78                                 struct cam_path **path);
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         path_id = cam_sim_path(ahd->platform_data->sim);
87         return (xpt_create_path(path, /*periph*/NULL,
88                                 path_id, target, lun));
89 }
90
91 int
92 ahd_map_int(struct ahd_softc *ahd)
93 {
94         int error;
95
96         /* Hook up our interrupt handler */
97         error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
98                                0, ahd_platform_intr, ahd,
99                                &ahd->platform_data->ih, NULL);
100         if (error != 0)
101                 device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
102                               error);
103         return (error);
104 }
105
106 /*
107  * Attach all the sub-devices we can find
108  */
109 int
110 ahd_attach(struct ahd_softc *ahd)
111 {
112         char   ahd_info[256];
113         struct ccb_setasync csa;
114         struct cam_sim *sim;
115         struct cam_path *path;
116         int count;
117
118         count = 0;
119         sim = NULL;
120
121         /*
122          * Create a thread to perform all recovery.
123          */
124         if (ahd_spawn_recovery_thread(ahd) != 0)
125                 goto fail;
126
127         ahd_controller_info(ahd, ahd_info);
128         kprintf("%s\n", ahd_info);
129         ahd_lock();
130
131         /*
132          * Construct our SIM entry
133          */
134         sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
135                             device_get_unit(ahd->dev_softc),
136                             1, AHD_MAX_QUEUE, NULL);
137         if (sim == NULL)
138                 goto fail;
139
140         if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
141                 cam_sim_free(sim);
142                 sim = NULL;
143                 goto fail;
144         }
145         
146         if (xpt_create_path(&path, /*periph*/NULL,
147                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
148                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
149                 xpt_bus_deregister(cam_sim_path(sim));
150                 cam_sim_free(sim);
151                 sim = NULL;
152                 goto fail;
153         }
154                 
155         xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
156         csa.ccb_h.func_code = XPT_SASYNC_CB;
157         csa.event_enable = AC_LOST_DEVICE;
158         csa.callback = ahd_async;
159         csa.callback_arg = sim;
160         xpt_action((union ccb *)&csa);
161         count++;
162
163 fail:
164         ahd->platform_data->sim = sim;
165         ahd->platform_data->path = path;
166         if (count != 0) {
167                 /* We have to wait until after any system dumps... */
168                 ahd->platform_data->eh =
169                     EVENTHANDLER_REGISTER(shutdown_post_sync, ahd_shutdown,
170                                           ahd, SHUTDOWN_PRI_DEFAULT);
171                 ahd_intr_enable(ahd, TRUE);
172         }
173
174         ahd_unlock();
175
176         return (count);
177 }
178
179 /*
180  * Catch an interrupt from the adapter
181  */
182 void
183 ahd_platform_intr(void *arg)
184 {
185         struct  ahd_softc *ahd;
186
187         ahd = (struct ahd_softc *)arg; 
188         ahd_intr(ahd);
189 }
190
191 /*
192  * We have an scb which has been processed by the
193  * adaptor, now we look to see how the operation
194  * went.
195  */
196 void
197 ahd_done(struct ahd_softc *ahd, struct scb *scb)
198 {
199         union ccb *ccb;
200
201         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
202                   ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
203
204         ccb = scb->io_ctx;
205         LIST_REMOVE(scb, pending_links);
206         if ((scb->flags & SCB_TIMEDOUT) != 0)
207                 LIST_REMOVE(scb, timedout_links);
208
209         callout_stop(&ccb->ccb_h.timeout_ch);
210
211         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
212                 bus_dmasync_op_t op;
213
214                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
215                         op = BUS_DMASYNC_POSTREAD;
216                 else
217                         op = BUS_DMASYNC_POSTWRITE;
218                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
219                 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
220         }
221
222 #ifdef AHD_TARGET_MODE
223         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
224                 struct cam_path *ccb_path;
225
226                 /*
227                  * If we have finally disconnected, clean up our
228                  * pending device state.
229                  * XXX - There may be error states that cause where
230                  *       we will remain connected.
231                  */
232                 ccb_path = ccb->ccb_h.path;
233                 if (ahd->pending_device != NULL
234                  && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
235
236                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
237                                 ahd->pending_device = NULL;
238                         } else {
239                                 xpt_print_path(ccb->ccb_h.path);
240                                 kprintf("Still disconnected\n");
241                                 ahd_freeze_ccb(ccb);
242                         }
243                 }
244
245                 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
246                         ccb->ccb_h.status |= CAM_REQ_CMP;
247                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
248                 ahd_free_scb(ahd, scb);
249                 xpt_done(ccb);
250                 return;
251         }
252 #endif
253
254         /*
255          * If the recovery SCB completes, we have to be
256          * out of our timeout.
257          */
258         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
259                 struct  scb *list_scb;
260
261                 /*
262                  * We were able to complete the command successfully,
263                  * so reinstate the timeouts for all other pending
264                  * commands.
265                  */
266                 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
267                         union ccb *ccb;
268                         uint64_t time;
269
270                         ccb = list_scb->io_ctx;
271                         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
272                                 continue;
273
274                         time = ccb->ccb_h.timeout;
275                         time *= hz;
276                         time /= 1000;
277                         callout_reset(&ccb->ccb_h.timeout_ch, time,
278                             ahd_platform_timeout, list_scb);
279                 }
280
281                 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
282                  || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
283                         aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
284                 ahd_print_path(ahd, scb);
285                 kprintf("no longer in timeout, status = %x\n",
286                        ccb->ccb_h.status);
287         }
288
289         /* Don't clobber any existing error state */
290         if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
291                 ccb->ccb_h.status |= CAM_REQ_CMP;
292         } else if ((scb->flags & SCB_SENSE) != 0) {
293                 /*
294                  * We performed autosense retrieval.
295                  *
296                  * Zero any sense not transferred by the
297                  * device.  The SCSI spec mandates that any
298                  * untransfered data should be assumed to be
299                  * zero.  Complete the 'bounce' of sense information
300                  * through buffers accessible via bus-space by
301                  * copying it into the clients csio.
302                  */
303                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
304                 memcpy(&ccb->csio.sense_data,
305                        ahd_get_sense_buf(ahd, scb),
306 /* XXX What size do we want to use??? */
307                         sizeof(ccb->csio.sense_data)
308                        - ccb->csio.sense_resid);
309                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
310         } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
311                 struct scsi_status_iu_header *siu;
312                 u_int sense_len;
313                 int i;
314
315                 /*
316                  * Copy only the sense data into the provided buffer.
317                  */
318                 siu = (struct scsi_status_iu_header *)scb->sense_data;
319                 sense_len = MIN(scsi_4btoul(siu->sense_length),
320                                 sizeof(ccb->csio.sense_data));
321                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
322                 memcpy(&ccb->csio.sense_data,
323                        ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
324                        sense_len);
325                 kprintf("Copied %d bytes of sense data offset %d:", sense_len,
326                        SIU_SENSE_OFFSET(siu));
327                 for (i = 0; i < sense_len; i++)
328                         kprintf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
329                 kprintf("\n");
330                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
331         }
332         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
333         ahd_free_scb(ahd, scb);
334         xpt_done(ccb);
335 }
336
337 static void
338 ahd_action(struct cam_sim *sim, union ccb *ccb)
339 {
340         struct  ahd_softc *ahd;
341 #ifdef AHD_TARGET_MODE
342         struct  ahd_tmode_lstate *lstate;
343 #endif
344         u_int   target_id;
345         u_int   our_id;
346
347         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
348         
349         ahd = (struct ahd_softc *)cam_sim_softc(sim);
350
351         target_id = ccb->ccb_h.target_id;
352         our_id = SIM_SCSI_ID(ahd, sim);
353         
354         switch (ccb->ccb_h.func_code) {
355         /* Common cases first */
356 #ifdef AHD_TARGET_MODE
357         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
358         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
359         {
360                 struct     ahd_tmode_tstate *tstate;
361                 cam_status status;
362
363                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
364                                              &lstate, TRUE);
365
366                 if (status != CAM_REQ_CMP) {
367                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
368                                 /* Response from the black hole device */
369                                 tstate = NULL;
370                                 lstate = ahd->black_hole;
371                         } else {
372                                 ccb->ccb_h.status = status;
373                                 xpt_done(ccb);
374                                 break;
375                         }
376                 }
377                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
378
379                         ahd_lock();
380                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
381                                           sim_links.sle);
382                         ccb->ccb_h.status = CAM_REQ_INPROG;
383                         if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
384                                 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
385                         ahd_unlock();
386                         break;
387                 }
388
389                 /*
390                  * The target_id represents the target we attempt to
391                  * select.  In target mode, this is the initiator of
392                  * the original command.
393                  */
394                 our_id = target_id;
395                 target_id = ccb->csio.init_id;
396                 /* FALLTHROUGH */
397         }
398 #endif
399         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
400         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
401         {
402                 struct  scb *scb;
403                 struct  hardware_scb *hscb;     
404                 struct  ahd_initiator_tinfo *tinfo;
405                 struct  ahd_tmode_tstate *tstate;
406                 u_int   col_idx;
407
408                 if ((ahd->flags & AHD_INITIATORROLE) == 0
409                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
410                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
411                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
412                         xpt_done(ccb);
413                         return;
414                 }
415
416                 /*
417                  * get an scb to use.
418                  */
419                 ahd_lock();
420                 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
421                                             target_id, &tstate);
422                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
423                  || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
424                  || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
425                         col_idx = AHD_NEVER_COL_IDX;
426                 } else {
427                         col_idx = AHD_BUILD_COL_IDX(target_id,
428                                                     ccb->ccb_h.target_lun);
429                 }
430                 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
431         
432                         xpt_freeze_simq(sim, /*count*/1);
433                         ahd->flags |= AHD_RESOURCE_SHORTAGE;
434                         ahd_unlock();
435                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
436                         xpt_done(ccb);
437                         return;
438                 }
439                 ahd_unlock();
440                 
441                 hscb = scb->hscb;
442                 
443                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
444                           ("start scb(%p)\n", scb));
445                 scb->io_ctx = ccb;
446                 /*
447                  * So we can find the SCB when an abort is requested
448                  */
449                 ccb->ccb_h.ccb_scb_ptr = scb;
450
451                 /*
452                  * Put all the arguments for the xfer in the scb
453                  */
454                 hscb->control = 0;
455                 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
456                 hscb->lun = ccb->ccb_h.target_lun;
457                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
458                         hscb->cdb_len = 0;
459                         scb->flags |= SCB_DEVICE_RESET;
460                         hscb->control |= MK_MESSAGE;
461                         hscb->task_management = SIU_TASKMGMT_LUN_RESET;
462                         ahd_execute_scb(scb, NULL, 0, 0);
463                 } else {
464 #ifdef AHD_TARGET_MODE
465                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
466                                 struct target_data *tdata;
467
468                                 tdata = &hscb->shared_data.tdata;
469                                 if (ahd->pending_device == lstate)
470                                         scb->flags |= SCB_TARGET_IMMEDIATE;
471                                 hscb->control |= TARGET_SCB;
472                                 tdata->target_phases = 0;
473                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
474                                         tdata->target_phases |= SPHASE_PENDING;
475                                         tdata->scsi_status =
476                                             ccb->csio.scsi_status;
477                                 }
478                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
479                                         tdata->target_phases |= NO_DISCONNECT;
480
481                                 tdata->initiator_tag =
482                                     ahd_htole16(ccb->csio.tag_id);
483                         }
484 #endif
485                         hscb->task_management = 0;
486                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
487                                 hscb->control |= ccb->csio.tag_action;
488                         
489                         ahd_setup_data(ahd, sim, &ccb->csio, scb);
490                 }
491                 break;
492         }
493 #ifdef AHD_TARGET_MODE
494         case XPT_NOTIFY_ACK:
495         case XPT_IMMED_NOTIFY:
496         {
497                 struct     ahd_tmode_tstate *tstate;
498                 struct     ahd_tmode_lstate *lstate;
499                 cam_status status;
500
501                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
502                                              &lstate, TRUE);
503
504                 if (status != CAM_REQ_CMP) {
505                         ccb->ccb_h.status = status;
506                         xpt_done(ccb);
507                         break;
508                 }
509                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
510                                   sim_links.sle);
511                 ccb->ccb_h.status = CAM_REQ_INPROG;
512                 ahd_send_lstate_events(ahd, lstate);
513                 break;
514         }
515         case XPT_EN_LUN:                /* Enable LUN as a target */
516                 ahd_handle_en_lun(ahd, sim, ccb);
517                 xpt_done(ccb);
518                 break;
519 #endif
520         case XPT_ABORT:                 /* Abort the specified CCB */
521         {
522                 ahd_abort_ccb(ahd, sim, ccb);
523                 break;
524         }
525         case XPT_SET_TRAN_SETTINGS:
526         {
527                 ahd_lock();
528                 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
529                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
530                 ahd_unlock();
531                 xpt_done(ccb);
532                 break;
533         }
534         case XPT_GET_TRAN_SETTINGS:
535         /* Get default/user set transfer settings for the target */
536         {
537                 ahd_lock();
538                 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
539                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
540                 ahd_unlock();
541                 xpt_done(ccb);
542                 break;
543         }
544         case XPT_CALC_GEOMETRY:
545         {
546                 cam_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
547                 xpt_done(ccb);
548                 break;
549         }
550         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
551         {
552                 int  found;
553                 
554                 ahd_lock();
555                 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
556                                           /*initiate reset*/TRUE);
557                 ahd_unlock();
558                 if (bootverbose) {
559                         xpt_print_path(SIM_PATH(ahd, sim));
560                         kprintf("SCSI bus reset delivered. "
561                                "%d SCBs aborted.\n", found);
562                 }
563                 ccb->ccb_h.status = CAM_REQ_CMP;
564                 xpt_done(ccb);
565                 break;
566         }
567         case XPT_TERM_IO:               /* Terminate the I/O process */
568                 /* XXX Implement */
569                 ccb->ccb_h.status = CAM_REQ_INVALID;
570                 xpt_done(ccb);
571                 break;
572         case XPT_PATH_INQ:              /* Path routing inquiry */
573         {
574                 struct ccb_pathinq *cpi = &ccb->cpi;
575                 
576                 cpi->version_num = 1; /* XXX??? */
577                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
578                 if ((ahd->features & AHD_WIDE) != 0)
579                         cpi->hba_inquiry |= PI_WIDE_16;
580                 if ((ahd->features & AHD_TARGETMODE) != 0) {
581                         cpi->target_sprt = PIT_PROCESSOR
582                                          | PIT_DISCONNECT
583                                          | PIT_TERM_IO;
584                 } else {
585                         cpi->target_sprt = 0;
586                 }
587                 cpi->hba_misc = 0;
588                 cpi->hba_eng_cnt = 0;
589                 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
590                 cpi->max_lun = AHD_NUM_LUNS - 1;
591                 cpi->initiator_id = ahd->our_id;
592                 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
593                         cpi->hba_misc |= PIM_NOBUSRESET;
594                 }
595                 cpi->bus_id = cam_sim_bus(sim);
596                 cpi->base_transfer_speed = 3300;
597                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
598                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
599                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
600                 cpi->unit_number = cam_sim_unit(sim);
601 #ifdef AHD_NEW_TRAN_SETTINGS
602                 cpi->protocol = PROTO_SCSI;
603                 cpi->protocol_version = SCSI_REV_2;
604                 cpi->transport = XPORT_SPI;
605                 cpi->transport_version = 2;
606                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
607                 cpi->transport_version = 4;
608                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
609 #endif
610                 cpi->ccb_h.status = CAM_REQ_CMP;
611                 xpt_done(ccb);
612                 break;
613         }
614         default:
615                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
616                 xpt_done(ccb);
617                 break;
618         }
619 }
620
621
622 static void
623 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
624                       struct ccb_trans_settings *cts)
625 {
626 #ifdef AHD_NEW_TRAN_SETTINGS
627         struct    ahd_devinfo devinfo;
628         struct    ccb_trans_settings_scsi *scsi;
629         struct    ccb_trans_settings_spi *spi;
630         struct    ahd_initiator_tinfo *tinfo;
631         struct    ahd_tmode_tstate *tstate;
632         uint16_t *discenable;
633         uint16_t *tagenable;
634         u_int     update_type;
635
636         scsi = &cts->proto_specific.scsi;
637         spi = &cts->xport_specific.spi;
638         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
639                             cts->ccb_h.target_id,
640                             cts->ccb_h.target_lun,
641                             SIM_CHANNEL(ahd, sim),
642                             ROLE_UNKNOWN);
643         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
644                                     devinfo.our_scsiid,
645                                     devinfo.target, &tstate);
646         update_type = 0;
647         if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
648                 update_type |= AHD_TRANS_GOAL;
649                 discenable = &tstate->discenable;
650                 tagenable = &tstate->tagenable;
651                 tinfo->curr.protocol_version = cts->protocol_version;
652                 tinfo->curr.transport_version = cts->transport_version;
653                 tinfo->goal.protocol_version = cts->protocol_version;
654                 tinfo->goal.transport_version = cts->transport_version;
655         } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
656                 update_type |= AHD_TRANS_USER;
657                 discenable = &ahd->user_discenable;
658                 tagenable = &ahd->user_tagenable;
659                 tinfo->user.protocol_version = cts->protocol_version;
660                 tinfo->user.transport_version = cts->transport_version;
661         } else {
662                 cts->ccb_h.status = CAM_REQ_INVALID;
663                 return;
664         }
665         
666         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
667                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
668                         *discenable |= devinfo.target_mask;
669                 else
670                         *discenable &= ~devinfo.target_mask;
671         }
672         
673         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
674                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
675                         *tagenable |= devinfo.target_mask;
676                 else
677                         *tagenable &= ~devinfo.target_mask;
678         }       
679
680         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
681                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
682                                    &spi->bus_width, ROLE_UNKNOWN);
683                 ahd_set_width(ahd, &devinfo, spi->bus_width,
684                               update_type, /*paused*/FALSE);
685         }
686
687         if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
688                 if (update_type == AHD_TRANS_USER)
689                         spi->ppr_options = tinfo->user.ppr_options;
690                 else
691                         spi->ppr_options = tinfo->goal.ppr_options;
692         }
693
694         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
695                 if (update_type == AHD_TRANS_USER)
696                         spi->sync_offset = tinfo->user.offset;
697                 else
698                         spi->sync_offset = tinfo->goal.offset;
699         }
700
701         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
702                 if (update_type == AHD_TRANS_USER)
703                         spi->sync_period = tinfo->user.period;
704                 else
705                         spi->sync_period = tinfo->goal.period;
706         }
707
708         if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
709          || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
710                 u_int   maxsync;
711
712                 maxsync = AHD_SYNCRATE_MAX;
713
714                 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
715                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
716
717                 if ((*discenable & devinfo.target_mask) == 0)
718                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
719
720                 ahd_find_syncrate(ahd, &spi->sync_period,
721                                   &spi->ppr_options, maxsync);
722                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
723                                     spi->sync_period, &spi->sync_offset,
724                                     spi->bus_width, ROLE_UNKNOWN);
725
726                 /* We use a period of 0 to represent async */
727                 if (spi->sync_offset == 0) {
728                         spi->sync_period = 0;
729                         spi->ppr_options = 0;
730                 }
731
732                 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
733                                  spi->sync_offset, spi->ppr_options,
734                                  update_type, /*paused*/FALSE);
735         }
736         cts->ccb_h.status = CAM_REQ_CMP;
737 #else
738         struct    ahd_devinfo devinfo;
739         struct    ahd_initiator_tinfo *tinfo;
740         struct    ahd_tmode_tstate *tstate;
741         uint16_t *discenable;
742         uint16_t *tagenable;
743         u_int     update_type;
744
745         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
746                             cts->ccb_h.target_id,
747                             cts->ccb_h.target_lun,
748                             SIM_CHANNEL(ahd, sim),
749                             ROLE_UNKNOWN);
750         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
751                                     devinfo.our_scsiid,
752                                     devinfo.target, &tstate);
753         update_type = 0;
754         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
755                 update_type |= AHD_TRANS_GOAL;
756                 discenable = &tstate->discenable;
757                 tagenable = &tstate->tagenable;
758         } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
759                 update_type |= AHD_TRANS_USER;
760                 discenable = &ahd->user_discenable;
761                 tagenable = &ahd->user_tagenable;
762         } else {
763                 cts->ccb_h.status = CAM_REQ_INVALID;
764                 return;
765         }
766         
767         if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
768                 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
769                         *discenable |= devinfo.target_mask;
770                 else
771                         *discenable &= ~devinfo.target_mask;
772         }
773         
774         if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
775                 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
776                         *tagenable |= devinfo.target_mask;
777                 else
778                         *tagenable &= ~devinfo.target_mask;
779         }       
780
781         if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
782                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
783                                    &cts->bus_width, ROLE_UNKNOWN);
784                 ahd_set_width(ahd, &devinfo, cts->bus_width,
785                               update_type, /*paused*/FALSE);
786         }
787
788         if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
789                 if (update_type == AHD_TRANS_USER)
790                         cts->sync_offset = tinfo->user.offset;
791                 else
792                         cts->sync_offset = tinfo->goal.offset;
793         }
794
795         if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
796                 if (update_type == AHD_TRANS_USER)
797                         cts->sync_period = tinfo->user.period;
798                 else
799                         cts->sync_period = tinfo->goal.period;
800         }
801
802         if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
803          || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
804          || ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
805          || ((cts->valid & CCB_TRANS_DISC_VALID) != 0)) {
806                 u_int ppr_options;
807                 u_int maxsync;
808
809                 maxsync = AHD_SYNCRATE_MAX;
810                 ppr_options = 0;
811                 if (cts->sync_period <= AHD_SYNCRATE_DT
812                  && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT) {
813                         ppr_options = tinfo->user.ppr_options
814                                     | MSG_EXT_PPR_DT_REQ;
815                 }
816
817                 if ((*tagenable & devinfo.target_mask) == 0
818                  || (*discenable & devinfo.target_mask) == 0)
819                         ppr_options &= ~MSG_EXT_PPR_IU_REQ;
820
821                 ahd_find_syncrate(ahd, &cts->sync_period,
822                                   &ppr_options, maxsync);
823                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
824                                     cts->sync_period, &cts->sync_offset,
825                                     MSG_EXT_WDTR_BUS_8_BIT,
826                                     ROLE_UNKNOWN);
827
828                 /* We use a period of 0 to represent async */
829                 if (cts->sync_offset == 0) {
830                         cts->sync_period = 0;
831                         ppr_options = 0;
832                 }
833
834                 if (ppr_options != 0
835                  && tinfo->user.transport_version >= 3) {
836                         tinfo->goal.transport_version =
837                             tinfo->user.transport_version;
838                         tinfo->curr.transport_version =
839                             tinfo->user.transport_version;
840                 }
841                 
842                 ahd_set_syncrate(ahd, &devinfo, cts->sync_period,
843                                  cts->sync_offset, ppr_options,
844                                  update_type, /*paused*/FALSE);
845         }
846         cts->ccb_h.status = CAM_REQ_CMP;
847 #endif
848 }
849
850 static void
851 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
852                       struct ccb_trans_settings *cts)
853 {
854 #ifdef AHD_NEW_TRAN_SETTINGS
855         struct  ahd_devinfo devinfo;
856         struct  ccb_trans_settings_scsi *scsi;
857         struct  ccb_trans_settings_spi *spi;
858         struct  ahd_initiator_tinfo *targ_info;
859         struct  ahd_tmode_tstate *tstate;
860         struct  ahd_transinfo *tinfo;
861
862         scsi = &cts->proto_specific.scsi;
863         spi = &cts->xport_specific.spi;
864         ahd_compile_devinfo(&devinfo, our_id,
865                             cts->ccb_h.target_id,
866                             cts->ccb_h.target_lun,
867                             channel, ROLE_UNKNOWN);
868         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
869                                         devinfo.our_scsiid,
870                                         devinfo.target, &tstate);
871         
872         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
873                 tinfo = &targ_info->curr;
874         else
875                 tinfo = &targ_info->user;
876         
877         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
878         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
879         if (cts->type == CTS_TYPE_USER_SETTINGS) {
880                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
881                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
882
883                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
884                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
885         } else {
886                 if ((tstate->discenable & devinfo.target_mask) != 0)
887                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
888
889                 if ((tstate->tagenable & devinfo.target_mask) != 0)
890                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
891         }
892         cts->protocol_version = tinfo->protocol_version;
893         cts->transport_version = tinfo->transport_version;
894
895         spi->sync_period = tinfo->period;
896         spi->sync_offset = tinfo->offset;
897         spi->bus_width = tinfo->width;
898         spi->ppr_options = tinfo->ppr_options;
899         
900         cts->protocol = PROTO_SCSI;
901         cts->transport = XPORT_SPI;
902         spi->valid = CTS_SPI_VALID_SYNC_RATE
903                    | CTS_SPI_VALID_SYNC_OFFSET
904                    | CTS_SPI_VALID_BUS_WIDTH
905                    | CTS_SPI_VALID_PPR_OPTIONS;
906
907         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
908                 scsi->valid = CTS_SCSI_VALID_TQ;
909                 spi->valid |= CTS_SPI_VALID_DISC;
910         } else {
911                 scsi->valid = 0;
912         }
913
914         cts->ccb_h.status = CAM_REQ_CMP;
915 #else
916         struct  ahd_devinfo devinfo;
917         struct  ahd_initiator_tinfo *targ_info;
918         struct  ahd_tmode_tstate *tstate;
919         struct  ahd_transinfo *tinfo;
920
921         ahd_compile_devinfo(&devinfo, our_id,
922                             cts->ccb_h.target_id,
923                             cts->ccb_h.target_lun,
924                             channel, ROLE_UNKNOWN);
925         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
926                                         devinfo.our_scsiid,
927                                         devinfo.target, &tstate);
928         
929         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
930                 tinfo = &targ_info->curr;
931         else
932                 tinfo = &targ_info->user;
933         
934         cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
935         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
936                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
937                         cts->flags |= CCB_TRANS_DISC_ENB;
938
939                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
940                         cts->flags |= CCB_TRANS_TAG_ENB;
941         } else {
942                 if ((tstate->discenable & devinfo.target_mask) != 0)
943                         cts->flags |= CCB_TRANS_DISC_ENB;
944
945                 if ((tstate->tagenable & devinfo.target_mask) != 0)
946                         cts->flags |= CCB_TRANS_TAG_ENB;
947         }
948         cts->sync_period = tinfo->period;
949         cts->sync_offset = tinfo->offset;
950         cts->bus_width = tinfo->width;
951         
952         cts->valid = CCB_TRANS_SYNC_RATE_VALID
953                    | CCB_TRANS_SYNC_OFFSET_VALID
954                    | CCB_TRANS_BUS_WIDTH_VALID;
955
956         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
957                 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
958
959         cts->ccb_h.status = CAM_REQ_CMP;
960 #endif
961 }
962
963 static void
964 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
965 {
966         struct ahd_softc *ahd;
967         struct cam_sim *sim;
968
969         sim = (struct cam_sim *)callback_arg;
970         ahd = (struct ahd_softc *)cam_sim_softc(sim);
971         switch (code) {
972         case AC_LOST_DEVICE:
973         {
974                 struct  ahd_devinfo devinfo;
975
976                 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
977                                     xpt_path_target_id(path),
978                                     xpt_path_lun_id(path),
979                                     SIM_CHANNEL(ahd, sim),
980                                     ROLE_UNKNOWN);
981
982                 /*
983                  * Revert to async/narrow transfers
984                  * for the next device.
985                  */
986                 ahd_lock();
987                 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
988                               AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
989                 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
990                                  /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
991                                  /*paused*/FALSE);
992                 ahd_unlock();
993                 break;
994         }
995         default:
996                 break;
997         }
998 }
999
1000 static void
1001 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1002                 int error)
1003 {
1004         struct  scb *scb;
1005         union   ccb *ccb;
1006         struct  ahd_softc *ahd;
1007         struct  ahd_initiator_tinfo *tinfo;
1008         struct  ahd_tmode_tstate *tstate;
1009         u_int   mask;
1010
1011         scb = (struct scb *)arg;
1012         ccb = scb->io_ctx;
1013         ahd = scb->ahd_softc;
1014
1015         if (error != 0) {
1016                 if (error == EFBIG)
1017                         aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1018                 else
1019                         aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1020                 if (nsegments != 0)
1021                         bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
1022                 ahd_lock();
1023                 ahd_free_scb(ahd, scb);
1024                 ahd_unlock();
1025                 xpt_done(ccb);
1026                 return;
1027         }
1028         scb->sg_count = 0;
1029         if (nsegments != 0) {
1030                 void *sg;
1031                 bus_dmasync_op_t op;
1032                 u_int i;
1033
1034                 /* Copy the segments into our SG list */
1035                 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
1036
1037                         sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
1038                                           dm_segs->ds_len,
1039                                           /*last*/i == 1);
1040                         dm_segs++;
1041                 }
1042                 
1043                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1044                         op = BUS_DMASYNC_PREREAD;
1045                 else
1046                         op = BUS_DMASYNC_PREWRITE;
1047
1048                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
1049
1050                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1051                         struct target_data *tdata;
1052
1053                         tdata = &scb->hscb->shared_data.tdata;
1054                         tdata->target_phases |= DPHASE_PENDING;
1055                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1056                                 tdata->data_phase = P_DATAOUT;
1057                         else
1058                                 tdata->data_phase = P_DATAIN;
1059                 }
1060         }
1061
1062         ahd_lock();
1063
1064         /*
1065          * Last time we need to check if this SCB needs to
1066          * be aborted.
1067          */
1068         if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1069                 if (nsegments != 0)
1070                         bus_dmamap_unload(ahd->buffer_dmat,
1071                                           scb->dmamap);
1072                 ahd_free_scb(ahd, scb);
1073                 ahd_unlock();
1074                 xpt_done(ccb);
1075                 return;
1076         }
1077
1078         tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1079                                     SCSIID_OUR_ID(scb->hscb->scsiid),
1080                                     SCSIID_TARGET(ahd, scb->hscb->scsiid),
1081                                     &tstate);
1082
1083         mask = SCB_GET_TARGET_MASK(ahd, scb);
1084
1085         if ((tstate->discenable & mask) != 0
1086          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1087                 scb->hscb->control |= DISCENB;
1088
1089         if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1090                 scb->flags |= SCB_PACKETIZED;
1091                 if (scb->hscb->task_management != 0)
1092                         scb->hscb->control &= ~MK_MESSAGE;
1093         }
1094
1095         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1096          && (tinfo->goal.width != 0
1097           || tinfo->goal.period != 0
1098           || tinfo->goal.ppr_options != 0)) {
1099                 scb->flags |= SCB_NEGOTIATE;
1100                 scb->hscb->control |= MK_MESSAGE;
1101         } else if ((tstate->auto_negotiate & mask) != 0) {
1102                 scb->flags |= SCB_AUTO_NEGOTIATE;
1103                 scb->hscb->control |= MK_MESSAGE;
1104         }
1105
1106         LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1107
1108         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1109
1110         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1111                 uint64_t time;
1112
1113                 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1114                         ccb->ccb_h.timeout = 5 * 1000;
1115
1116                 time = ccb->ccb_h.timeout;
1117                 time *= hz;
1118                 time /= 1000;
1119                 callout_reset(&ccb->ccb_h.timeout_ch, time,
1120                     ahd_platform_timeout, scb);
1121         }
1122
1123         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1124                 /* Define a mapping from our tag to the SCB. */
1125                 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1126                 ahd_pause(ahd);
1127                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1128                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1129                 ahd_unpause(ahd);
1130         } else {
1131                 ahd_queue_scb(ahd, scb);
1132         }
1133
1134         ahd_unlock();
1135 }
1136
1137 static void
1138 ahd_poll(struct cam_sim *sim)
1139 {
1140         ahd_intr(cam_sim_softc(sim));
1141 }
1142
1143 static void
1144 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1145                struct ccb_scsiio *csio, struct scb *scb)
1146 {
1147         struct hardware_scb *hscb;
1148         struct ccb_hdr *ccb_h;
1149         
1150         hscb = scb->hscb;
1151         ccb_h = &csio->ccb_h;
1152         
1153         csio->resid = 0;
1154         csio->sense_resid = 0;
1155         if (ccb_h->func_code == XPT_SCSI_IO) {
1156                 hscb->cdb_len = csio->cdb_len;
1157                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1158
1159                         if (hscb->cdb_len > MAX_CDB_LEN
1160                          && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1161                                 /*
1162                                  * Should CAM start to support CDB sizes
1163                                  * greater than 16 bytes, we could use
1164                                  * the sense buffer to store the CDB.
1165                                  */
1166                                 aic_set_transaction_status(scb,
1167                                                            CAM_REQ_INVALID);
1168                                 ahd_lock();
1169                                 ahd_free_scb(ahd, scb);
1170                                 ahd_unlock();
1171                                 xpt_done((union ccb *)csio);
1172                                 return;
1173                         }
1174                         if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1175                                 hscb->shared_data.idata.cdb_from_host.cdbptr =
1176                                    aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1177                                 hscb->shared_data.idata.cdb_from_host.cdblen =
1178                                    csio->cdb_len;
1179                                 hscb->cdb_len |= SCB_CDB_LEN_PTR;
1180                         } else {
1181                                 memcpy(hscb->shared_data.idata.cdb, 
1182                                        csio->cdb_io.cdb_ptr,
1183                                        hscb->cdb_len);
1184                         }
1185                 } else {
1186                         if (hscb->cdb_len > MAX_CDB_LEN) {
1187                                 aic_set_transaction_status(scb,
1188                                                            CAM_REQ_INVALID);
1189                                 ahd_lock();
1190                                 ahd_free_scb(ahd, scb);
1191                                 ahd_unlock();
1192                                 xpt_done((union ccb *)csio);
1193                                 return;
1194                         }
1195                         memcpy(hscb->shared_data.idata.cdb,
1196                                csio->cdb_io.cdb_bytes, hscb->cdb_len);
1197                 }
1198         }
1199                 
1200         /* Only use S/G if there is a transfer */
1201         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1202                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1203                         /* We've been given a pointer to a single buffer */
1204                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1205                                 int error;
1206
1207                                 crit_enter();
1208                                 error = bus_dmamap_load(ahd->buffer_dmat,
1209                                                         scb->dmamap,
1210                                                         csio->data_ptr,
1211                                                         csio->dxfer_len,
1212                                                         ahd_execute_scb,
1213                                                         scb, /*flags*/0);
1214                                 if (error == EINPROGRESS) {
1215                                         /*
1216                                          * So as to maintain ordering,
1217                                          * freeze the controller queue
1218                                          * until our mapping is
1219                                          * returned.
1220                                          */
1221                                         xpt_freeze_simq(sim,
1222                                                         /*count*/1);
1223                                         scb->io_ctx->ccb_h.status |=
1224                                             CAM_RELEASE_SIMQ;
1225                                 }
1226                                 crit_exit();
1227                         } else {
1228                                 struct bus_dma_segment seg;
1229
1230                                 /* Pointer to physical buffer */
1231                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1232                                         panic("ahd_setup_data - Transfer size "
1233                                               "larger than can device max");
1234
1235                                 seg.ds_addr =
1236                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1237                                 seg.ds_len = csio->dxfer_len;
1238                                 ahd_execute_scb(scb, &seg, 1, 0);
1239                         }
1240                 } else {
1241                         struct bus_dma_segment *segs;
1242
1243                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1244                                 panic("ahd_setup_data - Physical segment "
1245                                       "pointers unsupported");
1246
1247                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1248                                 panic("ahd_setup_data - Virtual segment "
1249                                       "addresses unsupported");
1250
1251                         /* Just use the segments provided */
1252                         segs = (struct bus_dma_segment *)csio->data_ptr;
1253                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1254                 }
1255         } else {
1256                 ahd_execute_scb(scb, NULL, 0, 0);
1257         }
1258 }
1259
1260 static void
1261 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1262 {
1263         union ccb *abort_ccb;
1264
1265         abort_ccb = ccb->cab.abort_ccb;
1266         switch (abort_ccb->ccb_h.func_code) {
1267 #ifdef AHD_TARGET_MODE
1268         case XPT_ACCEPT_TARGET_IO:
1269         case XPT_IMMED_NOTIFY:
1270         case XPT_CONT_TARGET_IO:
1271         {
1272                 struct ahd_tmode_tstate *tstate;
1273                 struct ahd_tmode_lstate *lstate;
1274                 struct ccb_hdr_slist *list;
1275                 cam_status status;
1276
1277                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1278                                              &lstate, TRUE);
1279
1280                 if (status != CAM_REQ_CMP) {
1281                         ccb->ccb_h.status = status;
1282                         break;
1283                 }
1284
1285                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1286                         list = &lstate->accept_tios;
1287                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1288                         list = &lstate->immed_notifies;
1289                 else
1290                         list = NULL;
1291
1292                 if (list != NULL) {
1293                         struct ccb_hdr *curelm;
1294                         int found;
1295
1296                         curelm = SLIST_FIRST(list);
1297                         found = 0;
1298                         if (curelm == &abort_ccb->ccb_h) {
1299                                 found = 1;
1300                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1301                         } else {
1302                                 while(curelm != NULL) {
1303                                         struct ccb_hdr *nextelm;
1304
1305                                         nextelm =
1306                                             SLIST_NEXT(curelm, sim_links.sle);
1307
1308                                         if (nextelm == &abort_ccb->ccb_h) {
1309                                                 found = 1;
1310                                                 SLIST_NEXT(curelm,
1311                                                            sim_links.sle) =
1312                                                     SLIST_NEXT(nextelm,
1313                                                                sim_links.sle);
1314                                                 break;
1315                                         }
1316                                         curelm = nextelm;
1317                                 }
1318                         }
1319
1320                         if (found) {
1321                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1322                                 xpt_done(abort_ccb);
1323                                 ccb->ccb_h.status = CAM_REQ_CMP;
1324                         } else {
1325                                 xpt_print_path(abort_ccb->ccb_h.path);
1326                                 kprintf("Not found\n");
1327                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1328                         }
1329                         break;
1330                 }
1331                 /* FALLTHROUGH */
1332         }
1333 #endif
1334         case XPT_SCSI_IO:
1335                 /* XXX Fully implement the hard ones */
1336                 ccb->ccb_h.status = CAM_UA_ABORT;
1337                 break;
1338         default:
1339                 ccb->ccb_h.status = CAM_REQ_INVALID;
1340                 break;
1341         }
1342         xpt_done(ccb);
1343 }
1344
1345 void
1346 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1347                 u_int lun, ac_code code, void *opt_arg)
1348 {
1349         struct  ccb_trans_settings cts;
1350         struct cam_path *path;
1351         void *arg;
1352         int error;
1353
1354         arg = NULL;
1355         error = ahd_create_path(ahd, channel, target, lun, &path);
1356
1357         if (error != CAM_REQ_CMP)
1358                 return;
1359
1360         switch (code) {
1361         case AC_TRANSFER_NEG:
1362         {
1363 #ifdef AHD_NEW_TRAN_SETTINGS
1364                 struct  ccb_trans_settings_scsi *scsi;
1365         
1366                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1367                 scsi = &cts.proto_specific.scsi;
1368 #else
1369                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1370 #endif
1371                 cts.ccb_h.path = path;
1372                 cts.ccb_h.target_id = target;
1373                 cts.ccb_h.target_lun = lun;
1374                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1375                 arg = &cts;
1376 #ifdef AHD_NEW_TRAN_SETTINGS
1377                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1378                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1379 #else
1380                 cts.valid &= ~CCB_TRANS_TQ_VALID;
1381                 cts.flags &= ~CCB_TRANS_TAG_ENB;
1382 #endif
1383                 if (opt_arg == NULL)
1384                         break;
1385                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1386 #ifdef AHD_NEW_TRAN_SETTINGS
1387                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1388                 scsi->valid |= CTS_SCSI_VALID_TQ;
1389 #else
1390                         cts.flags |= CCB_TRANS_TAG_ENB;
1391                 cts.valid |= CCB_TRANS_TQ_VALID;
1392 #endif
1393                 break;
1394         }
1395         case AC_SENT_BDR:
1396         case AC_BUS_RESET:
1397                 break;
1398         default:
1399                 panic("ahd_send_async: Unexpected async event");
1400         }
1401         xpt_async(code, path, arg);
1402         xpt_free_path(path);
1403 }
1404
1405 void
1406 ahd_platform_set_tags(struct ahd_softc *ahd,
1407                       struct ahd_devinfo *devinfo, int enable)
1408 {
1409 }
1410
1411 int
1412 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1413 {
1414         ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1415                                     M_INTWAIT | M_ZERO);
1416         return (0);
1417 }
1418
1419 void
1420 ahd_platform_free(struct ahd_softc *ahd)
1421 {
1422         struct ahd_platform_data *pdata;
1423
1424         pdata = ahd->platform_data;
1425         if (pdata != NULL) {
1426                 if (pdata->regs[0] != NULL)
1427                         bus_release_resource(ahd->dev_softc,
1428                                              pdata->regs_res_type[0],
1429                                              pdata->regs_res_id[0],
1430                                              pdata->regs[0]);
1431
1432                 if (pdata->regs[1] != NULL)
1433                         bus_release_resource(ahd->dev_softc,
1434                                              pdata->regs_res_type[1],
1435                                              pdata->regs_res_id[1],
1436                                              pdata->regs[1]);
1437
1438                 if (pdata->irq != NULL)
1439                         bus_release_resource(ahd->dev_softc,
1440                                              pdata->irq_res_type,
1441                                              0, pdata->irq);
1442
1443                 if (pdata->sim != NULL) {
1444                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1445                         xpt_free_path(pdata->path);
1446                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1447                         cam_sim_free(pdata->sim);
1448                 }
1449                 if (pdata->eh != NULL)
1450                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1451                 kfree(ahd->platform_data, M_DEVBUF);
1452         }
1453 }
1454
1455 int
1456 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1457 {
1458         /* We don't sort softcs under FreeBSD so report equal always */
1459         return (0);
1460 }
1461
1462 int
1463 ahd_detach(device_t dev)
1464 {
1465         struct ahd_softc *ahd;
1466
1467         device_printf(dev, "detaching device\n");
1468         ahd = device_get_softc(dev);
1469         ahd = ahd_find_softc(ahd);
1470         if (ahd == NULL) {
1471                 device_printf(dev, "aic7xxx already detached\n");
1472                 return (ENOENT);
1473         }
1474         TAILQ_REMOVE(&ahd_tailq, ahd, links);
1475         ahd_lock();
1476         ahd_intr_enable(ahd, FALSE);
1477         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1478         ahd_unlock();
1479         ahd_free(ahd);
1480         return (0);
1481 }
1482
1483 #if UNUSED
1484 static void
1485 ahd_dump_targcmd(struct target_cmd *cmd)
1486 {
1487         uint8_t *byte;
1488         uint8_t *last_byte;
1489         int i;
1490
1491         byte = &cmd->initiator_channel;
1492         /* Debugging info for received commands */
1493         last_byte = &cmd[1].initiator_channel;
1494
1495         i = 0;
1496         while (byte < last_byte) {
1497                 if (i == 0)
1498                         kprintf("\t");
1499                 kprintf("%#x", *byte++);
1500                 i++;
1501                 if (i == 8) {
1502                         kprintf("\n");
1503                         i = 0;
1504                 } else {
1505                         kprintf(", ");
1506                 }
1507         }
1508 }
1509 #endif
1510
1511 static int
1512 ahd_modevent(module_t mod, int type, void *data)
1513 {
1514         /* XXX Deal with busy status on unload. */
1515         return 0;
1516 }
1517   
1518 static moduledata_t ahd_mod = {
1519         "ahd",
1520         ahd_modevent,
1521         NULL
1522 };
1523
1524 /********************************** DDB Hooks *********************************/
1525 #ifdef DDB
1526 static struct ahd_softc *ahd_ddb_softc;
1527 static int ahd_ddb_paused;
1528 static int ahd_ddb_paused_on_entry;
1529 DB_COMMAND(ahd_set_unit, ahd_ddb_set_unit)
1530 {
1531         struct ahd_softc *list_ahd;
1532
1533         ahd_ddb_softc = NULL;
1534         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1535                 if (list_ahd->unit == addr)
1536                         ahd_ddb_softc = list_ahd;
1537         }
1538         if (ahd_ddb_softc == NULL)
1539                 db_error("No matching softc found!\n");
1540 }
1541
1542 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1543 {
1544         if (ahd_ddb_softc == NULL) {
1545                 db_error("Must set unit with ahd_set_unit first!\n");
1546                 return;
1547         }
1548         if (ahd_ddb_paused == 0) {
1549                 ahd_ddb_paused++;
1550                 if (ahd_is_paused(ahd_ddb_softc)) {
1551                         ahd_ddb_paused_on_entry++;
1552                         return;
1553                 }
1554                 ahd_pause(ahd_ddb_softc);
1555         }
1556 }
1557
1558 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1559 {
1560         if (ahd_ddb_softc == NULL) {
1561                 db_error("Must set unit with ahd_set_unit first!\n");
1562                 return;
1563         }
1564         if (ahd_ddb_paused != 0) {
1565                 ahd_ddb_paused = 0;
1566                 if (ahd_ddb_paused_on_entry)
1567                         return;
1568                 ahd_unpause(ahd_ddb_softc);
1569         } else if (ahd_ddb_paused_on_entry != 0) {
1570                 /* Two unpauses to clear a paused on entry. */
1571                 ahd_ddb_paused_on_entry = 0;
1572                 ahd_unpause(ahd_ddb_softc);
1573         }
1574 }
1575
1576 DB_COMMAND(ahd_in, ahd_ddb_in)
1577 {
1578         int c;
1579         int size;
1580  
1581         if (ahd_ddb_softc == NULL) {
1582                 db_error("Must set unit with ahd_set_unit first!\n");
1583                 return;
1584         }
1585         if (have_addr == 0)
1586                 return;
1587
1588         size = 1;
1589         while ((c = *modif++) != '\0') {
1590                 switch (c) {
1591                 case 'b':
1592                         size = 1;
1593                         break;
1594                 case 'w':
1595                         size = 2;
1596                         break;
1597                 case 'l':
1598                         size = 4;
1599                 break;
1600                 }
1601         }
1602
1603         if (count <= 0)
1604                 count = 1;
1605         while (--count >= 0) {
1606                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1607                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1608                 switch (size) {
1609                 case 1:
1610                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1611                         break;
1612                 case 2:
1613                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1614                         break;
1615                 case 4:
1616                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1617                         break;
1618                 }
1619         }
1620 }
1621
1622 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1623 {
1624         db_expr_t old_value;
1625         db_expr_t new_value;
1626         int       size;
1627  
1628         if (ahd_ddb_softc == NULL) {
1629                 db_error("Must set unit with ahd_set_unit first!\n");
1630                 return;
1631         }
1632
1633         switch (modif[0]) {
1634         case '\0':
1635         case 'b':
1636                 size = 1;
1637                 break;
1638         case 'h':
1639                 size = 2;
1640                 break;
1641         case 'l':
1642                 size = 4;
1643                 break;
1644         default:
1645                 db_error("Unknown size\n");
1646                 return;
1647         }
1648  
1649         while (db_expression(&new_value)) {
1650                 switch (size) {
1651                 default:
1652                 case 1:
1653                         old_value = ahd_inb(ahd_ddb_softc, addr);
1654                         ahd_outb(ahd_ddb_softc, addr, new_value);
1655                         break;
1656                 case 2:
1657                         old_value = ahd_inw(ahd_ddb_softc, addr);
1658                         ahd_outw(ahd_ddb_softc, addr, new_value);
1659                         break;
1660                 case 4:
1661                         old_value = ahd_inl(ahd_ddb_softc, addr);
1662                         ahd_outl(ahd_ddb_softc, addr, new_value);
1663                         break;
1664                 }
1665                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1666                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1667                           (u_long)old_value, (u_long)new_value);
1668                 addr += size;
1669         }
1670         db_skip_to_eol();
1671 }
1672
1673 #endif
1674
1675
1676 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1677 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1678 MODULE_VERSION(ahd, 1);