scsi_ctl.c revision 315138
1/*-
2 * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer,
11 *    without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 *    substantially similar to the "NO WARRANTY" disclaimer below
14 *    ("Disclaimer") and any redistribution must be conditioned upon
15 *    including a substantially similar Disclaimer requirement for further
16 *    binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
32 */
33/*
34 * Peripheral driver interface between CAM and CTL (CAM Target Layer).
35 *
36 * Author: Ken Merry <ken@FreeBSD.org>
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/scsi_ctl.c 315138 2017-03-12 06:18:41Z mav $");
41
42#include <sys/param.h>
43#include <sys/queue.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/condvar.h>
49#include <sys/malloc.h>
50#include <sys/bus.h>
51#include <sys/endian.h>
52#include <sys/sbuf.h>
53#include <sys/sysctl.h>
54#include <sys/types.h>
55#include <sys/systm.h>
56#include <sys/taskqueue.h>
57#include <machine/bus.h>
58
59#include <cam/cam.h>
60#include <cam/cam_ccb.h>
61#include <cam/cam_periph.h>
62#include <cam/cam_queue.h>
63#include <cam/cam_xpt_periph.h>
64#include <cam/cam_debug.h>
65#include <cam/cam_sim.h>
66#include <cam/cam_xpt.h>
67
68#include <cam/scsi/scsi_all.h>
69#include <cam/scsi/scsi_message.h>
70
71#include <cam/ctl/ctl_io.h>
72#include <cam/ctl/ctl.h>
73#include <cam/ctl/ctl_frontend.h>
74#include <cam/ctl/ctl_util.h>
75#include <cam/ctl/ctl_error.h>
76
77struct ctlfe_softc {
78	struct ctl_port	port;
79	path_id_t	path_id;
80	target_id_t	target_id;
81	uint32_t	hba_misc;
82	u_int		maxio;
83	struct cam_sim *sim;
84	char		port_name[DEV_IDLEN];
85	struct mtx	lun_softc_mtx;
86	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
87	STAILQ_ENTRY(ctlfe_softc) links;
88};
89
90STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
91struct mtx ctlfe_list_mtx;
92static char ctlfe_mtx_desc[] = "ctlfelist";
93
94typedef enum {
95	CTLFE_LUN_NONE		= 0x00,
96	CTLFE_LUN_WILDCARD	= 0x01
97} ctlfe_lun_flags;
98
99struct ctlfe_lun_softc {
100	struct ctlfe_softc *parent_softc;
101	struct cam_periph *periph;
102	ctlfe_lun_flags flags;
103	int	 ctios_sent;		/* Number of active CTIOs */
104	int	 refcount;		/* Number of active xpt_action() */
105	int	 atios_alloced;		/* Number of ATIOs not freed */
106	int	 inots_alloced;		/* Number of INOTs not freed */
107	struct task	refdrain_task;
108	TAILQ_HEAD(, ccb_hdr) work_queue;
109	STAILQ_ENTRY(ctlfe_lun_softc) links;
110};
111
112typedef enum {
113	CTLFE_CMD_NONE		= 0x00,
114	CTLFE_CMD_PIECEWISE	= 0x01
115} ctlfe_cmd_flags;
116
117struct ctlfe_cmd_info {
118	int cur_transfer_index;
119	size_t cur_transfer_off;
120	ctlfe_cmd_flags flags;
121	/*
122	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
123	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
124	 * i386 and 512 bytes on amd64.
125	 */
126#define CTLFE_MAX_SEGS	32
127	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
128};
129
130/*
131 * When we register the adapter/bus, request that this many ctl_ios be
132 * allocated.  This should be the maximum supported by the adapter, but we
133 * currently don't have a way to get that back from the path inquiry.
134 * XXX KDM add that to the path inquiry.
135 */
136#define	CTLFE_REQ_CTL_IO	4096
137/*
138 * Number of Accept Target I/O CCBs to allocate and queue down to the
139 * adapter per LUN.
140 * XXX KDM should this be controlled by CTL?
141 */
142#define	CTLFE_ATIO_PER_LUN	1024
143/*
144 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
145 * allocate and queue down to the adapter per LUN.
146 * XXX KDM should this be controlled by CTL?
147 */
148#define	CTLFE_IN_PER_LUN	1024
149
150/*
151 * Timeout (in seconds) on CTIO CCB doing DMA or sending status
152 */
153#define	CTLFE_TIMEOUT	5
154
155/*
156 * Turn this on to enable extra debugging prints.
157 */
158#if 0
159#define	CTLFE_DEBUG
160#endif
161
162MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
163
164#define	io_ptr		ppriv_ptr0
165
166/* This is only used in the CTIO */
167#define	ccb_atio	ppriv_ptr1
168
169#define PRIV_CCB(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
170#define PRIV_INFO(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
171
172static int		ctlfeinitialize(void);
173static int		ctlfeshutdown(void);
174static periph_init_t	ctlfeperiphinit;
175static void		ctlfeasync(void *callback_arg, uint32_t code,
176				   struct cam_path *path, void *arg);
177static periph_ctor_t	ctlferegister;
178static periph_oninv_t	ctlfeoninvalidate;
179static periph_dtor_t	ctlfecleanup;
180static periph_start_t	ctlfestart;
181static void		ctlfedone(struct cam_periph *periph,
182				  union ccb *done_ccb);
183
184static void 		ctlfe_onoffline(void *arg, int online);
185static void 		ctlfe_online(void *arg);
186static void 		ctlfe_offline(void *arg);
187static int 		ctlfe_lun_enable(void *arg, int lun_id);
188static int 		ctlfe_lun_disable(void *arg, int lun_id);
189static void		ctlfe_dump_sim(struct cam_sim *sim);
190static void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
191static void 		ctlfe_datamove(union ctl_io *io);
192static void 		ctlfe_done(union ctl_io *io);
193static void 		ctlfe_dump(void);
194static void		ctlfe_free_ccb(struct cam_periph *periph,
195			    union ccb *ccb);
196static void		ctlfe_requeue_ccb(struct cam_periph *periph,
197			    union ccb *ccb, int unlock);
198
199static struct periph_driver ctlfe_driver =
200{
201	ctlfeperiphinit, "ctl",
202	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
203	CAM_PERIPH_DRV_EARLY
204};
205
206static struct ctl_frontend ctlfe_frontend =
207{
208	.name = "camtgt",
209	.init = ctlfeinitialize,
210	.fe_dump = ctlfe_dump,
211	.shutdown = ctlfeshutdown,
212};
213CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
214
215static int
216ctlfeshutdown(void)
217{
218
219	/* CAM does not support periph driver unregister now. */
220	return (EBUSY);
221}
222
223static int
224ctlfeinitialize(void)
225{
226
227	STAILQ_INIT(&ctlfe_softc_list);
228	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
229	periphdriver_register(&ctlfe_driver);
230	return (0);
231}
232
233static void
234ctlfeperiphinit(void)
235{
236	cam_status status;
237
238	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
239				    AC_CONTRACT, ctlfeasync, NULL, NULL);
240	if (status != CAM_REQ_CMP) {
241		printf("ctl: Failed to attach async callback due to CAM "
242		       "status 0x%x!\n", status);
243	}
244}
245
246static void
247ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
248{
249	struct ctlfe_softc *softc;
250
251#ifdef CTLFEDEBUG
252	printf("%s: entered\n", __func__);
253#endif
254
255	mtx_lock(&ctlfe_list_mtx);
256	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
257		if (softc->path_id == xpt_path_path_id(path))
258			break;
259	}
260	mtx_unlock(&ctlfe_list_mtx);
261
262	/*
263	 * When a new path gets registered, and it is capable of target
264	 * mode, go ahead and attach.  Later on, we may need to be more
265	 * selective, but for now this will be sufficient.
266 	 */
267	switch (code) {
268	case AC_PATH_REGISTERED: {
269		struct ctl_port *port;
270		struct ccb_pathinq *cpi;
271		int retval;
272
273		cpi = (struct ccb_pathinq *)arg;
274
275		/* Don't attach if it doesn't support target mode */
276		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
277#ifdef CTLFEDEBUG
278			printf("%s: SIM %s%d doesn't support target mode\n",
279			       __func__, cpi->dev_name, cpi->unit_number);
280#endif
281			break;
282		}
283
284		if (softc != NULL) {
285#ifdef CTLFEDEBUG
286			printf("%s: CTL port for CAM path %u already exists\n",
287			       __func__, xpt_path_path_id(path));
288#endif
289			break;
290		}
291
292		/*
293		 * We're in an interrupt context here, so we have to
294		 * use M_NOWAIT.  Of course this means trouble if we
295		 * can't allocate memory.
296		 */
297		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
298		if (softc == NULL) {
299			printf("%s: unable to malloc %zd bytes for softc\n",
300			       __func__, sizeof(*softc));
301			return;
302		}
303
304		softc->path_id = cpi->ccb_h.path_id;
305		softc->target_id = cpi->initiator_id;
306		softc->sim = xpt_path_sim(path);
307		softc->hba_misc = cpi->hba_misc;
308		if (cpi->maxio != 0)
309			softc->maxio = cpi->maxio;
310		else
311			softc->maxio = DFLTPHYS;
312		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
313		STAILQ_INIT(&softc->lun_softc_list);
314
315		port = &softc->port;
316		port->frontend = &ctlfe_frontend;
317
318		/*
319		 * XXX KDM should we be more accurate here ?
320		 */
321		if (cpi->transport == XPORT_FC)
322			port->port_type = CTL_PORT_FC;
323		else if (cpi->transport == XPORT_SAS)
324			port->port_type = CTL_PORT_SAS;
325		else
326			port->port_type = CTL_PORT_SCSI;
327
328		/* XXX KDM what should the real number be here? */
329		port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
330		snprintf(softc->port_name, sizeof(softc->port_name),
331			 "%s%d", cpi->dev_name, cpi->unit_number);
332		/*
333		 * XXX KDM it would be nice to allocate storage in the
334		 * frontend structure itself.
335	 	 */
336		port->port_name = softc->port_name;
337		port->physical_port = cpi->bus_id;
338		port->virtual_port = 0;
339		port->port_online = ctlfe_online;
340		port->port_offline = ctlfe_offline;
341		port->onoff_arg = softc;
342		port->lun_enable = ctlfe_lun_enable;
343		port->lun_disable = ctlfe_lun_disable;
344		port->targ_lun_arg = softc;
345		port->fe_datamove = ctlfe_datamove;
346		port->fe_done = ctlfe_done;
347		/*
348		 * XXX KDM the path inquiry doesn't give us the maximum
349		 * number of targets supported.
350		 */
351		port->max_targets = cpi->max_target;
352		port->max_target_id = cpi->max_target;
353		port->targ_port = -1;
354
355		retval = ctl_port_register(port);
356		if (retval != 0) {
357			printf("%s: ctl_port_register() failed with "
358			       "error %d!\n", __func__, retval);
359			mtx_destroy(&softc->lun_softc_mtx);
360			free(softc, M_CTLFE);
361			break;
362		} else {
363			mtx_lock(&ctlfe_list_mtx);
364			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
365			mtx_unlock(&ctlfe_list_mtx);
366		}
367
368		break;
369	}
370	case AC_PATH_DEREGISTERED: {
371
372		if (softc != NULL) {
373			/*
374			 * XXX KDM are we certain at this point that there
375			 * are no outstanding commands for this frontend?
376			 */
377			mtx_lock(&ctlfe_list_mtx);
378			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
379			    links);
380			mtx_unlock(&ctlfe_list_mtx);
381			ctl_port_deregister(&softc->port);
382			mtx_destroy(&softc->lun_softc_mtx);
383			free(softc, M_CTLFE);
384		}
385		break;
386	}
387	case AC_CONTRACT: {
388		struct ac_contract *ac;
389
390		ac = (struct ac_contract *)arg;
391
392		switch (ac->contract_number) {
393		case AC_CONTRACT_DEV_CHG: {
394			struct ac_device_changed *dev_chg;
395			int retval;
396
397			dev_chg = (struct ac_device_changed *)ac->contract_data;
398
399			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
400			       __func__, dev_chg->wwpn, dev_chg->port,
401			       xpt_path_path_id(path), dev_chg->target,
402			       (dev_chg->arrived == 0) ?  "left" : "arrived");
403
404			if (softc == NULL) {
405				printf("%s: CTL port for CAM path %u not "
406				       "found!\n", __func__,
407				       xpt_path_path_id(path));
408				break;
409			}
410			if (dev_chg->arrived != 0) {
411				retval = ctl_add_initiator(&softc->port,
412				    dev_chg->target, dev_chg->wwpn, NULL);
413			} else {
414				retval = ctl_remove_initiator(&softc->port,
415				    dev_chg->target);
416			}
417
418			if (retval < 0) {
419				printf("%s: could not %s port %d iid %u "
420				       "WWPN %#jx!\n", __func__,
421				       (dev_chg->arrived != 0) ? "add" :
422				       "remove", softc->port.targ_port,
423				       dev_chg->target,
424				       (uintmax_t)dev_chg->wwpn);
425			}
426			break;
427		}
428		default:
429			printf("%s: unsupported contract number %ju\n",
430			       __func__, (uintmax_t)ac->contract_number);
431			break;
432		}
433		break;
434	}
435	default:
436		break;
437	}
438}
439
440static cam_status
441ctlferegister(struct cam_periph *periph, void *arg)
442{
443	struct ctlfe_softc *bus_softc;
444	struct ctlfe_lun_softc *softc;
445	union ccb en_lun_ccb;
446	cam_status status;
447	int i;
448
449	softc = (struct ctlfe_lun_softc *)arg;
450	bus_softc = softc->parent_softc;
451
452	TAILQ_INIT(&softc->work_queue);
453	softc->periph = periph;
454	periph->softc = softc;
455
456	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
457	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
458	en_lun_ccb.cel.grp6_len = 0;
459	en_lun_ccb.cel.grp7_len = 0;
460	en_lun_ccb.cel.enable = 1;
461	xpt_action(&en_lun_ccb);
462	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
463	if (status != CAM_REQ_CMP) {
464		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
465			  __func__, en_lun_ccb.ccb_h.status);
466		return (status);
467	}
468
469	status = CAM_REQ_CMP;
470
471	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
472		union ccb *new_ccb;
473		union ctl_io *new_io;
474		struct ctlfe_cmd_info *cmd_info;
475
476		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
477					      M_ZERO|M_NOWAIT);
478		if (new_ccb == NULL) {
479			status = CAM_RESRC_UNAVAIL;
480			break;
481		}
482		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
483		if (new_io == NULL) {
484			free(new_ccb, M_CTLFE);
485			status = CAM_RESRC_UNAVAIL;
486			break;
487		}
488		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
489		    M_ZERO | M_NOWAIT);
490		if (cmd_info == NULL) {
491			ctl_free_io(new_io);
492			free(new_ccb, M_CTLFE);
493			status = CAM_RESRC_UNAVAIL;
494			break;
495		}
496		PRIV_INFO(new_io) = cmd_info;
497		softc->atios_alloced++;
498		new_ccb->ccb_h.io_ptr = new_io;
499
500		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
501		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
502		new_ccb->ccb_h.cbfcnp = ctlfedone;
503		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
504		xpt_action(new_ccb);
505		status = new_ccb->ccb_h.status;
506		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
507			free(cmd_info, M_CTLFE);
508			ctl_free_io(new_io);
509			free(new_ccb, M_CTLFE);
510			break;
511		}
512	}
513
514	status = cam_periph_acquire(periph);
515	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
516		xpt_print(periph->path, "%s: could not acquire reference "
517			  "count, status = %#x\n", __func__, status);
518		return (status);
519	}
520
521	if (i == 0) {
522		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
523			  "status 0x%x\n", __func__, status);
524		return (CAM_REQ_CMP_ERR);
525	}
526
527	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
528		union ccb *new_ccb;
529		union ctl_io *new_io;
530
531		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
532					      M_ZERO|M_NOWAIT);
533		if (new_ccb == NULL) {
534			status = CAM_RESRC_UNAVAIL;
535			break;
536		}
537		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
538		if (new_io == NULL) {
539			free(new_ccb, M_CTLFE);
540			status = CAM_RESRC_UNAVAIL;
541			break;
542		}
543		softc->inots_alloced++;
544		new_ccb->ccb_h.io_ptr = new_io;
545
546		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
547		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
548		new_ccb->ccb_h.cbfcnp = ctlfedone;
549		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
550		xpt_action(new_ccb);
551		status = new_ccb->ccb_h.status;
552		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
553			/*
554			 * Note that we don't free the CCB here.  If the
555			 * status is not CAM_REQ_INPROG, then we're
556			 * probably talking to a SIM that says it is
557			 * target-capable but doesn't support the
558			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
559			 * older API.  In that case, it'll call xpt_done()
560			 * on the CCB, and we need to free it in our done
561			 * routine as a result.
562			 */
563			break;
564		}
565	}
566	if ((i == 0)
567	 || (status != CAM_REQ_INPROG)) {
568		xpt_print(periph->path, "%s: could not allocate immediate "
569			  "notify CCBs, status 0x%x\n", __func__, status);
570		return (CAM_REQ_CMP_ERR);
571	}
572	mtx_lock(&bus_softc->lun_softc_mtx);
573	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
574	mtx_unlock(&bus_softc->lun_softc_mtx);
575	return (CAM_REQ_CMP);
576}
577
578static void
579ctlfeoninvalidate(struct cam_periph *periph)
580{
581	union ccb en_lun_ccb;
582	cam_status status;
583	struct ctlfe_softc *bus_softc;
584	struct ctlfe_lun_softc *softc;
585
586	softc = (struct ctlfe_lun_softc *)periph->softc;
587
588	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
589	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
590	en_lun_ccb.cel.grp6_len = 0;
591	en_lun_ccb.cel.grp7_len = 0;
592	en_lun_ccb.cel.enable = 0;
593	xpt_action(&en_lun_ccb);
594	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
595	if (status != CAM_REQ_CMP) {
596		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
597			  __func__, en_lun_ccb.ccb_h.status);
598		/*
599		 * XXX KDM what do we do now?
600		 */
601	}
602
603	bus_softc = softc->parent_softc;
604	mtx_lock(&bus_softc->lun_softc_mtx);
605	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
606	mtx_unlock(&bus_softc->lun_softc_mtx);
607}
608
609static void
610ctlfecleanup(struct cam_periph *periph)
611{
612	struct ctlfe_lun_softc *softc;
613
614	softc = (struct ctlfe_lun_softc *)periph->softc;
615
616	KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
617	    __func__, softc->ctios_sent));
618	KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
619	    __func__, softc->refcount));
620	KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
621	    __func__, softc->atios_alloced));
622	KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
623	    __func__, softc->inots_alloced));
624
625	free(softc, M_CTLFE);
626}
627
628static void
629ctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
630    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
631    u_int16_t *sglist_cnt)
632{
633	struct ctlfe_softc *bus_softc;
634	struct ctlfe_cmd_info *cmd_info;
635	struct ctl_sg_entry *ctl_sglist;
636	bus_dma_segment_t *cam_sglist;
637	size_t off;
638	int i, idx;
639
640	cmd_info = PRIV_INFO(io);
641	bus_softc = softc->parent_softc;
642
643	/*
644	 * Set the direction, relative to the initiator.
645	 */
646	*flags &= ~CAM_DIR_MASK;
647	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
648		*flags |= CAM_DIR_IN;
649	else
650		*flags |= CAM_DIR_OUT;
651
652	*flags &= ~CAM_DATA_MASK;
653	idx = cmd_info->cur_transfer_index;
654	off = cmd_info->cur_transfer_off;
655	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
656	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
657
658		/* One time shift for SRR offset. */
659		off += io->scsiio.ext_data_filled;
660		io->scsiio.ext_data_filled = 0;
661
662		*data_ptr = io->scsiio.kern_data_ptr + off;
663		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
664			*dxfer_len = io->scsiio.kern_data_len - off;
665		} else {
666			*dxfer_len = bus_softc->maxio;
667			cmd_info->cur_transfer_off += bus_softc->maxio;
668			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
669		}
670		*sglist_cnt = 0;
671
672		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
673			*flags |= CAM_DATA_PADDR;
674		else
675			*flags |= CAM_DATA_VADDR;
676	} else {	/* S/G list with physical or virtual pointers. */
677		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
678
679		/* One time shift for SRR offset. */
680		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
681			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
682			idx++;
683			off = 0;
684		}
685		off += io->scsiio.ext_data_filled;
686		io->scsiio.ext_data_filled = 0;
687
688		cam_sglist = cmd_info->cam_sglist;
689		*dxfer_len = 0;
690		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
691			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
692			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
693				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
694				*dxfer_len += cam_sglist[i].ds_len;
695			} else {
696				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
697				cmd_info->cur_transfer_index = idx + i;
698				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
699				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
700				*dxfer_len += cam_sglist[i].ds_len;
701				if (ctl_sglist[i].len != 0)
702					i++;
703				break;
704			}
705			if (i == (CTLFE_MAX_SEGS - 1) &&
706			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
707				cmd_info->cur_transfer_index = idx + i + 1;
708				cmd_info->cur_transfer_off = 0;
709				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
710				i++;
711				break;
712			}
713			off = 0;
714		}
715		*sglist_cnt = i;
716		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
717			*flags |= CAM_DATA_SG_PADDR;
718		else
719			*flags |= CAM_DATA_SG;
720		*data_ptr = (uint8_t *)cam_sglist;
721	}
722}
723
724static void
725ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
726{
727	struct ctlfe_lun_softc *softc;
728	struct ctlfe_cmd_info *cmd_info;
729	struct ccb_hdr *ccb_h;
730	struct ccb_accept_tio *atio;
731	struct ccb_scsiio *csio;
732	uint8_t *data_ptr;
733	uint32_t dxfer_len;
734	ccb_flags flags;
735	union ctl_io *io;
736	uint8_t scsi_status;
737
738	softc = (struct ctlfe_lun_softc *)periph->softc;
739
740next:
741	ccb_h = TAILQ_FIRST(&softc->work_queue);
742	if (ccb_h == NULL) {
743		xpt_release_ccb(start_ccb);
744		return;
745	}
746
747	/* Take the ATIO off the work queue */
748	TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
749	atio = (struct ccb_accept_tio *)ccb_h;
750	io = (union ctl_io *)ccb_h->io_ptr;
751	csio = &start_ccb->csio;
752
753	flags = atio->ccb_h.flags &
754		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
755	cmd_info = PRIV_INFO(io);
756	cmd_info->cur_transfer_index = 0;
757	cmd_info->cur_transfer_off = 0;
758	cmd_info->flags = 0;
759
760	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
761		/*
762		 * Datamove call, we need to setup the S/G list.
763		 */
764		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
765		    &csio->sglist_cnt);
766	} else {
767		/*
768		 * We're done, send status back.
769		 */
770		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
771		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
772			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
773
774			/* Tell the SIM that we've aborted this ATIO */
775#ifdef CTLFEDEBUG
776			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
777#endif
778			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
779			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
780			start_ccb->ccb_h.func_code = XPT_ABORT;
781			start_ccb->cab.abort_ccb = (union ccb *)atio;
782			xpt_action(start_ccb);
783
784			ctlfe_requeue_ccb(periph, (union ccb *)atio,
785			    /* unlock */0);
786
787			/* XPT_ABORT is not queued, so we can take next I/O. */
788			goto next;
789		}
790		data_ptr = NULL;
791		dxfer_len = 0;
792		csio->sglist_cnt = 0;
793	}
794	scsi_status = 0;
795	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
796	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
797	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
798	     io->io_hdr.status == CTL_SUCCESS)) {
799		flags |= CAM_SEND_STATUS;
800		scsi_status = io->scsiio.scsi_status;
801		csio->sense_len = io->scsiio.sense_len;
802#ifdef CTLFEDEBUG
803		printf("%s: tag %04x status %x\n", __func__,
804		       atio->tag_id, io->io_hdr.status);
805#endif
806		if (csio->sense_len != 0) {
807			csio->sense_data = io->scsiio.sense_data;
808			flags |= CAM_SEND_SENSE;
809		}
810	}
811
812#ifdef CTLFEDEBUG
813	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
814	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
815	       atio->tag_id, flags, data_ptr, dxfer_len);
816#endif
817
818	/*
819	 * Valid combinations:
820	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
821	 *    sglist_cnt = 0
822	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
823	 *    sglist_cnt = 0
824	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
825	 *    sglist_cnt != 0
826	 */
827#ifdef CTLFEDEBUG
828	if (((flags & CAM_SEND_STATUS)
829	  && (((flags & CAM_DATA_SG) != 0)
830	   || (dxfer_len != 0)
831	   || (csio->sglist_cnt != 0)))
832	 || (((flags & CAM_SEND_STATUS) == 0)
833	  && (dxfer_len == 0))
834	 || ((flags & CAM_DATA_SG)
835	  && (csio->sglist_cnt == 0))
836	 || (((flags & CAM_DATA_SG) == 0)
837	  && (csio->sglist_cnt != 0))) {
838		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
839		       "%d sg %u\n", __func__, atio->tag_id,
840		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
841		       csio->sglist_cnt);
842		printf("%s: tag %04x io status %#x\n", __func__,
843		       atio->tag_id, io->io_hdr.status);
844	}
845#endif
846	cam_fill_ctio(csio,
847		      /*retries*/ 2,
848		      ctlfedone,
849		      flags,
850		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
851		      atio->tag_id,
852		      atio->init_id,
853		      scsi_status,
854		      /*data_ptr*/ data_ptr,
855		      /*dxfer_len*/ dxfer_len,
856		      /*timeout*/ CTLFE_TIMEOUT * 1000);
857	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
858	start_ccb->ccb_h.ccb_atio = atio;
859	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
860		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
861	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
862
863	softc->ctios_sent++;
864	softc->refcount++;
865	cam_periph_unlock(periph);
866	xpt_action(start_ccb);
867	cam_periph_lock(periph);
868	softc->refcount--;
869
870	/*
871	 * If we still have work to do, ask for another CCB.
872	 */
873	if (!TAILQ_EMPTY(&softc->work_queue))
874		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
875}
876
877static void
878ctlfe_drain(void *context, int pending)
879{
880	struct cam_periph *periph = context;
881	struct ctlfe_lun_softc *softc = periph->softc;
882
883	cam_periph_lock(periph);
884	while (softc->refcount != 0) {
885		cam_periph_sleep(periph, &softc->refcount, PRIBIO,
886		    "ctlfe_drain", 1);
887	}
888	cam_periph_unlock(periph);
889	cam_periph_release(periph);
890}
891
892static void
893ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
894{
895	struct ctlfe_lun_softc *softc;
896	union ctl_io *io;
897	struct ctlfe_cmd_info *cmd_info;
898
899	softc = (struct ctlfe_lun_softc *)periph->softc;
900	io = ccb->ccb_h.io_ptr;
901
902	switch (ccb->ccb_h.func_code) {
903	case XPT_ACCEPT_TARGET_IO:
904		softc->atios_alloced--;
905		cmd_info = PRIV_INFO(io);
906		free(cmd_info, M_CTLFE);
907		break;
908	case XPT_IMMEDIATE_NOTIFY:
909	case XPT_NOTIFY_ACKNOWLEDGE:
910		softc->inots_alloced--;
911		break;
912	default:
913		break;
914	}
915
916	ctl_free_io(io);
917	free(ccb, M_CTLFE);
918
919	KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
920	    __func__, softc->atios_alloced));
921	KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
922	    __func__, softc->inots_alloced));
923
924	/*
925	 * If we have received all of our CCBs, we can release our
926	 * reference on the peripheral driver.  It will probably go away
927	 * now.
928	 */
929	if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
930		if (softc->refcount == 0) {
931			cam_periph_release_locked(periph);
932		} else {
933			TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
934			taskqueue_enqueue(taskqueue_thread,
935			    &softc->refdrain_task);
936		}
937	}
938}
939
940/*
941 * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
942 */
943static void
944ctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
945{
946	struct ctlfe_lun_softc *softc;
947	struct mtx *mtx;
948
949	if (periph->flags & CAM_PERIPH_INVALID) {
950		mtx = cam_periph_mtx(periph);
951		ctlfe_free_ccb(periph, ccb);
952		if (unlock)
953			mtx_unlock(mtx);
954		return;
955	}
956	if (unlock)
957		cam_periph_unlock(periph);
958
959	/*
960	 * For a wildcard attachment, commands can come in with a specific
961	 * target/lun.  Reset the target and LUN fields back to the wildcard
962	 * values before we send them back down to the SIM.
963	 */
964	softc = (struct ctlfe_lun_softc *)periph->softc;
965	if (softc->flags & CTLFE_LUN_WILDCARD) {
966		ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
967		ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
968	}
969
970	xpt_action(ccb);
971}
972
973static int
974ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
975{
976	uint64_t lba;
977	uint32_t num_blocks, nbc;
978	uint8_t *cmdbyt = atio_cdb_ptr(atio);
979
980	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
981
982	switch (cmdbyt[0]) {
983	case READ_6:
984	case WRITE_6:
985	{
986		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
987		lba = scsi_3btoul(cdb->addr);
988		lba &= 0x1fffff;
989		num_blocks = cdb->length;
990		if (num_blocks == 0)
991			num_blocks = 256;
992		lba += nbc;
993		num_blocks -= nbc;
994		scsi_ulto3b(lba, cdb->addr);
995		cdb->length = num_blocks;
996		break;
997	}
998	case READ_10:
999	case WRITE_10:
1000	{
1001		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1002		lba = scsi_4btoul(cdb->addr);
1003		num_blocks = scsi_2btoul(cdb->length);
1004		lba += nbc;
1005		num_blocks -= nbc;
1006		scsi_ulto4b(lba, cdb->addr);
1007		scsi_ulto2b(num_blocks, cdb->length);
1008		break;
1009	}
1010	case READ_12:
1011	case WRITE_12:
1012	{
1013		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1014		lba = scsi_4btoul(cdb->addr);
1015		num_blocks = scsi_4btoul(cdb->length);
1016		lba += nbc;
1017		num_blocks -= nbc;
1018		scsi_ulto4b(lba, cdb->addr);
1019		scsi_ulto4b(num_blocks, cdb->length);
1020		break;
1021	}
1022	case READ_16:
1023	case WRITE_16:
1024	{
1025		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1026		lba = scsi_8btou64(cdb->addr);
1027		num_blocks = scsi_4btoul(cdb->length);
1028		lba += nbc;
1029		num_blocks -= nbc;
1030		scsi_u64to8b(lba, cdb->addr);
1031		scsi_ulto4b(num_blocks, cdb->length);
1032		break;
1033	}
1034	default:
1035		return -1;
1036	}
1037	return (0);
1038}
1039
1040static void
1041ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1042{
1043	struct ctlfe_lun_softc *softc;
1044	struct ctlfe_softc *bus_softc;
1045	struct ctlfe_cmd_info *cmd_info;
1046	struct ccb_accept_tio *atio = NULL;
1047	union ctl_io *io = NULL;
1048	struct mtx *mtx;
1049	cam_status status;
1050
1051	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1052	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1053#ifdef CTLFE_DEBUG
1054	printf("%s: entered, func_code = %#x\n", __func__,
1055	       done_ccb->ccb_h.func_code);
1056#endif
1057
1058	/*
1059	 * At this point CTL has no known use case for device queue freezes.
1060	 * In case some SIM think different -- drop its freeze right here.
1061	 */
1062	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1063		cam_release_devq(periph->path,
1064				 /*relsim_flags*/0,
1065				 /*reduction*/0,
1066				 /*timeout*/0,
1067				 /*getcount_only*/0);
1068		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1069	}
1070
1071	softc = (struct ctlfe_lun_softc *)periph->softc;
1072	bus_softc = softc->parent_softc;
1073	mtx = cam_periph_mtx(periph);
1074	mtx_lock(mtx);
1075
1076	switch (done_ccb->ccb_h.func_code) {
1077	case XPT_ACCEPT_TARGET_IO: {
1078
1079		atio = &done_ccb->atio;
1080		status = atio->ccb_h.status & CAM_STATUS_MASK;
1081		if (status != CAM_CDB_RECVD) {
1082			ctlfe_free_ccb(periph, done_ccb);
1083			goto out;
1084		}
1085
1086 resubmit:
1087		/*
1088		 * Allocate a ctl_io, pass it to CTL, and wait for the
1089		 * datamove or done.
1090		 */
1091		mtx_unlock(mtx);
1092		io = done_ccb->ccb_h.io_ptr;
1093		cmd_info = PRIV_INFO(io);
1094		ctl_zero_io(io);
1095
1096		/* Save pointers on both sides */
1097		PRIV_CCB(io) = done_ccb;
1098		PRIV_INFO(io) = cmd_info;
1099		done_ccb->ccb_h.io_ptr = io;
1100
1101		/*
1102		 * Only SCSI I/O comes down this path, resets, etc. come
1103		 * down the immediate notify path below.
1104		 */
1105		io->io_hdr.io_type = CTL_IO_SCSI;
1106		io->io_hdr.nexus.initid = atio->init_id;
1107		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1108		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1109			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1110			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1111		} else {
1112			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1113		}
1114		io->scsiio.tag_num = atio->tag_id;
1115		switch (atio->tag_action) {
1116		case CAM_TAG_ACTION_NONE:
1117			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1118			break;
1119		case MSG_SIMPLE_TASK:
1120			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1121			break;
1122		case MSG_HEAD_OF_QUEUE_TASK:
1123        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1124			break;
1125		case MSG_ORDERED_TASK:
1126        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1127			break;
1128		case MSG_ACA_TASK:
1129			io->scsiio.tag_type = CTL_TAG_ACA;
1130			break;
1131		default:
1132			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1133			printf("%s: unhandled tag type %#x!!\n", __func__,
1134			       atio->tag_action);
1135			break;
1136		}
1137		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1138			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1139			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1140		}
1141		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1142		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1143
1144#ifdef CTLFEDEBUG
1145		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1146		        io->io_hdr.nexus.initid,
1147		        io->io_hdr.nexus.targ_port,
1148		        io->io_hdr.nexus.targ_lun,
1149			io->scsiio.tag_num, io->scsiio.cdb[0]);
1150#endif
1151
1152		ctl_queue(io);
1153		return;
1154	}
1155	case XPT_CONT_TARGET_IO: {
1156		int srr = 0;
1157		uint32_t srr_off = 0;
1158
1159		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1160		io = (union ctl_io *)atio->ccb_h.io_ptr;
1161
1162		softc->ctios_sent--;
1163#ifdef CTLFEDEBUG
1164		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1165		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1166#endif
1167		/*
1168		 * Handle SRR case were the data pointer is pushed back hack
1169		 */
1170		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1171		    && done_ccb->csio.msg_ptr != NULL
1172		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1173		    && done_ccb->csio.msg_ptr[1] == 5
1174       		    && done_ccb->csio.msg_ptr[2] == 0) {
1175			srr = 1;
1176			srr_off =
1177			    (done_ccb->csio.msg_ptr[3] << 24)
1178			    | (done_ccb->csio.msg_ptr[4] << 16)
1179			    | (done_ccb->csio.msg_ptr[5] << 8)
1180			    | (done_ccb->csio.msg_ptr[6]);
1181		}
1182
1183		/*
1184		 * If we have an SRR and we're still sending data, we
1185		 * should be able to adjust offsets and cycle again.
1186		 * It is possible only if offset is from this datamove.
1187		 */
1188		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1189		    srr_off >= io->scsiio.kern_rel_offset &&
1190		    srr_off < io->scsiio.kern_rel_offset +
1191		     io->scsiio.kern_data_len) {
1192			io->scsiio.kern_data_resid =
1193			    io->scsiio.kern_rel_offset +
1194			    io->scsiio.kern_data_len - srr_off;
1195			io->scsiio.ext_data_filled = srr_off;
1196			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1197			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1198			xpt_release_ccb(done_ccb);
1199			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1200					  periph_links.tqe);
1201			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1202			break;
1203		}
1204
1205		/*
1206		 * If status was being sent, the back end data is now history.
1207		 * Hack it up and resubmit a new command with the CDB adjusted.
1208		 * If the SIM does the right thing, all of the resid math
1209		 * should work.
1210		 */
1211		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1212			xpt_release_ccb(done_ccb);
1213			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1214				done_ccb = (union ccb *)atio;
1215				goto resubmit;
1216			}
1217			/*
1218			 * Fall through to doom....
1219			 */
1220		}
1221
1222		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1223		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1224			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1225
1226		/*
1227		 * If we were sending status back to the initiator, free up
1228		 * resources.  If we were doing a datamove, call the
1229		 * datamove done routine.
1230		 */
1231		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1232			/*
1233			 * If we asked to send sense data but it wasn't sent,
1234			 * queue the I/O back to CTL for later REQUEST SENSE.
1235			 */
1236			if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1237			    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1238			    (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1239			    (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1240				PRIV_INFO(io) = PRIV_INFO(
1241				    (union ctl_io *)atio->ccb_h.io_ptr);
1242				ctl_queue_sense(atio->ccb_h.io_ptr);
1243				atio->ccb_h.io_ptr = io;
1244			}
1245
1246			/* Abort ATIO if CTIO sending status has failed. */
1247			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1248			    CAM_REQ_CMP) {
1249				done_ccb->ccb_h.func_code = XPT_ABORT;
1250				done_ccb->cab.abort_ccb = (union ccb *)atio;
1251				xpt_action(done_ccb);
1252			}
1253
1254			xpt_release_ccb(done_ccb);
1255			ctlfe_requeue_ccb(periph, (union ccb *)atio,
1256			    /* unlock */1);
1257			return;
1258		} else {
1259			struct ctlfe_cmd_info *cmd_info;
1260			struct ccb_scsiio *csio;
1261
1262			csio = &done_ccb->csio;
1263			cmd_info = PRIV_INFO(io);
1264
1265			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1266
1267			/*
1268			 * Translate CAM status to CTL status.  Success
1269			 * does not change the overall, ctl_io status.  In
1270			 * that case we just set port_status to 0.  If we
1271			 * have a failure, though, set a data phase error
1272			 * for the overall ctl_io.
1273			 */
1274			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1275			case CAM_REQ_CMP:
1276				io->scsiio.kern_data_resid -=
1277				    csio->dxfer_len - csio->resid;
1278				io->io_hdr.port_status = 0;
1279				break;
1280			default:
1281				/*
1282				 * XXX KDM we probably need to figure out a
1283				 * standard set of errors that the SIM
1284				 * drivers should return in the event of a
1285				 * data transfer failure.  A data phase
1286				 * error will at least point the user to a
1287				 * data transfer error of some sort.
1288				 * Hopefully the SIM printed out some
1289				 * additional information to give the user
1290				 * a clue what happened.
1291				 */
1292				io->io_hdr.port_status = 0xbad1;
1293				ctl_set_data_phase_error(&io->scsiio);
1294				/*
1295				 * XXX KDM figure out residual.
1296				 */
1297				break;
1298			}
1299			/*
1300			 * If we had to break this S/G list into multiple
1301			 * pieces, figure out where we are in the list, and
1302			 * continue sending pieces if necessary.
1303			 */
1304			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1305			    io->io_hdr.port_status == 0 && csio->resid == 0) {
1306				ccb_flags flags;
1307				uint8_t *data_ptr;
1308				uint32_t dxfer_len;
1309
1310				flags = atio->ccb_h.flags &
1311					(CAM_DIS_DISCONNECT|
1312					 CAM_TAG_ACTION_VALID);
1313
1314				ctlfedata(softc, io, &flags, &data_ptr,
1315				    &dxfer_len, &csio->sglist_cnt);
1316
1317				if (((flags & CAM_SEND_STATUS) == 0)
1318				 && (dxfer_len == 0)) {
1319					printf("%s: tag %04x no status or "
1320					       "len cdb = %02x\n", __func__,
1321					       atio->tag_id,
1322					       atio_cdb_ptr(atio)[0]);
1323					printf("%s: tag %04x io status %#x\n",
1324					       __func__, atio->tag_id,
1325					       io->io_hdr.status);
1326				}
1327
1328				cam_fill_ctio(csio,
1329					      /*retries*/ 2,
1330					      ctlfedone,
1331					      flags,
1332					      (flags & CAM_TAG_ACTION_VALID) ?
1333					       MSG_SIMPLE_Q_TAG : 0,
1334					      atio->tag_id,
1335					      atio->init_id,
1336					      0,
1337					      /*data_ptr*/ data_ptr,
1338					      /*dxfer_len*/ dxfer_len,
1339					      CTLFE_TIMEOUT * 1000);
1340
1341				csio->ccb_h.flags |= CAM_UNLOCKED;
1342				csio->resid = 0;
1343				csio->ccb_h.ccb_atio = atio;
1344				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1345				softc->ctios_sent++;
1346				mtx_unlock(mtx);
1347				xpt_action((union ccb *)csio);
1348			} else {
1349				/*
1350				 * Release the CTIO.  The ATIO will be sent back
1351				 * down to the SIM once we send status.
1352				 */
1353				xpt_release_ccb(done_ccb);
1354				mtx_unlock(mtx);
1355
1356				/* Call the backend move done callback */
1357				io->scsiio.be_move_done(io);
1358			}
1359			return;
1360		}
1361		break;
1362	}
1363	case XPT_IMMEDIATE_NOTIFY: {
1364		union ctl_io *io;
1365		struct ccb_immediate_notify *inot;
1366		int send_ctl_io;
1367
1368		inot = &done_ccb->cin1;
1369		io = done_ccb->ccb_h.io_ptr;
1370		ctl_zero_io(io);
1371
1372		send_ctl_io = 1;
1373
1374		io->io_hdr.io_type = CTL_IO_TASK;
1375		PRIV_CCB(io) = done_ccb;
1376		inot->ccb_h.io_ptr = io;
1377		io->io_hdr.nexus.initid = inot->initiator_id;
1378		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1379		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1380			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1381			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1382		} else {
1383			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1384		}
1385		/* XXX KDM should this be the tag_id? */
1386		io->taskio.tag_num = inot->seq_id;
1387
1388		status = inot->ccb_h.status & CAM_STATUS_MASK;
1389		switch (status) {
1390		case CAM_SCSI_BUS_RESET:
1391			io->taskio.task_action = CTL_TASK_BUS_RESET;
1392			break;
1393		case CAM_BDR_SENT:
1394			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1395			break;
1396		case CAM_MESSAGE_RECV:
1397			switch (inot->arg) {
1398			case MSG_ABORT_TASK_SET:
1399				io->taskio.task_action =
1400				    CTL_TASK_ABORT_TASK_SET;
1401				break;
1402			case MSG_TARGET_RESET:
1403				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1404				break;
1405			case MSG_ABORT_TASK:
1406				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1407				break;
1408			case MSG_LOGICAL_UNIT_RESET:
1409				io->taskio.task_action = CTL_TASK_LUN_RESET;
1410				break;
1411			case MSG_CLEAR_TASK_SET:
1412				io->taskio.task_action =
1413				    CTL_TASK_CLEAR_TASK_SET;
1414				break;
1415			case MSG_CLEAR_ACA:
1416				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1417				break;
1418			case MSG_QUERY_TASK:
1419				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1420				break;
1421			case MSG_QUERY_TASK_SET:
1422				io->taskio.task_action =
1423				    CTL_TASK_QUERY_TASK_SET;
1424				break;
1425			case MSG_QUERY_ASYNC_EVENT:
1426				io->taskio.task_action =
1427				    CTL_TASK_QUERY_ASYNC_EVENT;
1428				break;
1429			case MSG_NOOP:
1430				send_ctl_io = 0;
1431				break;
1432			default:
1433				xpt_print(periph->path,
1434				    "%s: unsupported INOT message 0x%x\n",
1435				    __func__, inot->arg);
1436				send_ctl_io = 0;
1437				break;
1438			}
1439			break;
1440		default:
1441			xpt_print(periph->path,
1442			    "%s: unsupported INOT status 0x%x\n",
1443			    __func__, status);
1444			/* FALLTHROUGH */
1445		case CAM_REQ_ABORTED:
1446		case CAM_REQ_INVALID:
1447		case CAM_DEV_NOT_THERE:
1448		case CAM_PROVIDE_FAIL:
1449			ctlfe_free_ccb(periph, done_ccb);
1450			goto out;
1451		}
1452		if (send_ctl_io != 0) {
1453			ctl_queue(io);
1454		} else {
1455			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1456			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1457			xpt_action(done_ccb);
1458		}
1459		break;
1460	}
1461	case XPT_NOTIFY_ACKNOWLEDGE:
1462		/* Queue this back down to the SIM as an immediate notify. */
1463		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1464		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1465		ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1466		return;
1467	case XPT_SET_SIM_KNOB:
1468	case XPT_GET_SIM_KNOB:
1469		break;
1470	default:
1471		panic("%s: unexpected CCB type %#x", __func__,
1472		      done_ccb->ccb_h.func_code);
1473		break;
1474	}
1475
1476out:
1477	mtx_unlock(mtx);
1478}
1479
1480static void
1481ctlfe_onoffline(void *arg, int online)
1482{
1483	struct ctlfe_softc *bus_softc;
1484	union ccb *ccb;
1485	cam_status status;
1486	struct cam_path *path;
1487	int set_wwnn;
1488
1489	bus_softc = (struct ctlfe_softc *)arg;
1490
1491	set_wwnn = 0;
1492
1493	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1494		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1495	if (status != CAM_REQ_CMP) {
1496		printf("%s: unable to create path!\n", __func__);
1497		return;
1498	}
1499	ccb = xpt_alloc_ccb();
1500	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1501	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1502	xpt_action(ccb);
1503
1504	/*
1505	 * Copan WWN format:
1506	 *
1507	 * Bits 63-60:	0x5		NAA, IEEE registered name
1508	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1509	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1510	 * Bits 11-8:			Type of port:
1511	 *					1 == N-Port
1512	 *					2 == F-Port
1513	 *					3 == NL-Port
1514	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1515	 */
1516	if (online != 0) {
1517		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1518
1519			printf("%s: %s current WWNN %#jx\n", __func__,
1520			       bus_softc->port_name,
1521			       ccb->knob.xport_specific.fc.wwnn);
1522			printf("%s: %s current WWPN %#jx\n", __func__,
1523			       bus_softc->port_name,
1524			       ccb->knob.xport_specific.fc.wwpn);
1525
1526			/*
1527			 * If the user has specified a WWNN/WWPN, send them
1528			 * down to the SIM.  Otherwise, record what the SIM
1529			 * has reported.
1530			 */
1531			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1532			    != ccb->knob.xport_specific.fc.wwnn) {
1533				ccb->knob.xport_specific.fc.wwnn =
1534				    bus_softc->port.wwnn;
1535				set_wwnn = 1;
1536			} else {
1537				ctl_port_set_wwns(&bus_softc->port,
1538				    true, ccb->knob.xport_specific.fc.wwnn,
1539				    false, 0);
1540			}
1541			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1542			     != ccb->knob.xport_specific.fc.wwpn) {
1543				ccb->knob.xport_specific.fc.wwpn =
1544				    bus_softc->port.wwpn;
1545				set_wwnn = 1;
1546			} else {
1547				ctl_port_set_wwns(&bus_softc->port,
1548				    false, 0,
1549				    true, ccb->knob.xport_specific.fc.wwpn);
1550			}
1551
1552
1553			if (set_wwnn != 0) {
1554				printf("%s: %s new WWNN %#jx\n", __func__,
1555				       bus_softc->port_name,
1556				ccb->knob.xport_specific.fc.wwnn);
1557				printf("%s: %s new WWPN %#jx\n", __func__,
1558				       bus_softc->port_name,
1559				       ccb->knob.xport_specific.fc.wwpn);
1560			}
1561		} else {
1562			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1563			       bus_softc->port_name);
1564		}
1565	}
1566	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1567	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1568	if (set_wwnn != 0)
1569		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1570
1571	if (online != 0)
1572		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1573	else
1574		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1575
1576	xpt_action(ccb);
1577
1578	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1579		printf("%s: SIM %s (path id %d) target %s failed with "
1580		       "status %#x\n",
1581		       __func__, bus_softc->port_name, bus_softc->path_id,
1582		       (online != 0) ? "enable" : "disable",
1583		       ccb->ccb_h.status);
1584	} else {
1585		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1586		       __func__, bus_softc->port_name, bus_softc->path_id,
1587		       (online != 0) ? "enable" : "disable");
1588	}
1589
1590	xpt_free_path(path);
1591	xpt_free_ccb(ccb);
1592}
1593
1594static void
1595ctlfe_online(void *arg)
1596{
1597	struct ctlfe_softc *bus_softc;
1598	struct cam_path *path;
1599	cam_status status;
1600	struct ctlfe_lun_softc *lun_softc;
1601	struct cam_periph *periph;
1602
1603	bus_softc = (struct ctlfe_softc *)arg;
1604
1605	/*
1606	 * Create the wildcard LUN before bringing the port online.
1607	 */
1608	status = xpt_create_path(&path, /*periph*/ NULL,
1609				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1610				 CAM_LUN_WILDCARD);
1611	if (status != CAM_REQ_CMP) {
1612		printf("%s: unable to create path for wildcard periph\n",
1613				__func__);
1614		return;
1615	}
1616
1617	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1618
1619	xpt_path_lock(path);
1620	periph = cam_periph_find(path, "ctl");
1621	if (periph != NULL) {
1622		/* We've already got a periph, no need to alloc a new one. */
1623		xpt_path_unlock(path);
1624		xpt_free_path(path);
1625		free(lun_softc, M_CTLFE);
1626		return;
1627	}
1628	lun_softc->parent_softc = bus_softc;
1629	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1630
1631	status = cam_periph_alloc(ctlferegister,
1632				  ctlfeoninvalidate,
1633				  ctlfecleanup,
1634				  ctlfestart,
1635				  "ctl",
1636				  CAM_PERIPH_BIO,
1637				  path,
1638				  ctlfeasync,
1639				  0,
1640				  lun_softc);
1641
1642	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1643		const struct cam_status_entry *entry;
1644
1645		entry = cam_fetch_status_entry(status);
1646		printf("%s: CAM error %s (%#x) returned from "
1647		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1648		       entry->status_text : "Unknown", status);
1649		free(lun_softc, M_CTLFE);
1650	}
1651
1652	xpt_path_unlock(path);
1653	ctlfe_onoffline(arg, /*online*/ 1);
1654	xpt_free_path(path);
1655}
1656
1657static void
1658ctlfe_offline(void *arg)
1659{
1660	struct ctlfe_softc *bus_softc;
1661	struct cam_path *path;
1662	cam_status status;
1663	struct cam_periph *periph;
1664
1665	bus_softc = (struct ctlfe_softc *)arg;
1666
1667	ctlfe_onoffline(arg, /*online*/ 0);
1668
1669	/*
1670	 * Disable the wildcard LUN for this port now that we have taken
1671	 * the port offline.
1672	 */
1673	status = xpt_create_path(&path, /*periph*/ NULL,
1674				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1675				 CAM_LUN_WILDCARD);
1676	if (status != CAM_REQ_CMP) {
1677		printf("%s: unable to create path for wildcard periph\n",
1678		       __func__);
1679		return;
1680	}
1681	xpt_path_lock(path);
1682	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1683		cam_periph_invalidate(periph);
1684	xpt_path_unlock(path);
1685	xpt_free_path(path);
1686}
1687
1688/*
1689 * This will get called to enable a LUN on every bus that is attached to
1690 * CTL.  So we only need to create a path/periph for this particular bus.
1691 */
1692static int
1693ctlfe_lun_enable(void *arg, int lun_id)
1694{
1695	struct ctlfe_softc *bus_softc;
1696	struct ctlfe_lun_softc *softc;
1697	struct cam_path *path;
1698	struct cam_periph *periph;
1699	cam_status status;
1700
1701	bus_softc = (struct ctlfe_softc *)arg;
1702	if (bus_softc->hba_misc & PIM_EXTLUNS)
1703		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1704
1705	status = xpt_create_path(&path, /*periph*/ NULL,
1706	    bus_softc->path_id, bus_softc->target_id, lun_id);
1707	/* XXX KDM need some way to return status to CTL here? */
1708	if (status != CAM_REQ_CMP) {
1709		printf("%s: could not create path, status %#x\n", __func__,
1710		       status);
1711		return (1);
1712	}
1713
1714	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1715	xpt_path_lock(path);
1716	periph = cam_periph_find(path, "ctl");
1717	if (periph != NULL) {
1718		/* We've already got a periph, no need to alloc a new one. */
1719		xpt_path_unlock(path);
1720		xpt_free_path(path);
1721		free(softc, M_CTLFE);
1722		return (0);
1723	}
1724	softc->parent_softc = bus_softc;
1725
1726	status = cam_periph_alloc(ctlferegister,
1727				  ctlfeoninvalidate,
1728				  ctlfecleanup,
1729				  ctlfestart,
1730				  "ctl",
1731				  CAM_PERIPH_BIO,
1732				  path,
1733				  ctlfeasync,
1734				  0,
1735				  softc);
1736
1737	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1738		const struct cam_status_entry *entry;
1739
1740		entry = cam_fetch_status_entry(status);
1741		printf("%s: CAM error %s (%#x) returned from "
1742		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1743		       entry->status_text : "Unknown", status);
1744		free(softc, M_CTLFE);
1745	}
1746
1747	xpt_path_unlock(path);
1748	xpt_free_path(path);
1749	return (0);
1750}
1751
1752/*
1753 * This will get called when the user removes a LUN to disable that LUN
1754 * on every bus that is attached to CTL.
1755 */
1756static int
1757ctlfe_lun_disable(void *arg, int lun_id)
1758{
1759	struct ctlfe_softc *softc;
1760	struct ctlfe_lun_softc *lun_softc;
1761
1762	softc = (struct ctlfe_softc *)arg;
1763	if (softc->hba_misc & PIM_EXTLUNS)
1764		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1765
1766	mtx_lock(&softc->lun_softc_mtx);
1767	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1768		struct cam_path *path;
1769
1770		path = lun_softc->periph->path;
1771
1772		if ((xpt_path_target_id(path) == softc->target_id)
1773		 && (xpt_path_lun_id(path) == lun_id)) {
1774			break;
1775		}
1776	}
1777	if (lun_softc == NULL) {
1778		mtx_unlock(&softc->lun_softc_mtx);
1779		printf("%s: can't find lun %d\n", __func__, lun_id);
1780		return (1);
1781	}
1782	cam_periph_acquire(lun_softc->periph);
1783	mtx_unlock(&softc->lun_softc_mtx);
1784
1785	cam_periph_lock(lun_softc->periph);
1786	cam_periph_invalidate(lun_softc->periph);
1787	cam_periph_unlock(lun_softc->periph);
1788	cam_periph_release(lun_softc->periph);
1789	return (0);
1790}
1791
1792static void
1793ctlfe_dump_sim(struct cam_sim *sim)
1794{
1795
1796	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1797	       sim->sim_name, sim->unit_number,
1798	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1799}
1800
1801/*
1802 * Assumes that the SIM lock is held.
1803 */
1804static void
1805ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1806{
1807	struct ccb_hdr *hdr;
1808	struct cam_periph *periph;
1809	int num_items;
1810
1811	periph = softc->periph;
1812	num_items = 0;
1813
1814	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1815		union ctl_io *io = hdr->io_ptr;
1816
1817		num_items++;
1818
1819		/*
1820		 * Only regular SCSI I/O is put on the work
1821		 * queue, so we can print sense here.  There may be no
1822		 * sense if it's no the queue for a DMA, but this serves to
1823		 * print out the CCB as well.
1824		 *
1825		 * XXX KDM switch this over to scsi_sense_print() when
1826		 * CTL is merged in with CAM.
1827		 */
1828		ctl_io_error_print(io, NULL);
1829
1830		/*
1831		 * Print DMA status if we are DMA_QUEUED.
1832		 */
1833		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1834			xpt_print(periph->path,
1835			    "Total %u, Current %u, Resid %u\n",
1836			    io->scsiio.kern_total_len,
1837			    io->scsiio.kern_data_len,
1838			    io->scsiio.kern_data_resid);
1839		}
1840	}
1841
1842	xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1843	xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1844}
1845
1846/*
1847 * Datamove/done routine called by CTL.  Put ourselves on the queue to
1848 * receive a CCB from CAM so we can queue the continue I/O request down
1849 * to the adapter.
1850 */
1851static void
1852ctlfe_datamove(union ctl_io *io)
1853{
1854	union ccb *ccb;
1855	struct cam_periph *periph;
1856	struct ctlfe_lun_softc *softc;
1857
1858	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1859	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1860
1861	io->scsiio.ext_data_filled = 0;
1862	ccb = PRIV_CCB(io);
1863	periph = xpt_path_periph(ccb->ccb_h.path);
1864	cam_periph_lock(periph);
1865	softc = (struct ctlfe_lun_softc *)periph->softc;
1866	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1867	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1868		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1869	TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1870			  periph_links.tqe);
1871	xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1872	cam_periph_unlock(periph);
1873}
1874
1875static void
1876ctlfe_done(union ctl_io *io)
1877{
1878	union ccb *ccb;
1879	struct cam_periph *periph;
1880	struct ctlfe_lun_softc *softc;
1881
1882	ccb = PRIV_CCB(io);
1883	periph = xpt_path_periph(ccb->ccb_h.path);
1884	cam_periph_lock(periph);
1885	softc = (struct ctlfe_lun_softc *)periph->softc;
1886
1887	if (io->io_hdr.io_type == CTL_IO_TASK) {
1888		/*
1889		 * Send the notify acknowledge down to the SIM, to let it
1890		 * know we processed the task management command.
1891		 */
1892		ccb->ccb_h.status = CAM_REQ_INPROG;
1893		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1894		switch (io->taskio.task_status) {
1895		case CTL_TASK_FUNCTION_COMPLETE:
1896			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1897			break;
1898		case CTL_TASK_FUNCTION_SUCCEEDED:
1899			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1900			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1901			break;
1902		case CTL_TASK_FUNCTION_REJECTED:
1903			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1904			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1905			break;
1906		case CTL_TASK_LUN_DOES_NOT_EXIST:
1907			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1908			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1909			break;
1910		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1911			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1912			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1913			break;
1914		}
1915		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1916		xpt_action(ccb);
1917	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1918		ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1919		return;
1920	} else {
1921		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1922		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1923				  periph_links.tqe);
1924		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1925	}
1926
1927	cam_periph_unlock(periph);
1928}
1929
1930static void
1931ctlfe_dump(void)
1932{
1933	struct ctlfe_softc *bus_softc;
1934	struct ctlfe_lun_softc *lun_softc;
1935
1936	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1937		ctlfe_dump_sim(bus_softc->sim);
1938		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1939			ctlfe_dump_queue(lun_softc);
1940	}
1941}
1942