xref: /freebsd/sys/dev/mfi/mfi_cam.c (revision 2e1417489338b971e5fd599ff48b5f65df9e8d3b)
1 /*-
2  * Copyright 2007 Scott Long
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_mfi.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/selinfo.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/eventhandler.h>
41 #include <sys/rman.h>
42 #include <sys/bus_dma.h>
43 #include <sys/bio.h>
44 #include <sys/ioccom.h>
45 #include <sys/uio.h>
46 #include <sys/proc.h>
47 #include <sys/signalvar.h>
48 #include <sys/sysctl.h>
49 
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_debug.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
57 
58 #include <machine/md_var.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
61 
62 #include <dev/mfi/mfireg.h>
63 #include <dev/mfi/mfi_ioctl.h>
64 #include <dev/mfi/mfivar.h>
65 
66 struct mfip_softc {
67 	device_t	dev;
68 	struct mfi_softc *mfi_sc;
69 	struct cam_devq *devq;
70 	struct cam_sim	*sim;
71 	struct cam_path	*path;
72 };
73 
74 static int	mfip_probe(device_t);
75 static int	mfip_attach(device_t);
76 static int	mfip_detach(device_t);
77 static void	mfip_cam_action(struct cam_sim *, union ccb *);
78 static void	mfip_cam_poll(struct cam_sim *);
79 static struct mfi_command * mfip_start(void *);
80 static void	mfip_done(struct mfi_command *cm);
81 
82 static devclass_t	mfip_devclass;
83 static device_method_t	mfip_methods[] = {
84 	DEVMETHOD(device_probe,		mfip_probe),
85 	DEVMETHOD(device_attach,	mfip_attach),
86 	DEVMETHOD(device_detach,	mfip_detach),
87 	{0, 0}
88 };
89 static driver_t mfip_driver = {
90 	"mfip",
91 	mfip_methods,
92 	sizeof(struct mfip_softc)
93 };
94 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
95 MODULE_DEPEND(mfip, cam, 1, 1, 1);
96 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
97 
98 #define ccb_mfip_ptr sim_priv.entries[0].ptr
99 
100 static int
101 mfip_probe(device_t dev)
102 {
103 
104 	device_set_desc(dev, "SCSI Passthrough Bus");
105 	return (0);
106 }
107 
108 static int
109 mfip_attach(device_t dev)
110 {
111 	struct mfip_softc *sc;
112 	struct mfi_softc *mfisc;
113 
114 	sc = device_get_softc(dev);
115 	if (sc == NULL)
116 		return (EINVAL);
117 
118 	mfisc = device_get_softc(device_get_parent(dev));
119 	sc->dev = dev;
120 	sc->mfi_sc = mfisc;
121 	mfisc->mfi_cam_start = mfip_start;
122 
123 	if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
124 		return (ENOMEM);
125 
126 	sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
127 				device_get_unit(dev), &mfisc->mfi_io_lock, 1,
128 				MFI_SCSI_MAX_CMDS, sc->devq);
129 	if (sc->sim == NULL) {
130 		cam_simq_free(sc->devq);
131 		device_printf(dev, "CAM SIM attach failed\n");
132 		return (EINVAL);
133 	}
134 
135 	mtx_lock(&mfisc->mfi_io_lock);
136 	if (xpt_bus_register(sc->sim, dev, 0) != 0) {
137 		device_printf(dev, "XPT bus registration failed\n");
138 		cam_sim_free(sc->sim, FALSE);
139 		cam_simq_free(sc->devq);
140 		mtx_unlock(&mfisc->mfi_io_lock);
141 		return (EINVAL);
142 	}
143 	mtx_unlock(&mfisc->mfi_io_lock);
144 
145 	return (0);
146 }
147 
148 static int
149 mfip_detach(device_t dev)
150 {
151 	struct mfip_softc *sc;
152 
153 	sc = device_get_softc(dev);
154 	if (sc == NULL)
155 		return (EINVAL);
156 
157 	if (sc->sim != NULL) {
158 		mtx_lock(&sc->mfi_sc->mfi_io_lock);
159 		xpt_bus_deregister(cam_sim_path(sc->sim));
160 		cam_sim_free(sc->sim, FALSE);
161 		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
162 	}
163 
164 	if (sc->devq != NULL)
165 		cam_simq_free(sc->devq);
166 
167 	return (0);
168 }
169 
170 static void
171 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
172 {
173 	struct mfip_softc *sc = cam_sim_softc(sim);
174 	struct mfi_softc *mfisc = sc->mfi_sc;
175 
176 	mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
177 
178 	switch (ccb->ccb_h.func_code) {
179 	case XPT_PATH_INQ:
180 	{
181 		struct ccb_pathinq *cpi = &ccb->cpi;
182 
183 		cpi->version_num = 1;
184 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
185 		cpi->target_sprt = 0;
186 		cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
187 		cpi->hba_eng_cnt = 0;
188 		cpi->max_target = MFI_SCSI_MAX_TARGETS;
189 		cpi->max_lun = MFI_SCSI_MAX_LUNS;
190 		cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
191 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
192 		strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
193 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
194 		cpi->unit_number = cam_sim_unit(sim);
195 		cpi->bus_id = cam_sim_bus(sim);
196 		cpi->base_transfer_speed = 150000;
197 		cpi->transport = XPORT_SAS;
198 		cpi->transport_version = 0;
199 		cpi->protocol = PROTO_SCSI;
200 		cpi->protocol_version = SCSI_REV_2;
201 		cpi->ccb_h.status = CAM_REQ_CMP;
202 		break;
203 	}
204 	case XPT_RESET_BUS:
205 		ccb->ccb_h.status = CAM_REQ_CMP;
206 		break;
207 	case XPT_RESET_DEV:
208 		ccb->ccb_h.status = CAM_REQ_CMP;
209 		break;
210 	case XPT_GET_TRAN_SETTINGS:
211 	{
212 		struct ccb_trans_settings_sas *sas =
213 		    &ccb->cts.xport_specific.sas;
214 
215 		ccb->cts.protocol = PROTO_SCSI;
216 		ccb->cts.protocol_version = SCSI_REV_2;
217 		ccb->cts.transport = XPORT_SAS;
218 		ccb->cts.transport_version = 0;
219 
220 		sas->valid &= ~CTS_SAS_VALID_SPEED;
221 		sas->bitrate = 150000;
222 
223 		ccb->ccb_h.status = CAM_REQ_CMP;
224 		break;
225 	}
226 	case XPT_SET_TRAN_SETTINGS:
227 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
228 		break;
229 	case XPT_SCSI_IO:
230 	{
231 		struct ccb_hdr		*ccbh = &ccb->ccb_h;
232 		struct ccb_scsiio	*csio = &ccb->csio;
233 
234 		ccbh->status = CAM_REQ_INPROG;
235 		if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
236 			ccbh->status = CAM_REQ_INVALID;
237 			break;
238 		}
239 		if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
240 			if (ccbh->flags & CAM_DATA_PHYS) {
241 				ccbh->status = CAM_REQ_INVALID;
242 				break;
243 			}
244 			if (ccbh->flags & CAM_SCATTER_VALID) {
245 				ccbh->status = CAM_REQ_INVALID;
246 				break;
247 			}
248 		}
249 
250 		ccbh->ccb_mfip_ptr = sc;
251 		TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
252 		mfi_startio(mfisc);
253 		return;
254 	}
255 	default:
256 		ccb->ccb_h.status = CAM_REQ_INVALID;
257 		break;
258 	}
259 
260 	xpt_done(ccb);
261 	return;
262 }
263 
264 static struct mfi_command *
265 mfip_start(void *data)
266 {
267 	union ccb *ccb = data;
268 	struct ccb_hdr *ccbh = &ccb->ccb_h;
269 	struct ccb_scsiio *csio = &ccb->csio;
270 	struct mfip_softc *sc;
271 	struct mfi_pass_frame *pt;
272 	struct mfi_command *cm;
273 
274 	sc = ccbh->ccb_mfip_ptr;
275 
276 	if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
277 		return (NULL);
278 
279 	pt = &cm->cm_frame->pass;
280 	pt->header.cmd = MFI_CMD_PD_SCSI_IO;
281 	pt->header.cmd_status = 0;
282 	pt->header.scsi_status = 0;
283 	pt->header.target_id = ccbh->target_id;
284 	pt->header.lun_id = ccbh->target_lun;
285 	pt->header.flags = 0;
286 	pt->header.timeout = 0;
287 	pt->header.data_len = csio->dxfer_len;
288 	pt->header.sense_len = MFI_SENSE_LEN;
289 	pt->header.cdb_len = csio->cdb_len;
290 	pt->sense_addr_lo = cm->cm_sense_busaddr;
291 	pt->sense_addr_hi = 0;
292 	if (ccbh->flags & CAM_CDB_POINTER)
293 		bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
294 	else
295 		bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
296 	cm->cm_complete = mfip_done;
297 	cm->cm_private = ccb;
298 	cm->cm_sg = &pt->sgl;
299 	cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
300 	cm->cm_data = csio->data_ptr;
301 	cm->cm_len = csio->dxfer_len;
302 	switch (ccbh->flags & CAM_DIR_MASK) {
303 	case CAM_DIR_IN:
304 		cm->cm_flags = MFI_CMD_DATAIN;
305 		break;
306 	case CAM_DIR_OUT:
307 		cm->cm_flags = MFI_CMD_DATAOUT;
308 		break;
309 	case CAM_DIR_NONE:
310 	default:
311 		cm->cm_data = NULL;
312 		cm->cm_len = 0;
313 		cm->cm_flags = 0;
314 		break;
315 	}
316 
317 	TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
318 	return (cm);
319 }
320 
321 static void
322 mfip_done(struct mfi_command *cm)
323 {
324 	union ccb *ccb = cm->cm_private;
325 	struct ccb_hdr *ccbh = &ccb->ccb_h;
326 	struct ccb_scsiio *csio = &ccb->csio;
327 	struct mfip_softc *sc;
328 	struct mfi_pass_frame *pt;
329 
330 	sc = ccbh->ccb_mfip_ptr;
331 	pt = &cm->cm_frame->pass;
332 
333 	switch (pt->header.cmd_status) {
334 	case MFI_STAT_OK:
335 	{
336 		uint8_t command, device;
337 
338 		ccbh->status = CAM_REQ_CMP;
339 		csio->scsi_status = pt->header.scsi_status;
340 		if (ccbh->flags & CAM_CDB_POINTER)
341 			command = csio->cdb_io.cdb_ptr[0];
342 		else
343 			command = csio->cdb_io.cdb_bytes[0];
344 		if (command == INQUIRY) {
345 			device = csio->data_ptr[0] & 0x1f;
346 			if ((device == T_DIRECT) || (device == T_PROCESSOR))
347 				csio->data_ptr[0] =
348 				     (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
349 		}
350 		break;
351 	}
352 	case MFI_STAT_SCSI_DONE_WITH_ERROR:
353 	{
354 		int sense_len;
355 
356 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
357 		csio->scsi_status = pt->header.scsi_status;
358 		if (pt->header.sense_len < csio->sense_len)
359 			csio->sense_resid = csio->sense_len -
360 			    pt->header.sense_len;
361 		else
362 			csio->sense_resid = 0;
363 		sense_len = min(pt->header.sense_len,
364 		    sizeof(struct scsi_sense_data));
365 		bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
366 		bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
367 		break;
368 	}
369 	case MFI_STAT_DEVICE_NOT_FOUND:
370 		ccbh->status = CAM_SEL_TIMEOUT;
371 		break;
372 	case MFI_STAT_SCSI_IO_FAILED:
373 		ccbh->status = CAM_REQ_CMP_ERR;
374 		csio->scsi_status = pt->header.scsi_status;
375 		break;
376 	default:
377 		ccbh->status = CAM_REQ_CMP_ERR;
378 		csio->scsi_status = pt->header.scsi_status;
379 		break;
380 	}
381 
382 	mfi_release_command(cm);
383 	xpt_done(ccb);
384 }
385 
386 static void
387 mfip_cam_poll(struct cam_sim *sim)
388 {
389 	return;
390 }
391 
392