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