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