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