xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * Copyright (c) 2011-2015 LSI Corp.
3  * Copyright (c) 2013-2016 Avago Technologies
4  * Copyright 2000-2020 Broadcom Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
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/proc.h>
51 #include <sys/queue.h>
52 #include <sys/kthread.h>
53 #include <sys/taskqueue.h>
54 #include <sys/sbuf.h>
55 #include <sys/reboot.h>
56 
57 #include <machine/bus.h>
58 #include <machine/resource.h>
59 #include <sys/rman.h>
60 
61 #include <machine/stdarg.h>
62 
63 #include <cam/cam.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_xpt_periph.h>
69 #include <cam/cam_periph.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.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_raid.h>
81 #include <dev/mpr/mpi/mpi2_tool.h>
82 #include <dev/mpr/mpr_ioctl.h>
83 #include <dev/mpr/mprvar.h>
84 #include <dev/mpr/mpr_table.h>
85 #include <dev/mpr/mpr_sas.h>
86 
87 /* For Hashed SAS Address creation for SATA Drives */
88 #define MPT2SAS_SN_LEN 20
89 #define MPT2SAS_MN_LEN 40
90 
91 struct mpr_fw_event_work {
92 	u16			event;
93 	void			*event_data;
94 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
95 };
96 
97 union _sata_sas_address {
98 	u8 wwid[8];
99 	struct {
100 		u32 high;
101 		u32 low;
102 	} word;
103 };
104 
105 /*
106  * define the IDENTIFY DEVICE structure
107  */
108 struct _ata_identify_device_data {
109 	u16 reserved1[10];	/* 0-9 */
110 	u16 serial_number[10];	/* 10-19 */
111 	u16 reserved2[7];	/* 20-26 */
112 	u16 model_number[20];	/* 27-46*/
113 	u16 reserved3[170];	/* 47-216 */
114 	u16 rotational_speed;	/* 217 */
115 	u16 reserved4[38];	/* 218-255 */
116 };
117 static u32 event_count;
118 static void mprsas_fw_work(struct mpr_softc *sc,
119     struct mpr_fw_event_work *fw_event);
120 static void mprsas_fw_event_free(struct mpr_softc *,
121     struct mpr_fw_event_work *);
122 static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
123 static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
124     u8 linkrate);
125 static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
126     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
127     u32 devinfo);
128 static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
129 int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
130     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
131 static int mprsas_volume_add(struct mpr_softc *sc,
132     u16 handle);
133 static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
134 static void mprsas_stop_unit_done(struct cam_periph *periph,
135     union ccb *done_ccb);
136 
137 void
138 mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
139     MPI2_EVENT_NOTIFICATION_REPLY *event)
140 {
141 	struct mpr_fw_event_work *fw_event;
142 	u16 sz;
143 
144 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
145 	MPR_DPRINT_EVENT(sc, sas, event);
146 	mprsas_record_event(sc, event);
147 
148 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
149 	     M_ZERO|M_NOWAIT);
150 	if (!fw_event) {
151 		printf("%s: allocate failed for fw_event\n", __func__);
152 		return;
153 	}
154 	sz = le16toh(event->EventDataLength) * 4;
155 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
156 	if (!fw_event->event_data) {
157 		printf("%s: allocate failed for event_data\n", __func__);
158 		free(fw_event, M_MPR);
159 		return;
160 	}
161 
162 	bcopy(event->EventData, fw_event->event_data, sz);
163 	fw_event->event = le16toh(event->Event);
164 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
165 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
166 	    fw_event->event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
167 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
168 	    sc->track_mapping_events)
169 		sc->pending_map_events++;
170 
171 	/*
172 	 * When wait_for_port_enable flag is set, make sure that all the events
173 	 * are processed. Increment the startup_refcount and decrement it after
174 	 * events are processed.
175 	 */
176 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
177 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
178 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
179 	    sc->wait_for_port_enable)
180 		mprsas_startup_increment(sc->sassc);
181 
182 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
183 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
184 }
185 
186 static void
187 mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
188 {
189 
190 	free(fw_event->event_data, M_MPR);
191 	free(fw_event, M_MPR);
192 }
193 
194 /**
195  * _mpr_fw_work - delayed task for processing firmware events
196  * @sc: per adapter object
197  * @fw_event: The fw_event_work object
198  * Context: user.
199  *
200  * Return nothing.
201  */
202 static void
203 mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
204 {
205 	struct mprsas_softc *sassc;
206 	sassc = sc->sassc;
207 
208 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
209 	    event_count++, __func__, fw_event->event);
210 	switch (fw_event->event) {
211 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
212 	{
213 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
214 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
215 		uint8_t i;
216 
217 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
218 		    fw_event->event_data;
219 
220 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
221 
222 		for (i = 0; i < data->NumEntries; i++) {
223 			phy = &data->PHY[i];
224 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
225 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
226 				if (mprsas_add_device(sc,
227 				    le16toh(phy->AttachedDevHandle),
228 				    phy->LinkRate)) {
229 					mpr_dprint(sc, MPR_ERROR, "%s: "
230 					    "failed to add device with handle "
231 					    "0x%x\n", __func__,
232 					    le16toh(phy->AttachedDevHandle));
233 					mprsas_prepare_remove(sassc, le16toh(
234 					    phy->AttachedDevHandle));
235 				}
236 				break;
237 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
238 				mprsas_prepare_remove(sassc, le16toh(
239 				    phy->AttachedDevHandle));
240 				break;
241 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
242 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
243 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
244 			default:
245 				break;
246 			}
247 		}
248 		/*
249 		 * refcount was incremented for this event in
250 		 * mprsas_evt_handler.  Decrement it here because the event has
251 		 * been processed.
252 		 */
253 		mprsas_startup_decrement(sassc);
254 		break;
255 	}
256 	case MPI2_EVENT_SAS_DISCOVERY:
257 	{
258 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
259 
260 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
261 
262 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
263 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
264 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
265 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
266 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
267 			mprsas_discovery_end(sassc);
268 		}
269 		break;
270 	}
271 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
272 	{
273 		Mpi2EventDataSasEnclDevStatusChange_t *data;
274 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
275 		    fw_event->event_data;
276 		mpr_mapping_enclosure_dev_status_change_event(sc,
277 		    fw_event->event_data);
278 		break;
279 	}
280 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
281 	{
282 		Mpi2EventIrConfigElement_t *element;
283 		int i;
284 		u8 foreign_config, reason;
285 		u16 elementType;
286 		Mpi2EventDataIrConfigChangeList_t *event_data;
287 		struct mprsas_target *targ;
288 		unsigned int id;
289 
290 		event_data = fw_event->event_data;
291 		foreign_config = (le32toh(event_data->Flags) &
292 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
293 
294 		element =
295 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
296 		id = mpr_mapping_get_raid_tid_from_handle(sc,
297 		    element->VolDevHandle);
298 
299 		mpr_mapping_ir_config_change_event(sc, event_data);
300 		for (i = 0; i < event_data->NumElements; i++, element++) {
301 			reason = element->ReasonCode;
302 			elementType = le16toh(element->ElementFlags) &
303 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
304 			/*
305 			 * check for element type of Phys Disk or Hot Spare
306 			 */
307 			if ((elementType !=
308 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
309 			    && (elementType !=
310 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
311 				// do next element
312 				goto skip_fp_send;
313 
314 			/*
315 			 * check for reason of Hide, Unhide, PD Created, or PD
316 			 * Deleted
317 			 */
318 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
319 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
320 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
321 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
322 				goto skip_fp_send;
323 
324 			// check for a reason of Hide or PD Created
325 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
326 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
327 			{
328 				// build RAID Action message
329 				Mpi2RaidActionRequest_t	*action;
330 				Mpi2RaidActionReply_t *reply = NULL;
331 				struct mpr_command *cm;
332 				int error = 0;
333 				if ((cm = mpr_alloc_command(sc)) == NULL) {
334 					printf("%s: command alloc failed\n",
335 					    __func__);
336 					return;
337 				}
338 
339 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
340 				    "from "
341 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
342 				    ":\n");
343 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
344 				action->Function = MPI2_FUNCTION_RAID_ACTION;
345 				action->Action =
346 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
347 				action->PhysDiskNum = element->PhysDiskNum;
348 				cm->cm_desc.Default.RequestFlags =
349 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
350 				error = mpr_request_polled(sc, &cm);
351 				if (cm != NULL)
352 					reply = (Mpi2RaidActionReply_t *)
353 					    cm->cm_reply;
354 				if (error || (reply == NULL)) {
355 					/* FIXME */
356 					/*
357 					 * If the poll returns error then we
358 					 * need to do diag reset
359 					 */
360 					printf("%s: poll for page completed "
361 					    "with error %d", __func__, error);
362 				}
363 				if (reply && (le16toh(reply->IOCStatus) &
364 				    MPI2_IOCSTATUS_MASK) !=
365 				    MPI2_IOCSTATUS_SUCCESS) {
366 					mpr_dprint(sc, MPR_ERROR, "%s: error "
367 					    "sending RaidActionPage; "
368 					    "iocstatus = 0x%x\n", __func__,
369 					    le16toh(reply->IOCStatus));
370 				}
371 
372 				if (cm)
373 					mpr_free_command(sc, cm);
374 			}
375 skip_fp_send:
376 			mpr_dprint(sc, MPR_EVENT, "Received "
377 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
378 			    "code %x:\n", element->ReasonCode);
379 			switch (element->ReasonCode) {
380 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
381 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
382 				if (!foreign_config) {
383 					if (mprsas_volume_add(sc,
384 					    le16toh(element->VolDevHandle))) {
385 						printf("%s: failed to add RAID "
386 						    "volume with handle 0x%x\n",
387 						    __func__, le16toh(element->
388 						    VolDevHandle));
389 					}
390 				}
391 				break;
392 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
393 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
394 				/*
395 				 * Rescan after volume is deleted or removed.
396 				 */
397 				if (!foreign_config) {
398 					if (id == MPR_MAP_BAD_ID) {
399 						printf("%s: could not get ID "
400 						    "for volume with handle "
401 						    "0x%04x\n", __func__,
402 						    le16toh(element->
403 						    VolDevHandle));
404 						break;
405 					}
406 
407 					targ = &sassc->targets[id];
408 					targ->handle = 0x0;
409 					targ->encl_slot = 0x0;
410 					targ->encl_handle = 0x0;
411 					targ->encl_level_valid = 0x0;
412 					targ->encl_level = 0x0;
413 					targ->connector_name[0] = ' ';
414 					targ->connector_name[1] = ' ';
415 					targ->connector_name[2] = ' ';
416 					targ->connector_name[3] = ' ';
417 					targ->exp_dev_handle = 0x0;
418 					targ->phy_num = 0x0;
419 					targ->linkrate = 0x0;
420 					mprsas_rescan_target(sc, targ);
421 					printf("RAID target id 0x%x removed\n",
422 					    targ->tid);
423 				}
424 				break;
425 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
426 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
427 				/*
428 				 * Phys Disk of a volume has been created.  Hide
429 				 * it from the OS.
430 				 */
431 				targ = mprsas_find_target_by_handle(sassc, 0,
432 				    element->PhysDiskDevHandle);
433 				if (targ == NULL)
434 					break;
435 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
436 				mprsas_rescan_target(sc, targ);
437 				break;
438 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
439 				/*
440 				 * Phys Disk of a volume has been deleted.
441 				 * Expose it to the OS.
442 				 */
443 				if (mprsas_add_device(sc,
444 				    le16toh(element->PhysDiskDevHandle), 0)) {
445 					printf("%s: failed to add device with "
446 					    "handle 0x%x\n", __func__,
447 					    le16toh(element->
448 					    PhysDiskDevHandle));
449 					mprsas_prepare_remove(sassc,
450 					    le16toh(element->
451 					    PhysDiskDevHandle));
452 				}
453 				break;
454 			}
455 		}
456 		/*
457 		 * refcount was incremented for this event in
458 		 * mprsas_evt_handler.  Decrement it here because the event has
459 		 * been processed.
460 		 */
461 		mprsas_startup_decrement(sassc);
462 		break;
463 	}
464 	case MPI2_EVENT_IR_VOLUME:
465 	{
466 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
467 
468 		/*
469 		 * Informational only.
470 		 */
471 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
472 		switch (event_data->ReasonCode) {
473 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
474   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
475   			    "changed from 0x%x to 0x%x for Volome with "
476  			    "handle 0x%x", le32toh(event_data->PreviousValue),
477  			    le32toh(event_data->NewValue),
478  			    le16toh(event_data->VolDevHandle));
479 			break;
480 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
481   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
482   			    "changed from 0x%x to 0x%x for Volome with "
483  			    "handle 0x%x", le32toh(event_data->PreviousValue),
484  			    le32toh(event_data->NewValue),
485  			    le16toh(event_data->VolDevHandle));
486 			break;
487 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
488   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
489   			    "changed from 0x%x to 0x%x for Volome with "
490  			    "handle 0x%x", le32toh(event_data->PreviousValue),
491  			    le32toh(event_data->NewValue),
492  			    le16toh(event_data->VolDevHandle));
493 				u32 state;
494 				struct mprsas_target *targ;
495 				state = le32toh(event_data->NewValue);
496 				switch (state) {
497 				case MPI2_RAID_VOL_STATE_MISSING:
498 				case MPI2_RAID_VOL_STATE_FAILED:
499 					mprsas_prepare_volume_remove(sassc,
500 					    event_data->VolDevHandle);
501 					break;
502 
503 				case MPI2_RAID_VOL_STATE_ONLINE:
504 				case MPI2_RAID_VOL_STATE_DEGRADED:
505 				case MPI2_RAID_VOL_STATE_OPTIMAL:
506 					targ =
507 					    mprsas_find_target_by_handle(sassc,
508 					    0, event_data->VolDevHandle);
509 					if (targ) {
510 						printf("%s %d: Volume handle "
511 						    "0x%x is already added \n",
512 						    __func__, __LINE__,
513 						    event_data->VolDevHandle);
514 						break;
515 					}
516 					if (mprsas_volume_add(sc,
517 					    le16toh(event_data->
518 					    VolDevHandle))) {
519 						printf("%s: failed to add RAID "
520 						    "volume with handle 0x%x\n",
521 						    __func__, le16toh(
522 						    event_data->VolDevHandle));
523 					}
524 					break;
525 				default:
526 					break;
527 				}
528 			break;
529 		default:
530 			break;
531 		}
532 		break;
533 	}
534 	case MPI2_EVENT_IR_PHYSICAL_DISK:
535 	{
536 		Mpi2EventDataIrPhysicalDisk_t *event_data =
537 		    fw_event->event_data;
538 		struct mprsas_target *targ;
539 
540 		/*
541 		 * Informational only.
542 		 */
543 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
544 		switch (event_data->ReasonCode) {
545 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
546   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
547   			    "changed from 0x%x to 0x%x for Phys Disk Number "
548   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
549  			    "%d", le32toh(event_data->PreviousValue),
550  			    le32toh(event_data->NewValue),
551 			    event_data->PhysDiskNum,
552  			    le16toh(event_data->PhysDiskDevHandle),
553  			    le16toh(event_data->EnclosureHandle),
554 			    le16toh(event_data->Slot));
555 			break;
556 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
557   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
558   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
559   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
560  			    le32toh(event_data->PreviousValue),
561  			    le32toh(event_data->NewValue),
562 			    event_data->PhysDiskNum,
563  			    le16toh(event_data->PhysDiskDevHandle),
564  			    le16toh(event_data->EnclosureHandle),
565 			    le16toh(event_data->Slot));
566 			break;
567 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
568   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
569   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
570   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
571  			    le32toh(event_data->PreviousValue),
572  			    le32toh(event_data->NewValue),
573 			    event_data->PhysDiskNum,
574  			    le16toh(event_data->PhysDiskDevHandle),
575  			    le16toh(event_data->EnclosureHandle),
576 			    le16toh(event_data->Slot));
577 			switch (event_data->NewValue) {
578 				case MPI2_RAID_PD_STATE_ONLINE:
579 				case MPI2_RAID_PD_STATE_DEGRADED:
580 				case MPI2_RAID_PD_STATE_REBUILDING:
581 				case MPI2_RAID_PD_STATE_OPTIMAL:
582 				case MPI2_RAID_PD_STATE_HOT_SPARE:
583 					targ = mprsas_find_target_by_handle(
584 					    sassc, 0,
585 					    event_data->PhysDiskDevHandle);
586 					if (targ) {
587 						targ->flags |=
588 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
589 						printf("%s %d: Found Target "
590 						    "for handle 0x%x.\n",
591 						    __func__, __LINE__ ,
592 						    event_data->
593 						    PhysDiskDevHandle);
594 					}
595 				break;
596 				case MPI2_RAID_PD_STATE_OFFLINE:
597 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
598 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
599 				default:
600 					targ = mprsas_find_target_by_handle(
601 					    sassc, 0,
602 					    event_data->PhysDiskDevHandle);
603 					if (targ) {
604 						targ->flags |=
605 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
606 						printf("%s %d: Found Target "
607 						    "for handle 0x%x.  \n",
608 						    __func__, __LINE__ ,
609 						    event_data->
610 						    PhysDiskDevHandle);
611 					}
612 				break;
613 			}
614 		default:
615 			break;
616 		}
617 		break;
618 	}
619 	case MPI2_EVENT_IR_OPERATION_STATUS:
620 	{
621 		Mpi2EventDataIrOperationStatus_t *event_data =
622 		    fw_event->event_data;
623 
624 		/*
625 		 * Informational only.
626 		 */
627 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
628 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
629 		    "percent complete for Volume with handle 0x%x",
630 		    event_data->RAIDOperation, event_data->PercentComplete,
631 		    le16toh(event_data->VolDevHandle));
632 		break;
633 	}
634 	case MPI2_EVENT_TEMP_THRESHOLD:
635 	{
636 		pMpi2EventDataTemperature_t	temp_event;
637 
638 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
639 
640 		/*
641 		 * The Temp Sensor Count must be greater than the event's Sensor
642 		 * Num to be valid.  If valid, print the temp thresholds that
643 		 * have been exceeded.
644 		 */
645 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
646 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
647 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
648 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
649 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
650 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
651 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
652 			    temp_event->SensorNum);
653 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
654 			    "%d\n", temp_event->CurrentTemperature);
655 		}
656 		break;
657 	}
658 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
659 	{
660 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
661 		ace_event_data =
662 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
663 
664 		switch(ace_event_data->ReasonCode) {
665 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
666 		{
667 			mpr_printf(sc, "Currently a cable with "
668 			    "ReceptacleID %d cannot be powered and device "
669 			    "connected to this active cable will not be seen. "
670 			    "This active cable requires %d mW of power.\n",
671 			    ace_event_data->ReceptacleID,
672 			    ace_event_data->ActiveCablePowerRequirement);
673 			break;
674 		}
675 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
676 		{
677 			mpr_printf(sc, "Currently a cable with "
678 			    "ReceptacleID %d is not running at optimal speed "
679 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
680 			break;
681 		}
682 		default:
683 			break;
684 		}
685 		break;
686 	}
687 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
688 	{
689 		pMpi26EventDataPCIeDeviceStatusChange_t	pcie_status_event_data;
690 		pcie_status_event_data =
691 		   (pMpi26EventDataPCIeDeviceStatusChange_t)fw_event->event_data;
692 
693 		switch (pcie_status_event_data->ReasonCode) {
694 		case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
695 		{
696 			mpr_printf(sc, "PCIe Host Reset failed on DevHandle "
697 			    "0x%x\n", pcie_status_event_data->DevHandle);
698 			break;
699 		}
700 		default:
701 			break;
702 		}
703 		break;
704 	}
705 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
706 	{
707 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
708 		uint64_t sas_address;
709 
710 		discovery_error_data =
711 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
712 		    fw_event->event_data;
713 
714 		sas_address = discovery_error_data->SASAddress.High;
715 		sas_address = (sas_address << 32) |
716 		    discovery_error_data->SASAddress.Low;
717 
718 		switch(discovery_error_data->ReasonCode) {
719 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
720 		{
721 			mpr_printf(sc, "SMP command failed during discovery "
722 			    "for expander with SAS Address %jx and "
723 			    "handle 0x%x.\n", sas_address,
724 			    discovery_error_data->DevHandle);
725 			break;
726 		}
727 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
728 		{
729 			mpr_printf(sc, "SMP command timed out during "
730 			    "discovery for expander with SAS Address %jx and "
731 			    "handle 0x%x.\n", sas_address,
732 			    discovery_error_data->DevHandle);
733 			break;
734 		}
735 		default:
736 			break;
737 		}
738 		break;
739 	}
740 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
741 	{
742 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
743 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
744 		uint8_t i, link_rate;
745 		uint16_t handle;
746 
747 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
748 		    fw_event->event_data;
749 
750 		mpr_mapping_pcie_topology_change_event(sc,
751 		    fw_event->event_data);
752 
753 		for (i = 0; i < data->NumEntries; i++) {
754 			port_entry = &data->PortEntry[i];
755 			handle = le16toh(port_entry->AttachedDevHandle);
756 			link_rate = port_entry->CurrentPortInfo &
757 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
758 			switch (port_entry->PortStatus) {
759 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
760 				if (link_rate <
761 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
762 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
763 					    "add PCIe device with handle 0x%x "
764 					    "with unknown link rate.\n",
765 					    __func__, handle);
766 					break;
767 				}
768 				if (mprsas_add_pcie_device(sc, handle,
769 				    link_rate)) {
770 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
771 					    "to add PCIe device with handle "
772 					    "0x%x\n", __func__, handle);
773 					mprsas_prepare_remove(sassc, handle);
774 				}
775 				break;
776 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
777 				mprsas_prepare_remove(sassc, handle);
778 				break;
779 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
780 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
781 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
782 			default:
783 				break;
784 			}
785 		}
786 		/*
787 		 * refcount was incremented for this event in
788 		 * mprsas_evt_handler.  Decrement it here because the event has
789 		 * been processed.
790 		 */
791 		mprsas_startup_decrement(sassc);
792 		break;
793 	}
794 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
795 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
796 	default:
797 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
798 		    fw_event->event);
799 		break;
800 	}
801 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
802 	    __func__, fw_event->event);
803 	mprsas_fw_event_free(sc, fw_event);
804 }
805 
806 void
807 mprsas_firmware_event_work(void *arg, int pending)
808 {
809 	struct mpr_fw_event_work *fw_event;
810 	struct mpr_softc *sc;
811 
812 	sc = (struct mpr_softc *)arg;
813 	mpr_lock(sc);
814 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
815 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
816 		mprsas_fw_work(sc, fw_event);
817 	}
818 	mpr_unlock(sc);
819 }
820 
821 static int
822 mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
823 {
824 	char devstring[80];
825 	struct mprsas_softc *sassc;
826 	struct mprsas_target *targ;
827 	Mpi2ConfigReply_t mpi_reply;
828 	Mpi2SasDevicePage0_t config_page;
829 	uint64_t sas_address, parent_sas_address = 0;
830 	u32 device_info, parent_devinfo = 0;
831 	unsigned int id;
832 	int ret = 1, error = 0, i;
833 	struct mprsas_lun *lun;
834 	u8 is_SATA_SSD = 0;
835 	struct mpr_command *cm;
836 
837 	sassc = sc->sassc;
838 	mprsas_startup_increment(sassc);
839 	if (mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
840 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
841 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
842 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
843 		    handle, mpi_reply.IOCStatus);
844 		error = ENXIO;
845 		goto out;
846 	}
847 
848 	device_info = le32toh(config_page.DeviceInfo);
849 
850 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
851 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
852 		Mpi2ConfigReply_t tmp_mpi_reply;
853 		Mpi2SasDevicePage0_t parent_config_page;
854 
855 		if (mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
856 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
857 		    le16toh(config_page.ParentDevHandle)) != 0) {
858 			mpr_dprint(sc, MPR_MAPPING|MPR_FAULT,
859 			    "Error reading parent SAS device %#x page0, "
860 			    "iocstatus= 0x%x\n",
861 			    le16toh(config_page.ParentDevHandle),
862 			    tmp_mpi_reply.IOCStatus);
863 		} else {
864 			parent_sas_address = parent_config_page.SASAddress.High;
865 			parent_sas_address = (parent_sas_address << 32) |
866 			    parent_config_page.SASAddress.Low;
867 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
868 		}
869 	}
870 	sas_address = htole32(config_page.SASAddress.High);
871 	sas_address = (sas_address << 32) | htole32(config_page.SASAddress.Low);
872 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
873 	    "page0 = %jx\n", handle, sas_address);
874 
875 	/*
876 	 * Always get SATA Identify information because this is used to
877 	 * determine if Start/Stop Unit should be sent to the drive when the
878 	 * system is shutdown.
879 	 */
880 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
881 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
882 		    handle, device_info, &is_SATA_SSD);
883 		if (ret) {
884 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
885 			    "%s: failed to get disk type (SSD or HDD) for SATA "
886 			    "device with handle 0x%04x\n",
887 			    __func__, handle);
888 		} else {
889 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
890 			    "from SATA device = %jx\n", handle, sas_address);
891 		}
892 	}
893 
894 	/*
895 	 * use_phynum:
896 	 *  1 - use the PhyNum field as a fallback to the mapping logic
897 	 *  0 - never use the PhyNum field
898 	 * -1 - only use the PhyNum field
899 	 *
900 	 * Note that using the Phy number to map a device can cause device adds
901 	 * to fail if multiple enclosures/expanders are in the topology. For
902 	 * example, if two devices are in the same slot number in two different
903 	 * enclosures within the topology, only one of those devices will be
904 	 * added. PhyNum mapping should not be used if multiple enclosures are
905 	 * in the topology.
906 	 */
907 	id = MPR_MAP_BAD_ID;
908 	if (sc->use_phynum != -1)
909 		id = mpr_mapping_get_tid(sc, sas_address, handle);
910 	if (id == MPR_MAP_BAD_ID) {
911 		if ((sc->use_phynum == 0) ||
912 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
913 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
914 			    "Could not get ID for device with handle 0x%04x\n",
915 			    __FILE__, __LINE__, __func__, handle);
916 			error = ENXIO;
917 			goto out;
918 		}
919 	}
920 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
921 	    __func__, id);
922 
923 	/*
924 	 * Only do the ID check and reuse check if the target is not from a
925 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
926 	 * when a volume is deleted because the mapping entry for the PD will
927 	 * still be in the mapping table. The ID check should not be done here
928 	 * either since this PD is already being used.
929 	 */
930 	targ = &sassc->targets[id];
931 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
932 		if (mprsas_check_id(sassc, id) != 0) {
933 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
934 			    "Excluding target id %d\n", id);
935 			error = ENXIO;
936 			goto out;
937 		}
938 
939 		if (targ->handle != 0x0) {
940 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
941 			    "target id %d handle 0x%04x\n", id, targ->handle);
942 			error = ENXIO;
943 			goto out;
944 		}
945 	}
946 
947 	targ->devinfo = device_info;
948 	targ->devname = le32toh(config_page.DeviceName.High);
949 	targ->devname = (targ->devname << 32) |
950 	    le32toh(config_page.DeviceName.Low);
951 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
952 	targ->encl_slot = le16toh(config_page.Slot);
953 	targ->encl_level = config_page.EnclosureLevel;
954 	targ->connector_name[0] = config_page.ConnectorName[0];
955 	targ->connector_name[1] = config_page.ConnectorName[1];
956 	targ->connector_name[2] = config_page.ConnectorName[2];
957 	targ->connector_name[3] = config_page.ConnectorName[3];
958 	targ->handle = handle;
959 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
960 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
961 	targ->parent_sasaddr = le64toh(parent_sas_address);
962 	targ->parent_devinfo = parent_devinfo;
963 	targ->tid = id;
964 	targ->linkrate = (linkrate>>4);
965 	targ->flags = 0;
966 	if (is_SATA_SSD) {
967 		targ->flags = MPR_TARGET_IS_SATA_SSD;
968 	}
969 	if ((le16toh(config_page.Flags) &
970 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
971 	    (le16toh(config_page.Flags) &
972 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
973 		targ->scsi_req_desc_type =
974 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
975 	}
976 	if (le16toh(config_page.Flags) &
977 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
978 		targ->encl_level_valid = TRUE;
979 	}
980 	TAILQ_INIT(&targ->commands);
981 	TAILQ_INIT(&targ->timedout_commands);
982 	while (!SLIST_EMPTY(&targ->luns)) {
983 		lun = SLIST_FIRST(&targ->luns);
984 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
985 		free(lun, M_MPR);
986 	}
987 	SLIST_INIT(&targ->luns);
988 
989 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
990 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
991 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
992 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
993 	    targ->handle, targ->encl_handle, targ->encl_slot);
994 	if (targ->encl_level_valid) {
995 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
996 		    "and connector name (%4s)\n", targ->encl_level,
997 		    targ->connector_name);
998 	}
999 	mprsas_rescan_target(sc, targ);
1000 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1001 
1002 	/*
1003 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
1004 	 * If so, send a Target Reset TM to the target that was just created.
1005 	 * An Abort Task TM should be used instead of a Target Reset, but that
1006 	 * would be much more difficult because targets have not been fully
1007 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
1008 	 * target instead of the LUN.
1009 	 */
1010 	for (i = 1; i < sc->num_reqs; i++) {
1011 		cm = &sc->commands[i];
1012 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1013 			targ->timeouts++;
1014 			cm->cm_flags |= MPR_CM_FLAGS_TIMEDOUT;
1015 
1016 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1017 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1018 				    "Reset for stuck SATA identify command "
1019 				    "(cm = %p)\n", __func__, cm);
1020 				targ->tm->cm_targ = targ;
1021 				mprsas_send_reset(sc, targ->tm,
1022 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1023 			} else {
1024 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
1025 				    "tm for Target Reset after SATA ID command "
1026 				    "timed out (cm %p)\n", cm);
1027 			}
1028 			/*
1029 			 * No need to check for more since the target is
1030 			 * already being reset.
1031 			 */
1032 			break;
1033 		}
1034 	}
1035 out:
1036 	/*
1037 	 * Free the commands that may not have been freed from the SATA ID call
1038 	 */
1039 	for (i = 1; i < sc->num_reqs; i++) {
1040 		cm = &sc->commands[i];
1041 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1042 			free(cm->cm_data, M_MPR);
1043 			mpr_free_command(sc, cm);
1044 		}
1045 	}
1046 	mprsas_startup_decrement(sassc);
1047 	return (error);
1048 }
1049 
1050 int
1051 mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1052     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1053 {
1054 	Mpi2SataPassthroughReply_t mpi_reply;
1055 	int i, rc, try_count;
1056 	u32 *bufferptr;
1057 	union _sata_sas_address hash_address;
1058 	struct _ata_identify_device_data ata_identify;
1059 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1060 	u32 ioc_status;
1061 	u8 sas_status;
1062 
1063 	memset(&ata_identify, 0, sizeof(ata_identify));
1064 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1065 	try_count = 0;
1066 	do {
1067 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1068 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1069 		try_count++;
1070 		ioc_status = le16toh(mpi_reply.IOCStatus)
1071 		    & MPI2_IOCSTATUS_MASK;
1072 		sas_status = mpi_reply.SASStatus;
1073 		switch (ioc_status) {
1074 		case MPI2_IOCSTATUS_SUCCESS:
1075 			break;
1076 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1077 			/* No sense sleeping.  this error won't get better */
1078 			break;
1079 		default:
1080 			if (sc->spinup_wait_time > 0) {
1081 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1082 				    "after SATA ID error to wait for spinup\n",
1083 				    sc->spinup_wait_time);
1084 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1085 				    "mprid", sc->spinup_wait_time * hz);
1086 			}
1087 		}
1088 	} while (((rc && (rc != EWOULDBLOCK)) ||
1089 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1090 	    || sas_status) && (try_count < 5));
1091 
1092 	if (rc == 0 && !ioc_status && !sas_status) {
1093 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1094 		    "successfully for handle = 0x%x with try_count = %d\n",
1095 		    __func__, handle, try_count);
1096 	} else {
1097 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1098 		    __func__, handle);
1099 		return -1;
1100 	}
1101 	/* Copy & byteswap the 40 byte model number to a buffer */
1102 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1103 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1104 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1105 	}
1106 	/* Copy & byteswap the 20 byte serial number to a buffer */
1107 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1108 		buffer[MPT2SAS_MN_LEN + i] =
1109 		    ((u8 *)ata_identify.serial_number)[i + 1];
1110 		buffer[MPT2SAS_MN_LEN + i + 1] =
1111 		    ((u8 *)ata_identify.serial_number)[i];
1112 	}
1113 	bufferptr = (u32 *)buffer;
1114 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1115 	 * so loop through the first 56 bytes (7*8),
1116 	 * and then add in the last dword.
1117 	 */
1118 	hash_address.word.low  = 0;
1119 	hash_address.word.high = 0;
1120 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1121 		hash_address.word.low += *bufferptr;
1122 		bufferptr++;
1123 		hash_address.word.high += *bufferptr;
1124 		bufferptr++;
1125 	}
1126 	/* Add the last dword */
1127 	hash_address.word.low += *bufferptr;
1128 	/* Make sure the hash doesn't start with 5, because it could clash
1129 	 * with a SAS address. Change 5 to a D.
1130 	 */
1131 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1132 		hash_address.word.high |= 0x00000080;
1133 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1134 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1135 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1136 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1137 	    (u64)hash_address.wwid[7];
1138 	if (ata_identify.rotational_speed == 1) {
1139 		*is_SATA_SSD = 1;
1140 	}
1141 
1142 	return 0;
1143 }
1144 
1145 static int
1146 mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1147     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1148 {
1149 	Mpi2SataPassthroughRequest_t *mpi_request;
1150 	Mpi2SataPassthroughReply_t *reply;
1151 	struct mpr_command *cm;
1152 	char *buffer;
1153 	int error = 0;
1154 
1155 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1156 	if (!buffer)
1157 		return ENOMEM;
1158 
1159 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1160 		free(buffer, M_MPR);
1161 		return (EBUSY);
1162 	}
1163 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1164 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1165 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1166 	mpi_request->VF_ID = 0;
1167 	mpi_request->DevHandle = htole16(handle);
1168 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1169 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1170 	mpi_request->DataLength = htole32(sz);
1171 	mpi_request->CommandFIS[0] = 0x27;
1172 	mpi_request->CommandFIS[1] = 0x80;
1173 	mpi_request->CommandFIS[2] =  (devinfo &
1174 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1175 	cm->cm_sge = &mpi_request->SGL;
1176 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1177 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1178 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1179 	cm->cm_data = buffer;
1180 	cm->cm_length = htole32(sz);
1181 
1182 	/*
1183 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
1184 	 * This fixes a problem where the FW does not send a reply sometimes
1185 	 * when a bad disk is in the topology. So, this is used to timeout the
1186 	 * command so that processing can continue normally.
1187 	 */
1188 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1189 
1190 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
1191 
1192 	/* mprsas_ata_id_timeout does not reset controller */
1193 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
1194 
1195 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1196 	if (error || (reply == NULL)) {
1197 		/* FIXME */
1198 		/*
1199 		 * If the request returns an error then we need to do a diag
1200 		 * reset
1201 		 */
1202 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1203 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
1204 		    error);
1205 		error = ENXIO;
1206 		goto out;
1207 	}
1208 	bcopy(buffer, id_buffer, sz);
1209 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1210 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1211 	    MPI2_IOCSTATUS_SUCCESS) {
1212 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1213 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1214 		    handle, reply->IOCStatus);
1215 		error = ENXIO;
1216 		goto out;
1217 	}
1218 out:
1219 	/*
1220 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
1221 	 * it.  The command and buffer will be freed after sending an Abort
1222 	 * Task TM.
1223 	 */
1224 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1225 		mpr_free_command(sc, cm);
1226 		free(buffer, M_MPR);
1227 	}
1228 	return (error);
1229 }
1230 
1231 static void
1232 mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1233 {
1234 
1235 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1236 	    __func__, cm, sc);
1237 
1238 	/*
1239 	 * The Abort Task cannot be sent from here because the driver has not
1240 	 * completed setting up targets.  Instead, the command is flagged so
1241 	 * that special handling will be used to send the abort. Now that
1242 	 * this command has timed out, it's no longer in the queue.
1243 	 */
1244 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1245 	cm->cm_state = MPR_CM_STATE_BUSY;
1246 }
1247 
1248 static int
1249 mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
1250 {
1251 	char devstring[80];
1252 	struct mprsas_softc *sassc;
1253 	struct mprsas_target *targ;
1254 	Mpi2ConfigReply_t mpi_reply;
1255 	Mpi26PCIeDevicePage0_t config_page;
1256 	Mpi26PCIeDevicePage2_t config_page2;
1257 	uint64_t pcie_wwid, parent_wwid = 0;
1258 	u32 device_info, parent_devinfo = 0;
1259 	unsigned int id;
1260 	int error = 0;
1261 	struct mprsas_lun *lun;
1262 
1263 	sassc = sc->sassc;
1264 	mprsas_startup_increment(sassc);
1265 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
1266 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
1267 		printf("%s: error reading PCIe device page0\n", __func__);
1268 		error = ENXIO;
1269 		goto out;
1270 	}
1271 
1272 	device_info = le32toh(config_page.DeviceInfo);
1273 
1274 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
1275 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
1276 		Mpi2ConfigReply_t tmp_mpi_reply;
1277 		Mpi26PCIeDevicePage0_t parent_config_page;
1278 
1279 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
1280 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
1281 		     le16toh(config_page.ParentDevHandle)))) {
1282 			printf("%s: error reading PCIe device %#x page0\n",
1283 			    __func__, le16toh(config_page.ParentDevHandle));
1284 		} else {
1285 			parent_wwid = parent_config_page.WWID.High;
1286 			parent_wwid = (parent_wwid << 32) |
1287 			    parent_config_page.WWID.Low;
1288 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
1289 		}
1290 	}
1291 	/* TODO Check proper endianness */
1292 	pcie_wwid = config_page.WWID.High;
1293 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
1294 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
1295 	    pcie_wwid);
1296 
1297 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
1298 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
1299 		printf("%s: error reading PCIe device page2\n", __func__);
1300 		error = ENXIO;
1301 		goto out;
1302 	}
1303 
1304 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
1305 	if (id == MPR_MAP_BAD_ID) {
1306 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1307 		    "Could not get ID for device with handle 0x%04x\n",
1308 		    __FILE__, __LINE__, __func__, handle);
1309 		error = ENXIO;
1310 		goto out;
1311 	}
1312 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1313 	    __func__, id);
1314 
1315 	if (mprsas_check_id(sassc, id) != 0) {
1316 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1317 		    "Excluding target id %d\n", id);
1318 		error = ENXIO;
1319 		goto out;
1320 	}
1321 
1322 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
1323 	    pcie_wwid);
1324 	targ = &sassc->targets[id];
1325 	targ->devinfo = device_info;
1326 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
1327 	targ->encl_slot = le16toh(config_page.Slot);
1328 	targ->encl_level = config_page.EnclosureLevel;
1329 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
1330 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
1331 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
1332 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
1333 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
1334 	targ->MDTS = config_page2.MaximumDataTransferSize;
1335 	if (targ->is_nvme)
1336 		targ->controller_reset_timeout = config_page2.ControllerResetTO;
1337 	/*
1338 	 * Assume always TRUE for encl_level_valid because there is no valid
1339 	 * flag for PCIe.
1340 	 */
1341 	targ->encl_level_valid = TRUE;
1342 	targ->handle = handle;
1343 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
1344 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
1345 	targ->parent_sasaddr = le64toh(parent_wwid);
1346 	targ->parent_devinfo = parent_devinfo;
1347 	targ->tid = id;
1348 	targ->linkrate = linkrate;
1349 	targ->flags = 0;
1350 	if ((le16toh(config_page.Flags) &
1351 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
1352 	    (le16toh(config_page.Flags) &
1353 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
1354 		targ->scsi_req_desc_type =
1355 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
1356 	}
1357 	TAILQ_INIT(&targ->commands);
1358 	TAILQ_INIT(&targ->timedout_commands);
1359 	while (!SLIST_EMPTY(&targ->luns)) {
1360 		lun = SLIST_FIRST(&targ->luns);
1361 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1362 		free(lun, M_MPR);
1363 	}
1364 	SLIST_INIT(&targ->luns);
1365 
1366 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
1367 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
1368 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
1369 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
1370 	    targ->handle, targ->encl_handle, targ->encl_slot);
1371 	if (targ->encl_level_valid) {
1372 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
1373 		    "and connector name (%4s)\n", targ->encl_level,
1374 		    targ->connector_name);
1375 	}
1376 	mprsas_rescan_target(sc, targ);
1377 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1378 
1379 out:
1380 	mprsas_startup_decrement(sassc);
1381 	return (error);
1382 }
1383 
1384 static int
1385 mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1386 {
1387 	struct mprsas_softc *sassc;
1388 	struct mprsas_target *targ;
1389 	u64 wwid;
1390 	unsigned int id;
1391 	int error = 0;
1392 	struct mprsas_lun *lun;
1393 
1394 	sassc = sc->sassc;
1395 	mprsas_startup_increment(sassc);
1396 	/* wwid is endian safe */
1397 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1398 	if (!wwid) {
1399 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1400 		    __func__);
1401 		error = ENXIO;
1402 		goto out;
1403 	}
1404 
1405 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1406 	if (id == MPR_MAP_BAD_ID) {
1407 		printf("%s: could not get ID for volume with handle 0x%04x and "
1408 		    "WWID 0x%016llx\n", __func__, handle,
1409 		    (unsigned long long)wwid);
1410 		error = ENXIO;
1411 		goto out;
1412 	}
1413 
1414 	targ = &sassc->targets[id];
1415 	targ->tid = id;
1416 	targ->handle = handle;
1417 	targ->devname = wwid;
1418 	TAILQ_INIT(&targ->commands);
1419 	TAILQ_INIT(&targ->timedout_commands);
1420 	while (!SLIST_EMPTY(&targ->luns)) {
1421 		lun = SLIST_FIRST(&targ->luns);
1422 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1423 		free(lun, M_MPR);
1424 	}
1425 	SLIST_INIT(&targ->luns);
1426 	mprsas_rescan_target(sc, targ);
1427 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1428 	    targ->tid, wwid);
1429 out:
1430 	mprsas_startup_decrement(sassc);
1431 	return (error);
1432 }
1433 
1434 /**
1435  * mprsas_SSU_to_SATA_devices
1436  * @sc: per adapter object
1437  *
1438  * Looks through the target list and issues a StartStopUnit SCSI command to each
1439  * SATA direct-access device.  This helps to ensure that data corruption is
1440  * avoided when the system is being shut down.  This must be called after the IR
1441  * System Shutdown RAID Action is sent if in IR mode.
1442  *
1443  * Return nothing.
1444  */
1445 static void
1446 mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto)
1447 {
1448 	struct mprsas_softc *sassc = sc->sassc;
1449 	union ccb *ccb;
1450 	path_id_t pathid = cam_sim_path(sassc->sim);
1451 	target_id_t targetid;
1452 	struct mprsas_target *target;
1453 	char path_str[64];
1454 	int timeout;
1455 
1456 	mpr_lock(sc);
1457 
1458 	/*
1459 	 * For each target, issue a StartStopUnit command to stop the device.
1460 	 */
1461 	sc->SSU_started = TRUE;
1462 	sc->SSU_refcount = 0;
1463 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1464 		target = &sassc->targets[targetid];
1465 		if (target->handle == 0x0) {
1466 			continue;
1467 		}
1468 
1469 		/*
1470 		 * The stop_at_shutdown flag will be set if this device is
1471 		 * a SATA direct-access end device.
1472 		 */
1473 		if (target->stop_at_shutdown) {
1474 			ccb = xpt_alloc_ccb_nowait();
1475 			if (ccb == NULL) {
1476 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
1477 				    "to stop unit.\n");
1478 				return;
1479 			}
1480 
1481 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1482 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1483 			    CAM_REQ_CMP) {
1484 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1485 				    "path to stop unit.\n");
1486 				xpt_free_ccb(ccb);
1487 				return;
1488 			}
1489 			xpt_path_string(ccb->ccb_h.path, path_str,
1490 			    sizeof(path_str));
1491 
1492 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1493 			    "handle %d\n", path_str, target->handle);
1494 
1495 			/*
1496 			 * Issue a START STOP UNIT command for the target.
1497 			 * Increment the SSU counter to be used to count the
1498 			 * number of required replies.
1499 			 */
1500 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1501 			sc->SSU_refcount++;
1502 			ccb->ccb_h.target_id =
1503 			    xpt_path_target_id(ccb->ccb_h.path);
1504 			ccb->ccb_h.ppriv_ptr1 = sassc;
1505 			scsi_start_stop(&ccb->csio,
1506 			    /*retries*/0,
1507 			    mprsas_stop_unit_done,
1508 			    MSG_SIMPLE_Q_TAG,
1509 			    /*start*/FALSE,
1510 			    /*load/eject*/0,
1511 			    /*immediate*/FALSE,
1512 			    MPR_SENSE_LEN,
1513 			    /*timeout*/10000);
1514 			xpt_action(ccb);
1515 		}
1516 	}
1517 
1518 	mpr_unlock(sc);
1519 
1520 	/*
1521 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1522 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1523 	 */
1524 	timeout = 600;
1525 	if (howto & RB_NOSYNC)
1526 		timeout = 100;
1527 
1528 	/*
1529 	 * Wait until all of the SSU commands have completed or time
1530 	 * has expired. Pause for 100ms each time through.  If any
1531 	 * command times out, the target will be reset in the SCSI
1532 	 * command timeout routine.
1533 	 */
1534 	while (sc->SSU_refcount > 0) {
1535 		pause("mprwait", hz/10);
1536 		if (SCHEDULER_STOPPED())
1537 			xpt_sim_poll(sassc->sim);
1538 
1539 		if (--timeout == 0) {
1540 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1541 			    "for SSU commands to complete.\n");
1542 			break;
1543 		}
1544 	}
1545 }
1546 
1547 static void
1548 mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1549 {
1550 	struct mprsas_softc *sassc;
1551 	char path_str[64];
1552 
1553 	if (done_ccb == NULL)
1554 		return;
1555 
1556 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1557 
1558 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1559 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1560 	    path_str);
1561 
1562 	/*
1563 	 * Nothing more to do except free the CCB and path.  If the command
1564 	 * timed out, an abort reset, then target reset will be issued during
1565 	 * the SCSI Command process.
1566 	 */
1567 	xpt_free_path(done_ccb->ccb_h.path);
1568 	xpt_free_ccb(done_ccb);
1569 }
1570 
1571 /**
1572  * mprsas_ir_shutdown - IR shutdown notification
1573  * @sc: per adapter object
1574  *
1575  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1576  * the host system is shutting down.
1577  *
1578  * Return nothing.
1579  */
1580 void
1581 mprsas_ir_shutdown(struct mpr_softc *sc, int howto)
1582 {
1583 	u16 volume_mapping_flags;
1584 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1585 	struct dev_mapping_table *mt_entry;
1586 	u32 start_idx, end_idx;
1587 	unsigned int id, found_volume = 0;
1588 	struct mpr_command *cm;
1589 	Mpi2RaidActionRequest_t	*action;
1590 	target_id_t targetid;
1591 	struct mprsas_target *target;
1592 
1593 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1594 
1595 	/* is IR firmware build loaded? */
1596 	if (!sc->ir_firmware)
1597 		goto out;
1598 
1599 	/* are there any volumes?  Look at IR target IDs. */
1600 	// TODO-later, this should be looked up in the RAID config structure
1601 	// when it is implemented.
1602 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1603 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1604 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1605 		start_idx = 0;
1606 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1607 			start_idx = 1;
1608 	} else
1609 		start_idx = sc->max_devices - sc->max_volumes;
1610 	end_idx = start_idx + sc->max_volumes - 1;
1611 
1612 	for (id = start_idx; id < end_idx; id++) {
1613 		mt_entry = &sc->mapping_table[id];
1614 		if ((mt_entry->physical_id != 0) &&
1615 		    (mt_entry->missing_count == 0)) {
1616 			found_volume = 1;
1617 			break;
1618 		}
1619 	}
1620 
1621 	if (!found_volume)
1622 		goto out;
1623 
1624 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1625 		printf("%s: command alloc failed\n", __func__);
1626 		goto out;
1627 	}
1628 
1629 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1630 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1631 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1632 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1633 	mpr_lock(sc);
1634 	mpr_wait_command(sc, &cm, 5, CAN_SLEEP);
1635 	mpr_unlock(sc);
1636 
1637 	/*
1638 	 * Don't check for reply, just leave.
1639 	 */
1640 	if (cm)
1641 		mpr_free_command(sc, cm);
1642 
1643 out:
1644 	/*
1645 	 * All of the targets must have the correct value set for
1646 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1647 	 *
1648 	 * The possible values for the 'enable_ssu' variable are:
1649 	 * 0: disable to SSD and HDD
1650 	 * 1: disable only to HDD (default)
1651 	 * 2: disable only to SSD
1652 	 * 3: enable to SSD and HDD
1653 	 * anything else will default to 1.
1654 	 */
1655 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1656 		target = &sc->sassc->targets[targetid];
1657 		if (target->handle == 0x0) {
1658 			continue;
1659 		}
1660 
1661 		if (target->supports_SSU) {
1662 			switch (sc->enable_ssu) {
1663 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1664 				target->stop_at_shutdown = FALSE;
1665 				break;
1666 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1667 				target->stop_at_shutdown = TRUE;
1668 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1669 					target->stop_at_shutdown = FALSE;
1670 				}
1671 				break;
1672 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1673 				target->stop_at_shutdown = TRUE;
1674 				break;
1675 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1676 			default:
1677 				target->stop_at_shutdown = TRUE;
1678 				if ((target->flags &
1679 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1680 					target->stop_at_shutdown = FALSE;
1681 				}
1682 				break;
1683 			}
1684 		}
1685 	}
1686 	mprsas_SSU_to_SATA_devices(sc, howto);
1687 }
1688