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