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