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