xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 055e2653d4ae358dd8b639ddf2c40fd9358e9ff8)
1991554f2SKenneth D. Merry /*-
2a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
37a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
4991554f2SKenneth D. Merry  * All rights reserved.
5991554f2SKenneth D. Merry  *
6991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
7991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
8991554f2SKenneth D. Merry  * are met:
9991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
10991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
11991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
12991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
13991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
14991554f2SKenneth D. Merry  *
15991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25991554f2SKenneth D. Merry  * SUCH DAMAGE.
26991554f2SKenneth D. Merry  *
27a2c14879SStephen McConnell  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
28991554f2SKenneth D. Merry  */
29991554f2SKenneth D. Merry 
30991554f2SKenneth D. Merry #include <sys/cdefs.h>
31991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
32991554f2SKenneth D. Merry 
337a2a6a1aSStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
34991554f2SKenneth D. Merry 
35991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
36991554f2SKenneth D. Merry #include <sys/types.h>
37991554f2SKenneth D. Merry #include <sys/param.h>
38991554f2SKenneth D. Merry #include <sys/systm.h>
39991554f2SKenneth D. Merry #include <sys/kernel.h>
40991554f2SKenneth D. Merry #include <sys/selinfo.h>
41991554f2SKenneth D. Merry #include <sys/module.h>
42991554f2SKenneth D. Merry #include <sys/bus.h>
43991554f2SKenneth D. Merry #include <sys/conf.h>
44991554f2SKenneth D. Merry #include <sys/bio.h>
45991554f2SKenneth D. Merry #include <sys/malloc.h>
46991554f2SKenneth D. Merry #include <sys/uio.h>
47991554f2SKenneth D. Merry #include <sys/sysctl.h>
48991554f2SKenneth D. Merry #include <sys/endian.h>
49991554f2SKenneth D. Merry #include <sys/queue.h>
50991554f2SKenneth D. Merry #include <sys/kthread.h>
51991554f2SKenneth D. Merry #include <sys/taskqueue.h>
52991554f2SKenneth D. Merry #include <sys/sbuf.h>
53991554f2SKenneth D. Merry 
54991554f2SKenneth D. Merry #include <machine/bus.h>
55991554f2SKenneth D. Merry #include <machine/resource.h>
56991554f2SKenneth D. Merry #include <sys/rman.h>
57991554f2SKenneth D. Merry 
58991554f2SKenneth D. Merry #include <machine/stdarg.h>
59991554f2SKenneth D. Merry 
60991554f2SKenneth D. Merry #include <cam/cam.h>
61991554f2SKenneth D. Merry #include <cam/cam_ccb.h>
62991554f2SKenneth D. Merry #include <cam/cam_debug.h>
63991554f2SKenneth D. Merry #include <cam/cam_sim.h>
64991554f2SKenneth D. Merry #include <cam/cam_xpt_sim.h>
65991554f2SKenneth D. Merry #include <cam/cam_xpt_periph.h>
66991554f2SKenneth D. Merry #include <cam/cam_periph.h>
67991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
68991554f2SKenneth D. Merry #include <cam/scsi/scsi_message.h>
69991554f2SKenneth D. Merry 
70991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
71991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
72991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7467feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
75991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
77991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_raid.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
82991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
83991554f2SKenneth D. Merry 
84991554f2SKenneth D. Merry /* For Hashed SAS Address creation for SATA Drives */
85991554f2SKenneth D. Merry #define MPT2SAS_SN_LEN 20
86991554f2SKenneth D. Merry #define MPT2SAS_MN_LEN 40
87991554f2SKenneth D. Merry 
88991554f2SKenneth D. Merry struct mpr_fw_event_work {
89991554f2SKenneth D. Merry 	u16			event;
90991554f2SKenneth D. Merry 	void			*event_data;
91991554f2SKenneth D. Merry 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
92991554f2SKenneth D. Merry };
93991554f2SKenneth D. Merry 
94991554f2SKenneth D. Merry union _sata_sas_address {
95991554f2SKenneth D. Merry 	u8 wwid[8];
96991554f2SKenneth D. Merry 	struct {
97991554f2SKenneth D. Merry 		u32 high;
98991554f2SKenneth D. Merry 		u32 low;
99991554f2SKenneth D. Merry 	} word;
100991554f2SKenneth D. Merry };
101991554f2SKenneth D. Merry 
102991554f2SKenneth D. Merry /*
103991554f2SKenneth D. Merry  * define the IDENTIFY DEVICE structure
104991554f2SKenneth D. Merry  */
105991554f2SKenneth D. Merry struct _ata_identify_device_data {
106991554f2SKenneth D. Merry 	u16 reserved1[10];	/* 0-9 */
107991554f2SKenneth D. Merry 	u16 serial_number[10];	/* 10-19 */
108991554f2SKenneth D. Merry 	u16 reserved2[7];	/* 20-26 */
109991554f2SKenneth D. Merry 	u16 model_number[20];	/* 27-46*/
110a2c14879SStephen McConnell 	u16 reserved3[170];	/* 47-216 */
111a2c14879SStephen McConnell 	u16 rotational_speed;	/* 217 */
112a2c14879SStephen McConnell 	u16 reserved4[38];	/* 218-255 */
113991554f2SKenneth D. Merry };
114991554f2SKenneth D. Merry static u32 event_count;
115991554f2SKenneth D. Merry static void mprsas_fw_work(struct mpr_softc *sc,
116991554f2SKenneth D. Merry     struct mpr_fw_event_work *fw_event);
117991554f2SKenneth D. Merry static void mprsas_fw_event_free(struct mpr_softc *,
118991554f2SKenneth D. Merry     struct mpr_fw_event_work *);
119991554f2SKenneth D. Merry static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
12067feec50SStephen McConnell static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
12167feec50SStephen McConnell     u8 linkrate);
122991554f2SKenneth D. Merry static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
123991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
124991554f2SKenneth D. Merry     u32 devinfo);
125a2c14879SStephen McConnell static void mprsas_ata_id_timeout(void *data);
126991554f2SKenneth D. Merry int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
127a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
128991554f2SKenneth D. Merry static int mprsas_volume_add(struct mpr_softc *sc,
129991554f2SKenneth D. Merry     u16 handle);
130991554f2SKenneth D. Merry static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc);
131991554f2SKenneth D. Merry static void mprsas_stop_unit_done(struct cam_periph *periph,
132991554f2SKenneth D. Merry     union ccb *done_ccb);
133991554f2SKenneth D. Merry 
134991554f2SKenneth D. Merry void
135991554f2SKenneth D. Merry mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
136991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
137991554f2SKenneth D. Merry {
138991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
139991554f2SKenneth D. Merry 	u16 sz;
140991554f2SKenneth D. Merry 
141991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
142*055e2653SScott Long 	MPR_DPRINT_EVENT(sc, sas, event);
143991554f2SKenneth D. Merry 	mprsas_record_event(sc, event);
144991554f2SKenneth D. Merry 
145991554f2SKenneth D. Merry 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
146991554f2SKenneth D. Merry 	     M_ZERO|M_NOWAIT);
147991554f2SKenneth D. Merry 	if (!fw_event) {
148991554f2SKenneth D. Merry 		printf("%s: allocate failed for fw_event\n", __func__);
149991554f2SKenneth D. Merry 		return;
150991554f2SKenneth D. Merry 	}
151991554f2SKenneth D. Merry 	sz = le16toh(event->EventDataLength) * 4;
152991554f2SKenneth D. Merry 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
153991554f2SKenneth D. Merry 	if (!fw_event->event_data) {
154991554f2SKenneth D. Merry 		printf("%s: allocate failed for event_data\n", __func__);
155991554f2SKenneth D. Merry 		free(fw_event, M_MPR);
156991554f2SKenneth D. Merry 		return;
157991554f2SKenneth D. Merry 	}
158991554f2SKenneth D. Merry 
159991554f2SKenneth D. Merry 	bcopy(event->EventData, fw_event->event_data, sz);
160991554f2SKenneth D. Merry 	fw_event->event = event->Event;
161991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
16267feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
163991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
164991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
165991554f2SKenneth D. Merry 	    sc->track_mapping_events)
166991554f2SKenneth D. Merry 		sc->pending_map_events++;
167991554f2SKenneth D. Merry 
168991554f2SKenneth D. Merry 	/*
169991554f2SKenneth D. Merry 	 * When wait_for_port_enable flag is set, make sure that all the events
170991554f2SKenneth D. Merry 	 * are processed. Increment the startup_refcount and decrement it after
171991554f2SKenneth D. Merry 	 * events are processed.
172991554f2SKenneth D. Merry 	 */
173991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
17467feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
175991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
176991554f2SKenneth D. Merry 	    sc->wait_for_port_enable)
177991554f2SKenneth D. Merry 		mprsas_startup_increment(sc->sassc);
178991554f2SKenneth D. Merry 
179991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
180991554f2SKenneth D. Merry 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
181991554f2SKenneth D. Merry }
182991554f2SKenneth D. Merry 
183991554f2SKenneth D. Merry static void
184991554f2SKenneth D. Merry mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
185991554f2SKenneth D. Merry {
186991554f2SKenneth D. Merry 
187991554f2SKenneth D. Merry 	free(fw_event->event_data, M_MPR);
188991554f2SKenneth D. Merry 	free(fw_event, M_MPR);
189991554f2SKenneth D. Merry }
190991554f2SKenneth D. Merry 
191991554f2SKenneth D. Merry /**
192991554f2SKenneth D. Merry  * _mpr_fw_work - delayed task for processing firmware events
193991554f2SKenneth D. Merry  * @sc: per adapter object
194991554f2SKenneth D. Merry  * @fw_event: The fw_event_work object
195991554f2SKenneth D. Merry  * Context: user.
196991554f2SKenneth D. Merry  *
197991554f2SKenneth D. Merry  * Return nothing.
198991554f2SKenneth D. Merry  */
199991554f2SKenneth D. Merry static void
200991554f2SKenneth D. Merry mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
201991554f2SKenneth D. Merry {
202991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
203991554f2SKenneth D. Merry 	sassc = sc->sassc;
204991554f2SKenneth D. Merry 
205991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
206991554f2SKenneth D. Merry 	    event_count++, __func__, fw_event->event);
207991554f2SKenneth D. Merry 	switch (fw_event->event) {
208991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
209991554f2SKenneth D. Merry 	{
210991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
211991554f2SKenneth D. Merry 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
21267feec50SStephen McConnell 		uint8_t i;
213991554f2SKenneth D. Merry 
214991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
215991554f2SKenneth D. Merry 		    fw_event->event_data;
216991554f2SKenneth D. Merry 
217991554f2SKenneth D. Merry 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
218991554f2SKenneth D. Merry 
219991554f2SKenneth D. Merry 		for (i = 0; i < data->NumEntries; i++) {
220991554f2SKenneth D. Merry 			phy = &data->PHY[i];
221991554f2SKenneth D. Merry 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
222991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
223991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
224991554f2SKenneth D. Merry 				    le16toh(phy->AttachedDevHandle),
225991554f2SKenneth D. Merry 				    phy->LinkRate)) {
226327f2e6cSStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: "
227327f2e6cSStephen McConnell 					    "failed to add device with handle "
228327f2e6cSStephen McConnell 					    "0x%x\n", __func__,
229991554f2SKenneth D. Merry 					    le16toh(phy->AttachedDevHandle));
230991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc, le16toh(
231991554f2SKenneth D. Merry 					    phy->AttachedDevHandle));
232991554f2SKenneth D. Merry 				}
233991554f2SKenneth D. Merry 				break;
234991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
235991554f2SKenneth D. Merry 				mprsas_prepare_remove(sassc, le16toh(
236991554f2SKenneth D. Merry 				    phy->AttachedDevHandle));
237991554f2SKenneth D. Merry 				break;
238991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
239991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
240991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
241991554f2SKenneth D. Merry 			default:
242991554f2SKenneth D. Merry 				break;
243991554f2SKenneth D. Merry 			}
244991554f2SKenneth D. Merry 		}
245991554f2SKenneth D. Merry 		/*
246991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
247991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
248991554f2SKenneth D. Merry 		 * been processed.
249991554f2SKenneth D. Merry 		 */
250991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
251991554f2SKenneth D. Merry 		break;
252991554f2SKenneth D. Merry 	}
253991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DISCOVERY:
254991554f2SKenneth D. Merry 	{
255991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
256991554f2SKenneth D. Merry 
257991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
258991554f2SKenneth D. Merry 
259991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
2607a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
261991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
262991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
263991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
264991554f2SKenneth D. Merry 			mprsas_discovery_end(sassc);
265991554f2SKenneth D. Merry 		}
266991554f2SKenneth D. Merry 		break;
267991554f2SKenneth D. Merry 	}
268991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
269991554f2SKenneth D. Merry 	{
270991554f2SKenneth D. Merry 		Mpi2EventDataSasEnclDevStatusChange_t *data;
271991554f2SKenneth D. Merry 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
272991554f2SKenneth D. Merry 		    fw_event->event_data;
273991554f2SKenneth D. Merry 		mpr_mapping_enclosure_dev_status_change_event(sc,
274991554f2SKenneth D. Merry 		    fw_event->event_data);
275991554f2SKenneth D. Merry 		break;
276991554f2SKenneth D. Merry 	}
277991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
278991554f2SKenneth D. Merry 	{
279991554f2SKenneth D. Merry 		Mpi2EventIrConfigElement_t *element;
280991554f2SKenneth D. Merry 		int i;
281991554f2SKenneth D. Merry 		u8 foreign_config, reason;
282991554f2SKenneth D. Merry 		u16 elementType;
283991554f2SKenneth D. Merry 		Mpi2EventDataIrConfigChangeList_t *event_data;
284991554f2SKenneth D. Merry 		struct mprsas_target *targ;
285991554f2SKenneth D. Merry 		unsigned int id;
286991554f2SKenneth D. Merry 
287991554f2SKenneth D. Merry 		event_data = fw_event->event_data;
288991554f2SKenneth D. Merry 		foreign_config = (le32toh(event_data->Flags) &
289991554f2SKenneth D. Merry 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
290991554f2SKenneth D. Merry 
291991554f2SKenneth D. Merry 		element =
292991554f2SKenneth D. Merry 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
293327f2e6cSStephen McConnell 		id = mpr_mapping_get_raid_tid_from_handle(sc,
294991554f2SKenneth D. Merry 		    element->VolDevHandle);
295991554f2SKenneth D. Merry 
296991554f2SKenneth D. Merry 		mpr_mapping_ir_config_change_event(sc, event_data);
297991554f2SKenneth D. Merry 		for (i = 0; i < event_data->NumElements; i++, element++) {
298991554f2SKenneth D. Merry 			reason = element->ReasonCode;
299991554f2SKenneth D. Merry 			elementType = le16toh(element->ElementFlags) &
300991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
301991554f2SKenneth D. Merry 			/*
302991554f2SKenneth D. Merry 			 * check for element type of Phys Disk or Hot Spare
303991554f2SKenneth D. Merry 			 */
304991554f2SKenneth D. Merry 			if ((elementType !=
305991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
306991554f2SKenneth D. Merry 			    && (elementType !=
307991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
308991554f2SKenneth D. Merry 				// do next element
309991554f2SKenneth D. Merry 				goto skip_fp_send;
310991554f2SKenneth D. Merry 
311991554f2SKenneth D. Merry 			/*
312991554f2SKenneth D. Merry 			 * check for reason of Hide, Unhide, PD Created, or PD
313991554f2SKenneth D. Merry 			 * Deleted
314991554f2SKenneth D. Merry 			 */
315991554f2SKenneth D. Merry 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
316991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
317991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
318991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
319991554f2SKenneth D. Merry 				goto skip_fp_send;
320991554f2SKenneth D. Merry 
321991554f2SKenneth D. Merry 			// check for a reason of Hide or PD Created
322991554f2SKenneth D. Merry 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
323991554f2SKenneth D. Merry 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
324991554f2SKenneth D. Merry 			{
325991554f2SKenneth D. Merry 				// build RAID Action message
326991554f2SKenneth D. Merry 				Mpi2RaidActionRequest_t	*action;
327991554f2SKenneth D. Merry 				Mpi2RaidActionReply_t *reply;
328991554f2SKenneth D. Merry 				struct mpr_command *cm;
329991554f2SKenneth D. Merry 				int error = 0;
330991554f2SKenneth D. Merry 				if ((cm = mpr_alloc_command(sc)) == NULL) {
331991554f2SKenneth D. Merry 					printf("%s: command alloc failed\n",
332991554f2SKenneth D. Merry 					    __func__);
333991554f2SKenneth D. Merry 					return;
334991554f2SKenneth D. Merry 				}
335991554f2SKenneth D. Merry 
336a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
337991554f2SKenneth D. Merry 				    "from "
338991554f2SKenneth D. Merry 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
339991554f2SKenneth D. Merry 				    ":\n");
340991554f2SKenneth D. Merry 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
341991554f2SKenneth D. Merry 				action->Function = MPI2_FUNCTION_RAID_ACTION;
342991554f2SKenneth D. Merry 				action->Action =
343991554f2SKenneth D. Merry 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
344991554f2SKenneth D. Merry 				action->PhysDiskNum = element->PhysDiskNum;
345991554f2SKenneth D. Merry 				cm->cm_desc.Default.RequestFlags =
346991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
347991554f2SKenneth D. Merry 				error = mpr_request_polled(sc, cm);
348991554f2SKenneth D. Merry 				reply = (Mpi2RaidActionReply_t *)cm->cm_reply;
349991554f2SKenneth D. Merry 				if (error || (reply == NULL)) {
350991554f2SKenneth D. Merry 					/* FIXME */
351991554f2SKenneth D. Merry 					/*
352991554f2SKenneth D. Merry 					 * If the poll returns error then we
353991554f2SKenneth D. Merry 					 * need to do diag reset
354991554f2SKenneth D. Merry 					 */
355991554f2SKenneth D. Merry 					printf("%s: poll for page completed "
356991554f2SKenneth D. Merry 					    "with error %d", __func__, error);
357991554f2SKenneth D. Merry 				}
358991554f2SKenneth D. Merry 				if (reply && (le16toh(reply->IOCStatus) &
359991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_MASK) !=
360991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_SUCCESS) {
361a2c14879SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: error "
362a2c14879SStephen McConnell 					    "sending RaidActionPage; "
363a2c14879SStephen McConnell 					    "iocstatus = 0x%x\n", __func__,
364991554f2SKenneth D. Merry 					    le16toh(reply->IOCStatus));
365991554f2SKenneth D. Merry 				}
366991554f2SKenneth D. Merry 
367991554f2SKenneth D. Merry 				if (cm)
368991554f2SKenneth D. Merry 					mpr_free_command(sc, cm);
369991554f2SKenneth D. Merry 			}
370991554f2SKenneth D. Merry skip_fp_send:
371a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_EVENT, "Received "
372991554f2SKenneth D. Merry 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
373991554f2SKenneth D. Merry 			    "code %x:\n", element->ReasonCode);
374991554f2SKenneth D. Merry 			switch (element->ReasonCode) {
375991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
376991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
377991554f2SKenneth D. Merry 				if (!foreign_config) {
378991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
379991554f2SKenneth D. Merry 					    le16toh(element->VolDevHandle))) {
380991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
381991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
382991554f2SKenneth D. Merry 						    __func__, le16toh(element->
383991554f2SKenneth D. Merry 						    VolDevHandle));
384991554f2SKenneth D. Merry 					}
385991554f2SKenneth D. Merry 				}
386991554f2SKenneth D. Merry 				break;
387991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
388991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
389991554f2SKenneth D. Merry 				/*
390991554f2SKenneth D. Merry 				 * Rescan after volume is deleted or removed.
391991554f2SKenneth D. Merry 				 */
392991554f2SKenneth D. Merry 				if (!foreign_config) {
393991554f2SKenneth D. Merry 					if (id == MPR_MAP_BAD_ID) {
394991554f2SKenneth D. Merry 						printf("%s: could not get ID "
395991554f2SKenneth D. Merry 						    "for volume with handle "
396991554f2SKenneth D. Merry 						    "0x%04x\n", __func__,
397991554f2SKenneth D. Merry 						    le16toh(element->
398991554f2SKenneth D. Merry 						    VolDevHandle));
399991554f2SKenneth D. Merry 						break;
400991554f2SKenneth D. Merry 					}
401991554f2SKenneth D. Merry 
402991554f2SKenneth D. Merry 					targ = &sassc->targets[id];
403991554f2SKenneth D. Merry 					targ->handle = 0x0;
404991554f2SKenneth D. Merry 					targ->encl_slot = 0x0;
405991554f2SKenneth D. Merry 					targ->encl_handle = 0x0;
406991554f2SKenneth D. Merry 					targ->encl_level_valid = 0x0;
407991554f2SKenneth D. Merry 					targ->encl_level = 0x0;
408991554f2SKenneth D. Merry 					targ->connector_name[0] = ' ';
409991554f2SKenneth D. Merry 					targ->connector_name[1] = ' ';
410991554f2SKenneth D. Merry 					targ->connector_name[2] = ' ';
411991554f2SKenneth D. Merry 					targ->connector_name[3] = ' ';
412991554f2SKenneth D. Merry 					targ->exp_dev_handle = 0x0;
413991554f2SKenneth D. Merry 					targ->phy_num = 0x0;
414991554f2SKenneth D. Merry 					targ->linkrate = 0x0;
415991554f2SKenneth D. Merry 					mprsas_rescan_target(sc, targ);
416991554f2SKenneth D. Merry 					printf("RAID target id 0x%x removed\n",
417991554f2SKenneth D. Merry 					    targ->tid);
418991554f2SKenneth D. Merry 				}
419991554f2SKenneth D. Merry 				break;
420991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
421991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
422991554f2SKenneth D. Merry 				/*
423991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been created.  Hide
424991554f2SKenneth D. Merry 				 * it from the OS.
425991554f2SKenneth D. Merry 				 */
426991554f2SKenneth D. Merry 				targ = mprsas_find_target_by_handle(sassc, 0,
427991554f2SKenneth D. Merry 				    element->PhysDiskDevHandle);
428991554f2SKenneth D. Merry 				if (targ == NULL)
429991554f2SKenneth D. Merry 					break;
430991554f2SKenneth D. Merry 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
431991554f2SKenneth D. Merry 				mprsas_rescan_target(sc, targ);
432991554f2SKenneth D. Merry 				break;
433991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
434991554f2SKenneth D. Merry 				/*
435991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been deleted.
436991554f2SKenneth D. Merry 				 * Expose it to the OS.
437991554f2SKenneth D. Merry 				 */
438991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
439991554f2SKenneth D. Merry 				    le16toh(element->PhysDiskDevHandle), 0)) {
440991554f2SKenneth D. Merry 					printf("%s: failed to add device with "
441991554f2SKenneth D. Merry 					    "handle 0x%x\n", __func__,
442991554f2SKenneth D. Merry 					    le16toh(element->
443991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
444991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc,
445991554f2SKenneth D. Merry 					    le16toh(element->
446991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
447991554f2SKenneth D. Merry 				}
448991554f2SKenneth D. Merry 				break;
449991554f2SKenneth D. Merry 			}
450991554f2SKenneth D. Merry 		}
451991554f2SKenneth D. Merry 		/*
452991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
453991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
454991554f2SKenneth D. Merry 		 * been processed.
455991554f2SKenneth D. Merry 		 */
456991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
457991554f2SKenneth D. Merry 		break;
458991554f2SKenneth D. Merry 	}
459991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_VOLUME:
460991554f2SKenneth D. Merry 	{
461991554f2SKenneth D. Merry 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
462991554f2SKenneth D. Merry 
463991554f2SKenneth D. Merry 		/*
464991554f2SKenneth D. Merry 		 * Informational only.
465991554f2SKenneth D. Merry 		 */
466991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
467991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
468991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
469991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
470991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
471991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
472991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
473991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
474991554f2SKenneth D. Merry 			break;
475991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
476991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
477991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
478991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
479991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
480991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
481991554f2SKenneth D. Merry 			break;
482991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
483991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
484991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
485991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
486991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
487991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
488991554f2SKenneth D. Merry 				u32 state;
489991554f2SKenneth D. Merry 				struct mprsas_target *targ;
490991554f2SKenneth D. Merry 				state = le32toh(event_data->NewValue);
491991554f2SKenneth D. Merry 				switch (state) {
492991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_MISSING:
493991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_FAILED:
494991554f2SKenneth D. Merry 					mprsas_prepare_volume_remove(sassc,
495991554f2SKenneth D. Merry 					    event_data->VolDevHandle);
496991554f2SKenneth D. Merry 					break;
497991554f2SKenneth D. Merry 
498991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_ONLINE:
499991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_DEGRADED:
500991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_OPTIMAL:
501991554f2SKenneth D. Merry 					targ =
502991554f2SKenneth D. Merry 					    mprsas_find_target_by_handle(sassc,
503991554f2SKenneth D. Merry 					    0, event_data->VolDevHandle);
504991554f2SKenneth D. Merry 					if (targ) {
505991554f2SKenneth D. Merry 						printf("%s %d: Volume handle "
506991554f2SKenneth D. Merry 						    "0x%x is already added \n",
507991554f2SKenneth D. Merry 						    __func__, __LINE__,
508991554f2SKenneth D. Merry 						    event_data->VolDevHandle);
509991554f2SKenneth D. Merry 						break;
510991554f2SKenneth D. Merry 					}
511991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
512991554f2SKenneth D. Merry 					    le16toh(event_data->
513991554f2SKenneth D. Merry 					    VolDevHandle))) {
514991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
515991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
516991554f2SKenneth D. Merry 						    __func__, le16toh(
517991554f2SKenneth D. Merry 						    event_data->VolDevHandle));
518991554f2SKenneth D. Merry 					}
519991554f2SKenneth D. Merry 					break;
520991554f2SKenneth D. Merry 				default:
521991554f2SKenneth D. Merry 					break;
522991554f2SKenneth D. Merry 				}
523991554f2SKenneth D. Merry 			break;
524991554f2SKenneth D. Merry 		default:
525991554f2SKenneth D. Merry 			break;
526991554f2SKenneth D. Merry 		}
527991554f2SKenneth D. Merry 		break;
528991554f2SKenneth D. Merry 	}
529991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_PHYSICAL_DISK:
530991554f2SKenneth D. Merry 	{
531991554f2SKenneth D. Merry 		Mpi2EventDataIrPhysicalDisk_t *event_data =
532991554f2SKenneth D. Merry 		    fw_event->event_data;
533991554f2SKenneth D. Merry 		struct mprsas_target *targ;
534991554f2SKenneth D. Merry 
535991554f2SKenneth D. Merry 		/*
536991554f2SKenneth D. Merry 		 * Informational only.
537991554f2SKenneth D. Merry 		 */
538991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
539991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
540991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
541991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
542991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Phys Disk Number "
543991554f2SKenneth D. Merry   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
544991554f2SKenneth D. Merry  			    "%d", le32toh(event_data->PreviousValue),
545991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
546991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
547991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
548991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
549991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
550991554f2SKenneth D. Merry 			break;
551991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
552991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
553991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
554991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
555991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
556991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
557991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
558991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
559991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
560991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
561991554f2SKenneth D. Merry 			break;
562991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
563991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
564991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
565991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
566991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
567991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
568991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
569991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
570991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
571991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
572991554f2SKenneth D. Merry 			switch (event_data->NewValue) {
573991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_ONLINE:
574991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_DEGRADED:
575991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_REBUILDING:
576991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OPTIMAL:
577991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_HOT_SPARE:
578991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
579991554f2SKenneth D. Merry 					    sassc, 0,
580991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
581991554f2SKenneth D. Merry 					if (targ) {
582991554f2SKenneth D. Merry 						targ->flags |=
583991554f2SKenneth D. Merry 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
584991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
585991554f2SKenneth D. Merry 						    "for handle 0x%x.\n",
586991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
587991554f2SKenneth D. Merry 						    event_data->
588991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
589991554f2SKenneth D. Merry 					}
590991554f2SKenneth D. Merry 				break;
591991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OFFLINE:
592991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
593991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
594991554f2SKenneth D. Merry 				default:
595991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
596991554f2SKenneth D. Merry 					    sassc, 0,
597991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
598991554f2SKenneth D. Merry 					if (targ) {
599991554f2SKenneth D. Merry 						targ->flags |=
600991554f2SKenneth D. Merry 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
601991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
602991554f2SKenneth D. Merry 						    "for handle 0x%x.  \n",
603991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
604991554f2SKenneth D. Merry 						    event_data->
605991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
606991554f2SKenneth D. Merry 					}
607991554f2SKenneth D. Merry 				break;
608991554f2SKenneth D. Merry 			}
609991554f2SKenneth D. Merry 		default:
610991554f2SKenneth D. Merry 			break;
611991554f2SKenneth D. Merry 		}
612991554f2SKenneth D. Merry 		break;
613991554f2SKenneth D. Merry 	}
614991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_OPERATION_STATUS:
615991554f2SKenneth D. Merry 	{
616991554f2SKenneth D. Merry 		Mpi2EventDataIrOperationStatus_t *event_data =
617991554f2SKenneth D. Merry 		    fw_event->event_data;
618991554f2SKenneth D. Merry 
619991554f2SKenneth D. Merry 		/*
620991554f2SKenneth D. Merry 		 * Informational only.
621991554f2SKenneth D. Merry 		 */
622991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
623991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
624991554f2SKenneth D. Merry 		    "percent complete for Volume with handle 0x%x",
625991554f2SKenneth D. Merry 		    event_data->RAIDOperation, event_data->PercentComplete,
626991554f2SKenneth D. Merry 		    le16toh(event_data->VolDevHandle));
627991554f2SKenneth D. Merry 		break;
628991554f2SKenneth D. Merry 	}
629991554f2SKenneth D. Merry 	case MPI2_EVENT_TEMP_THRESHOLD:
630991554f2SKenneth D. Merry 	{
631991554f2SKenneth D. Merry 		pMpi2EventDataTemperature_t	temp_event;
632991554f2SKenneth D. Merry 
633991554f2SKenneth D. Merry 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
634991554f2SKenneth D. Merry 
635991554f2SKenneth D. Merry 		/*
636991554f2SKenneth D. Merry 		 * The Temp Sensor Count must be greater than the event's Sensor
637991554f2SKenneth D. Merry 		 * Num to be valid.  If valid, print the temp thresholds that
638991554f2SKenneth D. Merry 		 * have been exceeded.
639991554f2SKenneth D. Merry 		 */
640991554f2SKenneth D. Merry 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
641991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
642991554f2SKenneth D. Merry 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
643991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
644991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
645991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
646991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
647991554f2SKenneth D. Merry 			    temp_event->SensorNum);
648991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
649991554f2SKenneth D. Merry 			    "%d\n", temp_event->CurrentTemperature);
650991554f2SKenneth D. Merry 		}
651991554f2SKenneth D. Merry 		break;
652991554f2SKenneth D. Merry 	}
6532bbc5fcbSStephen McConnell 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
6542bbc5fcbSStephen McConnell 	{
6552bbc5fcbSStephen McConnell 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
6562bbc5fcbSStephen McConnell 		ace_event_data =
6572bbc5fcbSStephen McConnell 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
6582bbc5fcbSStephen McConnell 
6590656476aSAlexander Motin 		switch(ace_event_data->ReasonCode) {
6600656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
6610656476aSAlexander Motin 		{
6620656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6632bbc5fcbSStephen McConnell 			    "ReceptacleID %d cannot be powered and device "
6642bbc5fcbSStephen McConnell 			    "connected to this active cable will not be seen. "
6652bbc5fcbSStephen McConnell 			    "This active cable requires %d mW of power.\n",
6662bbc5fcbSStephen McConnell 			    ace_event_data->ReceptacleID,
6672bbc5fcbSStephen McConnell 			    ace_event_data->ActiveCablePowerRequirement);
6680656476aSAlexander Motin 			break;
6690656476aSAlexander Motin 		}
6700656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
6710656476aSAlexander Motin 		{
6720656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6730656476aSAlexander Motin 			    "ReceptacleID %d is not running at optimal speed "
6740656476aSAlexander Motin 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
6750656476aSAlexander Motin 			break;
6760656476aSAlexander Motin 		}
6770656476aSAlexander Motin 		default:
6780656476aSAlexander Motin 			break;
6792bbc5fcbSStephen McConnell 		}
6802bbc5fcbSStephen McConnell 		break;
6812bbc5fcbSStephen McConnell 	}
68267feec50SStephen McConnell 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
68367feec50SStephen McConnell 	{
68467feec50SStephen McConnell 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
68567feec50SStephen McConnell 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
68667feec50SStephen McConnell 		uint8_t i, link_rate;
68767feec50SStephen McConnell 		uint16_t handle;
68867feec50SStephen McConnell 
68967feec50SStephen McConnell 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
69067feec50SStephen McConnell 		    fw_event->event_data;
69167feec50SStephen McConnell 
69267feec50SStephen McConnell 		mpr_mapping_pcie_topology_change_event(sc,
69367feec50SStephen McConnell 		    fw_event->event_data);
69467feec50SStephen McConnell 
69567feec50SStephen McConnell 		for (i = 0; i < data->NumEntries; i++) {
69667feec50SStephen McConnell 			port_entry = &data->PortEntry[i];
69767feec50SStephen McConnell 			handle = le16toh(port_entry->AttachedDevHandle);
69867feec50SStephen McConnell 			link_rate = port_entry->CurrentPortInfo &
69967feec50SStephen McConnell 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
70067feec50SStephen McConnell 			switch (port_entry->PortStatus) {
70167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
70267feec50SStephen McConnell 				if (link_rate <
70367feec50SStephen McConnell 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
70467feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
70567feec50SStephen McConnell 					    "add PCIe device with handle 0x%x "
70667feec50SStephen McConnell 					    "with unknown link rate.\n",
70767feec50SStephen McConnell 					    __func__, handle);
70867feec50SStephen McConnell 					break;
70967feec50SStephen McConnell 				}
71067feec50SStephen McConnell 				if (mprsas_add_pcie_device(sc, handle,
71167feec50SStephen McConnell 				    link_rate)) {
71267feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
71367feec50SStephen McConnell 					    "to add PCIe device with handle "
71467feec50SStephen McConnell 					    "0x%x\n", __func__, handle);
71567feec50SStephen McConnell 					mprsas_prepare_remove(sassc, handle);
71667feec50SStephen McConnell 				}
71767feec50SStephen McConnell 				break;
71867feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
71967feec50SStephen McConnell 				mprsas_prepare_remove(sassc, handle);
72067feec50SStephen McConnell 				break;
72167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
72267feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
72367feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
72467feec50SStephen McConnell 			default:
72567feec50SStephen McConnell 				break;
72667feec50SStephen McConnell 			}
72767feec50SStephen McConnell 		}
72867feec50SStephen McConnell 		/*
72967feec50SStephen McConnell 		 * refcount was incremented for this event in
73067feec50SStephen McConnell 		 * mprsas_evt_handler.  Decrement it here because the event has
73167feec50SStephen McConnell 		 * been processed.
73267feec50SStephen McConnell 		 */
73367feec50SStephen McConnell 		mprsas_startup_decrement(sassc);
73467feec50SStephen McConnell 		break;
73567feec50SStephen McConnell 	}
736991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
737991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
738991554f2SKenneth D. Merry 	default:
739991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
740991554f2SKenneth D. Merry 		    fw_event->event);
741991554f2SKenneth D. Merry 		break;
742991554f2SKenneth D. Merry 
743991554f2SKenneth D. Merry 	}
744991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
745991554f2SKenneth D. Merry 	    __func__, fw_event->event);
746991554f2SKenneth D. Merry 	mprsas_fw_event_free(sc, fw_event);
747991554f2SKenneth D. Merry }
748991554f2SKenneth D. Merry 
749991554f2SKenneth D. Merry void
750991554f2SKenneth D. Merry mprsas_firmware_event_work(void *arg, int pending)
751991554f2SKenneth D. Merry {
752991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
753991554f2SKenneth D. Merry 	struct mpr_softc *sc;
754991554f2SKenneth D. Merry 
755991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
756991554f2SKenneth D. Merry 	mpr_lock(sc);
757991554f2SKenneth D. Merry 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
758991554f2SKenneth D. Merry 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
759991554f2SKenneth D. Merry 		mprsas_fw_work(sc, fw_event);
760991554f2SKenneth D. Merry 	}
761991554f2SKenneth D. Merry 	mpr_unlock(sc);
762991554f2SKenneth D. Merry }
763991554f2SKenneth D. Merry 
764991554f2SKenneth D. Merry static int
76567feec50SStephen McConnell mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
76667feec50SStephen McConnell {
767991554f2SKenneth D. Merry 	char devstring[80];
768991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
769991554f2SKenneth D. Merry 	struct mprsas_target *targ;
770991554f2SKenneth D. Merry 	Mpi2ConfigReply_t mpi_reply;
771991554f2SKenneth D. Merry 	Mpi2SasDevicePage0_t config_page;
772a2c14879SStephen McConnell 	uint64_t sas_address, parent_sas_address = 0;
773991554f2SKenneth D. Merry 	u32 device_info, parent_devinfo = 0;
774991554f2SKenneth D. Merry 	unsigned int id;
775a2c14879SStephen McConnell 	int ret = 1, error = 0, i;
776991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
777a2c14879SStephen McConnell 	u8 is_SATA_SSD = 0;
778a2c14879SStephen McConnell 	struct mpr_command *cm;
779991554f2SKenneth D. Merry 
780991554f2SKenneth D. Merry 	sassc = sc->sassc;
781991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
782991554f2SKenneth D. Merry 	if ((mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
783991554f2SKenneth D. Merry 	     MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
784991554f2SKenneth D. Merry 		printf("%s: error reading SAS device page0\n", __func__);
785991554f2SKenneth D. Merry 		error = ENXIO;
786991554f2SKenneth D. Merry 		goto out;
787991554f2SKenneth D. Merry 	}
788991554f2SKenneth D. Merry 
789991554f2SKenneth D. Merry 	device_info = le32toh(config_page.DeviceInfo);
790991554f2SKenneth D. Merry 
791991554f2SKenneth D. Merry 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
792991554f2SKenneth D. Merry 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
793991554f2SKenneth D. Merry 		Mpi2ConfigReply_t tmp_mpi_reply;
794991554f2SKenneth D. Merry 		Mpi2SasDevicePage0_t parent_config_page;
795991554f2SKenneth D. Merry 
796991554f2SKenneth D. Merry 		if ((mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
797991554f2SKenneth D. Merry 		     &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
798991554f2SKenneth D. Merry 		     le16toh(config_page.ParentDevHandle)))) {
799991554f2SKenneth D. Merry 			printf("%s: error reading SAS device %#x page0\n",
800991554f2SKenneth D. Merry 			    __func__, le16toh(config_page.ParentDevHandle));
801991554f2SKenneth D. Merry 		} else {
802991554f2SKenneth D. Merry 			parent_sas_address = parent_config_page.SASAddress.High;
803991554f2SKenneth D. Merry 			parent_sas_address = (parent_sas_address << 32) |
804991554f2SKenneth D. Merry 			    parent_config_page.SASAddress.Low;
805991554f2SKenneth D. Merry 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
806991554f2SKenneth D. Merry 		}
807991554f2SKenneth D. Merry 	}
808453130d9SPedro F. Giffuni 	/* TODO Check proper endianness */
809991554f2SKenneth D. Merry 	sas_address = config_page.SASAddress.High;
810a2c14879SStephen McConnell 	sas_address = (sas_address << 32) | config_page.SASAddress.Low;
811a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "SAS Address from SAS device page0 = %jx\n",
812a2c14879SStephen McConnell 	    sas_address);
813991554f2SKenneth D. Merry 
814a2c14879SStephen McConnell 	/*
815a2c14879SStephen McConnell 	 * Always get SATA Identify information because this is used to
816a2c14879SStephen McConnell 	 * determine if Start/Stop Unit should be sent to the drive when the
817a2c14879SStephen McConnell 	 * system is shutdown.
818a2c14879SStephen McConnell 	 */
819991554f2SKenneth D. Merry 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
820a2c14879SStephen McConnell 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
821a2c14879SStephen McConnell 		    handle, device_info, &is_SATA_SSD);
822a2c14879SStephen McConnell 		if (ret) {
823a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "%s: failed to get disk type "
824a2c14879SStephen McConnell 			    "(SSD or HDD) for SATA device with handle 0x%04x\n",
825a2c14879SStephen McConnell 			    __func__, handle);
826a2c14879SStephen McConnell 		} else {
827a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "SAS Address from SATA "
828a2c14879SStephen McConnell 			    "device = %jx\n", sas_address);
829a2c14879SStephen McConnell 		}
830a2c14879SStephen McConnell 	}
831c91d307fSStephen McConnell 
8324ab1cdc5SScott Long 	/*
8334ab1cdc5SScott Long 	 * use_phynum:
8344ab1cdc5SScott Long 	 *  1 - use the PhyNum field as a fallback to the mapping logic
8354ab1cdc5SScott Long 	 *  0 - never use the PhyNum field
8364ab1cdc5SScott Long 	 * -1 - only use the PhyNum field
837327f2e6cSStephen McConnell 	 *
838327f2e6cSStephen McConnell 	 * Note that using the Phy number to map a device can cause device adds
839327f2e6cSStephen McConnell 	 * to fail if multiple enclosures/expanders are in the topology. For
840327f2e6cSStephen McConnell 	 * example, if two devices are in the same slot number in two different
841327f2e6cSStephen McConnell 	 * enclosures within the topology, only one of those devices will be
842327f2e6cSStephen McConnell 	 * added. PhyNum mapping should not be used if multiple enclosures are
843327f2e6cSStephen McConnell 	 * in the topology.
8444ab1cdc5SScott Long 	 */
8454ab1cdc5SScott Long 	id = MPR_MAP_BAD_ID;
8464ab1cdc5SScott Long 	if (sc->use_phynum != -1)
847327f2e6cSStephen McConnell 		id = mpr_mapping_get_tid(sc, sas_address, handle);
848991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
84967feec50SStephen McConnell 		if ((sc->use_phynum == 0) ||
85067feec50SStephen McConnell 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
8514ab1cdc5SScott Long 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
8524ab1cdc5SScott Long 			    "Could not get ID for device with handle 0x%04x\n",
8534ab1cdc5SScott Long 			    __FILE__, __LINE__, __func__, handle);
854991554f2SKenneth D. Merry 			error = ENXIO;
855991554f2SKenneth D. Merry 			goto out;
856991554f2SKenneth D. Merry 		}
8574ab1cdc5SScott Long 	}
858327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
859327f2e6cSStephen McConnell 	    __func__, id);
860991554f2SKenneth D. Merry 
861327f2e6cSStephen McConnell 	/*
862327f2e6cSStephen McConnell 	 * Only do the ID check and reuse check if the target is not from a
863327f2e6cSStephen McConnell 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
864327f2e6cSStephen McConnell 	 * when a volume is deleted because the mapping entry for the PD will
865327f2e6cSStephen McConnell 	 * still be in the mapping table. The ID check should not be done here
866327f2e6cSStephen McConnell 	 * either since this PD is already being used.
867327f2e6cSStephen McConnell 	 */
868327f2e6cSStephen McConnell 	targ = &sassc->targets[id];
869327f2e6cSStephen McConnell 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
870991554f2SKenneth D. Merry 		if (mprsas_check_id(sassc, id) != 0) {
871327f2e6cSStephen McConnell 			device_printf(sc->mpr_dev, "Excluding target id %d\n",
872327f2e6cSStephen McConnell 			    id);
873991554f2SKenneth D. Merry 			error = ENXIO;
874991554f2SKenneth D. Merry 			goto out;
875991554f2SKenneth D. Merry 		}
876991554f2SKenneth D. Merry 
8774ab1cdc5SScott Long 		if (targ->handle != 0x0) {
878327f2e6cSStephen McConnell 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
879327f2e6cSStephen McConnell 			    "target id %d handle 0x%04x\n", id, targ->handle);
8804ab1cdc5SScott Long 			error = ENXIO;
8814ab1cdc5SScott Long 			goto out;
8824ab1cdc5SScott Long 		}
883327f2e6cSStephen McConnell 	}
8844ab1cdc5SScott Long 
885991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "SAS Address from SAS device page0 = %jx\n",
886991554f2SKenneth D. Merry 	    sas_address);
887991554f2SKenneth D. Merry 	targ->devinfo = device_info;
888991554f2SKenneth D. Merry 	targ->devname = le32toh(config_page.DeviceName.High);
889991554f2SKenneth D. Merry 	targ->devname = (targ->devname << 32) |
890991554f2SKenneth D. Merry 	    le32toh(config_page.DeviceName.Low);
891991554f2SKenneth D. Merry 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
892991554f2SKenneth D. Merry 	targ->encl_slot = le16toh(config_page.Slot);
893991554f2SKenneth D. Merry 	targ->encl_level = config_page.EnclosureLevel;
894991554f2SKenneth D. Merry 	targ->connector_name[0] = config_page.ConnectorName[0];
895991554f2SKenneth D. Merry 	targ->connector_name[1] = config_page.ConnectorName[1];
896991554f2SKenneth D. Merry 	targ->connector_name[2] = config_page.ConnectorName[2];
897991554f2SKenneth D. Merry 	targ->connector_name[3] = config_page.ConnectorName[3];
898991554f2SKenneth D. Merry 	targ->handle = handle;
899991554f2SKenneth D. Merry 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
900991554f2SKenneth D. Merry 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
901991554f2SKenneth D. Merry 	targ->parent_sasaddr = le64toh(parent_sas_address);
902991554f2SKenneth D. Merry 	targ->parent_devinfo = parent_devinfo;
903991554f2SKenneth D. Merry 	targ->tid = id;
904991554f2SKenneth D. Merry 	targ->linkrate = (linkrate>>4);
905991554f2SKenneth D. Merry 	targ->flags = 0;
906a2c14879SStephen McConnell 	if (is_SATA_SSD) {
907a2c14879SStephen McConnell 		targ->flags = MPR_TARGET_IS_SATA_SSD;
908a2c14879SStephen McConnell 	}
90967feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
91067feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
91167feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
91267feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
913991554f2SKenneth D. Merry 		targ->scsi_req_desc_type =
914991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
915991554f2SKenneth D. Merry 	}
916991554f2SKenneth D. Merry 	if (le16toh(config_page.Flags) &
917991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
918991554f2SKenneth D. Merry 		targ->encl_level_valid = TRUE;
919991554f2SKenneth D. Merry 	}
920991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
921991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
922991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
923991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
924991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
925991554f2SKenneth D. Merry 		free(lun, M_MPR);
926991554f2SKenneth D. Merry 	}
927991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
928991554f2SKenneth D. Merry 
929991554f2SKenneth D. Merry 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
930a2c14879SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
931991554f2SKenneth D. Merry 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
932991554f2SKenneth D. Merry 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
933991554f2SKenneth D. Merry 	    targ->handle, targ->encl_handle, targ->encl_slot);
934991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
935a2c14879SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
936991554f2SKenneth D. Merry 		    "and connector name (%4s)\n", targ->encl_level,
937991554f2SKenneth D. Merry 		    targ->connector_name);
938991554f2SKenneth D. Merry 	}
939a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
940a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
941991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
942991554f2SKenneth D. Merry #endif
943991554f2SKenneth D. Merry 		mprsas_rescan_target(sc, targ);
944991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
945a2c14879SStephen McConnell 
946a2c14879SStephen McConnell 	/*
947a2c14879SStephen McConnell 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
948a2c14879SStephen McConnell 	 * If so, send a Target Reset TM to the target that was just created.
949a2c14879SStephen McConnell 	 * An Abort Task TM should be used instead of a Target Reset, but that
950a2c14879SStephen McConnell 	 * would be much more difficult because targets have not been fully
951a2c14879SStephen McConnell 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
952a2c14879SStephen McConnell 	 * target instead of the LUN.
953a2c14879SStephen McConnell 	 */
954a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
955a2c14879SStephen McConnell 		cm = &sc->commands[i];
956a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
957a2c14879SStephen McConnell 			targ->timeouts++;
958a2c14879SStephen McConnell 			cm->cm_state = MPR_CM_STATE_TIMEDOUT;
959a2c14879SStephen McConnell 
960a2c14879SStephen McConnell 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
961a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
962a2c14879SStephen McConnell 				    "Reset for stuck SATA identify command "
963a2c14879SStephen McConnell 				    "(cm = %p)\n", __func__, cm);
964a2c14879SStephen McConnell 				targ->tm->cm_targ = targ;
965a2c14879SStephen McConnell 				mprsas_send_reset(sc, targ->tm,
966a2c14879SStephen McConnell 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
967a2c14879SStephen McConnell 			} else {
968a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
9697a2a6a1aSStephen McConnell 				    "tm for Target Reset after SATA ID command "
9707a2a6a1aSStephen McConnell 				    "timed out (cm %p)\n", cm);
971a2c14879SStephen McConnell 			}
972a2c14879SStephen McConnell 			/*
973a2c14879SStephen McConnell 			 * No need to check for more since the target is
974a2c14879SStephen McConnell 			 * already being reset.
975a2c14879SStephen McConnell 			 */
976a2c14879SStephen McConnell 			break;
977a2c14879SStephen McConnell 		}
978a2c14879SStephen McConnell 	}
979991554f2SKenneth D. Merry out:
980a2c14879SStephen McConnell 	/*
981a2c14879SStephen McConnell 	 * Free the commands that may not have been freed from the SATA ID call
982a2c14879SStephen McConnell 	 */
983a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
984a2c14879SStephen McConnell 		cm = &sc->commands[i];
985a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
986a2c14879SStephen McConnell 			mpr_free_command(sc, cm);
987a2c14879SStephen McConnell 		}
988a2c14879SStephen McConnell 	}
989991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
990991554f2SKenneth D. Merry 	return (error);
991991554f2SKenneth D. Merry }
992991554f2SKenneth D. Merry 
993991554f2SKenneth D. Merry int
994991554f2SKenneth D. Merry mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
995a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
996991554f2SKenneth D. Merry {
997991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t mpi_reply;
998991554f2SKenneth D. Merry 	int i, rc, try_count;
999991554f2SKenneth D. Merry 	u32 *bufferptr;
1000991554f2SKenneth D. Merry 	union _sata_sas_address hash_address;
1001991554f2SKenneth D. Merry 	struct _ata_identify_device_data ata_identify;
1002991554f2SKenneth D. Merry 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1003991554f2SKenneth D. Merry 	u32 ioc_status;
1004991554f2SKenneth D. Merry 	u8 sas_status;
1005991554f2SKenneth D. Merry 
1006991554f2SKenneth D. Merry 	memset(&ata_identify, 0, sizeof(ata_identify));
10074e02badbSAlan Somers 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1008991554f2SKenneth D. Merry 	try_count = 0;
1009991554f2SKenneth D. Merry 	do {
1010991554f2SKenneth D. Merry 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1011991554f2SKenneth D. Merry 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1012991554f2SKenneth D. Merry 		try_count++;
1013991554f2SKenneth D. Merry 		ioc_status = le16toh(mpi_reply.IOCStatus)
1014991554f2SKenneth D. Merry 		    & MPI2_IOCSTATUS_MASK;
1015991554f2SKenneth D. Merry 		sas_status = mpi_reply.SASStatus;
1016a92fe027SAlan Somers 		switch (ioc_status) {
1017a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SUCCESS:
1018a92fe027SAlan Somers 			break;
1019a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1020a92fe027SAlan Somers 			/* No sense sleeping.  this error won't get better */
1021a92fe027SAlan Somers 			break;
1022a92fe027SAlan Somers 		default:
1023a2c14879SStephen McConnell 			if (sc->spinup_wait_time > 0) {
1024a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1025a2c14879SStephen McConnell 				    "after SATA ID error to wait for spinup\n",
1026a2c14879SStephen McConnell 				    sc->spinup_wait_time);
1027a2c14879SStephen McConnell 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1028a2c14879SStephen McConnell 				    "mprid", sc->spinup_wait_time * hz);
1029a2c14879SStephen McConnell 			}
1030a2c14879SStephen McConnell 		}
1031a92fe027SAlan Somers 	} while (((rc && (rc != EWOULDBLOCK)) ||
10327a2a6a1aSStephen McConnell 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1033a92fe027SAlan Somers 	    || sas_status) && (try_count < 5));
1034991554f2SKenneth D. Merry 
1035991554f2SKenneth D. Merry 	if (rc == 0 && !ioc_status && !sas_status) {
1036991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1037991554f2SKenneth D. Merry 		    "successfully for handle = 0x%x with try_count = %d\n",
1038991554f2SKenneth D. Merry 		    __func__, handle, try_count);
1039991554f2SKenneth D. Merry 	} else {
1040991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1041991554f2SKenneth D. Merry 		    __func__, handle);
1042991554f2SKenneth D. Merry 		return -1;
1043991554f2SKenneth D. Merry 	}
1044991554f2SKenneth D. Merry 	/* Copy & byteswap the 40 byte model number to a buffer */
1045991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1046991554f2SKenneth D. Merry 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1047991554f2SKenneth D. Merry 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1048991554f2SKenneth D. Merry 	}
1049991554f2SKenneth D. Merry 	/* Copy & byteswap the 20 byte serial number to a buffer */
1050991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1051991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i] =
1052991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i + 1];
1053991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i + 1] =
1054991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i];
1055991554f2SKenneth D. Merry 	}
1056991554f2SKenneth D. Merry 	bufferptr = (u32 *)buffer;
1057991554f2SKenneth D. Merry 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1058991554f2SKenneth D. Merry 	 * so loop through the first 56 bytes (7*8),
1059991554f2SKenneth D. Merry 	 * and then add in the last dword.
1060991554f2SKenneth D. Merry 	 */
1061991554f2SKenneth D. Merry 	hash_address.word.low  = 0;
1062991554f2SKenneth D. Merry 	hash_address.word.high = 0;
1063991554f2SKenneth D. Merry 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1064991554f2SKenneth D. Merry 		hash_address.word.low += *bufferptr;
1065991554f2SKenneth D. Merry 		bufferptr++;
1066991554f2SKenneth D. Merry 		hash_address.word.high += *bufferptr;
1067991554f2SKenneth D. Merry 		bufferptr++;
1068991554f2SKenneth D. Merry 	}
1069991554f2SKenneth D. Merry 	/* Add the last dword */
1070991554f2SKenneth D. Merry 	hash_address.word.low += *bufferptr;
1071991554f2SKenneth D. Merry 	/* Make sure the hash doesn't start with 5, because it could clash
1072991554f2SKenneth D. Merry 	 * with a SAS address. Change 5 to a D.
1073991554f2SKenneth D. Merry 	 */
1074991554f2SKenneth D. Merry 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1075991554f2SKenneth D. Merry 		hash_address.word.high |= 0x00000080;
1076991554f2SKenneth D. Merry 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1077991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1078991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1079991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1080991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[7];
1081a2c14879SStephen McConnell 	if (ata_identify.rotational_speed == 1) {
1082a2c14879SStephen McConnell 		*is_SATA_SSD = 1;
1083a2c14879SStephen McConnell 	}
1084a2c14879SStephen McConnell 
1085991554f2SKenneth D. Merry 	return 0;
1086991554f2SKenneth D. Merry }
1087991554f2SKenneth D. Merry 
1088991554f2SKenneth D. Merry static int
1089991554f2SKenneth D. Merry mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1090991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1091991554f2SKenneth D. Merry {
1092991554f2SKenneth D. Merry 	Mpi2SataPassthroughRequest_t *mpi_request;
1093991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t *reply;
1094991554f2SKenneth D. Merry 	struct mpr_command *cm;
1095991554f2SKenneth D. Merry 	char *buffer;
1096991554f2SKenneth D. Merry 	int error = 0;
1097991554f2SKenneth D. Merry 
1098991554f2SKenneth D. Merry 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1099991554f2SKenneth D. Merry 	if (!buffer)
1100991554f2SKenneth D. Merry 		return ENOMEM;
1101991554f2SKenneth D. Merry 
1102991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1103991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1104991554f2SKenneth D. Merry 		return (EBUSY);
1105991554f2SKenneth D. Merry 	}
1106991554f2SKenneth D. Merry 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1107991554f2SKenneth D. Merry 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1108991554f2SKenneth D. Merry 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1109991554f2SKenneth D. Merry 	mpi_request->VF_ID = 0;
1110991554f2SKenneth D. Merry 	mpi_request->DevHandle = htole16(handle);
1111991554f2SKenneth D. Merry 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1112991554f2SKenneth D. Merry 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1113991554f2SKenneth D. Merry 	mpi_request->DataLength = htole32(sz);
1114991554f2SKenneth D. Merry 	mpi_request->CommandFIS[0] = 0x27;
1115991554f2SKenneth D. Merry 	mpi_request->CommandFIS[1] = 0x80;
1116991554f2SKenneth D. Merry 	mpi_request->CommandFIS[2] =  (devinfo &
1117991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1118991554f2SKenneth D. Merry 	cm->cm_sge = &mpi_request->SGL;
1119991554f2SKenneth D. Merry 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1120991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1121991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1122991554f2SKenneth D. Merry 	cm->cm_data = buffer;
1123991554f2SKenneth D. Merry 	cm->cm_length = htole32(sz);
1124a2c14879SStephen McConnell 
1125a2c14879SStephen McConnell 	/*
1126a2c14879SStephen McConnell 	 * Start a timeout counter specifically for the SATA ID command. This
1127a2c14879SStephen McConnell 	 * is used to fix a problem where the FW does not send a reply sometimes
1128a2c14879SStephen McConnell 	 * when a bad disk is in the topology. So, this is used to timeout the
1129a2c14879SStephen McConnell 	 * command so that processing can continue normally.
1130a2c14879SStephen McConnell 	 */
1131a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s start timeout counter for SATA ID "
1132a2c14879SStephen McConnell 	    "command\n", __func__);
1133a2c14879SStephen McConnell 	callout_reset(&cm->cm_callout, MPR_ATA_ID_TIMEOUT * hz,
1134a2c14879SStephen McConnell 	    mprsas_ata_id_timeout, cm);
1135991554f2SKenneth D. Merry 	error = mpr_wait_command(sc, cm, 60, CAN_SLEEP);
1136a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_XINFO, "%s stop timeout counter for SATA ID "
1137a2c14879SStephen McConnell 	    "command\n", __func__);
1138a2c14879SStephen McConnell 	callout_stop(&cm->cm_callout);
1139a2c14879SStephen McConnell 
1140991554f2SKenneth D. Merry 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1141991554f2SKenneth D. Merry 	if (error || (reply == NULL)) {
1142991554f2SKenneth D. Merry 		/* FIXME */
1143991554f2SKenneth D. Merry 		/*
1144991554f2SKenneth D. Merry 		 * If the request returns an error then we need to do a diag
1145991554f2SKenneth D. Merry 		 * reset
1146991554f2SKenneth D. Merry 		 */
1147991554f2SKenneth D. Merry 		printf("%s: request for page completed with error %d",
1148991554f2SKenneth D. Merry 		    __func__, error);
1149991554f2SKenneth D. Merry 		error = ENXIO;
1150991554f2SKenneth D. Merry 		goto out;
1151991554f2SKenneth D. Merry 	}
1152991554f2SKenneth D. Merry 	bcopy(buffer, id_buffer, sz);
1153991554f2SKenneth D. Merry 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1154991554f2SKenneth D. Merry 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1155991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
1156991554f2SKenneth D. Merry 		printf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n",
1157991554f2SKenneth D. Merry 		    __func__, reply->IOCStatus);
1158991554f2SKenneth D. Merry 		error = ENXIO;
1159991554f2SKenneth D. Merry 		goto out;
1160991554f2SKenneth D. Merry 	}
1161991554f2SKenneth D. Merry out:
1162a2c14879SStephen McConnell 	/*
1163a2c14879SStephen McConnell 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
1164a2c14879SStephen McConnell 	 * it.  The command will be freed after sending a target reset TM. If
1165a2c14879SStephen McConnell 	 * the command did timeout, use EWOULDBLOCK.
1166a2c14879SStephen McConnell 	 */
1167a2c14879SStephen McConnell 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0)
1168991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1169a2c14879SStephen McConnell 	else if (error == 0)
1170a2c14879SStephen McConnell 		error = EWOULDBLOCK;
1171fa699bb2SAlan Somers 	cm->cm_data = NULL;
1172991554f2SKenneth D. Merry 	free(buffer, M_MPR);
1173991554f2SKenneth D. Merry 	return (error);
1174991554f2SKenneth D. Merry }
1175991554f2SKenneth D. Merry 
1176a2c14879SStephen McConnell static void
1177a2c14879SStephen McConnell mprsas_ata_id_timeout(void *data)
1178a2c14879SStephen McConnell {
1179a2c14879SStephen McConnell 	struct mpr_softc *sc;
1180a2c14879SStephen McConnell 	struct mpr_command *cm;
1181a2c14879SStephen McConnell 
1182a2c14879SStephen McConnell 	cm = (struct mpr_command *)data;
1183a2c14879SStephen McConnell 	sc = cm->cm_sc;
1184a2c14879SStephen McConnell 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
1185a2c14879SStephen McConnell 
1186a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "%s checking ATA ID command %p sc %p\n",
1187a2c14879SStephen McConnell 	    __func__, cm, sc);
1188a2c14879SStephen McConnell 	if ((callout_pending(&cm->cm_callout)) ||
1189a2c14879SStephen McConnell 	    (!callout_active(&cm->cm_callout))) {
11907a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s ATA ID command almost timed out\n",
11917a2a6a1aSStephen McConnell 		    __func__);
1192a2c14879SStephen McConnell 		return;
1193a2c14879SStephen McConnell 	}
1194a2c14879SStephen McConnell 	callout_deactivate(&cm->cm_callout);
1195a2c14879SStephen McConnell 
1196a2c14879SStephen McConnell 	/*
1197a2c14879SStephen McConnell 	 * Run the interrupt handler to make sure it's not pending.  This
1198a2c14879SStephen McConnell 	 * isn't perfect because the command could have already completed
1199a2c14879SStephen McConnell 	 * and been re-used, though this is unlikely.
1200a2c14879SStephen McConnell 	 */
1201a2c14879SStephen McConnell 	mpr_intr_locked(sc);
1202a2c14879SStephen McConnell 	if (cm->cm_state == MPR_CM_STATE_FREE) {
12037a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_INFO, "%s ATA ID command almost timed out\n",
12047a2a6a1aSStephen McConnell 		    __func__);
1205a2c14879SStephen McConnell 		return;
1206a2c14879SStephen McConnell 	}
1207a2c14879SStephen McConnell 
1208a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "ATA ID command timeout cm %p\n", cm);
1209a2c14879SStephen McConnell 
1210a2c14879SStephen McConnell 	/*
12117a2a6a1aSStephen McConnell 	 * Send wakeup() to the sleeping thread that issued this ATA ID command.
12127a2a6a1aSStephen McConnell 	 * wakeup() will cause msleep to return a 0 (not EWOULDBLOCK), and this
12137a2a6a1aSStephen McConnell 	 * will keep reinit() from being called. This way, an Abort Task TM can
12147a2a6a1aSStephen McConnell 	 * be issued so that the timed out command can be cleared. The Abort
12157a2a6a1aSStephen McConnell 	 * Task cannot be sent from here because the driver has not completed
12167a2a6a1aSStephen McConnell 	 * setting up targets.  Instead, the command is flagged so that special
12177a2a6a1aSStephen McConnell 	 * handling will be used to send the abort.
1218a2c14879SStephen McConnell 	 */
1219a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1220a2c14879SStephen McConnell 	wakeup(cm);
1221a2c14879SStephen McConnell }
1222a2c14879SStephen McConnell 
1223991554f2SKenneth D. Merry static int
122467feec50SStephen McConnell mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
122567feec50SStephen McConnell {
122667feec50SStephen McConnell 	char devstring[80];
122767feec50SStephen McConnell 	struct mprsas_softc *sassc;
122867feec50SStephen McConnell 	struct mprsas_target *targ;
122967feec50SStephen McConnell 	Mpi2ConfigReply_t mpi_reply;
123067feec50SStephen McConnell 	Mpi26PCIeDevicePage0_t config_page;
123167feec50SStephen McConnell 	Mpi26PCIeDevicePage2_t config_page2;
123267feec50SStephen McConnell 	uint64_t pcie_wwid, parent_wwid = 0;
123367feec50SStephen McConnell 	u32 device_info, parent_devinfo = 0;
123467feec50SStephen McConnell 	unsigned int id;
123567feec50SStephen McConnell 	int error = 0;
123667feec50SStephen McConnell 	struct mprsas_lun *lun;
123767feec50SStephen McConnell 
123867feec50SStephen McConnell 	sassc = sc->sassc;
123967feec50SStephen McConnell 	mprsas_startup_increment(sassc);
124067feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
124167feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
124267feec50SStephen McConnell 		printf("%s: error reading PCIe device page0\n", __func__);
124367feec50SStephen McConnell 		error = ENXIO;
124467feec50SStephen McConnell 		goto out;
124567feec50SStephen McConnell 	}
124667feec50SStephen McConnell 
124767feec50SStephen McConnell 	device_info = le32toh(config_page.DeviceInfo);
124867feec50SStephen McConnell 
124967feec50SStephen McConnell 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
125067feec50SStephen McConnell 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
125167feec50SStephen McConnell 		Mpi2ConfigReply_t tmp_mpi_reply;
125267feec50SStephen McConnell 		Mpi26PCIeDevicePage0_t parent_config_page;
125367feec50SStephen McConnell 
125467feec50SStephen McConnell 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
125567feec50SStephen McConnell 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
125667feec50SStephen McConnell 		     le16toh(config_page.ParentDevHandle)))) {
125767feec50SStephen McConnell 			printf("%s: error reading PCIe device %#x page0\n",
125867feec50SStephen McConnell 			    __func__, le16toh(config_page.ParentDevHandle));
125967feec50SStephen McConnell 		} else {
126067feec50SStephen McConnell 			parent_wwid = parent_config_page.WWID.High;
126167feec50SStephen McConnell 			parent_wwid = (parent_wwid << 32) |
126267feec50SStephen McConnell 			    parent_config_page.WWID.Low;
126367feec50SStephen McConnell 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
126467feec50SStephen McConnell 		}
126567feec50SStephen McConnell 	}
126667feec50SStephen McConnell 	/* TODO Check proper endianness */
126767feec50SStephen McConnell 	pcie_wwid = config_page.WWID.High;
126867feec50SStephen McConnell 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
126967feec50SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
127067feec50SStephen McConnell 	    pcie_wwid);
127167feec50SStephen McConnell 
127267feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
127367feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
127467feec50SStephen McConnell 		printf("%s: error reading PCIe device page2\n", __func__);
127567feec50SStephen McConnell 		error = ENXIO;
127667feec50SStephen McConnell 		goto out;
127767feec50SStephen McConnell 	}
127867feec50SStephen McConnell 
1279327f2e6cSStephen McConnell 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
128067feec50SStephen McConnell 	if (id == MPR_MAP_BAD_ID) {
1281327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1282327f2e6cSStephen McConnell 		    "Could not get ID for device with handle 0x%04x\n",
1283327f2e6cSStephen McConnell 		    __FILE__, __LINE__, __func__, handle);
128467feec50SStephen McConnell 		error = ENXIO;
128567feec50SStephen McConnell 		goto out;
128667feec50SStephen McConnell 	}
1287327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1288327f2e6cSStephen McConnell 	    __func__, id);
128967feec50SStephen McConnell 
129067feec50SStephen McConnell 	if (mprsas_check_id(sassc, id) != 0) {
129167feec50SStephen McConnell 		device_printf(sc->mpr_dev, "Excluding target id %d\n", id);
129267feec50SStephen McConnell 		error = ENXIO;
129367feec50SStephen McConnell 		goto out;
129467feec50SStephen McConnell 	}
129567feec50SStephen McConnell 
129667feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
129767feec50SStephen McConnell 	    pcie_wwid);
129867feec50SStephen McConnell 	targ = &sassc->targets[id];
129967feec50SStephen McConnell 	targ->devinfo = device_info;
130067feec50SStephen McConnell 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
130167feec50SStephen McConnell 	targ->encl_slot = le16toh(config_page.Slot);
130267feec50SStephen McConnell 	targ->encl_level = config_page.EnclosureLevel;
130367feec50SStephen McConnell 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
130467feec50SStephen McConnell 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
130567feec50SStephen McConnell 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
130667feec50SStephen McConnell 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
130767feec50SStephen McConnell 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
130867feec50SStephen McConnell 	targ->MDTS = config_page2.MaximumDataTransferSize;
130967feec50SStephen McConnell 	/*
131067feec50SStephen McConnell 	 * Assume always TRUE for encl_level_valid because there is no valid
131167feec50SStephen McConnell 	 * flag for PCIe.
131267feec50SStephen McConnell 	 */
131367feec50SStephen McConnell 	targ->encl_level_valid = TRUE;
131467feec50SStephen McConnell 	targ->handle = handle;
131567feec50SStephen McConnell 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
131667feec50SStephen McConnell 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
131767feec50SStephen McConnell 	targ->parent_sasaddr = le64toh(parent_wwid);
131867feec50SStephen McConnell 	targ->parent_devinfo = parent_devinfo;
131967feec50SStephen McConnell 	targ->tid = id;
132067feec50SStephen McConnell 	targ->linkrate = linkrate;
132167feec50SStephen McConnell 	targ->flags = 0;
132267feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
132367feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
132467feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
132567feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
132667feec50SStephen McConnell 		targ->scsi_req_desc_type =
132767feec50SStephen McConnell 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
132867feec50SStephen McConnell 	}
132967feec50SStephen McConnell 	TAILQ_INIT(&targ->commands);
133067feec50SStephen McConnell 	TAILQ_INIT(&targ->timedout_commands);
133167feec50SStephen McConnell 	while (!SLIST_EMPTY(&targ->luns)) {
133267feec50SStephen McConnell 		lun = SLIST_FIRST(&targ->luns);
133367feec50SStephen McConnell 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
133467feec50SStephen McConnell 		free(lun, M_MPR);
133567feec50SStephen McConnell 	}
133667feec50SStephen McConnell 	SLIST_INIT(&targ->luns);
133767feec50SStephen McConnell 
133867feec50SStephen McConnell 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
133967feec50SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
134067feec50SStephen McConnell 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
134167feec50SStephen McConnell 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
134267feec50SStephen McConnell 	    targ->handle, targ->encl_handle, targ->encl_slot);
134367feec50SStephen McConnell 	if (targ->encl_level_valid) {
134467feec50SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
134567feec50SStephen McConnell 		    "and connector name (%4s)\n", targ->encl_level,
134667feec50SStephen McConnell 		    targ->connector_name);
134767feec50SStephen McConnell 	}
134867feec50SStephen McConnell #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
134967feec50SStephen McConnell     (__FreeBSD_version < 902502)
135067feec50SStephen McConnell 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
135167feec50SStephen McConnell #endif
135267feec50SStephen McConnell 		mprsas_rescan_target(sc, targ);
135367feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
135467feec50SStephen McConnell 
135567feec50SStephen McConnell out:
135667feec50SStephen McConnell 	mprsas_startup_decrement(sassc);
135767feec50SStephen McConnell 	return (error);
135867feec50SStephen McConnell }
135967feec50SStephen McConnell 
136067feec50SStephen McConnell static int
1361991554f2SKenneth D. Merry mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1362991554f2SKenneth D. Merry {
1363991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1364991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1365991554f2SKenneth D. Merry 	u64 wwid;
1366991554f2SKenneth D. Merry 	unsigned int id;
1367991554f2SKenneth D. Merry 	int error = 0;
1368991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1369991554f2SKenneth D. Merry 
1370991554f2SKenneth D. Merry 	sassc = sc->sassc;
1371991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
1372991554f2SKenneth D. Merry 	/* wwid is endian safe */
1373991554f2SKenneth D. Merry 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1374991554f2SKenneth D. Merry 	if (!wwid) {
1375991554f2SKenneth D. Merry 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1376991554f2SKenneth D. Merry 		    __func__);
1377991554f2SKenneth D. Merry 		error = ENXIO;
1378991554f2SKenneth D. Merry 		goto out;
1379991554f2SKenneth D. Merry 	}
1380991554f2SKenneth D. Merry 
1381327f2e6cSStephen McConnell 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1382991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
1383991554f2SKenneth D. Merry 		printf("%s: could not get ID for volume with handle 0x%04x and "
1384991554f2SKenneth D. Merry 		    "WWID 0x%016llx\n", __func__, handle,
1385991554f2SKenneth D. Merry 		    (unsigned long long)wwid);
1386991554f2SKenneth D. Merry 		error = ENXIO;
1387991554f2SKenneth D. Merry 		goto out;
1388991554f2SKenneth D. Merry 	}
1389991554f2SKenneth D. Merry 
1390991554f2SKenneth D. Merry 	targ = &sassc->targets[id];
1391991554f2SKenneth D. Merry 	targ->tid = id;
1392991554f2SKenneth D. Merry 	targ->handle = handle;
1393991554f2SKenneth D. Merry 	targ->devname = wwid;
1394991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
1395991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
1396991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
1397991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
1398991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1399991554f2SKenneth D. Merry 		free(lun, M_MPR);
1400991554f2SKenneth D. Merry 	}
1401991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
1402a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
1403a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
1404991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
1405991554f2SKenneth D. Merry #endif
1406991554f2SKenneth D. Merry 		mprsas_rescan_target(sc, targ);
1407991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1408991554f2SKenneth D. Merry 	    targ->tid, wwid);
1409991554f2SKenneth D. Merry out:
1410991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1411991554f2SKenneth D. Merry 	return (error);
1412991554f2SKenneth D. Merry }
1413991554f2SKenneth D. Merry 
1414991554f2SKenneth D. Merry /**
1415991554f2SKenneth D. Merry  * mprsas_SSU_to_SATA_devices
1416991554f2SKenneth D. Merry  * @sc: per adapter object
1417991554f2SKenneth D. Merry  *
1418991554f2SKenneth D. Merry  * Looks through the target list and issues a StartStopUnit SCSI command to each
1419991554f2SKenneth D. Merry  * SATA direct-access device.  This helps to ensure that data corruption is
1420991554f2SKenneth D. Merry  * avoided when the system is being shut down.  This must be called after the IR
1421991554f2SKenneth D. Merry  * System Shutdown RAID Action is sent if in IR mode.
1422991554f2SKenneth D. Merry  *
1423991554f2SKenneth D. Merry  * Return nothing.
1424991554f2SKenneth D. Merry  */
1425991554f2SKenneth D. Merry static void
1426991554f2SKenneth D. Merry mprsas_SSU_to_SATA_devices(struct mpr_softc *sc)
1427991554f2SKenneth D. Merry {
1428991554f2SKenneth D. Merry 	struct mprsas_softc *sassc = sc->sassc;
1429991554f2SKenneth D. Merry 	union ccb *ccb;
1430991554f2SKenneth D. Merry 	path_id_t pathid = cam_sim_path(sassc->sim);
1431991554f2SKenneth D. Merry 	target_id_t targetid;
1432991554f2SKenneth D. Merry 	struct mprsas_target *target;
1433991554f2SKenneth D. Merry 	char path_str[64];
1434991554f2SKenneth D. Merry 	struct timeval cur_time, start_time;
1435991554f2SKenneth D. Merry 
143663941e98SKenneth D. Merry 	mpr_lock(sc);
143763941e98SKenneth D. Merry 
1438991554f2SKenneth D. Merry 	/*
1439a2c14879SStephen McConnell 	 * For each target, issue a StartStopUnit command to stop the device.
1440991554f2SKenneth D. Merry 	 */
1441991554f2SKenneth D. Merry 	sc->SSU_started = TRUE;
1442991554f2SKenneth D. Merry 	sc->SSU_refcount = 0;
1443327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1444991554f2SKenneth D. Merry 		target = &sassc->targets[targetid];
1445991554f2SKenneth D. Merry 		if (target->handle == 0x0) {
1446991554f2SKenneth D. Merry 			continue;
1447991554f2SKenneth D. Merry 		}
1448991554f2SKenneth D. Merry 
1449fa699bb2SAlan Somers 		/*
1450fa699bb2SAlan Somers 		 * The stop_at_shutdown flag will be set if this device is
1451fa699bb2SAlan Somers 		 * a SATA direct-access end device.
1452fa699bb2SAlan Somers 		 */
1453fa699bb2SAlan Somers 		if (target->stop_at_shutdown) {
1454991554f2SKenneth D. Merry 			ccb = xpt_alloc_ccb_nowait();
1455991554f2SKenneth D. Merry 			if (ccb == NULL) {
145667feec50SStephen McConnell 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
145767feec50SStephen McConnell 				    "to stop unit.\n");
1458991554f2SKenneth D. Merry 				return;
1459991554f2SKenneth D. Merry 			}
1460991554f2SKenneth D. Merry 
1461a2c14879SStephen McConnell 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1462a2c14879SStephen McConnell 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1463a2c14879SStephen McConnell 			    CAM_REQ_CMP) {
1464a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1465a2c14879SStephen McConnell 				    "path to stop unit.\n");
1466991554f2SKenneth D. Merry 				xpt_free_ccb(ccb);
1467991554f2SKenneth D. Merry 				return;
1468991554f2SKenneth D. Merry 			}
1469991554f2SKenneth D. Merry 			xpt_path_string(ccb->ccb_h.path, path_str,
1470991554f2SKenneth D. Merry 			    sizeof(path_str));
1471991554f2SKenneth D. Merry 
1472a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1473a2c14879SStephen McConnell 			    "handle %d\n", path_str, target->handle);
1474991554f2SKenneth D. Merry 
1475991554f2SKenneth D. Merry 			/*
1476a2c14879SStephen McConnell 			 * Issue a START STOP UNIT command for the target.
1477a2c14879SStephen McConnell 			 * Increment the SSU counter to be used to count the
1478a2c14879SStephen McConnell 			 * number of required replies.
1479991554f2SKenneth D. Merry 			 */
1480a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1481991554f2SKenneth D. Merry 			sc->SSU_refcount++;
1482991554f2SKenneth D. Merry 			ccb->ccb_h.target_id =
1483991554f2SKenneth D. Merry 			    xpt_path_target_id(ccb->ccb_h.path);
1484991554f2SKenneth D. Merry 			ccb->ccb_h.ppriv_ptr1 = sassc;
1485991554f2SKenneth D. Merry 			scsi_start_stop(&ccb->csio,
1486991554f2SKenneth D. Merry 			    /*retries*/0,
1487991554f2SKenneth D. Merry 			    mprsas_stop_unit_done,
1488991554f2SKenneth D. Merry 			    MSG_SIMPLE_Q_TAG,
1489991554f2SKenneth D. Merry 			    /*start*/FALSE,
1490991554f2SKenneth D. Merry 			    /*load/eject*/0,
1491991554f2SKenneth D. Merry 			    /*immediate*/FALSE,
1492991554f2SKenneth D. Merry 			    MPR_SENSE_LEN,
1493991554f2SKenneth D. Merry 			    /*timeout*/10000);
1494991554f2SKenneth D. Merry 			xpt_action(ccb);
1495991554f2SKenneth D. Merry 		}
1496991554f2SKenneth D. Merry 	}
1497991554f2SKenneth D. Merry 
149863941e98SKenneth D. Merry 	mpr_unlock(sc);
149963941e98SKenneth D. Merry 
1500991554f2SKenneth D. Merry 	/*
1501991554f2SKenneth D. Merry 	 * Wait until all of the SSU commands have completed or time has
1502a2c14879SStephen McConnell 	 * expired (60 seconds).  Pause for 100ms each time through.  If any
1503991554f2SKenneth D. Merry 	 * command times out, the target will be reset in the SCSI command
1504991554f2SKenneth D. Merry 	 * timeout routine.
1505991554f2SKenneth D. Merry 	 */
1506991554f2SKenneth D. Merry 	getmicrotime(&start_time);
1507991554f2SKenneth D. Merry 	while (sc->SSU_refcount) {
1508991554f2SKenneth D. Merry 		pause("mprwait", hz/10);
1509991554f2SKenneth D. Merry 
1510991554f2SKenneth D. Merry 		getmicrotime(&cur_time);
1511991554f2SKenneth D. Merry 		if ((cur_time.tv_sec - start_time.tv_sec) > 60) {
1512a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1513991554f2SKenneth D. Merry 			    "for SSU commands to complete.\n");
1514991554f2SKenneth D. Merry 			break;
1515991554f2SKenneth D. Merry 		}
1516991554f2SKenneth D. Merry 	}
1517991554f2SKenneth D. Merry }
1518991554f2SKenneth D. Merry 
1519991554f2SKenneth D. Merry static void
1520991554f2SKenneth D. Merry mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1521991554f2SKenneth D. Merry {
1522991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1523991554f2SKenneth D. Merry 	char path_str[64];
1524991554f2SKenneth D. Merry 
15255d21655fSStephen McConnell 	if (done_ccb == NULL)
15265d21655fSStephen McConnell 		return;
15275d21655fSStephen McConnell 
1528991554f2SKenneth D. Merry 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1529991554f2SKenneth D. Merry 
1530991554f2SKenneth D. Merry 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1531991554f2SKenneth D. Merry 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1532991554f2SKenneth D. Merry 	    path_str);
1533991554f2SKenneth D. Merry 
1534991554f2SKenneth D. Merry 	/*
1535991554f2SKenneth D. Merry 	 * Nothing more to do except free the CCB and path.  If the command
1536991554f2SKenneth D. Merry 	 * timed out, an abort reset, then target reset will be issued during
1537991554f2SKenneth D. Merry 	 * the SCSI Command process.
1538991554f2SKenneth D. Merry 	 */
1539991554f2SKenneth D. Merry 	xpt_free_path(done_ccb->ccb_h.path);
1540991554f2SKenneth D. Merry 	xpt_free_ccb(done_ccb);
1541991554f2SKenneth D. Merry }
1542991554f2SKenneth D. Merry 
1543991554f2SKenneth D. Merry /**
1544991554f2SKenneth D. Merry  * mprsas_ir_shutdown - IR shutdown notification
1545991554f2SKenneth D. Merry  * @sc: per adapter object
1546991554f2SKenneth D. Merry  *
1547991554f2SKenneth D. Merry  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1548991554f2SKenneth D. Merry  * the host system is shutting down.
1549991554f2SKenneth D. Merry  *
1550991554f2SKenneth D. Merry  * Return nothing.
1551991554f2SKenneth D. Merry  */
1552991554f2SKenneth D. Merry void
1553991554f2SKenneth D. Merry mprsas_ir_shutdown(struct mpr_softc *sc)
1554991554f2SKenneth D. Merry {
1555991554f2SKenneth D. Merry 	u16 volume_mapping_flags;
1556991554f2SKenneth D. Merry 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1557991554f2SKenneth D. Merry 	struct dev_mapping_table *mt_entry;
1558991554f2SKenneth D. Merry 	u32 start_idx, end_idx;
1559991554f2SKenneth D. Merry 	unsigned int id, found_volume = 0;
1560991554f2SKenneth D. Merry 	struct mpr_command *cm;
1561991554f2SKenneth D. Merry 	Mpi2RaidActionRequest_t	*action;
1562a2c14879SStephen McConnell 	target_id_t targetid;
1563a2c14879SStephen McConnell 	struct mprsas_target *target;
1564991554f2SKenneth D. Merry 
1565991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1566991554f2SKenneth D. Merry 
1567991554f2SKenneth D. Merry 	/* is IR firmware build loaded? */
1568991554f2SKenneth D. Merry 	if (!sc->ir_firmware)
1569991554f2SKenneth D. Merry 		goto out;
1570991554f2SKenneth D. Merry 
1571991554f2SKenneth D. Merry 	/* are there any volumes?  Look at IR target IDs. */
1572991554f2SKenneth D. Merry 	// TODO-later, this should be looked up in the RAID config structure
1573991554f2SKenneth D. Merry 	// when it is implemented.
1574991554f2SKenneth D. Merry 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1575991554f2SKenneth D. Merry 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1576991554f2SKenneth D. Merry 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1577991554f2SKenneth D. Merry 		start_idx = 0;
1578991554f2SKenneth D. Merry 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1579991554f2SKenneth D. Merry 			start_idx = 1;
1580991554f2SKenneth D. Merry 	} else
1581991554f2SKenneth D. Merry 		start_idx = sc->max_devices - sc->max_volumes;
1582991554f2SKenneth D. Merry 	end_idx = start_idx + sc->max_volumes - 1;
1583991554f2SKenneth D. Merry 
1584991554f2SKenneth D. Merry 	for (id = start_idx; id < end_idx; id++) {
1585991554f2SKenneth D. Merry 		mt_entry = &sc->mapping_table[id];
1586991554f2SKenneth D. Merry 		if ((mt_entry->physical_id != 0) &&
1587991554f2SKenneth D. Merry 		    (mt_entry->missing_count == 0)) {
1588991554f2SKenneth D. Merry 			found_volume = 1;
1589991554f2SKenneth D. Merry 			break;
1590991554f2SKenneth D. Merry 		}
1591991554f2SKenneth D. Merry 	}
1592991554f2SKenneth D. Merry 
1593991554f2SKenneth D. Merry 	if (!found_volume)
1594991554f2SKenneth D. Merry 		goto out;
1595991554f2SKenneth D. Merry 
1596991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1597991554f2SKenneth D. Merry 		printf("%s: command alloc failed\n", __func__);
1598991554f2SKenneth D. Merry 		goto out;
1599991554f2SKenneth D. Merry 	}
1600991554f2SKenneth D. Merry 
1601991554f2SKenneth D. Merry 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1602991554f2SKenneth D. Merry 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1603991554f2SKenneth D. Merry 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1604991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1605991554f2SKenneth D. Merry 	mpr_lock(sc);
1606991554f2SKenneth D. Merry 	mpr_wait_command(sc, cm, 5, CAN_SLEEP);
1607991554f2SKenneth D. Merry 	mpr_unlock(sc);
1608991554f2SKenneth D. Merry 
1609991554f2SKenneth D. Merry 	/*
1610991554f2SKenneth D. Merry 	 * Don't check for reply, just leave.
1611991554f2SKenneth D. Merry 	 */
1612991554f2SKenneth D. Merry 	if (cm)
1613991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1614991554f2SKenneth D. Merry 
1615991554f2SKenneth D. Merry out:
1616a2c14879SStephen McConnell 	/*
1617a2c14879SStephen McConnell 	 * All of the targets must have the correct value set for
1618a2c14879SStephen McConnell 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1619a2c14879SStephen McConnell 	 *
1620a2c14879SStephen McConnell 	 * The possible values for the 'enable_ssu' variable are:
1621a2c14879SStephen McConnell 	 * 0: disable to SSD and HDD
1622a2c14879SStephen McConnell 	 * 1: disable only to HDD (default)
1623a2c14879SStephen McConnell 	 * 2: disable only to SSD
1624a2c14879SStephen McConnell 	 * 3: enable to SSD and HDD
1625a2c14879SStephen McConnell 	 * anything else will default to 1.
1626a2c14879SStephen McConnell 	 */
1627327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1628a2c14879SStephen McConnell 		target = &sc->sassc->targets[targetid];
1629a2c14879SStephen McConnell 		if (target->handle == 0x0) {
1630a2c14879SStephen McConnell 			continue;
1631a2c14879SStephen McConnell 		}
1632a2c14879SStephen McConnell 
1633a2c14879SStephen McConnell 		if (target->supports_SSU) {
1634a2c14879SStephen McConnell 			switch (sc->enable_ssu) {
1635a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1636a2c14879SStephen McConnell 				target->stop_at_shutdown = FALSE;
1637a2c14879SStephen McConnell 				break;
1638a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1639a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1640a2c14879SStephen McConnell 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1641a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1642a2c14879SStephen McConnell 				}
1643a2c14879SStephen McConnell 				break;
1644a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1645a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1646a2c14879SStephen McConnell 				break;
1647a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1648a2c14879SStephen McConnell 			default:
1649a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1650a2c14879SStephen McConnell 				if ((target->flags &
1651a2c14879SStephen McConnell 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1652a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1653a2c14879SStephen McConnell 				}
1654a2c14879SStephen McConnell 				break;
1655a2c14879SStephen McConnell 			}
1656a2c14879SStephen McConnell 		}
1657a2c14879SStephen McConnell 	}
1658991554f2SKenneth D. Merry 	mprsas_SSU_to_SATA_devices(sc);
1659991554f2SKenneth D. Merry }
1660