ctl_frontend_cam_sim.c revision 265491
1/*-
2 * Copyright (c) 2009 Silicon Graphics International Corp.
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 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    substantially similar to the "NO WARRANTY" disclaimer below
13 *    ("Disclaimer") and any redistribution must be conditioned upon
14 *    including a substantially similar Disclaimer requirement for further
15 *    binary redistribution.
16 *
17 * NO WARRANTY
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
31 */
32/*
33 * CTL frontend to CAM SIM interface.  This allows access to CTL LUNs via
34 * the da(4) and pass(4) drivers from inside the system.
35 *
36 * Author: Ken Merry <ken@FreeBSD.org>
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_cam_sim.c 265491 2014-05-07 06:20:35Z trasz $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/types.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/condvar.h>
50#include <sys/queue.h>
51#include <sys/bus.h>
52#include <sys/sysctl.h>
53#include <machine/bus.h>
54#include <sys/sbuf.h>
55
56#include <cam/cam.h>
57#include <cam/cam_ccb.h>
58#include <cam/cam_sim.h>
59#include <cam/cam_xpt_sim.h>
60#include <cam/cam_xpt.h>
61#include <cam/cam_periph.h>
62#include <cam/scsi/scsi_all.h>
63#include <cam/scsi/scsi_message.h>
64#include <cam/ctl/ctl_io.h>
65#include <cam/ctl/ctl.h>
66#include <cam/ctl/ctl_frontend.h>
67#include <cam/ctl/ctl_frontend_internal.h>
68#include <cam/ctl/ctl_debug.h>
69
70#define	io_ptr		spriv_ptr1
71
72struct cfcs_io {
73	union ccb *ccb;
74};
75
76struct cfcs_softc {
77	struct ctl_frontend fe;
78	char port_name[32];
79	struct cam_sim *sim;
80	struct cam_devq *devq;
81	struct cam_path *path;
82	struct mtx lock;
83	char lock_desc[32];
84	uint64_t wwnn;
85	uint64_t wwpn;
86	uint32_t cur_tag_num;
87	int online;
88};
89
90/*
91 * We can't handle CCBs with these flags.  For the most part, we just don't
92 * handle physical addresses yet.  That would require mapping things in
93 * order to do the copy.
94 */
95#define	CFCS_BAD_CCB_FLAGS (CAM_DATA_ISPHYS | CAM_MSG_BUF_PHYS |	\
96	CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR |		\
97	CAM_SENSE_PHYS)
98
99int cfcs_init(void);
100void cfcs_shutdown(void);
101static void cfcs_poll(struct cam_sim *sim);
102static void cfcs_online(void *arg);
103static void cfcs_offline(void *arg);
104static int cfcs_targ_enable(void *arg, struct ctl_id targ_id);
105static int cfcs_targ_disable(void *arg, struct ctl_id targ_id);
106static int cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
107static int cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
108static void cfcs_datamove(union ctl_io *io);
109static void cfcs_done(union ctl_io *io);
110void cfcs_action(struct cam_sim *sim, union ccb *ccb);
111static void cfcs_async(void *callback_arg, uint32_t code,
112		       struct cam_path *path, void *arg);
113
114struct cfcs_softc cfcs_softc;
115/*
116 * This is primarly intended to allow for error injection to test the CAM
117 * sense data and sense residual handling code.  This sets the maximum
118 * amount of SCSI sense data that we will report to CAM.
119 */
120static int cfcs_max_sense = sizeof(struct scsi_sense_data);
121
122SYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
123	    "CAM Target Layer SIM frontend");
124SYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
125           &cfcs_max_sense, 0, "Maximum sense data size");
126
127static int cfcs_module_event_handler(module_t, int /*modeventtype_t*/, void *);
128
129static moduledata_t cfcs_moduledata = {
130	"ctlcfcs",
131	cfcs_module_event_handler,
132	NULL
133};
134
135DECLARE_MODULE(ctlcfcs, cfcs_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
136MODULE_VERSION(ctlcfcs, 1);
137MODULE_DEPEND(ctlcfi, ctl, 1, 1, 1);
138MODULE_DEPEND(ctlcfi, cam, 1, 1, 1);
139
140int
141cfcs_init(void)
142{
143	struct cfcs_softc *softc;
144	struct ccb_setasync csa;
145	struct ctl_frontend *fe;
146#ifdef NEEDTOPORT
147	char wwnn[8];
148#endif
149	int retval;
150
151	softc = &cfcs_softc;
152	retval = 0;
153	bzero(softc, sizeof(*softc));
154	sprintf(softc->lock_desc, "ctl2cam");
155	mtx_init(&softc->lock, softc->lock_desc, NULL, MTX_DEF);
156	fe = &softc->fe;
157
158	fe->port_type = CTL_PORT_INTERNAL;
159	/* XXX KDM what should the real number be here? */
160	fe->num_requested_ctl_io = 4096;
161	snprintf(softc->port_name, sizeof(softc->port_name), "ctl2cam");
162	fe->port_name = softc->port_name;
163	fe->port_online = cfcs_online;
164	fe->port_offline = cfcs_offline;
165	fe->onoff_arg = softc;
166	fe->targ_enable = cfcs_targ_enable;
167	fe->targ_disable = cfcs_targ_disable;
168	fe->lun_enable = cfcs_lun_enable;
169	fe->lun_disable = cfcs_lun_disable;
170	fe->targ_lun_arg = softc;
171	fe->fe_datamove = cfcs_datamove;
172	fe->fe_done = cfcs_done;
173
174	/* XXX KDM what should we report here? */
175	/* XXX These should probably be fetched from CTL. */
176	fe->max_targets = 1;
177	fe->max_target_id = 15;
178
179	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
180	if (retval != 0) {
181		printf("%s: ctl_frontend_register() failed with error %d!\n",
182		       __func__, retval);
183		mtx_destroy(&softc->lock);
184		return (retval);
185	}
186
187	/*
188	 * Get the WWNN out of the database, and create a WWPN as well.
189	 */
190#ifdef NEEDTOPORT
191	ddb_GetWWNN((char *)wwnn);
192	softc->wwnn = be64dec(wwnn);
193	softc->wwpn = softc->wwnn + (softc->fe.targ_port & 0xff);
194#endif
195
196	/*
197	 * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
198	 * ahead and set something random.
199	 */
200	if (fe->wwnn == 0) {
201		uint64_t random_bits;
202
203		arc4rand(&random_bits, sizeof(random_bits), 0);
204		softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
205			/* Company ID */ 0x5000000000000000ULL |
206			/* NL-Port */    0x0300;
207		softc->wwpn = softc->wwnn + fe->targ_port + 1;
208		fe->wwnn = softc->wwnn;
209		fe->wwpn = softc->wwpn;
210	} else {
211		softc->wwnn = fe->wwnn;
212		softc->wwpn = fe->wwpn;
213	}
214
215	mtx_lock(&softc->lock);
216	softc->devq = cam_simq_alloc(fe->num_requested_ctl_io);
217	if (softc->devq == NULL) {
218		printf("%s: error allocating devq\n", __func__);
219		retval = ENOMEM;
220		goto bailout;
221	}
222
223	softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
224				   softc, /*unit*/ 0, &softc->lock, 1,
225				   fe->num_requested_ctl_io, softc->devq);
226	if (softc->sim == NULL) {
227		printf("%s: error allocating SIM\n", __func__);
228		retval = ENOMEM;
229		goto bailout;
230	}
231
232	if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
233		printf("%s: error registering SIM\n", __func__);
234		retval = ENOMEM;
235		goto bailout;
236	}
237
238	if (xpt_create_path(&softc->path, /*periph*/NULL,
239			    cam_sim_path(softc->sim),
240			    CAM_TARGET_WILDCARD,
241			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
242		printf("%s: error creating path\n", __func__);
243		xpt_bus_deregister(cam_sim_path(softc->sim));
244		retval = EINVAL;
245		goto bailout;
246	}
247
248	xpt_setup_ccb(&csa.ccb_h, softc->path, CAM_PRIORITY_NONE);
249	csa.ccb_h.func_code = XPT_SASYNC_CB;
250	csa.event_enable = AC_LOST_DEVICE;
251	csa.callback = cfcs_async;
252        csa.callback_arg = softc->sim;
253        xpt_action((union ccb *)&csa);
254
255	mtx_unlock(&softc->lock);
256
257	return (retval);
258
259bailout:
260	if (softc->sim)
261		cam_sim_free(softc->sim, /*free_devq*/ TRUE);
262	else if (softc->devq)
263		cam_simq_free(softc->devq);
264	mtx_unlock(&softc->lock);
265	mtx_destroy(&softc->lock);
266
267	return (retval);
268}
269
270static void
271cfcs_poll(struct cam_sim *sim)
272{
273
274}
275
276void
277cfcs_shutdown(void)
278{
279
280}
281
282static int
283cfcs_module_event_handler(module_t mod, int what, void *arg)
284{
285
286	switch (what) {
287	case MOD_LOAD:
288		return (cfcs_init());
289	case MOD_UNLOAD:
290		return (EBUSY);
291	default:
292		return (EOPNOTSUPP);
293	}
294}
295
296static void
297cfcs_onoffline(void *arg, int online)
298{
299	struct cfcs_softc *softc;
300	union ccb *ccb;
301
302	softc = (struct cfcs_softc *)arg;
303
304	mtx_lock(&softc->lock);
305	softc->online = online;
306
307	ccb = xpt_alloc_ccb_nowait();
308	if (ccb == NULL) {
309		printf("%s: unable to allocate CCB for rescan\n", __func__);
310		goto bailout;
311	}
312
313	if (xpt_create_path(&ccb->ccb_h.path, NULL,
314			    cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
315			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
316		printf("%s: can't allocate path for rescan\n", __func__);
317		xpt_free_ccb(ccb);
318		goto bailout;
319	}
320	xpt_rescan(ccb);
321
322bailout:
323	mtx_unlock(&softc->lock);
324}
325
326static void
327cfcs_online(void *arg)
328{
329	cfcs_onoffline(arg, /*online*/ 1);
330}
331
332static void
333cfcs_offline(void *arg)
334{
335	cfcs_onoffline(arg, /*online*/ 0);
336}
337
338static int
339cfcs_targ_enable(void *arg, struct ctl_id targ_id)
340{
341	return (0);
342}
343
344static int
345cfcs_targ_disable(void *arg, struct ctl_id targ_id)
346{
347	return (0);
348}
349
350static int
351cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
352{
353	return (0);
354}
355static int
356cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
357{
358	return (0);
359}
360
361/*
362 * This function is very similar to ctl_ioctl_do_datamove().  Is there a
363 * way to combine the functionality?
364 *
365 * XXX KDM may need to move this into a thread.  We're doing a bcopy in the
366 * caller's context, which will usually be the backend.  That may not be a
367 * good thing.
368 */
369static void
370cfcs_datamove(union ctl_io *io)
371{
372	union ccb *ccb;
373	bus_dma_segment_t cam_sg_entry, *cam_sglist;
374	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
375	int cam_sg_count, ctl_sg_count, cam_sg_start;
376	int cam_sg_offset;
377	int len_to_copy, len_copied;
378	int ctl_watermark, cam_watermark;
379	int i, j;
380
381
382	cam_sg_offset = 0;
383	cam_sg_start = 0;
384
385	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
386
387	/*
388	 * Note that we have a check in cfcs_action() to make sure that any
389	 * CCBs with "bad" flags are returned with CAM_REQ_INVALID.  This
390	 * is just to make sure no one removes that check without updating
391	 * this code to provide the additional functionality necessary to
392	 * support those modes of operation.
393	 */
394	KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
395		  "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
396
397	/*
398	 * Simplify things on both sides by putting single buffers into a
399	 * single entry S/G list.
400	 */
401	switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
402	case CAM_DATA_SG: {
403		int len_seen;
404
405		cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
406		cam_sg_count = ccb->csio.sglist_cnt;
407
408		for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
409			if ((len_seen + cam_sglist[i].ds_len) >=
410			     io->scsiio.kern_rel_offset) {
411				cam_sg_start = i;
412				cam_sg_offset = io->scsiio.kern_rel_offset -
413					len_seen;
414				break;
415			}
416			len_seen += cam_sglist[i].ds_len;
417		}
418		break;
419	}
420	case CAM_DATA_VADDR:
421		cam_sglist = &cam_sg_entry;
422		cam_sglist[0].ds_len = ccb->csio.dxfer_len;
423		cam_sglist[0].ds_addr = (bus_addr_t)ccb->csio.data_ptr;
424		cam_sg_count = 1;
425		cam_sg_start = 0;
426		cam_sg_offset = io->scsiio.kern_rel_offset;
427		break;
428	default:
429		panic("Invalid CAM flags %#x", ccb->ccb_h.flags);
430	}
431
432	if (io->scsiio.kern_sg_entries > 0) {
433		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
434		ctl_sg_count = io->scsiio.kern_sg_entries;
435	} else {
436		ctl_sglist = &ctl_sg_entry;
437		ctl_sglist->addr = io->scsiio.kern_data_ptr;
438		ctl_sglist->len = io->scsiio.kern_data_len;
439		ctl_sg_count = 1;
440	}
441
442	ctl_watermark = 0;
443	cam_watermark = cam_sg_offset;
444	len_copied = 0;
445	for (i = cam_sg_start, j = 0;
446	     i < cam_sg_count && j < ctl_sg_count;) {
447		uint8_t *cam_ptr, *ctl_ptr;
448
449		len_to_copy = ctl_min(cam_sglist[i].ds_len - cam_watermark,
450				      ctl_sglist[j].len - ctl_watermark);
451
452		cam_ptr = (uint8_t *)cam_sglist[i].ds_addr;
453		cam_ptr = cam_ptr + cam_watermark;
454		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
455			/*
456			 * XXX KDM fix this!
457			 */
458			panic("need to implement bus address support");
459#if 0
460			kern_ptr = bus_to_virt(kern_sglist[j].addr);
461#endif
462		} else
463			ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
464		ctl_ptr = ctl_ptr + ctl_watermark;
465
466		ctl_watermark += len_to_copy;
467		cam_watermark += len_to_copy;
468
469		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
470		     CTL_FLAG_DATA_IN) {
471			CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
472					 __func__, len_to_copy));
473			CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
474					 __func__, cam_ptr));
475			bcopy(ctl_ptr, cam_ptr, len_to_copy);
476		} else {
477			CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
478					 __func__, len_to_copy));
479			CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
480					 __func__, ctl_ptr));
481			bcopy(cam_ptr, ctl_ptr, len_to_copy);
482		}
483
484		len_copied += len_to_copy;
485
486		if (cam_sglist[i].ds_len == cam_watermark) {
487			i++;
488			cam_watermark = 0;
489		}
490
491		if (ctl_sglist[j].len == ctl_watermark) {
492			j++;
493			ctl_watermark = 0;
494		}
495	}
496
497	io->scsiio.ext_data_filled += len_copied;
498
499	io->scsiio.be_move_done(io);
500}
501
502static void
503cfcs_done(union ctl_io *io)
504{
505	union ccb *ccb;
506
507	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
508
509	/*
510	 * At this point we should have status.  If we don't, that's a bug.
511	 */
512	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
513		("invalid CTL status %#x", io->io_hdr.status));
514
515	/*
516	 * Translate CTL status to CAM status.
517	 */
518	switch (io->io_hdr.status & CTL_STATUS_MASK) {
519	case CTL_SUCCESS:
520		ccb->ccb_h.status = CAM_REQ_CMP;
521		break;
522	case CTL_SCSI_ERROR:
523		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
524		ccb->csio.scsi_status = io->scsiio.scsi_status;
525		bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
526		      min(io->scsiio.sense_len, ccb->csio.sense_len));
527		if (ccb->csio.sense_len > io->scsiio.sense_len)
528			ccb->csio.sense_resid = ccb->csio.sense_len -
529						io->scsiio.sense_len;
530		else
531			ccb->csio.sense_resid = 0;
532		if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
533		     cfcs_max_sense) {
534			ccb->csio.sense_resid = ccb->csio.sense_len -
535						cfcs_max_sense;
536		}
537		break;
538	case CTL_CMD_ABORTED:
539		ccb->ccb_h.status = CAM_REQ_ABORTED;
540		break;
541	case CTL_ERROR:
542	default:
543		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
544		break;
545	}
546
547	xpt_done(ccb);
548	ctl_free_io(io);
549}
550
551void
552cfcs_action(struct cam_sim *sim, union ccb *ccb)
553{
554	struct cfcs_softc *softc;
555	int err;
556
557	softc = (struct cfcs_softc *)cam_sim_softc(sim);
558	mtx_assert(&softc->lock, MA_OWNED);
559
560	switch (ccb->ccb_h.func_code) {
561	case XPT_SCSI_IO: {
562		union ctl_io *io;
563		struct ccb_scsiio *csio;
564
565		csio = &ccb->csio;
566
567		/*
568		 * Catch CCB flags, like physical address flags, that
569	 	 * indicate situations we currently can't handle.
570		 */
571		if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
572			ccb->ccb_h.status = CAM_REQ_INVALID;
573			printf("%s: bad CCB flags %#x (all flags %#x)\n",
574			       __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
575			       ccb->ccb_h.flags);
576			xpt_done(ccb);
577			return;
578		}
579
580		/*
581		 * If we aren't online, there are no devices to see.
582		 */
583		if (softc->online == 0) {
584			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
585			xpt_done(ccb);
586			return;
587		}
588
589		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
590		if (io == NULL) {
591			printf("%s: can't allocate ctl_io\n", __func__);
592			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
593			xpt_freeze_devq(ccb->ccb_h.path, 1);
594			xpt_done(ccb);
595			return;
596		}
597		ctl_zero_io(io);
598		/* Save pointers on both sides */
599		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
600		ccb->ccb_h.io_ptr = io;
601
602		/*
603		 * Only SCSI I/O comes down this path, resets, etc. come
604		 * down via the XPT_RESET_BUS/LUN CCBs below.
605		 */
606		io->io_hdr.io_type = CTL_IO_SCSI;
607		io->io_hdr.nexus.initid.id = 1;
608		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
609		/*
610		 * XXX KDM how do we handle target IDs?
611		 */
612		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
613		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
614		/*
615		 * This tag scheme isn't the best, since we could in theory
616		 * have a very long-lived I/O and tag collision, especially
617		 * in a high I/O environment.  But it should work well
618		 * enough for now.  Since we're using unsigned ints,
619		 * they'll just wrap around.
620		 */
621		io->scsiio.tag_num = softc->cur_tag_num++;
622		csio->tag_id = io->scsiio.tag_num;
623		switch (csio->tag_action) {
624		case CAM_TAG_ACTION_NONE:
625			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
626			break;
627		case MSG_SIMPLE_TASK:
628			io->scsiio.tag_type = CTL_TAG_SIMPLE;
629			break;
630		case MSG_HEAD_OF_QUEUE_TASK:
631        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
632			break;
633		case MSG_ORDERED_TASK:
634        		io->scsiio.tag_type = CTL_TAG_ORDERED;
635			break;
636		case MSG_ACA_TASK:
637			io->scsiio.tag_type = CTL_TAG_ACA;
638			break;
639		default:
640			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
641			printf("%s: unhandled tag type %#x!!\n", __func__,
642			       csio->tag_action);
643			break;
644		}
645		if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
646			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
647			       __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
648		}
649		io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
650		bcopy(csio->cdb_io.cdb_bytes, io->scsiio.cdb,
651		      io->scsiio.cdb_len);
652
653		err = ctl_queue(io);
654		if (err != CTL_RETVAL_COMPLETE) {
655			printf("%s: func %d: error %d returned by "
656			       "ctl_queue()!\n", __func__,
657			       ccb->ccb_h.func_code, err);
658			ctl_free_io(io);
659		} else {
660			ccb->ccb_h.status |= CAM_SIM_QUEUED;
661		}
662		break;
663	}
664	case XPT_ABORT: {
665		union ctl_io *io;
666		union ccb *abort_ccb;
667
668		abort_ccb = ccb->cab.abort_ccb;
669
670		if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
671			ccb->ccb_h.status = CAM_REQ_INVALID;
672			xpt_done(ccb);
673		}
674
675		/*
676		 * If we aren't online, there are no devices to talk to.
677		 */
678		if (softc->online == 0) {
679			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
680			xpt_done(ccb);
681			return;
682		}
683
684		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
685		if (io == NULL) {
686			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
687			xpt_freeze_devq(ccb->ccb_h.path, 1);
688			xpt_done(ccb);
689			return;
690		}
691
692		ctl_zero_io(io);
693		/* Save pointers on both sides */
694		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
695		ccb->ccb_h.io_ptr = io;
696
697		io->io_hdr.io_type = CTL_IO_TASK;
698		io->io_hdr.nexus.initid.id = 1;
699		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
700		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
701		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
702		io->taskio.task_action = CTL_TASK_ABORT_TASK;
703		io->taskio.tag_num = abort_ccb->csio.tag_id;
704		switch (abort_ccb->csio.tag_action) {
705		case CAM_TAG_ACTION_NONE:
706			io->taskio.tag_type = CTL_TAG_UNTAGGED;
707			break;
708		case MSG_SIMPLE_TASK:
709			io->taskio.tag_type = CTL_TAG_SIMPLE;
710			break;
711		case MSG_HEAD_OF_QUEUE_TASK:
712        		io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
713			break;
714		case MSG_ORDERED_TASK:
715        		io->taskio.tag_type = CTL_TAG_ORDERED;
716			break;
717		case MSG_ACA_TASK:
718			io->taskio.tag_type = CTL_TAG_ACA;
719			break;
720		default:
721			io->taskio.tag_type = CTL_TAG_UNTAGGED;
722			printf("%s: unhandled tag type %#x!!\n", __func__,
723			       abort_ccb->csio.tag_action);
724			break;
725		}
726		err = ctl_queue(io);
727		if (err != CTL_RETVAL_COMPLETE) {
728			printf("%s func %d: error %d returned by "
729			       "ctl_queue()!\n", __func__,
730			       ccb->ccb_h.func_code, err);
731			ctl_free_io(io);
732		}
733		break;
734	}
735	case XPT_GET_TRAN_SETTINGS: {
736		struct ccb_trans_settings *cts;
737		struct ccb_trans_settings_scsi *scsi;
738		struct ccb_trans_settings_fc *fc;
739
740		cts = &ccb->cts;
741		scsi = &cts->proto_specific.scsi;
742		fc = &cts->xport_specific.fc;
743
744
745		cts->protocol = PROTO_SCSI;
746		cts->protocol_version = SCSI_REV_SPC2;
747		cts->transport = XPORT_FC;
748		cts->transport_version = 0;
749
750		scsi->valid = CTS_SCSI_VALID_TQ;
751		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
752		fc->valid = CTS_FC_VALID_SPEED;
753		fc->bitrate = 800000;
754		fc->wwnn = softc->wwnn;
755		fc->wwpn = softc->wwpn;
756       		fc->port = softc->fe.targ_port;
757		fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
758			CTS_FC_VALID_PORT;
759		ccb->ccb_h.status = CAM_REQ_CMP;
760		break;
761	}
762	case XPT_SET_TRAN_SETTINGS:
763		/* XXX KDM should we actually do something here? */
764		ccb->ccb_h.status = CAM_REQ_CMP;
765		break;
766	case XPT_RESET_BUS:
767	case XPT_RESET_DEV: {
768		union ctl_io *io;
769
770		/*
771		 * If we aren't online, there are no devices to talk to.
772		 */
773		if (softc->online == 0) {
774			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
775			xpt_done(ccb);
776			return;
777		}
778
779		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
780		if (io == NULL) {
781			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
782			xpt_freeze_devq(ccb->ccb_h.path, 1);
783			xpt_done(ccb);
784			return;
785		}
786
787		ctl_zero_io(io);
788		/* Save pointers on both sides */
789		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
790		ccb->ccb_h.io_ptr = io;
791
792		io->io_hdr.io_type = CTL_IO_TASK;
793		io->io_hdr.nexus.initid.id = 0;
794		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
795		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
796		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
797		if (ccb->ccb_h.func_code == XPT_RESET_BUS)
798			io->taskio.task_action = CTL_TASK_BUS_RESET;
799		else
800			io->taskio.task_action = CTL_TASK_LUN_RESET;
801
802		err = ctl_queue(io);
803		if (err != CTL_RETVAL_COMPLETE) {
804			printf("%s func %d: error %d returned by "
805			      "ctl_queue()!\n", __func__,
806			      ccb->ccb_h.func_code, err);
807			ctl_free_io(io);
808		}
809		break;
810	}
811	case XPT_CALC_GEOMETRY:
812		cam_calc_geometry(&ccb->ccg, 1);
813		xpt_done(ccb);
814		break;
815	case XPT_PATH_INQ: {
816		struct ccb_pathinq *cpi;
817
818		cpi = &ccb->cpi;
819
820		cpi->version_num = 0;
821		cpi->hba_inquiry = PI_TAG_ABLE;
822		cpi->target_sprt = 0;
823		cpi->hba_misc = 0;
824		cpi->hba_eng_cnt = 0;
825		cpi->max_target = 1;
826		cpi->max_lun = 1024;
827		/* Do we really have a limit? */
828		cpi->maxio = 1024 * 1024;
829		cpi->async_flags = 0;
830		cpi->hpath_id = 0;
831		cpi->initiator_id = 0;
832
833		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
834		strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
835		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
836		cpi->unit_number = 0;
837		cpi->bus_id = 0;
838		cpi->base_transfer_speed = 800000;
839		cpi->protocol = PROTO_SCSI;
840		cpi->protocol_version = SCSI_REV_SPC2;
841		/*
842		 * Pretend to be Fibre Channel.
843		 */
844		cpi->transport = XPORT_FC;
845		cpi->transport_version = 0;
846		cpi->xport_specific.fc.wwnn = softc->wwnn;
847		cpi->xport_specific.fc.wwpn = softc->wwpn;
848		cpi->xport_specific.fc.port = softc->fe.targ_port;
849		cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
850		cpi->ccb_h.status = CAM_REQ_CMP;
851		break;
852	}
853	default:
854		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
855		printf("%s: unsupported CCB type %#x\n", __func__,
856		       ccb->ccb_h.func_code);
857		xpt_done(ccb);
858		break;
859	}
860}
861
862static void
863cfcs_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
864{
865
866}
867