ctl_frontend_cam_sim.c revision 288731
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: stable/10/sys/cam/ctl/ctl_frontend_cam_sim.c 288731 2015-10-05 08:55:59Z mav $");
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
71struct cfcs_io {
72	union ccb *ccb;
73};
74
75struct 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
97int cfcs_init(void);
98static void cfcs_poll(struct cam_sim *sim);
99static void cfcs_online(void *arg);
100static void cfcs_offline(void *arg);
101static void cfcs_datamove(union ctl_io *io);
102static void cfcs_done(union ctl_io *io);
103void cfcs_action(struct cam_sim *sim, union ccb *ccb);
104static void cfcs_async(void *callback_arg, uint32_t code,
105		       struct cam_path *path, void *arg);
106
107struct 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 */
113static int cfcs_max_sense = sizeof(struct scsi_sense_data);
114
115SYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
116	    "CAM Target Layer SIM frontend");
117SYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
118           &cfcs_max_sense, 0, "Maximum sense data size");
119
120static struct ctl_frontend cfcs_frontend =
121{
122	.name = "camsim",
123	.init = cfcs_init,
124};
125CTL_FRONTEND_DECLARE(ctlcfcs, cfcs_frontend);
126
127int
128cfcs_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
240bailout:
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
251static void
252cfcs_poll(struct cam_sim *sim)
253{
254
255}
256
257static void
258cfcs_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
283bailout:
284	mtx_unlock(&softc->lock);
285}
286
287static void
288cfcs_online(void *arg)
289{
290	cfcs_onoffline(arg, /*online*/ 1);
291}
292
293static void
294cfcs_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 */
307static void
308cfcs_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
440static void
441cfcs_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
493void
494cfcs_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 = 1;
550		io->io_hdr.nexus.targ_port = softc->port.targ_port;
551		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
552		/*
553		 * This tag scheme isn't the best, since we could in theory
554		 * have a very long-lived I/O and tag collision, especially
555		 * in a high I/O environment.  But it should work well
556		 * enough for now.  Since we're using unsigned ints,
557		 * they'll just wrap around.
558		 */
559		io->scsiio.tag_num = softc->cur_tag_num++;
560		csio->tag_id = io->scsiio.tag_num;
561		switch (csio->tag_action) {
562		case CAM_TAG_ACTION_NONE:
563			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
564			break;
565		case MSG_SIMPLE_TASK:
566			io->scsiio.tag_type = CTL_TAG_SIMPLE;
567			break;
568		case MSG_HEAD_OF_QUEUE_TASK:
569        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
570			break;
571		case MSG_ORDERED_TASK:
572        		io->scsiio.tag_type = CTL_TAG_ORDERED;
573			break;
574		case MSG_ACA_TASK:
575			io->scsiio.tag_type = CTL_TAG_ACA;
576			break;
577		default:
578			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
579			printf("%s: unhandled tag type %#x!!\n", __func__,
580			       csio->tag_action);
581			break;
582		}
583		if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
584			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
585			       __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
586		}
587		io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
588		bcopy(csio->cdb_io.cdb_bytes, io->scsiio.cdb,
589		      io->scsiio.cdb_len);
590
591		ccb->ccb_h.status |= CAM_SIM_QUEUED;
592		err = ctl_queue(io);
593		if (err != CTL_RETVAL_COMPLETE) {
594			printf("%s: func %d: error %d returned by "
595			       "ctl_queue()!\n", __func__,
596			       ccb->ccb_h.func_code, err);
597			ctl_free_io(io);
598			ccb->ccb_h.status = CAM_REQ_INVALID;
599			xpt_done(ccb);
600			return;
601		}
602		break;
603	}
604	case XPT_ABORT: {
605		union ctl_io *io;
606		union ccb *abort_ccb;
607
608		abort_ccb = ccb->cab.abort_ccb;
609
610		if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
611			ccb->ccb_h.status = CAM_REQ_INVALID;
612			xpt_done(ccb);
613		}
614
615		/*
616		 * If we aren't online, there are no devices to talk to.
617		 */
618		if (softc->online == 0) {
619			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
620			xpt_done(ccb);
621			return;
622		}
623
624		io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
625		if (io == NULL) {
626			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
627			xpt_freeze_devq(ccb->ccb_h.path, 1);
628			xpt_done(ccb);
629			return;
630		}
631
632		ctl_zero_io(io);
633		/* Save pointers on both sides */
634		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
635		ccb->ccb_h.io_ptr = io;
636
637		io->io_hdr.io_type = CTL_IO_TASK;
638		io->io_hdr.nexus.initid = 1;
639		io->io_hdr.nexus.targ_port = softc->port.targ_port;
640		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
641		io->taskio.task_action = CTL_TASK_ABORT_TASK;
642		io->taskio.tag_num = abort_ccb->csio.tag_id;
643		switch (abort_ccb->csio.tag_action) {
644		case CAM_TAG_ACTION_NONE:
645			io->taskio.tag_type = CTL_TAG_UNTAGGED;
646			break;
647		case MSG_SIMPLE_TASK:
648			io->taskio.tag_type = CTL_TAG_SIMPLE;
649			break;
650		case MSG_HEAD_OF_QUEUE_TASK:
651        		io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
652			break;
653		case MSG_ORDERED_TASK:
654        		io->taskio.tag_type = CTL_TAG_ORDERED;
655			break;
656		case MSG_ACA_TASK:
657			io->taskio.tag_type = CTL_TAG_ACA;
658			break;
659		default:
660			io->taskio.tag_type = CTL_TAG_UNTAGGED;
661			printf("%s: unhandled tag type %#x!!\n", __func__,
662			       abort_ccb->csio.tag_action);
663			break;
664		}
665		err = ctl_queue(io);
666		if (err != CTL_RETVAL_COMPLETE) {
667			printf("%s func %d: error %d returned by "
668			       "ctl_queue()!\n", __func__,
669			       ccb->ccb_h.func_code, err);
670			ctl_free_io(io);
671		}
672		break;
673	}
674	case XPT_GET_TRAN_SETTINGS: {
675		struct ccb_trans_settings *cts;
676		struct ccb_trans_settings_scsi *scsi;
677		struct ccb_trans_settings_fc *fc;
678
679		cts = &ccb->cts;
680		scsi = &cts->proto_specific.scsi;
681		fc = &cts->xport_specific.fc;
682
683
684		cts->protocol = PROTO_SCSI;
685		cts->protocol_version = SCSI_REV_SPC2;
686		cts->transport = XPORT_FC;
687		cts->transport_version = 0;
688
689		scsi->valid = CTS_SCSI_VALID_TQ;
690		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
691		fc->valid = CTS_FC_VALID_SPEED;
692		fc->bitrate = 800000;
693		fc->wwnn = softc->wwnn;
694		fc->wwpn = softc->wwpn;
695		fc->port = softc->port.targ_port;
696		fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
697			CTS_FC_VALID_PORT;
698		ccb->ccb_h.status = CAM_REQ_CMP;
699		break;
700	}
701	case XPT_SET_TRAN_SETTINGS:
702		/* XXX KDM should we actually do something here? */
703		ccb->ccb_h.status = CAM_REQ_CMP;
704		break;
705	case XPT_RESET_BUS:
706	case XPT_RESET_DEV: {
707		union ctl_io *io;
708
709		/*
710		 * If we aren't online, there are no devices to talk to.
711		 */
712		if (softc->online == 0) {
713			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
714			xpt_done(ccb);
715			return;
716		}
717
718		io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
719		if (io == NULL) {
720			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
721			xpt_freeze_devq(ccb->ccb_h.path, 1);
722			xpt_done(ccb);
723			return;
724		}
725
726		ctl_zero_io(io);
727		/* Save pointers on both sides */
728		if (ccb->ccb_h.func_code == XPT_RESET_DEV)
729			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
730		ccb->ccb_h.io_ptr = io;
731
732		io->io_hdr.io_type = CTL_IO_TASK;
733		io->io_hdr.nexus.initid = 1;
734		io->io_hdr.nexus.targ_port = softc->port.targ_port;
735		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
736		if (ccb->ccb_h.func_code == XPT_RESET_BUS)
737			io->taskio.task_action = CTL_TASK_BUS_RESET;
738		else
739			io->taskio.task_action = CTL_TASK_LUN_RESET;
740
741		err = ctl_queue(io);
742		if (err != CTL_RETVAL_COMPLETE) {
743			printf("%s func %d: error %d returned by "
744			      "ctl_queue()!\n", __func__,
745			      ccb->ccb_h.func_code, err);
746			ctl_free_io(io);
747		}
748		break;
749	}
750	case XPT_CALC_GEOMETRY:
751		cam_calc_geometry(&ccb->ccg, 1);
752		xpt_done(ccb);
753		break;
754	case XPT_PATH_INQ: {
755		struct ccb_pathinq *cpi;
756
757		cpi = &ccb->cpi;
758
759		cpi->version_num = 0;
760		cpi->hba_inquiry = PI_TAG_ABLE;
761		cpi->target_sprt = 0;
762		cpi->hba_misc = 0;
763		cpi->hba_eng_cnt = 0;
764		cpi->max_target = 1;
765		cpi->max_lun = 1024;
766		/* Do we really have a limit? */
767		cpi->maxio = 1024 * 1024;
768		cpi->async_flags = 0;
769		cpi->hpath_id = 0;
770		cpi->initiator_id = 0;
771
772		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
773		strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
774		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
775		cpi->unit_number = 0;
776		cpi->bus_id = 0;
777		cpi->base_transfer_speed = 800000;
778		cpi->protocol = PROTO_SCSI;
779		cpi->protocol_version = SCSI_REV_SPC2;
780		/*
781		 * Pretend to be Fibre Channel.
782		 */
783		cpi->transport = XPORT_FC;
784		cpi->transport_version = 0;
785		cpi->xport_specific.fc.wwnn = softc->wwnn;
786		cpi->xport_specific.fc.wwpn = softc->wwpn;
787		cpi->xport_specific.fc.port = softc->port.targ_port;
788		cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
789		cpi->ccb_h.status = CAM_REQ_CMP;
790		break;
791	}
792	default:
793		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
794		printf("%s: unsupported CCB type %#x\n", __func__,
795		       ccb->ccb_h.func_code);
796		xpt_done(ccb);
797		break;
798	}
799}
800
801static void
802cfcs_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
803{
804
805}
806