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