1f6ad3f23SAlexander Motin /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3bec9534dSPedro F. Giffuni *
4f6ad3f23SAlexander Motin * Copyright (c) 2000 Matthew Jacob
5f6ad3f23SAlexander Motin * All rights reserved.
6f6ad3f23SAlexander Motin *
7f6ad3f23SAlexander Motin * Redistribution and use in source and binary forms, with or without
8f6ad3f23SAlexander Motin * modification, are permitted provided that the following conditions
9f6ad3f23SAlexander Motin * are met:
10f6ad3f23SAlexander Motin * 1. Redistributions of source code must retain the above copyright
11f6ad3f23SAlexander Motin * notice, this list of conditions, and the following disclaimer,
12f6ad3f23SAlexander Motin * without modification, immediately at the beginning of the file.
13f6ad3f23SAlexander Motin * 2. The name of the author may not be used to endorse or promote products
14f6ad3f23SAlexander Motin * derived from this software without specific prior written permission.
15f6ad3f23SAlexander Motin *
16f6ad3f23SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f6ad3f23SAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f6ad3f23SAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f6ad3f23SAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20f6ad3f23SAlexander Motin * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f6ad3f23SAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f6ad3f23SAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f6ad3f23SAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f6ad3f23SAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f6ad3f23SAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f6ad3f23SAlexander Motin * SUCH DAMAGE.
27f6ad3f23SAlexander Motin */
28f6ad3f23SAlexander Motin
29f6ad3f23SAlexander Motin /*
30f6ad3f23SAlexander Motin * This file contains definitions only intended for use within
31f6ad3f23SAlexander Motin * sys/cam/scsi/scsi_enc*.c, and not in other kernel components.
32f6ad3f23SAlexander Motin */
33f6ad3f23SAlexander Motin
34f6ad3f23SAlexander Motin #ifndef __SCSI_ENC_INTERNAL_H__
35f6ad3f23SAlexander Motin #define __SCSI_ENC_INTERNAL_H__
36f6ad3f23SAlexander Motin
376d4d6573SAlexander Motin #include <sys/sysctl.h>
386d4d6573SAlexander Motin
39f6ad3f23SAlexander Motin typedef struct enc_element {
40dd694648SAlexander Motin u_int elm_idx; /* index of element */
4153f5ac13SAlexander Motin uint8_t elm_type; /* element type */
4253f5ac13SAlexander Motin uint8_t subenclosure; /* subenclosure id */
4353f5ac13SAlexander Motin uint8_t type_elm_idx; /* index of element within type */
4453f5ac13SAlexander Motin uint8_t svalid; /* enclosure information valid */
45f6ad3f23SAlexander Motin uint8_t encstat[4]; /* state && stats */
46f6ad3f23SAlexander Motin u_int physical_path_len; /* Length of device path data. */
47dd694648SAlexander Motin uint8_t *physical_path; /* Device physical path data. */
48f6ad3f23SAlexander Motin void *elm_private; /* per-type object data */
49dd694648SAlexander Motin uint16_t priv;
50f6ad3f23SAlexander Motin } enc_element_t;
51f6ad3f23SAlexander Motin
52f6ad3f23SAlexander Motin typedef enum {
53f6ad3f23SAlexander Motin ENC_NONE,
54f6ad3f23SAlexander Motin ENC_SES,
55f6ad3f23SAlexander Motin ENC_SES_PASSTHROUGH,
56f6ad3f23SAlexander Motin ENC_SAFT,
57f6ad3f23SAlexander Motin ENC_SEMB_SES,
58f6ad3f23SAlexander Motin ENC_SEMB_SAFT
59f6ad3f23SAlexander Motin } enctyp;
60f6ad3f23SAlexander Motin
61f6ad3f23SAlexander Motin /* Platform Independent Driver Internal Definitions for enclosure devices. */
62f6ad3f23SAlexander Motin typedef struct enc_softc enc_softc_t;
63f6ad3f23SAlexander Motin
64f6ad3f23SAlexander Motin struct enc_fsm_state;
65f6ad3f23SAlexander Motin typedef int fsm_fill_handler_t(enc_softc_t *ssc,
66f6ad3f23SAlexander Motin struct enc_fsm_state *state,
67f6ad3f23SAlexander Motin union ccb *ccb,
68f6ad3f23SAlexander Motin uint8_t *buf);
69f6ad3f23SAlexander Motin typedef int fsm_error_handler_t(union ccb *ccb, uint32_t cflags,
70f6ad3f23SAlexander Motin uint32_t sflags);
71f6ad3f23SAlexander Motin typedef int fsm_done_handler_t(enc_softc_t *ssc,
72f6ad3f23SAlexander Motin struct enc_fsm_state *state, union ccb *ccb,
73f6ad3f23SAlexander Motin uint8_t **bufp, int error, int xfer_len);
74f6ad3f23SAlexander Motin
75f6ad3f23SAlexander Motin struct enc_fsm_state {
76f6ad3f23SAlexander Motin const char *name;
77f6ad3f23SAlexander Motin int page_code;
78f6ad3f23SAlexander Motin size_t buf_size;
79f6ad3f23SAlexander Motin uint32_t timeout;
80f6ad3f23SAlexander Motin fsm_fill_handler_t *fill;
81f6ad3f23SAlexander Motin fsm_done_handler_t *done;
82f6ad3f23SAlexander Motin fsm_error_handler_t *error;
83f6ad3f23SAlexander Motin };
84f6ad3f23SAlexander Motin
85f6ad3f23SAlexander Motin typedef int (enc_softc_init_t)(enc_softc_t *);
86f6ad3f23SAlexander Motin typedef void (enc_softc_invalidate_t)(enc_softc_t *);
87f6ad3f23SAlexander Motin typedef void (enc_softc_cleanup_t)(enc_softc_t *);
88f6ad3f23SAlexander Motin typedef int (enc_init_enc_t)(enc_softc_t *);
89f6ad3f23SAlexander Motin typedef int (enc_set_enc_status_t)(enc_softc_t *, encioc_enc_status_t, int);
90f6ad3f23SAlexander Motin typedef int (enc_get_elm_status_t)(enc_softc_t *, encioc_elm_status_t *, int);
91f6ad3f23SAlexander Motin typedef int (enc_set_elm_status_t)(enc_softc_t *, encioc_elm_status_t *, int);
92f6ad3f23SAlexander Motin typedef int (enc_get_elm_desc_t)(enc_softc_t *, encioc_elm_desc_t *);
93f6ad3f23SAlexander Motin typedef int (enc_get_elm_devnames_t)(enc_softc_t *, encioc_elm_devnames_t *);
94170a0a8eSAlan Somers typedef int (enc_handle_string_t)(enc_softc_t *, encioc_string_t *,
95170a0a8eSAlan Somers unsigned long);
96f6ad3f23SAlexander Motin typedef void (enc_device_found_t)(enc_softc_t *);
97f6ad3f23SAlexander Motin typedef void (enc_poll_status_t)(enc_softc_t *);
98f6ad3f23SAlexander Motin
99f6ad3f23SAlexander Motin struct enc_vec {
100f6ad3f23SAlexander Motin enc_softc_invalidate_t *softc_invalidate;
101f6ad3f23SAlexander Motin enc_softc_cleanup_t *softc_cleanup;
102f6ad3f23SAlexander Motin enc_init_enc_t *init_enc;
103f6ad3f23SAlexander Motin enc_set_enc_status_t *set_enc_status;
104f6ad3f23SAlexander Motin enc_get_elm_status_t *get_elm_status;
105f6ad3f23SAlexander Motin enc_set_elm_status_t *set_elm_status;
106f6ad3f23SAlexander Motin enc_get_elm_desc_t *get_elm_desc;
107f6ad3f23SAlexander Motin enc_get_elm_devnames_t *get_elm_devnames;
108f6ad3f23SAlexander Motin enc_handle_string_t *handle_string;
109f6ad3f23SAlexander Motin enc_device_found_t *device_found;
110f6ad3f23SAlexander Motin enc_poll_status_t *poll_status;
111f6ad3f23SAlexander Motin };
112f6ad3f23SAlexander Motin
113f6ad3f23SAlexander Motin typedef struct enc_cache {
114f6ad3f23SAlexander Motin enc_element_t *elm_map; /* objects */
115f6ad3f23SAlexander Motin int nelms; /* number of objects */
116f6ad3f23SAlexander Motin encioc_enc_status_t enc_status; /* overall status */
117f6ad3f23SAlexander Motin void *private; /* per-type private data */
118f6ad3f23SAlexander Motin } enc_cache_t;
119f6ad3f23SAlexander Motin
120f6ad3f23SAlexander Motin /* Enclosure instance toplevel structure */
121f6ad3f23SAlexander Motin struct enc_softc {
122f6ad3f23SAlexander Motin enctyp enc_type; /* type of enclosure */
123f6ad3f23SAlexander Motin struct enc_vec enc_vec; /* vector to handlers */
124f6ad3f23SAlexander Motin void *enc_private; /* per-type private data */
125f6ad3f23SAlexander Motin
126f6ad3f23SAlexander Motin /**
127f6ad3f23SAlexander Motin * "Published" configuration and state data available to
128f6ad3f23SAlexander Motin * external consumers.
129f6ad3f23SAlexander Motin */
130f6ad3f23SAlexander Motin enc_cache_t enc_cache;
131f6ad3f23SAlexander Motin
132f6ad3f23SAlexander Motin /**
133f6ad3f23SAlexander Motin * Configuration and state data being actively updated
134f6ad3f23SAlexander Motin * by the enclosure daemon.
135f6ad3f23SAlexander Motin */
136f6ad3f23SAlexander Motin enc_cache_t enc_daemon_cache;
137f6ad3f23SAlexander Motin
138f6ad3f23SAlexander Motin struct sx enc_cache_lock;
139f6ad3f23SAlexander Motin uint8_t enc_flags;
140f6ad3f23SAlexander Motin #define ENC_FLAG_INVALID 0x01
141f6ad3f23SAlexander Motin #define ENC_FLAG_INITIALIZED 0x02
142f6ad3f23SAlexander Motin #define ENC_FLAG_SHUTDOWN 0x04
143f6ad3f23SAlexander Motin struct cdev *enc_dev;
144f6ad3f23SAlexander Motin struct cam_periph *periph;
14586d45c7fSKenneth D. Merry int open_count;
146f6ad3f23SAlexander Motin
147f6ad3f23SAlexander Motin /* Bitmap of pending operations. */
148f6ad3f23SAlexander Motin uint32_t pending_actions;
149f6ad3f23SAlexander Motin
150f6ad3f23SAlexander Motin /* The action on which the state machine is currently working. */
151f6ad3f23SAlexander Motin uint32_t current_action;
152f6ad3f23SAlexander Motin #define ENC_UPDATE_NONE 0x00
153f6ad3f23SAlexander Motin #define ENC_UPDATE_INVALID 0xff
154f6ad3f23SAlexander Motin
155f6ad3f23SAlexander Motin /* Callout for auto-updating enclosure status */
156f6ad3f23SAlexander Motin struct callout status_updater;
157f6ad3f23SAlexander Motin
158f6ad3f23SAlexander Motin struct proc *enc_daemon;
159f6ad3f23SAlexander Motin
160f6ad3f23SAlexander Motin struct enc_fsm_state *enc_fsm_states;
161f6ad3f23SAlexander Motin
1625d01277fSScott Long #define ENC_ANNOUNCE_SZ 400
1635d01277fSScott Long char announce_buf[ENC_ANNOUNCE_SZ];
164f6ad3f23SAlexander Motin };
165f6ad3f23SAlexander Motin
166f6ad3f23SAlexander Motin static inline enc_cache_t *
enc_other_cache(enc_softc_t * enc,enc_cache_t * primary)167f6ad3f23SAlexander Motin enc_other_cache(enc_softc_t *enc, enc_cache_t *primary)
168f6ad3f23SAlexander Motin {
169f6ad3f23SAlexander Motin return (primary == &enc->enc_cache
170f6ad3f23SAlexander Motin ? &enc->enc_daemon_cache : &enc->enc_cache);
171f6ad3f23SAlexander Motin }
172f6ad3f23SAlexander Motin
173f6ad3f23SAlexander Motin /* SES Management mode page - SES2r20 Table 59 */
174f6ad3f23SAlexander Motin struct ses_mgmt_mode_page {
175f6ad3f23SAlexander Motin struct scsi_mode_header_6 header;
176f6ad3f23SAlexander Motin struct scsi_mode_blk_desc blk_desc;
177f6ad3f23SAlexander Motin uint8_t byte0; /* ps : 1, spf : 1, page_code : 6 */
178f6ad3f23SAlexander Motin #define SES_MGMT_MODE_PAGE_CODE 0x14
179f6ad3f23SAlexander Motin uint8_t length;
180f6ad3f23SAlexander Motin #define SES_MGMT_MODE_PAGE_LEN 6
181f6ad3f23SAlexander Motin uint8_t reserved[3];
182f6ad3f23SAlexander Motin uint8_t byte5; /* reserved : 7, enbltc : 1 */
183f6ad3f23SAlexander Motin #define SES_MGMT_TIMED_COMP_EN 0x1
184f6ad3f23SAlexander Motin uint8_t max_comp_time[2];
185f6ad3f23SAlexander Motin };
186f6ad3f23SAlexander Motin
187f6ad3f23SAlexander Motin /* Enclosure core interface for sub-drivers */
188f6ad3f23SAlexander Motin int enc_runcmd(struct enc_softc *, char *, int, char *, int *);
189f6ad3f23SAlexander Motin void enc_log(struct enc_softc *, const char *, ...);
190f6ad3f23SAlexander Motin int enc_error(union ccb *, uint32_t, uint32_t);
191f6ad3f23SAlexander Motin void enc_update_request(enc_softc_t *, uint32_t);
192f6ad3f23SAlexander Motin
193f6ad3f23SAlexander Motin /* SES Native interface */
194f6ad3f23SAlexander Motin enc_softc_init_t ses_softc_init;
195f6ad3f23SAlexander Motin
196f6ad3f23SAlexander Motin /* SAF-TE interface */
197f6ad3f23SAlexander Motin enc_softc_init_t safte_softc_init;
198f6ad3f23SAlexander Motin
1996d4d6573SAlexander Motin SYSCTL_DECL(_kern_cam_enc);
2006d4d6573SAlexander Motin extern int enc_verbose;
2016d4d6573SAlexander Motin
202f6ad3f23SAlexander Motin /* Helper macros */
203f6ad3f23SAlexander Motin MALLOC_DECLARE(M_SCSIENC);
204f6ad3f23SAlexander Motin #define ENC_CFLAGS CAM_RETRY_SELTO
205f6ad3f23SAlexander Motin #define ENC_FLAGS SF_NO_PRINT | SF_RETRY_UA
206f6ad3f23SAlexander Motin #define STRNCMP strncmp
207f6ad3f23SAlexander Motin #define PRINTF printf
208f6ad3f23SAlexander Motin #define ENC_LOG enc_log
209f6ad3f23SAlexander Motin #if defined(DEBUG) || defined(ENC_DEBUG)
210f6ad3f23SAlexander Motin #define ENC_DLOG enc_log
211f6ad3f23SAlexander Motin #else
212f6ad3f23SAlexander Motin #define ENC_DLOG if (0) enc_log
213f6ad3f23SAlexander Motin #endif
2146d4d6573SAlexander Motin #define ENC_VLOG if (enc_verbose) enc_log
215f6ad3f23SAlexander Motin #define ENC_MALLOC(amt) malloc(amt, M_SCSIENC, M_NOWAIT)
216f6ad3f23SAlexander Motin #define ENC_MALLOCZ(amt) malloc(amt, M_SCSIENC, M_ZERO|M_NOWAIT)
217f6ad3f23SAlexander Motin /* Cast away const avoiding GCC warnings. */
218f6ad3f23SAlexander Motin #define ENC_FREE(ptr) free((void *)((uintptr_t)ptr), M_SCSIENC)
219f6ad3f23SAlexander Motin #define ENC_FREE_AND_NULL(ptr) do { \
220f6ad3f23SAlexander Motin if (ptr != NULL) { \
221f6ad3f23SAlexander Motin ENC_FREE(ptr); \
222f6ad3f23SAlexander Motin ptr = NULL; \
223f6ad3f23SAlexander Motin } \
224f6ad3f23SAlexander Motin } while(0)
225f6ad3f23SAlexander Motin #define MEMZERO bzero
226f6ad3f23SAlexander Motin #define MEMCPY(dest, src, amt) bcopy(src, dest, amt)
227f6ad3f23SAlexander Motin
228f6ad3f23SAlexander Motin #endif /* __SCSI_ENC_INTERNAL_H__ */
229