kernel: Add D_MPSAFE to the ops of mfi(4), mrsas(4) and twa(4).
[dragonfly.git] / sys / dev / raid / mps / mps_sas_lsi.c
1 /*-
2  * Copyright (c) 2011 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  * $FreeBSD: src/sys/dev/mps/mps_sas_lsi.c,v 1.1 2012/01/26 18:17:21 ken Exp $
29  */
30
31 /* Communications core for LSI MPT2 */
32
33 /* TODO Move headers to mpsvar */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/bio.h>
43 #include <sys/malloc.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46 #include <sys/endian.h>
47 #include <sys/queue.h>
48 #include <sys/kthread.h>
49 #include <sys/taskqueue.h>
50 #include <sys/sbuf.h>
51
52 #include <sys/rman.h>
53
54 #include <machine/stdarg.h>
55
56 #include <bus/cam/cam.h>
57 #include <bus/cam/cam_ccb.h>
58 #include <bus/cam/cam_debug.h>
59 #include <bus/cam/cam_sim.h>
60 #include <bus/cam/cam_xpt_sim.h>
61 #include <bus/cam/cam_xpt_periph.h>
62 #include <bus/cam/cam_periph.h>
63 #include <bus/cam/scsi/scsi_all.h>
64 #include <bus/cam/scsi/scsi_message.h>
65
66 #include <dev/raid/mps/mpi/mpi2_type.h>
67 #include <dev/raid/mps/mpi/mpi2.h>
68 #include <dev/raid/mps/mpi/mpi2_ioc.h>
69 #include <dev/raid/mps/mpi/mpi2_sas.h>
70 #include <dev/raid/mps/mpi/mpi2_cnfg.h>
71 #include <dev/raid/mps/mpi/mpi2_init.h>
72 #include <dev/raid/mps/mpi/mpi2_raid.h>
73 #include <dev/raid/mps/mpi/mpi2_tool.h>
74 #include <dev/raid/mps/mps_ioctl.h>
75 #include <dev/raid/mps/mpsvar.h>
76 #include <dev/raid/mps/mps_table.h>
77 #include <dev/raid/mps/mps_sas.h>
78
79 /* For Hashed SAS Address creation for SATA Drives */
80 #define MPT2SAS_SN_LEN 20
81 #define MPT2SAS_MN_LEN 40
82
83 struct mps_fw_event_work {
84         u16                     event;
85         void                    *event_data;
86         TAILQ_ENTRY(mps_fw_event_work)  ev_link;
87 };
88
89 union _sata_sas_address {
90         u8 wwid[8];
91         struct {
92                 u32 high;
93                 u32 low;
94         } word;
95 };
96
97 /*
98  * define the IDENTIFY DEVICE structure
99  */
100 struct _ata_identify_device_data {
101         u16 reserved1[10];      /* 0-9 */
102         u16 serial_number[10];  /* 10-19 */
103         u16 reserved2[7];       /* 20-26 */
104         u16 model_number[20];   /* 27-46*/
105         u16 reserved3[209];     /* 47-255*/
106 };
107
108 static u32 event_count;
109
110 static void mpssas_fw_work(struct mps_softc *sc,
111     struct mps_fw_event_work *fw_event);
112 static void mpssas_fw_event_free(struct mps_softc *,
113     struct mps_fw_event_work *);
114 static int mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate);
115 static int mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
116     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz,
117     u32 devinfo);
118 int mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
119     u64 *sas_address, u16 handle, u32 device_info);
120 static int mpssas_volume_add(struct mps_softc *sc,
121     u16 handle);
122
123 void
124 mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
125     MPI2_EVENT_NOTIFICATION_REPLY *event)
126 {
127         struct mps_fw_event_work *fw_event;
128         u16 sz;
129
130         mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
131         mps_print_evt_sas(sc, event);
132         mpssas_record_event(sc, event);
133
134         fw_event = kmalloc(sizeof(struct mps_fw_event_work), M_MPT2,
135              M_ZERO|M_INTWAIT);
136         if (!fw_event) {
137                 kprintf("%s: allocate failed for fw_event\n", __func__);
138                 return;
139         }
140         sz = le16toh(event->EventDataLength) * 4;
141         fw_event->event_data = kmalloc(sz, M_MPT2, M_ZERO|M_INTWAIT);
142         if (!fw_event->event_data) {
143                 kprintf("%s: allocate failed for event_data\n", __func__);
144                 kfree(fw_event, M_MPT2);
145                 return;
146         }
147
148         bcopy(event->EventData, fw_event->event_data, sz);
149         fw_event->event = event->Event;
150         if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
151             event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE ||
152             event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
153             sc->track_mapping_events)
154                 sc->pending_map_events++;
155
156         /*
157          * When wait_for_port_enable flag is set, make sure that all the events
158          * are processed. Increment the startup_refcount and decrement it after
159          * events are processed.
160          */
161         if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
162             event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) &&
163             sc->wait_for_port_enable)
164                 mpssas_startup_increment(sc->sassc);
165
166         TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
167         taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
168
169 }
170
171 static void
172 mpssas_fw_event_free(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
173 {
174
175         kfree(fw_event->event_data, M_MPT2);
176         kfree(fw_event, M_MPT2);
177 }
178
179 /**
180  * _mps_fw_work - delayed task for processing firmware events
181  * @sc: per adapter object
182  * @fw_event: The fw_event_work object
183  * Context: user.
184  *
185  * Return nothing.
186  */
187 static void
188 mpssas_fw_work(struct mps_softc *sc, struct mps_fw_event_work *fw_event)
189 {
190         struct mpssas_softc *sassc;
191         sassc = sc->sassc;
192
193         mps_dprint(sc, MPS_INFO, "(%d)->(%s) Working on  Event: [%x]\n",
194                    event_count++,__func__,fw_event->event);
195
196         switch (fw_event->event) {
197         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
198         {
199                 MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
200                 MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
201                 int i;
202
203                 data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
204                     fw_event->event_data;
205
206                 mps_mapping_topology_change_event(sc, fw_event->event_data);
207
208                 for (i = 0; i < data->NumEntries; i++) {
209                         phy = &data->PHY[i];
210                         switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
211                         case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
212                                 if (mpssas_add_device(sc,
213                                     phy->AttachedDevHandle, phy->LinkRate)){
214                                         kprintf("%s: failed to add device with "
215                                             "handle 0x%x\n", __func__,
216                                             phy->AttachedDevHandle);
217                                         mpssas_prepare_remove(sassc, phy->
218                                             AttachedDevHandle);
219                                 }
220                                 break;
221                         case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
222                                 mpssas_prepare_remove(sassc, phy->
223                                     AttachedDevHandle);
224                                 break;
225                         case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
226                         case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
227                         case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
228                         default:
229                                 break;
230                         }
231                 }
232                 /*
233                  * refcount was incremented for this event in
234                  * mpssas_evt_handler.  Decrement it here because the event has
235                  * been processed.
236                  */
237                 mpssas_startup_decrement(sassc);
238                 break;
239         }
240         case MPI2_EVENT_SAS_DISCOVERY:
241         {
242                 MPI2_EVENT_DATA_SAS_DISCOVERY *data;
243
244                 data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
245
246                 if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
247                         mps_dprint(sc, MPS_TRACE,"SAS discovery start event\n");
248                 if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
249                         mps_dprint(sc, MPS_TRACE,"SAS discovery stop event\n");
250                         sassc->flags &= ~MPSSAS_IN_DISCOVERY;
251                         mpssas_discovery_end(sassc);
252                 }
253                 break;
254         }
255         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
256         {
257                 Mpi2EventDataSasEnclDevStatusChange_t *data;
258                 data = (Mpi2EventDataSasEnclDevStatusChange_t *)
259                     fw_event->event_data;
260                 mps_mapping_enclosure_dev_status_change_event(sc, data);
261                 break;
262         }
263         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
264         {
265                 Mpi2EventIrConfigElement_t *element;
266                 int i;
267                 u8 foreign_config;
268                 Mpi2EventDataIrConfigChangeList_t *event_data;
269                 struct mpssas_target *targ;
270                 unsigned int id;
271
272                 event_data = fw_event->event_data;
273                 foreign_config = (le32toh(event_data->Flags) &
274                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
275
276                 element =
277                     (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
278                 id = mps_mapping_get_raid_id_from_handle
279                     (sc, element->VolDevHandle);
280
281                 mps_mapping_ir_config_change_event(sc, event_data);
282
283                 for (i = 0; i < event_data->NumElements; i++, element++) {
284                         switch (element->ReasonCode) {
285                         case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
286                         case MPI2_EVENT_IR_CHANGE_RC_ADDED:
287                                 if (!foreign_config) {
288                                         if (mpssas_volume_add(sc, le16toh(element->VolDevHandle))) {
289                                                 kprintf("%s: failed to add RAID "
290                                                     "volume with handle 0x%x\n",
291                                                     __func__, le16toh(element->
292                                                     VolDevHandle));
293                                         }
294                                 }
295                                 break;
296                         case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
297                         case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
298                                 /*
299                                  * Rescan after volume is deleted or removed.
300                                  */
301                                 if (!foreign_config) {
302                                         if (id == MPS_MAP_BAD_ID) {
303                                                 kprintf("%s: could not get ID "
304                                                     "for volume with handle "
305                                                     "0x%04x\n", __func__,
306                                                     element->VolDevHandle);
307                                                 break;
308                                         }
309
310                                         targ = &sassc->targets[id];
311                                         targ->handle = 0x0;
312                                         targ->encl_slot = 0x0;
313                                         targ->encl_handle = 0x0;
314                                         targ->exp_dev_handle = 0x0;
315                                         targ->phy_num = 0x0;
316                                         targ->linkrate = 0x0;
317                                         mpssas_rescan_target(sc, targ);
318                                         kprintf("RAID target id 0x%x removed\n",
319                                             targ->tid);
320                                 }
321                                 break;
322                         case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
323                         case MPI2_EVENT_IR_CHANGE_RC_HIDE:
324                                 /*
325                                  * Phys Disk of a volume has been created.  Hide
326                                  * it from the OS.
327                                  */
328                                 targ = mpssas_find_target_by_handle(sassc, 0, element->PhysDiskDevHandle);
329                                 if (targ == NULL)
330                                         break;
331                                 targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
332                                 mpssas_rescan_target(sc, targ);
333                                 break;
334                         case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
335                                 /*
336                                  * Phys Disk of a volume has been deleted.
337                                  * Expose it to the OS.
338                                  */
339                                 if (mpssas_add_device(sc,
340                                     element->PhysDiskDevHandle, 0)){
341                                         kprintf("%s: failed to add device with "
342                                             "handle 0x%x\n", __func__,
343                                             element->PhysDiskDevHandle);
344                                         mpssas_prepare_remove(sassc, element->
345                                             PhysDiskDevHandle);
346                                 }
347                                 break;
348                         }
349                 }
350                 /*
351                  * refcount was incremented for this event in
352                  * mpssas_evt_handler.  Decrement it here because the event has
353                  * been processed.
354                  */
355                 mpssas_startup_decrement(sassc);
356                 break;
357         }
358         case MPI2_EVENT_IR_VOLUME:
359         {
360                 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
361
362                 /*
363                  * Informational only.
364                  */
365                 mps_dprint(sc, MPS_INFO, "Received IR Volume event:\n");
366                 switch (event_data->ReasonCode) {
367                 case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
368                         mps_dprint(sc, MPS_INFO, "   Volume Settings "
369                             "changed from 0x%x to 0x%x for Volome with "
370                             "handle 0x%x", event_data->PreviousValue,
371                             event_data->NewValue,
372                             event_data->VolDevHandle);
373                         break;
374                 case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
375                         mps_dprint(sc, MPS_INFO, "   Volume Status "
376                             "changed from 0x%x to 0x%x for Volome with "
377                             "handle 0x%x", event_data->PreviousValue,
378                             event_data->NewValue,
379                             event_data->VolDevHandle);
380                         break;
381                 case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
382                         mps_dprint(sc, MPS_INFO, "   Volume State "
383                             "changed from 0x%x to 0x%x for Volome with "
384                             "handle 0x%x", event_data->PreviousValue,
385                             event_data->NewValue,
386                             event_data->VolDevHandle);
387                         u32 state;
388                         struct mpssas_target *targ;
389
390                         state = le32toh(event_data->NewValue);
391                         switch (state) {
392                         case MPI2_RAID_VOL_STATE_MISSING:
393                         case MPI2_RAID_VOL_STATE_FAILED:
394                                 mpssas_prepare_volume_remove(sassc,
395                                                              event_data->VolDevHandle);
396                                 break;
397                         case MPI2_RAID_VOL_STATE_ONLINE:
398                         case MPI2_RAID_VOL_STATE_DEGRADED:
399                         case MPI2_RAID_VOL_STATE_OPTIMAL:
400                                 targ = mpssas_find_target_by_handle(sassc, 0, event_data->VolDevHandle);
401                                 if (targ) {
402                                         kprintf("%s %d: Volume handle 0x%x is already added \n",
403                                                 __func__, __LINE__ , event_data->VolDevHandle);
404                                         break;
405                                 }
406                                 if (mpssas_volume_add(sc, le16toh(event_data->VolDevHandle))) {
407                                         kprintf("%s: failed to add RAID "
408                                                 "volume with handle 0x%x\n",
409                                                 __func__, le16toh(event_data->
410                                                 VolDevHandle));
411                                 }
412                                 break;
413                         default:
414                                 break;
415                         }
416                         break;
417                 default:
418                         break;
419                 }
420                 break;
421         }
422         case MPI2_EVENT_IR_PHYSICAL_DISK:
423         {
424                 Mpi2EventDataIrPhysicalDisk_t *event_data =
425                     fw_event->event_data;
426                 struct mpssas_target *targ;
427
428                 /*
429                  * Informational only.
430                  */
431                 mps_dprint(sc, MPS_INFO, "Received IR Phys Disk event:\n");
432                 switch (event_data->ReasonCode) {
433                 case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
434                         mps_dprint(sc, MPS_INFO, "   Phys Disk Settings "
435                             "changed from 0x%x to 0x%x for Phys Disk Number "
436                             "%d and handle 0x%x at Enclosure handle 0x%x, Slot "
437                             "%d\n", event_data->PreviousValue,
438                             event_data->NewValue, event_data->PhysDiskNum,
439                             event_data->PhysDiskDevHandle,
440                             event_data->EnclosureHandle, event_data->Slot);
441                         break;
442                 case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
443                         mps_dprint(sc, MPS_INFO, "   Phys Disk Status changed "
444                             "from 0x%x to 0x%x for Phys Disk Number %d and "
445                             "handle 0x%x at Enclosure handle 0x%x, Slot %d\n",
446                             event_data->PreviousValue, event_data->NewValue,
447                             event_data->PhysDiskNum,
448                             event_data->PhysDiskDevHandle,
449                             event_data->EnclosureHandle, event_data->Slot);
450                         break;
451                 case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
452                         mps_dprint(sc, MPS_INFO, "   Phys Disk State changed "
453                             "from 0x%x to 0x%x for Phys Disk Number %d and "
454                             "handle 0x%x at Enclosure handle 0x%x, Slot %d\n",
455                             event_data->PreviousValue, event_data->NewValue,
456                             event_data->PhysDiskNum,
457                             event_data->PhysDiskDevHandle,
458                             event_data->EnclosureHandle, event_data->Slot);
459                         switch (event_data->NewValue) {
460                         case MPI2_RAID_PD_STATE_ONLINE:
461                         case MPI2_RAID_PD_STATE_DEGRADED:
462                         case MPI2_RAID_PD_STATE_REBUILDING:
463                         case MPI2_RAID_PD_STATE_OPTIMAL:
464                         case MPI2_RAID_PD_STATE_HOT_SPARE:
465                                 targ = mpssas_find_target_by_handle(sassc, 0,
466                                                 event_data->PhysDiskDevHandle);
467                                 if (targ) {
468                                         targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT;
469                                         kprintf("%s %d: Found Target for handle 0x%x.  \n",
470                                                 __func__, __LINE__,
471                                                 event_data->PhysDiskDevHandle);
472                                 }
473                                 break;
474                         case MPI2_RAID_PD_STATE_OFFLINE:
475                         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
476                         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
477                         default:
478                                 targ = mpssas_find_target_by_handle(sassc, 0,
479                                                 event_data->PhysDiskDevHandle);
480                                 if (targ) {
481                                         targ->flags |= ~MPS_TARGET_FLAGS_RAID_COMPONENT;
482                                         kprintf("%s %d: Found Target for handle 0x%x.  \n",
483                                                 __func__, __LINE__,
484                                                 event_data->PhysDiskDevHandle);
485                                 }
486                                 break;
487                         }
488                 default:
489                         break;
490                 }
491                 break;
492         }
493         case MPI2_EVENT_IR_OPERATION_STATUS:
494         {
495                 Mpi2EventDataIrOperationStatus_t *event_data =
496                     fw_event->event_data;
497
498                 /*
499                  * Informational only.
500                  */
501                 mps_dprint(sc, MPS_INFO, "Received IR Op Status event:\n");
502                 mps_dprint(sc, MPS_INFO, "   RAID Operation of %d is %d "
503                     "percent complete for Volume with handle 0x%x",
504                     event_data->RAIDOperation, event_data->PercentComplete,
505                     event_data->VolDevHandle);
506                 break;
507         }
508         case MPI2_EVENT_LOG_ENTRY_ADDED:
509         {
510                 pMpi2EventDataLogEntryAdded_t   logEntry;
511                 uint16_t                        logQualifier;
512                 uint8_t                         logCode;
513
514                 logEntry = (pMpi2EventDataLogEntryAdded_t)fw_event->event_data;
515                 logQualifier = logEntry->LogEntryQualifier;
516
517                 if (logQualifier == MPI2_WD_LOG_ENTRY) {
518                         logCode = logEntry->LogData[0];
519
520                         switch (logCode) {
521                         case MPI2_WD_SSD_THROTTLING:
522                                 kprintf("WarpDrive Warning: IO Throttling has "
523                                     "occurred in the WarpDrive subsystem. "
524                                     "Check WarpDrive documentation for "
525                                     "additional details\n");
526                                 break;
527                         case MPI2_WD_DRIVE_LIFE_WARN:
528                                 kprintf("WarpDrive Warning: Program/Erase "
529                                     "Cycles for the WarpDrive subsystem in "
530                                     "degraded range. Check WarpDrive "
531                                     "documentation for additional details\n");
532                                 break;
533                         case MPI2_WD_DRIVE_LIFE_DEAD:
534                                 kprintf("WarpDrive Fatal Error: There are no "
535                                     "Program/Erase Cycles for the WarpDrive "
536                                     "subsystem. The storage device will be in "
537                                     "read-only mode. Check WarpDrive "
538                                     "documentation for additional details\n");
539                                 break;
540                         case MPI2_WD_RAIL_MON_FAIL:
541                                 kprintf("WarpDrive Fatal Error: The Backup Rail "
542                                     "Monitor has failed on the WarpDrive "
543                                     "subsystem. Check WarpDrive documentation "
544                                     "for additional details\n");
545                                 break;
546                         default:
547                                 break;
548                         }
549                 }
550                 break;
551         }
552         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
553         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
554         default:
555                 mps_dprint(sc, MPS_TRACE,"Unhandled event 0x%0X\n",
556                     fw_event->event);
557                 break;
558
559         }
560         mps_dprint(sc, MPS_INFO, "(%d)->(%s) Event Free: [%x]\n",event_count,__func__, fw_event->event);
561         mpssas_fw_event_free(sc, fw_event);
562 }
563
564 void
565 mpssas_firmware_event_work(void *arg, int pending)
566 {
567         struct mps_fw_event_work *fw_event;
568         struct mps_softc *sc;
569
570         sc = (struct mps_softc *)arg;
571         mps_lock(sc);
572         while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) {
573                 TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link);
574                 mpssas_fw_work(sc, fw_event);
575         }
576         mps_unlock(sc);
577 }
578
579 static int
580 mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate){
581         char devstring[80];
582         struct mpssas_softc *sassc;
583         struct mpssas_target *targ;
584         Mpi2ConfigReply_t mpi_reply;
585         Mpi2SasDevicePage0_t config_page;
586         uint64_t sas_address, sata_sas_address;
587         uint64_t parent_sas_address = 0;
588         u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
589         u32 device_info, parent_devinfo = 0;
590         unsigned int id;
591         int ret;
592         int error = 0;
593
594         sassc = sc->sassc;
595         mpssas_startup_increment(sassc);
596         if ((mps_config_get_sas_device_pg0(sc, &mpi_reply, &config_page,
597              MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
598                 kprintf("%s: error reading SAS device page0\n", __func__);
599                 error = ENXIO;
600                 goto out;
601         }
602
603         device_info = le32toh(config_page.DeviceInfo);
604
605         if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0)
606          && (config_page.ParentDevHandle != 0)) {
607                 Mpi2ConfigReply_t tmp_mpi_reply;
608                 Mpi2SasDevicePage0_t parent_config_page;
609
610                 if ((mps_config_get_sas_device_pg0(sc, &tmp_mpi_reply,
611                      &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
612                      le16toh(config_page.ParentDevHandle)))) {
613                         kprintf("%s: error reading SAS device %#x page0\n",
614                                __func__, le16toh(config_page.ParentDevHandle));
615                 } else {
616                         parent_sas_address = parent_config_page.SASAddress.High;
617                         parent_sas_address = (parent_sas_address << 32) |
618                                 parent_config_page.SASAddress.Low;
619                         parent_devinfo = le32toh(parent_config_page.DeviceInfo);
620                 }
621         }
622         /* TODO Check proper endianess */
623         sas_address = config_page.SASAddress.High;
624         sas_address = (sas_address << 32) |
625             config_page.SASAddress.Low;
626
627         if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE)
628                     == MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
629                 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) {
630                         ret = mpssas_get_sas_address_for_sata_disk(sc,
631                             &sata_sas_address, handle, device_info);
632                         if (!ret)
633                                 id = mps_mapping_get_sas_id(sc,
634                                     sata_sas_address, handle);
635                         else
636                                 id = mps_mapping_get_sas_id(sc,
637                                     sas_address, handle);
638                 } else
639                         id = mps_mapping_get_sas_id(sc, sas_address,
640                             handle);
641         } else
642                 id = mps_mapping_get_sas_id(sc, sas_address, handle);
643
644         if (id == MPS_MAP_BAD_ID) {
645                 kprintf("failure at %s:%d/%s()! Could not get ID for device "
646                     "with handle 0x%04x\n", __FILE__, __LINE__, __func__,
647                     handle);
648                 error = ENXIO;
649                 goto out;
650         }
651         mps_dprint(sc, MPS_INFO, "SAS Address from SAS device page0 = %jx\n",
652                    sas_address);
653         targ = &sassc->targets[id];
654         targ->devinfo = device_info;
655         targ->devname = le32toh(config_page.DeviceName.High);
656         targ->devname = (targ->devname << 32) |
657             le32toh(config_page.DeviceName.Low);
658         targ->encl_handle = le16toh(config_page.EnclosureHandle);
659         targ->encl_slot = le16toh(config_page.Slot);
660         targ->handle = handle;
661         targ->parent_handle = le16toh(config_page.ParentDevHandle);
662         targ->sasaddr = mps_to_u64(&config_page.SASAddress);
663         targ->parent_sasaddr = le64toh(parent_sas_address);
664         targ->parent_devinfo = parent_devinfo;
665         targ->tid = id;
666         targ->linkrate = (linkrate>>4);
667         targ->flags = 0;
668         TAILQ_INIT(&targ->commands);
669         TAILQ_INIT(&targ->timedout_commands);
670         SLIST_INIT(&targ->luns);
671         mps_describe_devinfo(targ->devinfo, devstring, 80);
672         mps_dprint(sc, MPS_INFO, "Found device <%s> <%s> <0x%04x> <%d/%d>\n", devstring,
673             mps_describe_table(mps_linkrate_names, targ->linkrate),
674             targ->handle, targ->encl_handle, targ->encl_slot);
675         if ((sassc->flags & MPSSAS_IN_STARTUP) == 0)
676                 mpssas_rescan_target(sc, targ);
677         mps_dprint(sc, MPS_INFO, "Target id 0x%x added\n", targ->tid);
678 out:
679         mpssas_startup_decrement(sassc);
680         return (error);
681
682 }
683
684 int
685 mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc,
686     u64 *sas_address, u16 handle, u32 device_info)
687 {
688         Mpi2SataPassthroughReply_t mpi_reply;
689         int i, rc, try_count;
690         u32 *bufferptr;
691         union _sata_sas_address hash_address;
692         struct _ata_identify_device_data ata_identify;
693         u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
694         u32 ioc_status;
695         u8 sas_status;
696
697         memset(&ata_identify, 0, sizeof(ata_identify));
698         try_count = 0;
699         do {
700                 rc = mpssas_get_sata_identify(sc, handle, &mpi_reply,
701                     (char *)&ata_identify, sizeof(ata_identify), device_info);
702                 try_count++;
703                 ioc_status = le16toh(mpi_reply.IOCStatus)
704                     & MPI2_IOCSTATUS_MASK;
705                 sas_status = mpi_reply.SASStatus;
706         } while ((rc == -EAGAIN || ioc_status || sas_status) &&
707             (try_count < 5));
708
709         if (rc == 0 && !ioc_status && !sas_status) {
710                 mps_dprint(sc, MPS_INFO, "%s: got SATA identify successfully "
711                            "for handle = 0x%x with try_count = %d\n",
712                            __func__, handle, try_count);
713         } else {
714                 mps_dprint(sc, MPS_INFO, "%s: handle = 0x%x failed\n",
715                            __func__, handle);
716                 return -1;
717         }
718         /* Copy & byteswap the 40 byte model number to a buffer */
719         for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
720                 buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
721                 buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
722         }
723         /* Copy & byteswap the 20 byte serial number to a buffer */
724         for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
725                 buffer[MPT2SAS_MN_LEN + i] =
726                         ((u8 *)ata_identify.serial_number)[i + 1];
727                 buffer[MPT2SAS_MN_LEN + i + 1] =
728                         ((u8 *)ata_identify.serial_number)[i];
729         }
730         bufferptr = (u32 *)buffer;
731         /* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
732          * so loop through the first 56 bytes (7*8),
733          * and then add in the last dword.
734          */
735         hash_address.word.low  = 0;
736         hash_address.word.high = 0;
737         for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
738                 hash_address.word.low += *bufferptr;
739                 bufferptr++;
740                 hash_address.word.high += *bufferptr;
741                 bufferptr++;
742         }
743         /* Add the last dword */
744         hash_address.word.low += *bufferptr;
745         /* Make sure the hash doesn't start with 5, because it could clash
746          * with a SAS address. Change 5 to a D.
747          */
748         if ((hash_address.word.high & 0x000000F0) == (0x00000050))
749                 hash_address.word.high |= 0x00000080;
750         *sas_address = (u64)hash_address.wwid[0] << 56 |
751             (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
752             (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
753             (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] <<  8 |
754             (u64)hash_address.wwid[7];
755         return 0;
756 }
757
758 static int
759 mpssas_get_sata_identify(struct mps_softc *sc, u16 handle,
760     Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
761 {
762         Mpi2SataPassthroughRequest_t *mpi_request;
763         Mpi2SataPassthroughReply_t *reply;
764         struct mps_command *cm;
765         char *buffer;
766         int error = 0;
767
768         buffer = kmalloc( sz, M_MPT2, M_INTWAIT | M_ZERO);
769         if (!buffer)
770                 return ENOMEM;
771
772         if ((cm = mps_alloc_command(sc)) == NULL) {
773                 kfree(buffer, M_MPT2);
774                 return (EBUSY);
775         }
776         mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
777         bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
778         mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
779         mpi_request->VF_ID = 0;
780         mpi_request->DevHandle = htole16(handle);
781         mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
782             MPI2_SATA_PT_REQ_PT_FLAGS_READ);
783         mpi_request->DataLength = htole32(sz);
784         mpi_request->CommandFIS[0] = 0x27;
785         mpi_request->CommandFIS[1] = 0x80;
786         mpi_request->CommandFIS[2] =  (devinfo &
787             MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
788         cm->cm_sge = &mpi_request->SGL;
789         cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
790         cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
791         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
792         cm->cm_data = buffer;
793         cm->cm_length = htole32(sz);
794         error = mps_request_polled(sc, cm);
795         reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
796         if (error || (reply == NULL)) {
797                 /* FIXME */
798                 /* If the poll returns error then we need to do diag reset */
799                 kprintf("%s: poll for page completed with error %d",
800                     __func__, error);
801                 error = ENXIO;
802                 goto out;
803         }
804         bcopy(buffer, id_buffer, sz);
805         bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
806         if ((reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
807             MPI2_IOCSTATUS_SUCCESS) {
808                 kprintf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n",
809                     __func__, reply->IOCStatus);
810                 error = ENXIO;
811                 goto out;
812         }
813 out:
814         mps_free_command(sc, cm);
815         kfree(buffer, M_MPT2);
816         return (error);
817 }
818
819 static int
820 mpssas_volume_add(struct mps_softc *sc, u16 handle)
821 {
822         struct mpssas_softc *sassc;
823         struct mpssas_target *targ;
824         u64 wwid;
825         unsigned int id;
826         int error = 0;
827
828         sassc = sc->sassc;
829         mpssas_startup_increment(sassc);
830         mps_config_get_volume_wwid(sc, handle, &wwid);
831         if (!wwid) {
832                 kprintf("%s: invalid WWID; cannot add volume to mapping table\n",
833                     __func__);
834                 error = ENXIO;
835                 goto out;
836         }
837
838         id = mps_mapping_get_raid_id(sc, wwid, handle);
839         if (id == MPS_MAP_BAD_ID) {
840                 kprintf("%s: could not get ID for volume with handle 0x%04x and "
841                     "WWID 0x%016llx\n", __func__, handle,
842                     (unsigned long long)wwid);
843                 error = ENXIO;
844                 goto out;
845         }
846
847         targ = &sassc->targets[id];
848         targ->tid = id;
849         targ->handle = handle;
850         targ->devname = wwid;
851         TAILQ_INIT(&targ->commands);
852         TAILQ_INIT(&targ->timedout_commands);
853         SLIST_INIT(&targ->luns);
854         if ((sassc->flags & MPSSAS_IN_STARTUP) == 0)
855                 mpssas_rescan_target(sc, targ);
856         mps_dprint(sc, MPS_INFO, "RAID target id %d added (WWID = 0x%jx)\n",
857             targ->tid, wwid);
858 out:
859         mpssas_startup_decrement(sassc);
860         return (error);
861 }
862
863 /**
864  * mpssas_ir_shutdown - IR shutdown notification
865  * @sc: per adapter object
866  *
867  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
868  * the host system is shutting down.
869  *
870  * Return nothing.
871  */
872 void
873 mpssas_ir_shutdown(struct mps_softc *sc)
874 {
875         u16 volume_mapping_flags;
876         u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
877         struct dev_mapping_table *mt_entry;
878         u32 start_idx, end_idx;
879         unsigned int id, found_volume = 0;
880         struct mps_command *cm;
881         Mpi2RaidActionRequest_t *action;
882
883         mps_lock(sc);
884
885         mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
886
887         /* is IR firmware build loaded? */
888         if (!sc->ir_firmware)
889                 goto back;
890
891         /* are there any volumes?  Look at IR target IDs. */
892         // TODO-later, this should be looked up in the RAID config structure
893         // when it is implemented.
894         volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
895             MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
896         if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
897                 start_idx = 0;
898                 if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
899                         start_idx = 1;
900         } else
901                 start_idx = sc->max_devices - sc->max_volumes;
902         end_idx = start_idx + sc->max_volumes - 1;
903
904         for (id = start_idx; id < end_idx; id++) {
905                 mt_entry = &sc->mapping_table[id];
906                 if ((mt_entry->physical_id != 0) &&
907                     (mt_entry->missing_count == 0)) {
908                         found_volume = 1;
909                         break;
910                 }
911         }
912
913         if (!found_volume)
914                 goto back;
915
916         if ((cm = mps_alloc_command(sc)) == NULL) {
917                 kprintf("%s: command alloc failed\n", __func__);
918                 goto back;
919         }
920
921         action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
922         action->Function = MPI2_FUNCTION_RAID_ACTION;
923         action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
924         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
925         mps_lock(sc);
926         mps_request_polled(sc, cm);
927         mps_unlock(sc);
928
929         /*
930          * Don't check for reply, just leave.
931          */
932         if (cm)
933                 mps_free_command(sc, cm);
934
935 back:
936         mps_unlock(sc);
937 }