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