scsi_ctl.c revision 314737
1229997Sken/*-
2229997Sken * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3290776Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
4229997Sken * All rights reserved.
5229997Sken *
6229997Sken * Redistribution and use in source and binary forms, with or without
7229997Sken * modification, are permitted provided that the following conditions
8229997Sken * are met:
9229997Sken * 1. Redistributions of source code must retain the above copyright
10229997Sken *    notice, this list of conditions, and the following disclaimer,
11229997Sken *    without modification.
12229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
14229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
15229997Sken *    including a substantially similar Disclaimer requirement for further
16229997Sken *    binary redistribution.
17229997Sken *
18229997Sken * NO WARRANTY
19229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29229997Sken * POSSIBILITY OF SUCH DAMAGES.
30229997Sken *
31229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
32229997Sken */
33229997Sken/*
34229997Sken * Peripheral driver interface between CAM and CTL (CAM Target Layer).
35229997Sken *
36229997Sken * Author: Ken Merry <ken@FreeBSD.org>
37229997Sken */
38229997Sken
39229997Sken#include <sys/cdefs.h>
40229997Sken__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/scsi_ctl.c 314737 2017-03-06 06:24:53Z mav $");
41229997Sken
42229997Sken#include <sys/param.h>
43229997Sken#include <sys/queue.h>
44229997Sken#include <sys/systm.h>
45229997Sken#include <sys/kernel.h>
46229997Sken#include <sys/lock.h>
47229997Sken#include <sys/mutex.h>
48229997Sken#include <sys/condvar.h>
49229997Sken#include <sys/malloc.h>
50229997Sken#include <sys/bus.h>
51229997Sken#include <sys/endian.h>
52229997Sken#include <sys/sbuf.h>
53229997Sken#include <sys/sysctl.h>
54229997Sken#include <sys/types.h>
55229997Sken#include <sys/systm.h>
56229997Sken#include <machine/bus.h>
57229997Sken
58229997Sken#include <cam/cam.h>
59229997Sken#include <cam/cam_ccb.h>
60229997Sken#include <cam/cam_periph.h>
61229997Sken#include <cam/cam_queue.h>
62229997Sken#include <cam/cam_xpt_periph.h>
63229997Sken#include <cam/cam_debug.h>
64229997Sken#include <cam/cam_sim.h>
65229997Sken#include <cam/cam_xpt.h>
66229997Sken
67229997Sken#include <cam/scsi/scsi_all.h>
68229997Sken#include <cam/scsi/scsi_message.h>
69229997Sken
70229997Sken#include <cam/ctl/ctl_io.h>
71229997Sken#include <cam/ctl/ctl.h>
72229997Sken#include <cam/ctl/ctl_frontend.h>
73229997Sken#include <cam/ctl/ctl_util.h>
74229997Sken#include <cam/ctl/ctl_error.h>
75229997Sken
76229997Skenstruct ctlfe_softc {
77290776Smav	struct ctl_port	port;
78290776Smav	path_id_t	path_id;
79290776Smav	target_id_t	target_id;
80290776Smav	uint32_t	hba_misc;
81290776Smav	u_int		maxio;
82229997Sken	struct cam_sim *sim;
83290776Smav	char		port_name[DEV_IDLEN];
84290776Smav	struct mtx	lun_softc_mtx;
85229997Sken	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
86229997Sken	STAILQ_ENTRY(ctlfe_softc) links;
87229997Sken};
88229997Sken
89229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
90229997Skenstruct mtx ctlfe_list_mtx;
91229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist";
92229997Sken#ifdef CTLFE_INIT_ENABLE
93229997Skenstatic int ctlfe_max_targets = 1;
94229997Skenstatic int ctlfe_num_targets = 0;
95229997Sken#endif
96229997Sken
97229997Skentypedef enum {
98229997Sken	CTLFE_LUN_NONE		= 0x00,
99229997Sken	CTLFE_LUN_WILDCARD	= 0x01
100229997Sken} ctlfe_lun_flags;
101229997Sken
102229997Skenstruct ctlfe_lun_softc {
103229997Sken	struct ctlfe_softc *parent_softc;
104229997Sken	struct cam_periph *periph;
105229997Sken	ctlfe_lun_flags flags;
106229997Sken	uint64_t ccbs_alloced;
107229997Sken	uint64_t ccbs_freed;
108229997Sken	uint64_t ctios_sent;
109229997Sken	uint64_t ctios_returned;
110284793Smav	uint64_t atios_alloced;
111284793Smav	uint64_t atios_freed;
112284793Smav	uint64_t inots_alloced;
113284793Smav	uint64_t inots_freed;
114229997Sken	/* bus_dma_tag_t dma_tag; */
115229997Sken	TAILQ_HEAD(, ccb_hdr) work_queue;
116229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
117229997Sken};
118229997Sken
119229997Skentypedef enum {
120229997Sken	CTLFE_CMD_NONE		= 0x00,
121229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
122229997Sken} ctlfe_cmd_flags;
123229997Sken
124288723Smavstruct ctlfe_cmd_info {
125229997Sken	int cur_transfer_index;
126265641Smav	size_t cur_transfer_off;
127229997Sken	ctlfe_cmd_flags flags;
128229997Sken	/*
129229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
130229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
131229997Sken	 * i386 and 512 bytes on amd64.
132229997Sken	 */
133265641Smav#define CTLFE_MAX_SEGS	32
134265641Smav	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
135229997Sken};
136229997Sken
137229997Sken/*
138229997Sken * When we register the adapter/bus, request that this many ctl_ios be
139229997Sken * allocated.  This should be the maximum supported by the adapter, but we
140229997Sken * currently don't have a way to get that back from the path inquiry.
141229997Sken * XXX KDM add that to the path inquiry.
142229997Sken */
143229997Sken#define	CTLFE_REQ_CTL_IO	4096
144229997Sken/*
145229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
146229997Sken * adapter per LUN.
147229997Sken * XXX KDM should this be controlled by CTL?
148229997Sken */
149229997Sken#define	CTLFE_ATIO_PER_LUN	1024
150229997Sken/*
151229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
152229997Sken * allocate and queue down to the adapter per LUN.
153229997Sken * XXX KDM should this be controlled by CTL?
154229997Sken */
155229997Sken#define	CTLFE_IN_PER_LUN	1024
156229997Sken
157229997Sken/*
158229997Sken * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
159229997Sken * status to the initiator.  The SIM is expected to have its own timeouts,
160229997Sken * so we're not putting this timeout around the CCB execution time.  The
161229997Sken * SIM should timeout and let us know if it has an issue.
162229997Sken */
163229997Sken#define	CTLFE_DMA_TIMEOUT	60
164229997Sken
165229997Sken/*
166229997Sken * Turn this on to enable extra debugging prints.
167229997Sken */
168229997Sken#if 0
169229997Sken#define	CTLFE_DEBUG
170229997Sken#endif
171229997Sken
172229997Sken/*
173229997Sken * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
174229997Sken * in the FreeBSD initiator that makes it unable to rescan the target if
175229997Sken * the target gets rebooted and the WWNN/WWPN stay the same.
176229997Sken */
177229997Sken#if 0
178229997Sken#define	RANDOM_WWNN
179229997Sken#endif
180229997Sken
181229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
182229997Sken
183275878Smav#define	io_ptr		ppriv_ptr0
184229997Sken
185229997Sken/* This is only used in the CTIO */
186229997Sken#define	ccb_atio	ppriv_ptr1
187229997Sken
188312585Smav#define PRIV_CCB(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
189312585Smav#define PRIV_INFO(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
190312585Smav
191313369Smavstatic int		ctlfeinitialize(void);
192313369Smavstatic int		ctlfeshutdown(void);
193268677Smavstatic periph_init_t	ctlfeperiphinit;
194229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
195229997Sken				   struct cam_path *path, void *arg);
196229997Skenstatic periph_ctor_t	ctlferegister;
197229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
198229997Skenstatic periph_dtor_t	ctlfecleanup;
199229997Skenstatic periph_start_t	ctlfestart;
200229997Skenstatic void		ctlfedone(struct cam_periph *periph,
201229997Sken				  union ccb *done_ccb);
202229997Sken
203229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
204229997Skenstatic void 		ctlfe_online(void *arg);
205229997Skenstatic void 		ctlfe_offline(void *arg);
206284798Smavstatic int 		ctlfe_lun_enable(void *arg, int lun_id);
207284798Smavstatic int 		ctlfe_lun_disable(void *arg, int lun_id);
208229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
209229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
210275880Smavstatic void 		ctlfe_datamove(union ctl_io *io);
211275880Smavstatic void 		ctlfe_done(union ctl_io *io);
212229997Skenstatic void 		ctlfe_dump(void);
213229997Sken
214229997Skenstatic struct periph_driver ctlfe_driver =
215229997Sken{
216268677Smav	ctlfeperiphinit, "ctl",
217273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
218273316Smav	CAM_PERIPH_DRV_EARLY
219229997Sken};
220229997Sken
221268677Smavstatic struct ctl_frontend ctlfe_frontend =
222268677Smav{
223273318Smav	.name = "camtgt",
224268677Smav	.init = ctlfeinitialize,
225268677Smav	.fe_dump = ctlfe_dump,
226268677Smav	.shutdown = ctlfeshutdown,
227249009Strasz};
228268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
229249009Strasz
230313369Smavstatic int
231229997Skenctlfeshutdown(void)
232229997Sken{
233313369Smav
234313369Smav	/* CAM does not support periph driver unregister now. */
235313369Smav	return (EBUSY);
236229997Sken}
237229997Sken
238313369Smavstatic int
239268677Smavctlfeinitialize(void)
240229997Sken{
241229997Sken
242229997Sken	STAILQ_INIT(&ctlfe_softc_list);
243229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
244268677Smav	periphdriver_register(&ctlfe_driver);
245268677Smav	return (0);
246268677Smav}
247229997Sken
248313369Smavstatic void
249268677Smavctlfeperiphinit(void)
250268677Smav{
251268677Smav	cam_status status;
252229997Sken
253229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
254229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
255229997Sken	if (status != CAM_REQ_CMP) {
256229997Sken		printf("ctl: Failed to attach async callback due to CAM "
257229997Sken		       "status 0x%x!\n", status);
258229997Sken	}
259229997Sken}
260229997Sken
261229997Skenstatic void
262229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
263229997Sken{
264273317Smav	struct ctlfe_softc *softc;
265229997Sken
266229997Sken#ifdef CTLFEDEBUG
267229997Sken	printf("%s: entered\n", __func__);
268229997Sken#endif
269229997Sken
270273317Smav	mtx_lock(&ctlfe_list_mtx);
271273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
272273317Smav		if (softc->path_id == xpt_path_path_id(path))
273273317Smav			break;
274273317Smav	}
275273317Smav	mtx_unlock(&ctlfe_list_mtx);
276273317Smav
277229997Sken	/*
278229997Sken	 * When a new path gets registered, and it is capable of target
279229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
280229997Sken	 * selective, but for now this will be sufficient.
281229997Sken 	 */
282229997Sken	switch (code) {
283229997Sken	case AC_PATH_REGISTERED: {
284268677Smav		struct ctl_port *port;
285229997Sken		struct ccb_pathinq *cpi;
286229997Sken		int retval;
287229997Sken
288229997Sken		cpi = (struct ccb_pathinq *)arg;
289229997Sken
290229997Sken		/* Don't attach if it doesn't support target mode */
291229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
292230033Sken#ifdef CTLFEDEBUG
293229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
294229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
295230033Sken#endif
296229997Sken			break;
297229997Sken		}
298229997Sken
299273317Smav		if (softc != NULL) {
300273317Smav#ifdef CTLFEDEBUG
301273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
302273317Smav			       __func__, xpt_path_path_id(path));
303273317Smav#endif
304273317Smav			break;
305273317Smav		}
306273317Smav
307229997Sken#ifdef CTLFE_INIT_ENABLE
308229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
309229997Sken			union ccb *ccb;
310229997Sken
311229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
312229997Sken						  M_NOWAIT | M_ZERO);
313229997Sken			if (ccb == NULL) {
314229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
315229997Sken				return;
316229997Sken			}
317260387Sscottl			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
318229997Sken
319229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
320229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
321229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
322229997Sken
323229997Sken			xpt_action(ccb);
324229997Sken
325229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
326229997Sken			     CAM_REQ_CMP) {
327229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
328229997Sken				       "enable failed with status %#x\n",
329229997Sken				       __func__, cpi->dev_name,
330229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
331229997Sken				       ccb->ccb_h.status);
332229997Sken			} else {
333229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
334229997Sken				       "enable succeeded\n",
335229997Sken				       __func__, cpi->dev_name,
336229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
337229997Sken			}
338229997Sken
339229997Sken			free(ccb, M_TEMP);
340229997Sken
341229997Sken			break;
342229997Sken		} else {
343229997Sken			ctlfe_num_targets++;
344229997Sken		}
345229997Sken
346229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
347229997Sken		       ctlfe_num_targets);
348229997Sken#endif /* CTLFE_INIT_ENABLE */
349229997Sken
350229997Sken		/*
351229997Sken		 * We're in an interrupt context here, so we have to
352229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
353229997Sken		 * can't allocate memory.
354229997Sken		 */
355273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
356273317Smav		if (softc == NULL) {
357229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
358273317Smav			       __func__, sizeof(*softc));
359229997Sken			return;
360229997Sken		}
361229997Sken
362273317Smav		softc->path_id = cpi->ccb_h.path_id;
363288713Smav		softc->target_id = cpi->initiator_id;
364273317Smav		softc->sim = xpt_path_sim(path);
365290776Smav		softc->hba_misc = cpi->hba_misc;
366265641Smav		if (cpi->maxio != 0)
367273317Smav			softc->maxio = cpi->maxio;
368265641Smav		else
369273317Smav			softc->maxio = DFLTPHYS;
370273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
371273317Smav		STAILQ_INIT(&softc->lun_softc_list);
372229997Sken
373273317Smav		port = &softc->port;
374268677Smav		port->frontend = &ctlfe_frontend;
375229997Sken
376229997Sken		/*
377229997Sken		 * XXX KDM should we be more accurate here ?
378229997Sken		 */
379229997Sken		if (cpi->transport == XPORT_FC)
380268677Smav			port->port_type = CTL_PORT_FC;
381268694Smav		else if (cpi->transport == XPORT_SAS)
382268694Smav			port->port_type = CTL_PORT_SAS;
383229997Sken		else
384268677Smav			port->port_type = CTL_PORT_SCSI;
385229997Sken
386229997Sken		/* XXX KDM what should the real number be here? */
387268677Smav		port->num_requested_ctl_io = 4096;
388273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
389229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
390229997Sken		/*
391229997Sken		 * XXX KDM it would be nice to allocate storage in the
392229997Sken		 * frontend structure itself.
393229997Sken	 	 */
394273317Smav		port->port_name = softc->port_name;
395273319Smav		port->physical_port = cpi->bus_id;
396273319Smav		port->virtual_port = 0;
397268677Smav		port->port_online = ctlfe_online;
398268677Smav		port->port_offline = ctlfe_offline;
399273317Smav		port->onoff_arg = softc;
400268677Smav		port->lun_enable = ctlfe_lun_enable;
401268677Smav		port->lun_disable = ctlfe_lun_disable;
402273317Smav		port->targ_lun_arg = softc;
403275880Smav		port->fe_datamove = ctlfe_datamove;
404275880Smav		port->fe_done = ctlfe_done;
405229997Sken		/*
406229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
407229997Sken		 * number of targets supported.
408229997Sken		 */
409268677Smav		port->max_targets = cpi->max_target;
410268677Smav		port->max_target_id = cpi->max_target;
411288732Smav		port->targ_port = -1;
412229997Sken
413229997Sken		/*
414229997Sken		 * XXX KDM need to figure out whether we're the master or
415229997Sken		 * slave.
416229997Sken		 */
417230033Sken#ifdef CTLFEDEBUG
418268677Smav		printf("%s: calling ctl_port_register() for %s%d\n",
419229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
420230033Sken#endif
421275493Smav		retval = ctl_port_register(port);
422229997Sken		if (retval != 0) {
423268677Smav			printf("%s: ctl_port_register() failed with "
424229997Sken			       "error %d!\n", __func__, retval);
425273317Smav			mtx_destroy(&softc->lun_softc_mtx);
426273317Smav			free(softc, M_CTLFE);
427229997Sken			break;
428229997Sken		} else {
429229997Sken			mtx_lock(&ctlfe_list_mtx);
430273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
431229997Sken			mtx_unlock(&ctlfe_list_mtx);
432229997Sken		}
433229997Sken
434245228Sken		break;
435245228Sken	}
436245228Sken	case AC_PATH_DEREGISTERED: {
437245228Sken
438245228Sken		if (softc != NULL) {
439245228Sken			/*
440245228Sken			 * XXX KDM are we certain at this point that there
441245228Sken			 * are no outstanding commands for this frontend?
442245228Sken			 */
443273317Smav			mtx_lock(&ctlfe_list_mtx);
444273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
445273317Smav			    links);
446273317Smav			mtx_unlock(&ctlfe_list_mtx);
447268677Smav			ctl_port_deregister(&softc->port);
448260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
449245228Sken			free(softc, M_CTLFE);
450229997Sken		}
451229997Sken		break;
452229997Sken	}
453229997Sken	case AC_CONTRACT: {
454229997Sken		struct ac_contract *ac;
455229997Sken
456229997Sken		ac = (struct ac_contract *)arg;
457229997Sken
458229997Sken		switch (ac->contract_number) {
459229997Sken		case AC_CONTRACT_DEV_CHG: {
460229997Sken			struct ac_device_changed *dev_chg;
461273317Smav			int retval;
462229997Sken
463229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
464229997Sken
465236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
466229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
467229997Sken			       xpt_path_path_id(path), dev_chg->target,
468229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
469229997Sken
470273317Smav			if (softc == NULL) {
471229997Sken				printf("%s: CTL port for CAM path %u not "
472229997Sken				       "found!\n", __func__,
473229997Sken				       xpt_path_path_id(path));
474229997Sken				break;
475229997Sken			}
476229997Sken			if (dev_chg->arrived != 0) {
477268692Smav				retval = ctl_add_initiator(&softc->port,
478268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
479229997Sken			} else {
480268692Smav				retval = ctl_remove_initiator(&softc->port,
481268692Smav				    dev_chg->target);
482229997Sken			}
483229997Sken
484268692Smav			if (retval < 0) {
485229997Sken				printf("%s: could not %s port %d iid %u "
486229997Sken				       "WWPN %#jx!\n", __func__,
487229997Sken				       (dev_chg->arrived != 0) ? "add" :
488268677Smav				       "remove", softc->port.targ_port,
489229997Sken				       dev_chg->target,
490229997Sken				       (uintmax_t)dev_chg->wwpn);
491229997Sken			}
492229997Sken			break;
493229997Sken		}
494229997Sken		default:
495229997Sken			printf("%s: unsupported contract number %ju\n",
496229997Sken			       __func__, (uintmax_t)ac->contract_number);
497229997Sken			break;
498229997Sken		}
499229997Sken		break;
500229997Sken	}
501229997Sken	default:
502229997Sken		break;
503229997Sken	}
504229997Sken}
505229997Sken
506229997Skenstatic cam_status
507229997Skenctlferegister(struct cam_periph *periph, void *arg)
508229997Sken{
509229997Sken	struct ctlfe_softc *bus_softc;
510229997Sken	struct ctlfe_lun_softc *softc;
511229997Sken	union ccb en_lun_ccb;
512229997Sken	cam_status status;
513229997Sken	int i;
514229997Sken
515229997Sken	softc = (struct ctlfe_lun_softc *)arg;
516229997Sken	bus_softc = softc->parent_softc;
517229997Sken
518229997Sken	TAILQ_INIT(&softc->work_queue);
519229997Sken	softc->periph = periph;
520229997Sken	periph->softc = softc;
521229997Sken
522242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
523229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
524229997Sken	en_lun_ccb.cel.grp6_len = 0;
525229997Sken	en_lun_ccb.cel.grp7_len = 0;
526229997Sken	en_lun_ccb.cel.enable = 1;
527229997Sken	xpt_action(&en_lun_ccb);
528229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
529229997Sken	if (status != CAM_REQ_CMP) {
530229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
531229997Sken			  __func__, en_lun_ccb.ccb_h.status);
532229997Sken		return (status);
533229997Sken	}
534229997Sken
535229997Sken	status = CAM_REQ_CMP;
536229997Sken
537229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
538229997Sken		union ccb *new_ccb;
539275878Smav		union ctl_io *new_io;
540288723Smav		struct ctlfe_cmd_info *cmd_info;
541229997Sken
542229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
543236426Smjacob					      M_ZERO|M_NOWAIT);
544229997Sken		if (new_ccb == NULL) {
545229997Sken			status = CAM_RESRC_UNAVAIL;
546229997Sken			break;
547229997Sken		}
548275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
549275878Smav		if (new_io == NULL) {
550275878Smav			free(new_ccb, M_CTLFE);
551275878Smav			status = CAM_RESRC_UNAVAIL;
552275878Smav			break;
553275878Smav		}
554288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
555288723Smav		    M_ZERO | M_NOWAIT);
556288723Smav		if (cmd_info == NULL) {
557288723Smav			ctl_free_io(new_io);
558288723Smav			free(new_ccb, M_CTLFE);
559288723Smav			status = CAM_RESRC_UNAVAIL;
560288723Smav			break;
561288723Smav		}
562312585Smav		PRIV_INFO(new_io) = cmd_info;
563284793Smav		softc->atios_alloced++;
564275878Smav		new_ccb->ccb_h.io_ptr = new_io;
565275878Smav
566229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
567229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
568229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
569260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
570229997Sken		xpt_action(new_ccb);
571229997Sken		status = new_ccb->ccb_h.status;
572229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
573288723Smav			free(cmd_info, M_CTLFE);
574275878Smav			ctl_free_io(new_io);
575229997Sken			free(new_ccb, M_CTLFE);
576229997Sken			break;
577229997Sken		}
578229997Sken	}
579229997Sken
580229997Sken	status = cam_periph_acquire(periph);
581229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
582229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
583229997Sken			  "count, status = %#x\n", __func__, status);
584229997Sken		return (status);
585229997Sken	}
586229997Sken
587229997Sken	if (i == 0) {
588229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
589229997Sken			  "status 0x%x\n", __func__, status);
590229997Sken		return (CAM_REQ_CMP_ERR);
591229997Sken	}
592229997Sken
593229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
594229997Sken		union ccb *new_ccb;
595275878Smav		union ctl_io *new_io;
596229997Sken
597229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
598236426Smjacob					      M_ZERO|M_NOWAIT);
599229997Sken		if (new_ccb == NULL) {
600229997Sken			status = CAM_RESRC_UNAVAIL;
601229997Sken			break;
602229997Sken		}
603275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
604275878Smav		if (new_io == NULL) {
605275878Smav			free(new_ccb, M_CTLFE);
606275878Smav			status = CAM_RESRC_UNAVAIL;
607275878Smav			break;
608275878Smav		}
609284793Smav		softc->inots_alloced++;
610275878Smav		new_ccb->ccb_h.io_ptr = new_io;
611229997Sken
612229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
613229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
614229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
615260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
616229997Sken		xpt_action(new_ccb);
617229997Sken		status = new_ccb->ccb_h.status;
618237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
619237601Sken			/*
620237601Sken			 * Note that we don't free the CCB here.  If the
621237601Sken			 * status is not CAM_REQ_INPROG, then we're
622237601Sken			 * probably talking to a SIM that says it is
623237601Sken			 * target-capable but doesn't support the
624237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
625237601Sken			 * older API.  In that case, it'll call xpt_done()
626237601Sken			 * on the CCB, and we need to free it in our done
627237601Sken			 * routine as a result.
628237601Sken			 */
629229997Sken			break;
630229997Sken		}
631229997Sken	}
632237601Sken	if ((i == 0)
633237601Sken	 || (status != CAM_REQ_INPROG)) {
634229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
635229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
636229997Sken		return (CAM_REQ_CMP_ERR);
637229997Sken	}
638275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
639275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
640275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
641229997Sken	return (CAM_REQ_CMP);
642229997Sken}
643229997Sken
644229997Skenstatic void
645229997Skenctlfeoninvalidate(struct cam_periph *periph)
646229997Sken{
647229997Sken	union ccb en_lun_ccb;
648229997Sken	cam_status status;
649260387Sscottl	struct ctlfe_softc *bus_softc;
650229997Sken	struct ctlfe_lun_softc *softc;
651229997Sken
652229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
653229997Sken
654242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
655229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
656229997Sken	en_lun_ccb.cel.grp6_len = 0;
657229997Sken	en_lun_ccb.cel.grp7_len = 0;
658229997Sken	en_lun_ccb.cel.enable = 0;
659229997Sken	xpt_action(&en_lun_ccb);
660229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
661229997Sken	if (status != CAM_REQ_CMP) {
662229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
663229997Sken			  __func__, en_lun_ccb.ccb_h.status);
664229997Sken		/*
665229997Sken		 * XXX KDM what do we do now?
666229997Sken		 */
667229997Sken	}
668260387Sscottl
669260387Sscottl	bus_softc = softc->parent_softc;
670260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
671260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
672260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
673229997Sken}
674229997Sken
675229997Skenstatic void
676229997Skenctlfecleanup(struct cam_periph *periph)
677229997Sken{
678229997Sken	struct ctlfe_lun_softc *softc;
679229997Sken
680229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
681229997Sken
682284793Smav	KASSERT(softc->ccbs_freed == softc->ccbs_alloced, ("%s: "
683284793Smav		"ccbs_freed %ju != ccbs_alloced %ju", __func__,
684284793Smav		softc->ccbs_freed, softc->ccbs_alloced));
685284793Smav	KASSERT(softc->ctios_returned == softc->ctios_sent, ("%s: "
686284793Smav		"ctios_returned %ju != ctios_sent %ju", __func__,
687284793Smav		softc->ctios_returned, softc->ctios_sent));
688284793Smav	KASSERT(softc->atios_freed == softc->atios_alloced, ("%s: "
689284793Smav		"atios_freed %ju != atios_alloced %ju", __func__,
690284793Smav		softc->atios_freed, softc->atios_alloced));
691284793Smav	KASSERT(softc->inots_freed == softc->inots_alloced, ("%s: "
692284793Smav		"inots_freed %ju != inots_alloced %ju", __func__,
693284793Smav		softc->inots_freed, softc->inots_alloced));
694245228Sken
695229997Sken	free(softc, M_CTLFE);
696229997Sken}
697229997Sken
698229997Skenstatic void
699265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
700265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
701265641Smav    u_int16_t *sglist_cnt)
702265641Smav{
703265641Smav	struct ctlfe_softc *bus_softc;
704288723Smav	struct ctlfe_cmd_info *cmd_info;
705265641Smav	struct ctl_sg_entry *ctl_sglist;
706265641Smav	bus_dma_segment_t *cam_sglist;
707265641Smav	size_t off;
708265641Smav	int i, idx;
709265641Smav
710312585Smav	cmd_info = PRIV_INFO(io);
711265641Smav	bus_softc = softc->parent_softc;
712265641Smav
713265641Smav	/*
714265641Smav	 * Set the direction, relative to the initiator.
715265641Smav	 */
716265641Smav	*flags &= ~CAM_DIR_MASK;
717265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
718265641Smav		*flags |= CAM_DIR_IN;
719265641Smav	else
720265641Smav		*flags |= CAM_DIR_OUT;
721265641Smav
722265641Smav	*flags &= ~CAM_DATA_MASK;
723265641Smav	idx = cmd_info->cur_transfer_index;
724265641Smav	off = cmd_info->cur_transfer_off;
725265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
726313365Smav	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
727313365Smav
728313365Smav		/* One time shift for SRR offset. */
729313365Smav		off += io->scsiio.ext_data_filled;
730313365Smav		io->scsiio.ext_data_filled = 0;
731313365Smav
732265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
733265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
734265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
735265641Smav		} else {
736265641Smav			*dxfer_len = bus_softc->maxio;
737313365Smav			cmd_info->cur_transfer_off += bus_softc->maxio;
738265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
739265641Smav		}
740265641Smav		*sglist_cnt = 0;
741265641Smav
742265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
743265641Smav			*flags |= CAM_DATA_PADDR;
744265641Smav		else
745265641Smav			*flags |= CAM_DATA_VADDR;
746313365Smav	} else {	/* S/G list with physical or virtual pointers. */
747265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
748313365Smav
749313365Smav		/* One time shift for SRR offset. */
750313365Smav		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
751313365Smav			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
752313365Smav			idx++;
753313365Smav			off = 0;
754313365Smav		}
755313365Smav		off += io->scsiio.ext_data_filled;
756313365Smav		io->scsiio.ext_data_filled = 0;
757313365Smav
758265641Smav		cam_sglist = cmd_info->cam_sglist;
759265641Smav		*dxfer_len = 0;
760265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
761265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
762265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
763265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
764265641Smav				*dxfer_len += cam_sglist[i].ds_len;
765265641Smav			} else {
766265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
767265641Smav				cmd_info->cur_transfer_index = idx + i;
768265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
769265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
770265641Smav				*dxfer_len += cam_sglist[i].ds_len;
771265641Smav				if (ctl_sglist[i].len != 0)
772265641Smav					i++;
773265641Smav				break;
774265641Smav			}
775265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
776265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
777265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
778265641Smav				cmd_info->cur_transfer_off = 0;
779265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
780265641Smav				i++;
781265641Smav				break;
782265641Smav			}
783265641Smav			off = 0;
784265641Smav		}
785265641Smav		*sglist_cnt = i;
786265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
787265641Smav			*flags |= CAM_DATA_SG_PADDR;
788265641Smav		else
789265641Smav			*flags |= CAM_DATA_SG;
790265641Smav		*data_ptr = (uint8_t *)cam_sglist;
791265641Smav	}
792265641Smav}
793265641Smav
794265641Smavstatic void
795229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
796229997Sken{
797229997Sken	struct ctlfe_lun_softc *softc;
798288723Smav	struct ctlfe_cmd_info *cmd_info;
799229997Sken	struct ccb_hdr *ccb_h;
800275880Smav	struct ccb_accept_tio *atio;
801275880Smav	struct ccb_scsiio *csio;
802275880Smav	uint8_t *data_ptr;
803275880Smav	uint32_t dxfer_len;
804275880Smav	ccb_flags flags;
805275880Smav	union ctl_io *io;
806275880Smav	uint8_t scsi_status;
807229997Sken
808229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
809229997Sken	softc->ccbs_alloced++;
810229997Sken
811229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
812260387Sscottl	if (ccb_h == NULL) {
813229997Sken		softc->ccbs_freed++;
814229997Sken		xpt_release_ccb(start_ccb);
815275880Smav		return;
816275880Smav	}
817229997Sken
818275880Smav	/* Take the ATIO off the work queue */
819275880Smav	TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
820275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
821275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
822275880Smav	csio = &start_ccb->csio;
823229997Sken
824275880Smav	flags = atio->ccb_h.flags &
825275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
826312585Smav	cmd_info = PRIV_INFO(io);
827275881Smav	cmd_info->cur_transfer_index = 0;
828275881Smav	cmd_info->cur_transfer_off = 0;
829275881Smav	cmd_info->flags = 0;
830229997Sken
831275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
832275880Smav		/*
833275880Smav		 * Datamove call, we need to setup the S/G list.
834275880Smav		 */
835275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
836275880Smav		    &csio->sglist_cnt);
837275880Smav	} else {
838275880Smav		/*
839275880Smav		 * We're done, send status back.
840275880Smav		 */
841275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
842275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
843275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
844275880Smav
845314737Smav			/* Tell the SIM that we've aborted this ATIO */
846314737Smav#ifdef CTLFEDEBUG
847314737Smav			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
848314737Smav#endif
849314737Smav			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
850314737Smav			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
851275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
852275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
853275880Smav			xpt_action(start_ccb);
854275880Smav			softc->ccbs_freed++;
855275880Smav			xpt_release_ccb(start_ccb);
856229997Sken
857229997Sken			/*
858275880Smav			 * Send the ATIO back down to the SIM.
859314737Smav			 * For a wildcard attachment, commands can come in
860314737Smav			 * with a specific target/lun.  Reset the target and
861314737Smav			 * LUN fields back to the wildcard values before we
862314737Smav			 * send them back down to the SIM.
863229997Sken			 */
864314737Smav			if (softc->flags & CTLFE_LUN_WILDCARD) {
865314737Smav				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
866314737Smav				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
867314737Smav			}
868275880Smav			xpt_action((union ccb *)atio);
869229997Sken
870314737Smav			/* If we still have work to do, ask for another CCB. */
871275880Smav			if (!TAILQ_EMPTY(&softc->work_queue))
872275880Smav				xpt_schedule(periph, /*priority*/ 1);
873275880Smav			return;
874275880Smav		}
875275881Smav		data_ptr = NULL;
876275881Smav		dxfer_len = 0;
877275881Smav		csio->sglist_cnt = 0;
878275881Smav	}
879313365Smav	scsi_status = 0;
880275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
881275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
882275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
883275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
884275880Smav		flags |= CAM_SEND_STATUS;
885275880Smav		scsi_status = io->scsiio.scsi_status;
886275880Smav		csio->sense_len = io->scsiio.sense_len;
887275880Smav#ifdef CTLFEDEBUG
888275880Smav		printf("%s: tag %04x status %x\n", __func__,
889275880Smav		       atio->tag_id, io->io_hdr.status);
890275880Smav#endif
891275880Smav		if (csio->sense_len != 0) {
892275880Smav			csio->sense_data = io->scsiio.sense_data;
893275880Smav			flags |= CAM_SEND_SENSE;
894229997Sken		}
895275880Smav	}
896229997Sken
897229997Sken#ifdef CTLFEDEBUG
898275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
899275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
900275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
901229997Sken#endif
902229997Sken
903275880Smav	/*
904275880Smav	 * Valid combinations:
905275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
906275880Smav	 *    sglist_cnt = 0
907275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
908275880Smav	 *    sglist_cnt = 0
909275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
910275880Smav	 *    sglist_cnt != 0
911275880Smav	 */
912229997Sken#ifdef CTLFEDEBUG
913275880Smav	if (((flags & CAM_SEND_STATUS)
914275880Smav	  && (((flags & CAM_DATA_SG) != 0)
915275880Smav	   || (dxfer_len != 0)
916275880Smav	   || (csio->sglist_cnt != 0)))
917275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
918275880Smav	  && (dxfer_len == 0))
919275880Smav	 || ((flags & CAM_DATA_SG)
920275880Smav	  && (csio->sglist_cnt == 0))
921275880Smav	 || (((flags & CAM_DATA_SG) == 0)
922275880Smav	  && (csio->sglist_cnt != 0))) {
923275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
924275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
925312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
926275880Smav		       csio->sglist_cnt);
927275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
928275880Smav		       atio->tag_id, io->io_hdr.status);
929275880Smav	}
930229997Sken#endif
931275880Smav	cam_fill_ctio(csio,
932275880Smav		      /*retries*/ 2,
933275880Smav		      ctlfedone,
934275880Smav		      flags,
935275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
936275880Smav		      atio->tag_id,
937275880Smav		      atio->init_id,
938275880Smav		      scsi_status,
939275880Smav		      /*data_ptr*/ data_ptr,
940275880Smav		      /*dxfer_len*/ dxfer_len,
941275880Smav		      /*timeout*/ 5 * 1000);
942275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
943275880Smav	start_ccb->ccb_h.ccb_atio = atio;
944275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
945275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
946275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
947229997Sken
948275880Smav	softc->ctios_sent++;
949229997Sken
950275880Smav	cam_periph_unlock(periph);
951275880Smav	xpt_action(start_ccb);
952275880Smav	cam_periph_lock(periph);
953229997Sken
954229997Sken	/*
955275880Smav	 * If we still have work to do, ask for another CCB.
956229997Sken	 */
957275880Smav	if (!TAILQ_EMPTY(&softc->work_queue))
958229997Sken		xpt_schedule(periph, /*priority*/ 1);
959229997Sken}
960229997Sken
961229997Skenstatic void
962229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
963229997Sken{
964229997Sken	struct ctlfe_lun_softc *softc;
965288723Smav	union ctl_io *io;
966288723Smav	struct ctlfe_cmd_info *cmd_info;
967229997Sken
968229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
969288723Smav	io = ccb->ccb_h.io_ptr;
970229997Sken
971229997Sken	switch (ccb->ccb_h.func_code) {
972229997Sken	case XPT_ACCEPT_TARGET_IO:
973284793Smav		softc->atios_freed++;
974312585Smav		cmd_info = PRIV_INFO(io);
975288723Smav		free(cmd_info, M_CTLFE);
976229997Sken		break;
977229997Sken	case XPT_IMMEDIATE_NOTIFY:
978229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
979284793Smav		softc->inots_freed++;
980229997Sken		break;
981229997Sken	default:
982229997Sken		break;
983229997Sken	}
984229997Sken
985288723Smav	ctl_free_io(io);
986229997Sken	free(ccb, M_CTLFE);
987229997Sken
988284793Smav	KASSERT(softc->atios_freed <= softc->atios_alloced, ("%s: "
989284793Smav		"atios_freed %ju > atios_alloced %ju", __func__,
990284793Smav		softc->atios_freed, softc->atios_alloced));
991284793Smav	KASSERT(softc->inots_freed <= softc->inots_alloced, ("%s: "
992284793Smav		"inots_freed %ju > inots_alloced %ju", __func__,
993284793Smav		softc->inots_freed, softc->inots_alloced));
994229997Sken
995229997Sken	/*
996229997Sken	 * If we have received all of our CCBs, we can release our
997229997Sken	 * reference on the peripheral driver.  It will probably go away
998229997Sken	 * now.
999229997Sken	 */
1000284793Smav	if ((softc->atios_freed == softc->atios_alloced)
1001284793Smav	 && (softc->inots_freed == softc->inots_alloced)) {
1002229997Sken		cam_periph_release_locked(periph);
1003229997Sken	}
1004229997Sken}
1005229997Sken
1006238870Smjacobstatic int
1007238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1008238870Smjacob{
1009238870Smjacob	uint64_t lba;
1010238870Smjacob	uint32_t num_blocks, nbc;
1011312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
1012238870Smjacob
1013238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1014238870Smjacob
1015238870Smjacob	switch (cmdbyt[0]) {
1016238870Smjacob	case READ_6:
1017238870Smjacob	case WRITE_6:
1018238870Smjacob	{
1019238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1020238870Smjacob		lba = scsi_3btoul(cdb->addr);
1021238870Smjacob		lba &= 0x1fffff;
1022238870Smjacob		num_blocks = cdb->length;
1023238870Smjacob		if (num_blocks == 0)
1024238870Smjacob			num_blocks = 256;
1025238870Smjacob		lba += nbc;
1026238870Smjacob		num_blocks -= nbc;
1027238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1028238870Smjacob		cdb->length = num_blocks;
1029238870Smjacob		break;
1030238870Smjacob	}
1031238870Smjacob	case READ_10:
1032238870Smjacob	case WRITE_10:
1033238870Smjacob	{
1034238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1035238870Smjacob		lba = scsi_4btoul(cdb->addr);
1036238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1037238870Smjacob		lba += nbc;
1038238870Smjacob		num_blocks -= nbc;
1039238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1040238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1041238870Smjacob		break;
1042238870Smjacob	}
1043238870Smjacob	case READ_12:
1044238870Smjacob	case WRITE_12:
1045238870Smjacob	{
1046238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1047238870Smjacob		lba = scsi_4btoul(cdb->addr);
1048238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1049238870Smjacob		lba += nbc;
1050238870Smjacob		num_blocks -= nbc;
1051238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1052238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1053238870Smjacob		break;
1054238870Smjacob	}
1055238870Smjacob	case READ_16:
1056238870Smjacob	case WRITE_16:
1057238870Smjacob	{
1058238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1059238870Smjacob		lba = scsi_8btou64(cdb->addr);
1060238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1061238870Smjacob		lba += nbc;
1062238870Smjacob		num_blocks -= nbc;
1063238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1064238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1065238870Smjacob		break;
1066238870Smjacob	}
1067238870Smjacob	default:
1068238870Smjacob		return -1;
1069238870Smjacob	}
1070238870Smjacob	return (0);
1071238870Smjacob}
1072238870Smjacob
1073229997Skenstatic void
1074229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1075229997Sken{
1076229997Sken	struct ctlfe_lun_softc *softc;
1077229997Sken	struct ctlfe_softc *bus_softc;
1078288723Smav	struct ctlfe_cmd_info *cmd_info;
1079238870Smjacob	struct ccb_accept_tio *atio = NULL;
1080238870Smjacob	union ctl_io *io = NULL;
1081260387Sscottl	struct mtx *mtx;
1082314727Smav	cam_status status;
1083229997Sken
1084260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1085260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1086229997Sken#ifdef CTLFE_DEBUG
1087275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1088275878Smav	       done_ccb->ccb_h.func_code);
1089229997Sken#endif
1090229997Sken
1091284794Smav	/*
1092284794Smav	 * At this point CTL has no known use case for device queue freezes.
1093284794Smav	 * In case some SIM think different -- drop its freeze right here.
1094284794Smav	 */
1095284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1096284794Smav		cam_release_devq(periph->path,
1097284794Smav				 /*relsim_flags*/0,
1098284794Smav				 /*reduction*/0,
1099284794Smav				 /*timeout*/0,
1100284794Smav				 /*getcount_only*/0);
1101284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1102284794Smav	}
1103284794Smav
1104229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1105229997Sken	bus_softc = softc->parent_softc;
1106260387Sscottl	mtx = cam_periph_mtx(periph);
1107260387Sscottl	mtx_lock(mtx);
1108229997Sken
1109229997Sken	switch (done_ccb->ccb_h.func_code) {
1110229997Sken	case XPT_ACCEPT_TARGET_IO: {
1111229997Sken
1112229997Sken		atio = &done_ccb->atio;
1113314727Smav		status = atio->ccb_h.status & CAM_STATUS_MASK;
1114314727Smav		if (status != CAM_CDB_RECVD) {
1115314727Smav			ctlfe_free_ccb(periph, done_ccb);
1116314727Smav			goto out;
1117314727Smav		}
1118229997Sken
1119238870Smjacob resubmit:
1120229997Sken		/*
1121229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1122229997Sken		 * datamove or done.
1123229997Sken		 */
1124260387Sscottl		mtx_unlock(mtx);
1125275878Smav		io = done_ccb->ccb_h.io_ptr;
1126312585Smav		cmd_info = PRIV_INFO(io);
1127229997Sken		ctl_zero_io(io);
1128229997Sken
1129229997Sken		/* Save pointers on both sides */
1130312585Smav		PRIV_CCB(io) = done_ccb;
1131312585Smav		PRIV_INFO(io) = cmd_info;
1132229997Sken		done_ccb->ccb_h.io_ptr = io;
1133229997Sken
1134229997Sken		/*
1135229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1136229997Sken		 * down the immediate notify path below.
1137229997Sken		 */
1138229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1139288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1140268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1141290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1142290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1143290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1144290776Smav		} else {
1145290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1146290776Smav		}
1147229997Sken		io->scsiio.tag_num = atio->tag_id;
1148229997Sken		switch (atio->tag_action) {
1149229997Sken		case CAM_TAG_ACTION_NONE:
1150229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1151229997Sken			break;
1152229997Sken		case MSG_SIMPLE_TASK:
1153229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1154229997Sken			break;
1155229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1156229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1157229997Sken			break;
1158229997Sken		case MSG_ORDERED_TASK:
1159229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1160229997Sken			break;
1161229997Sken		case MSG_ACA_TASK:
1162229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1163229997Sken			break;
1164229997Sken		default:
1165229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1166229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1167229997Sken			       atio->tag_action);
1168229997Sken			break;
1169229997Sken		}
1170229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1171229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1172229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1173229997Sken		}
1174229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1175312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1176229997Sken
1177229997Sken#ifdef CTLFEDEBUG
1178288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1179288731Smav		        io->io_hdr.nexus.initid,
1180229997Sken		        io->io_hdr.nexus.targ_port,
1181229997Sken		        io->io_hdr.nexus.targ_lun,
1182229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1183229997Sken#endif
1184229997Sken
1185229997Sken		ctl_queue(io);
1186260387Sscottl		return;
1187229997Sken	}
1188229997Sken	case XPT_CONT_TARGET_IO: {
1189238870Smjacob		int srr = 0;
1190238870Smjacob		uint32_t srr_off = 0;
1191229997Sken
1192229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1193229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1194229997Sken
1195229997Sken		softc->ctios_returned++;
1196229997Sken#ifdef CTLFEDEBUG
1197229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1198229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1199229997Sken#endif
1200229997Sken		/*
1201238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1202238870Smjacob		 */
1203238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1204238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1205238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1206238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1207238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1208238870Smjacob			srr = 1;
1209238870Smjacob			srr_off =
1210238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1211238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1212238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1213238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1214238870Smjacob		}
1215238870Smjacob
1216313365Smav		/*
1217313365Smav		 * If we have an SRR and we're still sending data, we
1218313365Smav		 * should be able to adjust offsets and cycle again.
1219313365Smav		 * It is possible only if offset is from this datamove.
1220313365Smav		 */
1221313365Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1222313365Smav		    srr_off >= io->scsiio.kern_rel_offset &&
1223313365Smav		    srr_off < io->scsiio.kern_rel_offset +
1224313365Smav		     io->scsiio.kern_data_len) {
1225313365Smav			io->scsiio.kern_data_resid =
1226313365Smav			    io->scsiio.kern_rel_offset +
1227313365Smav			    io->scsiio.kern_data_len - srr_off;
1228313365Smav			io->scsiio.ext_data_filled = srr_off;
1229313365Smav			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1230313365Smav			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1231313365Smav			softc->ccbs_freed++;
1232313365Smav			xpt_release_ccb(done_ccb);
1233313365Smav			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1234313365Smav					  periph_links.tqe);
1235313365Smav			xpt_schedule(periph, /*priority*/ 1);
1236313365Smav			break;
1237313365Smav		}
1238313365Smav
1239313365Smav		/*
1240313365Smav		 * If status was being sent, the back end data is now history.
1241313365Smav		 * Hack it up and resubmit a new command with the CDB adjusted.
1242313365Smav		 * If the SIM does the right thing, all of the resid math
1243313365Smav		 * should work.
1244313365Smav		 */
1245275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1246238870Smjacob			softc->ccbs_freed++;
1247238870Smjacob			xpt_release_ccb(done_ccb);
1248238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1249238870Smjacob				done_ccb = (union ccb *)atio;
1250238870Smjacob				goto resubmit;
1251238870Smjacob			}
1252238870Smjacob			/*
1253238870Smjacob			 * Fall through to doom....
1254238870Smjacob			 */
1255238870Smjacob		}
1256238870Smjacob
1257277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1258277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1259277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1260277919Smav
1261238870Smjacob		/*
1262229997Sken		 * If we were sending status back to the initiator, free up
1263229997Sken		 * resources.  If we were doing a datamove, call the
1264229997Sken		 * datamove done routine.
1265229997Sken		 */
1266275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1267229997Sken			softc->ccbs_freed++;
1268229997Sken			xpt_release_ccb(done_ccb);
1269229997Sken			/*
1270229997Sken			 * For a wildcard attachment, commands can come in
1271229997Sken			 * with a specific target/lun.  Reset the target
1272229997Sken			 * and LUN fields back to the wildcard values before
1273229997Sken			 * we send them back down to the SIM.  The SIM has
1274229997Sken			 * a wildcard LUN enabled, not whatever target/lun
1275229997Sken			 * these happened to be.
1276229997Sken			 */
1277229997Sken			if (softc->flags & CTLFE_LUN_WILDCARD) {
1278229997Sken				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1279229997Sken				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1280229997Sken			}
1281229997Sken			if (periph->flags & CAM_PERIPH_INVALID) {
1282229997Sken				ctlfe_free_ccb(periph, (union ccb *)atio);
1283229997Sken			} else {
1284260387Sscottl				mtx_unlock(mtx);
1285229997Sken				xpt_action((union ccb *)atio);
1286260387Sscottl				return;
1287229997Sken			}
1288229997Sken		} else {
1289288723Smav			struct ctlfe_cmd_info *cmd_info;
1290229997Sken			struct ccb_scsiio *csio;
1291229997Sken
1292229997Sken			csio = &done_ccb->csio;
1293312585Smav			cmd_info = PRIV_INFO(io);
1294229997Sken
1295229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1296229997Sken
1297229997Sken			/*
1298229997Sken			 * Translate CAM status to CTL status.  Success
1299229997Sken			 * does not change the overall, ctl_io status.  In
1300229997Sken			 * that case we just set port_status to 0.  If we
1301229997Sken			 * have a failure, though, set a data phase error
1302229997Sken			 * for the overall ctl_io.
1303229997Sken			 */
1304229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1305229997Sken			case CAM_REQ_CMP:
1306313365Smav				io->scsiio.kern_data_resid -= csio->dxfer_len;
1307229997Sken				io->io_hdr.port_status = 0;
1308229997Sken				break;
1309229997Sken			default:
1310229997Sken				/*
1311245228Sken				 * XXX KDM we probably need to figure out a
1312245228Sken				 * standard set of errors that the SIM
1313245228Sken				 * drivers should return in the event of a
1314245228Sken				 * data transfer failure.  A data phase
1315245228Sken				 * error will at least point the user to a
1316245228Sken				 * data transfer error of some sort.
1317245228Sken				 * Hopefully the SIM printed out some
1318245228Sken				 * additional information to give the user
1319245228Sken				 * a clue what happened.
1320229997Sken				 */
1321229997Sken				io->io_hdr.port_status = 0xbad1;
1322229997Sken				ctl_set_data_phase_error(&io->scsiio);
1323229997Sken				/*
1324229997Sken				 * XXX KDM figure out residual.
1325229997Sken				 */
1326229997Sken				break;
1327229997Sken			}
1328229997Sken			/*
1329229997Sken			 * If we had to break this S/G list into multiple
1330229997Sken			 * pieces, figure out where we are in the list, and
1331229997Sken			 * continue sending pieces if necessary.
1332229997Sken			 */
1333229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1334265641Smav			 && (io->io_hdr.port_status == 0)) {
1335229997Sken				ccb_flags flags;
1336229997Sken				uint8_t *data_ptr;
1337229997Sken				uint32_t dxfer_len;
1338229997Sken
1339229997Sken				flags = atio->ccb_h.flags &
1340229997Sken					(CAM_DIS_DISCONNECT|
1341265641Smav					 CAM_TAG_ACTION_VALID);
1342229997Sken
1343265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1344265641Smav				    &dxfer_len, &csio->sglist_cnt);
1345229997Sken
1346229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1347229997Sken				 && (dxfer_len == 0)) {
1348229997Sken					printf("%s: tag %04x no status or "
1349229997Sken					       "len cdb = %02x\n", __func__,
1350229997Sken					       atio->tag_id,
1351312845Smav					       atio_cdb_ptr(atio)[0]);
1352229997Sken					printf("%s: tag %04x io status %#x\n",
1353229997Sken					       __func__, atio->tag_id,
1354229997Sken					       io->io_hdr.status);
1355229997Sken				}
1356229997Sken
1357229997Sken				cam_fill_ctio(csio,
1358229997Sken					      /*retries*/ 2,
1359229997Sken					      ctlfedone,
1360229997Sken					      flags,
1361229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1362229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1363229997Sken					      atio->tag_id,
1364229997Sken					      atio->init_id,
1365313365Smav					      0,
1366229997Sken					      /*data_ptr*/ data_ptr,
1367229997Sken					      /*dxfer_len*/ dxfer_len,
1368229997Sken					      /*timeout*/ 5 * 1000);
1369229997Sken
1370260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1371229997Sken				csio->resid = 0;
1372229997Sken				csio->ccb_h.ccb_atio = atio;
1373229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1374229997Sken				softc->ctios_sent++;
1375260387Sscottl				mtx_unlock(mtx);
1376229997Sken				xpt_action((union ccb *)csio);
1377229997Sken			} else {
1378229997Sken				/*
1379229997Sken				 * Release the CTIO.  The ATIO will be sent back
1380229997Sken				 * down to the SIM once we send status.
1381229997Sken				 */
1382229997Sken				softc->ccbs_freed++;
1383229997Sken				xpt_release_ccb(done_ccb);
1384260387Sscottl				mtx_unlock(mtx);
1385229997Sken
1386229997Sken				/* Call the backend move done callback */
1387229997Sken				io->scsiio.be_move_done(io);
1388229997Sken			}
1389260387Sscottl			return;
1390229997Sken		}
1391229997Sken		break;
1392229997Sken	}
1393229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1394229997Sken		union ctl_io *io;
1395229997Sken		struct ccb_immediate_notify *inot;
1396284794Smav		int send_ctl_io;
1397229997Sken
1398229997Sken		inot = &done_ccb->cin1;
1399275878Smav		io = done_ccb->ccb_h.io_ptr;
1400275878Smav		ctl_zero_io(io);
1401229997Sken
1402275878Smav		send_ctl_io = 1;
1403229997Sken
1404275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1405312585Smav		PRIV_CCB(io) = done_ccb;
1406275878Smav		inot->ccb_h.io_ptr = io;
1407288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1408275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1409290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1410290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1411290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1412290776Smav		} else {
1413290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1414290776Smav		}
1415275878Smav		/* XXX KDM should this be the tag_id? */
1416275878Smav		io->taskio.tag_num = inot->seq_id;
1417229997Sken
1418275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1419275878Smav		switch (status) {
1420275878Smav		case CAM_SCSI_BUS_RESET:
1421275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1422275878Smav			break;
1423275878Smav		case CAM_BDR_SENT:
1424275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1425275878Smav			break;
1426275878Smav		case CAM_MESSAGE_RECV:
1427275878Smav			switch (inot->arg) {
1428275878Smav			case MSG_ABORT_TASK_SET:
1429275878Smav				io->taskio.task_action =
1430275878Smav				    CTL_TASK_ABORT_TASK_SET;
1431229997Sken				break;
1432275878Smav			case MSG_TARGET_RESET:
1433290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1434229997Sken				break;
1435275878Smav			case MSG_ABORT_TASK:
1436290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1437229997Sken				break;
1438275878Smav			case MSG_LOGICAL_UNIT_RESET:
1439290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1440275878Smav				break;
1441275878Smav			case MSG_CLEAR_TASK_SET:
1442275878Smav				io->taskio.task_action =
1443290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1444275878Smav				break;
1445275878Smav			case MSG_CLEAR_ACA:
1446290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1447290775Smav				break;
1448290775Smav			case MSG_QUERY_TASK:
1449290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1450290775Smav				break;
1451290775Smav			case MSG_QUERY_TASK_SET:
1452275878Smav				io->taskio.task_action =
1453290775Smav				    CTL_TASK_QUERY_TASK_SET;
1454275878Smav				break;
1455290775Smav			case MSG_QUERY_ASYNC_EVENT:
1456290775Smav				io->taskio.task_action =
1457290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1458290775Smav				break;
1459275878Smav			case MSG_NOOP:
1460229997Sken				send_ctl_io = 0;
1461229997Sken				break;
1462229997Sken			default:
1463275878Smav				xpt_print(periph->path,
1464314727Smav				    "%s: unsupported INOT message 0x%x\n",
1465314727Smav				    __func__, inot->arg);
1466275878Smav				send_ctl_io = 0;
1467275878Smav				break;
1468275878Smav			}
1469275878Smav			break;
1470314727Smav		default:
1471314727Smav			xpt_print(periph->path,
1472314727Smav			    "%s: unsupported INOT status 0x%x\n",
1473314727Smav			    __func__, status);
1474314727Smav			/* FALLTHROUGH */
1475275878Smav		case CAM_REQ_ABORTED:
1476275878Smav		case CAM_REQ_INVALID:
1477314727Smav		case CAM_DEV_NOT_THERE:
1478275878Smav		case CAM_PROVIDE_FAIL:
1479275878Smav			ctlfe_free_ccb(periph, done_ccb);
1480275878Smav			goto out;
1481275878Smav		}
1482275878Smav		if (send_ctl_io != 0) {
1483275878Smav			ctl_queue(io);
1484229997Sken		} else {
1485229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1486229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1487229997Sken			xpt_action(done_ccb);
1488229997Sken		}
1489229997Sken		break;
1490229997Sken	}
1491229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1492314727Smav		if (periph->flags & CAM_PERIPH_INVALID) {
1493314727Smav			ctlfe_free_ccb(periph, done_ccb);
1494314727Smav			goto out;
1495314727Smav		}
1496314727Smav
1497229997Sken		/*
1498229997Sken		 * Queue this back down to the SIM as an immediate notify.
1499229997Sken		 */
1500304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1501229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1502229997Sken		xpt_action(done_ccb);
1503229997Sken		break;
1504229997Sken	case XPT_SET_SIM_KNOB:
1505229997Sken	case XPT_GET_SIM_KNOB:
1506229997Sken		break;
1507229997Sken	default:
1508229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1509229997Sken		      done_ccb->ccb_h.func_code);
1510229997Sken		break;
1511229997Sken	}
1512260387Sscottl
1513260387Sscottlout:
1514260387Sscottl	mtx_unlock(mtx);
1515229997Sken}
1516229997Sken
1517229997Skenstatic void
1518229997Skenctlfe_onoffline(void *arg, int online)
1519229997Sken{
1520229997Sken	struct ctlfe_softc *bus_softc;
1521229997Sken	union ccb *ccb;
1522229997Sken	cam_status status;
1523229997Sken	struct cam_path *path;
1524229997Sken	int set_wwnn;
1525229997Sken
1526229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1527229997Sken
1528229997Sken	set_wwnn = 0;
1529229997Sken
1530229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1531229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1532229997Sken	if (status != CAM_REQ_CMP) {
1533229997Sken		printf("%s: unable to create path!\n", __func__);
1534229997Sken		return;
1535229997Sken	}
1536275882Smav	ccb = xpt_alloc_ccb();
1537242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1538288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1539288713Smav	xpt_action(ccb);
1540229997Sken
1541229997Sken	/*
1542229997Sken	 * Copan WWN format:
1543229997Sken	 *
1544229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1545229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1546229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1547229997Sken	 * Bits 11-8:			Type of port:
1548229997Sken	 *					1 == N-Port
1549229997Sken	 *					2 == F-Port
1550229997Sken	 *					3 == NL-Port
1551229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1552229997Sken	 */
1553229997Sken	if (online != 0) {
1554229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1555229997Sken#ifdef RANDOM_WWNN
1556229997Sken			uint64_t random_bits;
1557229997Sken#endif
1558229997Sken
1559229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1560229997Sken			       bus_softc->port_name,
1561229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1562229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1563229997Sken			       bus_softc->port_name,
1564229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1565229997Sken
1566229997Sken#ifdef RANDOM_WWNN
1567229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1568229997Sken#endif
1569229997Sken
1570229997Sken			/*
1571229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1572229997Sken			 * take the current WWNN/WWPN from the card, and
1573229997Sken			 * replace the company identifier and the NL-Port
1574229997Sken			 * indicator and the port number (for the WWPN).
1575229997Sken			 * This should be replaced later with ddb_GetWWNN,
1576229997Sken			 * or possibly a more centralized scheme.  (It
1577229997Sken			 * would be nice to have the WWNN/WWPN for each
1578268677Smav			 * port stored in the ctl_port structure.)
1579229997Sken			 */
1580229997Sken#ifdef RANDOM_WWNN
1581229997Sken			ccb->knob.xport_specific.fc.wwnn =
1582229997Sken				(random_bits &
1583229997Sken				0x0000000fffffff00ULL) |
1584229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1585229997Sken				/* NL-Port */    0x0300;
1586229997Sken			ccb->knob.xport_specific.fc.wwpn =
1587229997Sken				(random_bits &
1588229997Sken				0x0000000fffffff00ULL) |
1589229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1590229997Sken				/* NL-Port */    0x3000 |
1591268677Smav				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1592229997Sken
1593229997Sken			/*
1594229997Sken			 * This is a bit of an API break/reversal, but if
1595229997Sken			 * we're doing the random WWNN that's a little
1596229997Sken			 * different anyway.  So record what we're actually
1597229997Sken			 * using with the frontend code so it's reported
1598229997Sken			 * accurately.
1599229997Sken			 */
1600268683Smav			ctl_port_set_wwns(&bus_softc->port,
1601268683Smav			    true, ccb->knob.xport_specific.fc.wwnn,
1602268683Smav			    true, ccb->knob.xport_specific.fc.wwpn);
1603229997Sken			set_wwnn = 1;
1604229997Sken#else /* RANDOM_WWNN */
1605229997Sken			/*
1606229997Sken			 * If the user has specified a WWNN/WWPN, send them
1607229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1608229997Sken			 * has reported.
1609229997Sken			 */
1610284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1611284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1612229997Sken				ccb->knob.xport_specific.fc.wwnn =
1613284586Smav				    bus_softc->port.wwnn;
1614284586Smav				set_wwnn = 1;
1615284586Smav			} else {
1616284586Smav				ctl_port_set_wwns(&bus_softc->port,
1617284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1618284586Smav				    false, 0);
1619284586Smav			}
1620284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1621284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1622229997Sken				ccb->knob.xport_specific.fc.wwpn =
1623284586Smav				    bus_softc->port.wwpn;
1624229997Sken				set_wwnn = 1;
1625229997Sken			} else {
1626268683Smav				ctl_port_set_wwns(&bus_softc->port,
1627284586Smav				    false, 0,
1628268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1629229997Sken			}
1630229997Sken#endif /* RANDOM_WWNN */
1631229997Sken
1632229997Sken
1633229997Sken			if (set_wwnn != 0) {
1634229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1635229997Sken				       bus_softc->port_name,
1636229997Sken				ccb->knob.xport_specific.fc.wwnn);
1637229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1638229997Sken				       bus_softc->port_name,
1639229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1640229997Sken			}
1641229997Sken		} else {
1642229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1643229997Sken			       bus_softc->port_name);
1644229997Sken		}
1645229997Sken	}
1646229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1647229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1648229997Sken	if (set_wwnn != 0)
1649229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1650229997Sken
1651229997Sken	if (online != 0)
1652288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1653229997Sken	else
1654288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1655229997Sken
1656229997Sken	xpt_action(ccb);
1657229997Sken
1658229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1659229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1660229997Sken		       "status %#x\n",
1661229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1662229997Sken		       (online != 0) ? "enable" : "disable",
1663229997Sken		       ccb->ccb_h.status);
1664229997Sken	} else {
1665229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1666229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1667229997Sken		       (online != 0) ? "enable" : "disable");
1668229997Sken	}
1669229997Sken
1670229997Sken	xpt_free_path(path);
1671275882Smav	xpt_free_ccb(ccb);
1672229997Sken}
1673229997Sken
1674229997Skenstatic void
1675229997Skenctlfe_online(void *arg)
1676229997Sken{
1677245228Sken	struct ctlfe_softc *bus_softc;
1678245228Sken	struct cam_path *path;
1679245228Sken	cam_status status;
1680245228Sken	struct ctlfe_lun_softc *lun_softc;
1681274388Smav	struct cam_periph *periph;
1682245228Sken
1683245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1684245228Sken
1685245228Sken	/*
1686245228Sken	 * Create the wildcard LUN before bringing the port online.
1687245228Sken	 */
1688245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1689245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1690245228Sken				 CAM_LUN_WILDCARD);
1691245228Sken	if (status != CAM_REQ_CMP) {
1692245228Sken		printf("%s: unable to create path for wildcard periph\n",
1693245228Sken				__func__);
1694245228Sken		return;
1695245228Sken	}
1696245228Sken
1697275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1698245228Sken
1699260387Sscottl	xpt_path_lock(path);
1700274388Smav	periph = cam_periph_find(path, "ctl");
1701274388Smav	if (periph != NULL) {
1702274388Smav		/* We've already got a periph, no need to alloc a new one. */
1703274388Smav		xpt_path_unlock(path);
1704274388Smav		xpt_free_path(path);
1705274388Smav		free(lun_softc, M_CTLFE);
1706274388Smav		return;
1707274388Smav	}
1708245228Sken	lun_softc->parent_softc = bus_softc;
1709245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1710245228Sken
1711245228Sken	status = cam_periph_alloc(ctlferegister,
1712245228Sken				  ctlfeoninvalidate,
1713245228Sken				  ctlfecleanup,
1714245228Sken				  ctlfestart,
1715245228Sken				  "ctl",
1716245228Sken				  CAM_PERIPH_BIO,
1717245228Sken				  path,
1718245228Sken				  ctlfeasync,
1719245228Sken				  0,
1720245228Sken				  lun_softc);
1721245228Sken
1722245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1723245228Sken		const struct cam_status_entry *entry;
1724245228Sken
1725245228Sken		entry = cam_fetch_status_entry(status);
1726245228Sken		printf("%s: CAM error %s (%#x) returned from "
1727245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1728245228Sken		       entry->status_text : "Unknown", status);
1729274388Smav		free(lun_softc, M_CTLFE);
1730245228Sken	}
1731245228Sken
1732260387Sscottl	xpt_path_unlock(path);
1733275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1734260387Sscottl	xpt_free_path(path);
1735229997Sken}
1736229997Sken
1737229997Skenstatic void
1738229997Skenctlfe_offline(void *arg)
1739229997Sken{
1740245228Sken	struct ctlfe_softc *bus_softc;
1741245228Sken	struct cam_path *path;
1742245228Sken	cam_status status;
1743245228Sken	struct cam_periph *periph;
1744245228Sken
1745245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1746245228Sken
1747275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1748275882Smav
1749245228Sken	/*
1750245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1751245228Sken	 * the port offline.
1752245228Sken	 */
1753245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1754245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1755245228Sken				 CAM_LUN_WILDCARD);
1756245228Sken	if (status != CAM_REQ_CMP) {
1757245228Sken		printf("%s: unable to create path for wildcard periph\n",
1758245228Sken		       __func__);
1759245228Sken		return;
1760245228Sken	}
1761260387Sscottl	xpt_path_lock(path);
1762245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1763245228Sken		cam_periph_invalidate(periph);
1764260387Sscottl	xpt_path_unlock(path);
1765245228Sken	xpt_free_path(path);
1766229997Sken}
1767229997Sken
1768229997Sken/*
1769229997Sken * This will get called to enable a LUN on every bus that is attached to
1770229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1771229997Sken */
1772229997Skenstatic int
1773284798Smavctlfe_lun_enable(void *arg, int lun_id)
1774229997Sken{
1775229997Sken	struct ctlfe_softc *bus_softc;
1776229997Sken	struct ctlfe_lun_softc *softc;
1777229997Sken	struct cam_path *path;
1778229997Sken	struct cam_periph *periph;
1779229997Sken	cam_status status;
1780229997Sken
1781229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1782290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1783290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1784229997Sken
1785260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1786290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1787229997Sken	/* XXX KDM need some way to return status to CTL here? */
1788229997Sken	if (status != CAM_REQ_CMP) {
1789229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1790229997Sken		       status);
1791229997Sken		return (1);
1792229997Sken	}
1793229997Sken
1794229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1795260387Sscottl	xpt_path_lock(path);
1796229997Sken	periph = cam_periph_find(path, "ctl");
1797229997Sken	if (periph != NULL) {
1798229997Sken		/* We've already got a periph, no need to alloc a new one. */
1799260387Sscottl		xpt_path_unlock(path);
1800229997Sken		xpt_free_path(path);
1801229997Sken		free(softc, M_CTLFE);
1802229997Sken		return (0);
1803229997Sken	}
1804229997Sken	softc->parent_softc = bus_softc;
1805229997Sken
1806229997Sken	status = cam_periph_alloc(ctlferegister,
1807229997Sken				  ctlfeoninvalidate,
1808229997Sken				  ctlfecleanup,
1809229997Sken				  ctlfestart,
1810229997Sken				  "ctl",
1811229997Sken				  CAM_PERIPH_BIO,
1812229997Sken				  path,
1813229997Sken				  ctlfeasync,
1814229997Sken				  0,
1815229997Sken				  softc);
1816229997Sken
1817274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1818274388Smav		const struct cam_status_entry *entry;
1819274388Smav
1820274388Smav		entry = cam_fetch_status_entry(status);
1821274388Smav		printf("%s: CAM error %s (%#x) returned from "
1822274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1823274388Smav		       entry->status_text : "Unknown", status);
1824274388Smav		free(softc, M_CTLFE);
1825274388Smav	}
1826274388Smav
1827260387Sscottl	xpt_path_unlock(path);
1828244016Sken	xpt_free_path(path);
1829229997Sken	return (0);
1830229997Sken}
1831229997Sken
1832229997Sken/*
1833245228Sken * This will get called when the user removes a LUN to disable that LUN
1834245228Sken * on every bus that is attached to CTL.
1835229997Sken */
1836229997Skenstatic int
1837284798Smavctlfe_lun_disable(void *arg, int lun_id)
1838229997Sken{
1839229997Sken	struct ctlfe_softc *softc;
1840229997Sken	struct ctlfe_lun_softc *lun_softc;
1841229997Sken
1842229997Sken	softc = (struct ctlfe_softc *)arg;
1843290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1844290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1845229997Sken
1846260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1847229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1848229997Sken		struct cam_path *path;
1849229997Sken
1850229997Sken		path = lun_softc->periph->path;
1851229997Sken
1852290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1853229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1854229997Sken			break;
1855229997Sken		}
1856229997Sken	}
1857229997Sken	if (lun_softc == NULL) {
1858260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1859284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1860229997Sken		return (1);
1861229997Sken	}
1862260387Sscottl	cam_periph_acquire(lun_softc->periph);
1863260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1864229997Sken
1865260387Sscottl	cam_periph_lock(lun_softc->periph);
1866229997Sken	cam_periph_invalidate(lun_softc->periph);
1867260387Sscottl	cam_periph_unlock(lun_softc->periph);
1868260387Sscottl	cam_periph_release(lun_softc->periph);
1869229997Sken	return (0);
1870229997Sken}
1871229997Sken
1872229997Skenstatic void
1873229997Skenctlfe_dump_sim(struct cam_sim *sim)
1874229997Sken{
1875229997Sken
1876229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1877229997Sken	       sim->sim_name, sim->unit_number,
1878229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1879229997Sken}
1880229997Sken
1881229997Sken/*
1882229997Sken * Assumes that the SIM lock is held.
1883229997Sken */
1884229997Skenstatic void
1885229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1886229997Sken{
1887229997Sken	struct ccb_hdr *hdr;
1888229997Sken	struct cam_periph *periph;
1889229997Sken	int num_items;
1890229997Sken
1891229997Sken	periph = softc->periph;
1892229997Sken	num_items = 0;
1893229997Sken
1894229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1895275880Smav		union ctl_io *io = hdr->io_ptr;
1896229997Sken
1897229997Sken		num_items++;
1898229997Sken
1899229997Sken		/*
1900229997Sken		 * Only regular SCSI I/O is put on the work
1901229997Sken		 * queue, so we can print sense here.  There may be no
1902229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1903229997Sken		 * print out the CCB as well.
1904229997Sken		 *
1905229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1906229997Sken		 * CTL is merged in with CAM.
1907229997Sken		 */
1908229997Sken		ctl_io_error_print(io, NULL);
1909229997Sken
1910229997Sken		/*
1911275880Smav		 * Print DMA status if we are DMA_QUEUED.
1912229997Sken		 */
1913275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1914275880Smav			xpt_print(periph->path,
1915275880Smav			    "Total %u, Current %u, Resid %u\n",
1916275880Smav			    io->scsiio.kern_total_len,
1917275880Smav			    io->scsiio.kern_data_len,
1918275880Smav			    io->scsiio.kern_data_resid);
1919275880Smav		}
1920229997Sken	}
1921229997Sken
1922229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1923229997Sken		  num_items);
1924250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
1925229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1926229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1927229997Sken		  (uintmax_t)softc->ccbs_freed);
1928229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1929229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
1930229997Sken		  softc->ctios_returned), softc->ctios_sent,
1931229997Sken		  softc->ctios_returned);
1932229997Sken}
1933229997Sken
1934229997Sken/*
1935275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1936275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1937275880Smav * to the adapter.
1938229997Sken */
1939229997Skenstatic void
1940275880Smavctlfe_datamove(union ctl_io *io)
1941229997Sken{
1942275880Smav	union ccb *ccb;
1943275880Smav	struct cam_periph *periph;
1944229997Sken	struct ctlfe_lun_softc *softc;
1945229997Sken
1946275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1947275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1948229997Sken
1949313365Smav	io->scsiio.ext_data_filled = 0;
1950312585Smav	ccb = PRIV_CCB(io);
1951275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
1952275880Smav	cam_periph_lock(periph);
1953275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1954275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1955275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1956275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1957275880Smav	TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1958275880Smav			  periph_links.tqe);
1959229997Sken	xpt_schedule(periph, /*priority*/ 1);
1960275880Smav	cam_periph_unlock(periph);
1961229997Sken}
1962229997Sken
1963229997Skenstatic void
1964275880Smavctlfe_done(union ctl_io *io)
1965229997Sken{
1966229997Sken	union ccb *ccb;
1967229997Sken	struct cam_periph *periph;
1968229997Sken	struct ctlfe_lun_softc *softc;
1969229997Sken
1970312585Smav	ccb = PRIV_CCB(io);
1971229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
1972260387Sscottl	cam_periph_lock(periph);
1973229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1974229997Sken
1975229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
1976229997Sken		/*
1977229997Sken		 * Send the notify acknowledge down to the SIM, to let it
1978229997Sken		 * know we processed the task management command.
1979229997Sken		 */
1980229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
1981229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1982304417Smav		switch (io->taskio.task_status) {
1983304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
1984304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1985304417Smav			break;
1986304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
1987304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1988304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1989304417Smav			break;
1990304417Smav		case CTL_TASK_FUNCTION_REJECTED:
1991304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1992304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1993304417Smav			break;
1994304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
1995304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1996304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1997304417Smav			break;
1998304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1999304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
2000304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2001304417Smav			break;
2002304417Smav		}
2003304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
2004229997Sken		xpt_action(ccb);
2005275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
2006275881Smav		if (softc->flags & CTLFE_LUN_WILDCARD) {
2007275881Smav			ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
2008275881Smav			ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
2009275881Smav		}
2010275881Smav		if (periph->flags & CAM_PERIPH_INVALID) {
2011275881Smav			ctlfe_free_ccb(periph, ccb);
2012275881Smav		} else {
2013275881Smav			cam_periph_unlock(periph);
2014275881Smav			xpt_action(ccb);
2015275881Smav			return;
2016275881Smav		}
2017229997Sken	} else {
2018275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2019229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2020229997Sken				  periph_links.tqe);
2021275880Smav		xpt_schedule(periph, /*priority*/ 1);
2022229997Sken	}
2023229997Sken
2024260387Sscottl	cam_periph_unlock(periph);
2025229997Sken}
2026229997Sken
2027229997Skenstatic void
2028229997Skenctlfe_dump(void)
2029229997Sken{
2030229997Sken	struct ctlfe_softc *bus_softc;
2031275880Smav	struct ctlfe_lun_softc *lun_softc;
2032229997Sken
2033229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2034229997Sken		ctlfe_dump_sim(bus_softc->sim);
2035275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
2036229997Sken			ctlfe_dump_queue(lun_softc);
2037229997Sken	}
2038229997Sken}
2039