ctl_frontend_cam_sim.c revision 260387
196643Sobrien/*-
296643Sobrien * Copyright (c) 2009 Silicon Graphics International Corp.
396643Sobrien * All rights reserved.
496643Sobrien *
596643Sobrien * Redistribution and use in source and binary forms, with or without
696643Sobrien * modification, are permitted provided that the following conditions
796643Sobrien * are met:
896643Sobrien * 1. Redistributions of source code must retain the above copyright
996643Sobrien *    notice, this list of conditions, and the following disclaimer,
1096643Sobrien *    without modification.
1196643Sobrien * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1296643Sobrien *    substantially similar to the "NO WARRANTY" disclaimer below
1396643Sobrien *    ("Disclaimer") and any redistribution must be conditioned upon
1496643Sobrien *    including a substantially similar Disclaimer requirement for further
1596643Sobrien *    binary redistribution.
1696643Sobrien *
1796643Sobrien * NO WARRANTY
1896643Sobrien * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1996643Sobrien * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2096643Sobrien * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
2196643Sobrien * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2296643Sobrien * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2396643Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2496643Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2596643Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2696643Sobrien * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2796643Sobrien * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2896643Sobrien * POSSIBILITY OF SUCH DAMAGES.
2996643Sobrien *
3096643Sobrien * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
3196643Sobrien */
3296643Sobrien/*
3396643Sobrien * CTL frontend to CAM SIM interface.  This allows access to CTL LUNs via
3496643Sobrien * the da(4) and pass(4) drivers from inside the system.
3596643Sobrien *
3696643Sobrien * Author: Ken Merry <ken@FreeBSD.org>
3796643Sobrien */
3896643Sobrien
3996643Sobrien#include <sys/cdefs.h>
4096643Sobrien__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_cam_sim.c 260387 2014-01-07 01:51:48Z scottl $");
4196643Sobrien
4296643Sobrien#include <sys/param.h>
4396643Sobrien#include <sys/systm.h>
4496643Sobrien#include <sys/kernel.h>
4596643Sobrien#include <sys/types.h>
4696643Sobrien#include <sys/malloc.h>
4796643Sobrien#include <sys/lock.h>
4896643Sobrien#include <sys/mutex.h>
4996643Sobrien#include <sys/condvar.h>
5096643Sobrien#include <sys/queue.h>
5196643Sobrien#include <sys/bus.h>
5296643Sobrien#include <sys/sysctl.h>
5396643Sobrien#include <machine/bus.h>
5496643Sobrien#include <sys/sbuf.h>
5596643Sobrien
5696643Sobrien#include <cam/cam.h>
5796643Sobrien#include <cam/cam_ccb.h>
5896643Sobrien#include <cam/cam_sim.h>
5996643Sobrien#include <cam/cam_xpt_sim.h>
6096643Sobrien#include <cam/cam_xpt.h>
6196643Sobrien#include <cam/cam_periph.h>
6296643Sobrien#include <cam/scsi/scsi_all.h>
6396643Sobrien#include <cam/scsi/scsi_message.h>
6496643Sobrien#include <cam/ctl/ctl_io.h>
6596643Sobrien#include <cam/ctl/ctl.h>
6696643Sobrien#include <cam/ctl/ctl_frontend.h>
6796643Sobrien#include <cam/ctl/ctl_frontend_internal.h>
6896643Sobrien#include <cam/ctl/ctl_mem_pool.h>
6996643Sobrien#include <cam/ctl/ctl_debug.h>
7096643Sobrien
7196643Sobrien#define	io_ptr		spriv_ptr1
7296643Sobrien
7396643Sobrienstruct cfcs_io {
7496643Sobrien	union ccb *ccb;
7596643Sobrien};
7696643Sobrien
7796643Sobrienstruct cfcs_softc {
7896643Sobrien	struct ctl_frontend fe;
7996643Sobrien	char port_name[32];
8096643Sobrien	struct cam_sim *sim;
8196643Sobrien	struct cam_devq *devq;
8296643Sobrien	struct cam_path *path;
8396643Sobrien	struct mtx lock;
8496643Sobrien	char lock_desc[32];
8596643Sobrien	uint64_t wwnn;
8696643Sobrien	uint64_t wwpn;
8796643Sobrien	uint32_t cur_tag_num;
8896643Sobrien	int online;
8996643Sobrien};
9096643Sobrien
9196643Sobrien/*
9296643Sobrien * We can't handle CCBs with these flags.  For the most part, we just don't
9396643Sobrien * handle physical addresses yet.  That would require mapping things in
9496643Sobrien * order to do the copy.
9596643Sobrien */
9696643Sobrien#define	CFCS_BAD_CCB_FLAGS (CAM_DATA_ISPHYS | CAM_MSG_BUF_PHYS |	\
9796643Sobrien	CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR |		\
9896643Sobrien	CAM_SENSE_PHYS)
9996643Sobrien
10096643Sobrienint cfcs_init(void);
10196643Sobrienvoid cfcs_shutdown(void);
10296643Sobrienstatic void cfcs_poll(struct cam_sim *sim);
10396643Sobrienstatic void cfcs_online(void *arg);
10496643Sobrienstatic void cfcs_offline(void *arg);
10596643Sobrienstatic int cfcs_targ_enable(void *arg, struct ctl_id targ_id);
10696643Sobrienstatic int cfcs_targ_disable(void *arg, struct ctl_id targ_id);
10796643Sobrienstatic int cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
10896643Sobrienstatic int cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
10996643Sobrienstatic void cfcs_datamove(union ctl_io *io);
11096643Sobrienstatic void cfcs_done(union ctl_io *io);
11196643Sobrienvoid cfcs_action(struct cam_sim *sim, union ccb *ccb);
11296643Sobrienstatic void cfcs_async(void *callback_arg, uint32_t code,
11396643Sobrien		       struct cam_path *path, void *arg);
11496643Sobrien
11596643Sobrienstruct cfcs_softc cfcs_softc;
11696643Sobrien/*
11796643Sobrien * This is primarly intended to allow for error injection to test the CAM
11896643Sobrien * sense data and sense residual handling code.  This sets the maximum
11996643Sobrien * amount of SCSI sense data that we will report to CAM.
12096643Sobrien */
12196643Sobrienstatic int cfcs_max_sense = sizeof(struct scsi_sense_data);
12296643Sobrien
12396643SobrienSYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
12496643Sobrien	    "CAM Target Layer SIM frontend");
12596643SobrienSYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
12696643Sobrien           &cfcs_max_sense, 0, "Maximum sense data size");
12796643Sobrien
12896643Sobrienstatic int cfcs_module_event_handler(module_t, int /*modeventtype_t*/, void *);
12996643Sobrien
13096643Sobrienstatic moduledata_t cfcs_moduledata = {
13196643Sobrien	"ctlcfcs",
13296643Sobrien	cfcs_module_event_handler,
13396643Sobrien	NULL
13496643Sobrien};
13596643Sobrien
13696643SobrienDECLARE_MODULE(ctlcfcs, cfcs_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
13796643SobrienMODULE_VERSION(ctlcfcs, 1);
13896643SobrienMODULE_DEPEND(ctlcfi, ctl, 1, 1, 1);
13996643SobrienMODULE_DEPEND(ctlcfi, cam, 1, 1, 1);
14096643Sobrien
14196643Sobrienint
14296643Sobriencfcs_init(void)
14396643Sobrien{
14496643Sobrien	struct cfcs_softc *softc;
14596643Sobrien	struct ccb_setasync csa;
14696643Sobrien	struct ctl_frontend *fe;
14796643Sobrien#ifdef NEEDTOPORT
14896643Sobrien	char wwnn[8];
14996643Sobrien#endif
15096643Sobrien	int retval;
15196643Sobrien
15296643Sobrien	softc = &cfcs_softc;
15396643Sobrien	retval = 0;
15496643Sobrien	bzero(softc, sizeof(*softc));
15596643Sobrien	sprintf(softc->lock_desc, "ctl2cam");
15696643Sobrien	mtx_init(&softc->lock, softc->lock_desc, NULL, MTX_DEF);
15796643Sobrien	fe = &softc->fe;
15896643Sobrien
15996643Sobrien	fe->port_type = CTL_PORT_INTERNAL;
16096643Sobrien	/* XXX KDM what should the real number be here? */
16196643Sobrien	fe->num_requested_ctl_io = 4096;
16296643Sobrien	snprintf(softc->port_name, sizeof(softc->port_name), "ctl2cam");
16396643Sobrien	fe->port_name = softc->port_name;
16496643Sobrien	fe->port_online = cfcs_online;
16596643Sobrien	fe->port_offline = cfcs_offline;
16696643Sobrien	fe->onoff_arg = softc;
16796643Sobrien	fe->targ_enable = cfcs_targ_enable;
16896643Sobrien	fe->targ_disable = cfcs_targ_disable;
16996643Sobrien	fe->lun_enable = cfcs_lun_enable;
17096643Sobrien	fe->lun_disable = cfcs_lun_disable;
17196643Sobrien	fe->targ_lun_arg = softc;
17296643Sobrien	fe->fe_datamove = cfcs_datamove;
17396643Sobrien	fe->fe_done = cfcs_done;
17496643Sobrien
17596643Sobrien	/* XXX KDM what should we report here? */
176	/* XXX These should probably be fetched from CTL. */
177	fe->max_targets = 1;
178	fe->max_target_id = 15;
179
180	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
181	if (retval != 0) {
182		printf("%s: ctl_frontend_register() failed with error %d!\n",
183		       __func__, retval);
184		mtx_destroy(&softc->lock);
185		return (retval);
186	}
187
188	/*
189	 * Get the WWNN out of the database, and create a WWPN as well.
190	 */
191#ifdef NEEDTOPORT
192	ddb_GetWWNN((char *)wwnn);
193	softc->wwnn = be64dec(wwnn);
194	softc->wwpn = softc->wwnn + (softc->fe.targ_port & 0xff);
195#endif
196
197	/*
198	 * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
199	 * ahead and set something random.
200	 */
201	if (fe->wwnn == 0) {
202		uint64_t random_bits;
203
204		arc4rand(&random_bits, sizeof(random_bits), 0);
205		softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
206			/* Company ID */ 0x5000000000000000ULL |
207			/* NL-Port */    0x0300;
208		softc->wwpn = softc->wwnn + fe->targ_port + 1;
209		fe->wwnn = softc->wwnn;
210		fe->wwpn = softc->wwpn;
211	} else {
212		softc->wwnn = fe->wwnn;
213		softc->wwpn = fe->wwpn;
214	}
215
216	mtx_lock(&softc->lock);
217	softc->devq = cam_simq_alloc(fe->num_requested_ctl_io);
218	if (softc->devq == NULL) {
219		printf("%s: error allocating devq\n", __func__);
220		retval = ENOMEM;
221		goto bailout;
222	}
223
224	softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
225				   softc, /*unit*/ 0, &softc->lock, 1,
226				   fe->num_requested_ctl_io, softc->devq);
227	if (softc->sim == NULL) {
228		printf("%s: error allocating SIM\n", __func__);
229		retval = ENOMEM;
230		goto bailout;
231	}
232
233	if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
234		printf("%s: error registering SIM\n", __func__);
235		retval = ENOMEM;
236		goto bailout;
237	}
238
239	if (xpt_create_path(&softc->path, /*periph*/NULL,
240			    cam_sim_path(softc->sim),
241			    CAM_TARGET_WILDCARD,
242			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
243		printf("%s: error creating path\n", __func__);
244		xpt_bus_deregister(cam_sim_path(softc->sim));
245		retval = EINVAL;
246		goto bailout;
247	}
248
249	xpt_setup_ccb(&csa.ccb_h, softc->path, CAM_PRIORITY_NONE);
250	csa.ccb_h.func_code = XPT_SASYNC_CB;
251	csa.event_enable = AC_LOST_DEVICE;
252	csa.callback = cfcs_async;
253        csa.callback_arg = softc->sim;
254        xpt_action((union ccb *)&csa);
255
256	mtx_unlock(&softc->lock);
257
258	return (retval);
259
260bailout:
261	if (softc->sim)
262		cam_sim_free(softc->sim, /*free_devq*/ TRUE);
263	else if (softc->devq)
264		cam_simq_free(softc->devq);
265	mtx_unlock(&softc->lock);
266	mtx_destroy(&softc->lock);
267
268	return (retval);
269}
270
271static void
272cfcs_poll(struct cam_sim *sim)
273{
274
275}
276
277void
278cfcs_shutdown(void)
279{
280
281}
282
283static int
284cfcs_module_event_handler(module_t mod, int what, void *arg)
285{
286
287	switch (what) {
288	case MOD_LOAD:
289		return (cfcs_init());
290	case MOD_UNLOAD:
291		return (EBUSY);
292	default:
293		return (EOPNOTSUPP);
294	}
295}
296
297static void
298cfcs_onoffline(void *arg, int online)
299{
300	struct cfcs_softc *softc;
301	union ccb *ccb;
302
303	softc = (struct cfcs_softc *)arg;
304
305	mtx_lock(&softc->lock);
306	softc->online = online;
307
308	ccb = xpt_alloc_ccb_nowait();
309	if (ccb == NULL) {
310		printf("%s: unable to allocate CCB for rescan\n", __func__);
311		goto bailout;
312	}
313
314	if (xpt_create_path(&ccb->ccb_h.path, NULL,
315			    cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
316			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
317		printf("%s: can't allocate path for rescan\n", __func__);
318		xpt_free_ccb(ccb);
319		goto bailout;
320	}
321	xpt_rescan(ccb);
322
323bailout:
324	mtx_unlock(&softc->lock);
325}
326
327static void
328cfcs_online(void *arg)
329{
330	cfcs_onoffline(arg, /*online*/ 1);
331}
332
333static void
334cfcs_offline(void *arg)
335{
336	cfcs_onoffline(arg, /*online*/ 0);
337}
338
339static int
340cfcs_targ_enable(void *arg, struct ctl_id targ_id)
341{
342	return (0);
343}
344
345static int
346cfcs_targ_disable(void *arg, struct ctl_id targ_id)
347{
348	return (0);
349}
350
351static int
352cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
353{
354	return (0);
355}
356static int
357cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
358{
359	return (0);
360}
361
362/*
363 * This function is very similar to ctl_ioctl_do_datamove().  Is there a
364 * way to combine the functionality?
365 *
366 * XXX KDM may need to move this into a thread.  We're doing a bcopy in the
367 * caller's context, which will usually be the backend.  That may not be a
368 * good thing.
369 */
370static void
371cfcs_datamove(union ctl_io *io)
372{
373	union ccb *ccb;
374	bus_dma_segment_t cam_sg_entry, *cam_sglist;
375	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
376	int cam_sg_count, ctl_sg_count, cam_sg_start;
377	int cam_sg_offset;
378	int len_to_copy, len_copied;
379	int ctl_watermark, cam_watermark;
380	int i, j;
381
382
383	cam_sg_offset = 0;
384	cam_sg_start = 0;
385
386	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
387
388	/*
389	 * Note that we have a check in cfcs_action() to make sure that any
390	 * CCBs with "bad" flags are returned with CAM_REQ_INVALID.  This
391	 * is just to make sure no one removes that check without updating
392	 * this code to provide the additional functionality necessary to
393	 * support those modes of operation.
394	 */
395	KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
396		  "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
397
398	/*
399	 * Simplify things on both sides by putting single buffers into a
400	 * single entry S/G list.
401	 */
402	switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
403	case CAM_DATA_SG: {
404		int len_seen;
405
406		cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
407		cam_sg_count = ccb->csio.sglist_cnt;
408
409		for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
410			if ((len_seen + cam_sglist[i].ds_len) >=
411			     io->scsiio.kern_rel_offset) {
412				cam_sg_start = i;
413				cam_sg_offset = io->scsiio.kern_rel_offset -
414					len_seen;
415				break;
416			}
417			len_seen += cam_sglist[i].ds_len;
418		}
419		break;
420	}
421	case CAM_DATA_VADDR:
422		cam_sglist = &cam_sg_entry;
423		cam_sglist[0].ds_len = ccb->csio.dxfer_len;
424		cam_sglist[0].ds_addr = (bus_addr_t)ccb->csio.data_ptr;
425		cam_sg_count = 1;
426		cam_sg_start = 0;
427		cam_sg_offset = io->scsiio.kern_rel_offset;
428		break;
429	default:
430		panic("Invalid CAM flags %#x", ccb->ccb_h.flags);
431	}
432
433	if (io->scsiio.kern_sg_entries > 0) {
434		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
435		ctl_sg_count = io->scsiio.kern_sg_entries;
436	} else {
437		ctl_sglist = &ctl_sg_entry;
438		ctl_sglist->addr = io->scsiio.kern_data_ptr;
439		ctl_sglist->len = io->scsiio.kern_data_len;
440		ctl_sg_count = 1;
441	}
442
443	ctl_watermark = 0;
444	cam_watermark = cam_sg_offset;
445	len_copied = 0;
446	for (i = cam_sg_start, j = 0;
447	     i < cam_sg_count && j < ctl_sg_count;) {
448		uint8_t *cam_ptr, *ctl_ptr;
449
450		len_to_copy = ctl_min(cam_sglist[i].ds_len - cam_watermark,
451				      ctl_sglist[j].len - ctl_watermark);
452
453		cam_ptr = (uint8_t *)cam_sglist[i].ds_addr;
454		cam_ptr = cam_ptr + cam_watermark;
455		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
456			/*
457			 * XXX KDM fix this!
458			 */
459			panic("need to implement bus address support");
460#if 0
461			kern_ptr = bus_to_virt(kern_sglist[j].addr);
462#endif
463		} else
464			ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
465		ctl_ptr = ctl_ptr + ctl_watermark;
466
467		ctl_watermark += len_to_copy;
468		cam_watermark += len_to_copy;
469
470		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
471		     CTL_FLAG_DATA_IN) {
472			CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
473					 __func__, len_to_copy));
474			CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
475					 __func__, cam_ptr));
476			bcopy(ctl_ptr, cam_ptr, len_to_copy);
477		} else {
478			CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
479					 __func__, len_to_copy));
480			CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
481					 __func__, ctl_ptr));
482			bcopy(cam_ptr, ctl_ptr, len_to_copy);
483		}
484
485		len_copied += len_to_copy;
486
487		if (cam_sglist[i].ds_len == cam_watermark) {
488			i++;
489			cam_watermark = 0;
490		}
491
492		if (ctl_sglist[j].len == ctl_watermark) {
493			j++;
494			ctl_watermark = 0;
495		}
496	}
497
498	io->scsiio.ext_data_filled += len_copied;
499
500	io->scsiio.be_move_done(io);
501}
502
503static void
504cfcs_done(union ctl_io *io)
505{
506	union ccb *ccb;
507
508	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
509
510	/*
511	 * At this point we should have status.  If we don't, that's a bug.
512	 */
513	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
514		("invalid CTL status %#x", io->io_hdr.status));
515
516	/*
517	 * Translate CTL status to CAM status.
518	 */
519	switch (io->io_hdr.status & CTL_STATUS_MASK) {
520	case CTL_SUCCESS:
521		ccb->ccb_h.status = CAM_REQ_CMP;
522		break;
523	case CTL_SCSI_ERROR:
524		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
525		ccb->csio.scsi_status = io->scsiio.scsi_status;
526		bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
527		      min(io->scsiio.sense_len, ccb->csio.sense_len));
528		if (ccb->csio.sense_len > io->scsiio.sense_len)
529			ccb->csio.sense_resid = ccb->csio.sense_len -
530						io->scsiio.sense_len;
531		else
532			ccb->csio.sense_resid = 0;
533		if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
534		     cfcs_max_sense) {
535			ccb->csio.sense_resid = ccb->csio.sense_len -
536						cfcs_max_sense;
537		}
538		break;
539	case CTL_CMD_ABORTED:
540		ccb->ccb_h.status = CAM_REQ_ABORTED;
541		break;
542	case CTL_ERROR:
543	default:
544		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
545		break;
546	}
547
548	xpt_done(ccb);
549	ctl_free_io(io);
550}
551
552void
553cfcs_action(struct cam_sim *sim, union ccb *ccb)
554{
555	struct cfcs_softc *softc;
556	int err;
557
558	softc = (struct cfcs_softc *)cam_sim_softc(sim);
559	mtx_assert(&softc->lock, MA_OWNED);
560
561	switch (ccb->ccb_h.func_code) {
562	case XPT_SCSI_IO: {
563		union ctl_io *io;
564		struct ccb_scsiio *csio;
565
566		csio = &ccb->csio;
567
568		/*
569		 * Catch CCB flags, like physical address flags, that
570	 	 * indicate situations we currently can't handle.
571		 */
572		if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
573			ccb->ccb_h.status = CAM_REQ_INVALID;
574			printf("%s: bad CCB flags %#x (all flags %#x)\n",
575			       __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
576			       ccb->ccb_h.flags);
577			xpt_done(ccb);
578			return;
579		}
580
581		/*
582		 * If we aren't online, there are no devices to see.
583		 */
584		if (softc->online == 0) {
585			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
586			xpt_done(ccb);
587			return;
588		}
589
590		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
591		if (io == NULL) {
592			printf("%s: can't allocate ctl_io\n", __func__);
593			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
594			xpt_freeze_devq(ccb->ccb_h.path, 1);
595			xpt_done(ccb);
596			return;
597		}
598		ctl_zero_io(io);
599		/* Save pointers on both sides */
600		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
601		ccb->ccb_h.io_ptr = io;
602
603		/*
604		 * Only SCSI I/O comes down this path, resets, etc. come
605		 * down via the XPT_RESET_BUS/LUN CCBs below.
606		 */
607		io->io_hdr.io_type = CTL_IO_SCSI;
608		io->io_hdr.nexus.initid.id = 1;
609		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
610		/*
611		 * XXX KDM how do we handle target IDs?
612		 */
613		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
614		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
615		/*
616		 * This tag scheme isn't the best, since we could in theory
617		 * have a very long-lived I/O and tag collision, especially
618		 * in a high I/O environment.  But it should work well
619		 * enough for now.  Since we're using unsigned ints,
620		 * they'll just wrap around.
621		 */
622		io->scsiio.tag_num = softc->cur_tag_num++;
623		csio->tag_id = io->scsiio.tag_num;
624		switch (csio->tag_action) {
625		case CAM_TAG_ACTION_NONE:
626			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
627			break;
628		case MSG_SIMPLE_TASK:
629			io->scsiio.tag_type = CTL_TAG_SIMPLE;
630			break;
631		case MSG_HEAD_OF_QUEUE_TASK:
632        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
633			break;
634		case MSG_ORDERED_TASK:
635        		io->scsiio.tag_type = CTL_TAG_ORDERED;
636			break;
637		case MSG_ACA_TASK:
638			io->scsiio.tag_type = CTL_TAG_ACA;
639			break;
640		default:
641			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
642			printf("%s: unhandled tag type %#x!!\n", __func__,
643			       csio->tag_action);
644			break;
645		}
646		if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
647			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
648			       __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
649		}
650		io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
651		bcopy(csio->cdb_io.cdb_bytes, io->scsiio.cdb,
652		      io->scsiio.cdb_len);
653
654		err = ctl_queue(io);
655		if (err != CTL_RETVAL_COMPLETE) {
656			printf("%s: func %d: error %d returned by "
657			       "ctl_queue()!\n", __func__,
658			       ccb->ccb_h.func_code, err);
659			ctl_free_io(io);
660		} else {
661			ccb->ccb_h.status |= CAM_SIM_QUEUED;
662		}
663		break;
664	}
665	case XPT_ABORT: {
666		union ctl_io *io;
667		union ccb *abort_ccb;
668
669		abort_ccb = ccb->cab.abort_ccb;
670
671		if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
672			ccb->ccb_h.status = CAM_REQ_INVALID;
673			xpt_done(ccb);
674		}
675
676		/*
677		 * If we aren't online, there are no devices to talk to.
678		 */
679		if (softc->online == 0) {
680			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
681			xpt_done(ccb);
682			return;
683		}
684
685		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
686		if (io == NULL) {
687			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
688			xpt_freeze_devq(ccb->ccb_h.path, 1);
689			xpt_done(ccb);
690			return;
691		}
692
693		ctl_zero_io(io);
694		/* Save pointers on both sides */
695		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
696		ccb->ccb_h.io_ptr = io;
697
698		io->io_hdr.io_type = CTL_IO_TASK;
699		io->io_hdr.nexus.initid.id = 1;
700		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
701		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
702		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
703		io->taskio.task_action = CTL_TASK_ABORT_TASK;
704		io->taskio.tag_num = abort_ccb->csio.tag_id;
705		switch (abort_ccb->csio.tag_action) {
706		case CAM_TAG_ACTION_NONE:
707			io->taskio.tag_type = CTL_TAG_UNTAGGED;
708			break;
709		case MSG_SIMPLE_TASK:
710			io->taskio.tag_type = CTL_TAG_SIMPLE;
711			break;
712		case MSG_HEAD_OF_QUEUE_TASK:
713        		io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
714			break;
715		case MSG_ORDERED_TASK:
716        		io->taskio.tag_type = CTL_TAG_ORDERED;
717			break;
718		case MSG_ACA_TASK:
719			io->taskio.tag_type = CTL_TAG_ACA;
720			break;
721		default:
722			io->taskio.tag_type = CTL_TAG_UNTAGGED;
723			printf("%s: unhandled tag type %#x!!\n", __func__,
724			       abort_ccb->csio.tag_action);
725			break;
726		}
727		err = ctl_queue(io);
728		if (err != CTL_RETVAL_COMPLETE) {
729			printf("%s func %d: error %d returned by "
730			       "ctl_queue()!\n", __func__,
731			       ccb->ccb_h.func_code, err);
732			ctl_free_io(io);
733		}
734		break;
735	}
736	case XPT_GET_TRAN_SETTINGS: {
737		struct ccb_trans_settings *cts;
738		struct ccb_trans_settings_scsi *scsi;
739		struct ccb_trans_settings_fc *fc;
740
741		cts = &ccb->cts;
742		scsi = &cts->proto_specific.scsi;
743		fc = &cts->xport_specific.fc;
744
745
746		cts->protocol = PROTO_SCSI;
747		cts->protocol_version = SCSI_REV_SPC2;
748		cts->transport = XPORT_FC;
749		cts->transport_version = 0;
750
751		scsi->valid = CTS_SCSI_VALID_TQ;
752		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
753		fc->valid = CTS_FC_VALID_SPEED;
754		fc->bitrate = 800000;
755		fc->wwnn = softc->wwnn;
756		fc->wwpn = softc->wwpn;
757       		fc->port = softc->fe.targ_port;
758		fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
759			CTS_FC_VALID_PORT;
760		ccb->ccb_h.status = CAM_REQ_CMP;
761		break;
762	}
763	case XPT_SET_TRAN_SETTINGS:
764		/* XXX KDM should we actually do something here? */
765		ccb->ccb_h.status = CAM_REQ_CMP;
766		break;
767	case XPT_RESET_BUS:
768	case XPT_RESET_DEV: {
769		union ctl_io *io;
770
771		/*
772		 * If we aren't online, there are no devices to talk to.
773		 */
774		if (softc->online == 0) {
775			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
776			xpt_done(ccb);
777			return;
778		}
779
780		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
781		if (io == NULL) {
782			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
783			xpt_freeze_devq(ccb->ccb_h.path, 1);
784			xpt_done(ccb);
785			return;
786		}
787
788		ctl_zero_io(io);
789		/* Save pointers on both sides */
790		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
791		ccb->ccb_h.io_ptr = io;
792
793		io->io_hdr.io_type = CTL_IO_TASK;
794		io->io_hdr.nexus.initid.id = 0;
795		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
796		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
797		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
798		if (ccb->ccb_h.func_code == XPT_RESET_BUS)
799			io->taskio.task_action = CTL_TASK_BUS_RESET;
800		else
801			io->taskio.task_action = CTL_TASK_LUN_RESET;
802
803		err = ctl_queue(io);
804		if (err != CTL_RETVAL_COMPLETE) {
805			printf("%s func %d: error %d returned by "
806			      "ctl_queue()!\n", __func__,
807			      ccb->ccb_h.func_code, err);
808			ctl_free_io(io);
809		}
810		break;
811	}
812	case XPT_CALC_GEOMETRY:
813		cam_calc_geometry(&ccb->ccg, 1);
814		xpt_done(ccb);
815		break;
816	case XPT_PATH_INQ: {
817		struct ccb_pathinq *cpi;
818
819		cpi = &ccb->cpi;
820
821		cpi->version_num = 0;
822		cpi->hba_inquiry = PI_TAG_ABLE;
823		cpi->target_sprt = 0;
824		cpi->hba_misc = 0;
825		cpi->hba_eng_cnt = 0;
826		cpi->max_target = 1;
827		cpi->max_lun = 1024;
828		/* Do we really have a limit? */
829		cpi->maxio = 1024 * 1024;
830		cpi->async_flags = 0;
831		cpi->hpath_id = 0;
832		cpi->initiator_id = 0;
833
834		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
835		strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
836		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
837		cpi->unit_number = 0;
838		cpi->bus_id = 0;
839		cpi->base_transfer_speed = 800000;
840		cpi->protocol = PROTO_SCSI;
841		cpi->protocol_version = SCSI_REV_SPC2;
842		/*
843		 * Pretend to be Fibre Channel.
844		 */
845		cpi->transport = XPORT_FC;
846		cpi->transport_version = 0;
847		cpi->xport_specific.fc.wwnn = softc->wwnn;
848		cpi->xport_specific.fc.wwpn = softc->wwpn;
849		cpi->xport_specific.fc.port = softc->fe.targ_port;
850		cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
851		cpi->ccb_h.status = CAM_REQ_CMP;
852		break;
853	}
854	default:
855		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
856		printf("%s: unsupported CCB type %#x\n", __func__,
857		       ccb->ccb_h.func_code);
858		xpt_done(ccb);
859		break;
860	}
861}
862
863static void
864cfcs_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
865{
866
867}
868