2 * Copyright 2007 Scott Long
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/mfi/mfi_cam.c,v 1.5 2010/12/06 17:02:56 jhb Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
38 #include <sys/eventhandler.h>
40 #include <sys/bus_dma.h>
42 #include <sys/ioccom.h>
45 #include <sys/signalvar.h>
47 #include <bus/cam/cam.h>
48 #include <bus/cam/cam_ccb.h>
49 #include <bus/cam/cam_debug.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/scsi/scsi_all.h>
53 #include <bus/cam/scsi/scsi_message.h>
57 #include <machine/md_var.h>
60 #include <dev/raid/mfi/mfireg.h>
61 #include <dev/raid/mfi/mfi_ioctl.h>
62 #include <dev/raid/mfi/mfivar.h>
66 struct mfi_softc *mfi_sc;
67 struct cam_devq *devq;
69 struct cam_path *path;
72 static int mfip_probe(device_t);
73 static int mfip_attach(device_t);
74 static int mfip_detach(device_t);
75 static void mfip_cam_action(struct cam_sim *, union ccb *);
76 static void mfip_cam_poll(struct cam_sim *);
77 static struct mfi_command * mfip_start(void *);
78 static void mfip_done(struct mfi_command *cm);
80 static devclass_t mfip_devclass;
81 static device_method_t mfip_methods[] = {
82 DEVMETHOD(device_probe, mfip_probe),
83 DEVMETHOD(device_attach, mfip_attach),
84 DEVMETHOD(device_detach, mfip_detach),
87 static driver_t mfip_driver = {
90 sizeof(struct mfip_softc)
92 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
93 MODULE_DEPEND(mfip, cam, 1, 1, 1);
94 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
96 #define ccb_mfip_ptr sim_priv.entries[0].ptr
99 mfip_probe(device_t dev)
102 device_set_desc(dev, "SCSI Passthrough Bus");
107 mfip_attach(device_t dev)
109 struct mfip_softc *sc;
110 struct mfi_softc *mfisc;
112 sc = device_get_softc(dev);
116 mfisc = device_get_softc(device_get_parent(dev));
119 mfisc->mfi_cam_start = mfip_start;
121 if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
124 sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
125 device_get_unit(dev), &mfisc->mfi_io_lock, 1,
126 MFI_SCSI_MAX_CMDS, sc->devq);
127 if (sc->sim == NULL) {
128 device_printf(dev, "CAM SIM attach failed\n");
132 lockmgr(&mfisc->mfi_io_lock, LK_EXCLUSIVE);
133 if (xpt_bus_register(sc->sim, 0) != 0) {
134 device_printf(dev, "XPT bus registration failed\n");
135 cam_sim_free(sc->sim);
136 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
139 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
145 mfip_detach(device_t dev)
147 struct mfip_softc *sc;
149 sc = device_get_softc(dev);
153 if (sc->sim != NULL) {
154 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_EXCLUSIVE);
155 xpt_bus_deregister(cam_sim_path(sc->sim));
156 cam_sim_free(sc->sim);
157 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_RELEASE);
164 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
166 struct mfip_softc *sc = cam_sim_softc(sim);
167 struct mfi_softc *mfisc = sc->mfi_sc;
169 KKASSERT(lockstatus(&mfisc->mfi_io_lock, curthread) != 0);
171 switch (ccb->ccb_h.func_code) {
174 struct ccb_pathinq *cpi = &ccb->cpi;
176 cpi->version_num = 1;
177 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
178 cpi->target_sprt = 0;
179 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
180 cpi->hba_eng_cnt = 0;
181 cpi->max_target = MFI_SCSI_MAX_TARGETS;
182 cpi->max_lun = MFI_SCSI_MAX_LUNS;
183 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
184 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
185 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
186 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
187 cpi->unit_number = cam_sim_unit(sim);
188 cpi->bus_id = cam_sim_bus(sim);
189 cpi->base_transfer_speed = 150000;
190 cpi->transport = XPORT_SAS;
191 cpi->transport_version = 0;
192 cpi->protocol = PROTO_SCSI;
193 cpi->protocol_version = SCSI_REV_2;
194 cpi->ccb_h.status = CAM_REQ_CMP;
198 ccb->ccb_h.status = CAM_REQ_CMP;
201 ccb->ccb_h.status = CAM_REQ_CMP;
203 case XPT_GET_TRAN_SETTINGS:
205 struct ccb_trans_settings_sas *sas =
206 &ccb->cts.xport_specific.sas;
208 ccb->cts.protocol = PROTO_SCSI;
209 ccb->cts.protocol_version = SCSI_REV_2;
210 ccb->cts.transport = XPORT_SAS;
211 ccb->cts.transport_version = 0;
213 sas->valid &= ~CTS_SAS_VALID_SPEED;
214 sas->bitrate = 150000;
216 ccb->ccb_h.status = CAM_REQ_CMP;
219 case XPT_SET_TRAN_SETTINGS:
220 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
224 struct ccb_hdr *ccbh = &ccb->ccb_h;
225 struct ccb_scsiio *csio = &ccb->csio;
227 ccbh->status = CAM_REQ_INPROG;
228 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
229 ccbh->status = CAM_REQ_INVALID;
232 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
233 if (ccbh->flags & CAM_DATA_PHYS) {
234 ccbh->status = CAM_REQ_INVALID;
237 if (ccbh->flags & CAM_SCATTER_VALID) {
238 ccbh->status = CAM_REQ_INVALID;
243 ccbh->ccb_mfip_ptr = sc;
244 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
249 ccb->ccb_h.status = CAM_REQ_INVALID;
257 static struct mfi_command *
258 mfip_start(void *data)
260 union ccb *ccb = data;
261 struct ccb_hdr *ccbh = &ccb->ccb_h;
262 struct ccb_scsiio *csio = &ccb->csio;
263 struct mfip_softc *sc;
264 struct mfi_pass_frame *pt;
265 struct mfi_command *cm;
267 sc = ccbh->ccb_mfip_ptr;
269 if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
272 pt = &cm->cm_frame->pass;
273 pt->header.cmd = MFI_CMD_PD_SCSI_IO;
274 pt->header.cmd_status = 0;
275 pt->header.scsi_status = 0;
276 pt->header.target_id = ccbh->target_id;
277 pt->header.lun_id = ccbh->target_lun;
278 pt->header.flags = 0;
279 pt->header.timeout = 0;
280 pt->header.data_len = csio->dxfer_len;
281 pt->header.sense_len = MFI_SENSE_LEN;
282 pt->header.cdb_len = csio->cdb_len;
283 pt->sense_addr_lo = cm->cm_sense_busaddr;
284 pt->sense_addr_hi = 0;
285 if (ccbh->flags & CAM_CDB_POINTER)
286 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
288 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
289 cm->cm_complete = mfip_done;
290 cm->cm_private = ccb;
291 cm->cm_sg = &pt->sgl;
292 cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
293 cm->cm_data = csio->data_ptr;
294 cm->cm_len = csio->dxfer_len;
295 switch (ccbh->flags & CAM_DIR_MASK) {
297 cm->cm_flags = MFI_CMD_DATAIN;
300 cm->cm_flags = MFI_CMD_DATAOUT;
310 TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
315 mfip_done(struct mfi_command *cm)
317 union ccb *ccb = cm->cm_private;
318 struct ccb_hdr *ccbh = &ccb->ccb_h;
319 struct ccb_scsiio *csio = &ccb->csio;
320 struct mfip_softc *sc;
321 struct mfi_pass_frame *pt;
323 sc = ccbh->ccb_mfip_ptr;
324 pt = &cm->cm_frame->pass;
326 switch (pt->header.cmd_status) {
329 uint8_t command, device;
331 ccbh->status = CAM_REQ_CMP;
332 csio->scsi_status = pt->header.scsi_status;
333 if (ccbh->flags & CAM_CDB_POINTER)
334 command = csio->cdb_io.cdb_ptr[0];
336 command = csio->cdb_io.cdb_bytes[0];
337 if (command == INQUIRY) {
338 device = csio->data_ptr[0] & 0x1f;
339 if ((device == T_DIRECT) || (device == T_PROCESSOR))
341 (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
345 case MFI_STAT_SCSI_DONE_WITH_ERROR:
349 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
350 csio->scsi_status = pt->header.scsi_status;
351 sense_len = min(pt->header.sense_len, sizeof(struct scsi_sense_data));
352 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
353 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
356 case MFI_STAT_DEVICE_NOT_FOUND:
357 ccbh->status = CAM_SEL_TIMEOUT;
359 case MFI_STAT_SCSI_IO_FAILED:
360 ccbh->status = CAM_REQ_CMP_ERR;
361 csio->scsi_status = pt->header.scsi_status;
364 ccbh->status = CAM_REQ_CMP_ERR;
365 csio->scsi_status = pt->header.scsi_status;
369 mfi_release_command(cm);
374 mfip_cam_poll(struct cam_sim *sim)