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