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