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.6 2011/06/28 08:36:48 kevlo 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>
55 #include <machine/md_var.h>
57 #include <dev/raid/mfi/mfireg.h>
58 #include <dev/raid/mfi/mfi_ioctl.h>
59 #include <dev/raid/mfi/mfivar.h>
63 struct mfi_softc *mfi_sc;
64 struct cam_devq *devq;
66 struct cam_path *path;
69 static int mfip_probe(device_t);
70 static int mfip_attach(device_t);
71 static int mfip_detach(device_t);
72 static void mfip_cam_action(struct cam_sim *, union ccb *);
73 static void mfip_cam_poll(struct cam_sim *);
74 static struct mfi_command * mfip_start(void *);
75 static void mfip_done(struct mfi_command *cm);
77 static devclass_t mfip_devclass;
78 static device_method_t mfip_methods[] = {
79 DEVMETHOD(device_probe, mfip_probe),
80 DEVMETHOD(device_attach, mfip_attach),
81 DEVMETHOD(device_detach, mfip_detach),
84 static driver_t mfip_driver = {
87 sizeof(struct mfip_softc)
89 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, NULL, NULL);
90 MODULE_DEPEND(mfip, cam, 1, 1, 1);
91 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
93 #define ccb_mfip_ptr sim_priv.entries[0].ptr
96 mfip_probe(device_t dev)
99 device_set_desc(dev, "SCSI Passthrough Bus");
104 mfip_attach(device_t dev)
106 struct mfip_softc *sc;
107 struct mfi_softc *mfisc;
109 sc = device_get_softc(dev);
113 mfisc = device_get_softc(device_get_parent(dev));
116 mfisc->mfi_cam_start = mfip_start;
118 if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
121 sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
122 device_get_unit(dev), &mfisc->mfi_io_lock, 1,
123 MFI_SCSI_MAX_CMDS, sc->devq);
124 if (sc->sim == NULL) {
125 device_printf(dev, "CAM SIM attach failed\n");
129 lockmgr(&mfisc->mfi_io_lock, LK_EXCLUSIVE);
130 if (xpt_bus_register(sc->sim, 0) != 0) {
131 device_printf(dev, "XPT bus registration failed\n");
132 cam_sim_free(sc->sim);
133 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
136 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
142 mfip_detach(device_t dev)
144 struct mfip_softc *sc;
146 sc = device_get_softc(dev);
150 if (sc->sim != NULL) {
151 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_EXCLUSIVE);
152 xpt_bus_deregister(cam_sim_path(sc->sim));
153 cam_sim_free(sc->sim);
154 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_RELEASE);
161 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
163 struct mfip_softc *sc = cam_sim_softc(sim);
164 struct mfi_softc *mfisc = sc->mfi_sc;
166 KKASSERT(lockstatus(&mfisc->mfi_io_lock, curthread) != 0);
168 switch (ccb->ccb_h.func_code) {
171 struct ccb_pathinq *cpi = &ccb->cpi;
173 cpi->version_num = 1;
174 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
175 cpi->target_sprt = 0;
176 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
177 cpi->hba_eng_cnt = 0;
178 cpi->max_target = MFI_SCSI_MAX_TARGETS;
179 cpi->max_lun = MFI_SCSI_MAX_LUNS;
180 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
181 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
182 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
183 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
184 cpi->unit_number = cam_sim_unit(sim);
185 cpi->bus_id = cam_sim_bus(sim);
186 cpi->base_transfer_speed = 150000;
187 cpi->transport = XPORT_SAS;
188 cpi->transport_version = 0;
189 cpi->protocol = PROTO_SCSI;
190 cpi->protocol_version = SCSI_REV_2;
191 cpi->ccb_h.status = CAM_REQ_CMP;
195 ccb->ccb_h.status = CAM_REQ_CMP;
198 ccb->ccb_h.status = CAM_REQ_CMP;
200 case XPT_GET_TRAN_SETTINGS:
202 struct ccb_trans_settings_sas *sas =
203 &ccb->cts.xport_specific.sas;
205 ccb->cts.protocol = PROTO_SCSI;
206 ccb->cts.protocol_version = SCSI_REV_2;
207 ccb->cts.transport = XPORT_SAS;
208 ccb->cts.transport_version = 0;
210 sas->valid &= ~CTS_SAS_VALID_SPEED;
211 sas->bitrate = 150000;
213 ccb->ccb_h.status = CAM_REQ_CMP;
216 case XPT_SET_TRAN_SETTINGS:
217 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
221 struct ccb_hdr *ccbh = &ccb->ccb_h;
222 struct ccb_scsiio *csio = &ccb->csio;
224 ccbh->status = CAM_REQ_INPROG;
225 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
226 ccbh->status = CAM_REQ_INVALID;
229 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
230 if (ccbh->flags & CAM_DATA_PHYS) {
231 ccbh->status = CAM_REQ_INVALID;
234 if (ccbh->flags & CAM_SCATTER_VALID) {
235 ccbh->status = CAM_REQ_INVALID;
240 ccbh->ccb_mfip_ptr = sc;
241 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
246 ccb->ccb_h.status = CAM_REQ_INVALID;
254 static struct mfi_command *
255 mfip_start(void *data)
257 union ccb *ccb = data;
258 struct ccb_hdr *ccbh = &ccb->ccb_h;
259 struct ccb_scsiio *csio = &ccb->csio;
260 struct mfip_softc *sc;
261 struct mfi_pass_frame *pt;
262 struct mfi_command *cm;
264 sc = ccbh->ccb_mfip_ptr;
266 if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
269 pt = &cm->cm_frame->pass;
270 pt->header.cmd = MFI_CMD_PD_SCSI_IO;
271 pt->header.cmd_status = 0;
272 pt->header.scsi_status = 0;
273 pt->header.target_id = ccbh->target_id;
274 pt->header.lun_id = ccbh->target_lun;
275 pt->header.flags = 0;
276 pt->header.timeout = 0;
277 pt->header.data_len = csio->dxfer_len;
278 pt->header.sense_len = MFI_SENSE_LEN;
279 pt->header.cdb_len = csio->cdb_len;
280 pt->sense_addr_lo = cm->cm_sense_busaddr;
281 pt->sense_addr_hi = 0;
282 if (ccbh->flags & CAM_CDB_POINTER)
283 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
285 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
286 cm->cm_complete = mfip_done;
287 cm->cm_private = ccb;
288 cm->cm_sg = &pt->sgl;
289 cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
290 cm->cm_data = csio->data_ptr;
291 cm->cm_len = csio->dxfer_len;
292 switch (ccbh->flags & CAM_DIR_MASK) {
294 cm->cm_flags = MFI_CMD_DATAIN;
297 cm->cm_flags = MFI_CMD_DATAOUT;
307 TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
312 mfip_done(struct mfi_command *cm)
314 union ccb *ccb = cm->cm_private;
315 struct ccb_hdr *ccbh = &ccb->ccb_h;
316 struct ccb_scsiio *csio = &ccb->csio;
317 struct mfip_softc *sc;
318 struct mfi_pass_frame *pt;
320 sc = ccbh->ccb_mfip_ptr;
321 pt = &cm->cm_frame->pass;
323 switch (pt->header.cmd_status) {
326 uint8_t command, device;
328 ccbh->status = CAM_REQ_CMP;
329 csio->scsi_status = pt->header.scsi_status;
330 if (ccbh->flags & CAM_CDB_POINTER)
331 command = csio->cdb_io.cdb_ptr[0];
333 command = csio->cdb_io.cdb_bytes[0];
334 if (command == INQUIRY) {
335 device = csio->data_ptr[0] & 0x1f;
336 if ((device == T_DIRECT) || (device == T_PROCESSOR))
338 (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
342 case MFI_STAT_SCSI_DONE_WITH_ERROR:
346 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
347 csio->scsi_status = pt->header.scsi_status;
348 sense_len = min(pt->header.sense_len, sizeof(struct scsi_sense_data));
349 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
350 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
353 case MFI_STAT_DEVICE_NOT_FOUND:
354 ccbh->status = CAM_SEL_TIMEOUT;
356 case MFI_STAT_SCSI_IO_FAILED:
357 ccbh->status = CAM_REQ_CMP_ERR;
358 csio->scsi_status = pt->header.scsi_status;
361 ccbh->status = CAM_REQ_CMP_ERR;
362 csio->scsi_status = pt->header.scsi_status;
366 mfi_release_command(cm);
371 mfip_cam_poll(struct cam_sim *sim)