xref: /freebsd/sys/cam/scsi/scsi_enc_ses.c (revision 771501e96ffd7e314c338300a351ad76d28a2f69)
1f6ad3f23SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3bec9534dSPedro F. Giffuni  *
4f6ad3f23SAlexander Motin  * Copyright (c) 2000 Matthew Jacob
5f6ad3f23SAlexander Motin  * Copyright (c) 2010 Spectra Logic Corporation
6f6ad3f23SAlexander Motin  * All rights reserved.
7f6ad3f23SAlexander Motin  *
8f6ad3f23SAlexander Motin  * Redistribution and use in source and binary forms, with or without
9f6ad3f23SAlexander Motin  * modification, are permitted provided that the following conditions
10f6ad3f23SAlexander Motin  * are met:
11f6ad3f23SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
12f6ad3f23SAlexander Motin  *    notice, this list of conditions, and the following disclaimer,
13f6ad3f23SAlexander Motin  *    without modification, immediately at the beginning of the file.
14f6ad3f23SAlexander Motin  * 2. The name of the author may not be used to endorse or promote products
15f6ad3f23SAlexander Motin  *    derived from this software without specific prior written permission.
16f6ad3f23SAlexander Motin  *
17f6ad3f23SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18f6ad3f23SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f6ad3f23SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f6ad3f23SAlexander Motin  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21f6ad3f23SAlexander Motin  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f6ad3f23SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f6ad3f23SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f6ad3f23SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f6ad3f23SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f6ad3f23SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f6ad3f23SAlexander Motin  * SUCH DAMAGE.
28f6ad3f23SAlexander Motin  */
29f6ad3f23SAlexander Motin 
30f6ad3f23SAlexander Motin /**
31f6ad3f23SAlexander Motin  * \file scsi_enc_ses.c
32f6ad3f23SAlexander Motin  *
33f6ad3f23SAlexander Motin  * Structures and routines specific && private to SES only
34f6ad3f23SAlexander Motin  */
35f6ad3f23SAlexander Motin 
36f6ad3f23SAlexander Motin #include <sys/param.h>
37f6ad3f23SAlexander Motin 
38a9f2ac59SJaakko Heinonen #include <sys/ctype.h>
39f6ad3f23SAlexander Motin #include <sys/errno.h>
40f6ad3f23SAlexander Motin #include <sys/kernel.h>
41f6ad3f23SAlexander Motin #include <sys/lock.h>
42f6ad3f23SAlexander Motin #include <sys/malloc.h>
43f6ad3f23SAlexander Motin #include <sys/mutex.h>
44f6ad3f23SAlexander Motin #include <sys/queue.h>
45f6ad3f23SAlexander Motin #include <sys/sbuf.h>
46f6ad3f23SAlexander Motin #include <sys/sx.h>
47f6ad3f23SAlexander Motin #include <sys/systm.h>
48f6ad3f23SAlexander Motin #include <sys/types.h>
49f6ad3f23SAlexander Motin 
50f6ad3f23SAlexander Motin #include <cam/cam.h>
51f6ad3f23SAlexander Motin #include <cam/cam_ccb.h>
52f6ad3f23SAlexander Motin #include <cam/cam_xpt_periph.h>
53f6ad3f23SAlexander Motin #include <cam/cam_periph.h>
54f6ad3f23SAlexander Motin 
55f6ad3f23SAlexander Motin #include <cam/scsi/scsi_message.h>
56f6ad3f23SAlexander Motin #include <cam/scsi/scsi_enc.h>
57f6ad3f23SAlexander Motin #include <cam/scsi/scsi_enc_internal.h>
58f6ad3f23SAlexander Motin 
59f6ad3f23SAlexander Motin /* SES Native Type Device Support */
60f6ad3f23SAlexander Motin 
61f6ad3f23SAlexander Motin /* SES Diagnostic Page Codes */
62f6ad3f23SAlexander Motin typedef enum {
63f6ad3f23SAlexander Motin 	SesSupportedPages	= 0x0,
64f6ad3f23SAlexander Motin 	SesConfigPage		= 0x1,
65f6ad3f23SAlexander Motin 	SesControlPage		= 0x2,
66f6ad3f23SAlexander Motin 	SesStatusPage		= SesControlPage,
67f6ad3f23SAlexander Motin 	SesHelpTxt		= 0x3,
68f6ad3f23SAlexander Motin 	SesStringOut		= 0x4,
69f6ad3f23SAlexander Motin 	SesStringIn		= SesStringOut,
70f6ad3f23SAlexander Motin 	SesThresholdOut		= 0x5,
71f6ad3f23SAlexander Motin 	SesThresholdIn		= SesThresholdOut,
72f6ad3f23SAlexander Motin 	SesArrayControl		= 0x6,	/* Obsolete in SES v2 */
73f6ad3f23SAlexander Motin 	SesArrayStatus		= SesArrayControl,
74f6ad3f23SAlexander Motin 	SesElementDescriptor	= 0x7,
75f6ad3f23SAlexander Motin 	SesShortStatus		= 0x8,
76f6ad3f23SAlexander Motin 	SesEnclosureBusy	= 0x9,
77f6ad3f23SAlexander Motin 	SesAddlElementStatus	= 0xa
78f6ad3f23SAlexander Motin } SesDiagPageCodes;
79f6ad3f23SAlexander Motin 
80f6ad3f23SAlexander Motin typedef struct ses_type {
81f6ad3f23SAlexander Motin 	const struct ses_elm_type_desc  *hdr;
82f6ad3f23SAlexander Motin 	const char			*text;
83f6ad3f23SAlexander Motin } ses_type_t;
84f6ad3f23SAlexander Motin 
85f6ad3f23SAlexander Motin typedef struct ses_comstat {
86f6ad3f23SAlexander Motin 	uint8_t	comstatus;
87f6ad3f23SAlexander Motin 	uint8_t	comstat[3];
88f6ad3f23SAlexander Motin } ses_comstat_t;
89f6ad3f23SAlexander Motin 
90f6ad3f23SAlexander Motin typedef union ses_addl_data {
91f6ad3f23SAlexander Motin 	struct ses_elm_sas_device_phy *sasdev_phys;
92f6ad3f23SAlexander Motin 	struct ses_elm_sas_expander_phy *sasexp_phys;
93f6ad3f23SAlexander Motin 	struct ses_elm_sas_port_phy *sasport_phys;
94f6ad3f23SAlexander Motin 	struct ses_fcobj_port *fc_ports;
95f6ad3f23SAlexander Motin } ses_add_data_t;
96f6ad3f23SAlexander Motin 
97f6ad3f23SAlexander Motin typedef struct ses_addl_status {
98f6ad3f23SAlexander Motin 	struct ses_elm_addlstatus_base_hdr *hdr;
99f6ad3f23SAlexander Motin 	union {
100f6ad3f23SAlexander Motin 		union ses_fcobj_hdr *fc;
101f6ad3f23SAlexander Motin 		union ses_elm_sas_hdr *sas;
10253f5ac13SAlexander Motin 		struct ses_elm_ata_hdr *ata;
103f6ad3f23SAlexander Motin 	} proto_hdr;
104f6ad3f23SAlexander Motin 	union ses_addl_data proto_data;	/* array sizes stored in header */
105f6ad3f23SAlexander Motin } ses_add_status_t;
106f6ad3f23SAlexander Motin 
107f6ad3f23SAlexander Motin typedef struct ses_element {
108f6ad3f23SAlexander Motin 	uint8_t eip;			/* eip bit is set */
109f6ad3f23SAlexander Motin 	uint16_t descr_len;		/* length of the descriptor */
110e083fb08SAlan Somers 	const char *descr;		/* descriptor for this object */
111f6ad3f23SAlexander Motin 	struct ses_addl_status addl;	/* additional status info */
112f6ad3f23SAlexander Motin } ses_element_t;
113f6ad3f23SAlexander Motin 
114f6ad3f23SAlexander Motin typedef struct ses_control_request {
115f6ad3f23SAlexander Motin 	int	      elm_idx;
116f6ad3f23SAlexander Motin 	ses_comstat_t elm_stat;
117f6ad3f23SAlexander Motin 	int	      result;
118f6ad3f23SAlexander Motin 	TAILQ_ENTRY(ses_control_request) links;
119f6ad3f23SAlexander Motin } ses_control_request_t;
120f6ad3f23SAlexander Motin TAILQ_HEAD(ses_control_reqlist, ses_control_request);
121f6ad3f23SAlexander Motin typedef struct ses_control_reqlist ses_control_reqlist_t;
122f6ad3f23SAlexander Motin enum {
123f6ad3f23SAlexander Motin 	SES_SETSTATUS_ENC_IDX = -1
124f6ad3f23SAlexander Motin };
125f6ad3f23SAlexander Motin 
126f6ad3f23SAlexander Motin static void
ses_terminate_control_requests(ses_control_reqlist_t * reqlist,int result)127f6ad3f23SAlexander Motin ses_terminate_control_requests(ses_control_reqlist_t *reqlist, int result)
128f6ad3f23SAlexander Motin {
129f6ad3f23SAlexander Motin 	ses_control_request_t *req;
130f6ad3f23SAlexander Motin 
131f6ad3f23SAlexander Motin 	while ((req = TAILQ_FIRST(reqlist)) != NULL) {
132f6ad3f23SAlexander Motin 		TAILQ_REMOVE(reqlist, req, links);
133f6ad3f23SAlexander Motin 		req->result = result;
134f6ad3f23SAlexander Motin 		wakeup(req);
135f6ad3f23SAlexander Motin 	}
136f6ad3f23SAlexander Motin }
137f6ad3f23SAlexander Motin 
138f6ad3f23SAlexander Motin enum ses_iter_index_values {
139f6ad3f23SAlexander Motin 	/**
140f6ad3f23SAlexander Motin 	 * \brief  Value of an initialized but invalid index
141f6ad3f23SAlexander Motin 	 *         in a ses_iterator object.
142f6ad3f23SAlexander Motin 	 *
143f6ad3f23SAlexander Motin 	 * This value is used for the  individual_element_index of
144f6ad3f23SAlexander Motin 	 * overal status elements and for all index types when
145f6ad3f23SAlexander Motin 	 * an iterator is first initialized.
146f6ad3f23SAlexander Motin 	 */
147f6ad3f23SAlexander Motin 	ITERATOR_INDEX_INVALID = -1,
148f6ad3f23SAlexander Motin 
149f6ad3f23SAlexander Motin 	/**
150f6ad3f23SAlexander Motin 	 * \brief  Value of an index in a ses_iterator object
151f6ad3f23SAlexander Motin 	 *	   when the iterator has traversed past the last
152f6ad3f23SAlexander Motin 	 *	   valid element..
153f6ad3f23SAlexander Motin 	 */
154f6ad3f23SAlexander Motin 	ITERATOR_INDEX_END     = INT_MAX
155f6ad3f23SAlexander Motin };
156f6ad3f23SAlexander Motin 
157f6ad3f23SAlexander Motin /**
158f6ad3f23SAlexander Motin  * \brief Structure encapsulating all data necessary to traverse the
159f6ad3f23SAlexander Motin  *        elements of a SES configuration.
160f6ad3f23SAlexander Motin  *
161f6ad3f23SAlexander Motin  * The ses_iterator object simplifies the task of iterating through all
162f6ad3f23SAlexander Motin  * elements detected via the SES configuration page by tracking the numerous
163f6ad3f23SAlexander Motin  * element indexes that, instead of memoizing in the softc, we calculate
164f6ad3f23SAlexander Motin  * on the fly during the traversal of the element objects.  The various
165f6ad3f23SAlexander Motin  * indexes are necessary due to the varying needs of matching objects in
166f6ad3f23SAlexander Motin  * the different SES pages.  Some pages (e.g. Status/Control) contain all
167f6ad3f23SAlexander Motin  * elements, while others (e.g. Additional Element Status) only contain
168f6ad3f23SAlexander Motin  * individual elements (no overal status elements) of particular types.
169f6ad3f23SAlexander Motin  *
170f6ad3f23SAlexander Motin  * To use an iterator, initialize it with ses_iter_init(), and then
171f6ad3f23SAlexander Motin  * use ses_iter_next() to traverse the elements (including the first) in
172f6ad3f23SAlexander Motin  * the configuration.  Once an iterator is initiailized with ses_iter_init(),
173f6ad3f23SAlexander Motin  * you may also seek to any particular element by either it's global or
174f6ad3f23SAlexander Motin  * individual element index via the ses_iter_seek_to() function.  You may
175f6ad3f23SAlexander Motin  * also return an iterator to the position just before the first element
176f6ad3f23SAlexander Motin  * (i.e. the same state as after an ses_iter_init()), with ses_iter_reset().
177f6ad3f23SAlexander Motin  */
178f6ad3f23SAlexander Motin struct ses_iterator {
179f6ad3f23SAlexander Motin 	/**
180f6ad3f23SAlexander Motin 	 * \brief Backlink to the overal software configuration structure.
181f6ad3f23SAlexander Motin 	 *
182f6ad3f23SAlexander Motin 	 * This is included for convenience so the iteration functions
183f6ad3f23SAlexander Motin 	 * need only take a single, struct ses_iterator *, argument.
184f6ad3f23SAlexander Motin 	 */
185f6ad3f23SAlexander Motin 	enc_softc_t *enc;
186f6ad3f23SAlexander Motin 
187f6ad3f23SAlexander Motin 	enc_cache_t *cache;
188f6ad3f23SAlexander Motin 
189f6ad3f23SAlexander Motin 	/**
190f6ad3f23SAlexander Motin 	 * \brief Index of the type of the current element within the
191f6ad3f23SAlexander Motin 	 *        ses_cache's ses_types array.
192f6ad3f23SAlexander Motin 	 */
193f6ad3f23SAlexander Motin 	int	          type_index;
194f6ad3f23SAlexander Motin 
195f6ad3f23SAlexander Motin 	/**
196f6ad3f23SAlexander Motin 	 * \brief The position (0 based) of this element relative to all other
197f6ad3f23SAlexander Motin 	 *        elements of this type.
198f6ad3f23SAlexander Motin 	 *
199f6ad3f23SAlexander Motin 	 * This index resets to zero every time the iterator transitions
200f6ad3f23SAlexander Motin 	 * to elements of a new type in the configuration.
201f6ad3f23SAlexander Motin 	 */
202f6ad3f23SAlexander Motin 	int	          type_element_index;
203f6ad3f23SAlexander Motin 
204f6ad3f23SAlexander Motin 	/**
205f6ad3f23SAlexander Motin 	 * \brief The position (0 based) of this element relative to all
206f6ad3f23SAlexander Motin 	 *        other individual status elements in the configuration.
207f6ad3f23SAlexander Motin 	 *
208f6ad3f23SAlexander Motin 	 * This index ranges from 0 through the number of individual
209f6ad3f23SAlexander Motin 	 * elements in the configuration.  When the iterator returns
210f6ad3f23SAlexander Motin 	 * an overall status element, individual_element_index is
211f6ad3f23SAlexander Motin 	 * set to ITERATOR_INDEX_INVALID, to indicate that it does
212f6ad3f23SAlexander Motin 	 * not apply to the current element.
213f6ad3f23SAlexander Motin 	 */
214f6ad3f23SAlexander Motin 	int	          individual_element_index;
215f6ad3f23SAlexander Motin 
216f6ad3f23SAlexander Motin 	/**
217f6ad3f23SAlexander Motin 	 * \brief The position (0 based) of this element relative to
218f6ad3f23SAlexander Motin 	 *        all elements in the configration.
219f6ad3f23SAlexander Motin 	 *
220f6ad3f23SAlexander Motin 	 * This index is appropriate for indexing into enc->ses_elm_map.
221f6ad3f23SAlexander Motin 	 */
222f6ad3f23SAlexander Motin 	int	          global_element_index;
223f6ad3f23SAlexander Motin 
224f6ad3f23SAlexander Motin 	/**
225f6ad3f23SAlexander Motin 	 * \brief The last valid individual element index of this
226f6ad3f23SAlexander Motin 	 *        iterator.
227f6ad3f23SAlexander Motin 	 *
228f6ad3f23SAlexander Motin 	 * When an iterator traverses an overal status element, the
229f6ad3f23SAlexander Motin 	 * individual element index is reset to ITERATOR_INDEX_INVALID
230f6ad3f23SAlexander Motin 	 * to prevent unintential use of the individual_element_index
231f6ad3f23SAlexander Motin 	 * field.  The saved_individual_element_index allows the iterator
232f6ad3f23SAlexander Motin 	 * to restore it's position in the individual elements upon
233f6ad3f23SAlexander Motin 	 * reaching the next individual element.
234f6ad3f23SAlexander Motin 	 */
235f6ad3f23SAlexander Motin 	int	          saved_individual_element_index;
236f6ad3f23SAlexander Motin };
237f6ad3f23SAlexander Motin 
238f6ad3f23SAlexander Motin typedef enum {
239f6ad3f23SAlexander Motin 	SES_UPDATE_NONE,
240f6ad3f23SAlexander Motin 	SES_UPDATE_PAGES,
241f6ad3f23SAlexander Motin 	SES_UPDATE_GETCONFIG,
242f6ad3f23SAlexander Motin 	SES_UPDATE_GETSTATUS,
243f6ad3f23SAlexander Motin 	SES_UPDATE_GETELMDESCS,
244f6ad3f23SAlexander Motin 	SES_UPDATE_GETELMADDLSTATUS,
245f6ad3f23SAlexander Motin 	SES_PROCESS_CONTROL_REQS,
246f6ad3f23SAlexander Motin 	SES_PUBLISH_PHYSPATHS,
247f6ad3f23SAlexander Motin 	SES_PUBLISH_CACHE,
248f6ad3f23SAlexander Motin 	SES_NUM_UPDATE_STATES
249f6ad3f23SAlexander Motin } ses_update_action;
250f6ad3f23SAlexander Motin 
251f6ad3f23SAlexander Motin static enc_softc_cleanup_t ses_softc_cleanup;
252f6ad3f23SAlexander Motin 
253f6ad3f23SAlexander Motin #define	SCSZ	0x8000
254f6ad3f23SAlexander Motin 
255f6ad3f23SAlexander Motin static fsm_fill_handler_t ses_fill_rcv_diag_io;
256f6ad3f23SAlexander Motin static fsm_fill_handler_t ses_fill_control_request;
257f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_pages;
258f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_config;
259f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_status;
260f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_elm_descs;
261f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_elm_addlstatus;
262f6ad3f23SAlexander Motin static fsm_done_handler_t ses_process_control_request;
263f6ad3f23SAlexander Motin static fsm_done_handler_t ses_publish_physpaths;
264f6ad3f23SAlexander Motin static fsm_done_handler_t ses_publish_cache;
265f6ad3f23SAlexander Motin 
266f6ad3f23SAlexander Motin static struct enc_fsm_state enc_fsm_states[SES_NUM_UPDATE_STATES] =
267f6ad3f23SAlexander Motin {
268f6ad3f23SAlexander Motin 	{ "SES_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
269f6ad3f23SAlexander Motin 	{
270f6ad3f23SAlexander Motin 		"SES_UPDATE_PAGES",
271f6ad3f23SAlexander Motin 		SesSupportedPages,
272f6ad3f23SAlexander Motin 		SCSZ,
273f6ad3f23SAlexander Motin 		60 * 1000,
274f6ad3f23SAlexander Motin 		ses_fill_rcv_diag_io,
275f6ad3f23SAlexander Motin 		ses_process_pages,
276f6ad3f23SAlexander Motin 		enc_error
277f6ad3f23SAlexander Motin 	},
278f6ad3f23SAlexander Motin 	{
279f6ad3f23SAlexander Motin 		"SES_UPDATE_GETCONFIG",
280f6ad3f23SAlexander Motin 		SesConfigPage,
281f6ad3f23SAlexander Motin 		SCSZ,
282f6ad3f23SAlexander Motin 		60 * 1000,
283f6ad3f23SAlexander Motin 		ses_fill_rcv_diag_io,
284f6ad3f23SAlexander Motin 		ses_process_config,
285f6ad3f23SAlexander Motin 		enc_error
286f6ad3f23SAlexander Motin 	},
287f6ad3f23SAlexander Motin 	{
288f6ad3f23SAlexander Motin 		"SES_UPDATE_GETSTATUS",
289f6ad3f23SAlexander Motin 		SesStatusPage,
290f6ad3f23SAlexander Motin 		SCSZ,
291f6ad3f23SAlexander Motin 		60 * 1000,
292f6ad3f23SAlexander Motin 		ses_fill_rcv_diag_io,
293f6ad3f23SAlexander Motin 		ses_process_status,
294f6ad3f23SAlexander Motin 		enc_error
295f6ad3f23SAlexander Motin 	},
296f6ad3f23SAlexander Motin 	{
297f6ad3f23SAlexander Motin 		"SES_UPDATE_GETELMDESCS",
298f6ad3f23SAlexander Motin 		SesElementDescriptor,
299f6ad3f23SAlexander Motin 		SCSZ,
300f6ad3f23SAlexander Motin 		60 * 1000,
301f6ad3f23SAlexander Motin 		ses_fill_rcv_diag_io,
302f6ad3f23SAlexander Motin 		ses_process_elm_descs,
303f6ad3f23SAlexander Motin 		enc_error
304f6ad3f23SAlexander Motin 	},
305f6ad3f23SAlexander Motin 	{
306f6ad3f23SAlexander Motin 		"SES_UPDATE_GETELMADDLSTATUS",
307f6ad3f23SAlexander Motin 		SesAddlElementStatus,
308f6ad3f23SAlexander Motin 		SCSZ,
309f6ad3f23SAlexander Motin 		60 * 1000,
310f6ad3f23SAlexander Motin 		ses_fill_rcv_diag_io,
311f6ad3f23SAlexander Motin 		ses_process_elm_addlstatus,
312f6ad3f23SAlexander Motin 		enc_error
313f6ad3f23SAlexander Motin 	},
314f6ad3f23SAlexander Motin 	{
315f6ad3f23SAlexander Motin 		"SES_PROCESS_CONTROL_REQS",
316f6ad3f23SAlexander Motin 		SesControlPage,
317f6ad3f23SAlexander Motin 		SCSZ,
318f6ad3f23SAlexander Motin 		60 * 1000,
319f6ad3f23SAlexander Motin 		ses_fill_control_request,
320f6ad3f23SAlexander Motin 		ses_process_control_request,
321f6ad3f23SAlexander Motin 		enc_error
322f6ad3f23SAlexander Motin 	},
323f6ad3f23SAlexander Motin 	{
324f6ad3f23SAlexander Motin 		"SES_PUBLISH_PHYSPATHS",
325f6ad3f23SAlexander Motin 		0,
326f6ad3f23SAlexander Motin 		0,
327f6ad3f23SAlexander Motin 		0,
328f6ad3f23SAlexander Motin 		NULL,
329f6ad3f23SAlexander Motin 		ses_publish_physpaths,
330f6ad3f23SAlexander Motin 		NULL
331f6ad3f23SAlexander Motin 	},
332f6ad3f23SAlexander Motin 	{
333f6ad3f23SAlexander Motin 		"SES_PUBLISH_CACHE",
334f6ad3f23SAlexander Motin 		0,
335f6ad3f23SAlexander Motin 		0,
336f6ad3f23SAlexander Motin 		0,
337f6ad3f23SAlexander Motin 		NULL,
338f6ad3f23SAlexander Motin 		ses_publish_cache,
339f6ad3f23SAlexander Motin 		NULL
340f6ad3f23SAlexander Motin 	}
341f6ad3f23SAlexander Motin };
342f6ad3f23SAlexander Motin 
343f6ad3f23SAlexander Motin typedef struct ses_cache {
344f6ad3f23SAlexander Motin 	/* Source for all the configuration data pointers */
345f6ad3f23SAlexander Motin 	const struct ses_cfg_page		*cfg_page;
346f6ad3f23SAlexander Motin 
347f6ad3f23SAlexander Motin 	/* References into the config page. */
3486d2a1fbfSAlexander Motin 	int					 ses_nsubencs;
349f6ad3f23SAlexander Motin 	const struct ses_enc_desc * const	*subencs;
3506d2a1fbfSAlexander Motin 	int					 ses_ntypes;
351f6ad3f23SAlexander Motin 	const ses_type_t			*ses_types;
352f6ad3f23SAlexander Motin 
353f6ad3f23SAlexander Motin 	/* Source for all the status pointers */
354f6ad3f23SAlexander Motin 	const struct ses_status_page		*status_page;
355f6ad3f23SAlexander Motin 
356f6ad3f23SAlexander Motin 	/* Source for all the object descriptor pointers */
357f6ad3f23SAlexander Motin 	const struct ses_elem_descr_page	*elm_descs_page;
358f6ad3f23SAlexander Motin 
359f6ad3f23SAlexander Motin 	/* Source for all the additional object status pointers */
360f6ad3f23SAlexander Motin 	const struct ses_addl_elem_status_page  *elm_addlstatus_page;
361f6ad3f23SAlexander Motin 
362f6ad3f23SAlexander Motin } ses_cache_t;
363f6ad3f23SAlexander Motin 
364f6ad3f23SAlexander Motin typedef struct ses_softc {
365f6ad3f23SAlexander Motin 	uint32_t		ses_flags;
366f6ad3f23SAlexander Motin #define	SES_FLAG_TIMEDCOMP	0x01
367f6ad3f23SAlexander Motin #define	SES_FLAG_ADDLSTATUS	0x02
368715c4a72SAlexander Motin #define	SES_FLAG_DESC		0x04
369f6ad3f23SAlexander Motin 
370f6ad3f23SAlexander Motin 	ses_control_reqlist_t	ses_requests;
371f6ad3f23SAlexander Motin 	ses_control_reqlist_t	ses_pending_requests;
372f6ad3f23SAlexander Motin } ses_softc_t;
373f6ad3f23SAlexander Motin 
374730ea72cSAlexander Motin static int ses_search_globally = 0;
375730ea72cSAlexander Motin SYSCTL_INT(_kern_cam_enc, OID_AUTO, search_globally, CTLFLAG_RWTUN,
376730ea72cSAlexander Motin            &ses_search_globally, 0, "Search for disks on other buses");
377730ea72cSAlexander Motin 
378f6ad3f23SAlexander Motin /**
379f6ad3f23SAlexander Motin  * \brief Reset a SES iterator to just before the first element
380f6ad3f23SAlexander Motin  *        in the configuration.
381f6ad3f23SAlexander Motin  *
382f6ad3f23SAlexander Motin  * \param iter  The iterator object to reset.
383f6ad3f23SAlexander Motin  *
384f6ad3f23SAlexander Motin  * The indexes within a reset iterator are invalid and will only
385f6ad3f23SAlexander Motin  * become valid upon completion of a ses_iter_seek_to() or a
386f6ad3f23SAlexander Motin  * ses_iter_next().
387f6ad3f23SAlexander Motin  */
388f6ad3f23SAlexander Motin static void
ses_iter_reset(struct ses_iterator * iter)389f6ad3f23SAlexander Motin ses_iter_reset(struct ses_iterator *iter)
390f6ad3f23SAlexander Motin {
391f6ad3f23SAlexander Motin 	/*
392f6ad3f23SAlexander Motin 	 * Set our indexes to just before the first valid element
393f6ad3f23SAlexander Motin 	 * of the first type (ITERATOR_INDEX_INVALID == -1).  This
394f6ad3f23SAlexander Motin 	 * simplifies the implementation of ses_iter_next().
395f6ad3f23SAlexander Motin 	 */
396f6ad3f23SAlexander Motin 	iter->type_index                     = 0;
397f6ad3f23SAlexander Motin 	iter->type_element_index             = ITERATOR_INDEX_INVALID;
398f6ad3f23SAlexander Motin 	iter->global_element_index           = ITERATOR_INDEX_INVALID;
399f6ad3f23SAlexander Motin 	iter->individual_element_index       = ITERATOR_INDEX_INVALID;
400f6ad3f23SAlexander Motin 	iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
401f6ad3f23SAlexander Motin }
402f6ad3f23SAlexander Motin 
403f6ad3f23SAlexander Motin /**
404f6ad3f23SAlexander Motin  * \brief Initialize the storage of a SES iterator and reset it to
405f6ad3f23SAlexander Motin  *        the position just before the first element of the
406f6ad3f23SAlexander Motin  *        configuration.
407f6ad3f23SAlexander Motin  *
408f6ad3f23SAlexander Motin  * \param enc	The SES softc for the SES instance whose configuration
409f6ad3f23SAlexander Motin  *              will be enumerated by this iterator.
410f6ad3f23SAlexander Motin  * \param iter  The iterator object to initialize.
411f6ad3f23SAlexander Motin  */
412f6ad3f23SAlexander Motin static void
ses_iter_init(enc_softc_t * enc,enc_cache_t * cache,struct ses_iterator * iter)413f6ad3f23SAlexander Motin ses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
414f6ad3f23SAlexander Motin {
415f6ad3f23SAlexander Motin 	iter->enc = enc;
416f6ad3f23SAlexander Motin 	iter->cache = cache;
417f6ad3f23SAlexander Motin 	ses_iter_reset(iter);
418f6ad3f23SAlexander Motin }
419f6ad3f23SAlexander Motin 
420f6ad3f23SAlexander Motin /**
421f6ad3f23SAlexander Motin  * \brief Traverse the provided SES iterator to the next element
42204389c85SGordon Bergling  *        within the configuration.
423f6ad3f23SAlexander Motin  *
424f6ad3f23SAlexander Motin  * \param iter  The iterator to move.
425f6ad3f23SAlexander Motin  *
426f6ad3f23SAlexander Motin  * \return  If a valid next element exists, a pointer to it's enc_element_t.
427f6ad3f23SAlexander Motin  *          Otherwise NULL.
428f6ad3f23SAlexander Motin  */
429f6ad3f23SAlexander Motin static enc_element_t *
ses_iter_next(struct ses_iterator * iter)430f6ad3f23SAlexander Motin ses_iter_next(struct ses_iterator *iter)
431f6ad3f23SAlexander Motin {
432f6ad3f23SAlexander Motin 	ses_cache_t	 *ses_cache;
433f6ad3f23SAlexander Motin 	const ses_type_t *element_type;
434f6ad3f23SAlexander Motin 
435f6ad3f23SAlexander Motin 	ses_cache = iter->cache->private;
436f6ad3f23SAlexander Motin 
437f6ad3f23SAlexander Motin 	/*
438f6ad3f23SAlexander Motin 	 * Note: Treat nelms as signed, so we will hit this case
439f6ad3f23SAlexander Motin 	 *       and immediately terminate the iteration if the
440f6ad3f23SAlexander Motin 	 *	 configuration has 0 objects.
441f6ad3f23SAlexander Motin 	 */
442f6ad3f23SAlexander Motin 	if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
443f6ad3f23SAlexander Motin 		/* Elements exhausted. */
444f6ad3f23SAlexander Motin 		iter->type_index	       = ITERATOR_INDEX_END;
445f6ad3f23SAlexander Motin 		iter->type_element_index       = ITERATOR_INDEX_END;
446f6ad3f23SAlexander Motin 		iter->global_element_index     = ITERATOR_INDEX_END;
447f6ad3f23SAlexander Motin 		iter->individual_element_index = ITERATOR_INDEX_END;
4487318fcb5SAlexander Motin 		iter->saved_individual_element_index = ITERATOR_INDEX_END;
449f6ad3f23SAlexander Motin 		return (NULL);
450f6ad3f23SAlexander Motin 	}
451f6ad3f23SAlexander Motin 
452f6ad3f23SAlexander Motin 	KASSERT((iter->type_index < ses_cache->ses_ntypes),
453f6ad3f23SAlexander Motin 		("Corrupted element iterator. %d not less than %d",
454f6ad3f23SAlexander Motin 		 iter->type_index, ses_cache->ses_ntypes));
455f6ad3f23SAlexander Motin 
456f6ad3f23SAlexander Motin 	element_type = &ses_cache->ses_types[iter->type_index];
457f6ad3f23SAlexander Motin 	iter->global_element_index++;
458f6ad3f23SAlexander Motin 	iter->type_element_index++;
459f6ad3f23SAlexander Motin 
460f6ad3f23SAlexander Motin 	/*
461f6ad3f23SAlexander Motin 	 * There is an object for overal type status in addition
462f6ad3f23SAlexander Motin 	 * to one for each allowed element, but only if the element
463f6ad3f23SAlexander Motin 	 * count is non-zero.
464f6ad3f23SAlexander Motin 	 */
465f6ad3f23SAlexander Motin 	if (iter->type_element_index > element_type->hdr->etype_maxelt) {
466f6ad3f23SAlexander Motin 		/*
467f6ad3f23SAlexander Motin 		 * We've exhausted the elements of this type.
468f6ad3f23SAlexander Motin 		 * This next element belongs to the next type.
469f6ad3f23SAlexander Motin 		 */
470f6ad3f23SAlexander Motin 		iter->type_index++;
471f6ad3f23SAlexander Motin 		iter->type_element_index = 0;
472f6ad3f23SAlexander Motin 		iter->individual_element_index = ITERATOR_INDEX_INVALID;
473f6ad3f23SAlexander Motin 	}
474f6ad3f23SAlexander Motin 
475f6ad3f23SAlexander Motin 	if (iter->type_element_index > 0) {
4767318fcb5SAlexander Motin 		iter->individual_element_index =
4777318fcb5SAlexander Motin 		    ++iter->saved_individual_element_index;
478f6ad3f23SAlexander Motin 	}
479f6ad3f23SAlexander Motin 
480f6ad3f23SAlexander Motin 	return (&iter->cache->elm_map[iter->global_element_index]);
481f6ad3f23SAlexander Motin }
482f6ad3f23SAlexander Motin 
483f6ad3f23SAlexander Motin /**
484f6ad3f23SAlexander Motin  * Element index types tracked by a SES iterator.
485f6ad3f23SAlexander Motin  */
486f6ad3f23SAlexander Motin typedef enum {
487f6ad3f23SAlexander Motin 	/**
488f6ad3f23SAlexander Motin 	 * Index relative to all elements (overall and individual)
489f6ad3f23SAlexander Motin 	 * in the system.
490f6ad3f23SAlexander Motin 	 */
491f6ad3f23SAlexander Motin 	SES_ELEM_INDEX_GLOBAL,
492f6ad3f23SAlexander Motin 
493f6ad3f23SAlexander Motin 	/**
494f6ad3f23SAlexander Motin 	 * \brief Index relative to all individual elements in the system.
495f6ad3f23SAlexander Motin 	 *
496f6ad3f23SAlexander Motin 	 * This index counts only individual elements, skipping overall
497f6ad3f23SAlexander Motin 	 * status elements.  This is the index space of the additional
498f6ad3f23SAlexander Motin 	 * element status page (page 0xa).
499f6ad3f23SAlexander Motin 	 */
500f6ad3f23SAlexander Motin 	SES_ELEM_INDEX_INDIVIDUAL
501f6ad3f23SAlexander Motin } ses_elem_index_type_t;
502f6ad3f23SAlexander Motin 
503f6ad3f23SAlexander Motin /**
504f6ad3f23SAlexander Motin  * \brief Move the provided iterator forwards or backwards to the object
505f6ad3f23SAlexander Motin  *        having the give index.
506f6ad3f23SAlexander Motin  *
507f6ad3f23SAlexander Motin  * \param iter           The iterator on which to perform the seek.
508f6ad3f23SAlexander Motin  * \param element_index  The index of the element to find.
509f6ad3f23SAlexander Motin  * \param index_type     The type (global or individual) of element_index.
510f6ad3f23SAlexander Motin  *
511f6ad3f23SAlexander Motin  * \return  If the element is found, a pointer to it's enc_element_t.
512f6ad3f23SAlexander Motin  *          Otherwise NULL.
513f6ad3f23SAlexander Motin  */
514f6ad3f23SAlexander Motin static enc_element_t *
ses_iter_seek_to(struct ses_iterator * iter,int element_index,ses_elem_index_type_t index_type)515f6ad3f23SAlexander Motin ses_iter_seek_to(struct ses_iterator *iter, int element_index,
516f6ad3f23SAlexander Motin 		 ses_elem_index_type_t index_type)
517f6ad3f23SAlexander Motin {
518f6ad3f23SAlexander Motin 	enc_element_t	*element;
519f6ad3f23SAlexander Motin 	int		*cur_index;
520f6ad3f23SAlexander Motin 
521f6ad3f23SAlexander Motin 	if (index_type == SES_ELEM_INDEX_GLOBAL)
522f6ad3f23SAlexander Motin 		cur_index = &iter->global_element_index;
523f6ad3f23SAlexander Motin 	else
524f6ad3f23SAlexander Motin 		cur_index = &iter->individual_element_index;
525f6ad3f23SAlexander Motin 
526f6ad3f23SAlexander Motin 	if (*cur_index == element_index) {
527f6ad3f23SAlexander Motin 		/* Already there. */
528f6ad3f23SAlexander Motin 		return (&iter->cache->elm_map[iter->global_element_index]);
529f6ad3f23SAlexander Motin 	}
530f6ad3f23SAlexander Motin 
531f6ad3f23SAlexander Motin 	ses_iter_reset(iter);
532f6ad3f23SAlexander Motin 	while ((element = ses_iter_next(iter)) != NULL
533f6ad3f23SAlexander Motin 	    && *cur_index != element_index)
534f6ad3f23SAlexander Motin 		;
535f6ad3f23SAlexander Motin 
536f6ad3f23SAlexander Motin 	if (*cur_index != element_index)
537f6ad3f23SAlexander Motin 		return (NULL);
538f6ad3f23SAlexander Motin 
539f6ad3f23SAlexander Motin 	return (element);
540f6ad3f23SAlexander Motin }
541f6ad3f23SAlexander Motin 
542f6ad3f23SAlexander Motin #if 0
543f6ad3f23SAlexander Motin static int ses_encode(enc_softc_t *, uint8_t *, int, int,
544f6ad3f23SAlexander Motin     struct ses_comstat *);
545f6ad3f23SAlexander Motin #endif
546f6ad3f23SAlexander Motin static int ses_set_timed_completion(enc_softc_t *, uint8_t);
547f6ad3f23SAlexander Motin #if 0
548f6ad3f23SAlexander Motin static int ses_putstatus(enc_softc_t *, int, struct ses_comstat *);
549f6ad3f23SAlexander Motin #endif
550f6ad3f23SAlexander Motin 
5517027c01cSAlexander Motin static void ses_poll_status(enc_softc_t *);
552f6ad3f23SAlexander Motin static void ses_print_addl_data(enc_softc_t *, enc_element_t *);
553f6ad3f23SAlexander Motin 
554f6ad3f23SAlexander Motin /*=========================== SES cleanup routines ===========================*/
555f6ad3f23SAlexander Motin 
556f6ad3f23SAlexander Motin static void
ses_cache_free_elm_addlstatus(enc_softc_t * enc,enc_cache_t * cache)557f6ad3f23SAlexander Motin ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
558f6ad3f23SAlexander Motin {
559f6ad3f23SAlexander Motin 	ses_cache_t   *ses_cache;
560f6ad3f23SAlexander Motin 	ses_cache_t   *other_ses_cache;
561f6ad3f23SAlexander Motin 	enc_element_t *cur_elm;
562f6ad3f23SAlexander Motin 	enc_element_t *last_elm;
563f6ad3f23SAlexander Motin 
564f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter\n", __func__);
565f6ad3f23SAlexander Motin 	ses_cache = cache->private;
566f6ad3f23SAlexander Motin 	if (ses_cache->elm_addlstatus_page == NULL)
567f6ad3f23SAlexander Motin 		return;
568f6ad3f23SAlexander Motin 
569f6ad3f23SAlexander Motin 	for (cur_elm = cache->elm_map,
570d7a52e7bSAlexander Motin 	     last_elm = &cache->elm_map[cache->nelms];
571d7a52e7bSAlexander Motin 	     cur_elm != last_elm; cur_elm++) {
572f6ad3f23SAlexander Motin 		ses_element_t *elmpriv;
573f6ad3f23SAlexander Motin 
574f6ad3f23SAlexander Motin 		elmpriv = cur_elm->elm_private;
575f6ad3f23SAlexander Motin 
576f6ad3f23SAlexander Motin 		/* Clear references to the additional status page. */
577f6ad3f23SAlexander Motin 		bzero(&elmpriv->addl, sizeof(elmpriv->addl));
578f6ad3f23SAlexander Motin 	}
579f6ad3f23SAlexander Motin 
580f6ad3f23SAlexander Motin 	other_ses_cache = enc_other_cache(enc, cache)->private;
581f6ad3f23SAlexander Motin 	if (other_ses_cache->elm_addlstatus_page
582f6ad3f23SAlexander Motin 	 != ses_cache->elm_addlstatus_page)
583f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->elm_addlstatus_page);
584f6ad3f23SAlexander Motin 	ses_cache->elm_addlstatus_page = NULL;
585f6ad3f23SAlexander Motin }
586f6ad3f23SAlexander Motin 
587f6ad3f23SAlexander Motin static void
ses_cache_free_elm_descs(enc_softc_t * enc,enc_cache_t * cache)588f6ad3f23SAlexander Motin ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
589f6ad3f23SAlexander Motin {
590f6ad3f23SAlexander Motin 	ses_cache_t   *ses_cache;
591f6ad3f23SAlexander Motin 	ses_cache_t   *other_ses_cache;
592f6ad3f23SAlexander Motin 	enc_element_t *cur_elm;
593f6ad3f23SAlexander Motin 	enc_element_t *last_elm;
594f6ad3f23SAlexander Motin 
595f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter\n", __func__);
596f6ad3f23SAlexander Motin 	ses_cache = cache->private;
597f6ad3f23SAlexander Motin 	if (ses_cache->elm_descs_page == NULL)
598f6ad3f23SAlexander Motin 		return;
599f6ad3f23SAlexander Motin 
600f6ad3f23SAlexander Motin 	for (cur_elm = cache->elm_map,
601d7a52e7bSAlexander Motin 	     last_elm = &cache->elm_map[cache->nelms];
602d7a52e7bSAlexander Motin 	     cur_elm != last_elm; cur_elm++) {
603f6ad3f23SAlexander Motin 		ses_element_t *elmpriv;
604f6ad3f23SAlexander Motin 
605f6ad3f23SAlexander Motin 		elmpriv = cur_elm->elm_private;
606f6ad3f23SAlexander Motin 		elmpriv->descr_len = 0;
607f6ad3f23SAlexander Motin 		elmpriv->descr = NULL;
608f6ad3f23SAlexander Motin 	}
609f6ad3f23SAlexander Motin 
610f6ad3f23SAlexander Motin 	other_ses_cache = enc_other_cache(enc, cache)->private;
611f6ad3f23SAlexander Motin 	if (other_ses_cache->elm_descs_page
612f6ad3f23SAlexander Motin 	 != ses_cache->elm_descs_page)
613f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->elm_descs_page);
614f6ad3f23SAlexander Motin 	ses_cache->elm_descs_page = NULL;
615f6ad3f23SAlexander Motin }
616f6ad3f23SAlexander Motin 
617f6ad3f23SAlexander Motin static void
ses_cache_free_status(enc_softc_t * enc,enc_cache_t * cache)618f6ad3f23SAlexander Motin ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
619f6ad3f23SAlexander Motin {
620f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
621f6ad3f23SAlexander Motin 	ses_cache_t *other_ses_cache;
622f6ad3f23SAlexander Motin 
623f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter\n", __func__);
624f6ad3f23SAlexander Motin 	ses_cache   = cache->private;
625f6ad3f23SAlexander Motin 	if (ses_cache->status_page == NULL)
626f6ad3f23SAlexander Motin 		return;
627f6ad3f23SAlexander Motin 
628f6ad3f23SAlexander Motin 	other_ses_cache = enc_other_cache(enc, cache)->private;
629f6ad3f23SAlexander Motin 	if (other_ses_cache->status_page != ses_cache->status_page)
630f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->status_page);
631f6ad3f23SAlexander Motin 	ses_cache->status_page = NULL;
632f6ad3f23SAlexander Motin }
633f6ad3f23SAlexander Motin 
634f6ad3f23SAlexander Motin static void
ses_cache_free_elm_map(enc_softc_t * enc,enc_cache_t * cache)635f6ad3f23SAlexander Motin ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
636f6ad3f23SAlexander Motin {
637f6ad3f23SAlexander Motin 	enc_element_t *cur_elm;
638f6ad3f23SAlexander Motin 	enc_element_t *last_elm;
639f6ad3f23SAlexander Motin 
640f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter\n", __func__);
641f6ad3f23SAlexander Motin 	if (cache->elm_map == NULL)
642f6ad3f23SAlexander Motin 		return;
643f6ad3f23SAlexander Motin 
644f6ad3f23SAlexander Motin 	ses_cache_free_elm_descs(enc, cache);
645f6ad3f23SAlexander Motin 	ses_cache_free_elm_addlstatus(enc, cache);
646f6ad3f23SAlexander Motin 	for (cur_elm = cache->elm_map,
647d7a52e7bSAlexander Motin 	     last_elm = &cache->elm_map[cache->nelms];
648d7a52e7bSAlexander Motin 	     cur_elm != last_elm; cur_elm++) {
649f6ad3f23SAlexander Motin 		ENC_FREE_AND_NULL(cur_elm->elm_private);
650f6ad3f23SAlexander Motin 	}
651f6ad3f23SAlexander Motin 	ENC_FREE_AND_NULL(cache->elm_map);
652f6ad3f23SAlexander Motin 	cache->nelms = 0;
653f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: exit\n", __func__);
654f6ad3f23SAlexander Motin }
655f6ad3f23SAlexander Motin 
656f6ad3f23SAlexander Motin static void
ses_cache_free(enc_softc_t * enc,enc_cache_t * cache)657f6ad3f23SAlexander Motin ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
658f6ad3f23SAlexander Motin {
659f6ad3f23SAlexander Motin 	ses_cache_t *other_ses_cache;
660f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
661f6ad3f23SAlexander Motin 
662f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter\n", __func__);
663f6ad3f23SAlexander Motin 	ses_cache_free_elm_addlstatus(enc, cache);
664f6ad3f23SAlexander Motin 	ses_cache_free_status(enc, cache);
665f6ad3f23SAlexander Motin 	ses_cache_free_elm_map(enc, cache);
666f6ad3f23SAlexander Motin 
667f6ad3f23SAlexander Motin 	ses_cache = cache->private;
668f6ad3f23SAlexander Motin 	ses_cache->ses_ntypes = 0;
669f6ad3f23SAlexander Motin 
670f6ad3f23SAlexander Motin 	other_ses_cache = enc_other_cache(enc, cache)->private;
671f6ad3f23SAlexander Motin 	if (other_ses_cache->subencs != ses_cache->subencs)
672f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->subencs);
673f6ad3f23SAlexander Motin 	ses_cache->subencs = NULL;
674f6ad3f23SAlexander Motin 
675f6ad3f23SAlexander Motin 	if (other_ses_cache->ses_types != ses_cache->ses_types)
676f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->ses_types);
677f6ad3f23SAlexander Motin 	ses_cache->ses_types = NULL;
678f6ad3f23SAlexander Motin 
679f6ad3f23SAlexander Motin 	if (other_ses_cache->cfg_page != ses_cache->cfg_page)
680f6ad3f23SAlexander Motin 		ENC_FREE(ses_cache->cfg_page);
681f6ad3f23SAlexander Motin 	ses_cache->cfg_page = NULL;
682f6ad3f23SAlexander Motin 
683f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: exit\n", __func__);
684f6ad3f23SAlexander Motin }
685f6ad3f23SAlexander Motin 
686f6ad3f23SAlexander Motin static void
ses_cache_clone(enc_softc_t * enc,enc_cache_t * src,enc_cache_t * dst)687f6ad3f23SAlexander Motin ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
688f6ad3f23SAlexander Motin {
689f6ad3f23SAlexander Motin 	ses_cache_t   *dst_ses_cache;
690f6ad3f23SAlexander Motin 	ses_cache_t   *src_ses_cache;
691f6ad3f23SAlexander Motin 	enc_element_t *src_elm;
692f6ad3f23SAlexander Motin 	enc_element_t *dst_elm;
693f6ad3f23SAlexander Motin 	enc_element_t *last_elm;
694f6ad3f23SAlexander Motin 
695f6ad3f23SAlexander Motin 	ses_cache_free(enc, dst);
696f6ad3f23SAlexander Motin 	src_ses_cache = src->private;
697f6ad3f23SAlexander Motin 	dst_ses_cache = dst->private;
698f6ad3f23SAlexander Motin 
699f6ad3f23SAlexander Motin 	/*
700f6ad3f23SAlexander Motin 	 * The cloned enclosure cache and ses specific cache are
701f6ad3f23SAlexander Motin 	 * mostly identical to the source.
702f6ad3f23SAlexander Motin 	 */
703f6ad3f23SAlexander Motin 	*dst = *src;
704f6ad3f23SAlexander Motin 	*dst_ses_cache = *src_ses_cache;
705f6ad3f23SAlexander Motin 
706f6ad3f23SAlexander Motin 	/*
707f6ad3f23SAlexander Motin 	 * But the ses cache storage is still independent.  Restore
708f6ad3f23SAlexander Motin 	 * the pointer that was clobbered by the structure copy above.
709f6ad3f23SAlexander Motin 	 */
710f6ad3f23SAlexander Motin 	dst->private = dst_ses_cache;
711f6ad3f23SAlexander Motin 
712f6ad3f23SAlexander Motin 	/*
713f6ad3f23SAlexander Motin 	 * The element map is independent even though it starts out
714f6ad3f23SAlexander Motin 	 * pointing to the same constant page data.
715f6ad3f23SAlexander Motin 	 */
716ed246e82SAlexander Motin 	dst->elm_map = malloc(dst->nelms * sizeof(enc_element_t),
717ed246e82SAlexander Motin 	    M_SCSIENC, M_WAITOK);
718f6ad3f23SAlexander Motin 	memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
719f6ad3f23SAlexander Motin 	for (dst_elm = dst->elm_map, src_elm = src->elm_map,
720d7a52e7bSAlexander Motin 	     last_elm = &src->elm_map[src->nelms];
721d7a52e7bSAlexander Motin 	     src_elm != last_elm; src_elm++, dst_elm++) {
722ed246e82SAlexander Motin 		dst_elm->elm_private = malloc(sizeof(ses_element_t),
723ed246e82SAlexander Motin 		    M_SCSIENC, M_WAITOK);
724f6ad3f23SAlexander Motin 		memcpy(dst_elm->elm_private, src_elm->elm_private,
725f6ad3f23SAlexander Motin 		       sizeof(ses_element_t));
726f6ad3f23SAlexander Motin 	}
727f6ad3f23SAlexander Motin }
728f6ad3f23SAlexander Motin 
729f6ad3f23SAlexander Motin /* Structure accessors.  These are strongly typed to avoid errors. */
730f6ad3f23SAlexander Motin 
731f6ad3f23SAlexander Motin int
ses_elm_sas_descr_type(union ses_elm_sas_hdr * obj)732f6ad3f23SAlexander Motin ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
733f6ad3f23SAlexander Motin {
734f6ad3f23SAlexander Motin 	return ((obj)->base_hdr.byte1 >> 6);
735f6ad3f23SAlexander Motin }
736f6ad3f23SAlexander Motin int
ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr * hdr)737f6ad3f23SAlexander Motin ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
738f6ad3f23SAlexander Motin {
739f6ad3f23SAlexander Motin 	return ((hdr)->byte0 & 0xf);
740f6ad3f23SAlexander Motin }
741f6ad3f23SAlexander Motin int
ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr * hdr)742f6ad3f23SAlexander Motin ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
743f6ad3f23SAlexander Motin {
744170a0a8eSAlan Somers 	return ((hdr)->byte0 >> 4 & 0x1);
745f6ad3f23SAlexander Motin }
746f6ad3f23SAlexander Motin int
ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr * hdr)747f6ad3f23SAlexander Motin ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
748f6ad3f23SAlexander Motin {
749f6ad3f23SAlexander Motin 	return ((hdr)->byte0 >> 7);
750f6ad3f23SAlexander Motin }
751f6ad3f23SAlexander Motin int
ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr * hdr)752f6ad3f23SAlexander Motin ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
753f6ad3f23SAlexander Motin {
754f6ad3f23SAlexander Motin 	return ((hdr)->type0_noneip.byte1 & 0x1);
755f6ad3f23SAlexander Motin }
756f6ad3f23SAlexander Motin int
ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy * phy)757f6ad3f23SAlexander Motin ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
758f6ad3f23SAlexander Motin {
759f6ad3f23SAlexander Motin 	return ((phy)->target_ports & 0x1);
760f6ad3f23SAlexander Motin }
761f6ad3f23SAlexander Motin int
ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy * phy)762f6ad3f23SAlexander Motin ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
763f6ad3f23SAlexander Motin {
764f6ad3f23SAlexander Motin 	return ((phy)->target_ports >> 7);
765f6ad3f23SAlexander Motin }
766f6ad3f23SAlexander Motin int
ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy * phy)767f6ad3f23SAlexander Motin ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
768f6ad3f23SAlexander Motin {
769f6ad3f23SAlexander Motin 	return (((phy)->byte0 >> 4) & 0x7);
770f6ad3f23SAlexander Motin }
771f6ad3f23SAlexander Motin 
772f6ad3f23SAlexander Motin /**
773f6ad3f23SAlexander Motin  * \brief Verify that the cached configuration data in our softc
774f6ad3f23SAlexander Motin  *        is valid for processing the page data corresponding to
775f6ad3f23SAlexander Motin  *        the provided page header.
776f6ad3f23SAlexander Motin  *
777f6ad3f23SAlexander Motin  * \param ses_cache The SES cache to validate.
778f6ad3f23SAlexander Motin  * \param gen_code  The 4 byte generation code from a SES diagnostic
779f6ad3f23SAlexander Motin  *		    page header.
780f6ad3f23SAlexander Motin  *
781f6ad3f23SAlexander Motin  * \return  non-zero if true, 0 if false.
782f6ad3f23SAlexander Motin  */
783f6ad3f23SAlexander Motin static int
ses_config_cache_valid(ses_cache_t * ses_cache,const uint8_t * gen_code)784f6ad3f23SAlexander Motin ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
785f6ad3f23SAlexander Motin {
786f6ad3f23SAlexander Motin 	uint32_t cache_gc;
787f6ad3f23SAlexander Motin 	uint32_t cur_gc;
788f6ad3f23SAlexander Motin 
789f6ad3f23SAlexander Motin 	if (ses_cache->cfg_page == NULL)
790f6ad3f23SAlexander Motin 		return (0);
791f6ad3f23SAlexander Motin 
792f6ad3f23SAlexander Motin 	cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
793f6ad3f23SAlexander Motin 	cur_gc   = scsi_4btoul(gen_code);
794f6ad3f23SAlexander Motin 	return (cache_gc == cur_gc);
795f6ad3f23SAlexander Motin }
796f6ad3f23SAlexander Motin 
797f6ad3f23SAlexander Motin /**
798f6ad3f23SAlexander Motin  * Function signature for consumers of the ses_devids_iter() interface.
799f6ad3f23SAlexander Motin  */
800f6ad3f23SAlexander Motin typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
801f6ad3f23SAlexander Motin 				  struct scsi_vpd_id_descriptor *, void *);
802f6ad3f23SAlexander Motin 
803f6ad3f23SAlexander Motin /**
804f6ad3f23SAlexander Motin  * \brief Iterate over and create vpd device id records from the
805f6ad3f23SAlexander Motin  *        additional element status data for elm, passing that data
806f6ad3f23SAlexander Motin  *        to the provided callback.
807f6ad3f23SAlexander Motin  *
808f6ad3f23SAlexander Motin  * \param enc	        SES instance containing elm
809f6ad3f23SAlexander Motin  * \param elm	        Element for which to extract device ID data.
810f6ad3f23SAlexander Motin  * \param callback      The callback function to invoke on each generated
811f6ad3f23SAlexander Motin  *                      device id descriptor for elm.
812f6ad3f23SAlexander Motin  * \param callback_arg  Argument passed through to callback on each invocation.
813f6ad3f23SAlexander Motin  */
814f6ad3f23SAlexander Motin static void
ses_devids_iter(enc_softc_t * enc,enc_element_t * elm,ses_devid_callback_t * callback,void * callback_arg)815f6ad3f23SAlexander Motin ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
816f6ad3f23SAlexander Motin 		ses_devid_callback_t *callback, void *callback_arg)
817f6ad3f23SAlexander Motin {
818f6ad3f23SAlexander Motin 	ses_element_t           *elmpriv;
819f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
820f6ad3f23SAlexander Motin 	u_int                   i;
821f6ad3f23SAlexander Motin 	size_t			devid_record_size;
822f6ad3f23SAlexander Motin 
823f6ad3f23SAlexander Motin 	elmpriv = elm->elm_private;
824f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
825f6ad3f23SAlexander Motin 
826f6ad3f23SAlexander Motin 	devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
827f6ad3f23SAlexander Motin 			  + sizeof(struct scsi_vpd_id_naa_ieee_reg);
828f6ad3f23SAlexander Motin 	for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
829f6ad3f23SAlexander Motin 		uint8_t			       devid_buf[devid_record_size];
830f6ad3f23SAlexander Motin 		struct scsi_vpd_id_descriptor *devid;
831f6ad3f23SAlexander Motin 		uint8_t			      *phy_addr;
832f6ad3f23SAlexander Motin 
833f6ad3f23SAlexander Motin 		devid = (struct scsi_vpd_id_descriptor *)devid_buf;
834f6ad3f23SAlexander Motin 		phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
835f6ad3f23SAlexander Motin 		devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
836f6ad3f23SAlexander Motin 				     | SVPD_ID_CODESET_BINARY;
837f6ad3f23SAlexander Motin 		devid->id_type       = SVPD_ID_PIV
838f6ad3f23SAlexander Motin 				     | SVPD_ID_ASSOC_PORT
839f6ad3f23SAlexander Motin 				     | SVPD_ID_TYPE_NAA;
840f6ad3f23SAlexander Motin 		devid->reserved	     = 0;
841f6ad3f23SAlexander Motin 		devid->length	     = sizeof(struct scsi_vpd_id_naa_ieee_reg);
842f6ad3f23SAlexander Motin 		memcpy(devid->identifier, phy_addr, devid->length);
843f6ad3f23SAlexander Motin 
844f6ad3f23SAlexander Motin 		callback(enc, elm, devid, callback_arg);
845f6ad3f23SAlexander Motin 	}
846f6ad3f23SAlexander Motin }
847f6ad3f23SAlexander Motin 
848f6ad3f23SAlexander Motin /**
849f6ad3f23SAlexander Motin  * Function signature for consumers of the ses_paths_iter() interface.
850f6ad3f23SAlexander Motin  */
851f6ad3f23SAlexander Motin typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
852f6ad3f23SAlexander Motin 				 struct cam_path *, void *);
853f6ad3f23SAlexander Motin 
854f6ad3f23SAlexander Motin /**
855f6ad3f23SAlexander Motin  * Argument package passed through ses_devids_iter() by
856f6ad3f23SAlexander Motin  * ses_paths_iter() to ses_path_iter_devid_callback().
857f6ad3f23SAlexander Motin  */
858f6ad3f23SAlexander Motin typedef struct ses_path_iter_args {
859f6ad3f23SAlexander Motin 	ses_path_callback_t *callback;
860f6ad3f23SAlexander Motin 	void		    *callback_arg;
861f6ad3f23SAlexander Motin } ses_path_iter_args_t;
862f6ad3f23SAlexander Motin 
863f6ad3f23SAlexander Motin /**
864f6ad3f23SAlexander Motin  * ses_devids_iter() callback function used by ses_paths_iter()
865f6ad3f23SAlexander Motin  * to map device ids to peripheral driver instances.
866f6ad3f23SAlexander Motin  *
867f6ad3f23SAlexander Motin  * \param enc	  SES instance containing elm
868f6ad3f23SAlexander Motin  * \param elm	  Element on which device ID matching is active.
869f6ad3f23SAlexander Motin  * \param periph  A device ID corresponding to elm.
870f6ad3f23SAlexander Motin  * \param arg     Argument passed through to callback on each invocation.
871f6ad3f23SAlexander Motin  */
872f6ad3f23SAlexander Motin static void
ses_path_iter_devid_callback(enc_softc_t * enc,enc_element_t * elem,struct scsi_vpd_id_descriptor * devid,void * arg)873f6ad3f23SAlexander Motin ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
874f6ad3f23SAlexander Motin 			       struct scsi_vpd_id_descriptor *devid,
875f6ad3f23SAlexander Motin 			       void *arg)
876f6ad3f23SAlexander Motin {
877f6ad3f23SAlexander Motin 	struct ccb_dev_match         cdm;
878f6ad3f23SAlexander Motin 	struct dev_match_pattern     match_pattern;
879f6ad3f23SAlexander Motin 	struct dev_match_result      match_result;
880f6ad3f23SAlexander Motin 	struct device_match_result  *device_match;
881f6ad3f23SAlexander Motin 	struct device_match_pattern *device_pattern;
882f6ad3f23SAlexander Motin 	ses_path_iter_args_t	    *args;
88307f7e4c8SAlexander Motin 	struct cam_path		    *path;
884f6ad3f23SAlexander Motin 
885f6ad3f23SAlexander Motin 	args = (ses_path_iter_args_t *)arg;
886f6ad3f23SAlexander Motin 	match_pattern.type = DEV_MATCH_DEVICE;
887f6ad3f23SAlexander Motin 	device_pattern = &match_pattern.pattern.device_pattern;
888f6ad3f23SAlexander Motin 	device_pattern->flags = DEV_MATCH_DEVID;
889f6ad3f23SAlexander Motin 	device_pattern->data.devid_pat.id_len =
890f6ad3f23SAlexander Motin 	    offsetof(struct scsi_vpd_id_descriptor, identifier)
891f6ad3f23SAlexander Motin 	  + devid->length;
892f6ad3f23SAlexander Motin 	memcpy(device_pattern->data.devid_pat.id, devid,
893f6ad3f23SAlexander Motin 	       device_pattern->data.devid_pat.id_len);
894730ea72cSAlexander Motin 	if (!ses_search_globally) {
895730ea72cSAlexander Motin 		device_pattern->flags |= DEV_MATCH_PATH;
896730ea72cSAlexander Motin 		device_pattern->path_id = xpt_path_path_id(enc->periph->path);
897730ea72cSAlexander Motin 	}
898f6ad3f23SAlexander Motin 
899f6ad3f23SAlexander Motin 	memset(&cdm, 0, sizeof(cdm));
900227d67aaSAlexander Motin 	if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
90192c40f40SAlexander Motin 			     CAM_XPT_PATH_ID,
902f6ad3f23SAlexander Motin 			     CAM_TARGET_WILDCARD,
903f6ad3f23SAlexander Motin 			     CAM_LUN_WILDCARD) != CAM_REQ_CMP)
904f6ad3f23SAlexander Motin 		return;
905f6ad3f23SAlexander Motin 
906f6ad3f23SAlexander Motin 	cdm.ccb_h.func_code = XPT_DEV_MATCH;
907f6ad3f23SAlexander Motin 	cdm.num_patterns    = 1;
908f6ad3f23SAlexander Motin 	cdm.patterns        = &match_pattern;
909f6ad3f23SAlexander Motin 	cdm.pattern_buf_len = sizeof(match_pattern);
910f6ad3f23SAlexander Motin 	cdm.match_buf_len   = sizeof(match_result);
911f6ad3f23SAlexander Motin 	cdm.matches         = &match_result;
912f6ad3f23SAlexander Motin 
91307f7e4c8SAlexander Motin 	do {
914f6ad3f23SAlexander Motin 		xpt_action((union ccb *)&cdm);
915f6ad3f23SAlexander Motin 
91607f7e4c8SAlexander Motin 		if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP ||
91707f7e4c8SAlexander Motin 		    (cdm.status != CAM_DEV_MATCH_LAST &&
91807f7e4c8SAlexander Motin 		     cdm.status != CAM_DEV_MATCH_MORE) ||
91907f7e4c8SAlexander Motin 		    cdm.num_matches == 0)
92007f7e4c8SAlexander Motin 			break;
921f6ad3f23SAlexander Motin 
922f6ad3f23SAlexander Motin 		device_match = &match_result.result.device_result;
92307f7e4c8SAlexander Motin 		if (xpt_create_path(&path, /*periph*/NULL,
924f6ad3f23SAlexander Motin 				    device_match->path_id,
925f6ad3f23SAlexander Motin 				    device_match->target_id,
92607f7e4c8SAlexander Motin 				    device_match->target_lun) == CAM_REQ_CMP) {
92707f7e4c8SAlexander Motin 			args->callback(enc, elem, path, args->callback_arg);
92807f7e4c8SAlexander Motin 
92907f7e4c8SAlexander Motin 			xpt_free_path(path);
93007f7e4c8SAlexander Motin 		}
93107f7e4c8SAlexander Motin 	} while (cdm.status == CAM_DEV_MATCH_MORE);
93224a1ba91SAlexander Motin 
933f6ad3f23SAlexander Motin 	xpt_free_path(cdm.ccb_h.path);
934f6ad3f23SAlexander Motin }
935f6ad3f23SAlexander Motin 
936f6ad3f23SAlexander Motin /**
937f6ad3f23SAlexander Motin  * \brief Iterate over and find the matching periph objects for the
938f6ad3f23SAlexander Motin  *        specified element.
939f6ad3f23SAlexander Motin  *
940f6ad3f23SAlexander Motin  * \param enc	        SES instance containing elm
941f6ad3f23SAlexander Motin  * \param elm	        Element for which to perform periph object matching.
942f6ad3f23SAlexander Motin  * \param callback      The callback function to invoke with each matching
943f6ad3f23SAlexander Motin  *                      periph object.
944f6ad3f23SAlexander Motin  * \param callback_arg  Argument passed through to callback on each invocation.
945f6ad3f23SAlexander Motin  */
946f6ad3f23SAlexander Motin static void
ses_paths_iter(enc_softc_t * enc,enc_element_t * elm,ses_path_callback_t * callback,void * callback_arg)947f6ad3f23SAlexander Motin ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
948f6ad3f23SAlexander Motin 	       ses_path_callback_t *callback, void *callback_arg)
949f6ad3f23SAlexander Motin {
95053f5ac13SAlexander Motin 	ses_element_t *elmpriv;
95153f5ac13SAlexander Motin 	struct ses_addl_status *addl;
95253f5ac13SAlexander Motin 
95353f5ac13SAlexander Motin 	elmpriv = elm->elm_private;
95453f5ac13SAlexander Motin 	addl = &(elmpriv->addl);
95553f5ac13SAlexander Motin 
95653f5ac13SAlexander Motin 	if (addl->hdr == NULL)
95753f5ac13SAlexander Motin 		return;
95853f5ac13SAlexander Motin 
959e4ee6c54SAlexander Motin 	switch(ses_elm_addlstatus_proto(addl->hdr)) {
960e4ee6c54SAlexander Motin 	case SPSP_PROTO_SAS:
96153f5ac13SAlexander Motin 		if (addl->proto_hdr.sas != NULL &&
96253f5ac13SAlexander Motin 		    addl->proto_data.sasdev_phys != NULL) {
963f6ad3f23SAlexander Motin 			ses_path_iter_args_t args;
964f6ad3f23SAlexander Motin 
965f6ad3f23SAlexander Motin 			args.callback     = callback;
966f6ad3f23SAlexander Motin 			args.callback_arg = callback_arg;
967e4ee6c54SAlexander Motin 			ses_devids_iter(enc, elm, ses_path_iter_devid_callback,
968e4ee6c54SAlexander Motin 			    &args);
969e4ee6c54SAlexander Motin 		}
970e4ee6c54SAlexander Motin 		break;
971e4ee6c54SAlexander Motin 	case SPSP_PROTO_ATA:
972e4ee6c54SAlexander Motin 		if (addl->proto_hdr.ata != NULL) {
97353f5ac13SAlexander Motin 			struct cam_path *path;
97453f5ac13SAlexander Motin 			struct ccb_getdev cgd;
97553f5ac13SAlexander Motin 
97653f5ac13SAlexander Motin 			if (xpt_create_path(&path, /*periph*/NULL,
97753f5ac13SAlexander Motin 			    scsi_4btoul(addl->proto_hdr.ata->bus),
97853f5ac13SAlexander Motin 			    scsi_4btoul(addl->proto_hdr.ata->target), 0)
97953f5ac13SAlexander Motin 			     != CAM_REQ_CMP)
98053f5ac13SAlexander Motin 				return;
98153f5ac13SAlexander Motin 
982ec5325dbSEdward Tomasz Napierala 			memset(&cgd, 0, sizeof(cgd));
98353f5ac13SAlexander Motin 			xpt_setup_ccb(&cgd.ccb_h, path, CAM_PRIORITY_NORMAL);
98453f5ac13SAlexander Motin 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
98553f5ac13SAlexander Motin 			xpt_action((union ccb *)&cgd);
986fd02926aSWarner Losh 			if (cam_ccb_success((union ccb *)&cgd))
98753f5ac13SAlexander Motin 				callback(enc, elm, path, callback_arg);
98853f5ac13SAlexander Motin 
98953f5ac13SAlexander Motin 			xpt_free_path(path);
99053f5ac13SAlexander Motin 		}
991e4ee6c54SAlexander Motin 		break;
992e4ee6c54SAlexander Motin 	}
993f6ad3f23SAlexander Motin }
994f6ad3f23SAlexander Motin 
995f6ad3f23SAlexander Motin /**
996f6ad3f23SAlexander Motin  * ses_paths_iter() callback function used by ses_get_elmdevname()
997f6ad3f23SAlexander Motin  * to record periph driver instance strings corresponding to a SES
998f6ad3f23SAlexander Motin  * element.
999f6ad3f23SAlexander Motin  *
1000f6ad3f23SAlexander Motin  * \param enc	  SES instance containing elm
1001f6ad3f23SAlexander Motin  * \param elm	  Element on which periph matching is active.
1002f6ad3f23SAlexander Motin  * \param periph  A periph instance that matches elm.
1003f6ad3f23SAlexander Motin  * \param arg     Argument passed through to callback on each invocation.
1004f6ad3f23SAlexander Motin  */
1005f6ad3f23SAlexander Motin static void
ses_elmdevname_callback(enc_softc_t * enc,enc_element_t * elem,struct cam_path * path,void * arg)1006f6ad3f23SAlexander Motin ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
1007f6ad3f23SAlexander Motin 			struct cam_path *path, void *arg)
1008f6ad3f23SAlexander Motin {
1009f6ad3f23SAlexander Motin 	struct sbuf *sb;
1010f6ad3f23SAlexander Motin 
1011f6ad3f23SAlexander Motin 	sb = (struct sbuf *)arg;
1012f6ad3f23SAlexander Motin 	cam_periph_list(path, sb);
1013f6ad3f23SAlexander Motin }
1014f6ad3f23SAlexander Motin 
1015f6ad3f23SAlexander Motin /**
1016f6ad3f23SAlexander Motin  * Argument package passed through ses_paths_iter() to
1017f6ad3f23SAlexander Motin  * ses_getcampath_callback.
1018f6ad3f23SAlexander Motin  */
1019f6ad3f23SAlexander Motin typedef struct ses_setphyspath_callback_args {
1020f6ad3f23SAlexander Motin 	struct sbuf *physpath;
1021f6ad3f23SAlexander Motin 	int          num_set;
1022f6ad3f23SAlexander Motin } ses_setphyspath_callback_args_t;
1023f6ad3f23SAlexander Motin 
1024f6ad3f23SAlexander Motin /**
1025f6ad3f23SAlexander Motin  * \brief ses_paths_iter() callback to set the physical path on the
1026f6ad3f23SAlexander Motin  *        CAM EDT entries corresponding to a given SES element.
1027f6ad3f23SAlexander Motin  *
1028f6ad3f23SAlexander Motin  * \param enc	  SES instance containing elm
1029f6ad3f23SAlexander Motin  * \param elm	  Element on which periph matching is active.
1030f6ad3f23SAlexander Motin  * \param periph  A periph instance that matches elm.
1031f6ad3f23SAlexander Motin  * \param arg     Argument passed through to callback on each invocation.
1032f6ad3f23SAlexander Motin  */
1033f6ad3f23SAlexander Motin static void
ses_setphyspath_callback(enc_softc_t * enc,enc_element_t * elm,struct cam_path * path,void * arg)1034f6ad3f23SAlexander Motin ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1035f6ad3f23SAlexander Motin 			 struct cam_path *path, void *arg)
1036f6ad3f23SAlexander Motin {
1037f6ad3f23SAlexander Motin 	struct ccb_dev_advinfo cdai;
1038f6ad3f23SAlexander Motin 	ses_setphyspath_callback_args_t *args;
1039f6ad3f23SAlexander Motin 	char *old_physpath;
1040f6ad3f23SAlexander Motin 
1041f6ad3f23SAlexander Motin 	args = (ses_setphyspath_callback_args_t *)arg;
1042f6ad3f23SAlexander Motin 	old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
10436a216c0bSAlexander Motin 	xpt_path_lock(path);
1044ec5325dbSEdward Tomasz Napierala 	memset(&cdai, 0, sizeof(cdai));
1045f6ad3f23SAlexander Motin 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1046f6ad3f23SAlexander Motin 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1047f6ad3f23SAlexander Motin 	cdai.buftype = CDAI_TYPE_PHYS_PATH;
1048e8577fb4SKenneth D. Merry 	cdai.flags = CDAI_FLAG_NONE;
1049f6ad3f23SAlexander Motin 	cdai.bufsiz = MAXPATHLEN;
1050f6ad3f23SAlexander Motin 	cdai.buf = old_physpath;
1051f6ad3f23SAlexander Motin 	xpt_action((union ccb *)&cdai);
1052f6ad3f23SAlexander Motin 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1053f6ad3f23SAlexander Motin 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1054f6ad3f23SAlexander Motin 
1055f6ad3f23SAlexander Motin 	if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1056f6ad3f23SAlexander Motin 		xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1057f6ad3f23SAlexander Motin 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1058f6ad3f23SAlexander Motin 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
1059e8577fb4SKenneth D. Merry 		cdai.flags = CDAI_FLAG_STORE;
1060f6ad3f23SAlexander Motin 		cdai.bufsiz = sbuf_len(args->physpath);
1061f6ad3f23SAlexander Motin 		cdai.buf = sbuf_data(args->physpath);
1062f6ad3f23SAlexander Motin 		xpt_action((union ccb *)&cdai);
1063f6ad3f23SAlexander Motin 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1064f6ad3f23SAlexander Motin 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1065fd02926aSWarner Losh 		if (cam_ccb_success((union ccb *)&cdai))
1066f6ad3f23SAlexander Motin 			args->num_set++;
1067f6ad3f23SAlexander Motin 	}
10686a216c0bSAlexander Motin 	xpt_path_unlock(path);
1069f6ad3f23SAlexander Motin 	free(old_physpath, M_SCSIENC);
1070f6ad3f23SAlexander Motin }
1071f6ad3f23SAlexander Motin 
1072f6ad3f23SAlexander Motin /**
1073f6ad3f23SAlexander Motin  * \brief Set a device's physical path string in CAM XPT.
1074f6ad3f23SAlexander Motin  *
1075f6ad3f23SAlexander Motin  * \param enc	SES instance containing elm
1076f6ad3f23SAlexander Motin  * \param elm	Element to publish physical path string for
1077f6ad3f23SAlexander Motin  * \param iter	Iterator whose state corresponds to elm
1078f6ad3f23SAlexander Motin  *
1079f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
1080f6ad3f23SAlexander Motin  */
1081f6ad3f23SAlexander Motin static int
ses_set_physpath(enc_softc_t * enc,enc_element_t * elm,struct ses_iterator * iter)1082f6ad3f23SAlexander Motin ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1083f6ad3f23SAlexander Motin 		 struct ses_iterator *iter)
1084f6ad3f23SAlexander Motin {
1085f6ad3f23SAlexander Motin 	struct ccb_dev_advinfo cdai;
1086f6ad3f23SAlexander Motin 	ses_setphyspath_callback_args_t args;
1087a9f2ac59SJaakko Heinonen 	int i, ret;
1088f6ad3f23SAlexander Motin 	struct sbuf sb;
1089ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
1090ccba7102SAlexander Motin 	uint8_t *devid;
1091f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
1092a9f2ac59SJaakko Heinonen 	const char *c;
1093f6ad3f23SAlexander Motin 
1094f6ad3f23SAlexander Motin 	ret = EIO;
1095f6ad3f23SAlexander Motin 	devid = NULL;
1096f6ad3f23SAlexander Motin 
109753f5ac13SAlexander Motin 	elmpriv = elm->elm_private;
109853f5ac13SAlexander Motin 	if (elmpriv->addl.hdr == NULL)
109953f5ac13SAlexander Motin 		goto out;
110053f5ac13SAlexander Motin 
1101f6ad3f23SAlexander Motin 	/*
1102f6ad3f23SAlexander Motin 	 * Assemble the components of the physical path starting with
1103f6ad3f23SAlexander Motin 	 * the device ID of the enclosure itself.
1104f6ad3f23SAlexander Motin 	 */
1105ec5325dbSEdward Tomasz Napierala 	memset(&cdai, 0, sizeof(cdai));
1106f6ad3f23SAlexander Motin 	xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1107f6ad3f23SAlexander Motin 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1108ab4327bbSAlexander Motin 	cdai.flags = CDAI_FLAG_NONE;
1109f6ad3f23SAlexander Motin 	cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1110f6ad3f23SAlexander Motin 	cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1111ed246e82SAlexander Motin 	cdai.buf = devid = malloc(cdai.bufsiz, M_SCSIENC, M_WAITOK|M_ZERO);
111224a1ba91SAlexander Motin 	cam_periph_lock(enc->periph);
1113f6ad3f23SAlexander Motin 	xpt_action((union ccb *)&cdai);
1114f6ad3f23SAlexander Motin 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1115f6ad3f23SAlexander Motin 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
111624a1ba91SAlexander Motin 	cam_periph_unlock(enc->periph);
1117f6ad3f23SAlexander Motin 	if (cdai.ccb_h.status != CAM_REQ_CMP)
1118f6ad3f23SAlexander Motin 		goto out;
1119f6ad3f23SAlexander Motin 
1120ccba7102SAlexander Motin 	idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1121f6ad3f23SAlexander Motin 	    cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1122ccba7102SAlexander Motin 	if (idd == NULL)
1123f6ad3f23SAlexander Motin 		goto out;
1124f6ad3f23SAlexander Motin 
1125f6ad3f23SAlexander Motin 	if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1126f6ad3f23SAlexander Motin 		ret = ENOMEM;
1127f6ad3f23SAlexander Motin 		goto out;
1128f6ad3f23SAlexander Motin 	}
1129f6ad3f23SAlexander Motin 	/* Next, generate the physical path string */
1130f6ad3f23SAlexander Motin 	sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1131ccba7102SAlexander Motin 	    scsi_8btou64(idd->identifier), iter->type_index,
1132f6ad3f23SAlexander Motin 	    iter->type_element_index);
1133f6ad3f23SAlexander Motin 	/* Append the element descriptor if one exists */
1134f6ad3f23SAlexander Motin 	if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1135f6ad3f23SAlexander Motin 		sbuf_cat(&sb, "/elmdesc@");
1136a9f2ac59SJaakko Heinonen 		for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1137a9f2ac59SJaakko Heinonen 		    i++, c++) {
1138a9f2ac59SJaakko Heinonen 			if (!isprint(*c) || isspace(*c) || *c == '/')
1139a9f2ac59SJaakko Heinonen 				sbuf_putc(&sb, '_');
1140a9f2ac59SJaakko Heinonen 			else
1141a9f2ac59SJaakko Heinonen 				sbuf_putc(&sb, *c);
1142a9f2ac59SJaakko Heinonen 		}
1143f6ad3f23SAlexander Motin 	}
1144f6ad3f23SAlexander Motin 	sbuf_finish(&sb);
1145f6ad3f23SAlexander Motin 
1146f6ad3f23SAlexander Motin 	/*
1147f6ad3f23SAlexander Motin 	 * Set this physical path on any CAM devices with a device ID
1148f6ad3f23SAlexander Motin 	 * descriptor that matches one created from the SES additional
1149f6ad3f23SAlexander Motin 	 * status data for this element.
1150f6ad3f23SAlexander Motin 	 */
1151f6ad3f23SAlexander Motin 	args.physpath= &sb;
1152f6ad3f23SAlexander Motin 	args.num_set = 0;
1153f6ad3f23SAlexander Motin 	ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1154f6ad3f23SAlexander Motin 	sbuf_delete(&sb);
1155f6ad3f23SAlexander Motin 
1156f6ad3f23SAlexander Motin 	ret = args.num_set == 0 ? ENOENT : 0;
1157f6ad3f23SAlexander Motin 
1158f6ad3f23SAlexander Motin out:
1159f6ad3f23SAlexander Motin 	if (devid != NULL)
1160f6ad3f23SAlexander Motin 		ENC_FREE(devid);
1161f6ad3f23SAlexander Motin 	return (ret);
1162f6ad3f23SAlexander Motin }
1163f6ad3f23SAlexander Motin 
1164f6ad3f23SAlexander Motin /**
1165f6ad3f23SAlexander Motin  * \brief Helper to set the CDB fields appropriately.
1166f6ad3f23SAlexander Motin  *
1167f6ad3f23SAlexander Motin  * \param cdb		Buffer containing the cdb.
1168f6ad3f23SAlexander Motin  * \param pagenum	SES diagnostic page to query for.
1169f6ad3f23SAlexander Motin  * \param dir		Direction of query.
1170f6ad3f23SAlexander Motin  */
1171f6ad3f23SAlexander Motin static void
ses_page_cdb(char * cdb,int bufsiz,SesDiagPageCodes pagenum,int dir)1172f6ad3f23SAlexander Motin ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1173f6ad3f23SAlexander Motin {
1174f6ad3f23SAlexander Motin 
1175f6ad3f23SAlexander Motin 	/* Ref: SPC-4 r25 Section 6.20 Table 223 */
1176f6ad3f23SAlexander Motin 	if (dir == CAM_DIR_IN) {
1177f6ad3f23SAlexander Motin 		cdb[0] = RECEIVE_DIAGNOSTIC;
1178f6ad3f23SAlexander Motin 		cdb[1] = 1; /* Set page code valid bit */
1179f6ad3f23SAlexander Motin 		cdb[2] = pagenum;
1180f6ad3f23SAlexander Motin 	} else {
1181f6ad3f23SAlexander Motin 		cdb[0] = SEND_DIAGNOSTIC;
1182f6ad3f23SAlexander Motin 		cdb[1] = 0x10;
1183f6ad3f23SAlexander Motin 		cdb[2] = pagenum;
1184f6ad3f23SAlexander Motin 	}
1185f6ad3f23SAlexander Motin 	cdb[3] = bufsiz >> 8;	/* high bits */
1186f6ad3f23SAlexander Motin 	cdb[4] = bufsiz & 0xff;	/* low bits */
1187f6ad3f23SAlexander Motin 	cdb[5] = 0;
1188f6ad3f23SAlexander Motin }
1189f6ad3f23SAlexander Motin 
1190f6ad3f23SAlexander Motin /**
1191f6ad3f23SAlexander Motin  * \brief Discover whether this instance supports timed completion of a
1192f6ad3f23SAlexander Motin  * 	  RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1193f6ad3f23SAlexander Motin  * 	  page, and store the result in the softc, updating if necessary.
1194f6ad3f23SAlexander Motin  *
1195f6ad3f23SAlexander Motin  * \param enc	SES instance to query and update.
1196f6ad3f23SAlexander Motin  * \param tc_en	Value of timed completion to set (see \return).
1197f6ad3f23SAlexander Motin  *
1198f6ad3f23SAlexander Motin  * \return	1 if timed completion enabled, 0 otherwise.
1199f6ad3f23SAlexander Motin  */
1200f6ad3f23SAlexander Motin static int
ses_set_timed_completion(enc_softc_t * enc,uint8_t tc_en)1201f6ad3f23SAlexander Motin ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1202f6ad3f23SAlexander Motin {
1203f6ad3f23SAlexander Motin 	union ccb *ccb;
1204f6ad3f23SAlexander Motin 	struct cam_periph *periph;
1205f6ad3f23SAlexander Motin 	struct ses_mgmt_mode_page *mgmt;
1206f6ad3f23SAlexander Motin 	uint8_t *mode_buf;
1207f6ad3f23SAlexander Motin 	size_t mode_buf_len;
1208f6ad3f23SAlexander Motin 	ses_softc_t *ses;
1209f6ad3f23SAlexander Motin 
1210f6ad3f23SAlexander Motin 	periph = enc->periph;
1211f6ad3f23SAlexander Motin 	ses = enc->enc_private;
1212f6ad3f23SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1213f6ad3f23SAlexander Motin 
1214f6ad3f23SAlexander Motin 	mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1215f6ad3f23SAlexander Motin 	mode_buf = ENC_MALLOCZ(mode_buf_len);
1216f6ad3f23SAlexander Motin 	if (mode_buf == NULL)
1217f6ad3f23SAlexander Motin 		goto out;
1218f6ad3f23SAlexander Motin 
1219227d67aaSAlexander Motin 	scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1220f6ad3f23SAlexander Motin 	    /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1221f6ad3f23SAlexander Motin 	    mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1222f6ad3f23SAlexander Motin 
1223f6ad3f23SAlexander Motin 	/*
1224f6ad3f23SAlexander Motin 	 * Ignore illegal request errors, as they are quite common and we
1225f6ad3f23SAlexander Motin 	 * will print something out in that case anyway.
1226f6ad3f23SAlexander Motin 	 */
12278a27a339SWarner Losh 	cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1228f6ad3f23SAlexander Motin 	    ENC_FLAGS|SF_QUIET_IR, NULL);
1229f6ad3f23SAlexander Motin 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
12305e6609a2SMatt Jacob 		ENC_VLOG(enc, "Timed Completion Unsupported\n");
1231f6ad3f23SAlexander Motin 		goto release;
1232f6ad3f23SAlexander Motin 	}
1233f6ad3f23SAlexander Motin 
1234f6ad3f23SAlexander Motin 	/* Skip the mode select if the desired value is already set */
1235f6ad3f23SAlexander Motin 	mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1236f6ad3f23SAlexander Motin 	if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1237f6ad3f23SAlexander Motin 		goto done;
1238f6ad3f23SAlexander Motin 
1239f6ad3f23SAlexander Motin 	/* Value is not what we wanted, set it */
1240f6ad3f23SAlexander Motin 	if (tc_en)
1241f6ad3f23SAlexander Motin 		mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1242f6ad3f23SAlexander Motin 	else
1243f6ad3f23SAlexander Motin 		mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1244f6ad3f23SAlexander Motin 	/* SES2r20: a completion time of zero means as long as possible */
1245f6ad3f23SAlexander Motin 	bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1246f6ad3f23SAlexander Motin 
1247227d67aaSAlexander Motin 	scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1248f6ad3f23SAlexander Motin 	    /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1249f6ad3f23SAlexander Motin 	    SSD_FULL_SIZE, /*timeout*/60 * 1000);
1250f6ad3f23SAlexander Motin 
12518a27a339SWarner Losh 	cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1252f6ad3f23SAlexander Motin 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
12535e6609a2SMatt Jacob 		ENC_VLOG(enc, "Timed Completion Set Failed\n");
1254f6ad3f23SAlexander Motin 		goto release;
1255f6ad3f23SAlexander Motin 	}
1256f6ad3f23SAlexander Motin 
1257f6ad3f23SAlexander Motin done:
1258f6ad3f23SAlexander Motin 	if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1259f6ad3f23SAlexander Motin 		ENC_LOG(enc, "Timed Completion Enabled\n");
1260f6ad3f23SAlexander Motin 		ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1261f6ad3f23SAlexander Motin 	} else {
1262f6ad3f23SAlexander Motin 		ENC_LOG(enc, "Timed Completion Disabled\n");
1263f6ad3f23SAlexander Motin 		ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1264f6ad3f23SAlexander Motin 	}
1265f6ad3f23SAlexander Motin release:
1266f6ad3f23SAlexander Motin 	ENC_FREE(mode_buf);
1267f6ad3f23SAlexander Motin 	xpt_release_ccb(ccb);
1268f6ad3f23SAlexander Motin out:
1269f6ad3f23SAlexander Motin 	return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1270f6ad3f23SAlexander Motin }
1271f6ad3f23SAlexander Motin 
1272f6ad3f23SAlexander Motin /**
1273f6ad3f23SAlexander Motin  * \brief Process the list of supported pages and update flags.
1274f6ad3f23SAlexander Motin  *
1275f6ad3f23SAlexander Motin  * \param enc       SES device to query.
1276f6ad3f23SAlexander Motin  * \param buf       Buffer containing the config page.
1277f6ad3f23SAlexander Motin  * \param xfer_len  Length of the config page in the buffer.
1278f6ad3f23SAlexander Motin  *
1279f6ad3f23SAlexander Motin  * \return  0 on success, errno otherwise.
1280f6ad3f23SAlexander Motin  */
1281f6ad3f23SAlexander Motin static int
ses_process_pages(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1282f6ad3f23SAlexander Motin ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1283f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1284f6ad3f23SAlexander Motin {
1285f6ad3f23SAlexander Motin 	ses_softc_t *ses;
1286f6ad3f23SAlexander Motin 	struct scsi_diag_page *page;
1287f6ad3f23SAlexander Motin 	int err, i, length;
1288f6ad3f23SAlexander Motin 
1289f6ad3f23SAlexander Motin 	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1290f6ad3f23SAlexander Motin 	    ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1291f6ad3f23SAlexander Motin 	ses = enc->enc_private;
1292f6ad3f23SAlexander Motin 	err = -1;
1293f6ad3f23SAlexander Motin 
1294f6ad3f23SAlexander Motin 	if (error != 0) {
1295f6ad3f23SAlexander Motin 		err = error;
1296f6ad3f23SAlexander Motin 		goto out;
1297f6ad3f23SAlexander Motin 	}
1298f6ad3f23SAlexander Motin 	if (xfer_len < sizeof(*page)) {
12995e6609a2SMatt Jacob 		ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1300f6ad3f23SAlexander Motin 		err = EIO;
1301f6ad3f23SAlexander Motin 		goto out;
1302f6ad3f23SAlexander Motin 	}
1303f6ad3f23SAlexander Motin 	page = (struct scsi_diag_page *)*bufp;
1304f6ad3f23SAlexander Motin 	length = scsi_2btoul(page->length);
1305f6ad3f23SAlexander Motin 	if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
13065e6609a2SMatt Jacob 		ENC_VLOG(enc, "Diag Pages List Too Long\n");
1307f6ad3f23SAlexander Motin 		goto out;
1308f6ad3f23SAlexander Motin 	}
1309f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1310f6ad3f23SAlexander Motin 		 __func__, length, xfer_len);
1311f6ad3f23SAlexander Motin 
1312f6ad3f23SAlexander Motin 	err = 0;
1313f6ad3f23SAlexander Motin 	for (i = 0; i < length; i++) {
131444525d12SAlexander Motin 		if (page->params[i] == SesElementDescriptor)
1315715c4a72SAlexander Motin 			ses->ses_flags |= SES_FLAG_DESC;
131644525d12SAlexander Motin 		else if (page->params[i] == SesAddlElementStatus)
1317f6ad3f23SAlexander Motin 			ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1318f6ad3f23SAlexander Motin 	}
1319f6ad3f23SAlexander Motin 
1320f6ad3f23SAlexander Motin out:
1321f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1322f6ad3f23SAlexander Motin 	return (err);
1323f6ad3f23SAlexander Motin }
1324f6ad3f23SAlexander Motin 
1325f6ad3f23SAlexander Motin /**
1326f6ad3f23SAlexander Motin  * \brief Process the config page and update associated structures.
1327f6ad3f23SAlexander Motin  *
1328f6ad3f23SAlexander Motin  * \param enc       SES device to query.
1329f6ad3f23SAlexander Motin  * \param buf       Buffer containing the config page.
1330f6ad3f23SAlexander Motin  * \param xfer_len  Length of the config page in the buffer.
1331f6ad3f23SAlexander Motin  *
1332f6ad3f23SAlexander Motin  * \return  0 on success, errno otherwise.
1333f6ad3f23SAlexander Motin  */
1334f6ad3f23SAlexander Motin static int
ses_process_config(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1335f6ad3f23SAlexander Motin ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1336f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1337f6ad3f23SAlexander Motin {
1338f6ad3f23SAlexander Motin 	struct ses_iterator iter;
1339f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
1340f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
1341f6ad3f23SAlexander Motin 	uint8_t *buf;
1342f6ad3f23SAlexander Motin 	int length;
1343f6ad3f23SAlexander Motin 	int err;
1344f6ad3f23SAlexander Motin 	int nelm;
1345f6ad3f23SAlexander Motin 	int ntype;
1346f6ad3f23SAlexander Motin 	struct ses_cfg_page *cfg_page;
1347f6ad3f23SAlexander Motin 	struct ses_enc_desc *buf_subenc;
1348f6ad3f23SAlexander Motin 	const struct ses_enc_desc **subencs;
1349f6ad3f23SAlexander Motin 	const struct ses_enc_desc **cur_subenc;
1350f6ad3f23SAlexander Motin 	const struct ses_enc_desc **last_subenc;
1351f6ad3f23SAlexander Motin 	ses_type_t *ses_types;
1352f6ad3f23SAlexander Motin 	ses_type_t *sestype;
1353f6ad3f23SAlexander Motin 	const struct ses_elm_type_desc *cur_buf_type;
1354f6ad3f23SAlexander Motin 	const struct ses_elm_type_desc *last_buf_type;
1355f6ad3f23SAlexander Motin 	uint8_t *last_valid_byte;
1356f6ad3f23SAlexander Motin 	enc_element_t *element;
1357f6ad3f23SAlexander Motin 	const char *type_text;
1358f6ad3f23SAlexander Motin 
1359f6ad3f23SAlexander Motin 	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1360f6ad3f23SAlexander Motin 	    ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1361f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
1362f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
1363f6ad3f23SAlexander Motin 	buf = *bufp;
1364db702c59SEitan Adler 	err = -1;
1365f6ad3f23SAlexander Motin 
1366f6ad3f23SAlexander Motin 	if (error != 0) {
1367f6ad3f23SAlexander Motin 		err = error;
1368f6ad3f23SAlexander Motin 		goto out;
1369f6ad3f23SAlexander Motin 	}
1370f6ad3f23SAlexander Motin 	if (xfer_len < sizeof(cfg_page->hdr)) {
13715e6609a2SMatt Jacob 		ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1372f6ad3f23SAlexander Motin 		err = EIO;
1373f6ad3f23SAlexander Motin 		goto out;
1374f6ad3f23SAlexander Motin 	}
1375f6ad3f23SAlexander Motin 
1376f6ad3f23SAlexander Motin 	cfg_page = (struct ses_cfg_page *)buf;
1377f6ad3f23SAlexander Motin 	length = ses_page_length(&cfg_page->hdr);
1378f6ad3f23SAlexander Motin 	if (length > xfer_len) {
13795e6609a2SMatt Jacob 		ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1380f6ad3f23SAlexander Motin 		goto out;
1381f6ad3f23SAlexander Motin 	}
1382f6ad3f23SAlexander Motin 	last_valid_byte = &buf[length - 1];
1383f6ad3f23SAlexander Motin 
1384f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1385f6ad3f23SAlexander Motin 		 __func__, length, xfer_len);
1386f6ad3f23SAlexander Motin 
1387f6ad3f23SAlexander Motin 	err = 0;
1388f6ad3f23SAlexander Motin 	if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1389f6ad3f23SAlexander Motin 		/* Our cache is still valid.  Proceed to fetching status. */
1390f6ad3f23SAlexander Motin 		goto out;
1391f6ad3f23SAlexander Motin 	}
1392f6ad3f23SAlexander Motin 
1393f6ad3f23SAlexander Motin 	/* Cache is no longer valid.  Free old data to make way for new. */
1394f6ad3f23SAlexander Motin 	ses_cache_free(enc, enc_cache);
1395f6ad3f23SAlexander Motin 	ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1396f6ad3f23SAlexander Motin 	    scsi_4btoul(cfg_page->hdr.gen_code),
1397f6ad3f23SAlexander Motin 	    ses_cfg_page_get_num_subenc(cfg_page));
1398f6ad3f23SAlexander Motin 
1399f6ad3f23SAlexander Motin 	/* Take ownership of the buffer. */
1400f6ad3f23SAlexander Motin 	ses_cache->cfg_page = cfg_page;
1401f6ad3f23SAlexander Motin 	*bufp = NULL;
1402f6ad3f23SAlexander Motin 
1403f6ad3f23SAlexander Motin 	/*
1404f6ad3f23SAlexander Motin 	 * Now waltz through all the subenclosures summing the number of
1405f6ad3f23SAlexander Motin 	 * types available in each.
1406f6ad3f23SAlexander Motin 	 */
1407ed246e82SAlexander Motin 	subencs = malloc(ses_cfg_page_get_num_subenc(cfg_page)
1408ed246e82SAlexander Motin 	    * sizeof(*subencs), M_SCSIENC, M_WAITOK|M_ZERO);
1409f6ad3f23SAlexander Motin 	/*
1410f6ad3f23SAlexander Motin 	 * Sub-enclosure data is const after construction (i.e. when
1411f6ad3f23SAlexander Motin 	 * accessed via our cache object.
1412f6ad3f23SAlexander Motin 	 *
1413f6ad3f23SAlexander Motin 	 * The cast here is not required in C++ but C99 is not so
1414f6ad3f23SAlexander Motin 	 * sophisticated (see C99 6.5.16.1(1)).
1415f6ad3f23SAlexander Motin 	 */
14166d2a1fbfSAlexander Motin 	ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1417f6ad3f23SAlexander Motin 	ses_cache->subencs = subencs;
1418f6ad3f23SAlexander Motin 
1419f6ad3f23SAlexander Motin 	buf_subenc = cfg_page->subencs;
1420f6ad3f23SAlexander Motin 	cur_subenc = subencs;
14216d2a1fbfSAlexander Motin 	last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1422f6ad3f23SAlexander Motin 	ntype = 0;
1423f6ad3f23SAlexander Motin 	while (cur_subenc <= last_subenc) {
1424f6ad3f23SAlexander Motin 		if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
14255e6609a2SMatt Jacob 			ENC_VLOG(enc, "Enclosure %d Beyond End of "
1426f6ad3f23SAlexander Motin 			    "Descriptors\n", cur_subenc - subencs);
1427f6ad3f23SAlexander Motin 			err = EIO;
1428f6ad3f23SAlexander Motin 			goto out;
1429f6ad3f23SAlexander Motin 		}
1430f6ad3f23SAlexander Motin 
1431f6ad3f23SAlexander Motin 		ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1432f6ad3f23SAlexander Motin 		    "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1433f6ad3f23SAlexander Motin 		    buf_subenc->num_types, buf_subenc->length,
1434f6ad3f23SAlexander Motin 		    &buf_subenc->byte0 - buf);
1435f6ad3f23SAlexander Motin 		ENC_VLOG(enc, "WWN: %jx\n",
1436f6ad3f23SAlexander Motin 		    (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1437f6ad3f23SAlexander Motin 
1438f6ad3f23SAlexander Motin 		ntype += buf_subenc->num_types;
1439f6ad3f23SAlexander Motin 		*cur_subenc = buf_subenc;
1440f6ad3f23SAlexander Motin 		cur_subenc++;
1441f6ad3f23SAlexander Motin 		buf_subenc = ses_enc_desc_next(buf_subenc);
1442f6ad3f23SAlexander Motin 	}
1443f6ad3f23SAlexander Motin 
1444f6ad3f23SAlexander Motin 	/* Process the type headers. */
1445ed246e82SAlexander Motin 	ses_types = malloc(ntype * sizeof(*ses_types),
1446ed246e82SAlexander Motin 	    M_SCSIENC, M_WAITOK|M_ZERO);
1447f6ad3f23SAlexander Motin 	/*
1448f6ad3f23SAlexander Motin 	 * Type data is const after construction (i.e. when accessed via
1449f6ad3f23SAlexander Motin 	 * our cache object.
1450f6ad3f23SAlexander Motin 	 */
14516d2a1fbfSAlexander Motin 	ses_cache->ses_ntypes = ntype;
1452f6ad3f23SAlexander Motin 	ses_cache->ses_types = ses_types;
1453f6ad3f23SAlexander Motin 
1454f6ad3f23SAlexander Motin 	cur_buf_type = (const struct ses_elm_type_desc *)
1455f6ad3f23SAlexander Motin 	    (&(*last_subenc)->length + (*last_subenc)->length + 1);
1456f6ad3f23SAlexander Motin 	last_buf_type = cur_buf_type + ntype - 1;
1457f6ad3f23SAlexander Motin 	type_text = (const uint8_t *)(last_buf_type + 1);
1458f6ad3f23SAlexander Motin 	nelm = 0;
1459f6ad3f23SAlexander Motin 	sestype = ses_types;
1460f6ad3f23SAlexander Motin 	while (cur_buf_type <= last_buf_type) {
1461f6ad3f23SAlexander Motin 		if (&cur_buf_type->etype_txt_len > last_valid_byte) {
14625e6609a2SMatt Jacob 			ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1463f6ad3f23SAlexander Motin 			    sestype - ses_types);
1464f6ad3f23SAlexander Motin 			err = EIO;
1465f6ad3f23SAlexander Motin 			goto out;
1466f6ad3f23SAlexander Motin 		}
1467f6ad3f23SAlexander Motin 		sestype->hdr  = cur_buf_type;
1468f6ad3f23SAlexander Motin 		sestype->text = type_text;
1469f6ad3f23SAlexander Motin 		type_text += cur_buf_type->etype_txt_len;
14705e6609a2SMatt Jacob 		ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1471f6ad3f23SAlexander Motin 		    "%d, Text Length %d: %.*s\n", sestype - ses_types,
1472f6ad3f23SAlexander Motin 		    sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1473f6ad3f23SAlexander Motin 		    sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1474f6ad3f23SAlexander Motin 		    sestype->hdr->etype_txt_len, sestype->text);
1475f6ad3f23SAlexander Motin 
1476f6ad3f23SAlexander Motin 		nelm += sestype->hdr->etype_maxelt
1477f6ad3f23SAlexander Motin 		      + /*overall status element*/1;
1478f6ad3f23SAlexander Motin 		sestype++;
1479f6ad3f23SAlexander Motin 		cur_buf_type++;
1480f6ad3f23SAlexander Motin 	}
1481f6ad3f23SAlexander Motin 
1482f6ad3f23SAlexander Motin 	/* Create the object map. */
1483ed246e82SAlexander Motin 	enc_cache->elm_map = malloc(nelm * sizeof(enc_element_t),
1484ed246e82SAlexander Motin 	    M_SCSIENC, M_WAITOK|M_ZERO);
1485f6ad3f23SAlexander Motin 	enc_cache->nelms = nelm;
1486f6ad3f23SAlexander Motin 
1487f6ad3f23SAlexander Motin 	ses_iter_init(enc, enc_cache, &iter);
1488f6ad3f23SAlexander Motin 	while ((element = ses_iter_next(&iter)) != NULL) {
1489f6ad3f23SAlexander Motin 		const struct ses_elm_type_desc *thdr;
1490f6ad3f23SAlexander Motin 
1491f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1492f6ad3f23SAlexander Motin 		    iter.global_element_index, iter.type_index, nelm,
1493f6ad3f23SAlexander Motin 		    iter.type_element_index);
1494f6ad3f23SAlexander Motin 		thdr = ses_cache->ses_types[iter.type_index].hdr;
149553f5ac13SAlexander Motin 		element->elm_idx = iter.global_element_index;
149653f5ac13SAlexander Motin 		element->elm_type = thdr->etype_elm_type;
1497f6ad3f23SAlexander Motin 		element->subenclosure = thdr->etype_subenc;
149853f5ac13SAlexander Motin 		element->type_elm_idx = iter.type_element_index;
1499ed246e82SAlexander Motin 		element->elm_private = malloc(sizeof(ses_element_t),
1500ed246e82SAlexander Motin 		    M_SCSIENC, M_WAITOK|M_ZERO);
1501f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1502f6ad3f23SAlexander Motin 		    "type 0x%x\n", __func__, iter.global_element_index,
1503f6ad3f23SAlexander Motin 		    iter.type_index, iter.type_element_index,
1504f6ad3f23SAlexander Motin 		    thdr->etype_subenc, thdr->etype_elm_type);
1505f6ad3f23SAlexander Motin 	}
1506f6ad3f23SAlexander Motin 
1507f6ad3f23SAlexander Motin 	err = 0;
1508f6ad3f23SAlexander Motin 
1509f6ad3f23SAlexander Motin out:
1510f6ad3f23SAlexander Motin 	if (err)
1511262b5c50SAlexander Motin 		ses_cache_free(enc, enc_cache);
1512f6ad3f23SAlexander Motin 	else {
15137027c01cSAlexander Motin 		ses_poll_status(enc);
1514f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_PUBLISH_CACHE);
1515f6ad3f23SAlexander Motin 	}
1516f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1517f6ad3f23SAlexander Motin 	return (err);
1518f6ad3f23SAlexander Motin }
1519f6ad3f23SAlexander Motin 
1520f6ad3f23SAlexander Motin /**
1521f6ad3f23SAlexander Motin  * \brief Update the status page and associated structures.
1522f6ad3f23SAlexander Motin  *
1523f6ad3f23SAlexander Motin  * \param enc   SES softc to update for.
1524f6ad3f23SAlexander Motin  * \param buf   Buffer containing the status page.
1525f6ad3f23SAlexander Motin  * \param bufsz	Amount of data in the buffer.
1526f6ad3f23SAlexander Motin  *
1527f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
1528f6ad3f23SAlexander Motin  */
1529f6ad3f23SAlexander Motin static int
ses_process_status(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1530f6ad3f23SAlexander Motin ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1531f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1532f6ad3f23SAlexander Motin {
1533f6ad3f23SAlexander Motin 	struct ses_iterator iter;
1534f6ad3f23SAlexander Motin 	enc_element_t *element;
1535f6ad3f23SAlexander Motin 	ses_softc_t *ses;
1536f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
1537f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
1538f6ad3f23SAlexander Motin 	uint8_t *buf;
1539f6ad3f23SAlexander Motin 	int err = -1;
1540f6ad3f23SAlexander Motin 	int length;
1541f6ad3f23SAlexander Motin 	struct ses_status_page *page;
1542f6ad3f23SAlexander Motin 	union ses_status_element *cur_stat;
1543f6ad3f23SAlexander Motin 	union ses_status_element *last_stat;
1544f6ad3f23SAlexander Motin 
1545f6ad3f23SAlexander Motin 	ses = enc->enc_private;
1546f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
1547f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
1548f6ad3f23SAlexander Motin 	buf = *bufp;
1549f6ad3f23SAlexander Motin 
1550f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1551f6ad3f23SAlexander Motin 	page = (struct ses_status_page *)buf;
1552f6ad3f23SAlexander Motin 	length = ses_page_length(&page->hdr);
1553f6ad3f23SAlexander Motin 
1554f6ad3f23SAlexander Motin 	if (error != 0) {
1555f6ad3f23SAlexander Motin 		err = error;
1556f6ad3f23SAlexander Motin 		goto out;
1557f6ad3f23SAlexander Motin 	}
1558f6ad3f23SAlexander Motin 	/*
1559f6ad3f23SAlexander Motin 	 * Make sure the length fits in the buffer.
1560f6ad3f23SAlexander Motin 	 *
1561f6ad3f23SAlexander Motin 	 * XXX all this means is that the page is larger than the space
1562f6ad3f23SAlexander Motin 	 * we allocated.  Since we use a statically sized buffer, this
1563f6ad3f23SAlexander Motin 	 * could happen... Need to use dynamic discovery of the size.
1564f6ad3f23SAlexander Motin 	 */
1565f6ad3f23SAlexander Motin 	if (length > xfer_len) {
15665e6609a2SMatt Jacob 		ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1567f6ad3f23SAlexander Motin 		goto out;
1568f6ad3f23SAlexander Motin 	}
1569f9004a5dSAlexander Motin 
1570f9004a5dSAlexander Motin 	/* Check for simple enclosure reporting short enclosure status. */
1571f9004a5dSAlexander Motin 	if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1572f9004a5dSAlexander Motin 		ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1573f9004a5dSAlexander Motin 		ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1574f9004a5dSAlexander Motin 		ses_cache_free(enc, enc_cache);
1575f9004a5dSAlexander Motin 		enc_cache->enc_status = page->hdr.page_specific_flags;
1576f9004a5dSAlexander Motin 		enc_update_request(enc, SES_PUBLISH_CACHE);
1577f9004a5dSAlexander Motin 		err = 0;
1578f9004a5dSAlexander Motin 		goto out;
1579f9004a5dSAlexander Motin 	}
1580f9004a5dSAlexander Motin 
1581f6ad3f23SAlexander Motin 	/* Make sure the length contains at least one header and status */
1582f6ad3f23SAlexander Motin 	if (length < (sizeof(*page) + sizeof(*page->elements))) {
15835e6609a2SMatt Jacob 		ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1584f6ad3f23SAlexander Motin 		goto out;
1585f6ad3f23SAlexander Motin 	}
1586f6ad3f23SAlexander Motin 
1587f6ad3f23SAlexander Motin 	if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1588f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: Generation count change detected\n",
1589f6ad3f23SAlexander Motin 		    __func__);
1590f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_UPDATE_GETCONFIG);
1591f6ad3f23SAlexander Motin 		goto out;
1592f6ad3f23SAlexander Motin 	}
1593f6ad3f23SAlexander Motin 
1594f6ad3f23SAlexander Motin 	ses_cache_free_status(enc, enc_cache);
1595f6ad3f23SAlexander Motin 	ses_cache->status_page = page;
1596f6ad3f23SAlexander Motin 	*bufp = NULL;
1597f6ad3f23SAlexander Motin 
1598f6ad3f23SAlexander Motin 	enc_cache->enc_status = page->hdr.page_specific_flags;
1599f6ad3f23SAlexander Motin 
1600f6ad3f23SAlexander Motin 	/*
1601f6ad3f23SAlexander Motin 	 * Read in individual element status.  The element order
1602f6ad3f23SAlexander Motin 	 * matches the order reported in the config page (i.e. the
1603f6ad3f23SAlexander Motin 	 * order of an unfiltered iteration of the config objects)..
1604f6ad3f23SAlexander Motin 	 */
1605f6ad3f23SAlexander Motin 	ses_iter_init(enc, enc_cache, &iter);
1606f6ad3f23SAlexander Motin 	cur_stat  = page->elements;
1607f6ad3f23SAlexander Motin 	last_stat = (union ses_status_element *)
1608f6ad3f23SAlexander Motin 	    &buf[length - sizeof(*last_stat)];
1609f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1610f6ad3f23SAlexander Motin 		__func__, length, xfer_len);
1611f6ad3f23SAlexander Motin 	while (cur_stat <= last_stat
1612f6ad3f23SAlexander Motin 	    && (element = ses_iter_next(&iter)) != NULL) {
1613f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1614f6ad3f23SAlexander Motin 		    __func__, iter.global_element_index, iter.type_index,
1615f6ad3f23SAlexander Motin 		    iter.type_element_index, (uint8_t *)cur_stat - buf,
1616f6ad3f23SAlexander Motin 		    scsi_4btoul(cur_stat->bytes));
1617f6ad3f23SAlexander Motin 
1618f6ad3f23SAlexander Motin 		memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1619f6ad3f23SAlexander Motin 		element->svalid = 1;
1620f6ad3f23SAlexander Motin 		cur_stat++;
1621f6ad3f23SAlexander Motin 	}
1622f6ad3f23SAlexander Motin 
1623f6ad3f23SAlexander Motin 	if (ses_iter_next(&iter) != NULL) {
16245e6609a2SMatt Jacob 		ENC_VLOG(enc, "Status page, length insufficient for "
1625f6ad3f23SAlexander Motin 			"expected number of objects\n");
1626f6ad3f23SAlexander Motin 	} else {
1627f6ad3f23SAlexander Motin 		if (cur_stat <= last_stat)
16285e6609a2SMatt Jacob 			ENC_VLOG(enc, "Status page, exhausted objects before "
1629f6ad3f23SAlexander Motin 				"exhausing page\n");
1630f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_PUBLISH_CACHE);
1631f6ad3f23SAlexander Motin 		err = 0;
1632f6ad3f23SAlexander Motin 	}
1633f6ad3f23SAlexander Motin out:
1634f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1635f6ad3f23SAlexander Motin 	return (err);
1636f6ad3f23SAlexander Motin }
1637f6ad3f23SAlexander Motin 
1638f6ad3f23SAlexander Motin typedef enum {
1639f6ad3f23SAlexander Motin 	/**
1640f6ad3f23SAlexander Motin 	 * The enclosure should not provide additional element
1641f6ad3f23SAlexander Motin 	 * status for this element type in page 0x0A.
1642f6ad3f23SAlexander Motin 	 *
1643f6ad3f23SAlexander Motin 	 * \note  This status is returned for any types not
1644f6ad3f23SAlexander Motin 	 *        listed SES3r02.  Further types added in a
1645f6ad3f23SAlexander Motin 	 *        future specification will be incorrectly
1646f6ad3f23SAlexander Motin 	 *        classified.
1647f6ad3f23SAlexander Motin 	 */
1648f6ad3f23SAlexander Motin 	TYPE_ADDLSTATUS_NONE,
1649f6ad3f23SAlexander Motin 
1650f6ad3f23SAlexander Motin 	/**
1651f6ad3f23SAlexander Motin 	 * The element type provides additional element status
1652f6ad3f23SAlexander Motin 	 * in page 0x0A.
1653f6ad3f23SAlexander Motin 	 */
1654f6ad3f23SAlexander Motin 	TYPE_ADDLSTATUS_MANDATORY,
1655f6ad3f23SAlexander Motin 
1656f6ad3f23SAlexander Motin 	/**
1657f6ad3f23SAlexander Motin 	 * The element type may provide additional element status
1658f6ad3f23SAlexander Motin 	 * in page 0x0A, but i
1659f6ad3f23SAlexander Motin 	 */
1660f6ad3f23SAlexander Motin 	TYPE_ADDLSTATUS_OPTIONAL
1661f6ad3f23SAlexander Motin } ses_addlstatus_avail_t;
1662f6ad3f23SAlexander Motin 
1663f6ad3f23SAlexander Motin /**
1664f6ad3f23SAlexander Motin  * \brief Check to see whether a given type (as obtained via type headers) is
1665f6ad3f23SAlexander Motin  *	  supported by the additional status command.
1666f6ad3f23SAlexander Motin  *
1667f6ad3f23SAlexander Motin  * \param enc     SES softc to check.
1668f6ad3f23SAlexander Motin  * \param typidx  Type index to check for.
1669f6ad3f23SAlexander Motin  *
1670f6ad3f23SAlexander Motin  * \return  An enumeration indicating if additional status is mandatory,
1671f6ad3f23SAlexander Motin  *          optional, or not required for this type.
1672f6ad3f23SAlexander Motin  */
1673f6ad3f23SAlexander Motin static ses_addlstatus_avail_t
ses_typehasaddlstatus(enc_softc_t * enc,uint8_t typidx)1674f6ad3f23SAlexander Motin ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1675f6ad3f23SAlexander Motin {
1676f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
1677f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
1678f6ad3f23SAlexander Motin 
1679f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
1680f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
1681f6ad3f23SAlexander Motin 	switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1682f6ad3f23SAlexander Motin 	case ELMTYP_DEVICE:
1683f6ad3f23SAlexander Motin 	case ELMTYP_ARRAY_DEV:
1684f6ad3f23SAlexander Motin 	case ELMTYP_SAS_EXP:
1685f6ad3f23SAlexander Motin 		return (TYPE_ADDLSTATUS_MANDATORY);
1686f6ad3f23SAlexander Motin 	case ELMTYP_SCSI_INI:
1687f6ad3f23SAlexander Motin 	case ELMTYP_SCSI_TGT:
1688f6ad3f23SAlexander Motin 	case ELMTYP_ESCC:
1689f6ad3f23SAlexander Motin 		return (TYPE_ADDLSTATUS_OPTIONAL);
1690f6ad3f23SAlexander Motin 	default:
1691f6ad3f23SAlexander Motin 		/* No additional status information available. */
1692f6ad3f23SAlexander Motin 		break;
1693f6ad3f23SAlexander Motin 	}
1694f6ad3f23SAlexander Motin 	return (TYPE_ADDLSTATUS_NONE);
1695f6ad3f23SAlexander Motin }
1696f6ad3f23SAlexander Motin 
1697f6ad3f23SAlexander Motin static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1698f6ad3f23SAlexander Motin 				     uint8_t *, int);
1699f6ad3f23SAlexander Motin static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1700f6ad3f23SAlexander Motin 				      int, int, int, int);
170153f5ac13SAlexander Motin static int ses_get_elm_addlstatus_ata(enc_softc_t *, enc_cache_t *, uint8_t *,
170253f5ac13SAlexander Motin 				      int, int, int, int);
1703f6ad3f23SAlexander Motin 
1704f6ad3f23SAlexander Motin /**
1705f6ad3f23SAlexander Motin  * \brief Parse the additional status element data for each object.
1706f6ad3f23SAlexander Motin  *
1707f6ad3f23SAlexander Motin  * \param enc       The SES softc to update.
1708f6ad3f23SAlexander Motin  * \param buf       The buffer containing the additional status
1709f6ad3f23SAlexander Motin  *                  element response.
1710f6ad3f23SAlexander Motin  * \param xfer_len  Size of the buffer.
1711f6ad3f23SAlexander Motin  *
1712f6ad3f23SAlexander Motin  * \return  0 on success, errno otherwise.
1713f6ad3f23SAlexander Motin  */
1714f6ad3f23SAlexander Motin static int
ses_process_elm_addlstatus(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1715f6ad3f23SAlexander Motin ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1716f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1717f6ad3f23SAlexander Motin {
1718f6ad3f23SAlexander Motin 	struct ses_iterator iter, titer;
1719f6ad3f23SAlexander Motin 	int eip;
1720f6ad3f23SAlexander Motin 	int err;
1721f6ad3f23SAlexander Motin 	int length;
1722f6ad3f23SAlexander Motin 	int offset;
1723f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
1724f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
1725f6ad3f23SAlexander Motin 	uint8_t *buf;
1726f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
1727f6ad3f23SAlexander Motin 	const struct ses_page_hdr *hdr;
1728f6ad3f23SAlexander Motin 	enc_element_t *element, *telement;
1729f6ad3f23SAlexander Motin 
1730f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
1731f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
1732f6ad3f23SAlexander Motin 	buf = *bufp;
1733f6ad3f23SAlexander Motin 	err = -1;
1734f6ad3f23SAlexander Motin 
1735f6ad3f23SAlexander Motin 	if (error != 0) {
1736f6ad3f23SAlexander Motin 		err = error;
1737f6ad3f23SAlexander Motin 		goto out;
1738f6ad3f23SAlexander Motin 	}
1739f6ad3f23SAlexander Motin 	ses_cache_free_elm_addlstatus(enc, enc_cache);
1740f6ad3f23SAlexander Motin 	ses_cache->elm_addlstatus_page =
1741f6ad3f23SAlexander Motin 	    (struct ses_addl_elem_status_page *)buf;
1742f6ad3f23SAlexander Motin 	*bufp = NULL;
1743f6ad3f23SAlexander Motin 
1744f6ad3f23SAlexander Motin 	/*
1745f6ad3f23SAlexander Motin 	 * The objects appear in the same order here as in Enclosure Status,
1746f6ad3f23SAlexander Motin 	 * which itself is ordered by the Type Descriptors from the Config
1747f6ad3f23SAlexander Motin 	 * page.  However, it is necessary to skip elements that are not
1748f6ad3f23SAlexander Motin 	 * supported by this page when counting them.
1749f6ad3f23SAlexander Motin 	 */
1750f6ad3f23SAlexander Motin 	hdr = &ses_cache->elm_addlstatus_page->hdr;
1751f6ad3f23SAlexander Motin 	length = ses_page_length(hdr);
1752f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1753f6ad3f23SAlexander Motin 	/* Make sure the length includes at least one header. */
1754f6ad3f23SAlexander Motin 	if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
17555e6609a2SMatt Jacob 		ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1756f6ad3f23SAlexander Motin 		goto out;
1757f6ad3f23SAlexander Motin 	}
1758f6ad3f23SAlexander Motin 	if (length > xfer_len) {
17595e6609a2SMatt Jacob 		ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1760f6ad3f23SAlexander Motin 		goto out;
1761f6ad3f23SAlexander Motin 	}
1762f6ad3f23SAlexander Motin 
1763f6ad3f23SAlexander Motin 	if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1764f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: Generation count change detected\n",
1765f6ad3f23SAlexander Motin 		    __func__);
1766f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_UPDATE_GETCONFIG);
1767f6ad3f23SAlexander Motin 		goto out;
1768f6ad3f23SAlexander Motin 	}
1769f6ad3f23SAlexander Motin 
1770f6ad3f23SAlexander Motin 	offset = sizeof(struct ses_page_hdr);
1771f6ad3f23SAlexander Motin 	ses_iter_init(enc, enc_cache, &iter);
1772f6ad3f23SAlexander Motin 	while (offset < length
1773f6ad3f23SAlexander Motin 	    && (element = ses_iter_next(&iter)) != NULL) {
1774f6ad3f23SAlexander Motin 		struct ses_elm_addlstatus_base_hdr *elm_hdr;
1775f6ad3f23SAlexander Motin 		int proto_info_len;
1776f6ad3f23SAlexander Motin 		ses_addlstatus_avail_t status_type;
1777f6ad3f23SAlexander Motin 
1778f6ad3f23SAlexander Motin 		/*
1779f6ad3f23SAlexander Motin 		 * Additional element status is only provided for
1780f6ad3f23SAlexander Motin 		 * individual elements (i.e. overal status elements
1781f6ad3f23SAlexander Motin 		 * are excluded) and those of the types specified
1782f6ad3f23SAlexander Motin 		 * in the SES spec.
1783f6ad3f23SAlexander Motin 		 */
1784f6ad3f23SAlexander Motin 		status_type = ses_typehasaddlstatus(enc, iter.type_index);
1785f6ad3f23SAlexander Motin 		if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1786f6ad3f23SAlexander Motin 		 || status_type == TYPE_ADDLSTATUS_NONE)
1787f6ad3f23SAlexander Motin 			continue;
1788f6ad3f23SAlexander Motin 
1789f6ad3f23SAlexander Motin 		elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1790f6ad3f23SAlexander Motin 		eip = ses_elm_addlstatus_eip(elm_hdr);
17916805c9b7SAlexander Motin 		if (eip) {
1792f6ad3f23SAlexander Motin 			struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1793bceff6e3SAlexander Motin 			int expected_index, index;
1794bceff6e3SAlexander Motin 			ses_elem_index_type_t index_type;
1795f6ad3f23SAlexander Motin 
1796f6ad3f23SAlexander Motin 			eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1797c119ccdbSAlexander Motin 			if (SES_ADDL_EIP_EIIOE_EI_GLOB(eip_hdr->byte2)) {
1798bceff6e3SAlexander Motin 				index_type = SES_ELEM_INDEX_GLOBAL;
1799bceff6e3SAlexander Motin 				expected_index = iter.global_element_index;
1800bceff6e3SAlexander Motin 			} else {
1801bceff6e3SAlexander Motin 				index_type = SES_ELEM_INDEX_INDIVIDUAL;
1802f6ad3f23SAlexander Motin 				expected_index = iter.individual_element_index;
1803bceff6e3SAlexander Motin 			}
18046805c9b7SAlexander Motin 			if (eip_hdr->element_index < expected_index) {
18056805c9b7SAlexander Motin 				ENC_VLOG(enc, "%s: provided %selement index "
18066805c9b7SAlexander Motin 				    "%d is lower then expected %d\n",
1807c119ccdbSAlexander Motin 				    __func__, SES_ADDL_EIP_EIIOE_EI_GLOB(
1808c119ccdbSAlexander Motin 				    eip_hdr->byte2) ? "global " : "",
18096805c9b7SAlexander Motin 				    eip_hdr->element_index, expected_index);
18106805c9b7SAlexander Motin 				goto badindex;
18116805c9b7SAlexander Motin 			}
1812f6ad3f23SAlexander Motin 			titer = iter;
1813f6ad3f23SAlexander Motin 			telement = ses_iter_seek_to(&titer,
1814bceff6e3SAlexander Motin 			    eip_hdr->element_index, index_type);
18156805c9b7SAlexander Motin 			if (telement == NULL) {
18166805c9b7SAlexander Motin 				ENC_VLOG(enc, "%s: provided %selement index "
18176805c9b7SAlexander Motin 				    "%d does not exist\n", __func__,
1818c119ccdbSAlexander Motin 				    SES_ADDL_EIP_EIIOE_EI_GLOB(eip_hdr->byte2) ?
18196805c9b7SAlexander Motin 				    "global " : "", eip_hdr->element_index);
18206805c9b7SAlexander Motin 				goto badindex;
18216805c9b7SAlexander Motin 			}
18226805c9b7SAlexander Motin 			if (ses_typehasaddlstatus(enc, titer.type_index) ==
18236805c9b7SAlexander Motin 			    TYPE_ADDLSTATUS_NONE) {
18246805c9b7SAlexander Motin 				ENC_VLOG(enc, "%s: provided %selement index "
18256805c9b7SAlexander Motin 				    "%d can't have additional status\n",
18266805c9b7SAlexander Motin 				    __func__,
1827c119ccdbSAlexander Motin 				    SES_ADDL_EIP_EIIOE_EI_GLOB(eip_hdr->byte2) ?
18286805c9b7SAlexander Motin 				    "global " : "", eip_hdr->element_index);
18296805c9b7SAlexander Motin badindex:
18306805c9b7SAlexander Motin 				/*
18316805c9b7SAlexander Motin 				 * If we expected mandatory element, we may
18326805c9b7SAlexander Motin 				 * guess it was just a wrong index and we may
18336805c9b7SAlexander Motin 				 * use the status.  If element was optional,
18346805c9b7SAlexander Motin 				 * then we have no idea where status belongs.
18356805c9b7SAlexander Motin 				 */
18366805c9b7SAlexander Motin 				if (status_type == TYPE_ADDLSTATUS_OPTIONAL)
18376805c9b7SAlexander Motin 					break;
18386805c9b7SAlexander Motin 			} else {
1839f6ad3f23SAlexander Motin 				iter = titer;
1840f6ad3f23SAlexander Motin 				element = telement;
18416805c9b7SAlexander Motin 			}
1842f6ad3f23SAlexander Motin 
1843c119ccdbSAlexander Motin 			if (SES_ADDL_EIP_EIIOE_EI_GLOB(eip_hdr->byte2))
1844bceff6e3SAlexander Motin 				index = iter.global_element_index;
1845bceff6e3SAlexander Motin 			else
1846bceff6e3SAlexander Motin 				index = iter.individual_element_index;
1847bceff6e3SAlexander Motin 			if (index > expected_index
1848f6ad3f23SAlexander Motin 			 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1849bceff6e3SAlexander Motin 				ENC_VLOG(enc, "%s: provided %s element"
1850f6ad3f23SAlexander Motin 					"index %d skips mandatory status "
1851f6ad3f23SAlexander Motin 					" element at index %d\n",
1852c119ccdbSAlexander Motin 					__func__, SES_ADDL_EIP_EIIOE_EI_GLOB(
1853c119ccdbSAlexander Motin 					eip_hdr->byte2) ? "global " : "",
1854bceff6e3SAlexander Motin 					index, expected_index);
1855f6ad3f23SAlexander Motin 			}
1856f6ad3f23SAlexander Motin 		}
1857f6ad3f23SAlexander Motin 		elmpriv = element->elm_private;
1858f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1859f6ad3f23SAlexander Motin 		    "type element index=%d, offset=0x%x, "
1860f6ad3f23SAlexander Motin 		    "byte0=0x%x, length=0x%x\n", __func__,
1861f6ad3f23SAlexander Motin 		    iter.global_element_index, iter.type_index,
18626805c9b7SAlexander Motin 		    iter.type_element_index, offset, elm_hdr->byte0,
18636805c9b7SAlexander Motin 		    elm_hdr->length);
1864f6ad3f23SAlexander Motin 
1865f6ad3f23SAlexander Motin 		/* Skip to after the length field */
1866f6ad3f23SAlexander Motin 		offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1867f6ad3f23SAlexander Motin 
1868f6ad3f23SAlexander Motin 		/* Make sure the descriptor is within bounds */
18696805c9b7SAlexander Motin 		if ((offset + elm_hdr->length) > length) {
18705e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d Beyond End "
1871f6ad3f23SAlexander Motin 			    "of Additional Element Status Descriptors\n",
1872f6ad3f23SAlexander Motin 			    iter.global_element_index);
1873b2e6b2f8SAlexander Motin 			break;
1874f6ad3f23SAlexander Motin 		}
1875f6ad3f23SAlexander Motin 
18766805c9b7SAlexander Motin 		/* Skip elements marked as invalid. */
18776805c9b7SAlexander Motin 		if (ses_elm_addlstatus_invalid(elm_hdr)) {
18786805c9b7SAlexander Motin 			offset += elm_hdr->length;
18796805c9b7SAlexander Motin 			continue;
18806805c9b7SAlexander Motin 		}
188153f5ac13SAlexander Motin 		elmpriv->addl.hdr = elm_hdr;
18826805c9b7SAlexander Motin 
1883f6ad3f23SAlexander Motin 		/* Advance to the protocol data, skipping eip bytes if needed */
1884f6ad3f23SAlexander Motin 		offset += (eip * SES_EIP_HDR_EXTRA_LEN);
18856805c9b7SAlexander Motin 		proto_info_len = elm_hdr->length
1886f6ad3f23SAlexander Motin 			       - (eip * SES_EIP_HDR_EXTRA_LEN);
1887f6ad3f23SAlexander Motin 
1888f6ad3f23SAlexander Motin 		/* Errors in this block are ignored as they are non-fatal */
18896805c9b7SAlexander Motin 		switch(ses_elm_addlstatus_proto(elm_hdr)) {
1890f6ad3f23SAlexander Motin 		case SPSP_PROTO_FC:
18916805c9b7SAlexander Motin 			if (elm_hdr->length == 0)
1892f6ad3f23SAlexander Motin 				break;
1893f6ad3f23SAlexander Motin 			ses_get_elm_addlstatus_fc(enc, enc_cache,
1894f6ad3f23SAlexander Motin 						  &buf[offset], proto_info_len);
1895f6ad3f23SAlexander Motin 			break;
1896f6ad3f23SAlexander Motin 		case SPSP_PROTO_SAS:
18976805c9b7SAlexander Motin 			if (elm_hdr->length <= 2)
1898f6ad3f23SAlexander Motin 				break;
1899f6ad3f23SAlexander Motin 			ses_get_elm_addlstatus_sas(enc, enc_cache,
1900f6ad3f23SAlexander Motin 						   &buf[offset],
1901f6ad3f23SAlexander Motin 						   proto_info_len,
1902f6ad3f23SAlexander Motin 						   eip, iter.type_index,
1903f6ad3f23SAlexander Motin 						   iter.global_element_index);
1904f6ad3f23SAlexander Motin 			break;
190553f5ac13SAlexander Motin 		case SPSP_PROTO_ATA:
190653f5ac13SAlexander Motin 			ses_get_elm_addlstatus_ata(enc, enc_cache,
190753f5ac13SAlexander Motin 						   &buf[offset],
190853f5ac13SAlexander Motin 						   proto_info_len,
190953f5ac13SAlexander Motin 						   eip, iter.type_index,
191053f5ac13SAlexander Motin 						   iter.global_element_index);
191153f5ac13SAlexander Motin 			break;
1912f6ad3f23SAlexander Motin 		default:
19135e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1914f6ad3f23SAlexander Motin 			    "Protocol 0x%x\n", iter.global_element_index,
19156805c9b7SAlexander Motin 			    ses_elm_addlstatus_proto(elm_hdr));
1916b2e6b2f8SAlexander Motin 			break;
1917f6ad3f23SAlexander Motin 		}
1918f6ad3f23SAlexander Motin 
1919f6ad3f23SAlexander Motin 		offset += proto_info_len;
1920f6ad3f23SAlexander Motin 	}
1921f6ad3f23SAlexander Motin 	err = 0;
1922f6ad3f23SAlexander Motin out:
1923f6ad3f23SAlexander Motin 	if (err)
1924f6ad3f23SAlexander Motin 		ses_cache_free_elm_addlstatus(enc, enc_cache);
1925f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1926f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PUBLISH_CACHE);
1927f6ad3f23SAlexander Motin 	return (err);
1928f6ad3f23SAlexander Motin }
1929f6ad3f23SAlexander Motin 
1930f6ad3f23SAlexander Motin static int
ses_process_control_request(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1931f6ad3f23SAlexander Motin ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1932f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1933f6ad3f23SAlexander Motin {
1934f6ad3f23SAlexander Motin 	ses_softc_t *ses;
1935f6ad3f23SAlexander Motin 
1936f6ad3f23SAlexander Motin 	ses = enc->enc_private;
1937f6ad3f23SAlexander Motin 	/*
1938f6ad3f23SAlexander Motin 	 * Possible errors:
1939f6ad3f23SAlexander Motin 	 *  o Generation count wrong.
1940f6ad3f23SAlexander Motin 	 *  o Some SCSI status error.
1941f6ad3f23SAlexander Motin 	 */
1942f6ad3f23SAlexander Motin 	ses_terminate_control_requests(&ses->ses_pending_requests, error);
19437027c01cSAlexander Motin 	ses_poll_status(enc);
1944f6ad3f23SAlexander Motin 	return (0);
1945f6ad3f23SAlexander Motin }
1946f6ad3f23SAlexander Motin 
1947f6ad3f23SAlexander Motin static int
ses_publish_physpaths(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1948f6ad3f23SAlexander Motin ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1949f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1950f6ad3f23SAlexander Motin {
1951f6ad3f23SAlexander Motin 	struct ses_iterator iter;
1952f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
1953f6ad3f23SAlexander Motin 	enc_element_t *element;
1954f6ad3f23SAlexander Motin 
1955f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
1956f6ad3f23SAlexander Motin 
1957f6ad3f23SAlexander Motin 	ses_iter_init(enc, enc_cache, &iter);
1958f6ad3f23SAlexander Motin 	while ((element = ses_iter_next(&iter)) != NULL) {
1959f6ad3f23SAlexander Motin 		/*
1960f6ad3f23SAlexander Motin 		 * ses_set_physpath() returns success if we changed
1961f6ad3f23SAlexander Motin 		 * the physpath of any element.  This allows us to
1962f6ad3f23SAlexander Motin 		 * only announce devices once regardless of how
1963f6ad3f23SAlexander Motin 		 * many times we process additional element status.
1964f6ad3f23SAlexander Motin 		 */
1965f6ad3f23SAlexander Motin 		if (ses_set_physpath(enc, element, &iter) == 0)
1966f6ad3f23SAlexander Motin 			ses_print_addl_data(enc, element);
1967f6ad3f23SAlexander Motin 	}
1968f6ad3f23SAlexander Motin 
1969f6ad3f23SAlexander Motin 	return (0);
1970f6ad3f23SAlexander Motin }
1971f6ad3f23SAlexander Motin 
1972f6ad3f23SAlexander Motin static int
ses_publish_cache(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)1973f6ad3f23SAlexander Motin ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1974f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1975f6ad3f23SAlexander Motin {
1976f6ad3f23SAlexander Motin 
1977f6ad3f23SAlexander Motin 	sx_xlock(&enc->enc_cache_lock);
1978f6ad3f23SAlexander Motin 	ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1979f6ad3f23SAlexander Motin 			/*dst*/&enc->enc_cache);
1980f6ad3f23SAlexander Motin 	sx_xunlock(&enc->enc_cache_lock);
1981f6ad3f23SAlexander Motin 
1982f6ad3f23SAlexander Motin 	return (0);
1983f6ad3f23SAlexander Motin }
1984f6ad3f23SAlexander Motin 
1985e083fb08SAlan Somers /*
1986e083fb08SAlan Somers  * \brief Sanitize an element descriptor
1987e083fb08SAlan Somers  *
1988e083fb08SAlan Somers  * The SES4r3 standard, sections 3.1.2 and 6.1.10, specifies that element
1989e083fb08SAlan Somers  * descriptors may only contain ASCII characters in the range 0x20 to 0x7e.
1990e083fb08SAlan Somers  * But some vendors violate that rule.  Ensure that we only expose compliant
1991e083fb08SAlan Somers  * descriptors to userland.
1992e083fb08SAlan Somers  *
1993e083fb08SAlan Somers  * \param desc		SES element descriptor as reported by the hardware
1994e083fb08SAlan Somers  * \param len		Length of desc in bytes, not necessarily including
1995e083fb08SAlan Somers  * 			trailing NUL.  It will be modified if desc is invalid.
1996e083fb08SAlan Somers  */
1997e083fb08SAlan Somers static const char*
ses_sanitize_elm_desc(const char * desc,uint16_t * len)1998e083fb08SAlan Somers ses_sanitize_elm_desc(const char *desc, uint16_t *len)
1999e083fb08SAlan Somers {
2000e083fb08SAlan Somers 	const char *invalid = "<invalid>";
2001e083fb08SAlan Somers 	int i;
2002e083fb08SAlan Somers 
2003e083fb08SAlan Somers 	for (i = 0; i < *len; i++) {
200412fec4c3SAlexander Motin 		if (desc[i] == 0) {
200512fec4c3SAlexander Motin 			break;
200612fec4c3SAlexander Motin 		} else if (desc[i] < 0x20 || desc[i] > 0x7e) {
2007e083fb08SAlan Somers 			*len = strlen(invalid);
2008e083fb08SAlan Somers 			return (invalid);
2009e083fb08SAlan Somers 		}
2010e083fb08SAlan Somers 	}
2011e083fb08SAlan Somers 	return (desc);
2012e083fb08SAlan Somers }
2013e083fb08SAlan Somers 
2014f6ad3f23SAlexander Motin /**
2015f6ad3f23SAlexander Motin  * \brief Parse the descriptors for each object.
2016f6ad3f23SAlexander Motin  *
2017f6ad3f23SAlexander Motin  * \param enc       The SES softc to update.
2018f6ad3f23SAlexander Motin  * \param buf       The buffer containing the descriptor list response.
2019f6ad3f23SAlexander Motin  * \param xfer_len  Size of the buffer.
2020f6ad3f23SAlexander Motin  *
2021f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
2022f6ad3f23SAlexander Motin  */
2023f6ad3f23SAlexander Motin static int
ses_process_elm_descs(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t ** bufp,int error,int xfer_len)2024f6ad3f23SAlexander Motin ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
2025f6ad3f23SAlexander Motin     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
2026f6ad3f23SAlexander Motin {
2027f6ad3f23SAlexander Motin 	ses_softc_t *ses;
2028f6ad3f23SAlexander Motin 	struct ses_iterator iter;
2029f6ad3f23SAlexander Motin 	enc_element_t *element;
2030f6ad3f23SAlexander Motin 	int err;
2031f6ad3f23SAlexander Motin 	int offset;
2032f6ad3f23SAlexander Motin 	u_long length, plength;
2033f6ad3f23SAlexander Motin 	enc_cache_t *enc_cache;
2034f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
2035f6ad3f23SAlexander Motin 	uint8_t *buf;
2036f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2037f6ad3f23SAlexander Motin 	const struct ses_page_hdr *phdr;
2038f6ad3f23SAlexander Motin 	const struct ses_elm_desc_hdr *hdr;
2039f6ad3f23SAlexander Motin 
2040f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2041f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
2042f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
2043f6ad3f23SAlexander Motin 	buf = *bufp;
2044f6ad3f23SAlexander Motin 	err = -1;
2045f6ad3f23SAlexander Motin 
2046f6ad3f23SAlexander Motin 	if (error != 0) {
2047f6ad3f23SAlexander Motin 		err = error;
2048f6ad3f23SAlexander Motin 		goto out;
2049f6ad3f23SAlexander Motin 	}
2050f6ad3f23SAlexander Motin 	ses_cache_free_elm_descs(enc, enc_cache);
2051f6ad3f23SAlexander Motin 	ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
2052f6ad3f23SAlexander Motin 	*bufp = NULL;
2053f6ad3f23SAlexander Motin 
2054f6ad3f23SAlexander Motin 	phdr = &ses_cache->elm_descs_page->hdr;
2055f6ad3f23SAlexander Motin 	plength = ses_page_length(phdr);
2056f6ad3f23SAlexander Motin 	if (xfer_len < sizeof(struct ses_page_hdr)) {
20575e6609a2SMatt Jacob 		ENC_VLOG(enc, "Runt Element Descriptor Page\n");
2058f6ad3f23SAlexander Motin 		goto out;
2059f6ad3f23SAlexander Motin 	}
2060f6ad3f23SAlexander Motin 	if (plength > xfer_len) {
20615e6609a2SMatt Jacob 		ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
2062f6ad3f23SAlexander Motin 		goto out;
2063f6ad3f23SAlexander Motin 	}
2064f6ad3f23SAlexander Motin 
2065f6ad3f23SAlexander Motin 	if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
2066f6ad3f23SAlexander Motin 		ENC_VLOG(enc, "%s: Generation count change detected\n",
2067f6ad3f23SAlexander Motin 		    __func__);
2068f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_UPDATE_GETCONFIG);
2069f6ad3f23SAlexander Motin 		goto out;
2070f6ad3f23SAlexander Motin 	}
2071f6ad3f23SAlexander Motin 
2072f6ad3f23SAlexander Motin 	offset = sizeof(struct ses_page_hdr);
2073f6ad3f23SAlexander Motin 
2074f6ad3f23SAlexander Motin 	ses_iter_init(enc, enc_cache, &iter);
2075f6ad3f23SAlexander Motin 	while (offset < plength
2076f6ad3f23SAlexander Motin 	    && (element = ses_iter_next(&iter)) != NULL) {
2077f6ad3f23SAlexander Motin 		if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
20785e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d Descriptor Header Past "
2079f6ad3f23SAlexander Motin 			    "End of Buffer\n", iter.global_element_index);
2080f6ad3f23SAlexander Motin 			goto out;
2081f6ad3f23SAlexander Motin 		}
2082f6ad3f23SAlexander Motin 		hdr = (struct ses_elm_desc_hdr *)&buf[offset];
2083f6ad3f23SAlexander Motin 		length = scsi_2btoul(hdr->length);
2084f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
2085f6ad3f23SAlexander Motin 		    iter.global_element_index, iter.type_index,
2086f6ad3f23SAlexander Motin 		    iter.type_element_index, length, offset);
2087f6ad3f23SAlexander Motin 		if ((offset + sizeof(*hdr) + length) > plength) {
20885e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element%d Descriptor Past "
2089f6ad3f23SAlexander Motin 			    "End of Buffer\n", iter.global_element_index);
2090f6ad3f23SAlexander Motin 			goto out;
2091f6ad3f23SAlexander Motin 		}
2092f6ad3f23SAlexander Motin 		offset += sizeof(*hdr);
2093f6ad3f23SAlexander Motin 
2094f6ad3f23SAlexander Motin 		if (length > 0) {
2095f6ad3f23SAlexander Motin 			elmpriv = element->elm_private;
2096f6ad3f23SAlexander Motin 			elmpriv->descr_len = length;
2097e083fb08SAlan Somers 			elmpriv->descr = ses_sanitize_elm_desc(&buf[offset],
2098e083fb08SAlan Somers 			    &elmpriv->descr_len);
2099f6ad3f23SAlexander Motin 		}
2100f6ad3f23SAlexander Motin 
2101f6ad3f23SAlexander Motin 		/* skip over the descriptor itself */
2102f6ad3f23SAlexander Motin 		offset += length;
2103f6ad3f23SAlexander Motin 	}
2104f6ad3f23SAlexander Motin 
2105f6ad3f23SAlexander Motin 	err = 0;
2106f6ad3f23SAlexander Motin out:
2107f6ad3f23SAlexander Motin 	if (err == 0) {
2108f6ad3f23SAlexander Motin 		if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2109f6ad3f23SAlexander Motin 			enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2110f6ad3f23SAlexander Motin 	}
2111f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PUBLISH_CACHE);
2112f6ad3f23SAlexander Motin 	return (err);
2113f6ad3f23SAlexander Motin }
2114f6ad3f23SAlexander Motin 
2115f6ad3f23SAlexander Motin static int
ses_fill_rcv_diag_io(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t * buf)2116f6ad3f23SAlexander Motin ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2117f6ad3f23SAlexander Motin 		       union ccb *ccb, uint8_t *buf)
2118f6ad3f23SAlexander Motin {
2119f6ad3f23SAlexander Motin 
2120f6ad3f23SAlexander Motin 	if (enc->enc_type == ENC_SEMB_SES) {
2121f6ad3f23SAlexander Motin 		semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2122227d67aaSAlexander Motin 					NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2123f6ad3f23SAlexander Motin 					state->page_code, buf, state->buf_size,
2124f6ad3f23SAlexander Motin 					state->timeout);
2125f6ad3f23SAlexander Motin 	} else {
2126f6ad3f23SAlexander Motin 		scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2127227d67aaSAlexander Motin 					NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2128f6ad3f23SAlexander Motin 					state->page_code, buf, state->buf_size,
2129f6ad3f23SAlexander Motin 					SSD_FULL_SIZE, state->timeout);
2130f6ad3f23SAlexander Motin 	}
2131f6ad3f23SAlexander Motin 	return (0);
2132f6ad3f23SAlexander Motin }
2133f6ad3f23SAlexander Motin 
2134f6ad3f23SAlexander Motin /**
2135f6ad3f23SAlexander Motin  * \brief Encode the object status into the response buffer, which is
2136f6ad3f23SAlexander Motin  *	  expected to contain the current enclosure status.  This function
2137f6ad3f23SAlexander Motin  *	  turns off all the 'select' bits for the objects except for the
2138f6ad3f23SAlexander Motin  *	  object specified, then sends it back to the enclosure.
2139f6ad3f23SAlexander Motin  *
2140f6ad3f23SAlexander Motin  * \param enc	SES enclosure the change is being applied to.
2141f6ad3f23SAlexander Motin  * \param buf	Buffer containing the current enclosure status response.
2142f6ad3f23SAlexander Motin  * \param amt	Length of the response in the buffer.
2143f6ad3f23SAlexander Motin  * \param req	The control request to be applied to buf.
2144f6ad3f23SAlexander Motin  *
2145f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
2146f6ad3f23SAlexander Motin  */
2147f6ad3f23SAlexander Motin static int
ses_encode(enc_softc_t * enc,uint8_t * buf,int amt,ses_control_request_t * req)2148f6ad3f23SAlexander Motin ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2149f6ad3f23SAlexander Motin {
2150f6ad3f23SAlexander Motin 	struct ses_iterator iter;
2151f6ad3f23SAlexander Motin 	enc_element_t *element;
2152f6ad3f23SAlexander Motin 	int offset;
2153f6ad3f23SAlexander Motin 	struct ses_control_page_hdr *hdr;
2154f6ad3f23SAlexander Motin 
2155f6ad3f23SAlexander Motin 	ses_iter_init(enc, &enc->enc_cache, &iter);
2156f6ad3f23SAlexander Motin 	hdr = (struct ses_control_page_hdr *)buf;
2157f6ad3f23SAlexander Motin 	if (req->elm_idx == -1) {
2158f6ad3f23SAlexander Motin 		/* for enclosure status, at least 2 bytes are needed */
2159f6ad3f23SAlexander Motin 		if (amt < 2)
2160f6ad3f23SAlexander Motin 			return EIO;
2161f6ad3f23SAlexander Motin 		hdr->control_flags =
2162f6ad3f23SAlexander Motin 		    req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2163f6ad3f23SAlexander Motin 		ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2164f6ad3f23SAlexander Motin 		return (0);
2165f6ad3f23SAlexander Motin 	}
2166f6ad3f23SAlexander Motin 
2167f6ad3f23SAlexander Motin 	element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2168f6ad3f23SAlexander Motin 	if (element == NULL)
2169f6ad3f23SAlexander Motin 		return (ENXIO);
2170f6ad3f23SAlexander Motin 
2171f6ad3f23SAlexander Motin 	/*
2172f6ad3f23SAlexander Motin 	 * Seek to the type set that corresponds to the requested object.
2173f6ad3f23SAlexander Motin 	 * The +1 is for the overall status element for the type.
2174f6ad3f23SAlexander Motin 	 */
2175f6ad3f23SAlexander Motin 	offset = sizeof(struct ses_control_page_hdr)
2176f6ad3f23SAlexander Motin 	       + (iter.global_element_index * sizeof(struct ses_comstat));
2177f6ad3f23SAlexander Motin 
2178f6ad3f23SAlexander Motin 	/* Check for buffer overflow. */
2179f6ad3f23SAlexander Motin 	if (offset + sizeof(struct ses_comstat) > amt)
2180f6ad3f23SAlexander Motin 		return (EIO);
2181f6ad3f23SAlexander Motin 
2182f6ad3f23SAlexander Motin 	/* Set the status. */
2183f6ad3f23SAlexander Motin 	memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2184f6ad3f23SAlexander Motin 
2185f6ad3f23SAlexander Motin 	ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2186f6ad3f23SAlexander Motin 	    iter.type_index, iter.global_element_index, offset,
2187f6ad3f23SAlexander Motin 	    req->elm_stat.comstatus, req->elm_stat.comstat[0],
2188f6ad3f23SAlexander Motin 	    req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2189f6ad3f23SAlexander Motin 
2190f6ad3f23SAlexander Motin 	return (0);
2191f6ad3f23SAlexander Motin }
2192f6ad3f23SAlexander Motin 
2193f6ad3f23SAlexander Motin static int
ses_fill_control_request(enc_softc_t * enc,struct enc_fsm_state * state,union ccb * ccb,uint8_t * buf)2194f6ad3f23SAlexander Motin ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2195f6ad3f23SAlexander Motin 			 union ccb *ccb, uint8_t *buf)
2196f6ad3f23SAlexander Motin {
2197f6ad3f23SAlexander Motin 	ses_softc_t			*ses;
2198f6ad3f23SAlexander Motin 	enc_cache_t			*enc_cache;
2199f6ad3f23SAlexander Motin 	ses_cache_t			*ses_cache;
2200f6ad3f23SAlexander Motin 	struct ses_control_page_hdr	*hdr;
2201f6ad3f23SAlexander Motin 	ses_control_request_t		*req;
2202f6ad3f23SAlexander Motin 	size_t				 plength;
2203f6ad3f23SAlexander Motin 	size_t				 offset;
2204f6ad3f23SAlexander Motin 
2205f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2206f6ad3f23SAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
2207f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
2208f6ad3f23SAlexander Motin 	hdr = (struct ses_control_page_hdr *)buf;
2209f6ad3f23SAlexander Motin 
2210f6ad3f23SAlexander Motin 	if (ses_cache->status_page == NULL) {
2211f6ad3f23SAlexander Motin 		ses_terminate_control_requests(&ses->ses_requests, EIO);
2212f6ad3f23SAlexander Motin 		return (EIO);
2213f6ad3f23SAlexander Motin 	}
2214f6ad3f23SAlexander Motin 
2215f6ad3f23SAlexander Motin 	plength = ses_page_length(&ses_cache->status_page->hdr);
2216f6ad3f23SAlexander Motin 	memcpy(buf, ses_cache->status_page, plength);
2217f6ad3f23SAlexander Motin 
2218f6ad3f23SAlexander Motin 	/* Disable the select bits in all status entries.  */
2219f6ad3f23SAlexander Motin 	offset = sizeof(struct ses_control_page_hdr);
2220f6ad3f23SAlexander Motin 	for (offset = sizeof(struct ses_control_page_hdr);
2221f6ad3f23SAlexander Motin 	     offset < plength; offset += sizeof(struct ses_comstat)) {
2222f6ad3f23SAlexander Motin 		buf[offset] &= ~SESCTL_CSEL;
2223f6ad3f23SAlexander Motin 	}
2224f6ad3f23SAlexander Motin 
2225f6ad3f23SAlexander Motin 	/* And make sure the INVOP bit is clear.  */
2226f6ad3f23SAlexander Motin 	hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2227f6ad3f23SAlexander Motin 
2228f6ad3f23SAlexander Motin 	/* Apply incoming requests. */
2229f6ad3f23SAlexander Motin 	while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2230f6ad3f23SAlexander Motin 		TAILQ_REMOVE(&ses->ses_requests, req, links);
2231f6ad3f23SAlexander Motin 		req->result = ses_encode(enc, buf, plength, req);
2232f6ad3f23SAlexander Motin 		if (req->result != 0) {
2233f6ad3f23SAlexander Motin 			wakeup(req);
2234f6ad3f23SAlexander Motin 			continue;
2235f6ad3f23SAlexander Motin 		}
2236f6ad3f23SAlexander Motin 		TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2237f6ad3f23SAlexander Motin 	}
2238f6ad3f23SAlexander Motin 
2239f6ad3f23SAlexander Motin 	if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2240f6ad3f23SAlexander Motin 		return (ENOENT);
2241f6ad3f23SAlexander Motin 
2242f6ad3f23SAlexander Motin 	/* Fill out the ccb */
2243f6ad3f23SAlexander Motin 	if (enc->enc_type == ENC_SEMB_SES) {
2244227d67aaSAlexander Motin 		semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2245f6ad3f23SAlexander Motin 			     MSG_SIMPLE_Q_TAG,
2246f6ad3f23SAlexander Motin 			     buf, ses_page_length(&ses_cache->status_page->hdr),
2247f6ad3f23SAlexander Motin 			     state->timeout);
2248f6ad3f23SAlexander Motin 	} else {
2249227d67aaSAlexander Motin 		scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2250f6ad3f23SAlexander Motin 			     MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2251f6ad3f23SAlexander Motin 			     /*device_offline*/0, /*self_test*/0,
2252f6ad3f23SAlexander Motin 			     /*page_format*/1, /*self_test_code*/0,
2253f6ad3f23SAlexander Motin 			     buf, ses_page_length(&ses_cache->status_page->hdr),
2254f6ad3f23SAlexander Motin 			     SSD_FULL_SIZE, state->timeout);
2255f6ad3f23SAlexander Motin 	}
2256f6ad3f23SAlexander Motin 	return (0);
2257f6ad3f23SAlexander Motin }
2258f6ad3f23SAlexander Motin 
2259f6ad3f23SAlexander Motin static int
ses_get_elm_addlstatus_fc(enc_softc_t * enc,enc_cache_t * enc_cache,uint8_t * buf,int bufsiz)2260f6ad3f23SAlexander Motin ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2261f6ad3f23SAlexander Motin 			  uint8_t *buf, int bufsiz)
2262f6ad3f23SAlexander Motin {
22635e6609a2SMatt Jacob 	ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2264f6ad3f23SAlexander Motin 	return (ENODEV);
2265f6ad3f23SAlexander Motin }
2266f6ad3f23SAlexander Motin 
2267f6ad3f23SAlexander Motin #define	SES_PRINT_PORTS(p, type) do {					\
226853f5ac13SAlexander Motin 	if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) != 0) {		\
2269f6ad3f23SAlexander Motin 		sbuf_printf(sbp, " %s (", type);			\
2270f6ad3f23SAlexander Motin 		if ((p) & SES_SASOBJ_DEV_PHY_SMP)			\
2271519b24f0SAlexander Motin 			sbuf_cat(sbp, " SMP");				\
2272f6ad3f23SAlexander Motin 		if ((p) & SES_SASOBJ_DEV_PHY_STP)			\
2273519b24f0SAlexander Motin 			sbuf_cat(sbp, " STP");				\
2274f6ad3f23SAlexander Motin 		if ((p) & SES_SASOBJ_DEV_PHY_SSP)			\
2275519b24f0SAlexander Motin 			sbuf_cat(sbp, " SSP");				\
2276519b24f0SAlexander Motin 		sbuf_cat(sbp, " )");					\
227753f5ac13SAlexander Motin 	}								\
2278f6ad3f23SAlexander Motin } while(0)
2279f6ad3f23SAlexander Motin 
2280f6ad3f23SAlexander Motin /**
2281f6ad3f23SAlexander Motin  * \brief Print the additional element status data for this object, for SAS
2282f6ad3f23SAlexander Motin  * 	  type 0 objects.  See SES2 r20 Section 6.1.13.3.2.
2283f6ad3f23SAlexander Motin  *
2284f6ad3f23SAlexander Motin  * \param sesname	SES device name associated with the object.
2285f6ad3f23SAlexander Motin  * \param sbp		Sbuf to print to.
2286f6ad3f23SAlexander Motin  * \param obj		The object to print the data for.
2287f6ad3f23SAlexander Motin  */
2288f6ad3f23SAlexander Motin static void
ses_print_addl_data_sas_type0(char * sesname,struct sbuf * sbp,enc_element_t * obj)2289f6ad3f23SAlexander Motin ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
229053f5ac13SAlexander Motin 			      enc_element_t *obj)
2291f6ad3f23SAlexander Motin {
2292f6ad3f23SAlexander Motin 	int i;
2293f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2294f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
2295f6ad3f23SAlexander Motin 	struct ses_elm_sas_device_phy *phy;
2296f6ad3f23SAlexander Motin 
2297f6ad3f23SAlexander Motin 	elmpriv = obj->elm_private;
2298f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
229953f5ac13SAlexander Motin 	sbuf_printf(sbp, ", SAS Slot: %d%s phys",
230053f5ac13SAlexander Motin 	    addl->proto_hdr.sas->base_hdr.num_phys,
230153f5ac13SAlexander Motin 	    ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas) ? "+" : "");
2302f6ad3f23SAlexander Motin 	if (ses_elm_addlstatus_eip(addl->hdr))
230353f5ac13SAlexander Motin 		sbuf_printf(sbp, " at slot %d",
2304f6ad3f23SAlexander Motin 		    addl->proto_hdr.sas->type0_eip.dev_slot_num);
2305519b24f0SAlexander Motin 	sbuf_putc(sbp, '\n');
2306f6ad3f23SAlexander Motin 	if (addl->proto_data.sasdev_phys == NULL)
2307f6ad3f23SAlexander Motin 		return;
2308f6ad3f23SAlexander Motin 	for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2309f6ad3f23SAlexander Motin 		phy = &addl->proto_data.sasdev_phys[i];
2310f6ad3f23SAlexander Motin 		sbuf_printf(sbp, "%s:  phy %d:", sesname, i);
2311f6ad3f23SAlexander Motin 		if (ses_elm_sas_dev_phy_sata_dev(phy))
2312f6ad3f23SAlexander Motin 			/* Spec says all other fields are specific values */
2313519b24f0SAlexander Motin 			sbuf_cat(sbp, " SATA device\n");
2314f6ad3f23SAlexander Motin 		else {
231553f5ac13SAlexander Motin 			sbuf_printf(sbp, " SAS device type %d phy %d",
2316f6ad3f23SAlexander Motin 			    ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2317f6ad3f23SAlexander Motin 			SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2318f6ad3f23SAlexander Motin 			SES_PRINT_PORTS(phy->target_ports, "Target");
2319519b24f0SAlexander Motin 			sbuf_putc(sbp, '\n');
2320f6ad3f23SAlexander Motin 		}
2321f6ad3f23SAlexander Motin 		sbuf_printf(sbp, "%s:  phy %d: parent %jx addr %jx\n",
2322f6ad3f23SAlexander Motin 		    sesname, i,
2323f6ad3f23SAlexander Motin 		    (uintmax_t)scsi_8btou64(phy->parent_addr),
2324f6ad3f23SAlexander Motin 		    (uintmax_t)scsi_8btou64(phy->phy_addr));
2325f6ad3f23SAlexander Motin 	}
2326f6ad3f23SAlexander Motin }
2327f6ad3f23SAlexander Motin #undef SES_PRINT_PORTS
2328f6ad3f23SAlexander Motin 
2329f6ad3f23SAlexander Motin /**
2330f6ad3f23SAlexander Motin  * \brief Print the additional element status data for this object, for SAS
2331f6ad3f23SAlexander Motin  *	  type 1 objects.  See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2332f6ad3f23SAlexander Motin  *
2333f6ad3f23SAlexander Motin  * \param sesname	SES device name associated with the object.
2334f6ad3f23SAlexander Motin  * \param sbp		Sbuf to print to.
2335f6ad3f23SAlexander Motin  * \param obj		The object to print the data for.
2336f6ad3f23SAlexander Motin  */
2337f6ad3f23SAlexander Motin static void
ses_print_addl_data_sas_type1(char * sesname,struct sbuf * sbp,enc_element_t * obj)233853f5ac13SAlexander Motin ses_print_addl_data_sas_type1(char *sesname, struct sbuf *sbp,
233953f5ac13SAlexander Motin 			      enc_element_t *obj)
2340f6ad3f23SAlexander Motin {
2341f6ad3f23SAlexander Motin 	int i, num_phys;
2342f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2343f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
2344f6ad3f23SAlexander Motin 	struct ses_elm_sas_expander_phy *exp_phy;
2345f6ad3f23SAlexander Motin 	struct ses_elm_sas_port_phy *port_phy;
2346f6ad3f23SAlexander Motin 
2347f6ad3f23SAlexander Motin 	elmpriv = obj->elm_private;
2348f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
2349519b24f0SAlexander Motin 	sbuf_cat(sbp, ", SAS ");
235053f5ac13SAlexander Motin 	if (obj->elm_type == ELMTYP_SAS_EXP) {
2351f6ad3f23SAlexander Motin 		num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
235253f5ac13SAlexander Motin 		sbuf_printf(sbp, "Expander: %d phys", num_phys);
2353f6ad3f23SAlexander Motin 		if (addl->proto_data.sasexp_phys == NULL)
2354f6ad3f23SAlexander Motin 			return;
2355f6ad3f23SAlexander Motin 		for (i = 0;i < num_phys;i++) {
2356f6ad3f23SAlexander Motin 			exp_phy = &addl->proto_data.sasexp_phys[i];
2357f6ad3f23SAlexander Motin 			sbuf_printf(sbp, "%s:  phy %d: connector %d other %d\n",
2358f6ad3f23SAlexander Motin 			    sesname, i, exp_phy->connector_index,
2359f6ad3f23SAlexander Motin 			    exp_phy->other_index);
2360f6ad3f23SAlexander Motin 		}
2361f6ad3f23SAlexander Motin 	} else {
2362f6ad3f23SAlexander Motin 		num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
236353f5ac13SAlexander Motin 		sbuf_printf(sbp, "Port: %d phys", num_phys);
2364f6ad3f23SAlexander Motin 		if (addl->proto_data.sasport_phys == NULL)
2365f6ad3f23SAlexander Motin 			return;
2366f6ad3f23SAlexander Motin 		for (i = 0;i < num_phys;i++) {
2367f6ad3f23SAlexander Motin 			port_phy = &addl->proto_data.sasport_phys[i];
2368f6ad3f23SAlexander Motin 			sbuf_printf(sbp,
2369f6ad3f23SAlexander Motin 			    "%s:  phy %d: id %d connector %d other %d\n",
2370f6ad3f23SAlexander Motin 			    sesname, i, port_phy->phy_id,
2371f6ad3f23SAlexander Motin 			    port_phy->connector_index, port_phy->other_index);
2372f6ad3f23SAlexander Motin 			sbuf_printf(sbp, "%s:  phy %d: addr %jx\n", sesname, i,
2373f6ad3f23SAlexander Motin 			    (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2374f6ad3f23SAlexander Motin 		}
2375f6ad3f23SAlexander Motin 	}
2376f6ad3f23SAlexander Motin }
2377f6ad3f23SAlexander Motin 
2378f6ad3f23SAlexander Motin /**
237953f5ac13SAlexander Motin  * \brief Print the additional element status data for this object, for
238053f5ac13SAlexander Motin  *	  ATA objects.
238153f5ac13SAlexander Motin  *
238253f5ac13SAlexander Motin  * \param sbp		Sbuf to print to.
238353f5ac13SAlexander Motin  * \param obj		The object to print the data for.
238453f5ac13SAlexander Motin  */
238553f5ac13SAlexander Motin static void
ses_print_addl_data_ata(struct sbuf * sbp,enc_element_t * obj)238653f5ac13SAlexander Motin ses_print_addl_data_ata(struct sbuf *sbp, enc_element_t *obj)
238753f5ac13SAlexander Motin {
238853f5ac13SAlexander Motin 	ses_element_t *elmpriv = obj->elm_private;
238953f5ac13SAlexander Motin 	struct ses_addl_status *addl = &elmpriv->addl;
239053f5ac13SAlexander Motin 	struct ses_elm_ata_hdr *ata = addl->proto_hdr.ata;
239153f5ac13SAlexander Motin 
239253f5ac13SAlexander Motin 	sbuf_printf(sbp, ", SATA Slot: scbus%d target %d\n",
239353f5ac13SAlexander Motin 	    scsi_4btoul(ata->bus), scsi_4btoul(ata->target));
239453f5ac13SAlexander Motin }
239553f5ac13SAlexander Motin 
239653f5ac13SAlexander Motin /**
2397f6ad3f23SAlexander Motin  * \brief Print the additional element status data for this object.
2398f6ad3f23SAlexander Motin  *
2399f6ad3f23SAlexander Motin  * \param enc		SES softc associated with the object.
2400f6ad3f23SAlexander Motin  * \param obj		The object to print the data for.
2401f6ad3f23SAlexander Motin  */
2402f6ad3f23SAlexander Motin static void
ses_print_addl_data(enc_softc_t * enc,enc_element_t * obj)2403f6ad3f23SAlexander Motin ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2404f6ad3f23SAlexander Motin {
2405f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2406f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
2407f6ad3f23SAlexander Motin 	struct sbuf sesname, name, out;
2408f6ad3f23SAlexander Motin 
2409f6ad3f23SAlexander Motin 	elmpriv = obj->elm_private;
2410f6ad3f23SAlexander Motin 	if (elmpriv == NULL)
2411f6ad3f23SAlexander Motin 		return;
2412f6ad3f23SAlexander Motin 
2413f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
2414f6ad3f23SAlexander Motin 	if (addl->hdr == NULL)
2415f6ad3f23SAlexander Motin 		return;
2416f6ad3f23SAlexander Motin 
2417f6ad3f23SAlexander Motin 	sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2418f6ad3f23SAlexander Motin 	sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2419f6ad3f23SAlexander Motin 	sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2420f6ad3f23SAlexander Motin 	ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2421f6ad3f23SAlexander Motin 	if (sbuf_len(&name) == 0)
2422519b24f0SAlexander Motin 		sbuf_cat(&name, "(none)");
2423f6ad3f23SAlexander Motin 	sbuf_finish(&name);
2424f6ad3f23SAlexander Motin 	sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2425f6ad3f23SAlexander Motin 	    enc->periph->unit_number);
2426f6ad3f23SAlexander Motin 	sbuf_finish(&sesname);
242753f5ac13SAlexander Motin 	sbuf_printf(&out, "%s: %s in ", sbuf_data(&sesname), sbuf_data(&name));
2428f6ad3f23SAlexander Motin 	if (elmpriv->descr != NULL)
242953f5ac13SAlexander Motin 		sbuf_printf(&out, "'%s'", elmpriv->descr);
243053f5ac13SAlexander Motin 	else {
243153f5ac13SAlexander Motin 		if (obj->elm_type <= ELMTYP_LAST)
243253f5ac13SAlexander Motin 			sbuf_cat(&out, elm_type_names[obj->elm_type]);
243353f5ac13SAlexander Motin 		else
243453f5ac13SAlexander Motin 			sbuf_printf(&out, "<Type 0x%02x>", obj->elm_type);
243553f5ac13SAlexander Motin 		sbuf_printf(&out, " %d", obj->type_elm_idx);
243653f5ac13SAlexander Motin 		if (obj->subenclosure != 0)
243753f5ac13SAlexander Motin 			sbuf_printf(&out, " of subenc %d", obj->subenclosure);
243853f5ac13SAlexander Motin 	}
2439f6ad3f23SAlexander Motin 	switch(ses_elm_addlstatus_proto(addl->hdr)) {
244053f5ac13SAlexander Motin 	case SPSP_PROTO_FC:
244153f5ac13SAlexander Motin 		goto noaddl;	/* stubbed for now */
2442f6ad3f23SAlexander Motin 	case SPSP_PROTO_SAS:
244353f5ac13SAlexander Motin 		if (addl->proto_hdr.sas == NULL)
244453f5ac13SAlexander Motin 			goto noaddl;
2445f6ad3f23SAlexander Motin 		switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2446f6ad3f23SAlexander Motin 		case SES_SASOBJ_TYPE_SLOT:
2447f6ad3f23SAlexander Motin 			ses_print_addl_data_sas_type0(sbuf_data(&sesname),
244853f5ac13SAlexander Motin 			    &out, obj);
2449f6ad3f23SAlexander Motin 			break;
2450f6ad3f23SAlexander Motin 		case SES_SASOBJ_TYPE_OTHER:
245153f5ac13SAlexander Motin 			ses_print_addl_data_sas_type1(sbuf_data(&sesname),
245253f5ac13SAlexander Motin 			    &out, obj);
2453f6ad3f23SAlexander Motin 			break;
2454f6ad3f23SAlexander Motin 		default:
245553f5ac13SAlexander Motin 			goto noaddl;
2456f6ad3f23SAlexander Motin 		}
2457f6ad3f23SAlexander Motin 		break;
245853f5ac13SAlexander Motin 	case SPSP_PROTO_ATA:
245953f5ac13SAlexander Motin 		if (addl->proto_hdr.ata == NULL)
246053f5ac13SAlexander Motin 			goto noaddl;
246153f5ac13SAlexander Motin 		ses_print_addl_data_ata(&out, obj);
2462f6ad3f23SAlexander Motin 		break;
2463f6ad3f23SAlexander Motin 	default:
246453f5ac13SAlexander Motin noaddl:
246553f5ac13SAlexander Motin 		sbuf_cat(&out, "\n");
2466f6ad3f23SAlexander Motin 		break;
2467f6ad3f23SAlexander Motin 	}
2468f6ad3f23SAlexander Motin 	sbuf_finish(&out);
2469f6ad3f23SAlexander Motin 	printf("%s", sbuf_data(&out));
2470f6ad3f23SAlexander Motin 	sbuf_delete(&out);
2471f6ad3f23SAlexander Motin 	sbuf_delete(&name);
2472f6ad3f23SAlexander Motin 	sbuf_delete(&sesname);
2473f6ad3f23SAlexander Motin }
2474f6ad3f23SAlexander Motin 
2475f6ad3f23SAlexander Motin /**
2476f6ad3f23SAlexander Motin  * \brief Update the softc with the additional element status data for this
2477f6ad3f23SAlexander Motin  * 	  object, for SAS type 0 objects.
2478f6ad3f23SAlexander Motin  *
2479f6ad3f23SAlexander Motin  * \param enc		SES softc to be updated.
2480f6ad3f23SAlexander Motin  * \param buf		The additional element status response buffer.
2481f6ad3f23SAlexander Motin  * \param bufsiz	Size of the response buffer.
2482f6ad3f23SAlexander Motin  * \param eip		The EIP bit value.
2483f6ad3f23SAlexander Motin  * \param nobj		Number of objects attached to the SES softc.
2484f6ad3f23SAlexander Motin  *
2485f6ad3f23SAlexander Motin  * \return		0 on success, errno otherwise.
2486f6ad3f23SAlexander Motin  */
2487f6ad3f23SAlexander Motin static int
ses_get_elm_addlstatus_sas_type0(enc_softc_t * enc,enc_cache_t * enc_cache,uint8_t * buf,int bufsiz,int eip,int nobj)2488f6ad3f23SAlexander Motin ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2489f6ad3f23SAlexander Motin 				 uint8_t *buf, int bufsiz, int eip, int nobj)
2490f6ad3f23SAlexander Motin {
2491f6ad3f23SAlexander Motin 	int err, offset, physz;
2492f6ad3f23SAlexander Motin 	enc_element_t *obj;
2493f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2494f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
2495f6ad3f23SAlexander Motin 
2496f6ad3f23SAlexander Motin 	err = offset = 0;
2497f6ad3f23SAlexander Motin 
2498f6ad3f23SAlexander Motin 	/* basic object setup */
2499f6ad3f23SAlexander Motin 	obj = &(enc_cache->elm_map[nobj]);
2500f6ad3f23SAlexander Motin 	elmpriv = obj->elm_private;
2501f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
2502f6ad3f23SAlexander Motin 
2503f6ad3f23SAlexander Motin 	addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2504f6ad3f23SAlexander Motin 
2505f6ad3f23SAlexander Motin 	/* Don't assume this object has any phys */
2506f6ad3f23SAlexander Motin 	bzero(&addl->proto_data, sizeof(addl->proto_data));
2507f6ad3f23SAlexander Motin 	if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2508f6ad3f23SAlexander Motin 		goto out;
2509f6ad3f23SAlexander Motin 
2510f6ad3f23SAlexander Motin 	/* Skip forward to the phy list */
2511f6ad3f23SAlexander Motin 	if (eip)
2512f6ad3f23SAlexander Motin 		offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2513f6ad3f23SAlexander Motin 	else
2514f6ad3f23SAlexander Motin 		offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2515f6ad3f23SAlexander Motin 
2516f6ad3f23SAlexander Motin 	/* Make sure the phy list fits in the buffer */
2517f6ad3f23SAlexander Motin 	physz = addl->proto_hdr.sas->base_hdr.num_phys;
2518f6ad3f23SAlexander Motin 	physz *= sizeof(struct ses_elm_sas_device_phy);
2519f6ad3f23SAlexander Motin 	if (physz > (bufsiz - offset + 4)) {
25205e6609a2SMatt Jacob 		ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2521f6ad3f23SAlexander Motin 		    nobj);
2522f6ad3f23SAlexander Motin 		err = EIO;
2523f6ad3f23SAlexander Motin 		goto out;
2524f6ad3f23SAlexander Motin 	}
2525f6ad3f23SAlexander Motin 
2526f6ad3f23SAlexander Motin 	/* Point to the phy list */
2527f6ad3f23SAlexander Motin 	addl->proto_data.sasdev_phys =
2528f6ad3f23SAlexander Motin 	    (struct ses_elm_sas_device_phy *)&buf[offset];
2529f6ad3f23SAlexander Motin 
2530f6ad3f23SAlexander Motin out:
2531f6ad3f23SAlexander Motin 	return (err);
2532f6ad3f23SAlexander Motin }
2533f6ad3f23SAlexander Motin 
2534f6ad3f23SAlexander Motin /**
2535f6ad3f23SAlexander Motin  * \brief Update the softc with the additional element status data for this
2536f6ad3f23SAlexander Motin  * 	  object, for SAS type 1 objects.
2537f6ad3f23SAlexander Motin  *
2538f6ad3f23SAlexander Motin  * \param enc		SES softc to be updated.
2539f6ad3f23SAlexander Motin  * \param buf		The additional element status response buffer.
2540f6ad3f23SAlexander Motin  * \param bufsiz	Size of the response buffer.
2541f6ad3f23SAlexander Motin  * \param eip		The EIP bit value.
2542f6ad3f23SAlexander Motin  * \param nobj		Number of objects attached to the SES softc.
2543f6ad3f23SAlexander Motin  *
2544f6ad3f23SAlexander Motin  * \return		0 on success, errno otherwise.
2545f6ad3f23SAlexander Motin  */
2546f6ad3f23SAlexander Motin static int
ses_get_elm_addlstatus_sas_type1(enc_softc_t * enc,enc_cache_t * enc_cache,uint8_t * buf,int bufsiz,int eip,int nobj)2547f6ad3f23SAlexander Motin ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2548f6ad3f23SAlexander Motin 			         uint8_t *buf, int bufsiz, int eip, int nobj)
2549f6ad3f23SAlexander Motin {
2550f6ad3f23SAlexander Motin 	int err, offset, physz;
2551f6ad3f23SAlexander Motin 	enc_element_t *obj;
2552f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2553f6ad3f23SAlexander Motin 	struct ses_addl_status *addl;
2554f6ad3f23SAlexander Motin 
2555f6ad3f23SAlexander Motin 	err = offset = 0;
2556f6ad3f23SAlexander Motin 
2557f6ad3f23SAlexander Motin 	/* basic object setup */
2558f6ad3f23SAlexander Motin 	obj = &(enc_cache->elm_map[nobj]);
2559f6ad3f23SAlexander Motin 	elmpriv = obj->elm_private;
2560f6ad3f23SAlexander Motin 	addl = &(elmpriv->addl);
2561f6ad3f23SAlexander Motin 
2562f6ad3f23SAlexander Motin 	addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2563f6ad3f23SAlexander Motin 
2564f6ad3f23SAlexander Motin 	/* Don't assume this object has any phys */
2565f6ad3f23SAlexander Motin 	bzero(&addl->proto_data, sizeof(addl->proto_data));
2566f6ad3f23SAlexander Motin 	if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2567f6ad3f23SAlexander Motin 		goto out;
2568f6ad3f23SAlexander Motin 
2569f6ad3f23SAlexander Motin 	/* Process expanders differently from other type1 cases */
257053f5ac13SAlexander Motin 	if (obj->elm_type == ELMTYP_SAS_EXP) {
2571f6ad3f23SAlexander Motin 		offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2572f6ad3f23SAlexander Motin 		physz = addl->proto_hdr.sas->base_hdr.num_phys *
2573f6ad3f23SAlexander Motin 		    sizeof(struct ses_elm_sas_expander_phy);
2574f6ad3f23SAlexander Motin 		if (physz > (bufsiz - offset)) {
25755e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2576f6ad3f23SAlexander Motin 			    "End Of Buffer\n", nobj);
2577f6ad3f23SAlexander Motin 			err = EIO;
2578f6ad3f23SAlexander Motin 			goto out;
2579f6ad3f23SAlexander Motin 		}
2580f6ad3f23SAlexander Motin 		addl->proto_data.sasexp_phys =
2581f6ad3f23SAlexander Motin 		    (struct ses_elm_sas_expander_phy *)&buf[offset];
2582f6ad3f23SAlexander Motin 	} else {
2583f6ad3f23SAlexander Motin 		offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2584f6ad3f23SAlexander Motin 		physz = addl->proto_hdr.sas->base_hdr.num_phys *
2585f6ad3f23SAlexander Motin 		    sizeof(struct ses_elm_sas_port_phy);
2586f6ad3f23SAlexander Motin 		if (physz > (bufsiz - offset + 4)) {
25875e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2588f6ad3f23SAlexander Motin 			    "Of Buffer\n", nobj);
2589f6ad3f23SAlexander Motin 			err = EIO;
2590f6ad3f23SAlexander Motin 			goto out;
2591f6ad3f23SAlexander Motin 		}
2592f6ad3f23SAlexander Motin 		addl->proto_data.sasport_phys =
2593f6ad3f23SAlexander Motin 		    (struct ses_elm_sas_port_phy *)&buf[offset];
2594f6ad3f23SAlexander Motin 	}
2595f6ad3f23SAlexander Motin 
2596f6ad3f23SAlexander Motin out:
2597f6ad3f23SAlexander Motin 	return (err);
2598f6ad3f23SAlexander Motin }
2599f6ad3f23SAlexander Motin 
2600f6ad3f23SAlexander Motin /**
2601f6ad3f23SAlexander Motin  * \brief Update the softc with the additional element status data for this
2602f6ad3f23SAlexander Motin  * 	  object, for SAS objects.
2603f6ad3f23SAlexander Motin  *
2604f6ad3f23SAlexander Motin  * \param enc		SES softc to be updated.
2605f6ad3f23SAlexander Motin  * \param buf		The additional element status response buffer.
2606f6ad3f23SAlexander Motin  * \param bufsiz	Size of the response buffer.
2607f6ad3f23SAlexander Motin  * \param eip		The EIP bit value.
2608f6ad3f23SAlexander Motin  * \param tidx		Type index for this object.
2609f6ad3f23SAlexander Motin  * \param nobj		Number of objects attached to the SES softc.
2610f6ad3f23SAlexander Motin  *
2611f6ad3f23SAlexander Motin  * \return		0 on success, errno otherwise.
2612f6ad3f23SAlexander Motin  */
2613f6ad3f23SAlexander Motin static int
ses_get_elm_addlstatus_sas(enc_softc_t * enc,enc_cache_t * enc_cache,uint8_t * buf,int bufsiz,int eip,int tidx,int nobj)2614f6ad3f23SAlexander Motin ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2615f6ad3f23SAlexander Motin 			   uint8_t *buf, int bufsiz, int eip, int tidx,
2616f6ad3f23SAlexander Motin 			   int nobj)
2617f6ad3f23SAlexander Motin {
2618f6ad3f23SAlexander Motin 	int dtype, err;
2619f6ad3f23SAlexander Motin 	ses_cache_t *ses_cache;
2620f6ad3f23SAlexander Motin 	union ses_elm_sas_hdr *hdr;
2621f6ad3f23SAlexander Motin 
2622f6ad3f23SAlexander Motin 	/* Need to be able to read the descriptor type! */
2623f6ad3f23SAlexander Motin 	if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2624f6ad3f23SAlexander Motin 		err = EIO;
2625f6ad3f23SAlexander Motin 		goto out;
2626f6ad3f23SAlexander Motin 	}
2627f6ad3f23SAlexander Motin 
2628f6ad3f23SAlexander Motin 	ses_cache = enc_cache->private;
2629f6ad3f23SAlexander Motin 
2630f6ad3f23SAlexander Motin 	hdr = (union ses_elm_sas_hdr *)buf;
2631f6ad3f23SAlexander Motin 	dtype = ses_elm_sas_descr_type(hdr);
2632f6ad3f23SAlexander Motin 	switch(dtype) {
2633f6ad3f23SAlexander Motin 	case SES_SASOBJ_TYPE_SLOT:
2634f6ad3f23SAlexander Motin 		switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2635f6ad3f23SAlexander Motin 		case ELMTYP_DEVICE:
2636f6ad3f23SAlexander Motin 		case ELMTYP_ARRAY_DEV:
2637f6ad3f23SAlexander Motin 			break;
2638f6ad3f23SAlexander Motin 		default:
26395e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2640f6ad3f23SAlexander Motin 			    "invalid for SES element type 0x%x\n", nobj,
2641f6ad3f23SAlexander Motin 			    ses_cache->ses_types[tidx].hdr->etype_elm_type);
2642f6ad3f23SAlexander Motin 			err = ENODEV;
2643f6ad3f23SAlexander Motin 			goto out;
2644f6ad3f23SAlexander Motin 		}
2645f6ad3f23SAlexander Motin 		err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2646f6ad3f23SAlexander Motin 						       buf, bufsiz, eip,
2647f6ad3f23SAlexander Motin 		    nobj);
2648f6ad3f23SAlexander Motin 		break;
2649f6ad3f23SAlexander Motin 	case SES_SASOBJ_TYPE_OTHER:
2650f6ad3f23SAlexander Motin 		switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2651f6ad3f23SAlexander Motin 		case ELMTYP_SAS_EXP:
2652f6ad3f23SAlexander Motin 		case ELMTYP_SCSI_INI:
2653f6ad3f23SAlexander Motin 		case ELMTYP_SCSI_TGT:
2654f6ad3f23SAlexander Motin 		case ELMTYP_ESCC:
2655f6ad3f23SAlexander Motin 			break;
2656f6ad3f23SAlexander Motin 		default:
26575e6609a2SMatt Jacob 			ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2658f6ad3f23SAlexander Motin 			    "invalid for SES element type 0x%x\n", nobj,
2659f6ad3f23SAlexander Motin 			    ses_cache->ses_types[tidx].hdr->etype_elm_type);
2660f6ad3f23SAlexander Motin 			err = ENODEV;
2661f6ad3f23SAlexander Motin 			goto out;
2662f6ad3f23SAlexander Motin 		}
2663f6ad3f23SAlexander Motin 		err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2664f6ad3f23SAlexander Motin 						       bufsiz, eip, nobj);
2665f6ad3f23SAlexander Motin 		break;
2666f6ad3f23SAlexander Motin 	default:
26675e6609a2SMatt Jacob 		ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2668f6ad3f23SAlexander Motin 		    "of unknown type 0x%x\n", nobj,
2669f6ad3f23SAlexander Motin 		    ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2670f6ad3f23SAlexander Motin 		err = ENODEV;
2671f6ad3f23SAlexander Motin 		break;
2672f6ad3f23SAlexander Motin 	}
2673f6ad3f23SAlexander Motin 
2674f6ad3f23SAlexander Motin out:
2675f6ad3f23SAlexander Motin 	return (err);
2676f6ad3f23SAlexander Motin }
2677f6ad3f23SAlexander Motin 
267853f5ac13SAlexander Motin /**
267953f5ac13SAlexander Motin  * \brief Update the softc with the additional element status data for this
268053f5ac13SAlexander Motin  * 	  object, for ATA objects.
268153f5ac13SAlexander Motin  *
268253f5ac13SAlexander Motin  * \param enc		SES softc to be updated.
268353f5ac13SAlexander Motin  * \param buf		The additional element status response buffer.
268453f5ac13SAlexander Motin  * \param bufsiz	Size of the response buffer.
268553f5ac13SAlexander Motin  * \param eip		The EIP bit value.
268653f5ac13SAlexander Motin  * \param tidx		Type index for this object.
268753f5ac13SAlexander Motin  * \param nobj		Number of objects attached to the SES softc.
268853f5ac13SAlexander Motin  *
268953f5ac13SAlexander Motin  * \return		0 on success, errno otherwise.
269053f5ac13SAlexander Motin  */
269153f5ac13SAlexander Motin static int
ses_get_elm_addlstatus_ata(enc_softc_t * enc,enc_cache_t * enc_cache,uint8_t * buf,int bufsiz,int eip,int tidx,int nobj)269253f5ac13SAlexander Motin ses_get_elm_addlstatus_ata(enc_softc_t *enc, enc_cache_t *enc_cache,
269353f5ac13SAlexander Motin 			   uint8_t *buf, int bufsiz, int eip, int tidx,
269453f5ac13SAlexander Motin 			   int nobj)
269553f5ac13SAlexander Motin {
269653f5ac13SAlexander Motin 	int err;
269753f5ac13SAlexander Motin 	ses_cache_t *ses_cache;
269853f5ac13SAlexander Motin 
269953f5ac13SAlexander Motin 	if (bufsiz < sizeof(struct ses_elm_ata_hdr)) {
270053f5ac13SAlexander Motin 		err = EIO;
270153f5ac13SAlexander Motin 		goto out;
270253f5ac13SAlexander Motin 	}
270353f5ac13SAlexander Motin 
270453f5ac13SAlexander Motin 	ses_cache = enc_cache->private;
270553f5ac13SAlexander Motin 	switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
270653f5ac13SAlexander Motin 	case ELMTYP_DEVICE:
270753f5ac13SAlexander Motin 	case ELMTYP_ARRAY_DEV:
270853f5ac13SAlexander Motin 		break;
270953f5ac13SAlexander Motin 	default:
271053f5ac13SAlexander Motin 		ENC_VLOG(enc, "Element %d has Additional Status, "
271153f5ac13SAlexander Motin 		    "invalid for SES element type 0x%x\n", nobj,
271253f5ac13SAlexander Motin 		    ses_cache->ses_types[tidx].hdr->etype_elm_type);
271353f5ac13SAlexander Motin 		err = ENODEV;
271453f5ac13SAlexander Motin 		goto out;
271553f5ac13SAlexander Motin 	}
271653f5ac13SAlexander Motin 
271753f5ac13SAlexander Motin 	((ses_element_t *)enc_cache->elm_map[nobj].elm_private)
271853f5ac13SAlexander Motin 	    ->addl.proto_hdr.ata = (struct ses_elm_ata_hdr *)buf;
271953f5ac13SAlexander Motin 	err = 0;
272053f5ac13SAlexander Motin 
272153f5ac13SAlexander Motin out:
272253f5ac13SAlexander Motin 	return (err);
272353f5ac13SAlexander Motin }
272453f5ac13SAlexander Motin 
2725f6ad3f23SAlexander Motin static void
ses_softc_invalidate(enc_softc_t * enc)2726f6ad3f23SAlexander Motin ses_softc_invalidate(enc_softc_t *enc)
2727f6ad3f23SAlexander Motin {
2728f6ad3f23SAlexander Motin 	ses_softc_t *ses;
2729f6ad3f23SAlexander Motin 
2730f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2731f6ad3f23SAlexander Motin 	ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2732f6ad3f23SAlexander Motin }
2733f6ad3f23SAlexander Motin 
2734f6ad3f23SAlexander Motin static void
ses_softc_cleanup(enc_softc_t * enc)2735f6ad3f23SAlexander Motin ses_softc_cleanup(enc_softc_t *enc)
2736f6ad3f23SAlexander Motin {
2737f6ad3f23SAlexander Motin 
2738f6ad3f23SAlexander Motin 	ses_cache_free(enc, &enc->enc_cache);
2739f6ad3f23SAlexander Motin 	ses_cache_free(enc, &enc->enc_daemon_cache);
2740f6ad3f23SAlexander Motin 	ENC_FREE_AND_NULL(enc->enc_private);
2741f6ad3f23SAlexander Motin 	ENC_FREE_AND_NULL(enc->enc_cache.private);
2742f6ad3f23SAlexander Motin 	ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2743f6ad3f23SAlexander Motin }
2744f6ad3f23SAlexander Motin 
2745f6ad3f23SAlexander Motin static int
ses_init_enc(enc_softc_t * enc)2746f6ad3f23SAlexander Motin ses_init_enc(enc_softc_t *enc)
2747f6ad3f23SAlexander Motin {
2748f6ad3f23SAlexander Motin 	return (0);
2749f6ad3f23SAlexander Motin }
2750f6ad3f23SAlexander Motin 
2751f6ad3f23SAlexander Motin static int
ses_set_enc_status(enc_softc_t * enc,uint8_t encstat,int slpflag)2752f6ad3f23SAlexander Motin ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2753f6ad3f23SAlexander Motin {
2754f6ad3f23SAlexander Motin 	ses_control_request_t req;
2755f6ad3f23SAlexander Motin 	ses_softc_t	     *ses;
2756f6ad3f23SAlexander Motin 
2757f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2758f6ad3f23SAlexander Motin 	req.elm_idx = SES_SETSTATUS_ENC_IDX;
2759f6ad3f23SAlexander Motin 	req.elm_stat.comstatus = encstat & 0xf;
2760f6ad3f23SAlexander Motin 
2761f6ad3f23SAlexander Motin 	TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2762f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2763f6ad3f23SAlexander Motin 	cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2764f6ad3f23SAlexander Motin 
2765f6ad3f23SAlexander Motin 	return (req.result);
2766f6ad3f23SAlexander Motin }
2767f6ad3f23SAlexander Motin 
2768f6ad3f23SAlexander Motin static int
ses_get_elm_status(enc_softc_t * enc,encioc_elm_status_t * elms,int slpflag)2769f6ad3f23SAlexander Motin ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2770f6ad3f23SAlexander Motin {
2771f6ad3f23SAlexander Motin 	unsigned int i = elms->elm_idx;
2772f6ad3f23SAlexander Motin 
2773f6ad3f23SAlexander Motin 	memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2774f6ad3f23SAlexander Motin 	return (0);
2775f6ad3f23SAlexander Motin }
2776f6ad3f23SAlexander Motin 
2777f6ad3f23SAlexander Motin static int
ses_set_elm_status(enc_softc_t * enc,encioc_elm_status_t * elms,int slpflag)2778f6ad3f23SAlexander Motin ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2779f6ad3f23SAlexander Motin {
2780f6ad3f23SAlexander Motin 	ses_control_request_t req;
2781f6ad3f23SAlexander Motin 	ses_softc_t	     *ses;
2782f6ad3f23SAlexander Motin 
2783f6ad3f23SAlexander Motin 	/* If this is clear, we don't do diddly.  */
2784f6ad3f23SAlexander Motin 	if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2785f6ad3f23SAlexander Motin 		return (0);
2786f6ad3f23SAlexander Motin 
2787f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2788f6ad3f23SAlexander Motin 	req.elm_idx = elms->elm_idx;
2789f6ad3f23SAlexander Motin 	memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2790f6ad3f23SAlexander Motin 
2791f6ad3f23SAlexander Motin 	TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2792f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2793f6ad3f23SAlexander Motin 	cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2794f6ad3f23SAlexander Motin 
2795f6ad3f23SAlexander Motin 	return (req.result);
2796f6ad3f23SAlexander Motin }
2797f6ad3f23SAlexander Motin 
2798f6ad3f23SAlexander Motin static int
ses_get_elm_desc(enc_softc_t * enc,encioc_elm_desc_t * elmd)2799f6ad3f23SAlexander Motin ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2800f6ad3f23SAlexander Motin {
2801f6ad3f23SAlexander Motin 	int i = (int)elmd->elm_idx;
2802f6ad3f23SAlexander Motin 	ses_element_t *elmpriv;
2803f6ad3f23SAlexander Motin 
2804f6ad3f23SAlexander Motin 	/* Assume caller has already checked obj_id validity */
2805f6ad3f23SAlexander Motin 	elmpriv = enc->enc_cache.elm_map[i].elm_private;
2806f6ad3f23SAlexander Motin 	/* object might not have a descriptor */
2807f6ad3f23SAlexander Motin 	if (elmpriv == NULL || elmpriv->descr == NULL) {
2808f6ad3f23SAlexander Motin 		elmd->elm_desc_len = 0;
2809f6ad3f23SAlexander Motin 		return (0);
2810f6ad3f23SAlexander Motin 	}
2811f6ad3f23SAlexander Motin 	if (elmd->elm_desc_len > elmpriv->descr_len)
2812f6ad3f23SAlexander Motin 		elmd->elm_desc_len = elmpriv->descr_len;
2813*771501e9SMark Johnston 	return (copyout(elmpriv->descr, elmd->elm_desc_str,
2814*771501e9SMark Johnston 	    elmd->elm_desc_len));
2815f6ad3f23SAlexander Motin }
2816f6ad3f23SAlexander Motin 
2817f6ad3f23SAlexander Motin /**
2818f6ad3f23SAlexander Motin  * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2819f6ad3f23SAlexander Motin  *	  given object id if one is available.
2820f6ad3f23SAlexander Motin  *
2821f6ad3f23SAlexander Motin  * \param enc	SES softc to examine.
2822f6ad3f23SAlexander Motin  * \param objdn	ioctl structure to read/write device name info.
2823f6ad3f23SAlexander Motin  *
2824f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
2825f6ad3f23SAlexander Motin  */
2826f6ad3f23SAlexander Motin static int
ses_get_elm_devnames(enc_softc_t * enc,encioc_elm_devnames_t * elmdn)2827f6ad3f23SAlexander Motin ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2828f6ad3f23SAlexander Motin {
2829f6ad3f23SAlexander Motin 	struct sbuf sb;
2830*771501e9SMark Johnston 	int error, len;
2831f6ad3f23SAlexander Motin 
2832f6ad3f23SAlexander Motin 	len = elmdn->elm_names_size;
2833f6ad3f23SAlexander Motin 	if (len < 0)
2834f6ad3f23SAlexander Motin 		return (EINVAL);
2835f6ad3f23SAlexander Motin 
2836f6ad3f23SAlexander Motin 	cam_periph_unlock(enc->periph);
283725ee4c04SAlexander Motin 	sbuf_new(&sb, NULL, len, SBUF_FIXEDLEN);
2838f6ad3f23SAlexander Motin 	ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2839f6ad3f23SAlexander Motin 	    ses_elmdevname_callback, &sb);
2840f6ad3f23SAlexander Motin 	sbuf_finish(&sb);
2841f6ad3f23SAlexander Motin 	elmdn->elm_names_len = sbuf_len(&sb);
2842*771501e9SMark Johnston 	error = copyout(sbuf_data(&sb), elmdn->elm_devnames,
2843*771501e9SMark Johnston 	    elmdn->elm_names_len + 1);
2844b3db6c01SMark Johnston 	sbuf_delete(&sb);
2845f6ad3f23SAlexander Motin 	cam_periph_lock(enc->periph);
2846*771501e9SMark Johnston 	if (error == 0 && elmdn->elm_names_len == 0)
2847*771501e9SMark Johnston 		error = ENODEV;
2848*771501e9SMark Johnston 	return (error);
2849f6ad3f23SAlexander Motin }
2850f6ad3f23SAlexander Motin 
2851f6ad3f23SAlexander Motin /**
2852f6ad3f23SAlexander Motin  * \brief Send a string to the primary subenclosure using the String Out
2853f6ad3f23SAlexander Motin  * 	  SES diagnostic page.
2854f6ad3f23SAlexander Motin  *
2855f6ad3f23SAlexander Motin  * \param enc	SES enclosure to run the command on.
2856f6ad3f23SAlexander Motin  * \param sstr	SES string structure to operate on
2857f6ad3f23SAlexander Motin  * \param ioc	Ioctl being performed
2858f6ad3f23SAlexander Motin  *
2859f6ad3f23SAlexander Motin  * \return	0 on success, errno otherwise.
2860f6ad3f23SAlexander Motin  */
2861f6ad3f23SAlexander Motin static int
ses_handle_string(enc_softc_t * enc,encioc_string_t * sstr,unsigned long ioc)2862170a0a8eSAlan Somers ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, unsigned long ioc)
2863f6ad3f23SAlexander Motin {
28646d2a1fbfSAlexander Motin 	enc_cache_t *enc_cache;
28656d2a1fbfSAlexander Motin 	ses_cache_t *ses_cache;
28666d2a1fbfSAlexander Motin 	const struct ses_enc_desc *enc_desc;
2867f6ad3f23SAlexander Motin 	int amt, payload, ret;
2868f6ad3f23SAlexander Motin 	char cdb[6];
28696d2a1fbfSAlexander Motin 	char str[32];
28706d2a1fbfSAlexander Motin 	char vendor[9];
28716d2a1fbfSAlexander Motin 	char product[17];
28726d2a1fbfSAlexander Motin 	char rev[5];
2873f6ad3f23SAlexander Motin 	uint8_t *buf;
28746d2a1fbfSAlexander Motin 	size_t size, rsize;
28756d2a1fbfSAlexander Motin 
28766d2a1fbfSAlexander Motin 	enc_cache = &enc->enc_daemon_cache;
28776d2a1fbfSAlexander Motin 	ses_cache = enc_cache->private;
2878f6ad3f23SAlexander Motin 
2879f6ad3f23SAlexander Motin 	/* Implement SES2r20 6.1.6 */
2880170a0a8eSAlan Somers 	if (sstr->bufsiz > ENC_STRING_MAX)
2881f6ad3f23SAlexander Motin 		return (EINVAL); /* buffer size too large */
2882f6ad3f23SAlexander Motin 
28836a69d2a4SAlexander Motin 	switch (ioc) {
28846a69d2a4SAlexander Motin 	case ENCIOC_SETSTRING:
2885f6ad3f23SAlexander Motin 		payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2886f6ad3f23SAlexander Motin 		amt = 0 - payload;
2887f6ad3f23SAlexander Motin 		buf = ENC_MALLOC(payload);
2888f6ad3f23SAlexander Motin 		if (buf == NULL)
28896a69d2a4SAlexander Motin 			return (ENOMEM);
2890f6ad3f23SAlexander Motin 		ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2891f6ad3f23SAlexander Motin 		/* Construct the page request */
2892f6ad3f23SAlexander Motin 		buf[0] = SesStringOut;
2893f6ad3f23SAlexander Motin 		buf[1] = 0;
2894f6ad3f23SAlexander Motin 		buf[2] = sstr->bufsiz >> 8;
2895f6ad3f23SAlexander Motin 		buf[3] = sstr->bufsiz & 0xff;
289647e735adSJohn Baldwin 		ret = copyin(sstr->buf, &buf[4], sstr->bufsiz);
289747e735adSJohn Baldwin 		if (ret != 0) {
289847e735adSJohn Baldwin 			ENC_FREE(buf);
289947e735adSJohn Baldwin 			return (ret);
290047e735adSJohn Baldwin 		}
29016a69d2a4SAlexander Motin 		break;
29026a69d2a4SAlexander Motin 	case ENCIOC_GETSTRING:
2903f6ad3f23SAlexander Motin 		payload = sstr->bufsiz;
2904f6ad3f23SAlexander Motin 		amt = payload;
290547e735adSJohn Baldwin 		buf = ENC_MALLOC(payload);
290647e735adSJohn Baldwin 		if (buf == NULL)
290747e735adSJohn Baldwin 			return (ENOMEM);
2908f6ad3f23SAlexander Motin 		ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
29096a69d2a4SAlexander Motin 		break;
29106a69d2a4SAlexander Motin 	case ENCIOC_GETENCNAME:
29116d2a1fbfSAlexander Motin 		if (ses_cache->ses_nsubencs < 1)
29126d2a1fbfSAlexander Motin 			return (ENODEV);
29136d2a1fbfSAlexander Motin 		enc_desc = ses_cache->subencs[0];
29146d2a1fbfSAlexander Motin 		cam_strvis(vendor, enc_desc->vendor_id,
29156d2a1fbfSAlexander Motin 		    sizeof(enc_desc->vendor_id), sizeof(vendor));
29166d2a1fbfSAlexander Motin 		cam_strvis(product, enc_desc->product_id,
29176d2a1fbfSAlexander Motin 		    sizeof(enc_desc->product_id), sizeof(product));
29186d2a1fbfSAlexander Motin 		cam_strvis(rev, enc_desc->product_rev,
29196d2a1fbfSAlexander Motin 		    sizeof(enc_desc->product_rev), sizeof(rev));
29206d2a1fbfSAlexander Motin 		rsize = snprintf(str, sizeof(str), "%s %s %s",
29216d2a1fbfSAlexander Motin 		    vendor, product, rev) + 1;
29226d2a1fbfSAlexander Motin 		if (rsize > sizeof(str))
29236d2a1fbfSAlexander Motin 			rsize = sizeof(str);
29246d2a1fbfSAlexander Motin 		size = rsize;
29256d2a1fbfSAlexander Motin 		if (size > sstr->bufsiz)
29266d2a1fbfSAlexander Motin 			size = sstr->bufsiz;
2927*771501e9SMark Johnston 		ret = copyout(str, sstr->buf, size);
29285326289dSJohn Baldwin 		sstr->bufsiz = rsize;
2929*771501e9SMark Johnston 		return (ret != 0 ? ret : (size == rsize ? 0 : ENOMEM));
29306a69d2a4SAlexander Motin 	case ENCIOC_GETENCID:
29316d2a1fbfSAlexander Motin 		if (ses_cache->ses_nsubencs < 1)
29326d2a1fbfSAlexander Motin 			return (ENODEV);
29336d2a1fbfSAlexander Motin 		enc_desc = ses_cache->subencs[0];
29346d2a1fbfSAlexander Motin 		rsize = snprintf(str, sizeof(str), "%16jx",
29356d2a1fbfSAlexander Motin 		    scsi_8btou64(enc_desc->logical_id)) + 1;
29366d2a1fbfSAlexander Motin 		if (rsize > sizeof(str))
29376d2a1fbfSAlexander Motin 			rsize = sizeof(str);
29386d2a1fbfSAlexander Motin 		size = rsize;
29396d2a1fbfSAlexander Motin 		if (size > sstr->bufsiz)
29406d2a1fbfSAlexander Motin 			size = sstr->bufsiz;
2941*771501e9SMark Johnston 		ret = copyout(str, sstr->buf, size);
29425326289dSJohn Baldwin 		sstr->bufsiz = rsize;
2943*771501e9SMark Johnston 		return (ret != 0 ? ret : (size == rsize ? 0 : ENOMEM));
29446a69d2a4SAlexander Motin 	default:
29456a69d2a4SAlexander Motin 		return (EINVAL);
29466a69d2a4SAlexander Motin 	}
2947f6ad3f23SAlexander Motin 	ret = enc_runcmd(enc, cdb, 6, buf, &amt);
294847e735adSJohn Baldwin 	if (ret == 0 && ioc == ENCIOC_GETSTRING)
294947e735adSJohn Baldwin 		ret = copyout(buf, sstr->buf, sstr->bufsiz);
2950f721fcbcSJohn Baldwin 	if (ioc == ENCIOC_SETSTRING || ioc == ENCIOC_GETSTRING)
2951f6ad3f23SAlexander Motin 		ENC_FREE(buf);
29526a69d2a4SAlexander Motin 	return (ret);
2953f6ad3f23SAlexander Motin }
2954f6ad3f23SAlexander Motin 
2955f6ad3f23SAlexander Motin /**
2956f6ad3f23SAlexander Motin  * \invariant Called with cam_periph mutex held.
2957f6ad3f23SAlexander Motin  */
2958f6ad3f23SAlexander Motin static void
ses_poll_status(enc_softc_t * enc)2959f6ad3f23SAlexander Motin ses_poll_status(enc_softc_t *enc)
2960f6ad3f23SAlexander Motin {
2961f6ad3f23SAlexander Motin 	ses_softc_t *ses;
2962f6ad3f23SAlexander Motin 
2963f6ad3f23SAlexander Motin 	ses = enc->enc_private;
2964f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_UPDATE_GETSTATUS);
29657027c01cSAlexander Motin 	if (ses->ses_flags & SES_FLAG_DESC)
29667027c01cSAlexander Motin 		enc_update_request(enc, SES_UPDATE_GETELMDESCS);
2967f6ad3f23SAlexander Motin 	if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2968f6ad3f23SAlexander Motin 		enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2969f6ad3f23SAlexander Motin }
2970f6ad3f23SAlexander Motin 
2971f6ad3f23SAlexander Motin /**
2972f6ad3f23SAlexander Motin  * \brief Notification received when CAM detects a new device in the
2973f6ad3f23SAlexander Motin  *        SCSI domain in which this SEP resides.
2974f6ad3f23SAlexander Motin  *
2975f6ad3f23SAlexander Motin  * \param enc	SES enclosure instance.
2976f6ad3f23SAlexander Motin  */
2977f6ad3f23SAlexander Motin static void
ses_device_found(enc_softc_t * enc)2978f6ad3f23SAlexander Motin ses_device_found(enc_softc_t *enc)
2979f6ad3f23SAlexander Motin {
2980f6ad3f23SAlexander Motin 	ses_poll_status(enc);
2981f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2982f6ad3f23SAlexander Motin }
2983f6ad3f23SAlexander Motin 
2984f6ad3f23SAlexander Motin static struct enc_vec ses_enc_vec =
2985f6ad3f23SAlexander Motin {
2986f6ad3f23SAlexander Motin 	.softc_invalidate	= ses_softc_invalidate,
2987f6ad3f23SAlexander Motin 	.softc_cleanup		= ses_softc_cleanup,
2988f6ad3f23SAlexander Motin 	.init_enc		= ses_init_enc,
2989f6ad3f23SAlexander Motin 	.set_enc_status		= ses_set_enc_status,
2990f6ad3f23SAlexander Motin 	.get_elm_status		= ses_get_elm_status,
2991f6ad3f23SAlexander Motin 	.set_elm_status		= ses_set_elm_status,
2992f6ad3f23SAlexander Motin 	.get_elm_desc		= ses_get_elm_desc,
2993f6ad3f23SAlexander Motin 	.get_elm_devnames	= ses_get_elm_devnames,
2994f6ad3f23SAlexander Motin 	.handle_string		= ses_handle_string,
2995f6ad3f23SAlexander Motin 	.device_found		= ses_device_found,
2996f6ad3f23SAlexander Motin 	.poll_status		= ses_poll_status
2997f6ad3f23SAlexander Motin };
2998f6ad3f23SAlexander Motin 
2999f6ad3f23SAlexander Motin /**
3000f6ad3f23SAlexander Motin  * \brief Initialize a new SES instance.
3001f6ad3f23SAlexander Motin  *
3002f6ad3f23SAlexander Motin  * \param enc		SES softc structure to set up the instance in.
3003f6ad3f23SAlexander Motin  * \param doinit	Do the initialization (see main driver).
3004f6ad3f23SAlexander Motin  *
3005f6ad3f23SAlexander Motin  * \return		0 on success, errno otherwise.
3006f6ad3f23SAlexander Motin  */
3007f6ad3f23SAlexander Motin int
ses_softc_init(enc_softc_t * enc)3008f6ad3f23SAlexander Motin ses_softc_init(enc_softc_t *enc)
3009f6ad3f23SAlexander Motin {
3010f6ad3f23SAlexander Motin 	ses_softc_t *ses_softc;
3011f6ad3f23SAlexander Motin 
3012f6ad3f23SAlexander Motin 	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
3013f6ad3f23SAlexander Motin 	    ("entering enc_softc_init(%p)\n", enc));
3014f6ad3f23SAlexander Motin 
3015f6ad3f23SAlexander Motin 	enc->enc_vec = ses_enc_vec;
3016f6ad3f23SAlexander Motin 	enc->enc_fsm_states = enc_fsm_states;
3017f6ad3f23SAlexander Motin 
3018f6ad3f23SAlexander Motin 	if (enc->enc_private == NULL)
3019f6ad3f23SAlexander Motin 		enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
3020f6ad3f23SAlexander Motin 	if (enc->enc_cache.private == NULL)
3021f6ad3f23SAlexander Motin 		enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
3022f6ad3f23SAlexander Motin 	if (enc->enc_daemon_cache.private == NULL)
3023f6ad3f23SAlexander Motin 		enc->enc_daemon_cache.private =
3024f6ad3f23SAlexander Motin 		     ENC_MALLOCZ(sizeof(ses_cache_t));
3025f6ad3f23SAlexander Motin 
3026f6ad3f23SAlexander Motin 	if (enc->enc_private == NULL
3027f6ad3f23SAlexander Motin 	 || enc->enc_cache.private == NULL
3028f6ad3f23SAlexander Motin 	 || enc->enc_daemon_cache.private == NULL) {
3029f6ad3f23SAlexander Motin 		ENC_FREE_AND_NULL(enc->enc_private);
3030f6ad3f23SAlexander Motin 		ENC_FREE_AND_NULL(enc->enc_cache.private);
3031f6ad3f23SAlexander Motin 		ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
3032f6ad3f23SAlexander Motin 		return (ENOMEM);
3033f6ad3f23SAlexander Motin 	}
3034f6ad3f23SAlexander Motin 
3035f6ad3f23SAlexander Motin 	ses_softc = enc->enc_private;
3036f6ad3f23SAlexander Motin 	TAILQ_INIT(&ses_softc->ses_requests);
3037f6ad3f23SAlexander Motin 	TAILQ_INIT(&ses_softc->ses_pending_requests);
3038f6ad3f23SAlexander Motin 
3039f6ad3f23SAlexander Motin 	enc_update_request(enc, SES_UPDATE_PAGES);
3040f6ad3f23SAlexander Motin 
3041f6ad3f23SAlexander Motin 	// XXX: Move this to the FSM so it doesn't hang init
3042f6ad3f23SAlexander Motin 	if (0) (void) ses_set_timed_completion(enc, 1);
3043f6ad3f23SAlexander Motin 
3044f6ad3f23SAlexander Motin 	return (0);
3045f6ad3f23SAlexander Motin }
3046