xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 71900a794da046ad5322caae2774aed5b3d361b9)
1991554f2SKenneth D. Merry /*-
2a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
37a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
446b23587SKashyap D Desai  * Copyright 2000-2020 Broadcom Inc.
5991554f2SKenneth D. Merry  * All rights reserved.
6991554f2SKenneth D. Merry  *
7991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
8991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
9991554f2SKenneth D. Merry  * are met:
10991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
11991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
12991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
13991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
14991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
15991554f2SKenneth D. Merry  *
16991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26991554f2SKenneth D. Merry  * SUCH DAMAGE.
27991554f2SKenneth D. Merry  *
2846b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
29991554f2SKenneth D. Merry  */
30991554f2SKenneth D. Merry 
31991554f2SKenneth D. Merry #include <sys/cdefs.h>
32991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
33991554f2SKenneth D. Merry 
347a2a6a1aSStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
35991554f2SKenneth D. Merry 
36991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
37991554f2SKenneth D. Merry #include <sys/types.h>
38991554f2SKenneth D. Merry #include <sys/param.h>
39991554f2SKenneth D. Merry #include <sys/systm.h>
40991554f2SKenneth D. Merry #include <sys/kernel.h>
41991554f2SKenneth D. Merry #include <sys/selinfo.h>
42991554f2SKenneth D. Merry #include <sys/module.h>
43991554f2SKenneth D. Merry #include <sys/bus.h>
44991554f2SKenneth D. Merry #include <sys/conf.h>
45991554f2SKenneth D. Merry #include <sys/bio.h>
46991554f2SKenneth D. Merry #include <sys/malloc.h>
47991554f2SKenneth D. Merry #include <sys/uio.h>
48991554f2SKenneth D. Merry #include <sys/sysctl.h>
49991554f2SKenneth D. Merry #include <sys/endian.h>
50acc173a6SWarner Losh #include <sys/proc.h>
51991554f2SKenneth D. Merry #include <sys/queue.h>
52991554f2SKenneth D. Merry #include <sys/kthread.h>
53991554f2SKenneth D. Merry #include <sys/taskqueue.h>
54991554f2SKenneth D. Merry #include <sys/sbuf.h>
55acc173a6SWarner Losh #include <sys/reboot.h>
56991554f2SKenneth D. Merry 
57991554f2SKenneth D. Merry #include <machine/bus.h>
58991554f2SKenneth D. Merry #include <machine/resource.h>
59991554f2SKenneth D. Merry #include <sys/rman.h>
60991554f2SKenneth D. Merry 
61991554f2SKenneth D. Merry #include <machine/stdarg.h>
62991554f2SKenneth D. Merry 
63991554f2SKenneth D. Merry #include <cam/cam.h>
64991554f2SKenneth D. Merry #include <cam/cam_ccb.h>
65991554f2SKenneth D. Merry #include <cam/cam_debug.h>
66991554f2SKenneth D. Merry #include <cam/cam_sim.h>
67991554f2SKenneth D. Merry #include <cam/cam_xpt_sim.h>
68991554f2SKenneth D. Merry #include <cam/cam_xpt_periph.h>
69991554f2SKenneth D. Merry #include <cam/cam_periph.h>
70991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
71991554f2SKenneth D. Merry #include <cam/scsi/scsi_message.h>
72991554f2SKenneth D. Merry 
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
74991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
75991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7767feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_raid.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
82991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
83991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
84991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
85991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
86991554f2SKenneth D. Merry 
87991554f2SKenneth D. Merry /* For Hashed SAS Address creation for SATA Drives */
88991554f2SKenneth D. Merry #define MPT2SAS_SN_LEN 20
89991554f2SKenneth D. Merry #define MPT2SAS_MN_LEN 40
90991554f2SKenneth D. Merry 
91991554f2SKenneth D. Merry struct mpr_fw_event_work {
92991554f2SKenneth D. Merry 	u16			event;
93991554f2SKenneth D. Merry 	void			*event_data;
94991554f2SKenneth D. Merry 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
95991554f2SKenneth D. Merry };
96991554f2SKenneth D. Merry 
97991554f2SKenneth D. Merry union _sata_sas_address {
98991554f2SKenneth D. Merry 	u8 wwid[8];
99991554f2SKenneth D. Merry 	struct {
100991554f2SKenneth D. Merry 		u32 high;
101991554f2SKenneth D. Merry 		u32 low;
102991554f2SKenneth D. Merry 	} word;
103991554f2SKenneth D. Merry };
104991554f2SKenneth D. Merry 
105991554f2SKenneth D. Merry /*
106991554f2SKenneth D. Merry  * define the IDENTIFY DEVICE structure
107991554f2SKenneth D. Merry  */
108991554f2SKenneth D. Merry struct _ata_identify_device_data {
109991554f2SKenneth D. Merry 	u16 reserved1[10];	/* 0-9 */
110991554f2SKenneth D. Merry 	u16 serial_number[10];	/* 10-19 */
111991554f2SKenneth D. Merry 	u16 reserved2[7];	/* 20-26 */
112991554f2SKenneth D. Merry 	u16 model_number[20];	/* 27-46*/
113a2c14879SStephen McConnell 	u16 reserved3[170];	/* 47-216 */
114a2c14879SStephen McConnell 	u16 rotational_speed;	/* 217 */
115a2c14879SStephen McConnell 	u16 reserved4[38];	/* 218-255 */
116991554f2SKenneth D. Merry };
117991554f2SKenneth D. Merry static u32 event_count;
118991554f2SKenneth D. Merry static void mprsas_fw_work(struct mpr_softc *sc,
119991554f2SKenneth D. Merry     struct mpr_fw_event_work *fw_event);
120991554f2SKenneth D. Merry static void mprsas_fw_event_free(struct mpr_softc *,
121991554f2SKenneth D. Merry     struct mpr_fw_event_work *);
122991554f2SKenneth D. Merry static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
12367feec50SStephen McConnell static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
12467feec50SStephen McConnell     u8 linkrate);
125991554f2SKenneth D. Merry static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
126991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
127991554f2SKenneth D. Merry     u32 devinfo);
12886312e46SConrad Meyer static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
129991554f2SKenneth D. Merry int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
130a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
131991554f2SKenneth D. Merry static int mprsas_volume_add(struct mpr_softc *sc,
132991554f2SKenneth D. Merry     u16 handle);
133acc173a6SWarner Losh static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
134991554f2SKenneth D. Merry static void mprsas_stop_unit_done(struct cam_periph *periph,
135991554f2SKenneth D. Merry     union ccb *done_ccb);
136991554f2SKenneth D. Merry 
137991554f2SKenneth D. Merry void
138991554f2SKenneth D. Merry mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
139991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
140991554f2SKenneth D. Merry {
141991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
142991554f2SKenneth D. Merry 	u16 sz;
143991554f2SKenneth D. Merry 
144991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
145055e2653SScott Long 	MPR_DPRINT_EVENT(sc, sas, event);
146991554f2SKenneth D. Merry 	mprsas_record_event(sc, event);
147991554f2SKenneth D. Merry 
148991554f2SKenneth D. Merry 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
149991554f2SKenneth D. Merry 	     M_ZERO|M_NOWAIT);
150991554f2SKenneth D. Merry 	if (!fw_event) {
151991554f2SKenneth D. Merry 		printf("%s: allocate failed for fw_event\n", __func__);
152991554f2SKenneth D. Merry 		return;
153991554f2SKenneth D. Merry 	}
154991554f2SKenneth D. Merry 	sz = le16toh(event->EventDataLength) * 4;
155991554f2SKenneth D. Merry 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
156991554f2SKenneth D. Merry 	if (!fw_event->event_data) {
157991554f2SKenneth D. Merry 		printf("%s: allocate failed for event_data\n", __func__);
158991554f2SKenneth D. Merry 		free(fw_event, M_MPR);
159991554f2SKenneth D. Merry 		return;
160991554f2SKenneth D. Merry 	}
161991554f2SKenneth D. Merry 
162991554f2SKenneth D. Merry 	bcopy(event->EventData, fw_event->event_data, sz);
163*71900a79SAlfredo Dal'Ava Junior 	fw_event->event = le16toh(event->Event);
164*71900a79SAlfredo Dal'Ava Junior 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
165*71900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
166*71900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
167*71900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
168991554f2SKenneth D. Merry 	    sc->track_mapping_events)
169991554f2SKenneth D. Merry 		sc->pending_map_events++;
170991554f2SKenneth D. Merry 
171991554f2SKenneth D. Merry 	/*
172991554f2SKenneth D. Merry 	 * When wait_for_port_enable flag is set, make sure that all the events
173991554f2SKenneth D. Merry 	 * are processed. Increment the startup_refcount and decrement it after
174991554f2SKenneth D. Merry 	 * events are processed.
175991554f2SKenneth D. Merry 	 */
176*71900a79SAlfredo Dal'Ava Junior 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
177*71900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
178*71900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
179991554f2SKenneth D. Merry 	    sc->wait_for_port_enable)
180991554f2SKenneth D. Merry 		mprsas_startup_increment(sc->sassc);
181991554f2SKenneth D. Merry 
182991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
183991554f2SKenneth D. Merry 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
184991554f2SKenneth D. Merry }
185991554f2SKenneth D. Merry 
186991554f2SKenneth D. Merry static void
187991554f2SKenneth D. Merry mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
188991554f2SKenneth D. Merry {
189991554f2SKenneth D. Merry 
190991554f2SKenneth D. Merry 	free(fw_event->event_data, M_MPR);
191991554f2SKenneth D. Merry 	free(fw_event, M_MPR);
192991554f2SKenneth D. Merry }
193991554f2SKenneth D. Merry 
194991554f2SKenneth D. Merry /**
195991554f2SKenneth D. Merry  * _mpr_fw_work - delayed task for processing firmware events
196991554f2SKenneth D. Merry  * @sc: per adapter object
197991554f2SKenneth D. Merry  * @fw_event: The fw_event_work object
198991554f2SKenneth D. Merry  * Context: user.
199991554f2SKenneth D. Merry  *
200991554f2SKenneth D. Merry  * Return nothing.
201991554f2SKenneth D. Merry  */
202991554f2SKenneth D. Merry static void
203991554f2SKenneth D. Merry mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
204991554f2SKenneth D. Merry {
205991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
206991554f2SKenneth D. Merry 	sassc = sc->sassc;
207991554f2SKenneth D. Merry 
208991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
209991554f2SKenneth D. Merry 	    event_count++, __func__, fw_event->event);
210991554f2SKenneth D. Merry 	switch (fw_event->event) {
211991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
212991554f2SKenneth D. Merry 	{
213991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
214991554f2SKenneth D. Merry 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
21567feec50SStephen McConnell 		uint8_t i;
216991554f2SKenneth D. Merry 
217991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
218991554f2SKenneth D. Merry 		    fw_event->event_data;
219991554f2SKenneth D. Merry 
220991554f2SKenneth D. Merry 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
221991554f2SKenneth D. Merry 
222991554f2SKenneth D. Merry 		for (i = 0; i < data->NumEntries; i++) {
223991554f2SKenneth D. Merry 			phy = &data->PHY[i];
224991554f2SKenneth D. Merry 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
225991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
226991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
227991554f2SKenneth D. Merry 				    le16toh(phy->AttachedDevHandle),
228991554f2SKenneth D. Merry 				    phy->LinkRate)) {
229327f2e6cSStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: "
230327f2e6cSStephen McConnell 					    "failed to add device with handle "
231327f2e6cSStephen McConnell 					    "0x%x\n", __func__,
232991554f2SKenneth D. Merry 					    le16toh(phy->AttachedDevHandle));
233991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc, le16toh(
234991554f2SKenneth D. Merry 					    phy->AttachedDevHandle));
235991554f2SKenneth D. Merry 				}
236991554f2SKenneth D. Merry 				break;
237991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
238991554f2SKenneth D. Merry 				mprsas_prepare_remove(sassc, le16toh(
239991554f2SKenneth D. Merry 				    phy->AttachedDevHandle));
240991554f2SKenneth D. Merry 				break;
241991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
242991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
243991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
244991554f2SKenneth D. Merry 			default:
245991554f2SKenneth D. Merry 				break;
246991554f2SKenneth D. Merry 			}
247991554f2SKenneth D. Merry 		}
248991554f2SKenneth D. Merry 		/*
249991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
250991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
251991554f2SKenneth D. Merry 		 * been processed.
252991554f2SKenneth D. Merry 		 */
253991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
254991554f2SKenneth D. Merry 		break;
255991554f2SKenneth D. Merry 	}
256991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DISCOVERY:
257991554f2SKenneth D. Merry 	{
258991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
259991554f2SKenneth D. Merry 
260991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
261991554f2SKenneth D. Merry 
262991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
2637a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
264991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
265991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
266991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
267991554f2SKenneth D. Merry 			mprsas_discovery_end(sassc);
268991554f2SKenneth D. Merry 		}
269991554f2SKenneth D. Merry 		break;
270991554f2SKenneth D. Merry 	}
271991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
272991554f2SKenneth D. Merry 	{
273991554f2SKenneth D. Merry 		Mpi2EventDataSasEnclDevStatusChange_t *data;
274991554f2SKenneth D. Merry 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
275991554f2SKenneth D. Merry 		    fw_event->event_data;
276991554f2SKenneth D. Merry 		mpr_mapping_enclosure_dev_status_change_event(sc,
277991554f2SKenneth D. Merry 		    fw_event->event_data);
278991554f2SKenneth D. Merry 		break;
279991554f2SKenneth D. Merry 	}
280991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
281991554f2SKenneth D. Merry 	{
282991554f2SKenneth D. Merry 		Mpi2EventIrConfigElement_t *element;
283991554f2SKenneth D. Merry 		int i;
284991554f2SKenneth D. Merry 		u8 foreign_config, reason;
285991554f2SKenneth D. Merry 		u16 elementType;
286991554f2SKenneth D. Merry 		Mpi2EventDataIrConfigChangeList_t *event_data;
287991554f2SKenneth D. Merry 		struct mprsas_target *targ;
288991554f2SKenneth D. Merry 		unsigned int id;
289991554f2SKenneth D. Merry 
290991554f2SKenneth D. Merry 		event_data = fw_event->event_data;
291991554f2SKenneth D. Merry 		foreign_config = (le32toh(event_data->Flags) &
292991554f2SKenneth D. Merry 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
293991554f2SKenneth D. Merry 
294991554f2SKenneth D. Merry 		element =
295991554f2SKenneth D. Merry 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
296327f2e6cSStephen McConnell 		id = mpr_mapping_get_raid_tid_from_handle(sc,
297991554f2SKenneth D. Merry 		    element->VolDevHandle);
298991554f2SKenneth D. Merry 
299991554f2SKenneth D. Merry 		mpr_mapping_ir_config_change_event(sc, event_data);
300991554f2SKenneth D. Merry 		for (i = 0; i < event_data->NumElements; i++, element++) {
301991554f2SKenneth D. Merry 			reason = element->ReasonCode;
302991554f2SKenneth D. Merry 			elementType = le16toh(element->ElementFlags) &
303991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
304991554f2SKenneth D. Merry 			/*
305991554f2SKenneth D. Merry 			 * check for element type of Phys Disk or Hot Spare
306991554f2SKenneth D. Merry 			 */
307991554f2SKenneth D. Merry 			if ((elementType !=
308991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
309991554f2SKenneth D. Merry 			    && (elementType !=
310991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
311991554f2SKenneth D. Merry 				// do next element
312991554f2SKenneth D. Merry 				goto skip_fp_send;
313991554f2SKenneth D. Merry 
314991554f2SKenneth D. Merry 			/*
315991554f2SKenneth D. Merry 			 * check for reason of Hide, Unhide, PD Created, or PD
316991554f2SKenneth D. Merry 			 * Deleted
317991554f2SKenneth D. Merry 			 */
318991554f2SKenneth D. Merry 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
319991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
320991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
321991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
322991554f2SKenneth D. Merry 				goto skip_fp_send;
323991554f2SKenneth D. Merry 
324991554f2SKenneth D. Merry 			// check for a reason of Hide or PD Created
325991554f2SKenneth D. Merry 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
326991554f2SKenneth D. Merry 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
327991554f2SKenneth D. Merry 			{
328991554f2SKenneth D. Merry 				// build RAID Action message
329991554f2SKenneth D. Merry 				Mpi2RaidActionRequest_t	*action;
3306d4ffcb4SKenneth D. Merry 				Mpi2RaidActionReply_t *reply = NULL;
331991554f2SKenneth D. Merry 				struct mpr_command *cm;
332991554f2SKenneth D. Merry 				int error = 0;
333991554f2SKenneth D. Merry 				if ((cm = mpr_alloc_command(sc)) == NULL) {
334991554f2SKenneth D. Merry 					printf("%s: command alloc failed\n",
335991554f2SKenneth D. Merry 					    __func__);
336991554f2SKenneth D. Merry 					return;
337991554f2SKenneth D. Merry 				}
338991554f2SKenneth D. Merry 
339a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
340991554f2SKenneth D. Merry 				    "from "
341991554f2SKenneth D. Merry 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
342991554f2SKenneth D. Merry 				    ":\n");
343991554f2SKenneth D. Merry 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
344991554f2SKenneth D. Merry 				action->Function = MPI2_FUNCTION_RAID_ACTION;
345991554f2SKenneth D. Merry 				action->Action =
346991554f2SKenneth D. Merry 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
347991554f2SKenneth D. Merry 				action->PhysDiskNum = element->PhysDiskNum;
348991554f2SKenneth D. Merry 				cm->cm_desc.Default.RequestFlags =
349991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3506d4ffcb4SKenneth D. Merry 				error = mpr_request_polled(sc, &cm);
3516d4ffcb4SKenneth D. Merry 				if (cm != NULL)
3526d4ffcb4SKenneth D. Merry 					reply = (Mpi2RaidActionReply_t *)
3536d4ffcb4SKenneth D. Merry 					    cm->cm_reply;
354991554f2SKenneth D. Merry 				if (error || (reply == NULL)) {
355991554f2SKenneth D. Merry 					/* FIXME */
356991554f2SKenneth D. Merry 					/*
357991554f2SKenneth D. Merry 					 * If the poll returns error then we
358991554f2SKenneth D. Merry 					 * need to do diag reset
359991554f2SKenneth D. Merry 					 */
360991554f2SKenneth D. Merry 					printf("%s: poll for page completed "
361991554f2SKenneth D. Merry 					    "with error %d", __func__, error);
362991554f2SKenneth D. Merry 				}
363991554f2SKenneth D. Merry 				if (reply && (le16toh(reply->IOCStatus) &
364991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_MASK) !=
365991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_SUCCESS) {
366a2c14879SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: error "
367a2c14879SStephen McConnell 					    "sending RaidActionPage; "
368a2c14879SStephen McConnell 					    "iocstatus = 0x%x\n", __func__,
369991554f2SKenneth D. Merry 					    le16toh(reply->IOCStatus));
370991554f2SKenneth D. Merry 				}
371991554f2SKenneth D. Merry 
372991554f2SKenneth D. Merry 				if (cm)
373991554f2SKenneth D. Merry 					mpr_free_command(sc, cm);
374991554f2SKenneth D. Merry 			}
375991554f2SKenneth D. Merry skip_fp_send:
376a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_EVENT, "Received "
377991554f2SKenneth D. Merry 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
378991554f2SKenneth D. Merry 			    "code %x:\n", element->ReasonCode);
379991554f2SKenneth D. Merry 			switch (element->ReasonCode) {
380991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
381991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
382991554f2SKenneth D. Merry 				if (!foreign_config) {
383991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
384991554f2SKenneth D. Merry 					    le16toh(element->VolDevHandle))) {
385991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
386991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
387991554f2SKenneth D. Merry 						    __func__, le16toh(element->
388991554f2SKenneth D. Merry 						    VolDevHandle));
389991554f2SKenneth D. Merry 					}
390991554f2SKenneth D. Merry 				}
391991554f2SKenneth D. Merry 				break;
392991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
393991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
394991554f2SKenneth D. Merry 				/*
395991554f2SKenneth D. Merry 				 * Rescan after volume is deleted or removed.
396991554f2SKenneth D. Merry 				 */
397991554f2SKenneth D. Merry 				if (!foreign_config) {
398991554f2SKenneth D. Merry 					if (id == MPR_MAP_BAD_ID) {
399991554f2SKenneth D. Merry 						printf("%s: could not get ID "
400991554f2SKenneth D. Merry 						    "for volume with handle "
401991554f2SKenneth D. Merry 						    "0x%04x\n", __func__,
402991554f2SKenneth D. Merry 						    le16toh(element->
403991554f2SKenneth D. Merry 						    VolDevHandle));
404991554f2SKenneth D. Merry 						break;
405991554f2SKenneth D. Merry 					}
406991554f2SKenneth D. Merry 
407991554f2SKenneth D. Merry 					targ = &sassc->targets[id];
408991554f2SKenneth D. Merry 					targ->handle = 0x0;
409991554f2SKenneth D. Merry 					targ->encl_slot = 0x0;
410991554f2SKenneth D. Merry 					targ->encl_handle = 0x0;
411991554f2SKenneth D. Merry 					targ->encl_level_valid = 0x0;
412991554f2SKenneth D. Merry 					targ->encl_level = 0x0;
413991554f2SKenneth D. Merry 					targ->connector_name[0] = ' ';
414991554f2SKenneth D. Merry 					targ->connector_name[1] = ' ';
415991554f2SKenneth D. Merry 					targ->connector_name[2] = ' ';
416991554f2SKenneth D. Merry 					targ->connector_name[3] = ' ';
417991554f2SKenneth D. Merry 					targ->exp_dev_handle = 0x0;
418991554f2SKenneth D. Merry 					targ->phy_num = 0x0;
419991554f2SKenneth D. Merry 					targ->linkrate = 0x0;
420991554f2SKenneth D. Merry 					mprsas_rescan_target(sc, targ);
421991554f2SKenneth D. Merry 					printf("RAID target id 0x%x removed\n",
422991554f2SKenneth D. Merry 					    targ->tid);
423991554f2SKenneth D. Merry 				}
424991554f2SKenneth D. Merry 				break;
425991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
426991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
427991554f2SKenneth D. Merry 				/*
428991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been created.  Hide
429991554f2SKenneth D. Merry 				 * it from the OS.
430991554f2SKenneth D. Merry 				 */
431991554f2SKenneth D. Merry 				targ = mprsas_find_target_by_handle(sassc, 0,
432991554f2SKenneth D. Merry 				    element->PhysDiskDevHandle);
433991554f2SKenneth D. Merry 				if (targ == NULL)
434991554f2SKenneth D. Merry 					break;
435991554f2SKenneth D. Merry 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
436991554f2SKenneth D. Merry 				mprsas_rescan_target(sc, targ);
437991554f2SKenneth D. Merry 				break;
438991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
439991554f2SKenneth D. Merry 				/*
440991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been deleted.
441991554f2SKenneth D. Merry 				 * Expose it to the OS.
442991554f2SKenneth D. Merry 				 */
443991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
444991554f2SKenneth D. Merry 				    le16toh(element->PhysDiskDevHandle), 0)) {
445991554f2SKenneth D. Merry 					printf("%s: failed to add device with "
446991554f2SKenneth D. Merry 					    "handle 0x%x\n", __func__,
447991554f2SKenneth D. Merry 					    le16toh(element->
448991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
449991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc,
450991554f2SKenneth D. Merry 					    le16toh(element->
451991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
452991554f2SKenneth D. Merry 				}
453991554f2SKenneth D. Merry 				break;
454991554f2SKenneth D. Merry 			}
455991554f2SKenneth D. Merry 		}
456991554f2SKenneth D. Merry 		/*
457991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
458991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
459991554f2SKenneth D. Merry 		 * been processed.
460991554f2SKenneth D. Merry 		 */
461991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
462991554f2SKenneth D. Merry 		break;
463991554f2SKenneth D. Merry 	}
464991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_VOLUME:
465991554f2SKenneth D. Merry 	{
466991554f2SKenneth D. Merry 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
467991554f2SKenneth D. Merry 
468991554f2SKenneth D. Merry 		/*
469991554f2SKenneth D. Merry 		 * Informational only.
470991554f2SKenneth D. Merry 		 */
471991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
472991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
473991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
474991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
475991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
476991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
477991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
478991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
479991554f2SKenneth D. Merry 			break;
480991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
481991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
482991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
483991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
484991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
485991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
486991554f2SKenneth D. Merry 			break;
487991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
488991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
489991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
490991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
491991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
492991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
493991554f2SKenneth D. Merry 				u32 state;
494991554f2SKenneth D. Merry 				struct mprsas_target *targ;
495991554f2SKenneth D. Merry 				state = le32toh(event_data->NewValue);
496991554f2SKenneth D. Merry 				switch (state) {
497991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_MISSING:
498991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_FAILED:
499991554f2SKenneth D. Merry 					mprsas_prepare_volume_remove(sassc,
500991554f2SKenneth D. Merry 					    event_data->VolDevHandle);
501991554f2SKenneth D. Merry 					break;
502991554f2SKenneth D. Merry 
503991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_ONLINE:
504991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_DEGRADED:
505991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_OPTIMAL:
506991554f2SKenneth D. Merry 					targ =
507991554f2SKenneth D. Merry 					    mprsas_find_target_by_handle(sassc,
508991554f2SKenneth D. Merry 					    0, event_data->VolDevHandle);
509991554f2SKenneth D. Merry 					if (targ) {
510991554f2SKenneth D. Merry 						printf("%s %d: Volume handle "
511991554f2SKenneth D. Merry 						    "0x%x is already added \n",
512991554f2SKenneth D. Merry 						    __func__, __LINE__,
513991554f2SKenneth D. Merry 						    event_data->VolDevHandle);
514991554f2SKenneth D. Merry 						break;
515991554f2SKenneth D. Merry 					}
516991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
517991554f2SKenneth D. Merry 					    le16toh(event_data->
518991554f2SKenneth D. Merry 					    VolDevHandle))) {
519991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
520991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
521991554f2SKenneth D. Merry 						    __func__, le16toh(
522991554f2SKenneth D. Merry 						    event_data->VolDevHandle));
523991554f2SKenneth D. Merry 					}
524991554f2SKenneth D. Merry 					break;
525991554f2SKenneth D. Merry 				default:
526991554f2SKenneth D. Merry 					break;
527991554f2SKenneth D. Merry 				}
528991554f2SKenneth D. Merry 			break;
529991554f2SKenneth D. Merry 		default:
530991554f2SKenneth D. Merry 			break;
531991554f2SKenneth D. Merry 		}
532991554f2SKenneth D. Merry 		break;
533991554f2SKenneth D. Merry 	}
534991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_PHYSICAL_DISK:
535991554f2SKenneth D. Merry 	{
536991554f2SKenneth D. Merry 		Mpi2EventDataIrPhysicalDisk_t *event_data =
537991554f2SKenneth D. Merry 		    fw_event->event_data;
538991554f2SKenneth D. Merry 		struct mprsas_target *targ;
539991554f2SKenneth D. Merry 
540991554f2SKenneth D. Merry 		/*
541991554f2SKenneth D. Merry 		 * Informational only.
542991554f2SKenneth D. Merry 		 */
543991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
544991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
545991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
546991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
547991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Phys Disk Number "
548991554f2SKenneth D. Merry   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
549991554f2SKenneth D. Merry  			    "%d", le32toh(event_data->PreviousValue),
550991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
551991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
552991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
553991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
554991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
555991554f2SKenneth D. Merry 			break;
556991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
557991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
558991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
559991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
560991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
561991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
562991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
563991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
564991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
565991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
566991554f2SKenneth D. Merry 			break;
567991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
568991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
569991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
570991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
571991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
572991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
573991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
574991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
575991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
576991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
577991554f2SKenneth D. Merry 			switch (event_data->NewValue) {
578991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_ONLINE:
579991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_DEGRADED:
580991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_REBUILDING:
581991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OPTIMAL:
582991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_HOT_SPARE:
583991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
584991554f2SKenneth D. Merry 					    sassc, 0,
585991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
586991554f2SKenneth D. Merry 					if (targ) {
587991554f2SKenneth D. Merry 						targ->flags |=
588991554f2SKenneth D. Merry 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
589991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
590991554f2SKenneth D. Merry 						    "for handle 0x%x.\n",
591991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
592991554f2SKenneth D. Merry 						    event_data->
593991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
594991554f2SKenneth D. Merry 					}
595991554f2SKenneth D. Merry 				break;
596991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OFFLINE:
597991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
598991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
599991554f2SKenneth D. Merry 				default:
600991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
601991554f2SKenneth D. Merry 					    sassc, 0,
602991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
603991554f2SKenneth D. Merry 					if (targ) {
604991554f2SKenneth D. Merry 						targ->flags |=
605991554f2SKenneth D. Merry 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
606991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
607991554f2SKenneth D. Merry 						    "for handle 0x%x.  \n",
608991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
609991554f2SKenneth D. Merry 						    event_data->
610991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
611991554f2SKenneth D. Merry 					}
612991554f2SKenneth D. Merry 				break;
613991554f2SKenneth D. Merry 			}
614991554f2SKenneth D. Merry 		default:
615991554f2SKenneth D. Merry 			break;
616991554f2SKenneth D. Merry 		}
617991554f2SKenneth D. Merry 		break;
618991554f2SKenneth D. Merry 	}
619991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_OPERATION_STATUS:
620991554f2SKenneth D. Merry 	{
621991554f2SKenneth D. Merry 		Mpi2EventDataIrOperationStatus_t *event_data =
622991554f2SKenneth D. Merry 		    fw_event->event_data;
623991554f2SKenneth D. Merry 
624991554f2SKenneth D. Merry 		/*
625991554f2SKenneth D. Merry 		 * Informational only.
626991554f2SKenneth D. Merry 		 */
627991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
628991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
629991554f2SKenneth D. Merry 		    "percent complete for Volume with handle 0x%x",
630991554f2SKenneth D. Merry 		    event_data->RAIDOperation, event_data->PercentComplete,
631991554f2SKenneth D. Merry 		    le16toh(event_data->VolDevHandle));
632991554f2SKenneth D. Merry 		break;
633991554f2SKenneth D. Merry 	}
634991554f2SKenneth D. Merry 	case MPI2_EVENT_TEMP_THRESHOLD:
635991554f2SKenneth D. Merry 	{
636991554f2SKenneth D. Merry 		pMpi2EventDataTemperature_t	temp_event;
637991554f2SKenneth D. Merry 
638991554f2SKenneth D. Merry 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
639991554f2SKenneth D. Merry 
640991554f2SKenneth D. Merry 		/*
641991554f2SKenneth D. Merry 		 * The Temp Sensor Count must be greater than the event's Sensor
642991554f2SKenneth D. Merry 		 * Num to be valid.  If valid, print the temp thresholds that
643991554f2SKenneth D. Merry 		 * have been exceeded.
644991554f2SKenneth D. Merry 		 */
645991554f2SKenneth D. Merry 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
646991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
647991554f2SKenneth D. Merry 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
648991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
649991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
650991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
651991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
652991554f2SKenneth D. Merry 			    temp_event->SensorNum);
653991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
654991554f2SKenneth D. Merry 			    "%d\n", temp_event->CurrentTemperature);
655991554f2SKenneth D. Merry 		}
656991554f2SKenneth D. Merry 		break;
657991554f2SKenneth D. Merry 	}
6582bbc5fcbSStephen McConnell 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
6592bbc5fcbSStephen McConnell 	{
6602bbc5fcbSStephen McConnell 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
6612bbc5fcbSStephen McConnell 		ace_event_data =
6622bbc5fcbSStephen McConnell 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
6632bbc5fcbSStephen McConnell 
6640656476aSAlexander Motin 		switch(ace_event_data->ReasonCode) {
6650656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
6660656476aSAlexander Motin 		{
6670656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6682bbc5fcbSStephen McConnell 			    "ReceptacleID %d cannot be powered and device "
6692bbc5fcbSStephen McConnell 			    "connected to this active cable will not be seen. "
6702bbc5fcbSStephen McConnell 			    "This active cable requires %d mW of power.\n",
6712bbc5fcbSStephen McConnell 			    ace_event_data->ReceptacleID,
6722bbc5fcbSStephen McConnell 			    ace_event_data->ActiveCablePowerRequirement);
6730656476aSAlexander Motin 			break;
6740656476aSAlexander Motin 		}
6750656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
6760656476aSAlexander Motin 		{
6770656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6780656476aSAlexander Motin 			    "ReceptacleID %d is not running at optimal speed "
6790656476aSAlexander Motin 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
6800656476aSAlexander Motin 			break;
6810656476aSAlexander Motin 		}
6820656476aSAlexander Motin 		default:
6830656476aSAlexander Motin 			break;
6842bbc5fcbSStephen McConnell 		}
6852bbc5fcbSStephen McConnell 		break;
6862bbc5fcbSStephen McConnell 	}
68789d1c21fSKashyap D Desai 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
68889d1c21fSKashyap D Desai 	{
68989d1c21fSKashyap D Desai 		pMpi26EventDataPCIeDeviceStatusChange_t	pcie_status_event_data;
69089d1c21fSKashyap D Desai 		pcie_status_event_data =
69189d1c21fSKashyap D Desai 		   (pMpi26EventDataPCIeDeviceStatusChange_t)fw_event->event_data;
69289d1c21fSKashyap D Desai 
69389d1c21fSKashyap D Desai 		switch (pcie_status_event_data->ReasonCode) {
69489d1c21fSKashyap D Desai 		case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
69589d1c21fSKashyap D Desai 		{
69689d1c21fSKashyap D Desai 			mpr_printf(sc, "PCIe Host Reset failed on DevHandle "
69789d1c21fSKashyap D Desai 			    "0x%x\n", pcie_status_event_data->DevHandle);
69889d1c21fSKashyap D Desai 			break;
69989d1c21fSKashyap D Desai 		}
70089d1c21fSKashyap D Desai 		default:
70189d1c21fSKashyap D Desai 			break;
70289d1c21fSKashyap D Desai 		}
70389d1c21fSKashyap D Desai 		break;
70489d1c21fSKashyap D Desai 	}
7055f5baf0eSAlexander Motin 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
7065f5baf0eSAlexander Motin 	{
7075f5baf0eSAlexander Motin 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
7085f5baf0eSAlexander Motin 		uint64_t sas_address;
7095f5baf0eSAlexander Motin 
7105f5baf0eSAlexander Motin 		discovery_error_data =
7115f5baf0eSAlexander Motin 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
7125f5baf0eSAlexander Motin 		    fw_event->event_data;
7135f5baf0eSAlexander Motin 
7145f5baf0eSAlexander Motin 		sas_address = discovery_error_data->SASAddress.High;
7155f5baf0eSAlexander Motin 		sas_address = (sas_address << 32) |
7165f5baf0eSAlexander Motin 		    discovery_error_data->SASAddress.Low;
7175f5baf0eSAlexander Motin 
7185f5baf0eSAlexander Motin 		switch(discovery_error_data->ReasonCode) {
7195f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
7205f5baf0eSAlexander Motin 		{
7215f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command failed during discovery "
7225f5baf0eSAlexander Motin 			    "for expander with SAS Address %jx and "
7235f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7245f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7255f5baf0eSAlexander Motin 			break;
7265f5baf0eSAlexander Motin 		}
7275f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
7285f5baf0eSAlexander Motin 		{
7295f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command timed out during "
7305f5baf0eSAlexander Motin 			    "discovery for expander with SAS Address %jx and "
7315f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7325f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7335f5baf0eSAlexander Motin 			break;
7345f5baf0eSAlexander Motin 		}
7355f5baf0eSAlexander Motin 		default:
7365f5baf0eSAlexander Motin 			break;
7375f5baf0eSAlexander Motin 		}
7385f5baf0eSAlexander Motin 		break;
7395f5baf0eSAlexander Motin 	}
74067feec50SStephen McConnell 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
74167feec50SStephen McConnell 	{
74267feec50SStephen McConnell 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
74367feec50SStephen McConnell 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
74467feec50SStephen McConnell 		uint8_t i, link_rate;
74567feec50SStephen McConnell 		uint16_t handle;
74667feec50SStephen McConnell 
74767feec50SStephen McConnell 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
74867feec50SStephen McConnell 		    fw_event->event_data;
74967feec50SStephen McConnell 
75067feec50SStephen McConnell 		mpr_mapping_pcie_topology_change_event(sc,
75167feec50SStephen McConnell 		    fw_event->event_data);
75267feec50SStephen McConnell 
75367feec50SStephen McConnell 		for (i = 0; i < data->NumEntries; i++) {
75467feec50SStephen McConnell 			port_entry = &data->PortEntry[i];
75567feec50SStephen McConnell 			handle = le16toh(port_entry->AttachedDevHandle);
75667feec50SStephen McConnell 			link_rate = port_entry->CurrentPortInfo &
75767feec50SStephen McConnell 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
75867feec50SStephen McConnell 			switch (port_entry->PortStatus) {
75967feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
76067feec50SStephen McConnell 				if (link_rate <
76167feec50SStephen McConnell 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
76267feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
76367feec50SStephen McConnell 					    "add PCIe device with handle 0x%x "
76467feec50SStephen McConnell 					    "with unknown link rate.\n",
76567feec50SStephen McConnell 					    __func__, handle);
76667feec50SStephen McConnell 					break;
76767feec50SStephen McConnell 				}
76867feec50SStephen McConnell 				if (mprsas_add_pcie_device(sc, handle,
76967feec50SStephen McConnell 				    link_rate)) {
77067feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
77167feec50SStephen McConnell 					    "to add PCIe device with handle "
77267feec50SStephen McConnell 					    "0x%x\n", __func__, handle);
77367feec50SStephen McConnell 					mprsas_prepare_remove(sassc, handle);
77467feec50SStephen McConnell 				}
77567feec50SStephen McConnell 				break;
77667feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
77767feec50SStephen McConnell 				mprsas_prepare_remove(sassc, handle);
77867feec50SStephen McConnell 				break;
77967feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
78067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
78167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
78267feec50SStephen McConnell 			default:
78367feec50SStephen McConnell 				break;
78467feec50SStephen McConnell 			}
78567feec50SStephen McConnell 		}
78667feec50SStephen McConnell 		/*
78767feec50SStephen McConnell 		 * refcount was incremented for this event in
78867feec50SStephen McConnell 		 * mprsas_evt_handler.  Decrement it here because the event has
78967feec50SStephen McConnell 		 * been processed.
79067feec50SStephen McConnell 		 */
79167feec50SStephen McConnell 		mprsas_startup_decrement(sassc);
79267feec50SStephen McConnell 		break;
79367feec50SStephen McConnell 	}
794991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
795991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
796991554f2SKenneth D. Merry 	default:
797991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
798991554f2SKenneth D. Merry 		    fw_event->event);
799991554f2SKenneth D. Merry 		break;
800991554f2SKenneth D. Merry 	}
801991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
802991554f2SKenneth D. Merry 	    __func__, fw_event->event);
803991554f2SKenneth D. Merry 	mprsas_fw_event_free(sc, fw_event);
804991554f2SKenneth D. Merry }
805991554f2SKenneth D. Merry 
806991554f2SKenneth D. Merry void
807991554f2SKenneth D. Merry mprsas_firmware_event_work(void *arg, int pending)
808991554f2SKenneth D. Merry {
809991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
810991554f2SKenneth D. Merry 	struct mpr_softc *sc;
811991554f2SKenneth D. Merry 
812991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
813991554f2SKenneth D. Merry 	mpr_lock(sc);
814991554f2SKenneth D. Merry 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
815991554f2SKenneth D. Merry 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
816991554f2SKenneth D. Merry 		mprsas_fw_work(sc, fw_event);
817991554f2SKenneth D. Merry 	}
818991554f2SKenneth D. Merry 	mpr_unlock(sc);
819991554f2SKenneth D. Merry }
820991554f2SKenneth D. Merry 
821991554f2SKenneth D. Merry static int
82267feec50SStephen McConnell mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
82367feec50SStephen McConnell {
824991554f2SKenneth D. Merry 	char devstring[80];
825991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
826991554f2SKenneth D. Merry 	struct mprsas_target *targ;
827991554f2SKenneth D. Merry 	Mpi2ConfigReply_t mpi_reply;
828991554f2SKenneth D. Merry 	Mpi2SasDevicePage0_t config_page;
829a2c14879SStephen McConnell 	uint64_t sas_address, parent_sas_address = 0;
830991554f2SKenneth D. Merry 	u32 device_info, parent_devinfo = 0;
831991554f2SKenneth D. Merry 	unsigned int id;
832a2c14879SStephen McConnell 	int ret = 1, error = 0, i;
833991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
834a2c14879SStephen McConnell 	u8 is_SATA_SSD = 0;
835a2c14879SStephen McConnell 	struct mpr_command *cm;
836991554f2SKenneth D. Merry 
837991554f2SKenneth D. Merry 	sassc = sc->sassc;
838991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
839aeb9ac0dSScott Long 	if (mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
840aeb9ac0dSScott Long 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
841aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
842aeb9ac0dSScott Long 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
843aeb9ac0dSScott Long 		    handle, mpi_reply.IOCStatus);
844991554f2SKenneth D. Merry 		error = ENXIO;
845991554f2SKenneth D. Merry 		goto out;
846991554f2SKenneth D. Merry 	}
847991554f2SKenneth D. Merry 
848991554f2SKenneth D. Merry 	device_info = le32toh(config_page.DeviceInfo);
849991554f2SKenneth D. Merry 
850991554f2SKenneth D. Merry 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
851991554f2SKenneth D. Merry 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
852991554f2SKenneth D. Merry 		Mpi2ConfigReply_t tmp_mpi_reply;
853991554f2SKenneth D. Merry 		Mpi2SasDevicePage0_t parent_config_page;
854991554f2SKenneth D. Merry 
855aeb9ac0dSScott Long 		if (mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
856991554f2SKenneth D. Merry 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
857aeb9ac0dSScott Long 		    le16toh(config_page.ParentDevHandle)) != 0) {
858757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_FAULT,
859aeb9ac0dSScott Long 			    "Error reading parent SAS device %#x page0, "
860aeb9ac0dSScott Long 			    "iocstatus= 0x%x\n",
861aeb9ac0dSScott Long 			    le16toh(config_page.ParentDevHandle),
862aeb9ac0dSScott Long 			    tmp_mpi_reply.IOCStatus);
863991554f2SKenneth D. Merry 		} else {
864991554f2SKenneth D. Merry 			parent_sas_address = parent_config_page.SASAddress.High;
865991554f2SKenneth D. Merry 			parent_sas_address = (parent_sas_address << 32) |
866991554f2SKenneth D. Merry 			    parent_config_page.SASAddress.Low;
867991554f2SKenneth D. Merry 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
868991554f2SKenneth D. Merry 		}
869991554f2SKenneth D. Merry 	}
870*71900a79SAlfredo Dal'Ava Junior 	sas_address = htole32(config_page.SASAddress.High);
871*71900a79SAlfredo Dal'Ava Junior 	sas_address = (sas_address << 32) | htole32(config_page.SASAddress.Low);
872757ff642SScott Long 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
873757ff642SScott Long 	    "page0 = %jx\n", handle, sas_address);
874991554f2SKenneth D. Merry 
875a2c14879SStephen McConnell 	/*
876a2c14879SStephen McConnell 	 * Always get SATA Identify information because this is used to
877a2c14879SStephen McConnell 	 * determine if Start/Stop Unit should be sent to the drive when the
878a2c14879SStephen McConnell 	 * system is shutdown.
879a2c14879SStephen McConnell 	 */
880991554f2SKenneth D. Merry 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
881a2c14879SStephen McConnell 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
882a2c14879SStephen McConnell 		    handle, device_info, &is_SATA_SSD);
883a2c14879SStephen McConnell 		if (ret) {
884757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
885757ff642SScott Long 			    "%s: failed to get disk type (SSD or HDD) for SATA "
886757ff642SScott Long 			    "device with handle 0x%04x\n",
887a2c14879SStephen McConnell 			    __func__, handle);
888a2c14879SStephen McConnell 		} else {
889757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
890757ff642SScott Long 			    "from SATA device = %jx\n", handle, sas_address);
891a2c14879SStephen McConnell 		}
892a2c14879SStephen McConnell 	}
893c91d307fSStephen McConnell 
8944ab1cdc5SScott Long 	/*
8954ab1cdc5SScott Long 	 * use_phynum:
8964ab1cdc5SScott Long 	 *  1 - use the PhyNum field as a fallback to the mapping logic
8974ab1cdc5SScott Long 	 *  0 - never use the PhyNum field
8984ab1cdc5SScott Long 	 * -1 - only use the PhyNum field
899327f2e6cSStephen McConnell 	 *
900327f2e6cSStephen McConnell 	 * Note that using the Phy number to map a device can cause device adds
901327f2e6cSStephen McConnell 	 * to fail if multiple enclosures/expanders are in the topology. For
902327f2e6cSStephen McConnell 	 * example, if two devices are in the same slot number in two different
903327f2e6cSStephen McConnell 	 * enclosures within the topology, only one of those devices will be
904327f2e6cSStephen McConnell 	 * added. PhyNum mapping should not be used if multiple enclosures are
905327f2e6cSStephen McConnell 	 * in the topology.
9064ab1cdc5SScott Long 	 */
9074ab1cdc5SScott Long 	id = MPR_MAP_BAD_ID;
9084ab1cdc5SScott Long 	if (sc->use_phynum != -1)
909327f2e6cSStephen McConnell 		id = mpr_mapping_get_tid(sc, sas_address, handle);
910991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
91167feec50SStephen McConnell 		if ((sc->use_phynum == 0) ||
91267feec50SStephen McConnell 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
9134ab1cdc5SScott Long 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
9144ab1cdc5SScott Long 			    "Could not get ID for device with handle 0x%04x\n",
9154ab1cdc5SScott Long 			    __FILE__, __LINE__, __func__, handle);
916991554f2SKenneth D. Merry 			error = ENXIO;
917991554f2SKenneth D. Merry 			goto out;
918991554f2SKenneth D. Merry 		}
9194ab1cdc5SScott Long 	}
920327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
921327f2e6cSStephen McConnell 	    __func__, id);
922991554f2SKenneth D. Merry 
923327f2e6cSStephen McConnell 	/*
924327f2e6cSStephen McConnell 	 * Only do the ID check and reuse check if the target is not from a
925327f2e6cSStephen McConnell 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
926327f2e6cSStephen McConnell 	 * when a volume is deleted because the mapping entry for the PD will
927327f2e6cSStephen McConnell 	 * still be in the mapping table. The ID check should not be done here
928327f2e6cSStephen McConnell 	 * either since this PD is already being used.
929327f2e6cSStephen McConnell 	 */
930327f2e6cSStephen McConnell 	targ = &sassc->targets[id];
931327f2e6cSStephen McConnell 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
932991554f2SKenneth D. Merry 		if (mprsas_check_id(sassc, id) != 0) {
933757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
934757ff642SScott Long 			    "Excluding target id %d\n", id);
935991554f2SKenneth D. Merry 			error = ENXIO;
936991554f2SKenneth D. Merry 			goto out;
937991554f2SKenneth D. Merry 		}
938991554f2SKenneth D. Merry 
9394ab1cdc5SScott Long 		if (targ->handle != 0x0) {
940327f2e6cSStephen McConnell 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
941327f2e6cSStephen McConnell 			    "target id %d handle 0x%04x\n", id, targ->handle);
9424ab1cdc5SScott Long 			error = ENXIO;
9434ab1cdc5SScott Long 			goto out;
9444ab1cdc5SScott Long 		}
945327f2e6cSStephen McConnell 	}
9464ab1cdc5SScott Long 
947991554f2SKenneth D. Merry 	targ->devinfo = device_info;
948991554f2SKenneth D. Merry 	targ->devname = le32toh(config_page.DeviceName.High);
949991554f2SKenneth D. Merry 	targ->devname = (targ->devname << 32) |
950991554f2SKenneth D. Merry 	    le32toh(config_page.DeviceName.Low);
951991554f2SKenneth D. Merry 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
952991554f2SKenneth D. Merry 	targ->encl_slot = le16toh(config_page.Slot);
953991554f2SKenneth D. Merry 	targ->encl_level = config_page.EnclosureLevel;
954991554f2SKenneth D. Merry 	targ->connector_name[0] = config_page.ConnectorName[0];
955991554f2SKenneth D. Merry 	targ->connector_name[1] = config_page.ConnectorName[1];
956991554f2SKenneth D. Merry 	targ->connector_name[2] = config_page.ConnectorName[2];
957991554f2SKenneth D. Merry 	targ->connector_name[3] = config_page.ConnectorName[3];
958991554f2SKenneth D. Merry 	targ->handle = handle;
959991554f2SKenneth D. Merry 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
960991554f2SKenneth D. Merry 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
961991554f2SKenneth D. Merry 	targ->parent_sasaddr = le64toh(parent_sas_address);
962991554f2SKenneth D. Merry 	targ->parent_devinfo = parent_devinfo;
963991554f2SKenneth D. Merry 	targ->tid = id;
964991554f2SKenneth D. Merry 	targ->linkrate = (linkrate>>4);
965991554f2SKenneth D. Merry 	targ->flags = 0;
966a2c14879SStephen McConnell 	if (is_SATA_SSD) {
967a2c14879SStephen McConnell 		targ->flags = MPR_TARGET_IS_SATA_SSD;
968a2c14879SStephen McConnell 	}
96967feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
97067feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
97167feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
97267feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
973991554f2SKenneth D. Merry 		targ->scsi_req_desc_type =
974991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
975991554f2SKenneth D. Merry 	}
976991554f2SKenneth D. Merry 	if (le16toh(config_page.Flags) &
977991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
978991554f2SKenneth D. Merry 		targ->encl_level_valid = TRUE;
979991554f2SKenneth D. Merry 	}
980991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
981991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
982991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
983991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
984991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
985991554f2SKenneth D. Merry 		free(lun, M_MPR);
986991554f2SKenneth D. Merry 	}
987991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
988991554f2SKenneth D. Merry 
989991554f2SKenneth D. Merry 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
990a2c14879SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
991991554f2SKenneth D. Merry 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
992991554f2SKenneth D. Merry 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
993991554f2SKenneth D. Merry 	    targ->handle, targ->encl_handle, targ->encl_slot);
994991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
995a2c14879SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
996991554f2SKenneth D. Merry 		    "and connector name (%4s)\n", targ->encl_level,
997991554f2SKenneth D. Merry 		    targ->connector_name);
998991554f2SKenneth D. Merry 	}
999991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
1000991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1001a2c14879SStephen McConnell 
1002a2c14879SStephen McConnell 	/*
1003a2c14879SStephen McConnell 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
1004a2c14879SStephen McConnell 	 * If so, send a Target Reset TM to the target that was just created.
1005a2c14879SStephen McConnell 	 * An Abort Task TM should be used instead of a Target Reset, but that
1006a2c14879SStephen McConnell 	 * would be much more difficult because targets have not been fully
1007a2c14879SStephen McConnell 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
1008a2c14879SStephen McConnell 	 * target instead of the LUN.
1009a2c14879SStephen McConnell 	 */
1010a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1011a2c14879SStephen McConnell 		cm = &sc->commands[i];
1012a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1013a2c14879SStephen McConnell 			targ->timeouts++;
10148fe7bf06SWarner Losh 			cm->cm_flags |= MPR_CM_FLAGS_TIMEDOUT;
1015a2c14879SStephen McConnell 
1016a2c14879SStephen McConnell 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1017a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1018a2c14879SStephen McConnell 				    "Reset for stuck SATA identify command "
1019a2c14879SStephen McConnell 				    "(cm = %p)\n", __func__, cm);
1020a2c14879SStephen McConnell 				targ->tm->cm_targ = targ;
1021a2c14879SStephen McConnell 				mprsas_send_reset(sc, targ->tm,
1022a2c14879SStephen McConnell 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1023a2c14879SStephen McConnell 			} else {
1024a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
10257a2a6a1aSStephen McConnell 				    "tm for Target Reset after SATA ID command "
10267a2a6a1aSStephen McConnell 				    "timed out (cm %p)\n", cm);
1027a2c14879SStephen McConnell 			}
1028a2c14879SStephen McConnell 			/*
1029a2c14879SStephen McConnell 			 * No need to check for more since the target is
1030a2c14879SStephen McConnell 			 * already being reset.
1031a2c14879SStephen McConnell 			 */
1032a2c14879SStephen McConnell 			break;
1033a2c14879SStephen McConnell 		}
1034a2c14879SStephen McConnell 	}
1035991554f2SKenneth D. Merry out:
1036a2c14879SStephen McConnell 	/*
1037a2c14879SStephen McConnell 	 * Free the commands that may not have been freed from the SATA ID call
1038a2c14879SStephen McConnell 	 */
1039a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1040a2c14879SStephen McConnell 		cm = &sc->commands[i];
1041a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
10428277ce2bSConrad Meyer 			free(cm->cm_data, M_MPR);
1043a2c14879SStephen McConnell 			mpr_free_command(sc, cm);
1044a2c14879SStephen McConnell 		}
1045a2c14879SStephen McConnell 	}
1046991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1047991554f2SKenneth D. Merry 	return (error);
1048991554f2SKenneth D. Merry }
1049991554f2SKenneth D. Merry 
1050991554f2SKenneth D. Merry int
1051991554f2SKenneth D. Merry mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1052a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1053991554f2SKenneth D. Merry {
1054991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t mpi_reply;
1055991554f2SKenneth D. Merry 	int i, rc, try_count;
1056991554f2SKenneth D. Merry 	u32 *bufferptr;
1057991554f2SKenneth D. Merry 	union _sata_sas_address hash_address;
1058991554f2SKenneth D. Merry 	struct _ata_identify_device_data ata_identify;
1059991554f2SKenneth D. Merry 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1060991554f2SKenneth D. Merry 	u32 ioc_status;
1061991554f2SKenneth D. Merry 	u8 sas_status;
1062991554f2SKenneth D. Merry 
1063991554f2SKenneth D. Merry 	memset(&ata_identify, 0, sizeof(ata_identify));
10644e02badbSAlan Somers 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1065991554f2SKenneth D. Merry 	try_count = 0;
1066991554f2SKenneth D. Merry 	do {
1067991554f2SKenneth D. Merry 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1068991554f2SKenneth D. Merry 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1069991554f2SKenneth D. Merry 		try_count++;
1070991554f2SKenneth D. Merry 		ioc_status = le16toh(mpi_reply.IOCStatus)
1071991554f2SKenneth D. Merry 		    & MPI2_IOCSTATUS_MASK;
1072991554f2SKenneth D. Merry 		sas_status = mpi_reply.SASStatus;
1073a92fe027SAlan Somers 		switch (ioc_status) {
1074a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SUCCESS:
1075a92fe027SAlan Somers 			break;
1076a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1077a92fe027SAlan Somers 			/* No sense sleeping.  this error won't get better */
1078a92fe027SAlan Somers 			break;
1079a92fe027SAlan Somers 		default:
1080a2c14879SStephen McConnell 			if (sc->spinup_wait_time > 0) {
1081a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1082a2c14879SStephen McConnell 				    "after SATA ID error to wait for spinup\n",
1083a2c14879SStephen McConnell 				    sc->spinup_wait_time);
1084a2c14879SStephen McConnell 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1085a2c14879SStephen McConnell 				    "mprid", sc->spinup_wait_time * hz);
1086a2c14879SStephen McConnell 			}
1087a2c14879SStephen McConnell 		}
1088a92fe027SAlan Somers 	} while (((rc && (rc != EWOULDBLOCK)) ||
10897a2a6a1aSStephen McConnell 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1090a92fe027SAlan Somers 	    || sas_status) && (try_count < 5));
1091991554f2SKenneth D. Merry 
1092991554f2SKenneth D. Merry 	if (rc == 0 && !ioc_status && !sas_status) {
1093991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1094991554f2SKenneth D. Merry 		    "successfully for handle = 0x%x with try_count = %d\n",
1095991554f2SKenneth D. Merry 		    __func__, handle, try_count);
1096991554f2SKenneth D. Merry 	} else {
1097991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1098991554f2SKenneth D. Merry 		    __func__, handle);
1099991554f2SKenneth D. Merry 		return -1;
1100991554f2SKenneth D. Merry 	}
1101991554f2SKenneth D. Merry 	/* Copy & byteswap the 40 byte model number to a buffer */
1102991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1103991554f2SKenneth D. Merry 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1104991554f2SKenneth D. Merry 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1105991554f2SKenneth D. Merry 	}
1106991554f2SKenneth D. Merry 	/* Copy & byteswap the 20 byte serial number to a buffer */
1107991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1108991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i] =
1109991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i + 1];
1110991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i + 1] =
1111991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i];
1112991554f2SKenneth D. Merry 	}
1113991554f2SKenneth D. Merry 	bufferptr = (u32 *)buffer;
1114991554f2SKenneth D. Merry 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1115991554f2SKenneth D. Merry 	 * so loop through the first 56 bytes (7*8),
1116991554f2SKenneth D. Merry 	 * and then add in the last dword.
1117991554f2SKenneth D. Merry 	 */
1118991554f2SKenneth D. Merry 	hash_address.word.low  = 0;
1119991554f2SKenneth D. Merry 	hash_address.word.high = 0;
1120991554f2SKenneth D. Merry 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1121991554f2SKenneth D. Merry 		hash_address.word.low += *bufferptr;
1122991554f2SKenneth D. Merry 		bufferptr++;
1123991554f2SKenneth D. Merry 		hash_address.word.high += *bufferptr;
1124991554f2SKenneth D. Merry 		bufferptr++;
1125991554f2SKenneth D. Merry 	}
1126991554f2SKenneth D. Merry 	/* Add the last dword */
1127991554f2SKenneth D. Merry 	hash_address.word.low += *bufferptr;
1128991554f2SKenneth D. Merry 	/* Make sure the hash doesn't start with 5, because it could clash
1129991554f2SKenneth D. Merry 	 * with a SAS address. Change 5 to a D.
1130991554f2SKenneth D. Merry 	 */
1131991554f2SKenneth D. Merry 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1132991554f2SKenneth D. Merry 		hash_address.word.high |= 0x00000080;
1133991554f2SKenneth D. Merry 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1134991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1135991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1136991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1137991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[7];
1138a2c14879SStephen McConnell 	if (ata_identify.rotational_speed == 1) {
1139a2c14879SStephen McConnell 		*is_SATA_SSD = 1;
1140a2c14879SStephen McConnell 	}
1141a2c14879SStephen McConnell 
1142991554f2SKenneth D. Merry 	return 0;
1143991554f2SKenneth D. Merry }
1144991554f2SKenneth D. Merry 
1145991554f2SKenneth D. Merry static int
1146991554f2SKenneth D. Merry mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1147991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1148991554f2SKenneth D. Merry {
1149991554f2SKenneth D. Merry 	Mpi2SataPassthroughRequest_t *mpi_request;
1150991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t *reply;
1151991554f2SKenneth D. Merry 	struct mpr_command *cm;
1152991554f2SKenneth D. Merry 	char *buffer;
1153991554f2SKenneth D. Merry 	int error = 0;
1154991554f2SKenneth D. Merry 
1155991554f2SKenneth D. Merry 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1156991554f2SKenneth D. Merry 	if (!buffer)
1157991554f2SKenneth D. Merry 		return ENOMEM;
1158991554f2SKenneth D. Merry 
1159991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1160991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1161991554f2SKenneth D. Merry 		return (EBUSY);
1162991554f2SKenneth D. Merry 	}
1163991554f2SKenneth D. Merry 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1164991554f2SKenneth D. Merry 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1165991554f2SKenneth D. Merry 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1166991554f2SKenneth D. Merry 	mpi_request->VF_ID = 0;
1167991554f2SKenneth D. Merry 	mpi_request->DevHandle = htole16(handle);
1168991554f2SKenneth D. Merry 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1169991554f2SKenneth D. Merry 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1170991554f2SKenneth D. Merry 	mpi_request->DataLength = htole32(sz);
1171991554f2SKenneth D. Merry 	mpi_request->CommandFIS[0] = 0x27;
1172991554f2SKenneth D. Merry 	mpi_request->CommandFIS[1] = 0x80;
1173991554f2SKenneth D. Merry 	mpi_request->CommandFIS[2] =  (devinfo &
1174991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1175991554f2SKenneth D. Merry 	cm->cm_sge = &mpi_request->SGL;
1176991554f2SKenneth D. Merry 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1177991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1178991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1179991554f2SKenneth D. Merry 	cm->cm_data = buffer;
1180991554f2SKenneth D. Merry 	cm->cm_length = htole32(sz);
1181a2c14879SStephen McConnell 
1182a2c14879SStephen McConnell 	/*
118386312e46SConrad Meyer 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
118486312e46SConrad Meyer 	 * This fixes a problem where the FW does not send a reply sometimes
1185a2c14879SStephen McConnell 	 * when a bad disk is in the topology. So, this is used to timeout the
1186a2c14879SStephen McConnell 	 * command so that processing can continue normally.
1187a2c14879SStephen McConnell 	 */
118886312e46SConrad Meyer 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1189a2c14879SStephen McConnell 
119086312e46SConrad Meyer 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
119186312e46SConrad Meyer 
119286312e46SConrad Meyer 	/* mprsas_ata_id_timeout does not reset controller */
119386312e46SConrad Meyer 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
119486312e46SConrad Meyer 
1195991554f2SKenneth D. Merry 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1196991554f2SKenneth D. Merry 	if (error || (reply == NULL)) {
1197991554f2SKenneth D. Merry 		/* FIXME */
1198991554f2SKenneth D. Merry 		/*
1199991554f2SKenneth D. Merry 		 * If the request returns an error then we need to do a diag
1200991554f2SKenneth D. Merry 		 * reset
1201991554f2SKenneth D. Merry 		 */
1202aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1203e7ef108cSWarner Losh 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
1204aeb9ac0dSScott Long 		    error);
1205991554f2SKenneth D. Merry 		error = ENXIO;
1206991554f2SKenneth D. Merry 		goto out;
1207991554f2SKenneth D. Merry 	}
1208991554f2SKenneth D. Merry 	bcopy(buffer, id_buffer, sz);
1209991554f2SKenneth D. Merry 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1210991554f2SKenneth D. Merry 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1211991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
1212aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1213aeb9ac0dSScott Long 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1214aeb9ac0dSScott Long 		    handle, reply->IOCStatus);
1215991554f2SKenneth D. Merry 		error = ENXIO;
1216991554f2SKenneth D. Merry 		goto out;
1217991554f2SKenneth D. Merry 	}
1218991554f2SKenneth D. Merry out:
1219a2c14879SStephen McConnell 	/*
1220a2c14879SStephen McConnell 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
12218277ce2bSConrad Meyer 	 * it.  The command and buffer will be freed after sending an Abort
122286312e46SConrad Meyer 	 * Task TM.
1223a2c14879SStephen McConnell 	 */
12248277ce2bSConrad Meyer 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1225991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1226991554f2SKenneth D. Merry 		free(buffer, M_MPR);
122786312e46SConrad Meyer 	}
1228991554f2SKenneth D. Merry 	return (error);
1229991554f2SKenneth D. Merry }
1230991554f2SKenneth D. Merry 
1231a2c14879SStephen McConnell static void
123286312e46SConrad Meyer mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1233a2c14879SStephen McConnell {
1234a2c14879SStephen McConnell 
123586312e46SConrad Meyer 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1236a2c14879SStephen McConnell 	    __func__, cm, sc);
1237a2c14879SStephen McConnell 
1238a2c14879SStephen McConnell 	/*
123986312e46SConrad Meyer 	 * The Abort Task cannot be sent from here because the driver has not
124086312e46SConrad Meyer 	 * completed setting up targets.  Instead, the command is flagged so
12418fe7bf06SWarner Losh 	 * that special handling will be used to send the abort. Now that
12428fe7bf06SWarner Losh 	 * this command has timed out, it's no longer in the queue.
1243a2c14879SStephen McConnell 	 */
1244a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
12458fe7bf06SWarner Losh 	cm->cm_state = MPR_CM_STATE_BUSY;
1246a2c14879SStephen McConnell }
1247a2c14879SStephen McConnell 
1248991554f2SKenneth D. Merry static int
124967feec50SStephen McConnell mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
125067feec50SStephen McConnell {
125167feec50SStephen McConnell 	char devstring[80];
125267feec50SStephen McConnell 	struct mprsas_softc *sassc;
125367feec50SStephen McConnell 	struct mprsas_target *targ;
125467feec50SStephen McConnell 	Mpi2ConfigReply_t mpi_reply;
125567feec50SStephen McConnell 	Mpi26PCIeDevicePage0_t config_page;
125667feec50SStephen McConnell 	Mpi26PCIeDevicePage2_t config_page2;
125767feec50SStephen McConnell 	uint64_t pcie_wwid, parent_wwid = 0;
125867feec50SStephen McConnell 	u32 device_info, parent_devinfo = 0;
125967feec50SStephen McConnell 	unsigned int id;
126067feec50SStephen McConnell 	int error = 0;
126167feec50SStephen McConnell 	struct mprsas_lun *lun;
126267feec50SStephen McConnell 
126367feec50SStephen McConnell 	sassc = sc->sassc;
126467feec50SStephen McConnell 	mprsas_startup_increment(sassc);
126567feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
126667feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
126767feec50SStephen McConnell 		printf("%s: error reading PCIe device page0\n", __func__);
126867feec50SStephen McConnell 		error = ENXIO;
126967feec50SStephen McConnell 		goto out;
127067feec50SStephen McConnell 	}
127167feec50SStephen McConnell 
127267feec50SStephen McConnell 	device_info = le32toh(config_page.DeviceInfo);
127367feec50SStephen McConnell 
127467feec50SStephen McConnell 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
127567feec50SStephen McConnell 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
127667feec50SStephen McConnell 		Mpi2ConfigReply_t tmp_mpi_reply;
127767feec50SStephen McConnell 		Mpi26PCIeDevicePage0_t parent_config_page;
127867feec50SStephen McConnell 
127967feec50SStephen McConnell 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
128067feec50SStephen McConnell 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
128167feec50SStephen McConnell 		     le16toh(config_page.ParentDevHandle)))) {
128267feec50SStephen McConnell 			printf("%s: error reading PCIe device %#x page0\n",
128367feec50SStephen McConnell 			    __func__, le16toh(config_page.ParentDevHandle));
128467feec50SStephen McConnell 		} else {
128567feec50SStephen McConnell 			parent_wwid = parent_config_page.WWID.High;
128667feec50SStephen McConnell 			parent_wwid = (parent_wwid << 32) |
128767feec50SStephen McConnell 			    parent_config_page.WWID.Low;
128867feec50SStephen McConnell 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
128967feec50SStephen McConnell 		}
129067feec50SStephen McConnell 	}
129167feec50SStephen McConnell 	/* TODO Check proper endianness */
129267feec50SStephen McConnell 	pcie_wwid = config_page.WWID.High;
129367feec50SStephen McConnell 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
129467feec50SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
129567feec50SStephen McConnell 	    pcie_wwid);
129667feec50SStephen McConnell 
129767feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
129867feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
129967feec50SStephen McConnell 		printf("%s: error reading PCIe device page2\n", __func__);
130067feec50SStephen McConnell 		error = ENXIO;
130167feec50SStephen McConnell 		goto out;
130267feec50SStephen McConnell 	}
130367feec50SStephen McConnell 
1304327f2e6cSStephen McConnell 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
130567feec50SStephen McConnell 	if (id == MPR_MAP_BAD_ID) {
1306327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1307327f2e6cSStephen McConnell 		    "Could not get ID for device with handle 0x%04x\n",
1308327f2e6cSStephen McConnell 		    __FILE__, __LINE__, __func__, handle);
130967feec50SStephen McConnell 		error = ENXIO;
131067feec50SStephen McConnell 		goto out;
131167feec50SStephen McConnell 	}
1312327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1313327f2e6cSStephen McConnell 	    __func__, id);
131467feec50SStephen McConnell 
131567feec50SStephen McConnell 	if (mprsas_check_id(sassc, id) != 0) {
1316757ff642SScott Long 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1317757ff642SScott Long 		    "Excluding target id %d\n", id);
131867feec50SStephen McConnell 		error = ENXIO;
131967feec50SStephen McConnell 		goto out;
132067feec50SStephen McConnell 	}
132167feec50SStephen McConnell 
132267feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
132367feec50SStephen McConnell 	    pcie_wwid);
132467feec50SStephen McConnell 	targ = &sassc->targets[id];
132567feec50SStephen McConnell 	targ->devinfo = device_info;
132667feec50SStephen McConnell 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
132767feec50SStephen McConnell 	targ->encl_slot = le16toh(config_page.Slot);
132867feec50SStephen McConnell 	targ->encl_level = config_page.EnclosureLevel;
132967feec50SStephen McConnell 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
133067feec50SStephen McConnell 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
133167feec50SStephen McConnell 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
133267feec50SStephen McConnell 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
133367feec50SStephen McConnell 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
133467feec50SStephen McConnell 	targ->MDTS = config_page2.MaximumDataTransferSize;
133589d1c21fSKashyap D Desai 	if (targ->is_nvme)
133689d1c21fSKashyap D Desai 		targ->controller_reset_timeout = config_page2.ControllerResetTO;
133767feec50SStephen McConnell 	/*
133867feec50SStephen McConnell 	 * Assume always TRUE for encl_level_valid because there is no valid
133967feec50SStephen McConnell 	 * flag for PCIe.
134067feec50SStephen McConnell 	 */
134167feec50SStephen McConnell 	targ->encl_level_valid = TRUE;
134267feec50SStephen McConnell 	targ->handle = handle;
134367feec50SStephen McConnell 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
134467feec50SStephen McConnell 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
134567feec50SStephen McConnell 	targ->parent_sasaddr = le64toh(parent_wwid);
134667feec50SStephen McConnell 	targ->parent_devinfo = parent_devinfo;
134767feec50SStephen McConnell 	targ->tid = id;
134867feec50SStephen McConnell 	targ->linkrate = linkrate;
134967feec50SStephen McConnell 	targ->flags = 0;
135067feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
135167feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
135267feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
135367feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
135467feec50SStephen McConnell 		targ->scsi_req_desc_type =
135567feec50SStephen McConnell 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
135667feec50SStephen McConnell 	}
135767feec50SStephen McConnell 	TAILQ_INIT(&targ->commands);
135867feec50SStephen McConnell 	TAILQ_INIT(&targ->timedout_commands);
135967feec50SStephen McConnell 	while (!SLIST_EMPTY(&targ->luns)) {
136067feec50SStephen McConnell 		lun = SLIST_FIRST(&targ->luns);
136167feec50SStephen McConnell 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
136267feec50SStephen McConnell 		free(lun, M_MPR);
136367feec50SStephen McConnell 	}
136467feec50SStephen McConnell 	SLIST_INIT(&targ->luns);
136567feec50SStephen McConnell 
136667feec50SStephen McConnell 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
136767feec50SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
136867feec50SStephen McConnell 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
136967feec50SStephen McConnell 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
137067feec50SStephen McConnell 	    targ->handle, targ->encl_handle, targ->encl_slot);
137167feec50SStephen McConnell 	if (targ->encl_level_valid) {
137267feec50SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
137367feec50SStephen McConnell 		    "and connector name (%4s)\n", targ->encl_level,
137467feec50SStephen McConnell 		    targ->connector_name);
137567feec50SStephen McConnell 	}
137667feec50SStephen McConnell 	mprsas_rescan_target(sc, targ);
137767feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
137867feec50SStephen McConnell 
137967feec50SStephen McConnell out:
138067feec50SStephen McConnell 	mprsas_startup_decrement(sassc);
138167feec50SStephen McConnell 	return (error);
138267feec50SStephen McConnell }
138367feec50SStephen McConnell 
138467feec50SStephen McConnell static int
1385991554f2SKenneth D. Merry mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1386991554f2SKenneth D. Merry {
1387991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1388991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1389991554f2SKenneth D. Merry 	u64 wwid;
1390991554f2SKenneth D. Merry 	unsigned int id;
1391991554f2SKenneth D. Merry 	int error = 0;
1392991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1393991554f2SKenneth D. Merry 
1394991554f2SKenneth D. Merry 	sassc = sc->sassc;
1395991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
1396991554f2SKenneth D. Merry 	/* wwid is endian safe */
1397991554f2SKenneth D. Merry 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1398991554f2SKenneth D. Merry 	if (!wwid) {
1399991554f2SKenneth D. Merry 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1400991554f2SKenneth D. Merry 		    __func__);
1401991554f2SKenneth D. Merry 		error = ENXIO;
1402991554f2SKenneth D. Merry 		goto out;
1403991554f2SKenneth D. Merry 	}
1404991554f2SKenneth D. Merry 
1405327f2e6cSStephen McConnell 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1406991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
1407991554f2SKenneth D. Merry 		printf("%s: could not get ID for volume with handle 0x%04x and "
1408991554f2SKenneth D. Merry 		    "WWID 0x%016llx\n", __func__, handle,
1409991554f2SKenneth D. Merry 		    (unsigned long long)wwid);
1410991554f2SKenneth D. Merry 		error = ENXIO;
1411991554f2SKenneth D. Merry 		goto out;
1412991554f2SKenneth D. Merry 	}
1413991554f2SKenneth D. Merry 
1414991554f2SKenneth D. Merry 	targ = &sassc->targets[id];
1415991554f2SKenneth D. Merry 	targ->tid = id;
1416991554f2SKenneth D. Merry 	targ->handle = handle;
1417991554f2SKenneth D. Merry 	targ->devname = wwid;
1418991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
1419991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
1420991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
1421991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
1422991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1423991554f2SKenneth D. Merry 		free(lun, M_MPR);
1424991554f2SKenneth D. Merry 	}
1425991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
1426991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
1427991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1428991554f2SKenneth D. Merry 	    targ->tid, wwid);
1429991554f2SKenneth D. Merry out:
1430991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1431991554f2SKenneth D. Merry 	return (error);
1432991554f2SKenneth D. Merry }
1433991554f2SKenneth D. Merry 
1434991554f2SKenneth D. Merry /**
1435991554f2SKenneth D. Merry  * mprsas_SSU_to_SATA_devices
1436991554f2SKenneth D. Merry  * @sc: per adapter object
1437991554f2SKenneth D. Merry  *
1438991554f2SKenneth D. Merry  * Looks through the target list and issues a StartStopUnit SCSI command to each
1439991554f2SKenneth D. Merry  * SATA direct-access device.  This helps to ensure that data corruption is
1440991554f2SKenneth D. Merry  * avoided when the system is being shut down.  This must be called after the IR
1441991554f2SKenneth D. Merry  * System Shutdown RAID Action is sent if in IR mode.
1442991554f2SKenneth D. Merry  *
1443991554f2SKenneth D. Merry  * Return nothing.
1444991554f2SKenneth D. Merry  */
1445991554f2SKenneth D. Merry static void
1446acc173a6SWarner Losh mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto)
1447991554f2SKenneth D. Merry {
1448991554f2SKenneth D. Merry 	struct mprsas_softc *sassc = sc->sassc;
1449991554f2SKenneth D. Merry 	union ccb *ccb;
1450991554f2SKenneth D. Merry 	path_id_t pathid = cam_sim_path(sassc->sim);
1451991554f2SKenneth D. Merry 	target_id_t targetid;
1452991554f2SKenneth D. Merry 	struct mprsas_target *target;
1453991554f2SKenneth D. Merry 	char path_str[64];
1454acc173a6SWarner Losh 	int timeout;
1455991554f2SKenneth D. Merry 
145663941e98SKenneth D. Merry 	mpr_lock(sc);
145763941e98SKenneth D. Merry 
1458991554f2SKenneth D. Merry 	/*
1459a2c14879SStephen McConnell 	 * For each target, issue a StartStopUnit command to stop the device.
1460991554f2SKenneth D. Merry 	 */
1461991554f2SKenneth D. Merry 	sc->SSU_started = TRUE;
1462991554f2SKenneth D. Merry 	sc->SSU_refcount = 0;
1463327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1464991554f2SKenneth D. Merry 		target = &sassc->targets[targetid];
1465991554f2SKenneth D. Merry 		if (target->handle == 0x0) {
1466991554f2SKenneth D. Merry 			continue;
1467991554f2SKenneth D. Merry 		}
1468991554f2SKenneth D. Merry 
1469fa699bb2SAlan Somers 		/*
1470fa699bb2SAlan Somers 		 * The stop_at_shutdown flag will be set if this device is
1471fa699bb2SAlan Somers 		 * a SATA direct-access end device.
1472fa699bb2SAlan Somers 		 */
1473fa699bb2SAlan Somers 		if (target->stop_at_shutdown) {
1474991554f2SKenneth D. Merry 			ccb = xpt_alloc_ccb_nowait();
1475991554f2SKenneth D. Merry 			if (ccb == NULL) {
147667feec50SStephen McConnell 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
147767feec50SStephen McConnell 				    "to stop unit.\n");
1478991554f2SKenneth D. Merry 				return;
1479991554f2SKenneth D. Merry 			}
1480991554f2SKenneth D. Merry 
1481a2c14879SStephen McConnell 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1482a2c14879SStephen McConnell 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1483a2c14879SStephen McConnell 			    CAM_REQ_CMP) {
1484a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1485a2c14879SStephen McConnell 				    "path to stop unit.\n");
1486991554f2SKenneth D. Merry 				xpt_free_ccb(ccb);
1487991554f2SKenneth D. Merry 				return;
1488991554f2SKenneth D. Merry 			}
1489991554f2SKenneth D. Merry 			xpt_path_string(ccb->ccb_h.path, path_str,
1490991554f2SKenneth D. Merry 			    sizeof(path_str));
1491991554f2SKenneth D. Merry 
1492a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1493a2c14879SStephen McConnell 			    "handle %d\n", path_str, target->handle);
1494991554f2SKenneth D. Merry 
1495991554f2SKenneth D. Merry 			/*
1496a2c14879SStephen McConnell 			 * Issue a START STOP UNIT command for the target.
1497a2c14879SStephen McConnell 			 * Increment the SSU counter to be used to count the
1498a2c14879SStephen McConnell 			 * number of required replies.
1499991554f2SKenneth D. Merry 			 */
1500a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1501991554f2SKenneth D. Merry 			sc->SSU_refcount++;
1502991554f2SKenneth D. Merry 			ccb->ccb_h.target_id =
1503991554f2SKenneth D. Merry 			    xpt_path_target_id(ccb->ccb_h.path);
1504991554f2SKenneth D. Merry 			ccb->ccb_h.ppriv_ptr1 = sassc;
1505991554f2SKenneth D. Merry 			scsi_start_stop(&ccb->csio,
1506991554f2SKenneth D. Merry 			    /*retries*/0,
1507991554f2SKenneth D. Merry 			    mprsas_stop_unit_done,
1508991554f2SKenneth D. Merry 			    MSG_SIMPLE_Q_TAG,
1509991554f2SKenneth D. Merry 			    /*start*/FALSE,
1510991554f2SKenneth D. Merry 			    /*load/eject*/0,
1511991554f2SKenneth D. Merry 			    /*immediate*/FALSE,
1512991554f2SKenneth D. Merry 			    MPR_SENSE_LEN,
1513991554f2SKenneth D. Merry 			    /*timeout*/10000);
1514991554f2SKenneth D. Merry 			xpt_action(ccb);
1515991554f2SKenneth D. Merry 		}
1516991554f2SKenneth D. Merry 	}
1517991554f2SKenneth D. Merry 
151863941e98SKenneth D. Merry 	mpr_unlock(sc);
151963941e98SKenneth D. Merry 
1520991554f2SKenneth D. Merry 	/*
1521acc173a6SWarner Losh 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1522acc173a6SWarner Losh 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1523991554f2SKenneth D. Merry 	 */
1524acc173a6SWarner Losh 	timeout = 600;
1525acc173a6SWarner Losh 	if (howto & RB_NOSYNC)
1526acc173a6SWarner Losh 		timeout = 100;
1527991554f2SKenneth D. Merry 
1528acc173a6SWarner Losh 	/*
1529acc173a6SWarner Losh 	 * Wait until all of the SSU commands have completed or time
1530acc173a6SWarner Losh 	 * has expired. Pause for 100ms each time through.  If any
1531acc173a6SWarner Losh 	 * command times out, the target will be reset in the SCSI
1532acc173a6SWarner Losh 	 * command timeout routine.
1533acc173a6SWarner Losh 	 */
1534acc173a6SWarner Losh 	while (sc->SSU_refcount > 0) {
1535acc173a6SWarner Losh 		pause("mprwait", hz/10);
1536acc173a6SWarner Losh 		if (SCHEDULER_STOPPED())
1537acc173a6SWarner Losh 			xpt_sim_poll(sassc->sim);
1538acc173a6SWarner Losh 
1539acc173a6SWarner Losh 		if (--timeout == 0) {
1540a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1541991554f2SKenneth D. Merry 			    "for SSU commands to complete.\n");
1542991554f2SKenneth D. Merry 			break;
1543991554f2SKenneth D. Merry 		}
1544991554f2SKenneth D. Merry 	}
1545991554f2SKenneth D. Merry }
1546991554f2SKenneth D. Merry 
1547991554f2SKenneth D. Merry static void
1548991554f2SKenneth D. Merry mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1549991554f2SKenneth D. Merry {
1550991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1551991554f2SKenneth D. Merry 	char path_str[64];
1552991554f2SKenneth D. Merry 
15535d21655fSStephen McConnell 	if (done_ccb == NULL)
15545d21655fSStephen McConnell 		return;
15555d21655fSStephen McConnell 
1556991554f2SKenneth D. Merry 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1557991554f2SKenneth D. Merry 
1558991554f2SKenneth D. Merry 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1559991554f2SKenneth D. Merry 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1560991554f2SKenneth D. Merry 	    path_str);
1561991554f2SKenneth D. Merry 
1562991554f2SKenneth D. Merry 	/*
1563991554f2SKenneth D. Merry 	 * Nothing more to do except free the CCB and path.  If the command
1564991554f2SKenneth D. Merry 	 * timed out, an abort reset, then target reset will be issued during
1565991554f2SKenneth D. Merry 	 * the SCSI Command process.
1566991554f2SKenneth D. Merry 	 */
1567991554f2SKenneth D. Merry 	xpt_free_path(done_ccb->ccb_h.path);
1568991554f2SKenneth D. Merry 	xpt_free_ccb(done_ccb);
1569991554f2SKenneth D. Merry }
1570991554f2SKenneth D. Merry 
1571991554f2SKenneth D. Merry /**
1572991554f2SKenneth D. Merry  * mprsas_ir_shutdown - IR shutdown notification
1573991554f2SKenneth D. Merry  * @sc: per adapter object
1574991554f2SKenneth D. Merry  *
1575991554f2SKenneth D. Merry  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1576991554f2SKenneth D. Merry  * the host system is shutting down.
1577991554f2SKenneth D. Merry  *
1578991554f2SKenneth D. Merry  * Return nothing.
1579991554f2SKenneth D. Merry  */
1580991554f2SKenneth D. Merry void
1581acc173a6SWarner Losh mprsas_ir_shutdown(struct mpr_softc *sc, int howto)
1582991554f2SKenneth D. Merry {
1583991554f2SKenneth D. Merry 	u16 volume_mapping_flags;
1584991554f2SKenneth D. Merry 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1585991554f2SKenneth D. Merry 	struct dev_mapping_table *mt_entry;
1586991554f2SKenneth D. Merry 	u32 start_idx, end_idx;
1587991554f2SKenneth D. Merry 	unsigned int id, found_volume = 0;
1588991554f2SKenneth D. Merry 	struct mpr_command *cm;
1589991554f2SKenneth D. Merry 	Mpi2RaidActionRequest_t	*action;
1590a2c14879SStephen McConnell 	target_id_t targetid;
1591a2c14879SStephen McConnell 	struct mprsas_target *target;
1592991554f2SKenneth D. Merry 
1593991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1594991554f2SKenneth D. Merry 
1595991554f2SKenneth D. Merry 	/* is IR firmware build loaded? */
1596991554f2SKenneth D. Merry 	if (!sc->ir_firmware)
1597991554f2SKenneth D. Merry 		goto out;
1598991554f2SKenneth D. Merry 
1599991554f2SKenneth D. Merry 	/* are there any volumes?  Look at IR target IDs. */
1600991554f2SKenneth D. Merry 	// TODO-later, this should be looked up in the RAID config structure
1601991554f2SKenneth D. Merry 	// when it is implemented.
1602991554f2SKenneth D. Merry 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1603991554f2SKenneth D. Merry 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1604991554f2SKenneth D. Merry 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1605991554f2SKenneth D. Merry 		start_idx = 0;
1606991554f2SKenneth D. Merry 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1607991554f2SKenneth D. Merry 			start_idx = 1;
1608991554f2SKenneth D. Merry 	} else
1609991554f2SKenneth D. Merry 		start_idx = sc->max_devices - sc->max_volumes;
1610991554f2SKenneth D. Merry 	end_idx = start_idx + sc->max_volumes - 1;
1611991554f2SKenneth D. Merry 
1612991554f2SKenneth D. Merry 	for (id = start_idx; id < end_idx; id++) {
1613991554f2SKenneth D. Merry 		mt_entry = &sc->mapping_table[id];
1614991554f2SKenneth D. Merry 		if ((mt_entry->physical_id != 0) &&
1615991554f2SKenneth D. Merry 		    (mt_entry->missing_count == 0)) {
1616991554f2SKenneth D. Merry 			found_volume = 1;
1617991554f2SKenneth D. Merry 			break;
1618991554f2SKenneth D. Merry 		}
1619991554f2SKenneth D. Merry 	}
1620991554f2SKenneth D. Merry 
1621991554f2SKenneth D. Merry 	if (!found_volume)
1622991554f2SKenneth D. Merry 		goto out;
1623991554f2SKenneth D. Merry 
1624991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1625991554f2SKenneth D. Merry 		printf("%s: command alloc failed\n", __func__);
1626991554f2SKenneth D. Merry 		goto out;
1627991554f2SKenneth D. Merry 	}
1628991554f2SKenneth D. Merry 
1629991554f2SKenneth D. Merry 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1630991554f2SKenneth D. Merry 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1631991554f2SKenneth D. Merry 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1632991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1633991554f2SKenneth D. Merry 	mpr_lock(sc);
16346d4ffcb4SKenneth D. Merry 	mpr_wait_command(sc, &cm, 5, CAN_SLEEP);
1635991554f2SKenneth D. Merry 	mpr_unlock(sc);
1636991554f2SKenneth D. Merry 
1637991554f2SKenneth D. Merry 	/*
1638991554f2SKenneth D. Merry 	 * Don't check for reply, just leave.
1639991554f2SKenneth D. Merry 	 */
1640991554f2SKenneth D. Merry 	if (cm)
1641991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1642991554f2SKenneth D. Merry 
1643991554f2SKenneth D. Merry out:
1644a2c14879SStephen McConnell 	/*
1645a2c14879SStephen McConnell 	 * All of the targets must have the correct value set for
1646a2c14879SStephen McConnell 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1647a2c14879SStephen McConnell 	 *
1648a2c14879SStephen McConnell 	 * The possible values for the 'enable_ssu' variable are:
1649a2c14879SStephen McConnell 	 * 0: disable to SSD and HDD
1650a2c14879SStephen McConnell 	 * 1: disable only to HDD (default)
1651a2c14879SStephen McConnell 	 * 2: disable only to SSD
1652a2c14879SStephen McConnell 	 * 3: enable to SSD and HDD
1653a2c14879SStephen McConnell 	 * anything else will default to 1.
1654a2c14879SStephen McConnell 	 */
1655327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1656a2c14879SStephen McConnell 		target = &sc->sassc->targets[targetid];
1657a2c14879SStephen McConnell 		if (target->handle == 0x0) {
1658a2c14879SStephen McConnell 			continue;
1659a2c14879SStephen McConnell 		}
1660a2c14879SStephen McConnell 
1661a2c14879SStephen McConnell 		if (target->supports_SSU) {
1662a2c14879SStephen McConnell 			switch (sc->enable_ssu) {
1663a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1664a2c14879SStephen McConnell 				target->stop_at_shutdown = FALSE;
1665a2c14879SStephen McConnell 				break;
1666a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1667a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1668a2c14879SStephen McConnell 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1669a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1670a2c14879SStephen McConnell 				}
1671a2c14879SStephen McConnell 				break;
1672a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1673a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1674a2c14879SStephen McConnell 				break;
1675a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1676a2c14879SStephen McConnell 			default:
1677a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1678a2c14879SStephen McConnell 				if ((target->flags &
1679a2c14879SStephen McConnell 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1680a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1681a2c14879SStephen McConnell 				}
1682a2c14879SStephen McConnell 				break;
1683a2c14879SStephen McConnell 			}
1684a2c14879SStephen McConnell 		}
1685a2c14879SStephen McConnell 	}
1686acc173a6SWarner Losh 	mprsas_SSU_to_SATA_devices(sc, howto);
1687991554f2SKenneth D. Merry }
1688