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