scsi_ctl.c revision 314739
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 314739 2017-03-06 06:26:02Z 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);
213314739Smavstatic void		ctlfe_free_ccb(struct cam_periph *periph,
214314739Smav			    union ccb *ccb);
215314739Smavstatic void		ctlfe_requeue_ccb(struct cam_periph *periph,
216314739Smav			    union ccb *ccb, int unlock);
217229997Sken
218229997Skenstatic struct periph_driver ctlfe_driver =
219229997Sken{
220268677Smav	ctlfeperiphinit, "ctl",
221273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
222273316Smav	CAM_PERIPH_DRV_EARLY
223229997Sken};
224229997Sken
225268677Smavstatic struct ctl_frontend ctlfe_frontend =
226268677Smav{
227273318Smav	.name = "camtgt",
228268677Smav	.init = ctlfeinitialize,
229268677Smav	.fe_dump = ctlfe_dump,
230268677Smav	.shutdown = ctlfeshutdown,
231249009Strasz};
232268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
233249009Strasz
234313369Smavstatic int
235229997Skenctlfeshutdown(void)
236229997Sken{
237313369Smav
238313369Smav	/* CAM does not support periph driver unregister now. */
239313369Smav	return (EBUSY);
240229997Sken}
241229997Sken
242313369Smavstatic int
243268677Smavctlfeinitialize(void)
244229997Sken{
245229997Sken
246229997Sken	STAILQ_INIT(&ctlfe_softc_list);
247229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
248268677Smav	periphdriver_register(&ctlfe_driver);
249268677Smav	return (0);
250268677Smav}
251229997Sken
252313369Smavstatic void
253268677Smavctlfeperiphinit(void)
254268677Smav{
255268677Smav	cam_status status;
256229997Sken
257229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
258229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
259229997Sken	if (status != CAM_REQ_CMP) {
260229997Sken		printf("ctl: Failed to attach async callback due to CAM "
261229997Sken		       "status 0x%x!\n", status);
262229997Sken	}
263229997Sken}
264229997Sken
265229997Skenstatic void
266229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
267229997Sken{
268273317Smav	struct ctlfe_softc *softc;
269229997Sken
270229997Sken#ifdef CTLFEDEBUG
271229997Sken	printf("%s: entered\n", __func__);
272229997Sken#endif
273229997Sken
274273317Smav	mtx_lock(&ctlfe_list_mtx);
275273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
276273317Smav		if (softc->path_id == xpt_path_path_id(path))
277273317Smav			break;
278273317Smav	}
279273317Smav	mtx_unlock(&ctlfe_list_mtx);
280273317Smav
281229997Sken	/*
282229997Sken	 * When a new path gets registered, and it is capable of target
283229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
284229997Sken	 * selective, but for now this will be sufficient.
285229997Sken 	 */
286229997Sken	switch (code) {
287229997Sken	case AC_PATH_REGISTERED: {
288268677Smav		struct ctl_port *port;
289229997Sken		struct ccb_pathinq *cpi;
290229997Sken		int retval;
291229997Sken
292229997Sken		cpi = (struct ccb_pathinq *)arg;
293229997Sken
294229997Sken		/* Don't attach if it doesn't support target mode */
295229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
296230033Sken#ifdef CTLFEDEBUG
297229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
298229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
299230033Sken#endif
300229997Sken			break;
301229997Sken		}
302229997Sken
303273317Smav		if (softc != NULL) {
304273317Smav#ifdef CTLFEDEBUG
305273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
306273317Smav			       __func__, xpt_path_path_id(path));
307273317Smav#endif
308273317Smav			break;
309273317Smav		}
310273317Smav
311229997Sken#ifdef CTLFE_INIT_ENABLE
312229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
313229997Sken			union ccb *ccb;
314229997Sken
315229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
316229997Sken						  M_NOWAIT | M_ZERO);
317229997Sken			if (ccb == NULL) {
318229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
319229997Sken				return;
320229997Sken			}
321260387Sscottl			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
322229997Sken
323229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
324229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
325229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
326229997Sken
327229997Sken			xpt_action(ccb);
328229997Sken
329229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
330229997Sken			     CAM_REQ_CMP) {
331229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
332229997Sken				       "enable failed with status %#x\n",
333229997Sken				       __func__, cpi->dev_name,
334229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
335229997Sken				       ccb->ccb_h.status);
336229997Sken			} else {
337229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
338229997Sken				       "enable succeeded\n",
339229997Sken				       __func__, cpi->dev_name,
340229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
341229997Sken			}
342229997Sken
343229997Sken			free(ccb, M_TEMP);
344229997Sken
345229997Sken			break;
346229997Sken		} else {
347229997Sken			ctlfe_num_targets++;
348229997Sken		}
349229997Sken
350229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
351229997Sken		       ctlfe_num_targets);
352229997Sken#endif /* CTLFE_INIT_ENABLE */
353229997Sken
354229997Sken		/*
355229997Sken		 * We're in an interrupt context here, so we have to
356229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
357229997Sken		 * can't allocate memory.
358229997Sken		 */
359273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
360273317Smav		if (softc == NULL) {
361229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
362273317Smav			       __func__, sizeof(*softc));
363229997Sken			return;
364229997Sken		}
365229997Sken
366273317Smav		softc->path_id = cpi->ccb_h.path_id;
367288713Smav		softc->target_id = cpi->initiator_id;
368273317Smav		softc->sim = xpt_path_sim(path);
369290776Smav		softc->hba_misc = cpi->hba_misc;
370265641Smav		if (cpi->maxio != 0)
371273317Smav			softc->maxio = cpi->maxio;
372265641Smav		else
373273317Smav			softc->maxio = DFLTPHYS;
374273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
375273317Smav		STAILQ_INIT(&softc->lun_softc_list);
376229997Sken
377273317Smav		port = &softc->port;
378268677Smav		port->frontend = &ctlfe_frontend;
379229997Sken
380229997Sken		/*
381229997Sken		 * XXX KDM should we be more accurate here ?
382229997Sken		 */
383229997Sken		if (cpi->transport == XPORT_FC)
384268677Smav			port->port_type = CTL_PORT_FC;
385268694Smav		else if (cpi->transport == XPORT_SAS)
386268694Smav			port->port_type = CTL_PORT_SAS;
387229997Sken		else
388268677Smav			port->port_type = CTL_PORT_SCSI;
389229997Sken
390229997Sken		/* XXX KDM what should the real number be here? */
391268677Smav		port->num_requested_ctl_io = 4096;
392273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
393229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
394229997Sken		/*
395229997Sken		 * XXX KDM it would be nice to allocate storage in the
396229997Sken		 * frontend structure itself.
397229997Sken	 	 */
398273317Smav		port->port_name = softc->port_name;
399273319Smav		port->physical_port = cpi->bus_id;
400273319Smav		port->virtual_port = 0;
401268677Smav		port->port_online = ctlfe_online;
402268677Smav		port->port_offline = ctlfe_offline;
403273317Smav		port->onoff_arg = softc;
404268677Smav		port->lun_enable = ctlfe_lun_enable;
405268677Smav		port->lun_disable = ctlfe_lun_disable;
406273317Smav		port->targ_lun_arg = softc;
407275880Smav		port->fe_datamove = ctlfe_datamove;
408275880Smav		port->fe_done = ctlfe_done;
409229997Sken		/*
410229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
411229997Sken		 * number of targets supported.
412229997Sken		 */
413268677Smav		port->max_targets = cpi->max_target;
414268677Smav		port->max_target_id = cpi->max_target;
415288732Smav		port->targ_port = -1;
416229997Sken
417229997Sken		/*
418229997Sken		 * XXX KDM need to figure out whether we're the master or
419229997Sken		 * slave.
420229997Sken		 */
421230033Sken#ifdef CTLFEDEBUG
422268677Smav		printf("%s: calling ctl_port_register() for %s%d\n",
423229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
424230033Sken#endif
425275493Smav		retval = ctl_port_register(port);
426229997Sken		if (retval != 0) {
427268677Smav			printf("%s: ctl_port_register() failed with "
428229997Sken			       "error %d!\n", __func__, retval);
429273317Smav			mtx_destroy(&softc->lun_softc_mtx);
430273317Smav			free(softc, M_CTLFE);
431229997Sken			break;
432229997Sken		} else {
433229997Sken			mtx_lock(&ctlfe_list_mtx);
434273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
435229997Sken			mtx_unlock(&ctlfe_list_mtx);
436229997Sken		}
437229997Sken
438245228Sken		break;
439245228Sken	}
440245228Sken	case AC_PATH_DEREGISTERED: {
441245228Sken
442245228Sken		if (softc != NULL) {
443245228Sken			/*
444245228Sken			 * XXX KDM are we certain at this point that there
445245228Sken			 * are no outstanding commands for this frontend?
446245228Sken			 */
447273317Smav			mtx_lock(&ctlfe_list_mtx);
448273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
449273317Smav			    links);
450273317Smav			mtx_unlock(&ctlfe_list_mtx);
451268677Smav			ctl_port_deregister(&softc->port);
452260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
453245228Sken			free(softc, M_CTLFE);
454229997Sken		}
455229997Sken		break;
456229997Sken	}
457229997Sken	case AC_CONTRACT: {
458229997Sken		struct ac_contract *ac;
459229997Sken
460229997Sken		ac = (struct ac_contract *)arg;
461229997Sken
462229997Sken		switch (ac->contract_number) {
463229997Sken		case AC_CONTRACT_DEV_CHG: {
464229997Sken			struct ac_device_changed *dev_chg;
465273317Smav			int retval;
466229997Sken
467229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
468229997Sken
469236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
470229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
471229997Sken			       xpt_path_path_id(path), dev_chg->target,
472229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
473229997Sken
474273317Smav			if (softc == NULL) {
475229997Sken				printf("%s: CTL port for CAM path %u not "
476229997Sken				       "found!\n", __func__,
477229997Sken				       xpt_path_path_id(path));
478229997Sken				break;
479229997Sken			}
480229997Sken			if (dev_chg->arrived != 0) {
481268692Smav				retval = ctl_add_initiator(&softc->port,
482268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
483229997Sken			} else {
484268692Smav				retval = ctl_remove_initiator(&softc->port,
485268692Smav				    dev_chg->target);
486229997Sken			}
487229997Sken
488268692Smav			if (retval < 0) {
489229997Sken				printf("%s: could not %s port %d iid %u "
490229997Sken				       "WWPN %#jx!\n", __func__,
491229997Sken				       (dev_chg->arrived != 0) ? "add" :
492268677Smav				       "remove", softc->port.targ_port,
493229997Sken				       dev_chg->target,
494229997Sken				       (uintmax_t)dev_chg->wwpn);
495229997Sken			}
496229997Sken			break;
497229997Sken		}
498229997Sken		default:
499229997Sken			printf("%s: unsupported contract number %ju\n",
500229997Sken			       __func__, (uintmax_t)ac->contract_number);
501229997Sken			break;
502229997Sken		}
503229997Sken		break;
504229997Sken	}
505229997Sken	default:
506229997Sken		break;
507229997Sken	}
508229997Sken}
509229997Sken
510229997Skenstatic cam_status
511229997Skenctlferegister(struct cam_periph *periph, void *arg)
512229997Sken{
513229997Sken	struct ctlfe_softc *bus_softc;
514229997Sken	struct ctlfe_lun_softc *softc;
515229997Sken	union ccb en_lun_ccb;
516229997Sken	cam_status status;
517229997Sken	int i;
518229997Sken
519229997Sken	softc = (struct ctlfe_lun_softc *)arg;
520229997Sken	bus_softc = softc->parent_softc;
521229997Sken
522229997Sken	TAILQ_INIT(&softc->work_queue);
523229997Sken	softc->periph = periph;
524229997Sken	periph->softc = softc;
525229997Sken
526242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
527229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
528229997Sken	en_lun_ccb.cel.grp6_len = 0;
529229997Sken	en_lun_ccb.cel.grp7_len = 0;
530229997Sken	en_lun_ccb.cel.enable = 1;
531229997Sken	xpt_action(&en_lun_ccb);
532229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
533229997Sken	if (status != CAM_REQ_CMP) {
534229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
535229997Sken			  __func__, en_lun_ccb.ccb_h.status);
536229997Sken		return (status);
537229997Sken	}
538229997Sken
539229997Sken	status = CAM_REQ_CMP;
540229997Sken
541229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
542229997Sken		union ccb *new_ccb;
543275878Smav		union ctl_io *new_io;
544288723Smav		struct ctlfe_cmd_info *cmd_info;
545229997Sken
546229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
547236426Smjacob					      M_ZERO|M_NOWAIT);
548229997Sken		if (new_ccb == NULL) {
549229997Sken			status = CAM_RESRC_UNAVAIL;
550229997Sken			break;
551229997Sken		}
552275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
553275878Smav		if (new_io == NULL) {
554275878Smav			free(new_ccb, M_CTLFE);
555275878Smav			status = CAM_RESRC_UNAVAIL;
556275878Smav			break;
557275878Smav		}
558288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
559288723Smav		    M_ZERO | M_NOWAIT);
560288723Smav		if (cmd_info == NULL) {
561288723Smav			ctl_free_io(new_io);
562288723Smav			free(new_ccb, M_CTLFE);
563288723Smav			status = CAM_RESRC_UNAVAIL;
564288723Smav			break;
565288723Smav		}
566312585Smav		PRIV_INFO(new_io) = cmd_info;
567284793Smav		softc->atios_alloced++;
568275878Smav		new_ccb->ccb_h.io_ptr = new_io;
569275878Smav
570229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
571229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
572229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
573260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
574229997Sken		xpt_action(new_ccb);
575229997Sken		status = new_ccb->ccb_h.status;
576229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
577288723Smav			free(cmd_info, M_CTLFE);
578275878Smav			ctl_free_io(new_io);
579229997Sken			free(new_ccb, M_CTLFE);
580229997Sken			break;
581229997Sken		}
582229997Sken	}
583229997Sken
584229997Sken	status = cam_periph_acquire(periph);
585229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
586229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
587229997Sken			  "count, status = %#x\n", __func__, status);
588229997Sken		return (status);
589229997Sken	}
590229997Sken
591229997Sken	if (i == 0) {
592229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
593229997Sken			  "status 0x%x\n", __func__, status);
594229997Sken		return (CAM_REQ_CMP_ERR);
595229997Sken	}
596229997Sken
597229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
598229997Sken		union ccb *new_ccb;
599275878Smav		union ctl_io *new_io;
600229997Sken
601229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
602236426Smjacob					      M_ZERO|M_NOWAIT);
603229997Sken		if (new_ccb == NULL) {
604229997Sken			status = CAM_RESRC_UNAVAIL;
605229997Sken			break;
606229997Sken		}
607275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
608275878Smav		if (new_io == NULL) {
609275878Smav			free(new_ccb, M_CTLFE);
610275878Smav			status = CAM_RESRC_UNAVAIL;
611275878Smav			break;
612275878Smav		}
613284793Smav		softc->inots_alloced++;
614275878Smav		new_ccb->ccb_h.io_ptr = new_io;
615229997Sken
616229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
617229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
618229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
619260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
620229997Sken		xpt_action(new_ccb);
621229997Sken		status = new_ccb->ccb_h.status;
622237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
623237601Sken			/*
624237601Sken			 * Note that we don't free the CCB here.  If the
625237601Sken			 * status is not CAM_REQ_INPROG, then we're
626237601Sken			 * probably talking to a SIM that says it is
627237601Sken			 * target-capable but doesn't support the
628237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
629237601Sken			 * older API.  In that case, it'll call xpt_done()
630237601Sken			 * on the CCB, and we need to free it in our done
631237601Sken			 * routine as a result.
632237601Sken			 */
633229997Sken			break;
634229997Sken		}
635229997Sken	}
636237601Sken	if ((i == 0)
637237601Sken	 || (status != CAM_REQ_INPROG)) {
638229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
639229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
640229997Sken		return (CAM_REQ_CMP_ERR);
641229997Sken	}
642275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
643275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
644275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
645229997Sken	return (CAM_REQ_CMP);
646229997Sken}
647229997Sken
648229997Skenstatic void
649229997Skenctlfeoninvalidate(struct cam_periph *periph)
650229997Sken{
651229997Sken	union ccb en_lun_ccb;
652229997Sken	cam_status status;
653260387Sscottl	struct ctlfe_softc *bus_softc;
654229997Sken	struct ctlfe_lun_softc *softc;
655229997Sken
656229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
657229997Sken
658242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
659229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
660229997Sken	en_lun_ccb.cel.grp6_len = 0;
661229997Sken	en_lun_ccb.cel.grp7_len = 0;
662229997Sken	en_lun_ccb.cel.enable = 0;
663229997Sken	xpt_action(&en_lun_ccb);
664229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
665229997Sken	if (status != CAM_REQ_CMP) {
666229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
667229997Sken			  __func__, en_lun_ccb.ccb_h.status);
668229997Sken		/*
669229997Sken		 * XXX KDM what do we do now?
670229997Sken		 */
671229997Sken	}
672260387Sscottl
673260387Sscottl	bus_softc = softc->parent_softc;
674260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
675260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
676260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
677229997Sken}
678229997Sken
679229997Skenstatic void
680229997Skenctlfecleanup(struct cam_periph *periph)
681229997Sken{
682229997Sken	struct ctlfe_lun_softc *softc;
683229997Sken
684229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
685229997Sken
686284793Smav	KASSERT(softc->ccbs_freed == softc->ccbs_alloced, ("%s: "
687284793Smav		"ccbs_freed %ju != ccbs_alloced %ju", __func__,
688284793Smav		softc->ccbs_freed, softc->ccbs_alloced));
689284793Smav	KASSERT(softc->ctios_returned == softc->ctios_sent, ("%s: "
690284793Smav		"ctios_returned %ju != ctios_sent %ju", __func__,
691284793Smav		softc->ctios_returned, softc->ctios_sent));
692284793Smav	KASSERT(softc->atios_freed == softc->atios_alloced, ("%s: "
693284793Smav		"atios_freed %ju != atios_alloced %ju", __func__,
694284793Smav		softc->atios_freed, softc->atios_alloced));
695284793Smav	KASSERT(softc->inots_freed == softc->inots_alloced, ("%s: "
696284793Smav		"inots_freed %ju != inots_alloced %ju", __func__,
697284793Smav		softc->inots_freed, softc->inots_alloced));
698245228Sken
699229997Sken	free(softc, M_CTLFE);
700229997Sken}
701229997Sken
702229997Skenstatic void
703265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
704265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
705265641Smav    u_int16_t *sglist_cnt)
706265641Smav{
707265641Smav	struct ctlfe_softc *bus_softc;
708288723Smav	struct ctlfe_cmd_info *cmd_info;
709265641Smav	struct ctl_sg_entry *ctl_sglist;
710265641Smav	bus_dma_segment_t *cam_sglist;
711265641Smav	size_t off;
712265641Smav	int i, idx;
713265641Smav
714312585Smav	cmd_info = PRIV_INFO(io);
715265641Smav	bus_softc = softc->parent_softc;
716265641Smav
717265641Smav	/*
718265641Smav	 * Set the direction, relative to the initiator.
719265641Smav	 */
720265641Smav	*flags &= ~CAM_DIR_MASK;
721265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
722265641Smav		*flags |= CAM_DIR_IN;
723265641Smav	else
724265641Smav		*flags |= CAM_DIR_OUT;
725265641Smav
726265641Smav	*flags &= ~CAM_DATA_MASK;
727265641Smav	idx = cmd_info->cur_transfer_index;
728265641Smav	off = cmd_info->cur_transfer_off;
729265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
730313365Smav	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
731313365Smav
732313365Smav		/* One time shift for SRR offset. */
733313365Smav		off += io->scsiio.ext_data_filled;
734313365Smav		io->scsiio.ext_data_filled = 0;
735313365Smav
736265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
737265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
738265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
739265641Smav		} else {
740265641Smav			*dxfer_len = bus_softc->maxio;
741313365Smav			cmd_info->cur_transfer_off += bus_softc->maxio;
742265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
743265641Smav		}
744265641Smav		*sglist_cnt = 0;
745265641Smav
746265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
747265641Smav			*flags |= CAM_DATA_PADDR;
748265641Smav		else
749265641Smav			*flags |= CAM_DATA_VADDR;
750313365Smav	} else {	/* S/G list with physical or virtual pointers. */
751265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
752313365Smav
753313365Smav		/* One time shift for SRR offset. */
754313365Smav		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
755313365Smav			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
756313365Smav			idx++;
757313365Smav			off = 0;
758313365Smav		}
759313365Smav		off += io->scsiio.ext_data_filled;
760313365Smav		io->scsiio.ext_data_filled = 0;
761313365Smav
762265641Smav		cam_sglist = cmd_info->cam_sglist;
763265641Smav		*dxfer_len = 0;
764265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
765265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
766265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
767265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
768265641Smav				*dxfer_len += cam_sglist[i].ds_len;
769265641Smav			} else {
770265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
771265641Smav				cmd_info->cur_transfer_index = idx + i;
772265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
773265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
774265641Smav				*dxfer_len += cam_sglist[i].ds_len;
775265641Smav				if (ctl_sglist[i].len != 0)
776265641Smav					i++;
777265641Smav				break;
778265641Smav			}
779265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
780265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
781265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
782265641Smav				cmd_info->cur_transfer_off = 0;
783265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
784265641Smav				i++;
785265641Smav				break;
786265641Smav			}
787265641Smav			off = 0;
788265641Smav		}
789265641Smav		*sglist_cnt = i;
790265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
791265641Smav			*flags |= CAM_DATA_SG_PADDR;
792265641Smav		else
793265641Smav			*flags |= CAM_DATA_SG;
794265641Smav		*data_ptr = (uint8_t *)cam_sglist;
795265641Smav	}
796265641Smav}
797265641Smav
798265641Smavstatic void
799229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
800229997Sken{
801229997Sken	struct ctlfe_lun_softc *softc;
802288723Smav	struct ctlfe_cmd_info *cmd_info;
803229997Sken	struct ccb_hdr *ccb_h;
804275880Smav	struct ccb_accept_tio *atio;
805275880Smav	struct ccb_scsiio *csio;
806275880Smav	uint8_t *data_ptr;
807275880Smav	uint32_t dxfer_len;
808275880Smav	ccb_flags flags;
809275880Smav	union ctl_io *io;
810275880Smav	uint8_t scsi_status;
811229997Sken
812229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
813229997Sken	softc->ccbs_alloced++;
814229997Sken
815314739Smavnext:
816229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
817260387Sscottl	if (ccb_h == NULL) {
818229997Sken		softc->ccbs_freed++;
819229997Sken		xpt_release_ccb(start_ccb);
820275880Smav		return;
821275880Smav	}
822229997Sken
823275880Smav	/* Take the ATIO off the work queue */
824275880Smav	TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
825275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
826275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
827275880Smav	csio = &start_ccb->csio;
828229997Sken
829275880Smav	flags = atio->ccb_h.flags &
830275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
831312585Smav	cmd_info = PRIV_INFO(io);
832275881Smav	cmd_info->cur_transfer_index = 0;
833275881Smav	cmd_info->cur_transfer_off = 0;
834275881Smav	cmd_info->flags = 0;
835229997Sken
836275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
837275880Smav		/*
838275880Smav		 * Datamove call, we need to setup the S/G list.
839275880Smav		 */
840275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
841275880Smav		    &csio->sglist_cnt);
842275880Smav	} else {
843275880Smav		/*
844275880Smav		 * We're done, send status back.
845275880Smav		 */
846275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
847275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
848275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
849275880Smav
850314737Smav			/* Tell the SIM that we've aborted this ATIO */
851314737Smav#ifdef CTLFEDEBUG
852314737Smav			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
853314737Smav#endif
854314737Smav			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
855314737Smav			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
856275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
857275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
858275880Smav			xpt_action(start_ccb);
859229997Sken
860314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
861314739Smav			    /* unlock */0);
862229997Sken
863314739Smav			/* XPT_ABORT is not queued, so we can take next I/O. */
864314739Smav			goto next;
865275880Smav		}
866275881Smav		data_ptr = NULL;
867275881Smav		dxfer_len = 0;
868275881Smav		csio->sglist_cnt = 0;
869275881Smav	}
870313365Smav	scsi_status = 0;
871275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
872275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
873275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
874275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
875275880Smav		flags |= CAM_SEND_STATUS;
876275880Smav		scsi_status = io->scsiio.scsi_status;
877275880Smav		csio->sense_len = io->scsiio.sense_len;
878275880Smav#ifdef CTLFEDEBUG
879275880Smav		printf("%s: tag %04x status %x\n", __func__,
880275880Smav		       atio->tag_id, io->io_hdr.status);
881275880Smav#endif
882275880Smav		if (csio->sense_len != 0) {
883275880Smav			csio->sense_data = io->scsiio.sense_data;
884275880Smav			flags |= CAM_SEND_SENSE;
885229997Sken		}
886275880Smav	}
887229997Sken
888229997Sken#ifdef CTLFEDEBUG
889275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
890275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
891275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
892229997Sken#endif
893229997Sken
894275880Smav	/*
895275880Smav	 * Valid combinations:
896275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
897275880Smav	 *    sglist_cnt = 0
898275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
899275880Smav	 *    sglist_cnt = 0
900275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
901275880Smav	 *    sglist_cnt != 0
902275880Smav	 */
903229997Sken#ifdef CTLFEDEBUG
904275880Smav	if (((flags & CAM_SEND_STATUS)
905275880Smav	  && (((flags & CAM_DATA_SG) != 0)
906275880Smav	   || (dxfer_len != 0)
907275880Smav	   || (csio->sglist_cnt != 0)))
908275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
909275880Smav	  && (dxfer_len == 0))
910275880Smav	 || ((flags & CAM_DATA_SG)
911275880Smav	  && (csio->sglist_cnt == 0))
912275880Smav	 || (((flags & CAM_DATA_SG) == 0)
913275880Smav	  && (csio->sglist_cnt != 0))) {
914275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
915275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
916312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
917275880Smav		       csio->sglist_cnt);
918275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
919275880Smav		       atio->tag_id, io->io_hdr.status);
920275880Smav	}
921229997Sken#endif
922275880Smav	cam_fill_ctio(csio,
923275880Smav		      /*retries*/ 2,
924275880Smav		      ctlfedone,
925275880Smav		      flags,
926275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
927275880Smav		      atio->tag_id,
928275880Smav		      atio->init_id,
929275880Smav		      scsi_status,
930275880Smav		      /*data_ptr*/ data_ptr,
931275880Smav		      /*dxfer_len*/ dxfer_len,
932275880Smav		      /*timeout*/ 5 * 1000);
933275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
934275880Smav	start_ccb->ccb_h.ccb_atio = atio;
935275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
936275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
937275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
938229997Sken
939275880Smav	softc->ctios_sent++;
940229997Sken
941275880Smav	cam_periph_unlock(periph);
942275880Smav	xpt_action(start_ccb);
943275880Smav	cam_periph_lock(periph);
944229997Sken
945229997Sken	/*
946275880Smav	 * If we still have work to do, ask for another CCB.
947229997Sken	 */
948275880Smav	if (!TAILQ_EMPTY(&softc->work_queue))
949229997Sken		xpt_schedule(periph, /*priority*/ 1);
950229997Sken}
951229997Sken
952229997Skenstatic void
953229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
954229997Sken{
955229997Sken	struct ctlfe_lun_softc *softc;
956288723Smav	union ctl_io *io;
957288723Smav	struct ctlfe_cmd_info *cmd_info;
958229997Sken
959229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
960288723Smav	io = ccb->ccb_h.io_ptr;
961229997Sken
962229997Sken	switch (ccb->ccb_h.func_code) {
963229997Sken	case XPT_ACCEPT_TARGET_IO:
964284793Smav		softc->atios_freed++;
965312585Smav		cmd_info = PRIV_INFO(io);
966288723Smav		free(cmd_info, M_CTLFE);
967229997Sken		break;
968229997Sken	case XPT_IMMEDIATE_NOTIFY:
969229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
970284793Smav		softc->inots_freed++;
971229997Sken		break;
972229997Sken	default:
973229997Sken		break;
974229997Sken	}
975229997Sken
976288723Smav	ctl_free_io(io);
977229997Sken	free(ccb, M_CTLFE);
978229997Sken
979284793Smav	KASSERT(softc->atios_freed <= softc->atios_alloced, ("%s: "
980284793Smav		"atios_freed %ju > atios_alloced %ju", __func__,
981284793Smav		softc->atios_freed, softc->atios_alloced));
982284793Smav	KASSERT(softc->inots_freed <= softc->inots_alloced, ("%s: "
983284793Smav		"inots_freed %ju > inots_alloced %ju", __func__,
984284793Smav		softc->inots_freed, softc->inots_alloced));
985229997Sken
986229997Sken	/*
987229997Sken	 * If we have received all of our CCBs, we can release our
988229997Sken	 * reference on the peripheral driver.  It will probably go away
989229997Sken	 * now.
990229997Sken	 */
991284793Smav	if ((softc->atios_freed == softc->atios_alloced)
992284793Smav	 && (softc->inots_freed == softc->inots_alloced)) {
993229997Sken		cam_periph_release_locked(periph);
994229997Sken	}
995229997Sken}
996229997Sken
997314739Smav/*
998314739Smav * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
999314739Smav */
1000314739Smavstatic void
1001314739Smavctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
1002314739Smav{
1003314739Smav	struct ctlfe_lun_softc *softc;
1004314739Smav
1005314739Smav	if (periph->flags & CAM_PERIPH_INVALID) {
1006314739Smav		ctlfe_free_ccb(periph, ccb);
1007314739Smav		if (unlock)
1008314739Smav			cam_periph_unlock(periph);
1009314739Smav		return;
1010314739Smav	}
1011314739Smav	if (unlock)
1012314739Smav		cam_periph_unlock(periph);
1013314739Smav
1014314739Smav	/*
1015314739Smav	 * For a wildcard attachment, commands can come in with a specific
1016314739Smav	 * target/lun.  Reset the target and LUN fields back to the wildcard
1017314739Smav	 * values before we send them back down to the SIM.
1018314739Smav	 */
1019314739Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1020314739Smav	if (softc->flags & CTLFE_LUN_WILDCARD) {
1021314739Smav		ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
1022314739Smav		ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
1023314739Smav	}
1024314739Smav
1025314739Smav	xpt_action(ccb);
1026314739Smav}
1027314739Smav
1028238870Smjacobstatic int
1029238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1030238870Smjacob{
1031238870Smjacob	uint64_t lba;
1032238870Smjacob	uint32_t num_blocks, nbc;
1033312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
1034238870Smjacob
1035238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1036238870Smjacob
1037238870Smjacob	switch (cmdbyt[0]) {
1038238870Smjacob	case READ_6:
1039238870Smjacob	case WRITE_6:
1040238870Smjacob	{
1041238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1042238870Smjacob		lba = scsi_3btoul(cdb->addr);
1043238870Smjacob		lba &= 0x1fffff;
1044238870Smjacob		num_blocks = cdb->length;
1045238870Smjacob		if (num_blocks == 0)
1046238870Smjacob			num_blocks = 256;
1047238870Smjacob		lba += nbc;
1048238870Smjacob		num_blocks -= nbc;
1049238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1050238870Smjacob		cdb->length = num_blocks;
1051238870Smjacob		break;
1052238870Smjacob	}
1053238870Smjacob	case READ_10:
1054238870Smjacob	case WRITE_10:
1055238870Smjacob	{
1056238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1057238870Smjacob		lba = scsi_4btoul(cdb->addr);
1058238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1059238870Smjacob		lba += nbc;
1060238870Smjacob		num_blocks -= nbc;
1061238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1062238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1063238870Smjacob		break;
1064238870Smjacob	}
1065238870Smjacob	case READ_12:
1066238870Smjacob	case WRITE_12:
1067238870Smjacob	{
1068238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1069238870Smjacob		lba = scsi_4btoul(cdb->addr);
1070238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1071238870Smjacob		lba += nbc;
1072238870Smjacob		num_blocks -= nbc;
1073238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1074238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1075238870Smjacob		break;
1076238870Smjacob	}
1077238870Smjacob	case READ_16:
1078238870Smjacob	case WRITE_16:
1079238870Smjacob	{
1080238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1081238870Smjacob		lba = scsi_8btou64(cdb->addr);
1082238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1083238870Smjacob		lba += nbc;
1084238870Smjacob		num_blocks -= nbc;
1085238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1086238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1087238870Smjacob		break;
1088238870Smjacob	}
1089238870Smjacob	default:
1090238870Smjacob		return -1;
1091238870Smjacob	}
1092238870Smjacob	return (0);
1093238870Smjacob}
1094238870Smjacob
1095229997Skenstatic void
1096229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1097229997Sken{
1098229997Sken	struct ctlfe_lun_softc *softc;
1099229997Sken	struct ctlfe_softc *bus_softc;
1100288723Smav	struct ctlfe_cmd_info *cmd_info;
1101238870Smjacob	struct ccb_accept_tio *atio = NULL;
1102238870Smjacob	union ctl_io *io = NULL;
1103260387Sscottl	struct mtx *mtx;
1104314727Smav	cam_status status;
1105229997Sken
1106260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1107260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1108229997Sken#ifdef CTLFE_DEBUG
1109275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1110275878Smav	       done_ccb->ccb_h.func_code);
1111229997Sken#endif
1112229997Sken
1113284794Smav	/*
1114284794Smav	 * At this point CTL has no known use case for device queue freezes.
1115284794Smav	 * In case some SIM think different -- drop its freeze right here.
1116284794Smav	 */
1117284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1118284794Smav		cam_release_devq(periph->path,
1119284794Smav				 /*relsim_flags*/0,
1120284794Smav				 /*reduction*/0,
1121284794Smav				 /*timeout*/0,
1122284794Smav				 /*getcount_only*/0);
1123284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1124284794Smav	}
1125284794Smav
1126229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1127229997Sken	bus_softc = softc->parent_softc;
1128260387Sscottl	mtx = cam_periph_mtx(periph);
1129260387Sscottl	mtx_lock(mtx);
1130229997Sken
1131229997Sken	switch (done_ccb->ccb_h.func_code) {
1132229997Sken	case XPT_ACCEPT_TARGET_IO: {
1133229997Sken
1134229997Sken		atio = &done_ccb->atio;
1135314727Smav		status = atio->ccb_h.status & CAM_STATUS_MASK;
1136314727Smav		if (status != CAM_CDB_RECVD) {
1137314727Smav			ctlfe_free_ccb(periph, done_ccb);
1138314727Smav			goto out;
1139314727Smav		}
1140229997Sken
1141238870Smjacob resubmit:
1142229997Sken		/*
1143229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1144229997Sken		 * datamove or done.
1145229997Sken		 */
1146260387Sscottl		mtx_unlock(mtx);
1147275878Smav		io = done_ccb->ccb_h.io_ptr;
1148312585Smav		cmd_info = PRIV_INFO(io);
1149229997Sken		ctl_zero_io(io);
1150229997Sken
1151229997Sken		/* Save pointers on both sides */
1152312585Smav		PRIV_CCB(io) = done_ccb;
1153312585Smav		PRIV_INFO(io) = cmd_info;
1154229997Sken		done_ccb->ccb_h.io_ptr = io;
1155229997Sken
1156229997Sken		/*
1157229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1158229997Sken		 * down the immediate notify path below.
1159229997Sken		 */
1160229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1161288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1162268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1163290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1164290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1165290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1166290776Smav		} else {
1167290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1168290776Smav		}
1169229997Sken		io->scsiio.tag_num = atio->tag_id;
1170229997Sken		switch (atio->tag_action) {
1171229997Sken		case CAM_TAG_ACTION_NONE:
1172229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1173229997Sken			break;
1174229997Sken		case MSG_SIMPLE_TASK:
1175229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1176229997Sken			break;
1177229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1178229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1179229997Sken			break;
1180229997Sken		case MSG_ORDERED_TASK:
1181229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1182229997Sken			break;
1183229997Sken		case MSG_ACA_TASK:
1184229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1185229997Sken			break;
1186229997Sken		default:
1187229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1188229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1189229997Sken			       atio->tag_action);
1190229997Sken			break;
1191229997Sken		}
1192229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1193229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1194229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1195229997Sken		}
1196229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1197312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1198229997Sken
1199229997Sken#ifdef CTLFEDEBUG
1200288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1201288731Smav		        io->io_hdr.nexus.initid,
1202229997Sken		        io->io_hdr.nexus.targ_port,
1203229997Sken		        io->io_hdr.nexus.targ_lun,
1204229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1205229997Sken#endif
1206229997Sken
1207229997Sken		ctl_queue(io);
1208260387Sscottl		return;
1209229997Sken	}
1210229997Sken	case XPT_CONT_TARGET_IO: {
1211238870Smjacob		int srr = 0;
1212238870Smjacob		uint32_t srr_off = 0;
1213229997Sken
1214229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1215229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1216229997Sken
1217229997Sken		softc->ctios_returned++;
1218229997Sken#ifdef CTLFEDEBUG
1219229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1220229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1221229997Sken#endif
1222229997Sken		/*
1223238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1224238870Smjacob		 */
1225238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1226238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1227238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1228238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1229238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1230238870Smjacob			srr = 1;
1231238870Smjacob			srr_off =
1232238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1233238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1234238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1235238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1236238870Smjacob		}
1237238870Smjacob
1238313365Smav		/*
1239313365Smav		 * If we have an SRR and we're still sending data, we
1240313365Smav		 * should be able to adjust offsets and cycle again.
1241313365Smav		 * It is possible only if offset is from this datamove.
1242313365Smav		 */
1243313365Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1244313365Smav		    srr_off >= io->scsiio.kern_rel_offset &&
1245313365Smav		    srr_off < io->scsiio.kern_rel_offset +
1246313365Smav		     io->scsiio.kern_data_len) {
1247313365Smav			io->scsiio.kern_data_resid =
1248313365Smav			    io->scsiio.kern_rel_offset +
1249313365Smav			    io->scsiio.kern_data_len - srr_off;
1250313365Smav			io->scsiio.ext_data_filled = srr_off;
1251313365Smav			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1252313365Smav			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1253313365Smav			softc->ccbs_freed++;
1254313365Smav			xpt_release_ccb(done_ccb);
1255313365Smav			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1256313365Smav					  periph_links.tqe);
1257313365Smav			xpt_schedule(periph, /*priority*/ 1);
1258313365Smav			break;
1259313365Smav		}
1260313365Smav
1261313365Smav		/*
1262313365Smav		 * If status was being sent, the back end data is now history.
1263313365Smav		 * Hack it up and resubmit a new command with the CDB adjusted.
1264313365Smav		 * If the SIM does the right thing, all of the resid math
1265313365Smav		 * should work.
1266313365Smav		 */
1267275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1268238870Smjacob			softc->ccbs_freed++;
1269238870Smjacob			xpt_release_ccb(done_ccb);
1270238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1271238870Smjacob				done_ccb = (union ccb *)atio;
1272238870Smjacob				goto resubmit;
1273238870Smjacob			}
1274238870Smjacob			/*
1275238870Smjacob			 * Fall through to doom....
1276238870Smjacob			 */
1277238870Smjacob		}
1278238870Smjacob
1279277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1280277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1281277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1282277919Smav
1283238870Smjacob		/*
1284229997Sken		 * If we were sending status back to the initiator, free up
1285229997Sken		 * resources.  If we were doing a datamove, call the
1286229997Sken		 * datamove done routine.
1287229997Sken		 */
1288275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1289229997Sken			softc->ccbs_freed++;
1290229997Sken			xpt_release_ccb(done_ccb);
1291314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
1292314739Smav			    /* unlock */1);
1293314739Smav			return;
1294229997Sken		} else {
1295288723Smav			struct ctlfe_cmd_info *cmd_info;
1296229997Sken			struct ccb_scsiio *csio;
1297229997Sken
1298229997Sken			csio = &done_ccb->csio;
1299312585Smav			cmd_info = PRIV_INFO(io);
1300229997Sken
1301229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1302229997Sken
1303229997Sken			/*
1304229997Sken			 * Translate CAM status to CTL status.  Success
1305229997Sken			 * does not change the overall, ctl_io status.  In
1306229997Sken			 * that case we just set port_status to 0.  If we
1307229997Sken			 * have a failure, though, set a data phase error
1308229997Sken			 * for the overall ctl_io.
1309229997Sken			 */
1310229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1311229997Sken			case CAM_REQ_CMP:
1312313365Smav				io->scsiio.kern_data_resid -= csio->dxfer_len;
1313229997Sken				io->io_hdr.port_status = 0;
1314229997Sken				break;
1315229997Sken			default:
1316229997Sken				/*
1317245228Sken				 * XXX KDM we probably need to figure out a
1318245228Sken				 * standard set of errors that the SIM
1319245228Sken				 * drivers should return in the event of a
1320245228Sken				 * data transfer failure.  A data phase
1321245228Sken				 * error will at least point the user to a
1322245228Sken				 * data transfer error of some sort.
1323245228Sken				 * Hopefully the SIM printed out some
1324245228Sken				 * additional information to give the user
1325245228Sken				 * a clue what happened.
1326229997Sken				 */
1327229997Sken				io->io_hdr.port_status = 0xbad1;
1328229997Sken				ctl_set_data_phase_error(&io->scsiio);
1329229997Sken				/*
1330229997Sken				 * XXX KDM figure out residual.
1331229997Sken				 */
1332229997Sken				break;
1333229997Sken			}
1334229997Sken			/*
1335229997Sken			 * If we had to break this S/G list into multiple
1336229997Sken			 * pieces, figure out where we are in the list, and
1337229997Sken			 * continue sending pieces if necessary.
1338229997Sken			 */
1339229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1340265641Smav			 && (io->io_hdr.port_status == 0)) {
1341229997Sken				ccb_flags flags;
1342229997Sken				uint8_t *data_ptr;
1343229997Sken				uint32_t dxfer_len;
1344229997Sken
1345229997Sken				flags = atio->ccb_h.flags &
1346229997Sken					(CAM_DIS_DISCONNECT|
1347265641Smav					 CAM_TAG_ACTION_VALID);
1348229997Sken
1349265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1350265641Smav				    &dxfer_len, &csio->sglist_cnt);
1351229997Sken
1352229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1353229997Sken				 && (dxfer_len == 0)) {
1354229997Sken					printf("%s: tag %04x no status or "
1355229997Sken					       "len cdb = %02x\n", __func__,
1356229997Sken					       atio->tag_id,
1357312845Smav					       atio_cdb_ptr(atio)[0]);
1358229997Sken					printf("%s: tag %04x io status %#x\n",
1359229997Sken					       __func__, atio->tag_id,
1360229997Sken					       io->io_hdr.status);
1361229997Sken				}
1362229997Sken
1363229997Sken				cam_fill_ctio(csio,
1364229997Sken					      /*retries*/ 2,
1365229997Sken					      ctlfedone,
1366229997Sken					      flags,
1367229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1368229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1369229997Sken					      atio->tag_id,
1370229997Sken					      atio->init_id,
1371313365Smav					      0,
1372229997Sken					      /*data_ptr*/ data_ptr,
1373229997Sken					      /*dxfer_len*/ dxfer_len,
1374229997Sken					      /*timeout*/ 5 * 1000);
1375229997Sken
1376260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1377229997Sken				csio->resid = 0;
1378229997Sken				csio->ccb_h.ccb_atio = atio;
1379229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1380229997Sken				softc->ctios_sent++;
1381260387Sscottl				mtx_unlock(mtx);
1382229997Sken				xpt_action((union ccb *)csio);
1383229997Sken			} else {
1384229997Sken				/*
1385229997Sken				 * Release the CTIO.  The ATIO will be sent back
1386229997Sken				 * down to the SIM once we send status.
1387229997Sken				 */
1388229997Sken				softc->ccbs_freed++;
1389229997Sken				xpt_release_ccb(done_ccb);
1390260387Sscottl				mtx_unlock(mtx);
1391229997Sken
1392229997Sken				/* Call the backend move done callback */
1393229997Sken				io->scsiio.be_move_done(io);
1394229997Sken			}
1395260387Sscottl			return;
1396229997Sken		}
1397229997Sken		break;
1398229997Sken	}
1399229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1400229997Sken		union ctl_io *io;
1401229997Sken		struct ccb_immediate_notify *inot;
1402284794Smav		int send_ctl_io;
1403229997Sken
1404229997Sken		inot = &done_ccb->cin1;
1405275878Smav		io = done_ccb->ccb_h.io_ptr;
1406275878Smav		ctl_zero_io(io);
1407229997Sken
1408275878Smav		send_ctl_io = 1;
1409229997Sken
1410275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1411312585Smav		PRIV_CCB(io) = done_ccb;
1412275878Smav		inot->ccb_h.io_ptr = io;
1413288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1414275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1415290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1416290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1417290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1418290776Smav		} else {
1419290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1420290776Smav		}
1421275878Smav		/* XXX KDM should this be the tag_id? */
1422275878Smav		io->taskio.tag_num = inot->seq_id;
1423229997Sken
1424275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1425275878Smav		switch (status) {
1426275878Smav		case CAM_SCSI_BUS_RESET:
1427275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1428275878Smav			break;
1429275878Smav		case CAM_BDR_SENT:
1430275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1431275878Smav			break;
1432275878Smav		case CAM_MESSAGE_RECV:
1433275878Smav			switch (inot->arg) {
1434275878Smav			case MSG_ABORT_TASK_SET:
1435275878Smav				io->taskio.task_action =
1436275878Smav				    CTL_TASK_ABORT_TASK_SET;
1437229997Sken				break;
1438275878Smav			case MSG_TARGET_RESET:
1439290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1440229997Sken				break;
1441275878Smav			case MSG_ABORT_TASK:
1442290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1443229997Sken				break;
1444275878Smav			case MSG_LOGICAL_UNIT_RESET:
1445290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1446275878Smav				break;
1447275878Smav			case MSG_CLEAR_TASK_SET:
1448275878Smav				io->taskio.task_action =
1449290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1450275878Smav				break;
1451275878Smav			case MSG_CLEAR_ACA:
1452290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1453290775Smav				break;
1454290775Smav			case MSG_QUERY_TASK:
1455290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1456290775Smav				break;
1457290775Smav			case MSG_QUERY_TASK_SET:
1458275878Smav				io->taskio.task_action =
1459290775Smav				    CTL_TASK_QUERY_TASK_SET;
1460275878Smav				break;
1461290775Smav			case MSG_QUERY_ASYNC_EVENT:
1462290775Smav				io->taskio.task_action =
1463290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1464290775Smav				break;
1465275878Smav			case MSG_NOOP:
1466229997Sken				send_ctl_io = 0;
1467229997Sken				break;
1468229997Sken			default:
1469275878Smav				xpt_print(periph->path,
1470314727Smav				    "%s: unsupported INOT message 0x%x\n",
1471314727Smav				    __func__, inot->arg);
1472275878Smav				send_ctl_io = 0;
1473275878Smav				break;
1474275878Smav			}
1475275878Smav			break;
1476314727Smav		default:
1477314727Smav			xpt_print(periph->path,
1478314727Smav			    "%s: unsupported INOT status 0x%x\n",
1479314727Smav			    __func__, status);
1480314727Smav			/* FALLTHROUGH */
1481275878Smav		case CAM_REQ_ABORTED:
1482275878Smav		case CAM_REQ_INVALID:
1483314727Smav		case CAM_DEV_NOT_THERE:
1484275878Smav		case CAM_PROVIDE_FAIL:
1485275878Smav			ctlfe_free_ccb(periph, done_ccb);
1486275878Smav			goto out;
1487275878Smav		}
1488275878Smav		if (send_ctl_io != 0) {
1489275878Smav			ctl_queue(io);
1490229997Sken		} else {
1491229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1492229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1493229997Sken			xpt_action(done_ccb);
1494229997Sken		}
1495229997Sken		break;
1496229997Sken	}
1497229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1498314739Smav		/* Queue this back down to the SIM as an immediate notify. */
1499304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1500229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1501314739Smav		ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1502314739Smav		return;
1503229997Sken	case XPT_SET_SIM_KNOB:
1504229997Sken	case XPT_GET_SIM_KNOB:
1505229997Sken		break;
1506229997Sken	default:
1507229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1508229997Sken		      done_ccb->ccb_h.func_code);
1509229997Sken		break;
1510229997Sken	}
1511260387Sscottl
1512260387Sscottlout:
1513260387Sscottl	mtx_unlock(mtx);
1514229997Sken}
1515229997Sken
1516229997Skenstatic void
1517229997Skenctlfe_onoffline(void *arg, int online)
1518229997Sken{
1519229997Sken	struct ctlfe_softc *bus_softc;
1520229997Sken	union ccb *ccb;
1521229997Sken	cam_status status;
1522229997Sken	struct cam_path *path;
1523229997Sken	int set_wwnn;
1524229997Sken
1525229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1526229997Sken
1527229997Sken	set_wwnn = 0;
1528229997Sken
1529229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1530229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1531229997Sken	if (status != CAM_REQ_CMP) {
1532229997Sken		printf("%s: unable to create path!\n", __func__);
1533229997Sken		return;
1534229997Sken	}
1535275882Smav	ccb = xpt_alloc_ccb();
1536242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1537288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1538288713Smav	xpt_action(ccb);
1539229997Sken
1540229997Sken	/*
1541229997Sken	 * Copan WWN format:
1542229997Sken	 *
1543229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1544229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1545229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1546229997Sken	 * Bits 11-8:			Type of port:
1547229997Sken	 *					1 == N-Port
1548229997Sken	 *					2 == F-Port
1549229997Sken	 *					3 == NL-Port
1550229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1551229997Sken	 */
1552229997Sken	if (online != 0) {
1553229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1554229997Sken#ifdef RANDOM_WWNN
1555229997Sken			uint64_t random_bits;
1556229997Sken#endif
1557229997Sken
1558229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1559229997Sken			       bus_softc->port_name,
1560229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1561229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1562229997Sken			       bus_softc->port_name,
1563229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1564229997Sken
1565229997Sken#ifdef RANDOM_WWNN
1566229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1567229997Sken#endif
1568229997Sken
1569229997Sken			/*
1570229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1571229997Sken			 * take the current WWNN/WWPN from the card, and
1572229997Sken			 * replace the company identifier and the NL-Port
1573229997Sken			 * indicator and the port number (for the WWPN).
1574229997Sken			 * This should be replaced later with ddb_GetWWNN,
1575229997Sken			 * or possibly a more centralized scheme.  (It
1576229997Sken			 * would be nice to have the WWNN/WWPN for each
1577268677Smav			 * port stored in the ctl_port structure.)
1578229997Sken			 */
1579229997Sken#ifdef RANDOM_WWNN
1580229997Sken			ccb->knob.xport_specific.fc.wwnn =
1581229997Sken				(random_bits &
1582229997Sken				0x0000000fffffff00ULL) |
1583229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1584229997Sken				/* NL-Port */    0x0300;
1585229997Sken			ccb->knob.xport_specific.fc.wwpn =
1586229997Sken				(random_bits &
1587229997Sken				0x0000000fffffff00ULL) |
1588229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1589229997Sken				/* NL-Port */    0x3000 |
1590268677Smav				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1591229997Sken
1592229997Sken			/*
1593229997Sken			 * This is a bit of an API break/reversal, but if
1594229997Sken			 * we're doing the random WWNN that's a little
1595229997Sken			 * different anyway.  So record what we're actually
1596229997Sken			 * using with the frontend code so it's reported
1597229997Sken			 * accurately.
1598229997Sken			 */
1599268683Smav			ctl_port_set_wwns(&bus_softc->port,
1600268683Smav			    true, ccb->knob.xport_specific.fc.wwnn,
1601268683Smav			    true, ccb->knob.xport_specific.fc.wwpn);
1602229997Sken			set_wwnn = 1;
1603229997Sken#else /* RANDOM_WWNN */
1604229997Sken			/*
1605229997Sken			 * If the user has specified a WWNN/WWPN, send them
1606229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1607229997Sken			 * has reported.
1608229997Sken			 */
1609284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1610284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1611229997Sken				ccb->knob.xport_specific.fc.wwnn =
1612284586Smav				    bus_softc->port.wwnn;
1613284586Smav				set_wwnn = 1;
1614284586Smav			} else {
1615284586Smav				ctl_port_set_wwns(&bus_softc->port,
1616284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1617284586Smav				    false, 0);
1618284586Smav			}
1619284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1620284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1621229997Sken				ccb->knob.xport_specific.fc.wwpn =
1622284586Smav				    bus_softc->port.wwpn;
1623229997Sken				set_wwnn = 1;
1624229997Sken			} else {
1625268683Smav				ctl_port_set_wwns(&bus_softc->port,
1626284586Smav				    false, 0,
1627268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1628229997Sken			}
1629229997Sken#endif /* RANDOM_WWNN */
1630229997Sken
1631229997Sken
1632229997Sken			if (set_wwnn != 0) {
1633229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1634229997Sken				       bus_softc->port_name,
1635229997Sken				ccb->knob.xport_specific.fc.wwnn);
1636229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1637229997Sken				       bus_softc->port_name,
1638229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1639229997Sken			}
1640229997Sken		} else {
1641229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1642229997Sken			       bus_softc->port_name);
1643229997Sken		}
1644229997Sken	}
1645229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1646229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1647229997Sken	if (set_wwnn != 0)
1648229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1649229997Sken
1650229997Sken	if (online != 0)
1651288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1652229997Sken	else
1653288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1654229997Sken
1655229997Sken	xpt_action(ccb);
1656229997Sken
1657229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1658229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1659229997Sken		       "status %#x\n",
1660229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1661229997Sken		       (online != 0) ? "enable" : "disable",
1662229997Sken		       ccb->ccb_h.status);
1663229997Sken	} else {
1664229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1665229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1666229997Sken		       (online != 0) ? "enable" : "disable");
1667229997Sken	}
1668229997Sken
1669229997Sken	xpt_free_path(path);
1670275882Smav	xpt_free_ccb(ccb);
1671229997Sken}
1672229997Sken
1673229997Skenstatic void
1674229997Skenctlfe_online(void *arg)
1675229997Sken{
1676245228Sken	struct ctlfe_softc *bus_softc;
1677245228Sken	struct cam_path *path;
1678245228Sken	cam_status status;
1679245228Sken	struct ctlfe_lun_softc *lun_softc;
1680274388Smav	struct cam_periph *periph;
1681245228Sken
1682245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1683245228Sken
1684245228Sken	/*
1685245228Sken	 * Create the wildcard LUN before bringing the port online.
1686245228Sken	 */
1687245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1688245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1689245228Sken				 CAM_LUN_WILDCARD);
1690245228Sken	if (status != CAM_REQ_CMP) {
1691245228Sken		printf("%s: unable to create path for wildcard periph\n",
1692245228Sken				__func__);
1693245228Sken		return;
1694245228Sken	}
1695245228Sken
1696275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1697245228Sken
1698260387Sscottl	xpt_path_lock(path);
1699274388Smav	periph = cam_periph_find(path, "ctl");
1700274388Smav	if (periph != NULL) {
1701274388Smav		/* We've already got a periph, no need to alloc a new one. */
1702274388Smav		xpt_path_unlock(path);
1703274388Smav		xpt_free_path(path);
1704274388Smav		free(lun_softc, M_CTLFE);
1705274388Smav		return;
1706274388Smav	}
1707245228Sken	lun_softc->parent_softc = bus_softc;
1708245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1709245228Sken
1710245228Sken	status = cam_periph_alloc(ctlferegister,
1711245228Sken				  ctlfeoninvalidate,
1712245228Sken				  ctlfecleanup,
1713245228Sken				  ctlfestart,
1714245228Sken				  "ctl",
1715245228Sken				  CAM_PERIPH_BIO,
1716245228Sken				  path,
1717245228Sken				  ctlfeasync,
1718245228Sken				  0,
1719245228Sken				  lun_softc);
1720245228Sken
1721245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1722245228Sken		const struct cam_status_entry *entry;
1723245228Sken
1724245228Sken		entry = cam_fetch_status_entry(status);
1725245228Sken		printf("%s: CAM error %s (%#x) returned from "
1726245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1727245228Sken		       entry->status_text : "Unknown", status);
1728274388Smav		free(lun_softc, M_CTLFE);
1729245228Sken	}
1730245228Sken
1731260387Sscottl	xpt_path_unlock(path);
1732275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1733260387Sscottl	xpt_free_path(path);
1734229997Sken}
1735229997Sken
1736229997Skenstatic void
1737229997Skenctlfe_offline(void *arg)
1738229997Sken{
1739245228Sken	struct ctlfe_softc *bus_softc;
1740245228Sken	struct cam_path *path;
1741245228Sken	cam_status status;
1742245228Sken	struct cam_periph *periph;
1743245228Sken
1744245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1745245228Sken
1746275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1747275882Smav
1748245228Sken	/*
1749245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1750245228Sken	 * the port offline.
1751245228Sken	 */
1752245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1753245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1754245228Sken				 CAM_LUN_WILDCARD);
1755245228Sken	if (status != CAM_REQ_CMP) {
1756245228Sken		printf("%s: unable to create path for wildcard periph\n",
1757245228Sken		       __func__);
1758245228Sken		return;
1759245228Sken	}
1760260387Sscottl	xpt_path_lock(path);
1761245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1762245228Sken		cam_periph_invalidate(periph);
1763260387Sscottl	xpt_path_unlock(path);
1764245228Sken	xpt_free_path(path);
1765229997Sken}
1766229997Sken
1767229997Sken/*
1768229997Sken * This will get called to enable a LUN on every bus that is attached to
1769229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1770229997Sken */
1771229997Skenstatic int
1772284798Smavctlfe_lun_enable(void *arg, int lun_id)
1773229997Sken{
1774229997Sken	struct ctlfe_softc *bus_softc;
1775229997Sken	struct ctlfe_lun_softc *softc;
1776229997Sken	struct cam_path *path;
1777229997Sken	struct cam_periph *periph;
1778229997Sken	cam_status status;
1779229997Sken
1780229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1781290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1782290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1783229997Sken
1784260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1785290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1786229997Sken	/* XXX KDM need some way to return status to CTL here? */
1787229997Sken	if (status != CAM_REQ_CMP) {
1788229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1789229997Sken		       status);
1790229997Sken		return (1);
1791229997Sken	}
1792229997Sken
1793229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1794260387Sscottl	xpt_path_lock(path);
1795229997Sken	periph = cam_periph_find(path, "ctl");
1796229997Sken	if (periph != NULL) {
1797229997Sken		/* We've already got a periph, no need to alloc a new one. */
1798260387Sscottl		xpt_path_unlock(path);
1799229997Sken		xpt_free_path(path);
1800229997Sken		free(softc, M_CTLFE);
1801229997Sken		return (0);
1802229997Sken	}
1803229997Sken	softc->parent_softc = bus_softc;
1804229997Sken
1805229997Sken	status = cam_periph_alloc(ctlferegister,
1806229997Sken				  ctlfeoninvalidate,
1807229997Sken				  ctlfecleanup,
1808229997Sken				  ctlfestart,
1809229997Sken				  "ctl",
1810229997Sken				  CAM_PERIPH_BIO,
1811229997Sken				  path,
1812229997Sken				  ctlfeasync,
1813229997Sken				  0,
1814229997Sken				  softc);
1815229997Sken
1816274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1817274388Smav		const struct cam_status_entry *entry;
1818274388Smav
1819274388Smav		entry = cam_fetch_status_entry(status);
1820274388Smav		printf("%s: CAM error %s (%#x) returned from "
1821274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1822274388Smav		       entry->status_text : "Unknown", status);
1823274388Smav		free(softc, M_CTLFE);
1824274388Smav	}
1825274388Smav
1826260387Sscottl	xpt_path_unlock(path);
1827244016Sken	xpt_free_path(path);
1828229997Sken	return (0);
1829229997Sken}
1830229997Sken
1831229997Sken/*
1832245228Sken * This will get called when the user removes a LUN to disable that LUN
1833245228Sken * on every bus that is attached to CTL.
1834229997Sken */
1835229997Skenstatic int
1836284798Smavctlfe_lun_disable(void *arg, int lun_id)
1837229997Sken{
1838229997Sken	struct ctlfe_softc *softc;
1839229997Sken	struct ctlfe_lun_softc *lun_softc;
1840229997Sken
1841229997Sken	softc = (struct ctlfe_softc *)arg;
1842290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1843290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1844229997Sken
1845260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1846229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1847229997Sken		struct cam_path *path;
1848229997Sken
1849229997Sken		path = lun_softc->periph->path;
1850229997Sken
1851290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1852229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1853229997Sken			break;
1854229997Sken		}
1855229997Sken	}
1856229997Sken	if (lun_softc == NULL) {
1857260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1858284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1859229997Sken		return (1);
1860229997Sken	}
1861260387Sscottl	cam_periph_acquire(lun_softc->periph);
1862260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1863229997Sken
1864260387Sscottl	cam_periph_lock(lun_softc->periph);
1865229997Sken	cam_periph_invalidate(lun_softc->periph);
1866260387Sscottl	cam_periph_unlock(lun_softc->periph);
1867260387Sscottl	cam_periph_release(lun_softc->periph);
1868229997Sken	return (0);
1869229997Sken}
1870229997Sken
1871229997Skenstatic void
1872229997Skenctlfe_dump_sim(struct cam_sim *sim)
1873229997Sken{
1874229997Sken
1875229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1876229997Sken	       sim->sim_name, sim->unit_number,
1877229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1878229997Sken}
1879229997Sken
1880229997Sken/*
1881229997Sken * Assumes that the SIM lock is held.
1882229997Sken */
1883229997Skenstatic void
1884229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1885229997Sken{
1886229997Sken	struct ccb_hdr *hdr;
1887229997Sken	struct cam_periph *periph;
1888229997Sken	int num_items;
1889229997Sken
1890229997Sken	periph = softc->periph;
1891229997Sken	num_items = 0;
1892229997Sken
1893229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1894275880Smav		union ctl_io *io = hdr->io_ptr;
1895229997Sken
1896229997Sken		num_items++;
1897229997Sken
1898229997Sken		/*
1899229997Sken		 * Only regular SCSI I/O is put on the work
1900229997Sken		 * queue, so we can print sense here.  There may be no
1901229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1902229997Sken		 * print out the CCB as well.
1903229997Sken		 *
1904229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1905229997Sken		 * CTL is merged in with CAM.
1906229997Sken		 */
1907229997Sken		ctl_io_error_print(io, NULL);
1908229997Sken
1909229997Sken		/*
1910275880Smav		 * Print DMA status if we are DMA_QUEUED.
1911229997Sken		 */
1912275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1913275880Smav			xpt_print(periph->path,
1914275880Smav			    "Total %u, Current %u, Resid %u\n",
1915275880Smav			    io->scsiio.kern_total_len,
1916275880Smav			    io->scsiio.kern_data_len,
1917275880Smav			    io->scsiio.kern_data_resid);
1918275880Smav		}
1919229997Sken	}
1920229997Sken
1921229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1922229997Sken		  num_items);
1923250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
1924229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1925229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1926229997Sken		  (uintmax_t)softc->ccbs_freed);
1927229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1928229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
1929229997Sken		  softc->ctios_returned), softc->ctios_sent,
1930229997Sken		  softc->ctios_returned);
1931229997Sken}
1932229997Sken
1933229997Sken/*
1934275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1935275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1936275880Smav * to the adapter.
1937229997Sken */
1938229997Skenstatic void
1939275880Smavctlfe_datamove(union ctl_io *io)
1940229997Sken{
1941275880Smav	union ccb *ccb;
1942275880Smav	struct cam_periph *periph;
1943229997Sken	struct ctlfe_lun_softc *softc;
1944229997Sken
1945275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1946275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1947229997Sken
1948313365Smav	io->scsiio.ext_data_filled = 0;
1949312585Smav	ccb = PRIV_CCB(io);
1950275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
1951275880Smav	cam_periph_lock(periph);
1952275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1953275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1954275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1955275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1956275880Smav	TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1957275880Smav			  periph_links.tqe);
1958229997Sken	xpt_schedule(periph, /*priority*/ 1);
1959275880Smav	cam_periph_unlock(periph);
1960229997Sken}
1961229997Sken
1962229997Skenstatic void
1963275880Smavctlfe_done(union ctl_io *io)
1964229997Sken{
1965229997Sken	union ccb *ccb;
1966229997Sken	struct cam_periph *periph;
1967229997Sken	struct ctlfe_lun_softc *softc;
1968229997Sken
1969312585Smav	ccb = PRIV_CCB(io);
1970229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
1971260387Sscottl	cam_periph_lock(periph);
1972229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1973229997Sken
1974229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
1975229997Sken		/*
1976229997Sken		 * Send the notify acknowledge down to the SIM, to let it
1977229997Sken		 * know we processed the task management command.
1978229997Sken		 */
1979229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
1980229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1981304417Smav		switch (io->taskio.task_status) {
1982304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
1983304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1984304417Smav			break;
1985304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
1986304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1987304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1988304417Smav			break;
1989304417Smav		case CTL_TASK_FUNCTION_REJECTED:
1990304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1991304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1992304417Smav			break;
1993304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
1994304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1995304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1996304417Smav			break;
1997304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1998304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1999304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2000304417Smav			break;
2001304417Smav		}
2002304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
2003229997Sken		xpt_action(ccb);
2004275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
2005314739Smav		ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
2006314739Smav		return;
2007229997Sken	} else {
2008275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2009229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2010229997Sken				  periph_links.tqe);
2011275880Smav		xpt_schedule(periph, /*priority*/ 1);
2012229997Sken	}
2013229997Sken
2014260387Sscottl	cam_periph_unlock(periph);
2015229997Sken}
2016229997Sken
2017229997Skenstatic void
2018229997Skenctlfe_dump(void)
2019229997Sken{
2020229997Sken	struct ctlfe_softc *bus_softc;
2021275880Smav	struct ctlfe_lun_softc *lun_softc;
2022229997Sken
2023229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2024229997Sken		ctlfe_dump_sim(bus_softc->sim);
2025275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
2026229997Sken			ctlfe_dump_queue(lun_softc);
2027229997Sken	}
2028229997Sken}
2029