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