Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / raid / amr / amr_cam.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Copyright (c) 2002 Eric Moore
28  * Copyright (c) 2002 LSI Logic Corporation
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The party using or redistributing the source code and binary forms
40  *    agrees to the disclaimer below and the terms and conditions set forth
41  *    herein.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  *      $FreeBSD: src/sys/dev/amr/amr_cam.c,v 1.1.2.3 2002/11/11 13:19:10 emoore Exp $
56  *      $DragonFly: src/sys/dev/raid/amr/amr_cam.c,v 1.8 2006/10/25 20:56:00 dillon Exp $
57  */
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/kernel.h>
63
64 #include "amr_compat.h"
65 #include <sys/bus.h>
66 #include <sys/conf.h>
67 #include <sys/devicestat.h>
68 #include <sys/disk.h>
69 #include <sys/stat.h>
70
71 #include <bus/cam/cam.h>
72 #include <bus/cam/cam_ccb.h>
73 #include <bus/cam/cam_sim.h>
74 #include <bus/cam/cam_xpt.h>
75 #include <bus/cam/cam_xpt_sim.h>
76 #include <bus/cam/cam_debug.h>
77 #include <bus/cam/scsi/scsi_all.h>
78 #include <bus/cam/scsi/scsi_message.h>
79
80 #include "amrreg.h"
81 #include "amrvar.h"
82
83 static void             amr_cam_action(struct cam_sim *sim, union ccb *ccb);
84 static void             amr_cam_poll(struct cam_sim *sim);
85 static void             amr_cam_complete(struct amr_command *ac);
86 static void             amr_cam_complete_extcdb(struct amr_command *ac);
87
88
89 /********************************************************************************
90  * Enqueue/dequeue functions
91  */
92 static __inline void
93 amr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
94 {
95     crit_enter();
96     TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
97     crit_exit();
98 }
99
100 static __inline void
101 amr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
102 {
103     crit_enter();
104     TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
105     crit_exit();
106 }
107
108 static __inline union ccb *
109 amr_dequeue_ccb(struct amr_softc *sc)
110 {
111     union ccb   *ccb;
112
113     crit_enter();
114     if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
115         TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
116     crit_exit();
117     return(ccb);
118 }
119
120 /********************************************************************************
121  * Attach our 'real' SCSI channels to CAM
122  */
123 int
124 amr_cam_attach(struct amr_softc *sc)
125 {
126     struct cam_devq     *devq;
127     int                 chn;
128
129     /* initialise the ccb queue */
130     TAILQ_INIT(&sc->amr_cam_ccbq);
131
132     /*
133      * Allocate a devq for all our channels combined.  This should
134      * allow for the maximum number of SCSI commands we will accept
135      * at one time.
136      */
137     if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
138         return(ENOMEM);
139
140     /*
141      * Iterate over our channels, registering them with CAM
142      */
143     for (chn = 0; chn < sc->amr_maxchan; chn++) {
144
145         /* allocate a sim */
146         if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
147                                                   amr_cam_poll,
148                                                   "amr",
149                                                   sc,
150                                                   device_get_unit(sc->amr_dev),
151                                                   1,
152                                                   AMR_MAX_SCSI_CMDS,
153                                                   devq)) == NULL) {
154             device_printf(sc->amr_dev, "CAM SIM attach failed\n");
155             return(ENOMEM);
156         }
157
158         /* register the bus ID so we can get it later */
159         if (xpt_bus_register(sc->amr_cam_sim[chn], chn)) {
160             device_printf(sc->amr_dev, "CAM XPT bus registration failed\n");
161             return(ENXIO);
162         }
163     }
164     cam_simq_release(devq);
165     /*
166      * XXX we should scan the config and work out which devices are actually
167      * protected.
168      */
169     return(0);
170 }
171
172 /********************************************************************************
173  * Disconnect ourselves from CAM
174  */
175 void
176 amr_cam_detach(struct amr_softc *sc)
177 {
178     int         chn;
179
180     /*
181      * If a sim was allocated for a channel, free it
182      */
183     for (chn = 0; chn < sc->amr_maxchan; chn++) {
184         if (sc->amr_cam_sim[chn] != NULL) {
185             xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
186             cam_sim_free(sc->amr_cam_sim[chn]);
187         }
188     }
189 }
190
191 /********************************************************************************
192  ********************************************************************************
193                                                         CAM passthrough interface
194  ********************************************************************************
195  ********************************************************************************/
196
197 /********************************************************************************
198  * Handle a request for action from CAM
199  */
200 static void
201 amr_cam_action(struct cam_sim *sim, union ccb *ccb)
202 {
203     struct amr_softc    *sc = cam_sim_softc(sim);
204
205     switch(ccb->ccb_h.func_code) {
206
207     /*
208      * Perform SCSI I/O to a physical device.
209      */
210     case XPT_SCSI_IO:
211     {
212         struct ccb_hdr          *ccbh = &ccb->ccb_h;
213         struct ccb_scsiio       *csio = &ccb->csio;
214
215         /* Validate the CCB */
216         ccbh->status = CAM_REQ_INPROG;
217
218         /* check the CDB length */
219         if (csio->cdb_len > AMR_MAX_EXTCDB_LEN)
220             ccbh->status = CAM_REQ_CMP_ERR;
221
222         if ((csio->cdb_len > AMR_MAX_CDB_LEN) && (sc->support_ext_cdb == 0 ))
223             ccbh->status = CAM_REQ_CMP_ERR;
224
225         /* check that the CDB pointer is not to a physical address */
226         if ((ccbh->flags & CAM_CDB_POINTER) && (ccbh->flags & CAM_CDB_PHYS))
227             ccbh->status = CAM_REQ_CMP_ERR;
228
229         /* if there is data transfer, it must be to/from a virtual address */
230         if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
231             if (ccbh->flags & CAM_DATA_PHYS)            /* we can't map it */
232                 ccbh->status = CAM_REQ_CMP_ERR;         
233             if (ccbh->flags & CAM_SCATTER_VALID)        /* we want to do the s/g setup */
234                 ccbh->status = CAM_REQ_CMP_ERR;
235         }
236
237         /*
238          * If the command is to a LUN other than 0, fail it.
239          * This is probably incorrect, but during testing the firmware did not
240          * seem to respect the LUN field, and thus devices appear echoed.
241          */
242         if (csio->ccb_h.target_lun != 0)
243             ccbh->status = CAM_REQ_CMP_ERR;
244
245         /* if we're happy with the request, queue it for attention */
246         if (ccbh->status == CAM_REQ_INPROG) {
247
248             /* save the channel number in the ccb */
249             csio->ccb_h.sim_priv.entries[0].field = cam_sim_bus(sim);
250
251             amr_enqueue_ccb(sc, ccb);
252             amr_startio(sc);
253             return;
254         }
255         break;
256     }
257
258     case XPT_CALC_GEOMETRY:
259     {
260         struct    ccb_calc_geometry *ccg = &ccb->ccg;
261         u_int32_t size_in_mb;
262         u_int32_t secs_per_cylinder;
263
264         size_in_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
265
266         if (size_in_mb > 1024) {
267             ccg->heads = 255;
268             ccg->secs_per_track = 63;
269         } else {
270             ccg->heads = 64;
271             ccg->secs_per_track = 32;
272         }
273         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
274         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
275         ccb->ccb_h.status = CAM_REQ_CMP;
276         break;
277     }
278
279     /*
280      * Return path stats.  Some of these should probably be
281      * amended.
282      */
283     case XPT_PATH_INQ:
284     {
285         struct ccb_pathinq      *cpi = & ccb->cpi;
286
287         debug(3, "XPT_PATH_INQ");
288         cpi->version_num = 1;           /* XXX??? */
289         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
290         cpi->target_sprt = 0;
291         cpi->hba_misc = PIM_NOBUSRESET;
292         cpi->hba_eng_cnt = 0;
293         cpi->max_target = AMR_MAX_TARGETS;
294         cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
295         cpi->initiator_id = 7;          /* XXX variable? */
296         strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
297         strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
298         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
299         cpi->unit_number = cam_sim_unit(sim);
300         cpi->bus_id = cam_sim_bus(sim);
301         cpi->base_transfer_speed = 132 * 1024;  /* XXX get from controller? */
302         cpi->ccb_h.status = CAM_REQ_CMP;
303
304         break;
305     }
306
307     case XPT_RESET_BUS:
308     {
309         struct ccb_pathinq      *cpi = & ccb->cpi;
310
311         debug(1, "XPT_RESET_BUS");
312         cpi->ccb_h.status = CAM_REQ_CMP;
313         break;
314     }
315
316     case XPT_RESET_DEV:
317     {
318         debug(1, "XPT_RESET_DEV");
319         ccb->ccb_h.status = CAM_REQ_CMP;
320         break;
321     }
322
323     case XPT_GET_TRAN_SETTINGS:
324     {
325         struct ccb_trans_settings       *cts;
326
327         debug(3, "XPT_GET_TRAN_SETTINGS");
328
329         cts = &(ccb->cts);
330
331         if ((cts->flags & CCB_TRANS_USER_SETTINGS) == 0) {
332                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
333                 break;
334         }
335
336         cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
337         cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
338         cts->sync_period = 6;   /* 40MHz how wide is this bus? */
339         cts->sync_offset = 31;  /* How to extract this from board? */
340
341         cts->valid = CCB_TRANS_SYNC_RATE_VALID
342             | CCB_TRANS_SYNC_OFFSET_VALID
343             | CCB_TRANS_BUS_WIDTH_VALID
344             | CCB_TRANS_DISC_VALID
345             | CCB_TRANS_TQ_VALID;
346         ccb->ccb_h.status = CAM_REQ_CMP;
347         break;
348     }
349
350     case XPT_SET_TRAN_SETTINGS:
351         debug(3, "XPT_SET_TRAN_SETTINGS");
352         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
353         break;
354
355
356     /*
357      * Reject anything else as unsupported.
358      */
359     default:
360         /* we can't do this */
361         ccb->ccb_h.status = CAM_REQ_INVALID;
362         break;
363     }
364     xpt_done(ccb);
365 }
366
367 /********************************************************************************
368  * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI command.
369  */
370 int
371 amr_cam_command(struct amr_softc *sc, struct amr_command **acp)
372 {
373     struct amr_command          *ac;
374     struct amr_passthrough              *ap;
375     struct amr_ext_passthrough  *aep;
376     struct ccb_scsiio                   *csio;
377     int                         bus, target, error;
378
379     error = 0;
380     ac = NULL;
381     ap = NULL;
382     aep = NULL;
383
384     /* check to see if there is a ccb for us to work with */
385     if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
386         goto out;
387
388     /* get bus/target, XXX validate against protected devices? */
389     bus = csio->ccb_h.sim_priv.entries[0].field;
390     target = csio->ccb_h.target_id;
391
392     /*
393      * Build a passthrough command.
394      */
395
396     /* construct passthrough */
397     if (sc->support_ext_cdb ) {
398             aep = kmalloc(sizeof(*aep), M_DEVBUF, M_INTWAIT | M_ZERO);
399             aep->ap_timeout = 2;
400             aep->ap_ars = 1;
401             aep->ap_request_sense_length = 14;
402             aep->ap_islogical = 0;
403             aep->ap_channel = bus;
404             aep->ap_scsi_id = target;
405             aep->ap_logical_drive_no = csio->ccb_h.target_lun;
406             aep->ap_cdb_length = csio->cdb_len;
407             aep->ap_data_transfer_length = csio->dxfer_len;
408             if (csio->ccb_h.flags & CAM_CDB_POINTER) {
409                 bcopy(csio->cdb_io.cdb_ptr, aep->ap_cdb, csio->cdb_len);
410             } else {
411                 bcopy(csio->cdb_io.cdb_bytes, aep->ap_cdb, csio->cdb_len);
412             }
413             /* we leave the data s/g list and s/g count to the map routine later */
414
415             debug(2, " COMMAND %x/%d+%d to %d:%d:%d", aep->ap_cdb[0], aep->ap_cdb_length, csio->dxfer_len,
416                   aep->ap_channel, aep->ap_scsi_id, aep->ap_logical_drive_no);
417
418     } else {
419             ap = kmalloc(sizeof(*ap), M_DEVBUF, M_INTWAIT | M_ZERO);
420             ap->ap_timeout = 0;
421             ap->ap_ars = 1;
422             ap->ap_request_sense_length = 14;
423             ap->ap_islogical = 0;
424             ap->ap_channel = bus;
425             ap->ap_scsi_id = target;
426             ap->ap_logical_drive_no = csio->ccb_h.target_lun;
427             ap->ap_cdb_length = csio->cdb_len;
428             ap->ap_data_transfer_length = csio->dxfer_len;
429             if (csio->ccb_h.flags & CAM_CDB_POINTER) {
430                 bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
431             } else {
432                 bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb, csio->cdb_len);
433             }
434             /* we leave the data s/g list and s/g count to the map routine later */
435
436             debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0], ap->ap_cdb_length, csio->dxfer_len,
437                   ap->ap_channel, ap->ap_scsi_id, ap->ap_logical_drive_no);
438     }
439
440     /* construct command */
441     if ((ac = amr_alloccmd(sc)) == NULL) {
442         error = ENOMEM;
443         goto out;
444     }
445
446     ac->ac_flags |= AMR_CMD_DATAOUT;
447
448     ac->ac_ccb_data = csio->data_ptr;
449     ac->ac_ccb_length = csio->dxfer_len;
450     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
451         ac->ac_flags |= AMR_CMD_CCB_DATAIN;
452     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
453         ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
454
455     ac->ac_private = csio;
456     if ( sc->support_ext_cdb ) {
457             ac->ac_data = aep;
458             ac->ac_length = sizeof(*aep);
459             ac->ac_complete = amr_cam_complete_extcdb;
460             ac->ac_mailbox.mb_command = AMR_CMD_EXTPASS;
461     } else {
462             ac->ac_data = ap;
463             ac->ac_length = sizeof(*ap);
464             ac->ac_complete = amr_cam_complete;
465             ac->ac_mailbox.mb_command = AMR_CMD_PASS;
466     }
467
468 out:
469     if (error != 0) {
470         if (ac != NULL)
471             amr_releasecmd(ac);
472         if (ap != NULL)
473             kfree(ap, M_DEVBUF);
474         if (aep != NULL)
475             kfree(aep, M_DEVBUF);
476         if (csio != NULL)                       /* put it back and try again later */
477             amr_requeue_ccb(sc, (union ccb *)csio);
478     }
479     *acp = ac;
480     return(error);
481 }
482
483 /********************************************************************************
484  * Check for interrupt status
485  */
486 static void
487 amr_cam_poll(struct cam_sim *sim)
488 {
489     amr_done(cam_sim_softc(sim));
490 }
491
492  /********************************************************************************
493  * Handle completion of a command submitted via CAM.
494  */
495 static void
496 amr_cam_complete(struct amr_command *ac)
497 {
498     struct amr_passthrough      *ap = (struct amr_passthrough *)ac->ac_data;
499     struct ccb_scsiio           *csio = (struct ccb_scsiio *)ac->ac_private;
500     struct scsi_inquiry_data    *inq = (struct scsi_inquiry_data *)csio->data_ptr;
501
502     /* XXX note that we're ignoring ac->ac_status - good idea? */
503
504     debug(1, "status 0x%x  scsi_status 0x%x", ac->ac_status, ap->ap_scsi_status);
505
506     /*
507      * Hide disks from CAM so that they're not picked up and treated as 'normal' disks.
508      *
509      * If the configuration provides a mechanism to mark a disk a "not managed", we
510      * could add handling for that to allow disks to be selectively visible.
511      */
512
513     if ((ap->ap_cdb[0] == INQUIRY) && (SID_TYPE(inq) == T_DIRECT)) {
514         bzero(csio->data_ptr, csio->dxfer_len);
515         if (ap->ap_scsi_status == 0xf0) {
516             csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
517         } else {
518             csio->ccb_h.status = CAM_DEV_NOT_THERE;
519         }
520     } else {
521
522         /* handle passthrough SCSI status */
523         switch(ap->ap_scsi_status) {
524         case 0:                         /* completed OK */
525             csio->ccb_h.status = CAM_REQ_CMP;
526             break;
527
528         case 0x02:
529             csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
530             csio->scsi_status = SCSI_STATUS_CHECK_COND;
531             bcopy(ap->ap_request_sense_area, &csio->sense_data, AMR_MAX_REQ_SENSE_LEN);
532             csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
533             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
534             break;
535
536         case 0x08:
537             csio->ccb_h.status = CAM_SCSI_BUSY;
538             break;
539
540         case 0xf0:
541         case 0xf4:
542         default:
543             csio->ccb_h.status = CAM_REQ_CMP_ERR;
544             break;
545         }
546     }
547     kfree(ap, M_DEVBUF);
548     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
549         debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr, " ");
550     xpt_done((union ccb *)csio);
551     amr_releasecmd(ac);
552 }
553
554 /********************************************************************************
555  * Handle completion of a command submitted via CAM.
556  * Completion for extended cdb
557  */
558 static void
559 amr_cam_complete_extcdb(struct amr_command *ac)
560 {
561     struct amr_ext_passthrough      *aep = (struct amr_ext_passthrough *)ac->ac_data;
562     struct ccb_scsiio           *csio = (struct ccb_scsiio *)ac->ac_private;
563     struct scsi_inquiry_data    *inq = (struct scsi_inquiry_data *)csio->data_ptr;
564
565     /* XXX note that we're ignoring ac->ac_status - good idea? */
566
567     debug(1, "status 0x%x  scsi_status 0x%x", ac->ac_status, aep->ap_scsi_status);
568
569     /*
570      * Hide disks from CAM so that they're not picked up and treated as 'normal' disks.
571      *
572      * If the configuration provides a mechanism to mark a disk a "not managed", we
573      * could add handling for that to allow disks to be selectively visible.
574      */
575
576     if ((aep->ap_cdb[0] == INQUIRY) && (SID_TYPE(inq) == T_DIRECT)) {
577         bzero(csio->data_ptr, csio->dxfer_len);
578         if (aep->ap_scsi_status == 0xf0) {
579             csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
580         } else {
581             csio->ccb_h.status = CAM_DEV_NOT_THERE;
582         }
583     } else {
584
585         /* handle passthrough SCSI status */
586         switch(aep->ap_scsi_status) {
587         case 0:                         /* completed OK */
588             csio->ccb_h.status = CAM_REQ_CMP;
589             break;
590
591         case 0x02:
592             csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
593             csio->scsi_status = SCSI_STATUS_CHECK_COND;
594             bcopy(aep->ap_request_sense_area, &csio->sense_data, AMR_MAX_REQ_SENSE_LEN);
595             csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
596             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
597             break;
598
599         case 0x08:
600             csio->ccb_h.status = CAM_SCSI_BUSY;
601             break;
602
603         case 0xf0:
604         case 0xf4:
605         default:
606             csio->ccb_h.status = CAM_REQ_CMP_ERR;
607             break;
608         }
609     }
610     kfree(aep, M_DEVBUF);
611     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
612         debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr, " ");
613     xpt_done((union ccb *)csio);
614     amr_releasecmd(ac);
615 }