scsi_ctl.c revision 312845
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 312845 2017-01-26 21:07:46Z 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
191229997Skenint			ctlfeinitialize(void);
192229997Skenvoid			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
230229997Skenvoid
231229997Skenctlfeshutdown(void)
232229997Sken{
233229997Sken	return;
234229997Sken}
235229997Sken
236268677Smavint
237268677Smavctlfeinitialize(void)
238229997Sken{
239229997Sken
240229997Sken	STAILQ_INIT(&ctlfe_softc_list);
241229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
242268677Smav	periphdriver_register(&ctlfe_driver);
243268677Smav	return (0);
244268677Smav}
245229997Sken
246268677Smavvoid
247268677Smavctlfeperiphinit(void)
248268677Smav{
249268677Smav	cam_status status;
250229997Sken
251229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
252229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
253229997Sken	if (status != CAM_REQ_CMP) {
254229997Sken		printf("ctl: Failed to attach async callback due to CAM "
255229997Sken		       "status 0x%x!\n", status);
256229997Sken	}
257229997Sken}
258229997Sken
259229997Skenstatic void
260229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
261229997Sken{
262273317Smav	struct ctlfe_softc *softc;
263229997Sken
264229997Sken#ifdef CTLFEDEBUG
265229997Sken	printf("%s: entered\n", __func__);
266229997Sken#endif
267229997Sken
268273317Smav	mtx_lock(&ctlfe_list_mtx);
269273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
270273317Smav		if (softc->path_id == xpt_path_path_id(path))
271273317Smav			break;
272273317Smav	}
273273317Smav	mtx_unlock(&ctlfe_list_mtx);
274273317Smav
275229997Sken	/*
276229997Sken	 * When a new path gets registered, and it is capable of target
277229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
278229997Sken	 * selective, but for now this will be sufficient.
279229997Sken 	 */
280229997Sken	switch (code) {
281229997Sken	case AC_PATH_REGISTERED: {
282268677Smav		struct ctl_port *port;
283229997Sken		struct ccb_pathinq *cpi;
284229997Sken		int retval;
285229997Sken
286229997Sken		cpi = (struct ccb_pathinq *)arg;
287229997Sken
288229997Sken		/* Don't attach if it doesn't support target mode */
289229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
290230033Sken#ifdef CTLFEDEBUG
291229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
292229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
293230033Sken#endif
294229997Sken			break;
295229997Sken		}
296229997Sken
297273317Smav		if (softc != NULL) {
298273317Smav#ifdef CTLFEDEBUG
299273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
300273317Smav			       __func__, xpt_path_path_id(path));
301273317Smav#endif
302273317Smav			break;
303273317Smav		}
304273317Smav
305229997Sken#ifdef CTLFE_INIT_ENABLE
306229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
307229997Sken			union ccb *ccb;
308229997Sken
309229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
310229997Sken						  M_NOWAIT | M_ZERO);
311229997Sken			if (ccb == NULL) {
312229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
313229997Sken				return;
314229997Sken			}
315260387Sscottl			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
316229997Sken
317229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
318229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
319229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
320229997Sken
321229997Sken			xpt_action(ccb);
322229997Sken
323229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
324229997Sken			     CAM_REQ_CMP) {
325229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
326229997Sken				       "enable failed with status %#x\n",
327229997Sken				       __func__, cpi->dev_name,
328229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
329229997Sken				       ccb->ccb_h.status);
330229997Sken			} else {
331229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
332229997Sken				       "enable succeeded\n",
333229997Sken				       __func__, cpi->dev_name,
334229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
335229997Sken			}
336229997Sken
337229997Sken			free(ccb, M_TEMP);
338229997Sken
339229997Sken			break;
340229997Sken		} else {
341229997Sken			ctlfe_num_targets++;
342229997Sken		}
343229997Sken
344229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
345229997Sken		       ctlfe_num_targets);
346229997Sken#endif /* CTLFE_INIT_ENABLE */
347229997Sken
348229997Sken		/*
349229997Sken		 * We're in an interrupt context here, so we have to
350229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
351229997Sken		 * can't allocate memory.
352229997Sken		 */
353273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
354273317Smav		if (softc == NULL) {
355229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
356273317Smav			       __func__, sizeof(*softc));
357229997Sken			return;
358229997Sken		}
359229997Sken
360273317Smav		softc->path_id = cpi->ccb_h.path_id;
361288713Smav		softc->target_id = cpi->initiator_id;
362273317Smav		softc->sim = xpt_path_sim(path);
363290776Smav		softc->hba_misc = cpi->hba_misc;
364265641Smav		if (cpi->maxio != 0)
365273317Smav			softc->maxio = cpi->maxio;
366265641Smav		else
367273317Smav			softc->maxio = DFLTPHYS;
368273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
369273317Smav		STAILQ_INIT(&softc->lun_softc_list);
370229997Sken
371273317Smav		port = &softc->port;
372268677Smav		port->frontend = &ctlfe_frontend;
373229997Sken
374229997Sken		/*
375229997Sken		 * XXX KDM should we be more accurate here ?
376229997Sken		 */
377229997Sken		if (cpi->transport == XPORT_FC)
378268677Smav			port->port_type = CTL_PORT_FC;
379268694Smav		else if (cpi->transport == XPORT_SAS)
380268694Smav			port->port_type = CTL_PORT_SAS;
381229997Sken		else
382268677Smav			port->port_type = CTL_PORT_SCSI;
383229997Sken
384229997Sken		/* XXX KDM what should the real number be here? */
385268677Smav		port->num_requested_ctl_io = 4096;
386273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
387229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
388229997Sken		/*
389229997Sken		 * XXX KDM it would be nice to allocate storage in the
390229997Sken		 * frontend structure itself.
391229997Sken	 	 */
392273317Smav		port->port_name = softc->port_name;
393273319Smav		port->physical_port = cpi->bus_id;
394273319Smav		port->virtual_port = 0;
395268677Smav		port->port_online = ctlfe_online;
396268677Smav		port->port_offline = ctlfe_offline;
397273317Smav		port->onoff_arg = softc;
398268677Smav		port->lun_enable = ctlfe_lun_enable;
399268677Smav		port->lun_disable = ctlfe_lun_disable;
400273317Smav		port->targ_lun_arg = softc;
401275880Smav		port->fe_datamove = ctlfe_datamove;
402275880Smav		port->fe_done = ctlfe_done;
403229997Sken		/*
404229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
405229997Sken		 * number of targets supported.
406229997Sken		 */
407268677Smav		port->max_targets = cpi->max_target;
408268677Smav		port->max_target_id = cpi->max_target;
409288732Smav		port->targ_port = -1;
410229997Sken
411229997Sken		/*
412229997Sken		 * XXX KDM need to figure out whether we're the master or
413229997Sken		 * slave.
414229997Sken		 */
415230033Sken#ifdef CTLFEDEBUG
416268677Smav		printf("%s: calling ctl_port_register() for %s%d\n",
417229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
418230033Sken#endif
419275493Smav		retval = ctl_port_register(port);
420229997Sken		if (retval != 0) {
421268677Smav			printf("%s: ctl_port_register() failed with "
422229997Sken			       "error %d!\n", __func__, retval);
423273317Smav			mtx_destroy(&softc->lun_softc_mtx);
424273317Smav			free(softc, M_CTLFE);
425229997Sken			break;
426229997Sken		} else {
427229997Sken			mtx_lock(&ctlfe_list_mtx);
428273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
429229997Sken			mtx_unlock(&ctlfe_list_mtx);
430229997Sken		}
431229997Sken
432245228Sken		break;
433245228Sken	}
434245228Sken	case AC_PATH_DEREGISTERED: {
435245228Sken
436245228Sken		if (softc != NULL) {
437245228Sken			/*
438245228Sken			 * XXX KDM are we certain at this point that there
439245228Sken			 * are no outstanding commands for this frontend?
440245228Sken			 */
441273317Smav			mtx_lock(&ctlfe_list_mtx);
442273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
443273317Smav			    links);
444273317Smav			mtx_unlock(&ctlfe_list_mtx);
445268677Smav			ctl_port_deregister(&softc->port);
446260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
447245228Sken			free(softc, M_CTLFE);
448229997Sken		}
449229997Sken		break;
450229997Sken	}
451229997Sken	case AC_CONTRACT: {
452229997Sken		struct ac_contract *ac;
453229997Sken
454229997Sken		ac = (struct ac_contract *)arg;
455229997Sken
456229997Sken		switch (ac->contract_number) {
457229997Sken		case AC_CONTRACT_DEV_CHG: {
458229997Sken			struct ac_device_changed *dev_chg;
459273317Smav			int retval;
460229997Sken
461229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
462229997Sken
463236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
464229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
465229997Sken			       xpt_path_path_id(path), dev_chg->target,
466229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
467229997Sken
468273317Smav			if (softc == NULL) {
469229997Sken				printf("%s: CTL port for CAM path %u not "
470229997Sken				       "found!\n", __func__,
471229997Sken				       xpt_path_path_id(path));
472229997Sken				break;
473229997Sken			}
474229997Sken			if (dev_chg->arrived != 0) {
475268692Smav				retval = ctl_add_initiator(&softc->port,
476268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
477229997Sken			} else {
478268692Smav				retval = ctl_remove_initiator(&softc->port,
479268692Smav				    dev_chg->target);
480229997Sken			}
481229997Sken
482268692Smav			if (retval < 0) {
483229997Sken				printf("%s: could not %s port %d iid %u "
484229997Sken				       "WWPN %#jx!\n", __func__,
485229997Sken				       (dev_chg->arrived != 0) ? "add" :
486268677Smav				       "remove", softc->port.targ_port,
487229997Sken				       dev_chg->target,
488229997Sken				       (uintmax_t)dev_chg->wwpn);
489229997Sken			}
490229997Sken			break;
491229997Sken		}
492229997Sken		default:
493229997Sken			printf("%s: unsupported contract number %ju\n",
494229997Sken			       __func__, (uintmax_t)ac->contract_number);
495229997Sken			break;
496229997Sken		}
497229997Sken		break;
498229997Sken	}
499229997Sken	default:
500229997Sken		break;
501229997Sken	}
502229997Sken}
503229997Sken
504229997Skenstatic cam_status
505229997Skenctlferegister(struct cam_periph *periph, void *arg)
506229997Sken{
507229997Sken	struct ctlfe_softc *bus_softc;
508229997Sken	struct ctlfe_lun_softc *softc;
509229997Sken	union ccb en_lun_ccb;
510229997Sken	cam_status status;
511229997Sken	int i;
512229997Sken
513229997Sken	softc = (struct ctlfe_lun_softc *)arg;
514229997Sken	bus_softc = softc->parent_softc;
515229997Sken
516229997Sken	TAILQ_INIT(&softc->work_queue);
517229997Sken	softc->periph = periph;
518229997Sken	periph->softc = softc;
519229997Sken
520242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
521229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
522229997Sken	en_lun_ccb.cel.grp6_len = 0;
523229997Sken	en_lun_ccb.cel.grp7_len = 0;
524229997Sken	en_lun_ccb.cel.enable = 1;
525229997Sken	xpt_action(&en_lun_ccb);
526229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
527229997Sken	if (status != CAM_REQ_CMP) {
528229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
529229997Sken			  __func__, en_lun_ccb.ccb_h.status);
530229997Sken		return (status);
531229997Sken	}
532229997Sken
533229997Sken	status = CAM_REQ_CMP;
534229997Sken
535229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
536229997Sken		union ccb *new_ccb;
537275878Smav		union ctl_io *new_io;
538288723Smav		struct ctlfe_cmd_info *cmd_info;
539229997Sken
540229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
541236426Smjacob					      M_ZERO|M_NOWAIT);
542229997Sken		if (new_ccb == NULL) {
543229997Sken			status = CAM_RESRC_UNAVAIL;
544229997Sken			break;
545229997Sken		}
546275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
547275878Smav		if (new_io == NULL) {
548275878Smav			free(new_ccb, M_CTLFE);
549275878Smav			status = CAM_RESRC_UNAVAIL;
550275878Smav			break;
551275878Smav		}
552288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
553288723Smav		    M_ZERO | M_NOWAIT);
554288723Smav		if (cmd_info == NULL) {
555288723Smav			ctl_free_io(new_io);
556288723Smav			free(new_ccb, M_CTLFE);
557288723Smav			status = CAM_RESRC_UNAVAIL;
558288723Smav			break;
559288723Smav		}
560312585Smav		PRIV_INFO(new_io) = cmd_info;
561284793Smav		softc->atios_alloced++;
562275878Smav		new_ccb->ccb_h.io_ptr = new_io;
563275878Smav
564229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
565229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
566229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
567260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
568229997Sken		xpt_action(new_ccb);
569229997Sken		status = new_ccb->ccb_h.status;
570229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
571288723Smav			free(cmd_info, M_CTLFE);
572275878Smav			ctl_free_io(new_io);
573229997Sken			free(new_ccb, M_CTLFE);
574229997Sken			break;
575229997Sken		}
576229997Sken	}
577229997Sken
578229997Sken	status = cam_periph_acquire(periph);
579229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
580229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
581229997Sken			  "count, status = %#x\n", __func__, status);
582229997Sken		return (status);
583229997Sken	}
584229997Sken
585229997Sken	if (i == 0) {
586229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
587229997Sken			  "status 0x%x\n", __func__, status);
588229997Sken		return (CAM_REQ_CMP_ERR);
589229997Sken	}
590229997Sken
591229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
592229997Sken		union ccb *new_ccb;
593275878Smav		union ctl_io *new_io;
594229997Sken
595229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
596236426Smjacob					      M_ZERO|M_NOWAIT);
597229997Sken		if (new_ccb == NULL) {
598229997Sken			status = CAM_RESRC_UNAVAIL;
599229997Sken			break;
600229997Sken		}
601275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
602275878Smav		if (new_io == NULL) {
603275878Smav			free(new_ccb, M_CTLFE);
604275878Smav			status = CAM_RESRC_UNAVAIL;
605275878Smav			break;
606275878Smav		}
607284793Smav		softc->inots_alloced++;
608275878Smav		new_ccb->ccb_h.io_ptr = new_io;
609229997Sken
610229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
611229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
612229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
613260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
614229997Sken		xpt_action(new_ccb);
615229997Sken		status = new_ccb->ccb_h.status;
616237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
617237601Sken			/*
618237601Sken			 * Note that we don't free the CCB here.  If the
619237601Sken			 * status is not CAM_REQ_INPROG, then we're
620237601Sken			 * probably talking to a SIM that says it is
621237601Sken			 * target-capable but doesn't support the
622237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
623237601Sken			 * older API.  In that case, it'll call xpt_done()
624237601Sken			 * on the CCB, and we need to free it in our done
625237601Sken			 * routine as a result.
626237601Sken			 */
627229997Sken			break;
628229997Sken		}
629229997Sken	}
630237601Sken	if ((i == 0)
631237601Sken	 || (status != CAM_REQ_INPROG)) {
632229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
633229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
634229997Sken		return (CAM_REQ_CMP_ERR);
635229997Sken	}
636275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
637275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
638275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
639229997Sken	return (CAM_REQ_CMP);
640229997Sken}
641229997Sken
642229997Skenstatic void
643229997Skenctlfeoninvalidate(struct cam_periph *periph)
644229997Sken{
645229997Sken	union ccb en_lun_ccb;
646229997Sken	cam_status status;
647260387Sscottl	struct ctlfe_softc *bus_softc;
648229997Sken	struct ctlfe_lun_softc *softc;
649229997Sken
650229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
651229997Sken
652242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
653229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
654229997Sken	en_lun_ccb.cel.grp6_len = 0;
655229997Sken	en_lun_ccb.cel.grp7_len = 0;
656229997Sken	en_lun_ccb.cel.enable = 0;
657229997Sken	xpt_action(&en_lun_ccb);
658229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
659229997Sken	if (status != CAM_REQ_CMP) {
660229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
661229997Sken			  __func__, en_lun_ccb.ccb_h.status);
662229997Sken		/*
663229997Sken		 * XXX KDM what do we do now?
664229997Sken		 */
665229997Sken	}
666260387Sscottl
667260387Sscottl	bus_softc = softc->parent_softc;
668260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
669260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
670260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
671229997Sken}
672229997Sken
673229997Skenstatic void
674229997Skenctlfecleanup(struct cam_periph *periph)
675229997Sken{
676229997Sken	struct ctlfe_lun_softc *softc;
677229997Sken
678229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
679229997Sken
680284793Smav	KASSERT(softc->ccbs_freed == softc->ccbs_alloced, ("%s: "
681284793Smav		"ccbs_freed %ju != ccbs_alloced %ju", __func__,
682284793Smav		softc->ccbs_freed, softc->ccbs_alloced));
683284793Smav	KASSERT(softc->ctios_returned == softc->ctios_sent, ("%s: "
684284793Smav		"ctios_returned %ju != ctios_sent %ju", __func__,
685284793Smav		softc->ctios_returned, softc->ctios_sent));
686284793Smav	KASSERT(softc->atios_freed == softc->atios_alloced, ("%s: "
687284793Smav		"atios_freed %ju != atios_alloced %ju", __func__,
688284793Smav		softc->atios_freed, softc->atios_alloced));
689284793Smav	KASSERT(softc->inots_freed == softc->inots_alloced, ("%s: "
690284793Smav		"inots_freed %ju != inots_alloced %ju", __func__,
691284793Smav		softc->inots_freed, softc->inots_alloced));
692245228Sken
693229997Sken	free(softc, M_CTLFE);
694229997Sken}
695229997Sken
696229997Skenstatic void
697265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
698265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
699265641Smav    u_int16_t *sglist_cnt)
700265641Smav{
701265641Smav	struct ctlfe_softc *bus_softc;
702288723Smav	struct ctlfe_cmd_info *cmd_info;
703265641Smav	struct ctl_sg_entry *ctl_sglist;
704265641Smav	bus_dma_segment_t *cam_sglist;
705265641Smav	size_t off;
706265641Smav	int i, idx;
707265641Smav
708312585Smav	cmd_info = PRIV_INFO(io);
709265641Smav	bus_softc = softc->parent_softc;
710265641Smav
711265641Smav	/*
712265641Smav	 * Set the direction, relative to the initiator.
713265641Smav	 */
714265641Smav	*flags &= ~CAM_DIR_MASK;
715265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
716265641Smav		*flags |= CAM_DIR_IN;
717265641Smav	else
718265641Smav		*flags |= CAM_DIR_OUT;
719265641Smav
720265641Smav	*flags &= ~CAM_DATA_MASK;
721265641Smav	idx = cmd_info->cur_transfer_index;
722265641Smav	off = cmd_info->cur_transfer_off;
723265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
724265641Smav	if (io->scsiio.kern_sg_entries == 0) {
725265641Smav		/* No S/G list. */
726265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
727265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
728265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
729265641Smav		} else {
730265641Smav			*dxfer_len = bus_softc->maxio;
731265641Smav			cmd_info->cur_transfer_index = -1;
732265641Smav			cmd_info->cur_transfer_off = bus_softc->maxio;
733265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
734265641Smav		}
735265641Smav		*sglist_cnt = 0;
736265641Smav
737265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
738265641Smav			*flags |= CAM_DATA_PADDR;
739265641Smav		else
740265641Smav			*flags |= CAM_DATA_VADDR;
741265641Smav	} else {
742265641Smav		/* S/G list with physical or virtual pointers. */
743265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
744265641Smav		cam_sglist = cmd_info->cam_sglist;
745265641Smav		*dxfer_len = 0;
746265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
747265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
748265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
749265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
750265641Smav				*dxfer_len += cam_sglist[i].ds_len;
751265641Smav			} else {
752265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
753265641Smav				cmd_info->cur_transfer_index = idx + i;
754265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
755265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
756265641Smav				*dxfer_len += cam_sglist[i].ds_len;
757265641Smav				if (ctl_sglist[i].len != 0)
758265641Smav					i++;
759265641Smav				break;
760265641Smav			}
761265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
762265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
763265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
764265641Smav				cmd_info->cur_transfer_off = 0;
765265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
766265641Smav				i++;
767265641Smav				break;
768265641Smav			}
769265641Smav			off = 0;
770265641Smav		}
771265641Smav		*sglist_cnt = i;
772265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
773265641Smav			*flags |= CAM_DATA_SG_PADDR;
774265641Smav		else
775265641Smav			*flags |= CAM_DATA_SG;
776265641Smav		*data_ptr = (uint8_t *)cam_sglist;
777265641Smav	}
778265641Smav}
779265641Smav
780265641Smavstatic void
781229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
782229997Sken{
783229997Sken	struct ctlfe_lun_softc *softc;
784288723Smav	struct ctlfe_cmd_info *cmd_info;
785229997Sken	struct ccb_hdr *ccb_h;
786275880Smav	struct ccb_accept_tio *atio;
787275880Smav	struct ccb_scsiio *csio;
788275880Smav	uint8_t *data_ptr;
789275880Smav	uint32_t dxfer_len;
790275880Smav	ccb_flags flags;
791275880Smav	union ctl_io *io;
792275880Smav	uint8_t scsi_status;
793229997Sken
794229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
795229997Sken	softc->ccbs_alloced++;
796229997Sken
797229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
798260387Sscottl	if (ccb_h == NULL) {
799229997Sken		softc->ccbs_freed++;
800229997Sken		xpt_release_ccb(start_ccb);
801275880Smav		return;
802275880Smav	}
803229997Sken
804275880Smav	/* Take the ATIO off the work queue */
805275880Smav	TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
806275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
807275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
808275880Smav	csio = &start_ccb->csio;
809229997Sken
810275880Smav	flags = atio->ccb_h.flags &
811275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
812312585Smav	cmd_info = PRIV_INFO(io);
813275881Smav	cmd_info->cur_transfer_index = 0;
814275881Smav	cmd_info->cur_transfer_off = 0;
815275881Smav	cmd_info->flags = 0;
816229997Sken
817275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
818275880Smav		/*
819275880Smav		 * Datamove call, we need to setup the S/G list.
820275880Smav		 */
821275880Smav		scsi_status = 0;
822275880Smav		csio->cdb_len = atio->cdb_len;
823275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
824275880Smav		    &csio->sglist_cnt);
825275880Smav		io->scsiio.ext_data_filled += dxfer_len;
826275880Smav		if (io->scsiio.ext_data_filled > io->scsiio.kern_total_len) {
827275880Smav			xpt_print(periph->path, "%s: tag 0x%04x "
828275880Smav				  "fill len %u > total %u\n",
829275880Smav				  __func__, io->scsiio.tag_num,
830275880Smav				  io->scsiio.ext_data_filled,
831275880Smav				  io->scsiio.kern_total_len);
832275880Smav		}
833275880Smav	} else {
834275880Smav		/*
835275880Smav		 * We're done, send status back.
836275880Smav		 */
837275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
838275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
839275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
840275880Smav
841229997Sken			/*
842275880Smav			 * If this command was aborted, we don't
843275880Smav			 * need to send status back to the SIM.
844275880Smav			 * Just free the CTIO and ctl_io, and
845275880Smav			 * recycle the ATIO back to the SIM.
846229997Sken			 */
847275880Smav			xpt_print(periph->path, "%s: aborted "
848275880Smav				  "command 0x%04x discarded\n",
849275880Smav				  __func__, io->scsiio.tag_num);
850275880Smav			/*
851275880Smav			 * For a wildcard attachment, commands can
852275880Smav			 * come in with a specific target/lun.  Reset
853275880Smav			 * the target and LUN fields back to the
854275880Smav			 * wildcard values before we send them back
855275880Smav			 * down to the SIM.  The SIM has a wildcard
856275880Smav			 * LUN enabled, not whatever target/lun
857275880Smav			 * these happened to be.
858275880Smav			 */
859275880Smav			if (softc->flags & CTLFE_LUN_WILDCARD) {
860275880Smav				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
861275880Smav				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
862275880Smav			}
863229997Sken
864275880Smav			if (atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
865275880Smav				xpt_print(periph->path, "%s: func_code "
866275880Smav					  "is %#x\n", __func__,
867275880Smav					  atio->ccb_h.func_code);
868275880Smav			}
869275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
870275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
871229997Sken
872275880Smav			/* Tell the SIM that we've aborted this ATIO */
873275880Smav			xpt_action(start_ccb);
874275880Smav			softc->ccbs_freed++;
875275880Smav			xpt_release_ccb(start_ccb);
876229997Sken
877229997Sken			/*
878275880Smav			 * Send the ATIO back down to the SIM.
879229997Sken			 */
880275880Smav			xpt_action((union ccb *)atio);
881229997Sken
882229997Sken			/*
883275880Smav			 * If we still have work to do, ask for
884275880Smav			 * another CCB.  Otherwise, deactivate our
885275880Smav			 * callout.
886229997Sken			 */
887275880Smav			if (!TAILQ_EMPTY(&softc->work_queue))
888275880Smav				xpt_schedule(periph, /*priority*/ 1);
889275880Smav			return;
890275880Smav		}
891275881Smav		data_ptr = NULL;
892275881Smav		dxfer_len = 0;
893275881Smav		csio->sglist_cnt = 0;
894275881Smav		scsi_status = 0;
895275881Smav	}
896275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
897275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
898275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
899275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
900275880Smav		flags |= CAM_SEND_STATUS;
901275880Smav		scsi_status = io->scsiio.scsi_status;
902275880Smav		csio->sense_len = io->scsiio.sense_len;
903275880Smav#ifdef CTLFEDEBUG
904275880Smav		printf("%s: tag %04x status %x\n", __func__,
905275880Smav		       atio->tag_id, io->io_hdr.status);
906275880Smav#endif
907275880Smav		if (csio->sense_len != 0) {
908275880Smav			csio->sense_data = io->scsiio.sense_data;
909275880Smav			flags |= CAM_SEND_SENSE;
910275880Smav		} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
911275880Smav			xpt_print(periph->path, "%s: check condition "
912275880Smav				  "with no sense\n", __func__);
913229997Sken		}
914275880Smav	}
915229997Sken
916229997Sken#ifdef CTLFEDEBUG
917275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
918275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
919275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
920229997Sken#endif
921229997Sken
922275880Smav	/*
923275880Smav	 * Valid combinations:
924275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
925275880Smav	 *    sglist_cnt = 0
926275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
927275880Smav	 *    sglist_cnt = 0
928275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
929275880Smav	 *    sglist_cnt != 0
930275880Smav	 */
931229997Sken#ifdef CTLFEDEBUG
932275880Smav	if (((flags & CAM_SEND_STATUS)
933275880Smav	  && (((flags & CAM_DATA_SG) != 0)
934275880Smav	   || (dxfer_len != 0)
935275880Smav	   || (csio->sglist_cnt != 0)))
936275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
937275880Smav	  && (dxfer_len == 0))
938275880Smav	 || ((flags & CAM_DATA_SG)
939275880Smav	  && (csio->sglist_cnt == 0))
940275880Smav	 || (((flags & CAM_DATA_SG) == 0)
941275880Smav	  && (csio->sglist_cnt != 0))) {
942275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
943275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
944312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
945275880Smav		       csio->sglist_cnt);
946275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
947275880Smav		       atio->tag_id, io->io_hdr.status);
948275880Smav	}
949229997Sken#endif
950275880Smav	cam_fill_ctio(csio,
951275880Smav		      /*retries*/ 2,
952275880Smav		      ctlfedone,
953275880Smav		      flags,
954275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
955275880Smav		      atio->tag_id,
956275880Smav		      atio->init_id,
957275880Smav		      scsi_status,
958275880Smav		      /*data_ptr*/ data_ptr,
959275880Smav		      /*dxfer_len*/ dxfer_len,
960275880Smav		      /*timeout*/ 5 * 1000);
961275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
962275880Smav	start_ccb->ccb_h.ccb_atio = atio;
963275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
964275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
965275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
966229997Sken
967275880Smav	softc->ctios_sent++;
968229997Sken
969275880Smav	cam_periph_unlock(periph);
970275880Smav	xpt_action(start_ccb);
971275880Smav	cam_periph_lock(periph);
972229997Sken
973229997Sken	/*
974275880Smav	 * If we still have work to do, ask for another CCB.
975229997Sken	 */
976275880Smav	if (!TAILQ_EMPTY(&softc->work_queue))
977229997Sken		xpt_schedule(periph, /*priority*/ 1);
978229997Sken}
979229997Sken
980229997Skenstatic void
981229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
982229997Sken{
983229997Sken	struct ctlfe_lun_softc *softc;
984288723Smav	union ctl_io *io;
985288723Smav	struct ctlfe_cmd_info *cmd_info;
986229997Sken
987229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
988288723Smav	io = ccb->ccb_h.io_ptr;
989229997Sken
990229997Sken	switch (ccb->ccb_h.func_code) {
991229997Sken	case XPT_ACCEPT_TARGET_IO:
992284793Smav		softc->atios_freed++;
993312585Smav		cmd_info = PRIV_INFO(io);
994288723Smav		free(cmd_info, M_CTLFE);
995229997Sken		break;
996229997Sken	case XPT_IMMEDIATE_NOTIFY:
997229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
998284793Smav		softc->inots_freed++;
999229997Sken		break;
1000229997Sken	default:
1001229997Sken		break;
1002229997Sken	}
1003229997Sken
1004288723Smav	ctl_free_io(io);
1005229997Sken	free(ccb, M_CTLFE);
1006229997Sken
1007284793Smav	KASSERT(softc->atios_freed <= softc->atios_alloced, ("%s: "
1008284793Smav		"atios_freed %ju > atios_alloced %ju", __func__,
1009284793Smav		softc->atios_freed, softc->atios_alloced));
1010284793Smav	KASSERT(softc->inots_freed <= softc->inots_alloced, ("%s: "
1011284793Smav		"inots_freed %ju > inots_alloced %ju", __func__,
1012284793Smav		softc->inots_freed, softc->inots_alloced));
1013229997Sken
1014229997Sken	/*
1015229997Sken	 * If we have received all of our CCBs, we can release our
1016229997Sken	 * reference on the peripheral driver.  It will probably go away
1017229997Sken	 * now.
1018229997Sken	 */
1019284793Smav	if ((softc->atios_freed == softc->atios_alloced)
1020284793Smav	 && (softc->inots_freed == softc->inots_alloced)) {
1021229997Sken		cam_periph_release_locked(periph);
1022229997Sken	}
1023229997Sken}
1024229997Sken
1025238870Smjacobstatic int
1026238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1027238870Smjacob{
1028238870Smjacob	uint64_t lba;
1029238870Smjacob	uint32_t num_blocks, nbc;
1030312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
1031238870Smjacob
1032238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1033238870Smjacob
1034238870Smjacob	switch (cmdbyt[0]) {
1035238870Smjacob	case READ_6:
1036238870Smjacob	case WRITE_6:
1037238870Smjacob	{
1038238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1039238870Smjacob		lba = scsi_3btoul(cdb->addr);
1040238870Smjacob		lba &= 0x1fffff;
1041238870Smjacob		num_blocks = cdb->length;
1042238870Smjacob		if (num_blocks == 0)
1043238870Smjacob			num_blocks = 256;
1044238870Smjacob		lba += nbc;
1045238870Smjacob		num_blocks -= nbc;
1046238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1047238870Smjacob		cdb->length = num_blocks;
1048238870Smjacob		break;
1049238870Smjacob	}
1050238870Smjacob	case READ_10:
1051238870Smjacob	case WRITE_10:
1052238870Smjacob	{
1053238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1054238870Smjacob		lba = scsi_4btoul(cdb->addr);
1055238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1056238870Smjacob		lba += nbc;
1057238870Smjacob		num_blocks -= nbc;
1058238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1059238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1060238870Smjacob		break;
1061238870Smjacob	}
1062238870Smjacob	case READ_12:
1063238870Smjacob	case WRITE_12:
1064238870Smjacob	{
1065238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1066238870Smjacob		lba = scsi_4btoul(cdb->addr);
1067238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1068238870Smjacob		lba += nbc;
1069238870Smjacob		num_blocks -= nbc;
1070238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1071238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1072238870Smjacob		break;
1073238870Smjacob	}
1074238870Smjacob	case READ_16:
1075238870Smjacob	case WRITE_16:
1076238870Smjacob	{
1077238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1078238870Smjacob		lba = scsi_8btou64(cdb->addr);
1079238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1080238870Smjacob		lba += nbc;
1081238870Smjacob		num_blocks -= nbc;
1082238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1083238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1084238870Smjacob		break;
1085238870Smjacob	}
1086238870Smjacob	default:
1087238870Smjacob		return -1;
1088238870Smjacob	}
1089238870Smjacob	return (0);
1090238870Smjacob}
1091238870Smjacob
1092229997Skenstatic void
1093229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1094229997Sken{
1095229997Sken	struct ctlfe_lun_softc *softc;
1096229997Sken	struct ctlfe_softc *bus_softc;
1097288723Smav	struct ctlfe_cmd_info *cmd_info;
1098238870Smjacob	struct ccb_accept_tio *atio = NULL;
1099238870Smjacob	union ctl_io *io = NULL;
1100260387Sscottl	struct mtx *mtx;
1101229997Sken
1102260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1103260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1104229997Sken#ifdef CTLFE_DEBUG
1105275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1106275878Smav	       done_ccb->ccb_h.func_code);
1107229997Sken#endif
1108229997Sken
1109284794Smav	/*
1110284794Smav	 * At this point CTL has no known use case for device queue freezes.
1111284794Smav	 * In case some SIM think different -- drop its freeze right here.
1112284794Smav	 */
1113284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1114284794Smav		cam_release_devq(periph->path,
1115284794Smav				 /*relsim_flags*/0,
1116284794Smav				 /*reduction*/0,
1117284794Smav				 /*timeout*/0,
1118284794Smav				 /*getcount_only*/0);
1119284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1120284794Smav	}
1121284794Smav
1122229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1123229997Sken	bus_softc = softc->parent_softc;
1124260387Sscottl	mtx = cam_periph_mtx(periph);
1125260387Sscottl	mtx_lock(mtx);
1126229997Sken
1127229997Sken	/*
1128229997Sken	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1129229997Sken	 * need to be freed.  Most of the ATIOs and INOTs that come back
1130229997Sken	 * will be CCBs that are being returned from the SIM as a result of
1131229997Sken	 * our disabling the LUN.
1132229997Sken	 *
1133229997Sken	 * Other CCB types are handled in their respective cases below.
1134229997Sken	 */
1135229997Sken	if (periph->flags & CAM_PERIPH_INVALID) {
1136229997Sken		switch (done_ccb->ccb_h.func_code) {
1137229997Sken		case XPT_ACCEPT_TARGET_IO:
1138229997Sken		case XPT_IMMEDIATE_NOTIFY:
1139229997Sken		case XPT_NOTIFY_ACKNOWLEDGE:
1140229997Sken			ctlfe_free_ccb(periph, done_ccb);
1141260387Sscottl			goto out;
1142229997Sken		default:
1143229997Sken			break;
1144229997Sken		}
1145229997Sken
1146229997Sken	}
1147229997Sken	switch (done_ccb->ccb_h.func_code) {
1148229997Sken	case XPT_ACCEPT_TARGET_IO: {
1149229997Sken
1150229997Sken		atio = &done_ccb->atio;
1151229997Sken
1152238870Smjacob resubmit:
1153229997Sken		/*
1154229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1155229997Sken		 * datamove or done.
1156229997Sken		 */
1157260387Sscottl		mtx_unlock(mtx);
1158275878Smav		io = done_ccb->ccb_h.io_ptr;
1159312585Smav		cmd_info = PRIV_INFO(io);
1160229997Sken		ctl_zero_io(io);
1161229997Sken
1162229997Sken		/* Save pointers on both sides */
1163312585Smav		PRIV_CCB(io) = done_ccb;
1164312585Smav		PRIV_INFO(io) = cmd_info;
1165229997Sken		done_ccb->ccb_h.io_ptr = io;
1166229997Sken
1167229997Sken		/*
1168229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1169229997Sken		 * down the immediate notify path below.
1170229997Sken		 */
1171229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1172288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1173268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1174290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1175290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1176290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1177290776Smav		} else {
1178290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1179290776Smav		}
1180229997Sken		io->scsiio.tag_num = atio->tag_id;
1181229997Sken		switch (atio->tag_action) {
1182229997Sken		case CAM_TAG_ACTION_NONE:
1183229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1184229997Sken			break;
1185229997Sken		case MSG_SIMPLE_TASK:
1186229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1187229997Sken			break;
1188229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1189229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1190229997Sken			break;
1191229997Sken		case MSG_ORDERED_TASK:
1192229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1193229997Sken			break;
1194229997Sken		case MSG_ACA_TASK:
1195229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1196229997Sken			break;
1197229997Sken		default:
1198229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1199229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1200229997Sken			       atio->tag_action);
1201229997Sken			break;
1202229997Sken		}
1203229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1204229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1205229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1206229997Sken		}
1207229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1208312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1209229997Sken
1210229997Sken#ifdef CTLFEDEBUG
1211288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1212288731Smav		        io->io_hdr.nexus.initid,
1213229997Sken		        io->io_hdr.nexus.targ_port,
1214229997Sken		        io->io_hdr.nexus.targ_lun,
1215229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1216229997Sken#endif
1217229997Sken
1218229997Sken		ctl_queue(io);
1219260387Sscottl		return;
1220229997Sken	}
1221229997Sken	case XPT_CONT_TARGET_IO: {
1222238870Smjacob		int srr = 0;
1223238870Smjacob		uint32_t srr_off = 0;
1224229997Sken
1225229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1226229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1227229997Sken
1228229997Sken		softc->ctios_returned++;
1229229997Sken#ifdef CTLFEDEBUG
1230229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1231229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1232229997Sken#endif
1233229997Sken		/*
1234238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1235238870Smjacob		 */
1236238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1237238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1238238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1239238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1240238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1241238870Smjacob			srr = 1;
1242238870Smjacob			srr_off =
1243238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1244238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1245238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1246238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1247238870Smjacob		}
1248238870Smjacob
1249275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1250238870Smjacob			/*
1251238870Smjacob			 * If status was being sent, the back end data is now
1252238870Smjacob			 * history. Hack it up and resubmit a new command with
1253238870Smjacob			 * the CDB adjusted. If the SIM does the right thing,
1254238870Smjacob			 * all of the resid math should work.
1255238870Smjacob			 */
1256238870Smjacob			softc->ccbs_freed++;
1257238870Smjacob			xpt_release_ccb(done_ccb);
1258238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1259238870Smjacob				done_ccb = (union ccb *)atio;
1260238870Smjacob				goto resubmit;
1261238870Smjacob			}
1262238870Smjacob			/*
1263238870Smjacob			 * Fall through to doom....
1264238870Smjacob			 */
1265238870Smjacob		} else if (srr) {
1266238870Smjacob			/*
1267238870Smjacob			 * If we have an srr and we're still sending data, we
1268238870Smjacob			 * should be able to adjust offsets and cycle again.
1269238870Smjacob			 */
1270238870Smjacob			io->scsiio.kern_rel_offset =
1271238870Smjacob			    io->scsiio.ext_data_filled = srr_off;
1272238870Smjacob			io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1273238870Smjacob			    io->scsiio.kern_rel_offset;
1274238870Smjacob			softc->ccbs_freed++;
1275238870Smjacob			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1276238870Smjacob			xpt_release_ccb(done_ccb);
1277238870Smjacob			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1278238870Smjacob					  periph_links.tqe);
1279238870Smjacob			xpt_schedule(periph, /*priority*/ 1);
1280260387Sscottl			break;
1281238870Smjacob		}
1282238870Smjacob
1283277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1284277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1285277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1286277919Smav
1287238870Smjacob		/*
1288229997Sken		 * If we were sending status back to the initiator, free up
1289229997Sken		 * resources.  If we were doing a datamove, call the
1290229997Sken		 * datamove done routine.
1291229997Sken		 */
1292275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1293229997Sken			softc->ccbs_freed++;
1294229997Sken			xpt_release_ccb(done_ccb);
1295229997Sken			/*
1296229997Sken			 * For a wildcard attachment, commands can come in
1297229997Sken			 * with a specific target/lun.  Reset the target
1298229997Sken			 * and LUN fields back to the wildcard values before
1299229997Sken			 * we send them back down to the SIM.  The SIM has
1300229997Sken			 * a wildcard LUN enabled, not whatever target/lun
1301229997Sken			 * these happened to be.
1302229997Sken			 */
1303229997Sken			if (softc->flags & CTLFE_LUN_WILDCARD) {
1304229997Sken				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1305229997Sken				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1306229997Sken			}
1307229997Sken			if (periph->flags & CAM_PERIPH_INVALID) {
1308229997Sken				ctlfe_free_ccb(periph, (union ccb *)atio);
1309229997Sken			} else {
1310260387Sscottl				mtx_unlock(mtx);
1311229997Sken				xpt_action((union ccb *)atio);
1312260387Sscottl				return;
1313229997Sken			}
1314229997Sken		} else {
1315288723Smav			struct ctlfe_cmd_info *cmd_info;
1316229997Sken			struct ccb_scsiio *csio;
1317229997Sken
1318229997Sken			csio = &done_ccb->csio;
1319312585Smav			cmd_info = PRIV_INFO(io);
1320229997Sken
1321229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1322229997Sken
1323229997Sken			io->scsiio.ext_data_len += csio->dxfer_len;
1324229997Sken			if (io->scsiio.ext_data_len >
1325229997Sken			    io->scsiio.kern_total_len) {
1326229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
1327229997Sken					  "done len %u > total %u sent %u\n",
1328229997Sken					  __func__, io->scsiio.tag_num,
1329229997Sken					  io->scsiio.ext_data_len,
1330229997Sken					  io->scsiio.kern_total_len,
1331229997Sken					  io->scsiio.ext_data_filled);
1332229997Sken			}
1333229997Sken			/*
1334229997Sken			 * Translate CAM status to CTL status.  Success
1335229997Sken			 * does not change the overall, ctl_io status.  In
1336229997Sken			 * that case we just set port_status to 0.  If we
1337229997Sken			 * have a failure, though, set a data phase error
1338229997Sken			 * for the overall ctl_io.
1339229997Sken			 */
1340229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1341229997Sken			case CAM_REQ_CMP:
1342229997Sken				io->io_hdr.port_status = 0;
1343229997Sken				break;
1344229997Sken			default:
1345229997Sken				/*
1346245228Sken				 * XXX KDM we probably need to figure out a
1347245228Sken				 * standard set of errors that the SIM
1348245228Sken				 * drivers should return in the event of a
1349245228Sken				 * data transfer failure.  A data phase
1350245228Sken				 * error will at least point the user to a
1351245228Sken				 * data transfer error of some sort.
1352245228Sken				 * Hopefully the SIM printed out some
1353245228Sken				 * additional information to give the user
1354245228Sken				 * a clue what happened.
1355229997Sken				 */
1356229997Sken				io->io_hdr.port_status = 0xbad1;
1357229997Sken				ctl_set_data_phase_error(&io->scsiio);
1358229997Sken				/*
1359229997Sken				 * XXX KDM figure out residual.
1360229997Sken				 */
1361229997Sken				break;
1362229997Sken			}
1363229997Sken			/*
1364229997Sken			 * If we had to break this S/G list into multiple
1365229997Sken			 * pieces, figure out where we are in the list, and
1366229997Sken			 * continue sending pieces if necessary.
1367229997Sken			 */
1368229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1369265641Smav			 && (io->io_hdr.port_status == 0)) {
1370229997Sken				ccb_flags flags;
1371229997Sken				uint8_t scsi_status;
1372229997Sken				uint8_t *data_ptr;
1373229997Sken				uint32_t dxfer_len;
1374229997Sken
1375229997Sken				flags = atio->ccb_h.flags &
1376229997Sken					(CAM_DIS_DISCONNECT|
1377265641Smav					 CAM_TAG_ACTION_VALID);
1378229997Sken
1379265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1380265641Smav				    &dxfer_len, &csio->sglist_cnt);
1381229997Sken
1382229997Sken				scsi_status = 0;
1383229997Sken
1384229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1385229997Sken				 && (dxfer_len == 0)) {
1386229997Sken					printf("%s: tag %04x no status or "
1387229997Sken					       "len cdb = %02x\n", __func__,
1388229997Sken					       atio->tag_id,
1389312845Smav					       atio_cdb_ptr(atio)[0]);
1390229997Sken					printf("%s: tag %04x io status %#x\n",
1391229997Sken					       __func__, atio->tag_id,
1392229997Sken					       io->io_hdr.status);
1393229997Sken				}
1394229997Sken
1395229997Sken				cam_fill_ctio(csio,
1396229997Sken					      /*retries*/ 2,
1397229997Sken					      ctlfedone,
1398229997Sken					      flags,
1399229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1400229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1401229997Sken					      atio->tag_id,
1402229997Sken					      atio->init_id,
1403229997Sken					      scsi_status,
1404229997Sken					      /*data_ptr*/ data_ptr,
1405229997Sken					      /*dxfer_len*/ dxfer_len,
1406229997Sken					      /*timeout*/ 5 * 1000);
1407229997Sken
1408260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1409229997Sken				csio->resid = 0;
1410229997Sken				csio->ccb_h.ccb_atio = atio;
1411229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1412229997Sken				softc->ctios_sent++;
1413260387Sscottl				mtx_unlock(mtx);
1414229997Sken				xpt_action((union ccb *)csio);
1415229997Sken			} else {
1416229997Sken				/*
1417229997Sken				 * Release the CTIO.  The ATIO will be sent back
1418229997Sken				 * down to the SIM once we send status.
1419229997Sken				 */
1420229997Sken				softc->ccbs_freed++;
1421229997Sken				xpt_release_ccb(done_ccb);
1422260387Sscottl				mtx_unlock(mtx);
1423229997Sken
1424229997Sken				/* Call the backend move done callback */
1425229997Sken				io->scsiio.be_move_done(io);
1426229997Sken			}
1427260387Sscottl			return;
1428229997Sken		}
1429229997Sken		break;
1430229997Sken	}
1431229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1432229997Sken		union ctl_io *io;
1433229997Sken		struct ccb_immediate_notify *inot;
1434229997Sken		cam_status status;
1435284794Smav		int send_ctl_io;
1436229997Sken
1437229997Sken		inot = &done_ccb->cin1;
1438229997Sken		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1439229997Sken		       "seq %#x\n", __func__, inot->ccb_h.status,
1440229997Sken		       inot->tag_id, inot->seq_id);
1441229997Sken
1442275878Smav		io = done_ccb->ccb_h.io_ptr;
1443275878Smav		ctl_zero_io(io);
1444229997Sken
1445275878Smav		send_ctl_io = 1;
1446229997Sken
1447275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1448312585Smav		PRIV_CCB(io) = done_ccb;
1449275878Smav		inot->ccb_h.io_ptr = io;
1450288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1451275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1452290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1453290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1454290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1455290776Smav		} else {
1456290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1457290776Smav		}
1458275878Smav		/* XXX KDM should this be the tag_id? */
1459275878Smav		io->taskio.tag_num = inot->seq_id;
1460229997Sken
1461275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1462275878Smav		switch (status) {
1463275878Smav		case CAM_SCSI_BUS_RESET:
1464275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1465275878Smav			break;
1466275878Smav		case CAM_BDR_SENT:
1467275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1468275878Smav			break;
1469275878Smav		case CAM_MESSAGE_RECV:
1470275878Smav			switch (inot->arg) {
1471275878Smav			case MSG_ABORT_TASK_SET:
1472275878Smav				io->taskio.task_action =
1473275878Smav				    CTL_TASK_ABORT_TASK_SET;
1474229997Sken				break;
1475275878Smav			case MSG_TARGET_RESET:
1476290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1477229997Sken				break;
1478275878Smav			case MSG_ABORT_TASK:
1479290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1480229997Sken				break;
1481275878Smav			case MSG_LOGICAL_UNIT_RESET:
1482290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1483275878Smav				break;
1484275878Smav			case MSG_CLEAR_TASK_SET:
1485275878Smav				io->taskio.task_action =
1486290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1487275878Smav				break;
1488275878Smav			case MSG_CLEAR_ACA:
1489290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1490290775Smav				break;
1491290775Smav			case MSG_QUERY_TASK:
1492290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1493290775Smav				break;
1494290775Smav			case MSG_QUERY_TASK_SET:
1495275878Smav				io->taskio.task_action =
1496290775Smav				    CTL_TASK_QUERY_TASK_SET;
1497275878Smav				break;
1498290775Smav			case MSG_QUERY_ASYNC_EVENT:
1499290775Smav				io->taskio.task_action =
1500290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1501290775Smav				break;
1502275878Smav			case MSG_NOOP:
1503229997Sken				send_ctl_io = 0;
1504229997Sken				break;
1505229997Sken			default:
1506275878Smav				xpt_print(periph->path,
1507275878Smav					  "%s: unsupported message 0x%x\n",
1508275878Smav					  __func__, inot->arg);
1509275878Smav				send_ctl_io = 0;
1510275878Smav				break;
1511275878Smav			}
1512275878Smav			break;
1513275878Smav		case CAM_REQ_ABORTED:
1514275878Smav			/*
1515275878Smav			 * This request was sent back by the driver.
1516275878Smav			 * XXX KDM what do we do here?
1517275878Smav			 */
1518275878Smav			send_ctl_io = 0;
1519275878Smav			break;
1520275878Smav		case CAM_REQ_INVALID:
1521275878Smav		case CAM_PROVIDE_FAIL:
1522275878Smav		default:
1523275878Smav			/*
1524275878Smav			 * We should only get here if we're talking
1525275878Smav			 * to a talking to a SIM that is target
1526275878Smav			 * capable but supports the old API.  In
1527275878Smav			 * that case, we need to just free the CCB.
1528275878Smav			 * If we actually send a notify acknowledge,
1529275878Smav			 * it will send that back with an error as
1530275878Smav			 * well.
1531275878Smav			 */
1532237601Sken
1533275878Smav			if ((status != CAM_REQ_INVALID)
1534275878Smav			 && (status != CAM_PROVIDE_FAIL))
1535275878Smav				xpt_print(periph->path,
1536275878Smav					  "%s: unsupported CAM status 0x%x\n",
1537275878Smav					  __func__, status);
1538237601Sken
1539275878Smav			ctlfe_free_ccb(periph, done_ccb);
1540237601Sken
1541275878Smav			goto out;
1542275878Smav		}
1543275878Smav		if (send_ctl_io != 0) {
1544275878Smav			ctl_queue(io);
1545229997Sken		} else {
1546229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1547229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1548229997Sken			xpt_action(done_ccb);
1549229997Sken		}
1550229997Sken		break;
1551229997Sken	}
1552229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1553229997Sken		/*
1554229997Sken		 * Queue this back down to the SIM as an immediate notify.
1555229997Sken		 */
1556304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1557229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1558229997Sken		xpt_action(done_ccb);
1559229997Sken		break;
1560229997Sken	case XPT_SET_SIM_KNOB:
1561229997Sken	case XPT_GET_SIM_KNOB:
1562229997Sken		break;
1563229997Sken	default:
1564229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1565229997Sken		      done_ccb->ccb_h.func_code);
1566229997Sken		break;
1567229997Sken	}
1568260387Sscottl
1569260387Sscottlout:
1570260387Sscottl	mtx_unlock(mtx);
1571229997Sken}
1572229997Sken
1573229997Skenstatic void
1574229997Skenctlfe_onoffline(void *arg, int online)
1575229997Sken{
1576229997Sken	struct ctlfe_softc *bus_softc;
1577229997Sken	union ccb *ccb;
1578229997Sken	cam_status status;
1579229997Sken	struct cam_path *path;
1580229997Sken	int set_wwnn;
1581229997Sken
1582229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1583229997Sken
1584229997Sken	set_wwnn = 0;
1585229997Sken
1586229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1587229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1588229997Sken	if (status != CAM_REQ_CMP) {
1589229997Sken		printf("%s: unable to create path!\n", __func__);
1590229997Sken		return;
1591229997Sken	}
1592275882Smav	ccb = xpt_alloc_ccb();
1593242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1594288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1595288713Smav	xpt_action(ccb);
1596229997Sken
1597229997Sken	/*
1598229997Sken	 * Copan WWN format:
1599229997Sken	 *
1600229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1601229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1602229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1603229997Sken	 * Bits 11-8:			Type of port:
1604229997Sken	 *					1 == N-Port
1605229997Sken	 *					2 == F-Port
1606229997Sken	 *					3 == NL-Port
1607229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1608229997Sken	 */
1609229997Sken	if (online != 0) {
1610229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1611229997Sken#ifdef RANDOM_WWNN
1612229997Sken			uint64_t random_bits;
1613229997Sken#endif
1614229997Sken
1615229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1616229997Sken			       bus_softc->port_name,
1617229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1618229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1619229997Sken			       bus_softc->port_name,
1620229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1621229997Sken
1622229997Sken#ifdef RANDOM_WWNN
1623229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1624229997Sken#endif
1625229997Sken
1626229997Sken			/*
1627229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1628229997Sken			 * take the current WWNN/WWPN from the card, and
1629229997Sken			 * replace the company identifier and the NL-Port
1630229997Sken			 * indicator and the port number (for the WWPN).
1631229997Sken			 * This should be replaced later with ddb_GetWWNN,
1632229997Sken			 * or possibly a more centralized scheme.  (It
1633229997Sken			 * would be nice to have the WWNN/WWPN for each
1634268677Smav			 * port stored in the ctl_port structure.)
1635229997Sken			 */
1636229997Sken#ifdef RANDOM_WWNN
1637229997Sken			ccb->knob.xport_specific.fc.wwnn =
1638229997Sken				(random_bits &
1639229997Sken				0x0000000fffffff00ULL) |
1640229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1641229997Sken				/* NL-Port */    0x0300;
1642229997Sken			ccb->knob.xport_specific.fc.wwpn =
1643229997Sken				(random_bits &
1644229997Sken				0x0000000fffffff00ULL) |
1645229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1646229997Sken				/* NL-Port */    0x3000 |
1647268677Smav				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1648229997Sken
1649229997Sken			/*
1650229997Sken			 * This is a bit of an API break/reversal, but if
1651229997Sken			 * we're doing the random WWNN that's a little
1652229997Sken			 * different anyway.  So record what we're actually
1653229997Sken			 * using with the frontend code so it's reported
1654229997Sken			 * accurately.
1655229997Sken			 */
1656268683Smav			ctl_port_set_wwns(&bus_softc->port,
1657268683Smav			    true, ccb->knob.xport_specific.fc.wwnn,
1658268683Smav			    true, ccb->knob.xport_specific.fc.wwpn);
1659229997Sken			set_wwnn = 1;
1660229997Sken#else /* RANDOM_WWNN */
1661229997Sken			/*
1662229997Sken			 * If the user has specified a WWNN/WWPN, send them
1663229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1664229997Sken			 * has reported.
1665229997Sken			 */
1666284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1667284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1668229997Sken				ccb->knob.xport_specific.fc.wwnn =
1669284586Smav				    bus_softc->port.wwnn;
1670284586Smav				set_wwnn = 1;
1671284586Smav			} else {
1672284586Smav				ctl_port_set_wwns(&bus_softc->port,
1673284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1674284586Smav				    false, 0);
1675284586Smav			}
1676284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1677284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1678229997Sken				ccb->knob.xport_specific.fc.wwpn =
1679284586Smav				    bus_softc->port.wwpn;
1680229997Sken				set_wwnn = 1;
1681229997Sken			} else {
1682268683Smav				ctl_port_set_wwns(&bus_softc->port,
1683284586Smav				    false, 0,
1684268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1685229997Sken			}
1686229997Sken#endif /* RANDOM_WWNN */
1687229997Sken
1688229997Sken
1689229997Sken			if (set_wwnn != 0) {
1690229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1691229997Sken				       bus_softc->port_name,
1692229997Sken				ccb->knob.xport_specific.fc.wwnn);
1693229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1694229997Sken				       bus_softc->port_name,
1695229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1696229997Sken			}
1697229997Sken		} else {
1698229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1699229997Sken			       bus_softc->port_name);
1700229997Sken		}
1701229997Sken	}
1702229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1703229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1704229997Sken	if (set_wwnn != 0)
1705229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1706229997Sken
1707229997Sken	if (online != 0)
1708288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1709229997Sken	else
1710288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1711229997Sken
1712229997Sken	xpt_action(ccb);
1713229997Sken
1714229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1715229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1716229997Sken		       "status %#x\n",
1717229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1718229997Sken		       (online != 0) ? "enable" : "disable",
1719229997Sken		       ccb->ccb_h.status);
1720229997Sken	} else {
1721229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1722229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1723229997Sken		       (online != 0) ? "enable" : "disable");
1724229997Sken	}
1725229997Sken
1726229997Sken	xpt_free_path(path);
1727275882Smav	xpt_free_ccb(ccb);
1728229997Sken}
1729229997Sken
1730229997Skenstatic void
1731229997Skenctlfe_online(void *arg)
1732229997Sken{
1733245228Sken	struct ctlfe_softc *bus_softc;
1734245228Sken	struct cam_path *path;
1735245228Sken	cam_status status;
1736245228Sken	struct ctlfe_lun_softc *lun_softc;
1737274388Smav	struct cam_periph *periph;
1738245228Sken
1739245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1740245228Sken
1741245228Sken	/*
1742245228Sken	 * Create the wildcard LUN before bringing the port online.
1743245228Sken	 */
1744245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1745245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1746245228Sken				 CAM_LUN_WILDCARD);
1747245228Sken	if (status != CAM_REQ_CMP) {
1748245228Sken		printf("%s: unable to create path for wildcard periph\n",
1749245228Sken				__func__);
1750245228Sken		return;
1751245228Sken	}
1752245228Sken
1753275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1754245228Sken
1755260387Sscottl	xpt_path_lock(path);
1756274388Smav	periph = cam_periph_find(path, "ctl");
1757274388Smav	if (periph != NULL) {
1758274388Smav		/* We've already got a periph, no need to alloc a new one. */
1759274388Smav		xpt_path_unlock(path);
1760274388Smav		xpt_free_path(path);
1761274388Smav		free(lun_softc, M_CTLFE);
1762274388Smav		return;
1763274388Smav	}
1764245228Sken	lun_softc->parent_softc = bus_softc;
1765245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1766245228Sken
1767245228Sken	status = cam_periph_alloc(ctlferegister,
1768245228Sken				  ctlfeoninvalidate,
1769245228Sken				  ctlfecleanup,
1770245228Sken				  ctlfestart,
1771245228Sken				  "ctl",
1772245228Sken				  CAM_PERIPH_BIO,
1773245228Sken				  path,
1774245228Sken				  ctlfeasync,
1775245228Sken				  0,
1776245228Sken				  lun_softc);
1777245228Sken
1778245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1779245228Sken		const struct cam_status_entry *entry;
1780245228Sken
1781245228Sken		entry = cam_fetch_status_entry(status);
1782245228Sken		printf("%s: CAM error %s (%#x) returned from "
1783245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1784245228Sken		       entry->status_text : "Unknown", status);
1785274388Smav		free(lun_softc, M_CTLFE);
1786245228Sken	}
1787245228Sken
1788260387Sscottl	xpt_path_unlock(path);
1789275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1790260387Sscottl	xpt_free_path(path);
1791229997Sken}
1792229997Sken
1793229997Skenstatic void
1794229997Skenctlfe_offline(void *arg)
1795229997Sken{
1796245228Sken	struct ctlfe_softc *bus_softc;
1797245228Sken	struct cam_path *path;
1798245228Sken	cam_status status;
1799245228Sken	struct cam_periph *periph;
1800245228Sken
1801245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1802245228Sken
1803275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1804275882Smav
1805245228Sken	/*
1806245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1807245228Sken	 * the port offline.
1808245228Sken	 */
1809245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1810245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1811245228Sken				 CAM_LUN_WILDCARD);
1812245228Sken	if (status != CAM_REQ_CMP) {
1813245228Sken		printf("%s: unable to create path for wildcard periph\n",
1814245228Sken		       __func__);
1815245228Sken		return;
1816245228Sken	}
1817260387Sscottl	xpt_path_lock(path);
1818245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1819245228Sken		cam_periph_invalidate(periph);
1820260387Sscottl	xpt_path_unlock(path);
1821245228Sken	xpt_free_path(path);
1822229997Sken}
1823229997Sken
1824229997Sken/*
1825229997Sken * This will get called to enable a LUN on every bus that is attached to
1826229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1827229997Sken */
1828229997Skenstatic int
1829284798Smavctlfe_lun_enable(void *arg, int lun_id)
1830229997Sken{
1831229997Sken	struct ctlfe_softc *bus_softc;
1832229997Sken	struct ctlfe_lun_softc *softc;
1833229997Sken	struct cam_path *path;
1834229997Sken	struct cam_periph *periph;
1835229997Sken	cam_status status;
1836229997Sken
1837229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1838290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1839290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1840229997Sken
1841260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1842290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1843229997Sken	/* XXX KDM need some way to return status to CTL here? */
1844229997Sken	if (status != CAM_REQ_CMP) {
1845229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1846229997Sken		       status);
1847229997Sken		return (1);
1848229997Sken	}
1849229997Sken
1850229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1851260387Sscottl	xpt_path_lock(path);
1852229997Sken	periph = cam_periph_find(path, "ctl");
1853229997Sken	if (periph != NULL) {
1854229997Sken		/* We've already got a periph, no need to alloc a new one. */
1855260387Sscottl		xpt_path_unlock(path);
1856229997Sken		xpt_free_path(path);
1857229997Sken		free(softc, M_CTLFE);
1858229997Sken		return (0);
1859229997Sken	}
1860229997Sken	softc->parent_softc = bus_softc;
1861229997Sken
1862229997Sken	status = cam_periph_alloc(ctlferegister,
1863229997Sken				  ctlfeoninvalidate,
1864229997Sken				  ctlfecleanup,
1865229997Sken				  ctlfestart,
1866229997Sken				  "ctl",
1867229997Sken				  CAM_PERIPH_BIO,
1868229997Sken				  path,
1869229997Sken				  ctlfeasync,
1870229997Sken				  0,
1871229997Sken				  softc);
1872229997Sken
1873274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1874274388Smav		const struct cam_status_entry *entry;
1875274388Smav
1876274388Smav		entry = cam_fetch_status_entry(status);
1877274388Smav		printf("%s: CAM error %s (%#x) returned from "
1878274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1879274388Smav		       entry->status_text : "Unknown", status);
1880274388Smav		free(softc, M_CTLFE);
1881274388Smav	}
1882274388Smav
1883260387Sscottl	xpt_path_unlock(path);
1884244016Sken	xpt_free_path(path);
1885229997Sken	return (0);
1886229997Sken}
1887229997Sken
1888229997Sken/*
1889245228Sken * This will get called when the user removes a LUN to disable that LUN
1890245228Sken * on every bus that is attached to CTL.
1891229997Sken */
1892229997Skenstatic int
1893284798Smavctlfe_lun_disable(void *arg, int lun_id)
1894229997Sken{
1895229997Sken	struct ctlfe_softc *softc;
1896229997Sken	struct ctlfe_lun_softc *lun_softc;
1897229997Sken
1898229997Sken	softc = (struct ctlfe_softc *)arg;
1899290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1900290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1901229997Sken
1902260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1903229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1904229997Sken		struct cam_path *path;
1905229997Sken
1906229997Sken		path = lun_softc->periph->path;
1907229997Sken
1908290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1909229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1910229997Sken			break;
1911229997Sken		}
1912229997Sken	}
1913229997Sken	if (lun_softc == NULL) {
1914260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1915284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1916229997Sken		return (1);
1917229997Sken	}
1918260387Sscottl	cam_periph_acquire(lun_softc->periph);
1919260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1920229997Sken
1921260387Sscottl	cam_periph_lock(lun_softc->periph);
1922229997Sken	cam_periph_invalidate(lun_softc->periph);
1923260387Sscottl	cam_periph_unlock(lun_softc->periph);
1924260387Sscottl	cam_periph_release(lun_softc->periph);
1925229997Sken	return (0);
1926229997Sken}
1927229997Sken
1928229997Skenstatic void
1929229997Skenctlfe_dump_sim(struct cam_sim *sim)
1930229997Sken{
1931229997Sken
1932229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1933229997Sken	       sim->sim_name, sim->unit_number,
1934229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1935229997Sken}
1936229997Sken
1937229997Sken/*
1938229997Sken * Assumes that the SIM lock is held.
1939229997Sken */
1940229997Skenstatic void
1941229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1942229997Sken{
1943229997Sken	struct ccb_hdr *hdr;
1944229997Sken	struct cam_periph *periph;
1945229997Sken	int num_items;
1946229997Sken
1947229997Sken	periph = softc->periph;
1948229997Sken	num_items = 0;
1949229997Sken
1950229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1951275880Smav		union ctl_io *io = hdr->io_ptr;
1952229997Sken
1953229997Sken		num_items++;
1954229997Sken
1955229997Sken		/*
1956229997Sken		 * Only regular SCSI I/O is put on the work
1957229997Sken		 * queue, so we can print sense here.  There may be no
1958229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1959229997Sken		 * print out the CCB as well.
1960229997Sken		 *
1961229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1962229997Sken		 * CTL is merged in with CAM.
1963229997Sken		 */
1964229997Sken		ctl_io_error_print(io, NULL);
1965229997Sken
1966229997Sken		/*
1967275880Smav		 * Print DMA status if we are DMA_QUEUED.
1968229997Sken		 */
1969275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1970275880Smav			xpt_print(periph->path,
1971275880Smav			    "Total %u, Current %u, Resid %u\n",
1972275880Smav			    io->scsiio.kern_total_len,
1973275880Smav			    io->scsiio.kern_data_len,
1974275880Smav			    io->scsiio.kern_data_resid);
1975275880Smav		}
1976229997Sken	}
1977229997Sken
1978229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1979229997Sken		  num_items);
1980250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
1981229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1982229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1983229997Sken		  (uintmax_t)softc->ccbs_freed);
1984229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1985229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
1986229997Sken		  softc->ctios_returned), softc->ctios_sent,
1987229997Sken		  softc->ctios_returned);
1988229997Sken}
1989229997Sken
1990229997Sken/*
1991275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1992275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1993275880Smav * to the adapter.
1994229997Sken */
1995229997Skenstatic void
1996275880Smavctlfe_datamove(union ctl_io *io)
1997229997Sken{
1998275880Smav	union ccb *ccb;
1999275880Smav	struct cam_periph *periph;
2000229997Sken	struct ctlfe_lun_softc *softc;
2001229997Sken
2002275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
2003275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
2004229997Sken
2005312585Smav	ccb = PRIV_CCB(io);
2006275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
2007275880Smav	cam_periph_lock(periph);
2008275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
2009275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2010275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2011275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2012275880Smav	TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2013275880Smav			  periph_links.tqe);
2014229997Sken	xpt_schedule(periph, /*priority*/ 1);
2015275880Smav	cam_periph_unlock(periph);
2016229997Sken}
2017229997Sken
2018229997Skenstatic void
2019275880Smavctlfe_done(union ctl_io *io)
2020229997Sken{
2021229997Sken	union ccb *ccb;
2022229997Sken	struct cam_periph *periph;
2023229997Sken	struct ctlfe_lun_softc *softc;
2024229997Sken
2025312585Smav	ccb = PRIV_CCB(io);
2026229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
2027260387Sscottl	cam_periph_lock(periph);
2028229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
2029229997Sken
2030229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
2031229997Sken		/*
2032229997Sken		 * Task management commands don't require any further
2033229997Sken		 * communication back to the adapter.  Requeue the CCB
2034229997Sken		 * to the adapter, and free the CTL I/O.
2035229997Sken		 */
2036229997Sken		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2037229997Sken			  "tag %#x seq %#x\n", __func__,
2038229997Sken			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2039229997Sken		/*
2040229997Sken		 * Send the notify acknowledge down to the SIM, to let it
2041229997Sken		 * know we processed the task management command.
2042229997Sken		 */
2043229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
2044229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2045304417Smav		switch (io->taskio.task_status) {
2046304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
2047304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
2048304417Smav			break;
2049304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
2050304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
2051304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2052304417Smav			break;
2053304417Smav		case CTL_TASK_FUNCTION_REJECTED:
2054304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
2055304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2056304417Smav			break;
2057304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
2058304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
2059304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2060304417Smav			break;
2061304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2062304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
2063304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2064304417Smav			break;
2065304417Smav		}
2066304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
2067229997Sken		xpt_action(ccb);
2068275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
2069275881Smav		if (softc->flags & CTLFE_LUN_WILDCARD) {
2070275881Smav			ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
2071275881Smav			ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
2072275881Smav		}
2073275881Smav		if (periph->flags & CAM_PERIPH_INVALID) {
2074275881Smav			ctlfe_free_ccb(periph, ccb);
2075275881Smav		} else {
2076275881Smav			cam_periph_unlock(periph);
2077275881Smav			xpt_action(ccb);
2078275881Smav			return;
2079275881Smav		}
2080229997Sken	} else {
2081275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2082229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2083229997Sken				  periph_links.tqe);
2084275880Smav		xpt_schedule(periph, /*priority*/ 1);
2085229997Sken	}
2086229997Sken
2087260387Sscottl	cam_periph_unlock(periph);
2088229997Sken}
2089229997Sken
2090229997Skenstatic void
2091229997Skenctlfe_dump(void)
2092229997Sken{
2093229997Sken	struct ctlfe_softc *bus_softc;
2094275880Smav	struct ctlfe_lun_softc *lun_softc;
2095229997Sken
2096229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2097229997Sken		ctlfe_dump_sim(bus_softc->sim);
2098275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
2099229997Sken			ctlfe_dump_queue(lun_softc);
2100229997Sken	}
2101229997Sken}
2102