xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 86312e466c7f7d1fff90e9139e57bfd44e4906f3)
1991554f2SKenneth D. Merry /*-
2a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
37a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
4991554f2SKenneth D. Merry  * All rights reserved.
5991554f2SKenneth D. Merry  *
6991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
7991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
8991554f2SKenneth D. Merry  * are met:
9991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
10991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
11991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
12991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
13991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
14991554f2SKenneth D. Merry  *
15991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25991554f2SKenneth D. Merry  * SUCH DAMAGE.
26991554f2SKenneth D. Merry  *
27a2c14879SStephen McConnell  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
28991554f2SKenneth D. Merry  */
29991554f2SKenneth D. Merry 
30991554f2SKenneth D. Merry #include <sys/cdefs.h>
31991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
32991554f2SKenneth D. Merry 
337a2a6a1aSStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
34991554f2SKenneth D. Merry 
35991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
36991554f2SKenneth D. Merry #include <sys/types.h>
37991554f2SKenneth D. Merry #include <sys/param.h>
38991554f2SKenneth D. Merry #include <sys/systm.h>
39991554f2SKenneth D. Merry #include <sys/kernel.h>
40991554f2SKenneth D. Merry #include <sys/selinfo.h>
41991554f2SKenneth D. Merry #include <sys/module.h>
42991554f2SKenneth D. Merry #include <sys/bus.h>
43991554f2SKenneth D. Merry #include <sys/conf.h>
44991554f2SKenneth D. Merry #include <sys/bio.h>
45991554f2SKenneth D. Merry #include <sys/malloc.h>
46991554f2SKenneth D. Merry #include <sys/uio.h>
47991554f2SKenneth D. Merry #include <sys/sysctl.h>
48991554f2SKenneth D. Merry #include <sys/endian.h>
49acc173a6SWarner Losh #include <sys/proc.h>
50991554f2SKenneth D. Merry #include <sys/queue.h>
51991554f2SKenneth D. Merry #include <sys/kthread.h>
52991554f2SKenneth D. Merry #include <sys/taskqueue.h>
53991554f2SKenneth D. Merry #include <sys/sbuf.h>
54acc173a6SWarner Losh #include <sys/reboot.h>
55991554f2SKenneth D. Merry 
56991554f2SKenneth D. Merry #include <machine/bus.h>
57991554f2SKenneth D. Merry #include <machine/resource.h>
58991554f2SKenneth D. Merry #include <sys/rman.h>
59991554f2SKenneth D. Merry 
60991554f2SKenneth D. Merry #include <machine/stdarg.h>
61991554f2SKenneth D. Merry 
62991554f2SKenneth D. Merry #include <cam/cam.h>
63991554f2SKenneth D. Merry #include <cam/cam_ccb.h>
64991554f2SKenneth D. Merry #include <cam/cam_debug.h>
65991554f2SKenneth D. Merry #include <cam/cam_sim.h>
66991554f2SKenneth D. Merry #include <cam/cam_xpt_sim.h>
67991554f2SKenneth D. Merry #include <cam/cam_xpt_periph.h>
68991554f2SKenneth D. Merry #include <cam/cam_periph.h>
69991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
70991554f2SKenneth D. Merry #include <cam/scsi/scsi_message.h>
71991554f2SKenneth D. Merry 
72991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
74991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
75991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7667feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
77991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_raid.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
82991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
83991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
84991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
85991554f2SKenneth D. Merry 
86991554f2SKenneth D. Merry /* For Hashed SAS Address creation for SATA Drives */
87991554f2SKenneth D. Merry #define MPT2SAS_SN_LEN 20
88991554f2SKenneth D. Merry #define MPT2SAS_MN_LEN 40
89991554f2SKenneth D. Merry 
90991554f2SKenneth D. Merry struct mpr_fw_event_work {
91991554f2SKenneth D. Merry 	u16			event;
92991554f2SKenneth D. Merry 	void			*event_data;
93991554f2SKenneth D. Merry 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
94991554f2SKenneth D. Merry };
95991554f2SKenneth D. Merry 
96991554f2SKenneth D. Merry union _sata_sas_address {
97991554f2SKenneth D. Merry 	u8 wwid[8];
98991554f2SKenneth D. Merry 	struct {
99991554f2SKenneth D. Merry 		u32 high;
100991554f2SKenneth D. Merry 		u32 low;
101991554f2SKenneth D. Merry 	} word;
102991554f2SKenneth D. Merry };
103991554f2SKenneth D. Merry 
104991554f2SKenneth D. Merry /*
105991554f2SKenneth D. Merry  * define the IDENTIFY DEVICE structure
106991554f2SKenneth D. Merry  */
107991554f2SKenneth D. Merry struct _ata_identify_device_data {
108991554f2SKenneth D. Merry 	u16 reserved1[10];	/* 0-9 */
109991554f2SKenneth D. Merry 	u16 serial_number[10];	/* 10-19 */
110991554f2SKenneth D. Merry 	u16 reserved2[7];	/* 20-26 */
111991554f2SKenneth D. Merry 	u16 model_number[20];	/* 27-46*/
112a2c14879SStephen McConnell 	u16 reserved3[170];	/* 47-216 */
113a2c14879SStephen McConnell 	u16 rotational_speed;	/* 217 */
114a2c14879SStephen McConnell 	u16 reserved4[38];	/* 218-255 */
115991554f2SKenneth D. Merry };
116991554f2SKenneth D. Merry static u32 event_count;
117991554f2SKenneth D. Merry static void mprsas_fw_work(struct mpr_softc *sc,
118991554f2SKenneth D. Merry     struct mpr_fw_event_work *fw_event);
119991554f2SKenneth D. Merry static void mprsas_fw_event_free(struct mpr_softc *,
120991554f2SKenneth D. Merry     struct mpr_fw_event_work *);
121991554f2SKenneth D. Merry static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
12267feec50SStephen McConnell static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
12367feec50SStephen McConnell     u8 linkrate);
124991554f2SKenneth D. Merry static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
125991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
126991554f2SKenneth D. Merry     u32 devinfo);
127*86312e46SConrad Meyer static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
128991554f2SKenneth D. Merry int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
129a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
130991554f2SKenneth D. Merry static int mprsas_volume_add(struct mpr_softc *sc,
131991554f2SKenneth D. Merry     u16 handle);
132acc173a6SWarner Losh static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
133991554f2SKenneth D. Merry static void mprsas_stop_unit_done(struct cam_periph *periph,
134991554f2SKenneth D. Merry     union ccb *done_ccb);
135991554f2SKenneth D. Merry 
136991554f2SKenneth D. Merry void
137991554f2SKenneth D. Merry mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
138991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
139991554f2SKenneth D. Merry {
140991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
141991554f2SKenneth D. Merry 	u16 sz;
142991554f2SKenneth D. Merry 
143991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
144055e2653SScott Long 	MPR_DPRINT_EVENT(sc, sas, event);
145991554f2SKenneth D. Merry 	mprsas_record_event(sc, event);
146991554f2SKenneth D. Merry 
147991554f2SKenneth D. Merry 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
148991554f2SKenneth D. Merry 	     M_ZERO|M_NOWAIT);
149991554f2SKenneth D. Merry 	if (!fw_event) {
150991554f2SKenneth D. Merry 		printf("%s: allocate failed for fw_event\n", __func__);
151991554f2SKenneth D. Merry 		return;
152991554f2SKenneth D. Merry 	}
153991554f2SKenneth D. Merry 	sz = le16toh(event->EventDataLength) * 4;
154991554f2SKenneth D. Merry 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
155991554f2SKenneth D. Merry 	if (!fw_event->event_data) {
156991554f2SKenneth D. Merry 		printf("%s: allocate failed for event_data\n", __func__);
157991554f2SKenneth D. Merry 		free(fw_event, M_MPR);
158991554f2SKenneth D. Merry 		return;
159991554f2SKenneth D. Merry 	}
160991554f2SKenneth D. Merry 
161991554f2SKenneth D. Merry 	bcopy(event->EventData, fw_event->event_data, sz);
162991554f2SKenneth D. Merry 	fw_event->event = event->Event;
163991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
16467feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
165991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
166991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
167991554f2SKenneth D. Merry 	    sc->track_mapping_events)
168991554f2SKenneth D. Merry 		sc->pending_map_events++;
169991554f2SKenneth D. Merry 
170991554f2SKenneth D. Merry 	/*
171991554f2SKenneth D. Merry 	 * When wait_for_port_enable flag is set, make sure that all the events
172991554f2SKenneth D. Merry 	 * are processed. Increment the startup_refcount and decrement it after
173991554f2SKenneth D. Merry 	 * events are processed.
174991554f2SKenneth D. Merry 	 */
175991554f2SKenneth D. Merry 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
17667feec50SStephen McConnell 	    event->Event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
177991554f2SKenneth D. Merry 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
178991554f2SKenneth D. Merry 	    sc->wait_for_port_enable)
179991554f2SKenneth D. Merry 		mprsas_startup_increment(sc->sassc);
180991554f2SKenneth D. Merry 
181991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
182991554f2SKenneth D. Merry 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
183991554f2SKenneth D. Merry }
184991554f2SKenneth D. Merry 
185991554f2SKenneth D. Merry static void
186991554f2SKenneth D. Merry mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
187991554f2SKenneth D. Merry {
188991554f2SKenneth D. Merry 
189991554f2SKenneth D. Merry 	free(fw_event->event_data, M_MPR);
190991554f2SKenneth D. Merry 	free(fw_event, M_MPR);
191991554f2SKenneth D. Merry }
192991554f2SKenneth D. Merry 
193991554f2SKenneth D. Merry /**
194991554f2SKenneth D. Merry  * _mpr_fw_work - delayed task for processing firmware events
195991554f2SKenneth D. Merry  * @sc: per adapter object
196991554f2SKenneth D. Merry  * @fw_event: The fw_event_work object
197991554f2SKenneth D. Merry  * Context: user.
198991554f2SKenneth D. Merry  *
199991554f2SKenneth D. Merry  * Return nothing.
200991554f2SKenneth D. Merry  */
201991554f2SKenneth D. Merry static void
202991554f2SKenneth D. Merry mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
203991554f2SKenneth D. Merry {
204991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
205991554f2SKenneth D. Merry 	sassc = sc->sassc;
206991554f2SKenneth D. Merry 
207991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
208991554f2SKenneth D. Merry 	    event_count++, __func__, fw_event->event);
209991554f2SKenneth D. Merry 	switch (fw_event->event) {
210991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
211991554f2SKenneth D. Merry 	{
212991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
213991554f2SKenneth D. Merry 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
21467feec50SStephen McConnell 		uint8_t i;
215991554f2SKenneth D. Merry 
216991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
217991554f2SKenneth D. Merry 		    fw_event->event_data;
218991554f2SKenneth D. Merry 
219991554f2SKenneth D. Merry 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
220991554f2SKenneth D. Merry 
221991554f2SKenneth D. Merry 		for (i = 0; i < data->NumEntries; i++) {
222991554f2SKenneth D. Merry 			phy = &data->PHY[i];
223991554f2SKenneth D. Merry 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
224991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
225991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
226991554f2SKenneth D. Merry 				    le16toh(phy->AttachedDevHandle),
227991554f2SKenneth D. Merry 				    phy->LinkRate)) {
228327f2e6cSStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: "
229327f2e6cSStephen McConnell 					    "failed to add device with handle "
230327f2e6cSStephen McConnell 					    "0x%x\n", __func__,
231991554f2SKenneth D. Merry 					    le16toh(phy->AttachedDevHandle));
232991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc, le16toh(
233991554f2SKenneth D. Merry 					    phy->AttachedDevHandle));
234991554f2SKenneth D. Merry 				}
235991554f2SKenneth D. Merry 				break;
236991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
237991554f2SKenneth D. Merry 				mprsas_prepare_remove(sassc, le16toh(
238991554f2SKenneth D. Merry 				    phy->AttachedDevHandle));
239991554f2SKenneth D. Merry 				break;
240991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
241991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
242991554f2SKenneth D. Merry 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
243991554f2SKenneth D. Merry 			default:
244991554f2SKenneth D. Merry 				break;
245991554f2SKenneth D. Merry 			}
246991554f2SKenneth D. Merry 		}
247991554f2SKenneth D. Merry 		/*
248991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
249991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
250991554f2SKenneth D. Merry 		 * been processed.
251991554f2SKenneth D. Merry 		 */
252991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
253991554f2SKenneth D. Merry 		break;
254991554f2SKenneth D. Merry 	}
255991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DISCOVERY:
256991554f2SKenneth D. Merry 	{
257991554f2SKenneth D. Merry 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
258991554f2SKenneth D. Merry 
259991554f2SKenneth D. Merry 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
260991554f2SKenneth D. Merry 
261991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
2627a2a6a1aSStephen McConnell 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
263991554f2SKenneth D. Merry 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
264991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
265991554f2SKenneth D. Merry 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
266991554f2SKenneth D. Merry 			mprsas_discovery_end(sassc);
267991554f2SKenneth D. Merry 		}
268991554f2SKenneth D. Merry 		break;
269991554f2SKenneth D. Merry 	}
270991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
271991554f2SKenneth D. Merry 	{
272991554f2SKenneth D. Merry 		Mpi2EventDataSasEnclDevStatusChange_t *data;
273991554f2SKenneth D. Merry 		data = (Mpi2EventDataSasEnclDevStatusChange_t *)
274991554f2SKenneth D. Merry 		    fw_event->event_data;
275991554f2SKenneth D. Merry 		mpr_mapping_enclosure_dev_status_change_event(sc,
276991554f2SKenneth D. Merry 		    fw_event->event_data);
277991554f2SKenneth D. Merry 		break;
278991554f2SKenneth D. Merry 	}
279991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
280991554f2SKenneth D. Merry 	{
281991554f2SKenneth D. Merry 		Mpi2EventIrConfigElement_t *element;
282991554f2SKenneth D. Merry 		int i;
283991554f2SKenneth D. Merry 		u8 foreign_config, reason;
284991554f2SKenneth D. Merry 		u16 elementType;
285991554f2SKenneth D. Merry 		Mpi2EventDataIrConfigChangeList_t *event_data;
286991554f2SKenneth D. Merry 		struct mprsas_target *targ;
287991554f2SKenneth D. Merry 		unsigned int id;
288991554f2SKenneth D. Merry 
289991554f2SKenneth D. Merry 		event_data = fw_event->event_data;
290991554f2SKenneth D. Merry 		foreign_config = (le32toh(event_data->Flags) &
291991554f2SKenneth D. Merry 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
292991554f2SKenneth D. Merry 
293991554f2SKenneth D. Merry 		element =
294991554f2SKenneth D. Merry 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
295327f2e6cSStephen McConnell 		id = mpr_mapping_get_raid_tid_from_handle(sc,
296991554f2SKenneth D. Merry 		    element->VolDevHandle);
297991554f2SKenneth D. Merry 
298991554f2SKenneth D. Merry 		mpr_mapping_ir_config_change_event(sc, event_data);
299991554f2SKenneth D. Merry 		for (i = 0; i < event_data->NumElements; i++, element++) {
300991554f2SKenneth D. Merry 			reason = element->ReasonCode;
301991554f2SKenneth D. Merry 			elementType = le16toh(element->ElementFlags) &
302991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
303991554f2SKenneth D. Merry 			/*
304991554f2SKenneth D. Merry 			 * check for element type of Phys Disk or Hot Spare
305991554f2SKenneth D. Merry 			 */
306991554f2SKenneth D. Merry 			if ((elementType !=
307991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
308991554f2SKenneth D. Merry 			    && (elementType !=
309991554f2SKenneth D. Merry 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
310991554f2SKenneth D. Merry 				// do next element
311991554f2SKenneth D. Merry 				goto skip_fp_send;
312991554f2SKenneth D. Merry 
313991554f2SKenneth D. Merry 			/*
314991554f2SKenneth D. Merry 			 * check for reason of Hide, Unhide, PD Created, or PD
315991554f2SKenneth D. Merry 			 * Deleted
316991554f2SKenneth D. Merry 			 */
317991554f2SKenneth D. Merry 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
318991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
319991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
320991554f2SKenneth D. Merry 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
321991554f2SKenneth D. Merry 				goto skip_fp_send;
322991554f2SKenneth D. Merry 
323991554f2SKenneth D. Merry 			// check for a reason of Hide or PD Created
324991554f2SKenneth D. Merry 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
325991554f2SKenneth D. Merry 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
326991554f2SKenneth D. Merry 			{
327991554f2SKenneth D. Merry 				// build RAID Action message
328991554f2SKenneth D. Merry 				Mpi2RaidActionRequest_t	*action;
3296d4ffcb4SKenneth D. Merry 				Mpi2RaidActionReply_t *reply = NULL;
330991554f2SKenneth D. Merry 				struct mpr_command *cm;
331991554f2SKenneth D. Merry 				int error = 0;
332991554f2SKenneth D. Merry 				if ((cm = mpr_alloc_command(sc)) == NULL) {
333991554f2SKenneth D. Merry 					printf("%s: command alloc failed\n",
334991554f2SKenneth D. Merry 					    __func__);
335991554f2SKenneth D. Merry 					return;
336991554f2SKenneth D. Merry 				}
337991554f2SKenneth D. Merry 
338a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
339991554f2SKenneth D. Merry 				    "from "
340991554f2SKenneth D. Merry 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
341991554f2SKenneth D. Merry 				    ":\n");
342991554f2SKenneth D. Merry 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
343991554f2SKenneth D. Merry 				action->Function = MPI2_FUNCTION_RAID_ACTION;
344991554f2SKenneth D. Merry 				action->Action =
345991554f2SKenneth D. Merry 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
346991554f2SKenneth D. Merry 				action->PhysDiskNum = element->PhysDiskNum;
347991554f2SKenneth D. Merry 				cm->cm_desc.Default.RequestFlags =
348991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3496d4ffcb4SKenneth D. Merry 				error = mpr_request_polled(sc, &cm);
3506d4ffcb4SKenneth D. Merry 				if (cm != NULL)
3516d4ffcb4SKenneth D. Merry 					reply = (Mpi2RaidActionReply_t *)
3526d4ffcb4SKenneth D. Merry 					    cm->cm_reply;
353991554f2SKenneth D. Merry 				if (error || (reply == NULL)) {
354991554f2SKenneth D. Merry 					/* FIXME */
355991554f2SKenneth D. Merry 					/*
356991554f2SKenneth D. Merry 					 * If the poll returns error then we
357991554f2SKenneth D. Merry 					 * need to do diag reset
358991554f2SKenneth D. Merry 					 */
359991554f2SKenneth D. Merry 					printf("%s: poll for page completed "
360991554f2SKenneth D. Merry 					    "with error %d", __func__, error);
361991554f2SKenneth D. Merry 				}
362991554f2SKenneth D. Merry 				if (reply && (le16toh(reply->IOCStatus) &
363991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_MASK) !=
364991554f2SKenneth D. Merry 				    MPI2_IOCSTATUS_SUCCESS) {
365a2c14879SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: error "
366a2c14879SStephen McConnell 					    "sending RaidActionPage; "
367a2c14879SStephen McConnell 					    "iocstatus = 0x%x\n", __func__,
368991554f2SKenneth D. Merry 					    le16toh(reply->IOCStatus));
369991554f2SKenneth D. Merry 				}
370991554f2SKenneth D. Merry 
371991554f2SKenneth D. Merry 				if (cm)
372991554f2SKenneth D. Merry 					mpr_free_command(sc, cm);
373991554f2SKenneth D. Merry 			}
374991554f2SKenneth D. Merry skip_fp_send:
375a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_EVENT, "Received "
376991554f2SKenneth D. Merry 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
377991554f2SKenneth D. Merry 			    "code %x:\n", element->ReasonCode);
378991554f2SKenneth D. Merry 			switch (element->ReasonCode) {
379991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
380991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
381991554f2SKenneth D. Merry 				if (!foreign_config) {
382991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
383991554f2SKenneth D. Merry 					    le16toh(element->VolDevHandle))) {
384991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
385991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
386991554f2SKenneth D. Merry 						    __func__, le16toh(element->
387991554f2SKenneth D. Merry 						    VolDevHandle));
388991554f2SKenneth D. Merry 					}
389991554f2SKenneth D. Merry 				}
390991554f2SKenneth D. Merry 				break;
391991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
392991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
393991554f2SKenneth D. Merry 				/*
394991554f2SKenneth D. Merry 				 * Rescan after volume is deleted or removed.
395991554f2SKenneth D. Merry 				 */
396991554f2SKenneth D. Merry 				if (!foreign_config) {
397991554f2SKenneth D. Merry 					if (id == MPR_MAP_BAD_ID) {
398991554f2SKenneth D. Merry 						printf("%s: could not get ID "
399991554f2SKenneth D. Merry 						    "for volume with handle "
400991554f2SKenneth D. Merry 						    "0x%04x\n", __func__,
401991554f2SKenneth D. Merry 						    le16toh(element->
402991554f2SKenneth D. Merry 						    VolDevHandle));
403991554f2SKenneth D. Merry 						break;
404991554f2SKenneth D. Merry 					}
405991554f2SKenneth D. Merry 
406991554f2SKenneth D. Merry 					targ = &sassc->targets[id];
407991554f2SKenneth D. Merry 					targ->handle = 0x0;
408991554f2SKenneth D. Merry 					targ->encl_slot = 0x0;
409991554f2SKenneth D. Merry 					targ->encl_handle = 0x0;
410991554f2SKenneth D. Merry 					targ->encl_level_valid = 0x0;
411991554f2SKenneth D. Merry 					targ->encl_level = 0x0;
412991554f2SKenneth D. Merry 					targ->connector_name[0] = ' ';
413991554f2SKenneth D. Merry 					targ->connector_name[1] = ' ';
414991554f2SKenneth D. Merry 					targ->connector_name[2] = ' ';
415991554f2SKenneth D. Merry 					targ->connector_name[3] = ' ';
416991554f2SKenneth D. Merry 					targ->exp_dev_handle = 0x0;
417991554f2SKenneth D. Merry 					targ->phy_num = 0x0;
418991554f2SKenneth D. Merry 					targ->linkrate = 0x0;
419991554f2SKenneth D. Merry 					mprsas_rescan_target(sc, targ);
420991554f2SKenneth D. Merry 					printf("RAID target id 0x%x removed\n",
421991554f2SKenneth D. Merry 					    targ->tid);
422991554f2SKenneth D. Merry 				}
423991554f2SKenneth D. Merry 				break;
424991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
425991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
426991554f2SKenneth D. Merry 				/*
427991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been created.  Hide
428991554f2SKenneth D. Merry 				 * it from the OS.
429991554f2SKenneth D. Merry 				 */
430991554f2SKenneth D. Merry 				targ = mprsas_find_target_by_handle(sassc, 0,
431991554f2SKenneth D. Merry 				    element->PhysDiskDevHandle);
432991554f2SKenneth D. Merry 				if (targ == NULL)
433991554f2SKenneth D. Merry 					break;
434991554f2SKenneth D. Merry 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
435991554f2SKenneth D. Merry 				mprsas_rescan_target(sc, targ);
436991554f2SKenneth D. Merry 				break;
437991554f2SKenneth D. Merry 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
438991554f2SKenneth D. Merry 				/*
439991554f2SKenneth D. Merry 				 * Phys Disk of a volume has been deleted.
440991554f2SKenneth D. Merry 				 * Expose it to the OS.
441991554f2SKenneth D. Merry 				 */
442991554f2SKenneth D. Merry 				if (mprsas_add_device(sc,
443991554f2SKenneth D. Merry 				    le16toh(element->PhysDiskDevHandle), 0)) {
444991554f2SKenneth D. Merry 					printf("%s: failed to add device with "
445991554f2SKenneth D. Merry 					    "handle 0x%x\n", __func__,
446991554f2SKenneth D. Merry 					    le16toh(element->
447991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
448991554f2SKenneth D. Merry 					mprsas_prepare_remove(sassc,
449991554f2SKenneth D. Merry 					    le16toh(element->
450991554f2SKenneth D. Merry 					    PhysDiskDevHandle));
451991554f2SKenneth D. Merry 				}
452991554f2SKenneth D. Merry 				break;
453991554f2SKenneth D. Merry 			}
454991554f2SKenneth D. Merry 		}
455991554f2SKenneth D. Merry 		/*
456991554f2SKenneth D. Merry 		 * refcount was incremented for this event in
457991554f2SKenneth D. Merry 		 * mprsas_evt_handler.  Decrement it here because the event has
458991554f2SKenneth D. Merry 		 * been processed.
459991554f2SKenneth D. Merry 		 */
460991554f2SKenneth D. Merry 		mprsas_startup_decrement(sassc);
461991554f2SKenneth D. Merry 		break;
462991554f2SKenneth D. Merry 	}
463991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_VOLUME:
464991554f2SKenneth D. Merry 	{
465991554f2SKenneth D. Merry 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
466991554f2SKenneth D. Merry 
467991554f2SKenneth D. Merry 		/*
468991554f2SKenneth D. Merry 		 * Informational only.
469991554f2SKenneth D. Merry 		 */
470991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
471991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
472991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
473991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
474991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
475991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
476991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
477991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
478991554f2SKenneth D. Merry 			break;
479991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
480991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
481991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
482991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
483991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
484991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
485991554f2SKenneth D. Merry 			break;
486991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
487991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
488991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Volome with "
489991554f2SKenneth D. Merry  			    "handle 0x%x", le32toh(event_data->PreviousValue),
490991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
491991554f2SKenneth D. Merry  			    le16toh(event_data->VolDevHandle));
492991554f2SKenneth D. Merry 				u32 state;
493991554f2SKenneth D. Merry 				struct mprsas_target *targ;
494991554f2SKenneth D. Merry 				state = le32toh(event_data->NewValue);
495991554f2SKenneth D. Merry 				switch (state) {
496991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_MISSING:
497991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_FAILED:
498991554f2SKenneth D. Merry 					mprsas_prepare_volume_remove(sassc,
499991554f2SKenneth D. Merry 					    event_data->VolDevHandle);
500991554f2SKenneth D. Merry 					break;
501991554f2SKenneth D. Merry 
502991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_ONLINE:
503991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_DEGRADED:
504991554f2SKenneth D. Merry 				case MPI2_RAID_VOL_STATE_OPTIMAL:
505991554f2SKenneth D. Merry 					targ =
506991554f2SKenneth D. Merry 					    mprsas_find_target_by_handle(sassc,
507991554f2SKenneth D. Merry 					    0, event_data->VolDevHandle);
508991554f2SKenneth D. Merry 					if (targ) {
509991554f2SKenneth D. Merry 						printf("%s %d: Volume handle "
510991554f2SKenneth D. Merry 						    "0x%x is already added \n",
511991554f2SKenneth D. Merry 						    __func__, __LINE__,
512991554f2SKenneth D. Merry 						    event_data->VolDevHandle);
513991554f2SKenneth D. Merry 						break;
514991554f2SKenneth D. Merry 					}
515991554f2SKenneth D. Merry 					if (mprsas_volume_add(sc,
516991554f2SKenneth D. Merry 					    le16toh(event_data->
517991554f2SKenneth D. Merry 					    VolDevHandle))) {
518991554f2SKenneth D. Merry 						printf("%s: failed to add RAID "
519991554f2SKenneth D. Merry 						    "volume with handle 0x%x\n",
520991554f2SKenneth D. Merry 						    __func__, le16toh(
521991554f2SKenneth D. Merry 						    event_data->VolDevHandle));
522991554f2SKenneth D. Merry 					}
523991554f2SKenneth D. Merry 					break;
524991554f2SKenneth D. Merry 				default:
525991554f2SKenneth D. Merry 					break;
526991554f2SKenneth D. Merry 				}
527991554f2SKenneth D. Merry 			break;
528991554f2SKenneth D. Merry 		default:
529991554f2SKenneth D. Merry 			break;
530991554f2SKenneth D. Merry 		}
531991554f2SKenneth D. Merry 		break;
532991554f2SKenneth D. Merry 	}
533991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_PHYSICAL_DISK:
534991554f2SKenneth D. Merry 	{
535991554f2SKenneth D. Merry 		Mpi2EventDataIrPhysicalDisk_t *event_data =
536991554f2SKenneth D. Merry 		    fw_event->event_data;
537991554f2SKenneth D. Merry 		struct mprsas_target *targ;
538991554f2SKenneth D. Merry 
539991554f2SKenneth D. Merry 		/*
540991554f2SKenneth D. Merry 		 * Informational only.
541991554f2SKenneth D. Merry 		 */
542991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
543991554f2SKenneth D. Merry 		switch (event_data->ReasonCode) {
544991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
545991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
546991554f2SKenneth D. Merry   			    "changed from 0x%x to 0x%x for Phys Disk Number "
547991554f2SKenneth D. Merry   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
548991554f2SKenneth D. Merry  			    "%d", le32toh(event_data->PreviousValue),
549991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
550991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
551991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
552991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
553991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
554991554f2SKenneth D. Merry 			break;
555991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
556991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
557991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
558991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
559991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
560991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
561991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
562991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
563991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
564991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
565991554f2SKenneth D. Merry 			break;
566991554f2SKenneth D. Merry 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
567991554f2SKenneth D. Merry   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
568991554f2SKenneth D. Merry   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
569991554f2SKenneth D. Merry   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
570991554f2SKenneth D. Merry  			    le32toh(event_data->PreviousValue),
571991554f2SKenneth D. Merry  			    le32toh(event_data->NewValue),
572991554f2SKenneth D. Merry 			    event_data->PhysDiskNum,
573991554f2SKenneth D. Merry  			    le16toh(event_data->PhysDiskDevHandle),
574991554f2SKenneth D. Merry  			    le16toh(event_data->EnclosureHandle),
575991554f2SKenneth D. Merry 			    le16toh(event_data->Slot));
576991554f2SKenneth D. Merry 			switch (event_data->NewValue) {
577991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_ONLINE:
578991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_DEGRADED:
579991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_REBUILDING:
580991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OPTIMAL:
581991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_HOT_SPARE:
582991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
583991554f2SKenneth D. Merry 					    sassc, 0,
584991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
585991554f2SKenneth D. Merry 					if (targ) {
586991554f2SKenneth D. Merry 						targ->flags |=
587991554f2SKenneth D. Merry 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
588991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
589991554f2SKenneth D. Merry 						    "for handle 0x%x.\n",
590991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
591991554f2SKenneth D. Merry 						    event_data->
592991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
593991554f2SKenneth D. Merry 					}
594991554f2SKenneth D. Merry 				break;
595991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_OFFLINE:
596991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
597991554f2SKenneth D. Merry 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
598991554f2SKenneth D. Merry 				default:
599991554f2SKenneth D. Merry 					targ = mprsas_find_target_by_handle(
600991554f2SKenneth D. Merry 					    sassc, 0,
601991554f2SKenneth D. Merry 					    event_data->PhysDiskDevHandle);
602991554f2SKenneth D. Merry 					if (targ) {
603991554f2SKenneth D. Merry 						targ->flags |=
604991554f2SKenneth D. Merry 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
605991554f2SKenneth D. Merry 						printf("%s %d: Found Target "
606991554f2SKenneth D. Merry 						    "for handle 0x%x.  \n",
607991554f2SKenneth D. Merry 						    __func__, __LINE__ ,
608991554f2SKenneth D. Merry 						    event_data->
609991554f2SKenneth D. Merry 						    PhysDiskDevHandle);
610991554f2SKenneth D. Merry 					}
611991554f2SKenneth D. Merry 				break;
612991554f2SKenneth D. Merry 			}
613991554f2SKenneth D. Merry 		default:
614991554f2SKenneth D. Merry 			break;
615991554f2SKenneth D. Merry 		}
616991554f2SKenneth D. Merry 		break;
617991554f2SKenneth D. Merry 	}
618991554f2SKenneth D. Merry 	case MPI2_EVENT_IR_OPERATION_STATUS:
619991554f2SKenneth D. Merry 	{
620991554f2SKenneth D. Merry 		Mpi2EventDataIrOperationStatus_t *event_data =
621991554f2SKenneth D. Merry 		    fw_event->event_data;
622991554f2SKenneth D. Merry 
623991554f2SKenneth D. Merry 		/*
624991554f2SKenneth D. Merry 		 * Informational only.
625991554f2SKenneth D. Merry 		 */
626991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
627991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
628991554f2SKenneth D. Merry 		    "percent complete for Volume with handle 0x%x",
629991554f2SKenneth D. Merry 		    event_data->RAIDOperation, event_data->PercentComplete,
630991554f2SKenneth D. Merry 		    le16toh(event_data->VolDevHandle));
631991554f2SKenneth D. Merry 		break;
632991554f2SKenneth D. Merry 	}
633991554f2SKenneth D. Merry 	case MPI2_EVENT_TEMP_THRESHOLD:
634991554f2SKenneth D. Merry 	{
635991554f2SKenneth D. Merry 		pMpi2EventDataTemperature_t	temp_event;
636991554f2SKenneth D. Merry 
637991554f2SKenneth D. Merry 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
638991554f2SKenneth D. Merry 
639991554f2SKenneth D. Merry 		/*
640991554f2SKenneth D. Merry 		 * The Temp Sensor Count must be greater than the event's Sensor
641991554f2SKenneth D. Merry 		 * Num to be valid.  If valid, print the temp thresholds that
642991554f2SKenneth D. Merry 		 * have been exceeded.
643991554f2SKenneth D. Merry 		 */
644991554f2SKenneth D. Merry 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
645991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
646991554f2SKenneth D. Merry 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
647991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
648991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
649991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
650991554f2SKenneth D. Merry 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
651991554f2SKenneth D. Merry 			    temp_event->SensorNum);
652991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
653991554f2SKenneth D. Merry 			    "%d\n", temp_event->CurrentTemperature);
654991554f2SKenneth D. Merry 		}
655991554f2SKenneth D. Merry 		break;
656991554f2SKenneth D. Merry 	}
6572bbc5fcbSStephen McConnell 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
6582bbc5fcbSStephen McConnell 	{
6592bbc5fcbSStephen McConnell 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
6602bbc5fcbSStephen McConnell 		ace_event_data =
6612bbc5fcbSStephen McConnell 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
6622bbc5fcbSStephen McConnell 
6630656476aSAlexander Motin 		switch(ace_event_data->ReasonCode) {
6640656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
6650656476aSAlexander Motin 		{
6660656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6672bbc5fcbSStephen McConnell 			    "ReceptacleID %d cannot be powered and device "
6682bbc5fcbSStephen McConnell 			    "connected to this active cable will not be seen. "
6692bbc5fcbSStephen McConnell 			    "This active cable requires %d mW of power.\n",
6702bbc5fcbSStephen McConnell 			    ace_event_data->ReceptacleID,
6712bbc5fcbSStephen McConnell 			    ace_event_data->ActiveCablePowerRequirement);
6720656476aSAlexander Motin 			break;
6730656476aSAlexander Motin 		}
6740656476aSAlexander Motin 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
6750656476aSAlexander Motin 		{
6760656476aSAlexander Motin 			mpr_printf(sc, "Currently a cable with "
6770656476aSAlexander Motin 			    "ReceptacleID %d is not running at optimal speed "
6780656476aSAlexander Motin 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
6790656476aSAlexander Motin 			break;
6800656476aSAlexander Motin 		}
6810656476aSAlexander Motin 		default:
6820656476aSAlexander Motin 			break;
6832bbc5fcbSStephen McConnell 		}
6842bbc5fcbSStephen McConnell 		break;
6852bbc5fcbSStephen McConnell 	}
6865f5baf0eSAlexander Motin 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
6875f5baf0eSAlexander Motin 	{
6885f5baf0eSAlexander Motin 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
6895f5baf0eSAlexander Motin 		uint64_t sas_address;
6905f5baf0eSAlexander Motin 
6915f5baf0eSAlexander Motin 		discovery_error_data =
6925f5baf0eSAlexander Motin 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
6935f5baf0eSAlexander Motin 		    fw_event->event_data;
6945f5baf0eSAlexander Motin 
6955f5baf0eSAlexander Motin 		sas_address = discovery_error_data->SASAddress.High;
6965f5baf0eSAlexander Motin 		sas_address = (sas_address << 32) |
6975f5baf0eSAlexander Motin 		    discovery_error_data->SASAddress.Low;
6985f5baf0eSAlexander Motin 
6995f5baf0eSAlexander Motin 		switch(discovery_error_data->ReasonCode) {
7005f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
7015f5baf0eSAlexander Motin 		{
7025f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command failed during discovery "
7035f5baf0eSAlexander Motin 			    "for expander with SAS Address %jx and "
7045f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7055f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7065f5baf0eSAlexander Motin 			break;
7075f5baf0eSAlexander Motin 		}
7085f5baf0eSAlexander Motin 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
7095f5baf0eSAlexander Motin 		{
7105f5baf0eSAlexander Motin 			mpr_printf(sc, "SMP command timed out during "
7115f5baf0eSAlexander Motin 			    "discovery for expander with SAS Address %jx and "
7125f5baf0eSAlexander Motin 			    "handle 0x%x.\n", sas_address,
7135f5baf0eSAlexander Motin 			    discovery_error_data->DevHandle);
7145f5baf0eSAlexander Motin 			break;
7155f5baf0eSAlexander Motin 		}
7165f5baf0eSAlexander Motin 		default:
7175f5baf0eSAlexander Motin 			break;
7185f5baf0eSAlexander Motin 		}
7195f5baf0eSAlexander Motin 		break;
7205f5baf0eSAlexander Motin 	}
72167feec50SStephen McConnell 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
72267feec50SStephen McConnell 	{
72367feec50SStephen McConnell 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
72467feec50SStephen McConnell 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
72567feec50SStephen McConnell 		uint8_t i, link_rate;
72667feec50SStephen McConnell 		uint16_t handle;
72767feec50SStephen McConnell 
72867feec50SStephen McConnell 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
72967feec50SStephen McConnell 		    fw_event->event_data;
73067feec50SStephen McConnell 
73167feec50SStephen McConnell 		mpr_mapping_pcie_topology_change_event(sc,
73267feec50SStephen McConnell 		    fw_event->event_data);
73367feec50SStephen McConnell 
73467feec50SStephen McConnell 		for (i = 0; i < data->NumEntries; i++) {
73567feec50SStephen McConnell 			port_entry = &data->PortEntry[i];
73667feec50SStephen McConnell 			handle = le16toh(port_entry->AttachedDevHandle);
73767feec50SStephen McConnell 			link_rate = port_entry->CurrentPortInfo &
73867feec50SStephen McConnell 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
73967feec50SStephen McConnell 			switch (port_entry->PortStatus) {
74067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
74167feec50SStephen McConnell 				if (link_rate <
74267feec50SStephen McConnell 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
74367feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
74467feec50SStephen McConnell 					    "add PCIe device with handle 0x%x "
74567feec50SStephen McConnell 					    "with unknown link rate.\n",
74667feec50SStephen McConnell 					    __func__, handle);
74767feec50SStephen McConnell 					break;
74867feec50SStephen McConnell 				}
74967feec50SStephen McConnell 				if (mprsas_add_pcie_device(sc, handle,
75067feec50SStephen McConnell 				    link_rate)) {
75167feec50SStephen McConnell 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
75267feec50SStephen McConnell 					    "to add PCIe device with handle "
75367feec50SStephen McConnell 					    "0x%x\n", __func__, handle);
75467feec50SStephen McConnell 					mprsas_prepare_remove(sassc, handle);
75567feec50SStephen McConnell 				}
75667feec50SStephen McConnell 				break;
75767feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
75867feec50SStephen McConnell 				mprsas_prepare_remove(sassc, handle);
75967feec50SStephen McConnell 				break;
76067feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
76167feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
76267feec50SStephen McConnell 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
76367feec50SStephen McConnell 			default:
76467feec50SStephen McConnell 				break;
76567feec50SStephen McConnell 			}
76667feec50SStephen McConnell 		}
76767feec50SStephen McConnell 		/*
76867feec50SStephen McConnell 		 * refcount was incremented for this event in
76967feec50SStephen McConnell 		 * mprsas_evt_handler.  Decrement it here because the event has
77067feec50SStephen McConnell 		 * been processed.
77167feec50SStephen McConnell 		 */
77267feec50SStephen McConnell 		mprsas_startup_decrement(sassc);
77367feec50SStephen McConnell 		break;
77467feec50SStephen McConnell 	}
775991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
776991554f2SKenneth D. Merry 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
777991554f2SKenneth D. Merry 	default:
778991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
779991554f2SKenneth D. Merry 		    fw_event->event);
780991554f2SKenneth D. Merry 		break;
781991554f2SKenneth D. Merry 
782991554f2SKenneth D. Merry 	}
783991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
784991554f2SKenneth D. Merry 	    __func__, fw_event->event);
785991554f2SKenneth D. Merry 	mprsas_fw_event_free(sc, fw_event);
786991554f2SKenneth D. Merry }
787991554f2SKenneth D. Merry 
788991554f2SKenneth D. Merry void
789991554f2SKenneth D. Merry mprsas_firmware_event_work(void *arg, int pending)
790991554f2SKenneth D. Merry {
791991554f2SKenneth D. Merry 	struct mpr_fw_event_work *fw_event;
792991554f2SKenneth D. Merry 	struct mpr_softc *sc;
793991554f2SKenneth D. Merry 
794991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
795991554f2SKenneth D. Merry 	mpr_lock(sc);
796991554f2SKenneth D. Merry 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
797991554f2SKenneth D. Merry 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
798991554f2SKenneth D. Merry 		mprsas_fw_work(sc, fw_event);
799991554f2SKenneth D. Merry 	}
800991554f2SKenneth D. Merry 	mpr_unlock(sc);
801991554f2SKenneth D. Merry }
802991554f2SKenneth D. Merry 
803991554f2SKenneth D. Merry static int
80467feec50SStephen McConnell mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
80567feec50SStephen McConnell {
806991554f2SKenneth D. Merry 	char devstring[80];
807991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
808991554f2SKenneth D. Merry 	struct mprsas_target *targ;
809991554f2SKenneth D. Merry 	Mpi2ConfigReply_t mpi_reply;
810991554f2SKenneth D. Merry 	Mpi2SasDevicePage0_t config_page;
811a2c14879SStephen McConnell 	uint64_t sas_address, parent_sas_address = 0;
812991554f2SKenneth D. Merry 	u32 device_info, parent_devinfo = 0;
813991554f2SKenneth D. Merry 	unsigned int id;
814a2c14879SStephen McConnell 	int ret = 1, error = 0, i;
815991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
816a2c14879SStephen McConnell 	u8 is_SATA_SSD = 0;
817a2c14879SStephen McConnell 	struct mpr_command *cm;
818991554f2SKenneth D. Merry 
819991554f2SKenneth D. Merry 	sassc = sc->sassc;
820991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
821aeb9ac0dSScott Long 	if (mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
822aeb9ac0dSScott Long 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
823aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
824aeb9ac0dSScott Long 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
825aeb9ac0dSScott Long 		    handle, mpi_reply.IOCStatus);
826991554f2SKenneth D. Merry 		error = ENXIO;
827991554f2SKenneth D. Merry 		goto out;
828991554f2SKenneth D. Merry 	}
829991554f2SKenneth D. Merry 
830991554f2SKenneth D. Merry 	device_info = le32toh(config_page.DeviceInfo);
831991554f2SKenneth D. Merry 
832991554f2SKenneth D. Merry 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
833991554f2SKenneth D. Merry 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
834991554f2SKenneth D. Merry 		Mpi2ConfigReply_t tmp_mpi_reply;
835991554f2SKenneth D. Merry 		Mpi2SasDevicePage0_t parent_config_page;
836991554f2SKenneth D. Merry 
837aeb9ac0dSScott Long 		if (mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
838991554f2SKenneth D. Merry 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
839aeb9ac0dSScott Long 		    le16toh(config_page.ParentDevHandle)) != 0) {
840757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_FAULT,
841aeb9ac0dSScott Long 			    "Error reading parent SAS device %#x page0, "
842aeb9ac0dSScott Long 			    "iocstatus= 0x%x\n",
843aeb9ac0dSScott Long 			    le16toh(config_page.ParentDevHandle),
844aeb9ac0dSScott Long 			    tmp_mpi_reply.IOCStatus);
845991554f2SKenneth D. Merry 		} else {
846991554f2SKenneth D. Merry 			parent_sas_address = parent_config_page.SASAddress.High;
847991554f2SKenneth D. Merry 			parent_sas_address = (parent_sas_address << 32) |
848991554f2SKenneth D. Merry 			    parent_config_page.SASAddress.Low;
849991554f2SKenneth D. Merry 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
850991554f2SKenneth D. Merry 		}
851991554f2SKenneth D. Merry 	}
852453130d9SPedro F. Giffuni 	/* TODO Check proper endianness */
853991554f2SKenneth D. Merry 	sas_address = config_page.SASAddress.High;
854a2c14879SStephen McConnell 	sas_address = (sas_address << 32) | config_page.SASAddress.Low;
855757ff642SScott Long 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
856757ff642SScott Long 	    "page0 = %jx\n", handle, sas_address);
857991554f2SKenneth D. Merry 
858a2c14879SStephen McConnell 	/*
859a2c14879SStephen McConnell 	 * Always get SATA Identify information because this is used to
860a2c14879SStephen McConnell 	 * determine if Start/Stop Unit should be sent to the drive when the
861a2c14879SStephen McConnell 	 * system is shutdown.
862a2c14879SStephen McConnell 	 */
863991554f2SKenneth D. Merry 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
864a2c14879SStephen McConnell 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
865a2c14879SStephen McConnell 		    handle, device_info, &is_SATA_SSD);
866a2c14879SStephen McConnell 		if (ret) {
867757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
868757ff642SScott Long 			    "%s: failed to get disk type (SSD or HDD) for SATA "
869757ff642SScott Long 			    "device with handle 0x%04x\n",
870a2c14879SStephen McConnell 			    __func__, handle);
871a2c14879SStephen McConnell 		} else {
872757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
873757ff642SScott Long 			    "from SATA device = %jx\n", handle, sas_address);
874a2c14879SStephen McConnell 		}
875a2c14879SStephen McConnell 	}
876c91d307fSStephen McConnell 
8774ab1cdc5SScott Long 	/*
8784ab1cdc5SScott Long 	 * use_phynum:
8794ab1cdc5SScott Long 	 *  1 - use the PhyNum field as a fallback to the mapping logic
8804ab1cdc5SScott Long 	 *  0 - never use the PhyNum field
8814ab1cdc5SScott Long 	 * -1 - only use the PhyNum field
882327f2e6cSStephen McConnell 	 *
883327f2e6cSStephen McConnell 	 * Note that using the Phy number to map a device can cause device adds
884327f2e6cSStephen McConnell 	 * to fail if multiple enclosures/expanders are in the topology. For
885327f2e6cSStephen McConnell 	 * example, if two devices are in the same slot number in two different
886327f2e6cSStephen McConnell 	 * enclosures within the topology, only one of those devices will be
887327f2e6cSStephen McConnell 	 * added. PhyNum mapping should not be used if multiple enclosures are
888327f2e6cSStephen McConnell 	 * in the topology.
8894ab1cdc5SScott Long 	 */
8904ab1cdc5SScott Long 	id = MPR_MAP_BAD_ID;
8914ab1cdc5SScott Long 	if (sc->use_phynum != -1)
892327f2e6cSStephen McConnell 		id = mpr_mapping_get_tid(sc, sas_address, handle);
893991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
89467feec50SStephen McConnell 		if ((sc->use_phynum == 0) ||
89567feec50SStephen McConnell 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
8964ab1cdc5SScott Long 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
8974ab1cdc5SScott Long 			    "Could not get ID for device with handle 0x%04x\n",
8984ab1cdc5SScott Long 			    __FILE__, __LINE__, __func__, handle);
899991554f2SKenneth D. Merry 			error = ENXIO;
900991554f2SKenneth D. Merry 			goto out;
901991554f2SKenneth D. Merry 		}
9024ab1cdc5SScott Long 	}
903327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
904327f2e6cSStephen McConnell 	    __func__, id);
905991554f2SKenneth D. Merry 
906327f2e6cSStephen McConnell 	/*
907327f2e6cSStephen McConnell 	 * Only do the ID check and reuse check if the target is not from a
908327f2e6cSStephen McConnell 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
909327f2e6cSStephen McConnell 	 * when a volume is deleted because the mapping entry for the PD will
910327f2e6cSStephen McConnell 	 * still be in the mapping table. The ID check should not be done here
911327f2e6cSStephen McConnell 	 * either since this PD is already being used.
912327f2e6cSStephen McConnell 	 */
913327f2e6cSStephen McConnell 	targ = &sassc->targets[id];
914327f2e6cSStephen McConnell 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
915991554f2SKenneth D. Merry 		if (mprsas_check_id(sassc, id) != 0) {
916757ff642SScott Long 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
917757ff642SScott Long 			    "Excluding target id %d\n", id);
918991554f2SKenneth D. Merry 			error = ENXIO;
919991554f2SKenneth D. Merry 			goto out;
920991554f2SKenneth D. Merry 		}
921991554f2SKenneth D. Merry 
9224ab1cdc5SScott Long 		if (targ->handle != 0x0) {
923327f2e6cSStephen McConnell 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
924327f2e6cSStephen McConnell 			    "target id %d handle 0x%04x\n", id, targ->handle);
9254ab1cdc5SScott Long 			error = ENXIO;
9264ab1cdc5SScott Long 			goto out;
9274ab1cdc5SScott Long 		}
928327f2e6cSStephen McConnell 	}
9294ab1cdc5SScott Long 
930991554f2SKenneth D. Merry 	targ->devinfo = device_info;
931991554f2SKenneth D. Merry 	targ->devname = le32toh(config_page.DeviceName.High);
932991554f2SKenneth D. Merry 	targ->devname = (targ->devname << 32) |
933991554f2SKenneth D. Merry 	    le32toh(config_page.DeviceName.Low);
934991554f2SKenneth D. Merry 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
935991554f2SKenneth D. Merry 	targ->encl_slot = le16toh(config_page.Slot);
936991554f2SKenneth D. Merry 	targ->encl_level = config_page.EnclosureLevel;
937991554f2SKenneth D. Merry 	targ->connector_name[0] = config_page.ConnectorName[0];
938991554f2SKenneth D. Merry 	targ->connector_name[1] = config_page.ConnectorName[1];
939991554f2SKenneth D. Merry 	targ->connector_name[2] = config_page.ConnectorName[2];
940991554f2SKenneth D. Merry 	targ->connector_name[3] = config_page.ConnectorName[3];
941991554f2SKenneth D. Merry 	targ->handle = handle;
942991554f2SKenneth D. Merry 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
943991554f2SKenneth D. Merry 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
944991554f2SKenneth D. Merry 	targ->parent_sasaddr = le64toh(parent_sas_address);
945991554f2SKenneth D. Merry 	targ->parent_devinfo = parent_devinfo;
946991554f2SKenneth D. Merry 	targ->tid = id;
947991554f2SKenneth D. Merry 	targ->linkrate = (linkrate>>4);
948991554f2SKenneth D. Merry 	targ->flags = 0;
949a2c14879SStephen McConnell 	if (is_SATA_SSD) {
950a2c14879SStephen McConnell 		targ->flags = MPR_TARGET_IS_SATA_SSD;
951a2c14879SStephen McConnell 	}
95267feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
95367feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
95467feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
95567feec50SStephen McConnell 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
956991554f2SKenneth D. Merry 		targ->scsi_req_desc_type =
957991554f2SKenneth D. Merry 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
958991554f2SKenneth D. Merry 	}
959991554f2SKenneth D. Merry 	if (le16toh(config_page.Flags) &
960991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
961991554f2SKenneth D. Merry 		targ->encl_level_valid = TRUE;
962991554f2SKenneth D. Merry 	}
963991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
964991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
965991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
966991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
967991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
968991554f2SKenneth D. Merry 		free(lun, M_MPR);
969991554f2SKenneth D. Merry 	}
970991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
971991554f2SKenneth D. Merry 
972991554f2SKenneth D. Merry 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
973a2c14879SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
974991554f2SKenneth D. Merry 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
975991554f2SKenneth D. Merry 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
976991554f2SKenneth D. Merry 	    targ->handle, targ->encl_handle, targ->encl_slot);
977991554f2SKenneth D. Merry 	if (targ->encl_level_valid) {
978a2c14879SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
979991554f2SKenneth D. Merry 		    "and connector name (%4s)\n", targ->encl_level,
980991554f2SKenneth D. Merry 		    targ->connector_name);
981991554f2SKenneth D. Merry 	}
982a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
983a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
984991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
985991554f2SKenneth D. Merry #endif
986991554f2SKenneth D. Merry 		mprsas_rescan_target(sc, targ);
987991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
988a2c14879SStephen McConnell 
989a2c14879SStephen McConnell 	/*
990a2c14879SStephen McConnell 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
991a2c14879SStephen McConnell 	 * If so, send a Target Reset TM to the target that was just created.
992a2c14879SStephen McConnell 	 * An Abort Task TM should be used instead of a Target Reset, but that
993a2c14879SStephen McConnell 	 * would be much more difficult because targets have not been fully
994a2c14879SStephen McConnell 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
995a2c14879SStephen McConnell 	 * target instead of the LUN.
996a2c14879SStephen McConnell 	 */
997a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
998a2c14879SStephen McConnell 		cm = &sc->commands[i];
999a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1000a2c14879SStephen McConnell 			targ->timeouts++;
1001a2c14879SStephen McConnell 			cm->cm_state = MPR_CM_STATE_TIMEDOUT;
1002a2c14879SStephen McConnell 
1003a2c14879SStephen McConnell 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1004a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1005a2c14879SStephen McConnell 				    "Reset for stuck SATA identify command "
1006a2c14879SStephen McConnell 				    "(cm = %p)\n", __func__, cm);
1007a2c14879SStephen McConnell 				targ->tm->cm_targ = targ;
1008a2c14879SStephen McConnell 				mprsas_send_reset(sc, targ->tm,
1009a2c14879SStephen McConnell 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1010a2c14879SStephen McConnell 			} else {
1011a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
10127a2a6a1aSStephen McConnell 				    "tm for Target Reset after SATA ID command "
10137a2a6a1aSStephen McConnell 				    "timed out (cm %p)\n", cm);
1014a2c14879SStephen McConnell 			}
1015a2c14879SStephen McConnell 			/*
1016a2c14879SStephen McConnell 			 * No need to check for more since the target is
1017a2c14879SStephen McConnell 			 * already being reset.
1018a2c14879SStephen McConnell 			 */
1019a2c14879SStephen McConnell 			break;
1020a2c14879SStephen McConnell 		}
1021a2c14879SStephen McConnell 	}
1022991554f2SKenneth D. Merry out:
1023a2c14879SStephen McConnell 	/*
1024a2c14879SStephen McConnell 	 * Free the commands that may not have been freed from the SATA ID call
1025a2c14879SStephen McConnell 	 */
1026a2c14879SStephen McConnell 	for (i = 1; i < sc->num_reqs; i++) {
1027a2c14879SStephen McConnell 		cm = &sc->commands[i];
1028a2c14879SStephen McConnell 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
10298277ce2bSConrad Meyer 			free(cm->cm_data, M_MPR);
1030a2c14879SStephen McConnell 			mpr_free_command(sc, cm);
1031a2c14879SStephen McConnell 		}
1032a2c14879SStephen McConnell 	}
1033991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1034991554f2SKenneth D. Merry 	return (error);
1035991554f2SKenneth D. Merry }
1036991554f2SKenneth D. Merry 
1037991554f2SKenneth D. Merry int
1038991554f2SKenneth D. Merry mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1039a2c14879SStephen McConnell     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1040991554f2SKenneth D. Merry {
1041991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t mpi_reply;
1042991554f2SKenneth D. Merry 	int i, rc, try_count;
1043991554f2SKenneth D. Merry 	u32 *bufferptr;
1044991554f2SKenneth D. Merry 	union _sata_sas_address hash_address;
1045991554f2SKenneth D. Merry 	struct _ata_identify_device_data ata_identify;
1046991554f2SKenneth D. Merry 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1047991554f2SKenneth D. Merry 	u32 ioc_status;
1048991554f2SKenneth D. Merry 	u8 sas_status;
1049991554f2SKenneth D. Merry 
1050991554f2SKenneth D. Merry 	memset(&ata_identify, 0, sizeof(ata_identify));
10514e02badbSAlan Somers 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1052991554f2SKenneth D. Merry 	try_count = 0;
1053991554f2SKenneth D. Merry 	do {
1054991554f2SKenneth D. Merry 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1055991554f2SKenneth D. Merry 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1056991554f2SKenneth D. Merry 		try_count++;
1057991554f2SKenneth D. Merry 		ioc_status = le16toh(mpi_reply.IOCStatus)
1058991554f2SKenneth D. Merry 		    & MPI2_IOCSTATUS_MASK;
1059991554f2SKenneth D. Merry 		sas_status = mpi_reply.SASStatus;
1060a92fe027SAlan Somers 		switch (ioc_status) {
1061a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SUCCESS:
1062a92fe027SAlan Somers 			break;
1063a92fe027SAlan Somers 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1064a92fe027SAlan Somers 			/* No sense sleeping.  this error won't get better */
1065a92fe027SAlan Somers 			break;
1066a92fe027SAlan Somers 		default:
1067a2c14879SStephen McConnell 			if (sc->spinup_wait_time > 0) {
1068a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1069a2c14879SStephen McConnell 				    "after SATA ID error to wait for spinup\n",
1070a2c14879SStephen McConnell 				    sc->spinup_wait_time);
1071a2c14879SStephen McConnell 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1072a2c14879SStephen McConnell 				    "mprid", sc->spinup_wait_time * hz);
1073a2c14879SStephen McConnell 			}
1074a2c14879SStephen McConnell 		}
1075a92fe027SAlan Somers 	} while (((rc && (rc != EWOULDBLOCK)) ||
10767a2a6a1aSStephen McConnell 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1077a92fe027SAlan Somers 	    || sas_status) && (try_count < 5));
1078991554f2SKenneth D. Merry 
1079991554f2SKenneth D. Merry 	if (rc == 0 && !ioc_status && !sas_status) {
1080991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1081991554f2SKenneth D. Merry 		    "successfully for handle = 0x%x with try_count = %d\n",
1082991554f2SKenneth D. Merry 		    __func__, handle, try_count);
1083991554f2SKenneth D. Merry 	} else {
1084991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1085991554f2SKenneth D. Merry 		    __func__, handle);
1086991554f2SKenneth D. Merry 		return -1;
1087991554f2SKenneth D. Merry 	}
1088991554f2SKenneth D. Merry 	/* Copy & byteswap the 40 byte model number to a buffer */
1089991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1090991554f2SKenneth D. Merry 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1091991554f2SKenneth D. Merry 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1092991554f2SKenneth D. Merry 	}
1093991554f2SKenneth D. Merry 	/* Copy & byteswap the 20 byte serial number to a buffer */
1094991554f2SKenneth D. Merry 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1095991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i] =
1096991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i + 1];
1097991554f2SKenneth D. Merry 		buffer[MPT2SAS_MN_LEN + i + 1] =
1098991554f2SKenneth D. Merry 		    ((u8 *)ata_identify.serial_number)[i];
1099991554f2SKenneth D. Merry 	}
1100991554f2SKenneth D. Merry 	bufferptr = (u32 *)buffer;
1101991554f2SKenneth D. Merry 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1102991554f2SKenneth D. Merry 	 * so loop through the first 56 bytes (7*8),
1103991554f2SKenneth D. Merry 	 * and then add in the last dword.
1104991554f2SKenneth D. Merry 	 */
1105991554f2SKenneth D. Merry 	hash_address.word.low  = 0;
1106991554f2SKenneth D. Merry 	hash_address.word.high = 0;
1107991554f2SKenneth D. Merry 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1108991554f2SKenneth D. Merry 		hash_address.word.low += *bufferptr;
1109991554f2SKenneth D. Merry 		bufferptr++;
1110991554f2SKenneth D. Merry 		hash_address.word.high += *bufferptr;
1111991554f2SKenneth D. Merry 		bufferptr++;
1112991554f2SKenneth D. Merry 	}
1113991554f2SKenneth D. Merry 	/* Add the last dword */
1114991554f2SKenneth D. Merry 	hash_address.word.low += *bufferptr;
1115991554f2SKenneth D. Merry 	/* Make sure the hash doesn't start with 5, because it could clash
1116991554f2SKenneth D. Merry 	 * with a SAS address. Change 5 to a D.
1117991554f2SKenneth D. Merry 	 */
1118991554f2SKenneth D. Merry 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1119991554f2SKenneth D. Merry 		hash_address.word.high |= 0x00000080;
1120991554f2SKenneth D. Merry 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1121991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1122991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1123991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1124991554f2SKenneth D. Merry 	    (u64)hash_address.wwid[7];
1125a2c14879SStephen McConnell 	if (ata_identify.rotational_speed == 1) {
1126a2c14879SStephen McConnell 		*is_SATA_SSD = 1;
1127a2c14879SStephen McConnell 	}
1128a2c14879SStephen McConnell 
1129991554f2SKenneth D. Merry 	return 0;
1130991554f2SKenneth D. Merry }
1131991554f2SKenneth D. Merry 
1132991554f2SKenneth D. Merry static int
1133991554f2SKenneth D. Merry mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1134991554f2SKenneth D. Merry     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1135991554f2SKenneth D. Merry {
1136991554f2SKenneth D. Merry 	Mpi2SataPassthroughRequest_t *mpi_request;
1137991554f2SKenneth D. Merry 	Mpi2SataPassthroughReply_t *reply;
1138991554f2SKenneth D. Merry 	struct mpr_command *cm;
1139991554f2SKenneth D. Merry 	char *buffer;
1140991554f2SKenneth D. Merry 	int error = 0;
1141991554f2SKenneth D. Merry 
1142991554f2SKenneth D. Merry 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1143991554f2SKenneth D. Merry 	if (!buffer)
1144991554f2SKenneth D. Merry 		return ENOMEM;
1145991554f2SKenneth D. Merry 
1146991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1147991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1148991554f2SKenneth D. Merry 		return (EBUSY);
1149991554f2SKenneth D. Merry 	}
1150991554f2SKenneth D. Merry 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1151991554f2SKenneth D. Merry 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1152991554f2SKenneth D. Merry 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1153991554f2SKenneth D. Merry 	mpi_request->VF_ID = 0;
1154991554f2SKenneth D. Merry 	mpi_request->DevHandle = htole16(handle);
1155991554f2SKenneth D. Merry 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1156991554f2SKenneth D. Merry 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1157991554f2SKenneth D. Merry 	mpi_request->DataLength = htole32(sz);
1158991554f2SKenneth D. Merry 	mpi_request->CommandFIS[0] = 0x27;
1159991554f2SKenneth D. Merry 	mpi_request->CommandFIS[1] = 0x80;
1160991554f2SKenneth D. Merry 	mpi_request->CommandFIS[2] =  (devinfo &
1161991554f2SKenneth D. Merry 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1162991554f2SKenneth D. Merry 	cm->cm_sge = &mpi_request->SGL;
1163991554f2SKenneth D. Merry 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1164991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1165991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1166991554f2SKenneth D. Merry 	cm->cm_data = buffer;
1167991554f2SKenneth D. Merry 	cm->cm_length = htole32(sz);
1168a2c14879SStephen McConnell 
1169a2c14879SStephen McConnell 	/*
1170*86312e46SConrad Meyer 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
1171*86312e46SConrad Meyer 	 * This fixes a problem where the FW does not send a reply sometimes
1172a2c14879SStephen McConnell 	 * when a bad disk is in the topology. So, this is used to timeout the
1173a2c14879SStephen McConnell 	 * command so that processing can continue normally.
1174a2c14879SStephen McConnell 	 */
1175*86312e46SConrad Meyer 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1176a2c14879SStephen McConnell 
1177*86312e46SConrad Meyer 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
1178*86312e46SConrad Meyer 
1179*86312e46SConrad Meyer 	/* mprsas_ata_id_timeout does not reset controller */
1180*86312e46SConrad Meyer 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
1181*86312e46SConrad Meyer 
1182991554f2SKenneth D. Merry 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1183991554f2SKenneth D. Merry 	if (error || (reply == NULL)) {
1184991554f2SKenneth D. Merry 		/* FIXME */
1185991554f2SKenneth D. Merry 		/*
1186991554f2SKenneth D. Merry 		 * If the request returns an error then we need to do a diag
1187991554f2SKenneth D. Merry 		 * reset
1188991554f2SKenneth D. Merry 		 */
1189aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1190aeb9ac0dSScott Long 		    "Request for SATA PASSTHROUGH page completed with error %d",
1191aeb9ac0dSScott Long 		    error);
1192991554f2SKenneth D. Merry 		error = ENXIO;
1193991554f2SKenneth D. Merry 		goto out;
1194991554f2SKenneth D. Merry 	}
1195991554f2SKenneth D. Merry 	bcopy(buffer, id_buffer, sz);
1196991554f2SKenneth D. Merry 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1197991554f2SKenneth D. Merry 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1198991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
1199aeb9ac0dSScott Long 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1200aeb9ac0dSScott Long 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1201aeb9ac0dSScott Long 		    handle, reply->IOCStatus);
1202991554f2SKenneth D. Merry 		error = ENXIO;
1203991554f2SKenneth D. Merry 		goto out;
1204991554f2SKenneth D. Merry 	}
1205991554f2SKenneth D. Merry out:
1206a2c14879SStephen McConnell 	/*
1207a2c14879SStephen McConnell 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
12088277ce2bSConrad Meyer 	 * it.  The command and buffer will be freed after sending an Abort
1209*86312e46SConrad Meyer 	 * Task TM.
1210a2c14879SStephen McConnell 	 */
12118277ce2bSConrad Meyer 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1212991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1213991554f2SKenneth D. Merry 		free(buffer, M_MPR);
1214*86312e46SConrad Meyer 	}
1215991554f2SKenneth D. Merry 	return (error);
1216991554f2SKenneth D. Merry }
1217991554f2SKenneth D. Merry 
1218a2c14879SStephen McConnell static void
1219*86312e46SConrad Meyer mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1220a2c14879SStephen McConnell {
1221a2c14879SStephen McConnell 
1222*86312e46SConrad Meyer 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1223a2c14879SStephen McConnell 	    __func__, cm, sc);
1224a2c14879SStephen McConnell 
1225a2c14879SStephen McConnell 	/*
1226*86312e46SConrad Meyer 	 * The Abort Task cannot be sent from here because the driver has not
1227*86312e46SConrad Meyer 	 * completed setting up targets.  Instead, the command is flagged so
1228*86312e46SConrad Meyer 	 * that special handling will be used to send the abort.
1229a2c14879SStephen McConnell 	 */
1230a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1231a2c14879SStephen McConnell }
1232a2c14879SStephen McConnell 
1233991554f2SKenneth D. Merry static int
123467feec50SStephen McConnell mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
123567feec50SStephen McConnell {
123667feec50SStephen McConnell 	char devstring[80];
123767feec50SStephen McConnell 	struct mprsas_softc *sassc;
123867feec50SStephen McConnell 	struct mprsas_target *targ;
123967feec50SStephen McConnell 	Mpi2ConfigReply_t mpi_reply;
124067feec50SStephen McConnell 	Mpi26PCIeDevicePage0_t config_page;
124167feec50SStephen McConnell 	Mpi26PCIeDevicePage2_t config_page2;
124267feec50SStephen McConnell 	uint64_t pcie_wwid, parent_wwid = 0;
124367feec50SStephen McConnell 	u32 device_info, parent_devinfo = 0;
124467feec50SStephen McConnell 	unsigned int id;
124567feec50SStephen McConnell 	int error = 0;
124667feec50SStephen McConnell 	struct mprsas_lun *lun;
124767feec50SStephen McConnell 
124867feec50SStephen McConnell 	sassc = sc->sassc;
124967feec50SStephen McConnell 	mprsas_startup_increment(sassc);
125067feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
125167feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
125267feec50SStephen McConnell 		printf("%s: error reading PCIe device page0\n", __func__);
125367feec50SStephen McConnell 		error = ENXIO;
125467feec50SStephen McConnell 		goto out;
125567feec50SStephen McConnell 	}
125667feec50SStephen McConnell 
125767feec50SStephen McConnell 	device_info = le32toh(config_page.DeviceInfo);
125867feec50SStephen McConnell 
125967feec50SStephen McConnell 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
126067feec50SStephen McConnell 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
126167feec50SStephen McConnell 		Mpi2ConfigReply_t tmp_mpi_reply;
126267feec50SStephen McConnell 		Mpi26PCIeDevicePage0_t parent_config_page;
126367feec50SStephen McConnell 
126467feec50SStephen McConnell 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
126567feec50SStephen McConnell 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
126667feec50SStephen McConnell 		     le16toh(config_page.ParentDevHandle)))) {
126767feec50SStephen McConnell 			printf("%s: error reading PCIe device %#x page0\n",
126867feec50SStephen McConnell 			    __func__, le16toh(config_page.ParentDevHandle));
126967feec50SStephen McConnell 		} else {
127067feec50SStephen McConnell 			parent_wwid = parent_config_page.WWID.High;
127167feec50SStephen McConnell 			parent_wwid = (parent_wwid << 32) |
127267feec50SStephen McConnell 			    parent_config_page.WWID.Low;
127367feec50SStephen McConnell 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
127467feec50SStephen McConnell 		}
127567feec50SStephen McConnell 	}
127667feec50SStephen McConnell 	/* TODO Check proper endianness */
127767feec50SStephen McConnell 	pcie_wwid = config_page.WWID.High;
127867feec50SStephen McConnell 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
127967feec50SStephen McConnell 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
128067feec50SStephen McConnell 	    pcie_wwid);
128167feec50SStephen McConnell 
128267feec50SStephen McConnell 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
128367feec50SStephen McConnell 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
128467feec50SStephen McConnell 		printf("%s: error reading PCIe device page2\n", __func__);
128567feec50SStephen McConnell 		error = ENXIO;
128667feec50SStephen McConnell 		goto out;
128767feec50SStephen McConnell 	}
128867feec50SStephen McConnell 
1289327f2e6cSStephen McConnell 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
129067feec50SStephen McConnell 	if (id == MPR_MAP_BAD_ID) {
1291327f2e6cSStephen McConnell 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1292327f2e6cSStephen McConnell 		    "Could not get ID for device with handle 0x%04x\n",
1293327f2e6cSStephen McConnell 		    __FILE__, __LINE__, __func__, handle);
129467feec50SStephen McConnell 		error = ENXIO;
129567feec50SStephen McConnell 		goto out;
129667feec50SStephen McConnell 	}
1297327f2e6cSStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1298327f2e6cSStephen McConnell 	    __func__, id);
129967feec50SStephen McConnell 
130067feec50SStephen McConnell 	if (mprsas_check_id(sassc, id) != 0) {
1301757ff642SScott Long 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1302757ff642SScott Long 		    "Excluding target id %d\n", id);
130367feec50SStephen McConnell 		error = ENXIO;
130467feec50SStephen McConnell 		goto out;
130567feec50SStephen McConnell 	}
130667feec50SStephen McConnell 
130767feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
130867feec50SStephen McConnell 	    pcie_wwid);
130967feec50SStephen McConnell 	targ = &sassc->targets[id];
131067feec50SStephen McConnell 	targ->devinfo = device_info;
131167feec50SStephen McConnell 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
131267feec50SStephen McConnell 	targ->encl_slot = le16toh(config_page.Slot);
131367feec50SStephen McConnell 	targ->encl_level = config_page.EnclosureLevel;
131467feec50SStephen McConnell 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
131567feec50SStephen McConnell 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
131667feec50SStephen McConnell 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
131767feec50SStephen McConnell 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
131867feec50SStephen McConnell 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
131967feec50SStephen McConnell 	targ->MDTS = config_page2.MaximumDataTransferSize;
132067feec50SStephen McConnell 	/*
132167feec50SStephen McConnell 	 * Assume always TRUE for encl_level_valid because there is no valid
132267feec50SStephen McConnell 	 * flag for PCIe.
132367feec50SStephen McConnell 	 */
132467feec50SStephen McConnell 	targ->encl_level_valid = TRUE;
132567feec50SStephen McConnell 	targ->handle = handle;
132667feec50SStephen McConnell 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
132767feec50SStephen McConnell 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
132867feec50SStephen McConnell 	targ->parent_sasaddr = le64toh(parent_wwid);
132967feec50SStephen McConnell 	targ->parent_devinfo = parent_devinfo;
133067feec50SStephen McConnell 	targ->tid = id;
133167feec50SStephen McConnell 	targ->linkrate = linkrate;
133267feec50SStephen McConnell 	targ->flags = 0;
133367feec50SStephen McConnell 	if ((le16toh(config_page.Flags) &
133467feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
133567feec50SStephen McConnell 	    (le16toh(config_page.Flags) &
133667feec50SStephen McConnell 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
133767feec50SStephen McConnell 		targ->scsi_req_desc_type =
133867feec50SStephen McConnell 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
133967feec50SStephen McConnell 	}
134067feec50SStephen McConnell 	TAILQ_INIT(&targ->commands);
134167feec50SStephen McConnell 	TAILQ_INIT(&targ->timedout_commands);
134267feec50SStephen McConnell 	while (!SLIST_EMPTY(&targ->luns)) {
134367feec50SStephen McConnell 		lun = SLIST_FIRST(&targ->luns);
134467feec50SStephen McConnell 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
134567feec50SStephen McConnell 		free(lun, M_MPR);
134667feec50SStephen McConnell 	}
134767feec50SStephen McConnell 	SLIST_INIT(&targ->luns);
134867feec50SStephen McConnell 
134967feec50SStephen McConnell 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
135067feec50SStephen McConnell 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
135167feec50SStephen McConnell 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
135267feec50SStephen McConnell 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
135367feec50SStephen McConnell 	    targ->handle, targ->encl_handle, targ->encl_slot);
135467feec50SStephen McConnell 	if (targ->encl_level_valid) {
135567feec50SStephen McConnell 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
135667feec50SStephen McConnell 		    "and connector name (%4s)\n", targ->encl_level,
135767feec50SStephen McConnell 		    targ->connector_name);
135867feec50SStephen McConnell 	}
135967feec50SStephen McConnell #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
136067feec50SStephen McConnell     (__FreeBSD_version < 902502)
136167feec50SStephen McConnell 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
136267feec50SStephen McConnell #endif
136367feec50SStephen McConnell 		mprsas_rescan_target(sc, targ);
136467feec50SStephen McConnell 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
136567feec50SStephen McConnell 
136667feec50SStephen McConnell out:
136767feec50SStephen McConnell 	mprsas_startup_decrement(sassc);
136867feec50SStephen McConnell 	return (error);
136967feec50SStephen McConnell }
137067feec50SStephen McConnell 
137167feec50SStephen McConnell static int
1372991554f2SKenneth D. Merry mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1373991554f2SKenneth D. Merry {
1374991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1375991554f2SKenneth D. Merry 	struct mprsas_target *targ;
1376991554f2SKenneth D. Merry 	u64 wwid;
1377991554f2SKenneth D. Merry 	unsigned int id;
1378991554f2SKenneth D. Merry 	int error = 0;
1379991554f2SKenneth D. Merry 	struct mprsas_lun *lun;
1380991554f2SKenneth D. Merry 
1381991554f2SKenneth D. Merry 	sassc = sc->sassc;
1382991554f2SKenneth D. Merry 	mprsas_startup_increment(sassc);
1383991554f2SKenneth D. Merry 	/* wwid is endian safe */
1384991554f2SKenneth D. Merry 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1385991554f2SKenneth D. Merry 	if (!wwid) {
1386991554f2SKenneth D. Merry 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1387991554f2SKenneth D. Merry 		    __func__);
1388991554f2SKenneth D. Merry 		error = ENXIO;
1389991554f2SKenneth D. Merry 		goto out;
1390991554f2SKenneth D. Merry 	}
1391991554f2SKenneth D. Merry 
1392327f2e6cSStephen McConnell 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1393991554f2SKenneth D. Merry 	if (id == MPR_MAP_BAD_ID) {
1394991554f2SKenneth D. Merry 		printf("%s: could not get ID for volume with handle 0x%04x and "
1395991554f2SKenneth D. Merry 		    "WWID 0x%016llx\n", __func__, handle,
1396991554f2SKenneth D. Merry 		    (unsigned long long)wwid);
1397991554f2SKenneth D. Merry 		error = ENXIO;
1398991554f2SKenneth D. Merry 		goto out;
1399991554f2SKenneth D. Merry 	}
1400991554f2SKenneth D. Merry 
1401991554f2SKenneth D. Merry 	targ = &sassc->targets[id];
1402991554f2SKenneth D. Merry 	targ->tid = id;
1403991554f2SKenneth D. Merry 	targ->handle = handle;
1404991554f2SKenneth D. Merry 	targ->devname = wwid;
1405991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->commands);
1406991554f2SKenneth D. Merry 	TAILQ_INIT(&targ->timedout_commands);
1407991554f2SKenneth D. Merry 	while (!SLIST_EMPTY(&targ->luns)) {
1408991554f2SKenneth D. Merry 		lun = SLIST_FIRST(&targ->luns);
1409991554f2SKenneth D. Merry 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1410991554f2SKenneth D. Merry 		free(lun, M_MPR);
1411991554f2SKenneth D. Merry 	}
1412991554f2SKenneth D. Merry 	SLIST_INIT(&targ->luns);
1413a371d6f9SKenneth D. Merry #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
1414a371d6f9SKenneth D. Merry     (__FreeBSD_version < 902502)
1415991554f2SKenneth D. Merry 	if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
1416991554f2SKenneth D. Merry #endif
1417991554f2SKenneth D. Merry 		mprsas_rescan_target(sc, targ);
1418991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1419991554f2SKenneth D. Merry 	    targ->tid, wwid);
1420991554f2SKenneth D. Merry out:
1421991554f2SKenneth D. Merry 	mprsas_startup_decrement(sassc);
1422991554f2SKenneth D. Merry 	return (error);
1423991554f2SKenneth D. Merry }
1424991554f2SKenneth D. Merry 
1425991554f2SKenneth D. Merry /**
1426991554f2SKenneth D. Merry  * mprsas_SSU_to_SATA_devices
1427991554f2SKenneth D. Merry  * @sc: per adapter object
1428991554f2SKenneth D. Merry  *
1429991554f2SKenneth D. Merry  * Looks through the target list and issues a StartStopUnit SCSI command to each
1430991554f2SKenneth D. Merry  * SATA direct-access device.  This helps to ensure that data corruption is
1431991554f2SKenneth D. Merry  * avoided when the system is being shut down.  This must be called after the IR
1432991554f2SKenneth D. Merry  * System Shutdown RAID Action is sent if in IR mode.
1433991554f2SKenneth D. Merry  *
1434991554f2SKenneth D. Merry  * Return nothing.
1435991554f2SKenneth D. Merry  */
1436991554f2SKenneth D. Merry static void
1437acc173a6SWarner Losh mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto)
1438991554f2SKenneth D. Merry {
1439991554f2SKenneth D. Merry 	struct mprsas_softc *sassc = sc->sassc;
1440991554f2SKenneth D. Merry 	union ccb *ccb;
1441991554f2SKenneth D. Merry 	path_id_t pathid = cam_sim_path(sassc->sim);
1442991554f2SKenneth D. Merry 	target_id_t targetid;
1443991554f2SKenneth D. Merry 	struct mprsas_target *target;
1444991554f2SKenneth D. Merry 	char path_str[64];
1445acc173a6SWarner Losh 	int timeout;
1446991554f2SKenneth D. Merry 
144763941e98SKenneth D. Merry 	mpr_lock(sc);
144863941e98SKenneth D. Merry 
1449991554f2SKenneth D. Merry 	/*
1450a2c14879SStephen McConnell 	 * For each target, issue a StartStopUnit command to stop the device.
1451991554f2SKenneth D. Merry 	 */
1452991554f2SKenneth D. Merry 	sc->SSU_started = TRUE;
1453991554f2SKenneth D. Merry 	sc->SSU_refcount = 0;
1454327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1455991554f2SKenneth D. Merry 		target = &sassc->targets[targetid];
1456991554f2SKenneth D. Merry 		if (target->handle == 0x0) {
1457991554f2SKenneth D. Merry 			continue;
1458991554f2SKenneth D. Merry 		}
1459991554f2SKenneth D. Merry 
1460fa699bb2SAlan Somers 		/*
1461fa699bb2SAlan Somers 		 * The stop_at_shutdown flag will be set if this device is
1462fa699bb2SAlan Somers 		 * a SATA direct-access end device.
1463fa699bb2SAlan Somers 		 */
1464fa699bb2SAlan Somers 		if (target->stop_at_shutdown) {
1465991554f2SKenneth D. Merry 			ccb = xpt_alloc_ccb_nowait();
1466991554f2SKenneth D. Merry 			if (ccb == NULL) {
146767feec50SStephen McConnell 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
146867feec50SStephen McConnell 				    "to stop unit.\n");
1469991554f2SKenneth D. Merry 				return;
1470991554f2SKenneth D. Merry 			}
1471991554f2SKenneth D. Merry 
1472a2c14879SStephen McConnell 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1473a2c14879SStephen McConnell 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1474a2c14879SStephen McConnell 			    CAM_REQ_CMP) {
1475a2c14879SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1476a2c14879SStephen McConnell 				    "path to stop unit.\n");
1477991554f2SKenneth D. Merry 				xpt_free_ccb(ccb);
1478991554f2SKenneth D. Merry 				return;
1479991554f2SKenneth D. Merry 			}
1480991554f2SKenneth D. Merry 			xpt_path_string(ccb->ccb_h.path, path_str,
1481991554f2SKenneth D. Merry 			    sizeof(path_str));
1482991554f2SKenneth D. Merry 
1483a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1484a2c14879SStephen McConnell 			    "handle %d\n", path_str, target->handle);
1485991554f2SKenneth D. Merry 
1486991554f2SKenneth D. Merry 			/*
1487a2c14879SStephen McConnell 			 * Issue a START STOP UNIT command for the target.
1488a2c14879SStephen McConnell 			 * Increment the SSU counter to be used to count the
1489a2c14879SStephen McConnell 			 * number of required replies.
1490991554f2SKenneth D. Merry 			 */
1491a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1492991554f2SKenneth D. Merry 			sc->SSU_refcount++;
1493991554f2SKenneth D. Merry 			ccb->ccb_h.target_id =
1494991554f2SKenneth D. Merry 			    xpt_path_target_id(ccb->ccb_h.path);
1495991554f2SKenneth D. Merry 			ccb->ccb_h.ppriv_ptr1 = sassc;
1496991554f2SKenneth D. Merry 			scsi_start_stop(&ccb->csio,
1497991554f2SKenneth D. Merry 			    /*retries*/0,
1498991554f2SKenneth D. Merry 			    mprsas_stop_unit_done,
1499991554f2SKenneth D. Merry 			    MSG_SIMPLE_Q_TAG,
1500991554f2SKenneth D. Merry 			    /*start*/FALSE,
1501991554f2SKenneth D. Merry 			    /*load/eject*/0,
1502991554f2SKenneth D. Merry 			    /*immediate*/FALSE,
1503991554f2SKenneth D. Merry 			    MPR_SENSE_LEN,
1504991554f2SKenneth D. Merry 			    /*timeout*/10000);
1505991554f2SKenneth D. Merry 			xpt_action(ccb);
1506991554f2SKenneth D. Merry 		}
1507991554f2SKenneth D. Merry 	}
1508991554f2SKenneth D. Merry 
150963941e98SKenneth D. Merry 	mpr_unlock(sc);
151063941e98SKenneth D. Merry 
1511991554f2SKenneth D. Merry 	/*
1512acc173a6SWarner Losh 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1513acc173a6SWarner Losh 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1514991554f2SKenneth D. Merry 	 */
1515acc173a6SWarner Losh 	timeout = 600;
1516acc173a6SWarner Losh 	if (howto & RB_NOSYNC)
1517acc173a6SWarner Losh 		timeout = 100;
1518991554f2SKenneth D. Merry 
1519acc173a6SWarner Losh 	/*
1520acc173a6SWarner Losh 	 * Wait until all of the SSU commands have completed or time
1521acc173a6SWarner Losh 	 * has expired. Pause for 100ms each time through.  If any
1522acc173a6SWarner Losh 	 * command times out, the target will be reset in the SCSI
1523acc173a6SWarner Losh 	 * command timeout routine.
1524acc173a6SWarner Losh 	 */
1525acc173a6SWarner Losh 	while (sc->SSU_refcount > 0) {
1526acc173a6SWarner Losh 		pause("mprwait", hz/10);
1527acc173a6SWarner Losh 		if (SCHEDULER_STOPPED())
1528acc173a6SWarner Losh 			xpt_sim_poll(sassc->sim);
1529acc173a6SWarner Losh 
1530acc173a6SWarner Losh 		if (--timeout == 0) {
1531a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1532991554f2SKenneth D. Merry 			    "for SSU commands to complete.\n");
1533991554f2SKenneth D. Merry 			break;
1534991554f2SKenneth D. Merry 		}
1535991554f2SKenneth D. Merry 	}
1536991554f2SKenneth D. Merry }
1537991554f2SKenneth D. Merry 
1538991554f2SKenneth D. Merry static void
1539991554f2SKenneth D. Merry mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1540991554f2SKenneth D. Merry {
1541991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
1542991554f2SKenneth D. Merry 	char path_str[64];
1543991554f2SKenneth D. Merry 
15445d21655fSStephen McConnell 	if (done_ccb == NULL)
15455d21655fSStephen McConnell 		return;
15465d21655fSStephen McConnell 
1547991554f2SKenneth D. Merry 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1548991554f2SKenneth D. Merry 
1549991554f2SKenneth D. Merry 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1550991554f2SKenneth D. Merry 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1551991554f2SKenneth D. Merry 	    path_str);
1552991554f2SKenneth D. Merry 
1553991554f2SKenneth D. Merry 	/*
1554991554f2SKenneth D. Merry 	 * Nothing more to do except free the CCB and path.  If the command
1555991554f2SKenneth D. Merry 	 * timed out, an abort reset, then target reset will be issued during
1556991554f2SKenneth D. Merry 	 * the SCSI Command process.
1557991554f2SKenneth D. Merry 	 */
1558991554f2SKenneth D. Merry 	xpt_free_path(done_ccb->ccb_h.path);
1559991554f2SKenneth D. Merry 	xpt_free_ccb(done_ccb);
1560991554f2SKenneth D. Merry }
1561991554f2SKenneth D. Merry 
1562991554f2SKenneth D. Merry /**
1563991554f2SKenneth D. Merry  * mprsas_ir_shutdown - IR shutdown notification
1564991554f2SKenneth D. Merry  * @sc: per adapter object
1565991554f2SKenneth D. Merry  *
1566991554f2SKenneth D. Merry  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1567991554f2SKenneth D. Merry  * the host system is shutting down.
1568991554f2SKenneth D. Merry  *
1569991554f2SKenneth D. Merry  * Return nothing.
1570991554f2SKenneth D. Merry  */
1571991554f2SKenneth D. Merry void
1572acc173a6SWarner Losh mprsas_ir_shutdown(struct mpr_softc *sc, int howto)
1573991554f2SKenneth D. Merry {
1574991554f2SKenneth D. Merry 	u16 volume_mapping_flags;
1575991554f2SKenneth D. Merry 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1576991554f2SKenneth D. Merry 	struct dev_mapping_table *mt_entry;
1577991554f2SKenneth D. Merry 	u32 start_idx, end_idx;
1578991554f2SKenneth D. Merry 	unsigned int id, found_volume = 0;
1579991554f2SKenneth D. Merry 	struct mpr_command *cm;
1580991554f2SKenneth D. Merry 	Mpi2RaidActionRequest_t	*action;
1581a2c14879SStephen McConnell 	target_id_t targetid;
1582a2c14879SStephen McConnell 	struct mprsas_target *target;
1583991554f2SKenneth D. Merry 
1584991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1585991554f2SKenneth D. Merry 
1586991554f2SKenneth D. Merry 	/* is IR firmware build loaded? */
1587991554f2SKenneth D. Merry 	if (!sc->ir_firmware)
1588991554f2SKenneth D. Merry 		goto out;
1589991554f2SKenneth D. Merry 
1590991554f2SKenneth D. Merry 	/* are there any volumes?  Look at IR target IDs. */
1591991554f2SKenneth D. Merry 	// TODO-later, this should be looked up in the RAID config structure
1592991554f2SKenneth D. Merry 	// when it is implemented.
1593991554f2SKenneth D. Merry 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1594991554f2SKenneth D. Merry 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1595991554f2SKenneth D. Merry 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1596991554f2SKenneth D. Merry 		start_idx = 0;
1597991554f2SKenneth D. Merry 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1598991554f2SKenneth D. Merry 			start_idx = 1;
1599991554f2SKenneth D. Merry 	} else
1600991554f2SKenneth D. Merry 		start_idx = sc->max_devices - sc->max_volumes;
1601991554f2SKenneth D. Merry 	end_idx = start_idx + sc->max_volumes - 1;
1602991554f2SKenneth D. Merry 
1603991554f2SKenneth D. Merry 	for (id = start_idx; id < end_idx; id++) {
1604991554f2SKenneth D. Merry 		mt_entry = &sc->mapping_table[id];
1605991554f2SKenneth D. Merry 		if ((mt_entry->physical_id != 0) &&
1606991554f2SKenneth D. Merry 		    (mt_entry->missing_count == 0)) {
1607991554f2SKenneth D. Merry 			found_volume = 1;
1608991554f2SKenneth D. Merry 			break;
1609991554f2SKenneth D. Merry 		}
1610991554f2SKenneth D. Merry 	}
1611991554f2SKenneth D. Merry 
1612991554f2SKenneth D. Merry 	if (!found_volume)
1613991554f2SKenneth D. Merry 		goto out;
1614991554f2SKenneth D. Merry 
1615991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1616991554f2SKenneth D. Merry 		printf("%s: command alloc failed\n", __func__);
1617991554f2SKenneth D. Merry 		goto out;
1618991554f2SKenneth D. Merry 	}
1619991554f2SKenneth D. Merry 
1620991554f2SKenneth D. Merry 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1621991554f2SKenneth D. Merry 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1622991554f2SKenneth D. Merry 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1623991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1624991554f2SKenneth D. Merry 	mpr_lock(sc);
16256d4ffcb4SKenneth D. Merry 	mpr_wait_command(sc, &cm, 5, CAN_SLEEP);
1626991554f2SKenneth D. Merry 	mpr_unlock(sc);
1627991554f2SKenneth D. Merry 
1628991554f2SKenneth D. Merry 	/*
1629991554f2SKenneth D. Merry 	 * Don't check for reply, just leave.
1630991554f2SKenneth D. Merry 	 */
1631991554f2SKenneth D. Merry 	if (cm)
1632991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1633991554f2SKenneth D. Merry 
1634991554f2SKenneth D. Merry out:
1635a2c14879SStephen McConnell 	/*
1636a2c14879SStephen McConnell 	 * All of the targets must have the correct value set for
1637a2c14879SStephen McConnell 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1638a2c14879SStephen McConnell 	 *
1639a2c14879SStephen McConnell 	 * The possible values for the 'enable_ssu' variable are:
1640a2c14879SStephen McConnell 	 * 0: disable to SSD and HDD
1641a2c14879SStephen McConnell 	 * 1: disable only to HDD (default)
1642a2c14879SStephen McConnell 	 * 2: disable only to SSD
1643a2c14879SStephen McConnell 	 * 3: enable to SSD and HDD
1644a2c14879SStephen McConnell 	 * anything else will default to 1.
1645a2c14879SStephen McConnell 	 */
1646327f2e6cSStephen McConnell 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1647a2c14879SStephen McConnell 		target = &sc->sassc->targets[targetid];
1648a2c14879SStephen McConnell 		if (target->handle == 0x0) {
1649a2c14879SStephen McConnell 			continue;
1650a2c14879SStephen McConnell 		}
1651a2c14879SStephen McConnell 
1652a2c14879SStephen McConnell 		if (target->supports_SSU) {
1653a2c14879SStephen McConnell 			switch (sc->enable_ssu) {
1654a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1655a2c14879SStephen McConnell 				target->stop_at_shutdown = FALSE;
1656a2c14879SStephen McConnell 				break;
1657a2c14879SStephen McConnell 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1658a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1659a2c14879SStephen McConnell 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1660a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1661a2c14879SStephen McConnell 				}
1662a2c14879SStephen McConnell 				break;
1663a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1664a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1665a2c14879SStephen McConnell 				break;
1666a2c14879SStephen McConnell 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1667a2c14879SStephen McConnell 			default:
1668a2c14879SStephen McConnell 				target->stop_at_shutdown = TRUE;
1669a2c14879SStephen McConnell 				if ((target->flags &
1670a2c14879SStephen McConnell 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1671a2c14879SStephen McConnell 					target->stop_at_shutdown = FALSE;
1672a2c14879SStephen McConnell 				}
1673a2c14879SStephen McConnell 				break;
1674a2c14879SStephen McConnell 			}
1675a2c14879SStephen McConnell 		}
1676a2c14879SStephen McConnell 	}
1677acc173a6SWarner Losh 	mprsas_SSU_to_SATA_devices(sc, howto);
1678991554f2SKenneth D. Merry }
1679