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