xref: /freebsd/sys/dev/aacraid/aacraid_cam.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
1 /*-
2  * Copyright (c) 2002-2010 Adaptec, Inc.
3  * Copyright (c) 2010-2012 PMC-Sierra, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * CAM front-end for communicating with non-DASD devices
33  */
34 
35 #include "opt_aacraid.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 
46 #include <cam/cam.h>
47 #include <cam/cam_ccb.h>
48 #include <cam/cam_debug.h>
49 #include <cam/cam_periph.h>
50 #if __FreeBSD_version < 801000
51 #include <cam/cam_xpt_periph.h>
52 #endif
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
57 
58 #include <sys/bus.h>
59 #include <sys/conf.h>
60 #include <sys/disk.h>
61 
62 #include <machine/md_var.h>
63 #include <machine/bus.h>
64 #include <sys/rman.h>
65 
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 
69 #include <dev/aacraid/aacraid_reg.h>
70 #include <sys/aac_ioctl.h>
71 #include <dev/aacraid/aacraid_debug.h>
72 #include <dev/aacraid/aacraid_var.h>
73 
74 #if __FreeBSD_version >= 700025
75 #ifndef	CAM_NEW_TRAN_CODE
76 #define	CAM_NEW_TRAN_CODE	1
77 #endif
78 #endif
79 
80 #ifndef SVPD_SUPPORTED_PAGE_LIST
81 struct scsi_vpd_supported_page_list
82 {
83 	u_int8_t device;
84 	u_int8_t page_code;
85 #define	SVPD_SUPPORTED_PAGE_LIST 0x00
86 	u_int8_t reserved;
87 	u_int8_t length;	/* number of VPD entries */
88 #define	SVPD_SUPPORTED_PAGES_SIZE	251
89 	u_int8_t list[SVPD_SUPPORTED_PAGES_SIZE];
90 };
91 #endif
92 
93 /************************** Version Compatibility *************************/
94 #if	__FreeBSD_version < 700031
95 #define	aac_sim_alloc(a,b,c,d,e,f,g,h,i)	cam_sim_alloc(a,b,c,d,e,g,h,i)
96 #else
97 #define	aac_sim_alloc				cam_sim_alloc
98 #endif
99 
100 struct aac_cam {
101 	device_t		dev;
102 	struct aac_sim		*inf;
103 	struct cam_sim		*sim;
104 	struct cam_path		*path;
105 };
106 
107 static int aac_cam_probe(device_t dev);
108 static int aac_cam_attach(device_t dev);
109 static int aac_cam_detach(device_t dev);
110 static void aac_cam_action(struct cam_sim *, union ccb *);
111 static void aac_cam_poll(struct cam_sim *);
112 static void aac_cam_complete(struct aac_command *);
113 static void aac_container_complete(struct aac_command *);
114 #if __FreeBSD_version >= 700000
115 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
116 	uint32_t target_id);
117 #endif
118 static void aac_set_scsi_error(struct aac_softc *sc, union ccb *ccb,
119 	u_int8_t status, u_int8_t key, u_int8_t asc, u_int8_t ascq);
120 static int aac_load_map_command_sg(struct aac_softc *, struct aac_command *);
121 static u_int64_t aac_eval_blockno(u_int8_t *);
122 static void aac_container_rw_command(struct cam_sim *, union ccb *, u_int8_t *);
123 static void aac_container_special_command(struct cam_sim *, union ccb *,
124 	u_int8_t *);
125 static void aac_passthrough_command(struct cam_sim *, union ccb *);
126 
127 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
128 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
129 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
130 
131 static devclass_t	aacraid_pass_devclass;
132 
133 static device_method_t	aacraid_pass_methods[] = {
134 	DEVMETHOD(device_probe,		aac_cam_probe),
135 	DEVMETHOD(device_attach,	aac_cam_attach),
136 	DEVMETHOD(device_detach,	aac_cam_detach),
137 	{ 0, 0 }
138 };
139 
140 static driver_t	aacraid_pass_driver = {
141 	"aacraidp",
142 	aacraid_pass_methods,
143 	sizeof(struct aac_cam)
144 };
145 
146 DRIVER_MODULE(aacraidp, aacraid, aacraid_pass_driver, aacraid_pass_devclass, 0, 0);
147 MODULE_DEPEND(aacraidp, cam, 1, 1, 1);
148 
149 MALLOC_DEFINE(M_AACRAIDCAM, "aacraidcam", "AACRAID CAM info");
150 
151 static void
152 aac_set_scsi_error(struct aac_softc *sc, union ccb *ccb, u_int8_t status,
153 	u_int8_t key, u_int8_t asc, u_int8_t ascq)
154 {
155 #if __FreeBSD_version >= 900000
156 	struct scsi_sense_data_fixed *sense =
157 		(struct scsi_sense_data_fixed *)&ccb->csio.sense_data;
158 #else
159 	struct scsi_sense_data *sense = &ccb->csio.sense_data;
160 #endif
161 
162 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "Error %d!", status);
163 
164 	ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
165 	ccb->csio.scsi_status = status;
166 	if (status == SCSI_STATUS_CHECK_COND) {
167 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
168 		bzero(&ccb->csio.sense_data, ccb->csio.sense_len);
169 		ccb->csio.sense_data.error_code =
170 			SSD_CURRENT_ERROR | SSD_ERRCODE_VALID;
171 		sense->flags = key;
172 		if (ccb->csio.sense_len >= 14) {
173 			sense->extra_len = 6;
174 			sense->add_sense_code = asc;
175 			sense->add_sense_code_qual = ascq;
176 		}
177 	}
178 }
179 
180 #if __FreeBSD_version >= 700000
181 static void
182 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
183 {
184 	union ccb *ccb;
185 	struct aac_sim *sim;
186 	struct aac_cam *camsc;
187 
188 	if (target_id == AAC_CAM_TARGET_WILDCARD)
189 		target_id = CAM_TARGET_WILDCARD;
190 
191 	TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
192 		camsc = sim->aac_cam;
193 		if (camsc == NULL || camsc->inf == NULL ||
194 		    camsc->inf->BusNumber != channel)
195 			continue;
196 
197 		ccb = xpt_alloc_ccb_nowait();
198 		if (ccb == NULL) {
199 			device_printf(sc->aac_dev,
200 			    "Cannot allocate ccb for bus rescan.\n");
201 			return;
202 		}
203 
204 		if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
205 		    cam_sim_path(camsc->sim),
206 		    target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
207 			xpt_free_ccb(ccb);
208 			device_printf(sc->aac_dev,
209 			    "Cannot create path for bus rescan.\n");
210 			return;
211 		}
212 		xpt_rescan(ccb);
213 		break;
214 	}
215 }
216 #endif
217 
218 static void
219 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
220 {
221 	union ccb *ccb;
222 	struct aac_cam *camsc;
223 
224 	switch (event->ev_type) {
225 	case AAC_EVENT_CMFREE:
226 		ccb = arg;
227 		camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
228 		free(event, M_AACRAIDCAM);
229 		xpt_release_simq(camsc->sim, 1);
230 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
231 		xpt_done(ccb);
232 		break;
233 	default:
234 		device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
235 		    event->ev_type);
236 		break;
237 	}
238 
239 	return;
240 }
241 
242 static int
243 aac_cam_probe(device_t dev)
244 {
245 	struct aac_softc *sc;
246 	struct aac_cam *camsc;
247 
248 	camsc = (struct aac_cam *)device_get_softc(dev);
249 	if (!camsc->inf)
250 		return (0);
251 	sc = camsc->inf->aac_sc;
252 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
253 	return (0);
254 }
255 
256 static int
257 aac_cam_detach(device_t dev)
258 {
259 	struct aac_softc *sc;
260 	struct aac_cam *camsc;
261 
262 	camsc = (struct aac_cam *)device_get_softc(dev);
263 	if (!camsc->inf)
264 		return (0);
265 	sc = camsc->inf->aac_sc;
266 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
267 	camsc->inf->aac_cam = NULL;
268 
269 	mtx_lock(&sc->aac_io_lock);
270 
271 	xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
272 	xpt_free_path(camsc->path);
273 	xpt_bus_deregister(cam_sim_path(camsc->sim));
274 	cam_sim_free(camsc->sim, /*free_devq*/TRUE);
275 
276 	sc->cam_rescan_cb = NULL;
277 
278 	mtx_unlock(&sc->aac_io_lock);
279 
280 	return (0);
281 }
282 
283 /*
284  * Register the driver as a CAM SIM
285  */
286 static int
287 aac_cam_attach(device_t dev)
288 {
289 	struct cam_devq *devq;
290 	struct cam_sim *sim;
291 	struct cam_path *path;
292 	struct aac_cam *camsc;
293 	struct aac_sim *inf;
294 
295 	camsc = (struct aac_cam *)device_get_softc(dev);
296 	inf = (struct aac_sim *)device_get_ivars(dev);
297 	if (!inf)
298 		return (EIO);
299 	fwprintf(inf->aac_sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
300 	camsc->inf = inf;
301 	camsc->inf->aac_cam = camsc;
302 
303 	devq = cam_simq_alloc(inf->TargetsPerBus);
304 	if (devq == NULL)
305 		return (EIO);
306 
307 	sim = aac_sim_alloc(aac_cam_action, aac_cam_poll, "aacraidp", camsc,
308 	    device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
309 	if (sim == NULL) {
310 		cam_simq_free(devq);
311 		return (EIO);
312 	}
313 
314 	/* Since every bus has it's own sim, every bus 'appears' as bus 0 */
315 	mtx_lock(&inf->aac_sc->aac_io_lock);
316 	if (aac_xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
317 		cam_sim_free(sim, TRUE);
318 		mtx_unlock(&inf->aac_sc->aac_io_lock);
319 		return (EIO);
320 	}
321 
322 	if (xpt_create_path(&path, NULL, cam_sim_path(sim),
323 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
324 		xpt_bus_deregister(cam_sim_path(sim));
325 		cam_sim_free(sim, TRUE);
326 		mtx_unlock(&inf->aac_sc->aac_io_lock);
327 		return (EIO);
328 	}
329 
330 #if __FreeBSD_version >= 700000
331 	inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
332 #endif
333 	mtx_unlock(&inf->aac_sc->aac_io_lock);
334 
335 	camsc->sim = sim;
336 	camsc->path = path;
337 
338 	return (0);
339 }
340 
341 static u_int64_t
342 aac_eval_blockno(u_int8_t *cmdp)
343 {
344 	u_int64_t blockno;
345 
346 	switch (cmdp[0]) {
347 	case READ_6:
348 	case WRITE_6:
349 		blockno = scsi_3btoul(((struct scsi_rw_6 *)cmdp)->addr);
350 		break;
351 	case READ_10:
352 	case WRITE_10:
353 		blockno = scsi_4btoul(((struct scsi_rw_10 *)cmdp)->addr);
354 		break;
355 	case READ_12:
356 	case WRITE_12:
357 		blockno = scsi_4btoul(((struct scsi_rw_12 *)cmdp)->addr);
358 		break;
359 	case READ_16:
360 	case WRITE_16:
361 		blockno = scsi_8btou64(((struct scsi_rw_16 *)cmdp)->addr);
362 		break;
363 	default:
364 		blockno = 0;
365 		break;
366 	}
367 	return(blockno);
368 }
369 
370 static void
371 aac_container_rw_command(struct cam_sim *sim, union ccb *ccb, u_int8_t *cmdp)
372 {
373 	struct	aac_cam *camsc;
374 	struct	aac_softc *sc;
375 	struct	aac_command *cm;
376 	struct	aac_fib *fib;
377 	u_int64_t blockno;
378 
379 	camsc = (struct aac_cam *)cam_sim_softc(sim);
380 	sc = camsc->inf->aac_sc;
381 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
382 
383 	if (aacraid_alloc_command(sc, &cm)) {
384 		struct aac_event *event;
385 
386 		xpt_freeze_simq(sim, 1);
387 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
388 		ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
389 		event = malloc(sizeof(struct aac_event), M_AACRAIDCAM,
390 		    M_NOWAIT | M_ZERO);
391 		if (event == NULL) {
392 			device_printf(sc->aac_dev,
393 			    "Warning, out of memory for event\n");
394 			return;
395 		}
396 		event->ev_callback = aac_cam_event;
397 		event->ev_arg = ccb;
398 		event->ev_type = AAC_EVENT_CMFREE;
399 		aacraid_add_event(sc, event);
400 		return;
401 	}
402 
403 	fib = cm->cm_fib;
404 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
405 	case CAM_DIR_IN:
406 		cm->cm_flags |= AAC_CMD_DATAIN;
407 		break;
408 	case CAM_DIR_OUT:
409 		cm->cm_flags |= AAC_CMD_DATAOUT;
410 		break;
411 	case CAM_DIR_NONE:
412 		break;
413 	default:
414 		cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
415 		break;
416 	}
417 
418 	blockno = aac_eval_blockno(cmdp);
419 
420 	cm->cm_complete = aac_container_complete;
421 	cm->cm_ccb = ccb;
422 	cm->cm_timestamp = time_uptime;
423 	cm->cm_data = (void *)ccb->csio.data_ptr;
424 	cm->cm_datalen = ccb->csio.dxfer_len;
425 
426 	fib->Header.Size = sizeof(struct aac_fib_header);
427 	fib->Header.XferState =
428 		AAC_FIBSTATE_HOSTOWNED   |
429 		AAC_FIBSTATE_INITIALISED |
430 		AAC_FIBSTATE_EMPTY	 |
431 		AAC_FIBSTATE_FROMHOST	 |
432 		AAC_FIBSTATE_REXPECTED   |
433 		AAC_FIBSTATE_NORM	 |
434 		AAC_FIBSTATE_ASYNC	 |
435 		AAC_FIBSTATE_FAST_RESPONSE;
436 
437 	if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE2) {
438 		struct aac_raw_io2 *raw;
439 		raw = (struct aac_raw_io2 *)&fib->data[0];
440 		bzero(raw, sizeof(struct aac_raw_io2));
441 		fib->Header.Command = RawIo2;
442 		raw->strtBlkLow = (u_int32_t)blockno;
443 		raw->strtBlkHigh = (u_int32_t)(blockno >> 32);
444 		raw->byteCnt = cm->cm_datalen;
445 		raw->ldNum = ccb->ccb_h.target_id;
446 		fib->Header.Size += sizeof(struct aac_raw_io2);
447 		cm->cm_sgtable = (struct aac_sg_table *)raw->sge;
448 		if (cm->cm_flags & AAC_CMD_DATAIN)
449 			raw->flags = RIO2_IO_TYPE_READ | RIO2_SG_FORMAT_IEEE1212;
450 		else
451 			raw->flags = RIO2_IO_TYPE_WRITE | RIO2_SG_FORMAT_IEEE1212;
452 	} else if (sc->flags & AAC_FLAGS_RAW_IO) {
453 		struct aac_raw_io *raw;
454 		raw = (struct aac_raw_io *)&fib->data[0];
455 		bzero(raw, sizeof(struct aac_raw_io));
456 		fib->Header.Command = RawIo;
457 		raw->BlockNumber = blockno;
458 		raw->ByteCount = cm->cm_datalen;
459 		raw->ContainerId = ccb->ccb_h.target_id;
460 		fib->Header.Size += sizeof(struct aac_raw_io);
461 		cm->cm_sgtable = (struct aac_sg_table *)
462 			&raw->SgMapRaw;
463 		if (cm->cm_flags & AAC_CMD_DATAIN)
464 			raw->Flags = 1;
465 	} else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
466 		fib->Header.Command = ContainerCommand;
467 		if (cm->cm_flags & AAC_CMD_DATAIN) {
468 			struct aac_blockread *br;
469 			br = (struct aac_blockread *)&fib->data[0];
470 			br->Command = VM_CtBlockRead;
471 			br->ContainerId = ccb->ccb_h.target_id;
472 			br->BlockNumber = blockno;
473 			br->ByteCount = cm->cm_datalen;
474 			fib->Header.Size += sizeof(struct aac_blockread);
475 			cm->cm_sgtable = &br->SgMap;
476 		} else {
477 			struct aac_blockwrite *bw;
478 			bw = (struct aac_blockwrite *)&fib->data[0];
479 			bw->Command = VM_CtBlockWrite;
480 			bw->ContainerId = ccb->ccb_h.target_id;
481 			bw->BlockNumber = blockno;
482 			bw->ByteCount = cm->cm_datalen;
483 			bw->Stable = CUNSTABLE;
484 			fib->Header.Size += sizeof(struct aac_blockwrite);
485 			cm->cm_sgtable = &bw->SgMap;
486 		}
487 	} else {
488 		fib->Header.Command = ContainerCommand64;
489 		if (cm->cm_flags & AAC_CMD_DATAIN) {
490 			struct aac_blockread64 *br;
491 			br = (struct aac_blockread64 *)&fib->data[0];
492 			br->Command = VM_CtHostRead64;
493 			br->ContainerId = ccb->ccb_h.target_id;
494 			br->SectorCount = cm->cm_datalen/AAC_BLOCK_SIZE;
495 			br->BlockNumber = blockno;
496 			br->Pad = 0;
497 			br->Flags = 0;
498 			fib->Header.Size += sizeof(struct aac_blockread64);
499 			cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
500 		} else {
501 			struct aac_blockwrite64 *bw;
502 			bw = (struct aac_blockwrite64 *)&fib->data[0];
503 			bw->Command = VM_CtHostWrite64;
504 			bw->ContainerId = ccb->ccb_h.target_id;
505 			bw->SectorCount = cm->cm_datalen/AAC_BLOCK_SIZE;
506 			bw->BlockNumber = blockno;
507 			bw->Pad = 0;
508 			bw->Flags = 0;
509 			fib->Header.Size += sizeof(struct aac_blockwrite64);
510 			cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
511 		}
512 	}
513 	aac_enqueue_ready(cm);
514 	aacraid_startio(cm->cm_sc);
515 }
516 
517 static void
518 aac_container_special_command(struct cam_sim *sim, union ccb *ccb,
519 	u_int8_t *cmdp)
520 {
521 	struct	aac_cam *camsc;
522 	struct	aac_softc *sc;
523 	struct	aac_container *co;
524 
525 	camsc = (struct aac_cam *)cam_sim_softc(sim);
526 	sc = camsc->inf->aac_sc;
527 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
528 
529 	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
530 		fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "found container %d search for %d", co->co_mntobj.ObjectId, ccb->ccb_h.target_id);
531 		if (co->co_mntobj.ObjectId == ccb->ccb_h.target_id)
532 			break;
533 	}
534 	if (co == NULL || ccb->ccb_h.target_lun != 0) {
535 		fwprintf(sc, HBA_FLAGS_DBG_ERROR_B,
536 			"Container not present: cmd 0x%x id %d lun %d len %d",
537 			*cmdp, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
538 			ccb->csio.dxfer_len);
539 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
540 		xpt_done(ccb);
541 		return;
542 	}
543 
544 	if (ccb->csio.dxfer_len)
545 		bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
546 
547 	switch (*cmdp) {
548 	case INQUIRY:
549 	{
550 		struct scsi_inquiry *inq = (struct scsi_inquiry *)cmdp;
551 
552 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
553 		"Container INQUIRY id %d lun %d len %d VPD 0x%x Page 0x%x",
554 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
555 			ccb->csio.dxfer_len, inq->byte2, inq->page_code);
556 		if (!(inq->byte2 & SI_EVPD)) {
557 			struct scsi_inquiry_data *p =
558 				(struct scsi_inquiry_data *)ccb->csio.data_ptr;
559 			if (inq->page_code != 0) {
560 				aac_set_scsi_error(sc, ccb,
561 					SCSI_STATUS_CHECK_COND,
562 					SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x00);
563 				xpt_done(ccb);
564 				return;
565 			}
566 			p->device = T_DIRECT;
567 			p->version = SCSI_REV_SPC2;
568 			p->response_format = 2;
569 			if (ccb->csio.dxfer_len >= 36) {
570 				p->additional_length = 31;
571 				p->flags = SID_WBus16|SID_Sync|SID_CmdQue;
572 				/* OEM Vendor defines */
573 				strcpy(p->vendor,"Adaptec ");
574 				strcpy(p->product,"Array           ");
575 				strcpy(p->revision,"V1.0");
576 			}
577 		} else {
578 			if (inq->page_code == SVPD_SUPPORTED_PAGE_LIST) {
579 				struct scsi_vpd_supported_page_list *p =
580 					(struct scsi_vpd_supported_page_list *)
581 					ccb->csio.data_ptr;
582 				p->device = T_DIRECT;
583 				p->page_code = SVPD_SUPPORTED_PAGE_LIST;
584 				p->length = 2;
585 				p->list[0] = SVPD_SUPPORTED_PAGE_LIST;
586 				p->list[1] = SVPD_UNIT_SERIAL_NUMBER;
587 			} else if (inq->page_code == SVPD_UNIT_SERIAL_NUMBER) {
588 				struct scsi_vpd_unit_serial_number *p =
589 					(struct scsi_vpd_unit_serial_number *)
590 					ccb->csio.data_ptr;
591 				p->device = T_DIRECT;
592 				p->page_code = SVPD_UNIT_SERIAL_NUMBER;
593 				p->length = sprintf((char *)p->serial_num,
594 					"%08X%02X", co->co_uid,
595 					ccb->ccb_h.target_id);
596 			} else {
597 				aac_set_scsi_error(sc, ccb,
598 					SCSI_STATUS_CHECK_COND,
599 					SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x00);
600 				xpt_done(ccb);
601 				return;
602 			}
603 		}
604 		ccb->ccb_h.status = CAM_REQ_CMP;
605 		break;
606 	}
607 
608 	case REPORT_LUNS:
609 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
610 		"Container REPORT_LUNS id %d lun %d len %d",
611 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
612 		ccb->csio.dxfer_len);
613 		ccb->ccb_h.status = CAM_REQ_CMP;
614 		break;
615 
616 	case START_STOP:
617 	{
618 		struct scsi_start_stop_unit *ss =
619 			(struct scsi_start_stop_unit *)cmdp;
620 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
621 		"Container START_STOP id %d lun %d len %d",
622 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
623 		ccb->csio.dxfer_len);
624 		if (sc->aac_support_opt2 & AAC_SUPPORTED_POWER_MANAGEMENT) {
625 			struct aac_command *cm;
626 			struct aac_fib *fib;
627 			struct aac_cnt_config *ccfg;
628 
629 			if (aacraid_alloc_command(sc, &cm)) {
630 				struct aac_event *event;
631 
632 				xpt_freeze_simq(sim, 1);
633 				ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
634 				ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
635 				event = malloc(sizeof(struct aac_event), M_AACRAIDCAM,
636 					M_NOWAIT | M_ZERO);
637 				if (event == NULL) {
638 					device_printf(sc->aac_dev,
639 						"Warning, out of memory for event\n");
640 					return;
641 				}
642 				event->ev_callback = aac_cam_event;
643 				event->ev_arg = ccb;
644 				event->ev_type = AAC_EVENT_CMFREE;
645 				aacraid_add_event(sc, event);
646 				return;
647 			}
648 
649 			fib = cm->cm_fib;
650 			cm->cm_timestamp = time_uptime;
651 			cm->cm_datalen = 0;
652 
653 			fib->Header.Size =
654 				sizeof(struct aac_fib_header) + sizeof(struct aac_cnt_config);
655 			fib->Header.XferState =
656 				AAC_FIBSTATE_HOSTOWNED   |
657 				AAC_FIBSTATE_INITIALISED |
658 				AAC_FIBSTATE_EMPTY	 |
659 				AAC_FIBSTATE_FROMHOST	 |
660 				AAC_FIBSTATE_REXPECTED   |
661 				AAC_FIBSTATE_NORM	 |
662 				AAC_FIBSTATE_ASYNC	 |
663 				AAC_FIBSTATE_FAST_RESPONSE;
664 			fib->Header.Command = ContainerCommand;
665 
666 			/* Start unit */
667 			ccfg = (struct aac_cnt_config *)&fib->data[0];
668 			bzero(ccfg, sizeof (*ccfg) - CT_PACKET_SIZE);
669 			ccfg->Command = VM_ContainerConfig;
670 			ccfg->CTCommand.command = CT_PM_DRIVER_SUPPORT;
671 			ccfg->CTCommand.param[0] = (ss->how & SSS_START ?
672 				AAC_PM_DRIVERSUP_START_UNIT :
673 				AAC_PM_DRIVERSUP_STOP_UNIT);
674 			ccfg->CTCommand.param[1] = co->co_mntobj.ObjectId;
675 			ccfg->CTCommand.param[2] = 0;	/* 1 - immediate */
676 
677 			if (aacraid_wait_command(cm) != 0 ||
678 				*(u_int32_t *)&fib->data[0] != 0) {
679 				printf("Power Management: Error start/stop container %d\n",
680 				co->co_mntobj.ObjectId);
681 			}
682 			aacraid_release_command(cm);
683 		}
684 		ccb->ccb_h.status = CAM_REQ_CMP;
685 		break;
686 	}
687 
688 	case TEST_UNIT_READY:
689 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
690 		"Container TEST_UNIT_READY id %d lun %d len %d",
691 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
692 		ccb->csio.dxfer_len);
693 		ccb->ccb_h.status = CAM_REQ_CMP;
694 		break;
695 
696 	case REQUEST_SENSE:
697 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
698 		"Container REQUEST_SENSE id %d lun %d len %d",
699 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
700 		ccb->csio.dxfer_len);
701 		ccb->ccb_h.status = CAM_REQ_CMP;
702 		break;
703 
704 	case READ_CAPACITY:
705 	{
706 		struct scsi_read_capacity_data *p =
707 			(struct scsi_read_capacity_data *)ccb->csio.data_ptr;
708 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
709 		"Container READ_CAPACITY id %d lun %d len %d",
710 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
711 		ccb->csio.dxfer_len);
712 		scsi_ulto4b(co->co_mntobj.ObjExtension.BlockSize, p->length);
713 		/* check if greater than 2TB */
714 		if (co->co_mntobj.CapacityHigh) {
715 			if (sc->flags & AAC_FLAGS_LBA_64BIT)
716 				scsi_ulto4b(0xffffffff, p->addr);
717 		} else {
718 			scsi_ulto4b(co->co_mntobj.Capacity-1, p->addr);
719 		}
720 		ccb->ccb_h.status = CAM_REQ_CMP;
721 		break;
722 	}
723 
724 	case SERVICE_ACTION_IN:
725 	{
726 		struct scsi_read_capacity_data_long *p =
727 			(struct scsi_read_capacity_data_long *)
728 			ccb->csio.data_ptr;
729 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
730 		"Container SERVICE_ACTION_IN id %d lun %d len %d",
731 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
732 		ccb->csio.dxfer_len);
733 		if (((struct scsi_read_capacity_16 *)cmdp)->service_action !=
734 			SRC16_SERVICE_ACTION) {
735 			aac_set_scsi_error(sc, ccb, SCSI_STATUS_CHECK_COND,
736 				SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x00);
737 			xpt_done(ccb);
738 			return;
739 		}
740 		scsi_ulto4b(co->co_mntobj.ObjExtension.BlockSize, p->length);
741 		scsi_ulto4b(co->co_mntobj.CapacityHigh, p->addr);
742 		scsi_ulto4b(co->co_mntobj.Capacity-1, &p->addr[4]);
743 		ccb->ccb_h.status = CAM_REQ_CMP;
744 		break;
745 	}
746 
747 	case MODE_SENSE_6:
748 	{
749 		struct scsi_mode_sense_6 *msp =(struct scsi_mode_sense_6 *)cmdp;
750 		struct ms6_data {
751 			struct scsi_mode_hdr_6 hd;
752 			struct scsi_mode_block_descr bd;
753 			char pages;
754 		} *p = (struct ms6_data *)ccb->csio.data_ptr;
755 		char *pagep;
756 		int return_all_pages = FALSE;
757 
758 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
759 		"Container MODE_SENSE id %d lun %d len %d page %d",
760 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
761 		ccb->csio.dxfer_len, msp->page);
762 		p->hd.datalen = sizeof(struct scsi_mode_hdr_6) - 1;
763 		if (co->co_mntobj.ContentState & AAC_FSCS_READONLY)
764 			p->hd.dev_specific = 0x80;	/* WP */
765 		p->hd.dev_specific |= 0x10;	/* DPOFUA */
766 		if (msp->byte2 & SMS_DBD) {
767 			p->hd.block_descr_len = 0;
768 		} else {
769 			p->hd.block_descr_len =
770 				sizeof(struct scsi_mode_block_descr);
771 			p->hd.datalen += p->hd.block_descr_len;
772 			scsi_ulto3b(co->co_mntobj.ObjExtension.BlockSize, p->bd.block_len);
773 			if (co->co_mntobj.Capacity > 0xffffff ||
774 				co->co_mntobj.CapacityHigh) {
775 				p->bd.num_blocks[0] = 0xff;
776 				p->bd.num_blocks[1] = 0xff;
777 				p->bd.num_blocks[2] = 0xff;
778 			} else {
779 				p->bd.num_blocks[0] = (u_int8_t)
780 					(co->co_mntobj.Capacity >> 16);
781 				p->bd.num_blocks[1] = (u_int8_t)
782 					(co->co_mntobj.Capacity >> 8);
783 				p->bd.num_blocks[2] = (u_int8_t)
784 					(co->co_mntobj.Capacity);
785 			}
786 		}
787 		pagep = &p->pages;
788 		switch (msp->page & SMS_PAGE_CODE) {
789 		case SMS_ALL_PAGES_PAGE:
790 			return_all_pages = TRUE;
791 		case SMS_CONTROL_MODE_PAGE:
792 		{
793 			struct scsi_control_page *cp =
794 				(struct scsi_control_page *)pagep;
795 
796 			if (ccb->csio.dxfer_len <= p->hd.datalen + 8) {
797 				aac_set_scsi_error(sc, ccb,
798 					SCSI_STATUS_CHECK_COND,
799 					SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x00);
800 				xpt_done(ccb);
801 				return;
802 			}
803 			cp->page_code = SMS_CONTROL_MODE_PAGE;
804 			cp->page_length = 6;
805 			p->hd.datalen += 8;
806 			pagep += 8;
807 			if (!return_all_pages)
808 				break;
809 		}
810 		case SMS_VENDOR_SPECIFIC_PAGE:
811 			break;
812 		default:
813 			aac_set_scsi_error(sc, ccb, SCSI_STATUS_CHECK_COND,
814 				SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x00);
815 			xpt_done(ccb);
816 			return;
817 		}
818 		ccb->ccb_h.status = CAM_REQ_CMP;
819 		break;
820 	}
821 
822 	case SYNCHRONIZE_CACHE:
823 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B,
824 		"Container SYNCHRONIZE_CACHE id %d lun %d len %d",
825 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
826 		ccb->csio.dxfer_len);
827 		ccb->ccb_h.status = CAM_REQ_CMP;
828 		break;
829 
830 	default:
831 		fwprintf(sc, HBA_FLAGS_DBG_ERROR_B,
832 		"Container unsupp. cmd 0x%x id %d lun %d len %d",
833 		*cmdp, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
834 		ccb->csio.dxfer_len);
835 		ccb->ccb_h.status = CAM_REQ_CMP; /*CAM_REQ_INVALID*/
836 		break;
837 	}
838 	xpt_done(ccb);
839 }
840 
841 static void
842 aac_passthrough_command(struct cam_sim *sim, union ccb *ccb)
843 {
844 	struct	aac_cam *camsc;
845 	struct	aac_softc *sc;
846 	struct	aac_command *cm;
847 	struct	aac_fib *fib;
848 	struct	aac_srb *srb;
849 
850 	camsc = (struct aac_cam *)cam_sim_softc(sim);
851 	sc = camsc->inf->aac_sc;
852 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
853 
854 	if (aacraid_alloc_command(sc, &cm)) {
855 		struct aac_event *event;
856 
857 		xpt_freeze_simq(sim, 1);
858 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
859 		ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
860 		event = malloc(sizeof(struct aac_event), M_AACRAIDCAM,
861 		    M_NOWAIT | M_ZERO);
862 		if (event == NULL) {
863 			device_printf(sc->aac_dev,
864 			    "Warning, out of memory for event\n");
865 			return;
866 		}
867 		event->ev_callback = aac_cam_event;
868 		event->ev_arg = ccb;
869 		event->ev_type = AAC_EVENT_CMFREE;
870 		aacraid_add_event(sc, event);
871 		return;
872 	}
873 
874 	fib = cm->cm_fib;
875 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
876 	case CAM_DIR_IN:
877 		cm->cm_flags |= AAC_CMD_DATAIN;
878 		break;
879 	case CAM_DIR_OUT:
880 		cm->cm_flags |= AAC_CMD_DATAOUT;
881 		break;
882 	case CAM_DIR_NONE:
883 		break;
884 	default:
885 		cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
886 		break;
887 	}
888 
889 	srb = (struct aac_srb *)&fib->data[0];
890 	srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
891 	if (cm->cm_flags & (AAC_CMD_DATAIN|AAC_CMD_DATAOUT))
892 		srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
893 	if (cm->cm_flags & AAC_CMD_DATAIN)
894 		srb->flags = AAC_SRB_FLAGS_DATA_IN;
895 	else if (cm->cm_flags & AAC_CMD_DATAOUT)
896 		srb->flags = AAC_SRB_FLAGS_DATA_OUT;
897 	else
898 		srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
899 
900 	/*
901 	 * Copy the CDB into the SRB.  It's only 6-16 bytes,
902 	 * so a copy is not too expensive.
903 	 */
904 	srb->cdb_len = ccb->csio.cdb_len;
905 	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
906 		bcopy(ccb->csio.cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
907 			srb->cdb_len);
908 	else
909 		bcopy(ccb->csio.cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
910 			srb->cdb_len);
911 
912 	/* Set command */
913 	fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
914 		ScsiPortCommandU64 : ScsiPortCommand;
915 	fib->Header.Size = sizeof(struct aac_fib_header) +
916 			sizeof(struct aac_srb);
917 
918 	/* Map the s/g list */
919 	cm->cm_sgtable = &srb->sg_map;
920 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
921 		/*
922 		 * Arrange things so that the S/G
923 		 * map will get set up automagically
924 		 */
925 		cm->cm_data = (void *)ccb->csio.data_ptr;
926 		cm->cm_datalen = ccb->csio.dxfer_len;
927 		srb->data_len = ccb->csio.dxfer_len;
928 	} else {
929 		cm->cm_data = NULL;
930 		cm->cm_datalen = 0;
931 		srb->data_len = 0;
932 	}
933 
934 	srb->bus = camsc->inf->BusNumber - 1; /* Bus no. rel. to the card */
935 	srb->target = ccb->ccb_h.target_id;
936 	srb->lun = ccb->ccb_h.target_lun;
937 	srb->timeout = ccb->ccb_h.timeout;	/* XXX */
938 	srb->retry_limit = 0;
939 
940 	cm->cm_complete = aac_cam_complete;
941 	cm->cm_ccb = ccb;
942 	cm->cm_timestamp = time_uptime;
943 
944 	fib->Header.XferState =
945 			AAC_FIBSTATE_HOSTOWNED	|
946 			AAC_FIBSTATE_INITIALISED	|
947 			AAC_FIBSTATE_FROMHOST	|
948 			AAC_FIBSTATE_REXPECTED	|
949 			AAC_FIBSTATE_NORM	|
950 			AAC_FIBSTATE_ASYNC	 |
951 			AAC_FIBSTATE_FAST_RESPONSE;
952 
953 	aac_enqueue_ready(cm);
954 	aacraid_startio(cm->cm_sc);
955 }
956 
957 static void
958 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
959 {
960 	struct	aac_cam *camsc;
961 	struct	aac_softc *sc;
962 
963 	camsc = (struct aac_cam *)cam_sim_softc(sim);
964 	sc = camsc->inf->aac_sc;
965 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
966 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
967 
968 	/* Synchronous ops, and ops that don't require communication with the
969 	 * controller */
970 	switch(ccb->ccb_h.func_code) {
971 	case XPT_SCSI_IO:
972 		/* This is handled down below */
973 		break;
974 	case XPT_CALC_GEOMETRY:
975 	{
976 		struct ccb_calc_geometry *ccg;
977 		u_int32_t size_mb;
978 		u_int32_t secs_per_cylinder;
979 
980 		ccg = &ccb->ccg;
981 		size_mb = ccg->volume_size /
982 		    ((1024L * 1024L) / ccg->block_size);
983 		if (size_mb >= (2 * 1024)) {		/* 2GB */
984 			ccg->heads = 255;
985 			ccg->secs_per_track = 63;
986 		} else if (size_mb >= (1 * 1024)) {	/* 1GB */
987 			ccg->heads = 128;
988 			ccg->secs_per_track = 32;
989 		} else {
990 			ccg->heads = 64;
991 			ccg->secs_per_track = 32;
992 		}
993 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
994 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
995 
996 		ccb->ccb_h.status = CAM_REQ_CMP;
997 		xpt_done(ccb);
998 		return;
999 	}
1000 	case XPT_PATH_INQ:
1001 	{
1002 		struct ccb_pathinq *cpi = &ccb->cpi;
1003 
1004 		cpi->version_num = 1;
1005 		cpi->target_sprt = 0;
1006 		cpi->hba_eng_cnt = 0;
1007 		cpi->max_target = camsc->inf->TargetsPerBus;
1008 		cpi->max_lun = 8;	/* Per the controller spec */
1009 		cpi->initiator_id = camsc->inf->InitiatorBusId;
1010 		cpi->bus_id = camsc->inf->BusNumber;
1011 #if __FreeBSD_version >= 800000
1012 		cpi->maxio = sc->aac_max_sectors << 9;
1013 #endif
1014 
1015 		/*
1016 		 * Resetting via the passthrough or parallel bus scan
1017 		 * causes problems.
1018 		 */
1019 		cpi->hba_misc = PIM_NOBUSRESET;
1020 		cpi->hba_inquiry = PI_TAG_ABLE;
1021 		cpi->base_transfer_speed = 300000;
1022 #ifdef CAM_NEW_TRAN_CODE
1023 		cpi->hba_misc |= PIM_SEQSCAN;
1024 		cpi->protocol = PROTO_SCSI;
1025 		cpi->transport = XPORT_SAS;
1026 		cpi->transport_version = 0;
1027 		cpi->protocol_version = SCSI_REV_SPC2;
1028 #endif
1029 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1030 		strncpy(cpi->hba_vid, "PMC-Sierra", HBA_IDLEN);
1031 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1032 		cpi->unit_number = cam_sim_unit(sim);
1033 		ccb->ccb_h.status = CAM_REQ_CMP;
1034 		xpt_done(ccb);
1035 		return;
1036 	}
1037 	case XPT_GET_TRAN_SETTINGS:
1038 	{
1039 #ifdef CAM_NEW_TRAN_CODE
1040 		struct ccb_trans_settings_scsi *scsi =
1041 			&ccb->cts.proto_specific.scsi;
1042 		struct ccb_trans_settings_spi *spi =
1043 			&ccb->cts.xport_specific.spi;
1044 		ccb->cts.protocol = PROTO_SCSI;
1045 		ccb->cts.protocol_version = SCSI_REV_SPC2;
1046 		ccb->cts.transport = XPORT_SAS;
1047 		ccb->cts.transport_version = 0;
1048 		scsi->valid = CTS_SCSI_VALID_TQ;
1049 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1050 		spi->valid |= CTS_SPI_VALID_DISC;
1051 		spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1052 #else
1053 		ccb->cts.flags = ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
1054 		ccb->cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
1055 #endif
1056 		ccb->ccb_h.status = CAM_REQ_CMP;
1057 		xpt_done(ccb);
1058 		return;
1059 	}
1060 	case XPT_SET_TRAN_SETTINGS:
1061 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1062 		xpt_done(ccb);
1063 		return;
1064 	case XPT_RESET_BUS:
1065 		if (!(sc->flags & AAC_FLAGS_CAM_NORESET) &&
1066 			camsc->inf->BusType != CONTAINER_BUS) {
1067 			ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
1068 		} else {
1069 			ccb->ccb_h.status = CAM_REQ_CMP;
1070 		}
1071 		xpt_done(ccb);
1072 		return;
1073 	case XPT_RESET_DEV:
1074 		ccb->ccb_h.status = CAM_REQ_CMP;
1075 		xpt_done(ccb);
1076 		return;
1077 	case XPT_ABORT:
1078 		ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
1079 		xpt_done(ccb);
1080 		return;
1081 	case XPT_TERM_IO:
1082 		ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
1083 		xpt_done(ccb);
1084 		return;
1085 	default:
1086 		device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
1087 		    ccb->ccb_h.func_code);
1088 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1089 		xpt_done(ccb);
1090 		return;
1091 	}
1092 
1093 	/* Async ops that require communcation with the controller */
1094 	if (camsc->inf->BusType == CONTAINER_BUS) {
1095 		u_int8_t *cmdp;
1096 
1097 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1098 			cmdp = ccb->csio.cdb_io.cdb_ptr;
1099 		else
1100 			cmdp = &ccb->csio.cdb_io.cdb_bytes[0];
1101 
1102 		if (*cmdp==READ_6 || *cmdp==WRITE_6 || *cmdp==READ_10 ||
1103 			*cmdp==WRITE_10 || *cmdp==READ_12 || *cmdp==WRITE_12 ||
1104 			*cmdp==READ_16 || *cmdp==WRITE_16)
1105 			aac_container_rw_command(sim, ccb, cmdp);
1106 		else
1107 			aac_container_special_command(sim, ccb, cmdp);
1108 	} else {
1109 		aac_passthrough_command(sim, ccb);
1110 	}
1111 }
1112 
1113 static void
1114 aac_cam_poll(struct cam_sim *sim)
1115 {
1116 	/*
1117 	 * Pinging the interrupt routine isn't very safe, nor is it
1118 	 * really necessary.  Do nothing.
1119 	 */
1120 }
1121 
1122 static void
1123 aac_container_complete(struct aac_command *cm)
1124 {
1125 	struct	aac_softc *sc;
1126 	union	ccb *ccb;
1127 	u_int32_t status;
1128 
1129 	sc = cm->cm_sc;
1130 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1131 	ccb = cm->cm_ccb;
1132 	status = ((u_int32_t *)cm->cm_fib->data)[0];
1133 
1134 	if (cm->cm_flags & AAC_CMD_RESET) {
1135 		ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1136 	} else if (status == ST_OK) {
1137 		ccb->ccb_h.status = CAM_REQ_CMP;
1138 	} else if (status == ST_NOT_READY) {
1139 		ccb->ccb_h.status = CAM_BUSY;
1140 	} else {
1141 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1142 	}
1143 
1144 	aacraid_release_command(cm);
1145 	xpt_done(ccb);
1146 }
1147 
1148 static void
1149 aac_cam_complete(struct aac_command *cm)
1150 {
1151 	union	ccb *ccb;
1152 	struct 	aac_srb_response *srbr;
1153 	struct	aac_softc *sc;
1154 
1155 	sc = cm->cm_sc;
1156 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1157 	ccb = cm->cm_ccb;
1158 	srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
1159 
1160 	if (cm->cm_flags & AAC_CMD_FASTRESP) {
1161 		/* fast response */
1162 		srbr->srb_status = CAM_REQ_CMP;
1163 		srbr->scsi_status = SCSI_STATUS_OK;
1164 		srbr->sense_len = 0;
1165 	}
1166 
1167 	if (cm->cm_flags & AAC_CMD_RESET) {
1168 		ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1169 	} else if (srbr->fib_status != 0) {
1170 		device_printf(sc->aac_dev, "Passthru FIB failed!\n");
1171 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1172 	} else {
1173 		/*
1174 		 * The SRB error codes just happen to match the CAM error
1175 		 * codes.  How convienient!
1176 		 */
1177 		ccb->ccb_h.status = srbr->srb_status;
1178 
1179 		/* Take care of SCSI_IO ops. */
1180 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1181 			u_int8_t command, device;
1182 
1183 			ccb->csio.scsi_status = srbr->scsi_status;
1184 
1185 			/* Take care of autosense */
1186 			if (srbr->sense_len) {
1187 				int sense_len, scsi_sense_len;
1188 
1189 				scsi_sense_len = sizeof(struct scsi_sense_data);
1190 				bzero(&ccb->csio.sense_data, scsi_sense_len);
1191 				sense_len = (srbr->sense_len >
1192 				    scsi_sense_len) ? scsi_sense_len :
1193 				    srbr->sense_len;
1194 				bcopy(&srbr->sense[0], &ccb->csio.sense_data,
1195 				    srbr->sense_len);
1196 				ccb->csio.sense_len = sense_len;
1197 				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1198 				// scsi_sense_print(&ccb->csio);
1199 			}
1200 
1201 			/* If this is an inquiry command, fake things out */
1202 			if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1203 				command = ccb->csio.cdb_io.cdb_ptr[0];
1204 			else
1205 				command = ccb->csio.cdb_io.cdb_bytes[0];
1206 
1207 			if (command == INQUIRY) {
1208 				if (ccb->ccb_h.status == CAM_REQ_CMP) {
1209 				  device = ccb->csio.data_ptr[0] & 0x1f;
1210 				  /*
1211 				   * We want DASD and PROC devices to only be
1212 				   * visible through the pass device.
1213 				   */
1214 				  if ((device == T_DIRECT &&
1215 				    !(sc->aac_feature_bits & AAC_SUPPL_SUPPORTED_JBOD)) ||
1216 				    (device == T_PROCESSOR) ||
1217 				    (sc->flags & AAC_FLAGS_CAM_PASSONLY))
1218 				    ccb->csio.data_ptr[0] =
1219 				  	((device & 0xe0) | T_NODEVICE);
1220 
1221 				} else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
1222 				  ccb->ccb_h.target_lun != 0) {
1223 				  /* fix for INQUIRYs on Lun>0 */
1224 				  ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1225 				}
1226 			}
1227 		}
1228 	}
1229 
1230 	aacraid_release_command(cm);
1231 	xpt_done(ccb);
1232 }
1233 
1234 static u_int32_t
1235 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
1236 {
1237 	struct aac_command *cm;
1238 	struct aac_fib *fib;
1239 	struct aac_softc *sc;
1240 	struct aac_cam *camsc;
1241 	struct aac_vmioctl *vmi;
1242 	struct aac_resetbus *rbc;
1243 	u_int32_t rval;
1244 
1245 	camsc = (struct aac_cam *)cam_sim_softc(sim);
1246 	sc = camsc->inf->aac_sc;
1247 
1248 	if (sc == NULL) {
1249 		printf("aac: Null sc?\n");
1250 		return (CAM_REQ_ABORTED);
1251 	}
1252 
1253 	if (aacraid_alloc_command(sc, &cm)) {
1254 		struct aac_event *event;
1255 
1256 		xpt_freeze_simq(sim, 1);
1257 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1258 		ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
1259 		event = malloc(sizeof(struct aac_event), M_AACRAIDCAM,
1260 			M_NOWAIT | M_ZERO);
1261 		if (event == NULL) {
1262 			device_printf(sc->aac_dev,
1263 				"Warning, out of memory for event\n");
1264 			return (CAM_REQ_ABORTED);
1265 		}
1266 		event->ev_callback = aac_cam_event;
1267 		event->ev_arg = ccb;
1268 		event->ev_type = AAC_EVENT_CMFREE;
1269 		aacraid_add_event(sc, event);
1270 		return (CAM_REQ_ABORTED);
1271 	}
1272 
1273 	fib = cm->cm_fib;
1274 	cm->cm_timestamp = time_uptime;
1275 	cm->cm_datalen = 0;
1276 
1277 	fib->Header.Size =
1278 		sizeof(struct aac_fib_header) + sizeof(struct aac_vmioctl);
1279 	fib->Header.XferState =
1280 		AAC_FIBSTATE_HOSTOWNED   |
1281 		AAC_FIBSTATE_INITIALISED |
1282 		AAC_FIBSTATE_EMPTY	 |
1283 		AAC_FIBSTATE_FROMHOST	 |
1284 		AAC_FIBSTATE_REXPECTED   |
1285 		AAC_FIBSTATE_NORM	 |
1286 		AAC_FIBSTATE_ASYNC	 |
1287 		AAC_FIBSTATE_FAST_RESPONSE;
1288 	fib->Header.Command = ContainerCommand;
1289 
1290 	vmi = (struct aac_vmioctl *)&fib->data[0];
1291 	bzero(vmi, sizeof(struct aac_vmioctl));
1292 
1293 	vmi->Command = VM_Ioctl;
1294 	vmi->ObjType = FT_DRIVE;
1295 	vmi->MethId = sc->scsi_method_id;
1296 	vmi->ObjId = 0;
1297 	vmi->IoctlCmd = ResetBus;
1298 
1299 	rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
1300 	rbc->BusNumber = camsc->inf->BusNumber - 1;
1301 
1302 	if (aacraid_wait_command(cm) != 0) {
1303 		device_printf(sc->aac_dev,"Error sending ResetBus command\n");
1304 		rval = CAM_REQ_ABORTED;
1305 	} else {
1306 		rval = CAM_REQ_CMP;
1307 	}
1308 	aacraid_release_command(cm);
1309 	return (rval);
1310 }
1311 
1312 static u_int32_t
1313 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
1314 {
1315 	return (CAM_UA_ABORT);
1316 }
1317 
1318 static u_int32_t
1319 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
1320 {
1321 	return (CAM_UA_TERMIO);
1322 }
1323 
1324 static int
1325 aac_load_map_command_sg(struct aac_softc *sc, struct aac_command *cm)
1326 {
1327 	int error;
1328 
1329 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1330 	error = bus_dmamap_load(sc->aac_buffer_dmat,
1331 				cm->cm_datamap, cm->cm_data, cm->cm_datalen,
1332 				aacraid_map_command_sg, cm, 0);
1333 	if (error == EINPROGRESS) {
1334 		fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "freezing queue\n");
1335 		sc->flags |= AAC_QUEUE_FRZN;
1336 		error = 0;
1337 	} else if (error != 0) {
1338 		panic("aac_load_map_command_sg: unexpected error %d from "
1339 	     		"busdma", error);
1340 	}
1341 	return(error);
1342 }
1343 
1344 /*
1345  * Start as much queued I/O as possible on the controller
1346  */
1347 void
1348 aacraid_startio(struct aac_softc *sc)
1349 {
1350 	struct aac_command *cm;
1351 
1352 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1353 
1354 	for (;;) {
1355 		if (sc->aac_state & AAC_STATE_RESET) {
1356 			fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "AAC_STATE_RESET");
1357 			break;
1358 		}
1359 		/*
1360 		 * This flag might be set if the card is out of resources.
1361 		 * Checking it here prevents an infinite loop of deferrals.
1362 		 */
1363 		if (sc->flags & AAC_QUEUE_FRZN) {
1364 			fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "AAC_QUEUE_FRZN");
1365 			break;
1366 		}
1367 
1368 		/*
1369 		 * Try to get a command that's been put off for lack of
1370 		 * resources
1371 		 */
1372 		if (sc->flags & AAC_FLAGS_SYNC_MODE) {
1373 			/* sync. transfer mode */
1374 			if (sc->aac_sync_cm)
1375 				break;
1376 			cm = aac_dequeue_ready(sc);
1377 			sc->aac_sync_cm = cm;
1378 		} else {
1379 			cm = aac_dequeue_ready(sc);
1380 		}
1381 
1382 		/* nothing to do? */
1383 		if (cm == NULL)
1384 			break;
1385 
1386 		/* don't map more than once */
1387 		if (cm->cm_flags & AAC_CMD_MAPPED)
1388 			panic("aac: command %p already mapped", cm);
1389 
1390 		/*
1391 		 * Set up the command to go to the controller.  If there are no
1392 		 * data buffers associated with the command then it can bypass
1393 		 * busdma.
1394 		 */
1395 		if (cm->cm_datalen)
1396 			aac_load_map_command_sg(sc, cm);
1397 		else
1398 			aacraid_map_command_sg(cm, NULL, 0, 0);
1399 	}
1400 }
1401