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