scsi_ctl.c revision 229997
1/*-
2 * Copyright (c) 2008, 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/scsi_ctl.c#4 $
31 */
32/*
33 * Peripheral driver interface between CAM and CTL (CAM Target Layer).
34 *
35 * Author: Ken Merry <ken@FreeBSD.org>
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/cam/ctl/scsi_ctl.c 229997 2012-01-12 00:34:33Z ken $");
40
41#include <sys/param.h>
42#include <sys/queue.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/condvar.h>
48#include <sys/malloc.h>
49#include <sys/bus.h>
50#include <sys/endian.h>
51#include <sys/sbuf.h>
52#include <sys/sysctl.h>
53#include <sys/types.h>
54#include <sys/systm.h>
55#include <machine/bus.h>
56
57#include <cam/cam.h>
58#include <cam/cam_ccb.h>
59#include <cam/cam_periph.h>
60#include <cam/cam_queue.h>
61#include <cam/cam_xpt_periph.h>
62#include <cam/cam_debug.h>
63#include <cam/cam_sim.h>
64#include <cam/cam_xpt.h>
65
66#include <cam/scsi/scsi_all.h>
67#include <cam/scsi/scsi_message.h>
68
69#include <cam/ctl/ctl_io.h>
70#include <cam/ctl/ctl.h>
71#include <cam/ctl/ctl_frontend.h>
72#include <cam/ctl/ctl_util.h>
73#include <cam/ctl/ctl_error.h>
74
75typedef enum {
76	CTLFE_CCB_WAITING 	= 0x01
77} ctlfe_ccb_types;
78
79struct ctlfe_softc {
80	struct ctl_frontend fe;
81	path_id_t path_id;
82	struct cam_sim *sim;
83	char port_name[DEV_IDLEN];
84	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
85	STAILQ_ENTRY(ctlfe_softc) links;
86};
87
88STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
89struct mtx ctlfe_list_mtx;
90static char ctlfe_mtx_desc[] = "ctlfelist";
91static int ctlfe_dma_enabled = 1;
92#ifdef CTLFE_INIT_ENABLE
93static int ctlfe_max_targets = 1;
94static int ctlfe_num_targets = 0;
95#endif
96
97typedef enum {
98	CTLFE_LUN_NONE		= 0x00,
99	CTLFE_LUN_WILDCARD	= 0x01
100} ctlfe_lun_flags;
101
102struct ctlfe_lun_softc {
103	struct ctlfe_softc *parent_softc;
104	struct cam_periph *periph;
105	ctlfe_lun_flags flags;
106	struct callout dma_callout;
107	uint64_t ccbs_alloced;
108	uint64_t ccbs_freed;
109	uint64_t ctios_sent;
110	uint64_t ctios_returned;
111	uint64_t atios_sent;
112	uint64_t atios_returned;
113	uint64_t inots_sent;
114	uint64_t inots_returned;
115	/* bus_dma_tag_t dma_tag; */
116	TAILQ_HEAD(, ccb_hdr) work_queue;
117	STAILQ_ENTRY(ctlfe_lun_softc) links;
118};
119
120typedef enum {
121	CTLFE_CMD_NONE		= 0x00,
122	CTLFE_CMD_PIECEWISE	= 0x01
123} ctlfe_cmd_flags;
124
125/*
126 * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
127 * Currently that is 600 bytes.
128 */
129struct ctlfe_lun_cmd_info {
130	int cur_transfer_index;
131	ctlfe_cmd_flags flags;
132	/*
133	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
134	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
135	 * i386 and 512 bytes on amd64.
136	 */
137	bus_dma_segment_t cam_sglist[32];
138};
139
140/*
141 * When we register the adapter/bus, request that this many ctl_ios be
142 * allocated.  This should be the maximum supported by the adapter, but we
143 * currently don't have a way to get that back from the path inquiry.
144 * XXX KDM add that to the path inquiry.
145 */
146#define	CTLFE_REQ_CTL_IO	4096
147/*
148 * Number of Accept Target I/O CCBs to allocate and queue down to the
149 * adapter per LUN.
150 * XXX KDM should this be controlled by CTL?
151 */
152#define	CTLFE_ATIO_PER_LUN	1024
153/*
154 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
155 * allocate and queue down to the adapter per LUN.
156 * XXX KDM should this be controlled by CTL?
157 */
158#define	CTLFE_IN_PER_LUN	1024
159
160/*
161 * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
162 * status to the initiator.  The SIM is expected to have its own timeouts,
163 * so we're not putting this timeout around the CCB execution time.  The
164 * SIM should timeout and let us know if it has an issue.
165 */
166#define	CTLFE_DMA_TIMEOUT	60
167
168/*
169 * Turn this on to enable extra debugging prints.
170 */
171#if 0
172#define	CTLFE_DEBUG
173#endif
174
175/*
176 * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
177 * in the FreeBSD initiator that makes it unable to rescan the target if
178 * the target gets rebooted and the WWNN/WWPN stay the same.
179 */
180#if 0
181#define	RANDOM_WWNN
182#endif
183
184SYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
185	   &ctlfe_dma_enabled, 0, "DMA enabled");
186MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
187
188#define	ccb_type	ppriv_field0
189/* This is only used in the ATIO */
190#define	io_ptr		ppriv_ptr1
191
192/* This is only used in the CTIO */
193#define	ccb_atio	ppriv_ptr1
194
195int			ctlfeinitialize(void);
196void			ctlfeshutdown(void);
197static periph_init_t	ctlfeinit;
198static void		ctlfeasync(void *callback_arg, uint32_t code,
199				   struct cam_path *path, void *arg);
200static periph_ctor_t	ctlferegister;
201static periph_oninv_t	ctlfeoninvalidate;
202static periph_dtor_t	ctlfecleanup;
203static periph_start_t	ctlfestart;
204static void		ctlfedone(struct cam_periph *periph,
205				  union ccb *done_ccb);
206
207static void 		ctlfe_onoffline(void *arg, int online);
208static void 		ctlfe_online(void *arg);
209static void 		ctlfe_offline(void *arg);
210static int 		ctlfe_targ_enable(void *arg, struct ctl_id targ_id);
211static int 		ctlfe_targ_disable(void *arg, struct ctl_id targ_id);
212static int 		ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
213					 int lun_id);
214static int 		ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
215					  int lun_id);
216static void		ctlfe_dump_sim(struct cam_sim *sim);
217static void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
218static void		ctlfe_dma_timeout(void *arg);
219static void 		ctlfe_datamove_done(union ctl_io *io);
220static void 		ctlfe_dump(void);
221
222static struct periph_driver ctlfe_driver =
223{
224	ctlfeinit, "ctl",
225	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0
226};
227PERIPHDRIVER_DECLARE(ctl, ctlfe_driver);
228
229extern struct ctl_softc *control_softc;
230
231int
232ctlfeinitialize(void)
233{
234	cam_status status;
235
236	STAILQ_INIT(&ctlfe_softc_list);
237
238	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
239
240	xpt_lock_buses();
241	periphdriver_register(&ctlfe_driver);
242	xpt_unlock_buses();
243
244	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
245				    AC_CONTRACT, ctlfeasync, NULL, NULL);
246
247	if (status != CAM_REQ_CMP) {
248		printf("ctl: Failed to attach async callback due to CAM "
249		       "status 0x%x!\n", status);
250	}
251
252	return (0);
253}
254
255void
256ctlfeshutdown(void)
257{
258	return;
259}
260
261void
262ctlfeinit(void)
263{
264	cam_status status;
265
266	STAILQ_INIT(&ctlfe_softc_list);
267
268	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
269
270	KASSERT(control_softc != NULL, ("CTL is not initialized!"));
271
272	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
273				    AC_CONTRACT, ctlfeasync, NULL, NULL);
274
275	if (status != CAM_REQ_CMP) {
276		printf("ctl: Failed to attach async callback due to CAM "
277		       "status 0x%x!\n", status);
278	}
279}
280
281static void
282ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
283{
284
285#ifdef CTLFEDEBUG
286	printf("%s: entered\n", __func__);
287#endif
288
289	/*
290	 * When a new path gets registered, and it is capable of target
291	 * mode, go ahead and attach.  Later on, we may need to be more
292	 * selective, but for now this will be sufficient.
293 	 */
294	switch (code) {
295	case AC_PATH_REGISTERED: {
296		struct ctl_frontend *fe;
297		struct ctlfe_softc *bus_softc;
298		struct ctlfe_lun_softc *lun_softc;
299		struct cam_path *path;
300		struct ccb_pathinq *cpi;
301		cam_status status;
302		int retval;
303
304		cpi = (struct ccb_pathinq *)arg;
305
306		/* Don't attach if it doesn't support target mode */
307		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
308			printf("%s: SIM %s%d doesn't support target mode\n",
309			       __func__, cpi->dev_name, cpi->unit_number);
310			break;
311		}
312
313#ifdef CTLFE_INIT_ENABLE
314		if (ctlfe_num_targets >= ctlfe_max_targets) {
315			union ccb *ccb;
316			struct cam_sim *sim;
317
318			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
319						  M_NOWAIT | M_ZERO);
320			if (ccb == NULL) {
321				printf("%s: unable to malloc CCB!\n", __func__);
322				xpt_free_path(path);
323				return;
324			}
325			xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path,
326				      /*priority*/ 1);
327
328			sim = xpt_path_sim(cpi->ccb_h.path);
329
330			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
331			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
332			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
333
334			/* We should hold the SIM lock here */
335			mtx_assert(sim->mtx, MA_OWNED);
336
337			xpt_action(ccb);
338
339			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
340			     CAM_REQ_CMP) {
341				printf("%s: SIM %s%d (path id %d) initiator "
342				       "enable failed with status %#x\n",
343				       __func__, cpi->dev_name,
344				       cpi->unit_number, cpi->ccb_h.path_id,
345				       ccb->ccb_h.status);
346			} else {
347				printf("%s: SIM %s%d (path id %d) initiator "
348				       "enable succeeded\n",
349				       __func__, cpi->dev_name,
350				       cpi->unit_number, cpi->ccb_h.path_id);
351			}
352
353			free(ccb, M_TEMP);
354
355			break;
356		} else {
357			ctlfe_num_targets++;
358		}
359
360		printf("%s: ctlfe_num_targets = %d\n", __func__,
361		       ctlfe_num_targets);
362#endif /* CTLFE_INIT_ENABLE */
363
364		/*
365		 * We're in an interrupt context here, so we have to
366		 * use M_NOWAIT.  Of course this means trouble if we
367		 * can't allocate memory.
368		 */
369		bus_softc = malloc(sizeof(*bus_softc), M_CTLFE,
370				   M_NOWAIT | M_ZERO);
371		if (bus_softc == NULL) {
372			printf("%s: unable to malloc %zd bytes for softc\n",
373			       __func__, sizeof(*bus_softc));
374			return;
375		}
376
377		bus_softc->path_id = cpi->ccb_h.path_id;
378		bus_softc->sim = xpt_path_sim(cpi->ccb_h.path);
379		STAILQ_INIT(&bus_softc->lun_softc_list);
380
381		fe = &bus_softc->fe;
382
383		/*
384		 * XXX KDM should we be more accurate here ?
385		 */
386		if (cpi->transport == XPORT_FC)
387			fe->port_type = CTL_PORT_FC;
388		else
389			fe->port_type = CTL_PORT_SCSI;
390
391		/* XXX KDM what should the real number be here? */
392		fe->num_requested_ctl_io = 4096;
393		snprintf(bus_softc->port_name, sizeof(bus_softc->port_name),
394			 "%s%d", cpi->dev_name, cpi->unit_number);
395		/*
396		 * XXX KDM it would be nice to allocate storage in the
397		 * frontend structure itself.
398	 	 */
399		fe->port_name = bus_softc->port_name;
400		fe->physical_port = cpi->unit_number;
401		fe->virtual_port = cpi->bus_id;
402		fe->port_online = ctlfe_online;
403		fe->port_offline = ctlfe_offline;
404		fe->onoff_arg = bus_softc;
405		fe->targ_enable = ctlfe_targ_enable;
406		fe->targ_disable = ctlfe_targ_disable;
407		fe->lun_enable = ctlfe_lun_enable;
408		fe->lun_disable = ctlfe_lun_disable;
409		fe->targ_lun_arg = bus_softc;
410		fe->fe_datamove = ctlfe_datamove_done;
411		fe->fe_done = ctlfe_datamove_done;
412		fe->fe_dump = ctlfe_dump;
413		/*
414		 * XXX KDM the path inquiry doesn't give us the maximum
415		 * number of targets supported.
416		 */
417		fe->max_targets = cpi->max_target;
418		fe->max_target_id = cpi->max_target;
419
420		/*
421		 * XXX KDM need to figure out whether we're the master or
422		 * slave.
423		 */
424		printf("%s: calling ctl_frontend_register() for %s%d\n",
425		       __func__, cpi->dev_name, cpi->unit_number);
426		retval = ctl_frontend_register(fe, /*master_SC*/ 1);
427		if (retval != 0) {
428			printf("%s: ctl_frontend_register() failed with "
429			       "error %d!\n", __func__, retval);
430			free(bus_softc, M_CTLFE);
431			break;
432		} else {
433			mtx_lock(&ctlfe_list_mtx);
434			STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links);
435			mtx_unlock(&ctlfe_list_mtx);
436		}
437
438		status = xpt_create_path(&path, /*periph*/ NULL,
439					 bus_softc->path_id,CAM_TARGET_WILDCARD,
440					 CAM_LUN_WILDCARD);
441		if (status != CAM_REQ_CMP) {
442			printf("%s: unable to create path for wildcard "
443			       "periph\n", __func__);
444			break;
445		}
446		lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
447				   M_NOWAIT | M_ZERO);
448		if (lun_softc == NULL) {
449			xpt_print(path, "%s: unable to allocate softc for "
450				  "wildcard periph\n", __func__);
451			xpt_free_path(path);
452			break;
453		}
454
455		lun_softc->parent_softc = bus_softc;
456		lun_softc->flags |= CTLFE_LUN_WILDCARD;
457
458		status = cam_periph_alloc(ctlferegister,
459					  ctlfeoninvalidate,
460					  ctlfecleanup,
461					  ctlfestart,
462					  "ctl",
463					  CAM_PERIPH_BIO,
464					  path,
465					  ctlfeasync,
466					  0,
467					  lun_softc);
468
469		xpt_free_path(path);
470
471		break;
472	}
473	case AC_PATH_DEREGISTERED:
474		/* ctl_frontend_deregister() */
475		break;
476	case AC_CONTRACT: {
477		struct ac_contract *ac;
478
479		ac = (struct ac_contract *)arg;
480
481		switch (ac->contract_number) {
482		case AC_CONTRACT_DEV_CHG: {
483			struct ac_device_changed *dev_chg;
484			struct ctlfe_softc *softc;
485			int retval, found;
486
487			dev_chg = (struct ac_device_changed *)ac->contract_data;
488
489			printf("%s: WWPN %#jx port %u path %u target %u %s\n",
490			       __func__, dev_chg->wwpn, dev_chg->port,
491			       xpt_path_path_id(path), dev_chg->target,
492			       (dev_chg->arrived == 0) ?  "left" : "arrived");
493
494			found = 0;
495
496			mtx_lock(&ctlfe_list_mtx);
497			STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
498				if (softc->path_id == xpt_path_path_id(path)) {
499					found = 1;
500					break;
501				}
502			}
503			mtx_unlock(&ctlfe_list_mtx);
504
505			if (found == 0) {
506				printf("%s: CTL port for CAM path %u not "
507				       "found!\n", __func__,
508				       xpt_path_path_id(path));
509				break;
510			}
511			if (dev_chg->arrived != 0) {
512				retval = ctl_add_initiator(dev_chg->wwpn,
513					softc->fe.targ_port, dev_chg->target);
514			} else {
515				retval = ctl_remove_initiator(
516					softc->fe.targ_port, dev_chg->target);
517			}
518
519			if (retval != 0) {
520				printf("%s: could not %s port %d iid %u "
521				       "WWPN %#jx!\n", __func__,
522				       (dev_chg->arrived != 0) ? "add" :
523				       "remove", softc->fe.targ_port,
524				       dev_chg->target,
525				       (uintmax_t)dev_chg->wwpn);
526			}
527			break;
528		}
529		default:
530			printf("%s: unsupported contract number %ju\n",
531			       __func__, (uintmax_t)ac->contract_number);
532			break;
533		}
534		break;
535	}
536	default:
537		break;
538	}
539}
540
541static cam_status
542ctlferegister(struct cam_periph *periph, void *arg)
543{
544	struct ctlfe_softc *bus_softc;
545	struct ctlfe_lun_softc *softc;
546	struct cam_sim *sim;
547	union ccb en_lun_ccb;
548	cam_status status;
549	int i;
550
551	softc = (struct ctlfe_lun_softc *)arg;
552	bus_softc = softc->parent_softc;
553	sim = xpt_path_sim(periph->path);
554
555	TAILQ_INIT(&softc->work_queue);
556	softc->periph = periph;
557	softc->parent_softc = bus_softc;
558
559	callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0);
560	periph->softc = softc;
561
562	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, /*priority*/ 1);
563	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
564	en_lun_ccb.cel.grp6_len = 0;
565	en_lun_ccb.cel.grp7_len = 0;
566	en_lun_ccb.cel.enable = 1;
567	xpt_action(&en_lun_ccb);
568	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
569	if (status != CAM_REQ_CMP) {
570		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
571			  __func__, en_lun_ccb.ccb_h.status);
572		return (status);
573	}
574
575	status = CAM_REQ_CMP;
576
577	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
578		union ccb *new_ccb;
579
580		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
581					      M_NOWAIT);
582		if (new_ccb == NULL) {
583			status = CAM_RESRC_UNAVAIL;
584			break;
585		}
586		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
587		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
588		new_ccb->ccb_h.cbfcnp = ctlfedone;
589		xpt_action(new_ccb);
590		softc->atios_sent++;
591		status = new_ccb->ccb_h.status;
592		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
593			free(new_ccb, M_CTLFE);
594			break;
595		}
596	}
597
598	status = cam_periph_acquire(periph);
599	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
600		xpt_print(periph->path, "%s: could not acquire reference "
601			  "count, status = %#x\n", __func__, status);
602		return (status);
603	}
604
605	if (i == 0) {
606		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
607			  "status 0x%x\n", __func__, status);
608		return (CAM_REQ_CMP_ERR);
609	}
610
611	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
612		union ccb *new_ccb;
613
614		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
615					      M_NOWAIT);
616		if (new_ccb == NULL) {
617			status = CAM_RESRC_UNAVAIL;
618			break;
619		}
620
621		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
622		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
623		new_ccb->ccb_h.cbfcnp = ctlfedone;
624		xpt_action(new_ccb);
625		softc->inots_sent++;
626		status = new_ccb->ccb_h.status;
627		if (status != CAM_REQ_INPROG) {
628			free(new_ccb, M_CTLFE);
629			break;
630		}
631	}
632	if (i == 0) {
633		xpt_print(periph->path, "%s: could not allocate immediate "
634			  "notify CCBs, status 0x%x\n", __func__, status);
635		return (CAM_REQ_CMP_ERR);
636	}
637	return (CAM_REQ_CMP);
638}
639
640static void
641ctlfeoninvalidate(struct cam_periph *periph)
642{
643	union ccb en_lun_ccb;
644	cam_status status;
645	struct ctlfe_lun_softc *softc;
646
647	softc = (struct ctlfe_lun_softc *)periph->softc;
648
649	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, /*priority*/ 1);
650	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
651	en_lun_ccb.cel.grp6_len = 0;
652	en_lun_ccb.cel.grp7_len = 0;
653	en_lun_ccb.cel.enable = 0;
654	xpt_action(&en_lun_ccb);
655	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
656	if (status != CAM_REQ_CMP) {
657		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
658			  __func__, en_lun_ccb.ccb_h.status);
659		/*
660		 * XXX KDM what do we do now?
661		 */
662	}
663	xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
664		  "INOTs outstanding, %d refs\n", softc->atios_sent -
665		  softc->atios_returned, softc->inots_sent -
666		  softc->inots_returned, periph->refcount);
667}
668
669static void
670ctlfecleanup(struct cam_periph *periph)
671{
672	struct ctlfe_lun_softc *softc;
673	struct ctlfe_softc *bus_softc;
674
675	xpt_print(periph->path, "%s: Called\n", __func__);
676
677	softc = (struct ctlfe_lun_softc *)periph->softc;
678	bus_softc = softc->parent_softc;
679
680	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc,links);
681
682	/*
683	 * XXX KDM is there anything else that needs to be done here?
684	 */
685	free(softc, M_CTLFE);
686}
687
688static void
689ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
690{
691	struct ctlfe_lun_softc *softc;
692	struct ccb_hdr *ccb_h;
693
694	softc = (struct ctlfe_lun_softc *)periph->softc;
695
696	softc->ccbs_alloced++;
697
698	ccb_h = TAILQ_FIRST(&softc->work_queue);
699	if (periph->immediate_priority <= periph->pinfo.priority) {
700		panic("shouldn't get to the CCB waiting case!");
701		start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING;
702		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
703				  periph_links.sle);
704		periph->immediate_priority = CAM_PRIORITY_NONE;
705		wakeup(&periph->ccb_list);
706	} else if (ccb_h == NULL) {
707		softc->ccbs_freed++;
708		xpt_release_ccb(start_ccb);
709	} else {
710		struct ccb_accept_tio *atio;
711		struct ccb_scsiio *csio;
712		uint8_t *data_ptr;
713		uint32_t dxfer_len;
714		ccb_flags flags;
715		union ctl_io *io;
716		uint8_t scsi_status;
717
718		/* Take the ATIO off the work queue */
719		TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
720		atio = (struct ccb_accept_tio *)ccb_h;
721		io = (union ctl_io *)ccb_h->io_ptr;
722		csio = &start_ccb->csio;
723
724		flags = atio->ccb_h.flags &
725			(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
726
727		if ((io == NULL)
728		 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
729			/*
730			 * We're done, send status back.
731			 */
732			flags |= CAM_SEND_STATUS;
733			if (io == NULL) {
734				scsi_status = SCSI_STATUS_BUSY;
735				csio->sense_len = 0;
736			} else if ((io->io_hdr.status & CTL_STATUS_MASK) ==
737				   CTL_CMD_ABORTED) {
738				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
739
740				/*
741				 * If this command was aborted, we don't
742				 * need to send status back to the SIM.
743				 * Just free the CTIO and ctl_io, and
744				 * recycle the ATIO back to the SIM.
745				 */
746				xpt_print(periph->path, "%s: aborted "
747					  "command 0x%04x discarded\n",
748					  __func__, io->scsiio.tag_num);
749				ctl_free_io(io);
750				/*
751				 * For a wildcard attachment, commands can
752				 * come in with a specific target/lun.  Reset
753				 * the target and LUN fields back to the
754				 * wildcard values before we send them back
755				 * down to the SIM.  The SIM has a wildcard
756				 * LUN enabled, not whatever target/lun
757				 * these happened to be.
758				 */
759				if (softc->flags & CTLFE_LUN_WILDCARD) {
760					atio->ccb_h.target_id =
761						CAM_TARGET_WILDCARD;
762					atio->ccb_h.target_lun =
763						CAM_LUN_WILDCARD;
764				}
765
766				if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
767					cam_release_devq(periph->path,
768							 /*relsim_flags*/0,
769							 /*reduction*/0,
770 							 /*timeout*/0,
771							 /*getcount_only*/0);
772					atio->ccb_h.status &= ~CAM_DEV_QFRZN;
773				}
774
775				ccb_h = TAILQ_FIRST(&softc->work_queue);
776
777				if (atio->ccb_h.func_code !=
778				    XPT_ACCEPT_TARGET_IO) {
779					xpt_print(periph->path, "%s: func_code "
780						  "is %#x\n", __func__,
781						  atio->ccb_h.func_code);
782				}
783				start_ccb->ccb_h.func_code = XPT_ABORT;
784				start_ccb->cab.abort_ccb = (union ccb *)atio;
785				start_ccb->ccb_h.cbfcnp = ctlfedone;
786
787				/* Tell the SIM that we've aborted this ATIO */
788				xpt_action(start_ccb);
789				softc->ccbs_freed++;
790				xpt_release_ccb(start_ccb);
791
792				/*
793				 * Send the ATIO back down to the SIM.
794				 */
795				xpt_action((union ccb *)atio);
796				softc->atios_sent++;
797
798				/*
799				 * If we still have work to do, ask for
800				 * another CCB.  Otherwise, deactivate our
801				 * callout.
802				 */
803				if (ccb_h != NULL)
804					xpt_schedule(periph, /*priority*/ 1);
805				else
806					callout_stop(&softc->dma_callout);
807
808				return;
809			} else {
810				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
811				scsi_status = io->scsiio.scsi_status;
812				csio->sense_len = io->scsiio.sense_len;
813			}
814			data_ptr = NULL;
815			dxfer_len = 0;
816			if (io == NULL) {
817				printf("%s: tag %04x io is NULL\n", __func__,
818				       atio->tag_id);
819			} else {
820#ifdef CTLFEDEBUG
821				printf("%s: tag %04x status %x\n", __func__,
822				       atio->tag_id, io->io_hdr.status);
823#endif
824			}
825			csio->sglist_cnt = 0;
826			if (csio->sense_len != 0) {
827				csio->sense_data = io->scsiio.sense_data;
828				flags |= CAM_SEND_SENSE;
829			} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
830				xpt_print(periph->path, "%s: check condition "
831					  "with no sense\n", __func__);
832			}
833		} else {
834			struct ctlfe_lun_cmd_info *cmd_info;
835
836			/*
837			 * Datamove call, we need to setup the S/G list.
838			 * If we pass in a S/G list, the isp(4) driver at
839			 * least expects physical/bus addresses.
840			 */
841
842			cmd_info = (struct ctlfe_lun_cmd_info *)
843				io->io_hdr.port_priv;
844
845			KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
846				("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
847				"CTL_PORT_PRIV_SIZE %d", __func__,
848				sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
849			io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
850
851			/*
852			 * Need to zero this, in case it has been used for
853			 * a previous datamove for this particular I/O.
854			 */
855			bzero(cmd_info, sizeof(*cmd_info));
856			scsi_status = 0;
857
858			/*
859			 * Set the direction, relative to the initiator.
860			 */
861			flags &= ~CAM_DIR_MASK;
862			if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
863			     CTL_FLAG_DATA_IN)
864				flags |= CAM_DIR_IN;
865			else
866				flags |= CAM_DIR_OUT;
867
868			csio->cdb_len = atio->cdb_len;
869
870			if (io->scsiio.kern_sg_entries == 0) {
871				/* No S/G list */
872				data_ptr = io->scsiio.kern_data_ptr;
873				dxfer_len = io->scsiio.kern_data_len;
874				csio->sglist_cnt = 0;
875
876				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
877					flags |= CAM_DATA_PHYS;
878			} else if (io->scsiio.kern_sg_entries <=
879				   (sizeof(cmd_info->cam_sglist)/
880				   sizeof(cmd_info->cam_sglist[0]))) {
881				/*
882				 * S/G list with physical or virtual pointers.
883				 * Just populate the CAM S/G list with the
884				 * pointers.
885				 */
886				int i;
887				struct ctl_sg_entry *ctl_sglist;
888				bus_dma_segment_t *cam_sglist;
889
890				ctl_sglist = (struct ctl_sg_entry *)
891					io->scsiio.kern_data_ptr;
892				cam_sglist = cmd_info->cam_sglist;
893
894				for (i = 0; i < io->scsiio.kern_sg_entries;i++){
895					cam_sglist[i].ds_addr =
896						(bus_addr_t)ctl_sglist[i].addr;
897					cam_sglist[i].ds_len =
898						ctl_sglist[i].len;
899				}
900				csio->sglist_cnt = io->scsiio.kern_sg_entries;
901				flags |= CAM_SCATTER_VALID;
902				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
903					flags |= CAM_SG_LIST_PHYS;
904				else
905					flags &= ~CAM_SG_LIST_PHYS;
906				data_ptr = (uint8_t *)cam_sglist;
907				dxfer_len = io->scsiio.kern_data_len;
908			} else {
909				/* S/G list with virtual pointers */
910				struct ctl_sg_entry *sglist;
911				int *ti;
912
913				/*
914				 * XXX KDM this is a temporary hack.  The
915				 * isp(4) driver can't deal with S/G lists
916				 * with virtual pointers, so we need to
917				 * go through and send down one virtual
918				 * pointer at a time.
919				 */
920				sglist = (struct ctl_sg_entry *)
921					io->scsiio.kern_data_ptr;
922				ti = &cmd_info->cur_transfer_index;
923				data_ptr = sglist[*ti].addr;
924				dxfer_len = sglist[*ti].len;
925				csio->sglist_cnt = 0;
926				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
927				(*ti)++;
928			}
929
930			io->scsiio.ext_data_filled += dxfer_len;
931
932			if (io->scsiio.ext_data_filled >
933			    io->scsiio.kern_total_len) {
934				xpt_print(periph->path, "%s: tag 0x%04x "
935					  "fill len %u > total %u\n",
936					  __func__, io->scsiio.tag_num,
937					  io->scsiio.ext_data_filled,
938					  io->scsiio.kern_total_len);
939			}
940		}
941
942#ifdef CTLFEDEBUG
943		printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
944		       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
945		       atio->tag_id, flags, data_ptr, dxfer_len);
946#endif
947
948		/*
949		 * Valid combinations:
950		 *  - CAM_SEND_STATUS, SCATTER_VALID = 0, dxfer_len = 0,
951		 *    sglist_cnt = 0
952		 *  - CAM_SEND_STATUS = 0, SCATTER_VALID = 0, dxfer_len != 0,
953		 *    sglist_cnt = 0
954		 *  - CAM_SEND_STATUS = 0, SCATTER_VALID, dxfer_len != 0,
955		 *    sglist_cnt != 0
956		 */
957#ifdef CTLFEDEBUG
958		if (((flags & CAM_SEND_STATUS)
959		  && (((flags & CAM_SCATTER_VALID) != 0)
960		   || (dxfer_len != 0)
961		   || (csio->sglist_cnt != 0)))
962		 || (((flags & CAM_SEND_STATUS) == 0)
963		  && (dxfer_len == 0))
964		 || ((flags & CAM_SCATTER_VALID)
965		  && (csio->sglist_cnt == 0))
966		 || (((flags & CAM_SCATTER_VALID) == 0)
967		  && (csio->sglist_cnt != 0))) {
968			printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
969			       "%d sg %u\n", __func__, atio->tag_id,
970			       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
971			       csio->sglist_cnt);
972			if (io != NULL) {
973				printf("%s: tag %04x io status %#x\n", __func__,
974				       atio->tag_id, io->io_hdr.status);
975			} else {
976				printf("%s: tag %04x no associated io\n",
977				       __func__, atio->tag_id);
978			}
979		}
980#endif
981		cam_fill_ctio(csio,
982			      /*retries*/ 2,
983			      ctlfedone,
984			      flags,
985			      (flags & CAM_TAG_ACTION_VALID) ?
986			       MSG_SIMPLE_Q_TAG : 0,
987			      atio->tag_id,
988			      atio->init_id,
989			      scsi_status,
990			      /*data_ptr*/ data_ptr,
991			      /*dxfer_len*/ dxfer_len,
992			      /*timeout*/ 5 * 1000);
993		start_ccb->ccb_h.ccb_atio = atio;
994		if (((flags & CAM_SEND_STATUS) == 0)
995		 && (io != NULL))
996			io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
997
998		softc->ctios_sent++;
999
1000		xpt_action(start_ccb);
1001
1002		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1003			cam_release_devq(periph->path,
1004					 /*relsim_flags*/0,
1005					 /*reduction*/0,
1006 					 /*timeout*/0,
1007					 /*getcount_only*/0);
1008			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
1009		}
1010
1011		ccb_h = TAILQ_FIRST(&softc->work_queue);
1012	}
1013	/*
1014	 * If we still have work to do, ask for another CCB.  Otherwise,
1015	 * deactivate our callout.
1016	 */
1017	if (ccb_h != NULL)
1018		xpt_schedule(periph, /*priority*/ 1);
1019	else
1020		callout_stop(&softc->dma_callout);
1021}
1022
1023static void
1024ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
1025{
1026	struct ctlfe_lun_softc *softc;
1027
1028	softc = (struct ctlfe_lun_softc *)periph->softc;
1029
1030	switch (ccb->ccb_h.func_code) {
1031	case XPT_ACCEPT_TARGET_IO:
1032		softc->atios_returned++;
1033		break;
1034	case XPT_IMMEDIATE_NOTIFY:
1035	case XPT_NOTIFY_ACKNOWLEDGE:
1036		softc->inots_returned++;
1037		break;
1038	default:
1039		break;
1040	}
1041
1042	free(ccb, M_CTLFE);
1043
1044	KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1045		"atios_returned %ju > atios_sent %ju", __func__,
1046		softc->atios_returned, softc->atios_sent));
1047	KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1048		"inots_returned %ju > inots_sent %ju", __func__,
1049		softc->inots_returned, softc->inots_sent));
1050
1051	/*
1052	 * If we have received all of our CCBs, we can release our
1053	 * reference on the peripheral driver.  It will probably go away
1054	 * now.
1055	 */
1056	if ((softc->atios_returned == softc->atios_sent)
1057	 && (softc->inots_returned == softc->inots_sent)) {
1058		cam_periph_release_locked(periph);
1059	}
1060}
1061
1062static void
1063ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1064{
1065	struct ctlfe_lun_softc *softc;
1066	struct ctlfe_softc *bus_softc;
1067
1068#ifdef CTLFE_DEBUG
1069	printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
1070	       done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
1071#endif
1072
1073	softc = (struct ctlfe_lun_softc *)periph->softc;
1074	bus_softc = softc->parent_softc;
1075
1076	if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) {
1077		panic("shouldn't get to the CCB waiting case!");
1078		wakeup(&done_ccb->ccb_h.cbfcnp);
1079		return;
1080	}
1081
1082	/*
1083	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1084	 * need to be freed.  Most of the ATIOs and INOTs that come back
1085	 * will be CCBs that are being returned from the SIM as a result of
1086	 * our disabling the LUN.
1087	 *
1088	 * Other CCB types are handled in their respective cases below.
1089	 */
1090	if (periph->flags & CAM_PERIPH_INVALID) {
1091		switch (done_ccb->ccb_h.func_code) {
1092		case XPT_ACCEPT_TARGET_IO:
1093		case XPT_IMMEDIATE_NOTIFY:
1094		case XPT_NOTIFY_ACKNOWLEDGE:
1095			ctlfe_free_ccb(periph, done_ccb);
1096			return;
1097		default:
1098			break;
1099		}
1100
1101	}
1102	switch (done_ccb->ccb_h.func_code) {
1103	case XPT_ACCEPT_TARGET_IO: {
1104		union ctl_io *io;
1105		struct ccb_accept_tio *atio;
1106
1107		atio = &done_ccb->atio;
1108
1109		softc->atios_returned++;
1110
1111		/*
1112		 * Allocate a ctl_io, pass it to CTL, and wait for the
1113		 * datamove or done.
1114		 */
1115		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1116		if (io == NULL) {
1117			atio->ccb_h.flags &= ~CAM_DIR_MASK;
1118			atio->ccb_h.flags |= CAM_DIR_NONE;
1119
1120			printf("%s: ctl_alloc_io failed!\n", __func__);
1121
1122			/*
1123			 * XXX KDM need to set SCSI_STATUS_BUSY, but there
1124			 * is no field in the ATIO structure to do that,
1125			 * and we aren't able to allocate a ctl_io here.
1126			 * What to do?
1127			 */
1128			atio->sense_len = 0;
1129			done_ccb->ccb_h.io_ptr = NULL;
1130			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
1131					  periph_links.tqe);
1132			xpt_schedule(periph, /*priority*/ 1);
1133			break;
1134		}
1135		ctl_zero_io(io);
1136
1137		/* Save pointers on both sides */
1138		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1139		done_ccb->ccb_h.io_ptr = io;
1140
1141		/*
1142		 * Only SCSI I/O comes down this path, resets, etc. come
1143		 * down the immediate notify path below.
1144		 */
1145		io->io_hdr.io_type = CTL_IO_SCSI;
1146		io->io_hdr.nexus.initid.id = atio->init_id;
1147		io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1148		io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1149		io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1150		io->scsiio.tag_num = atio->tag_id;
1151		switch (atio->tag_action) {
1152		case CAM_TAG_ACTION_NONE:
1153			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1154			break;
1155		case MSG_SIMPLE_TASK:
1156			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1157			break;
1158		case MSG_HEAD_OF_QUEUE_TASK:
1159        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1160			break;
1161		case MSG_ORDERED_TASK:
1162        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1163			break;
1164		case MSG_ACA_TASK:
1165			io->scsiio.tag_type = CTL_TAG_ACA;
1166			break;
1167		default:
1168			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1169			printf("%s: unhandled tag type %#x!!\n", __func__,
1170			       atio->tag_action);
1171			break;
1172		}
1173		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1174			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1175			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1176		}
1177		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1178		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1179		      io->scsiio.cdb_len);
1180
1181#ifdef CTLFEDEBUG
1182		printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1183		        (uintmax_t)io->io_hdr.nexus.initid.id,
1184		        io->io_hdr.nexus.targ_port,
1185		        (uintmax_t)io->io_hdr.nexus.targ_target.id,
1186		        io->io_hdr.nexus.targ_lun,
1187			io->scsiio.tag_num, io->scsiio.cdb[0]);
1188#endif
1189
1190		ctl_queue(io);
1191		break;
1192	}
1193	case XPT_CONT_TARGET_IO: {
1194		struct ccb_accept_tio *atio;
1195		union ctl_io *io;
1196
1197		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1198		io = (union ctl_io *)atio->ccb_h.io_ptr;
1199
1200		softc->ctios_returned++;
1201#ifdef CTLFEDEBUG
1202		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1203		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1204#endif
1205		/*
1206		 * If we were sending status back to the initiator, free up
1207		 * resources.  If we were doing a datamove, call the
1208		 * datamove done routine.
1209		 */
1210		if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
1211			softc->ccbs_freed++;
1212			xpt_release_ccb(done_ccb);
1213			ctl_free_io(io);
1214			/*
1215			 * For a wildcard attachment, commands can come in
1216			 * with a specific target/lun.  Reset the target
1217			 * and LUN fields back to the wildcard values before
1218			 * we send them back down to the SIM.  The SIM has
1219			 * a wildcard LUN enabled, not whatever target/lun
1220			 * these happened to be.
1221			 */
1222			if (softc->flags & CTLFE_LUN_WILDCARD) {
1223				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1224				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1225			}
1226			if (periph->flags & CAM_PERIPH_INVALID) {
1227				ctlfe_free_ccb(periph, (union ccb *)atio);
1228				return;
1229			} else {
1230				xpt_action((union ccb *)atio);
1231				softc->atios_sent++;
1232			}
1233		} else {
1234			struct ctlfe_lun_cmd_info *cmd_info;
1235			struct ccb_scsiio *csio;
1236
1237			csio = &done_ccb->csio;
1238			cmd_info = (struct ctlfe_lun_cmd_info *)
1239				io->io_hdr.port_priv;
1240
1241			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1242
1243			io->scsiio.ext_data_len += csio->dxfer_len;
1244			if (io->scsiio.ext_data_len >
1245			    io->scsiio.kern_total_len) {
1246				xpt_print(periph->path, "%s: tag 0x%04x "
1247					  "done len %u > total %u sent %u\n",
1248					  __func__, io->scsiio.tag_num,
1249					  io->scsiio.ext_data_len,
1250					  io->scsiio.kern_total_len,
1251					  io->scsiio.ext_data_filled);
1252			}
1253			/*
1254			 * Translate CAM status to CTL status.  Success
1255			 * does not change the overall, ctl_io status.  In
1256			 * that case we just set port_status to 0.  If we
1257			 * have a failure, though, set a data phase error
1258			 * for the overall ctl_io.
1259			 */
1260			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1261			case CAM_REQ_CMP:
1262				io->io_hdr.port_status = 0;
1263				break;
1264			default:
1265				/*
1266				 * XXX KDM the isp(4) driver doesn't really
1267				 * seem to send errors back for data
1268				 * transfers that I can tell.  There is one
1269				 * case where it'll send CAM_REQ_CMP_ERR,
1270				 * but probably not that many more cases.
1271				 * So set a generic data phase error here,
1272				 * like the SXP driver sets.
1273				 */
1274				io->io_hdr.port_status = 0xbad1;
1275				ctl_set_data_phase_error(&io->scsiio);
1276				/*
1277				 * XXX KDM figure out residual.
1278				 */
1279				break;
1280			}
1281			/*
1282			 * If we had to break this S/G list into multiple
1283			 * pieces, figure out where we are in the list, and
1284			 * continue sending pieces if necessary.
1285			 */
1286			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1287			 && (io->io_hdr.port_status == 0)
1288			 && (cmd_info->cur_transfer_index <
1289			     io->scsiio.kern_sg_entries)) {
1290				struct ctl_sg_entry *sglist;
1291				ccb_flags flags;
1292				uint8_t scsi_status;
1293				uint8_t *data_ptr;
1294				uint32_t dxfer_len;
1295				int *ti;
1296
1297				sglist = (struct ctl_sg_entry *)
1298					io->scsiio.kern_data_ptr;
1299				ti = &cmd_info->cur_transfer_index;
1300				flags = atio->ccb_h.flags &
1301					(CAM_DIS_DISCONNECT|
1302					 CAM_TAG_ACTION_VALID|
1303					 CAM_DIR_MASK);
1304
1305				/*
1306				 * Set the direction, relative to the initiator.
1307				 */
1308				flags &= ~CAM_DIR_MASK;
1309				if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1310				     CTL_FLAG_DATA_IN)
1311					flags |= CAM_DIR_IN;
1312				else
1313					flags |= CAM_DIR_OUT;
1314
1315				data_ptr = sglist[*ti].addr;
1316				dxfer_len = sglist[*ti].len;
1317				(*ti)++;
1318
1319				scsi_status = 0;
1320
1321				if (((flags & CAM_SEND_STATUS) == 0)
1322				 && (dxfer_len == 0)) {
1323					printf("%s: tag %04x no status or "
1324					       "len cdb = %02x\n", __func__,
1325					       atio->tag_id,
1326					atio->cdb_io.cdb_bytes[0]);
1327					printf("%s: tag %04x io status %#x\n",
1328					       __func__, atio->tag_id,
1329					       io->io_hdr.status);
1330				}
1331
1332				cam_fill_ctio(csio,
1333					      /*retries*/ 2,
1334					      ctlfedone,
1335					      flags,
1336					      (flags & CAM_TAG_ACTION_VALID) ?
1337					       MSG_SIMPLE_Q_TAG : 0,
1338					      atio->tag_id,
1339					      atio->init_id,
1340					      scsi_status,
1341					      /*data_ptr*/ data_ptr,
1342					      /*dxfer_len*/ dxfer_len,
1343					      /*timeout*/ 5 * 1000);
1344
1345				csio->resid = 0;
1346				csio->ccb_h.ccb_atio = atio;
1347				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1348				softc->ctios_sent++;
1349				xpt_action((union ccb *)csio);
1350			} else {
1351				/*
1352				 * Release the CTIO.  The ATIO will be sent back
1353				 * down to the SIM once we send status.
1354				 */
1355				softc->ccbs_freed++;
1356				xpt_release_ccb(done_ccb);
1357
1358				/* Call the backend move done callback */
1359				io->scsiio.be_move_done(io);
1360			}
1361		}
1362		break;
1363	}
1364	case XPT_IMMEDIATE_NOTIFY: {
1365		union ctl_io *io;
1366		struct ccb_immediate_notify *inot;
1367		cam_status status;
1368		int frozen;
1369
1370		inot = &done_ccb->cin1;
1371
1372		softc->inots_returned++;
1373
1374		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1375
1376		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1377		       "seq %#x\n", __func__, inot->ccb_h.status,
1378		       inot->tag_id, inot->seq_id);
1379
1380		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1381		if (io != NULL) {
1382			int send_ctl_io;
1383
1384			send_ctl_io = 1;
1385
1386			ctl_zero_io(io);
1387			io->io_hdr.io_type = CTL_IO_TASK;
1388			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1389			inot->ccb_h.io_ptr = io;
1390			io->io_hdr.nexus.initid.id = inot->initiator_id;
1391			io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1392			io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1393			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1394			/* XXX KDM should this be the tag_id? */
1395			io->taskio.tag_num = inot->seq_id;
1396
1397			status = inot->ccb_h.status & CAM_STATUS_MASK;
1398			switch (status) {
1399			case CAM_SCSI_BUS_RESET:
1400				io->taskio.task_action = CTL_TASK_BUS_RESET;
1401				break;
1402			case CAM_BDR_SENT:
1403				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1404				break;
1405			case CAM_MESSAGE_RECV:
1406				switch (inot->arg) {
1407				case MSG_ABORT_TASK_SET:
1408					/*
1409					 * XXX KDM this isn't currently
1410					 * supported by CTL.  It ends up
1411					 * being a no-op.
1412					 */
1413					io->taskio.task_action =
1414						CTL_TASK_ABORT_TASK_SET;
1415					break;
1416				case MSG_TARGET_RESET:
1417					io->taskio.task_action =
1418						CTL_TASK_TARGET_RESET;
1419					break;
1420				case MSG_ABORT_TASK:
1421					io->taskio.task_action =
1422						CTL_TASK_ABORT_TASK;
1423					break;
1424				case MSG_LOGICAL_UNIT_RESET:
1425					io->taskio.task_action =
1426						CTL_TASK_LUN_RESET;
1427					break;
1428				case MSG_CLEAR_TASK_SET:
1429					/*
1430					 * XXX KDM this isn't currently
1431					 * supported by CTL.  It ends up
1432					 * being a no-op.
1433					 */
1434					io->taskio.task_action =
1435						CTL_TASK_CLEAR_TASK_SET;
1436					break;
1437				case MSG_CLEAR_ACA:
1438					io->taskio.task_action =
1439						CTL_TASK_CLEAR_ACA;
1440					break;
1441				case MSG_NOOP:
1442					send_ctl_io = 0;
1443					break;
1444				default:
1445					xpt_print(periph->path, "%s: "
1446						  "unsupported message 0x%x\n",
1447						  __func__, inot->arg);
1448					send_ctl_io = 0;
1449					break;
1450				}
1451				break;
1452			case CAM_REQ_ABORTED:
1453				/*
1454				 * This request was sent back by the driver.
1455				 * XXX KDM what do we do here?
1456				 */
1457				send_ctl_io = 0;
1458				break;
1459			default:
1460				xpt_print(periph->path, "%s: "
1461					  "unsupported CAM status 0x%x\n",
1462					  __func__, status);
1463				send_ctl_io = 0;
1464				break;
1465			}
1466			if (send_ctl_io != 0) {
1467				ctl_queue(io);
1468			} else {
1469				ctl_free_io(io);
1470				done_ccb->ccb_h.status = CAM_REQ_INPROG;
1471				done_ccb->ccb_h.func_code =
1472					XPT_NOTIFY_ACKNOWLEDGE;
1473				xpt_action(done_ccb);
1474			}
1475		} else {
1476			xpt_print(periph->path, "%s: could not allocate "
1477				  "ctl_io for immediate notify!\n", __func__);
1478			/* requeue this to the adapter */
1479			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1480			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1481			xpt_action(done_ccb);
1482		}
1483
1484		if (frozen != 0) {
1485			cam_release_devq(periph->path,
1486					 /*relsim_flags*/ 0,
1487					 /*opening reduction*/ 0,
1488					 /*timeout*/ 0,
1489					 /*getcount_only*/ 0);
1490		}
1491		break;
1492	}
1493	case XPT_NOTIFY_ACKNOWLEDGE:
1494		/*
1495		 * Queue this back down to the SIM as an immediate notify.
1496		 */
1497		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1498		xpt_action(done_ccb);
1499		softc->inots_sent++;
1500		break;
1501	case XPT_ABORT:
1502		/*
1503		 * XPT_ABORT is an immediate CCB, we shouldn't get here.
1504		 */
1505		panic("%s: XPT_ABORT CCB returned!", __func__);
1506		break;
1507	case XPT_SET_SIM_KNOB:
1508	case XPT_GET_SIM_KNOB:
1509		break;
1510	default:
1511		panic("%s: unexpected CCB type %#x", __func__,
1512		      done_ccb->ccb_h.func_code);
1513		break;
1514	}
1515}
1516
1517static void
1518ctlfe_onoffline(void *arg, int online)
1519{
1520	struct ctlfe_softc *bus_softc;
1521	union ccb *ccb;
1522	cam_status status;
1523	struct cam_path *path;
1524	struct cam_sim *sim;
1525	int set_wwnn;
1526
1527	bus_softc = (struct ctlfe_softc *)arg;
1528
1529	set_wwnn = 0;
1530
1531	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1532		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1533	if (status != CAM_REQ_CMP) {
1534		printf("%s: unable to create path!\n", __func__);
1535		return;
1536	}
1537	ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_WAITOK | M_ZERO);
1538	if (ccb == NULL) {
1539		printf("%s: unable to malloc CCB!\n", __func__);
1540		xpt_free_path(path);
1541		return;
1542	}
1543	xpt_setup_ccb(&ccb->ccb_h, path, /*priority*/ 1);
1544
1545	sim = xpt_path_sim(path);
1546
1547	/*
1548	 * Copan WWN format:
1549	 *
1550	 * Bits 63-60:	0x5		NAA, IEEE registered name
1551	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1552	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1553	 * Bits 11-8:			Type of port:
1554	 *					1 == N-Port
1555	 *					2 == F-Port
1556	 *					3 == NL-Port
1557	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1558	 */
1559
1560	if (online != 0) {
1561
1562		ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1563
1564		CAM_SIM_LOCK(sim);
1565
1566		xpt_action(ccb);
1567
1568		CAM_SIM_UNLOCK(sim);
1569
1570		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1571#ifdef RANDOM_WWNN
1572			uint64_t random_bits;
1573#endif
1574
1575			printf("%s: %s current WWNN %#jx\n", __func__,
1576			       bus_softc->port_name,
1577			       ccb->knob.xport_specific.fc.wwnn);
1578			printf("%s: %s current WWPN %#jx\n", __func__,
1579			       bus_softc->port_name,
1580			       ccb->knob.xport_specific.fc.wwpn);
1581
1582#ifdef RANDOM_WWNN
1583			arc4rand(&random_bits, sizeof(random_bits), 0);
1584#endif
1585
1586			/*
1587			 * XXX KDM this is a bit of a kludge for now.  We
1588			 * take the current WWNN/WWPN from the card, and
1589			 * replace the company identifier and the NL-Port
1590			 * indicator and the port number (for the WWPN).
1591			 * This should be replaced later with ddb_GetWWNN,
1592			 * or possibly a more centralized scheme.  (It
1593			 * would be nice to have the WWNN/WWPN for each
1594			 * port stored in the ctl_frontend structure.)
1595			 */
1596#ifdef RANDOM_WWNN
1597			ccb->knob.xport_specific.fc.wwnn =
1598				(random_bits &
1599				0x0000000fffffff00ULL) |
1600				/* Company ID */ 0x5000ED5000000000ULL |
1601				/* NL-Port */    0x0300;
1602			ccb->knob.xport_specific.fc.wwpn =
1603				(random_bits &
1604				0x0000000fffffff00ULL) |
1605				/* Company ID */ 0x5000ED5000000000ULL |
1606				/* NL-Port */    0x3000 |
1607				/* Port Num */ (bus_softc->fe.targ_port & 0xff);
1608
1609			/*
1610			 * This is a bit of an API break/reversal, but if
1611			 * we're doing the random WWNN that's a little
1612			 * different anyway.  So record what we're actually
1613			 * using with the frontend code so it's reported
1614			 * accurately.
1615			 */
1616			bus_softc->fe.wwnn =
1617				ccb->knob.xport_specific.fc.wwnn;
1618			bus_softc->fe.wwpn =
1619				ccb->knob.xport_specific.fc.wwpn;
1620			set_wwnn = 1;
1621#else /* RANDOM_WWNN */
1622			/*
1623			 * If the user has specified a WWNN/WWPN, send them
1624			 * down to the SIM.  Otherwise, record what the SIM
1625			 * has reported.
1626			 */
1627			if ((bus_softc->fe.wwnn != 0)
1628			 && (bus_softc->fe.wwpn != 0)) {
1629				ccb->knob.xport_specific.fc.wwnn =
1630					bus_softc->fe.wwnn;
1631				ccb->knob.xport_specific.fc.wwpn =
1632					bus_softc->fe.wwpn;
1633				set_wwnn = 1;
1634			} else {
1635				bus_softc->fe.wwnn =
1636					ccb->knob.xport_specific.fc.wwnn;
1637				bus_softc->fe.wwpn =
1638					ccb->knob.xport_specific.fc.wwpn;
1639			}
1640#endif /* RANDOM_WWNN */
1641
1642
1643			if (set_wwnn != 0) {
1644				printf("%s: %s new WWNN %#jx\n", __func__,
1645				       bus_softc->port_name,
1646				ccb->knob.xport_specific.fc.wwnn);
1647				printf("%s: %s new WWPN %#jx\n", __func__,
1648				       bus_softc->port_name,
1649				       ccb->knob.xport_specific.fc.wwpn);
1650			}
1651		} else {
1652			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1653			       bus_softc->port_name);
1654		}
1655	}
1656	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1657	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1658	if (set_wwnn != 0)
1659		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1660
1661	if (online != 0)
1662		ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1663	else
1664		ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1665
1666
1667	CAM_SIM_LOCK(sim);
1668
1669	xpt_action(ccb);
1670
1671	CAM_SIM_UNLOCK(sim);
1672
1673	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1674		printf("%s: SIM %s (path id %d) target %s failed with "
1675		       "status %#x\n",
1676		       __func__, bus_softc->port_name, bus_softc->path_id,
1677		       (online != 0) ? "enable" : "disable",
1678		       ccb->ccb_h.status);
1679	} else {
1680		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1681		       __func__, bus_softc->port_name, bus_softc->path_id,
1682		       (online != 0) ? "enable" : "disable");
1683	}
1684
1685	free(ccb, M_TEMP);
1686	xpt_free_path(path);
1687
1688	return;
1689}
1690
1691static void
1692ctlfe_online(void *arg)
1693{
1694	ctlfe_onoffline(arg, /*online*/ 1);
1695}
1696
1697static void
1698ctlfe_offline(void *arg)
1699{
1700	ctlfe_onoffline(arg, /*online*/ 0);
1701}
1702
1703static int
1704ctlfe_targ_enable(void *arg, struct ctl_id targ_id)
1705{
1706	return (0);
1707}
1708
1709static int
1710ctlfe_targ_disable(void *arg, struct ctl_id targ_id)
1711{
1712	return (0);
1713}
1714
1715/*
1716 * This will get called to enable a LUN on every bus that is attached to
1717 * CTL.  So we only need to create a path/periph for this particular bus.
1718 */
1719static int
1720ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1721{
1722	struct ctlfe_softc *bus_softc;
1723	struct ctlfe_lun_softc *softc;
1724	struct cam_path *path;
1725	struct cam_periph *periph;
1726	struct cam_sim *sim;
1727	cam_status status;
1728
1729
1730	bus_softc = (struct ctlfe_softc *)arg;
1731
1732	status = xpt_create_path_unlocked(&path, /*periph*/ NULL,
1733					  bus_softc->path_id,
1734					  targ_id.id,
1735					  lun_id);
1736	/* XXX KDM need some way to return status to CTL here? */
1737	if (status != CAM_REQ_CMP) {
1738		printf("%s: could not create path, status %#x\n", __func__,
1739		       status);
1740		return (1);
1741	}
1742
1743	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1744	if (softc == NULL) {
1745		printf("%s: could not allocate %zd bytes for softc\n",
1746		       __func__, sizeof(*softc));
1747		xpt_free_path(path);
1748		return (1);
1749	}
1750	sim = xpt_path_sim(path);
1751	mtx_lock(sim->mtx);
1752	periph = cam_periph_find(path, "ctl");
1753	if (periph != NULL) {
1754		/* We've already got a periph, no need to alloc a new one. */
1755		xpt_free_path(path);
1756		free(softc, M_CTLFE);
1757		mtx_unlock(sim->mtx);
1758		return (0);
1759	}
1760
1761	softc->parent_softc = bus_softc;
1762	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
1763
1764	status = cam_periph_alloc(ctlferegister,
1765				  ctlfeoninvalidate,
1766				  ctlfecleanup,
1767				  ctlfestart,
1768				  "ctl",
1769				  CAM_PERIPH_BIO,
1770				  path,
1771				  ctlfeasync,
1772				  0,
1773				  softc);
1774
1775	mtx_unlock(sim->mtx);
1776
1777	xpt_free_path(path);
1778
1779	return (0);
1780}
1781
1782/*
1783 * XXX KDM we disable LUN removal here.  The problem is that the isp(4)
1784 * driver doesn't currently handle LUN removal properly.  We need to keep
1785 * enough state here at the peripheral level even after LUNs have been
1786 * removed inside CTL.
1787 *
1788 * Once the isp(4) driver is fixed, this can be re-enabled.
1789 */
1790static int
1791ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1792{
1793#ifdef NOTYET
1794	struct ctlfe_softc *softc;
1795	struct ctlfe_lun_softc *lun_softc;
1796
1797	softc = (struct ctlfe_softc *)arg;
1798
1799	mtx_lock(softc->sim->mtx);
1800	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1801		struct cam_path *path;
1802
1803		path = lun_softc->periph->path;
1804
1805		if ((xpt_path_target_id(path) == targ_id.id)
1806		 && (xpt_path_lun_id(path) == lun_id)) {
1807			break;
1808		}
1809	}
1810	if (lun_softc == NULL) {
1811		mtx_unlock(softc->sim->mtx);
1812		printf("%s: can't find target %d lun %d\n", __func__,
1813		       targ_id.id, lun_id);
1814		return (1);
1815	}
1816
1817	cam_periph_invalidate(lun_softc->periph);
1818
1819	mtx_unlock(softc->sim->mtx);
1820#endif
1821
1822	return (0);
1823}
1824
1825static void
1826ctlfe_dump_sim(struct cam_sim *sim)
1827{
1828	int i;
1829
1830	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1831	       sim->sim_name, sim->unit_number,
1832	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1833	printf("%s%d: max_ccbs: %u, ccb_count: %u\n",
1834	       sim->sim_name, sim->unit_number,
1835	       sim->max_ccbs, sim->ccb_count);
1836	printf("%s%d: ccb_freeq is %sempty\n",
1837	       sim->sim_name, sim->unit_number,
1838	       (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT ");
1839	printf("%s%d: alloc_queue.entries %d, alloc_openings %d\n",
1840	       sim->sim_name, sim->unit_number,
1841	       sim->devq->alloc_queue.entries, sim->devq->alloc_openings);
1842	printf("%s%d: qfrozen_cnt:", sim->sim_name, sim->unit_number);
1843	for (i = 0; i < CAM_RL_VALUES; i++) {
1844		printf("%s%u", (i != 0) ? ":" : "",
1845		sim->devq->alloc_queue.qfrozen_cnt[i]);
1846	}
1847	printf("\n");
1848}
1849
1850/*
1851 * Assumes that the SIM lock is held.
1852 */
1853static void
1854ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1855{
1856	struct ccb_hdr *hdr;
1857	struct cam_periph *periph;
1858	int num_items;
1859
1860	periph = softc->periph;
1861	num_items = 0;
1862
1863	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1864		union ctl_io *io;
1865
1866		io = hdr->io_ptr;
1867
1868		num_items++;
1869
1870		/*
1871		 * This can happen when we get an ATIO but can't allocate
1872		 * a ctl_io.  See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
1873		 */
1874		if (io == NULL) {
1875			struct ccb_scsiio *csio;
1876
1877			csio = (struct ccb_scsiio *)hdr;
1878
1879			xpt_print(periph->path, "CCB %#x ctl_io allocation "
1880				  "failed\n", csio->tag_id);
1881			continue;
1882		}
1883
1884		/*
1885		 * Only regular SCSI I/O is put on the work
1886		 * queue, so we can print sense here.  There may be no
1887		 * sense if it's no the queue for a DMA, but this serves to
1888		 * print out the CCB as well.
1889		 *
1890		 * XXX KDM switch this over to scsi_sense_print() when
1891		 * CTL is merged in with CAM.
1892		 */
1893		ctl_io_error_print(io, NULL);
1894
1895		/*
1896		 * We're sending status back to the
1897		 * initiator, so we're on the queue waiting
1898		 * for a CTIO to do that.
1899		 */
1900		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1901			continue;
1902
1903		/*
1904		 * Otherwise, we're on the queue waiting to
1905		 * do a data transfer.
1906		 */
1907		xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
1908			  io->scsiio.kern_total_len, io->scsiio.kern_data_len,
1909			  io->scsiio.kern_data_resid);
1910	}
1911
1912	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1913		  num_items);
1914	xpt_print(periph->path, "%ju CCBs oustanding (%ju allocated, %ju "
1915		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1916		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1917		  (uintmax_t)softc->ccbs_freed);
1918	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1919		  "returned\n", (uintmax_t)(softc->ctios_sent -
1920		  softc->ctios_returned), softc->ctios_sent,
1921		  softc->ctios_returned);
1922}
1923
1924/*
1925 * This function is called when we fail to get a CCB for a DMA or status return
1926 * to the initiator within the specified time period.
1927 *
1928 * The callout code should insure that we hold the sim mutex here.
1929 */
1930static void
1931ctlfe_dma_timeout(void *arg)
1932{
1933	struct ctlfe_lun_softc *softc;
1934	struct cam_periph *periph;
1935	struct cam_sim *sim;
1936	int num_queued;
1937
1938	softc = (struct ctlfe_lun_softc *)arg;
1939	periph = softc->periph;
1940	sim = xpt_path_sim(periph->path);
1941	num_queued = 0;
1942
1943	/*
1944	 * Nothing to do...
1945	 */
1946	if (TAILQ_FIRST(&softc->work_queue) == NULL) {
1947		xpt_print(periph->path, "TIMEOUT triggered after %d "
1948			  "seconds, but nothing on work queue??\n",
1949			  CTLFE_DMA_TIMEOUT);
1950		return;
1951	}
1952
1953	xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
1954		  "start\n", CTLFE_DMA_TIMEOUT);
1955
1956	ctlfe_dump_queue(softc);
1957
1958	ctlfe_dump_sim(sim);
1959
1960	xpt_print(periph->path, "calling xpt_schedule() to attempt to "
1961		  "unstick our queue\n");
1962
1963	xpt_schedule(periph, /*priority*/ 1);
1964
1965	xpt_print(periph->path, "xpt_schedule() call complete\n");
1966}
1967
1968/*
1969 * Datamove/done routine called by CTL.  Put ourselves on the queue to
1970 * receive a CCB from CAM so we can queue the continue I/O request down
1971 * to the adapter.
1972 */
1973static void
1974ctlfe_datamove_done(union ctl_io *io)
1975{
1976	union ccb *ccb;
1977	struct cam_sim *sim;
1978	struct cam_periph *periph;
1979	struct ctlfe_lun_softc *softc;
1980
1981	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1982
1983	sim = xpt_path_sim(ccb->ccb_h.path);
1984
1985	mtx_lock(sim->mtx);
1986
1987	periph = xpt_path_periph(ccb->ccb_h.path);
1988
1989	softc = (struct ctlfe_lun_softc *)periph->softc;
1990
1991	if (io->io_hdr.io_type == CTL_IO_TASK) {
1992		/*
1993		 * Task management commands don't require any further
1994		 * communication back to the adapter.  Requeue the CCB
1995		 * to the adapter, and free the CTL I/O.
1996		 */
1997		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
1998			  "tag %#x seq %#x\n", __func__,
1999			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2000		/*
2001		 * Send the notify acknowledge down to the SIM, to let it
2002		 * know we processed the task management command.
2003		 */
2004		ccb->ccb_h.status = CAM_REQ_INPROG;
2005		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2006		xpt_action(ccb);
2007		ctl_free_io(io);
2008	} else {
2009		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2010			io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2011		else
2012			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2013
2014		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2015				  periph_links.tqe);
2016
2017		/*
2018		 * Reset the timeout for our latest active DMA.
2019		 */
2020		callout_reset(&softc->dma_callout,
2021			      CTLFE_DMA_TIMEOUT * hz,
2022			      ctlfe_dma_timeout, softc);
2023		/*
2024		 * Ask for the CAM transport layer to send us a CCB to do
2025		 * the DMA or send status, unless ctlfe_dma_enabled is set
2026		 * to 0.
2027		 */
2028		if (ctlfe_dma_enabled != 0)
2029			xpt_schedule(periph, /*priority*/ 1);
2030	}
2031
2032	mtx_unlock(sim->mtx);
2033}
2034
2035static void
2036ctlfe_dump(void)
2037{
2038	struct ctlfe_softc *bus_softc;
2039
2040	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2041		struct ctlfe_lun_softc *lun_softc;
2042
2043		ctlfe_dump_sim(bus_softc->sim);
2044
2045		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
2046			ctlfe_dump_queue(lun_softc);
2047		}
2048	}
2049}
2050