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