165245Smsmith/*-
265245Smsmith * Copyright (c) 2000 Michael Smith
365245Smsmith * Copyright (c) 2000 BSDi
465245Smsmith * All rights reserved.
565245Smsmith *
665245Smsmith * Redistribution and use in source and binary forms, with or without
765245Smsmith * modification, are permitted provided that the following conditions
865245Smsmith * are met:
965245Smsmith * 1. Redistributions of source code must retain the above copyright
10174186Sscottl *	notice, this list of conditions and the following disclaimer.
1165245Smsmith * 2. Redistributions in binary form must reproduce the above copyright
12174186Sscottl *	notice, this list of conditions and the following disclaimer in the
13174186Sscottl *	documentation and/or other materials provided with the distribution.
1465245Smsmith *
1565245Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1665245Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1765245Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1865245Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1965245Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2065245Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2165245Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2265245Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2365245Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2465245Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2565245Smsmith * SUCH DAMAGE.
26119418Sobrien */
27139749Simp/*-
28106225Semoore * Copyright (c) 2002 Eric Moore
29106225Semoore * Copyright (c) 2002 LSI Logic Corporation
30106225Semoore * All rights reserved.
31106225Semoore *
32106225Semoore * Redistribution and use in source and binary forms, with or without
33106225Semoore * modification, are permitted provided that the following conditions
34106225Semoore * are met:
35106225Semoore * 1. Redistributions of source code must retain the above copyright
36174186Sscottl *	notice, this list of conditions and the following disclaimer.
37106225Semoore * 2. Redistributions in binary form must reproduce the above copyright
38174186Sscottl *	notice, this list of conditions and the following disclaimer in the
39174186Sscottl *	documentation and/or other materials provided with the distribution.
40105419Semoore * 3. The party using or redistributing the source code and binary forms
41174186Sscottl *	agrees to the disclaimer below and the terms and conditions set forth
42174186Sscottl *	herein.
43105419Semoore *
44106225Semoore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45106225Semoore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46106225Semoore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47106225Semoore * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48106225Semoore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49106225Semoore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50106225Semoore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51106225Semoore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52106225Semoore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53106225Semoore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54106225Semoore * SUCH DAMAGE.
5565245Smsmith */
5665245Smsmith
57119418Sobrien#include <sys/cdefs.h>
58119418Sobrien__FBSDID("$FreeBSD$");
59119418Sobrien
6065245Smsmith#include <sys/param.h>
6165245Smsmith#include <sys/systm.h>
6265245Smsmith#include <sys/malloc.h>
6365245Smsmith#include <sys/kernel.h>
64184573Sscottl#include <sys/module.h>
6565245Smsmith
66148850Sscottl#include <sys/bio.h>
6765245Smsmith#include <sys/bus.h>
6865245Smsmith#include <sys/conf.h>
6965245Smsmith#include <sys/stat.h>
7065245Smsmith
7165245Smsmith#include <cam/cam.h>
7265245Smsmith#include <cam/cam_ccb.h>
7365245Smsmith#include <cam/cam_sim.h>
7465245Smsmith#include <cam/cam_xpt.h>
7565245Smsmith#include <cam/cam_xpt_sim.h>
7665245Smsmith#include <cam/cam_debug.h>
7765245Smsmith#include <cam/scsi/scsi_all.h>
7865245Smsmith#include <cam/scsi/scsi_message.h>
7965245Smsmith
8065245Smsmith#include <machine/resource.h>
8165245Smsmith#include <machine/bus.h>
8265245Smsmith
8365245Smsmith#include <dev/amr/amrreg.h>
8465245Smsmith#include <dev/amr/amrvar.h>
8565245Smsmith
86184573Sscottlstatic int	amr_cam_probe(device_t dev);
87184573Sscottlstatic int	amr_cam_attach(device_t dev);
88184573Sscottlstatic int	amr_cam_detach(device_t dev);
89174186Sscottlstatic void	amr_cam_action(struct cam_sim *sim, union ccb *ccb);
90174186Sscottlstatic void	amr_cam_poll(struct cam_sim *sim);
91174186Sscottlstatic void	amr_cam_complete(struct amr_command *ac);
92184573Sscottlstatic int	amr_cam_command(struct amr_softc *sc, struct amr_command **acp);
9365245Smsmith
94184573Sscottlstatic devclass_t	amr_pass_devclass;
95184573Sscottl
96184573Sscottlstatic device_method_t	amr_pass_methods[] = {
97184573Sscottl	DEVMETHOD(device_probe,		amr_cam_probe),
98184573Sscottl	DEVMETHOD(device_attach,	amr_cam_attach),
99184573Sscottl	DEVMETHOD(device_detach,	amr_cam_detach),
100184573Sscottl	{ 0, 0 }
101184573Sscottl};
102184573Sscottl
103184573Sscottlstatic driver_t	amr_pass_driver = {
104184573Sscottl	"amrp",
105184573Sscottl	amr_pass_methods,
106184573Sscottl	0
107184573Sscottl};
108184573Sscottl
109184573SscottlDRIVER_MODULE(amrp, amr, amr_pass_driver, amr_pass_devclass, 0, 0);
110184573SscottlMODULE_DEPEND(amrp, cam, 1, 1, 1);
111184573Sscottl
112227293Sedstatic MALLOC_DEFINE(M_AMRCAM, "amrcam", "AMR CAM memory");
11365245Smsmith
114174186Sscottl/***********************************************************************
11565245Smsmith * Enqueue/dequeue functions
11665245Smsmith */
11765245Smsmithstatic __inline void
11865245Smsmithamr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
11965245Smsmith{
12065245Smsmith
121174186Sscottl	TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
12265245Smsmith}
12365245Smsmith
12465245Smsmithstatic __inline void
12565245Smsmithamr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
12665245Smsmith{
12765245Smsmith
128174186Sscottl	TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
12965245Smsmith}
13065245Smsmith
13165245Smsmithstatic __inline union ccb *
13265245Smsmithamr_dequeue_ccb(struct amr_softc *sc)
13365245Smsmith{
134174186Sscottl	union ccb	*ccb;
13565245Smsmith
136174186Sscottl	if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
137174186Sscottl		TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
138174186Sscottl	return(ccb);
13965245Smsmith}
14065245Smsmith
141184573Sscottlstatic int
142184573Sscottlamr_cam_probe(device_t dev)
143184573Sscottl{
144184573Sscottl	return (0);
145184573Sscottl}
146184573Sscottl
14765245Smsmith/********************************************************************************
14865245Smsmith * Attach our 'real' SCSI channels to CAM
14965245Smsmith */
150184573Sscottlstatic int
151184573Sscottlamr_cam_attach(device_t dev)
15265245Smsmith{
153184573Sscottl	struct amr_softc *sc;
154174186Sscottl	struct cam_devq	*devq;
155184573Sscottl	int chn, error;
15665245Smsmith
157184573Sscottl	sc = device_get_softc(dev);
158184573Sscottl
159174186Sscottl	/* initialise the ccb queue */
160174186Sscottl	TAILQ_INIT(&sc->amr_cam_ccbq);
16165245Smsmith
162174186Sscottl	/*
163174186Sscottl	 * Allocate a devq for all our channels combined.  This should
164174186Sscottl	 * allow for the maximum number of SCSI commands we will accept
165174186Sscottl	 * at one time. Save the pointer in the softc so we can find it later
166174186Sscottl	 * during detach.
167174186Sscottl	 */
168174186Sscottl	if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
169184573Sscottl		return(ENOMEM);
170174186Sscottl	sc->amr_cam_devq = devq;
17165245Smsmith
172174186Sscottl	/*
173174186Sscottl	 * Iterate over our channels, registering them with CAM
174174186Sscottl	 */
175174186Sscottl	for (chn = 0; chn < sc->amr_maxchan; chn++) {
176105419Semoore
177174186Sscottl		/* allocate a sim */
178174186Sscottl		if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
179174186Sscottl		    amr_cam_poll, "amr", sc, device_get_unit(sc->amr_dev),
180174186Sscottl		    &sc->amr_list_lock, 1, AMR_MAX_SCSI_CMDS, devq)) == NULL) {
181174186Sscottl			cam_simq_free(devq);
182174186Sscottl			device_printf(sc->amr_dev, "CAM SIM attach failed\n");
183174186Sscottl			return(ENOMEM);
184174186Sscottl		}
185105419Semoore
186174186Sscottl		/* register the bus ID so we can get it later */
187174186Sscottl		mtx_lock(&sc->amr_list_lock);
188174186Sscottl		error = xpt_bus_register(sc->amr_cam_sim[chn], sc->amr_dev,chn);
189174186Sscottl		mtx_unlock(&sc->amr_list_lock);
190174186Sscottl		if (error) {
191174186Sscottl			device_printf(sc->amr_dev,
192174186Sscottl			    "CAM XPT bus registration failed\n");
193174186Sscottl			return(ENXIO);
194174186Sscottl		}
19565245Smsmith	}
196174186Sscottl	/*
197174186Sscottl	 * XXX we should scan the config and work out which devices are
198174186Sscottl	 * actually protected.
199174186Sscottl	 */
200184573Sscottl	sc->amr_cam_command = amr_cam_command;
201174186Sscottl	return(0);
20265245Smsmith}
20365245Smsmith
20465245Smsmith/********************************************************************************
20565245Smsmith * Disconnect ourselves from CAM
20665245Smsmith */
207184573Sscottlstatic int
208184573Sscottlamr_cam_detach(device_t dev)
20965245Smsmith{
210184573Sscottl	struct amr_softc *sc;
211174186Sscottl	int		chn;
21265245Smsmith
213184573Sscottl	sc = device_get_softc(dev);
214174186Sscottl	mtx_lock(&sc->amr_list_lock);
215174186Sscottl	for (chn = 0; chn < sc->amr_maxchan; chn++) {
216174186Sscottl		/*
217174186Sscottl		 * If a sim was allocated for this channel, free it
218174186Sscottl		 */
219174186Sscottl		if (sc->amr_cam_sim[chn] != NULL) {
220174186Sscottl			xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
221174186Sscottl			cam_sim_free(sc->amr_cam_sim[chn], FALSE);
222174186Sscottl		}
22365245Smsmith	}
224174186Sscottl	mtx_unlock(&sc->amr_list_lock);
225139952Sdwhite
226174186Sscottl	/* Now free the devq */
227174186Sscottl	if (sc->amr_cam_devq != NULL)
228174186Sscottl		cam_simq_free(sc->amr_cam_devq);
229184573Sscottl
230184573Sscottl	return (0);
23165245Smsmith}
23265245Smsmith
233174186Sscottl/***********************************************************************
234174186Sscottl ***********************************************************************
235174186Sscottl			CAM passthrough interface
236174186Sscottl ***********************************************************************
237174186Sscottl ***********************************************************************/
23865245Smsmith
239174186Sscottl/***********************************************************************
24065245Smsmith * Handle a request for action from CAM
24165245Smsmith */
24265245Smsmithstatic void
24365245Smsmithamr_cam_action(struct cam_sim *sim, union ccb *ccb)
24465245Smsmith{
245174186Sscottl	struct amr_softc	*sc = cam_sim_softc(sim);
24665245Smsmith
247174186Sscottl	switch(ccb->ccb_h.func_code) {
24865245Smsmith
249174186Sscottl	/*
250174186Sscottl	 * Perform SCSI I/O to a physical device.
251174186Sscottl	 */
252174186Sscottl	case XPT_SCSI_IO:
253174186Sscottl	{
254174186Sscottl		struct ccb_hdr		*ccbh = &ccb->ccb_h;
255174186Sscottl		struct ccb_scsiio	*csio = &ccb->csio;
25665245Smsmith
257174186Sscottl		/* Validate the CCB */
258174186Sscottl		ccbh->status = CAM_REQ_INPROG;
25965245Smsmith
260174186Sscottl		/* check the CDB length */
261174186Sscottl		if (csio->cdb_len > AMR_MAX_EXTCDB_LEN)
262174544Sscottl			ccbh->status = CAM_REQ_INVALID;
26365245Smsmith
264174186Sscottl		if ((csio->cdb_len > AMR_MAX_CDB_LEN) &&
265174186Sscottl		    (sc->support_ext_cdb == 0))
266174544Sscottl			ccbh->status = CAM_REQ_INVALID;
267174186Sscottl
268174186Sscottl		/* check that the CDB pointer is not to a physical address */
269174186Sscottl		if ((ccbh->flags & CAM_CDB_POINTER) &&
270174186Sscottl		    (ccbh->flags & CAM_CDB_PHYS))
271174544Sscottl			ccbh->status = CAM_REQ_INVALID;
272174186Sscottl		/*
273174186Sscottl		 * if there is data transfer, it must be to/from a virtual
274174186Sscottl		 * address
275174186Sscottl		 */
276174186Sscottl		if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
277246713Skib			if ((ccbh->flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
278174186Sscottl				/* we can't map it */
279174544Sscottl				ccbh->status = CAM_REQ_INVALID;
280174186Sscottl		}
281174186Sscottl
282174186Sscottl		/*
283174186Sscottl		 * If the command is to a LUN other than 0, fail it.
284174186Sscottl		 * This is probably incorrect, but during testing the
285174186Sscottl		 * firmware did not seem to respect the LUN field, and thus
286174186Sscottl		 * devices appear echoed.
287174186Sscottl		 */
288174186Sscottl		if (csio->ccb_h.target_lun != 0)
289174544Sscottl			ccbh->status = CAM_DEV_NOT_THERE;
290106225Semoore
291174186Sscottl		/* if we're happy with the request, queue it for attention */
292174186Sscottl		if (ccbh->status == CAM_REQ_INPROG) {
29365245Smsmith
294174186Sscottl			/* save the channel number in the ccb */
295174186Sscottl			csio->ccb_h.sim_priv.entries[0].field= cam_sim_bus(sim);
296174186Sscottl
297174186Sscottl			amr_enqueue_ccb(sc, ccb);
298174186Sscottl			amr_startio(sc);
299174186Sscottl			return;
300174186Sscottl		}
301174186Sscottl		break;
30265245Smsmith	}
30365245Smsmith
304174186Sscottl	case XPT_CALC_GEOMETRY:
305174186Sscottl	{
306174186Sscottl		cam_calc_geometry(&ccb->ccg, /*extended*/1);
307174186Sscottl		break;
308174186Sscottl	}
309174186Sscottl
31065245Smsmith	/*
311174186Sscottl	 * Return path stats.  Some of these should probably be amended.
31265245Smsmith	 */
313174186Sscottl	case XPT_PATH_INQ:
314174186Sscottl	{
315174186Sscottl		struct ccb_pathinq	  *cpi = & ccb->cpi;
31665245Smsmith
317174186Sscottl		debug(3, "XPT_PATH_INQ");
318174186Sscottl		cpi->version_num = 1;		   /* XXX??? */
319174186Sscottl		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
320174186Sscottl		cpi->target_sprt = 0;
321174186Sscottl		cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
322174186Sscottl		cpi->hba_eng_cnt = 0;
323174186Sscottl		cpi->max_target = AMR_MAX_TARGETS;
324174186Sscottl		cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
325174186Sscottl		cpi->initiator_id = 7;		  /* XXX variable? */
326174186Sscottl		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
327174186Sscottl		strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
328174186Sscottl		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
329174186Sscottl		cpi->unit_number = cam_sim_unit(sim);
330174186Sscottl		cpi->bus_id = cam_sim_bus(sim);
331174186Sscottl		cpi->base_transfer_speed = 132 * 1024;  /* XXX */
332174186Sscottl		cpi->transport = XPORT_SPI;
333174186Sscottl		cpi->transport_version = 2;
334174186Sscottl		cpi->protocol = PROTO_SCSI;
335174186Sscottl		cpi->protocol_version = SCSI_REV_2;
336174186Sscottl		cpi->ccb_h.status = CAM_REQ_CMP;
33765245Smsmith
338174186Sscottl		break;
33965245Smsmith	}
34065245Smsmith
341174186Sscottl	case XPT_RESET_BUS:
342174186Sscottl	{
343174186Sscottl		struct ccb_pathinq	*cpi = & ccb->cpi;
34465245Smsmith
345174186Sscottl		debug(1, "XPT_RESET_BUS");
346174186Sscottl		cpi->ccb_h.status = CAM_REQ_CMP;
347174186Sscottl		break;
348174186Sscottl	}
34965245Smsmith
350174186Sscottl	case XPT_RESET_DEV:
351174186Sscottl	{
352174186Sscottl		debug(1, "XPT_RESET_DEV");
353174186Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
354174186Sscottl		break;
355174186Sscottl	}
35665245Smsmith
357174186Sscottl	case XPT_GET_TRAN_SETTINGS:
358174186Sscottl	{
359174186Sscottl		struct ccb_trans_settings	*cts = &(ccb->cts);
36065245Smsmith
361174186Sscottl		debug(3, "XPT_GET_TRAN_SETTINGS");
362105419Semoore
363174186Sscottl		struct ccb_trans_settings_scsi *scsi;
364174186Sscottl		struct ccb_trans_settings_spi *spi;
365105419Semoore
366174186Sscottl		scsi = &cts->proto_specific.scsi;
367174186Sscottl		spi = &cts->xport_specific.spi;
368105419Semoore
369174186Sscottl		cts->protocol = PROTO_SCSI;
370174186Sscottl		cts->protocol_version = SCSI_REV_2;
371174186Sscottl		cts->transport = XPORT_SPI;
372174186Sscottl		cts->transport_version = 2;
373105419Semoore
374174186Sscottl		if (cts->type == CTS_TYPE_USER_SETTINGS) {
375174186Sscottl			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
376174186Sscottl			break;
377174186Sscottl		}
378105419Semoore
379174186Sscottl		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
380174186Sscottl		spi->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
381174186Sscottl		spi->sync_period = 6;   /* 40MHz how wide is this bus? */
382174186Sscottl		spi->sync_offset = 31;  /* How to extract this from board? */
383105419Semoore
384174186Sscottl		spi->valid = CTS_SPI_VALID_SYNC_RATE
385174186Sscottl			| CTS_SPI_VALID_SYNC_OFFSET
386174186Sscottl			| CTS_SPI_VALID_BUS_WIDTH
387174186Sscottl			| CTS_SPI_VALID_DISC;
388174186Sscottl		scsi->valid = CTS_SCSI_VALID_TQ;
389174186Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
390174186Sscottl		break;
391174186Sscottl	}
392163816Smjacob
393174186Sscottl	case XPT_SET_TRAN_SETTINGS:
394174186Sscottl		debug(3, "XPT_SET_TRAN_SETTINGS");
395163816Smjacob		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
396163816Smjacob		break;
397163816Smjacob
398163816Smjacob
399174186Sscottl	/*
400174186Sscottl	 * Reject anything else as unsupported.
401174186Sscottl	 */
402174186Sscottl	default:
403174186Sscottl		/* we can't do this */
404174186Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
405174186Sscottl		break;
406174186Sscottl	}
407105419Semoore
408174186Sscottl	mtx_assert(&sc->amr_list_lock, MA_OWNED);
409174186Sscottl	xpt_done(ccb);
41065245Smsmith}
41165245Smsmith
412174186Sscottl/***********************************************************************
413174186Sscottl * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI
414174186Sscottl * command.
41565245Smsmith */
416184573Sscottlstatic int
41765245Smsmithamr_cam_command(struct amr_softc *sc, struct amr_command **acp)
41865245Smsmith{
419174186Sscottl	struct amr_command		*ac;
420174186Sscottl	struct amr_passthrough		*ap;
421174186Sscottl	struct amr_ext_passthrough	*aep;
422174186Sscottl	struct ccb_scsiio		*csio;
423174186Sscottl	int				bus, target, error;
42465245Smsmith
425174186Sscottl	error = 0;
426174186Sscottl	ac = NULL;
427174186Sscottl	ap = NULL;
428174186Sscottl	aep = NULL;
42965245Smsmith
430174186Sscottl	/* check to see if there is a ccb for us to work with */
431174186Sscottl	if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
43265245Smsmith	goto out;
43365245Smsmith
434174186Sscottl	/* get bus/target, XXX validate against protected devices? */
435174186Sscottl	bus = csio->ccb_h.sim_priv.entries[0].field;
436174186Sscottl	target = csio->ccb_h.target_id;
43765245Smsmith
438174186Sscottl	/*
439174186Sscottl	 * Build a passthrough command.
440174186Sscottl	 */
44165245Smsmith
442174544Sscottl	/* construct command */
443174544Sscottl	if ((ac = amr_alloccmd(sc)) == NULL) {
444174544Sscottl		error = ENOMEM;
445174544Sscottl		goto out;
446174544Sscottl	}
447174544Sscottl
448174186Sscottl	/* construct passthrough */
449174186Sscottl	if (sc->support_ext_cdb ) {
450174544Sscottl		aep = &ac->ac_ccb->ccb_epthru;
451174186Sscottl		aep->ap_timeout = 2;
452174186Sscottl		aep->ap_ars = 1;
453174186Sscottl		aep->ap_request_sense_length = 14;
454174186Sscottl		aep->ap_islogical = 0;
455174186Sscottl		aep->ap_channel = bus;
456174186Sscottl		aep->ap_scsi_id = target;
457174186Sscottl		aep->ap_logical_drive_no = csio->ccb_h.target_lun;
458174186Sscottl		aep->ap_cdb_length = csio->cdb_len;
459174186Sscottl		aep->ap_data_transfer_length = csio->dxfer_len;
460174186Sscottl		if (csio->ccb_h.flags & CAM_CDB_POINTER) {
461174186Sscottl			bcopy(csio->cdb_io.cdb_ptr, aep->ap_cdb, csio->cdb_len);
462174186Sscottl		} else {
463174186Sscottl			bcopy(csio->cdb_io.cdb_bytes, aep->ap_cdb,
464174186Sscottl			    csio->cdb_len);
465174186Sscottl		}
466174186Sscottl		/*
467174186Sscottl		 * we leave the data s/g list and s/g count to the map routine
468174186Sscottl		 * later
469174186Sscottl		 */
470106225Semoore
471174186Sscottl		debug(2, " COMMAND %x/%d+%d to %d:%d:%d", aep->ap_cdb[0],
472174186Sscottl		    aep->ap_cdb_length, csio->dxfer_len, aep->ap_channel,
473174186Sscottl		    aep->ap_scsi_id, aep->ap_logical_drive_no);
474106225Semoore
475174186Sscottl	} else {
476174544Sscottl		ap = &ac->ac_ccb->ccb_pthru;
477174186Sscottl		ap->ap_timeout = 0;
478174186Sscottl		ap->ap_ars = 1;
479174186Sscottl		ap->ap_request_sense_length = 14;
480174186Sscottl		ap->ap_islogical = 0;
481174186Sscottl		ap->ap_channel = bus;
482174186Sscottl		ap->ap_scsi_id = target;
483174186Sscottl		ap->ap_logical_drive_no = csio->ccb_h.target_lun;
484174186Sscottl		ap->ap_cdb_length = csio->cdb_len;
485174186Sscottl		ap->ap_data_transfer_length = csio->dxfer_len;
486174186Sscottl		if (csio->ccb_h.flags & CAM_CDB_POINTER) {
487174186Sscottl			bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
488174186Sscottl		} else {
489174186Sscottl			bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb,
490174186Sscottl			    csio->cdb_len);
491174186Sscottl		}
492174186Sscottl		/*
493174186Sscottl		 * we leave the data s/g list and s/g count to the map routine
494174186Sscottl		 * later
495174186Sscottl		 */
496174186Sscottl
497174186Sscottl		debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0],
498174186Sscottl		    ap->ap_cdb_length, csio->dxfer_len, ap->ap_channel,
499174186Sscottl		    ap->ap_scsi_id, ap->ap_logical_drive_no);
500174186Sscottl	}
501174186Sscottl
502174544Sscottl	ac->ac_flags |= AMR_CMD_CCB;
503106225Semoore
504174544Sscottl	ac->ac_data = csio->data_ptr;
505174544Sscottl	ac->ac_length = csio->dxfer_len;
506174186Sscottl	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
507174544Sscottl		ac->ac_flags |= AMR_CMD_DATAIN;
508174186Sscottl	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
509174544Sscottl		ac->ac_flags |= AMR_CMD_DATAOUT;
51065245Smsmith
511174186Sscottl	ac->ac_private = csio;
512174193Sscottl	ac->ac_complete = amr_cam_complete;
513174186Sscottl	if ( sc->support_ext_cdb ) {
514174186Sscottl		ac->ac_mailbox.mb_command = AMR_CMD_EXTPASS;
515174186Sscottl	} else {
516174186Sscottl		ac->ac_mailbox.mb_command = AMR_CMD_PASS;
517174186Sscottl	}
51865245Smsmith
51965245Smsmithout:
520174186Sscottl	if (error != 0) {
521174186Sscottl		if (ac != NULL)
522174186Sscottl			amr_releasecmd(ac);
523174186Sscottl		if (csio != NULL)
524174186Sscottl			/* put it back and try again later */
525174186Sscottl			amr_requeue_ccb(sc, (union ccb *)csio);
526174186Sscottl	}
527174186Sscottl	*acp = ac;
528174186Sscottl	return(error);
52965245Smsmith}
53065245Smsmith
531174186Sscottl/***********************************************************************
53265245Smsmith * Check for interrupt status
53365245Smsmith */
53465245Smsmithstatic void
53565245Smsmithamr_cam_poll(struct cam_sim *sim)
53665245Smsmith{
537140340Sscottl
538174186Sscottl	amr_done(cam_sim_softc(sim));
53965245Smsmith}
54065245Smsmith
541174186Sscottl /**********************************************************************
54265245Smsmith * Handle completion of a command submitted via CAM.
54365245Smsmith */
54465245Smsmithstatic void
54565245Smsmithamr_cam_complete(struct amr_command *ac)
54665245Smsmith{
547174186Sscottl	struct amr_passthrough		*ap;
548174193Sscottl	struct amr_ext_passthrough	*aep;
549174186Sscottl	struct ccb_scsiio		*csio;
550174186Sscottl	struct scsi_inquiry_data	*inq;
551174193Sscottl	int				scsi_status, cdb0;
55265245Smsmith
553174544Sscottl	ap = &ac->ac_ccb->ccb_pthru;
554174544Sscottl	aep = &ac->ac_ccb->ccb_epthru;
555174186Sscottl	csio = (struct ccb_scsiio *)ac->ac_private;
556174186Sscottl	inq = (struct scsi_inquiry_data *)csio->data_ptr;
55765245Smsmith
558174544Sscottl	if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
559174544Sscottl		scsi_status = aep->ap_scsi_status;
560174544Sscottl	else
561174193Sscottl		scsi_status = ap->ap_scsi_status;
562174186Sscottl	debug(1, "status 0x%x  AP scsi_status 0x%x", ac->ac_status,
563174193Sscottl	    scsi_status);
564105419Semoore
565174544Sscottl	/* Make sure the status is sane */
566174544Sscottl	if ((ac->ac_status != AMR_STATUS_SUCCESS) && (scsi_status == 0)) {
567174186Sscottl		csio->ccb_h.status = CAM_REQ_CMP_ERR;
568174186Sscottl		goto out;
569174186Sscottl	}
570106225Semoore
571174186Sscottl	/*
572174186Sscottl	 * Hide disks from CAM so that they're not picked up and treated as
573174186Sscottl	 * 'normal' disks.
574174186Sscottl	 *
575174186Sscottl	 * If the configuration provides a mechanism to mark a disk a "not
576174186Sscottl	 * managed", we could add handling for that to allow disks to be
577174186Sscottl	 * selectively visible.
578174186Sscottl	 */
579106225Semoore
580174186Sscottl	/* handle passthrough SCSI status */
581174193Sscottl	switch(scsi_status) {
582174186Sscottl	case 0:	/* completed OK */
583174544Sscottl		if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
584174544Sscottl			cdb0 = aep->ap_cdb[0];
585174544Sscottl		else
586174193Sscottl			cdb0 = ap->ap_cdb[0];
587174193Sscottl		if ((cdb0 == INQUIRY) && (SID_TYPE(inq) == T_DIRECT))
588174186Sscottl			inq->device = (inq->device & 0xe0) | T_NODEVICE;
589174186Sscottl		csio->ccb_h.status = CAM_REQ_CMP;
590174186Sscottl		break;
591106225Semoore
592174186Sscottl	case 0x02:
593174186Sscottl		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
594174186Sscottl		csio->scsi_status = SCSI_STATUS_CHECK_COND;
595174544Sscottl		if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
596174544Sscottl			bcopy(aep->ap_request_sense_area, &csio->sense_data,
597174193Sscottl			    AMR_MAX_REQ_SENSE_LEN);
598174193Sscottl		else
599174544Sscottl			bcopy(ap->ap_request_sense_area, &csio->sense_data,
600174193Sscottl			    AMR_MAX_REQ_SENSE_LEN);
601174186Sscottl		csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
602174186Sscottl		csio->ccb_h.status |= CAM_AUTOSNS_VALID;
603174186Sscottl		break;
604106225Semoore
605174186Sscottl	case 0x08:
606174186Sscottl		csio->ccb_h.status = CAM_SCSI_BUSY;
607174186Sscottl		break;
608106225Semoore
609174186Sscottl	case 0xf0:
610174186Sscottl	case 0xf4:
611174193Sscottl	default:
612174544Sscottl		/*
613174544Sscottl		 * Non-zero LUNs are already filtered, so there's no need
614174544Sscottl		 * to return CAM_DEV_NOT_THERE.
615174544Sscottl		 */
616174544Sscottl		csio->ccb_h.status = CAM_SEL_TIMEOUT;
617174186Sscottl		break;
618174186Sscottl	}
619174186Sscottl
620174030Sscottlout:
621174186Sscottl	if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
622174186Sscottl		debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr,
623174186Sscottl		    " ");
624174193Sscottl
625174544Sscottl	mtx_lock(&ac->ac_sc->amr_list_lock);
626174186Sscottl	xpt_done((union ccb *)csio);
627174186Sscottl	amr_releasecmd(ac);
628174193Sscottl	mtx_unlock(&ac->ac_sc->amr_list_lock);
629106225Semoore}
630