scsi_ctl.c revision 304417
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 304417 2016-08-18 11:38:47Z 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
188229997Skenint			ctlfeinitialize(void);
189229997Skenvoid			ctlfeshutdown(void);
190268677Smavstatic periph_init_t	ctlfeperiphinit;
191229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
192229997Sken				   struct cam_path *path, void *arg);
193229997Skenstatic periph_ctor_t	ctlferegister;
194229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
195229997Skenstatic periph_dtor_t	ctlfecleanup;
196229997Skenstatic periph_start_t	ctlfestart;
197229997Skenstatic void		ctlfedone(struct cam_periph *periph,
198229997Sken				  union ccb *done_ccb);
199229997Sken
200229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
201229997Skenstatic void 		ctlfe_online(void *arg);
202229997Skenstatic void 		ctlfe_offline(void *arg);
203284798Smavstatic int 		ctlfe_lun_enable(void *arg, int lun_id);
204284798Smavstatic int 		ctlfe_lun_disable(void *arg, int lun_id);
205229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
206229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
207275880Smavstatic void 		ctlfe_datamove(union ctl_io *io);
208275880Smavstatic void 		ctlfe_done(union ctl_io *io);
209229997Skenstatic void 		ctlfe_dump(void);
210229997Sken
211229997Skenstatic struct periph_driver ctlfe_driver =
212229997Sken{
213268677Smav	ctlfeperiphinit, "ctl",
214273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
215273316Smav	CAM_PERIPH_DRV_EARLY
216229997Sken};
217229997Sken
218268677Smavstatic struct ctl_frontend ctlfe_frontend =
219268677Smav{
220273318Smav	.name = "camtgt",
221268677Smav	.init = ctlfeinitialize,
222268677Smav	.fe_dump = ctlfe_dump,
223268677Smav	.shutdown = ctlfeshutdown,
224249009Strasz};
225268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
226249009Strasz
227229997Skenvoid
228229997Skenctlfeshutdown(void)
229229997Sken{
230229997Sken	return;
231229997Sken}
232229997Sken
233268677Smavint
234268677Smavctlfeinitialize(void)
235229997Sken{
236229997Sken
237229997Sken	STAILQ_INIT(&ctlfe_softc_list);
238229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
239268677Smav	periphdriver_register(&ctlfe_driver);
240268677Smav	return (0);
241268677Smav}
242229997Sken
243268677Smavvoid
244268677Smavctlfeperiphinit(void)
245268677Smav{
246268677Smav	cam_status status;
247229997Sken
248229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
249229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
250229997Sken	if (status != CAM_REQ_CMP) {
251229997Sken		printf("ctl: Failed to attach async callback due to CAM "
252229997Sken		       "status 0x%x!\n", status);
253229997Sken	}
254229997Sken}
255229997Sken
256229997Skenstatic void
257229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
258229997Sken{
259273317Smav	struct ctlfe_softc *softc;
260229997Sken
261229997Sken#ifdef CTLFEDEBUG
262229997Sken	printf("%s: entered\n", __func__);
263229997Sken#endif
264229997Sken
265273317Smav	mtx_lock(&ctlfe_list_mtx);
266273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
267273317Smav		if (softc->path_id == xpt_path_path_id(path))
268273317Smav			break;
269273317Smav	}
270273317Smav	mtx_unlock(&ctlfe_list_mtx);
271273317Smav
272229997Sken	/*
273229997Sken	 * When a new path gets registered, and it is capable of target
274229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
275229997Sken	 * selective, but for now this will be sufficient.
276229997Sken 	 */
277229997Sken	switch (code) {
278229997Sken	case AC_PATH_REGISTERED: {
279268677Smav		struct ctl_port *port;
280229997Sken		struct ccb_pathinq *cpi;
281229997Sken		int retval;
282229997Sken
283229997Sken		cpi = (struct ccb_pathinq *)arg;
284229997Sken
285229997Sken		/* Don't attach if it doesn't support target mode */
286229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
287230033Sken#ifdef CTLFEDEBUG
288229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
289229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
290230033Sken#endif
291229997Sken			break;
292229997Sken		}
293229997Sken
294273317Smav		if (softc != NULL) {
295273317Smav#ifdef CTLFEDEBUG
296273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
297273317Smav			       __func__, xpt_path_path_id(path));
298273317Smav#endif
299273317Smav			break;
300273317Smav		}
301273317Smav
302229997Sken#ifdef CTLFE_INIT_ENABLE
303229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
304229997Sken			union ccb *ccb;
305229997Sken
306229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
307229997Sken						  M_NOWAIT | M_ZERO);
308229997Sken			if (ccb == NULL) {
309229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
310229997Sken				return;
311229997Sken			}
312260387Sscottl			xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
313229997Sken
314229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
315229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
316229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
317229997Sken
318229997Sken			xpt_action(ccb);
319229997Sken
320229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
321229997Sken			     CAM_REQ_CMP) {
322229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
323229997Sken				       "enable failed with status %#x\n",
324229997Sken				       __func__, cpi->dev_name,
325229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
326229997Sken				       ccb->ccb_h.status);
327229997Sken			} else {
328229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
329229997Sken				       "enable succeeded\n",
330229997Sken				       __func__, cpi->dev_name,
331229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
332229997Sken			}
333229997Sken
334229997Sken			free(ccb, M_TEMP);
335229997Sken
336229997Sken			break;
337229997Sken		} else {
338229997Sken			ctlfe_num_targets++;
339229997Sken		}
340229997Sken
341229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
342229997Sken		       ctlfe_num_targets);
343229997Sken#endif /* CTLFE_INIT_ENABLE */
344229997Sken
345229997Sken		/*
346229997Sken		 * We're in an interrupt context here, so we have to
347229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
348229997Sken		 * can't allocate memory.
349229997Sken		 */
350273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
351273317Smav		if (softc == NULL) {
352229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
353273317Smav			       __func__, sizeof(*softc));
354229997Sken			return;
355229997Sken		}
356229997Sken
357273317Smav		softc->path_id = cpi->ccb_h.path_id;
358288713Smav		softc->target_id = cpi->initiator_id;
359273317Smav		softc->sim = xpt_path_sim(path);
360290776Smav		softc->hba_misc = cpi->hba_misc;
361265641Smav		if (cpi->maxio != 0)
362273317Smav			softc->maxio = cpi->maxio;
363265641Smav		else
364273317Smav			softc->maxio = DFLTPHYS;
365273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
366273317Smav		STAILQ_INIT(&softc->lun_softc_list);
367229997Sken
368273317Smav		port = &softc->port;
369268677Smav		port->frontend = &ctlfe_frontend;
370229997Sken
371229997Sken		/*
372229997Sken		 * XXX KDM should we be more accurate here ?
373229997Sken		 */
374229997Sken		if (cpi->transport == XPORT_FC)
375268677Smav			port->port_type = CTL_PORT_FC;
376268694Smav		else if (cpi->transport == XPORT_SAS)
377268694Smav			port->port_type = CTL_PORT_SAS;
378229997Sken		else
379268677Smav			port->port_type = CTL_PORT_SCSI;
380229997Sken
381229997Sken		/* XXX KDM what should the real number be here? */
382268677Smav		port->num_requested_ctl_io = 4096;
383273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
384229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
385229997Sken		/*
386229997Sken		 * XXX KDM it would be nice to allocate storage in the
387229997Sken		 * frontend structure itself.
388229997Sken	 	 */
389273317Smav		port->port_name = softc->port_name;
390273319Smav		port->physical_port = cpi->bus_id;
391273319Smav		port->virtual_port = 0;
392268677Smav		port->port_online = ctlfe_online;
393268677Smav		port->port_offline = ctlfe_offline;
394273317Smav		port->onoff_arg = softc;
395268677Smav		port->lun_enable = ctlfe_lun_enable;
396268677Smav		port->lun_disable = ctlfe_lun_disable;
397273317Smav		port->targ_lun_arg = softc;
398275880Smav		port->fe_datamove = ctlfe_datamove;
399275880Smav		port->fe_done = ctlfe_done;
400229997Sken		/*
401229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
402229997Sken		 * number of targets supported.
403229997Sken		 */
404268677Smav		port->max_targets = cpi->max_target;
405268677Smav		port->max_target_id = cpi->max_target;
406288732Smav		port->targ_port = -1;
407229997Sken
408229997Sken		/*
409229997Sken		 * XXX KDM need to figure out whether we're the master or
410229997Sken		 * slave.
411229997Sken		 */
412230033Sken#ifdef CTLFEDEBUG
413268677Smav		printf("%s: calling ctl_port_register() for %s%d\n",
414229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
415230033Sken#endif
416275493Smav		retval = ctl_port_register(port);
417229997Sken		if (retval != 0) {
418268677Smav			printf("%s: ctl_port_register() failed with "
419229997Sken			       "error %d!\n", __func__, retval);
420273317Smav			mtx_destroy(&softc->lun_softc_mtx);
421273317Smav			free(softc, M_CTLFE);
422229997Sken			break;
423229997Sken		} else {
424229997Sken			mtx_lock(&ctlfe_list_mtx);
425273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
426229997Sken			mtx_unlock(&ctlfe_list_mtx);
427229997Sken		}
428229997Sken
429245228Sken		break;
430245228Sken	}
431245228Sken	case AC_PATH_DEREGISTERED: {
432245228Sken
433245228Sken		if (softc != NULL) {
434245228Sken			/*
435245228Sken			 * XXX KDM are we certain at this point that there
436245228Sken			 * are no outstanding commands for this frontend?
437245228Sken			 */
438273317Smav			mtx_lock(&ctlfe_list_mtx);
439273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
440273317Smav			    links);
441273317Smav			mtx_unlock(&ctlfe_list_mtx);
442268677Smav			ctl_port_deregister(&softc->port);
443260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
444245228Sken			free(softc, M_CTLFE);
445229997Sken		}
446229997Sken		break;
447229997Sken	}
448229997Sken	case AC_CONTRACT: {
449229997Sken		struct ac_contract *ac;
450229997Sken
451229997Sken		ac = (struct ac_contract *)arg;
452229997Sken
453229997Sken		switch (ac->contract_number) {
454229997Sken		case AC_CONTRACT_DEV_CHG: {
455229997Sken			struct ac_device_changed *dev_chg;
456273317Smav			int retval;
457229997Sken
458229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
459229997Sken
460236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
461229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
462229997Sken			       xpt_path_path_id(path), dev_chg->target,
463229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
464229997Sken
465273317Smav			if (softc == NULL) {
466229997Sken				printf("%s: CTL port for CAM path %u not "
467229997Sken				       "found!\n", __func__,
468229997Sken				       xpt_path_path_id(path));
469229997Sken				break;
470229997Sken			}
471229997Sken			if (dev_chg->arrived != 0) {
472268692Smav				retval = ctl_add_initiator(&softc->port,
473268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
474229997Sken			} else {
475268692Smav				retval = ctl_remove_initiator(&softc->port,
476268692Smav				    dev_chg->target);
477229997Sken			}
478229997Sken
479268692Smav			if (retval < 0) {
480229997Sken				printf("%s: could not %s port %d iid %u "
481229997Sken				       "WWPN %#jx!\n", __func__,
482229997Sken				       (dev_chg->arrived != 0) ? "add" :
483268677Smav				       "remove", softc->port.targ_port,
484229997Sken				       dev_chg->target,
485229997Sken				       (uintmax_t)dev_chg->wwpn);
486229997Sken			}
487229997Sken			break;
488229997Sken		}
489229997Sken		default:
490229997Sken			printf("%s: unsupported contract number %ju\n",
491229997Sken			       __func__, (uintmax_t)ac->contract_number);
492229997Sken			break;
493229997Sken		}
494229997Sken		break;
495229997Sken	}
496229997Sken	default:
497229997Sken		break;
498229997Sken	}
499229997Sken}
500229997Sken
501229997Skenstatic cam_status
502229997Skenctlferegister(struct cam_periph *periph, void *arg)
503229997Sken{
504229997Sken	struct ctlfe_softc *bus_softc;
505229997Sken	struct ctlfe_lun_softc *softc;
506229997Sken	union ccb en_lun_ccb;
507229997Sken	cam_status status;
508229997Sken	int i;
509229997Sken
510229997Sken	softc = (struct ctlfe_lun_softc *)arg;
511229997Sken	bus_softc = softc->parent_softc;
512229997Sken
513229997Sken	TAILQ_INIT(&softc->work_queue);
514229997Sken	softc->periph = periph;
515229997Sken	periph->softc = softc;
516229997Sken
517242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
518229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
519229997Sken	en_lun_ccb.cel.grp6_len = 0;
520229997Sken	en_lun_ccb.cel.grp7_len = 0;
521229997Sken	en_lun_ccb.cel.enable = 1;
522229997Sken	xpt_action(&en_lun_ccb);
523229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
524229997Sken	if (status != CAM_REQ_CMP) {
525229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
526229997Sken			  __func__, en_lun_ccb.ccb_h.status);
527229997Sken		return (status);
528229997Sken	}
529229997Sken
530229997Sken	status = CAM_REQ_CMP;
531229997Sken
532229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
533229997Sken		union ccb *new_ccb;
534275878Smav		union ctl_io *new_io;
535288723Smav		struct ctlfe_cmd_info *cmd_info;
536229997Sken
537229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
538236426Smjacob					      M_ZERO|M_NOWAIT);
539229997Sken		if (new_ccb == NULL) {
540229997Sken			status = CAM_RESRC_UNAVAIL;
541229997Sken			break;
542229997Sken		}
543275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
544275878Smav		if (new_io == NULL) {
545275878Smav			free(new_ccb, M_CTLFE);
546275878Smav			status = CAM_RESRC_UNAVAIL;
547275878Smav			break;
548275878Smav		}
549288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
550288723Smav		    M_ZERO | M_NOWAIT);
551288723Smav		if (cmd_info == NULL) {
552288723Smav			ctl_free_io(new_io);
553288723Smav			free(new_ccb, M_CTLFE);
554288723Smav			status = CAM_RESRC_UNAVAIL;
555288723Smav			break;
556288723Smav		}
557288723Smav		new_io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr = cmd_info;
558284793Smav		softc->atios_alloced++;
559275878Smav		new_ccb->ccb_h.io_ptr = new_io;
560275878Smav
561229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
562229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
563229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
564260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
565229997Sken		xpt_action(new_ccb);
566229997Sken		status = new_ccb->ccb_h.status;
567229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
568288723Smav			free(cmd_info, M_CTLFE);
569275878Smav			ctl_free_io(new_io);
570229997Sken			free(new_ccb, M_CTLFE);
571229997Sken			break;
572229997Sken		}
573229997Sken	}
574229997Sken
575229997Sken	status = cam_periph_acquire(periph);
576229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
577229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
578229997Sken			  "count, status = %#x\n", __func__, status);
579229997Sken		return (status);
580229997Sken	}
581229997Sken
582229997Sken	if (i == 0) {
583229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
584229997Sken			  "status 0x%x\n", __func__, status);
585229997Sken		return (CAM_REQ_CMP_ERR);
586229997Sken	}
587229997Sken
588229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
589229997Sken		union ccb *new_ccb;
590275878Smav		union ctl_io *new_io;
591229997Sken
592229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
593236426Smjacob					      M_ZERO|M_NOWAIT);
594229997Sken		if (new_ccb == NULL) {
595229997Sken			status = CAM_RESRC_UNAVAIL;
596229997Sken			break;
597229997Sken		}
598275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
599275878Smav		if (new_io == NULL) {
600275878Smav			free(new_ccb, M_CTLFE);
601275878Smav			status = CAM_RESRC_UNAVAIL;
602275878Smav			break;
603275878Smav		}
604284793Smav		softc->inots_alloced++;
605275878Smav		new_ccb->ccb_h.io_ptr = new_io;
606229997Sken
607229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
608229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
609229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
610260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
611229997Sken		xpt_action(new_ccb);
612229997Sken		status = new_ccb->ccb_h.status;
613237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
614237601Sken			/*
615237601Sken			 * Note that we don't free the CCB here.  If the
616237601Sken			 * status is not CAM_REQ_INPROG, then we're
617237601Sken			 * probably talking to a SIM that says it is
618237601Sken			 * target-capable but doesn't support the
619237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
620237601Sken			 * older API.  In that case, it'll call xpt_done()
621237601Sken			 * on the CCB, and we need to free it in our done
622237601Sken			 * routine as a result.
623237601Sken			 */
624229997Sken			break;
625229997Sken		}
626229997Sken	}
627237601Sken	if ((i == 0)
628237601Sken	 || (status != CAM_REQ_INPROG)) {
629229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
630229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
631229997Sken		return (CAM_REQ_CMP_ERR);
632229997Sken	}
633275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
634275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
635275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
636229997Sken	return (CAM_REQ_CMP);
637229997Sken}
638229997Sken
639229997Skenstatic void
640229997Skenctlfeoninvalidate(struct cam_periph *periph)
641229997Sken{
642229997Sken	union ccb en_lun_ccb;
643229997Sken	cam_status status;
644260387Sscottl	struct ctlfe_softc *bus_softc;
645229997Sken	struct ctlfe_lun_softc *softc;
646229997Sken
647229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
648229997Sken
649242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
650229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
651229997Sken	en_lun_ccb.cel.grp6_len = 0;
652229997Sken	en_lun_ccb.cel.grp7_len = 0;
653229997Sken	en_lun_ccb.cel.enable = 0;
654229997Sken	xpt_action(&en_lun_ccb);
655229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
656229997Sken	if (status != CAM_REQ_CMP) {
657229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
658229997Sken			  __func__, en_lun_ccb.ccb_h.status);
659229997Sken		/*
660229997Sken		 * XXX KDM what do we do now?
661229997Sken		 */
662229997Sken	}
663260387Sscottl
664260387Sscottl	bus_softc = softc->parent_softc;
665260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
666260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
667260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
668229997Sken}
669229997Sken
670229997Skenstatic void
671229997Skenctlfecleanup(struct cam_periph *periph)
672229997Sken{
673229997Sken	struct ctlfe_lun_softc *softc;
674229997Sken
675229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
676229997Sken
677284793Smav	KASSERT(softc->ccbs_freed == softc->ccbs_alloced, ("%s: "
678284793Smav		"ccbs_freed %ju != ccbs_alloced %ju", __func__,
679284793Smav		softc->ccbs_freed, softc->ccbs_alloced));
680284793Smav	KASSERT(softc->ctios_returned == softc->ctios_sent, ("%s: "
681284793Smav		"ctios_returned %ju != ctios_sent %ju", __func__,
682284793Smav		softc->ctios_returned, softc->ctios_sent));
683284793Smav	KASSERT(softc->atios_freed == softc->atios_alloced, ("%s: "
684284793Smav		"atios_freed %ju != atios_alloced %ju", __func__,
685284793Smav		softc->atios_freed, softc->atios_alloced));
686284793Smav	KASSERT(softc->inots_freed == softc->inots_alloced, ("%s: "
687284793Smav		"inots_freed %ju != inots_alloced %ju", __func__,
688284793Smav		softc->inots_freed, softc->inots_alloced));
689245228Sken
690229997Sken	free(softc, M_CTLFE);
691229997Sken}
692229997Sken
693229997Skenstatic void
694265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
695265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
696265641Smav    u_int16_t *sglist_cnt)
697265641Smav{
698265641Smav	struct ctlfe_softc *bus_softc;
699288723Smav	struct ctlfe_cmd_info *cmd_info;
700265641Smav	struct ctl_sg_entry *ctl_sglist;
701265641Smav	bus_dma_segment_t *cam_sglist;
702265641Smav	size_t off;
703265641Smav	int i, idx;
704265641Smav
705288723Smav	cmd_info = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr;
706265641Smav	bus_softc = softc->parent_softc;
707265641Smav
708265641Smav	/*
709265641Smav	 * Set the direction, relative to the initiator.
710265641Smav	 */
711265641Smav	*flags &= ~CAM_DIR_MASK;
712265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
713265641Smav		*flags |= CAM_DIR_IN;
714265641Smav	else
715265641Smav		*flags |= CAM_DIR_OUT;
716265641Smav
717265641Smav	*flags &= ~CAM_DATA_MASK;
718265641Smav	idx = cmd_info->cur_transfer_index;
719265641Smav	off = cmd_info->cur_transfer_off;
720265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
721265641Smav	if (io->scsiio.kern_sg_entries == 0) {
722265641Smav		/* No S/G list. */
723265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
724265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
725265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
726265641Smav		} else {
727265641Smav			*dxfer_len = bus_softc->maxio;
728265641Smav			cmd_info->cur_transfer_index = -1;
729265641Smav			cmd_info->cur_transfer_off = bus_softc->maxio;
730265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
731265641Smav		}
732265641Smav		*sglist_cnt = 0;
733265641Smav
734265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
735265641Smav			*flags |= CAM_DATA_PADDR;
736265641Smav		else
737265641Smav			*flags |= CAM_DATA_VADDR;
738265641Smav	} else {
739265641Smav		/* S/G list with physical or virtual pointers. */
740265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
741265641Smav		cam_sglist = cmd_info->cam_sglist;
742265641Smav		*dxfer_len = 0;
743265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
744265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
745265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
746265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
747265641Smav				*dxfer_len += cam_sglist[i].ds_len;
748265641Smav			} else {
749265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
750265641Smav				cmd_info->cur_transfer_index = idx + i;
751265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
752265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
753265641Smav				*dxfer_len += cam_sglist[i].ds_len;
754265641Smav				if (ctl_sglist[i].len != 0)
755265641Smav					i++;
756265641Smav				break;
757265641Smav			}
758265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
759265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
760265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
761265641Smav				cmd_info->cur_transfer_off = 0;
762265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
763265641Smav				i++;
764265641Smav				break;
765265641Smav			}
766265641Smav			off = 0;
767265641Smav		}
768265641Smav		*sglist_cnt = i;
769265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
770265641Smav			*flags |= CAM_DATA_SG_PADDR;
771265641Smav		else
772265641Smav			*flags |= CAM_DATA_SG;
773265641Smav		*data_ptr = (uint8_t *)cam_sglist;
774265641Smav	}
775265641Smav}
776265641Smav
777265641Smavstatic void
778229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
779229997Sken{
780229997Sken	struct ctlfe_lun_softc *softc;
781288723Smav	struct ctlfe_cmd_info *cmd_info;
782229997Sken	struct ccb_hdr *ccb_h;
783275880Smav	struct ccb_accept_tio *atio;
784275880Smav	struct ccb_scsiio *csio;
785275880Smav	uint8_t *data_ptr;
786275880Smav	uint32_t dxfer_len;
787275880Smav	ccb_flags flags;
788275880Smav	union ctl_io *io;
789275880Smav	uint8_t scsi_status;
790229997Sken
791229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
792229997Sken	softc->ccbs_alloced++;
793229997Sken
794229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
795260387Sscottl	if (ccb_h == NULL) {
796229997Sken		softc->ccbs_freed++;
797229997Sken		xpt_release_ccb(start_ccb);
798275880Smav		return;
799275880Smav	}
800229997Sken
801275880Smav	/* Take the ATIO off the work queue */
802275880Smav	TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
803275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
804275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
805275880Smav	csio = &start_ccb->csio;
806229997Sken
807275880Smav	flags = atio->ccb_h.flags &
808275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
809288723Smav	cmd_info = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr;
810275881Smav	cmd_info->cur_transfer_index = 0;
811275881Smav	cmd_info->cur_transfer_off = 0;
812275881Smav	cmd_info->flags = 0;
813229997Sken
814275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
815275880Smav		/*
816275880Smav		 * Datamove call, we need to setup the S/G list.
817275880Smav		 */
818275880Smav		scsi_status = 0;
819275880Smav		csio->cdb_len = atio->cdb_len;
820275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
821275880Smav		    &csio->sglist_cnt);
822275880Smav		io->scsiio.ext_data_filled += dxfer_len;
823275880Smav		if (io->scsiio.ext_data_filled > io->scsiio.kern_total_len) {
824275880Smav			xpt_print(periph->path, "%s: tag 0x%04x "
825275880Smav				  "fill len %u > total %u\n",
826275880Smav				  __func__, io->scsiio.tag_num,
827275880Smav				  io->scsiio.ext_data_filled,
828275880Smav				  io->scsiio.kern_total_len);
829275880Smav		}
830275880Smav	} else {
831275880Smav		/*
832275880Smav		 * We're done, send status back.
833275880Smav		 */
834275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
835275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
836275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
837275880Smav
838229997Sken			/*
839275880Smav			 * If this command was aborted, we don't
840275880Smav			 * need to send status back to the SIM.
841275880Smav			 * Just free the CTIO and ctl_io, and
842275880Smav			 * recycle the ATIO back to the SIM.
843229997Sken			 */
844275880Smav			xpt_print(periph->path, "%s: aborted "
845275880Smav				  "command 0x%04x discarded\n",
846275880Smav				  __func__, io->scsiio.tag_num);
847275880Smav			/*
848275880Smav			 * For a wildcard attachment, commands can
849275880Smav			 * come in with a specific target/lun.  Reset
850275880Smav			 * the target and LUN fields back to the
851275880Smav			 * wildcard values before we send them back
852275880Smav			 * down to the SIM.  The SIM has a wildcard
853275880Smav			 * LUN enabled, not whatever target/lun
854275880Smav			 * these happened to be.
855275880Smav			 */
856275880Smav			if (softc->flags & CTLFE_LUN_WILDCARD) {
857275880Smav				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
858275880Smav				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
859275880Smav			}
860229997Sken
861275880Smav			if (atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
862275880Smav				xpt_print(periph->path, "%s: func_code "
863275880Smav					  "is %#x\n", __func__,
864275880Smav					  atio->ccb_h.func_code);
865275880Smav			}
866275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
867275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
868229997Sken
869275880Smav			/* Tell the SIM that we've aborted this ATIO */
870275880Smav			xpt_action(start_ccb);
871275880Smav			softc->ccbs_freed++;
872275880Smav			xpt_release_ccb(start_ccb);
873229997Sken
874229997Sken			/*
875275880Smav			 * Send the ATIO back down to the SIM.
876229997Sken			 */
877275880Smav			xpt_action((union ccb *)atio);
878229997Sken
879229997Sken			/*
880275880Smav			 * If we still have work to do, ask for
881275880Smav			 * another CCB.  Otherwise, deactivate our
882275880Smav			 * callout.
883229997Sken			 */
884275880Smav			if (!TAILQ_EMPTY(&softc->work_queue))
885275880Smav				xpt_schedule(periph, /*priority*/ 1);
886275880Smav			return;
887275880Smav		}
888275881Smav		data_ptr = NULL;
889275881Smav		dxfer_len = 0;
890275881Smav		csio->sglist_cnt = 0;
891275881Smav		scsi_status = 0;
892275881Smav	}
893275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
894275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
895275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
896275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
897275880Smav		flags |= CAM_SEND_STATUS;
898275880Smav		scsi_status = io->scsiio.scsi_status;
899275880Smav		csio->sense_len = io->scsiio.sense_len;
900275880Smav#ifdef CTLFEDEBUG
901275880Smav		printf("%s: tag %04x status %x\n", __func__,
902275880Smav		       atio->tag_id, io->io_hdr.status);
903275880Smav#endif
904275880Smav		if (csio->sense_len != 0) {
905275880Smav			csio->sense_data = io->scsiio.sense_data;
906275880Smav			flags |= CAM_SEND_SENSE;
907275880Smav		} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
908275880Smav			xpt_print(periph->path, "%s: check condition "
909275880Smav				  "with no sense\n", __func__);
910229997Sken		}
911275880Smav	}
912229997Sken
913229997Sken#ifdef CTLFEDEBUG
914275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
915275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
916275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
917229997Sken#endif
918229997Sken
919275880Smav	/*
920275880Smav	 * Valid combinations:
921275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
922275880Smav	 *    sglist_cnt = 0
923275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
924275880Smav	 *    sglist_cnt = 0
925275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
926275880Smav	 *    sglist_cnt != 0
927275880Smav	 */
928229997Sken#ifdef CTLFEDEBUG
929275880Smav	if (((flags & CAM_SEND_STATUS)
930275880Smav	  && (((flags & CAM_DATA_SG) != 0)
931275880Smav	   || (dxfer_len != 0)
932275880Smav	   || (csio->sglist_cnt != 0)))
933275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
934275880Smav	  && (dxfer_len == 0))
935275880Smav	 || ((flags & CAM_DATA_SG)
936275880Smav	  && (csio->sglist_cnt == 0))
937275880Smav	 || (((flags & CAM_DATA_SG) == 0)
938275880Smav	  && (csio->sglist_cnt != 0))) {
939275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
940275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
941275880Smav		       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
942275880Smav		       csio->sglist_cnt);
943275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
944275880Smav		       atio->tag_id, io->io_hdr.status);
945275880Smav	}
946229997Sken#endif
947275880Smav	cam_fill_ctio(csio,
948275880Smav		      /*retries*/ 2,
949275880Smav		      ctlfedone,
950275880Smav		      flags,
951275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
952275880Smav		      atio->tag_id,
953275880Smav		      atio->init_id,
954275880Smav		      scsi_status,
955275880Smav		      /*data_ptr*/ data_ptr,
956275880Smav		      /*dxfer_len*/ dxfer_len,
957275880Smav		      /*timeout*/ 5 * 1000);
958275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
959275880Smav	start_ccb->ccb_h.ccb_atio = atio;
960275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
961275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
962275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
963229997Sken
964275880Smav	softc->ctios_sent++;
965229997Sken
966275880Smav	cam_periph_unlock(periph);
967275880Smav	xpt_action(start_ccb);
968275880Smav	cam_periph_lock(periph);
969229997Sken
970229997Sken	/*
971275880Smav	 * If we still have work to do, ask for another CCB.
972229997Sken	 */
973275880Smav	if (!TAILQ_EMPTY(&softc->work_queue))
974229997Sken		xpt_schedule(periph, /*priority*/ 1);
975229997Sken}
976229997Sken
977229997Skenstatic void
978229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
979229997Sken{
980229997Sken	struct ctlfe_lun_softc *softc;
981288723Smav	union ctl_io *io;
982288723Smav	struct ctlfe_cmd_info *cmd_info;
983229997Sken
984229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
985288723Smav	io = ccb->ccb_h.io_ptr;
986229997Sken
987229997Sken	switch (ccb->ccb_h.func_code) {
988229997Sken	case XPT_ACCEPT_TARGET_IO:
989284793Smav		softc->atios_freed++;
990288723Smav		cmd_info = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr;
991288723Smav		free(cmd_info, M_CTLFE);
992229997Sken		break;
993229997Sken	case XPT_IMMEDIATE_NOTIFY:
994229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
995284793Smav		softc->inots_freed++;
996229997Sken		break;
997229997Sken	default:
998229997Sken		break;
999229997Sken	}
1000229997Sken
1001288723Smav	ctl_free_io(io);
1002229997Sken	free(ccb, M_CTLFE);
1003229997Sken
1004284793Smav	KASSERT(softc->atios_freed <= softc->atios_alloced, ("%s: "
1005284793Smav		"atios_freed %ju > atios_alloced %ju", __func__,
1006284793Smav		softc->atios_freed, softc->atios_alloced));
1007284793Smav	KASSERT(softc->inots_freed <= softc->inots_alloced, ("%s: "
1008284793Smav		"inots_freed %ju > inots_alloced %ju", __func__,
1009284793Smav		softc->inots_freed, softc->inots_alloced));
1010229997Sken
1011229997Sken	/*
1012229997Sken	 * If we have received all of our CCBs, we can release our
1013229997Sken	 * reference on the peripheral driver.  It will probably go away
1014229997Sken	 * now.
1015229997Sken	 */
1016284793Smav	if ((softc->atios_freed == softc->atios_alloced)
1017284793Smav	 && (softc->inots_freed == softc->inots_alloced)) {
1018229997Sken		cam_periph_release_locked(periph);
1019229997Sken	}
1020229997Sken}
1021229997Sken
1022238870Smjacobstatic int
1023238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1024238870Smjacob{
1025238870Smjacob	uint64_t lba;
1026238870Smjacob	uint32_t num_blocks, nbc;
1027238870Smjacob	uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1028238870Smjacob	    atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1029238870Smjacob
1030238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1031238870Smjacob
1032238870Smjacob	switch (cmdbyt[0]) {
1033238870Smjacob	case READ_6:
1034238870Smjacob	case WRITE_6:
1035238870Smjacob	{
1036238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1037238870Smjacob		lba = scsi_3btoul(cdb->addr);
1038238870Smjacob		lba &= 0x1fffff;
1039238870Smjacob		num_blocks = cdb->length;
1040238870Smjacob		if (num_blocks == 0)
1041238870Smjacob			num_blocks = 256;
1042238870Smjacob		lba += nbc;
1043238870Smjacob		num_blocks -= nbc;
1044238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1045238870Smjacob		cdb->length = num_blocks;
1046238870Smjacob		break;
1047238870Smjacob	}
1048238870Smjacob	case READ_10:
1049238870Smjacob	case WRITE_10:
1050238870Smjacob	{
1051238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1052238870Smjacob		lba = scsi_4btoul(cdb->addr);
1053238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1054238870Smjacob		lba += nbc;
1055238870Smjacob		num_blocks -= nbc;
1056238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1057238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1058238870Smjacob		break;
1059238870Smjacob	}
1060238870Smjacob	case READ_12:
1061238870Smjacob	case WRITE_12:
1062238870Smjacob	{
1063238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1064238870Smjacob		lba = scsi_4btoul(cdb->addr);
1065238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1066238870Smjacob		lba += nbc;
1067238870Smjacob		num_blocks -= nbc;
1068238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1069238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1070238870Smjacob		break;
1071238870Smjacob	}
1072238870Smjacob	case READ_16:
1073238870Smjacob	case WRITE_16:
1074238870Smjacob	{
1075238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1076238870Smjacob		lba = scsi_8btou64(cdb->addr);
1077238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1078238870Smjacob		lba += nbc;
1079238870Smjacob		num_blocks -= nbc;
1080238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1081238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1082238870Smjacob		break;
1083238870Smjacob	}
1084238870Smjacob	default:
1085238870Smjacob		return -1;
1086238870Smjacob	}
1087238870Smjacob	return (0);
1088238870Smjacob}
1089238870Smjacob
1090229997Skenstatic void
1091229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1092229997Sken{
1093229997Sken	struct ctlfe_lun_softc *softc;
1094229997Sken	struct ctlfe_softc *bus_softc;
1095288723Smav	struct ctlfe_cmd_info *cmd_info;
1096238870Smjacob	struct ccb_accept_tio *atio = NULL;
1097238870Smjacob	union ctl_io *io = NULL;
1098260387Sscottl	struct mtx *mtx;
1099229997Sken
1100260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1101260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1102229997Sken#ifdef CTLFE_DEBUG
1103275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1104275878Smav	       done_ccb->ccb_h.func_code);
1105229997Sken#endif
1106229997Sken
1107284794Smav	/*
1108284794Smav	 * At this point CTL has no known use case for device queue freezes.
1109284794Smav	 * In case some SIM think different -- drop its freeze right here.
1110284794Smav	 */
1111284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1112284794Smav		cam_release_devq(periph->path,
1113284794Smav				 /*relsim_flags*/0,
1114284794Smav				 /*reduction*/0,
1115284794Smav				 /*timeout*/0,
1116284794Smav				 /*getcount_only*/0);
1117284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1118284794Smav	}
1119284794Smav
1120229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1121229997Sken	bus_softc = softc->parent_softc;
1122260387Sscottl	mtx = cam_periph_mtx(periph);
1123260387Sscottl	mtx_lock(mtx);
1124229997Sken
1125229997Sken	/*
1126229997Sken	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1127229997Sken	 * need to be freed.  Most of the ATIOs and INOTs that come back
1128229997Sken	 * will be CCBs that are being returned from the SIM as a result of
1129229997Sken	 * our disabling the LUN.
1130229997Sken	 *
1131229997Sken	 * Other CCB types are handled in their respective cases below.
1132229997Sken	 */
1133229997Sken	if (periph->flags & CAM_PERIPH_INVALID) {
1134229997Sken		switch (done_ccb->ccb_h.func_code) {
1135229997Sken		case XPT_ACCEPT_TARGET_IO:
1136229997Sken		case XPT_IMMEDIATE_NOTIFY:
1137229997Sken		case XPT_NOTIFY_ACKNOWLEDGE:
1138229997Sken			ctlfe_free_ccb(periph, done_ccb);
1139260387Sscottl			goto out;
1140229997Sken		default:
1141229997Sken			break;
1142229997Sken		}
1143229997Sken
1144229997Sken	}
1145229997Sken	switch (done_ccb->ccb_h.func_code) {
1146229997Sken	case XPT_ACCEPT_TARGET_IO: {
1147229997Sken
1148229997Sken		atio = &done_ccb->atio;
1149229997Sken
1150238870Smjacob resubmit:
1151229997Sken		/*
1152229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1153229997Sken		 * datamove or done.
1154229997Sken		 */
1155260387Sscottl		mtx_unlock(mtx);
1156275878Smav		io = done_ccb->ccb_h.io_ptr;
1157288723Smav		cmd_info = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr;
1158229997Sken		ctl_zero_io(io);
1159229997Sken
1160229997Sken		/* Save pointers on both sides */
1161229997Sken		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1162288723Smav		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr = cmd_info;
1163229997Sken		done_ccb->ccb_h.io_ptr = io;
1164229997Sken
1165229997Sken		/*
1166229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1167229997Sken		 * down the immediate notify path below.
1168229997Sken		 */
1169229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1170288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1171268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1172290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1173290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1174290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1175290776Smav		} else {
1176290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1177290776Smav		}
1178229997Sken		io->scsiio.tag_num = atio->tag_id;
1179229997Sken		switch (atio->tag_action) {
1180229997Sken		case CAM_TAG_ACTION_NONE:
1181229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1182229997Sken			break;
1183229997Sken		case MSG_SIMPLE_TASK:
1184229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1185229997Sken			break;
1186229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1187229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1188229997Sken			break;
1189229997Sken		case MSG_ORDERED_TASK:
1190229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1191229997Sken			break;
1192229997Sken		case MSG_ACA_TASK:
1193229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1194229997Sken			break;
1195229997Sken		default:
1196229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1197229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1198229997Sken			       atio->tag_action);
1199229997Sken			break;
1200229997Sken		}
1201229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1202229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1203229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1204229997Sken		}
1205229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1206229997Sken		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1207229997Sken		      io->scsiio.cdb_len);
1208229997Sken
1209229997Sken#ifdef CTLFEDEBUG
1210288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1211288731Smav		        io->io_hdr.nexus.initid,
1212229997Sken		        io->io_hdr.nexus.targ_port,
1213229997Sken		        io->io_hdr.nexus.targ_lun,
1214229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1215229997Sken#endif
1216229997Sken
1217229997Sken		ctl_queue(io);
1218260387Sscottl		return;
1219229997Sken	}
1220229997Sken	case XPT_CONT_TARGET_IO: {
1221238870Smjacob		int srr = 0;
1222238870Smjacob		uint32_t srr_off = 0;
1223229997Sken
1224229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1225229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1226229997Sken
1227229997Sken		softc->ctios_returned++;
1228229997Sken#ifdef CTLFEDEBUG
1229229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1230229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1231229997Sken#endif
1232229997Sken		/*
1233238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1234238870Smjacob		 */
1235238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1236238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1237238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1238238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1239238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1240238870Smjacob			srr = 1;
1241238870Smjacob			srr_off =
1242238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1243238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1244238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1245238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1246238870Smjacob		}
1247238870Smjacob
1248275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1249238870Smjacob			/*
1250238870Smjacob			 * If status was being sent, the back end data is now
1251238870Smjacob			 * history. Hack it up and resubmit a new command with
1252238870Smjacob			 * the CDB adjusted. If the SIM does the right thing,
1253238870Smjacob			 * all of the resid math should work.
1254238870Smjacob			 */
1255238870Smjacob			softc->ccbs_freed++;
1256238870Smjacob			xpt_release_ccb(done_ccb);
1257238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1258238870Smjacob				done_ccb = (union ccb *)atio;
1259238870Smjacob				goto resubmit;
1260238870Smjacob			}
1261238870Smjacob			/*
1262238870Smjacob			 * Fall through to doom....
1263238870Smjacob			 */
1264238870Smjacob		} else if (srr) {
1265238870Smjacob			/*
1266238870Smjacob			 * If we have an srr and we're still sending data, we
1267238870Smjacob			 * should be able to adjust offsets and cycle again.
1268238870Smjacob			 */
1269238870Smjacob			io->scsiio.kern_rel_offset =
1270238870Smjacob			    io->scsiio.ext_data_filled = srr_off;
1271238870Smjacob			io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1272238870Smjacob			    io->scsiio.kern_rel_offset;
1273238870Smjacob			softc->ccbs_freed++;
1274238870Smjacob			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1275238870Smjacob			xpt_release_ccb(done_ccb);
1276238870Smjacob			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1277238870Smjacob					  periph_links.tqe);
1278238870Smjacob			xpt_schedule(periph, /*priority*/ 1);
1279260387Sscottl			break;
1280238870Smjacob		}
1281238870Smjacob
1282277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1283277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1284277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1285277919Smav
1286238870Smjacob		/*
1287229997Sken		 * If we were sending status back to the initiator, free up
1288229997Sken		 * resources.  If we were doing a datamove, call the
1289229997Sken		 * datamove done routine.
1290229997Sken		 */
1291275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1292229997Sken			softc->ccbs_freed++;
1293229997Sken			xpt_release_ccb(done_ccb);
1294229997Sken			/*
1295229997Sken			 * For a wildcard attachment, commands can come in
1296229997Sken			 * with a specific target/lun.  Reset the target
1297229997Sken			 * and LUN fields back to the wildcard values before
1298229997Sken			 * we send them back down to the SIM.  The SIM has
1299229997Sken			 * a wildcard LUN enabled, not whatever target/lun
1300229997Sken			 * these happened to be.
1301229997Sken			 */
1302229997Sken			if (softc->flags & CTLFE_LUN_WILDCARD) {
1303229997Sken				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1304229997Sken				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1305229997Sken			}
1306229997Sken			if (periph->flags & CAM_PERIPH_INVALID) {
1307229997Sken				ctlfe_free_ccb(periph, (union ccb *)atio);
1308229997Sken			} else {
1309260387Sscottl				mtx_unlock(mtx);
1310229997Sken				xpt_action((union ccb *)atio);
1311260387Sscottl				return;
1312229997Sken			}
1313229997Sken		} else {
1314288723Smav			struct ctlfe_cmd_info *cmd_info;
1315229997Sken			struct ccb_scsiio *csio;
1316229997Sken
1317229997Sken			csio = &done_ccb->csio;
1318288723Smav			cmd_info = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr;
1319229997Sken
1320229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1321229997Sken
1322229997Sken			io->scsiio.ext_data_len += csio->dxfer_len;
1323229997Sken			if (io->scsiio.ext_data_len >
1324229997Sken			    io->scsiio.kern_total_len) {
1325229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
1326229997Sken					  "done len %u > total %u sent %u\n",
1327229997Sken					  __func__, io->scsiio.tag_num,
1328229997Sken					  io->scsiio.ext_data_len,
1329229997Sken					  io->scsiio.kern_total_len,
1330229997Sken					  io->scsiio.ext_data_filled);
1331229997Sken			}
1332229997Sken			/*
1333229997Sken			 * Translate CAM status to CTL status.  Success
1334229997Sken			 * does not change the overall, ctl_io status.  In
1335229997Sken			 * that case we just set port_status to 0.  If we
1336229997Sken			 * have a failure, though, set a data phase error
1337229997Sken			 * for the overall ctl_io.
1338229997Sken			 */
1339229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1340229997Sken			case CAM_REQ_CMP:
1341229997Sken				io->io_hdr.port_status = 0;
1342229997Sken				break;
1343229997Sken			default:
1344229997Sken				/*
1345245228Sken				 * XXX KDM we probably need to figure out a
1346245228Sken				 * standard set of errors that the SIM
1347245228Sken				 * drivers should return in the event of a
1348245228Sken				 * data transfer failure.  A data phase
1349245228Sken				 * error will at least point the user to a
1350245228Sken				 * data transfer error of some sort.
1351245228Sken				 * Hopefully the SIM printed out some
1352245228Sken				 * additional information to give the user
1353245228Sken				 * a clue what happened.
1354229997Sken				 */
1355229997Sken				io->io_hdr.port_status = 0xbad1;
1356229997Sken				ctl_set_data_phase_error(&io->scsiio);
1357229997Sken				/*
1358229997Sken				 * XXX KDM figure out residual.
1359229997Sken				 */
1360229997Sken				break;
1361229997Sken			}
1362229997Sken			/*
1363229997Sken			 * If we had to break this S/G list into multiple
1364229997Sken			 * pieces, figure out where we are in the list, and
1365229997Sken			 * continue sending pieces if necessary.
1366229997Sken			 */
1367229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1368265641Smav			 && (io->io_hdr.port_status == 0)) {
1369229997Sken				ccb_flags flags;
1370229997Sken				uint8_t scsi_status;
1371229997Sken				uint8_t *data_ptr;
1372229997Sken				uint32_t dxfer_len;
1373229997Sken
1374229997Sken				flags = atio->ccb_h.flags &
1375229997Sken					(CAM_DIS_DISCONNECT|
1376265641Smav					 CAM_TAG_ACTION_VALID);
1377229997Sken
1378265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1379265641Smav				    &dxfer_len, &csio->sglist_cnt);
1380229997Sken
1381229997Sken				scsi_status = 0;
1382229997Sken
1383229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1384229997Sken				 && (dxfer_len == 0)) {
1385229997Sken					printf("%s: tag %04x no status or "
1386229997Sken					       "len cdb = %02x\n", __func__,
1387229997Sken					       atio->tag_id,
1388229997Sken					atio->cdb_io.cdb_bytes[0]);
1389229997Sken					printf("%s: tag %04x io status %#x\n",
1390229997Sken					       __func__, atio->tag_id,
1391229997Sken					       io->io_hdr.status);
1392229997Sken				}
1393229997Sken
1394229997Sken				cam_fill_ctio(csio,
1395229997Sken					      /*retries*/ 2,
1396229997Sken					      ctlfedone,
1397229997Sken					      flags,
1398229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1399229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1400229997Sken					      atio->tag_id,
1401229997Sken					      atio->init_id,
1402229997Sken					      scsi_status,
1403229997Sken					      /*data_ptr*/ data_ptr,
1404229997Sken					      /*dxfer_len*/ dxfer_len,
1405229997Sken					      /*timeout*/ 5 * 1000);
1406229997Sken
1407260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1408229997Sken				csio->resid = 0;
1409229997Sken				csio->ccb_h.ccb_atio = atio;
1410229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1411229997Sken				softc->ctios_sent++;
1412260387Sscottl				mtx_unlock(mtx);
1413229997Sken				xpt_action((union ccb *)csio);
1414229997Sken			} else {
1415229997Sken				/*
1416229997Sken				 * Release the CTIO.  The ATIO will be sent back
1417229997Sken				 * down to the SIM once we send status.
1418229997Sken				 */
1419229997Sken				softc->ccbs_freed++;
1420229997Sken				xpt_release_ccb(done_ccb);
1421260387Sscottl				mtx_unlock(mtx);
1422229997Sken
1423229997Sken				/* Call the backend move done callback */
1424229997Sken				io->scsiio.be_move_done(io);
1425229997Sken			}
1426260387Sscottl			return;
1427229997Sken		}
1428229997Sken		break;
1429229997Sken	}
1430229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1431229997Sken		union ctl_io *io;
1432229997Sken		struct ccb_immediate_notify *inot;
1433229997Sken		cam_status status;
1434284794Smav		int send_ctl_io;
1435229997Sken
1436229997Sken		inot = &done_ccb->cin1;
1437229997Sken		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1438229997Sken		       "seq %#x\n", __func__, inot->ccb_h.status,
1439229997Sken		       inot->tag_id, inot->seq_id);
1440229997Sken
1441275878Smav		io = done_ccb->ccb_h.io_ptr;
1442275878Smav		ctl_zero_io(io);
1443229997Sken
1444275878Smav		send_ctl_io = 1;
1445229997Sken
1446275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1447275878Smav		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1448275878Smav		inot->ccb_h.io_ptr = io;
1449288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1450275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1451290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1452290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1453290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1454290776Smav		} else {
1455290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1456290776Smav		}
1457275878Smav		/* XXX KDM should this be the tag_id? */
1458275878Smav		io->taskio.tag_num = inot->seq_id;
1459229997Sken
1460275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1461275878Smav		switch (status) {
1462275878Smav		case CAM_SCSI_BUS_RESET:
1463275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1464275878Smav			break;
1465275878Smav		case CAM_BDR_SENT:
1466275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1467275878Smav			break;
1468275878Smav		case CAM_MESSAGE_RECV:
1469275878Smav			switch (inot->arg) {
1470275878Smav			case MSG_ABORT_TASK_SET:
1471275878Smav				io->taskio.task_action =
1472275878Smav				    CTL_TASK_ABORT_TASK_SET;
1473229997Sken				break;
1474275878Smav			case MSG_TARGET_RESET:
1475290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1476229997Sken				break;
1477275878Smav			case MSG_ABORT_TASK:
1478290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1479229997Sken				break;
1480275878Smav			case MSG_LOGICAL_UNIT_RESET:
1481290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1482275878Smav				break;
1483275878Smav			case MSG_CLEAR_TASK_SET:
1484275878Smav				io->taskio.task_action =
1485290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1486275878Smav				break;
1487275878Smav			case MSG_CLEAR_ACA:
1488290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1489290775Smav				break;
1490290775Smav			case MSG_QUERY_TASK:
1491290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1492290775Smav				break;
1493290775Smav			case MSG_QUERY_TASK_SET:
1494275878Smav				io->taskio.task_action =
1495290775Smav				    CTL_TASK_QUERY_TASK_SET;
1496275878Smav				break;
1497290775Smav			case MSG_QUERY_ASYNC_EVENT:
1498290775Smav				io->taskio.task_action =
1499290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1500290775Smav				break;
1501275878Smav			case MSG_NOOP:
1502229997Sken				send_ctl_io = 0;
1503229997Sken				break;
1504229997Sken			default:
1505275878Smav				xpt_print(periph->path,
1506275878Smav					  "%s: unsupported message 0x%x\n",
1507275878Smav					  __func__, inot->arg);
1508275878Smav				send_ctl_io = 0;
1509275878Smav				break;
1510275878Smav			}
1511275878Smav			break;
1512275878Smav		case CAM_REQ_ABORTED:
1513275878Smav			/*
1514275878Smav			 * This request was sent back by the driver.
1515275878Smav			 * XXX KDM what do we do here?
1516275878Smav			 */
1517275878Smav			send_ctl_io = 0;
1518275878Smav			break;
1519275878Smav		case CAM_REQ_INVALID:
1520275878Smav		case CAM_PROVIDE_FAIL:
1521275878Smav		default:
1522275878Smav			/*
1523275878Smav			 * We should only get here if we're talking
1524275878Smav			 * to a talking to a SIM that is target
1525275878Smav			 * capable but supports the old API.  In
1526275878Smav			 * that case, we need to just free the CCB.
1527275878Smav			 * If we actually send a notify acknowledge,
1528275878Smav			 * it will send that back with an error as
1529275878Smav			 * well.
1530275878Smav			 */
1531237601Sken
1532275878Smav			if ((status != CAM_REQ_INVALID)
1533275878Smav			 && (status != CAM_PROVIDE_FAIL))
1534275878Smav				xpt_print(periph->path,
1535275878Smav					  "%s: unsupported CAM status 0x%x\n",
1536275878Smav					  __func__, status);
1537237601Sken
1538275878Smav			ctlfe_free_ccb(periph, done_ccb);
1539237601Sken
1540275878Smav			goto out;
1541275878Smav		}
1542275878Smav		if (send_ctl_io != 0) {
1543275878Smav			ctl_queue(io);
1544229997Sken		} else {
1545229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1546229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1547229997Sken			xpt_action(done_ccb);
1548229997Sken		}
1549229997Sken		break;
1550229997Sken	}
1551229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1552229997Sken		/*
1553229997Sken		 * Queue this back down to the SIM as an immediate notify.
1554229997Sken		 */
1555304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1556229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1557229997Sken		xpt_action(done_ccb);
1558229997Sken		break;
1559229997Sken	case XPT_SET_SIM_KNOB:
1560229997Sken	case XPT_GET_SIM_KNOB:
1561229997Sken		break;
1562229997Sken	default:
1563229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1564229997Sken		      done_ccb->ccb_h.func_code);
1565229997Sken		break;
1566229997Sken	}
1567260387Sscottl
1568260387Sscottlout:
1569260387Sscottl	mtx_unlock(mtx);
1570229997Sken}
1571229997Sken
1572229997Skenstatic void
1573229997Skenctlfe_onoffline(void *arg, int online)
1574229997Sken{
1575229997Sken	struct ctlfe_softc *bus_softc;
1576229997Sken	union ccb *ccb;
1577229997Sken	cam_status status;
1578229997Sken	struct cam_path *path;
1579229997Sken	int set_wwnn;
1580229997Sken
1581229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1582229997Sken
1583229997Sken	set_wwnn = 0;
1584229997Sken
1585229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1586229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1587229997Sken	if (status != CAM_REQ_CMP) {
1588229997Sken		printf("%s: unable to create path!\n", __func__);
1589229997Sken		return;
1590229997Sken	}
1591275882Smav	ccb = xpt_alloc_ccb();
1592242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1593288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1594288713Smav	xpt_action(ccb);
1595229997Sken
1596229997Sken	/*
1597229997Sken	 * Copan WWN format:
1598229997Sken	 *
1599229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1600229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1601229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1602229997Sken	 * Bits 11-8:			Type of port:
1603229997Sken	 *					1 == N-Port
1604229997Sken	 *					2 == F-Port
1605229997Sken	 *					3 == NL-Port
1606229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1607229997Sken	 */
1608229997Sken	if (online != 0) {
1609229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1610229997Sken#ifdef RANDOM_WWNN
1611229997Sken			uint64_t random_bits;
1612229997Sken#endif
1613229997Sken
1614229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1615229997Sken			       bus_softc->port_name,
1616229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1617229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1618229997Sken			       bus_softc->port_name,
1619229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1620229997Sken
1621229997Sken#ifdef RANDOM_WWNN
1622229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1623229997Sken#endif
1624229997Sken
1625229997Sken			/*
1626229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1627229997Sken			 * take the current WWNN/WWPN from the card, and
1628229997Sken			 * replace the company identifier and the NL-Port
1629229997Sken			 * indicator and the port number (for the WWPN).
1630229997Sken			 * This should be replaced later with ddb_GetWWNN,
1631229997Sken			 * or possibly a more centralized scheme.  (It
1632229997Sken			 * would be nice to have the WWNN/WWPN for each
1633268677Smav			 * port stored in the ctl_port structure.)
1634229997Sken			 */
1635229997Sken#ifdef RANDOM_WWNN
1636229997Sken			ccb->knob.xport_specific.fc.wwnn =
1637229997Sken				(random_bits &
1638229997Sken				0x0000000fffffff00ULL) |
1639229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1640229997Sken				/* NL-Port */    0x0300;
1641229997Sken			ccb->knob.xport_specific.fc.wwpn =
1642229997Sken				(random_bits &
1643229997Sken				0x0000000fffffff00ULL) |
1644229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1645229997Sken				/* NL-Port */    0x3000 |
1646268677Smav				/* Port Num */ (bus_softc->port.targ_port & 0xff);
1647229997Sken
1648229997Sken			/*
1649229997Sken			 * This is a bit of an API break/reversal, but if
1650229997Sken			 * we're doing the random WWNN that's a little
1651229997Sken			 * different anyway.  So record what we're actually
1652229997Sken			 * using with the frontend code so it's reported
1653229997Sken			 * accurately.
1654229997Sken			 */
1655268683Smav			ctl_port_set_wwns(&bus_softc->port,
1656268683Smav			    true, ccb->knob.xport_specific.fc.wwnn,
1657268683Smav			    true, ccb->knob.xport_specific.fc.wwpn);
1658229997Sken			set_wwnn = 1;
1659229997Sken#else /* RANDOM_WWNN */
1660229997Sken			/*
1661229997Sken			 * If the user has specified a WWNN/WWPN, send them
1662229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1663229997Sken			 * has reported.
1664229997Sken			 */
1665284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1666284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1667229997Sken				ccb->knob.xport_specific.fc.wwnn =
1668284586Smav				    bus_softc->port.wwnn;
1669284586Smav				set_wwnn = 1;
1670284586Smav			} else {
1671284586Smav				ctl_port_set_wwns(&bus_softc->port,
1672284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1673284586Smav				    false, 0);
1674284586Smav			}
1675284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1676284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1677229997Sken				ccb->knob.xport_specific.fc.wwpn =
1678284586Smav				    bus_softc->port.wwpn;
1679229997Sken				set_wwnn = 1;
1680229997Sken			} else {
1681268683Smav				ctl_port_set_wwns(&bus_softc->port,
1682284586Smav				    false, 0,
1683268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1684229997Sken			}
1685229997Sken#endif /* RANDOM_WWNN */
1686229997Sken
1687229997Sken
1688229997Sken			if (set_wwnn != 0) {
1689229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1690229997Sken				       bus_softc->port_name,
1691229997Sken				ccb->knob.xport_specific.fc.wwnn);
1692229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1693229997Sken				       bus_softc->port_name,
1694229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1695229997Sken			}
1696229997Sken		} else {
1697229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1698229997Sken			       bus_softc->port_name);
1699229997Sken		}
1700229997Sken	}
1701229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1702229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1703229997Sken	if (set_wwnn != 0)
1704229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1705229997Sken
1706229997Sken	if (online != 0)
1707288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1708229997Sken	else
1709288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1710229997Sken
1711229997Sken	xpt_action(ccb);
1712229997Sken
1713229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1714229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1715229997Sken		       "status %#x\n",
1716229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1717229997Sken		       (online != 0) ? "enable" : "disable",
1718229997Sken		       ccb->ccb_h.status);
1719229997Sken	} else {
1720229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1721229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1722229997Sken		       (online != 0) ? "enable" : "disable");
1723229997Sken	}
1724229997Sken
1725229997Sken	xpt_free_path(path);
1726275882Smav	xpt_free_ccb(ccb);
1727229997Sken}
1728229997Sken
1729229997Skenstatic void
1730229997Skenctlfe_online(void *arg)
1731229997Sken{
1732245228Sken	struct ctlfe_softc *bus_softc;
1733245228Sken	struct cam_path *path;
1734245228Sken	cam_status status;
1735245228Sken	struct ctlfe_lun_softc *lun_softc;
1736274388Smav	struct cam_periph *periph;
1737245228Sken
1738245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1739245228Sken
1740245228Sken	/*
1741245228Sken	 * Create the wildcard LUN before bringing the port online.
1742245228Sken	 */
1743245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1744245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1745245228Sken				 CAM_LUN_WILDCARD);
1746245228Sken	if (status != CAM_REQ_CMP) {
1747245228Sken		printf("%s: unable to create path for wildcard periph\n",
1748245228Sken				__func__);
1749245228Sken		return;
1750245228Sken	}
1751245228Sken
1752275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1753245228Sken
1754260387Sscottl	xpt_path_lock(path);
1755274388Smav	periph = cam_periph_find(path, "ctl");
1756274388Smav	if (periph != NULL) {
1757274388Smav		/* We've already got a periph, no need to alloc a new one. */
1758274388Smav		xpt_path_unlock(path);
1759274388Smav		xpt_free_path(path);
1760274388Smav		free(lun_softc, M_CTLFE);
1761274388Smav		return;
1762274388Smav	}
1763245228Sken	lun_softc->parent_softc = bus_softc;
1764245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1765245228Sken
1766245228Sken	status = cam_periph_alloc(ctlferegister,
1767245228Sken				  ctlfeoninvalidate,
1768245228Sken				  ctlfecleanup,
1769245228Sken				  ctlfestart,
1770245228Sken				  "ctl",
1771245228Sken				  CAM_PERIPH_BIO,
1772245228Sken				  path,
1773245228Sken				  ctlfeasync,
1774245228Sken				  0,
1775245228Sken				  lun_softc);
1776245228Sken
1777245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1778245228Sken		const struct cam_status_entry *entry;
1779245228Sken
1780245228Sken		entry = cam_fetch_status_entry(status);
1781245228Sken		printf("%s: CAM error %s (%#x) returned from "
1782245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1783245228Sken		       entry->status_text : "Unknown", status);
1784274388Smav		free(lun_softc, M_CTLFE);
1785245228Sken	}
1786245228Sken
1787260387Sscottl	xpt_path_unlock(path);
1788275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1789260387Sscottl	xpt_free_path(path);
1790229997Sken}
1791229997Sken
1792229997Skenstatic void
1793229997Skenctlfe_offline(void *arg)
1794229997Sken{
1795245228Sken	struct ctlfe_softc *bus_softc;
1796245228Sken	struct cam_path *path;
1797245228Sken	cam_status status;
1798245228Sken	struct cam_periph *periph;
1799245228Sken
1800245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1801245228Sken
1802275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1803275882Smav
1804245228Sken	/*
1805245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1806245228Sken	 * the port offline.
1807245228Sken	 */
1808245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1809245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1810245228Sken				 CAM_LUN_WILDCARD);
1811245228Sken	if (status != CAM_REQ_CMP) {
1812245228Sken		printf("%s: unable to create path for wildcard periph\n",
1813245228Sken		       __func__);
1814245228Sken		return;
1815245228Sken	}
1816260387Sscottl	xpt_path_lock(path);
1817245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1818245228Sken		cam_periph_invalidate(periph);
1819260387Sscottl	xpt_path_unlock(path);
1820245228Sken	xpt_free_path(path);
1821229997Sken}
1822229997Sken
1823229997Sken/*
1824229997Sken * This will get called to enable a LUN on every bus that is attached to
1825229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1826229997Sken */
1827229997Skenstatic int
1828284798Smavctlfe_lun_enable(void *arg, int lun_id)
1829229997Sken{
1830229997Sken	struct ctlfe_softc *bus_softc;
1831229997Sken	struct ctlfe_lun_softc *softc;
1832229997Sken	struct cam_path *path;
1833229997Sken	struct cam_periph *periph;
1834229997Sken	cam_status status;
1835229997Sken
1836229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1837290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1838290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1839229997Sken
1840260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1841290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1842229997Sken	/* XXX KDM need some way to return status to CTL here? */
1843229997Sken	if (status != CAM_REQ_CMP) {
1844229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1845229997Sken		       status);
1846229997Sken		return (1);
1847229997Sken	}
1848229997Sken
1849229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1850260387Sscottl	xpt_path_lock(path);
1851229997Sken	periph = cam_periph_find(path, "ctl");
1852229997Sken	if (periph != NULL) {
1853229997Sken		/* We've already got a periph, no need to alloc a new one. */
1854260387Sscottl		xpt_path_unlock(path);
1855229997Sken		xpt_free_path(path);
1856229997Sken		free(softc, M_CTLFE);
1857229997Sken		return (0);
1858229997Sken	}
1859229997Sken	softc->parent_softc = bus_softc;
1860229997Sken
1861229997Sken	status = cam_periph_alloc(ctlferegister,
1862229997Sken				  ctlfeoninvalidate,
1863229997Sken				  ctlfecleanup,
1864229997Sken				  ctlfestart,
1865229997Sken				  "ctl",
1866229997Sken				  CAM_PERIPH_BIO,
1867229997Sken				  path,
1868229997Sken				  ctlfeasync,
1869229997Sken				  0,
1870229997Sken				  softc);
1871229997Sken
1872274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1873274388Smav		const struct cam_status_entry *entry;
1874274388Smav
1875274388Smav		entry = cam_fetch_status_entry(status);
1876274388Smav		printf("%s: CAM error %s (%#x) returned from "
1877274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1878274388Smav		       entry->status_text : "Unknown", status);
1879274388Smav		free(softc, M_CTLFE);
1880274388Smav	}
1881274388Smav
1882260387Sscottl	xpt_path_unlock(path);
1883244016Sken	xpt_free_path(path);
1884229997Sken	return (0);
1885229997Sken}
1886229997Sken
1887229997Sken/*
1888245228Sken * This will get called when the user removes a LUN to disable that LUN
1889245228Sken * on every bus that is attached to CTL.
1890229997Sken */
1891229997Skenstatic int
1892284798Smavctlfe_lun_disable(void *arg, int lun_id)
1893229997Sken{
1894229997Sken	struct ctlfe_softc *softc;
1895229997Sken	struct ctlfe_lun_softc *lun_softc;
1896229997Sken
1897229997Sken	softc = (struct ctlfe_softc *)arg;
1898290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1899290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1900229997Sken
1901260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1902229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1903229997Sken		struct cam_path *path;
1904229997Sken
1905229997Sken		path = lun_softc->periph->path;
1906229997Sken
1907290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1908229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1909229997Sken			break;
1910229997Sken		}
1911229997Sken	}
1912229997Sken	if (lun_softc == NULL) {
1913260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1914284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1915229997Sken		return (1);
1916229997Sken	}
1917260387Sscottl	cam_periph_acquire(lun_softc->periph);
1918260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1919229997Sken
1920260387Sscottl	cam_periph_lock(lun_softc->periph);
1921229997Sken	cam_periph_invalidate(lun_softc->periph);
1922260387Sscottl	cam_periph_unlock(lun_softc->periph);
1923260387Sscottl	cam_periph_release(lun_softc->periph);
1924229997Sken	return (0);
1925229997Sken}
1926229997Sken
1927229997Skenstatic void
1928229997Skenctlfe_dump_sim(struct cam_sim *sim)
1929229997Sken{
1930229997Sken
1931229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1932229997Sken	       sim->sim_name, sim->unit_number,
1933229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1934229997Sken}
1935229997Sken
1936229997Sken/*
1937229997Sken * Assumes that the SIM lock is held.
1938229997Sken */
1939229997Skenstatic void
1940229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1941229997Sken{
1942229997Sken	struct ccb_hdr *hdr;
1943229997Sken	struct cam_periph *periph;
1944229997Sken	int num_items;
1945229997Sken
1946229997Sken	periph = softc->periph;
1947229997Sken	num_items = 0;
1948229997Sken
1949229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1950275880Smav		union ctl_io *io = hdr->io_ptr;
1951229997Sken
1952229997Sken		num_items++;
1953229997Sken
1954229997Sken		/*
1955229997Sken		 * Only regular SCSI I/O is put on the work
1956229997Sken		 * queue, so we can print sense here.  There may be no
1957229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1958229997Sken		 * print out the CCB as well.
1959229997Sken		 *
1960229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1961229997Sken		 * CTL is merged in with CAM.
1962229997Sken		 */
1963229997Sken		ctl_io_error_print(io, NULL);
1964229997Sken
1965229997Sken		/*
1966275880Smav		 * Print DMA status if we are DMA_QUEUED.
1967229997Sken		 */
1968275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1969275880Smav			xpt_print(periph->path,
1970275880Smav			    "Total %u, Current %u, Resid %u\n",
1971275880Smav			    io->scsiio.kern_total_len,
1972275880Smav			    io->scsiio.kern_data_len,
1973275880Smav			    io->scsiio.kern_data_resid);
1974275880Smav		}
1975229997Sken	}
1976229997Sken
1977229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1978229997Sken		  num_items);
1979250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
1980229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1981229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1982229997Sken		  (uintmax_t)softc->ccbs_freed);
1983229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1984229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
1985229997Sken		  softc->ctios_returned), softc->ctios_sent,
1986229997Sken		  softc->ctios_returned);
1987229997Sken}
1988229997Sken
1989229997Sken/*
1990275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1991275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1992275880Smav * to the adapter.
1993229997Sken */
1994229997Skenstatic void
1995275880Smavctlfe_datamove(union ctl_io *io)
1996229997Sken{
1997275880Smav	union ccb *ccb;
1998275880Smav	struct cam_periph *periph;
1999229997Sken	struct ctlfe_lun_softc *softc;
2000229997Sken
2001275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
2002275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
2003229997Sken
2004275880Smav	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2005275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
2006275880Smav	cam_periph_lock(periph);
2007275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
2008275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2009275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2010275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2011275880Smav	TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2012275880Smav			  periph_links.tqe);
2013229997Sken	xpt_schedule(periph, /*priority*/ 1);
2014275880Smav	cam_periph_unlock(periph);
2015229997Sken}
2016229997Sken
2017229997Skenstatic void
2018275880Smavctlfe_done(union ctl_io *io)
2019229997Sken{
2020229997Sken	union ccb *ccb;
2021229997Sken	struct cam_periph *periph;
2022229997Sken	struct ctlfe_lun_softc *softc;
2023229997Sken
2024229997Sken	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2025229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
2026260387Sscottl	cam_periph_lock(periph);
2027229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
2028229997Sken
2029229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
2030229997Sken		/*
2031229997Sken		 * Task management commands don't require any further
2032229997Sken		 * communication back to the adapter.  Requeue the CCB
2033229997Sken		 * to the adapter, and free the CTL I/O.
2034229997Sken		 */
2035229997Sken		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2036229997Sken			  "tag %#x seq %#x\n", __func__,
2037229997Sken			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2038229997Sken		/*
2039229997Sken		 * Send the notify acknowledge down to the SIM, to let it
2040229997Sken		 * know we processed the task management command.
2041229997Sken		 */
2042229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
2043229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2044304417Smav		switch (io->taskio.task_status) {
2045304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
2046304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
2047304417Smav			break;
2048304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
2049304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
2050304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2051304417Smav			break;
2052304417Smav		case CTL_TASK_FUNCTION_REJECTED:
2053304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
2054304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2055304417Smav			break;
2056304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
2057304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
2058304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2059304417Smav			break;
2060304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2061304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
2062304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
2063304417Smav			break;
2064304417Smav		}
2065304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
2066229997Sken		xpt_action(ccb);
2067275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
2068275881Smav		if (softc->flags & CTLFE_LUN_WILDCARD) {
2069275881Smav			ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
2070275881Smav			ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
2071275881Smav		}
2072275881Smav		if (periph->flags & CAM_PERIPH_INVALID) {
2073275881Smav			ctlfe_free_ccb(periph, ccb);
2074275881Smav		} else {
2075275881Smav			cam_periph_unlock(periph);
2076275881Smav			xpt_action(ccb);
2077275881Smav			return;
2078275881Smav		}
2079229997Sken	} else {
2080275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2081229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2082229997Sken				  periph_links.tqe);
2083275880Smav		xpt_schedule(periph, /*priority*/ 1);
2084229997Sken	}
2085229997Sken
2086260387Sscottl	cam_periph_unlock(periph);
2087229997Sken}
2088229997Sken
2089229997Skenstatic void
2090229997Skenctlfe_dump(void)
2091229997Sken{
2092229997Sken	struct ctlfe_softc *bus_softc;
2093275880Smav	struct ctlfe_lun_softc *lun_softc;
2094229997Sken
2095229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2096229997Sken		ctlfe_dump_sim(bus_softc->sim);
2097275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
2098229997Sken			ctlfe_dump_queue(lun_softc);
2099229997Sken	}
2100229997Sken}
2101