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