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