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