xref: /freebsd/sys/dev/mfi/mfi_cam.c (revision a18eacbefdfa1085ca3db829e86ece78cd416493)
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_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 
60 #include <machine/md_var.h>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 
64 #include <dev/mfi/mfireg.h>
65 #include <dev/mfi/mfi_ioctl.h>
66 #include <dev/mfi/mfivar.h>
67 
68 enum mfip_state {
69 	MFIP_STATE_NONE,
70 	MFIP_STATE_DETACH,
71 	MFIP_STATE_RESCAN
72 };
73 
74 struct mfip_softc {
75 	device_t	dev;
76 	struct mfi_softc *mfi_sc;
77 	struct cam_devq *devq;
78 	struct cam_sim	*sim;
79 	struct cam_path	*path;
80 	enum mfip_state	state;
81 };
82 
83 static int	mfip_probe(device_t);
84 static int	mfip_attach(device_t);
85 static int	mfip_detach(device_t);
86 static void	mfip_cam_action(struct cam_sim *, union ccb *);
87 static void	mfip_cam_poll(struct cam_sim *);
88 static void	mfip_cam_rescan(struct mfi_softc *, uint32_t tid);
89 static struct mfi_command * mfip_start(void *);
90 static void	mfip_done(struct mfi_command *cm);
91 
92 static int mfi_allow_disks = 0;
93 TUNABLE_INT("hw.mfi.allow_cam_disk_passthrough", &mfi_allow_disks);
94 SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RD,
95     &mfi_allow_disks, 0, "event message locale");
96 
97 static devclass_t	mfip_devclass;
98 static device_method_t	mfip_methods[] = {
99 	DEVMETHOD(device_probe,		mfip_probe),
100 	DEVMETHOD(device_attach,	mfip_attach),
101 	DEVMETHOD(device_detach,	mfip_detach),
102 
103 	DEVMETHOD_END
104 };
105 static driver_t mfip_driver = {
106 	"mfip",
107 	mfip_methods,
108 	sizeof(struct mfip_softc)
109 };
110 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
111 MODULE_DEPEND(mfip, cam, 1, 1, 1);
112 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
113 
114 #define ccb_mfip_ptr sim_priv.entries[0].ptr
115 
116 static int
117 mfip_probe(device_t dev)
118 {
119 
120 	device_set_desc(dev, "SCSI Passthrough Bus");
121 	return (0);
122 }
123 
124 static int
125 mfip_attach(device_t dev)
126 {
127 	struct mfip_softc *sc;
128 	struct mfi_softc *mfisc;
129 
130 	sc = device_get_softc(dev);
131 	if (sc == NULL)
132 		return (EINVAL);
133 
134 	mfisc = device_get_softc(device_get_parent(dev));
135 	sc->dev = dev;
136 	sc->state = MFIP_STATE_NONE;
137 	sc->mfi_sc = mfisc;
138 	mfisc->mfi_cam_start = mfip_start;
139 
140 	if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
141 		return (ENOMEM);
142 
143 	sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
144 				device_get_unit(dev), &mfisc->mfi_io_lock, 1,
145 				MFI_SCSI_MAX_CMDS, sc->devq);
146 	if (sc->sim == NULL) {
147 		cam_simq_free(sc->devq);
148 		sc->devq = NULL;
149 		device_printf(dev, "CAM SIM attach failed\n");
150 		return (EINVAL);
151 	}
152 
153 	mfisc->mfi_cam_rescan_cb = mfip_cam_rescan;
154 
155 	mtx_lock(&mfisc->mfi_io_lock);
156 	if (xpt_bus_register(sc->sim, dev, 0) != 0) {
157 		device_printf(dev, "XPT bus registration failed\n");
158 		cam_sim_free(sc->sim, FALSE);
159 		sc->sim = NULL;
160 		cam_simq_free(sc->devq);
161 		sc->devq = NULL;
162 		mtx_unlock(&mfisc->mfi_io_lock);
163 		return (EINVAL);
164 	}
165 	mtx_unlock(&mfisc->mfi_io_lock);
166 
167 	return (0);
168 }
169 
170 static int
171 mfip_detach(device_t dev)
172 {
173 	struct mfip_softc *sc;
174 
175 	sc = device_get_softc(dev);
176 	if (sc == NULL)
177 		return (EINVAL);
178 
179 	mtx_lock(&sc->mfi_sc->mfi_io_lock);
180 	if (sc->state == MFIP_STATE_RESCAN) {
181 		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
182 		return (EBUSY);
183 	}
184 	sc->state = MFIP_STATE_DETACH;
185 	mtx_unlock(&sc->mfi_sc->mfi_io_lock);
186 
187 	sc->mfi_sc->mfi_cam_rescan_cb = NULL;
188 
189 	if (sc->sim != NULL) {
190 		mtx_lock(&sc->mfi_sc->mfi_io_lock);
191 		xpt_bus_deregister(cam_sim_path(sc->sim));
192 		cam_sim_free(sc->sim, FALSE);
193 		sc->sim = NULL;
194 		mtx_unlock(&sc->mfi_sc->mfi_io_lock);
195 	}
196 
197 	if (sc->devq != NULL) {
198 		cam_simq_free(sc->devq);
199 		sc->devq = NULL;
200 	}
201 
202 	return (0);
203 }
204 
205 static void
206 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
207 {
208 	struct mfip_softc *sc = cam_sim_softc(sim);
209 	struct mfi_softc *mfisc = sc->mfi_sc;
210 
211 	mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
212 
213 	switch (ccb->ccb_h.func_code) {
214 	case XPT_PATH_INQ:
215 	{
216 		struct ccb_pathinq *cpi = &ccb->cpi;
217 
218 		cpi->version_num = 1;
219 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
220 		cpi->target_sprt = 0;
221 		cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
222 		cpi->hba_eng_cnt = 0;
223 		cpi->max_target = MFI_SCSI_MAX_TARGETS;
224 		cpi->max_lun = MFI_SCSI_MAX_LUNS;
225 		cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
226 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
227 		strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
228 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
229 		cpi->unit_number = cam_sim_unit(sim);
230 		cpi->bus_id = cam_sim_bus(sim);
231 		cpi->base_transfer_speed = 150000;
232 		cpi->transport = XPORT_SAS;
233 		cpi->transport_version = 0;
234 		cpi->protocol = PROTO_SCSI;
235 		cpi->protocol_version = SCSI_REV_2;
236 		cpi->ccb_h.status = CAM_REQ_CMP;
237 		break;
238 	}
239 	case XPT_RESET_BUS:
240 		ccb->ccb_h.status = CAM_REQ_CMP;
241 		break;
242 	case XPT_RESET_DEV:
243 		ccb->ccb_h.status = CAM_REQ_CMP;
244 		break;
245 	case XPT_GET_TRAN_SETTINGS:
246 	{
247 		struct ccb_trans_settings_sas *sas =
248 		    &ccb->cts.xport_specific.sas;
249 
250 		ccb->cts.protocol = PROTO_SCSI;
251 		ccb->cts.protocol_version = SCSI_REV_2;
252 		ccb->cts.transport = XPORT_SAS;
253 		ccb->cts.transport_version = 0;
254 
255 		sas->valid &= ~CTS_SAS_VALID_SPEED;
256 		sas->bitrate = 150000;
257 
258 		ccb->ccb_h.status = CAM_REQ_CMP;
259 		break;
260 	}
261 	case XPT_SET_TRAN_SETTINGS:
262 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
263 		break;
264 	case XPT_SCSI_IO:
265 	{
266 		struct ccb_hdr		*ccbh = &ccb->ccb_h;
267 		struct ccb_scsiio	*csio = &ccb->csio;
268 
269 		ccbh->status = CAM_REQ_INPROG;
270 		if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
271 			ccbh->status = CAM_REQ_INVALID;
272 			break;
273 		}
274 		ccbh->ccb_mfip_ptr = sc;
275 		TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
276 		mfi_startio(mfisc);
277 		return;
278 	}
279 	default:
280 		ccb->ccb_h.status = CAM_REQ_INVALID;
281 		break;
282 	}
283 
284 	xpt_done(ccb);
285 	return;
286 }
287 
288 static void
289 mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid)
290 {
291 	union ccb *ccb;
292 	struct mfip_softc *camsc;
293 	struct cam_sim *sim;
294 	device_t mfip_dev;
295 
296 	mtx_lock(&Giant);
297 	mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1);
298 	mtx_unlock(&Giant);
299 	if (mfip_dev == NULL) {
300 		device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n");
301 		return;
302 	}
303 
304 	mtx_lock(&sc->mfi_io_lock);
305 	camsc = device_get_softc(mfip_dev);
306 	if (camsc->state == MFIP_STATE_DETACH) {
307 		mtx_unlock(&sc->mfi_io_lock);
308 		return;
309 	}
310 	camsc->state = MFIP_STATE_RESCAN;
311 
312 	ccb = xpt_alloc_ccb_nowait();
313 	if (ccb == NULL) {
314 		mtx_unlock(&sc->mfi_io_lock);
315 		device_printf(sc->mfi_dev,
316 		    "Cannot allocate ccb for bus rescan.\n");
317 		return;
318 	}
319 
320 	sim = camsc->sim;
321 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim),
322 	    tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
323 		xpt_free_ccb(ccb);
324 		mtx_unlock(&sc->mfi_io_lock);
325 		device_printf(sc->mfi_dev,
326 		    "Cannot create path for bus rescan.\n");
327 		return;
328 	}
329 	xpt_rescan(ccb);
330 
331 	camsc->state = MFIP_STATE_NONE;
332 	mtx_unlock(&sc->mfi_io_lock);
333 }
334 
335 static struct mfi_command *
336 mfip_start(void *data)
337 {
338 	union ccb *ccb = data;
339 	struct ccb_hdr *ccbh = &ccb->ccb_h;
340 	struct ccb_scsiio *csio = &ccb->csio;
341 	struct mfip_softc *sc;
342 	struct mfi_pass_frame *pt;
343 	struct mfi_command *cm;
344 	uint32_t context = 0;
345 
346 	sc = ccbh->ccb_mfip_ptr;
347 
348 	if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
349 		return (NULL);
350 
351 	/* Zero out the MFI frame */
352 	context = cm->cm_frame->header.context;
353 	bzero(cm->cm_frame, sizeof(union mfi_frame));
354 	cm->cm_frame->header.context = context;
355 
356 	pt = &cm->cm_frame->pass;
357 	pt->header.cmd = MFI_CMD_PD_SCSI_IO;
358 	pt->header.cmd_status = 0;
359 	pt->header.scsi_status = 0;
360 	pt->header.target_id = ccbh->target_id;
361 	pt->header.lun_id = ccbh->target_lun;
362 	pt->header.flags = 0;
363 	pt->header.timeout = 0;
364 	pt->header.data_len = csio->dxfer_len;
365 	pt->header.sense_len = MFI_SENSE_LEN;
366 	pt->header.cdb_len = csio->cdb_len;
367 	pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
368 	pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
369 	if (ccbh->flags & CAM_CDB_POINTER)
370 		bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
371 	else
372 		bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
373 	cm->cm_complete = mfip_done;
374 	cm->cm_private = ccb;
375 	cm->cm_sg = &pt->sgl;
376 	cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
377 	cm->cm_data = ccb;
378 	cm->cm_len = csio->dxfer_len;
379 	switch (ccbh->flags & CAM_DIR_MASK) {
380 	case CAM_DIR_IN:
381 		cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB;
382 		break;
383 	case CAM_DIR_OUT:
384 		cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB;
385 		break;
386 	case CAM_DIR_NONE:
387 	default:
388 		cm->cm_data = NULL;
389 		cm->cm_len = 0;
390 		cm->cm_flags = 0;
391 		break;
392 	}
393 
394 	TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
395 	return (cm);
396 }
397 
398 static void
399 mfip_done(struct mfi_command *cm)
400 {
401 	union ccb *ccb = cm->cm_private;
402 	struct ccb_hdr *ccbh = &ccb->ccb_h;
403 	struct ccb_scsiio *csio = &ccb->csio;
404 	struct mfip_softc *sc;
405 	struct mfi_pass_frame *pt;
406 
407 	sc = ccbh->ccb_mfip_ptr;
408 	pt = &cm->cm_frame->pass;
409 
410 	switch (pt->header.cmd_status) {
411 	case MFI_STAT_OK:
412 	{
413 		uint8_t command, device;
414 
415 		ccbh->status = CAM_REQ_CMP;
416 		csio->scsi_status = pt->header.scsi_status;
417 		if (ccbh->flags & CAM_CDB_POINTER)
418 			command = csio->cdb_io.cdb_ptr[0];
419 		else
420 			command = csio->cdb_io.cdb_bytes[0];
421 		if (command == INQUIRY) {
422 			device = csio->data_ptr[0] & 0x1f;
423 			if ((!mfi_allow_disks && device == T_DIRECT) ||
424 			    (device == T_PROCESSOR))
425 				csio->data_ptr[0] =
426 				     (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
427 		}
428 		break;
429 	}
430 	case MFI_STAT_SCSI_DONE_WITH_ERROR:
431 	{
432 		int sense_len;
433 
434 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
435 		csio->scsi_status = pt->header.scsi_status;
436 		if (pt->header.sense_len < csio->sense_len)
437 			csio->sense_resid = csio->sense_len -
438 			    pt->header.sense_len;
439 		else
440 			csio->sense_resid = 0;
441 		sense_len = min(pt->header.sense_len,
442 		    sizeof(struct scsi_sense_data));
443 		bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
444 		bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
445 		break;
446 	}
447 	case MFI_STAT_DEVICE_NOT_FOUND:
448 		ccbh->status = CAM_SEL_TIMEOUT;
449 		break;
450 	case MFI_STAT_SCSI_IO_FAILED:
451 		ccbh->status = CAM_REQ_CMP_ERR;
452 		csio->scsi_status = pt->header.scsi_status;
453 		break;
454 	default:
455 		ccbh->status = CAM_REQ_CMP_ERR;
456 		csio->scsi_status = pt->header.scsi_status;
457 		break;
458 	}
459 
460 	mfi_release_command(cm);
461 	xpt_done(ccb);
462 }
463 
464 static void
465 mfip_cam_poll(struct cam_sim *sim)
466 {
467 	struct mfip_softc *sc = cam_sim_softc(sim);
468 	struct mfi_softc *mfisc = sc->mfi_sc;
469 
470 	mfisc->mfi_intr_ptr(mfisc);
471 }
472 
473