xref: /freebsd/sys/dev/mpr/mpr_sas.c (revision 7608b98c4394590840a53d2b38b908582253de32)
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;
311a2386b6fSAlexander 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  * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
417991554f2SKenneth D. Merry  * Otherwise Volume Delete is same as Bare Drive Removal.
418991554f2SKenneth D. Merry  */
419991554f2SKenneth D. Merry void
420991554f2SKenneth D. Merry mprsas_prepare_volume_remove(struct mprsas_softc *sassc, uint16_t handle)
421991554f2SKenneth D. Merry {
422991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
423991554f2SKenneth D. Merry 	struct mpr_softc *sc;
424991554f2SKenneth D. Merry 	struct mpr_command *cm;
425991554f2SKenneth D. Merry 	struct mprsas_target *targ = NULL;
426991554f2SKenneth D. Merry 
427991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
428991554f2SKenneth D. Merry 	sc = sassc->sc;
429991554f2SKenneth D. Merry 
430991554f2SKenneth D. Merry 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
431991554f2SKenneth D. Merry 	if (targ == NULL) {
432991554f2SKenneth D. Merry 		/* FIXME: what is the action? */
433991554f2SKenneth D. Merry 		/* We don't know about this device? */
434991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR,
435991554f2SKenneth D. Merry 		   "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
436991554f2SKenneth D. Merry 		return;
437991554f2SKenneth D. Merry 	}
438991554f2SKenneth D. Merry 
439991554f2SKenneth D. Merry 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
440991554f2SKenneth D. Merry 
441991554f2SKenneth D. Merry 	cm = mprsas_alloc_tm(sc);
442991554f2SKenneth D. Merry 	if (cm == NULL) {
443991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR,
444991554f2SKenneth D. Merry 		    "%s: command alloc failure\n", __func__);
445991554f2SKenneth D. Merry 		return;
446991554f2SKenneth D. Merry 	}
447991554f2SKenneth D. Merry 
448991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
449991554f2SKenneth D. Merry 
450991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
451991554f2SKenneth D. Merry 	req->DevHandle = targ->handle;
452991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
453991554f2SKenneth D. Merry 
45489d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling) {
455991554f2SKenneth D. Merry 		/* SAS Hard Link Reset / SATA Link Reset */
456991554f2SKenneth D. Merry 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
45789d1c21fSKashyap D Desai 	} else {
45889d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
45989d1c21fSKashyap D Desai 		req->MsgFlags =
46089d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
46189d1c21fSKashyap D Desai 	}
462991554f2SKenneth D. Merry 
463991554f2SKenneth D. Merry 	cm->cm_targ = targ;
464991554f2SKenneth D. Merry 	cm->cm_data = NULL;
465991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_remove_volume;
466991554f2SKenneth D. Merry 	cm->cm_complete_data = (void *)(uintptr_t)handle;
467a2c14879SStephen McConnell 
468a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
469a2c14879SStephen McConnell 	    __func__, targ->tid);
470a2c14879SStephen McConnell 	mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
471a2c14879SStephen McConnell 
472991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
473991554f2SKenneth D. Merry }
474991554f2SKenneth D. Merry 
475991554f2SKenneth D. Merry /*
47667feec50SStephen McConnell  * The firmware performs debounce on the link to avoid transient link errors
47767feec50SStephen McConnell  * and false removals.  When it does decide that link has been lost and a
47867feec50SStephen McConnell  * device needs to go away, it expects that the host will perform a target reset
47967feec50SStephen McConnell  * and then an op remove.  The reset has the side-effect of aborting any
48067feec50SStephen McConnell  * outstanding requests for the device, which is required for the op-remove to
48167feec50SStephen McConnell  * succeed.  It's not clear if the host should check for the device coming back
48267feec50SStephen McConnell  * alive after the reset.
483991554f2SKenneth D. Merry  */
484991554f2SKenneth D. Merry void
485991554f2SKenneth D. Merry mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle)
486991554f2SKenneth D. Merry {
487991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
488991554f2SKenneth D. Merry 	struct mpr_softc *sc;
48946b9415fSScott Long 	struct mpr_command *tm;
490991554f2SKenneth D. Merry 	struct mprsas_target *targ = NULL;
491991554f2SKenneth D. Merry 
492991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
493991554f2SKenneth D. Merry 
494991554f2SKenneth D. Merry 	sc = sassc->sc;
495991554f2SKenneth D. Merry 
496991554f2SKenneth D. Merry 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
497991554f2SKenneth D. Merry 	if (targ == NULL) {
498991554f2SKenneth D. Merry 		/* FIXME: what is the action? */
499991554f2SKenneth D. Merry 		/* We don't know about this device? */
500991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s : invalid handle 0x%x \n",
501991554f2SKenneth D. Merry 		    __func__, handle);
502991554f2SKenneth D. Merry 		return;
503991554f2SKenneth D. Merry 	}
504991554f2SKenneth D. Merry 
505991554f2SKenneth D. Merry 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
506991554f2SKenneth D. Merry 
50746b9415fSScott Long 	tm = mprsas_alloc_tm(sc);
50846b9415fSScott Long 	if (tm == NULL) {
509991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: command alloc failure\n",
510991554f2SKenneth D. Merry 		    __func__);
511991554f2SKenneth D. Merry 		return;
512991554f2SKenneth D. Merry 	}
513991554f2SKenneth D. Merry 
514991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
515991554f2SKenneth D. Merry 
51646b9415fSScott Long 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
517991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
518991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
519991554f2SKenneth D. Merry 
520991554f2SKenneth D. Merry 	/* SAS Hard Link Reset / SATA Link Reset */
521991554f2SKenneth D. Merry 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
522991554f2SKenneth D. Merry 
52346b9415fSScott Long 	tm->cm_targ = targ;
52446b9415fSScott Long 	tm->cm_data = NULL;
52546b9415fSScott Long 	tm->cm_complete = mprsas_remove_device;
52646b9415fSScott Long 	tm->cm_complete_data = (void *)(uintptr_t)handle;
527a2c14879SStephen McConnell 
528a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
529a2c14879SStephen McConnell 	    __func__, targ->tid);
53046b9415fSScott Long 	mprsas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
531a2c14879SStephen McConnell 
53246b9415fSScott Long 	mpr_map_command(sc, tm);
533991554f2SKenneth D. Merry }
534991554f2SKenneth D. Merry 
535991554f2SKenneth D. Merry static void
536991554f2SKenneth D. Merry mprsas_remove_device(struct mpr_softc *sc, struct mpr_command *tm)
537991554f2SKenneth D. Merry {
538991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
539991554f2SKenneth D. Merry 	MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
540991554f2SKenneth D. Merry 	struct mprsas_target *targ;
541991554f2SKenneth D. Merry 	uint16_t handle;
542991554f2SKenneth D. Merry 
543991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
544991554f2SKenneth D. Merry 
545991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
546991554f2SKenneth D. Merry 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
547991554f2SKenneth D. Merry 	targ = tm->cm_targ;
548991554f2SKenneth D. Merry 
549991554f2SKenneth D. Merry 	/*
550991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
551991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
552991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
553991554f2SKenneth D. Merry 	 */
554991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
555991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for remove of "
556991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
557991554f2SKenneth D. Merry 		    tm->cm_flags, handle);
558991554f2SKenneth D. Merry 	}
559991554f2SKenneth D. Merry 
560991554f2SKenneth D. Merry 	if (reply == NULL) {
561991554f2SKenneth D. Merry 		/* XXX retry the remove after the diag reset completes? */
562991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply resetting device "
563991554f2SKenneth D. Merry 		    "0x%04x\n", __func__, handle);
564991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
565991554f2SKenneth D. Merry 		return;
566991554f2SKenneth D. Merry 	}
567991554f2SKenneth D. Merry 
568d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
569d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
57058581c13SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "IOCStatus = 0x%x while resetting "
571991554f2SKenneth D. Merry 		    "device 0x%x\n", le16toh(reply->IOCStatus), handle);
572991554f2SKenneth D. Merry 	}
573991554f2SKenneth D. Merry 
574991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "Reset aborted %u commands\n",
575991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
576991554f2SKenneth D. Merry 	mpr_free_reply(sc, tm->cm_reply_data);
577991554f2SKenneth D. Merry 	tm->cm_reply = NULL;	/* Ensures the reply won't get re-freed */
578991554f2SKenneth D. Merry 
579991554f2SKenneth D. Merry 	/* Reuse the existing command */
580991554f2SKenneth D. Merry 	req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
581991554f2SKenneth D. Merry 	memset(req, 0, sizeof(*req));
582991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
583991554f2SKenneth D. Merry 	req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
584991554f2SKenneth D. Merry 	req->DevHandle = htole16(handle);
585991554f2SKenneth D. Merry 	tm->cm_data = NULL;
586991554f2SKenneth D. Merry 	tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
587991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_remove_complete;
588991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)(uintptr_t)handle;
589991554f2SKenneth D. Merry 
5904c1cdd4aSWarner Losh 	/*
5914c1cdd4aSWarner Losh 	 * Wait to send the REMOVE_DEVICE until all the commands have cleared.
5924c1cdd4aSWarner Losh 	 * They should be aborted or time out and we'll kick thus off there
5934c1cdd4aSWarner Losh 	 * if so.
5944c1cdd4aSWarner Losh 	 */
5954c1cdd4aSWarner Losh 	if (TAILQ_FIRST(&targ->commands) == NULL) {
5964c1cdd4aSWarner Losh 		mpr_dprint(sc, MPR_INFO, "No pending commands: starting remove_device\n");
597991554f2SKenneth D. Merry 		mpr_map_command(sc, tm);
5984c1cdd4aSWarner Losh 		targ->pending_remove_tm = NULL;
5994c1cdd4aSWarner Losh 	} else {
6004c1cdd4aSWarner Losh 		targ->pending_remove_tm = tm;
6014c1cdd4aSWarner Losh 	}
602991554f2SKenneth D. Merry 
603a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "clearing target %u handle 0x%04x\n",
604991554f2SKenneth D. Merry 	    targ->tid, handle);
605991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
606a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "At enclosure level %d, slot %d, "
607991554f2SKenneth D. Merry 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
608991554f2SKenneth D. Merry 		    targ->connector_name);
609991554f2SKenneth D. Merry 	}
610991554f2SKenneth D. Merry }
611991554f2SKenneth D. Merry 
612991554f2SKenneth D. Merry static void
613991554f2SKenneth D. Merry mprsas_remove_complete(struct mpr_softc *sc, struct mpr_command *tm)
614991554f2SKenneth D. Merry {
615991554f2SKenneth D. Merry 	MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
616991554f2SKenneth D. Merry 	uint16_t handle;
617991554f2SKenneth D. Merry 	struct mprsas_target *targ;
618991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
619991554f2SKenneth D. Merry 
620991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
621991554f2SKenneth D. Merry 
622991554f2SKenneth D. Merry 	reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
623991554f2SKenneth D. Merry 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
624991554f2SKenneth D. Merry 
6254c1cdd4aSWarner Losh 	targ = tm->cm_targ;
6264c1cdd4aSWarner Losh 
6274c1cdd4aSWarner Losh 	/*
6284c1cdd4aSWarner Losh 	 * At this point, we should have no pending commands for the target.
6294c1cdd4aSWarner Losh 	 * The remove target has just completed.
6304c1cdd4aSWarner Losh 	 */
6314c1cdd4aSWarner Losh 	KASSERT(TAILQ_FIRST(&targ->commands) == NULL,
6324c1cdd4aSWarner Losh 	    ("%s: no commands should be pending\n", __func__));
6334c1cdd4aSWarner Losh 
634991554f2SKenneth D. Merry 	/*
635991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
636991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
637991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
638991554f2SKenneth D. Merry 	 */
639991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
640991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "%s: cm_flags = %#x for remove of "
641991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
642991554f2SKenneth D. Merry 		    tm->cm_flags, handle);
643991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
644991554f2SKenneth D. Merry 		return;
645991554f2SKenneth D. Merry 	}
646991554f2SKenneth D. Merry 
647991554f2SKenneth D. Merry 	if (reply == NULL) {
648991554f2SKenneth D. Merry 		/* most likely a chip reset */
649991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply removing device "
650991554f2SKenneth D. Merry 		    "0x%04x\n", __func__, handle);
651991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
652991554f2SKenneth D. Merry 		return;
653991554f2SKenneth D. Merry 	}
654991554f2SKenneth D. Merry 
655991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s on handle 0x%04x, IOCStatus= 0x%x\n",
656991554f2SKenneth D. Merry 	    __func__, handle, le16toh(reply->IOCStatus));
657991554f2SKenneth D. Merry 
658991554f2SKenneth D. Merry 	/*
659991554f2SKenneth D. Merry 	 * Don't clear target if remove fails because things will get confusing.
660991554f2SKenneth D. Merry 	 * Leave the devname and sasaddr intact so that we know to avoid reusing
661991554f2SKenneth D. Merry 	 * this target id if possible, and so we can assign the same target id
662991554f2SKenneth D. Merry 	 * to this device if it comes back in the future.
663991554f2SKenneth D. Merry 	 */
664d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
665d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
666991554f2SKenneth D. Merry 		targ->handle = 0x0;
667991554f2SKenneth D. Merry 		targ->encl_handle = 0x0;
668991554f2SKenneth D. Merry 		targ->encl_level_valid = 0x0;
669991554f2SKenneth D. Merry 		targ->encl_level = 0x0;
670991554f2SKenneth D. Merry 		targ->connector_name[0] = ' ';
671991554f2SKenneth D. Merry 		targ->connector_name[1] = ' ';
672991554f2SKenneth D. Merry 		targ->connector_name[2] = ' ';
673991554f2SKenneth D. Merry 		targ->connector_name[3] = ' ';
674991554f2SKenneth D. Merry 		targ->encl_slot = 0x0;
675991554f2SKenneth D. Merry 		targ->exp_dev_handle = 0x0;
676991554f2SKenneth D. Merry 		targ->phy_num = 0x0;
677991554f2SKenneth D. Merry 		targ->linkrate = 0x0;
678991554f2SKenneth D. Merry 		targ->devinfo = 0x0;
679991554f2SKenneth D. Merry 		targ->flags = 0x0;
680991554f2SKenneth D. Merry 		targ->scsi_req_desc_type = 0;
681991554f2SKenneth D. Merry 
682991554f2SKenneth D. Merry 		while (!SLIST_EMPTY(&targ->luns)) {
683991554f2SKenneth D. Merry 			lun = SLIST_FIRST(&targ->luns);
684991554f2SKenneth D. Merry 			SLIST_REMOVE_HEAD(&targ->luns, lun_link);
685991554f2SKenneth D. Merry 			free(lun, M_MPR);
686991554f2SKenneth D. Merry 		}
687991554f2SKenneth D. Merry 	}
688991554f2SKenneth D. Merry 
689991554f2SKenneth D. Merry 	mprsas_free_tm(sc, tm);
690991554f2SKenneth D. Merry }
691991554f2SKenneth D. Merry 
692991554f2SKenneth D. Merry static int
693991554f2SKenneth D. Merry mprsas_register_events(struct mpr_softc *sc)
694991554f2SKenneth D. Merry {
695991554f2SKenneth D. Merry 	uint8_t events[16];
696991554f2SKenneth D. Merry 
697991554f2SKenneth D. Merry 	bzero(events, 16);
698991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
699991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_DISCOVERY);
700991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
701991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
702991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
703991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
704991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
705991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
706991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_VOLUME);
707991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
708991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
709991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_TEMP_THRESHOLD);
7105f5baf0eSAlexander Motin 	setbit(events, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
71167feec50SStephen McConnell 	if (sc->facts->MsgVersion >= MPI2_VERSION_02_06) {
7122bbc5fcbSStephen McConnell 		setbit(events, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
71367feec50SStephen McConnell 		if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC) {
71467feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
71567feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_ENUMERATION);
71667feec50SStephen McConnell 			setbit(events, MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
71767feec50SStephen McConnell 		}
71867feec50SStephen McConnell 	}
719991554f2SKenneth D. Merry 
720991554f2SKenneth D. Merry 	mpr_register_events(sc, events, mprsas_evt_handler, NULL,
721991554f2SKenneth D. Merry 	    &sc->sassc->mprsas_eh);
722991554f2SKenneth D. Merry 
723991554f2SKenneth D. Merry 	return (0);
724991554f2SKenneth D. Merry }
725991554f2SKenneth D. Merry 
726991554f2SKenneth D. Merry int
727991554f2SKenneth D. Merry mpr_attach_sas(struct mpr_softc *sc)
728991554f2SKenneth D. Merry {
729991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
730991554f2SKenneth D. Merry 	cam_status status;
73162a09ee9SAlexander Motin 	int unit, error = 0, reqs;
732991554f2SKenneth D. Merry 
733991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
734757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
735991554f2SKenneth D. Merry 
736991554f2SKenneth D. Merry 	sassc = malloc(sizeof(struct mprsas_softc), M_MPR, M_WAITOK|M_ZERO);
737991554f2SKenneth D. Merry 
738991554f2SKenneth D. Merry 	/*
739a2c14879SStephen McConnell 	 * XXX MaxTargets could change during a reinit.  Since we don't
740991554f2SKenneth D. Merry 	 * resize the targets[] array during such an event, cache the value
741991554f2SKenneth D. Merry 	 * of MaxTargets here so that we don't get into trouble later.  This
742991554f2SKenneth D. Merry 	 * should move into the reinit logic.
743991554f2SKenneth D. Merry 	 */
744327f2e6cSStephen McConnell 	sassc->maxtargets = sc->facts->MaxTargets + sc->facts->MaxVolumes;
745991554f2SKenneth D. Merry 	sassc->targets = malloc(sizeof(struct mprsas_target) *
746991554f2SKenneth D. Merry 	    sassc->maxtargets, M_MPR, M_WAITOK|M_ZERO);
747991554f2SKenneth D. Merry 	sc->sassc = sassc;
748991554f2SKenneth D. Merry 	sassc->sc = sc;
749991554f2SKenneth D. Merry 
75062a09ee9SAlexander Motin 	reqs = sc->num_reqs - sc->num_prireqs - 1;
75162a09ee9SAlexander Motin 	if ((sassc->devq = cam_simq_alloc(reqs)) == NULL) {
752757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR, "Cannot allocate SIMQ\n");
753991554f2SKenneth D. Merry 		error = ENOMEM;
754991554f2SKenneth D. Merry 		goto out;
755991554f2SKenneth D. Merry 	}
756991554f2SKenneth D. Merry 
757991554f2SKenneth D. Merry 	unit = device_get_unit(sc->mpr_dev);
758991554f2SKenneth D. Merry 	sassc->sim = cam_sim_alloc(mprsas_action, mprsas_poll, "mpr", sassc,
75962a09ee9SAlexander Motin 	    unit, &sc->mpr_mtx, reqs, reqs, sassc->devq);
760991554f2SKenneth D. Merry 	if (sassc->sim == NULL) {
761757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR, "Cannot allocate SIM\n");
762991554f2SKenneth D. Merry 		error = EINVAL;
763991554f2SKenneth D. Merry 		goto out;
764991554f2SKenneth D. Merry 	}
765991554f2SKenneth D. Merry 
766991554f2SKenneth D. Merry 	TAILQ_INIT(&sassc->ev_queue);
767991554f2SKenneth D. Merry 
768991554f2SKenneth D. Merry 	/* Initialize taskqueue for Event Handling */
769991554f2SKenneth D. Merry 	TASK_INIT(&sassc->ev_task, 0, mprsas_firmware_event_work, sc);
770991554f2SKenneth D. Merry 	sassc->ev_tq = taskqueue_create("mpr_taskq", M_NOWAIT | M_ZERO,
771991554f2SKenneth D. Merry 	    taskqueue_thread_enqueue, &sassc->ev_tq);
772c2a0f07aSAlexander Motin 	taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
773991554f2SKenneth D. Merry 	    device_get_nameunit(sc->mpr_dev));
774991554f2SKenneth D. Merry 
775991554f2SKenneth D. Merry 	mpr_lock(sc);
776991554f2SKenneth D. Merry 
777991554f2SKenneth D. Merry 	/*
778991554f2SKenneth D. Merry 	 * XXX There should be a bus for every port on the adapter, but since
779991554f2SKenneth D. Merry 	 * we're just going to fake the topology for now, we'll pretend that
780991554f2SKenneth D. Merry 	 * everything is just a target on a single bus.
781991554f2SKenneth D. Merry 	 */
782991554f2SKenneth D. Merry 	if ((error = xpt_bus_register(sassc->sim, sc->mpr_dev, 0)) != 0) {
783757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
784757ff642SScott Long 		    "Error %d registering SCSI bus\n", error);
785991554f2SKenneth D. Merry 		mpr_unlock(sc);
786991554f2SKenneth D. Merry 		goto out;
787991554f2SKenneth D. Merry 	}
788991554f2SKenneth D. Merry 
789991554f2SKenneth D. Merry 	/*
790a2c14879SStephen McConnell 	 * Assume that discovery events will start right away.
791991554f2SKenneth D. Merry 	 *
792991554f2SKenneth D. Merry 	 * Hold off boot until discovery is complete.
793991554f2SKenneth D. Merry 	 */
794991554f2SKenneth D. Merry 	sassc->flags |= MPRSAS_IN_STARTUP | MPRSAS_IN_DISCOVERY;
795991554f2SKenneth D. Merry 	sc->sassc->startup_refcount = 0;
796991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
797991554f2SKenneth D. Merry 
798a2c14879SStephen McConnell 	callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
799991554f2SKenneth D. Merry 
800991554f2SKenneth D. Merry 	/*
801991554f2SKenneth D. Merry 	 * Register for async events so we can determine the EEDP
802991554f2SKenneth D. Merry 	 * capabilities of devices.
803991554f2SKenneth D. Merry 	 */
804991554f2SKenneth D. Merry 	status = xpt_create_path(&sassc->path, /*periph*/NULL,
805991554f2SKenneth D. Merry 	    cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
806991554f2SKenneth D. Merry 	    CAM_LUN_WILDCARD);
807991554f2SKenneth D. Merry 	if (status != CAM_REQ_CMP) {
808757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
809757ff642SScott Long 		    "Error %#x creating sim path\n", status);
810991554f2SKenneth D. Merry 		sassc->path = NULL;
811991554f2SKenneth D. Merry 	} else {
812991554f2SKenneth D. Merry 		int event;
813991554f2SKenneth D. Merry 
814991554f2SKenneth D. Merry 		event = AC_ADVINFO_CHANGED | AC_FOUND_DEVICE;
815991554f2SKenneth D. Merry 		status = xpt_register_async(event, mprsas_async, sc,
816991554f2SKenneth D. Merry 					    sassc->path);
81707aa4de1SKenneth D. Merry 
818991554f2SKenneth D. Merry 		if (status != CAM_REQ_CMP) {
819991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR,
820991554f2SKenneth D. Merry 			    "Error %#x registering async handler for "
821991554f2SKenneth D. Merry 			    "AC_ADVINFO_CHANGED events\n", status);
822991554f2SKenneth D. Merry 			xpt_free_path(sassc->path);
823991554f2SKenneth D. Merry 			sassc->path = NULL;
824991554f2SKenneth D. Merry 		}
825991554f2SKenneth D. Merry 	}
826991554f2SKenneth D. Merry 	if (status != CAM_REQ_CMP) {
827991554f2SKenneth D. Merry 		/*
828991554f2SKenneth D. Merry 		 * EEDP use is the exception, not the rule.
829991554f2SKenneth D. Merry 		 * Warn the user, but do not fail to attach.
830991554f2SKenneth D. Merry 		 */
831991554f2SKenneth D. Merry 		mpr_printf(sc, "EEDP capabilities disabled.\n");
832991554f2SKenneth D. Merry 	}
833991554f2SKenneth D. Merry 
834991554f2SKenneth D. Merry 	mpr_unlock(sc);
835991554f2SKenneth D. Merry 
836991554f2SKenneth D. Merry 	mprsas_register_events(sc);
837991554f2SKenneth D. Merry out:
838991554f2SKenneth D. Merry 	if (error)
839991554f2SKenneth D. Merry 		mpr_detach_sas(sc);
840757ff642SScott Long 
841757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit, error= %d\n", __func__, error);
842991554f2SKenneth D. Merry 	return (error);
843991554f2SKenneth D. Merry }
844991554f2SKenneth D. Merry 
845991554f2SKenneth D. Merry int
846991554f2SKenneth D. Merry mpr_detach_sas(struct mpr_softc *sc)
847991554f2SKenneth D. Merry {
848991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
849991554f2SKenneth D. Merry 	struct mprsas_lun *lun, *lun_tmp;
850991554f2SKenneth D. Merry 	struct mprsas_target *targ;
851991554f2SKenneth D. Merry 	int i;
852991554f2SKenneth D. Merry 
853991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
854991554f2SKenneth D. Merry 
855991554f2SKenneth D. Merry 	if (sc->sassc == NULL)
856991554f2SKenneth D. Merry 		return (0);
857991554f2SKenneth D. Merry 
858991554f2SKenneth D. Merry 	sassc = sc->sassc;
859991554f2SKenneth D. Merry 	mpr_deregister_events(sc, sassc->mprsas_eh);
860991554f2SKenneth D. Merry 
861991554f2SKenneth D. Merry 	/*
862991554f2SKenneth D. Merry 	 * Drain and free the event handling taskqueue with the lock
863991554f2SKenneth D. Merry 	 * unheld so that any parallel processing tasks drain properly
864991554f2SKenneth D. Merry 	 * without deadlocking.
865991554f2SKenneth D. Merry 	 */
866991554f2SKenneth D. Merry 	if (sassc->ev_tq != NULL)
867991554f2SKenneth D. Merry 		taskqueue_free(sassc->ev_tq);
868991554f2SKenneth D. Merry 
869991554f2SKenneth D. Merry 	/* Make sure CAM doesn't wedge if we had to bail out early. */
870991554f2SKenneth D. Merry 	mpr_lock(sc);
871991554f2SKenneth D. Merry 
8723c5ac992SScott Long 	while (sassc->startup_refcount != 0)
8733c5ac992SScott Long 		mprsas_startup_decrement(sassc);
8743c5ac992SScott Long 
875991554f2SKenneth D. Merry 	/* Deregister our async handler */
876991554f2SKenneth D. Merry 	if (sassc->path != NULL) {
877991554f2SKenneth D. Merry 		xpt_register_async(0, mprsas_async, sc, sassc->path);
878991554f2SKenneth D. Merry 		xpt_free_path(sassc->path);
879991554f2SKenneth D. Merry 		sassc->path = NULL;
880991554f2SKenneth D. Merry 	}
881991554f2SKenneth D. Merry 
882991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_IN_STARTUP)
883991554f2SKenneth D. Merry 		xpt_release_simq(sassc->sim, 1);
884991554f2SKenneth D. Merry 
885991554f2SKenneth D. Merry 	if (sassc->sim != NULL) {
886991554f2SKenneth D. Merry 		xpt_bus_deregister(cam_sim_path(sassc->sim));
887991554f2SKenneth D. Merry 		cam_sim_free(sassc->sim, FALSE);
888991554f2SKenneth D. Merry 	}
889991554f2SKenneth D. Merry 
890991554f2SKenneth D. Merry 	mpr_unlock(sc);
891991554f2SKenneth D. Merry 
892991554f2SKenneth D. Merry 	if (sassc->devq != NULL)
893991554f2SKenneth D. Merry 		cam_simq_free(sassc->devq);
894991554f2SKenneth D. Merry 
895991554f2SKenneth D. Merry 	for (i = 0; i < sassc->maxtargets; i++) {
896991554f2SKenneth D. Merry 		targ = &sassc->targets[i];
897991554f2SKenneth D. Merry 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
898991554f2SKenneth D. Merry 			free(lun, M_MPR);
899991554f2SKenneth D. Merry 		}
900991554f2SKenneth D. Merry 	}
901991554f2SKenneth D. Merry 	free(sassc->targets, M_MPR);
902991554f2SKenneth D. Merry 	free(sassc, M_MPR);
903991554f2SKenneth D. Merry 	sc->sassc = NULL;
904991554f2SKenneth D. Merry 
905991554f2SKenneth D. Merry 	return (0);
906991554f2SKenneth D. Merry }
907991554f2SKenneth D. Merry 
908991554f2SKenneth D. Merry void
909991554f2SKenneth D. Merry mprsas_discovery_end(struct mprsas_softc *sassc)
910991554f2SKenneth D. Merry {
911991554f2SKenneth D. Merry 	struct mpr_softc *sc = sassc->sc;
912991554f2SKenneth D. Merry 
913991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
914991554f2SKenneth D. Merry 
915991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_DISCOVERY_TIMEOUT_PENDING)
916991554f2SKenneth D. Merry 		callout_stop(&sassc->discovery_callout);
917991554f2SKenneth D. Merry 
918327f2e6cSStephen McConnell 	/*
919327f2e6cSStephen McConnell 	 * After discovery has completed, check the mapping table for any
920327f2e6cSStephen McConnell 	 * missing devices and update their missing counts. Only do this once
921327f2e6cSStephen McConnell 	 * whenever the driver is initialized so that missing counts aren't
922327f2e6cSStephen McConnell 	 * updated unnecessarily. Note that just because discovery has
923327f2e6cSStephen McConnell 	 * completed doesn't mean that events have been processed yet. The
924327f2e6cSStephen McConnell 	 * check_devices function is a callout timer that checks if ALL devices
925327f2e6cSStephen McConnell 	 * are missing. If so, it will wait a little longer for events to
926327f2e6cSStephen McConnell 	 * complete and keep resetting itself until some device in the mapping
927327f2e6cSStephen McConnell 	 * table is not missing, meaning that event processing has started.
928327f2e6cSStephen McConnell 	 */
929327f2e6cSStephen McConnell 	if (sc->track_mapping_events) {
930327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_XINFO | MPR_MAPPING, "Discovery has "
931327f2e6cSStephen McConnell 		    "completed. Check for missing devices in the mapping "
932327f2e6cSStephen McConnell 		    "table.\n");
933327f2e6cSStephen McConnell 		callout_reset(&sc->device_check_callout,
934327f2e6cSStephen McConnell 		    MPR_MISSING_CHECK_DELAY * hz, mpr_mapping_check_devices,
935327f2e6cSStephen McConnell 		    sc);
936327f2e6cSStephen McConnell 	}
937991554f2SKenneth D. Merry }
938991554f2SKenneth D. Merry 
939991554f2SKenneth D. Merry static void
940991554f2SKenneth D. Merry mprsas_action(struct cam_sim *sim, union ccb *ccb)
941991554f2SKenneth D. Merry {
942991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
943991554f2SKenneth D. Merry 
944991554f2SKenneth D. Merry 	sassc = cam_sim_softc(sim);
945991554f2SKenneth D. Merry 
946991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
947a2c14879SStephen McConnell 	mpr_dprint(sassc->sc, MPR_TRACE, "ccb func_code 0x%x\n",
948991554f2SKenneth D. Merry 	    ccb->ccb_h.func_code);
949991554f2SKenneth D. Merry 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
950991554f2SKenneth D. Merry 
951991554f2SKenneth D. Merry 	switch (ccb->ccb_h.func_code) {
952991554f2SKenneth D. Merry 	case XPT_PATH_INQ:
953991554f2SKenneth D. Merry 	{
954991554f2SKenneth D. Merry 		struct ccb_pathinq *cpi = &ccb->cpi;
95532b0a21eSStephen McConnell 		struct mpr_softc *sc = sassc->sc;
956991554f2SKenneth D. Merry 
957991554f2SKenneth D. Merry 		cpi->version_num = 1;
958991554f2SKenneth D. Merry 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
959991554f2SKenneth D. Merry 		cpi->target_sprt = 0;
960991554f2SKenneth D. Merry 		cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
961991554f2SKenneth D. Merry 		cpi->hba_eng_cnt = 0;
962991554f2SKenneth D. Merry 		cpi->max_target = sassc->maxtargets - 1;
963991554f2SKenneth D. Merry 		cpi->max_lun = 255;
964327f2e6cSStephen McConnell 
965327f2e6cSStephen McConnell 		/*
966327f2e6cSStephen McConnell 		 * initiator_id is set here to an ID outside the set of valid
967327f2e6cSStephen McConnell 		 * target IDs (including volumes).
968327f2e6cSStephen McConnell 		 */
969327f2e6cSStephen McConnell 		cpi->initiator_id = sassc->maxtargets;
9704195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
9714195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN);
9724195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
973991554f2SKenneth D. Merry 		cpi->unit_number = cam_sim_unit(sim);
974991554f2SKenneth D. Merry 		cpi->bus_id = cam_sim_bus(sim);
975991554f2SKenneth D. Merry 		/*
976991554f2SKenneth D. Merry 		 * XXXSLM-I think this needs to change based on config page or
977991554f2SKenneth D. Merry 		 * something instead of hardcoded to 150000.
978991554f2SKenneth D. Merry 		 */
979991554f2SKenneth D. Merry 		cpi->base_transfer_speed = 150000;
980991554f2SKenneth D. Merry 		cpi->transport = XPORT_SAS;
981991554f2SKenneth D. Merry 		cpi->transport_version = 0;
982991554f2SKenneth D. Merry 		cpi->protocol = PROTO_SCSI;
983991554f2SKenneth D. Merry 		cpi->protocol_version = SCSI_REV_SPC;
9844f5d6573SAlexander Motin 		cpi->maxio = sc->maxio;
985a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
986991554f2SKenneth D. Merry 		break;
987991554f2SKenneth D. Merry 	}
988991554f2SKenneth D. Merry 	case XPT_GET_TRAN_SETTINGS:
989991554f2SKenneth D. Merry 	{
990991554f2SKenneth D. Merry 		struct ccb_trans_settings	*cts;
991991554f2SKenneth D. Merry 		struct ccb_trans_settings_sas	*sas;
992991554f2SKenneth D. Merry 		struct ccb_trans_settings_scsi	*scsi;
993991554f2SKenneth D. Merry 		struct mprsas_target *targ;
994991554f2SKenneth D. Merry 
995991554f2SKenneth D. Merry 		cts = &ccb->cts;
996991554f2SKenneth D. Merry 		sas = &cts->xport_specific.sas;
997991554f2SKenneth D. Merry 		scsi = &cts->proto_specific.scsi;
998991554f2SKenneth D. Merry 
999991554f2SKenneth D. Merry 		KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
1000991554f2SKenneth D. Merry 		    ("Target %d out of bounds in XPT_GET_TRAN_SETTINGS\n",
1001991554f2SKenneth D. Merry 		    cts->ccb_h.target_id));
1002991554f2SKenneth D. Merry 		targ = &sassc->targets[cts->ccb_h.target_id];
1003991554f2SKenneth D. Merry 		if (targ->handle == 0x0) {
1004a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1005991554f2SKenneth D. Merry 			break;
1006991554f2SKenneth D. Merry 		}
1007991554f2SKenneth D. Merry 
1008991554f2SKenneth D. Merry 		cts->protocol_version = SCSI_REV_SPC2;
1009991554f2SKenneth D. Merry 		cts->transport = XPORT_SAS;
1010991554f2SKenneth D. Merry 		cts->transport_version = 0;
1011991554f2SKenneth D. Merry 
1012991554f2SKenneth D. Merry 		sas->valid = CTS_SAS_VALID_SPEED;
1013991554f2SKenneth D. Merry 		switch (targ->linkrate) {
1014991554f2SKenneth D. Merry 		case 0x08:
1015991554f2SKenneth D. Merry 			sas->bitrate = 150000;
1016991554f2SKenneth D. Merry 			break;
1017991554f2SKenneth D. Merry 		case 0x09:
1018991554f2SKenneth D. Merry 			sas->bitrate = 300000;
1019991554f2SKenneth D. Merry 			break;
1020991554f2SKenneth D. Merry 		case 0x0a:
1021991554f2SKenneth D. Merry 			sas->bitrate = 600000;
1022991554f2SKenneth D. Merry 			break;
102382315915SAlexander Motin 		case 0x0b:
102482315915SAlexander Motin 			sas->bitrate = 1200000;
102582315915SAlexander Motin 			break;
1026991554f2SKenneth D. Merry 		default:
1027991554f2SKenneth D. Merry 			sas->valid = 0;
1028991554f2SKenneth D. Merry 		}
1029991554f2SKenneth D. Merry 
1030991554f2SKenneth D. Merry 		cts->protocol = PROTO_SCSI;
1031991554f2SKenneth D. Merry 		scsi->valid = CTS_SCSI_VALID_TQ;
1032991554f2SKenneth D. Merry 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1033991554f2SKenneth D. Merry 
1034a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1035991554f2SKenneth D. Merry 		break;
1036991554f2SKenneth D. Merry 	}
1037991554f2SKenneth D. Merry 	case XPT_CALC_GEOMETRY:
1038991554f2SKenneth D. Merry 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
1039a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1040991554f2SKenneth D. Merry 		break;
1041991554f2SKenneth D. Merry 	case XPT_RESET_DEV:
10427a2a6a1aSStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action "
10437a2a6a1aSStephen McConnell 		    "XPT_RESET_DEV\n");
1044991554f2SKenneth D. Merry 		mprsas_action_resetdev(sassc, ccb);
1045991554f2SKenneth D. Merry 		return;
1046991554f2SKenneth D. Merry 	case XPT_RESET_BUS:
1047991554f2SKenneth D. Merry 	case XPT_ABORT:
1048991554f2SKenneth D. Merry 	case XPT_TERM_IO:
10497a2a6a1aSStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action faking success "
10507a2a6a1aSStephen McConnell 		    "for abort or reset\n");
1051a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1052991554f2SKenneth D. Merry 		break;
1053991554f2SKenneth D. Merry 	case XPT_SCSI_IO:
1054991554f2SKenneth D. Merry 		mprsas_action_scsiio(sassc, ccb);
1055991554f2SKenneth D. Merry 		return;
1056991554f2SKenneth D. Merry 	case XPT_SMP_IO:
1057991554f2SKenneth D. Merry 		mprsas_action_smpio(sassc, ccb);
1058991554f2SKenneth D. Merry 		return;
1059991554f2SKenneth D. Merry 	default:
1060a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1061991554f2SKenneth D. Merry 		break;
1062991554f2SKenneth D. Merry 	}
1063991554f2SKenneth D. Merry 	xpt_done(ccb);
1064991554f2SKenneth D. Merry 
1065991554f2SKenneth D. Merry }
1066991554f2SKenneth D. Merry 
1067991554f2SKenneth D. Merry static void
1068991554f2SKenneth D. Merry mprsas_announce_reset(struct mpr_softc *sc, uint32_t ac_code,
1069991554f2SKenneth D. Merry     target_id_t target_id, lun_id_t lun_id)
1070991554f2SKenneth D. Merry {
1071991554f2SKenneth D. Merry 	path_id_t path_id = cam_sim_path(sc->sassc->sim);
1072991554f2SKenneth D. Merry 	struct cam_path *path;
1073991554f2SKenneth D. Merry 
1074991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s code %x target %d lun %jx\n", __func__,
1075991554f2SKenneth D. Merry 	    ac_code, target_id, (uintmax_t)lun_id);
1076991554f2SKenneth D. Merry 
1077991554f2SKenneth D. Merry 	if (xpt_create_path(&path, NULL,
1078991554f2SKenneth D. Merry 		path_id, target_id, lun_id) != CAM_REQ_CMP) {
1079991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unable to create path for reset "
1080991554f2SKenneth D. Merry 		    "notification\n");
1081991554f2SKenneth D. Merry 		return;
1082991554f2SKenneth D. Merry 	}
1083991554f2SKenneth D. Merry 
1084991554f2SKenneth D. Merry 	xpt_async(ac_code, path, NULL);
1085991554f2SKenneth D. Merry 	xpt_free_path(path);
1086991554f2SKenneth D. Merry }
1087991554f2SKenneth D. Merry 
1088991554f2SKenneth D. Merry static void
1089991554f2SKenneth D. Merry mprsas_complete_all_commands(struct mpr_softc *sc)
1090991554f2SKenneth D. Merry {
1091991554f2SKenneth D. Merry 	struct mpr_command *cm;
1092991554f2SKenneth D. Merry 	int i;
1093991554f2SKenneth D. Merry 	int completed;
1094991554f2SKenneth D. Merry 
1095991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1096991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1097991554f2SKenneth D. Merry 
1098991554f2SKenneth D. Merry 	/* complete all commands with a NULL reply */
1099991554f2SKenneth D. Merry 	for (i = 1; i < sc->num_reqs; i++) {
1100991554f2SKenneth D. Merry 		cm = &sc->commands[i];
1101f0779b04SScott Long 		if (cm->cm_state == MPR_CM_STATE_FREE)
1102f0779b04SScott Long 			continue;
1103f0779b04SScott Long 
1104f0779b04SScott Long 		cm->cm_state = MPR_CM_STATE_BUSY;
1105991554f2SKenneth D. Merry 		cm->cm_reply = NULL;
1106991554f2SKenneth D. Merry 		completed = 0;
1107991554f2SKenneth D. Merry 
11088277ce2bSConrad Meyer 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
11098277ce2bSConrad Meyer 			MPASS(cm->cm_data);
11108277ce2bSConrad Meyer 			free(cm->cm_data, M_MPR);
11118277ce2bSConrad Meyer 			cm->cm_data = NULL;
11128277ce2bSConrad Meyer 		}
11138277ce2bSConrad Meyer 
1114991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_POLLED)
1115991554f2SKenneth D. Merry 			cm->cm_flags |= MPR_CM_FLAGS_COMPLETE;
1116991554f2SKenneth D. Merry 
1117991554f2SKenneth D. Merry 		if (cm->cm_complete != NULL) {
1118991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
11197a2a6a1aSStephen McConnell 			    "completing cm %p state %x ccb %p for diag reset\n",
11207a2a6a1aSStephen McConnell 			    cm, cm->cm_state, cm->cm_ccb);
1121991554f2SKenneth D. Merry 			cm->cm_complete(sc, cm);
1122991554f2SKenneth D. Merry 			completed = 1;
1123f0779b04SScott Long 		} else if (cm->cm_flags & MPR_CM_FLAGS_WAKEUP) {
1124991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
1125991554f2SKenneth D. Merry 			    "waking up cm %p state %x ccb %p for diag reset\n",
1126991554f2SKenneth D. Merry 			    cm, cm->cm_state, cm->cm_ccb);
1127991554f2SKenneth D. Merry 			wakeup(cm);
1128991554f2SKenneth D. Merry 			completed = 1;
1129991554f2SKenneth D. Merry 		}
1130991554f2SKenneth D. Merry 
1131991554f2SKenneth D. Merry 		if ((completed == 0) && (cm->cm_state != MPR_CM_STATE_FREE)) {
1132991554f2SKenneth D. Merry 			/* this should never happen, but if it does, log */
1133991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
1134991554f2SKenneth D. Merry 			    "cm %p state %x flags 0x%x ccb %p during diag "
1135991554f2SKenneth D. Merry 			    "reset\n", cm, cm->cm_state, cm->cm_flags,
1136991554f2SKenneth D. Merry 			    cm->cm_ccb);
1137991554f2SKenneth D. Merry 		}
1138991554f2SKenneth D. Merry 	}
1139f0779b04SScott Long 
1140f0779b04SScott Long 	sc->io_cmds_active = 0;
1141991554f2SKenneth D. Merry }
1142991554f2SKenneth D. Merry 
1143991554f2SKenneth D. Merry void
1144991554f2SKenneth D. Merry mprsas_handle_reinit(struct mpr_softc *sc)
1145991554f2SKenneth D. Merry {
1146991554f2SKenneth D. Merry 	int i;
1147991554f2SKenneth D. Merry 
1148991554f2SKenneth D. Merry 	/* Go back into startup mode and freeze the simq, so that CAM
1149991554f2SKenneth D. Merry 	 * doesn't send any commands until after we've rediscovered all
1150991554f2SKenneth D. Merry 	 * targets and found the proper device handles for them.
1151991554f2SKenneth D. Merry 	 *
1152991554f2SKenneth D. Merry 	 * After the reset, portenable will trigger discovery, and after all
1153991554f2SKenneth D. Merry 	 * discovery-related activities have finished, the simq will be
1154991554f2SKenneth D. Merry 	 * released.
1155991554f2SKenneth D. Merry 	 */
1156991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_INIT, "%s startup\n", __func__);
1157991554f2SKenneth D. Merry 	sc->sassc->flags |= MPRSAS_IN_STARTUP;
1158991554f2SKenneth D. Merry 	sc->sassc->flags |= MPRSAS_IN_DISCOVERY;
1159991554f2SKenneth D. Merry 	mprsas_startup_increment(sc->sassc);
1160991554f2SKenneth D. Merry 
1161991554f2SKenneth D. Merry 	/* notify CAM of a bus reset */
1162991554f2SKenneth D. Merry 	mprsas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1163991554f2SKenneth D. Merry 	    CAM_LUN_WILDCARD);
1164991554f2SKenneth D. Merry 
1165991554f2SKenneth D. Merry 	/* complete and cleanup after all outstanding commands */
1166991554f2SKenneth D. Merry 	mprsas_complete_all_commands(sc);
1167991554f2SKenneth D. Merry 
1168a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INIT, "%s startup %u after command completion\n",
1169a2c14879SStephen McConnell 	    __func__, sc->sassc->startup_refcount);
1170991554f2SKenneth D. Merry 
1171991554f2SKenneth D. Merry 	/* zero all the target handles, since they may change after the
1172991554f2SKenneth D. Merry 	 * reset, and we have to rediscover all the targets and use the new
1173991554f2SKenneth D. Merry 	 * handles.
1174991554f2SKenneth D. Merry 	 */
1175991554f2SKenneth D. Merry 	for (i = 0; i < sc->sassc->maxtargets; i++) {
1176991554f2SKenneth D. Merry 		if (sc->sassc->targets[i].outstanding != 0)
1177991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_INIT, "target %u outstanding %u\n",
1178991554f2SKenneth D. Merry 			    i, sc->sassc->targets[i].outstanding);
1179991554f2SKenneth D. Merry 		sc->sassc->targets[i].handle = 0x0;
1180991554f2SKenneth D. Merry 		sc->sassc->targets[i].exp_dev_handle = 0x0;
1181991554f2SKenneth D. Merry 		sc->sassc->targets[i].outstanding = 0;
1182991554f2SKenneth D. Merry 		sc->sassc->targets[i].flags = MPRSAS_TARGET_INDIAGRESET;
1183991554f2SKenneth D. Merry 	}
1184991554f2SKenneth D. Merry }
1185991554f2SKenneth D. Merry static void
1186991554f2SKenneth D. Merry mprsas_tm_timeout(void *data)
1187991554f2SKenneth D. Merry {
1188991554f2SKenneth D. Merry 	struct mpr_command *tm = data;
1189991554f2SKenneth D. Merry 	struct mpr_softc *sc = tm->cm_sc;
1190991554f2SKenneth D. Merry 
1191991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1192991554f2SKenneth D. Merry 
11937a2a6a1aSStephen McConnell 	mprsas_log_command(tm, MPR_INFO|MPR_RECOVERY, "task mgmt %p timed "
11947a2a6a1aSStephen McConnell 	    "out\n", tm);
1195f0779b04SScott Long 
1196f0779b04SScott Long 	KASSERT(tm->cm_state == MPR_CM_STATE_INQUEUE,
1197f0779b04SScott Long 	    ("command not inqueue\n"));
1198f0779b04SScott Long 
1199f0779b04SScott Long 	tm->cm_state = MPR_CM_STATE_BUSY;
1200991554f2SKenneth D. Merry 	mpr_reinit(sc);
1201991554f2SKenneth D. Merry }
1202991554f2SKenneth D. Merry 
1203991554f2SKenneth D. Merry static void
12047a2a6a1aSStephen McConnell mprsas_logical_unit_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1205991554f2SKenneth D. Merry {
1206991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1207991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1208991554f2SKenneth D. Merry 	unsigned int cm_count = 0;
1209991554f2SKenneth D. Merry 	struct mpr_command *cm;
1210991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1211991554f2SKenneth D. Merry 
1212991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1213991554f2SKenneth D. Merry 
1214991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1215991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1216991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1217991554f2SKenneth D. Merry 
1218991554f2SKenneth D. Merry 	/*
1219991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1220991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1221991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1222991554f2SKenneth D. Merry 	 */
1223991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
12246eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_ERROR,
12256eea4f46SScott Long 		    "%s: cm_flags = %#x for LUN reset! "
1226991554f2SKenneth D. Merry 		    "This should not happen!\n", __func__, tm->cm_flags);
1227991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1228991554f2SKenneth D. Merry 		return;
1229991554f2SKenneth D. Merry 	}
1230991554f2SKenneth D. Merry 
1231991554f2SKenneth D. Merry 	if (reply == NULL) {
12326eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY, "NULL reset reply for tm %p\n",
12336eea4f46SScott Long 		    tm);
1234991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1235991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
12366eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
12376eea4f46SScott Long 			    "reset, ignoring NULL LUN reset reply\n");
1238991554f2SKenneth D. Merry 			targ->tm = NULL;
1239991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1240991554f2SKenneth D. Merry 		}
1241991554f2SKenneth D. Merry 		else {
1242991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
12436eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
12446eea4f46SScott Long 			    "LUN reset attempt, resetting controller\n");
1245991554f2SKenneth D. Merry 			mpr_reinit(sc);
1246991554f2SKenneth D. Merry 		}
1247991554f2SKenneth D. Merry 		return;
1248991554f2SKenneth D. Merry 	}
1249991554f2SKenneth D. Merry 
12506eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1251991554f2SKenneth D. Merry 	    "logical unit reset status 0x%x code 0x%x count %u\n",
1252991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1253991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1254991554f2SKenneth D. Merry 
12556eea4f46SScott Long 	/*
12566eea4f46SScott Long 	 * See if there are any outstanding commands for this LUN.
1257991554f2SKenneth D. Merry 	 * This could be made more efficient by using a per-LU data
1258991554f2SKenneth D. Merry 	 * structure of some sort.
1259991554f2SKenneth D. Merry 	 */
1260991554f2SKenneth D. Merry 	TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1261991554f2SKenneth D. Merry 		if (cm->cm_lun == tm->cm_lun)
1262991554f2SKenneth D. Merry 			cm_count++;
1263991554f2SKenneth D. Merry 	}
1264991554f2SKenneth D. Merry 
1265991554f2SKenneth D. Merry 	if (cm_count == 0) {
12666eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
12676eea4f46SScott Long 		    "Finished recovery after LUN reset for target %u\n",
12686eea4f46SScott Long 		    targ->tid);
1269991554f2SKenneth D. Merry 
12706eea4f46SScott Long 		mprsas_announce_reset(sc, AC_SENT_BDR, targ->tid,
1271991554f2SKenneth D. Merry 		    tm->cm_lun);
1272991554f2SKenneth D. Merry 
12736eea4f46SScott Long 		/*
12746eea4f46SScott Long 		 * We've finished recovery for this logical unit.  check and
1275991554f2SKenneth D. Merry 		 * see if some other logical unit has a timedout command
1276991554f2SKenneth D. Merry 		 * that needs to be processed.
1277991554f2SKenneth D. Merry 		 */
1278991554f2SKenneth D. Merry 		cm = TAILQ_FIRST(&targ->timedout_commands);
1279991554f2SKenneth D. Merry 		if (cm) {
12806eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
12816eea4f46SScott Long 			   "More commands to abort for target %u\n", targ->tid);
1282991554f2SKenneth D. Merry 			mprsas_send_abort(sc, tm, cm);
12836eea4f46SScott Long 		} else {
1284991554f2SKenneth D. Merry 			targ->tm = NULL;
1285991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1286991554f2SKenneth D. Merry 		}
12876eea4f46SScott Long 	} else {
1288991554f2SKenneth D. Merry 		/* if we still have commands for this LUN, the reset
1289991554f2SKenneth D. Merry 		 * effectively failed, regardless of the status reported.
1290991554f2SKenneth D. Merry 		 * Escalate to a target reset.
1291991554f2SKenneth D. Merry 		 */
12926eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
12936eea4f46SScott Long 		    "logical unit reset complete for target %u, but still "
12946eea4f46SScott Long 		    "have %u command(s), sending target reset\n", targ->tid,
12956eea4f46SScott Long 		    cm_count);
129689d1c21fSKashyap D Desai 		if (!targ->is_nvme || sc->custom_nvme_tm_handling)
1297991554f2SKenneth D. Merry 			mprsas_send_reset(sc, tm,
1298991554f2SKenneth D. Merry 			    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
129989d1c21fSKashyap D Desai 		else
130089d1c21fSKashyap D Desai 			mpr_reinit(sc);
1301991554f2SKenneth D. Merry 	}
1302991554f2SKenneth D. Merry }
1303991554f2SKenneth D. Merry 
1304991554f2SKenneth D. Merry static void
1305991554f2SKenneth D. Merry mprsas_target_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1306991554f2SKenneth D. Merry {
1307991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1308991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1309991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1310991554f2SKenneth D. Merry 
1311991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1312991554f2SKenneth D. Merry 
1313991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1314991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1315991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1316991554f2SKenneth D. Merry 
1317991554f2SKenneth D. Merry 	/*
1318991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1319991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1320991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1321991554f2SKenneth D. Merry 	 */
1322991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
1323a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for target "
1324a2c14879SStephen McConnell 		    "reset! This should not happen!\n", __func__, tm->cm_flags);
1325991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1326991554f2SKenneth D. Merry 		return;
1327991554f2SKenneth D. Merry 	}
1328991554f2SKenneth D. Merry 
1329991554f2SKenneth D. Merry 	if (reply == NULL) {
13306eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY,
13316eea4f46SScott Long 		    "NULL target reset reply for tm %p TaskMID %u\n",
13326eea4f46SScott Long 		    tm, le16toh(req->TaskMID));
1333991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1334991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
13356eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
13366eea4f46SScott Long 			    "reset, ignoring NULL target reset reply\n");
1337991554f2SKenneth D. Merry 			targ->tm = NULL;
1338991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
1339991554f2SKenneth D. Merry 		}
1340991554f2SKenneth D. Merry 		else {
1341991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
13426eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
13436eea4f46SScott Long 			    "target reset attempt, resetting controller\n");
1344991554f2SKenneth D. Merry 			mpr_reinit(sc);
1345991554f2SKenneth D. Merry 		}
1346991554f2SKenneth D. Merry 		return;
1347991554f2SKenneth D. Merry 	}
1348991554f2SKenneth D. Merry 
13496eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1350991554f2SKenneth D. Merry 	    "target reset status 0x%x code 0x%x count %u\n",
1351991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1352991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1353991554f2SKenneth D. Merry 
1354991554f2SKenneth D. Merry 	if (targ->outstanding == 0) {
13556eea4f46SScott Long 		/*
13566eea4f46SScott Long 		 * We've finished recovery for this target and all
1357991554f2SKenneth D. Merry 		 * of its logical units.
1358991554f2SKenneth D. Merry 		 */
13596eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
13606eea4f46SScott Long 		    "Finished reset recovery for target %u\n", targ->tid);
1361991554f2SKenneth D. Merry 
1362991554f2SKenneth D. Merry 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1363991554f2SKenneth D. Merry 		    CAM_LUN_WILDCARD);
1364991554f2SKenneth D. Merry 
1365991554f2SKenneth D. Merry 		targ->tm = NULL;
1366991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
13676eea4f46SScott Long 	} else {
13686eea4f46SScott Long 		/*
13696eea4f46SScott Long 		 * After a target reset, if this target still has
1370991554f2SKenneth D. Merry 		 * outstanding commands, the reset effectively failed,
1371991554f2SKenneth D. Merry 		 * regardless of the status reported.  escalate.
1372991554f2SKenneth D. Merry 		 */
13736eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
13746eea4f46SScott Long 		    "Target reset complete for target %u, but still have %u "
13756eea4f46SScott Long 		    "command(s), resetting controller\n", targ->tid,
13766eea4f46SScott Long 		    targ->outstanding);
1377991554f2SKenneth D. Merry 		mpr_reinit(sc);
1378991554f2SKenneth D. Merry 	}
1379991554f2SKenneth D. Merry }
1380991554f2SKenneth D. Merry 
1381991554f2SKenneth D. Merry #define MPR_RESET_TIMEOUT 30
1382991554f2SKenneth D. Merry 
1383a2c14879SStephen McConnell int
1384991554f2SKenneth D. Merry mprsas_send_reset(struct mpr_softc *sc, struct mpr_command *tm, uint8_t type)
1385991554f2SKenneth D. Merry {
1386991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1387991554f2SKenneth D. Merry 	struct mprsas_target *target;
138889d1c21fSKashyap D Desai 	int err, timeout;
1389991554f2SKenneth D. Merry 
1390991554f2SKenneth D. Merry 	target = tm->cm_targ;
1391991554f2SKenneth D. Merry 	if (target->handle == 0) {
1392a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s null devhandle for target_id "
1393a2c14879SStephen McConnell 		    "%d\n", __func__, target->tid);
1394991554f2SKenneth D. Merry 		return -1;
1395991554f2SKenneth D. Merry 	}
1396991554f2SKenneth D. Merry 
1397991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1398991554f2SKenneth D. Merry 	req->DevHandle = htole16(target->handle);
1399991554f2SKenneth D. Merry 	req->TaskType = type;
1400991554f2SKenneth D. Merry 
140189d1c21fSKashyap D Desai 	if (!target->is_nvme || sc->custom_nvme_tm_handling) {
140289d1c21fSKashyap D Desai 		timeout = MPR_RESET_TIMEOUT;
140389d1c21fSKashyap D Desai 		/*
140489d1c21fSKashyap D Desai 		 * Target reset method =
140589d1c21fSKashyap D Desai 		 *     SAS Hard Link Reset / SATA Link Reset
140689d1c21fSKashyap D Desai 		 */
140789d1c21fSKashyap D Desai 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
140889d1c21fSKashyap D Desai 	} else {
140989d1c21fSKashyap D Desai 		timeout = (target->controller_reset_timeout) ? (
141089d1c21fSKashyap D Desai 		    target->controller_reset_timeout) : (MPR_RESET_TIMEOUT);
141189d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
141289d1c21fSKashyap D Desai 		req->MsgFlags =
141389d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
141489d1c21fSKashyap D Desai 	}
141589d1c21fSKashyap D Desai 
1416991554f2SKenneth D. Merry 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1417991554f2SKenneth D. Merry 		/* XXX Need to handle invalid LUNs */
1418991554f2SKenneth D. Merry 		MPR_SET_LUN(req->LUN, tm->cm_lun);
1419991554f2SKenneth D. Merry 		tm->cm_targ->logical_unit_resets++;
14206eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14216eea4f46SScott Long 		    "Sending logical unit reset to target %u lun %d\n",
14226eea4f46SScott Long 		    target->tid, tm->cm_lun);
1423991554f2SKenneth D. Merry 		tm->cm_complete = mprsas_logical_unit_reset_complete;
1424a2c14879SStephen McConnell 		mprsas_prepare_for_tm(sc, tm, target, tm->cm_lun);
14256eea4f46SScott Long 	} else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1426991554f2SKenneth D. Merry 		tm->cm_targ->target_resets++;
14276eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14286eea4f46SScott Long 		    "Sending target reset to target %u\n", target->tid);
1429991554f2SKenneth D. Merry 		tm->cm_complete = mprsas_target_reset_complete;
1430a2c14879SStephen McConnell 		mprsas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1431991554f2SKenneth D. Merry 	}
1432991554f2SKenneth D. Merry 	else {
1433991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "unexpected reset type 0x%x\n", type);
1434991554f2SKenneth D. Merry 		return -1;
1435991554f2SKenneth D. Merry 	}
1436991554f2SKenneth D. Merry 
1437991554f2SKenneth D. Merry 	if (target->encl_level_valid) {
14386eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
14396eea4f46SScott Long 		    "At enclosure level %d, slot %d, connector name (%4s)\n",
14406eea4f46SScott Long 		    target->encl_level, target->encl_slot,
14416eea4f46SScott Long 		    target->connector_name);
1442991554f2SKenneth D. Merry 	}
1443991554f2SKenneth D. Merry 
1444991554f2SKenneth D. Merry 	tm->cm_data = NULL;
1445991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)tm;
1446991554f2SKenneth D. Merry 
144789d1c21fSKashyap D Desai 	callout_reset(&tm->cm_callout, timeout * hz,
1448991554f2SKenneth D. Merry 	    mprsas_tm_timeout, tm);
1449991554f2SKenneth D. Merry 
1450991554f2SKenneth D. Merry 	err = mpr_map_command(sc, tm);
1451991554f2SKenneth D. Merry 	if (err)
14526eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
1453a2c14879SStephen McConnell 		    "error %d sending reset type %u\n", err, type);
1454991554f2SKenneth D. Merry 
1455991554f2SKenneth D. Merry 	return err;
1456991554f2SKenneth D. Merry }
1457991554f2SKenneth D. Merry 
1458991554f2SKenneth D. Merry static void
1459991554f2SKenneth D. Merry mprsas_abort_complete(struct mpr_softc *sc, struct mpr_command *tm)
1460991554f2SKenneth D. Merry {
1461991554f2SKenneth D. Merry 	struct mpr_command *cm;
1462991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1463991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1464991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1465991554f2SKenneth D. Merry 
1466991554f2SKenneth D. Merry 	callout_stop(&tm->cm_callout);
1467991554f2SKenneth D. Merry 
1468991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1469991554f2SKenneth D. Merry 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1470991554f2SKenneth D. Merry 	targ = tm->cm_targ;
1471991554f2SKenneth D. Merry 
1472991554f2SKenneth D. Merry 	/*
1473991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
1474991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
1475991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
1476991554f2SKenneth D. Merry 	 */
1477991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
14786eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_ERROR,
1479991554f2SKenneth D. Merry 		    "cm_flags = %#x for abort %p TaskMID %u!\n",
1480991554f2SKenneth D. Merry 		    tm->cm_flags, tm, le16toh(req->TaskMID));
1481991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
1482991554f2SKenneth D. Merry 		return;
1483991554f2SKenneth D. Merry 	}
1484991554f2SKenneth D. Merry 
1485991554f2SKenneth D. Merry 	if (reply == NULL) {
14866eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY,
1487991554f2SKenneth D. Merry 		    "NULL abort reply for tm %p TaskMID %u\n",
1488991554f2SKenneth D. Merry 		    tm, le16toh(req->TaskMID));
1489991554f2SKenneth D. Merry 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1490991554f2SKenneth D. Merry 			/* this completion was due to a reset, just cleanup */
14916eea4f46SScott Long 			mpr_dprint(sc, MPR_RECOVERY, "Hardware undergoing "
14926eea4f46SScott Long 			    "reset, ignoring NULL abort reply\n");
1493991554f2SKenneth D. Merry 			targ->tm = NULL;
1494991554f2SKenneth D. Merry 			mprsas_free_tm(sc, tm);
14956eea4f46SScott Long 		} else {
1496991554f2SKenneth D. Merry 			/* we should have gotten a reply. */
14976eea4f46SScott Long 			mpr_dprint(sc, MPR_INFO|MPR_RECOVERY, "NULL reply on "
14986eea4f46SScott Long 			    "abort attempt, resetting controller\n");
1499991554f2SKenneth D. Merry 			mpr_reinit(sc);
1500991554f2SKenneth D. Merry 		}
1501991554f2SKenneth D. Merry 		return;
1502991554f2SKenneth D. Merry 	}
1503991554f2SKenneth D. Merry 
15046eea4f46SScott Long 	mpr_dprint(sc, MPR_RECOVERY,
1505991554f2SKenneth D. Merry 	    "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1506991554f2SKenneth D. Merry 	    le16toh(req->TaskMID),
1507991554f2SKenneth D. Merry 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1508991554f2SKenneth D. Merry 	    le32toh(reply->TerminationCount));
1509991554f2SKenneth D. Merry 
1510991554f2SKenneth D. Merry 	cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1511991554f2SKenneth D. Merry 	if (cm == NULL) {
15126eea4f46SScott Long 		/*
15136eea4f46SScott Long 		 * if there are no more timedout commands, we're done with
1514991554f2SKenneth D. Merry 		 * error recovery for this target.
1515991554f2SKenneth D. Merry 		 */
15166eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15176eea4f46SScott Long 		    "Finished abort recovery for target %u\n", targ->tid);
1518991554f2SKenneth D. Merry 		targ->tm = NULL;
1519991554f2SKenneth D. Merry 		mprsas_free_tm(sc, tm);
15206eea4f46SScott Long 	} else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1521991554f2SKenneth D. Merry 		/* abort success, but we have more timedout commands to abort */
15226eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15236eea4f46SScott Long 		    "Continuing abort recovery for target %u\n", targ->tid);
1524991554f2SKenneth D. Merry 		mprsas_send_abort(sc, tm, cm);
15256eea4f46SScott Long 	} else {
15266eea4f46SScott Long 		/*
15276eea4f46SScott Long 		 * we didn't get a command completion, so the abort
1528991554f2SKenneth D. Merry 		 * failed as far as we're concerned.  escalate.
1529991554f2SKenneth D. Merry 		 */
15306eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
15316eea4f46SScott Long 		    "Abort failed for target %u, sending logical unit reset\n",
15326eea4f46SScott Long 		    targ->tid);
1533991554f2SKenneth D. Merry 
1534991554f2SKenneth D. Merry 		mprsas_send_reset(sc, tm,
1535991554f2SKenneth D. Merry 		    MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1536991554f2SKenneth D. Merry 	}
1537991554f2SKenneth D. Merry }
1538991554f2SKenneth D. Merry 
1539991554f2SKenneth D. Merry #define MPR_ABORT_TIMEOUT 5
1540991554f2SKenneth D. Merry 
1541991554f2SKenneth D. Merry static int
1542991554f2SKenneth D. Merry mprsas_send_abort(struct mpr_softc *sc, struct mpr_command *tm,
1543991554f2SKenneth D. Merry     struct mpr_command *cm)
1544991554f2SKenneth D. Merry {
1545991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1546991554f2SKenneth D. Merry 	struct mprsas_target *targ;
154789d1c21fSKashyap D Desai 	int err, timeout;
1548991554f2SKenneth D. Merry 
1549991554f2SKenneth D. Merry 	targ = cm->cm_targ;
1550991554f2SKenneth D. Merry 	if (targ->handle == 0) {
15516eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
15526eea4f46SScott Long 		   "%s null devhandle for target_id %d\n",
1553991554f2SKenneth D. Merry 		    __func__, cm->cm_ccb->ccb_h.target_id);
1554991554f2SKenneth D. Merry 		return -1;
1555991554f2SKenneth D. Merry 	}
1556991554f2SKenneth D. Merry 
1557855fe445SScott Long 	mprsas_log_command(cm, MPR_RECOVERY|MPR_INFO,
1558991554f2SKenneth D. Merry 	    "Aborting command %p\n", cm);
1559991554f2SKenneth D. Merry 
1560991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1561991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
1562991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1563991554f2SKenneth D. Merry 
1564991554f2SKenneth D. Merry 	/* XXX Need to handle invalid LUNs */
1565991554f2SKenneth D. Merry 	MPR_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1566991554f2SKenneth D. Merry 
1567991554f2SKenneth D. Merry 	req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1568991554f2SKenneth D. Merry 
1569991554f2SKenneth D. Merry 	tm->cm_data = NULL;
1570991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_abort_complete;
1571991554f2SKenneth D. Merry 	tm->cm_complete_data = (void *)tm;
1572991554f2SKenneth D. Merry 	tm->cm_targ = cm->cm_targ;
1573991554f2SKenneth D. Merry 	tm->cm_lun = cm->cm_lun;
1574991554f2SKenneth D. Merry 
157589d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling)
157689d1c21fSKashyap D Desai 		timeout	= MPR_ABORT_TIMEOUT;
157789d1c21fSKashyap D Desai 	else
157889d1c21fSKashyap D Desai 		timeout = sc->nvme_abort_timeout;
157989d1c21fSKashyap D Desai 
158089d1c21fSKashyap D Desai 	callout_reset(&tm->cm_callout, timeout * hz,
1581991554f2SKenneth D. Merry 	    mprsas_tm_timeout, tm);
1582991554f2SKenneth D. Merry 
1583991554f2SKenneth D. Merry 	targ->aborts++;
1584991554f2SKenneth D. Merry 
1585a2c14879SStephen McConnell 	mprsas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1586a2c14879SStephen McConnell 
1587991554f2SKenneth D. Merry 	err = mpr_map_command(sc, tm);
1588991554f2SKenneth D. Merry 	if (err)
15896eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
1590991554f2SKenneth D. Merry 		    "error %d sending abort for cm %p SMID %u\n",
1591991554f2SKenneth D. Merry 		    err, cm, req->TaskMID);
1592991554f2SKenneth D. Merry 	return err;
1593991554f2SKenneth D. Merry }
1594991554f2SKenneth D. Merry 
1595991554f2SKenneth D. Merry static void
1596991554f2SKenneth D. Merry mprsas_scsiio_timeout(void *data)
1597991554f2SKenneth D. Merry {
15986eea4f46SScott Long 	sbintime_t elapsed, now;
15996eea4f46SScott Long 	union ccb *ccb;
1600991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1601991554f2SKenneth D. Merry 	struct mpr_command *cm;
1602991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1603991554f2SKenneth D. Merry 
1604991554f2SKenneth D. Merry 	cm = (struct mpr_command *)data;
1605991554f2SKenneth D. Merry 	sc = cm->cm_sc;
16066eea4f46SScott Long 	ccb = cm->cm_ccb;
16076eea4f46SScott Long 	now = sbinuptime();
1608991554f2SKenneth D. Merry 
1609991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1610991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1611991554f2SKenneth D. Merry 
16126eea4f46SScott Long 	mpr_dprint(sc, MPR_XINFO|MPR_RECOVERY, "Timeout checking cm %p\n", cm);
1613991554f2SKenneth D. Merry 
1614991554f2SKenneth D. Merry 	/*
1615991554f2SKenneth D. Merry 	 * Run the interrupt handler to make sure it's not pending.  This
1616991554f2SKenneth D. Merry 	 * isn't perfect because the command could have already completed
1617991554f2SKenneth D. Merry 	 * and been re-used, though this is unlikely.
1618991554f2SKenneth D. Merry 	 */
1619991554f2SKenneth D. Merry 	mpr_intr_locked(sc);
16208fe7bf06SWarner Losh 	if (cm->cm_flags & MPR_CM_FLAGS_ON_RECOVERY) {
1621991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_XINFO,
1622991554f2SKenneth D. Merry 		    "SCSI command %p almost timed out\n", cm);
1623991554f2SKenneth D. Merry 		return;
1624991554f2SKenneth D. Merry 	}
1625991554f2SKenneth D. Merry 
1626991554f2SKenneth D. Merry 	if (cm->cm_ccb == NULL) {
1627991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "command timeout with NULL ccb\n");
1628991554f2SKenneth D. Merry 		return;
1629991554f2SKenneth D. Merry 	}
1630991554f2SKenneth D. Merry 
1631991554f2SKenneth D. Merry 	targ = cm->cm_targ;
1632991554f2SKenneth D. Merry 	targ->timeouts++;
1633991554f2SKenneth D. Merry 
16346eea4f46SScott Long 	elapsed = now - ccb->ccb_h.qos.sim_data;
16356eea4f46SScott Long 	mprsas_log_command(cm, MPR_INFO|MPR_RECOVERY,
16366eea4f46SScott Long 	    "Command timeout on target %u(0x%04x), %d set, %d.%d elapsed\n",
16376eea4f46SScott Long 	    targ->tid, targ->handle, ccb->ccb_h.timeout,
16386eea4f46SScott Long 	    sbintime_getsec(elapsed), elapsed & 0xffffffff);
1639991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
16406eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO|MPR_RECOVERY,
16416eea4f46SScott Long 		    "At enclosure level %d, slot %d, connector name (%4s)\n",
16426eea4f46SScott Long 		    targ->encl_level, targ->encl_slot, targ->connector_name);
1643991554f2SKenneth D. Merry 	}
1644991554f2SKenneth D. Merry 
1645991554f2SKenneth D. Merry 	/* XXX first, check the firmware state, to see if it's still
1646991554f2SKenneth D. Merry 	 * operational.  if not, do a diag reset.
1647991554f2SKenneth D. Merry 	 */
1648a2c14879SStephen McConnell 	mprsas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
16498fe7bf06SWarner Losh 	cm->cm_flags |= MPR_CM_FLAGS_ON_RECOVERY | MPR_CM_FLAGS_TIMEDOUT;
1650991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1651991554f2SKenneth D. Merry 
1652991554f2SKenneth D. Merry 	if (targ->tm != NULL) {
1653991554f2SKenneth D. Merry 		/* target already in recovery, just queue up another
1654991554f2SKenneth D. Merry 		 * timedout command to be processed later.
1655991554f2SKenneth D. Merry 		 */
1656991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_RECOVERY, "queued timedout cm %p for "
1657991554f2SKenneth D. Merry 		    "processing by tm %p\n", cm, targ->tm);
1658991554f2SKenneth D. Merry 	}
1659991554f2SKenneth D. Merry 	else if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1660991554f2SKenneth D. Merry 		/* start recovery by aborting the first timedout command */
16616eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY|MPR_INFO,
16626eea4f46SScott Long 		    "Sending abort to target %u for SMID %d\n", targ->tid,
16636eea4f46SScott Long 		    cm->cm_desc.Default.SMID);
16646eea4f46SScott Long 		mpr_dprint(sc, MPR_RECOVERY, "timedout cm %p allocated tm %p\n",
16656eea4f46SScott Long 		    cm, targ->tm);
1666991554f2SKenneth D. Merry 		mprsas_send_abort(sc, targ->tm, cm);
1667991554f2SKenneth D. Merry 	}
1668991554f2SKenneth D. Merry 	else {
1669991554f2SKenneth D. Merry 		/* XXX queue this target up for recovery once a TM becomes
1670991554f2SKenneth D. Merry 		 * available.  The firmware only has a limited number of
1671991554f2SKenneth D. Merry 		 * HighPriority credits for the high priority requests used
1672991554f2SKenneth D. Merry 		 * for task management, and we ran out.
1673991554f2SKenneth D. Merry 		 *
1674991554f2SKenneth D. Merry 		 * Isilon: don't worry about this for now, since we have
1675991554f2SKenneth D. Merry 		 * more credits than disks in an enclosure, and limit
1676991554f2SKenneth D. Merry 		 * ourselves to one TM per target for recovery.
1677991554f2SKenneth D. Merry 		 */
16786eea4f46SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_RECOVERY,
16796eea4f46SScott Long 		    "timedout cm %p failed to allocate a tm\n", cm);
1680991554f2SKenneth D. Merry 	}
1681991554f2SKenneth D. Merry }
1682991554f2SKenneth D. Merry 
168367feec50SStephen McConnell /**
168467feec50SStephen McConnell  * mprsas_build_nvme_unmap - Build Native NVMe DSM command equivalent
168567feec50SStephen McConnell  *			     to SCSI Unmap.
168667feec50SStephen McConnell  * Return 0 - for success,
168767feec50SStephen McConnell  *	  1 - to immediately return back the command with success status to CAM
168867feec50SStephen McConnell  *	  negative value - to fallback to firmware path i.e. issue scsi unmap
168967feec50SStephen McConnell  *			   to FW without any translation.
169067feec50SStephen McConnell  */
169167feec50SStephen McConnell static int
169267feec50SStephen McConnell mprsas_build_nvme_unmap(struct mpr_softc *sc, struct mpr_command *cm,
169367feec50SStephen McConnell     union ccb *ccb, struct mprsas_target *targ)
169467feec50SStephen McConnell {
169567feec50SStephen McConnell 	Mpi26NVMeEncapsulatedRequest_t *req = NULL;
169667feec50SStephen McConnell 	struct ccb_scsiio *csio;
169767feec50SStephen McConnell 	struct unmap_parm_list *plist;
169867feec50SStephen McConnell 	struct nvme_dsm_range *nvme_dsm_ranges = NULL;
169967feec50SStephen McConnell 	struct nvme_command *c;
170067feec50SStephen McConnell 	int i, res;
170167feec50SStephen McConnell 	uint16_t ndesc, list_len, data_length;
170267feec50SStephen McConnell 	struct mpr_prp_page *prp_page_info;
170367feec50SStephen McConnell 	uint64_t nvme_dsm_ranges_dma_handle;
170467feec50SStephen McConnell 
170567feec50SStephen McConnell 	csio = &ccb->csio;
170667feec50SStephen McConnell 	list_len = (scsiio_cdb_ptr(csio)[7] << 8 | scsiio_cdb_ptr(csio)[8]);
170767feec50SStephen McConnell 	if (!list_len) {
170867feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "Parameter list length is Zero\n");
170967feec50SStephen McConnell 		return -EINVAL;
171067feec50SStephen McConnell 	}
171167feec50SStephen McConnell 
171267feec50SStephen McConnell 	plist = malloc(csio->dxfer_len, M_MPR, M_ZERO|M_NOWAIT);
171367feec50SStephen McConnell 	if (!plist) {
171467feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "Unable to allocate memory to "
171567feec50SStephen McConnell 		    "save UNMAP data\n");
171667feec50SStephen McConnell 		return -ENOMEM;
171767feec50SStephen McConnell 	}
171867feec50SStephen McConnell 
171967feec50SStephen McConnell 	/* Copy SCSI unmap data to a local buffer */
172067feec50SStephen McConnell 	bcopy(csio->data_ptr, plist, csio->dxfer_len);
172167feec50SStephen McConnell 
172267feec50SStephen McConnell 	/* return back the unmap command to CAM with success status,
172367feec50SStephen McConnell 	 * if number of descripts is zero.
172467feec50SStephen McConnell 	 */
172567feec50SStephen McConnell 	ndesc = be16toh(plist->unmap_blk_desc_data_len) >> 4;
172667feec50SStephen McConnell 	if (!ndesc) {
172767feec50SStephen McConnell 		mpr_dprint(sc, MPR_XINFO, "Number of descriptors in "
172867feec50SStephen McConnell 		    "UNMAP cmd is Zero\n");
172967feec50SStephen McConnell 		res = 1;
173067feec50SStephen McConnell 		goto out;
173167feec50SStephen McConnell 	}
173267feec50SStephen McConnell 
173367feec50SStephen McConnell 	data_length = ndesc * sizeof(struct nvme_dsm_range);
173467feec50SStephen McConnell 	if (data_length > targ->MDTS) {
173567feec50SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "data length: %d is greater than "
173667feec50SStephen McConnell 		    "Device's MDTS: %d\n", data_length, targ->MDTS);
173767feec50SStephen McConnell 		res = -EINVAL;
173867feec50SStephen McConnell 		goto out;
173967feec50SStephen McConnell 	}
174067feec50SStephen McConnell 
174167feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
174267feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There is no PRP Page for "
174367feec50SStephen McConnell 	    "UNMAP command.\n", __func__));
174467feec50SStephen McConnell 
174567feec50SStephen McConnell 	/*
174667feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
174767feec50SStephen McConnell 	 * will be freed when the command is freed.
174867feec50SStephen McConnell 	 */
174967feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
175067feec50SStephen McConnell 
175167feec50SStephen McConnell 	nvme_dsm_ranges = (struct nvme_dsm_range *)prp_page_info->prp_page;
175267feec50SStephen McConnell 	nvme_dsm_ranges_dma_handle = prp_page_info->prp_page_busaddr;
175367feec50SStephen McConnell 
175467feec50SStephen McConnell 	bzero(nvme_dsm_ranges, data_length);
175567feec50SStephen McConnell 
175667feec50SStephen McConnell 	/* Convert SCSI unmap's descriptor data to NVMe DSM specific Range data
175767feec50SStephen McConnell 	 * for each descriptors contained in SCSI UNMAP data.
175867feec50SStephen McConnell 	 */
175967feec50SStephen McConnell 	for (i = 0; i < ndesc; i++) {
176067feec50SStephen McConnell 		nvme_dsm_ranges[i].length =
176167feec50SStephen McConnell 		    htole32(be32toh(plist->desc[i].nlb));
176267feec50SStephen McConnell 		nvme_dsm_ranges[i].starting_lba =
176367feec50SStephen McConnell 		    htole64(be64toh(plist->desc[i].slba));
176467feec50SStephen McConnell 		nvme_dsm_ranges[i].attributes = 0;
176567feec50SStephen McConnell 	}
176667feec50SStephen McConnell 
176767feec50SStephen McConnell 	/* Build MPI2.6's NVMe Encapsulated Request Message */
176867feec50SStephen McConnell 	req = (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req;
176967feec50SStephen McConnell 	bzero(req, sizeof(*req));
177067feec50SStephen McConnell 	req->DevHandle = htole16(targ->handle);
177167feec50SStephen McConnell 	req->Function = MPI2_FUNCTION_NVME_ENCAPSULATED;
177267feec50SStephen McConnell 	req->Flags = MPI26_NVME_FLAGS_WRITE;
177367feec50SStephen McConnell 	req->ErrorResponseBaseAddress.High =
177467feec50SStephen McConnell 	    htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32));
177567feec50SStephen McConnell 	req->ErrorResponseBaseAddress.Low =
177667feec50SStephen McConnell 	    htole32(cm->cm_sense_busaddr);
177767feec50SStephen McConnell 	req->ErrorResponseAllocationLength =
177867feec50SStephen McConnell 	    htole16(sizeof(struct nvme_completion));
177967feec50SStephen McConnell 	req->EncapsulatedCommandLength =
178067feec50SStephen McConnell 	    htole16(sizeof(struct nvme_command));
178167feec50SStephen McConnell 	req->DataLength = htole32(data_length);
178267feec50SStephen McConnell 
178367feec50SStephen McConnell 	/* Build NVMe DSM command */
178467feec50SStephen McConnell 	c = (struct nvme_command *) req->NVMe_Command;
17859544e6dcSChuck Tuffli 	c->opc = NVME_OPC_DATASET_MANAGEMENT;
178667feec50SStephen McConnell 	c->nsid = htole32(csio->ccb_h.target_lun + 1);
178767feec50SStephen McConnell 	c->cdw10 = htole32(ndesc - 1);
178867feec50SStephen McConnell 	c->cdw11 = htole32(NVME_DSM_ATTR_DEALLOCATE);
178967feec50SStephen McConnell 
179067feec50SStephen McConnell 	cm->cm_length = data_length;
179167feec50SStephen McConnell 	cm->cm_data = NULL;
179267feec50SStephen McConnell 
179367feec50SStephen McConnell 	cm->cm_complete = mprsas_scsiio_complete;
179467feec50SStephen McConnell 	cm->cm_complete_data = ccb;
179567feec50SStephen McConnell 	cm->cm_targ = targ;
179667feec50SStephen McConnell 	cm->cm_lun = csio->ccb_h.target_lun;
179767feec50SStephen McConnell 	cm->cm_ccb = ccb;
179867feec50SStephen McConnell 
179967feec50SStephen McConnell 	cm->cm_desc.Default.RequestFlags =
180067feec50SStephen McConnell 	    MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
180167feec50SStephen McConnell 
18026eea4f46SScott Long 	csio->ccb_h.qos.sim_data = sbinuptime();
180367feec50SStephen McConnell 	callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
180467feec50SStephen McConnell 	    mprsas_scsiio_timeout, cm, 0);
180567feec50SStephen McConnell 
180667feec50SStephen McConnell 	targ->issued++;
180767feec50SStephen McConnell 	targ->outstanding++;
180867feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
180967feec50SStephen McConnell 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
181067feec50SStephen McConnell 
181167feec50SStephen McConnell 	mprsas_log_command(cm, MPR_XINFO, "%s cm %p ccb %p outstanding %u\n",
181267feec50SStephen McConnell 	    __func__, cm, ccb, targ->outstanding);
181367feec50SStephen McConnell 
181418982e8fSStephen McConnell 	mpr_build_nvme_prp(sc, cm, req,
181518982e8fSStephen McConnell 	    (void *)(uintptr_t)nvme_dsm_ranges_dma_handle, 0, data_length);
181667feec50SStephen McConnell 	mpr_map_command(sc, cm);
181767feec50SStephen McConnell 
181867feec50SStephen McConnell out:
181967feec50SStephen McConnell 	free(plist, M_MPR);
182067feec50SStephen McConnell 	return 0;
182167feec50SStephen McConnell }
182267feec50SStephen McConnell 
1823991554f2SKenneth D. Merry static void
1824991554f2SKenneth D. Merry mprsas_action_scsiio(struct mprsas_softc *sassc, union ccb *ccb)
1825991554f2SKenneth D. Merry {
1826991554f2SKenneth D. Merry 	MPI2_SCSI_IO_REQUEST *req;
1827991554f2SKenneth D. Merry 	struct ccb_scsiio *csio;
1828991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1829991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1830991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1831991554f2SKenneth D. Merry 	struct mpr_command *cm;
183267feec50SStephen McConnell 	uint8_t i, lba_byte, *ref_tag_addr, scsi_opcode;
1833991554f2SKenneth D. Merry 	uint16_t eedp_flags;
1834991554f2SKenneth D. Merry 	uint32_t mpi_control;
183567feec50SStephen McConnell 	int rc;
1836991554f2SKenneth D. Merry 
1837991554f2SKenneth D. Merry 	sc = sassc->sc;
1838991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1839991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1840991554f2SKenneth D. Merry 
1841991554f2SKenneth D. Merry 	csio = &ccb->csio;
1842a2c14879SStephen McConnell 	KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1843a2c14879SStephen McConnell 	    ("Target %d out of bounds in XPT_SCSI_IO\n",
1844a2c14879SStephen McConnell 	     csio->ccb_h.target_id));
1845991554f2SKenneth D. Merry 	targ = &sassc->targets[csio->ccb_h.target_id];
1846991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1847991554f2SKenneth D. Merry 	if (targ->handle == 0x0) {
1848991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s NULL handle for target %u\n",
1849991554f2SKenneth D. Merry 		    __func__, csio->ccb_h.target_id);
1850a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1851991554f2SKenneth D. Merry 		xpt_done(ccb);
1852991554f2SKenneth D. Merry 		return;
1853991554f2SKenneth D. Merry 	}
1854991554f2SKenneth D. Merry 	if (targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT) {
1855a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s Raid component no SCSI IO "
1856991554f2SKenneth D. Merry 		    "supported %u\n", __func__, csio->ccb_h.target_id);
1857a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1858991554f2SKenneth D. Merry 		xpt_done(ccb);
1859991554f2SKenneth D. Merry 		return;
1860991554f2SKenneth D. Merry 	}
1861991554f2SKenneth D. Merry 	/*
1862991554f2SKenneth D. Merry 	 * Sometimes, it is possible to get a command that is not "In
1863991554f2SKenneth D. Merry 	 * Progress" and was actually aborted by the upper layer.  Check for
1864991554f2SKenneth D. Merry 	 * this here and complete the command without error.
1865991554f2SKenneth D. Merry 	 */
1866a2c14879SStephen McConnell 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1867991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE, "%s Command is not in progress for "
1868991554f2SKenneth D. Merry 		    "target %u\n", __func__, csio->ccb_h.target_id);
1869991554f2SKenneth D. Merry 		xpt_done(ccb);
1870991554f2SKenneth D. Merry 		return;
1871991554f2SKenneth D. Merry 	}
1872991554f2SKenneth D. Merry 	/*
1873991554f2SKenneth D. Merry 	 * If devinfo is 0 this will be a volume.  In that case don't tell CAM
1874991554f2SKenneth D. Merry 	 * that the volume has timed out.  We want volumes to be enumerated
18754c1cdd4aSWarner Losh 	 * until they are deleted/removed, not just failed. In either event,
18764c1cdd4aSWarner Losh 	 * we're removing the target due to a firmware event telling us
18774c1cdd4aSWarner Losh 	 * the device is now gone (as opposed to some transient event). Since
18784c1cdd4aSWarner Losh 	 * we're opting to remove failed devices from the OS's view, we need
18794c1cdd4aSWarner Losh 	 * to propagate that status up the stack.
1880991554f2SKenneth D. Merry 	 */
1881991554f2SKenneth D. Merry 	if (targ->flags & MPRSAS_TARGET_INREMOVAL) {
1882991554f2SKenneth D. Merry 		if (targ->devinfo == 0)
1883a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1884991554f2SKenneth D. Merry 		else
18854c1cdd4aSWarner Losh 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1886991554f2SKenneth D. Merry 		xpt_done(ccb);
1887991554f2SKenneth D. Merry 		return;
1888991554f2SKenneth D. Merry 	}
1889991554f2SKenneth D. Merry 
1890991554f2SKenneth D. Merry 	if ((sc->mpr_flags & MPR_FLAGS_SHUTDOWN) != 0) {
1891a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s shutting down\n", __func__);
1892a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1893a2c14879SStephen McConnell 		xpt_done(ccb);
1894a2c14879SStephen McConnell 		return;
1895a2c14879SStephen McConnell 	}
1896a2c14879SStephen McConnell 
1897a2c14879SStephen McConnell 	/*
1898a2c14879SStephen McConnell 	 * If target has a reset in progress, freeze the devq and return.  The
1899a2c14879SStephen McConnell 	 * devq will be released when the TM reset is finished.
1900a2c14879SStephen McConnell 	 */
1901a2c14879SStephen McConnell 	if (targ->flags & MPRSAS_TARGET_INRESET) {
1902a2c14879SStephen McConnell 		ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1903a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s: Freezing devq for target ID %d\n",
1904a2c14879SStephen McConnell 		    __func__, targ->tid);
1905a2c14879SStephen McConnell 		xpt_freeze_devq(ccb->ccb_h.path, 1);
1906991554f2SKenneth D. Merry 		xpt_done(ccb);
1907991554f2SKenneth D. Merry 		return;
1908991554f2SKenneth D. Merry 	}
1909991554f2SKenneth D. Merry 
1910991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
1911991554f2SKenneth D. Merry 	if (cm == NULL || (sc->mpr_flags & MPR_FLAGS_DIAGRESET)) {
1912991554f2SKenneth D. Merry 		if (cm != NULL) {
1913991554f2SKenneth D. Merry 			mpr_free_command(sc, cm);
1914991554f2SKenneth D. Merry 		}
1915991554f2SKenneth D. Merry 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
1916991554f2SKenneth D. Merry 			xpt_freeze_simq(sassc->sim, 1);
1917991554f2SKenneth D. Merry 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
1918991554f2SKenneth D. Merry 		}
1919991554f2SKenneth D. Merry 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1920991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1921991554f2SKenneth D. Merry 		xpt_done(ccb);
1922991554f2SKenneth D. Merry 		return;
1923991554f2SKenneth D. Merry 	}
1924991554f2SKenneth D. Merry 
192567feec50SStephen McConnell 	/* For NVME device's issue UNMAP command directly to NVME drives by
192667feec50SStephen McConnell 	 * constructing equivalent native NVMe DataSetManagement command.
192767feec50SStephen McConnell 	 */
192867feec50SStephen McConnell 	scsi_opcode = scsiio_cdb_ptr(csio)[0];
192967feec50SStephen McConnell 	if (scsi_opcode == UNMAP &&
193067feec50SStephen McConnell 	    targ->is_nvme &&
193167feec50SStephen McConnell 	    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
193267feec50SStephen McConnell 		rc = mprsas_build_nvme_unmap(sc, cm, ccb, targ);
193367feec50SStephen McConnell 		if (rc == 1) { /* return command to CAM with success status */
193467feec50SStephen McConnell 			mpr_free_command(sc, cm);
193567feec50SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
193667feec50SStephen McConnell 			xpt_done(ccb);
193767feec50SStephen McConnell 			return;
193867feec50SStephen McConnell 		} else if (!rc) /* Issued NVMe Encapsulated Request Message */
193967feec50SStephen McConnell 			return;
194067feec50SStephen McConnell 	}
194167feec50SStephen McConnell 
1942991554f2SKenneth D. Merry 	req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1943991554f2SKenneth D. Merry 	bzero(req, sizeof(*req));
1944991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
1945991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1946991554f2SKenneth D. Merry 	req->MsgFlags = 0;
1947991554f2SKenneth D. Merry 	req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1948991554f2SKenneth D. Merry 	req->SenseBufferLength = MPR_SENSE_LEN;
1949991554f2SKenneth D. Merry 	req->SGLFlags = 0;
1950991554f2SKenneth D. Merry 	req->ChainOffset = 0;
1951991554f2SKenneth D. Merry 	req->SGLOffset0 = 24;	/* 32bit word offset to the SGL */
1952991554f2SKenneth D. Merry 	req->SGLOffset1= 0;
1953991554f2SKenneth D. Merry 	req->SGLOffset2= 0;
1954991554f2SKenneth D. Merry 	req->SGLOffset3= 0;
1955991554f2SKenneth D. Merry 	req->SkipCount = 0;
1956991554f2SKenneth D. Merry 	req->DataLength = htole32(csio->dxfer_len);
1957991554f2SKenneth D. Merry 	req->BidirectionalDataLength = 0;
1958991554f2SKenneth D. Merry 	req->IoFlags = htole16(csio->cdb_len);
1959991554f2SKenneth D. Merry 	req->EEDPFlags = 0;
1960991554f2SKenneth D. Merry 
1961991554f2SKenneth D. Merry 	/* Note: BiDirectional transfers are not supported */
1962991554f2SKenneth D. Merry 	switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1963991554f2SKenneth D. Merry 	case CAM_DIR_IN:
1964991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
1965991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
1966991554f2SKenneth D. Merry 		break;
1967991554f2SKenneth D. Merry 	case CAM_DIR_OUT:
1968991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1969991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
1970991554f2SKenneth D. Merry 		break;
1971991554f2SKenneth D. Merry 	case CAM_DIR_NONE:
1972991554f2SKenneth D. Merry 	default:
1973991554f2SKenneth D. Merry 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1974991554f2SKenneth D. Merry 		break;
1975991554f2SKenneth D. Merry 	}
1976991554f2SKenneth D. Merry 
1977991554f2SKenneth D. Merry 	if (csio->cdb_len == 32)
1978991554f2SKenneth D. Merry 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1979991554f2SKenneth D. Merry 	/*
1980991554f2SKenneth D. Merry 	 * It looks like the hardware doesn't require an explicit tag
1981991554f2SKenneth D. Merry 	 * number for each transaction.  SAM Task Management not supported
1982991554f2SKenneth D. Merry 	 * at the moment.
1983991554f2SKenneth D. Merry 	 */
1984991554f2SKenneth D. Merry 	switch (csio->tag_action) {
1985991554f2SKenneth D. Merry 	case MSG_HEAD_OF_Q_TAG:
1986991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1987991554f2SKenneth D. Merry 		break;
1988991554f2SKenneth D. Merry 	case MSG_ORDERED_Q_TAG:
1989991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1990991554f2SKenneth D. Merry 		break;
1991991554f2SKenneth D. Merry 	case MSG_ACA_TASK:
1992991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1993991554f2SKenneth D. Merry 		break;
1994991554f2SKenneth D. Merry 	case CAM_TAG_ACTION_NONE:
1995991554f2SKenneth D. Merry 	case MSG_SIMPLE_Q_TAG:
1996991554f2SKenneth D. Merry 	default:
1997991554f2SKenneth D. Merry 		mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1998991554f2SKenneth D. Merry 		break;
1999991554f2SKenneth D. Merry 	}
200088364968SAlexander Motin 	mpi_control |= (csio->priority << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT) &
200188364968SAlexander Motin 	    MPI2_SCSIIO_CONTROL_CMDPRI_MASK;
2002991554f2SKenneth D. Merry 	mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
2003991554f2SKenneth D. Merry 	req->Control = htole32(mpi_control);
2004991554f2SKenneth D. Merry 
2005991554f2SKenneth D. Merry 	if (MPR_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
2006991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2007a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_LUN_INVALID);
2008991554f2SKenneth D. Merry 		xpt_done(ccb);
2009991554f2SKenneth D. Merry 		return;
2010991554f2SKenneth D. Merry 	}
2011991554f2SKenneth D. Merry 
2012991554f2SKenneth D. Merry 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
2013991554f2SKenneth D. Merry 		bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
2014fa699bb2SAlan Somers 	else {
2015fa699bb2SAlan Somers 		KASSERT(csio->cdb_len <= IOCDBLEN,
201667feec50SStephen McConnell 		    ("cdb_len %d is greater than IOCDBLEN but CAM_CDB_POINTER "
201767feec50SStephen McConnell 		    "is not set", csio->cdb_len));
2018991554f2SKenneth D. Merry 		bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
2019fa699bb2SAlan Somers 	}
2020991554f2SKenneth D. Merry 	req->IoFlags = htole16(csio->cdb_len);
2021991554f2SKenneth D. Merry 
2022991554f2SKenneth D. Merry 	/*
2023991554f2SKenneth D. Merry 	 * Check if EEDP is supported and enabled.  If it is then check if the
2024991554f2SKenneth D. Merry 	 * SCSI opcode could be using EEDP.  If so, make sure the LUN exists and
2025991554f2SKenneth D. Merry 	 * is formatted for EEDP support.  If all of this is true, set CDB up
2026991554f2SKenneth D. Merry 	 * for EEDP transfer.
2027991554f2SKenneth D. Merry 	 */
2028991554f2SKenneth D. Merry 	eedp_flags = op_code_prot[req->CDB.CDB32[0]];
2029991554f2SKenneth D. Merry 	if (sc->eedp_enabled && eedp_flags) {
2030991554f2SKenneth D. Merry 		SLIST_FOREACH(lun, &targ->luns, lun_link) {
2031991554f2SKenneth D. Merry 			if (lun->lun_id == csio->ccb_h.target_lun) {
2032991554f2SKenneth D. Merry 				break;
2033991554f2SKenneth D. Merry 			}
2034991554f2SKenneth D. Merry 		}
2035991554f2SKenneth D. Merry 
2036991554f2SKenneth D. Merry 		if ((lun != NULL) && (lun->eedp_formatted)) {
203771900a79SAlfredo Dal'Ava Junior 			req->EEDPBlockSize = htole32(lun->eedp_block_size);
2038991554f2SKenneth D. Merry 			eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2039991554f2SKenneth D. Merry 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2040991554f2SKenneth D. Merry 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
204167feec50SStephen McConnell 			if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC) {
204267feec50SStephen McConnell 				eedp_flags |=
204367feec50SStephen McConnell 				    MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
204467feec50SStephen McConnell 			}
2045991554f2SKenneth D. Merry 			req->EEDPFlags = htole16(eedp_flags);
2046991554f2SKenneth D. Merry 
2047991554f2SKenneth D. Merry 			/*
2048991554f2SKenneth D. Merry 			 * If CDB less than 32, fill in Primary Ref Tag with
2049991554f2SKenneth D. Merry 			 * low 4 bytes of LBA.  If CDB is 32, tag stuff is
2050991554f2SKenneth D. Merry 			 * already there.  Also, set protection bit.  FreeBSD
2051991554f2SKenneth D. Merry 			 * currently does not support CDBs bigger than 16, but
2052991554f2SKenneth D. Merry 			 * the code doesn't hurt, and will be here for the
2053991554f2SKenneth D. Merry 			 * future.
2054991554f2SKenneth D. Merry 			 */
2055991554f2SKenneth D. Merry 			if (csio->cdb_len != 32) {
2056991554f2SKenneth D. Merry 				lba_byte = (csio->cdb_len == 16) ? 6 : 2;
2057991554f2SKenneth D. Merry 				ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
2058991554f2SKenneth D. Merry 				    PrimaryReferenceTag;
2059991554f2SKenneth D. Merry 				for (i = 0; i < 4; i++) {
2060991554f2SKenneth D. Merry 					*ref_tag_addr =
2061991554f2SKenneth D. Merry 					    req->CDB.CDB32[lba_byte + i];
2062991554f2SKenneth D. Merry 					ref_tag_addr++;
2063991554f2SKenneth D. Merry 				}
2064991554f2SKenneth D. Merry 				req->CDB.EEDP32.PrimaryReferenceTag =
2065991554f2SKenneth D. Merry 				    htole32(req->
2066991554f2SKenneth D. Merry 				    CDB.EEDP32.PrimaryReferenceTag);
2067991554f2SKenneth D. Merry 				req->CDB.EEDP32.PrimaryApplicationTagMask =
2068991554f2SKenneth D. Merry 				    0xFFFF;
20698881681bSKenneth D. Merry 				req->CDB.CDB32[1] =
20708881681bSKenneth D. Merry 				    (req->CDB.CDB32[1] & 0x1F) | 0x20;
2071991554f2SKenneth D. Merry 			} else {
2072991554f2SKenneth D. Merry 				eedp_flags |=
2073991554f2SKenneth D. Merry 				    MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
2074991554f2SKenneth D. Merry 				req->EEDPFlags = htole16(eedp_flags);
2075991554f2SKenneth D. Merry 				req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
2076991554f2SKenneth D. Merry 				    0x1F) | 0x20;
2077991554f2SKenneth D. Merry 			}
2078991554f2SKenneth D. Merry 		}
2079991554f2SKenneth D. Merry 	}
2080991554f2SKenneth D. Merry 
2081991554f2SKenneth D. Merry 	cm->cm_length = csio->dxfer_len;
2082991554f2SKenneth D. Merry 	if (cm->cm_length != 0) {
2083991554f2SKenneth D. Merry 		cm->cm_data = ccb;
2084991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_USE_CCB;
2085991554f2SKenneth D. Merry 	} else {
2086991554f2SKenneth D. Merry 		cm->cm_data = NULL;
2087991554f2SKenneth D. Merry 	}
2088991554f2SKenneth D. Merry 	cm->cm_sge = &req->SGL;
2089991554f2SKenneth D. Merry 	cm->cm_sglsize = (32 - 24) * 4;
2090991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_scsiio_complete;
2091991554f2SKenneth D. Merry 	cm->cm_complete_data = ccb;
2092991554f2SKenneth D. Merry 	cm->cm_targ = targ;
2093991554f2SKenneth D. Merry 	cm->cm_lun = csio->ccb_h.target_lun;
2094991554f2SKenneth D. Merry 	cm->cm_ccb = ccb;
2095991554f2SKenneth D. Merry 	/*
2096991554f2SKenneth D. Merry 	 * If using FP desc type, need to set a bit in IoFlags (SCSI IO is 0)
2097991554f2SKenneth D. Merry 	 * and set descriptor type.
2098991554f2SKenneth D. Merry 	 */
2099991554f2SKenneth D. Merry 	if (targ->scsi_req_desc_type ==
2100991554f2SKenneth D. Merry 	    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
2101991554f2SKenneth D. Merry 		req->IoFlags |= MPI25_SCSIIO_IOFLAGS_FAST_PATH;
2102991554f2SKenneth D. Merry 		cm->cm_desc.FastPathSCSIIO.RequestFlags =
2103991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
210467feec50SStephen McConnell 		if (!sc->atomic_desc_capable) {
210567feec50SStephen McConnell 			cm->cm_desc.FastPathSCSIIO.DevHandle =
210667feec50SStephen McConnell 			    htole16(targ->handle);
210767feec50SStephen McConnell 		}
2108991554f2SKenneth D. Merry 	} else {
2109991554f2SKenneth D. Merry 		cm->cm_desc.SCSIIO.RequestFlags =
2110991554f2SKenneth D. Merry 		    MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
211167feec50SStephen McConnell 		if (!sc->atomic_desc_capable)
2112991554f2SKenneth D. Merry 			cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
2113991554f2SKenneth D. Merry 	}
2114991554f2SKenneth D. Merry 
21156eea4f46SScott Long 	csio->ccb_h.qos.sim_data = sbinuptime();
211685c9dd9dSSteven Hartland 	callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
211785c9dd9dSSteven Hartland 	    mprsas_scsiio_timeout, cm, 0);
2118991554f2SKenneth D. Merry 
2119991554f2SKenneth D. Merry 	targ->issued++;
2120991554f2SKenneth D. Merry 	targ->outstanding++;
2121991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
2122991554f2SKenneth D. Merry 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
2123991554f2SKenneth D. Merry 
2124991554f2SKenneth D. Merry 	mprsas_log_command(cm, MPR_XINFO, "%s cm %p ccb %p outstanding %u\n",
2125991554f2SKenneth D. Merry 	    __func__, cm, ccb, targ->outstanding);
2126991554f2SKenneth D. Merry 
2127991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
2128991554f2SKenneth D. Merry 	return;
2129991554f2SKenneth D. Merry }
2130991554f2SKenneth D. Merry 
2131991554f2SKenneth D. Merry /**
2132991554f2SKenneth D. Merry  * mpr_sc_failed_io_info - translated non-succesfull SCSI_IO request
2133991554f2SKenneth D. Merry  */
2134991554f2SKenneth D. Merry static void
2135991554f2SKenneth D. Merry mpr_sc_failed_io_info(struct mpr_softc *sc, struct ccb_scsiio *csio,
2136991554f2SKenneth D. Merry     Mpi2SCSIIOReply_t *mpi_reply, struct mprsas_target *targ)
2137991554f2SKenneth D. Merry {
2138991554f2SKenneth D. Merry 	u32 response_info;
2139991554f2SKenneth D. Merry 	u8 *response_bytes;
2140991554f2SKenneth D. Merry 	u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
2141991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_MASK;
2142991554f2SKenneth D. Merry 	u8 scsi_state = mpi_reply->SCSIState;
2143991554f2SKenneth D. Merry 	u8 scsi_status = mpi_reply->SCSIStatus;
2144991554f2SKenneth D. Merry 	char *desc_ioc_state = NULL;
2145991554f2SKenneth D. Merry 	char *desc_scsi_status = NULL;
2146991554f2SKenneth D. Merry 	u32 log_info = le32toh(mpi_reply->IOCLogInfo);
2147991554f2SKenneth D. Merry 
2148991554f2SKenneth D. Merry 	if (log_info == 0x31170000)
2149991554f2SKenneth D. Merry 		return;
2150991554f2SKenneth D. Merry 
21512bf620cbSScott Long 	desc_ioc_state = mpr_describe_table(mpr_iocstatus_string,
21522bf620cbSScott Long 	     ioc_status);
21532bf620cbSScott Long 	desc_scsi_status = mpr_describe_table(mpr_scsi_status_string,
21542bf620cbSScott Long 	    scsi_status);
2155991554f2SKenneth D. Merry 
2156991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
2157991554f2SKenneth D. Merry 	    le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
2158991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
2159991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "At enclosure level %d, slot %d, "
2160991554f2SKenneth D. Merry 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
2161991554f2SKenneth D. Merry 		    targ->connector_name);
2162991554f2SKenneth D. Merry 	}
21632bf620cbSScott Long 
21642bf620cbSScott Long 	/*
21652bf620cbSScott Long 	 * We can add more detail about underflow data here
2166991554f2SKenneth D. Merry 	 * TO-DO
21672bf620cbSScott Long 	 */
2168991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "\tscsi_status(%s)(0x%02x), "
21692bf620cbSScott Long 	    "scsi_state %b\n", desc_scsi_status, scsi_status,
21702bf620cbSScott Long 	    scsi_state, "\20" "\1AutosenseValid" "\2AutosenseFailed"
21712bf620cbSScott Long 	    "\3NoScsiStatus" "\4Terminated" "\5Response InfoValid");
2172991554f2SKenneth D. Merry 
2173991554f2SKenneth D. Merry 	if (sc->mpr_debug & MPR_XINFO &&
2174991554f2SKenneth D. Merry 	    scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2175991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : Start :\n");
2176991554f2SKenneth D. Merry 		scsi_sense_print(csio);
2177991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : End :\n");
2178991554f2SKenneth D. Merry 	}
2179991554f2SKenneth D. Merry 
2180991554f2SKenneth D. Merry 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2181991554f2SKenneth D. Merry 		response_info = le32toh(mpi_reply->ResponseInfo);
2182991554f2SKenneth D. Merry 		response_bytes = (u8 *)&response_info;
21832bf620cbSScott Long 		mpr_dprint(sc, MPR_XINFO, "response code(0x%01x): %s\n",
21842bf620cbSScott Long 		    response_bytes[0],
21852bf620cbSScott Long 		    mpr_describe_table(mpr_scsi_taskmgmt_string,
21862bf620cbSScott Long 		    response_bytes[0]));
2187991554f2SKenneth D. Merry 	}
2188991554f2SKenneth D. Merry }
2189991554f2SKenneth D. Merry 
219067feec50SStephen McConnell /** mprsas_nvme_trans_status_code
219167feec50SStephen McConnell  *
219267feec50SStephen McConnell  * Convert Native NVMe command error status to
219367feec50SStephen McConnell  * equivalent SCSI error status.
219467feec50SStephen McConnell  *
219567feec50SStephen McConnell  * Returns appropriate scsi_status
219667feec50SStephen McConnell  */
219767feec50SStephen McConnell static u8
21980d787e9bSWojciech Macek mprsas_nvme_trans_status_code(uint16_t nvme_status,
219967feec50SStephen McConnell     struct mpr_command *cm)
220067feec50SStephen McConnell {
220167feec50SStephen McConnell 	u8 status = MPI2_SCSI_STATUS_GOOD;
220267feec50SStephen McConnell 	int skey, asc, ascq;
220367feec50SStephen McConnell 	union ccb *ccb = cm->cm_complete_data;
220467feec50SStephen McConnell 	int returned_sense_len;
22050d787e9bSWojciech Macek 	uint8_t sct, sc;
22060d787e9bSWojciech Macek 
22070d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(nvme_status);
22080d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(nvme_status);
220967feec50SStephen McConnell 
221067feec50SStephen McConnell 	status = MPI2_SCSI_STATUS_CHECK_CONDITION;
221167feec50SStephen McConnell 	skey = SSD_KEY_ILLEGAL_REQUEST;
221267feec50SStephen McConnell 	asc = SCSI_ASC_NO_SENSE;
221367feec50SStephen McConnell 	ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
221467feec50SStephen McConnell 
22150d787e9bSWojciech Macek 	switch (sct) {
221667feec50SStephen McConnell 	case NVME_SCT_GENERIC:
22170d787e9bSWojciech Macek 		switch (sc) {
221867feec50SStephen McConnell 		case NVME_SC_SUCCESS:
221967feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_GOOD;
222067feec50SStephen McConnell 			skey = SSD_KEY_NO_SENSE;
222167feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
222267feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
222367feec50SStephen McConnell 			break;
222467feec50SStephen McConnell 		case NVME_SC_INVALID_OPCODE:
222567feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
222667feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
222767feec50SStephen McConnell 			asc = SCSI_ASC_ILLEGAL_COMMAND;
222867feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
222967feec50SStephen McConnell 			break;
223067feec50SStephen McConnell 		case NVME_SC_INVALID_FIELD:
223167feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
223267feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
223367feec50SStephen McConnell 			asc = SCSI_ASC_INVALID_CDB;
223467feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
223567feec50SStephen McConnell 			break;
223667feec50SStephen McConnell 		case NVME_SC_DATA_TRANSFER_ERROR:
223767feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
223867feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
223967feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
224067feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
224167feec50SStephen McConnell 			break;
224267feec50SStephen McConnell 		case NVME_SC_ABORTED_POWER_LOSS:
224367feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_TASK_ABORTED;
224467feec50SStephen McConnell 			skey = SSD_KEY_ABORTED_COMMAND;
224567feec50SStephen McConnell 			asc = SCSI_ASC_WARNING;
224667feec50SStephen McConnell 			ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED;
224767feec50SStephen McConnell 			break;
224867feec50SStephen McConnell 		case NVME_SC_INTERNAL_DEVICE_ERROR:
224967feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
225067feec50SStephen McConnell 			skey = SSD_KEY_HARDWARE_ERROR;
225167feec50SStephen McConnell 			asc = SCSI_ASC_INTERNAL_TARGET_FAILURE;
225267feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
225367feec50SStephen McConnell 			break;
225467feec50SStephen McConnell 		case NVME_SC_ABORTED_BY_REQUEST:
225567feec50SStephen McConnell 		case NVME_SC_ABORTED_SQ_DELETION:
225667feec50SStephen McConnell 		case NVME_SC_ABORTED_FAILED_FUSED:
225767feec50SStephen McConnell 		case NVME_SC_ABORTED_MISSING_FUSED:
225867feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_TASK_ABORTED;
225967feec50SStephen McConnell 			skey = SSD_KEY_ABORTED_COMMAND;
226067feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
226167feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
226267feec50SStephen McConnell 			break;
226367feec50SStephen McConnell 		case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
226467feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
226567feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
226667feec50SStephen McConnell 			asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
226767feec50SStephen McConnell 			ascq = SCSI_ASCQ_INVALID_LUN_ID;
226867feec50SStephen McConnell 			break;
226967feec50SStephen McConnell 		case NVME_SC_LBA_OUT_OF_RANGE:
227067feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
227167feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
227267feec50SStephen McConnell 			asc = SCSI_ASC_ILLEGAL_BLOCK;
227367feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
227467feec50SStephen McConnell 			break;
227567feec50SStephen McConnell 		case NVME_SC_CAPACITY_EXCEEDED:
227667feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
227767feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
227867feec50SStephen McConnell 			asc = SCSI_ASC_NO_SENSE;
227967feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
228067feec50SStephen McConnell 			break;
228167feec50SStephen McConnell 		case NVME_SC_NAMESPACE_NOT_READY:
228267feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
228367feec50SStephen McConnell 			skey = SSD_KEY_NOT_READY;
228467feec50SStephen McConnell 			asc = SCSI_ASC_LUN_NOT_READY;
228567feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
228667feec50SStephen McConnell 			break;
228767feec50SStephen McConnell 		}
228867feec50SStephen McConnell 		break;
228967feec50SStephen McConnell 	case NVME_SCT_COMMAND_SPECIFIC:
22900d787e9bSWojciech Macek 		switch (sc) {
229167feec50SStephen McConnell 		case NVME_SC_INVALID_FORMAT:
229267feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
229367feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
229467feec50SStephen McConnell 			asc = SCSI_ASC_FORMAT_COMMAND_FAILED;
229567feec50SStephen McConnell 			ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED;
229667feec50SStephen McConnell 			break;
229767feec50SStephen McConnell 		case NVME_SC_CONFLICTING_ATTRIBUTES:
229867feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
229967feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
230067feec50SStephen McConnell 			asc = SCSI_ASC_INVALID_CDB;
230167feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
230267feec50SStephen McConnell 			break;
230367feec50SStephen McConnell 		}
230467feec50SStephen McConnell 		break;
230567feec50SStephen McConnell 	case NVME_SCT_MEDIA_ERROR:
23060d787e9bSWojciech Macek 		switch (sc) {
230767feec50SStephen McConnell 		case NVME_SC_WRITE_FAULTS:
230867feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
230967feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
231067feec50SStephen McConnell 			asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT;
231167feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
231267feec50SStephen McConnell 			break;
231367feec50SStephen McConnell 		case NVME_SC_UNRECOVERED_READ_ERROR:
231467feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
231567feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
231667feec50SStephen McConnell 			asc = SCSI_ASC_UNRECOVERED_READ_ERROR;
231767feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
231867feec50SStephen McConnell 			break;
231967feec50SStephen McConnell 		case NVME_SC_GUARD_CHECK_ERROR:
232067feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
232167feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
232267feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED;
232367feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED;
232467feec50SStephen McConnell 			break;
232567feec50SStephen McConnell 		case NVME_SC_APPLICATION_TAG_CHECK_ERROR:
232667feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
232767feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
232867feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED;
232967feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED;
233067feec50SStephen McConnell 			break;
233167feec50SStephen McConnell 		case NVME_SC_REFERENCE_TAG_CHECK_ERROR:
233267feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
233367feec50SStephen McConnell 			skey = SSD_KEY_MEDIUM_ERROR;
233467feec50SStephen McConnell 			asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED;
233567feec50SStephen McConnell 			ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED;
233667feec50SStephen McConnell 			break;
233767feec50SStephen McConnell 		case NVME_SC_COMPARE_FAILURE:
233867feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
233967feec50SStephen McConnell 			skey = SSD_KEY_MISCOMPARE;
234067feec50SStephen McConnell 			asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY;
234167feec50SStephen McConnell 			ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
234267feec50SStephen McConnell 			break;
234367feec50SStephen McConnell 		case NVME_SC_ACCESS_DENIED:
234467feec50SStephen McConnell 			status = MPI2_SCSI_STATUS_CHECK_CONDITION;
234567feec50SStephen McConnell 			skey = SSD_KEY_ILLEGAL_REQUEST;
234667feec50SStephen McConnell 			asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
234767feec50SStephen McConnell 			ascq = SCSI_ASCQ_INVALID_LUN_ID;
234867feec50SStephen McConnell 			break;
234967feec50SStephen McConnell 		}
235067feec50SStephen McConnell 		break;
235167feec50SStephen McConnell 	}
235267feec50SStephen McConnell 
235367feec50SStephen McConnell 	returned_sense_len = sizeof(struct scsi_sense_data);
235467feec50SStephen McConnell 	if (returned_sense_len < ccb->csio.sense_len)
235567feec50SStephen McConnell 		ccb->csio.sense_resid = ccb->csio.sense_len -
235667feec50SStephen McConnell 		    returned_sense_len;
235767feec50SStephen McConnell 	else
235867feec50SStephen McConnell 		ccb->csio.sense_resid = 0;
235967feec50SStephen McConnell 
236067feec50SStephen McConnell 	scsi_set_sense_data(&ccb->csio.sense_data, SSD_TYPE_FIXED,
236167feec50SStephen McConnell 	    1, skey, asc, ascq, SSD_ELEM_NONE);
236267feec50SStephen McConnell 	ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
236367feec50SStephen McConnell 
236467feec50SStephen McConnell 	return status;
236567feec50SStephen McConnell }
236667feec50SStephen McConnell 
236767feec50SStephen McConnell /** mprsas_complete_nvme_unmap
236867feec50SStephen McConnell  *
236967feec50SStephen McConnell  * Complete native NVMe command issued using NVMe Encapsulated
237067feec50SStephen McConnell  * Request Message.
237167feec50SStephen McConnell  */
237267feec50SStephen McConnell static u8
237367feec50SStephen McConnell mprsas_complete_nvme_unmap(struct mpr_softc *sc, struct mpr_command *cm)
237467feec50SStephen McConnell {
237567feec50SStephen McConnell 	Mpi26NVMeEncapsulatedErrorReply_t *mpi_reply;
237667feec50SStephen McConnell 	struct nvme_completion *nvme_completion = NULL;
237767feec50SStephen McConnell 	u8 scsi_status = MPI2_SCSI_STATUS_GOOD;
237867feec50SStephen McConnell 
237967feec50SStephen McConnell 	mpi_reply =(Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply;
238067feec50SStephen McConnell 	if (le16toh(mpi_reply->ErrorResponseCount)){
238167feec50SStephen McConnell 		nvme_completion = (struct nvme_completion *)cm->cm_sense;
238267feec50SStephen McConnell 		scsi_status = mprsas_nvme_trans_status_code(
238367feec50SStephen McConnell 		    nvme_completion->status, cm);
238467feec50SStephen McConnell 	}
238567feec50SStephen McConnell 	return scsi_status;
238667feec50SStephen McConnell }
238767feec50SStephen McConnell 
2388991554f2SKenneth D. Merry static void
2389991554f2SKenneth D. Merry mprsas_scsiio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2390991554f2SKenneth D. Merry {
2391991554f2SKenneth D. Merry 	MPI2_SCSI_IO_REPLY *rep;
2392991554f2SKenneth D. Merry 	union ccb *ccb;
2393991554f2SKenneth D. Merry 	struct ccb_scsiio *csio;
2394991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
2395991554f2SKenneth D. Merry 	struct scsi_vpd_supported_page_list *vpd_list = NULL;
239667feec50SStephen McConnell 	u8 *TLR_bits, TLR_on, *scsi_cdb;
2397991554f2SKenneth D. Merry 	int dir = 0, i;
2398991554f2SKenneth D. Merry 	u16 alloc_len;
2399a2c14879SStephen McConnell 	struct mprsas_target *target;
2400a2c14879SStephen McConnell 	target_id_t target_id;
2401991554f2SKenneth D. Merry 
2402991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
2403991554f2SKenneth D. Merry 
2404991554f2SKenneth D. Merry 	callout_stop(&cm->cm_callout);
2405991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
2406991554f2SKenneth D. Merry 
2407991554f2SKenneth D. Merry 	sassc = sc->sassc;
2408991554f2SKenneth D. Merry 	ccb = cm->cm_complete_data;
2409991554f2SKenneth D. Merry 	csio = &ccb->csio;
2410a2c14879SStephen McConnell 	target_id = csio->ccb_h.target_id;
2411991554f2SKenneth D. Merry 	rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
241271900a79SAlfredo Dal'Ava Junior 	mpr_dprint(sc, MPR_TRACE,
241371900a79SAlfredo Dal'Ava Junior 	    "cm %p SMID %u ccb %p reply %p outstanding %u csio->scsi_status 0x%x,"
241471900a79SAlfredo Dal'Ava Junior 	    "csio->dxfer_len 0x%x, csio->msg_le 0x%xn\n", cm,
241571900a79SAlfredo Dal'Ava Junior 	    cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
241671900a79SAlfredo Dal'Ava Junior 	    cm->cm_targ->outstanding, csio->scsi_status,
241771900a79SAlfredo Dal'Ava Junior 	    csio->dxfer_len, csio->msg_len);
2418991554f2SKenneth D. Merry 	/*
2419991554f2SKenneth D. Merry 	 * XXX KDM if the chain allocation fails, does it matter if we do
2420991554f2SKenneth D. Merry 	 * the sync and unload here?  It is simpler to do it in every case,
2421991554f2SKenneth D. Merry 	 * assuming it doesn't cause problems.
2422991554f2SKenneth D. Merry 	 */
2423991554f2SKenneth D. Merry 	if (cm->cm_data != NULL) {
2424991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
2425991554f2SKenneth D. Merry 			dir = BUS_DMASYNC_POSTREAD;
2426991554f2SKenneth D. Merry 		else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
2427991554f2SKenneth D. Merry 			dir = BUS_DMASYNC_POSTWRITE;
2428991554f2SKenneth D. Merry 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2429991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2430991554f2SKenneth D. Merry 	}
2431991554f2SKenneth D. Merry 
2432991554f2SKenneth D. Merry 	cm->cm_targ->completed++;
2433991554f2SKenneth D. Merry 	cm->cm_targ->outstanding--;
2434991554f2SKenneth D. Merry 	TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2435991554f2SKenneth D. Merry 	ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2436991554f2SKenneth D. Merry 
24378fe7bf06SWarner Losh 	if (cm->cm_flags & MPR_CM_FLAGS_ON_RECOVERY) {
2438991554f2SKenneth D. Merry 		TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
24398fe7bf06SWarner Losh 		KASSERT(cm->cm_state == MPR_CM_STATE_BUSY,
24408fe7bf06SWarner Losh 		    ("Not busy for CM_FLAGS_TIMEDOUT: %d\n", cm->cm_state));
24418fe7bf06SWarner Losh 		cm->cm_flags &= ~MPR_CM_FLAGS_ON_RECOVERY;
2442991554f2SKenneth D. Merry 		if (cm->cm_reply != NULL)
2443991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2444991554f2SKenneth D. Merry 			    "completed timedout cm %p ccb %p during recovery "
2445991554f2SKenneth D. Merry 			    "ioc %x scsi %x state %x xfer %u\n", cm, cm->cm_ccb,
2446991554f2SKenneth D. Merry 			    le16toh(rep->IOCStatus), rep->SCSIStatus,
2447991554f2SKenneth D. Merry 			    rep->SCSIState, le32toh(rep->TransferCount));
2448991554f2SKenneth D. Merry 		else
2449991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2450991554f2SKenneth D. Merry 			    "completed timedout cm %p ccb %p during recovery\n",
2451991554f2SKenneth D. Merry 			    cm, cm->cm_ccb);
2452991554f2SKenneth D. Merry 	} else if (cm->cm_targ->tm != NULL) {
2453991554f2SKenneth D. Merry 		if (cm->cm_reply != NULL)
2454991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2455991554f2SKenneth D. Merry 			    "completed cm %p ccb %p during recovery "
2456991554f2SKenneth D. Merry 			    "ioc %x scsi %x state %x xfer %u\n",
2457991554f2SKenneth D. Merry 			    cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2458991554f2SKenneth D. Merry 			    rep->SCSIStatus, rep->SCSIState,
2459991554f2SKenneth D. Merry 			    le32toh(rep->TransferCount));
2460991554f2SKenneth D. Merry 		else
2461991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_RECOVERY,
2462991554f2SKenneth D. Merry 			    "completed cm %p ccb %p during recovery\n",
2463991554f2SKenneth D. Merry 			    cm, cm->cm_ccb);
2464991554f2SKenneth D. Merry 	} else if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
2465991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_RECOVERY,
2466991554f2SKenneth D. Merry 		    "reset completed cm %p ccb %p\n", cm, cm->cm_ccb);
2467991554f2SKenneth D. Merry 	}
2468991554f2SKenneth D. Merry 
2469991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2470991554f2SKenneth D. Merry 		/*
2471991554f2SKenneth D. Merry 		 * We ran into an error after we tried to map the command,
2472991554f2SKenneth D. Merry 		 * so we're getting a callback without queueing the command
2473991554f2SKenneth D. Merry 		 * to the hardware.  So we set the status here, and it will
2474991554f2SKenneth D. Merry 		 * be retained below.  We'll go through the "fast path",
2475991554f2SKenneth D. Merry 		 * because there can be no reply when we haven't actually
2476991554f2SKenneth D. Merry 		 * gone out to the hardware.
2477991554f2SKenneth D. Merry 		 */
2478a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2479991554f2SKenneth D. Merry 
2480991554f2SKenneth D. Merry 		/*
2481991554f2SKenneth D. Merry 		 * Currently the only error included in the mask is
2482991554f2SKenneth D. Merry 		 * MPR_CM_FLAGS_CHAIN_FAILED, which means we're out of
2483991554f2SKenneth D. Merry 		 * chain frames.  We need to freeze the queue until we get
2484991554f2SKenneth D. Merry 		 * a command that completed without this error, which will
2485991554f2SKenneth D. Merry 		 * hopefully have some chain frames attached that we can
2486991554f2SKenneth D. Merry 		 * use.  If we wanted to get smarter about it, we would
2487991554f2SKenneth D. Merry 		 * only unfreeze the queue in this condition when we're
2488991554f2SKenneth D. Merry 		 * sure that we're getting some chain frames back.  That's
2489991554f2SKenneth D. Merry 		 * probably unnecessary.
2490991554f2SKenneth D. Merry 		 */
2491991554f2SKenneth D. Merry 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
2492991554f2SKenneth D. Merry 			xpt_freeze_simq(sassc->sim, 1);
2493991554f2SKenneth D. Merry 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
24946c85e33eSScott Long 			mpr_dprint(sc, MPR_XINFO, "Error sending command, "
2495991554f2SKenneth D. Merry 			    "freezing SIM queue\n");
2496991554f2SKenneth D. Merry 		}
2497991554f2SKenneth D. Merry 	}
2498991554f2SKenneth D. Merry 
2499991554f2SKenneth D. Merry 	/*
250067feec50SStephen McConnell 	 * Point to the SCSI CDB, which is dependent on the CAM_CDB_POINTER
250167feec50SStephen McConnell 	 * flag, and use it in a few places in the rest of this function for
250267feec50SStephen McConnell 	 * convenience. Use the macro if available.
250367feec50SStephen McConnell 	 */
250467feec50SStephen McConnell 	scsi_cdb = scsiio_cdb_ptr(csio);
250567feec50SStephen McConnell 
250667feec50SStephen McConnell 	/*
2507991554f2SKenneth D. Merry 	 * If this is a Start Stop Unit command and it was issued by the driver
2508991554f2SKenneth D. Merry 	 * during shutdown, decrement the refcount to account for all of the
2509991554f2SKenneth D. Merry 	 * commands that were sent.  All SSU commands should be completed before
2510991554f2SKenneth D. Merry 	 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2511991554f2SKenneth D. Merry 	 * is TRUE.
2512991554f2SKenneth D. Merry 	 */
251367feec50SStephen McConnell 	if (sc->SSU_started && (scsi_cdb[0] == START_STOP_UNIT)) {
2514991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INFO, "Decrementing SSU count.\n");
2515991554f2SKenneth D. Merry 		sc->SSU_refcount--;
2516991554f2SKenneth D. Merry 	}
2517991554f2SKenneth D. Merry 
2518991554f2SKenneth D. Merry 	/* Take the fast path to completion */
2519991554f2SKenneth D. Merry 	if (cm->cm_reply == NULL) {
2520a2c14879SStephen McConnell 		if (mprsas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2521991554f2SKenneth D. Merry 			if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0)
2522a2c14879SStephen McConnell 				mprsas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2523991554f2SKenneth D. Merry 			else {
2524a2c14879SStephen McConnell 				mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2525a2c14879SStephen McConnell 				csio->scsi_status = SCSI_STATUS_OK;
2526991554f2SKenneth D. Merry 			}
2527991554f2SKenneth D. Merry 			if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2528991554f2SKenneth D. Merry 				ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2529991554f2SKenneth D. Merry 				sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2530991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_XINFO,
2531991554f2SKenneth D. Merry 				    "Unfreezing SIM queue\n");
2532991554f2SKenneth D. Merry 			}
2533991554f2SKenneth D. Merry 		}
2534991554f2SKenneth D. Merry 
2535991554f2SKenneth D. Merry 		/*
2536991554f2SKenneth D. Merry 		 * There are two scenarios where the status won't be
2537991554f2SKenneth D. Merry 		 * CAM_REQ_CMP.  The first is if MPR_CM_FLAGS_ERROR_MASK is
2538991554f2SKenneth D. Merry 		 * set, the second is in the MPR_FLAGS_DIAGRESET above.
2539991554f2SKenneth D. Merry 		 */
2540a2c14879SStephen McConnell 		if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2541991554f2SKenneth D. Merry 			/*
2542991554f2SKenneth D. Merry 			 * Freeze the dev queue so that commands are
2543a2c14879SStephen McConnell 			 * executed in the correct order after error
2544991554f2SKenneth D. Merry 			 * recovery.
2545991554f2SKenneth D. Merry 			 */
2546991554f2SKenneth D. Merry 			ccb->ccb_h.status |= CAM_DEV_QFRZN;
2547991554f2SKenneth D. Merry 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2548991554f2SKenneth D. Merry 		}
2549991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2550991554f2SKenneth D. Merry 		xpt_done(ccb);
2551991554f2SKenneth D. Merry 		return;
2552991554f2SKenneth D. Merry 	}
2553991554f2SKenneth D. Merry 
255467feec50SStephen McConnell 	target = &sassc->targets[target_id];
255567feec50SStephen McConnell 	if (scsi_cdb[0] == UNMAP &&
255667feec50SStephen McConnell 	    target->is_nvme &&
255767feec50SStephen McConnell 	    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
255867feec50SStephen McConnell 		rep->SCSIStatus = mprsas_complete_nvme_unmap(sc, cm);
255967feec50SStephen McConnell 		csio->scsi_status = rep->SCSIStatus;
256067feec50SStephen McConnell 	}
256167feec50SStephen McConnell 
2562991554f2SKenneth D. Merry 	mprsas_log_command(cm, MPR_XINFO,
2563991554f2SKenneth D. Merry 	    "ioc %x scsi %x state %x xfer %u\n",
2564991554f2SKenneth D. Merry 	    le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2565991554f2SKenneth D. Merry 	    le32toh(rep->TransferCount));
2566991554f2SKenneth D. Merry 
2567991554f2SKenneth D. Merry 	switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2568991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2569991554f2SKenneth D. Merry 		csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2570991554f2SKenneth D. Merry 		/* FALLTHROUGH */
2571991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SUCCESS:
2572991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2573991554f2SKenneth D. Merry 		if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2574991554f2SKenneth D. Merry 		    MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2575991554f2SKenneth D. Merry 			mprsas_log_command(cm, MPR_XINFO, "recovered error\n");
2576991554f2SKenneth D. Merry 
2577991554f2SKenneth D. Merry 		/* Completion failed at the transport level. */
2578991554f2SKenneth D. Merry 		if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2579991554f2SKenneth D. Merry 		    MPI2_SCSI_STATE_TERMINATED)) {
2580a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2581991554f2SKenneth D. Merry 			break;
2582991554f2SKenneth D. Merry 		}
2583991554f2SKenneth D. Merry 
2584991554f2SKenneth D. Merry 		/* In a modern packetized environment, an autosense failure
2585991554f2SKenneth D. Merry 		 * implies that there's not much else that can be done to
2586991554f2SKenneth D. Merry 		 * recover the command.
2587991554f2SKenneth D. Merry 		 */
2588991554f2SKenneth D. Merry 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2589a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2590991554f2SKenneth D. Merry 			break;
2591991554f2SKenneth D. Merry 		}
2592991554f2SKenneth D. Merry 
2593991554f2SKenneth D. Merry 		/*
2594991554f2SKenneth D. Merry 		 * CAM doesn't care about SAS Response Info data, but if this is
2595991554f2SKenneth D. Merry 		 * the state check if TLR should be done.  If not, clear the
2596991554f2SKenneth D. Merry 		 * TLR_bits for the target.
2597991554f2SKenneth D. Merry 		 */
2598991554f2SKenneth D. Merry 		if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2599991554f2SKenneth D. Merry 		    ((le32toh(rep->ResponseInfo) & MPI2_SCSI_RI_MASK_REASONCODE)
2600991554f2SKenneth D. Merry 		    == MPR_SCSI_RI_INVALID_FRAME)) {
2601a2c14879SStephen McConnell 			sc->mapping_table[target_id].TLR_bits =
2602991554f2SKenneth D. Merry 			    (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2603991554f2SKenneth D. Merry 		}
2604991554f2SKenneth D. Merry 
2605991554f2SKenneth D. Merry 		/*
2606991554f2SKenneth D. Merry 		 * Intentionally override the normal SCSI status reporting
2607991554f2SKenneth D. Merry 		 * for these two cases.  These are likely to happen in a
2608991554f2SKenneth D. Merry 		 * multi-initiator environment, and we want to make sure that
2609991554f2SKenneth D. Merry 		 * CAM retries these commands rather than fail them.
2610991554f2SKenneth D. Merry 		 */
2611991554f2SKenneth D. Merry 		if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2612991554f2SKenneth D. Merry 		    (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2613a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2614991554f2SKenneth D. Merry 			break;
2615991554f2SKenneth D. Merry 		}
2616991554f2SKenneth D. Merry 
2617991554f2SKenneth D. Merry 		/* Handle normal status and sense */
2618991554f2SKenneth D. Merry 		csio->scsi_status = rep->SCSIStatus;
2619991554f2SKenneth D. Merry 		if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2620a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2621991554f2SKenneth D. Merry 		else
2622a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2623991554f2SKenneth D. Merry 
2624991554f2SKenneth D. Merry 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2625991554f2SKenneth D. Merry 			int sense_len, returned_sense_len;
2626991554f2SKenneth D. Merry 
2627991554f2SKenneth D. Merry 			returned_sense_len = min(le32toh(rep->SenseCount),
2628991554f2SKenneth D. Merry 			    sizeof(struct scsi_sense_data));
2629991554f2SKenneth D. Merry 			if (returned_sense_len < csio->sense_len)
2630991554f2SKenneth D. Merry 				csio->sense_resid = csio->sense_len -
2631991554f2SKenneth D. Merry 				    returned_sense_len;
2632991554f2SKenneth D. Merry 			else
2633991554f2SKenneth D. Merry 				csio->sense_resid = 0;
2634991554f2SKenneth D. Merry 
2635991554f2SKenneth D. Merry 			sense_len = min(returned_sense_len,
2636991554f2SKenneth D. Merry 			    csio->sense_len - csio->sense_resid);
2637991554f2SKenneth D. Merry 			bzero(&csio->sense_data, sizeof(csio->sense_data));
2638991554f2SKenneth D. Merry 			bcopy(cm->cm_sense, &csio->sense_data, sense_len);
2639991554f2SKenneth D. Merry 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2640991554f2SKenneth D. Merry 		}
2641991554f2SKenneth D. Merry 
2642991554f2SKenneth D. Merry 		/*
2643991554f2SKenneth D. Merry 		 * Check if this is an INQUIRY command.  If it's a VPD inquiry,
2644991554f2SKenneth D. Merry 		 * and it's page code 0 (Supported Page List), and there is
2645991554f2SKenneth D. Merry 		 * inquiry data, and this is for a sequential access device, and
2646991554f2SKenneth D. Merry 		 * the device is an SSP target, and TLR is supported by the
2647991554f2SKenneth D. Merry 		 * controller, turn the TLR_bits value ON if page 0x90 is
2648991554f2SKenneth D. Merry 		 * supported.
2649991554f2SKenneth D. Merry 		 */
265067feec50SStephen McConnell 		if ((scsi_cdb[0] == INQUIRY) &&
265167feec50SStephen McConnell 		    (scsi_cdb[1] & SI_EVPD) &&
265267feec50SStephen McConnell 		    (scsi_cdb[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2653991554f2SKenneth D. Merry 		    ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2654d2b4e18bSKenneth D. Merry 		    (csio->data_ptr != NULL) &&
2655d2b4e18bSKenneth D. Merry 		    ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2656d2b4e18bSKenneth D. Merry 		    (sc->control_TLR) &&
2657a2c14879SStephen McConnell 		    (sc->mapping_table[target_id].device_info &
2658991554f2SKenneth D. Merry 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2659991554f2SKenneth D. Merry 			vpd_list = (struct scsi_vpd_supported_page_list *)
2660991554f2SKenneth D. Merry 			    csio->data_ptr;
2661a2c14879SStephen McConnell 			TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2662991554f2SKenneth D. Merry 			*TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2663991554f2SKenneth D. Merry 			TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
266467feec50SStephen McConnell 			alloc_len = ((u16)scsi_cdb[3] << 8) + scsi_cdb[4];
2665d2b4e18bSKenneth D. Merry 			alloc_len -= csio->resid;
2666991554f2SKenneth D. Merry 			for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2667991554f2SKenneth D. Merry 				if (vpd_list->list[i] == 0x90) {
2668991554f2SKenneth D. Merry 					*TLR_bits = TLR_on;
2669991554f2SKenneth D. Merry 					break;
2670991554f2SKenneth D. Merry 				}
2671991554f2SKenneth D. Merry 			}
2672991554f2SKenneth D. Merry 		}
2673a2c14879SStephen McConnell 
2674a2c14879SStephen McConnell 		/*
2675a2c14879SStephen McConnell 		 * If this is a SATA direct-access end device, mark it so that
2676a2c14879SStephen McConnell 		 * a SCSI StartStopUnit command will be sent to it when the
2677a2c14879SStephen McConnell 		 * driver is being shutdown.
2678a2c14879SStephen McConnell 		 */
267967feec50SStephen McConnell 		if ((scsi_cdb[0] == INQUIRY) &&
2680fa699bb2SAlan Somers 		    (csio->data_ptr != NULL) &&
2681a2c14879SStephen McConnell 		    ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2682a2c14879SStephen McConnell 		    (sc->mapping_table[target_id].device_info &
2683a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2684a2c14879SStephen McConnell 		    ((sc->mapping_table[target_id].device_info &
2685a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2686a2c14879SStephen McConnell 		    MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2687a2c14879SStephen McConnell 			target = &sassc->targets[target_id];
2688a2c14879SStephen McConnell 			target->supports_SSU = TRUE;
2689a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_XINFO, "Target %d supports SSU\n",
2690a2c14879SStephen McConnell 			    target_id);
2691a2c14879SStephen McConnell 		}
2692991554f2SKenneth D. Merry 		break;
2693991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2694991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2695991554f2SKenneth D. Merry 		/*
2696991554f2SKenneth D. Merry 		 * If devinfo is 0 this will be a volume.  In that case don't
2697991554f2SKenneth D. Merry 		 * tell CAM that the volume is not there.  We want volumes to
2698991554f2SKenneth D. Merry 		 * be enumerated until they are deleted/removed, not just
2699991554f2SKenneth D. Merry 		 * failed.
2700991554f2SKenneth D. Merry 		 */
2701991554f2SKenneth D. Merry 		if (cm->cm_targ->devinfo == 0)
2702a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2703991554f2SKenneth D. Merry 		else
2704a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2705991554f2SKenneth D. Merry 		break;
2706991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_SGL:
2707991554f2SKenneth D. Merry 		mpr_print_scsiio_cmd(sc, cm);
2708a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2709991554f2SKenneth D. Merry 		break;
2710991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2711991554f2SKenneth D. Merry 		/*
2712991554f2SKenneth D. Merry 		 * This is one of the responses that comes back when an I/O
2713991554f2SKenneth D. Merry 		 * has been aborted.  If it is because of a timeout that we
2714991554f2SKenneth D. Merry 		 * initiated, just set the status to CAM_CMD_TIMEOUT.
2715991554f2SKenneth D. Merry 		 * Otherwise set it to CAM_REQ_ABORTED.  The effect on the
2716991554f2SKenneth D. Merry 		 * command is the same (it gets retried, subject to the
2717991554f2SKenneth D. Merry 		 * retry counter), the only difference is what gets printed
2718991554f2SKenneth D. Merry 		 * on the console.
2719991554f2SKenneth D. Merry 		 */
27208fe7bf06SWarner Losh 		if (cm->cm_flags & MPR_CM_FLAGS_TIMEDOUT)
2721a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2722991554f2SKenneth D. Merry 		else
2723a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2724991554f2SKenneth D. Merry 		break;
2725991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2726991554f2SKenneth D. Merry 		/* resid is ignored for this condition */
2727991554f2SKenneth D. Merry 		csio->resid = 0;
2728a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2729991554f2SKenneth D. Merry 		break;
2730991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2731991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2732991554f2SKenneth D. Merry 		/*
27336adfa7edSAlan Somers 		 * These can sometimes be transient transport-related
27346adfa7edSAlan Somers 		 * errors, and sometimes persistent drive-related errors.
27356adfa7edSAlan Somers 		 * We used to retry these without decrementing the retry
27366adfa7edSAlan Somers 		 * count by returning CAM_REQUEUE_REQ.  Unfortunately, if
27376adfa7edSAlan Somers 		 * we hit a persistent drive problem that returns one of
27386adfa7edSAlan Somers 		 * these error codes, we would retry indefinitely.  So,
27396adfa7edSAlan Somers 		 * return CAM_REQ_CMP_ERROR so that we decrement the retry
27406adfa7edSAlan Somers 		 * count and avoid infinite retries.  We're taking the
27416adfa7edSAlan Somers 		 * potential risk of flagging false failures in the event
27426adfa7edSAlan Somers 		 * of a topology-related error (e.g. a SAS expander problem
27436adfa7edSAlan Somers 		 * causes a command addressed to a drive to fail), but
27444c1cdd4aSWarner Losh 		 * avoiding getting into an infinite retry loop. However,
27454c1cdd4aSWarner Losh 		 * if we get them while were moving a device, we should
27464c1cdd4aSWarner Losh 		 * fail the request as 'not there' because the device
27474c1cdd4aSWarner Losh 		 * is effectively gone.
2748991554f2SKenneth D. Merry 		 */
27494c1cdd4aSWarner Losh 		if (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL)
27504c1cdd4aSWarner Losh 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
27514c1cdd4aSWarner Losh 		else
27526adfa7edSAlan Somers 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
27536eea4f46SScott Long 		mpr_dprint(sc, MPR_INFO,
27544c1cdd4aSWarner Losh 		    "Controller reported %s tgt %u SMID %u loginfo %x%s\n",
27552bf620cbSScott Long 		    mpr_describe_table(mpr_iocstatus_string,
27562bf620cbSScott Long 		    le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK),
27572bf620cbSScott Long 		    target_id, cm->cm_desc.Default.SMID,
27584c1cdd4aSWarner Losh 		    le32toh(rep->IOCLogInfo),
27594c1cdd4aSWarner Losh 		    (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL) ? " departing" : "");
27606eea4f46SScott Long 		mpr_dprint(sc, MPR_XINFO,
27616eea4f46SScott Long 		    "SCSIStatus %x SCSIState %x xfercount %u\n",
2762694cb8b8SScott Long 		    rep->SCSIStatus, rep->SCSIState,
2763991554f2SKenneth D. Merry 		    le32toh(rep->TransferCount));
2764991554f2SKenneth D. Merry 		break;
2765991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
2766991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
2767991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_VPID:
2768991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_FIELD:
2769991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_INVALID_STATE:
2770991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2771991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2772991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2773991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2774991554f2SKenneth D. Merry 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2775991554f2SKenneth D. Merry 	default:
2776991554f2SKenneth D. Merry 		mprsas_log_command(cm, MPR_XINFO,
2777694cb8b8SScott Long 		    "completed ioc %x loginfo %x scsi %x state %x xfer %u\n",
2778694cb8b8SScott Long 		    le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2779694cb8b8SScott Long 		    rep->SCSIStatus, rep->SCSIState,
2780991554f2SKenneth D. Merry 		    le32toh(rep->TransferCount));
2781991554f2SKenneth D. Merry 		csio->resid = cm->cm_length;
278267feec50SStephen McConnell 
278367feec50SStephen McConnell 		if (scsi_cdb[0] == UNMAP &&
278467feec50SStephen McConnell 		    target->is_nvme &&
278567feec50SStephen McConnell 		    (csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR)
278667feec50SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
278767feec50SStephen McConnell 		else
2788a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
278967feec50SStephen McConnell 
2790991554f2SKenneth D. Merry 		break;
2791991554f2SKenneth D. Merry 	}
2792991554f2SKenneth D. Merry 
2793991554f2SKenneth D. Merry 	mpr_sc_failed_io_info(sc, csio, rep, cm->cm_targ);
2794991554f2SKenneth D. Merry 
2795991554f2SKenneth D. Merry 	if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2796991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2797991554f2SKenneth D. Merry 		sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2798991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "Command completed, unfreezing SIM "
2799991554f2SKenneth D. Merry 		    "queue\n");
2800991554f2SKenneth D. Merry 	}
2801991554f2SKenneth D. Merry 
2802a2c14879SStephen McConnell 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2803991554f2SKenneth D. Merry 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
2804991554f2SKenneth D. Merry 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2805991554f2SKenneth D. Merry 	}
2806991554f2SKenneth D. Merry 
28074c1cdd4aSWarner Losh 	/*
28084c1cdd4aSWarner Losh 	 * Check to see if we're removing the device. If so, and this is the
28094c1cdd4aSWarner Losh 	 * last command on the queue, proceed with the deferred removal of the
28104c1cdd4aSWarner Losh 	 * device.  Note, for removing a volume, this won't trigger because
28114c1cdd4aSWarner Losh 	 * pending_remove_tm will be NULL.
28124c1cdd4aSWarner Losh 	 */
28134c1cdd4aSWarner Losh 	if (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL) {
28144c1cdd4aSWarner Losh 		if (TAILQ_FIRST(&cm->cm_targ->commands) == NULL &&
28154c1cdd4aSWarner Losh 		    cm->cm_targ->pending_remove_tm != NULL) {
28164c1cdd4aSWarner Losh 			mpr_dprint(sc, MPR_INFO, "Last pending command complete: starting remove_device\n");
28174c1cdd4aSWarner Losh 			mpr_map_command(sc, cm->cm_targ->pending_remove_tm);
28184c1cdd4aSWarner Losh 			cm->cm_targ->pending_remove_tm = NULL;
28194c1cdd4aSWarner Losh 		}
28204c1cdd4aSWarner Losh 	}
28214c1cdd4aSWarner Losh 
2822991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2823991554f2SKenneth D. Merry 	xpt_done(ccb);
2824991554f2SKenneth D. Merry }
2825991554f2SKenneth D. Merry 
2826991554f2SKenneth D. Merry static void
2827991554f2SKenneth D. Merry mprsas_smpio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2828991554f2SKenneth D. Merry {
2829991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2830991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2831991554f2SKenneth D. Merry 	uint64_t sasaddr;
2832991554f2SKenneth D. Merry 	union ccb *ccb;
2833991554f2SKenneth D. Merry 
2834991554f2SKenneth D. Merry 	ccb = cm->cm_complete_data;
2835991554f2SKenneth D. Merry 
2836991554f2SKenneth D. Merry 	/*
2837991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
2838991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and SMP
2839991554f2SKenneth D. Merry 	 * commands require two S/G elements only.  That should be handled
2840991554f2SKenneth D. Merry 	 * in the standard request size.
2841991554f2SKenneth D. Merry 	 */
2842991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2843a2c14879SStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x on SMP "
2844a2c14879SStephen McConnell 		    "request!\n", __func__, cm->cm_flags);
2845a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2846991554f2SKenneth D. Merry 		goto bailout;
2847991554f2SKenneth D. Merry         }
2848991554f2SKenneth D. Merry 
2849991554f2SKenneth D. Merry 	rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2850991554f2SKenneth D. Merry 	if (rpl == NULL) {
2851991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: NULL cm_reply!\n", __func__);
2852a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2853991554f2SKenneth D. Merry 		goto bailout;
2854991554f2SKenneth D. Merry 	}
2855991554f2SKenneth D. Merry 
2856991554f2SKenneth D. Merry 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2857991554f2SKenneth D. Merry 	sasaddr = le32toh(req->SASAddress.Low);
2858991554f2SKenneth D. Merry 	sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2859991554f2SKenneth D. Merry 
2860991554f2SKenneth D. Merry 	if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2861991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS ||
2862991554f2SKenneth D. Merry 	    rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2863991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2864991554f2SKenneth D. Merry 		    __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2865a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2866991554f2SKenneth D. Merry 		goto bailout;
2867991554f2SKenneth D. Merry 	}
2868991554f2SKenneth D. Merry 
2869a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s: SMP request to SAS address %#jx "
2870a2c14879SStephen McConnell 	    "completed successfully\n", __func__, (uintmax_t)sasaddr);
2871991554f2SKenneth D. Merry 
2872991554f2SKenneth D. Merry 	if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2873a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2874991554f2SKenneth D. Merry 	else
2875a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2876991554f2SKenneth D. Merry 
2877991554f2SKenneth D. Merry bailout:
2878991554f2SKenneth D. Merry 	/*
2879991554f2SKenneth D. Merry 	 * We sync in both directions because we had DMAs in the S/G list
2880991554f2SKenneth D. Merry 	 * in both directions.
2881991554f2SKenneth D. Merry 	 */
2882991554f2SKenneth D. Merry 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2883991554f2SKenneth D. Merry 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2884991554f2SKenneth D. Merry 	bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2885991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2886991554f2SKenneth D. Merry 	xpt_done(ccb);
2887991554f2SKenneth D. Merry }
2888991554f2SKenneth D. Merry 
2889991554f2SKenneth D. Merry static void
28907a2a6a1aSStephen McConnell mprsas_send_smpcmd(struct mprsas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2891991554f2SKenneth D. Merry {
2892991554f2SKenneth D. Merry 	struct mpr_command *cm;
2893991554f2SKenneth D. Merry 	uint8_t *request, *response;
2894991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2895991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2896991554f2SKenneth D. Merry 	struct sglist *sg;
2897991554f2SKenneth D. Merry 	int error;
2898991554f2SKenneth D. Merry 
2899991554f2SKenneth D. Merry 	sc = sassc->sc;
2900991554f2SKenneth D. Merry 	sg = NULL;
2901991554f2SKenneth D. Merry 	error = 0;
2902991554f2SKenneth D. Merry 
2903991554f2SKenneth D. Merry 	switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
2904991554f2SKenneth D. Merry 	case CAM_DATA_PADDR:
2905991554f2SKenneth D. Merry 	case CAM_DATA_SG_PADDR:
2906991554f2SKenneth D. Merry 		/*
2907991554f2SKenneth D. Merry 		 * XXX We don't yet support physical addresses here.
2908991554f2SKenneth D. Merry 		 */
2909991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: physical addresses not "
2910991554f2SKenneth D. Merry 		    "supported\n", __func__);
2911a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2912991554f2SKenneth D. Merry 		xpt_done(ccb);
2913991554f2SKenneth D. Merry 		return;
2914991554f2SKenneth D. Merry 	case CAM_DATA_SG:
2915991554f2SKenneth D. Merry 		/*
2916991554f2SKenneth D. Merry 		 * The chip does not support more than one buffer for the
2917991554f2SKenneth D. Merry 		 * request or response.
2918991554f2SKenneth D. Merry 		 */
2919991554f2SKenneth D. Merry 		if ((ccb->smpio.smp_request_sglist_cnt > 1)
2920991554f2SKenneth D. Merry 		    || (ccb->smpio.smp_response_sglist_cnt > 1)) {
29217a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "%s: multiple request or "
29227a2a6a1aSStephen McConnell 			    "response buffer segments not supported for SMP\n",
29237a2a6a1aSStephen McConnell 			    __func__);
2924a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2925991554f2SKenneth D. Merry 			xpt_done(ccb);
2926991554f2SKenneth D. Merry 			return;
2927991554f2SKenneth D. Merry 		}
2928991554f2SKenneth D. Merry 
2929991554f2SKenneth D. Merry 		/*
2930991554f2SKenneth D. Merry 		 * The CAM_SCATTER_VALID flag was originally implemented
2931991554f2SKenneth D. Merry 		 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2932991554f2SKenneth D. Merry 		 * We have two.  So, just take that flag to mean that we
2933991554f2SKenneth D. Merry 		 * might have S/G lists, and look at the S/G segment count
2934991554f2SKenneth D. Merry 		 * to figure out whether that is the case for each individual
2935991554f2SKenneth D. Merry 		 * buffer.
2936991554f2SKenneth D. Merry 		 */
2937991554f2SKenneth D. Merry 		if (ccb->smpio.smp_request_sglist_cnt != 0) {
2938991554f2SKenneth D. Merry 			bus_dma_segment_t *req_sg;
2939991554f2SKenneth D. Merry 
2940991554f2SKenneth D. Merry 			req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2941991554f2SKenneth D. Merry 			request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2942991554f2SKenneth D. Merry 		} else
2943991554f2SKenneth D. Merry 			request = ccb->smpio.smp_request;
2944991554f2SKenneth D. Merry 
2945991554f2SKenneth D. Merry 		if (ccb->smpio.smp_response_sglist_cnt != 0) {
2946991554f2SKenneth D. Merry 			bus_dma_segment_t *rsp_sg;
2947991554f2SKenneth D. Merry 
2948991554f2SKenneth D. Merry 			rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2949991554f2SKenneth D. Merry 			response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2950991554f2SKenneth D. Merry 		} else
2951991554f2SKenneth D. Merry 			response = ccb->smpio.smp_response;
2952991554f2SKenneth D. Merry 		break;
2953991554f2SKenneth D. Merry 	case CAM_DATA_VADDR:
2954991554f2SKenneth D. Merry 		request = ccb->smpio.smp_request;
2955991554f2SKenneth D. Merry 		response = ccb->smpio.smp_response;
2956991554f2SKenneth D. Merry 		break;
2957991554f2SKenneth D. Merry 	default:
2958a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2959991554f2SKenneth D. Merry 		xpt_done(ccb);
2960991554f2SKenneth D. Merry 		return;
2961991554f2SKenneth D. Merry 	}
2962991554f2SKenneth D. Merry 
2963991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
2964991554f2SKenneth D. Merry 	if (cm == NULL) {
29657a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: cannot allocate command\n",
29667a2a6a1aSStephen McConnell 		    __func__);
2967a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2968991554f2SKenneth D. Merry 		xpt_done(ccb);
2969991554f2SKenneth D. Merry 		return;
2970991554f2SKenneth D. Merry 	}
2971991554f2SKenneth D. Merry 
2972991554f2SKenneth D. Merry 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2973991554f2SKenneth D. Merry 	bzero(req, sizeof(*req));
2974991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2975991554f2SKenneth D. Merry 
2976991554f2SKenneth D. Merry 	/* Allow the chip to use any route to this SAS address. */
2977991554f2SKenneth D. Merry 	req->PhysicalPort = 0xff;
2978991554f2SKenneth D. Merry 
2979991554f2SKenneth D. Merry 	req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2980991554f2SKenneth D. Merry 	req->SGLFlags =
2981991554f2SKenneth D. Merry 	    MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2982991554f2SKenneth D. Merry 
2983991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO, "%s: sending SMP request to SAS address "
2984991554f2SKenneth D. Merry 	    "%#jx\n", __func__, (uintmax_t)sasaddr);
2985991554f2SKenneth D. Merry 
2986991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
2987991554f2SKenneth D. Merry 
2988991554f2SKenneth D. Merry 	/*
2989991554f2SKenneth D. Merry 	 * Set up a uio to pass into mpr_map_command().  This allows us to
2990991554f2SKenneth D. Merry 	 * do one map command, and one busdma call in there.
2991991554f2SKenneth D. Merry 	 */
2992991554f2SKenneth D. Merry 	cm->cm_uio.uio_iov = cm->cm_iovec;
2993991554f2SKenneth D. Merry 	cm->cm_uio.uio_iovcnt = 2;
2994991554f2SKenneth D. Merry 	cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2995991554f2SKenneth D. Merry 
2996991554f2SKenneth D. Merry 	/*
2997991554f2SKenneth D. Merry 	 * The read/write flag isn't used by busdma, but set it just in
2998991554f2SKenneth D. Merry 	 * case.  This isn't exactly accurate, either, since we're going in
2999991554f2SKenneth D. Merry 	 * both directions.
3000991554f2SKenneth D. Merry 	 */
3001991554f2SKenneth D. Merry 	cm->cm_uio.uio_rw = UIO_WRITE;
3002991554f2SKenneth D. Merry 
3003991554f2SKenneth D. Merry 	cm->cm_iovec[0].iov_base = request;
3004991554f2SKenneth D. Merry 	cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
3005991554f2SKenneth D. Merry 	cm->cm_iovec[1].iov_base = response;
3006991554f2SKenneth D. Merry 	cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
3007991554f2SKenneth D. Merry 
3008991554f2SKenneth D. Merry 	cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
3009991554f2SKenneth D. Merry 			       cm->cm_iovec[1].iov_len;
3010991554f2SKenneth D. Merry 
3011991554f2SKenneth D. Merry 	/*
3012991554f2SKenneth D. Merry 	 * Trigger a warning message in mpr_data_cb() for the user if we
3013991554f2SKenneth D. Merry 	 * wind up exceeding two S/G segments.  The chip expects one
3014991554f2SKenneth D. Merry 	 * segment for the request and another for the response.
3015991554f2SKenneth D. Merry 	 */
3016991554f2SKenneth D. Merry 	cm->cm_max_segs = 2;
3017991554f2SKenneth D. Merry 
3018991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3019991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_smpio_complete;
3020991554f2SKenneth D. Merry 	cm->cm_complete_data = ccb;
3021991554f2SKenneth D. Merry 
3022991554f2SKenneth D. Merry 	/*
3023991554f2SKenneth D. Merry 	 * Tell the mapping code that we're using a uio, and that this is
3024991554f2SKenneth D. Merry 	 * an SMP passthrough request.  There is a little special-case
3025991554f2SKenneth D. Merry 	 * logic there (in mpr_data_cb()) to handle the bidirectional
3026991554f2SKenneth D. Merry 	 * transfer.
3027991554f2SKenneth D. Merry 	 */
3028991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_USE_UIO | MPR_CM_FLAGS_SMP_PASS |
3029991554f2SKenneth D. Merry 			MPR_CM_FLAGS_DATAIN | MPR_CM_FLAGS_DATAOUT;
3030991554f2SKenneth D. Merry 
3031991554f2SKenneth D. Merry 	/* The chip data format is little endian. */
3032991554f2SKenneth D. Merry 	req->SASAddress.High = htole32(sasaddr >> 32);
3033991554f2SKenneth D. Merry 	req->SASAddress.Low = htole32(sasaddr);
3034991554f2SKenneth D. Merry 
3035991554f2SKenneth D. Merry 	/*
3036991554f2SKenneth D. Merry 	 * XXX Note that we don't have a timeout/abort mechanism here.
3037991554f2SKenneth D. Merry 	 * From the manual, it looks like task management requests only
3038991554f2SKenneth D. Merry 	 * work for SCSI IO and SATA passthrough requests.  We may need to
3039991554f2SKenneth D. Merry 	 * have a mechanism to retry requests in the event of a chip reset
3040991554f2SKenneth D. Merry 	 * at least.  Hopefully the chip will insure that any errors short
3041991554f2SKenneth D. Merry 	 * of that are relayed back to the driver.
3042991554f2SKenneth D. Merry 	 */
3043991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
3044991554f2SKenneth D. Merry 	if ((error != 0) && (error != EINPROGRESS)) {
3045991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: error %d returned from "
3046991554f2SKenneth D. Merry 		    "mpr_map_command()\n", __func__, error);
3047991554f2SKenneth D. Merry 		goto bailout_error;
3048991554f2SKenneth D. Merry 	}
3049991554f2SKenneth D. Merry 
3050991554f2SKenneth D. Merry 	return;
3051991554f2SKenneth D. Merry 
3052991554f2SKenneth D. Merry bailout_error:
3053991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
3054a2c14879SStephen McConnell 	mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3055991554f2SKenneth D. Merry 	xpt_done(ccb);
3056991554f2SKenneth D. Merry 	return;
3057991554f2SKenneth D. Merry }
3058991554f2SKenneth D. Merry 
3059991554f2SKenneth D. Merry static void
3060991554f2SKenneth D. Merry mprsas_action_smpio(struct mprsas_softc *sassc, union ccb *ccb)
3061991554f2SKenneth D. Merry {
3062991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3063991554f2SKenneth D. Merry 	struct mprsas_target *targ;
3064991554f2SKenneth D. Merry 	uint64_t sasaddr = 0;
3065991554f2SKenneth D. Merry 
3066991554f2SKenneth D. Merry 	sc = sassc->sc;
3067991554f2SKenneth D. Merry 
3068991554f2SKenneth D. Merry 	/*
3069991554f2SKenneth D. Merry 	 * Make sure the target exists.
3070991554f2SKenneth D. Merry 	 */
3071991554f2SKenneth D. Merry 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
3072991554f2SKenneth D. Merry 	    ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
3073991554f2SKenneth D. Merry 	targ = &sassc->targets[ccb->ccb_h.target_id];
3074991554f2SKenneth D. Merry 	if (targ->handle == 0x0) {
3075991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: target %d does not exist!\n",
3076991554f2SKenneth D. Merry 		    __func__, ccb->ccb_h.target_id);
3077a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
3078991554f2SKenneth D. Merry 		xpt_done(ccb);
3079991554f2SKenneth D. Merry 		return;
3080991554f2SKenneth D. Merry 	}
3081991554f2SKenneth D. Merry 
3082991554f2SKenneth D. Merry 	/*
3083991554f2SKenneth D. Merry 	 * If this device has an embedded SMP target, we'll talk to it
3084991554f2SKenneth D. Merry 	 * directly.
3085991554f2SKenneth D. Merry 	 * figure out what the expander's address is.
3086991554f2SKenneth D. Merry 	 */
3087991554f2SKenneth D. Merry 	if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
3088991554f2SKenneth D. Merry 		sasaddr = targ->sasaddr;
3089991554f2SKenneth D. Merry 
3090991554f2SKenneth D. Merry 	/*
3091991554f2SKenneth D. Merry 	 * If we don't have a SAS address for the expander yet, try
3092991554f2SKenneth D. Merry 	 * grabbing it from the page 0x83 information cached in the
3093991554f2SKenneth D. Merry 	 * transport layer for this target.  LSI expanders report the
3094991554f2SKenneth D. Merry 	 * expander SAS address as the port-associated SAS address in
3095991554f2SKenneth D. Merry 	 * Inquiry VPD page 0x83.  Maxim expanders don't report it in page
3096991554f2SKenneth D. Merry 	 * 0x83.
3097991554f2SKenneth D. Merry 	 *
3098991554f2SKenneth D. Merry 	 * XXX KDM disable this for now, but leave it commented out so that
3099991554f2SKenneth D. Merry 	 * it is obvious that this is another possible way to get the SAS
3100991554f2SKenneth D. Merry 	 * address.
3101991554f2SKenneth D. Merry 	 *
3102991554f2SKenneth D. Merry 	 * The parent handle method below is a little more reliable, and
3103991554f2SKenneth D. Merry 	 * the other benefit is that it works for devices other than SES
3104991554f2SKenneth D. Merry 	 * devices.  So you can send a SMP request to a da(4) device and it
3105991554f2SKenneth D. Merry 	 * will get routed to the expander that device is attached to.
3106991554f2SKenneth D. Merry 	 * (Assuming the da(4) device doesn't contain an SMP target...)
3107991554f2SKenneth D. Merry 	 */
3108991554f2SKenneth D. Merry #if 0
3109991554f2SKenneth D. Merry 	if (sasaddr == 0)
3110991554f2SKenneth D. Merry 		sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
3111991554f2SKenneth D. Merry #endif
3112991554f2SKenneth D. Merry 
3113991554f2SKenneth D. Merry 	/*
3114991554f2SKenneth D. Merry 	 * If we still don't have a SAS address for the expander, look for
3115991554f2SKenneth D. Merry 	 * the parent device of this device, which is probably the expander.
3116991554f2SKenneth D. Merry 	 */
3117991554f2SKenneth D. Merry 	if (sasaddr == 0) {
3118991554f2SKenneth D. Merry #ifdef OLD_MPR_PROBE
3119991554f2SKenneth D. Merry 		struct mprsas_target *parent_target;
3120991554f2SKenneth D. Merry #endif
3121991554f2SKenneth D. Merry 
3122991554f2SKenneth D. Merry 		if (targ->parent_handle == 0x0) {
3123991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
3124991554f2SKenneth D. Merry 			    "a valid parent handle!\n", __func__, targ->handle);
3125a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3126991554f2SKenneth D. Merry 			goto bailout;
3127991554f2SKenneth D. Merry 		}
3128991554f2SKenneth D. Merry #ifdef OLD_MPR_PROBE
3129991554f2SKenneth D. Merry 		parent_target = mprsas_find_target_by_handle(sassc, 0,
3130991554f2SKenneth D. Merry 		    targ->parent_handle);
3131991554f2SKenneth D. Merry 
3132991554f2SKenneth D. Merry 		if (parent_target == NULL) {
3133991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
3134991554f2SKenneth D. Merry 			    "a valid parent target!\n", __func__, targ->handle);
3135a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3136991554f2SKenneth D. Merry 			goto bailout;
3137991554f2SKenneth D. Merry 		}
3138991554f2SKenneth D. Merry 
3139991554f2SKenneth D. Merry 		if ((parent_target->devinfo &
3140991554f2SKenneth D. Merry 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3141991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent %d "
3142991554f2SKenneth D. Merry 			    "does not have an SMP target!\n", __func__,
3143991554f2SKenneth D. Merry 			    targ->handle, parent_target->handle);
3144a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3145991554f2SKenneth D. Merry 			goto bailout;
3146991554f2SKenneth D. Merry 		}
3147991554f2SKenneth D. Merry 
3148991554f2SKenneth D. Merry 		sasaddr = parent_target->sasaddr;
3149991554f2SKenneth D. Merry #else /* OLD_MPR_PROBE */
3150991554f2SKenneth D. Merry 		if ((targ->parent_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, targ->parent_handle);
3155a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3156991554f2SKenneth D. Merry 			goto bailout;
3157991554f2SKenneth D. Merry 		}
3158991554f2SKenneth D. Merry 		if (targ->parent_sasaddr == 0x0) {
3159991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent handle "
3160991554f2SKenneth D. Merry 			    "%d does not have a valid SAS address!\n", __func__,
3161991554f2SKenneth D. Merry 			    targ->handle, targ->parent_handle);
3162a2c14879SStephen McConnell 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3163991554f2SKenneth D. Merry 			goto bailout;
3164991554f2SKenneth D. Merry 		}
3165991554f2SKenneth D. Merry 
3166991554f2SKenneth D. Merry 		sasaddr = targ->parent_sasaddr;
3167991554f2SKenneth D. Merry #endif /* OLD_MPR_PROBE */
3168991554f2SKenneth D. Merry 	}
3169991554f2SKenneth D. Merry 
3170991554f2SKenneth D. Merry 	if (sasaddr == 0) {
3171991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INFO, "%s: unable to find SAS address for "
3172991554f2SKenneth D. Merry 		    "handle %d\n", __func__, targ->handle);
3173a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3174991554f2SKenneth D. Merry 		goto bailout;
3175991554f2SKenneth D. Merry 	}
3176991554f2SKenneth D. Merry 	mprsas_send_smpcmd(sassc, ccb, sasaddr);
3177991554f2SKenneth D. Merry 
3178991554f2SKenneth D. Merry 	return;
3179991554f2SKenneth D. Merry 
3180991554f2SKenneth D. Merry bailout:
3181991554f2SKenneth D. Merry 	xpt_done(ccb);
3182991554f2SKenneth D. Merry 
3183991554f2SKenneth D. Merry }
3184991554f2SKenneth D. Merry 
3185991554f2SKenneth D. Merry static void
3186991554f2SKenneth D. Merry mprsas_action_resetdev(struct mprsas_softc *sassc, union ccb *ccb)
3187991554f2SKenneth D. Merry {
3188991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3189991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3190991554f2SKenneth D. Merry 	struct mpr_command *tm;
3191991554f2SKenneth D. Merry 	struct mprsas_target *targ;
3192991554f2SKenneth D. Merry 
3193991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sassc->sc);
3194991554f2SKenneth D. Merry 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
3195991554f2SKenneth D. Merry 
31967a2a6a1aSStephen McConnell 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets, ("Target %d out of "
31977a2a6a1aSStephen McConnell 	    "bounds in XPT_RESET_DEV\n", ccb->ccb_h.target_id));
3198991554f2SKenneth D. Merry 	sc = sassc->sc;
31993921a9f7SScott Long 	tm = mprsas_alloc_tm(sc);
3200991554f2SKenneth D. Merry 	if (tm == NULL) {
32017a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "command alloc failure in "
32027a2a6a1aSStephen McConnell 		    "mprsas_action_resetdev\n");
3203a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3204991554f2SKenneth D. Merry 		xpt_done(ccb);
3205991554f2SKenneth D. Merry 		return;
3206991554f2SKenneth D. Merry 	}
3207991554f2SKenneth D. Merry 
3208991554f2SKenneth D. Merry 	targ = &sassc->targets[ccb->ccb_h.target_id];
3209991554f2SKenneth D. Merry 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3210991554f2SKenneth D. Merry 	req->DevHandle = htole16(targ->handle);
3211991554f2SKenneth D. Merry 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3212991554f2SKenneth D. Merry 
321389d1c21fSKashyap D Desai 	if (!targ->is_nvme || sc->custom_nvme_tm_handling) {
3214991554f2SKenneth D. Merry 		/* SAS Hard Link Reset / SATA Link Reset */
3215991554f2SKenneth D. Merry 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
321689d1c21fSKashyap D Desai 	} else {
321789d1c21fSKashyap D Desai 		/* PCIe Protocol Level Reset*/
321889d1c21fSKashyap D Desai 		req->MsgFlags =
321989d1c21fSKashyap D Desai 		    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
322089d1c21fSKashyap D Desai 	}
3221991554f2SKenneth D. Merry 
3222991554f2SKenneth D. Merry 	tm->cm_data = NULL;
3223991554f2SKenneth D. Merry 	tm->cm_complete = mprsas_resetdev_complete;
3224991554f2SKenneth D. Merry 	tm->cm_complete_data = ccb;
3225a2c14879SStephen McConnell 
3226a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
3227a2c14879SStephen McConnell 	    __func__, targ->tid);
3228991554f2SKenneth D. Merry 	tm->cm_targ = targ;
3229a2c14879SStephen McConnell 
323046b9415fSScott Long 	mprsas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
3231991554f2SKenneth D. Merry 	mpr_map_command(sc, tm);
3232991554f2SKenneth D. Merry }
3233991554f2SKenneth D. Merry 
3234991554f2SKenneth D. Merry static void
3235991554f2SKenneth D. Merry mprsas_resetdev_complete(struct mpr_softc *sc, struct mpr_command *tm)
3236991554f2SKenneth D. Merry {
3237991554f2SKenneth D. Merry 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3238991554f2SKenneth D. Merry 	union ccb *ccb;
3239991554f2SKenneth D. Merry 
3240991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3241991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
3242991554f2SKenneth D. Merry 
3243991554f2SKenneth D. Merry 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3244991554f2SKenneth D. Merry 	ccb = tm->cm_complete_data;
3245991554f2SKenneth D. Merry 
3246991554f2SKenneth D. Merry 	/*
3247991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
3248991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
3249991554f2SKenneth D. Merry 	 * task management commands don't have S/G lists.
3250991554f2SKenneth D. Merry 	 */
3251991554f2SKenneth D. Merry 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3252991554f2SKenneth D. Merry 		MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3253991554f2SKenneth D. Merry 
3254991554f2SKenneth D. Merry 		req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3255991554f2SKenneth D. Merry 
3256991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for reset of "
3257991554f2SKenneth D. Merry 		    "handle %#04x! This should not happen!\n", __func__,
3258991554f2SKenneth D. Merry 		    tm->cm_flags, req->DevHandle);
3259a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3260991554f2SKenneth D. Merry 		goto bailout;
3261991554f2SKenneth D. Merry 	}
3262991554f2SKenneth D. Merry 
32637a2a6a1aSStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n",
32647a2a6a1aSStephen McConnell 	    __func__, le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3265991554f2SKenneth D. Merry 
3266991554f2SKenneth D. Merry 	if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3267a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
3268991554f2SKenneth D. Merry 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3269991554f2SKenneth D. Merry 		    CAM_LUN_WILDCARD);
3270991554f2SKenneth D. Merry 	}
3271991554f2SKenneth D. Merry 	else
3272a2c14879SStephen McConnell 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3273991554f2SKenneth D. Merry 
3274991554f2SKenneth D. Merry bailout:
3275991554f2SKenneth D. Merry 
3276991554f2SKenneth D. Merry 	mprsas_free_tm(sc, tm);
3277991554f2SKenneth D. Merry 	xpt_done(ccb);
3278991554f2SKenneth D. Merry }
3279991554f2SKenneth D. Merry 
3280991554f2SKenneth D. Merry static void
3281991554f2SKenneth D. Merry mprsas_poll(struct cam_sim *sim)
3282991554f2SKenneth D. Merry {
3283991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
3284991554f2SKenneth D. Merry 
3285991554f2SKenneth D. Merry 	sassc = cam_sim_softc(sim);
3286991554f2SKenneth D. Merry 
3287991554f2SKenneth D. Merry 	if (sassc->sc->mpr_debug & MPR_TRACE) {
3288991554f2SKenneth D. Merry 		/* frequent debug messages during a panic just slow
3289991554f2SKenneth D. Merry 		 * everything down too much.
3290991554f2SKenneth D. Merry 		 */
3291a2c14879SStephen McConnell 		mpr_dprint(sassc->sc, MPR_XINFO, "%s clearing MPR_TRACE\n",
3292a2c14879SStephen McConnell 		    __func__);
3293991554f2SKenneth D. Merry 		sassc->sc->mpr_debug &= ~MPR_TRACE;
3294991554f2SKenneth D. Merry 	}
3295991554f2SKenneth D. Merry 
3296991554f2SKenneth D. Merry 	mpr_intr_locked(sassc->sc);
3297991554f2SKenneth D. Merry }
3298991554f2SKenneth D. Merry 
3299991554f2SKenneth D. Merry static void
3300991554f2SKenneth D. Merry mprsas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3301991554f2SKenneth D. Merry     void *arg)
3302991554f2SKenneth D. Merry {
3303991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3304991554f2SKenneth D. Merry 
3305991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)callback_arg;
3306991554f2SKenneth D. Merry 
3307991554f2SKenneth D. Merry 	switch (code) {
3308991554f2SKenneth D. Merry 	case AC_ADVINFO_CHANGED: {
3309991554f2SKenneth D. Merry 		struct mprsas_target *target;
3310991554f2SKenneth D. Merry 		struct mprsas_softc *sassc;
3311991554f2SKenneth D. Merry 		struct scsi_read_capacity_data_long rcap_buf;
3312991554f2SKenneth D. Merry 		struct ccb_dev_advinfo cdai;
3313991554f2SKenneth D. Merry 		struct mprsas_lun *lun;
3314991554f2SKenneth D. Merry 		lun_id_t lunid;
3315991554f2SKenneth D. Merry 		int found_lun;
3316991554f2SKenneth D. Merry 		uintptr_t buftype;
3317991554f2SKenneth D. Merry 
3318991554f2SKenneth D. Merry 		buftype = (uintptr_t)arg;
3319991554f2SKenneth D. Merry 
3320991554f2SKenneth D. Merry 		found_lun = 0;
3321991554f2SKenneth D. Merry 		sassc = sc->sassc;
3322991554f2SKenneth D. Merry 
3323991554f2SKenneth D. Merry 		/*
3324991554f2SKenneth D. Merry 		 * We're only interested in read capacity data changes.
3325991554f2SKenneth D. Merry 		 */
3326991554f2SKenneth D. Merry 		if (buftype != CDAI_TYPE_RCAPLONG)
3327991554f2SKenneth D. Merry 			break;
3328991554f2SKenneth D. Merry 
3329991554f2SKenneth D. Merry 		/*
3330991554f2SKenneth D. Merry 		 * We should have a handle for this, but check to make sure.
3331991554f2SKenneth D. Merry 		 */
3332991554f2SKenneth D. Merry 		KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3333991554f2SKenneth D. Merry 		    ("Target %d out of bounds in mprsas_async\n",
3334991554f2SKenneth D. Merry 		    xpt_path_target_id(path)));
3335991554f2SKenneth D. Merry 		target = &sassc->targets[xpt_path_target_id(path)];
3336991554f2SKenneth D. Merry 		if (target->handle == 0)
3337991554f2SKenneth D. Merry 			break;
3338991554f2SKenneth D. Merry 
3339991554f2SKenneth D. Merry 		lunid = xpt_path_lun_id(path);
3340991554f2SKenneth D. Merry 
3341991554f2SKenneth D. Merry 		SLIST_FOREACH(lun, &target->luns, lun_link) {
3342991554f2SKenneth D. Merry 			if (lun->lun_id == lunid) {
3343991554f2SKenneth D. Merry 				found_lun = 1;
3344991554f2SKenneth D. Merry 				break;
3345991554f2SKenneth D. Merry 			}
3346991554f2SKenneth D. Merry 		}
3347991554f2SKenneth D. Merry 
3348991554f2SKenneth D. Merry 		if (found_lun == 0) {
3349991554f2SKenneth D. Merry 			lun = malloc(sizeof(struct mprsas_lun), M_MPR,
3350991554f2SKenneth D. Merry 			    M_NOWAIT | M_ZERO);
3351991554f2SKenneth D. Merry 			if (lun == NULL) {
3352991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_ERROR, "Unable to alloc "
3353991554f2SKenneth D. Merry 				    "LUN for EEDP support.\n");
3354991554f2SKenneth D. Merry 				break;
3355991554f2SKenneth D. Merry 			}
3356991554f2SKenneth D. Merry 			lun->lun_id = lunid;
3357991554f2SKenneth D. Merry 			SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3358991554f2SKenneth D. Merry 		}
3359991554f2SKenneth D. Merry 
3360991554f2SKenneth D. Merry 		bzero(&rcap_buf, sizeof(rcap_buf));
3361*7608b98cSEdward Tomasz Napierala 		bzero(&cdai, sizeof(cdai));
3362991554f2SKenneth D. Merry 		xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3363991554f2SKenneth D. Merry 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3364991554f2SKenneth D. Merry 		cdai.ccb_h.flags = CAM_DIR_IN;
3365991554f2SKenneth D. Merry 		cdai.buftype = CDAI_TYPE_RCAPLONG;
3366e8577fb4SKenneth D. Merry 		cdai.flags = CDAI_FLAG_NONE;
3367991554f2SKenneth D. Merry 		cdai.bufsiz = sizeof(rcap_buf);
3368991554f2SKenneth D. Merry 		cdai.buf = (uint8_t *)&rcap_buf;
3369991554f2SKenneth D. Merry 		xpt_action((union ccb *)&cdai);
3370991554f2SKenneth D. Merry 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3371991554f2SKenneth D. Merry 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3372991554f2SKenneth D. Merry 
3373a2c14879SStephen McConnell 		if ((mprsas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3374991554f2SKenneth D. Merry 		    && (rcap_buf.prot & SRC16_PROT_EN)) {
33758881681bSKenneth D. Merry 			switch (rcap_buf.prot & SRC16_P_TYPE) {
33768881681bSKenneth D. Merry 			case SRC16_PTYPE_1:
33778881681bSKenneth D. Merry 			case SRC16_PTYPE_3:
3378991554f2SKenneth D. Merry 				lun->eedp_formatted = TRUE;
33798881681bSKenneth D. Merry 				lun->eedp_block_size =
33808881681bSKenneth D. Merry 				    scsi_4btoul(rcap_buf.length);
33818881681bSKenneth D. Merry 				break;
33828881681bSKenneth D. Merry 			case SRC16_PTYPE_2:
33838881681bSKenneth D. Merry 			default:
33848881681bSKenneth D. Merry 				lun->eedp_formatted = FALSE;
33858881681bSKenneth D. Merry 				lun->eedp_block_size = 0;
33868881681bSKenneth D. Merry 				break;
33878881681bSKenneth D. Merry 			}
3388991554f2SKenneth D. Merry 		} else {
3389991554f2SKenneth D. Merry 			lun->eedp_formatted = FALSE;
3390991554f2SKenneth D. Merry 			lun->eedp_block_size = 0;
3391991554f2SKenneth D. Merry 		}
3392991554f2SKenneth D. Merry 		break;
3393991554f2SKenneth D. Merry 	}
33940d87f3c7SWarner Losh 	case AC_FOUND_DEVICE:
3395991554f2SKenneth D. Merry 	default:
3396991554f2SKenneth D. Merry 		break;
3397991554f2SKenneth D. Merry 	}
3398991554f2SKenneth D. Merry }
3399991554f2SKenneth D. Merry 
3400a2c14879SStephen McConnell /*
3401a2c14879SStephen McConnell  * Set the INRESET flag for this target so that no I/O will be sent to
3402a2c14879SStephen McConnell  * the target until the reset has completed.  If an I/O request does
3403a2c14879SStephen McConnell  * happen, the devq will be frozen.  The CCB holds the path which is
3404a2c14879SStephen McConnell  * used to release the devq.  The devq is released and the CCB is freed
3405a2c14879SStephen McConnell  * when the TM completes.
3406a2c14879SStephen McConnell  */
3407b7f1ee79SScott Long void
3408b7f1ee79SScott Long mprsas_prepare_for_tm(struct mpr_softc *sc, struct mpr_command *tm,
3409b7f1ee79SScott Long     struct mprsas_target *target, lun_id_t lun_id)
3410b7f1ee79SScott Long {
3411b7f1ee79SScott Long 	union ccb *ccb;
3412b7f1ee79SScott Long 	path_id_t path_id;
3413b7f1ee79SScott Long 
3414a2c14879SStephen McConnell 	ccb = xpt_alloc_ccb_nowait();
3415a2c14879SStephen McConnell 	if (ccb) {
3416a2c14879SStephen McConnell 		path_id = cam_sim_path(sc->sassc->sim);
3417a2c14879SStephen McConnell 		if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3418a2c14879SStephen McConnell 		    target->tid, lun_id) != CAM_REQ_CMP) {
3419a2c14879SStephen McConnell 			xpt_free_ccb(ccb);
3420a2c14879SStephen McConnell 		} else {
3421a2c14879SStephen McConnell 			tm->cm_ccb = ccb;
3422a2c14879SStephen McConnell 			tm->cm_targ = target;
3423a2c14879SStephen McConnell 			target->flags |= MPRSAS_TARGET_INRESET;
3424a2c14879SStephen McConnell 		}
3425a2c14879SStephen McConnell 	}
3426a2c14879SStephen McConnell }
3427a2c14879SStephen McConnell 
3428991554f2SKenneth D. Merry int
3429991554f2SKenneth D. Merry mprsas_startup(struct mpr_softc *sc)
3430991554f2SKenneth D. Merry {
3431991554f2SKenneth D. Merry 	/*
3432991554f2SKenneth D. Merry 	 * Send the port enable message and set the wait_for_port_enable flag.
3433991554f2SKenneth D. Merry 	 * This flag helps to keep the simq frozen until all discovery events
3434991554f2SKenneth D. Merry 	 * are processed.
3435991554f2SKenneth D. Merry 	 */
3436991554f2SKenneth D. Merry 	sc->wait_for_port_enable = 1;
3437991554f2SKenneth D. Merry 	mprsas_send_portenable(sc);
3438991554f2SKenneth D. Merry 	return (0);
3439991554f2SKenneth D. Merry }
3440991554f2SKenneth D. Merry 
3441991554f2SKenneth D. Merry static int
3442991554f2SKenneth D. Merry mprsas_send_portenable(struct mpr_softc *sc)
3443991554f2SKenneth D. Merry {
3444991554f2SKenneth D. Merry 	MPI2_PORT_ENABLE_REQUEST *request;
3445991554f2SKenneth D. Merry 	struct mpr_command *cm;
3446991554f2SKenneth D. Merry 
3447991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3448991554f2SKenneth D. Merry 
3449991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
3450991554f2SKenneth D. Merry 		return (EBUSY);
3451991554f2SKenneth D. Merry 	request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3452991554f2SKenneth D. Merry 	request->Function = MPI2_FUNCTION_PORT_ENABLE;
3453991554f2SKenneth D. Merry 	request->MsgFlags = 0;
3454991554f2SKenneth D. Merry 	request->VP_ID = 0;
3455991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3456991554f2SKenneth D. Merry 	cm->cm_complete = mprsas_portenable_complete;
3457991554f2SKenneth D. Merry 	cm->cm_data = NULL;
3458991554f2SKenneth D. Merry 	cm->cm_sge = NULL;
3459991554f2SKenneth D. Merry 
3460991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
3461991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_XINFO,
3462991554f2SKenneth D. Merry 	    "mpr_send_portenable finished cm %p req %p complete %p\n",
3463991554f2SKenneth D. Merry 	    cm, cm->cm_req, cm->cm_complete);
3464991554f2SKenneth D. Merry 	return (0);
3465991554f2SKenneth D. Merry }
3466991554f2SKenneth D. Merry 
3467991554f2SKenneth D. Merry static void
3468991554f2SKenneth D. Merry mprsas_portenable_complete(struct mpr_softc *sc, struct mpr_command *cm)
3469991554f2SKenneth D. Merry {
3470991554f2SKenneth D. Merry 	MPI2_PORT_ENABLE_REPLY *reply;
3471991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
3472991554f2SKenneth D. Merry 
3473991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3474991554f2SKenneth D. Merry 	sassc = sc->sassc;
3475991554f2SKenneth D. Merry 
3476991554f2SKenneth D. Merry 	/*
3477991554f2SKenneth D. Merry 	 * Currently there should be no way we can hit this case.  It only
3478991554f2SKenneth D. Merry 	 * happens when we have a failure to allocate chain frames, and
3479991554f2SKenneth D. Merry 	 * port enable commands don't have S/G lists.
3480991554f2SKenneth D. Merry 	 */
3481991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3482991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for port enable! "
3483991554f2SKenneth D. Merry 		    "This should not happen!\n", __func__, cm->cm_flags);
3484991554f2SKenneth D. Merry 	}
3485991554f2SKenneth D. Merry 
3486991554f2SKenneth D. Merry 	reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3487991554f2SKenneth D. Merry 	if (reply == NULL)
3488991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Portenable NULL reply\n");
348971900a79SAlfredo Dal'Ava Junior 	else if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
3490991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS)
3491991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Portenable failed\n");
3492991554f2SKenneth D. Merry 
3493991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
3494991554f2SKenneth D. Merry 	/*
3495991554f2SKenneth D. Merry 	 * Done waiting for port enable to complete.  Decrement the refcount.
3496991554f2SKenneth D. Merry 	 * If refcount is 0, discovery is complete and a rescan of the bus can
3497991554f2SKenneth D. Merry 	 * take place.
3498991554f2SKenneth D. Merry 	 */
3499991554f2SKenneth D. Merry 	sc->wait_for_port_enable = 0;
3500991554f2SKenneth D. Merry 	sc->port_enable_complete = 1;
3501991554f2SKenneth D. Merry 	wakeup(&sc->port_enable_complete);
3502991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
3503991554f2SKenneth D. Merry }
3504991554f2SKenneth D. Merry 
3505991554f2SKenneth D. Merry int
3506991554f2SKenneth D. Merry mprsas_check_id(struct mprsas_softc *sassc, int id)
3507991554f2SKenneth D. Merry {
3508991554f2SKenneth D. Merry 	struct mpr_softc *sc = sassc->sc;
3509991554f2SKenneth D. Merry 	char *ids;
3510991554f2SKenneth D. Merry 	char *name;
3511991554f2SKenneth D. Merry 
3512991554f2SKenneth D. Merry 	ids = &sc->exclude_ids[0];
3513991554f2SKenneth D. Merry 	while((name = strsep(&ids, ",")) != NULL) {
3514991554f2SKenneth D. Merry 		if (name[0] == '\0')
3515991554f2SKenneth D. Merry 			continue;
3516991554f2SKenneth D. Merry 		if (strtol(name, NULL, 0) == (long)id)
3517991554f2SKenneth D. Merry 			return (1);
3518991554f2SKenneth D. Merry 	}
3519991554f2SKenneth D. Merry 
3520991554f2SKenneth D. Merry 	return (0);
3521991554f2SKenneth D. Merry }
3522a2c14879SStephen McConnell 
3523a2c14879SStephen McConnell void
3524a2c14879SStephen McConnell mprsas_realloc_targets(struct mpr_softc *sc, int maxtargets)
3525a2c14879SStephen McConnell {
3526a2c14879SStephen McConnell 	struct mprsas_softc *sassc;
3527a2c14879SStephen McConnell 	struct mprsas_lun *lun, *lun_tmp;
3528a2c14879SStephen McConnell 	struct mprsas_target *targ;
3529a2c14879SStephen McConnell 	int i;
3530a2c14879SStephen McConnell 
3531a2c14879SStephen McConnell 	sassc = sc->sassc;
3532a2c14879SStephen McConnell 	/*
3533a2c14879SStephen McConnell 	 * The number of targets is based on IOC Facts, so free all of
3534a2c14879SStephen McConnell 	 * the allocated LUNs for each target and then the target buffer
3535a2c14879SStephen McConnell 	 * itself.
3536a2c14879SStephen McConnell 	 */
3537a2c14879SStephen McConnell 	for (i=0; i< maxtargets; i++) {
3538a2c14879SStephen McConnell 		targ = &sassc->targets[i];
3539a2c14879SStephen McConnell 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3540a2c14879SStephen McConnell 			free(lun, M_MPR);
3541a2c14879SStephen McConnell 		}
3542a2c14879SStephen McConnell 	}
3543a2c14879SStephen McConnell 	free(sassc->targets, M_MPR);
3544a2c14879SStephen McConnell 
3545a2c14879SStephen McConnell 	sassc->targets = malloc(sizeof(struct mprsas_target) * maxtargets,
3546a2c14879SStephen McConnell 	    M_MPR, M_WAITOK|M_ZERO);
3547a2c14879SStephen McConnell }
3548