2 * Copyright (c) 2011-2014 LSI Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * LSI MPT-Fusion Host Adapter FreeBSD
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 /* Communications core for LSI MPT2 */
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>
44 #include <sys/malloc.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>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
57 #include <machine/stdarg.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>
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>
82 /* For Hashed SAS Address creation for SATA Drives */
83 #define MPT2SAS_SN_LEN 20
84 #define MPT2SAS_MN_LEN 40
86 struct mpr_fw_event_work {
89 TAILQ_ENTRY(mpr_fw_event_work) ev_link;
92 union _sata_sas_address {
101 * define the IDENTIFY DEVICE structure
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*/
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,
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,
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);
128 mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
129 MPI2_EVENT_NOTIFICATION_REPLY *event)
131 struct mpr_fw_event_work *fw_event;
134 mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
135 mpr_print_evt_sas(sc, event);
136 mprsas_record_event(sc, event);
138 fw_event = malloc(sizeof(struct mpr_fw_event_work), M_MPR,
141 printf("%s: allocate failed for fw_event\n", __func__);
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);
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++;
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.
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);
170 TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link);
171 taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task);
176 mprsas_fw_event_free(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
179 free(fw_event->event_data, M_MPR);
180 free(fw_event, M_MPR);
184 * _mpr_fw_work - delayed task for processing firmware events
185 * @sc: per adapter object
186 * @fw_event: The fw_event_work object
192 mprsas_fw_work(struct mpr_softc *sc, struct mpr_fw_event_work *fw_event)
194 struct mprsas_softc *sassc;
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:
202 MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
203 MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
206 data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
207 fw_event->event_data;
209 mpr_mapping_topology_change_event(sc, fw_event->event_data);
211 for (i = 0; i < data->NumEntries; 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),
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));
225 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
226 mprsas_prepare_remove(sassc, le16toh(
227 phy->AttachedDevHandle));
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:
237 * refcount was incremented for this event in
238 * mprsas_evt_handler. Decrement it here because the event has
241 mprsas_startup_decrement(sassc);
244 case MPI2_EVENT_SAS_DISCOVERY:
246 MPI2_EVENT_DATA_SAS_DISCOVERY *data;
248 data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data;
250 if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
251 mpr_dprint(sc, MPR_TRACE,"SAS discovery start "
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);
260 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
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);
269 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
271 Mpi2EventIrConfigElement_t *element;
273 u8 foreign_config, reason;
275 Mpi2EventDataIrConfigChangeList_t *event_data;
276 struct mprsas_target *targ;
279 event_data = fw_event->event_data;
280 foreign_config = (le32toh(event_data->Flags) &
281 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
284 (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
285 id = mpr_mapping_get_raid_id_from_handle(sc,
286 element->VolDevHandle);
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;
294 * check for element type of Phys Disk or Hot Spare
297 MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT)
299 MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT))
304 * check for reason of Hide, Unhide, PD Created, or PD
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))
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))
317 // build RAID Action message
318 Mpi2RaidActionRequest_t *action;
319 Mpi2RaidActionReply_t *reply;
320 struct mpr_command *cm;
322 if ((cm = mpr_alloc_command(sc)) == NULL) {
323 printf("%s: command alloc failed\n",
328 mpr_dprint(sc, MPR_INFO, "Sending FP action "
330 "MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST "
332 action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
333 action->Function = MPI2_FUNCTION_RAID_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)) {
344 * If the poll returns error then we
345 * need to do diag reset
347 printf("%s: poll for page completed "
348 "with error %d", __func__, error);
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));
360 mpr_free_command(sc, cm);
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->
379 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
380 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
382 * Rescan after volume is deleted or removed.
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__,
394 targ = &sassc->targets[id];
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;
406 targ->linkrate = 0x0;
407 mprsas_rescan_target(sc, targ);
408 printf("RAID target id 0x%x removed\n",
412 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
413 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
415 * Phys Disk of a volume has been created. Hide
418 targ = mprsas_find_target_by_handle(sassc, 0,
419 element->PhysDiskDevHandle);
422 targ->flags |= MPR_TARGET_FLAGS_RAID_COMPONENT;
423 mprsas_rescan_target(sc, targ);
426 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
428 * Phys Disk of a volume has been deleted.
429 * Expose it to the OS.
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__,
437 mprsas_prepare_remove(sassc,
445 * refcount was incremented for this event in
446 * mprsas_evt_handler. Decrement it here because the event has
449 mprsas_startup_decrement(sassc);
452 case MPI2_EVENT_IR_VOLUME:
454 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
457 * Informational only.
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));
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));
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));
482 struct mprsas_target *targ;
483 state = le32toh(event_data->NewValue);
485 case MPI2_RAID_VOL_STATE_MISSING:
486 case MPI2_RAID_VOL_STATE_FAILED:
487 mprsas_prepare_volume_remove(sassc,
488 event_data->VolDevHandle);
491 case MPI2_RAID_VOL_STATE_ONLINE:
492 case MPI2_RAID_VOL_STATE_DEGRADED:
493 case MPI2_RAID_VOL_STATE_OPTIMAL:
495 mprsas_find_target_by_handle(sassc,
496 0, event_data->VolDevHandle);
498 printf("%s %d: Volume handle "
499 "0x%x is already added \n",
501 event_data->VolDevHandle);
504 if (mprsas_volume_add(sc,
507 printf("%s: failed to add RAID "
508 "volume with handle 0x%x\n",
510 event_data->VolDevHandle));
522 case MPI2_EVENT_IR_PHYSICAL_DISK:
524 Mpi2EventDataIrPhysicalDisk_t *event_data =
525 fw_event->event_data;
526 struct mprsas_target *targ;
529 * Informational only.
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));
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));
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(
573 event_data->PhysDiskDevHandle);
576 MPR_TARGET_FLAGS_RAID_COMPONENT;
577 printf("%s %d: Found Target "
578 "for handle 0x%x.\n",
584 case MPI2_RAID_PD_STATE_OFFLINE:
585 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
586 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
588 targ = mprsas_find_target_by_handle(
590 event_data->PhysDiskDevHandle);
593 ~MPR_TARGET_FLAGS_RAID_COMPONENT;
594 printf("%s %d: Found Target "
595 "for handle 0x%x. \n",
607 case MPI2_EVENT_IR_OPERATION_STATUS:
609 Mpi2EventDataIrOperationStatus_t *event_data =
610 fw_event->event_data;
613 * Informational only.
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));
622 case MPI2_EVENT_TEMP_THRESHOLD:
624 pMpi2EventDataTemperature_t temp_event;
626 temp_event = (pMpi2EventDataTemperature_t)fw_event->event_data;
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.
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);
646 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
647 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
649 mpr_dprint(sc, MPR_TRACE,"Unhandled event 0x%0X\n",
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);
660 mprsas_firmware_event_work(void *arg, int pending)
662 struct mpr_fw_event_work *fw_event;
663 struct mpr_softc *sc;
665 sc = (struct mpr_softc *)arg;
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);
675 mprsas_add_device(struct mpr_softc *sc, u16 handle, u8 linkrate){
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;
688 struct mprsas_lun *lun;
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__);
699 device_info = le32toh(config_page.DeviceInfo);
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;
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));
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);
718 /* TODO Check proper endianess */
719 sas_address = config_page.SASAddress.High;
720 sas_address = (sas_address << 32) |
721 config_page.SASAddress.Low;
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);
729 id = mpr_mapping_get_sas_id(sc,
730 sata_sas_address, handle);
732 id = mpr_mapping_get_sas_id(sc,
733 sas_address, handle);
735 id = mpr_mapping_get_sas_id(sc, sas_address,
738 id = mpr_mapping_get_sas_id(sc, sas_address, handle);
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__,
748 if (mprsas_check_id(sassc, id) != 0) {
749 device_printf(sc->mpr_dev, "Excluding target id %d\n", id);
754 mpr_dprint(sc, MPR_MAPPING, "SAS Address from SAS device page0 = %jx\n",
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;
774 targ->linkrate = (linkrate>>4);
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;
781 if (le16toh(config_page.Flags) &
782 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
783 targ->encl_level_valid = TRUE;
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);
792 SLIST_INIT(&targ->luns);
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);
804 #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
805 (__FreeBSD_version < 902502)
806 if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
808 mprsas_rescan_target(sc, targ);
809 mpr_dprint(sc, MPR_MAPPING, "Target id 0x%x added\n", targ->tid);
811 mprsas_startup_decrement(sassc);
817 mprsas_get_sas_address_for_sata_disk(struct mpr_softc *sc,
818 u64 *sas_address, u16 handle, u32 device_info)
820 Mpi2SataPassthroughReply_t mpi_reply;
821 int i, rc, try_count;
823 union _sata_sas_address hash_address;
824 struct _ata_identify_device_data ata_identify;
825 u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN];
829 memset(&ata_identify, 0, sizeof(ata_identify));
832 rc = mprsas_get_sata_identify(sc, handle, &mpi_reply,
833 (char *)&ata_identify, sizeof(ata_identify), device_info);
835 ioc_status = le16toh(mpi_reply.IOCStatus)
836 & MPI2_IOCSTATUS_MASK;
837 sas_status = mpi_reply.SASStatus;
838 } while ((rc == -EAGAIN || ioc_status || sas_status) &&
841 if (rc == 0 && !ioc_status && !sas_status) {
842 mpr_dprint(sc, MPR_MAPPING, "%s: got SATA identify "
843 "successfully for handle = 0x%x with try_count = %d\n",
844 __func__, handle, try_count);
846 mpr_dprint(sc, MPR_MAPPING, "%s: handle = 0x%x failed\n",
850 /* Copy & byteswap the 40 byte model number to a buffer */
851 for (i = 0; i < MPT2SAS_MN_LEN; i += 2) {
852 buffer[i] = ((u8 *)ata_identify.model_number)[i + 1];
853 buffer[i + 1] = ((u8 *)ata_identify.model_number)[i];
855 /* Copy & byteswap the 20 byte serial number to a buffer */
856 for (i = 0; i < MPT2SAS_SN_LEN; i += 2) {
857 buffer[MPT2SAS_MN_LEN + i] =
858 ((u8 *)ata_identify.serial_number)[i + 1];
859 buffer[MPT2SAS_MN_LEN + i + 1] =
860 ((u8 *)ata_identify.serial_number)[i];
862 bufferptr = (u32 *)buffer;
863 /* There are 60 bytes to hash down to 8. 60 isn't divisible by 8,
864 * so loop through the first 56 bytes (7*8),
865 * and then add in the last dword.
867 hash_address.word.low = 0;
868 hash_address.word.high = 0;
869 for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) {
870 hash_address.word.low += *bufferptr;
872 hash_address.word.high += *bufferptr;
875 /* Add the last dword */
876 hash_address.word.low += *bufferptr;
877 /* Make sure the hash doesn't start with 5, because it could clash
878 * with a SAS address. Change 5 to a D.
880 if ((hash_address.word.high & 0x000000F0) == (0x00000050))
881 hash_address.word.high |= 0x00000080;
882 *sas_address = (u64)hash_address.wwid[0] << 56 |
883 (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 |
884 (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 |
885 (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] << 8 |
886 (u64)hash_address.wwid[7];
891 mprsas_get_sata_identify(struct mpr_softc *sc, u16 handle,
892 Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo)
894 Mpi2SataPassthroughRequest_t *mpi_request;
895 Mpi2SataPassthroughReply_t *reply;
896 struct mpr_command *cm;
900 buffer = malloc( sz, M_MPR, M_NOWAIT | M_ZERO);
904 if ((cm = mpr_alloc_command(sc)) == NULL) {
908 mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req;
909 bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST));
910 mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH;
911 mpi_request->VF_ID = 0;
912 mpi_request->DevHandle = htole16(handle);
913 mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO |
914 MPI2_SATA_PT_REQ_PT_FLAGS_READ);
915 mpi_request->DataLength = htole32(sz);
916 mpi_request->CommandFIS[0] = 0x27;
917 mpi_request->CommandFIS[1] = 0x80;
918 mpi_request->CommandFIS[2] = (devinfo &
919 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC;
920 cm->cm_sge = &mpi_request->SGL;
921 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
922 cm->cm_flags = MPR_CM_FLAGS_DATAIN;
923 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
924 cm->cm_data = buffer;
925 cm->cm_length = htole32(sz);
926 error = mpr_wait_command(sc, cm, 60, CAN_SLEEP);
927 reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply;
928 if (error || (reply == NULL)) {
931 * If the request returns an error then we need to do a diag
934 printf("%s: request for page completed with error %d",
939 bcopy(buffer, id_buffer, sz);
940 bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t));
941 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
942 MPI2_IOCSTATUS_SUCCESS) {
943 printf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n",
944 __func__, reply->IOCStatus);
949 mpr_free_command(sc, cm);
955 mprsas_volume_add(struct mpr_softc *sc, u16 handle)
957 struct mprsas_softc *sassc;
958 struct mprsas_target *targ;
962 struct mprsas_lun *lun;
965 mprsas_startup_increment(sassc);
966 /* wwid is endian safe */
967 mpr_config_get_volume_wwid(sc, handle, &wwid);
969 printf("%s: invalid WWID; cannot add volume to mapping table\n",
975 id = mpr_mapping_get_raid_id(sc, wwid, handle);
976 if (id == MPR_MAP_BAD_ID) {
977 printf("%s: could not get ID for volume with handle 0x%04x and "
978 "WWID 0x%016llx\n", __func__, handle,
979 (unsigned long long)wwid);
984 targ = &sassc->targets[id];
986 targ->handle = handle;
987 targ->devname = wwid;
988 TAILQ_INIT(&targ->commands);
989 TAILQ_INIT(&targ->timedout_commands);
990 while (!SLIST_EMPTY(&targ->luns)) {
991 lun = SLIST_FIRST(&targ->luns);
992 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
995 SLIST_INIT(&targ->luns);
996 #if ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000039)) || \
997 (__FreeBSD_version < 902502)
998 if ((sassc->flags & MPRSAS_IN_STARTUP) == 0)
1000 mprsas_rescan_target(sc, targ);
1001 mpr_dprint(sc, MPR_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n",
1004 mprsas_startup_decrement(sassc);
1009 * mprsas_SSU_to_SATA_devices
1010 * @sc: per adapter object
1012 * Looks through the target list and issues a StartStopUnit SCSI command to each
1013 * SATA direct-access device. This helps to ensure that data corruption is
1014 * avoided when the system is being shut down. This must be called after the IR
1015 * System Shutdown RAID Action is sent if in IR mode.
1020 mprsas_SSU_to_SATA_devices(struct mpr_softc *sc)
1022 struct mprsas_softc *sassc = sc->sassc;
1024 path_id_t pathid = cam_sim_path(sassc->sim);
1025 target_id_t targetid;
1026 struct mprsas_target *target;
1027 struct mprsas_lun *lun;
1029 struct timeval cur_time, start_time;
1034 * For each LUN of each target, issue a StartStopUnit command to stop
1037 sc->SSU_started = TRUE;
1038 sc->SSU_refcount = 0;
1039 for (targetid = 0; targetid < sc->facts->MaxTargets; targetid++) {
1040 target = &sassc->targets[targetid];
1041 if (target->handle == 0x0) {
1045 SLIST_FOREACH(lun, &target->luns, lun_link) {
1046 ccb = xpt_alloc_ccb_nowait();
1049 mpr_dprint(sc, MPR_FAULT, "Unable to alloc "
1050 "CCB to stop unit.\n");
1055 * The stop_at_shutdown flag will be set if this LUN is
1056 * a SATA direct-access end device.
1058 if (lun->stop_at_shutdown) {
1059 if (xpt_create_path(&ccb->ccb_h.path,
1060 xpt_periph, pathid, targetid,
1061 lun->lun_id) != CAM_REQ_CMP) {
1062 mpr_dprint(sc, MPR_FAULT, "Unable to "
1063 "create LUN path to stop unit.\n");
1068 xpt_path_string(ccb->ccb_h.path, path_str,
1071 mpr_dprint(sc, MPR_INFO, "Sending StopUnit: "
1072 "path %s handle %d\n", path_str,
1076 * Issue a START STOP UNIT command for the LUN.
1077 * Increment the SSU counter to be used to
1078 * count the number of required replies.
1080 mpr_dprint(sc, MPR_INFO, "Incrementing SSU "
1083 ccb->ccb_h.target_id =
1084 xpt_path_target_id(ccb->ccb_h.path);
1085 ccb->ccb_h.target_lun = lun->lun_id;
1086 ccb->ccb_h.ppriv_ptr1 = sassc;
1087 scsi_start_stop(&ccb->csio,
1089 mprsas_stop_unit_done,
1104 * Wait until all of the SSU commands have completed or time has
1105 * expired (60 seconds). pause for 100ms each time through. If any
1106 * command times out, the target will be reset in the SCSI command
1109 getmicrotime(&start_time);
1110 while (sc->SSU_refcount) {
1111 pause("mprwait", hz/10);
1113 getmicrotime(&cur_time);
1114 if ((cur_time.tv_sec - start_time.tv_sec) > 60) {
1115 mpr_dprint(sc, MPR_FAULT, "Time has expired waiting "
1116 "for SSU commands to complete.\n");
1123 mprsas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb)
1125 struct mprsas_softc *sassc;
1128 sassc = (struct mprsas_softc *)done_ccb->ccb_h.ppriv_ptr1;
1130 xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str));
1131 mpr_dprint(sassc->sc, MPR_INFO, "Completing stop unit for %s\n",
1134 if (done_ccb == NULL)
1138 * Nothing more to do except free the CCB and path. If the command
1139 * timed out, an abort reset, then target reset will be issued during
1140 * the SCSI Command process.
1142 xpt_free_path(done_ccb->ccb_h.path);
1143 xpt_free_ccb(done_ccb);
1147 * mprsas_ir_shutdown - IR shutdown notification
1148 * @sc: per adapter object
1150 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
1151 * the host system is shutting down.
1156 mprsas_ir_shutdown(struct mpr_softc *sc)
1158 u16 volume_mapping_flags;
1159 u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1160 struct dev_mapping_table *mt_entry;
1161 u32 start_idx, end_idx;
1162 unsigned int id, found_volume = 0;
1163 struct mpr_command *cm;
1164 Mpi2RaidActionRequest_t *action;
1166 mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
1168 /* is IR firmware build loaded? */
1169 if (!sc->ir_firmware)
1172 /* are there any volumes? Look at IR target IDs. */
1173 // TODO-later, this should be looked up in the RAID config structure
1174 // when it is implemented.
1175 volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
1176 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
1177 if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
1179 if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
1182 start_idx = sc->max_devices - sc->max_volumes;
1183 end_idx = start_idx + sc->max_volumes - 1;
1185 for (id = start_idx; id < end_idx; id++) {
1186 mt_entry = &sc->mapping_table[id];
1187 if ((mt_entry->physical_id != 0) &&
1188 (mt_entry->missing_count == 0)) {
1197 if ((cm = mpr_alloc_command(sc)) == NULL) {
1198 printf("%s: command alloc failed\n", __func__);
1202 action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req;
1203 action->Function = MPI2_FUNCTION_RAID_ACTION;
1204 action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
1205 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1207 mpr_wait_command(sc, cm, 5, CAN_SLEEP);
1211 * Don't check for reply, just leave.
1214 mpr_free_command(sc, cm);
1217 mprsas_SSU_to_SATA_devices(sc);