xref: /freebsd/sys/dev/mpr/mpr_sas.c (revision efe3b0de1438e7a8473d92f2be57072394559e3c)
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011-2015 LSI Corp.
4  * Copyright (c) 2013-2016 Avago Technologies
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /* Communications core for Avago Technologies (LSI) MPT3 */
36 
37 /* TODO Move headers to mprvar */
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/selinfo.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/conf.h>
46 #include <sys/bio.h>
47 #include <sys/malloc.h>
48 #include <sys/uio.h>
49 #include <sys/sysctl.h>
50 #include <sys/endian.h>
51 #include <sys/queue.h>
52 #include <sys/kthread.h>
53 #include <sys/taskqueue.h>
54 #include <sys/sbuf.h>
55 
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59 
60 #include <machine/stdarg.h>
61 
62 #include <cam/cam.h>
63 #include <cam/cam_ccb.h>
64 #include <cam/cam_debug.h>
65 #include <cam/cam_sim.h>
66 #include <cam/cam_xpt_sim.h>
67 #include <cam/cam_xpt_periph.h>
68 #include <cam/cam_periph.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_message.h>
71 #if __FreeBSD_version >= 900026
72 #include <cam/scsi/smp_all.h>
73 #endif
74 
75 #include <dev/mpr/mpi/mpi2_type.h>
76 #include <dev/mpr/mpi/mpi2.h>
77 #include <dev/mpr/mpi/mpi2_ioc.h>
78 #include <dev/mpr/mpi/mpi2_sas.h>
79 #include <dev/mpr/mpi/mpi2_cnfg.h>
80 #include <dev/mpr/mpi/mpi2_init.h>
81 #include <dev/mpr/mpi/mpi2_tool.h>
82 #include <dev/mpr/mpr_ioctl.h>
83 #include <dev/mpr/mprvar.h>
84 #include <dev/mpr/mpr_table.h>
85 #include <dev/mpr/mpr_sas.h>
86 
87 #define MPRSAS_DISCOVERY_TIMEOUT	20
88 #define MPRSAS_MAX_DISCOVERY_TIMEOUTS	10 /* 200 seconds */
89 
90 /*
91  * static array to check SCSI OpCode for EEDP protection bits
92  */
93 #define	PRO_R MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
94 #define	PRO_W MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
95 #define	PRO_V MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
96 static uint8_t op_code_prot[256] = {
97 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
100 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 	0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
106 	0, 0, 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 	0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
108 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
113 };
114 
115 MALLOC_DEFINE(M_MPRSAS, "MPRSAS", "MPR SAS memory");
116 
117 static void mprsas_remove_device(struct mpr_softc *, struct mpr_command *);
118 static void mprsas_remove_complete(struct mpr_softc *, struct mpr_command *);
119 static void mprsas_action(struct cam_sim *sim, union ccb *ccb);
120 static void mprsas_poll(struct cam_sim *sim);
121 static void mprsas_scsiio_timeout(void *data);
122 static void mprsas_abort_complete(struct mpr_softc *sc, struct mpr_command *cm);
123 static void mprsas_action_scsiio(struct mprsas_softc *, union ccb *);
124 static void mprsas_scsiio_complete(struct mpr_softc *, struct mpr_command *);
125 static void mprsas_action_resetdev(struct mprsas_softc *, union ccb *);
126 static void mprsas_resetdev_complete(struct mpr_softc *, struct mpr_command *);
127 static int mprsas_send_abort(struct mpr_softc *sc, struct mpr_command *tm,
128     struct mpr_command *cm);
129 static void mprsas_async(void *callback_arg, uint32_t code,
130     struct cam_path *path, void *arg);
131 #if (__FreeBSD_version < 901503) || \
132     ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
133 static void mprsas_check_eedp(struct mpr_softc *sc, struct cam_path *path,
134     struct ccb_getdev *cgd);
135 static void mprsas_read_cap_done(struct cam_periph *periph,
136     union ccb *done_ccb);
137 #endif
138 static int mprsas_send_portenable(struct mpr_softc *sc);
139 static void mprsas_portenable_complete(struct mpr_softc *sc,
140     struct mpr_command *cm);
141 
142 #if __FreeBSD_version >= 900026
143 static void mprsas_smpio_complete(struct mpr_softc *sc, struct mpr_command *cm);
144 static void mprsas_send_smpcmd(struct mprsas_softc *sassc, union ccb *ccb,
145     uint64_t sasaddr);
146 static void mprsas_action_smpio(struct mprsas_softc *sassc, union ccb *ccb);
147 #endif //FreeBSD_version >= 900026
148 
149 struct mprsas_target *
150 mprsas_find_target_by_handle(struct mprsas_softc *sassc, int start,
151     uint16_t handle)
152 {
153 	struct mprsas_target *target;
154 	int i;
155 
156 	for (i = start; i < sassc->maxtargets; i++) {
157 		target = &sassc->targets[i];
158 		if (target->handle == handle)
159 			return (target);
160 	}
161 
162 	return (NULL);
163 }
164 
165 /* we need to freeze the simq during attach and diag reset, to avoid failing
166  * commands before device handles have been found by discovery.  Since
167  * discovery involves reading config pages and possibly sending commands,
168  * discovery actions may continue even after we receive the end of discovery
169  * event, so refcount discovery actions instead of assuming we can unfreeze
170  * the simq when we get the event.
171  */
172 void
173 mprsas_startup_increment(struct mprsas_softc *sassc)
174 {
175 	MPR_FUNCTRACE(sassc->sc);
176 
177 	if ((sassc->flags & MPRSAS_IN_STARTUP) != 0) {
178 		if (sassc->startup_refcount++ == 0) {
179 			/* just starting, freeze the simq */
180 			mpr_dprint(sassc->sc, MPR_INIT,
181 			    "%s freezing simq\n", __func__);
182 #if (__FreeBSD_version >= 1000039) || \
183     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
184 			xpt_hold_boot();
185 #endif
186 			xpt_freeze_simq(sassc->sim, 1);
187 		}
188 		mpr_dprint(sassc->sc, MPR_INIT, "%s refcount %u\n", __func__,
189 		    sassc->startup_refcount);
190 	}
191 }
192 
193 void
194 mprsas_release_simq_reinit(struct mprsas_softc *sassc)
195 {
196 	if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
197 		sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
198 		xpt_release_simq(sassc->sim, 1);
199 		mpr_dprint(sassc->sc, MPR_INFO, "Unfreezing SIM queue\n");
200 	}
201 }
202 
203 void
204 mprsas_startup_decrement(struct mprsas_softc *sassc)
205 {
206 	MPR_FUNCTRACE(sassc->sc);
207 
208 	if ((sassc->flags & MPRSAS_IN_STARTUP) != 0) {
209 		if (--sassc->startup_refcount == 0) {
210 			/* finished all discovery-related actions, release
211 			 * the simq and rescan for the latest topology.
212 			 */
213 			mpr_dprint(sassc->sc, MPR_INIT,
214 			    "%s releasing simq\n", __func__);
215 			sassc->flags &= ~MPRSAS_IN_STARTUP;
216 			xpt_release_simq(sassc->sim, 1);
217 #if (__FreeBSD_version >= 1000039) || \
218     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
219 			xpt_release_boot();
220 #else
221 			mprsas_rescan_target(sassc->sc, NULL);
222 #endif
223 		}
224 		mpr_dprint(sassc->sc, MPR_INIT, "%s refcount %u\n", __func__,
225 		    sassc->startup_refcount);
226 	}
227 }
228 
229 /* The firmware requires us to stop sending commands when we're doing task
230  * management, so refcount the TMs and keep the simq frozen when any are in
231  * use.
232  */
233 struct mpr_command *
234 mprsas_alloc_tm(struct mpr_softc *sc)
235 {
236 	struct mpr_command *tm;
237 
238 	MPR_FUNCTRACE(sc);
239 	tm = mpr_alloc_high_priority_command(sc);
240 	return tm;
241 }
242 
243 void
244 mprsas_free_tm(struct mpr_softc *sc, struct mpr_command *tm)
245 {
246 	int target_id = 0xFFFFFFFF;
247 
248 	MPR_FUNCTRACE(sc);
249 	if (tm == NULL)
250 		return;
251 
252 	/*
253 	 * For TM's the devq is frozen for the device.  Unfreeze it here and
254 	 * free the resources used for freezing the devq.  Must clear the
255 	 * INRESET flag as well or scsi I/O will not work.
256 	 */
257 	if (tm->cm_targ != NULL) {
258 		tm->cm_targ->flags &= ~MPRSAS_TARGET_INRESET;
259 		target_id = tm->cm_targ->tid;
260 	}
261 	if (tm->cm_ccb) {
262 		mpr_dprint(sc, MPR_INFO, "Unfreezing devq for target ID %d\n",
263 		    target_id);
264 		xpt_release_devq(tm->cm_ccb->ccb_h.path, 1, TRUE);
265 		xpt_free_path(tm->cm_ccb->ccb_h.path);
266 		xpt_free_ccb(tm->cm_ccb);
267 	}
268 
269 	mpr_free_high_priority_command(sc, tm);
270 }
271 
272 void
273 mprsas_rescan_target(struct mpr_softc *sc, struct mprsas_target *targ)
274 {
275 	struct mprsas_softc *sassc = sc->sassc;
276 	path_id_t pathid;
277 	target_id_t targetid;
278 	union ccb *ccb;
279 
280 	MPR_FUNCTRACE(sc);
281 	pathid = cam_sim_path(sassc->sim);
282 	if (targ == NULL)
283 		targetid = CAM_TARGET_WILDCARD;
284 	else
285 		targetid = targ - sassc->targets;
286 
287 	/*
288 	 * Allocate a CCB and schedule a rescan.
289 	 */
290 	ccb = xpt_alloc_ccb_nowait();
291 	if (ccb == NULL) {
292 		mpr_dprint(sc, MPR_ERROR, "unable to alloc CCB for rescan\n");
293 		return;
294 	}
295 
296 	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
297 	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
298 		mpr_dprint(sc, MPR_ERROR, "unable to create path for rescan\n");
299 		xpt_free_ccb(ccb);
300 		return;
301 	}
302 
303 	if (targetid == CAM_TARGET_WILDCARD)
304 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
305 	else
306 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
307 
308 	mpr_dprint(sc, MPR_TRACE, "%s targetid %u\n", __func__, targetid);
309 	xpt_rescan(ccb);
310 }
311 
312 static void
313 mprsas_log_command(struct mpr_command *cm, u_int level, const char *fmt, ...)
314 {
315 	struct sbuf sb;
316 	va_list ap;
317 	char str[192];
318 	char path_str[64];
319 
320 	if (cm == NULL)
321 		return;
322 
323 	/* No need to be in here if debugging isn't enabled */
324 	if ((cm->cm_sc->mpr_debug & level) == 0)
325 		return;
326 
327 	sbuf_new(&sb, str, sizeof(str), 0);
328 
329 	va_start(ap, fmt);
330 
331 	if (cm->cm_ccb != NULL) {
332 		xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str,
333 		    sizeof(path_str));
334 		sbuf_cat(&sb, path_str);
335 		if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) {
336 			scsi_command_string(&cm->cm_ccb->csio, &sb);
337 			sbuf_printf(&sb, "length %d ",
338 			    cm->cm_ccb->csio.dxfer_len);
339 		}
340 	} else {
341 		sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ",
342 		    cam_sim_name(cm->cm_sc->sassc->sim),
343 		    cam_sim_unit(cm->cm_sc->sassc->sim),
344 		    cam_sim_bus(cm->cm_sc->sassc->sim),
345 		    cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF,
346 		    cm->cm_lun);
347 	}
348 
349 	sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID);
350 	sbuf_vprintf(&sb, fmt, ap);
351 	sbuf_finish(&sb);
352 	mpr_print_field(cm->cm_sc, "%s", sbuf_data(&sb));
353 
354 	va_end(ap);
355 }
356 
357 static void
358 mprsas_remove_volume(struct mpr_softc *sc, struct mpr_command *tm)
359 {
360 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
361 	struct mprsas_target *targ;
362 	uint16_t handle;
363 
364 	MPR_FUNCTRACE(sc);
365 
366 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
367 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
368 	targ = tm->cm_targ;
369 
370 	if (reply == NULL) {
371 		/* XXX retry the remove after the diag reset completes? */
372 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply resetting device "
373 		    "0x%04x\n", __func__, handle);
374 		mprsas_free_tm(sc, tm);
375 		return;
376 	}
377 
378 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
379 	    MPI2_IOCSTATUS_SUCCESS) {
380 		mpr_dprint(sc, MPR_ERROR, "IOCStatus = 0x%x while resetting "
381 		    "device 0x%x\n", le16toh(reply->IOCStatus), handle);
382 	}
383 
384 	mpr_dprint(sc, MPR_XINFO, "Reset aborted %u commands\n",
385 	    le32toh(reply->TerminationCount));
386 	mpr_free_reply(sc, tm->cm_reply_data);
387 	tm->cm_reply = NULL;	/* Ensures the reply won't get re-freed */
388 
389 	mpr_dprint(sc, MPR_XINFO, "clearing target %u handle 0x%04x\n",
390 	    targ->tid, handle);
391 
392 	/*
393 	 * Don't clear target if remove fails because things will get confusing.
394 	 * Leave the devname and sasaddr intact so that we know to avoid reusing
395 	 * this target id if possible, and so we can assign the same target id
396 	 * to this device if it comes back in the future.
397 	 */
398 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
399 	    MPI2_IOCSTATUS_SUCCESS) {
400 		targ = tm->cm_targ;
401 		targ->handle = 0x0;
402 		targ->encl_handle = 0x0;
403 		targ->encl_level_valid = 0x0;
404 		targ->encl_level = 0x0;
405 		targ->connector_name[0] = ' ';
406 		targ->connector_name[1] = ' ';
407 		targ->connector_name[2] = ' ';
408 		targ->connector_name[3] = ' ';
409 		targ->encl_slot = 0x0;
410 		targ->exp_dev_handle = 0x0;
411 		targ->phy_num = 0x0;
412 		targ->linkrate = 0x0;
413 		targ->devinfo = 0x0;
414 		targ->flags = 0x0;
415 		targ->scsi_req_desc_type = 0;
416 	}
417 
418 	mprsas_free_tm(sc, tm);
419 }
420 
421 
422 /*
423  * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
424  * Otherwise Volume Delete is same as Bare Drive Removal.
425  */
426 void
427 mprsas_prepare_volume_remove(struct mprsas_softc *sassc, uint16_t handle)
428 {
429 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
430 	struct mpr_softc *sc;
431 	struct mpr_command *cm;
432 	struct mprsas_target *targ = NULL;
433 
434 	MPR_FUNCTRACE(sassc->sc);
435 	sc = sassc->sc;
436 
437 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
438 	if (targ == NULL) {
439 		/* FIXME: what is the action? */
440 		/* We don't know about this device? */
441 		mpr_dprint(sc, MPR_ERROR,
442 		   "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
443 		return;
444 	}
445 
446 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
447 
448 	cm = mprsas_alloc_tm(sc);
449 	if (cm == NULL) {
450 		mpr_dprint(sc, MPR_ERROR,
451 		    "%s: command alloc failure\n", __func__);
452 		return;
453 	}
454 
455 	mprsas_rescan_target(sc, targ);
456 
457 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
458 	req->DevHandle = targ->handle;
459 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
460 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
461 
462 	/* SAS Hard Link Reset / SATA Link Reset */
463 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
464 
465 	cm->cm_targ = targ;
466 	cm->cm_data = NULL;
467 	cm->cm_desc.HighPriority.RequestFlags =
468 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
469 	cm->cm_complete = mprsas_remove_volume;
470 	cm->cm_complete_data = (void *)(uintptr_t)handle;
471 
472 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
473 	    __func__, targ->tid);
474 	mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
475 
476 	mpr_map_command(sc, cm);
477 }
478 
479 /*
480  * The MPT3 firmware performs debounce on the link to avoid transient link
481  * errors and false removals.  When it does decide that link has been lost
482  * and a device needs to go away, it expects that the host will perform a
483  * target reset and then an op remove.  The reset has the side-effect of
484  * aborting any outstanding requests for the device, which is required for
485  * the op-remove to succeed.  It's not clear if the host should check for
486  * the device coming back alive after the reset.
487  */
488 void
489 mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle)
490 {
491 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
492 	struct mpr_softc *sc;
493 	struct mpr_command *cm;
494 	struct mprsas_target *targ = NULL;
495 
496 	MPR_FUNCTRACE(sassc->sc);
497 
498 	sc = sassc->sc;
499 
500 	targ = mprsas_find_target_by_handle(sassc, 0, handle);
501 	if (targ == NULL) {
502 		/* FIXME: what is the action? */
503 		/* We don't know about this device? */
504 		mpr_dprint(sc, MPR_ERROR, "%s : invalid handle 0x%x \n",
505 		    __func__, handle);
506 		return;
507 	}
508 
509 	targ->flags |= MPRSAS_TARGET_INREMOVAL;
510 
511 	cm = mprsas_alloc_tm(sc);
512 	if (cm == NULL) {
513 		mpr_dprint(sc, MPR_ERROR, "%s: command alloc failure\n",
514 		    __func__);
515 		return;
516 	}
517 
518 	mprsas_rescan_target(sc, targ);
519 
520 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
521 	memset(req, 0, sizeof(*req));
522 	req->DevHandle = htole16(targ->handle);
523 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
524 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
525 
526 	/* SAS Hard Link Reset / SATA Link Reset */
527 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
528 
529 	cm->cm_targ = targ;
530 	cm->cm_data = NULL;
531 	cm->cm_desc.HighPriority.RequestFlags =
532 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
533 	cm->cm_complete = mprsas_remove_device;
534 	cm->cm_complete_data = (void *)(uintptr_t)handle;
535 
536 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
537 	    __func__, targ->tid);
538 	mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
539 
540 	mpr_map_command(sc, cm);
541 }
542 
543 static void
544 mprsas_remove_device(struct mpr_softc *sc, struct mpr_command *tm)
545 {
546 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
547 	MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
548 	struct mprsas_target *targ;
549 	struct mpr_command *next_cm;
550 	uint16_t handle;
551 
552 	MPR_FUNCTRACE(sc);
553 
554 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
555 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
556 	targ = tm->cm_targ;
557 
558 	/*
559 	 * Currently there should be no way we can hit this case.  It only
560 	 * happens when we have a failure to allocate chain frames, and
561 	 * task management commands don't have S/G lists.
562 	 */
563 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
564 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for remove of "
565 		    "handle %#04x! This should not happen!\n", __func__,
566 		    tm->cm_flags, handle);
567 	}
568 
569 	if (reply == NULL) {
570 		/* XXX retry the remove after the diag reset completes? */
571 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply resetting device "
572 		    "0x%04x\n", __func__, handle);
573 		mprsas_free_tm(sc, tm);
574 		return;
575 	}
576 
577 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
578 	    MPI2_IOCSTATUS_SUCCESS) {
579 		mpr_dprint(sc, MPR_ERROR, "IOCStatus = 0x%x while resetting "
580 		    "device 0x%x\n", le16toh(reply->IOCStatus), handle);
581 	}
582 
583 	mpr_dprint(sc, MPR_XINFO, "Reset aborted %u commands\n",
584 	    le32toh(reply->TerminationCount));
585 	mpr_free_reply(sc, tm->cm_reply_data);
586 	tm->cm_reply = NULL;	/* Ensures the reply won't get re-freed */
587 
588 	/* Reuse the existing command */
589 	req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
590 	memset(req, 0, sizeof(*req));
591 	req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
592 	req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
593 	req->DevHandle = htole16(handle);
594 	tm->cm_data = NULL;
595 	tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
596 	tm->cm_complete = mprsas_remove_complete;
597 	tm->cm_complete_data = (void *)(uintptr_t)handle;
598 
599 	mpr_map_command(sc, tm);
600 
601 	mpr_dprint(sc, MPR_INFO, "clearing target %u handle 0x%04x\n",
602 	    targ->tid, handle);
603 	if (targ->encl_level_valid) {
604 		mpr_dprint(sc, MPR_INFO, "At enclosure level %d, slot %d, "
605 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
606 		    targ->connector_name);
607 	}
608 	TAILQ_FOREACH_SAFE(tm, &targ->commands, cm_link, next_cm) {
609 		union ccb *ccb;
610 
611 		mpr_dprint(sc, MPR_XINFO, "Completing missed command %p\n", tm);
612 		ccb = tm->cm_complete_data;
613 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
614 		mprsas_scsiio_complete(sc, tm);
615 	}
616 }
617 
618 static void
619 mprsas_remove_complete(struct mpr_softc *sc, struct mpr_command *tm)
620 {
621 	MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
622 	uint16_t handle;
623 	struct mprsas_target *targ;
624 	struct mprsas_lun *lun;
625 
626 	MPR_FUNCTRACE(sc);
627 
628 	reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
629 	handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
630 
631 	/*
632 	 * Currently there should be no way we can hit this case.  It only
633 	 * happens when we have a failure to allocate chain frames, and
634 	 * task management commands don't have S/G lists.
635 	 */
636 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
637 		mpr_dprint(sc, MPR_XINFO, "%s: cm_flags = %#x for remove of "
638 		    "handle %#04x! This should not happen!\n", __func__,
639 		    tm->cm_flags, handle);
640 		mprsas_free_tm(sc, tm);
641 		return;
642 	}
643 
644 	if (reply == NULL) {
645 		/* most likely a chip reset */
646 		mpr_dprint(sc, MPR_FAULT, "%s NULL reply removing device "
647 		    "0x%04x\n", __func__, handle);
648 		mprsas_free_tm(sc, tm);
649 		return;
650 	}
651 
652 	mpr_dprint(sc, MPR_XINFO, "%s on handle 0x%04x, IOCStatus= 0x%x\n",
653 	    __func__, handle, le16toh(reply->IOCStatus));
654 
655 	/*
656 	 * Don't clear target if remove fails because things will get confusing.
657 	 * Leave the devname and sasaddr intact so that we know to avoid reusing
658 	 * this target id if possible, and so we can assign the same target id
659 	 * to this device if it comes back in the future.
660 	 */
661 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
662 	    MPI2_IOCSTATUS_SUCCESS) {
663 		targ = tm->cm_targ;
664 		targ->handle = 0x0;
665 		targ->encl_handle = 0x0;
666 		targ->encl_level_valid = 0x0;
667 		targ->encl_level = 0x0;
668 		targ->connector_name[0] = ' ';
669 		targ->connector_name[1] = ' ';
670 		targ->connector_name[2] = ' ';
671 		targ->connector_name[3] = ' ';
672 		targ->encl_slot = 0x0;
673 		targ->exp_dev_handle = 0x0;
674 		targ->phy_num = 0x0;
675 		targ->linkrate = 0x0;
676 		targ->devinfo = 0x0;
677 		targ->flags = 0x0;
678 		targ->scsi_req_desc_type = 0;
679 
680 		while (!SLIST_EMPTY(&targ->luns)) {
681 			lun = SLIST_FIRST(&targ->luns);
682 			SLIST_REMOVE_HEAD(&targ->luns, lun_link);
683 			free(lun, M_MPR);
684 		}
685 	}
686 
687 	mprsas_free_tm(sc, tm);
688 }
689 
690 static int
691 mprsas_register_events(struct mpr_softc *sc)
692 {
693 	uint8_t events[16];
694 
695 	bzero(events, 16);
696 	setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
697 	setbit(events, MPI2_EVENT_SAS_DISCOVERY);
698 	setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
699 	setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
700 	setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
701 	setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
702 	setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
703 	setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
704 	setbit(events, MPI2_EVENT_IR_VOLUME);
705 	setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
706 	setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
707 	setbit(events, MPI2_EVENT_TEMP_THRESHOLD);
708 	setbit(events, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
709 
710 	mpr_register_events(sc, events, mprsas_evt_handler, NULL,
711 	    &sc->sassc->mprsas_eh);
712 
713 	return (0);
714 }
715 
716 int
717 mpr_attach_sas(struct mpr_softc *sc)
718 {
719 	struct mprsas_softc *sassc;
720 	cam_status status;
721 	int unit, error = 0;
722 
723 	MPR_FUNCTRACE(sc);
724 
725 	sassc = malloc(sizeof(struct mprsas_softc), M_MPR, M_WAITOK|M_ZERO);
726 	if (!sassc) {
727 		device_printf(sc->mpr_dev, "Cannot allocate memory %s %d\n",
728 		    __func__, __LINE__);
729 		return (ENOMEM);
730 	}
731 
732 	/*
733 	 * XXX MaxTargets could change during a reinit.  Since we don't
734 	 * resize the targets[] array during such an event, cache the value
735 	 * of MaxTargets here so that we don't get into trouble later.  This
736 	 * should move into the reinit logic.
737 	 */
738 	sassc->maxtargets = sc->facts->MaxTargets;
739 	sassc->targets = malloc(sizeof(struct mprsas_target) *
740 	    sassc->maxtargets, M_MPR, M_WAITOK|M_ZERO);
741 	if (!sassc->targets) {
742 		device_printf(sc->mpr_dev, "Cannot allocate memory %s %d\n",
743 		    __func__, __LINE__);
744 		free(sassc, M_MPR);
745 		return (ENOMEM);
746 	}
747 	sc->sassc = sassc;
748 	sassc->sc = sc;
749 
750 	if ((sassc->devq = cam_simq_alloc(sc->num_reqs)) == NULL) {
751 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate SIMQ\n");
752 		error = ENOMEM;
753 		goto out;
754 	}
755 
756 	unit = device_get_unit(sc->mpr_dev);
757 	sassc->sim = cam_sim_alloc(mprsas_action, mprsas_poll, "mpr", sassc,
758 	    unit, &sc->mpr_mtx, sc->num_reqs, sc->num_reqs, sassc->devq);
759 	if (sassc->sim == NULL) {
760 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate SIM\n");
761 		error = EINVAL;
762 		goto out;
763 	}
764 
765 	TAILQ_INIT(&sassc->ev_queue);
766 
767 	/* Initialize taskqueue for Event Handling */
768 	TASK_INIT(&sassc->ev_task, 0, mprsas_firmware_event_work, sc);
769 	sassc->ev_tq = taskqueue_create("mpr_taskq", M_NOWAIT | M_ZERO,
770 	    taskqueue_thread_enqueue, &sassc->ev_tq);
771 	taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
772 	    device_get_nameunit(sc->mpr_dev));
773 
774 	mpr_lock(sc);
775 
776 	/*
777 	 * XXX There should be a bus for every port on the adapter, but since
778 	 * we're just going to fake the topology for now, we'll pretend that
779 	 * everything is just a target on a single bus.
780 	 */
781 	if ((error = xpt_bus_register(sassc->sim, sc->mpr_dev, 0)) != 0) {
782 		mpr_dprint(sc, MPR_ERROR, "Error %d registering SCSI bus\n",
783 		    error);
784 		mpr_unlock(sc);
785 		goto out;
786 	}
787 
788 	/*
789 	 * Assume that discovery events will start right away.
790 	 *
791 	 * Hold off boot until discovery is complete.
792 	 */
793 	sassc->flags |= MPRSAS_IN_STARTUP | MPRSAS_IN_DISCOVERY;
794 	sc->sassc->startup_refcount = 0;
795 	mprsas_startup_increment(sassc);
796 
797 	callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
798 
799 	/*
800 	 * Register for async events so we can determine the EEDP
801 	 * capabilities of devices.
802 	 */
803 	status = xpt_create_path(&sassc->path, /*periph*/NULL,
804 	    cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
805 	    CAM_LUN_WILDCARD);
806 	if (status != CAM_REQ_CMP) {
807 		mpr_printf(sc, "Error %#x creating sim path\n", status);
808 		sassc->path = NULL;
809 	} else {
810 		int event;
811 
812 #if (__FreeBSD_version >= 1000006) || \
813     ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
814 		event = AC_ADVINFO_CHANGED | AC_FOUND_DEVICE;
815 #else
816 		event = AC_FOUND_DEVICE;
817 #endif
818 
819 		/*
820 		 * Prior to the CAM locking improvements, we can't call
821 		 * xpt_register_async() with a particular path specified.
822 		 *
823 		 * If a path isn't specified, xpt_register_async() will
824 		 * generate a wildcard path and acquire the XPT lock while
825 		 * it calls xpt_action() to execute the XPT_SASYNC_CB CCB.
826 		 * It will then drop the XPT lock once that is done.
827 		 *
828 		 * If a path is specified for xpt_register_async(), it will
829 		 * not acquire and drop the XPT lock around the call to
830 		 * xpt_action().  xpt_action() asserts that the caller
831 		 * holds the SIM lock, so the SIM lock has to be held when
832 		 * calling xpt_register_async() when the path is specified.
833 		 *
834 		 * But xpt_register_async calls xpt_for_all_devices(),
835 		 * which calls xptbustraverse(), which will acquire each
836 		 * SIM lock.  When it traverses our particular bus, it will
837 		 * necessarily acquire the SIM lock, which will lead to a
838 		 * recursive lock acquisition.
839 		 *
840 		 * The CAM locking changes fix this problem by acquiring
841 		 * the XPT topology lock around bus traversal in
842 		 * xptbustraverse(), so the caller can hold the SIM lock
843 		 * and it does not cause a recursive lock acquisition.
844 		 *
845 		 * These __FreeBSD_version values are approximate, especially
846 		 * for stable/10, which is two months later than the actual
847 		 * change.
848 		 */
849 
850 #if (__FreeBSD_version < 1000703) || \
851     ((__FreeBSD_version >= 1100000) && (__FreeBSD_version < 1100002))
852 		mpr_unlock(sc);
853 		status = xpt_register_async(event, mprsas_async, sc,
854 					    NULL);
855 		mpr_lock(sc);
856 #else
857 		status = xpt_register_async(event, mprsas_async, sc,
858 					    sassc->path);
859 #endif
860 
861 		if (status != CAM_REQ_CMP) {
862 			mpr_dprint(sc, MPR_ERROR,
863 			    "Error %#x registering async handler for "
864 			    "AC_ADVINFO_CHANGED events\n", status);
865 			xpt_free_path(sassc->path);
866 			sassc->path = NULL;
867 		}
868 	}
869 	if (status != CAM_REQ_CMP) {
870 		/*
871 		 * EEDP use is the exception, not the rule.
872 		 * Warn the user, but do not fail to attach.
873 		 */
874 		mpr_printf(sc, "EEDP capabilities disabled.\n");
875 	}
876 
877 	mpr_unlock(sc);
878 
879 	mprsas_register_events(sc);
880 out:
881 	if (error)
882 		mpr_detach_sas(sc);
883 	return (error);
884 }
885 
886 int
887 mpr_detach_sas(struct mpr_softc *sc)
888 {
889 	struct mprsas_softc *sassc;
890 	struct mprsas_lun *lun, *lun_tmp;
891 	struct mprsas_target *targ;
892 	int i;
893 
894 	MPR_FUNCTRACE(sc);
895 
896 	if (sc->sassc == NULL)
897 		return (0);
898 
899 	sassc = sc->sassc;
900 	mpr_deregister_events(sc, sassc->mprsas_eh);
901 
902 	/*
903 	 * Drain and free the event handling taskqueue with the lock
904 	 * unheld so that any parallel processing tasks drain properly
905 	 * without deadlocking.
906 	 */
907 	if (sassc->ev_tq != NULL)
908 		taskqueue_free(sassc->ev_tq);
909 
910 	/* Make sure CAM doesn't wedge if we had to bail out early. */
911 	mpr_lock(sc);
912 
913 	/* Deregister our async handler */
914 	if (sassc->path != NULL) {
915 		xpt_register_async(0, mprsas_async, sc, sassc->path);
916 		xpt_free_path(sassc->path);
917 		sassc->path = NULL;
918 	}
919 
920 	if (sassc->flags & MPRSAS_IN_STARTUP)
921 		xpt_release_simq(sassc->sim, 1);
922 
923 	if (sassc->sim != NULL) {
924 		xpt_bus_deregister(cam_sim_path(sassc->sim));
925 		cam_sim_free(sassc->sim, FALSE);
926 	}
927 
928 	mpr_unlock(sc);
929 
930 	if (sassc->devq != NULL)
931 		cam_simq_free(sassc->devq);
932 
933 	for (i = 0; i < sassc->maxtargets; i++) {
934 		targ = &sassc->targets[i];
935 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
936 			free(lun, M_MPR);
937 		}
938 	}
939 	free(sassc->targets, M_MPR);
940 	free(sassc, M_MPR);
941 	sc->sassc = NULL;
942 
943 	return (0);
944 }
945 
946 void
947 mprsas_discovery_end(struct mprsas_softc *sassc)
948 {
949 	struct mpr_softc *sc = sassc->sc;
950 
951 	MPR_FUNCTRACE(sc);
952 
953 	if (sassc->flags & MPRSAS_DISCOVERY_TIMEOUT_PENDING)
954 		callout_stop(&sassc->discovery_callout);
955 
956 }
957 
958 static void
959 mprsas_action(struct cam_sim *sim, union ccb *ccb)
960 {
961 	struct mprsas_softc *sassc;
962 
963 	sassc = cam_sim_softc(sim);
964 
965 	MPR_FUNCTRACE(sassc->sc);
966 	mpr_dprint(sassc->sc, MPR_TRACE, "ccb func_code 0x%x\n",
967 	    ccb->ccb_h.func_code);
968 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
969 
970 	switch (ccb->ccb_h.func_code) {
971 	case XPT_PATH_INQ:
972 	{
973 		struct ccb_pathinq *cpi = &ccb->cpi;
974 		struct mpr_softc *sc = sassc->sc;
975 		uint8_t sges_per_frame;
976 
977 		cpi->version_num = 1;
978 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
979 		cpi->target_sprt = 0;
980 #if (__FreeBSD_version >= 1000039) || \
981     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
982 		cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
983 #else
984 		cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
985 #endif
986 		cpi->hba_eng_cnt = 0;
987 		cpi->max_target = sassc->maxtargets - 1;
988 		cpi->max_lun = 255;
989 		cpi->initiator_id = sassc->maxtargets - 1;
990 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
991 		strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN);
992 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
993 		cpi->unit_number = cam_sim_unit(sim);
994 		cpi->bus_id = cam_sim_bus(sim);
995 		/*
996 		 * XXXSLM-I think this needs to change based on config page or
997 		 * something instead of hardcoded to 150000.
998 		 */
999 		cpi->base_transfer_speed = 150000;
1000 		cpi->transport = XPORT_SAS;
1001 		cpi->transport_version = 0;
1002 		cpi->protocol = PROTO_SCSI;
1003 		cpi->protocol_version = SCSI_REV_SPC;
1004 
1005 		/*
1006 		 * Max IO Size is Page Size * the following:
1007 		 * ((SGEs per frame - 1 for chain element) *
1008 		 * Max Chain Depth) + 1 for no chain needed in last frame
1009 		 *
1010 		 * If user suggests a Max IO size to use, use the smaller of the
1011 		 * user's value and the calculated value as long as the user's
1012 		 * value is larger than 0. The user's value is in pages.
1013 		 */
1014 		sges_per_frame = (sc->chain_frame_size /
1015 		    sizeof(MPI2_IEEE_SGE_SIMPLE64)) - 1;
1016 		cpi->maxio = (sges_per_frame * sc->facts->MaxChainDepth) + 1;
1017 		cpi->maxio *= PAGE_SIZE;
1018 		if ((sc->max_io_pages > 0) && (sc->max_io_pages * PAGE_SIZE <
1019 		    cpi->maxio))
1020 			cpi->maxio = sc->max_io_pages * PAGE_SIZE;
1021 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1022 		break;
1023 	}
1024 	case XPT_GET_TRAN_SETTINGS:
1025 	{
1026 		struct ccb_trans_settings	*cts;
1027 		struct ccb_trans_settings_sas	*sas;
1028 		struct ccb_trans_settings_scsi	*scsi;
1029 		struct mprsas_target *targ;
1030 
1031 		cts = &ccb->cts;
1032 		sas = &cts->xport_specific.sas;
1033 		scsi = &cts->proto_specific.scsi;
1034 
1035 		KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
1036 		    ("Target %d out of bounds in XPT_GET_TRAN_SETTINGS\n",
1037 		    cts->ccb_h.target_id));
1038 		targ = &sassc->targets[cts->ccb_h.target_id];
1039 		if (targ->handle == 0x0) {
1040 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1041 			break;
1042 		}
1043 
1044 		cts->protocol_version = SCSI_REV_SPC2;
1045 		cts->transport = XPORT_SAS;
1046 		cts->transport_version = 0;
1047 
1048 		sas->valid = CTS_SAS_VALID_SPEED;
1049 		switch (targ->linkrate) {
1050 		case 0x08:
1051 			sas->bitrate = 150000;
1052 			break;
1053 		case 0x09:
1054 			sas->bitrate = 300000;
1055 			break;
1056 		case 0x0a:
1057 			sas->bitrate = 600000;
1058 			break;
1059 		case 0x0b:
1060 			sas->bitrate = 1200000;
1061 			break;
1062 		default:
1063 			sas->valid = 0;
1064 		}
1065 
1066 		cts->protocol = PROTO_SCSI;
1067 		scsi->valid = CTS_SCSI_VALID_TQ;
1068 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1069 
1070 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1071 		break;
1072 	}
1073 	case XPT_CALC_GEOMETRY:
1074 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
1075 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1076 		break;
1077 	case XPT_RESET_DEV:
1078 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action "
1079 		    "XPT_RESET_DEV\n");
1080 		mprsas_action_resetdev(sassc, ccb);
1081 		return;
1082 	case XPT_RESET_BUS:
1083 	case XPT_ABORT:
1084 	case XPT_TERM_IO:
1085 		mpr_dprint(sassc->sc, MPR_XINFO, "mprsas_action faking success "
1086 		    "for abort or reset\n");
1087 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1088 		break;
1089 	case XPT_SCSI_IO:
1090 		mprsas_action_scsiio(sassc, ccb);
1091 		return;
1092 #if __FreeBSD_version >= 900026
1093 	case XPT_SMP_IO:
1094 		mprsas_action_smpio(sassc, ccb);
1095 		return;
1096 #endif
1097 	default:
1098 		mprsas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1099 		break;
1100 	}
1101 	xpt_done(ccb);
1102 
1103 }
1104 
1105 static void
1106 mprsas_announce_reset(struct mpr_softc *sc, uint32_t ac_code,
1107     target_id_t target_id, lun_id_t lun_id)
1108 {
1109 	path_id_t path_id = cam_sim_path(sc->sassc->sim);
1110 	struct cam_path *path;
1111 
1112 	mpr_dprint(sc, MPR_XINFO, "%s code %x target %d lun %jx\n", __func__,
1113 	    ac_code, target_id, (uintmax_t)lun_id);
1114 
1115 	if (xpt_create_path(&path, NULL,
1116 		path_id, target_id, lun_id) != CAM_REQ_CMP) {
1117 		mpr_dprint(sc, MPR_ERROR, "unable to create path for reset "
1118 		    "notification\n");
1119 		return;
1120 	}
1121 
1122 	xpt_async(ac_code, path, NULL);
1123 	xpt_free_path(path);
1124 }
1125 
1126 static void
1127 mprsas_complete_all_commands(struct mpr_softc *sc)
1128 {
1129 	struct mpr_command *cm;
1130 	int i;
1131 	int completed;
1132 
1133 	MPR_FUNCTRACE(sc);
1134 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1135 
1136 	/* complete all commands with a NULL reply */
1137 	for (i = 1; i < sc->num_reqs; i++) {
1138 		cm = &sc->commands[i];
1139 		cm->cm_reply = NULL;
1140 		completed = 0;
1141 
1142 		if (cm->cm_flags & MPR_CM_FLAGS_POLLED)
1143 			cm->cm_flags |= MPR_CM_FLAGS_COMPLETE;
1144 
1145 		if (cm->cm_complete != NULL) {
1146 			mprsas_log_command(cm, MPR_RECOVERY,
1147 			    "completing cm %p state %x ccb %p for diag reset\n",
1148 			    cm, cm->cm_state, cm->cm_ccb);
1149 			cm->cm_complete(sc, cm);
1150 			completed = 1;
1151 		}
1152 
1153 		if (cm->cm_flags & MPR_CM_FLAGS_WAKEUP) {
1154 			mprsas_log_command(cm, MPR_RECOVERY,
1155 			    "waking up cm %p state %x ccb %p for diag reset\n",
1156 			    cm, cm->cm_state, cm->cm_ccb);
1157 			wakeup(cm);
1158 			completed = 1;
1159 		}
1160 
1161 		if (cm->cm_sc->io_cmds_active != 0) {
1162 			cm->cm_sc->io_cmds_active--;
1163 		} else {
1164 			mpr_dprint(cm->cm_sc, MPR_INFO, "Warning: "
1165 			    "io_cmds_active is out of sync - resynching to "
1166 			    "0\n");
1167 		}
1168 
1169 		if ((completed == 0) && (cm->cm_state != MPR_CM_STATE_FREE)) {
1170 			/* this should never happen, but if it does, log */
1171 			mprsas_log_command(cm, MPR_RECOVERY,
1172 			    "cm %p state %x flags 0x%x ccb %p during diag "
1173 			    "reset\n", cm, cm->cm_state, cm->cm_flags,
1174 			    cm->cm_ccb);
1175 		}
1176 	}
1177 }
1178 
1179 void
1180 mprsas_handle_reinit(struct mpr_softc *sc)
1181 {
1182 	int i;
1183 
1184 	/* Go back into startup mode and freeze the simq, so that CAM
1185 	 * doesn't send any commands until after we've rediscovered all
1186 	 * targets and found the proper device handles for them.
1187 	 *
1188 	 * After the reset, portenable will trigger discovery, and after all
1189 	 * discovery-related activities have finished, the simq will be
1190 	 * released.
1191 	 */
1192 	mpr_dprint(sc, MPR_INIT, "%s startup\n", __func__);
1193 	sc->sassc->flags |= MPRSAS_IN_STARTUP;
1194 	sc->sassc->flags |= MPRSAS_IN_DISCOVERY;
1195 	mprsas_startup_increment(sc->sassc);
1196 
1197 	/* notify CAM of a bus reset */
1198 	mprsas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1199 	    CAM_LUN_WILDCARD);
1200 
1201 	/* complete and cleanup after all outstanding commands */
1202 	mprsas_complete_all_commands(sc);
1203 
1204 	mpr_dprint(sc, MPR_INIT, "%s startup %u after command completion\n",
1205 	    __func__, sc->sassc->startup_refcount);
1206 
1207 	/* zero all the target handles, since they may change after the
1208 	 * reset, and we have to rediscover all the targets and use the new
1209 	 * handles.
1210 	 */
1211 	for (i = 0; i < sc->sassc->maxtargets; i++) {
1212 		if (sc->sassc->targets[i].outstanding != 0)
1213 			mpr_dprint(sc, MPR_INIT, "target %u outstanding %u\n",
1214 			    i, sc->sassc->targets[i].outstanding);
1215 		sc->sassc->targets[i].handle = 0x0;
1216 		sc->sassc->targets[i].exp_dev_handle = 0x0;
1217 		sc->sassc->targets[i].outstanding = 0;
1218 		sc->sassc->targets[i].flags = MPRSAS_TARGET_INDIAGRESET;
1219 	}
1220 }
1221 static void
1222 mprsas_tm_timeout(void *data)
1223 {
1224 	struct mpr_command *tm = data;
1225 	struct mpr_softc *sc = tm->cm_sc;
1226 
1227 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1228 
1229 	mprsas_log_command(tm, MPR_INFO|MPR_RECOVERY, "task mgmt %p timed "
1230 	    "out\n", tm);
1231 	mpr_reinit(sc);
1232 }
1233 
1234 static void
1235 mprsas_logical_unit_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1236 {
1237 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1238 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1239 	unsigned int cm_count = 0;
1240 	struct mpr_command *cm;
1241 	struct mprsas_target *targ;
1242 
1243 	callout_stop(&tm->cm_callout);
1244 
1245 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1246 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1247 	targ = tm->cm_targ;
1248 
1249 	/*
1250 	 * Currently there should be no way we can hit this case.  It only
1251 	 * happens when we have a failure to allocate chain frames, and
1252 	 * task management commands don't have S/G lists.
1253 	 */
1254 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
1255 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for LUN reset! "
1256 		    "This should not happen!\n", __func__, tm->cm_flags);
1257 		mprsas_free_tm(sc, tm);
1258 		return;
1259 	}
1260 
1261 	if (reply == NULL) {
1262 		mprsas_log_command(tm, MPR_RECOVERY, "NULL reset reply for tm "
1263 		    "%p\n", tm);
1264 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1265 			/* this completion was due to a reset, just cleanup */
1266 			targ->tm = NULL;
1267 			mprsas_free_tm(sc, tm);
1268 		}
1269 		else {
1270 			/* we should have gotten a reply. */
1271 			mpr_reinit(sc);
1272 		}
1273 		return;
1274 	}
1275 
1276 	mprsas_log_command(tm, MPR_RECOVERY,
1277 	    "logical unit reset status 0x%x code 0x%x count %u\n",
1278 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1279 	    le32toh(reply->TerminationCount));
1280 
1281 	/* See if there are any outstanding commands for this LUN.
1282 	 * This could be made more efficient by using a per-LU data
1283 	 * structure of some sort.
1284 	 */
1285 	TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1286 		if (cm->cm_lun == tm->cm_lun)
1287 			cm_count++;
1288 	}
1289 
1290 	if (cm_count == 0) {
1291 		mprsas_log_command(tm, MPR_RECOVERY|MPR_INFO,
1292 		    "logical unit %u finished recovery after reset\n",
1293 		    tm->cm_lun, tm);
1294 
1295 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1296 		    tm->cm_lun);
1297 
1298 		/* we've finished recovery for this logical unit.  check and
1299 		 * see if some other logical unit has a timedout command
1300 		 * that needs to be processed.
1301 		 */
1302 		cm = TAILQ_FIRST(&targ->timedout_commands);
1303 		if (cm) {
1304 			mprsas_send_abort(sc, tm, cm);
1305 		}
1306 		else {
1307 			targ->tm = NULL;
1308 			mprsas_free_tm(sc, tm);
1309 		}
1310 	}
1311 	else {
1312 		/* if we still have commands for this LUN, the reset
1313 		 * effectively failed, regardless of the status reported.
1314 		 * Escalate to a target reset.
1315 		 */
1316 		mprsas_log_command(tm, MPR_RECOVERY,
1317 		    "logical unit reset complete for tm %p, but still have %u "
1318 		    "command(s)\n", tm, cm_count);
1319 		mprsas_send_reset(sc, tm,
1320 		    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1321 	}
1322 }
1323 
1324 static void
1325 mprsas_target_reset_complete(struct mpr_softc *sc, struct mpr_command *tm)
1326 {
1327 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1328 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1329 	struct mprsas_target *targ;
1330 
1331 	callout_stop(&tm->cm_callout);
1332 
1333 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1334 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1335 	targ = tm->cm_targ;
1336 
1337 	/*
1338 	 * Currently there should be no way we can hit this case.  It only
1339 	 * happens when we have a failure to allocate chain frames, and
1340 	 * task management commands don't have S/G lists.
1341 	 */
1342 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
1343 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for target "
1344 		    "reset! This should not happen!\n", __func__, tm->cm_flags);
1345 		mprsas_free_tm(sc, tm);
1346 		return;
1347 	}
1348 
1349 	if (reply == NULL) {
1350 		mprsas_log_command(tm, MPR_RECOVERY, "NULL reset reply for tm "
1351 		    "%p\n", tm);
1352 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1353 			/* this completion was due to a reset, just cleanup */
1354 			targ->tm = NULL;
1355 			mprsas_free_tm(sc, tm);
1356 		}
1357 		else {
1358 			/* we should have gotten a reply. */
1359 			mpr_reinit(sc);
1360 		}
1361 		return;
1362 	}
1363 
1364 	mprsas_log_command(tm, MPR_RECOVERY,
1365 	    "target reset status 0x%x code 0x%x count %u\n",
1366 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1367 	    le32toh(reply->TerminationCount));
1368 
1369 	if (targ->outstanding == 0) {
1370 		/* we've finished recovery for this target and all
1371 		 * of its logical units.
1372 		 */
1373 		mprsas_log_command(tm, MPR_RECOVERY|MPR_INFO,
1374 		    "recovery finished after target reset\n");
1375 
1376 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1377 		    CAM_LUN_WILDCARD);
1378 
1379 		targ->tm = NULL;
1380 		mprsas_free_tm(sc, tm);
1381 	}
1382 	else {
1383 		/* after a target reset, if this target still has
1384 		 * outstanding commands, the reset effectively failed,
1385 		 * regardless of the status reported.  escalate.
1386 		 */
1387 		mprsas_log_command(tm, MPR_RECOVERY,
1388 		    "target reset complete for tm %p, but still have %u "
1389 		    "command(s)\n", tm, targ->outstanding);
1390 		mpr_reinit(sc);
1391 	}
1392 }
1393 
1394 #define MPR_RESET_TIMEOUT 30
1395 
1396 int
1397 mprsas_send_reset(struct mpr_softc *sc, struct mpr_command *tm, uint8_t type)
1398 {
1399 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1400 	struct mprsas_target *target;
1401 	int err;
1402 
1403 	target = tm->cm_targ;
1404 	if (target->handle == 0) {
1405 		mpr_dprint(sc, MPR_ERROR, "%s null devhandle for target_id "
1406 		    "%d\n", __func__, target->tid);
1407 		return -1;
1408 	}
1409 
1410 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1411 	req->DevHandle = htole16(target->handle);
1412 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1413 	req->TaskType = type;
1414 
1415 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1416 		/* XXX Need to handle invalid LUNs */
1417 		MPR_SET_LUN(req->LUN, tm->cm_lun);
1418 		tm->cm_targ->logical_unit_resets++;
1419 		mprsas_log_command(tm, MPR_RECOVERY|MPR_INFO,
1420 		    "sending logical unit reset\n");
1421 		tm->cm_complete = mprsas_logical_unit_reset_complete;
1422 		mprsas_prepare_for_tm(sc, tm, target, tm->cm_lun);
1423 	}
1424 	else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1425 		/*
1426 		 * Target reset method =
1427 		 *     SAS Hard Link Reset / SATA Link Reset
1428 		 */
1429 		req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1430 		tm->cm_targ->target_resets++;
1431 		mprsas_log_command(tm, MPR_RECOVERY|MPR_INFO,
1432 		    "sending target reset\n");
1433 		tm->cm_complete = mprsas_target_reset_complete;
1434 		mprsas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1435 	}
1436 	else {
1437 		mpr_dprint(sc, MPR_ERROR, "unexpected reset type 0x%x\n", type);
1438 		return -1;
1439 	}
1440 
1441 	mpr_dprint(sc, MPR_INFO, "to target %u handle 0x%04x\n", target->tid,
1442 	    target->handle);
1443 	if (target->encl_level_valid) {
1444 		mpr_dprint(sc, MPR_INFO, "At enclosure level %d, slot %d, "
1445 		    "connector name (%4s)\n", target->encl_level,
1446 		    target->encl_slot, target->connector_name);
1447 	}
1448 
1449 	tm->cm_data = NULL;
1450 	tm->cm_desc.HighPriority.RequestFlags =
1451 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1452 	tm->cm_complete_data = (void *)tm;
1453 
1454 	callout_reset(&tm->cm_callout, MPR_RESET_TIMEOUT * hz,
1455 	    mprsas_tm_timeout, tm);
1456 
1457 	err = mpr_map_command(sc, tm);
1458 	if (err)
1459 		mprsas_log_command(tm, MPR_RECOVERY,
1460 		    "error %d sending reset type %u\n", err, type);
1461 
1462 	return err;
1463 }
1464 
1465 
1466 static void
1467 mprsas_abort_complete(struct mpr_softc *sc, struct mpr_command *tm)
1468 {
1469 	struct mpr_command *cm;
1470 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1471 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1472 	struct mprsas_target *targ;
1473 
1474 	callout_stop(&tm->cm_callout);
1475 
1476 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1477 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1478 	targ = tm->cm_targ;
1479 
1480 	/*
1481 	 * Currently there should be no way we can hit this case.  It only
1482 	 * happens when we have a failure to allocate chain frames, and
1483 	 * task management commands don't have S/G lists.
1484 	 */
1485 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
1486 		mprsas_log_command(tm, MPR_RECOVERY,
1487 		    "cm_flags = %#x for abort %p TaskMID %u!\n",
1488 		    tm->cm_flags, tm, le16toh(req->TaskMID));
1489 		mprsas_free_tm(sc, tm);
1490 		return;
1491 	}
1492 
1493 	if (reply == NULL) {
1494 		mprsas_log_command(tm, MPR_RECOVERY,
1495 		    "NULL abort reply for tm %p TaskMID %u\n",
1496 		    tm, le16toh(req->TaskMID));
1497 		if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
1498 			/* this completion was due to a reset, just cleanup */
1499 			targ->tm = NULL;
1500 			mprsas_free_tm(sc, tm);
1501 		}
1502 		else {
1503 			/* we should have gotten a reply. */
1504 			mpr_reinit(sc);
1505 		}
1506 		return;
1507 	}
1508 
1509 	mprsas_log_command(tm, MPR_RECOVERY,
1510 	    "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1511 	    le16toh(req->TaskMID),
1512 	    le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1513 	    le32toh(reply->TerminationCount));
1514 
1515 	cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1516 	if (cm == NULL) {
1517 		/* if there are no more timedout commands, we're done with
1518 		 * error recovery for this target.
1519 		 */
1520 		mprsas_log_command(tm, MPR_RECOVERY,
1521 		    "finished recovery after aborting TaskMID %u\n",
1522 		    le16toh(req->TaskMID));
1523 
1524 		targ->tm = NULL;
1525 		mprsas_free_tm(sc, tm);
1526 	}
1527 	else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1528 		/* abort success, but we have more timedout commands to abort */
1529 		mprsas_log_command(tm, MPR_RECOVERY,
1530 		    "continuing recovery after aborting TaskMID %u\n",
1531 		    le16toh(req->TaskMID));
1532 
1533 		mprsas_send_abort(sc, tm, cm);
1534 	}
1535 	else {
1536 		/* we didn't get a command completion, so the abort
1537 		 * failed as far as we're concerned.  escalate.
1538 		 */
1539 		mprsas_log_command(tm, MPR_RECOVERY,
1540 		    "abort failed for TaskMID %u tm %p\n",
1541 		    le16toh(req->TaskMID), tm);
1542 
1543 		mprsas_send_reset(sc, tm,
1544 		    MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1545 	}
1546 }
1547 
1548 #define MPR_ABORT_TIMEOUT 5
1549 
1550 static int
1551 mprsas_send_abort(struct mpr_softc *sc, struct mpr_command *tm,
1552     struct mpr_command *cm)
1553 {
1554 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1555 	struct mprsas_target *targ;
1556 	int err;
1557 
1558 	targ = cm->cm_targ;
1559 	if (targ->handle == 0) {
1560 		mpr_dprint(sc, MPR_ERROR,"%s null devhandle for target_id %d\n",
1561 		    __func__, cm->cm_ccb->ccb_h.target_id);
1562 		return -1;
1563 	}
1564 
1565 	mprsas_log_command(tm, MPR_RECOVERY|MPR_INFO,
1566 	    "Aborting command %p\n", cm);
1567 
1568 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1569 	req->DevHandle = htole16(targ->handle);
1570 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1571 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1572 
1573 	/* XXX Need to handle invalid LUNs */
1574 	MPR_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1575 
1576 	req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1577 
1578 	tm->cm_data = NULL;
1579 	tm->cm_desc.HighPriority.RequestFlags =
1580 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1581 	tm->cm_complete = mprsas_abort_complete;
1582 	tm->cm_complete_data = (void *)tm;
1583 	tm->cm_targ = cm->cm_targ;
1584 	tm->cm_lun = cm->cm_lun;
1585 
1586 	callout_reset(&tm->cm_callout, MPR_ABORT_TIMEOUT * hz,
1587 	    mprsas_tm_timeout, tm);
1588 
1589 	targ->aborts++;
1590 
1591 	mpr_dprint(sc, MPR_INFO, "Sending reset from %s for target ID %d\n",
1592 	    __func__, targ->tid);
1593 	mprsas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1594 
1595 	err = mpr_map_command(sc, tm);
1596 	if (err)
1597 		mprsas_log_command(tm, MPR_RECOVERY,
1598 		    "error %d sending abort for cm %p SMID %u\n",
1599 		    err, cm, req->TaskMID);
1600 	return err;
1601 }
1602 
1603 static void
1604 mprsas_scsiio_timeout(void *data)
1605 {
1606 	struct mpr_softc *sc;
1607 	struct mpr_command *cm;
1608 	struct mprsas_target *targ;
1609 
1610 	cm = (struct mpr_command *)data;
1611 	sc = cm->cm_sc;
1612 
1613 	MPR_FUNCTRACE(sc);
1614 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1615 
1616 	mpr_dprint(sc, MPR_XINFO, "Timeout checking cm %p\n", cm);
1617 
1618 	/*
1619 	 * Run the interrupt handler to make sure it's not pending.  This
1620 	 * isn't perfect because the command could have already completed
1621 	 * and been re-used, though this is unlikely.
1622 	 */
1623 	mpr_intr_locked(sc);
1624 	if (cm->cm_state == MPR_CM_STATE_FREE) {
1625 		mprsas_log_command(cm, MPR_XINFO,
1626 		    "SCSI command %p almost timed out\n", cm);
1627 		return;
1628 	}
1629 
1630 	if (cm->cm_ccb == NULL) {
1631 		mpr_dprint(sc, MPR_ERROR, "command timeout with NULL ccb\n");
1632 		return;
1633 	}
1634 
1635 	targ = cm->cm_targ;
1636 	targ->timeouts++;
1637 
1638 	mprsas_log_command(cm, MPR_ERROR, "command timeout cm %p ccb %p target "
1639 	    "%u, handle(0x%04x)\n", cm, cm->cm_ccb, targ->tid, targ->handle);
1640 	if (targ->encl_level_valid) {
1641 		mpr_dprint(sc, MPR_ERROR, "At enclosure level %d, slot %d, "
1642 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
1643 		    targ->connector_name);
1644 	}
1645 
1646 	/* XXX first, check the firmware state, to see if it's still
1647 	 * operational.  if not, do a diag reset.
1648 	 */
1649 	mprsas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
1650 	cm->cm_state = MPR_CM_STATE_TIMEDOUT;
1651 	TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1652 
1653 	if (targ->tm != NULL) {
1654 		/* target already in recovery, just queue up another
1655 		 * timedout command to be processed later.
1656 		 */
1657 		mpr_dprint(sc, MPR_RECOVERY, "queued timedout cm %p for "
1658 		    "processing by tm %p\n", cm, targ->tm);
1659 	}
1660 	else if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1661 		mpr_dprint(sc, MPR_RECOVERY, "timedout cm %p allocated tm %p\n",
1662 		    cm, targ->tm);
1663 
1664 		/* start recovery by aborting the first timedout command */
1665 		mprsas_send_abort(sc, targ->tm, cm);
1666 	}
1667 	else {
1668 		/* XXX queue this target up for recovery once a TM becomes
1669 		 * available.  The firmware only has a limited number of
1670 		 * HighPriority credits for the high priority requests used
1671 		 * for task management, and we ran out.
1672 		 *
1673 		 * Isilon: don't worry about this for now, since we have
1674 		 * more credits than disks in an enclosure, and limit
1675 		 * ourselves to one TM per target for recovery.
1676 		 */
1677 		mpr_dprint(sc, MPR_RECOVERY, "timedout cm %p failed to "
1678 		    "allocate a tm\n", cm);
1679 	}
1680 }
1681 
1682 static void
1683 mprsas_action_scsiio(struct mprsas_softc *sassc, union ccb *ccb)
1684 {
1685 	MPI2_SCSI_IO_REQUEST *req;
1686 	struct ccb_scsiio *csio;
1687 	struct mpr_softc *sc;
1688 	struct mprsas_target *targ;
1689 	struct mprsas_lun *lun;
1690 	struct mpr_command *cm;
1691 	uint8_t i, lba_byte, *ref_tag_addr;
1692 	uint16_t eedp_flags;
1693 	uint32_t mpi_control;
1694 
1695 	sc = sassc->sc;
1696 	MPR_FUNCTRACE(sc);
1697 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1698 
1699 	csio = &ccb->csio;
1700 	KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1701 	    ("Target %d out of bounds in XPT_SCSI_IO\n",
1702 	     csio->ccb_h.target_id));
1703 	targ = &sassc->targets[csio->ccb_h.target_id];
1704 	mpr_dprint(sc, MPR_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1705 	if (targ->handle == 0x0) {
1706 		mpr_dprint(sc, MPR_ERROR, "%s NULL handle for target %u\n",
1707 		    __func__, csio->ccb_h.target_id);
1708 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1709 		xpt_done(ccb);
1710 		return;
1711 	}
1712 	if (targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT) {
1713 		mpr_dprint(sc, MPR_ERROR, "%s Raid component no SCSI IO "
1714 		    "supported %u\n", __func__, csio->ccb_h.target_id);
1715 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1716 		xpt_done(ccb);
1717 		return;
1718 	}
1719 	/*
1720 	 * Sometimes, it is possible to get a command that is not "In
1721 	 * Progress" and was actually aborted by the upper layer.  Check for
1722 	 * this here and complete the command without error.
1723 	 */
1724 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1725 		mpr_dprint(sc, MPR_TRACE, "%s Command is not in progress for "
1726 		    "target %u\n", __func__, csio->ccb_h.target_id);
1727 		xpt_done(ccb);
1728 		return;
1729 	}
1730 	/*
1731 	 * If devinfo is 0 this will be a volume.  In that case don't tell CAM
1732 	 * that the volume has timed out.  We want volumes to be enumerated
1733 	 * until they are deleted/removed, not just failed.
1734 	 */
1735 	if (targ->flags & MPRSAS_TARGET_INREMOVAL) {
1736 		if (targ->devinfo == 0)
1737 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
1738 		else
1739 			mprsas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
1740 		xpt_done(ccb);
1741 		return;
1742 	}
1743 
1744 	if ((sc->mpr_flags & MPR_FLAGS_SHUTDOWN) != 0) {
1745 		mpr_dprint(sc, MPR_INFO, "%s shutting down\n", __func__);
1746 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1747 		xpt_done(ccb);
1748 		return;
1749 	}
1750 
1751 	/*
1752 	 * If target has a reset in progress, freeze the devq and return.  The
1753 	 * devq will be released when the TM reset is finished.
1754 	 */
1755 	if (targ->flags & MPRSAS_TARGET_INRESET) {
1756 		ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1757 		mpr_dprint(sc, MPR_INFO, "%s: Freezing devq for target ID %d\n",
1758 		    __func__, targ->tid);
1759 		xpt_freeze_devq(ccb->ccb_h.path, 1);
1760 		xpt_done(ccb);
1761 		return;
1762 	}
1763 
1764 	cm = mpr_alloc_command(sc);
1765 	if (cm == NULL || (sc->mpr_flags & MPR_FLAGS_DIAGRESET)) {
1766 		if (cm != NULL) {
1767 			mpr_free_command(sc, cm);
1768 		}
1769 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
1770 			xpt_freeze_simq(sassc->sim, 1);
1771 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
1772 		}
1773 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1774 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1775 		xpt_done(ccb);
1776 		return;
1777 	}
1778 
1779 	req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1780 	bzero(req, sizeof(*req));
1781 	req->DevHandle = htole16(targ->handle);
1782 	req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1783 	req->MsgFlags = 0;
1784 	req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1785 	req->SenseBufferLength = MPR_SENSE_LEN;
1786 	req->SGLFlags = 0;
1787 	req->ChainOffset = 0;
1788 	req->SGLOffset0 = 24;	/* 32bit word offset to the SGL */
1789 	req->SGLOffset1= 0;
1790 	req->SGLOffset2= 0;
1791 	req->SGLOffset3= 0;
1792 	req->SkipCount = 0;
1793 	req->DataLength = htole32(csio->dxfer_len);
1794 	req->BidirectionalDataLength = 0;
1795 	req->IoFlags = htole16(csio->cdb_len);
1796 	req->EEDPFlags = 0;
1797 
1798 	/* Note: BiDirectional transfers are not supported */
1799 	switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1800 	case CAM_DIR_IN:
1801 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
1802 		cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
1803 		break;
1804 	case CAM_DIR_OUT:
1805 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1806 		cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
1807 		break;
1808 	case CAM_DIR_NONE:
1809 	default:
1810 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1811 		break;
1812 	}
1813 
1814 	if (csio->cdb_len == 32)
1815 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1816 	/*
1817 	 * It looks like the hardware doesn't require an explicit tag
1818 	 * number for each transaction.  SAM Task Management not supported
1819 	 * at the moment.
1820 	 */
1821 	switch (csio->tag_action) {
1822 	case MSG_HEAD_OF_Q_TAG:
1823 		mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1824 		break;
1825 	case MSG_ORDERED_Q_TAG:
1826 		mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1827 		break;
1828 	case MSG_ACA_TASK:
1829 		mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1830 		break;
1831 	case CAM_TAG_ACTION_NONE:
1832 	case MSG_SIMPLE_Q_TAG:
1833 	default:
1834 		mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1835 		break;
1836 	}
1837 	mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
1838 	req->Control = htole32(mpi_control);
1839 
1840 	if (MPR_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1841 		mpr_free_command(sc, cm);
1842 		mprsas_set_ccbstatus(ccb, CAM_LUN_INVALID);
1843 		xpt_done(ccb);
1844 		return;
1845 	}
1846 
1847 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
1848 		bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1849 	else {
1850 		KASSERT(csio->cdb_len <= IOCDBLEN,
1851 		    ("cdb_len %d is greater than IOCDBLEN but CAM_CDB_POINTER is not set",
1852 		     csio->cdb_len));
1853 		bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1854 	}
1855 	req->IoFlags = htole16(csio->cdb_len);
1856 
1857 	/*
1858 	 * Check if EEDP is supported and enabled.  If it is then check if the
1859 	 * SCSI opcode could be using EEDP.  If so, make sure the LUN exists and
1860 	 * is formatted for EEDP support.  If all of this is true, set CDB up
1861 	 * for EEDP transfer.
1862 	 */
1863 	eedp_flags = op_code_prot[req->CDB.CDB32[0]];
1864 	if (sc->eedp_enabled && eedp_flags) {
1865 		SLIST_FOREACH(lun, &targ->luns, lun_link) {
1866 			if (lun->lun_id == csio->ccb_h.target_lun) {
1867 				break;
1868 			}
1869 		}
1870 
1871 		if ((lun != NULL) && (lun->eedp_formatted)) {
1872 			req->EEDPBlockSize = htole16(lun->eedp_block_size);
1873 			eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1874 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1875 			    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1876 			req->EEDPFlags = htole16(eedp_flags);
1877 
1878 			/*
1879 			 * If CDB less than 32, fill in Primary Ref Tag with
1880 			 * low 4 bytes of LBA.  If CDB is 32, tag stuff is
1881 			 * already there.  Also, set protection bit.  FreeBSD
1882 			 * currently does not support CDBs bigger than 16, but
1883 			 * the code doesn't hurt, and will be here for the
1884 			 * future.
1885 			 */
1886 			if (csio->cdb_len != 32) {
1887 				lba_byte = (csio->cdb_len == 16) ? 6 : 2;
1888 				ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
1889 				    PrimaryReferenceTag;
1890 				for (i = 0; i < 4; i++) {
1891 					*ref_tag_addr =
1892 					    req->CDB.CDB32[lba_byte + i];
1893 					ref_tag_addr++;
1894 				}
1895 				req->CDB.EEDP32.PrimaryReferenceTag =
1896 				    htole32(req->
1897 				    CDB.EEDP32.PrimaryReferenceTag);
1898 				req->CDB.EEDP32.PrimaryApplicationTagMask =
1899 				    0xFFFF;
1900 				req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) |
1901 				    0x20;
1902 			} else {
1903 				eedp_flags |=
1904 				    MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
1905 				req->EEDPFlags = htole16(eedp_flags);
1906 				req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
1907 				    0x1F) | 0x20;
1908 			}
1909 		}
1910 	}
1911 
1912 	cm->cm_length = csio->dxfer_len;
1913 	if (cm->cm_length != 0) {
1914 		cm->cm_data = ccb;
1915 		cm->cm_flags |= MPR_CM_FLAGS_USE_CCB;
1916 	} else {
1917 		cm->cm_data = NULL;
1918 	}
1919 	cm->cm_sge = &req->SGL;
1920 	cm->cm_sglsize = (32 - 24) * 4;
1921 	cm->cm_complete = mprsas_scsiio_complete;
1922 	cm->cm_complete_data = ccb;
1923 	cm->cm_targ = targ;
1924 	cm->cm_lun = csio->ccb_h.target_lun;
1925 	cm->cm_ccb = ccb;
1926 	/*
1927 	 * If using FP desc type, need to set a bit in IoFlags (SCSI IO is 0)
1928 	 * and set descriptor type.
1929 	 */
1930 	if (targ->scsi_req_desc_type ==
1931 	    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
1932 		req->IoFlags |= MPI25_SCSIIO_IOFLAGS_FAST_PATH;
1933 		cm->cm_desc.FastPathSCSIIO.RequestFlags =
1934 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
1935 		cm->cm_desc.FastPathSCSIIO.DevHandle = htole16(targ->handle);
1936 	} else {
1937 		cm->cm_desc.SCSIIO.RequestFlags =
1938 		    MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1939 		cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
1940 	}
1941 
1942 #if __FreeBSD_version >= 1000029
1943 	callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
1944 	    mprsas_scsiio_timeout, cm, 0);
1945 #else //__FreeBSD_version < 1000029
1946 	callout_reset(&cm->cm_callout, (ccb->ccb_h.timeout * hz) / 1000,
1947 	    mprsas_scsiio_timeout, cm);
1948 #endif //__FreeBSD_version >= 1000029
1949 
1950 	targ->issued++;
1951 	targ->outstanding++;
1952 	TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
1953 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1954 
1955 	mprsas_log_command(cm, MPR_XINFO, "%s cm %p ccb %p outstanding %u\n",
1956 	    __func__, cm, ccb, targ->outstanding);
1957 
1958 	mpr_map_command(sc, cm);
1959 	return;
1960 }
1961 
1962 static void
1963 mpr_response_code(struct mpr_softc *sc, u8 response_code)
1964 {
1965         char *desc;
1966 
1967         switch (response_code) {
1968         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1969                 desc = "task management request completed";
1970                 break;
1971         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1972                 desc = "invalid frame";
1973                 break;
1974         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1975                 desc = "task management request not supported";
1976                 break;
1977         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1978                 desc = "task management request failed";
1979                 break;
1980         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1981                 desc = "task management request succeeded";
1982                 break;
1983         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1984                 desc = "invalid lun";
1985                 break;
1986         case 0xA:
1987                 desc = "overlapped tag attempted";
1988                 break;
1989         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1990                 desc = "task queued, however not sent to target";
1991                 break;
1992         default:
1993                 desc = "unknown";
1994                 break;
1995         }
1996 	mpr_dprint(sc, MPR_XINFO, "response_code(0x%01x): %s\n", response_code,
1997 	    desc);
1998 }
1999 
2000 /**
2001  * mpr_sc_failed_io_info - translated non-succesfull SCSI_IO request
2002  */
2003 static void
2004 mpr_sc_failed_io_info(struct mpr_softc *sc, struct ccb_scsiio *csio,
2005     Mpi2SCSIIOReply_t *mpi_reply, struct mprsas_target *targ)
2006 {
2007 	u32 response_info;
2008 	u8 *response_bytes;
2009 	u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
2010 	    MPI2_IOCSTATUS_MASK;
2011 	u8 scsi_state = mpi_reply->SCSIState;
2012 	u8 scsi_status = mpi_reply->SCSIStatus;
2013 	char *desc_ioc_state = NULL;
2014 	char *desc_scsi_status = NULL;
2015 	char *desc_scsi_state = sc->tmp_string;
2016 	u32 log_info = le32toh(mpi_reply->IOCLogInfo);
2017 
2018 	if (log_info == 0x31170000)
2019 		return;
2020 
2021 	switch (ioc_status) {
2022 	case MPI2_IOCSTATUS_SUCCESS:
2023 		desc_ioc_state = "success";
2024 		break;
2025 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
2026 		desc_ioc_state = "invalid function";
2027 		break;
2028 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2029 		desc_ioc_state = "scsi recovered error";
2030 		break;
2031 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2032 		desc_ioc_state = "scsi invalid dev handle";
2033 		break;
2034 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2035 		desc_ioc_state = "scsi device not there";
2036 		break;
2037 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2038 		desc_ioc_state = "scsi data overrun";
2039 		break;
2040 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2041 		desc_ioc_state = "scsi data underrun";
2042 		break;
2043 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2044 		desc_ioc_state = "scsi io data error";
2045 		break;
2046 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2047 		desc_ioc_state = "scsi protocol error";
2048 		break;
2049 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2050 		desc_ioc_state = "scsi task terminated";
2051 		break;
2052 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2053 		desc_ioc_state = "scsi residual mismatch";
2054 		break;
2055 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2056 		desc_ioc_state = "scsi task mgmt failed";
2057 		break;
2058 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2059 		desc_ioc_state = "scsi ioc terminated";
2060 		break;
2061 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2062 		desc_ioc_state = "scsi ext terminated";
2063 		break;
2064 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2065 		desc_ioc_state = "eedp guard error";
2066 		break;
2067 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2068 		desc_ioc_state = "eedp ref tag error";
2069 		break;
2070 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2071 		desc_ioc_state = "eedp app tag error";
2072 		break;
2073 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
2074 		desc_ioc_state = "insufficient power";
2075 		break;
2076 	default:
2077 		desc_ioc_state = "unknown";
2078 		break;
2079 	}
2080 
2081 	switch (scsi_status) {
2082 	case MPI2_SCSI_STATUS_GOOD:
2083 		desc_scsi_status = "good";
2084 		break;
2085 	case MPI2_SCSI_STATUS_CHECK_CONDITION:
2086 		desc_scsi_status = "check condition";
2087 		break;
2088 	case MPI2_SCSI_STATUS_CONDITION_MET:
2089 		desc_scsi_status = "condition met";
2090 		break;
2091 	case MPI2_SCSI_STATUS_BUSY:
2092 		desc_scsi_status = "busy";
2093 		break;
2094 	case MPI2_SCSI_STATUS_INTERMEDIATE:
2095 		desc_scsi_status = "intermediate";
2096 		break;
2097 	case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2098 		desc_scsi_status = "intermediate condmet";
2099 		break;
2100 	case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2101 		desc_scsi_status = "reservation conflict";
2102 		break;
2103 	case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2104 		desc_scsi_status = "command terminated";
2105 		break;
2106 	case MPI2_SCSI_STATUS_TASK_SET_FULL:
2107 		desc_scsi_status = "task set full";
2108 		break;
2109 	case MPI2_SCSI_STATUS_ACA_ACTIVE:
2110 		desc_scsi_status = "aca active";
2111 		break;
2112 	case MPI2_SCSI_STATUS_TASK_ABORTED:
2113 		desc_scsi_status = "task aborted";
2114 		break;
2115 	default:
2116 		desc_scsi_status = "unknown";
2117 		break;
2118 	}
2119 
2120 	desc_scsi_state[0] = '\0';
2121 	if (!scsi_state)
2122 		desc_scsi_state = " ";
2123 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2124 		strcat(desc_scsi_state, "response info ");
2125 	if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2126 		strcat(desc_scsi_state, "state terminated ");
2127 	if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2128 		strcat(desc_scsi_state, "no status ");
2129 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2130 		strcat(desc_scsi_state, "autosense failed ");
2131 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2132 		strcat(desc_scsi_state, "autosense valid ");
2133 
2134 	mpr_dprint(sc, MPR_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
2135 	    le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
2136 	if (targ->encl_level_valid) {
2137 		mpr_dprint(sc, MPR_XINFO, "At enclosure level %d, slot %d, "
2138 		    "connector name (%4s)\n", targ->encl_level, targ->encl_slot,
2139 		    targ->connector_name);
2140 	}
2141 	/* We can add more detail about underflow data here
2142 	 * TO-DO
2143 	 * */
2144 	mpr_dprint(sc, MPR_XINFO, "\tscsi_status(%s)(0x%02x), "
2145 	    "scsi_state(%s)(0x%02x)\n", desc_scsi_status, scsi_status,
2146 	    desc_scsi_state, scsi_state);
2147 
2148 	if (sc->mpr_debug & MPR_XINFO &&
2149 	    scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2150 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : Start :\n");
2151 		scsi_sense_print(csio);
2152 		mpr_dprint(sc, MPR_XINFO, "-> Sense Buffer Data : End :\n");
2153 	}
2154 
2155 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2156 		response_info = le32toh(mpi_reply->ResponseInfo);
2157 		response_bytes = (u8 *)&response_info;
2158 		mpr_response_code(sc,response_bytes[0]);
2159 	}
2160 }
2161 
2162 static void
2163 mprsas_scsiio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2164 {
2165 	MPI2_SCSI_IO_REPLY *rep;
2166 	union ccb *ccb;
2167 	struct ccb_scsiio *csio;
2168 	struct mprsas_softc *sassc;
2169 	struct scsi_vpd_supported_page_list *vpd_list = NULL;
2170 	u8 *TLR_bits, TLR_on;
2171 	int dir = 0, i;
2172 	u16 alloc_len;
2173 	struct mprsas_target *target;
2174 	target_id_t target_id;
2175 
2176 	MPR_FUNCTRACE(sc);
2177 	mpr_dprint(sc, MPR_TRACE,
2178 	    "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
2179 	    cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
2180 	    cm->cm_targ->outstanding);
2181 
2182 	callout_stop(&cm->cm_callout);
2183 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
2184 
2185 	sassc = sc->sassc;
2186 	ccb = cm->cm_complete_data;
2187 	csio = &ccb->csio;
2188 	target_id = csio->ccb_h.target_id;
2189 	rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
2190 	/*
2191 	 * XXX KDM if the chain allocation fails, does it matter if we do
2192 	 * the sync and unload here?  It is simpler to do it in every case,
2193 	 * assuming it doesn't cause problems.
2194 	 */
2195 	if (cm->cm_data != NULL) {
2196 		if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
2197 			dir = BUS_DMASYNC_POSTREAD;
2198 		else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
2199 			dir = BUS_DMASYNC_POSTWRITE;
2200 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2201 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2202 	}
2203 
2204 	cm->cm_targ->completed++;
2205 	cm->cm_targ->outstanding--;
2206 	TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2207 	ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2208 
2209 	if (cm->cm_state == MPR_CM_STATE_TIMEDOUT) {
2210 		TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
2211 		if (cm->cm_reply != NULL)
2212 			mprsas_log_command(cm, MPR_RECOVERY,
2213 			    "completed timedout cm %p ccb %p during recovery "
2214 			    "ioc %x scsi %x state %x xfer %u\n", cm, cm->cm_ccb,
2215 			    le16toh(rep->IOCStatus), rep->SCSIStatus,
2216 			    rep->SCSIState, le32toh(rep->TransferCount));
2217 		else
2218 			mprsas_log_command(cm, MPR_RECOVERY,
2219 			    "completed timedout cm %p ccb %p during recovery\n",
2220 			    cm, cm->cm_ccb);
2221 	} else if (cm->cm_targ->tm != NULL) {
2222 		if (cm->cm_reply != NULL)
2223 			mprsas_log_command(cm, MPR_RECOVERY,
2224 			    "completed cm %p ccb %p during recovery "
2225 			    "ioc %x scsi %x state %x xfer %u\n",
2226 			    cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2227 			    rep->SCSIStatus, rep->SCSIState,
2228 			    le32toh(rep->TransferCount));
2229 		else
2230 			mprsas_log_command(cm, MPR_RECOVERY,
2231 			    "completed cm %p ccb %p during recovery\n",
2232 			    cm, cm->cm_ccb);
2233 	} else if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0) {
2234 		mprsas_log_command(cm, MPR_RECOVERY,
2235 		    "reset completed cm %p ccb %p\n", cm, cm->cm_ccb);
2236 	}
2237 
2238 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2239 		/*
2240 		 * We ran into an error after we tried to map the command,
2241 		 * so we're getting a callback without queueing the command
2242 		 * to the hardware.  So we set the status here, and it will
2243 		 * be retained below.  We'll go through the "fast path",
2244 		 * because there can be no reply when we haven't actually
2245 		 * gone out to the hardware.
2246 		 */
2247 		mprsas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2248 
2249 		/*
2250 		 * Currently the only error included in the mask is
2251 		 * MPR_CM_FLAGS_CHAIN_FAILED, which means we're out of
2252 		 * chain frames.  We need to freeze the queue until we get
2253 		 * a command that completed without this error, which will
2254 		 * hopefully have some chain frames attached that we can
2255 		 * use.  If we wanted to get smarter about it, we would
2256 		 * only unfreeze the queue in this condition when we're
2257 		 * sure that we're getting some chain frames back.  That's
2258 		 * probably unnecessary.
2259 		 */
2260 		if ((sassc->flags & MPRSAS_QUEUE_FROZEN) == 0) {
2261 			xpt_freeze_simq(sassc->sim, 1);
2262 			sassc->flags |= MPRSAS_QUEUE_FROZEN;
2263 			mpr_dprint(sc, MPR_INFO, "Error sending command, "
2264 			    "freezing SIM queue\n");
2265 		}
2266 	}
2267 
2268 	/*
2269 	 * If this is a Start Stop Unit command and it was issued by the driver
2270 	 * during shutdown, decrement the refcount to account for all of the
2271 	 * commands that were sent.  All SSU commands should be completed before
2272 	 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2273 	 * is TRUE.
2274 	 */
2275 	if (sc->SSU_started && (csio->cdb_io.cdb_bytes[0] == START_STOP_UNIT)) {
2276 		mpr_dprint(sc, MPR_INFO, "Decrementing SSU count.\n");
2277 		sc->SSU_refcount--;
2278 	}
2279 
2280 	/* Take the fast path to completion */
2281 	if (cm->cm_reply == NULL) {
2282 		if (mprsas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2283 			if ((sc->mpr_flags & MPR_FLAGS_DIAGRESET) != 0)
2284 				mprsas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2285 			else {
2286 				mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2287 				csio->scsi_status = SCSI_STATUS_OK;
2288 			}
2289 			if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2290 				ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2291 				sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2292 				mpr_dprint(sc, MPR_XINFO,
2293 				    "Unfreezing SIM queue\n");
2294 			}
2295 		}
2296 
2297 		/*
2298 		 * There are two scenarios where the status won't be
2299 		 * CAM_REQ_CMP.  The first is if MPR_CM_FLAGS_ERROR_MASK is
2300 		 * set, the second is in the MPR_FLAGS_DIAGRESET above.
2301 		 */
2302 		if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2303 			/*
2304 			 * Freeze the dev queue so that commands are
2305 			 * executed in the correct order after error
2306 			 * recovery.
2307 			 */
2308 			ccb->ccb_h.status |= CAM_DEV_QFRZN;
2309 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2310 		}
2311 		mpr_free_command(sc, cm);
2312 		xpt_done(ccb);
2313 		return;
2314 	}
2315 
2316 	mprsas_log_command(cm, MPR_XINFO,
2317 	    "ioc %x scsi %x state %x xfer %u\n",
2318 	    le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2319 	    le32toh(rep->TransferCount));
2320 
2321 	switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2322 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2323 		csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2324 		/* FALLTHROUGH */
2325 	case MPI2_IOCSTATUS_SUCCESS:
2326 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2327 
2328 		if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2329 		    MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2330 			mprsas_log_command(cm, MPR_XINFO, "recovered error\n");
2331 
2332 		/* Completion failed at the transport level. */
2333 		if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2334 		    MPI2_SCSI_STATE_TERMINATED)) {
2335 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2336 			break;
2337 		}
2338 
2339 		/* In a modern packetized environment, an autosense failure
2340 		 * implies that there's not much else that can be done to
2341 		 * recover the command.
2342 		 */
2343 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2344 			mprsas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2345 			break;
2346 		}
2347 
2348 		/*
2349 		 * CAM doesn't care about SAS Response Info data, but if this is
2350 		 * the state check if TLR should be done.  If not, clear the
2351 		 * TLR_bits for the target.
2352 		 */
2353 		if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2354 		    ((le32toh(rep->ResponseInfo) & MPI2_SCSI_RI_MASK_REASONCODE)
2355 		    == MPR_SCSI_RI_INVALID_FRAME)) {
2356 			sc->mapping_table[target_id].TLR_bits =
2357 			    (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2358 		}
2359 
2360 		/*
2361 		 * Intentionally override the normal SCSI status reporting
2362 		 * for these two cases.  These are likely to happen in a
2363 		 * multi-initiator environment, and we want to make sure that
2364 		 * CAM retries these commands rather than fail them.
2365 		 */
2366 		if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2367 		    (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2368 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2369 			break;
2370 		}
2371 
2372 		/* Handle normal status and sense */
2373 		csio->scsi_status = rep->SCSIStatus;
2374 		if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2375 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2376 		else
2377 			mprsas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2378 
2379 		if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2380 			int sense_len, returned_sense_len;
2381 
2382 			returned_sense_len = min(le32toh(rep->SenseCount),
2383 			    sizeof(struct scsi_sense_data));
2384 			if (returned_sense_len < csio->sense_len)
2385 				csio->sense_resid = csio->sense_len -
2386 				    returned_sense_len;
2387 			else
2388 				csio->sense_resid = 0;
2389 
2390 			sense_len = min(returned_sense_len,
2391 			    csio->sense_len - csio->sense_resid);
2392 			bzero(&csio->sense_data, sizeof(csio->sense_data));
2393 			bcopy(cm->cm_sense, &csio->sense_data, sense_len);
2394 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2395 		}
2396 
2397 		/*
2398 		 * Check if this is an INQUIRY command.  If it's a VPD inquiry,
2399 		 * and it's page code 0 (Supported Page List), and there is
2400 		 * inquiry data, and this is for a sequential access device, and
2401 		 * the device is an SSP target, and TLR is supported by the
2402 		 * controller, turn the TLR_bits value ON if page 0x90 is
2403 		 * supported.
2404 		 */
2405 		if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2406 		    (csio->cdb_io.cdb_bytes[1] & SI_EVPD) &&
2407 		    (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2408 		    ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2409 		    (csio->data_ptr != NULL) &&
2410 		    ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2411 		    (sc->control_TLR) &&
2412 		    (sc->mapping_table[target_id].device_info &
2413 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2414 			vpd_list = (struct scsi_vpd_supported_page_list *)
2415 			    csio->data_ptr;
2416 			TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2417 			*TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2418 			TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
2419 			alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) +
2420 			    csio->cdb_io.cdb_bytes[4];
2421 			alloc_len -= csio->resid;
2422 			for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2423 				if (vpd_list->list[i] == 0x90) {
2424 					*TLR_bits = TLR_on;
2425 					break;
2426 				}
2427 			}
2428 		}
2429 
2430 		/*
2431 		 * If this is a SATA direct-access end device, mark it so that
2432 		 * a SCSI StartStopUnit command will be sent to it when the
2433 		 * driver is being shutdown.
2434 		 */
2435 		if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2436 		    (csio->data_ptr != NULL) &&
2437 		    ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2438 		    (sc->mapping_table[target_id].device_info &
2439 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2440 		    ((sc->mapping_table[target_id].device_info &
2441 		    MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2442 		    MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2443 			target = &sassc->targets[target_id];
2444 			target->supports_SSU = TRUE;
2445 			mpr_dprint(sc, MPR_XINFO, "Target %d supports SSU\n",
2446 			    target_id);
2447 		}
2448 		break;
2449 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2450 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2451 		/*
2452 		 * If devinfo is 0 this will be a volume.  In that case don't
2453 		 * tell CAM that the volume is not there.  We want volumes to
2454 		 * be enumerated until they are deleted/removed, not just
2455 		 * failed.
2456 		 */
2457 		if (cm->cm_targ->devinfo == 0)
2458 			mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2459 		else
2460 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2461 		break;
2462 	case MPI2_IOCSTATUS_INVALID_SGL:
2463 		mpr_print_scsiio_cmd(sc, cm);
2464 		mprsas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2465 		break;
2466 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2467 		/*
2468 		 * This is one of the responses that comes back when an I/O
2469 		 * has been aborted.  If it is because of a timeout that we
2470 		 * initiated, just set the status to CAM_CMD_TIMEOUT.
2471 		 * Otherwise set it to CAM_REQ_ABORTED.  The effect on the
2472 		 * command is the same (it gets retried, subject to the
2473 		 * retry counter), the only difference is what gets printed
2474 		 * on the console.
2475 		 */
2476 		if (cm->cm_state == MPR_CM_STATE_TIMEDOUT)
2477 			mprsas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2478 		else
2479 			mprsas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2480 		break;
2481 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2482 		/* resid is ignored for this condition */
2483 		csio->resid = 0;
2484 		mprsas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2485 		break;
2486 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2487 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2488 		/*
2489 		 * These can sometimes be transient transport-related
2490 		 * errors, and sometimes persistent drive-related errors.
2491 		 * We used to retry these without decrementing the retry
2492 		 * count by returning CAM_REQUEUE_REQ.  Unfortunately, if
2493 		 * we hit a persistent drive problem that returns one of
2494 		 * these error codes, we would retry indefinitely.  So,
2495 		 * return CAM_REQ_CMP_ERROR so that we decrement the retry
2496 		 * count and avoid infinite retries.  We're taking the
2497 		 * potential risk of flagging false failures in the event
2498 		 * of a topology-related error (e.g. a SAS expander problem
2499 		 * causes a command addressed to a drive to fail), but
2500 		 * avoiding getting into an infinite retry loop.
2501 		 */
2502 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2503 		mprsas_log_command(cm, MPR_INFO,
2504 		    "terminated ioc %x loginfo %x scsi %x state %x xfer %u\n",
2505 		    le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2506 		    rep->SCSIStatus, rep->SCSIState,
2507 		    le32toh(rep->TransferCount));
2508 		break;
2509 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
2510 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
2511 	case MPI2_IOCSTATUS_INVALID_VPID:
2512 	case MPI2_IOCSTATUS_INVALID_FIELD:
2513 	case MPI2_IOCSTATUS_INVALID_STATE:
2514 	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2515 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2516 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2517 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2518 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2519 	default:
2520 		mprsas_log_command(cm, MPR_XINFO,
2521 		    "completed ioc %x loginfo %x scsi %x state %x xfer %u\n",
2522 		    le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2523 		    rep->SCSIStatus, rep->SCSIState,
2524 		    le32toh(rep->TransferCount));
2525 		csio->resid = cm->cm_length;
2526 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2527 		break;
2528 	}
2529 
2530 	mpr_sc_failed_io_info(sc, csio, rep, cm->cm_targ);
2531 
2532 	if (sassc->flags & MPRSAS_QUEUE_FROZEN) {
2533 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2534 		sassc->flags &= ~MPRSAS_QUEUE_FROZEN;
2535 		mpr_dprint(sc, MPR_XINFO, "Command completed, unfreezing SIM "
2536 		    "queue\n");
2537 	}
2538 
2539 	if (mprsas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2540 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
2541 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2542 	}
2543 
2544 	mpr_free_command(sc, cm);
2545 	xpt_done(ccb);
2546 }
2547 
2548 #if __FreeBSD_version >= 900026
2549 static void
2550 mprsas_smpio_complete(struct mpr_softc *sc, struct mpr_command *cm)
2551 {
2552 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2553 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2554 	uint64_t sasaddr;
2555 	union ccb *ccb;
2556 
2557 	ccb = cm->cm_complete_data;
2558 
2559 	/*
2560 	 * Currently there should be no way we can hit this case.  It only
2561 	 * happens when we have a failure to allocate chain frames, and SMP
2562 	 * commands require two S/G elements only.  That should be handled
2563 	 * in the standard request size.
2564 	 */
2565 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
2566 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x on SMP "
2567 		    "request!\n", __func__, cm->cm_flags);
2568 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2569 		goto bailout;
2570         }
2571 
2572 	rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2573 	if (rpl == NULL) {
2574 		mpr_dprint(sc, MPR_ERROR, "%s: NULL cm_reply!\n", __func__);
2575 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2576 		goto bailout;
2577 	}
2578 
2579 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2580 	sasaddr = le32toh(req->SASAddress.Low);
2581 	sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2582 
2583 	if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2584 	    MPI2_IOCSTATUS_SUCCESS ||
2585 	    rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2586 		mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2587 		    __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2588 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2589 		goto bailout;
2590 	}
2591 
2592 	mpr_dprint(sc, MPR_XINFO, "%s: SMP request to SAS address %#jx "
2593 	    "completed successfully\n", __func__, (uintmax_t)sasaddr);
2594 
2595 	if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2596 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
2597 	else
2598 		mprsas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2599 
2600 bailout:
2601 	/*
2602 	 * We sync in both directions because we had DMAs in the S/G list
2603 	 * in both directions.
2604 	 */
2605 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2606 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2607 	bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2608 	mpr_free_command(sc, cm);
2609 	xpt_done(ccb);
2610 }
2611 
2612 static void
2613 mprsas_send_smpcmd(struct mprsas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2614 {
2615 	struct mpr_command *cm;
2616 	uint8_t *request, *response;
2617 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
2618 	struct mpr_softc *sc;
2619 	struct sglist *sg;
2620 	int error;
2621 
2622 	sc = sassc->sc;
2623 	sg = NULL;
2624 	error = 0;
2625 
2626 #if (__FreeBSD_version >= 1000028) || \
2627     ((__FreeBSD_version >= 902001) && (__FreeBSD_version < 1000000))
2628 	switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
2629 	case CAM_DATA_PADDR:
2630 	case CAM_DATA_SG_PADDR:
2631 		/*
2632 		 * XXX We don't yet support physical addresses here.
2633 		 */
2634 		mpr_dprint(sc, MPR_ERROR, "%s: physical addresses not "
2635 		    "supported\n", __func__);
2636 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2637 		xpt_done(ccb);
2638 		return;
2639 	case CAM_DATA_SG:
2640 		/*
2641 		 * The chip does not support more than one buffer for the
2642 		 * request or response.
2643 		 */
2644 		if ((ccb->smpio.smp_request_sglist_cnt > 1)
2645 		    || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2646 			mpr_dprint(sc, MPR_ERROR, "%s: multiple request or "
2647 			    "response buffer segments not supported for SMP\n",
2648 			    __func__);
2649 			mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2650 			xpt_done(ccb);
2651 			return;
2652 		}
2653 
2654 		/*
2655 		 * The CAM_SCATTER_VALID flag was originally implemented
2656 		 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2657 		 * We have two.  So, just take that flag to mean that we
2658 		 * might have S/G lists, and look at the S/G segment count
2659 		 * to figure out whether that is the case for each individual
2660 		 * buffer.
2661 		 */
2662 		if (ccb->smpio.smp_request_sglist_cnt != 0) {
2663 			bus_dma_segment_t *req_sg;
2664 
2665 			req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2666 			request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2667 		} else
2668 			request = ccb->smpio.smp_request;
2669 
2670 		if (ccb->smpio.smp_response_sglist_cnt != 0) {
2671 			bus_dma_segment_t *rsp_sg;
2672 
2673 			rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2674 			response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2675 		} else
2676 			response = ccb->smpio.smp_response;
2677 		break;
2678 	case CAM_DATA_VADDR:
2679 		request = ccb->smpio.smp_request;
2680 		response = ccb->smpio.smp_response;
2681 		break;
2682 	default:
2683 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2684 		xpt_done(ccb);
2685 		return;
2686 	}
2687 #else /* __FreeBSD_version < 1000028 */
2688 	/*
2689 	 * XXX We don't yet support physical addresses here.
2690 	 */
2691 	if (ccb->ccb_h.flags & (CAM_DATA_PHYS|CAM_SG_LIST_PHYS)) {
2692 		mpr_dprint(sc, MPR_ERROR, "%s: physical addresses not "
2693 		    "supported\n", __func__);
2694 		mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2695 		xpt_done(ccb);
2696 		return;
2697 	}
2698 
2699 	/*
2700 	 * If the user wants to send an S/G list, check to make sure they
2701 	 * have single buffers.
2702 	 */
2703 	if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
2704 		/*
2705 		 * The chip does not support more than one buffer for the
2706 		 * request or response.
2707 		 */
2708 	 	if ((ccb->smpio.smp_request_sglist_cnt > 1)
2709 		  || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2710 			mpr_dprint(sc, MPR_ERROR, "%s: multiple request or "
2711 			    "response buffer segments not supported for SMP\n",
2712 			    __func__);
2713 			mprsas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2714 			xpt_done(ccb);
2715 			return;
2716 		}
2717 
2718 		/*
2719 		 * The CAM_SCATTER_VALID flag was originally implemented
2720 		 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2721 		 * We have two.  So, just take that flag to mean that we
2722 		 * might have S/G lists, and look at the S/G segment count
2723 		 * to figure out whether that is the case for each individual
2724 		 * buffer.
2725 		 */
2726 		if (ccb->smpio.smp_request_sglist_cnt != 0) {
2727 			bus_dma_segment_t *req_sg;
2728 
2729 			req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2730 			request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2731 		} else
2732 			request = ccb->smpio.smp_request;
2733 
2734 		if (ccb->smpio.smp_response_sglist_cnt != 0) {
2735 			bus_dma_segment_t *rsp_sg;
2736 
2737 			rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2738 			response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2739 		} else
2740 			response = ccb->smpio.smp_response;
2741 	} else {
2742 		request = ccb->smpio.smp_request;
2743 		response = ccb->smpio.smp_response;
2744 	}
2745 #endif /* __FreeBSD_version < 1000028 */
2746 
2747 	cm = mpr_alloc_command(sc);
2748 	if (cm == NULL) {
2749 		mpr_dprint(sc, MPR_ERROR, "%s: cannot allocate command\n",
2750 		    __func__);
2751 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2752 		xpt_done(ccb);
2753 		return;
2754 	}
2755 
2756 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2757 	bzero(req, sizeof(*req));
2758 	req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2759 
2760 	/* Allow the chip to use any route to this SAS address. */
2761 	req->PhysicalPort = 0xff;
2762 
2763 	req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2764 	req->SGLFlags =
2765 	    MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2766 
2767 	mpr_dprint(sc, MPR_XINFO, "%s: sending SMP request to SAS address "
2768 	    "%#jx\n", __func__, (uintmax_t)sasaddr);
2769 
2770 	mpr_init_sge(cm, req, &req->SGL);
2771 
2772 	/*
2773 	 * Set up a uio to pass into mpr_map_command().  This allows us to
2774 	 * do one map command, and one busdma call in there.
2775 	 */
2776 	cm->cm_uio.uio_iov = cm->cm_iovec;
2777 	cm->cm_uio.uio_iovcnt = 2;
2778 	cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2779 
2780 	/*
2781 	 * The read/write flag isn't used by busdma, but set it just in
2782 	 * case.  This isn't exactly accurate, either, since we're going in
2783 	 * both directions.
2784 	 */
2785 	cm->cm_uio.uio_rw = UIO_WRITE;
2786 
2787 	cm->cm_iovec[0].iov_base = request;
2788 	cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
2789 	cm->cm_iovec[1].iov_base = response;
2790 	cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
2791 
2792 	cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
2793 			       cm->cm_iovec[1].iov_len;
2794 
2795 	/*
2796 	 * Trigger a warning message in mpr_data_cb() for the user if we
2797 	 * wind up exceeding two S/G segments.  The chip expects one
2798 	 * segment for the request and another for the response.
2799 	 */
2800 	cm->cm_max_segs = 2;
2801 
2802 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2803 	cm->cm_complete = mprsas_smpio_complete;
2804 	cm->cm_complete_data = ccb;
2805 
2806 	/*
2807 	 * Tell the mapping code that we're using a uio, and that this is
2808 	 * an SMP passthrough request.  There is a little special-case
2809 	 * logic there (in mpr_data_cb()) to handle the bidirectional
2810 	 * transfer.
2811 	 */
2812 	cm->cm_flags |= MPR_CM_FLAGS_USE_UIO | MPR_CM_FLAGS_SMP_PASS |
2813 			MPR_CM_FLAGS_DATAIN | MPR_CM_FLAGS_DATAOUT;
2814 
2815 	/* The chip data format is little endian. */
2816 	req->SASAddress.High = htole32(sasaddr >> 32);
2817 	req->SASAddress.Low = htole32(sasaddr);
2818 
2819 	/*
2820 	 * XXX Note that we don't have a timeout/abort mechanism here.
2821 	 * From the manual, it looks like task management requests only
2822 	 * work for SCSI IO and SATA passthrough requests.  We may need to
2823 	 * have a mechanism to retry requests in the event of a chip reset
2824 	 * at least.  Hopefully the chip will insure that any errors short
2825 	 * of that are relayed back to the driver.
2826 	 */
2827 	error = mpr_map_command(sc, cm);
2828 	if ((error != 0) && (error != EINPROGRESS)) {
2829 		mpr_dprint(sc, MPR_ERROR, "%s: error %d returned from "
2830 		    "mpr_map_command()\n", __func__, error);
2831 		goto bailout_error;
2832 	}
2833 
2834 	return;
2835 
2836 bailout_error:
2837 	mpr_free_command(sc, cm);
2838 	mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2839 	xpt_done(ccb);
2840 	return;
2841 }
2842 
2843 static void
2844 mprsas_action_smpio(struct mprsas_softc *sassc, union ccb *ccb)
2845 {
2846 	struct mpr_softc *sc;
2847 	struct mprsas_target *targ;
2848 	uint64_t sasaddr = 0;
2849 
2850 	sc = sassc->sc;
2851 
2852 	/*
2853 	 * Make sure the target exists.
2854 	 */
2855 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
2856 	    ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
2857 	targ = &sassc->targets[ccb->ccb_h.target_id];
2858 	if (targ->handle == 0x0) {
2859 		mpr_dprint(sc, MPR_ERROR, "%s: target %d does not exist!\n",
2860 		    __func__, ccb->ccb_h.target_id);
2861 		mprsas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
2862 		xpt_done(ccb);
2863 		return;
2864 	}
2865 
2866 	/*
2867 	 * If this device has an embedded SMP target, we'll talk to it
2868 	 * directly.
2869 	 * figure out what the expander's address is.
2870 	 */
2871 	if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
2872 		sasaddr = targ->sasaddr;
2873 
2874 	/*
2875 	 * If we don't have a SAS address for the expander yet, try
2876 	 * grabbing it from the page 0x83 information cached in the
2877 	 * transport layer for this target.  LSI expanders report the
2878 	 * expander SAS address as the port-associated SAS address in
2879 	 * Inquiry VPD page 0x83.  Maxim expanders don't report it in page
2880 	 * 0x83.
2881 	 *
2882 	 * XXX KDM disable this for now, but leave it commented out so that
2883 	 * it is obvious that this is another possible way to get the SAS
2884 	 * address.
2885 	 *
2886 	 * The parent handle method below is a little more reliable, and
2887 	 * the other benefit is that it works for devices other than SES
2888 	 * devices.  So you can send a SMP request to a da(4) device and it
2889 	 * will get routed to the expander that device is attached to.
2890 	 * (Assuming the da(4) device doesn't contain an SMP target...)
2891 	 */
2892 #if 0
2893 	if (sasaddr == 0)
2894 		sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
2895 #endif
2896 
2897 	/*
2898 	 * If we still don't have a SAS address for the expander, look for
2899 	 * the parent device of this device, which is probably the expander.
2900 	 */
2901 	if (sasaddr == 0) {
2902 #ifdef OLD_MPR_PROBE
2903 		struct mprsas_target *parent_target;
2904 #endif
2905 
2906 		if (targ->parent_handle == 0x0) {
2907 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
2908 			    "a valid parent handle!\n", __func__, targ->handle);
2909 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2910 			goto bailout;
2911 		}
2912 #ifdef OLD_MPR_PROBE
2913 		parent_target = mprsas_find_target_by_handle(sassc, 0,
2914 		    targ->parent_handle);
2915 
2916 		if (parent_target == NULL) {
2917 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d does not have "
2918 			    "a valid parent target!\n", __func__, targ->handle);
2919 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2920 			goto bailout;
2921 		}
2922 
2923 		if ((parent_target->devinfo &
2924 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
2925 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent %d "
2926 			    "does not have an SMP target!\n", __func__,
2927 			    targ->handle, parent_target->handle);
2928 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2929 			goto bailout;
2930 		}
2931 
2932 		sasaddr = parent_target->sasaddr;
2933 #else /* OLD_MPR_PROBE */
2934 		if ((targ->parent_devinfo &
2935 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
2936 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent %d "
2937 			    "does not have an SMP target!\n", __func__,
2938 			    targ->handle, targ->parent_handle);
2939 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2940 			goto bailout;
2941 
2942 		}
2943 		if (targ->parent_sasaddr == 0x0) {
2944 			mpr_dprint(sc, MPR_ERROR, "%s: handle %d parent handle "
2945 			    "%d does not have a valid SAS address!\n", __func__,
2946 			    targ->handle, targ->parent_handle);
2947 			mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2948 			goto bailout;
2949 		}
2950 
2951 		sasaddr = targ->parent_sasaddr;
2952 #endif /* OLD_MPR_PROBE */
2953 
2954 	}
2955 
2956 	if (sasaddr == 0) {
2957 		mpr_dprint(sc, MPR_INFO, "%s: unable to find SAS address for "
2958 		    "handle %d\n", __func__, targ->handle);
2959 		mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2960 		goto bailout;
2961 	}
2962 	mprsas_send_smpcmd(sassc, ccb, sasaddr);
2963 
2964 	return;
2965 
2966 bailout:
2967 	xpt_done(ccb);
2968 
2969 }
2970 #endif //__FreeBSD_version >= 900026
2971 
2972 static void
2973 mprsas_action_resetdev(struct mprsas_softc *sassc, union ccb *ccb)
2974 {
2975 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
2976 	struct mpr_softc *sc;
2977 	struct mpr_command *tm;
2978 	struct mprsas_target *targ;
2979 
2980 	MPR_FUNCTRACE(sassc->sc);
2981 	mtx_assert(&sassc->sc->mpr_mtx, MA_OWNED);
2982 
2983 	KASSERT(ccb->ccb_h.target_id < sassc->maxtargets, ("Target %d out of "
2984 	    "bounds in XPT_RESET_DEV\n", ccb->ccb_h.target_id));
2985 	sc = sassc->sc;
2986 	tm = mpr_alloc_command(sc);
2987 	if (tm == NULL) {
2988 		mpr_dprint(sc, MPR_ERROR, "command alloc failure in "
2989 		    "mprsas_action_resetdev\n");
2990 		mprsas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2991 		xpt_done(ccb);
2992 		return;
2993 	}
2994 
2995 	targ = &sassc->targets[ccb->ccb_h.target_id];
2996 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
2997 	req->DevHandle = htole16(targ->handle);
2998 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2999 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3000 
3001 	/* SAS Hard Link Reset / SATA Link Reset */
3002 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3003 
3004 	tm->cm_data = NULL;
3005 	tm->cm_desc.HighPriority.RequestFlags =
3006 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3007 	tm->cm_complete = mprsas_resetdev_complete;
3008 	tm->cm_complete_data = ccb;
3009 
3010 	mpr_dprint(sc, MPR_INFO, "%s: Sending reset for target ID %d\n",
3011 	    __func__, targ->tid);
3012 	tm->cm_targ = targ;
3013 	targ->flags |= MPRSAS_TARGET_INRESET;
3014 
3015 	mpr_map_command(sc, tm);
3016 }
3017 
3018 static void
3019 mprsas_resetdev_complete(struct mpr_softc *sc, struct mpr_command *tm)
3020 {
3021 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3022 	union ccb *ccb;
3023 
3024 	MPR_FUNCTRACE(sc);
3025 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
3026 
3027 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3028 	ccb = tm->cm_complete_data;
3029 
3030 	/*
3031 	 * Currently there should be no way we can hit this case.  It only
3032 	 * happens when we have a failure to allocate chain frames, and
3033 	 * task management commands don't have S/G lists.
3034 	 */
3035 	if ((tm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3036 		MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3037 
3038 		req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3039 
3040 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for reset of "
3041 		    "handle %#04x! This should not happen!\n", __func__,
3042 		    tm->cm_flags, req->DevHandle);
3043 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3044 		goto bailout;
3045 	}
3046 
3047 	mpr_dprint(sc, MPR_XINFO, "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n",
3048 	    __func__, le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3049 
3050 	if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3051 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP);
3052 		mprsas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3053 		    CAM_LUN_WILDCARD);
3054 	}
3055 	else
3056 		mprsas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3057 
3058 bailout:
3059 
3060 	mprsas_free_tm(sc, tm);
3061 	xpt_done(ccb);
3062 }
3063 
3064 static void
3065 mprsas_poll(struct cam_sim *sim)
3066 {
3067 	struct mprsas_softc *sassc;
3068 
3069 	sassc = cam_sim_softc(sim);
3070 
3071 	if (sassc->sc->mpr_debug & MPR_TRACE) {
3072 		/* frequent debug messages during a panic just slow
3073 		 * everything down too much.
3074 		 */
3075 		mpr_dprint(sassc->sc, MPR_XINFO, "%s clearing MPR_TRACE\n",
3076 		    __func__);
3077 		sassc->sc->mpr_debug &= ~MPR_TRACE;
3078 	}
3079 
3080 	mpr_intr_locked(sassc->sc);
3081 }
3082 
3083 static void
3084 mprsas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3085     void *arg)
3086 {
3087 	struct mpr_softc *sc;
3088 
3089 	sc = (struct mpr_softc *)callback_arg;
3090 
3091 	switch (code) {
3092 #if (__FreeBSD_version >= 1000006) || \
3093     ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
3094 	case AC_ADVINFO_CHANGED: {
3095 		struct mprsas_target *target;
3096 		struct mprsas_softc *sassc;
3097 		struct scsi_read_capacity_data_long rcap_buf;
3098 		struct ccb_dev_advinfo cdai;
3099 		struct mprsas_lun *lun;
3100 		lun_id_t lunid;
3101 		int found_lun;
3102 		uintptr_t buftype;
3103 
3104 		buftype = (uintptr_t)arg;
3105 
3106 		found_lun = 0;
3107 		sassc = sc->sassc;
3108 
3109 		/*
3110 		 * We're only interested in read capacity data changes.
3111 		 */
3112 		if (buftype != CDAI_TYPE_RCAPLONG)
3113 			break;
3114 
3115 		/*
3116 		 * See the comment in mpr_attach_sas() for a detailed
3117 		 * explanation.  In these versions of FreeBSD we register
3118 		 * for all events and filter out the events that don't
3119 		 * apply to us.
3120 		 */
3121 #if (__FreeBSD_version < 1000703) || \
3122     ((__FreeBSD_version >= 1100000) && (__FreeBSD_version < 1100002))
3123 		if (xpt_path_path_id(path) != sassc->sim->path_id)
3124 			break;
3125 #endif
3126 
3127 		/*
3128 		 * We should have a handle for this, but check to make sure.
3129 		 */
3130 		KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3131 		    ("Target %d out of bounds in mprsas_async\n",
3132 		    xpt_path_target_id(path)));
3133 		target = &sassc->targets[xpt_path_target_id(path)];
3134 		if (target->handle == 0)
3135 			break;
3136 
3137 		lunid = xpt_path_lun_id(path);
3138 
3139 		SLIST_FOREACH(lun, &target->luns, lun_link) {
3140 			if (lun->lun_id == lunid) {
3141 				found_lun = 1;
3142 				break;
3143 			}
3144 		}
3145 
3146 		if (found_lun == 0) {
3147 			lun = malloc(sizeof(struct mprsas_lun), M_MPR,
3148 			    M_NOWAIT | M_ZERO);
3149 			if (lun == NULL) {
3150 				mpr_dprint(sc, MPR_ERROR, "Unable to alloc "
3151 				    "LUN for EEDP support.\n");
3152 				break;
3153 			}
3154 			lun->lun_id = lunid;
3155 			SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3156 		}
3157 
3158 		bzero(&rcap_buf, sizeof(rcap_buf));
3159 		xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3160 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3161 		cdai.ccb_h.flags = CAM_DIR_IN;
3162 		cdai.buftype = CDAI_TYPE_RCAPLONG;
3163 #if (__FreeBSD_version >= 1100061) || \
3164     ((__FreeBSD_version >= 1001510) && (__FreeBSD_version < 1100000))
3165 		cdai.flags = CDAI_FLAG_NONE;
3166 #else
3167 		cdai.flags = 0;
3168 #endif
3169 		cdai.bufsiz = sizeof(rcap_buf);
3170 		cdai.buf = (uint8_t *)&rcap_buf;
3171 		xpt_action((union ccb *)&cdai);
3172 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3173 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3174 
3175 		if ((mprsas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3176 		    && (rcap_buf.prot & SRC16_PROT_EN)) {
3177 			lun->eedp_formatted = TRUE;
3178 			lun->eedp_block_size = scsi_4btoul(rcap_buf.length);
3179 		} else {
3180 			lun->eedp_formatted = FALSE;
3181 			lun->eedp_block_size = 0;
3182 		}
3183 		break;
3184 	}
3185 #endif
3186 	case AC_FOUND_DEVICE: {
3187 		struct ccb_getdev *cgd;
3188 
3189 		/*
3190 		 * See the comment in mpr_attach_sas() for a detailed
3191 		 * explanation.  In these versions of FreeBSD we register
3192 		 * for all events and filter out the events that don't
3193 		 * apply to us.
3194 		 */
3195 #if (__FreeBSD_version < 1000703) || \
3196     ((__FreeBSD_version >= 1100000) && (__FreeBSD_version < 1100002))
3197 		if (xpt_path_path_id(path) != sc->sassc->sim->path_id)
3198 			break;
3199 #endif
3200 
3201 		cgd = arg;
3202 #if (__FreeBSD_version < 901503) || \
3203     ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
3204 		mprsas_check_eedp(sc, path, cgd);
3205 #endif
3206 		break;
3207 	}
3208 	default:
3209 		break;
3210 	}
3211 }
3212 
3213 #if (__FreeBSD_version < 901503) || \
3214     ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
3215 static void
3216 mprsas_check_eedp(struct mpr_softc *sc, struct cam_path *path,
3217     struct ccb_getdev *cgd)
3218 {
3219 	struct mprsas_softc *sassc = sc->sassc;
3220 	struct ccb_scsiio *csio;
3221 	struct scsi_read_capacity_16 *scsi_cmd;
3222 	struct scsi_read_capacity_eedp *rcap_buf;
3223 	path_id_t pathid;
3224 	target_id_t targetid;
3225 	lun_id_t lunid;
3226 	union ccb *ccb;
3227 	struct cam_path *local_path;
3228 	struct mprsas_target *target;
3229 	struct mprsas_lun *lun;
3230 	uint8_t	found_lun;
3231 	char path_str[64];
3232 
3233 	pathid = cam_sim_path(sassc->sim);
3234 	targetid = xpt_path_target_id(path);
3235 	lunid = xpt_path_lun_id(path);
3236 
3237 	KASSERT(targetid < sassc->maxtargets, ("Target %d out of bounds in "
3238 	    "mprsas_check_eedp\n", targetid));
3239 	target = &sassc->targets[targetid];
3240 	if (target->handle == 0x0)
3241 		return;
3242 
3243 	/*
3244 	 * Determine if the device is EEDP capable.
3245 	 *
3246 	 * If this flag is set in the inquiry data, the device supports
3247 	 * protection information, and must support the 16 byte read capacity
3248 	 * command, otherwise continue without sending read cap 16.
3249 	 */
3250 	if ((cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) == 0)
3251 		return;
3252 
3253 	/*
3254 	 * Issue a READ CAPACITY 16 command.  This info is used to determine if
3255 	 * the LUN is formatted for EEDP support.
3256 	 */
3257 	ccb = xpt_alloc_ccb_nowait();
3258 	if (ccb == NULL) {
3259 		mpr_dprint(sc, MPR_ERROR, "Unable to alloc CCB for EEDP "
3260 		    "support.\n");
3261 		return;
3262 	}
3263 
3264 	if (xpt_create_path(&local_path, xpt_periph, pathid, targetid, lunid) !=
3265 	    CAM_REQ_CMP) {
3266 		mpr_dprint(sc, MPR_ERROR, "Unable to create path for EEDP "
3267 		    "support.\n");
3268 		xpt_free_ccb(ccb);
3269 		return;
3270 	}
3271 
3272 	/*
3273 	 * If LUN is already in list, don't create a new one.
3274 	 */
3275 	found_lun = FALSE;
3276 	SLIST_FOREACH(lun, &target->luns, lun_link) {
3277 		if (lun->lun_id == lunid) {
3278 			found_lun = TRUE;
3279 			break;
3280 		}
3281 	}
3282 	if (!found_lun) {
3283 		lun = malloc(sizeof(struct mprsas_lun), M_MPR,
3284 		    M_NOWAIT | M_ZERO);
3285 		if (lun == NULL) {
3286 			mpr_dprint(sc, MPR_ERROR, "Unable to alloc LUN for "
3287 			    "EEDP support.\n");
3288 			xpt_free_path(local_path);
3289 			xpt_free_ccb(ccb);
3290 			return;
3291 		}
3292 		lun->lun_id = lunid;
3293 		SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3294 	}
3295 
3296 	xpt_path_string(local_path, path_str, sizeof(path_str));
3297 	mpr_dprint(sc, MPR_INFO, "Sending read cap: path %s handle %d\n",
3298 	    path_str, target->handle);
3299 
3300 	/*
3301 	 * Issue a READ CAPACITY 16 command for the LUN.  The
3302 	 * mprsas_read_cap_done function will load the read cap info into the
3303 	 * LUN struct.
3304 	 */
3305 	rcap_buf = malloc(sizeof(struct scsi_read_capacity_eedp), M_MPR,
3306 	    M_NOWAIT | M_ZERO);
3307 	if (rcap_buf == NULL) {
3308 		mpr_dprint(sc, MPR_ERROR, "Unable to alloc read capacity "
3309 		    "buffer for EEDP support.\n");
3310 		xpt_free_path(ccb->ccb_h.path);
3311 		xpt_free_ccb(ccb);
3312 		return;
3313 	}
3314 	xpt_setup_ccb(&ccb->ccb_h, local_path, CAM_PRIORITY_XPT);
3315 	csio = &ccb->csio;
3316 	csio->ccb_h.func_code = XPT_SCSI_IO;
3317 	csio->ccb_h.flags = CAM_DIR_IN;
3318 	csio->ccb_h.retry_count = 4;
3319 	csio->ccb_h.cbfcnp = mprsas_read_cap_done;
3320 	csio->ccb_h.timeout = 60000;
3321 	csio->data_ptr = (uint8_t *)rcap_buf;
3322 	csio->dxfer_len = sizeof(struct scsi_read_capacity_eedp);
3323 	csio->sense_len = MPR_SENSE_LEN;
3324 	csio->cdb_len = sizeof(*scsi_cmd);
3325 	csio->tag_action = MSG_SIMPLE_Q_TAG;
3326 
3327 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
3328 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3329 	scsi_cmd->opcode = 0x9E;
3330 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
3331 	((uint8_t *)scsi_cmd)[13] = sizeof(struct scsi_read_capacity_eedp);
3332 
3333 	ccb->ccb_h.ppriv_ptr1 = sassc;
3334 	xpt_action(ccb);
3335 }
3336 
3337 static void
3338 mprsas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb)
3339 {
3340 	struct mprsas_softc *sassc;
3341 	struct mprsas_target *target;
3342 	struct mprsas_lun *lun;
3343 	struct scsi_read_capacity_eedp *rcap_buf;
3344 
3345 	if (done_ccb == NULL)
3346 		return;
3347 
3348 	/* Driver need to release devq, it Scsi command is
3349 	 * generated by driver internally.
3350 	 * Currently there is a single place where driver
3351 	 * calls scsi command internally. In future if driver
3352 	 * calls more scsi command internally, it needs to release
3353 	 * devq internally, since those command will not go back to
3354 	 * cam_periph.
3355 	 */
3356 	if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) ) {
3357         	done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3358 		xpt_release_devq(done_ccb->ccb_h.path,
3359 			       	/*count*/ 1, /*run_queue*/TRUE);
3360 	}
3361 
3362 	rcap_buf = (struct scsi_read_capacity_eedp *)done_ccb->csio.data_ptr;
3363 
3364 	/*
3365 	 * Get the LUN ID for the path and look it up in the LUN list for the
3366 	 * target.
3367 	 */
3368 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
3369 	KASSERT(done_ccb->ccb_h.target_id < sassc->maxtargets, ("Target %d out "
3370 	    "of bounds in mprsas_read_cap_done\n", done_ccb->ccb_h.target_id));
3371 	target = &sassc->targets[done_ccb->ccb_h.target_id];
3372 	SLIST_FOREACH(lun, &target->luns, lun_link) {
3373 		if (lun->lun_id != done_ccb->ccb_h.target_lun)
3374 			continue;
3375 
3376 		/*
3377 		 * Got the LUN in the target's LUN list.  Fill it in with EEDP
3378 		 * info. If the READ CAP 16 command had some SCSI error (common
3379 		 * if command is not supported), mark the lun as not supporting
3380 		 * EEDP and set the block size to 0.
3381 		 */
3382 		if ((mprsas_get_ccbstatus(done_ccb) != CAM_REQ_CMP) ||
3383 		    (done_ccb->csio.scsi_status != SCSI_STATUS_OK)) {
3384 			lun->eedp_formatted = FALSE;
3385 			lun->eedp_block_size = 0;
3386 			break;
3387 		}
3388 
3389 		if (rcap_buf->protect & 0x01) {
3390 			mpr_dprint(sassc->sc, MPR_INFO, "LUN %d for target ID "
3391 			    "%d is formatted for EEDP support.\n",
3392 			    done_ccb->ccb_h.target_lun,
3393 			    done_ccb->ccb_h.target_id);
3394 			lun->eedp_formatted = TRUE;
3395 			lun->eedp_block_size = scsi_4btoul(rcap_buf->length);
3396 		}
3397 		break;
3398 	}
3399 
3400 	// Finished with this CCB and path.
3401 	free(rcap_buf, M_MPR);
3402 	xpt_free_path(done_ccb->ccb_h.path);
3403 	xpt_free_ccb(done_ccb);
3404 }
3405 #endif /* (__FreeBSD_version < 901503) || \
3406           ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) */
3407 
3408 void
3409 mprsas_prepare_for_tm(struct mpr_softc *sc, struct mpr_command *tm,
3410     struct mprsas_target *target, lun_id_t lun_id)
3411 {
3412 	union ccb *ccb;
3413 	path_id_t path_id;
3414 
3415 	/*
3416 	 * Set the INRESET flag for this target so that no I/O will be sent to
3417 	 * the target until the reset has completed.  If an I/O request does
3418 	 * happen, the devq will be frozen.  The CCB holds the path which is
3419 	 * used to release the devq.  The devq is released and the CCB is freed
3420 	 * when the TM completes.
3421 	 */
3422 	ccb = xpt_alloc_ccb_nowait();
3423 	if (ccb) {
3424 		path_id = cam_sim_path(sc->sassc->sim);
3425 		if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3426 		    target->tid, lun_id) != CAM_REQ_CMP) {
3427 			xpt_free_ccb(ccb);
3428 		} else {
3429 			tm->cm_ccb = ccb;
3430 			tm->cm_targ = target;
3431 			target->flags |= MPRSAS_TARGET_INRESET;
3432 		}
3433 	}
3434 }
3435 
3436 int
3437 mprsas_startup(struct mpr_softc *sc)
3438 {
3439 	/*
3440 	 * Send the port enable message and set the wait_for_port_enable flag.
3441 	 * This flag helps to keep the simq frozen until all discovery events
3442 	 * are processed.
3443 	 */
3444 	sc->wait_for_port_enable = 1;
3445 	mprsas_send_portenable(sc);
3446 	return (0);
3447 }
3448 
3449 static int
3450 mprsas_send_portenable(struct mpr_softc *sc)
3451 {
3452 	MPI2_PORT_ENABLE_REQUEST *request;
3453 	struct mpr_command *cm;
3454 
3455 	MPR_FUNCTRACE(sc);
3456 
3457 	if ((cm = mpr_alloc_command(sc)) == NULL)
3458 		return (EBUSY);
3459 	request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3460 	request->Function = MPI2_FUNCTION_PORT_ENABLE;
3461 	request->MsgFlags = 0;
3462 	request->VP_ID = 0;
3463 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3464 	cm->cm_complete = mprsas_portenable_complete;
3465 	cm->cm_data = NULL;
3466 	cm->cm_sge = NULL;
3467 
3468 	mpr_map_command(sc, cm);
3469 	mpr_dprint(sc, MPR_XINFO,
3470 	    "mpr_send_portenable finished cm %p req %p complete %p\n",
3471 	    cm, cm->cm_req, cm->cm_complete);
3472 	return (0);
3473 }
3474 
3475 static void
3476 mprsas_portenable_complete(struct mpr_softc *sc, struct mpr_command *cm)
3477 {
3478 	MPI2_PORT_ENABLE_REPLY *reply;
3479 	struct mprsas_softc *sassc;
3480 
3481 	MPR_FUNCTRACE(sc);
3482 	sassc = sc->sassc;
3483 
3484 	/*
3485 	 * Currently there should be no way we can hit this case.  It only
3486 	 * happens when we have a failure to allocate chain frames, and
3487 	 * port enable commands don't have S/G lists.
3488 	 */
3489 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3490 		mpr_dprint(sc, MPR_ERROR, "%s: cm_flags = %#x for port enable! "
3491 		    "This should not happen!\n", __func__, cm->cm_flags);
3492 	}
3493 
3494 	reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3495 	if (reply == NULL)
3496 		mpr_dprint(sc, MPR_FAULT, "Portenable NULL reply\n");
3497 	else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3498 	    MPI2_IOCSTATUS_SUCCESS)
3499 		mpr_dprint(sc, MPR_FAULT, "Portenable failed\n");
3500 
3501 	mpr_free_command(sc, cm);
3502 	if (sc->mpr_ich.ich_arg != NULL) {
3503 		mpr_dprint(sc, MPR_XINFO, "disestablish config intrhook\n");
3504 		config_intrhook_disestablish(&sc->mpr_ich);
3505 		sc->mpr_ich.ich_arg = NULL;
3506 	}
3507 
3508 	/*
3509 	 * Done waiting for port enable to complete.  Decrement the refcount.
3510 	 * If refcount is 0, discovery is complete and a rescan of the bus can
3511 	 * take place.
3512 	 */
3513 	sc->wait_for_port_enable = 0;
3514 	sc->port_enable_complete = 1;
3515 	wakeup(&sc->port_enable_complete);
3516 	mprsas_startup_decrement(sassc);
3517 }
3518 
3519 int
3520 mprsas_check_id(struct mprsas_softc *sassc, int id)
3521 {
3522 	struct mpr_softc *sc = sassc->sc;
3523 	char *ids;
3524 	char *name;
3525 
3526 	ids = &sc->exclude_ids[0];
3527 	while((name = strsep(&ids, ",")) != NULL) {
3528 		if (name[0] == '\0')
3529 			continue;
3530 		if (strtol(name, NULL, 0) == (long)id)
3531 			return (1);
3532 	}
3533 
3534 	return (0);
3535 }
3536 
3537 void
3538 mprsas_realloc_targets(struct mpr_softc *sc, int maxtargets)
3539 {
3540 	struct mprsas_softc *sassc;
3541 	struct mprsas_lun *lun, *lun_tmp;
3542 	struct mprsas_target *targ;
3543 	int i;
3544 
3545 	sassc = sc->sassc;
3546 	/*
3547 	 * The number of targets is based on IOC Facts, so free all of
3548 	 * the allocated LUNs for each target and then the target buffer
3549 	 * itself.
3550 	 */
3551 	for (i=0; i< maxtargets; i++) {
3552 		targ = &sassc->targets[i];
3553 		SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3554 			free(lun, M_MPR);
3555 		}
3556 	}
3557 	free(sassc->targets, M_MPR);
3558 
3559 	sassc->targets = malloc(sizeof(struct mprsas_target) * maxtargets,
3560 	    M_MPR, M_WAITOK|M_ZERO);
3561 	if (!sassc->targets) {
3562 		panic("%s failed to alloc targets with error %d\n",
3563 		    __func__, ENOMEM);
3564 	}
3565 }
3566