scsi_ctl.c revision 315939
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 315939 2017-03-25 11:45:19Z 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>
56314749Smav#include <sys/taskqueue.h>
57229997Sken#include <machine/bus.h>
58229997Sken
59229997Sken#include <cam/cam.h>
60229997Sken#include <cam/cam_ccb.h>
61229997Sken#include <cam/cam_periph.h>
62229997Sken#include <cam/cam_queue.h>
63229997Sken#include <cam/cam_xpt_periph.h>
64229997Sken#include <cam/cam_debug.h>
65229997Sken#include <cam/cam_sim.h>
66229997Sken#include <cam/cam_xpt.h>
67229997Sken
68229997Sken#include <cam/scsi/scsi_all.h>
69229997Sken#include <cam/scsi/scsi_message.h>
70229997Sken
71229997Sken#include <cam/ctl/ctl_io.h>
72229997Sken#include <cam/ctl/ctl.h>
73229997Sken#include <cam/ctl/ctl_frontend.h>
74229997Sken#include <cam/ctl/ctl_util.h>
75229997Sken#include <cam/ctl/ctl_error.h>
76229997Sken
77229997Skenstruct ctlfe_softc {
78290776Smav	struct ctl_port	port;
79290776Smav	path_id_t	path_id;
80290776Smav	target_id_t	target_id;
81290776Smav	uint32_t	hba_misc;
82290776Smav	u_int		maxio;
83229997Sken	struct cam_sim *sim;
84290776Smav	char		port_name[DEV_IDLEN];
85290776Smav	struct mtx	lun_softc_mtx;
86229997Sken	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
87229997Sken	STAILQ_ENTRY(ctlfe_softc) links;
88229997Sken};
89229997Sken
90229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
91229997Skenstruct mtx ctlfe_list_mtx;
92229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist";
93229997Sken
94229997Skentypedef enum {
95229997Sken	CTLFE_LUN_NONE		= 0x00,
96229997Sken	CTLFE_LUN_WILDCARD	= 0x01
97229997Sken} ctlfe_lun_flags;
98229997Sken
99229997Skenstruct ctlfe_lun_softc {
100229997Sken	struct ctlfe_softc *parent_softc;
101229997Sken	struct cam_periph *periph;
102229997Sken	ctlfe_lun_flags flags;
103314749Smav	int	 ctios_sent;		/* Number of active CTIOs */
104314749Smav	int	 refcount;		/* Number of active xpt_action() */
105314749Smav	int	 atios_alloced;		/* Number of ATIOs not freed */
106314749Smav	int	 inots_alloced;		/* Number of INOTs not freed */
107314749Smav	struct task	refdrain_task;
108315887Smav	STAILQ_HEAD(, ccb_hdr) work_queue;
109315889Smav	LIST_HEAD(, ccb_hdr) atio_list;	/* List of ATIOs queued to SIM. */
110315889Smav	LIST_HEAD(, ccb_hdr) inot_list;	/* List of INOTs queued to SIM. */
111229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
112229997Sken};
113229997Sken
114229997Skentypedef enum {
115229997Sken	CTLFE_CMD_NONE		= 0x00,
116229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
117229997Sken} ctlfe_cmd_flags;
118229997Sken
119288723Smavstruct ctlfe_cmd_info {
120229997Sken	int cur_transfer_index;
121265641Smav	size_t cur_transfer_off;
122229997Sken	ctlfe_cmd_flags flags;
123229997Sken	/*
124229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
125229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
126229997Sken	 * i386 and 512 bytes on amd64.
127229997Sken	 */
128265641Smav#define CTLFE_MAX_SEGS	32
129265641Smav	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
130229997Sken};
131229997Sken
132229997Sken/*
133229997Sken * When we register the adapter/bus, request that this many ctl_ios be
134229997Sken * allocated.  This should be the maximum supported by the adapter, but we
135229997Sken * currently don't have a way to get that back from the path inquiry.
136229997Sken * XXX KDM add that to the path inquiry.
137229997Sken */
138229997Sken#define	CTLFE_REQ_CTL_IO	4096
139229997Sken/*
140229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
141229997Sken * adapter per LUN.
142229997Sken * XXX KDM should this be controlled by CTL?
143229997Sken */
144229997Sken#define	CTLFE_ATIO_PER_LUN	1024
145229997Sken/*
146229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
147229997Sken * allocate and queue down to the adapter per LUN.
148229997Sken * XXX KDM should this be controlled by CTL?
149229997Sken */
150229997Sken#define	CTLFE_IN_PER_LUN	1024
151229997Sken
152229997Sken/*
153314751Smav * Timeout (in seconds) on CTIO CCB doing DMA or sending status
154229997Sken */
155314751Smav#define	CTLFE_TIMEOUT	5
156229997Sken
157229997Sken/*
158229997Sken * Turn this on to enable extra debugging prints.
159229997Sken */
160229997Sken#if 0
161229997Sken#define	CTLFE_DEBUG
162229997Sken#endif
163229997Sken
164229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
165229997Sken
166275878Smav#define	io_ptr		ppriv_ptr0
167229997Sken
168229997Sken/* This is only used in the CTIO */
169229997Sken#define	ccb_atio	ppriv_ptr1
170229997Sken
171312585Smav#define PRIV_CCB(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
172312585Smav#define PRIV_INFO(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
173312585Smav
174313369Smavstatic int		ctlfeinitialize(void);
175313369Smavstatic int		ctlfeshutdown(void);
176268677Smavstatic periph_init_t	ctlfeperiphinit;
177229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
178229997Sken				   struct cam_path *path, void *arg);
179229997Skenstatic periph_ctor_t	ctlferegister;
180229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
181229997Skenstatic periph_dtor_t	ctlfecleanup;
182229997Skenstatic periph_start_t	ctlfestart;
183229997Skenstatic void		ctlfedone(struct cam_periph *periph,
184229997Sken				  union ccb *done_ccb);
185229997Sken
186229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
187229997Skenstatic void 		ctlfe_online(void *arg);
188229997Skenstatic void 		ctlfe_offline(void *arg);
189284798Smavstatic int 		ctlfe_lun_enable(void *arg, int lun_id);
190284798Smavstatic int 		ctlfe_lun_disable(void *arg, int lun_id);
191229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
192229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
193275880Smavstatic void 		ctlfe_datamove(union ctl_io *io);
194275880Smavstatic void 		ctlfe_done(union ctl_io *io);
195229997Skenstatic void 		ctlfe_dump(void);
196314739Smavstatic void		ctlfe_free_ccb(struct cam_periph *periph,
197314739Smav			    union ccb *ccb);
198314739Smavstatic void		ctlfe_requeue_ccb(struct cam_periph *periph,
199314739Smav			    union ccb *ccb, int unlock);
200229997Sken
201229997Skenstatic struct periph_driver ctlfe_driver =
202229997Sken{
203268677Smav	ctlfeperiphinit, "ctl",
204273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
205273316Smav	CAM_PERIPH_DRV_EARLY
206229997Sken};
207229997Sken
208268677Smavstatic struct ctl_frontend ctlfe_frontend =
209268677Smav{
210273318Smav	.name = "camtgt",
211268677Smav	.init = ctlfeinitialize,
212268677Smav	.fe_dump = ctlfe_dump,
213268677Smav	.shutdown = ctlfeshutdown,
214249009Strasz};
215268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
216249009Strasz
217313369Smavstatic int
218229997Skenctlfeshutdown(void)
219229997Sken{
220313369Smav
221313369Smav	/* CAM does not support periph driver unregister now. */
222313369Smav	return (EBUSY);
223229997Sken}
224229997Sken
225313369Smavstatic int
226268677Smavctlfeinitialize(void)
227229997Sken{
228229997Sken
229229997Sken	STAILQ_INIT(&ctlfe_softc_list);
230229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
231268677Smav	periphdriver_register(&ctlfe_driver);
232268677Smav	return (0);
233268677Smav}
234229997Sken
235313369Smavstatic void
236268677Smavctlfeperiphinit(void)
237268677Smav{
238268677Smav	cam_status status;
239229997Sken
240229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
241229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
242229997Sken	if (status != CAM_REQ_CMP) {
243229997Sken		printf("ctl: Failed to attach async callback due to CAM "
244229997Sken		       "status 0x%x!\n", status);
245229997Sken	}
246229997Sken}
247229997Sken
248229997Skenstatic void
249229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
250229997Sken{
251273317Smav	struct ctlfe_softc *softc;
252229997Sken
253229997Sken#ifdef CTLFEDEBUG
254229997Sken	printf("%s: entered\n", __func__);
255229997Sken#endif
256229997Sken
257273317Smav	mtx_lock(&ctlfe_list_mtx);
258273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
259273317Smav		if (softc->path_id == xpt_path_path_id(path))
260273317Smav			break;
261273317Smav	}
262273317Smav	mtx_unlock(&ctlfe_list_mtx);
263273317Smav
264229997Sken	/*
265229997Sken	 * When a new path gets registered, and it is capable of target
266229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
267229997Sken	 * selective, but for now this will be sufficient.
268229997Sken 	 */
269229997Sken	switch (code) {
270229997Sken	case AC_PATH_REGISTERED: {
271268677Smav		struct ctl_port *port;
272229997Sken		struct ccb_pathinq *cpi;
273229997Sken		int retval;
274229997Sken
275229997Sken		cpi = (struct ccb_pathinq *)arg;
276229997Sken
277229997Sken		/* Don't attach if it doesn't support target mode */
278229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
279230033Sken#ifdef CTLFEDEBUG
280229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
281229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
282230033Sken#endif
283229997Sken			break;
284229997Sken		}
285229997Sken
286273317Smav		if (softc != NULL) {
287273317Smav#ifdef CTLFEDEBUG
288273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
289273317Smav			       __func__, xpt_path_path_id(path));
290273317Smav#endif
291273317Smav			break;
292273317Smav		}
293273317Smav
294229997Sken		/*
295229997Sken		 * We're in an interrupt context here, so we have to
296229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
297229997Sken		 * can't allocate memory.
298229997Sken		 */
299273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
300273317Smav		if (softc == NULL) {
301229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
302273317Smav			       __func__, sizeof(*softc));
303229997Sken			return;
304229997Sken		}
305229997Sken
306273317Smav		softc->path_id = cpi->ccb_h.path_id;
307288713Smav		softc->target_id = cpi->initiator_id;
308273317Smav		softc->sim = xpt_path_sim(path);
309290776Smav		softc->hba_misc = cpi->hba_misc;
310265641Smav		if (cpi->maxio != 0)
311273317Smav			softc->maxio = cpi->maxio;
312265641Smav		else
313273317Smav			softc->maxio = DFLTPHYS;
314273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
315273317Smav		STAILQ_INIT(&softc->lun_softc_list);
316229997Sken
317273317Smav		port = &softc->port;
318268677Smav		port->frontend = &ctlfe_frontend;
319229997Sken
320229997Sken		/*
321229997Sken		 * XXX KDM should we be more accurate here ?
322229997Sken		 */
323229997Sken		if (cpi->transport == XPORT_FC)
324268677Smav			port->port_type = CTL_PORT_FC;
325268694Smav		else if (cpi->transport == XPORT_SAS)
326268694Smav			port->port_type = CTL_PORT_SAS;
327229997Sken		else
328268677Smav			port->port_type = CTL_PORT_SCSI;
329229997Sken
330229997Sken		/* XXX KDM what should the real number be here? */
331314751Smav		port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
332273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
333229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
334229997Sken		/*
335229997Sken		 * XXX KDM it would be nice to allocate storage in the
336229997Sken		 * frontend structure itself.
337229997Sken	 	 */
338273317Smav		port->port_name = softc->port_name;
339273319Smav		port->physical_port = cpi->bus_id;
340273319Smav		port->virtual_port = 0;
341268677Smav		port->port_online = ctlfe_online;
342268677Smav		port->port_offline = ctlfe_offline;
343273317Smav		port->onoff_arg = softc;
344268677Smav		port->lun_enable = ctlfe_lun_enable;
345268677Smav		port->lun_disable = ctlfe_lun_disable;
346273317Smav		port->targ_lun_arg = softc;
347275880Smav		port->fe_datamove = ctlfe_datamove;
348275880Smav		port->fe_done = ctlfe_done;
349229997Sken		/*
350229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
351229997Sken		 * number of targets supported.
352229997Sken		 */
353268677Smav		port->max_targets = cpi->max_target;
354268677Smav		port->max_target_id = cpi->max_target;
355288732Smav		port->targ_port = -1;
356314751Smav
357275493Smav		retval = ctl_port_register(port);
358229997Sken		if (retval != 0) {
359268677Smav			printf("%s: ctl_port_register() failed with "
360229997Sken			       "error %d!\n", __func__, retval);
361273317Smav			mtx_destroy(&softc->lun_softc_mtx);
362273317Smav			free(softc, M_CTLFE);
363229997Sken			break;
364229997Sken		} else {
365229997Sken			mtx_lock(&ctlfe_list_mtx);
366273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
367229997Sken			mtx_unlock(&ctlfe_list_mtx);
368229997Sken		}
369229997Sken
370245228Sken		break;
371245228Sken	}
372245228Sken	case AC_PATH_DEREGISTERED: {
373245228Sken
374245228Sken		if (softc != NULL) {
375245228Sken			/*
376245228Sken			 * XXX KDM are we certain at this point that there
377245228Sken			 * are no outstanding commands for this frontend?
378245228Sken			 */
379273317Smav			mtx_lock(&ctlfe_list_mtx);
380273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
381273317Smav			    links);
382273317Smav			mtx_unlock(&ctlfe_list_mtx);
383268677Smav			ctl_port_deregister(&softc->port);
384260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
385245228Sken			free(softc, M_CTLFE);
386229997Sken		}
387229997Sken		break;
388229997Sken	}
389229997Sken	case AC_CONTRACT: {
390229997Sken		struct ac_contract *ac;
391229997Sken
392229997Sken		ac = (struct ac_contract *)arg;
393229997Sken
394229997Sken		switch (ac->contract_number) {
395229997Sken		case AC_CONTRACT_DEV_CHG: {
396229997Sken			struct ac_device_changed *dev_chg;
397273317Smav			int retval;
398229997Sken
399229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
400229997Sken
401236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
402229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
403229997Sken			       xpt_path_path_id(path), dev_chg->target,
404229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
405229997Sken
406273317Smav			if (softc == NULL) {
407229997Sken				printf("%s: CTL port for CAM path %u not "
408229997Sken				       "found!\n", __func__,
409229997Sken				       xpt_path_path_id(path));
410229997Sken				break;
411229997Sken			}
412229997Sken			if (dev_chg->arrived != 0) {
413268692Smav				retval = ctl_add_initiator(&softc->port,
414268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
415229997Sken			} else {
416268692Smav				retval = ctl_remove_initiator(&softc->port,
417268692Smav				    dev_chg->target);
418229997Sken			}
419229997Sken
420268692Smav			if (retval < 0) {
421229997Sken				printf("%s: could not %s port %d iid %u "
422229997Sken				       "WWPN %#jx!\n", __func__,
423229997Sken				       (dev_chg->arrived != 0) ? "add" :
424268677Smav				       "remove", softc->port.targ_port,
425229997Sken				       dev_chg->target,
426229997Sken				       (uintmax_t)dev_chg->wwpn);
427229997Sken			}
428229997Sken			break;
429229997Sken		}
430229997Sken		default:
431229997Sken			printf("%s: unsupported contract number %ju\n",
432229997Sken			       __func__, (uintmax_t)ac->contract_number);
433229997Sken			break;
434229997Sken		}
435229997Sken		break;
436229997Sken	}
437229997Sken	default:
438229997Sken		break;
439229997Sken	}
440229997Sken}
441229997Sken
442229997Skenstatic cam_status
443229997Skenctlferegister(struct cam_periph *periph, void *arg)
444229997Sken{
445229997Sken	struct ctlfe_softc *bus_softc;
446229997Sken	struct ctlfe_lun_softc *softc;
447315889Smav	union ccb ccb;
448229997Sken	cam_status status;
449229997Sken	int i;
450229997Sken
451229997Sken	softc = (struct ctlfe_lun_softc *)arg;
452229997Sken	bus_softc = softc->parent_softc;
453229997Sken
454315887Smav	STAILQ_INIT(&softc->work_queue);
455315889Smav	LIST_INIT(&softc->atio_list);
456315889Smav	LIST_INIT(&softc->inot_list);
457229997Sken	softc->periph = periph;
458229997Sken	periph->softc = softc;
459229997Sken
460315939Smav	/* Increase device openings to maximum for the SIM. */
461315939Smav	if (bus_softc->sim->max_tagged_dev_openings >
462315939Smav	    bus_softc->sim->max_dev_openings) {
463315939Smav		cam_release_devq(periph->path,
464315939Smav		    /*relsim_flags*/RELSIM_ADJUST_OPENINGS,
465315939Smav		    /*openings*/bus_softc->sim->max_tagged_dev_openings,
466315939Smav		    /*timeout*/0,
467315939Smav		    /*getcount_only*/1);
468315939Smav	}
469315939Smav
470315889Smav	xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
471315889Smav	ccb.ccb_h.func_code = XPT_EN_LUN;
472315889Smav	ccb.cel.grp6_len = 0;
473315889Smav	ccb.cel.grp7_len = 0;
474315889Smav	ccb.cel.enable = 1;
475315889Smav	xpt_action(&ccb);
476315889Smav	status = (ccb.ccb_h.status & CAM_STATUS_MASK);
477229997Sken	if (status != CAM_REQ_CMP) {
478229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
479315889Smav			  __func__, ccb.ccb_h.status);
480229997Sken		return (status);
481229997Sken	}
482229997Sken
483229997Sken	status = CAM_REQ_CMP;
484229997Sken
485229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
486229997Sken		union ccb *new_ccb;
487275878Smav		union ctl_io *new_io;
488288723Smav		struct ctlfe_cmd_info *cmd_info;
489229997Sken
490229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
491236426Smjacob					      M_ZERO|M_NOWAIT);
492229997Sken		if (new_ccb == NULL) {
493229997Sken			status = CAM_RESRC_UNAVAIL;
494229997Sken			break;
495229997Sken		}
496275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
497275878Smav		if (new_io == NULL) {
498275878Smav			free(new_ccb, M_CTLFE);
499275878Smav			status = CAM_RESRC_UNAVAIL;
500275878Smav			break;
501275878Smav		}
502288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
503288723Smav		    M_ZERO | M_NOWAIT);
504288723Smav		if (cmd_info == NULL) {
505288723Smav			ctl_free_io(new_io);
506288723Smav			free(new_ccb, M_CTLFE);
507288723Smav			status = CAM_RESRC_UNAVAIL;
508288723Smav			break;
509288723Smav		}
510312585Smav		PRIV_INFO(new_io) = cmd_info;
511284793Smav		softc->atios_alloced++;
512275878Smav		new_ccb->ccb_h.io_ptr = new_io;
513315889Smav		LIST_INSERT_HEAD(&softc->atio_list, &new_ccb->ccb_h, periph_links.le);
514275878Smav
515229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
516229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
517229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
518260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
519229997Sken		xpt_action(new_ccb);
520229997Sken		status = new_ccb->ccb_h.status;
521229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
522288723Smav			free(cmd_info, M_CTLFE);
523275878Smav			ctl_free_io(new_io);
524229997Sken			free(new_ccb, M_CTLFE);
525229997Sken			break;
526229997Sken		}
527229997Sken	}
528229997Sken
529229997Sken	status = cam_periph_acquire(periph);
530229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
531229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
532229997Sken			  "count, status = %#x\n", __func__, status);
533229997Sken		return (status);
534229997Sken	}
535229997Sken
536229997Sken	if (i == 0) {
537229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
538229997Sken			  "status 0x%x\n", __func__, status);
539229997Sken		return (CAM_REQ_CMP_ERR);
540229997Sken	}
541229997Sken
542229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
543229997Sken		union ccb *new_ccb;
544275878Smav		union ctl_io *new_io;
545229997Sken
546229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
547236426Smjacob					      M_ZERO|M_NOWAIT);
548229997Sken		if (new_ccb == NULL) {
549229997Sken			status = CAM_RESRC_UNAVAIL;
550229997Sken			break;
551229997Sken		}
552275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
553275878Smav		if (new_io == NULL) {
554275878Smav			free(new_ccb, M_CTLFE);
555275878Smav			status = CAM_RESRC_UNAVAIL;
556275878Smav			break;
557275878Smav		}
558284793Smav		softc->inots_alloced++;
559275878Smav		new_ccb->ccb_h.io_ptr = new_io;
560315889Smav		LIST_INSERT_HEAD(&softc->inot_list, &new_ccb->ccb_h, periph_links.le);
561229997Sken
562229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
563229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
564229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
565260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
566229997Sken		xpt_action(new_ccb);
567229997Sken		status = new_ccb->ccb_h.status;
568237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
569237601Sken			/*
570237601Sken			 * Note that we don't free the CCB here.  If the
571237601Sken			 * status is not CAM_REQ_INPROG, then we're
572237601Sken			 * probably talking to a SIM that says it is
573237601Sken			 * target-capable but doesn't support the
574237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
575237601Sken			 * older API.  In that case, it'll call xpt_done()
576237601Sken			 * on the CCB, and we need to free it in our done
577237601Sken			 * routine as a result.
578237601Sken			 */
579229997Sken			break;
580229997Sken		}
581229997Sken	}
582237601Sken	if ((i == 0)
583237601Sken	 || (status != CAM_REQ_INPROG)) {
584229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
585229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
586229997Sken		return (CAM_REQ_CMP_ERR);
587229997Sken	}
588275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
589275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
590275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
591229997Sken	return (CAM_REQ_CMP);
592229997Sken}
593229997Sken
594229997Skenstatic void
595229997Skenctlfeoninvalidate(struct cam_periph *periph)
596229997Sken{
597315889Smav	struct ctlfe_lun_softc *softc = (struct ctlfe_lun_softc *)periph->softc;
598315889Smav	struct ctlfe_softc *bus_softc;
599315889Smav	union ccb ccb;
600315889Smav	struct ccb_hdr *hdr;
601229997Sken	cam_status status;
602229997Sken
603315889Smav	/* Abort all ATIOs and INOTs queued to SIM. */
604315889Smav	xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
605315889Smav	ccb.ccb_h.func_code = XPT_ABORT;
606315889Smav	LIST_FOREACH(hdr, &softc->atio_list, periph_links.le) {
607315889Smav		ccb.cab.abort_ccb = (union ccb *)hdr;
608315889Smav		xpt_action(&ccb);
609315889Smav	}
610315889Smav	LIST_FOREACH(hdr, &softc->inot_list, periph_links.le) {
611315889Smav		ccb.cab.abort_ccb = (union ccb *)hdr;
612315889Smav		xpt_action(&ccb);
613315889Smav	}
614229997Sken
615315889Smav	/* Disable the LUN in SIM. */
616315889Smav	ccb.ccb_h.func_code = XPT_EN_LUN;
617315889Smav	ccb.cel.grp6_len = 0;
618315889Smav	ccb.cel.grp7_len = 0;
619315889Smav	ccb.cel.enable = 0;
620315889Smav	xpt_action(&ccb);
621315889Smav	status = (ccb.ccb_h.status & CAM_STATUS_MASK);
622229997Sken	if (status != CAM_REQ_CMP) {
623229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
624315889Smav			  __func__, ccb.ccb_h.status);
625229997Sken		/*
626229997Sken		 * XXX KDM what do we do now?
627229997Sken		 */
628229997Sken	}
629260387Sscottl
630260387Sscottl	bus_softc = softc->parent_softc;
631260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
632260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
633260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
634229997Sken}
635229997Sken
636229997Skenstatic void
637229997Skenctlfecleanup(struct cam_periph *periph)
638229997Sken{
639229997Sken	struct ctlfe_lun_softc *softc;
640229997Sken
641229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
642229997Sken
643314749Smav	KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
644314749Smav	    __func__, softc->ctios_sent));
645314749Smav	KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
646314749Smav	    __func__, softc->refcount));
647314749Smav	KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
648314749Smav	    __func__, softc->atios_alloced));
649314749Smav	KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
650314749Smav	    __func__, softc->inots_alloced));
651245228Sken
652229997Sken	free(softc, M_CTLFE);
653229997Sken}
654229997Sken
655229997Skenstatic void
656265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
657265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
658265641Smav    u_int16_t *sglist_cnt)
659265641Smav{
660265641Smav	struct ctlfe_softc *bus_softc;
661288723Smav	struct ctlfe_cmd_info *cmd_info;
662265641Smav	struct ctl_sg_entry *ctl_sglist;
663265641Smav	bus_dma_segment_t *cam_sglist;
664265641Smav	size_t off;
665265641Smav	int i, idx;
666265641Smav
667312585Smav	cmd_info = PRIV_INFO(io);
668265641Smav	bus_softc = softc->parent_softc;
669265641Smav
670265641Smav	/*
671265641Smav	 * Set the direction, relative to the initiator.
672265641Smav	 */
673265641Smav	*flags &= ~CAM_DIR_MASK;
674265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
675265641Smav		*flags |= CAM_DIR_IN;
676265641Smav	else
677265641Smav		*flags |= CAM_DIR_OUT;
678265641Smav
679265641Smav	*flags &= ~CAM_DATA_MASK;
680265641Smav	idx = cmd_info->cur_transfer_index;
681265641Smav	off = cmd_info->cur_transfer_off;
682265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
683313365Smav	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
684313365Smav
685313365Smav		/* One time shift for SRR offset. */
686313365Smav		off += io->scsiio.ext_data_filled;
687313365Smav		io->scsiio.ext_data_filled = 0;
688313365Smav
689265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
690265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
691265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
692265641Smav		} else {
693265641Smav			*dxfer_len = bus_softc->maxio;
694313365Smav			cmd_info->cur_transfer_off += bus_softc->maxio;
695265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
696265641Smav		}
697265641Smav		*sglist_cnt = 0;
698265641Smav
699265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
700265641Smav			*flags |= CAM_DATA_PADDR;
701265641Smav		else
702265641Smav			*flags |= CAM_DATA_VADDR;
703313365Smav	} else {	/* S/G list with physical or virtual pointers. */
704265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
705313365Smav
706313365Smav		/* One time shift for SRR offset. */
707313365Smav		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
708313365Smav			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
709313365Smav			idx++;
710313365Smav			off = 0;
711313365Smav		}
712313365Smav		off += io->scsiio.ext_data_filled;
713313365Smav		io->scsiio.ext_data_filled = 0;
714313365Smav
715265641Smav		cam_sglist = cmd_info->cam_sglist;
716265641Smav		*dxfer_len = 0;
717265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
718265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
719265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
720265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
721265641Smav				*dxfer_len += cam_sglist[i].ds_len;
722265641Smav			} else {
723265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
724265641Smav				cmd_info->cur_transfer_index = idx + i;
725265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
726265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
727265641Smav				*dxfer_len += cam_sglist[i].ds_len;
728265641Smav				if (ctl_sglist[i].len != 0)
729265641Smav					i++;
730265641Smav				break;
731265641Smav			}
732265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
733265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
734265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
735265641Smav				cmd_info->cur_transfer_off = 0;
736265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
737265641Smav				i++;
738265641Smav				break;
739265641Smav			}
740265641Smav			off = 0;
741265641Smav		}
742265641Smav		*sglist_cnt = i;
743265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
744265641Smav			*flags |= CAM_DATA_SG_PADDR;
745265641Smav		else
746265641Smav			*flags |= CAM_DATA_SG;
747265641Smav		*data_ptr = (uint8_t *)cam_sglist;
748265641Smav	}
749265641Smav}
750265641Smav
751265641Smavstatic void
752229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
753229997Sken{
754229997Sken	struct ctlfe_lun_softc *softc;
755288723Smav	struct ctlfe_cmd_info *cmd_info;
756229997Sken	struct ccb_hdr *ccb_h;
757275880Smav	struct ccb_accept_tio *atio;
758275880Smav	struct ccb_scsiio *csio;
759275880Smav	uint8_t *data_ptr;
760275880Smav	uint32_t dxfer_len;
761275880Smav	ccb_flags flags;
762275880Smav	union ctl_io *io;
763275880Smav	uint8_t scsi_status;
764229997Sken
765229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
766229997Sken
767314739Smavnext:
768315887Smav	/* Take the ATIO off the work queue */
769315887Smav	ccb_h = STAILQ_FIRST(&softc->work_queue);
770260387Sscottl	if (ccb_h == NULL) {
771229997Sken		xpt_release_ccb(start_ccb);
772275880Smav		return;
773275880Smav	}
774315887Smav	STAILQ_REMOVE_HEAD(&softc->work_queue, periph_links.stqe);
775275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
776275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
777275880Smav	csio = &start_ccb->csio;
778229997Sken
779275880Smav	flags = atio->ccb_h.flags &
780275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
781312585Smav	cmd_info = PRIV_INFO(io);
782275881Smav	cmd_info->cur_transfer_index = 0;
783275881Smav	cmd_info->cur_transfer_off = 0;
784275881Smav	cmd_info->flags = 0;
785229997Sken
786275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
787275880Smav		/*
788275880Smav		 * Datamove call, we need to setup the S/G list.
789275880Smav		 */
790275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
791275880Smav		    &csio->sglist_cnt);
792275880Smav	} else {
793275880Smav		/*
794275880Smav		 * We're done, send status back.
795275880Smav		 */
796275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
797275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
798275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
799275880Smav
800314737Smav			/* Tell the SIM that we've aborted this ATIO */
801314737Smav#ifdef CTLFEDEBUG
802314737Smav			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
803314737Smav#endif
804314737Smav			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
805314737Smav			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
806275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
807275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
808275880Smav			xpt_action(start_ccb);
809229997Sken
810314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
811314739Smav			    /* unlock */0);
812229997Sken
813314739Smav			/* XPT_ABORT is not queued, so we can take next I/O. */
814314739Smav			goto next;
815275880Smav		}
816275881Smav		data_ptr = NULL;
817275881Smav		dxfer_len = 0;
818275881Smav		csio->sglist_cnt = 0;
819275881Smav	}
820313365Smav	scsi_status = 0;
821275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
822275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
823275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
824275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
825275880Smav		flags |= CAM_SEND_STATUS;
826275880Smav		scsi_status = io->scsiio.scsi_status;
827275880Smav		csio->sense_len = io->scsiio.sense_len;
828275880Smav#ifdef CTLFEDEBUG
829275880Smav		printf("%s: tag %04x status %x\n", __func__,
830275880Smav		       atio->tag_id, io->io_hdr.status);
831275880Smav#endif
832275880Smav		if (csio->sense_len != 0) {
833275880Smav			csio->sense_data = io->scsiio.sense_data;
834275880Smav			flags |= CAM_SEND_SENSE;
835229997Sken		}
836275880Smav	}
837229997Sken
838229997Sken#ifdef CTLFEDEBUG
839275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
840275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
841275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
842229997Sken#endif
843229997Sken
844275880Smav	/*
845275880Smav	 * Valid combinations:
846275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
847275880Smav	 *    sglist_cnt = 0
848275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
849275880Smav	 *    sglist_cnt = 0
850275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
851275880Smav	 *    sglist_cnt != 0
852275880Smav	 */
853229997Sken#ifdef CTLFEDEBUG
854275880Smav	if (((flags & CAM_SEND_STATUS)
855275880Smav	  && (((flags & CAM_DATA_SG) != 0)
856275880Smav	   || (dxfer_len != 0)
857275880Smav	   || (csio->sglist_cnt != 0)))
858275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
859275880Smav	  && (dxfer_len == 0))
860275880Smav	 || ((flags & CAM_DATA_SG)
861275880Smav	  && (csio->sglist_cnt == 0))
862275880Smav	 || (((flags & CAM_DATA_SG) == 0)
863275880Smav	  && (csio->sglist_cnt != 0))) {
864275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
865275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
866312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
867275880Smav		       csio->sglist_cnt);
868275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
869275880Smav		       atio->tag_id, io->io_hdr.status);
870275880Smav	}
871229997Sken#endif
872275880Smav	cam_fill_ctio(csio,
873275880Smav		      /*retries*/ 2,
874275880Smav		      ctlfedone,
875275880Smav		      flags,
876275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
877275880Smav		      atio->tag_id,
878275880Smav		      atio->init_id,
879275880Smav		      scsi_status,
880275880Smav		      /*data_ptr*/ data_ptr,
881275880Smav		      /*dxfer_len*/ dxfer_len,
882314751Smav		      /*timeout*/ CTLFE_TIMEOUT * 1000);
883275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
884275880Smav	start_ccb->ccb_h.ccb_atio = atio;
885275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
886275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
887275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
888229997Sken
889275880Smav	softc->ctios_sent++;
890314749Smav	softc->refcount++;
891275880Smav	cam_periph_unlock(periph);
892275880Smav	xpt_action(start_ccb);
893275880Smav	cam_periph_lock(periph);
894314749Smav	softc->refcount--;
895229997Sken
896229997Sken	/*
897275880Smav	 * If we still have work to do, ask for another CCB.
898229997Sken	 */
899315887Smav	if (!STAILQ_EMPTY(&softc->work_queue))
900314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
901229997Sken}
902229997Sken
903229997Skenstatic void
904314749Smavctlfe_drain(void *context, int pending)
905314749Smav{
906314749Smav	struct cam_periph *periph = context;
907314749Smav	struct ctlfe_lun_softc *softc = periph->softc;
908314749Smav
909314749Smav	cam_periph_lock(periph);
910314749Smav	while (softc->refcount != 0) {
911314749Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO,
912314749Smav		    "ctlfe_drain", 1);
913314749Smav	}
914314749Smav	cam_periph_unlock(periph);
915314749Smav	cam_periph_release(periph);
916314749Smav}
917314749Smav
918314749Smavstatic void
919229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
920229997Sken{
921229997Sken	struct ctlfe_lun_softc *softc;
922288723Smav	union ctl_io *io;
923288723Smav	struct ctlfe_cmd_info *cmd_info;
924229997Sken
925229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
926288723Smav	io = ccb->ccb_h.io_ptr;
927229997Sken
928229997Sken	switch (ccb->ccb_h.func_code) {
929229997Sken	case XPT_ACCEPT_TARGET_IO:
930314749Smav		softc->atios_alloced--;
931312585Smav		cmd_info = PRIV_INFO(io);
932288723Smav		free(cmd_info, M_CTLFE);
933229997Sken		break;
934229997Sken	case XPT_IMMEDIATE_NOTIFY:
935229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
936314749Smav		softc->inots_alloced--;
937229997Sken		break;
938229997Sken	default:
939229997Sken		break;
940229997Sken	}
941229997Sken
942288723Smav	ctl_free_io(io);
943229997Sken	free(ccb, M_CTLFE);
944229997Sken
945314749Smav	KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
946314749Smav	    __func__, softc->atios_alloced));
947314749Smav	KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
948314749Smav	    __func__, softc->inots_alloced));
949229997Sken
950229997Sken	/*
951229997Sken	 * If we have received all of our CCBs, we can release our
952229997Sken	 * reference on the peripheral driver.  It will probably go away
953229997Sken	 * now.
954229997Sken	 */
955314749Smav	if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
956314749Smav		if (softc->refcount == 0) {
957314749Smav			cam_periph_release_locked(periph);
958314749Smav		} else {
959314749Smav			TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
960314749Smav			taskqueue_enqueue(taskqueue_thread,
961314749Smav			    &softc->refdrain_task);
962314749Smav		}
963229997Sken	}
964229997Sken}
965229997Sken
966314739Smav/*
967314739Smav * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
968314739Smav */
969314739Smavstatic void
970314739Smavctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
971314739Smav{
972314739Smav	struct ctlfe_lun_softc *softc;
973314740Smav	struct mtx *mtx;
974314739Smav
975314739Smav	if (periph->flags & CAM_PERIPH_INVALID) {
976314740Smav		mtx = cam_periph_mtx(periph);
977314739Smav		ctlfe_free_ccb(periph, ccb);
978314739Smav		if (unlock)
979314740Smav			mtx_unlock(mtx);
980314739Smav		return;
981314739Smav	}
982315889Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
983315889Smav	if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
984315889Smav		LIST_INSERT_HEAD(&softc->atio_list, &ccb->ccb_h, periph_links.le);
985315889Smav	else
986315889Smav		LIST_INSERT_HEAD(&softc->inot_list, &ccb->ccb_h, periph_links.le);
987314739Smav	if (unlock)
988314739Smav		cam_periph_unlock(periph);
989314739Smav
990314739Smav	/*
991314739Smav	 * For a wildcard attachment, commands can come in with a specific
992314739Smav	 * target/lun.  Reset the target and LUN fields back to the wildcard
993314739Smav	 * values before we send them back down to the SIM.
994314739Smav	 */
995314739Smav	if (softc->flags & CTLFE_LUN_WILDCARD) {
996314739Smav		ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
997314739Smav		ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
998314739Smav	}
999314739Smav
1000314739Smav	xpt_action(ccb);
1001314739Smav}
1002314739Smav
1003238870Smjacobstatic int
1004238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1005238870Smjacob{
1006238870Smjacob	uint64_t lba;
1007238870Smjacob	uint32_t num_blocks, nbc;
1008312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
1009238870Smjacob
1010238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1011238870Smjacob
1012238870Smjacob	switch (cmdbyt[0]) {
1013238870Smjacob	case READ_6:
1014238870Smjacob	case WRITE_6:
1015238870Smjacob	{
1016238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1017238870Smjacob		lba = scsi_3btoul(cdb->addr);
1018238870Smjacob		lba &= 0x1fffff;
1019238870Smjacob		num_blocks = cdb->length;
1020238870Smjacob		if (num_blocks == 0)
1021238870Smjacob			num_blocks = 256;
1022238870Smjacob		lba += nbc;
1023238870Smjacob		num_blocks -= nbc;
1024238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1025238870Smjacob		cdb->length = num_blocks;
1026238870Smjacob		break;
1027238870Smjacob	}
1028238870Smjacob	case READ_10:
1029238870Smjacob	case WRITE_10:
1030238870Smjacob	{
1031238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1032238870Smjacob		lba = scsi_4btoul(cdb->addr);
1033238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1034238870Smjacob		lba += nbc;
1035238870Smjacob		num_blocks -= nbc;
1036238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1037238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1038238870Smjacob		break;
1039238870Smjacob	}
1040238870Smjacob	case READ_12:
1041238870Smjacob	case WRITE_12:
1042238870Smjacob	{
1043238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1044238870Smjacob		lba = scsi_4btoul(cdb->addr);
1045238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1046238870Smjacob		lba += nbc;
1047238870Smjacob		num_blocks -= nbc;
1048238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1049238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1050238870Smjacob		break;
1051238870Smjacob	}
1052238870Smjacob	case READ_16:
1053238870Smjacob	case WRITE_16:
1054238870Smjacob	{
1055238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1056238870Smjacob		lba = scsi_8btou64(cdb->addr);
1057238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1058238870Smjacob		lba += nbc;
1059238870Smjacob		num_blocks -= nbc;
1060238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1061238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1062238870Smjacob		break;
1063238870Smjacob	}
1064238870Smjacob	default:
1065238870Smjacob		return -1;
1066238870Smjacob	}
1067238870Smjacob	return (0);
1068238870Smjacob}
1069238870Smjacob
1070229997Skenstatic void
1071229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1072229997Sken{
1073229997Sken	struct ctlfe_lun_softc *softc;
1074229997Sken	struct ctlfe_softc *bus_softc;
1075288723Smav	struct ctlfe_cmd_info *cmd_info;
1076238870Smjacob	struct ccb_accept_tio *atio = NULL;
1077238870Smjacob	union ctl_io *io = NULL;
1078260387Sscottl	struct mtx *mtx;
1079314727Smav	cam_status status;
1080229997Sken
1081260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1082260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1083229997Sken#ifdef CTLFE_DEBUG
1084275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1085275878Smav	       done_ccb->ccb_h.func_code);
1086229997Sken#endif
1087229997Sken
1088284794Smav	/*
1089284794Smav	 * At this point CTL has no known use case for device queue freezes.
1090284794Smav	 * In case some SIM think different -- drop its freeze right here.
1091284794Smav	 */
1092284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1093284794Smav		cam_release_devq(periph->path,
1094284794Smav				 /*relsim_flags*/0,
1095284794Smav				 /*reduction*/0,
1096284794Smav				 /*timeout*/0,
1097284794Smav				 /*getcount_only*/0);
1098284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1099284794Smav	}
1100284794Smav
1101229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1102229997Sken	bus_softc = softc->parent_softc;
1103260387Sscottl	mtx = cam_periph_mtx(periph);
1104260387Sscottl	mtx_lock(mtx);
1105229997Sken
1106229997Sken	switch (done_ccb->ccb_h.func_code) {
1107229997Sken	case XPT_ACCEPT_TARGET_IO: {
1108229997Sken
1109315889Smav		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1110229997Sken		atio = &done_ccb->atio;
1111314727Smav		status = atio->ccb_h.status & CAM_STATUS_MASK;
1112314727Smav		if (status != CAM_CDB_RECVD) {
1113314727Smav			ctlfe_free_ccb(periph, done_ccb);
1114314727Smav			goto out;
1115314727Smav		}
1116229997Sken
1117238870Smjacob resubmit:
1118229997Sken		/*
1119229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1120229997Sken		 * datamove or done.
1121229997Sken		 */
1122260387Sscottl		mtx_unlock(mtx);
1123275878Smav		io = done_ccb->ccb_h.io_ptr;
1124312585Smav		cmd_info = PRIV_INFO(io);
1125229997Sken		ctl_zero_io(io);
1126229997Sken
1127229997Sken		/* Save pointers on both sides */
1128312585Smav		PRIV_CCB(io) = done_ccb;
1129312585Smav		PRIV_INFO(io) = cmd_info;
1130229997Sken		done_ccb->ccb_h.io_ptr = io;
1131229997Sken
1132229997Sken		/*
1133229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1134229997Sken		 * down the immediate notify path below.
1135229997Sken		 */
1136229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1137288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1138268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1139290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1140290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1141290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1142290776Smav		} else {
1143290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1144290776Smav		}
1145229997Sken		io->scsiio.tag_num = atio->tag_id;
1146229997Sken		switch (atio->tag_action) {
1147229997Sken		case CAM_TAG_ACTION_NONE:
1148229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1149229997Sken			break;
1150229997Sken		case MSG_SIMPLE_TASK:
1151229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1152229997Sken			break;
1153229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1154229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1155229997Sken			break;
1156229997Sken		case MSG_ORDERED_TASK:
1157229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1158229997Sken			break;
1159229997Sken		case MSG_ACA_TASK:
1160229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1161229997Sken			break;
1162229997Sken		default:
1163229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1164229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1165229997Sken			       atio->tag_action);
1166229997Sken			break;
1167229997Sken		}
1168229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1169229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1170229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1171229997Sken		}
1172229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1173312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1174229997Sken
1175229997Sken#ifdef CTLFEDEBUG
1176288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1177288731Smav		        io->io_hdr.nexus.initid,
1178229997Sken		        io->io_hdr.nexus.targ_port,
1179229997Sken		        io->io_hdr.nexus.targ_lun,
1180229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1181229997Sken#endif
1182229997Sken
1183229997Sken		ctl_queue(io);
1184260387Sscottl		return;
1185229997Sken	}
1186229997Sken	case XPT_CONT_TARGET_IO: {
1187238870Smjacob		int srr = 0;
1188238870Smjacob		uint32_t srr_off = 0;
1189229997Sken
1190229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1191229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1192229997Sken
1193314749Smav		softc->ctios_sent--;
1194229997Sken#ifdef CTLFEDEBUG
1195229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1196229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1197229997Sken#endif
1198229997Sken		/*
1199238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1200238870Smjacob		 */
1201238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1202238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1203238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1204238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1205238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1206238870Smjacob			srr = 1;
1207238870Smjacob			srr_off =
1208238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1209238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1210238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1211238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1212238870Smjacob		}
1213238870Smjacob
1214313365Smav		/*
1215313365Smav		 * If we have an SRR and we're still sending data, we
1216313365Smav		 * should be able to adjust offsets and cycle again.
1217313365Smav		 * It is possible only if offset is from this datamove.
1218313365Smav		 */
1219313365Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1220313365Smav		    srr_off >= io->scsiio.kern_rel_offset &&
1221313365Smav		    srr_off < io->scsiio.kern_rel_offset +
1222313365Smav		     io->scsiio.kern_data_len) {
1223313365Smav			io->scsiio.kern_data_resid =
1224313365Smav			    io->scsiio.kern_rel_offset +
1225313365Smav			    io->scsiio.kern_data_len - srr_off;
1226313365Smav			io->scsiio.ext_data_filled = srr_off;
1227313365Smav			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1228313365Smav			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1229313365Smav			xpt_release_ccb(done_ccb);
1230315887Smav			STAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1231315887Smav					  periph_links.stqe);
1232314749Smav			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1233313365Smav			break;
1234313365Smav		}
1235313365Smav
1236313365Smav		/*
1237313365Smav		 * If status was being sent, the back end data is now history.
1238313365Smav		 * Hack it up and resubmit a new command with the CDB adjusted.
1239313365Smav		 * If the SIM does the right thing, all of the resid math
1240313365Smav		 * should work.
1241313365Smav		 */
1242275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1243238870Smjacob			xpt_release_ccb(done_ccb);
1244238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1245238870Smjacob				done_ccb = (union ccb *)atio;
1246238870Smjacob				goto resubmit;
1247238870Smjacob			}
1248238870Smjacob			/*
1249238870Smjacob			 * Fall through to doom....
1250238870Smjacob			 */
1251238870Smjacob		}
1252238870Smjacob
1253277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1254277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1255277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1256277919Smav
1257238870Smjacob		/*
1258229997Sken		 * If we were sending status back to the initiator, free up
1259229997Sken		 * resources.  If we were doing a datamove, call the
1260229997Sken		 * datamove done routine.
1261229997Sken		 */
1262275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1263315138Smav			/*
1264315138Smav			 * If we asked to send sense data but it wasn't sent,
1265315138Smav			 * queue the I/O back to CTL for later REQUEST SENSE.
1266315138Smav			 */
1267315138Smav			if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1268315138Smav			    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1269315138Smav			    (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1270315138Smav			    (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1271315138Smav				PRIV_INFO(io) = PRIV_INFO(
1272315138Smav				    (union ctl_io *)atio->ccb_h.io_ptr);
1273315138Smav				ctl_queue_sense(atio->ccb_h.io_ptr);
1274315138Smav				atio->ccb_h.io_ptr = io;
1275315138Smav			}
1276315138Smav
1277314745Smav			/* Abort ATIO if CTIO sending status has failed. */
1278314745Smav			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1279314745Smav			    CAM_REQ_CMP) {
1280314745Smav				done_ccb->ccb_h.func_code = XPT_ABORT;
1281314745Smav				done_ccb->cab.abort_ccb = (union ccb *)atio;
1282314745Smav				xpt_action(done_ccb);
1283314745Smav			}
1284314745Smav
1285229997Sken			xpt_release_ccb(done_ccb);
1286314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
1287314739Smav			    /* unlock */1);
1288314739Smav			return;
1289229997Sken		} else {
1290288723Smav			struct ctlfe_cmd_info *cmd_info;
1291229997Sken			struct ccb_scsiio *csio;
1292229997Sken
1293229997Sken			csio = &done_ccb->csio;
1294312585Smav			cmd_info = PRIV_INFO(io);
1295229997Sken
1296229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1297229997Sken
1298229997Sken			/*
1299229997Sken			 * Translate CAM status to CTL status.  Success
1300229997Sken			 * does not change the overall, ctl_io status.  In
1301229997Sken			 * that case we just set port_status to 0.  If we
1302229997Sken			 * have a failure, though, set a data phase error
1303229997Sken			 * for the overall ctl_io.
1304229997Sken			 */
1305229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1306229997Sken			case CAM_REQ_CMP:
1307314757Smav				io->scsiio.kern_data_resid -=
1308314757Smav				    csio->dxfer_len - csio->resid;
1309229997Sken				io->io_hdr.port_status = 0;
1310229997Sken				break;
1311229997Sken			default:
1312229997Sken				/*
1313245228Sken				 * XXX KDM we probably need to figure out a
1314245228Sken				 * standard set of errors that the SIM
1315245228Sken				 * drivers should return in the event of a
1316245228Sken				 * data transfer failure.  A data phase
1317245228Sken				 * error will at least point the user to a
1318245228Sken				 * data transfer error of some sort.
1319245228Sken				 * Hopefully the SIM printed out some
1320245228Sken				 * additional information to give the user
1321245228Sken				 * a clue what happened.
1322229997Sken				 */
1323229997Sken				io->io_hdr.port_status = 0xbad1;
1324229997Sken				ctl_set_data_phase_error(&io->scsiio);
1325229997Sken				/*
1326229997Sken				 * XXX KDM figure out residual.
1327229997Sken				 */
1328229997Sken				break;
1329229997Sken			}
1330229997Sken			/*
1331229997Sken			 * If we had to break this S/G list into multiple
1332229997Sken			 * pieces, figure out where we are in the list, and
1333229997Sken			 * continue sending pieces if necessary.
1334229997Sken			 */
1335314757Smav			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1336314757Smav			    io->io_hdr.port_status == 0 && csio->resid == 0) {
1337229997Sken				ccb_flags flags;
1338229997Sken				uint8_t *data_ptr;
1339229997Sken				uint32_t dxfer_len;
1340229997Sken
1341229997Sken				flags = atio->ccb_h.flags &
1342229997Sken					(CAM_DIS_DISCONNECT|
1343265641Smav					 CAM_TAG_ACTION_VALID);
1344229997Sken
1345265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1346265641Smav				    &dxfer_len, &csio->sglist_cnt);
1347229997Sken
1348229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1349229997Sken				 && (dxfer_len == 0)) {
1350229997Sken					printf("%s: tag %04x no status or "
1351229997Sken					       "len cdb = %02x\n", __func__,
1352229997Sken					       atio->tag_id,
1353312845Smav					       atio_cdb_ptr(atio)[0]);
1354229997Sken					printf("%s: tag %04x io status %#x\n",
1355229997Sken					       __func__, atio->tag_id,
1356229997Sken					       io->io_hdr.status);
1357229997Sken				}
1358229997Sken
1359229997Sken				cam_fill_ctio(csio,
1360229997Sken					      /*retries*/ 2,
1361229997Sken					      ctlfedone,
1362229997Sken					      flags,
1363229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1364229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1365229997Sken					      atio->tag_id,
1366229997Sken					      atio->init_id,
1367313365Smav					      0,
1368229997Sken					      /*data_ptr*/ data_ptr,
1369229997Sken					      /*dxfer_len*/ dxfer_len,
1370314751Smav					      CTLFE_TIMEOUT * 1000);
1371229997Sken
1372260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1373229997Sken				csio->resid = 0;
1374229997Sken				csio->ccb_h.ccb_atio = atio;
1375229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1376229997Sken				softc->ctios_sent++;
1377260387Sscottl				mtx_unlock(mtx);
1378229997Sken				xpt_action((union ccb *)csio);
1379229997Sken			} else {
1380229997Sken				/*
1381229997Sken				 * Release the CTIO.  The ATIO will be sent back
1382229997Sken				 * down to the SIM once we send status.
1383229997Sken				 */
1384229997Sken				xpt_release_ccb(done_ccb);
1385260387Sscottl				mtx_unlock(mtx);
1386229997Sken
1387229997Sken				/* Call the backend move done callback */
1388229997Sken				io->scsiio.be_move_done(io);
1389229997Sken			}
1390260387Sscottl			return;
1391229997Sken		}
1392229997Sken		break;
1393229997Sken	}
1394229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1395229997Sken		union ctl_io *io;
1396229997Sken		struct ccb_immediate_notify *inot;
1397284794Smav		int send_ctl_io;
1398229997Sken
1399315889Smav		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1400229997Sken		inot = &done_ccb->cin1;
1401275878Smav		io = done_ccb->ccb_h.io_ptr;
1402275878Smav		ctl_zero_io(io);
1403229997Sken
1404275878Smav		send_ctl_io = 1;
1405229997Sken
1406275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1407312585Smav		PRIV_CCB(io) = done_ccb;
1408275878Smav		inot->ccb_h.io_ptr = io;
1409288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1410275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1411290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1412290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1413290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1414290776Smav		} else {
1415290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1416290776Smav		}
1417275878Smav		/* XXX KDM should this be the tag_id? */
1418275878Smav		io->taskio.tag_num = inot->seq_id;
1419229997Sken
1420275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1421275878Smav		switch (status) {
1422275878Smav		case CAM_SCSI_BUS_RESET:
1423275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1424275878Smav			break;
1425275878Smav		case CAM_BDR_SENT:
1426275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1427275878Smav			break;
1428275878Smav		case CAM_MESSAGE_RECV:
1429275878Smav			switch (inot->arg) {
1430275878Smav			case MSG_ABORT_TASK_SET:
1431275878Smav				io->taskio.task_action =
1432275878Smav				    CTL_TASK_ABORT_TASK_SET;
1433229997Sken				break;
1434275878Smav			case MSG_TARGET_RESET:
1435290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1436229997Sken				break;
1437275878Smav			case MSG_ABORT_TASK:
1438290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1439229997Sken				break;
1440275878Smav			case MSG_LOGICAL_UNIT_RESET:
1441290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1442275878Smav				break;
1443275878Smav			case MSG_CLEAR_TASK_SET:
1444275878Smav				io->taskio.task_action =
1445290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1446275878Smav				break;
1447275878Smav			case MSG_CLEAR_ACA:
1448290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1449290775Smav				break;
1450290775Smav			case MSG_QUERY_TASK:
1451290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1452290775Smav				break;
1453290775Smav			case MSG_QUERY_TASK_SET:
1454275878Smav				io->taskio.task_action =
1455290775Smav				    CTL_TASK_QUERY_TASK_SET;
1456275878Smav				break;
1457290775Smav			case MSG_QUERY_ASYNC_EVENT:
1458290775Smav				io->taskio.task_action =
1459290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1460290775Smav				break;
1461275878Smav			case MSG_NOOP:
1462229997Sken				send_ctl_io = 0;
1463229997Sken				break;
1464229997Sken			default:
1465275878Smav				xpt_print(periph->path,
1466314727Smav				    "%s: unsupported INOT message 0x%x\n",
1467314727Smav				    __func__, inot->arg);
1468275878Smav				send_ctl_io = 0;
1469275878Smav				break;
1470275878Smav			}
1471275878Smav			break;
1472314727Smav		default:
1473314727Smav			xpt_print(periph->path,
1474314727Smav			    "%s: unsupported INOT status 0x%x\n",
1475314727Smav			    __func__, status);
1476314727Smav			/* FALLTHROUGH */
1477275878Smav		case CAM_REQ_ABORTED:
1478275878Smav		case CAM_REQ_INVALID:
1479314727Smav		case CAM_DEV_NOT_THERE:
1480275878Smav		case CAM_PROVIDE_FAIL:
1481275878Smav			ctlfe_free_ccb(periph, done_ccb);
1482275878Smav			goto out;
1483275878Smav		}
1484275878Smav		if (send_ctl_io != 0) {
1485275878Smav			ctl_queue(io);
1486229997Sken		} else {
1487229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1488229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1489229997Sken			xpt_action(done_ccb);
1490229997Sken		}
1491229997Sken		break;
1492229997Sken	}
1493229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1494314739Smav		/* Queue this back down to the SIM as an immediate notify. */
1495304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1496229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1497314739Smav		ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1498314739Smav		return;
1499229997Sken	case XPT_SET_SIM_KNOB:
1500229997Sken	case XPT_GET_SIM_KNOB:
1501229997Sken		break;
1502229997Sken	default:
1503229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1504229997Sken		      done_ccb->ccb_h.func_code);
1505229997Sken		break;
1506229997Sken	}
1507260387Sscottl
1508260387Sscottlout:
1509260387Sscottl	mtx_unlock(mtx);
1510229997Sken}
1511229997Sken
1512229997Skenstatic void
1513229997Skenctlfe_onoffline(void *arg, int online)
1514229997Sken{
1515315891Smav	struct ctlfe_softc *bus_softc = arg;
1516229997Sken	union ccb *ccb;
1517229997Sken	cam_status status;
1518229997Sken	struct cam_path *path;
1519315891Smav	int set_wwnn = 0;
1520229997Sken
1521229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1522315891Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1523229997Sken	if (status != CAM_REQ_CMP) {
1524229997Sken		printf("%s: unable to create path!\n", __func__);
1525229997Sken		return;
1526229997Sken	}
1527275882Smav	ccb = xpt_alloc_ccb();
1528242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1529288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1530288713Smav	xpt_action(ccb);
1531229997Sken
1532315891Smav	/* Check whether we should change WWNs. */
1533229997Sken	if (online != 0) {
1534229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1535229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1536229997Sken			       bus_softc->port_name,
1537229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1538229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1539229997Sken			       bus_softc->port_name,
1540229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1541229997Sken
1542229997Sken			/*
1543229997Sken			 * If the user has specified a WWNN/WWPN, send them
1544229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1545229997Sken			 * has reported.
1546229997Sken			 */
1547284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1548284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1549229997Sken				ccb->knob.xport_specific.fc.wwnn =
1550284586Smav				    bus_softc->port.wwnn;
1551284586Smav				set_wwnn = 1;
1552284586Smav			} else {
1553284586Smav				ctl_port_set_wwns(&bus_softc->port,
1554284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1555284586Smav				    false, 0);
1556284586Smav			}
1557284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1558284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1559229997Sken				ccb->knob.xport_specific.fc.wwpn =
1560284586Smav				    bus_softc->port.wwpn;
1561229997Sken				set_wwnn = 1;
1562229997Sken			} else {
1563268683Smav				ctl_port_set_wwns(&bus_softc->port,
1564284586Smav				    false, 0,
1565268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1566229997Sken			}
1567229997Sken		} else {
1568229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1569229997Sken			       bus_softc->port_name);
1570315891Smav			if (bus_softc->port.wwnn != 0) {
1571315891Smav				ccb->knob.xport_specific.fc.wwnn =
1572315891Smav				    bus_softc->port.wwnn;
1573315891Smav				set_wwnn = 1;
1574315891Smav			}
1575315891Smav			if (bus_softc->port.wwpn != 0) {
1576315891Smav				ccb->knob.xport_specific.fc.wwpn =
1577315891Smav				    bus_softc->port.wwpn;
1578315891Smav				set_wwnn = 1;
1579315891Smav			}
1580229997Sken		}
1581229997Sken	}
1582315891Smav	if (set_wwnn) {
1583315891Smav		ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1584315891Smav		ccb->knob.xport_specific.valid = KNOB_VALID_ADDRESS;
1585315891Smav		xpt_action(ccb);
1586315891Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1587315891Smav			printf("%s: %s (path id %d) failed set WWNs: %#x\n",
1588315891Smav			    __func__, bus_softc->port_name, bus_softc->path_id,
1589315891Smav			    ccb->ccb_h.status);
1590315891Smav		} else {
1591315891Smav			printf("%s: %s new WWNN %#jx\n", __func__,
1592315891Smav			       bus_softc->port_name,
1593315891Smav			       ccb->knob.xport_specific.fc.wwnn);
1594315891Smav			printf("%s: %s new WWPN %#jx\n", __func__,
1595315891Smav			       bus_softc->port_name,
1596315891Smav			       ccb->knob.xport_specific.fc.wwpn);
1597315891Smav		}
1598315891Smav	}
1599229997Sken
1600315891Smav	/* Check whether we should change role. */
1601315891Smav	if ((ccb->knob.xport_specific.valid & KNOB_VALID_ROLE) == 0 ||
1602315891Smav	    ((online != 0) ^
1603315891Smav	    ((ccb->knob.xport_specific.fc.role & KNOB_ROLE_TARGET) != 0)) != 0) {
1604315891Smav		ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1605315891Smav		ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1606315891Smav		if (online)
1607315891Smav			ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1608315891Smav		else
1609315891Smav			ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1610315891Smav		xpt_action(ccb);
1611315891Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1612315891Smav			printf("%s: %s (path id %d) failed %s target role: %#x\n",
1613315891Smav			    __func__, bus_softc->port_name, bus_softc->path_id,
1614315891Smav			    online ? "enable" : "disable", ccb->ccb_h.status);
1615315891Smav		} else {
1616315891Smav			printf("%s: %s (path id %d) target role %s succeeded\n",
1617315891Smav			    __func__, bus_softc->port_name, bus_softc->path_id,
1618315891Smav			    online ? "enable" : "disable");
1619315891Smav		}
1620229997Sken	}
1621229997Sken
1622229997Sken	xpt_free_path(path);
1623275882Smav	xpt_free_ccb(ccb);
1624229997Sken}
1625229997Sken
1626229997Skenstatic void
1627229997Skenctlfe_online(void *arg)
1628229997Sken{
1629245228Sken	struct ctlfe_softc *bus_softc;
1630245228Sken	struct cam_path *path;
1631245228Sken	cam_status status;
1632245228Sken	struct ctlfe_lun_softc *lun_softc;
1633274388Smav	struct cam_periph *periph;
1634245228Sken
1635245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1636245228Sken
1637245228Sken	/*
1638245228Sken	 * Create the wildcard LUN before bringing the port online.
1639245228Sken	 */
1640245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1641245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1642245228Sken				 CAM_LUN_WILDCARD);
1643245228Sken	if (status != CAM_REQ_CMP) {
1644245228Sken		printf("%s: unable to create path for wildcard periph\n",
1645245228Sken				__func__);
1646245228Sken		return;
1647245228Sken	}
1648245228Sken
1649275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1650245228Sken
1651260387Sscottl	xpt_path_lock(path);
1652274388Smav	periph = cam_periph_find(path, "ctl");
1653274388Smav	if (periph != NULL) {
1654274388Smav		/* We've already got a periph, no need to alloc a new one. */
1655274388Smav		xpt_path_unlock(path);
1656274388Smav		xpt_free_path(path);
1657274388Smav		free(lun_softc, M_CTLFE);
1658274388Smav		return;
1659274388Smav	}
1660245228Sken	lun_softc->parent_softc = bus_softc;
1661245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1662245228Sken
1663245228Sken	status = cam_periph_alloc(ctlferegister,
1664245228Sken				  ctlfeoninvalidate,
1665245228Sken				  ctlfecleanup,
1666245228Sken				  ctlfestart,
1667245228Sken				  "ctl",
1668245228Sken				  CAM_PERIPH_BIO,
1669245228Sken				  path,
1670245228Sken				  ctlfeasync,
1671245228Sken				  0,
1672245228Sken				  lun_softc);
1673245228Sken
1674245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1675245228Sken		const struct cam_status_entry *entry;
1676245228Sken
1677245228Sken		entry = cam_fetch_status_entry(status);
1678245228Sken		printf("%s: CAM error %s (%#x) returned from "
1679245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1680245228Sken		       entry->status_text : "Unknown", status);
1681274388Smav		free(lun_softc, M_CTLFE);
1682245228Sken	}
1683245228Sken
1684260387Sscottl	xpt_path_unlock(path);
1685275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1686260387Sscottl	xpt_free_path(path);
1687229997Sken}
1688229997Sken
1689229997Skenstatic void
1690229997Skenctlfe_offline(void *arg)
1691229997Sken{
1692245228Sken	struct ctlfe_softc *bus_softc;
1693245228Sken	struct cam_path *path;
1694245228Sken	cam_status status;
1695245228Sken	struct cam_periph *periph;
1696245228Sken
1697245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1698245228Sken
1699275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1700275882Smav
1701245228Sken	/*
1702245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1703245228Sken	 * the port offline.
1704245228Sken	 */
1705245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1706245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1707245228Sken				 CAM_LUN_WILDCARD);
1708245228Sken	if (status != CAM_REQ_CMP) {
1709245228Sken		printf("%s: unable to create path for wildcard periph\n",
1710245228Sken		       __func__);
1711245228Sken		return;
1712245228Sken	}
1713260387Sscottl	xpt_path_lock(path);
1714245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1715245228Sken		cam_periph_invalidate(periph);
1716260387Sscottl	xpt_path_unlock(path);
1717245228Sken	xpt_free_path(path);
1718229997Sken}
1719229997Sken
1720229997Sken/*
1721229997Sken * This will get called to enable a LUN on every bus that is attached to
1722229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1723229997Sken */
1724229997Skenstatic int
1725284798Smavctlfe_lun_enable(void *arg, int lun_id)
1726229997Sken{
1727229997Sken	struct ctlfe_softc *bus_softc;
1728229997Sken	struct ctlfe_lun_softc *softc;
1729229997Sken	struct cam_path *path;
1730229997Sken	struct cam_periph *periph;
1731229997Sken	cam_status status;
1732229997Sken
1733229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1734290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1735290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1736229997Sken
1737260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1738290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1739229997Sken	/* XXX KDM need some way to return status to CTL here? */
1740229997Sken	if (status != CAM_REQ_CMP) {
1741229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1742229997Sken		       status);
1743229997Sken		return (1);
1744229997Sken	}
1745229997Sken
1746229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1747260387Sscottl	xpt_path_lock(path);
1748229997Sken	periph = cam_periph_find(path, "ctl");
1749229997Sken	if (periph != NULL) {
1750229997Sken		/* We've already got a periph, no need to alloc a new one. */
1751260387Sscottl		xpt_path_unlock(path);
1752229997Sken		xpt_free_path(path);
1753229997Sken		free(softc, M_CTLFE);
1754229997Sken		return (0);
1755229997Sken	}
1756229997Sken	softc->parent_softc = bus_softc;
1757229997Sken
1758229997Sken	status = cam_periph_alloc(ctlferegister,
1759229997Sken				  ctlfeoninvalidate,
1760229997Sken				  ctlfecleanup,
1761229997Sken				  ctlfestart,
1762229997Sken				  "ctl",
1763229997Sken				  CAM_PERIPH_BIO,
1764229997Sken				  path,
1765229997Sken				  ctlfeasync,
1766229997Sken				  0,
1767229997Sken				  softc);
1768229997Sken
1769274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1770274388Smav		const struct cam_status_entry *entry;
1771274388Smav
1772274388Smav		entry = cam_fetch_status_entry(status);
1773274388Smav		printf("%s: CAM error %s (%#x) returned from "
1774274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1775274388Smav		       entry->status_text : "Unknown", status);
1776274388Smav		free(softc, M_CTLFE);
1777274388Smav	}
1778274388Smav
1779260387Sscottl	xpt_path_unlock(path);
1780244016Sken	xpt_free_path(path);
1781229997Sken	return (0);
1782229997Sken}
1783229997Sken
1784229997Sken/*
1785245228Sken * This will get called when the user removes a LUN to disable that LUN
1786245228Sken * on every bus that is attached to CTL.
1787229997Sken */
1788229997Skenstatic int
1789284798Smavctlfe_lun_disable(void *arg, int lun_id)
1790229997Sken{
1791229997Sken	struct ctlfe_softc *softc;
1792229997Sken	struct ctlfe_lun_softc *lun_softc;
1793229997Sken
1794229997Sken	softc = (struct ctlfe_softc *)arg;
1795290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1796290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1797229997Sken
1798260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1799229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1800229997Sken		struct cam_path *path;
1801229997Sken
1802229997Sken		path = lun_softc->periph->path;
1803229997Sken
1804290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1805229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1806229997Sken			break;
1807229997Sken		}
1808229997Sken	}
1809229997Sken	if (lun_softc == NULL) {
1810260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1811284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1812229997Sken		return (1);
1813229997Sken	}
1814260387Sscottl	cam_periph_acquire(lun_softc->periph);
1815260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1816229997Sken
1817260387Sscottl	cam_periph_lock(lun_softc->periph);
1818229997Sken	cam_periph_invalidate(lun_softc->periph);
1819260387Sscottl	cam_periph_unlock(lun_softc->periph);
1820260387Sscottl	cam_periph_release(lun_softc->periph);
1821229997Sken	return (0);
1822229997Sken}
1823229997Sken
1824229997Skenstatic void
1825229997Skenctlfe_dump_sim(struct cam_sim *sim)
1826229997Sken{
1827229997Sken
1828315939Smav	printf("%s%d: max dev openings: %d, max tagged dev openings: %d\n",
1829315939Smav	    sim->sim_name, sim->unit_number, sim->max_dev_openings,
1830315939Smav	    sim->max_tagged_dev_openings);
1831229997Sken}
1832229997Sken
1833229997Sken/*
1834229997Sken * Assumes that the SIM lock is held.
1835229997Sken */
1836229997Skenstatic void
1837229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1838229997Sken{
1839315939Smav	struct cam_periph *periph = softc->periph;
1840229997Sken	struct ccb_hdr *hdr;
1841315939Smav	struct ccb_getdevstats cgds;
1842229997Sken	int num_items;
1843229997Sken
1844315939Smav	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1845315939Smav	cgds.ccb_h.func_code = XPT_GDEV_STATS;
1846315939Smav	xpt_action((union ccb *)&cgds);
1847315939Smav	if ((cgds.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1848315939Smav		xpt_print(periph->path, "devq: openings %d, active %d, "
1849315939Smav		    "allocated %d, queued %d, held %d\n",
1850315939Smav		    cgds.dev_openings, cgds.dev_active, cgds.allocated,
1851315939Smav		    cgds.queued, cgds.held);
1852315939Smav	}
1853315939Smav
1854229997Sken	num_items = 0;
1855229997Sken
1856315887Smav	STAILQ_FOREACH(hdr, &softc->work_queue, periph_links.stqe) {
1857275880Smav		union ctl_io *io = hdr->io_ptr;
1858229997Sken
1859229997Sken		num_items++;
1860229997Sken
1861229997Sken		/*
1862229997Sken		 * Only regular SCSI I/O is put on the work
1863229997Sken		 * queue, so we can print sense here.  There may be no
1864229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1865229997Sken		 * print out the CCB as well.
1866229997Sken		 *
1867229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1868229997Sken		 * CTL is merged in with CAM.
1869229997Sken		 */
1870229997Sken		ctl_io_error_print(io, NULL);
1871229997Sken
1872229997Sken		/*
1873275880Smav		 * Print DMA status if we are DMA_QUEUED.
1874229997Sken		 */
1875275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1876275880Smav			xpt_print(periph->path,
1877275880Smav			    "Total %u, Current %u, Resid %u\n",
1878275880Smav			    io->scsiio.kern_total_len,
1879275880Smav			    io->scsiio.kern_data_len,
1880275880Smav			    io->scsiio.kern_data_resid);
1881275880Smav		}
1882229997Sken	}
1883229997Sken
1884314749Smav	xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1885314749Smav	xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1886229997Sken}
1887229997Sken
1888229997Sken/*
1889275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1890275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1891275880Smav * to the adapter.
1892229997Sken */
1893229997Skenstatic void
1894275880Smavctlfe_datamove(union ctl_io *io)
1895229997Sken{
1896275880Smav	union ccb *ccb;
1897275880Smav	struct cam_periph *periph;
1898229997Sken	struct ctlfe_lun_softc *softc;
1899229997Sken
1900275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1901275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1902229997Sken
1903313365Smav	io->scsiio.ext_data_filled = 0;
1904312585Smav	ccb = PRIV_CCB(io);
1905275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
1906275880Smav	cam_periph_lock(periph);
1907275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1908275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1909275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1910275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1911315887Smav	STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1912315887Smav			  periph_links.stqe);
1913314749Smav	xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1914275880Smav	cam_periph_unlock(periph);
1915229997Sken}
1916229997Sken
1917229997Skenstatic void
1918275880Smavctlfe_done(union ctl_io *io)
1919229997Sken{
1920229997Sken	union ccb *ccb;
1921229997Sken	struct cam_periph *periph;
1922229997Sken	struct ctlfe_lun_softc *softc;
1923229997Sken
1924312585Smav	ccb = PRIV_CCB(io);
1925229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
1926260387Sscottl	cam_periph_lock(periph);
1927229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1928229997Sken
1929229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
1930229997Sken		/*
1931229997Sken		 * Send the notify acknowledge down to the SIM, to let it
1932229997Sken		 * know we processed the task management command.
1933229997Sken		 */
1934229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
1935229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1936304417Smav		switch (io->taskio.task_status) {
1937304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
1938304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1939304417Smav			break;
1940304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
1941304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1942304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1943304417Smav			break;
1944304417Smav		case CTL_TASK_FUNCTION_REJECTED:
1945304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1946304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1947304417Smav			break;
1948304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
1949304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1950304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1951304417Smav			break;
1952304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1953304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1954304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1955304417Smav			break;
1956304417Smav		}
1957304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1958229997Sken		xpt_action(ccb);
1959275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1960314739Smav		ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1961314739Smav		return;
1962229997Sken	} else {
1963275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1964315887Smav		STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1965315887Smav				  periph_links.stqe);
1966314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1967229997Sken	}
1968229997Sken
1969260387Sscottl	cam_periph_unlock(periph);
1970229997Sken}
1971229997Sken
1972229997Skenstatic void
1973229997Skenctlfe_dump(void)
1974229997Sken{
1975229997Sken	struct ctlfe_softc *bus_softc;
1976275880Smav	struct ctlfe_lun_softc *lun_softc;
1977229997Sken
1978229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1979229997Sken		ctlfe_dump_sim(bus_softc->sim);
1980275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1981229997Sken			ctlfe_dump_queue(lun_softc);
1982229997Sken	}
1983229997Sken}
1984