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