xref: /freebsd/sys/dev/mps/mps_sas_lsi.c (revision 52f72944b8f5abb2386eae924357dee8aea17d5b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011-2015 LSI Corp.
5  * Copyright (c) 2013-2015 Avago Technologies
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /* Communications core for Avago Technologies (LSI) MPT2 */
36 
37 /* TODO Move headers to mpsvar */
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/selinfo.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/conf.h>
46 #include <sys/bio.h>
47 #include <sys/malloc.h>
48 #include <sys/uio.h>
49 #include <sys/sysctl.h>
50 #include <sys/endian.h>
51 #include <sys/queue.h>
52 #include <sys/kthread.h>
53 #include <sys/taskqueue.h>
54 #include <sys/sbuf.h>
55 #include <sys/reboot.h>
56 
57 #include <machine/bus.h>
58 #include <machine/resource.h>
59 #include <sys/rman.h>
60 
61 #include <machine/stdarg.h>
62 
63 #include <cam/cam.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_xpt_periph.h>
69 #include <cam/cam_periph.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
72 
73 #include <dev/mps/mpi/mpi2_type.h>
74 #include <dev/mps/mpi/mpi2.h>
75 #include <dev/mps/mpi/mpi2_ioc.h>
76 #include <dev/mps/mpi/mpi2_sas.h>
77 #include <dev/mps/mpi/mpi2_cnfg.h>
78 #include <dev/mps/mpi/mpi2_init.h>
79 #include <dev/mps/mpi/mpi2_raid.h>
80 #include <dev/mps/mpi/mpi2_tool.h>
81 #include <dev/mps/mps_ioctl.h>
82 #include <dev/mps/mpsvar.h>
83 #include <dev/mps/mps_table.h>
84 #include <dev/mps/mps_sas.h>
85 
86 /* For Hashed SAS Address creation for SATA Drives */
87 #define MPT2SAS_SN_LEN 20
88 #define MPT2SAS_MN_LEN 40
89 
90 struct mps_fw_event_work {
91 	u16			event;
92 	void			*event_data;
93 	TAILQ_ENTRY(mps_fw_event_work)	ev_link;
94 };
95 
96 union _sata_sas_address {
97 	u8 wwid[8];
98 	struct {
99 		u32 high;
100 		u32 low;
101 	} word;
102 };
103 
104 /*
105  * define the IDENTIFY DEVICE structure
106  */
107 struct _ata_identify_device_data {
108 	u16 reserved1[10];	/* 0-9 */
109 	u16 serial_number[10];	/* 10-19 */
110 	u16 reserved2[7];	/* 20-26 */
111 	u16 model_number[20];	/* 27-46*/
112 	u16 reserved3[170];	/* 47-216 */
113 	u16 rotational_speed;	/* 217 */
114 	u16 reserved4[38];	/* 218-255 */
115 };
116 static u32 event_count;
117 static void mpssas_fw_work(struct mps_softc *sc,
118     struct mps_fw_event_work *fw_event);
119 static void mpssas_fw_event_free(struct mps_softc *,
120     struct mps_fw_event_work *);
121 static int mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate);
122 static int mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
123     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
124     u32 devinfo);
125 static void mpssas_ata_id_timeout(void *data);
126 int mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
127     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
128 static int mpssas_volume_add(struct mps_softc *sc,
129     u16 handle);
130 static void mpssas_SSU_to_SATA_devices(struct mps_softc *sc, int howto);
131 static void mpssas_stop_unit_done(struct cam_periph *periph,
132     union ccb *done_ccb);
133 
134 void
135 mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
136     MPI2_EVENT_NOTIFICATION_REPLY *event)
137 {
138 	struct mps_fw_event_work *fw_event;
139 	u16 sz;
140 
141 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
142 	MPS_DPRINT_EVENT(sc, sas, event);
143 	mpssas_record_event(sc, event);
144 
145 	fw_event = malloc(sizeof(struct mps_fw_event_work), M_MPT2,
146 	     M_ZERO|M_NOWAIT);
147 	if (!fw_event) {
148 		printf("%s: allocate failed for fw_event\n", __func__);
149 		return;
150 	}
151 	sz = le16toh(event->EventDataLength) * 4;
152 	fw_event->event_data = malloc(sz, M_MPT2, M_ZERO|M_NOWAIT);
153 	if (!fw_event->event_data) {
154 		printf("%s: allocate failed for event_data\n", __func__);
155 		free(fw_event, M_MPT2);
156 		return;
157 	}
158 
159 	bcopy(event->EventData, fw_event->event_data, sz);
160 	fw_event->event = event->Event;
161 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
162 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
163 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
164 	    sc->track_mapping_events)
165 		sc->pending_map_events++;
166 
167 	/*
168 	 * When wait_for_port_enable flag is set, make sure that all the events
169 	 * are processed. Increment the startup_refcount and decrement it after
170 	 * events are processed.
171 	 */
172 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
173 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
174 	    sc->wait_for_port_enable)
175 		mpssas_startup_increment(sc->sassc);
176 
177 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
178 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
179 
180 }
181 
182 static void
183 mpssas_fw_event_free(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
184 {
185 
186 	free(fw_event->event_data, M_MPT2);
187 	free(fw_event, M_MPT2);
188 }
189 
190 /**
191  * _mps_fw_work - delayed task for processing firmware events
192  * @sc: per adapter object
193  * @fw_event: The fw_event_work object
194  * Context: user.
195  *
196  * Return nothing.
197  */
198 static void
199 mpssas_fw_work(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
200 {
201 	struct mpssas_softc *sassc;
202 	sassc = sc->sassc;
203 
204 	mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
205 			event_count++,__func__,fw_event->event);
206 	switch (fw_event->event) {
207 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
208 	{
209 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
210 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
211 		int i;
212 
213 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
214 		    fw_event->event_data;
215 
216 		mps_mapping_topology_change_event(sc, fw_event->event_data);
217 
218 		for (i = 0; i < data->NumEntries; i++) {
219 			phy = &data->PHY[i];
220 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
221 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
222 				if (mpssas_add_device(sc,
223 				    le16toh(phy->AttachedDevHandle),
224 				    phy->LinkRate)){
225 					mps_dprint(sc, MPS_ERROR, "%s: "
226 					    "failed to add device with handle "
227 					    "0x%x\n", __func__,
228 					    le16toh(phy->AttachedDevHandle));
229 					mpssas_prepare_remove(sassc, le16toh(
230 						phy->AttachedDevHandle));
231 				}
232 				break;
233 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
234 				mpssas_prepare_remove(sassc,le16toh(
235 					phy->AttachedDevHandle));
236 				break;
237 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
238 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
239 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
240 			default:
241 				break;
242 			}
243 		}
244 		/*
245 		 * refcount was incremented for this event in
246 		 * mpssas_evt_handler.  Decrement it here because the event has
247 		 * been processed.
248 		 */
249 		mpssas_startup_decrement(sassc);
250 		break;
251 	}
252 	case MPI2_EVENT_SAS_DISCOVERY:
253 	{
254 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
255 
256 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
257 
258 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
259 			mps_dprint(sc, MPS_TRACE,"SAS discovery start event\n");
260 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
261 			mps_dprint(sc, MPS_TRACE,"SAS discovery stop event\n");
262 			sassc->flags &= ~MPSSAS_IN_DISCOVERY;
263 			mpssas_discovery_end(sassc);
264 		}
265 		break;
266 	}
267 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
268 	{
269 		Mpi2EventDataSasEnclDevStatusChange_t *data;
270 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
271 		    fw_event->event_data;
272 		mps_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;
281 		Mpi2EventDataIrConfigChangeList_t *event_data;
282 		struct mpssas_target *targ;
283 		unsigned int id;
284 
285 		event_data = fw_event->event_data;
286 		foreign_config = (le32toh(event_data->Flags) &
287 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
288 
289 		element =
290 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
291 		id = mps_mapping_get_raid_tid_from_handle(sc,
292 		    element->VolDevHandle);
293 
294 		mps_mapping_ir_config_change_event(sc, event_data);
295 
296 		for (i = 0; i < event_data->NumElements; i++, element++) {
297 			switch (element->ReasonCode) {
298 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
299 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
300 				if (!foreign_config) {
301 					if (mpssas_volume_add(sc,
302 					    le16toh(element->VolDevHandle))){
303 						printf("%s: failed to add RAID "
304 						    "volume with handle 0x%x\n",
305 						    __func__, le16toh(element->
306 						    VolDevHandle));
307 					}
308 				}
309 				break;
310 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
311 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
312 				/*
313 				 * Rescan after volume is deleted or removed.
314 				 */
315 				if (!foreign_config) {
316 					if (id == MPS_MAP_BAD_ID) {
317 						printf("%s: could not get ID "
318 						    "for volume with handle "
319 						    "0x%04x\n", __func__,
320 						    le16toh(element->VolDevHandle));
321 						break;
322 					}
323 
324 					targ = &sassc->targets[id];
325 					targ->handle = 0x0;
326 					targ->encl_slot = 0x0;
327 					targ->encl_handle = 0x0;
328 					targ->exp_dev_handle = 0x0;
329 					targ->phy_num = 0x0;
330 					targ->linkrate = 0x0;
331 					mpssas_rescan_target(sc, targ);
332 					printf("RAID target id 0x%x removed\n",
333 					    targ->tid);
334 				}
335 				break;
336 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
337 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
338 				/*
339 				 * Phys Disk of a volume has been created.  Hide
340 				 * it from the OS.
341 				 */
342 				targ = mpssas_find_target_by_handle(sassc, 0,
343 				    element->PhysDiskDevHandle);
344 				if (targ == NULL)
345 					break;
346 
347 				/*
348 				 * Set raid component flags only if it is not
349 				 * WD. OR WrapDrive with
350 				 * WD_HIDE_ALWAYS/WD_HIDE_IF_VOLUME is set in
351 				 * NVRAM
352 				 */
353 				if((!sc->WD_available) ||
354 				((sc->WD_available &&
355 				(sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) ||
356 				(sc->WD_valid_config && (sc->WD_hide_expose ==
357 				MPS_WD_HIDE_IF_VOLUME)))) {
358 					targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
359 				}
360 				mpssas_rescan_target(sc, targ);
361 
362 				break;
363 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
364 				/*
365 				 * Phys Disk of a volume has been deleted.
366 				 * Expose it to the OS.
367 				 */
368 				if (mpssas_add_device(sc,
369 				    le16toh(element->PhysDiskDevHandle), 0)){
370 					printf("%s: failed to add device with "
371 					    "handle 0x%x\n", __func__,
372 					    le16toh(element->PhysDiskDevHandle));
373 					mpssas_prepare_remove(sassc, le16toh(element->
374 					    PhysDiskDevHandle));
375 				}
376 				break;
377 			}
378 		}
379 		/*
380 		 * refcount was incremented for this event in
381 		 * mpssas_evt_handler.  Decrement it here because the event has
382 		 * been processed.
383 		 */
384 		mpssas_startup_decrement(sassc);
385 		break;
386 	}
387 	case MPI2_EVENT_IR_VOLUME:
388 	{
389 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
390 
391 		/*
392 		 * Informational only.
393 		 */
394 		mps_dprint(sc, MPS_EVENT, "Received IR Volume event:\n");
395 		switch (event_data->ReasonCode) {
396 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
397   			mps_dprint(sc, MPS_EVENT, "   Volume Settings "
398   			    "changed from 0x%x to 0x%x for Volome with "
399  			    "handle 0x%x", le32toh(event_data->PreviousValue),
400  			    le32toh(event_data->NewValue),
401  			    le16toh(event_data->VolDevHandle));
402 			break;
403 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
404   			mps_dprint(sc, MPS_EVENT, "   Volume Status "
405   			    "changed from 0x%x to 0x%x for Volome with "
406  			    "handle 0x%x", le32toh(event_data->PreviousValue),
407  			    le32toh(event_data->NewValue),
408  			    le16toh(event_data->VolDevHandle));
409 			break;
410 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
411   			mps_dprint(sc, MPS_EVENT, "   Volume State "
412   			    "changed from 0x%x to 0x%x for Volome with "
413  			    "handle 0x%x", le32toh(event_data->PreviousValue),
414  			    le32toh(event_data->NewValue),
415  			    le16toh(event_data->VolDevHandle));
416 				u32 state;
417 				struct mpssas_target *targ;
418 				state = le32toh(event_data->NewValue);
419 				switch (state) {
420 				case MPI2_RAID_VOL_STATE_MISSING:
421 				case MPI2_RAID_VOL_STATE_FAILED:
422 					mpssas_prepare_volume_remove(sassc, event_data->
423 							VolDevHandle);
424 					break;
425 
426 				case MPI2_RAID_VOL_STATE_ONLINE:
427 				case MPI2_RAID_VOL_STATE_DEGRADED:
428 				case MPI2_RAID_VOL_STATE_OPTIMAL:
429 					targ = mpssas_find_target_by_handle(sassc, 0, event_data->VolDevHandle);
430 					if (targ) {
431 						printf("%s %d: Volume handle 0x%x is already added \n",
432 							       	__func__, __LINE__ , event_data->VolDevHandle);
433 						break;
434 					}
435 					if (mpssas_volume_add(sc, le16toh(event_data->VolDevHandle))) {
436 						printf("%s: failed to add RAID "
437 							"volume with handle 0x%x\n",
438 							__func__, le16toh(event_data->
439 							VolDevHandle));
440 					}
441 					break;
442 				default:
443 					break;
444 				}
445 			break;
446 		default:
447 			break;
448 		}
449 		break;
450 	}
451 	case MPI2_EVENT_IR_PHYSICAL_DISK:
452 	{
453 		Mpi2EventDataIrPhysicalDisk_t *event_data =
454 		    fw_event->event_data;
455 		struct mpssas_target *targ;
456 
457 		/*
458 		 * Informational only.
459 		 */
460 		mps_dprint(sc, MPS_EVENT, "Received IR Phys Disk event:\n");
461 		switch (event_data->ReasonCode) {
462 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
463   			mps_dprint(sc, MPS_EVENT, "   Phys Disk Settings "
464   			    "changed from 0x%x to 0x%x for Phys Disk Number "
465   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
466  			    "%d", le32toh(event_data->PreviousValue),
467  			    le32toh(event_data->NewValue),
468  				event_data->PhysDiskNum,
469  			    le16toh(event_data->PhysDiskDevHandle),
470  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
471 			break;
472 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
473   			mps_dprint(sc, MPS_EVENT, "   Phys Disk Status changed "
474   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
475   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
476  				le32toh(event_data->PreviousValue),
477  			    le32toh(event_data->NewValue), event_data->PhysDiskNum,
478  			    le16toh(event_data->PhysDiskDevHandle),
479  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
480 			break;
481 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
482   			mps_dprint(sc, MPS_EVENT, "   Phys Disk State changed "
483   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
484   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
485  				le32toh(event_data->PreviousValue),
486  			    le32toh(event_data->NewValue), event_data->PhysDiskNum,
487  			    le16toh(event_data->PhysDiskDevHandle),
488  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
489 			switch (event_data->NewValue) {
490 				case MPI2_RAID_PD_STATE_ONLINE:
491 				case MPI2_RAID_PD_STATE_DEGRADED:
492 				case MPI2_RAID_PD_STATE_REBUILDING:
493 				case MPI2_RAID_PD_STATE_OPTIMAL:
494 				case MPI2_RAID_PD_STATE_HOT_SPARE:
495 					targ = mpssas_find_target_by_handle(sassc, 0,
496 							event_data->PhysDiskDevHandle);
497 					if (targ) {
498 						if(!sc->WD_available) {
499 							targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
500 							printf("%s %d: Found Target for handle 0x%x.  \n",
501 							__func__, __LINE__ , event_data->PhysDiskDevHandle);
502 						} else if ((sc->WD_available &&
503 							(sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) ||
504         						(sc->WD_valid_config && (sc->WD_hide_expose ==
505         						MPS_WD_HIDE_IF_VOLUME))) {
506 							targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
507 							printf("%s %d: WD: Found Target for handle 0x%x.  \n",
508 							__func__, __LINE__ , event_data->PhysDiskDevHandle);
509 						}
510  					}
511 				break;
512 				case MPI2_RAID_PD_STATE_OFFLINE:
513 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
514 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
515 				default:
516 					targ = mpssas_find_target_by_handle(sassc, 0,
517 							event_data->PhysDiskDevHandle);
518 					if (targ) {
519 						targ->flags |= ~MPS_TARGET_FLAGS_RAID_COMPONENT;
520 						printf("%s %d: Found Target for handle 0x%x.  \n",
521 						__func__, __LINE__ , event_data->PhysDiskDevHandle);
522 					}
523 				break;
524 			}
525 		default:
526 			break;
527 		}
528 		break;
529 	}
530 	case MPI2_EVENT_IR_OPERATION_STATUS:
531 	{
532 		Mpi2EventDataIrOperationStatus_t *event_data =
533 		    fw_event->event_data;
534 
535 		/*
536 		 * Informational only.
537 		 */
538 		mps_dprint(sc, MPS_EVENT, "Received IR Op Status event:\n");
539 		mps_dprint(sc, MPS_EVENT, "   RAID Operation of %d is %d "
540 		    "percent complete for Volume with handle 0x%x",
541 		    event_data->RAIDOperation, event_data->PercentComplete,
542 		    le16toh(event_data->VolDevHandle));
543 		break;
544 	}
545 	case MPI2_EVENT_LOG_ENTRY_ADDED:
546 	{
547 		pMpi2EventDataLogEntryAdded_t	logEntry;
548 		uint16_t			logQualifier;
549 		uint8_t				logCode;
550 
551 		logEntry = (pMpi2EventDataLogEntryAdded_t)fw_event->event_data;
552 		logQualifier = logEntry->LogEntryQualifier;
553 
554 		if (logQualifier == MPI2_WD_LOG_ENTRY) {
555 			logCode = logEntry->LogData[0];
556 
557 			switch (logCode) {
558 			case MPI2_WD_SSD_THROTTLING:
559 				printf("WarpDrive Warning: IO Throttling has "
560 				    "occurred in the WarpDrive subsystem. "
561 				    "Check WarpDrive documentation for "
562 				    "additional details\n");
563 				break;
564 			case MPI2_WD_DRIVE_LIFE_WARN:
565 				printf("WarpDrive Warning: Program/Erase "
566 				    "Cycles for the WarpDrive subsystem in "
567 				    "degraded range. Check WarpDrive "
568 				    "documentation for additional details\n");
569 				break;
570 			case MPI2_WD_DRIVE_LIFE_DEAD:
571 				printf("WarpDrive Fatal Error: There are no "
572 				    "Program/Erase Cycles for the WarpDrive "
573 				    "subsystem. The storage device will be in "
574 				    "read-only mode. Check WarpDrive "
575 				    "documentation for additional details\n");
576 				break;
577 			case MPI2_WD_RAIL_MON_FAIL:
578 				printf("WarpDrive Fatal Error: The Backup Rail "
579 				    "Monitor has failed on the WarpDrive "
580 				    "subsystem. Check WarpDrive documentation "
581 				    "for additional details\n");
582 				break;
583 			default:
584 				break;
585 			}
586 		}
587 		break;
588 	}
589 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
590 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
591 	default:
592 		mps_dprint(sc, MPS_TRACE,"Unhandled event 0x%0X\n",
593 		    fw_event->event);
594 		break;
595 
596 	}
597 	mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Event Free: [%x]\n",event_count,__func__, fw_event->event);
598 	mpssas_fw_event_free(sc, fw_event);
599 }
600 
601 void
602 mpssas_firmware_event_work(void *arg, int pending)
603 {
604 	struct mps_fw_event_work *fw_event;
605 	struct mps_softc *sc;
606 
607 	sc = (struct mps_softc *)arg;
608 	mps_lock(sc);
609 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
610 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
611 		mpssas_fw_work(sc, fw_event);
612 	}
613 	mps_unlock(sc);
614 }
615 
616 static int
617 mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate){
618 	char devstring[80];
619 	struct mpssas_softc *sassc;
620 	struct mpssas_target *targ;
621 	Mpi2ConfigReply_t mpi_reply;
622 	Mpi2SasDevicePage0_t config_page;
623 	uint64_t sas_address;
624 	uint64_t parent_sas_address = 0;
625 	u32 device_info, parent_devinfo = 0;
626 	unsigned int id;
627 	int ret = 1, error = 0, i;
628 	struct mpssas_lun *lun;
629 	u8 is_SATA_SSD = 0;
630 	struct mps_command *cm;
631 
632 	sassc = sc->sassc;
633 	mpssas_startup_increment(sassc);
634 	if (mps_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
635 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
636 		mps_dprint(sc, MPS_INFO|MPS_MAPPING|MPS_FAULT,
637 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
638 		    handle, mpi_reply.IOCStatus);
639 		error = ENXIO;
640 		goto out;
641 	}
642 
643 	device_info = le32toh(config_page.DeviceInfo);
644 
645 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
646 	 && (le16toh(config_page.ParentDevHandle) != 0)) {
647 		Mpi2ConfigReply_t tmp_mpi_reply;
648 		Mpi2SasDevicePage0_t parent_config_page;
649 
650 		if (mps_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
651 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
652 		    le16toh(config_page.ParentDevHandle)) != 0) {
653 			mps_dprint(sc, MPS_MAPPING|MPS_FAULT,
654 			    "Error reading parent SAS device %#x page0, "
655 			    "iocstatus= 0x%x\n",
656 			    le16toh(config_page.ParentDevHandle),
657 			    tmp_mpi_reply.IOCStatus);
658 		} else {
659 			parent_sas_address = parent_config_page.SASAddress.High;
660 			parent_sas_address = (parent_sas_address << 32) |
661 				parent_config_page.SASAddress.Low;
662 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
663 		}
664 	}
665 	/* TODO Check proper endianness */
666 	sas_address = config_page.SASAddress.High;
667 	sas_address = (sas_address << 32) | config_page.SASAddress.Low;
668         mps_dprint(sc, MPS_MAPPING, "Handle 0x%04x SAS Address from SAS device "
669             "page0 = %jx\n", handle, sas_address);
670 
671 	/*
672 	 * Always get SATA Identify information because this is used to
673 	 * determine if Start/Stop Unit should be sent to the drive when the
674 	 * system is shutdown.
675 	 */
676 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
677 		ret = mpssas_get_sas_address_for_sata_disk(sc, &sas_address,
678 		    handle, device_info, &is_SATA_SSD);
679 		if (ret) {
680 			mps_dprint(sc, MPS_MAPPING|MPS_ERROR,
681 			    "%s: failed to get disk type (SSD or HDD) for SATA "
682 			    "device with handle 0x%04x\n",
683 			    __func__, handle);
684 		} else {
685 			mps_dprint(sc, MPS_MAPPING, "Handle 0x%04x SAS Address "
686 			    "from SATA device = %jx\n", handle, sas_address);
687 		}
688 	}
689 
690 	/*
691 	 * use_phynum:
692 	 *  1 - use the PhyNum field as a fallback to the mapping logic
693 	 *  0 - never use the PhyNum field
694 	 * -1 - only use the PhyNum field
695 	 *
696 	 * Note that using the Phy number to map a device can cause device adds
697 	 * to fail if multiple enclosures/expanders are in the topology. For
698 	 * example, if two devices are in the same slot number in two different
699 	 * enclosures within the topology, only one of those devices will be
700 	 * added. PhyNum mapping should not be used if multiple enclosures are
701 	 * in the topology.
702 	 */
703 	id = MPS_MAP_BAD_ID;
704 	if (sc->use_phynum != -1)
705 		id = mps_mapping_get_tid(sc, sas_address, handle);
706 	if (id == MPS_MAP_BAD_ID) {
707 		if ((sc->use_phynum == 0)
708 		 || ((id = config_page.PhyNum) > sassc->maxtargets)) {
709 			mps_dprint(sc, MPS_INFO, "failure at %s:%d/%s()! "
710 			    "Could not get ID for device with handle 0x%04x\n",
711 			    __FILE__, __LINE__, __func__, handle);
712 			error = ENXIO;
713 			goto out;
714 		}
715 	}
716 	mps_dprint(sc, MPS_MAPPING, "%s: Target ID for added device is %d.\n",
717 	    __func__, id);
718 
719 	/*
720 	 * Only do the ID check and reuse check if the target is not from a
721 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
722 	 * when a volume is deleted because the mapping entry for the PD will
723 	 * still be in the mapping table. The ID check should not be done here
724 	 * either since this PD is already being used.
725 	 */
726 	targ = &sassc->targets[id];
727 	if (!(targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT)) {
728 		if (mpssas_check_id(sassc, id) != 0) {
729 			mps_dprint(sc, MPS_MAPPING|MPS_INFO,
730 			    "Excluding target id %d\n", id);
731 			error = ENXIO;
732 			goto out;
733 		}
734 
735 		if (targ->handle != 0x0) {
736 			mps_dprint(sc, MPS_MAPPING, "Attempting to reuse "
737 			    "target id %d handle 0x%04x\n", id, targ->handle);
738 			error = ENXIO;
739 			goto out;
740 		}
741 	}
742 
743 	targ->devinfo = device_info;
744 	targ->devname = le32toh(config_page.DeviceName.High);
745 	targ->devname = (targ->devname << 32) |
746 	    le32toh(config_page.DeviceName.Low);
747 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
748 	targ->encl_slot = le16toh(config_page.Slot);
749 	targ->handle = handle;
750 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
751 	targ->sasaddr = mps_to_u64(&config_page.SASAddress);
752 	targ->parent_sasaddr = le64toh(parent_sas_address);
753 	targ->parent_devinfo = parent_devinfo;
754 	targ->tid = id;
755 	targ->linkrate = (linkrate>>4);
756 	targ->flags = 0;
757 	if (is_SATA_SSD) {
758 		targ->flags = MPS_TARGET_IS_SATA_SSD;
759 	}
760 	TAILQ_INIT(&targ->commands);
761 	TAILQ_INIT(&targ->timedout_commands);
762 	while(!SLIST_EMPTY(&targ->luns)) {
763 		lun = SLIST_FIRST(&targ->luns);
764 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
765 		free(lun, M_MPT2);
766 	}
767 	SLIST_INIT(&targ->luns);
768 
769 	mps_describe_devinfo(targ->devinfo, devstring, 80);
770 	mps_dprint(sc, MPS_MAPPING, "Found device <%s> <%s> <0x%04x> <%d/%d>\n",
771 	    devstring, mps_describe_table(mps_linkrate_names, targ->linkrate),
772 	    targ->handle, targ->encl_handle, targ->encl_slot);
773 
774 #if __FreeBSD_version < 1000039
775 	if ((sassc->flags & MPSSAS_IN_STARTUP) == 0)
776 #endif
777 		mpssas_rescan_target(sc, targ);
778 	mps_dprint(sc, MPS_MAPPING, "Target id 0x%x added\n", targ->tid);
779 
780 	/*
781 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
782 	 * If so, send a Target Reset TM to the target that was just created.
783 	 * An Abort Task TM should be used instead of a Target Reset, but that
784 	 * would be much more difficult because targets have not been fully
785 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
786 	 * target instead of the LUN.
787 	 */
788 	for (i = 1; i < sc->num_reqs; i++) {
789 		cm = &sc->commands[i];
790 		if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) {
791 			targ->timeouts++;
792 			cm->cm_state = MPS_CM_STATE_TIMEDOUT;
793 
794 			if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
795 				mps_dprint(sc, MPS_INFO, "%s: sending Target "
796 				    "Reset for stuck SATA identify command "
797 				    "(cm = %p)\n", __func__, cm);
798 				targ->tm->cm_targ = targ;
799 				mpssas_send_reset(sc, targ->tm,
800 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
801 			} else {
802 				mps_dprint(sc, MPS_ERROR, "Failed to allocate "
803 				    "tm for Target Reset after SATA ID command "
804 				    "timed out (cm %p)\n", cm);
805 			}
806 			/*
807 			 * No need to check for more since the target is
808 			 * already being reset.
809 			 */
810 			break;
811 		}
812 	}
813 out:
814 	/*
815 	 * Free the commands that may not have been freed from the SATA ID call
816 	 */
817 	for (i = 1; i < sc->num_reqs; i++) {
818 		cm = &sc->commands[i];
819 		if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) {
820 			mps_free_command(sc, cm);
821 		}
822 	}
823 	mpssas_startup_decrement(sassc);
824 	return (error);
825 }
826 
827 int
828 mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
829     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
830 {
831 	Mpi2SataPassthroughReply_t mpi_reply;
832 	int i, rc, try_count;
833 	u32 *bufferptr;
834 	union _sata_sas_address hash_address;
835 	struct _ata_identify_device_data ata_identify;
836 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
837 	u32 ioc_status;
838 	u8 sas_status;
839 
840 	memset(&ata_identify, 0, sizeof(ata_identify));
841 	try_count = 0;
842 	do {
843 		rc = mpssas_get_sata_identify(sc, handle, &mpi_reply,
844 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
845 		try_count++;
846 		ioc_status = le16toh(mpi_reply.IOCStatus)
847 		    & MPI2_IOCSTATUS_MASK;
848 		sas_status = mpi_reply.SASStatus;
849 		switch (ioc_status) {
850 		case MPI2_IOCSTATUS_SUCCESS:
851 			break;
852 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
853 			/* No sense sleeping.  this error won't get better */
854 			break;
855 		default:
856 			if (sc->spinup_wait_time > 0) {
857 				mps_dprint(sc, MPS_INFO, "Sleeping %d seconds "
858 				    "after SATA ID error to wait for spinup\n",
859 				    sc->spinup_wait_time);
860 				msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
861 				    "mpsid", sc->spinup_wait_time * hz);
862 			}
863 		}
864 	} while (((rc && (rc != EWOULDBLOCK)) ||
865 	    	 (ioc_status &&
866 		  (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
867 	       || sas_status) && (try_count < 5));
868 
869 	if (rc == 0 && !ioc_status && !sas_status) {
870 		mps_dprint(sc, MPS_MAPPING, "%s: got SATA identify "
871 		    "successfully for handle = 0x%x with try_count = %d\n",
872 		    __func__, handle, try_count);
873 	} else {
874 		mps_dprint(sc, MPS_MAPPING, "%s: handle = 0x%x failed\n",
875 		    __func__, handle);
876 		return -1;
877 	}
878 	/* Copy & byteswap the 40 byte model number to a buffer */
879 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
880 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
881 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
882 	}
883 	/* Copy & byteswap the 20 byte serial number to a buffer */
884 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
885 		buffer[MPT2SAS_MN_LEN + i] =
886 		    ((u8 *)ata_identify.serial_number)[i + 1];
887 		buffer[MPT2SAS_MN_LEN + i + 1] =
888 		    ((u8 *)ata_identify.serial_number)[i];
889 	}
890 	bufferptr = (u32 *)buffer;
891 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
892 	 * so loop through the first 56 bytes (7*8),
893 	 * and then add in the last dword.
894 	 */
895 	hash_address.word.low  = 0;
896 	hash_address.word.high = 0;
897 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
898 		hash_address.word.low += *bufferptr;
899 		bufferptr++;
900 		hash_address.word.high += *bufferptr;
901 		bufferptr++;
902 	}
903 	/* Add the last dword */
904 	hash_address.word.low += *bufferptr;
905 	/* Make sure the hash doesn't start with 5, because it could clash
906 	 * with a SAS address. Change 5 to a D.
907 	 */
908 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
909 		hash_address.word.high |= 0x00000080;
910 	*sas_address = (u64)hash_address.wwid[0] << 56 |
911 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
912 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
913 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
914 	    (u64)hash_address.wwid[7];
915 	if (ata_identify.rotational_speed == 1) {
916 		*is_SATA_SSD = 1;
917 	}
918 
919 	return 0;
920 }
921 
922 static int
923 mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
924     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
925 {
926 	Mpi2SataPassthroughRequest_t *mpi_request;
927 	Mpi2SataPassthroughReply_t *reply = NULL;
928 	struct mps_command *cm;
929 	char *buffer;
930 	int error = 0;
931 
932 	buffer = malloc( sz, M_MPT2, M_NOWAIT | M_ZERO);
933 	if (!buffer)
934 		return ENOMEM;
935 
936 	if ((cm = mps_alloc_command(sc)) == NULL) {
937 		free(buffer, M_MPT2);
938 		return (EBUSY);
939 	}
940 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
941 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
942 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
943 	mpi_request->VF_ID = 0;
944 	mpi_request->DevHandle = htole16(handle);
945 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
946 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
947 	mpi_request->DataLength = htole32(sz);
948 	mpi_request->CommandFIS[0] = 0x27;
949 	mpi_request->CommandFIS[1] = 0x80;
950 	mpi_request->CommandFIS[2] =  (devinfo &
951 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
952 	cm->cm_sge = &mpi_request->SGL;
953 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
954 	cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
955 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
956 	cm->cm_data = buffer;
957 	cm->cm_length = htole32(sz);
958 
959 	/*
960 	 * Start a timeout counter specifically for the SATA ID command. This
961 	 * is used to fix a problem where the FW does not send a reply sometimes
962 	 * when a bad disk is in the topology. So, this is used to timeout the
963 	 * command so that processing can continue normally.
964 	 */
965 	mps_dprint(sc, MPS_XINFO, "%s start timeout counter for SATA ID "
966 	    "command\n", __func__);
967 	callout_reset(&cm->cm_callout, MPS_ATA_ID_TIMEOUT * hz,
968 	    mpssas_ata_id_timeout, cm);
969 	error = mps_wait_command(sc, &cm, 60, CAN_SLEEP);
970 	mps_dprint(sc, MPS_XINFO, "%s stop timeout counter for SATA ID "
971 	    "command\n", __func__);
972 	/* XXX KDM need to fix the case where this command is destroyed */
973 	callout_stop(&cm->cm_callout);
974 
975 	if (cm != NULL)
976 		reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
977 	if (error || (reply == NULL)) {
978 		/* FIXME */
979  		/*
980  		 * If the request returns an error then we need to do a diag
981  		 * reset
982  		 */
983  		mps_dprint(sc, MPS_INFO|MPS_FAULT|MPS_MAPPING,
984 		    "Request for SATA PASSTHROUGH page completed with error %d",
985 		    error);
986 		error = ENXIO;
987 		goto out;
988 	}
989 	bcopy(buffer, id_buffer, sz);
990 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
991 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
992 	    MPI2_IOCSTATUS_SUCCESS) {
993 		mps_dprint(sc, MPS_INFO|MPS_MAPPING|MPS_FAULT,
994 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
995 		    handle, reply->IOCStatus);
996 		error = ENXIO;
997 		goto out;
998 	}
999 out:
1000 	/*
1001 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
1002 	 * it.  The command will be freed after sending a target reset TM. If
1003 	 * the command did timeout, use EWOULDBLOCK.
1004 	 */
1005 	if ((cm != NULL)
1006 	 && (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) == 0)
1007 		mps_free_command(sc, cm);
1008 	else if (error == 0)
1009 		error = EWOULDBLOCK;
1010 	free(buffer, M_MPT2);
1011 	return (error);
1012 }
1013 
1014 static void
1015 mpssas_ata_id_timeout(void *data)
1016 {
1017 	struct mps_softc *sc;
1018 	struct mps_command *cm;
1019 
1020 	cm = (struct mps_command *)data;
1021 	sc = cm->cm_sc;
1022 	mtx_assert(&sc->mps_mtx, MA_OWNED);
1023 
1024 	mps_dprint(sc, MPS_INFO, "%s checking ATA ID command %p sc %p\n",
1025 	    __func__, cm, sc);
1026 	if ((callout_pending(&cm->cm_callout)) ||
1027 	    (!callout_active(&cm->cm_callout))) {
1028 		mps_dprint(sc, MPS_INFO, "%s ATA ID command almost timed out\n",
1029 		    __func__);
1030 		return;
1031 	}
1032 	callout_deactivate(&cm->cm_callout);
1033 
1034 	/*
1035 	 * Run the interrupt handler to make sure it's not pending.  This
1036 	 * isn't perfect because the command could have already completed
1037 	 * and been re-used, though this is unlikely.
1038 	 */
1039 	mps_intr_locked(sc);
1040 	if (cm->cm_state == MPS_CM_STATE_FREE) {
1041 		mps_dprint(sc, MPS_INFO, "%s ATA ID command almost timed out\n",
1042 		    __func__);
1043 		return;
1044 	}
1045 
1046 	mps_dprint(sc, MPS_INFO, "ATA ID command timeout cm %p\n", cm);
1047 
1048 	/*
1049 	 * Send wakeup() to the sleeping thread that issued this ATA ID command.
1050 	 * wakeup() will cause msleep to return a 0 (not EWOULDBLOCK), and this
1051 	 * will keep reinit() from being called. This way, an Abort Task TM can
1052 	 * be issued so that the timed out command can be cleared.  The Abort
1053 	 * Task cannot be sent from here because the driver has not completed
1054 	 * setting up targets.  Instead, the command is flagged so that special
1055 	 * handling will be used to send the abort.
1056 	 */
1057 	cm->cm_flags |= MPS_CM_FLAGS_SATA_ID_TIMEOUT;
1058 	wakeup(cm);
1059 }
1060 
1061 static int
1062 mpssas_volume_add(struct mps_softc *sc, u16 handle)
1063 {
1064 	struct mpssas_softc *sassc;
1065 	struct mpssas_target *targ;
1066 	u64 wwid;
1067 	unsigned int id;
1068 	int error = 0;
1069 	struct mpssas_lun *lun;
1070 
1071 	sassc = sc->sassc;
1072 	mpssas_startup_increment(sassc);
1073 	/* wwid is endian safe */
1074 	mps_config_get_volume_wwid(sc, handle, &wwid);
1075 	if (!wwid) {
1076 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1077 		    __func__);
1078 		error = ENXIO;
1079 		goto out;
1080 	}
1081 
1082 	id = mps_mapping_get_raid_tid(sc, wwid, handle);
1083 	if (id == MPS_MAP_BAD_ID) {
1084 		printf("%s: could not get ID for volume with handle 0x%04x and "
1085 		    "WWID 0x%016llx\n", __func__, handle,
1086 		    (unsigned long long)wwid);
1087 		error = ENXIO;
1088 		goto out;
1089 	}
1090 
1091 	targ = &sassc->targets[id];
1092 	targ->tid = id;
1093 	targ->handle = handle;
1094 	targ->devname = wwid;
1095 	TAILQ_INIT(&targ->commands);
1096 	TAILQ_INIT(&targ->timedout_commands);
1097 	while(!SLIST_EMPTY(&targ->luns)) {
1098 		lun = SLIST_FIRST(&targ->luns);
1099 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1100 		free(lun, M_MPT2);
1101 	}
1102 	SLIST_INIT(&targ->luns);
1103 #if __FreeBSD_version < 1000039
1104 	if ((sassc->flags & MPSSAS_IN_STARTUP) == 0)
1105 #endif
1106 		mpssas_rescan_target(sc, targ);
1107 	mps_dprint(sc, MPS_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1108 	    targ->tid, wwid);
1109 out:
1110 	mpssas_startup_decrement(sassc);
1111 	return (error);
1112 }
1113 
1114 /**
1115  * mpssas_SSU_to_SATA_devices
1116  * @sc: per adapter object
1117  *
1118  * Looks through the target list and issues a StartStopUnit SCSI command to each
1119  * SATA direct-access device.  This helps to ensure that data corruption is
1120  * avoided when the system is being shut down.  This must be called after the IR
1121  * System Shutdown RAID Action is sent if in IR mode.
1122  *
1123  * Return nothing.
1124  */
1125 static void
1126 mpssas_SSU_to_SATA_devices(struct mps_softc *sc, int howto)
1127 {
1128 	struct mpssas_softc *sassc = sc->sassc;
1129 	union ccb *ccb;
1130 	path_id_t pathid = cam_sim_path(sassc->sim);
1131 	target_id_t targetid;
1132 	struct mpssas_target *target;
1133 	char path_str[64];
1134 	int timeout;
1135 
1136 	/*
1137 	 * For each target, issue a StartStopUnit command to stop the device.
1138 	 */
1139 	sc->SSU_started = TRUE;
1140 	sc->SSU_refcount = 0;
1141 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1142 		target = &sassc->targets[targetid];
1143 		if (target->handle == 0x0) {
1144 			continue;
1145 		}
1146 
1147 		ccb = xpt_alloc_ccb_nowait();
1148 		if (ccb == NULL) {
1149 			mps_dprint(sc, MPS_FAULT, "Unable to alloc CCB to stop "
1150 			    "unit.\n");
1151 			return;
1152 		}
1153 
1154 		/*
1155 		 * The stop_at_shutdown flag will be set if this device is
1156 		 * a SATA direct-access end device.
1157 		 */
1158 		if (target->stop_at_shutdown) {
1159 			if (xpt_create_path(&ccb->ccb_h.path,
1160 			    xpt_periph, pathid, targetid,
1161 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1162 				mps_dprint(sc, MPS_FAULT, "Unable to create "
1163 				    "LUN path to stop unit.\n");
1164 				xpt_free_ccb(ccb);
1165 				return;
1166 			}
1167 			xpt_path_string(ccb->ccb_h.path, path_str,
1168 			    sizeof(path_str));
1169 
1170 			mps_dprint(sc, MPS_INFO, "Sending StopUnit: path %s "
1171 			    "handle %d\n", path_str, target->handle);
1172 
1173 			/*
1174 			 * Issue a START STOP UNIT command for the target.
1175 			 * Increment the SSU counter to be used to count the
1176 			 * number of required replies.
1177 			 */
1178 			mps_dprint(sc, MPS_INFO, "Incrementing SSU count\n");
1179 			sc->SSU_refcount++;
1180 			ccb->ccb_h.target_id =
1181 			    xpt_path_target_id(ccb->ccb_h.path);
1182 			ccb->ccb_h.ppriv_ptr1 = sassc;
1183 			scsi_start_stop(&ccb->csio,
1184 			    /*retries*/0,
1185 			    mpssas_stop_unit_done,
1186 			    MSG_SIMPLE_Q_TAG,
1187 			    /*start*/FALSE,
1188 			    /*load/eject*/0,
1189 			    /*immediate*/FALSE,
1190 			    MPS_SENSE_LEN,
1191 			    /*timeout*/10000);
1192 			xpt_action(ccb);
1193 		}
1194 	}
1195 
1196 	/*
1197 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1198 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1199 	 */
1200 	timeout = 600;
1201 	if (howto & RB_NOSYNC)
1202 		timeout = 100;
1203 
1204 	/*
1205 	 * Wait until all of the SSU commands have completed or timeout has
1206 	 * expired.  Pause for 100ms each time through.  If any command
1207 	 * times out, the target will be reset in the SCSI command timeout
1208 	 * routine.
1209 	 */
1210 	while (sc->SSU_refcount > 0) {
1211 		pause("mpswait", hz/10);
1212 
1213 		if (--timeout == 0) {
1214 			mps_dprint(sc, MPS_FAULT, "Time has expired waiting "
1215 			    "for SSU commands to complete.\n");
1216 			break;
1217 		}
1218 	}
1219 }
1220 
1221 static void
1222 mpssas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1223 {
1224 	struct mpssas_softc *sassc;
1225 	char path_str[64];
1226 
1227 	if (done_ccb == NULL)
1228 		return;
1229 
1230 	sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1231 
1232 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1233 	mps_dprint(sassc->sc, MPS_INFO, "Completing stop unit for %s\n",
1234 	    path_str);
1235 
1236 	/*
1237 	 * Nothing more to do except free the CCB and path.  If the command
1238 	 * timed out, an abort reset, then target reset will be issued during
1239 	 * the SCSI Command process.
1240 	 */
1241 	xpt_free_path(done_ccb->ccb_h.path);
1242 	xpt_free_ccb(done_ccb);
1243 }
1244 
1245 /**
1246  * mpssas_ir_shutdown - IR shutdown notification
1247  * @sc: per adapter object
1248  *
1249  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1250  * the host system is shutting down.
1251  *
1252  * Return nothing.
1253  */
1254 void
1255 mpssas_ir_shutdown(struct mps_softc *sc, int howto)
1256 {
1257 	u16 volume_mapping_flags;
1258 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1259 	struct dev_mapping_table *mt_entry;
1260 	u32 start_idx, end_idx;
1261 	unsigned int id, found_volume = 0;
1262 	struct mps_command *cm;
1263 	Mpi2RaidActionRequest_t	*action;
1264 	target_id_t targetid;
1265 	struct mpssas_target *target;
1266 
1267 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1268 
1269 	/* is IR firmware build loaded? */
1270 	if (!sc->ir_firmware)
1271 		goto out;
1272 
1273 	/* are there any volumes?  Look at IR target IDs. */
1274 	// TODO-later, this should be looked up in the RAID config structure
1275 	// when it is implemented.
1276 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1277 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1278 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1279 		start_idx = 0;
1280 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1281 			start_idx = 1;
1282 	} else
1283 		start_idx = sc->max_devices - sc->max_volumes;
1284 	end_idx = start_idx + sc->max_volumes - 1;
1285 
1286 	for (id = start_idx; id < end_idx; id++) {
1287 		mt_entry = &sc->mapping_table[id];
1288 		if ((mt_entry->physical_id != 0) &&
1289 		    (mt_entry->missing_count == 0)) {
1290 			found_volume = 1;
1291 			break;
1292 		}
1293 	}
1294 
1295 	if (!found_volume)
1296 		goto out;
1297 
1298 	if ((cm = mps_alloc_command(sc)) == NULL) {
1299 		printf("%s: command alloc failed\n", __func__);
1300 		goto out;
1301 	}
1302 
1303 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1304 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1305 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1306 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1307 	mps_lock(sc);
1308 	mps_wait_command(sc, &cm, 5, CAN_SLEEP);
1309 	mps_unlock(sc);
1310 
1311 	/*
1312 	 * Don't check for reply, just leave.
1313 	 */
1314 	if (cm)
1315 		mps_free_command(sc, cm);
1316 
1317 out:
1318 	/*
1319 	 * All of the targets must have the correct value set for
1320 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1321 	 *
1322 	 * The possible values for the 'enable_ssu' variable are:
1323 	 * 0: disable to SSD and HDD
1324 	 * 1: disable only to HDD (default)
1325 	 * 2: disable only to SSD
1326 	 * 3: enable to SSD and HDD
1327 	 * anything else will default to 1.
1328 	 */
1329 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1330 		target = &sc->sassc->targets[targetid];
1331 		if (target->handle == 0x0) {
1332 			continue;
1333 		}
1334 
1335 		if (target->supports_SSU) {
1336 			switch (sc->enable_ssu) {
1337 			case MPS_SSU_DISABLE_SSD_DISABLE_HDD:
1338 				target->stop_at_shutdown = FALSE;
1339 				break;
1340 			case MPS_SSU_DISABLE_SSD_ENABLE_HDD:
1341 				target->stop_at_shutdown = TRUE;
1342 				if (target->flags & MPS_TARGET_IS_SATA_SSD) {
1343 					target->stop_at_shutdown = FALSE;
1344 				}
1345 				break;
1346 			case MPS_SSU_ENABLE_SSD_ENABLE_HDD:
1347 				target->stop_at_shutdown = TRUE;
1348 				break;
1349 			case MPS_SSU_ENABLE_SSD_DISABLE_HDD:
1350 			default:
1351 				target->stop_at_shutdown = TRUE;
1352 				if ((target->flags &
1353 				    MPS_TARGET_IS_SATA_SSD) == 0) {
1354 					target->stop_at_shutdown = FALSE;
1355 				}
1356 				break;
1357 			}
1358 		}
1359 	}
1360 	mpssas_SSU_to_SATA_devices(sc, howto);
1361 }
1362