xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision e3c5965c259f7029afe01612b248c3acf9f5b3e0)
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);
128175ad3d0SKenneth D. Merry static void mprsas_ata_id_complete(struct mpr_softc *, struct mpr_command *);
12986312e46SConrad Meyer static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
130991554f2SKenneth D. Merry int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
131a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
132991554f2SKenneth D. Merry static int mprsas_volume_add(struct mpr_softc *sc,
133991554f2SKenneth D. Merry     u16 handle);
134acc173a6SWarner Losh static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
135991554f2SKenneth D. Merry static void mprsas_stop_unit_done(struct cam_periph *periph,
136991554f2SKenneth D. Merry     union ccb *done_ccb);
137991554f2SKenneth D. Merry 
138991554f2SKenneth D. Merry void
139991554f2SKenneth D. Merry mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
140991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
141991554f2SKenneth D. Merry {
142991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
143991554f2SKenneth D. Merry 	u16 sz;
144991554f2SKenneth D. Merry 
145991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
146055e2653SScott Long 	MPR_DPRINT_EVENT(sc, sas, event);
147991554f2SKenneth D. Merry 	mprsas_record_event(sc, event);
148991554f2SKenneth D. Merry 
149991554f2SKenneth D. Merry 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
150991554f2SKenneth D. Merry 	     M_ZERO|M_NOWAIT);
151991554f2SKenneth D. Merry 	if (!fw_event) {
152991554f2SKenneth D. Merry 		printf("%s: allocate failed for fw_event\n", __func__);
153991554f2SKenneth D. Merry 		return;
154991554f2SKenneth D. Merry 	}
155991554f2SKenneth D. Merry 	sz = le16toh(event->EventDataLength) * 4;
156991554f2SKenneth D. Merry 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
157991554f2SKenneth D. Merry 	if (!fw_event->event_data) {
158991554f2SKenneth D. Merry 		printf("%s: allocate failed for event_data\n", __func__);
159991554f2SKenneth D. Merry 		free(fw_event, M_MPR);
160991554f2SKenneth D. Merry 		return;
161991554f2SKenneth D. Merry 	}
162991554f2SKenneth D. Merry 
163991554f2SKenneth D. Merry 	bcopy(event->EventData, fw_event->event_data, sz);
16471900a79SAlfredo Dal'Ava Junior 	fw_event->event = le16toh(event->Event);
16571900a79SAlfredo Dal'Ava Junior 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
16671900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
16771900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
16871900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
169991554f2SKenneth D. Merry 	    sc->track_mapping_events)
170991554f2SKenneth D. Merry 		sc->pending_map_events++;
171991554f2SKenneth D. Merry 
172991554f2SKenneth D. Merry 	/*
173991554f2SKenneth D. Merry 	 * When wait_for_port_enable flag is set, make sure that all the events
174991554f2SKenneth D. Merry 	 * are processed. Increment the startup_refcount and decrement it after
175991554f2SKenneth D. Merry 	 * events are processed.
176991554f2SKenneth D. Merry 	 */
17771900a79SAlfredo Dal'Ava Junior 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
17871900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
17971900a79SAlfredo Dal'Ava Junior 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
180991554f2SKenneth D. Merry 	    sc->wait_for_port_enable)
181991554f2SKenneth D. Merry 		mprsas_startup_increment(sc->sassc);
182991554f2SKenneth D. Merry 
183991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
184991554f2SKenneth D. Merry 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
185991554f2SKenneth D. Merry }
186991554f2SKenneth D. Merry 
187991554f2SKenneth D. Merry static void
188991554f2SKenneth D. Merry mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
189991554f2SKenneth D. Merry {
190991554f2SKenneth D. Merry 
191991554f2SKenneth D. Merry 	free(fw_event->event_data, M_MPR);
192991554f2SKenneth D. Merry 	free(fw_event, M_MPR);
193991554f2SKenneth D. Merry }
194991554f2SKenneth D. Merry 
195991554f2SKenneth D. Merry /**
196991554f2SKenneth D. Merry  * _mpr_fw_work - delayed task for processing firmware events
197991554f2SKenneth D. Merry  * @sc: per adapter object
198991554f2SKenneth D. Merry  * @fw_event: The fw_event_work object
199991554f2SKenneth D. Merry  * Context: user.
200991554f2SKenneth D. Merry  *
201991554f2SKenneth D. Merry  * Return nothing.
202991554f2SKenneth D. Merry  */
203991554f2SKenneth D. Merry static void
204991554f2SKenneth D. Merry mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
205991554f2SKenneth D. Merry {
206991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
207991554f2SKenneth D. Merry 	sassc = sc->sassc;
208991554f2SKenneth D. Merry 
209991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
210991554f2SKenneth D. Merry 	    event_count++, __func__, fw_event->event);
211991554f2SKenneth D. Merry 	switch (fw_event->event) {
212991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
213991554f2SKenneth D. Merry 	{
214991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
215991554f2SKenneth D. Merry 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
21667feec50SStephen McConnell 		uint8_t i;
217991554f2SKenneth D. Merry 
218991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
219991554f2SKenneth D. Merry 		    fw_event->event_data;
220991554f2SKenneth D. Merry 
221991554f2SKenneth D. Merry 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
222991554f2SKenneth D. Merry 
223991554f2SKenneth D. Merry 		for (i = 0; i < data->NumEntries; i++) {
224991554f2SKenneth D. Merry 			phy = &data->PHY[i];
225991554f2SKenneth D. Merry 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
226991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
227991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
228991554f2SKenneth D. Merry 				    le16toh(phy->AttachedDevHandle),
229991554f2SKenneth D. Merry 				    phy->LinkRate)) {
230327f2e6cSStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: "
231327f2e6cSStephen McConnell 					    "failed to add device with handle "
232327f2e6cSStephen McConnell 					    "0x%x\n", __func__,
233991554f2SKenneth D. Merry 					    le16toh(phy->AttachedDevHandle));
234991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc, le16toh(
235991554f2SKenneth D. Merry 					    phy->AttachedDevHandle));
236991554f2SKenneth D. Merry 				}
237991554f2SKenneth D. Merry 				break;
238991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
239991554f2SKenneth D. Merry 				mprsas_prepare_remove(sassc, le16toh(
240991554f2SKenneth D. Merry 				    phy->AttachedDevHandle));
241991554f2SKenneth D. Merry 				break;
242991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
243991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
244991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
245991554f2SKenneth D. Merry 			default:
246991554f2SKenneth D. Merry 				break;
247991554f2SKenneth D. Merry 			}
248991554f2SKenneth D. Merry 		}
249991554f2SKenneth D. Merry 		/*
250991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
251991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
252991554f2SKenneth D. Merry 		 * been processed.
253991554f2SKenneth D. Merry 		 */
254991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
255991554f2SKenneth D. Merry 		break;
256991554f2SKenneth D. Merry 	}
257991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DISCOVERY:
258991554f2SKenneth D. Merry 	{
259991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
260991554f2SKenneth D. Merry 
261991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
262991554f2SKenneth D. Merry 
263991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
2647a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
265991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
266991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
267991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
268991554f2SKenneth D. Merry 			mprsas_discovery_end(sassc);
269991554f2SKenneth D. Merry 		}
270991554f2SKenneth D. Merry 		break;
271991554f2SKenneth D. Merry 	}
272991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
273991554f2SKenneth D. Merry 	{
274991554f2SKenneth D. Merry 		Mpi2EventDataSasEnclDevStatusChange_t *data;
275991554f2SKenneth D. Merry 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
276991554f2SKenneth D. Merry 		    fw_event->event_data;
277991554f2SKenneth D. Merry 		mpr_mapping_enclosure_dev_status_change_event(sc,
278991554f2SKenneth D. Merry 		    fw_event->event_data);
279991554f2SKenneth D. Merry 		break;
280991554f2SKenneth D. Merry 	}
281991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
282991554f2SKenneth D. Merry 	{
283991554f2SKenneth D. Merry 		Mpi2EventIrConfigElement_t *element;
284991554f2SKenneth D. Merry 		int i;
285991554f2SKenneth D. Merry 		u8 foreign_config, reason;
286991554f2SKenneth D. Merry 		u16 elementType;
287991554f2SKenneth D. Merry 		Mpi2EventDataIrConfigChangeList_t *event_data;
288991554f2SKenneth D. Merry 		struct mprsas_target *targ;
289991554f2SKenneth D. Merry 		unsigned int id;
290991554f2SKenneth D. Merry 
291991554f2SKenneth D. Merry 		event_data = fw_event->event_data;
292991554f2SKenneth D. Merry 		foreign_config = (le32toh(event_data->Flags) &
293991554f2SKenneth D. Merry 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
294991554f2SKenneth D. Merry 
295991554f2SKenneth D. Merry 		element =
296991554f2SKenneth D. Merry 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
297327f2e6cSStephen McConnell 		id = mpr_mapping_get_raid_tid_from_handle(sc,
298991554f2SKenneth D. Merry 		    element->VolDevHandle);
299991554f2SKenneth D. Merry 
300991554f2SKenneth D. Merry 		mpr_mapping_ir_config_change_event(sc, event_data);
301991554f2SKenneth D. Merry 		for (i = 0; i < event_data->NumElements; i++, element++) {
302991554f2SKenneth D. Merry 			reason = element->ReasonCode;
303991554f2SKenneth D. Merry 			elementType = le16toh(element->ElementFlags) &
304991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
305991554f2SKenneth D. Merry 			/*
306991554f2SKenneth D. Merry 			 * check for element type of Phys Disk or Hot Spare
307991554f2SKenneth D. Merry 			 */
308991554f2SKenneth D. Merry 			if ((elementType !=
309991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
310991554f2SKenneth D. Merry 			    && (elementType !=
311991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
312991554f2SKenneth D. Merry 				// do next element
313991554f2SKenneth D. Merry 				goto skip_fp_send;
314991554f2SKenneth D. Merry 
315991554f2SKenneth D. Merry 			/*
316991554f2SKenneth D. Merry 			 * check for reason of Hide, Unhide, PD Created, or PD
317991554f2SKenneth D. Merry 			 * Deleted
318991554f2SKenneth D. Merry 			 */
319991554f2SKenneth D. Merry 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
320991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
321991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
322991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
323991554f2SKenneth D. Merry 				goto skip_fp_send;
324991554f2SKenneth D. Merry 
325991554f2SKenneth D. Merry 			// check for a reason of Hide or PD Created
326991554f2SKenneth D. Merry 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
327991554f2SKenneth D. Merry 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
328991554f2SKenneth D. Merry 			{
329991554f2SKenneth D. Merry 				// build RAID Action message
330991554f2SKenneth D. Merry 				Mpi2RaidActionRequest_t	*action;
3316d4ffcb4SKenneth D. Merry 				Mpi2RaidActionReply_t *reply = NULL;
332991554f2SKenneth D. Merry 				struct mpr_command *cm;
333991554f2SKenneth D. Merry 				int error = 0;
334991554f2SKenneth D. Merry 				if ((cm = mpr_alloc_command(sc)) == NULL) {
335991554f2SKenneth D. Merry 					printf("%s: command alloc failed\n",
336991554f2SKenneth D. Merry 					    __func__);
337991554f2SKenneth D. Merry 					return;
338991554f2SKenneth D. Merry 				}
339991554f2SKenneth D. Merry 
340a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
341991554f2SKenneth D. Merry 				    "from "
342991554f2SKenneth D. Merry 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
343991554f2SKenneth D. Merry 				    ":\n");
344991554f2SKenneth D. Merry 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
345991554f2SKenneth D. Merry 				action->Function = MPI2_FUNCTION_RAID_ACTION;
346991554f2SKenneth D. Merry 				action->Action =
347991554f2SKenneth D. Merry 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
348991554f2SKenneth D. Merry 				action->PhysDiskNum = element->PhysDiskNum;
349991554f2SKenneth D. Merry 				cm->cm_desc.Default.RequestFlags =
350991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3516d4ffcb4SKenneth D. Merry 				error = mpr_request_polled(sc, &cm);
3526d4ffcb4SKenneth D. Merry 				if (cm != NULL)
3536d4ffcb4SKenneth D. Merry 					reply = (Mpi2RaidActionReply_t *)
3546d4ffcb4SKenneth D. Merry 					    cm->cm_reply;
355991554f2SKenneth D. Merry 				if (error || (reply == NULL)) {
356991554f2SKenneth D. Merry 					/* FIXME */
357991554f2SKenneth D. Merry 					/*
358991554f2SKenneth D. Merry 					 * If the poll returns error then we
359991554f2SKenneth D. Merry 					 * need to do diag reset
360991554f2SKenneth D. Merry 					 */
361991554f2SKenneth D. Merry 					printf("%s: poll for page completed "
362991554f2SKenneth D. Merry 					    "with error %d", __func__, error);
363991554f2SKenneth D. Merry 				}
364991554f2SKenneth D. Merry 				if (reply && (le16toh(reply->IOCStatus) &
365991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_MASK) !=
366991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_SUCCESS) {
367a2c14879SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: error "
368a2c14879SStephen McConnell 					    "sending RaidActionPage; "
369a2c14879SStephen McConnell 					    "iocstatus = 0x%x\n", __func__,
370991554f2SKenneth D. Merry 					    le16toh(reply->IOCStatus));
371991554f2SKenneth D. Merry 				}
372991554f2SKenneth D. Merry 
373991554f2SKenneth D. Merry 				if (cm)
374991554f2SKenneth D. Merry 					mpr_free_command(sc, cm);
375991554f2SKenneth D. Merry 			}
376991554f2SKenneth D. Merry skip_fp_send:
377a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_EVENT, "Received "
378991554f2SKenneth D. Merry 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
379991554f2SKenneth D. Merry 			    "code %x:\n", element->ReasonCode);
380991554f2SKenneth D. Merry 			switch (element->ReasonCode) {
381991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
382991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
383991554f2SKenneth D. Merry 				if (!foreign_config) {
384991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
385991554f2SKenneth D. Merry 					    le16toh(element->VolDevHandle))) {
386991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
387991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
388991554f2SKenneth D. Merry 						    __func__, le16toh(element->
389991554f2SKenneth D. Merry 						    VolDevHandle));
390991554f2SKenneth D. Merry 					}
391991554f2SKenneth D. Merry 				}
392991554f2SKenneth D. Merry 				break;
393991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
394991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
395991554f2SKenneth D. Merry 				/*
396991554f2SKenneth D. Merry 				 * Rescan after volume is deleted or removed.
397991554f2SKenneth D. Merry 				 */
398991554f2SKenneth D. Merry 				if (!foreign_config) {
399991554f2SKenneth D. Merry 					if (id == MPR_MAP_BAD_ID) {
400991554f2SKenneth D. Merry 						printf("%s: could not get ID "
401991554f2SKenneth D. Merry 						    "for volume with handle "
402991554f2SKenneth D. Merry 						    "0x%04x\n", __func__,
403991554f2SKenneth D. Merry 						    le16toh(element->
404991554f2SKenneth D. Merry 						    VolDevHandle));
405991554f2SKenneth D. Merry 						break;
406991554f2SKenneth D. Merry 					}
407991554f2SKenneth D. Merry 
408991554f2SKenneth D. Merry 					targ = &sassc->targets[id];
409991554f2SKenneth D. Merry 					targ->handle = 0x0;
410991554f2SKenneth D. Merry 					targ->encl_slot = 0x0;
411991554f2SKenneth D. Merry 					targ->encl_handle = 0x0;
412991554f2SKenneth D. Merry 					targ->encl_level_valid = 0x0;
413991554f2SKenneth D. Merry 					targ->encl_level = 0x0;
414991554f2SKenneth D. Merry 					targ->connector_name[0] = ' ';
415991554f2SKenneth D. Merry 					targ->connector_name[1] = ' ';
416991554f2SKenneth D. Merry 					targ->connector_name[2] = ' ';
417991554f2SKenneth D. Merry 					targ->connector_name[3] = ' ';
418991554f2SKenneth D. Merry 					targ->exp_dev_handle = 0x0;
419991554f2SKenneth D. Merry 					targ->phy_num = 0x0;
420991554f2SKenneth D. Merry 					targ->linkrate = 0x0;
421991554f2SKenneth D. Merry 					mprsas_rescan_target(sc, targ);
422991554f2SKenneth D. Merry 					printf("RAID target id 0x%x removed\n",
423991554f2SKenneth D. Merry 					    targ->tid);
424991554f2SKenneth D. Merry 				}
425991554f2SKenneth D. Merry 				break;
426991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
427991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
428991554f2SKenneth D. Merry 				/*
429991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been created.  Hide
430991554f2SKenneth D. Merry 				 * it from the OS.
431991554f2SKenneth D. Merry 				 */
432991554f2SKenneth D. Merry 				targ = mprsas_find_target_by_handle(sassc, 0,
433991554f2SKenneth D. Merry 				    element->PhysDiskDevHandle);
434991554f2SKenneth D. Merry 				if (targ == NULL)
435991554f2SKenneth D. Merry 					break;
436991554f2SKenneth D. Merry 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
437991554f2SKenneth D. Merry 				mprsas_rescan_target(sc, targ);
438991554f2SKenneth D. Merry 				break;
439991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
440991554f2SKenneth D. Merry 				/*
441991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been deleted.
442991554f2SKenneth D. Merry 				 * Expose it to the OS.
443991554f2SKenneth D. Merry 				 */
444991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
445991554f2SKenneth D. Merry 				    le16toh(element->PhysDiskDevHandle), 0)) {
446991554f2SKenneth D. Merry 					printf("%s: failed to add device with "
447991554f2SKenneth D. Merry 					    "handle 0x%x\n", __func__,
448991554f2SKenneth D. Merry 					    le16toh(element->
449991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
450991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc,
451991554f2SKenneth D. Merry 					    le16toh(element->
452991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
453991554f2SKenneth D. Merry 				}
454991554f2SKenneth D. Merry 				break;
455991554f2SKenneth D. Merry 			}
456991554f2SKenneth D. Merry 		}
457991554f2SKenneth D. Merry 		/*
458991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
459991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
460991554f2SKenneth D. Merry 		 * been processed.
461991554f2SKenneth D. Merry 		 */
462991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
463991554f2SKenneth D. Merry 		break;
464991554f2SKenneth D. Merry 	}
465991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_VOLUME:
466991554f2SKenneth D. Merry 	{
467991554f2SKenneth D. Merry 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
468991554f2SKenneth D. Merry 
469991554f2SKenneth D. Merry 		/*
470991554f2SKenneth D. Merry 		 * Informational only.
471991554f2SKenneth D. Merry 		 */
472991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
473991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
474991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
475991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
476991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
477991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
478991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
479991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
480991554f2SKenneth D. Merry 			break;
481991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
482991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
483991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
484991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
485991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
486991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
487991554f2SKenneth D. Merry 			break;
488991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
489991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
490991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
491991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
492991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
493991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
494991554f2SKenneth D. Merry 				u32 state;
495991554f2SKenneth D. Merry 				struct mprsas_target *targ;
496991554f2SKenneth D. Merry 				state = le32toh(event_data->NewValue);
497991554f2SKenneth D. Merry 				switch (state) {
498991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_MISSING:
499991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_FAILED:
500991554f2SKenneth D. Merry 					mprsas_prepare_volume_remove(sassc,
501991554f2SKenneth D. Merry 					    event_data->VolDevHandle);
502991554f2SKenneth D. Merry 					break;
503991554f2SKenneth D. Merry 
504991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_ONLINE:
505991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_DEGRADED:
506991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_OPTIMAL:
507991554f2SKenneth D. Merry 					targ =
508991554f2SKenneth D. Merry 					    mprsas_find_target_by_handle(sassc,
509991554f2SKenneth D. Merry 					    0, event_data->VolDevHandle);
510991554f2SKenneth D. Merry 					if (targ) {
511991554f2SKenneth D. Merry 						printf("%s %d: Volume handle "
512991554f2SKenneth D. Merry 						    "0x%x is already added \n",
513991554f2SKenneth D. Merry 						    __func__, __LINE__,
514991554f2SKenneth D. Merry 						    event_data->VolDevHandle);
515991554f2SKenneth D. Merry 						break;
516991554f2SKenneth D. Merry 					}
517991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
518991554f2SKenneth D. Merry 					    le16toh(event_data->
519991554f2SKenneth D. Merry 					    VolDevHandle))) {
520991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
521991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
522991554f2SKenneth D. Merry 						    __func__, le16toh(
523991554f2SKenneth D. Merry 						    event_data->VolDevHandle));
524991554f2SKenneth D. Merry 					}
525991554f2SKenneth D. Merry 					break;
526991554f2SKenneth D. Merry 				default:
527991554f2SKenneth D. Merry 					break;
528991554f2SKenneth D. Merry 				}
529991554f2SKenneth D. Merry 			break;
530991554f2SKenneth D. Merry 		default:
531991554f2SKenneth D. Merry 			break;
532991554f2SKenneth D. Merry 		}
533991554f2SKenneth D. Merry 		break;
534991554f2SKenneth D. Merry 	}
535991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_PHYSICAL_DISK:
536991554f2SKenneth D. Merry 	{
537991554f2SKenneth D. Merry 		Mpi2EventDataIrPhysicalDisk_t *event_data =
538991554f2SKenneth D. Merry 		    fw_event->event_data;
539991554f2SKenneth D. Merry 		struct mprsas_target *targ;
540991554f2SKenneth D. Merry 
541991554f2SKenneth D. Merry 		/*
542991554f2SKenneth D. Merry 		 * Informational only.
543991554f2SKenneth D. Merry 		 */
544991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
545991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
546991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
547991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
548991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Phys Disk Number "
549991554f2SKenneth D. Merry   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
550991554f2SKenneth D. Merry  			    "%d", le32toh(event_data->PreviousValue),
551991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
552991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
553991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
554991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
555991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
556991554f2SKenneth D. Merry 			break;
557991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
558991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
559991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
560991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
561991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
562991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
563991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
564991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
565991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
566991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
567991554f2SKenneth D. Merry 			break;
568991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
569991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
570991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
571991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
572991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
573991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
574991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
575991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
576991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
577991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
578991554f2SKenneth D. Merry 			switch (event_data->NewValue) {
579991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_ONLINE:
580991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_DEGRADED:
581991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_REBUILDING:
582991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OPTIMAL:
583991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_HOT_SPARE:
584991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
585991554f2SKenneth D. Merry 					    sassc, 0,
586991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
587991554f2SKenneth D. Merry 					if (targ) {
588991554f2SKenneth D. Merry 						targ->flags |=
589991554f2SKenneth D. Merry 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
590991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
591991554f2SKenneth D. Merry 						    "for handle 0x%x.\n",
592991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
593991554f2SKenneth D. Merry 						    event_data->
594991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
595991554f2SKenneth D. Merry 					}
596991554f2SKenneth D. Merry 				break;
597991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OFFLINE:
598991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
599991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
600991554f2SKenneth D. Merry 				default:
601991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
602991554f2SKenneth D. Merry 					    sassc, 0,
603991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
604991554f2SKenneth D. Merry 					if (targ) {
605991554f2SKenneth D. Merry 						targ->flags |=
606991554f2SKenneth D. Merry 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
607991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
608991554f2SKenneth D. Merry 						    "for handle 0x%x.  \n",
609991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
610991554f2SKenneth D. Merry 						    event_data->
611991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
612991554f2SKenneth D. Merry 					}
613991554f2SKenneth D. Merry 				break;
614991554f2SKenneth D. Merry 			}
615991554f2SKenneth D. Merry 		default:
616991554f2SKenneth D. Merry 			break;
617991554f2SKenneth D. Merry 		}
618991554f2SKenneth D. Merry 		break;
619991554f2SKenneth D. Merry 	}
620991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_OPERATION_STATUS:
621991554f2SKenneth D. Merry 	{
622991554f2SKenneth D. Merry 		Mpi2EventDataIrOperationStatus_t *event_data =
623991554f2SKenneth D. Merry 		    fw_event->event_data;
624991554f2SKenneth D. Merry 
625991554f2SKenneth D. Merry 		/*
626991554f2SKenneth D. Merry 		 * Informational only.
627991554f2SKenneth D. Merry 		 */
628991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
629991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
630991554f2SKenneth D. Merry 		    "percent complete for Volume with handle 0x%x",
631991554f2SKenneth D. Merry 		    event_data->RAIDOperation, event_data->PercentComplete,
632991554f2SKenneth D. Merry 		    le16toh(event_data->VolDevHandle));
633991554f2SKenneth D. Merry 		break;
634991554f2SKenneth D. Merry 	}
635991554f2SKenneth D. Merry 	case MPI2_EVENT_TEMP_THRESHOLD:
636991554f2SKenneth D. Merry 	{
637991554f2SKenneth D. Merry 		pMpi2EventDataTemperature_t	temp_event;
638991554f2SKenneth D. Merry 
639991554f2SKenneth D. Merry 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
640991554f2SKenneth D. Merry 
641991554f2SKenneth D. Merry 		/*
642991554f2SKenneth D. Merry 		 * The Temp Sensor Count must be greater than the event's Sensor
643991554f2SKenneth D. Merry 		 * Num to be valid.  If valid, print the temp thresholds that
644991554f2SKenneth D. Merry 		 * have been exceeded.
645991554f2SKenneth D. Merry 		 */
646991554f2SKenneth D. Merry 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
647991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
648991554f2SKenneth D. Merry 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
649991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
650991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
651991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
652991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
653991554f2SKenneth D. Merry 			    temp_event->SensorNum);
654991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
655991554f2SKenneth D. Merry 			    "%d\n", temp_event->CurrentTemperature);
656991554f2SKenneth D. Merry 		}
657991554f2SKenneth D. Merry 		break;
658991554f2SKenneth D. Merry 	}
6592bbc5fcbSStephen McConnell 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
6602bbc5fcbSStephen McConnell 	{
6612bbc5fcbSStephen McConnell 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
6622bbc5fcbSStephen McConnell 		ace_event_data =
6632bbc5fcbSStephen McConnell 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
6642bbc5fcbSStephen McConnell 
6650656476aSAlexander Motin 		switch(ace_event_data->ReasonCode) {
6660656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
6670656476aSAlexander Motin 		{
6680656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6692bbc5fcbSStephen McConnell 			    "ReceptacleID %d cannot be powered and device "
6702bbc5fcbSStephen McConnell 			    "connected to this active cable will not be seen. "
6712bbc5fcbSStephen McConnell 			    "This active cable requires %d mW of power.\n",
6722bbc5fcbSStephen McConnell 			    ace_event_data->ReceptacleID,
6732bbc5fcbSStephen McConnell 			    ace_event_data->ActiveCablePowerRequirement);
6740656476aSAlexander Motin 			break;
6750656476aSAlexander Motin 		}
6760656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
6770656476aSAlexander Motin 		{
6780656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6790656476aSAlexander Motin 			    "ReceptacleID %d is not running at optimal speed "
6800656476aSAlexander Motin 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
6810656476aSAlexander Motin 			break;
6820656476aSAlexander Motin 		}
6830656476aSAlexander Motin 		default:
6840656476aSAlexander Motin 			break;
6852bbc5fcbSStephen McConnell 		}
6862bbc5fcbSStephen McConnell 		break;
6872bbc5fcbSStephen McConnell 	}
68889d1c21fSKashyap D Desai 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
68989d1c21fSKashyap D Desai 	{
69089d1c21fSKashyap D Desai 		pMpi26EventDataPCIeDeviceStatusChange_t	pcie_status_event_data;
69189d1c21fSKashyap D Desai 		pcie_status_event_data =
69289d1c21fSKashyap D Desai 		   (pMpi26EventDataPCIeDeviceStatusChange_t)fw_event->event_data;
69389d1c21fSKashyap D Desai 
69489d1c21fSKashyap D Desai 		switch (pcie_status_event_data->ReasonCode) {
69589d1c21fSKashyap D Desai 		case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
69689d1c21fSKashyap D Desai 		{
69789d1c21fSKashyap D Desai 			mpr_printf(sc, "PCIe Host Reset failed on DevHandle "
69889d1c21fSKashyap D Desai 			    "0x%x\n", pcie_status_event_data->DevHandle);
69989d1c21fSKashyap D Desai 			break;
70089d1c21fSKashyap D Desai 		}
70189d1c21fSKashyap D Desai 		default:
70289d1c21fSKashyap D Desai 			break;
70389d1c21fSKashyap D Desai 		}
70489d1c21fSKashyap D Desai 		break;
70589d1c21fSKashyap D Desai 	}
7065f5baf0eSAlexander Motin 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
7075f5baf0eSAlexander Motin 	{
7085f5baf0eSAlexander Motin 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
7095f5baf0eSAlexander Motin 		uint64_t sas_address;
7105f5baf0eSAlexander Motin 
7115f5baf0eSAlexander Motin 		discovery_error_data =
7125f5baf0eSAlexander Motin 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
7135f5baf0eSAlexander Motin 		    fw_event->event_data;
7145f5baf0eSAlexander Motin 
7155f5baf0eSAlexander Motin 		sas_address = discovery_error_data->SASAddress.High;
7165f5baf0eSAlexander Motin 		sas_address = (sas_address << 32) |
7175f5baf0eSAlexander Motin 		    discovery_error_data->SASAddress.Low;
7185f5baf0eSAlexander Motin 
7195f5baf0eSAlexander Motin 		switch(discovery_error_data->ReasonCode) {
7205f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
7215f5baf0eSAlexander Motin 		{
7225f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command failed during discovery "
7235f5baf0eSAlexander Motin 			    "for expander with SAS Address %jx and "
7245f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7255f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7265f5baf0eSAlexander Motin 			break;
7275f5baf0eSAlexander Motin 		}
7285f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
7295f5baf0eSAlexander Motin 		{
7305f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command timed out during "
7315f5baf0eSAlexander Motin 			    "discovery for expander with SAS Address %jx and "
7325f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7335f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7345f5baf0eSAlexander Motin 			break;
7355f5baf0eSAlexander Motin 		}
7365f5baf0eSAlexander Motin 		default:
7375f5baf0eSAlexander Motin 			break;
7385f5baf0eSAlexander Motin 		}
7395f5baf0eSAlexander Motin 		break;
7405f5baf0eSAlexander Motin 	}
74167feec50SStephen McConnell 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
74267feec50SStephen McConnell 	{
74367feec50SStephen McConnell 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
74467feec50SStephen McConnell 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
74567feec50SStephen McConnell 		uint8_t i, link_rate;
74667feec50SStephen McConnell 		uint16_t handle;
74767feec50SStephen McConnell 
74867feec50SStephen McConnell 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
74967feec50SStephen McConnell 		    fw_event->event_data;
75067feec50SStephen McConnell 
75167feec50SStephen McConnell 		mpr_mapping_pcie_topology_change_event(sc,
75267feec50SStephen McConnell 		    fw_event->event_data);
75367feec50SStephen McConnell 
75467feec50SStephen McConnell 		for (i = 0; i < data->NumEntries; i++) {
75567feec50SStephen McConnell 			port_entry = &data->PortEntry[i];
75667feec50SStephen McConnell 			handle = le16toh(port_entry->AttachedDevHandle);
75767feec50SStephen McConnell 			link_rate = port_entry->CurrentPortInfo &
75867feec50SStephen McConnell 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
75967feec50SStephen McConnell 			switch (port_entry->PortStatus) {
76067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
76167feec50SStephen McConnell 				if (link_rate <
76267feec50SStephen McConnell 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
76367feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
76467feec50SStephen McConnell 					    "add PCIe device with handle 0x%x "
76567feec50SStephen McConnell 					    "with unknown link rate.\n",
76667feec50SStephen McConnell 					    __func__, handle);
76767feec50SStephen McConnell 					break;
76867feec50SStephen McConnell 				}
76967feec50SStephen McConnell 				if (mprsas_add_pcie_device(sc, handle,
77067feec50SStephen McConnell 				    link_rate)) {
77167feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
77267feec50SStephen McConnell 					    "to add PCIe device with handle "
77367feec50SStephen McConnell 					    "0x%x\n", __func__, handle);
77467feec50SStephen McConnell 					mprsas_prepare_remove(sassc, handle);
77567feec50SStephen McConnell 				}
77667feec50SStephen McConnell 				break;
77767feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
77867feec50SStephen McConnell 				mprsas_prepare_remove(sassc, handle);
77967feec50SStephen McConnell 				break;
78067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
78167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
78267feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
78367feec50SStephen McConnell 			default:
78467feec50SStephen McConnell 				break;
78567feec50SStephen McConnell 			}
78667feec50SStephen McConnell 		}
78767feec50SStephen McConnell 		/*
78867feec50SStephen McConnell 		 * refcount was incremented for this event in
78967feec50SStephen McConnell 		 * mprsas_evt_handler.  Decrement it here because the event has
79067feec50SStephen McConnell 		 * been processed.
79167feec50SStephen McConnell 		 */
79267feec50SStephen McConnell 		mprsas_startup_decrement(sassc);
79367feec50SStephen McConnell 		break;
79467feec50SStephen McConnell 	}
795991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
796991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
797991554f2SKenneth D. Merry 	default:
798991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
799991554f2SKenneth D. Merry 		    fw_event->event);
800991554f2SKenneth D. Merry 		break;
801991554f2SKenneth D. Merry 	}
802991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
803991554f2SKenneth D. Merry 	    __func__, fw_event->event);
804991554f2SKenneth D. Merry 	mprsas_fw_event_free(sc, fw_event);
805991554f2SKenneth D. Merry }
806991554f2SKenneth D. Merry 
807991554f2SKenneth D. Merry void
808991554f2SKenneth D. Merry mprsas_firmware_event_work(void *arg, int pending)
809991554f2SKenneth D. Merry {
810991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
811991554f2SKenneth D. Merry 	struct mpr_softc *sc;
812991554f2SKenneth D. Merry 
813991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
814991554f2SKenneth D. Merry 	mpr_lock(sc);
815991554f2SKenneth D. Merry 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
816991554f2SKenneth D. Merry 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
817991554f2SKenneth D. Merry 		mprsas_fw_work(sc, fw_event);
818991554f2SKenneth D. Merry 	}
819991554f2SKenneth D. Merry 	mpr_unlock(sc);
820991554f2SKenneth D. Merry }
821991554f2SKenneth D. Merry 
822991554f2SKenneth D. Merry static int
82367feec50SStephen McConnell mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
82467feec50SStephen McConnell {
825991554f2SKenneth D. Merry 	char devstring[80];
826991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
827991554f2SKenneth D. Merry 	struct mprsas_target *targ;
828991554f2SKenneth D. Merry 	Mpi2ConfigReply_t mpi_reply;
829991554f2SKenneth D. Merry 	Mpi2SasDevicePage0_t config_page;
830a2c14879SStephen McConnell 	uint64_t sas_address, parent_sas_address = 0;
831991554f2SKenneth D. Merry 	u32 device_info, parent_devinfo = 0;
832991554f2SKenneth D. Merry 	unsigned int id;
833a2c14879SStephen McConnell 	int ret = 1, error = 0, i;
834991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
835a2c14879SStephen McConnell 	u8 is_SATA_SSD = 0;
836a2c14879SStephen McConnell 	struct mpr_command *cm;
837991554f2SKenneth D. Merry 
838991554f2SKenneth D. Merry 	sassc = sc->sassc;
839991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
840aeb9ac0dSScott Long 	if (mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
841aeb9ac0dSScott Long 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
842aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
843aeb9ac0dSScott Long 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
844aeb9ac0dSScott Long 		    handle, mpi_reply.IOCStatus);
845991554f2SKenneth D. Merry 		error = ENXIO;
846991554f2SKenneth D. Merry 		goto out;
847991554f2SKenneth D. Merry 	}
848991554f2SKenneth D. Merry 
849991554f2SKenneth D. Merry 	device_info = le32toh(config_page.DeviceInfo);
850991554f2SKenneth D. Merry 
851991554f2SKenneth D. Merry 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
852991554f2SKenneth D. Merry 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
853991554f2SKenneth D. Merry 		Mpi2ConfigReply_t tmp_mpi_reply;
854991554f2SKenneth D. Merry 		Mpi2SasDevicePage0_t parent_config_page;
855991554f2SKenneth D. Merry 
856aeb9ac0dSScott Long 		if (mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
857991554f2SKenneth D. Merry 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
858aeb9ac0dSScott Long 		    le16toh(config_page.ParentDevHandle)) != 0) {
859757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_FAULT,
860aeb9ac0dSScott Long 			    "Error reading parent SAS device %#x page0, "
861aeb9ac0dSScott Long 			    "iocstatus= 0x%x\n",
862aeb9ac0dSScott Long 			    le16toh(config_page.ParentDevHandle),
863aeb9ac0dSScott Long 			    tmp_mpi_reply.IOCStatus);
864991554f2SKenneth D. Merry 		} else {
865991554f2SKenneth D. Merry 			parent_sas_address = parent_config_page.SASAddress.High;
866991554f2SKenneth D. Merry 			parent_sas_address = (parent_sas_address << 32) |
867991554f2SKenneth D. Merry 			    parent_config_page.SASAddress.Low;
868991554f2SKenneth D. Merry 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
869991554f2SKenneth D. Merry 		}
870991554f2SKenneth D. Merry 	}
87171900a79SAlfredo Dal'Ava Junior 	sas_address = htole32(config_page.SASAddress.High);
87271900a79SAlfredo Dal'Ava Junior 	sas_address = (sas_address << 32) | htole32(config_page.SASAddress.Low);
873757ff642SScott Long 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
874757ff642SScott Long 	    "page0 = %jx\n", handle, sas_address);
875991554f2SKenneth D. Merry 
876a2c14879SStephen McConnell 	/*
877a2c14879SStephen McConnell 	 * Always get SATA Identify information because this is used to
878a2c14879SStephen McConnell 	 * determine if Start/Stop Unit should be sent to the drive when the
879a2c14879SStephen McConnell 	 * system is shutdown.
880a2c14879SStephen McConnell 	 */
881991554f2SKenneth D. Merry 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
882a2c14879SStephen McConnell 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
883a2c14879SStephen McConnell 		    handle, device_info, &is_SATA_SSD);
884a2c14879SStephen McConnell 		if (ret) {
885757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
886757ff642SScott Long 			    "%s: failed to get disk type (SSD or HDD) for SATA "
887757ff642SScott Long 			    "device with handle 0x%04x\n",
888a2c14879SStephen McConnell 			    __func__, handle);
889a2c14879SStephen McConnell 		} else {
890757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
891757ff642SScott Long 			    "from SATA device = %jx\n", handle, sas_address);
892a2c14879SStephen McConnell 		}
893a2c14879SStephen McConnell 	}
894c91d307fSStephen McConnell 
8954ab1cdc5SScott Long 	/*
8964ab1cdc5SScott Long 	 * use_phynum:
8974ab1cdc5SScott Long 	 *  1 - use the PhyNum field as a fallback to the mapping logic
8984ab1cdc5SScott Long 	 *  0 - never use the PhyNum field
8994ab1cdc5SScott Long 	 * -1 - only use the PhyNum field
900327f2e6cSStephen McConnell 	 *
901327f2e6cSStephen McConnell 	 * Note that using the Phy number to map a device can cause device adds
902327f2e6cSStephen McConnell 	 * to fail if multiple enclosures/expanders are in the topology. For
903327f2e6cSStephen McConnell 	 * example, if two devices are in the same slot number in two different
904327f2e6cSStephen McConnell 	 * enclosures within the topology, only one of those devices will be
905327f2e6cSStephen McConnell 	 * added. PhyNum mapping should not be used if multiple enclosures are
906327f2e6cSStephen McConnell 	 * in the topology.
9074ab1cdc5SScott Long 	 */
9084ab1cdc5SScott Long 	id = MPR_MAP_BAD_ID;
9094ab1cdc5SScott Long 	if (sc->use_phynum != -1)
910327f2e6cSStephen McConnell 		id = mpr_mapping_get_tid(sc, sas_address, handle);
911991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
91267feec50SStephen McConnell 		if ((sc->use_phynum == 0) ||
91367feec50SStephen McConnell 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
9144ab1cdc5SScott Long 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
9154ab1cdc5SScott Long 			    "Could not get ID for device with handle 0x%04x\n",
9164ab1cdc5SScott Long 			    __FILE__, __LINE__, __func__, handle);
917991554f2SKenneth D. Merry 			error = ENXIO;
918991554f2SKenneth D. Merry 			goto out;
919991554f2SKenneth D. Merry 		}
9204ab1cdc5SScott Long 	}
921327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
922327f2e6cSStephen McConnell 	    __func__, id);
923991554f2SKenneth D. Merry 
924327f2e6cSStephen McConnell 	/*
925327f2e6cSStephen McConnell 	 * Only do the ID check and reuse check if the target is not from a
926327f2e6cSStephen McConnell 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
927327f2e6cSStephen McConnell 	 * when a volume is deleted because the mapping entry for the PD will
928327f2e6cSStephen McConnell 	 * still be in the mapping table. The ID check should not be done here
929327f2e6cSStephen McConnell 	 * either since this PD is already being used.
930327f2e6cSStephen McConnell 	 */
931327f2e6cSStephen McConnell 	targ = &sassc->targets[id];
932327f2e6cSStephen McConnell 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
933991554f2SKenneth D. Merry 		if (mprsas_check_id(sassc, id) != 0) {
934757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
935757ff642SScott Long 			    "Excluding target id %d\n", id);
936991554f2SKenneth D. Merry 			error = ENXIO;
937991554f2SKenneth D. Merry 			goto out;
938991554f2SKenneth D. Merry 		}
939991554f2SKenneth D. Merry 
9404ab1cdc5SScott Long 		if (targ->handle != 0x0) {
941327f2e6cSStephen McConnell 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
942327f2e6cSStephen McConnell 			    "target id %d handle 0x%04x\n", id, targ->handle);
9434ab1cdc5SScott Long 			error = ENXIO;
9444ab1cdc5SScott Long 			goto out;
9454ab1cdc5SScott Long 		}
946327f2e6cSStephen McConnell 	}
9474ab1cdc5SScott Long 
948991554f2SKenneth D. Merry 	targ->devinfo = device_info;
949991554f2SKenneth D. Merry 	targ->devname = le32toh(config_page.DeviceName.High);
950991554f2SKenneth D. Merry 	targ->devname = (targ->devname << 32) |
951991554f2SKenneth D. Merry 	    le32toh(config_page.DeviceName.Low);
952991554f2SKenneth D. Merry 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
953991554f2SKenneth D. Merry 	targ->encl_slot = le16toh(config_page.Slot);
954991554f2SKenneth D. Merry 	targ->encl_level = config_page.EnclosureLevel;
955991554f2SKenneth D. Merry 	targ->connector_name[0] = config_page.ConnectorName[0];
956991554f2SKenneth D. Merry 	targ->connector_name[1] = config_page.ConnectorName[1];
957991554f2SKenneth D. Merry 	targ->connector_name[2] = config_page.ConnectorName[2];
958991554f2SKenneth D. Merry 	targ->connector_name[3] = config_page.ConnectorName[3];
959991554f2SKenneth D. Merry 	targ->handle = handle;
960991554f2SKenneth D. Merry 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
961991554f2SKenneth D. Merry 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
962991554f2SKenneth D. Merry 	targ->parent_sasaddr = le64toh(parent_sas_address);
963991554f2SKenneth D. Merry 	targ->parent_devinfo = parent_devinfo;
964991554f2SKenneth D. Merry 	targ->tid = id;
965991554f2SKenneth D. Merry 	targ->linkrate = (linkrate>>4);
966991554f2SKenneth D. Merry 	targ->flags = 0;
967a2c14879SStephen McConnell 	if (is_SATA_SSD) {
968a2c14879SStephen McConnell 		targ->flags = MPR_TARGET_IS_SATA_SSD;
969a2c14879SStephen McConnell 	}
97067feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
97167feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
97267feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
97367feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
974991554f2SKenneth D. Merry 		targ->scsi_req_desc_type =
975991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
976991554f2SKenneth D. Merry 	}
977991554f2SKenneth D. Merry 	if (le16toh(config_page.Flags) &
978991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
979991554f2SKenneth D. Merry 		targ->encl_level_valid = TRUE;
980991554f2SKenneth D. Merry 	}
981991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
982991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
983991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
984991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
985991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
986991554f2SKenneth D. Merry 		free(lun, M_MPR);
987991554f2SKenneth D. Merry 	}
988991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
989991554f2SKenneth D. Merry 
990991554f2SKenneth D. Merry 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
991a2c14879SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
992991554f2SKenneth D. Merry 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
993991554f2SKenneth D. Merry 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
994991554f2SKenneth D. Merry 	    targ->handle, targ->encl_handle, targ->encl_slot);
995991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
996a2c14879SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
997991554f2SKenneth D. Merry 		    "and connector name (%4s)\n", targ->encl_level,
998991554f2SKenneth D. Merry 		    targ->connector_name);
999991554f2SKenneth D. Merry 	}
1000991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
1001991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1002a2c14879SStephen McConnell 
1003a2c14879SStephen McConnell 	/*
1004a2c14879SStephen McConnell 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
1005a2c14879SStephen McConnell 	 * If so, send a Target Reset TM to the target that was just created.
1006a2c14879SStephen McConnell 	 * An Abort Task TM should be used instead of a Target Reset, but that
1007a2c14879SStephen McConnell 	 * would be much more difficult because targets have not been fully
1008a2c14879SStephen McConnell 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
1009175ad3d0SKenneth D. Merry 	 * target instead of the LUN.  The commands should complete once
1010175ad3d0SKenneth D. Merry 	 * the target has been reset.
1011a2c14879SStephen McConnell 	 */
1012a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1013a2c14879SStephen McConnell 		cm = &sc->commands[i];
1014a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1015a2c14879SStephen McConnell 			targ->timeouts++;
10168fe7bf06SWarner Losh 			cm->cm_flags |= MPR_CM_FLAGS_TIMEDOUT;
1017a2c14879SStephen McConnell 
1018a2c14879SStephen McConnell 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1019a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1020a2c14879SStephen McConnell 				    "Reset for stuck SATA identify command "
1021a2c14879SStephen McConnell 				    "(cm = %p)\n", __func__, cm);
1022a2c14879SStephen McConnell 				targ->tm->cm_targ = targ;
1023a2c14879SStephen McConnell 				mprsas_send_reset(sc, targ->tm,
1024a2c14879SStephen McConnell 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1025a2c14879SStephen McConnell 			} else {
1026a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
10277a2a6a1aSStephen McConnell 				    "tm for Target Reset after SATA ID command "
10287a2a6a1aSStephen McConnell 				    "timed out (cm %p)\n", cm);
1029a2c14879SStephen McConnell 			}
1030a2c14879SStephen McConnell 			/*
1031a2c14879SStephen McConnell 			 * No need to check for more since the target is
1032a2c14879SStephen McConnell 			 * already being reset.
1033a2c14879SStephen McConnell 			 */
1034a2c14879SStephen McConnell 			break;
1035a2c14879SStephen McConnell 		}
1036a2c14879SStephen McConnell 	}
1037991554f2SKenneth D. Merry out:
1038991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1039991554f2SKenneth D. Merry 	return (error);
1040991554f2SKenneth D. Merry }
1041991554f2SKenneth D. Merry 
1042991554f2SKenneth D. Merry int
1043991554f2SKenneth D. Merry mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1044a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1045991554f2SKenneth D. Merry {
1046991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t mpi_reply;
1047991554f2SKenneth D. Merry 	int i, rc, try_count;
1048991554f2SKenneth D. Merry 	u32 *bufferptr;
1049991554f2SKenneth D. Merry 	union _sata_sas_address hash_address;
1050991554f2SKenneth D. Merry 	struct _ata_identify_device_data ata_identify;
1051991554f2SKenneth D. Merry 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1052991554f2SKenneth D. Merry 	u32 ioc_status;
1053991554f2SKenneth D. Merry 	u8 sas_status;
1054991554f2SKenneth D. Merry 
1055991554f2SKenneth D. Merry 	memset(&ata_identify, 0, sizeof(ata_identify));
10564e02badbSAlan Somers 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1057991554f2SKenneth D. Merry 	try_count = 0;
1058991554f2SKenneth D. Merry 	do {
1059991554f2SKenneth D. Merry 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1060991554f2SKenneth D. Merry 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1061991554f2SKenneth D. Merry 		try_count++;
1062991554f2SKenneth D. Merry 		ioc_status = le16toh(mpi_reply.IOCStatus)
1063991554f2SKenneth D. Merry 		    & MPI2_IOCSTATUS_MASK;
1064991554f2SKenneth D. Merry 		sas_status = mpi_reply.SASStatus;
1065a92fe027SAlan Somers 		switch (ioc_status) {
1066a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SUCCESS:
1067a92fe027SAlan Somers 			break;
1068a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1069a92fe027SAlan Somers 			/* No sense sleeping.  this error won't get better */
1070a92fe027SAlan Somers 			break;
1071a92fe027SAlan Somers 		default:
1072a2c14879SStephen McConnell 			if (sc->spinup_wait_time > 0) {
1073a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1074a2c14879SStephen McConnell 				    "after SATA ID error to wait for spinup\n",
1075a2c14879SStephen McConnell 				    sc->spinup_wait_time);
1076a2c14879SStephen McConnell 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1077a2c14879SStephen McConnell 				    "mprid", sc->spinup_wait_time * hz);
1078a2c14879SStephen McConnell 			}
1079a2c14879SStephen McConnell 		}
1080a92fe027SAlan Somers 	} while (((rc && (rc != EWOULDBLOCK)) ||
10817a2a6a1aSStephen McConnell 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1082a92fe027SAlan Somers 	    || sas_status) && (try_count < 5));
1083991554f2SKenneth D. Merry 
1084991554f2SKenneth D. Merry 	if (rc == 0 && !ioc_status && !sas_status) {
1085991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1086991554f2SKenneth D. Merry 		    "successfully for handle = 0x%x with try_count = %d\n",
1087991554f2SKenneth D. Merry 		    __func__, handle, try_count);
1088991554f2SKenneth D. Merry 	} else {
1089991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1090991554f2SKenneth D. Merry 		    __func__, handle);
1091991554f2SKenneth D. Merry 		return -1;
1092991554f2SKenneth D. Merry 	}
1093991554f2SKenneth D. Merry 	/* Copy & byteswap the 40 byte model number to a buffer */
1094991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1095991554f2SKenneth D. Merry 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1096991554f2SKenneth D. Merry 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1097991554f2SKenneth D. Merry 	}
1098991554f2SKenneth D. Merry 	/* Copy & byteswap the 20 byte serial number to a buffer */
1099991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1100991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i] =
1101991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i + 1];
1102991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i + 1] =
1103991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i];
1104991554f2SKenneth D. Merry 	}
1105991554f2SKenneth D. Merry 	bufferptr = (u32 *)buffer;
1106991554f2SKenneth D. Merry 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1107991554f2SKenneth D. Merry 	 * so loop through the first 56 bytes (7*8),
1108991554f2SKenneth D. Merry 	 * and then add in the last dword.
1109991554f2SKenneth D. Merry 	 */
1110991554f2SKenneth D. Merry 	hash_address.word.low  = 0;
1111991554f2SKenneth D. Merry 	hash_address.word.high = 0;
1112991554f2SKenneth D. Merry 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1113991554f2SKenneth D. Merry 		hash_address.word.low += *bufferptr;
1114991554f2SKenneth D. Merry 		bufferptr++;
1115991554f2SKenneth D. Merry 		hash_address.word.high += *bufferptr;
1116991554f2SKenneth D. Merry 		bufferptr++;
1117991554f2SKenneth D. Merry 	}
1118991554f2SKenneth D. Merry 	/* Add the last dword */
1119991554f2SKenneth D. Merry 	hash_address.word.low += *bufferptr;
1120991554f2SKenneth D. Merry 	/* Make sure the hash doesn't start with 5, because it could clash
1121991554f2SKenneth D. Merry 	 * with a SAS address. Change 5 to a D.
1122991554f2SKenneth D. Merry 	 */
1123991554f2SKenneth D. Merry 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1124991554f2SKenneth D. Merry 		hash_address.word.high |= 0x00000080;
1125991554f2SKenneth D. Merry 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1126991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1127991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1128991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1129991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[7];
1130a2c14879SStephen McConnell 	if (ata_identify.rotational_speed == 1) {
1131a2c14879SStephen McConnell 		*is_SATA_SSD = 1;
1132a2c14879SStephen McConnell 	}
1133a2c14879SStephen McConnell 
1134991554f2SKenneth D. Merry 	return 0;
1135991554f2SKenneth D. Merry }
1136991554f2SKenneth D. Merry 
1137991554f2SKenneth D. Merry static int
1138991554f2SKenneth D. Merry mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1139991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1140991554f2SKenneth D. Merry {
1141991554f2SKenneth D. Merry 	Mpi2SataPassthroughRequest_t *mpi_request;
1142991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t *reply;
1143991554f2SKenneth D. Merry 	struct mpr_command *cm;
1144991554f2SKenneth D. Merry 	char *buffer;
1145991554f2SKenneth D. Merry 	int error = 0;
1146991554f2SKenneth D. Merry 
1147991554f2SKenneth D. Merry 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1148991554f2SKenneth D. Merry 	if (!buffer)
1149991554f2SKenneth D. Merry 		return ENOMEM;
1150991554f2SKenneth D. Merry 
1151991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1152991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1153991554f2SKenneth D. Merry 		return (EBUSY);
1154991554f2SKenneth D. Merry 	}
1155991554f2SKenneth D. Merry 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1156991554f2SKenneth D. Merry 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1157991554f2SKenneth D. Merry 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1158991554f2SKenneth D. Merry 	mpi_request->VF_ID = 0;
1159991554f2SKenneth D. Merry 	mpi_request->DevHandle = htole16(handle);
1160991554f2SKenneth D. Merry 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1161991554f2SKenneth D. Merry 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1162991554f2SKenneth D. Merry 	mpi_request->DataLength = htole32(sz);
1163991554f2SKenneth D. Merry 	mpi_request->CommandFIS[0] = 0x27;
1164991554f2SKenneth D. Merry 	mpi_request->CommandFIS[1] = 0x80;
1165991554f2SKenneth D. Merry 	mpi_request->CommandFIS[2] =  (devinfo &
1166991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1167991554f2SKenneth D. Merry 	cm->cm_sge = &mpi_request->SGL;
1168991554f2SKenneth D. Merry 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1169991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1170991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1171991554f2SKenneth D. Merry 	cm->cm_data = buffer;
1172991554f2SKenneth D. Merry 	cm->cm_length = htole32(sz);
1173a2c14879SStephen McConnell 
1174a2c14879SStephen McConnell 	/*
117586312e46SConrad Meyer 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
117686312e46SConrad Meyer 	 * This fixes a problem where the FW does not send a reply sometimes
1177a2c14879SStephen McConnell 	 * when a bad disk is in the topology. So, this is used to timeout the
1178a2c14879SStephen McConnell 	 * command so that processing can continue normally.
1179a2c14879SStephen McConnell 	 */
118086312e46SConrad Meyer 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1181a2c14879SStephen McConnell 
118286312e46SConrad Meyer 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
118386312e46SConrad Meyer 
118486312e46SConrad Meyer 	/* mprsas_ata_id_timeout does not reset controller */
118586312e46SConrad Meyer 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
118686312e46SConrad Meyer 
1187991554f2SKenneth D. Merry 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1188991554f2SKenneth D. Merry 	if (error || (reply == NULL)) {
1189991554f2SKenneth D. Merry 		/* FIXME */
1190991554f2SKenneth D. Merry 		/*
1191991554f2SKenneth D. Merry 		 * If the request returns an error then we need to do a diag
1192991554f2SKenneth D. Merry 		 * reset
1193991554f2SKenneth D. Merry 		 */
1194aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1195e7ef108cSWarner Losh 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
1196aeb9ac0dSScott Long 		    error);
1197991554f2SKenneth D. Merry 		error = ENXIO;
1198991554f2SKenneth D. Merry 		goto out;
1199991554f2SKenneth D. Merry 	}
1200991554f2SKenneth D. Merry 	bcopy(buffer, id_buffer, sz);
1201991554f2SKenneth D. Merry 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1202991554f2SKenneth D. Merry 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1203991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
1204aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1205aeb9ac0dSScott Long 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1206aeb9ac0dSScott Long 		    handle, reply->IOCStatus);
1207991554f2SKenneth D. Merry 		error = ENXIO;
1208991554f2SKenneth D. Merry 		goto out;
1209991554f2SKenneth D. Merry 	}
1210991554f2SKenneth D. Merry out:
1211a2c14879SStephen McConnell 	/*
1212a2c14879SStephen McConnell 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
1213175ad3d0SKenneth D. Merry 	 * it.  The command and buffer will be freed after we send a Target
1214175ad3d0SKenneth D. Merry 	 * Reset TM and the command comes back from the controller.
1215a2c14879SStephen McConnell 	 */
12168277ce2bSConrad Meyer 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1217991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1218991554f2SKenneth D. Merry 		free(buffer, M_MPR);
121986312e46SConrad Meyer 	}
1220991554f2SKenneth D. Merry 	return (error);
1221991554f2SKenneth D. Merry }
1222991554f2SKenneth D. Merry 
1223175ad3d0SKenneth D. Merry /*
1224175ad3d0SKenneth D. Merry  * This is completion handler to make sure that commands and allocated
1225175ad3d0SKenneth D. Merry  * buffers get freed when timed out SATA ID commands finally complete after
1226175ad3d0SKenneth D. Merry  * we've reset the target.  In the normal case, we wait for the command to
1227175ad3d0SKenneth D. Merry  * complete.
1228175ad3d0SKenneth D. Merry  */
1229175ad3d0SKenneth D. Merry static void
1230175ad3d0SKenneth D. Merry mprsas_ata_id_complete(struct mpr_softc *sc, struct mpr_command *cm)
1231175ad3d0SKenneth D. Merry {
1232175ad3d0SKenneth D. Merry 	mpr_dprint(sc, MPR_INFO, "%s ATA ID completed late cm %p sc %p\n",
1233175ad3d0SKenneth D. Merry 	    __func__, cm, sc);
1234175ad3d0SKenneth D. Merry 
1235175ad3d0SKenneth D. Merry 	free(cm->cm_data, M_MPR);
1236175ad3d0SKenneth D. Merry 	mpr_free_command(sc, cm);
1237175ad3d0SKenneth D. Merry }
1238175ad3d0SKenneth D. Merry 
1239a2c14879SStephen McConnell static void
124086312e46SConrad Meyer mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1241a2c14879SStephen McConnell {
1242a2c14879SStephen McConnell 
124386312e46SConrad Meyer 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1244a2c14879SStephen McConnell 	    __func__, cm, sc);
1245a2c14879SStephen McConnell 
1246a2c14879SStephen McConnell 	/*
124786312e46SConrad Meyer 	 * The Abort Task cannot be sent from here because the driver has not
124886312e46SConrad Meyer 	 * completed setting up targets.  Instead, the command is flagged so
12498fe7bf06SWarner Losh 	 * that special handling will be used to send the abort. Now that
12508fe7bf06SWarner Losh 	 * this command has timed out, it's no longer in the queue.
1251a2c14879SStephen McConnell 	 */
1252a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1253175ad3d0SKenneth D. Merry 
1254175ad3d0SKenneth D. Merry 	/*
1255175ad3d0SKenneth D. Merry 	 * Since we will no longer be waiting for the command to complete,
1256175ad3d0SKenneth D. Merry 	 * set a completion handler to make sure we free all resources.
1257175ad3d0SKenneth D. Merry 	 */
1258175ad3d0SKenneth D. Merry 	cm->cm_complete = mprsas_ata_id_complete;
1259a2c14879SStephen McConnell }
1260a2c14879SStephen McConnell 
1261991554f2SKenneth D. Merry static int
126267feec50SStephen McConnell mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
126367feec50SStephen McConnell {
126467feec50SStephen McConnell 	char devstring[80];
126567feec50SStephen McConnell 	struct mprsas_softc *sassc;
126667feec50SStephen McConnell 	struct mprsas_target *targ;
126767feec50SStephen McConnell 	Mpi2ConfigReply_t mpi_reply;
126867feec50SStephen McConnell 	Mpi26PCIeDevicePage0_t config_page;
126967feec50SStephen McConnell 	Mpi26PCIeDevicePage2_t config_page2;
127067feec50SStephen McConnell 	uint64_t pcie_wwid, parent_wwid = 0;
127167feec50SStephen McConnell 	u32 device_info, parent_devinfo = 0;
127267feec50SStephen McConnell 	unsigned int id;
127367feec50SStephen McConnell 	int error = 0;
127467feec50SStephen McConnell 	struct mprsas_lun *lun;
127567feec50SStephen McConnell 
127667feec50SStephen McConnell 	sassc = sc->sassc;
127767feec50SStephen McConnell 	mprsas_startup_increment(sassc);
127867feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
127967feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
128067feec50SStephen McConnell 		printf("%s: error reading PCIe device page0\n", __func__);
128167feec50SStephen McConnell 		error = ENXIO;
128267feec50SStephen McConnell 		goto out;
128367feec50SStephen McConnell 	}
128467feec50SStephen McConnell 
128567feec50SStephen McConnell 	device_info = le32toh(config_page.DeviceInfo);
128667feec50SStephen McConnell 
128767feec50SStephen McConnell 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
128867feec50SStephen McConnell 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
128967feec50SStephen McConnell 		Mpi2ConfigReply_t tmp_mpi_reply;
129067feec50SStephen McConnell 		Mpi26PCIeDevicePage0_t parent_config_page;
129167feec50SStephen McConnell 
129267feec50SStephen McConnell 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
129367feec50SStephen McConnell 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
129467feec50SStephen McConnell 		     le16toh(config_page.ParentDevHandle)))) {
129567feec50SStephen McConnell 			printf("%s: error reading PCIe device %#x page0\n",
129667feec50SStephen McConnell 			    __func__, le16toh(config_page.ParentDevHandle));
129767feec50SStephen McConnell 		} else {
129867feec50SStephen McConnell 			parent_wwid = parent_config_page.WWID.High;
129967feec50SStephen McConnell 			parent_wwid = (parent_wwid << 32) |
130067feec50SStephen McConnell 			    parent_config_page.WWID.Low;
130167feec50SStephen McConnell 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
130267feec50SStephen McConnell 		}
130367feec50SStephen McConnell 	}
130467feec50SStephen McConnell 	/* TODO Check proper endianness */
130567feec50SStephen McConnell 	pcie_wwid = config_page.WWID.High;
130667feec50SStephen McConnell 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
130767feec50SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
130867feec50SStephen McConnell 	    pcie_wwid);
130967feec50SStephen McConnell 
131067feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
131167feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
131267feec50SStephen McConnell 		printf("%s: error reading PCIe device page2\n", __func__);
131367feec50SStephen McConnell 		error = ENXIO;
131467feec50SStephen McConnell 		goto out;
131567feec50SStephen McConnell 	}
131667feec50SStephen McConnell 
1317327f2e6cSStephen McConnell 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
131867feec50SStephen McConnell 	if (id == MPR_MAP_BAD_ID) {
1319327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1320327f2e6cSStephen McConnell 		    "Could not get ID for device with handle 0x%04x\n",
1321327f2e6cSStephen McConnell 		    __FILE__, __LINE__, __func__, handle);
132267feec50SStephen McConnell 		error = ENXIO;
132367feec50SStephen McConnell 		goto out;
132467feec50SStephen McConnell 	}
1325327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1326327f2e6cSStephen McConnell 	    __func__, id);
132767feec50SStephen McConnell 
132867feec50SStephen McConnell 	if (mprsas_check_id(sassc, id) != 0) {
1329757ff642SScott Long 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1330757ff642SScott Long 		    "Excluding target id %d\n", id);
133167feec50SStephen McConnell 		error = ENXIO;
133267feec50SStephen McConnell 		goto out;
133367feec50SStephen McConnell 	}
133467feec50SStephen McConnell 
133567feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
133667feec50SStephen McConnell 	    pcie_wwid);
133767feec50SStephen McConnell 	targ = &sassc->targets[id];
133867feec50SStephen McConnell 	targ->devinfo = device_info;
133967feec50SStephen McConnell 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
134067feec50SStephen McConnell 	targ->encl_slot = le16toh(config_page.Slot);
134167feec50SStephen McConnell 	targ->encl_level = config_page.EnclosureLevel;
134267feec50SStephen McConnell 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
134367feec50SStephen McConnell 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
134467feec50SStephen McConnell 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
134567feec50SStephen McConnell 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
134667feec50SStephen McConnell 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
134767feec50SStephen McConnell 	targ->MDTS = config_page2.MaximumDataTransferSize;
134889d1c21fSKashyap D Desai 	if (targ->is_nvme)
134989d1c21fSKashyap D Desai 		targ->controller_reset_timeout = config_page2.ControllerResetTO;
135067feec50SStephen McConnell 	/*
135167feec50SStephen McConnell 	 * Assume always TRUE for encl_level_valid because there is no valid
135267feec50SStephen McConnell 	 * flag for PCIe.
135367feec50SStephen McConnell 	 */
135467feec50SStephen McConnell 	targ->encl_level_valid = TRUE;
135567feec50SStephen McConnell 	targ->handle = handle;
135667feec50SStephen McConnell 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
135767feec50SStephen McConnell 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
135867feec50SStephen McConnell 	targ->parent_sasaddr = le64toh(parent_wwid);
135967feec50SStephen McConnell 	targ->parent_devinfo = parent_devinfo;
136067feec50SStephen McConnell 	targ->tid = id;
136167feec50SStephen McConnell 	targ->linkrate = linkrate;
136267feec50SStephen McConnell 	targ->flags = 0;
136367feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
136467feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
136567feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
136667feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
136767feec50SStephen McConnell 		targ->scsi_req_desc_type =
136867feec50SStephen McConnell 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
136967feec50SStephen McConnell 	}
137067feec50SStephen McConnell 	TAILQ_INIT(&targ->commands);
137167feec50SStephen McConnell 	TAILQ_INIT(&targ->timedout_commands);
137267feec50SStephen McConnell 	while (!SLIST_EMPTY(&targ->luns)) {
137367feec50SStephen McConnell 		lun = SLIST_FIRST(&targ->luns);
137467feec50SStephen McConnell 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
137567feec50SStephen McConnell 		free(lun, M_MPR);
137667feec50SStephen McConnell 	}
137767feec50SStephen McConnell 	SLIST_INIT(&targ->luns);
137867feec50SStephen McConnell 
137967feec50SStephen McConnell 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
138067feec50SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
138167feec50SStephen McConnell 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
138267feec50SStephen McConnell 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
138367feec50SStephen McConnell 	    targ->handle, targ->encl_handle, targ->encl_slot);
138467feec50SStephen McConnell 	if (targ->encl_level_valid) {
138567feec50SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
138667feec50SStephen McConnell 		    "and connector name (%4s)\n", targ->encl_level,
138767feec50SStephen McConnell 		    targ->connector_name);
138867feec50SStephen McConnell 	}
138967feec50SStephen McConnell 	mprsas_rescan_target(sc, targ);
139067feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
139167feec50SStephen McConnell 
139267feec50SStephen McConnell out:
139367feec50SStephen McConnell 	mprsas_startup_decrement(sassc);
139467feec50SStephen McConnell 	return (error);
139567feec50SStephen McConnell }
139667feec50SStephen McConnell 
139767feec50SStephen McConnell static int
1398991554f2SKenneth D. Merry mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1399991554f2SKenneth D. Merry {
1400991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1401991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1402991554f2SKenneth D. Merry 	u64 wwid;
1403991554f2SKenneth D. Merry 	unsigned int id;
1404991554f2SKenneth D. Merry 	int error = 0;
1405991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1406991554f2SKenneth D. Merry 
1407991554f2SKenneth D. Merry 	sassc = sc->sassc;
1408991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
1409991554f2SKenneth D. Merry 	/* wwid is endian safe */
1410991554f2SKenneth D. Merry 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1411991554f2SKenneth D. Merry 	if (!wwid) {
1412991554f2SKenneth D. Merry 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1413991554f2SKenneth D. Merry 		    __func__);
1414991554f2SKenneth D. Merry 		error = ENXIO;
1415991554f2SKenneth D. Merry 		goto out;
1416991554f2SKenneth D. Merry 	}
1417991554f2SKenneth D. Merry 
1418327f2e6cSStephen McConnell 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1419991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
1420991554f2SKenneth D. Merry 		printf("%s: could not get ID for volume with handle 0x%04x and "
1421991554f2SKenneth D. Merry 		    "WWID 0x%016llx\n", __func__, handle,
1422991554f2SKenneth D. Merry 		    (unsigned long long)wwid);
1423991554f2SKenneth D. Merry 		error = ENXIO;
1424991554f2SKenneth D. Merry 		goto out;
1425991554f2SKenneth D. Merry 	}
1426991554f2SKenneth D. Merry 
1427991554f2SKenneth D. Merry 	targ = &sassc->targets[id];
1428991554f2SKenneth D. Merry 	targ->tid = id;
1429991554f2SKenneth D. Merry 	targ->handle = handle;
1430991554f2SKenneth D. Merry 	targ->devname = wwid;
1431*e3c5965cSAlexander Motin 	targ->flags = MPR_TARGET_FLAGS_VOLUME;
1432991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
1433991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
1434991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
1435991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
1436991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1437991554f2SKenneth D. Merry 		free(lun, M_MPR);
1438991554f2SKenneth D. Merry 	}
1439991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
1440991554f2SKenneth D. Merry 	mprsas_rescan_target(sc, targ);
1441991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1442991554f2SKenneth D. Merry 	    targ->tid, wwid);
1443991554f2SKenneth D. Merry out:
1444991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1445991554f2SKenneth D. Merry 	return (error);
1446991554f2SKenneth D. Merry }
1447991554f2SKenneth D. Merry 
1448991554f2SKenneth D. Merry /**
1449991554f2SKenneth D. Merry  * mprsas_SSU_to_SATA_devices
1450991554f2SKenneth D. Merry  * @sc: per adapter object
1451991554f2SKenneth D. Merry  *
1452991554f2SKenneth D. Merry  * Looks through the target list and issues a StartStopUnit SCSI command to each
1453991554f2SKenneth D. Merry  * SATA direct-access device.  This helps to ensure that data corruption is
1454991554f2SKenneth D. Merry  * avoided when the system is being shut down.  This must be called after the IR
1455991554f2SKenneth D. Merry  * System Shutdown RAID Action is sent if in IR mode.
1456991554f2SKenneth D. Merry  *
1457991554f2SKenneth D. Merry  * Return nothing.
1458991554f2SKenneth D. Merry  */
1459991554f2SKenneth D. Merry static void
1460acc173a6SWarner Losh mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto)
1461991554f2SKenneth D. Merry {
1462991554f2SKenneth D. Merry 	struct mprsas_softc *sassc = sc->sassc;
1463991554f2SKenneth D. Merry 	union ccb *ccb;
1464991554f2SKenneth D. Merry 	path_id_t pathid = cam_sim_path(sassc->sim);
1465991554f2SKenneth D. Merry 	target_id_t targetid;
1466991554f2SKenneth D. Merry 	struct mprsas_target *target;
1467991554f2SKenneth D. Merry 	char path_str[64];
1468acc173a6SWarner Losh 	int timeout;
1469991554f2SKenneth D. Merry 
147063941e98SKenneth D. Merry 	mpr_lock(sc);
147163941e98SKenneth D. Merry 
1472991554f2SKenneth D. Merry 	/*
1473a2c14879SStephen McConnell 	 * For each target, issue a StartStopUnit command to stop the device.
1474991554f2SKenneth D. Merry 	 */
1475991554f2SKenneth D. Merry 	sc->SSU_started = TRUE;
1476991554f2SKenneth D. Merry 	sc->SSU_refcount = 0;
1477327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1478991554f2SKenneth D. Merry 		target = &sassc->targets[targetid];
1479991554f2SKenneth D. Merry 		if (target->handle == 0x0) {
1480991554f2SKenneth D. Merry 			continue;
1481991554f2SKenneth D. Merry 		}
1482991554f2SKenneth D. Merry 
1483fa699bb2SAlan Somers 		/*
1484fa699bb2SAlan Somers 		 * The stop_at_shutdown flag will be set if this device is
1485fa699bb2SAlan Somers 		 * a SATA direct-access end device.
1486fa699bb2SAlan Somers 		 */
1487fa699bb2SAlan Somers 		if (target->stop_at_shutdown) {
1488991554f2SKenneth D. Merry 			ccb = xpt_alloc_ccb_nowait();
1489991554f2SKenneth D. Merry 			if (ccb == NULL) {
149067feec50SStephen McConnell 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
149167feec50SStephen McConnell 				    "to stop unit.\n");
1492991554f2SKenneth D. Merry 				return;
1493991554f2SKenneth D. Merry 			}
1494991554f2SKenneth D. Merry 
1495a2c14879SStephen McConnell 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1496a2c14879SStephen McConnell 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1497a2c14879SStephen McConnell 			    CAM_REQ_CMP) {
1498a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1499a2c14879SStephen McConnell 				    "path to stop unit.\n");
1500991554f2SKenneth D. Merry 				xpt_free_ccb(ccb);
1501991554f2SKenneth D. Merry 				return;
1502991554f2SKenneth D. Merry 			}
1503991554f2SKenneth D. Merry 			xpt_path_string(ccb->ccb_h.path, path_str,
1504991554f2SKenneth D. Merry 			    sizeof(path_str));
1505991554f2SKenneth D. Merry 
1506a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1507a2c14879SStephen McConnell 			    "handle %d\n", path_str, target->handle);
1508991554f2SKenneth D. Merry 
1509991554f2SKenneth D. Merry 			/*
1510a2c14879SStephen McConnell 			 * Issue a START STOP UNIT command for the target.
1511a2c14879SStephen McConnell 			 * Increment the SSU counter to be used to count the
1512a2c14879SStephen McConnell 			 * number of required replies.
1513991554f2SKenneth D. Merry 			 */
1514a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1515991554f2SKenneth D. Merry 			sc->SSU_refcount++;
1516991554f2SKenneth D. Merry 			ccb->ccb_h.target_id =
1517991554f2SKenneth D. Merry 			    xpt_path_target_id(ccb->ccb_h.path);
1518991554f2SKenneth D. Merry 			ccb->ccb_h.ppriv_ptr1 = sassc;
1519991554f2SKenneth D. Merry 			scsi_start_stop(&ccb->csio,
1520991554f2SKenneth D. Merry 			    /*retries*/0,
1521991554f2SKenneth D. Merry 			    mprsas_stop_unit_done,
1522991554f2SKenneth D. Merry 			    MSG_SIMPLE_Q_TAG,
1523991554f2SKenneth D. Merry 			    /*start*/FALSE,
1524991554f2SKenneth D. Merry 			    /*load/eject*/0,
1525991554f2SKenneth D. Merry 			    /*immediate*/FALSE,
1526991554f2SKenneth D. Merry 			    MPR_SENSE_LEN,
1527991554f2SKenneth D. Merry 			    /*timeout*/10000);
1528991554f2SKenneth D. Merry 			xpt_action(ccb);
1529991554f2SKenneth D. Merry 		}
1530991554f2SKenneth D. Merry 	}
1531991554f2SKenneth D. Merry 
153263941e98SKenneth D. Merry 	mpr_unlock(sc);
153363941e98SKenneth D. Merry 
1534991554f2SKenneth D. Merry 	/*
1535acc173a6SWarner Losh 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1536acc173a6SWarner Losh 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1537991554f2SKenneth D. Merry 	 */
1538acc173a6SWarner Losh 	timeout = 600;
1539acc173a6SWarner Losh 	if (howto & RB_NOSYNC)
1540acc173a6SWarner Losh 		timeout = 100;
1541991554f2SKenneth D. Merry 
1542acc173a6SWarner Losh 	/*
1543acc173a6SWarner Losh 	 * Wait until all of the SSU commands have completed or time
1544acc173a6SWarner Losh 	 * has expired. Pause for 100ms each time through.  If any
1545acc173a6SWarner Losh 	 * command times out, the target will be reset in the SCSI
1546acc173a6SWarner Losh 	 * command timeout routine.
1547acc173a6SWarner Losh 	 */
1548acc173a6SWarner Losh 	while (sc->SSU_refcount > 0) {
1549acc173a6SWarner Losh 		pause("mprwait", hz/10);
1550acc173a6SWarner Losh 		if (SCHEDULER_STOPPED())
1551acc173a6SWarner Losh 			xpt_sim_poll(sassc->sim);
1552acc173a6SWarner Losh 
1553acc173a6SWarner Losh 		if (--timeout == 0) {
1554a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1555991554f2SKenneth D. Merry 			    "for SSU commands to complete.\n");
1556991554f2SKenneth D. Merry 			break;
1557991554f2SKenneth D. Merry 		}
1558991554f2SKenneth D. Merry 	}
1559991554f2SKenneth D. Merry }
1560991554f2SKenneth D. Merry 
1561991554f2SKenneth D. Merry static void
1562991554f2SKenneth D. Merry mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1563991554f2SKenneth D. Merry {
1564991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1565991554f2SKenneth D. Merry 	char path_str[64];
1566991554f2SKenneth D. Merry 
15675d21655fSStephen McConnell 	if (done_ccb == NULL)
15685d21655fSStephen McConnell 		return;
15695d21655fSStephen McConnell 
1570991554f2SKenneth D. Merry 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1571991554f2SKenneth D. Merry 
1572991554f2SKenneth D. Merry 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1573991554f2SKenneth D. Merry 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1574991554f2SKenneth D. Merry 	    path_str);
1575991554f2SKenneth D. Merry 
1576991554f2SKenneth D. Merry 	/*
1577991554f2SKenneth D. Merry 	 * Nothing more to do except free the CCB and path.  If the command
1578991554f2SKenneth D. Merry 	 * timed out, an abort reset, then target reset will be issued during
1579991554f2SKenneth D. Merry 	 * the SCSI Command process.
1580991554f2SKenneth D. Merry 	 */
1581991554f2SKenneth D. Merry 	xpt_free_path(done_ccb->ccb_h.path);
1582991554f2SKenneth D. Merry 	xpt_free_ccb(done_ccb);
1583991554f2SKenneth D. Merry }
1584991554f2SKenneth D. Merry 
1585991554f2SKenneth D. Merry /**
1586991554f2SKenneth D. Merry  * mprsas_ir_shutdown - IR shutdown notification
1587991554f2SKenneth D. Merry  * @sc: per adapter object
1588991554f2SKenneth D. Merry  *
1589991554f2SKenneth D. Merry  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1590991554f2SKenneth D. Merry  * the host system is shutting down.
1591991554f2SKenneth D. Merry  *
1592991554f2SKenneth D. Merry  * Return nothing.
1593991554f2SKenneth D. Merry  */
1594991554f2SKenneth D. Merry void
1595acc173a6SWarner Losh mprsas_ir_shutdown(struct mpr_softc *sc, int howto)
1596991554f2SKenneth D. Merry {
1597991554f2SKenneth D. Merry 	u16 volume_mapping_flags;
1598991554f2SKenneth D. Merry 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1599991554f2SKenneth D. Merry 	struct dev_mapping_table *mt_entry;
1600991554f2SKenneth D. Merry 	u32 start_idx, end_idx;
1601991554f2SKenneth D. Merry 	unsigned int id, found_volume = 0;
1602991554f2SKenneth D. Merry 	struct mpr_command *cm;
1603991554f2SKenneth D. Merry 	Mpi2RaidActionRequest_t	*action;
1604a2c14879SStephen McConnell 	target_id_t targetid;
1605a2c14879SStephen McConnell 	struct mprsas_target *target;
1606991554f2SKenneth D. Merry 
1607991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1608991554f2SKenneth D. Merry 
1609991554f2SKenneth D. Merry 	/* is IR firmware build loaded? */
1610991554f2SKenneth D. Merry 	if (!sc->ir_firmware)
1611991554f2SKenneth D. Merry 		goto out;
1612991554f2SKenneth D. Merry 
1613991554f2SKenneth D. Merry 	/* are there any volumes?  Look at IR target IDs. */
1614991554f2SKenneth D. Merry 	// TODO-later, this should be looked up in the RAID config structure
1615991554f2SKenneth D. Merry 	// when it is implemented.
1616991554f2SKenneth D. Merry 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1617991554f2SKenneth D. Merry 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1618991554f2SKenneth D. Merry 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1619991554f2SKenneth D. Merry 		start_idx = 0;
1620991554f2SKenneth D. Merry 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1621991554f2SKenneth D. Merry 			start_idx = 1;
1622991554f2SKenneth D. Merry 	} else
1623991554f2SKenneth D. Merry 		start_idx = sc->max_devices - sc->max_volumes;
1624991554f2SKenneth D. Merry 	end_idx = start_idx + sc->max_volumes - 1;
1625991554f2SKenneth D. Merry 
1626991554f2SKenneth D. Merry 	for (id = start_idx; id < end_idx; id++) {
1627991554f2SKenneth D. Merry 		mt_entry = &sc->mapping_table[id];
1628991554f2SKenneth D. Merry 		if ((mt_entry->physical_id != 0) &&
1629991554f2SKenneth D. Merry 		    (mt_entry->missing_count == 0)) {
1630991554f2SKenneth D. Merry 			found_volume = 1;
1631991554f2SKenneth D. Merry 			break;
1632991554f2SKenneth D. Merry 		}
1633991554f2SKenneth D. Merry 	}
1634991554f2SKenneth D. Merry 
1635991554f2SKenneth D. Merry 	if (!found_volume)
1636991554f2SKenneth D. Merry 		goto out;
1637991554f2SKenneth D. Merry 
1638991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1639991554f2SKenneth D. Merry 		printf("%s: command alloc failed\n", __func__);
1640991554f2SKenneth D. Merry 		goto out;
1641991554f2SKenneth D. Merry 	}
1642991554f2SKenneth D. Merry 
1643991554f2SKenneth D. Merry 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1644991554f2SKenneth D. Merry 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1645991554f2SKenneth D. Merry 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1646991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1647991554f2SKenneth D. Merry 	mpr_lock(sc);
16486d4ffcb4SKenneth D. Merry 	mpr_wait_command(sc, &cm, 5, CAN_SLEEP);
1649991554f2SKenneth D. Merry 	mpr_unlock(sc);
1650991554f2SKenneth D. Merry 
1651991554f2SKenneth D. Merry 	/*
1652991554f2SKenneth D. Merry 	 * Don't check for reply, just leave.
1653991554f2SKenneth D. Merry 	 */
1654991554f2SKenneth D. Merry 	if (cm)
1655991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1656991554f2SKenneth D. Merry 
1657991554f2SKenneth D. Merry out:
1658a2c14879SStephen McConnell 	/*
1659a2c14879SStephen McConnell 	 * All of the targets must have the correct value set for
1660a2c14879SStephen McConnell 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1661a2c14879SStephen McConnell 	 *
1662a2c14879SStephen McConnell 	 * The possible values for the 'enable_ssu' variable are:
1663a2c14879SStephen McConnell 	 * 0: disable to SSD and HDD
1664a2c14879SStephen McConnell 	 * 1: disable only to HDD (default)
1665a2c14879SStephen McConnell 	 * 2: disable only to SSD
1666a2c14879SStephen McConnell 	 * 3: enable to SSD and HDD
1667a2c14879SStephen McConnell 	 * anything else will default to 1.
1668a2c14879SStephen McConnell 	 */
1669327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1670a2c14879SStephen McConnell 		target = &sc->sassc->targets[targetid];
1671a2c14879SStephen McConnell 		if (target->handle == 0x0) {
1672a2c14879SStephen McConnell 			continue;
1673a2c14879SStephen McConnell 		}
1674a2c14879SStephen McConnell 
1675a2c14879SStephen McConnell 		if (target->supports_SSU) {
1676a2c14879SStephen McConnell 			switch (sc->enable_ssu) {
1677a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1678a2c14879SStephen McConnell 				target->stop_at_shutdown = FALSE;
1679a2c14879SStephen McConnell 				break;
1680a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1681a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1682a2c14879SStephen McConnell 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1683a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1684a2c14879SStephen McConnell 				}
1685a2c14879SStephen McConnell 				break;
1686a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1687a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1688a2c14879SStephen McConnell 				break;
1689a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1690a2c14879SStephen McConnell 			default:
1691a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1692a2c14879SStephen McConnell 				if ((target->flags &
1693a2c14879SStephen McConnell 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1694a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1695a2c14879SStephen McConnell 				}
1696a2c14879SStephen McConnell 				break;
1697a2c14879SStephen McConnell 			}
1698a2c14879SStephen McConnell 		}
1699a2c14879SStephen McConnell 	}
1700acc173a6SWarner Losh 	mprsas_SSU_to_SATA_devices(sc, howto);
1701991554f2SKenneth D. Merry }
1702