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