xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*-
2  * Copyright (c) 2011-2014 LSI Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * LSI MPT-Fusion Host Adapter FreeBSD
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /* Communications core for LSI MPT2 */
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/queue.h>
49 #include <sys/kthread.h>
50 #include <sys/taskqueue.h>
51 #include <sys/sbuf.h>
52 
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <sys/rman.h>
56 
57 #include <machine/stdarg.h>
58 
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_debug.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_periph.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68 
69 #include <dev/mpr/mpi/mpi2_type.h>
70 #include <dev/mpr/mpi/mpi2.h>
71 #include <dev/mpr/mpi/mpi2_ioc.h>
72 #include <dev/mpr/mpi/mpi2_sas.h>
73 #include <dev/mpr/mpi/mpi2_cnfg.h>
74 #include <dev/mpr/mpi/mpi2_init.h>
75 #include <dev/mpr/mpi/mpi2_raid.h>
76 #include <dev/mpr/mpi/mpi2_tool.h>
77 #include <dev/mpr/mpr_ioctl.h>
78 #include <dev/mpr/mprvar.h>
79 #include <dev/mpr/mpr_table.h>
80 #include <dev/mpr/mpr_sas.h>
81 
82 /* For Hashed SAS Address creation for SATA Drives */
83 #define MPT2SAS_SN_LEN 20
84 #define MPT2SAS_MN_LEN 40
85 
86 struct mpr_fw_event_work {
87 	u16			event;
88 	void			*event_data;
89 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
90 };
91 
92 union _sata_sas_address {
93 	u8 wwid[8];
94 	struct {
95 		u32 high;
96 		u32 low;
97 	} word;
98 };
99 
100 /*
101  * define the IDENTIFY DEVICE structure
102  */
103 struct _ata_identify_device_data {
104 	u16 reserved1[10];	/* 0-9 */
105 	u16 serial_number[10];	/* 10-19 */
106 	u16 reserved2[7];	/* 20-26 */
107 	u16 model_number[20];	/* 27-46*/
108 	u16 reserved3[209];	/* 47-255*/
109 };
110 static u32 event_count;
111 static void mprsas_fw_work(struct mpr_softc *sc,
112     struct mpr_fw_event_work *fw_event);
113 static void mprsas_fw_event_free(struct mpr_softc *,
114     struct mpr_fw_event_work *);
115 static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
116 static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
117     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
118     u32 devinfo);
119 int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
120     u64 *sas_address, u16 handle, u32 device_info);
121 static int mprsas_volume_add(struct mpr_softc *sc,
122     u16 handle);
123 static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc);
124 static void mprsas_stop_unit_done(struct cam_periph *periph,
125     union ccb *done_ccb);
126 
127 void
128 mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
129     MPI2_EVENT_NOTIFICATION_REPLY *event)
130 {
131 	struct mpr_fw_event_work *fw_event;
132 	u16 sz;
133 
134 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
135 	mpr_print_evt_sas(sc, event);
136 	mprsas_record_event(sc, event);
137 
138 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
139 	     M_ZERO|M_NOWAIT);
140 	if (!fw_event) {
141 		printf("%s: allocate failed for fw_event\n", __func__);
142 		return;
143 	}
144 	sz = le16toh(event->EventDataLength) * 4;
145 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
146 	if (!fw_event->event_data) {
147 		printf("%s: allocate failed for event_data\n", __func__);
148 		free(fw_event, M_MPR);
149 		return;
150 	}
151 
152 	bcopy(event->EventData, fw_event->event_data, sz);
153 	fw_event->event = event->Event;
154 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
155 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
156 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
157 	    sc->track_mapping_events)
158 		sc->pending_map_events++;
159 
160 	/*
161 	 * When wait_for_port_enable flag is set, make sure that all the events
162 	 * are processed. Increment the startup_refcount and decrement it after
163 	 * events are processed.
164 	 */
165 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
166 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
167 	    sc->wait_for_port_enable)
168 		mprsas_startup_increment(sc->sassc);
169 
170 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
171 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
172 
173 }
174 
175 static void
176 mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
177 {
178 
179 	free(fw_event->event_data, M_MPR);
180 	free(fw_event, M_MPR);
181 }
182 
183 /**
184  * _mpr_fw_work - delayed task for processing firmware events
185  * @sc: per adapter object
186  * @fw_event: The fw_event_work object
187  * Context: user.
188  *
189  * Return nothing.
190  */
191 static void
192 mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
193 {
194 	struct mprsas_softc *sassc;
195 	sassc = sc->sassc;
196 
197 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
198 	    event_count++, __func__, fw_event->event);
199 	switch (fw_event->event) {
200 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
201 	{
202 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
203 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
204 		int i;
205 
206 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
207 		    fw_event->event_data;
208 
209 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
210 
211 		for (i = 0; i < data->NumEntries; i++) {
212 			phy = &data->PHY[i];
213 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
214 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
215 				if (mprsas_add_device(sc,
216 				    le16toh(phy->AttachedDevHandle),
217 				    phy->LinkRate)) {
218 					printf("%s: failed to add device with "
219 					    "handle 0x%x\n", __func__,
220 					    le16toh(phy->AttachedDevHandle));
221 					mprsas_prepare_remove(sassc, le16toh(
222 					    phy->AttachedDevHandle));
223 				}
224 				break;
225 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
226 				mprsas_prepare_remove(sassc, le16toh(
227 				    phy->AttachedDevHandle));
228 				break;
229 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
230 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
231 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
232 			default:
233 				break;
234 			}
235 		}
236 		/*
237 		 * refcount was incremented for this event in
238 		 * mprsas_evt_handler.  Decrement it here because the event has
239 		 * been processed.
240 		 */
241 		mprsas_startup_decrement(sassc);
242 		break;
243 	}
244 	case MPI2_EVENT_SAS_DISCOVERY:
245 	{
246 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
247 
248 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
249 
250 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
251 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start "
252 			    "event\n");
253 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
254 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
255 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
256 			mprsas_discovery_end(sassc);
257 		}
258 		break;
259 	}
260 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
261 	{
262 		Mpi2EventDataSasEnclDevStatusChange_t *data;
263 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
264 		    fw_event->event_data;
265 		mpr_mapping_enclosure_dev_status_change_event(sc,
266 		    fw_event->event_data);
267 		break;
268 	}
269 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
270 	{
271 		Mpi2EventIrConfigElement_t *element;
272 		int i;
273 		u8 foreign_config, reason;
274 		u16 elementType;
275 		Mpi2EventDataIrConfigChangeList_t *event_data;
276 		struct mprsas_target *targ;
277 		unsigned int id;
278 
279 		event_data = fw_event->event_data;
280 		foreign_config = (le32toh(event_data->Flags) &
281 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
282 
283 		element =
284 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
285 		id = mpr_mapping_get_raid_id_from_handle(sc,
286 		    element->VolDevHandle);
287 
288 		mpr_mapping_ir_config_change_event(sc, event_data);
289 		for (i = 0; i < event_data->NumElements; i++, element++) {
290 			reason = element->ReasonCode;
291 			elementType = le16toh(element->ElementFlags) &
292 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
293 			/*
294 			 * check for element type of Phys Disk or Hot Spare
295 			 */
296 			if ((elementType !=
297 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
298 			    && (elementType !=
299 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
300 				// do next element
301 				goto skip_fp_send;
302 
303 			/*
304 			 * check for reason of Hide, Unhide, PD Created, or PD
305 			 * Deleted
306 			 */
307 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
308 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
309 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
310 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
311 				goto skip_fp_send;
312 
313 			// check for a reason of Hide or PD Created
314 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
315 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
316 			{
317 				// build RAID Action message
318 				Mpi2RaidActionRequest_t	*action;
319 				Mpi2RaidActionReply_t *reply;
320 				struct mpr_command *cm;
321 				int error = 0;
322 				if ((cm = mpr_alloc_command(sc)) == NULL) {
323 					printf("%s: command alloc failed\n",
324 					    __func__);
325 					return;
326 				}
327 
328 				mpr_dprint(sc, MPR_INFO, "Sending FP action "
329 				    "from "
330 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
331 				    ":\n");
332 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
333 				action->Function = MPI2_FUNCTION_RAID_ACTION;
334 				action->Action =
335 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
336 				action->PhysDiskNum = element->PhysDiskNum;
337 				cm->cm_desc.Default.RequestFlags =
338 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
339 				error = mpr_request_polled(sc, cm);
340 				reply = (Mpi2RaidActionReply_t *)cm->cm_reply;
341 				if (error || (reply == NULL)) {
342 					/* FIXME */
343 					/*
344 					 * If the poll returns error then we
345 					 * need to do diag reset
346 					 */
347 					printf("%s: poll for page completed "
348 					    "with error %d", __func__, error);
349 				}
350 				if (reply && (le16toh(reply->IOCStatus) &
351 				    MPI2_IOCSTATUS_MASK) !=
352 				    MPI2_IOCSTATUS_SUCCESS) {
353 					mpr_dprint(sc, MPR_INFO, "%s: error "
354 					    "sending RaidActionPage; iocstatus "
355 					    "= 0x%x\n", __func__,
356 					    le16toh(reply->IOCStatus));
357 				}
358 
359 				if (cm)
360 					mpr_free_command(sc, cm);
361 			}
362 skip_fp_send:
363 			mpr_dprint(sc, MPR_INFO, "Received "
364 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
365 			    "code %x:\n", element->ReasonCode);
366 			switch (element->ReasonCode) {
367 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
368 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
369 				if (!foreign_config) {
370 					if (mprsas_volume_add(sc,
371 					    le16toh(element->VolDevHandle))) {
372 						printf("%s: failed to add RAID "
373 						    "volume with handle 0x%x\n",
374 						    __func__, le16toh(element->
375 						    VolDevHandle));
376 					}
377 				}
378 				break;
379 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
380 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
381 				/*
382 				 * Rescan after volume is deleted or removed.
383 				 */
384 				if (!foreign_config) {
385 					if (id == MPR_MAP_BAD_ID) {
386 						printf("%s: could not get ID "
387 						    "for volume with handle "
388 						    "0x%04x\n", __func__,
389 						    le16toh(element->
390 						    VolDevHandle));
391 						break;
392 					}
393 
394 					targ = &sassc->targets[id];
395 					targ->handle = 0x0;
396 					targ->encl_slot = 0x0;
397 					targ->encl_handle = 0x0;
398 					targ->encl_level_valid = 0x0;
399 					targ->encl_level = 0x0;
400 					targ->connector_name[0] = ' ';
401 					targ->connector_name[1] = ' ';
402 					targ->connector_name[2] = ' ';
403 					targ->connector_name[3] = ' ';
404 					targ->exp_dev_handle = 0x0;
405 					targ->phy_num = 0x0;
406 					targ->linkrate = 0x0;
407 					mprsas_rescan_target(sc, targ);
408 					printf("RAID target id 0x%x removed\n",
409 					    targ->tid);
410 				}
411 				break;
412 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
413 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
414 				/*
415 				 * Phys Disk of a volume has been created.  Hide
416 				 * it from the OS.
417 				 */
418 				targ = mprsas_find_target_by_handle(sassc, 0,
419 				    element->PhysDiskDevHandle);
420 				if (targ == NULL)
421 					break;
422 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
423 				mprsas_rescan_target(sc, targ);
424 
425 				break;
426 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
427 				/*
428 				 * Phys Disk of a volume has been deleted.
429 				 * Expose it to the OS.
430 				 */
431 				if (mprsas_add_device(sc,
432 				    le16toh(element->PhysDiskDevHandle), 0)) {
433 					printf("%s: failed to add device with "
434 					    "handle 0x%x\n", __func__,
435 					    le16toh(element->
436 					    PhysDiskDevHandle));
437 					mprsas_prepare_remove(sassc,
438 					    le16toh(element->
439 					    PhysDiskDevHandle));
440 				}
441 				break;
442 			}
443 		}
444 		/*
445 		 * refcount was incremented for this event in
446 		 * mprsas_evt_handler.  Decrement it here because the event has
447 		 * been processed.
448 		 */
449 		mprsas_startup_decrement(sassc);
450 		break;
451 	}
452 	case MPI2_EVENT_IR_VOLUME:
453 	{
454 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
455 
456 		/*
457 		 * Informational only.
458 		 */
459 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
460 		switch (event_data->ReasonCode) {
461 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
462   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
463   			    "changed from 0x%x to 0x%x for Volome with "
464  			    "handle 0x%x", le32toh(event_data->PreviousValue),
465  			    le32toh(event_data->NewValue),
466  			    le16toh(event_data->VolDevHandle));
467 			break;
468 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
469   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
470   			    "changed from 0x%x to 0x%x for Volome with "
471  			    "handle 0x%x", le32toh(event_data->PreviousValue),
472  			    le32toh(event_data->NewValue),
473  			    le16toh(event_data->VolDevHandle));
474 			break;
475 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
476   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
477   			    "changed from 0x%x to 0x%x for Volome with "
478  			    "handle 0x%x", le32toh(event_data->PreviousValue),
479  			    le32toh(event_data->NewValue),
480  			    le16toh(event_data->VolDevHandle));
481 				u32 state;
482 				struct mprsas_target *targ;
483 				state = le32toh(event_data->NewValue);
484 				switch (state) {
485 				case MPI2_RAID_VOL_STATE_MISSING:
486 				case MPI2_RAID_VOL_STATE_FAILED:
487 					mprsas_prepare_volume_remove(sassc,
488 					    event_data->VolDevHandle);
489 					break;
490 
491 				case MPI2_RAID_VOL_STATE_ONLINE:
492 				case MPI2_RAID_VOL_STATE_DEGRADED:
493 				case MPI2_RAID_VOL_STATE_OPTIMAL:
494 					targ =
495 					    mprsas_find_target_by_handle(sassc,
496 					    0, event_data->VolDevHandle);
497 					if (targ) {
498 						printf("%s %d: Volume handle "
499 						    "0x%x is already added \n",
500 						    __func__, __LINE__,
501 						    event_data->VolDevHandle);
502 						break;
503 					}
504 					if (mprsas_volume_add(sc,
505 					    le16toh(event_data->
506 					    VolDevHandle))) {
507 						printf("%s: failed to add RAID "
508 						    "volume with handle 0x%x\n",
509 						    __func__, le16toh(
510 						    event_data->VolDevHandle));
511 					}
512 					break;
513 				default:
514 					break;
515 				}
516 			break;
517 		default:
518 			break;
519 		}
520 		break;
521 	}
522 	case MPI2_EVENT_IR_PHYSICAL_DISK:
523 	{
524 		Mpi2EventDataIrPhysicalDisk_t *event_data =
525 		    fw_event->event_data;
526 		struct mprsas_target *targ;
527 
528 		/*
529 		 * Informational only.
530 		 */
531 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
532 		switch (event_data->ReasonCode) {
533 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
534   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
535   			    "changed from 0x%x to 0x%x for Phys Disk Number "
536   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
537  			    "%d", le32toh(event_data->PreviousValue),
538  			    le32toh(event_data->NewValue),
539 			    event_data->PhysDiskNum,
540  			    le16toh(event_data->PhysDiskDevHandle),
541  			    le16toh(event_data->EnclosureHandle),
542 			    le16toh(event_data->Slot));
543 			break;
544 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
545   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
546   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
547   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
548  			    le32toh(event_data->PreviousValue),
549  			    le32toh(event_data->NewValue),
550 			    event_data->PhysDiskNum,
551  			    le16toh(event_data->PhysDiskDevHandle),
552  			    le16toh(event_data->EnclosureHandle),
553 			    le16toh(event_data->Slot));
554 			break;
555 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
556   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
557   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
558   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
559  			    le32toh(event_data->PreviousValue),
560  			    le32toh(event_data->NewValue),
561 			    event_data->PhysDiskNum,
562  			    le16toh(event_data->PhysDiskDevHandle),
563  			    le16toh(event_data->EnclosureHandle),
564 			    le16toh(event_data->Slot));
565 			switch (event_data->NewValue) {
566 				case MPI2_RAID_PD_STATE_ONLINE:
567 				case MPI2_RAID_PD_STATE_DEGRADED:
568 				case MPI2_RAID_PD_STATE_REBUILDING:
569 				case MPI2_RAID_PD_STATE_OPTIMAL:
570 				case MPI2_RAID_PD_STATE_HOT_SPARE:
571 					targ = mprsas_find_target_by_handle(
572 					    sassc, 0,
573 					    event_data->PhysDiskDevHandle);
574 					if (targ) {
575 						targ->flags |=
576 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
577 						printf("%s %d: Found Target "
578 						    "for handle 0x%x.\n",
579 						    __func__, __LINE__ ,
580 						    event_data->
581 						    PhysDiskDevHandle);
582 					}
583 				break;
584 				case MPI2_RAID_PD_STATE_OFFLINE:
585 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
586 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
587 				default:
588 					targ = mprsas_find_target_by_handle(
589 					    sassc, 0,
590 					    event_data->PhysDiskDevHandle);
591 					if (targ) {
592 						targ->flags |=
593 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
594 						printf("%s %d: Found Target "
595 						    "for handle 0x%x.  \n",
596 						    __func__, __LINE__ ,
597 						    event_data->
598 						    PhysDiskDevHandle);
599 					}
600 				break;
601 			}
602 		default:
603 			break;
604 		}
605 		break;
606 	}
607 	case MPI2_EVENT_IR_OPERATION_STATUS:
608 	{
609 		Mpi2EventDataIrOperationStatus_t *event_data =
610 		    fw_event->event_data;
611 
612 		/*
613 		 * Informational only.
614 		 */
615 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
616 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
617 		    "percent complete for Volume with handle 0x%x",
618 		    event_data->RAIDOperation, event_data->PercentComplete,
619 		    le16toh(event_data->VolDevHandle));
620 		break;
621 	}
622 	case MPI2_EVENT_TEMP_THRESHOLD:
623 	{
624 		pMpi2EventDataTemperature_t	temp_event;
625 
626 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
627 
628 		/*
629 		 * The Temp Sensor Count must be greater than the event's Sensor
630 		 * Num to be valid.  If valid, print the temp thresholds that
631 		 * have been exceeded.
632 		 */
633 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
634 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
635 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
636 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
637 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
638 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
639 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
640 			    temp_event->SensorNum);
641 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
642 			    "%d\n", temp_event->CurrentTemperature);
643 		}
644 		break;
645 	}
646 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
647 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
648 	default:
649 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
650 		    fw_event->event);
651 		break;
652 
653 	}
654 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
655 	    __func__, fw_event->event);
656 	mprsas_fw_event_free(sc, fw_event);
657 }
658 
659 void
660 mprsas_firmware_event_work(void *arg, int pending)
661 {
662 	struct mpr_fw_event_work *fw_event;
663 	struct mpr_softc *sc;
664 
665 	sc = (struct mpr_softc *)arg;
666 	mpr_lock(sc);
667 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
668 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
669 		mprsas_fw_work(sc, fw_event);
670 	}
671 	mpr_unlock(sc);
672 }
673 
674 static int
675 mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate){
676 	char devstring[80];
677 	struct mprsas_softc *sassc;
678 	struct mprsas_target *targ;
679 	Mpi2ConfigReply_t mpi_reply;
680 	Mpi2SasDevicePage0_t config_page;
681 	uint64_t sas_address, sata_sas_address;
682 	uint64_t parent_sas_address = 0;
683 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
684 	u32 device_info, parent_devinfo = 0;
685 	unsigned int id;
686 	int ret;
687 	int error = 0;
688 	struct mprsas_lun *lun;
689 
690 	sassc = sc->sassc;
691 	mprsas_startup_increment(sassc);
692 	if ((mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
693 	     MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
694 		printf("%s: error reading SAS device page0\n", __func__);
695 		error = ENXIO;
696 		goto out;
697 	}
698 
699 	device_info = le32toh(config_page.DeviceInfo);
700 
701 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
702 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
703 		Mpi2ConfigReply_t tmp_mpi_reply;
704 		Mpi2SasDevicePage0_t parent_config_page;
705 
706 		if ((mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
707 		     &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
708 		     le16toh(config_page.ParentDevHandle)))) {
709 			printf("%s: error reading SAS device %#x page0\n",
710 			    __func__, le16toh(config_page.ParentDevHandle));
711 		} else {
712 			parent_sas_address = parent_config_page.SASAddress.High;
713 			parent_sas_address = (parent_sas_address << 32) |
714 			    parent_config_page.SASAddress.Low;
715 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
716 		}
717 	}
718 	/* TODO Check proper endianess */
719 	sas_address = config_page.SASAddress.High;
720 	sas_address = (sas_address << 32) |
721 	    config_page.SASAddress.Low;
722 
723 	if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE)
724 	    == MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
725 		if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
726 			ret = mprsas_get_sas_address_for_sata_disk(sc,
727 			    &sata_sas_address, handle, device_info);
728 			if (!ret)
729 				id = mpr_mapping_get_sas_id(sc,
730 				    sata_sas_address, handle);
731 			else
732 				id = mpr_mapping_get_sas_id(sc,
733 				    sas_address, handle);
734 		} else
735 			id = mpr_mapping_get_sas_id(sc, sas_address,
736 			    handle);
737 	} else
738 		id = mpr_mapping_get_sas_id(sc, sas_address, handle);
739 
740 	if (id == MPR_MAP_BAD_ID) {
741 		printf("failure at %s:%d/%s()! Could not get ID for device "
742 		    "with handle 0x%04x\n", __FILE__, __LINE__, __func__,
743 		    handle);
744 		error = ENXIO;
745 		goto out;
746 	}
747 
748 	if (mprsas_check_id(sassc, id) != 0) {
749 		device_printf(sc->mpr_dev, "Excluding target id %d\n", id);
750 		error = ENXIO;
751 		goto out;
752 	}
753 
754 	mpr_dprint(sc, MPR_MAPPING, "SAS Address from SAS device page0 = %jx\n",
755 	    sas_address);
756 	targ = &sassc->targets[id];
757 	targ->devinfo = device_info;
758 	targ->devname = le32toh(config_page.DeviceName.High);
759 	targ->devname = (targ->devname << 32) |
760 	    le32toh(config_page.DeviceName.Low);
761 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
762 	targ->encl_slot = le16toh(config_page.Slot);
763 	targ->encl_level = config_page.EnclosureLevel;
764 	targ->connector_name[0] = config_page.ConnectorName[0];
765 	targ->connector_name[1] = config_page.ConnectorName[1];
766 	targ->connector_name[2] = config_page.ConnectorName[2];
767 	targ->connector_name[3] = config_page.ConnectorName[3];
768 	targ->handle = handle;
769 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
770 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
771 	targ->parent_sasaddr = le64toh(parent_sas_address);
772 	targ->parent_devinfo = parent_devinfo;
773 	targ->tid = id;
774 	targ->linkrate = (linkrate>>4);
775 	targ->flags = 0;
776 	if (le16toh(config_page.Flags) &
777 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) {
778 		targ->scsi_req_desc_type =
779 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
780 	}
781 	if (le16toh(config_page.Flags) &
782 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
783 		targ->encl_level_valid = TRUE;
784 	}
785 	TAILQ_INIT(&targ->commands);
786 	TAILQ_INIT(&targ->timedout_commands);
787 	while (!SLIST_EMPTY(&targ->luns)) {
788 		lun = SLIST_FIRST(&targ->luns);
789 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
790 		free(lun, M_MPR);
791 	}
792 	SLIST_INIT(&targ->luns);
793 
794 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
795 	mpr_dprint(sc, (MPR_XINFO|MPR_MAPPING), "Found device <%s> <%s> "
796 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
797 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
798 	    targ->handle, targ->encl_handle, targ->encl_slot);
799 	if (targ->encl_level_valid) {
800 		mpr_dprint(sc, (MPR_XINFO|MPR_MAPPING), "At enclosure level %d "
801 		    "and connector name (%4s)\n", targ->encl_level,
802 		    targ->connector_name);
803 	}
804 #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
805     (__FreeBSD_version < 902502)
806 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
807 #endif
808 		mprsas_rescan_target(sc, targ);
809 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
810 out:
811 	mprsas_startup_decrement(sassc);
812 	return (error);
813 
814 }
815 
816 int
817 mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
818     u64 *sas_address, u16 handle, u32 device_info)
819 {
820 	Mpi2SataPassthroughReply_t mpi_reply;
821 	int i, rc, try_count;
822 	u32 *bufferptr;
823 	union _sata_sas_address hash_address;
824 	struct _ata_identify_device_data ata_identify;
825 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
826 	u32 ioc_status;
827 	u8 sas_status;
828 
829 	memset(&ata_identify, 0, sizeof(ata_identify));
830 	try_count = 0;
831 	do {
832 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
833 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
834 		try_count++;
835 		ioc_status = le16toh(mpi_reply.IOCStatus)
836 		    & MPI2_IOCSTATUS_MASK;
837 		sas_status = mpi_reply.SASStatus;
838 	} while ((rc == -EAGAIN || ioc_status || sas_status) &&
839 	    (try_count < 5));
840 
841 	if (rc == 0 && !ioc_status && !sas_status) {
842 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
843 		    "successfully for handle = 0x%x with try_count = %d\n",
844 		    __func__, handle, try_count);
845 	} else {
846 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
847 		    __func__, handle);
848 		return -1;
849 	}
850 	/* Copy & byteswap the 40 byte model number to a buffer */
851 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
852 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
853 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
854 	}
855 	/* Copy & byteswap the 20 byte serial number to a buffer */
856 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
857 		buffer[MPT2SAS_MN_LEN + i] =
858 		    ((u8 *)ata_identify.serial_number)[i + 1];
859 		buffer[MPT2SAS_MN_LEN + i + 1] =
860 		    ((u8 *)ata_identify.serial_number)[i];
861 	}
862 	bufferptr = (u32 *)buffer;
863 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
864 	 * so loop through the first 56 bytes (7*8),
865 	 * and then add in the last dword.
866 	 */
867 	hash_address.word.low  = 0;
868 	hash_address.word.high = 0;
869 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
870 		hash_address.word.low += *bufferptr;
871 		bufferptr++;
872 		hash_address.word.high += *bufferptr;
873 		bufferptr++;
874 	}
875 	/* Add the last dword */
876 	hash_address.word.low += *bufferptr;
877 	/* Make sure the hash doesn't start with 5, because it could clash
878 	 * with a SAS address. Change 5 to a D.
879 	 */
880 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
881 		hash_address.word.high |= 0x00000080;
882 	*sas_address = (u64)hash_address.wwid[0] << 56 |
883 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
884 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
885 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
886 	    (u64)hash_address.wwid[7];
887 	return 0;
888 }
889 
890 static int
891 mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
892     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
893 {
894 	Mpi2SataPassthroughRequest_t *mpi_request;
895 	Mpi2SataPassthroughReply_t *reply;
896 	struct mpr_command *cm;
897 	char *buffer;
898 	int error = 0;
899 
900 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
901 	if (!buffer)
902 		return ENOMEM;
903 
904 	if ((cm = mpr_alloc_command(sc)) == NULL) {
905 		free(buffer, M_MPR);
906 		return (EBUSY);
907 	}
908 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
909 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
910 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
911 	mpi_request->VF_ID = 0;
912 	mpi_request->DevHandle = htole16(handle);
913 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
914 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
915 	mpi_request->DataLength = htole32(sz);
916 	mpi_request->CommandFIS[0] = 0x27;
917 	mpi_request->CommandFIS[1] = 0x80;
918 	mpi_request->CommandFIS[2] =  (devinfo &
919 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
920 	cm->cm_sge = &mpi_request->SGL;
921 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
922 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
923 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
924 	cm->cm_data = buffer;
925 	cm->cm_length = htole32(sz);
926 	error = mpr_wait_command(sc, cm, 60, CAN_SLEEP);
927 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
928 	if (error || (reply == NULL)) {
929 		/* FIXME */
930 		/*
931 		 * If the request returns an error then we need to do a diag
932 		 * reset
933 		 */
934 		printf("%s: request for page completed with error %d",
935 		    __func__, error);
936 		error = ENXIO;
937 		goto out;
938 	}
939 	bcopy(buffer, id_buffer, sz);
940 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
941 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
942 	    MPI2_IOCSTATUS_SUCCESS) {
943 		printf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n",
944 		    __func__, reply->IOCStatus);
945 		error = ENXIO;
946 		goto out;
947 	}
948 out:
949 	mpr_free_command(sc, cm);
950 	free(buffer, M_MPR);
951 	return (error);
952 }
953 
954 static int
955 mprsas_volume_add(struct mpr_softc *sc, u16 handle)
956 {
957 	struct mprsas_softc *sassc;
958 	struct mprsas_target *targ;
959 	u64 wwid;
960 	unsigned int id;
961 	int error = 0;
962 	struct mprsas_lun *lun;
963 
964 	sassc = sc->sassc;
965 	mprsas_startup_increment(sassc);
966 	/* wwid is endian safe */
967 	mpr_config_get_volume_wwid(sc, handle, &wwid);
968 	if (!wwid) {
969 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
970 		    __func__);
971 		error = ENXIO;
972 		goto out;
973 	}
974 
975 	id = mpr_mapping_get_raid_id(sc, wwid, handle);
976 	if (id == MPR_MAP_BAD_ID) {
977 		printf("%s: could not get ID for volume with handle 0x%04x and "
978 		    "WWID 0x%016llx\n", __func__, handle,
979 		    (unsigned long long)wwid);
980 		error = ENXIO;
981 		goto out;
982 	}
983 
984 	targ = &sassc->targets[id];
985 	targ->tid = id;
986 	targ->handle = handle;
987 	targ->devname = wwid;
988 	TAILQ_INIT(&targ->commands);
989 	TAILQ_INIT(&targ->timedout_commands);
990 	while (!SLIST_EMPTY(&targ->luns)) {
991 		lun = SLIST_FIRST(&targ->luns);
992 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
993 		free(lun, M_MPR);
994 	}
995 	SLIST_INIT(&targ->luns);
996 #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
997     (__FreeBSD_version < 902502)
998 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
999 #endif
1000 		mprsas_rescan_target(sc, targ);
1001 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1002 	    targ->tid, wwid);
1003 out:
1004 	mprsas_startup_decrement(sassc);
1005 	return (error);
1006 }
1007 
1008 /**
1009  * mprsas_SSU_to_SATA_devices
1010  * @sc: per adapter object
1011  *
1012  * Looks through the target list and issues a StartStopUnit SCSI command to each
1013  * SATA direct-access device.  This helps to ensure that data corruption is
1014  * avoided when the system is being shut down.  This must be called after the IR
1015  * System Shutdown RAID Action is sent if in IR mode.
1016  *
1017  * Return nothing.
1018  */
1019 static void
1020 mprsas_SSU_to_SATA_devices(struct mpr_softc *sc)
1021 {
1022 	struct mprsas_softc *sassc = sc->sassc;
1023 	union ccb *ccb;
1024 	path_id_t pathid = cam_sim_path(sassc->sim);
1025 	target_id_t targetid;
1026 	struct mprsas_target *target;
1027 	struct mprsas_lun *lun;
1028 	char path_str[64];
1029 	struct timeval cur_time, start_time;
1030 
1031 	mpr_lock(sc);
1032 
1033 	/*
1034 	 * For each LUN of each target, issue a StartStopUnit command to stop
1035 	 * the device.
1036 	 */
1037 	sc->SSU_started = TRUE;
1038 	sc->SSU_refcount = 0;
1039 	for (targetid = 0; targetid < sc->facts->MaxTargets; targetid++) {
1040 		target = &sassc->targets[targetid];
1041 		if (target->handle == 0x0) {
1042 			continue;
1043 		}
1044 
1045 		SLIST_FOREACH(lun, &target->luns, lun_link) {
1046 			ccb = xpt_alloc_ccb_nowait();
1047 			if (ccb == NULL) {
1048 				mpr_unlock(sc);
1049 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc "
1050 				    "CCB to stop unit.\n");
1051 				return;
1052 			}
1053 
1054 			/*
1055 			 * The stop_at_shutdown flag will be set if this LUN is
1056 			 * a SATA direct-access end device.
1057 			 */
1058 			if (lun->stop_at_shutdown) {
1059 				if (xpt_create_path(&ccb->ccb_h.path,
1060 				    xpt_periph, pathid, targetid,
1061 				    lun->lun_id) != CAM_REQ_CMP) {
1062 					mpr_dprint(sc, MPR_FAULT, "Unable to "
1063 					    "create LUN path to stop unit.\n");
1064 					xpt_free_ccb(ccb);
1065 					mpr_unlock(sc);
1066 					return;
1067 				}
1068 				xpt_path_string(ccb->ccb_h.path, path_str,
1069 				    sizeof(path_str));
1070 
1071 				mpr_dprint(sc, MPR_INFO, "Sending StopUnit: "
1072 				    "path %s handle %d\n", path_str,
1073 				    target->handle);
1074 
1075 				/*
1076 				 * Issue a START STOP UNIT command for the LUN.
1077 				 * Increment the SSU counter to be used to
1078 				 * count the number of required replies.
1079 				 */
1080 				mpr_dprint(sc, MPR_INFO, "Incrementing SSU "
1081 				    "count\n");
1082 				sc->SSU_refcount++;
1083 				ccb->ccb_h.target_id =
1084 				    xpt_path_target_id(ccb->ccb_h.path);
1085 				ccb->ccb_h.target_lun = lun->lun_id;
1086 				ccb->ccb_h.ppriv_ptr1 = sassc;
1087 				scsi_start_stop(&ccb->csio,
1088 				    /*retries*/0,
1089 				    mprsas_stop_unit_done,
1090 				    MSG_SIMPLE_Q_TAG,
1091 				    /*start*/FALSE,
1092 				    /*load/eject*/0,
1093 				    /*immediate*/FALSE,
1094 				    MPR_SENSE_LEN,
1095 				    /*timeout*/10000);
1096 				xpt_action(ccb);
1097 			}
1098 		}
1099 	}
1100 
1101 	mpr_unlock(sc);
1102 
1103 	/*
1104 	 * Wait until all of the SSU commands have completed or time has
1105 	 * expired (60 seconds).  pause for 100ms each time through.  If any
1106 	 * command times out, the target will be reset in the SCSI command
1107 	 * timeout routine.
1108 	 */
1109 	getmicrotime(&start_time);
1110 	while (sc->SSU_refcount) {
1111 		pause("mprwait", hz/10);
1112 
1113 		getmicrotime(&cur_time);
1114 		if ((cur_time.tv_sec - start_time.tv_sec) > 60) {
1115 			mpr_dprint(sc, MPR_FAULT, "Time has expired waiting "
1116 			    "for SSU commands to complete.\n");
1117 			break;
1118 		}
1119 	}
1120 }
1121 
1122 static void
1123 mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1124 {
1125 	struct mprsas_softc *sassc;
1126 	char path_str[64];
1127 
1128 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1129 
1130 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1131 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1132 	    path_str);
1133 
1134 	if (done_ccb == NULL)
1135 		return;
1136 
1137 	/*
1138 	 * Nothing more to do except free the CCB and path.  If the command
1139 	 * timed out, an abort reset, then target reset will be issued during
1140 	 * the SCSI Command process.
1141 	 */
1142 	xpt_free_path(done_ccb->ccb_h.path);
1143 	xpt_free_ccb(done_ccb);
1144 }
1145 
1146 /**
1147  * mprsas_ir_shutdown - IR shutdown notification
1148  * @sc: per adapter object
1149  *
1150  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1151  * the host system is shutting down.
1152  *
1153  * Return nothing.
1154  */
1155 void
1156 mprsas_ir_shutdown(struct mpr_softc *sc)
1157 {
1158 	u16 volume_mapping_flags;
1159 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1160 	struct dev_mapping_table *mt_entry;
1161 	u32 start_idx, end_idx;
1162 	unsigned int id, found_volume = 0;
1163 	struct mpr_command *cm;
1164 	Mpi2RaidActionRequest_t	*action;
1165 
1166 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1167 
1168 	/* is IR firmware build loaded? */
1169 	if (!sc->ir_firmware)
1170 		goto out;
1171 
1172 	/* are there any volumes?  Look at IR target IDs. */
1173 	// TODO-later, this should be looked up in the RAID config structure
1174 	// when it is implemented.
1175 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1176 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1177 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1178 		start_idx = 0;
1179 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1180 			start_idx = 1;
1181 	} else
1182 		start_idx = sc->max_devices - sc->max_volumes;
1183 	end_idx = start_idx + sc->max_volumes - 1;
1184 
1185 	for (id = start_idx; id < end_idx; id++) {
1186 		mt_entry = &sc->mapping_table[id];
1187 		if ((mt_entry->physical_id != 0) &&
1188 		    (mt_entry->missing_count == 0)) {
1189 			found_volume = 1;
1190 			break;
1191 		}
1192 	}
1193 
1194 	if (!found_volume)
1195 		goto out;
1196 
1197 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1198 		printf("%s: command alloc failed\n", __func__);
1199 		goto out;
1200 	}
1201 
1202 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1203 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1204 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1205 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1206 	mpr_lock(sc);
1207 	mpr_wait_command(sc, cm, 5, CAN_SLEEP);
1208 	mpr_unlock(sc);
1209 
1210 	/*
1211 	 * Don't check for reply, just leave.
1212 	 */
1213 	if (cm)
1214 		mpr_free_command(sc, cm);
1215 
1216 out:
1217 	mprsas_SSU_to_SATA_devices(sc);
1218 }
1219