scsi_ctl.c revision 274388
1229997Sken/*-
2229997Sken * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3229997Sken * All rights reserved.
4229997Sken *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7229997Sken * are met:
8229997Sken * 1. Redistributions of source code must retain the above copyright
9229997Sken *    notice, this list of conditions, and the following disclaimer,
10229997Sken *    without modification.
11229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
13229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
14229997Sken *    including a substantially similar Disclaimer requirement for further
15229997Sken *    binary redistribution.
16229997Sken *
17229997Sken * NO WARRANTY
18229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28229997Sken * POSSIBILITY OF SUCH DAMAGES.
29229997Sken *
30229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
31229997Sken */
32229997Sken/*
33229997Sken * Peripheral driver interface between CAM and CTL (CAM Target Layer).
34229997Sken *
35229997Sken * Author: Ken Merry <ken@FreeBSD.org>
36229997Sken */
37229997Sken
38229997Sken#include <sys/cdefs.h>
39229997Sken__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/scsi_ctl.c 274388 2014-11-11 17:17:41Z mav $");
40229997Sken
41229997Sken#include <sys/param.h>
42229997Sken#include <sys/queue.h>
43229997Sken#include <sys/systm.h>
44229997Sken#include <sys/kernel.h>
45229997Sken#include <sys/lock.h>
46229997Sken#include <sys/mutex.h>
47229997Sken#include <sys/condvar.h>
48229997Sken#include <sys/malloc.h>
49229997Sken#include <sys/bus.h>
50229997Sken#include <sys/endian.h>
51229997Sken#include <sys/sbuf.h>
52229997Sken#include <sys/sysctl.h>
53229997Sken#include <sys/types.h>
54229997Sken#include <sys/systm.h>
55229997Sken#include <machine/bus.h>
56229997Sken
57229997Sken#include <cam/cam.h>
58229997Sken#include <cam/cam_ccb.h>
59229997Sken#include <cam/cam_periph.h>
60229997Sken#include <cam/cam_queue.h>
61229997Sken#include <cam/cam_xpt_periph.h>
62229997Sken#include <cam/cam_debug.h>
63229997Sken#include <cam/cam_sim.h>
64229997Sken#include <cam/cam_xpt.h>
65229997Sken
66229997Sken#include <cam/scsi/scsi_all.h>
67229997Sken#include <cam/scsi/scsi_message.h>
68229997Sken
69229997Sken#include <cam/ctl/ctl_io.h>
70229997Sken#include <cam/ctl/ctl.h>
71229997Sken#include <cam/ctl/ctl_frontend.h>
72229997Sken#include <cam/ctl/ctl_util.h>
73229997Sken#include <cam/ctl/ctl_error.h>
74229997Sken
75229997Skentypedef enum {
76260387Sscottl	CTLFE_CCB_DEFAULT	= 0x00
77229997Sken} ctlfe_ccb_types;
78229997Sken
79229997Skenstruct ctlfe_softc {
80268677Smav	struct ctl_port port;
81229997Sken	path_id_t path_id;
82265641Smav	u_int	maxio;
83229997Sken	struct cam_sim *sim;
84229997Sken	char port_name[DEV_IDLEN];
85260387Sscottl	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";
93229997Skenstatic int ctlfe_dma_enabled = 1;
94229997Sken#ifdef CTLFE_INIT_ENABLE
95229997Skenstatic int ctlfe_max_targets = 1;
96229997Skenstatic int ctlfe_num_targets = 0;
97229997Sken#endif
98229997Sken
99229997Skentypedef enum {
100229997Sken	CTLFE_LUN_NONE		= 0x00,
101229997Sken	CTLFE_LUN_WILDCARD	= 0x01
102229997Sken} ctlfe_lun_flags;
103229997Sken
104229997Skenstruct ctlfe_lun_softc {
105229997Sken	struct ctlfe_softc *parent_softc;
106229997Sken	struct cam_periph *periph;
107229997Sken	ctlfe_lun_flags flags;
108229997Sken	struct callout dma_callout;
109229997Sken	uint64_t ccbs_alloced;
110229997Sken	uint64_t ccbs_freed;
111229997Sken	uint64_t ctios_sent;
112229997Sken	uint64_t ctios_returned;
113229997Sken	uint64_t atios_sent;
114229997Sken	uint64_t atios_returned;
115229997Sken	uint64_t inots_sent;
116229997Sken	uint64_t inots_returned;
117229997Sken	/* bus_dma_tag_t dma_tag; */
118229997Sken	TAILQ_HEAD(, ccb_hdr) work_queue;
119229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
120229997Sken};
121229997Sken
122229997Skentypedef enum {
123229997Sken	CTLFE_CMD_NONE		= 0x00,
124229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
125229997Sken} ctlfe_cmd_flags;
126229997Sken
127229997Sken/*
128229997Sken * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
129229997Sken * Currently that is 600 bytes.
130229997Sken */
131229997Skenstruct ctlfe_lun_cmd_info {
132229997Sken	int cur_transfer_index;
133265641Smav	size_t cur_transfer_off;
134229997Sken	ctlfe_cmd_flags flags;
135229997Sken	/*
136229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
137229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
138229997Sken	 * i386 and 512 bytes on amd64.
139229997Sken	 */
140265641Smav#define CTLFE_MAX_SEGS	32
141265641Smav	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
142229997Sken};
143229997Sken
144229997Sken/*
145229997Sken * When we register the adapter/bus, request that this many ctl_ios be
146229997Sken * allocated.  This should be the maximum supported by the adapter, but we
147229997Sken * currently don't have a way to get that back from the path inquiry.
148229997Sken * XXX KDM add that to the path inquiry.
149229997Sken */
150229997Sken#define	CTLFE_REQ_CTL_IO	4096
151229997Sken/*
152229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
153229997Sken * adapter per LUN.
154229997Sken * XXX KDM should this be controlled by CTL?
155229997Sken */
156229997Sken#define	CTLFE_ATIO_PER_LUN	1024
157229997Sken/*
158229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
159229997Sken * allocate and queue down to the adapter per LUN.
160229997Sken * XXX KDM should this be controlled by CTL?
161229997Sken */
162229997Sken#define	CTLFE_IN_PER_LUN	1024
163229997Sken
164229997Sken/*
165229997Sken * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
166229997Sken * status to the initiator.  The SIM is expected to have its own timeouts,
167229997Sken * so we're not putting this timeout around the CCB execution time.  The
168229997Sken * SIM should timeout and let us know if it has an issue.
169229997Sken */
170229997Sken#define	CTLFE_DMA_TIMEOUT	60
171229997Sken
172229997Sken/*
173229997Sken * Turn this on to enable extra debugging prints.
174229997Sken */
175229997Sken#if 0
176229997Sken#define	CTLFE_DEBUG
177229997Sken#endif
178229997Sken
179229997Sken/*
180229997Sken * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
181229997Sken * in the FreeBSD initiator that makes it unable to rescan the target if
182229997Sken * the target gets rebooted and the WWNN/WWPN stay the same.
183229997Sken */
184229997Sken#if 0
185229997Sken#define	RANDOM_WWNN
186229997Sken#endif
187229997Sken
188229997SkenSYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
189229997Sken	   &ctlfe_dma_enabled, 0, "DMA enabled");
190229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
191229997Sken
192229997Sken#define	ccb_type	ppriv_field0
193229997Sken/* This is only used in the ATIO */
194229997Sken#define	io_ptr		ppriv_ptr1
195229997Sken
196229997Sken/* This is only used in the CTIO */
197229997Sken#define	ccb_atio	ppriv_ptr1
198229997Sken
199229997Skenint			ctlfeinitialize(void);
200229997Skenvoid			ctlfeshutdown(void);
201268677Smavstatic periph_init_t	ctlfeperiphinit;
202229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
203229997Sken				   struct cam_path *path, void *arg);
204229997Skenstatic periph_ctor_t	ctlferegister;
205229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
206229997Skenstatic periph_dtor_t	ctlfecleanup;
207229997Skenstatic periph_start_t	ctlfestart;
208229997Skenstatic void		ctlfedone(struct cam_periph *periph,
209229997Sken				  union ccb *done_ccb);
210229997Sken
211229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
212229997Skenstatic void 		ctlfe_online(void *arg);
213229997Skenstatic void 		ctlfe_offline(void *arg);
214229997Skenstatic int 		ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
215229997Sken					 int lun_id);
216229997Skenstatic int 		ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
217229997Sken					  int lun_id);
218229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
219229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
220229997Skenstatic void		ctlfe_dma_timeout(void *arg);
221229997Skenstatic void 		ctlfe_datamove_done(union ctl_io *io);
222229997Skenstatic void 		ctlfe_dump(void);
223229997Sken
224229997Skenstatic struct periph_driver ctlfe_driver =
225229997Sken{
226268677Smav	ctlfeperiphinit, "ctl",
227273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
228273316Smav	CAM_PERIPH_DRV_EARLY
229229997Sken};
230229997Sken
231268677Smavstatic struct ctl_frontend ctlfe_frontend =
232268677Smav{
233273318Smav	.name = "camtgt",
234268677Smav	.init = ctlfeinitialize,
235268677Smav	.fe_dump = ctlfe_dump,
236268677Smav	.shutdown = ctlfeshutdown,
237249009Strasz};
238268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
239249009Strasz
240229997Skenextern struct ctl_softc *control_softc;
241229997Sken
242229997Skenvoid
243229997Skenctlfeshutdown(void)
244229997Sken{
245229997Sken	return;
246229997Sken}
247229997Sken
248268677Smavint
249268677Smavctlfeinitialize(void)
250229997Sken{
251229997Sken
252229997Sken	STAILQ_INIT(&ctlfe_softc_list);
253229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
254268677Smav	periphdriver_register(&ctlfe_driver);
255268677Smav	return (0);
256268677Smav}
257229997Sken
258268677Smavvoid
259268677Smavctlfeperiphinit(void)
260268677Smav{
261268677Smav	cam_status status;
262229997Sken
263229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
264229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
265229997Sken	if (status != CAM_REQ_CMP) {
266229997Sken		printf("ctl: Failed to attach async callback due to CAM "
267229997Sken		       "status 0x%x!\n", status);
268229997Sken	}
269229997Sken}
270229997Sken
271229997Skenstatic void
272229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
273229997Sken{
274273317Smav	struct ctlfe_softc *softc;
275229997Sken
276229997Sken#ifdef CTLFEDEBUG
277229997Sken	printf("%s: entered\n", __func__);
278229997Sken#endif
279229997Sken
280273317Smav	mtx_lock(&ctlfe_list_mtx);
281273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
282273317Smav		if (softc->path_id == xpt_path_path_id(path))
283273317Smav			break;
284273317Smav	}
285273317Smav	mtx_unlock(&ctlfe_list_mtx);
286273317Smav
287229997Sken	/*
288229997Sken	 * When a new path gets registered, and it is capable of target
289229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
290229997Sken	 * selective, but for now this will be sufficient.
291229997Sken 	 */
292229997Sken	switch (code) {
293229997Sken	case AC_PATH_REGISTERED: {
294268677Smav		struct ctl_port *port;
295229997Sken		struct ccb_pathinq *cpi;
296229997Sken		int retval;
297229997Sken
298229997Sken		cpi = (struct ccb_pathinq *)arg;
299229997Sken
300229997Sken		/* Don't attach if it doesn't support target mode */
301229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
302230033Sken#ifdef CTLFEDEBUG
303229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
304229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
305230033Sken#endif
306229997Sken			break;
307229997Sken		}
308229997Sken
309273317Smav		if (softc != NULL) {
310273317Smav#ifdef CTLFEDEBUG
311273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
312273317Smav			       __func__, xpt_path_path_id(path));
313273317Smav#endif
314273317Smav			break;
315273317Smav		}
316273317Smav
317229997Sken#ifdef CTLFE_INIT_ENABLE
318229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
319229997Sken			union ccb *ccb;
320229997Sken
321229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
322229997Sken						  M_NOWAIT | M_ZERO);
323229997Sken			if (ccb == NULL) {
324229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
325229997Sken				return;
326229997Sken			}
327260387Sscottl			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
328229997Sken
329229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
330229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
331229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
332229997Sken
333229997Sken			xpt_action(ccb);
334229997Sken
335229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
336229997Sken			     CAM_REQ_CMP) {
337229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
338229997Sken				       "enable failed with status %#x\n",
339229997Sken				       __func__, cpi->dev_name,
340229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
341229997Sken				       ccb->ccb_h.status);
342229997Sken			} else {
343229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
344229997Sken				       "enable succeeded\n",
345229997Sken				       __func__, cpi->dev_name,
346229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
347229997Sken			}
348229997Sken
349229997Sken			free(ccb, M_TEMP);
350229997Sken
351229997Sken			break;
352229997Sken		} else {
353229997Sken			ctlfe_num_targets++;
354229997Sken		}
355229997Sken
356229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
357229997Sken		       ctlfe_num_targets);
358229997Sken#endif /* CTLFE_INIT_ENABLE */
359229997Sken
360229997Sken		/*
361229997Sken		 * We're in an interrupt context here, so we have to
362229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
363229997Sken		 * can't allocate memory.
364229997Sken		 */
365273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
366273317Smav		if (softc == NULL) {
367229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
368273317Smav			       __func__, sizeof(*softc));
369229997Sken			return;
370229997Sken		}
371229997Sken
372273317Smav		softc->path_id = cpi->ccb_h.path_id;
373273317Smav		softc->sim = xpt_path_sim(path);
374265641Smav		if (cpi->maxio != 0)
375273317Smav			softc->maxio = cpi->maxio;
376265641Smav		else
377273317Smav			softc->maxio = DFLTPHYS;
378273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
379273317Smav		STAILQ_INIT(&softc->lun_softc_list);
380229997Sken
381273317Smav		port = &softc->port;
382268677Smav		port->frontend = &ctlfe_frontend;
383229997Sken
384229997Sken		/*
385229997Sken		 * XXX KDM should we be more accurate here ?
386229997Sken		 */
387229997Sken		if (cpi->transport == XPORT_FC)
388268677Smav			port->port_type = CTL_PORT_FC;
389268694Smav		else if (cpi->transport == XPORT_SAS)
390268694Smav			port->port_type = CTL_PORT_SAS;
391229997Sken		else
392268677Smav			port->port_type = CTL_PORT_SCSI;
393229997Sken
394229997Sken		/* XXX KDM what should the real number be here? */
395268677Smav		port->num_requested_ctl_io = 4096;
396273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
397229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
398229997Sken		/*
399229997Sken		 * XXX KDM it would be nice to allocate storage in the
400229997Sken		 * frontend structure itself.
401229997Sken	 	 */
402273317Smav		port->port_name = softc->port_name;
403273319Smav		port->physical_port = cpi->bus_id;
404273319Smav		port->virtual_port = 0;
405268677Smav		port->port_online = ctlfe_online;
406268677Smav		port->port_offline = ctlfe_offline;
407273317Smav		port->onoff_arg = softc;
408268677Smav		port->lun_enable = ctlfe_lun_enable;
409268677Smav		port->lun_disable = ctlfe_lun_disable;
410273317Smav		port->targ_lun_arg = softc;
411268677Smav		port->fe_datamove = ctlfe_datamove_done;
412268677Smav		port->fe_done = ctlfe_datamove_done;
413229997Sken		/*
414229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
415229997Sken		 * number of targets supported.
416229997Sken		 */
417268677Smav		port->max_targets = cpi->max_target;
418268677Smav		port->max_target_id = cpi->max_target;
419229997Sken
420229997Sken		/*
421229997Sken		 * XXX KDM need to figure out whether we're the master or
422229997Sken		 * slave.
423229997Sken		 */
424230033Sken#ifdef CTLFEDEBUG
425268677Smav		printf("%s: calling ctl_port_register() for %s%d\n",
426229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
427230033Sken#endif
428268677Smav		retval = ctl_port_register(port, /*master_SC*/ 1);
429229997Sken		if (retval != 0) {
430268677Smav			printf("%s: ctl_port_register() failed with "
431229997Sken			       "error %d!\n", __func__, retval);
432273317Smav			mtx_destroy(&softc->lun_softc_mtx);
433273317Smav			free(softc, M_CTLFE);
434229997Sken			break;
435229997Sken		} else {
436229997Sken			mtx_lock(&ctlfe_list_mtx);
437273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
438229997Sken			mtx_unlock(&ctlfe_list_mtx);
439229997Sken		}
440229997Sken
441245228Sken		break;
442245228Sken	}
443245228Sken	case AC_PATH_DEREGISTERED: {
444245228Sken
445245228Sken		if (softc != NULL) {
446245228Sken			/*
447245228Sken			 * XXX KDM are we certain at this point that there
448245228Sken			 * are no outstanding commands for this frontend?
449245228Sken			 */
450273317Smav			mtx_lock(&ctlfe_list_mtx);
451273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
452273317Smav			    links);
453273317Smav			mtx_unlock(&ctlfe_list_mtx);
454268677Smav			ctl_port_deregister(&softc->port);
455260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
456245228Sken			free(softc, M_CTLFE);
457229997Sken		}
458229997Sken		break;
459229997Sken	}
460229997Sken	case AC_CONTRACT: {
461229997Sken		struct ac_contract *ac;
462229997Sken
463229997Sken		ac = (struct ac_contract *)arg;
464229997Sken
465229997Sken		switch (ac->contract_number) {
466229997Sken		case AC_CONTRACT_DEV_CHG: {
467229997Sken			struct ac_device_changed *dev_chg;
468273317Smav			int retval;
469229997Sken
470229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
471229997Sken
472236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
473229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
474229997Sken			       xpt_path_path_id(path), dev_chg->target,
475229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
476229997Sken
477273317Smav			if (softc == NULL) {
478229997Sken				printf("%s: CTL port for CAM path %u not "
479229997Sken				       "found!\n", __func__,
480229997Sken				       xpt_path_path_id(path));
481229997Sken				break;
482229997Sken			}
483229997Sken			if (dev_chg->arrived != 0) {
484268692Smav				retval = ctl_add_initiator(&softc->port,
485268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
486229997Sken			} else {
487268692Smav				retval = ctl_remove_initiator(&softc->port,
488268692Smav				    dev_chg->target);
489229997Sken			}
490229997Sken
491268692Smav			if (retval < 0) {
492229997Sken				printf("%s: could not %s port %d iid %u "
493229997Sken				       "WWPN %#jx!\n", __func__,
494229997Sken				       (dev_chg->arrived != 0) ? "add" :
495268677Smav				       "remove", softc->port.targ_port,
496229997Sken				       dev_chg->target,
497229997Sken				       (uintmax_t)dev_chg->wwpn);
498229997Sken			}
499229997Sken			break;
500229997Sken		}
501229997Sken		default:
502229997Sken			printf("%s: unsupported contract number %ju\n",
503229997Sken			       __func__, (uintmax_t)ac->contract_number);
504229997Sken			break;
505229997Sken		}
506229997Sken		break;
507229997Sken	}
508229997Sken	default:
509229997Sken		break;
510229997Sken	}
511229997Sken}
512229997Sken
513229997Skenstatic cam_status
514229997Skenctlferegister(struct cam_periph *periph, void *arg)
515229997Sken{
516229997Sken	struct ctlfe_softc *bus_softc;
517229997Sken	struct ctlfe_lun_softc *softc;
518229997Sken	union ccb en_lun_ccb;
519229997Sken	cam_status status;
520229997Sken	int i;
521229997Sken
522229997Sken	softc = (struct ctlfe_lun_softc *)arg;
523229997Sken	bus_softc = softc->parent_softc;
524229997Sken
525229997Sken	TAILQ_INIT(&softc->work_queue);
526229997Sken	softc->periph = periph;
527229997Sken
528260387Sscottl	callout_init_mtx(&softc->dma_callout, xpt_path_mtx(periph->path),
529260387Sscottl	    /*flags*/ 0);
530229997Sken	periph->softc = softc;
531229997Sken
532242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
533229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
534229997Sken	en_lun_ccb.cel.grp6_len = 0;
535229997Sken	en_lun_ccb.cel.grp7_len = 0;
536229997Sken	en_lun_ccb.cel.enable = 1;
537229997Sken	xpt_action(&en_lun_ccb);
538229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
539229997Sken	if (status != CAM_REQ_CMP) {
540229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
541229997Sken			  __func__, en_lun_ccb.ccb_h.status);
542229997Sken		return (status);
543229997Sken	}
544229997Sken
545229997Sken	status = CAM_REQ_CMP;
546229997Sken
547229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
548229997Sken		union ccb *new_ccb;
549229997Sken
550229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
551236426Smjacob					      M_ZERO|M_NOWAIT);
552229997Sken		if (new_ccb == NULL) {
553229997Sken			status = CAM_RESRC_UNAVAIL;
554229997Sken			break;
555229997Sken		}
556229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
557229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
558229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
559260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
560229997Sken		xpt_action(new_ccb);
561229997Sken		softc->atios_sent++;
562229997Sken		status = new_ccb->ccb_h.status;
563229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
564229997Sken			free(new_ccb, M_CTLFE);
565229997Sken			break;
566229997Sken		}
567229997Sken	}
568229997Sken
569229997Sken	status = cam_periph_acquire(periph);
570229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
571229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
572229997Sken			  "count, status = %#x\n", __func__, status);
573229997Sken		return (status);
574229997Sken	}
575229997Sken
576229997Sken	if (i == 0) {
577229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
578229997Sken			  "status 0x%x\n", __func__, status);
579229997Sken		return (CAM_REQ_CMP_ERR);
580229997Sken	}
581229997Sken
582229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
583229997Sken		union ccb *new_ccb;
584229997Sken
585229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
586236426Smjacob					      M_ZERO|M_NOWAIT);
587229997Sken		if (new_ccb == NULL) {
588229997Sken			status = CAM_RESRC_UNAVAIL;
589229997Sken			break;
590229997Sken		}
591229997Sken
592229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
593229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
594229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
595260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
596229997Sken		xpt_action(new_ccb);
597229997Sken		softc->inots_sent++;
598229997Sken		status = new_ccb->ccb_h.status;
599237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
600237601Sken			/*
601237601Sken			 * Note that we don't free the CCB here.  If the
602237601Sken			 * status is not CAM_REQ_INPROG, then we're
603237601Sken			 * probably talking to a SIM that says it is
604237601Sken			 * target-capable but doesn't support the
605237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
606237601Sken			 * older API.  In that case, it'll call xpt_done()
607237601Sken			 * on the CCB, and we need to free it in our done
608237601Sken			 * routine as a result.
609237601Sken			 */
610229997Sken			break;
611229997Sken		}
612229997Sken	}
613237601Sken	if ((i == 0)
614237601Sken	 || (status != CAM_REQ_INPROG)) {
615229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
616229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
617229997Sken		return (CAM_REQ_CMP_ERR);
618229997Sken	}
619229997Sken	return (CAM_REQ_CMP);
620229997Sken}
621229997Sken
622229997Skenstatic void
623229997Skenctlfeoninvalidate(struct cam_periph *periph)
624229997Sken{
625229997Sken	union ccb en_lun_ccb;
626229997Sken	cam_status status;
627260387Sscottl	struct ctlfe_softc *bus_softc;
628229997Sken	struct ctlfe_lun_softc *softc;
629229997Sken
630229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
631229997Sken
632242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
633229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
634229997Sken	en_lun_ccb.cel.grp6_len = 0;
635229997Sken	en_lun_ccb.cel.grp7_len = 0;
636229997Sken	en_lun_ccb.cel.enable = 0;
637229997Sken	xpt_action(&en_lun_ccb);
638229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
639229997Sken	if (status != CAM_REQ_CMP) {
640229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
641229997Sken			  __func__, en_lun_ccb.ccb_h.status);
642229997Sken		/*
643229997Sken		 * XXX KDM what do we do now?
644229997Sken		 */
645229997Sken	}
646229997Sken	xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
647229997Sken		  "INOTs outstanding, %d refs\n", softc->atios_sent -
648229997Sken		  softc->atios_returned, softc->inots_sent -
649229997Sken		  softc->inots_returned, periph->refcount);
650260387Sscottl
651260387Sscottl	bus_softc = softc->parent_softc;
652260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
653260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
654260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
655229997Sken}
656229997Sken
657229997Skenstatic void
658229997Skenctlfecleanup(struct cam_periph *periph)
659229997Sken{
660229997Sken	struct ctlfe_lun_softc *softc;
661229997Sken
662229997Sken	xpt_print(periph->path, "%s: Called\n", __func__);
663229997Sken
664229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
665229997Sken
666229997Sken	/*
667229997Sken	 * XXX KDM is there anything else that needs to be done here?
668229997Sken	 */
669245228Sken
670245228Sken	callout_stop(&softc->dma_callout);
671245228Sken
672229997Sken	free(softc, M_CTLFE);
673229997Sken}
674229997Sken
675229997Skenstatic void
676265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
677265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
678265641Smav    u_int16_t *sglist_cnt)
679265641Smav{
680265641Smav	struct ctlfe_softc *bus_softc;
681265641Smav	struct ctlfe_lun_cmd_info *cmd_info;
682265641Smav	struct ctl_sg_entry *ctl_sglist;
683265641Smav	bus_dma_segment_t *cam_sglist;
684265641Smav	size_t off;
685265641Smav	int i, idx;
686265641Smav
687265641Smav	cmd_info = (struct ctlfe_lun_cmd_info *)io->io_hdr.port_priv;
688265641Smav	bus_softc = softc->parent_softc;
689265641Smav
690265641Smav	/*
691265641Smav	 * Set the direction, relative to the initiator.
692265641Smav	 */
693265641Smav	*flags &= ~CAM_DIR_MASK;
694265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
695265641Smav		*flags |= CAM_DIR_IN;
696265641Smav	else
697265641Smav		*flags |= CAM_DIR_OUT;
698265641Smav
699265641Smav	*flags &= ~CAM_DATA_MASK;
700265641Smav	idx = cmd_info->cur_transfer_index;
701265641Smav	off = cmd_info->cur_transfer_off;
702265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
703265641Smav	if (io->scsiio.kern_sg_entries == 0) {
704265641Smav		/* No S/G list. */
705265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
706265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
707265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
708265641Smav		} else {
709265641Smav			*dxfer_len = bus_softc->maxio;
710265641Smav			cmd_info->cur_transfer_index = -1;
711265641Smav			cmd_info->cur_transfer_off = bus_softc->maxio;
712265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
713265641Smav		}
714265641Smav		*sglist_cnt = 0;
715265641Smav
716265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
717265641Smav			*flags |= CAM_DATA_PADDR;
718265641Smav		else
719265641Smav			*flags |= CAM_DATA_VADDR;
720265641Smav	} else {
721265641Smav		/* S/G list with physical or virtual pointers. */
722265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
723265641Smav		cam_sglist = cmd_info->cam_sglist;
724265641Smav		*dxfer_len = 0;
725265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
726265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
727265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
728265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
729265641Smav				*dxfer_len += cam_sglist[i].ds_len;
730265641Smav			} else {
731265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
732265641Smav				cmd_info->cur_transfer_index = idx + i;
733265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
734265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
735265641Smav				*dxfer_len += cam_sglist[i].ds_len;
736265641Smav				if (ctl_sglist[i].len != 0)
737265641Smav					i++;
738265641Smav				break;
739265641Smav			}
740265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
741265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
742265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
743265641Smav				cmd_info->cur_transfer_off = 0;
744265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
745265641Smav				i++;
746265641Smav				break;
747265641Smav			}
748265641Smav			off = 0;
749265641Smav		}
750265641Smav		*sglist_cnt = i;
751265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
752265641Smav			*flags |= CAM_DATA_SG_PADDR;
753265641Smav		else
754265641Smav			*flags |= CAM_DATA_SG;
755265641Smav		*data_ptr = (uint8_t *)cam_sglist;
756265641Smav	}
757265641Smav}
758265641Smav
759265641Smavstatic void
760229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
761229997Sken{
762229997Sken	struct ctlfe_lun_softc *softc;
763229997Sken	struct ccb_hdr *ccb_h;
764229997Sken
765229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
766229997Sken
767229997Sken	softc->ccbs_alloced++;
768229997Sken
769245228Sken	start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT;
770245228Sken
771229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
772260387Sscottl	if (ccb_h == NULL) {
773229997Sken		softc->ccbs_freed++;
774229997Sken		xpt_release_ccb(start_ccb);
775229997Sken	} else {
776229997Sken		struct ccb_accept_tio *atio;
777229997Sken		struct ccb_scsiio *csio;
778229997Sken		uint8_t *data_ptr;
779229997Sken		uint32_t dxfer_len;
780229997Sken		ccb_flags flags;
781229997Sken		union ctl_io *io;
782229997Sken		uint8_t scsi_status;
783229997Sken
784229997Sken		/* Take the ATIO off the work queue */
785229997Sken		TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
786229997Sken		atio = (struct ccb_accept_tio *)ccb_h;
787229997Sken		io = (union ctl_io *)ccb_h->io_ptr;
788229997Sken		csio = &start_ccb->csio;
789229997Sken
790229997Sken		flags = atio->ccb_h.flags &
791229997Sken			(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
792229997Sken
793229997Sken		if ((io == NULL)
794229997Sken		 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
795229997Sken			/*
796229997Sken			 * We're done, send status back.
797229997Sken			 */
798229997Sken			flags |= CAM_SEND_STATUS;
799229997Sken			if (io == NULL) {
800229997Sken				scsi_status = SCSI_STATUS_BUSY;
801229997Sken				csio->sense_len = 0;
802269296Smav			} else if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
803268697Smav			    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
804229997Sken				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
805229997Sken
806229997Sken				/*
807229997Sken				 * If this command was aborted, we don't
808229997Sken				 * need to send status back to the SIM.
809229997Sken				 * Just free the CTIO and ctl_io, and
810229997Sken				 * recycle the ATIO back to the SIM.
811229997Sken				 */
812229997Sken				xpt_print(periph->path, "%s: aborted "
813229997Sken					  "command 0x%04x discarded\n",
814229997Sken					  __func__, io->scsiio.tag_num);
815229997Sken				ctl_free_io(io);
816229997Sken				/*
817229997Sken				 * For a wildcard attachment, commands can
818229997Sken				 * come in with a specific target/lun.  Reset
819229997Sken				 * the target and LUN fields back to the
820229997Sken				 * wildcard values before we send them back
821229997Sken				 * down to the SIM.  The SIM has a wildcard
822229997Sken				 * LUN enabled, not whatever target/lun
823229997Sken				 * these happened to be.
824229997Sken				 */
825229997Sken				if (softc->flags & CTLFE_LUN_WILDCARD) {
826229997Sken					atio->ccb_h.target_id =
827229997Sken						CAM_TARGET_WILDCARD;
828229997Sken					atio->ccb_h.target_lun =
829229997Sken						CAM_LUN_WILDCARD;
830229997Sken				}
831229997Sken
832229997Sken				if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
833229997Sken					cam_release_devq(periph->path,
834229997Sken							 /*relsim_flags*/0,
835229997Sken							 /*reduction*/0,
836229997Sken 							 /*timeout*/0,
837229997Sken							 /*getcount_only*/0);
838229997Sken					atio->ccb_h.status &= ~CAM_DEV_QFRZN;
839229997Sken				}
840229997Sken
841229997Sken				ccb_h = TAILQ_FIRST(&softc->work_queue);
842229997Sken
843229997Sken				if (atio->ccb_h.func_code !=
844229997Sken				    XPT_ACCEPT_TARGET_IO) {
845229997Sken					xpt_print(periph->path, "%s: func_code "
846229997Sken						  "is %#x\n", __func__,
847229997Sken						  atio->ccb_h.func_code);
848229997Sken				}
849229997Sken				start_ccb->ccb_h.func_code = XPT_ABORT;
850229997Sken				start_ccb->cab.abort_ccb = (union ccb *)atio;
851229997Sken
852229997Sken				/* Tell the SIM that we've aborted this ATIO */
853229997Sken				xpt_action(start_ccb);
854229997Sken				softc->ccbs_freed++;
855229997Sken				xpt_release_ccb(start_ccb);
856229997Sken
857229997Sken				/*
858229997Sken				 * Send the ATIO back down to the SIM.
859229997Sken				 */
860229997Sken				xpt_action((union ccb *)atio);
861229997Sken				softc->atios_sent++;
862229997Sken
863229997Sken				/*
864229997Sken				 * If we still have work to do, ask for
865229997Sken				 * another CCB.  Otherwise, deactivate our
866229997Sken				 * callout.
867229997Sken				 */
868229997Sken				if (ccb_h != NULL)
869229997Sken					xpt_schedule(periph, /*priority*/ 1);
870229997Sken				else
871229997Sken					callout_stop(&softc->dma_callout);
872229997Sken
873229997Sken				return;
874229997Sken			} else {
875229997Sken				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
876229997Sken				scsi_status = io->scsiio.scsi_status;
877229997Sken				csio->sense_len = io->scsiio.sense_len;
878229997Sken			}
879229997Sken			data_ptr = NULL;
880229997Sken			dxfer_len = 0;
881229997Sken			if (io == NULL) {
882229997Sken				printf("%s: tag %04x io is NULL\n", __func__,
883229997Sken				       atio->tag_id);
884229997Sken			} else {
885229997Sken#ifdef CTLFEDEBUG
886229997Sken				printf("%s: tag %04x status %x\n", __func__,
887229997Sken				       atio->tag_id, io->io_hdr.status);
888229997Sken#endif
889229997Sken			}
890229997Sken			csio->sglist_cnt = 0;
891229997Sken			if (csio->sense_len != 0) {
892229997Sken				csio->sense_data = io->scsiio.sense_data;
893229997Sken				flags |= CAM_SEND_SENSE;
894229997Sken			} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
895229997Sken				xpt_print(periph->path, "%s: check condition "
896229997Sken					  "with no sense\n", __func__);
897229997Sken			}
898229997Sken		} else {
899229997Sken			struct ctlfe_lun_cmd_info *cmd_info;
900229997Sken
901229997Sken			/*
902229997Sken			 * Datamove call, we need to setup the S/G list.
903229997Sken			 */
904229997Sken
905229997Sken			cmd_info = (struct ctlfe_lun_cmd_info *)
906229997Sken				io->io_hdr.port_priv;
907229997Sken
908229997Sken			KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
909229997Sken				("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
910229997Sken				"CTL_PORT_PRIV_SIZE %d", __func__,
911229997Sken				sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
912229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
913229997Sken
914229997Sken			/*
915229997Sken			 * Need to zero this, in case it has been used for
916229997Sken			 * a previous datamove for this particular I/O.
917229997Sken			 */
918229997Sken			bzero(cmd_info, sizeof(*cmd_info));
919229997Sken			scsi_status = 0;
920229997Sken
921229997Sken			csio->cdb_len = atio->cdb_len;
922229997Sken
923265641Smav			ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
924265641Smav			    &csio->sglist_cnt);
925229997Sken
926229997Sken			io->scsiio.ext_data_filled += dxfer_len;
927229997Sken
928229997Sken			if (io->scsiio.ext_data_filled >
929229997Sken			    io->scsiio.kern_total_len) {
930229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
931229997Sken					  "fill len %u > total %u\n",
932229997Sken					  __func__, io->scsiio.tag_num,
933229997Sken					  io->scsiio.ext_data_filled,
934229997Sken					  io->scsiio.kern_total_len);
935229997Sken			}
936229997Sken		}
937229997Sken
938229997Sken#ifdef CTLFEDEBUG
939229997Sken		printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
940229997Sken		       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
941229997Sken		       atio->tag_id, flags, data_ptr, dxfer_len);
942229997Sken#endif
943229997Sken
944229997Sken		/*
945229997Sken		 * Valid combinations:
946255117Smav		 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
947229997Sken		 *    sglist_cnt = 0
948255117Smav		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
949229997Sken		 *    sglist_cnt = 0
950255117Smav		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
951229997Sken		 *    sglist_cnt != 0
952229997Sken		 */
953229997Sken#ifdef CTLFEDEBUG
954229997Sken		if (((flags & CAM_SEND_STATUS)
955255117Smav		  && (((flags & CAM_DATA_SG) != 0)
956229997Sken		   || (dxfer_len != 0)
957229997Sken		   || (csio->sglist_cnt != 0)))
958229997Sken		 || (((flags & CAM_SEND_STATUS) == 0)
959229997Sken		  && (dxfer_len == 0))
960255117Smav		 || ((flags & CAM_DATA_SG)
961229997Sken		  && (csio->sglist_cnt == 0))
962255117Smav		 || (((flags & CAM_DATA_SG) == 0)
963229997Sken		  && (csio->sglist_cnt != 0))) {
964229997Sken			printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
965229997Sken			       "%d sg %u\n", __func__, atio->tag_id,
966229997Sken			       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
967229997Sken			       csio->sglist_cnt);
968229997Sken			if (io != NULL) {
969229997Sken				printf("%s: tag %04x io status %#x\n", __func__,
970229997Sken				       atio->tag_id, io->io_hdr.status);
971229997Sken			} else {
972229997Sken				printf("%s: tag %04x no associated io\n",
973229997Sken				       __func__, atio->tag_id);
974229997Sken			}
975229997Sken		}
976229997Sken#endif
977229997Sken		cam_fill_ctio(csio,
978229997Sken			      /*retries*/ 2,
979229997Sken			      ctlfedone,
980229997Sken			      flags,
981229997Sken			      (flags & CAM_TAG_ACTION_VALID) ?
982229997Sken			       MSG_SIMPLE_Q_TAG : 0,
983229997Sken			      atio->tag_id,
984229997Sken			      atio->init_id,
985229997Sken			      scsi_status,
986229997Sken			      /*data_ptr*/ data_ptr,
987229997Sken			      /*dxfer_len*/ dxfer_len,
988229997Sken			      /*timeout*/ 5 * 1000);
989260387Sscottl		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
990229997Sken		start_ccb->ccb_h.ccb_atio = atio;
991229997Sken		if (((flags & CAM_SEND_STATUS) == 0)
992229997Sken		 && (io != NULL))
993229997Sken			io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
994229997Sken
995229997Sken		softc->ctios_sent++;
996229997Sken
997260387Sscottl		cam_periph_unlock(periph);
998229997Sken		xpt_action(start_ccb);
999260387Sscottl		cam_periph_lock(periph);
1000229997Sken
1001229997Sken		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1002229997Sken			cam_release_devq(periph->path,
1003229997Sken					 /*relsim_flags*/0,
1004229997Sken					 /*reduction*/0,
1005229997Sken 					 /*timeout*/0,
1006229997Sken					 /*getcount_only*/0);
1007229997Sken			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
1008229997Sken		}
1009229997Sken
1010229997Sken		ccb_h = TAILQ_FIRST(&softc->work_queue);
1011229997Sken	}
1012229997Sken	/*
1013229997Sken	 * If we still have work to do, ask for another CCB.  Otherwise,
1014229997Sken	 * deactivate our callout.
1015229997Sken	 */
1016229997Sken	if (ccb_h != NULL)
1017229997Sken		xpt_schedule(periph, /*priority*/ 1);
1018229997Sken	else
1019229997Sken		callout_stop(&softc->dma_callout);
1020229997Sken}
1021229997Sken
1022229997Skenstatic void
1023229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
1024229997Sken{
1025229997Sken	struct ctlfe_lun_softc *softc;
1026229997Sken
1027229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1028229997Sken
1029229997Sken	switch (ccb->ccb_h.func_code) {
1030229997Sken	case XPT_ACCEPT_TARGET_IO:
1031229997Sken		softc->atios_returned++;
1032229997Sken		break;
1033229997Sken	case XPT_IMMEDIATE_NOTIFY:
1034229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1035229997Sken		softc->inots_returned++;
1036229997Sken		break;
1037229997Sken	default:
1038229997Sken		break;
1039229997Sken	}
1040229997Sken
1041229997Sken	free(ccb, M_CTLFE);
1042229997Sken
1043229997Sken	KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1044229997Sken		"atios_returned %ju > atios_sent %ju", __func__,
1045229997Sken		softc->atios_returned, softc->atios_sent));
1046229997Sken	KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1047229997Sken		"inots_returned %ju > inots_sent %ju", __func__,
1048229997Sken		softc->inots_returned, softc->inots_sent));
1049229997Sken
1050229997Sken	/*
1051229997Sken	 * If we have received all of our CCBs, we can release our
1052229997Sken	 * reference on the peripheral driver.  It will probably go away
1053229997Sken	 * now.
1054229997Sken	 */
1055229997Sken	if ((softc->atios_returned == softc->atios_sent)
1056229997Sken	 && (softc->inots_returned == softc->inots_sent)) {
1057229997Sken		cam_periph_release_locked(periph);
1058229997Sken	}
1059229997Sken}
1060229997Sken
1061238870Smjacobstatic int
1062238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1063238870Smjacob{
1064238870Smjacob	uint64_t lba;
1065238870Smjacob	uint32_t num_blocks, nbc;
1066238870Smjacob	uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1067238870Smjacob	    atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1068238870Smjacob
1069238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1070238870Smjacob
1071238870Smjacob	switch (cmdbyt[0]) {
1072238870Smjacob	case READ_6:
1073238870Smjacob	case WRITE_6:
1074238870Smjacob	{
1075238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1076238870Smjacob		lba = scsi_3btoul(cdb->addr);
1077238870Smjacob		lba &= 0x1fffff;
1078238870Smjacob		num_blocks = cdb->length;
1079238870Smjacob		if (num_blocks == 0)
1080238870Smjacob			num_blocks = 256;
1081238870Smjacob		lba += nbc;
1082238870Smjacob		num_blocks -= nbc;
1083238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1084238870Smjacob		cdb->length = num_blocks;
1085238870Smjacob		break;
1086238870Smjacob	}
1087238870Smjacob	case READ_10:
1088238870Smjacob	case WRITE_10:
1089238870Smjacob	{
1090238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1091238870Smjacob		lba = scsi_4btoul(cdb->addr);
1092238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1093238870Smjacob		lba += nbc;
1094238870Smjacob		num_blocks -= nbc;
1095238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1096238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1097238870Smjacob		break;
1098238870Smjacob	}
1099238870Smjacob	case READ_12:
1100238870Smjacob	case WRITE_12:
1101238870Smjacob	{
1102238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1103238870Smjacob		lba = scsi_4btoul(cdb->addr);
1104238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1105238870Smjacob		lba += nbc;
1106238870Smjacob		num_blocks -= nbc;
1107238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1108238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1109238870Smjacob		break;
1110238870Smjacob	}
1111238870Smjacob	case READ_16:
1112238870Smjacob	case WRITE_16:
1113273311Smav	case WRITE_ATOMIC_16:
1114238870Smjacob	{
1115238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1116238870Smjacob		lba = scsi_8btou64(cdb->addr);
1117238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1118238870Smjacob		lba += nbc;
1119238870Smjacob		num_blocks -= nbc;
1120238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1121238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1122238870Smjacob		break;
1123238870Smjacob	}
1124238870Smjacob	default:
1125238870Smjacob		return -1;
1126238870Smjacob	}
1127238870Smjacob	return (0);
1128238870Smjacob}
1129238870Smjacob
1130229997Skenstatic void
1131229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1132229997Sken{
1133229997Sken	struct ctlfe_lun_softc *softc;
1134229997Sken	struct ctlfe_softc *bus_softc;
1135238870Smjacob	struct ccb_accept_tio *atio = NULL;
1136238870Smjacob	union ctl_io *io = NULL;
1137260387Sscottl	struct mtx *mtx;
1138229997Sken
1139260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1140260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1141229997Sken#ifdef CTLFE_DEBUG
1142229997Sken	printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
1143229997Sken	       done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
1144229997Sken#endif
1145229997Sken
1146229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1147229997Sken	bus_softc = softc->parent_softc;
1148260387Sscottl	mtx = cam_periph_mtx(periph);
1149260387Sscottl	mtx_lock(mtx);
1150229997Sken
1151229997Sken	/*
1152229997Sken	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1153229997Sken	 * need to be freed.  Most of the ATIOs and INOTs that come back
1154229997Sken	 * will be CCBs that are being returned from the SIM as a result of
1155229997Sken	 * our disabling the LUN.
1156229997Sken	 *
1157229997Sken	 * Other CCB types are handled in their respective cases below.
1158229997Sken	 */
1159229997Sken	if (periph->flags & CAM_PERIPH_INVALID) {
1160229997Sken		switch (done_ccb->ccb_h.func_code) {
1161229997Sken		case XPT_ACCEPT_TARGET_IO:
1162229997Sken		case XPT_IMMEDIATE_NOTIFY:
1163229997Sken		case XPT_NOTIFY_ACKNOWLEDGE:
1164229997Sken			ctlfe_free_ccb(periph, done_ccb);
1165260387Sscottl			goto out;
1166229997Sken		default:
1167229997Sken			break;
1168229997Sken		}
1169229997Sken
1170229997Sken	}
1171229997Sken	switch (done_ccb->ccb_h.func_code) {
1172229997Sken	case XPT_ACCEPT_TARGET_IO: {
1173229997Sken
1174229997Sken		atio = &done_ccb->atio;
1175229997Sken
1176229997Sken		softc->atios_returned++;
1177229997Sken
1178238870Smjacob resubmit:
1179229997Sken		/*
1180229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1181229997Sken		 * datamove or done.
1182229997Sken		 */
1183268677Smav		io = ctl_alloc_io(bus_softc->port.ctl_pool_ref);
1184229997Sken		if (io == NULL) {
1185229997Sken			atio->ccb_h.flags &= ~CAM_DIR_MASK;
1186229997Sken			atio->ccb_h.flags |= CAM_DIR_NONE;
1187229997Sken
1188229997Sken			printf("%s: ctl_alloc_io failed!\n", __func__);
1189229997Sken
1190229997Sken			/*
1191229997Sken			 * XXX KDM need to set SCSI_STATUS_BUSY, but there
1192229997Sken			 * is no field in the ATIO structure to do that,
1193229997Sken			 * and we aren't able to allocate a ctl_io here.
1194229997Sken			 * What to do?
1195229997Sken			 */
1196229997Sken			atio->sense_len = 0;
1197229997Sken			done_ccb->ccb_h.io_ptr = NULL;
1198229997Sken			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
1199229997Sken					  periph_links.tqe);
1200229997Sken			xpt_schedule(periph, /*priority*/ 1);
1201229997Sken			break;
1202229997Sken		}
1203260387Sscottl		mtx_unlock(mtx);
1204229997Sken		ctl_zero_io(io);
1205229997Sken
1206229997Sken		/* Save pointers on both sides */
1207229997Sken		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1208229997Sken		done_ccb->ccb_h.io_ptr = io;
1209229997Sken
1210229997Sken		/*
1211229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1212229997Sken		 * down the immediate notify path below.
1213229997Sken		 */
1214229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1215229997Sken		io->io_hdr.nexus.initid.id = atio->init_id;
1216268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1217229997Sken		io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1218229997Sken		io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1219229997Sken		io->scsiio.tag_num = atio->tag_id;
1220229997Sken		switch (atio->tag_action) {
1221229997Sken		case CAM_TAG_ACTION_NONE:
1222229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1223229997Sken			break;
1224229997Sken		case MSG_SIMPLE_TASK:
1225229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1226229997Sken			break;
1227229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1228229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1229229997Sken			break;
1230229997Sken		case MSG_ORDERED_TASK:
1231229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1232229997Sken			break;
1233229997Sken		case MSG_ACA_TASK:
1234229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1235229997Sken			break;
1236229997Sken		default:
1237229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1238229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1239229997Sken			       atio->tag_action);
1240229997Sken			break;
1241229997Sken		}
1242229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1243229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1244229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1245229997Sken		}
1246229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1247229997Sken		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1248229997Sken		      io->scsiio.cdb_len);
1249229997Sken
1250229997Sken#ifdef CTLFEDEBUG
1251229997Sken		printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1252229997Sken		        (uintmax_t)io->io_hdr.nexus.initid.id,
1253229997Sken		        io->io_hdr.nexus.targ_port,
1254229997Sken		        (uintmax_t)io->io_hdr.nexus.targ_target.id,
1255229997Sken		        io->io_hdr.nexus.targ_lun,
1256229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1257229997Sken#endif
1258229997Sken
1259229997Sken		ctl_queue(io);
1260260387Sscottl		return;
1261229997Sken	}
1262229997Sken	case XPT_CONT_TARGET_IO: {
1263238870Smjacob		int srr = 0;
1264238870Smjacob		uint32_t srr_off = 0;
1265229997Sken
1266229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1267229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1268229997Sken
1269229997Sken		softc->ctios_returned++;
1270229997Sken#ifdef CTLFEDEBUG
1271229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1272229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1273229997Sken#endif
1274229997Sken		/*
1275238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1276238870Smjacob		 */
1277238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1278238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1279238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1280238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1281238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1282238870Smjacob			srr = 1;
1283238870Smjacob			srr_off =
1284238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1285238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1286238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1287238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1288238870Smjacob		}
1289238870Smjacob
1290238870Smjacob		if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) {
1291238870Smjacob			/*
1292238870Smjacob			 * If status was being sent, the back end data is now
1293238870Smjacob			 * history. Hack it up and resubmit a new command with
1294238870Smjacob			 * the CDB adjusted. If the SIM does the right thing,
1295238870Smjacob			 * all of the resid math should work.
1296238870Smjacob			 */
1297238870Smjacob			softc->ccbs_freed++;
1298238870Smjacob			xpt_release_ccb(done_ccb);
1299238870Smjacob			ctl_free_io(io);
1300238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1301238870Smjacob				done_ccb = (union ccb *)atio;
1302238870Smjacob				goto resubmit;
1303238870Smjacob			}
1304238870Smjacob			/*
1305238870Smjacob			 * Fall through to doom....
1306238870Smjacob			 */
1307238870Smjacob		} else if (srr) {
1308238870Smjacob			/*
1309238870Smjacob			 * If we have an srr and we're still sending data, we
1310238870Smjacob			 * should be able to adjust offsets and cycle again.
1311238870Smjacob			 */
1312238870Smjacob			io->scsiio.kern_rel_offset =
1313238870Smjacob			    io->scsiio.ext_data_filled = srr_off;
1314238870Smjacob			io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1315238870Smjacob			    io->scsiio.kern_rel_offset;
1316238870Smjacob			softc->ccbs_freed++;
1317238870Smjacob			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1318238870Smjacob			xpt_release_ccb(done_ccb);
1319238870Smjacob			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1320238870Smjacob					  periph_links.tqe);
1321238870Smjacob			xpt_schedule(periph, /*priority*/ 1);
1322260387Sscottl			break;
1323238870Smjacob		}
1324238870Smjacob
1325238870Smjacob		/*
1326229997Sken		 * If we were sending status back to the initiator, free up
1327229997Sken		 * resources.  If we were doing a datamove, call the
1328229997Sken		 * datamove done routine.
1329229997Sken		 */
1330229997Sken		if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
1331229997Sken			softc->ccbs_freed++;
1332229997Sken			xpt_release_ccb(done_ccb);
1333229997Sken			ctl_free_io(io);
1334229997Sken			/*
1335229997Sken			 * For a wildcard attachment, commands can come in
1336229997Sken			 * with a specific target/lun.  Reset the target
1337229997Sken			 * and LUN fields back to the wildcard values before
1338229997Sken			 * we send them back down to the SIM.  The SIM has
1339229997Sken			 * a wildcard LUN enabled, not whatever target/lun
1340229997Sken			 * these happened to be.
1341229997Sken			 */
1342229997Sken			if (softc->flags & CTLFE_LUN_WILDCARD) {
1343229997Sken				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1344229997Sken				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1345229997Sken			}
1346229997Sken			if (periph->flags & CAM_PERIPH_INVALID) {
1347229997Sken				ctlfe_free_ccb(periph, (union ccb *)atio);
1348229997Sken			} else {
1349260387Sscottl				softc->atios_sent++;
1350260387Sscottl				mtx_unlock(mtx);
1351229997Sken				xpt_action((union ccb *)atio);
1352260387Sscottl				return;
1353229997Sken			}
1354229997Sken		} else {
1355229997Sken			struct ctlfe_lun_cmd_info *cmd_info;
1356229997Sken			struct ccb_scsiio *csio;
1357229997Sken
1358229997Sken			csio = &done_ccb->csio;
1359229997Sken			cmd_info = (struct ctlfe_lun_cmd_info *)
1360229997Sken				io->io_hdr.port_priv;
1361229997Sken
1362229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1363229997Sken
1364229997Sken			io->scsiio.ext_data_len += csio->dxfer_len;
1365229997Sken			if (io->scsiio.ext_data_len >
1366229997Sken			    io->scsiio.kern_total_len) {
1367229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
1368229997Sken					  "done len %u > total %u sent %u\n",
1369229997Sken					  __func__, io->scsiio.tag_num,
1370229997Sken					  io->scsiio.ext_data_len,
1371229997Sken					  io->scsiio.kern_total_len,
1372229997Sken					  io->scsiio.ext_data_filled);
1373229997Sken			}
1374229997Sken			/*
1375229997Sken			 * Translate CAM status to CTL status.  Success
1376229997Sken			 * does not change the overall, ctl_io status.  In
1377229997Sken			 * that case we just set port_status to 0.  If we
1378229997Sken			 * have a failure, though, set a data phase error
1379229997Sken			 * for the overall ctl_io.
1380229997Sken			 */
1381229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1382229997Sken			case CAM_REQ_CMP:
1383229997Sken				io->io_hdr.port_status = 0;
1384229997Sken				break;
1385229997Sken			default:
1386229997Sken				/*
1387245228Sken				 * XXX KDM we probably need to figure out a
1388245228Sken				 * standard set of errors that the SIM
1389245228Sken				 * drivers should return in the event of a
1390245228Sken				 * data transfer failure.  A data phase
1391245228Sken				 * error will at least point the user to a
1392245228Sken				 * data transfer error of some sort.
1393245228Sken				 * Hopefully the SIM printed out some
1394245228Sken				 * additional information to give the user
1395245228Sken				 * a clue what happened.
1396229997Sken				 */
1397229997Sken				io->io_hdr.port_status = 0xbad1;
1398229997Sken				ctl_set_data_phase_error(&io->scsiio);
1399229997Sken				/*
1400229997Sken				 * XXX KDM figure out residual.
1401229997Sken				 */
1402229997Sken				break;
1403229997Sken			}
1404229997Sken			/*
1405229997Sken			 * If we had to break this S/G list into multiple
1406229997Sken			 * pieces, figure out where we are in the list, and
1407229997Sken			 * continue sending pieces if necessary.
1408229997Sken			 */
1409229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1410265641Smav			 && (io->io_hdr.port_status == 0)) {
1411229997Sken				ccb_flags flags;
1412229997Sken				uint8_t scsi_status;
1413229997Sken				uint8_t *data_ptr;
1414229997Sken				uint32_t dxfer_len;
1415229997Sken
1416229997Sken				flags = atio->ccb_h.flags &
1417229997Sken					(CAM_DIS_DISCONNECT|
1418265641Smav					 CAM_TAG_ACTION_VALID);
1419229997Sken
1420265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1421265641Smav				    &dxfer_len, &csio->sglist_cnt);
1422229997Sken
1423229997Sken				scsi_status = 0;
1424229997Sken
1425229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1426229997Sken				 && (dxfer_len == 0)) {
1427229997Sken					printf("%s: tag %04x no status or "
1428229997Sken					       "len cdb = %02x\n", __func__,
1429229997Sken					       atio->tag_id,
1430229997Sken					atio->cdb_io.cdb_bytes[0]);
1431229997Sken					printf("%s: tag %04x io status %#x\n",
1432229997Sken					       __func__, atio->tag_id,
1433229997Sken					       io->io_hdr.status);
1434229997Sken				}
1435229997Sken
1436229997Sken				cam_fill_ctio(csio,
1437229997Sken					      /*retries*/ 2,
1438229997Sken					      ctlfedone,
1439229997Sken					      flags,
1440229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1441229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1442229997Sken					      atio->tag_id,
1443229997Sken					      atio->init_id,
1444229997Sken					      scsi_status,
1445229997Sken					      /*data_ptr*/ data_ptr,
1446229997Sken					      /*dxfer_len*/ dxfer_len,
1447229997Sken					      /*timeout*/ 5 * 1000);
1448229997Sken
1449260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1450229997Sken				csio->resid = 0;
1451229997Sken				csio->ccb_h.ccb_atio = atio;
1452229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1453229997Sken				softc->ctios_sent++;
1454260387Sscottl				mtx_unlock(mtx);
1455229997Sken				xpt_action((union ccb *)csio);
1456229997Sken			} else {
1457229997Sken				/*
1458229997Sken				 * Release the CTIO.  The ATIO will be sent back
1459229997Sken				 * down to the SIM once we send status.
1460229997Sken				 */
1461229997Sken				softc->ccbs_freed++;
1462229997Sken				xpt_release_ccb(done_ccb);
1463260387Sscottl				mtx_unlock(mtx);
1464229997Sken
1465229997Sken				/* Call the backend move done callback */
1466229997Sken				io->scsiio.be_move_done(io);
1467229997Sken			}
1468260387Sscottl			return;
1469229997Sken		}
1470229997Sken		break;
1471229997Sken	}
1472229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1473229997Sken		union ctl_io *io;
1474229997Sken		struct ccb_immediate_notify *inot;
1475229997Sken		cam_status status;
1476229997Sken		int frozen;
1477229997Sken
1478229997Sken		inot = &done_ccb->cin1;
1479229997Sken
1480229997Sken		softc->inots_returned++;
1481229997Sken
1482229997Sken		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1483229997Sken
1484229997Sken		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1485229997Sken		       "seq %#x\n", __func__, inot->ccb_h.status,
1486229997Sken		       inot->tag_id, inot->seq_id);
1487229997Sken
1488268677Smav		io = ctl_alloc_io(bus_softc->port.ctl_pool_ref);
1489229997Sken		if (io != NULL) {
1490229997Sken			int send_ctl_io;
1491229997Sken
1492229997Sken			send_ctl_io = 1;
1493229997Sken
1494229997Sken			ctl_zero_io(io);
1495229997Sken			io->io_hdr.io_type = CTL_IO_TASK;
1496229997Sken			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1497229997Sken			inot->ccb_h.io_ptr = io;
1498229997Sken			io->io_hdr.nexus.initid.id = inot->initiator_id;
1499268677Smav			io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1500229997Sken			io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1501229997Sken			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1502229997Sken			/* XXX KDM should this be the tag_id? */
1503229997Sken			io->taskio.tag_num = inot->seq_id;
1504229997Sken
1505229997Sken			status = inot->ccb_h.status & CAM_STATUS_MASK;
1506229997Sken			switch (status) {
1507229997Sken			case CAM_SCSI_BUS_RESET:
1508229997Sken				io->taskio.task_action = CTL_TASK_BUS_RESET;
1509229997Sken				break;
1510229997Sken			case CAM_BDR_SENT:
1511229997Sken				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1512229997Sken				break;
1513229997Sken			case CAM_MESSAGE_RECV:
1514229997Sken				switch (inot->arg) {
1515229997Sken				case MSG_ABORT_TASK_SET:
1516229997Sken					io->taskio.task_action =
1517229997Sken						CTL_TASK_ABORT_TASK_SET;
1518229997Sken					break;
1519229997Sken				case MSG_TARGET_RESET:
1520229997Sken					io->taskio.task_action =
1521229997Sken						CTL_TASK_TARGET_RESET;
1522229997Sken					break;
1523229997Sken				case MSG_ABORT_TASK:
1524229997Sken					io->taskio.task_action =
1525229997Sken						CTL_TASK_ABORT_TASK;
1526229997Sken					break;
1527229997Sken				case MSG_LOGICAL_UNIT_RESET:
1528229997Sken					io->taskio.task_action =
1529229997Sken						CTL_TASK_LUN_RESET;
1530229997Sken					break;
1531229997Sken				case MSG_CLEAR_TASK_SET:
1532229997Sken					io->taskio.task_action =
1533229997Sken						CTL_TASK_CLEAR_TASK_SET;
1534229997Sken					break;
1535229997Sken				case MSG_CLEAR_ACA:
1536229997Sken					io->taskio.task_action =
1537229997Sken						CTL_TASK_CLEAR_ACA;
1538229997Sken					break;
1539229997Sken				case MSG_NOOP:
1540229997Sken					send_ctl_io = 0;
1541229997Sken					break;
1542229997Sken				default:
1543229997Sken					xpt_print(periph->path, "%s: "
1544229997Sken						  "unsupported message 0x%x\n",
1545229997Sken						  __func__, inot->arg);
1546229997Sken					send_ctl_io = 0;
1547229997Sken					break;
1548229997Sken				}
1549229997Sken				break;
1550229997Sken			case CAM_REQ_ABORTED:
1551229997Sken				/*
1552229997Sken				 * This request was sent back by the driver.
1553229997Sken				 * XXX KDM what do we do here?
1554229997Sken				 */
1555229997Sken				send_ctl_io = 0;
1556229997Sken				break;
1557237601Sken			case CAM_REQ_INVALID:
1558237601Sken			case CAM_PROVIDE_FAIL:
1559229997Sken			default:
1560237601Sken				/*
1561237601Sken				 * We should only get here if we're talking
1562237601Sken				 * to a talking to a SIM that is target
1563237601Sken				 * capable but supports the old API.  In
1564237601Sken				 * that case, we need to just free the CCB.
1565237601Sken				 * If we actually send a notify acknowledge,
1566237601Sken				 * it will send that back with an error as
1567237601Sken				 * well.
1568237601Sken				 */
1569237601Sken
1570237601Sken				if ((status != CAM_REQ_INVALID)
1571237601Sken				 && (status != CAM_PROVIDE_FAIL))
1572237601Sken					xpt_print(periph->path, "%s: "
1573237601Sken						  "unsupported CAM status "
1574237601Sken						  "0x%x\n", __func__, status);
1575237601Sken
1576237601Sken				ctl_free_io(io);
1577237601Sken				ctlfe_free_ccb(periph, done_ccb);
1578237601Sken
1579260387Sscottl				goto out;
1580229997Sken			}
1581229997Sken			if (send_ctl_io != 0) {
1582229997Sken				ctl_queue(io);
1583229997Sken			} else {
1584229997Sken				ctl_free_io(io);
1585229997Sken				done_ccb->ccb_h.status = CAM_REQ_INPROG;
1586229997Sken				done_ccb->ccb_h.func_code =
1587229997Sken					XPT_NOTIFY_ACKNOWLEDGE;
1588229997Sken				xpt_action(done_ccb);
1589229997Sken			}
1590229997Sken		} else {
1591229997Sken			xpt_print(periph->path, "%s: could not allocate "
1592229997Sken				  "ctl_io for immediate notify!\n", __func__);
1593229997Sken			/* requeue this to the adapter */
1594229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1595229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1596229997Sken			xpt_action(done_ccb);
1597229997Sken		}
1598229997Sken
1599229997Sken		if (frozen != 0) {
1600229997Sken			cam_release_devq(periph->path,
1601229997Sken					 /*relsim_flags*/ 0,
1602229997Sken					 /*opening reduction*/ 0,
1603229997Sken					 /*timeout*/ 0,
1604229997Sken					 /*getcount_only*/ 0);
1605229997Sken		}
1606229997Sken		break;
1607229997Sken	}
1608229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1609229997Sken		/*
1610229997Sken		 * Queue this back down to the SIM as an immediate notify.
1611229997Sken		 */
1612229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1613229997Sken		xpt_action(done_ccb);
1614229997Sken		softc->inots_sent++;
1615229997Sken		break;
1616229997Sken	case XPT_SET_SIM_KNOB:
1617229997Sken	case XPT_GET_SIM_KNOB:
1618229997Sken		break;
1619229997Sken	default:
1620229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1621229997Sken		      done_ccb->ccb_h.func_code);
1622229997Sken		break;
1623229997Sken	}
1624260387Sscottl
1625260387Sscottlout:
1626260387Sscottl	mtx_unlock(mtx);
1627229997Sken}
1628229997Sken
1629229997Skenstatic void
1630229997Skenctlfe_onoffline(void *arg, int online)
1631229997Sken{
1632229997Sken	struct ctlfe_softc *bus_softc;
1633229997Sken	union ccb *ccb;
1634229997Sken	cam_status status;
1635229997Sken	struct cam_path *path;
1636229997Sken	int set_wwnn;
1637229997Sken
1638229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1639229997Sken
1640229997Sken	set_wwnn = 0;
1641229997Sken
1642229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1643229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1644229997Sken	if (status != CAM_REQ_CMP) {
1645229997Sken		printf("%s: unable to create path!\n", __func__);
1646229997Sken		return;
1647229997Sken	}
1648245228Sken	ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO);
1649245228Sken	if (ccb == NULL) {
1650245228Sken		printf("%s: unable to malloc CCB!\n", __func__);
1651245228Sken		xpt_free_path(path);
1652245228Sken		return;
1653245228Sken	}
1654242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1655229997Sken
1656229997Sken	/*
1657229997Sken	 * Copan WWN format:
1658229997Sken	 *
1659229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1660229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1661229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1662229997Sken	 * Bits 11-8:			Type of port:
1663229997Sken	 *					1 == N-Port
1664229997Sken	 *					2 == F-Port
1665229997Sken	 *					3 == NL-Port
1666229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1667229997Sken	 */
1668229997Sken
1669229997Sken	if (online != 0) {
1670229997Sken
1671229997Sken		ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1672229997Sken
1673229997Sken
1674229997Sken		xpt_action(ccb);
1675229997Sken
1676229997Sken
1677229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1678229997Sken#ifdef RANDOM_WWNN
1679229997Sken			uint64_t random_bits;
1680229997Sken#endif
1681229997Sken
1682229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1683229997Sken			       bus_softc->port_name,
1684229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1685229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1686229997Sken			       bus_softc->port_name,
1687229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1688229997Sken
1689229997Sken#ifdef RANDOM_WWNN
1690229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1691229997Sken#endif
1692229997Sken
1693229997Sken			/*
1694229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1695229997Sken			 * take the current WWNN/WWPN from the card, and
1696229997Sken			 * replace the company identifier and the NL-Port
1697229997Sken			 * indicator and the port number (for the WWPN).
1698229997Sken			 * This should be replaced later with ddb_GetWWNN,
1699229997Sken			 * or possibly a more centralized scheme.  (It
1700229997Sken			 * would be nice to have the WWNN/WWPN for each
1701268677Smav			 * port stored in the ctl_port structure.)
1702229997Sken			 */
1703229997Sken#ifdef RANDOM_WWNN
1704229997Sken			ccb->knob.xport_specific.fc.wwnn =
1705229997Sken				(random_bits &
1706229997Sken				0x0000000fffffff00ULL) |
1707229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1708229997Sken				/* NL-Port */    0x0300;
1709229997Sken			ccb->knob.xport_specific.fc.wwpn =
1710229997Sken				(random_bits &
1711229997Sken				0x0000000fffffff00ULL) |
1712229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1713229997Sken				/* NL-Port */    0x3000 |
1714268677Smav				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1715229997Sken
1716229997Sken			/*
1717229997Sken			 * This is a bit of an API break/reversal, but if
1718229997Sken			 * we're doing the random WWNN that's a little
1719229997Sken			 * different anyway.  So record what we're actually
1720229997Sken			 * using with the frontend code so it's reported
1721229997Sken			 * accurately.
1722229997Sken			 */
1723268683Smav			ctl_port_set_wwns(&bus_softc->port,
1724268683Smav			    true, ccb->knob.xport_specific.fc.wwnn,
1725268683Smav			    true, ccb->knob.xport_specific.fc.wwpn);
1726229997Sken			set_wwnn = 1;
1727229997Sken#else /* RANDOM_WWNN */
1728229997Sken			/*
1729229997Sken			 * If the user has specified a WWNN/WWPN, send them
1730229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1731229997Sken			 * has reported.
1732229997Sken			 */
1733268677Smav			if ((bus_softc->port.wwnn != 0)
1734268677Smav			 && (bus_softc->port.wwpn != 0)) {
1735229997Sken				ccb->knob.xport_specific.fc.wwnn =
1736268677Smav					bus_softc->port.wwnn;
1737229997Sken				ccb->knob.xport_specific.fc.wwpn =
1738268677Smav					bus_softc->port.wwpn;
1739229997Sken				set_wwnn = 1;
1740229997Sken			} else {
1741268683Smav				ctl_port_set_wwns(&bus_softc->port,
1742268683Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1743268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1744229997Sken			}
1745229997Sken#endif /* RANDOM_WWNN */
1746229997Sken
1747229997Sken
1748229997Sken			if (set_wwnn != 0) {
1749229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1750229997Sken				       bus_softc->port_name,
1751229997Sken				ccb->knob.xport_specific.fc.wwnn);
1752229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1753229997Sken				       bus_softc->port_name,
1754229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1755229997Sken			}
1756229997Sken		} else {
1757229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1758229997Sken			       bus_softc->port_name);
1759229997Sken		}
1760229997Sken	}
1761229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1762229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1763229997Sken	if (set_wwnn != 0)
1764229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1765229997Sken
1766229997Sken	if (online != 0)
1767229997Sken		ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1768229997Sken	else
1769229997Sken		ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1770229997Sken
1771229997Sken	xpt_action(ccb);
1772229997Sken
1773229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1774229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1775229997Sken		       "status %#x\n",
1776229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1777229997Sken		       (online != 0) ? "enable" : "disable",
1778229997Sken		       ccb->ccb_h.status);
1779229997Sken	} else {
1780229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1781229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1782229997Sken		       (online != 0) ? "enable" : "disable");
1783229997Sken	}
1784229997Sken
1785229997Sken	xpt_free_path(path);
1786229997Sken
1787244016Sken	free(ccb, M_TEMP);
1788244016Sken
1789229997Sken	return;
1790229997Sken}
1791229997Sken
1792229997Skenstatic void
1793229997Skenctlfe_online(void *arg)
1794229997Sken{
1795245228Sken	struct ctlfe_softc *bus_softc;
1796245228Sken	struct cam_path *path;
1797245228Sken	cam_status status;
1798245228Sken	struct ctlfe_lun_softc *lun_softc;
1799274388Smav	struct cam_periph *periph;
1800245228Sken
1801245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1802245228Sken
1803245228Sken	/*
1804245228Sken	 * Create the wildcard LUN before bringing the port online.
1805245228Sken	 */
1806245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1807245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1808245228Sken				 CAM_LUN_WILDCARD);
1809245228Sken	if (status != CAM_REQ_CMP) {
1810245228Sken		printf("%s: unable to create path for wildcard periph\n",
1811245228Sken				__func__);
1812245228Sken		return;
1813245228Sken	}
1814245228Sken
1815245228Sken	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
1816245228Sken			M_NOWAIT | M_ZERO);
1817245228Sken	if (lun_softc == NULL) {
1818245228Sken		xpt_print(path, "%s: unable to allocate softc for "
1819245228Sken				"wildcard periph\n", __func__);
1820245228Sken		xpt_free_path(path);
1821245228Sken		return;
1822245228Sken	}
1823245228Sken
1824260387Sscottl	xpt_path_lock(path);
1825274388Smav	periph = cam_periph_find(path, "ctl");
1826274388Smav	if (periph != NULL) {
1827274388Smav		/* We've already got a periph, no need to alloc a new one. */
1828274388Smav		xpt_path_unlock(path);
1829274388Smav		xpt_free_path(path);
1830274388Smav		free(lun_softc, M_CTLFE);
1831274388Smav		return;
1832274388Smav	}
1833245228Sken	lun_softc->parent_softc = bus_softc;
1834245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1835245228Sken
1836245228Sken	status = cam_periph_alloc(ctlferegister,
1837245228Sken				  ctlfeoninvalidate,
1838245228Sken				  ctlfecleanup,
1839245228Sken				  ctlfestart,
1840245228Sken				  "ctl",
1841245228Sken				  CAM_PERIPH_BIO,
1842245228Sken				  path,
1843245228Sken				  ctlfeasync,
1844245228Sken				  0,
1845245228Sken				  lun_softc);
1846245228Sken
1847245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1848245228Sken		const struct cam_status_entry *entry;
1849245228Sken
1850245228Sken		entry = cam_fetch_status_entry(status);
1851245228Sken		printf("%s: CAM error %s (%#x) returned from "
1852245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1853245228Sken		       entry->status_text : "Unknown", status);
1854274388Smav		free(lun_softc, M_CTLFE);
1855274388Smav	} else {
1856274388Smav		mtx_lock(&bus_softc->lun_softc_mtx);
1857274388Smav		STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links);
1858274388Smav		mtx_unlock(&bus_softc->lun_softc_mtx);
1859274388Smav		ctlfe_onoffline(arg, /*online*/ 1);
1860245228Sken	}
1861245228Sken
1862260387Sscottl	xpt_path_unlock(path);
1863260387Sscottl	xpt_free_path(path);
1864229997Sken}
1865229997Sken
1866229997Skenstatic void
1867229997Skenctlfe_offline(void *arg)
1868229997Sken{
1869245228Sken	struct ctlfe_softc *bus_softc;
1870245228Sken	struct cam_path *path;
1871245228Sken	cam_status status;
1872245228Sken	struct cam_periph *periph;
1873245228Sken
1874245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1875245228Sken
1876245228Sken	/*
1877245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1878245228Sken	 * the port offline.
1879245228Sken	 */
1880245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1881245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1882245228Sken				 CAM_LUN_WILDCARD);
1883245228Sken	if (status != CAM_REQ_CMP) {
1884245228Sken		printf("%s: unable to create path for wildcard periph\n",
1885245228Sken		       __func__);
1886245228Sken		return;
1887245228Sken	}
1888245228Sken
1889260387Sscottl	xpt_path_lock(path);
1890245228Sken
1891260387Sscottl	ctlfe_onoffline(arg, /*online*/ 0);
1892260387Sscottl
1893245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1894245228Sken		cam_periph_invalidate(periph);
1895245228Sken
1896260387Sscottl	xpt_path_unlock(path);
1897245228Sken	xpt_free_path(path);
1898229997Sken}
1899229997Sken
1900229997Sken/*
1901229997Sken * This will get called to enable a LUN on every bus that is attached to
1902229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1903229997Sken */
1904229997Skenstatic int
1905229997Skenctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1906229997Sken{
1907229997Sken	struct ctlfe_softc *bus_softc;
1908229997Sken	struct ctlfe_lun_softc *softc;
1909229997Sken	struct cam_path *path;
1910229997Sken	struct cam_periph *periph;
1911229997Sken	cam_status status;
1912229997Sken
1913229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1914229997Sken
1915260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1916260387Sscottl				  bus_softc->path_id,
1917260387Sscottl				  targ_id.id, lun_id);
1918229997Sken	/* XXX KDM need some way to return status to CTL here? */
1919229997Sken	if (status != CAM_REQ_CMP) {
1920229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1921229997Sken		       status);
1922229997Sken		return (1);
1923229997Sken	}
1924229997Sken
1925229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1926260387Sscottl	xpt_path_lock(path);
1927229997Sken	periph = cam_periph_find(path, "ctl");
1928229997Sken	if (periph != NULL) {
1929229997Sken		/* We've already got a periph, no need to alloc a new one. */
1930260387Sscottl		xpt_path_unlock(path);
1931229997Sken		xpt_free_path(path);
1932229997Sken		free(softc, M_CTLFE);
1933229997Sken		return (0);
1934229997Sken	}
1935229997Sken	softc->parent_softc = bus_softc;
1936229997Sken
1937229997Sken	status = cam_periph_alloc(ctlferegister,
1938229997Sken				  ctlfeoninvalidate,
1939229997Sken				  ctlfecleanup,
1940229997Sken				  ctlfestart,
1941229997Sken				  "ctl",
1942229997Sken				  CAM_PERIPH_BIO,
1943229997Sken				  path,
1944229997Sken				  ctlfeasync,
1945229997Sken				  0,
1946229997Sken				  softc);
1947229997Sken
1948274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1949274388Smav		const struct cam_status_entry *entry;
1950274388Smav
1951274388Smav		entry = cam_fetch_status_entry(status);
1952274388Smav		printf("%s: CAM error %s (%#x) returned from "
1953274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1954274388Smav		       entry->status_text : "Unknown", status);
1955274388Smav		free(softc, M_CTLFE);
1956274388Smav	} else {
1957274388Smav		mtx_lock(&bus_softc->lun_softc_mtx);
1958274388Smav		STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
1959274388Smav		mtx_unlock(&bus_softc->lun_softc_mtx);
1960274388Smav		ctlfe_onoffline(arg, /*online*/ 1);
1961274388Smav	}
1962274388Smav
1963260387Sscottl	xpt_path_unlock(path);
1964244016Sken	xpt_free_path(path);
1965229997Sken	return (0);
1966229997Sken}
1967229997Sken
1968229997Sken/*
1969245228Sken * This will get called when the user removes a LUN to disable that LUN
1970245228Sken * on every bus that is attached to CTL.
1971229997Sken */
1972229997Skenstatic int
1973229997Skenctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1974229997Sken{
1975229997Sken	struct ctlfe_softc *softc;
1976229997Sken	struct ctlfe_lun_softc *lun_softc;
1977229997Sken
1978229997Sken	softc = (struct ctlfe_softc *)arg;
1979229997Sken
1980260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1981229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1982229997Sken		struct cam_path *path;
1983229997Sken
1984229997Sken		path = lun_softc->periph->path;
1985229997Sken
1986229997Sken		if ((xpt_path_target_id(path) == targ_id.id)
1987229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1988229997Sken			break;
1989229997Sken		}
1990229997Sken	}
1991229997Sken	if (lun_softc == NULL) {
1992260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1993229997Sken		printf("%s: can't find target %d lun %d\n", __func__,
1994229997Sken		       targ_id.id, lun_id);
1995229997Sken		return (1);
1996229997Sken	}
1997260387Sscottl	cam_periph_acquire(lun_softc->periph);
1998260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1999229997Sken
2000260387Sscottl	cam_periph_lock(lun_softc->periph);
2001229997Sken	cam_periph_invalidate(lun_softc->periph);
2002260387Sscottl	cam_periph_unlock(lun_softc->periph);
2003260387Sscottl	cam_periph_release(lun_softc->periph);
2004229997Sken	return (0);
2005229997Sken}
2006229997Sken
2007229997Skenstatic void
2008229997Skenctlfe_dump_sim(struct cam_sim *sim)
2009229997Sken{
2010229997Sken
2011229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
2012229997Sken	       sim->sim_name, sim->unit_number,
2013229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
2014229997Sken	printf("\n");
2015229997Sken}
2016229997Sken
2017229997Sken/*
2018229997Sken * Assumes that the SIM lock is held.
2019229997Sken */
2020229997Skenstatic void
2021229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
2022229997Sken{
2023229997Sken	struct ccb_hdr *hdr;
2024229997Sken	struct cam_periph *periph;
2025229997Sken	int num_items;
2026229997Sken
2027229997Sken	periph = softc->periph;
2028229997Sken	num_items = 0;
2029229997Sken
2030229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
2031229997Sken		union ctl_io *io;
2032229997Sken
2033229997Sken		io = hdr->io_ptr;
2034229997Sken
2035229997Sken		num_items++;
2036229997Sken
2037229997Sken		/*
2038229997Sken		 * This can happen when we get an ATIO but can't allocate
2039229997Sken		 * a ctl_io.  See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
2040229997Sken		 */
2041229997Sken		if (io == NULL) {
2042229997Sken			struct ccb_scsiio *csio;
2043229997Sken
2044229997Sken			csio = (struct ccb_scsiio *)hdr;
2045229997Sken
2046229997Sken			xpt_print(periph->path, "CCB %#x ctl_io allocation "
2047229997Sken				  "failed\n", csio->tag_id);
2048229997Sken			continue;
2049229997Sken		}
2050229997Sken
2051229997Sken		/*
2052229997Sken		 * Only regular SCSI I/O is put on the work
2053229997Sken		 * queue, so we can print sense here.  There may be no
2054229997Sken		 * sense if it's no the queue for a DMA, but this serves to
2055229997Sken		 * print out the CCB as well.
2056229997Sken		 *
2057229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
2058229997Sken		 * CTL is merged in with CAM.
2059229997Sken		 */
2060229997Sken		ctl_io_error_print(io, NULL);
2061229997Sken
2062229997Sken		/*
2063229997Sken		 * We're sending status back to the
2064229997Sken		 * initiator, so we're on the queue waiting
2065229997Sken		 * for a CTIO to do that.
2066229997Sken		 */
2067229997Sken		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2068229997Sken			continue;
2069229997Sken
2070229997Sken		/*
2071229997Sken		 * Otherwise, we're on the queue waiting to
2072229997Sken		 * do a data transfer.
2073229997Sken		 */
2074229997Sken		xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
2075229997Sken			  io->scsiio.kern_total_len, io->scsiio.kern_data_len,
2076229997Sken			  io->scsiio.kern_data_resid);
2077229997Sken	}
2078229997Sken
2079229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
2080229997Sken		  num_items);
2081250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
2082229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
2083229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
2084229997Sken		  (uintmax_t)softc->ccbs_freed);
2085229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
2086229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
2087229997Sken		  softc->ctios_returned), softc->ctios_sent,
2088229997Sken		  softc->ctios_returned);
2089229997Sken}
2090229997Sken
2091229997Sken/*
2092229997Sken * This function is called when we fail to get a CCB for a DMA or status return
2093229997Sken * to the initiator within the specified time period.
2094229997Sken *
2095229997Sken * The callout code should insure that we hold the sim mutex here.
2096229997Sken */
2097229997Skenstatic void
2098229997Skenctlfe_dma_timeout(void *arg)
2099229997Sken{
2100229997Sken	struct ctlfe_lun_softc *softc;
2101229997Sken	struct cam_periph *periph;
2102229997Sken	struct cam_sim *sim;
2103229997Sken	int num_queued;
2104229997Sken
2105229997Sken	softc = (struct ctlfe_lun_softc *)arg;
2106229997Sken	periph = softc->periph;
2107229997Sken	sim = xpt_path_sim(periph->path);
2108229997Sken	num_queued = 0;
2109229997Sken
2110229997Sken	/*
2111229997Sken	 * Nothing to do...
2112229997Sken	 */
2113229997Sken	if (TAILQ_FIRST(&softc->work_queue) == NULL) {
2114229997Sken		xpt_print(periph->path, "TIMEOUT triggered after %d "
2115229997Sken			  "seconds, but nothing on work queue??\n",
2116229997Sken			  CTLFE_DMA_TIMEOUT);
2117229997Sken		return;
2118229997Sken	}
2119229997Sken
2120229997Sken	xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
2121229997Sken		  "start\n", CTLFE_DMA_TIMEOUT);
2122229997Sken
2123229997Sken	ctlfe_dump_queue(softc);
2124229997Sken
2125229997Sken	ctlfe_dump_sim(sim);
2126229997Sken
2127229997Sken	xpt_print(periph->path, "calling xpt_schedule() to attempt to "
2128229997Sken		  "unstick our queue\n");
2129229997Sken
2130229997Sken	xpt_schedule(periph, /*priority*/ 1);
2131229997Sken
2132229997Sken	xpt_print(periph->path, "xpt_schedule() call complete\n");
2133229997Sken}
2134229997Sken
2135229997Sken/*
2136229997Sken * Datamove/done routine called by CTL.  Put ourselves on the queue to
2137229997Sken * receive a CCB from CAM so we can queue the continue I/O request down
2138229997Sken * to the adapter.
2139229997Sken */
2140229997Skenstatic void
2141229997Skenctlfe_datamove_done(union ctl_io *io)
2142229997Sken{
2143229997Sken	union ccb *ccb;
2144229997Sken	struct cam_periph *periph;
2145229997Sken	struct ctlfe_lun_softc *softc;
2146229997Sken
2147229997Sken	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2148229997Sken
2149229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
2150260387Sscottl	cam_periph_lock(periph);
2151229997Sken
2152229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
2153229997Sken
2154229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
2155229997Sken		/*
2156229997Sken		 * Task management commands don't require any further
2157229997Sken		 * communication back to the adapter.  Requeue the CCB
2158229997Sken		 * to the adapter, and free the CTL I/O.
2159229997Sken		 */
2160229997Sken		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2161229997Sken			  "tag %#x seq %#x\n", __func__,
2162229997Sken			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2163229997Sken		/*
2164229997Sken		 * Send the notify acknowledge down to the SIM, to let it
2165229997Sken		 * know we processed the task management command.
2166229997Sken		 */
2167229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
2168229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2169229997Sken		xpt_action(ccb);
2170229997Sken		ctl_free_io(io);
2171229997Sken	} else {
2172229997Sken		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2173229997Sken			io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2174229997Sken		else
2175229997Sken			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2176229997Sken
2177229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2178229997Sken				  periph_links.tqe);
2179229997Sken
2180229997Sken		/*
2181229997Sken		 * Reset the timeout for our latest active DMA.
2182229997Sken		 */
2183229997Sken		callout_reset(&softc->dma_callout,
2184229997Sken			      CTLFE_DMA_TIMEOUT * hz,
2185229997Sken			      ctlfe_dma_timeout, softc);
2186229997Sken		/*
2187229997Sken		 * Ask for the CAM transport layer to send us a CCB to do
2188229997Sken		 * the DMA or send status, unless ctlfe_dma_enabled is set
2189229997Sken		 * to 0.
2190229997Sken		 */
2191229997Sken		if (ctlfe_dma_enabled != 0)
2192229997Sken			xpt_schedule(periph, /*priority*/ 1);
2193229997Sken	}
2194229997Sken
2195260387Sscottl	cam_periph_unlock(periph);
2196229997Sken}
2197229997Sken
2198229997Skenstatic void
2199229997Skenctlfe_dump(void)
2200229997Sken{
2201229997Sken	struct ctlfe_softc *bus_softc;
2202229997Sken
2203229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2204229997Sken		struct ctlfe_lun_softc *lun_softc;
2205229997Sken
2206229997Sken		ctlfe_dump_sim(bus_softc->sim);
2207229997Sken
2208229997Sken		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
2209229997Sken			ctlfe_dump_queue(lun_softc);
2210229997Sken		}
2211229997Sken	}
2212229997Sken}
2213