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