]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mpr/mpr_sas_lsi.c
MFC the mpr(4) driver for LSI's 12Gb SAS cards.
[FreeBSD/stable/10.git] / sys / dev / mpr / mpr_sas_lsi.c
1 /*-
2  * Copyright (c) 2011-2014 LSI Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * LSI MPT-Fusion Host Adapter FreeBSD
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /* Communications core for LSI MPT2 */
33
34 /* TODO Move headers to mprvar */
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/selinfo.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/bio.h>
44 #include <sys/malloc.h>
45 #include <sys/uio.h>
46 #include <sys/sysctl.h>
47 #include <sys/endian.h>
48 #include <sys/queue.h>
49 #include <sys/kthread.h>
50 #include <sys/taskqueue.h>
51 #include <sys/sbuf.h>
52
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <sys/rman.h>
56
57 #include <machine/stdarg.h>
58
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_debug.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_periph.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68
69 #include <dev/mpr/mpi/mpi2_type.h>
70 #include <dev/mpr/mpi/mpi2.h>
71 #include <dev/mpr/mpi/mpi2_ioc.h>
72 #include <dev/mpr/mpi/mpi2_sas.h>
73 #include <dev/mpr/mpi/mpi2_cnfg.h>
74 #include <dev/mpr/mpi/mpi2_init.h>
75 #include <dev/mpr/mpi/mpi2_raid.h>
76 #include <dev/mpr/mpi/mpi2_tool.h>
77 #include <dev/mpr/mpr_ioctl.h>
78 #include <dev/mpr/mprvar.h>
79 #include <dev/mpr/mpr_table.h>
80 #include <dev/mpr/mpr_sas.h>
81
82 /* For Hashed SAS Address creation for SATA Drives */
83 #define MPT2SAS_SN_LEN 20
84 #define MPT2SAS_MN_LEN 40
85
86 struct mpr_fw_event_work {
87         u16                     event;
88         void                    *event_data;
89         TAILQ_ENTRY(mpr_fw_event_work)  ev_link;
90 };
91
92 union _sata_sas_address {
93         u8 wwid[8];
94         struct {
95                 u32 high;
96                 u32 low;
97         } word;
98 };
99
100 /*
101  * define the IDENTIFY DEVICE structure
102  */
103 struct _ata_identify_device_data {
104         u16 reserved1[10];      /* 0-9 */
105         u16 serial_number[10];  /* 10-19 */
106         u16 reserved2[7];       /* 20-26 */
107         u16 model_number[20];   /* 27-46*/
108         u16 reserved3[209];     /* 47-255*/
109 };
110 static u32 event_count;
111 static void mprsas_fw_work(struct mpr_softc *sc,
112     struct mpr_fw_event_work *fw_event);
113 static void mprsas_fw_event_free(struct mpr_softc *,
114     struct mpr_fw_event_work *);
115 static int mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate);
116 static int mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
117     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
118     u32 devinfo);
119 int mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
120     u64 *sas_address, u16 handle, u32 device_info);
121 static int mprsas_volume_add(struct mpr_softc *sc,
122     u16 handle);
123 static void mprsas_SSU_to_SATA_devices(struct mpr_softc *sc);
124 static void mprsas_stop_unit_done(struct cam_periph *periph,
125     union ccb *done_ccb);
126
127 void
128 mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
129     MPI2_EVENT_NOTIFICATION_REPLY *event)
130 {
131         struct mpr_fw_event_work *fw_event;
132         u16 sz;
133
134         mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
135         mpr_print_evt_sas(sc, event);
136         mprsas_record_event(sc, event);
137
138         fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
139              M_ZERO|M_NOWAIT);
140         if (!fw_event) {
141                 printf("%s: allocate failed for fw_event\n", __func__);
142                 return;
143         }
144         sz = le16toh(event->EventDataLength) * 4;
145         fw_event->event_data = malloc(sz, M_MPR, M_ZERO|M_NOWAIT);
146         if (!fw_event->event_data) {
147                 printf("%s: allocate failed for event_data\n", __func__);
148                 free(fw_event, M_MPR);
149                 return;
150         }
151
152         bcopy(event->EventData, fw_event->event_data, sz);
153         fw_event->event = event->Event;
154         if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
155             event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
156             event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
157             sc->track_mapping_events)
158                 sc->pending_map_events++;
159
160         /*
161          * When wait_for_port_enable flag is set, make sure that all the events
162          * are processed. Increment the startup_refcount and decrement it after
163          * events are processed.
164          */
165         if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
166             event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
167             sc->wait_for_port_enable)
168                 mprsas_startup_increment(sc->sassc);
169
170         TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
171         taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
172
173 }
174
175 static void
176 mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
177 {
178
179         free(fw_event->event_data, M_MPR);
180         free(fw_event, M_MPR);
181 }
182
183 /**
184  * _mpr_fw_work - delayed task for processing firmware events
185  * @sc: per adapter object
186  * @fw_event: The fw_event_work object
187  * Context: user.
188  *
189  * Return nothing.
190  */
191 static void
192 mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
193 {
194         struct mprsas_softc *sassc;
195         sassc = sc->sassc;
196
197         mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Working on  Event: [%x]\n",
198             event_count++, __func__, fw_event->event);
199         switch (fw_event->event) {
200         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST: 
201         {
202                 MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
203                 MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
204                 int i;
205
206                 data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
207                     fw_event->event_data;
208
209                 mpr_mapping_topology_change_event(sc, fw_event->event_data);
210
211                 for (i = 0; i < data->NumEntries; i++) {
212                         phy = &data->PHY[i];
213                         switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
214                         case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
215                                 if (mprsas_add_device(sc,
216                                     le16toh(phy->AttachedDevHandle),
217                                     phy->LinkRate)) {
218                                         printf("%s: failed to add device with "
219                                             "handle 0x%x\n", __func__,
220                                             le16toh(phy->AttachedDevHandle));
221                                         mprsas_prepare_remove(sassc, le16toh(
222                                             phy->AttachedDevHandle));
223                                 }
224                                 break;
225                         case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
226                                 mprsas_prepare_remove(sassc, le16toh(
227                                     phy->AttachedDevHandle));
228                                 break;
229                         case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
230                         case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
231                         case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
232                         default:
233                                 break;
234                         }
235                 }
236                 /*
237                  * refcount was incremented for this event in
238                  * mprsas_evt_handler.  Decrement it here because the event has
239                  * been processed.
240                  */
241                 mprsas_startup_decrement(sassc);
242                 break;
243         }
244         case MPI2_EVENT_SAS_DISCOVERY:
245         {
246                 MPI2_EVENT_DATA_SAS_DISCOVERY *data;
247
248                 data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
249
250                 if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
251                         mpr_dprint(sc, MPR_TRACE,"SAS discovery start "
252                             "event\n");
253                 if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
254                         mpr_dprint(sc, MPR_TRACE,"SAS discovery stop event\n");
255                         sassc->flags &= ~MPRSAS_IN_DISCOVERY;
256                         mprsas_discovery_end(sassc);
257                 }
258                 break;
259         }
260         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
261         {
262                 Mpi2EventDataSasEnclDevStatusChange_t *data;
263                 data = (Mpi2EventDataSasEnclDevStatusChange_t *)
264                     fw_event->event_data;
265                 mpr_mapping_enclosure_dev_status_change_event(sc,
266                     fw_event->event_data);
267                 break;
268         }
269         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
270         {
271                 Mpi2EventIrConfigElement_t *element;
272                 int i;
273                 u8 foreign_config, reason;
274                 u16 elementType;
275                 Mpi2EventDataIrConfigChangeList_t *event_data;
276                 struct mprsas_target *targ;
277                 unsigned int id;
278
279                 event_data = fw_event->event_data;
280                 foreign_config = (le32toh(event_data->Flags) &
281                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
282
283                 element =
284                     (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
285                 id = mpr_mapping_get_raid_id_from_handle(sc,
286                     element->VolDevHandle);
287
288                 mpr_mapping_ir_config_change_event(sc, event_data);
289                 for (i = 0; i < event_data->NumElements; i++, element++) {
290                         reason = element->ReasonCode;
291                         elementType = le16toh(element->ElementFlags) &
292                             MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
293                         /*
294                          * check for element type of Phys Disk or Hot Spare
295                          */
296                         if ((elementType != 
297                             MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
298                             && (elementType !=
299                             MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
300                                 // do next element
301                                 goto skip_fp_send;
302
303                         /*
304                          * check for reason of Hide, Unhide, PD Created, or PD
305                          * Deleted
306                          */
307                         if ((reason != MPI2_EVENT_IR_CHANGE_RC_HIDE) &&
308                             (reason != MPI2_EVENT_IR_CHANGE_RC_UNHIDE) &&
309                             (reason != MPI2_EVENT_IR_CHANGE_RC_PD_CREATED) &&
310                             (reason != MPI2_EVENT_IR_CHANGE_RC_PD_DELETED))
311                                 goto skip_fp_send;
312
313                         // check for a reason of Hide or PD Created
314                         if ((reason == MPI2_EVENT_IR_CHANGE_RC_HIDE) ||
315                             (reason == MPI2_EVENT_IR_CHANGE_RC_PD_CREATED))
316                         {
317                                 // build RAID Action message
318                                 Mpi2RaidActionRequest_t *action;
319                                 Mpi2RaidActionReply_t *reply;
320                                 struct mpr_command *cm;
321                                 int error = 0;
322                                 if ((cm = mpr_alloc_command(sc)) == NULL) {
323                                         printf("%s: command alloc failed\n",
324                                             __func__);
325                                         return;
326                                 }
327
328                                 mpr_dprint(sc, MPR_INFO, "Sending FP action "
329                                     "from "
330                                     "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
331                                     ":\n");
332                                 action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
333                                 action->Function = MPI2_FUNCTION_RAID_ACTION;
334                                 action->Action =
335                                     MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
336                                 action->PhysDiskNum = element->PhysDiskNum;
337                                 cm->cm_desc.Default.RequestFlags =
338                                     MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
339                                 error = mpr_request_polled(sc, cm);
340                                 reply = (Mpi2RaidActionReply_t *)cm->cm_reply;
341                                 if (error || (reply == NULL)) {
342                                         /* FIXME */
343                                         /*
344                                          * If the poll returns error then we
345                                          * need to do diag reset
346                                          */
347                                         printf("%s: poll for page completed "
348                                             "with error %d", __func__, error);
349                                 }
350                                 if (reply && (le16toh(reply->IOCStatus) &
351                                     MPI2_IOCSTATUS_MASK) !=
352                                     MPI2_IOCSTATUS_SUCCESS) {
353                                         mpr_dprint(sc, MPR_INFO, "%s: error "
354                                             "sending RaidActionPage; iocstatus "
355                                             "= 0x%x\n", __func__,
356                                             le16toh(reply->IOCStatus));
357                                 }
358
359                                 if (cm)
360                                         mpr_free_command(sc, cm);
361                         }
362 skip_fp_send:
363                         mpr_dprint(sc, MPR_INFO, "Received "
364                             "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST Reason "
365                             "code %x:\n", element->ReasonCode);
366                         switch (element->ReasonCode) {
367                         case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
368                         case MPI2_EVENT_IR_CHANGE_RC_ADDED:
369                                 if (!foreign_config) {
370                                         if (mprsas_volume_add(sc,
371                                             le16toh(element->VolDevHandle))) {
372                                                 printf("%s: failed to add RAID "
373                                                     "volume with handle 0x%x\n",
374                                                     __func__, le16toh(element->
375                                                     VolDevHandle));
376                                         }
377                                 }
378                                 break;
379                         case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
380                         case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
381                                 /*
382                                  * Rescan after volume is deleted or removed.
383                                  */
384                                 if (!foreign_config) {
385                                         if (id == MPR_MAP_BAD_ID) {
386                                                 printf("%s: could not get ID "
387                                                     "for volume with handle "
388                                                     "0x%04x\n", __func__,
389                                                     le16toh(element->
390                                                     VolDevHandle));
391                                                 break;
392                                         }
393                                         
394                                         targ = &sassc->targets[id];
395                                         targ->handle = 0x0;
396                                         targ->encl_slot = 0x0;
397                                         targ->encl_handle = 0x0;
398                                         targ->encl_level_valid = 0x0;
399                                         targ->encl_level = 0x0;
400                                         targ->connector_name[0] = ' ';
401                                         targ->connector_name[1] = ' ';
402                                         targ->connector_name[2] = ' ';
403                                         targ->connector_name[3] = ' ';
404                                         targ->exp_dev_handle = 0x0;
405                                         targ->phy_num = 0x0;
406                                         targ->linkrate = 0x0;
407                                         mprsas_rescan_target(sc, targ);
408                                         printf("RAID target id 0x%x removed\n",
409                                             targ->tid);
410                                 }
411                                 break;
412                         case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
413                         case MPI2_EVENT_IR_CHANGE_RC_HIDE:
414                                 /*
415                                  * Phys Disk of a volume has been created.  Hide
416                                  * it from the OS.
417                                  */
418                                 targ = mprsas_find_target_by_handle(sassc, 0,
419                                     element->PhysDiskDevHandle);
420                                 if (targ == NULL) 
421                                         break;
422                                 targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
423                                 mprsas_rescan_target(sc, targ);
424                                 
425                                 break;
426                         case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
427                                 /*
428                                  * Phys Disk of a volume has been deleted.
429                                  * Expose it to the OS.
430                                  */
431                                 if (mprsas_add_device(sc,
432                                     le16toh(element->PhysDiskDevHandle), 0)) {
433                                         printf("%s: failed to add device with "
434                                             "handle 0x%x\n", __func__,
435                                             le16toh(element->
436                                             PhysDiskDevHandle));
437                                         mprsas_prepare_remove(sassc,
438                                             le16toh(element->
439                                             PhysDiskDevHandle));
440                                 }
441                                 break;
442                         }
443                 }
444                 /*
445                  * refcount was incremented for this event in
446                  * mprsas_evt_handler.  Decrement it here because the event has
447                  * been processed.
448                  */
449                 mprsas_startup_decrement(sassc);
450                 break;
451         }
452         case MPI2_EVENT_IR_VOLUME:
453         {
454                 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
455
456                 /*
457                  * Informational only.
458                  */
459                 mpr_dprint(sc, MPR_EVENT, "Received IR Volume event:\n");
460                 switch (event_data->ReasonCode) {
461                 case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
462                         mpr_dprint(sc, MPR_EVENT, "   Volume Settings "
463                             "changed from 0x%x to 0x%x for Volome with "
464                             "handle 0x%x", le32toh(event_data->PreviousValue),
465                             le32toh(event_data->NewValue),
466                             le16toh(event_data->VolDevHandle));
467                         break;
468                 case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
469                         mpr_dprint(sc, MPR_EVENT, "   Volume Status "
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_STATE_CHANGED:
476                         mpr_dprint(sc, MPR_EVENT, "   Volume State "
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                                 u32 state;
482                                 struct mprsas_target *targ;
483                                 state = le32toh(event_data->NewValue);
484                                 switch (state) {
485                                 case MPI2_RAID_VOL_STATE_MISSING:
486                                 case MPI2_RAID_VOL_STATE_FAILED:
487                                         mprsas_prepare_volume_remove(sassc,
488                                             event_data->VolDevHandle);
489                                         break;
490                  
491                                 case MPI2_RAID_VOL_STATE_ONLINE:
492                                 case MPI2_RAID_VOL_STATE_DEGRADED:
493                                 case MPI2_RAID_VOL_STATE_OPTIMAL:
494                                         targ =
495                                             mprsas_find_target_by_handle(sassc,
496                                             0, event_data->VolDevHandle);
497                                         if (targ) {
498                                                 printf("%s %d: Volume handle "
499                                                     "0x%x is already added \n",
500                                                     __func__, __LINE__,
501                                                     event_data->VolDevHandle);
502                                                 break;
503                                         }
504                                         if (mprsas_volume_add(sc,
505                                             le16toh(event_data->
506                                             VolDevHandle))) {
507                                                 printf("%s: failed to add RAID "
508                                                     "volume with handle 0x%x\n",
509                                                     __func__, le16toh(
510                                                     event_data->VolDevHandle));
511                                         }
512                                         break;
513                                 default:
514                                         break;
515                                 }
516                         break;
517                 default:
518                         break;
519                 }
520                 break;
521         }
522         case MPI2_EVENT_IR_PHYSICAL_DISK:
523         {
524                 Mpi2EventDataIrPhysicalDisk_t *event_data =
525                     fw_event->event_data;
526                 struct mprsas_target *targ;
527
528                 /*
529                  * Informational only.
530                  */
531                 mpr_dprint(sc, MPR_EVENT, "Received IR Phys Disk event:\n");
532                 switch (event_data->ReasonCode) {
533                 case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
534                         mpr_dprint(sc, MPR_EVENT, "   Phys Disk Settings "
535                             "changed from 0x%x to 0x%x for Phys Disk Number "
536                             "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
537                             "%d", le32toh(event_data->PreviousValue),
538                             le32toh(event_data->NewValue),
539                             event_data->PhysDiskNum,
540                             le16toh(event_data->PhysDiskDevHandle),
541                             le16toh(event_data->EnclosureHandle),
542                             le16toh(event_data->Slot));
543                         break;
544                 case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
545                         mpr_dprint(sc, MPR_EVENT, "   Phys Disk Status changed "
546                             "from 0x%x to 0x%x for Phys Disk Number %d and "
547                             "handle 0x%x at Enclosure handle 0x%x, Slot %d",
548                             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_STATE_CHANGED:
556                         mpr_dprint(sc, MPR_EVENT, "   Phys Disk State 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                         switch (event_data->NewValue) {
566                                 case MPI2_RAID_PD_STATE_ONLINE:
567                                 case MPI2_RAID_PD_STATE_DEGRADED:
568                                 case MPI2_RAID_PD_STATE_REBUILDING:
569                                 case MPI2_RAID_PD_STATE_OPTIMAL:
570                                 case MPI2_RAID_PD_STATE_HOT_SPARE:
571                                         targ = mprsas_find_target_by_handle(
572                                             sassc, 0,
573                                             event_data->PhysDiskDevHandle);
574                                         if (targ) {
575                                                 targ->flags |=
576                                                     MPR_TARGET_FLAGS_RAID_COMPONENT;
577                                                 printf("%s %d: Found Target "
578                                                     "for handle 0x%x.\n", 
579                                                     __func__, __LINE__ ,
580                                                     event_data->
581                                                     PhysDiskDevHandle);
582                                         }
583                                 break;
584                                 case MPI2_RAID_PD_STATE_OFFLINE:
585                                 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
586                                 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
587                                 default:
588                                         targ = mprsas_find_target_by_handle(
589                                             sassc, 0,
590                                             event_data->PhysDiskDevHandle);
591                                         if (targ) {
592                                                 targ->flags |=
593                                             ~MPR_TARGET_FLAGS_RAID_COMPONENT;
594                                                 printf("%s %d: Found Target "
595                                                     "for handle 0x%x.  \n",
596                                                     __func__, __LINE__ ,
597                                                     event_data->
598                                                     PhysDiskDevHandle);
599                                         }
600                                 break;
601                         }
602                 default:
603                         break;
604                 }
605                 break;
606         }
607         case MPI2_EVENT_IR_OPERATION_STATUS:
608         {
609                 Mpi2EventDataIrOperationStatus_t *event_data =
610                     fw_event->event_data;
611
612                 /*
613                  * Informational only.
614                  */
615                 mpr_dprint(sc, MPR_EVENT, "Received IR Op Status event:\n");
616                 mpr_dprint(sc, MPR_EVENT, "   RAID Operation of %d is %d "
617                     "percent complete for Volume with handle 0x%x",
618                     event_data->RAIDOperation, event_data->PercentComplete,
619                     le16toh(event_data->VolDevHandle));
620                 break;
621         }
622         case MPI2_EVENT_TEMP_THRESHOLD:
623         {
624                 pMpi2EventDataTemperature_t     temp_event;
625
626                 temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
627
628                 /*
629                  * The Temp Sensor Count must be greater than the event's Sensor
630                  * Num to be valid.  If valid, print the temp thresholds that
631                  * have been exceeded.
632                  */
633                 if (sc->iounit_pg8.NumSensors > temp_event->SensorNum) {
634                         mpr_dprint(sc, MPR_FAULT, "Temperature Threshold flags "
635                             "%s %s %s %s exceeded for Sensor: %d !!!\n",
636                             ((temp_event->Status & 0x01) == 1) ? "0 " : " ",
637                             ((temp_event->Status & 0x02) == 2) ? "1 " : " ",
638                             ((temp_event->Status & 0x04) == 4) ? "2 " : " ",
639                             ((temp_event->Status & 0x08) == 8) ? "3 " : " ",
640                             temp_event->SensorNum);
641                         mpr_dprint(sc, MPR_FAULT, "Current Temp in Celsius: "
642                             "%d\n", temp_event->CurrentTemperature);
643                 }
644                 break;
645         }
646         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
647         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
648         default:
649                 mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
650                     fw_event->event);
651                 break;
652
653         }
654         mpr_dprint(sc, MPR_EVENT, "(%d)->(%s) Event Free: [%x]\n", event_count,
655             __func__, fw_event->event);
656         mprsas_fw_event_free(sc, fw_event);
657 }
658
659 void
660 mprsas_firmware_event_work(void *arg, int pending)
661 {
662         struct mpr_fw_event_work *fw_event;
663         struct mpr_softc *sc;
664
665         sc = (struct mpr_softc *)arg;
666         mpr_lock(sc);
667         while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
668                 TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
669                 mprsas_fw_work(sc, fw_event);
670         }
671         mpr_unlock(sc);
672 }
673
674 static int
675 mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate){
676         char devstring[80];
677         struct mprsas_softc *sassc;
678         struct mprsas_target *targ;
679         Mpi2ConfigReply_t mpi_reply;
680         Mpi2SasDevicePage0_t config_page;
681         uint64_t sas_address, sata_sas_address;
682         uint64_t parent_sas_address = 0;
683         u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
684         u32 device_info, parent_devinfo = 0;
685         unsigned int id;
686         int ret;
687         int error = 0;
688         struct mprsas_lun *lun;
689
690         sassc = sc->sassc;
691         mprsas_startup_increment(sassc);
692         if ((mpr_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
693              MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
694                 printf("%s: error reading SAS device page0\n", __func__);
695                 error = ENXIO;
696                 goto out;
697         }
698
699         device_info = le32toh(config_page.DeviceInfo);
700
701         if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
702             && (le16toh(config_page.ParentDevHandle) != 0)) {
703                 Mpi2ConfigReply_t tmp_mpi_reply;
704                 Mpi2SasDevicePage0_t parent_config_page;
705
706                 if ((mpr_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
707                      &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
708                      le16toh(config_page.ParentDevHandle)))) {
709                         printf("%s: error reading SAS device %#x page0\n",
710                             __func__, le16toh(config_page.ParentDevHandle));
711                 } else {
712                         parent_sas_address = parent_config_page.SASAddress.High;
713                         parent_sas_address = (parent_sas_address << 32) |
714                             parent_config_page.SASAddress.Low;
715                         parent_devinfo = le32toh(parent_config_page.DeviceInfo);
716                 }
717         }
718         /* TODO Check proper endianess */
719         sas_address = config_page.SASAddress.High;
720         sas_address = (sas_address << 32) |
721             config_page.SASAddress.Low;
722
723         if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE)
724             == MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
725                 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
726                         ret = mprsas_get_sas_address_for_sata_disk(sc,
727                             &sata_sas_address, handle, device_info);
728                         if (!ret)
729                                 id = mpr_mapping_get_sas_id(sc,
730                                     sata_sas_address, handle);
731                         else
732                                 id = mpr_mapping_get_sas_id(sc,
733                                     sas_address, handle);
734                 } else
735                         id = mpr_mapping_get_sas_id(sc, sas_address,
736                             handle);
737         } else
738                 id = mpr_mapping_get_sas_id(sc, sas_address, handle);
739
740         if (id == MPR_MAP_BAD_ID) {
741                 printf("failure at %s:%d/%s()! Could not get ID for device "
742                     "with handle 0x%04x\n", __FILE__, __LINE__, __func__,
743                     handle);
744                 error = ENXIO;
745                 goto out;
746         }
747
748         if (mprsas_check_id(sassc, id) != 0) {
749                 device_printf(sc->mpr_dev, "Excluding target id %d\n", id);
750                 error = ENXIO;
751                 goto out;
752         }
753  
754         mpr_dprint(sc, MPR_MAPPING, "SAS Address from SAS device page0 = %jx\n",
755             sas_address);
756         targ = &sassc->targets[id];
757         targ->devinfo = device_info;
758         targ->devname = le32toh(config_page.DeviceName.High);
759         targ->devname = (targ->devname << 32) | 
760             le32toh(config_page.DeviceName.Low);
761         targ->encl_handle = le16toh(config_page.EnclosureHandle);
762         targ->encl_slot = le16toh(config_page.Slot);
763         targ->encl_level = config_page.EnclosureLevel;
764         targ->connector_name[0] = config_page.ConnectorName[0];
765         targ->connector_name[1] = config_page.ConnectorName[1];
766         targ->connector_name[2] = config_page.ConnectorName[2];
767         targ->connector_name[3] = config_page.ConnectorName[3];
768         targ->handle = handle;
769         targ->parent_handle = le16toh(config_page.ParentDevHandle);
770         targ->sasaddr = mpr_to_u64(&config_page.SASAddress);
771         targ->parent_sasaddr = le64toh(parent_sas_address);
772         targ->parent_devinfo = parent_devinfo;
773         targ->tid = id;
774         targ->linkrate = (linkrate>>4);
775         targ->flags = 0;
776         if (le16toh(config_page.Flags) &
777             MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) {
778                 targ->scsi_req_desc_type =
779                     MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
780         }
781         if (le16toh(config_page.Flags) &
782             MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
783                 targ->encl_level_valid = TRUE;
784         }
785         TAILQ_INIT(&targ->commands);
786         TAILQ_INIT(&targ->timedout_commands);
787         while (!SLIST_EMPTY(&targ->luns)) {
788                 lun = SLIST_FIRST(&targ->luns);
789                 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
790                 free(lun, M_MPR);
791         }
792         SLIST_INIT(&targ->luns);
793
794         mpr_describe_devinfo(targ->devinfo, devstring, 80);
795         mpr_dprint(sc, (MPR_XINFO|MPR_MAPPING), "Found device <%s> <%s> "
796             "handle<0x%04x> enclosureHandle<0x%04x> slot %d\n", devstring,
797             mpr_describe_table(mpr_linkrate_names, targ->linkrate),
798             targ->handle, targ->encl_handle, targ->encl_slot);
799         if (targ->encl_level_valid) {
800                 mpr_dprint(sc, (MPR_XINFO|MPR_MAPPING), "At enclosure level %d "
801                     "and connector name (%4s)\n", targ->encl_level,
802                     targ->connector_name);
803         }
804 #if __FreeBSD_version < 1000039
805         if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
806 #endif
807                 mprsas_rescan_target(sc, targ);
808         mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
809 out:
810         mprsas_startup_decrement(sassc);
811         return (error);
812         
813 }
814         
815 int
816 mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
817     u64 *sas_address, u16 handle, u32 device_info)
818 {
819         Mpi2SataPassthroughReply_t mpi_reply;
820         int i, rc, try_count;
821         u32 *bufferptr;
822         union _sata_sas_address hash_address;
823         struct _ata_identify_device_data ata_identify;
824         u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
825         u32 ioc_status;
826         u8 sas_status;
827
828         memset(&ata_identify, 0, sizeof(ata_identify));
829         try_count = 0;
830         do {
831                 rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
832                     (char *)&ata_identify, sizeof(ata_identify), device_info);
833                 try_count++;
834                 ioc_status = le16toh(mpi_reply.IOCStatus)
835                     & MPI2_IOCSTATUS_MASK;
836                 sas_status = mpi_reply.SASStatus;
837         } while ((rc == -EAGAIN || ioc_status || sas_status) &&
838             (try_count < 5));
839
840         if (rc == 0 && !ioc_status && !sas_status) {
841                 mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
842                     "successfully for handle = 0x%x with try_count = %d\n",
843                     __func__, handle, try_count);
844         } else {
845                 mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
846                     __func__, handle);
847                 return -1;
848         }
849         /* Copy & byteswap the 40 byte model number to a buffer */
850         for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
851                 buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
852                 buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
853         }
854         /* Copy & byteswap the 20 byte serial number to a buffer */
855         for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
856                 buffer[MPT2SAS_MN_LEN + i] =
857                     ((u8 *)ata_identify.serial_number)[i + 1];
858                 buffer[MPT2SAS_MN_LEN + i + 1] =
859                     ((u8 *)ata_identify.serial_number)[i];
860         }
861         bufferptr = (u32 *)buffer;
862         /* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
863          * so loop through the first 56 bytes (7*8),
864          * and then add in the last dword.
865          */
866         hash_address.word.low  = 0;
867         hash_address.word.high = 0;
868         for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
869                 hash_address.word.low += *bufferptr;
870                 bufferptr++;
871                 hash_address.word.high += *bufferptr;
872                 bufferptr++;
873         }
874         /* Add the last dword */
875         hash_address.word.low += *bufferptr;
876         /* Make sure the hash doesn't start with 5, because it could clash
877          * with a SAS address. Change 5 to a D.
878          */
879         if ((hash_address.word.high & 0x000000F0) == (0x00000050))
880                 hash_address.word.high |= 0x00000080;
881         *sas_address = (u64)hash_address.wwid[0] << 56 |
882             (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
883             (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
884             (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
885             (u64)hash_address.wwid[7];
886         return 0;
887 }
888
889 static int
890 mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
891     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
892 {
893         Mpi2SataPassthroughRequest_t *mpi_request;
894         Mpi2SataPassthroughReply_t *reply;
895         struct mpr_command *cm;
896         char *buffer;
897         int error = 0;
898
899         buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
900         if (!buffer)
901                 return ENOMEM;
902
903         if ((cm = mpr_alloc_command(sc)) == NULL) {
904                 free(buffer, M_MPR);
905                 return (EBUSY);
906         }
907         mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
908         bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
909         mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
910         mpi_request->VF_ID = 0;
911         mpi_request->DevHandle = htole16(handle);
912         mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
913             MPI2_SATA_PT_REQ_PT_FLAGS_READ);
914         mpi_request->DataLength = htole32(sz);
915         mpi_request->CommandFIS[0] = 0x27;
916         mpi_request->CommandFIS[1] = 0x80;
917         mpi_request->CommandFIS[2] =  (devinfo &
918             MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
919         cm->cm_sge = &mpi_request->SGL;
920         cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
921         cm->cm_flags = MPR_CM_FLAGS_DATAIN;
922         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
923         cm->cm_data = buffer;
924         cm->cm_length = htole32(sz);
925         error = mpr_wait_command(sc, cm, 60, CAN_SLEEP);
926         reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
927         if (error || (reply == NULL)) {
928                 /* FIXME */
929                 /*
930                  * If the request returns an error then we need to do a diag
931                  * reset
932                  */ 
933                 printf("%s: request for page completed with error %d",
934                     __func__, error);
935                 error = ENXIO;
936                 goto out;
937         }
938         bcopy(buffer, id_buffer, sz);
939         bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
940         if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
941             MPI2_IOCSTATUS_SUCCESS) {
942                 printf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n",
943                     __func__, reply->IOCStatus);
944                 error = ENXIO;
945                 goto out;
946         }
947 out:
948         mpr_free_command(sc, cm);
949         free(buffer, M_MPR);    
950         return (error);
951 }
952
953 static int
954 mprsas_volume_add(struct mpr_softc *sc, u16 handle)
955 {
956         struct mprsas_softc *sassc;
957         struct mprsas_target *targ;
958         u64 wwid;
959         unsigned int id;
960         int error = 0;
961         struct mprsas_lun *lun;
962
963         sassc = sc->sassc;
964         mprsas_startup_increment(sassc);
965         /* wwid is endian safe */
966         mpr_config_get_volume_wwid(sc, handle, &wwid);
967         if (!wwid) {
968                 printf("%s: invalid WWID; cannot add volume to mapping table\n",
969                     __func__);
970                 error = ENXIO;
971                 goto out;
972         }
973
974         id = mpr_mapping_get_raid_id(sc, wwid, handle);
975         if (id == MPR_MAP_BAD_ID) {
976                 printf("%s: could not get ID for volume with handle 0x%04x and "
977                     "WWID 0x%016llx\n", __func__, handle,
978                     (unsigned long long)wwid);
979                 error = ENXIO;
980                 goto out;
981         }
982
983         targ = &sassc->targets[id];
984         targ->tid = id;
985         targ->handle = handle;
986         targ->devname = wwid;
987         TAILQ_INIT(&targ->commands);
988         TAILQ_INIT(&targ->timedout_commands);
989         while (!SLIST_EMPTY(&targ->luns)) {
990                 lun = SLIST_FIRST(&targ->luns);
991                 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
992                 free(lun, M_MPR);
993         }
994         SLIST_INIT(&targ->luns);
995 #if __FreeBSD_version < 1000039
996         if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
997 #endif
998                 mprsas_rescan_target(sc, targ);
999         mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1000             targ->tid, wwid);
1001 out:
1002         mprsas_startup_decrement(sassc);
1003         return (error);
1004 }
1005
1006 /**
1007  * mprsas_SSU_to_SATA_devices 
1008  * @sc: per adapter object
1009  *
1010  * Looks through the target list and issues a StartStopUnit SCSI command to each
1011  * SATA direct-access device.  This helps to ensure that data corruption is
1012  * avoided when the system is being shut down.  This must be called after the IR
1013  * System Shutdown RAID Action is sent if in IR mode.
1014  *
1015  * Return nothing.
1016  */
1017 static void
1018 mprsas_SSU_to_SATA_devices(struct mpr_softc *sc)
1019 {
1020         struct mprsas_softc *sassc = sc->sassc;
1021         union ccb *ccb;
1022         path_id_t pathid = cam_sim_path(sassc->sim);
1023         target_id_t targetid;
1024         struct mprsas_target *target;
1025         struct mprsas_lun *lun;
1026         char path_str[64];
1027         struct timeval cur_time, start_time;
1028
1029         /*
1030          * For each LUN of each target, issue a StartStopUnit command to stop
1031          * the device.
1032          */
1033         sc->SSU_started = TRUE;
1034         sc->SSU_refcount = 0;
1035         for (targetid = 0; targetid < sc->facts->MaxTargets; targetid++) {
1036                 target = &sassc->targets[targetid];
1037                 if (target->handle == 0x0) {
1038                         continue;
1039                 }
1040
1041                 SLIST_FOREACH(lun, &target->luns, lun_link) {
1042                         ccb = xpt_alloc_ccb_nowait();
1043                         if (ccb == NULL) {
1044                                 mpr_dprint(sc, MPR_FAULT, "Unable to alloc "
1045                                     "CCB to stop unit.\n");
1046                                 return;
1047                         }
1048
1049                         /*
1050                          * The stop_at_shutdown flag will be set if this LUN is
1051                          * a SATA direct-access end device.
1052                          */
1053                         if (lun->stop_at_shutdown) {
1054                                 if (xpt_create_path(&ccb->ccb_h.path,
1055                                     xpt_periph, pathid, targetid,
1056                                     lun->lun_id) != CAM_REQ_CMP) {
1057                                         mpr_dprint(sc, MPR_FAULT, "Unable to "
1058                                             "create LUN path to stop unit.\n");
1059                                         xpt_free_ccb(ccb);
1060                                         return;
1061                                 }
1062                                 xpt_path_string(ccb->ccb_h.path, path_str,
1063                                     sizeof(path_str));
1064
1065                                 mpr_dprint(sc, MPR_INFO, "Sending StopUnit: "
1066                                     "path %s handle %d\n", path_str,
1067                                     target->handle);
1068                         
1069                                 /*
1070                                  * Issue a START STOP UNIT command for the LUN.
1071                                  * Increment the SSU counter to be used to
1072                                  * count the number of required replies.
1073                                  */
1074                                 mpr_dprint(sc, MPR_INFO, "Incrementing SSU "
1075                                     "count\n");
1076                                 sc->SSU_refcount++;
1077                                 ccb->ccb_h.target_id =
1078                                     xpt_path_target_id(ccb->ccb_h.path);
1079                                 ccb->ccb_h.target_lun = lun->lun_id;
1080                                 ccb->ccb_h.ppriv_ptr1 = sassc;
1081                                 scsi_start_stop(&ccb->csio,
1082                                     /*retries*/0,
1083                                     mprsas_stop_unit_done,
1084                                     MSG_SIMPLE_Q_TAG,
1085                                     /*start*/FALSE,
1086                                     /*load/eject*/0,
1087                                     /*immediate*/FALSE,
1088                                     MPR_SENSE_LEN,
1089                                     /*timeout*/10000);
1090                                 xpt_action(ccb);
1091                         }
1092                 }
1093         }
1094
1095         /*
1096          * Wait until all of the SSU commands have completed or time has
1097          * expired (60 seconds).  pause for 100ms each time through.  If any
1098          * command times out, the target will be reset in the SCSI command
1099          * timeout routine.
1100          */
1101         getmicrotime(&start_time);
1102         while (sc->SSU_refcount) {
1103                 pause("mprwait", hz/10);
1104                 
1105                 getmicrotime(&cur_time);
1106                 if ((cur_time.tv_sec - start_time.tv_sec) > 60) {
1107                         mpr_dprint(sc, MPR_FAULT, "Time has expired waiting "
1108                             "for SSU commands to complete.\n");
1109                         break;
1110                 }
1111         }
1112 }
1113
1114 static void
1115 mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1116 {
1117         struct mprsas_softc *sassc;
1118         char path_str[64];
1119
1120         sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1121
1122         xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1123         mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1124             path_str);
1125
1126         if (done_ccb == NULL)
1127                 return;
1128
1129         /*
1130          * Nothing more to do except free the CCB and path.  If the command
1131          * timed out, an abort reset, then target reset will be issued during
1132          * the SCSI Command process.
1133          */
1134         xpt_free_path(done_ccb->ccb_h.path);
1135         xpt_free_ccb(done_ccb);
1136 }
1137
1138 /**
1139  * mprsas_ir_shutdown - IR shutdown notification
1140  * @sc: per adapter object
1141  *
1142  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1143  * the host system is shutting down.
1144  *
1145  * Return nothing.
1146  */
1147 void
1148 mprsas_ir_shutdown(struct mpr_softc *sc)
1149 {
1150         u16 volume_mapping_flags;
1151         u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1152         struct dev_mapping_table *mt_entry;
1153         u32 start_idx, end_idx;
1154         unsigned int id, found_volume = 0;
1155         struct mpr_command *cm;
1156         Mpi2RaidActionRequest_t *action;
1157
1158         mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1159
1160         /* is IR firmware build loaded? */
1161         if (!sc->ir_firmware)
1162                 goto out;
1163
1164         /* are there any volumes?  Look at IR target IDs. */
1165         // TODO-later, this should be looked up in the RAID config structure
1166         // when it is implemented.
1167         volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1168             MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1169         if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1170                 start_idx = 0;
1171                 if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1172                         start_idx = 1;
1173         } else
1174                 start_idx = sc->max_devices - sc->max_volumes;
1175         end_idx = start_idx + sc->max_volumes - 1;
1176
1177         for (id = start_idx; id < end_idx; id++) {
1178                 mt_entry = &sc->mapping_table[id];
1179                 if ((mt_entry->physical_id != 0) &&
1180                     (mt_entry->missing_count == 0)) {
1181                         found_volume = 1;
1182                         break;
1183                 }
1184         }
1185
1186         if (!found_volume)
1187                 goto out;
1188
1189         if ((cm = mpr_alloc_command(sc)) == NULL) {
1190                 printf("%s: command alloc failed\n", __func__);
1191                 goto out;
1192         }
1193
1194         action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1195         action->Function = MPI2_FUNCTION_RAID_ACTION;
1196         action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1197         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1198         mpr_lock(sc);
1199         mpr_wait_command(sc, cm, 5, CAN_SLEEP);
1200         mpr_unlock(sc);
1201
1202         /*
1203          * Don't check for reply, just leave.
1204          */
1205         if (cm)
1206                 mpr_free_command(sc, cm);
1207
1208 out:
1209         mprsas_SSU_to_SATA_devices(sc);
1210 }