1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 #ifndef _LIBSCSI_H
  27 #define _LIBSCSI_H
  28 
  29 #ifdef  __cplusplus
  30 extern "C" {
  31 #endif
  32 
  33 #include <sys/types.h>
  34 #include <sys/sysmacros.h>
  35 #include <sys/scsi/impl/spc3_types.h>
  36 #include <stdarg.h>
  37 
  38 #define LIBSCSI_VERSION         1
  39 #define LIBSCSI_STATUS_INVALID  ((sam4_status_t)-1)
  40 #define LIBSCSI_DEFAULT_ENGINE_PATH     "/usr/lib/scsi/plugins/scsi/engines"
  41 #define LIBSCSI_DEFAULT_ENGINE  "uscsi"
  42 
  43 /*
  44  * Flags for action creation.  Selected to avoid overlap with the uscsi
  45  * flags with similar or identical meaning.
  46  */
  47 #define LIBSCSI_AF_READ         0x80000000
  48 #define LIBSCSI_AF_WRITE        0x40000000
  49 #define LIBSCSI_AF_SILENT       0x20000000
  50 #define LIBSCSI_AF_DIAGNOSE     0x10000000
  51 #define LIBSCSI_AF_ISOLATE      0x08000000
  52 #define LIBSCSI_AF_RQSENSE      0x04000000
  53 
  54 typedef enum libscsi_errno {
  55         ESCSI_NONE,             /* no error */
  56         ESCSI_NOMEM,            /* no memory */
  57         ESCSI_ZERO_LENGTH,      /* zero-length allocation requested */
  58         ESCSI_VERSION,          /* library version mismatch */
  59         ESCSI_BADTARGET,        /* invalid target specification */
  60         ESCSI_BADCMD,           /* invalid SCSI command */
  61         ESCSI_BADENGINE,        /* engine library corrupt */
  62         ESCSI_NOENGINE,         /* engine library not found */
  63         ESCSI_ENGINE_INIT,      /* engine initialization failed */
  64         ESCSI_ENGINE_VER,       /* engine version mismatch */
  65         ESCSI_ENGINE_BADPATH,   /* engine path contains no usable components */
  66         ESCSI_BADFLAGS,         /* incorrect action flags */
  67         ESCSI_BOGUSFLAGS,       /* unknown flag value */
  68         ESCSI_BADLENGTH,        /* buffer length overflow */
  69         ESCSI_NEEDBUF,          /* missing required buffer */
  70         ESCSI_IO,               /* I/O operation failed */
  71         ESCSI_SYS,              /* system call failed */
  72         ESCSI_PERM,             /* insufficient permissions */
  73         ESCSI_RANGE,            /* parameter outside valid range */
  74         ESCSI_NOTSUP,           /* operation not supported */
  75         ESCSI_UNKNOWN,          /* error of unknown type */
  76         ESCSI_INQUIRY_FAILED,   /* initial inquiry command failed */
  77         ESCSI_MAX               /* maximum libscsi errno value */
  78 } libscsi_errno_t;
  79 
  80 struct libscsi_hdl;
  81 typedef struct libscsi_hdl libscsi_hdl_t;
  82 
  83 struct libscsi_target;
  84 typedef struct libscsi_target libscsi_target_t;
  85 
  86 typedef struct libscsi_status {
  87         uint64_t lss_status;            /* SCSI status of this command */
  88         size_t lss_sense_len;           /* Length in bytes of sense data */
  89         uint8_t *lss_sense_data;        /* Pointer to sense data */
  90 } libscsi_status_t;
  91 
  92 struct libscsi_action;
  93 typedef struct libscsi_action libscsi_action_t;
  94 
  95 typedef struct libscsi_engine_ops {
  96         void *(*lseo_open)(libscsi_hdl_t *, const void *);
  97         void (*lseo_close)(libscsi_hdl_t *, void *);
  98         int (*lseo_exec)(libscsi_hdl_t *, void *, libscsi_action_t *);
  99         void (*lseo_target_name)(libscsi_hdl_t *, void *, char *, size_t);
 100 } libscsi_engine_ops_t;
 101 
 102 typedef struct libscsi_engine {
 103         const char *lse_name;
 104         uint_t lse_libversion;
 105         const libscsi_engine_ops_t *lse_ops;
 106 } libscsi_engine_t;
 107 
 108 extern libscsi_hdl_t *libscsi_init(uint_t, libscsi_errno_t *);
 109 extern void libscsi_fini(libscsi_hdl_t *);
 110 
 111 extern libscsi_target_t *libscsi_open(libscsi_hdl_t *, const char *,
 112     const void *);
 113 extern void libscsi_close(libscsi_hdl_t *, libscsi_target_t *);
 114 extern libscsi_hdl_t *libscsi_get_handle(libscsi_target_t *);
 115 
 116 extern const char *libscsi_vendor(libscsi_target_t *);
 117 extern const char *libscsi_product(libscsi_target_t *);
 118 extern const char *libscsi_revision(libscsi_target_t *);
 119 
 120 extern libscsi_errno_t libscsi_errno(libscsi_hdl_t *);
 121 extern const char *libscsi_errmsg(libscsi_hdl_t *);
 122 extern const char *libscsi_strerror(libscsi_errno_t);
 123 extern const char *libscsi_errname(libscsi_errno_t);
 124 extern libscsi_errno_t libscsi_errcode(const char *);
 125 
 126 extern libscsi_action_t *libscsi_action_alloc(libscsi_hdl_t *, spc3_cmd_t,
 127     uint_t, void *, size_t);
 128 extern sam4_status_t libscsi_action_get_status(const libscsi_action_t *);
 129 extern void libscsi_action_set_timeout(libscsi_action_t *, uint32_t);
 130 extern uint32_t libscsi_action_get_timeout(const libscsi_action_t *);
 131 extern uint_t libscsi_action_get_flags(const libscsi_action_t *);
 132 extern uint8_t *libscsi_action_get_cdb(const libscsi_action_t *);
 133 extern int libscsi_action_get_buffer(const libscsi_action_t *,
 134     uint8_t **, size_t *, size_t *);
 135 extern int libscsi_action_get_sense(const libscsi_action_t *,
 136     uint8_t **, size_t *, size_t *);
 137 extern int libscsi_action_parse_sense(const libscsi_action_t *, uint64_t *,
 138     uint64_t *, uint64_t *, diskaddr_t *);
 139 extern void libscsi_action_set_status(libscsi_action_t *, sam4_status_t);
 140 extern int libscsi_action_set_datalen(libscsi_action_t *, size_t);
 141 extern int libscsi_action_set_senselen(libscsi_action_t *, size_t);
 142 extern int libscsi_exec(libscsi_action_t *, libscsi_target_t *);
 143 extern void libscsi_action_free(libscsi_action_t *);
 144 
 145 extern const char *libscsi_sense_key_name(uint64_t);
 146 extern const char *libscsi_sense_code_name(uint64_t, uint64_t);
 147 
 148 /*
 149  * Interfaces for engine providers
 150  */
 151 extern void *libscsi_alloc(libscsi_hdl_t *, size_t);
 152 extern void *libscsi_zalloc(libscsi_hdl_t *, size_t);
 153 extern char *libscsi_strdup(libscsi_hdl_t *, const char *);
 154 extern void libscsi_free(libscsi_hdl_t *, void *);
 155 extern libscsi_status_t *libscsi_status_alloc(libscsi_hdl_t *, size_t);
 156 extern int libscsi_status_fill(libscsi_hdl_t *, libscsi_status_t *,
 157     uint16_t, size_t);
 158 extern void libscsi_status_free(libscsi_hdl_t *, libscsi_status_t *);
 159 
 160 extern int libscsi_set_errno(libscsi_hdl_t *, libscsi_errno_t);
 161 extern int libscsi_verror(libscsi_hdl_t *, libscsi_errno_t, const char *,
 162     va_list);
 163 extern int libscsi_error(libscsi_hdl_t *, libscsi_errno_t, const char *, ...);
 164 
 165 typedef const libscsi_engine_t *(*libscsi_engine_init_f)(libscsi_hdl_t *);
 166 
 167 /*
 168  * Generic SCSI utility functions.
 169  */
 170 extern size_t libscsi_cmd_cdblen(libscsi_hdl_t *, uint8_t);
 171 
 172 #ifdef  __cplusplus
 173 }
 174 #endif
 175 
 176 #endif  /* _LIBSCSI_H */