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