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