2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/scsi/scsi_address.h>
2N/A#include <sys/scsi/impl/usmp.h>
2N/A#include <sys/libdevid.h>
2N/A
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <stdio.h>
2N/A#include <limits.h>
2N/A
2N/A#include <scsi/libsmp.h>
2N/A#include <scsi/libsmp_plugin.h>
2N/A
2N/A#include <libdevinfo.h>
2N/A
2N/Astruct usmp_dev {
2N/A int ud_fd;
2N/A char *ud_dev;
2N/A uint64_t ud_addr;
2N/A};
2N/A
2N/Astruct di_walk_arg {
2N/A dev_t dev;
2N/A uint64_t addr;
2N/A};
2N/A
2N/Astatic int
2N/Adi_walk(di_node_t node, di_minor_t minor, void *arg)
2N/A{
2N/A struct di_walk_arg *wp = arg;
2N/A char *wwn;
2N/A
2N/A if (di_minor_spectype(minor) != S_IFCHR)
2N/A return (DI_WALK_CONTINUE);
2N/A
2N/A if (di_minor_devt(minor) == wp->dev) {
2N/A if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2N/A SCSI_ADDR_PROP_TARGET_PORT, &wwn) != 1 &&
2N/A di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2N/A "smp-wwn", &wwn) != 1)
2N/A return (DI_WALK_CONTINUE);
2N/A
2N/A if (scsi_wwnstr_to_wwn(wwn, &wp->addr) != DDI_SUCCESS)
2N/A return (DI_WALK_CONTINUE);
2N/A
2N/A return (DI_WALK_TERMINATE);
2N/A }
2N/A
2N/A return (DI_WALK_CONTINUE);
2N/A}
2N/A
2N/Astatic void *
2N/Ausmp_open(const void *target)
2N/A{
2N/A struct usmp_dev *dp;
2N/A const char *target_name = (const char *)target;
2N/A
2N/A struct stat64 st;
2N/A di_node_t root, smp;
2N/A struct di_walk_arg walk;
2N/A
2N/A if ((dp = smp_zalloc(sizeof (struct usmp_dev))) == NULL)
2N/A return (NULL);
2N/A
2N/A if ((dp->ud_dev = smp_strdup(target_name)) == NULL) {
2N/A smp_free(dp);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((dp->ud_fd = open(target_name, O_RDONLY)) < 0) {
2N/A (void) smp_error(ESMP_BADTARGET,
2N/A "failed to open %s for reading: %s",
2N/A target_name, strerror(errno));
2N/A smp_free(dp->ud_dev);
2N/A smp_free(dp);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (fstat64(dp->ud_fd, &st) != 0) {
2N/A (void) smp_error(ESMP_BADTARGET,
2N/A "failed to stat %s: %s", target_name, strerror(errno));
2N/A (void) close(dp->ud_fd);
2N/A smp_free(dp->ud_dev);
2N/A smp_free(dp);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((root = di_init("/", DINFOCACHE)) != DI_NODE_NIL) {
2N/A for (smp = di_drv_first_node("smp", root); smp != DI_NODE_NIL;
2N/A smp = di_drv_next_node(smp)) {
2N/A bzero(&walk, sizeof (walk));
2N/A walk.dev = st.st_rdev;
2N/A (void) di_walk_minor(smp, NULL, 0, &walk, di_walk);
2N/A if (walk.addr != 0) {
2N/A dp->ud_addr = walk.addr;
2N/A break;
2N/A }
2N/A }
2N/A di_fini(root);
2N/A }
2N/A
2N/A return (dp);
2N/A}
2N/A
2N/Astatic void
2N/Ausmp_close(void *private)
2N/A{
2N/A struct usmp_dev *dp = (struct usmp_dev *)private;
2N/A
2N/A if (dp == NULL)
2N/A return;
2N/A
2N/A if (dp->ud_fd > 0)
2N/A (void) close(dp->ud_fd);
2N/A
2N/A smp_free(dp->ud_dev);
2N/A smp_free(dp);
2N/A}
2N/A
2N/Astatic int
2N/Ausmp_exec(void *private, smp_action_t *ap)
2N/A{
2N/A struct usmp_dev *dp = (struct usmp_dev *)private;
2N/A struct usmp_cmd cmd;
2N/A void *req, *resp;
2N/A size_t reqlen, resplen;
2N/A
2N/A bzero(&cmd, sizeof (cmd));
2N/A
2N/A smp_action_get_request_frame(ap, &req, &reqlen);
2N/A smp_action_get_response_frame(ap, &resp, &resplen);
2N/A
2N/A ASSERT(req != NULL);
2N/A ASSERT(resp != NULL);
2N/A ASSERT(reqlen != 0);
2N/A ASSERT(resplen != 0);
2N/A
2N/A cmd.usmp_req = req;
2N/A cmd.usmp_reqsize = reqlen;
2N/A cmd.usmp_rsp = resp;
2N/A cmd.usmp_rspsize = resplen;
2N/A cmd.usmp_timeout = (int)smp_action_get_timeout(ap);
2N/A
2N/A if (ioctl(dp->ud_fd, USMPFUNC, &cmd) < 0) {
2N/A ASSERT(errno != EFAULT);
2N/A switch (errno) {
2N/A case EINVAL:
2N/A return (smp_error(ESMP_BADFUNC, "internal usmp error"));
2N/A case EPERM:
2N/A return (smp_error(ESMP_PERM,
2N/A "insufficient privileges"));
2N/A case EIO:
2N/A return (smp_error(ESMP_IO, "I/O error"));
2N/A default:
2N/A return (smp_error(ESMP_SYS, "usmp ioctl failed: %s",
2N/A strerror(errno)));
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * There is no way to determine the amount of data actually transferred
2N/A * so we will just place the upper bound at the allocated size.
2N/A */
2N/A smp_action_set_response_len(ap, resplen);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Ausmp_target_name(void *private, char *buf, size_t len)
2N/A{
2N/A struct usmp_dev *dp = (struct usmp_dev *)private;
2N/A
2N/A (void) strlcpy(buf, dp->ud_dev, len);
2N/A}
2N/A
2N/Astatic uint64_t
2N/Ausmp_target_addr(void *private)
2N/A{
2N/A struct usmp_dev *dp = (struct usmp_dev *)private;
2N/A
2N/A return (dp->ud_addr);
2N/A}
2N/A
2N/Astatic const smp_engine_ops_t usmp_ops = {
2N/A .seo_open = usmp_open,
2N/A .seo_close = usmp_close,
2N/A .seo_exec = usmp_exec,
2N/A .seo_target_name = usmp_target_name,
2N/A .seo_target_addr = usmp_target_addr
2N/A};
2N/A
2N/Aint
2N/A_smp_init(smp_engine_t *ep)
2N/A{
2N/A smp_engine_config_t config = {
2N/A .sec_name = "usmp",
2N/A .sec_ops = &usmp_ops
2N/A };
2N/A
2N/A return (smp_engine_register(ep, LIBSMP_ENGINE_VERSION, &config));
2N/A}