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