xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 8fe7bf064fe2bbde5d71e0e7d6711c6638147775)
1991554f2SKenneth D. Merry /*-
2a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
37a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
446b23587SKashyap D Desai  * Copyright 2000-2020 Broadcom Inc.
5991554f2SKenneth D. Merry  * All rights reserved.
6991554f2SKenneth D. Merry  *
7991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
8991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
9991554f2SKenneth D. Merry  * are met:
10991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
11991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
12991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
13991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
14991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
15991554f2SKenneth D. Merry  *
16991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26991554f2SKenneth D. Merry  * SUCH DAMAGE.
27991554f2SKenneth D. Merry  *
2846b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
29991554f2SKenneth D. Merry  */
30991554f2SKenneth D. Merry 
31991554f2SKenneth D. Merry #include <sys/cdefs.h>
32991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
33991554f2SKenneth D. Merry 
347a2a6a1aSStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
35991554f2SKenneth D. Merry 
36991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
37991554f2SKenneth D. Merry #include <sys/types.h>
38991554f2SKenneth D. Merry #include <sys/param.h>
39991554f2SKenneth D. Merry #include <sys/systm.h>
40991554f2SKenneth D. Merry #include <sys/kernel.h>
41991554f2SKenneth D. Merry #include <sys/selinfo.h>
42991554f2SKenneth D. Merry #include <sys/module.h>
43991554f2SKenneth D. Merry #include <sys/bus.h>
44991554f2SKenneth D. Merry #include <sys/conf.h>
45991554f2SKenneth D. Merry #include <sys/bio.h>
46991554f2SKenneth D. Merry #include <sys/malloc.h>
47991554f2SKenneth D. Merry #include <sys/uio.h>
48991554f2SKenneth D. Merry #include <sys/sysctl.h>
49991554f2SKenneth D. Merry #include <sys/endian.h>
50acc173a6SWarner Losh #include <sys/proc.h>
51991554f2SKenneth D. Merry #include <sys/queue.h>
52991554f2SKenneth D. Merry #include <sys/kthread.h>
53991554f2SKenneth D. Merry #include <sys/taskqueue.h>
54991554f2SKenneth D. Merry #include <sys/sbuf.h>
55acc173a6SWarner Losh #include <sys/reboot.h>
56991554f2SKenneth D. Merry 
57991554f2SKenneth D. Merry #include <machine/bus.h>
58991554f2SKenneth D. Merry #include <machine/resource.h>
59991554f2SKenneth D. Merry #include <sys/rman.h>
60991554f2SKenneth D. Merry 
61991554f2SKenneth D. Merry #include <machine/stdarg.h>
62991554f2SKenneth D. Merry 
63991554f2SKenneth D. Merry #include <cam/cam.h>
64991554f2SKenneth D. Merry #include <cam/cam_ccb.h>
65991554f2SKenneth D. Merry #include <cam/cam_debug.h>
66991554f2SKenneth D. Merry #include <cam/cam_sim.h>
67991554f2SKenneth D. Merry #include <cam/cam_xpt_sim.h>
68991554f2SKenneth D. Merry #include <cam/cam_xpt_periph.h>
69991554f2SKenneth D. Merry #include <cam/cam_periph.h>
70991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
71991554f2SKenneth D. Merry #include <cam/scsi/scsi_message.h>
72991554f2SKenneth D. Merry 
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
74991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
75991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7767feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_raid.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
82991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
83991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
84991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
85991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
86991554f2SKenneth D. Merry 
87991554f2SKenneth D. Merry /* For Hashed SAS Address creation for SATA Drives */
88991554f2SKenneth D. Merry #define MPT2SAS_SN_LEN 20
89991554f2SKenneth D. Merry #define MPT2SAS_MN_LEN 40
90991554f2SKenneth D. Merry 
91991554f2SKenneth D. Merry struct mpr_fw_event_work {
92991554f2SKenneth D. Merry 	u16			event;
93991554f2SKenneth D. Merry 	void			*event_data;
94991554f2SKenneth D. Merry 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
95991554f2SKenneth D. Merry };
96991554f2SKenneth D. Merry 
97991554f2SKenneth D. Merry union _sata_sas_address {
98991554f2SKenneth D. Merry 	u8 wwid[8];
99991554f2SKenneth D. Merry 	struct {
100991554f2SKenneth D. Merry 		u32 high;
101991554f2SKenneth D. Merry 		u32 low;
102991554f2SKenneth D. Merry 	} word;
103991554f2SKenneth D. Merry };
104991554f2SKenneth D. Merry 
105991554f2SKenneth D. Merry /*
106991554f2SKenneth D. Merry  * define the IDENTIFY DEVICE structure
107991554f2SKenneth D. Merry  */
108991554f2SKenneth D. Merry struct _ata_identify_device_data {
109991554f2SKenneth D. Merry 	u16 reserved1[10];	/* 0-9 */
110991554f2SKenneth D. Merry 	u16 serial_number[10];	/* 10-19 */
111991554f2SKenneth D. Merry 	u16 reserved2[7];	/* 20-26 */
112991554f2SKenneth D. Merry 	u16 model_number[20];	/* 27-46*/
113a2c14879SStephen McConnell 	u16 reserved3[170];	/* 47-216 */
114a2c14879SStephen McConnell 	u16 rotational_speed;	/* 217 */
115a2c14879SStephen McConnell 	u16 reserved4[38];	/* 218-255 */
116991554f2SKenneth D. Merry };
117991554f2SKenneth D. Merry static u32 event_count;
118991554f2SKenneth D. Merry static void mprsas_fw_work(struct mpr_softc *sc,
119991554f2SKenneth D. Merry     struct mpr_fw_event_work *fw_event);
120991554f2SKenneth D. Merry static void mprsas_fw_event_free(struct mpr_softc *,
121991554f2SKenneth D. Merry     struct mpr_fw_event_work *);
122991554f2SKenneth D. Merry static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
12367feec50SStephen McConnell static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
12467feec50SStephen McConnell     u8 linkrate);
125991554f2SKenneth D. Merry static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
126991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
127991554f2SKenneth D. Merry     u32 devinfo);
12886312e46SConrad Meyer static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
129991554f2SKenneth D. Merry int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
130a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
131991554f2SKenneth D. Merry static int mprsas_volume_add(struct mpr_softc *sc,
132991554f2SKenneth D. Merry     u16 handle);
133acc173a6SWarner Losh static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
134991554f2SKenneth D. Merry static void mprsas_stop_unit_done(struct cam_periph *periph,
135991554f2SKenneth D. Merry     union ccb *done_ccb);
136991554f2SKenneth D. Merry 
137991554f2SKenneth D. Merry void
138991554f2SKenneth D. Merry mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
139991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
140991554f2SKenneth D. Merry {
141991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
142991554f2SKenneth D. Merry 	u16 sz;
143991554f2SKenneth D. Merry 
144991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
145055e2653SScott Long 	MPR_DPRINT_EVENT(sc, sas, event);
146991554f2SKenneth D. Merry 	mprsas_record_event(sc, event);
147991554f2SKenneth D. Merry 
148991554f2SKenneth D. Merry 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
149991554f2SKenneth D. Merry 	     M_ZERO|M_NOWAIT);
150991554f2SKenneth D. Merry 	if (!fw_event) {
151991554f2SKenneth D. Merry 		printf("%s: allocate failed for fw_event\n", __func__);
152991554f2SKenneth D. Merry 		return;
153991554f2SKenneth D. Merry 	}
154991554f2SKenneth D. Merry 	sz = le16toh(event->EventDataLength) * 4;
155991554f2SKenneth D. Merry 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
156991554f2SKenneth D. Merry 	if (!fw_event->event_data) {
157991554f2SKenneth D. Merry 		printf("%s: allocate failed for event_data\n", __func__);
158991554f2SKenneth D. Merry 		free(fw_event, M_MPR);
159991554f2SKenneth D. Merry 		return;
160991554f2SKenneth D. Merry 	}
161991554f2SKenneth D. Merry 
162991554f2SKenneth D. Merry 	bcopy(event->EventData, fw_event->event_data, sz);
163991554f2SKenneth D. Merry 	fw_event->event = event->Event;
164991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
16567feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
166991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
167991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
168991554f2SKenneth D. Merry 	    sc->track_mapping_events)
169991554f2SKenneth D. Merry 		sc->pending_map_events++;
170991554f2SKenneth D. Merry 
171991554f2SKenneth D. Merry 	/*
172991554f2SKenneth D. Merry 	 * When wait_for_port_enable flag is set, make sure that all the events
173991554f2SKenneth D. Merry 	 * are processed. Increment the startup_refcount and decrement it after
174991554f2SKenneth D. Merry 	 * events are processed.
175991554f2SKenneth D. Merry 	 */
176991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
17767feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
178991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
179991554f2SKenneth D. Merry 	    sc->wait_for_port_enable)
180991554f2SKenneth D. Merry 		mprsas_startup_increment(sc->sassc);
181991554f2SKenneth D. Merry 
182991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
183991554f2SKenneth D. Merry 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
184991554f2SKenneth D. Merry }
185991554f2SKenneth D. Merry 
186991554f2SKenneth D. Merry static void
187991554f2SKenneth D. Merry mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
188991554f2SKenneth D. Merry {
189991554f2SKenneth D. Merry 
190991554f2SKenneth D. Merry 	free(fw_event->event_data, M_MPR);
191991554f2SKenneth D. Merry 	free(fw_event, M_MPR);
192991554f2SKenneth D. Merry }
193991554f2SKenneth D. Merry 
194991554f2SKenneth D. Merry /**
195991554f2SKenneth D. Merry  * _mpr_fw_work - delayed task for processing firmware events
196991554f2SKenneth D. Merry  * @sc: per adapter object
197991554f2SKenneth D. Merry  * @fw_event: The fw_event_work object
198991554f2SKenneth D. Merry  * Context: user.
199991554f2SKenneth D. Merry  *
200991554f2SKenneth D. Merry  * Return nothing.
201991554f2SKenneth D. Merry  */
202991554f2SKenneth D. Merry static void
203991554f2SKenneth D. Merry mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
204991554f2SKenneth D. Merry {
205991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
206991554f2SKenneth D. Merry 	sassc = sc->sassc;
207991554f2SKenneth D. Merry 
208991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
209991554f2SKenneth D. Merry 	    event_count++, __func__, fw_event->event);
210991554f2SKenneth D. Merry 	switch (fw_event->event) {
211991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
212991554f2SKenneth D. Merry 	{
213991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
214991554f2SKenneth D. Merry 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
21567feec50SStephen McConnell 		uint8_t i;
216991554f2SKenneth D. Merry 
217991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
218991554f2SKenneth D. Merry 		    fw_event->event_data;
219991554f2SKenneth D. Merry 
220991554f2SKenneth D. Merry 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
221991554f2SKenneth D. Merry 
222991554f2SKenneth D. Merry 		for (i = 0; i < data->NumEntries; i++) {
223991554f2SKenneth D. Merry 			phy = &data->PHY[i];
224991554f2SKenneth D. Merry 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
225991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
226991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
227991554f2SKenneth D. Merry 				    le16toh(phy->AttachedDevHandle),
228991554f2SKenneth D. Merry 				    phy->LinkRate)) {
229327f2e6cSStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: "
230327f2e6cSStephen McConnell 					    "failed to add device with handle "
231327f2e6cSStephen McConnell 					    "0x%x\n", __func__,
232991554f2SKenneth D. Merry 					    le16toh(phy->AttachedDevHandle));
233991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc, le16toh(
234991554f2SKenneth D. Merry 					    phy->AttachedDevHandle));
235991554f2SKenneth D. Merry 				}
236991554f2SKenneth D. Merry 				break;
237991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
238991554f2SKenneth D. Merry 				mprsas_prepare_remove(sassc, le16toh(
239991554f2SKenneth D. Merry 				    phy->AttachedDevHandle));
240991554f2SKenneth D. Merry 				break;
241991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
242991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
243991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
244991554f2SKenneth D. Merry 			default:
245991554f2SKenneth D. Merry 				break;
246991554f2SKenneth D. Merry 			}
247991554f2SKenneth D. Merry 		}
248991554f2SKenneth D. Merry 		/*
249991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
250991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
251991554f2SKenneth D. Merry 		 * been processed.
252991554f2SKenneth D. Merry 		 */
253991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
254991554f2SKenneth D. Merry 		break;
255991554f2SKenneth D. Merry 	}
256991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DISCOVERY:
257991554f2SKenneth D. Merry 	{
258991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
259991554f2SKenneth D. Merry 
260991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
261991554f2SKenneth D. Merry 
262991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
2637a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
264991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
265991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
266991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
267991554f2SKenneth D. Merry 			mprsas_discovery_end(sassc);
268991554f2SKenneth D. Merry 		}
269991554f2SKenneth D. Merry 		break;
270991554f2SKenneth D. Merry 	}
271991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
272991554f2SKenneth D. Merry 	{
273991554f2SKenneth D. Merry 		Mpi2EventDataSasEnclDevStatusChange_t *data;
274991554f2SKenneth D. Merry 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
275991554f2SKenneth D. Merry 		    fw_event->event_data;
276991554f2SKenneth D. Merry 		mpr_mapping_enclosure_dev_status_change_event(sc,
277991554f2SKenneth D. Merry 		    fw_event->event_data);
278991554f2SKenneth D. Merry 		break;
279991554f2SKenneth D. Merry 	}
280991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
281991554f2SKenneth D. Merry 	{
282991554f2SKenneth D. Merry 		Mpi2EventIrConfigElement_t *element;
283991554f2SKenneth D. Merry 		int i;
284991554f2SKenneth D. Merry 		u8 foreign_config, reason;
285991554f2SKenneth D. Merry 		u16 elementType;
286991554f2SKenneth D. Merry 		Mpi2EventDataIrConfigChangeList_t *event_data;
287991554f2SKenneth D. Merry 		struct mprsas_target *targ;
288991554f2SKenneth D. Merry 		unsigned int id;
289991554f2SKenneth D. Merry 
290991554f2SKenneth D. Merry 		event_data = fw_event->event_data;
291991554f2SKenneth D. Merry 		foreign_config = (le32toh(event_data->Flags) &
292991554f2SKenneth D. Merry 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
293991554f2SKenneth D. Merry 
294991554f2SKenneth D. Merry 		element =
295991554f2SKenneth D. Merry 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
296327f2e6cSStephen McConnell 		id = mpr_mapping_get_raid_tid_from_handle(sc,
297991554f2SKenneth D. Merry 		    element->VolDevHandle);
298991554f2SKenneth D. Merry 
299991554f2SKenneth D. Merry 		mpr_mapping_ir_config_change_event(sc, event_data);
300991554f2SKenneth D. Merry 		for (i = 0; i < event_data->NumElements; i++, element++) {
301991554f2SKenneth D. Merry 			reason = element->ReasonCode;
302991554f2SKenneth D. Merry 			elementType = le16toh(element->ElementFlags) &
303991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
304991554f2SKenneth D. Merry 			/*
305991554f2SKenneth D. Merry 			 * check for element type of Phys Disk or Hot Spare
306991554f2SKenneth D. Merry 			 */
307991554f2SKenneth D. Merry 			if ((elementType !=
308991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
309991554f2SKenneth D. Merry 			    && (elementType !=
310991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
311991554f2SKenneth D. Merry 				// do next element
312991554f2SKenneth D. Merry 				goto skip_fp_send;
313991554f2SKenneth D. Merry 
314991554f2SKenneth D. Merry 			/*
315991554f2SKenneth D. Merry 			 * check for reason of Hide, Unhide, PD Created, or PD
316991554f2SKenneth D. Merry 			 * Deleted
317991554f2SKenneth D. Merry 			 */
318991554f2SKenneth D. Merry 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
319991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
320991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
321991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
322991554f2SKenneth D. Merry 				goto skip_fp_send;
323991554f2SKenneth D. Merry 
324991554f2SKenneth D. Merry 			// check for a reason of Hide or PD Created
325991554f2SKenneth D. Merry 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
326991554f2SKenneth D. Merry 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
327991554f2SKenneth D. Merry 			{
328991554f2SKenneth D. Merry 				// build RAID Action message
329991554f2SKenneth D. Merry 				Mpi2RaidActionRequest_t	*action;
3306d4ffcb4SKenneth D. Merry 				Mpi2RaidActionReply_t *reply = NULL;
331991554f2SKenneth D. Merry 				struct mpr_command *cm;
332991554f2SKenneth D. Merry 				int error = 0;
333991554f2SKenneth D. Merry 				if ((cm = mpr_alloc_command(sc)) == NULL) {
334991554f2SKenneth D. Merry 					printf("%s: command alloc failed\n",
335991554f2SKenneth D. Merry 					    __func__);
336991554f2SKenneth D. Merry 					return;
337991554f2SKenneth D. Merry 				}
338991554f2SKenneth D. Merry 
339a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
340991554f2SKenneth D. Merry 				    "from "
341991554f2SKenneth D. Merry 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
342991554f2SKenneth D. Merry 				    ":\n");
343991554f2SKenneth D. Merry 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
344991554f2SKenneth D. Merry 				action->Function = MPI2_FUNCTION_RAID_ACTION;
345991554f2SKenneth D. Merry 				action->Action =
346991554f2SKenneth D. Merry 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
347991554f2SKenneth D. Merry 				action->PhysDiskNum = element->PhysDiskNum;
348991554f2SKenneth D. Merry 				cm->cm_desc.Default.RequestFlags =
349991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3506d4ffcb4SKenneth D. Merry 				error = mpr_request_polled(sc, &cm);
3516d4ffcb4SKenneth D. Merry 				if (cm != NULL)
3526d4ffcb4SKenneth D. Merry 					reply = (Mpi2RaidActionReply_t *)
3536d4ffcb4SKenneth D. Merry 					    cm->cm_reply;
354991554f2SKenneth D. Merry 				if (error || (reply == NULL)) {
355991554f2SKenneth D. Merry 					/* FIXME */
356991554f2SKenneth D. Merry 					/*
357991554f2SKenneth D. Merry 					 * If the poll returns error then we
358991554f2SKenneth D. Merry 					 * need to do diag reset
359991554f2SKenneth D. Merry 					 */
360991554f2SKenneth D. Merry 					printf("%s: poll for page completed "
361991554f2SKenneth D. Merry 					    "with error %d", __func__, error);
362991554f2SKenneth D. Merry 				}
363991554f2SKenneth D. Merry 				if (reply && (le16toh(reply->IOCStatus) &
364991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_MASK) !=
365991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_SUCCESS) {
366a2c14879SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: error "
367a2c14879SStephen McConnell 					    "sending RaidActionPage; "
368a2c14879SStephen McConnell 					    "iocstatus = 0x%x\n", __func__,
369991554f2SKenneth D. Merry 					    le16toh(reply->IOCStatus));
370991554f2SKenneth D. Merry 				}
371991554f2SKenneth D. Merry 
372991554f2SKenneth D. Merry 				if (cm)
373991554f2SKenneth D. Merry 					mpr_free_command(sc, cm);
374991554f2SKenneth D. Merry 			}
375991554f2SKenneth D. Merry skip_fp_send:
376a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_EVENT, "Received "
377991554f2SKenneth D. Merry 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
378991554f2SKenneth D. Merry 			    "code %x:\n", element->ReasonCode);
379991554f2SKenneth D. Merry 			switch (element->ReasonCode) {
380991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
381991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
382991554f2SKenneth D. Merry 				if (!foreign_config) {
383991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
384991554f2SKenneth D. Merry 					    le16toh(element->VolDevHandle))) {
385991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
386991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
387991554f2SKenneth D. Merry 						    __func__, le16toh(element->
388991554f2SKenneth D. Merry 						    VolDevHandle));
389991554f2SKenneth D. Merry 					}
390991554f2SKenneth D. Merry 				}
391991554f2SKenneth D. Merry 				break;
392991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
393991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
394991554f2SKenneth D. Merry 				/*
395991554f2SKenneth D. Merry 				 * Rescan after volume is deleted or removed.
396991554f2SKenneth D. Merry 				 */
397991554f2SKenneth D. Merry 				if (!foreign_config) {
398991554f2SKenneth D. Merry 					if (id == MPR_MAP_BAD_ID) {
399991554f2SKenneth D. Merry 						printf("%s: could not get ID "
400991554f2SKenneth D. Merry 						    "for volume with handle "
401991554f2SKenneth D. Merry 						    "0x%04x\n", __func__,
402991554f2SKenneth D. Merry 						    le16toh(element->
403991554f2SKenneth D. Merry 						    VolDevHandle));
404991554f2SKenneth D. Merry 						break;
405991554f2SKenneth D. Merry 					}
406991554f2SKenneth D. Merry 
407991554f2SKenneth D. Merry 					targ = &sassc->targets[id];
408991554f2SKenneth D. Merry 					targ->handle = 0x0;
409991554f2SKenneth D. Merry 					targ->encl_slot = 0x0;
410991554f2SKenneth D. Merry 					targ->encl_handle = 0x0;
411991554f2SKenneth D. Merry 					targ->encl_level_valid = 0x0;
412991554f2SKenneth D. Merry 					targ->encl_level = 0x0;
413991554f2SKenneth D. Merry 					targ->connector_name[0] = ' ';
414991554f2SKenneth D. Merry 					targ->connector_name[1] = ' ';
415991554f2SKenneth D. Merry 					targ->connector_name[2] = ' ';
416991554f2SKenneth D. Merry 					targ->connector_name[3] = ' ';
417991554f2SKenneth D. Merry 					targ->exp_dev_handle = 0x0;
418991554f2SKenneth D. Merry 					targ->phy_num = 0x0;
419991554f2SKenneth D. Merry 					targ->linkrate = 0x0;
420991554f2SKenneth D. Merry 					mprsas_rescan_target(sc, targ);
421991554f2SKenneth D. Merry 					printf("RAID target id 0x%x removed\n",
422991554f2SKenneth D. Merry 					    targ->tid);
423991554f2SKenneth D. Merry 				}
424991554f2SKenneth D. Merry 				break;
425991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
426991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
427991554f2SKenneth D. Merry 				/*
428991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been created.  Hide
429991554f2SKenneth D. Merry 				 * it from the OS.
430991554f2SKenneth D. Merry 				 */
431991554f2SKenneth D. Merry 				targ = mprsas_find_target_by_handle(sassc, 0,
432991554f2SKenneth D. Merry 				    element->PhysDiskDevHandle);
433991554f2SKenneth D. Merry 				if (targ == NULL)
434991554f2SKenneth D. Merry 					break;
435991554f2SKenneth D. Merry 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
436991554f2SKenneth D. Merry 				mprsas_rescan_target(sc, targ);
437991554f2SKenneth D. Merry 				break;
438991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
439991554f2SKenneth D. Merry 				/*
440991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been deleted.
441991554f2SKenneth D. Merry 				 * Expose it to the OS.
442991554f2SKenneth D. Merry 				 */
443991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
444991554f2SKenneth D. Merry 				    le16toh(element->PhysDiskDevHandle), 0)) {
445991554f2SKenneth D. Merry 					printf("%s: failed to add device with "
446991554f2SKenneth D. Merry 					    "handle 0x%x\n", __func__,
447991554f2SKenneth D. Merry 					    le16toh(element->
448991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
449991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc,
450991554f2SKenneth D. Merry 					    le16toh(element->
451991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
452991554f2SKenneth D. Merry 				}
453991554f2SKenneth D. Merry 				break;
454991554f2SKenneth D. Merry 			}
455991554f2SKenneth D. Merry 		}
456991554f2SKenneth D. Merry 		/*
457991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
458991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
459991554f2SKenneth D. Merry 		 * been processed.
460991554f2SKenneth D. Merry 		 */
461991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
462991554f2SKenneth D. Merry 		break;
463991554f2SKenneth D. Merry 	}
464991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_VOLUME:
465991554f2SKenneth D. Merry 	{
466991554f2SKenneth D. Merry 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
467991554f2SKenneth D. Merry 
468991554f2SKenneth D. Merry 		/*
469991554f2SKenneth D. Merry 		 * Informational only.
470991554f2SKenneth D. Merry 		 */
471991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
472991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
473991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
474991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
475991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
476991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
477991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
478991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
479991554f2SKenneth D. Merry 			break;
480991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
481991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
482991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
483991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
484991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
485991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
486991554f2SKenneth D. Merry 			break;
487991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
488991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
489991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
490991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
491991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
492991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
493991554f2SKenneth D. Merry 				u32 state;
494991554f2SKenneth D. Merry 				struct mprsas_target *targ;
495991554f2SKenneth D. Merry 				state = le32toh(event_data->NewValue);
496991554f2SKenneth D. Merry 				switch (state) {
497991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_MISSING:
498991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_FAILED:
499991554f2SKenneth D. Merry 					mprsas_prepare_volume_remove(sassc,
500991554f2SKenneth D. Merry 					    event_data->VolDevHandle);
501991554f2SKenneth D. Merry 					break;
502991554f2SKenneth D. Merry 
503991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_ONLINE:
504991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_DEGRADED:
505991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_OPTIMAL:
506991554f2SKenneth D. Merry 					targ =
507991554f2SKenneth D. Merry 					    mprsas_find_target_by_handle(sassc,
508991554f2SKenneth D. Merry 					    0, event_data->VolDevHandle);
509991554f2SKenneth D. Merry 					if (targ) {
510991554f2SKenneth D. Merry 						printf("%s %d: Volume handle "
511991554f2SKenneth D. Merry 						    "0x%x is already added \n",
512991554f2SKenneth D. Merry 						    __func__, __LINE__,
513991554f2SKenneth D. Merry 						    event_data->VolDevHandle);
514991554f2SKenneth D. Merry 						break;
515991554f2SKenneth D. Merry 					}
516991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
517991554f2SKenneth D. Merry 					    le16toh(event_data->
518991554f2SKenneth D. Merry 					    VolDevHandle))) {
519991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
520991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
521991554f2SKenneth D. Merry 						    __func__, le16toh(
522991554f2SKenneth D. Merry 						    event_data->VolDevHandle));
523991554f2SKenneth D. Merry 					}
524991554f2SKenneth D. Merry 					break;
525991554f2SKenneth D. Merry 				default:
526991554f2SKenneth D. Merry 					break;
527991554f2SKenneth D. Merry 				}
528991554f2SKenneth D. Merry 			break;
529991554f2SKenneth D. Merry 		default:
530991554f2SKenneth D. Merry 			break;
531991554f2SKenneth D. Merry 		}
532991554f2SKenneth D. Merry 		break;
533991554f2SKenneth D. Merry 	}
534991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_PHYSICAL_DISK:
535991554f2SKenneth D. Merry 	{
536991554f2SKenneth D. Merry 		Mpi2EventDataIrPhysicalDisk_t *event_data =
537991554f2SKenneth D. Merry 		    fw_event->event_data;
538991554f2SKenneth D. Merry 		struct mprsas_target *targ;
539991554f2SKenneth D. Merry 
540991554f2SKenneth D. Merry 		/*
541991554f2SKenneth D. Merry 		 * Informational only.
542991554f2SKenneth D. Merry 		 */
543991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
544991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
545991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
546991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
547991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Phys Disk Number "
548991554f2SKenneth D. Merry   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
549991554f2SKenneth D. Merry  			    "%d", le32toh(event_data->PreviousValue),
550991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
551991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
552991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
553991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
554991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
555991554f2SKenneth D. Merry 			break;
556991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
557991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
558991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
559991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
560991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
561991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
562991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
563991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
564991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
565991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
566991554f2SKenneth D. Merry 			break;
567991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
568991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
569991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
570991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
571991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
572991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
573991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
574991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
575991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
576991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
577991554f2SKenneth D. Merry 			switch (event_data->NewValue) {
578991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_ONLINE:
579991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_DEGRADED:
580991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_REBUILDING:
581991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OPTIMAL:
582991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_HOT_SPARE:
583991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
584991554f2SKenneth D. Merry 					    sassc, 0,
585991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
586991554f2SKenneth D. Merry 					if (targ) {
587991554f2SKenneth D. Merry 						targ->flags |=
588991554f2SKenneth D. Merry 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
589991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
590991554f2SKenneth D. Merry 						    "for handle 0x%x.\n",
591991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
592991554f2SKenneth D. Merry 						    event_data->
593991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
594991554f2SKenneth D. Merry 					}
595991554f2SKenneth D. Merry 				break;
596991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OFFLINE:
597991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
598991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
599991554f2SKenneth D. Merry 				default:
600991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
601991554f2SKenneth D. Merry 					    sassc, 0,
602991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
603991554f2SKenneth D. Merry 					if (targ) {
604991554f2SKenneth D. Merry 						targ->flags |=
605991554f2SKenneth D. Merry 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
606991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
607991554f2SKenneth D. Merry 						    "for handle 0x%x.  \n",
608991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
609991554f2SKenneth D. Merry 						    event_data->
610991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
611991554f2SKenneth D. Merry 					}
612991554f2SKenneth D. Merry 				break;
613991554f2SKenneth D. Merry 			}
614991554f2SKenneth D. Merry 		default:
615991554f2SKenneth D. Merry 			break;
616991554f2SKenneth D. Merry 		}
617991554f2SKenneth D. Merry 		break;
618991554f2SKenneth D. Merry 	}
619991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_OPERATION_STATUS:
620991554f2SKenneth D. Merry 	{
621991554f2SKenneth D. Merry 		Mpi2EventDataIrOperationStatus_t *event_data =
622991554f2SKenneth D. Merry 		    fw_event->event_data;
623991554f2SKenneth D. Merry 
624991554f2SKenneth D. Merry 		/*
625991554f2SKenneth D. Merry 		 * Informational only.
626991554f2SKenneth D. Merry 		 */
627991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
628991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
629991554f2SKenneth D. Merry 		    "percent complete for Volume with handle 0x%x",
630991554f2SKenneth D. Merry 		    event_data->RAIDOperation, event_data->PercentComplete,
631991554f2SKenneth D. Merry 		    le16toh(event_data->VolDevHandle));
632991554f2SKenneth D. Merry 		break;
633991554f2SKenneth D. Merry 	}
634991554f2SKenneth D. Merry 	case MPI2_EVENT_TEMP_THRESHOLD:
635991554f2SKenneth D. Merry 	{
636991554f2SKenneth D. Merry 		pMpi2EventDataTemperature_t	temp_event;
637991554f2SKenneth D. Merry 
638991554f2SKenneth D. Merry 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
639991554f2SKenneth D. Merry 
640991554f2SKenneth D. Merry 		/*
641991554f2SKenneth D. Merry 		 * The Temp Sensor Count must be greater than the event's Sensor
642991554f2SKenneth D. Merry 		 * Num to be valid.  If valid, print the temp thresholds that
643991554f2SKenneth D. Merry 		 * have been exceeded.
644991554f2SKenneth D. Merry 		 */
645991554f2SKenneth D. Merry 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
646991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
647991554f2SKenneth D. Merry 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
648991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
649991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
650991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
651991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
652991554f2SKenneth D. Merry 			    temp_event->SensorNum);
653991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
654991554f2SKenneth D. Merry 			    "%d\n", temp_event->CurrentTemperature);
655991554f2SKenneth D. Merry 		}
656991554f2SKenneth D. Merry 		break;
657991554f2SKenneth D. Merry 	}
6582bbc5fcbSStephen McConnell 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
6592bbc5fcbSStephen McConnell 	{
6602bbc5fcbSStephen McConnell 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
6612bbc5fcbSStephen McConnell 		ace_event_data =
6622bbc5fcbSStephen McConnell 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
6632bbc5fcbSStephen McConnell 
6640656476aSAlexander Motin 		switch(ace_event_data->ReasonCode) {
6650656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
6660656476aSAlexander Motin 		{
6670656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6682bbc5fcbSStephen McConnell 			    "ReceptacleID %d cannot be powered and device "
6692bbc5fcbSStephen McConnell 			    "connected to this active cable will not be seen. "
6702bbc5fcbSStephen McConnell 			    "This active cable requires %d mW of power.\n",
6712bbc5fcbSStephen McConnell 			    ace_event_data->ReceptacleID,
6722bbc5fcbSStephen McConnell 			    ace_event_data->ActiveCablePowerRequirement);
6730656476aSAlexander Motin 			break;
6740656476aSAlexander Motin 		}
6750656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
6760656476aSAlexander Motin 		{
6770656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6780656476aSAlexander Motin 			    "ReceptacleID %d is not running at optimal speed "
6790656476aSAlexander Motin 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
6800656476aSAlexander Motin 			break;
6810656476aSAlexander Motin 		}
6820656476aSAlexander Motin 		default:
6830656476aSAlexander Motin 			break;
6842bbc5fcbSStephen McConnell 		}
6852bbc5fcbSStephen McConnell 		break;
6862bbc5fcbSStephen McConnell 	}
68789d1c21fSKashyap D Desai 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
68889d1c21fSKashyap D Desai 	{
68989d1c21fSKashyap D Desai 		pMpi26EventDataPCIeDeviceStatusChange_t	pcie_status_event_data;
69089d1c21fSKashyap D Desai 		pcie_status_event_data =
69189d1c21fSKashyap D Desai 		   (pMpi26EventDataPCIeDeviceStatusChange_t)fw_event->event_data;
69289d1c21fSKashyap D Desai 
69389d1c21fSKashyap D Desai 		switch (pcie_status_event_data->ReasonCode) {
69489d1c21fSKashyap D Desai 		case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
69589d1c21fSKashyap D Desai 		{
69689d1c21fSKashyap D Desai 			mpr_printf(sc, "PCIe Host Reset failed on DevHandle "
69789d1c21fSKashyap D Desai 			    "0x%x\n", pcie_status_event_data->DevHandle);
69889d1c21fSKashyap D Desai 			break;
69989d1c21fSKashyap D Desai 		}
70089d1c21fSKashyap D Desai 		default:
70189d1c21fSKashyap D Desai 			break;
70289d1c21fSKashyap D Desai 		}
70389d1c21fSKashyap D Desai 		break;
70489d1c21fSKashyap D Desai 	}
7055f5baf0eSAlexander Motin 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
7065f5baf0eSAlexander Motin 	{
7075f5baf0eSAlexander Motin 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
7085f5baf0eSAlexander Motin 		uint64_t sas_address;
7095f5baf0eSAlexander Motin 
7105f5baf0eSAlexander Motin 		discovery_error_data =
7115f5baf0eSAlexander Motin 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
7125f5baf0eSAlexander Motin 		    fw_event->event_data;
7135f5baf0eSAlexander Motin 
7145f5baf0eSAlexander Motin 		sas_address = discovery_error_data->SASAddress.High;
7155f5baf0eSAlexander Motin 		sas_address = (sas_address << 32) |
7165f5baf0eSAlexander Motin 		    discovery_error_data->SASAddress.Low;
7175f5baf0eSAlexander Motin 
7185f5baf0eSAlexander Motin 		switch(discovery_error_data->ReasonCode) {
7195f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
7205f5baf0eSAlexander Motin 		{
7215f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command failed during discovery "
7225f5baf0eSAlexander Motin 			    "for expander with SAS Address %jx and "
7235f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7245f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7255f5baf0eSAlexander Motin 			break;
7265f5baf0eSAlexander Motin 		}
7275f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
7285f5baf0eSAlexander Motin 		{
7295f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command timed out during "
7305f5baf0eSAlexander Motin 			    "discovery for expander with SAS Address %jx and "
7315f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7325f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7335f5baf0eSAlexander Motin 			break;
7345f5baf0eSAlexander Motin 		}
7355f5baf0eSAlexander Motin 		default:
7365f5baf0eSAlexander Motin 			break;
7375f5baf0eSAlexander Motin 		}
7385f5baf0eSAlexander Motin 		break;
7395f5baf0eSAlexander Motin 	}
74067feec50SStephen McConnell 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
74167feec50SStephen McConnell 	{
74267feec50SStephen McConnell 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
74367feec50SStephen McConnell 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
74467feec50SStephen McConnell 		uint8_t i, link_rate;
74567feec50SStephen McConnell 		uint16_t handle;
74667feec50SStephen McConnell 
74767feec50SStephen McConnell 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
74867feec50SStephen McConnell 		    fw_event->event_data;
74967feec50SStephen McConnell 
75067feec50SStephen McConnell 		mpr_mapping_pcie_topology_change_event(sc,
75167feec50SStephen McConnell 		    fw_event->event_data);
75267feec50SStephen McConnell 
75367feec50SStephen McConnell 		for (i = 0; i < data->NumEntries; i++) {
75467feec50SStephen McConnell 			port_entry = &data->PortEntry[i];
75567feec50SStephen McConnell 			handle = le16toh(port_entry->AttachedDevHandle);
75667feec50SStephen McConnell 			link_rate = port_entry->CurrentPortInfo &
75767feec50SStephen McConnell 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
75867feec50SStephen McConnell 			switch (port_entry->PortStatus) {
75967feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
76067feec50SStephen McConnell 				if (link_rate <
76167feec50SStephen McConnell 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
76267feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
76367feec50SStephen McConnell 					    "add PCIe device with handle 0x%x "
76467feec50SStephen McConnell 					    "with unknown link rate.\n",
76567feec50SStephen McConnell 					    __func__, handle);
76667feec50SStephen McConnell 					break;
76767feec50SStephen McConnell 				}
76867feec50SStephen McConnell 				if (mprsas_add_pcie_device(sc, handle,
76967feec50SStephen McConnell 				    link_rate)) {
77067feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
77167feec50SStephen McConnell 					    "to add PCIe device with handle "
77267feec50SStephen McConnell 					    "0x%x\n", __func__, handle);
77367feec50SStephen McConnell 					mprsas_prepare_remove(sassc, handle);
77467feec50SStephen McConnell 				}
77567feec50SStephen McConnell 				break;
77667feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
77767feec50SStephen McConnell 				mprsas_prepare_remove(sassc, handle);
77867feec50SStephen McConnell 				break;
77967feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
78067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
78167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
78267feec50SStephen McConnell 			default:
78367feec50SStephen McConnell 				break;
78467feec50SStephen McConnell 			}
78567feec50SStephen McConnell 		}
78667feec50SStephen McConnell 		/*
78767feec50SStephen McConnell 		 * refcount was incremented for this event in
78867feec50SStephen McConnell 		 * mprsas_evt_handler.  Decrement it here because the event has
78967feec50SStephen McConnell 		 * been processed.
79067feec50SStephen McConnell 		 */
79167feec50SStephen McConnell 		mprsas_startup_decrement(sassc);
79267feec50SStephen McConnell 		break;
79367feec50SStephen McConnell 	}
794991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
795991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
796991554f2SKenneth D. Merry 	default:
797991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
798991554f2SKenneth D. Merry 		    fw_event->event);
799991554f2SKenneth D. Merry 		break;
800991554f2SKenneth D. Merry 
801991554f2SKenneth D. Merry 	}
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 	}
871453130d9SPedro F. Giffuni 	/* TODO Check proper endianness */
872991554f2SKenneth D. Merry 	sas_address = config_page.SASAddress.High;
873a2c14879SStephen McConnell 	sas_address = (sas_address << 32) | config_page.SASAddress.Low;
874757ff642SScott Long 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
875757ff642SScott Long 	    "page0 = %jx\n", handle, sas_address);
876991554f2SKenneth D. Merry 
877a2c14879SStephen McConnell 	/*
878a2c14879SStephen McConnell 	 * Always get SATA Identify information because this is used to
879a2c14879SStephen McConnell 	 * determine if Start/Stop Unit should be sent to the drive when the
880a2c14879SStephen McConnell 	 * system is shutdown.
881a2c14879SStephen McConnell 	 */
882991554f2SKenneth D. Merry 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
883a2c14879SStephen McConnell 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
884a2c14879SStephen McConnell 		    handle, device_info, &is_SATA_SSD);
885a2c14879SStephen McConnell 		if (ret) {
886757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
887757ff642SScott Long 			    "%s: failed to get disk type (SSD or HDD) for SATA "
888757ff642SScott Long 			    "device with handle 0x%04x\n",
889a2c14879SStephen McConnell 			    __func__, handle);
890a2c14879SStephen McConnell 		} else {
891757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
892757ff642SScott Long 			    "from SATA device = %jx\n", handle, sas_address);
893a2c14879SStephen McConnell 		}
894a2c14879SStephen McConnell 	}
895c91d307fSStephen McConnell 
8964ab1cdc5SScott Long 	/*
8974ab1cdc5SScott Long 	 * use_phynum:
8984ab1cdc5SScott Long 	 *  1 - use the PhyNum field as a fallback to the mapping logic
8994ab1cdc5SScott Long 	 *  0 - never use the PhyNum field
9004ab1cdc5SScott Long 	 * -1 - only use the PhyNum field
901327f2e6cSStephen McConnell 	 *
902327f2e6cSStephen McConnell 	 * Note that using the Phy number to map a device can cause device adds
903327f2e6cSStephen McConnell 	 * to fail if multiple enclosures/expanders are in the topology. For
904327f2e6cSStephen McConnell 	 * example, if two devices are in the same slot number in two different
905327f2e6cSStephen McConnell 	 * enclosures within the topology, only one of those devices will be
906327f2e6cSStephen McConnell 	 * added. PhyNum mapping should not be used if multiple enclosures are
907327f2e6cSStephen McConnell 	 * in the topology.
9084ab1cdc5SScott Long 	 */
9094ab1cdc5SScott Long 	id = MPR_MAP_BAD_ID;
9104ab1cdc5SScott Long 	if (sc->use_phynum != -1)
911327f2e6cSStephen McConnell 		id = mpr_mapping_get_tid(sc, sas_address, handle);
912991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
91367feec50SStephen McConnell 		if ((sc->use_phynum == 0) ||
91467feec50SStephen McConnell 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
9154ab1cdc5SScott Long 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
9164ab1cdc5SScott Long 			    "Could not get ID for device with handle 0x%04x\n",
9174ab1cdc5SScott Long 			    __FILE__, __LINE__, __func__, handle);
918991554f2SKenneth D. Merry 			error = ENXIO;
919991554f2SKenneth D. Merry 			goto out;
920991554f2SKenneth D. Merry 		}
9214ab1cdc5SScott Long 	}
922327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
923327f2e6cSStephen McConnell 	    __func__, id);
924991554f2SKenneth D. Merry 
925327f2e6cSStephen McConnell 	/*
926327f2e6cSStephen McConnell 	 * Only do the ID check and reuse check if the target is not from a
927327f2e6cSStephen McConnell 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
928327f2e6cSStephen McConnell 	 * when a volume is deleted because the mapping entry for the PD will
929327f2e6cSStephen McConnell 	 * still be in the mapping table. The ID check should not be done here
930327f2e6cSStephen McConnell 	 * either since this PD is already being used.
931327f2e6cSStephen McConnell 	 */
932327f2e6cSStephen McConnell 	targ = &sassc->targets[id];
933327f2e6cSStephen McConnell 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
934991554f2SKenneth D. Merry 		if (mprsas_check_id(sassc, id) != 0) {
935757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
936757ff642SScott Long 			    "Excluding target id %d\n", id);
937991554f2SKenneth D. Merry 			error = ENXIO;
938991554f2SKenneth D. Merry 			goto out;
939991554f2SKenneth D. Merry 		}
940991554f2SKenneth D. Merry 
9414ab1cdc5SScott Long 		if (targ->handle != 0x0) {
942327f2e6cSStephen McConnell 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
943327f2e6cSStephen McConnell 			    "target id %d handle 0x%04x\n", id, targ->handle);
9444ab1cdc5SScott Long 			error = ENXIO;
9454ab1cdc5SScott Long 			goto out;
9464ab1cdc5SScott Long 		}
947327f2e6cSStephen McConnell 	}
9484ab1cdc5SScott Long 
949991554f2SKenneth D. Merry 	targ->devinfo = device_info;
950991554f2SKenneth D. Merry 	targ->devname = le32toh(config_page.DeviceName.High);
951991554f2SKenneth D. Merry 	targ->devname = (targ->devname << 32) |
952991554f2SKenneth D. Merry 	    le32toh(config_page.DeviceName.Low);
953991554f2SKenneth D. Merry 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
954991554f2SKenneth D. Merry 	targ->encl_slot = le16toh(config_page.Slot);
955991554f2SKenneth D. Merry 	targ->encl_level = config_page.EnclosureLevel;
956991554f2SKenneth D. Merry 	targ->connector_name[0] = config_page.ConnectorName[0];
957991554f2SKenneth D. Merry 	targ->connector_name[1] = config_page.ConnectorName[1];
958991554f2SKenneth D. Merry 	targ->connector_name[2] = config_page.ConnectorName[2];
959991554f2SKenneth D. Merry 	targ->connector_name[3] = config_page.ConnectorName[3];
960991554f2SKenneth D. Merry 	targ->handle = handle;
961991554f2SKenneth D. Merry 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
962991554f2SKenneth D. Merry 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
963991554f2SKenneth D. Merry 	targ->parent_sasaddr = le64toh(parent_sas_address);
964991554f2SKenneth D. Merry 	targ->parent_devinfo = parent_devinfo;
965991554f2SKenneth D. Merry 	targ->tid = id;
966991554f2SKenneth D. Merry 	targ->linkrate = (linkrate>>4);
967991554f2SKenneth D. Merry 	targ->flags = 0;
968a2c14879SStephen McConnell 	if (is_SATA_SSD) {
969a2c14879SStephen McConnell 		targ->flags = MPR_TARGET_IS_SATA_SSD;
970a2c14879SStephen McConnell 	}
97167feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
97267feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
97367feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
97467feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
975991554f2SKenneth D. Merry 		targ->scsi_req_desc_type =
976991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
977991554f2SKenneth D. Merry 	}
978991554f2SKenneth D. Merry 	if (le16toh(config_page.Flags) &
979991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
980991554f2SKenneth D. Merry 		targ->encl_level_valid = TRUE;
981991554f2SKenneth D. Merry 	}
982991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
983991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
984991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
985991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
986991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
987991554f2SKenneth D. Merry 		free(lun, M_MPR);
988991554f2SKenneth D. Merry 	}
989991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
990991554f2SKenneth D. Merry 
991991554f2SKenneth D. Merry 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
992a2c14879SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
993991554f2SKenneth D. Merry 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
994991554f2SKenneth D. Merry 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
995991554f2SKenneth D. Merry 	    targ->handle, targ->encl_handle, targ->encl_slot);
996991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
997a2c14879SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
998991554f2SKenneth D. Merry 		    "and connector name (%4s)\n", targ->encl_level,
999991554f2SKenneth D. Merry 		    targ->connector_name);
1000991554f2SKenneth D. Merry 	}
1001a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
1002a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
1003991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
1004991554f2SKenneth D. Merry #endif
1005991554f2SKenneth D. Merry 		mprsas_rescan_target(sc, targ);
1006991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1007a2c14879SStephen McConnell 
1008a2c14879SStephen McConnell 	/*
1009a2c14879SStephen McConnell 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
1010a2c14879SStephen McConnell 	 * If so, send a Target Reset TM to the target that was just created.
1011a2c14879SStephen McConnell 	 * An Abort Task TM should be used instead of a Target Reset, but that
1012a2c14879SStephen McConnell 	 * would be much more difficult because targets have not been fully
1013a2c14879SStephen McConnell 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
1014a2c14879SStephen McConnell 	 * target instead of the LUN.
1015a2c14879SStephen McConnell 	 */
1016a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1017a2c14879SStephen McConnell 		cm = &sc->commands[i];
1018a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1019a2c14879SStephen McConnell 			targ->timeouts++;
1020*8fe7bf06SWarner Losh 			cm->cm_flags |= MPR_CM_FLAGS_TIMEDOUT;
1021a2c14879SStephen McConnell 
1022a2c14879SStephen McConnell 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1023a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1024a2c14879SStephen McConnell 				    "Reset for stuck SATA identify command "
1025a2c14879SStephen McConnell 				    "(cm = %p)\n", __func__, cm);
1026a2c14879SStephen McConnell 				targ->tm->cm_targ = targ;
1027a2c14879SStephen McConnell 				mprsas_send_reset(sc, targ->tm,
1028a2c14879SStephen McConnell 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1029a2c14879SStephen McConnell 			} else {
1030a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
10317a2a6a1aSStephen McConnell 				    "tm for Target Reset after SATA ID command "
10327a2a6a1aSStephen McConnell 				    "timed out (cm %p)\n", cm);
1033a2c14879SStephen McConnell 			}
1034a2c14879SStephen McConnell 			/*
1035a2c14879SStephen McConnell 			 * No need to check for more since the target is
1036a2c14879SStephen McConnell 			 * already being reset.
1037a2c14879SStephen McConnell 			 */
1038a2c14879SStephen McConnell 			break;
1039a2c14879SStephen McConnell 		}
1040a2c14879SStephen McConnell 	}
1041991554f2SKenneth D. Merry out:
1042a2c14879SStephen McConnell 	/*
1043a2c14879SStephen McConnell 	 * Free the commands that may not have been freed from the SATA ID call
1044a2c14879SStephen McConnell 	 */
1045a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1046a2c14879SStephen McConnell 		cm = &sc->commands[i];
1047a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
10488277ce2bSConrad Meyer 			free(cm->cm_data, M_MPR);
1049a2c14879SStephen McConnell 			mpr_free_command(sc, cm);
1050a2c14879SStephen McConnell 		}
1051a2c14879SStephen McConnell 	}
1052991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1053991554f2SKenneth D. Merry 	return (error);
1054991554f2SKenneth D. Merry }
1055991554f2SKenneth D. Merry 
1056991554f2SKenneth D. Merry int
1057991554f2SKenneth D. Merry mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1058a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1059991554f2SKenneth D. Merry {
1060991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t mpi_reply;
1061991554f2SKenneth D. Merry 	int i, rc, try_count;
1062991554f2SKenneth D. Merry 	u32 *bufferptr;
1063991554f2SKenneth D. Merry 	union _sata_sas_address hash_address;
1064991554f2SKenneth D. Merry 	struct _ata_identify_device_data ata_identify;
1065991554f2SKenneth D. Merry 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1066991554f2SKenneth D. Merry 	u32 ioc_status;
1067991554f2SKenneth D. Merry 	u8 sas_status;
1068991554f2SKenneth D. Merry 
1069991554f2SKenneth D. Merry 	memset(&ata_identify, 0, sizeof(ata_identify));
10704e02badbSAlan Somers 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1071991554f2SKenneth D. Merry 	try_count = 0;
1072991554f2SKenneth D. Merry 	do {
1073991554f2SKenneth D. Merry 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1074991554f2SKenneth D. Merry 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1075991554f2SKenneth D. Merry 		try_count++;
1076991554f2SKenneth D. Merry 		ioc_status = le16toh(mpi_reply.IOCStatus)
1077991554f2SKenneth D. Merry 		    & MPI2_IOCSTATUS_MASK;
1078991554f2SKenneth D. Merry 		sas_status = mpi_reply.SASStatus;
1079a92fe027SAlan Somers 		switch (ioc_status) {
1080a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SUCCESS:
1081a92fe027SAlan Somers 			break;
1082a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1083a92fe027SAlan Somers 			/* No sense sleeping.  this error won't get better */
1084a92fe027SAlan Somers 			break;
1085a92fe027SAlan Somers 		default:
1086a2c14879SStephen McConnell 			if (sc->spinup_wait_time > 0) {
1087a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1088a2c14879SStephen McConnell 				    "after SATA ID error to wait for spinup\n",
1089a2c14879SStephen McConnell 				    sc->spinup_wait_time);
1090a2c14879SStephen McConnell 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1091a2c14879SStephen McConnell 				    "mprid", sc->spinup_wait_time * hz);
1092a2c14879SStephen McConnell 			}
1093a2c14879SStephen McConnell 		}
1094a92fe027SAlan Somers 	} while (((rc && (rc != EWOULDBLOCK)) ||
10957a2a6a1aSStephen McConnell 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1096a92fe027SAlan Somers 	    || sas_status) && (try_count < 5));
1097991554f2SKenneth D. Merry 
1098991554f2SKenneth D. Merry 	if (rc == 0 && !ioc_status && !sas_status) {
1099991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1100991554f2SKenneth D. Merry 		    "successfully for handle = 0x%x with try_count = %d\n",
1101991554f2SKenneth D. Merry 		    __func__, handle, try_count);
1102991554f2SKenneth D. Merry 	} else {
1103991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1104991554f2SKenneth D. Merry 		    __func__, handle);
1105991554f2SKenneth D. Merry 		return -1;
1106991554f2SKenneth D. Merry 	}
1107991554f2SKenneth D. Merry 	/* Copy & byteswap the 40 byte model number to a buffer */
1108991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1109991554f2SKenneth D. Merry 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1110991554f2SKenneth D. Merry 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1111991554f2SKenneth D. Merry 	}
1112991554f2SKenneth D. Merry 	/* Copy & byteswap the 20 byte serial number to a buffer */
1113991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1114991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i] =
1115991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i + 1];
1116991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i + 1] =
1117991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i];
1118991554f2SKenneth D. Merry 	}
1119991554f2SKenneth D. Merry 	bufferptr = (u32 *)buffer;
1120991554f2SKenneth D. Merry 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1121991554f2SKenneth D. Merry 	 * so loop through the first 56 bytes (7*8),
1122991554f2SKenneth D. Merry 	 * and then add in the last dword.
1123991554f2SKenneth D. Merry 	 */
1124991554f2SKenneth D. Merry 	hash_address.word.low  = 0;
1125991554f2SKenneth D. Merry 	hash_address.word.high = 0;
1126991554f2SKenneth D. Merry 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1127991554f2SKenneth D. Merry 		hash_address.word.low += *bufferptr;
1128991554f2SKenneth D. Merry 		bufferptr++;
1129991554f2SKenneth D. Merry 		hash_address.word.high += *bufferptr;
1130991554f2SKenneth D. Merry 		bufferptr++;
1131991554f2SKenneth D. Merry 	}
1132991554f2SKenneth D. Merry 	/* Add the last dword */
1133991554f2SKenneth D. Merry 	hash_address.word.low += *bufferptr;
1134991554f2SKenneth D. Merry 	/* Make sure the hash doesn't start with 5, because it could clash
1135991554f2SKenneth D. Merry 	 * with a SAS address. Change 5 to a D.
1136991554f2SKenneth D. Merry 	 */
1137991554f2SKenneth D. Merry 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1138991554f2SKenneth D. Merry 		hash_address.word.high |= 0x00000080;
1139991554f2SKenneth D. Merry 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1140991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1141991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1142991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1143991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[7];
1144a2c14879SStephen McConnell 	if (ata_identify.rotational_speed == 1) {
1145a2c14879SStephen McConnell 		*is_SATA_SSD = 1;
1146a2c14879SStephen McConnell 	}
1147a2c14879SStephen McConnell 
1148991554f2SKenneth D. Merry 	return 0;
1149991554f2SKenneth D. Merry }
1150991554f2SKenneth D. Merry 
1151991554f2SKenneth D. Merry static int
1152991554f2SKenneth D. Merry mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1153991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1154991554f2SKenneth D. Merry {
1155991554f2SKenneth D. Merry 	Mpi2SataPassthroughRequest_t *mpi_request;
1156991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t *reply;
1157991554f2SKenneth D. Merry 	struct mpr_command *cm;
1158991554f2SKenneth D. Merry 	char *buffer;
1159991554f2SKenneth D. Merry 	int error = 0;
1160991554f2SKenneth D. Merry 
1161991554f2SKenneth D. Merry 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1162991554f2SKenneth D. Merry 	if (!buffer)
1163991554f2SKenneth D. Merry 		return ENOMEM;
1164991554f2SKenneth D. Merry 
1165991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1166991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1167991554f2SKenneth D. Merry 		return (EBUSY);
1168991554f2SKenneth D. Merry 	}
1169991554f2SKenneth D. Merry 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1170991554f2SKenneth D. Merry 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1171991554f2SKenneth D. Merry 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1172991554f2SKenneth D. Merry 	mpi_request->VF_ID = 0;
1173991554f2SKenneth D. Merry 	mpi_request->DevHandle = htole16(handle);
1174991554f2SKenneth D. Merry 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1175991554f2SKenneth D. Merry 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1176991554f2SKenneth D. Merry 	mpi_request->DataLength = htole32(sz);
1177991554f2SKenneth D. Merry 	mpi_request->CommandFIS[0] = 0x27;
1178991554f2SKenneth D. Merry 	mpi_request->CommandFIS[1] = 0x80;
1179991554f2SKenneth D. Merry 	mpi_request->CommandFIS[2] =  (devinfo &
1180991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1181991554f2SKenneth D. Merry 	cm->cm_sge = &mpi_request->SGL;
1182991554f2SKenneth D. Merry 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1183991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1184991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1185991554f2SKenneth D. Merry 	cm->cm_data = buffer;
1186991554f2SKenneth D. Merry 	cm->cm_length = htole32(sz);
1187a2c14879SStephen McConnell 
1188a2c14879SStephen McConnell 	/*
118986312e46SConrad Meyer 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
119086312e46SConrad Meyer 	 * This fixes a problem where the FW does not send a reply sometimes
1191a2c14879SStephen McConnell 	 * when a bad disk is in the topology. So, this is used to timeout the
1192a2c14879SStephen McConnell 	 * command so that processing can continue normally.
1193a2c14879SStephen McConnell 	 */
119486312e46SConrad Meyer 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1195a2c14879SStephen McConnell 
119686312e46SConrad Meyer 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
119786312e46SConrad Meyer 
119886312e46SConrad Meyer 	/* mprsas_ata_id_timeout does not reset controller */
119986312e46SConrad Meyer 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
120086312e46SConrad Meyer 
1201991554f2SKenneth D. Merry 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1202991554f2SKenneth D. Merry 	if (error || (reply == NULL)) {
1203991554f2SKenneth D. Merry 		/* FIXME */
1204991554f2SKenneth D. Merry 		/*
1205991554f2SKenneth D. Merry 		 * If the request returns an error then we need to do a diag
1206991554f2SKenneth D. Merry 		 * reset
1207991554f2SKenneth D. Merry 		 */
1208aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1209e7ef108cSWarner Losh 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
1210aeb9ac0dSScott Long 		    error);
1211991554f2SKenneth D. Merry 		error = ENXIO;
1212991554f2SKenneth D. Merry 		goto out;
1213991554f2SKenneth D. Merry 	}
1214991554f2SKenneth D. Merry 	bcopy(buffer, id_buffer, sz);
1215991554f2SKenneth D. Merry 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1216991554f2SKenneth D. Merry 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1217991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
1218aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1219aeb9ac0dSScott Long 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1220aeb9ac0dSScott Long 		    handle, reply->IOCStatus);
1221991554f2SKenneth D. Merry 		error = ENXIO;
1222991554f2SKenneth D. Merry 		goto out;
1223991554f2SKenneth D. Merry 	}
1224991554f2SKenneth D. Merry out:
1225a2c14879SStephen McConnell 	/*
1226a2c14879SStephen McConnell 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
12278277ce2bSConrad Meyer 	 * it.  The command and buffer will be freed after sending an Abort
122886312e46SConrad Meyer 	 * Task TM.
1229a2c14879SStephen McConnell 	 */
12308277ce2bSConrad Meyer 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1231991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1232991554f2SKenneth D. Merry 		free(buffer, M_MPR);
123386312e46SConrad Meyer 	}
1234991554f2SKenneth D. Merry 	return (error);
1235991554f2SKenneth D. Merry }
1236991554f2SKenneth D. Merry 
1237a2c14879SStephen McConnell static void
123886312e46SConrad Meyer mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1239a2c14879SStephen McConnell {
1240a2c14879SStephen McConnell 
124186312e46SConrad Meyer 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1242a2c14879SStephen McConnell 	    __func__, cm, sc);
1243a2c14879SStephen McConnell 
1244a2c14879SStephen McConnell 	/*
124586312e46SConrad Meyer 	 * The Abort Task cannot be sent from here because the driver has not
124686312e46SConrad Meyer 	 * completed setting up targets.  Instead, the command is flagged so
1247*8fe7bf06SWarner Losh 	 * that special handling will be used to send the abort. Now that
1248*8fe7bf06SWarner Losh 	 * this command has timed out, it's no longer in the queue.
1249a2c14879SStephen McConnell 	 */
1250a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1251*8fe7bf06SWarner Losh 	cm->cm_state = MPR_CM_STATE_BUSY;
1252a2c14879SStephen McConnell }
1253a2c14879SStephen McConnell 
1254991554f2SKenneth D. Merry static int
125567feec50SStephen McConnell mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
125667feec50SStephen McConnell {
125767feec50SStephen McConnell 	char devstring[80];
125867feec50SStephen McConnell 	struct mprsas_softc *sassc;
125967feec50SStephen McConnell 	struct mprsas_target *targ;
126067feec50SStephen McConnell 	Mpi2ConfigReply_t mpi_reply;
126167feec50SStephen McConnell 	Mpi26PCIeDevicePage0_t config_page;
126267feec50SStephen McConnell 	Mpi26PCIeDevicePage2_t config_page2;
126367feec50SStephen McConnell 	uint64_t pcie_wwid, parent_wwid = 0;
126467feec50SStephen McConnell 	u32 device_info, parent_devinfo = 0;
126567feec50SStephen McConnell 	unsigned int id;
126667feec50SStephen McConnell 	int error = 0;
126767feec50SStephen McConnell 	struct mprsas_lun *lun;
126867feec50SStephen McConnell 
126967feec50SStephen McConnell 	sassc = sc->sassc;
127067feec50SStephen McConnell 	mprsas_startup_increment(sassc);
127167feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
127267feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
127367feec50SStephen McConnell 		printf("%s: error reading PCIe device page0\n", __func__);
127467feec50SStephen McConnell 		error = ENXIO;
127567feec50SStephen McConnell 		goto out;
127667feec50SStephen McConnell 	}
127767feec50SStephen McConnell 
127867feec50SStephen McConnell 	device_info = le32toh(config_page.DeviceInfo);
127967feec50SStephen McConnell 
128067feec50SStephen McConnell 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
128167feec50SStephen McConnell 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
128267feec50SStephen McConnell 		Mpi2ConfigReply_t tmp_mpi_reply;
128367feec50SStephen McConnell 		Mpi26PCIeDevicePage0_t parent_config_page;
128467feec50SStephen McConnell 
128567feec50SStephen McConnell 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
128667feec50SStephen McConnell 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
128767feec50SStephen McConnell 		     le16toh(config_page.ParentDevHandle)))) {
128867feec50SStephen McConnell 			printf("%s: error reading PCIe device %#x page0\n",
128967feec50SStephen McConnell 			    __func__, le16toh(config_page.ParentDevHandle));
129067feec50SStephen McConnell 		} else {
129167feec50SStephen McConnell 			parent_wwid = parent_config_page.WWID.High;
129267feec50SStephen McConnell 			parent_wwid = (parent_wwid << 32) |
129367feec50SStephen McConnell 			    parent_config_page.WWID.Low;
129467feec50SStephen McConnell 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
129567feec50SStephen McConnell 		}
129667feec50SStephen McConnell 	}
129767feec50SStephen McConnell 	/* TODO Check proper endianness */
129867feec50SStephen McConnell 	pcie_wwid = config_page.WWID.High;
129967feec50SStephen McConnell 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
130067feec50SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
130167feec50SStephen McConnell 	    pcie_wwid);
130267feec50SStephen McConnell 
130367feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
130467feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
130567feec50SStephen McConnell 		printf("%s: error reading PCIe device page2\n", __func__);
130667feec50SStephen McConnell 		error = ENXIO;
130767feec50SStephen McConnell 		goto out;
130867feec50SStephen McConnell 	}
130967feec50SStephen McConnell 
1310327f2e6cSStephen McConnell 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
131167feec50SStephen McConnell 	if (id == MPR_MAP_BAD_ID) {
1312327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1313327f2e6cSStephen McConnell 		    "Could not get ID for device with handle 0x%04x\n",
1314327f2e6cSStephen McConnell 		    __FILE__, __LINE__, __func__, handle);
131567feec50SStephen McConnell 		error = ENXIO;
131667feec50SStephen McConnell 		goto out;
131767feec50SStephen McConnell 	}
1318327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1319327f2e6cSStephen McConnell 	    __func__, id);
132067feec50SStephen McConnell 
132167feec50SStephen McConnell 	if (mprsas_check_id(sassc, id) != 0) {
1322757ff642SScott Long 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1323757ff642SScott Long 		    "Excluding target id %d\n", id);
132467feec50SStephen McConnell 		error = ENXIO;
132567feec50SStephen McConnell 		goto out;
132667feec50SStephen McConnell 	}
132767feec50SStephen McConnell 
132867feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
132967feec50SStephen McConnell 	    pcie_wwid);
133067feec50SStephen McConnell 	targ = &sassc->targets[id];
133167feec50SStephen McConnell 	targ->devinfo = device_info;
133267feec50SStephen McConnell 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
133367feec50SStephen McConnell 	targ->encl_slot = le16toh(config_page.Slot);
133467feec50SStephen McConnell 	targ->encl_level = config_page.EnclosureLevel;
133567feec50SStephen McConnell 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
133667feec50SStephen McConnell 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
133767feec50SStephen McConnell 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
133867feec50SStephen McConnell 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
133967feec50SStephen McConnell 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
134067feec50SStephen McConnell 	targ->MDTS = config_page2.MaximumDataTransferSize;
134189d1c21fSKashyap D Desai 	if (targ->is_nvme)
134289d1c21fSKashyap D Desai 		targ->controller_reset_timeout = config_page2.ControllerResetTO;
134367feec50SStephen McConnell 	/*
134467feec50SStephen McConnell 	 * Assume always TRUE for encl_level_valid because there is no valid
134567feec50SStephen McConnell 	 * flag for PCIe.
134667feec50SStephen McConnell 	 */
134767feec50SStephen McConnell 	targ->encl_level_valid = TRUE;
134867feec50SStephen McConnell 	targ->handle = handle;
134967feec50SStephen McConnell 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
135067feec50SStephen McConnell 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
135167feec50SStephen McConnell 	targ->parent_sasaddr = le64toh(parent_wwid);
135267feec50SStephen McConnell 	targ->parent_devinfo = parent_devinfo;
135367feec50SStephen McConnell 	targ->tid = id;
135467feec50SStephen McConnell 	targ->linkrate = linkrate;
135567feec50SStephen McConnell 	targ->flags = 0;
135667feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
135767feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
135867feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
135967feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
136067feec50SStephen McConnell 		targ->scsi_req_desc_type =
136167feec50SStephen McConnell 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
136267feec50SStephen McConnell 	}
136367feec50SStephen McConnell 	TAILQ_INIT(&targ->commands);
136467feec50SStephen McConnell 	TAILQ_INIT(&targ->timedout_commands);
136567feec50SStephen McConnell 	while (!SLIST_EMPTY(&targ->luns)) {
136667feec50SStephen McConnell 		lun = SLIST_FIRST(&targ->luns);
136767feec50SStephen McConnell 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
136867feec50SStephen McConnell 		free(lun, M_MPR);
136967feec50SStephen McConnell 	}
137067feec50SStephen McConnell 	SLIST_INIT(&targ->luns);
137167feec50SStephen McConnell 
137267feec50SStephen McConnell 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
137367feec50SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
137467feec50SStephen McConnell 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
137567feec50SStephen McConnell 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
137667feec50SStephen McConnell 	    targ->handle, targ->encl_handle, targ->encl_slot);
137767feec50SStephen McConnell 	if (targ->encl_level_valid) {
137867feec50SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
137967feec50SStephen McConnell 		    "and connector name (%4s)\n", targ->encl_level,
138067feec50SStephen McConnell 		    targ->connector_name);
138167feec50SStephen McConnell 	}
138267feec50SStephen McConnell #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
138367feec50SStephen McConnell     (__FreeBSD_version < 902502)
138467feec50SStephen McConnell 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
138567feec50SStephen McConnell #endif
138667feec50SStephen McConnell 		mprsas_rescan_target(sc, targ);
138767feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
138867feec50SStephen McConnell 
138967feec50SStephen McConnell out:
139067feec50SStephen McConnell 	mprsas_startup_decrement(sassc);
139167feec50SStephen McConnell 	return (error);
139267feec50SStephen McConnell }
139367feec50SStephen McConnell 
139467feec50SStephen McConnell static int
1395991554f2SKenneth D. Merry mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1396991554f2SKenneth D. Merry {
1397991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1398991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1399991554f2SKenneth D. Merry 	u64 wwid;
1400991554f2SKenneth D. Merry 	unsigned int id;
1401991554f2SKenneth D. Merry 	int error = 0;
1402991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1403991554f2SKenneth D. Merry 
1404991554f2SKenneth D. Merry 	sassc = sc->sassc;
1405991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
1406991554f2SKenneth D. Merry 	/* wwid is endian safe */
1407991554f2SKenneth D. Merry 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1408991554f2SKenneth D. Merry 	if (!wwid) {
1409991554f2SKenneth D. Merry 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1410991554f2SKenneth D. Merry 		    __func__);
1411991554f2SKenneth D. Merry 		error = ENXIO;
1412991554f2SKenneth D. Merry 		goto out;
1413991554f2SKenneth D. Merry 	}
1414991554f2SKenneth D. Merry 
1415327f2e6cSStephen McConnell 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1416991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
1417991554f2SKenneth D. Merry 		printf("%s: could not get ID for volume with handle 0x%04x and "
1418991554f2SKenneth D. Merry 		    "WWID 0x%016llx\n", __func__, handle,
1419991554f2SKenneth D. Merry 		    (unsigned long long)wwid);
1420991554f2SKenneth D. Merry 		error = ENXIO;
1421991554f2SKenneth D. Merry 		goto out;
1422991554f2SKenneth D. Merry 	}
1423991554f2SKenneth D. Merry 
1424991554f2SKenneth D. Merry 	targ = &sassc->targets[id];
1425991554f2SKenneth D. Merry 	targ->tid = id;
1426991554f2SKenneth D. Merry 	targ->handle = handle;
1427991554f2SKenneth D. Merry 	targ->devname = wwid;
1428991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
1429991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
1430991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
1431991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
1432991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1433991554f2SKenneth D. Merry 		free(lun, M_MPR);
1434991554f2SKenneth D. Merry 	}
1435991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
1436a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
1437a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
1438991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
1439991554f2SKenneth D. Merry #endif
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