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 /*
27 * Copyright 2019 Joyent, Inc.
28 * Copyright 2025 Hans Rosenfeld
29 */
30
31 #include <sys/types.h>
32 #include <sys/scsi/generic/commands.h>
33 #include <sys/scsi/impl/spc3_types.h>
34
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <alloca.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <dlfcn.h>
43
44 #include <scsi/libscsi.h>
45 #include "libscsi_impl.h"
46
47 int
libscsi_assert(const char * expr,const char * file,int line)48 libscsi_assert(const char *expr, const char *file, int line)
49 {
50 char *msg;
51 size_t len;
52
53 len = snprintf(NULL, 0,
54 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
55
56 msg = alloca(len + 1);
57
58 (void) snprintf(msg, len + 1,
59 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
60
61 (void) write(STDERR_FILENO, msg, strlen(msg));
62
63 abort();
64 /*NOTREACHED*/
65 }
66
67 int
libscsi_set_errno(libscsi_hdl_t * hp,libscsi_errno_t err)68 libscsi_set_errno(libscsi_hdl_t *hp, libscsi_errno_t err)
69 {
70 hp->lsh_errno = err;
71 hp->lsh_errmsg[0] = '\0';
72
73 return (-1);
74 }
75
76 /*
77 * Internal routine for setting both _ue_errno and _ue_errmsg. We save
78 * and restore the UNIX errno across this routing so the caller can use either
79 * libscsi_set_errno(), libscsi_error(), or libscsi_verror() without this value
80 * changing.
81 */
82 int
libscsi_verror(libscsi_hdl_t * hp,libscsi_errno_t err,const char * fmt,va_list ap)83 libscsi_verror(libscsi_hdl_t *hp, libscsi_errno_t err, const char *fmt,
84 va_list ap)
85 {
86 size_t n;
87 char *errmsg;
88
89 /*
90 * To allow the existing error message to itself be used in an error
91 * message, we put the new error message into a buffer on the stack,
92 * and then copy it into lsh_errmsg. We also need to set the errno,
93 * but because the call to libscsi_set_errno() is destructive to
94 * lsh_errmsg, we do this after we print into our temporary buffer
95 * (in case _libscsi_errmsg is part of the error message) and before we
96 * copy the temporary buffer on to _libscsi_errmsg (to prevent our new
97 * message from being nuked by the call to libscsi_set_errno()).
98 */
99 errmsg = alloca(sizeof (hp->lsh_errmsg));
100 (void) vsnprintf(errmsg, sizeof (hp->lsh_errmsg), fmt, ap);
101 (void) libscsi_set_errno(hp, err);
102
103 n = strlen(errmsg);
104
105 if (n != 0 && errmsg[n - 1] == '\n')
106 errmsg[n - 1] = '\0';
107
108 bcopy(errmsg, hp->lsh_errmsg, n + 1);
109
110 return (-1);
111 }
112
113 /*PRINTFLIKE3*/
114 int
libscsi_error(libscsi_hdl_t * hp,libscsi_errno_t err,const char * fmt,...)115 libscsi_error(libscsi_hdl_t *hp, libscsi_errno_t err, const char *fmt, ...)
116 {
117 va_list ap;
118
119 if (fmt == NULL)
120 return (libscsi_set_errno(hp, err));
121
122 va_start(ap, fmt);
123 err = libscsi_verror(hp, err, fmt, ap);
124 va_end(ap);
125
126 return (err);
127 }
128
129 libscsi_errno_t
libscsi_errno(libscsi_hdl_t * hp)130 libscsi_errno(libscsi_hdl_t *hp)
131 {
132 return (hp->lsh_errno);
133 }
134
135 const char *
libscsi_errmsg(libscsi_hdl_t * hp)136 libscsi_errmsg(libscsi_hdl_t *hp)
137 {
138 if (hp->lsh_errmsg[0] == '\0')
139 (void) strlcpy(hp->lsh_errmsg, libscsi_strerror(hp->lsh_errno),
140 sizeof (hp->lsh_errmsg));
141
142 return (hp->lsh_errmsg);
143 }
144
145 void *
libscsi_alloc(libscsi_hdl_t * hp,size_t size)146 libscsi_alloc(libscsi_hdl_t *hp, size_t size)
147 {
148 void *mem;
149
150 if (size == 0) {
151 (void) libscsi_set_errno(hp, ESCSI_ZERO_LENGTH);
152 return (NULL);
153 }
154
155 if ((mem = malloc(size)) == NULL)
156 (void) libscsi_set_errno(hp, ESCSI_NOMEM);
157
158 return (mem);
159 }
160
161 void *
libscsi_zalloc(libscsi_hdl_t * hp,size_t size)162 libscsi_zalloc(libscsi_hdl_t *hp, size_t size)
163 {
164 void *mem;
165
166 if ((mem = libscsi_alloc(hp, size)) == NULL)
167 return (NULL);
168
169 bzero(mem, size);
170
171 return (mem);
172 }
173
174 char *
libscsi_strdup(libscsi_hdl_t * hp,const char * str)175 libscsi_strdup(libscsi_hdl_t *hp, const char *str)
176 {
177 size_t len = strlen(str);
178 char *dup = libscsi_alloc(hp, len + 1);
179
180 if (dup == NULL)
181 return (NULL);
182
183 return (strcpy(dup, str));
184 }
185
186 /*ARGSUSED*/
187 void
libscsi_free(libscsi_hdl_t * hp,void * ptr)188 libscsi_free(libscsi_hdl_t *hp, void *ptr)
189 {
190 free(ptr);
191 }
192
193 libscsi_hdl_t *
libscsi_init(uint_t version,libscsi_errno_t * errp)194 libscsi_init(uint_t version, libscsi_errno_t *errp)
195 {
196 libscsi_hdl_t *hp;
197
198 if (version != LIBSCSI_VERSION) {
199 if (errp != NULL)
200 *errp = ESCSI_VERSION;
201 return (NULL);
202 }
203
204 if ((hp = malloc(sizeof (libscsi_hdl_t))) == NULL) {
205 if (errp != NULL)
206 *errp = ESCSI_NOMEM;
207 return (NULL);
208 }
209
210 bzero(hp, sizeof (libscsi_hdl_t));
211 hp->lsh_version = version;
212
213 return (hp);
214 }
215
216 void
libscsi_fini(libscsi_hdl_t * hp)217 libscsi_fini(libscsi_hdl_t *hp)
218 {
219 libscsi_engine_impl_t *eip, *neip;
220
221 if (hp == NULL)
222 return;
223
224 ASSERT(hp->lsh_targets == 0);
225
226 for (eip = hp->lsh_engines; eip != NULL; eip = neip) {
227 neip = eip->lsei_next;
228 (void) dlclose(eip->lsei_dl_hdl);
229 libscsi_free(hp, eip);
230 }
231
232 free(hp);
233 }
234
235 size_t
libscsi_cmd_cdblen(libscsi_hdl_t * hp,uint8_t cmd)236 libscsi_cmd_cdblen(libscsi_hdl_t *hp, uint8_t cmd)
237 {
238 size_t sz;
239
240 switch (CDB_GROUPID(cmd)) {
241 case CDB_GROUPID_0:
242 sz = CDB_GROUP0;
243 break;
244 case CDB_GROUPID_1:
245 sz = CDB_GROUP1;
246 break;
247 case CDB_GROUPID_2:
248 sz = CDB_GROUP2;
249 break;
250 case CDB_GROUPID_3:
251 sz = CDB_GROUP3;
252 break;
253 case CDB_GROUPID_4:
254 sz = CDB_GROUP4;
255 break;
256 case CDB_GROUPID_5:
257 sz = CDB_GROUP5;
258 break;
259 case CDB_GROUPID_6:
260 sz = CDB_GROUP6;
261 break;
262 case CDB_GROUPID_7:
263 sz = CDB_GROUP7;
264 break;
265 default:
266 sz = 0;
267 }
268
269 if (sz == 0)
270 (void) libscsi_error(hp, ESCSI_BADCMD,
271 "unknown or unsupported command %u", cmd);
272
273 return (sz);
274 }
275
276 static char *
libscsi_process_inquiry_string(libscsi_hdl_t * hp,const char * raw,size_t len)277 libscsi_process_inquiry_string(libscsi_hdl_t *hp, const char *raw, size_t len)
278 {
279 char *buf;
280
281 buf = alloca(len + 1);
282 bcopy(raw, buf, len);
283
284 for (; len > 0; len--) {
285 if (buf[len - 1] != ' ')
286 break;
287 }
288
289 buf[len] = '\0';
290
291 return (libscsi_strdup(hp, buf));
292 }
293
294 /*
295 * As part of basic initialization, we always retrieve the INQUIRY information
296 * to have the vendor/product/revision information available for all consumers.
297 */
298 int
libscsi_get_inquiry(libscsi_hdl_t * hp,libscsi_target_t * tp)299 libscsi_get_inquiry(libscsi_hdl_t *hp, libscsi_target_t *tp)
300 {
301 libscsi_action_t *ap;
302 spc3_inquiry_cdb_t *cp;
303 spc3_inquiry_data_t data;
304 size_t len;
305
306 if ((ap = libscsi_action_alloc(hp, SPC3_CMD_INQUIRY,
307 LIBSCSI_AF_READ | LIBSCSI_AF_SILENT | LIBSCSI_AF_DIAGNOSE, &data,
308 offsetof(spc3_inquiry_data_t, id_vs_36[0]))) == NULL)
309 return (-1);
310
311 cp = (spc3_inquiry_cdb_t *)libscsi_action_get_cdb(ap);
312
313 SCSI_WRITE16(&cp->ic_allocation_length,
314 offsetof(spc3_inquiry_data_t, id_vs_36[0]));
315
316 if (libscsi_exec(ap, tp) != 0 ||
317 libscsi_action_get_status(ap) != 0) {
318 libscsi_action_free(ap);
319 return (libscsi_set_errno(hp, ESCSI_INQUIRY_FAILED));
320 }
321
322 (void) libscsi_action_get_buffer(ap, NULL, NULL, &len);
323 libscsi_action_free(ap);
324
325 if (len < offsetof(spc3_inquiry_data_t, id_vs_36))
326 return (libscsi_set_errno(hp, ESCSI_INQUIRY_FAILED));
327
328 if ((tp->lst_vendor = libscsi_process_inquiry_string(hp,
329 data.id_vendor_id, sizeof (data.id_vendor_id))) == NULL ||
330 (tp->lst_product = libscsi_process_inquiry_string(hp,
331 data.id_product_id, sizeof (data.id_product_id))) == NULL ||
332 (tp->lst_revision = libscsi_process_inquiry_string(hp,
333 data.id_product_revision,
334 sizeof (data.id_product_revision))) == NULL) {
335 return (-1);
336 }
337
338 return (0);
339 }
340
341 const char *
libscsi_vendor(libscsi_target_t * tp)342 libscsi_vendor(libscsi_target_t *tp)
343 {
344 return (tp->lst_vendor);
345 }
346
347 const char *
libscsi_product(libscsi_target_t * tp)348 libscsi_product(libscsi_target_t *tp)
349 {
350 return (tp->lst_product);
351 }
352
353 const char *
libscsi_revision(libscsi_target_t * tp)354 libscsi_revision(libscsi_target_t *tp)
355 {
356 return (tp->lst_revision);
357 }
358