Blame src/ahci.c

Packit 7e09eb
/*
Packit 7e09eb
 * Intel(R) Enclosure LED Utilities
Packit 7e09eb
 * Copyright (C) 2009-2019 Intel Corporation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is free software; you can redistribute it and/or modify it
Packit 7e09eb
 * under the terms and conditions of the GNU General Public License,
Packit 7e09eb
 * version 2, as published by the Free Software Foundation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit 7e09eb
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 7e09eb
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit 7e09eb
 * more details.
Packit 7e09eb
 *
Packit 7e09eb
 * You should have received a copy of the GNU General Public License along with
Packit 7e09eb
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit 7e09eb
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit 7e09eb
 *
Packit 7e09eb
 */
Packit 7e09eb
Packit 7e09eb
#include <errno.h>
Packit 7e09eb
#include <limits.h>
Packit 7e09eb
#include <stdint.h>
Packit 7e09eb
#include <stdio.h>
Packit 7e09eb
#include <stdlib.h>
Packit 7e09eb
#include <string.h>
Packit 7e09eb
#include <unistd.h>
Packit 7e09eb
Packit 7e09eb
#if _HAVE_DMALLOC_H
Packit 7e09eb
#include <dmalloc.h>
Packit 7e09eb
#endif
Packit 7e09eb
Packit 7e09eb
#include "ahci.h"
Packit 7e09eb
#include "config.h"
Packit 7e09eb
#include "utils.h"
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 * Time interval in nano seconds to wait before enclosure management message
Packit 7e09eb
 * is being sent to AHCI controller.
Packit 7e09eb
 */
Packit 7e09eb
#define EM_MSG_WAIT       1500000	/* 0.0015 seconds */
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 * This array maps IBPI pattern to value recognized by AHCI driver. The driver
Packit 7e09eb
 * uses this control number to issue SGPIO signals appropriately.
Packit 7e09eb
 */
Packit 7e09eb
static const unsigned int ibpi2sgpio[] = {
Packit 7e09eb
	[IBPI_PATTERN_REBUILD]        = 0x00480000,
Packit 7e09eb
	[IBPI_PATTERN_FAILED_DRIVE]   = 0x00400000,
Packit 7e09eb
	[IBPI_PATTERN_LOCATE]         = 0x00080000,
Packit 7e09eb
	[IBPI_PATTERN_UNKNOWN]        = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_ONESHOT_NORMAL] = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_NORMAL]         = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_LOCATE_OFF]     = 0x00000000,
Packit 7e09eb
#ifdef DEBUG_IBPI
Packit 7e09eb
	[IBPI_PATTERN_DEGRADED]       = 0x00200000,
Packit 7e09eb
	[IBPI_PATTERN_FAILED_ARRAY]   = 0x00280000,
Packit 7e09eb
	[IBPI_PATTERN_HOTSPARE]       = 0x01800000,
Packit 7e09eb
	[IBPI_PATTERN_PFA]            = 0x01400000
Packit 7e09eb
#else
Packit 7e09eb
	[IBPI_PATTERN_DEGRADED]       = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_FAILED_ARRAY]   = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_HOTSPARE]       = 0x00000000,
Packit 7e09eb
	[IBPI_PATTERN_PFA]            = 0x00000000
Packit 7e09eb
#endif
Packit 7e09eb
};
Packit 7e09eb
Packit 7e09eb
/*
Packit 7e09eb
 * The function sends a LED control message to AHCI controller. It uses
Packit 7e09eb
 * SGPIO to control the LEDs. See ahci.h for details.
Packit 7e09eb
 */
Packit 7e09eb
int ahci_sgpio_write(struct block_device *device, enum ibpi_pattern ibpi)
Packit 7e09eb
{
Packit 7e09eb
	char temp[WRITE_BUFFER_SIZE];
Packit 7e09eb
	char path[PATH_MAX];
Packit 7e09eb
	char *sysfs_path = device->cntrl_path;
Packit 7e09eb
	const struct timespec waittime = {
Packit 7e09eb
		.tv_sec = 0,
Packit 7e09eb
		.tv_nsec = EM_MSG_WAIT
Packit 7e09eb
	};
Packit 7e09eb
Packit 7e09eb
	/* write only if state has changed */
Packit 7e09eb
	if (ibpi == device->ibpi_prev)
Packit 7e09eb
		return 1;
Packit 7e09eb
Packit 7e09eb
	if (sysfs_path == NULL)
Packit 7e09eb
		__set_errno_and_return(EINVAL);
Packit 7e09eb
	if ((ibpi < IBPI_PATTERN_NORMAL) || (ibpi > IBPI_PATTERN_LOCATE_OFF))
Packit 7e09eb
		__set_errno_and_return(ERANGE);
Packit 7e09eb
Packit 7e09eb
	sprintf(temp, "%u", ibpi2sgpio[ibpi]);
Packit 7e09eb
Packit 7e09eb
	snprintf(path, sizeof(path), "%s/em_message", sysfs_path);
Packit 7e09eb
Packit 7e09eb
	nanosleep(&waittime, NULL);
Packit 7e09eb
	return buf_write(path, temp) > 0;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
#define SCSI_HOST "/scsi_host"
Packit 7e09eb
/*
Packit 7e09eb
 * The function return path to SATA port in sysfs tree. See ahci.h for details.
Packit 7e09eb
 */
Packit 7e09eb
Packit 7e09eb
char *ahci_get_port_path(const char *path)
Packit 7e09eb
{
Packit 7e09eb
	char *p;
Packit 7e09eb
	char tmp[PATH_MAX];
Packit 7e09eb
	char *buf;
Packit 7e09eb
	size_t buf_size;
Packit 7e09eb
Packit 7e09eb
	p = strstr(path, "/target");
Packit 7e09eb
	if (p == NULL)
Packit 7e09eb
		return NULL;
Packit 7e09eb
Packit 7e09eb
	if (sizeof(tmp) <= (p - path))
Packit 7e09eb
		return NULL;
Packit 7e09eb
	strncpy(tmp, path, p - path);
Packit 7e09eb
	tmp[p - path] = '\0';
Packit 7e09eb
	p = strrchr(tmp, PATH_DELIM);
Packit 7e09eb
	if (p == NULL)
Packit 7e09eb
		return NULL;
Packit 7e09eb
Packit 7e09eb
	buf_size = strlen(tmp) + strlen(p) + strlen(SCSI_HOST) + 1;
Packit 7e09eb
	buf = malloc(buf_size);
Packit 7e09eb
	if (buf == NULL)
Packit 7e09eb
		return NULL;
Packit 7e09eb
Packit 7e09eb
	snprintf(buf, buf_size, "%s%s%s", tmp, SCSI_HOST, p);
Packit 7e09eb
	return buf;
Packit 7e09eb
}