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