scsi_ctl.c revision 315887
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 315887 2017-03-24 07:00:16Z 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;
109229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
110229997Sken};
111229997Sken
112229997Skentypedef enum {
113229997Sken	CTLFE_CMD_NONE		= 0x00,
114229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
115229997Sken} ctlfe_cmd_flags;
116229997Sken
117288723Smavstruct ctlfe_cmd_info {
118229997Sken	int cur_transfer_index;
119265641Smav	size_t cur_transfer_off;
120229997Sken	ctlfe_cmd_flags flags;
121229997Sken	/*
122229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
123229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
124229997Sken	 * i386 and 512 bytes on amd64.
125229997Sken	 */
126265641Smav#define CTLFE_MAX_SEGS	32
127265641Smav	bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
128229997Sken};
129229997Sken
130229997Sken/*
131229997Sken * When we register the adapter/bus, request that this many ctl_ios be
132229997Sken * allocated.  This should be the maximum supported by the adapter, but we
133229997Sken * currently don't have a way to get that back from the path inquiry.
134229997Sken * XXX KDM add that to the path inquiry.
135229997Sken */
136229997Sken#define	CTLFE_REQ_CTL_IO	4096
137229997Sken/*
138229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
139229997Sken * adapter per LUN.
140229997Sken * XXX KDM should this be controlled by CTL?
141229997Sken */
142229997Sken#define	CTLFE_ATIO_PER_LUN	1024
143229997Sken/*
144229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
145229997Sken * allocate and queue down to the adapter per LUN.
146229997Sken * XXX KDM should this be controlled by CTL?
147229997Sken */
148229997Sken#define	CTLFE_IN_PER_LUN	1024
149229997Sken
150229997Sken/*
151314751Smav * Timeout (in seconds) on CTIO CCB doing DMA or sending status
152229997Sken */
153314751Smav#define	CTLFE_TIMEOUT	5
154229997Sken
155229997Sken/*
156229997Sken * Turn this on to enable extra debugging prints.
157229997Sken */
158229997Sken#if 0
159229997Sken#define	CTLFE_DEBUG
160229997Sken#endif
161229997Sken
162229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
163229997Sken
164275878Smav#define	io_ptr		ppriv_ptr0
165229997Sken
166229997Sken/* This is only used in the CTIO */
167229997Sken#define	ccb_atio	ppriv_ptr1
168229997Sken
169312585Smav#define PRIV_CCB(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
170312585Smav#define PRIV_INFO(io)	((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
171312585Smav
172313369Smavstatic int		ctlfeinitialize(void);
173313369Smavstatic int		ctlfeshutdown(void);
174268677Smavstatic periph_init_t	ctlfeperiphinit;
175229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
176229997Sken				   struct cam_path *path, void *arg);
177229997Skenstatic periph_ctor_t	ctlferegister;
178229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
179229997Skenstatic periph_dtor_t	ctlfecleanup;
180229997Skenstatic periph_start_t	ctlfestart;
181229997Skenstatic void		ctlfedone(struct cam_periph *periph,
182229997Sken				  union ccb *done_ccb);
183229997Sken
184229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
185229997Skenstatic void 		ctlfe_online(void *arg);
186229997Skenstatic void 		ctlfe_offline(void *arg);
187284798Smavstatic int 		ctlfe_lun_enable(void *arg, int lun_id);
188284798Smavstatic int 		ctlfe_lun_disable(void *arg, int lun_id);
189229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
190229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
191275880Smavstatic void 		ctlfe_datamove(union ctl_io *io);
192275880Smavstatic void 		ctlfe_done(union ctl_io *io);
193229997Skenstatic void 		ctlfe_dump(void);
194314739Smavstatic void		ctlfe_free_ccb(struct cam_periph *periph,
195314739Smav			    union ccb *ccb);
196314739Smavstatic void		ctlfe_requeue_ccb(struct cam_periph *periph,
197314739Smav			    union ccb *ccb, int unlock);
198229997Sken
199229997Skenstatic struct periph_driver ctlfe_driver =
200229997Sken{
201268677Smav	ctlfeperiphinit, "ctl",
202273316Smav	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
203273316Smav	CAM_PERIPH_DRV_EARLY
204229997Sken};
205229997Sken
206268677Smavstatic struct ctl_frontend ctlfe_frontend =
207268677Smav{
208273318Smav	.name = "camtgt",
209268677Smav	.init = ctlfeinitialize,
210268677Smav	.fe_dump = ctlfe_dump,
211268677Smav	.shutdown = ctlfeshutdown,
212249009Strasz};
213268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
214249009Strasz
215313369Smavstatic int
216229997Skenctlfeshutdown(void)
217229997Sken{
218313369Smav
219313369Smav	/* CAM does not support periph driver unregister now. */
220313369Smav	return (EBUSY);
221229997Sken}
222229997Sken
223313369Smavstatic int
224268677Smavctlfeinitialize(void)
225229997Sken{
226229997Sken
227229997Sken	STAILQ_INIT(&ctlfe_softc_list);
228229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
229268677Smav	periphdriver_register(&ctlfe_driver);
230268677Smav	return (0);
231268677Smav}
232229997Sken
233313369Smavstatic void
234268677Smavctlfeperiphinit(void)
235268677Smav{
236268677Smav	cam_status status;
237229997Sken
238229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
239229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
240229997Sken	if (status != CAM_REQ_CMP) {
241229997Sken		printf("ctl: Failed to attach async callback due to CAM "
242229997Sken		       "status 0x%x!\n", status);
243229997Sken	}
244229997Sken}
245229997Sken
246229997Skenstatic void
247229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
248229997Sken{
249273317Smav	struct ctlfe_softc *softc;
250229997Sken
251229997Sken#ifdef CTLFEDEBUG
252229997Sken	printf("%s: entered\n", __func__);
253229997Sken#endif
254229997Sken
255273317Smav	mtx_lock(&ctlfe_list_mtx);
256273317Smav	STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
257273317Smav		if (softc->path_id == xpt_path_path_id(path))
258273317Smav			break;
259273317Smav	}
260273317Smav	mtx_unlock(&ctlfe_list_mtx);
261273317Smav
262229997Sken	/*
263229997Sken	 * When a new path gets registered, and it is capable of target
264229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
265229997Sken	 * selective, but for now this will be sufficient.
266229997Sken 	 */
267229997Sken	switch (code) {
268229997Sken	case AC_PATH_REGISTERED: {
269268677Smav		struct ctl_port *port;
270229997Sken		struct ccb_pathinq *cpi;
271229997Sken		int retval;
272229997Sken
273229997Sken		cpi = (struct ccb_pathinq *)arg;
274229997Sken
275229997Sken		/* Don't attach if it doesn't support target mode */
276229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
277230033Sken#ifdef CTLFEDEBUG
278229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
279229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
280230033Sken#endif
281229997Sken			break;
282229997Sken		}
283229997Sken
284273317Smav		if (softc != NULL) {
285273317Smav#ifdef CTLFEDEBUG
286273317Smav			printf("%s: CTL port for CAM path %u already exists\n",
287273317Smav			       __func__, xpt_path_path_id(path));
288273317Smav#endif
289273317Smav			break;
290273317Smav		}
291273317Smav
292229997Sken		/*
293229997Sken		 * We're in an interrupt context here, so we have to
294229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
295229997Sken		 * can't allocate memory.
296229997Sken		 */
297273317Smav		softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
298273317Smav		if (softc == NULL) {
299229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
300273317Smav			       __func__, sizeof(*softc));
301229997Sken			return;
302229997Sken		}
303229997Sken
304273317Smav		softc->path_id = cpi->ccb_h.path_id;
305288713Smav		softc->target_id = cpi->initiator_id;
306273317Smav		softc->sim = xpt_path_sim(path);
307290776Smav		softc->hba_misc = cpi->hba_misc;
308265641Smav		if (cpi->maxio != 0)
309273317Smav			softc->maxio = cpi->maxio;
310265641Smav		else
311273317Smav			softc->maxio = DFLTPHYS;
312273317Smav		mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
313273317Smav		STAILQ_INIT(&softc->lun_softc_list);
314229997Sken
315273317Smav		port = &softc->port;
316268677Smav		port->frontend = &ctlfe_frontend;
317229997Sken
318229997Sken		/*
319229997Sken		 * XXX KDM should we be more accurate here ?
320229997Sken		 */
321229997Sken		if (cpi->transport == XPORT_FC)
322268677Smav			port->port_type = CTL_PORT_FC;
323268694Smav		else if (cpi->transport == XPORT_SAS)
324268694Smav			port->port_type = CTL_PORT_SAS;
325229997Sken		else
326268677Smav			port->port_type = CTL_PORT_SCSI;
327229997Sken
328229997Sken		/* XXX KDM what should the real number be here? */
329314751Smav		port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
330273317Smav		snprintf(softc->port_name, sizeof(softc->port_name),
331229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
332229997Sken		/*
333229997Sken		 * XXX KDM it would be nice to allocate storage in the
334229997Sken		 * frontend structure itself.
335229997Sken	 	 */
336273317Smav		port->port_name = softc->port_name;
337273319Smav		port->physical_port = cpi->bus_id;
338273319Smav		port->virtual_port = 0;
339268677Smav		port->port_online = ctlfe_online;
340268677Smav		port->port_offline = ctlfe_offline;
341273317Smav		port->onoff_arg = softc;
342268677Smav		port->lun_enable = ctlfe_lun_enable;
343268677Smav		port->lun_disable = ctlfe_lun_disable;
344273317Smav		port->targ_lun_arg = softc;
345275880Smav		port->fe_datamove = ctlfe_datamove;
346275880Smav		port->fe_done = ctlfe_done;
347229997Sken		/*
348229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
349229997Sken		 * number of targets supported.
350229997Sken		 */
351268677Smav		port->max_targets = cpi->max_target;
352268677Smav		port->max_target_id = cpi->max_target;
353288732Smav		port->targ_port = -1;
354314751Smav
355275493Smav		retval = ctl_port_register(port);
356229997Sken		if (retval != 0) {
357268677Smav			printf("%s: ctl_port_register() failed with "
358229997Sken			       "error %d!\n", __func__, retval);
359273317Smav			mtx_destroy(&softc->lun_softc_mtx);
360273317Smav			free(softc, M_CTLFE);
361229997Sken			break;
362229997Sken		} else {
363229997Sken			mtx_lock(&ctlfe_list_mtx);
364273317Smav			STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
365229997Sken			mtx_unlock(&ctlfe_list_mtx);
366229997Sken		}
367229997Sken
368245228Sken		break;
369245228Sken	}
370245228Sken	case AC_PATH_DEREGISTERED: {
371245228Sken
372245228Sken		if (softc != NULL) {
373245228Sken			/*
374245228Sken			 * XXX KDM are we certain at this point that there
375245228Sken			 * are no outstanding commands for this frontend?
376245228Sken			 */
377273317Smav			mtx_lock(&ctlfe_list_mtx);
378273317Smav			STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
379273317Smav			    links);
380273317Smav			mtx_unlock(&ctlfe_list_mtx);
381268677Smav			ctl_port_deregister(&softc->port);
382260387Sscottl			mtx_destroy(&softc->lun_softc_mtx);
383245228Sken			free(softc, M_CTLFE);
384229997Sken		}
385229997Sken		break;
386229997Sken	}
387229997Sken	case AC_CONTRACT: {
388229997Sken		struct ac_contract *ac;
389229997Sken
390229997Sken		ac = (struct ac_contract *)arg;
391229997Sken
392229997Sken		switch (ac->contract_number) {
393229997Sken		case AC_CONTRACT_DEV_CHG: {
394229997Sken			struct ac_device_changed *dev_chg;
395273317Smav			int retval;
396229997Sken
397229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
398229997Sken
399236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
400229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
401229997Sken			       xpt_path_path_id(path), dev_chg->target,
402229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
403229997Sken
404273317Smav			if (softc == NULL) {
405229997Sken				printf("%s: CTL port for CAM path %u not "
406229997Sken				       "found!\n", __func__,
407229997Sken				       xpt_path_path_id(path));
408229997Sken				break;
409229997Sken			}
410229997Sken			if (dev_chg->arrived != 0) {
411268692Smav				retval = ctl_add_initiator(&softc->port,
412268692Smav				    dev_chg->target, dev_chg->wwpn, NULL);
413229997Sken			} else {
414268692Smav				retval = ctl_remove_initiator(&softc->port,
415268692Smav				    dev_chg->target);
416229997Sken			}
417229997Sken
418268692Smav			if (retval < 0) {
419229997Sken				printf("%s: could not %s port %d iid %u "
420229997Sken				       "WWPN %#jx!\n", __func__,
421229997Sken				       (dev_chg->arrived != 0) ? "add" :
422268677Smav				       "remove", softc->port.targ_port,
423229997Sken				       dev_chg->target,
424229997Sken				       (uintmax_t)dev_chg->wwpn);
425229997Sken			}
426229997Sken			break;
427229997Sken		}
428229997Sken		default:
429229997Sken			printf("%s: unsupported contract number %ju\n",
430229997Sken			       __func__, (uintmax_t)ac->contract_number);
431229997Sken			break;
432229997Sken		}
433229997Sken		break;
434229997Sken	}
435229997Sken	default:
436229997Sken		break;
437229997Sken	}
438229997Sken}
439229997Sken
440229997Skenstatic cam_status
441229997Skenctlferegister(struct cam_periph *periph, void *arg)
442229997Sken{
443229997Sken	struct ctlfe_softc *bus_softc;
444229997Sken	struct ctlfe_lun_softc *softc;
445229997Sken	union ccb en_lun_ccb;
446229997Sken	cam_status status;
447229997Sken	int i;
448229997Sken
449229997Sken	softc = (struct ctlfe_lun_softc *)arg;
450229997Sken	bus_softc = softc->parent_softc;
451229997Sken
452315887Smav	STAILQ_INIT(&softc->work_queue);
453229997Sken	softc->periph = periph;
454229997Sken	periph->softc = softc;
455229997Sken
456242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
457229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
458229997Sken	en_lun_ccb.cel.grp6_len = 0;
459229997Sken	en_lun_ccb.cel.grp7_len = 0;
460229997Sken	en_lun_ccb.cel.enable = 1;
461229997Sken	xpt_action(&en_lun_ccb);
462229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
463229997Sken	if (status != CAM_REQ_CMP) {
464229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
465229997Sken			  __func__, en_lun_ccb.ccb_h.status);
466229997Sken		return (status);
467229997Sken	}
468229997Sken
469229997Sken	status = CAM_REQ_CMP;
470229997Sken
471229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
472229997Sken		union ccb *new_ccb;
473275878Smav		union ctl_io *new_io;
474288723Smav		struct ctlfe_cmd_info *cmd_info;
475229997Sken
476229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
477236426Smjacob					      M_ZERO|M_NOWAIT);
478229997Sken		if (new_ccb == NULL) {
479229997Sken			status = CAM_RESRC_UNAVAIL;
480229997Sken			break;
481229997Sken		}
482275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
483275878Smav		if (new_io == NULL) {
484275878Smav			free(new_ccb, M_CTLFE);
485275878Smav			status = CAM_RESRC_UNAVAIL;
486275878Smav			break;
487275878Smav		}
488288723Smav		cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
489288723Smav		    M_ZERO | M_NOWAIT);
490288723Smav		if (cmd_info == NULL) {
491288723Smav			ctl_free_io(new_io);
492288723Smav			free(new_ccb, M_CTLFE);
493288723Smav			status = CAM_RESRC_UNAVAIL;
494288723Smav			break;
495288723Smav		}
496312585Smav		PRIV_INFO(new_io) = cmd_info;
497284793Smav		softc->atios_alloced++;
498275878Smav		new_ccb->ccb_h.io_ptr = new_io;
499275878Smav
500229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
501229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
502229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
503260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
504229997Sken		xpt_action(new_ccb);
505229997Sken		status = new_ccb->ccb_h.status;
506229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
507288723Smav			free(cmd_info, M_CTLFE);
508275878Smav			ctl_free_io(new_io);
509229997Sken			free(new_ccb, M_CTLFE);
510229997Sken			break;
511229997Sken		}
512229997Sken	}
513229997Sken
514229997Sken	status = cam_periph_acquire(periph);
515229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
516229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
517229997Sken			  "count, status = %#x\n", __func__, status);
518229997Sken		return (status);
519229997Sken	}
520229997Sken
521229997Sken	if (i == 0) {
522229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
523229997Sken			  "status 0x%x\n", __func__, status);
524229997Sken		return (CAM_REQ_CMP_ERR);
525229997Sken	}
526229997Sken
527229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
528229997Sken		union ccb *new_ccb;
529275878Smav		union ctl_io *new_io;
530229997Sken
531229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
532236426Smjacob					      M_ZERO|M_NOWAIT);
533229997Sken		if (new_ccb == NULL) {
534229997Sken			status = CAM_RESRC_UNAVAIL;
535229997Sken			break;
536229997Sken		}
537275878Smav		new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
538275878Smav		if (new_io == NULL) {
539275878Smav			free(new_ccb, M_CTLFE);
540275878Smav			status = CAM_RESRC_UNAVAIL;
541275878Smav			break;
542275878Smav		}
543284793Smav		softc->inots_alloced++;
544275878Smav		new_ccb->ccb_h.io_ptr = new_io;
545229997Sken
546229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
547229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
548229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
549260387Sscottl		new_ccb->ccb_h.flags |= CAM_UNLOCKED;
550229997Sken		xpt_action(new_ccb);
551229997Sken		status = new_ccb->ccb_h.status;
552237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
553237601Sken			/*
554237601Sken			 * Note that we don't free the CCB here.  If the
555237601Sken			 * status is not CAM_REQ_INPROG, then we're
556237601Sken			 * probably talking to a SIM that says it is
557237601Sken			 * target-capable but doesn't support the
558237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
559237601Sken			 * older API.  In that case, it'll call xpt_done()
560237601Sken			 * on the CCB, and we need to free it in our done
561237601Sken			 * routine as a result.
562237601Sken			 */
563229997Sken			break;
564229997Sken		}
565229997Sken	}
566237601Sken	if ((i == 0)
567237601Sken	 || (status != CAM_REQ_INPROG)) {
568229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
569229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
570229997Sken		return (CAM_REQ_CMP_ERR);
571229997Sken	}
572275882Smav	mtx_lock(&bus_softc->lun_softc_mtx);
573275882Smav	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
574275882Smav	mtx_unlock(&bus_softc->lun_softc_mtx);
575229997Sken	return (CAM_REQ_CMP);
576229997Sken}
577229997Sken
578229997Skenstatic void
579229997Skenctlfeoninvalidate(struct cam_periph *periph)
580229997Sken{
581229997Sken	union ccb en_lun_ccb;
582229997Sken	cam_status status;
583260387Sscottl	struct ctlfe_softc *bus_softc;
584229997Sken	struct ctlfe_lun_softc *softc;
585229997Sken
586229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
587229997Sken
588242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
589229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
590229997Sken	en_lun_ccb.cel.grp6_len = 0;
591229997Sken	en_lun_ccb.cel.grp7_len = 0;
592229997Sken	en_lun_ccb.cel.enable = 0;
593229997Sken	xpt_action(&en_lun_ccb);
594229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
595229997Sken	if (status != CAM_REQ_CMP) {
596229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
597229997Sken			  __func__, en_lun_ccb.ccb_h.status);
598229997Sken		/*
599229997Sken		 * XXX KDM what do we do now?
600229997Sken		 */
601229997Sken	}
602260387Sscottl
603260387Sscottl	bus_softc = softc->parent_softc;
604260387Sscottl	mtx_lock(&bus_softc->lun_softc_mtx);
605260387Sscottl	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
606260387Sscottl	mtx_unlock(&bus_softc->lun_softc_mtx);
607229997Sken}
608229997Sken
609229997Skenstatic void
610229997Skenctlfecleanup(struct cam_periph *periph)
611229997Sken{
612229997Sken	struct ctlfe_lun_softc *softc;
613229997Sken
614229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
615229997Sken
616314749Smav	KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
617314749Smav	    __func__, softc->ctios_sent));
618314749Smav	KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
619314749Smav	    __func__, softc->refcount));
620314749Smav	KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
621314749Smav	    __func__, softc->atios_alloced));
622314749Smav	KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
623314749Smav	    __func__, softc->inots_alloced));
624245228Sken
625229997Sken	free(softc, M_CTLFE);
626229997Sken}
627229997Sken
628229997Skenstatic void
629265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
630265641Smav    ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
631265641Smav    u_int16_t *sglist_cnt)
632265641Smav{
633265641Smav	struct ctlfe_softc *bus_softc;
634288723Smav	struct ctlfe_cmd_info *cmd_info;
635265641Smav	struct ctl_sg_entry *ctl_sglist;
636265641Smav	bus_dma_segment_t *cam_sglist;
637265641Smav	size_t off;
638265641Smav	int i, idx;
639265641Smav
640312585Smav	cmd_info = PRIV_INFO(io);
641265641Smav	bus_softc = softc->parent_softc;
642265641Smav
643265641Smav	/*
644265641Smav	 * Set the direction, relative to the initiator.
645265641Smav	 */
646265641Smav	*flags &= ~CAM_DIR_MASK;
647265641Smav	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
648265641Smav		*flags |= CAM_DIR_IN;
649265641Smav	else
650265641Smav		*flags |= CAM_DIR_OUT;
651265641Smav
652265641Smav	*flags &= ~CAM_DATA_MASK;
653265641Smav	idx = cmd_info->cur_transfer_index;
654265641Smav	off = cmd_info->cur_transfer_off;
655265641Smav	cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
656313365Smav	if (io->scsiio.kern_sg_entries == 0) {	/* No S/G list. */
657313365Smav
658313365Smav		/* One time shift for SRR offset. */
659313365Smav		off += io->scsiio.ext_data_filled;
660313365Smav		io->scsiio.ext_data_filled = 0;
661313365Smav
662265641Smav		*data_ptr = io->scsiio.kern_data_ptr + off;
663265641Smav		if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
664265641Smav			*dxfer_len = io->scsiio.kern_data_len - off;
665265641Smav		} else {
666265641Smav			*dxfer_len = bus_softc->maxio;
667313365Smav			cmd_info->cur_transfer_off += bus_softc->maxio;
668265641Smav			cmd_info->flags |= CTLFE_CMD_PIECEWISE;
669265641Smav		}
670265641Smav		*sglist_cnt = 0;
671265641Smav
672265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
673265641Smav			*flags |= CAM_DATA_PADDR;
674265641Smav		else
675265641Smav			*flags |= CAM_DATA_VADDR;
676313365Smav	} else {	/* S/G list with physical or virtual pointers. */
677265641Smav		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
678313365Smav
679313365Smav		/* One time shift for SRR offset. */
680313365Smav		while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
681313365Smav			io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
682313365Smav			idx++;
683313365Smav			off = 0;
684313365Smav		}
685313365Smav		off += io->scsiio.ext_data_filled;
686313365Smav		io->scsiio.ext_data_filled = 0;
687313365Smav
688265641Smav		cam_sglist = cmd_info->cam_sglist;
689265641Smav		*dxfer_len = 0;
690265641Smav		for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
691265641Smav			cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
692265641Smav			if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
693265641Smav				cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
694265641Smav				*dxfer_len += cam_sglist[i].ds_len;
695265641Smav			} else {
696265641Smav				cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
697265641Smav				cmd_info->cur_transfer_index = idx + i;
698265641Smav				cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
699265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
700265641Smav				*dxfer_len += cam_sglist[i].ds_len;
701265641Smav				if (ctl_sglist[i].len != 0)
702265641Smav					i++;
703265641Smav				break;
704265641Smav			}
705265641Smav			if (i == (CTLFE_MAX_SEGS - 1) &&
706265641Smav			    idx + i < (io->scsiio.kern_sg_entries - 1)) {
707265641Smav				cmd_info->cur_transfer_index = idx + i + 1;
708265641Smav				cmd_info->cur_transfer_off = 0;
709265641Smav				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
710265641Smav				i++;
711265641Smav				break;
712265641Smav			}
713265641Smav			off = 0;
714265641Smav		}
715265641Smav		*sglist_cnt = i;
716265641Smav		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
717265641Smav			*flags |= CAM_DATA_SG_PADDR;
718265641Smav		else
719265641Smav			*flags |= CAM_DATA_SG;
720265641Smav		*data_ptr = (uint8_t *)cam_sglist;
721265641Smav	}
722265641Smav}
723265641Smav
724265641Smavstatic void
725229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
726229997Sken{
727229997Sken	struct ctlfe_lun_softc *softc;
728288723Smav	struct ctlfe_cmd_info *cmd_info;
729229997Sken	struct ccb_hdr *ccb_h;
730275880Smav	struct ccb_accept_tio *atio;
731275880Smav	struct ccb_scsiio *csio;
732275880Smav	uint8_t *data_ptr;
733275880Smav	uint32_t dxfer_len;
734275880Smav	ccb_flags flags;
735275880Smav	union ctl_io *io;
736275880Smav	uint8_t scsi_status;
737229997Sken
738229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
739229997Sken
740314739Smavnext:
741315887Smav	/* Take the ATIO off the work queue */
742315887Smav	ccb_h = STAILQ_FIRST(&softc->work_queue);
743260387Sscottl	if (ccb_h == NULL) {
744229997Sken		xpt_release_ccb(start_ccb);
745275880Smav		return;
746275880Smav	}
747315887Smav	STAILQ_REMOVE_HEAD(&softc->work_queue, periph_links.stqe);
748275880Smav	atio = (struct ccb_accept_tio *)ccb_h;
749275880Smav	io = (union ctl_io *)ccb_h->io_ptr;
750275880Smav	csio = &start_ccb->csio;
751229997Sken
752275880Smav	flags = atio->ccb_h.flags &
753275880Smav		(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
754312585Smav	cmd_info = PRIV_INFO(io);
755275881Smav	cmd_info->cur_transfer_index = 0;
756275881Smav	cmd_info->cur_transfer_off = 0;
757275881Smav	cmd_info->flags = 0;
758229997Sken
759275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
760275880Smav		/*
761275880Smav		 * Datamove call, we need to setup the S/G list.
762275880Smav		 */
763275880Smav		ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
764275880Smav		    &csio->sglist_cnt);
765275880Smav	} else {
766275880Smav		/*
767275880Smav		 * We're done, send status back.
768275880Smav		 */
769275880Smav		if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
770275880Smav		    (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
771275880Smav			io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
772275880Smav
773314737Smav			/* Tell the SIM that we've aborted this ATIO */
774314737Smav#ifdef CTLFEDEBUG
775314737Smav			printf("%s: tag %04x abort\n", __func__, atio->tag_id);
776314737Smav#endif
777314737Smav			KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
778314737Smav			    ("func_code %#x is not ATIO", atio->ccb_h.func_code));
779275880Smav			start_ccb->ccb_h.func_code = XPT_ABORT;
780275880Smav			start_ccb->cab.abort_ccb = (union ccb *)atio;
781275880Smav			xpt_action(start_ccb);
782229997Sken
783314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
784314739Smav			    /* unlock */0);
785229997Sken
786314739Smav			/* XPT_ABORT is not queued, so we can take next I/O. */
787314739Smav			goto next;
788275880Smav		}
789275881Smav		data_ptr = NULL;
790275881Smav		dxfer_len = 0;
791275881Smav		csio->sglist_cnt = 0;
792275881Smav	}
793313365Smav	scsi_status = 0;
794275881Smav	if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
795275881Smav	    (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
796275881Smav	    ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
797275881Smav	     io->io_hdr.status == CTL_SUCCESS)) {
798275880Smav		flags |= CAM_SEND_STATUS;
799275880Smav		scsi_status = io->scsiio.scsi_status;
800275880Smav		csio->sense_len = io->scsiio.sense_len;
801275880Smav#ifdef CTLFEDEBUG
802275880Smav		printf("%s: tag %04x status %x\n", __func__,
803275880Smav		       atio->tag_id, io->io_hdr.status);
804275880Smav#endif
805275880Smav		if (csio->sense_len != 0) {
806275880Smav			csio->sense_data = io->scsiio.sense_data;
807275880Smav			flags |= CAM_SEND_SENSE;
808229997Sken		}
809275880Smav	}
810229997Sken
811229997Sken#ifdef CTLFEDEBUG
812275880Smav	printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
813275880Smav	       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
814275880Smav	       atio->tag_id, flags, data_ptr, dxfer_len);
815229997Sken#endif
816229997Sken
817275880Smav	/*
818275880Smav	 * Valid combinations:
819275880Smav	 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
820275880Smav	 *    sglist_cnt = 0
821275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
822275880Smav	 *    sglist_cnt = 0
823275880Smav	 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
824275880Smav	 *    sglist_cnt != 0
825275880Smav	 */
826229997Sken#ifdef CTLFEDEBUG
827275880Smav	if (((flags & CAM_SEND_STATUS)
828275880Smav	  && (((flags & CAM_DATA_SG) != 0)
829275880Smav	   || (dxfer_len != 0)
830275880Smav	   || (csio->sglist_cnt != 0)))
831275880Smav	 || (((flags & CAM_SEND_STATUS) == 0)
832275880Smav	  && (dxfer_len == 0))
833275880Smav	 || ((flags & CAM_DATA_SG)
834275880Smav	  && (csio->sglist_cnt == 0))
835275880Smav	 || (((flags & CAM_DATA_SG) == 0)
836275880Smav	  && (csio->sglist_cnt != 0))) {
837275880Smav		printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
838275880Smav		       "%d sg %u\n", __func__, atio->tag_id,
839312845Smav		       atio_cdb_ptr(atio)[0], flags, dxfer_len,
840275880Smav		       csio->sglist_cnt);
841275880Smav		printf("%s: tag %04x io status %#x\n", __func__,
842275880Smav		       atio->tag_id, io->io_hdr.status);
843275880Smav	}
844229997Sken#endif
845275880Smav	cam_fill_ctio(csio,
846275880Smav		      /*retries*/ 2,
847275880Smav		      ctlfedone,
848275880Smav		      flags,
849275880Smav		      (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
850275880Smav		      atio->tag_id,
851275880Smav		      atio->init_id,
852275880Smav		      scsi_status,
853275880Smav		      /*data_ptr*/ data_ptr,
854275880Smav		      /*dxfer_len*/ dxfer_len,
855314751Smav		      /*timeout*/ CTLFE_TIMEOUT * 1000);
856275880Smav	start_ccb->ccb_h.flags |= CAM_UNLOCKED;
857275880Smav	start_ccb->ccb_h.ccb_atio = atio;
858275880Smav	if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
859275880Smav		io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
860275880Smav	io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
861229997Sken
862275880Smav	softc->ctios_sent++;
863314749Smav	softc->refcount++;
864275880Smav	cam_periph_unlock(periph);
865275880Smav	xpt_action(start_ccb);
866275880Smav	cam_periph_lock(periph);
867314749Smav	softc->refcount--;
868229997Sken
869229997Sken	/*
870275880Smav	 * If we still have work to do, ask for another CCB.
871229997Sken	 */
872315887Smav	if (!STAILQ_EMPTY(&softc->work_queue))
873314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
874229997Sken}
875229997Sken
876229997Skenstatic void
877314749Smavctlfe_drain(void *context, int pending)
878314749Smav{
879314749Smav	struct cam_periph *periph = context;
880314749Smav	struct ctlfe_lun_softc *softc = periph->softc;
881314749Smav
882314749Smav	cam_periph_lock(periph);
883314749Smav	while (softc->refcount != 0) {
884314749Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO,
885314749Smav		    "ctlfe_drain", 1);
886314749Smav	}
887314749Smav	cam_periph_unlock(periph);
888314749Smav	cam_periph_release(periph);
889314749Smav}
890314749Smav
891314749Smavstatic void
892229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
893229997Sken{
894229997Sken	struct ctlfe_lun_softc *softc;
895288723Smav	union ctl_io *io;
896288723Smav	struct ctlfe_cmd_info *cmd_info;
897229997Sken
898229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
899288723Smav	io = ccb->ccb_h.io_ptr;
900229997Sken
901229997Sken	switch (ccb->ccb_h.func_code) {
902229997Sken	case XPT_ACCEPT_TARGET_IO:
903314749Smav		softc->atios_alloced--;
904312585Smav		cmd_info = PRIV_INFO(io);
905288723Smav		free(cmd_info, M_CTLFE);
906229997Sken		break;
907229997Sken	case XPT_IMMEDIATE_NOTIFY:
908229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
909314749Smav		softc->inots_alloced--;
910229997Sken		break;
911229997Sken	default:
912229997Sken		break;
913229997Sken	}
914229997Sken
915288723Smav	ctl_free_io(io);
916229997Sken	free(ccb, M_CTLFE);
917229997Sken
918314749Smav	KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
919314749Smav	    __func__, softc->atios_alloced));
920314749Smav	KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
921314749Smav	    __func__, softc->inots_alloced));
922229997Sken
923229997Sken	/*
924229997Sken	 * If we have received all of our CCBs, we can release our
925229997Sken	 * reference on the peripheral driver.  It will probably go away
926229997Sken	 * now.
927229997Sken	 */
928314749Smav	if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
929314749Smav		if (softc->refcount == 0) {
930314749Smav			cam_periph_release_locked(periph);
931314749Smav		} else {
932314749Smav			TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
933314749Smav			taskqueue_enqueue(taskqueue_thread,
934314749Smav			    &softc->refdrain_task);
935314749Smav		}
936229997Sken	}
937229997Sken}
938229997Sken
939314739Smav/*
940314739Smav * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
941314739Smav */
942314739Smavstatic void
943314739Smavctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
944314739Smav{
945314739Smav	struct ctlfe_lun_softc *softc;
946314740Smav	struct mtx *mtx;
947314739Smav
948314739Smav	if (periph->flags & CAM_PERIPH_INVALID) {
949314740Smav		mtx = cam_periph_mtx(periph);
950314739Smav		ctlfe_free_ccb(periph, ccb);
951314739Smav		if (unlock)
952314740Smav			mtx_unlock(mtx);
953314739Smav		return;
954314739Smav	}
955314739Smav	if (unlock)
956314739Smav		cam_periph_unlock(periph);
957314739Smav
958314739Smav	/*
959314739Smav	 * For a wildcard attachment, commands can come in with a specific
960314739Smav	 * target/lun.  Reset the target and LUN fields back to the wildcard
961314739Smav	 * values before we send them back down to the SIM.
962314739Smav	 */
963314739Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
964314739Smav	if (softc->flags & CTLFE_LUN_WILDCARD) {
965314739Smav		ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
966314739Smav		ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
967314739Smav	}
968314739Smav
969314739Smav	xpt_action(ccb);
970314739Smav}
971314739Smav
972238870Smjacobstatic int
973238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
974238870Smjacob{
975238870Smjacob	uint64_t lba;
976238870Smjacob	uint32_t num_blocks, nbc;
977312845Smav	uint8_t *cmdbyt = atio_cdb_ptr(atio);
978238870Smjacob
979238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
980238870Smjacob
981238870Smjacob	switch (cmdbyt[0]) {
982238870Smjacob	case READ_6:
983238870Smjacob	case WRITE_6:
984238870Smjacob	{
985238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
986238870Smjacob		lba = scsi_3btoul(cdb->addr);
987238870Smjacob		lba &= 0x1fffff;
988238870Smjacob		num_blocks = cdb->length;
989238870Smjacob		if (num_blocks == 0)
990238870Smjacob			num_blocks = 256;
991238870Smjacob		lba += nbc;
992238870Smjacob		num_blocks -= nbc;
993238870Smjacob		scsi_ulto3b(lba, cdb->addr);
994238870Smjacob		cdb->length = num_blocks;
995238870Smjacob		break;
996238870Smjacob	}
997238870Smjacob	case READ_10:
998238870Smjacob	case WRITE_10:
999238870Smjacob	{
1000238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1001238870Smjacob		lba = scsi_4btoul(cdb->addr);
1002238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1003238870Smjacob		lba += nbc;
1004238870Smjacob		num_blocks -= nbc;
1005238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1006238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1007238870Smjacob		break;
1008238870Smjacob	}
1009238870Smjacob	case READ_12:
1010238870Smjacob	case WRITE_12:
1011238870Smjacob	{
1012238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1013238870Smjacob		lba = scsi_4btoul(cdb->addr);
1014238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1015238870Smjacob		lba += nbc;
1016238870Smjacob		num_blocks -= nbc;
1017238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1018238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1019238870Smjacob		break;
1020238870Smjacob	}
1021238870Smjacob	case READ_16:
1022238870Smjacob	case WRITE_16:
1023238870Smjacob	{
1024238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1025238870Smjacob		lba = scsi_8btou64(cdb->addr);
1026238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1027238870Smjacob		lba += nbc;
1028238870Smjacob		num_blocks -= nbc;
1029238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1030238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1031238870Smjacob		break;
1032238870Smjacob	}
1033238870Smjacob	default:
1034238870Smjacob		return -1;
1035238870Smjacob	}
1036238870Smjacob	return (0);
1037238870Smjacob}
1038238870Smjacob
1039229997Skenstatic void
1040229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1041229997Sken{
1042229997Sken	struct ctlfe_lun_softc *softc;
1043229997Sken	struct ctlfe_softc *bus_softc;
1044288723Smav	struct ctlfe_cmd_info *cmd_info;
1045238870Smjacob	struct ccb_accept_tio *atio = NULL;
1046238870Smjacob	union ctl_io *io = NULL;
1047260387Sscottl	struct mtx *mtx;
1048314727Smav	cam_status status;
1049229997Sken
1050260387Sscottl	KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1051260387Sscottl	    ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1052229997Sken#ifdef CTLFE_DEBUG
1053275878Smav	printf("%s: entered, func_code = %#x\n", __func__,
1054275878Smav	       done_ccb->ccb_h.func_code);
1055229997Sken#endif
1056229997Sken
1057284794Smav	/*
1058284794Smav	 * At this point CTL has no known use case for device queue freezes.
1059284794Smav	 * In case some SIM think different -- drop its freeze right here.
1060284794Smav	 */
1061284794Smav	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1062284794Smav		cam_release_devq(periph->path,
1063284794Smav				 /*relsim_flags*/0,
1064284794Smav				 /*reduction*/0,
1065284794Smav				 /*timeout*/0,
1066284794Smav				 /*getcount_only*/0);
1067284794Smav		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1068284794Smav	}
1069284794Smav
1070229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1071229997Sken	bus_softc = softc->parent_softc;
1072260387Sscottl	mtx = cam_periph_mtx(periph);
1073260387Sscottl	mtx_lock(mtx);
1074229997Sken
1075229997Sken	switch (done_ccb->ccb_h.func_code) {
1076229997Sken	case XPT_ACCEPT_TARGET_IO: {
1077229997Sken
1078229997Sken		atio = &done_ccb->atio;
1079314727Smav		status = atio->ccb_h.status & CAM_STATUS_MASK;
1080314727Smav		if (status != CAM_CDB_RECVD) {
1081314727Smav			ctlfe_free_ccb(periph, done_ccb);
1082314727Smav			goto out;
1083314727Smav		}
1084229997Sken
1085238870Smjacob resubmit:
1086229997Sken		/*
1087229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1088229997Sken		 * datamove or done.
1089229997Sken		 */
1090260387Sscottl		mtx_unlock(mtx);
1091275878Smav		io = done_ccb->ccb_h.io_ptr;
1092312585Smav		cmd_info = PRIV_INFO(io);
1093229997Sken		ctl_zero_io(io);
1094229997Sken
1095229997Sken		/* Save pointers on both sides */
1096312585Smav		PRIV_CCB(io) = done_ccb;
1097312585Smav		PRIV_INFO(io) = cmd_info;
1098229997Sken		done_ccb->ccb_h.io_ptr = io;
1099229997Sken
1100229997Sken		/*
1101229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1102229997Sken		 * down the immediate notify path below.
1103229997Sken		 */
1104229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1105288731Smav		io->io_hdr.nexus.initid = atio->init_id;
1106268677Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1107290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1108290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1109290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1110290776Smav		} else {
1111290776Smav			io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1112290776Smav		}
1113229997Sken		io->scsiio.tag_num = atio->tag_id;
1114229997Sken		switch (atio->tag_action) {
1115229997Sken		case CAM_TAG_ACTION_NONE:
1116229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1117229997Sken			break;
1118229997Sken		case MSG_SIMPLE_TASK:
1119229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1120229997Sken			break;
1121229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1122229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1123229997Sken			break;
1124229997Sken		case MSG_ORDERED_TASK:
1125229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1126229997Sken			break;
1127229997Sken		case MSG_ACA_TASK:
1128229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1129229997Sken			break;
1130229997Sken		default:
1131229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1132229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1133229997Sken			       atio->tag_action);
1134229997Sken			break;
1135229997Sken		}
1136229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1137229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1138229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1139229997Sken		}
1140229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1141312845Smav		bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1142229997Sken
1143229997Sken#ifdef CTLFEDEBUG
1144288731Smav		printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1145288731Smav		        io->io_hdr.nexus.initid,
1146229997Sken		        io->io_hdr.nexus.targ_port,
1147229997Sken		        io->io_hdr.nexus.targ_lun,
1148229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1149229997Sken#endif
1150229997Sken
1151229997Sken		ctl_queue(io);
1152260387Sscottl		return;
1153229997Sken	}
1154229997Sken	case XPT_CONT_TARGET_IO: {
1155238870Smjacob		int srr = 0;
1156238870Smjacob		uint32_t srr_off = 0;
1157229997Sken
1158229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1159229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1160229997Sken
1161314749Smav		softc->ctios_sent--;
1162229997Sken#ifdef CTLFEDEBUG
1163229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1164229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1165229997Sken#endif
1166229997Sken		/*
1167238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1168238870Smjacob		 */
1169238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1170238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1171238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1172238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1173238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1174238870Smjacob			srr = 1;
1175238870Smjacob			srr_off =
1176238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1177238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1178238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1179238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1180238870Smjacob		}
1181238870Smjacob
1182313365Smav		/*
1183313365Smav		 * If we have an SRR and we're still sending data, we
1184313365Smav		 * should be able to adjust offsets and cycle again.
1185313365Smav		 * It is possible only if offset is from this datamove.
1186313365Smav		 */
1187313365Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1188313365Smav		    srr_off >= io->scsiio.kern_rel_offset &&
1189313365Smav		    srr_off < io->scsiio.kern_rel_offset +
1190313365Smav		     io->scsiio.kern_data_len) {
1191313365Smav			io->scsiio.kern_data_resid =
1192313365Smav			    io->scsiio.kern_rel_offset +
1193313365Smav			    io->scsiio.kern_data_len - srr_off;
1194313365Smav			io->scsiio.ext_data_filled = srr_off;
1195313365Smav			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1196313365Smav			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1197313365Smav			xpt_release_ccb(done_ccb);
1198315887Smav			STAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1199315887Smav					  periph_links.stqe);
1200314749Smav			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1201313365Smav			break;
1202313365Smav		}
1203313365Smav
1204313365Smav		/*
1205313365Smav		 * If status was being sent, the back end data is now history.
1206313365Smav		 * Hack it up and resubmit a new command with the CDB adjusted.
1207313365Smav		 * If the SIM does the right thing, all of the resid math
1208313365Smav		 * should work.
1209313365Smav		 */
1210275880Smav		if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1211238870Smjacob			xpt_release_ccb(done_ccb);
1212238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1213238870Smjacob				done_ccb = (union ccb *)atio;
1214238870Smjacob				goto resubmit;
1215238870Smjacob			}
1216238870Smjacob			/*
1217238870Smjacob			 * Fall through to doom....
1218238870Smjacob			 */
1219238870Smjacob		}
1220238870Smjacob
1221277919Smav		if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1222277919Smav		    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1223277919Smav			io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1224277919Smav
1225238870Smjacob		/*
1226229997Sken		 * If we were sending status back to the initiator, free up
1227229997Sken		 * resources.  If we were doing a datamove, call the
1228229997Sken		 * datamove done routine.
1229229997Sken		 */
1230275880Smav		if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1231315138Smav			/*
1232315138Smav			 * If we asked to send sense data but it wasn't sent,
1233315138Smav			 * queue the I/O back to CTL for later REQUEST SENSE.
1234315138Smav			 */
1235315138Smav			if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1236315138Smav			    (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1237315138Smav			    (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1238315138Smav			    (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1239315138Smav				PRIV_INFO(io) = PRIV_INFO(
1240315138Smav				    (union ctl_io *)atio->ccb_h.io_ptr);
1241315138Smav				ctl_queue_sense(atio->ccb_h.io_ptr);
1242315138Smav				atio->ccb_h.io_ptr = io;
1243315138Smav			}
1244315138Smav
1245314745Smav			/* Abort ATIO if CTIO sending status has failed. */
1246314745Smav			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1247314745Smav			    CAM_REQ_CMP) {
1248314745Smav				done_ccb->ccb_h.func_code = XPT_ABORT;
1249314745Smav				done_ccb->cab.abort_ccb = (union ccb *)atio;
1250314745Smav				xpt_action(done_ccb);
1251314745Smav			}
1252314745Smav
1253229997Sken			xpt_release_ccb(done_ccb);
1254314739Smav			ctlfe_requeue_ccb(periph, (union ccb *)atio,
1255314739Smav			    /* unlock */1);
1256314739Smav			return;
1257229997Sken		} else {
1258288723Smav			struct ctlfe_cmd_info *cmd_info;
1259229997Sken			struct ccb_scsiio *csio;
1260229997Sken
1261229997Sken			csio = &done_ccb->csio;
1262312585Smav			cmd_info = PRIV_INFO(io);
1263229997Sken
1264229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1265229997Sken
1266229997Sken			/*
1267229997Sken			 * Translate CAM status to CTL status.  Success
1268229997Sken			 * does not change the overall, ctl_io status.  In
1269229997Sken			 * that case we just set port_status to 0.  If we
1270229997Sken			 * have a failure, though, set a data phase error
1271229997Sken			 * for the overall ctl_io.
1272229997Sken			 */
1273229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1274229997Sken			case CAM_REQ_CMP:
1275314757Smav				io->scsiio.kern_data_resid -=
1276314757Smav				    csio->dxfer_len - csio->resid;
1277229997Sken				io->io_hdr.port_status = 0;
1278229997Sken				break;
1279229997Sken			default:
1280229997Sken				/*
1281245228Sken				 * XXX KDM we probably need to figure out a
1282245228Sken				 * standard set of errors that the SIM
1283245228Sken				 * drivers should return in the event of a
1284245228Sken				 * data transfer failure.  A data phase
1285245228Sken				 * error will at least point the user to a
1286245228Sken				 * data transfer error of some sort.
1287245228Sken				 * Hopefully the SIM printed out some
1288245228Sken				 * additional information to give the user
1289245228Sken				 * a clue what happened.
1290229997Sken				 */
1291229997Sken				io->io_hdr.port_status = 0xbad1;
1292229997Sken				ctl_set_data_phase_error(&io->scsiio);
1293229997Sken				/*
1294229997Sken				 * XXX KDM figure out residual.
1295229997Sken				 */
1296229997Sken				break;
1297229997Sken			}
1298229997Sken			/*
1299229997Sken			 * If we had to break this S/G list into multiple
1300229997Sken			 * pieces, figure out where we are in the list, and
1301229997Sken			 * continue sending pieces if necessary.
1302229997Sken			 */
1303314757Smav			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1304314757Smav			    io->io_hdr.port_status == 0 && csio->resid == 0) {
1305229997Sken				ccb_flags flags;
1306229997Sken				uint8_t *data_ptr;
1307229997Sken				uint32_t dxfer_len;
1308229997Sken
1309229997Sken				flags = atio->ccb_h.flags &
1310229997Sken					(CAM_DIS_DISCONNECT|
1311265641Smav					 CAM_TAG_ACTION_VALID);
1312229997Sken
1313265641Smav				ctlfedata(softc, io, &flags, &data_ptr,
1314265641Smav				    &dxfer_len, &csio->sglist_cnt);
1315229997Sken
1316229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1317229997Sken				 && (dxfer_len == 0)) {
1318229997Sken					printf("%s: tag %04x no status or "
1319229997Sken					       "len cdb = %02x\n", __func__,
1320229997Sken					       atio->tag_id,
1321312845Smav					       atio_cdb_ptr(atio)[0]);
1322229997Sken					printf("%s: tag %04x io status %#x\n",
1323229997Sken					       __func__, atio->tag_id,
1324229997Sken					       io->io_hdr.status);
1325229997Sken				}
1326229997Sken
1327229997Sken				cam_fill_ctio(csio,
1328229997Sken					      /*retries*/ 2,
1329229997Sken					      ctlfedone,
1330229997Sken					      flags,
1331229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1332229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1333229997Sken					      atio->tag_id,
1334229997Sken					      atio->init_id,
1335313365Smav					      0,
1336229997Sken					      /*data_ptr*/ data_ptr,
1337229997Sken					      /*dxfer_len*/ dxfer_len,
1338314751Smav					      CTLFE_TIMEOUT * 1000);
1339229997Sken
1340260387Sscottl				csio->ccb_h.flags |= CAM_UNLOCKED;
1341229997Sken				csio->resid = 0;
1342229997Sken				csio->ccb_h.ccb_atio = atio;
1343229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1344229997Sken				softc->ctios_sent++;
1345260387Sscottl				mtx_unlock(mtx);
1346229997Sken				xpt_action((union ccb *)csio);
1347229997Sken			} else {
1348229997Sken				/*
1349229997Sken				 * Release the CTIO.  The ATIO will be sent back
1350229997Sken				 * down to the SIM once we send status.
1351229997Sken				 */
1352229997Sken				xpt_release_ccb(done_ccb);
1353260387Sscottl				mtx_unlock(mtx);
1354229997Sken
1355229997Sken				/* Call the backend move done callback */
1356229997Sken				io->scsiio.be_move_done(io);
1357229997Sken			}
1358260387Sscottl			return;
1359229997Sken		}
1360229997Sken		break;
1361229997Sken	}
1362229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1363229997Sken		union ctl_io *io;
1364229997Sken		struct ccb_immediate_notify *inot;
1365284794Smav		int send_ctl_io;
1366229997Sken
1367229997Sken		inot = &done_ccb->cin1;
1368275878Smav		io = done_ccb->ccb_h.io_ptr;
1369275878Smav		ctl_zero_io(io);
1370229997Sken
1371275878Smav		send_ctl_io = 1;
1372229997Sken
1373275878Smav		io->io_hdr.io_type = CTL_IO_TASK;
1374312585Smav		PRIV_CCB(io) = done_ccb;
1375275878Smav		inot->ccb_h.io_ptr = io;
1376288731Smav		io->io_hdr.nexus.initid = inot->initiator_id;
1377275878Smav		io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1378290776Smav		if (bus_softc->hba_misc & PIM_EXTLUNS) {
1379290776Smav			io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1380290776Smav			    CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1381290776Smav		} else {
1382290776Smav			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1383290776Smav		}
1384275878Smav		/* XXX KDM should this be the tag_id? */
1385275878Smav		io->taskio.tag_num = inot->seq_id;
1386229997Sken
1387275878Smav		status = inot->ccb_h.status & CAM_STATUS_MASK;
1388275878Smav		switch (status) {
1389275878Smav		case CAM_SCSI_BUS_RESET:
1390275878Smav			io->taskio.task_action = CTL_TASK_BUS_RESET;
1391275878Smav			break;
1392275878Smav		case CAM_BDR_SENT:
1393275878Smav			io->taskio.task_action = CTL_TASK_TARGET_RESET;
1394275878Smav			break;
1395275878Smav		case CAM_MESSAGE_RECV:
1396275878Smav			switch (inot->arg) {
1397275878Smav			case MSG_ABORT_TASK_SET:
1398275878Smav				io->taskio.task_action =
1399275878Smav				    CTL_TASK_ABORT_TASK_SET;
1400229997Sken				break;
1401275878Smav			case MSG_TARGET_RESET:
1402290775Smav				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1403229997Sken				break;
1404275878Smav			case MSG_ABORT_TASK:
1405290775Smav				io->taskio.task_action = CTL_TASK_ABORT_TASK;
1406229997Sken				break;
1407275878Smav			case MSG_LOGICAL_UNIT_RESET:
1408290775Smav				io->taskio.task_action = CTL_TASK_LUN_RESET;
1409275878Smav				break;
1410275878Smav			case MSG_CLEAR_TASK_SET:
1411275878Smav				io->taskio.task_action =
1412290775Smav				    CTL_TASK_CLEAR_TASK_SET;
1413275878Smav				break;
1414275878Smav			case MSG_CLEAR_ACA:
1415290775Smav				io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1416290775Smav				break;
1417290775Smav			case MSG_QUERY_TASK:
1418290775Smav				io->taskio.task_action = CTL_TASK_QUERY_TASK;
1419290775Smav				break;
1420290775Smav			case MSG_QUERY_TASK_SET:
1421275878Smav				io->taskio.task_action =
1422290775Smav				    CTL_TASK_QUERY_TASK_SET;
1423275878Smav				break;
1424290775Smav			case MSG_QUERY_ASYNC_EVENT:
1425290775Smav				io->taskio.task_action =
1426290775Smav				    CTL_TASK_QUERY_ASYNC_EVENT;
1427290775Smav				break;
1428275878Smav			case MSG_NOOP:
1429229997Sken				send_ctl_io = 0;
1430229997Sken				break;
1431229997Sken			default:
1432275878Smav				xpt_print(periph->path,
1433314727Smav				    "%s: unsupported INOT message 0x%x\n",
1434314727Smav				    __func__, inot->arg);
1435275878Smav				send_ctl_io = 0;
1436275878Smav				break;
1437275878Smav			}
1438275878Smav			break;
1439314727Smav		default:
1440314727Smav			xpt_print(periph->path,
1441314727Smav			    "%s: unsupported INOT status 0x%x\n",
1442314727Smav			    __func__, status);
1443314727Smav			/* FALLTHROUGH */
1444275878Smav		case CAM_REQ_ABORTED:
1445275878Smav		case CAM_REQ_INVALID:
1446314727Smav		case CAM_DEV_NOT_THERE:
1447275878Smav		case CAM_PROVIDE_FAIL:
1448275878Smav			ctlfe_free_ccb(periph, done_ccb);
1449275878Smav			goto out;
1450275878Smav		}
1451275878Smav		if (send_ctl_io != 0) {
1452275878Smav			ctl_queue(io);
1453229997Sken		} else {
1454229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1455229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1456229997Sken			xpt_action(done_ccb);
1457229997Sken		}
1458229997Sken		break;
1459229997Sken	}
1460229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1461314739Smav		/* Queue this back down to the SIM as an immediate notify. */
1462304417Smav		done_ccb->ccb_h.status = CAM_REQ_INPROG;
1463229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1464314739Smav		ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1465314739Smav		return;
1466229997Sken	case XPT_SET_SIM_KNOB:
1467229997Sken	case XPT_GET_SIM_KNOB:
1468229997Sken		break;
1469229997Sken	default:
1470229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1471229997Sken		      done_ccb->ccb_h.func_code);
1472229997Sken		break;
1473229997Sken	}
1474260387Sscottl
1475260387Sscottlout:
1476260387Sscottl	mtx_unlock(mtx);
1477229997Sken}
1478229997Sken
1479229997Skenstatic void
1480229997Skenctlfe_onoffline(void *arg, int online)
1481229997Sken{
1482229997Sken	struct ctlfe_softc *bus_softc;
1483229997Sken	union ccb *ccb;
1484229997Sken	cam_status status;
1485229997Sken	struct cam_path *path;
1486229997Sken	int set_wwnn;
1487229997Sken
1488229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1489229997Sken
1490229997Sken	set_wwnn = 0;
1491229997Sken
1492229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1493229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1494229997Sken	if (status != CAM_REQ_CMP) {
1495229997Sken		printf("%s: unable to create path!\n", __func__);
1496229997Sken		return;
1497229997Sken	}
1498275882Smav	ccb = xpt_alloc_ccb();
1499242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1500288713Smav	ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1501288713Smav	xpt_action(ccb);
1502229997Sken
1503229997Sken	/*
1504229997Sken	 * Copan WWN format:
1505229997Sken	 *
1506229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1507229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1508229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1509229997Sken	 * Bits 11-8:			Type of port:
1510229997Sken	 *					1 == N-Port
1511229997Sken	 *					2 == F-Port
1512229997Sken	 *					3 == NL-Port
1513229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1514229997Sken	 */
1515229997Sken	if (online != 0) {
1516229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1517229997Sken
1518229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1519229997Sken			       bus_softc->port_name,
1520229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1521229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1522229997Sken			       bus_softc->port_name,
1523229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1524229997Sken
1525229997Sken			/*
1526229997Sken			 * If the user has specified a WWNN/WWPN, send them
1527229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1528229997Sken			 * has reported.
1529229997Sken			 */
1530284586Smav			if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1531284586Smav			    != ccb->knob.xport_specific.fc.wwnn) {
1532229997Sken				ccb->knob.xport_specific.fc.wwnn =
1533284586Smav				    bus_softc->port.wwnn;
1534284586Smav				set_wwnn = 1;
1535284586Smav			} else {
1536284586Smav				ctl_port_set_wwns(&bus_softc->port,
1537284586Smav				    true, ccb->knob.xport_specific.fc.wwnn,
1538284586Smav				    false, 0);
1539284586Smav			}
1540284586Smav			if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1541284586Smav			     != ccb->knob.xport_specific.fc.wwpn) {
1542229997Sken				ccb->knob.xport_specific.fc.wwpn =
1543284586Smav				    bus_softc->port.wwpn;
1544229997Sken				set_wwnn = 1;
1545229997Sken			} else {
1546268683Smav				ctl_port_set_wwns(&bus_softc->port,
1547284586Smav				    false, 0,
1548268683Smav				    true, ccb->knob.xport_specific.fc.wwpn);
1549229997Sken			}
1550229997Sken
1551229997Sken
1552229997Sken			if (set_wwnn != 0) {
1553229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1554229997Sken				       bus_softc->port_name,
1555229997Sken				ccb->knob.xport_specific.fc.wwnn);
1556229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1557229997Sken				       bus_softc->port_name,
1558229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1559229997Sken			}
1560229997Sken		} else {
1561229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1562229997Sken			       bus_softc->port_name);
1563229997Sken		}
1564229997Sken	}
1565229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1566229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1567229997Sken	if (set_wwnn != 0)
1568229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1569229997Sken
1570229997Sken	if (online != 0)
1571288713Smav		ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1572229997Sken	else
1573288713Smav		ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1574229997Sken
1575229997Sken	xpt_action(ccb);
1576229997Sken
1577229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1578229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1579229997Sken		       "status %#x\n",
1580229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1581229997Sken		       (online != 0) ? "enable" : "disable",
1582229997Sken		       ccb->ccb_h.status);
1583229997Sken	} else {
1584229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1585229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1586229997Sken		       (online != 0) ? "enable" : "disable");
1587229997Sken	}
1588229997Sken
1589229997Sken	xpt_free_path(path);
1590275882Smav	xpt_free_ccb(ccb);
1591229997Sken}
1592229997Sken
1593229997Skenstatic void
1594229997Skenctlfe_online(void *arg)
1595229997Sken{
1596245228Sken	struct ctlfe_softc *bus_softc;
1597245228Sken	struct cam_path *path;
1598245228Sken	cam_status status;
1599245228Sken	struct ctlfe_lun_softc *lun_softc;
1600274388Smav	struct cam_periph *periph;
1601245228Sken
1602245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1603245228Sken
1604245228Sken	/*
1605245228Sken	 * Create the wildcard LUN before bringing the port online.
1606245228Sken	 */
1607245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1608245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1609245228Sken				 CAM_LUN_WILDCARD);
1610245228Sken	if (status != CAM_REQ_CMP) {
1611245228Sken		printf("%s: unable to create path for wildcard periph\n",
1612245228Sken				__func__);
1613245228Sken		return;
1614245228Sken	}
1615245228Sken
1616275882Smav	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1617245228Sken
1618260387Sscottl	xpt_path_lock(path);
1619274388Smav	periph = cam_periph_find(path, "ctl");
1620274388Smav	if (periph != NULL) {
1621274388Smav		/* We've already got a periph, no need to alloc a new one. */
1622274388Smav		xpt_path_unlock(path);
1623274388Smav		xpt_free_path(path);
1624274388Smav		free(lun_softc, M_CTLFE);
1625274388Smav		return;
1626274388Smav	}
1627245228Sken	lun_softc->parent_softc = bus_softc;
1628245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1629245228Sken
1630245228Sken	status = cam_periph_alloc(ctlferegister,
1631245228Sken				  ctlfeoninvalidate,
1632245228Sken				  ctlfecleanup,
1633245228Sken				  ctlfestart,
1634245228Sken				  "ctl",
1635245228Sken				  CAM_PERIPH_BIO,
1636245228Sken				  path,
1637245228Sken				  ctlfeasync,
1638245228Sken				  0,
1639245228Sken				  lun_softc);
1640245228Sken
1641245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1642245228Sken		const struct cam_status_entry *entry;
1643245228Sken
1644245228Sken		entry = cam_fetch_status_entry(status);
1645245228Sken		printf("%s: CAM error %s (%#x) returned from "
1646245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1647245228Sken		       entry->status_text : "Unknown", status);
1648274388Smav		free(lun_softc, M_CTLFE);
1649245228Sken	}
1650245228Sken
1651260387Sscottl	xpt_path_unlock(path);
1652275882Smav	ctlfe_onoffline(arg, /*online*/ 1);
1653260387Sscottl	xpt_free_path(path);
1654229997Sken}
1655229997Sken
1656229997Skenstatic void
1657229997Skenctlfe_offline(void *arg)
1658229997Sken{
1659245228Sken	struct ctlfe_softc *bus_softc;
1660245228Sken	struct cam_path *path;
1661245228Sken	cam_status status;
1662245228Sken	struct cam_periph *periph;
1663245228Sken
1664245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1665245228Sken
1666275882Smav	ctlfe_onoffline(arg, /*online*/ 0);
1667275882Smav
1668245228Sken	/*
1669245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1670245228Sken	 * the port offline.
1671245228Sken	 */
1672245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1673245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1674245228Sken				 CAM_LUN_WILDCARD);
1675245228Sken	if (status != CAM_REQ_CMP) {
1676245228Sken		printf("%s: unable to create path for wildcard periph\n",
1677245228Sken		       __func__);
1678245228Sken		return;
1679245228Sken	}
1680260387Sscottl	xpt_path_lock(path);
1681245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1682245228Sken		cam_periph_invalidate(periph);
1683260387Sscottl	xpt_path_unlock(path);
1684245228Sken	xpt_free_path(path);
1685229997Sken}
1686229997Sken
1687229997Sken/*
1688229997Sken * This will get called to enable a LUN on every bus that is attached to
1689229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1690229997Sken */
1691229997Skenstatic int
1692284798Smavctlfe_lun_enable(void *arg, int lun_id)
1693229997Sken{
1694229997Sken	struct ctlfe_softc *bus_softc;
1695229997Sken	struct ctlfe_lun_softc *softc;
1696229997Sken	struct cam_path *path;
1697229997Sken	struct cam_periph *periph;
1698229997Sken	cam_status status;
1699229997Sken
1700229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1701290776Smav	if (bus_softc->hba_misc & PIM_EXTLUNS)
1702290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1703229997Sken
1704260387Sscottl	status = xpt_create_path(&path, /*periph*/ NULL,
1705290776Smav	    bus_softc->path_id, bus_softc->target_id, lun_id);
1706229997Sken	/* XXX KDM need some way to return status to CTL here? */
1707229997Sken	if (status != CAM_REQ_CMP) {
1708229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1709229997Sken		       status);
1710229997Sken		return (1);
1711229997Sken	}
1712229997Sken
1713229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1714260387Sscottl	xpt_path_lock(path);
1715229997Sken	periph = cam_periph_find(path, "ctl");
1716229997Sken	if (periph != NULL) {
1717229997Sken		/* We've already got a periph, no need to alloc a new one. */
1718260387Sscottl		xpt_path_unlock(path);
1719229997Sken		xpt_free_path(path);
1720229997Sken		free(softc, M_CTLFE);
1721229997Sken		return (0);
1722229997Sken	}
1723229997Sken	softc->parent_softc = bus_softc;
1724229997Sken
1725229997Sken	status = cam_periph_alloc(ctlferegister,
1726229997Sken				  ctlfeoninvalidate,
1727229997Sken				  ctlfecleanup,
1728229997Sken				  ctlfestart,
1729229997Sken				  "ctl",
1730229997Sken				  CAM_PERIPH_BIO,
1731229997Sken				  path,
1732229997Sken				  ctlfeasync,
1733229997Sken				  0,
1734229997Sken				  softc);
1735229997Sken
1736274388Smav	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1737274388Smav		const struct cam_status_entry *entry;
1738274388Smav
1739274388Smav		entry = cam_fetch_status_entry(status);
1740274388Smav		printf("%s: CAM error %s (%#x) returned from "
1741274388Smav		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1742274388Smav		       entry->status_text : "Unknown", status);
1743274388Smav		free(softc, M_CTLFE);
1744274388Smav	}
1745274388Smav
1746260387Sscottl	xpt_path_unlock(path);
1747244016Sken	xpt_free_path(path);
1748229997Sken	return (0);
1749229997Sken}
1750229997Sken
1751229997Sken/*
1752245228Sken * This will get called when the user removes a LUN to disable that LUN
1753245228Sken * on every bus that is attached to CTL.
1754229997Sken */
1755229997Skenstatic int
1756284798Smavctlfe_lun_disable(void *arg, int lun_id)
1757229997Sken{
1758229997Sken	struct ctlfe_softc *softc;
1759229997Sken	struct ctlfe_lun_softc *lun_softc;
1760229997Sken
1761229997Sken	softc = (struct ctlfe_softc *)arg;
1762290776Smav	if (softc->hba_misc & PIM_EXTLUNS)
1763290776Smav		lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1764229997Sken
1765260387Sscottl	mtx_lock(&softc->lun_softc_mtx);
1766229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1767229997Sken		struct cam_path *path;
1768229997Sken
1769229997Sken		path = lun_softc->periph->path;
1770229997Sken
1771290172Smav		if ((xpt_path_target_id(path) == softc->target_id)
1772229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
1773229997Sken			break;
1774229997Sken		}
1775229997Sken	}
1776229997Sken	if (lun_softc == NULL) {
1777260387Sscottl		mtx_unlock(&softc->lun_softc_mtx);
1778284798Smav		printf("%s: can't find lun %d\n", __func__, lun_id);
1779229997Sken		return (1);
1780229997Sken	}
1781260387Sscottl	cam_periph_acquire(lun_softc->periph);
1782260387Sscottl	mtx_unlock(&softc->lun_softc_mtx);
1783229997Sken
1784260387Sscottl	cam_periph_lock(lun_softc->periph);
1785229997Sken	cam_periph_invalidate(lun_softc->periph);
1786260387Sscottl	cam_periph_unlock(lun_softc->periph);
1787260387Sscottl	cam_periph_release(lun_softc->periph);
1788229997Sken	return (0);
1789229997Sken}
1790229997Sken
1791229997Skenstatic void
1792229997Skenctlfe_dump_sim(struct cam_sim *sim)
1793229997Sken{
1794229997Sken
1795229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1796229997Sken	       sim->sim_name, sim->unit_number,
1797229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
1798229997Sken}
1799229997Sken
1800229997Sken/*
1801229997Sken * Assumes that the SIM lock is held.
1802229997Sken */
1803229997Skenstatic void
1804229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1805229997Sken{
1806229997Sken	struct ccb_hdr *hdr;
1807229997Sken	struct cam_periph *periph;
1808229997Sken	int num_items;
1809229997Sken
1810229997Sken	periph = softc->periph;
1811229997Sken	num_items = 0;
1812229997Sken
1813315887Smav	STAILQ_FOREACH(hdr, &softc->work_queue, periph_links.stqe) {
1814275880Smav		union ctl_io *io = hdr->io_ptr;
1815229997Sken
1816229997Sken		num_items++;
1817229997Sken
1818229997Sken		/*
1819229997Sken		 * Only regular SCSI I/O is put on the work
1820229997Sken		 * queue, so we can print sense here.  There may be no
1821229997Sken		 * sense if it's no the queue for a DMA, but this serves to
1822229997Sken		 * print out the CCB as well.
1823229997Sken		 *
1824229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
1825229997Sken		 * CTL is merged in with CAM.
1826229997Sken		 */
1827229997Sken		ctl_io_error_print(io, NULL);
1828229997Sken
1829229997Sken		/*
1830275880Smav		 * Print DMA status if we are DMA_QUEUED.
1831229997Sken		 */
1832275880Smav		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1833275880Smav			xpt_print(periph->path,
1834275880Smav			    "Total %u, Current %u, Resid %u\n",
1835275880Smav			    io->scsiio.kern_total_len,
1836275880Smav			    io->scsiio.kern_data_len,
1837275880Smav			    io->scsiio.kern_data_resid);
1838275880Smav		}
1839229997Sken	}
1840229997Sken
1841314749Smav	xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1842314749Smav	xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1843229997Sken}
1844229997Sken
1845229997Sken/*
1846275880Smav * Datamove/done routine called by CTL.  Put ourselves on the queue to
1847275880Smav * receive a CCB from CAM so we can queue the continue I/O request down
1848275880Smav * to the adapter.
1849229997Sken */
1850229997Skenstatic void
1851275880Smavctlfe_datamove(union ctl_io *io)
1852229997Sken{
1853275880Smav	union ccb *ccb;
1854275880Smav	struct cam_periph *periph;
1855229997Sken	struct ctlfe_lun_softc *softc;
1856229997Sken
1857275880Smav	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1858275880Smav	    ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1859229997Sken
1860313365Smav	io->scsiio.ext_data_filled = 0;
1861312585Smav	ccb = PRIV_CCB(io);
1862275880Smav	periph = xpt_path_periph(ccb->ccb_h.path);
1863275880Smav	cam_periph_lock(periph);
1864275880Smav	softc = (struct ctlfe_lun_softc *)periph->softc;
1865275880Smav	io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1866275880Smav	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1867275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1868315887Smav	STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1869315887Smav			  periph_links.stqe);
1870314749Smav	xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1871275880Smav	cam_periph_unlock(periph);
1872229997Sken}
1873229997Sken
1874229997Skenstatic void
1875275880Smavctlfe_done(union ctl_io *io)
1876229997Sken{
1877229997Sken	union ccb *ccb;
1878229997Sken	struct cam_periph *periph;
1879229997Sken	struct ctlfe_lun_softc *softc;
1880229997Sken
1881312585Smav	ccb = PRIV_CCB(io);
1882229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
1883260387Sscottl	cam_periph_lock(periph);
1884229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1885229997Sken
1886229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
1887229997Sken		/*
1888229997Sken		 * Send the notify acknowledge down to the SIM, to let it
1889229997Sken		 * know we processed the task management command.
1890229997Sken		 */
1891229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
1892229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1893304417Smav		switch (io->taskio.task_status) {
1894304417Smav		case CTL_TASK_FUNCTION_COMPLETE:
1895304417Smav			ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1896304417Smav			break;
1897304417Smav		case CTL_TASK_FUNCTION_SUCCEEDED:
1898304417Smav			ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1899304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1900304417Smav			break;
1901304417Smav		case CTL_TASK_FUNCTION_REJECTED:
1902304417Smav			ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1903304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1904304417Smav			break;
1905304417Smav		case CTL_TASK_LUN_DOES_NOT_EXIST:
1906304417Smav			ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1907304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1908304417Smav			break;
1909304417Smav		case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1910304417Smav			ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1911304417Smav			ccb->ccb_h.flags |= CAM_SEND_STATUS;
1912304417Smav			break;
1913304417Smav		}
1914304417Smav		ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1915229997Sken		xpt_action(ccb);
1916275881Smav	} else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1917314739Smav		ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1918314739Smav		return;
1919229997Sken	} else {
1920275880Smav		io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1921315887Smav		STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1922315887Smav				  periph_links.stqe);
1923314749Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1924229997Sken	}
1925229997Sken
1926260387Sscottl	cam_periph_unlock(periph);
1927229997Sken}
1928229997Sken
1929229997Skenstatic void
1930229997Skenctlfe_dump(void)
1931229997Sken{
1932229997Sken	struct ctlfe_softc *bus_softc;
1933275880Smav	struct ctlfe_lun_softc *lun_softc;
1934229997Sken
1935229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1936229997Sken		ctlfe_dump_sim(bus_softc->sim);
1937275880Smav		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1938229997Sken			ctlfe_dump_queue(lun_softc);
1939229997Sken	}
1940229997Sken}
1941