mfi_cam.c revision 235525
193383Smdodd/*-
293383Smdodd * Copyright 2007 Scott Long
321826Sjoerg * All rights reserved.
421826Sjoerg *
521826Sjoerg * Redistribution and use in source and binary forms, with or without
621826Sjoerg * modification, are permitted provided that the following conditions
721826Sjoerg * are met:
821826Sjoerg * 1. Redistributions of source code must retain the above copyright
921826Sjoerg *    notice, this list of conditions and the following disclaimer.
1021826Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1121826Sjoerg *    notice, this list of conditions and the following disclaimer in the
1221826Sjoerg *    documentation and/or other materials provided with the distribution.
1393383Smdodd *
1421826Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1521826Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1621826Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1721826Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1821826Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1921826Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2021826Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2121826Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2221826Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2321826Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2421826Sjoerg * SUCH DAMAGE.
2521826Sjoerg */
2693383Smdodd
2721826Sjoerg#include <sys/cdefs.h>
2821826Sjoerg__FBSDID("$FreeBSD: stable/9/sys/dev/mfi/mfi_cam.c 235525 2012-05-16 22:19:22Z sbruno $");
2921826Sjoerg
30119418Sobrien#include "opt_mfi.h"
31119418Sobrien
32119418Sobrien#include <sys/param.h>
3321826Sjoerg#include <sys/systm.h>
3421826Sjoerg#include <sys/kernel.h>
3521826Sjoerg#include <sys/malloc.h>
3693383Smdodd#include <sys/module.h>
3721826Sjoerg#include <sys/selinfo.h>
3821826Sjoerg#include <sys/bus.h>
3921826Sjoerg#include <sys/conf.h>
4021826Sjoerg#include <sys/eventhandler.h>
4121826Sjoerg#include <sys/rman.h>
4221826Sjoerg#include <sys/bus_dma.h>
4321826Sjoerg#include <sys/bio.h>
4421826Sjoerg#include <sys/ioccom.h>
4593383Smdodd#include <sys/uio.h>
4621826Sjoerg#include <sys/proc.h>
4721826Sjoerg#include <sys/signalvar.h>
4893383Smdodd#include <sys/sysctl.h>
4993383Smdodd
5093383Smdodd#include <cam/cam.h>
5193383Smdodd#include <cam/cam_ccb.h>
5221830Sjoerg#include <cam/cam_debug.h>
5321826Sjoerg#include <cam/cam_sim.h>
5421826Sjoerg#include <cam/cam_xpt_sim.h>
5521826Sjoerg#include <cam/scsi/scsi_all.h>
5621826Sjoerg#include <cam/scsi/scsi_message.h>
5721826Sjoerg
5821826Sjoerg#include <machine/md_var.h>
5921826Sjoerg#include <machine/bus.h>
6021826Sjoerg#include <machine/resource.h>
6121826Sjoerg
6221826Sjoerg#include <dev/mfi/mfireg.h>
6321826Sjoerg#include <dev/mfi/mfi_ioctl.h>
6421826Sjoerg#include <dev/mfi/mfivar.h>
6521826Sjoerg
66298955Spfgstruct mfip_softc {
6721826Sjoerg	device_t	dev;
6821826Sjoerg	struct mfi_softc *mfi_sc;
6921826Sjoerg	struct cam_devq *devq;
7021826Sjoerg	struct cam_sim	*sim;
7121826Sjoerg	struct cam_path	*path;
7221826Sjoerg};
7321826Sjoerg
7421826Sjoergstatic int	mfip_probe(device_t);
7521826Sjoergstatic int	mfip_attach(device_t);
7621826Sjoergstatic int	mfip_detach(device_t);
7721826Sjoergstatic void	mfip_cam_action(struct cam_sim *, union ccb *);
7821826Sjoergstatic void	mfip_cam_poll(struct cam_sim *);
7921826Sjoergstatic struct mfi_command * mfip_start(void *);
8021826Sjoergstatic void	mfip_done(struct mfi_command *cm);
8121826Sjoerg
8221826Sjoergstatic devclass_t	mfip_devclass;
8393383Smdoddstatic device_method_t	mfip_methods[] = {
8421826Sjoerg	DEVMETHOD(device_probe,		mfip_probe),
8521826Sjoerg	DEVMETHOD(device_attach,	mfip_attach),
8621826Sjoerg	DEVMETHOD(device_detach,	mfip_detach),
8721826Sjoerg	{0, 0}
8821826Sjoerg};
8921826Sjoergstatic driver_t mfip_driver = {
9021826Sjoerg	"mfip",
9121826Sjoerg	mfip_methods,
9221826Sjoerg	sizeof(struct mfip_softc)
9321826Sjoerg};
9421826SjoergDRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
9521826SjoergMODULE_DEPEND(mfip, cam, 1, 1, 1);
9621826SjoergMODULE_DEPEND(mfip, mfi, 1, 1, 1);
9721826Sjoerg
9821826Sjoerg#define ccb_mfip_ptr sim_priv.entries[0].ptr
9921826Sjoerg
10021826Sjoergstatic int
10121826Sjoergmfip_probe(device_t dev)
10221826Sjoerg{
10321826Sjoerg
10421826Sjoerg	device_set_desc(dev, "SCSI Passthrough Bus");
10521826Sjoerg	return (0);
10621826Sjoerg}
10721826Sjoerg
10821826Sjoergstatic int
10921826Sjoergmfip_attach(device_t dev)
11021826Sjoerg{
11121826Sjoerg	struct mfip_softc *sc;
11221826Sjoerg	struct mfi_softc *mfisc;
11321826Sjoerg
11421826Sjoerg	sc = device_get_softc(dev);
11521826Sjoerg	if (sc == NULL)
11621826Sjoerg		return (EINVAL);
11721826Sjoerg
11821826Sjoerg	mfisc = device_get_softc(device_get_parent(dev));
11921826Sjoerg	sc->dev = dev;
12021826Sjoerg	sc->mfi_sc = mfisc;
12121826Sjoerg	mfisc->mfi_cam_start = mfip_start;
12221826Sjoerg
12321826Sjoerg	if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
12421826Sjoerg		return (ENOMEM);
12521826Sjoerg
12621826Sjoerg	sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
12721826Sjoerg				device_get_unit(dev), &mfisc->mfi_io_lock, 1,
12821826Sjoerg				MFI_SCSI_MAX_CMDS, sc->devq);
12921826Sjoerg	if (sc->sim == NULL) {
13021826Sjoerg		cam_simq_free(sc->devq);
13121826Sjoerg		device_printf(dev, "CAM SIM attach failed\n");
13221826Sjoerg		return (EINVAL);
13321826Sjoerg	}
13421826Sjoerg
13521826Sjoerg	mtx_lock(&mfisc->mfi_io_lock);
13621826Sjoerg	if (xpt_bus_register(sc->sim, dev, 0) != 0) {
13721826Sjoerg		device_printf(dev, "XPT bus registration failed\n");
13821826Sjoerg		cam_sim_free(sc->sim, FALSE);
13921826Sjoerg		cam_simq_free(sc->devq);
14021826Sjoerg		mtx_unlock(&mfisc->mfi_io_lock);
14121826Sjoerg		return (EINVAL);
14221826Sjoerg	}
14321826Sjoerg	mtx_unlock(&mfisc->mfi_io_lock);
14421826Sjoerg
14521826Sjoerg	return (0);
14621826Sjoerg}
14721826Sjoerg
14821826Sjoergstatic int
14921826Sjoergmfip_detach(device_t dev)
15021826Sjoerg{
15121826Sjoerg	struct mfip_softc *sc;
15221826Sjoerg
15321826Sjoerg	sc = device_get_softc(dev);
15421826Sjoerg	if (sc == NULL)
15521826Sjoerg		return (EINVAL);
15621826Sjoerg
15721826Sjoerg	if (sc->sim != NULL) {
15821826Sjoerg		mtx_lock(&sc->mfi_sc->mfi_io_lock);
15921826Sjoerg		xpt_bus_deregister(cam_sim_path(sc->sim));
16021826Sjoerg		cam_sim_free(sc->sim, FALSE);
16121826Sjoerg		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
16221826Sjoerg	}
16321826Sjoerg
16421826Sjoerg	if (sc->devq != NULL)
16521826Sjoerg		cam_simq_free(sc->devq);
16621826Sjoerg
16721826Sjoerg	return (0);
16821826Sjoerg}
16921826Sjoerg
17021826Sjoergstatic void
17121826Sjoergmfip_cam_action(struct cam_sim *sim, union ccb *ccb)
17221826Sjoerg{
17321826Sjoerg	struct mfip_softc *sc = cam_sim_softc(sim);
17421826Sjoerg	struct mfi_softc *mfisc = sc->mfi_sc;
17521826Sjoerg
17621826Sjoerg	mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
17721826Sjoerg
17821826Sjoerg	switch (ccb->ccb_h.func_code) {
17921826Sjoerg	case XPT_PATH_INQ:
18021826Sjoerg	{
18121826Sjoerg		struct ccb_pathinq *cpi = &ccb->cpi;
18221826Sjoerg
18321826Sjoerg		cpi->version_num = 1;
18421826Sjoerg		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
18521826Sjoerg		cpi->target_sprt = 0;
18621826Sjoerg		cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
18721826Sjoerg		cpi->hba_eng_cnt = 0;
18821826Sjoerg		cpi->max_target = MFI_SCSI_MAX_TARGETS;
18921826Sjoerg		cpi->max_lun = MFI_SCSI_MAX_LUNS;
19021826Sjoerg		cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
19121826Sjoerg		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
19221826Sjoerg		strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
19321826Sjoerg		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
19421826Sjoerg		cpi->unit_number = cam_sim_unit(sim);
19521826Sjoerg		cpi->bus_id = cam_sim_bus(sim);
19621826Sjoerg		cpi->base_transfer_speed = 150000;
19721826Sjoerg		cpi->transport = XPORT_SAS;
19821826Sjoerg		cpi->transport_version = 0;
19921826Sjoerg		cpi->protocol = PROTO_SCSI;
20021826Sjoerg		cpi->protocol_version = SCSI_REV_2;
20121826Sjoerg		cpi->ccb_h.status = CAM_REQ_CMP;
20221826Sjoerg		break;
20321826Sjoerg	}
20421826Sjoerg	case XPT_RESET_BUS:
20521826Sjoerg		ccb->ccb_h.status = CAM_REQ_CMP;
20621826Sjoerg		break;
20721826Sjoerg	case XPT_RESET_DEV:
20821826Sjoerg		ccb->ccb_h.status = CAM_REQ_CMP;
20921826Sjoerg		break;
21021826Sjoerg	case XPT_GET_TRAN_SETTINGS:
21121826Sjoerg	{
21221826Sjoerg		struct ccb_trans_settings_sas *sas =
21321826Sjoerg		    &ccb->cts.xport_specific.sas;
21421826Sjoerg
21521826Sjoerg		ccb->cts.protocol = PROTO_SCSI;
21621826Sjoerg		ccb->cts.protocol_version = SCSI_REV_2;
21721826Sjoerg		ccb->cts.transport = XPORT_SAS;
21821826Sjoerg		ccb->cts.transport_version = 0;
21921826Sjoerg
22093383Smdodd		sas->valid &= ~CTS_SAS_VALID_SPEED;
22193383Smdodd		sas->bitrate = 150000;
22221826Sjoerg
22321826Sjoerg		ccb->ccb_h.status = CAM_REQ_CMP;
22421826Sjoerg		break;
22521826Sjoerg	}
22621826Sjoerg	case XPT_SET_TRAN_SETTINGS:
22721826Sjoerg		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
22821826Sjoerg		break;
22921826Sjoerg	case XPT_SCSI_IO:
23021826Sjoerg	{
23121826Sjoerg		struct ccb_hdr		*ccbh = &ccb->ccb_h;
23221826Sjoerg		struct ccb_scsiio	*csio = &ccb->csio;
23321826Sjoerg
23421826Sjoerg		ccbh->status = CAM_REQ_INPROG;
23521826Sjoerg		if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
23621826Sjoerg			ccbh->status = CAM_REQ_INVALID;
23721826Sjoerg			break;
23821826Sjoerg		}
23921826Sjoerg		if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
24021826Sjoerg			if (ccbh->flags & CAM_DATA_PHYS) {
24121826Sjoerg				ccbh->status = CAM_REQ_INVALID;
24221826Sjoerg				break;
24321826Sjoerg			}
24421826Sjoerg			if (ccbh->flags & CAM_SCATTER_VALID) {
24521826Sjoerg				ccbh->status = CAM_REQ_INVALID;
24621826Sjoerg				break;
24721826Sjoerg			}
24821826Sjoerg		}
24921826Sjoerg
25021826Sjoerg		ccbh->ccb_mfip_ptr = sc;
25121826Sjoerg		TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
25221826Sjoerg		mfi_startio(mfisc);
25321826Sjoerg		return;
25421826Sjoerg	}
25521826Sjoerg	default:
25621826Sjoerg		ccb->ccb_h.status = CAM_REQ_INVALID;
25721826Sjoerg		break;
25821826Sjoerg	}
25921826Sjoerg
26021826Sjoerg	xpt_done(ccb);
26121826Sjoerg	return;
26221826Sjoerg}
26321826Sjoerg
26421826Sjoergstatic struct mfi_command *
26593383Smdoddmfip_start(void *data)
26621826Sjoerg{
26721826Sjoerg	union ccb *ccb = data;
26821826Sjoerg	struct ccb_hdr *ccbh = &ccb->ccb_h;
26921826Sjoerg	struct ccb_scsiio *csio = &ccb->csio;
27021826Sjoerg	struct mfip_softc *sc;
27121826Sjoerg	struct mfi_pass_frame *pt;
27221826Sjoerg	struct mfi_command *cm;
27393383Smdodd	uint32_t context = 0;
27421826Sjoerg
27521826Sjoerg	sc = ccbh->ccb_mfip_ptr;
27621826Sjoerg
27721826Sjoerg	if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
27821826Sjoerg		return (NULL);
27921826Sjoerg
28021826Sjoerg	/* Zero out the MFI frame */
28121826Sjoerg	context = cm->cm_frame->header.context;
28221826Sjoerg	bzero(cm->cm_frame, sizeof(union mfi_frame));
28321826Sjoerg	cm->cm_frame->header.context = context;
28421826Sjoerg
28521826Sjoerg	pt = &cm->cm_frame->pass;
28621826Sjoerg	pt->header.cmd = MFI_CMD_PD_SCSI_IO;
28721826Sjoerg	pt->header.cmd_status = 0;
28821826Sjoerg	pt->header.scsi_status = 0;
28921826Sjoerg	pt->header.target_id = ccbh->target_id;
29021826Sjoerg	pt->header.lun_id = ccbh->target_lun;
29121826Sjoerg	pt->header.flags = 0;
29221826Sjoerg	pt->header.timeout = 0;
29321826Sjoerg	pt->header.data_len = csio->dxfer_len;
29421826Sjoerg	pt->header.sense_len = MFI_SENSE_LEN;
29521826Sjoerg	pt->header.cdb_len = csio->cdb_len;
29621826Sjoerg	pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
29721826Sjoerg	pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
29821826Sjoerg	if (ccbh->flags & CAM_CDB_POINTER)
29921826Sjoerg		bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
30021826Sjoerg	else
30121826Sjoerg		bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
30221826Sjoerg	cm->cm_complete = mfip_done;
30321826Sjoerg	cm->cm_private = ccb;
30421826Sjoerg	cm->cm_sg = &pt->sgl;
30521826Sjoerg	cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
30621826Sjoerg	cm->cm_data = csio->data_ptr;
30721826Sjoerg	cm->cm_len = csio->dxfer_len;
30821826Sjoerg	switch (ccbh->flags & CAM_DIR_MASK) {
30921826Sjoerg	case CAM_DIR_IN:
31021826Sjoerg		cm->cm_flags = MFI_CMD_DATAIN;
31121826Sjoerg		break;
31221826Sjoerg	case CAM_DIR_OUT:
31321826Sjoerg		cm->cm_flags = MFI_CMD_DATAOUT;
31421826Sjoerg		break;
31521826Sjoerg	case CAM_DIR_NONE:
31621826Sjoerg	default:
31721826Sjoerg		cm->cm_data = NULL;
31821826Sjoerg		cm->cm_len = 0;
31921826Sjoerg		cm->cm_flags = 0;
32021826Sjoerg		break;
32121826Sjoerg	}
32221826Sjoerg
32321826Sjoerg	TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
32421826Sjoerg	return (cm);
32521826Sjoerg}
32621826Sjoerg
32721826Sjoergstatic void
32821826Sjoergmfip_done(struct mfi_command *cm)
32921826Sjoerg{
33021826Sjoerg	union ccb *ccb = cm->cm_private;
33121826Sjoerg	struct ccb_hdr *ccbh = &ccb->ccb_h;
33221826Sjoerg	struct ccb_scsiio *csio = &ccb->csio;
33321826Sjoerg	struct mfip_softc *sc;
33421826Sjoerg	struct mfi_pass_frame *pt;
33521826Sjoerg
33621826Sjoerg	sc = ccbh->ccb_mfip_ptr;
33721826Sjoerg	pt = &cm->cm_frame->pass;
33821826Sjoerg
33921826Sjoerg	switch (pt->header.cmd_status) {
34021826Sjoerg	case MFI_STAT_OK:
34121826Sjoerg	{
34221826Sjoerg		uint8_t command, device;
34321826Sjoerg
34421826Sjoerg		ccbh->status = CAM_REQ_CMP;
34521826Sjoerg		csio->scsi_status = pt->header.scsi_status;
34621826Sjoerg		if (ccbh->flags & CAM_CDB_POINTER)
34721826Sjoerg			command = csio->cdb_io.cdb_ptr[0];
34821826Sjoerg		else
34921826Sjoerg			command = csio->cdb_io.cdb_bytes[0];
35021826Sjoerg		if (command == INQUIRY) {
35121826Sjoerg			device = csio->data_ptr[0] & 0x1f;
35221826Sjoerg			if ((device == T_DIRECT) || (device == T_PROCESSOR))
35321826Sjoerg				csio->data_ptr[0] =
35421826Sjoerg				     (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
35521826Sjoerg		}
35621826Sjoerg		break;
35721826Sjoerg	}
35821826Sjoerg	case MFI_STAT_SCSI_DONE_WITH_ERROR:
35921826Sjoerg	{
36021826Sjoerg		int sense_len;
36121826Sjoerg
36221826Sjoerg		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
36321826Sjoerg		csio->scsi_status = pt->header.scsi_status;
36421826Sjoerg		if (pt->header.sense_len < csio->sense_len)
36521826Sjoerg			csio->sense_resid = csio->sense_len -
36621826Sjoerg			    pt->header.sense_len;
36721826Sjoerg		else
36821826Sjoerg			csio->sense_resid = 0;
36921826Sjoerg		sense_len = min(pt->header.sense_len,
37021826Sjoerg		    sizeof(struct scsi_sense_data));
37121826Sjoerg		bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
37221826Sjoerg		bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
37321826Sjoerg		break;
37421826Sjoerg	}
37521826Sjoerg	case MFI_STAT_DEVICE_NOT_FOUND:
37621826Sjoerg		ccbh->status = CAM_SEL_TIMEOUT;
37721826Sjoerg		break;
37821826Sjoerg	case MFI_STAT_SCSI_IO_FAILED:
37921826Sjoerg		ccbh->status = CAM_REQ_CMP_ERR;
38021826Sjoerg		csio->scsi_status = pt->header.scsi_status;
38121826Sjoerg		break;
38221826Sjoerg	default:
38321826Sjoerg		ccbh->status = CAM_REQ_CMP_ERR;
38421826Sjoerg		csio->scsi_status = pt->header.scsi_status;
38521826Sjoerg		break;
38621826Sjoerg	}
38721826Sjoerg
38821826Sjoerg	mfi_release_command(cm);
38921826Sjoerg	xpt_done(ccb);
39021826Sjoerg}
39121826Sjoerg
39221826Sjoergstatic void
39321826Sjoergmfip_cam_poll(struct cam_sim *sim)
39421826Sjoerg{
39521826Sjoerg	return;
39621826Sjoerg}
39721826Sjoerg
39821826Sjoerg