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