1323124Sdes/*- 2180740Sdes * Copyright (c) 2011-2015 LSI Corp. 3180740Sdes * Copyright (c) 2013-2015 Avago Technologies 4180740Sdes * All rights reserved. 5180740Sdes * 6180740Sdes * Redistribution and use in source and binary forms, with or without 7180740Sdes * modification, are permitted provided that the following conditions 8180740Sdes * are met: 9180740Sdes * 1. Redistributions of source code must retain the above copyright 10180740Sdes * notice, this list of conditions and the following disclaimer. 11180740Sdes * 2. Redistributions in binary form must reproduce the above copyright 12180740Sdes * notice, this list of conditions and the following disclaimer in the 13180740Sdes * documentation and/or other materials provided with the distribution. 14180740Sdes * 15180740Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16180740Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17180740Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18180740Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19180740Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20180740Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21180740Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22180740Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23180740Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24180740Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25180740Sdes * SUCH DAMAGE. 26180740Sdes * 27180740Sdes * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD 28180740Sdes */ 29180740Sdes 30180740Sdes#include <sys/cdefs.h> 31180740Sdes__FBSDID("$FreeBSD: stable/10/sys/dev/mps/mps_sas_lsi.c 322661 2017-08-18 15:38:08Z ken $"); 32180740Sdes 33180740Sdes/* Communications core for Avago Technologies (LSI) MPT2 */ 34180740Sdes 35180740Sdes/* TODO Move headers to mpsvar */ 36180740Sdes#include <sys/types.h> 37180740Sdes#include <sys/param.h> 38180740Sdes#include <sys/systm.h> 39180740Sdes#include <sys/kernel.h> 40180740Sdes#include <sys/selinfo.h> 41180740Sdes#include <sys/module.h> 42180740Sdes#include <sys/bus.h> 43180740Sdes#include <sys/conf.h> 44180740Sdes#include <sys/bio.h> 45180740Sdes#include <sys/malloc.h> 46180740Sdes#include <sys/uio.h> 47180740Sdes#include <sys/sysctl.h> 48180740Sdes#include <sys/endian.h> 49180740Sdes#include <sys/queue.h> 50180740Sdes#include <sys/kthread.h> 51180740Sdes#include <sys/taskqueue.h> 52180740Sdes#include <sys/sbuf.h> 53180740Sdes 54180740Sdes#include <machine/bus.h> 55180740Sdes#include <machine/resource.h> 56180740Sdes#include <sys/rman.h> 57180740Sdes 58180740Sdes#include <machine/stdarg.h> 59180740Sdes 60180740Sdes#include <cam/cam.h> 61180740Sdes#include <cam/cam_ccb.h> 62180740Sdes#include <cam/cam_debug.h> 63180740Sdes#include <cam/cam_sim.h> 64180740Sdes#include <cam/cam_xpt_sim.h> 65180740Sdes#include <cam/cam_xpt_periph.h> 66180740Sdes#include <cam/cam_periph.h> 67180740Sdes#include <cam/scsi/scsi_all.h> 68180740Sdes#include <cam/scsi/scsi_message.h> 69180740Sdes 70180740Sdes#include <dev/mps/mpi/mpi2_type.h> 71180740Sdes#include <dev/mps/mpi/mpi2.h> 72180740Sdes#include <dev/mps/mpi/mpi2_ioc.h> 73180740Sdes#include <dev/mps/mpi/mpi2_sas.h> 74180740Sdes#include <dev/mps/mpi/mpi2_cnfg.h> 75180740Sdes#include <dev/mps/mpi/mpi2_init.h> 76180740Sdes#include <dev/mps/mpi/mpi2_raid.h> 77214979Sdes#include <dev/mps/mpi/mpi2_tool.h> 78180740Sdes#include <dev/mps/mps_ioctl.h> 79204861Sdes#include <dev/mps/mpsvar.h> 80180740Sdes#include <dev/mps/mps_table.h> 81180740Sdes#include <dev/mps/mps_sas.h> 82180740Sdes 83180740Sdes/* For Hashed SAS Address creation for SATA Drives */ 84180740Sdes#define MPT2SAS_SN_LEN 20 85180740Sdes#define MPT2SAS_MN_LEN 40 86180740Sdes 87225825Sdesstruct mps_fw_event_work { 88180740Sdes u16 event; 89295367Sdes void *event_data; 90225825Sdes TAILQ_ENTRY(mps_fw_event_work) ev_link; 91180740Sdes}; 92296781Sdes 93180740Sdesunion _sata_sas_address { 94225825Sdes u8 wwid[8]; 95180740Sdes struct { 96180740Sdes u32 high; 97225825Sdes u32 low; 98180740Sdes } word; 99180740Sdes}; 100225825Sdes 101180740Sdes/* 102180740Sdes * define the IDENTIFY DEVICE structure 103225825Sdes */ 104225825Sdesstruct _ata_identify_device_data { 105180740Sdes u16 reserved1[10]; /* 0-9 */ 106180740Sdes u16 serial_number[10]; /* 10-19 */ 107180740Sdes u16 reserved2[7]; /* 20-26 */ 108180740Sdes u16 model_number[20]; /* 27-46*/ 109180740Sdes u16 reserved3[170]; /* 47-216 */ 110180740Sdes u16 rotational_speed; /* 217 */ 111180740Sdes u16 reserved4[38]; /* 218-255 */ 112180740Sdes}; 113180740Sdesstatic u32 event_count; 114180740Sdesstatic void mpssas_fw_work(struct mps_softc *sc, 115180740Sdes struct mps_fw_event_work *fw_event); 116180740Sdesstatic void mpssas_fw_event_free(struct mps_softc *, 117225825Sdes struct mps_fw_event_work *); 118180740Sdesstatic int mpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate); 119180740Sdesstatic int mpssas_get_sata_identify(struct mps_softc *sc, u16 handle, 120180740Sdes Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, 121180740Sdes u32 devinfo); 122180740Sdesstatic void mpssas_ata_id_timeout(void *data); 123180740Sdesint mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc, 124180740Sdes u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD); 125180740Sdesstatic int mpssas_volume_add(struct mps_softc *sc, 126180740Sdes u16 handle); 127180740Sdesstatic void mpssas_SSU_to_SATA_devices(struct mps_softc *sc); 128180740Sdesstatic void mpssas_stop_unit_done(struct cam_periph *periph, 129180740Sdes union ccb *done_ccb); 130180740Sdes 131180740Sdesvoid 132180740Sdesmpssas_evt_handler(struct mps_softc *sc, uintptr_t data, 133180740Sdes MPI2_EVENT_NOTIFICATION_REPLY *event) 134180740Sdes{ 135180740Sdes struct mps_fw_event_work *fw_event; 136180740Sdes u16 sz; 137180740Sdes 138180740Sdes mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 139180740Sdes MPS_DPRINT_EVENT(sc, sas, event); 140180740Sdes mpssas_record_event(sc, event); 141180740Sdes 142180740Sdes fw_event = malloc(sizeof(struct mps_fw_event_work), M_MPT2, 143180740Sdes M_ZERO|M_NOWAIT); 144180740Sdes if (!fw_event) { 145180740Sdes printf("%s: allocate failed for fw_event\n", __func__); 146180740Sdes return; 147180740Sdes } 148180740Sdes sz = le16toh(event->EventDataLength) * 4; 149180740Sdes fw_event->event_data = malloc(sz, M_MPT2, M_ZERO|M_NOWAIT); 150180740Sdes if (!fw_event->event_data) { 151180740Sdes printf("%s: allocate failed for event_data\n", __func__); 152180740Sdes free(fw_event, M_MPT2); 153180740Sdes return; 154180740Sdes } 155180740Sdes 156180740Sdes bcopy(event->EventData, fw_event->event_data, sz); 157180740Sdes fw_event->event = event->Event; 158180740Sdes if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST || 159180740Sdes event->Event == MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE || 160180740Sdes event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) && 161180740Sdes sc->track_mapping_events) 162180740Sdes sc->pending_map_events++; 163180740Sdes 164180740Sdes /* 165180740Sdes * When wait_for_port_enable flag is set, make sure that all the events 166180740Sdes * are processed. Increment the startup_refcount and decrement it after 167180740Sdes * events are processed. 168180740Sdes */ 169180740Sdes if ((event->Event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST || 170180740Sdes event->Event == MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST) && 171180740Sdes sc->wait_for_port_enable) 172180740Sdes mpssas_startup_increment(sc->sassc); 173180740Sdes 174180740Sdes TAILQ_INSERT_TAIL(&sc->sassc->ev_queue, fw_event, ev_link); 175180740Sdes taskqueue_enqueue(sc->sassc->ev_tq, &sc->sassc->ev_task); 176180740Sdes 177180740Sdes} 178180740Sdes 179180740Sdesstatic void 180180740Sdesmpssas_fw_event_free(struct mps_softc *sc, struct mps_fw_event_work *fw_event) 181180740Sdes{ 182180740Sdes 183180740Sdes free(fw_event->event_data, M_MPT2); 184180740Sdes free(fw_event, M_MPT2); 185180740Sdes} 186180740Sdes 187296781Sdes/** 188180740Sdes * _mps_fw_work - delayed task for processing firmware events 189180740Sdes * @sc: per adapter object 190180740Sdes * @fw_event: The fw_event_work object 191180740Sdes * Context: user. 192180740Sdes * 193180740Sdes * Return nothing. 194180740Sdes */ 195180740Sdesstatic void 196180740Sdesmpssas_fw_work(struct mps_softc *sc, struct mps_fw_event_work *fw_event) 197180740Sdes{ 198180740Sdes struct mpssas_softc *sassc; 199180740Sdes sassc = sc->sassc; 200180740Sdes 201180740Sdes mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Working on Event: [%x]\n", 202180740Sdes event_count++,__func__,fw_event->event); 203180740Sdes switch (fw_event->event) { 204180740Sdes case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST: 205180740Sdes { 206180740Sdes MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data; 207180740Sdes MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy; 208180740Sdes int i; 209180740Sdes 210180740Sdes data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *) 211180740Sdes fw_event->event_data; 212180740Sdes 213180740Sdes mps_mapping_topology_change_event(sc, fw_event->event_data); 214180740Sdes 215180740Sdes for (i = 0; i < data->NumEntries; i++) { 216180740Sdes phy = &data->PHY[i]; 217180740Sdes switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) { 218180740Sdes case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED: 219180740Sdes if (mpssas_add_device(sc, 220180740Sdes le16toh(phy->AttachedDevHandle), 221180740Sdes phy->LinkRate)){ 222180740Sdes mps_dprint(sc, MPS_ERROR, "%s: " 223180740Sdes "failed to add device with handle " 224180740Sdes "0x%x\n", __func__, 225180740Sdes le16toh(phy->AttachedDevHandle)); 226180740Sdes mpssas_prepare_remove(sassc, le16toh( 227180740Sdes phy->AttachedDevHandle)); 228180740Sdes } 229180740Sdes break; 230180740Sdes case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING: 231180740Sdes mpssas_prepare_remove(sassc,le16toh( 232180740Sdes phy->AttachedDevHandle)); 233180740Sdes break; 234180740Sdes case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED: 235180740Sdes case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE: 236180740Sdes case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING: 237180740Sdes default: 238180740Sdes break; 239180740Sdes } 240180740Sdes } 241180740Sdes /* 242180740Sdes * refcount was incremented for this event in 243180740Sdes * mpssas_evt_handler. Decrement it here because the event has 244180740Sdes * been processed. 245180740Sdes */ 246180740Sdes mpssas_startup_decrement(sassc); 247180740Sdes break; 248180740Sdes } 249180740Sdes case MPI2_EVENT_SAS_DISCOVERY: 250180740Sdes { 251180740Sdes MPI2_EVENT_DATA_SAS_DISCOVERY *data; 252180740Sdes 253180740Sdes data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)fw_event->event_data; 254180740Sdes 255180740Sdes if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED) 256180740Sdes mps_dprint(sc, MPS_TRACE,"SAS discovery start event\n"); 257180740Sdes if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) { 258180740Sdes mps_dprint(sc, MPS_TRACE,"SAS discovery stop event\n"); 259180740Sdes sassc->flags &= ~MPSSAS_IN_DISCOVERY; 260180740Sdes mpssas_discovery_end(sassc); 261180740Sdes } 262180740Sdes break; 263180740Sdes } 264180740Sdes case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE: 265180740Sdes { 266180740Sdes Mpi2EventDataSasEnclDevStatusChange_t *data; 267180740Sdes data = (Mpi2EventDataSasEnclDevStatusChange_t *) 268180740Sdes fw_event->event_data; 269180740Sdes mps_mapping_enclosure_dev_status_change_event(sc, 270180740Sdes fw_event->event_data); 271180740Sdes break; 272180740Sdes } 273180740Sdes case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST: 274180740Sdes { 275180740Sdes Mpi2EventIrConfigElement_t *element; 276180740Sdes int i; 277180740Sdes u8 foreign_config; 278180740Sdes Mpi2EventDataIrConfigChangeList_t *event_data; 279180740Sdes struct mpssas_target *targ; 280180740Sdes unsigned int id; 281180740Sdes 282180740Sdes event_data = fw_event->event_data; 283180740Sdes foreign_config = (le32toh(event_data->Flags) & 284180740Sdes MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0; 285180740Sdes 286180740Sdes element = 287180740Sdes (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0]; 288180740Sdes id = mps_mapping_get_raid_tid_from_handle(sc, 289180740Sdes element->VolDevHandle); 290180740Sdes 291180740Sdes mps_mapping_ir_config_change_event(sc, event_data); 292180740Sdes 293180740Sdes for (i = 0; i < event_data->NumElements; i++, element++) { 294180740Sdes switch (element->ReasonCode) { 295180740Sdes case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED: 296180740Sdes case MPI2_EVENT_IR_CHANGE_RC_ADDED: 297180740Sdes if (!foreign_config) { 298180740Sdes if (mpssas_volume_add(sc, 299180740Sdes le16toh(element->VolDevHandle))){ 300180740Sdes printf("%s: failed to add RAID " 301180740Sdes "volume with handle 0x%x\n", 302180740Sdes __func__, le16toh(element-> 303180740Sdes VolDevHandle)); 304180740Sdes } 305180740Sdes } 306180740Sdes break; 307180740Sdes case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED: 308180740Sdes case MPI2_EVENT_IR_CHANGE_RC_REMOVED: 309180740Sdes /* 310180740Sdes * Rescan after volume is deleted or removed. 311180740Sdes */ 312180740Sdes if (!foreign_config) { 313180740Sdes if (id == MPS_MAP_BAD_ID) { 314180740Sdes printf("%s: could not get ID " 315180740Sdes "for volume with handle " 316180740Sdes "0x%04x\n", __func__, 317180740Sdes le16toh(element->VolDevHandle)); 318180740Sdes break; 319180740Sdes } 320180740Sdes 321180740Sdes targ = &sassc->targets[id]; 322180740Sdes targ->handle = 0x0; 323180740Sdes targ->encl_slot = 0x0; 324180740Sdes targ->encl_handle = 0x0; 325180740Sdes targ->exp_dev_handle = 0x0; 326180740Sdes targ->phy_num = 0x0; 327180740Sdes targ->linkrate = 0x0; 328180740Sdes mpssas_rescan_target(sc, targ); 329180740Sdes printf("RAID target id 0x%x removed\n", 330180740Sdes targ->tid); 331180740Sdes } 332180740Sdes break; 333180740Sdes case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED: 334180740Sdes case MPI2_EVENT_IR_CHANGE_RC_HIDE: 335180740Sdes /* 336239844Sdes * Phys Disk of a volume has been created. Hide 337180740Sdes * it from the OS. 338180740Sdes */ 339180740Sdes targ = mpssas_find_target_by_handle(sassc, 0, 340180740Sdes element->PhysDiskDevHandle); 341180740Sdes if (targ == NULL) 342180740Sdes break; 343180740Sdes 344180740Sdes /* 345180740Sdes * Set raid component flags only if it is not 346204861Sdes * WD. OR WrapDrive with 347180740Sdes * WD_HIDE_ALWAYS/WD_HIDE_IF_VOLUME is set in 348204861Sdes * NVRAM 349180740Sdes */ 350180740Sdes if((!sc->WD_available) || 351180740Sdes ((sc->WD_available && 352180740Sdes (sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) || 353180740Sdes (sc->WD_valid_config && (sc->WD_hide_expose == 354180740Sdes MPS_WD_HIDE_IF_VOLUME)))) { 355180740Sdes targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT; 356180740Sdes } 357180740Sdes mpssas_rescan_target(sc, targ); 358180740Sdes 359180740Sdes break; 360180740Sdes case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED: 361180740Sdes /* 362180740Sdes * Phys Disk of a volume has been deleted. 363180740Sdes * Expose it to the OS. 364180740Sdes */ 365180740Sdes if (mpssas_add_device(sc, 366180740Sdes le16toh(element->PhysDiskDevHandle), 0)){ 367180740Sdes printf("%s: failed to add device with " 368180740Sdes "handle 0x%x\n", __func__, 369180740Sdes le16toh(element->PhysDiskDevHandle)); 370180740Sdes mpssas_prepare_remove(sassc, le16toh(element-> 371180740Sdes PhysDiskDevHandle)); 372180740Sdes } 373180740Sdes break; 374180740Sdes } 375180740Sdes } 376180740Sdes /* 377180740Sdes * refcount was incremented for this event in 378180740Sdes * mpssas_evt_handler. Decrement it here because the event has 379180750Sdes * been processed. 380180740Sdes */ 381180740Sdes mpssas_startup_decrement(sassc); 382180740Sdes break; 383180740Sdes } 384180740Sdes case MPI2_EVENT_IR_VOLUME: 385180740Sdes { 386180740Sdes Mpi2EventDataIrVolume_t *event_data = fw_event->event_data; 387180740Sdes 388180740Sdes /* 389180740Sdes * Informational only. 390180740Sdes */ 391180740Sdes mps_dprint(sc, MPS_EVENT, "Received IR Volume event:\n"); 392180740Sdes switch (event_data->ReasonCode) { 393180740Sdes case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED: 394180740Sdes mps_dprint(sc, MPS_EVENT, " Volume Settings " 395180740Sdes "changed from 0x%x to 0x%x for Volome with " 396180740Sdes "handle 0x%x", le32toh(event_data->PreviousValue), 397180740Sdes le32toh(event_data->NewValue), 398180740Sdes le16toh(event_data->VolDevHandle)); 399180740Sdes break; 400180740Sdes case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED: 401180740Sdes mps_dprint(sc, MPS_EVENT, " Volume Status " 402180740Sdes "changed from 0x%x to 0x%x for Volome with " 403180740Sdes "handle 0x%x", le32toh(event_data->PreviousValue), 404180740Sdes le32toh(event_data->NewValue), 405180740Sdes le16toh(event_data->VolDevHandle)); 406214979Sdes break; 407214979Sdes case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED: 408214979Sdes mps_dprint(sc, MPS_EVENT, " Volume State " 409180740Sdes "changed from 0x%x to 0x%x for Volome with " 410180740Sdes "handle 0x%x", le32toh(event_data->PreviousValue), 411180740Sdes le32toh(event_data->NewValue), 412180740Sdes le16toh(event_data->VolDevHandle)); 413180740Sdes u32 state; 414180740Sdes struct mpssas_target *targ; 415180740Sdes state = le32toh(event_data->NewValue); 416180740Sdes switch (state) { 417180740Sdes case MPI2_RAID_VOL_STATE_MISSING: 418180740Sdes case MPI2_RAID_VOL_STATE_FAILED: 419180740Sdes mpssas_prepare_volume_remove(sassc, event_data-> 420180740Sdes VolDevHandle); 421180740Sdes break; 422180740Sdes 423180740Sdes case MPI2_RAID_VOL_STATE_ONLINE: 424180740Sdes case MPI2_RAID_VOL_STATE_DEGRADED: 425180740Sdes case MPI2_RAID_VOL_STATE_OPTIMAL: 426180740Sdes targ = mpssas_find_target_by_handle(sassc, 0, event_data->VolDevHandle); 427180740Sdes if (targ) { 428180740Sdes printf("%s %d: Volume handle 0x%x is already added \n", 429180740Sdes __func__, __LINE__ , event_data->VolDevHandle); 430180740Sdes break; 431180740Sdes } 432180740Sdes if (mpssas_volume_add(sc, le16toh(event_data->VolDevHandle))) { 433180740Sdes printf("%s: failed to add RAID " 434180740Sdes "volume with handle 0x%x\n", 435180740Sdes __func__, le16toh(event_data-> 436180740Sdes VolDevHandle)); 437180740Sdes } 438180740Sdes break; 439180740Sdes default: 440180740Sdes break; 441180740Sdes } 442180740Sdes break; 443180740Sdes default: 444180740Sdes break; 445180740Sdes } 446180740Sdes break; 447180740Sdes } 448180740Sdes case MPI2_EVENT_IR_PHYSICAL_DISK: 449180740Sdes { 450180740Sdes Mpi2EventDataIrPhysicalDisk_t *event_data = 451180740Sdes fw_event->event_data; 452180740Sdes struct mpssas_target *targ; 453180740Sdes 454180740Sdes /* 455180740Sdes * Informational only. 456180740Sdes */ 457180740Sdes mps_dprint(sc, MPS_EVENT, "Received IR Phys Disk event:\n"); 458180740Sdes switch (event_data->ReasonCode) { 459180740Sdes case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED: 460180740Sdes mps_dprint(sc, MPS_EVENT, " Phys Disk Settings " 461180740Sdes "changed from 0x%x to 0x%x for Phys Disk Number " 462180740Sdes "%d and handle 0x%x at Enclosure handle 0x%x, Slot " 463180740Sdes "%d", le32toh(event_data->PreviousValue), 464180740Sdes le32toh(event_data->NewValue), 465180740Sdes event_data->PhysDiskNum, 466180740Sdes le16toh(event_data->PhysDiskDevHandle), 467180740Sdes le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot)); 468180740Sdes break; 469180740Sdes case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED: 470180740Sdes mps_dprint(sc, MPS_EVENT, " Phys Disk Status changed " 471180740Sdes "from 0x%x to 0x%x for Phys Disk Number %d and " 472180740Sdes "handle 0x%x at Enclosure handle 0x%x, Slot %d", 473180740Sdes le32toh(event_data->PreviousValue), 474180740Sdes le32toh(event_data->NewValue), event_data->PhysDiskNum, 475180740Sdes le16toh(event_data->PhysDiskDevHandle), 476180740Sdes le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot)); 477180740Sdes break; 478180740Sdes case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED: 479180740Sdes mps_dprint(sc, MPS_EVENT, " Phys Disk State changed " 480180740Sdes "from 0x%x to 0x%x for Phys Disk Number %d and " 481180740Sdes "handle 0x%x at Enclosure handle 0x%x, Slot %d", 482180740Sdes le32toh(event_data->PreviousValue), 483180740Sdes le32toh(event_data->NewValue), event_data->PhysDiskNum, 484180740Sdes le16toh(event_data->PhysDiskDevHandle), 485180740Sdes le16toh(event_data->EnclosureHandle), le16toh(event_data->Slot)); 486180740Sdes switch (event_data->NewValue) { 487180740Sdes case MPI2_RAID_PD_STATE_ONLINE: 488180740Sdes case MPI2_RAID_PD_STATE_DEGRADED: 489180740Sdes case MPI2_RAID_PD_STATE_REBUILDING: 490180740Sdes case MPI2_RAID_PD_STATE_OPTIMAL: 491180740Sdes case MPI2_RAID_PD_STATE_HOT_SPARE: 492180740Sdes targ = mpssas_find_target_by_handle(sassc, 0, 493180740Sdes event_data->PhysDiskDevHandle); 494180740Sdes if (targ) { 495180740Sdes if(!sc->WD_available) { 496180740Sdes targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT; 497180740Sdes printf("%s %d: Found Target for handle 0x%x. \n", 498180740Sdes __func__, __LINE__ , event_data->PhysDiskDevHandle); 499180740Sdes } else if ((sc->WD_available && 500180740Sdes (sc->WD_hide_expose == MPS_WD_HIDE_ALWAYS)) || 501180740Sdes (sc->WD_valid_config && (sc->WD_hide_expose == 502180740Sdes MPS_WD_HIDE_IF_VOLUME))) { 503180740Sdes targ->flags |= MPS_TARGET_FLAGS_RAID_COMPONENT; 504180740Sdes printf("%s %d: WD: Found Target for handle 0x%x. \n", 505180740Sdes __func__, __LINE__ , event_data->PhysDiskDevHandle); 506180740Sdes } 507180740Sdes } 508180740Sdes break; 509180740Sdes case MPI2_RAID_PD_STATE_OFFLINE: 510180740Sdes case MPI2_RAID_PD_STATE_NOT_CONFIGURED: 511180740Sdes case MPI2_RAID_PD_STATE_NOT_COMPATIBLE: 512180740Sdes default: 513180740Sdes targ = mpssas_find_target_by_handle(sassc, 0, 514180740Sdes event_data->PhysDiskDevHandle); 515180740Sdes if (targ) { 516180740Sdes targ->flags |= ~MPS_TARGET_FLAGS_RAID_COMPONENT; 517180740Sdes printf("%s %d: Found Target for handle 0x%x. \n", 518180740Sdes __func__, __LINE__ , event_data->PhysDiskDevHandle); 519180740Sdes } 520180740Sdes break; 521180740Sdes } 522180740Sdes default: 523180740Sdes break; 524180740Sdes } 525180740Sdes break; 526180740Sdes } 527180740Sdes case MPI2_EVENT_IR_OPERATION_STATUS: 528180740Sdes { 529180740Sdes Mpi2EventDataIrOperationStatus_t *event_data = 530180740Sdes fw_event->event_data; 531180740Sdes 532180740Sdes /* 533180740Sdes * Informational only. 534180740Sdes */ 535180740Sdes mps_dprint(sc, MPS_EVENT, "Received IR Op Status event:\n"); 536180740Sdes mps_dprint(sc, MPS_EVENT, " RAID Operation of %d is %d " 537180740Sdes "percent complete for Volume with handle 0x%x", 538180740Sdes event_data->RAIDOperation, event_data->PercentComplete, 539180740Sdes le16toh(event_data->VolDevHandle)); 540180740Sdes break; 541180740Sdes } 542180740Sdes case MPI2_EVENT_LOG_ENTRY_ADDED: 543180740Sdes { 544180740Sdes pMpi2EventDataLogEntryAdded_t logEntry; 545180740Sdes uint16_t logQualifier; 546180740Sdes uint8_t logCode; 547180740Sdes 548180740Sdes logEntry = (pMpi2EventDataLogEntryAdded_t)fw_event->event_data; 549180740Sdes logQualifier = logEntry->LogEntryQualifier; 550180740Sdes 551180740Sdes if (logQualifier == MPI2_WD_LOG_ENTRY) { 552180740Sdes logCode = logEntry->LogData[0]; 553180740Sdes 554180740Sdes switch (logCode) { 555180740Sdes case MPI2_WD_SSD_THROTTLING: 556180740Sdes printf("WarpDrive Warning: IO Throttling has " 557180740Sdes "occurred in the WarpDrive subsystem. " 558180740Sdes "Check WarpDrive documentation for " 559180740Sdes "additional details\n"); 560180740Sdes break; 561180740Sdes case MPI2_WD_DRIVE_LIFE_WARN: 562180740Sdes printf("WarpDrive Warning: Program/Erase " 563180740Sdes "Cycles for the WarpDrive subsystem in " 564180740Sdes "degraded range. Check WarpDrive " 565180740Sdes "documentation for additional details\n"); 566180740Sdes break; 567180740Sdes case MPI2_WD_DRIVE_LIFE_DEAD: 568180740Sdes printf("WarpDrive Fatal Error: There are no " 569180740Sdes "Program/Erase Cycles for the WarpDrive " 570180740Sdes "subsystem. The storage device will be in " 571180740Sdes "read-only mode. Check WarpDrive " 572180740Sdes "documentation for additional details\n"); 573180740Sdes break; 574180740Sdes case MPI2_WD_RAIL_MON_FAIL: 575180740Sdes printf("WarpDrive Fatal Error: The Backup Rail " 576180740Sdes "Monitor has failed on the WarpDrive " 577180740Sdes "subsystem. Check WarpDrive documentation " 578180740Sdes "for additional details\n"); 579180740Sdes break; 580180740Sdes default: 581180740Sdes break; 582180740Sdes } 583180740Sdes } 584180740Sdes break; 585180740Sdes } 586180740Sdes case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE: 587180740Sdes case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE: 588180740Sdes default: 589180740Sdes mps_dprint(sc, MPS_TRACE,"Unhandled event 0x%0X\n", 590180740Sdes fw_event->event); 591180740Sdes break; 592180740Sdes 593180740Sdes } 594180740Sdes mps_dprint(sc, MPS_EVENT, "(%d)->(%s) Event Free: [%x]\n",event_count,__func__, fw_event->event); 595180740Sdes mpssas_fw_event_free(sc, fw_event); 596180740Sdes} 597180740Sdes 598180740Sdesvoid 599180740Sdesmpssas_firmware_event_work(void *arg, int pending) 600180740Sdes{ 601180740Sdes struct mps_fw_event_work *fw_event; 602180740Sdes struct mps_softc *sc; 603180740Sdes 604180740Sdes sc = (struct mps_softc *)arg; 605180740Sdes mps_lock(sc); 606180740Sdes while ((fw_event = TAILQ_FIRST(&sc->sassc->ev_queue)) != NULL) { 607180740Sdes TAILQ_REMOVE(&sc->sassc->ev_queue, fw_event, ev_link); 608180740Sdes mpssas_fw_work(sc, fw_event); 609180740Sdes } 610180740Sdes mps_unlock(sc); 611180740Sdes} 612180740Sdes 613180740Sdesstatic int 614180740Sdesmpssas_add_device(struct mps_softc *sc, u16 handle, u8 linkrate){ 615180740Sdes char devstring[80]; 616180740Sdes struct mpssas_softc *sassc; 617180740Sdes struct mpssas_target *targ; 618180740Sdes Mpi2ConfigReply_t mpi_reply; 619180740Sdes Mpi2SasDevicePage0_t config_page; 620180740Sdes uint64_t sas_address; 621180740Sdes uint64_t parent_sas_address = 0; 622180740Sdes u32 device_info, parent_devinfo = 0; 623180740Sdes unsigned int id; 624180740Sdes int ret = 1, error = 0, i; 625180740Sdes struct mpssas_lun *lun; 626180740Sdes u8 is_SATA_SSD = 0; 627180740Sdes struct mps_command *cm; 628180740Sdes 629180740Sdes sassc = sc->sassc; 630180740Sdes mpssas_startup_increment(sassc); 631180740Sdes if ((mps_config_get_sas_device_pg0(sc, &mpi_reply, &config_page, 632180740Sdes MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) { 633180740Sdes printf("%s: error reading SAS device page0\n", __func__); 634180740Sdes error = ENXIO; 635180740Sdes goto out; 636180740Sdes } 637180740Sdes 638180740Sdes device_info = le32toh(config_page.DeviceInfo); 639180740Sdes 640180740Sdes if (((device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) 641180740Sdes && (le16toh(config_page.ParentDevHandle) != 0)) { 642180740Sdes Mpi2ConfigReply_t tmp_mpi_reply; 643180740Sdes Mpi2SasDevicePage0_t parent_config_page; 644180740Sdes 645180740Sdes if ((mps_config_get_sas_device_pg0(sc, &tmp_mpi_reply, 646180740Sdes &parent_config_page, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, 647180740Sdes le16toh(config_page.ParentDevHandle)))) { 648180740Sdes printf("%s: error reading SAS device %#x page0\n", 649180740Sdes __func__, le16toh(config_page.ParentDevHandle)); 650180740Sdes } else { 651180740Sdes parent_sas_address = parent_config_page.SASAddress.High; 652180740Sdes parent_sas_address = (parent_sas_address << 32) | 653180740Sdes parent_config_page.SASAddress.Low; 654180740Sdes parent_devinfo = le32toh(parent_config_page.DeviceInfo); 655180740Sdes } 656180740Sdes } 657180740Sdes /* TODO Check proper endianess */ 658180740Sdes sas_address = config_page.SASAddress.High; 659180740Sdes sas_address = (sas_address << 32) | config_page.SASAddress.Low; 660180740Sdes 661180740Sdes /* 662180740Sdes * Always get SATA Identify information because this is used to 663180740Sdes * determine if Start/Stop Unit should be sent to the drive when the 664180740Sdes * system is shutdown. 665180740Sdes */ 666180740Sdes if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) { 667180740Sdes ret = mpssas_get_sas_address_for_sata_disk(sc, &sas_address, 668180740Sdes handle, device_info, &is_SATA_SSD); 669180740Sdes if (ret) { 670180740Sdes mps_dprint(sc, MPS_INFO, "%s: failed to get disk type " 671180740Sdes "(SSD or HDD) for SATA device with handle 0x%04x\n", 672180740Sdes __func__, handle); 673180740Sdes } else { 674180740Sdes mps_dprint(sc, MPS_INFO, "SAS Address from SATA " 675180740Sdes "device = %jx\n", sas_address); 676180740Sdes } 677180740Sdes } 678180740Sdes 679180740Sdes /* 680180740Sdes * use_phynum: 681180740Sdes * 1 - use the PhyNum field as a fallback to the mapping logic 682180740Sdes * 0 - never use the PhyNum field 683180740Sdes * -1 - only use the PhyNum field 684180740Sdes * 685180740Sdes * Note that using the Phy number to map a device can cause device adds 686180740Sdes * to fail if multiple enclosures/expanders are in the topology. For 687180740Sdes * example, if two devices are in the same slot number in two different 688180740Sdes * enclosures within the topology, only one of those devices will be 689180740Sdes * added. PhyNum mapping should not be used if multiple enclosures are 690180740Sdes * in the topology. 691180740Sdes */ 692180740Sdes id = MPS_MAP_BAD_ID; 693180740Sdes if (sc->use_phynum != -1) 694180740Sdes id = mps_mapping_get_tid(sc, sas_address, handle); 695180740Sdes if (id == MPS_MAP_BAD_ID) { 696180740Sdes if ((sc->use_phynum == 0) 697180740Sdes || ((id = config_page.PhyNum) > sassc->maxtargets)) { 698180740Sdes mps_dprint(sc, MPS_INFO, "failure at %s:%d/%s()! " 699180740Sdes "Could not get ID for device with handle 0x%04x\n", 700180740Sdes __FILE__, __LINE__, __func__, handle); 701180740Sdes error = ENXIO; 702180740Sdes goto out; 703180740Sdes } 704180740Sdes } 705180740Sdes mps_dprint(sc, MPS_MAPPING, "%s: Target ID for added device is %d.\n", 706180740Sdes __func__, id); 707180740Sdes 708180740Sdes /* 709180740Sdes * Only do the ID check and reuse check if the target is not from a 710180740Sdes * RAID Component. For Physical Disks of a Volume, the ID will be reused 711225825Sdes * when a volume is deleted because the mapping entry for the PD will 712180740Sdes * still be in the mapping table. The ID check should not be done here 713180740Sdes * either since this PD is already being used. 714180740Sdes */ 715180740Sdes targ = &sassc->targets[id]; 716180740Sdes if (!(targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT)) { 717180740Sdes if (mpssas_check_id(sassc, id) != 0) { 718180740Sdes device_printf(sc->mps_dev, "Excluding target id %d\n", 719180740Sdes id); 720225825Sdes error = ENXIO; 721180740Sdes goto out; 722180740Sdes } 723180740Sdes 724180740Sdes if (targ->handle != 0x0) { 725180740Sdes mps_dprint(sc, MPS_MAPPING, "Attempting to reuse " 726180740Sdes "target id %d handle 0x%04x\n", id, targ->handle); 727180740Sdes error = ENXIO; 728180740Sdes goto out; 729180740Sdes } 730180740Sdes } 731180740Sdes 732180740Sdes mps_dprint(sc, MPS_MAPPING, "SAS Address from SAS device page0 = %jx\n", 733180740Sdes sas_address); 734180740Sdes targ->devinfo = device_info; 735180740Sdes targ->devname = le32toh(config_page.DeviceName.High); 736180740Sdes targ->devname = (targ->devname << 32) | 737180740Sdes le32toh(config_page.DeviceName.Low); 738180740Sdes targ->encl_handle = le16toh(config_page.EnclosureHandle); 739180740Sdes targ->encl_slot = le16toh(config_page.Slot); 740180740Sdes targ->handle = handle; 741180740Sdes targ->parent_handle = le16toh(config_page.ParentDevHandle); 742180740Sdes targ->sasaddr = mps_to_u64(&config_page.SASAddress); 743180740Sdes targ->parent_sasaddr = le64toh(parent_sas_address); 744180740Sdes targ->parent_devinfo = parent_devinfo; 745180740Sdes targ->tid = id; 746180740Sdes targ->linkrate = (linkrate>>4); 747180740Sdes targ->flags = 0; 748180740Sdes if (is_SATA_SSD) { 749180740Sdes targ->flags = MPS_TARGET_IS_SATA_SSD; 750180740Sdes } 751180740Sdes TAILQ_INIT(&targ->commands); 752180740Sdes TAILQ_INIT(&targ->timedout_commands); 753180740Sdes while(!SLIST_EMPTY(&targ->luns)) { 754180740Sdes lun = SLIST_FIRST(&targ->luns); 755180740Sdes SLIST_REMOVE_HEAD(&targ->luns, lun_link); 756180740Sdes free(lun, M_MPT2); 757180740Sdes } 758180740Sdes SLIST_INIT(&targ->luns); 759180740Sdes 760180740Sdes mps_describe_devinfo(targ->devinfo, devstring, 80); 761180740Sdes mps_dprint(sc, MPS_MAPPING, "Found device <%s> <%s> <0x%04x> <%d/%d>\n", 762180740Sdes devstring, mps_describe_table(mps_linkrate_names, targ->linkrate), 763180740Sdes targ->handle, targ->encl_handle, targ->encl_slot); 764180740Sdes 765180740Sdes#if __FreeBSD_version < 1000039 766180740Sdes if ((sassc->flags & MPSSAS_IN_STARTUP) == 0) 767180740Sdes#endif 768180740Sdes mpssas_rescan_target(sc, targ); 769180740Sdes mps_dprint(sc, MPS_MAPPING, "Target id 0x%x added\n", targ->tid); 770180740Sdes 771180740Sdes /* 772180740Sdes * Check all commands to see if the SATA_ID_TIMEOUT flag has been set. 773180740Sdes * If so, send a Target Reset TM to the target that was just created. 774180740Sdes * An Abort Task TM should be used instead of a Target Reset, but that 775180740Sdes * would be much more difficult because targets have not been fully 776180740Sdes * discovered yet, and LUN's haven't been setup. So, just reset the 777180740Sdes * target instead of the LUN. 778180740Sdes */ 779180740Sdes for (i = 1; i < sc->num_reqs; i++) { 780180740Sdes cm = &sc->commands[i]; 781180740Sdes if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) { 782180740Sdes targ->timeouts++; 783180740Sdes cm->cm_state = MPS_CM_STATE_TIMEDOUT; 784180740Sdes 785180740Sdes if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) { 786180740Sdes mps_dprint(sc, MPS_INFO, "%s: sending Target " 787180740Sdes "Reset for stuck SATA identify command " 788180740Sdes "(cm = %p)\n", __func__, cm); 789180740Sdes targ->tm->cm_targ = targ; 790180740Sdes mpssas_send_reset(sc, targ->tm, 791180740Sdes MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET); 792180740Sdes } else { 793180740Sdes mps_dprint(sc, MPS_ERROR, "Failed to allocate " 794180740Sdes "tm for Target Reset after SATA ID command " 795180740Sdes "timed out (cm %p)\n", cm); 796180740Sdes } 797180740Sdes /* 798180740Sdes * No need to check for more since the target is 799180740Sdes * already being reset. 800180740Sdes */ 801180740Sdes break; 802180740Sdes } 803180740Sdes } 804180740Sdesout: 805180740Sdes /* 806180740Sdes * Free the commands that may not have been freed from the SATA ID call 807180740Sdes */ 808180740Sdes for (i = 1; i < sc->num_reqs; i++) { 809 cm = &sc->commands[i]; 810 if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) { 811 mps_free_command(sc, cm); 812 } 813 } 814 mpssas_startup_decrement(sassc); 815 return (error); 816} 817 818int 819mpssas_get_sas_address_for_sata_disk(struct mps_softc *sc, 820 u64 *sas_address, u16 handle, u32 device_info, u8 *is_SATA_SSD) 821{ 822 Mpi2SataPassthroughReply_t mpi_reply; 823 int i, rc, try_count; 824 u32 *bufferptr; 825 union _sata_sas_address hash_address; 826 struct _ata_identify_device_data ata_identify; 827 u8 buffer[MPT2SAS_MN_LEN + MPT2SAS_SN_LEN]; 828 u32 ioc_status; 829 u8 sas_status; 830 831 memset(&ata_identify, 0, sizeof(ata_identify)); 832 try_count = 0; 833 do { 834 rc = mpssas_get_sata_identify(sc, handle, &mpi_reply, 835 (char *)&ata_identify, sizeof(ata_identify), device_info); 836 try_count++; 837 ioc_status = le16toh(mpi_reply.IOCStatus) 838 & MPI2_IOCSTATUS_MASK; 839 sas_status = mpi_reply.SASStatus; 840 switch (ioc_status) { 841 case MPI2_IOCSTATUS_SUCCESS: 842 break; 843 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR: 844 /* No sense sleeping. this error won't get better */ 845 break; 846 default: 847 if (sc->spinup_wait_time > 0) { 848 mps_dprint(sc, MPS_INFO, "Sleeping %d seconds " 849 "after SATA ID error to wait for spinup\n", 850 sc->spinup_wait_time); 851 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, 852 "mpsid", sc->spinup_wait_time * hz); 853 } 854 } 855 } while (((rc && (rc != EWOULDBLOCK)) || 856 (ioc_status && 857 (ioc_status != MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR)) 858 || sas_status) && (try_count < 5)); 859 860 if (rc == 0 && !ioc_status && !sas_status) { 861 mps_dprint(sc, MPS_MAPPING, "%s: got SATA identify " 862 "successfully for handle = 0x%x with try_count = %d\n", 863 __func__, handle, try_count); 864 } else { 865 mps_dprint(sc, MPS_MAPPING, "%s: handle = 0x%x failed\n", 866 __func__, handle); 867 return -1; 868 } 869 /* Copy & byteswap the 40 byte model number to a buffer */ 870 for (i = 0; i < MPT2SAS_MN_LEN; i += 2) { 871 buffer[i] = ((u8 *)ata_identify.model_number)[i + 1]; 872 buffer[i + 1] = ((u8 *)ata_identify.model_number)[i]; 873 } 874 /* Copy & byteswap the 20 byte serial number to a buffer */ 875 for (i = 0; i < MPT2SAS_SN_LEN; i += 2) { 876 buffer[MPT2SAS_MN_LEN + i] = 877 ((u8 *)ata_identify.serial_number)[i + 1]; 878 buffer[MPT2SAS_MN_LEN + i + 1] = 879 ((u8 *)ata_identify.serial_number)[i]; 880 } 881 bufferptr = (u32 *)buffer; 882 /* There are 60 bytes to hash down to 8. 60 isn't divisible by 8, 883 * so loop through the first 56 bytes (7*8), 884 * and then add in the last dword. 885 */ 886 hash_address.word.low = 0; 887 hash_address.word.high = 0; 888 for (i = 0; (i < ((MPT2SAS_MN_LEN+MPT2SAS_SN_LEN)/8)); i++) { 889 hash_address.word.low += *bufferptr; 890 bufferptr++; 891 hash_address.word.high += *bufferptr; 892 bufferptr++; 893 } 894 /* Add the last dword */ 895 hash_address.word.low += *bufferptr; 896 /* Make sure the hash doesn't start with 5, because it could clash 897 * with a SAS address. Change 5 to a D. 898 */ 899 if ((hash_address.word.high & 0x000000F0) == (0x00000050)) 900 hash_address.word.high |= 0x00000080; 901 *sas_address = (u64)hash_address.wwid[0] << 56 | 902 (u64)hash_address.wwid[1] << 48 | (u64)hash_address.wwid[2] << 40 | 903 (u64)hash_address.wwid[3] << 32 | (u64)hash_address.wwid[4] << 24 | 904 (u64)hash_address.wwid[5] << 16 | (u64)hash_address.wwid[6] << 8 | 905 (u64)hash_address.wwid[7]; 906 if (ata_identify.rotational_speed == 1) { 907 *is_SATA_SSD = 1; 908 } 909 910 return 0; 911} 912 913static int 914mpssas_get_sata_identify(struct mps_softc *sc, u16 handle, 915 Mpi2SataPassthroughReply_t *mpi_reply, char *id_buffer, int sz, u32 devinfo) 916{ 917 Mpi2SataPassthroughRequest_t *mpi_request; 918 Mpi2SataPassthroughReply_t *reply = NULL; 919 struct mps_command *cm; 920 char *buffer; 921 int error = 0; 922 923 buffer = malloc( sz, M_MPT2, M_NOWAIT | M_ZERO); 924 if (!buffer) 925 return ENOMEM; 926 927 if ((cm = mps_alloc_command(sc)) == NULL) { 928 free(buffer, M_MPT2); 929 return (EBUSY); 930 } 931 mpi_request = (MPI2_SATA_PASSTHROUGH_REQUEST *)cm->cm_req; 932 bzero(mpi_request,sizeof(MPI2_SATA_PASSTHROUGH_REQUEST)); 933 mpi_request->Function = MPI2_FUNCTION_SATA_PASSTHROUGH; 934 mpi_request->VF_ID = 0; 935 mpi_request->DevHandle = htole16(handle); 936 mpi_request->PassthroughFlags = (MPI2_SATA_PT_REQ_PT_FLAGS_PIO | 937 MPI2_SATA_PT_REQ_PT_FLAGS_READ); 938 mpi_request->DataLength = htole32(sz); 939 mpi_request->CommandFIS[0] = 0x27; 940 mpi_request->CommandFIS[1] = 0x80; 941 mpi_request->CommandFIS[2] = (devinfo & 942 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? 0xA1 : 0xEC; 943 cm->cm_sge = &mpi_request->SGL; 944 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION); 945 cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN; 946 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 947 cm->cm_data = buffer; 948 cm->cm_length = htole32(sz); 949 950 /* 951 * Start a timeout counter specifically for the SATA ID command. This 952 * is used to fix a problem where the FW does not send a reply sometimes 953 * when a bad disk is in the topology. So, this is used to timeout the 954 * command so that processing can continue normally. 955 */ 956 mps_dprint(sc, MPS_XINFO, "%s start timeout counter for SATA ID " 957 "command\n", __func__); 958 callout_reset(&cm->cm_callout, MPS_ATA_ID_TIMEOUT * hz, 959 mpssas_ata_id_timeout, cm); 960 error = mps_wait_command(sc, &cm, 60, CAN_SLEEP); 961 mps_dprint(sc, MPS_XINFO, "%s stop timeout counter for SATA ID " 962 "command\n", __func__); 963 /* XXX KDM need to fix the case where this command is destroyed */ 964 callout_stop(&cm->cm_callout); 965 966 if (cm != NULL) 967 reply = (Mpi2SataPassthroughReply_t *)cm->cm_reply; 968 if (error || (reply == NULL)) { 969 /* FIXME */ 970 /* 971 * If the request returns an error then we need to do a diag 972 * reset 973 */ 974 printf("%s: request for page completed with error %d", 975 __func__, error); 976 error = ENXIO; 977 goto out; 978 } 979 bcopy(buffer, id_buffer, sz); 980 bcopy(reply, mpi_reply, sizeof(Mpi2SataPassthroughReply_t)); 981 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) != 982 MPI2_IOCSTATUS_SUCCESS) { 983 printf("%s: error reading SATA PASSTHRU; iocstatus = 0x%x\n", 984 __func__, reply->IOCStatus); 985 error = ENXIO; 986 goto out; 987 } 988out: 989 /* 990 * If the SATA_ID_TIMEOUT flag has been set for this command, don't free 991 * it. The command will be freed after sending a target reset TM. If 992 * the command did timeout, use EWOULDBLOCK. 993 */ 994 if ((cm != NULL) 995 && (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) == 0) 996 mps_free_command(sc, cm); 997 else if (error == 0) 998 error = EWOULDBLOCK; 999 free(buffer, M_MPT2); 1000 return (error); 1001} 1002 1003static void 1004mpssas_ata_id_timeout(void *data) 1005{ 1006 struct mps_softc *sc; 1007 struct mps_command *cm; 1008 1009 cm = (struct mps_command *)data; 1010 sc = cm->cm_sc; 1011 mtx_assert(&sc->mps_mtx, MA_OWNED); 1012 1013 mps_dprint(sc, MPS_INFO, "%s checking ATA ID command %p sc %p\n", 1014 __func__, cm, sc); 1015 if ((callout_pending(&cm->cm_callout)) || 1016 (!callout_active(&cm->cm_callout))) { 1017 mps_dprint(sc, MPS_INFO, "%s ATA ID command almost timed out\n", 1018 __func__); 1019 return; 1020 } 1021 callout_deactivate(&cm->cm_callout); 1022 1023 /* 1024 * Run the interrupt handler to make sure it's not pending. This 1025 * isn't perfect because the command could have already completed 1026 * and been re-used, though this is unlikely. 1027 */ 1028 mps_intr_locked(sc); 1029 if (cm->cm_state == MPS_CM_STATE_FREE) { 1030 mps_dprint(sc, MPS_INFO, "%s ATA ID command almost timed out\n", 1031 __func__); 1032 return; 1033 } 1034 1035 mps_dprint(sc, MPS_INFO, "ATA ID command timeout cm %p\n", cm); 1036 1037 /* 1038 * Send wakeup() to the sleeping thread that issued this ATA ID command. 1039 * wakeup() will cause msleep to return a 0 (not EWOULDBLOCK), and this 1040 * will keep reinit() from being called. This way, an Abort Task TM can 1041 * be issued so that the timed out command can be cleared. The Abort 1042 * Task cannot be sent from here because the driver has not completed 1043 * setting up targets. Instead, the command is flagged so that special 1044 * handling will be used to send the abort. 1045 */ 1046 cm->cm_flags |= MPS_CM_FLAGS_SATA_ID_TIMEOUT; 1047 wakeup(cm); 1048} 1049 1050static int 1051mpssas_volume_add(struct mps_softc *sc, u16 handle) 1052{ 1053 struct mpssas_softc *sassc; 1054 struct mpssas_target *targ; 1055 u64 wwid; 1056 unsigned int id; 1057 int error = 0; 1058 struct mpssas_lun *lun; 1059 1060 sassc = sc->sassc; 1061 mpssas_startup_increment(sassc); 1062 /* wwid is endian safe */ 1063 mps_config_get_volume_wwid(sc, handle, &wwid); 1064 if (!wwid) { 1065 printf("%s: invalid WWID; cannot add volume to mapping table\n", 1066 __func__); 1067 error = ENXIO; 1068 goto out; 1069 } 1070 1071 id = mps_mapping_get_raid_tid(sc, wwid, handle); 1072 if (id == MPS_MAP_BAD_ID) { 1073 printf("%s: could not get ID for volume with handle 0x%04x and " 1074 "WWID 0x%016llx\n", __func__, handle, 1075 (unsigned long long)wwid); 1076 error = ENXIO; 1077 goto out; 1078 } 1079 1080 targ = &sassc->targets[id]; 1081 targ->tid = id; 1082 targ->handle = handle; 1083 targ->devname = wwid; 1084 TAILQ_INIT(&targ->commands); 1085 TAILQ_INIT(&targ->timedout_commands); 1086 while(!SLIST_EMPTY(&targ->luns)) { 1087 lun = SLIST_FIRST(&targ->luns); 1088 SLIST_REMOVE_HEAD(&targ->luns, lun_link); 1089 free(lun, M_MPT2); 1090 } 1091 SLIST_INIT(&targ->luns); 1092#if __FreeBSD_version < 1000039 1093 if ((sassc->flags & MPSSAS_IN_STARTUP) == 0) 1094#endif 1095 mpssas_rescan_target(sc, targ); 1096 mps_dprint(sc, MPS_MAPPING, "RAID target id %d added (WWID = 0x%jx)\n", 1097 targ->tid, wwid); 1098out: 1099 mpssas_startup_decrement(sassc); 1100 return (error); 1101} 1102 1103/** 1104 * mpssas_SSU_to_SATA_devices 1105 * @sc: per adapter object 1106 * 1107 * Looks through the target list and issues a StartStopUnit SCSI command to each 1108 * SATA direct-access device. This helps to ensure that data corruption is 1109 * avoided when the system is being shut down. This must be called after the IR 1110 * System Shutdown RAID Action is sent if in IR mode. 1111 * 1112 * Return nothing. 1113 */ 1114static void 1115mpssas_SSU_to_SATA_devices(struct mps_softc *sc) 1116{ 1117 struct mpssas_softc *sassc = sc->sassc; 1118 union ccb *ccb; 1119 path_id_t pathid = cam_sim_path(sassc->sim); 1120 target_id_t targetid; 1121 struct mpssas_target *target; 1122 char path_str[64]; 1123 struct timeval cur_time, start_time; 1124 1125 /* 1126 * For each target, issue a StartStopUnit command to stop the device. 1127 */ 1128 sc->SSU_started = TRUE; 1129 sc->SSU_refcount = 0; 1130 for (targetid = 0; targetid < sc->max_devices; targetid++) { 1131 target = &sassc->targets[targetid]; 1132 if (target->handle == 0x0) { 1133 continue; 1134 } 1135 1136 ccb = xpt_alloc_ccb_nowait(); 1137 if (ccb == NULL) { 1138 mps_dprint(sc, MPS_FAULT, "Unable to alloc CCB to stop " 1139 "unit.\n"); 1140 return; 1141 } 1142 1143 /* 1144 * The stop_at_shutdown flag will be set if this device is 1145 * a SATA direct-access end device. 1146 */ 1147 if (target->stop_at_shutdown) { 1148 if (xpt_create_path(&ccb->ccb_h.path, 1149 xpt_periph, pathid, targetid, 1150 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1151 mps_dprint(sc, MPS_FAULT, "Unable to create " 1152 "LUN path to stop unit.\n"); 1153 xpt_free_ccb(ccb); 1154 return; 1155 } 1156 xpt_path_string(ccb->ccb_h.path, path_str, 1157 sizeof(path_str)); 1158 1159 mps_dprint(sc, MPS_INFO, "Sending StopUnit: path %s " 1160 "handle %d\n", path_str, target->handle); 1161 1162 /* 1163 * Issue a START STOP UNIT command for the target. 1164 * Increment the SSU counter to be used to count the 1165 * number of required replies. 1166 */ 1167 mps_dprint(sc, MPS_INFO, "Incrementing SSU count\n"); 1168 sc->SSU_refcount++; 1169 ccb->ccb_h.target_id = 1170 xpt_path_target_id(ccb->ccb_h.path); 1171 ccb->ccb_h.ppriv_ptr1 = sassc; 1172 scsi_start_stop(&ccb->csio, 1173 /*retries*/0, 1174 mpssas_stop_unit_done, 1175 MSG_SIMPLE_Q_TAG, 1176 /*start*/FALSE, 1177 /*load/eject*/0, 1178 /*immediate*/FALSE, 1179 MPS_SENSE_LEN, 1180 /*timeout*/10000); 1181 xpt_action(ccb); 1182 } 1183 } 1184 1185 /* 1186 * Wait until all of the SSU commands have completed or time has 1187 * expired (60 seconds). Pause for 100ms each time through. If any 1188 * command times out, the target will be reset in the SCSI command 1189 * timeout routine. 1190 */ 1191 getmicrotime(&start_time); 1192 while (sc->SSU_refcount) { 1193 pause("mpswait", hz/10); 1194 1195 getmicrotime(&cur_time); 1196 if ((cur_time.tv_sec - start_time.tv_sec) > 60) { 1197 mps_dprint(sc, MPS_FAULT, "Time has expired waiting " 1198 "for SSU commands to complete.\n"); 1199 break; 1200 } 1201 } 1202} 1203 1204static void 1205mpssas_stop_unit_done(struct cam_periph *periph, union ccb *done_ccb) 1206{ 1207 struct mpssas_softc *sassc; 1208 char path_str[64]; 1209 1210 if (done_ccb == NULL) 1211 return; 1212 1213 sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1; 1214 1215 xpt_path_string(done_ccb->ccb_h.path, path_str, sizeof(path_str)); 1216 mps_dprint(sassc->sc, MPS_INFO, "Completing stop unit for %s\n", 1217 path_str); 1218 1219 /* 1220 * Nothing more to do except free the CCB and path. If the command 1221 * timed out, an abort reset, then target reset will be issued during 1222 * the SCSI Command process. 1223 */ 1224 xpt_free_path(done_ccb->ccb_h.path); 1225 xpt_free_ccb(done_ccb); 1226} 1227 1228/** 1229 * mpssas_ir_shutdown - IR shutdown notification 1230 * @sc: per adapter object 1231 * 1232 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that 1233 * the host system is shutting down. 1234 * 1235 * Return nothing. 1236 */ 1237void 1238mpssas_ir_shutdown(struct mps_softc *sc) 1239{ 1240 u16 volume_mapping_flags; 1241 u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags); 1242 struct dev_mapping_table *mt_entry; 1243 u32 start_idx, end_idx; 1244 unsigned int id, found_volume = 0; 1245 struct mps_command *cm; 1246 Mpi2RaidActionRequest_t *action; 1247 target_id_t targetid; 1248 struct mpssas_target *target; 1249 1250 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 1251 1252 /* is IR firmware build loaded? */ 1253 if (!sc->ir_firmware) 1254 goto out; 1255 1256 /* are there any volumes? Look at IR target IDs. */ 1257 // TODO-later, this should be looked up in the RAID config structure 1258 // when it is implemented. 1259 volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) & 1260 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE; 1261 if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) { 1262 start_idx = 0; 1263 if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0) 1264 start_idx = 1; 1265 } else 1266 start_idx = sc->max_devices - sc->max_volumes; 1267 end_idx = start_idx + sc->max_volumes - 1; 1268 1269 for (id = start_idx; id < end_idx; id++) { 1270 mt_entry = &sc->mapping_table[id]; 1271 if ((mt_entry->physical_id != 0) && 1272 (mt_entry->missing_count == 0)) { 1273 found_volume = 1; 1274 break; 1275 } 1276 } 1277 1278 if (!found_volume) 1279 goto out; 1280 1281 if ((cm = mps_alloc_command(sc)) == NULL) { 1282 printf("%s: command alloc failed\n", __func__); 1283 goto out; 1284 } 1285 1286 action = (MPI2_RAID_ACTION_REQUEST *)cm->cm_req; 1287 action->Function = MPI2_FUNCTION_RAID_ACTION; 1288 action->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED; 1289 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 1290 mps_lock(sc); 1291 mps_wait_command(sc, &cm, 5, CAN_SLEEP); 1292 mps_unlock(sc); 1293 1294 /* 1295 * Don't check for reply, just leave. 1296 */ 1297 if (cm) 1298 mps_free_command(sc, cm); 1299 1300out: 1301 /* 1302 * All of the targets must have the correct value set for 1303 * 'stop_at_shutdown' for the current 'enable_ssu' sysctl variable. 1304 * 1305 * The possible values for the 'enable_ssu' variable are: 1306 * 0: disable to SSD and HDD 1307 * 1: disable only to HDD (default) 1308 * 2: disable only to SSD 1309 * 3: enable to SSD and HDD 1310 * anything else will default to 1. 1311 */ 1312 for (targetid = 0; targetid < sc->max_devices; targetid++) { 1313 target = &sc->sassc->targets[targetid]; 1314 if (target->handle == 0x0) { 1315 continue; 1316 } 1317 1318 if (target->supports_SSU) { 1319 switch (sc->enable_ssu) { 1320 case MPS_SSU_DISABLE_SSD_DISABLE_HDD: 1321 target->stop_at_shutdown = FALSE; 1322 break; 1323 case MPS_SSU_DISABLE_SSD_ENABLE_HDD: 1324 target->stop_at_shutdown = TRUE; 1325 if (target->flags & MPS_TARGET_IS_SATA_SSD) { 1326 target->stop_at_shutdown = FALSE; 1327 } 1328 break; 1329 case MPS_SSU_ENABLE_SSD_ENABLE_HDD: 1330 target->stop_at_shutdown = TRUE; 1331 break; 1332 case MPS_SSU_ENABLE_SSD_DISABLE_HDD: 1333 default: 1334 target->stop_at_shutdown = TRUE; 1335 if ((target->flags & 1336 MPS_TARGET_IS_SATA_SSD) == 0) { 1337 target->stop_at_shutdown = FALSE; 1338 } 1339 break; 1340 } 1341 } 1342 } 1343 mpssas_SSU_to_SATA_devices(sc); 1344} 1345