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