1/* $OpenBSD: scsiconf.h,v 1.202 2023/05/10 15:28:26 krw Exp $ */
2/* $NetBSD: scsiconf.h,v 1.35 1997/04/02 02:29:38 mycroft Exp $ */
3
4/*
5 * Copyright (c) 1993, 1994, 1995 Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 *
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48 */
49
50#ifndef _SCSI_SCSICONF_H
51#define _SCSI_SCSICONF_H
52
53#include <sys/queue.h>
54#include <sys/timeout.h>
55#include <sys/mutex.h>
56
57static __inline void _lto2b(u_int32_t val, u_int8_t *bytes);
58static __inline void _lto3b(u_int32_t val, u_int8_t *bytes);
59static __inline void _lto4b(u_int32_t val, u_int8_t *bytes);
60static __inline void _lto8b(u_int64_t val, u_int8_t *bytes);
61static __inline u_int32_t _2btol(u_int8_t *bytes);
62static __inline u_int32_t _3btol(u_int8_t *bytes);
63static __inline u_int32_t _4btol(u_int8_t *bytes);
64static __inline u_int64_t _5btol(u_int8_t *bytes);
65static __inline u_int64_t _8btol(u_int8_t *bytes);
66
67static __inline void
68_lto2b(u_int32_t val, u_int8_t *bytes)
69{
70
71 bytes[0] = (val >> 8) & 0xff;
72 bytes[1] = val & 0xff;
73}
74
75static __inline void
76_lto3b(u_int32_t val, u_int8_t *bytes)
77{
78
79 bytes[0] = (val >> 16) & 0xff;
80 bytes[1] = (val >> 8) & 0xff;
81 bytes[2] = val & 0xff;
82}
83
84static __inline void
85_lto4b(u_int32_t val, u_int8_t *bytes)
86{
87
88 bytes[0] = (val >> 24) & 0xff;
89 bytes[1] = (val >> 16) & 0xff;
90 bytes[2] = (val >> 8) & 0xff;
91 bytes[3] = val & 0xff;
92}
93
94static __inline void
95_lto8b(u_int64_t val, u_int8_t *bytes)
96{
97
98 bytes[0] = (val >> 56) & 0xff;
99 bytes[1] = (val >> 48) & 0xff;
100 bytes[2] = (val >> 40) & 0xff;
101 bytes[3] = (val >> 32) & 0xff;
102 bytes[4] = (val >> 24) & 0xff;
103 bytes[5] = (val >> 16) & 0xff;
104 bytes[6] = (val >> 8) & 0xff;
105 bytes[7] = val & 0xff;
106}
107
108static __inline u_int32_t
109_2btol(u_int8_t *bytes)
110{
111 u_int32_t rv;
112
113 rv = (bytes[0] << 8) | bytes[1];
114 return rv;
115}
116
117static __inline u_int32_t
118_3btol(u_int8_t *bytes)
119{
120 u_int32_t rv;
121
122 rv = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
123 return rv;
124}
125
126static __inline u_int32_t
127_4btol(u_int8_t *bytes)
128{
129 u_int32_t rv;
130
131 rv = (bytes[0] << 24) | (bytes[1] << 16) |
132 (bytes[2] << 8) | bytes[3];
133 return rv;
134}
135
136static __inline u_int64_t
137_5btol(u_int8_t *bytes)
138{
139 u_int64_t rv;
140
141 rv = ((u_int64_t)bytes[0] << 32) |
142 ((u_int64_t)bytes[1] << 24) |
143 ((u_int64_t)bytes[2] << 16) |
144 ((u_int64_t)bytes[3] << 8) |
145 (u_int64_t)bytes[4];
146 return rv;
147}
148
149static __inline u_int64_t
150_8btol(u_int8_t *bytes)
151{
152 u_int64_t rv;
153
154 rv = (((u_int64_t)bytes[0]) << 56) |
155 (((u_int64_t)bytes[1]) << 48) |
156 (((u_int64_t)bytes[2]) << 40) |
157 (((u_int64_t)bytes[3]) << 32) |
158 (((u_int64_t)bytes[4]) << 24) |
159 (((u_int64_t)bytes[5]) << 16) |
160 (((u_int64_t)bytes[6]) << 8) |
161 ((u_int64_t)bytes[7]);
162 return rv;
163}
164
165#ifdef _KERNEL
166
167#define DEVID_NONE 0
168#define DEVID_NAA 1
169#define DEVID_EUI 2
170#define DEVID_T10 3
171#define DEVID_SERIAL 4
172#define DEVID_WWN 5
173
174struct devid {
175 u_int8_t d_type;
176 u_int8_t d_flags;
177#define DEVID_F_PRINT (1<<0)
178 u_int8_t d_refcount;
179 u_int8_t d_len;
180
181 /*
182 * the devid struct is basically a header, the actual id is allocated
183 * immediately after it.
184 */
185};
186
187#define DEVID_CMP(_a, _b) ( \
188 (_a) != NULL && (_b) != NULL && \
189 ((_a) == (_b) || \
190 ((_a)->d_type != DEVID_NONE && \
191 (_a)->d_type == (_b)->d_type && \
192 (_a)->d_len == (_b)->d_len && \
193 bcmp((_a) + 1, (_b) + 1, (_a)->d_len) == 0)) \
194)
195
196struct devid * devid_alloc(u_int8_t, u_int8_t, u_int8_t, u_int8_t *);
197struct devid * devid_copy(struct devid *);
198void devid_free(struct devid *);
199
200/*
201 * Each existing device (scsibus + target + lun)
202 * - is described by a scsi_link struct.
203 * Each scsi_link struct
204 * - identifies the device's softc and scsi_adapter.
205 * Each scsi_adapter struct
206 * - contains pointers to the device's scsi functions.
207 * Each scsibus_softc has an SLIST
208 * - holding pointers to the scsi_link structs of devices on that scsi bus.
209 * Each individual device
210 * - knows the address of its scsi_link structure.
211 */
212
213struct scsi_xfer;
214struct scsi_link;
215struct scsibus_softc;
216
217/*
218 * Temporary hack
219 */
220extern int scsi_autoconf;
221
222/*
223 * These entrypoints are called by the high-end drivers to get services from
224 * whatever low-end drivers they are attached to. Each adapter type has one
225 * of these statically allocated.
226 */
227struct scsi_adapter {
228 void (*scsi_cmd)(struct scsi_xfer *);
229 void (*dev_minphys)(struct buf *, struct scsi_link *);
230 int (*dev_probe)(struct scsi_link *);
231 void (*dev_free)(struct scsi_link *);
232 int (*ioctl)(struct scsi_link *, u_long, caddr_t, int);
233};
234
235struct scsi_iopool;
236
237struct scsi_iohandler {
238 TAILQ_ENTRY(scsi_iohandler) q_entry;
239 u_int q_state;
240
241 struct scsi_iopool *pool;
242 void (*handler)(void *, void *);
243 void *cookie;
244};
245TAILQ_HEAD(scsi_runq, scsi_iohandler);
246
247struct scsi_iopool {
248 /* access to the IOs */
249 void *iocookie;
250 /*
251 * Get an IO. This must reserve all resources that are necessary
252 * to send the transfer to the device. The resources must stay
253 * reserved during the lifetime of the IO, as the IO may be re-used
254 * without being io_put(), first.
255 */
256 void *(*io_get)(void *);
257 void (*io_put)(void *, void *);
258
259 /* the runqueue */
260 struct scsi_runq queue;
261 /* runqueue semaphore */
262 u_int running;
263 /* protection for the runqueue and its semaphore */
264 struct mutex mtx;
265};
266
267struct scsi_xshandler {
268 struct scsi_iohandler ioh; /* must be first */
269
270 struct scsi_link *link;
271 void (*handler)(struct scsi_xfer *);
272};
273
274/*
275 * This structure describes the connection between an adapter driver and
276 * a device driver, and is used by each to call services provided by
277 * the other, and to allow generic scsi glue code to call these services
278 * as well.
279 */
280struct scsi_link {
281 SLIST_ENTRY(scsi_link) bus_list;
282
283 u_int state;
284#define SDEV_S_DYING (1<<1)
285
286 u_int16_t target; /* targ of this dev */
287 u_int16_t lun; /* lun of this dev */
288 u_int16_t openings; /* available operations per lun */
289 u_int64_t port_wwn; /* world wide name of port */
290 u_int64_t node_wwn; /* world wide name of node */
291 u_int16_t flags; /* flags that all devices have */
292#define SDEV_REMOVABLE 0x0001 /* media is removable */
293#define SDEV_MEDIA_LOADED 0x0002 /* device figures are still valid */
294#define SDEV_READONLY 0x0004 /* device is read-only */
295#define SDEV_OPEN 0x0008 /* at least 1 open session */
296#define SDEV_DBX 0x00f0 /* debugging flags (scsi_debug.h) */
297#define SDEV_EJECTING 0x0100 /* eject on device close */
298#define SDEV_ATAPI 0x0200 /* device is ATAPI */
299#define SDEV_UMASS 0x0400 /* device is UMASS SCSI */
300#define SDEV_VIRTUAL 0x0800 /* device is virtualised on the hba */
301#define SDEV_OWN_IOPL 0x1000 /* scsibus */
302#define SDEV_UFI 0x2000 /* Universal Floppy Interface */
303 u_int16_t quirks; /* per-device oddities */
304#define SDEV_AUTOSAVE 0x0001 /* do implicit SAVEDATAPOINTER on disconnect */
305#define SDEV_NOSYNC 0x0002 /* does not grok SDTR */
306#define SDEV_NOWIDE 0x0004 /* does not grok WDTR */
307#define SDEV_NOTAGS 0x0008 /* lies about having tagged queueing */
308#define SDEV_NOSYNCCACHE 0x0010 /* no SYNCHRONIZE_CACHE */
309#define ADEV_NOSENSE 0x0020 /* No request sense - ATAPI */
310#define ADEV_LITTLETOC 0x0040 /* little-endian TOC - ATAPI */
311#define ADEV_NOCAPACITY 0x0080 /* no READ CD CAPACITY */
312#define ADEV_NODOORLOCK 0x0100 /* can't lock door */
313 int (*interpret_sense)(struct scsi_xfer *);
314 void *device_softc; /* needed for call to foo_start */
315 struct scsibus_softc *bus; /* link to the scsibus we're on */
316 struct scsi_inquiry_data inqdata; /* copy of INQUIRY data from probe */
317 struct devid *id;
318
319 struct scsi_runq queue;
320 u_int running;
321 u_short pending;
322
323 struct scsi_iopool *pool;
324};
325
326int scsiprint(void *, const char *);
327
328/*
329 * This describes matching information for scsi_inqmatch(). The more things
330 * match, the higher the configuration priority.
331 */
332struct scsi_inquiry_pattern {
333 u_int8_t type;
334 int removable;
335 char *vendor;
336 char *product;
337 char *revision;
338};
339
340struct scsibus_attach_args {
341 const struct scsi_adapter *saa_adapter;
342 void *saa_adapter_softc;
343 struct scsi_iopool *saa_pool;
344 u_int64_t saa_wwpn;
345 u_int64_t saa_wwnn;
346 u_int16_t saa_quirks;
347 u_int16_t saa_flags;
348 u_int16_t saa_openings;
349 u_int16_t saa_adapter_target;
350#define SDEV_NO_ADAPTER_TARGET 0xffff
351 u_int16_t saa_adapter_buswidth;
352 u_int8_t saa_luns;
353};
354
355/*
356 * One of these is allocated and filled in for each scsi bus.
357 * It holds pointers to allow the scsi bus to get to the driver
358 * that is running each LUN on the bus.
359 * It also has a template entry which is the prototype struct
360 * supplied by the adapter driver. This is used to initialise
361 * the others, before they have the rest of the fields filled in.
362 */
363struct scsibus_softc {
364 struct device sc_dev;
365 SLIST_HEAD(, scsi_link) sc_link_list;
366 void *sb_adapter_softc;
367 const struct scsi_adapter *sb_adapter;
368 struct scsi_iopool *sb_pool;
369 u_int16_t sb_quirks;
370 u_int16_t sb_flags;
371 u_int16_t sb_openings;
372 u_int16_t sb_adapter_buswidth;
373 u_int16_t sb_adapter_target;
374 u_int8_t sb_luns;
375};
376
377/*
378 * This is used to pass information from the high-level configuration code
379 * to the device-specific drivers.
380 */
381struct scsi_attach_args {
382 struct scsi_link *sa_sc_link;
383};
384
385/*
386 * Each scsi transaction is fully described by one of these structures.
387 * It includes information about the source of the command and also the
388 * device and adapter for which the command is destined.
389 * (via the scsi_link structure)
390 */
391struct scsi_xfer {
392 SIMPLEQ_ENTRY(scsi_xfer) xfer_list;
393 int flags;
394 struct scsi_link *sc_link; /* all about our device and adapter */
395 int retries; /* the number of times to retry */
396 int timeout; /* in milliseconds */
397 struct scsi_generic cmd; /* The scsi command to execute */
398 int cmdlen; /* how long it is */
399 u_char *data; /* dma address OR a uio address */
400 int datalen; /* data len (blank if uio) */
401 size_t resid; /* how much buffer was not touched */
402 int error; /* an error value */
403 struct buf *bp; /* If we need to associate with a buf */
404 struct scsi_sense_data sense; /* 18 bytes*/
405 u_int8_t status; /* SCSI status */
406 /*
407 * timeout structure for hba's to use for a command
408 */
409 struct timeout stimeout;
410 void *cookie;
411 void (*done)(struct scsi_xfer *);
412
413 void *io; /* adapter io resource */
414};
415SIMPLEQ_HEAD(scsi_xfer_list, scsi_xfer);
416
417/*
418 * Per-request Flag values
419 */
420#define SCSI_NOSLEEP 0x00001 /* don't sleep */
421#define SCSI_POLL 0x00002 /* poll for completion */
422#define SCSI_AUTOCONF 0x00003 /* shorthand for SCSI_POLL | SCSI_NOSLEEP */
423#define ITSDONE 0x00008 /* the transfer is as done as it gets */
424#define SCSI_SILENT 0x00020 /* don't announce NOT READY or MEDIA CHANGE */
425#define SCSI_IGNORE_NOT_READY 0x00040 /* ignore NOT READY */
426#define SCSI_IGNORE_MEDIA_CHANGE 0x00080 /* ignore MEDIA CHANGE */
427#define SCSI_IGNORE_ILLEGAL_REQUEST 0x00100 /* ignore ILLEGAL REQUEST */
428#define SCSI_RESET 0x00200 /* Reset the device in question */
429#define SCSI_DATA_IN 0x00800 /* expect data to come INTO memory */
430#define SCSI_DATA_OUT 0x01000 /* expect data to flow OUT of memory */
431#define SCSI_TARGET 0x02000 /* This defines a TARGET mode op. */
432#define SCSI_ESCAPE 0x04000 /* Escape operation */
433#define SCSI_PRIVATE 0xf0000 /* private to each HBA flags */
434
435/*
436 * Escape op-codes. This provides an extensible setup for operations
437 * that are not scsi commands. They are intended for modal operations.
438 */
439
440#define SCSI_OP_TARGET 0x0001
441#define SCSI_OP_RESET 0x0002
442#define SCSI_OP_BDINFO 0x0003
443
444/*
445 * Error values an adapter driver may return
446 */
447#define XS_NOERROR 0 /* there is no error, (sense is invalid) */
448#define XS_SENSE 1 /* Check the returned sense for the error */
449#define XS_DRIVER_STUFFUP 2 /* Driver failed to perform operation */
450#define XS_SELTIMEOUT 3 /* The device timed out.. turned off? */
451#define XS_TIMEOUT 4 /* The Timeout reported was caught by SW */
452#define XS_BUSY 5 /* The device busy, try again later? */
453#define XS_SHORTSENSE 6 /* Check the ATAPI sense for the error */
454#define XS_RESET 8 /* bus was reset; possible retry command */
455
456/*
457 * Possible retries for scsi_test_unit_ready()
458 */
459#define TEST_READY_RETRIES 5
460
461/*
462 * Possible retries for most SCSI commands.
463 */
464#define SCSI_RETRIES 4
465
466const void *scsi_inqmatch(struct scsi_inquiry_data *, const void *, int,
467 int, int *);
468
469void scsi_init(void);
470int scsi_test_unit_ready(struct scsi_link *, int, int);
471int scsi_inquire(struct scsi_link *, struct scsi_inquiry_data *, int);
472int scsi_read_cap_10(struct scsi_link *, struct scsi_read_cap_data *, int);
473int scsi_read_cap_16(struct scsi_link *, struct scsi_read_cap_data_16 *,
474 int);
475int scsi_inquire_vpd(struct scsi_link *, void *, u_int, u_int8_t, int);
476void scsi_init_inquiry(struct scsi_xfer *, u_int8_t, u_int8_t,
477 void *, size_t);
478int scsi_prevent(struct scsi_link *, int, int);
479int scsi_start(struct scsi_link *, int, int);
480void scsi_parse_blkdesc(struct scsi_link *, union scsi_mode_sense_buf *, int,
481 u_int32_t *, u_int64_t *, u_int32_t *);
482int scsi_do_mode_sense(struct scsi_link *, int,
483 union scsi_mode_sense_buf *, void **, int, int, int *);
484void scsi_parse_blkdesc(struct scsi_link *, union scsi_mode_sense_buf *, int,
485 u_int32_t *, u_int64_t *, u_int32_t *);
486int scsi_mode_select(struct scsi_link *, int, struct scsi_mode_header *,
487 int, int);
488int scsi_mode_select_big(struct scsi_link *, int,
489 struct scsi_mode_header_big *, int, int);
490void scsi_copy_internal_data(struct scsi_xfer *, void *, size_t);
491void scsi_done(struct scsi_xfer *);
492int scsi_do_ioctl(struct scsi_link *, u_long, caddr_t, int);
493void sc_print_addr(struct scsi_link *);
494int scsi_report_luns(struct scsi_link *, int,
495 struct scsi_report_luns_data *, u_int32_t, int, int);
496int scsi_interpret_sense(struct scsi_xfer *);
497
498void scsi_print_sense(struct scsi_xfer *);
499void scsi_strvis(u_char *, u_char *, int);
500int scsi_delay(struct scsi_xfer *, int);
501
502int scsi_probe(struct scsibus_softc *, int, int);
503int scsi_probe_bus(struct scsibus_softc *);
504int scsi_probe_target(struct scsibus_softc *, int);
505int scsi_probe_lun(struct scsibus_softc *, int, int);
506
507int scsi_detach(struct scsibus_softc *, int, int, int);
508int scsi_detach_target(struct scsibus_softc *, int, int);
509int scsi_detach_lun(struct scsibus_softc *, int, int, int);
510
511int scsi_req_probe(struct scsibus_softc *, int, int);
512int scsi_req_detach(struct scsibus_softc *, int, int, int);
513
514int scsi_activate(struct scsibus_softc *, int, int, int);
515
516struct scsi_link * scsi_get_link(struct scsibus_softc *, int, int);
517
518#define SID_ANSII_REV(x) ((x)->version & SID_ANSII)
519#define SID_RESPONSE_FORMAT(x) ((x)->response_format & SID_RESPONSE_DATA_FMT)
520
521#define SCSI_REV_0 0x00 /* No conformance to any standard. */
522#define SCSI_REV_1 0x01 /* (Obsolete) SCSI-1 in olden times. */
523#define SCSI_REV_2 0x02 /* (Obsolete) SCSI-2 in olden times. */
524#define SCSI_REV_SPC 0x03 /* ANSI INCITS 301-1997 (SPC). */
525#define SCSI_REV_SPC2 0x04 /* ANSI INCITS 351-2001 (SPC-2) */
526#define SCSI_REV_SPC3 0x05 /* ANSI INCITS 408-2005 (SPC-3) */
527#define SCSI_REV_SPC4 0x06 /* ANSI INCITS 513-2015 (SPC-4) */
528#define SCSI_REV_SPC5 0x07 /* T10/BSR INCITS 503 (SPC-5) */
529
530struct scsi_xfer * scsi_xs_get(struct scsi_link *, int);
531void scsi_xs_exec(struct scsi_xfer *);
532int scsi_xs_sync(struct scsi_xfer *);
533void scsi_xs_put(struct scsi_xfer *);
534
535/*
536 * iopool stuff
537 */
538void scsi_iopool_init(struct scsi_iopool *, void *,
539 void *(*)(void *), void (*)(void *, void *));
540void scsi_iopool_run(struct scsi_iopool *);
541void scsi_iopool_destroy(struct scsi_iopool *);
542void scsi_link_shutdown(struct scsi_link *);
543
544void * scsi_io_get(struct scsi_iopool *, int);
545void scsi_io_put(struct scsi_iopool *, void *);
546
547/*
548 * default io allocator.
549 */
550#define SCSI_IOPOOL_POISON ((void *)0x5c5)
551void * scsi_default_get(void *);
552void scsi_default_put(void *, void *);
553
554/*
555 * io handler interface
556 */
557void scsi_ioh_set(struct scsi_iohandler *, struct scsi_iopool *,
558 void (*)(void *, void *), void *);
559int scsi_ioh_add(struct scsi_iohandler *);
560int scsi_ioh_del(struct scsi_iohandler *);
561
562void scsi_xsh_set(struct scsi_xshandler *, struct scsi_link *,
563 void (*)(struct scsi_xfer *));
564int scsi_xsh_add(struct scsi_xshandler *);
565int scsi_xsh_del(struct scsi_xshandler *);
566
567/*
568 * utility functions
569 */
570int scsi_pending_start(struct mutex *, u_int *);
571int scsi_pending_finish(struct mutex *, u_int *);
572
573/*
574 * Utility functions for SCSI HBA emulation.
575 */
576void scsi_cmd_rw_decode(struct scsi_generic *, u_int64_t *, u_int32_t *);
577
578#endif /* _KERNEL */
579#endif /* _SCSI_SCSICONF_H */