scsi_ctl.c revision 315889
1229997Sken/*-
2229997Sken * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3290776Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
4229997Sken * All rights reserved.
5229997Sken *
6229997Sken * Redistribution and use in source and binary forms, with or without
7229997Sken * modification, are permitted provided that the following conditions
8229997Sken * are met:
9229997Sken * 1. Redistributions of source code must retain the above copyright
10229997Sken *    notice, this list of conditions, and the following disclaimer,
11229997Sken *    without modification.
12229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
14229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
15229997Sken *    including a substantially similar Disclaimer requirement for further
16229997Sken *    binary redistribution.
17229997Sken *
18229997Sken * NO WARRANTY
19229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29229997Sken * POSSIBILITY OF SUCH DAMAGES.
30229997Sken *
31229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
32229997Sken */
33229997Sken/*
34229997Sken * Peripheral driver interface between CAM and CTL (CAM Target Layer).
35229997Sken *
36229997Sken * Author: Ken Merry <ken@FreeBSD.org>
37229997Sken */
38229997Sken
39229997Sken#include <sys/cdefs.h>
40229997Sken__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/scsi_ctl.c 315889 2017-03-24 07:02:03Z mav $");
41229997Sken
42229997Sken#include <sys/param.h>
43229997Sken#include <sys/queue.h>
44229997Sken#include <sys/systm.h>
45229997Sken#include <sys/kernel.h>
46229997Sken#include <sys/lock.h>
47229997Sken#include <sys/mutex.h>
48229997Sken#include <sys/condvar.h>
49229997Sken#include <sys/malloc.h>
50229997Sken#include <sys/bus.h>
51229997Sken#include <sys/endian.h>
52229997Sken#include <sys/sbuf.h>
53229997Sken#include <sys/sysctl.h>
54229997Sken#include <sys/types.h>
55229997Sken#include <sys/systm.h>
56314749Smav#include <sys/taskqueue.h>
57229997Sken#include <machine/bus.h>
58229997Sken
59229997Sken#include <cam/cam.h>
60229997Sken#include <cam/cam_ccb.h>
61229997Sken#include <cam/cam_periph.h>
62229997Sken#include <cam/cam_queue.h>
63229997Sken#include <cam/cam_xpt_periph.h>
64229997Sken#include <cam/cam_debug.h>
65229997Sken#include <cam/cam_sim.h>
66229997Sken#include <cam/cam_xpt.h>
67229997Sken
68229997Sken#include <cam/scsi/scsi_all.h>
69229997Sken#include <cam/scsi/scsi_message.h>
70229997Sken
71229997Sken#include <cam/ctl/ctl_io.h>
72229997Sken#include <cam/ctl/ctl.h>
73229997Sken#include <cam/ctl/ctl_frontend.h>
74229997Sken#include <cam/ctl/ctl_util.h>
75229997Sken#include <cam/ctl/ctl_error.h>
76229997Sken
77229997Skenstruct ctlfe_softc {
78290776Smav	struct ctl_port	port;
79290776Smav	path_id_t	path_id;
80290776Smav	target_id_t	target_id;
81290776Smav	uint32_t	hba_misc;
82290776Smav	u_int		maxio;
83229997Sken	struct cam_sim *sim;
84290776Smav	char		port_name[DEV_IDLEN];
85290776Smav	struct mtx	lun_softc_mtx;
86229997Sken	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
87229997Sken	STAILQ_ENTRY(ctlfe_softc) links;
88229997Sken};
89229997Sken
90229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
91229997Skenstruct mtx ctlfe_list_mtx;
92229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist";
93229997Sken
94229997Skentypedef enum {
95229997Sken	CTLFE_LUN_NONE		= 0x00,
96229997Sken	CTLFE_LUN_WILDCARD	= 0x01
97229997Sken} ctlfe_lun_flags;
98229997Sken
99229997Skenstruct ctlfe_lun_softc {
100229997Sken	struct ctlfe_softc *parent_softc;
101229997Sken	struct cam_periph *periph;
102229997Sken	ctlfe_lun_flags flags;
103314749Smav	int	 ctios_sent;		/* Number of active CTIOs */
104314749Smav	int	 refcount;		/* Number of active xpt_action() */
105314749Smav	int	 atios_alloced;		/* Number of ATIOs not freed */
106314749Smav	int	 inots_alloced;		/* Number of INOTs not freed */
107314749Smav	struct task	refdrain_task;
108315887Smav	STAILQ_HEAD(, ccb_hdr) work_queue;
109315889Smav	LIST_HEAD(, ccb_hdr) atio_list;	/* List of ATIOs queued to SIM. */
110315889Smav	LIST_HEAD(, ccb_hdr) inot_list;	/* List of INOTs queued to SIM. */
111229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
112229997Sken};
113229997Sken
114229997Skentypedef enum {
115229997Sken	CTLFE_CMD_NONE		= 0x00,
116229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
117229997Sken} ctlfe_cmd_flags;
118229997Sken
119288723Smavstruct ctlfe_cmd_info {
120229997Sken	int cur_transfer_index;
121265641Smav	size_t cur_transfer_off;
122229997Sken	ctlfe_cmd_flags flags;
123229997Sken	/*
124229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
125229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
126229997Sken	 * i386 and 512 bytes on amd64.
127229997Sken	 */
128265641Smav#define CTLFE_MAX_SEGS	32
129265641Smav	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
130229997Sken};
131229997Sken
132229997Sken/*
133229997Sken * When we register the adapter/bus, request that this many ctl_ios be
134229997Sken * allocated.  This should be the maximum supported by the adapter, but we
135229997Sken * currently don't have a way to get that back from the path inquiry.
136229997Sken * XXX KDM add that to the path inquiry.
137229997Sken */
138229997Sken#define	CTLFE_REQ_CTL_IO	4096
139229997Sken/*
140229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
141229997Sken * adapter per LUN.
142229997Sken * XXX KDM should this be controlled by CTL?
143229997Sken */
144229997Sken#define	CTLFE_ATIO_PER_LUN	1024
145229997Sken/*
146229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
147229997Sken * allocate and queue down to the adapter per LUN.
148229997Sken * XXX KDM should this be controlled by CTL?
149229997Sken */
150229997Sken#define	CTLFE_IN_PER_LUN	1024
151229997Sken
152229997Sken/*
153314751Smav * Timeout (in seconds) on CTIO CCB doing DMA or sending status
154229997Sken */
155314751Smav#define	CTLFE_TIMEOUT	5
156229997Sken
157229997Sken/*
158229997Sken * Turn this on to enable extra debugging prints.
159229997Sken */
160229997Sken#if 0
161229997Sken#define	CTLFE_DEBUG
162229997Sken#endif
163229997Sken
164229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
165229997Sken
166275878Smav#define	io_ptr		ppriv_ptr0
167229997Sken
168229997Sken/* This is only used in the CTIO */
169229997Sken#define	ccb_atio	ppriv_ptr1
170229997Sken
171312585Smav#define PRIV_CCB(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
172312585Smav#define PRIV_INFO(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
173312585Smav
174313369Smavstatic int		ctlfeinitialize(void);
175313369Smavstatic int		ctlfeshutdown(void);
176268677Smavstatic periph_init_t	ctlfeperiphinit;
177229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
178229997Sken				   struct cam_path *path, void *arg);
179229997Skenstatic periph_ctor_t	ctlferegister;
180229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
181229997Skenstatic periph_dtor_t	ctlfecleanup;
182229997Skenstatic periph_start_t	ctlfestart;
183229997Skenstatic void		ctlfedone(struct cam_periph *periph,
184229997Sken				  union ccb *done_ccb);
185229997Sken
186229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
187229997Skenstatic void 		ctlfe_online(void *arg);
188229997Skenstatic void 		ctlfe_offline(void *arg);
189284798Smavstatic int 		ctlfe_lun_enable(void *arg, int lun_id);
190284798Smavstatic int 		ctlfe_lun_disable(void *arg, int lun_id);
191229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
192229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
193275880Smavstatic void 		ctlfe_datamove(union ctl_io *io);
194275880Smavstatic void 		ctlfe_done(union ctl_io *io);
195229997Skenstatic void 		ctlfe_dump(void);
196314739Smavstatic void		ctlfe_free_ccb(struct cam_periph *periph,
197314739Smav			    union ccb *ccb);
198314739Smavstatic void		ctlfe_requeue_ccb(struct cam_periph *periph,
199314739Smav			    union ccb *ccb, int unlock);
200229997Sken
201229997Skenstatic struct periph_driver ctlfe_driver =
202229997Sken{
203268677Smav	ctlfeperiphinit, "ctl",
204273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
205273316Smav	CAM_PERIPH_DRV_EARLY
206229997Sken};
207229997Sken
208268677Smavstatic struct ctl_frontend ctlfe_frontend =
209268677Smav{
210273318Smav	.name = "camtgt",
211268677Smav	.init = ctlfeinitialize,
212268677Smav	.fe_dump = ctlfe_dump,
213268677Smav	.shutdown = ctlfeshutdown,
214249009Strasz};
215268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
216249009Strasz
217313369Smavstatic int
218229997Skenctlfeshutdown(void)
219229997Sken{
220313369Smav
221313369Smav	/* CAM does not support periph driver unregister now. */
222313369Smav	return (EBUSY);
223229997Sken}
224229997Sken
225313369Smavstatic int
226268677Smavctlfeinitialize(void)
227229997Sken{
228229997Sken
229229997Sken	STAILQ_INIT(&ctlfe_softc_list);
230229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
231268677Smav	periphdriver_register(&ctlfe_driver);
232268677Smav	return (0);
233268677Smav}
234229997Sken
235313369Smavstatic void
236268677Smavctlfeperiphinit(void)
237268677Smav{
238268677Smav	cam_status status;
239229997Sken
240229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
241229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
242229997Sken	if (status != CAM_REQ_CMP) {
243229997Sken		printf("ctl: Failed to attach async callback due to CAM "
244229997Sken		       "status 0x%x!\n", status);
245229997Sken	}
246229997Sken}
247229997Sken
248229997Skenstatic void
249229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
250229997Sken{
251273317Smav	struct ctlfe_softc *softc;
252229997Sken
253229997Sken#ifdef CTLFEDEBUG
254229997Sken	printf("%s: entered\n", __func__);
255229997Sken#endif
256229997Sken
257273317Smav	mtx_lock(&ctlfe_list_mtx);
258273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
259273317Smav		if (softc->path_id == xpt_path_path_id(path))
260273317Smav			break;
261273317Smav	}
262273317Smav	mtx_unlock(&ctlfe_list_mtx);
263273317Smav
264229997Sken	/*
265229997Sken	 * When a new path gets registered, and it is capable of target
266229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
267229997Sken	 * selective, but for now this will be sufficient.
268229997Sken 	 */
269229997Sken	switch (code) {
270229997Sken	case AC_PATH_REGISTERED: {
271268677Smav		struct ctl_port *port;
272229997Sken		struct ccb_pathinq *cpi;
273229997Sken		int retval;
274229997Sken
275229997Sken		cpi = (struct ccb_pathinq *)arg;
276229997Sken
277229997Sken		/* Don't attach if it doesn't support target mode */
278229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
279230033Sken#ifdef CTLFEDEBUG
280229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
281229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
282230033Sken#endif
283229997Sken			break;
284229997Sken		}
285229997Sken
286273317Smav		if (softc != NULL) {
287273317Smav#ifdef CTLFEDEBUG
288273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
289273317Smav			       __func__, xpt_path_path_id(path));
290273317Smav#endif
291273317Smav			break;
292273317Smav		}
293273317Smav
294229997Sken		/*
295229997Sken		 * We're in an interrupt context here, so we have to
296229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
297229997Sken		 * can't allocate memory.
298229997Sken		 */
299273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
300273317Smav		if (softc == NULL) {
301229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
302273317Smav			       __func__, sizeof(*softc));
303229997Sken			return;
304229997Sken		}
305229997Sken
306273317Smav		softc->path_id = cpi->ccb_h.path_id;
307288713Smav		softc->target_id = cpi->initiator_id;
308273317Smav		softc->sim = xpt_path_sim(path);
309290776Smav		softc->hba_misc = cpi->hba_misc;
310265641Smav		if (cpi->maxio != 0)
311273317Smav			softc->maxio = cpi->maxio;
312265641Smav		else
313273317Smav			softc->maxio = DFLTPHYS;
314273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
315273317Smav		STAILQ_INIT(&softc->lun_softc_list);
316229997Sken
317273317Smav		port = &softc->port;
318268677Smav		port->frontend = &ctlfe_frontend;
319229997Sken
320229997Sken		/*
321229997Sken		 * XXX KDM should we be more accurate here ?
322229997Sken		 */
323229997Sken		if (cpi->transport == XPORT_FC)
324268677Smav			port->port_type = CTL_PORT_FC;
325268694Smav		else if (cpi->transport == XPORT_SAS)
326268694Smav			port->port_type = CTL_PORT_SAS;
327229997Sken		else
328268677Smav			port->port_type = CTL_PORT_SCSI;
329229997Sken
330229997Sken		/* XXX KDM what should the real number be here? */
331314751Smav		port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
332273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
333229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
334229997Sken		/*
335229997Sken		 * XXX KDM it would be nice to allocate storage in the
336229997Sken		 * frontend structure itself.
337229997Sken	 	 */
338273317Smav		port->port_name = softc->port_name;
339273319Smav		port->physical_port = cpi->bus_id;
340273319Smav		port->virtual_port = 0;
341268677Smav		port->port_online = ctlfe_online;
342268677Smav		port->port_offline = ctlfe_offline;
343273317Smav		port->onoff_arg = softc;
344268677Smav		port->lun_enable = ctlfe_lun_enable;
345268677Smav		port->lun_disable = ctlfe_lun_disable;
346273317Smav		port->targ_lun_arg = softc;
347275880Smav		port->fe_datamove = ctlfe_datamove;
348275880Smav		port->fe_done = ctlfe_done;
349229997Sken		/*
350229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
351229997Sken		 * number of targets supported.
352229997Sken		 */
353268677Smav		port->max_targets = cpi->max_target;
354268677Smav		port->max_target_id = cpi->max_target;
355288732Smav		port->targ_port = -1;
356314751Smav
357275493Smav		retval = ctl_port_register(port);
358229997Sken		if (retval != 0) {
359268677Smav			printf("%s: ctl_port_register() failed with "
360229997Sken			       "error %d!\n", __func__, retval);
361273317Smav			mtx_destroy(&softc->lun_softc_mtx);
362273317Smav			free(softc, M_CTLFE);
363229997Sken			break;
364229997Sken		} else {
365229997Sken			mtx_lock(&ctlfe_list_mtx);
366273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
367229997Sken			mtx_unlock(&ctlfe_list_mtx);
368229997Sken		}
369229997Sken
370245228Sken		break;
371245228Sken	}
372245228Sken	case AC_PATH_DEREGISTERED: {
373245228Sken
374245228Sken		if (softc != NULL) {
375245228Sken			/*
376245228Sken			 * XXX KDM are we certain at this point that there
377245228Sken			 * are no outstanding commands for this frontend?
378245228Sken			 */
379273317Smav			mtx_lock(&ctlfe_list_mtx);
380273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
381273317Smav			    links);
382273317Smav			mtx_unlock(&ctlfe_list_mtx);
383268677Smav			ctl_port_deregister(&softc->port);
384260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
385245228Sken			free(softc, M_CTLFE);
386229997Sken		}
387229997Sken		break;
388229997Sken	}
389229997Sken	case AC_CONTRACT: {
390229997Sken		struct ac_contract *ac;
391229997Sken
392229997Sken		ac = (struct ac_contract *)arg;
393229997Sken
394229997Sken		switch (ac->contract_number) {
395229997Sken		case AC_CONTRACT_DEV_CHG: {
396229997Sken			struct ac_device_changed *dev_chg;
397273317Smav			int retval;
398229997Sken
399229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
400229997Sken
401236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
402229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
403229997Sken			       xpt_path_path_id(path), dev_chg->target,
404229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
405229997Sken
406273317Smav			if (softc == NULL) {
407229997Sken				printf("%s: CTL port for CAM path %u not "
408229997Sken				       "found!\n", __func__,
409229997Sken				       xpt_path_path_id(path));
410229997Sken				break;
411229997Sken			}
412229997Sken			if (dev_chg->arrived != 0) {
413268692Smav				retval = ctl_add_initiator(&softc->port,
414268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
415229997Sken			} else {
416268692Smav				retval = ctl_remove_initiator(&softc->port,
417268692Smav				    dev_chg->target);
418229997Sken			}
419229997Sken
420268692Smav			if (retval < 0) {
421229997Sken				printf("%s: could not %s port %d iid %u "
422229997Sken				       "WWPN %#jx!\n", __func__,
423229997Sken				       (dev_chg->arrived != 0) ? "add" :
424268677Smav				       "remove", softc->port.targ_port,
425229997Sken				       dev_chg->target,
426229997Sken				       (uintmax_t)dev_chg->wwpn);
427229997Sken			}
428229997Sken			break;
429229997Sken		}
430229997Sken		default:
431229997Sken			printf("%s: unsupported contract number %ju\n",
432229997Sken			       __func__, (uintmax_t)ac->contract_number);
433229997Sken			break;
434229997Sken		}
435229997Sken		break;
436229997Sken	}
437229997Sken	default:
438229997Sken		break;
439229997Sken	}
440229997Sken}
441229997Sken
442229997Skenstatic cam_status
443229997Skenctlferegister(struct cam_periph *periph, void *arg)
444229997Sken{
445229997Sken	struct ctlfe_softc *bus_softc;
446229997Sken	struct ctlfe_lun_softc *softc;
447315889Smav	union ccb ccb;
448229997Sken	cam_status status;
449229997Sken	int i;
450229997Sken
451229997Sken	softc = (struct ctlfe_lun_softc *)arg;
452229997Sken	bus_softc = softc->parent_softc;
453229997Sken
454315887Smav	STAILQ_INIT(&softc->work_queue);
455315889Smav	LIST_INIT(&softc->atio_list);
456315889Smav	LIST_INIT(&softc->inot_list);
457229997Sken	softc->periph = periph;
458229997Sken	periph->softc = softc;
459229997Sken
460315889Smav	xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
461315889Smav	ccb.ccb_h.func_code = XPT_EN_LUN;
462315889Smav	ccb.cel.grp6_len = 0;
463315889Smav	ccb.cel.grp7_len = 0;
464315889Smav	ccb.cel.enable = 1;
465315889Smav	xpt_action(&ccb);
466315889Smav	status = (ccb.ccb_h.status & CAM_STATUS_MASK);
467229997Sken	if (status != CAM_REQ_CMP) {
468229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
469315889Smav			  __func__, ccb.ccb_h.status);
470229997Sken		return (status);
471229997Sken	}
472229997Sken
473229997Sken	status = CAM_REQ_CMP;
474229997Sken
475229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
476229997Sken		union ccb *new_ccb;
477275878Smav		union ctl_io *new_io;
478288723Smav		struct ctlfe_cmd_info *cmd_info;
479229997Sken
480229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
481236426Smjacob					      M_ZERO|M_NOWAIT);
482229997Sken		if (new_ccb == NULL) {
483229997Sken			status = CAM_RESRC_UNAVAIL;
484229997Sken			break;
485229997Sken		}
486275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
487275878Smav		if (new_io == NULL) {
488275878Smav			free(new_ccb, M_CTLFE);
489275878Smav			status = CAM_RESRC_UNAVAIL;
490275878Smav			break;
491275878Smav		}
492288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
493288723Smav		    M_ZERO | M_NOWAIT);
494288723Smav		if (cmd_info == NULL) {
495288723Smav			ctl_free_io(new_io);
496288723Smav			free(new_ccb, M_CTLFE);
497288723Smav			status = CAM_RESRC_UNAVAIL;
498288723Smav			break;
499288723Smav		}
500312585Smav		PRIV_INFO(new_io) = cmd_info;
501284793Smav		softc->atios_alloced++;
502275878Smav		new_ccb->ccb_h.io_ptr = new_io;
503315889Smav		LIST_INSERT_HEAD(&softc->atio_list, &new_ccb->ccb_h, periph_links.le);
504275878Smav
505229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
506229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
507229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
508260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
509229997Sken		xpt_action(new_ccb);
510229997Sken		status = new_ccb->ccb_h.status;
511229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
512288723Smav			free(cmd_info, M_CTLFE);
513275878Smav			ctl_free_io(new_io);
514229997Sken			free(new_ccb, M_CTLFE);
515229997Sken			break;
516229997Sken		}
517229997Sken	}
518229997Sken
519229997Sken	status = cam_periph_acquire(periph);
520229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
521229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
522229997Sken			  "count, status = %#x\n", __func__, status);
523229997Sken		return (status);
524229997Sken	}
525229997Sken
526229997Sken	if (i == 0) {
527229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
528229997Sken			  "status 0x%x\n", __func__, status);
529229997Sken		return (CAM_REQ_CMP_ERR);
530229997Sken	}
531229997Sken
532229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
533229997Sken		union ccb *new_ccb;
534275878Smav		union ctl_io *new_io;
535229997Sken
536229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
537236426Smjacob					      M_ZERO|M_NOWAIT);
538229997Sken		if (new_ccb == NULL) {
539229997Sken			status = CAM_RESRC_UNAVAIL;
540229997Sken			break;
541229997Sken		}
542275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
543275878Smav		if (new_io == NULL) {
544275878Smav			free(new_ccb, M_CTLFE);
545275878Smav			status = CAM_RESRC_UNAVAIL;
546275878Smav			break;
547275878Smav		}
548284793Smav		softc->inots_alloced++;
549275878Smav		new_ccb->ccb_h.io_ptr = new_io;
550315889Smav		LIST_INSERT_HEAD(&softc->inot_list, &new_ccb->ccb_h, periph_links.le);
551229997Sken
552229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
553229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
554229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
555260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
556229997Sken		xpt_action(new_ccb);
557229997Sken		status = new_ccb->ccb_h.status;
558237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
559237601Sken			/*
560237601Sken			 * Note that we don't free the CCB here.  If the
561237601Sken			 * status is not CAM_REQ_INPROG, then we're
562237601Sken			 * probably talking to a SIM that says it is
563237601Sken			 * target-capable but doesn't support the
564237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
565237601Sken			 * older API.  In that case, it'll call xpt_done()
566237601Sken			 * on the CCB, and we need to free it in our done
567237601Sken			 * routine as a result.
568237601Sken			 */
569229997Sken			break;
570229997Sken		}
571229997Sken	}
572237601Sken	if ((i == 0)
573237601Sken	 || (status != CAM_REQ_INPROG)) {
574229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
575229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
576229997Sken		return (CAM_REQ_CMP_ERR);
577229997Sken	}
578275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
579275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
580275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
581229997Sken	return (CAM_REQ_CMP);
582229997Sken}
583229997Sken
584229997Skenstatic void
585229997Skenctlfeoninvalidate(struct cam_periph *periph)
586229997Sken{
587315889Smav	struct ctlfe_lun_softc *softc = (struct ctlfe_lun_softc *)periph->softc;
588315889Smav	struct ctlfe_softc *bus_softc;
589315889Smav	union ccb ccb;
590315889Smav	struct ccb_hdr *hdr;
591229997Sken	cam_status status;
592229997Sken
593315889Smav	/* Abort all ATIOs and INOTs queued to SIM. */
594315889Smav	xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
595315889Smav	ccb.ccb_h.func_code = XPT_ABORT;
596315889Smav	LIST_FOREACH(hdr, &softc->atio_list, periph_links.le) {
597315889Smav		ccb.cab.abort_ccb = (union ccb *)hdr;
598315889Smav		xpt_action(&ccb);
599315889Smav	}
600315889Smav	LIST_FOREACH(hdr, &softc->inot_list, periph_links.le) {
601315889Smav		ccb.cab.abort_ccb = (union ccb *)hdr;
602315889Smav		xpt_action(&ccb);
603315889Smav	}
604229997Sken
605315889Smav	/* Disable the LUN in SIM. */
606315889Smav	ccb.ccb_h.func_code = XPT_EN_LUN;
607315889Smav	ccb.cel.grp6_len = 0;
608315889Smav	ccb.cel.grp7_len = 0;
609315889Smav	ccb.cel.enable = 0;
610315889Smav	xpt_action(&ccb);
611315889Smav	status = (ccb.ccb_h.status & CAM_STATUS_MASK);
612229997Sken	if (status != CAM_REQ_CMP) {
613229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
614315889Smav			  __func__, ccb.ccb_h.status);
615229997Sken		/*
616229997Sken		 * XXX KDM what do we do now?
617229997Sken		 */
618229997Sken	}
619260387Sscottl
620260387Sscottl	bus_softc = softc->parent_softc;
621260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
622260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
623260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
624229997Sken}
625229997Sken
626229997Skenstatic void
627229997Skenctlfecleanup(struct cam_periph *periph)
628229997Sken{
629229997Sken	struct ctlfe_lun_softc *softc;
630229997Sken
631229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
632229997Sken
633314749Smav	KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
634314749Smav	    __func__, softc->ctios_sent));
635314749Smav	KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
636314749Smav	    __func__, softc->refcount));
637314749Smav	KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
638314749Smav	    __func__, softc->atios_alloced));
639314749Smav	KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
640314749Smav	    __func__, softc->inots_alloced));
641245228Sken
642229997Sken	free(softc, M_CTLFE);
643229997Sken}
644229997Sken
645229997Skenstatic void
646265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
647265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
648265641Smav    u_int16_t *sglist_cnt)
649265641Smav{
650265641Smav	struct ctlfe_softc *bus_softc;
651288723Smav	struct ctlfe_cmd_info *cmd_info;
652265641Smav	struct ctl_sg_entry *ctl_sglist;
653265641Smav	bus_dma_segment_t *cam_sglist;
654265641Smav	size_t off;
655265641Smav	int i, idx;
656265641Smav
657312585Smav	cmd_info = PRIV_INFO(io);
658265641Smav	bus_softc = softc->parent_softc;
659265641Smav
660265641Smav	/*
661265641Smav	 * Set the direction, relative to the initiator.
662265641Smav	 */
663265641Smav	*flags &= ~CAM_DIR_MASK;
664265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
665265641Smav		*flags |= CAM_DIR_IN;
666265641Smav	else
667265641Smav		*flags |= CAM_DIR_OUT;
668265641Smav
669265641Smav	*flags &= ~CAM_DATA_MASK;
670265641Smav	idx = cmd_info->cur_transfer_index;
671265641Smav	off = cmd_info->cur_transfer_off;
672265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
673313365Smav	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
674313365Smav
675313365Smav		/* One time shift for SRR offset. */
676313365Smav		off += io->scsiio.ext_data_filled;
677313365Smav		io->scsiio.ext_data_filled = 0;
678313365Smav
679265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
680265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
681265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
682265641Smav		} else {
683265641Smav			*dxfer_len = bus_softc->maxio;
684313365Smav			cmd_info->cur_transfer_off += bus_softc->maxio;
685265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
686265641Smav		}
687265641Smav		*sglist_cnt = 0;
688265641Smav
689265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
690265641Smav			*flags |= CAM_DATA_PADDR;
691265641Smav		else
692265641Smav			*flags |= CAM_DATA_VADDR;
693313365Smav	} else {	/* S/G list with physical or virtual pointers. */
694265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
695313365Smav
696313365Smav		/* One time shift for SRR offset. */
697313365Smav		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
698313365Smav			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
699313365Smav			idx++;
700313365Smav			off = 0;
701313365Smav		}
702313365Smav		off += io->scsiio.ext_data_filled;
703313365Smav		io->scsiio.ext_data_filled = 0;
704313365Smav
705265641Smav		cam_sglist = cmd_info->cam_sglist;
706265641Smav		*dxfer_len = 0;
707265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
708265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
709265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
710265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
711265641Smav				*dxfer_len += cam_sglist[i].ds_len;
712265641Smav			} else {
713265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
714265641Smav				cmd_info->cur_transfer_index = idx + i;
715265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
716265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
717265641Smav				*dxfer_len += cam_sglist[i].ds_len;
718265641Smav				if (ctl_sglist[i].len != 0)
719265641Smav					i++;
720265641Smav				break;
721265641Smav			}
722265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
723265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
724265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
725265641Smav				cmd_info->cur_transfer_off = 0;
726265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
727265641Smav				i++;
728265641Smav				break;
729265641Smav			}
730265641Smav			off = 0;
731265641Smav		}
732265641Smav		*sglist_cnt = i;
733265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
734265641Smav			*flags |= CAM_DATA_SG_PADDR;
735265641Smav		else
736265641Smav			*flags |= CAM_DATA_SG;
737265641Smav		*data_ptr = (uint8_t *)cam_sglist;
738265641Smav	}
739265641Smav}
740265641Smav
741265641Smavstatic void
742229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
743229997Sken{
744229997Sken	struct ctlfe_lun_softc *softc;
745288723Smav	struct ctlfe_cmd_info *cmd_info;
746229997Sken	struct ccb_hdr *ccb_h;
747275880Smav	struct ccb_accept_tio *atio;
748275880Smav	struct ccb_scsiio *csio;
749275880Smav	uint8_t *data_ptr;
750275880Smav	uint32_t dxfer_len;
751275880Smav	ccb_flags flags;
752275880Smav	union ctl_io *io;
753275880Smav	uint8_t scsi_status;
754229997Sken
755229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
756229997Sken
757314739Smavnext:
758315887Smav	/* Take the ATIO off the work queue */
759315887Smav	ccb_h = STAILQ_FIRST(&softc->work_queue);
760260387Sscottl	if (ccb_h == NULL) {
761229997Sken		xpt_release_ccb(start_ccb);
762275880Smav		return;
763275880Smav	}
764315887Smav	STAILQ_REMOVE_HEAD(&softc->work_queue, periph_links.stqe);
765275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
766275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
767275880Smav	csio = &start_ccb->csio;
768229997Sken
769275880Smav	flags = atio->ccb_h.flags &
770275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
771312585Smav	cmd_info = PRIV_INFO(io);
772275881Smav	cmd_info->cur_transfer_index = 0;
773275881Smav	cmd_info->cur_transfer_off = 0;
774275881Smav	cmd_info->flags = 0;
775229997Sken
776275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
777275880Smav		/*
778275880Smav		 * Datamove call, we need to setup the S/G list.
779275880Smav		 */
780275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
781275880Smav		    &csio->sglist_cnt);
782275880Smav	} else {
783275880Smav		/*
784275880Smav		 * We're done, send status back.
785275880Smav		 */
786275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
787275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
788275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
789275880Smav
790314737Smav			/* Tell the SIM that we've aborted this ATIO */
791314737Smav#ifdef CTLFEDEBUG
792314737Smav			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
793314737Smav#endif
794314737Smav			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
795314737Smav			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
796275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
797275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
798275880Smav			xpt_action(start_ccb);
799229997Sken
800314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
801314739Smav			    /* unlock */0);
802229997Sken
803314739Smav			/* XPT_ABORT is not queued, so we can take next I/O. */
804314739Smav			goto next;
805275880Smav		}
806275881Smav		data_ptr = NULL;
807275881Smav		dxfer_len = 0;
808275881Smav		csio->sglist_cnt = 0;
809275881Smav	}
810313365Smav	scsi_status = 0;
811275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
812275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
813275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
814275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
815275880Smav		flags |= CAM_SEND_STATUS;
816275880Smav		scsi_status = io->scsiio.scsi_status;
817275880Smav		csio->sense_len = io->scsiio.sense_len;
818275880Smav#ifdef CTLFEDEBUG
819275880Smav		printf("%s: tag %04x status %x\n", __func__,
820275880Smav		       atio->tag_id, io->io_hdr.status);
821275880Smav#endif
822275880Smav		if (csio->sense_len != 0) {
823275880Smav			csio->sense_data = io->scsiio.sense_data;
824275880Smav			flags |= CAM_SEND_SENSE;
825229997Sken		}
826275880Smav	}
827229997Sken
828229997Sken#ifdef CTLFEDEBUG
829275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
830275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
831275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
832229997Sken#endif
833229997Sken
834275880Smav	/*
835275880Smav	 * Valid combinations:
836275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
837275880Smav	 *    sglist_cnt = 0
838275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
839275880Smav	 *    sglist_cnt = 0
840275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
841275880Smav	 *    sglist_cnt != 0
842275880Smav	 */
843229997Sken#ifdef CTLFEDEBUG
844275880Smav	if (((flags & CAM_SEND_STATUS)
845275880Smav	  && (((flags & CAM_DATA_SG) != 0)
846275880Smav	   || (dxfer_len != 0)
847275880Smav	   || (csio->sglist_cnt != 0)))
848275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
849275880Smav	  && (dxfer_len == 0))
850275880Smav	 || ((flags & CAM_DATA_SG)
851275880Smav	  && (csio->sglist_cnt == 0))
852275880Smav	 || (((flags & CAM_DATA_SG) == 0)
853275880Smav	  && (csio->sglist_cnt != 0))) {
854275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
855275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
856312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
857275880Smav		       csio->sglist_cnt);
858275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
859275880Smav		       atio->tag_id, io->io_hdr.status);
860275880Smav	}
861229997Sken#endif
862275880Smav	cam_fill_ctio(csio,
863275880Smav		      /*retries*/ 2,
864275880Smav		      ctlfedone,
865275880Smav		      flags,
866275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
867275880Smav		      atio->tag_id,
868275880Smav		      atio->init_id,
869275880Smav		      scsi_status,
870275880Smav		      /*data_ptr*/ data_ptr,
871275880Smav		      /*dxfer_len*/ dxfer_len,
872314751Smav		      /*timeout*/ CTLFE_TIMEOUT * 1000);
873275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
874275880Smav	start_ccb->ccb_h.ccb_atio = atio;
875275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
876275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
877275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
878229997Sken
879275880Smav	softc->ctios_sent++;
880314749Smav	softc->refcount++;
881275880Smav	cam_periph_unlock(periph);
882275880Smav	xpt_action(start_ccb);
883275880Smav	cam_periph_lock(periph);
884314749Smav	softc->refcount--;
885229997Sken
886229997Sken	/*
887275880Smav	 * If we still have work to do, ask for another CCB.
888229997Sken	 */
889315887Smav	if (!STAILQ_EMPTY(&softc->work_queue))
890314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
891229997Sken}
892229997Sken
893229997Skenstatic void
894314749Smavctlfe_drain(void *context, int pending)
895314749Smav{
896314749Smav	struct cam_periph *periph = context;
897314749Smav	struct ctlfe_lun_softc *softc = periph->softc;
898314749Smav
899314749Smav	cam_periph_lock(periph);
900314749Smav	while (softc->refcount != 0) {
901314749Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO,
902314749Smav		    "ctlfe_drain", 1);
903314749Smav	}
904314749Smav	cam_periph_unlock(periph);
905314749Smav	cam_periph_release(periph);
906314749Smav}
907314749Smav
908314749Smavstatic void
909229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
910229997Sken{
911229997Sken	struct ctlfe_lun_softc *softc;
912288723Smav	union ctl_io *io;
913288723Smav	struct ctlfe_cmd_info *cmd_info;
914229997Sken
915229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
916288723Smav	io = ccb->ccb_h.io_ptr;
917229997Sken
918229997Sken	switch (ccb->ccb_h.func_code) {
919229997Sken	case XPT_ACCEPT_TARGET_IO:
920314749Smav		softc->atios_alloced--;
921312585Smav		cmd_info = PRIV_INFO(io);
922288723Smav		free(cmd_info, M_CTLFE);
923229997Sken		break;
924229997Sken	case XPT_IMMEDIATE_NOTIFY:
925229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
926314749Smav		softc->inots_alloced--;
927229997Sken		break;
928229997Sken	default:
929229997Sken		break;
930229997Sken	}
931229997Sken
932288723Smav	ctl_free_io(io);
933229997Sken	free(ccb, M_CTLFE);
934229997Sken
935314749Smav	KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
936314749Smav	    __func__, softc->atios_alloced));
937314749Smav	KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
938314749Smav	    __func__, softc->inots_alloced));
939229997Sken
940229997Sken	/*
941229997Sken	 * If we have received all of our CCBs, we can release our
942229997Sken	 * reference on the peripheral driver.  It will probably go away
943229997Sken	 * now.
944229997Sken	 */
945314749Smav	if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
946314749Smav		if (softc->refcount == 0) {
947314749Smav			cam_periph_release_locked(periph);
948314749Smav		} else {
949314749Smav			TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
950314749Smav			taskqueue_enqueue(taskqueue_thread,
951314749Smav			    &softc->refdrain_task);
952314749Smav		}
953229997Sken	}
954229997Sken}
955229997Sken
956314739Smav/*
957314739Smav * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
958314739Smav */
959314739Smavstatic void
960314739Smavctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
961314739Smav{
962314739Smav	struct ctlfe_lun_softc *softc;
963314740Smav	struct mtx *mtx;
964314739Smav
965314739Smav	if (periph->flags & CAM_PERIPH_INVALID) {
966314740Smav		mtx = cam_periph_mtx(periph);
967314739Smav		ctlfe_free_ccb(periph, ccb);
968314739Smav		if (unlock)
969314740Smav			mtx_unlock(mtx);
970314739Smav		return;
971314739Smav	}
972315889Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
973315889Smav	if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
974315889Smav		LIST_INSERT_HEAD(&softc->atio_list, &ccb->ccb_h, periph_links.le);
975315889Smav	else
976315889Smav		LIST_INSERT_HEAD(&softc->inot_list, &ccb->ccb_h, periph_links.le);
977314739Smav	if (unlock)
978314739Smav		cam_periph_unlock(periph);
979314739Smav
980314739Smav	/*
981314739Smav	 * For a wildcard attachment, commands can come in with a specific
982314739Smav	 * target/lun.  Reset the target and LUN fields back to the wildcard
983314739Smav	 * values before we send them back down to the SIM.
984314739Smav	 */
985314739Smav	if (softc->flags & CTLFE_LUN_WILDCARD) {
986314739Smav		ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
987314739Smav		ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
988314739Smav	}
989314739Smav
990314739Smav	xpt_action(ccb);
991314739Smav}
992314739Smav
993238870Smjacobstatic int
994238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
995238870Smjacob{
996238870Smjacob	uint64_t lba;
997238870Smjacob	uint32_t num_blocks, nbc;
998312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
999238870Smjacob
1000238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1001238870Smjacob
1002238870Smjacob	switch (cmdbyt[0]) {
1003238870Smjacob	case READ_6:
1004238870Smjacob	case WRITE_6:
1005238870Smjacob	{
1006238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1007238870Smjacob		lba = scsi_3btoul(cdb->addr);
1008238870Smjacob		lba &= 0x1fffff;
1009238870Smjacob		num_blocks = cdb->length;
1010238870Smjacob		if (num_blocks == 0)
1011238870Smjacob			num_blocks = 256;
1012238870Smjacob		lba += nbc;
1013238870Smjacob		num_blocks -= nbc;
1014238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1015238870Smjacob		cdb->length = num_blocks;
1016238870Smjacob		break;
1017238870Smjacob	}
1018238870Smjacob	case READ_10:
1019238870Smjacob	case WRITE_10:
1020238870Smjacob	{
1021238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1022238870Smjacob		lba = scsi_4btoul(cdb->addr);
1023238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1024238870Smjacob		lba += nbc;
1025238870Smjacob		num_blocks -= nbc;
1026238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1027238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1028238870Smjacob		break;
1029238870Smjacob	}
1030238870Smjacob	case READ_12:
1031238870Smjacob	case WRITE_12:
1032238870Smjacob	{
1033238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1034238870Smjacob		lba = scsi_4btoul(cdb->addr);
1035238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1036238870Smjacob		lba += nbc;
1037238870Smjacob		num_blocks -= nbc;
1038238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1039238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1040238870Smjacob		break;
1041238870Smjacob	}
1042238870Smjacob	case READ_16:
1043238870Smjacob	case WRITE_16:
1044238870Smjacob	{
1045238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1046238870Smjacob		lba = scsi_8btou64(cdb->addr);
1047238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1048238870Smjacob		lba += nbc;
1049238870Smjacob		num_blocks -= nbc;
1050238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1051238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1052238870Smjacob		break;
1053238870Smjacob	}
1054238870Smjacob	default:
1055238870Smjacob		return -1;
1056238870Smjacob	}
1057238870Smjacob	return (0);
1058238870Smjacob}
1059238870Smjacob
1060229997Skenstatic void
1061229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1062229997Sken{
1063229997Sken	struct ctlfe_lun_softc *softc;
1064229997Sken	struct ctlfe_softc *bus_softc;
1065288723Smav	struct ctlfe_cmd_info *cmd_info;
1066238870Smjacob	struct ccb_accept_tio *atio = NULL;
1067238870Smjacob	union ctl_io *io = NULL;
1068260387Sscottl	struct mtx *mtx;
1069314727Smav	cam_status status;
1070229997Sken
1071260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1072260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1073229997Sken#ifdef CTLFE_DEBUG
1074275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1075275878Smav	       done_ccb->ccb_h.func_code);
1076229997Sken#endif
1077229997Sken
1078284794Smav	/*
1079284794Smav	 * At this point CTL has no known use case for device queue freezes.
1080284794Smav	 * In case some SIM think different -- drop its freeze right here.
1081284794Smav	 */
1082284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1083284794Smav		cam_release_devq(periph->path,
1084284794Smav				 /*relsim_flags*/0,
1085284794Smav				 /*reduction*/0,
1086284794Smav				 /*timeout*/0,
1087284794Smav				 /*getcount_only*/0);
1088284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1089284794Smav	}
1090284794Smav
1091229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1092229997Sken	bus_softc = softc->parent_softc;
1093260387Sscottl	mtx = cam_periph_mtx(periph);
1094260387Sscottl	mtx_lock(mtx);
1095229997Sken
1096229997Sken	switch (done_ccb->ccb_h.func_code) {
1097229997Sken	case XPT_ACCEPT_TARGET_IO: {
1098229997Sken
1099315889Smav		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1100229997Sken		atio = &done_ccb->atio;
1101314727Smav		status = atio->ccb_h.status & CAM_STATUS_MASK;
1102314727Smav		if (status != CAM_CDB_RECVD) {
1103314727Smav			ctlfe_free_ccb(periph, done_ccb);
1104314727Smav			goto out;
1105314727Smav		}
1106229997Sken
1107238870Smjacob resubmit:
1108229997Sken		/*
1109229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1110229997Sken		 * datamove or done.
1111229997Sken		 */
1112260387Sscottl		mtx_unlock(mtx);
1113275878Smav		io = done_ccb->ccb_h.io_ptr;
1114312585Smav		cmd_info = PRIV_INFO(io);
1115229997Sken		ctl_zero_io(io);
1116229997Sken
1117229997Sken		/* Save pointers on both sides */
1118312585Smav		PRIV_CCB(io) = done_ccb;
1119312585Smav		PRIV_INFO(io) = cmd_info;
1120229997Sken		done_ccb->ccb_h.io_ptr = io;
1121229997Sken
1122229997Sken		/*
1123229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1124229997Sken		 * down the immediate notify path below.
1125229997Sken		 */
1126229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1127288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1128268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1129290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1130290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1131290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1132290776Smav		} else {
1133290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1134290776Smav		}
1135229997Sken		io->scsiio.tag_num = atio->tag_id;
1136229997Sken		switch (atio->tag_action) {
1137229997Sken		case CAM_TAG_ACTION_NONE:
1138229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1139229997Sken			break;
1140229997Sken		case MSG_SIMPLE_TASK:
1141229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1142229997Sken			break;
1143229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1144229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1145229997Sken			break;
1146229997Sken		case MSG_ORDERED_TASK:
1147229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1148229997Sken			break;
1149229997Sken		case MSG_ACA_TASK:
1150229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1151229997Sken			break;
1152229997Sken		default:
1153229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1154229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1155229997Sken			       atio->tag_action);
1156229997Sken			break;
1157229997Sken		}
1158229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1159229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1160229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1161229997Sken		}
1162229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1163312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1164229997Sken
1165229997Sken#ifdef CTLFEDEBUG
1166288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1167288731Smav		        io->io_hdr.nexus.initid,
1168229997Sken		        io->io_hdr.nexus.targ_port,
1169229997Sken		        io->io_hdr.nexus.targ_lun,
1170229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1171229997Sken#endif
1172229997Sken
1173229997Sken		ctl_queue(io);
1174260387Sscottl		return;
1175229997Sken	}
1176229997Sken	case XPT_CONT_TARGET_IO: {
1177238870Smjacob		int srr = 0;
1178238870Smjacob		uint32_t srr_off = 0;
1179229997Sken
1180229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1181229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1182229997Sken
1183314749Smav		softc->ctios_sent--;
1184229997Sken#ifdef CTLFEDEBUG
1185229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1186229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1187229997Sken#endif
1188229997Sken		/*
1189238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1190238870Smjacob		 */
1191238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1192238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1193238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1194238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1195238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1196238870Smjacob			srr = 1;
1197238870Smjacob			srr_off =
1198238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1199238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1200238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1201238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1202238870Smjacob		}
1203238870Smjacob
1204313365Smav		/*
1205313365Smav		 * If we have an SRR and we're still sending data, we
1206313365Smav		 * should be able to adjust offsets and cycle again.
1207313365Smav		 * It is possible only if offset is from this datamove.
1208313365Smav		 */
1209313365Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1210313365Smav		    srr_off >= io->scsiio.kern_rel_offset &&
1211313365Smav		    srr_off < io->scsiio.kern_rel_offset +
1212313365Smav		     io->scsiio.kern_data_len) {
1213313365Smav			io->scsiio.kern_data_resid =
1214313365Smav			    io->scsiio.kern_rel_offset +
1215313365Smav			    io->scsiio.kern_data_len - srr_off;
1216313365Smav			io->scsiio.ext_data_filled = srr_off;
1217313365Smav			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1218313365Smav			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1219313365Smav			xpt_release_ccb(done_ccb);
1220315887Smav			STAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1221315887Smav					  periph_links.stqe);
1222314749Smav			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1223313365Smav			break;
1224313365Smav		}
1225313365Smav
1226313365Smav		/*
1227313365Smav		 * If status was being sent, the back end data is now history.
1228313365Smav		 * Hack it up and resubmit a new command with the CDB adjusted.
1229313365Smav		 * If the SIM does the right thing, all of the resid math
1230313365Smav		 * should work.
1231313365Smav		 */
1232275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1233238870Smjacob			xpt_release_ccb(done_ccb);
1234238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1235238870Smjacob				done_ccb = (union ccb *)atio;
1236238870Smjacob				goto resubmit;
1237238870Smjacob			}
1238238870Smjacob			/*
1239238870Smjacob			 * Fall through to doom....
1240238870Smjacob			 */
1241238870Smjacob		}
1242238870Smjacob
1243277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1244277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1245277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1246277919Smav
1247238870Smjacob		/*
1248229997Sken		 * If we were sending status back to the initiator, free up
1249229997Sken		 * resources.  If we were doing a datamove, call the
1250229997Sken		 * datamove done routine.
1251229997Sken		 */
1252275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1253315138Smav			/*
1254315138Smav			 * If we asked to send sense data but it wasn't sent,
1255315138Smav			 * queue the I/O back to CTL for later REQUEST SENSE.
1256315138Smav			 */
1257315138Smav			if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1258315138Smav			    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1259315138Smav			    (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1260315138Smav			    (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1261315138Smav				PRIV_INFO(io) = PRIV_INFO(
1262315138Smav				    (union ctl_io *)atio->ccb_h.io_ptr);
1263315138Smav				ctl_queue_sense(atio->ccb_h.io_ptr);
1264315138Smav				atio->ccb_h.io_ptr = io;
1265315138Smav			}
1266315138Smav
1267314745Smav			/* Abort ATIO if CTIO sending status has failed. */
1268314745Smav			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1269314745Smav			    CAM_REQ_CMP) {
1270314745Smav				done_ccb->ccb_h.func_code = XPT_ABORT;
1271314745Smav				done_ccb->cab.abort_ccb = (union ccb *)atio;
1272314745Smav				xpt_action(done_ccb);
1273314745Smav			}
1274314745Smav
1275229997Sken			xpt_release_ccb(done_ccb);
1276314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
1277314739Smav			    /* unlock */1);
1278314739Smav			return;
1279229997Sken		} else {
1280288723Smav			struct ctlfe_cmd_info *cmd_info;
1281229997Sken			struct ccb_scsiio *csio;
1282229997Sken
1283229997Sken			csio = &done_ccb->csio;
1284312585Smav			cmd_info = PRIV_INFO(io);
1285229997Sken
1286229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1287229997Sken
1288229997Sken			/*
1289229997Sken			 * Translate CAM status to CTL status.  Success
1290229997Sken			 * does not change the overall, ctl_io status.  In
1291229997Sken			 * that case we just set port_status to 0.  If we
1292229997Sken			 * have a failure, though, set a data phase error
1293229997Sken			 * for the overall ctl_io.
1294229997Sken			 */
1295229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1296229997Sken			case CAM_REQ_CMP:
1297314757Smav				io->scsiio.kern_data_resid -=
1298314757Smav				    csio->dxfer_len - csio->resid;
1299229997Sken				io->io_hdr.port_status = 0;
1300229997Sken				break;
1301229997Sken			default:
1302229997Sken				/*
1303245228Sken				 * XXX KDM we probably need to figure out a
1304245228Sken				 * standard set of errors that the SIM
1305245228Sken				 * drivers should return in the event of a
1306245228Sken				 * data transfer failure.  A data phase
1307245228Sken				 * error will at least point the user to a
1308245228Sken				 * data transfer error of some sort.
1309245228Sken				 * Hopefully the SIM printed out some
1310245228Sken				 * additional information to give the user
1311245228Sken				 * a clue what happened.
1312229997Sken				 */
1313229997Sken				io->io_hdr.port_status = 0xbad1;
1314229997Sken				ctl_set_data_phase_error(&io->scsiio);
1315229997Sken				/*
1316229997Sken				 * XXX KDM figure out residual.
1317229997Sken				 */
1318229997Sken				break;
1319229997Sken			}
1320229997Sken			/*
1321229997Sken			 * If we had to break this S/G list into multiple
1322229997Sken			 * pieces, figure out where we are in the list, and
1323229997Sken			 * continue sending pieces if necessary.
1324229997Sken			 */
1325314757Smav			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1326314757Smav			    io->io_hdr.port_status == 0 && csio->resid == 0) {
1327229997Sken				ccb_flags flags;
1328229997Sken				uint8_t *data_ptr;
1329229997Sken				uint32_t dxfer_len;
1330229997Sken
1331229997Sken				flags = atio->ccb_h.flags &
1332229997Sken					(CAM_DIS_DISCONNECT|
1333265641Smav					 CAM_TAG_ACTION_VALID);
1334229997Sken
1335265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1336265641Smav				    &dxfer_len, &csio->sglist_cnt);
1337229997Sken
1338229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1339229997Sken				 && (dxfer_len == 0)) {
1340229997Sken					printf("%s: tag %04x no status or "
1341229997Sken					       "len cdb = %02x\n", __func__,
1342229997Sken					       atio->tag_id,
1343312845Smav					       atio_cdb_ptr(atio)[0]);
1344229997Sken					printf("%s: tag %04x io status %#x\n",
1345229997Sken					       __func__, atio->tag_id,
1346229997Sken					       io->io_hdr.status);
1347229997Sken				}
1348229997Sken
1349229997Sken				cam_fill_ctio(csio,
1350229997Sken					      /*retries*/ 2,
1351229997Sken					      ctlfedone,
1352229997Sken					      flags,
1353229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1354229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1355229997Sken					      atio->tag_id,
1356229997Sken					      atio->init_id,
1357313365Smav					      0,
1358229997Sken					      /*data_ptr*/ data_ptr,
1359229997Sken					      /*dxfer_len*/ dxfer_len,
1360314751Smav					      CTLFE_TIMEOUT * 1000);
1361229997Sken
1362260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1363229997Sken				csio->resid = 0;
1364229997Sken				csio->ccb_h.ccb_atio = atio;
1365229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1366229997Sken				softc->ctios_sent++;
1367260387Sscottl				mtx_unlock(mtx);
1368229997Sken				xpt_action((union ccb *)csio);
1369229997Sken			} else {
1370229997Sken				/*
1371229997Sken				 * Release the CTIO.  The ATIO will be sent back
1372229997Sken				 * down to the SIM once we send status.
1373229997Sken				 */
1374229997Sken				xpt_release_ccb(done_ccb);
1375260387Sscottl				mtx_unlock(mtx);
1376229997Sken
1377229997Sken				/* Call the backend move done callback */
1378229997Sken				io->scsiio.be_move_done(io);
1379229997Sken			}
1380260387Sscottl			return;
1381229997Sken		}
1382229997Sken		break;
1383229997Sken	}
1384229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1385229997Sken		union ctl_io *io;
1386229997Sken		struct ccb_immediate_notify *inot;
1387284794Smav		int send_ctl_io;
1388229997Sken
1389315889Smav		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1390229997Sken		inot = &done_ccb->cin1;
1391275878Smav		io = done_ccb->ccb_h.io_ptr;
1392275878Smav		ctl_zero_io(io);
1393229997Sken
1394275878Smav		send_ctl_io = 1;
1395229997Sken
1396275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1397312585Smav		PRIV_CCB(io) = done_ccb;
1398275878Smav		inot->ccb_h.io_ptr = io;
1399288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1400275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1401290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1402290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1403290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1404290776Smav		} else {
1405290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1406290776Smav		}
1407275878Smav		/* XXX KDM should this be the tag_id? */
1408275878Smav		io->taskio.tag_num = inot->seq_id;
1409229997Sken
1410275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1411275878Smav		switch (status) {
1412275878Smav		case CAM_SCSI_BUS_RESET:
1413275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1414275878Smav			break;
1415275878Smav		case CAM_BDR_SENT:
1416275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1417275878Smav			break;
1418275878Smav		case CAM_MESSAGE_RECV:
1419275878Smav			switch (inot->arg) {
1420275878Smav			case MSG_ABORT_TASK_SET:
1421275878Smav				io->taskio.task_action =
1422275878Smav				    CTL_TASK_ABORT_TASK_SET;
1423229997Sken				break;
1424275878Smav			case MSG_TARGET_RESET:
1425290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1426229997Sken				break;
1427275878Smav			case MSG_ABORT_TASK:
1428290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1429229997Sken				break;
1430275878Smav			case MSG_LOGICAL_UNIT_RESET:
1431290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1432275878Smav				break;
1433275878Smav			case MSG_CLEAR_TASK_SET:
1434275878Smav				io->taskio.task_action =
1435290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1436275878Smav				break;
1437275878Smav			case MSG_CLEAR_ACA:
1438290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1439290775Smav				break;
1440290775Smav			case MSG_QUERY_TASK:
1441290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1442290775Smav				break;
1443290775Smav			case MSG_QUERY_TASK_SET:
1444275878Smav				io->taskio.task_action =
1445290775Smav				    CTL_TASK_QUERY_TASK_SET;
1446275878Smav				break;
1447290775Smav			case MSG_QUERY_ASYNC_EVENT:
1448290775Smav				io->taskio.task_action =
1449290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1450290775Smav				break;
1451275878Smav			case MSG_NOOP:
1452229997Sken				send_ctl_io = 0;
1453229997Sken				break;
1454229997Sken			default:
1455275878Smav				xpt_print(periph->path,
1456314727Smav				    "%s: unsupported INOT message 0x%x\n",
1457314727Smav				    __func__, inot->arg);
1458275878Smav				send_ctl_io = 0;
1459275878Smav				break;
1460275878Smav			}
1461275878Smav			break;
1462314727Smav		default:
1463314727Smav			xpt_print(periph->path,
1464314727Smav			    "%s: unsupported INOT status 0x%x\n",
1465314727Smav			    __func__, status);
1466314727Smav			/* FALLTHROUGH */
1467275878Smav		case CAM_REQ_ABORTED:
1468275878Smav		case CAM_REQ_INVALID:
1469314727Smav		case CAM_DEV_NOT_THERE:
1470275878Smav		case CAM_PROVIDE_FAIL:
1471275878Smav			ctlfe_free_ccb(periph, done_ccb);
1472275878Smav			goto out;
1473275878Smav		}
1474275878Smav		if (send_ctl_io != 0) {
1475275878Smav			ctl_queue(io);
1476229997Sken		} else {
1477229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1478229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1479229997Sken			xpt_action(done_ccb);
1480229997Sken		}
1481229997Sken		break;
1482229997Sken	}
1483229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1484314739Smav		/* Queue this back down to the SIM as an immediate notify. */
1485304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1486229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1487314739Smav		ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1488314739Smav		return;
1489229997Sken	case XPT_SET_SIM_KNOB:
1490229997Sken	case XPT_GET_SIM_KNOB:
1491229997Sken		break;
1492229997Sken	default:
1493229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1494229997Sken		      done_ccb->ccb_h.func_code);
1495229997Sken		break;
1496229997Sken	}
1497260387Sscottl
1498260387Sscottlout:
1499260387Sscottl	mtx_unlock(mtx);
1500229997Sken}
1501229997Sken
1502229997Skenstatic void
1503229997Skenctlfe_onoffline(void *arg, int online)
1504229997Sken{
1505229997Sken	struct ctlfe_softc *bus_softc;
1506229997Sken	union ccb *ccb;
1507229997Sken	cam_status status;
1508229997Sken	struct cam_path *path;
1509229997Sken	int set_wwnn;
1510229997Sken
1511229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1512229997Sken
1513229997Sken	set_wwnn = 0;
1514229997Sken
1515229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1516229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1517229997Sken	if (status != CAM_REQ_CMP) {
1518229997Sken		printf("%s: unable to create path!\n", __func__);
1519229997Sken		return;
1520229997Sken	}
1521275882Smav	ccb = xpt_alloc_ccb();
1522242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1523288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1524288713Smav	xpt_action(ccb);
1525229997Sken
1526229997Sken	/*
1527229997Sken	 * Copan WWN format:
1528229997Sken	 *
1529229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1530229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1531229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1532229997Sken	 * Bits 11-8:			Type of port:
1533229997Sken	 *					1 == N-Port
1534229997Sken	 *					2 == F-Port
1535229997Sken	 *					3 == NL-Port
1536229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1537229997Sken	 */
1538229997Sken	if (online != 0) {
1539229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1540229997Sken
1541229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1542229997Sken			       bus_softc->port_name,
1543229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1544229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1545229997Sken			       bus_softc->port_name,
1546229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1547229997Sken
1548229997Sken			/*
1549229997Sken			 * If the user has specified a WWNN/WWPN, send them
1550229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1551229997Sken			 * has reported.
1552229997Sken			 */
1553284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1554284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1555229997Sken				ccb->knob.xport_specific.fc.wwnn =
1556284586Smav				    bus_softc->port.wwnn;
1557284586Smav				set_wwnn = 1;
1558284586Smav			} else {
1559284586Smav				ctl_port_set_wwns(&bus_softc->port,
1560284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1561284586Smav				    false, 0);
1562284586Smav			}
1563284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1564284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1565229997Sken				ccb->knob.xport_specific.fc.wwpn =
1566284586Smav				    bus_softc->port.wwpn;
1567229997Sken				set_wwnn = 1;
1568229997Sken			} else {
1569268683Smav				ctl_port_set_wwns(&bus_softc->port,
1570284586Smav				    false, 0,
1571268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1572229997Sken			}
1573229997Sken
1574229997Sken
1575229997Sken			if (set_wwnn != 0) {
1576229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1577229997Sken				       bus_softc->port_name,
1578229997Sken				ccb->knob.xport_specific.fc.wwnn);
1579229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1580229997Sken				       bus_softc->port_name,
1581229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1582229997Sken			}
1583229997Sken		} else {
1584229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1585229997Sken			       bus_softc->port_name);
1586229997Sken		}
1587229997Sken	}
1588229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1589229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1590229997Sken	if (set_wwnn != 0)
1591229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1592229997Sken
1593229997Sken	if (online != 0)
1594288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1595229997Sken	else
1596288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1597229997Sken
1598229997Sken	xpt_action(ccb);
1599229997Sken
1600229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1601229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1602229997Sken		       "status %#x\n",
1603229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1604229997Sken		       (online != 0) ? "enable" : "disable",
1605229997Sken		       ccb->ccb_h.status);
1606229997Sken	} else {
1607229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1608229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1609229997Sken		       (online != 0) ? "enable" : "disable");
1610229997Sken	}
1611229997Sken
1612229997Sken	xpt_free_path(path);
1613275882Smav	xpt_free_ccb(ccb);
1614229997Sken}
1615229997Sken
1616229997Skenstatic void
1617229997Skenctlfe_online(void *arg)
1618229997Sken{
1619245228Sken	struct ctlfe_softc *bus_softc;
1620245228Sken	struct cam_path *path;
1621245228Sken	cam_status status;
1622245228Sken	struct ctlfe_lun_softc *lun_softc;
1623274388Smav	struct cam_periph *periph;
1624245228Sken
1625245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1626245228Sken
1627245228Sken	/*
1628245228Sken	 * Create the wildcard LUN before bringing the port online.
1629245228Sken	 */
1630245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1631245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1632245228Sken				 CAM_LUN_WILDCARD);
1633245228Sken	if (status != CAM_REQ_CMP) {
1634245228Sken		printf("%s: unable to create path for wildcard periph\n",
1635245228Sken				__func__);
1636245228Sken		return;
1637245228Sken	}
1638245228Sken
1639275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1640245228Sken
1641260387Sscottl	xpt_path_lock(path);
1642274388Smav	periph = cam_periph_find(path, "ctl");
1643274388Smav	if (periph != NULL) {
1644274388Smav		/* We've already got a periph, no need to alloc a new one. */
1645274388Smav		xpt_path_unlock(path);
1646274388Smav		xpt_free_path(path);
1647274388Smav		free(lun_softc, M_CTLFE);
1648274388Smav		return;
1649274388Smav	}
1650245228Sken	lun_softc->parent_softc = bus_softc;
1651245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1652245228Sken
1653245228Sken	status = cam_periph_alloc(ctlferegister,
1654245228Sken				  ctlfeoninvalidate,
1655245228Sken				  ctlfecleanup,
1656245228Sken				  ctlfestart,
1657245228Sken				  "ctl",
1658245228Sken				  CAM_PERIPH_BIO,
1659245228Sken				  path,
1660245228Sken				  ctlfeasync,
1661245228Sken				  0,
1662245228Sken				  lun_softc);
1663245228Sken
1664245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1665245228Sken		const struct cam_status_entry *entry;
1666245228Sken
1667245228Sken		entry = cam_fetch_status_entry(status);
1668245228Sken		printf("%s: CAM error %s (%#x) returned from "
1669245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1670245228Sken		       entry->status_text : "Unknown", status);
1671274388Smav		free(lun_softc, M_CTLFE);
1672245228Sken	}
1673245228Sken
1674260387Sscottl	xpt_path_unlock(path);
1675275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1676260387Sscottl	xpt_free_path(path);
1677229997Sken}
1678229997Sken
1679229997Skenstatic void
1680229997Skenctlfe_offline(void *arg)
1681229997Sken{
1682245228Sken	struct ctlfe_softc *bus_softc;
1683245228Sken	struct cam_path *path;
1684245228Sken	cam_status status;
1685245228Sken	struct cam_periph *periph;
1686245228Sken
1687245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1688245228Sken
1689275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1690275882Smav
1691245228Sken	/*
1692245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1693245228Sken	 * the port offline.
1694245228Sken	 */
1695245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1696245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1697245228Sken				 CAM_LUN_WILDCARD);
1698245228Sken	if (status != CAM_REQ_CMP) {
1699245228Sken		printf("%s: unable to create path for wildcard periph\n",
1700245228Sken		       __func__);
1701245228Sken		return;
1702245228Sken	}
1703260387Sscottl	xpt_path_lock(path);
1704245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1705245228Sken		cam_periph_invalidate(periph);
1706260387Sscottl	xpt_path_unlock(path);
1707245228Sken	xpt_free_path(path);
1708229997Sken}
1709229997Sken
1710229997Sken/*
1711229997Sken * This will get called to enable a LUN on every bus that is attached to
1712229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1713229997Sken */
1714229997Skenstatic int
1715284798Smavctlfe_lun_enable(void *arg, int lun_id)
1716229997Sken{
1717229997Sken	struct ctlfe_softc *bus_softc;
1718229997Sken	struct ctlfe_lun_softc *softc;
1719229997Sken	struct cam_path *path;
1720229997Sken	struct cam_periph *periph;
1721229997Sken	cam_status status;
1722229997Sken
1723229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1724290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1725290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1726229997Sken
1727260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1728290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1729229997Sken	/* XXX KDM need some way to return status to CTL here? */
1730229997Sken	if (status != CAM_REQ_CMP) {
1731229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1732229997Sken		       status);
1733229997Sken		return (1);
1734229997Sken	}
1735229997Sken
1736229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1737260387Sscottl	xpt_path_lock(path);
1738229997Sken	periph = cam_periph_find(path, "ctl");
1739229997Sken	if (periph != NULL) {
1740229997Sken		/* We've already got a periph, no need to alloc a new one. */
1741260387Sscottl		xpt_path_unlock(path);
1742229997Sken		xpt_free_path(path);
1743229997Sken		free(softc, M_CTLFE);
1744229997Sken		return (0);
1745229997Sken	}
1746229997Sken	softc->parent_softc = bus_softc;
1747229997Sken
1748229997Sken	status = cam_periph_alloc(ctlferegister,
1749229997Sken				  ctlfeoninvalidate,
1750229997Sken				  ctlfecleanup,
1751229997Sken				  ctlfestart,
1752229997Sken				  "ctl",
1753229997Sken				  CAM_PERIPH_BIO,
1754229997Sken				  path,
1755229997Sken				  ctlfeasync,
1756229997Sken				  0,
1757229997Sken				  softc);
1758229997Sken
1759274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1760274388Smav		const struct cam_status_entry *entry;
1761274388Smav
1762274388Smav		entry = cam_fetch_status_entry(status);
1763274388Smav		printf("%s: CAM error %s (%#x) returned from "
1764274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1765274388Smav		       entry->status_text : "Unknown", status);
1766274388Smav		free(softc, M_CTLFE);
1767274388Smav	}
1768274388Smav
1769260387Sscottl	xpt_path_unlock(path);
1770244016Sken	xpt_free_path(path);
1771229997Sken	return (0);
1772229997Sken}
1773229997Sken
1774229997Sken/*
1775245228Sken * This will get called when the user removes a LUN to disable that LUN
1776245228Sken * on every bus that is attached to CTL.
1777229997Sken */
1778229997Skenstatic int
1779284798Smavctlfe_lun_disable(void *arg, int lun_id)
1780229997Sken{
1781229997Sken	struct ctlfe_softc *softc;
1782229997Sken	struct ctlfe_lun_softc *lun_softc;
1783229997Sken
1784229997Sken	softc = (struct ctlfe_softc *)arg;
1785290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1786290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1787229997Sken
1788260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1789229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1790229997Sken		struct cam_path *path;
1791229997Sken
1792229997Sken		path = lun_softc->periph->path;
1793229997Sken
1794290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1795229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1796229997Sken			break;
1797229997Sken		}
1798229997Sken	}
1799229997Sken	if (lun_softc == NULL) {
1800260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1801284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1802229997Sken		return (1);
1803229997Sken	}
1804260387Sscottl	cam_periph_acquire(lun_softc->periph);
1805260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1806229997Sken
1807260387Sscottl	cam_periph_lock(lun_softc->periph);
1808229997Sken	cam_periph_invalidate(lun_softc->periph);
1809260387Sscottl	cam_periph_unlock(lun_softc->periph);
1810260387Sscottl	cam_periph_release(lun_softc->periph);
1811229997Sken	return (0);
1812229997Sken}
1813229997Sken
1814229997Skenstatic void
1815229997Skenctlfe_dump_sim(struct cam_sim *sim)
1816229997Sken{
1817229997Sken
1818229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1819229997Sken	       sim->sim_name, sim->unit_number,
1820229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1821229997Sken}
1822229997Sken
1823229997Sken/*
1824229997Sken * Assumes that the SIM lock is held.
1825229997Sken */
1826229997Skenstatic void
1827229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1828229997Sken{
1829229997Sken	struct ccb_hdr *hdr;
1830229997Sken	struct cam_periph *periph;
1831229997Sken	int num_items;
1832229997Sken
1833229997Sken	periph = softc->periph;
1834229997Sken	num_items = 0;
1835229997Sken
1836315887Smav	STAILQ_FOREACH(hdr, &softc->work_queue, periph_links.stqe) {
1837275880Smav		union ctl_io *io = hdr->io_ptr;
1838229997Sken
1839229997Sken		num_items++;
1840229997Sken
1841229997Sken		/*
1842229997Sken		 * Only regular SCSI I/O is put on the work
1843229997Sken		 * queue, so we can print sense here.  There may be no
1844229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1845229997Sken		 * print out the CCB as well.
1846229997Sken		 *
1847229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1848229997Sken		 * CTL is merged in with CAM.
1849229997Sken		 */
1850229997Sken		ctl_io_error_print(io, NULL);
1851229997Sken
1852229997Sken		/*
1853275880Smav		 * Print DMA status if we are DMA_QUEUED.
1854229997Sken		 */
1855275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1856275880Smav			xpt_print(periph->path,
1857275880Smav			    "Total %u, Current %u, Resid %u\n",
1858275880Smav			    io->scsiio.kern_total_len,
1859275880Smav			    io->scsiio.kern_data_len,
1860275880Smav			    io->scsiio.kern_data_resid);
1861275880Smav		}
1862229997Sken	}
1863229997Sken
1864314749Smav	xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1865314749Smav	xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1866229997Sken}
1867229997Sken
1868229997Sken/*
1869275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1870275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1871275880Smav * to the adapter.
1872229997Sken */
1873229997Skenstatic void
1874275880Smavctlfe_datamove(union ctl_io *io)
1875229997Sken{
1876275880Smav	union ccb *ccb;
1877275880Smav	struct cam_periph *periph;
1878229997Sken	struct ctlfe_lun_softc *softc;
1879229997Sken
1880275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1881275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1882229997Sken
1883313365Smav	io->scsiio.ext_data_filled = 0;
1884312585Smav	ccb = PRIV_CCB(io);
1885275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
1886275880Smav	cam_periph_lock(periph);
1887275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1888275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1889275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1890275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1891315887Smav	STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1892315887Smav			  periph_links.stqe);
1893314749Smav	xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1894275880Smav	cam_periph_unlock(periph);
1895229997Sken}
1896229997Sken
1897229997Skenstatic void
1898275880Smavctlfe_done(union ctl_io *io)
1899229997Sken{
1900229997Sken	union ccb *ccb;
1901229997Sken	struct cam_periph *periph;
1902229997Sken	struct ctlfe_lun_softc *softc;
1903229997Sken
1904312585Smav	ccb = PRIV_CCB(io);
1905229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
1906260387Sscottl	cam_periph_lock(periph);
1907229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1908229997Sken
1909229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
1910229997Sken		/*
1911229997Sken		 * Send the notify acknowledge down to the SIM, to let it
1912229997Sken		 * know we processed the task management command.
1913229997Sken		 */
1914229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
1915229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1916304417Smav		switch (io->taskio.task_status) {
1917304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
1918304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1919304417Smav			break;
1920304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
1921304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1922304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1923304417Smav			break;
1924304417Smav		case CTL_TASK_FUNCTION_REJECTED:
1925304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1926304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1927304417Smav			break;
1928304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
1929304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1930304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1931304417Smav			break;
1932304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1933304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1934304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1935304417Smav			break;
1936304417Smav		}
1937304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1938229997Sken		xpt_action(ccb);
1939275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1940314739Smav		ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1941314739Smav		return;
1942229997Sken	} else {
1943275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1944315887Smav		STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1945315887Smav				  periph_links.stqe);
1946314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1947229997Sken	}
1948229997Sken
1949260387Sscottl	cam_periph_unlock(periph);
1950229997Sken}
1951229997Sken
1952229997Skenstatic void
1953229997Skenctlfe_dump(void)
1954229997Sken{
1955229997Sken	struct ctlfe_softc *bus_softc;
1956275880Smav	struct ctlfe_lun_softc *lun_softc;
1957229997Sken
1958229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1959229997Sken		ctlfe_dump_sim(bus_softc->sim);
1960275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1961229997Sken			ctlfe_dump_queue(lun_softc);
1962229997Sken	}
1963229997Sken}
1964