xref: /freebsd/sys/dev/mpr/mpr_sas.c (revision a2386b6f6a872873609354f6099ae97f2de5356b)
1991554f2SKenneth D. Merry /*-
2991554f2SKenneth D. Merry  * Copyright (c) 2009 Yahoo! Inc.
3a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
47a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
546b23587SKashyap D Desai  * Copyright 2000-2020 Broadcom Inc.
6991554f2SKenneth D. Merry  * All rights reserved.
7991554f2SKenneth D. Merry  *
8991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
9991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
10991554f2SKenneth D. Merry  * are met:
11991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
12991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
13991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
14991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
15991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
16991554f2SKenneth D. Merry  *
17991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27991554f2SKenneth D. Merry  * SUCH DAMAGE.
28a2c14879SStephen McConnell  *
2946b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
30a2c14879SStephen McConnell  *
31991554f2SKenneth D. Merry  */
32991554f2SKenneth D. Merry 
33991554f2SKenneth D. Merry #include <sys/cdefs.h>
34991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
35991554f2SKenneth D. Merry 
36a2c14879SStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
37991554f2SKenneth D. Merry 
38991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
39991554f2SKenneth D. Merry #include <sys/types.h>
40991554f2SKenneth D. Merry #include <sys/param.h>
41991554f2SKenneth D. Merry #include <sys/systm.h>
42991554f2SKenneth D. Merry #include <sys/kernel.h>
43991554f2SKenneth D. Merry #include <sys/selinfo.h>
44991554f2SKenneth D. Merry #include <sys/module.h>
45991554f2SKenneth D. Merry #include <sys/bus.h>
46991554f2SKenneth D. Merry #include <sys/conf.h>
47991554f2SKenneth D. Merry #include <sys/bio.h>
48991554f2SKenneth D. Merry #include <sys/malloc.h>
49991554f2SKenneth D. Merry #include <sys/uio.h>
50991554f2SKenneth D. Merry #include <sys/sysctl.h>
51991554f2SKenneth D. Merry #include <sys/endian.h>
52991554f2SKenneth D. Merry #include <sys/queue.h>
53991554f2SKenneth D. Merry #include <sys/kthread.h>
54991554f2SKenneth D. Merry #include <sys/taskqueue.h>
55991554f2SKenneth D. Merry #include <sys/sbuf.h>
56991554f2SKenneth D. Merry 
57991554f2SKenneth D. Merry #include <machine/bus.h>
58991554f2SKenneth D. Merry #include <machine/resource.h>
59991554f2SKenneth D. Merry #include <sys/rman.h>
60991554f2SKenneth D. Merry 
61991554f2SKenneth D. Merry #include <machine/stdarg.h>
62991554f2SKenneth D. Merry 
63991554f2SKenneth D. Merry #include <cam/cam.h>
64991554f2SKenneth D. Merry #include <cam/cam_ccb.h>
65991554f2SKenneth D. Merry #include <cam/cam_debug.h>
66991554f2SKenneth D. Merry #include <cam/cam_sim.h>
67991554f2SKenneth D. Merry #include <cam/cam_xpt_sim.h>
68991554f2SKenneth D. Merry #include <cam/cam_xpt_periph.h>
69991554f2SKenneth D. Merry #include <cam/cam_periph.h>
70991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
71991554f2SKenneth D. Merry #include <cam/scsi/scsi_message.h>
72991554f2SKenneth D. Merry #include <cam/scsi/smp_all.h>
73991554f2SKenneth D. Merry 
7467feec50SStephen McConnell #include <dev/nvme/nvme.h>
7567feec50SStephen McConnell 
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
77991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
8067feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
82991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
83991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
84991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
85991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
86991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
87991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
88991554f2SKenneth D. Merry 
89991554f2SKenneth D. Merry #define MPRSAS_DISCOVERY_TIMEOUT	20
90991554f2SKenneth D. Merry #define MPRSAS_MAX_DISCOVERY_TIMEOUTS	10 /* 200 seconds */
91991554f2SKenneth D. Merry 
92991554f2SKenneth D. Merry /*
93991554f2SKenneth D. Merry  * static array to check SCSI OpCode for EEDP protection bits
94991554f2SKenneth D. Merry  */
95991554f2SKenneth D. Merry #define	PRO_R MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
96991554f2SKenneth D. Merry #define	PRO_W MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
97991554f2SKenneth D. Merry #define	PRO_V MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
98991554f2SKenneth D. Merry static uint8_t op_code_prot[256] = {
99991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
102991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103991554f2SKenneth D. Merry 	0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
108991554f2SKenneth D. Merry 	0, 0, 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
110991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114991554f2SKenneth D. Merry 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
115991554f2SKenneth D. Merry };
116991554f2SKenneth D. Merry 
117991554f2SKenneth D. Merry MALLOC_DEFINE(M_MPRSAS, "MPRSAS", "MPR SAS memory");
118991554f2SKenneth D. Merry 
119991554f2SKenneth D. Merry static void mprsas_remove_device(struct mpr_softc *, struct mpr_command *);
120991554f2SKenneth D. Merry static void mprsas_remove_complete(struct mpr_softc *, struct mpr_command *);
121991554f2SKenneth D. Merry static void mprsas_action(struct cam_sim *sim, union ccb *ccb);
122991554f2SKenneth D. Merry static void mprsas_poll(struct cam_sim *sim);
123991554f2SKenneth D. Merry static void mprsas_scsiio_timeout(void *data);
1247a2a6a1aSStephen McConnell static void mprsas_abort_complete(struct mpr_softc *sc, struct mpr_command *cm);
125991554f2SKenneth D. Merry static void mprsas_action_scsiio(struct mprsas_softc *, union ccb *);
126991554f2SKenneth D. Merry static void mprsas_scsiio_complete(struct mpr_softc *, struct mpr_command *);
127991554f2SKenneth D. Merry static void mprsas_action_resetdev(struct mprsas_softc *, union ccb *);
1287a2a6a1aSStephen McConnell static void mprsas_resetdev_complete(struct mpr_softc *, struct mpr_command *);
129991554f2SKenneth D. Merry static int mprsas_send_abort(struct mpr_softc *sc, struct mpr_command *tm,
130991554f2SKenneth D. Merry     struct mpr_command *cm);
131991554f2SKenneth D. Merry static void mprsas_async(void *callback_arg, uint32_t code,
132991554f2SKenneth D. Merry     struct cam_path *path, void *arg);
133991554f2SKenneth D. Merry static int mprsas_send_portenable(struct mpr_softc *sc);
134991554f2SKenneth D. Merry static void mprsas_portenable_complete(struct mpr_softc *sc,
135991554f2SKenneth D. Merry     struct mpr_command *cm);
136991554f2SKenneth D. Merry 
1377a2a6a1aSStephen McConnell static void mprsas_smpio_complete(struct mpr_softc *sc, struct mpr_command *cm);
1387a2a6a1aSStephen McConnell static void mprsas_send_smpcmd(struct mprsas_softc *sassc, union ccb *ccb,
1397a2a6a1aSStephen McConnell     uint64_t sasaddr);
140a2c14879SStephen McConnell static void mprsas_action_smpio(struct mprsas_softc *sassc, union ccb *ccb);
141991554f2SKenneth D. Merry 
142991554f2SKenneth D. Merry struct mprsas_target *
143991554f2SKenneth D. Merry mprsas_find_target_by_handle(struct mprsas_softc *sassc, int start,
144991554f2SKenneth D. Merry     uint16_t handle)
145991554f2SKenneth D. Merry {
146991554f2SKenneth D. Merry 	struct mprsas_target *target;
147991554f2SKenneth D. Merry 	int i;
148991554f2SKenneth D. Merry 
149991554f2SKenneth D. Merry 	for (i = start; i < sassc->maxtargets; i++) {
150991554f2SKenneth D. Merry 		target = &sassc->targets[i];
151991554f2SKenneth D. Merry 		if (target->handle == handle)
152991554f2SKenneth D. Merry 			return (target);
153991554f2SKenneth D. Merry 	}
154991554f2SKenneth D. Merry 
155991554f2SKenneth D. Merry 	return (NULL);
156991554f2SKenneth D. Merry }
157991554f2SKenneth D. Merry 
158991554f2SKenneth D. Merry /* we need to freeze the simq during attach and diag reset, to avoid failing
159991554f2SKenneth D. Merry  * commands before device handles have been found by discovery.  Since
160991554f2SKenneth D. Merry  * discovery involves reading config pages and possibly sending commands,
161991554f2SKenneth D. Merry  * discovery actions may continue even after we receive the end of discovery
162991554f2SKenneth D. Merry  * event, so refcount discovery actions instead of assuming we can unfreeze
163991554f2SKenneth D. Merry  * the simq when we get the event.
164991554f2SKenneth D. Merry  */
165991554f2SKenneth D. Merry void
166991554f2SKenneth D. Merry mprsas_startup_increment(struct mprsas_softc *sassc)
167991554f2SKenneth D. Merry {
168991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
169991554f2SKenneth D. Merry 
170991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) != 0) {
171991554f2SKenneth D. Merry 		if (sassc->startup_refcount++ == 0) {
172991554f2SKenneth D. Merry 			/* just starting, freeze the simq */
173991554f2SKenneth D. Merry 			mpr_dprint(sassc->sc, MPR_INIT,
174991554f2SKenneth D. Merry 			    "%s freezing simq\n", __func__);
175991554f2SKenneth D. Merry 			xpt_hold_boot();
176991554f2SKenneth D. Merry 			xpt_freeze_simq(sassc->sim, 1);
177991554f2SKenneth D. Merry 		}
178991554f2SKenneth D. Merry 		mpr_dprint(sassc->sc, MPR_INIT, "%s refcount %u\n", __func__,
179991554f2SKenneth D. Merry 		    sassc->startup_refcount);
180991554f2SKenneth D. Merry 	}
181991554f2SKenneth D. Merry }
182991554f2SKenneth D. Merry 
183991554f2SKenneth D. Merry void
184991554f2SKenneth D. Merry mprsas_release_simq_reinit(struct mprsas_softc *sassc)
185991554f2SKenneth D. Merry {
186991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
187991554f2SKenneth D. Merry 		sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
188991554f2SKenneth D. Merry 		xpt_release_simq(sassc->sim, 1);
189991554f2SKenneth D. Merry 		mpr_dprint(sassc->sc, MPR_INFO, "Unfreezing SIM queue\n");
190991554f2SKenneth D. Merry 	}
191991554f2SKenneth D. Merry }
192991554f2SKenneth D. Merry 
193991554f2SKenneth D. Merry void
194991554f2SKenneth D. Merry mprsas_startup_decrement(struct mprsas_softc *sassc)
195991554f2SKenneth D. Merry {
196991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
197991554f2SKenneth D. Merry 
198991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) != 0) {
199991554f2SKenneth D. Merry 		if (--sassc->startup_refcount == 0) {
200991554f2SKenneth D. Merry 			/* finished all discovery-related actions, release
201991554f2SKenneth D. Merry 			 * the simq and rescan for the latest topology.
202991554f2SKenneth D. Merry 			 */
203991554f2SKenneth D. Merry 			mpr_dprint(sassc->sc, MPR_INIT,
204991554f2SKenneth D. Merry 			    "%s releasing simq\n", __func__);
205991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_STARTUP;
206991554f2SKenneth D. Merry 			xpt_release_simq(sassc->sim, 1);
207991554f2SKenneth D. Merry 			xpt_release_boot();
208991554f2SKenneth D. Merry 		}
209991554f2SKenneth D. Merry 		mpr_dprint(sassc->sc, MPR_INIT, "%s refcount %u\n", __func__,
210991554f2SKenneth D. Merry 		    sassc->startup_refcount);
211991554f2SKenneth D. Merry 	}
212991554f2SKenneth D. Merry }
213991554f2SKenneth D. Merry 
214b7f1ee79SScott Long /*
215b7f1ee79SScott Long  * The firmware requires us to stop sending commands when we're doing task
216b7f1ee79SScott Long  * management.
217991554f2SKenneth D. Merry  * use.
218b7f1ee79SScott Long  * XXX The logic for serializing the device has been made lazy and moved to
219b7f1ee79SScott Long  * mprsas_prepare_for_tm().
220991554f2SKenneth D. Merry  */
221991554f2SKenneth D. Merry struct mpr_command *
222991554f2SKenneth D. Merry mprsas_alloc_tm(struct mpr_softc *sc)
223991554f2SKenneth D. Merry {
22446b9415fSScott Long 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
225991554f2SKenneth D. Merry 	struct mpr_command *tm;
226991554f2SKenneth D. Merry 
227991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
228991554f2SKenneth D. Merry 	tm = mpr_alloc_high_priority_command(sc);
22946b9415fSScott Long 	if (tm == NULL)
23046b9415fSScott Long 		return (NULL);
23146b9415fSScott Long 
23246b9415fSScott Long 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
23346b9415fSScott Long 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
234991554f2SKenneth D. Merry 	return tm;
235991554f2SKenneth D. Merry }
236991554f2SKenneth D. Merry 
237991554f2SKenneth D. Merry void
238991554f2SKenneth D. Merry mprsas_free_tm(struct mpr_softc *sc, struct mpr_command *tm)
239991554f2SKenneth D. Merry {
24088619392SStephen McConnell 	int target_id = 0xFFFFFFFF;
24188619392SStephen McConnell 
242a2c14879SStephen McConnell 	MPR_FUNCTRACE(sc);
243991554f2SKenneth D. Merry 	if (tm == NULL)
244991554f2SKenneth D. Merry 		return;
245991554f2SKenneth D. Merry 
246a2c14879SStephen McConnell 	/*
247a2c14879SStephen McConnell 	 * For TM's the devq is frozen for the device.  Unfreeze it here and
248a2c14879SStephen McConnell 	 * free the resources used for freezing the devq.  Must clear the
249a2c14879SStephen McConnell 	 * INRESET flag as well or scsi I/O will not work.
250991554f2SKenneth D. Merry 	 */
251a2c14879SStephen McConnell 	if (tm->cm_targ != NULL) {
252a2c14879SStephen McConnell 		tm->cm_targ->flags &= ~MPRSAS_TARGET_INRESET;
25388619392SStephen McConnell 		target_id = tm->cm_targ->tid;
254991554f2SKenneth D. Merry 	}
255a2c14879SStephen McConnell 	if (tm->cm_ccb) {
256a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "Unfreezing devq for target ID %d\n",
25788619392SStephen McConnell 		    target_id);
258a2c14879SStephen McConnell 		xpt_release_devq(tm->cm_ccb->ccb_h.path, 1, TRUE);
259a2c14879SStephen McConnell 		xpt_free_path(tm->cm_ccb->ccb_h.path);
260a2c14879SStephen McConnell 		xpt_free_ccb(tm->cm_ccb);
261a2c14879SStephen McConnell 	}
262991554f2SKenneth D. Merry 
263991554f2SKenneth D. Merry 	mpr_free_high_priority_command(sc, tm);
264991554f2SKenneth D. Merry }
265991554f2SKenneth D. Merry 
266991554f2SKenneth D. Merry void
267991554f2SKenneth D. Merry mprsas_rescan_target(struct mpr_softc *sc, struct mprsas_target *targ)
268991554f2SKenneth D. Merry {
269991554f2SKenneth D. Merry 	struct mprsas_softc *sassc = sc->sassc;
270991554f2SKenneth D. Merry 	path_id_t pathid;
271991554f2SKenneth D. Merry 	target_id_t targetid;
272991554f2SKenneth D. Merry 	union ccb *ccb;
273991554f2SKenneth D. Merry 
274991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
275991554f2SKenneth D. Merry 	pathid = cam_sim_path(sassc->sim);
276991554f2SKenneth D. Merry 	if (targ == NULL)
277991554f2SKenneth D. Merry 		targetid = CAM_TARGET_WILDCARD;
278991554f2SKenneth D. Merry 	else
279991554f2SKenneth D. Merry 		targetid = targ - sassc->targets;
280991554f2SKenneth D. Merry 
281991554f2SKenneth D. Merry 	/*
282991554f2SKenneth D. Merry 	 * Allocate a CCB and schedule a rescan.
283991554f2SKenneth D. Merry 	 */
284991554f2SKenneth D. Merry 	ccb = xpt_alloc_ccb_nowait();
285991554f2SKenneth D. Merry 	if (ccb == NULL) {
286991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unable to alloc CCB for rescan\n");
287991554f2SKenneth D. Merry 		return;
288991554f2SKenneth D. Merry 	}
289991554f2SKenneth D. Merry 
290a2c14879SStephen McConnell 	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
291a2c14879SStephen McConnell 	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
292991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unable to create path for rescan\n");
293991554f2SKenneth D. Merry 		xpt_free_ccb(ccb);
294991554f2SKenneth D. Merry 		return;
295991554f2SKenneth D. Merry 	}
296991554f2SKenneth D. Merry 
297991554f2SKenneth D. Merry 	if (targetid == CAM_TARGET_WILDCARD)
298991554f2SKenneth D. Merry 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
299991554f2SKenneth D. Merry 	else
300991554f2SKenneth D. Merry 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
301991554f2SKenneth D. Merry 
302991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s targetid %u\n", __func__, targetid);
303991554f2SKenneth D. Merry 	xpt_rescan(ccb);
304991554f2SKenneth D. Merry }
305991554f2SKenneth D. Merry 
306991554f2SKenneth D. Merry static void
307991554f2SKenneth D. Merry mprsas_log_command(struct mpr_command *cm, u_int level, const char *fmt, ...)
308991554f2SKenneth D. Merry {
309991554f2SKenneth D. Merry 	struct sbuf sb;
310991554f2SKenneth D. Merry 	va_list ap;
311*a2386b6fSAlexander Motin 	char str[224];
312991554f2SKenneth D. Merry 	char path_str[64];
313991554f2SKenneth D. Merry 
314991554f2SKenneth D. Merry 	if (cm == NULL)
315991554f2SKenneth D. Merry 		return;
316991554f2SKenneth D. Merry 
317991554f2SKenneth D. Merry 	/* No need to be in here if debugging isn't enabled */
318991554f2SKenneth D. Merry 	if ((cm->cm_sc->mpr_debug & level) == 0)
319991554f2SKenneth D. Merry 		return;
320991554f2SKenneth D. Merry 
321991554f2SKenneth D. Merry 	sbuf_new(&sb, str, sizeof(str), 0);
322991554f2SKenneth D. Merry 
323991554f2SKenneth D. Merry 	va_start(ap, fmt);
324991554f2SKenneth D. Merry 
325991554f2SKenneth D. Merry 	if (cm->cm_ccb != NULL) {
326991554f2SKenneth D. Merry 		xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str,
327991554f2SKenneth D. Merry 		    sizeof(path_str));
328991554f2SKenneth D. Merry 		sbuf_cat(&sb, path_str);
329991554f2SKenneth D. Merry 		if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) {
330991554f2SKenneth D. Merry 			scsi_command_string(&cm->cm_ccb->csio, &sb);
331991554f2SKenneth D. Merry 			sbuf_printf(&sb, "length %d ",
332991554f2SKenneth D. Merry 			    cm->cm_ccb->csio.dxfer_len);
333991554f2SKenneth D. Merry 		}
334991554f2SKenneth D. Merry 	} else {
335991554f2SKenneth D. Merry 		sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ",
336991554f2SKenneth D. Merry 		    cam_sim_name(cm->cm_sc->sassc->sim),
337991554f2SKenneth D. Merry 		    cam_sim_unit(cm->cm_sc->sassc->sim),
338991554f2SKenneth D. Merry 		    cam_sim_bus(cm->cm_sc->sassc->sim),
339991554f2SKenneth D. Merry 		    cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF,
340991554f2SKenneth D. Merry 		    cm->cm_lun);
341991554f2SKenneth D. Merry 	}
342991554f2SKenneth D. Merry 
343991554f2SKenneth D. Merry 	sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID);
344991554f2SKenneth D. Merry 	sbuf_vprintf(&sb, fmt, ap);
345991554f2SKenneth D. Merry 	sbuf_finish(&sb);
346c11c484fSScott Long 	mpr_print_field(cm->cm_sc, "%s", sbuf_data(&sb));
347991554f2SKenneth D. Merry 
348991554f2SKenneth D. Merry 	va_end(ap);
349991554f2SKenneth D. Merry }
350991554f2SKenneth D. Merry 
351991554f2SKenneth D. Merry static void
352991554f2SKenneth D. Merry mprsas_remove_volume(struct mpr_softc *sc, struct mpr_command *tm)
353991554f2SKenneth D. Merry {
354991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
355991554f2SKenneth D. Merry 	struct mprsas_target *targ;
356991554f2SKenneth D. Merry 	uint16_t handle;
357991554f2SKenneth D. Merry 
358991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
359991554f2SKenneth D. Merry 
360991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
361991554f2SKenneth D. Merry 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
362991554f2SKenneth D. Merry 	targ = tm->cm_targ;
363991554f2SKenneth D. Merry 
364991554f2SKenneth D. Merry 	if (reply == NULL) {
365991554f2SKenneth D. Merry 		/* XXX retry the remove after the diag reset completes? */
366991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply resetting device "
367991554f2SKenneth D. Merry 		    "0x%04x\n", __func__, handle);
368991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
369991554f2SKenneth D. Merry 		return;
370991554f2SKenneth D. Merry 	}
371991554f2SKenneth D. Merry 
372d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
373d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
37458581c13SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "IOCStatus = 0x%x while resetting "
375d3f6eabfSStephen McConnell 		    "device 0x%x\n", le16toh(reply->IOCStatus), handle);
376991554f2SKenneth D. Merry 	}
377991554f2SKenneth D. Merry 
378991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "Reset aborted %u commands\n",
379d3f6eabfSStephen McConnell 	    le32toh(reply->TerminationCount));
380991554f2SKenneth D. Merry 	mpr_free_reply(sc, tm->cm_reply_data);
381991554f2SKenneth D. Merry 	tm->cm_reply = NULL;	/* Ensures the reply won't get re-freed */
382991554f2SKenneth D. Merry 
383991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "clearing target %u handle 0x%04x\n",
384991554f2SKenneth D. Merry 	    targ->tid, handle);
385991554f2SKenneth D. Merry 
386991554f2SKenneth D. Merry 	/*
387991554f2SKenneth D. Merry 	 * Don't clear target if remove fails because things will get confusing.
388991554f2SKenneth D. Merry 	 * Leave the devname and sasaddr intact so that we know to avoid reusing
389991554f2SKenneth D. Merry 	 * this target id if possible, and so we can assign the same target id
390991554f2SKenneth D. Merry 	 * to this device if it comes back in the future.
391991554f2SKenneth D. Merry 	 */
392d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
393d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
394991554f2SKenneth D. Merry 		targ = tm->cm_targ;
395991554f2SKenneth D. Merry 		targ->handle = 0x0;
396991554f2SKenneth D. Merry 		targ->encl_handle = 0x0;
397991554f2SKenneth D. Merry 		targ->encl_level_valid = 0x0;
398991554f2SKenneth D. Merry 		targ->encl_level = 0x0;
399991554f2SKenneth D. Merry 		targ->connector_name[0] = ' ';
400991554f2SKenneth D. Merry 		targ->connector_name[1] = ' ';
401991554f2SKenneth D. Merry 		targ->connector_name[2] = ' ';
402991554f2SKenneth D. Merry 		targ->connector_name[3] = ' ';
403991554f2SKenneth D. Merry 		targ->encl_slot = 0x0;
404991554f2SKenneth D. Merry 		targ->exp_dev_handle = 0x0;
405991554f2SKenneth D. Merry 		targ->phy_num = 0x0;
406991554f2SKenneth D. Merry 		targ->linkrate = 0x0;
407991554f2SKenneth D. Merry 		targ->devinfo = 0x0;
408991554f2SKenneth D. Merry 		targ->flags = 0x0;
409991554f2SKenneth D. Merry 		targ->scsi_req_desc_type = 0;
410991554f2SKenneth D. Merry 	}
411991554f2SKenneth D. Merry 
412991554f2SKenneth D. Merry 	mprsas_free_tm(sc, tm);
413991554f2SKenneth D. Merry }
414991554f2SKenneth D. Merry 
415991554f2SKenneth D. Merry 
416991554f2SKenneth D. Merry /*
417991554f2SKenneth D. Merry  * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
418991554f2SKenneth D. Merry  * Otherwise Volume Delete is same as Bare Drive Removal.
419991554f2SKenneth D. Merry  */
420991554f2SKenneth D. Merry void
421991554f2SKenneth D. Merry mprsas_prepare_volume_remove(struct mprsas_softc *sassc, uint16_t handle)
422991554f2SKenneth D. Merry {
423991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
424991554f2SKenneth D. Merry 	struct mpr_softc *sc;
425991554f2SKenneth D. Merry 	struct mpr_command *cm;
426991554f2SKenneth D. Merry 	struct mprsas_target *targ = NULL;
427991554f2SKenneth D. Merry 
428991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
429991554f2SKenneth D. Merry 	sc = sassc->sc;
430991554f2SKenneth D. Merry 
431991554f2SKenneth D. Merry 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
432991554f2SKenneth D. Merry 	if (targ == NULL) {
433991554f2SKenneth D. Merry 		/* FIXME: what is the action? */
434991554f2SKenneth D. Merry 		/* We don't know about this device? */
435991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR,
436991554f2SKenneth D. Merry 		   "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
437991554f2SKenneth D. Merry 		return;
438991554f2SKenneth D. Merry 	}
439991554f2SKenneth D. Merry 
440991554f2SKenneth D. Merry 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
441991554f2SKenneth D. Merry 
442991554f2SKenneth D. Merry 	cm = mprsas_alloc_tm(sc);
443991554f2SKenneth D. Merry 	if (cm == NULL) {
444991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR,
445991554f2SKenneth D. Merry 		    "%s: command alloc failure\n", __func__);
446991554f2SKenneth D. Merry 		return;
447991554f2SKenneth D. Merry 	}
448991554f2SKenneth D. Merry 
449991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
450991554f2SKenneth D. Merry 
451991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
452991554f2SKenneth D. Merry 	req->DevHandle = targ->handle;
453991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
454991554f2SKenneth D. Merry 
45589d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling) {
456991554f2SKenneth D. Merry 		/* SAS Hard Link Reset / SATA Link Reset */
457991554f2SKenneth D. Merry 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
45889d1c21fSKashyap D Desai 	} else {
45989d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
46089d1c21fSKashyap D Desai 		req->MsgFlags =
46189d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
46289d1c21fSKashyap D Desai 	}
463991554f2SKenneth D. Merry 
464991554f2SKenneth D. Merry 	cm->cm_targ = targ;
465991554f2SKenneth D. Merry 	cm->cm_data = NULL;
466991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_remove_volume;
467991554f2SKenneth D. Merry 	cm->cm_complete_data = (void *)(uintptr_t)handle;
468a2c14879SStephen McConnell 
469a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
470a2c14879SStephen McConnell 	    __func__, targ->tid);
471a2c14879SStephen McConnell 	mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
472a2c14879SStephen McConnell 
473991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
474991554f2SKenneth D. Merry }
475991554f2SKenneth D. Merry 
476991554f2SKenneth D. Merry /*
47767feec50SStephen McConnell  * The firmware performs debounce on the link to avoid transient link errors
47867feec50SStephen McConnell  * and false removals.  When it does decide that link has been lost and a
47967feec50SStephen McConnell  * device needs to go away, it expects that the host will perform a target reset
48067feec50SStephen McConnell  * and then an op remove.  The reset has the side-effect of aborting any
48167feec50SStephen McConnell  * outstanding requests for the device, which is required for the op-remove to
48267feec50SStephen McConnell  * succeed.  It's not clear if the host should check for the device coming back
48367feec50SStephen McConnell  * alive after the reset.
484991554f2SKenneth D. Merry  */
485991554f2SKenneth D. Merry void
486991554f2SKenneth D. Merry mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle)
487991554f2SKenneth D. Merry {
488991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
489991554f2SKenneth D. Merry 	struct mpr_softc *sc;
49046b9415fSScott Long 	struct mpr_command *tm;
491991554f2SKenneth D. Merry 	struct mprsas_target *targ = NULL;
492991554f2SKenneth D. Merry 
493991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
494991554f2SKenneth D. Merry 
495991554f2SKenneth D. Merry 	sc = sassc->sc;
496991554f2SKenneth D. Merry 
497991554f2SKenneth D. Merry 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
498991554f2SKenneth D. Merry 	if (targ == NULL) {
499991554f2SKenneth D. Merry 		/* FIXME: what is the action? */
500991554f2SKenneth D. Merry 		/* We don't know about this device? */
501991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s : invalid handle 0x%x \n",
502991554f2SKenneth D. Merry 		    __func__, handle);
503991554f2SKenneth D. Merry 		return;
504991554f2SKenneth D. Merry 	}
505991554f2SKenneth D. Merry 
506991554f2SKenneth D. Merry 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
507991554f2SKenneth D. Merry 
50846b9415fSScott Long 	tm = mprsas_alloc_tm(sc);
50946b9415fSScott Long 	if (tm == NULL) {
510991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: command alloc failure\n",
511991554f2SKenneth D. Merry 		    __func__);
512991554f2SKenneth D. Merry 		return;
513991554f2SKenneth D. Merry 	}
514991554f2SKenneth D. Merry 
515991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
516991554f2SKenneth D. Merry 
51746b9415fSScott Long 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
518991554f2SKenneth D. Merry 	memset(req, 0, sizeof(*req));
519991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
520991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
521991554f2SKenneth D. Merry 
522991554f2SKenneth D. Merry 	/* SAS Hard Link Reset / SATA Link Reset */
523991554f2SKenneth D. Merry 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
524991554f2SKenneth D. Merry 
52546b9415fSScott Long 	tm->cm_targ = targ;
52646b9415fSScott Long 	tm->cm_data = NULL;
52746b9415fSScott Long 	tm->cm_complete = mprsas_remove_device;
52846b9415fSScott Long 	tm->cm_complete_data = (void *)(uintptr_t)handle;
529a2c14879SStephen McConnell 
530a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
531a2c14879SStephen McConnell 	    __func__, targ->tid);
53246b9415fSScott Long 	mprsas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
533a2c14879SStephen McConnell 
53446b9415fSScott Long 	mpr_map_command(sc, tm);
535991554f2SKenneth D. Merry }
536991554f2SKenneth D. Merry 
537991554f2SKenneth D. Merry static void
538991554f2SKenneth D. Merry mprsas_remove_device(struct mpr_softc *sc, struct mpr_command *tm)
539991554f2SKenneth D. Merry {
540991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
541991554f2SKenneth D. Merry 	MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
542991554f2SKenneth D. Merry 	struct mprsas_target *targ;
543991554f2SKenneth D. Merry 	uint16_t handle;
544991554f2SKenneth D. Merry 
545991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
546991554f2SKenneth D. Merry 
547991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
548991554f2SKenneth D. Merry 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
549991554f2SKenneth D. Merry 	targ = tm->cm_targ;
550991554f2SKenneth D. Merry 
551991554f2SKenneth D. Merry 	/*
552991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
553991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
554991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
555991554f2SKenneth D. Merry 	 */
556991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
557991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for remove of "
558991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
559991554f2SKenneth D. Merry 		    tm->cm_flags, handle);
560991554f2SKenneth D. Merry 	}
561991554f2SKenneth D. Merry 
562991554f2SKenneth D. Merry 	if (reply == NULL) {
563991554f2SKenneth D. Merry 		/* XXX retry the remove after the diag reset completes? */
564991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply resetting device "
565991554f2SKenneth D. Merry 		    "0x%04x\n", __func__, handle);
566991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
567991554f2SKenneth D. Merry 		return;
568991554f2SKenneth D. Merry 	}
569991554f2SKenneth D. Merry 
570d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
571d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
57258581c13SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "IOCStatus = 0x%x while resetting "
573991554f2SKenneth D. Merry 		    "device 0x%x\n", le16toh(reply->IOCStatus), handle);
574991554f2SKenneth D. Merry 	}
575991554f2SKenneth D. Merry 
576991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "Reset aborted %u commands\n",
577991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
578991554f2SKenneth D. Merry 	mpr_free_reply(sc, tm->cm_reply_data);
579991554f2SKenneth D. Merry 	tm->cm_reply = NULL;	/* Ensures the reply won't get re-freed */
580991554f2SKenneth D. Merry 
581991554f2SKenneth D. Merry 	/* Reuse the existing command */
582991554f2SKenneth D. Merry 	req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
583991554f2SKenneth D. Merry 	memset(req, 0, sizeof(*req));
584991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
585991554f2SKenneth D. Merry 	req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
586991554f2SKenneth D. Merry 	req->DevHandle = htole16(handle);
587991554f2SKenneth D. Merry 	tm->cm_data = NULL;
588991554f2SKenneth D. Merry 	tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
589991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_remove_complete;
590991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)(uintptr_t)handle;
591991554f2SKenneth D. Merry 
5924c1cdd4aSWarner Losh 	/*
5934c1cdd4aSWarner Losh 	 * Wait to send the REMOVE_DEVICE until all the commands have cleared.
5944c1cdd4aSWarner Losh 	 * They should be aborted or time out and we'll kick thus off there
5954c1cdd4aSWarner Losh 	 * if so.
5964c1cdd4aSWarner Losh 	 */
5974c1cdd4aSWarner Losh 	if (TAILQ_FIRST(&targ->commands) == NULL) {
5984c1cdd4aSWarner Losh 		mpr_dprint(sc, MPR_INFO, "No pending commands: starting remove_device\n");
599991554f2SKenneth D. Merry 		mpr_map_command(sc, tm);
6004c1cdd4aSWarner Losh 		targ->pending_remove_tm = NULL;
6014c1cdd4aSWarner Losh 	} else {
6024c1cdd4aSWarner Losh 		targ->pending_remove_tm = tm;
6034c1cdd4aSWarner Losh 	}
604991554f2SKenneth D. Merry 
605a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "clearing target %u handle 0x%04x\n",
606991554f2SKenneth D. Merry 	    targ->tid, handle);
607991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
608a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "At enclosure level %d, slot %d, "
609991554f2SKenneth D. Merry 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
610991554f2SKenneth D. Merry 		    targ->connector_name);
611991554f2SKenneth D. Merry 	}
612991554f2SKenneth D. Merry }
613991554f2SKenneth D. Merry 
614991554f2SKenneth D. Merry static void
615991554f2SKenneth D. Merry mprsas_remove_complete(struct mpr_softc *sc, struct mpr_command *tm)
616991554f2SKenneth D. Merry {
617991554f2SKenneth D. Merry 	MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
618991554f2SKenneth D. Merry 	uint16_t handle;
619991554f2SKenneth D. Merry 	struct mprsas_target *targ;
620991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
621991554f2SKenneth D. Merry 
622991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
623991554f2SKenneth D. Merry 
624991554f2SKenneth D. Merry 	reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
625991554f2SKenneth D. Merry 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
626991554f2SKenneth D. Merry 
6274c1cdd4aSWarner Losh 	targ = tm->cm_targ;
6284c1cdd4aSWarner Losh 
6294c1cdd4aSWarner Losh 	/*
6304c1cdd4aSWarner Losh 	 * At this point, we should have no pending commands for the target.
6314c1cdd4aSWarner Losh 	 * The remove target has just completed.
6324c1cdd4aSWarner Losh 	 */
6334c1cdd4aSWarner Losh 	KASSERT(TAILQ_FIRST(&targ->commands) == NULL,
6344c1cdd4aSWarner Losh 	    ("%s: no commands should be pending\n", __func__));
6354c1cdd4aSWarner Losh 
636991554f2SKenneth D. Merry 	/*
637991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
638991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
639991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
640991554f2SKenneth D. Merry 	 */
641991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
642991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "%s: cm_flags = %#x for remove of "
643991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
644991554f2SKenneth D. Merry 		    tm->cm_flags, handle);
645991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
646991554f2SKenneth D. Merry 		return;
647991554f2SKenneth D. Merry 	}
648991554f2SKenneth D. Merry 
649991554f2SKenneth D. Merry 	if (reply == NULL) {
650991554f2SKenneth D. Merry 		/* most likely a chip reset */
651991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply removing device "
652991554f2SKenneth D. Merry 		    "0x%04x\n", __func__, handle);
653991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
654991554f2SKenneth D. Merry 		return;
655991554f2SKenneth D. Merry 	}
656991554f2SKenneth D. Merry 
657991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s on handle 0x%04x, IOCStatus= 0x%x\n",
658991554f2SKenneth D. Merry 	    __func__, handle, le16toh(reply->IOCStatus));
659991554f2SKenneth D. Merry 
660991554f2SKenneth D. Merry 	/*
661991554f2SKenneth D. Merry 	 * Don't clear target if remove fails because things will get confusing.
662991554f2SKenneth D. Merry 	 * Leave the devname and sasaddr intact so that we know to avoid reusing
663991554f2SKenneth D. Merry 	 * this target id if possible, and so we can assign the same target id
664991554f2SKenneth D. Merry 	 * to this device if it comes back in the future.
665991554f2SKenneth D. Merry 	 */
666d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
667d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
668991554f2SKenneth D. Merry 		targ->handle = 0x0;
669991554f2SKenneth D. Merry 		targ->encl_handle = 0x0;
670991554f2SKenneth D. Merry 		targ->encl_level_valid = 0x0;
671991554f2SKenneth D. Merry 		targ->encl_level = 0x0;
672991554f2SKenneth D. Merry 		targ->connector_name[0] = ' ';
673991554f2SKenneth D. Merry 		targ->connector_name[1] = ' ';
674991554f2SKenneth D. Merry 		targ->connector_name[2] = ' ';
675991554f2SKenneth D. Merry 		targ->connector_name[3] = ' ';
676991554f2SKenneth D. Merry 		targ->encl_slot = 0x0;
677991554f2SKenneth D. Merry 		targ->exp_dev_handle = 0x0;
678991554f2SKenneth D. Merry 		targ->phy_num = 0x0;
679991554f2SKenneth D. Merry 		targ->linkrate = 0x0;
680991554f2SKenneth D. Merry 		targ->devinfo = 0x0;
681991554f2SKenneth D. Merry 		targ->flags = 0x0;
682991554f2SKenneth D. Merry 		targ->scsi_req_desc_type = 0;
683991554f2SKenneth D. Merry 
684991554f2SKenneth D. Merry 		while (!SLIST_EMPTY(&targ->luns)) {
685991554f2SKenneth D. Merry 			lun = SLIST_FIRST(&targ->luns);
686991554f2SKenneth D. Merry 			SLIST_REMOVE_HEAD(&targ->luns, lun_link);
687991554f2SKenneth D. Merry 			free(lun, M_MPR);
688991554f2SKenneth D. Merry 		}
689991554f2SKenneth D. Merry 	}
690991554f2SKenneth D. Merry 
691991554f2SKenneth D. Merry 	mprsas_free_tm(sc, tm);
692991554f2SKenneth D. Merry }
693991554f2SKenneth D. Merry 
694991554f2SKenneth D. Merry static int
695991554f2SKenneth D. Merry mprsas_register_events(struct mpr_softc *sc)
696991554f2SKenneth D. Merry {
697991554f2SKenneth D. Merry 	uint8_t events[16];
698991554f2SKenneth D. Merry 
699991554f2SKenneth D. Merry 	bzero(events, 16);
700991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
701991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_DISCOVERY);
702991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
703991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
704991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
705991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
706991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
707991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
708991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_VOLUME);
709991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
710991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
711991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_TEMP_THRESHOLD);
7125f5baf0eSAlexander Motin 	setbit(events, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
71367feec50SStephen McConnell 	if (sc->facts->MsgVersion >= MPI2_VERSION_02_06) {
7142bbc5fcbSStephen McConnell 		setbit(events, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
71567feec50SStephen McConnell 		if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC) {
71667feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
71767feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_ENUMERATION);
71867feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
71967feec50SStephen McConnell 		}
72067feec50SStephen McConnell 	}
721991554f2SKenneth D. Merry 
722991554f2SKenneth D. Merry 	mpr_register_events(sc, events, mprsas_evt_handler, NULL,
723991554f2SKenneth D. Merry 	    &sc->sassc->mprsas_eh);
724991554f2SKenneth D. Merry 
725991554f2SKenneth D. Merry 	return (0);
726991554f2SKenneth D. Merry }
727991554f2SKenneth D. Merry 
728991554f2SKenneth D. Merry int
729991554f2SKenneth D. Merry mpr_attach_sas(struct mpr_softc *sc)
730991554f2SKenneth D. Merry {
731991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
732991554f2SKenneth D. Merry 	cam_status status;
73362a09ee9SAlexander Motin 	int unit, error = 0, reqs;
734991554f2SKenneth D. Merry 
735991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
736757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
737991554f2SKenneth D. Merry 
738991554f2SKenneth D. Merry 	sassc = malloc(sizeof(struct mprsas_softc), M_MPR, M_WAITOK|M_ZERO);
739991554f2SKenneth D. Merry 	if (!sassc) {
740757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
741757ff642SScott Long 		    "Cannot allocate SAS subsystem memory\n");
742991554f2SKenneth D. Merry 		return (ENOMEM);
743991554f2SKenneth D. Merry 	}
744991554f2SKenneth D. Merry 
745991554f2SKenneth D. Merry 	/*
746a2c14879SStephen McConnell 	 * XXX MaxTargets could change during a reinit.  Since we don't
747991554f2SKenneth D. Merry 	 * resize the targets[] array during such an event, cache the value
748991554f2SKenneth D. Merry 	 * of MaxTargets here so that we don't get into trouble later.  This
749991554f2SKenneth D. Merry 	 * should move into the reinit logic.
750991554f2SKenneth D. Merry 	 */
751327f2e6cSStephen McConnell 	sassc->maxtargets = sc->facts->MaxTargets + sc->facts->MaxVolumes;
752991554f2SKenneth D. Merry 	sassc->targets = malloc(sizeof(struct mprsas_target) *
753991554f2SKenneth D. Merry 	    sassc->maxtargets, M_MPR, M_WAITOK|M_ZERO);
754991554f2SKenneth D. Merry 	if (!sassc->targets) {
755757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
756757ff642SScott Long 		    "Cannot allocate SAS target memory\n");
757991554f2SKenneth D. Merry 		free(sassc, M_MPR);
758991554f2SKenneth D. Merry 		return (ENOMEM);
759991554f2SKenneth D. Merry 	}
760991554f2SKenneth D. Merry 	sc->sassc = sassc;
761991554f2SKenneth D. Merry 	sassc->sc = sc;
762991554f2SKenneth D. Merry 
76362a09ee9SAlexander Motin 	reqs = sc->num_reqs - sc->num_prireqs - 1;
76462a09ee9SAlexander Motin 	if ((sassc->devq = cam_simq_alloc(reqs)) == NULL) {
765757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR, "Cannot allocate SIMQ\n");
766991554f2SKenneth D. Merry 		error = ENOMEM;
767991554f2SKenneth D. Merry 		goto out;
768991554f2SKenneth D. Merry 	}
769991554f2SKenneth D. Merry 
770991554f2SKenneth D. Merry 	unit = device_get_unit(sc->mpr_dev);
771991554f2SKenneth D. Merry 	sassc->sim = cam_sim_alloc(mprsas_action, mprsas_poll, "mpr", sassc,
77262a09ee9SAlexander Motin 	    unit, &sc->mpr_mtx, reqs, reqs, sassc->devq);
773991554f2SKenneth D. Merry 	if (sassc->sim == NULL) {
774757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR, "Cannot allocate SIM\n");
775991554f2SKenneth D. Merry 		error = EINVAL;
776991554f2SKenneth D. Merry 		goto out;
777991554f2SKenneth D. Merry 	}
778991554f2SKenneth D. Merry 
779991554f2SKenneth D. Merry 	TAILQ_INIT(&sassc->ev_queue);
780991554f2SKenneth D. Merry 
781991554f2SKenneth D. Merry 	/* Initialize taskqueue for Event Handling */
782991554f2SKenneth D. Merry 	TASK_INIT(&sassc->ev_task, 0, mprsas_firmware_event_work, sc);
783991554f2SKenneth D. Merry 	sassc->ev_tq = taskqueue_create("mpr_taskq", M_NOWAIT | M_ZERO,
784991554f2SKenneth D. Merry 	    taskqueue_thread_enqueue, &sassc->ev_tq);
785c2a0f07aSAlexander Motin 	taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
786991554f2SKenneth D. Merry 	    device_get_nameunit(sc->mpr_dev));
787991554f2SKenneth D. Merry 
788991554f2SKenneth D. Merry 	mpr_lock(sc);
789991554f2SKenneth D. Merry 
790991554f2SKenneth D. Merry 	/*
791991554f2SKenneth D. Merry 	 * XXX There should be a bus for every port on the adapter, but since
792991554f2SKenneth D. Merry 	 * we're just going to fake the topology for now, we'll pretend that
793991554f2SKenneth D. Merry 	 * everything is just a target on a single bus.
794991554f2SKenneth D. Merry 	 */
795991554f2SKenneth D. Merry 	if ((error = xpt_bus_register(sassc->sim, sc->mpr_dev, 0)) != 0) {
796757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
797757ff642SScott Long 		    "Error %d registering SCSI bus\n", error);
798991554f2SKenneth D. Merry 		mpr_unlock(sc);
799991554f2SKenneth D. Merry 		goto out;
800991554f2SKenneth D. Merry 	}
801991554f2SKenneth D. Merry 
802991554f2SKenneth D. Merry 	/*
803a2c14879SStephen McConnell 	 * Assume that discovery events will start right away.
804991554f2SKenneth D. Merry 	 *
805991554f2SKenneth D. Merry 	 * Hold off boot until discovery is complete.
806991554f2SKenneth D. Merry 	 */
807991554f2SKenneth D. Merry 	sassc->flags |= MPRSAS_IN_STARTUP | MPRSAS_IN_DISCOVERY;
808991554f2SKenneth D. Merry 	sc->sassc->startup_refcount = 0;
809991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
810991554f2SKenneth D. Merry 
811a2c14879SStephen McConnell 	callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
812991554f2SKenneth D. Merry 
813991554f2SKenneth D. Merry 	/*
814991554f2SKenneth D. Merry 	 * Register for async events so we can determine the EEDP
815991554f2SKenneth D. Merry 	 * capabilities of devices.
816991554f2SKenneth D. Merry 	 */
817991554f2SKenneth D. Merry 	status = xpt_create_path(&sassc->path, /*periph*/NULL,
818991554f2SKenneth D. Merry 	    cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
819991554f2SKenneth D. Merry 	    CAM_LUN_WILDCARD);
820991554f2SKenneth D. Merry 	if (status != CAM_REQ_CMP) {
821757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
822757ff642SScott Long 		    "Error %#x creating sim path\n", status);
823991554f2SKenneth D. Merry 		sassc->path = NULL;
824991554f2SKenneth D. Merry 	} else {
825991554f2SKenneth D. Merry 		int event;
826991554f2SKenneth D. Merry 
827991554f2SKenneth D. Merry 		event = AC_ADVINFO_CHANGED | AC_FOUND_DEVICE;
828991554f2SKenneth D. Merry 		status = xpt_register_async(event, mprsas_async, sc,
829991554f2SKenneth D. Merry 					    sassc->path);
83007aa4de1SKenneth D. Merry 
831991554f2SKenneth D. Merry 		if (status != CAM_REQ_CMP) {
832991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR,
833991554f2SKenneth D. Merry 			    "Error %#x registering async handler for "
834991554f2SKenneth D. Merry 			    "AC_ADVINFO_CHANGED events\n", status);
835991554f2SKenneth D. Merry 			xpt_free_path(sassc->path);
836991554f2SKenneth D. Merry 			sassc->path = NULL;
837991554f2SKenneth D. Merry 		}
838991554f2SKenneth D. Merry 	}
839991554f2SKenneth D. Merry 	if (status != CAM_REQ_CMP) {
840991554f2SKenneth D. Merry 		/*
841991554f2SKenneth D. Merry 		 * EEDP use is the exception, not the rule.
842991554f2SKenneth D. Merry 		 * Warn the user, but do not fail to attach.
843991554f2SKenneth D. Merry 		 */
844991554f2SKenneth D. Merry 		mpr_printf(sc, "EEDP capabilities disabled.\n");
845991554f2SKenneth D. Merry 	}
846991554f2SKenneth D. Merry 
847991554f2SKenneth D. Merry 	mpr_unlock(sc);
848991554f2SKenneth D. Merry 
849991554f2SKenneth D. Merry 	mprsas_register_events(sc);
850991554f2SKenneth D. Merry out:
851991554f2SKenneth D. Merry 	if (error)
852991554f2SKenneth D. Merry 		mpr_detach_sas(sc);
853757ff642SScott Long 
854757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit, error= %d\n", __func__, error);
855991554f2SKenneth D. Merry 	return (error);
856991554f2SKenneth D. Merry }
857991554f2SKenneth D. Merry 
858991554f2SKenneth D. Merry int
859991554f2SKenneth D. Merry mpr_detach_sas(struct mpr_softc *sc)
860991554f2SKenneth D. Merry {
861991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
862991554f2SKenneth D. Merry 	struct mprsas_lun *lun, *lun_tmp;
863991554f2SKenneth D. Merry 	struct mprsas_target *targ;
864991554f2SKenneth D. Merry 	int i;
865991554f2SKenneth D. Merry 
866991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
867991554f2SKenneth D. Merry 
868991554f2SKenneth D. Merry 	if (sc->sassc == NULL)
869991554f2SKenneth D. Merry 		return (0);
870991554f2SKenneth D. Merry 
871991554f2SKenneth D. Merry 	sassc = sc->sassc;
872991554f2SKenneth D. Merry 	mpr_deregister_events(sc, sassc->mprsas_eh);
873991554f2SKenneth D. Merry 
874991554f2SKenneth D. Merry 	/*
875991554f2SKenneth D. Merry 	 * Drain and free the event handling taskqueue with the lock
876991554f2SKenneth D. Merry 	 * unheld so that any parallel processing tasks drain properly
877991554f2SKenneth D. Merry 	 * without deadlocking.
878991554f2SKenneth D. Merry 	 */
879991554f2SKenneth D. Merry 	if (sassc->ev_tq != NULL)
880991554f2SKenneth D. Merry 		taskqueue_free(sassc->ev_tq);
881991554f2SKenneth D. Merry 
882991554f2SKenneth D. Merry 	/* Make sure CAM doesn't wedge if we had to bail out early. */
883991554f2SKenneth D. Merry 	mpr_lock(sc);
884991554f2SKenneth D. Merry 
8853c5ac992SScott Long 	while (sassc->startup_refcount != 0)
8863c5ac992SScott Long 		mprsas_startup_decrement(sassc);
8873c5ac992SScott Long 
888991554f2SKenneth D. Merry 	/* Deregister our async handler */
889991554f2SKenneth D. Merry 	if (sassc->path != NULL) {
890991554f2SKenneth D. Merry 		xpt_register_async(0, mprsas_async, sc, sassc->path);
891991554f2SKenneth D. Merry 		xpt_free_path(sassc->path);
892991554f2SKenneth D. Merry 		sassc->path = NULL;
893991554f2SKenneth D. Merry 	}
894991554f2SKenneth D. Merry 
895991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_IN_STARTUP)
896991554f2SKenneth D. Merry 		xpt_release_simq(sassc->sim, 1);
897991554f2SKenneth D. Merry 
898991554f2SKenneth D. Merry 	if (sassc->sim != NULL) {
899991554f2SKenneth D. Merry 		xpt_bus_deregister(cam_sim_path(sassc->sim));
900991554f2SKenneth D. Merry 		cam_sim_free(sassc->sim, FALSE);
901991554f2SKenneth D. Merry 	}
902991554f2SKenneth D. Merry 
903991554f2SKenneth D. Merry 	mpr_unlock(sc);
904991554f2SKenneth D. Merry 
905991554f2SKenneth D. Merry 	if (sassc->devq != NULL)
906991554f2SKenneth D. Merry 		cam_simq_free(sassc->devq);
907991554f2SKenneth D. Merry 
908991554f2SKenneth D. Merry 	for (i = 0; i < sassc->maxtargets; i++) {
909991554f2SKenneth D. Merry 		targ = &sassc->targets[i];
910991554f2SKenneth D. Merry 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
911991554f2SKenneth D. Merry 			free(lun, M_MPR);
912991554f2SKenneth D. Merry 		}
913991554f2SKenneth D. Merry 	}
914991554f2SKenneth D. Merry 	free(sassc->targets, M_MPR);
915991554f2SKenneth D. Merry 	free(sassc, M_MPR);
916991554f2SKenneth D. Merry 	sc->sassc = NULL;
917991554f2SKenneth D. Merry 
918991554f2SKenneth D. Merry 	return (0);
919991554f2SKenneth D. Merry }
920991554f2SKenneth D. Merry 
921991554f2SKenneth D. Merry void
922991554f2SKenneth D. Merry mprsas_discovery_end(struct mprsas_softc *sassc)
923991554f2SKenneth D. Merry {
924991554f2SKenneth D. Merry 	struct mpr_softc *sc = sassc->sc;
925991554f2SKenneth D. Merry 
926991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
927991554f2SKenneth D. Merry 
928991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_DISCOVERY_TIMEOUT_PENDING)
929991554f2SKenneth D. Merry 		callout_stop(&sassc->discovery_callout);
930991554f2SKenneth D. Merry 
931327f2e6cSStephen McConnell 	/*
932327f2e6cSStephen McConnell 	 * After discovery has completed, check the mapping table for any
933327f2e6cSStephen McConnell 	 * missing devices and update their missing counts. Only do this once
934327f2e6cSStephen McConnell 	 * whenever the driver is initialized so that missing counts aren't
935327f2e6cSStephen McConnell 	 * updated unnecessarily. Note that just because discovery has
936327f2e6cSStephen McConnell 	 * completed doesn't mean that events have been processed yet. The
937327f2e6cSStephen McConnell 	 * check_devices function is a callout timer that checks if ALL devices
938327f2e6cSStephen McConnell 	 * are missing. If so, it will wait a little longer for events to
939327f2e6cSStephen McConnell 	 * complete and keep resetting itself until some device in the mapping
940327f2e6cSStephen McConnell 	 * table is not missing, meaning that event processing has started.
941327f2e6cSStephen McConnell 	 */
942327f2e6cSStephen McConnell 	if (sc->track_mapping_events) {
943327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_XINFO | MPR_MAPPING, "Discovery has "
944327f2e6cSStephen McConnell 		    "completed. Check for missing devices in the mapping "
945327f2e6cSStephen McConnell 		    "table.\n");
946327f2e6cSStephen McConnell 		callout_reset(&sc->device_check_callout,
947327f2e6cSStephen McConnell 		    MPR_MISSING_CHECK_DELAY * hz, mpr_mapping_check_devices,
948327f2e6cSStephen McConnell 		    sc);
949327f2e6cSStephen McConnell 	}
950991554f2SKenneth D. Merry }
951991554f2SKenneth D. Merry 
952991554f2SKenneth D. Merry static void
953991554f2SKenneth D. Merry mprsas_action(struct cam_sim *sim, union ccb *ccb)
954991554f2SKenneth D. Merry {
955991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
956991554f2SKenneth D. Merry 
957991554f2SKenneth D. Merry 	sassc = cam_sim_softc(sim);
958991554f2SKenneth D. Merry 
959991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
960a2c14879SStephen McConnell 	mpr_dprint(sassc->sc, MPR_TRACE, "ccb func_code 0x%x\n",
961991554f2SKenneth D. Merry 	    ccb->ccb_h.func_code);
962991554f2SKenneth D. Merry 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
963991554f2SKenneth D. Merry 
964991554f2SKenneth D. Merry 	switch (ccb->ccb_h.func_code) {
965991554f2SKenneth D. Merry 	case XPT_PATH_INQ:
966991554f2SKenneth D. Merry 	{
967991554f2SKenneth D. Merry 		struct ccb_pathinq *cpi = &ccb->cpi;
96832b0a21eSStephen McConnell 		struct mpr_softc *sc = sassc->sc;
969991554f2SKenneth D. Merry 
970991554f2SKenneth D. Merry 		cpi->version_num = 1;
971991554f2SKenneth D. Merry 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
972991554f2SKenneth D. Merry 		cpi->target_sprt = 0;
973991554f2SKenneth D. Merry 		cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
974991554f2SKenneth D. Merry 		cpi->hba_eng_cnt = 0;
975991554f2SKenneth D. Merry 		cpi->max_target = sassc->maxtargets - 1;
976991554f2SKenneth D. Merry 		cpi->max_lun = 255;
977327f2e6cSStephen McConnell 
978327f2e6cSStephen McConnell 		/*
979327f2e6cSStephen McConnell 		 * initiator_id is set here to an ID outside the set of valid
980327f2e6cSStephen McConnell 		 * target IDs (including volumes).
981327f2e6cSStephen McConnell 		 */
982327f2e6cSStephen McConnell 		cpi->initiator_id = sassc->maxtargets;
9834195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
9844195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN);
9854195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
986991554f2SKenneth D. Merry 		cpi->unit_number = cam_sim_unit(sim);
987991554f2SKenneth D. Merry 		cpi->bus_id = cam_sim_bus(sim);
988991554f2SKenneth D. Merry 		/*
989991554f2SKenneth D. Merry 		 * XXXSLM-I think this needs to change based on config page or
990991554f2SKenneth D. Merry 		 * something instead of hardcoded to 150000.
991991554f2SKenneth D. Merry 		 */
992991554f2SKenneth D. Merry 		cpi->base_transfer_speed = 150000;
993991554f2SKenneth D. Merry 		cpi->transport = XPORT_SAS;
994991554f2SKenneth D. Merry 		cpi->transport_version = 0;
995991554f2SKenneth D. Merry 		cpi->protocol = PROTO_SCSI;
996991554f2SKenneth D. Merry 		cpi->protocol_version = SCSI_REV_SPC;
9974f5d6573SAlexander Motin 		cpi->maxio = sc->maxio;
998a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
999991554f2SKenneth D. Merry 		break;
1000991554f2SKenneth D. Merry 	}
1001991554f2SKenneth D. Merry 	case XPT_GET_TRAN_SETTINGS:
1002991554f2SKenneth D. Merry 	{
1003991554f2SKenneth D. Merry 		struct ccb_trans_settings	*cts;
1004991554f2SKenneth D. Merry 		struct ccb_trans_settings_sas	*sas;
1005991554f2SKenneth D. Merry 		struct ccb_trans_settings_scsi	*scsi;
1006991554f2SKenneth D. Merry 		struct mprsas_target *targ;
1007991554f2SKenneth D. Merry 
1008991554f2SKenneth D. Merry 		cts = &ccb->cts;
1009991554f2SKenneth D. Merry 		sas = &cts->xport_specific.sas;
1010991554f2SKenneth D. Merry 		scsi = &cts->proto_specific.scsi;
1011991554f2SKenneth D. Merry 
1012991554f2SKenneth D. Merry 		KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
1013991554f2SKenneth D. Merry 		    ("Target %d out of bounds in XPT_GET_TRAN_SETTINGS\n",
1014991554f2SKenneth D. Merry 		    cts->ccb_h.target_id));
1015991554f2SKenneth D. Merry 		targ = &sassc->targets[cts->ccb_h.target_id];
1016991554f2SKenneth D. Merry 		if (targ->handle == 0x0) {
1017a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1018991554f2SKenneth D. Merry 			break;
1019991554f2SKenneth D. Merry 		}
1020991554f2SKenneth D. Merry 
1021991554f2SKenneth D. Merry 		cts->protocol_version = SCSI_REV_SPC2;
1022991554f2SKenneth D. Merry 		cts->transport = XPORT_SAS;
1023991554f2SKenneth D. Merry 		cts->transport_version = 0;
1024991554f2SKenneth D. Merry 
1025991554f2SKenneth D. Merry 		sas->valid = CTS_SAS_VALID_SPEED;
1026991554f2SKenneth D. Merry 		switch (targ->linkrate) {
1027991554f2SKenneth D. Merry 		case 0x08:
1028991554f2SKenneth D. Merry 			sas->bitrate = 150000;
1029991554f2SKenneth D. Merry 			break;
1030991554f2SKenneth D. Merry 		case 0x09:
1031991554f2SKenneth D. Merry 			sas->bitrate = 300000;
1032991554f2SKenneth D. Merry 			break;
1033991554f2SKenneth D. Merry 		case 0x0a:
1034991554f2SKenneth D. Merry 			sas->bitrate = 600000;
1035991554f2SKenneth D. Merry 			break;
103682315915SAlexander Motin 		case 0x0b:
103782315915SAlexander Motin 			sas->bitrate = 1200000;
103882315915SAlexander Motin 			break;
1039991554f2SKenneth D. Merry 		default:
1040991554f2SKenneth D. Merry 			sas->valid = 0;
1041991554f2SKenneth D. Merry 		}
1042991554f2SKenneth D. Merry 
1043991554f2SKenneth D. Merry 		cts->protocol = PROTO_SCSI;
1044991554f2SKenneth D. Merry 		scsi->valid = CTS_SCSI_VALID_TQ;
1045991554f2SKenneth D. Merry 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1046991554f2SKenneth D. Merry 
1047a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1048991554f2SKenneth D. Merry 		break;
1049991554f2SKenneth D. Merry 	}
1050991554f2SKenneth D. Merry 	case XPT_CALC_GEOMETRY:
1051991554f2SKenneth D. Merry 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
1052a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1053991554f2SKenneth D. Merry 		break;
1054991554f2SKenneth D. Merry 	case XPT_RESET_DEV:
10557a2a6a1aSStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action "
10567a2a6a1aSStephen McConnell 		    "XPT_RESET_DEV\n");
1057991554f2SKenneth D. Merry 		mprsas_action_resetdev(sassc, ccb);
1058991554f2SKenneth D. Merry 		return;
1059991554f2SKenneth D. Merry 	case XPT_RESET_BUS:
1060991554f2SKenneth D. Merry 	case XPT_ABORT:
1061991554f2SKenneth D. Merry 	case XPT_TERM_IO:
10627a2a6a1aSStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action faking success "
10637a2a6a1aSStephen McConnell 		    "for abort or reset\n");
1064a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1065991554f2SKenneth D. Merry 		break;
1066991554f2SKenneth D. Merry 	case XPT_SCSI_IO:
1067991554f2SKenneth D. Merry 		mprsas_action_scsiio(sassc, ccb);
1068991554f2SKenneth D. Merry 		return;
1069991554f2SKenneth D. Merry 	case XPT_SMP_IO:
1070991554f2SKenneth D. Merry 		mprsas_action_smpio(sassc, ccb);
1071991554f2SKenneth D. Merry 		return;
1072991554f2SKenneth D. Merry 	default:
1073a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1074991554f2SKenneth D. Merry 		break;
1075991554f2SKenneth D. Merry 	}
1076991554f2SKenneth D. Merry 	xpt_done(ccb);
1077991554f2SKenneth D. Merry 
1078991554f2SKenneth D. Merry }
1079991554f2SKenneth D. Merry 
1080991554f2SKenneth D. Merry static void
1081991554f2SKenneth D. Merry mprsas_announce_reset(struct mpr_softc *sc, uint32_t ac_code,
1082991554f2SKenneth D. Merry     target_id_t target_id, lun_id_t lun_id)
1083991554f2SKenneth D. Merry {
1084991554f2SKenneth D. Merry 	path_id_t path_id = cam_sim_path(sc->sassc->sim);
1085991554f2SKenneth D. Merry 	struct cam_path *path;
1086991554f2SKenneth D. Merry 
1087991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s code %x target %d lun %jx\n", __func__,
1088991554f2SKenneth D. Merry 	    ac_code, target_id, (uintmax_t)lun_id);
1089991554f2SKenneth D. Merry 
1090991554f2SKenneth D. Merry 	if (xpt_create_path(&path, NULL,
1091991554f2SKenneth D. Merry 		path_id, target_id, lun_id) != CAM_REQ_CMP) {
1092991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unable to create path for reset "
1093991554f2SKenneth D. Merry 		    "notification\n");
1094991554f2SKenneth D. Merry 		return;
1095991554f2SKenneth D. Merry 	}
1096991554f2SKenneth D. Merry 
1097991554f2SKenneth D. Merry 	xpt_async(ac_code, path, NULL);
1098991554f2SKenneth D. Merry 	xpt_free_path(path);
1099991554f2SKenneth D. Merry }
1100991554f2SKenneth D. Merry 
1101991554f2SKenneth D. Merry static void
1102991554f2SKenneth D. Merry mprsas_complete_all_commands(struct mpr_softc *sc)
1103991554f2SKenneth D. Merry {
1104991554f2SKenneth D. Merry 	struct mpr_command *cm;
1105991554f2SKenneth D. Merry 	int i;
1106991554f2SKenneth D. Merry 	int completed;
1107991554f2SKenneth D. Merry 
1108991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1109991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1110991554f2SKenneth D. Merry 
1111991554f2SKenneth D. Merry 	/* complete all commands with a NULL reply */
1112991554f2SKenneth D. Merry 	for (i = 1; i < sc->num_reqs; i++) {
1113991554f2SKenneth D. Merry 		cm = &sc->commands[i];
1114f0779b04SScott Long 		if (cm->cm_state == MPR_CM_STATE_FREE)
1115f0779b04SScott Long 			continue;
1116f0779b04SScott Long 
1117f0779b04SScott Long 		cm->cm_state = MPR_CM_STATE_BUSY;
1118991554f2SKenneth D. Merry 		cm->cm_reply = NULL;
1119991554f2SKenneth D. Merry 		completed = 0;
1120991554f2SKenneth D. Merry 
11218277ce2bSConrad Meyer 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
11228277ce2bSConrad Meyer 			MPASS(cm->cm_data);
11238277ce2bSConrad Meyer 			free(cm->cm_data, M_MPR);
11248277ce2bSConrad Meyer 			cm->cm_data = NULL;
11258277ce2bSConrad Meyer 		}
11268277ce2bSConrad Meyer 
1127991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_POLLED)
1128991554f2SKenneth D. Merry 			cm->cm_flags |= MPR_CM_FLAGS_COMPLETE;
1129991554f2SKenneth D. Merry 
1130991554f2SKenneth D. Merry 		if (cm->cm_complete != NULL) {
1131991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
11327a2a6a1aSStephen McConnell 			    "completing cm %p state %x ccb %p for diag reset\n",
11337a2a6a1aSStephen McConnell 			    cm, cm->cm_state, cm->cm_ccb);
1134991554f2SKenneth D. Merry 			cm->cm_complete(sc, cm);
1135991554f2SKenneth D. Merry 			completed = 1;
1136f0779b04SScott Long 		} else if (cm->cm_flags & MPR_CM_FLAGS_WAKEUP) {
1137991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
1138991554f2SKenneth D. Merry 			    "waking up cm %p state %x ccb %p for diag reset\n",
1139991554f2SKenneth D. Merry 			    cm, cm->cm_state, cm->cm_ccb);
1140991554f2SKenneth D. Merry 			wakeup(cm);
1141991554f2SKenneth D. Merry 			completed = 1;
1142991554f2SKenneth D. Merry 		}
1143991554f2SKenneth D. Merry 
1144991554f2SKenneth D. Merry 		if ((completed == 0) && (cm->cm_state != MPR_CM_STATE_FREE)) {
1145991554f2SKenneth D. Merry 			/* this should never happen, but if it does, log */
1146991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
1147991554f2SKenneth D. Merry 			    "cm %p state %x flags 0x%x ccb %p during diag "
1148991554f2SKenneth D. Merry 			    "reset\n", cm, cm->cm_state, cm->cm_flags,
1149991554f2SKenneth D. Merry 			    cm->cm_ccb);
1150991554f2SKenneth D. Merry 		}
1151991554f2SKenneth D. Merry 	}
1152f0779b04SScott Long 
1153f0779b04SScott Long 	sc->io_cmds_active = 0;
1154991554f2SKenneth D. Merry }
1155991554f2SKenneth D. Merry 
1156991554f2SKenneth D. Merry void
1157991554f2SKenneth D. Merry mprsas_handle_reinit(struct mpr_softc *sc)
1158991554f2SKenneth D. Merry {
1159991554f2SKenneth D. Merry 	int i;
1160991554f2SKenneth D. Merry 
1161991554f2SKenneth D. Merry 	/* Go back into startup mode and freeze the simq, so that CAM
1162991554f2SKenneth D. Merry 	 * doesn't send any commands until after we've rediscovered all
1163991554f2SKenneth D. Merry 	 * targets and found the proper device handles for them.
1164991554f2SKenneth D. Merry 	 *
1165991554f2SKenneth D. Merry 	 * After the reset, portenable will trigger discovery, and after all
1166991554f2SKenneth D. Merry 	 * discovery-related activities have finished, the simq will be
1167991554f2SKenneth D. Merry 	 * released.
1168991554f2SKenneth D. Merry 	 */
1169991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_INIT, "%s startup\n", __func__);
1170991554f2SKenneth D. Merry 	sc->sassc->flags |= MPRSAS_IN_STARTUP;
1171991554f2SKenneth D. Merry 	sc->sassc->flags |= MPRSAS_IN_DISCOVERY;
1172991554f2SKenneth D. Merry 	mprsas_startup_increment(sc->sassc);
1173991554f2SKenneth D. Merry 
1174991554f2SKenneth D. Merry 	/* notify CAM of a bus reset */
1175991554f2SKenneth D. Merry 	mprsas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1176991554f2SKenneth D. Merry 	    CAM_LUN_WILDCARD);
1177991554f2SKenneth D. Merry 
1178991554f2SKenneth D. Merry 	/* complete and cleanup after all outstanding commands */
1179991554f2SKenneth D. Merry 	mprsas_complete_all_commands(sc);
1180991554f2SKenneth D. Merry 
1181a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INIT, "%s startup %u after command completion\n",
1182a2c14879SStephen McConnell 	    __func__, sc->sassc->startup_refcount);
1183991554f2SKenneth D. Merry 
1184991554f2SKenneth D. Merry 	/* zero all the target handles, since they may change after the
1185991554f2SKenneth D. Merry 	 * reset, and we have to rediscover all the targets and use the new
1186991554f2SKenneth D. Merry 	 * handles.
1187991554f2SKenneth D. Merry 	 */
1188991554f2SKenneth D. Merry 	for (i = 0; i < sc->sassc->maxtargets; i++) {
1189991554f2SKenneth D. Merry 		if (sc->sassc->targets[i].outstanding != 0)
1190991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_INIT, "target %u outstanding %u\n",
1191991554f2SKenneth D. Merry 			    i, sc->sassc->targets[i].outstanding);
1192991554f2SKenneth D. Merry 		sc->sassc->targets[i].handle = 0x0;
1193991554f2SKenneth D. Merry 		sc->sassc->targets[i].exp_dev_handle = 0x0;
1194991554f2SKenneth D. Merry 		sc->sassc->targets[i].outstanding = 0;
1195991554f2SKenneth D. Merry 		sc->sassc->targets[i].flags = MPRSAS_TARGET_INDIAGRESET;
1196991554f2SKenneth D. Merry 	}
1197991554f2SKenneth D. Merry }
1198991554f2SKenneth D. Merry static void
1199991554f2SKenneth D. Merry mprsas_tm_timeout(void *data)
1200991554f2SKenneth D. Merry {
1201991554f2SKenneth D. Merry 	struct mpr_command *tm = data;
1202991554f2SKenneth D. Merry 	struct mpr_softc *sc = tm->cm_sc;
1203991554f2SKenneth D. Merry 
1204991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1205991554f2SKenneth D. Merry 
12067a2a6a1aSStephen McConnell 	mprsas_log_command(tm, MPR_INFO|MPR_RECOVERY, "task mgmt %p timed "
12077a2a6a1aSStephen McConnell 	    "out\n", tm);
1208f0779b04SScott Long 
1209f0779b04SScott Long 	KASSERT(tm->cm_state == MPR_CM_STATE_INQUEUE,
1210f0779b04SScott Long 	    ("command not inqueue\n"));
1211f0779b04SScott Long 
1212f0779b04SScott Long 	tm->cm_state = MPR_CM_STATE_BUSY;
1213991554f2SKenneth D. Merry 	mpr_reinit(sc);
1214991554f2SKenneth D. Merry }
1215991554f2SKenneth D. Merry 
1216991554f2SKenneth D. Merry static void
12177a2a6a1aSStephen McConnell mprsas_logical_unit_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1218991554f2SKenneth D. Merry {
1219991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1220991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1221991554f2SKenneth D. Merry 	unsigned int cm_count = 0;
1222991554f2SKenneth D. Merry 	struct mpr_command *cm;
1223991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1224991554f2SKenneth D. Merry 
1225991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1226991554f2SKenneth D. Merry 
1227991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1228991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1229991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1230991554f2SKenneth D. Merry 
1231991554f2SKenneth D. Merry 	/*
1232991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1233991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1234991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1235991554f2SKenneth D. Merry 	 */
1236991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
12376eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_ERROR,
12386eea4f46SScott Long 		    "%s: cm_flags = %#x for LUN reset! "
1239991554f2SKenneth D. Merry 		    "This should not happen!\n", __func__, tm->cm_flags);
1240991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1241991554f2SKenneth D. Merry 		return;
1242991554f2SKenneth D. Merry 	}
1243991554f2SKenneth D. Merry 
1244991554f2SKenneth D. Merry 	if (reply == NULL) {
12456eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY, "NULL reset reply for tm %p\n",
12466eea4f46SScott Long 		    tm);
1247991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1248991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
12496eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
12506eea4f46SScott Long 			    "reset, ignoring NULL LUN reset reply\n");
1251991554f2SKenneth D. Merry 			targ->tm = NULL;
1252991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1253991554f2SKenneth D. Merry 		}
1254991554f2SKenneth D. Merry 		else {
1255991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
12566eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
12576eea4f46SScott Long 			    "LUN reset attempt, resetting controller\n");
1258991554f2SKenneth D. Merry 			mpr_reinit(sc);
1259991554f2SKenneth D. Merry 		}
1260991554f2SKenneth D. Merry 		return;
1261991554f2SKenneth D. Merry 	}
1262991554f2SKenneth D. Merry 
12636eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1264991554f2SKenneth D. Merry 	    "logical unit reset status 0x%x code 0x%x count %u\n",
1265991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1266991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1267991554f2SKenneth D. Merry 
12686eea4f46SScott Long 	/*
12696eea4f46SScott Long 	 * See if there are any outstanding commands for this LUN.
1270991554f2SKenneth D. Merry 	 * This could be made more efficient by using a per-LU data
1271991554f2SKenneth D. Merry 	 * structure of some sort.
1272991554f2SKenneth D. Merry 	 */
1273991554f2SKenneth D. Merry 	TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1274991554f2SKenneth D. Merry 		if (cm->cm_lun == tm->cm_lun)
1275991554f2SKenneth D. Merry 			cm_count++;
1276991554f2SKenneth D. Merry 	}
1277991554f2SKenneth D. Merry 
1278991554f2SKenneth D. Merry 	if (cm_count == 0) {
12796eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
12806eea4f46SScott Long 		    "Finished recovery after LUN reset for target %u\n",
12816eea4f46SScott Long 		    targ->tid);
1282991554f2SKenneth D. Merry 
12836eea4f46SScott Long 		mprsas_announce_reset(sc, AC_SENT_BDR, targ->tid,
1284991554f2SKenneth D. Merry 		    tm->cm_lun);
1285991554f2SKenneth D. Merry 
12866eea4f46SScott Long 		/*
12876eea4f46SScott Long 		 * We've finished recovery for this logical unit.  check and
1288991554f2SKenneth D. Merry 		 * see if some other logical unit has a timedout command
1289991554f2SKenneth D. Merry 		 * that needs to be processed.
1290991554f2SKenneth D. Merry 		 */
1291991554f2SKenneth D. Merry 		cm = TAILQ_FIRST(&targ->timedout_commands);
1292991554f2SKenneth D. Merry 		if (cm) {
12936eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
12946eea4f46SScott Long 			   "More commands to abort for target %u\n", targ->tid);
1295991554f2SKenneth D. Merry 			mprsas_send_abort(sc, tm, cm);
12966eea4f46SScott Long 		} else {
1297991554f2SKenneth D. Merry 			targ->tm = NULL;
1298991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1299991554f2SKenneth D. Merry 		}
13006eea4f46SScott Long 	} else {
1301991554f2SKenneth D. Merry 		/* if we still have commands for this LUN, the reset
1302991554f2SKenneth D. Merry 		 * effectively failed, regardless of the status reported.
1303991554f2SKenneth D. Merry 		 * Escalate to a target reset.
1304991554f2SKenneth D. Merry 		 */
13056eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
13066eea4f46SScott Long 		    "logical unit reset complete for target %u, but still "
13076eea4f46SScott Long 		    "have %u command(s), sending target reset\n", targ->tid,
13086eea4f46SScott Long 		    cm_count);
130989d1c21fSKashyap D Desai 		if (!targ->is_nvme || sc->custom_nvme_tm_handling)
1310991554f2SKenneth D. Merry 			mprsas_send_reset(sc, tm,
1311991554f2SKenneth D. Merry 			    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
131289d1c21fSKashyap D Desai 		else
131389d1c21fSKashyap D Desai 			mpr_reinit(sc);
1314991554f2SKenneth D. Merry 	}
1315991554f2SKenneth D. Merry }
1316991554f2SKenneth D. Merry 
1317991554f2SKenneth D. Merry static void
1318991554f2SKenneth D. Merry mprsas_target_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1319991554f2SKenneth D. Merry {
1320991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1321991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1322991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1323991554f2SKenneth D. Merry 
1324991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1325991554f2SKenneth D. Merry 
1326991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1327991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1328991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1329991554f2SKenneth D. Merry 
1330991554f2SKenneth D. Merry 	/*
1331991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1332991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1333991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1334991554f2SKenneth D. Merry 	 */
1335991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
1336a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for target "
1337a2c14879SStephen McConnell 		    "reset! This should not happen!\n", __func__, tm->cm_flags);
1338991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1339991554f2SKenneth D. Merry 		return;
1340991554f2SKenneth D. Merry 	}
1341991554f2SKenneth D. Merry 
1342991554f2SKenneth D. Merry 	if (reply == NULL) {
13436eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY,
13446eea4f46SScott Long 		    "NULL target reset reply for tm %p TaskMID %u\n",
13456eea4f46SScott Long 		    tm, le16toh(req->TaskMID));
1346991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1347991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
13486eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
13496eea4f46SScott Long 			    "reset, ignoring NULL target reset reply\n");
1350991554f2SKenneth D. Merry 			targ->tm = NULL;
1351991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1352991554f2SKenneth D. Merry 		}
1353991554f2SKenneth D. Merry 		else {
1354991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
13556eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
13566eea4f46SScott Long 			    "target reset attempt, resetting controller\n");
1357991554f2SKenneth D. Merry 			mpr_reinit(sc);
1358991554f2SKenneth D. Merry 		}
1359991554f2SKenneth D. Merry 		return;
1360991554f2SKenneth D. Merry 	}
1361991554f2SKenneth D. Merry 
13626eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1363991554f2SKenneth D. Merry 	    "target reset status 0x%x code 0x%x count %u\n",
1364991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1365991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1366991554f2SKenneth D. Merry 
1367991554f2SKenneth D. Merry 	if (targ->outstanding == 0) {
13686eea4f46SScott Long 		/*
13696eea4f46SScott Long 		 * We've finished recovery for this target and all
1370991554f2SKenneth D. Merry 		 * of its logical units.
1371991554f2SKenneth D. Merry 		 */
13726eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
13736eea4f46SScott Long 		    "Finished reset recovery for target %u\n", targ->tid);
1374991554f2SKenneth D. Merry 
1375991554f2SKenneth D. Merry 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1376991554f2SKenneth D. Merry 		    CAM_LUN_WILDCARD);
1377991554f2SKenneth D. Merry 
1378991554f2SKenneth D. Merry 		targ->tm = NULL;
1379991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
13806eea4f46SScott Long 	} else {
13816eea4f46SScott Long 		/*
13826eea4f46SScott Long 		 * After a target reset, if this target still has
1383991554f2SKenneth D. Merry 		 * outstanding commands, the reset effectively failed,
1384991554f2SKenneth D. Merry 		 * regardless of the status reported.  escalate.
1385991554f2SKenneth D. Merry 		 */
13866eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
13876eea4f46SScott Long 		    "Target reset complete for target %u, but still have %u "
13886eea4f46SScott Long 		    "command(s), resetting controller\n", targ->tid,
13896eea4f46SScott Long 		    targ->outstanding);
1390991554f2SKenneth D. Merry 		mpr_reinit(sc);
1391991554f2SKenneth D. Merry 	}
1392991554f2SKenneth D. Merry }
1393991554f2SKenneth D. Merry 
1394991554f2SKenneth D. Merry #define MPR_RESET_TIMEOUT 30
1395991554f2SKenneth D. Merry 
1396a2c14879SStephen McConnell int
1397991554f2SKenneth D. Merry mprsas_send_reset(struct mpr_softc *sc, struct mpr_command *tm, uint8_t type)
1398991554f2SKenneth D. Merry {
1399991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1400991554f2SKenneth D. Merry 	struct mprsas_target *target;
140189d1c21fSKashyap D Desai 	int err, timeout;
1402991554f2SKenneth D. Merry 
1403991554f2SKenneth D. Merry 	target = tm->cm_targ;
1404991554f2SKenneth D. Merry 	if (target->handle == 0) {
1405a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s null devhandle for target_id "
1406a2c14879SStephen McConnell 		    "%d\n", __func__, target->tid);
1407991554f2SKenneth D. Merry 		return -1;
1408991554f2SKenneth D. Merry 	}
1409991554f2SKenneth D. Merry 
1410991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1411991554f2SKenneth D. Merry 	req->DevHandle = htole16(target->handle);
1412991554f2SKenneth D. Merry 	req->TaskType = type;
1413991554f2SKenneth D. Merry 
141489d1c21fSKashyap D Desai 	if (!target->is_nvme || sc->custom_nvme_tm_handling) {
141589d1c21fSKashyap D Desai 		timeout = MPR_RESET_TIMEOUT;
141689d1c21fSKashyap D Desai 		/*
141789d1c21fSKashyap D Desai 		 * Target reset method =
141889d1c21fSKashyap D Desai 		 *     SAS Hard Link Reset / SATA Link Reset
141989d1c21fSKashyap D Desai 		 */
142089d1c21fSKashyap D Desai 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
142189d1c21fSKashyap D Desai 	} else {
142289d1c21fSKashyap D Desai 		timeout = (target->controller_reset_timeout) ? (
142389d1c21fSKashyap D Desai 		    target->controller_reset_timeout) : (MPR_RESET_TIMEOUT);
142489d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
142589d1c21fSKashyap D Desai 		req->MsgFlags =
142689d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
142789d1c21fSKashyap D Desai 	}
142889d1c21fSKashyap D Desai 
1429991554f2SKenneth D. Merry 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1430991554f2SKenneth D. Merry 		/* XXX Need to handle invalid LUNs */
1431991554f2SKenneth D. Merry 		MPR_SET_LUN(req->LUN, tm->cm_lun);
1432991554f2SKenneth D. Merry 		tm->cm_targ->logical_unit_resets++;
14336eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14346eea4f46SScott Long 		    "Sending logical unit reset to target %u lun %d\n",
14356eea4f46SScott Long 		    target->tid, tm->cm_lun);
1436991554f2SKenneth D. Merry 		tm->cm_complete = mprsas_logical_unit_reset_complete;
1437a2c14879SStephen McConnell 		mprsas_prepare_for_tm(sc, tm, target, tm->cm_lun);
14386eea4f46SScott Long 	} else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1439991554f2SKenneth D. Merry 		tm->cm_targ->target_resets++;
14406eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14416eea4f46SScott Long 		    "Sending target reset to target %u\n", target->tid);
1442991554f2SKenneth D. Merry 		tm->cm_complete = mprsas_target_reset_complete;
1443a2c14879SStephen McConnell 		mprsas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1444991554f2SKenneth D. Merry 	}
1445991554f2SKenneth D. Merry 	else {
1446991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unexpected reset type 0x%x\n", type);
1447991554f2SKenneth D. Merry 		return -1;
1448991554f2SKenneth D. Merry 	}
1449991554f2SKenneth D. Merry 
1450991554f2SKenneth D. Merry 	if (target->encl_level_valid) {
14516eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14526eea4f46SScott Long 		    "At enclosure level %d, slot %d, connector name (%4s)\n",
14536eea4f46SScott Long 		    target->encl_level, target->encl_slot,
14546eea4f46SScott Long 		    target->connector_name);
1455991554f2SKenneth D. Merry 	}
1456991554f2SKenneth D. Merry 
1457991554f2SKenneth D. Merry 	tm->cm_data = NULL;
1458991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)tm;
1459991554f2SKenneth D. Merry 
146089d1c21fSKashyap D Desai 	callout_reset(&tm->cm_callout, timeout * hz,
1461991554f2SKenneth D. Merry 	    mprsas_tm_timeout, tm);
1462991554f2SKenneth D. Merry 
1463991554f2SKenneth D. Merry 	err = mpr_map_command(sc, tm);
1464991554f2SKenneth D. Merry 	if (err)
14656eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
1466a2c14879SStephen McConnell 		    "error %d sending reset type %u\n", err, type);
1467991554f2SKenneth D. Merry 
1468991554f2SKenneth D. Merry 	return err;
1469991554f2SKenneth D. Merry }
1470991554f2SKenneth D. Merry 
1471991554f2SKenneth D. Merry 
1472991554f2SKenneth D. Merry static void
1473991554f2SKenneth D. Merry mprsas_abort_complete(struct mpr_softc *sc, struct mpr_command *tm)
1474991554f2SKenneth D. Merry {
1475991554f2SKenneth D. Merry 	struct mpr_command *cm;
1476991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1477991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1478991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1479991554f2SKenneth D. Merry 
1480991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1481991554f2SKenneth D. Merry 
1482991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1483991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1484991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1485991554f2SKenneth D. Merry 
1486991554f2SKenneth D. Merry 	/*
1487991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1488991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1489991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1490991554f2SKenneth D. Merry 	 */
1491991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
14926eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_ERROR,
1493991554f2SKenneth D. Merry 		    "cm_flags = %#x for abort %p TaskMID %u!\n",
1494991554f2SKenneth D. Merry 		    tm->cm_flags, tm, le16toh(req->TaskMID));
1495991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1496991554f2SKenneth D. Merry 		return;
1497991554f2SKenneth D. Merry 	}
1498991554f2SKenneth D. Merry 
1499991554f2SKenneth D. Merry 	if (reply == NULL) {
15006eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY,
1501991554f2SKenneth D. Merry 		    "NULL abort reply for tm %p TaskMID %u\n",
1502991554f2SKenneth D. Merry 		    tm, le16toh(req->TaskMID));
1503991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1504991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
15056eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
15066eea4f46SScott Long 			    "reset, ignoring NULL abort reply\n");
1507991554f2SKenneth D. Merry 			targ->tm = NULL;
1508991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
15096eea4f46SScott Long 		} else {
1510991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
15116eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
15126eea4f46SScott Long 			    "abort attempt, resetting controller\n");
1513991554f2SKenneth D. Merry 			mpr_reinit(sc);
1514991554f2SKenneth D. Merry 		}
1515991554f2SKenneth D. Merry 		return;
1516991554f2SKenneth D. Merry 	}
1517991554f2SKenneth D. Merry 
15186eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1519991554f2SKenneth D. Merry 	    "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1520991554f2SKenneth D. Merry 	    le16toh(req->TaskMID),
1521991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1522991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1523991554f2SKenneth D. Merry 
1524991554f2SKenneth D. Merry 	cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1525991554f2SKenneth D. Merry 	if (cm == NULL) {
15266eea4f46SScott Long 		/*
15276eea4f46SScott Long 		 * if there are no more timedout commands, we're done with
1528991554f2SKenneth D. Merry 		 * error recovery for this target.
1529991554f2SKenneth D. Merry 		 */
15306eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15316eea4f46SScott Long 		    "Finished abort recovery for target %u\n", targ->tid);
1532991554f2SKenneth D. Merry 		targ->tm = NULL;
1533991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
15346eea4f46SScott Long 	} else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1535991554f2SKenneth D. Merry 		/* abort success, but we have more timedout commands to abort */
15366eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15376eea4f46SScott Long 		    "Continuing abort recovery for target %u\n", targ->tid);
1538991554f2SKenneth D. Merry 		mprsas_send_abort(sc, tm, cm);
15396eea4f46SScott Long 	} else {
15406eea4f46SScott Long 		/*
15416eea4f46SScott Long 		 * we didn't get a command completion, so the abort
1542991554f2SKenneth D. Merry 		 * failed as far as we're concerned.  escalate.
1543991554f2SKenneth D. Merry 		 */
15446eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15456eea4f46SScott Long 		    "Abort failed for target %u, sending logical unit reset\n",
15466eea4f46SScott Long 		    targ->tid);
1547991554f2SKenneth D. Merry 
1548991554f2SKenneth D. Merry 		mprsas_send_reset(sc, tm,
1549991554f2SKenneth D. Merry 		    MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1550991554f2SKenneth D. Merry 	}
1551991554f2SKenneth D. Merry }
1552991554f2SKenneth D. Merry 
1553991554f2SKenneth D. Merry #define MPR_ABORT_TIMEOUT 5
1554991554f2SKenneth D. Merry 
1555991554f2SKenneth D. Merry static int
1556991554f2SKenneth D. Merry mprsas_send_abort(struct mpr_softc *sc, struct mpr_command *tm,
1557991554f2SKenneth D. Merry     struct mpr_command *cm)
1558991554f2SKenneth D. Merry {
1559991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1560991554f2SKenneth D. Merry 	struct mprsas_target *targ;
156189d1c21fSKashyap D Desai 	int err, timeout;
1562991554f2SKenneth D. Merry 
1563991554f2SKenneth D. Merry 	targ = cm->cm_targ;
1564991554f2SKenneth D. Merry 	if (targ->handle == 0) {
15656eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
15666eea4f46SScott Long 		   "%s null devhandle for target_id %d\n",
1567991554f2SKenneth D. Merry 		    __func__, cm->cm_ccb->ccb_h.target_id);
1568991554f2SKenneth D. Merry 		return -1;
1569991554f2SKenneth D. Merry 	}
1570991554f2SKenneth D. Merry 
1571855fe445SScott Long 	mprsas_log_command(cm, MPR_RECOVERY|MPR_INFO,
1572991554f2SKenneth D. Merry 	    "Aborting command %p\n", cm);
1573991554f2SKenneth D. Merry 
1574991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1575991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
1576991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1577991554f2SKenneth D. Merry 
1578991554f2SKenneth D. Merry 	/* XXX Need to handle invalid LUNs */
1579991554f2SKenneth D. Merry 	MPR_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1580991554f2SKenneth D. Merry 
1581991554f2SKenneth D. Merry 	req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1582991554f2SKenneth D. Merry 
1583991554f2SKenneth D. Merry 	tm->cm_data = NULL;
1584991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_abort_complete;
1585991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)tm;
1586991554f2SKenneth D. Merry 	tm->cm_targ = cm->cm_targ;
1587991554f2SKenneth D. Merry 	tm->cm_lun = cm->cm_lun;
1588991554f2SKenneth D. Merry 
158989d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling)
159089d1c21fSKashyap D Desai 		timeout	= MPR_ABORT_TIMEOUT;
159189d1c21fSKashyap D Desai 	else
159289d1c21fSKashyap D Desai 		timeout = sc->nvme_abort_timeout;
159389d1c21fSKashyap D Desai 
159489d1c21fSKashyap D Desai 	callout_reset(&tm->cm_callout, timeout * hz,
1595991554f2SKenneth D. Merry 	    mprsas_tm_timeout, tm);
1596991554f2SKenneth D. Merry 
1597991554f2SKenneth D. Merry 	targ->aborts++;
1598991554f2SKenneth D. Merry 
1599a2c14879SStephen McConnell 	mprsas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1600a2c14879SStephen McConnell 
1601991554f2SKenneth D. Merry 	err = mpr_map_command(sc, tm);
1602991554f2SKenneth D. Merry 	if (err)
16036eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
1604991554f2SKenneth D. Merry 		    "error %d sending abort for cm %p SMID %u\n",
1605991554f2SKenneth D. Merry 		    err, cm, req->TaskMID);
1606991554f2SKenneth D. Merry 	return err;
1607991554f2SKenneth D. Merry }
1608991554f2SKenneth D. Merry 
1609991554f2SKenneth D. Merry static void
1610991554f2SKenneth D. Merry mprsas_scsiio_timeout(void *data)
1611991554f2SKenneth D. Merry {
16126eea4f46SScott Long 	sbintime_t elapsed, now;
16136eea4f46SScott Long 	union ccb *ccb;
1614991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1615991554f2SKenneth D. Merry 	struct mpr_command *cm;
1616991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1617991554f2SKenneth D. Merry 
1618991554f2SKenneth D. Merry 	cm = (struct mpr_command *)data;
1619991554f2SKenneth D. Merry 	sc = cm->cm_sc;
16206eea4f46SScott Long 	ccb = cm->cm_ccb;
16216eea4f46SScott Long 	now = sbinuptime();
1622991554f2SKenneth D. Merry 
1623991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1624991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1625991554f2SKenneth D. Merry 
16266eea4f46SScott Long 	mpr_dprint(sc, MPR_XINFO|MPR_RECOVERY, "Timeout checking cm %p\n", cm);
1627991554f2SKenneth D. Merry 
1628991554f2SKenneth D. Merry 	/*
1629991554f2SKenneth D. Merry 	 * Run the interrupt handler to make sure it's not pending.  This
1630991554f2SKenneth D. Merry 	 * isn't perfect because the command could have already completed
1631991554f2SKenneth D. Merry 	 * and been re-used, though this is unlikely.
1632991554f2SKenneth D. Merry 	 */
1633991554f2SKenneth D. Merry 	mpr_intr_locked(sc);
16348fe7bf06SWarner Losh 	if (cm->cm_flags & MPR_CM_FLAGS_ON_RECOVERY) {
1635991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_XINFO,
1636991554f2SKenneth D. Merry 		    "SCSI command %p almost timed out\n", cm);
1637991554f2SKenneth D. Merry 		return;
1638991554f2SKenneth D. Merry 	}
1639991554f2SKenneth D. Merry 
1640991554f2SKenneth D. Merry 	if (cm->cm_ccb == NULL) {
1641991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "command timeout with NULL ccb\n");
1642991554f2SKenneth D. Merry 		return;
1643991554f2SKenneth D. Merry 	}
1644991554f2SKenneth D. Merry 
1645991554f2SKenneth D. Merry 	targ = cm->cm_targ;
1646991554f2SKenneth D. Merry 	targ->timeouts++;
1647991554f2SKenneth D. Merry 
16486eea4f46SScott Long 	elapsed = now - ccb->ccb_h.qos.sim_data;
16496eea4f46SScott Long 	mprsas_log_command(cm, MPR_INFO|MPR_RECOVERY,
16506eea4f46SScott Long 	    "Command timeout on target %u(0x%04x), %d set, %d.%d elapsed\n",
16516eea4f46SScott Long 	    targ->tid, targ->handle, ccb->ccb_h.timeout,
16526eea4f46SScott Long 	    sbintime_getsec(elapsed), elapsed & 0xffffffff);
1653991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
16546eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
16556eea4f46SScott Long 		    "At enclosure level %d, slot %d, connector name (%4s)\n",
16566eea4f46SScott Long 		    targ->encl_level, targ->encl_slot, targ->connector_name);
1657991554f2SKenneth D. Merry 	}
1658991554f2SKenneth D. Merry 
1659991554f2SKenneth D. Merry 	/* XXX first, check the firmware state, to see if it's still
1660991554f2SKenneth D. Merry 	 * operational.  if not, do a diag reset.
1661991554f2SKenneth D. Merry 	 */
1662a2c14879SStephen McConnell 	mprsas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
16638fe7bf06SWarner Losh 	cm->cm_flags |= MPR_CM_FLAGS_ON_RECOVERY | MPR_CM_FLAGS_TIMEDOUT;
1664991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1665991554f2SKenneth D. Merry 
1666991554f2SKenneth D. Merry 	if (targ->tm != NULL) {
1667991554f2SKenneth D. Merry 		/* target already in recovery, just queue up another
1668991554f2SKenneth D. Merry 		 * timedout command to be processed later.
1669991554f2SKenneth D. Merry 		 */
1670991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_RECOVERY, "queued timedout cm %p for "
1671991554f2SKenneth D. Merry 		    "processing by tm %p\n", cm, targ->tm);
1672991554f2SKenneth D. Merry 	}
1673991554f2SKenneth D. Merry 	else if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1674991554f2SKenneth D. Merry 
1675991554f2SKenneth D. Merry 		/* start recovery by aborting the first timedout command */
16766eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
16776eea4f46SScott Long 		    "Sending abort to target %u for SMID %d\n", targ->tid,
16786eea4f46SScott Long 		    cm->cm_desc.Default.SMID);
16796eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY, "timedout cm %p allocated tm %p\n",
16806eea4f46SScott Long 		    cm, targ->tm);
1681991554f2SKenneth D. Merry 		mprsas_send_abort(sc, targ->tm, cm);
1682991554f2SKenneth D. Merry 	}
1683991554f2SKenneth D. Merry 	else {
1684991554f2SKenneth D. Merry 		/* XXX queue this target up for recovery once a TM becomes
1685991554f2SKenneth D. Merry 		 * available.  The firmware only has a limited number of
1686991554f2SKenneth D. Merry 		 * HighPriority credits for the high priority requests used
1687991554f2SKenneth D. Merry 		 * for task management, and we ran out.
1688991554f2SKenneth D. Merry 		 *
1689991554f2SKenneth D. Merry 		 * Isilon: don't worry about this for now, since we have
1690991554f2SKenneth D. Merry 		 * more credits than disks in an enclosure, and limit
1691991554f2SKenneth D. Merry 		 * ourselves to one TM per target for recovery.
1692991554f2SKenneth D. Merry 		 */
16936eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
16946eea4f46SScott Long 		    "timedout cm %p failed to allocate a tm\n", cm);
1695991554f2SKenneth D. Merry 	}
1696991554f2SKenneth D. Merry }
1697991554f2SKenneth D. Merry 
169867feec50SStephen McConnell /**
169967feec50SStephen McConnell  * mprsas_build_nvme_unmap - Build Native NVMe DSM command equivalent
170067feec50SStephen McConnell  *			     to SCSI Unmap.
170167feec50SStephen McConnell  * Return 0 - for success,
170267feec50SStephen McConnell  *	  1 - to immediately return back the command with success status to CAM
170367feec50SStephen McConnell  *	  negative value - to fallback to firmware path i.e. issue scsi unmap
170467feec50SStephen McConnell  *			   to FW without any translation.
170567feec50SStephen McConnell  */
170667feec50SStephen McConnell static int
170767feec50SStephen McConnell mprsas_build_nvme_unmap(struct mpr_softc *sc, struct mpr_command *cm,
170867feec50SStephen McConnell     union ccb *ccb, struct mprsas_target *targ)
170967feec50SStephen McConnell {
171067feec50SStephen McConnell 	Mpi26NVMeEncapsulatedRequest_t *req = NULL;
171167feec50SStephen McConnell 	struct ccb_scsiio *csio;
171267feec50SStephen McConnell 	struct unmap_parm_list *plist;
171367feec50SStephen McConnell 	struct nvme_dsm_range *nvme_dsm_ranges = NULL;
171467feec50SStephen McConnell 	struct nvme_command *c;
171567feec50SStephen McConnell 	int i, res;
171667feec50SStephen McConnell 	uint16_t ndesc, list_len, data_length;
171767feec50SStephen McConnell 	struct mpr_prp_page *prp_page_info;
171867feec50SStephen McConnell 	uint64_t nvme_dsm_ranges_dma_handle;
171967feec50SStephen McConnell 
172067feec50SStephen McConnell 	csio = &ccb->csio;
172167feec50SStephen McConnell 	list_len = (scsiio_cdb_ptr(csio)[7] << 8 | scsiio_cdb_ptr(csio)[8]);
172267feec50SStephen McConnell 	if (!list_len) {
172367feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "Parameter list length is Zero\n");
172467feec50SStephen McConnell 		return -EINVAL;
172567feec50SStephen McConnell 	}
172667feec50SStephen McConnell 
172767feec50SStephen McConnell 	plist = malloc(csio->dxfer_len, M_MPR, M_ZERO|M_NOWAIT);
172867feec50SStephen McConnell 	if (!plist) {
172967feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "Unable to allocate memory to "
173067feec50SStephen McConnell 		    "save UNMAP data\n");
173167feec50SStephen McConnell 		return -ENOMEM;
173267feec50SStephen McConnell 	}
173367feec50SStephen McConnell 
173467feec50SStephen McConnell 	/* Copy SCSI unmap data to a local buffer */
173567feec50SStephen McConnell 	bcopy(csio->data_ptr, plist, csio->dxfer_len);
173667feec50SStephen McConnell 
173767feec50SStephen McConnell 	/* return back the unmap command to CAM with success status,
173867feec50SStephen McConnell 	 * if number of descripts is zero.
173967feec50SStephen McConnell 	 */
174067feec50SStephen McConnell 	ndesc = be16toh(plist->unmap_blk_desc_data_len) >> 4;
174167feec50SStephen McConnell 	if (!ndesc) {
174267feec50SStephen McConnell 		mpr_dprint(sc, MPR_XINFO, "Number of descriptors in "
174367feec50SStephen McConnell 		    "UNMAP cmd is Zero\n");
174467feec50SStephen McConnell 		res = 1;
174567feec50SStephen McConnell 		goto out;
174667feec50SStephen McConnell 	}
174767feec50SStephen McConnell 
174867feec50SStephen McConnell 	data_length = ndesc * sizeof(struct nvme_dsm_range);
174967feec50SStephen McConnell 	if (data_length > targ->MDTS) {
175067feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "data length: %d is greater than "
175167feec50SStephen McConnell 		    "Device's MDTS: %d\n", data_length, targ->MDTS);
175267feec50SStephen McConnell 		res = -EINVAL;
175367feec50SStephen McConnell 		goto out;
175467feec50SStephen McConnell 	}
175567feec50SStephen McConnell 
175667feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
175767feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There is no PRP Page for "
175867feec50SStephen McConnell 	    "UNMAP command.\n", __func__));
175967feec50SStephen McConnell 
176067feec50SStephen McConnell 	/*
176167feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
176267feec50SStephen McConnell 	 * will be freed when the command is freed.
176367feec50SStephen McConnell 	 */
176467feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
176567feec50SStephen McConnell 
176667feec50SStephen McConnell 	nvme_dsm_ranges = (struct nvme_dsm_range *)prp_page_info->prp_page;
176767feec50SStephen McConnell 	nvme_dsm_ranges_dma_handle = prp_page_info->prp_page_busaddr;
176867feec50SStephen McConnell 
176967feec50SStephen McConnell 	bzero(nvme_dsm_ranges, data_length);
177067feec50SStephen McConnell 
177167feec50SStephen McConnell 	/* Convert SCSI unmap's descriptor data to NVMe DSM specific Range data
177267feec50SStephen McConnell 	 * for each descriptors contained in SCSI UNMAP data.
177367feec50SStephen McConnell 	 */
177467feec50SStephen McConnell 	for (i = 0; i < ndesc; i++) {
177567feec50SStephen McConnell 		nvme_dsm_ranges[i].length =
177667feec50SStephen McConnell 		    htole32(be32toh(plist->desc[i].nlb));
177767feec50SStephen McConnell 		nvme_dsm_ranges[i].starting_lba =
177867feec50SStephen McConnell 		    htole64(be64toh(plist->desc[i].slba));
177967feec50SStephen McConnell 		nvme_dsm_ranges[i].attributes = 0;
178067feec50SStephen McConnell 	}
178167feec50SStephen McConnell 
178267feec50SStephen McConnell 	/* Build MPI2.6's NVMe Encapsulated Request Message */
178367feec50SStephen McConnell 	req = (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req;
178467feec50SStephen McConnell 	bzero(req, sizeof(*req));
178567feec50SStephen McConnell 	req->DevHandle = htole16(targ->handle);
178667feec50SStephen McConnell 	req->Function = MPI2_FUNCTION_NVME_ENCAPSULATED;
178767feec50SStephen McConnell 	req->Flags = MPI26_NVME_FLAGS_WRITE;
178867feec50SStephen McConnell 	req->ErrorResponseBaseAddress.High =
178967feec50SStephen McConnell 	    htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32));
179067feec50SStephen McConnell 	req->ErrorResponseBaseAddress.Low =
179167feec50SStephen McConnell 	    htole32(cm->cm_sense_busaddr);
179267feec50SStephen McConnell 	req->ErrorResponseAllocationLength =
179367feec50SStephen McConnell 	    htole16(sizeof(struct nvme_completion));
179467feec50SStephen McConnell 	req->EncapsulatedCommandLength =
179567feec50SStephen McConnell 	    htole16(sizeof(struct nvme_command));
179667feec50SStephen McConnell 	req->DataLength = htole32(data_length);
179767feec50SStephen McConnell 
179867feec50SStephen McConnell 	/* Build NVMe DSM command */
179967feec50SStephen McConnell 	c = (struct nvme_command *) req->NVMe_Command;
18009544e6dcSChuck Tuffli 	c->opc = NVME_OPC_DATASET_MANAGEMENT;
180167feec50SStephen McConnell 	c->nsid = htole32(csio->ccb_h.target_lun + 1);
180267feec50SStephen McConnell 	c->cdw10 = htole32(ndesc - 1);
180367feec50SStephen McConnell 	c->cdw11 = htole32(NVME_DSM_ATTR_DEALLOCATE);
180467feec50SStephen McConnell 
180567feec50SStephen McConnell 	cm->cm_length = data_length;
180667feec50SStephen McConnell 	cm->cm_data = NULL;
180767feec50SStephen McConnell 
180867feec50SStephen McConnell 	cm->cm_complete = mprsas_scsiio_complete;
180967feec50SStephen McConnell 	cm->cm_complete_data = ccb;
181067feec50SStephen McConnell 	cm->cm_targ = targ;
181167feec50SStephen McConnell 	cm->cm_lun = csio->ccb_h.target_lun;
181267feec50SStephen McConnell 	cm->cm_ccb = ccb;
181367feec50SStephen McConnell 
181467feec50SStephen McConnell 	cm->cm_desc.Default.RequestFlags =
181567feec50SStephen McConnell 	    MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
181667feec50SStephen McConnell 
18176eea4f46SScott Long 	csio->ccb_h.qos.sim_data = sbinuptime();
181867feec50SStephen McConnell 	callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
181967feec50SStephen McConnell 	    mprsas_scsiio_timeout, cm, 0);
182067feec50SStephen McConnell 
182167feec50SStephen McConnell 	targ->issued++;
182267feec50SStephen McConnell 	targ->outstanding++;
182367feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
182467feec50SStephen McConnell 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
182567feec50SStephen McConnell 
182667feec50SStephen McConnell 	mprsas_log_command(cm, MPR_XINFO, "%s cm %p ccb %p outstanding %u\n",
182767feec50SStephen McConnell 	    __func__, cm, ccb, targ->outstanding);
182867feec50SStephen McConnell 
182918982e8fSStephen McConnell 	mpr_build_nvme_prp(sc, cm, req,
183018982e8fSStephen McConnell 	    (void *)(uintptr_t)nvme_dsm_ranges_dma_handle, 0, data_length);
183167feec50SStephen McConnell 	mpr_map_command(sc, cm);
183267feec50SStephen McConnell 
183367feec50SStephen McConnell out:
183467feec50SStephen McConnell 	free(plist, M_MPR);
183567feec50SStephen McConnell 	return 0;
183667feec50SStephen McConnell }
183767feec50SStephen McConnell 
1838991554f2SKenneth D. Merry static void
1839991554f2SKenneth D. Merry mprsas_action_scsiio(struct mprsas_softc *sassc, union ccb *ccb)
1840991554f2SKenneth D. Merry {
1841991554f2SKenneth D. Merry 	MPI2_SCSI_IO_REQUEST *req;
1842991554f2SKenneth D. Merry 	struct ccb_scsiio *csio;
1843991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1844991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1845991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1846991554f2SKenneth D. Merry 	struct mpr_command *cm;
184767feec50SStephen McConnell 	uint8_t i, lba_byte, *ref_tag_addr, scsi_opcode;
1848991554f2SKenneth D. Merry 	uint16_t eedp_flags;
1849991554f2SKenneth D. Merry 	uint32_t mpi_control;
185067feec50SStephen McConnell 	int rc;
1851991554f2SKenneth D. Merry 
1852991554f2SKenneth D. Merry 	sc = sassc->sc;
1853991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1854991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1855991554f2SKenneth D. Merry 
1856991554f2SKenneth D. Merry 	csio = &ccb->csio;
1857a2c14879SStephen McConnell 	KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1858a2c14879SStephen McConnell 	    ("Target %d out of bounds in XPT_SCSI_IO\n",
1859a2c14879SStephen McConnell 	     csio->ccb_h.target_id));
1860991554f2SKenneth D. Merry 	targ = &sassc->targets[csio->ccb_h.target_id];
1861991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1862991554f2SKenneth D. Merry 	if (targ->handle == 0x0) {
1863991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s NULL handle for target %u\n",
1864991554f2SKenneth D. Merry 		    __func__, csio->ccb_h.target_id);
1865a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1866991554f2SKenneth D. Merry 		xpt_done(ccb);
1867991554f2SKenneth D. Merry 		return;
1868991554f2SKenneth D. Merry 	}
1869991554f2SKenneth D. Merry 	if (targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT) {
1870a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s Raid component no SCSI IO "
1871991554f2SKenneth D. Merry 		    "supported %u\n", __func__, csio->ccb_h.target_id);
1872a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1873991554f2SKenneth D. Merry 		xpt_done(ccb);
1874991554f2SKenneth D. Merry 		return;
1875991554f2SKenneth D. Merry 	}
1876991554f2SKenneth D. Merry 	/*
1877991554f2SKenneth D. Merry 	 * Sometimes, it is possible to get a command that is not "In
1878991554f2SKenneth D. Merry 	 * Progress" and was actually aborted by the upper layer.  Check for
1879991554f2SKenneth D. Merry 	 * this here and complete the command without error.
1880991554f2SKenneth D. Merry 	 */
1881a2c14879SStephen McConnell 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1882991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE, "%s Command is not in progress for "
1883991554f2SKenneth D. Merry 		    "target %u\n", __func__, csio->ccb_h.target_id);
1884991554f2SKenneth D. Merry 		xpt_done(ccb);
1885991554f2SKenneth D. Merry 		return;
1886991554f2SKenneth D. Merry 	}
1887991554f2SKenneth D. Merry 	/*
1888991554f2SKenneth D. Merry 	 * If devinfo is 0 this will be a volume.  In that case don't tell CAM
1889991554f2SKenneth D. Merry 	 * that the volume has timed out.  We want volumes to be enumerated
18904c1cdd4aSWarner Losh 	 * until they are deleted/removed, not just failed. In either event,
18914c1cdd4aSWarner Losh 	 * we're removing the target due to a firmware event telling us
18924c1cdd4aSWarner Losh 	 * the device is now gone (as opposed to some transient event). Since
18934c1cdd4aSWarner Losh 	 * we're opting to remove failed devices from the OS's view, we need
18944c1cdd4aSWarner Losh 	 * to propagate that status up the stack.
1895991554f2SKenneth D. Merry 	 */
1896991554f2SKenneth D. Merry 	if (targ->flags & MPRSAS_TARGET_INREMOVAL) {
1897991554f2SKenneth D. Merry 		if (targ->devinfo == 0)
1898a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1899991554f2SKenneth D. Merry 		else
19004c1cdd4aSWarner Losh 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1901991554f2SKenneth D. Merry 		xpt_done(ccb);
1902991554f2SKenneth D. Merry 		return;
1903991554f2SKenneth D. Merry 	}
1904991554f2SKenneth D. Merry 
1905991554f2SKenneth D. Merry 	if ((sc->mpr_flags & MPR_FLAGS_SHUTDOWN) != 0) {
1906a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s shutting down\n", __func__);
1907a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1908a2c14879SStephen McConnell 		xpt_done(ccb);
1909a2c14879SStephen McConnell 		return;
1910a2c14879SStephen McConnell 	}
1911a2c14879SStephen McConnell 
1912a2c14879SStephen McConnell 	/*
1913a2c14879SStephen McConnell 	 * If target has a reset in progress, freeze the devq and return.  The
1914a2c14879SStephen McConnell 	 * devq will be released when the TM reset is finished.
1915a2c14879SStephen McConnell 	 */
1916a2c14879SStephen McConnell 	if (targ->flags & MPRSAS_TARGET_INRESET) {
1917a2c14879SStephen McConnell 		ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1918a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s: Freezing devq for target ID %d\n",
1919a2c14879SStephen McConnell 		    __func__, targ->tid);
1920a2c14879SStephen McConnell 		xpt_freeze_devq(ccb->ccb_h.path, 1);
1921991554f2SKenneth D. Merry 		xpt_done(ccb);
1922991554f2SKenneth D. Merry 		return;
1923991554f2SKenneth D. Merry 	}
1924991554f2SKenneth D. Merry 
1925991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
1926991554f2SKenneth D. Merry 	if (cm == NULL || (sc->mpr_flags & MPR_FLAGS_DIAGRESET)) {
1927991554f2SKenneth D. Merry 		if (cm != NULL) {
1928991554f2SKenneth D. Merry 			mpr_free_command(sc, cm);
1929991554f2SKenneth D. Merry 		}
1930991554f2SKenneth D. Merry 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
1931991554f2SKenneth D. Merry 			xpt_freeze_simq(sassc->sim, 1);
1932991554f2SKenneth D. Merry 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
1933991554f2SKenneth D. Merry 		}
1934991554f2SKenneth D. Merry 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1935991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1936991554f2SKenneth D. Merry 		xpt_done(ccb);
1937991554f2SKenneth D. Merry 		return;
1938991554f2SKenneth D. Merry 	}
1939991554f2SKenneth D. Merry 
194067feec50SStephen McConnell 	/* For NVME device's issue UNMAP command directly to NVME drives by
194167feec50SStephen McConnell 	 * constructing equivalent native NVMe DataSetManagement command.
194267feec50SStephen McConnell 	 */
194367feec50SStephen McConnell 	scsi_opcode = scsiio_cdb_ptr(csio)[0];
194467feec50SStephen McConnell 	if (scsi_opcode == UNMAP &&
194567feec50SStephen McConnell 	    targ->is_nvme &&
194667feec50SStephen McConnell 	    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
194767feec50SStephen McConnell 		rc = mprsas_build_nvme_unmap(sc, cm, ccb, targ);
194867feec50SStephen McConnell 		if (rc == 1) { /* return command to CAM with success status */
194967feec50SStephen McConnell 			mpr_free_command(sc, cm);
195067feec50SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
195167feec50SStephen McConnell 			xpt_done(ccb);
195267feec50SStephen McConnell 			return;
195367feec50SStephen McConnell 		} else if (!rc) /* Issued NVMe Encapsulated Request Message */
195467feec50SStephen McConnell 			return;
195567feec50SStephen McConnell 	}
195667feec50SStephen McConnell 
1957991554f2SKenneth D. Merry 	req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1958991554f2SKenneth D. Merry 	bzero(req, sizeof(*req));
1959991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
1960991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1961991554f2SKenneth D. Merry 	req->MsgFlags = 0;
1962991554f2SKenneth D. Merry 	req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1963991554f2SKenneth D. Merry 	req->SenseBufferLength = MPR_SENSE_LEN;
1964991554f2SKenneth D. Merry 	req->SGLFlags = 0;
1965991554f2SKenneth D. Merry 	req->ChainOffset = 0;
1966991554f2SKenneth D. Merry 	req->SGLOffset0 = 24;	/* 32bit word offset to the SGL */
1967991554f2SKenneth D. Merry 	req->SGLOffset1= 0;
1968991554f2SKenneth D. Merry 	req->SGLOffset2= 0;
1969991554f2SKenneth D. Merry 	req->SGLOffset3= 0;
1970991554f2SKenneth D. Merry 	req->SkipCount = 0;
1971991554f2SKenneth D. Merry 	req->DataLength = htole32(csio->dxfer_len);
1972991554f2SKenneth D. Merry 	req->BidirectionalDataLength = 0;
1973991554f2SKenneth D. Merry 	req->IoFlags = htole16(csio->cdb_len);
1974991554f2SKenneth D. Merry 	req->EEDPFlags = 0;
1975991554f2SKenneth D. Merry 
1976991554f2SKenneth D. Merry 	/* Note: BiDirectional transfers are not supported */
1977991554f2SKenneth D. Merry 	switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1978991554f2SKenneth D. Merry 	case CAM_DIR_IN:
1979991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
1980991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
1981991554f2SKenneth D. Merry 		break;
1982991554f2SKenneth D. Merry 	case CAM_DIR_OUT:
1983991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1984991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
1985991554f2SKenneth D. Merry 		break;
1986991554f2SKenneth D. Merry 	case CAM_DIR_NONE:
1987991554f2SKenneth D. Merry 	default:
1988991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1989991554f2SKenneth D. Merry 		break;
1990991554f2SKenneth D. Merry 	}
1991991554f2SKenneth D. Merry 
1992991554f2SKenneth D. Merry 	if (csio->cdb_len == 32)
1993991554f2SKenneth D. Merry 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1994991554f2SKenneth D. Merry 	/*
1995991554f2SKenneth D. Merry 	 * It looks like the hardware doesn't require an explicit tag
1996991554f2SKenneth D. Merry 	 * number for each transaction.  SAM Task Management not supported
1997991554f2SKenneth D. Merry 	 * at the moment.
1998991554f2SKenneth D. Merry 	 */
1999991554f2SKenneth D. Merry 	switch (csio->tag_action) {
2000991554f2SKenneth D. Merry 	case MSG_HEAD_OF_Q_TAG:
2001991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
2002991554f2SKenneth D. Merry 		break;
2003991554f2SKenneth D. Merry 	case MSG_ORDERED_Q_TAG:
2004991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2005991554f2SKenneth D. Merry 		break;
2006991554f2SKenneth D. Merry 	case MSG_ACA_TASK:
2007991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
2008991554f2SKenneth D. Merry 		break;
2009991554f2SKenneth D. Merry 	case CAM_TAG_ACTION_NONE:
2010991554f2SKenneth D. Merry 	case MSG_SIMPLE_Q_TAG:
2011991554f2SKenneth D. Merry 	default:
2012991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2013991554f2SKenneth D. Merry 		break;
2014991554f2SKenneth D. Merry 	}
2015991554f2SKenneth D. Merry 	mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
2016991554f2SKenneth D. Merry 	req->Control = htole32(mpi_control);
2017991554f2SKenneth D. Merry 
2018991554f2SKenneth D. Merry 	if (MPR_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
2019991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2020a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_LUN_INVALID);
2021991554f2SKenneth D. Merry 		xpt_done(ccb);
2022991554f2SKenneth D. Merry 		return;
2023991554f2SKenneth D. Merry 	}
2024991554f2SKenneth D. Merry 
2025991554f2SKenneth D. Merry 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
2026991554f2SKenneth D. Merry 		bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
2027fa699bb2SAlan Somers 	else {
2028fa699bb2SAlan Somers 		KASSERT(csio->cdb_len <= IOCDBLEN,
202967feec50SStephen McConnell 		    ("cdb_len %d is greater than IOCDBLEN but CAM_CDB_POINTER "
203067feec50SStephen McConnell 		    "is not set", csio->cdb_len));
2031991554f2SKenneth D. Merry 		bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
2032fa699bb2SAlan Somers 	}
2033991554f2SKenneth D. Merry 	req->IoFlags = htole16(csio->cdb_len);
2034991554f2SKenneth D. Merry 
2035991554f2SKenneth D. Merry 	/*
2036991554f2SKenneth D. Merry 	 * Check if EEDP is supported and enabled.  If it is then check if the
2037991554f2SKenneth D. Merry 	 * SCSI opcode could be using EEDP.  If so, make sure the LUN exists and
2038991554f2SKenneth D. Merry 	 * is formatted for EEDP support.  If all of this is true, set CDB up
2039991554f2SKenneth D. Merry 	 * for EEDP transfer.
2040991554f2SKenneth D. Merry 	 */
2041991554f2SKenneth D. Merry 	eedp_flags = op_code_prot[req->CDB.CDB32[0]];
2042991554f2SKenneth D. Merry 	if (sc->eedp_enabled && eedp_flags) {
2043991554f2SKenneth D. Merry 		SLIST_FOREACH(lun, &targ->luns, lun_link) {
2044991554f2SKenneth D. Merry 			if (lun->lun_id == csio->ccb_h.target_lun) {
2045991554f2SKenneth D. Merry 				break;
2046991554f2SKenneth D. Merry 			}
2047991554f2SKenneth D. Merry 		}
2048991554f2SKenneth D. Merry 
2049991554f2SKenneth D. Merry 		if ((lun != NULL) && (lun->eedp_formatted)) {
2050991554f2SKenneth D. Merry 			req->EEDPBlockSize = htole16(lun->eedp_block_size);
2051991554f2SKenneth D. Merry 			eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2052991554f2SKenneth D. Merry 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2053991554f2SKenneth D. Merry 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
205467feec50SStephen McConnell 			if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC) {
205567feec50SStephen McConnell 				eedp_flags |=
205667feec50SStephen McConnell 				    MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
205767feec50SStephen McConnell 			}
2058991554f2SKenneth D. Merry 			req->EEDPFlags = htole16(eedp_flags);
2059991554f2SKenneth D. Merry 
2060991554f2SKenneth D. Merry 			/*
2061991554f2SKenneth D. Merry 			 * If CDB less than 32, fill in Primary Ref Tag with
2062991554f2SKenneth D. Merry 			 * low 4 bytes of LBA.  If CDB is 32, tag stuff is
2063991554f2SKenneth D. Merry 			 * already there.  Also, set protection bit.  FreeBSD
2064991554f2SKenneth D. Merry 			 * currently does not support CDBs bigger than 16, but
2065991554f2SKenneth D. Merry 			 * the code doesn't hurt, and will be here for the
2066991554f2SKenneth D. Merry 			 * future.
2067991554f2SKenneth D. Merry 			 */
2068991554f2SKenneth D. Merry 			if (csio->cdb_len != 32) {
2069991554f2SKenneth D. Merry 				lba_byte = (csio->cdb_len == 16) ? 6 : 2;
2070991554f2SKenneth D. Merry 				ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
2071991554f2SKenneth D. Merry 				    PrimaryReferenceTag;
2072991554f2SKenneth D. Merry 				for (i = 0; i < 4; i++) {
2073991554f2SKenneth D. Merry 					*ref_tag_addr =
2074991554f2SKenneth D. Merry 					    req->CDB.CDB32[lba_byte + i];
2075991554f2SKenneth D. Merry 					ref_tag_addr++;
2076991554f2SKenneth D. Merry 				}
2077991554f2SKenneth D. Merry 				req->CDB.EEDP32.PrimaryReferenceTag =
2078991554f2SKenneth D. Merry 				    htole32(req->
2079991554f2SKenneth D. Merry 				    CDB.EEDP32.PrimaryReferenceTag);
2080991554f2SKenneth D. Merry 				req->CDB.EEDP32.PrimaryApplicationTagMask =
2081991554f2SKenneth D. Merry 				    0xFFFF;
20828881681bSKenneth D. Merry 				req->CDB.CDB32[1] =
20838881681bSKenneth D. Merry 				    (req->CDB.CDB32[1] & 0x1F) | 0x20;
2084991554f2SKenneth D. Merry 			} else {
2085991554f2SKenneth D. Merry 				eedp_flags |=
2086991554f2SKenneth D. Merry 				    MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
2087991554f2SKenneth D. Merry 				req->EEDPFlags = htole16(eedp_flags);
2088991554f2SKenneth D. Merry 				req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
2089991554f2SKenneth D. Merry 				    0x1F) | 0x20;
2090991554f2SKenneth D. Merry 			}
2091991554f2SKenneth D. Merry 		}
2092991554f2SKenneth D. Merry 	}
2093991554f2SKenneth D. Merry 
2094991554f2SKenneth D. Merry 	cm->cm_length = csio->dxfer_len;
2095991554f2SKenneth D. Merry 	if (cm->cm_length != 0) {
2096991554f2SKenneth D. Merry 		cm->cm_data = ccb;
2097991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_USE_CCB;
2098991554f2SKenneth D. Merry 	} else {
2099991554f2SKenneth D. Merry 		cm->cm_data = NULL;
2100991554f2SKenneth D. Merry 	}
2101991554f2SKenneth D. Merry 	cm->cm_sge = &req->SGL;
2102991554f2SKenneth D. Merry 	cm->cm_sglsize = (32 - 24) * 4;
2103991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_scsiio_complete;
2104991554f2SKenneth D. Merry 	cm->cm_complete_data = ccb;
2105991554f2SKenneth D. Merry 	cm->cm_targ = targ;
2106991554f2SKenneth D. Merry 	cm->cm_lun = csio->ccb_h.target_lun;
2107991554f2SKenneth D. Merry 	cm->cm_ccb = ccb;
2108991554f2SKenneth D. Merry 	/*
2109991554f2SKenneth D. Merry 	 * If using FP desc type, need to set a bit in IoFlags (SCSI IO is 0)
2110991554f2SKenneth D. Merry 	 * and set descriptor type.
2111991554f2SKenneth D. Merry 	 */
2112991554f2SKenneth D. Merry 	if (targ->scsi_req_desc_type ==
2113991554f2SKenneth D. Merry 	    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
2114991554f2SKenneth D. Merry 		req->IoFlags |= MPI25_SCSIIO_IOFLAGS_FAST_PATH;
2115991554f2SKenneth D. Merry 		cm->cm_desc.FastPathSCSIIO.RequestFlags =
2116991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
211767feec50SStephen McConnell 		if (!sc->atomic_desc_capable) {
211867feec50SStephen McConnell 			cm->cm_desc.FastPathSCSIIO.DevHandle =
211967feec50SStephen McConnell 			    htole16(targ->handle);
212067feec50SStephen McConnell 		}
2121991554f2SKenneth D. Merry 	} else {
2122991554f2SKenneth D. Merry 		cm->cm_desc.SCSIIO.RequestFlags =
2123991554f2SKenneth D. Merry 		    MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
212467feec50SStephen McConnell 		if (!sc->atomic_desc_capable)
2125991554f2SKenneth D. Merry 			cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
2126991554f2SKenneth D. Merry 	}
2127991554f2SKenneth D. Merry 
21286eea4f46SScott Long 	csio->ccb_h.qos.sim_data = sbinuptime();
212985c9dd9dSSteven Hartland 	callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
213085c9dd9dSSteven Hartland 	    mprsas_scsiio_timeout, cm, 0);
2131991554f2SKenneth D. Merry 
2132991554f2SKenneth D. Merry 	targ->issued++;
2133991554f2SKenneth D. Merry 	targ->outstanding++;
2134991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
2135991554f2SKenneth D. Merry 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
2136991554f2SKenneth D. Merry 
2137991554f2SKenneth D. Merry 	mprsas_log_command(cm, MPR_XINFO, "%s cm %p ccb %p outstanding %u\n",
2138991554f2SKenneth D. Merry 	    __func__, cm, ccb, targ->outstanding);
2139991554f2SKenneth D. Merry 
2140991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
2141991554f2SKenneth D. Merry 	return;
2142991554f2SKenneth D. Merry }
2143991554f2SKenneth D. Merry 
2144991554f2SKenneth D. Merry /**
2145991554f2SKenneth D. Merry  * mpr_sc_failed_io_info - translated non-succesfull SCSI_IO request
2146991554f2SKenneth D. Merry  */
2147991554f2SKenneth D. Merry static void
2148991554f2SKenneth D. Merry mpr_sc_failed_io_info(struct mpr_softc *sc, struct ccb_scsiio *csio,
2149991554f2SKenneth D. Merry     Mpi2SCSIIOReply_t *mpi_reply, struct mprsas_target *targ)
2150991554f2SKenneth D. Merry {
2151991554f2SKenneth D. Merry 	u32 response_info;
2152991554f2SKenneth D. Merry 	u8 *response_bytes;
2153991554f2SKenneth D. Merry 	u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
2154991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_MASK;
2155991554f2SKenneth D. Merry 	u8 scsi_state = mpi_reply->SCSIState;
2156991554f2SKenneth D. Merry 	u8 scsi_status = mpi_reply->SCSIStatus;
2157991554f2SKenneth D. Merry 	char *desc_ioc_state = NULL;
2158991554f2SKenneth D. Merry 	char *desc_scsi_status = NULL;
2159991554f2SKenneth D. Merry 	u32 log_info = le32toh(mpi_reply->IOCLogInfo);
2160991554f2SKenneth D. Merry 
2161991554f2SKenneth D. Merry 	if (log_info == 0x31170000)
2162991554f2SKenneth D. Merry 		return;
2163991554f2SKenneth D. Merry 
21642bf620cbSScott Long 	desc_ioc_state = mpr_describe_table(mpr_iocstatus_string,
21652bf620cbSScott Long 	     ioc_status);
21662bf620cbSScott Long 	desc_scsi_status = mpr_describe_table(mpr_scsi_status_string,
21672bf620cbSScott Long 	    scsi_status);
2168991554f2SKenneth D. Merry 
2169991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
2170991554f2SKenneth D. Merry 	    le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
2171991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
2172991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "At enclosure level %d, slot %d, "
2173991554f2SKenneth D. Merry 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
2174991554f2SKenneth D. Merry 		    targ->connector_name);
2175991554f2SKenneth D. Merry 	}
21762bf620cbSScott Long 
21772bf620cbSScott Long 	/*
21782bf620cbSScott Long 	 * We can add more detail about underflow data here
2179991554f2SKenneth D. Merry 	 * TO-DO
21802bf620cbSScott Long 	 */
2181991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "\tscsi_status(%s)(0x%02x), "
21822bf620cbSScott Long 	    "scsi_state %b\n", desc_scsi_status, scsi_status,
21832bf620cbSScott Long 	    scsi_state, "\20" "\1AutosenseValid" "\2AutosenseFailed"
21842bf620cbSScott Long 	    "\3NoScsiStatus" "\4Terminated" "\5Response InfoValid");
2185991554f2SKenneth D. Merry 
2186991554f2SKenneth D. Merry 	if (sc->mpr_debug & MPR_XINFO &&
2187991554f2SKenneth D. Merry 	    scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2188991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : Start :\n");
2189991554f2SKenneth D. Merry 		scsi_sense_print(csio);
2190991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : End :\n");
2191991554f2SKenneth D. Merry 	}
2192991554f2SKenneth D. Merry 
2193991554f2SKenneth D. Merry 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2194991554f2SKenneth D. Merry 		response_info = le32toh(mpi_reply->ResponseInfo);
2195991554f2SKenneth D. Merry 		response_bytes = (u8 *)&response_info;
21962bf620cbSScott Long 		mpr_dprint(sc, MPR_XINFO, "response code(0x%01x): %s\n",
21972bf620cbSScott Long 		    response_bytes[0],
21982bf620cbSScott Long 		    mpr_describe_table(mpr_scsi_taskmgmt_string,
21992bf620cbSScott Long 		    response_bytes[0]));
2200991554f2SKenneth D. Merry 	}
2201991554f2SKenneth D. Merry }
2202991554f2SKenneth D. Merry 
220367feec50SStephen McConnell /** mprsas_nvme_trans_status_code
220467feec50SStephen McConnell  *
220567feec50SStephen McConnell  * Convert Native NVMe command error status to
220667feec50SStephen McConnell  * equivalent SCSI error status.
220767feec50SStephen McConnell  *
220867feec50SStephen McConnell  * Returns appropriate scsi_status
220967feec50SStephen McConnell  */
221067feec50SStephen McConnell static u8
22110d787e9bSWojciech Macek mprsas_nvme_trans_status_code(uint16_t nvme_status,
221267feec50SStephen McConnell     struct mpr_command *cm)
221367feec50SStephen McConnell {
221467feec50SStephen McConnell 	u8 status = MPI2_SCSI_STATUS_GOOD;
221567feec50SStephen McConnell 	int skey, asc, ascq;
221667feec50SStephen McConnell 	union ccb *ccb = cm->cm_complete_data;
221767feec50SStephen McConnell 	int returned_sense_len;
22180d787e9bSWojciech Macek 	uint8_t sct, sc;
22190d787e9bSWojciech Macek 
22200d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(nvme_status);
22210d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(nvme_status);
222267feec50SStephen McConnell 
222367feec50SStephen McConnell 	status = MPI2_SCSI_STATUS_CHECK_CONDITION;
222467feec50SStephen McConnell 	skey = SSD_KEY_ILLEGAL_REQUEST;
222567feec50SStephen McConnell 	asc = SCSI_ASC_NO_SENSE;
222667feec50SStephen McConnell 	ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
222767feec50SStephen McConnell 
22280d787e9bSWojciech Macek 	switch (sct) {
222967feec50SStephen McConnell 	case NVME_SCT_GENERIC:
22300d787e9bSWojciech Macek 		switch (sc) {
223167feec50SStephen McConnell 		case NVME_SC_SUCCESS:
223267feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_GOOD;
223367feec50SStephen McConnell 			skey = SSD_KEY_NO_SENSE;
223467feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
223567feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
223667feec50SStephen McConnell 			break;
223767feec50SStephen McConnell 		case NVME_SC_INVALID_OPCODE:
223867feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
223967feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
224067feec50SStephen McConnell 			asc = SCSI_ASC_ILLEGAL_COMMAND;
224167feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
224267feec50SStephen McConnell 			break;
224367feec50SStephen McConnell 		case NVME_SC_INVALID_FIELD:
224467feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
224567feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
224667feec50SStephen McConnell 			asc = SCSI_ASC_INVALID_CDB;
224767feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
224867feec50SStephen McConnell 			break;
224967feec50SStephen McConnell 		case NVME_SC_DATA_TRANSFER_ERROR:
225067feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
225167feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
225267feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
225367feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
225467feec50SStephen McConnell 			break;
225567feec50SStephen McConnell 		case NVME_SC_ABORTED_POWER_LOSS:
225667feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_TASK_ABORTED;
225767feec50SStephen McConnell 			skey = SSD_KEY_ABORTED_COMMAND;
225867feec50SStephen McConnell 			asc = SCSI_ASC_WARNING;
225967feec50SStephen McConnell 			ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED;
226067feec50SStephen McConnell 			break;
226167feec50SStephen McConnell 		case NVME_SC_INTERNAL_DEVICE_ERROR:
226267feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
226367feec50SStephen McConnell 			skey = SSD_KEY_HARDWARE_ERROR;
226467feec50SStephen McConnell 			asc = SCSI_ASC_INTERNAL_TARGET_FAILURE;
226567feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
226667feec50SStephen McConnell 			break;
226767feec50SStephen McConnell 		case NVME_SC_ABORTED_BY_REQUEST:
226867feec50SStephen McConnell 		case NVME_SC_ABORTED_SQ_DELETION:
226967feec50SStephen McConnell 		case NVME_SC_ABORTED_FAILED_FUSED:
227067feec50SStephen McConnell 		case NVME_SC_ABORTED_MISSING_FUSED:
227167feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_TASK_ABORTED;
227267feec50SStephen McConnell 			skey = SSD_KEY_ABORTED_COMMAND;
227367feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
227467feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
227567feec50SStephen McConnell 			break;
227667feec50SStephen McConnell 		case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
227767feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
227867feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
227967feec50SStephen McConnell 			asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
228067feec50SStephen McConnell 			ascq = SCSI_ASCQ_INVALID_LUN_ID;
228167feec50SStephen McConnell 			break;
228267feec50SStephen McConnell 		case NVME_SC_LBA_OUT_OF_RANGE:
228367feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
228467feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
228567feec50SStephen McConnell 			asc = SCSI_ASC_ILLEGAL_BLOCK;
228667feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
228767feec50SStephen McConnell 			break;
228867feec50SStephen McConnell 		case NVME_SC_CAPACITY_EXCEEDED:
228967feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
229067feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
229167feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
229267feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
229367feec50SStephen McConnell 			break;
229467feec50SStephen McConnell 		case NVME_SC_NAMESPACE_NOT_READY:
229567feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
229667feec50SStephen McConnell 			skey = SSD_KEY_NOT_READY;
229767feec50SStephen McConnell 			asc = SCSI_ASC_LUN_NOT_READY;
229867feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
229967feec50SStephen McConnell 			break;
230067feec50SStephen McConnell 		}
230167feec50SStephen McConnell 		break;
230267feec50SStephen McConnell 	case NVME_SCT_COMMAND_SPECIFIC:
23030d787e9bSWojciech Macek 		switch (sc) {
230467feec50SStephen McConnell 		case NVME_SC_INVALID_FORMAT:
230567feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
230667feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
230767feec50SStephen McConnell 			asc = SCSI_ASC_FORMAT_COMMAND_FAILED;
230867feec50SStephen McConnell 			ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED;
230967feec50SStephen McConnell 			break;
231067feec50SStephen McConnell 		case NVME_SC_CONFLICTING_ATTRIBUTES:
231167feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
231267feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
231367feec50SStephen McConnell 			asc = SCSI_ASC_INVALID_CDB;
231467feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
231567feec50SStephen McConnell 			break;
231667feec50SStephen McConnell 		}
231767feec50SStephen McConnell 		break;
231867feec50SStephen McConnell 	case NVME_SCT_MEDIA_ERROR:
23190d787e9bSWojciech Macek 		switch (sc) {
232067feec50SStephen McConnell 		case NVME_SC_WRITE_FAULTS:
232167feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
232267feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
232367feec50SStephen McConnell 			asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT;
232467feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
232567feec50SStephen McConnell 			break;
232667feec50SStephen McConnell 		case NVME_SC_UNRECOVERED_READ_ERROR:
232767feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
232867feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
232967feec50SStephen McConnell 			asc = SCSI_ASC_UNRECOVERED_READ_ERROR;
233067feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
233167feec50SStephen McConnell 			break;
233267feec50SStephen McConnell 		case NVME_SC_GUARD_CHECK_ERROR:
233367feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
233467feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
233567feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED;
233667feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED;
233767feec50SStephen McConnell 			break;
233867feec50SStephen McConnell 		case NVME_SC_APPLICATION_TAG_CHECK_ERROR:
233967feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
234067feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
234167feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED;
234267feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED;
234367feec50SStephen McConnell 			break;
234467feec50SStephen McConnell 		case NVME_SC_REFERENCE_TAG_CHECK_ERROR:
234567feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
234667feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
234767feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED;
234867feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED;
234967feec50SStephen McConnell 			break;
235067feec50SStephen McConnell 		case NVME_SC_COMPARE_FAILURE:
235167feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
235267feec50SStephen McConnell 			skey = SSD_KEY_MISCOMPARE;
235367feec50SStephen McConnell 			asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY;
235467feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
235567feec50SStephen McConnell 			break;
235667feec50SStephen McConnell 		case NVME_SC_ACCESS_DENIED:
235767feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
235867feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
235967feec50SStephen McConnell 			asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
236067feec50SStephen McConnell 			ascq = SCSI_ASCQ_INVALID_LUN_ID;
236167feec50SStephen McConnell 			break;
236267feec50SStephen McConnell 		}
236367feec50SStephen McConnell 		break;
236467feec50SStephen McConnell 	}
236567feec50SStephen McConnell 
236667feec50SStephen McConnell 	returned_sense_len = sizeof(struct scsi_sense_data);
236767feec50SStephen McConnell 	if (returned_sense_len < ccb->csio.sense_len)
236867feec50SStephen McConnell 		ccb->csio.sense_resid = ccb->csio.sense_len -
236967feec50SStephen McConnell 		    returned_sense_len;
237067feec50SStephen McConnell 	else
237167feec50SStephen McConnell 		ccb->csio.sense_resid = 0;
237267feec50SStephen McConnell 
237367feec50SStephen McConnell 	scsi_set_sense_data(&ccb->csio.sense_data, SSD_TYPE_FIXED,
237467feec50SStephen McConnell 	    1, skey, asc, ascq, SSD_ELEM_NONE);
237567feec50SStephen McConnell 	ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
237667feec50SStephen McConnell 
237767feec50SStephen McConnell 	return status;
237867feec50SStephen McConnell }
237967feec50SStephen McConnell 
238067feec50SStephen McConnell /** mprsas_complete_nvme_unmap
238167feec50SStephen McConnell  *
238267feec50SStephen McConnell  * Complete native NVMe command issued using NVMe Encapsulated
238367feec50SStephen McConnell  * Request Message.
238467feec50SStephen McConnell  */
238567feec50SStephen McConnell static u8
238667feec50SStephen McConnell mprsas_complete_nvme_unmap(struct mpr_softc *sc, struct mpr_command *cm)
238767feec50SStephen McConnell {
238867feec50SStephen McConnell 	Mpi26NVMeEncapsulatedErrorReply_t *mpi_reply;
238967feec50SStephen McConnell 	struct nvme_completion *nvme_completion = NULL;
239067feec50SStephen McConnell 	u8 scsi_status = MPI2_SCSI_STATUS_GOOD;
239167feec50SStephen McConnell 
239267feec50SStephen McConnell 	mpi_reply =(Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply;
239367feec50SStephen McConnell 	if (le16toh(mpi_reply->ErrorResponseCount)){
239467feec50SStephen McConnell 		nvme_completion = (struct nvme_completion *)cm->cm_sense;
239567feec50SStephen McConnell 		scsi_status = mprsas_nvme_trans_status_code(
239667feec50SStephen McConnell 		    nvme_completion->status, cm);
239767feec50SStephen McConnell 	}
239867feec50SStephen McConnell 	return scsi_status;
239967feec50SStephen McConnell }
240067feec50SStephen McConnell 
2401991554f2SKenneth D. Merry static void
2402991554f2SKenneth D. Merry mprsas_scsiio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2403991554f2SKenneth D. Merry {
2404991554f2SKenneth D. Merry 	MPI2_SCSI_IO_REPLY *rep;
2405991554f2SKenneth D. Merry 	union ccb *ccb;
2406991554f2SKenneth D. Merry 	struct ccb_scsiio *csio;
2407991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
2408991554f2SKenneth D. Merry 	struct scsi_vpd_supported_page_list *vpd_list = NULL;
240967feec50SStephen McConnell 	u8 *TLR_bits, TLR_on, *scsi_cdb;
2410991554f2SKenneth D. Merry 	int dir = 0, i;
2411991554f2SKenneth D. Merry 	u16 alloc_len;
2412a2c14879SStephen McConnell 	struct mprsas_target *target;
2413a2c14879SStephen McConnell 	target_id_t target_id;
2414991554f2SKenneth D. Merry 
2415991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
2416991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE,
2417991554f2SKenneth D. Merry 	    "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
2418991554f2SKenneth D. Merry 	    cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
2419991554f2SKenneth D. Merry 	    cm->cm_targ->outstanding);
2420991554f2SKenneth D. Merry 
2421991554f2SKenneth D. Merry 	callout_stop(&cm->cm_callout);
2422991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
2423991554f2SKenneth D. Merry 
2424991554f2SKenneth D. Merry 	sassc = sc->sassc;
2425991554f2SKenneth D. Merry 	ccb = cm->cm_complete_data;
2426991554f2SKenneth D. Merry 	csio = &ccb->csio;
2427a2c14879SStephen McConnell 	target_id = csio->ccb_h.target_id;
2428991554f2SKenneth D. Merry 	rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
2429991554f2SKenneth D. Merry 	/*
2430991554f2SKenneth D. Merry 	 * XXX KDM if the chain allocation fails, does it matter if we do
2431991554f2SKenneth D. Merry 	 * the sync and unload here?  It is simpler to do it in every case,
2432991554f2SKenneth D. Merry 	 * assuming it doesn't cause problems.
2433991554f2SKenneth D. Merry 	 */
2434991554f2SKenneth D. Merry 	if (cm->cm_data != NULL) {
2435991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
2436991554f2SKenneth D. Merry 			dir = BUS_DMASYNC_POSTREAD;
2437991554f2SKenneth D. Merry 		else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
2438991554f2SKenneth D. Merry 			dir = BUS_DMASYNC_POSTWRITE;
2439991554f2SKenneth D. Merry 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2440991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2441991554f2SKenneth D. Merry 	}
2442991554f2SKenneth D. Merry 
2443991554f2SKenneth D. Merry 	cm->cm_targ->completed++;
2444991554f2SKenneth D. Merry 	cm->cm_targ->outstanding--;
2445991554f2SKenneth D. Merry 	TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2446991554f2SKenneth D. Merry 	ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2447991554f2SKenneth D. Merry 
24488fe7bf06SWarner Losh 	if (cm->cm_flags & MPR_CM_FLAGS_ON_RECOVERY) {
2449991554f2SKenneth D. Merry 		TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
24508fe7bf06SWarner Losh 		KASSERT(cm->cm_state == MPR_CM_STATE_BUSY,
24518fe7bf06SWarner Losh 		    ("Not busy for CM_FLAGS_TIMEDOUT: %d\n", cm->cm_state));
24528fe7bf06SWarner Losh 		cm->cm_flags &= ~MPR_CM_FLAGS_ON_RECOVERY;
2453991554f2SKenneth D. Merry 		if (cm->cm_reply != NULL)
2454991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2455991554f2SKenneth D. Merry 			    "completed timedout cm %p ccb %p during recovery "
2456991554f2SKenneth D. Merry 			    "ioc %x scsi %x state %x xfer %u\n", cm, cm->cm_ccb,
2457991554f2SKenneth D. Merry 			    le16toh(rep->IOCStatus), rep->SCSIStatus,
2458991554f2SKenneth D. Merry 			    rep->SCSIState, le32toh(rep->TransferCount));
2459991554f2SKenneth D. Merry 		else
2460991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2461991554f2SKenneth D. Merry 			    "completed timedout cm %p ccb %p during recovery\n",
2462991554f2SKenneth D. Merry 			    cm, cm->cm_ccb);
2463991554f2SKenneth D. Merry 	} else if (cm->cm_targ->tm != NULL) {
2464991554f2SKenneth D. Merry 		if (cm->cm_reply != NULL)
2465991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2466991554f2SKenneth D. Merry 			    "completed cm %p ccb %p during recovery "
2467991554f2SKenneth D. Merry 			    "ioc %x scsi %x state %x xfer %u\n",
2468991554f2SKenneth D. Merry 			    cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2469991554f2SKenneth D. Merry 			    rep->SCSIStatus, rep->SCSIState,
2470991554f2SKenneth D. Merry 			    le32toh(rep->TransferCount));
2471991554f2SKenneth D. Merry 		else
2472991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2473991554f2SKenneth D. Merry 			    "completed cm %p ccb %p during recovery\n",
2474991554f2SKenneth D. Merry 			    cm, cm->cm_ccb);
2475991554f2SKenneth D. Merry 	} else if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
2476991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_RECOVERY,
2477991554f2SKenneth D. Merry 		    "reset completed cm %p ccb %p\n", cm, cm->cm_ccb);
2478991554f2SKenneth D. Merry 	}
2479991554f2SKenneth D. Merry 
2480991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2481991554f2SKenneth D. Merry 		/*
2482991554f2SKenneth D. Merry 		 * We ran into an error after we tried to map the command,
2483991554f2SKenneth D. Merry 		 * so we're getting a callback without queueing the command
2484991554f2SKenneth D. Merry 		 * to the hardware.  So we set the status here, and it will
2485991554f2SKenneth D. Merry 		 * be retained below.  We'll go through the "fast path",
2486991554f2SKenneth D. Merry 		 * because there can be no reply when we haven't actually
2487991554f2SKenneth D. Merry 		 * gone out to the hardware.
2488991554f2SKenneth D. Merry 		 */
2489a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2490991554f2SKenneth D. Merry 
2491991554f2SKenneth D. Merry 		/*
2492991554f2SKenneth D. Merry 		 * Currently the only error included in the mask is
2493991554f2SKenneth D. Merry 		 * MPR_CM_FLAGS_CHAIN_FAILED, which means we're out of
2494991554f2SKenneth D. Merry 		 * chain frames.  We need to freeze the queue until we get
2495991554f2SKenneth D. Merry 		 * a command that completed without this error, which will
2496991554f2SKenneth D. Merry 		 * hopefully have some chain frames attached that we can
2497991554f2SKenneth D. Merry 		 * use.  If we wanted to get smarter about it, we would
2498991554f2SKenneth D. Merry 		 * only unfreeze the queue in this condition when we're
2499991554f2SKenneth D. Merry 		 * sure that we're getting some chain frames back.  That's
2500991554f2SKenneth D. Merry 		 * probably unnecessary.
2501991554f2SKenneth D. Merry 		 */
2502991554f2SKenneth D. Merry 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
2503991554f2SKenneth D. Merry 			xpt_freeze_simq(sassc->sim, 1);
2504991554f2SKenneth D. Merry 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
25056c85e33eSScott Long 			mpr_dprint(sc, MPR_XINFO, "Error sending command, "
2506991554f2SKenneth D. Merry 			    "freezing SIM queue\n");
2507991554f2SKenneth D. Merry 		}
2508991554f2SKenneth D. Merry 	}
2509991554f2SKenneth D. Merry 
2510991554f2SKenneth D. Merry 	/*
251167feec50SStephen McConnell 	 * Point to the SCSI CDB, which is dependent on the CAM_CDB_POINTER
251267feec50SStephen McConnell 	 * flag, and use it in a few places in the rest of this function for
251367feec50SStephen McConnell 	 * convenience. Use the macro if available.
251467feec50SStephen McConnell 	 */
251567feec50SStephen McConnell 	scsi_cdb = scsiio_cdb_ptr(csio);
251667feec50SStephen McConnell 
251767feec50SStephen McConnell 	/*
2518991554f2SKenneth D. Merry 	 * If this is a Start Stop Unit command and it was issued by the driver
2519991554f2SKenneth D. Merry 	 * during shutdown, decrement the refcount to account for all of the
2520991554f2SKenneth D. Merry 	 * commands that were sent.  All SSU commands should be completed before
2521991554f2SKenneth D. Merry 	 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2522991554f2SKenneth D. Merry 	 * is TRUE.
2523991554f2SKenneth D. Merry 	 */
252467feec50SStephen McConnell 	if (sc->SSU_started && (scsi_cdb[0] == START_STOP_UNIT)) {
2525991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INFO, "Decrementing SSU count.\n");
2526991554f2SKenneth D. Merry 		sc->SSU_refcount--;
2527991554f2SKenneth D. Merry 	}
2528991554f2SKenneth D. Merry 
2529991554f2SKenneth D. Merry 	/* Take the fast path to completion */
2530991554f2SKenneth D. Merry 	if (cm->cm_reply == NULL) {
2531a2c14879SStephen McConnell 		if (mprsas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2532991554f2SKenneth D. Merry 			if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0)
2533a2c14879SStephen McConnell 				mprsas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2534991554f2SKenneth D. Merry 			else {
2535a2c14879SStephen McConnell 				mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2536a2c14879SStephen McConnell 				csio->scsi_status = SCSI_STATUS_OK;
2537991554f2SKenneth D. Merry 			}
2538991554f2SKenneth D. Merry 			if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2539991554f2SKenneth D. Merry 				ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2540991554f2SKenneth D. Merry 				sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2541991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_XINFO,
2542991554f2SKenneth D. Merry 				    "Unfreezing SIM queue\n");
2543991554f2SKenneth D. Merry 			}
2544991554f2SKenneth D. Merry 		}
2545991554f2SKenneth D. Merry 
2546991554f2SKenneth D. Merry 		/*
2547991554f2SKenneth D. Merry 		 * There are two scenarios where the status won't be
2548991554f2SKenneth D. Merry 		 * CAM_REQ_CMP.  The first is if MPR_CM_FLAGS_ERROR_MASK is
2549991554f2SKenneth D. Merry 		 * set, the second is in the MPR_FLAGS_DIAGRESET above.
2550991554f2SKenneth D. Merry 		 */
2551a2c14879SStephen McConnell 		if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2552991554f2SKenneth D. Merry 			/*
2553991554f2SKenneth D. Merry 			 * Freeze the dev queue so that commands are
2554a2c14879SStephen McConnell 			 * executed in the correct order after error
2555991554f2SKenneth D. Merry 			 * recovery.
2556991554f2SKenneth D. Merry 			 */
2557991554f2SKenneth D. Merry 			ccb->ccb_h.status |= CAM_DEV_QFRZN;
2558991554f2SKenneth D. Merry 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2559991554f2SKenneth D. Merry 		}
2560991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2561991554f2SKenneth D. Merry 		xpt_done(ccb);
2562991554f2SKenneth D. Merry 		return;
2563991554f2SKenneth D. Merry 	}
2564991554f2SKenneth D. Merry 
256567feec50SStephen McConnell 	target = &sassc->targets[target_id];
256667feec50SStephen McConnell 	if (scsi_cdb[0] == UNMAP &&
256767feec50SStephen McConnell 	    target->is_nvme &&
256867feec50SStephen McConnell 	    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
256967feec50SStephen McConnell 		rep->SCSIStatus = mprsas_complete_nvme_unmap(sc, cm);
257067feec50SStephen McConnell 		csio->scsi_status = rep->SCSIStatus;
257167feec50SStephen McConnell 	}
257267feec50SStephen McConnell 
2573991554f2SKenneth D. Merry 	mprsas_log_command(cm, MPR_XINFO,
2574991554f2SKenneth D. Merry 	    "ioc %x scsi %x state %x xfer %u\n",
2575991554f2SKenneth D. Merry 	    le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2576991554f2SKenneth D. Merry 	    le32toh(rep->TransferCount));
2577991554f2SKenneth D. Merry 
2578991554f2SKenneth D. Merry 	switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2579991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2580991554f2SKenneth D. Merry 		csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2581991554f2SKenneth D. Merry 		/* FALLTHROUGH */
2582991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SUCCESS:
2583991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2584991554f2SKenneth D. Merry 		if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2585991554f2SKenneth D. Merry 		    MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2586991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_XINFO, "recovered error\n");
2587991554f2SKenneth D. Merry 
2588991554f2SKenneth D. Merry 		/* Completion failed at the transport level. */
2589991554f2SKenneth D. Merry 		if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2590991554f2SKenneth D. Merry 		    MPI2_SCSI_STATE_TERMINATED)) {
2591a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2592991554f2SKenneth D. Merry 			break;
2593991554f2SKenneth D. Merry 		}
2594991554f2SKenneth D. Merry 
2595991554f2SKenneth D. Merry 		/* In a modern packetized environment, an autosense failure
2596991554f2SKenneth D. Merry 		 * implies that there's not much else that can be done to
2597991554f2SKenneth D. Merry 		 * recover the command.
2598991554f2SKenneth D. Merry 		 */
2599991554f2SKenneth D. Merry 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2600a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2601991554f2SKenneth D. Merry 			break;
2602991554f2SKenneth D. Merry 		}
2603991554f2SKenneth D. Merry 
2604991554f2SKenneth D. Merry 		/*
2605991554f2SKenneth D. Merry 		 * CAM doesn't care about SAS Response Info data, but if this is
2606991554f2SKenneth D. Merry 		 * the state check if TLR should be done.  If not, clear the
2607991554f2SKenneth D. Merry 		 * TLR_bits for the target.
2608991554f2SKenneth D. Merry 		 */
2609991554f2SKenneth D. Merry 		if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2610991554f2SKenneth D. Merry 		    ((le32toh(rep->ResponseInfo) & MPI2_SCSI_RI_MASK_REASONCODE)
2611991554f2SKenneth D. Merry 		    == MPR_SCSI_RI_INVALID_FRAME)) {
2612a2c14879SStephen McConnell 			sc->mapping_table[target_id].TLR_bits =
2613991554f2SKenneth D. Merry 			    (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2614991554f2SKenneth D. Merry 		}
2615991554f2SKenneth D. Merry 
2616991554f2SKenneth D. Merry 		/*
2617991554f2SKenneth D. Merry 		 * Intentionally override the normal SCSI status reporting
2618991554f2SKenneth D. Merry 		 * for these two cases.  These are likely to happen in a
2619991554f2SKenneth D. Merry 		 * multi-initiator environment, and we want to make sure that
2620991554f2SKenneth D. Merry 		 * CAM retries these commands rather than fail them.
2621991554f2SKenneth D. Merry 		 */
2622991554f2SKenneth D. Merry 		if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2623991554f2SKenneth D. Merry 		    (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2624a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2625991554f2SKenneth D. Merry 			break;
2626991554f2SKenneth D. Merry 		}
2627991554f2SKenneth D. Merry 
2628991554f2SKenneth D. Merry 		/* Handle normal status and sense */
2629991554f2SKenneth D. Merry 		csio->scsi_status = rep->SCSIStatus;
2630991554f2SKenneth D. Merry 		if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2631a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2632991554f2SKenneth D. Merry 		else
2633a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2634991554f2SKenneth D. Merry 
2635991554f2SKenneth D. Merry 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2636991554f2SKenneth D. Merry 			int sense_len, returned_sense_len;
2637991554f2SKenneth D. Merry 
2638991554f2SKenneth D. Merry 			returned_sense_len = min(le32toh(rep->SenseCount),
2639991554f2SKenneth D. Merry 			    sizeof(struct scsi_sense_data));
2640991554f2SKenneth D. Merry 			if (returned_sense_len < csio->sense_len)
2641991554f2SKenneth D. Merry 				csio->sense_resid = csio->sense_len -
2642991554f2SKenneth D. Merry 				    returned_sense_len;
2643991554f2SKenneth D. Merry 			else
2644991554f2SKenneth D. Merry 				csio->sense_resid = 0;
2645991554f2SKenneth D. Merry 
2646991554f2SKenneth D. Merry 			sense_len = min(returned_sense_len,
2647991554f2SKenneth D. Merry 			    csio->sense_len - csio->sense_resid);
2648991554f2SKenneth D. Merry 			bzero(&csio->sense_data, sizeof(csio->sense_data));
2649991554f2SKenneth D. Merry 			bcopy(cm->cm_sense, &csio->sense_data, sense_len);
2650991554f2SKenneth D. Merry 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2651991554f2SKenneth D. Merry 		}
2652991554f2SKenneth D. Merry 
2653991554f2SKenneth D. Merry 		/*
2654991554f2SKenneth D. Merry 		 * Check if this is an INQUIRY command.  If it's a VPD inquiry,
2655991554f2SKenneth D. Merry 		 * and it's page code 0 (Supported Page List), and there is
2656991554f2SKenneth D. Merry 		 * inquiry data, and this is for a sequential access device, and
2657991554f2SKenneth D. Merry 		 * the device is an SSP target, and TLR is supported by the
2658991554f2SKenneth D. Merry 		 * controller, turn the TLR_bits value ON if page 0x90 is
2659991554f2SKenneth D. Merry 		 * supported.
2660991554f2SKenneth D. Merry 		 */
266167feec50SStephen McConnell 		if ((scsi_cdb[0] == INQUIRY) &&
266267feec50SStephen McConnell 		    (scsi_cdb[1] & SI_EVPD) &&
266367feec50SStephen McConnell 		    (scsi_cdb[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2664991554f2SKenneth D. Merry 		    ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2665d2b4e18bSKenneth D. Merry 		    (csio->data_ptr != NULL) &&
2666d2b4e18bSKenneth D. Merry 		    ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2667d2b4e18bSKenneth D. Merry 		    (sc->control_TLR) &&
2668a2c14879SStephen McConnell 		    (sc->mapping_table[target_id].device_info &
2669991554f2SKenneth D. Merry 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2670991554f2SKenneth D. Merry 			vpd_list = (struct scsi_vpd_supported_page_list *)
2671991554f2SKenneth D. Merry 			    csio->data_ptr;
2672a2c14879SStephen McConnell 			TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2673991554f2SKenneth D. Merry 			*TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2674991554f2SKenneth D. Merry 			TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
267567feec50SStephen McConnell 			alloc_len = ((u16)scsi_cdb[3] << 8) + scsi_cdb[4];
2676d2b4e18bSKenneth D. Merry 			alloc_len -= csio->resid;
2677991554f2SKenneth D. Merry 			for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2678991554f2SKenneth D. Merry 				if (vpd_list->list[i] == 0x90) {
2679991554f2SKenneth D. Merry 					*TLR_bits = TLR_on;
2680991554f2SKenneth D. Merry 					break;
2681991554f2SKenneth D. Merry 				}
2682991554f2SKenneth D. Merry 			}
2683991554f2SKenneth D. Merry 		}
2684a2c14879SStephen McConnell 
2685a2c14879SStephen McConnell 		/*
2686a2c14879SStephen McConnell 		 * If this is a SATA direct-access end device, mark it so that
2687a2c14879SStephen McConnell 		 * a SCSI StartStopUnit command will be sent to it when the
2688a2c14879SStephen McConnell 		 * driver is being shutdown.
2689a2c14879SStephen McConnell 		 */
269067feec50SStephen McConnell 		if ((scsi_cdb[0] == INQUIRY) &&
2691fa699bb2SAlan Somers 		    (csio->data_ptr != NULL) &&
2692a2c14879SStephen McConnell 		    ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2693a2c14879SStephen McConnell 		    (sc->mapping_table[target_id].device_info &
2694a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2695a2c14879SStephen McConnell 		    ((sc->mapping_table[target_id].device_info &
2696a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2697a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2698a2c14879SStephen McConnell 			target = &sassc->targets[target_id];
2699a2c14879SStephen McConnell 			target->supports_SSU = TRUE;
2700a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_XINFO, "Target %d supports SSU\n",
2701a2c14879SStephen McConnell 			    target_id);
2702a2c14879SStephen McConnell 		}
2703991554f2SKenneth D. Merry 		break;
2704991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2705991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2706991554f2SKenneth D. Merry 		/*
2707991554f2SKenneth D. Merry 		 * If devinfo is 0 this will be a volume.  In that case don't
2708991554f2SKenneth D. Merry 		 * tell CAM that the volume is not there.  We want volumes to
2709991554f2SKenneth D. Merry 		 * be enumerated until they are deleted/removed, not just
2710991554f2SKenneth D. Merry 		 * failed.
2711991554f2SKenneth D. Merry 		 */
2712991554f2SKenneth D. Merry 		if (cm->cm_targ->devinfo == 0)
2713a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2714991554f2SKenneth D. Merry 		else
2715a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2716991554f2SKenneth D. Merry 		break;
2717991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_SGL:
2718991554f2SKenneth D. Merry 		mpr_print_scsiio_cmd(sc, cm);
2719a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2720991554f2SKenneth D. Merry 		break;
2721991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2722991554f2SKenneth D. Merry 		/*
2723991554f2SKenneth D. Merry 		 * This is one of the responses that comes back when an I/O
2724991554f2SKenneth D. Merry 		 * has been aborted.  If it is because of a timeout that we
2725991554f2SKenneth D. Merry 		 * initiated, just set the status to CAM_CMD_TIMEOUT.
2726991554f2SKenneth D. Merry 		 * Otherwise set it to CAM_REQ_ABORTED.  The effect on the
2727991554f2SKenneth D. Merry 		 * command is the same (it gets retried, subject to the
2728991554f2SKenneth D. Merry 		 * retry counter), the only difference is what gets printed
2729991554f2SKenneth D. Merry 		 * on the console.
2730991554f2SKenneth D. Merry 		 */
27318fe7bf06SWarner Losh 		if (cm->cm_flags & MPR_CM_FLAGS_TIMEDOUT)
2732a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2733991554f2SKenneth D. Merry 		else
2734a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2735991554f2SKenneth D. Merry 		break;
2736991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2737991554f2SKenneth D. Merry 		/* resid is ignored for this condition */
2738991554f2SKenneth D. Merry 		csio->resid = 0;
2739a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2740991554f2SKenneth D. Merry 		break;
2741991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2742991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2743991554f2SKenneth D. Merry 		/*
27446adfa7edSAlan Somers 		 * These can sometimes be transient transport-related
27456adfa7edSAlan Somers 		 * errors, and sometimes persistent drive-related errors.
27466adfa7edSAlan Somers 		 * We used to retry these without decrementing the retry
27476adfa7edSAlan Somers 		 * count by returning CAM_REQUEUE_REQ.  Unfortunately, if
27486adfa7edSAlan Somers 		 * we hit a persistent drive problem that returns one of
27496adfa7edSAlan Somers 		 * these error codes, we would retry indefinitely.  So,
27506adfa7edSAlan Somers 		 * return CAM_REQ_CMP_ERROR so that we decrement the retry
27516adfa7edSAlan Somers 		 * count and avoid infinite retries.  We're taking the
27526adfa7edSAlan Somers 		 * potential risk of flagging false failures in the event
27536adfa7edSAlan Somers 		 * of a topology-related error (e.g. a SAS expander problem
27546adfa7edSAlan Somers 		 * causes a command addressed to a drive to fail), but
27554c1cdd4aSWarner Losh 		 * avoiding getting into an infinite retry loop. However,
27564c1cdd4aSWarner Losh 		 * if we get them while were moving a device, we should
27574c1cdd4aSWarner Losh 		 * fail the request as 'not there' because the device
27584c1cdd4aSWarner Losh 		 * is effectively gone.
2759991554f2SKenneth D. Merry 		 */
27604c1cdd4aSWarner Losh 		if (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL)
27614c1cdd4aSWarner Losh 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
27624c1cdd4aSWarner Losh 		else
27636adfa7edSAlan Somers 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
27646eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO,
27654c1cdd4aSWarner Losh 		    "Controller reported %s tgt %u SMID %u loginfo %x%s\n",
27662bf620cbSScott Long 		    mpr_describe_table(mpr_iocstatus_string,
27672bf620cbSScott Long 		    le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK),
27682bf620cbSScott Long 		    target_id, cm->cm_desc.Default.SMID,
27694c1cdd4aSWarner Losh 		    le32toh(rep->IOCLogInfo),
27704c1cdd4aSWarner Losh 		    (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL) ? " departing" : "");
27716eea4f46SScott Long 		mpr_dprint(sc, MPR_XINFO,
27726eea4f46SScott Long 		    "SCSIStatus %x SCSIState %x xfercount %u\n",
2773694cb8b8SScott Long 		    rep->SCSIStatus, rep->SCSIState,
2774991554f2SKenneth D. Merry 		    le32toh(rep->TransferCount));
2775991554f2SKenneth D. Merry 		break;
2776991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
2777991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
2778991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_VPID:
2779991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_FIELD:
2780991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_STATE:
2781991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2782991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2783991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2784991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2785991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2786991554f2SKenneth D. Merry 	default:
2787991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_XINFO,
2788694cb8b8SScott Long 		    "completed ioc %x loginfo %x scsi %x state %x xfer %u\n",
2789694cb8b8SScott Long 		    le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2790694cb8b8SScott Long 		    rep->SCSIStatus, rep->SCSIState,
2791991554f2SKenneth D. Merry 		    le32toh(rep->TransferCount));
2792991554f2SKenneth D. Merry 		csio->resid = cm->cm_length;
279367feec50SStephen McConnell 
279467feec50SStephen McConnell 		if (scsi_cdb[0] == UNMAP &&
279567feec50SStephen McConnell 		    target->is_nvme &&
279667feec50SStephen McConnell 		    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR)
279767feec50SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
279867feec50SStephen McConnell 		else
2799a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
280067feec50SStephen McConnell 
2801991554f2SKenneth D. Merry 		break;
2802991554f2SKenneth D. Merry 	}
2803991554f2SKenneth D. Merry 
2804991554f2SKenneth D. Merry 	mpr_sc_failed_io_info(sc, csio, rep, cm->cm_targ);
2805991554f2SKenneth D. Merry 
2806991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2807991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2808991554f2SKenneth D. Merry 		sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2809991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "Command completed, unfreezing SIM "
2810991554f2SKenneth D. Merry 		    "queue\n");
2811991554f2SKenneth D. Merry 	}
2812991554f2SKenneth D. Merry 
2813a2c14879SStephen McConnell 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2814991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
2815991554f2SKenneth D. Merry 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2816991554f2SKenneth D. Merry 	}
2817991554f2SKenneth D. Merry 
28184c1cdd4aSWarner Losh 	/*
28194c1cdd4aSWarner Losh 	 * Check to see if we're removing the device. If so, and this is the
28204c1cdd4aSWarner Losh 	 * last command on the queue, proceed with the deferred removal of the
28214c1cdd4aSWarner Losh 	 * device.  Note, for removing a volume, this won't trigger because
28224c1cdd4aSWarner Losh 	 * pending_remove_tm will be NULL.
28234c1cdd4aSWarner Losh 	 */
28244c1cdd4aSWarner Losh 	if (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL) {
28254c1cdd4aSWarner Losh 		if (TAILQ_FIRST(&cm->cm_targ->commands) == NULL &&
28264c1cdd4aSWarner Losh 		    cm->cm_targ->pending_remove_tm != NULL) {
28274c1cdd4aSWarner Losh 			mpr_dprint(sc, MPR_INFO, "Last pending command complete: starting remove_device\n");
28284c1cdd4aSWarner Losh 			mpr_map_command(sc, cm->cm_targ->pending_remove_tm);
28294c1cdd4aSWarner Losh 			cm->cm_targ->pending_remove_tm = NULL;
28304c1cdd4aSWarner Losh 		}
28314c1cdd4aSWarner Losh 	}
28324c1cdd4aSWarner Losh 
2833991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2834991554f2SKenneth D. Merry 	xpt_done(ccb);
2835991554f2SKenneth D. Merry }
2836991554f2SKenneth D. Merry 
2837991554f2SKenneth D. Merry static void
2838991554f2SKenneth D. Merry mprsas_smpio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2839991554f2SKenneth D. Merry {
2840991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2841991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2842991554f2SKenneth D. Merry 	uint64_t sasaddr;
2843991554f2SKenneth D. Merry 	union ccb *ccb;
2844991554f2SKenneth D. Merry 
2845991554f2SKenneth D. Merry 	ccb = cm->cm_complete_data;
2846991554f2SKenneth D. Merry 
2847991554f2SKenneth D. Merry 	/*
2848991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
2849991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and SMP
2850991554f2SKenneth D. Merry 	 * commands require two S/G elements only.  That should be handled
2851991554f2SKenneth D. Merry 	 * in the standard request size.
2852991554f2SKenneth D. Merry 	 */
2853991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2854a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x on SMP "
2855a2c14879SStephen McConnell 		    "request!\n", __func__, cm->cm_flags);
2856a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2857991554f2SKenneth D. Merry 		goto bailout;
2858991554f2SKenneth D. Merry         }
2859991554f2SKenneth D. Merry 
2860991554f2SKenneth D. Merry 	rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2861991554f2SKenneth D. Merry 	if (rpl == NULL) {
2862991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: NULL cm_reply!\n", __func__);
2863a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2864991554f2SKenneth D. Merry 		goto bailout;
2865991554f2SKenneth D. Merry 	}
2866991554f2SKenneth D. Merry 
2867991554f2SKenneth D. Merry 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2868991554f2SKenneth D. Merry 	sasaddr = le32toh(req->SASAddress.Low);
2869991554f2SKenneth D. Merry 	sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2870991554f2SKenneth D. Merry 
2871991554f2SKenneth D. Merry 	if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2872991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS ||
2873991554f2SKenneth D. Merry 	    rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2874991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2875991554f2SKenneth D. Merry 		    __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2876a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2877991554f2SKenneth D. Merry 		goto bailout;
2878991554f2SKenneth D. Merry 	}
2879991554f2SKenneth D. Merry 
2880a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s: SMP request to SAS address %#jx "
2881a2c14879SStephen McConnell 	    "completed successfully\n", __func__, (uintmax_t)sasaddr);
2882991554f2SKenneth D. Merry 
2883991554f2SKenneth D. Merry 	if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2884a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2885991554f2SKenneth D. Merry 	else
2886a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2887991554f2SKenneth D. Merry 
2888991554f2SKenneth D. Merry bailout:
2889991554f2SKenneth D. Merry 	/*
2890991554f2SKenneth D. Merry 	 * We sync in both directions because we had DMAs in the S/G list
2891991554f2SKenneth D. Merry 	 * in both directions.
2892991554f2SKenneth D. Merry 	 */
2893991554f2SKenneth D. Merry 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2894991554f2SKenneth D. Merry 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2895991554f2SKenneth D. Merry 	bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2896991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2897991554f2SKenneth D. Merry 	xpt_done(ccb);
2898991554f2SKenneth D. Merry }
2899991554f2SKenneth D. Merry 
2900991554f2SKenneth D. Merry static void
29017a2a6a1aSStephen McConnell mprsas_send_smpcmd(struct mprsas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2902991554f2SKenneth D. Merry {
2903991554f2SKenneth D. Merry 	struct mpr_command *cm;
2904991554f2SKenneth D. Merry 	uint8_t *request, *response;
2905991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2906991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2907991554f2SKenneth D. Merry 	struct sglist *sg;
2908991554f2SKenneth D. Merry 	int error;
2909991554f2SKenneth D. Merry 
2910991554f2SKenneth D. Merry 	sc = sassc->sc;
2911991554f2SKenneth D. Merry 	sg = NULL;
2912991554f2SKenneth D. Merry 	error = 0;
2913991554f2SKenneth D. Merry 
2914991554f2SKenneth D. Merry 	switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
2915991554f2SKenneth D. Merry 	case CAM_DATA_PADDR:
2916991554f2SKenneth D. Merry 	case CAM_DATA_SG_PADDR:
2917991554f2SKenneth D. Merry 		/*
2918991554f2SKenneth D. Merry 		 * XXX We don't yet support physical addresses here.
2919991554f2SKenneth D. Merry 		 */
2920991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: physical addresses not "
2921991554f2SKenneth D. Merry 		    "supported\n", __func__);
2922a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2923991554f2SKenneth D. Merry 		xpt_done(ccb);
2924991554f2SKenneth D. Merry 		return;
2925991554f2SKenneth D. Merry 	case CAM_DATA_SG:
2926991554f2SKenneth D. Merry 		/*
2927991554f2SKenneth D. Merry 		 * The chip does not support more than one buffer for the
2928991554f2SKenneth D. Merry 		 * request or response.
2929991554f2SKenneth D. Merry 		 */
2930991554f2SKenneth D. Merry 		if ((ccb->smpio.smp_request_sglist_cnt > 1)
2931991554f2SKenneth D. Merry 		    || (ccb->smpio.smp_response_sglist_cnt > 1)) {
29327a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "%s: multiple request or "
29337a2a6a1aSStephen McConnell 			    "response buffer segments not supported for SMP\n",
29347a2a6a1aSStephen McConnell 			    __func__);
2935a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2936991554f2SKenneth D. Merry 			xpt_done(ccb);
2937991554f2SKenneth D. Merry 			return;
2938991554f2SKenneth D. Merry 		}
2939991554f2SKenneth D. Merry 
2940991554f2SKenneth D. Merry 		/*
2941991554f2SKenneth D. Merry 		 * The CAM_SCATTER_VALID flag was originally implemented
2942991554f2SKenneth D. Merry 		 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2943991554f2SKenneth D. Merry 		 * We have two.  So, just take that flag to mean that we
2944991554f2SKenneth D. Merry 		 * might have S/G lists, and look at the S/G segment count
2945991554f2SKenneth D. Merry 		 * to figure out whether that is the case for each individual
2946991554f2SKenneth D. Merry 		 * buffer.
2947991554f2SKenneth D. Merry 		 */
2948991554f2SKenneth D. Merry 		if (ccb->smpio.smp_request_sglist_cnt != 0) {
2949991554f2SKenneth D. Merry 			bus_dma_segment_t *req_sg;
2950991554f2SKenneth D. Merry 
2951991554f2SKenneth D. Merry 			req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2952991554f2SKenneth D. Merry 			request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2953991554f2SKenneth D. Merry 		} else
2954991554f2SKenneth D. Merry 			request = ccb->smpio.smp_request;
2955991554f2SKenneth D. Merry 
2956991554f2SKenneth D. Merry 		if (ccb->smpio.smp_response_sglist_cnt != 0) {
2957991554f2SKenneth D. Merry 			bus_dma_segment_t *rsp_sg;
2958991554f2SKenneth D. Merry 
2959991554f2SKenneth D. Merry 			rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2960991554f2SKenneth D. Merry 			response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2961991554f2SKenneth D. Merry 		} else
2962991554f2SKenneth D. Merry 			response = ccb->smpio.smp_response;
2963991554f2SKenneth D. Merry 		break;
2964991554f2SKenneth D. Merry 	case CAM_DATA_VADDR:
2965991554f2SKenneth D. Merry 		request = ccb->smpio.smp_request;
2966991554f2SKenneth D. Merry 		response = ccb->smpio.smp_response;
2967991554f2SKenneth D. Merry 		break;
2968991554f2SKenneth D. Merry 	default:
2969a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2970991554f2SKenneth D. Merry 		xpt_done(ccb);
2971991554f2SKenneth D. Merry 		return;
2972991554f2SKenneth D. Merry 	}
2973991554f2SKenneth D. Merry 
2974991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
2975991554f2SKenneth D. Merry 	if (cm == NULL) {
29767a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cannot allocate command\n",
29777a2a6a1aSStephen McConnell 		    __func__);
2978a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2979991554f2SKenneth D. Merry 		xpt_done(ccb);
2980991554f2SKenneth D. Merry 		return;
2981991554f2SKenneth D. Merry 	}
2982991554f2SKenneth D. Merry 
2983991554f2SKenneth D. Merry 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2984991554f2SKenneth D. Merry 	bzero(req, sizeof(*req));
2985991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2986991554f2SKenneth D. Merry 
2987991554f2SKenneth D. Merry 	/* Allow the chip to use any route to this SAS address. */
2988991554f2SKenneth D. Merry 	req->PhysicalPort = 0xff;
2989991554f2SKenneth D. Merry 
2990991554f2SKenneth D. Merry 	req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2991991554f2SKenneth D. Merry 	req->SGLFlags =
2992991554f2SKenneth D. Merry 	    MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2993991554f2SKenneth D. Merry 
2994991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s: sending SMP request to SAS address "
2995991554f2SKenneth D. Merry 	    "%#jx\n", __func__, (uintmax_t)sasaddr);
2996991554f2SKenneth D. Merry 
2997991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
2998991554f2SKenneth D. Merry 
2999991554f2SKenneth D. Merry 	/*
3000991554f2SKenneth D. Merry 	 * Set up a uio to pass into mpr_map_command().  This allows us to
3001991554f2SKenneth D. Merry 	 * do one map command, and one busdma call in there.
3002991554f2SKenneth D. Merry 	 */
3003991554f2SKenneth D. Merry 	cm->cm_uio.uio_iov = cm->cm_iovec;
3004991554f2SKenneth D. Merry 	cm->cm_uio.uio_iovcnt = 2;
3005991554f2SKenneth D. Merry 	cm->cm_uio.uio_segflg = UIO_SYSSPACE;
3006991554f2SKenneth D. Merry 
3007991554f2SKenneth D. Merry 	/*
3008991554f2SKenneth D. Merry 	 * The read/write flag isn't used by busdma, but set it just in
3009991554f2SKenneth D. Merry 	 * case.  This isn't exactly accurate, either, since we're going in
3010991554f2SKenneth D. Merry 	 * both directions.
3011991554f2SKenneth D. Merry 	 */
3012991554f2SKenneth D. Merry 	cm->cm_uio.uio_rw = UIO_WRITE;
3013991554f2SKenneth D. Merry 
3014991554f2SKenneth D. Merry 	cm->cm_iovec[0].iov_base = request;
3015991554f2SKenneth D. Merry 	cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
3016991554f2SKenneth D. Merry 	cm->cm_iovec[1].iov_base = response;
3017991554f2SKenneth D. Merry 	cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
3018991554f2SKenneth D. Merry 
3019991554f2SKenneth D. Merry 	cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
3020991554f2SKenneth D. Merry 			       cm->cm_iovec[1].iov_len;
3021991554f2SKenneth D. Merry 
3022991554f2SKenneth D. Merry 	/*
3023991554f2SKenneth D. Merry 	 * Trigger a warning message in mpr_data_cb() for the user if we
3024991554f2SKenneth D. Merry 	 * wind up exceeding two S/G segments.  The chip expects one
3025991554f2SKenneth D. Merry 	 * segment for the request and another for the response.
3026991554f2SKenneth D. Merry 	 */
3027991554f2SKenneth D. Merry 	cm->cm_max_segs = 2;
3028991554f2SKenneth D. Merry 
3029991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3030991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_smpio_complete;
3031991554f2SKenneth D. Merry 	cm->cm_complete_data = ccb;
3032991554f2SKenneth D. Merry 
3033991554f2SKenneth D. Merry 	/*
3034991554f2SKenneth D. Merry 	 * Tell the mapping code that we're using a uio, and that this is
3035991554f2SKenneth D. Merry 	 * an SMP passthrough request.  There is a little special-case
3036991554f2SKenneth D. Merry 	 * logic there (in mpr_data_cb()) to handle the bidirectional
3037991554f2SKenneth D. Merry 	 * transfer.
3038991554f2SKenneth D. Merry 	 */
3039991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_USE_UIO | MPR_CM_FLAGS_SMP_PASS |
3040991554f2SKenneth D. Merry 			MPR_CM_FLAGS_DATAIN | MPR_CM_FLAGS_DATAOUT;
3041991554f2SKenneth D. Merry 
3042991554f2SKenneth D. Merry 	/* The chip data format is little endian. */
3043991554f2SKenneth D. Merry 	req->SASAddress.High = htole32(sasaddr >> 32);
3044991554f2SKenneth D. Merry 	req->SASAddress.Low = htole32(sasaddr);
3045991554f2SKenneth D. Merry 
3046991554f2SKenneth D. Merry 	/*
3047991554f2SKenneth D. Merry 	 * XXX Note that we don't have a timeout/abort mechanism here.
3048991554f2SKenneth D. Merry 	 * From the manual, it looks like task management requests only
3049991554f2SKenneth D. Merry 	 * work for SCSI IO and SATA passthrough requests.  We may need to
3050991554f2SKenneth D. Merry 	 * have a mechanism to retry requests in the event of a chip reset
3051991554f2SKenneth D. Merry 	 * at least.  Hopefully the chip will insure that any errors short
3052991554f2SKenneth D. Merry 	 * of that are relayed back to the driver.
3053991554f2SKenneth D. Merry 	 */
3054991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
3055991554f2SKenneth D. Merry 	if ((error != 0) && (error != EINPROGRESS)) {
3056991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: error %d returned from "
3057991554f2SKenneth D. Merry 		    "mpr_map_command()\n", __func__, error);
3058991554f2SKenneth D. Merry 		goto bailout_error;
3059991554f2SKenneth D. Merry 	}
3060991554f2SKenneth D. Merry 
3061991554f2SKenneth D. Merry 	return;
3062991554f2SKenneth D. Merry 
3063991554f2SKenneth D. Merry bailout_error:
3064991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
3065a2c14879SStephen McConnell 	mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3066991554f2SKenneth D. Merry 	xpt_done(ccb);
3067991554f2SKenneth D. Merry 	return;
3068991554f2SKenneth D. Merry }
3069991554f2SKenneth D. Merry 
3070991554f2SKenneth D. Merry static void
3071991554f2SKenneth D. Merry mprsas_action_smpio(struct mprsas_softc *sassc, union ccb *ccb)
3072991554f2SKenneth D. Merry {
3073991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3074991554f2SKenneth D. Merry 	struct mprsas_target *targ;
3075991554f2SKenneth D. Merry 	uint64_t sasaddr = 0;
3076991554f2SKenneth D. Merry 
3077991554f2SKenneth D. Merry 	sc = sassc->sc;
3078991554f2SKenneth D. Merry 
3079991554f2SKenneth D. Merry 	/*
3080991554f2SKenneth D. Merry 	 * Make sure the target exists.
3081991554f2SKenneth D. Merry 	 */
3082991554f2SKenneth D. Merry 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
3083991554f2SKenneth D. Merry 	    ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
3084991554f2SKenneth D. Merry 	targ = &sassc->targets[ccb->ccb_h.target_id];
3085991554f2SKenneth D. Merry 	if (targ->handle == 0x0) {
3086991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: target %d does not exist!\n",
3087991554f2SKenneth D. Merry 		    __func__, ccb->ccb_h.target_id);
3088a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
3089991554f2SKenneth D. Merry 		xpt_done(ccb);
3090991554f2SKenneth D. Merry 		return;
3091991554f2SKenneth D. Merry 	}
3092991554f2SKenneth D. Merry 
3093991554f2SKenneth D. Merry 	/*
3094991554f2SKenneth D. Merry 	 * If this device has an embedded SMP target, we'll talk to it
3095991554f2SKenneth D. Merry 	 * directly.
3096991554f2SKenneth D. Merry 	 * figure out what the expander's address is.
3097991554f2SKenneth D. Merry 	 */
3098991554f2SKenneth D. Merry 	if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
3099991554f2SKenneth D. Merry 		sasaddr = targ->sasaddr;
3100991554f2SKenneth D. Merry 
3101991554f2SKenneth D. Merry 	/*
3102991554f2SKenneth D. Merry 	 * If we don't have a SAS address for the expander yet, try
3103991554f2SKenneth D. Merry 	 * grabbing it from the page 0x83 information cached in the
3104991554f2SKenneth D. Merry 	 * transport layer for this target.  LSI expanders report the
3105991554f2SKenneth D. Merry 	 * expander SAS address as the port-associated SAS address in
3106991554f2SKenneth D. Merry 	 * Inquiry VPD page 0x83.  Maxim expanders don't report it in page
3107991554f2SKenneth D. Merry 	 * 0x83.
3108991554f2SKenneth D. Merry 	 *
3109991554f2SKenneth D. Merry 	 * XXX KDM disable this for now, but leave it commented out so that
3110991554f2SKenneth D. Merry 	 * it is obvious that this is another possible way to get the SAS
3111991554f2SKenneth D. Merry 	 * address.
3112991554f2SKenneth D. Merry 	 *
3113991554f2SKenneth D. Merry 	 * The parent handle method below is a little more reliable, and
3114991554f2SKenneth D. Merry 	 * the other benefit is that it works for devices other than SES
3115991554f2SKenneth D. Merry 	 * devices.  So you can send a SMP request to a da(4) device and it
3116991554f2SKenneth D. Merry 	 * will get routed to the expander that device is attached to.
3117991554f2SKenneth D. Merry 	 * (Assuming the da(4) device doesn't contain an SMP target...)
3118991554f2SKenneth D. Merry 	 */
3119991554f2SKenneth D. Merry #if 0
3120991554f2SKenneth D. Merry 	if (sasaddr == 0)
3121991554f2SKenneth D. Merry 		sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
3122991554f2SKenneth D. Merry #endif
3123991554f2SKenneth D. Merry 
3124991554f2SKenneth D. Merry 	/*
3125991554f2SKenneth D. Merry 	 * If we still don't have a SAS address for the expander, look for
3126991554f2SKenneth D. Merry 	 * the parent device of this device, which is probably the expander.
3127991554f2SKenneth D. Merry 	 */
3128991554f2SKenneth D. Merry 	if (sasaddr == 0) {
3129991554f2SKenneth D. Merry #ifdef OLD_MPR_PROBE
3130991554f2SKenneth D. Merry 		struct mprsas_target *parent_target;
3131991554f2SKenneth D. Merry #endif
3132991554f2SKenneth D. Merry 
3133991554f2SKenneth D. Merry 		if (targ->parent_handle == 0x0) {
3134991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
3135991554f2SKenneth D. Merry 			    "a valid parent handle!\n", __func__, targ->handle);
3136a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3137991554f2SKenneth D. Merry 			goto bailout;
3138991554f2SKenneth D. Merry 		}
3139991554f2SKenneth D. Merry #ifdef OLD_MPR_PROBE
3140991554f2SKenneth D. Merry 		parent_target = mprsas_find_target_by_handle(sassc, 0,
3141991554f2SKenneth D. Merry 		    targ->parent_handle);
3142991554f2SKenneth D. Merry 
3143991554f2SKenneth D. Merry 		if (parent_target == NULL) {
3144991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
3145991554f2SKenneth D. Merry 			    "a valid parent target!\n", __func__, targ->handle);
3146a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3147991554f2SKenneth D. Merry 			goto bailout;
3148991554f2SKenneth D. Merry 		}
3149991554f2SKenneth D. Merry 
3150991554f2SKenneth D. Merry 		if ((parent_target->devinfo &
3151991554f2SKenneth D. Merry 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3152991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent %d "
3153991554f2SKenneth D. Merry 			    "does not have an SMP target!\n", __func__,
3154991554f2SKenneth D. Merry 			    targ->handle, parent_target->handle);
3155a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3156991554f2SKenneth D. Merry 			goto bailout;
3157991554f2SKenneth D. Merry 		}
3158991554f2SKenneth D. Merry 
3159991554f2SKenneth D. Merry 		sasaddr = parent_target->sasaddr;
3160991554f2SKenneth D. Merry #else /* OLD_MPR_PROBE */
3161991554f2SKenneth D. Merry 		if ((targ->parent_devinfo &
3162991554f2SKenneth D. Merry 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3163991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent %d "
3164991554f2SKenneth D. Merry 			    "does not have an SMP target!\n", __func__,
3165991554f2SKenneth D. Merry 			    targ->handle, targ->parent_handle);
3166a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3167991554f2SKenneth D. Merry 			goto bailout;
3168991554f2SKenneth D. Merry 
3169991554f2SKenneth D. Merry 		}
3170991554f2SKenneth D. Merry 		if (targ->parent_sasaddr == 0x0) {
3171991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent handle "
3172991554f2SKenneth D. Merry 			    "%d does not have a valid SAS address!\n", __func__,
3173991554f2SKenneth D. Merry 			    targ->handle, targ->parent_handle);
3174a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3175991554f2SKenneth D. Merry 			goto bailout;
3176991554f2SKenneth D. Merry 		}
3177991554f2SKenneth D. Merry 
3178991554f2SKenneth D. Merry 		sasaddr = targ->parent_sasaddr;
3179991554f2SKenneth D. Merry #endif /* OLD_MPR_PROBE */
3180991554f2SKenneth D. Merry 
3181991554f2SKenneth D. Merry 	}
3182991554f2SKenneth D. Merry 
3183991554f2SKenneth D. Merry 	if (sasaddr == 0) {
3184991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INFO, "%s: unable to find SAS address for "
3185991554f2SKenneth D. Merry 		    "handle %d\n", __func__, targ->handle);
3186a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3187991554f2SKenneth D. Merry 		goto bailout;
3188991554f2SKenneth D. Merry 	}
3189991554f2SKenneth D. Merry 	mprsas_send_smpcmd(sassc, ccb, sasaddr);
3190991554f2SKenneth D. Merry 
3191991554f2SKenneth D. Merry 	return;
3192991554f2SKenneth D. Merry 
3193991554f2SKenneth D. Merry bailout:
3194991554f2SKenneth D. Merry 	xpt_done(ccb);
3195991554f2SKenneth D. Merry 
3196991554f2SKenneth D. Merry }
3197991554f2SKenneth D. Merry 
3198991554f2SKenneth D. Merry static void
3199991554f2SKenneth D. Merry mprsas_action_resetdev(struct mprsas_softc *sassc, union ccb *ccb)
3200991554f2SKenneth D. Merry {
3201991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3202991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3203991554f2SKenneth D. Merry 	struct mpr_command *tm;
3204991554f2SKenneth D. Merry 	struct mprsas_target *targ;
3205991554f2SKenneth D. Merry 
3206991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
3207991554f2SKenneth D. Merry 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
3208991554f2SKenneth D. Merry 
32097a2a6a1aSStephen McConnell 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets, ("Target %d out of "
32107a2a6a1aSStephen McConnell 	    "bounds in XPT_RESET_DEV\n", ccb->ccb_h.target_id));
3211991554f2SKenneth D. Merry 	sc = sassc->sc;
32123921a9f7SScott Long 	tm = mprsas_alloc_tm(sc);
3213991554f2SKenneth D. Merry 	if (tm == NULL) {
32147a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "command alloc failure in "
32157a2a6a1aSStephen McConnell 		    "mprsas_action_resetdev\n");
3216a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3217991554f2SKenneth D. Merry 		xpt_done(ccb);
3218991554f2SKenneth D. Merry 		return;
3219991554f2SKenneth D. Merry 	}
3220991554f2SKenneth D. Merry 
3221991554f2SKenneth D. Merry 	targ = &sassc->targets[ccb->ccb_h.target_id];
3222991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3223991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
3224991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3225991554f2SKenneth D. Merry 
322689d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling) {
3227991554f2SKenneth D. Merry 		/* SAS Hard Link Reset / SATA Link Reset */
3228991554f2SKenneth D. Merry 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
322989d1c21fSKashyap D Desai 	} else {
323089d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
323189d1c21fSKashyap D Desai 		req->MsgFlags =
323289d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
323389d1c21fSKashyap D Desai 	}
3234991554f2SKenneth D. Merry 
3235991554f2SKenneth D. Merry 	tm->cm_data = NULL;
3236991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_resetdev_complete;
3237991554f2SKenneth D. Merry 	tm->cm_complete_data = ccb;
3238a2c14879SStephen McConnell 
3239a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
3240a2c14879SStephen McConnell 	    __func__, targ->tid);
3241991554f2SKenneth D. Merry 	tm->cm_targ = targ;
3242a2c14879SStephen McConnell 
324346b9415fSScott Long 	mprsas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
3244991554f2SKenneth D. Merry 	mpr_map_command(sc, tm);
3245991554f2SKenneth D. Merry }
3246991554f2SKenneth D. Merry 
3247991554f2SKenneth D. Merry static void
3248991554f2SKenneth D. Merry mprsas_resetdev_complete(struct mpr_softc *sc, struct mpr_command *tm)
3249991554f2SKenneth D. Merry {
3250991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3251991554f2SKenneth D. Merry 	union ccb *ccb;
3252991554f2SKenneth D. Merry 
3253991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3254991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
3255991554f2SKenneth D. Merry 
3256991554f2SKenneth D. Merry 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3257991554f2SKenneth D. Merry 	ccb = tm->cm_complete_data;
3258991554f2SKenneth D. Merry 
3259991554f2SKenneth D. Merry 	/*
3260991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
3261991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
3262991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
3263991554f2SKenneth D. Merry 	 */
3264991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3265991554f2SKenneth D. Merry 		MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3266991554f2SKenneth D. Merry 
3267991554f2SKenneth D. Merry 		req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3268991554f2SKenneth D. Merry 
3269991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for reset of "
3270991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
3271991554f2SKenneth D. Merry 		    tm->cm_flags, req->DevHandle);
3272a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3273991554f2SKenneth D. Merry 		goto bailout;
3274991554f2SKenneth D. Merry 	}
3275991554f2SKenneth D. Merry 
32767a2a6a1aSStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n",
32777a2a6a1aSStephen McConnell 	    __func__, le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3278991554f2SKenneth D. Merry 
3279991554f2SKenneth D. Merry 	if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3280a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
3281991554f2SKenneth D. Merry 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3282991554f2SKenneth D. Merry 		    CAM_LUN_WILDCARD);
3283991554f2SKenneth D. Merry 	}
3284991554f2SKenneth D. Merry 	else
3285a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3286991554f2SKenneth D. Merry 
3287991554f2SKenneth D. Merry bailout:
3288991554f2SKenneth D. Merry 
3289991554f2SKenneth D. Merry 	mprsas_free_tm(sc, tm);
3290991554f2SKenneth D. Merry 	xpt_done(ccb);
3291991554f2SKenneth D. Merry }
3292991554f2SKenneth D. Merry 
3293991554f2SKenneth D. Merry static void
3294991554f2SKenneth D. Merry mprsas_poll(struct cam_sim *sim)
3295991554f2SKenneth D. Merry {
3296991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
3297991554f2SKenneth D. Merry 
3298991554f2SKenneth D. Merry 	sassc = cam_sim_softc(sim);
3299991554f2SKenneth D. Merry 
3300991554f2SKenneth D. Merry 	if (sassc->sc->mpr_debug & MPR_TRACE) {
3301991554f2SKenneth D. Merry 		/* frequent debug messages during a panic just slow
3302991554f2SKenneth D. Merry 		 * everything down too much.
3303991554f2SKenneth D. Merry 		 */
3304a2c14879SStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "%s clearing MPR_TRACE\n",
3305a2c14879SStephen McConnell 		    __func__);
3306991554f2SKenneth D. Merry 		sassc->sc->mpr_debug &= ~MPR_TRACE;
3307991554f2SKenneth D. Merry 	}
3308991554f2SKenneth D. Merry 
3309991554f2SKenneth D. Merry 	mpr_intr_locked(sassc->sc);
3310991554f2SKenneth D. Merry }
3311991554f2SKenneth D. Merry 
3312991554f2SKenneth D. Merry static void
3313991554f2SKenneth D. Merry mprsas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3314991554f2SKenneth D. Merry     void *arg)
3315991554f2SKenneth D. Merry {
3316991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3317991554f2SKenneth D. Merry 
3318991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)callback_arg;
3319991554f2SKenneth D. Merry 
3320991554f2SKenneth D. Merry 	switch (code) {
3321991554f2SKenneth D. Merry 	case AC_ADVINFO_CHANGED: {
3322991554f2SKenneth D. Merry 		struct mprsas_target *target;
3323991554f2SKenneth D. Merry 		struct mprsas_softc *sassc;
3324991554f2SKenneth D. Merry 		struct scsi_read_capacity_data_long rcap_buf;
3325991554f2SKenneth D. Merry 		struct ccb_dev_advinfo cdai;
3326991554f2SKenneth D. Merry 		struct mprsas_lun *lun;
3327991554f2SKenneth D. Merry 		lun_id_t lunid;
3328991554f2SKenneth D. Merry 		int found_lun;
3329991554f2SKenneth D. Merry 		uintptr_t buftype;
3330991554f2SKenneth D. Merry 
3331991554f2SKenneth D. Merry 		buftype = (uintptr_t)arg;
3332991554f2SKenneth D. Merry 
3333991554f2SKenneth D. Merry 		found_lun = 0;
3334991554f2SKenneth D. Merry 		sassc = sc->sassc;
3335991554f2SKenneth D. Merry 
3336991554f2SKenneth D. Merry 		/*
3337991554f2SKenneth D. Merry 		 * We're only interested in read capacity data changes.
3338991554f2SKenneth D. Merry 		 */
3339991554f2SKenneth D. Merry 		if (buftype != CDAI_TYPE_RCAPLONG)
3340991554f2SKenneth D. Merry 			break;
3341991554f2SKenneth D. Merry 
3342991554f2SKenneth D. Merry 		/*
3343991554f2SKenneth D. Merry 		 * We should have a handle for this, but check to make sure.
3344991554f2SKenneth D. Merry 		 */
3345991554f2SKenneth D. Merry 		KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3346991554f2SKenneth D. Merry 		    ("Target %d out of bounds in mprsas_async\n",
3347991554f2SKenneth D. Merry 		    xpt_path_target_id(path)));
3348991554f2SKenneth D. Merry 		target = &sassc->targets[xpt_path_target_id(path)];
3349991554f2SKenneth D. Merry 		if (target->handle == 0)
3350991554f2SKenneth D. Merry 			break;
3351991554f2SKenneth D. Merry 
3352991554f2SKenneth D. Merry 		lunid = xpt_path_lun_id(path);
3353991554f2SKenneth D. Merry 
3354991554f2SKenneth D. Merry 		SLIST_FOREACH(lun, &target->luns, lun_link) {
3355991554f2SKenneth D. Merry 			if (lun->lun_id == lunid) {
3356991554f2SKenneth D. Merry 				found_lun = 1;
3357991554f2SKenneth D. Merry 				break;
3358991554f2SKenneth D. Merry 			}
3359991554f2SKenneth D. Merry 		}
3360991554f2SKenneth D. Merry 
3361991554f2SKenneth D. Merry 		if (found_lun == 0) {
3362991554f2SKenneth D. Merry 			lun = malloc(sizeof(struct mprsas_lun), M_MPR,
3363991554f2SKenneth D. Merry 			    M_NOWAIT | M_ZERO);
3364991554f2SKenneth D. Merry 			if (lun == NULL) {
3365991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_ERROR, "Unable to alloc "
3366991554f2SKenneth D. Merry 				    "LUN for EEDP support.\n");
3367991554f2SKenneth D. Merry 				break;
3368991554f2SKenneth D. Merry 			}
3369991554f2SKenneth D. Merry 			lun->lun_id = lunid;
3370991554f2SKenneth D. Merry 			SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3371991554f2SKenneth D. Merry 		}
3372991554f2SKenneth D. Merry 
3373991554f2SKenneth D. Merry 		bzero(&rcap_buf, sizeof(rcap_buf));
3374991554f2SKenneth D. Merry 		xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3375991554f2SKenneth D. Merry 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3376991554f2SKenneth D. Merry 		cdai.ccb_h.flags = CAM_DIR_IN;
3377991554f2SKenneth D. Merry 		cdai.buftype = CDAI_TYPE_RCAPLONG;
3378e8577fb4SKenneth D. Merry 		cdai.flags = CDAI_FLAG_NONE;
3379991554f2SKenneth D. Merry 		cdai.bufsiz = sizeof(rcap_buf);
3380991554f2SKenneth D. Merry 		cdai.buf = (uint8_t *)&rcap_buf;
3381991554f2SKenneth D. Merry 		xpt_action((union ccb *)&cdai);
3382991554f2SKenneth D. Merry 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3383991554f2SKenneth D. Merry 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3384991554f2SKenneth D. Merry 
3385a2c14879SStephen McConnell 		if ((mprsas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3386991554f2SKenneth D. Merry 		    && (rcap_buf.prot & SRC16_PROT_EN)) {
33878881681bSKenneth D. Merry 			switch (rcap_buf.prot & SRC16_P_TYPE) {
33888881681bSKenneth D. Merry 			case SRC16_PTYPE_1:
33898881681bSKenneth D. Merry 			case SRC16_PTYPE_3:
3390991554f2SKenneth D. Merry 				lun->eedp_formatted = TRUE;
33918881681bSKenneth D. Merry 				lun->eedp_block_size =
33928881681bSKenneth D. Merry 				    scsi_4btoul(rcap_buf.length);
33938881681bSKenneth D. Merry 				break;
33948881681bSKenneth D. Merry 			case SRC16_PTYPE_2:
33958881681bSKenneth D. Merry 			default:
33968881681bSKenneth D. Merry 				lun->eedp_formatted = FALSE;
33978881681bSKenneth D. Merry 				lun->eedp_block_size = 0;
33988881681bSKenneth D. Merry 				break;
33998881681bSKenneth D. Merry 			}
3400991554f2SKenneth D. Merry 		} else {
3401991554f2SKenneth D. Merry 			lun->eedp_formatted = FALSE;
3402991554f2SKenneth D. Merry 			lun->eedp_block_size = 0;
3403991554f2SKenneth D. Merry 		}
3404991554f2SKenneth D. Merry 		break;
3405991554f2SKenneth D. Merry 	}
34060d87f3c7SWarner Losh 	case AC_FOUND_DEVICE:
3407991554f2SKenneth D. Merry 	default:
3408991554f2SKenneth D. Merry 		break;
3409991554f2SKenneth D. Merry 	}
3410991554f2SKenneth D. Merry }
3411991554f2SKenneth D. Merry 
3412a2c14879SStephen McConnell /*
3413a2c14879SStephen McConnell  * Set the INRESET flag for this target so that no I/O will be sent to
3414a2c14879SStephen McConnell  * the target until the reset has completed.  If an I/O request does
3415a2c14879SStephen McConnell  * happen, the devq will be frozen.  The CCB holds the path which is
3416a2c14879SStephen McConnell  * used to release the devq.  The devq is released and the CCB is freed
3417a2c14879SStephen McConnell  * when the TM completes.
3418a2c14879SStephen McConnell  */
3419b7f1ee79SScott Long void
3420b7f1ee79SScott Long mprsas_prepare_for_tm(struct mpr_softc *sc, struct mpr_command *tm,
3421b7f1ee79SScott Long     struct mprsas_target *target, lun_id_t lun_id)
3422b7f1ee79SScott Long {
3423b7f1ee79SScott Long 	union ccb *ccb;
3424b7f1ee79SScott Long 	path_id_t path_id;
3425b7f1ee79SScott Long 
3426a2c14879SStephen McConnell 	ccb = xpt_alloc_ccb_nowait();
3427a2c14879SStephen McConnell 	if (ccb) {
3428a2c14879SStephen McConnell 		path_id = cam_sim_path(sc->sassc->sim);
3429a2c14879SStephen McConnell 		if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3430a2c14879SStephen McConnell 		    target->tid, lun_id) != CAM_REQ_CMP) {
3431a2c14879SStephen McConnell 			xpt_free_ccb(ccb);
3432a2c14879SStephen McConnell 		} else {
3433a2c14879SStephen McConnell 			tm->cm_ccb = ccb;
3434a2c14879SStephen McConnell 			tm->cm_targ = target;
3435a2c14879SStephen McConnell 			target->flags |= MPRSAS_TARGET_INRESET;
3436a2c14879SStephen McConnell 		}
3437a2c14879SStephen McConnell 	}
3438a2c14879SStephen McConnell }
3439a2c14879SStephen McConnell 
3440991554f2SKenneth D. Merry int
3441991554f2SKenneth D. Merry mprsas_startup(struct mpr_softc *sc)
3442991554f2SKenneth D. Merry {
3443991554f2SKenneth D. Merry 	/*
3444991554f2SKenneth D. Merry 	 * Send the port enable message and set the wait_for_port_enable flag.
3445991554f2SKenneth D. Merry 	 * This flag helps to keep the simq frozen until all discovery events
3446991554f2SKenneth D. Merry 	 * are processed.
3447991554f2SKenneth D. Merry 	 */
3448991554f2SKenneth D. Merry 	sc->wait_for_port_enable = 1;
3449991554f2SKenneth D. Merry 	mprsas_send_portenable(sc);
3450991554f2SKenneth D. Merry 	return (0);
3451991554f2SKenneth D. Merry }
3452991554f2SKenneth D. Merry 
3453991554f2SKenneth D. Merry static int
3454991554f2SKenneth D. Merry mprsas_send_portenable(struct mpr_softc *sc)
3455991554f2SKenneth D. Merry {
3456991554f2SKenneth D. Merry 	MPI2_PORT_ENABLE_REQUEST *request;
3457991554f2SKenneth D. Merry 	struct mpr_command *cm;
3458991554f2SKenneth D. Merry 
3459991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3460991554f2SKenneth D. Merry 
3461991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
3462991554f2SKenneth D. Merry 		return (EBUSY);
3463991554f2SKenneth D. Merry 	request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3464991554f2SKenneth D. Merry 	request->Function = MPI2_FUNCTION_PORT_ENABLE;
3465991554f2SKenneth D. Merry 	request->MsgFlags = 0;
3466991554f2SKenneth D. Merry 	request->VP_ID = 0;
3467991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3468991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_portenable_complete;
3469991554f2SKenneth D. Merry 	cm->cm_data = NULL;
3470991554f2SKenneth D. Merry 	cm->cm_sge = NULL;
3471991554f2SKenneth D. Merry 
3472991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
3473991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO,
3474991554f2SKenneth D. Merry 	    "mpr_send_portenable finished cm %p req %p complete %p\n",
3475991554f2SKenneth D. Merry 	    cm, cm->cm_req, cm->cm_complete);
3476991554f2SKenneth D. Merry 	return (0);
3477991554f2SKenneth D. Merry }
3478991554f2SKenneth D. Merry 
3479991554f2SKenneth D. Merry static void
3480991554f2SKenneth D. Merry mprsas_portenable_complete(struct mpr_softc *sc, struct mpr_command *cm)
3481991554f2SKenneth D. Merry {
3482991554f2SKenneth D. Merry 	MPI2_PORT_ENABLE_REPLY *reply;
3483991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
3484991554f2SKenneth D. Merry 
3485991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3486991554f2SKenneth D. Merry 	sassc = sc->sassc;
3487991554f2SKenneth D. Merry 
3488991554f2SKenneth D. Merry 	/*
3489991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
3490991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
3491991554f2SKenneth D. Merry 	 * port enable commands don't have S/G lists.
3492991554f2SKenneth D. Merry 	 */
3493991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3494991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for port enable! "
3495991554f2SKenneth D. Merry 		    "This should not happen!\n", __func__, cm->cm_flags);
3496991554f2SKenneth D. Merry 	}
3497991554f2SKenneth D. Merry 
3498991554f2SKenneth D. Merry 	reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3499991554f2SKenneth D. Merry 	if (reply == NULL)
3500991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Portenable NULL reply\n");
3501991554f2SKenneth D. Merry 	else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3502991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS)
3503991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Portenable failed\n");
3504991554f2SKenneth D. Merry 
3505991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
3506991554f2SKenneth D. Merry 	/*
3507991554f2SKenneth D. Merry 	 * Done waiting for port enable to complete.  Decrement the refcount.
3508991554f2SKenneth D. Merry 	 * If refcount is 0, discovery is complete and a rescan of the bus can
3509991554f2SKenneth D. Merry 	 * take place.
3510991554f2SKenneth D. Merry 	 */
3511991554f2SKenneth D. Merry 	sc->wait_for_port_enable = 0;
3512991554f2SKenneth D. Merry 	sc->port_enable_complete = 1;
3513991554f2SKenneth D. Merry 	wakeup(&sc->port_enable_complete);
3514991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
3515991554f2SKenneth D. Merry }
3516991554f2SKenneth D. Merry 
3517991554f2SKenneth D. Merry int
3518991554f2SKenneth D. Merry mprsas_check_id(struct mprsas_softc *sassc, int id)
3519991554f2SKenneth D. Merry {
3520991554f2SKenneth D. Merry 	struct mpr_softc *sc = sassc->sc;
3521991554f2SKenneth D. Merry 	char *ids;
3522991554f2SKenneth D. Merry 	char *name;
3523991554f2SKenneth D. Merry 
3524991554f2SKenneth D. Merry 	ids = &sc->exclude_ids[0];
3525991554f2SKenneth D. Merry 	while((name = strsep(&ids, ",")) != NULL) {
3526991554f2SKenneth D. Merry 		if (name[0] == '\0')
3527991554f2SKenneth D. Merry 			continue;
3528991554f2SKenneth D. Merry 		if (strtol(name, NULL, 0) == (long)id)
3529991554f2SKenneth D. Merry 			return (1);
3530991554f2SKenneth D. Merry 	}
3531991554f2SKenneth D. Merry 
3532991554f2SKenneth D. Merry 	return (0);
3533991554f2SKenneth D. Merry }
3534a2c14879SStephen McConnell 
3535a2c14879SStephen McConnell void
3536a2c14879SStephen McConnell mprsas_realloc_targets(struct mpr_softc *sc, int maxtargets)
3537a2c14879SStephen McConnell {
3538a2c14879SStephen McConnell 	struct mprsas_softc *sassc;
3539a2c14879SStephen McConnell 	struct mprsas_lun *lun, *lun_tmp;
3540a2c14879SStephen McConnell 	struct mprsas_target *targ;
3541a2c14879SStephen McConnell 	int i;
3542a2c14879SStephen McConnell 
3543a2c14879SStephen McConnell 	sassc = sc->sassc;
3544a2c14879SStephen McConnell 	/*
3545a2c14879SStephen McConnell 	 * The number of targets is based on IOC Facts, so free all of
3546a2c14879SStephen McConnell 	 * the allocated LUNs for each target and then the target buffer
3547a2c14879SStephen McConnell 	 * itself.
3548a2c14879SStephen McConnell 	 */
3549a2c14879SStephen McConnell 	for (i=0; i< maxtargets; i++) {
3550a2c14879SStephen McConnell 		targ = &sassc->targets[i];
3551a2c14879SStephen McConnell 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3552a2c14879SStephen McConnell 			free(lun, M_MPR);
3553a2c14879SStephen McConnell 		}
3554a2c14879SStephen McConnell 	}
3555a2c14879SStephen McConnell 	free(sassc->targets, M_MPR);
3556a2c14879SStephen McConnell 
3557a2c14879SStephen McConnell 	sassc->targets = malloc(sizeof(struct mprsas_target) * maxtargets,
3558a2c14879SStephen McConnell 	    M_MPR, M_WAITOK|M_ZERO);
3559a2c14879SStephen McConnell 	if (!sassc->targets) {
3560a2c14879SStephen McConnell 		panic("%s failed to alloc targets with error %d\n",
3561a2c14879SStephen McConnell 		    __func__, ENOMEM);
3562a2c14879SStephen McConnell 	}
3563a2c14879SStephen McConnell }
3564