1169609Sscottl/*-
2169609Sscottl * Copyright 2007 Scott Long
3169609Sscottl * All rights reserved.
4169609Sscottl *
5169609Sscottl * Redistribution and use in source and binary forms, with or without
6169609Sscottl * modification, are permitted provided that the following conditions
7169609Sscottl * are met:
8169609Sscottl * 1. Redistributions of source code must retain the above copyright
9169609Sscottl *    notice, this list of conditions and the following disclaimer.
10169609Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11169609Sscottl *    notice, this list of conditions and the following disclaimer in the
12169609Sscottl *    documentation and/or other materials provided with the distribution.
13169609Sscottl *
14169609Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15169609Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16169609Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17169609Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169609Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169609Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169609Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21169609Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22169609Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23169609Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24169609Sscottl * SUCH DAMAGE.
25169609Sscottl */
26169609Sscottl
27169609Sscottl#include <sys/cdefs.h>
28169609Sscottl__FBSDID("$FreeBSD$");
29169609Sscottl
30169609Sscottl#include "opt_mfi.h"
31169609Sscottl
32169609Sscottl#include <sys/param.h>
33169609Sscottl#include <sys/systm.h>
34169609Sscottl#include <sys/kernel.h>
35169609Sscottl#include <sys/malloc.h>
36169609Sscottl#include <sys/module.h>
37169609Sscottl#include <sys/selinfo.h>
38169609Sscottl#include <sys/bus.h>
39169609Sscottl#include <sys/conf.h>
40169609Sscottl#include <sys/eventhandler.h>
41169609Sscottl#include <sys/rman.h>
42169609Sscottl#include <sys/bus_dma.h>
43169609Sscottl#include <sys/bio.h>
44169609Sscottl#include <sys/ioccom.h>
45169609Sscottl#include <sys/uio.h>
46169609Sscottl#include <sys/proc.h>
47169609Sscottl#include <sys/signalvar.h>
48229611Sjhb#include <sys/sysctl.h>
49169609Sscottl
50169609Sscottl#include <cam/cam.h>
51169609Sscottl#include <cam/cam_ccb.h>
52169609Sscottl#include <cam/cam_debug.h>
53252643Smarkj#include <cam/cam_periph.h>
54169609Sscottl#include <cam/cam_sim.h>
55252643Smarkj#include <cam/cam_xpt_periph.h>
56169609Sscottl#include <cam/cam_xpt_sim.h>
57169609Sscottl#include <cam/scsi/scsi_all.h>
58169609Sscottl#include <cam/scsi/scsi_message.h>
59169609Sscottl
60169609Sscottl#include <machine/md_var.h>
61169609Sscottl#include <machine/bus.h>
62169609Sscottl#include <machine/resource.h>
63169609Sscottl
64169609Sscottl#include <dev/mfi/mfireg.h>
65169609Sscottl#include <dev/mfi/mfi_ioctl.h>
66169609Sscottl#include <dev/mfi/mfivar.h>
67169609Sscottl
68252643Smarkjenum mfip_state {
69252643Smarkj	MFIP_STATE_NONE,
70252643Smarkj	MFIP_STATE_DETACH,
71252643Smarkj	MFIP_STATE_RESCAN
72252643Smarkj};
73252643Smarkj
74169609Sscottlstruct mfip_softc {
75169609Sscottl	device_t	dev;
76169609Sscottl	struct mfi_softc *mfi_sc;
77169609Sscottl	struct cam_devq *devq;
78169609Sscottl	struct cam_sim	*sim;
79169609Sscottl	struct cam_path	*path;
80252643Smarkj	enum mfip_state	state;
81169609Sscottl};
82169609Sscottl
83169609Sscottlstatic int	mfip_probe(device_t);
84169609Sscottlstatic int	mfip_attach(device_t);
85169609Sscottlstatic int	mfip_detach(device_t);
86169609Sscottlstatic void	mfip_cam_action(struct cam_sim *, union ccb *);
87169609Sscottlstatic void	mfip_cam_poll(struct cam_sim *);
88252643Smarkjstatic void	mfip_cam_rescan(struct mfi_softc *, uint32_t tid);
89169609Sscottlstatic struct mfi_command * mfip_start(void *);
90169609Sscottlstatic void	mfip_done(struct mfi_command *cm);
91169609Sscottl
92243824Sdelphijstatic int mfi_allow_disks = 0;
93243824SdelphijTUNABLE_INT("hw.mfi.allow_cam_disk_passthrough", &mfi_allow_disks);
94243824SdelphijSYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RD,
95243824Sdelphij    &mfi_allow_disks, 0, "event message locale");
96243824Sdelphij
97169609Sscottlstatic devclass_t	mfip_devclass;
98169609Sscottlstatic device_method_t	mfip_methods[] = {
99169609Sscottl	DEVMETHOD(device_probe,		mfip_probe),
100169609Sscottl	DEVMETHOD(device_attach,	mfip_attach),
101169609Sscottl	DEVMETHOD(device_detach,	mfip_detach),
102169609Sscottl	{0, 0}
103169609Sscottl};
104169609Sscottlstatic driver_t mfip_driver = {
105169609Sscottl	"mfip",
106169609Sscottl	mfip_methods,
107169609Sscottl	sizeof(struct mfip_softc)
108169609Sscottl};
109169609SscottlDRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
110169609SscottlMODULE_DEPEND(mfip, cam, 1, 1, 1);
111204590SkibMODULE_DEPEND(mfip, mfi, 1, 1, 1);
112169609Sscottl
113169609Sscottl#define ccb_mfip_ptr sim_priv.entries[0].ptr
114169609Sscottl
115169609Sscottlstatic int
116169609Sscottlmfip_probe(device_t dev)
117169609Sscottl{
118169609Sscottl
119169609Sscottl	device_set_desc(dev, "SCSI Passthrough Bus");
120169609Sscottl	return (0);
121169609Sscottl}
122169609Sscottl
123169609Sscottlstatic int
124169609Sscottlmfip_attach(device_t dev)
125169609Sscottl{
126169609Sscottl	struct mfip_softc *sc;
127169609Sscottl	struct mfi_softc *mfisc;
128169609Sscottl
129169609Sscottl	sc = device_get_softc(dev);
130169609Sscottl	if (sc == NULL)
131169609Sscottl		return (EINVAL);
132169609Sscottl
133169609Sscottl	mfisc = device_get_softc(device_get_parent(dev));
134169609Sscottl	sc->dev = dev;
135252643Smarkj	sc->state = MFIP_STATE_NONE;
136169609Sscottl	sc->mfi_sc = mfisc;
137169609Sscottl	mfisc->mfi_cam_start = mfip_start;
138169609Sscottl
139169609Sscottl	if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
140169609Sscottl		return (ENOMEM);
141169609Sscottl
142169609Sscottl	sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
143169609Sscottl				device_get_unit(dev), &mfisc->mfi_io_lock, 1,
144169609Sscottl				MFI_SCSI_MAX_CMDS, sc->devq);
145169609Sscottl	if (sc->sim == NULL) {
146169609Sscottl		cam_simq_free(sc->devq);
147250496Ssmh		sc->devq = NULL;
148169609Sscottl		device_printf(dev, "CAM SIM attach failed\n");
149169609Sscottl		return (EINVAL);
150169609Sscottl	}
151169609Sscottl
152252643Smarkj	mfisc->mfi_cam_rescan_cb = mfip_cam_rescan;
153252643Smarkj
154169609Sscottl	mtx_lock(&mfisc->mfi_io_lock);
155170872Sscottl	if (xpt_bus_register(sc->sim, dev, 0) != 0) {
156169609Sscottl		device_printf(dev, "XPT bus registration failed\n");
157169609Sscottl		cam_sim_free(sc->sim, FALSE);
158250496Ssmh		sc->sim = NULL;
159169609Sscottl		cam_simq_free(sc->devq);
160250496Ssmh		sc->devq = NULL;
161169609Sscottl		mtx_unlock(&mfisc->mfi_io_lock);
162169609Sscottl		return (EINVAL);
163169609Sscottl	}
164169609Sscottl	mtx_unlock(&mfisc->mfi_io_lock);
165169609Sscottl
166169609Sscottl	return (0);
167169609Sscottl}
168169609Sscottl
169169609Sscottlstatic int
170169609Sscottlmfip_detach(device_t dev)
171169609Sscottl{
172169609Sscottl	struct mfip_softc *sc;
173169609Sscottl
174169609Sscottl	sc = device_get_softc(dev);
175169609Sscottl	if (sc == NULL)
176169609Sscottl		return (EINVAL);
177169609Sscottl
178252643Smarkj	mtx_lock(&sc->mfi_sc->mfi_io_lock);
179252643Smarkj	if (sc->state == MFIP_STATE_RESCAN) {
180252643Smarkj		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
181252643Smarkj		return (EBUSY);
182252643Smarkj	}
183252643Smarkj	sc->state = MFIP_STATE_DETACH;
184252643Smarkj	mtx_unlock(&sc->mfi_sc->mfi_io_lock);
185252643Smarkj
186252643Smarkj	sc->mfi_sc->mfi_cam_rescan_cb = NULL;
187252643Smarkj
188169609Sscottl	if (sc->sim != NULL) {
189169609Sscottl		mtx_lock(&sc->mfi_sc->mfi_io_lock);
190169609Sscottl		xpt_bus_deregister(cam_sim_path(sc->sim));
191169609Sscottl		cam_sim_free(sc->sim, FALSE);
192250496Ssmh		sc->sim = NULL;
193169609Sscottl		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
194169609Sscottl	}
195169609Sscottl
196250496Ssmh	if (sc->devq != NULL) {
197169609Sscottl		cam_simq_free(sc->devq);
198250496Ssmh		sc->devq = NULL;
199250496Ssmh	}
200169609Sscottl
201169609Sscottl	return (0);
202169609Sscottl}
203169609Sscottl
204169609Sscottlstatic void
205169609Sscottlmfip_cam_action(struct cam_sim *sim, union ccb *ccb)
206169609Sscottl{
207169609Sscottl	struct mfip_softc *sc = cam_sim_softc(sim);
208169609Sscottl	struct mfi_softc *mfisc = sc->mfi_sc;
209169609Sscottl
210169609Sscottl	mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
211169609Sscottl
212169609Sscottl	switch (ccb->ccb_h.func_code) {
213169609Sscottl	case XPT_PATH_INQ:
214169609Sscottl	{
215169609Sscottl		struct ccb_pathinq *cpi = &ccb->cpi;
216169609Sscottl
217169609Sscottl		cpi->version_num = 1;
218169609Sscottl		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
219169609Sscottl		cpi->target_sprt = 0;
220172588Sscottl		cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
221169609Sscottl		cpi->hba_eng_cnt = 0;
222169609Sscottl		cpi->max_target = MFI_SCSI_MAX_TARGETS;
223169609Sscottl		cpi->max_lun = MFI_SCSI_MAX_LUNS;
224169609Sscottl		cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
225169609Sscottl		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
226169609Sscottl		strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
227169609Sscottl		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
228169609Sscottl		cpi->unit_number = cam_sim_unit(sim);
229169609Sscottl		cpi->bus_id = cam_sim_bus(sim);
230169609Sscottl		cpi->base_transfer_speed = 150000;
231172588Sscottl		cpi->transport = XPORT_SAS;
232172588Sscottl		cpi->transport_version = 0;
233169609Sscottl		cpi->protocol = PROTO_SCSI;
234169609Sscottl		cpi->protocol_version = SCSI_REV_2;
235169609Sscottl		cpi->ccb_h.status = CAM_REQ_CMP;
236169609Sscottl		break;
237169609Sscottl	}
238169609Sscottl	case XPT_RESET_BUS:
239169609Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
240169609Sscottl		break;
241169609Sscottl	case XPT_RESET_DEV:
242169609Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
243169609Sscottl		break;
244169609Sscottl	case XPT_GET_TRAN_SETTINGS:
245169609Sscottl	{
246172588Sscottl		struct ccb_trans_settings_sas *sas =
247172588Sscottl		    &ccb->cts.xport_specific.sas;
248169609Sscottl
249169609Sscottl		ccb->cts.protocol = PROTO_SCSI;
250172588Sscottl		ccb->cts.protocol_version = SCSI_REV_2;
251172588Sscottl		ccb->cts.transport = XPORT_SAS;
252172588Sscottl		ccb->cts.transport_version = 0;
253172588Sscottl
254172588Sscottl		sas->valid &= ~CTS_SAS_VALID_SPEED;
255172588Sscottl		sas->bitrate = 150000;
256172588Sscottl
257169609Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
258169609Sscottl		break;
259169609Sscottl	}
260169609Sscottl	case XPT_SET_TRAN_SETTINGS:
261169609Sscottl		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
262169609Sscottl		break;
263169609Sscottl	case XPT_SCSI_IO:
264169609Sscottl	{
265169609Sscottl		struct ccb_hdr		*ccbh = &ccb->ccb_h;
266169609Sscottl		struct ccb_scsiio	*csio = &ccb->csio;
267169609Sscottl
268169609Sscottl		ccbh->status = CAM_REQ_INPROG;
269169609Sscottl		if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
270169609Sscottl			ccbh->status = CAM_REQ_INVALID;
271169609Sscottl			break;
272169609Sscottl		}
273169609Sscottl		ccbh->ccb_mfip_ptr = sc;
274169609Sscottl		TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
275169609Sscottl		mfi_startio(mfisc);
276169609Sscottl		return;
277169609Sscottl	}
278169609Sscottl	default:
279169609Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
280169609Sscottl		break;
281169609Sscottl	}
282169609Sscottl
283169609Sscottl	xpt_done(ccb);
284169609Sscottl	return;
285169609Sscottl}
286169609Sscottl
287252643Smarkjstatic void
288252643Smarkjmfip_cam_rescan(struct mfi_softc *sc, uint32_t tid)
289252643Smarkj{
290252643Smarkj	union ccb *ccb;
291252643Smarkj	struct mfip_softc *camsc;
292252643Smarkj	struct cam_sim *sim;
293252643Smarkj	device_t mfip_dev;
294252643Smarkj
295252643Smarkj	mtx_lock(&Giant);
296252643Smarkj	mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1);
297252643Smarkj	mtx_unlock(&Giant);
298252643Smarkj	if (mfip_dev == NULL) {
299252643Smarkj		device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n");
300252643Smarkj		return;
301252643Smarkj	}
302252643Smarkj
303252643Smarkj	mtx_lock(&sc->mfi_io_lock);
304252643Smarkj	camsc = device_get_softc(mfip_dev);
305252643Smarkj	if (camsc->state == MFIP_STATE_DETACH) {
306252643Smarkj		mtx_unlock(&sc->mfi_io_lock);
307252643Smarkj		return;
308252643Smarkj	}
309252643Smarkj	camsc->state = MFIP_STATE_RESCAN;
310252643Smarkj
311252643Smarkj	ccb = xpt_alloc_ccb_nowait();
312252643Smarkj	if (ccb == NULL) {
313255740Smarkj		mtx_unlock(&sc->mfi_io_lock);
314252643Smarkj		device_printf(sc->mfi_dev,
315252643Smarkj		    "Cannot allocate ccb for bus rescan.\n");
316252643Smarkj		return;
317252643Smarkj	}
318252643Smarkj
319252643Smarkj	sim = camsc->sim;
320253037Smav	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim),
321252643Smarkj	    tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
322252643Smarkj		xpt_free_ccb(ccb);
323252643Smarkj		mtx_unlock(&sc->mfi_io_lock);
324252643Smarkj		device_printf(sc->mfi_dev,
325252643Smarkj		    "Cannot create path for bus rescan.\n");
326252643Smarkj		return;
327252643Smarkj	}
328252643Smarkj	xpt_rescan(ccb);
329252643Smarkj
330252643Smarkj	camsc->state = MFIP_STATE_NONE;
331252643Smarkj	mtx_unlock(&sc->mfi_io_lock);
332252643Smarkj}
333252643Smarkj
334169609Sscottlstatic struct mfi_command *
335169609Sscottlmfip_start(void *data)
336169609Sscottl{
337169609Sscottl	union ccb *ccb = data;
338169609Sscottl	struct ccb_hdr *ccbh = &ccb->ccb_h;
339169609Sscottl	struct ccb_scsiio *csio = &ccb->csio;
340169609Sscottl	struct mfip_softc *sc;
341169609Sscottl	struct mfi_pass_frame *pt;
342169609Sscottl	struct mfi_command *cm;
343234429Sambrisko	uint32_t context = 0;
344169609Sscottl
345169609Sscottl	sc = ccbh->ccb_mfip_ptr;
346169609Sscottl
347169609Sscottl	if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
348169609Sscottl		return (NULL);
349169609Sscottl
350234429Sambrisko	/* Zero out the MFI frame */
351234429Sambrisko	context = cm->cm_frame->header.context;
352234429Sambrisko	bzero(cm->cm_frame, sizeof(union mfi_frame));
353234429Sambrisko	cm->cm_frame->header.context = context;
354234429Sambrisko
355169609Sscottl	pt = &cm->cm_frame->pass;
356169609Sscottl	pt->header.cmd = MFI_CMD_PD_SCSI_IO;
357169609Sscottl	pt->header.cmd_status = 0;
358169609Sscottl	pt->header.scsi_status = 0;
359169609Sscottl	pt->header.target_id = ccbh->target_id;
360169609Sscottl	pt->header.lun_id = ccbh->target_lun;
361169609Sscottl	pt->header.flags = 0;
362169609Sscottl	pt->header.timeout = 0;
363169609Sscottl	pt->header.data_len = csio->dxfer_len;
364169609Sscottl	pt->header.sense_len = MFI_SENSE_LEN;
365169609Sscottl	pt->header.cdb_len = csio->cdb_len;
366235525Ssbruno	pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
367235525Ssbruno	pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
368169609Sscottl	if (ccbh->flags & CAM_CDB_POINTER)
369169609Sscottl		bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
370169609Sscottl	else
371169609Sscottl		bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
372169609Sscottl	cm->cm_complete = mfip_done;
373169609Sscottl	cm->cm_private = ccb;
374169609Sscottl	cm->cm_sg = &pt->sgl;
375169609Sscottl	cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
376251874Sscottl	cm->cm_data = ccb;
377169609Sscottl	cm->cm_len = csio->dxfer_len;
378169609Sscottl	switch (ccbh->flags & CAM_DIR_MASK) {
379169609Sscottl	case CAM_DIR_IN:
380251874Sscottl		cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB;
381169609Sscottl		break;
382169609Sscottl	case CAM_DIR_OUT:
383251874Sscottl		cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB;
384169609Sscottl		break;
385169609Sscottl	case CAM_DIR_NONE:
386169609Sscottl	default:
387169609Sscottl		cm->cm_data = NULL;
388169609Sscottl		cm->cm_len = 0;
389169609Sscottl		cm->cm_flags = 0;
390169609Sscottl		break;
391169609Sscottl	}
392169609Sscottl
393169609Sscottl	TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
394169609Sscottl	return (cm);
395169609Sscottl}
396169609Sscottl
397169609Sscottlstatic void
398169609Sscottlmfip_done(struct mfi_command *cm)
399169609Sscottl{
400169609Sscottl	union ccb *ccb = cm->cm_private;
401169609Sscottl	struct ccb_hdr *ccbh = &ccb->ccb_h;
402169609Sscottl	struct ccb_scsiio *csio = &ccb->csio;
403169609Sscottl	struct mfip_softc *sc;
404169609Sscottl	struct mfi_pass_frame *pt;
405169609Sscottl
406169609Sscottl	sc = ccbh->ccb_mfip_ptr;
407169609Sscottl	pt = &cm->cm_frame->pass;
408169609Sscottl
409169609Sscottl	switch (pt->header.cmd_status) {
410169609Sscottl	case MFI_STAT_OK:
411169609Sscottl	{
412169609Sscottl		uint8_t command, device;
413169609Sscottl
414169609Sscottl		ccbh->status = CAM_REQ_CMP;
415169609Sscottl		csio->scsi_status = pt->header.scsi_status;
416169609Sscottl		if (ccbh->flags & CAM_CDB_POINTER)
417216235Sjhb			command = csio->cdb_io.cdb_ptr[0];
418169609Sscottl		else
419216235Sjhb			command = csio->cdb_io.cdb_bytes[0];
420169609Sscottl		if (command == INQUIRY) {
421216235Sjhb			device = csio->data_ptr[0] & 0x1f;
422243824Sdelphij			if ((!mfi_allow_disks && device == T_DIRECT) ||
423243824Sdelphij			    (device == T_PROCESSOR))
424169609Sscottl				csio->data_ptr[0] =
425216235Sjhb				     (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
426169609Sscottl		}
427169609Sscottl		break;
428169609Sscottl	}
429169609Sscottl	case MFI_STAT_SCSI_DONE_WITH_ERROR:
430169609Sscottl	{
431169609Sscottl		int sense_len;
432169609Sscottl
433172588Sscottl		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
434169609Sscottl		csio->scsi_status = pt->header.scsi_status;
435226910Smarius		if (pt->header.sense_len < csio->sense_len)
436226910Smarius			csio->sense_resid = csio->sense_len -
437226910Smarius			    pt->header.sense_len;
438226910Smarius		else
439226910Smarius			csio->sense_resid = 0;
440226910Smarius		sense_len = min(pt->header.sense_len,
441226910Smarius		    sizeof(struct scsi_sense_data));
442169609Sscottl		bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
443169609Sscottl		bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
444169609Sscottl		break;
445169609Sscottl	}
446169609Sscottl	case MFI_STAT_DEVICE_NOT_FOUND:
447172588Sscottl		ccbh->status = CAM_SEL_TIMEOUT;
448169609Sscottl		break;
449169609Sscottl	case MFI_STAT_SCSI_IO_FAILED:
450169609Sscottl		ccbh->status = CAM_REQ_CMP_ERR;
451169609Sscottl		csio->scsi_status = pt->header.scsi_status;
452169609Sscottl		break;
453169609Sscottl	default:
454169609Sscottl		ccbh->status = CAM_REQ_CMP_ERR;
455169609Sscottl		csio->scsi_status = pt->header.scsi_status;
456169609Sscottl		break;
457169609Sscottl	}
458169609Sscottl
459169609Sscottl	mfi_release_command(cm);
460169609Sscottl	xpt_done(ccb);
461169609Sscottl}
462169609Sscottl
463169609Sscottlstatic void
464169609Sscottlmfip_cam_poll(struct cam_sim *sim)
465169609Sscottl{
466243824Sdelphij	struct mfip_softc *sc = cam_sim_softc(sim);
467243824Sdelphij	struct mfi_softc *mfisc = sc->mfi_sc;
468243824Sdelphij
469243824Sdelphij	mfisc->mfi_intr_ptr(mfisc);
470169609Sscottl}
471169609Sscottl
472