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