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