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