1229997Sken/*-
2229997Sken * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3229997Sken * All rights reserved.
4229997Sken *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7229997Sken * are met:
8229997Sken * 1. Redistributions of source code must retain the above copyright
9229997Sken *    notice, this list of conditions, and the following disclaimer,
10229997Sken *    without modification.
11229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
13229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
14229997Sken *    including a substantially similar Disclaimer requirement for further
15229997Sken *    binary redistribution.
16229997Sken *
17229997Sken * NO WARRANTY
18229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28229997Sken * POSSIBILITY OF SUCH DAMAGES.
29229997Sken *
30229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
31229997Sken */
32229997Sken/*
33229997Sken * Peripheral driver interface between CAM and CTL (CAM Target Layer).
34229997Sken *
35229997Sken * Author: Ken Merry <ken@FreeBSD.org>
36229997Sken */
37229997Sken
38229997Sken#include <sys/cdefs.h>
39229997Sken__FBSDID("$FreeBSD$");
40229997Sken
41229997Sken#include <sys/param.h>
42229997Sken#include <sys/queue.h>
43229997Sken#include <sys/systm.h>
44229997Sken#include <sys/kernel.h>
45229997Sken#include <sys/lock.h>
46229997Sken#include <sys/mutex.h>
47229997Sken#include <sys/condvar.h>
48229997Sken#include <sys/malloc.h>
49229997Sken#include <sys/bus.h>
50229997Sken#include <sys/endian.h>
51229997Sken#include <sys/sbuf.h>
52229997Sken#include <sys/sysctl.h>
53229997Sken#include <sys/types.h>
54229997Sken#include <sys/systm.h>
55229997Sken#include <machine/bus.h>
56229997Sken
57229997Sken#include <cam/cam.h>
58229997Sken#include <cam/cam_ccb.h>
59229997Sken#include <cam/cam_periph.h>
60229997Sken#include <cam/cam_queue.h>
61229997Sken#include <cam/cam_xpt_periph.h>
62229997Sken#include <cam/cam_debug.h>
63229997Sken#include <cam/cam_sim.h>
64229997Sken#include <cam/cam_xpt.h>
65229997Sken
66229997Sken#include <cam/scsi/scsi_all.h>
67229997Sken#include <cam/scsi/scsi_message.h>
68229997Sken
69229997Sken#include <cam/ctl/ctl_io.h>
70229997Sken#include <cam/ctl/ctl.h>
71229997Sken#include <cam/ctl/ctl_frontend.h>
72229997Sken#include <cam/ctl/ctl_util.h>
73229997Sken#include <cam/ctl/ctl_error.h>
74229997Sken
75229997Skentypedef enum {
76245228Sken	CTLFE_CCB_DEFAULT	= 0x00,
77229997Sken	CTLFE_CCB_WAITING 	= 0x01
78229997Sken} ctlfe_ccb_types;
79229997Sken
80229997Skenstruct ctlfe_softc {
81229997Sken	struct ctl_frontend fe;
82229997Sken	path_id_t path_id;
83229997Sken	struct cam_sim *sim;
84229997Sken	char port_name[DEV_IDLEN];
85229997Sken	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
86229997Sken	STAILQ_ENTRY(ctlfe_softc) links;
87229997Sken};
88229997Sken
89229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
90229997Skenstruct mtx ctlfe_list_mtx;
91229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist";
92229997Skenstatic int ctlfe_dma_enabled = 1;
93229997Sken#ifdef CTLFE_INIT_ENABLE
94229997Skenstatic int ctlfe_max_targets = 1;
95229997Skenstatic int ctlfe_num_targets = 0;
96229997Sken#endif
97229997Sken
98229997Skentypedef enum {
99229997Sken	CTLFE_LUN_NONE		= 0x00,
100229997Sken	CTLFE_LUN_WILDCARD	= 0x01
101229997Sken} ctlfe_lun_flags;
102229997Sken
103229997Skenstruct ctlfe_lun_softc {
104229997Sken	struct ctlfe_softc *parent_softc;
105229997Sken	struct cam_periph *periph;
106229997Sken	ctlfe_lun_flags flags;
107229997Sken	struct callout dma_callout;
108229997Sken	uint64_t ccbs_alloced;
109229997Sken	uint64_t ccbs_freed;
110229997Sken	uint64_t ctios_sent;
111229997Sken	uint64_t ctios_returned;
112229997Sken	uint64_t atios_sent;
113229997Sken	uint64_t atios_returned;
114229997Sken	uint64_t inots_sent;
115229997Sken	uint64_t inots_returned;
116229997Sken	/* bus_dma_tag_t dma_tag; */
117229997Sken	TAILQ_HEAD(, ccb_hdr) work_queue;
118229997Sken	STAILQ_ENTRY(ctlfe_lun_softc) links;
119229997Sken};
120229997Sken
121229997Skentypedef enum {
122229997Sken	CTLFE_CMD_NONE		= 0x00,
123229997Sken	CTLFE_CMD_PIECEWISE	= 0x01
124229997Sken} ctlfe_cmd_flags;
125229997Sken
126229997Sken/*
127229997Sken * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
128229997Sken * Currently that is 600 bytes.
129229997Sken */
130229997Skenstruct ctlfe_lun_cmd_info {
131229997Sken	int cur_transfer_index;
132229997Sken	ctlfe_cmd_flags flags;
133229997Sken	/*
134229997Sken	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
135229997Sken	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
136229997Sken	 * i386 and 512 bytes on amd64.
137229997Sken	 */
138229997Sken	bus_dma_segment_t cam_sglist[32];
139229997Sken};
140229997Sken
141229997Sken/*
142229997Sken * When we register the adapter/bus, request that this many ctl_ios be
143229997Sken * allocated.  This should be the maximum supported by the adapter, but we
144229997Sken * currently don't have a way to get that back from the path inquiry.
145229997Sken * XXX KDM add that to the path inquiry.
146229997Sken */
147229997Sken#define	CTLFE_REQ_CTL_IO	4096
148229997Sken/*
149229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the
150229997Sken * adapter per LUN.
151229997Sken * XXX KDM should this be controlled by CTL?
152229997Sken */
153229997Sken#define	CTLFE_ATIO_PER_LUN	1024
154229997Sken/*
155229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
156229997Sken * allocate and queue down to the adapter per LUN.
157229997Sken * XXX KDM should this be controlled by CTL?
158229997Sken */
159229997Sken#define	CTLFE_IN_PER_LUN	1024
160229997Sken
161229997Sken/*
162229997Sken * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
163229997Sken * status to the initiator.  The SIM is expected to have its own timeouts,
164229997Sken * so we're not putting this timeout around the CCB execution time.  The
165229997Sken * SIM should timeout and let us know if it has an issue.
166229997Sken */
167229997Sken#define	CTLFE_DMA_TIMEOUT	60
168229997Sken
169229997Sken/*
170229997Sken * Turn this on to enable extra debugging prints.
171229997Sken */
172229997Sken#if 0
173229997Sken#define	CTLFE_DEBUG
174229997Sken#endif
175229997Sken
176229997Sken/*
177229997Sken * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
178229997Sken * in the FreeBSD initiator that makes it unable to rescan the target if
179229997Sken * the target gets rebooted and the WWNN/WWPN stay the same.
180229997Sken */
181229997Sken#if 0
182229997Sken#define	RANDOM_WWNN
183229997Sken#endif
184229997Sken
185229997SkenSYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
186229997Sken	   &ctlfe_dma_enabled, 0, "DMA enabled");
187229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
188229997Sken
189229997Sken#define	ccb_type	ppriv_field0
190229997Sken/* This is only used in the ATIO */
191229997Sken#define	io_ptr		ppriv_ptr1
192229997Sken
193229997Sken/* This is only used in the CTIO */
194229997Sken#define	ccb_atio	ppriv_ptr1
195229997Sken
196229997Skenint			ctlfeinitialize(void);
197229997Skenvoid			ctlfeshutdown(void);
198229997Skenstatic periph_init_t	ctlfeinit;
199229997Skenstatic void		ctlfeasync(void *callback_arg, uint32_t code,
200229997Sken				   struct cam_path *path, void *arg);
201229997Skenstatic periph_ctor_t	ctlferegister;
202229997Skenstatic periph_oninv_t	ctlfeoninvalidate;
203229997Skenstatic periph_dtor_t	ctlfecleanup;
204229997Skenstatic periph_start_t	ctlfestart;
205229997Skenstatic void		ctlfedone(struct cam_periph *periph,
206229997Sken				  union ccb *done_ccb);
207229997Sken
208229997Skenstatic void 		ctlfe_onoffline(void *arg, int online);
209229997Skenstatic void 		ctlfe_online(void *arg);
210229997Skenstatic void 		ctlfe_offline(void *arg);
211229997Skenstatic int 		ctlfe_targ_enable(void *arg, struct ctl_id targ_id);
212229997Skenstatic int 		ctlfe_targ_disable(void *arg, struct ctl_id targ_id);
213229997Skenstatic int 		ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
214229997Sken					 int lun_id);
215229997Skenstatic int 		ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
216229997Sken					  int lun_id);
217229997Skenstatic void		ctlfe_dump_sim(struct cam_sim *sim);
218229997Skenstatic void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
219229997Skenstatic void		ctlfe_dma_timeout(void *arg);
220229997Skenstatic void 		ctlfe_datamove_done(union ctl_io *io);
221229997Skenstatic void 		ctlfe_dump(void);
222229997Sken
223229997Skenstatic struct periph_driver ctlfe_driver =
224229997Sken{
225229997Sken	ctlfeinit, "ctl",
226229997Sken	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0
227229997Sken};
228229997Sken
229249009Straszstatic int ctlfe_module_event_handler(module_t, int /*modeventtype_t*/, void *);
230249009Strasz
231249009Strasz/*
232249009Strasz * We're not using PERIPHDRIVER_DECLARE(), because it runs at SI_SUB_DRIVERS,
233249009Strasz * and that happens before CTL gets initialised.
234249009Strasz */
235249009Straszstatic moduledata_t ctlfe_moduledata = {
236249009Strasz	"ctlfe",
237249009Strasz	ctlfe_module_event_handler,
238249009Strasz	NULL
239249009Strasz};
240249009Strasz
241249009StraszDECLARE_MODULE(ctlfe, ctlfe_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
242249009StraszMODULE_VERSION(ctlfe, 1);
243249009StraszMODULE_DEPEND(ctlfe, ctl, 1, 1, 1);
244249009StraszMODULE_DEPEND(ctlfe, cam, 1, 1, 1);
245249009Strasz
246229997Skenextern struct ctl_softc *control_softc;
247229997Sken
248229997Skenvoid
249229997Skenctlfeshutdown(void)
250229997Sken{
251229997Sken	return;
252229997Sken}
253229997Sken
254229997Skenvoid
255229997Skenctlfeinit(void)
256229997Sken{
257229997Sken	cam_status status;
258229997Sken
259229997Sken	STAILQ_INIT(&ctlfe_softc_list);
260229997Sken
261229997Sken	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
262229997Sken
263229997Sken	KASSERT(control_softc != NULL, ("CTL is not initialized!"));
264229997Sken
265229997Sken	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
266229997Sken				    AC_CONTRACT, ctlfeasync, NULL, NULL);
267229997Sken
268229997Sken	if (status != CAM_REQ_CMP) {
269229997Sken		printf("ctl: Failed to attach async callback due to CAM "
270229997Sken		       "status 0x%x!\n", status);
271229997Sken	}
272229997Sken}
273229997Sken
274249009Straszstatic int
275249009Straszctlfe_module_event_handler(module_t mod, int what, void *arg)
276249009Strasz{
277249009Strasz
278249009Strasz	switch (what) {
279249009Strasz	case MOD_LOAD:
280249009Strasz		periphdriver_register(&ctlfe_driver);
281249009Strasz		return (0);
282249009Strasz	case MOD_UNLOAD:
283249009Strasz		return (EBUSY);
284249009Strasz	default:
285249009Strasz		return (EOPNOTSUPP);
286249009Strasz	}
287249009Strasz}
288249009Strasz
289229997Skenstatic void
290229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
291229997Sken{
292229997Sken
293229997Sken#ifdef CTLFEDEBUG
294229997Sken	printf("%s: entered\n", __func__);
295229997Sken#endif
296229997Sken
297229997Sken	/*
298229997Sken	 * When a new path gets registered, and it is capable of target
299229997Sken	 * mode, go ahead and attach.  Later on, we may need to be more
300229997Sken	 * selective, but for now this will be sufficient.
301229997Sken 	 */
302229997Sken	switch (code) {
303229997Sken	case AC_PATH_REGISTERED: {
304229997Sken		struct ctl_frontend *fe;
305229997Sken		struct ctlfe_softc *bus_softc;
306229997Sken		struct ccb_pathinq *cpi;
307229997Sken		int retval;
308229997Sken
309229997Sken		cpi = (struct ccb_pathinq *)arg;
310229997Sken
311229997Sken		/* Don't attach if it doesn't support target mode */
312229997Sken		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
313230033Sken#ifdef CTLFEDEBUG
314229997Sken			printf("%s: SIM %s%d doesn't support target mode\n",
315229997Sken			       __func__, cpi->dev_name, cpi->unit_number);
316230033Sken#endif
317229997Sken			break;
318229997Sken		}
319229997Sken
320229997Sken#ifdef CTLFE_INIT_ENABLE
321229997Sken		if (ctlfe_num_targets >= ctlfe_max_targets) {
322229997Sken			union ccb *ccb;
323229997Sken			struct cam_sim *sim;
324229997Sken
325229997Sken			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
326229997Sken						  M_NOWAIT | M_ZERO);
327229997Sken			if (ccb == NULL) {
328229997Sken				printf("%s: unable to malloc CCB!\n", __func__);
329229997Sken				return;
330229997Sken			}
331229997Sken			xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path,
332242174Smav				      CAM_PRIORITY_NONE);
333229997Sken
334229997Sken			sim = xpt_path_sim(cpi->ccb_h.path);
335229997Sken
336229997Sken			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
337229997Sken			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
338229997Sken			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
339229997Sken
340229997Sken			/* We should hold the SIM lock here */
341229997Sken			mtx_assert(sim->mtx, MA_OWNED);
342229997Sken
343229997Sken			xpt_action(ccb);
344229997Sken
345229997Sken			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
346229997Sken			     CAM_REQ_CMP) {
347229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
348229997Sken				       "enable failed with status %#x\n",
349229997Sken				       __func__, cpi->dev_name,
350229997Sken				       cpi->unit_number, cpi->ccb_h.path_id,
351229997Sken				       ccb->ccb_h.status);
352229997Sken			} else {
353229997Sken				printf("%s: SIM %s%d (path id %d) initiator "
354229997Sken				       "enable succeeded\n",
355229997Sken				       __func__, cpi->dev_name,
356229997Sken				       cpi->unit_number, cpi->ccb_h.path_id);
357229997Sken			}
358229997Sken
359229997Sken			free(ccb, M_TEMP);
360229997Sken
361229997Sken			break;
362229997Sken		} else {
363229997Sken			ctlfe_num_targets++;
364229997Sken		}
365229997Sken
366229997Sken		printf("%s: ctlfe_num_targets = %d\n", __func__,
367229997Sken		       ctlfe_num_targets);
368229997Sken#endif /* CTLFE_INIT_ENABLE */
369229997Sken
370229997Sken		/*
371229997Sken		 * We're in an interrupt context here, so we have to
372229997Sken		 * use M_NOWAIT.  Of course this means trouble if we
373229997Sken		 * can't allocate memory.
374229997Sken		 */
375229997Sken		bus_softc = malloc(sizeof(*bus_softc), M_CTLFE,
376229997Sken				   M_NOWAIT | M_ZERO);
377229997Sken		if (bus_softc == NULL) {
378229997Sken			printf("%s: unable to malloc %zd bytes for softc\n",
379229997Sken			       __func__, sizeof(*bus_softc));
380229997Sken			return;
381229997Sken		}
382229997Sken
383229997Sken		bus_softc->path_id = cpi->ccb_h.path_id;
384229997Sken		bus_softc->sim = xpt_path_sim(cpi->ccb_h.path);
385229997Sken		STAILQ_INIT(&bus_softc->lun_softc_list);
386229997Sken
387229997Sken		fe = &bus_softc->fe;
388229997Sken
389229997Sken		/*
390229997Sken		 * XXX KDM should we be more accurate here ?
391229997Sken		 */
392229997Sken		if (cpi->transport == XPORT_FC)
393229997Sken			fe->port_type = CTL_PORT_FC;
394229997Sken		else
395229997Sken			fe->port_type = CTL_PORT_SCSI;
396229997Sken
397229997Sken		/* XXX KDM what should the real number be here? */
398229997Sken		fe->num_requested_ctl_io = 4096;
399229997Sken		snprintf(bus_softc->port_name, sizeof(bus_softc->port_name),
400229997Sken			 "%s%d", cpi->dev_name, cpi->unit_number);
401229997Sken		/*
402229997Sken		 * XXX KDM it would be nice to allocate storage in the
403229997Sken		 * frontend structure itself.
404229997Sken	 	 */
405229997Sken		fe->port_name = bus_softc->port_name;
406229997Sken		fe->physical_port = cpi->unit_number;
407229997Sken		fe->virtual_port = cpi->bus_id;
408229997Sken		fe->port_online = ctlfe_online;
409229997Sken		fe->port_offline = ctlfe_offline;
410229997Sken		fe->onoff_arg = bus_softc;
411229997Sken		fe->targ_enable = ctlfe_targ_enable;
412229997Sken		fe->targ_disable = ctlfe_targ_disable;
413229997Sken		fe->lun_enable = ctlfe_lun_enable;
414229997Sken		fe->lun_disable = ctlfe_lun_disable;
415229997Sken		fe->targ_lun_arg = bus_softc;
416229997Sken		fe->fe_datamove = ctlfe_datamove_done;
417229997Sken		fe->fe_done = ctlfe_datamove_done;
418229997Sken		fe->fe_dump = ctlfe_dump;
419229997Sken		/*
420229997Sken		 * XXX KDM the path inquiry doesn't give us the maximum
421229997Sken		 * number of targets supported.
422229997Sken		 */
423229997Sken		fe->max_targets = cpi->max_target;
424229997Sken		fe->max_target_id = cpi->max_target;
425229997Sken
426229997Sken		/*
427229997Sken		 * XXX KDM need to figure out whether we're the master or
428229997Sken		 * slave.
429229997Sken		 */
430230033Sken#ifdef CTLFEDEBUG
431229997Sken		printf("%s: calling ctl_frontend_register() for %s%d\n",
432229997Sken		       __func__, cpi->dev_name, cpi->unit_number);
433230033Sken#endif
434229997Sken		retval = ctl_frontend_register(fe, /*master_SC*/ 1);
435229997Sken		if (retval != 0) {
436229997Sken			printf("%s: ctl_frontend_register() failed with "
437229997Sken			       "error %d!\n", __func__, retval);
438229997Sken			free(bus_softc, M_CTLFE);
439229997Sken			break;
440229997Sken		} else {
441229997Sken			mtx_lock(&ctlfe_list_mtx);
442229997Sken			STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links);
443229997Sken			mtx_unlock(&ctlfe_list_mtx);
444229997Sken		}
445229997Sken
446245228Sken		break;
447245228Sken	}
448245228Sken	case AC_PATH_DEREGISTERED: {
449245228Sken		struct ctlfe_softc *softc = NULL;
450245228Sken
451245228Sken		mtx_lock(&ctlfe_list_mtx);
452245228Sken		STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
453245228Sken			if (softc->path_id == xpt_path_path_id(path)) {
454245228Sken				STAILQ_REMOVE(&ctlfe_softc_list, softc,
455245228Sken						ctlfe_softc, links);
456245228Sken				break;
457245228Sken			}
458229997Sken		}
459245228Sken		mtx_unlock(&ctlfe_list_mtx);
460245228Sken
461245228Sken		if (softc != NULL) {
462245228Sken			/*
463245228Sken			 * XXX KDM are we certain at this point that there
464245228Sken			 * are no outstanding commands for this frontend?
465245228Sken			 */
466245228Sken			ctl_frontend_deregister(&softc->fe);
467245228Sken			free(softc, M_CTLFE);
468229997Sken		}
469229997Sken		break;
470229997Sken	}
471229997Sken	case AC_CONTRACT: {
472229997Sken		struct ac_contract *ac;
473229997Sken
474229997Sken		ac = (struct ac_contract *)arg;
475229997Sken
476229997Sken		switch (ac->contract_number) {
477229997Sken		case AC_CONTRACT_DEV_CHG: {
478229997Sken			struct ac_device_changed *dev_chg;
479229997Sken			struct ctlfe_softc *softc;
480229997Sken			int retval, found;
481229997Sken
482229997Sken			dev_chg = (struct ac_device_changed *)ac->contract_data;
483229997Sken
484236426Smjacob			printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
485229997Sken			       __func__, dev_chg->wwpn, dev_chg->port,
486229997Sken			       xpt_path_path_id(path), dev_chg->target,
487229997Sken			       (dev_chg->arrived == 0) ?  "left" : "arrived");
488229997Sken
489229997Sken			found = 0;
490229997Sken
491229997Sken			mtx_lock(&ctlfe_list_mtx);
492229997Sken			STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
493229997Sken				if (softc->path_id == xpt_path_path_id(path)) {
494229997Sken					found = 1;
495229997Sken					break;
496229997Sken				}
497229997Sken			}
498229997Sken			mtx_unlock(&ctlfe_list_mtx);
499229997Sken
500229997Sken			if (found == 0) {
501229997Sken				printf("%s: CTL port for CAM path %u not "
502229997Sken				       "found!\n", __func__,
503229997Sken				       xpt_path_path_id(path));
504229997Sken				break;
505229997Sken			}
506229997Sken			if (dev_chg->arrived != 0) {
507229997Sken				retval = ctl_add_initiator(dev_chg->wwpn,
508229997Sken					softc->fe.targ_port, dev_chg->target);
509229997Sken			} else {
510229997Sken				retval = ctl_remove_initiator(
511229997Sken					softc->fe.targ_port, dev_chg->target);
512229997Sken			}
513229997Sken
514229997Sken			if (retval != 0) {
515229997Sken				printf("%s: could not %s port %d iid %u "
516229997Sken				       "WWPN %#jx!\n", __func__,
517229997Sken				       (dev_chg->arrived != 0) ? "add" :
518229997Sken				       "remove", softc->fe.targ_port,
519229997Sken				       dev_chg->target,
520229997Sken				       (uintmax_t)dev_chg->wwpn);
521229997Sken			}
522229997Sken			break;
523229997Sken		}
524229997Sken		default:
525229997Sken			printf("%s: unsupported contract number %ju\n",
526229997Sken			       __func__, (uintmax_t)ac->contract_number);
527229997Sken			break;
528229997Sken		}
529229997Sken		break;
530229997Sken	}
531229997Sken	default:
532229997Sken		break;
533229997Sken	}
534229997Sken}
535229997Sken
536229997Skenstatic cam_status
537229997Skenctlferegister(struct cam_periph *periph, void *arg)
538229997Sken{
539229997Sken	struct ctlfe_softc *bus_softc;
540229997Sken	struct ctlfe_lun_softc *softc;
541229997Sken	struct cam_sim *sim;
542229997Sken	union ccb en_lun_ccb;
543229997Sken	cam_status status;
544229997Sken	int i;
545229997Sken
546229997Sken	softc = (struct ctlfe_lun_softc *)arg;
547229997Sken	bus_softc = softc->parent_softc;
548229997Sken	sim = xpt_path_sim(periph->path);
549229997Sken
550229997Sken	TAILQ_INIT(&softc->work_queue);
551229997Sken	softc->periph = periph;
552229997Sken
553229997Sken	callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0);
554229997Sken	periph->softc = softc;
555229997Sken
556242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
557229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
558229997Sken	en_lun_ccb.cel.grp6_len = 0;
559229997Sken	en_lun_ccb.cel.grp7_len = 0;
560229997Sken	en_lun_ccb.cel.enable = 1;
561229997Sken	xpt_action(&en_lun_ccb);
562229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
563229997Sken	if (status != CAM_REQ_CMP) {
564229997Sken		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
565229997Sken			  __func__, en_lun_ccb.ccb_h.status);
566229997Sken		return (status);
567229997Sken	}
568229997Sken
569229997Sken	status = CAM_REQ_CMP;
570229997Sken
571229997Sken	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
572229997Sken		union ccb *new_ccb;
573229997Sken
574229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
575236426Smjacob					      M_ZERO|M_NOWAIT);
576229997Sken		if (new_ccb == NULL) {
577229997Sken			status = CAM_RESRC_UNAVAIL;
578229997Sken			break;
579229997Sken		}
580229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
581229997Sken		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
582229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
583229997Sken		xpt_action(new_ccb);
584229997Sken		softc->atios_sent++;
585229997Sken		status = new_ccb->ccb_h.status;
586229997Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
587229997Sken			free(new_ccb, M_CTLFE);
588229997Sken			break;
589229997Sken		}
590229997Sken	}
591229997Sken
592229997Sken	status = cam_periph_acquire(periph);
593229997Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
594229997Sken		xpt_print(periph->path, "%s: could not acquire reference "
595229997Sken			  "count, status = %#x\n", __func__, status);
596229997Sken		return (status);
597229997Sken	}
598229997Sken
599229997Sken	if (i == 0) {
600229997Sken		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
601229997Sken			  "status 0x%x\n", __func__, status);
602229997Sken		return (CAM_REQ_CMP_ERR);
603229997Sken	}
604229997Sken
605229997Sken	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
606229997Sken		union ccb *new_ccb;
607229997Sken
608229997Sken		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
609236426Smjacob					      M_ZERO|M_NOWAIT);
610229997Sken		if (new_ccb == NULL) {
611229997Sken			status = CAM_RESRC_UNAVAIL;
612229997Sken			break;
613229997Sken		}
614229997Sken
615229997Sken		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
616229997Sken		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
617229997Sken		new_ccb->ccb_h.cbfcnp = ctlfedone;
618229997Sken		xpt_action(new_ccb);
619229997Sken		softc->inots_sent++;
620229997Sken		status = new_ccb->ccb_h.status;
621237601Sken		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
622237601Sken			/*
623237601Sken			 * Note that we don't free the CCB here.  If the
624237601Sken			 * status is not CAM_REQ_INPROG, then we're
625237601Sken			 * probably talking to a SIM that says it is
626237601Sken			 * target-capable but doesn't support the
627237601Sken			 * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
628237601Sken			 * older API.  In that case, it'll call xpt_done()
629237601Sken			 * on the CCB, and we need to free it in our done
630237601Sken			 * routine as a result.
631237601Sken			 */
632229997Sken			break;
633229997Sken		}
634229997Sken	}
635237601Sken	if ((i == 0)
636237601Sken	 || (status != CAM_REQ_INPROG)) {
637229997Sken		xpt_print(periph->path, "%s: could not allocate immediate "
638229997Sken			  "notify CCBs, status 0x%x\n", __func__, status);
639229997Sken		return (CAM_REQ_CMP_ERR);
640229997Sken	}
641229997Sken	return (CAM_REQ_CMP);
642229997Sken}
643229997Sken
644229997Skenstatic void
645229997Skenctlfeoninvalidate(struct cam_periph *periph)
646229997Sken{
647229997Sken	union ccb en_lun_ccb;
648229997Sken	cam_status status;
649229997Sken	struct ctlfe_lun_softc *softc;
650229997Sken
651229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
652229997Sken
653242174Smav	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
654229997Sken	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
655229997Sken	en_lun_ccb.cel.grp6_len = 0;
656229997Sken	en_lun_ccb.cel.grp7_len = 0;
657229997Sken	en_lun_ccb.cel.enable = 0;
658229997Sken	xpt_action(&en_lun_ccb);
659229997Sken	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
660229997Sken	if (status != CAM_REQ_CMP) {
661229997Sken		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
662229997Sken			  __func__, en_lun_ccb.ccb_h.status);
663229997Sken		/*
664229997Sken		 * XXX KDM what do we do now?
665229997Sken		 */
666229997Sken	}
667229997Sken	xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
668229997Sken		  "INOTs outstanding, %d refs\n", softc->atios_sent -
669229997Sken		  softc->atios_returned, softc->inots_sent -
670229997Sken		  softc->inots_returned, periph->refcount);
671229997Sken}
672229997Sken
673229997Skenstatic void
674229997Skenctlfecleanup(struct cam_periph *periph)
675229997Sken{
676229997Sken	struct ctlfe_lun_softc *softc;
677229997Sken	struct ctlfe_softc *bus_softc;
678229997Sken
679229997Sken	xpt_print(periph->path, "%s: Called\n", __func__);
680229997Sken
681229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
682229997Sken	bus_softc = softc->parent_softc;
683229997Sken
684245228Sken	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
685229997Sken
686229997Sken	/*
687229997Sken	 * XXX KDM is there anything else that needs to be done here?
688229997Sken	 */
689245228Sken
690245228Sken	callout_stop(&softc->dma_callout);
691245228Sken
692229997Sken	free(softc, M_CTLFE);
693229997Sken}
694229997Sken
695229997Skenstatic void
696229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb)
697229997Sken{
698229997Sken	struct ctlfe_lun_softc *softc;
699229997Sken	struct ccb_hdr *ccb_h;
700229997Sken
701229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
702229997Sken
703229997Sken	softc->ccbs_alloced++;
704229997Sken
705245228Sken	start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT;
706245228Sken
707229997Sken	ccb_h = TAILQ_FIRST(&softc->work_queue);
708229997Sken	if (periph->immediate_priority <= periph->pinfo.priority) {
709229997Sken		panic("shouldn't get to the CCB waiting case!");
710229997Sken		start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING;
711229997Sken		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
712229997Sken				  periph_links.sle);
713229997Sken		periph->immediate_priority = CAM_PRIORITY_NONE;
714229997Sken		wakeup(&periph->ccb_list);
715229997Sken	} else if (ccb_h == NULL) {
716229997Sken		softc->ccbs_freed++;
717229997Sken		xpt_release_ccb(start_ccb);
718229997Sken	} else {
719229997Sken		struct ccb_accept_tio *atio;
720229997Sken		struct ccb_scsiio *csio;
721229997Sken		uint8_t *data_ptr;
722229997Sken		uint32_t dxfer_len;
723229997Sken		ccb_flags flags;
724229997Sken		union ctl_io *io;
725229997Sken		uint8_t scsi_status;
726229997Sken
727229997Sken		/* Take the ATIO off the work queue */
728229997Sken		TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
729229997Sken		atio = (struct ccb_accept_tio *)ccb_h;
730229997Sken		io = (union ctl_io *)ccb_h->io_ptr;
731229997Sken		csio = &start_ccb->csio;
732229997Sken
733229997Sken		flags = atio->ccb_h.flags &
734229997Sken			(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
735229997Sken
736229997Sken		if ((io == NULL)
737229997Sken		 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
738229997Sken			/*
739229997Sken			 * We're done, send status back.
740229997Sken			 */
741229997Sken			flags |= CAM_SEND_STATUS;
742229997Sken			if (io == NULL) {
743229997Sken				scsi_status = SCSI_STATUS_BUSY;
744229997Sken				csio->sense_len = 0;
745229997Sken			} else if ((io->io_hdr.status & CTL_STATUS_MASK) ==
746229997Sken				   CTL_CMD_ABORTED) {
747229997Sken				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
748229997Sken
749229997Sken				/*
750229997Sken				 * If this command was aborted, we don't
751229997Sken				 * need to send status back to the SIM.
752229997Sken				 * Just free the CTIO and ctl_io, and
753229997Sken				 * recycle the ATIO back to the SIM.
754229997Sken				 */
755229997Sken				xpt_print(periph->path, "%s: aborted "
756229997Sken					  "command 0x%04x discarded\n",
757229997Sken					  __func__, io->scsiio.tag_num);
758229997Sken				ctl_free_io(io);
759229997Sken				/*
760229997Sken				 * For a wildcard attachment, commands can
761229997Sken				 * come in with a specific target/lun.  Reset
762229997Sken				 * the target and LUN fields back to the
763229997Sken				 * wildcard values before we send them back
764229997Sken				 * down to the SIM.  The SIM has a wildcard
765229997Sken				 * LUN enabled, not whatever target/lun
766229997Sken				 * these happened to be.
767229997Sken				 */
768229997Sken				if (softc->flags & CTLFE_LUN_WILDCARD) {
769229997Sken					atio->ccb_h.target_id =
770229997Sken						CAM_TARGET_WILDCARD;
771229997Sken					atio->ccb_h.target_lun =
772229997Sken						CAM_LUN_WILDCARD;
773229997Sken				}
774229997Sken
775229997Sken				if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
776229997Sken					cam_release_devq(periph->path,
777229997Sken							 /*relsim_flags*/0,
778229997Sken							 /*reduction*/0,
779229997Sken 							 /*timeout*/0,
780229997Sken							 /*getcount_only*/0);
781229997Sken					atio->ccb_h.status &= ~CAM_DEV_QFRZN;
782229997Sken				}
783229997Sken
784229997Sken				ccb_h = TAILQ_FIRST(&softc->work_queue);
785229997Sken
786229997Sken				if (atio->ccb_h.func_code !=
787229997Sken				    XPT_ACCEPT_TARGET_IO) {
788229997Sken					xpt_print(periph->path, "%s: func_code "
789229997Sken						  "is %#x\n", __func__,
790229997Sken						  atio->ccb_h.func_code);
791229997Sken				}
792229997Sken				start_ccb->ccb_h.func_code = XPT_ABORT;
793229997Sken				start_ccb->cab.abort_ccb = (union ccb *)atio;
794229997Sken				start_ccb->ccb_h.cbfcnp = ctlfedone;
795229997Sken
796229997Sken				/* Tell the SIM that we've aborted this ATIO */
797229997Sken				xpt_action(start_ccb);
798229997Sken				softc->ccbs_freed++;
799229997Sken				xpt_release_ccb(start_ccb);
800229997Sken
801229997Sken				/*
802229997Sken				 * Send the ATIO back down to the SIM.
803229997Sken				 */
804229997Sken				xpt_action((union ccb *)atio);
805229997Sken				softc->atios_sent++;
806229997Sken
807229997Sken				/*
808229997Sken				 * If we still have work to do, ask for
809229997Sken				 * another CCB.  Otherwise, deactivate our
810229997Sken				 * callout.
811229997Sken				 */
812229997Sken				if (ccb_h != NULL)
813229997Sken					xpt_schedule(periph, /*priority*/ 1);
814229997Sken				else
815229997Sken					callout_stop(&softc->dma_callout);
816229997Sken
817229997Sken				return;
818229997Sken			} else {
819229997Sken				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
820229997Sken				scsi_status = io->scsiio.scsi_status;
821229997Sken				csio->sense_len = io->scsiio.sense_len;
822229997Sken			}
823229997Sken			data_ptr = NULL;
824229997Sken			dxfer_len = 0;
825229997Sken			if (io == NULL) {
826229997Sken				printf("%s: tag %04x io is NULL\n", __func__,
827229997Sken				       atio->tag_id);
828229997Sken			} else {
829229997Sken#ifdef CTLFEDEBUG
830229997Sken				printf("%s: tag %04x status %x\n", __func__,
831229997Sken				       atio->tag_id, io->io_hdr.status);
832229997Sken#endif
833229997Sken			}
834229997Sken			csio->sglist_cnt = 0;
835229997Sken			if (csio->sense_len != 0) {
836229997Sken				csio->sense_data = io->scsiio.sense_data;
837229997Sken				flags |= CAM_SEND_SENSE;
838229997Sken			} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
839229997Sken				xpt_print(periph->path, "%s: check condition "
840229997Sken					  "with no sense\n", __func__);
841229997Sken			}
842229997Sken		} else {
843229997Sken			struct ctlfe_lun_cmd_info *cmd_info;
844229997Sken
845229997Sken			/*
846229997Sken			 * Datamove call, we need to setup the S/G list.
847229997Sken			 */
848229997Sken
849229997Sken			cmd_info = (struct ctlfe_lun_cmd_info *)
850229997Sken				io->io_hdr.port_priv;
851229997Sken
852229997Sken			KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
853229997Sken				("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
854229997Sken				"CTL_PORT_PRIV_SIZE %d", __func__,
855229997Sken				sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
856229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
857229997Sken
858229997Sken			/*
859229997Sken			 * Need to zero this, in case it has been used for
860229997Sken			 * a previous datamove for this particular I/O.
861229997Sken			 */
862229997Sken			bzero(cmd_info, sizeof(*cmd_info));
863229997Sken			scsi_status = 0;
864229997Sken
865229997Sken			/*
866229997Sken			 * Set the direction, relative to the initiator.
867229997Sken			 */
868229997Sken			flags &= ~CAM_DIR_MASK;
869229997Sken			if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
870229997Sken			     CTL_FLAG_DATA_IN)
871229997Sken				flags |= CAM_DIR_IN;
872229997Sken			else
873229997Sken				flags |= CAM_DIR_OUT;
874229997Sken
875229997Sken			csio->cdb_len = atio->cdb_len;
876229997Sken
877246713Skib			flags &= ~CAM_DATA_MASK;
878229997Sken			if (io->scsiio.kern_sg_entries == 0) {
879229997Sken				/* No S/G list */
880229997Sken				data_ptr = io->scsiio.kern_data_ptr;
881229997Sken				dxfer_len = io->scsiio.kern_data_len;
882229997Sken				csio->sglist_cnt = 0;
883229997Sken
884229997Sken				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
885246713Skib					flags |= CAM_DATA_PADDR;
886246713Skib				else
887246713Skib					flags |= CAM_DATA_VADDR;
888229997Sken			} else if (io->scsiio.kern_sg_entries <=
889229997Sken				   (sizeof(cmd_info->cam_sglist)/
890229997Sken				   sizeof(cmd_info->cam_sglist[0]))) {
891229997Sken				/*
892229997Sken				 * S/G list with physical or virtual pointers.
893229997Sken				 * Just populate the CAM S/G list with the
894229997Sken				 * pointers.
895229997Sken				 */
896229997Sken				int i;
897229997Sken				struct ctl_sg_entry *ctl_sglist;
898229997Sken				bus_dma_segment_t *cam_sglist;
899229997Sken
900229997Sken				ctl_sglist = (struct ctl_sg_entry *)
901229997Sken					io->scsiio.kern_data_ptr;
902229997Sken				cam_sglist = cmd_info->cam_sglist;
903229997Sken
904229997Sken				for (i = 0; i < io->scsiio.kern_sg_entries;i++){
905229997Sken					cam_sglist[i].ds_addr =
906229997Sken						(bus_addr_t)ctl_sglist[i].addr;
907229997Sken					cam_sglist[i].ds_len =
908229997Sken						ctl_sglist[i].len;
909229997Sken				}
910229997Sken				csio->sglist_cnt = io->scsiio.kern_sg_entries;
911229997Sken				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
912246713Skib					flags |= CAM_DATA_SG_PADDR;
913229997Sken				else
914249028Sken					flags |= CAM_DATA_SG;
915229997Sken				data_ptr = (uint8_t *)cam_sglist;
916229997Sken				dxfer_len = io->scsiio.kern_data_len;
917229997Sken			} else {
918229997Sken				/* S/G list with virtual pointers */
919229997Sken				struct ctl_sg_entry *sglist;
920229997Sken				int *ti;
921229997Sken
922229997Sken				/*
923245228Sken				 * If we have more S/G list pointers than
924245228Sken				 * will fit in the available storage in the
925245228Sken				 * cmd_info structure inside the ctl_io header,
926245228Sken				 * then we need to send down the pointers
927245228Sken				 * one element at a time.
928229997Sken				 */
929245228Sken
930229997Sken				sglist = (struct ctl_sg_entry *)
931229997Sken					io->scsiio.kern_data_ptr;
932229997Sken				ti = &cmd_info->cur_transfer_index;
933229997Sken				data_ptr = sglist[*ti].addr;
934229997Sken				dxfer_len = sglist[*ti].len;
935229997Sken				csio->sglist_cnt = 0;
936249028Sken				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
937249028Sken					flags |= CAM_DATA_PADDR;
938249028Sken				else
939249028Sken					flags |= CAM_DATA_VADDR;
940229997Sken				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
941229997Sken				(*ti)++;
942229997Sken			}
943229997Sken
944229997Sken			io->scsiio.ext_data_filled += dxfer_len;
945229997Sken
946229997Sken			if (io->scsiio.ext_data_filled >
947229997Sken			    io->scsiio.kern_total_len) {
948229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
949229997Sken					  "fill len %u > total %u\n",
950229997Sken					  __func__, io->scsiio.tag_num,
951229997Sken					  io->scsiio.ext_data_filled,
952229997Sken					  io->scsiio.kern_total_len);
953229997Sken			}
954229997Sken		}
955229997Sken
956229997Sken#ifdef CTLFEDEBUG
957229997Sken		printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
958229997Sken		       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
959229997Sken		       atio->tag_id, flags, data_ptr, dxfer_len);
960229997Sken#endif
961229997Sken
962229997Sken		/*
963229997Sken		 * Valid combinations:
964255117Smav		 *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
965229997Sken		 *    sglist_cnt = 0
966255117Smav		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
967229997Sken		 *    sglist_cnt = 0
968255117Smav		 *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
969229997Sken		 *    sglist_cnt != 0
970229997Sken		 */
971229997Sken#ifdef CTLFEDEBUG
972229997Sken		if (((flags & CAM_SEND_STATUS)
973255117Smav		  && (((flags & CAM_DATA_SG) != 0)
974229997Sken		   || (dxfer_len != 0)
975229997Sken		   || (csio->sglist_cnt != 0)))
976229997Sken		 || (((flags & CAM_SEND_STATUS) == 0)
977229997Sken		  && (dxfer_len == 0))
978255117Smav		 || ((flags & CAM_DATA_SG)
979229997Sken		  && (csio->sglist_cnt == 0))
980255117Smav		 || (((flags & CAM_DATA_SG) == 0)
981229997Sken		  && (csio->sglist_cnt != 0))) {
982229997Sken			printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
983229997Sken			       "%d sg %u\n", __func__, atio->tag_id,
984229997Sken			       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
985229997Sken			       csio->sglist_cnt);
986229997Sken			if (io != NULL) {
987229997Sken				printf("%s: tag %04x io status %#x\n", __func__,
988229997Sken				       atio->tag_id, io->io_hdr.status);
989229997Sken			} else {
990229997Sken				printf("%s: tag %04x no associated io\n",
991229997Sken				       __func__, atio->tag_id);
992229997Sken			}
993229997Sken		}
994229997Sken#endif
995229997Sken		cam_fill_ctio(csio,
996229997Sken			      /*retries*/ 2,
997229997Sken			      ctlfedone,
998229997Sken			      flags,
999229997Sken			      (flags & CAM_TAG_ACTION_VALID) ?
1000229997Sken			       MSG_SIMPLE_Q_TAG : 0,
1001229997Sken			      atio->tag_id,
1002229997Sken			      atio->init_id,
1003229997Sken			      scsi_status,
1004229997Sken			      /*data_ptr*/ data_ptr,
1005229997Sken			      /*dxfer_len*/ dxfer_len,
1006229997Sken			      /*timeout*/ 5 * 1000);
1007229997Sken		start_ccb->ccb_h.ccb_atio = atio;
1008229997Sken		if (((flags & CAM_SEND_STATUS) == 0)
1009229997Sken		 && (io != NULL))
1010229997Sken			io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1011229997Sken
1012229997Sken		softc->ctios_sent++;
1013229997Sken
1014229997Sken		xpt_action(start_ccb);
1015229997Sken
1016229997Sken		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1017229997Sken			cam_release_devq(periph->path,
1018229997Sken					 /*relsim_flags*/0,
1019229997Sken					 /*reduction*/0,
1020229997Sken 					 /*timeout*/0,
1021229997Sken					 /*getcount_only*/0);
1022229997Sken			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
1023229997Sken		}
1024229997Sken
1025229997Sken		ccb_h = TAILQ_FIRST(&softc->work_queue);
1026229997Sken	}
1027229997Sken	/*
1028229997Sken	 * If we still have work to do, ask for another CCB.  Otherwise,
1029229997Sken	 * deactivate our callout.
1030229997Sken	 */
1031229997Sken	if (ccb_h != NULL)
1032229997Sken		xpt_schedule(periph, /*priority*/ 1);
1033229997Sken	else
1034229997Sken		callout_stop(&softc->dma_callout);
1035229997Sken}
1036229997Sken
1037229997Skenstatic void
1038229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
1039229997Sken{
1040229997Sken	struct ctlfe_lun_softc *softc;
1041229997Sken
1042229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1043229997Sken
1044229997Sken	switch (ccb->ccb_h.func_code) {
1045229997Sken	case XPT_ACCEPT_TARGET_IO:
1046229997Sken		softc->atios_returned++;
1047229997Sken		break;
1048229997Sken	case XPT_IMMEDIATE_NOTIFY:
1049229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1050229997Sken		softc->inots_returned++;
1051229997Sken		break;
1052229997Sken	default:
1053229997Sken		break;
1054229997Sken	}
1055229997Sken
1056229997Sken	free(ccb, M_CTLFE);
1057229997Sken
1058229997Sken	KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1059229997Sken		"atios_returned %ju > atios_sent %ju", __func__,
1060229997Sken		softc->atios_returned, softc->atios_sent));
1061229997Sken	KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1062229997Sken		"inots_returned %ju > inots_sent %ju", __func__,
1063229997Sken		softc->inots_returned, softc->inots_sent));
1064229997Sken
1065229997Sken	/*
1066229997Sken	 * If we have received all of our CCBs, we can release our
1067229997Sken	 * reference on the peripheral driver.  It will probably go away
1068229997Sken	 * now.
1069229997Sken	 */
1070229997Sken	if ((softc->atios_returned == softc->atios_sent)
1071229997Sken	 && (softc->inots_returned == softc->inots_sent)) {
1072229997Sken		cam_periph_release_locked(periph);
1073229997Sken	}
1074229997Sken}
1075229997Sken
1076238870Smjacobstatic int
1077238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1078238870Smjacob{
1079238870Smjacob	uint64_t lba;
1080238870Smjacob	uint32_t num_blocks, nbc;
1081238870Smjacob	uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1082238870Smjacob	    atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1083238870Smjacob
1084238870Smjacob	nbc = offset >> 9;	/* ASSUMING 512 BYTE BLOCKS */
1085238870Smjacob
1086238870Smjacob	switch (cmdbyt[0]) {
1087238870Smjacob	case READ_6:
1088238870Smjacob	case WRITE_6:
1089238870Smjacob	{
1090238870Smjacob		struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1091238870Smjacob		lba = scsi_3btoul(cdb->addr);
1092238870Smjacob		lba &= 0x1fffff;
1093238870Smjacob		num_blocks = cdb->length;
1094238870Smjacob		if (num_blocks == 0)
1095238870Smjacob			num_blocks = 256;
1096238870Smjacob		lba += nbc;
1097238870Smjacob		num_blocks -= nbc;
1098238870Smjacob		scsi_ulto3b(lba, cdb->addr);
1099238870Smjacob		cdb->length = num_blocks;
1100238870Smjacob		break;
1101238870Smjacob	}
1102238870Smjacob	case READ_10:
1103238870Smjacob	case WRITE_10:
1104238870Smjacob	{
1105238870Smjacob		struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1106238870Smjacob		lba = scsi_4btoul(cdb->addr);
1107238870Smjacob		num_blocks = scsi_2btoul(cdb->length);
1108238870Smjacob		lba += nbc;
1109238870Smjacob		num_blocks -= nbc;
1110238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1111238870Smjacob		scsi_ulto2b(num_blocks, cdb->length);
1112238870Smjacob		break;
1113238870Smjacob	}
1114238870Smjacob	case READ_12:
1115238870Smjacob	case WRITE_12:
1116238870Smjacob	{
1117238870Smjacob		struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1118238870Smjacob		lba = scsi_4btoul(cdb->addr);
1119238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1120238870Smjacob		lba += nbc;
1121238870Smjacob		num_blocks -= nbc;
1122238870Smjacob		scsi_ulto4b(lba, cdb->addr);
1123238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1124238870Smjacob		break;
1125238870Smjacob	}
1126238870Smjacob	case READ_16:
1127238870Smjacob	case WRITE_16:
1128238870Smjacob	{
1129238870Smjacob		struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1130238870Smjacob		lba = scsi_8btou64(cdb->addr);
1131238870Smjacob		num_blocks = scsi_4btoul(cdb->length);
1132238870Smjacob		lba += nbc;
1133238870Smjacob		num_blocks -= nbc;
1134238870Smjacob		scsi_u64to8b(lba, cdb->addr);
1135238870Smjacob		scsi_ulto4b(num_blocks, cdb->length);
1136238870Smjacob		break;
1137238870Smjacob	}
1138238870Smjacob	default:
1139238870Smjacob		return -1;
1140238870Smjacob	}
1141238870Smjacob	return (0);
1142238870Smjacob}
1143238870Smjacob
1144229997Skenstatic void
1145229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1146229997Sken{
1147229997Sken	struct ctlfe_lun_softc *softc;
1148229997Sken	struct ctlfe_softc *bus_softc;
1149238870Smjacob	struct ccb_accept_tio *atio = NULL;
1150238870Smjacob	union ctl_io *io = NULL;
1151229997Sken
1152229997Sken#ifdef CTLFE_DEBUG
1153229997Sken	printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
1154229997Sken	       done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
1155229997Sken#endif
1156229997Sken
1157229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
1158229997Sken	bus_softc = softc->parent_softc;
1159229997Sken
1160229997Sken	if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) {
1161229997Sken		panic("shouldn't get to the CCB waiting case!");
1162229997Sken		wakeup(&done_ccb->ccb_h.cbfcnp);
1163229997Sken		return;
1164229997Sken	}
1165229997Sken
1166229997Sken	/*
1167229997Sken	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1168229997Sken	 * need to be freed.  Most of the ATIOs and INOTs that come back
1169229997Sken	 * will be CCBs that are being returned from the SIM as a result of
1170229997Sken	 * our disabling the LUN.
1171229997Sken	 *
1172229997Sken	 * Other CCB types are handled in their respective cases below.
1173229997Sken	 */
1174229997Sken	if (periph->flags & CAM_PERIPH_INVALID) {
1175229997Sken		switch (done_ccb->ccb_h.func_code) {
1176229997Sken		case XPT_ACCEPT_TARGET_IO:
1177229997Sken		case XPT_IMMEDIATE_NOTIFY:
1178229997Sken		case XPT_NOTIFY_ACKNOWLEDGE:
1179229997Sken			ctlfe_free_ccb(periph, done_ccb);
1180229997Sken			return;
1181229997Sken		default:
1182229997Sken			break;
1183229997Sken		}
1184229997Sken
1185229997Sken	}
1186229997Sken	switch (done_ccb->ccb_h.func_code) {
1187229997Sken	case XPT_ACCEPT_TARGET_IO: {
1188229997Sken
1189229997Sken		atio = &done_ccb->atio;
1190229997Sken
1191229997Sken		softc->atios_returned++;
1192229997Sken
1193238870Smjacob resubmit:
1194229997Sken		/*
1195229997Sken		 * Allocate a ctl_io, pass it to CTL, and wait for the
1196229997Sken		 * datamove or done.
1197229997Sken		 */
1198229997Sken		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1199229997Sken		if (io == NULL) {
1200229997Sken			atio->ccb_h.flags &= ~CAM_DIR_MASK;
1201229997Sken			atio->ccb_h.flags |= CAM_DIR_NONE;
1202229997Sken
1203229997Sken			printf("%s: ctl_alloc_io failed!\n", __func__);
1204229997Sken
1205229997Sken			/*
1206229997Sken			 * XXX KDM need to set SCSI_STATUS_BUSY, but there
1207229997Sken			 * is no field in the ATIO structure to do that,
1208229997Sken			 * and we aren't able to allocate a ctl_io here.
1209229997Sken			 * What to do?
1210229997Sken			 */
1211229997Sken			atio->sense_len = 0;
1212229997Sken			done_ccb->ccb_h.io_ptr = NULL;
1213229997Sken			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
1214229997Sken					  periph_links.tqe);
1215229997Sken			xpt_schedule(periph, /*priority*/ 1);
1216229997Sken			break;
1217229997Sken		}
1218229997Sken		ctl_zero_io(io);
1219229997Sken
1220229997Sken		/* Save pointers on both sides */
1221229997Sken		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1222229997Sken		done_ccb->ccb_h.io_ptr = io;
1223229997Sken
1224229997Sken		/*
1225229997Sken		 * Only SCSI I/O comes down this path, resets, etc. come
1226229997Sken		 * down the immediate notify path below.
1227229997Sken		 */
1228229997Sken		io->io_hdr.io_type = CTL_IO_SCSI;
1229229997Sken		io->io_hdr.nexus.initid.id = atio->init_id;
1230229997Sken		io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1231229997Sken		io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1232229997Sken		io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1233229997Sken		io->scsiio.tag_num = atio->tag_id;
1234229997Sken		switch (atio->tag_action) {
1235229997Sken		case CAM_TAG_ACTION_NONE:
1236229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1237229997Sken			break;
1238229997Sken		case MSG_SIMPLE_TASK:
1239229997Sken			io->scsiio.tag_type = CTL_TAG_SIMPLE;
1240229997Sken			break;
1241229997Sken		case MSG_HEAD_OF_QUEUE_TASK:
1242229997Sken        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1243229997Sken			break;
1244229997Sken		case MSG_ORDERED_TASK:
1245229997Sken        		io->scsiio.tag_type = CTL_TAG_ORDERED;
1246229997Sken			break;
1247229997Sken		case MSG_ACA_TASK:
1248229997Sken			io->scsiio.tag_type = CTL_TAG_ACA;
1249229997Sken			break;
1250229997Sken		default:
1251229997Sken			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1252229997Sken			printf("%s: unhandled tag type %#x!!\n", __func__,
1253229997Sken			       atio->tag_action);
1254229997Sken			break;
1255229997Sken		}
1256229997Sken		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1257229997Sken			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1258229997Sken			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1259229997Sken		}
1260229997Sken		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1261229997Sken		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1262229997Sken		      io->scsiio.cdb_len);
1263229997Sken
1264229997Sken#ifdef CTLFEDEBUG
1265229997Sken		printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1266229997Sken		        (uintmax_t)io->io_hdr.nexus.initid.id,
1267229997Sken		        io->io_hdr.nexus.targ_port,
1268229997Sken		        (uintmax_t)io->io_hdr.nexus.targ_target.id,
1269229997Sken		        io->io_hdr.nexus.targ_lun,
1270229997Sken			io->scsiio.tag_num, io->scsiio.cdb[0]);
1271229997Sken#endif
1272229997Sken
1273229997Sken		ctl_queue(io);
1274229997Sken		break;
1275229997Sken	}
1276229997Sken	case XPT_CONT_TARGET_IO: {
1277238870Smjacob		int srr = 0;
1278238870Smjacob		uint32_t srr_off = 0;
1279229997Sken
1280229997Sken		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1281229997Sken		io = (union ctl_io *)atio->ccb_h.io_ptr;
1282229997Sken
1283229997Sken		softc->ctios_returned++;
1284229997Sken#ifdef CTLFEDEBUG
1285229997Sken		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1286229997Sken		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
1287229997Sken#endif
1288229997Sken		/*
1289238870Smjacob		 * Handle SRR case were the data pointer is pushed back hack
1290238870Smjacob		 */
1291238870Smjacob		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1292238870Smjacob		    && done_ccb->csio.msg_ptr != NULL
1293238870Smjacob		    && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1294238870Smjacob		    && done_ccb->csio.msg_ptr[1] == 5
1295238870Smjacob       		    && done_ccb->csio.msg_ptr[2] == 0) {
1296238870Smjacob			srr = 1;
1297238870Smjacob			srr_off =
1298238870Smjacob			    (done_ccb->csio.msg_ptr[3] << 24)
1299238870Smjacob			    | (done_ccb->csio.msg_ptr[4] << 16)
1300238870Smjacob			    | (done_ccb->csio.msg_ptr[5] << 8)
1301238870Smjacob			    | (done_ccb->csio.msg_ptr[6]);
1302238870Smjacob		}
1303238870Smjacob
1304238870Smjacob		if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) {
1305238870Smjacob			/*
1306238870Smjacob			 * If status was being sent, the back end data is now
1307238870Smjacob			 * history. Hack it up and resubmit a new command with
1308238870Smjacob			 * the CDB adjusted. If the SIM does the right thing,
1309238870Smjacob			 * all of the resid math should work.
1310238870Smjacob			 */
1311238870Smjacob			softc->ccbs_freed++;
1312238870Smjacob			xpt_release_ccb(done_ccb);
1313238870Smjacob			ctl_free_io(io);
1314238870Smjacob			if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1315238870Smjacob				done_ccb = (union ccb *)atio;
1316238870Smjacob				goto resubmit;
1317238870Smjacob			}
1318238870Smjacob			/*
1319238870Smjacob			 * Fall through to doom....
1320238870Smjacob			 */
1321238870Smjacob		} else if (srr) {
1322238870Smjacob			/*
1323238870Smjacob			 * If we have an srr and we're still sending data, we
1324238870Smjacob			 * should be able to adjust offsets and cycle again.
1325238870Smjacob			 */
1326238870Smjacob			io->scsiio.kern_rel_offset =
1327238870Smjacob			    io->scsiio.ext_data_filled = srr_off;
1328238870Smjacob			io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1329238870Smjacob			    io->scsiio.kern_rel_offset;
1330238870Smjacob			softc->ccbs_freed++;
1331238870Smjacob			io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1332238870Smjacob			xpt_release_ccb(done_ccb);
1333238870Smjacob			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1334238870Smjacob					  periph_links.tqe);
1335238870Smjacob			xpt_schedule(periph, /*priority*/ 1);
1336238870Smjacob			return;
1337238870Smjacob		}
1338238870Smjacob
1339238870Smjacob		/*
1340229997Sken		 * If we were sending status back to the initiator, free up
1341229997Sken		 * resources.  If we were doing a datamove, call the
1342229997Sken		 * datamove done routine.
1343229997Sken		 */
1344229997Sken		if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
1345229997Sken			softc->ccbs_freed++;
1346229997Sken			xpt_release_ccb(done_ccb);
1347229997Sken			ctl_free_io(io);
1348229997Sken			/*
1349229997Sken			 * For a wildcard attachment, commands can come in
1350229997Sken			 * with a specific target/lun.  Reset the target
1351229997Sken			 * and LUN fields back to the wildcard values before
1352229997Sken			 * we send them back down to the SIM.  The SIM has
1353229997Sken			 * a wildcard LUN enabled, not whatever target/lun
1354229997Sken			 * these happened to be.
1355229997Sken			 */
1356229997Sken			if (softc->flags & CTLFE_LUN_WILDCARD) {
1357229997Sken				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1358229997Sken				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1359229997Sken			}
1360229997Sken			if (periph->flags & CAM_PERIPH_INVALID) {
1361229997Sken				ctlfe_free_ccb(periph, (union ccb *)atio);
1362229997Sken				return;
1363229997Sken			} else {
1364229997Sken				xpt_action((union ccb *)atio);
1365229997Sken				softc->atios_sent++;
1366229997Sken			}
1367229997Sken		} else {
1368229997Sken			struct ctlfe_lun_cmd_info *cmd_info;
1369229997Sken			struct ccb_scsiio *csio;
1370229997Sken
1371229997Sken			csio = &done_ccb->csio;
1372229997Sken			cmd_info = (struct ctlfe_lun_cmd_info *)
1373229997Sken				io->io_hdr.port_priv;
1374229997Sken
1375229997Sken			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1376229997Sken
1377229997Sken			io->scsiio.ext_data_len += csio->dxfer_len;
1378229997Sken			if (io->scsiio.ext_data_len >
1379229997Sken			    io->scsiio.kern_total_len) {
1380229997Sken				xpt_print(periph->path, "%s: tag 0x%04x "
1381229997Sken					  "done len %u > total %u sent %u\n",
1382229997Sken					  __func__, io->scsiio.tag_num,
1383229997Sken					  io->scsiio.ext_data_len,
1384229997Sken					  io->scsiio.kern_total_len,
1385229997Sken					  io->scsiio.ext_data_filled);
1386229997Sken			}
1387229997Sken			/*
1388229997Sken			 * Translate CAM status to CTL status.  Success
1389229997Sken			 * does not change the overall, ctl_io status.  In
1390229997Sken			 * that case we just set port_status to 0.  If we
1391229997Sken			 * have a failure, though, set a data phase error
1392229997Sken			 * for the overall ctl_io.
1393229997Sken			 */
1394229997Sken			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1395229997Sken			case CAM_REQ_CMP:
1396229997Sken				io->io_hdr.port_status = 0;
1397229997Sken				break;
1398229997Sken			default:
1399229997Sken				/*
1400245228Sken				 * XXX KDM we probably need to figure out a
1401245228Sken				 * standard set of errors that the SIM
1402245228Sken				 * drivers should return in the event of a
1403245228Sken				 * data transfer failure.  A data phase
1404245228Sken				 * error will at least point the user to a
1405245228Sken				 * data transfer error of some sort.
1406245228Sken				 * Hopefully the SIM printed out some
1407245228Sken				 * additional information to give the user
1408245228Sken				 * a clue what happened.
1409229997Sken				 */
1410229997Sken				io->io_hdr.port_status = 0xbad1;
1411229997Sken				ctl_set_data_phase_error(&io->scsiio);
1412229997Sken				/*
1413229997Sken				 * XXX KDM figure out residual.
1414229997Sken				 */
1415229997Sken				break;
1416229997Sken			}
1417229997Sken			/*
1418229997Sken			 * If we had to break this S/G list into multiple
1419229997Sken			 * pieces, figure out where we are in the list, and
1420229997Sken			 * continue sending pieces if necessary.
1421229997Sken			 */
1422229997Sken			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1423229997Sken			 && (io->io_hdr.port_status == 0)
1424229997Sken			 && (cmd_info->cur_transfer_index <
1425229997Sken			     io->scsiio.kern_sg_entries)) {
1426229997Sken				struct ctl_sg_entry *sglist;
1427229997Sken				ccb_flags flags;
1428229997Sken				uint8_t scsi_status;
1429229997Sken				uint8_t *data_ptr;
1430229997Sken				uint32_t dxfer_len;
1431229997Sken				int *ti;
1432229997Sken
1433229997Sken				sglist = (struct ctl_sg_entry *)
1434229997Sken					io->scsiio.kern_data_ptr;
1435229997Sken				ti = &cmd_info->cur_transfer_index;
1436229997Sken				flags = atio->ccb_h.flags &
1437229997Sken					(CAM_DIS_DISCONNECT|
1438229997Sken					 CAM_TAG_ACTION_VALID|
1439229997Sken					 CAM_DIR_MASK);
1440229997Sken
1441229997Sken				/*
1442229997Sken				 * Set the direction, relative to the initiator.
1443229997Sken				 */
1444229997Sken				flags &= ~CAM_DIR_MASK;
1445229997Sken				if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1446229997Sken				     CTL_FLAG_DATA_IN)
1447229997Sken					flags |= CAM_DIR_IN;
1448229997Sken				else
1449229997Sken					flags |= CAM_DIR_OUT;
1450229997Sken
1451229997Sken				data_ptr = sglist[*ti].addr;
1452229997Sken				dxfer_len = sglist[*ti].len;
1453229997Sken				(*ti)++;
1454229997Sken
1455229997Sken				scsi_status = 0;
1456229997Sken
1457229997Sken				if (((flags & CAM_SEND_STATUS) == 0)
1458229997Sken				 && (dxfer_len == 0)) {
1459229997Sken					printf("%s: tag %04x no status or "
1460229997Sken					       "len cdb = %02x\n", __func__,
1461229997Sken					       atio->tag_id,
1462229997Sken					atio->cdb_io.cdb_bytes[0]);
1463229997Sken					printf("%s: tag %04x io status %#x\n",
1464229997Sken					       __func__, atio->tag_id,
1465229997Sken					       io->io_hdr.status);
1466229997Sken				}
1467229997Sken
1468229997Sken				cam_fill_ctio(csio,
1469229997Sken					      /*retries*/ 2,
1470229997Sken					      ctlfedone,
1471229997Sken					      flags,
1472229997Sken					      (flags & CAM_TAG_ACTION_VALID) ?
1473229997Sken					       MSG_SIMPLE_Q_TAG : 0,
1474229997Sken					      atio->tag_id,
1475229997Sken					      atio->init_id,
1476229997Sken					      scsi_status,
1477229997Sken					      /*data_ptr*/ data_ptr,
1478229997Sken					      /*dxfer_len*/ dxfer_len,
1479229997Sken					      /*timeout*/ 5 * 1000);
1480229997Sken
1481229997Sken				csio->resid = 0;
1482229997Sken				csio->ccb_h.ccb_atio = atio;
1483229997Sken				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1484229997Sken				softc->ctios_sent++;
1485229997Sken				xpt_action((union ccb *)csio);
1486229997Sken			} else {
1487229997Sken				/*
1488229997Sken				 * Release the CTIO.  The ATIO will be sent back
1489229997Sken				 * down to the SIM once we send status.
1490229997Sken				 */
1491229997Sken				softc->ccbs_freed++;
1492229997Sken				xpt_release_ccb(done_ccb);
1493229997Sken
1494229997Sken				/* Call the backend move done callback */
1495229997Sken				io->scsiio.be_move_done(io);
1496229997Sken			}
1497229997Sken		}
1498229997Sken		break;
1499229997Sken	}
1500229997Sken	case XPT_IMMEDIATE_NOTIFY: {
1501229997Sken		union ctl_io *io;
1502229997Sken		struct ccb_immediate_notify *inot;
1503229997Sken		cam_status status;
1504229997Sken		int frozen;
1505229997Sken
1506229997Sken		inot = &done_ccb->cin1;
1507229997Sken
1508229997Sken		softc->inots_returned++;
1509229997Sken
1510229997Sken		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1511229997Sken
1512229997Sken		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1513229997Sken		       "seq %#x\n", __func__, inot->ccb_h.status,
1514229997Sken		       inot->tag_id, inot->seq_id);
1515229997Sken
1516229997Sken		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1517229997Sken		if (io != NULL) {
1518229997Sken			int send_ctl_io;
1519229997Sken
1520229997Sken			send_ctl_io = 1;
1521229997Sken
1522229997Sken			ctl_zero_io(io);
1523229997Sken			io->io_hdr.io_type = CTL_IO_TASK;
1524229997Sken			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1525229997Sken			inot->ccb_h.io_ptr = io;
1526229997Sken			io->io_hdr.nexus.initid.id = inot->initiator_id;
1527229997Sken			io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1528229997Sken			io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1529229997Sken			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1530229997Sken			/* XXX KDM should this be the tag_id? */
1531229997Sken			io->taskio.tag_num = inot->seq_id;
1532229997Sken
1533229997Sken			status = inot->ccb_h.status & CAM_STATUS_MASK;
1534229997Sken			switch (status) {
1535229997Sken			case CAM_SCSI_BUS_RESET:
1536229997Sken				io->taskio.task_action = CTL_TASK_BUS_RESET;
1537229997Sken				break;
1538229997Sken			case CAM_BDR_SENT:
1539229997Sken				io->taskio.task_action = CTL_TASK_TARGET_RESET;
1540229997Sken				break;
1541229997Sken			case CAM_MESSAGE_RECV:
1542229997Sken				switch (inot->arg) {
1543229997Sken				case MSG_ABORT_TASK_SET:
1544229997Sken					/*
1545229997Sken					 * XXX KDM this isn't currently
1546229997Sken					 * supported by CTL.  It ends up
1547229997Sken					 * being a no-op.
1548229997Sken					 */
1549229997Sken					io->taskio.task_action =
1550229997Sken						CTL_TASK_ABORT_TASK_SET;
1551229997Sken					break;
1552229997Sken				case MSG_TARGET_RESET:
1553229997Sken					io->taskio.task_action =
1554229997Sken						CTL_TASK_TARGET_RESET;
1555229997Sken					break;
1556229997Sken				case MSG_ABORT_TASK:
1557229997Sken					io->taskio.task_action =
1558229997Sken						CTL_TASK_ABORT_TASK;
1559229997Sken					break;
1560229997Sken				case MSG_LOGICAL_UNIT_RESET:
1561229997Sken					io->taskio.task_action =
1562229997Sken						CTL_TASK_LUN_RESET;
1563229997Sken					break;
1564229997Sken				case MSG_CLEAR_TASK_SET:
1565229997Sken					/*
1566229997Sken					 * XXX KDM this isn't currently
1567229997Sken					 * supported by CTL.  It ends up
1568229997Sken					 * being a no-op.
1569229997Sken					 */
1570229997Sken					io->taskio.task_action =
1571229997Sken						CTL_TASK_CLEAR_TASK_SET;
1572229997Sken					break;
1573229997Sken				case MSG_CLEAR_ACA:
1574229997Sken					io->taskio.task_action =
1575229997Sken						CTL_TASK_CLEAR_ACA;
1576229997Sken					break;
1577229997Sken				case MSG_NOOP:
1578229997Sken					send_ctl_io = 0;
1579229997Sken					break;
1580229997Sken				default:
1581229997Sken					xpt_print(periph->path, "%s: "
1582229997Sken						  "unsupported message 0x%x\n",
1583229997Sken						  __func__, inot->arg);
1584229997Sken					send_ctl_io = 0;
1585229997Sken					break;
1586229997Sken				}
1587229997Sken				break;
1588229997Sken			case CAM_REQ_ABORTED:
1589229997Sken				/*
1590229997Sken				 * This request was sent back by the driver.
1591229997Sken				 * XXX KDM what do we do here?
1592229997Sken				 */
1593229997Sken				send_ctl_io = 0;
1594229997Sken				break;
1595237601Sken			case CAM_REQ_INVALID:
1596237601Sken			case CAM_PROVIDE_FAIL:
1597229997Sken			default:
1598237601Sken				/*
1599237601Sken				 * We should only get here if we're talking
1600237601Sken				 * to a talking to a SIM that is target
1601237601Sken				 * capable but supports the old API.  In
1602237601Sken				 * that case, we need to just free the CCB.
1603237601Sken				 * If we actually send a notify acknowledge,
1604237601Sken				 * it will send that back with an error as
1605237601Sken				 * well.
1606237601Sken				 */
1607237601Sken
1608237601Sken				if ((status != CAM_REQ_INVALID)
1609237601Sken				 && (status != CAM_PROVIDE_FAIL))
1610237601Sken					xpt_print(periph->path, "%s: "
1611237601Sken						  "unsupported CAM status "
1612237601Sken						  "0x%x\n", __func__, status);
1613237601Sken
1614237601Sken				ctl_free_io(io);
1615237601Sken				ctlfe_free_ccb(periph, done_ccb);
1616237601Sken
1617237601Sken				return;
1618229997Sken			}
1619229997Sken			if (send_ctl_io != 0) {
1620229997Sken				ctl_queue(io);
1621229997Sken			} else {
1622229997Sken				ctl_free_io(io);
1623229997Sken				done_ccb->ccb_h.status = CAM_REQ_INPROG;
1624229997Sken				done_ccb->ccb_h.func_code =
1625229997Sken					XPT_NOTIFY_ACKNOWLEDGE;
1626229997Sken				xpt_action(done_ccb);
1627229997Sken			}
1628229997Sken		} else {
1629229997Sken			xpt_print(periph->path, "%s: could not allocate "
1630229997Sken				  "ctl_io for immediate notify!\n", __func__);
1631229997Sken			/* requeue this to the adapter */
1632229997Sken			done_ccb->ccb_h.status = CAM_REQ_INPROG;
1633229997Sken			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1634229997Sken			xpt_action(done_ccb);
1635229997Sken		}
1636229997Sken
1637229997Sken		if (frozen != 0) {
1638229997Sken			cam_release_devq(periph->path,
1639229997Sken					 /*relsim_flags*/ 0,
1640229997Sken					 /*opening reduction*/ 0,
1641229997Sken					 /*timeout*/ 0,
1642229997Sken					 /*getcount_only*/ 0);
1643229997Sken		}
1644229997Sken		break;
1645229997Sken	}
1646229997Sken	case XPT_NOTIFY_ACKNOWLEDGE:
1647229997Sken		/*
1648229997Sken		 * Queue this back down to the SIM as an immediate notify.
1649229997Sken		 */
1650229997Sken		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1651229997Sken		xpt_action(done_ccb);
1652229997Sken		softc->inots_sent++;
1653229997Sken		break;
1654229997Sken	case XPT_ABORT:
1655229997Sken		/*
1656229997Sken		 * XPT_ABORT is an immediate CCB, we shouldn't get here.
1657229997Sken		 */
1658229997Sken		panic("%s: XPT_ABORT CCB returned!", __func__);
1659229997Sken		break;
1660229997Sken	case XPT_SET_SIM_KNOB:
1661229997Sken	case XPT_GET_SIM_KNOB:
1662229997Sken		break;
1663229997Sken	default:
1664229997Sken		panic("%s: unexpected CCB type %#x", __func__,
1665229997Sken		      done_ccb->ccb_h.func_code);
1666229997Sken		break;
1667229997Sken	}
1668229997Sken}
1669229997Sken
1670229997Skenstatic void
1671229997Skenctlfe_onoffline(void *arg, int online)
1672229997Sken{
1673229997Sken	struct ctlfe_softc *bus_softc;
1674229997Sken	union ccb *ccb;
1675229997Sken	cam_status status;
1676229997Sken	struct cam_path *path;
1677229997Sken	struct cam_sim *sim;
1678229997Sken	int set_wwnn;
1679229997Sken
1680229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1681229997Sken
1682229997Sken	set_wwnn = 0;
1683229997Sken
1684244052Sken	sim = bus_softc->sim;
1685244052Sken
1686245228Sken	mtx_assert(sim->mtx, MA_OWNED);
1687245228Sken
1688229997Sken	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1689229997Sken		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1690229997Sken	if (status != CAM_REQ_CMP) {
1691229997Sken		printf("%s: unable to create path!\n", __func__);
1692229997Sken		return;
1693229997Sken	}
1694245228Sken	ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO);
1695245228Sken	if (ccb == NULL) {
1696245228Sken		printf("%s: unable to malloc CCB!\n", __func__);
1697245228Sken		xpt_free_path(path);
1698245228Sken		return;
1699245228Sken	}
1700242174Smav	xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1701229997Sken
1702229997Sken	/*
1703229997Sken	 * Copan WWN format:
1704229997Sken	 *
1705229997Sken	 * Bits 63-60:	0x5		NAA, IEEE registered name
1706229997Sken	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
1707229997Sken	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
1708229997Sken	 * Bits 11-8:			Type of port:
1709229997Sken	 *					1 == N-Port
1710229997Sken	 *					2 == F-Port
1711229997Sken	 *					3 == NL-Port
1712229997Sken	 * Bits 7-0:			0 == Node Name, >0 == Port Number
1713229997Sken	 */
1714229997Sken
1715229997Sken	if (online != 0) {
1716229997Sken
1717229997Sken		ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1718229997Sken
1719229997Sken
1720229997Sken		xpt_action(ccb);
1721229997Sken
1722229997Sken
1723229997Sken		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1724229997Sken#ifdef RANDOM_WWNN
1725229997Sken			uint64_t random_bits;
1726229997Sken#endif
1727229997Sken
1728229997Sken			printf("%s: %s current WWNN %#jx\n", __func__,
1729229997Sken			       bus_softc->port_name,
1730229997Sken			       ccb->knob.xport_specific.fc.wwnn);
1731229997Sken			printf("%s: %s current WWPN %#jx\n", __func__,
1732229997Sken			       bus_softc->port_name,
1733229997Sken			       ccb->knob.xport_specific.fc.wwpn);
1734229997Sken
1735229997Sken#ifdef RANDOM_WWNN
1736229997Sken			arc4rand(&random_bits, sizeof(random_bits), 0);
1737229997Sken#endif
1738229997Sken
1739229997Sken			/*
1740229997Sken			 * XXX KDM this is a bit of a kludge for now.  We
1741229997Sken			 * take the current WWNN/WWPN from the card, and
1742229997Sken			 * replace the company identifier and the NL-Port
1743229997Sken			 * indicator and the port number (for the WWPN).
1744229997Sken			 * This should be replaced later with ddb_GetWWNN,
1745229997Sken			 * or possibly a more centralized scheme.  (It
1746229997Sken			 * would be nice to have the WWNN/WWPN for each
1747229997Sken			 * port stored in the ctl_frontend structure.)
1748229997Sken			 */
1749229997Sken#ifdef RANDOM_WWNN
1750229997Sken			ccb->knob.xport_specific.fc.wwnn =
1751229997Sken				(random_bits &
1752229997Sken				0x0000000fffffff00ULL) |
1753229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1754229997Sken				/* NL-Port */    0x0300;
1755229997Sken			ccb->knob.xport_specific.fc.wwpn =
1756229997Sken				(random_bits &
1757229997Sken				0x0000000fffffff00ULL) |
1758229997Sken				/* Company ID */ 0x5000ED5000000000ULL |
1759229997Sken				/* NL-Port */    0x3000 |
1760229997Sken				/* Port Num */ (bus_softc->fe.targ_port & 0xff);
1761229997Sken
1762229997Sken			/*
1763229997Sken			 * This is a bit of an API break/reversal, but if
1764229997Sken			 * we're doing the random WWNN that's a little
1765229997Sken			 * different anyway.  So record what we're actually
1766229997Sken			 * using with the frontend code so it's reported
1767229997Sken			 * accurately.
1768229997Sken			 */
1769229997Sken			bus_softc->fe.wwnn =
1770229997Sken				ccb->knob.xport_specific.fc.wwnn;
1771229997Sken			bus_softc->fe.wwpn =
1772229997Sken				ccb->knob.xport_specific.fc.wwpn;
1773229997Sken			set_wwnn = 1;
1774229997Sken#else /* RANDOM_WWNN */
1775229997Sken			/*
1776229997Sken			 * If the user has specified a WWNN/WWPN, send them
1777229997Sken			 * down to the SIM.  Otherwise, record what the SIM
1778229997Sken			 * has reported.
1779229997Sken			 */
1780229997Sken			if ((bus_softc->fe.wwnn != 0)
1781229997Sken			 && (bus_softc->fe.wwpn != 0)) {
1782229997Sken				ccb->knob.xport_specific.fc.wwnn =
1783229997Sken					bus_softc->fe.wwnn;
1784229997Sken				ccb->knob.xport_specific.fc.wwpn =
1785229997Sken					bus_softc->fe.wwpn;
1786229997Sken				set_wwnn = 1;
1787229997Sken			} else {
1788229997Sken				bus_softc->fe.wwnn =
1789229997Sken					ccb->knob.xport_specific.fc.wwnn;
1790229997Sken				bus_softc->fe.wwpn =
1791229997Sken					ccb->knob.xport_specific.fc.wwpn;
1792229997Sken			}
1793229997Sken#endif /* RANDOM_WWNN */
1794229997Sken
1795229997Sken
1796229997Sken			if (set_wwnn != 0) {
1797229997Sken				printf("%s: %s new WWNN %#jx\n", __func__,
1798229997Sken				       bus_softc->port_name,
1799229997Sken				ccb->knob.xport_specific.fc.wwnn);
1800229997Sken				printf("%s: %s new WWPN %#jx\n", __func__,
1801229997Sken				       bus_softc->port_name,
1802229997Sken				       ccb->knob.xport_specific.fc.wwpn);
1803229997Sken			}
1804229997Sken		} else {
1805229997Sken			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1806229997Sken			       bus_softc->port_name);
1807229997Sken		}
1808229997Sken	}
1809229997Sken	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1810229997Sken	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1811229997Sken	if (set_wwnn != 0)
1812229997Sken		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1813229997Sken
1814229997Sken	if (online != 0)
1815229997Sken		ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1816229997Sken	else
1817229997Sken		ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1818229997Sken
1819229997Sken	xpt_action(ccb);
1820229997Sken
1821229997Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1822229997Sken		printf("%s: SIM %s (path id %d) target %s failed with "
1823229997Sken		       "status %#x\n",
1824229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1825229997Sken		       (online != 0) ? "enable" : "disable",
1826229997Sken		       ccb->ccb_h.status);
1827229997Sken	} else {
1828229997Sken		printf("%s: SIM %s (path id %d) target %s succeeded\n",
1829229997Sken		       __func__, bus_softc->port_name, bus_softc->path_id,
1830229997Sken		       (online != 0) ? "enable" : "disable");
1831229997Sken	}
1832229997Sken
1833229997Sken	xpt_free_path(path);
1834229997Sken
1835244016Sken	free(ccb, M_TEMP);
1836244016Sken
1837229997Sken	return;
1838229997Sken}
1839229997Sken
1840229997Skenstatic void
1841229997Skenctlfe_online(void *arg)
1842229997Sken{
1843245228Sken	struct ctlfe_softc *bus_softc;
1844245228Sken	struct cam_path *path;
1845245228Sken	cam_status status;
1846245228Sken	struct ctlfe_lun_softc *lun_softc;
1847245228Sken	struct cam_sim *sim;
1848245228Sken
1849245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1850245228Sken	sim = bus_softc->sim;
1851245228Sken
1852245228Sken	CAM_SIM_LOCK(sim);
1853245228Sken
1854245228Sken	/*
1855245228Sken	 * Create the wildcard LUN before bringing the port online.
1856245228Sken	 */
1857245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1858245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1859245228Sken				 CAM_LUN_WILDCARD);
1860245228Sken	if (status != CAM_REQ_CMP) {
1861245228Sken		printf("%s: unable to create path for wildcard periph\n",
1862245228Sken				__func__);
1863245228Sken		CAM_SIM_UNLOCK(sim);
1864245228Sken		return;
1865245228Sken	}
1866245228Sken
1867245228Sken	lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
1868245228Sken			M_NOWAIT | M_ZERO);
1869245228Sken	if (lun_softc == NULL) {
1870245228Sken		xpt_print(path, "%s: unable to allocate softc for "
1871245228Sken				"wildcard periph\n", __func__);
1872245228Sken		xpt_free_path(path);
1873245228Sken		CAM_SIM_UNLOCK(sim);
1874245228Sken		return;
1875245228Sken	}
1876245228Sken
1877245228Sken	lun_softc->parent_softc = bus_softc;
1878245228Sken	lun_softc->flags |= CTLFE_LUN_WILDCARD;
1879245228Sken
1880245228Sken	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links);
1881245228Sken
1882245228Sken
1883245228Sken	status = cam_periph_alloc(ctlferegister,
1884245228Sken				  ctlfeoninvalidate,
1885245228Sken				  ctlfecleanup,
1886245228Sken				  ctlfestart,
1887245228Sken				  "ctl",
1888245228Sken				  CAM_PERIPH_BIO,
1889245228Sken				  path,
1890245228Sken				  ctlfeasync,
1891245228Sken				  0,
1892245228Sken				  lun_softc);
1893245228Sken
1894245228Sken	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1895245228Sken		const struct cam_status_entry *entry;
1896245228Sken
1897245228Sken		entry = cam_fetch_status_entry(status);
1898245228Sken
1899245228Sken		printf("%s: CAM error %s (%#x) returned from "
1900245228Sken		       "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1901245228Sken		       entry->status_text : "Unknown", status);
1902245228Sken	}
1903245228Sken
1904245228Sken	xpt_free_path(path);
1905245228Sken
1906229997Sken	ctlfe_onoffline(arg, /*online*/ 1);
1907245228Sken
1908245228Sken	CAM_SIM_UNLOCK(sim);
1909229997Sken}
1910229997Sken
1911229997Skenstatic void
1912229997Skenctlfe_offline(void *arg)
1913229997Sken{
1914245228Sken	struct ctlfe_softc *bus_softc;
1915245228Sken	struct cam_path *path;
1916245228Sken	cam_status status;
1917245228Sken	struct cam_periph *periph;
1918245228Sken	struct cam_sim *sim;
1919245228Sken
1920245228Sken	bus_softc = (struct ctlfe_softc *)arg;
1921245228Sken	sim = bus_softc->sim;
1922245228Sken
1923245228Sken	CAM_SIM_LOCK(sim);
1924245228Sken
1925229997Sken	ctlfe_onoffline(arg, /*online*/ 0);
1926245228Sken
1927245228Sken	/*
1928245228Sken	 * Disable the wildcard LUN for this port now that we have taken
1929245228Sken	 * the port offline.
1930245228Sken	 */
1931245228Sken	status = xpt_create_path(&path, /*periph*/ NULL,
1932245228Sken				 bus_softc->path_id, CAM_TARGET_WILDCARD,
1933245228Sken				 CAM_LUN_WILDCARD);
1934245228Sken	if (status != CAM_REQ_CMP) {
1935245228Sken		CAM_SIM_UNLOCK(sim);
1936245228Sken		printf("%s: unable to create path for wildcard periph\n",
1937245228Sken		       __func__);
1938245228Sken		return;
1939245228Sken	}
1940245228Sken
1941245228Sken
1942245228Sken	if ((periph = cam_periph_find(path, "ctl")) != NULL)
1943245228Sken		cam_periph_invalidate(periph);
1944245228Sken
1945245228Sken	xpt_free_path(path);
1946245228Sken
1947245228Sken	CAM_SIM_UNLOCK(sim);
1948229997Sken}
1949229997Sken
1950229997Skenstatic int
1951229997Skenctlfe_targ_enable(void *arg, struct ctl_id targ_id)
1952229997Sken{
1953229997Sken	return (0);
1954229997Sken}
1955229997Sken
1956229997Skenstatic int
1957229997Skenctlfe_targ_disable(void *arg, struct ctl_id targ_id)
1958229997Sken{
1959229997Sken	return (0);
1960229997Sken}
1961229997Sken
1962229997Sken/*
1963229997Sken * This will get called to enable a LUN on every bus that is attached to
1964229997Sken * CTL.  So we only need to create a path/periph for this particular bus.
1965229997Sken */
1966229997Skenstatic int
1967229997Skenctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1968229997Sken{
1969229997Sken	struct ctlfe_softc *bus_softc;
1970229997Sken	struct ctlfe_lun_softc *softc;
1971229997Sken	struct cam_path *path;
1972229997Sken	struct cam_periph *periph;
1973229997Sken	struct cam_sim *sim;
1974229997Sken	cam_status status;
1975229997Sken
1976229997Sken	bus_softc = (struct ctlfe_softc *)arg;
1977244052Sken	sim = bus_softc->sim;
1978229997Sken
1979245228Sken	status = xpt_create_path_unlocked(&path, /*periph*/ NULL,
1980245228Sken					  bus_softc->path_id,
1981245228Sken					  targ_id.id, lun_id);
1982229997Sken	/* XXX KDM need some way to return status to CTL here? */
1983229997Sken	if (status != CAM_REQ_CMP) {
1984229997Sken		printf("%s: could not create path, status %#x\n", __func__,
1985229997Sken		       status);
1986229997Sken		return (1);
1987229997Sken	}
1988229997Sken
1989229997Sken	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1990244052Sken	CAM_SIM_LOCK(sim);
1991229997Sken	periph = cam_periph_find(path, "ctl");
1992229997Sken	if (periph != NULL) {
1993229997Sken		/* We've already got a periph, no need to alloc a new one. */
1994229997Sken		xpt_free_path(path);
1995229997Sken		free(softc, M_CTLFE);
1996244052Sken		CAM_SIM_UNLOCK(sim);
1997229997Sken		return (0);
1998229997Sken	}
1999229997Sken
2000229997Sken	softc->parent_softc = bus_softc;
2001229997Sken	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
2002229997Sken
2003229997Sken	status = cam_periph_alloc(ctlferegister,
2004229997Sken				  ctlfeoninvalidate,
2005229997Sken				  ctlfecleanup,
2006229997Sken				  ctlfestart,
2007229997Sken				  "ctl",
2008229997Sken				  CAM_PERIPH_BIO,
2009229997Sken				  path,
2010229997Sken				  ctlfeasync,
2011229997Sken				  0,
2012229997Sken				  softc);
2013229997Sken
2014244016Sken	xpt_free_path(path);
2015244016Sken
2016244052Sken	CAM_SIM_UNLOCK(sim);
2017229997Sken
2018229997Sken	return (0);
2019229997Sken}
2020229997Sken
2021229997Sken/*
2022245228Sken * This will get called when the user removes a LUN to disable that LUN
2023245228Sken * on every bus that is attached to CTL.
2024229997Sken */
2025229997Skenstatic int
2026229997Skenctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
2027229997Sken{
2028229997Sken	struct ctlfe_softc *softc;
2029229997Sken	struct ctlfe_lun_softc *lun_softc;
2030245228Sken	struct cam_sim *sim;
2031229997Sken
2032229997Sken	softc = (struct ctlfe_softc *)arg;
2033245228Sken	sim = softc->sim;
2034229997Sken
2035245228Sken	CAM_SIM_LOCK(sim);
2036229997Sken	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
2037229997Sken		struct cam_path *path;
2038229997Sken
2039229997Sken		path = lun_softc->periph->path;
2040229997Sken
2041229997Sken		if ((xpt_path_target_id(path) == targ_id.id)
2042229997Sken		 && (xpt_path_lun_id(path) == lun_id)) {
2043229997Sken			break;
2044229997Sken		}
2045229997Sken	}
2046229997Sken	if (lun_softc == NULL) {
2047245228Sken		CAM_SIM_UNLOCK(sim);
2048229997Sken		printf("%s: can't find target %d lun %d\n", __func__,
2049229997Sken		       targ_id.id, lun_id);
2050229997Sken		return (1);
2051229997Sken	}
2052229997Sken
2053229997Sken	cam_periph_invalidate(lun_softc->periph);
2054229997Sken
2055245228Sken	CAM_SIM_UNLOCK(sim);
2056229997Sken
2057229997Sken	return (0);
2058229997Sken}
2059229997Sken
2060229997Skenstatic void
2061229997Skenctlfe_dump_sim(struct cam_sim *sim)
2062229997Sken{
2063229997Sken
2064229997Sken	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
2065229997Sken	       sim->sim_name, sim->unit_number,
2066229997Sken	       sim->max_tagged_dev_openings, sim->max_dev_openings);
2067229997Sken	printf("%s%d: max_ccbs: %u, ccb_count: %u\n",
2068229997Sken	       sim->sim_name, sim->unit_number,
2069229997Sken	       sim->max_ccbs, sim->ccb_count);
2070229997Sken	printf("%s%d: ccb_freeq is %sempty\n",
2071229997Sken	       sim->sim_name, sim->unit_number,
2072229997Sken	       (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT ");
2073229997Sken	printf("\n");
2074229997Sken}
2075229997Sken
2076229997Sken/*
2077229997Sken * Assumes that the SIM lock is held.
2078229997Sken */
2079229997Skenstatic void
2080229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc)
2081229997Sken{
2082229997Sken	struct ccb_hdr *hdr;
2083229997Sken	struct cam_periph *periph;
2084229997Sken	int num_items;
2085229997Sken
2086229997Sken	periph = softc->periph;
2087229997Sken	num_items = 0;
2088229997Sken
2089229997Sken	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
2090229997Sken		union ctl_io *io;
2091229997Sken
2092229997Sken		io = hdr->io_ptr;
2093229997Sken
2094229997Sken		num_items++;
2095229997Sken
2096229997Sken		/*
2097229997Sken		 * This can happen when we get an ATIO but can't allocate
2098229997Sken		 * a ctl_io.  See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
2099229997Sken		 */
2100229997Sken		if (io == NULL) {
2101229997Sken			struct ccb_scsiio *csio;
2102229997Sken
2103229997Sken			csio = (struct ccb_scsiio *)hdr;
2104229997Sken
2105229997Sken			xpt_print(periph->path, "CCB %#x ctl_io allocation "
2106229997Sken				  "failed\n", csio->tag_id);
2107229997Sken			continue;
2108229997Sken		}
2109229997Sken
2110229997Sken		/*
2111229997Sken		 * Only regular SCSI I/O is put on the work
2112229997Sken		 * queue, so we can print sense here.  There may be no
2113229997Sken		 * sense if it's no the queue for a DMA, but this serves to
2114229997Sken		 * print out the CCB as well.
2115229997Sken		 *
2116229997Sken		 * XXX KDM switch this over to scsi_sense_print() when
2117229997Sken		 * CTL is merged in with CAM.
2118229997Sken		 */
2119229997Sken		ctl_io_error_print(io, NULL);
2120229997Sken
2121229997Sken		/*
2122229997Sken		 * We're sending status back to the
2123229997Sken		 * initiator, so we're on the queue waiting
2124229997Sken		 * for a CTIO to do that.
2125229997Sken		 */
2126229997Sken		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2127229997Sken			continue;
2128229997Sken
2129229997Sken		/*
2130229997Sken		 * Otherwise, we're on the queue waiting to
2131229997Sken		 * do a data transfer.
2132229997Sken		 */
2133229997Sken		xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
2134229997Sken			  io->scsiio.kern_total_len, io->scsiio.kern_data_len,
2135229997Sken			  io->scsiio.kern_data_resid);
2136229997Sken	}
2137229997Sken
2138229997Sken	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
2139229997Sken		  num_items);
2140250460Seadler	xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
2141229997Sken		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
2142229997Sken		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
2143229997Sken		  (uintmax_t)softc->ccbs_freed);
2144229997Sken	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
2145229997Sken		  "returned\n", (uintmax_t)(softc->ctios_sent -
2146229997Sken		  softc->ctios_returned), softc->ctios_sent,
2147229997Sken		  softc->ctios_returned);
2148229997Sken}
2149229997Sken
2150229997Sken/*
2151229997Sken * This function is called when we fail to get a CCB for a DMA or status return
2152229997Sken * to the initiator within the specified time period.
2153229997Sken *
2154229997Sken * The callout code should insure that we hold the sim mutex here.
2155229997Sken */
2156229997Skenstatic void
2157229997Skenctlfe_dma_timeout(void *arg)
2158229997Sken{
2159229997Sken	struct ctlfe_lun_softc *softc;
2160229997Sken	struct cam_periph *periph;
2161229997Sken	struct cam_sim *sim;
2162229997Sken	int num_queued;
2163229997Sken
2164229997Sken	softc = (struct ctlfe_lun_softc *)arg;
2165229997Sken	periph = softc->periph;
2166229997Sken	sim = xpt_path_sim(periph->path);
2167229997Sken	num_queued = 0;
2168229997Sken
2169229997Sken	/*
2170229997Sken	 * Nothing to do...
2171229997Sken	 */
2172229997Sken	if (TAILQ_FIRST(&softc->work_queue) == NULL) {
2173229997Sken		xpt_print(periph->path, "TIMEOUT triggered after %d "
2174229997Sken			  "seconds, but nothing on work queue??\n",
2175229997Sken			  CTLFE_DMA_TIMEOUT);
2176229997Sken		return;
2177229997Sken	}
2178229997Sken
2179229997Sken	xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
2180229997Sken		  "start\n", CTLFE_DMA_TIMEOUT);
2181229997Sken
2182229997Sken	ctlfe_dump_queue(softc);
2183229997Sken
2184229997Sken	ctlfe_dump_sim(sim);
2185229997Sken
2186229997Sken	xpt_print(periph->path, "calling xpt_schedule() to attempt to "
2187229997Sken		  "unstick our queue\n");
2188229997Sken
2189229997Sken	xpt_schedule(periph, /*priority*/ 1);
2190229997Sken
2191229997Sken	xpt_print(periph->path, "xpt_schedule() call complete\n");
2192229997Sken}
2193229997Sken
2194229997Sken/*
2195229997Sken * Datamove/done routine called by CTL.  Put ourselves on the queue to
2196229997Sken * receive a CCB from CAM so we can queue the continue I/O request down
2197229997Sken * to the adapter.
2198229997Sken */
2199229997Skenstatic void
2200229997Skenctlfe_datamove_done(union ctl_io *io)
2201229997Sken{
2202229997Sken	union ccb *ccb;
2203229997Sken	struct cam_sim *sim;
2204229997Sken	struct cam_periph *periph;
2205229997Sken	struct ctlfe_lun_softc *softc;
2206229997Sken
2207229997Sken	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2208229997Sken
2209229997Sken	sim = xpt_path_sim(ccb->ccb_h.path);
2210229997Sken
2211245228Sken	CAM_SIM_LOCK(sim);
2212229997Sken
2213229997Sken	periph = xpt_path_periph(ccb->ccb_h.path);
2214229997Sken
2215229997Sken	softc = (struct ctlfe_lun_softc *)periph->softc;
2216229997Sken
2217229997Sken	if (io->io_hdr.io_type == CTL_IO_TASK) {
2218229997Sken		/*
2219229997Sken		 * Task management commands don't require any further
2220229997Sken		 * communication back to the adapter.  Requeue the CCB
2221229997Sken		 * to the adapter, and free the CTL I/O.
2222229997Sken		 */
2223229997Sken		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2224229997Sken			  "tag %#x seq %#x\n", __func__,
2225229997Sken			  ccb->cin1.tag_id, ccb->cin1.seq_id);
2226229997Sken		/*
2227229997Sken		 * Send the notify acknowledge down to the SIM, to let it
2228229997Sken		 * know we processed the task management command.
2229229997Sken		 */
2230229997Sken		ccb->ccb_h.status = CAM_REQ_INPROG;
2231229997Sken		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2232229997Sken		xpt_action(ccb);
2233229997Sken		ctl_free_io(io);
2234229997Sken	} else {
2235229997Sken		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2236229997Sken			io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2237229997Sken		else
2238229997Sken			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2239229997Sken
2240229997Sken		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2241229997Sken				  periph_links.tqe);
2242229997Sken
2243229997Sken		/*
2244229997Sken		 * Reset the timeout for our latest active DMA.
2245229997Sken		 */
2246229997Sken		callout_reset(&softc->dma_callout,
2247229997Sken			      CTLFE_DMA_TIMEOUT * hz,
2248229997Sken			      ctlfe_dma_timeout, softc);
2249229997Sken		/*
2250229997Sken		 * Ask for the CAM transport layer to send us a CCB to do
2251229997Sken		 * the DMA or send status, unless ctlfe_dma_enabled is set
2252229997Sken		 * to 0.
2253229997Sken		 */
2254229997Sken		if (ctlfe_dma_enabled != 0)
2255229997Sken			xpt_schedule(periph, /*priority*/ 1);
2256229997Sken	}
2257229997Sken
2258245228Sken	CAM_SIM_UNLOCK(sim);
2259229997Sken}
2260229997Sken
2261229997Skenstatic void
2262229997Skenctlfe_dump(void)
2263229997Sken{
2264229997Sken	struct ctlfe_softc *bus_softc;
2265229997Sken
2266229997Sken	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2267229997Sken		struct ctlfe_lun_softc *lun_softc;
2268229997Sken
2269229997Sken		ctlfe_dump_sim(bus_softc->sim);
2270229997Sken
2271229997Sken		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
2272229997Sken			ctlfe_dump_queue(lun_softc);
2273229997Sken		}
2274229997Sken	}
2275229997Sken}
2276