xref: /freebsd/sys/dev/mpr/mpr_sas_lsi.c (revision 8ef8c6abfadfc9eb0465ce57c6b09ca310415bdd)
1 /*-
2  * Copyright (c) 2011-2015 LSI Corp.
3  * Copyright (c) 2013-2016 Avago Technologies
4  * Copyright 2000-2020 Broadcom Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
29  */
30 
31 /* Communications core for Avago Technologies (LSI) MPT3 */
32 
33 /* TODO Move headers to mprvar */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/selinfo.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/bio.h>
43 #include <sys/malloc.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46 #include <sys/endian.h>
47 #include <sys/proc.h>
48 #include <sys/queue.h>
49 #include <sys/kthread.h>
50 #include <sys/taskqueue.h>
51 #include <sys/sbuf.h>
52 #include <sys/reboot.h>
53 #include <sys/stdarg.h>
54 
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 #include <sys/rman.h>
58 
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_debug.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_periph.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68 
69 #include <dev/mpr/mpi/mpi2_type.h>
70 #include <dev/mpr/mpi/mpi2.h>
71 #include <dev/mpr/mpi/mpi2_ioc.h>
72 #include <dev/mpr/mpi/mpi2_sas.h>
73 #include <dev/mpr/mpi/mpi2_pci.h>
74 #include <dev/mpr/mpi/mpi2_cnfg.h>
75 #include <dev/mpr/mpi/mpi2_init.h>
76 #include <dev/mpr/mpi/mpi2_raid.h>
77 #include <dev/mpr/mpi/mpi2_tool.h>
78 #include <dev/mpr/mpr_ioctl.h>
79 #include <dev/mpr/mprvar.h>
80 #include <dev/mpr/mpr_table.h>
81 #include <dev/mpr/mpr_sas.h>
82 
83 /* For Hashed SAS Address creation for SATA Drives */
84 #define MPT2SAS_SN_LEN 20
85 #define MPT2SAS_MN_LEN 40
86 
87 struct mpr_fw_event_work {
88 	u16			event;
89 	void			*event_data;
90 	TAILQ_ENTRY(mpr_fw_event_work)	ev_link;
91 };
92 
93 union _sata_sas_address {
94 	u8 wwid[8];
95 	struct {
96 		u32 high;
97 		u32 low;
98 	} word;
99 };
100 
101 /*
102  * define the IDENTIFY DEVICE structure
103  */
104 struct _ata_identify_device_data {
105 	u16 reserved1[10];	/* 0-9 */
106 	u16 serial_number[10];	/* 10-19 */
107 	u16 reserved2[7];	/* 20-26 */
108 	u16 model_number[20];	/* 27-46*/
109 	u16 reserved3[170];	/* 47-216 */
110 	u16 rotational_speed;	/* 217 */
111 	u16 reserved4[38];	/* 218-255 */
112 };
113 static u32 event_count;
114 static void mprsas_fw_work(struct mpr_softc *sc,
115     struct mpr_fw_event_work *fw_event);
116 static void mprsas_fw_event_free(struct mpr_softc *,
117     struct mpr_fw_event_work *);
118 static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
119 static int mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle,
120     u8 linkrate);
121 static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
122     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
123     u32 devinfo);
124 static void mprsas_ata_id_complete(struct mpr_softc *, struct mpr_command *);
125 static void mprsas_ata_id_timeout(struct mpr_softc *, struct mpr_command *);
126 int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
127     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
128 static int mprsas_volume_add(struct mpr_softc *sc,
129     u16 handle);
130 static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto);
131 static void mprsas_stop_unit_done(struct cam_periph *periph,
132     union ccb *done_ccb);
133 
134 void
mprsas_evt_handler(struct mpr_softc * sc,uintptr_t data,MPI2_EVENT_NOTIFICATION_REPLY * event)135 mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
136     MPI2_EVENT_NOTIFICATION_REPLY *event)
137 {
138 	struct mpr_fw_event_work *fw_event;
139 	u16 sz;
140 
141 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
142 	MPR_DPRINT_EVENT(sc, sas, event);
143 	mprsas_record_event(sc, event);
144 
145 	fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
146 	     M_ZERO|M_NOWAIT);
147 	if (!fw_event) {
148 		printf("%s: allocate failed for fw_event\n", __func__);
149 		return;
150 	}
151 	sz = le16toh(event->EventDataLength) * 4;
152 	fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
153 	if (!fw_event->event_data) {
154 		printf("%s: allocate failed for event_data\n", __func__);
155 		free(fw_event, M_MPR);
156 		return;
157 	}
158 
159 	bcopy(event->EventData, fw_event->event_data, sz);
160 	fw_event->event = le16toh(event->Event);
161 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
162 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
163 	    fw_event->event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
164 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
165 	    sc->track_mapping_events)
166 		sc->pending_map_events++;
167 
168 	/*
169 	 * When wait_for_port_enable flag is set, make sure that all the events
170 	 * are processed. Increment the startup_refcount and decrement it after
171 	 * events are processed.
172 	 */
173 	if ((fw_event->event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
174 	    fw_event->event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
175 	    fw_event->event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
176 	    sc->wait_for_port_enable)
177 		mprsas_startup_increment(sc->sassc);
178 
179 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
180 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
181 }
182 
183 static void
mprsas_fw_event_free(struct mpr_softc * sc,struct mpr_fw_event_work * fw_event)184 mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
185 {
186 
187 	free(fw_event->event_data, M_MPR);
188 	free(fw_event, M_MPR);
189 }
190 
191 /**
192  * _mpr_fw_work - delayed task for processing firmware events
193  * @sc: per adapter object
194  * @fw_event: The fw_event_work object
195  * Context: user.
196  *
197  * Return nothing.
198  */
199 static void
mprsas_fw_work(struct mpr_softc * sc,struct mpr_fw_event_work * fw_event)200 mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
201 {
202 	struct mprsas_softc *sassc;
203 	sassc = sc->sassc;
204 
205 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
206 	    event_count++, __func__, fw_event->event);
207 	switch (fw_event->event) {
208 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
209 	{
210 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
211 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
212 		uint8_t i;
213 
214 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
215 		    fw_event->event_data;
216 
217 		mpr_mapping_topology_change_event(sc, fw_event->event_data);
218 
219 		for (i = 0; i < data->NumEntries; i++) {
220 			phy = &data->PHY[i];
221 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
222 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
223 				if (mprsas_add_device(sc,
224 				    le16toh(phy->AttachedDevHandle),
225 				    phy->LinkRate)) {
226 					mpr_dprint(sc, MPR_ERROR, "%s: "
227 					    "failed to add device with handle "
228 					    "0x%x\n", __func__,
229 					    le16toh(phy->AttachedDevHandle));
230 					mprsas_prepare_remove(sassc, le16toh(
231 					    phy->AttachedDevHandle));
232 				}
233 				break;
234 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
235 				mprsas_prepare_remove(sassc, le16toh(
236 				    phy->AttachedDevHandle));
237 				break;
238 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
239 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
240 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
241 			default:
242 				break;
243 			}
244 		}
245 		/*
246 		 * refcount was incremented for this event in
247 		 * mprsas_evt_handler.  Decrement it here because the event has
248 		 * been processed.
249 		 */
250 		mprsas_startup_decrement(sassc);
251 		break;
252 	}
253 	case MPI2_EVENT_SAS_DISCOVERY:
254 	{
255 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
256 
257 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
258 
259 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
260 			mpr_dprint(sc, MPR_TRACE,"SAS discovery start event\n");
261 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
262 			mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
263 			sassc->flags &= ~MPRSAS_IN_DISCOVERY;
264 			mprsas_discovery_end(sassc);
265 		}
266 		break;
267 	}
268 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
269 	{
270 		mpr_mapping_enclosure_dev_status_change_event(sc,
271 		    fw_event->event_data);
272 		break;
273 	}
274 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
275 	{
276 		Mpi2EventIrConfigElement_t *element;
277 		int i;
278 		u8 foreign_config, reason;
279 		u16 elementType;
280 		Mpi2EventDataIrConfigChangeList_t *event_data;
281 		struct mprsas_target *targ;
282 		unsigned int id;
283 
284 		event_data = fw_event->event_data;
285 		foreign_config = (le32toh(event_data->Flags) &
286 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
287 
288 		element =
289 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
290 		id = mpr_mapping_get_raid_tid_from_handle(sc,
291 		    element->VolDevHandle);
292 
293 		mpr_mapping_ir_config_change_event(sc, event_data);
294 		for (i = 0; i < event_data->NumElements; i++, element++) {
295 			reason = element->ReasonCode;
296 			elementType = le16toh(element->ElementFlags) &
297 			    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
298 			/*
299 			 * check for element type of Phys Disk or Hot Spare
300 			 */
301 			if ((elementType !=
302 			    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
303 			    && (elementType !=
304 			    MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
305 				// do next element
306 				goto skip_fp_send;
307 
308 			/*
309 			 * check for reason of Hide, Unhide, PD Created, or PD
310 			 * Deleted
311 			 */
312 			if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
313 			    (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
314 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
315 			    (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
316 				goto skip_fp_send;
317 
318 			// check for a reason of Hide or PD Created
319 			if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
320 			    (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
321 			{
322 				// build RAID Action message
323 				Mpi2RaidActionRequest_t	*action;
324 				Mpi2RaidActionReply_t *reply = NULL;
325 				struct mpr_command *cm;
326 				int error = 0;
327 				if ((cm = mpr_alloc_command(sc)) == NULL) {
328 					printf("%s: command alloc failed\n",
329 					    __func__);
330 					return;
331 				}
332 
333 				mpr_dprint(sc, MPR_EVENT, "Sending FP action "
334 				    "from "
335 				    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
336 				    ":\n");
337 				action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
338 				action->Function = MPI2_FUNCTION_RAID_ACTION;
339 				action->Action =
340 				    MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
341 				action->PhysDiskNum = element->PhysDiskNum;
342 				cm->cm_desc.Default.RequestFlags =
343 				    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
344 				error = mpr_request_polled(sc, &cm);
345 				if (cm != NULL)
346 					reply = (Mpi2RaidActionReply_t *)
347 					    cm->cm_reply;
348 				if (error || (reply == NULL)) {
349 					/* FIXME */
350 					/*
351 					 * If the poll returns error then we
352 					 * need to do diag reset
353 					 */
354 					printf("%s: poll for page completed "
355 					    "with error %d\n", __func__, error);
356 				}
357 				if (reply && (le16toh(reply->IOCStatus) &
358 				    MPI2_IOCSTATUS_MASK) !=
359 				    MPI2_IOCSTATUS_SUCCESS) {
360 					mpr_dprint(sc, MPR_ERROR, "%s: error "
361 					    "sending RaidActionPage; "
362 					    "iocstatus = 0x%x\n", __func__,
363 					    le16toh(reply->IOCStatus));
364 				}
365 
366 				if (cm)
367 					mpr_free_command(sc, cm);
368 			}
369 skip_fp_send:
370 			mpr_dprint(sc, MPR_EVENT, "Received "
371 			    "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
372 			    "code %x:\n", element->ReasonCode);
373 			switch (element->ReasonCode) {
374 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
375 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
376 				if (!foreign_config) {
377 					if (mprsas_volume_add(sc,
378 					    le16toh(element->VolDevHandle))) {
379 						printf("%s: failed to add RAID "
380 						    "volume with handle 0x%x\n",
381 						    __func__, le16toh(element->
382 						    VolDevHandle));
383 					}
384 				}
385 				break;
386 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
387 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
388 				/*
389 				 * Rescan after volume is deleted or removed.
390 				 */
391 				if (!foreign_config) {
392 					if (id == MPR_MAP_BAD_ID) {
393 						printf("%s: could not get ID "
394 						    "for volume with handle "
395 						    "0x%04x\n", __func__,
396 						    le16toh(element->
397 						    VolDevHandle));
398 						break;
399 					}
400 
401 					targ = &sassc->targets[id];
402 					targ->handle = 0x0;
403 					targ->encl_slot = 0x0;
404 					targ->encl_handle = 0x0;
405 					targ->encl_level_valid = 0x0;
406 					targ->encl_level = 0x0;
407 					targ->connector_name[0] = ' ';
408 					targ->connector_name[1] = ' ';
409 					targ->connector_name[2] = ' ';
410 					targ->connector_name[3] = ' ';
411 					targ->exp_dev_handle = 0x0;
412 					targ->phy_num = 0x0;
413 					targ->linkrate = 0x0;
414 					mprsas_rescan_target(sc, targ);
415 					printf("RAID target id 0x%x removed\n",
416 					    targ->tid);
417 				}
418 				break;
419 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
420 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
421 				/*
422 				 * Phys Disk of a volume has been created.  Hide
423 				 * it from the OS.
424 				 */
425 				targ = mprsas_find_target_by_handle(sassc, 0,
426 				    element->PhysDiskDevHandle);
427 				if (targ == NULL)
428 					break;
429 				targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
430 				mprsas_rescan_target(sc, targ);
431 				break;
432 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
433 				/*
434 				 * Phys Disk of a volume has been deleted.
435 				 * Expose it to the OS.
436 				 */
437 				if (mprsas_add_device(sc,
438 				    le16toh(element->PhysDiskDevHandle), 0)) {
439 					printf("%s: failed to add device with "
440 					    "handle 0x%x\n", __func__,
441 					    le16toh(element->
442 					    PhysDiskDevHandle));
443 					mprsas_prepare_remove(sassc,
444 					    le16toh(element->
445 					    PhysDiskDevHandle));
446 				}
447 				break;
448 			}
449 		}
450 		/*
451 		 * refcount was incremented for this event in
452 		 * mprsas_evt_handler.  Decrement it here because the event has
453 		 * been processed.
454 		 */
455 		mprsas_startup_decrement(sassc);
456 		break;
457 	}
458 	case MPI2_EVENT_IR_VOLUME:
459 	{
460 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
461 
462 		/*
463 		 * Informational only.
464 		 */
465 		mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
466 		switch (event_data->ReasonCode) {
467 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
468   			mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
469   			    "changed from 0x%x to 0x%x for Volome with "
470  			    "handle 0x%x", le32toh(event_data->PreviousValue),
471  			    le32toh(event_data->NewValue),
472  			    le16toh(event_data->VolDevHandle));
473 			break;
474 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
475   			mpr_dprint(sc, MPR_EVENT, "   Volume Status "
476   			    "changed from 0x%x to 0x%x for Volome with "
477  			    "handle 0x%x", le32toh(event_data->PreviousValue),
478  			    le32toh(event_data->NewValue),
479  			    le16toh(event_data->VolDevHandle));
480 			break;
481 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
482   			mpr_dprint(sc, MPR_EVENT, "   Volume State "
483   			    "changed from 0x%x to 0x%x for Volome with "
484  			    "handle 0x%x", le32toh(event_data->PreviousValue),
485  			    le32toh(event_data->NewValue),
486  			    le16toh(event_data->VolDevHandle));
487 				u32 state;
488 				struct mprsas_target *targ;
489 				state = le32toh(event_data->NewValue);
490 				switch (state) {
491 				case MPI2_RAID_VOL_STATE_MISSING:
492 				case MPI2_RAID_VOL_STATE_FAILED:
493 					mprsas_prepare_volume_remove(sassc,
494 					    event_data->VolDevHandle);
495 					break;
496 
497 				case MPI2_RAID_VOL_STATE_ONLINE:
498 				case MPI2_RAID_VOL_STATE_DEGRADED:
499 				case MPI2_RAID_VOL_STATE_OPTIMAL:
500 					targ =
501 					    mprsas_find_target_by_handle(sassc,
502 					    0, event_data->VolDevHandle);
503 					if (targ) {
504 						printf("%s %d: Volume handle "
505 						    "0x%x is already added \n",
506 						    __func__, __LINE__,
507 						    event_data->VolDevHandle);
508 						break;
509 					}
510 					if (mprsas_volume_add(sc,
511 					    le16toh(event_data->
512 					    VolDevHandle))) {
513 						printf("%s: failed to add RAID "
514 						    "volume with handle 0x%x\n",
515 						    __func__, le16toh(
516 						    event_data->VolDevHandle));
517 					}
518 					break;
519 				default:
520 					break;
521 				}
522 			break;
523 		default:
524 			break;
525 		}
526 		break;
527 	}
528 	case MPI2_EVENT_IR_PHYSICAL_DISK:
529 	{
530 		Mpi2EventDataIrPhysicalDisk_t *event_data =
531 		    fw_event->event_data;
532 		struct mprsas_target *targ;
533 
534 		/*
535 		 * Informational only.
536 		 */
537 		mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
538 		switch (event_data->ReasonCode) {
539 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
540   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
541   			    "changed from 0x%x to 0x%x for Phys Disk Number "
542   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
543  			    "%d", le32toh(event_data->PreviousValue),
544  			    le32toh(event_data->NewValue),
545 			    event_data->PhysDiskNum,
546  			    le16toh(event_data->PhysDiskDevHandle),
547  			    le16toh(event_data->EnclosureHandle),
548 			    le16toh(event_data->Slot));
549 			break;
550 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
551   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
552   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
553   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
554  			    le32toh(event_data->PreviousValue),
555  			    le32toh(event_data->NewValue),
556 			    event_data->PhysDiskNum,
557  			    le16toh(event_data->PhysDiskDevHandle),
558  			    le16toh(event_data->EnclosureHandle),
559 			    le16toh(event_data->Slot));
560 			break;
561 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
562   			mpr_dprint(sc, MPR_EVENT, "   Phys Disk State changed "
563   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
564   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
565  			    le32toh(event_data->PreviousValue),
566  			    le32toh(event_data->NewValue),
567 			    event_data->PhysDiskNum,
568  			    le16toh(event_data->PhysDiskDevHandle),
569  			    le16toh(event_data->EnclosureHandle),
570 			    le16toh(event_data->Slot));
571 			switch (event_data->NewValue) {
572 				case MPI2_RAID_PD_STATE_ONLINE:
573 				case MPI2_RAID_PD_STATE_DEGRADED:
574 				case MPI2_RAID_PD_STATE_REBUILDING:
575 				case MPI2_RAID_PD_STATE_OPTIMAL:
576 				case MPI2_RAID_PD_STATE_HOT_SPARE:
577 					targ = mprsas_find_target_by_handle(
578 					    sassc, 0,
579 					    event_data->PhysDiskDevHandle);
580 					if (targ) {
581 						targ->flags |=
582 						    MPR_TARGET_FLAGS_RAID_COMPONENT;
583 						printf("%s %d: Found Target "
584 						    "for handle 0x%x.\n",
585 						    __func__, __LINE__ ,
586 						    event_data->
587 						    PhysDiskDevHandle);
588 					}
589 				break;
590 				case MPI2_RAID_PD_STATE_OFFLINE:
591 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
592 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
593 				default:
594 					targ = mprsas_find_target_by_handle(
595 					    sassc, 0,
596 					    event_data->PhysDiskDevHandle);
597 					if (targ) {
598 						targ->flags |=
599 					    ~MPR_TARGET_FLAGS_RAID_COMPONENT;
600 						printf("%s %d: Found Target "
601 						    "for handle 0x%x.  \n",
602 						    __func__, __LINE__ ,
603 						    event_data->
604 						    PhysDiskDevHandle);
605 					}
606 				break;
607 			}
608 		default:
609 			break;
610 		}
611 		break;
612 	}
613 	case MPI2_EVENT_IR_OPERATION_STATUS:
614 	{
615 		Mpi2EventDataIrOperationStatus_t *event_data =
616 		    fw_event->event_data;
617 
618 		/*
619 		 * Informational only.
620 		 */
621 		mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
622 		mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
623 		    "percent complete for Volume with handle 0x%x",
624 		    event_data->RAIDOperation, event_data->PercentComplete,
625 		    le16toh(event_data->VolDevHandle));
626 		break;
627 	}
628 	case MPI2_EVENT_TEMP_THRESHOLD:
629 	{
630 		pMpi2EventDataTemperature_t	temp_event;
631 
632 		temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
633 
634 		/*
635 		 * The Temp Sensor Count must be greater than the event's Sensor
636 		 * Num to be valid.  If valid, print the temp thresholds that
637 		 * have been exceeded.
638 		 */
639 		if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
640 			mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
641 			    "%s %s %s %s exceeded for Sensor: %d !!!\n",
642 			    ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
643 			    ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
644 			    ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
645 			    ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
646 			    temp_event->SensorNum);
647 			mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
648 			    "%d\n", temp_event->CurrentTemperature);
649 		}
650 		break;
651 	}
652 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
653 	{
654 		pMpi26EventDataActiveCableExcept_t	ace_event_data;
655 		ace_event_data =
656 		    (pMpi26EventDataActiveCableExcept_t)fw_event->event_data;
657 
658 		switch(ace_event_data->ReasonCode) {
659 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
660 		{
661 			mpr_printf(sc, "Currently a cable with "
662 			    "ReceptacleID %d cannot be powered and device "
663 			    "connected to this active cable will not be seen. "
664 			    "This active cable requires %d mW of power.\n",
665 			    ace_event_data->ReceptacleID,
666 			    ace_event_data->ActiveCablePowerRequirement);
667 			break;
668 		}
669 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
670 		{
671 			mpr_printf(sc, "Currently a cable with "
672 			    "ReceptacleID %d is not running at optimal speed "
673 			    "(12 Gb/s rate)\n", ace_event_data->ReceptacleID);
674 			break;
675 		}
676 		default:
677 			break;
678 		}
679 		break;
680 	}
681 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
682 	{
683 		pMpi26EventDataPCIeDeviceStatusChange_t	pcie_status_event_data;
684 		pcie_status_event_data =
685 		   (pMpi26EventDataPCIeDeviceStatusChange_t)fw_event->event_data;
686 
687 		switch (pcie_status_event_data->ReasonCode) {
688 		case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
689 		{
690 			mpr_printf(sc, "PCIe Host Reset failed on DevHandle "
691 			    "0x%x\n", pcie_status_event_data->DevHandle);
692 			break;
693 		}
694 		default:
695 			break;
696 		}
697 		break;
698 	}
699 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
700 	{
701 		pMpi25EventDataSasDeviceDiscoveryError_t discovery_error_data;
702 		uint64_t sas_address;
703 
704 		discovery_error_data =
705 		    (pMpi25EventDataSasDeviceDiscoveryError_t)
706 		    fw_event->event_data;
707 
708 		sas_address = discovery_error_data->SASAddress.High;
709 		sas_address = (sas_address << 32) |
710 		    discovery_error_data->SASAddress.Low;
711 
712 		switch(discovery_error_data->ReasonCode) {
713 		case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
714 		{
715 			mpr_printf(sc, "SMP command failed during discovery "
716 			    "for expander with SAS Address %jx and "
717 			    "handle 0x%x.\n", sas_address,
718 			    discovery_error_data->DevHandle);
719 			break;
720 		}
721 		case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
722 		{
723 			mpr_printf(sc, "SMP command timed out during "
724 			    "discovery for expander with SAS Address %jx and "
725 			    "handle 0x%x.\n", sas_address,
726 			    discovery_error_data->DevHandle);
727 			break;
728 		}
729 		default:
730 			break;
731 		}
732 		break;
733 	}
734 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
735 	{
736 		MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *data;
737 		MPI26_EVENT_PCIE_TOPO_PORT_ENTRY *port_entry;
738 		uint8_t i, link_rate;
739 		uint16_t handle;
740 
741 		data = (MPI26_EVENT_DATA_PCIE_TOPOLOGY_CHANGE_LIST *)
742 		    fw_event->event_data;
743 
744 		mpr_mapping_pcie_topology_change_event(sc,
745 		    fw_event->event_data);
746 
747 		for (i = 0; i < data->NumEntries; i++) {
748 			port_entry = &data->PortEntry[i];
749 			handle = le16toh(port_entry->AttachedDevHandle);
750 			link_rate = port_entry->CurrentPortInfo &
751 			    MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
752 			switch (port_entry->PortStatus) {
753 			case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
754 				if (link_rate <
755 				    MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5) {
756 					mpr_dprint(sc, MPR_ERROR, "%s: Cannot "
757 					    "add PCIe device with handle 0x%x "
758 					    "with unknown link rate.\n",
759 					    __func__, handle);
760 					break;
761 				}
762 				if (mprsas_add_pcie_device(sc, handle,
763 				    link_rate)) {
764 					mpr_dprint(sc, MPR_ERROR, "%s: failed "
765 					    "to add PCIe device with handle "
766 					    "0x%x\n", __func__, handle);
767 					mprsas_prepare_remove(sassc, handle);
768 				}
769 				break;
770 			case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
771 				mprsas_prepare_remove(sassc, handle);
772 				break;
773 			case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
774 			case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
775 			case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
776 			default:
777 				break;
778 			}
779 		}
780 		/*
781 		 * refcount was incremented for this event in
782 		 * mprsas_evt_handler.  Decrement it here because the event has
783 		 * been processed.
784 		 */
785 		mprsas_startup_decrement(sassc);
786 		break;
787 	}
788 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
789 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
790 	default:
791 		mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
792 		    fw_event->event);
793 		break;
794 	}
795 	mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
796 	    __func__, fw_event->event);
797 	mprsas_fw_event_free(sc, fw_event);
798 }
799 
800 void
mprsas_firmware_event_work(void * arg,int pending)801 mprsas_firmware_event_work(void *arg, int pending)
802 {
803 	struct mpr_fw_event_work *fw_event;
804 	struct mpr_softc *sc;
805 
806 	sc = (struct mpr_softc *)arg;
807 	mpr_lock(sc);
808 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
809 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
810 		mprsas_fw_work(sc, fw_event);
811 	}
812 	mpr_unlock(sc);
813 }
814 
815 static int
mprsas_add_device(struct mpr_softc * sc,u16 handle,u8 linkrate)816 mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
817 {
818 	char devstring[80];
819 	struct mprsas_softc *sassc;
820 	struct mprsas_target *targ;
821 	Mpi2ConfigReply_t mpi_reply;
822 	Mpi2SasDevicePage0_t config_page;
823 	uint64_t sas_address, parent_sas_address = 0;
824 	u32 device_info, parent_devinfo = 0;
825 	unsigned int id;
826 	int ret = 1, error = 0, i;
827 	struct mprsas_lun *lun;
828 	u8 is_SATA_SSD = 0;
829 	struct mpr_command *cm;
830 
831 	sassc = sc->sassc;
832 	mprsas_startup_increment(sassc);
833 	if (mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
834 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
835 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
836 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
837 		    handle, mpi_reply.IOCStatus);
838 		error = ENXIO;
839 		goto out;
840 	}
841 
842 	device_info = le32toh(config_page.DeviceInfo);
843 
844 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
845 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
846 		Mpi2ConfigReply_t tmp_mpi_reply;
847 		Mpi2SasDevicePage0_t parent_config_page;
848 
849 		if (mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
850 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
851 		    le16toh(config_page.ParentDevHandle)) != 0) {
852 			mpr_dprint(sc, MPR_MAPPING|MPR_FAULT,
853 			    "Error reading parent SAS device %#x page0, "
854 			    "iocstatus= 0x%x\n",
855 			    le16toh(config_page.ParentDevHandle),
856 			    tmp_mpi_reply.IOCStatus);
857 		} else {
858 			parent_sas_address = parent_config_page.SASAddress.High;
859 			parent_sas_address = (parent_sas_address << 32) |
860 			    parent_config_page.SASAddress.Low;
861 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
862 		}
863 	}
864 	sas_address = htole32(config_page.SASAddress.High);
865 	sas_address = (sas_address << 32) | htole32(config_page.SASAddress.Low);
866 	mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address from SAS device "
867 	    "page0 = %jx\n", handle, sas_address);
868 
869 	/*
870 	 * Always get SATA Identify information because this is used to
871 	 * determine if Start/Stop Unit should be sent to the drive when the
872 	 * system is shutdown.
873 	 */
874 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
875 		ret = mprsas_get_sas_address_for_sata_disk(sc, &sas_address,
876 		    handle, device_info, &is_SATA_SSD);
877 		if (ret) {
878 			mpr_dprint(sc, MPR_MAPPING|MPR_ERROR,
879 			    "%s: failed to get disk type (SSD or HDD) for SATA "
880 			    "device with handle 0x%04x\n",
881 			    __func__, handle);
882 		} else {
883 			mpr_dprint(sc, MPR_MAPPING, "Handle 0x%04x SAS Address "
884 			    "from SATA device = %jx\n", handle, sas_address);
885 		}
886 	}
887 
888 	/*
889 	 * use_phynum:
890 	 *  1 - use the PhyNum field as a fallback to the mapping logic
891 	 *  0 - never use the PhyNum field
892 	 * -1 - only use the PhyNum field
893 	 *
894 	 * Note that using the Phy number to map a device can cause device adds
895 	 * to fail if multiple enclosures/expanders are in the topology. For
896 	 * example, if two devices are in the same slot number in two different
897 	 * enclosures within the topology, only one of those devices will be
898 	 * added. PhyNum mapping should not be used if multiple enclosures are
899 	 * in the topology.
900 	 */
901 	id = MPR_MAP_BAD_ID;
902 	if (sc->use_phynum != -1)
903 		id = mpr_mapping_get_tid(sc, sas_address, handle);
904 	if (id == MPR_MAP_BAD_ID) {
905 		if ((sc->use_phynum == 0) ||
906 		    ((id = config_page.PhyNum) > sassc->maxtargets)) {
907 			mpr_dprint(sc, MPR_INFO, "failure at %s:%d/%s()! "
908 			    "Could not get ID for device with handle 0x%04x\n",
909 			    __FILE__, __LINE__, __func__, handle);
910 			error = ENXIO;
911 			goto out;
912 		}
913 	}
914 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
915 	    __func__, id);
916 
917 	/*
918 	 * Only do the ID check and reuse check if the target is not from a
919 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
920 	 * when a volume is deleted because the mapping entry for the PD will
921 	 * still be in the mapping table. The ID check should not be done here
922 	 * either since this PD is already being used.
923 	 */
924 	targ = &sassc->targets[id];
925 	if (!(targ->flags & MPR_TARGET_FLAGS_RAID_COMPONENT)) {
926 		if (mprsas_check_id(sassc, id) != 0) {
927 			mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
928 			    "Excluding target id %d\n", id);
929 			error = ENXIO;
930 			goto out;
931 		}
932 
933 		if (targ->handle != 0x0) {
934 			mpr_dprint(sc, MPR_MAPPING, "Attempting to reuse "
935 			    "target id %d handle 0x%04x\n", id, targ->handle);
936 			error = ENXIO;
937 			goto out;
938 		}
939 	}
940 
941 	targ->devinfo = device_info;
942 	targ->devname = le32toh(config_page.DeviceName.High);
943 	targ->devname = (targ->devname << 32) |
944 	    le32toh(config_page.DeviceName.Low);
945 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
946 	targ->encl_slot = le16toh(config_page.Slot);
947 	targ->encl_level = config_page.EnclosureLevel;
948 	targ->connector_name[0] = config_page.ConnectorName[0];
949 	targ->connector_name[1] = config_page.ConnectorName[1];
950 	targ->connector_name[2] = config_page.ConnectorName[2];
951 	targ->connector_name[3] = config_page.ConnectorName[3];
952 	targ->handle = handle;
953 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
954 	targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
955 	targ->parent_sasaddr = le64toh(parent_sas_address);
956 	targ->parent_devinfo = parent_devinfo;
957 	targ->tid = id;
958 	targ->linkrate = (linkrate>>4);
959 	targ->flags = 0;
960 	if (is_SATA_SSD) {
961 		targ->flags = MPR_TARGET_IS_SATA_SSD;
962 	}
963 	if ((le16toh(config_page.Flags) &
964 	    MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
965 	    (le16toh(config_page.Flags) &
966 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE)) {
967 		targ->scsi_req_desc_type =
968 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
969 	}
970 	if (le16toh(config_page.Flags) &
971 	    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
972 		targ->encl_level_valid = TRUE;
973 	}
974 	TAILQ_INIT(&targ->commands);
975 	TAILQ_INIT(&targ->timedout_commands);
976 	while (!SLIST_EMPTY(&targ->luns)) {
977 		lun = SLIST_FIRST(&targ->luns);
978 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
979 		free(lun, M_MPR);
980 	}
981 	SLIST_INIT(&targ->luns);
982 
983 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
984 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found device <%s> <%s> "
985 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
986 	    mpr_describe_table(mpr_linkrate_names, targ->linkrate),
987 	    targ->handle, targ->encl_handle, targ->encl_slot);
988 	if (targ->encl_level_valid) {
989 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
990 		    "and connector name (%4s)\n", targ->encl_level,
991 		    targ->connector_name);
992 	}
993 	mprsas_rescan_target(sc, targ);
994 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
995 
996 	/*
997 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
998 	 * If so, send a Target Reset TM to the target that was just created.
999 	 * An Abort Task TM should be used instead of a Target Reset, but that
1000 	 * would be much more difficult because targets have not been fully
1001 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
1002 	 * target instead of the LUN.  The commands should complete once
1003 	 * the target has been reset.
1004 	 */
1005 	for (i = 1; i < sc->num_reqs; i++) {
1006 		cm = &sc->commands[i];
1007 		if (cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) {
1008 			targ->timeouts++;
1009 			cm->cm_flags |= MPR_CM_FLAGS_TIMEDOUT;
1010 
1011 			if ((targ->tm = mprsas_alloc_tm(sc)) != NULL) {
1012 				mpr_dprint(sc, MPR_INFO, "%s: sending Target "
1013 				    "Reset for stuck SATA identify command "
1014 				    "(cm = %p)\n", __func__, cm);
1015 				targ->tm->cm_targ = targ;
1016 				mprsas_send_reset(sc, targ->tm,
1017 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1018 			} else {
1019 				mpr_dprint(sc, MPR_ERROR, "Failed to allocate "
1020 				    "tm for Target Reset after SATA ID command "
1021 				    "timed out (cm %p)\n", cm);
1022 			}
1023 			/*
1024 			 * No need to check for more since the target is
1025 			 * already being reset.
1026 			 */
1027 			break;
1028 		}
1029 	}
1030 out:
1031 	mprsas_startup_decrement(sassc);
1032 	return (error);
1033 }
1034 
1035 int
mprsas_get_sas_address_for_sata_disk(struct mpr_softc * sc,u64 * sas_address,u16 handle,u32 device_info,u8 * is_SATA_SSD)1036 mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
1037     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
1038 {
1039 	Mpi2SataPassthroughReply_t mpi_reply;
1040 	int i, rc, try_count;
1041 	u32 *bufferptr;
1042 	union _sata_sas_address hash_address;
1043 	struct _ata_identify_device_data ata_identify;
1044 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
1045 	u32 ioc_status;
1046 	u8 sas_status;
1047 
1048 	memset(&ata_identify, 0, sizeof(ata_identify));
1049 	memset(&mpi_reply, 0, sizeof(mpi_reply));
1050 	try_count = 0;
1051 	do {
1052 		rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
1053 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
1054 		try_count++;
1055 		ioc_status = le16toh(mpi_reply.IOCStatus)
1056 		    & MPI2_IOCSTATUS_MASK;
1057 		sas_status = mpi_reply.SASStatus;
1058 		switch (ioc_status) {
1059 		case MPI2_IOCSTATUS_SUCCESS:
1060 			break;
1061 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1062 			/* No sense sleeping.  this error won't get better */
1063 			break;
1064 		default:
1065 			if (sc->spinup_wait_time > 0) {
1066 				mpr_dprint(sc, MPR_INFO, "Sleeping %d seconds "
1067 				    "after SATA ID error to wait for spinup\n",
1068 				    sc->spinup_wait_time);
1069 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
1070 				    "mprid", sc->spinup_wait_time * hz);
1071 			}
1072 		}
1073 	} while (((rc && (rc != EWOULDBLOCK)) ||
1074 	    (ioc_status && (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
1075 	    || sas_status) && (try_count < 5));
1076 
1077 	if (rc == 0 && !ioc_status && !sas_status) {
1078 		mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
1079 		    "successfully for handle = 0x%x with try_count = %d\n",
1080 		    __func__, handle, try_count);
1081 	} else {
1082 		mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
1083 		    __func__, handle);
1084 		return -1;
1085 	}
1086 	/* Copy & byteswap the 40 byte model number to a buffer */
1087 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
1088 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
1089 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
1090 	}
1091 	/* Copy & byteswap the 20 byte serial number to a buffer */
1092 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
1093 		buffer[MPT2SAS_MN_LEN + i] =
1094 		    ((u8 *)ata_identify.serial_number)[i + 1];
1095 		buffer[MPT2SAS_MN_LEN + i + 1] =
1096 		    ((u8 *)ata_identify.serial_number)[i];
1097 	}
1098 	bufferptr = (u32 *)buffer;
1099 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
1100 	 * so loop through the first 56 bytes (7*8),
1101 	 * and then add in the last dword.
1102 	 */
1103 	hash_address.word.low  = 0;
1104 	hash_address.word.high = 0;
1105 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
1106 		hash_address.word.low += *bufferptr;
1107 		bufferptr++;
1108 		hash_address.word.high += *bufferptr;
1109 		bufferptr++;
1110 	}
1111 	/* Add the last dword */
1112 	hash_address.word.low += *bufferptr;
1113 	/* Make sure the hash doesn't start with 5, because it could clash
1114 	 * with a SAS address. Change 5 to a D.
1115 	 */
1116 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
1117 		hash_address.word.high |= 0x00000080;
1118 	*sas_address = (u64)hash_address.wwid[0] << 56 |
1119 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
1120 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
1121 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
1122 	    (u64)hash_address.wwid[7];
1123 	if (ata_identify.rotational_speed == 1) {
1124 		*is_SATA_SSD = 1;
1125 	}
1126 
1127 	return 0;
1128 }
1129 
1130 static int
mprsas_get_sata_identify(struct mpr_softc * sc,u16 handle,Mpi2SataPassthroughReply_t * mpi_reply,char * id_buffer,int sz,u32 devinfo)1131 mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
1132     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
1133 {
1134 	Mpi2SataPassthroughRequest_t *mpi_request;
1135 	Mpi2SataPassthroughReply_t *reply;
1136 	struct mpr_command *cm;
1137 	char *buffer;
1138 	int error = 0;
1139 
1140 	buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
1141 	if (!buffer)
1142 		return ENOMEM;
1143 
1144 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1145 		free(buffer, M_MPR);
1146 		return (EBUSY);
1147 	}
1148 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
1149 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
1150 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
1151 	mpi_request->VF_ID = 0;
1152 	mpi_request->DevHandle = htole16(handle);
1153 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
1154 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
1155 	mpi_request->DataLength = htole32(sz);
1156 	mpi_request->CommandFIS[0] = 0x27;
1157 	mpi_request->CommandFIS[1] = 0x80;
1158 	mpi_request->CommandFIS[2] =  (devinfo &
1159 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
1160 	cm->cm_sge = &mpi_request->SGL;
1161 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
1162 	cm->cm_flags = MPR_CM_FLAGS_DATAIN;
1163 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1164 	cm->cm_data = buffer;
1165 	cm->cm_length = htole32(sz);
1166 
1167 	/*
1168 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
1169 	 * This fixes a problem where the FW does not send a reply sometimes
1170 	 * when a bad disk is in the topology. So, this is used to timeout the
1171 	 * command so that processing can continue normally.
1172 	 */
1173 	cm->cm_timeout_handler = mprsas_ata_id_timeout;
1174 
1175 	error = mpr_wait_command(sc, &cm, MPR_ATA_ID_TIMEOUT, CAN_SLEEP);
1176 
1177 	/* mprsas_ata_id_timeout does not reset controller */
1178 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
1179 
1180 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
1181 	if (error || (reply == NULL)) {
1182 		/* FIXME */
1183 		/*
1184 		 * If the request returns an error then we need to do a diag
1185 		 * reset
1186 		 */
1187 		mpr_dprint(sc, MPR_INFO|MPR_FAULT|MPR_MAPPING,
1188 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
1189 		    error);
1190 		error = ENXIO;
1191 		goto out;
1192 	}
1193 	bcopy(buffer, id_buffer, sz);
1194 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
1195 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1196 	    MPI2_IOCSTATUS_SUCCESS) {
1197 		mpr_dprint(sc, MPR_INFO|MPR_MAPPING|MPR_FAULT,
1198 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
1199 		    handle, reply->IOCStatus);
1200 		error = ENXIO;
1201 		goto out;
1202 	}
1203 out:
1204 	/*
1205 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
1206 	 * it.  The command and buffer will be freed after we send a Target
1207 	 * Reset TM and the command comes back from the controller.
1208 	 */
1209 	if ((cm->cm_flags & MPR_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
1210 		mpr_free_command(sc, cm);
1211 		free(buffer, M_MPR);
1212 	}
1213 	return (error);
1214 }
1215 
1216 /*
1217  * This is completion handler to make sure that commands and allocated
1218  * buffers get freed when timed out SATA ID commands finally complete after
1219  * we've reset the target.  In the normal case, we wait for the command to
1220  * complete.
1221  */
1222 static void
mprsas_ata_id_complete(struct mpr_softc * sc,struct mpr_command * cm)1223 mprsas_ata_id_complete(struct mpr_softc *sc, struct mpr_command *cm)
1224 {
1225 	mpr_dprint(sc, MPR_INFO, "%s ATA ID completed late cm %p sc %p\n",
1226 	    __func__, cm, sc);
1227 
1228 	free(cm->cm_data, M_MPR);
1229 	mpr_free_command(sc, cm);
1230 }
1231 
1232 static void
mprsas_ata_id_timeout(struct mpr_softc * sc,struct mpr_command * cm)1233 mprsas_ata_id_timeout(struct mpr_softc *sc, struct mpr_command *cm)
1234 {
1235 
1236 	mpr_dprint(sc, MPR_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1237 	    __func__, cm, sc);
1238 
1239 	/*
1240 	 * The Abort Task cannot be sent from here because the driver has not
1241 	 * completed setting up targets.  Instead, the command is flagged so
1242 	 * that special handling will be used to send the abort. Now that
1243 	 * this command has timed out, it's no longer in the queue.
1244 	 */
1245 	cm->cm_flags |= MPR_CM_FLAGS_SATA_ID_TIMEOUT;
1246 
1247 	/*
1248 	 * Since we will no longer be waiting for the command to complete,
1249 	 * set a completion handler to make sure we free all resources.
1250 	 */
1251 	cm->cm_complete = mprsas_ata_id_complete;
1252 }
1253 
1254 static int
mprsas_add_pcie_device(struct mpr_softc * sc,u16 handle,u8 linkrate)1255 mprsas_add_pcie_device(struct mpr_softc *sc, u16 handle, u8 linkrate)
1256 {
1257 	char devstring[80];
1258 	struct mprsas_softc *sassc;
1259 	struct mprsas_target *targ;
1260 	Mpi2ConfigReply_t mpi_reply;
1261 	Mpi26PCIeDevicePage0_t config_page;
1262 	Mpi26PCIeDevicePage2_t config_page2;
1263 	uint64_t pcie_wwid, parent_wwid = 0;
1264 	u32 device_info, parent_devinfo = 0;
1265 	unsigned int id;
1266 	int error = 0;
1267 	struct mprsas_lun *lun;
1268 
1269 	sassc = sc->sassc;
1270 	mprsas_startup_increment(sassc);
1271 	if ((mpr_config_get_pcie_device_pg0(sc, &mpi_reply, &config_page,
1272 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
1273 		printf("%s: error reading PCIe device page0\n", __func__);
1274 		error = ENXIO;
1275 		goto out;
1276 	}
1277 
1278 	device_info = le32toh(config_page.DeviceInfo);
1279 
1280 	if (((device_info & MPI26_PCIE_DEVINFO_PCI_SWITCH) == 0)
1281 	    && (le16toh(config_page.ParentDevHandle) != 0)) {
1282 		Mpi2ConfigReply_t tmp_mpi_reply;
1283 		Mpi26PCIeDevicePage0_t parent_config_page;
1284 
1285 		if ((mpr_config_get_pcie_device_pg0(sc, &tmp_mpi_reply,
1286 		     &parent_config_page, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE,
1287 		     le16toh(config_page.ParentDevHandle)))) {
1288 			printf("%s: error reading PCIe device %#x page0\n",
1289 			    __func__, le16toh(config_page.ParentDevHandle));
1290 		} else {
1291 			parent_wwid = parent_config_page.WWID.High;
1292 			parent_wwid = (parent_wwid << 32) |
1293 			    parent_config_page.WWID.Low;
1294 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
1295 		}
1296 	}
1297 	/* TODO Check proper endianness */
1298 	pcie_wwid = config_page.WWID.High;
1299 	pcie_wwid = (pcie_wwid << 32) | config_page.WWID.Low;
1300 	mpr_dprint(sc, MPR_INFO, "PCIe WWID from PCIe device page0 = %jx\n",
1301 	    pcie_wwid);
1302 
1303 	if ((mpr_config_get_pcie_device_pg2(sc, &mpi_reply, &config_page2,
1304 	     MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
1305 		printf("%s: error reading PCIe device page2\n", __func__);
1306 		error = ENXIO;
1307 		goto out;
1308 	}
1309 
1310 	id = mpr_mapping_get_tid(sc, pcie_wwid, handle);
1311 	if (id == MPR_MAP_BAD_ID) {
1312 		mpr_dprint(sc, MPR_ERROR | MPR_INFO, "failure at %s:%d/%s()! "
1313 		    "Could not get ID for device with handle 0x%04x\n",
1314 		    __FILE__, __LINE__, __func__, handle);
1315 		error = ENXIO;
1316 		goto out;
1317 	}
1318 	mpr_dprint(sc, MPR_MAPPING, "%s: Target ID for added device is %d.\n",
1319 	    __func__, id);
1320 
1321 	if (mprsas_check_id(sassc, id) != 0) {
1322 		mpr_dprint(sc, MPR_MAPPING|MPR_INFO,
1323 		    "Excluding target id %d\n", id);
1324 		error = ENXIO;
1325 		goto out;
1326 	}
1327 
1328 	mpr_dprint(sc, MPR_MAPPING, "WWID from PCIe device page0 = %jx\n",
1329 	    pcie_wwid);
1330 	targ = &sassc->targets[id];
1331 	targ->devinfo = device_info;
1332 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
1333 	targ->encl_slot = le16toh(config_page.Slot);
1334 	targ->encl_level = config_page.EnclosureLevel;
1335 	targ->connector_name[0] = ((char *)&config_page.ConnectorName)[0];
1336 	targ->connector_name[1] = ((char *)&config_page.ConnectorName)[1];
1337 	targ->connector_name[2] = ((char *)&config_page.ConnectorName)[2];
1338 	targ->connector_name[3] = ((char *)&config_page.ConnectorName)[3];
1339 	targ->is_nvme = device_info & MPI26_PCIE_DEVINFO_NVME;
1340 	targ->MDTS = config_page2.MaximumDataTransferSize;
1341 	if (targ->is_nvme)
1342 		targ->controller_reset_timeout = config_page2.ControllerResetTO;
1343 	/*
1344 	 * Assume always TRUE for encl_level_valid because there is no valid
1345 	 * flag for PCIe.
1346 	 */
1347 	targ->encl_level_valid = TRUE;
1348 	targ->handle = handle;
1349 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
1350 	targ->sasaddr = mpr_to_u64(&config_page.WWID);
1351 	targ->parent_sasaddr = le64toh(parent_wwid);
1352 	targ->parent_devinfo = parent_devinfo;
1353 	targ->tid = id;
1354 	targ->linkrate = linkrate;
1355 	targ->flags = 0;
1356 	if ((le16toh(config_page.Flags) &
1357 	    MPI26_PCIEDEV0_FLAGS_ENABLED_FAST_PATH) &&
1358 	    (le16toh(config_page.Flags) &
1359 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE)) {
1360 		targ->scsi_req_desc_type =
1361 		    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
1362 	}
1363 	TAILQ_INIT(&targ->commands);
1364 	TAILQ_INIT(&targ->timedout_commands);
1365 	while (!SLIST_EMPTY(&targ->luns)) {
1366 		lun = SLIST_FIRST(&targ->luns);
1367 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1368 		free(lun, M_MPR);
1369 	}
1370 	SLIST_INIT(&targ->luns);
1371 
1372 	mpr_describe_devinfo(targ->devinfo, devstring, 80);
1373 	mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "Found PCIe device <%s> <%s> "
1374 	    "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
1375 	    mpr_describe_table(mpr_pcie_linkrate_names, targ->linkrate),
1376 	    targ->handle, targ->encl_handle, targ->encl_slot);
1377 	if (targ->encl_level_valid) {
1378 		mpr_dprint(sc, (MPR_INFO|MPR_MAPPING), "At enclosure level %d "
1379 		    "and connector name (%4s)\n", targ->encl_level,
1380 		    targ->connector_name);
1381 	}
1382 	mprsas_rescan_target(sc, targ);
1383 	mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
1384 
1385 out:
1386 	mprsas_startup_decrement(sassc);
1387 	return (error);
1388 }
1389 
1390 static int
mprsas_volume_add(struct mpr_softc * sc,u16 handle)1391 mprsas_volume_add(struct mpr_softc *sc, u16 handle)
1392 {
1393 	struct mprsas_softc *sassc;
1394 	struct mprsas_target *targ;
1395 	u64 wwid;
1396 	unsigned int id;
1397 	int error = 0;
1398 	struct mprsas_lun *lun;
1399 
1400 	sassc = sc->sassc;
1401 	mprsas_startup_increment(sassc);
1402 	/* wwid is endian safe */
1403 	mpr_config_get_volume_wwid(sc, handle, &wwid);
1404 	if (!wwid) {
1405 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1406 		    __func__);
1407 		error = ENXIO;
1408 		goto out;
1409 	}
1410 
1411 	id = mpr_mapping_get_raid_tid(sc, wwid, handle);
1412 	if (id == MPR_MAP_BAD_ID) {
1413 		printf("%s: could not get ID for volume with handle 0x%04x and "
1414 		    "WWID 0x%016llx\n", __func__, handle,
1415 		    (unsigned long long)wwid);
1416 		error = ENXIO;
1417 		goto out;
1418 	}
1419 
1420 	targ = &sassc->targets[id];
1421 	targ->tid = id;
1422 	targ->handle = handle;
1423 	targ->devname = wwid;
1424 	targ->flags = MPR_TARGET_FLAGS_VOLUME;
1425 	TAILQ_INIT(&targ->commands);
1426 	TAILQ_INIT(&targ->timedout_commands);
1427 	while (!SLIST_EMPTY(&targ->luns)) {
1428 		lun = SLIST_FIRST(&targ->luns);
1429 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1430 		free(lun, M_MPR);
1431 	}
1432 	SLIST_INIT(&targ->luns);
1433 	mprsas_rescan_target(sc, targ);
1434 	mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1435 	    targ->tid, wwid);
1436 out:
1437 	mprsas_startup_decrement(sassc);
1438 	return (error);
1439 }
1440 
1441 /**
1442  * mprsas_SSU_to_SATA_devices
1443  * @sc: per adapter object
1444  *
1445  * Looks through the target list and issues a StartStopUnit SCSI command to each
1446  * SATA direct-access device.  This helps to ensure that data corruption is
1447  * avoided when the system is being shut down.  This must be called after the IR
1448  * System Shutdown RAID Action is sent if in IR mode.
1449  *
1450  * Return nothing.
1451  */
1452 static void
mprsas_SSU_to_SATA_devices(struct mpr_softc * sc,int howto)1453 mprsas_SSU_to_SATA_devices(struct mpr_softc *sc, int howto)
1454 {
1455 	struct mprsas_softc *sassc = sc->sassc;
1456 	union ccb *ccb;
1457 	path_id_t pathid = cam_sim_path(sassc->sim);
1458 	target_id_t targetid;
1459 	struct mprsas_target *target;
1460 	char path_str[64];
1461 	int timeout;
1462 
1463 	mpr_lock(sc);
1464 
1465 	/*
1466 	 * For each target, issue a StartStopUnit command to stop the device.
1467 	 */
1468 	sc->SSU_started = TRUE;
1469 	sc->SSU_refcount = 0;
1470 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1471 		target = &sassc->targets[targetid];
1472 		if (target->handle == 0x0) {
1473 			continue;
1474 		}
1475 
1476 		/*
1477 		 * The stop_at_shutdown flag will be set if this device is
1478 		 * a SATA direct-access end device.
1479 		 */
1480 		if (target->stop_at_shutdown) {
1481 			ccb = xpt_alloc_ccb_nowait();
1482 			if (ccb == NULL) {
1483 				mpr_dprint(sc, MPR_FAULT, "Unable to alloc CCB "
1484 				    "to stop unit.\n");
1485 				return;
1486 			}
1487 
1488 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1489 			    pathid, targetid, CAM_LUN_WILDCARD) !=
1490 			    CAM_REQ_CMP) {
1491 				mpr_dprint(sc, MPR_ERROR, "Unable to create "
1492 				    "path to stop unit.\n");
1493 				xpt_free_ccb(ccb);
1494 				return;
1495 			}
1496 			xpt_path_string(ccb->ccb_h.path, path_str,
1497 			    sizeof(path_str));
1498 
1499 			mpr_dprint(sc, MPR_INFO, "Sending StopUnit: path %s "
1500 			    "handle %d\n", path_str, target->handle);
1501 
1502 			/*
1503 			 * Issue a START STOP UNIT command for the target.
1504 			 * Increment the SSU counter to be used to count the
1505 			 * number of required replies.
1506 			 */
1507 			mpr_dprint(sc, MPR_INFO, "Incrementing SSU count\n");
1508 			sc->SSU_refcount++;
1509 			ccb->ccb_h.target_id =
1510 			    xpt_path_target_id(ccb->ccb_h.path);
1511 			ccb->ccb_h.ppriv_ptr1 = sassc;
1512 			scsi_start_stop(&ccb->csio,
1513 			    /*retries*/0,
1514 			    mprsas_stop_unit_done,
1515 			    MSG_SIMPLE_Q_TAG,
1516 			    /*start*/FALSE,
1517 			    /*load/eject*/0,
1518 			    /*immediate*/FALSE,
1519 			    MPR_SENSE_LEN,
1520 			    /*timeout*/10000);
1521 			xpt_action(ccb);
1522 		}
1523 	}
1524 
1525 	mpr_unlock(sc);
1526 
1527 	/*
1528 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1529 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1530 	 */
1531 	timeout = 600;
1532 	if (howto & RB_NOSYNC)
1533 		timeout = 100;
1534 
1535 	/*
1536 	 * Wait until all of the SSU commands have completed or time
1537 	 * has expired. Pause for 100ms each time through.  If any
1538 	 * command times out, the target will be reset in the SCSI
1539 	 * command timeout routine.
1540 	 */
1541 	while (sc->SSU_refcount > 0) {
1542 		pause("mprwait", hz/10);
1543 		if (SCHEDULER_STOPPED())
1544 			xpt_sim_poll(sassc->sim);
1545 
1546 		if (--timeout == 0) {
1547 			mpr_dprint(sc, MPR_ERROR, "Time has expired waiting "
1548 			    "for SSU commands to complete.\n");
1549 			break;
1550 		}
1551 	}
1552 }
1553 
1554 static void
mprsas_stop_unit_done(struct cam_periph * periph,union ccb * done_ccb)1555 mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1556 {
1557 	struct mprsas_softc *sassc;
1558 	char path_str[64];
1559 
1560 	if (done_ccb == NULL)
1561 		return;
1562 
1563 	sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1564 
1565 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1566 	mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1567 	    path_str);
1568 
1569 	/*
1570 	 * Nothing more to do except free the CCB and path.  If the command
1571 	 * timed out, an abort reset, then target reset will be issued during
1572 	 * the SCSI Command process.
1573 	 */
1574 	xpt_free_path(done_ccb->ccb_h.path);
1575 	xpt_free_ccb(done_ccb);
1576 }
1577 
1578 /**
1579  * mprsas_ir_shutdown - IR shutdown notification
1580  * @sc: per adapter object
1581  *
1582  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1583  * the host system is shutting down.
1584  *
1585  * Return nothing.
1586  */
1587 void
mprsas_ir_shutdown(struct mpr_softc * sc,int howto)1588 mprsas_ir_shutdown(struct mpr_softc *sc, int howto)
1589 {
1590 	u16 volume_mapping_flags;
1591 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1592 	struct dev_mapping_table *mt_entry;
1593 	u32 start_idx, end_idx;
1594 	unsigned int id, found_volume = 0;
1595 	struct mpr_command *cm;
1596 	Mpi2RaidActionRequest_t	*action;
1597 	target_id_t targetid;
1598 	struct mprsas_target *target;
1599 
1600 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1601 
1602 	/* is IR firmware build loaded? */
1603 	if (!sc->ir_firmware)
1604 		goto out;
1605 
1606 	/* are there any volumes?  Look at IR target IDs. */
1607 	// TODO-later, this should be looked up in the RAID config structure
1608 	// when it is implemented.
1609 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1610 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1611 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1612 		start_idx = 0;
1613 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1614 			start_idx = 1;
1615 	} else
1616 		start_idx = sc->max_devices - sc->max_volumes;
1617 	end_idx = start_idx + sc->max_volumes - 1;
1618 
1619 	for (id = start_idx; id < end_idx; id++) {
1620 		mt_entry = &sc->mapping_table[id];
1621 		if ((mt_entry->physical_id != 0) &&
1622 		    (mt_entry->missing_count == 0)) {
1623 			found_volume = 1;
1624 			break;
1625 		}
1626 	}
1627 
1628 	if (!found_volume)
1629 		goto out;
1630 
1631 	if ((cm = mpr_alloc_command(sc)) == NULL) {
1632 		printf("%s: command alloc failed\n", __func__);
1633 		goto out;
1634 	}
1635 
1636 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1637 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1638 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1639 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1640 	mpr_lock(sc);
1641 	mpr_wait_command(sc, &cm, 5, CAN_SLEEP);
1642 	mpr_unlock(sc);
1643 
1644 	/*
1645 	 * Don't check for reply, just leave.
1646 	 */
1647 	if (cm)
1648 		mpr_free_command(sc, cm);
1649 
1650 out:
1651 	/*
1652 	 * All of the targets must have the correct value set for
1653 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1654 	 *
1655 	 * The possible values for the 'enable_ssu' variable are:
1656 	 * 0: disable to SSD and HDD
1657 	 * 1: disable only to HDD (default)
1658 	 * 2: disable only to SSD
1659 	 * 3: enable to SSD and HDD
1660 	 * anything else will default to 1.
1661 	 */
1662 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1663 		target = &sc->sassc->targets[targetid];
1664 		if (target->handle == 0x0) {
1665 			continue;
1666 		}
1667 
1668 		if (target->supports_SSU) {
1669 			switch (sc->enable_ssu) {
1670 			case MPR_SSU_DISABLE_SSD_DISABLE_HDD:
1671 				target->stop_at_shutdown = FALSE;
1672 				break;
1673 			case MPR_SSU_DISABLE_SSD_ENABLE_HDD:
1674 				target->stop_at_shutdown = TRUE;
1675 				if (target->flags & MPR_TARGET_IS_SATA_SSD) {
1676 					target->stop_at_shutdown = FALSE;
1677 				}
1678 				break;
1679 			case MPR_SSU_ENABLE_SSD_ENABLE_HDD:
1680 				target->stop_at_shutdown = TRUE;
1681 				break;
1682 			case MPR_SSU_ENABLE_SSD_DISABLE_HDD:
1683 			default:
1684 				target->stop_at_shutdown = TRUE;
1685 				if ((target->flags &
1686 				    MPR_TARGET_IS_SATA_SSD) == 0) {
1687 					target->stop_at_shutdown = FALSE;
1688 				}
1689 				break;
1690 			}
1691 		}
1692 	}
1693 	mprsas_SSU_to_SATA_devices(sc, howto);
1694 }
1695