xref: /src/sys/dev/mps/mps_sas_lsi.c (revision 8ef8c6abfadfc9eb0465ce57c6b09ca310415bdd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011-2015 LSI Corp.
5  * Copyright (c) 2013-2015 Avago Technologies
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
30  */
31 
32 /* Communications core for Avago Technologies (LSI) MPT2 */
33 
34 /* TODO Move headers to mpsvar */
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/mps/mpi/mpi2_type.h>
71 #include <dev/mps/mpi/mpi2.h>
72 #include <dev/mps/mpi/mpi2_ioc.h>
73 #include <dev/mps/mpi/mpi2_sas.h>
74 #include <dev/mps/mpi/mpi2_cnfg.h>
75 #include <dev/mps/mpi/mpi2_init.h>
76 #include <dev/mps/mpi/mpi2_raid.h>
77 #include <dev/mps/mpi/mpi2_tool.h>
78 #include <dev/mps/mps_ioctl.h>
79 #include <dev/mps/mpsvar.h>
80 #include <dev/mps/mps_table.h>
81 #include <dev/mps/mps_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 mps_fw_event_work {
88 	u16			event;
89 	void			*event_data;
90 	TAILQ_ENTRY(mps_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 mpssas_fw_work(struct mps_softc *sc,
115     struct mps_fw_event_work *fw_event);
116 static void mpssas_fw_event_free(struct mps_softc *,
117     struct mps_fw_event_work *);
118 static int mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate);
119 static int mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
120     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
121     u32 devinfo);
122 static void mpssas_ata_id_complete(struct mps_softc *, struct mps_command *);
123 static void mpssas_ata_id_timeout(struct mps_softc *, struct mps_command *);
124 int mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
125     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD);
126 static int mpssas_volume_add(struct mps_softc *sc,
127     u16 handle);
128 static void mpssas_SSU_to_SATA_devices(struct mps_softc *sc, int howto);
129 static void mpssas_stop_unit_done(struct cam_periph *periph,
130     union ccb *done_ccb);
131 
132 void
mpssas_evt_handler(struct mps_softc * sc,uintptr_t data,MPI2_EVENT_NOTIFICATION_REPLY * event)133 mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
134     MPI2_EVENT_NOTIFICATION_REPLY *event)
135 {
136 	struct mps_fw_event_work *fw_event;
137 	u16 sz;
138 
139 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
140 	MPS_DPRINT_EVENT(sc, sas, event);
141 	mpssas_record_event(sc, event);
142 
143 	fw_event = malloc(sizeof(struct mps_fw_event_work), M_MPT2,
144 	     M_ZERO|M_NOWAIT);
145 	if (!fw_event) {
146 		printf("%s: allocate failed for fw_event\n", __func__);
147 		return;
148 	}
149 	sz = le16toh(event->EventDataLength) * 4;
150 	fw_event->event_data = malloc(sz, M_MPT2, M_ZERO|M_NOWAIT);
151 	if (!fw_event->event_data) {
152 		printf("%s: allocate failed for event_data\n", __func__);
153 		free(fw_event, M_MPT2);
154 		return;
155 	}
156 
157 	bcopy(event->EventData, fw_event->event_data, sz);
158 	fw_event->event = event->Event;
159 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
160 	    event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
161 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
162 	    sc->track_mapping_events)
163 		sc->pending_map_events++;
164 
165 	/*
166 	 * When wait_for_port_enable flag is set, make sure that all the events
167 	 * are processed. Increment the startup_refcount and decrement it after
168 	 * events are processed.
169 	 */
170 	if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
171 	    event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
172 	    sc->wait_for_port_enable)
173 		mpssas_startup_increment(sc->sassc);
174 
175 	TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
176 	taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
177 
178 }
179 
180 static void
mpssas_fw_event_free(struct mps_softc * sc,struct mps_fw_event_work * fw_event)181 mpssas_fw_event_free(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
182 {
183 
184 	free(fw_event->event_data, M_MPT2);
185 	free(fw_event, M_MPT2);
186 }
187 
188 /**
189  * _mps_fw_work - delayed task for processing firmware events
190  * @sc: per adapter object
191  * @fw_event: The fw_event_work object
192  * Context: user.
193  *
194  * Return nothing.
195  */
196 static void
mpssas_fw_work(struct mps_softc * sc,struct mps_fw_event_work * fw_event)197 mpssas_fw_work(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
198 {
199 	struct mpssas_softc *sassc;
200 	sassc = sc->sassc;
201 
202 	mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
203 			event_count++,__func__,fw_event->event);
204 	switch (fw_event->event) {
205 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
206 	{
207 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
208 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
209 		int i;
210 
211 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
212 		    fw_event->event_data;
213 
214 		mps_mapping_topology_change_event(sc, fw_event->event_data);
215 
216 		for (i = 0; i < data->NumEntries; i++) {
217 			phy = &data->PHY[i];
218 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
219 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
220 				if (mpssas_add_device(sc,
221 				    le16toh(phy->AttachedDevHandle),
222 				    phy->LinkRate)){
223 					mps_dprint(sc, MPS_ERROR, "%s: "
224 					    "failed to add device with handle "
225 					    "0x%x\n", __func__,
226 					    le16toh(phy->AttachedDevHandle));
227 					mpssas_prepare_remove(sassc, le16toh(
228 						phy->AttachedDevHandle));
229 				}
230 				break;
231 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
232 				mpssas_prepare_remove(sassc,le16toh(
233 					phy->AttachedDevHandle));
234 				break;
235 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
236 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
237 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
238 			default:
239 				break;
240 			}
241 		}
242 		/*
243 		 * refcount was incremented for this event in
244 		 * mpssas_evt_handler.  Decrement it here because the event has
245 		 * been processed.
246 		 */
247 		mpssas_startup_decrement(sassc);
248 		break;
249 	}
250 	case MPI2_EVENT_SAS_DISCOVERY:
251 	{
252 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
253 
254 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
255 
256 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
257 			mps_dprint(sc, MPS_TRACE,"SAS discovery start event\n");
258 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
259 			mps_dprint(sc, MPS_TRACE,"SAS discovery stop event\n");
260 			sassc->flags &= ~MPSSAS_IN_DISCOVERY;
261 			mpssas_discovery_end(sassc);
262 		}
263 		break;
264 	}
265 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
266 	{
267 		mps_mapping_enclosure_dev_status_change_event(sc,
268 		    fw_event->event_data);
269 		break;
270 	}
271 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
272 	{
273 		Mpi2EventIrConfigElement_t *element;
274 		int i;
275 		u8 foreign_config;
276 		Mpi2EventDataIrConfigChangeList_t *event_data;
277 		struct mpssas_target *targ;
278 		unsigned int id;
279 
280 		event_data = fw_event->event_data;
281 		foreign_config = (le32toh(event_data->Flags) &
282 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
283 
284 		element =
285 		    (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
286 		id = mps_mapping_get_raid_tid_from_handle(sc,
287 		    element->VolDevHandle);
288 
289 		mps_mapping_ir_config_change_event(sc, event_data);
290 
291 		for (i = 0; i < event_data->NumElements; i++, element++) {
292 			switch (element->ReasonCode) {
293 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
294 			case MPI2_EVENT_IR_CHANGE_RC_ADDED:
295 				if (!foreign_config) {
296 					if (mpssas_volume_add(sc,
297 					    le16toh(element->VolDevHandle))){
298 						printf("%s: failed to add RAID "
299 						    "volume with handle 0x%x\n",
300 						    __func__, le16toh(element->
301 						    VolDevHandle));
302 					}
303 				}
304 				break;
305 			case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
306 			case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
307 				/*
308 				 * Rescan after volume is deleted or removed.
309 				 */
310 				if (!foreign_config) {
311 					if (id == MPS_MAP_BAD_ID) {
312 						printf("%s: could not get ID "
313 						    "for volume with handle "
314 						    "0x%04x\n", __func__,
315 						    le16toh(element->VolDevHandle));
316 						break;
317 					}
318 
319 					targ = &sassc->targets[id];
320 					targ->handle = 0x0;
321 					targ->encl_slot = 0x0;
322 					targ->encl_handle = 0x0;
323 					targ->exp_dev_handle = 0x0;
324 					targ->phy_num = 0x0;
325 					targ->linkrate = 0x0;
326 					mpssas_rescan_target(sc, targ);
327 					printf("RAID target id 0x%x removed\n",
328 					    targ->tid);
329 				}
330 				break;
331 			case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
332 			case MPI2_EVENT_IR_CHANGE_RC_HIDE:
333 				/*
334 				 * Phys Disk of a volume has been created.  Hide
335 				 * it from the OS.
336 				 */
337 				targ = mpssas_find_target_by_handle(sassc, 0,
338 				    element->PhysDiskDevHandle);
339 				if (targ == NULL)
340 					break;
341 
342 				/*
343 				 * Set raid component flags only if it is not
344 				 * WD. OR WrapDrive with
345 				 * WD_HIDE_ALWAYS/WD_HIDE_IF_VOLUME is set in
346 				 * NVRAM
347 				 */
348 				if((!sc->WD_available) ||
349 				((sc->WD_available &&
350 				(sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) ||
351 				(sc->WD_valid_config && (sc->WD_hide_expose ==
352 				MPS_WD_HIDE_IF_VOLUME)))) {
353 					targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
354 				}
355 				mpssas_rescan_target(sc, targ);
356 
357 				break;
358 			case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
359 				/*
360 				 * Phys Disk of a volume has been deleted.
361 				 * Expose it to the OS.
362 				 */
363 				if (mpssas_add_device(sc,
364 				    le16toh(element->PhysDiskDevHandle), 0)){
365 					printf("%s: failed to add device with "
366 					    "handle 0x%x\n", __func__,
367 					    le16toh(element->PhysDiskDevHandle));
368 					mpssas_prepare_remove(sassc, le16toh(element->
369 					    PhysDiskDevHandle));
370 				}
371 				break;
372 			}
373 		}
374 		/*
375 		 * refcount was incremented for this event in
376 		 * mpssas_evt_handler.  Decrement it here because the event has
377 		 * been processed.
378 		 */
379 		mpssas_startup_decrement(sassc);
380 		break;
381 	}
382 	case MPI2_EVENT_IR_VOLUME:
383 	{
384 		Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
385 
386 		/*
387 		 * Informational only.
388 		 */
389 		mps_dprint(sc, MPS_EVENT, "Received IR Volume event:\n");
390 		switch (event_data->ReasonCode) {
391 		case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
392   			mps_dprint(sc, MPS_EVENT, "   Volume Settings "
393   			    "changed from 0x%x to 0x%x for Volome with "
394  			    "handle 0x%x", le32toh(event_data->PreviousValue),
395  			    le32toh(event_data->NewValue),
396  			    le16toh(event_data->VolDevHandle));
397 			break;
398 		case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
399   			mps_dprint(sc, MPS_EVENT, "   Volume Status "
400   			    "changed from 0x%x to 0x%x for Volome with "
401  			    "handle 0x%x", le32toh(event_data->PreviousValue),
402  			    le32toh(event_data->NewValue),
403  			    le16toh(event_data->VolDevHandle));
404 			break;
405 		case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
406   			mps_dprint(sc, MPS_EVENT, "   Volume State "
407   			    "changed from 0x%x to 0x%x for Volome with "
408  			    "handle 0x%x", le32toh(event_data->PreviousValue),
409  			    le32toh(event_data->NewValue),
410  			    le16toh(event_data->VolDevHandle));
411 				u32 state;
412 				struct mpssas_target *targ;
413 				state = le32toh(event_data->NewValue);
414 				switch (state) {
415 				case MPI2_RAID_VOL_STATE_MISSING:
416 				case MPI2_RAID_VOL_STATE_FAILED:
417 					mpssas_prepare_volume_remove(sassc, event_data->
418 							VolDevHandle);
419 					break;
420 
421 				case MPI2_RAID_VOL_STATE_ONLINE:
422 				case MPI2_RAID_VOL_STATE_DEGRADED:
423 				case MPI2_RAID_VOL_STATE_OPTIMAL:
424 					targ = mpssas_find_target_by_handle(sassc, 0, event_data->VolDevHandle);
425 					if (targ) {
426 						printf("%s %d: Volume handle 0x%x is already added \n",
427 							       	__func__, __LINE__ , event_data->VolDevHandle);
428 						break;
429 					}
430 					if (mpssas_volume_add(sc, le16toh(event_data->VolDevHandle))) {
431 						printf("%s: failed to add RAID "
432 							"volume with handle 0x%x\n",
433 							__func__, le16toh(event_data->
434 							VolDevHandle));
435 					}
436 					break;
437 				default:
438 					break;
439 				}
440 			break;
441 		default:
442 			break;
443 		}
444 		break;
445 	}
446 	case MPI2_EVENT_IR_PHYSICAL_DISK:
447 	{
448 		Mpi2EventDataIrPhysicalDisk_t *event_data =
449 		    fw_event->event_data;
450 		struct mpssas_target *targ;
451 
452 		/*
453 		 * Informational only.
454 		 */
455 		mps_dprint(sc, MPS_EVENT, "Received IR Phys Disk event:\n");
456 		switch (event_data->ReasonCode) {
457 		case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
458   			mps_dprint(sc, MPS_EVENT, "   Phys Disk Settings "
459   			    "changed from 0x%x to 0x%x for Phys Disk Number "
460   			    "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
461  			    "%d", le32toh(event_data->PreviousValue),
462  			    le32toh(event_data->NewValue),
463  				event_data->PhysDiskNum,
464  			    le16toh(event_data->PhysDiskDevHandle),
465  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
466 			break;
467 		case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
468   			mps_dprint(sc, MPS_EVENT, "   Phys Disk Status changed "
469   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
470   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
471  				le32toh(event_data->PreviousValue),
472  			    le32toh(event_data->NewValue), event_data->PhysDiskNum,
473  			    le16toh(event_data->PhysDiskDevHandle),
474  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
475 			break;
476 		case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
477   			mps_dprint(sc, MPS_EVENT, "   Phys Disk State changed "
478   			    "from 0x%x to 0x%x for Phys Disk Number %d and "
479   			    "handle 0x%x at Enclosure handle 0x%x, Slot %d",
480  				le32toh(event_data->PreviousValue),
481  			    le32toh(event_data->NewValue), event_data->PhysDiskNum,
482  			    le16toh(event_data->PhysDiskDevHandle),
483  			    le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot));
484 			switch (event_data->NewValue) {
485 				case MPI2_RAID_PD_STATE_ONLINE:
486 				case MPI2_RAID_PD_STATE_DEGRADED:
487 				case MPI2_RAID_PD_STATE_REBUILDING:
488 				case MPI2_RAID_PD_STATE_OPTIMAL:
489 				case MPI2_RAID_PD_STATE_HOT_SPARE:
490 					targ = mpssas_find_target_by_handle(sassc, 0,
491 							event_data->PhysDiskDevHandle);
492 					if (targ) {
493 						if(!sc->WD_available) {
494 							targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
495 							printf("%s %d: Found Target for handle 0x%x.  \n",
496 							__func__, __LINE__ , event_data->PhysDiskDevHandle);
497 						} else if ((sc->WD_available &&
498 							(sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) ||
499         						(sc->WD_valid_config && (sc->WD_hide_expose ==
500         						MPS_WD_HIDE_IF_VOLUME))) {
501 							targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
502 							printf("%s %d: WD: Found Target for handle 0x%x.  \n",
503 							__func__, __LINE__ , event_data->PhysDiskDevHandle);
504 						}
505  					}
506 				break;
507 				case MPI2_RAID_PD_STATE_OFFLINE:
508 				case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
509 				case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
510 				default:
511 					targ = mpssas_find_target_by_handle(sassc, 0,
512 							event_data->PhysDiskDevHandle);
513 					if (targ) {
514 						targ->flags |= ~MPS_TARGET_FLAGS_RAID_COMPONENT;
515 						printf("%s %d: Found Target for handle 0x%x.  \n",
516 						__func__, __LINE__ , event_data->PhysDiskDevHandle);
517 					}
518 				break;
519 			}
520 		default:
521 			break;
522 		}
523 		break;
524 	}
525 	case MPI2_EVENT_IR_OPERATION_STATUS:
526 	{
527 		Mpi2EventDataIrOperationStatus_t *event_data =
528 		    fw_event->event_data;
529 
530 		/*
531 		 * Informational only.
532 		 */
533 		mps_dprint(sc, MPS_EVENT, "Received IR Op Status event:\n");
534 		mps_dprint(sc, MPS_EVENT, "   RAID Operation of %d is %d "
535 		    "percent complete for Volume with handle 0x%x",
536 		    event_data->RAIDOperation, event_data->PercentComplete,
537 		    le16toh(event_data->VolDevHandle));
538 		break;
539 	}
540 	case MPI2_EVENT_LOG_ENTRY_ADDED:
541 	{
542 		pMpi2EventDataLogEntryAdded_t	logEntry;
543 		uint16_t			logQualifier;
544 		uint8_t				logCode;
545 
546 		logEntry = (pMpi2EventDataLogEntryAdded_t)fw_event->event_data;
547 		logQualifier = logEntry->LogEntryQualifier;
548 
549 		if (logQualifier == MPI2_WD_LOG_ENTRY) {
550 			logCode = logEntry->LogData[0];
551 
552 			switch (logCode) {
553 			case MPI2_WD_SSD_THROTTLING:
554 				printf("WarpDrive Warning: IO Throttling has "
555 				    "occurred in the WarpDrive subsystem. "
556 				    "Check WarpDrive documentation for "
557 				    "additional details\n");
558 				break;
559 			case MPI2_WD_DRIVE_LIFE_WARN:
560 				printf("WarpDrive Warning: Program/Erase "
561 				    "Cycles for the WarpDrive subsystem in "
562 				    "degraded range. Check WarpDrive "
563 				    "documentation for additional details\n");
564 				break;
565 			case MPI2_WD_DRIVE_LIFE_DEAD:
566 				printf("WarpDrive Fatal Error: There are no "
567 				    "Program/Erase Cycles for the WarpDrive "
568 				    "subsystem. The storage device will be in "
569 				    "read-only mode. Check WarpDrive "
570 				    "documentation for additional details\n");
571 				break;
572 			case MPI2_WD_RAIL_MON_FAIL:
573 				printf("WarpDrive Fatal Error: The Backup Rail "
574 				    "Monitor has failed on the WarpDrive "
575 				    "subsystem. Check WarpDrive documentation "
576 				    "for additional details\n");
577 				break;
578 			default:
579 				break;
580 			}
581 		}
582 		break;
583 	}
584 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
585 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
586 	default:
587 		mps_dprint(sc, MPS_TRACE,"Unhandled event 0x%0X\n",
588 		    fw_event->event);
589 		break;
590 	}
591 	mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Event Free: [%x]\n",event_count,__func__, fw_event->event);
592 	mpssas_fw_event_free(sc, fw_event);
593 }
594 
595 void
mpssas_firmware_event_work(void * arg,int pending)596 mpssas_firmware_event_work(void *arg, int pending)
597 {
598 	struct mps_fw_event_work *fw_event;
599 	struct mps_softc *sc;
600 
601 	sc = (struct mps_softc *)arg;
602 	mps_lock(sc);
603 	while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
604 		TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
605 		mpssas_fw_work(sc, fw_event);
606 	}
607 	mps_unlock(sc);
608 }
609 
610 static int
mpssas_add_device(struct mps_softc * sc,u16 handle,u8 linkrate)611 mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate){
612 	char devstring[80];
613 	struct mpssas_softc *sassc;
614 	struct mpssas_target *targ;
615 	Mpi2ConfigReply_t mpi_reply;
616 	Mpi2SasDevicePage0_t config_page;
617 	uint64_t sas_address;
618 	uint64_t parent_sas_address = 0;
619 	u32 device_info, parent_devinfo = 0;
620 	unsigned int id;
621 	int ret = 1, error = 0, i;
622 	struct mpssas_lun *lun;
623 	u8 is_SATA_SSD = 0;
624 	struct mps_command *cm;
625 
626 	sassc = sc->sassc;
627 	mpssas_startup_increment(sassc);
628 	if (mps_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
629 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle) != 0) {
630 		mps_dprint(sc, MPS_INFO|MPS_MAPPING|MPS_FAULT,
631 		    "Error reading SAS device %#x page0, iocstatus= 0x%x\n",
632 		    handle, mpi_reply.IOCStatus);
633 		error = ENXIO;
634 		goto out;
635 	}
636 
637 	device_info = le32toh(config_page.DeviceInfo);
638 
639 	if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
640 	 && (le16toh(config_page.ParentDevHandle) != 0)) {
641 		Mpi2ConfigReply_t tmp_mpi_reply;
642 		Mpi2SasDevicePage0_t parent_config_page;
643 
644 		if (mps_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
645 		    &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
646 		    le16toh(config_page.ParentDevHandle)) != 0) {
647 			mps_dprint(sc, MPS_MAPPING|MPS_FAULT,
648 			    "Error reading parent SAS device %#x page0, "
649 			    "iocstatus= 0x%x\n",
650 			    le16toh(config_page.ParentDevHandle),
651 			    tmp_mpi_reply.IOCStatus);
652 		} else {
653 			parent_sas_address = parent_config_page.SASAddress.High;
654 			parent_sas_address = (parent_sas_address << 32) |
655 				parent_config_page.SASAddress.Low;
656 			parent_devinfo = le32toh(parent_config_page.DeviceInfo);
657 		}
658 	}
659 	/* TODO Check proper endianness */
660 	sas_address = config_page.SASAddress.High;
661 	sas_address = (sas_address << 32) | config_page.SASAddress.Low;
662         mps_dprint(sc, MPS_MAPPING, "Handle 0x%04x SAS Address from SAS device "
663             "page0 = %jx\n", handle, sas_address);
664 
665 	/*
666 	 * Always get SATA Identify information because this is used to
667 	 * determine if Start/Stop Unit should be sent to the drive when the
668 	 * system is shutdown.
669 	 */
670 	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
671 		ret = mpssas_get_sas_address_for_sata_disk(sc, &sas_address,
672 		    handle, device_info, &is_SATA_SSD);
673 		if (ret) {
674 			mps_dprint(sc, MPS_MAPPING|MPS_ERROR,
675 			    "%s: failed to get disk type (SSD or HDD) for SATA "
676 			    "device with handle 0x%04x\n",
677 			    __func__, handle);
678 		} else {
679 			mps_dprint(sc, MPS_MAPPING, "Handle 0x%04x SAS Address "
680 			    "from SATA device = %jx\n", handle, sas_address);
681 		}
682 	}
683 
684 	/*
685 	 * use_phynum:
686 	 *  1 - use the PhyNum field as a fallback to the mapping logic
687 	 *  0 - never use the PhyNum field
688 	 * -1 - only use the PhyNum field
689 	 *
690 	 * Note that using the Phy number to map a device can cause device adds
691 	 * to fail if multiple enclosures/expanders are in the topology. For
692 	 * example, if two devices are in the same slot number in two different
693 	 * enclosures within the topology, only one of those devices will be
694 	 * added. PhyNum mapping should not be used if multiple enclosures are
695 	 * in the topology.
696 	 */
697 	id = MPS_MAP_BAD_ID;
698 	if (sc->use_phynum != -1)
699 		id = mps_mapping_get_tid(sc, sas_address, handle);
700 	if (id == MPS_MAP_BAD_ID) {
701 		if ((sc->use_phynum == 0)
702 		 || ((id = config_page.PhyNum) > sassc->maxtargets)) {
703 			mps_dprint(sc, MPS_INFO, "failure at %s:%d/%s()! "
704 			    "Could not get ID for device with handle 0x%04x\n",
705 			    __FILE__, __LINE__, __func__, handle);
706 			error = ENXIO;
707 			goto out;
708 		}
709 	}
710 	mps_dprint(sc, MPS_MAPPING, "%s: Target ID for added device is %d.\n",
711 	    __func__, id);
712 
713 	/*
714 	 * Only do the ID check and reuse check if the target is not from a
715 	 * RAID Component. For Physical Disks of a Volume, the ID will be reused
716 	 * when a volume is deleted because the mapping entry for the PD will
717 	 * still be in the mapping table. The ID check should not be done here
718 	 * either since this PD is already being used.
719 	 */
720 	targ = &sassc->targets[id];
721 	if (!(targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT)) {
722 		if (mpssas_check_id(sassc, id) != 0) {
723 			mps_dprint(sc, MPS_MAPPING|MPS_INFO,
724 			    "Excluding target id %d\n", id);
725 			error = ENXIO;
726 			goto out;
727 		}
728 
729 		if (targ->handle != 0x0) {
730 			mps_dprint(sc, MPS_MAPPING, "Attempting to reuse "
731 			    "target id %d handle 0x%04x\n", id, targ->handle);
732 			error = ENXIO;
733 			goto out;
734 		}
735 	}
736 
737 	targ->devinfo = device_info;
738 	targ->devname = le32toh(config_page.DeviceName.High);
739 	targ->devname = (targ->devname << 32) |
740 	    le32toh(config_page.DeviceName.Low);
741 	targ->encl_handle = le16toh(config_page.EnclosureHandle);
742 	targ->encl_slot = le16toh(config_page.Slot);
743 	targ->handle = handle;
744 	targ->parent_handle = le16toh(config_page.ParentDevHandle);
745 	targ->sasaddr = mps_to_u64(&config_page.SASAddress);
746 	targ->parent_sasaddr = le64toh(parent_sas_address);
747 	targ->parent_devinfo = parent_devinfo;
748 	targ->tid = id;
749 	targ->linkrate = (linkrate>>4);
750 	targ->flags = 0;
751 	if (is_SATA_SSD) {
752 		targ->flags = MPS_TARGET_IS_SATA_SSD;
753 	}
754 	TAILQ_INIT(&targ->commands);
755 	TAILQ_INIT(&targ->timedout_commands);
756 	while(!SLIST_EMPTY(&targ->luns)) {
757 		lun = SLIST_FIRST(&targ->luns);
758 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
759 		free(lun, M_MPT2);
760 	}
761 	SLIST_INIT(&targ->luns);
762 
763 	mps_describe_devinfo(targ->devinfo, devstring, 80);
764 	mps_dprint(sc, MPS_MAPPING, "Found device <%s> <%s> <0x%04x> <%d/%d>\n",
765 	    devstring, mps_describe_table(mps_linkrate_names, targ->linkrate),
766 	    targ->handle, targ->encl_handle, targ->encl_slot);
767 
768 	mpssas_rescan_target(sc, targ);
769 	mps_dprint(sc, MPS_MAPPING, "Target id 0x%x added\n", targ->tid);
770 
771 	/*
772 	 * Check all commands to see if the SATA_ID_TIMEOUT flag has been set.
773 	 * If so, send a Target Reset TM to the target that was just created.
774 	 * An Abort Task TM should be used instead of a Target Reset, but that
775 	 * would be much more difficult because targets have not been fully
776 	 * discovered yet, and LUN's haven't been setup.  So, just reset the
777 	 * target instead of the LUN.  The commands should complete once the
778 	 * target has been reset.
779 	 */
780 	for (i = 1; i < sc->num_reqs; i++) {
781 		cm = &sc->commands[i];
782 		if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) {
783 			targ->timeouts++;
784 			cm->cm_flags |= MPS_CM_FLAGS_TIMEDOUT;
785 
786 			if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
787 				mps_dprint(sc, MPS_INFO, "%s: sending Target "
788 				    "Reset for stuck SATA identify command "
789 				    "(cm = %p)\n", __func__, cm);
790 				targ->tm->cm_targ = targ;
791 				mpssas_send_reset(sc, targ->tm,
792 				    MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
793 			} else {
794 				mps_dprint(sc, MPS_ERROR, "Failed to allocate "
795 				    "tm for Target Reset after SATA ID command "
796 				    "timed out (cm %p)\n", cm);
797 			}
798 			/*
799 			 * No need to check for more since the target is
800 			 * already being reset.
801 			 */
802 			break;
803 		}
804 	}
805 out:
806 	mpssas_startup_decrement(sassc);
807 	return (error);
808 }
809 
810 int
mpssas_get_sas_address_for_sata_disk(struct mps_softc * sc,u64 * sas_address,u16 handle,u32 device_info,u8 * is_SATA_SSD)811 mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
812     u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD)
813 {
814 	Mpi2SataPassthroughReply_t mpi_reply;
815 	int i, rc, try_count;
816 	u32 *bufferptr;
817 	union _sata_sas_address hash_address;
818 	struct _ata_identify_device_data ata_identify;
819 	u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
820 	u32 ioc_status;
821 	u8 sas_status;
822 
823 	memset(&ata_identify, 0, sizeof(ata_identify));
824 	try_count = 0;
825 	do {
826 		rc = mpssas_get_sata_identify(sc, handle, &mpi_reply,
827 		    (char *)&ata_identify, sizeof(ata_identify), device_info);
828 		try_count++;
829 		ioc_status = le16toh(mpi_reply.IOCStatus)
830 		    & MPI2_IOCSTATUS_MASK;
831 		sas_status = mpi_reply.SASStatus;
832 		switch (ioc_status) {
833 		case MPI2_IOCSTATUS_SUCCESS:
834 			break;
835 		case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
836 			/* No sense sleeping.  this error won't get better */
837 			break;
838 		default:
839 			if (sc->spinup_wait_time > 0) {
840 				mps_dprint(sc, MPS_INFO, "Sleeping %d seconds "
841 				    "after SATA ID error to wait for spinup\n",
842 				    sc->spinup_wait_time);
843 				msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
844 				    "mpsid", sc->spinup_wait_time * hz);
845 			}
846 		}
847 	} while (((rc && (rc != EWOULDBLOCK)) ||
848 	    	 (ioc_status &&
849 		  (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR))
850 	       || sas_status) && (try_count < 5));
851 
852 	if (rc == 0 && !ioc_status && !sas_status) {
853 		mps_dprint(sc, MPS_MAPPING, "%s: got SATA identify "
854 		    "successfully for handle = 0x%x with try_count = %d\n",
855 		    __func__, handle, try_count);
856 	} else {
857 		mps_dprint(sc, MPS_MAPPING, "%s: handle = 0x%x failed\n",
858 		    __func__, handle);
859 		return -1;
860 	}
861 	/* Copy & byteswap the 40 byte model number to a buffer */
862 	for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
863 		buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
864 		buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
865 	}
866 	/* Copy & byteswap the 20 byte serial number to a buffer */
867 	for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
868 		buffer[MPT2SAS_MN_LEN + i] =
869 		    ((u8 *)ata_identify.serial_number)[i + 1];
870 		buffer[MPT2SAS_MN_LEN + i + 1] =
871 		    ((u8 *)ata_identify.serial_number)[i];
872 	}
873 	bufferptr = (u32 *)buffer;
874 	/* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
875 	 * so loop through the first 56 bytes (7*8),
876 	 * and then add in the last dword.
877 	 */
878 	hash_address.word.low  = 0;
879 	hash_address.word.high = 0;
880 	for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
881 		hash_address.word.low += *bufferptr;
882 		bufferptr++;
883 		hash_address.word.high += *bufferptr;
884 		bufferptr++;
885 	}
886 	/* Add the last dword */
887 	hash_address.word.low += *bufferptr;
888 	/* Make sure the hash doesn't start with 5, because it could clash
889 	 * with a SAS address. Change 5 to a D.
890 	 */
891 	if ((hash_address.word.high & 0x000000F0) == (0x00000050))
892 		hash_address.word.high |= 0x00000080;
893 	*sas_address = (u64)hash_address.wwid[0] << 56 |
894 	    (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
895 	    (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
896 	    (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
897 	    (u64)hash_address.wwid[7];
898 	if (ata_identify.rotational_speed == 1) {
899 		*is_SATA_SSD = 1;
900 	}
901 
902 	return 0;
903 }
904 
905 static int
mpssas_get_sata_identify(struct mps_softc * sc,u16 handle,Mpi2SataPassthroughReply_t * mpi_reply,char * id_buffer,int sz,u32 devinfo)906 mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
907     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
908 {
909 	Mpi2SataPassthroughRequest_t *mpi_request;
910 	Mpi2SataPassthroughReply_t *reply = NULL;
911 	struct mps_command *cm;
912 	char *buffer;
913 	int error = 0;
914 
915 	buffer = malloc( sz, M_MPT2, M_NOWAIT | M_ZERO);
916 	if (!buffer)
917 		return ENOMEM;
918 
919 	if ((cm = mps_alloc_command(sc)) == NULL) {
920 		free(buffer, M_MPT2);
921 		return (EBUSY);
922 	}
923 	mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
924 	bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
925 	mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
926 	mpi_request->VF_ID = 0;
927 	mpi_request->DevHandle = htole16(handle);
928 	mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
929 	    MPI2_SATA_PT_REQ_PT_FLAGS_READ);
930 	mpi_request->DataLength = htole32(sz);
931 	mpi_request->CommandFIS[0] = 0x27;
932 	mpi_request->CommandFIS[1] = 0x80;
933 	mpi_request->CommandFIS[2] =  (devinfo &
934 	    MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
935 	cm->cm_sge = &mpi_request->SGL;
936 	cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
937 	cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
938 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
939 	cm->cm_data = buffer;
940 	cm->cm_length = htole32(sz);
941 
942 	/*
943 	 * Use a custom handler to avoid reinit'ing the controller on timeout.
944 	 * This fixes a problem where the FW does not send a reply sometimes
945 	 * when a bad disk is in the topology. So, this is used to timeout the
946 	 * command so that processing can continue normally.
947 	 */
948 	cm->cm_timeout_handler = mpssas_ata_id_timeout;
949 
950 	error = mps_wait_command(sc, &cm, MPS_ATA_ID_TIMEOUT, CAN_SLEEP);
951 
952 	/* mpssas_ata_id_timeout does not reset controller */
953 	KASSERT(cm != NULL, ("%s: surprise command freed", __func__));
954 
955 	reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
956 	if (error || (reply == NULL)) {
957 		/* FIXME */
958  		/*
959  		 * If the request returns an error then we need to do a diag
960  		 * reset
961  		 */
962  		mps_dprint(sc, MPS_INFO|MPS_FAULT|MPS_MAPPING,
963 		    "Request for SATA PASSTHROUGH page completed with error %d\n",
964 		    error);
965 		error = ENXIO;
966 		goto out;
967 	}
968 	bcopy(buffer, id_buffer, sz);
969 	bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
970 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
971 	    MPI2_IOCSTATUS_SUCCESS) {
972 		mps_dprint(sc, MPS_INFO|MPS_MAPPING|MPS_FAULT,
973 		    "Error reading device %#x SATA PASSTHRU; iocstatus= 0x%x\n",
974 		    handle, reply->IOCStatus);
975 		error = ENXIO;
976 		goto out;
977 	}
978 out:
979 	/*
980 	 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free
981 	 * it.  The command and buffer will be freed after we send a Target
982 	 * Reset TM and the command comes back from the controller.
983 	 */
984 	if ((cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) == 0) {
985 		mps_free_command(sc, cm);
986 		free(buffer, M_MPT2);
987 	}
988 	return (error);
989 }
990 
991 /*
992  * This is completion handler to make sure that commands and allocated
993  * buffers get freed when timed out SATA ID commands finally complete after
994  * we've reset the target.  In the normal case, we wait for the command to
995  * complete.
996  */
997 static void
mpssas_ata_id_complete(struct mps_softc * sc,struct mps_command * cm)998 mpssas_ata_id_complete(struct mps_softc *sc, struct mps_command *cm)
999 {
1000 	mps_dprint(sc, MPS_INFO, "%s ATA ID completed late cm %p sc %p\n",
1001 	    __func__, cm, sc);
1002 
1003 	free(cm->cm_data, M_MPT2);
1004 	mps_free_command(sc, cm);
1005 }
1006 
1007 
1008 static void
mpssas_ata_id_timeout(struct mps_softc * sc,struct mps_command * cm)1009 mpssas_ata_id_timeout(struct mps_softc *sc, struct mps_command *cm)
1010 {
1011 	mps_dprint(sc, MPS_INFO, "%s ATA ID command timeout cm %p sc %p\n",
1012 	    __func__, cm, sc);
1013 
1014 	/*
1015 	 * The Abort Task cannot be sent from here because the driver has not
1016 	 * completed setting up targets.  Instead, the command is flagged so
1017 	 * that special handling will be used to send a target reset.
1018 	 */
1019 	cm->cm_flags |= MPS_CM_FLAGS_SATA_ID_TIMEOUT;
1020 
1021 	/*
1022 	 * Since we will no longer be waiting for the command to complete,
1023 	 * set a completion handler to make sure we free all resources.
1024 	 */
1025 	cm->cm_complete = mpssas_ata_id_complete;
1026 }
1027 
1028 static int
mpssas_volume_add(struct mps_softc * sc,u16 handle)1029 mpssas_volume_add(struct mps_softc *sc, u16 handle)
1030 {
1031 	struct mpssas_softc *sassc;
1032 	struct mpssas_target *targ;
1033 	u64 wwid;
1034 	unsigned int id;
1035 	int error = 0;
1036 	struct mpssas_lun *lun;
1037 
1038 	sassc = sc->sassc;
1039 	mpssas_startup_increment(sassc);
1040 	/* wwid is endian safe */
1041 	mps_config_get_volume_wwid(sc, handle, &wwid);
1042 	if (!wwid) {
1043 		printf("%s: invalid WWID; cannot add volume to mapping table\n",
1044 		    __func__);
1045 		error = ENXIO;
1046 		goto out;
1047 	}
1048 
1049 	id = mps_mapping_get_raid_tid(sc, wwid, handle);
1050 	if (id == MPS_MAP_BAD_ID) {
1051 		printf("%s: could not get ID for volume with handle 0x%04x and "
1052 		    "WWID 0x%016llx\n", __func__, handle,
1053 		    (unsigned long long)wwid);
1054 		error = ENXIO;
1055 		goto out;
1056 	}
1057 
1058 	targ = &sassc->targets[id];
1059 	targ->tid = id;
1060 	targ->handle = handle;
1061 	targ->devname = wwid;
1062 	TAILQ_INIT(&targ->commands);
1063 	TAILQ_INIT(&targ->timedout_commands);
1064 	while(!SLIST_EMPTY(&targ->luns)) {
1065 		lun = SLIST_FIRST(&targ->luns);
1066 		SLIST_REMOVE_HEAD(&targ->luns, lun_link);
1067 		free(lun, M_MPT2);
1068 	}
1069 	SLIST_INIT(&targ->luns);
1070 	mpssas_rescan_target(sc, targ);
1071 	mps_dprint(sc, MPS_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1072 	    targ->tid, wwid);
1073 out:
1074 	mpssas_startup_decrement(sassc);
1075 	return (error);
1076 }
1077 
1078 /**
1079  * mpssas_SSU_to_SATA_devices
1080  * @sc: per adapter object
1081  * @howto: mast of RB_* bits for how we're rebooting
1082  *
1083  * Looks through the target list and issues a StartStopUnit SCSI command to each
1084  * SATA direct-access device.  This helps to ensure that data corruption is
1085  * avoided when the system is being shut down.  This must be called after the IR
1086  * System Shutdown RAID Action is sent if in IR mode.
1087  *
1088  * Return nothing.
1089  */
1090 static void
mpssas_SSU_to_SATA_devices(struct mps_softc * sc,int howto)1091 mpssas_SSU_to_SATA_devices(struct mps_softc *sc, int howto)
1092 {
1093 	struct mpssas_softc *sassc = sc->sassc;
1094 	union ccb *ccb;
1095 	path_id_t pathid = cam_sim_path(sassc->sim);
1096 	target_id_t targetid;
1097 	struct mpssas_target *target;
1098 	char path_str[64];
1099 	int timeout;
1100 
1101 	/*
1102 	 * For each target, issue a StartStopUnit command to stop the device.
1103 	 */
1104 	sc->SSU_started = TRUE;
1105 	sc->SSU_refcount = 0;
1106 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1107 		target = &sassc->targets[targetid];
1108 		if (target->handle == 0x0) {
1109 			continue;
1110 		}
1111 
1112 		ccb = xpt_alloc_ccb_nowait();
1113 		if (ccb == NULL) {
1114 			mps_dprint(sc, MPS_FAULT, "Unable to alloc CCB to stop "
1115 			    "unit.\n");
1116 			return;
1117 		}
1118 
1119 		/*
1120 		 * The stop_at_shutdown flag will be set if this device is
1121 		 * a SATA direct-access end device.
1122 		 */
1123 		if (target->stop_at_shutdown) {
1124 			if (xpt_create_path(&ccb->ccb_h.path,
1125 			    xpt_periph, pathid, targetid,
1126 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1127 				mps_dprint(sc, MPS_FAULT, "Unable to create "
1128 				    "LUN path to stop unit.\n");
1129 				xpt_free_ccb(ccb);
1130 				return;
1131 			}
1132 			xpt_path_string(ccb->ccb_h.path, path_str,
1133 			    sizeof(path_str));
1134 
1135 			mps_dprint(sc, MPS_INFO, "Sending StopUnit: path %s "
1136 			    "handle %d\n", path_str, target->handle);
1137 
1138 			/*
1139 			 * Issue a START STOP UNIT command for the target.
1140 			 * Increment the SSU counter to be used to count the
1141 			 * number of required replies.
1142 			 */
1143 			mps_dprint(sc, MPS_INFO, "Incrementing SSU count\n");
1144 			sc->SSU_refcount++;
1145 			ccb->ccb_h.target_id =
1146 			    xpt_path_target_id(ccb->ccb_h.path);
1147 			ccb->ccb_h.ppriv_ptr1 = sassc;
1148 			scsi_start_stop(&ccb->csio,
1149 			    /*retries*/0,
1150 			    mpssas_stop_unit_done,
1151 			    MSG_SIMPLE_Q_TAG,
1152 			    /*start*/FALSE,
1153 			    /*load/eject*/0,
1154 			    /*immediate*/FALSE,
1155 			    MPS_SENSE_LEN,
1156 			    /*timeout*/10000);
1157 			xpt_action(ccb);
1158 		}
1159 	}
1160 
1161 	/*
1162 	 * Timeout after 60 seconds by default or 10 seconds if howto has
1163 	 * RB_NOSYNC set which indicates we're likely handling a panic.
1164 	 */
1165 	timeout = 600;
1166 	if (howto & RB_NOSYNC)
1167 		timeout = 100;
1168 
1169 	/*
1170 	 * Wait until all of the SSU commands have completed or timeout has
1171 	 * expired.  Pause for 100ms each time through.  If any command
1172 	 * times out, the target will be reset in the SCSI command timeout
1173 	 * routine.
1174 	 */
1175 	while (sc->SSU_refcount > 0) {
1176 		pause("mpswait", hz/10);
1177 		if (SCHEDULER_STOPPED())
1178 			xpt_sim_poll(sassc->sim);
1179 
1180 		if (--timeout == 0) {
1181 			mps_dprint(sc, MPS_FAULT, "Time has expired waiting "
1182 			    "for SSU commands to complete.\n");
1183 			break;
1184 		}
1185 	}
1186 }
1187 
1188 static void
mpssas_stop_unit_done(struct cam_periph * periph,union ccb * done_ccb)1189 mpssas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1190 {
1191 	struct mpssas_softc *sassc;
1192 	char path_str[64];
1193 
1194 	if (done_ccb == NULL)
1195 		return;
1196 
1197 	sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1198 
1199 	xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1200 	mps_dprint(sassc->sc, MPS_INFO, "Completing stop unit for %s\n",
1201 	    path_str);
1202 
1203 	/*
1204 	 * Nothing more to do except free the CCB and path.  If the command
1205 	 * timed out, an abort reset, then target reset will be issued during
1206 	 * the SCSI Command process.
1207 	 */
1208 	xpt_free_path(done_ccb->ccb_h.path);
1209 	xpt_free_ccb(done_ccb);
1210 }
1211 
1212 /**
1213  * mpssas_ir_shutdown - IR shutdown notification
1214  * @sc: per adapter object
1215  * @howto: mast of RB_* bits for how we're rebooting
1216  *
1217  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1218  * the host system is shutting down.
1219  *
1220  * Return nothing.
1221  */
1222 void
mpssas_ir_shutdown(struct mps_softc * sc,int howto)1223 mpssas_ir_shutdown(struct mps_softc *sc, int howto)
1224 {
1225 	u16 volume_mapping_flags;
1226 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1227 	struct dev_mapping_table *mt_entry;
1228 	u32 start_idx, end_idx;
1229 	unsigned int id, found_volume = 0;
1230 	struct mps_command *cm;
1231 	Mpi2RaidActionRequest_t	*action;
1232 	target_id_t targetid;
1233 	struct mpssas_target *target;
1234 
1235 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1236 
1237 	/* is IR firmware build loaded? */
1238 	if (!sc->ir_firmware)
1239 		goto out;
1240 
1241 	/* are there any volumes?  Look at IR target IDs. */
1242 	// TODO-later, this should be looked up in the RAID config structure
1243 	// when it is implemented.
1244 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1245 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1246 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1247 		start_idx = 0;
1248 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1249 			start_idx = 1;
1250 	} else
1251 		start_idx = sc->max_devices - sc->max_volumes;
1252 	end_idx = start_idx + sc->max_volumes - 1;
1253 
1254 	for (id = start_idx; id < end_idx; id++) {
1255 		mt_entry = &sc->mapping_table[id];
1256 		if ((mt_entry->physical_id != 0) &&
1257 		    (mt_entry->missing_count == 0)) {
1258 			found_volume = 1;
1259 			break;
1260 		}
1261 	}
1262 
1263 	if (!found_volume)
1264 		goto out;
1265 
1266 	if ((cm = mps_alloc_command(sc)) == NULL) {
1267 		printf("%s: command alloc failed\n", __func__);
1268 		goto out;
1269 	}
1270 
1271 	action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1272 	action->Function = MPI2_FUNCTION_RAID_ACTION;
1273 	action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1274 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1275 	mps_lock(sc);
1276 	mps_wait_command(sc, &cm, 5, CAN_SLEEP);
1277 	mps_unlock(sc);
1278 
1279 	/*
1280 	 * Don't check for reply, just leave.
1281 	 */
1282 	if (cm)
1283 		mps_free_command(sc, cm);
1284 
1285 out:
1286 	/*
1287 	 * All of the targets must have the correct value set for
1288 	 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable.
1289 	 *
1290 	 * The possible values for the 'enable_ssu' variable are:
1291 	 * 0: disable to SSD and HDD
1292 	 * 1: disable only to HDD (default)
1293 	 * 2: disable only to SSD
1294 	 * 3: enable to SSD and HDD
1295 	 * anything else will default to 1.
1296 	 */
1297 	for (targetid = 0; targetid < sc->max_devices; targetid++) {
1298 		target = &sc->sassc->targets[targetid];
1299 		if (target->handle == 0x0) {
1300 			continue;
1301 		}
1302 
1303 		if (target->supports_SSU) {
1304 			switch (sc->enable_ssu) {
1305 			case MPS_SSU_DISABLE_SSD_DISABLE_HDD:
1306 				target->stop_at_shutdown = FALSE;
1307 				break;
1308 			case MPS_SSU_DISABLE_SSD_ENABLE_HDD:
1309 				target->stop_at_shutdown = TRUE;
1310 				if (target->flags & MPS_TARGET_IS_SATA_SSD) {
1311 					target->stop_at_shutdown = FALSE;
1312 				}
1313 				break;
1314 			case MPS_SSU_ENABLE_SSD_ENABLE_HDD:
1315 				target->stop_at_shutdown = TRUE;
1316 				break;
1317 			case MPS_SSU_ENABLE_SSD_DISABLE_HDD:
1318 			default:
1319 				target->stop_at_shutdown = TRUE;
1320 				if ((target->flags &
1321 				    MPS_TARGET_IS_SATA_SSD) == 0) {
1322 					target->stop_at_shutdown = FALSE;
1323 				}
1324 				break;
1325 			}
1326 		}
1327 	}
1328 	mpssas_SSU_to_SATA_devices(sc, howto);
1329 }
1330