ctl_frontend_cam_sim.c revision 275878
138816Sdfr/*-
248205Sjdp * Copyright (c) 2009 Silicon Graphics International Corp.
338816Sdfr * All rights reserved.
438816Sdfr *
538816Sdfr * Redistribution and use in source and binary forms, with or without
638816Sdfr * modification, are permitted provided that the following conditions
738816Sdfr * are met:
838816Sdfr * 1. Redistributions of source code must retain the above copyright
938816Sdfr *    notice, this list of conditions, and the following disclaimer,
1038816Sdfr *    without modification.
1138816Sdfr * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1238816Sdfr *    substantially similar to the "NO WARRANTY" disclaimer below
1338816Sdfr *    ("Disclaimer") and any redistribution must be conditioned upon
1438816Sdfr *    including a substantially similar Disclaimer requirement for further
1538816Sdfr *    binary redistribution.
1638816Sdfr *
1738816Sdfr * NO WARRANTY
1838816Sdfr * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1938816Sdfr * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2038816Sdfr * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
2138816Sdfr * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2238816Sdfr * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2338816Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2438816Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2550476Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2638816Sdfr * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2738816Sdfr * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2838816Sdfr * POSSIBILITY OF SUCH DAMAGES.
2938816Sdfr *
3038816Sdfr * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
3138816Sdfr */
3238816Sdfr/*
3338816Sdfr * CTL frontend to CAM SIM interface.  This allows access to CTL LUNs via
3438816Sdfr * the da(4) and pass(4) drivers from inside the system.
3538816Sdfr *
36133063Sdfr * Author: Ken Merry <ken@FreeBSD.org>
37133063Sdfr */
3838816Sdfr
3938816Sdfr#include <sys/cdefs.h>
4038816Sdfr__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_cam_sim.c 275878 2014-12-18 08:22:16Z mav $");
4138816Sdfr
4238816Sdfr#include <sys/param.h>
4338816Sdfr#include <sys/systm.h>
4438816Sdfr#include <sys/kernel.h>
4538816Sdfr#include <sys/types.h>
4638816Sdfr#include <sys/malloc.h>
4738816Sdfr#include <sys/lock.h>
4838816Sdfr#include <sys/mutex.h>
4938816Sdfr#include <sys/condvar.h>
5038816Sdfr#include <sys/queue.h>
51281453Skib#include <sys/bus.h>
5238816Sdfr#include <sys/sysctl.h>
5338816Sdfr#include <machine/bus.h>
5438816Sdfr#include <sys/sbuf.h>
5538816Sdfr
5638816Sdfr#include <cam/cam.h>
5738816Sdfr#include <cam/cam_ccb.h>
5838816Sdfr#include <cam/cam_sim.h>
5938816Sdfr#include <cam/cam_xpt_sim.h>
6038816Sdfr#include <cam/cam_xpt.h>
6138816Sdfr#include <cam/cam_periph.h>
6238816Sdfr#include <cam/scsi/scsi_all.h>
6338816Sdfr#include <cam/scsi/scsi_message.h>
6438816Sdfr#include <cam/ctl/ctl_io.h>
6538816Sdfr#include <cam/ctl/ctl.h>
6638816Sdfr#include <cam/ctl/ctl_frontend.h>
6738816Sdfr#include <cam/ctl/ctl_frontend_internal.h>
6838816Sdfr#include <cam/ctl/ctl_debug.h>
6938816Sdfr
7038816Sdfr#define	io_ptr		spriv_ptr1
7138816Sdfr
7238816Sdfrstruct cfcs_io {
7338816Sdfr	union ccb *ccb;
7438816Sdfr};
7538816Sdfr
7638816Sdfrstruct cfcs_softc {
77216695Skib	struct ctl_port port;
78216695Skib	char port_name[32];
79216695Skib	struct cam_sim *sim;
8038816Sdfr	struct cam_devq *devq;
8138816Sdfr	struct cam_path *path;
8238816Sdfr	struct mtx lock;
8338816Sdfr	uint64_t wwnn;
8438816Sdfr	uint64_t wwpn;
85216695Skib	uint32_t cur_tag_num;
86216695Skib	int online;
87233231Skib};
8838816Sdfr
89216695Skib/*
90216695Skib * We can't handle CCBs with these flags.  For the most part, we just don't
91216695Skib * handle physical addresses yet.  That would require mapping things in
92216695Skib * order to do the copy.
93216695Skib */
9438816Sdfr#define	CFCS_BAD_CCB_FLAGS (CAM_DATA_ISPHYS | CAM_MSG_BUF_PHYS |	\
95216695Skib	CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR |		\
96216695Skib	CAM_SENSE_PHYS)
9738816Sdfr
9838816Sdfrint cfcs_init(void);
9938816Sdfrstatic void cfcs_poll(struct cam_sim *sim);
10038816Sdfrstatic void cfcs_online(void *arg);
10138816Sdfrstatic void cfcs_offline(void *arg);
10238816Sdfrstatic int cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
10338816Sdfrstatic int cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
104216695Skibstatic void cfcs_datamove(union ctl_io *io);
10538816Sdfrstatic void cfcs_done(union ctl_io *io);
10638816Sdfrvoid cfcs_action(struct cam_sim *sim, union ccb *ccb);
10738816Sdfrstatic void cfcs_async(void *callback_arg, uint32_t code,
10838816Sdfr		       struct cam_path *path, void *arg);
10938816Sdfr
11038816Sdfrstruct cfcs_softc cfcs_softc;
11138816Sdfr/*
11245501Sjdp * This is primarly intended to allow for error injection to test the CAM
11345501Sjdp * sense data and sense residual handling code.  This sets the maximum
11445501Sjdp * amount of SCSI sense data that we will report to CAM.
11545501Sjdp */
11645501Sjdpstatic int cfcs_max_sense = sizeof(struct scsi_sense_data);
11745501Sjdp
11845501SjdpSYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
11945501Sjdp	    "CAM Target Layer SIM frontend");
12045501SjdpSYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
12145501Sjdp           &cfcs_max_sense, 0, "Maximum sense data size");
12238816Sdfr
12338816Sdfrstatic struct ctl_frontend cfcs_frontend =
124233231Skib{
125233231Skib	.name = "camsim",
12638816Sdfr	.init = cfcs_init,
12738816Sdfr};
12838816SdfrCTL_FRONTEND_DECLARE(ctlcfcs, cfcs_frontend);
12976296Sjdp
130271469Skibint
131271469Skibcfcs_init(void)
132271469Skib{
133271469Skib	struct cfcs_softc *softc;
13438816Sdfr	struct ccb_setasync csa;
135271469Skib	struct ctl_port *port;
13698100Sdillon#ifdef NEEDTOPORT
13798100Sdillon	char wwnn[8];
13898100Sdillon#endif
13998100Sdillon	int retval;
140208256Srdivacky
141271469Skib	softc = &cfcs_softc;
142271469Skib	retval = 0;
143208256Srdivacky	bzero(softc, sizeof(*softc));
144271469Skib	mtx_init(&softc->lock, "ctl2cam", NULL, MTX_DEF);
14576296Sjdp	port = &softc->port;
146271469Skib
14738816Sdfr	port->frontend = &cfcs_frontend;
148271469Skib	port->port_type = CTL_PORT_INTERNAL;
149271469Skib	/* XXX KDM what should the real number be here? */
150271469Skib	port->num_requested_ctl_io = 4096;
151271469Skib	snprintf(softc->port_name, sizeof(softc->port_name), "camsim");
152271469Skib	port->port_name = softc->port_name;
153271469Skib	port->port_online = cfcs_online;
154271469Skib	port->port_offline = cfcs_offline;
155271469Skib	port->onoff_arg = softc;
156271469Skib	port->lun_enable = cfcs_lun_enable;
157271469Skib	port->lun_disable = cfcs_lun_disable;
158271469Skib	port->targ_lun_arg = softc;
159271469Skib	port->fe_datamove = cfcs_datamove;
160271469Skib	port->fe_done = cfcs_done;
161271469Skib
162271469Skib	/* XXX KDM what should we report here? */
163271469Skib	/* XXX These should probably be fetched from CTL. */
164271469Skib	port->max_targets = 1;
165271469Skib	port->max_target_id = 15;
166271469Skib
167271469Skib	retval = ctl_port_register(port);
168271469Skib	if (retval != 0) {
169271469Skib		printf("%s: ctl_port_register() failed with error %d!\n",
170271469Skib		       __func__, retval);
171271469Skib		mtx_destroy(&softc->lock);
172271469Skib		return (retval);
173271469Skib	}
174271469Skib
175271469Skib	/*
176271469Skib	 * Get the WWNN out of the database, and create a WWPN as well.
177271469Skib	 */
178271469Skib#ifdef NEEDTOPORT
179271469Skib	ddb_GetWWNN((char *)wwnn);
180271469Skib	softc->wwnn = be64dec(wwnn);
181271469Skib	softc->wwpn = softc->wwnn + (softc->port.targ_port & 0xff);
182271469Skib#endif
183271469Skib
184271469Skib	/*
185271469Skib	 * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
186271469Skib	 * ahead and set something random.
187271469Skib	 */
188271469Skib	if (port->wwnn == 0) {
189271469Skib		uint64_t random_bits;
190271469Skib
19138816Sdfr		arc4rand(&random_bits, sizeof(random_bits), 0);
192271469Skib		softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
19338816Sdfr			/* Company ID */ 0x5000000000000000ULL |
194271469Skib			/* NL-Port */    0x0300;
195271469Skib		softc->wwpn = softc->wwnn + port->targ_port + 1;
196271469Skib		ctl_port_set_wwns(port, true, softc->wwnn, true, softc->wwpn);
197271469Skib	} else {
198271469Skib		softc->wwnn = port->wwnn;
199271469Skib		softc->wwpn = port->wwpn;
200271469Skib	}
201133063Sdfr
202271469Skib	mtx_lock(&softc->lock);
203271469Skib	softc->devq = cam_simq_alloc(port->num_requested_ctl_io);
204271469Skib	if (softc->devq == NULL) {
205133063Sdfr		printf("%s: error allocating devq\n", __func__);
206271469Skib		retval = ENOMEM;
207271469Skib		goto bailout;
208271469Skib	}
209271469Skib
210271469Skib	softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
211271469Skib				   softc, /*unit*/ 0, &softc->lock, 1,
212271469Skib				   port->num_requested_ctl_io, softc->devq);
213271469Skib	if (softc->sim == NULL) {
214271469Skib		printf("%s: error allocating SIM\n", __func__);
215271469Skib		retval = ENOMEM;
216271469Skib		goto bailout;
217271469Skib	}
218271469Skib
219271469Skib	if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
220133063Sdfr		printf("%s: error registering SIM\n", __func__);
221271469Skib		retval = ENOMEM;
222271469Skib		goto bailout;
223271469Skib	}
224271469Skib
225271469Skib	if (xpt_create_path(&softc->path, /*periph*/NULL,
226271469Skib			    cam_sim_path(softc->sim),
227271469Skib			    CAM_TARGET_WILDCARD,
228271469Skib			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
229271469Skib		printf("%s: error creating path\n", __func__);
230271469Skib		xpt_bus_deregister(cam_sim_path(softc->sim));
231271469Skib		retval = EINVAL;
232271469Skib		goto bailout;
233271469Skib	}
234271469Skib
235271469Skib	xpt_setup_ccb(&csa.ccb_h, softc->path, CAM_PRIORITY_NONE);
236271469Skib	csa.ccb_h.func_code = XPT_SASYNC_CB;
237271469Skib	csa.event_enable = AC_LOST_DEVICE;
238271469Skib	csa.callback = cfcs_async;
239271469Skib        csa.callback_arg = softc->sim;
240271469Skib        xpt_action((union ccb *)&csa);
241271469Skib
242271469Skib	mtx_unlock(&softc->lock);
243271469Skib
244271469Skib	return (retval);
245271469Skib
246271469Skibbailout:
247271469Skib	if (softc->sim)
248271469Skib		cam_sim_free(softc->sim, /*free_devq*/ TRUE);
249271469Skib	else if (softc->devq)
250271469Skib		cam_simq_free(softc->devq);
251271469Skib	mtx_unlock(&softc->lock);
252271469Skib	mtx_destroy(&softc->lock);
253271469Skib
254271469Skib	return (retval);
255271469Skib}
256271469Skib
257271469Skibstatic void
258271469Skibcfcs_poll(struct cam_sim *sim)
259271469Skib{
260271469Skib
261271469Skib}
262133063Sdfr
263133063Sdfrstatic void
26438816Sdfrcfcs_onoffline(void *arg, int online)
26598103Sdillon{
26698103Sdillon	struct cfcs_softc *softc;
267271469Skib	union ccb *ccb;
268233231Skib
26938816Sdfr	softc = (struct cfcs_softc *)arg;
27038816Sdfr
27138816Sdfr	mtx_lock(&softc->lock);
27238816Sdfr	softc->online = online;
27356780Sjdp
27438816Sdfr	ccb = xpt_alloc_ccb_nowait();
27548205Sjdp	if (ccb == NULL) {
27648205Sjdp		printf("%s: unable to allocate CCB for rescan\n", __func__);
27738816Sdfr		goto bailout;
27848205Sjdp	}
27948205Sjdp
280228435Skib	if (xpt_create_path(&ccb->ccb_h.path, NULL,
28138816Sdfr			    cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
282228435Skib			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
283228435Skib		printf("%s: can't allocate path for rescan\n", __func__);
284228435Skib		xpt_free_ccb(ccb);
285228435Skib		goto bailout;
286228435Skib	}
287228435Skib	xpt_rescan(ccb);
28838816Sdfr
289228435Skibbailout:
290228435Skib	mtx_unlock(&softc->lock);
291228435Skib}
292228435Skib
293228435Skibstatic void
294228435Skibcfcs_online(void *arg)
295228435Skib{
296228435Skib	cfcs_onoffline(arg, /*online*/ 1);
297228435Skib}
29856780Sjdp
29956780Sjdpstatic void
30056780Sjdpcfcs_offline(void *arg)
30138816Sdfr{
30256780Sjdp	cfcs_onoffline(arg, /*online*/ 0);
30356780Sjdp}
304233231Skib
30556780Sjdpstatic int
30656780Sjdpcfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
30756780Sjdp{
30848205Sjdp	return (0);
30956780Sjdp}
31056780Sjdpstatic int
31156780Sjdpcfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
31256780Sjdp{
31385004Sdfr	return (0);
31456780Sjdp}
31556780Sjdp
31656780Sjdp/*
317228435Skib * This function is very similar to ctl_ioctl_do_datamove().  Is there a
318228435Skib * way to combine the functionality?
319228435Skib *
320233231Skib * XXX KDM may need to move this into a thread.  We're doing a bcopy in the
321233231Skib * caller's context, which will usually be the backend.  That may not be a
322228435Skib * good thing.
323228435Skib */
324228435Skibstatic void
325228435Skibcfcs_datamove(union ctl_io *io)
326228435Skib{
327228435Skib	union ccb *ccb;
328228435Skib	bus_dma_segment_t cam_sg_entry, *cam_sglist;
329228435Skib	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
330228435Skib	int cam_sg_count, ctl_sg_count, cam_sg_start;
331228435Skib	int cam_sg_offset;
332228435Skib	int len_to_copy, len_copied;
333228435Skib	int ctl_watermark, cam_watermark;
334228435Skib	int i, j;
335228435Skib
336228435Skib
337228435Skib	cam_sg_offset = 0;
338228435Skib	cam_sg_start = 0;
339228435Skib
34048205Sjdp	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
341228435Skib
34256780Sjdp	/*
34338816Sdfr	 * Note that we have a check in cfcs_action() to make sure that any
34438816Sdfr	 * CCBs with "bad" flags are returned with CAM_REQ_INVALID.  This
345133063Sdfr	 * is just to make sure no one removes that check without updating
346228435Skib	 * this code to provide the additional functionality necessary to
347228435Skib	 * support those modes of operation.
348228435Skib	 */
349228435Skib	KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
350228435Skib		  "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
351228435Skib
352228435Skib	/*
353228503Skib	 * Simplify things on both sides by putting single buffers into a
354228503Skib	 * single entry S/G list.
355228435Skib	 */
356228435Skib	switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
357228435Skib	case CAM_DATA_SG: {
358228435Skib		int len_seen;
359228435Skib
360228503Skib		cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
361228503Skib		cam_sg_count = ccb->csio.sglist_cnt;
362228503Skib
363228435Skib		for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
364228435Skib			if ((len_seen + cam_sglist[i].ds_len) >=
365228435Skib			     io->scsiio.kern_rel_offset) {
366228435Skib				cam_sg_start = i;
367228503Skib				cam_sg_offset = io->scsiio.kern_rel_offset -
368228435Skib					len_seen;
369228435Skib				break;
370228435Skib			}
371228435Skib			len_seen += cam_sglist[i].ds_len;
372233231Skib		}
373228435Skib		break;
374228435Skib	}
375228435Skib	case CAM_DATA_VADDR:
376228435Skib		cam_sglist = &cam_sg_entry;
377228435Skib		cam_sglist[0].ds_len = ccb->csio.dxfer_len;
378228435Skib		cam_sglist[0].ds_addr = (bus_addr_t)ccb->csio.data_ptr;
379228435Skib		cam_sg_count = 1;
380228435Skib		cam_sg_start = 0;
381228435Skib		cam_sg_offset = io->scsiio.kern_rel_offset;
382228435Skib		break;
383228435Skib	default:
384228435Skib		panic("Invalid CAM flags %#x", ccb->ccb_h.flags);
385228435Skib	}
386228435Skib
387228435Skib	if (io->scsiio.kern_sg_entries > 0) {
388233231Skib		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
389233231Skib		ctl_sg_count = io->scsiio.kern_sg_entries;
390228435Skib	} else {
391228435Skib		ctl_sglist = &ctl_sg_entry;
392228435Skib		ctl_sglist->addr = io->scsiio.kern_data_ptr;
393228435Skib		ctl_sglist->len = io->scsiio.kern_data_len;
394228503Skib		ctl_sg_count = 1;
395228435Skib	}
396228503Skib
397228435Skib	ctl_watermark = 0;
398228435Skib	cam_watermark = cam_sg_offset;
399228435Skib	len_copied = 0;
400228435Skib	for (i = cam_sg_start, j = 0;
401228435Skib	     i < cam_sg_count && j < ctl_sg_count;) {
402228435Skib		uint8_t *cam_ptr, *ctl_ptr;
403228435Skib
404228435Skib		len_to_copy = ctl_min(cam_sglist[i].ds_len - cam_watermark,
405228435Skib				      ctl_sglist[j].len - ctl_watermark);
406133063Sdfr
407133063Sdfr		cam_ptr = (uint8_t *)cam_sglist[i].ds_addr;
408133063Sdfr		cam_ptr = cam_ptr + cam_watermark;
409133063Sdfr		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
410133063Sdfr			/*
411133063Sdfr			 * XXX KDM fix this!
412133063Sdfr			 */
413133063Sdfr			panic("need to implement bus address support");
414133063Sdfr#if 0
415133063Sdfr			kern_ptr = bus_to_virt(kern_sglist[j].addr);
416133063Sdfr#endif
417157198Sdavidxu		} else
418147673Speter			ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
419133063Sdfr		ctl_ptr = ctl_ptr + ctl_watermark;
420133063Sdfr
421133063Sdfr		ctl_watermark += len_to_copy;
422133063Sdfr		cam_watermark += len_to_copy;
423133063Sdfr
424133063Sdfr		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
425133063Sdfr		     CTL_FLAG_DATA_IN) {
426133063Sdfr			CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
427133063Sdfr					 __func__, len_to_copy));
428133063Sdfr			CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
429133063Sdfr					 __func__, cam_ptr));
430133063Sdfr			bcopy(ctl_ptr, cam_ptr, len_to_copy);
431133063Sdfr		} else {
432133063Sdfr			CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
433133063Sdfr					 __func__, len_to_copy));
434133063Sdfr			CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
435133063Sdfr					 __func__, ctl_ptr));
436133063Sdfr			bcopy(cam_ptr, ctl_ptr, len_to_copy);
437133063Sdfr		}
438133063Sdfr
439133063Sdfr		len_copied += len_to_copy;
440133063Sdfr
441		if (cam_sglist[i].ds_len == cam_watermark) {
442			i++;
443			cam_watermark = 0;
444		}
445
446		if (ctl_sglist[j].len == ctl_watermark) {
447			j++;
448			ctl_watermark = 0;
449		}
450	}
451
452	io->scsiio.ext_data_filled += len_copied;
453
454	io->scsiio.be_move_done(io);
455}
456
457static void
458cfcs_done(union ctl_io *io)
459{
460	union ccb *ccb;
461
462	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
463	if (ccb == NULL) {
464		ctl_free_io(io);
465		return;
466	}
467
468	/*
469	 * At this point we should have status.  If we don't, that's a bug.
470	 */
471	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
472		("invalid CTL status %#x", io->io_hdr.status));
473
474	/*
475	 * Translate CTL status to CAM status.
476	 */
477	switch (io->io_hdr.status & CTL_STATUS_MASK) {
478	case CTL_SUCCESS:
479		ccb->ccb_h.status = CAM_REQ_CMP;
480		break;
481	case CTL_SCSI_ERROR:
482		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
483		ccb->csio.scsi_status = io->scsiio.scsi_status;
484		bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
485		      min(io->scsiio.sense_len, ccb->csio.sense_len));
486		if (ccb->csio.sense_len > io->scsiio.sense_len)
487			ccb->csio.sense_resid = ccb->csio.sense_len -
488						io->scsiio.sense_len;
489		else
490			ccb->csio.sense_resid = 0;
491		if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
492		     cfcs_max_sense) {
493			ccb->csio.sense_resid = ccb->csio.sense_len -
494						cfcs_max_sense;
495		}
496		break;
497	case CTL_CMD_ABORTED:
498		ccb->ccb_h.status = CAM_REQ_ABORTED;
499		break;
500	case CTL_ERROR:
501	default:
502		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
503		break;
504	}
505
506	xpt_done(ccb);
507	ctl_free_io(io);
508}
509
510void
511cfcs_action(struct cam_sim *sim, union ccb *ccb)
512{
513	struct cfcs_softc *softc;
514	int err;
515
516	softc = (struct cfcs_softc *)cam_sim_softc(sim);
517	mtx_assert(&softc->lock, MA_OWNED);
518
519	switch (ccb->ccb_h.func_code) {
520	case XPT_SCSI_IO: {
521		union ctl_io *io;
522		struct ccb_scsiio *csio;
523
524		csio = &ccb->csio;
525
526		/*
527		 * Catch CCB flags, like physical address flags, that
528	 	 * indicate situations we currently can't handle.
529		 */
530		if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
531			ccb->ccb_h.status = CAM_REQ_INVALID;
532			printf("%s: bad CCB flags %#x (all flags %#x)\n",
533			       __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
534			       ccb->ccb_h.flags);
535			xpt_done(ccb);
536			return;
537		}
538
539		/*
540		 * If we aren't online, there are no devices to see.
541		 */
542		if (softc->online == 0) {
543			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
544			xpt_done(ccb);
545			return;
546		}
547
548		io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
549		if (io == NULL) {
550			printf("%s: can't allocate ctl_io\n", __func__);
551			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
552			xpt_freeze_devq(ccb->ccb_h.path, 1);
553			xpt_done(ccb);
554			return;
555		}
556		ctl_zero_io(io);
557		/* Save pointers on both sides */
558		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
559		ccb->ccb_h.io_ptr = io;
560
561		/*
562		 * Only SCSI I/O comes down this path, resets, etc. come
563		 * down via the XPT_RESET_BUS/LUN CCBs below.
564		 */
565		io->io_hdr.io_type = CTL_IO_SCSI;
566		io->io_hdr.nexus.initid.id = 1;
567		io->io_hdr.nexus.targ_port = softc->port.targ_port;
568		/*
569		 * XXX KDM how do we handle target IDs?
570		 */
571		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
572		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
573		/*
574		 * This tag scheme isn't the best, since we could in theory
575		 * have a very long-lived I/O and tag collision, especially
576		 * in a high I/O environment.  But it should work well
577		 * enough for now.  Since we're using unsigned ints,
578		 * they'll just wrap around.
579		 */
580		io->scsiio.tag_num = softc->cur_tag_num++;
581		csio->tag_id = io->scsiio.tag_num;
582		switch (csio->tag_action) {
583		case CAM_TAG_ACTION_NONE:
584			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
585			break;
586		case MSG_SIMPLE_TASK:
587			io->scsiio.tag_type = CTL_TAG_SIMPLE;
588			break;
589		case MSG_HEAD_OF_QUEUE_TASK:
590        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
591			break;
592		case MSG_ORDERED_TASK:
593        		io->scsiio.tag_type = CTL_TAG_ORDERED;
594			break;
595		case MSG_ACA_TASK:
596			io->scsiio.tag_type = CTL_TAG_ACA;
597			break;
598		default:
599			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
600			printf("%s: unhandled tag type %#x!!\n", __func__,
601			       csio->tag_action);
602			break;
603		}
604		if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
605			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
606			       __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
607		}
608		io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
609		bcopy(csio->cdb_io.cdb_bytes, io->scsiio.cdb,
610		      io->scsiio.cdb_len);
611
612		ccb->ccb_h.status |= CAM_SIM_QUEUED;
613		err = ctl_queue(io);
614		if (err != CTL_RETVAL_COMPLETE) {
615			printf("%s: func %d: error %d returned by "
616			       "ctl_queue()!\n", __func__,
617			       ccb->ccb_h.func_code, err);
618			ctl_free_io(io);
619			ccb->ccb_h.status = CAM_REQ_INVALID;
620			xpt_done(ccb);
621			return;
622		}
623		break;
624	}
625	case XPT_ABORT: {
626		union ctl_io *io;
627		union ccb *abort_ccb;
628
629		abort_ccb = ccb->cab.abort_ccb;
630
631		if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
632			ccb->ccb_h.status = CAM_REQ_INVALID;
633			xpt_done(ccb);
634		}
635
636		/*
637		 * If we aren't online, there are no devices to talk to.
638		 */
639		if (softc->online == 0) {
640			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
641			xpt_done(ccb);
642			return;
643		}
644
645		io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
646		if (io == NULL) {
647			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
648			xpt_freeze_devq(ccb->ccb_h.path, 1);
649			xpt_done(ccb);
650			return;
651		}
652
653		ctl_zero_io(io);
654		/* Save pointers on both sides */
655		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
656		ccb->ccb_h.io_ptr = io;
657
658		io->io_hdr.io_type = CTL_IO_TASK;
659		io->io_hdr.nexus.initid.id = 1;
660		io->io_hdr.nexus.targ_port = softc->port.targ_port;
661		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
662		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
663		io->taskio.task_action = CTL_TASK_ABORT_TASK;
664		io->taskio.tag_num = abort_ccb->csio.tag_id;
665		switch (abort_ccb->csio.tag_action) {
666		case CAM_TAG_ACTION_NONE:
667			io->taskio.tag_type = CTL_TAG_UNTAGGED;
668			break;
669		case MSG_SIMPLE_TASK:
670			io->taskio.tag_type = CTL_TAG_SIMPLE;
671			break;
672		case MSG_HEAD_OF_QUEUE_TASK:
673        		io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
674			break;
675		case MSG_ORDERED_TASK:
676        		io->taskio.tag_type = CTL_TAG_ORDERED;
677			break;
678		case MSG_ACA_TASK:
679			io->taskio.tag_type = CTL_TAG_ACA;
680			break;
681		default:
682			io->taskio.tag_type = CTL_TAG_UNTAGGED;
683			printf("%s: unhandled tag type %#x!!\n", __func__,
684			       abort_ccb->csio.tag_action);
685			break;
686		}
687		err = ctl_queue(io);
688		if (err != CTL_RETVAL_COMPLETE) {
689			printf("%s func %d: error %d returned by "
690			       "ctl_queue()!\n", __func__,
691			       ccb->ccb_h.func_code, err);
692			ctl_free_io(io);
693		}
694		break;
695	}
696	case XPT_GET_TRAN_SETTINGS: {
697		struct ccb_trans_settings *cts;
698		struct ccb_trans_settings_scsi *scsi;
699		struct ccb_trans_settings_fc *fc;
700
701		cts = &ccb->cts;
702		scsi = &cts->proto_specific.scsi;
703		fc = &cts->xport_specific.fc;
704
705
706		cts->protocol = PROTO_SCSI;
707		cts->protocol_version = SCSI_REV_SPC2;
708		cts->transport = XPORT_FC;
709		cts->transport_version = 0;
710
711		scsi->valid = CTS_SCSI_VALID_TQ;
712		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
713		fc->valid = CTS_FC_VALID_SPEED;
714		fc->bitrate = 800000;
715		fc->wwnn = softc->wwnn;
716		fc->wwpn = softc->wwpn;
717		fc->port = softc->port.targ_port;
718		fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
719			CTS_FC_VALID_PORT;
720		ccb->ccb_h.status = CAM_REQ_CMP;
721		break;
722	}
723	case XPT_SET_TRAN_SETTINGS:
724		/* XXX KDM should we actually do something here? */
725		ccb->ccb_h.status = CAM_REQ_CMP;
726		break;
727	case XPT_RESET_BUS:
728	case XPT_RESET_DEV: {
729		union ctl_io *io;
730
731		/*
732		 * If we aren't online, there are no devices to talk to.
733		 */
734		if (softc->online == 0) {
735			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
736			xpt_done(ccb);
737			return;
738		}
739
740		io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
741		if (io == NULL) {
742			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
743			xpt_freeze_devq(ccb->ccb_h.path, 1);
744			xpt_done(ccb);
745			return;
746		}
747
748		ctl_zero_io(io);
749		/* Save pointers on both sides */
750		if (ccb->ccb_h.func_code == XPT_RESET_DEV)
751			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
752		ccb->ccb_h.io_ptr = io;
753
754		io->io_hdr.io_type = CTL_IO_TASK;
755		io->io_hdr.nexus.initid.id = 0;
756		io->io_hdr.nexus.targ_port = softc->port.targ_port;
757		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
758		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
759		if (ccb->ccb_h.func_code == XPT_RESET_BUS)
760			io->taskio.task_action = CTL_TASK_BUS_RESET;
761		else
762			io->taskio.task_action = CTL_TASK_LUN_RESET;
763
764		err = ctl_queue(io);
765		if (err != CTL_RETVAL_COMPLETE) {
766			printf("%s func %d: error %d returned by "
767			      "ctl_queue()!\n", __func__,
768			      ccb->ccb_h.func_code, err);
769			ctl_free_io(io);
770		}
771		break;
772	}
773	case XPT_CALC_GEOMETRY:
774		cam_calc_geometry(&ccb->ccg, 1);
775		xpt_done(ccb);
776		break;
777	case XPT_PATH_INQ: {
778		struct ccb_pathinq *cpi;
779
780		cpi = &ccb->cpi;
781
782		cpi->version_num = 0;
783		cpi->hba_inquiry = PI_TAG_ABLE;
784		cpi->target_sprt = 0;
785		cpi->hba_misc = 0;
786		cpi->hba_eng_cnt = 0;
787		cpi->max_target = 1;
788		cpi->max_lun = 1024;
789		/* Do we really have a limit? */
790		cpi->maxio = 1024 * 1024;
791		cpi->async_flags = 0;
792		cpi->hpath_id = 0;
793		cpi->initiator_id = 0;
794
795		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
796		strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
797		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
798		cpi->unit_number = 0;
799		cpi->bus_id = 0;
800		cpi->base_transfer_speed = 800000;
801		cpi->protocol = PROTO_SCSI;
802		cpi->protocol_version = SCSI_REV_SPC2;
803		/*
804		 * Pretend to be Fibre Channel.
805		 */
806		cpi->transport = XPORT_FC;
807		cpi->transport_version = 0;
808		cpi->xport_specific.fc.wwnn = softc->wwnn;
809		cpi->xport_specific.fc.wwpn = softc->wwpn;
810		cpi->xport_specific.fc.port = softc->port.targ_port;
811		cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
812		cpi->ccb_h.status = CAM_REQ_CMP;
813		break;
814	}
815	default:
816		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
817		printf("%s: unsupported CCB type %#x\n", __func__,
818		       ccb->ccb_h.func_code);
819		xpt_done(ccb);
820		break;
821	}
822}
823
824static void
825cfcs_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
826{
827
828}
829