scsi_ctl.c revision 272734
1359575Sdim/*-
2359575Sdim * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3359575Sdim * All rights reserved.
4359575Sdim *
5359575Sdim * Redistribution and use in source and binary forms, with or without
6359575Sdim * modification, are permitted provided that the following conditions
7359575Sdim * are met:
8359575Sdim * 1. Redistributions of source code must retain the above copyright
9359575Sdim *    notice, this list of conditions, and the following disclaimer,
10359575Sdim *    without modification.
11359575Sdim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12359575Sdim *    substantially similar to the "NO WARRANTY" disclaimer below
13359575Sdim *    ("Disclaimer") and any redistribution must be conditioned upon
14359575Sdim *    including a substantially similar Disclaimer requirement for further
15359575Sdim *    binary redistribution.
16359575Sdim *
17359575Sdim * NO WARRANTY
18359575Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19359575Sdim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20359575Sdim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21359575Sdim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22359575Sdim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23359575Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24359575Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25359575Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26359575Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27359575Sdim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28359575Sdim * POSSIBILITY OF SUCH DAMAGES.
29359575Sdim *
30359575Sdim * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
31359575Sdim */
32359575Sdim/*
33359575Sdim * Peripheral driver interface between CAM and CTL (CAM Target Layer).
34359575Sdim *
35359575Sdim * Author: Ken Merry <ken@FreeBSD.org>
36359575Sdim */
37359575Sdim
38359575Sdim#include <sys/cdefs.h>
39359575Sdim__FBSDID("$FreeBSD: head/sys/cam/ctl/scsi_ctl.c 272734 2014-10-08 07:48:36Z mav $");
40359575Sdim
41359575Sdim#include <sys/param.h>
42359575Sdim#include <sys/queue.h>
43359575Sdim#include <sys/systm.h>
44359575Sdim#include <sys/kernel.h>
45359575Sdim#include <sys/lock.h>
46359575Sdim#include <sys/mutex.h>
47359575Sdim#include <sys/condvar.h>
48359575Sdim#include <sys/malloc.h>
49359575Sdim#include <sys/bus.h>
50359575Sdim#include <sys/endian.h>
51359575Sdim#include <sys/sbuf.h>
52359575Sdim#include <sys/sysctl.h>
53359575Sdim#include <sys/types.h>
54359575Sdim#include <sys/systm.h>
55359575Sdim#include <machine/bus.h>
56359575Sdim
57359575Sdim#include <cam/cam.h>
58359575Sdim#include <cam/cam_ccb.h>
59359575Sdim#include <cam/cam_periph.h>
60359575Sdim#include <cam/cam_queue.h>
61359575Sdim#include <cam/cam_xpt_periph.h>
62359575Sdim#include <cam/cam_debug.h>
63359575Sdim#include <cam/cam_sim.h>
64359575Sdim#include <cam/cam_xpt.h>
65359575Sdim
66359575Sdim#include <cam/scsi/scsi_all.h>
67359575Sdim#include <cam/scsi/scsi_message.h>
68359575Sdim
69359575Sdim#include <cam/ctl/ctl_io.h>
70359575Sdim#include <cam/ctl/ctl.h>
71359575Sdim#include <cam/ctl/ctl_frontend.h>
72359575Sdim#include <cam/ctl/ctl_util.h>
73359575Sdim#include <cam/ctl/ctl_error.h>
74359575Sdim
75359575Sdimtypedef enum {
76359575Sdim	CTLFE_CCB_DEFAULT	= 0x00
77359575Sdim} ctlfe_ccb_types;
78359575Sdim
79359575Sdimstruct ctlfe_softc {
80359575Sdim	struct ctl_port port;
81359575Sdim	path_id_t path_id;
82359575Sdim	u_int	maxio;
83359575Sdim	struct cam_sim *sim;
84359575Sdim	char port_name[DEV_IDLEN];
85359575Sdim	struct mtx lun_softc_mtx;
86359575Sdim	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
87359575Sdim	STAILQ_ENTRY(ctlfe_softc) links;
88359575Sdim};
89359575Sdim
90359575SdimSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
91359575Sdimstruct mtx ctlfe_list_mtx;
92359575Sdimstatic char ctlfe_mtx_desc[] = "ctlfelist";
93359575Sdimstatic int ctlfe_dma_enabled = 1;
94359575Sdim#ifdef CTLFE_INIT_ENABLE
95359575Sdimstatic int ctlfe_max_targets = 1;
96359575Sdimstatic int ctlfe_num_targets = 0;
97359575Sdim#endif
98359575Sdim
99359575Sdimtypedef enum {
100359575Sdim	CTLFE_LUN_NONE		= 0x00,
101359575Sdim	CTLFE_LUN_WILDCARD	= 0x01
102359575Sdim} ctlfe_lun_flags;
103359575Sdim
104359575Sdimstruct ctlfe_lun_softc {
105359575Sdim	struct ctlfe_softc *parent_softc;
106359575Sdim	struct cam_periph *periph;
107359575Sdim	ctlfe_lun_flags flags;
108359575Sdim	struct callout dma_callout;
109359575Sdim	uint64_t ccbs_alloced;
110359575Sdim	uint64_t ccbs_freed;
111359575Sdim	uint64_t ctios_sent;
112359575Sdim	uint64_t ctios_returned;
113359575Sdim	uint64_t atios_sent;
114359575Sdim	uint64_t atios_returned;
115359575Sdim	uint64_t inots_sent;
116359575Sdim	uint64_t inots_returned;
117359575Sdim	/* bus_dma_tag_t dma_tag; */
118359575Sdim	TAILQ_HEAD(, ccb_hdr) work_queue;
119359575Sdim	STAILQ_ENTRY(ctlfe_lun_softc) links;
120359575Sdim};
121359575Sdim
122359575Sdimtypedef enum {
123359575Sdim	CTLFE_CMD_NONE		= 0x00,
124359575Sdim	CTLFE_CMD_PIECEWISE	= 0x01
125359575Sdim} ctlfe_cmd_flags;
126359575Sdim
127359575Sdim/*
128359575Sdim * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
129359575Sdim * Currently that is 600 bytes.
130359575Sdim */
131359575Sdimstruct ctlfe_lun_cmd_info {
132359575Sdim	int cur_transfer_index;
133359575Sdim	size_t cur_transfer_off;
134359575Sdim	ctlfe_cmd_flags flags;
135359575Sdim	/*
136359575Sdim	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
137359575Sdim	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
138359575Sdim	 * i386 and 512 bytes on amd64.
139359575Sdim	 */
140359575Sdim#define CTLFE_MAX_SEGS	32
141359575Sdim	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
142359575Sdim};
143359575Sdim
144359575Sdim/*
145359575Sdim * When we register the adapter/bus, request that this many ctl_ios be
146359575Sdim * allocated.  This should be the maximum supported by the adapter, but we
147359575Sdim * currently don't have a way to get that back from the path inquiry.
148359575Sdim * XXX KDM add that to the path inquiry.
149359575Sdim */
150359575Sdim#define	CTLFE_REQ_CTL_IO	4096
151359575Sdim/*
152359575Sdim * Number of Accept Target I/O CCBs to allocate and queue down to the
153359575Sdim * adapter per LUN.
154359575Sdim * XXX KDM should this be controlled by CTL?
155359575Sdim */
156359575Sdim#define	CTLFE_ATIO_PER_LUN	1024
157359575Sdim/*
158359575Sdim * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
159359575Sdim * allocate and queue down to the adapter per LUN.
160359575Sdim * XXX KDM should this be controlled by CTL?
161359575Sdim */
162359575Sdim#define	CTLFE_IN_PER_LUN	1024
163359575Sdim
164359575Sdim/*
165359575Sdim * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
166359575Sdim * status to the initiator.  The SIM is expected to have its own timeouts,
167359575Sdim * so we're not putting this timeout around the CCB execution time.  The
168359575Sdim * SIM should timeout and let us know if it has an issue.
169359575Sdim */
170359575Sdim#define	CTLFE_DMA_TIMEOUT	60
171359575Sdim
172359575Sdim/*
173359575Sdim * Turn this on to enable extra debugging prints.
174359575Sdim */
175359575Sdim#if 0
176359575Sdim#define	CTLFE_DEBUG
177359575Sdim#endif
178359575Sdim
179359575Sdim/*
180359575Sdim * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
181359575Sdim * in the FreeBSD initiator that makes it unable to rescan the target if
182359575Sdim * the target gets rebooted and the WWNN/WWPN stay the same.
183359575Sdim */
184359575Sdim#if 0
185359575Sdim#define	RANDOM_WWNN
186359575Sdim#endif
187359575Sdim
188359575SdimSYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
189359575Sdim	   &ctlfe_dma_enabled, 0, "DMA enabled");
190359575SdimMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
191359575Sdim
192359575Sdim#define	ccb_type	ppriv_field0
193359575Sdim/* This is only used in the ATIO */
194359575Sdim#define	io_ptr		ppriv_ptr1
195359575Sdim
196359575Sdim/* This is only used in the CTIO */
197359575Sdim#define	ccb_atio	ppriv_ptr1
198359575Sdim
199359575Sdimint			ctlfeinitialize(void);
200359575Sdimvoid			ctlfeshutdown(void);
201359575Sdimstatic periph_init_t	ctlfeperiphinit;
202359575Sdimstatic void		ctlfeasync(void *callback_arg, uint32_t code,
203359575Sdim				   struct cam_path *path, void *arg);
204359575Sdimstatic periph_ctor_t	ctlferegister;
205359575Sdimstatic periph_oninv_t	ctlfeoninvalidate;
206359575Sdimstatic periph_dtor_t	ctlfecleanup;
207359575Sdimstatic periph_start_t	ctlfestart;
208359575Sdimstatic void		ctlfedone(struct cam_periph *periph,
209359575Sdim				  union ccb *done_ccb);
210359575Sdim
211359575Sdimstatic void 		ctlfe_onoffline(void *arg, int online);
212359575Sdimstatic void 		ctlfe_online(void *arg);
213359575Sdimstatic void 		ctlfe_offline(void *arg);
214359575Sdimstatic int 		ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
215359575Sdim					 int lun_id);
216359575Sdimstatic int 		ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
217359575Sdim					  int lun_id);
218359575Sdimstatic void		ctlfe_dump_sim(struct cam_sim *sim);
219359575Sdimstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
220359575Sdimstatic void		ctlfe_dma_timeout(void *arg);
221359575Sdimstatic void 		ctlfe_datamove_done(union ctl_io *io);
222359575Sdimstatic void 		ctlfe_dump(void);
223359575Sdim
224359575Sdimstatic struct periph_driver ctlfe_driver =
225359575Sdim{
226359575Sdim	ctlfeperiphinit, "ctl",
227359575Sdim	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0
228359575Sdim};
229359575Sdim
230359575Sdimstatic struct ctl_frontend ctlfe_frontend =
231359575Sdim{
232359575Sdim	.name = "camtarget",
233359575Sdim	.init = ctlfeinitialize,
234359575Sdim	.fe_dump = ctlfe_dump,
235359575Sdim	.shutdown = ctlfeshutdown,
236359575Sdim};
237359575SdimCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
238359575Sdim
239359575Sdimextern struct ctl_softc *control_softc;
240359575Sdim
241359575Sdimvoid
242359575Sdimctlfeshutdown(void)
243359575Sdim{
244359575Sdim	return;
245359575Sdim}
246359575Sdim
247359575Sdimint
248359575Sdimctlfeinitialize(void)
249359575Sdim{
250359575Sdim
251359575Sdim	STAILQ_INIT(&ctlfe_softc_list);
252359575Sdim	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
253359575Sdim	periphdriver_register(&ctlfe_driver);
254359575Sdim	return (0);
255359575Sdim}
256359575Sdim
257359575Sdimvoid
258359575Sdimctlfeperiphinit(void)
259359575Sdim{
260359575Sdim	cam_status status;
261359575Sdim
262359575Sdim	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
263359575Sdim				    AC_CONTRACT, ctlfeasync, NULL, NULL);
264359575Sdim	if (status != CAM_REQ_CMP) {
265359575Sdim		printf("ctl: Failed to attach async callback due to CAM "
266359575Sdim		       "status 0x%x!\n", status);
267359575Sdim	}
268359575Sdim}
269359575Sdim
270359575Sdimstatic void
271359575Sdimctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
272359575Sdim{
273359575Sdim
274359575Sdim#ifdef CTLFEDEBUG
275359575Sdim	printf("%s: entered\n", __func__);
276359575Sdim#endif
277359575Sdim
278359575Sdim	/*
279359575Sdim	 * When a new path gets registered, and it is capable of target
280359575Sdim	 * mode, go ahead and attach.  Later on, we may need to be more
281359575Sdim	 * selective, but for now this will be sufficient.
282359575Sdim 	 */
283359575Sdim	switch (code) {
284359575Sdim	case AC_PATH_REGISTERED: {
285359575Sdim		struct ctl_port *port;
286359575Sdim		struct ctlfe_softc *bus_softc;
287359575Sdim		struct ccb_pathinq *cpi;
288359575Sdim		int retval;
289359575Sdim
290359575Sdim		cpi = (struct ccb_pathinq *)arg;
291359575Sdim
292359575Sdim		/* Don't attach if it doesn't support target mode */
293359575Sdim		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
294359575Sdim#ifdef CTLFEDEBUG
295359575Sdim			printf("%s: SIM %s%d doesn't support target mode\n",
296359575Sdim			       __func__, cpi->dev_name, cpi->unit_number);
297359575Sdim#endif
298359575Sdim			break;
299359575Sdim		}
300359575Sdim
301359575Sdim#ifdef CTLFE_INIT_ENABLE
302359575Sdim		if (ctlfe_num_targets >= ctlfe_max_targets) {
303359575Sdim			union ccb *ccb;
304359575Sdim
305359575Sdim			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
306359575Sdim						  M_NOWAIT | M_ZERO);
307359575Sdim			if (ccb == NULL) {
308359575Sdim				printf("%s: unable to malloc CCB!\n", __func__);
309359575Sdim				return;
310359575Sdim			}
311359575Sdim			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
312359575Sdim
313359575Sdim			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
314359575Sdim			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
315359575Sdim			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
316359575Sdim
317359575Sdim			xpt_action(ccb);
318359575Sdim
319359575Sdim			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
320359575Sdim			     CAM_REQ_CMP) {
321359575Sdim				printf("%s: SIM %s%d (path id %d) initiator "
322359575Sdim				       "enable failed with status %#x\n",
323359575Sdim				       __func__, cpi->dev_name,
324359575Sdim				       cpi->unit_number, cpi->ccb_h.path_id,
325359575Sdim				       ccb->ccb_h.status);
326359575Sdim			} else {
327359575Sdim				printf("%s: SIM %s%d (path id %d) initiator "
328359575Sdim				       "enable succeeded\n",
329359575Sdim				       __func__, cpi->dev_name,
330359575Sdim				       cpi->unit_number, cpi->ccb_h.path_id);
331359575Sdim			}
332359575Sdim
333359575Sdim			free(ccb, M_TEMP);
334359575Sdim
335359575Sdim			break;
336359575Sdim		} else {
337359575Sdim			ctlfe_num_targets++;
338359575Sdim		}
339359575Sdim
340359575Sdim		printf("%s: ctlfe_num_targets = %d\n", __func__,
341359575Sdim		       ctlfe_num_targets);
342359575Sdim#endif /* CTLFE_INIT_ENABLE */
343359575Sdim
344359575Sdim		/*
345359575Sdim		 * We're in an interrupt context here, so we have to
346359575Sdim		 * use M_NOWAIT.  Of course this means trouble if we
347359575Sdim		 * can't allocate memory.
348359575Sdim		 */
349359575Sdim		bus_softc = malloc(sizeof(*bus_softc), M_CTLFE,
350359575Sdim				   M_NOWAIT | M_ZERO);
351359575Sdim		if (bus_softc == NULL) {
352359575Sdim			printf("%s: unable to malloc %zd bytes for softc\n",
353359575Sdim			       __func__, sizeof(*bus_softc));
354359575Sdim			return;
355359575Sdim		}
356359575Sdim
357359575Sdim		bus_softc->path_id = cpi->ccb_h.path_id;
358359575Sdim		bus_softc->sim = xpt_path_sim(path);
359359575Sdim		if (cpi->maxio != 0)
360359575Sdim			bus_softc->maxio = cpi->maxio;
361359575Sdim		else
362359575Sdim			bus_softc->maxio = DFLTPHYS;
363359575Sdim		mtx_init(&bus_softc->lun_softc_mtx, "LUN softc mtx", NULL,
364359575Sdim		    MTX_DEF);
365359575Sdim		STAILQ_INIT(&bus_softc->lun_softc_list);
366359575Sdim
367359575Sdim		port = &bus_softc->port;
368359575Sdim		port->frontend = &ctlfe_frontend;
369359575Sdim
370359575Sdim		/*
371359575Sdim		 * XXX KDM should we be more accurate here ?
372359575Sdim		 */
373359575Sdim		if (cpi->transport == XPORT_FC)
374359575Sdim			port->port_type = CTL_PORT_FC;
375359575Sdim		else if (cpi->transport == XPORT_SAS)
376359575Sdim			port->port_type = CTL_PORT_SAS;
377359575Sdim		else
378359575Sdim			port->port_type = CTL_PORT_SCSI;
379359575Sdim
380359575Sdim		/* XXX KDM what should the real number be here? */
381359575Sdim		port->num_requested_ctl_io = 4096;
382359575Sdim		snprintf(bus_softc->port_name, sizeof(bus_softc->port_name),
383359575Sdim			 "%s%d", cpi->dev_name, cpi->unit_number);
384359575Sdim		/*
385359575Sdim		 * XXX KDM it would be nice to allocate storage in the
386359575Sdim		 * frontend structure itself.
387359575Sdim	 	 */
388359575Sdim		port->port_name = bus_softc->port_name;
389359575Sdim		port->physical_port = cpi->unit_number;
390359575Sdim		port->virtual_port = cpi->bus_id;
391359575Sdim		port->port_online = ctlfe_online;
392359575Sdim		port->port_offline = ctlfe_offline;
393359575Sdim		port->onoff_arg = bus_softc;
394359575Sdim		port->lun_enable = ctlfe_lun_enable;
395359575Sdim		port->lun_disable = ctlfe_lun_disable;
396359575Sdim		port->targ_lun_arg = bus_softc;
397359575Sdim		port->fe_datamove = ctlfe_datamove_done;
398359575Sdim		port->fe_done = ctlfe_datamove_done;
399359575Sdim		/*
400359575Sdim		 * XXX KDM the path inquiry doesn't give us the maximum
401359575Sdim		 * number of targets supported.
402359575Sdim		 */
403359575Sdim		port->max_targets = cpi->max_target;
404359575Sdim		port->max_target_id = cpi->max_target;
405359575Sdim
406359575Sdim		/*
407359575Sdim		 * XXX KDM need to figure out whether we're the master or
408359575Sdim		 * slave.
409359575Sdim		 */
410359575Sdim#ifdef CTLFEDEBUG
411359575Sdim		printf("%s: calling ctl_port_register() for %s%d\n",
412359575Sdim		       __func__, cpi->dev_name, cpi->unit_number);
413359575Sdim#endif
414359575Sdim		retval = ctl_port_register(port, /*master_SC*/ 1);
415359575Sdim		if (retval != 0) {
416359575Sdim			printf("%s: ctl_port_register() failed with "
417359575Sdim			       "error %d!\n", __func__, retval);
418359575Sdim			mtx_destroy(&bus_softc->lun_softc_mtx);
419359575Sdim			free(bus_softc, M_CTLFE);
420359575Sdim			break;
421359575Sdim		} else {
422359575Sdim			mtx_lock(&ctlfe_list_mtx);
423359575Sdim			STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links);
424359575Sdim			mtx_unlock(&ctlfe_list_mtx);
425359575Sdim		}
426359575Sdim
427359575Sdim		break;
428359575Sdim	}
429359575Sdim	case AC_PATH_DEREGISTERED: {
430359575Sdim		struct ctlfe_softc *softc = NULL;
431359575Sdim
432359575Sdim		mtx_lock(&ctlfe_list_mtx);
433359575Sdim		STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
434359575Sdim			if (softc->path_id == xpt_path_path_id(path)) {
435359575Sdim				STAILQ_REMOVE(&ctlfe_softc_list, softc,
436359575Sdim						ctlfe_softc, links);
437359575Sdim				break;
438359575Sdim			}
439359575Sdim		}
440359575Sdim		mtx_unlock(&ctlfe_list_mtx);
441359575Sdim
442359575Sdim		if (softc != NULL) {
443359575Sdim			/*
444359575Sdim			 * XXX KDM are we certain at this point that there
445359575Sdim			 * are no outstanding commands for this frontend?
446359575Sdim			 */
447359575Sdim			ctl_port_deregister(&softc->port);
448359575Sdim			mtx_destroy(&softc->lun_softc_mtx);
449359575Sdim			free(softc, M_CTLFE);
450359575Sdim		}
451359575Sdim		break;
452359575Sdim	}
453359575Sdim	case AC_CONTRACT: {
454359575Sdim		struct ac_contract *ac;
455359575Sdim
456359575Sdim		ac = (struct ac_contract *)arg;
457359575Sdim
458359575Sdim		switch (ac->contract_number) {
459359575Sdim		case AC_CONTRACT_DEV_CHG: {
460359575Sdim			struct ac_device_changed *dev_chg;
461359575Sdim			struct ctlfe_softc *softc;
462359575Sdim			int retval, found;
463359575Sdim
464359575Sdim			dev_chg = (struct ac_device_changed *)ac->contract_data;
465359575Sdim
466359575Sdim			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
467359575Sdim			       __func__, dev_chg->wwpn, dev_chg->port,
468359575Sdim			       xpt_path_path_id(path), dev_chg->target,
469359575Sdim			       (dev_chg->arrived == 0) ?  "left" : "arrived");
470359575Sdim
471359575Sdim			found = 0;
472359575Sdim
473359575Sdim			mtx_lock(&ctlfe_list_mtx);
474359575Sdim			STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
475359575Sdim				if (softc->path_id == xpt_path_path_id(path)) {
476359575Sdim					found = 1;
477359575Sdim					break;
478359575Sdim				}
479359575Sdim			}
480359575Sdim			mtx_unlock(&ctlfe_list_mtx);
481359575Sdim
482359575Sdim			if (found == 0) {
483359575Sdim				printf("%s: CTL port for CAM path %u not "
484359575Sdim				       "found!\n", __func__,
485359575Sdim				       xpt_path_path_id(path));
486359575Sdim				break;
487359575Sdim			}
488359575Sdim			if (dev_chg->arrived != 0) {
489359575Sdim				retval = ctl_add_initiator(&softc->port,
490359575Sdim				    dev_chg->target, dev_chg->wwpn, NULL);
491359575Sdim			} else {
492359575Sdim				retval = ctl_remove_initiator(&softc->port,
493359575Sdim				    dev_chg->target);
494359575Sdim			}
495359575Sdim
496359575Sdim			if (retval < 0) {
497359575Sdim				printf("%s: could not %s port %d iid %u "
498359575Sdim				       "WWPN %#jx!\n", __func__,
499359575Sdim				       (dev_chg->arrived != 0) ? "add" :
500359575Sdim				       "remove", softc->port.targ_port,
501359575Sdim				       dev_chg->target,
502359575Sdim				       (uintmax_t)dev_chg->wwpn);
503359575Sdim			}
504359575Sdim			break;
505359575Sdim		}
506359575Sdim		default:
507359575Sdim			printf("%s: unsupported contract number %ju\n",
508359575Sdim			       __func__, (uintmax_t)ac->contract_number);
509359575Sdim			break;
510359575Sdim		}
511359575Sdim		break;
512359575Sdim	}
513359575Sdim	default:
514359575Sdim		break;
515359575Sdim	}
516359575Sdim}
517359575Sdim
518359575Sdimstatic cam_status
519359575Sdimctlferegister(struct cam_periph *periph, void *arg)
520359575Sdim{
521359575Sdim	struct ctlfe_softc *bus_softc;
522359575Sdim	struct ctlfe_lun_softc *softc;
523359575Sdim	union ccb en_lun_ccb;
524359575Sdim	cam_status status;
525359575Sdim	int i;
526359575Sdim
527359575Sdim	softc = (struct ctlfe_lun_softc *)arg;
528359575Sdim	bus_softc = softc->parent_softc;
529359575Sdim
530359575Sdim	TAILQ_INIT(&softc->work_queue);
531359575Sdim	softc->periph = periph;
532359575Sdim
533359575Sdim	callout_init_mtx(&softc->dma_callout, xpt_path_mtx(periph->path),
534359575Sdim	    /*flags*/ 0);
535359575Sdim	periph->softc = softc;
536359575Sdim
537359575Sdim	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
538359575Sdim	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
539359575Sdim	en_lun_ccb.cel.grp6_len = 0;
540359575Sdim	en_lun_ccb.cel.grp7_len = 0;
541359575Sdim	en_lun_ccb.cel.enable = 1;
542359575Sdim	xpt_action(&en_lun_ccb);
543359575Sdim	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
544359575Sdim	if (status != CAM_REQ_CMP) {
545359575Sdim		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
546359575Sdim			  __func__, en_lun_ccb.ccb_h.status);
547359575Sdim		return (status);
548359575Sdim	}
549359575Sdim
550359575Sdim	status = CAM_REQ_CMP;
551359575Sdim
552359575Sdim	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
553359575Sdim		union ccb *new_ccb;
554359575Sdim
555359575Sdim		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
556359575Sdim					      M_ZERO|M_NOWAIT);
557359575Sdim		if (new_ccb == NULL) {
558359575Sdim			status = CAM_RESRC_UNAVAIL;
559359575Sdim			break;
560359575Sdim		}
561359575Sdim		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
562359575Sdim		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
563359575Sdim		new_ccb->ccb_h.cbfcnp = ctlfedone;
564359575Sdim		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
565359575Sdim		xpt_action(new_ccb);
566359575Sdim		softc->atios_sent++;
567359575Sdim		status = new_ccb->ccb_h.status;
568359575Sdim		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
569359575Sdim			free(new_ccb, M_CTLFE);
570359575Sdim			break;
571359575Sdim		}
572359575Sdim	}
573359575Sdim
574359575Sdim	status = cam_periph_acquire(periph);
575359575Sdim	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
576359575Sdim		xpt_print(periph->path, "%s: could not acquire reference "
577359575Sdim			  "count, status = %#x\n", __func__, status);
578359575Sdim		return (status);
579359575Sdim	}
580359575Sdim
581359575Sdim	if (i == 0) {
582359575Sdim		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
583359575Sdim			  "status 0x%x\n", __func__, status);
584359575Sdim		return (CAM_REQ_CMP_ERR);
585359575Sdim	}
586359575Sdim
587359575Sdim	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
588359575Sdim		union ccb *new_ccb;
589359575Sdim
590359575Sdim		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
591359575Sdim					      M_ZERO|M_NOWAIT);
592359575Sdim		if (new_ccb == NULL) {
593359575Sdim			status = CAM_RESRC_UNAVAIL;
594359575Sdim			break;
595359575Sdim		}
596359575Sdim
597359575Sdim		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
598359575Sdim		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
599359575Sdim		new_ccb->ccb_h.cbfcnp = ctlfedone;
600359575Sdim		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
601359575Sdim		xpt_action(new_ccb);
602359575Sdim		softc->inots_sent++;
603359575Sdim		status = new_ccb->ccb_h.status;
604359575Sdim		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
605359575Sdim			/*
606359575Sdim			 * Note that we don't free the CCB here.  If the
607359575Sdim			 * status is not CAM_REQ_INPROG, then we're
608359575Sdim			 * probably talking to a SIM that says it is
609359575Sdim			 * target-capable but doesn't support the
610359575Sdim			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
611359575Sdim			 * older API.  In that case, it'll call xpt_done()
612359575Sdim			 * on the CCB, and we need to free it in our done
613359575Sdim			 * routine as a result.
614359575Sdim			 */
615359575Sdim			break;
616359575Sdim		}
617359575Sdim	}
618359575Sdim	if ((i == 0)
619359575Sdim	 || (status != CAM_REQ_INPROG)) {
620359575Sdim		xpt_print(periph->path, "%s: could not allocate immediate "
621359575Sdim			  "notify CCBs, status 0x%x\n", __func__, status);
622359575Sdim		return (CAM_REQ_CMP_ERR);
623359575Sdim	}
624359575Sdim	return (CAM_REQ_CMP);
625359575Sdim}
626359575Sdim
627359575Sdimstatic void
628359575Sdimctlfeoninvalidate(struct cam_periph *periph)
629359575Sdim{
630359575Sdim	union ccb en_lun_ccb;
631359575Sdim	cam_status status;
632359575Sdim	struct ctlfe_softc *bus_softc;
633359575Sdim	struct ctlfe_lun_softc *softc;
634359575Sdim
635359575Sdim	softc = (struct ctlfe_lun_softc *)periph->softc;
636359575Sdim
637359575Sdim	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
638359575Sdim	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
639359575Sdim	en_lun_ccb.cel.grp6_len = 0;
640359575Sdim	en_lun_ccb.cel.grp7_len = 0;
641359575Sdim	en_lun_ccb.cel.enable = 0;
642359575Sdim	xpt_action(&en_lun_ccb);
643359575Sdim	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
644359575Sdim	if (status != CAM_REQ_CMP) {
645359575Sdim		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
646359575Sdim			  __func__, en_lun_ccb.ccb_h.status);
647359575Sdim		/*
648359575Sdim		 * XXX KDM what do we do now?
649359575Sdim		 */
650359575Sdim	}
651359575Sdim	xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
652359575Sdim		  "INOTs outstanding, %d refs\n", softc->atios_sent -
653359575Sdim		  softc->atios_returned, softc->inots_sent -
654359575Sdim		  softc->inots_returned, periph->refcount);
655359575Sdim
656359575Sdim	bus_softc = softc->parent_softc;
657359575Sdim	mtx_lock(&bus_softc->lun_softc_mtx);
658359575Sdim	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
659359575Sdim	mtx_unlock(&bus_softc->lun_softc_mtx);
660359575Sdim}
661359575Sdim
662359575Sdimstatic void
663359575Sdimctlfecleanup(struct cam_periph *periph)
664359575Sdim{
665359575Sdim	struct ctlfe_lun_softc *softc;
666359575Sdim
667359575Sdim	xpt_print(periph->path, "%s: Called\n", __func__);
668359575Sdim
669359575Sdim	softc = (struct ctlfe_lun_softc *)periph->softc;
670359575Sdim
671359575Sdim	/*
672359575Sdim	 * XXX KDM is there anything else that needs to be done here?
673359575Sdim	 */
674359575Sdim
675359575Sdim	callout_stop(&softc->dma_callout);
676359575Sdim
677359575Sdim	free(softc, M_CTLFE);
678359575Sdim}
679359575Sdim
680359575Sdimstatic void
681359575Sdimctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
682359575Sdim    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
683359575Sdim    u_int16_t *sglist_cnt)
684359575Sdim{
685359575Sdim	struct ctlfe_softc *bus_softc;
686359575Sdim	struct ctlfe_lun_cmd_info *cmd_info;
687359575Sdim	struct ctl_sg_entry *ctl_sglist;
688359575Sdim	bus_dma_segment_t *cam_sglist;
689359575Sdim	size_t off;
690359575Sdim	int i, idx;
691359575Sdim
692359575Sdim	cmd_info = (struct ctlfe_lun_cmd_info *)io->io_hdr.port_priv;
693359575Sdim	bus_softc = softc->parent_softc;
694359575Sdim
695359575Sdim	/*
696359575Sdim	 * Set the direction, relative to the initiator.
697359575Sdim	 */
698359575Sdim	*flags &= ~CAM_DIR_MASK;
699359575Sdim	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
700359575Sdim		*flags |= CAM_DIR_IN;
701359575Sdim	else
702359575Sdim		*flags |= CAM_DIR_OUT;
703359575Sdim
704359575Sdim	*flags &= ~CAM_DATA_MASK;
705359575Sdim	idx = cmd_info->cur_transfer_index;
706359575Sdim	off = cmd_info->cur_transfer_off;
707359575Sdim	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
708359575Sdim	if (io->scsiio.kern_sg_entries == 0) {
709359575Sdim		/* No S/G list. */
710359575Sdim		*data_ptr = io->scsiio.kern_data_ptr + off;
711359575Sdim		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
712359575Sdim			*dxfer_len = io->scsiio.kern_data_len - off;
713359575Sdim		} else {
714359575Sdim			*dxfer_len = bus_softc->maxio;
715359575Sdim			cmd_info->cur_transfer_index = -1;
716359575Sdim			cmd_info->cur_transfer_off = bus_softc->maxio;
717359575Sdim			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
718359575Sdim		}
719359575Sdim		*sglist_cnt = 0;
720359575Sdim
721359575Sdim		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
722359575Sdim			*flags |= CAM_DATA_PADDR;
723359575Sdim		else
724359575Sdim			*flags |= CAM_DATA_VADDR;
725359575Sdim	} else {
726359575Sdim		/* S/G list with physical or virtual pointers. */
727359575Sdim		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
728359575Sdim		cam_sglist = cmd_info->cam_sglist;
729359575Sdim		*dxfer_len = 0;
730359575Sdim		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
731359575Sdim			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
732359575Sdim			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
733359575Sdim				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
734359575Sdim				*dxfer_len += cam_sglist[i].ds_len;
735359575Sdim			} else {
736359575Sdim				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
737359575Sdim				cmd_info->cur_transfer_index = idx + i;
738359575Sdim				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
739359575Sdim				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
740359575Sdim				*dxfer_len += cam_sglist[i].ds_len;
741359575Sdim				if (ctl_sglist[i].len != 0)
742359575Sdim					i++;
743359575Sdim				break;
744359575Sdim			}
745359575Sdim			if (i == (CTLFE_MAX_SEGS - 1) &&
746359575Sdim			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
747359575Sdim				cmd_info->cur_transfer_index = idx + i + 1;
748359575Sdim				cmd_info->cur_transfer_off = 0;
749359575Sdim				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
750359575Sdim				i++;
751359575Sdim				break;
752359575Sdim			}
753359575Sdim			off = 0;
754359575Sdim		}
755359575Sdim		*sglist_cnt = i;
756359575Sdim		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
757359575Sdim			*flags |= CAM_DATA_SG_PADDR;
758359575Sdim		else
759359575Sdim			*flags |= CAM_DATA_SG;
760359575Sdim		*data_ptr = (uint8_t *)cam_sglist;
761359575Sdim	}
762359575Sdim}
763359575Sdim
764359575Sdimstatic void
765359575Sdimctlfestart(struct cam_periph *periph, union ccb *start_ccb)
766359575Sdim{
767359575Sdim	struct ctlfe_lun_softc *softc;
768359575Sdim	struct ccb_hdr *ccb_h;
769359575Sdim
770359575Sdim	softc = (struct ctlfe_lun_softc *)periph->softc;
771359575Sdim
772359575Sdim	softc->ccbs_alloced++;
773359575Sdim
774359575Sdim	start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT;
775359575Sdim
776359575Sdim	ccb_h = TAILQ_FIRST(&softc->work_queue);
777359575Sdim	if (ccb_h == NULL) {
778359575Sdim		softc->ccbs_freed++;
779359575Sdim		xpt_release_ccb(start_ccb);
780359575Sdim	} else {
781359575Sdim		struct ccb_accept_tio *atio;
782359575Sdim		struct ccb_scsiio *csio;
783359575Sdim		uint8_t *data_ptr;
784359575Sdim		uint32_t dxfer_len;
785359575Sdim		ccb_flags flags;
786359575Sdim		union ctl_io *io;
787359575Sdim		uint8_t scsi_status;
788359575Sdim
789359575Sdim		/* Take the ATIO off the work queue */
790359575Sdim		TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
791359575Sdim		atio = (struct ccb_accept_tio *)ccb_h;
792359575Sdim		io = (union ctl_io *)ccb_h->io_ptr;
793359575Sdim		csio = &start_ccb->csio;
794359575Sdim
795359575Sdim		flags = atio->ccb_h.flags &
796359575Sdim			(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
797359575Sdim
798359575Sdim		if ((io == NULL)
799359575Sdim		 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
800359575Sdim			/*
801359575Sdim			 * We're done, send status back.
802359575Sdim			 */
803359575Sdim			flags |= CAM_SEND_STATUS;
804359575Sdim			if (io == NULL) {
805359575Sdim				scsi_status = SCSI_STATUS_BUSY;
806359575Sdim				csio->sense_len = 0;
807359575Sdim			} else if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
808359575Sdim			    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
809359575Sdim				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
810359575Sdim
811359575Sdim				/*
812359575Sdim				 * If this command was aborted, we don't
813359575Sdim				 * need to send status back to the SIM.
814359575Sdim				 * Just free the CTIO and ctl_io, and
815359575Sdim				 * recycle the ATIO back to the SIM.
816359575Sdim				 */
817359575Sdim				xpt_print(periph->path, "%s: aborted "
818359575Sdim					  "command 0x%04x discarded\n",
819359575Sdim					  __func__, io->scsiio.tag_num);
820359575Sdim				ctl_free_io(io);
821359575Sdim				/*
822359575Sdim				 * For a wildcard attachment, commands can
823359575Sdim				 * come in with a specific target/lun.  Reset
824359575Sdim				 * the target and LUN fields back to the
825359575Sdim				 * wildcard values before we send them back
826359575Sdim				 * down to the SIM.  The SIM has a wildcard
827359575Sdim				 * LUN enabled, not whatever target/lun
828359575Sdim				 * these happened to be.
829359575Sdim				 */
830359575Sdim				if (softc->flags & CTLFE_LUN_WILDCARD) {
831359575Sdim					atio->ccb_h.target_id =
832359575Sdim						CAM_TARGET_WILDCARD;
833359575Sdim					atio->ccb_h.target_lun =
834359575Sdim						CAM_LUN_WILDCARD;
835359575Sdim				}
836359575Sdim
837359575Sdim				if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
838359575Sdim					cam_release_devq(periph->path,
839359575Sdim							 /*relsim_flags*/0,
840359575Sdim							 /*reduction*/0,
841359575Sdim 							 /*timeout*/0,
842359575Sdim							 /*getcount_only*/0);
843359575Sdim					atio->ccb_h.status &= ~CAM_DEV_QFRZN;
844359575Sdim				}
845359575Sdim
846359575Sdim				ccb_h = TAILQ_FIRST(&softc->work_queue);
847359575Sdim
848359575Sdim				if (atio->ccb_h.func_code !=
849359575Sdim				    XPT_ACCEPT_TARGET_IO) {
850359575Sdim					xpt_print(periph->path, "%s: func_code "
851359575Sdim						  "is %#x\n", __func__,
852359575Sdim						  atio->ccb_h.func_code);
853359575Sdim				}
854359575Sdim				start_ccb->ccb_h.func_code = XPT_ABORT;
855359575Sdim				start_ccb->cab.abort_ccb = (union ccb *)atio;
856359575Sdim
857359575Sdim				/* Tell the SIM that we've aborted this ATIO */
858359575Sdim				xpt_action(start_ccb);
859359575Sdim				softc->ccbs_freed++;
860359575Sdim				xpt_release_ccb(start_ccb);
861359575Sdim
862359575Sdim				/*
863359575Sdim				 * Send the ATIO back down to the SIM.
864359575Sdim				 */
865359575Sdim				xpt_action((union ccb *)atio);
866359575Sdim				softc->atios_sent++;
867359575Sdim
868359575Sdim				/*
869359575Sdim				 * If we still have work to do, ask for
870359575Sdim				 * another CCB.  Otherwise, deactivate our
871359575Sdim				 * callout.
872359575Sdim				 */
873359575Sdim				if (ccb_h != NULL)
874359575Sdim					xpt_schedule(periph, /*priority*/ 1);
875359575Sdim				else
876359575Sdim					callout_stop(&softc->dma_callout);
877359575Sdim
878359575Sdim				return;
879359575Sdim			} else {
880359575Sdim				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
881359575Sdim				scsi_status = io->scsiio.scsi_status;
882359575Sdim				csio->sense_len = io->scsiio.sense_len;
883359575Sdim			}
884359575Sdim			data_ptr = NULL;
885359575Sdim			dxfer_len = 0;
886359575Sdim			if (io == NULL) {
887359575Sdim				printf("%s: tag %04x io is NULL\n", __func__,
888359575Sdim				       atio->tag_id);
889359575Sdim			} else {
890359575Sdim#ifdef CTLFEDEBUG
891359575Sdim				printf("%s: tag %04x status %x\n", __func__,
892359575Sdim				       atio->tag_id, io->io_hdr.status);
893359575Sdim#endif
894359575Sdim			}
895359575Sdim			csio->sglist_cnt = 0;
896359575Sdim			if (csio->sense_len != 0) {
897359575Sdim				csio->sense_data = io->scsiio.sense_data;
898359575Sdim				flags |= CAM_SEND_SENSE;
899359575Sdim			} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
900359575Sdim				xpt_print(periph->path, "%s: check condition "
901359575Sdim					  "with no sense\n", __func__);
902359575Sdim			}
903359575Sdim		} else {
904359575Sdim			struct ctlfe_lun_cmd_info *cmd_info;
905359575Sdim
906359575Sdim			/*
907359575Sdim			 * Datamove call, we need to setup the S/G list.
908359575Sdim			 */
909359575Sdim
910359575Sdim			cmd_info = (struct ctlfe_lun_cmd_info *)
911359575Sdim				io->io_hdr.port_priv;
912359575Sdim
913359575Sdim			KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
914359575Sdim				("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
915359575Sdim				"CTL_PORT_PRIV_SIZE %d", __func__,
916359575Sdim				sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
917359575Sdim			io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
918359575Sdim
919359575Sdim			/*
920359575Sdim			 * Need to zero this, in case it has been used for
921359575Sdim			 * a previous datamove for this particular I/O.
922359575Sdim			 */
923359575Sdim			bzero(cmd_info, sizeof(*cmd_info));
924359575Sdim			scsi_status = 0;
925359575Sdim
926359575Sdim			csio->cdb_len = atio->cdb_len;
927359575Sdim
928359575Sdim			ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
929359575Sdim			    &csio->sglist_cnt);
930359575Sdim
931359575Sdim			io->scsiio.ext_data_filled += dxfer_len;
932359575Sdim
933359575Sdim			if (io->scsiio.ext_data_filled >
934359575Sdim			    io->scsiio.kern_total_len) {
935359575Sdim				xpt_print(periph->path, "%s: tag 0x%04x "
936359575Sdim					  "fill len %u > total %u\n",
937359575Sdim					  __func__, io->scsiio.tag_num,
938359575Sdim					  io->scsiio.ext_data_filled,
939359575Sdim					  io->scsiio.kern_total_len);
940359575Sdim			}
941359575Sdim		}
942359575Sdim
943359575Sdim#ifdef CTLFEDEBUG
944359575Sdim		printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
945359575Sdim		       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
946359575Sdim		       atio->tag_id, flags, data_ptr, dxfer_len);
947359575Sdim#endif
948359575Sdim
949359575Sdim		/*
950359575Sdim		 * Valid combinations:
951359575Sdim		 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
952359575Sdim		 *    sglist_cnt = 0
953359575Sdim		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
954359575Sdim		 *    sglist_cnt = 0
955359575Sdim		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
956359575Sdim		 *    sglist_cnt != 0
957359575Sdim		 */
958359575Sdim#ifdef CTLFEDEBUG
959359575Sdim		if (((flags & CAM_SEND_STATUS)
960359575Sdim		  && (((flags & CAM_DATA_SG) != 0)
961359575Sdim		   || (dxfer_len != 0)
962359575Sdim		   || (csio->sglist_cnt != 0)))
963359575Sdim		 || (((flags & CAM_SEND_STATUS) == 0)
964359575Sdim		  && (dxfer_len == 0))
965359575Sdim		 || ((flags & CAM_DATA_SG)
966359575Sdim		  && (csio->sglist_cnt == 0))
967359575Sdim		 || (((flags & CAM_DATA_SG) == 0)
968359575Sdim		  && (csio->sglist_cnt != 0))) {
969359575Sdim			printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
970359575Sdim			       "%d sg %u\n", __func__, atio->tag_id,
971359575Sdim			       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
972359575Sdim			       csio->sglist_cnt);
973359575Sdim			if (io != NULL) {
974359575Sdim				printf("%s: tag %04x io status %#x\n", __func__,
975359575Sdim				       atio->tag_id, io->io_hdr.status);
976359575Sdim			} else {
977359575Sdim				printf("%s: tag %04x no associated io\n",
978359575Sdim				       __func__, atio->tag_id);
979359575Sdim			}
980359575Sdim		}
981359575Sdim#endif
982359575Sdim		cam_fill_ctio(csio,
983359575Sdim			      /*retries*/ 2,
984359575Sdim			      ctlfedone,
985359575Sdim			      flags,
986359575Sdim			      (flags & CAM_TAG_ACTION_VALID) ?
987359575Sdim			       MSG_SIMPLE_Q_TAG : 0,
988359575Sdim			      atio->tag_id,
989359575Sdim			      atio->init_id,
990359575Sdim			      scsi_status,
991359575Sdim			      /*data_ptr*/ data_ptr,
992359575Sdim			      /*dxfer_len*/ dxfer_len,
993359575Sdim			      /*timeout*/ 5 * 1000);
994359575Sdim		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
995359575Sdim		start_ccb->ccb_h.ccb_atio = atio;
996359575Sdim		if (((flags & CAM_SEND_STATUS) == 0)
997359575Sdim		 && (io != NULL))
998359575Sdim			io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
999359575Sdim
1000359575Sdim		softc->ctios_sent++;
1001359575Sdim
1002359575Sdim		cam_periph_unlock(periph);
1003359575Sdim		xpt_action(start_ccb);
1004359575Sdim		cam_periph_lock(periph);
1005359575Sdim
1006359575Sdim		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1007359575Sdim			cam_release_devq(periph->path,
1008359575Sdim					 /*relsim_flags*/0,
1009359575Sdim					 /*reduction*/0,
1010359575Sdim 					 /*timeout*/0,
1011359575Sdim					 /*getcount_only*/0);
1012359575Sdim			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
1013359575Sdim		}
1014359575Sdim
1015359575Sdim		ccb_h = TAILQ_FIRST(&softc->work_queue);
1016359575Sdim	}
1017359575Sdim	/*
1018359575Sdim	 * If we still have work to do, ask for another CCB.  Otherwise,
1019359575Sdim	 * deactivate our callout.
1020359575Sdim	 */
1021359575Sdim	if (ccb_h != NULL)
1022359575Sdim		xpt_schedule(periph, /*priority*/ 1);
1023359575Sdim	else
1024359575Sdim		callout_stop(&softc->dma_callout);
1025359575Sdim}
1026359575Sdim
1027359575Sdimstatic void
1028359575Sdimctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
1029359575Sdim{
1030359575Sdim	struct ctlfe_lun_softc *softc;
1031359575Sdim
1032359575Sdim	softc = (struct ctlfe_lun_softc *)periph->softc;
1033359575Sdim
1034359575Sdim	switch (ccb->ccb_h.func_code) {
1035359575Sdim	case XPT_ACCEPT_TARGET_IO:
1036359575Sdim		softc->atios_returned++;
1037359575Sdim		break;
1038359575Sdim	case XPT_IMMEDIATE_NOTIFY:
1039359575Sdim	case XPT_NOTIFY_ACKNOWLEDGE:
1040359575Sdim		softc->inots_returned++;
1041359575Sdim		break;
1042359575Sdim	default:
1043359575Sdim		break;
1044359575Sdim	}
1045359575Sdim
1046359575Sdim	free(ccb, M_CTLFE);
1047359575Sdim
1048359575Sdim	KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1049359575Sdim		"atios_returned %ju > atios_sent %ju", __func__,
1050359575Sdim		softc->atios_returned, softc->atios_sent));
1051359575Sdim	KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1052359575Sdim		"inots_returned %ju > inots_sent %ju", __func__,
1053359575Sdim		softc->inots_returned, softc->inots_sent));
1054359575Sdim
1055359575Sdim	/*
1056359575Sdim	 * If we have received all of our CCBs, we can release our
1057359575Sdim	 * reference on the peripheral driver.  It will probably go away
1058359575Sdim	 * now.
1059359575Sdim	 */
1060359575Sdim	if ((softc->atios_returned == softc->atios_sent)
1061359575Sdim	 && (softc->inots_returned == softc->inots_sent)) {
1062359575Sdim		cam_periph_release_locked(periph);
1063359575Sdim	}
1064359575Sdim}
1065359575Sdim
1066359575Sdimstatic int
1067359575Sdimctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1068359575Sdim{
1069359575Sdim	uint64_t lba;
1070359575Sdim	uint32_t num_blocks, nbc;
1071359575Sdim	uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1072359575Sdim	    atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1073359575Sdim
1074359575Sdim	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1075
1076	switch (cmdbyt[0]) {
1077	case READ_6:
1078	case WRITE_6:
1079	{
1080		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1081		lba = scsi_3btoul(cdb->addr);
1082		lba &= 0x1fffff;
1083		num_blocks = cdb->length;
1084		if (num_blocks == 0)
1085			num_blocks = 256;
1086		lba += nbc;
1087		num_blocks -= nbc;
1088		scsi_ulto3b(lba, cdb->addr);
1089		cdb->length = num_blocks;
1090		break;
1091	}
1092	case READ_10:
1093	case WRITE_10:
1094	{
1095		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1096		lba = scsi_4btoul(cdb->addr);
1097		num_blocks = scsi_2btoul(cdb->length);
1098		lba += nbc;
1099		num_blocks -= nbc;
1100		scsi_ulto4b(lba, cdb->addr);
1101		scsi_ulto2b(num_blocks, cdb->length);
1102		break;
1103	}
1104	case READ_12:
1105	case WRITE_12:
1106	{
1107		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1108		lba = scsi_4btoul(cdb->addr);
1109		num_blocks = scsi_4btoul(cdb->length);
1110		lba += nbc;
1111		num_blocks -= nbc;
1112		scsi_ulto4b(lba, cdb->addr);
1113		scsi_ulto4b(num_blocks, cdb->length);
1114		break;
1115	}
1116	case READ_16:
1117	case WRITE_16:
1118	case WRITE_ATOMIC_16:
1119	{
1120		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1121		lba = scsi_8btou64(cdb->addr);
1122		num_blocks = scsi_4btoul(cdb->length);
1123		lba += nbc;
1124		num_blocks -= nbc;
1125		scsi_u64to8b(lba, cdb->addr);
1126		scsi_ulto4b(num_blocks, cdb->length);
1127		break;
1128	}
1129	default:
1130		return -1;
1131	}
1132	return (0);
1133}
1134
1135static void
1136ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1137{
1138	struct ctlfe_lun_softc *softc;
1139	struct ctlfe_softc *bus_softc;
1140	struct ccb_accept_tio *atio = NULL;
1141	union ctl_io *io = NULL;
1142	struct mtx *mtx;
1143
1144	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1145	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1146#ifdef CTLFE_DEBUG
1147	printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
1148	       done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
1149#endif
1150
1151	softc = (struct ctlfe_lun_softc *)periph->softc;
1152	bus_softc = softc->parent_softc;
1153	mtx = cam_periph_mtx(periph);
1154	mtx_lock(mtx);
1155
1156	/*
1157	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1158	 * need to be freed.  Most of the ATIOs and INOTs that come back
1159	 * will be CCBs that are being returned from the SIM as a result of
1160	 * our disabling the LUN.
1161	 *
1162	 * Other CCB types are handled in their respective cases below.
1163	 */
1164	if (periph->flags & CAM_PERIPH_INVALID) {
1165		switch (done_ccb->ccb_h.func_code) {
1166		case XPT_ACCEPT_TARGET_IO:
1167		case XPT_IMMEDIATE_NOTIFY:
1168		case XPT_NOTIFY_ACKNOWLEDGE:
1169			ctlfe_free_ccb(periph, done_ccb);
1170			goto out;
1171		default:
1172			break;
1173		}
1174
1175	}
1176	switch (done_ccb->ccb_h.func_code) {
1177	case XPT_ACCEPT_TARGET_IO: {
1178
1179		atio = &done_ccb->atio;
1180
1181		softc->atios_returned++;
1182
1183 resubmit:
1184		/*
1185		 * Allocate a ctl_io, pass it to CTL, and wait for the
1186		 * datamove or done.
1187		 */
1188		io = ctl_alloc_io(bus_softc->port.ctl_pool_ref);
1189		if (io == NULL) {
1190			atio->ccb_h.flags &= ~CAM_DIR_MASK;
1191			atio->ccb_h.flags |= CAM_DIR_NONE;
1192
1193			printf("%s: ctl_alloc_io failed!\n", __func__);
1194
1195			/*
1196			 * XXX KDM need to set SCSI_STATUS_BUSY, but there
1197			 * is no field in the ATIO structure to do that,
1198			 * and we aren't able to allocate a ctl_io here.
1199			 * What to do?
1200			 */
1201			atio->sense_len = 0;
1202			done_ccb->ccb_h.io_ptr = NULL;
1203			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
1204					  periph_links.tqe);
1205			xpt_schedule(periph, /*priority*/ 1);
1206			break;
1207		}
1208		mtx_unlock(mtx);
1209		ctl_zero_io(io);
1210
1211		/* Save pointers on both sides */
1212		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1213		done_ccb->ccb_h.io_ptr = io;
1214
1215		/*
1216		 * Only SCSI I/O comes down this path, resets, etc. come
1217		 * down the immediate notify path below.
1218		 */
1219		io->io_hdr.io_type = CTL_IO_SCSI;
1220		io->io_hdr.nexus.initid.id = atio->init_id;
1221		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1222		io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1223		io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1224		io->scsiio.tag_num = atio->tag_id;
1225		switch (atio->tag_action) {
1226		case CAM_TAG_ACTION_NONE:
1227			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1228			break;
1229		case MSG_SIMPLE_TASK:
1230			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1231			break;
1232		case MSG_HEAD_OF_QUEUE_TASK:
1233        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1234			break;
1235		case MSG_ORDERED_TASK:
1236        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1237			break;
1238		case MSG_ACA_TASK:
1239			io->scsiio.tag_type = CTL_TAG_ACA;
1240			break;
1241		default:
1242			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1243			printf("%s: unhandled tag type %#x!!\n", __func__,
1244			       atio->tag_action);
1245			break;
1246		}
1247		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1248			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1249			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1250		}
1251		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1252		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1253		      io->scsiio.cdb_len);
1254
1255#ifdef CTLFEDEBUG
1256		printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1257		        (uintmax_t)io->io_hdr.nexus.initid.id,
1258		        io->io_hdr.nexus.targ_port,
1259		        (uintmax_t)io->io_hdr.nexus.targ_target.id,
1260		        io->io_hdr.nexus.targ_lun,
1261			io->scsiio.tag_num, io->scsiio.cdb[0]);
1262#endif
1263
1264		ctl_queue(io);
1265		return;
1266	}
1267	case XPT_CONT_TARGET_IO: {
1268		int srr = 0;
1269		uint32_t srr_off = 0;
1270
1271		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1272		io = (union ctl_io *)atio->ccb_h.io_ptr;
1273
1274		softc->ctios_returned++;
1275#ifdef CTLFEDEBUG
1276		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1277		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1278#endif
1279		/*
1280		 * Handle SRR case were the data pointer is pushed back hack
1281		 */
1282		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1283		    && done_ccb->csio.msg_ptr != NULL
1284		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1285		    && done_ccb->csio.msg_ptr[1] == 5
1286       		    && done_ccb->csio.msg_ptr[2] == 0) {
1287			srr = 1;
1288			srr_off =
1289			    (done_ccb->csio.msg_ptr[3] << 24)
1290			    | (done_ccb->csio.msg_ptr[4] << 16)
1291			    | (done_ccb->csio.msg_ptr[5] << 8)
1292			    | (done_ccb->csio.msg_ptr[6]);
1293		}
1294
1295		if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) {
1296			/*
1297			 * If status was being sent, the back end data is now
1298			 * history. Hack it up and resubmit a new command with
1299			 * the CDB adjusted. If the SIM does the right thing,
1300			 * all of the resid math should work.
1301			 */
1302			softc->ccbs_freed++;
1303			xpt_release_ccb(done_ccb);
1304			ctl_free_io(io);
1305			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1306				done_ccb = (union ccb *)atio;
1307				goto resubmit;
1308			}
1309			/*
1310			 * Fall through to doom....
1311			 */
1312		} else if (srr) {
1313			/*
1314			 * If we have an srr and we're still sending data, we
1315			 * should be able to adjust offsets and cycle again.
1316			 */
1317			io->scsiio.kern_rel_offset =
1318			    io->scsiio.ext_data_filled = srr_off;
1319			io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1320			    io->scsiio.kern_rel_offset;
1321			softc->ccbs_freed++;
1322			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1323			xpt_release_ccb(done_ccb);
1324			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1325					  periph_links.tqe);
1326			xpt_schedule(periph, /*priority*/ 1);
1327			break;
1328		}
1329
1330		/*
1331		 * If we were sending status back to the initiator, free up
1332		 * resources.  If we were doing a datamove, call the
1333		 * datamove done routine.
1334		 */
1335		if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
1336			softc->ccbs_freed++;
1337			xpt_release_ccb(done_ccb);
1338			ctl_free_io(io);
1339			/*
1340			 * For a wildcard attachment, commands can come in
1341			 * with a specific target/lun.  Reset the target
1342			 * and LUN fields back to the wildcard values before
1343			 * we send them back down to the SIM.  The SIM has
1344			 * a wildcard LUN enabled, not whatever target/lun
1345			 * these happened to be.
1346			 */
1347			if (softc->flags & CTLFE_LUN_WILDCARD) {
1348				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1349				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1350			}
1351			if (periph->flags & CAM_PERIPH_INVALID) {
1352				ctlfe_free_ccb(periph, (union ccb *)atio);
1353			} else {
1354				softc->atios_sent++;
1355				mtx_unlock(mtx);
1356				xpt_action((union ccb *)atio);
1357				return;
1358			}
1359		} else {
1360			struct ctlfe_lun_cmd_info *cmd_info;
1361			struct ccb_scsiio *csio;
1362
1363			csio = &done_ccb->csio;
1364			cmd_info = (struct ctlfe_lun_cmd_info *)
1365				io->io_hdr.port_priv;
1366
1367			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1368
1369			io->scsiio.ext_data_len += csio->dxfer_len;
1370			if (io->scsiio.ext_data_len >
1371			    io->scsiio.kern_total_len) {
1372				xpt_print(periph->path, "%s: tag 0x%04x "
1373					  "done len %u > total %u sent %u\n",
1374					  __func__, io->scsiio.tag_num,
1375					  io->scsiio.ext_data_len,
1376					  io->scsiio.kern_total_len,
1377					  io->scsiio.ext_data_filled);
1378			}
1379			/*
1380			 * Translate CAM status to CTL status.  Success
1381			 * does not change the overall, ctl_io status.  In
1382			 * that case we just set port_status to 0.  If we
1383			 * have a failure, though, set a data phase error
1384			 * for the overall ctl_io.
1385			 */
1386			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1387			case CAM_REQ_CMP:
1388				io->io_hdr.port_status = 0;
1389				break;
1390			default:
1391				/*
1392				 * XXX KDM we probably need to figure out a
1393				 * standard set of errors that the SIM
1394				 * drivers should return in the event of a
1395				 * data transfer failure.  A data phase
1396				 * error will at least point the user to a
1397				 * data transfer error of some sort.
1398				 * Hopefully the SIM printed out some
1399				 * additional information to give the user
1400				 * a clue what happened.
1401				 */
1402				io->io_hdr.port_status = 0xbad1;
1403				ctl_set_data_phase_error(&io->scsiio);
1404				/*
1405				 * XXX KDM figure out residual.
1406				 */
1407				break;
1408			}
1409			/*
1410			 * If we had to break this S/G list into multiple
1411			 * pieces, figure out where we are in the list, and
1412			 * continue sending pieces if necessary.
1413			 */
1414			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1415			 && (io->io_hdr.port_status == 0)) {
1416				ccb_flags flags;
1417				uint8_t scsi_status;
1418				uint8_t *data_ptr;
1419				uint32_t dxfer_len;
1420
1421				flags = atio->ccb_h.flags &
1422					(CAM_DIS_DISCONNECT|
1423					 CAM_TAG_ACTION_VALID);
1424
1425				ctlfedata(softc, io, &flags, &data_ptr,
1426				    &dxfer_len, &csio->sglist_cnt);
1427
1428				scsi_status = 0;
1429
1430				if (((flags & CAM_SEND_STATUS) == 0)
1431				 && (dxfer_len == 0)) {
1432					printf("%s: tag %04x no status or "
1433					       "len cdb = %02x\n", __func__,
1434					       atio->tag_id,
1435					atio->cdb_io.cdb_bytes[0]);
1436					printf("%s: tag %04x io status %#x\n",
1437					       __func__, atio->tag_id,
1438					       io->io_hdr.status);
1439				}
1440
1441				cam_fill_ctio(csio,
1442					      /*retries*/ 2,
1443					      ctlfedone,
1444					      flags,
1445					      (flags & CAM_TAG_ACTION_VALID) ?
1446					       MSG_SIMPLE_Q_TAG : 0,
1447					      atio->tag_id,
1448					      atio->init_id,
1449					      scsi_status,
1450					      /*data_ptr*/ data_ptr,
1451					      /*dxfer_len*/ dxfer_len,
1452					      /*timeout*/ 5 * 1000);
1453
1454				csio->ccb_h.flags |= CAM_UNLOCKED;
1455				csio->resid = 0;
1456				csio->ccb_h.ccb_atio = atio;
1457				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1458				softc->ctios_sent++;
1459				mtx_unlock(mtx);
1460				xpt_action((union ccb *)csio);
1461			} else {
1462				/*
1463				 * Release the CTIO.  The ATIO will be sent back
1464				 * down to the SIM once we send status.
1465				 */
1466				softc->ccbs_freed++;
1467				xpt_release_ccb(done_ccb);
1468				mtx_unlock(mtx);
1469
1470				/* Call the backend move done callback */
1471				io->scsiio.be_move_done(io);
1472			}
1473			return;
1474		}
1475		break;
1476	}
1477	case XPT_IMMEDIATE_NOTIFY: {
1478		union ctl_io *io;
1479		struct ccb_immediate_notify *inot;
1480		cam_status status;
1481		int frozen;
1482
1483		inot = &done_ccb->cin1;
1484
1485		softc->inots_returned++;
1486
1487		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1488
1489		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1490		       "seq %#x\n", __func__, inot->ccb_h.status,
1491		       inot->tag_id, inot->seq_id);
1492
1493		io = ctl_alloc_io(bus_softc->port.ctl_pool_ref);
1494		if (io != NULL) {
1495			int send_ctl_io;
1496
1497			send_ctl_io = 1;
1498
1499			ctl_zero_io(io);
1500			io->io_hdr.io_type = CTL_IO_TASK;
1501			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1502			inot->ccb_h.io_ptr = io;
1503			io->io_hdr.nexus.initid.id = inot->initiator_id;
1504			io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1505			io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1506			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1507			/* XXX KDM should this be the tag_id? */
1508			io->taskio.tag_num = inot->seq_id;
1509
1510			status = inot->ccb_h.status & CAM_STATUS_MASK;
1511			switch (status) {
1512			case CAM_SCSI_BUS_RESET:
1513				io->taskio.task_action = CTL_TASK_BUS_RESET;
1514				break;
1515			case CAM_BDR_SENT:
1516				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1517				break;
1518			case CAM_MESSAGE_RECV:
1519				switch (inot->arg) {
1520				case MSG_ABORT_TASK_SET:
1521					/*
1522					 * XXX KDM this isn't currently
1523					 * supported by CTL.  It ends up
1524					 * being a no-op.
1525					 */
1526					io->taskio.task_action =
1527						CTL_TASK_ABORT_TASK_SET;
1528					break;
1529				case MSG_TARGET_RESET:
1530					io->taskio.task_action =
1531						CTL_TASK_TARGET_RESET;
1532					break;
1533				case MSG_ABORT_TASK:
1534					io->taskio.task_action =
1535						CTL_TASK_ABORT_TASK;
1536					break;
1537				case MSG_LOGICAL_UNIT_RESET:
1538					io->taskio.task_action =
1539						CTL_TASK_LUN_RESET;
1540					break;
1541				case MSG_CLEAR_TASK_SET:
1542					/*
1543					 * XXX KDM this isn't currently
1544					 * supported by CTL.  It ends up
1545					 * being a no-op.
1546					 */
1547					io->taskio.task_action =
1548						CTL_TASK_CLEAR_TASK_SET;
1549					break;
1550				case MSG_CLEAR_ACA:
1551					io->taskio.task_action =
1552						CTL_TASK_CLEAR_ACA;
1553					break;
1554				case MSG_NOOP:
1555					send_ctl_io = 0;
1556					break;
1557				default:
1558					xpt_print(periph->path, "%s: "
1559						  "unsupported message 0x%x\n",
1560						  __func__, inot->arg);
1561					send_ctl_io = 0;
1562					break;
1563				}
1564				break;
1565			case CAM_REQ_ABORTED:
1566				/*
1567				 * This request was sent back by the driver.
1568				 * XXX KDM what do we do here?
1569				 */
1570				send_ctl_io = 0;
1571				break;
1572			case CAM_REQ_INVALID:
1573			case CAM_PROVIDE_FAIL:
1574			default:
1575				/*
1576				 * We should only get here if we're talking
1577				 * to a talking to a SIM that is target
1578				 * capable but supports the old API.  In
1579				 * that case, we need to just free the CCB.
1580				 * If we actually send a notify acknowledge,
1581				 * it will send that back with an error as
1582				 * well.
1583				 */
1584
1585				if ((status != CAM_REQ_INVALID)
1586				 && (status != CAM_PROVIDE_FAIL))
1587					xpt_print(periph->path, "%s: "
1588						  "unsupported CAM status "
1589						  "0x%x\n", __func__, status);
1590
1591				ctl_free_io(io);
1592				ctlfe_free_ccb(periph, done_ccb);
1593
1594				goto out;
1595			}
1596			if (send_ctl_io != 0) {
1597				ctl_queue(io);
1598			} else {
1599				ctl_free_io(io);
1600				done_ccb->ccb_h.status = CAM_REQ_INPROG;
1601				done_ccb->ccb_h.func_code =
1602					XPT_NOTIFY_ACKNOWLEDGE;
1603				xpt_action(done_ccb);
1604			}
1605		} else {
1606			xpt_print(periph->path, "%s: could not allocate "
1607				  "ctl_io for immediate notify!\n", __func__);
1608			/* requeue this to the adapter */
1609			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1610			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1611			xpt_action(done_ccb);
1612		}
1613
1614		if (frozen != 0) {
1615			cam_release_devq(periph->path,
1616					 /*relsim_flags*/ 0,
1617					 /*opening reduction*/ 0,
1618					 /*timeout*/ 0,
1619					 /*getcount_only*/ 0);
1620		}
1621		break;
1622	}
1623	case XPT_NOTIFY_ACKNOWLEDGE:
1624		/*
1625		 * Queue this back down to the SIM as an immediate notify.
1626		 */
1627		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1628		xpt_action(done_ccb);
1629		softc->inots_sent++;
1630		break;
1631	case XPT_SET_SIM_KNOB:
1632	case XPT_GET_SIM_KNOB:
1633		break;
1634	default:
1635		panic("%s: unexpected CCB type %#x", __func__,
1636		      done_ccb->ccb_h.func_code);
1637		break;
1638	}
1639
1640out:
1641	mtx_unlock(mtx);
1642}
1643
1644static void
1645ctlfe_onoffline(void *arg, int online)
1646{
1647	struct ctlfe_softc *bus_softc;
1648	union ccb *ccb;
1649	cam_status status;
1650	struct cam_path *path;
1651	int set_wwnn;
1652
1653	bus_softc = (struct ctlfe_softc *)arg;
1654
1655	set_wwnn = 0;
1656
1657	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1658		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1659	if (status != CAM_REQ_CMP) {
1660		printf("%s: unable to create path!\n", __func__);
1661		return;
1662	}
1663	ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO);
1664	if (ccb == NULL) {
1665		printf("%s: unable to malloc CCB!\n", __func__);
1666		xpt_free_path(path);
1667		return;
1668	}
1669	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1670
1671	/*
1672	 * Copan WWN format:
1673	 *
1674	 * Bits 63-60:	0x5		NAA, IEEE registered name
1675	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1676	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1677	 * Bits 11-8:			Type of port:
1678	 *					1 == N-Port
1679	 *					2 == F-Port
1680	 *					3 == NL-Port
1681	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1682	 */
1683
1684	if (online != 0) {
1685
1686		ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1687
1688
1689		xpt_action(ccb);
1690
1691
1692		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1693#ifdef RANDOM_WWNN
1694			uint64_t random_bits;
1695#endif
1696
1697			printf("%s: %s current WWNN %#jx\n", __func__,
1698			       bus_softc->port_name,
1699			       ccb->knob.xport_specific.fc.wwnn);
1700			printf("%s: %s current WWPN %#jx\n", __func__,
1701			       bus_softc->port_name,
1702			       ccb->knob.xport_specific.fc.wwpn);
1703
1704#ifdef RANDOM_WWNN
1705			arc4rand(&random_bits, sizeof(random_bits), 0);
1706#endif
1707
1708			/*
1709			 * XXX KDM this is a bit of a kludge for now.  We
1710			 * take the current WWNN/WWPN from the card, and
1711			 * replace the company identifier and the NL-Port
1712			 * indicator and the port number (for the WWPN).
1713			 * This should be replaced later with ddb_GetWWNN,
1714			 * or possibly a more centralized scheme.  (It
1715			 * would be nice to have the WWNN/WWPN for each
1716			 * port stored in the ctl_port structure.)
1717			 */
1718#ifdef RANDOM_WWNN
1719			ccb->knob.xport_specific.fc.wwnn =
1720				(random_bits &
1721				0x0000000fffffff00ULL) |
1722				/* Company ID */ 0x5000ED5000000000ULL |
1723				/* NL-Port */    0x0300;
1724			ccb->knob.xport_specific.fc.wwpn =
1725				(random_bits &
1726				0x0000000fffffff00ULL) |
1727				/* Company ID */ 0x5000ED5000000000ULL |
1728				/* NL-Port */    0x3000 |
1729				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1730
1731			/*
1732			 * This is a bit of an API break/reversal, but if
1733			 * we're doing the random WWNN that's a little
1734			 * different anyway.  So record what we're actually
1735			 * using with the frontend code so it's reported
1736			 * accurately.
1737			 */
1738			ctl_port_set_wwns(&bus_softc->port,
1739			    true, ccb->knob.xport_specific.fc.wwnn,
1740			    true, ccb->knob.xport_specific.fc.wwpn);
1741			set_wwnn = 1;
1742#else /* RANDOM_WWNN */
1743			/*
1744			 * If the user has specified a WWNN/WWPN, send them
1745			 * down to the SIM.  Otherwise, record what the SIM
1746			 * has reported.
1747			 */
1748			if ((bus_softc->port.wwnn != 0)
1749			 && (bus_softc->port.wwpn != 0)) {
1750				ccb->knob.xport_specific.fc.wwnn =
1751					bus_softc->port.wwnn;
1752				ccb->knob.xport_specific.fc.wwpn =
1753					bus_softc->port.wwpn;
1754				set_wwnn = 1;
1755			} else {
1756				ctl_port_set_wwns(&bus_softc->port,
1757				    true, ccb->knob.xport_specific.fc.wwnn,
1758				    true, ccb->knob.xport_specific.fc.wwpn);
1759			}
1760#endif /* RANDOM_WWNN */
1761
1762
1763			if (set_wwnn != 0) {
1764				printf("%s: %s new WWNN %#jx\n", __func__,
1765				       bus_softc->port_name,
1766				ccb->knob.xport_specific.fc.wwnn);
1767				printf("%s: %s new WWPN %#jx\n", __func__,
1768				       bus_softc->port_name,
1769				       ccb->knob.xport_specific.fc.wwpn);
1770			}
1771		} else {
1772			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1773			       bus_softc->port_name);
1774		}
1775	}
1776	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1777	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1778	if (set_wwnn != 0)
1779		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1780
1781	if (online != 0)
1782		ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1783	else
1784		ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1785
1786	xpt_action(ccb);
1787
1788	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1789		printf("%s: SIM %s (path id %d) target %s failed with "
1790		       "status %#x\n",
1791		       __func__, bus_softc->port_name, bus_softc->path_id,
1792		       (online != 0) ? "enable" : "disable",
1793		       ccb->ccb_h.status);
1794	} else {
1795		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1796		       __func__, bus_softc->port_name, bus_softc->path_id,
1797		       (online != 0) ? "enable" : "disable");
1798	}
1799
1800	xpt_free_path(path);
1801
1802	free(ccb, M_TEMP);
1803
1804	return;
1805}
1806
1807static void
1808ctlfe_online(void *arg)
1809{
1810	struct ctlfe_softc *bus_softc;
1811	struct cam_path *path;
1812	cam_status status;
1813	struct ctlfe_lun_softc *lun_softc;
1814
1815	bus_softc = (struct ctlfe_softc *)arg;
1816
1817	/*
1818	 * Create the wildcard LUN before bringing the port online.
1819	 */
1820	status = xpt_create_path(&path, /*periph*/ NULL,
1821				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1822				 CAM_LUN_WILDCARD);
1823	if (status != CAM_REQ_CMP) {
1824		printf("%s: unable to create path for wildcard periph\n",
1825				__func__);
1826		return;
1827	}
1828
1829	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
1830			M_NOWAIT | M_ZERO);
1831	if (lun_softc == NULL) {
1832		xpt_print(path, "%s: unable to allocate softc for "
1833				"wildcard periph\n", __func__);
1834		xpt_free_path(path);
1835		return;
1836	}
1837
1838	xpt_path_lock(path);
1839	lun_softc->parent_softc = bus_softc;
1840	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1841
1842	mtx_lock(&bus_softc->lun_softc_mtx);
1843	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links);
1844	mtx_unlock(&bus_softc->lun_softc_mtx);
1845
1846	status = cam_periph_alloc(ctlferegister,
1847				  ctlfeoninvalidate,
1848				  ctlfecleanup,
1849				  ctlfestart,
1850				  "ctl",
1851				  CAM_PERIPH_BIO,
1852				  path,
1853				  ctlfeasync,
1854				  0,
1855				  lun_softc);
1856
1857	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1858		const struct cam_status_entry *entry;
1859
1860		entry = cam_fetch_status_entry(status);
1861
1862		printf("%s: CAM error %s (%#x) returned from "
1863		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1864		       entry->status_text : "Unknown", status);
1865	}
1866
1867	ctlfe_onoffline(arg, /*online*/ 1);
1868
1869	xpt_path_unlock(path);
1870	xpt_free_path(path);
1871}
1872
1873static void
1874ctlfe_offline(void *arg)
1875{
1876	struct ctlfe_softc *bus_softc;
1877	struct cam_path *path;
1878	cam_status status;
1879	struct cam_periph *periph;
1880
1881	bus_softc = (struct ctlfe_softc *)arg;
1882
1883	/*
1884	 * Disable the wildcard LUN for this port now that we have taken
1885	 * the port offline.
1886	 */
1887	status = xpt_create_path(&path, /*periph*/ NULL,
1888				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1889				 CAM_LUN_WILDCARD);
1890	if (status != CAM_REQ_CMP) {
1891		printf("%s: unable to create path for wildcard periph\n",
1892		       __func__);
1893		return;
1894	}
1895
1896	xpt_path_lock(path);
1897
1898	ctlfe_onoffline(arg, /*online*/ 0);
1899
1900	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1901		cam_periph_invalidate(periph);
1902
1903	xpt_path_unlock(path);
1904	xpt_free_path(path);
1905}
1906
1907/*
1908 * This will get called to enable a LUN on every bus that is attached to
1909 * CTL.  So we only need to create a path/periph for this particular bus.
1910 */
1911static int
1912ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1913{
1914	struct ctlfe_softc *bus_softc;
1915	struct ctlfe_lun_softc *softc;
1916	struct cam_path *path;
1917	struct cam_periph *periph;
1918	cam_status status;
1919
1920	bus_softc = (struct ctlfe_softc *)arg;
1921
1922	status = xpt_create_path(&path, /*periph*/ NULL,
1923				  bus_softc->path_id,
1924				  targ_id.id, lun_id);
1925	/* XXX KDM need some way to return status to CTL here? */
1926	if (status != CAM_REQ_CMP) {
1927		printf("%s: could not create path, status %#x\n", __func__,
1928		       status);
1929		return (1);
1930	}
1931
1932	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1933	xpt_path_lock(path);
1934	periph = cam_periph_find(path, "ctl");
1935	if (periph != NULL) {
1936		/* We've already got a periph, no need to alloc a new one. */
1937		xpt_path_unlock(path);
1938		xpt_free_path(path);
1939		free(softc, M_CTLFE);
1940		return (0);
1941	}
1942
1943	softc->parent_softc = bus_softc;
1944	mtx_lock(&bus_softc->lun_softc_mtx);
1945	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
1946	mtx_unlock(&bus_softc->lun_softc_mtx);
1947
1948	status = cam_periph_alloc(ctlferegister,
1949				  ctlfeoninvalidate,
1950				  ctlfecleanup,
1951				  ctlfestart,
1952				  "ctl",
1953				  CAM_PERIPH_BIO,
1954				  path,
1955				  ctlfeasync,
1956				  0,
1957				  softc);
1958
1959	xpt_path_unlock(path);
1960	xpt_free_path(path);
1961	return (0);
1962}
1963
1964/*
1965 * This will get called when the user removes a LUN to disable that LUN
1966 * on every bus that is attached to CTL.
1967 */
1968static int
1969ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1970{
1971	struct ctlfe_softc *softc;
1972	struct ctlfe_lun_softc *lun_softc;
1973
1974	softc = (struct ctlfe_softc *)arg;
1975
1976	mtx_lock(&softc->lun_softc_mtx);
1977	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1978		struct cam_path *path;
1979
1980		path = lun_softc->periph->path;
1981
1982		if ((xpt_path_target_id(path) == targ_id.id)
1983		 && (xpt_path_lun_id(path) == lun_id)) {
1984			break;
1985		}
1986	}
1987	if (lun_softc == NULL) {
1988		mtx_unlock(&softc->lun_softc_mtx);
1989		printf("%s: can't find target %d lun %d\n", __func__,
1990		       targ_id.id, lun_id);
1991		return (1);
1992	}
1993	cam_periph_acquire(lun_softc->periph);
1994	mtx_unlock(&softc->lun_softc_mtx);
1995
1996	cam_periph_lock(lun_softc->periph);
1997	cam_periph_invalidate(lun_softc->periph);
1998	cam_periph_unlock(lun_softc->periph);
1999	cam_periph_release(lun_softc->periph);
2000	return (0);
2001}
2002
2003static void
2004ctlfe_dump_sim(struct cam_sim *sim)
2005{
2006
2007	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
2008	       sim->sim_name, sim->unit_number,
2009	       sim->max_tagged_dev_openings, sim->max_dev_openings);
2010	printf("\n");
2011}
2012
2013/*
2014 * Assumes that the SIM lock is held.
2015 */
2016static void
2017ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
2018{
2019	struct ccb_hdr *hdr;
2020	struct cam_periph *periph;
2021	int num_items;
2022
2023	periph = softc->periph;
2024	num_items = 0;
2025
2026	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
2027		union ctl_io *io;
2028
2029		io = hdr->io_ptr;
2030
2031		num_items++;
2032
2033		/*
2034		 * This can happen when we get an ATIO but can't allocate
2035		 * a ctl_io.  See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
2036		 */
2037		if (io == NULL) {
2038			struct ccb_scsiio *csio;
2039
2040			csio = (struct ccb_scsiio *)hdr;
2041
2042			xpt_print(periph->path, "CCB %#x ctl_io allocation "
2043				  "failed\n", csio->tag_id);
2044			continue;
2045		}
2046
2047		/*
2048		 * Only regular SCSI I/O is put on the work
2049		 * queue, so we can print sense here.  There may be no
2050		 * sense if it's no the queue for a DMA, but this serves to
2051		 * print out the CCB as well.
2052		 *
2053		 * XXX KDM switch this over to scsi_sense_print() when
2054		 * CTL is merged in with CAM.
2055		 */
2056		ctl_io_error_print(io, NULL);
2057
2058		/*
2059		 * We're sending status back to the
2060		 * initiator, so we're on the queue waiting
2061		 * for a CTIO to do that.
2062		 */
2063		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2064			continue;
2065
2066		/*
2067		 * Otherwise, we're on the queue waiting to
2068		 * do a data transfer.
2069		 */
2070		xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
2071			  io->scsiio.kern_total_len, io->scsiio.kern_data_len,
2072			  io->scsiio.kern_data_resid);
2073	}
2074
2075	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
2076		  num_items);
2077	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
2078		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
2079		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
2080		  (uintmax_t)softc->ccbs_freed);
2081	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
2082		  "returned\n", (uintmax_t)(softc->ctios_sent -
2083		  softc->ctios_returned), softc->ctios_sent,
2084		  softc->ctios_returned);
2085}
2086
2087/*
2088 * This function is called when we fail to get a CCB for a DMA or status return
2089 * to the initiator within the specified time period.
2090 *
2091 * The callout code should insure that we hold the sim mutex here.
2092 */
2093static void
2094ctlfe_dma_timeout(void *arg)
2095{
2096	struct ctlfe_lun_softc *softc;
2097	struct cam_periph *periph;
2098	struct cam_sim *sim;
2099	int num_queued;
2100
2101	softc = (struct ctlfe_lun_softc *)arg;
2102	periph = softc->periph;
2103	sim = xpt_path_sim(periph->path);
2104	num_queued = 0;
2105
2106	/*
2107	 * Nothing to do...
2108	 */
2109	if (TAILQ_FIRST(&softc->work_queue) == NULL) {
2110		xpt_print(periph->path, "TIMEOUT triggered after %d "
2111			  "seconds, but nothing on work queue??\n",
2112			  CTLFE_DMA_TIMEOUT);
2113		return;
2114	}
2115
2116	xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
2117		  "start\n", CTLFE_DMA_TIMEOUT);
2118
2119	ctlfe_dump_queue(softc);
2120
2121	ctlfe_dump_sim(sim);
2122
2123	xpt_print(periph->path, "calling xpt_schedule() to attempt to "
2124		  "unstick our queue\n");
2125
2126	xpt_schedule(periph, /*priority*/ 1);
2127
2128	xpt_print(periph->path, "xpt_schedule() call complete\n");
2129}
2130
2131/*
2132 * Datamove/done routine called by CTL.  Put ourselves on the queue to
2133 * receive a CCB from CAM so we can queue the continue I/O request down
2134 * to the adapter.
2135 */
2136static void
2137ctlfe_datamove_done(union ctl_io *io)
2138{
2139	union ccb *ccb;
2140	struct cam_periph *periph;
2141	struct ctlfe_lun_softc *softc;
2142
2143	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2144
2145	periph = xpt_path_periph(ccb->ccb_h.path);
2146	cam_periph_lock(periph);
2147
2148	softc = (struct ctlfe_lun_softc *)periph->softc;
2149
2150	if (io->io_hdr.io_type == CTL_IO_TASK) {
2151		/*
2152		 * Task management commands don't require any further
2153		 * communication back to the adapter.  Requeue the CCB
2154		 * to the adapter, and free the CTL I/O.
2155		 */
2156		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2157			  "tag %#x seq %#x\n", __func__,
2158			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2159		/*
2160		 * Send the notify acknowledge down to the SIM, to let it
2161		 * know we processed the task management command.
2162		 */
2163		ccb->ccb_h.status = CAM_REQ_INPROG;
2164		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2165		xpt_action(ccb);
2166		ctl_free_io(io);
2167	} else {
2168		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2169			io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2170		else
2171			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2172
2173		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2174				  periph_links.tqe);
2175
2176		/*
2177		 * Reset the timeout for our latest active DMA.
2178		 */
2179		callout_reset(&softc->dma_callout,
2180			      CTLFE_DMA_TIMEOUT * hz,
2181			      ctlfe_dma_timeout, softc);
2182		/*
2183		 * Ask for the CAM transport layer to send us a CCB to do
2184		 * the DMA or send status, unless ctlfe_dma_enabled is set
2185		 * to 0.
2186		 */
2187		if (ctlfe_dma_enabled != 0)
2188			xpt_schedule(periph, /*priority*/ 1);
2189	}
2190
2191	cam_periph_unlock(periph);
2192}
2193
2194static void
2195ctlfe_dump(void)
2196{
2197	struct ctlfe_softc *bus_softc;
2198
2199	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2200		struct ctlfe_lun_softc *lun_softc;
2201
2202		ctlfe_dump_sim(bus_softc->sim);
2203
2204		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
2205			ctlfe_dump_queue(lun_softc);
2206		}
2207	}
2208}
2209