xref: /freebsd/sys/dev/mfi/mfi_cam.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2007 Scott Long
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_mfi.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/selinfo.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/eventhandler.h>
43 #include <sys/rman.h>
44 #include <sys/bio.h>
45 #include <sys/ioccom.h>
46 #include <sys/uio.h>
47 #include <sys/proc.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 
61 #include <machine/md_var.h>
62 #include <machine/bus.h>
63 #include <machine/resource.h>
64 
65 #include <dev/mfi/mfireg.h>
66 #include <dev/mfi/mfi_ioctl.h>
67 #include <dev/mfi/mfivar.h>
68 
69 enum mfip_state {
70 	MFIP_STATE_NONE,
71 	MFIP_STATE_DETACH,
72 	MFIP_STATE_RESCAN
73 };
74 
75 struct mfip_softc {
76 	device_t	dev;
77 	struct mfi_softc *mfi_sc;
78 	struct cam_devq *devq;
79 	struct cam_sim	*sim;
80 	struct cam_path	*path;
81 	enum mfip_state	state;
82 };
83 
84 static int	mfip_probe(device_t);
85 static int	mfip_attach(device_t);
86 static int	mfip_detach(device_t);
87 static void	mfip_cam_action(struct cam_sim *, union ccb *);
88 static void	mfip_cam_poll(struct cam_sim *);
89 static void	mfip_cam_rescan(struct mfi_softc *, uint32_t tid);
90 static struct mfi_command * mfip_start(void *);
91 static void	mfip_done(struct mfi_command *cm);
92 
93 static int mfi_allow_disks = 0;
94 SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RDTUN,
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_TAG_ABLE;
220 		cpi->target_sprt = 0;
221 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN | PIM_UNMAPPED;
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 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
227 		strlcpy(cpi->hba_vid, "LSI", HBA_IDLEN);
228 		strlcpy(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_scsi *scsi =
248 		    &ccb->cts.proto_specific.scsi;
249 		struct ccb_trans_settings_sas *sas =
250 		    &ccb->cts.xport_specific.sas;
251 
252 		ccb->cts.protocol = PROTO_SCSI;
253 		ccb->cts.protocol_version = SCSI_REV_2;
254 		ccb->cts.transport = XPORT_SAS;
255 		ccb->cts.transport_version = 0;
256 
257 		scsi->valid = CTS_SCSI_VALID_TQ;
258 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
259 
260 		sas->valid &= ~CTS_SAS_VALID_SPEED;
261 		sas->bitrate = 150000;
262 
263 		ccb->ccb_h.status = CAM_REQ_CMP;
264 		break;
265 	}
266 	case XPT_SET_TRAN_SETTINGS:
267 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
268 		break;
269 	case XPT_SCSI_IO:
270 	{
271 		struct ccb_hdr		*ccbh = &ccb->ccb_h;
272 		struct ccb_scsiio	*csio = &ccb->csio;
273 
274 		ccbh->status = CAM_REQ_INPROG;
275 		if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
276 			ccbh->status = CAM_REQ_INVALID;
277 			break;
278 		}
279 		ccbh->ccb_mfip_ptr = sc;
280 		TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
281 		mfi_startio(mfisc);
282 		return;
283 	}
284 	default:
285 		ccb->ccb_h.status = CAM_REQ_INVALID;
286 		break;
287 	}
288 
289 	xpt_done(ccb);
290 	return;
291 }
292 
293 static void
294 mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid)
295 {
296 	union ccb *ccb;
297 	struct mfip_softc *camsc;
298 	struct cam_sim *sim;
299 	device_t mfip_dev;
300 
301 	mtx_lock(&Giant);
302 	mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1);
303 	mtx_unlock(&Giant);
304 	if (mfip_dev == NULL) {
305 		device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n");
306 		return;
307 	}
308 
309 	mtx_lock(&sc->mfi_io_lock);
310 	camsc = device_get_softc(mfip_dev);
311 	if (camsc->state == MFIP_STATE_DETACH) {
312 		mtx_unlock(&sc->mfi_io_lock);
313 		return;
314 	}
315 	camsc->state = MFIP_STATE_RESCAN;
316 
317 	ccb = xpt_alloc_ccb_nowait();
318 	if (ccb == NULL) {
319 		mtx_unlock(&sc->mfi_io_lock);
320 		device_printf(sc->mfi_dev,
321 		    "Cannot allocate ccb for bus rescan.\n");
322 		return;
323 	}
324 
325 	sim = camsc->sim;
326 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim),
327 	    tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
328 		xpt_free_ccb(ccb);
329 		mtx_unlock(&sc->mfi_io_lock);
330 		device_printf(sc->mfi_dev,
331 		    "Cannot create path for bus rescan.\n");
332 		return;
333 	}
334 	xpt_rescan(ccb);
335 
336 	camsc->state = MFIP_STATE_NONE;
337 	mtx_unlock(&sc->mfi_io_lock);
338 }
339 
340 static struct mfi_command *
341 mfip_start(void *data)
342 {
343 	union ccb *ccb = data;
344 	struct ccb_hdr *ccbh = &ccb->ccb_h;
345 	struct ccb_scsiio *csio = &ccb->csio;
346 	struct mfip_softc *sc;
347 	struct mfi_pass_frame *pt;
348 	struct mfi_command *cm;
349 	uint32_t context = 0;
350 
351 	sc = ccbh->ccb_mfip_ptr;
352 
353 	if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
354 		return (NULL);
355 
356 	/* Zero out the MFI frame */
357 	context = cm->cm_frame->header.context;
358 	bzero(cm->cm_frame, sizeof(union mfi_frame));
359 	cm->cm_frame->header.context = context;
360 
361 	pt = &cm->cm_frame->pass;
362 	pt->header.cmd = MFI_CMD_PD_SCSI_IO;
363 	pt->header.cmd_status = 0;
364 	pt->header.scsi_status = 0;
365 	pt->header.target_id = ccbh->target_id;
366 	pt->header.lun_id = ccbh->target_lun;
367 	pt->header.flags = 0;
368 	pt->header.timeout = 0;
369 	pt->header.data_len = csio->dxfer_len;
370 	pt->header.sense_len = MFI_SENSE_LEN;
371 	pt->header.cdb_len = csio->cdb_len;
372 	pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
373 	pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
374 	if (ccbh->flags & CAM_CDB_POINTER)
375 		bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
376 	else
377 		bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
378 	cm->cm_complete = mfip_done;
379 	cm->cm_private = ccb;
380 	cm->cm_sg = &pt->sgl;
381 	cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
382 	cm->cm_data = ccb;
383 	cm->cm_len = csio->dxfer_len;
384 	switch (ccbh->flags & CAM_DIR_MASK) {
385 	case CAM_DIR_IN:
386 		cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB;
387 		break;
388 	case CAM_DIR_OUT:
389 		cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB;
390 		break;
391 	case CAM_DIR_NONE:
392 	default:
393 		cm->cm_data = NULL;
394 		cm->cm_len = 0;
395 		cm->cm_flags = 0;
396 		break;
397 	}
398 
399 	TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
400 	return (cm);
401 }
402 
403 static void
404 mfip_done(struct mfi_command *cm)
405 {
406 	union ccb *ccb = cm->cm_private;
407 	struct ccb_hdr *ccbh = &ccb->ccb_h;
408 	struct ccb_scsiio *csio = &ccb->csio;
409 	struct mfip_softc *sc;
410 	struct mfi_pass_frame *pt;
411 
412 	sc = ccbh->ccb_mfip_ptr;
413 	pt = &cm->cm_frame->pass;
414 
415 	switch (pt->header.cmd_status) {
416 	case MFI_STAT_OK:
417 	{
418 		uint8_t command, device;
419 
420 		ccbh->status = CAM_REQ_CMP;
421 		csio->scsi_status = pt->header.scsi_status;
422 		if (ccbh->flags & CAM_CDB_POINTER)
423 			command = csio->cdb_io.cdb_ptr[0];
424 		else
425 			command = csio->cdb_io.cdb_bytes[0];
426 		if (command == INQUIRY) {
427 			device = csio->data_ptr[0] & 0x1f;
428 			if ((!mfi_allow_disks && device == T_DIRECT) ||
429 			    (device == T_PROCESSOR))
430 				csio->data_ptr[0] =
431 				     (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
432 		}
433 		break;
434 	}
435 	case MFI_STAT_SCSI_DONE_WITH_ERROR:
436 	{
437 		int sense_len;
438 
439 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
440 		csio->scsi_status = pt->header.scsi_status;
441 		if (pt->header.sense_len < csio->sense_len)
442 			csio->sense_resid = csio->sense_len -
443 			    pt->header.sense_len;
444 		else
445 			csio->sense_resid = 0;
446 		sense_len = min(pt->header.sense_len,
447 		    sizeof(struct scsi_sense_data));
448 		bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
449 		bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
450 		break;
451 	}
452 	case MFI_STAT_DEVICE_NOT_FOUND:
453 		ccbh->status = CAM_SEL_TIMEOUT;
454 		break;
455 	case MFI_STAT_SCSI_IO_FAILED:
456 		ccbh->status = CAM_REQ_CMP_ERR;
457 		csio->scsi_status = pt->header.scsi_status;
458 		break;
459 	default:
460 		ccbh->status = CAM_REQ_CMP_ERR;
461 		csio->scsi_status = pt->header.scsi_status;
462 		break;
463 	}
464 
465 	mfi_release_command(cm);
466 	xpt_done(ccb);
467 }
468 
469 static void
470 mfip_cam_poll(struct cam_sim *sim)
471 {
472 	struct mfip_softc *sc = cam_sim_softc(sim);
473 	struct mfi_softc *mfisc = sc->mfi_sc;
474 
475 	mfisc->mfi_intr_ptr(mfisc);
476 }
477