xref: /freebsd/sys/dev/aac/aac_cam.c (revision 243e928310d073338c5ec089f0dce238a80b9866)
1 /*-
2  * Copyright (c) 2002 Adaptec, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * CAM front-end for communicating with non-DASD devices
32  */
33 
34 #include "opt_aac.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 
45 #include <cam/cam.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_debug.h>
48 #include <cam/cam_periph.h>
49 #include <cam/cam_sim.h>
50 #include <cam/cam_xpt_sim.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_message.h>
53 
54 #include <sys/bus.h>
55 #include <sys/conf.h>
56 #include <sys/disk.h>
57 
58 #include <machine/md_var.h>
59 #include <machine/bus.h>
60 #include <sys/rman.h>
61 
62 #include <vm/vm.h>
63 #include <vm/pmap.h>
64 
65 #include <dev/aac/aacreg.h>
66 #include <sys/aac_ioctl.h>
67 #include <dev/aac/aacvar.h>
68 
69 struct aac_cam {
70 	device_t		dev;
71 	struct aac_sim		*inf;
72 	struct cam_sim		*sim;
73 	struct cam_path		*path;
74 };
75 
76 static int aac_cam_probe(device_t dev);
77 static int aac_cam_attach(device_t dev);
78 static int aac_cam_detach(device_t dev);
79 static void aac_cam_action(struct cam_sim *, union ccb *);
80 static void aac_cam_poll(struct cam_sim *);
81 static void aac_cam_complete(struct aac_command *);
82 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
83     uint32_t target_id);
84 
85 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
86 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
87 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
88 
89 static devclass_t	aac_pass_devclass;
90 
91 static device_method_t	aac_pass_methods[] = {
92 	DEVMETHOD(device_probe,		aac_cam_probe),
93 	DEVMETHOD(device_attach,	aac_cam_attach),
94 	DEVMETHOD(device_detach,	aac_cam_detach),
95 	DEVMETHOD_END
96 };
97 
98 static driver_t	aac_pass_driver = {
99 	"aacp",
100 	aac_pass_methods,
101 	sizeof(struct aac_cam)
102 };
103 
104 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, NULL, NULL);
105 MODULE_DEPEND(aacp, cam, 1, 1, 1);
106 
107 static MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
108 
109 static void
110 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
111 {
112 	union ccb *ccb;
113 	struct aac_sim *sim;
114 	struct aac_cam *camsc;
115 
116 	if (target_id == AAC_CAM_TARGET_WILDCARD)
117 		target_id = CAM_TARGET_WILDCARD;
118 
119 	TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
120 		camsc = sim->aac_cam;
121 		if (camsc == NULL || camsc->inf == NULL ||
122 		    camsc->inf->BusNumber != channel)
123 			continue;
124 
125 		ccb = xpt_alloc_ccb_nowait();
126 		if (ccb == NULL) {
127 			device_printf(sc->aac_dev,
128 			    "Cannot allocate ccb for bus rescan.\n");
129 			return;
130 		}
131 
132 		if (xpt_create_path(&ccb->ccb_h.path, NULL,
133 		    cam_sim_path(camsc->sim),
134 		    target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
135 			xpt_free_ccb(ccb);
136 			device_printf(sc->aac_dev,
137 			    "Cannot create path for bus rescan.\n");
138 			return;
139 		}
140 		xpt_rescan(ccb);
141 		break;
142 	}
143 }
144 
145 
146 static void
147 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
148 {
149 	union ccb *ccb;
150 	struct aac_cam *camsc;
151 
152 	switch (event->ev_type) {
153 	case AAC_EVENT_CMFREE:
154 		ccb = arg;
155 		camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
156 		free(event, M_AACCAM);
157 		xpt_release_simq(camsc->sim, 1);
158 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
159 		xpt_done(ccb);
160 		break;
161 	default:
162 		device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
163 		    event->ev_type);
164 		break;
165 	}
166 }
167 
168 static int
169 aac_cam_probe(device_t dev)
170 {
171 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
172 
173 	return (0);
174 }
175 
176 static int
177 aac_cam_detach(device_t dev)
178 {
179 	struct aac_softc *sc;
180 	struct aac_cam *camsc;
181 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
182 
183 	camsc = (struct aac_cam *)device_get_softc(dev);
184 	sc = camsc->inf->aac_sc;
185 	camsc->inf->aac_cam = NULL;
186 
187 	mtx_lock(&sc->aac_io_lock);
188 
189 	xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
190 	xpt_free_path(camsc->path);
191 	xpt_bus_deregister(cam_sim_path(camsc->sim));
192 	cam_sim_free(camsc->sim, /*free_devq*/TRUE);
193 
194 	sc->cam_rescan_cb = NULL;
195 
196 	mtx_unlock(&sc->aac_io_lock);
197 
198 	return (0);
199 }
200 
201 /*
202  * Register the driver as a CAM SIM
203  */
204 static int
205 aac_cam_attach(device_t dev)
206 {
207 	struct cam_devq *devq;
208 	struct cam_sim *sim;
209 	struct cam_path *path;
210 	struct aac_cam *camsc;
211 	struct aac_sim *inf;
212 
213 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
214 
215 	camsc = (struct aac_cam *)device_get_softc(dev);
216 	inf = (struct aac_sim *)device_get_ivars(dev);
217 	camsc->inf = inf;
218 	camsc->inf->aac_cam = camsc;
219 
220 	devq = cam_simq_alloc(inf->TargetsPerBus);
221 	if (devq == NULL)
222 		return (EIO);
223 
224 	sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
225 	    device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
226 	if (sim == NULL) {
227 		cam_simq_free(devq);
228 		return (EIO);
229 	}
230 
231 	/* Since every bus has it's own sim, every bus 'appears' as bus 0 */
232 	mtx_lock(&inf->aac_sc->aac_io_lock);
233 	if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
234 		cam_sim_free(sim, TRUE);
235 		mtx_unlock(&inf->aac_sc->aac_io_lock);
236 		return (EIO);
237 	}
238 
239 	if (xpt_create_path(&path, NULL, cam_sim_path(sim),
240 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
241 		xpt_bus_deregister(cam_sim_path(sim));
242 		cam_sim_free(sim, TRUE);
243 		mtx_unlock(&inf->aac_sc->aac_io_lock);
244 		return (EIO);
245 	}
246 	inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
247 	mtx_unlock(&inf->aac_sc->aac_io_lock);
248 
249 	camsc->sim = sim;
250 	camsc->path = path;
251 
252 	return (0);
253 }
254 
255 static void
256 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
257 {
258 	struct	aac_cam *camsc;
259 	struct	aac_softc *sc;
260 	struct	aac_srb *srb;
261 	struct	aac_fib *fib;
262 	struct	aac_command *cm;
263 
264 	camsc = (struct aac_cam *)cam_sim_softc(sim);
265 	sc = camsc->inf->aac_sc;
266 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
267 
268 	/* Synchronous ops, and ops that don't require communication with the
269 	 * controller */
270 	switch(ccb->ccb_h.func_code) {
271 	case XPT_SCSI_IO:
272 	case XPT_RESET_DEV:
273 		/* These are handled down below */
274 		break;
275 	case XPT_CALC_GEOMETRY:
276 	{
277 		struct ccb_calc_geometry *ccg;
278 		u_int32_t size_mb;
279 		u_int32_t secs_per_cylinder;
280 
281 		ccg = &ccb->ccg;
282 		size_mb = ccg->volume_size /
283 		    ((1024L * 1024L) / ccg->block_size);
284 		if (size_mb >= (2 * 1024)) {		/* 2GB */
285 			ccg->heads = 255;
286 			ccg->secs_per_track = 63;
287 		} else if (size_mb >= (1 * 1024)) {	/* 1GB */
288 			ccg->heads = 128;
289 			ccg->secs_per_track = 32;
290 		} else {
291 			ccg->heads = 64;
292 			ccg->secs_per_track = 32;
293 		}
294 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
295 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
296 
297 		ccb->ccb_h.status = CAM_REQ_CMP;
298 		xpt_done(ccb);
299 		return;
300 	}
301 	case XPT_PATH_INQ:
302 	{
303 		struct ccb_pathinq *cpi = &ccb->cpi;
304 
305 		cpi->version_num = 1;
306 		cpi->hba_inquiry = PI_WIDE_16;
307 		cpi->target_sprt = 0;
308 
309 		/*
310 		 * Resetting via the passthrough or parallel bus scan
311 		 * causes problems.
312 		 */
313 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
314 		cpi->hba_eng_cnt = 0;
315 		cpi->max_target = camsc->inf->TargetsPerBus;
316 		cpi->max_lun = 8;	/* Per the controller spec */
317 		cpi->initiator_id = camsc->inf->InitiatorBusId;
318 		cpi->bus_id = camsc->inf->BusNumber;
319 		cpi->base_transfer_speed = 3300;
320 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
321 		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
322 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
323 		cpi->unit_number = cam_sim_unit(sim);
324 		cpi->transport = XPORT_SPI;
325 		cpi->transport_version = 2;
326 		cpi->protocol = PROTO_SCSI;
327 		cpi->protocol_version = SCSI_REV_2;
328 		ccb->ccb_h.status = CAM_REQ_CMP;
329 		xpt_done(ccb);
330 		return;
331 	}
332 	case XPT_GET_TRAN_SETTINGS:
333 	{
334 		struct ccb_trans_settings_scsi *scsi =
335 			&ccb->cts.proto_specific.scsi;
336 		struct ccb_trans_settings_spi *spi =
337 			&ccb->cts.xport_specific.spi;
338 		ccb->cts.protocol = PROTO_SCSI;
339 		ccb->cts.protocol_version = SCSI_REV_2;
340 		ccb->cts.transport = XPORT_SPI;
341 		ccb->cts.transport_version = 2;
342 		if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
343 			scsi->valid = CTS_SCSI_VALID_TQ;
344 			spi->valid |= CTS_SPI_VALID_DISC;
345 		} else {
346 			scsi->valid = 0;
347 		}
348 		ccb->ccb_h.status = CAM_REQ_CMP;
349 		xpt_done(ccb);
350 		return;
351 	}
352 	case XPT_SET_TRAN_SETTINGS:
353 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
354 		xpt_done(ccb);
355 		return;
356 	case XPT_RESET_BUS:
357 		if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
358 			ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
359 		} else {
360 			ccb->ccb_h.status = CAM_REQ_CMP;
361 		}
362 		xpt_done(ccb);
363 		return;
364 	case XPT_ABORT:
365 		ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
366 		xpt_done(ccb);
367 		return;
368 	case XPT_TERM_IO:
369 		ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
370 		xpt_done(ccb);
371 		return;
372 	default:
373 		device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
374 		    ccb->ccb_h.func_code);
375 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
376 		xpt_done(ccb);
377 		return;
378 	}
379 
380 	/* Async ops that require communcation with the controller */
381 
382 	if (aac_alloc_command(sc, &cm)) {
383 		struct aac_event *event;
384 
385 		xpt_freeze_simq(sim, 1);
386 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
387 		ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
388 		event = malloc(sizeof(struct aac_event), M_AACCAM,
389 		    M_NOWAIT | M_ZERO);
390 		if (event == NULL) {
391 			device_printf(sc->aac_dev,
392 			    "Warning, out of memory for event\n");
393 			return;
394 		}
395 		event->ev_callback = aac_cam_event;
396 		event->ev_arg = ccb;
397 		event->ev_type = AAC_EVENT_CMFREE;
398 		aac_add_event(sc, event);
399 		return;
400 	}
401 
402 	fib = cm->cm_fib;
403 	srb = (struct aac_srb *)&fib->data[0];
404 	cm->cm_datalen = 0;
405 
406 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
407 	case CAM_DIR_IN:
408 		srb->flags = AAC_SRB_FLAGS_DATA_IN;
409 		cm->cm_flags |= AAC_CMD_DATAIN;
410 		break;
411 	case CAM_DIR_OUT:
412 		srb->flags = AAC_SRB_FLAGS_DATA_OUT;
413 		cm->cm_flags |= AAC_CMD_DATAOUT;
414 		break;
415 	case CAM_DIR_NONE:
416 		srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
417 		break;
418 	default:
419 		srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
420 		cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
421 		break;
422 	}
423 
424 	switch(ccb->ccb_h.func_code) {
425 	case XPT_SCSI_IO:
426 	{
427 		struct ccb_scsiio *csio = &ccb->csio;
428 
429 		srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
430 
431 		/*
432 		 * Copy the CDB into the SRB.  It's only 6-16 bytes,
433 		 * so a copy is not too expensive.
434 		 */
435 		srb->cdb_len = csio->cdb_len;
436 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
437 			bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
438 			    srb->cdb_len);
439 		else
440 			bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
441 			    srb->cdb_len);
442 
443 		/* Set command */
444 		fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
445 			ScsiPortCommandU64 : ScsiPortCommand;
446 
447 		/* Map the s/g list. XXX 32bit addresses only! */
448 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
449 			switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
450 			case CAM_DATA_VADDR:
451 				srb->data_len = csio->dxfer_len;
452 				/*
453 				 * Arrange things so that the S/G
454 				 * map will get set up automagically
455 				 */
456 				cm->cm_data = (void *)csio->data_ptr;
457 				cm->cm_datalen = csio->dxfer_len;
458 				cm->cm_sgtable = &srb->sg_map;
459 				break;
460 			case CAM_DATA_PADDR:
461 				/* Send a 32bit command */
462 				fib->Header.Command = ScsiPortCommand;
463 				srb->sg_map.SgCount = 1;
464 				srb->sg_map.SgEntry[0].SgAddress =
465 				    (uint32_t)(uintptr_t)csio->data_ptr;
466 				srb->sg_map.SgEntry[0].SgByteCount =
467 				    csio->dxfer_len;
468 				srb->data_len = csio->dxfer_len;
469 				break;
470 			default:
471 				/* XXX Need to handle multiple s/g elements */
472 				panic("aac_cam: multiple s/g elements");
473 			}
474 		} else {
475 			srb->sg_map.SgCount = 0;
476 			srb->sg_map.SgEntry[0].SgByteCount = 0;
477 			srb->data_len = 0;
478 		}
479 
480 		break;
481 	}
482 	case XPT_RESET_DEV:
483 		if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
484 			srb->function = AAC_SRB_FUNC_RESET_DEVICE;
485 			break;
486 		} else {
487 			ccb->ccb_h.status = CAM_REQ_CMP;
488 			xpt_done(ccb);
489 			return;
490 		}
491 	default:
492 		break;
493 	}
494 
495 	srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
496 	srb->target = ccb->ccb_h.target_id;
497 	srb->lun = ccb->ccb_h.target_lun;
498 	srb->timeout = ccb->ccb_h.timeout;	/* XXX */
499 	srb->retry_limit = 0;
500 
501 	cm->cm_complete = aac_cam_complete;
502 	cm->cm_private = ccb;
503 	cm->cm_timestamp = time_uptime;
504 
505 	fib->Header.XferState =
506 	    AAC_FIBSTATE_HOSTOWNED	|
507 	    AAC_FIBSTATE_INITIALISED	|
508 	    AAC_FIBSTATE_FROMHOST	|
509 	    AAC_FIBSTATE_REXPECTED	|
510 	    AAC_FIBSTATE_NORM;
511 	fib->Header.Size = sizeof(struct aac_fib_header) +
512 	    sizeof(struct aac_srb);
513 
514 	aac_enqueue_ready(cm);
515 	aac_startio(cm->cm_sc);
516 }
517 
518 static void
519 aac_cam_poll(struct cam_sim *sim)
520 {
521 	/*
522 	 * Pinging the interrupt routine isn't very safe, nor is it
523 	 * really necessary.  Do nothing.
524 	 */
525 }
526 
527 static void
528 aac_cam_fix_inquiry(struct aac_softc *sc, union ccb *ccb)
529 {
530 	struct scsi_inquiry_data *inq;
531 	uint8_t *data;
532 	uint8_t device, qual;
533 
534 	/* If this is an inquiry command, fake things out */
535 	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
536 		data = ccb->csio.cdb_io.cdb_ptr;
537 	else
538 		data = ccb->csio.cdb_io.cdb_bytes;
539 
540 	if (data[0] != INQUIRY)
541 		return;
542 
543 	if (ccb->ccb_h.status == CAM_REQ_CMP) {
544 		inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
545 		device = SID_TYPE(inq);
546 		qual = SID_QUAL(inq);
547 
548 		/*
549 		 * We want DASD and PROC devices to only be
550 		 * visible through the pass device.
551 		 */
552 		if (((device == T_DIRECT) ||
553 		    (device == T_PROCESSOR) ||
554 		    (sc->flags & AAC_FLAGS_CAM_PASSONLY))) {
555 			/*
556 			 * Some aac(4) adapters will always report that a direct
557 			 * access device is offline in response to a INQUIRY
558 			 * command that does not retrieve vital product data.
559 			 * Force the qualifier to connected so that upper layers
560 			 * correctly recognize that a disk is present.
561 			 */
562 			if ((data[1] & SI_EVPD) == 0 && device == T_DIRECT &&
563 			    qual == SID_QUAL_LU_OFFLINE)
564 				qual = SID_QUAL_LU_CONNECTED;
565 			ccb->csio.data_ptr[0] = (qual << 5) | T_NODEVICE;
566 		}
567 	} else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
568 		ccb->ccb_h.target_lun != 0) {
569 		/* fix for INQUIRYs on Lun>0 */
570 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
571 	}
572 }
573 
574 static void
575 aac_cam_complete(struct aac_command *cm)
576 {
577 	union	ccb *ccb;
578 	struct 	aac_srb_response *srbr;
579 	struct	aac_softc *sc;
580 	int	sense_returned;
581 
582 	sc = cm->cm_sc;
583 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
584 	ccb = cm->cm_private;
585 	srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
586 
587 	if (srbr->fib_status != 0) {
588 		device_printf(sc->aac_dev, "Passthru FIB failed!\n");
589 		ccb->ccb_h.status = CAM_REQ_ABORTED;
590 	} else {
591 		/*
592 		 * The SRB error codes just happen to match the CAM error
593 		 * codes.  How convenient!
594 		 */
595 		ccb->ccb_h.status = srbr->srb_status;
596 
597 		/* Take care of SCSI_IO ops. */
598 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
599 			ccb->csio.scsi_status = srbr->scsi_status;
600 
601 			/* Take care of autosense */
602 			if (srbr->sense_len) {
603 				sense_returned = srbr->sense_len;
604 				if (sense_returned < ccb->csio.sense_len)
605 					ccb->csio.sense_resid =
606 					   ccb->csio.sense_len -
607 					   sense_returned;
608 					else
609 					    ccb->csio.sense_resid = 0;
610 				bzero(&ccb->csio.sense_data,
611 				    sizeof(struct scsi_sense_data));
612 				bcopy(&srbr->sense[0], &ccb->csio.sense_data,
613 				    min(ccb->csio.sense_len, sense_returned));
614 				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
615 				// scsi_sense_print(&ccb->csio);
616 			}
617 
618 			aac_cam_fix_inquiry(sc, ccb);
619 		}
620 	}
621 
622 	aac_release_command(cm);
623 	xpt_done(ccb);
624 }
625 
626 static u_int32_t
627 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
628 {
629 	struct aac_fib *fib;
630 	struct aac_softc *sc;
631 	struct aac_cam *camsc;
632 	struct aac_vmioctl *vmi;
633 	struct aac_resetbus *rbc;
634 	int e;
635 
636 	camsc = (struct aac_cam *)cam_sim_softc(sim);
637 	sc = camsc->inf->aac_sc;
638 
639 	if (sc == NULL) {
640 		printf("aac: Null sc?\n");
641 		return (CAM_REQ_ABORTED);
642 	}
643 
644 	aac_alloc_sync_fib(sc, &fib);
645 
646 	vmi = (struct aac_vmioctl *)&fib->data[0];
647 	bzero(vmi, sizeof(struct aac_vmioctl));
648 
649 	vmi->Command = VM_Ioctl;
650 	vmi->ObjType = FT_DRIVE;
651 	vmi->MethId = sc->scsi_method_id;
652 	vmi->ObjId = 0;
653 	vmi->IoctlCmd = ResetBus;
654 
655 	rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
656 	rbc->BusNumber = camsc->inf->BusNumber;
657 
658 	e = aac_sync_fib(sc, ContainerCommand, 0, fib,
659 	    sizeof(struct aac_vmioctl));
660 	if (e) {
661 		device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
662 		    e);
663 		aac_release_sync_fib(sc);
664 		return (CAM_REQ_ABORTED);
665 	}
666 
667 	aac_release_sync_fib(sc);
668 	return (CAM_REQ_CMP);
669 }
670 
671 static u_int32_t
672 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
673 {
674 	return (CAM_UA_ABORT);
675 }
676 
677 static u_int32_t
678 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
679 {
680 	return (CAM_UA_TERMIO);
681 }
682