scsi_enc_ses.c revision 309629
184865Sobrien/*-
2130561Sobrien * Copyright (c) 2000 Matthew Jacob
384865Sobrien * Copyright (c) 2010 Spectra Logic Corporation
484865Sobrien * All rights reserved.
584865Sobrien *
684865Sobrien * Redistribution and use in source and binary forms, with or without
784865Sobrien * modification, are permitted provided that the following conditions
884865Sobrien * are met:
984865Sobrien * 1. Redistributions of source code must retain the above copyright
1084865Sobrien *    notice, this list of conditions, and the following disclaimer,
1184865Sobrien *    without modification, immediately at the beginning of the file.
1284865Sobrien * 2. The name of the author may not be used to endorse or promote products
1384865Sobrien *    derived from this software without specific prior written permission.
1484865Sobrien *
1584865Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1684865Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1784865Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1884865Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19218822Sdim * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20218822Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2184865Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2284865Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2384865Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2484865Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2584865Sobrien * SUCH DAMAGE.
2684865Sobrien */
2784865Sobrien
2884865Sobrien/**
2984865Sobrien * \file scsi_enc_ses.c
3084865Sobrien *
3184865Sobrien * Structures and routines specific && private to SES only
3284865Sobrien */
3384865Sobrien
3484865Sobrien#include <sys/cdefs.h>
3584865Sobrien__FBSDID("$FreeBSD: stable/10/sys/cam/scsi/scsi_enc_ses.c 309629 2016-12-06 17:10:17Z mav $");
3684865Sobrien
3784865Sobrien#include <sys/param.h>
3884865Sobrien
39130561Sobrien#include <sys/ctype.h>
4084865Sobrien#include <sys/errno.h>
4184865Sobrien#include <sys/kernel.h>
4284865Sobrien#include <sys/lock.h>
4384865Sobrien#include <sys/malloc.h>
4484865Sobrien#include <sys/mutex.h>
4584865Sobrien#include <sys/queue.h>
4684865Sobrien#include <sys/sbuf.h>
4784865Sobrien#include <sys/sx.h>
4884865Sobrien#include <sys/systm.h>
4984865Sobrien#include <sys/types.h>
5084865Sobrien
5184865Sobrien#include <cam/cam.h>
52130561Sobrien#include <cam/cam_ccb.h>
5384865Sobrien#include <cam/cam_xpt_periph.h>
5484865Sobrien#include <cam/cam_periph.h>
5584865Sobrien
5684865Sobrien#include <cam/scsi/scsi_message.h>
5784865Sobrien#include <cam/scsi/scsi_enc.h>
5884865Sobrien#include <cam/scsi/scsi_enc_internal.h>
5984865Sobrien
6084865Sobrien/* SES Native Type Device Support */
6184865Sobrien
6284865Sobrien/* SES Diagnostic Page Codes */
6384865Sobrientypedef enum {
6484865Sobrien	SesSupportedPages	= 0x0,
6584865Sobrien	SesConfigPage		= 0x1,
6684865Sobrien	SesControlPage		= 0x2,
6784865Sobrien	SesStatusPage		= SesControlPage,
6884865Sobrien	SesHelpTxt		= 0x3,
6984865Sobrien	SesStringOut		= 0x4,
7084865Sobrien	SesStringIn		= SesStringOut,
7184865Sobrien	SesThresholdOut		= 0x5,
7284865Sobrien	SesThresholdIn		= SesThresholdOut,
7384865Sobrien	SesArrayControl		= 0x6,	/* Obsolete in SES v2 */
74130561Sobrien	SesArrayStatus		= SesArrayControl,
75130561Sobrien	SesElementDescriptor	= 0x7,
7684865Sobrien	SesShortStatus		= 0x8,
7784865Sobrien	SesEnclosureBusy	= 0x9,
7884865Sobrien	SesAddlElementStatus	= 0xa
7984865Sobrien} SesDiagPageCodes;
8084865Sobrien
81130561Sobrientypedef struct ses_type {
82130561Sobrien	const struct ses_elm_type_desc  *hdr;
83130561Sobrien	const char			*text;
84130561Sobrien} ses_type_t;
8584865Sobrien
8684865Sobrientypedef struct ses_comstat {
87130561Sobrien	uint8_t	comstatus;
8884865Sobrien	uint8_t	comstat[3];
89130561Sobrien} ses_comstat_t;
90130561Sobrien
91130561Sobrientypedef union ses_addl_data {
92130561Sobrien	struct ses_elm_sas_device_phy *sasdev_phys;
93130561Sobrien	struct ses_elm_sas_expander_phy *sasexp_phys;
9484865Sobrien	struct ses_elm_sas_port_phy *sasport_phys;
95130561Sobrien	struct ses_fcobj_port *fc_ports;
96130561Sobrien} ses_add_data_t;
97130561Sobrien
98130561Sobrientypedef struct ses_addl_status {
99130561Sobrien	struct ses_elm_addlstatus_base_hdr *hdr;
10084865Sobrien	union {
101130561Sobrien		union ses_fcobj_hdr *fc;
102130561Sobrien		union ses_elm_sas_hdr *sas;
103130561Sobrien	} proto_hdr;
104130561Sobrien	union ses_addl_data proto_data;	/* array sizes stored in header */
105130561Sobrien} ses_add_status_t;
106130561Sobrien
107130561Sobrientypedef struct ses_element {
108130561Sobrien	uint8_t eip;			/* eip bit is set */
109130561Sobrien	uint16_t descr_len;		/* length of the descriptor */
110130561Sobrien	char *descr;			/* descriptor for this object */
111130561Sobrien	struct ses_addl_status addl;	/* additional status info */
112130561Sobrien} ses_element_t;
113130561Sobrien
114130561Sobrientypedef struct ses_control_request {
115130561Sobrien	int	      elm_idx;
116130561Sobrien	ses_comstat_t elm_stat;
117130561Sobrien	int	      result;
118130561Sobrien	TAILQ_ENTRY(ses_control_request) links;
119130561Sobrien} ses_control_request_t;
120130561SobrienTAILQ_HEAD(ses_control_reqlist, ses_control_request);
12184865Sobrientypedef struct ses_control_reqlist ses_control_reqlist_t;
122130561Sobrienenum {
123130561Sobrien	SES_SETSTATUS_ENC_IDX = -1
124130561Sobrien};
125130561Sobrien
126130561Sobrienstatic void
127130561Sobrienses_terminate_control_requests(ses_control_reqlist_t *reqlist, int result)
12884865Sobrien{
129130561Sobrien	ses_control_request_t *req;
130130561Sobrien
131130561Sobrien	while ((req = TAILQ_FIRST(reqlist)) != NULL) {
132130561Sobrien		TAILQ_REMOVE(reqlist, req, links);
133130561Sobrien		req->result = result;
134130561Sobrien		wakeup(req);
135130561Sobrien	}
136130561Sobrien}
137130561Sobrien
138130561Sobrienenum ses_iter_index_values {
139130561Sobrien	/**
140130561Sobrien	 * \brief  Value of an initialized but invalid index
141130561Sobrien	 *         in a ses_iterator object.
142130561Sobrien	 *
14384865Sobrien	 * This value is used for the  individual_element_index of
144130561Sobrien	 * overal status elements and for all index types when
145130561Sobrien	 * an iterator is first initialized.
146130561Sobrien	 */
147130561Sobrien	ITERATOR_INDEX_INVALID = -1,
148130561Sobrien
149130561Sobrien	/**
150130561Sobrien	 * \brief  Value of an index in a ses_iterator object
151130561Sobrien	 *	   when the iterator has traversed past the last
152130561Sobrien	 *	   valid element..
153130561Sobrien	 */
154130561Sobrien	ITERATOR_INDEX_END     = INT_MAX
155130561Sobrien};
156130561Sobrien
157130561Sobrien/**
158130561Sobrien * \brief Structure encapsulating all data necessary to traverse the
159130561Sobrien *        elements of a SES configuration.
160130561Sobrien *
161130561Sobrien * The ses_iterator object simplifies the task of iterating through all
162130561Sobrien * elements detected via the SES configuration page by tracking the numerous
163130561Sobrien * element indexes that, instead of memoizing in the softc, we calculate
16484865Sobrien * on the fly during the traversal of the element objects.  The various
165130561Sobrien * indexes are necessary due to the varying needs of matching objects in
16684865Sobrien * the different SES pages.  Some pages (e.g. Status/Control) contain all
167130561Sobrien * elements, while others (e.g. Additional Element Status) only contain
168130561Sobrien * individual elements (no overal status elements) of particular types.
169130561Sobrien *
170130561Sobrien * To use an iterator, initialize it with ses_iter_init(), and then
171130561Sobrien * use ses_iter_next() to traverse the elements (including the first) in
172130561Sobrien * the configuration.  Once an iterator is initiailized with ses_iter_init(),
173130561Sobrien * you may also seek to any particular element by either it's global or
174130561Sobrien * individual element index via the ses_iter_seek_to() function.  You may
175130561Sobrien * also return an iterator to the position just before the first element
176130561Sobrien * (i.e. the same state as after an ses_iter_init()), with ses_iter_reset().
177130561Sobrien */
178130561Sobrienstruct ses_iterator {
179130561Sobrien	/**
180130561Sobrien	 * \brief Backlink to the overal software configuration structure.
181130561Sobrien	 *
18284865Sobrien	 * This is included for convenience so the iteration functions
183130561Sobrien	 * need only take a single, struct ses_iterator *, argument.
184130561Sobrien	 */
185130561Sobrien	enc_softc_t *enc;
18684865Sobrien
187130561Sobrien	enc_cache_t *cache;
188130561Sobrien
189130561Sobrien	/**
190130561Sobrien	 * \brief Index of the type of the current element within the
191130561Sobrien	 *        ses_cache's ses_types array.
19284865Sobrien	 */
193130561Sobrien	int	          type_index;
194130561Sobrien
195130561Sobrien	/**
196130561Sobrien	 * \brief The position (0 based) of this element relative to all other
197130561Sobrien	 *        elements of this type.
19884865Sobrien	 *
199130561Sobrien	 * This index resets to zero every time the iterator transitions
200130561Sobrien	 * to elements of a new type in the configuration.
201130561Sobrien	 */
202130561Sobrien	int	          type_element_index;
203130561Sobrien
204130561Sobrien	/**
205130561Sobrien	 * \brief The position (0 based) of this element relative to all
206130561Sobrien	 *        other individual status elements in the configuration.
207130561Sobrien	 *
208130561Sobrien	 * This index ranges from 0 through the number of individual
209130561Sobrien	 * elements in the configuration.  When the iterator returns
210130561Sobrien	 * an overall status element, individual_element_index is
211130561Sobrien	 * set to ITERATOR_INDEX_INVALID, to indicate that it does
212130561Sobrien	 * not apply to the current element.
213130561Sobrien	 */
214130561Sobrien	int	          individual_element_index;
215130561Sobrien
216130561Sobrien	/**
217130561Sobrien	 * \brief The position (0 based) of this element relative to
218130561Sobrien	 *        all elements in the configration.
21984865Sobrien	 *
220130561Sobrien	 * This index is appropriate for indexing into enc->ses_elm_map.
221130561Sobrien	 */
222130561Sobrien	int	          global_element_index;
223130561Sobrien
224130561Sobrien	/**
225130561Sobrien	 * \brief The last valid individual element index of this
226130561Sobrien	 *        iterator.
227130561Sobrien	 *
228130561Sobrien	 * When an iterator traverses an overal status element, the
229130561Sobrien	 * individual element index is reset to ITERATOR_INDEX_INVALID
230130561Sobrien	 * to prevent unintential use of the individual_element_index
231130561Sobrien	 * field.  The saved_individual_element_index allows the iterator
232130561Sobrien	 * to restore it's position in the individual elements upon
233130561Sobrien	 * reaching the next individual element.
234130561Sobrien	 */
235130561Sobrien	int	          saved_individual_element_index;
236130561Sobrien};
237130561Sobrien
238130561Sobrientypedef enum {
239130561Sobrien	SES_UPDATE_NONE,
240130561Sobrien	SES_UPDATE_PAGES,
241130561Sobrien	SES_UPDATE_GETCONFIG,
242130561Sobrien	SES_UPDATE_GETSTATUS,
243130561Sobrien	SES_UPDATE_GETELMDESCS,
244130561Sobrien	SES_UPDATE_GETELMADDLSTATUS,
245130561Sobrien	SES_PROCESS_CONTROL_REQS,
246130561Sobrien	SES_PUBLISH_PHYSPATHS,
247130561Sobrien	SES_PUBLISH_CACHE,
248130561Sobrien	SES_NUM_UPDATE_STATES
249130561Sobrien} ses_update_action;
250130561Sobrien
251130561Sobrienstatic enc_softc_cleanup_t ses_softc_cleanup;
252130561Sobrien
253130561Sobrien#define	SCSZ	0x8000
254130561Sobrien
255130561Sobrienstatic fsm_fill_handler_t ses_fill_rcv_diag_io;
256130561Sobrienstatic fsm_fill_handler_t ses_fill_control_request;
257130561Sobrienstatic fsm_done_handler_t ses_process_pages;
258130561Sobrienstatic fsm_done_handler_t ses_process_config;
259130561Sobrienstatic fsm_done_handler_t ses_process_status;
260130561Sobrienstatic fsm_done_handler_t ses_process_elm_descs;
261130561Sobrienstatic fsm_done_handler_t ses_process_elm_addlstatus;
262130561Sobrienstatic fsm_done_handler_t ses_process_control_request;
263130561Sobrienstatic fsm_done_handler_t ses_publish_physpaths;
264130561Sobrienstatic fsm_done_handler_t ses_publish_cache;
265130561Sobrien
266130561Sobrienstatic struct enc_fsm_state enc_fsm_states[SES_NUM_UPDATE_STATES] =
267130561Sobrien{
268130561Sobrien	{ "SES_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
269130561Sobrien	{
270130561Sobrien		"SES_UPDATE_PAGES",
271130561Sobrien		SesSupportedPages,
272130561Sobrien		SCSZ,
273130561Sobrien		60 * 1000,
274130561Sobrien		ses_fill_rcv_diag_io,
275130561Sobrien		ses_process_pages,
276130561Sobrien		enc_error
277130561Sobrien	},
278130561Sobrien	{
279130561Sobrien		"SES_UPDATE_GETCONFIG",
28084865Sobrien		SesConfigPage,
281130561Sobrien		SCSZ,
282130561Sobrien		60 * 1000,
283130561Sobrien		ses_fill_rcv_diag_io,
284130561Sobrien		ses_process_config,
285130561Sobrien		enc_error
286130561Sobrien	},
28784865Sobrien	{
288130561Sobrien		"SES_UPDATE_GETSTATUS",
289130561Sobrien		SesStatusPage,
290130561Sobrien		SCSZ,
291130561Sobrien		60 * 1000,
292130561Sobrien		ses_fill_rcv_diag_io,
293130561Sobrien		ses_process_status,
294130561Sobrien		enc_error
295130561Sobrien	},
296130561Sobrien	{
297130561Sobrien		"SES_UPDATE_GETELMDESCS",
298130561Sobrien		SesElementDescriptor,
299130561Sobrien		SCSZ,
300130561Sobrien		60 * 1000,
301130561Sobrien		ses_fill_rcv_diag_io,
302130561Sobrien		ses_process_elm_descs,
303130561Sobrien		enc_error
304130561Sobrien	},
305130561Sobrien	{
306130561Sobrien		"SES_UPDATE_GETELMADDLSTATUS",
307130561Sobrien		SesAddlElementStatus,
30884865Sobrien		SCSZ,
309130561Sobrien		60 * 1000,
310130561Sobrien		ses_fill_rcv_diag_io,
311130561Sobrien		ses_process_elm_addlstatus,
312130561Sobrien		enc_error
313130561Sobrien	},
314130561Sobrien	{
315130561Sobrien		"SES_PROCESS_CONTROL_REQS",
316130561Sobrien		SesControlPage,
317130561Sobrien		SCSZ,
318130561Sobrien		60 * 1000,
319130561Sobrien		ses_fill_control_request,
320130561Sobrien		ses_process_control_request,
321130561Sobrien		enc_error
322130561Sobrien	},
323130561Sobrien	{
324130561Sobrien		"SES_PUBLISH_PHYSPATHS",
325130561Sobrien		0,
326130561Sobrien		0,
327130561Sobrien		0,
328130561Sobrien		NULL,
329130561Sobrien		ses_publish_physpaths,
330130561Sobrien		NULL
331130561Sobrien	},
332130561Sobrien	{
333130561Sobrien		"SES_PUBLISH_CACHE",
334130561Sobrien		0,
335130561Sobrien		0,
336130561Sobrien		0,
337130561Sobrien		NULL,
338130561Sobrien		ses_publish_cache,
339130561Sobrien		NULL
340130561Sobrien	}
341130561Sobrien};
342130561Sobrien
343130561Sobrientypedef struct ses_cache {
344130561Sobrien	/* Source for all the configuration data pointers */
345130561Sobrien	const struct ses_cfg_page		*cfg_page;
346130561Sobrien
347130561Sobrien	/* References into the config page. */
348130561Sobrien	int					 ses_nsubencs;
34984865Sobrien	const struct ses_enc_desc * const	*subencs;
35084865Sobrien	int					 ses_ntypes;
351130561Sobrien	const ses_type_t			*ses_types;
352130561Sobrien
353130561Sobrien	/* Source for all the status pointers */
354130561Sobrien	const struct ses_status_page		*status_page;
355130561Sobrien
356130561Sobrien	/* Source for all the object descriptor pointers */
357130561Sobrien	const struct ses_elem_descr_page	*elm_descs_page;
358130561Sobrien
359130561Sobrien	/* Source for all the additional object status pointers */
360130561Sobrien	const struct ses_addl_elem_status_page  *elm_addlstatus_page;
361130561Sobrien
362130561Sobrien} ses_cache_t;
363130561Sobrien
364130561Sobrientypedef struct ses_softc {
365130561Sobrien	uint32_t		ses_flags;
366130561Sobrien#define	SES_FLAG_TIMEDCOMP	0x01
367130561Sobrien#define	SES_FLAG_ADDLSTATUS	0x02
368130561Sobrien#define	SES_FLAG_DESC		0x04
369130561Sobrien
370130561Sobrien	ses_control_reqlist_t	ses_requests;
371130561Sobrien	ses_control_reqlist_t	ses_pending_requests;
372130561Sobrien} ses_softc_t;
373130561Sobrien
374130561Sobrien/**
375130561Sobrien * \brief Reset a SES iterator to just before the first element
376130561Sobrien *        in the configuration.
377130561Sobrien *
378130561Sobrien * \param iter  The iterator object to reset.
379130561Sobrien *
380130561Sobrien * The indexes within a reset iterator are invalid and will only
381130561Sobrien * become valid upon completion of a ses_iter_seek_to() or a
382130561Sobrien * ses_iter_next().
383130561Sobrien */
384130561Sobrienstatic void
385130561Sobrienses_iter_reset(struct ses_iterator *iter)
386130561Sobrien{
387130561Sobrien	/*
388130561Sobrien	 * Set our indexes to just before the first valid element
389130561Sobrien	 * of the first type (ITERATOR_INDEX_INVALID == -1).  This
390130561Sobrien	 * simplifies the implementation of ses_iter_next().
391130561Sobrien	 */
392130561Sobrien	iter->type_index                     = 0;
393130561Sobrien	iter->type_element_index             = ITERATOR_INDEX_INVALID;
394130561Sobrien	iter->global_element_index           = ITERATOR_INDEX_INVALID;
395130561Sobrien	iter->individual_element_index       = ITERATOR_INDEX_INVALID;
396130561Sobrien	iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
397130561Sobrien}
398130561Sobrien
399130561Sobrien/**
400130561Sobrien * \brief Initialize the storage of a SES iterator and reset it to
401130561Sobrien *        the position just before the first element of the
402130561Sobrien *        configuration.
403130561Sobrien *
404130561Sobrien * \param enc	The SES softc for the SES instance whose configuration
405130561Sobrien *              will be enumerated by this iterator.
406130561Sobrien * \param iter  The iterator object to initialize.
407130561Sobrien */
408130561Sobrienstatic void
409130561Sobrienses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
410130561Sobrien{
411130561Sobrien	iter->enc = enc;
412130561Sobrien	iter->cache = cache;
413130561Sobrien	ses_iter_reset(iter);
414130561Sobrien}
415130561Sobrien
416130561Sobrien/**
417130561Sobrien * \brief Traverse the provided SES iterator to the next element
418130561Sobrien *        within the configuraiton.
419130561Sobrien *
420130561Sobrien * \param iter  The iterator to move.
421130561Sobrien *
422130561Sobrien * \return  If a valid next element exists, a pointer to it's enc_element_t.
423130561Sobrien *          Otherwise NULL.
424130561Sobrien */
425130561Sobrienstatic enc_element_t *
426130561Sobrienses_iter_next(struct ses_iterator *iter)
427130561Sobrien{
428130561Sobrien	ses_cache_t	 *ses_cache;
429130561Sobrien	const ses_type_t *element_type;
430130561Sobrien
43184865Sobrien	ses_cache = iter->cache->private;
432130561Sobrien
433130561Sobrien	/*
434130561Sobrien	 * Note: Treat nelms as signed, so we will hit this case
435130561Sobrien	 *       and immediately terminate the iteration if the
43684865Sobrien	 *	 configuration has 0 objects.
43784865Sobrien	 */
438130561Sobrien	if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
439130561Sobrien
440130561Sobrien		/* Elements exhausted. */
441130561Sobrien		iter->type_index	       = ITERATOR_INDEX_END;
442130561Sobrien		iter->type_element_index       = ITERATOR_INDEX_END;
443130561Sobrien		iter->global_element_index     = ITERATOR_INDEX_END;
444130561Sobrien		iter->individual_element_index = ITERATOR_INDEX_END;
445130561Sobrien		return (NULL);
446130561Sobrien	}
447130561Sobrien
448130561Sobrien	KASSERT((iter->type_index < ses_cache->ses_ntypes),
449130561Sobrien		("Corrupted element iterator. %d not less than %d",
450130561Sobrien		 iter->type_index, ses_cache->ses_ntypes));
451130561Sobrien
452130561Sobrien	element_type = &ses_cache->ses_types[iter->type_index];
453130561Sobrien	iter->global_element_index++;
454130561Sobrien	iter->type_element_index++;
455130561Sobrien
456130561Sobrien	/*
457130561Sobrien	 * There is an object for overal type status in addition
458130561Sobrien	 * to one for each allowed element, but only if the element
459130561Sobrien	 * count is non-zero.
460130561Sobrien	 */
461130561Sobrien	if (iter->type_element_index > element_type->hdr->etype_maxelt) {
462130561Sobrien
463130561Sobrien		/*
464130561Sobrien		 * We've exhausted the elements of this type.
465130561Sobrien		 * This next element belongs to the next type.
466130561Sobrien		 */
467130561Sobrien		iter->type_index++;
468130561Sobrien		iter->type_element_index = 0;
469130561Sobrien		iter->saved_individual_element_index
470130561Sobrien		    = iter->individual_element_index;
471130561Sobrien		iter->individual_element_index = ITERATOR_INDEX_INVALID;
472130561Sobrien	}
473130561Sobrien
474130561Sobrien	if (iter->type_element_index > 0) {
475130561Sobrien		if (iter->type_element_index == 1) {
476130561Sobrien			iter->individual_element_index
477130561Sobrien			    = iter->saved_individual_element_index;
478130561Sobrien		}
479130561Sobrien		iter->individual_element_index++;
480130561Sobrien	}
481130561Sobrien
482130561Sobrien	return (&iter->cache->elm_map[iter->global_element_index]);
483130561Sobrien}
484130561Sobrien
485130561Sobrien/**
486130561Sobrien * Element index types tracked by a SES iterator.
487130561Sobrien */
48884865Sobrientypedef enum {
489130561Sobrien	/**
490130561Sobrien	 * Index relative to all elements (overall and individual)
491130561Sobrien	 * in the system.
492130561Sobrien	 */
493130561Sobrien	SES_ELEM_INDEX_GLOBAL,
494130561Sobrien
495130561Sobrien	/**
496130561Sobrien	 * \brief Index relative to all individual elements in the system.
497130561Sobrien	 *
498130561Sobrien	 * This index counts only individual elements, skipping overall
499130561Sobrien	 * status elements.  This is the index space of the additional
500130561Sobrien	 * element status page (page 0xa).
501130561Sobrien	 */
502130561Sobrien	SES_ELEM_INDEX_INDIVIDUAL
503130561Sobrien} ses_elem_index_type_t;
504130561Sobrien
505130561Sobrien/**
506130561Sobrien * \brief Move the provided iterator forwards or backwards to the object
507130561Sobrien *        having the give index.
508130561Sobrien *
509130561Sobrien * \param iter           The iterator on which to perform the seek.
510130561Sobrien * \param element_index  The index of the element to find.
511130561Sobrien * \param index_type     The type (global or individual) of element_index.
512130561Sobrien *
513130561Sobrien * \return  If the element is found, a pointer to it's enc_element_t.
51484865Sobrien *          Otherwise NULL.
515130561Sobrien */
516130561Sobrienstatic enc_element_t *
517130561Sobrienses_iter_seek_to(struct ses_iterator *iter, int element_index,
518130561Sobrien		 ses_elem_index_type_t index_type)
519130561Sobrien{
520130561Sobrien	enc_element_t	*element;
521130561Sobrien	int		*cur_index;
522130561Sobrien
523130561Sobrien	if (index_type == SES_ELEM_INDEX_GLOBAL)
524130561Sobrien		cur_index = &iter->global_element_index;
52584865Sobrien	else
526130561Sobrien		cur_index = &iter->individual_element_index;
527130561Sobrien
528130561Sobrien	if (*cur_index == element_index) {
529130561Sobrien		/* Already there. */
530130561Sobrien		return (&iter->cache->elm_map[iter->global_element_index]);
531130561Sobrien	}
532130561Sobrien
533130561Sobrien	ses_iter_reset(iter);
534130561Sobrien	while ((element = ses_iter_next(iter)) != NULL
535130561Sobrien	    && *cur_index != element_index)
536130561Sobrien		;
537130561Sobrien
538130561Sobrien	if (*cur_index != element_index)
539130561Sobrien		return (NULL);
540130561Sobrien
541130561Sobrien	return (element);
542130561Sobrien}
543130561Sobrien
544130561Sobrien#if 0
545130561Sobrienstatic int ses_encode(enc_softc_t *, uint8_t *, int, int,
546130561Sobrien    struct ses_comstat *);
547130561Sobrien#endif
548130561Sobrienstatic int ses_set_timed_completion(enc_softc_t *, uint8_t);
549130561Sobrien#if 0
550130561Sobrienstatic int ses_putstatus(enc_softc_t *, int, struct ses_comstat *);
551130561Sobrien#endif
552130561Sobrien
553130561Sobrienstatic void ses_print_addl_data(enc_softc_t *, enc_element_t *);
554130561Sobrien
555130561Sobrien/*=========================== SES cleanup routines ===========================*/
55684865Sobrien
557130561Sobrienstatic void
558130561Sobrienses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
559130561Sobrien{
560130561Sobrien	ses_cache_t   *ses_cache;
561130561Sobrien	ses_cache_t   *other_ses_cache;
56284865Sobrien	enc_element_t *cur_elm;
563130561Sobrien	enc_element_t *last_elm;
564130561Sobrien
565130561Sobrien	ENC_DLOG(enc, "%s: enter\n", __func__);
566130561Sobrien	ses_cache = cache->private;
567130561Sobrien	if (ses_cache->elm_addlstatus_page == NULL)
568130561Sobrien		return;
569130561Sobrien
570130561Sobrien	for (cur_elm = cache->elm_map,
571130561Sobrien	     last_elm = &cache->elm_map[cache->nelms];
572130561Sobrien	     cur_elm != last_elm; cur_elm++) {
573130561Sobrien		ses_element_t *elmpriv;
574130561Sobrien
575130561Sobrien		elmpriv = cur_elm->elm_private;
576130561Sobrien
577130561Sobrien		/* Clear references to the additional status page. */
578130561Sobrien		bzero(&elmpriv->addl, sizeof(elmpriv->addl));
579130561Sobrien	}
580130561Sobrien
581130561Sobrien	other_ses_cache = enc_other_cache(enc, cache)->private;
582130561Sobrien	if (other_ses_cache->elm_addlstatus_page
583130561Sobrien	 != ses_cache->elm_addlstatus_page)
584130561Sobrien		ENC_FREE(ses_cache->elm_addlstatus_page);
585130561Sobrien	ses_cache->elm_addlstatus_page = NULL;
586130561Sobrien}
587130561Sobrien
588130561Sobrienstatic void
589130561Sobrienses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
590130561Sobrien{
591130561Sobrien	ses_cache_t   *ses_cache;
592130561Sobrien	ses_cache_t   *other_ses_cache;
59384865Sobrien	enc_element_t *cur_elm;
594130561Sobrien	enc_element_t *last_elm;
595130561Sobrien
596130561Sobrien	ENC_DLOG(enc, "%s: enter\n", __func__);
597130561Sobrien	ses_cache = cache->private;
598130561Sobrien	if (ses_cache->elm_descs_page == NULL)
599130561Sobrien		return;
600130561Sobrien
601130561Sobrien	for (cur_elm = cache->elm_map,
602130561Sobrien	     last_elm = &cache->elm_map[cache->nelms];
603130561Sobrien	     cur_elm != last_elm; cur_elm++) {
60484865Sobrien		ses_element_t *elmpriv;
605130561Sobrien
606130561Sobrien		elmpriv = cur_elm->elm_private;
607130561Sobrien		elmpriv->descr_len = 0;
608130561Sobrien		elmpriv->descr = NULL;
609130561Sobrien	}
610130561Sobrien
611130561Sobrien	other_ses_cache = enc_other_cache(enc, cache)->private;
612130561Sobrien	if (other_ses_cache->elm_descs_page
61384865Sobrien	 != ses_cache->elm_descs_page)
614130561Sobrien		ENC_FREE(ses_cache->elm_descs_page);
61584865Sobrien	ses_cache->elm_descs_page = NULL;
616130561Sobrien}
61784865Sobrien
61884865Sobrienstatic void
61984865Sobrienses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
62084865Sobrien{
62184865Sobrien	ses_cache_t *ses_cache;
62284865Sobrien	ses_cache_t *other_ses_cache;
62384865Sobrien
62484865Sobrien	ENC_DLOG(enc, "%s: enter\n", __func__);
62584865Sobrien	ses_cache   = cache->private;
62684865Sobrien	if (ses_cache->status_page == NULL)
62784865Sobrien		return;
62884865Sobrien
62984865Sobrien	other_ses_cache = enc_other_cache(enc, cache)->private;
63084865Sobrien	if (other_ses_cache->status_page != ses_cache->status_page)
63184865Sobrien		ENC_FREE(ses_cache->status_page);
63284865Sobrien	ses_cache->status_page = NULL;
63384865Sobrien}
63484865Sobrien
63584865Sobrienstatic void
63684865Sobrienses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
63784865Sobrien{
63884865Sobrien	enc_element_t *cur_elm;
63984865Sobrien	enc_element_t *last_elm;
64084865Sobrien
64184865Sobrien	ENC_DLOG(enc, "%s: enter\n", __func__);
64284865Sobrien	if (cache->elm_map == NULL)
64384865Sobrien		return;
64484865Sobrien
64584865Sobrien	ses_cache_free_elm_descs(enc, cache);
64684865Sobrien	ses_cache_free_elm_addlstatus(enc, cache);
64784865Sobrien	for (cur_elm = cache->elm_map,
64884865Sobrien	     last_elm = &cache->elm_map[cache->nelms];
64984865Sobrien	     cur_elm != last_elm; cur_elm++) {
65084865Sobrien
65184865Sobrien		ENC_FREE_AND_NULL(cur_elm->elm_private);
65284865Sobrien	}
65384865Sobrien	ENC_FREE_AND_NULL(cache->elm_map);
65484865Sobrien	cache->nelms = 0;
65584865Sobrien	ENC_DLOG(enc, "%s: exit\n", __func__);
656130561Sobrien}
657
658static void
659ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
660{
661	ses_cache_t *other_ses_cache;
662	ses_cache_t *ses_cache;
663
664	ENC_DLOG(enc, "%s: enter\n", __func__);
665	ses_cache_free_elm_addlstatus(enc, cache);
666	ses_cache_free_status(enc, cache);
667	ses_cache_free_elm_map(enc, cache);
668
669	ses_cache = cache->private;
670	ses_cache->ses_ntypes = 0;
671
672	other_ses_cache = enc_other_cache(enc, cache)->private;
673	if (other_ses_cache->subencs != ses_cache->subencs)
674		ENC_FREE(ses_cache->subencs);
675	ses_cache->subencs = NULL;
676
677	if (other_ses_cache->ses_types != ses_cache->ses_types)
678		ENC_FREE(ses_cache->ses_types);
679	ses_cache->ses_types = NULL;
680
681	if (other_ses_cache->cfg_page != ses_cache->cfg_page)
682		ENC_FREE(ses_cache->cfg_page);
683	ses_cache->cfg_page = NULL;
684
685	ENC_DLOG(enc, "%s: exit\n", __func__);
686}
687
688static void
689ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
690{
691	ses_cache_t   *dst_ses_cache;
692	ses_cache_t   *src_ses_cache;
693	enc_element_t *src_elm;
694	enc_element_t *dst_elm;
695	enc_element_t *last_elm;
696
697	ses_cache_free(enc, dst);
698	src_ses_cache = src->private;
699	dst_ses_cache = dst->private;
700
701	/*
702	 * The cloned enclosure cache and ses specific cache are
703	 * mostly identical to the source.
704	 */
705	*dst = *src;
706	*dst_ses_cache = *src_ses_cache;
707
708	/*
709	 * But the ses cache storage is still independent.  Restore
710	 * the pointer that was clobbered by the structure copy above.
711	 */
712	dst->private = dst_ses_cache;
713
714	/*
715	 * The element map is independent even though it starts out
716	 * pointing to the same constant page data.
717	 */
718	dst->elm_map = malloc(dst->nelms * sizeof(enc_element_t),
719	    M_SCSIENC, M_WAITOK);
720	memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
721	for (dst_elm = dst->elm_map, src_elm = src->elm_map,
722	     last_elm = &src->elm_map[src->nelms];
723	     src_elm != last_elm; src_elm++, dst_elm++) {
724
725		dst_elm->elm_private = malloc(sizeof(ses_element_t),
726		    M_SCSIENC, M_WAITOK);
727		memcpy(dst_elm->elm_private, src_elm->elm_private,
728		       sizeof(ses_element_t));
729	}
730}
731
732/* Structure accessors.  These are strongly typed to avoid errors. */
733
734int
735ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
736{
737	return ((obj)->base_hdr.byte1 >> 6);
738}
739int
740ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
741{
742	return ((hdr)->byte0 & 0xf);
743}
744int
745ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
746{
747	return ((hdr)->byte0 >> 4) & 0x1;
748}
749int
750ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
751{
752	return ((hdr)->byte0 >> 7);
753}
754int
755ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
756{
757	return ((hdr)->type0_noneip.byte1 & 0x1);
758}
759int
760ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
761{
762	return ((phy)->target_ports & 0x1);
763}
764int
765ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
766{
767	return ((phy)->target_ports >> 7);
768}
769int
770ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
771{
772	return (((phy)->byte0 >> 4) & 0x7);
773}
774
775/**
776 * \brief Verify that the cached configuration data in our softc
777 *        is valid for processing the page data corresponding to
778 *        the provided page header.
779 *
780 * \param ses_cache The SES cache to validate.
781 * \param gen_code  The 4 byte generation code from a SES diagnostic
782 *		    page header.
783 *
784 * \return  non-zero if true, 0 if false.
785 */
786static int
787ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
788{
789	uint32_t cache_gc;
790	uint32_t cur_gc;
791
792	if (ses_cache->cfg_page == NULL)
793		return (0);
794
795	cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
796	cur_gc   = scsi_4btoul(gen_code);
797	return (cache_gc == cur_gc);
798}
799
800/**
801 * Function signature for consumers of the ses_devids_iter() interface.
802 */
803typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
804				  struct scsi_vpd_id_descriptor *, void *);
805
806/**
807 * \brief Iterate over and create vpd device id records from the
808 *        additional element status data for elm, passing that data
809 *        to the provided callback.
810 *
811 * \param enc	        SES instance containing elm
812 * \param elm	        Element for which to extract device ID data.
813 * \param callback      The callback function to invoke on each generated
814 *                      device id descriptor for elm.
815 * \param callback_arg  Argument passed through to callback on each invocation.
816 */
817static void
818ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
819		ses_devid_callback_t *callback, void *callback_arg)
820{
821	ses_element_t           *elmpriv;
822	struct ses_addl_status *addl;
823	u_int                   i;
824	size_t			devid_record_size;
825
826	elmpriv = elm->elm_private;
827	addl = &(elmpriv->addl);
828
829	/*
830	 * Don't assume this object has additional status information, or
831	 * that it is a SAS device, or that it is a device slot device.
832	 */
833	if (addl->hdr == NULL || addl->proto_hdr.sas == NULL
834	 || addl->proto_data.sasdev_phys == NULL)
835		return;
836
837	devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
838			  + sizeof(struct scsi_vpd_id_naa_ieee_reg);
839	for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
840		uint8_t			       devid_buf[devid_record_size];
841		struct scsi_vpd_id_descriptor *devid;
842		uint8_t			      *phy_addr;
843
844		devid = (struct scsi_vpd_id_descriptor *)devid_buf;
845		phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
846		devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
847				     | SVPD_ID_CODESET_BINARY;
848		devid->id_type       = SVPD_ID_PIV
849				     | SVPD_ID_ASSOC_PORT
850				     | SVPD_ID_TYPE_NAA;
851		devid->reserved	     = 0;
852		devid->length	     = sizeof(struct scsi_vpd_id_naa_ieee_reg);
853		memcpy(devid->identifier, phy_addr, devid->length);
854
855		callback(enc, elm, devid, callback_arg);
856	}
857}
858
859/**
860 * Function signature for consumers of the ses_paths_iter() interface.
861 */
862typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
863				 struct cam_path *, void *);
864
865/**
866 * Argument package passed through ses_devids_iter() by
867 * ses_paths_iter() to ses_path_iter_devid_callback().
868 */
869typedef struct ses_path_iter_args {
870	ses_path_callback_t *callback;
871	void		    *callback_arg;
872} ses_path_iter_args_t;
873
874/**
875 * ses_devids_iter() callback function used by ses_paths_iter()
876 * to map device ids to peripheral driver instances.
877 *
878 * \param enc	  SES instance containing elm
879 * \param elm	  Element on which device ID matching is active.
880 * \param periph  A device ID corresponding to elm.
881 * \param arg     Argument passed through to callback on each invocation.
882 */
883static void
884ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
885			       struct scsi_vpd_id_descriptor *devid,
886			       void *arg)
887{
888	struct ccb_dev_match         cdm;
889	struct dev_match_pattern     match_pattern;
890	struct dev_match_result      match_result;
891	struct device_match_result  *device_match;
892	struct device_match_pattern *device_pattern;
893	ses_path_iter_args_t	    *args;
894
895	args = (ses_path_iter_args_t *)arg;
896	match_pattern.type = DEV_MATCH_DEVICE;
897	device_pattern = &match_pattern.pattern.device_pattern;
898	device_pattern->flags = DEV_MATCH_DEVID;
899	device_pattern->data.devid_pat.id_len =
900	    offsetof(struct scsi_vpd_id_descriptor, identifier)
901	  + devid->length;
902	memcpy(device_pattern->data.devid_pat.id, devid,
903	       device_pattern->data.devid_pat.id_len);
904
905	memset(&cdm, 0, sizeof(cdm));
906	if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
907			     CAM_XPT_PATH_ID,
908			     CAM_TARGET_WILDCARD,
909			     CAM_LUN_WILDCARD) != CAM_REQ_CMP)
910		return;
911
912	cdm.ccb_h.func_code = XPT_DEV_MATCH;
913	cdm.num_patterns    = 1;
914	cdm.patterns        = &match_pattern;
915	cdm.pattern_buf_len = sizeof(match_pattern);
916	cdm.match_buf_len   = sizeof(match_result);
917	cdm.matches         = &match_result;
918
919	xpt_action((union ccb *)&cdm);
920	xpt_free_path(cdm.ccb_h.path);
921
922	if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
923	 || (cdm.status != CAM_DEV_MATCH_LAST
924	  && cdm.status != CAM_DEV_MATCH_MORE)
925	 || cdm.num_matches == 0)
926		return;
927
928	device_match = &match_result.result.device_result;
929	if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
930			     device_match->path_id,
931			     device_match->target_id,
932			     device_match->target_lun) != CAM_REQ_CMP)
933		return;
934
935	args->callback(enc, elem, cdm.ccb_h.path, args->callback_arg);
936
937	xpt_free_path(cdm.ccb_h.path);
938}
939
940/**
941 * \brief Iterate over and find the matching periph objects for the
942 *        specified element.
943 *
944 * \param enc	        SES instance containing elm
945 * \param elm	        Element for which to perform periph object matching.
946 * \param callback      The callback function to invoke with each matching
947 *                      periph object.
948 * \param callback_arg  Argument passed through to callback on each invocation.
949 */
950static void
951ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
952	       ses_path_callback_t *callback, void *callback_arg)
953{
954	ses_path_iter_args_t args;
955
956	args.callback     = callback;
957	args.callback_arg = callback_arg;
958	ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
959}
960
961/**
962 * ses_paths_iter() callback function used by ses_get_elmdevname()
963 * to record periph driver instance strings corresponding to a SES
964 * element.
965 *
966 * \param enc	  SES instance containing elm
967 * \param elm	  Element on which periph matching is active.
968 * \param periph  A periph instance that matches elm.
969 * \param arg     Argument passed through to callback on each invocation.
970 */
971static void
972ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
973			struct cam_path *path, void *arg)
974{
975	struct sbuf *sb;
976
977	sb = (struct sbuf *)arg;
978	cam_periph_list(path, sb);
979}
980
981/**
982 * Argument package passed through ses_paths_iter() to
983 * ses_getcampath_callback.
984 */
985typedef struct ses_setphyspath_callback_args {
986	struct sbuf *physpath;
987	int          num_set;
988} ses_setphyspath_callback_args_t;
989
990/**
991 * \brief ses_paths_iter() callback to set the physical path on the
992 *        CAM EDT entries corresponding to a given SES element.
993 *
994 * \param enc	  SES instance containing elm
995 * \param elm	  Element on which periph matching is active.
996 * \param periph  A periph instance that matches elm.
997 * \param arg     Argument passed through to callback on each invocation.
998 */
999static void
1000ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1001			 struct cam_path *path, void *arg)
1002{
1003	struct ccb_dev_advinfo cdai;
1004	ses_setphyspath_callback_args_t *args;
1005	char *old_physpath;
1006
1007	args = (ses_setphyspath_callback_args_t *)arg;
1008	old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1009	cam_periph_lock(enc->periph);
1010	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1011	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1012	cdai.buftype = CDAI_TYPE_PHYS_PATH;
1013	cdai.flags = CDAI_FLAG_NONE;
1014	cdai.bufsiz = MAXPATHLEN;
1015	cdai.buf = old_physpath;
1016	xpt_action((union ccb *)&cdai);
1017	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1018		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1019
1020	if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1021
1022		xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1023		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1024		cdai.buftype = CDAI_TYPE_PHYS_PATH;
1025		cdai.flags = CDAI_FLAG_STORE;
1026		cdai.bufsiz = sbuf_len(args->physpath);
1027		cdai.buf = sbuf_data(args->physpath);
1028		xpt_action((union ccb *)&cdai);
1029		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1030			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1031		if (cdai.ccb_h.status == CAM_REQ_CMP)
1032			args->num_set++;
1033	}
1034	cam_periph_unlock(enc->periph);
1035	free(old_physpath, M_SCSIENC);
1036}
1037
1038/**
1039 * \brief Set a device's physical path string in CAM XPT.
1040 *
1041 * \param enc	SES instance containing elm
1042 * \param elm	Element to publish physical path string for
1043 * \param iter	Iterator whose state corresponds to elm
1044 *
1045 * \return	0 on success, errno otherwise.
1046 */
1047static int
1048ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1049		 struct ses_iterator *iter)
1050{
1051	struct ccb_dev_advinfo cdai;
1052	ses_setphyspath_callback_args_t args;
1053	int i, ret;
1054	struct sbuf sb;
1055	struct scsi_vpd_id_descriptor *idd;
1056	uint8_t *devid;
1057	ses_element_t *elmpriv;
1058	const char *c;
1059
1060	ret = EIO;
1061	devid = NULL;
1062
1063	/*
1064	 * Assemble the components of the physical path starting with
1065	 * the device ID of the enclosure itself.
1066	 */
1067	xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1068	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1069	cdai.flags = CDAI_FLAG_NONE;
1070	cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1071	cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1072	cdai.buf = devid = malloc(cdai.bufsiz, M_SCSIENC, M_WAITOK|M_ZERO);
1073	cam_periph_lock(enc->periph);
1074	xpt_action((union ccb *)&cdai);
1075	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1076		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1077	cam_periph_unlock(enc->periph);
1078	if (cdai.ccb_h.status != CAM_REQ_CMP)
1079		goto out;
1080
1081	idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1082	    cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1083	if (idd == NULL)
1084		goto out;
1085
1086	if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1087		ret = ENOMEM;
1088		goto out;
1089	}
1090	/* Next, generate the physical path string */
1091	sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1092	    scsi_8btou64(idd->identifier), iter->type_index,
1093	    iter->type_element_index);
1094	/* Append the element descriptor if one exists */
1095	elmpriv = elm->elm_private;
1096	if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1097		sbuf_cat(&sb, "/elmdesc@");
1098		for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1099		    i++, c++) {
1100			if (!isprint(*c) || isspace(*c) || *c == '/')
1101				sbuf_putc(&sb, '_');
1102			else
1103				sbuf_putc(&sb, *c);
1104		}
1105	}
1106	sbuf_finish(&sb);
1107
1108	/*
1109	 * Set this physical path on any CAM devices with a device ID
1110	 * descriptor that matches one created from the SES additional
1111	 * status data for this element.
1112	 */
1113	args.physpath= &sb;
1114	args.num_set = 0;
1115	ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1116	sbuf_delete(&sb);
1117
1118	ret = args.num_set == 0 ? ENOENT : 0;
1119
1120out:
1121	if (devid != NULL)
1122		ENC_FREE(devid);
1123	return (ret);
1124}
1125
1126/**
1127 * \brief Helper to set the CDB fields appropriately.
1128 *
1129 * \param cdb		Buffer containing the cdb.
1130 * \param pagenum	SES diagnostic page to query for.
1131 * \param dir		Direction of query.
1132 */
1133static void
1134ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1135{
1136
1137	/* Ref: SPC-4 r25 Section 6.20 Table 223 */
1138	if (dir == CAM_DIR_IN) {
1139		cdb[0] = RECEIVE_DIAGNOSTIC;
1140		cdb[1] = 1; /* Set page code valid bit */
1141		cdb[2] = pagenum;
1142	} else {
1143		cdb[0] = SEND_DIAGNOSTIC;
1144		cdb[1] = 0x10;
1145		cdb[2] = pagenum;
1146	}
1147	cdb[3] = bufsiz >> 8;	/* high bits */
1148	cdb[4] = bufsiz & 0xff;	/* low bits */
1149	cdb[5] = 0;
1150}
1151
1152/**
1153 * \brief Discover whether this instance supports timed completion of a
1154 * 	  RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1155 * 	  page, and store the result in the softc, updating if necessary.
1156 *
1157 * \param enc	SES instance to query and update.
1158 * \param tc_en	Value of timed completion to set (see \return).
1159 *
1160 * \return	1 if timed completion enabled, 0 otherwise.
1161 */
1162static int
1163ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1164{
1165	int err;
1166	union ccb *ccb;
1167	struct cam_periph *periph;
1168	struct ses_mgmt_mode_page *mgmt;
1169	uint8_t *mode_buf;
1170	size_t mode_buf_len;
1171	ses_softc_t *ses;
1172
1173	periph = enc->periph;
1174	ses = enc->enc_private;
1175	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1176
1177	mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1178	mode_buf = ENC_MALLOCZ(mode_buf_len);
1179	if (mode_buf == NULL)
1180		goto out;
1181
1182	scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1183	    /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1184	    mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1185
1186	/*
1187	 * Ignore illegal request errors, as they are quite common and we
1188	 * will print something out in that case anyway.
1189	 */
1190	err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1191	    ENC_FLAGS|SF_QUIET_IR, NULL);
1192	if (ccb->ccb_h.status != CAM_REQ_CMP) {
1193		ENC_VLOG(enc, "Timed Completion Unsupported\n");
1194		goto release;
1195	}
1196
1197	/* Skip the mode select if the desired value is already set */
1198	mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1199	if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1200		goto done;
1201
1202	/* Value is not what we wanted, set it */
1203	if (tc_en)
1204		mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1205	else
1206		mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1207	/* SES2r20: a completion time of zero means as long as possible */
1208	bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1209
1210	scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1211	    /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1212	    SSD_FULL_SIZE, /*timeout*/60 * 1000);
1213
1214	err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1215	if (ccb->ccb_h.status != CAM_REQ_CMP) {
1216		ENC_VLOG(enc, "Timed Completion Set Failed\n");
1217		goto release;
1218	}
1219
1220done:
1221	if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1222		ENC_LOG(enc, "Timed Completion Enabled\n");
1223		ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1224	} else {
1225		ENC_LOG(enc, "Timed Completion Disabled\n");
1226		ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1227	}
1228release:
1229	ENC_FREE(mode_buf);
1230	xpt_release_ccb(ccb);
1231out:
1232	return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1233}
1234
1235/**
1236 * \brief Process the list of supported pages and update flags.
1237 *
1238 * \param enc       SES device to query.
1239 * \param buf       Buffer containing the config page.
1240 * \param xfer_len  Length of the config page in the buffer.
1241 *
1242 * \return  0 on success, errno otherwise.
1243 */
1244static int
1245ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1246    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1247{
1248	ses_softc_t *ses;
1249	struct scsi_diag_page *page;
1250	int err, i, length;
1251
1252	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1253	    ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1254	ses = enc->enc_private;
1255	err = -1;
1256
1257	if (error != 0) {
1258		err = error;
1259		goto out;
1260	}
1261	if (xfer_len < sizeof(*page)) {
1262		ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1263		err = EIO;
1264		goto out;
1265	}
1266	page = (struct scsi_diag_page *)*bufp;
1267	length = scsi_2btoul(page->length);
1268	if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1269		ENC_VLOG(enc, "Diag Pages List Too Long\n");
1270		goto out;
1271	}
1272	ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1273		 __func__, length, xfer_len);
1274
1275	err = 0;
1276	for (i = 0; i < length; i++) {
1277		if (page->params[i] == SesElementDescriptor)
1278			ses->ses_flags |= SES_FLAG_DESC;
1279		else if (page->params[i] == SesAddlElementStatus)
1280			ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1281	}
1282
1283out:
1284	ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1285	return (err);
1286}
1287
1288/**
1289 * \brief Process the config page and update associated structures.
1290 *
1291 * \param enc       SES device to query.
1292 * \param buf       Buffer containing the config page.
1293 * \param xfer_len  Length of the config page in the buffer.
1294 *
1295 * \return  0 on success, errno otherwise.
1296 */
1297static int
1298ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1299    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1300{
1301	struct ses_iterator iter;
1302	ses_softc_t *ses;
1303	enc_cache_t *enc_cache;
1304	ses_cache_t *ses_cache;
1305	uint8_t *buf;
1306	int length;
1307	int err;
1308	int nelm;
1309	int ntype;
1310	struct ses_cfg_page *cfg_page;
1311	struct ses_enc_desc *buf_subenc;
1312	const struct ses_enc_desc **subencs;
1313	const struct ses_enc_desc **cur_subenc;
1314	const struct ses_enc_desc **last_subenc;
1315	ses_type_t *ses_types;
1316	ses_type_t *sestype;
1317	const struct ses_elm_type_desc *cur_buf_type;
1318	const struct ses_elm_type_desc *last_buf_type;
1319	uint8_t *last_valid_byte;
1320	enc_element_t *element;
1321	const char *type_text;
1322
1323	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1324	    ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1325	ses = enc->enc_private;
1326	enc_cache = &enc->enc_daemon_cache;
1327	ses_cache = enc_cache->private;
1328	buf = *bufp;
1329	err = -1;
1330
1331	if (error != 0) {
1332		err = error;
1333		goto out;
1334	}
1335	if (xfer_len < sizeof(cfg_page->hdr)) {
1336		ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1337		err = EIO;
1338		goto out;
1339	}
1340
1341	cfg_page = (struct ses_cfg_page *)buf;
1342	length = ses_page_length(&cfg_page->hdr);
1343	if (length > xfer_len) {
1344		ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1345		goto out;
1346	}
1347	last_valid_byte = &buf[length - 1];
1348
1349	ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1350		 __func__, length, xfer_len);
1351
1352	err = 0;
1353	if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1354
1355		/* Our cache is still valid.  Proceed to fetching status. */
1356		goto out;
1357	}
1358
1359	/* Cache is no longer valid.  Free old data to make way for new. */
1360	ses_cache_free(enc, enc_cache);
1361	ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1362	    scsi_4btoul(cfg_page->hdr.gen_code),
1363	    ses_cfg_page_get_num_subenc(cfg_page));
1364
1365	/* Take ownership of the buffer. */
1366	ses_cache->cfg_page = cfg_page;
1367	*bufp = NULL;
1368
1369	/*
1370	 * Now waltz through all the subenclosures summing the number of
1371	 * types available in each.
1372	 */
1373	subencs = malloc(ses_cfg_page_get_num_subenc(cfg_page)
1374	    * sizeof(*subencs), M_SCSIENC, M_WAITOK|M_ZERO);
1375	/*
1376	 * Sub-enclosure data is const after construction (i.e. when
1377	 * accessed via our cache object.
1378	 *
1379	 * The cast here is not required in C++ but C99 is not so
1380	 * sophisticated (see C99 6.5.16.1(1)).
1381	 */
1382	ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1383	ses_cache->subencs = subencs;
1384
1385	buf_subenc = cfg_page->subencs;
1386	cur_subenc = subencs;
1387	last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1388	ntype = 0;
1389	while (cur_subenc <= last_subenc) {
1390
1391		if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1392			ENC_VLOG(enc, "Enclosure %d Beyond End of "
1393			    "Descriptors\n", cur_subenc - subencs);
1394			err = EIO;
1395			goto out;
1396		}
1397
1398		ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1399		    "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1400		    buf_subenc->num_types, buf_subenc->length,
1401		    &buf_subenc->byte0 - buf);
1402		ENC_VLOG(enc, "WWN: %jx\n",
1403		    (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1404
1405		ntype += buf_subenc->num_types;
1406		*cur_subenc = buf_subenc;
1407		cur_subenc++;
1408		buf_subenc = ses_enc_desc_next(buf_subenc);
1409	}
1410
1411	/* Process the type headers. */
1412	ses_types = malloc(ntype * sizeof(*ses_types),
1413	    M_SCSIENC, M_WAITOK|M_ZERO);
1414	/*
1415	 * Type data is const after construction (i.e. when accessed via
1416	 * our cache object.
1417	 */
1418	ses_cache->ses_ntypes = ntype;
1419	ses_cache->ses_types = ses_types;
1420
1421	cur_buf_type = (const struct ses_elm_type_desc *)
1422	    (&(*last_subenc)->length + (*last_subenc)->length + 1);
1423	last_buf_type = cur_buf_type + ntype - 1;
1424	type_text = (const uint8_t *)(last_buf_type + 1);
1425	nelm = 0;
1426	sestype = ses_types;
1427	while (cur_buf_type <= last_buf_type) {
1428		if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1429			ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1430			    sestype - ses_types);
1431			err = EIO;
1432			goto out;
1433		}
1434		sestype->hdr  = cur_buf_type;
1435		sestype->text = type_text;
1436		type_text += cur_buf_type->etype_txt_len;
1437		ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1438		    "%d, Text Length %d: %.*s\n", sestype - ses_types,
1439		    sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1440		    sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1441		    sestype->hdr->etype_txt_len, sestype->text);
1442
1443		nelm += sestype->hdr->etype_maxelt
1444		      + /*overall status element*/1;
1445		sestype++;
1446		cur_buf_type++;
1447	}
1448
1449	/* Create the object map. */
1450	enc_cache->elm_map = malloc(nelm * sizeof(enc_element_t),
1451	    M_SCSIENC, M_WAITOK|M_ZERO);
1452	enc_cache->nelms = nelm;
1453
1454	ses_iter_init(enc, enc_cache, &iter);
1455	while ((element = ses_iter_next(&iter)) != NULL) {
1456		const struct ses_elm_type_desc *thdr;
1457
1458		ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1459		    iter.global_element_index, iter.type_index, nelm,
1460		    iter.type_element_index);
1461		thdr = ses_cache->ses_types[iter.type_index].hdr;
1462		element->subenclosure = thdr->etype_subenc;
1463		element->enctype = thdr->etype_elm_type;
1464		element->overall_status_elem = iter.type_element_index == 0;
1465		element->elm_private = malloc(sizeof(ses_element_t),
1466		    M_SCSIENC, M_WAITOK|M_ZERO);
1467		ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1468		    "type 0x%x\n", __func__, iter.global_element_index,
1469		    iter.type_index, iter.type_element_index,
1470		    thdr->etype_subenc, thdr->etype_elm_type);
1471	}
1472
1473	err = 0;
1474
1475out:
1476	if (err)
1477		ses_cache_free(enc, enc_cache);
1478	else {
1479		enc_update_request(enc, SES_UPDATE_GETSTATUS);
1480		if (ses->ses_flags & SES_FLAG_DESC)
1481			enc_update_request(enc, SES_UPDATE_GETELMDESCS);
1482		if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
1483			enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
1484		enc_update_request(enc, SES_PUBLISH_CACHE);
1485	}
1486	ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1487	return (err);
1488}
1489
1490/**
1491 * \brief Update the status page and associated structures.
1492 *
1493 * \param enc   SES softc to update for.
1494 * \param buf   Buffer containing the status page.
1495 * \param bufsz	Amount of data in the buffer.
1496 *
1497 * \return	0 on success, errno otherwise.
1498 */
1499static int
1500ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1501    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1502{
1503	struct ses_iterator iter;
1504	enc_element_t *element;
1505	ses_softc_t *ses;
1506	enc_cache_t *enc_cache;
1507	ses_cache_t *ses_cache;
1508	uint8_t *buf;
1509	int err = -1;
1510	int length;
1511	struct ses_status_page *page;
1512	union ses_status_element *cur_stat;
1513	union ses_status_element *last_stat;
1514
1515	ses = enc->enc_private;
1516	enc_cache = &enc->enc_daemon_cache;
1517	ses_cache = enc_cache->private;
1518	buf = *bufp;
1519
1520	ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1521	page = (struct ses_status_page *)buf;
1522	length = ses_page_length(&page->hdr);
1523
1524	if (error != 0) {
1525		err = error;
1526		goto out;
1527	}
1528	/*
1529	 * Make sure the length fits in the buffer.
1530	 *
1531	 * XXX all this means is that the page is larger than the space
1532	 * we allocated.  Since we use a statically sized buffer, this
1533	 * could happen... Need to use dynamic discovery of the size.
1534	 */
1535	if (length > xfer_len) {
1536		ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1537		goto out;
1538	}
1539
1540	/* Check for simple enclosure reporting short enclosure status. */
1541	if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1542		ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1543		ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1544		ses_cache_free(enc, enc_cache);
1545		enc_cache->enc_status = page->hdr.page_specific_flags;
1546		enc_update_request(enc, SES_PUBLISH_CACHE);
1547		err = 0;
1548		goto out;
1549	}
1550
1551	/* Make sure the length contains at least one header and status */
1552	if (length < (sizeof(*page) + sizeof(*page->elements))) {
1553		ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1554		goto out;
1555	}
1556
1557	if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1558		ENC_DLOG(enc, "%s: Generation count change detected\n",
1559		    __func__);
1560		enc_update_request(enc, SES_UPDATE_GETCONFIG);
1561		goto out;
1562	}
1563
1564	ses_cache_free_status(enc, enc_cache);
1565	ses_cache->status_page = page;
1566	*bufp = NULL;
1567
1568	enc_cache->enc_status = page->hdr.page_specific_flags;
1569
1570	/*
1571	 * Read in individual element status.  The element order
1572	 * matches the order reported in the config page (i.e. the
1573	 * order of an unfiltered iteration of the config objects)..
1574	 */
1575	ses_iter_init(enc, enc_cache, &iter);
1576	cur_stat  = page->elements;
1577	last_stat = (union ses_status_element *)
1578	    &buf[length - sizeof(*last_stat)];
1579	ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1580		__func__, length, xfer_len);
1581	while (cur_stat <= last_stat
1582	    && (element = ses_iter_next(&iter)) != NULL) {
1583
1584		ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1585		    __func__, iter.global_element_index, iter.type_index,
1586		    iter.type_element_index, (uint8_t *)cur_stat - buf,
1587		    scsi_4btoul(cur_stat->bytes));
1588
1589		memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1590		element->svalid = 1;
1591		cur_stat++;
1592	}
1593
1594	if (ses_iter_next(&iter) != NULL) {
1595		ENC_VLOG(enc, "Status page, length insufficient for "
1596			"expected number of objects\n");
1597	} else {
1598		if (cur_stat <= last_stat)
1599			ENC_VLOG(enc, "Status page, exhausted objects before "
1600				"exhausing page\n");
1601		enc_update_request(enc, SES_PUBLISH_CACHE);
1602		err = 0;
1603	}
1604out:
1605	ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1606	return (err);
1607}
1608
1609typedef enum {
1610	/**
1611	 * The enclosure should not provide additional element
1612	 * status for this element type in page 0x0A.
1613	 *
1614	 * \note  This status is returned for any types not
1615	 *        listed SES3r02.  Further types added in a
1616	 *        future specification will be incorrectly
1617	 *        classified.
1618	 */
1619	TYPE_ADDLSTATUS_NONE,
1620
1621	/**
1622	 * The element type provides additional element status
1623	 * in page 0x0A.
1624	 */
1625	TYPE_ADDLSTATUS_MANDATORY,
1626
1627	/**
1628	 * The element type may provide additional element status
1629	 * in page 0x0A, but i
1630	 */
1631	TYPE_ADDLSTATUS_OPTIONAL
1632} ses_addlstatus_avail_t;
1633
1634/**
1635 * \brief Check to see whether a given type (as obtained via type headers) is
1636 *	  supported by the additional status command.
1637 *
1638 * \param enc     SES softc to check.
1639 * \param typidx  Type index to check for.
1640 *
1641 * \return  An enumeration indicating if additional status is mandatory,
1642 *          optional, or not required for this type.
1643 */
1644static ses_addlstatus_avail_t
1645ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1646{
1647	enc_cache_t *enc_cache;
1648	ses_cache_t *ses_cache;
1649
1650	enc_cache = &enc->enc_daemon_cache;
1651	ses_cache = enc_cache->private;
1652	switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1653	case ELMTYP_DEVICE:
1654	case ELMTYP_ARRAY_DEV:
1655	case ELMTYP_SAS_EXP:
1656		return (TYPE_ADDLSTATUS_MANDATORY);
1657	case ELMTYP_SCSI_INI:
1658	case ELMTYP_SCSI_TGT:
1659	case ELMTYP_ESCC:
1660		return (TYPE_ADDLSTATUS_OPTIONAL);
1661	default:
1662		/* No additional status information available. */
1663		break;
1664	}
1665	return (TYPE_ADDLSTATUS_NONE);
1666}
1667
1668static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1669				     uint8_t *, int);
1670static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1671				      int, int, int, int);
1672
1673/**
1674 * \brief Parse the additional status element data for each object.
1675 *
1676 * \param enc       The SES softc to update.
1677 * \param buf       The buffer containing the additional status
1678 *                  element response.
1679 * \param xfer_len  Size of the buffer.
1680 *
1681 * \return  0 on success, errno otherwise.
1682 */
1683static int
1684ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1685    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1686{
1687	struct ses_iterator iter, titer;
1688	int eip;
1689	int err;
1690	int ignore_index = 0;
1691	int length;
1692	int offset;
1693	enc_cache_t *enc_cache;
1694	ses_cache_t *ses_cache;
1695	uint8_t *buf;
1696	ses_element_t *elmpriv;
1697	const struct ses_page_hdr *hdr;
1698	enc_element_t *element, *telement;
1699
1700	enc_cache = &enc->enc_daemon_cache;
1701	ses_cache = enc_cache->private;
1702	buf = *bufp;
1703	err = -1;
1704
1705	if (error != 0) {
1706		err = error;
1707		goto out;
1708	}
1709	ses_cache_free_elm_addlstatus(enc, enc_cache);
1710	ses_cache->elm_addlstatus_page =
1711	    (struct ses_addl_elem_status_page *)buf;
1712	*bufp = NULL;
1713
1714	/*
1715	 * The objects appear in the same order here as in Enclosure Status,
1716	 * which itself is ordered by the Type Descriptors from the Config
1717	 * page.  However, it is necessary to skip elements that are not
1718	 * supported by this page when counting them.
1719	 */
1720	hdr = &ses_cache->elm_addlstatus_page->hdr;
1721	length = ses_page_length(hdr);
1722	ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1723	/* Make sure the length includes at least one header. */
1724	if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1725		ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1726		goto out;
1727	}
1728	if (length > xfer_len) {
1729		ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1730		goto out;
1731	}
1732
1733	if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1734		ENC_DLOG(enc, "%s: Generation count change detected\n",
1735		    __func__);
1736		enc_update_request(enc, SES_UPDATE_GETCONFIG);
1737		goto out;
1738	}
1739
1740	offset = sizeof(struct ses_page_hdr);
1741	ses_iter_init(enc, enc_cache, &iter);
1742	while (offset < length
1743	    && (element = ses_iter_next(&iter)) != NULL) {
1744		struct ses_elm_addlstatus_base_hdr *elm_hdr;
1745		int proto_info_len;
1746		ses_addlstatus_avail_t status_type;
1747
1748		/*
1749		 * Additional element status is only provided for
1750		 * individual elements (i.e. overal status elements
1751		 * are excluded) and those of the types specified
1752		 * in the SES spec.
1753		 */
1754		status_type = ses_typehasaddlstatus(enc, iter.type_index);
1755		if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1756		 || status_type == TYPE_ADDLSTATUS_NONE)
1757			continue;
1758
1759		elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1760		eip = ses_elm_addlstatus_eip(elm_hdr);
1761		if (eip && !ignore_index) {
1762			struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1763			int expected_index, index;
1764			ses_elem_index_type_t index_type;
1765
1766			eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1767			if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) {
1768				index_type = SES_ELEM_INDEX_GLOBAL;
1769				expected_index = iter.global_element_index;
1770			} else {
1771				index_type = SES_ELEM_INDEX_INDIVIDUAL;
1772				expected_index = iter.individual_element_index;
1773			}
1774			titer = iter;
1775			telement = ses_iter_seek_to(&titer,
1776			    eip_hdr->element_index, index_type);
1777			if (telement != NULL &&
1778			    (ses_typehasaddlstatus(enc, titer.type_index) !=
1779			     TYPE_ADDLSTATUS_NONE ||
1780			     titer.type_index > ELMTYP_SAS_CONN)) {
1781				iter = titer;
1782				element = telement;
1783			} else
1784				ignore_index = 1;
1785
1786			if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE)
1787				index = iter.global_element_index;
1788			else
1789				index = iter.individual_element_index;
1790			if (index > expected_index
1791			 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1792				ENC_VLOG(enc, "%s: provided %s element"
1793					"index %d skips mandatory status "
1794					" element at index %d\n",
1795					__func__, (eip_hdr->byte2 &
1796					SES_ADDL_EIP_EIIOE) ? "global " : "",
1797					index, expected_index);
1798			}
1799		}
1800		elmpriv = element->elm_private;
1801		elmpriv->addl.hdr = elm_hdr;
1802		ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1803		    "type element index=%d, offset=0x%x, "
1804		    "byte0=0x%x, length=0x%x\n", __func__,
1805		    iter.global_element_index, iter.type_index,
1806		    iter.type_element_index, offset, elmpriv->addl.hdr->byte0,
1807		    elmpriv->addl.hdr->length);
1808
1809		/* Skip to after the length field */
1810		offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1811
1812		/* Make sure the descriptor is within bounds */
1813		if ((offset + elmpriv->addl.hdr->length) > length) {
1814			ENC_VLOG(enc, "Element %d Beyond End "
1815			    "of Additional Element Status Descriptors\n",
1816			    iter.global_element_index);
1817			break;
1818		}
1819
1820		/* Advance to the protocol data, skipping eip bytes if needed */
1821		offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1822		proto_info_len = elmpriv->addl.hdr->length
1823			       - (eip * SES_EIP_HDR_EXTRA_LEN);
1824
1825		/* Errors in this block are ignored as they are non-fatal */
1826		switch(ses_elm_addlstatus_proto(elmpriv->addl.hdr)) {
1827		case SPSP_PROTO_FC:
1828			if (elmpriv->addl.hdr->length == 0)
1829				break;
1830			ses_get_elm_addlstatus_fc(enc, enc_cache,
1831						  &buf[offset], proto_info_len);
1832			break;
1833		case SPSP_PROTO_SAS:
1834			if (elmpriv->addl.hdr->length <= 2)
1835				break;
1836			ses_get_elm_addlstatus_sas(enc, enc_cache,
1837						   &buf[offset],
1838						   proto_info_len,
1839						   eip, iter.type_index,
1840						   iter.global_element_index);
1841			break;
1842		default:
1843			ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1844			    "Protocol 0x%x\n", iter.global_element_index,
1845			    ses_elm_addlstatus_proto(elmpriv->addl.hdr));
1846			break;
1847		}
1848
1849		offset += proto_info_len;
1850	}
1851	err = 0;
1852out:
1853	if (err)
1854		ses_cache_free_elm_addlstatus(enc, enc_cache);
1855	enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1856	enc_update_request(enc, SES_PUBLISH_CACHE);
1857	return (err);
1858}
1859
1860static int
1861ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1862    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1863{
1864	ses_softc_t *ses;
1865
1866	ses = enc->enc_private;
1867	/*
1868	 * Possible errors:
1869	 *  o Generation count wrong.
1870	 *  o Some SCSI status error.
1871	 */
1872	ses_terminate_control_requests(&ses->ses_pending_requests, error);
1873	enc_update_request(enc, SES_UPDATE_GETSTATUS);
1874	return (0);
1875}
1876
1877static int
1878ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1879    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1880{
1881	struct ses_iterator iter;
1882	enc_cache_t *enc_cache;
1883	ses_cache_t *ses_cache;
1884	enc_element_t *element;
1885
1886	enc_cache = &enc->enc_daemon_cache;
1887	ses_cache = enc_cache->private;
1888
1889	ses_iter_init(enc, enc_cache, &iter);
1890	while ((element = ses_iter_next(&iter)) != NULL) {
1891		/*
1892		 * ses_set_physpath() returns success if we changed
1893		 * the physpath of any element.  This allows us to
1894		 * only announce devices once regardless of how
1895		 * many times we process additional element status.
1896		 */
1897		if (ses_set_physpath(enc, element, &iter) == 0)
1898			ses_print_addl_data(enc, element);
1899	}
1900
1901	return (0);
1902}
1903
1904static int
1905ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1906    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1907{
1908
1909	sx_xlock(&enc->enc_cache_lock);
1910	ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1911			/*dst*/&enc->enc_cache);
1912	sx_xunlock(&enc->enc_cache_lock);
1913
1914	return (0);
1915}
1916
1917/**
1918 * \brief Parse the descriptors for each object.
1919 *
1920 * \param enc       The SES softc to update.
1921 * \param buf       The buffer containing the descriptor list response.
1922 * \param xfer_len  Size of the buffer.
1923 *
1924 * \return	0 on success, errno otherwise.
1925 */
1926static int
1927ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1928    union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1929{
1930	ses_softc_t *ses;
1931	struct ses_iterator iter;
1932	enc_element_t *element;
1933	int err;
1934	int offset;
1935	u_long length, plength;
1936	enc_cache_t *enc_cache;
1937	ses_cache_t *ses_cache;
1938	uint8_t *buf;
1939	ses_element_t *elmpriv;
1940	const struct ses_page_hdr *phdr;
1941	const struct ses_elm_desc_hdr *hdr;
1942
1943	ses = enc->enc_private;
1944	enc_cache = &enc->enc_daemon_cache;
1945	ses_cache = enc_cache->private;
1946	buf = *bufp;
1947	err = -1;
1948
1949	if (error != 0) {
1950		err = error;
1951		goto out;
1952	}
1953	ses_cache_free_elm_descs(enc, enc_cache);
1954	ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
1955	*bufp = NULL;
1956
1957	phdr = &ses_cache->elm_descs_page->hdr;
1958	plength = ses_page_length(phdr);
1959	if (xfer_len < sizeof(struct ses_page_hdr)) {
1960		ENC_VLOG(enc, "Runt Element Descriptor Page\n");
1961		goto out;
1962	}
1963	if (plength > xfer_len) {
1964		ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
1965		goto out;
1966	}
1967
1968	if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
1969		ENC_VLOG(enc, "%s: Generation count change detected\n",
1970		    __func__);
1971		enc_update_request(enc, SES_UPDATE_GETCONFIG);
1972		goto out;
1973	}
1974
1975	offset = sizeof(struct ses_page_hdr);
1976
1977	ses_iter_init(enc, enc_cache, &iter);
1978	while (offset < plength
1979	    && (element = ses_iter_next(&iter)) != NULL) {
1980
1981		if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
1982			ENC_VLOG(enc, "Element %d Descriptor Header Past "
1983			    "End of Buffer\n", iter.global_element_index);
1984			goto out;
1985		}
1986		hdr = (struct ses_elm_desc_hdr *)&buf[offset];
1987		length = scsi_2btoul(hdr->length);
1988		ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
1989		    iter.global_element_index, iter.type_index,
1990		    iter.type_element_index, length, offset);
1991		if ((offset + sizeof(*hdr) + length) > plength) {
1992			ENC_VLOG(enc, "Element%d Descriptor Past "
1993			    "End of Buffer\n", iter.global_element_index);
1994			goto out;
1995		}
1996		offset += sizeof(*hdr);
1997
1998		if (length > 0) {
1999			elmpriv = element->elm_private;
2000			elmpriv->descr_len = length;
2001			elmpriv->descr = &buf[offset];
2002		}
2003
2004		/* skip over the descriptor itself */
2005		offset += length;
2006	}
2007
2008	err = 0;
2009out:
2010	if (err == 0) {
2011		if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2012			enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2013	}
2014	enc_update_request(enc, SES_PUBLISH_CACHE);
2015	return (err);
2016}
2017
2018static int
2019ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2020		       union ccb *ccb, uint8_t *buf)
2021{
2022
2023	if (enc->enc_type == ENC_SEMB_SES) {
2024		semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2025					NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2026					state->page_code, buf, state->buf_size,
2027					state->timeout);
2028	} else {
2029		scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2030					NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2031					state->page_code, buf, state->buf_size,
2032					SSD_FULL_SIZE, state->timeout);
2033	}
2034	return (0);
2035}
2036
2037/**
2038 * \brief Encode the object status into the response buffer, which is
2039 *	  expected to contain the current enclosure status.  This function
2040 *	  turns off all the 'select' bits for the objects except for the
2041 *	  object specified, then sends it back to the enclosure.
2042 *
2043 * \param enc	SES enclosure the change is being applied to.
2044 * \param buf	Buffer containing the current enclosure status response.
2045 * \param amt	Length of the response in the buffer.
2046 * \param req	The control request to be applied to buf.
2047 *
2048 * \return	0 on success, errno otherwise.
2049 */
2050static int
2051ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2052{
2053	struct ses_iterator iter;
2054	enc_element_t *element;
2055	int offset;
2056	struct ses_control_page_hdr *hdr;
2057
2058	ses_iter_init(enc, &enc->enc_cache, &iter);
2059	hdr = (struct ses_control_page_hdr *)buf;
2060	if (req->elm_idx == -1) {
2061		/* for enclosure status, at least 2 bytes are needed */
2062		if (amt < 2)
2063			return EIO;
2064		hdr->control_flags =
2065		    req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2066		ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2067		return (0);
2068	}
2069
2070	element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2071	if (element == NULL)
2072		return (ENXIO);
2073
2074	/*
2075	 * Seek to the type set that corresponds to the requested object.
2076	 * The +1 is for the overall status element for the type.
2077	 */
2078	offset = sizeof(struct ses_control_page_hdr)
2079	       + (iter.global_element_index * sizeof(struct ses_comstat));
2080
2081	/* Check for buffer overflow. */
2082	if (offset + sizeof(struct ses_comstat) > amt)
2083		return (EIO);
2084
2085	/* Set the status. */
2086	memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2087
2088	ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2089	    iter.type_index, iter.global_element_index, offset,
2090	    req->elm_stat.comstatus, req->elm_stat.comstat[0],
2091	    req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2092
2093	return (0);
2094}
2095
2096static int
2097ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2098			 union ccb *ccb, uint8_t *buf)
2099{
2100	ses_softc_t			*ses;
2101	enc_cache_t			*enc_cache;
2102	ses_cache_t			*ses_cache;
2103	struct ses_control_page_hdr	*hdr;
2104	ses_control_request_t		*req;
2105	size_t				 plength;
2106	size_t				 offset;
2107
2108	ses = enc->enc_private;
2109	enc_cache = &enc->enc_daemon_cache;
2110	ses_cache = enc_cache->private;
2111	hdr = (struct ses_control_page_hdr *)buf;
2112
2113	if (ses_cache->status_page == NULL) {
2114		ses_terminate_control_requests(&ses->ses_requests, EIO);
2115		return (EIO);
2116	}
2117
2118	plength = ses_page_length(&ses_cache->status_page->hdr);
2119	memcpy(buf, ses_cache->status_page, plength);
2120
2121	/* Disable the select bits in all status entries.  */
2122	offset = sizeof(struct ses_control_page_hdr);
2123	for (offset = sizeof(struct ses_control_page_hdr);
2124	     offset < plength; offset += sizeof(struct ses_comstat)) {
2125		buf[offset] &= ~SESCTL_CSEL;
2126	}
2127
2128	/* And make sure the INVOP bit is clear.  */
2129	hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2130
2131	/* Apply incoming requests. */
2132	while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2133
2134		TAILQ_REMOVE(&ses->ses_requests, req, links);
2135		req->result = ses_encode(enc, buf, plength, req);
2136		if (req->result != 0) {
2137			wakeup(req);
2138			continue;
2139		}
2140		TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2141	}
2142
2143	if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2144		return (ENOENT);
2145
2146	/* Fill out the ccb */
2147	if (enc->enc_type == ENC_SEMB_SES) {
2148		semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2149			     MSG_SIMPLE_Q_TAG,
2150			     buf, ses_page_length(&ses_cache->status_page->hdr),
2151			     state->timeout);
2152	} else {
2153		scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2154			     MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2155			     /*device_offline*/0, /*self_test*/0,
2156			     /*page_format*/1, /*self_test_code*/0,
2157			     buf, ses_page_length(&ses_cache->status_page->hdr),
2158			     SSD_FULL_SIZE, state->timeout);
2159	}
2160	return (0);
2161}
2162
2163static int
2164ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2165			  uint8_t *buf, int bufsiz)
2166{
2167	ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2168	return (ENODEV);
2169}
2170
2171#define	SES_PRINT_PORTS(p, type) do {					\
2172	sbuf_printf(sbp, " %s(", type);					\
2173	if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) == 0)			\
2174		sbuf_printf(sbp, " None");				\
2175	else {								\
2176		if ((p) & SES_SASOBJ_DEV_PHY_SMP)			\
2177			sbuf_printf(sbp, " SMP");			\
2178		if ((p) & SES_SASOBJ_DEV_PHY_STP)			\
2179			sbuf_printf(sbp, " STP");			\
2180		if ((p) & SES_SASOBJ_DEV_PHY_SSP)			\
2181			sbuf_printf(sbp, " SSP");			\
2182	}								\
2183	sbuf_printf(sbp, " )");						\
2184} while(0)
2185
2186/**
2187 * \brief Print the additional element status data for this object, for SAS
2188 * 	  type 0 objects.  See SES2 r20 Section 6.1.13.3.2.
2189 *
2190 * \param sesname	SES device name associated with the object.
2191 * \param sbp		Sbuf to print to.
2192 * \param obj		The object to print the data for.
2193 * \param periph_name	Peripheral string associated with the object.
2194 */
2195static void
2196ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2197			      enc_element_t *obj, char *periph_name)
2198{
2199	int i;
2200	ses_element_t *elmpriv;
2201	struct ses_addl_status *addl;
2202	struct ses_elm_sas_device_phy *phy;
2203
2204	elmpriv = obj->elm_private;
2205	addl = &(elmpriv->addl);
2206	if (addl->proto_hdr.sas == NULL)
2207		return;
2208	sbuf_printf(sbp, "%s: %s: SAS Device Slot Element:",
2209	    sesname, periph_name);
2210	sbuf_printf(sbp, " %d Phys", addl->proto_hdr.sas->base_hdr.num_phys);
2211	if (ses_elm_addlstatus_eip(addl->hdr))
2212		sbuf_printf(sbp, " at Slot %d",
2213		    addl->proto_hdr.sas->type0_eip.dev_slot_num);
2214	if (ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas))
2215		sbuf_printf(sbp, ", Not All Phys");
2216	sbuf_printf(sbp, "\n");
2217	if (addl->proto_data.sasdev_phys == NULL)
2218		return;
2219	for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2220		phy = &addl->proto_data.sasdev_phys[i];
2221		sbuf_printf(sbp, "%s:  phy %d:", sesname, i);
2222		if (ses_elm_sas_dev_phy_sata_dev(phy))
2223			/* Spec says all other fields are specific values */
2224			sbuf_printf(sbp, " SATA device\n");
2225		else {
2226			sbuf_printf(sbp, " SAS device type %d id %d\n",
2227			    ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2228			sbuf_printf(sbp, "%s:  phy %d: protocols:", sesname, i);
2229			SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2230			SES_PRINT_PORTS(phy->target_ports, "Target");
2231			sbuf_printf(sbp, "\n");
2232		}
2233		sbuf_printf(sbp, "%s:  phy %d: parent %jx addr %jx\n",
2234		    sesname, i,
2235		    (uintmax_t)scsi_8btou64(phy->parent_addr),
2236		    (uintmax_t)scsi_8btou64(phy->phy_addr));
2237	}
2238}
2239#undef SES_PRINT_PORTS
2240
2241/**
2242 * \brief Report whether a given enclosure object is an expander.
2243 *
2244 * \param enc	SES softc associated with object.
2245 * \param obj	Enclosure object to report for.
2246 *
2247 * \return	1 if true, 0 otherwise.
2248 */
2249static int
2250ses_obj_is_expander(enc_softc_t *enc, enc_element_t *obj)
2251{
2252	return (obj->enctype == ELMTYP_SAS_EXP);
2253}
2254
2255/**
2256 * \brief Print the additional element status data for this object, for SAS
2257 *	  type 1 objects.  See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2258 *
2259 * \param enc		SES enclosure, needed for type identification.
2260 * \param sesname	SES device name associated with the object.
2261 * \param sbp		Sbuf to print to.
2262 * \param obj		The object to print the data for.
2263 * \param periph_name	Peripheral string associated with the object.
2264 */
2265static void
2266ses_print_addl_data_sas_type1(enc_softc_t *enc, char *sesname,
2267    struct sbuf *sbp, enc_element_t *obj, char *periph_name)
2268{
2269	int i, num_phys;
2270	ses_element_t *elmpriv;
2271	struct ses_addl_status *addl;
2272	struct ses_elm_sas_expander_phy *exp_phy;
2273	struct ses_elm_sas_port_phy *port_phy;
2274
2275	elmpriv = obj->elm_private;
2276	addl = &(elmpriv->addl);
2277	if (addl->proto_hdr.sas == NULL)
2278		return;
2279	sbuf_printf(sbp, "%s: %s: SAS ", sesname, periph_name);
2280	if (ses_obj_is_expander(enc, obj)) {
2281		num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2282		sbuf_printf(sbp, "Expander: %d Phys", num_phys);
2283		if (addl->proto_data.sasexp_phys == NULL)
2284			return;
2285		for (i = 0;i < num_phys;i++) {
2286			exp_phy = &addl->proto_data.sasexp_phys[i];
2287			sbuf_printf(sbp, "%s:  phy %d: connector %d other %d\n",
2288			    sesname, i, exp_phy->connector_index,
2289			    exp_phy->other_index);
2290		}
2291	} else {
2292		num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2293		sbuf_printf(sbp, "Port: %d Phys", num_phys);
2294		if (addl->proto_data.sasport_phys == NULL)
2295			return;
2296		for (i = 0;i < num_phys;i++) {
2297			port_phy = &addl->proto_data.sasport_phys[i];
2298			sbuf_printf(sbp,
2299			    "%s:  phy %d: id %d connector %d other %d\n",
2300			    sesname, i, port_phy->phy_id,
2301			    port_phy->connector_index, port_phy->other_index);
2302			sbuf_printf(sbp, "%s:  phy %d: addr %jx\n", sesname, i,
2303			    (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2304		}
2305	}
2306}
2307
2308/**
2309 * \brief Print the additional element status data for this object.
2310 *
2311 * \param enc		SES softc associated with the object.
2312 * \param obj		The object to print the data for.
2313 */
2314static void
2315ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2316{
2317	ses_element_t *elmpriv;
2318	struct ses_addl_status *addl;
2319	struct sbuf sesname, name, out;
2320
2321	elmpriv = obj->elm_private;
2322	if (elmpriv == NULL)
2323		return;
2324
2325	addl = &(elmpriv->addl);
2326	if (addl->hdr == NULL)
2327		return;
2328
2329	sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2330	sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2331	sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2332	ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2333	if (sbuf_len(&name) == 0)
2334		sbuf_printf(&name, "(none)");
2335	sbuf_finish(&name);
2336	sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2337	    enc->periph->unit_number);
2338	sbuf_finish(&sesname);
2339	if (elmpriv->descr != NULL)
2340		sbuf_printf(&out, "%s: %s: Element descriptor: '%s'\n",
2341		    sbuf_data(&sesname), sbuf_data(&name), elmpriv->descr);
2342	switch(ses_elm_addlstatus_proto(addl->hdr)) {
2343	case SPSP_PROTO_SAS:
2344		switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2345		case SES_SASOBJ_TYPE_SLOT:
2346			ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2347			    &out, obj, sbuf_data(&name));
2348			break;
2349		case SES_SASOBJ_TYPE_OTHER:
2350			ses_print_addl_data_sas_type1(enc, sbuf_data(&sesname),
2351			    &out, obj, sbuf_data(&name));
2352			break;
2353		default:
2354			break;
2355		}
2356		break;
2357	case SPSP_PROTO_FC:	/* stubbed for now */
2358		break;
2359	default:
2360		break;
2361	}
2362	sbuf_finish(&out);
2363	printf("%s", sbuf_data(&out));
2364	sbuf_delete(&out);
2365	sbuf_delete(&name);
2366	sbuf_delete(&sesname);
2367}
2368
2369/**
2370 * \brief Update the softc with the additional element status data for this
2371 * 	  object, for SAS type 0 objects.
2372 *
2373 * \param enc		SES softc to be updated.
2374 * \param buf		The additional element status response buffer.
2375 * \param bufsiz	Size of the response buffer.
2376 * \param eip		The EIP bit value.
2377 * \param nobj		Number of objects attached to the SES softc.
2378 *
2379 * \return		0 on success, errno otherwise.
2380 */
2381static int
2382ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2383				 uint8_t *buf, int bufsiz, int eip, int nobj)
2384{
2385	int err, offset, physz;
2386	enc_element_t *obj;
2387	ses_element_t *elmpriv;
2388	struct ses_addl_status *addl;
2389
2390	err = offset = 0;
2391
2392	/* basic object setup */
2393	obj = &(enc_cache->elm_map[nobj]);
2394	elmpriv = obj->elm_private;
2395	addl = &(elmpriv->addl);
2396
2397	addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2398
2399	/* Don't assume this object has any phys */
2400	bzero(&addl->proto_data, sizeof(addl->proto_data));
2401	if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2402		goto out;
2403
2404	/* Skip forward to the phy list */
2405	if (eip)
2406		offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2407	else
2408		offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2409
2410	/* Make sure the phy list fits in the buffer */
2411	physz = addl->proto_hdr.sas->base_hdr.num_phys;
2412	physz *= sizeof(struct ses_elm_sas_device_phy);
2413	if (physz > (bufsiz - offset + 4)) {
2414		ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2415		    nobj);
2416		err = EIO;
2417		goto out;
2418	}
2419
2420	/* Point to the phy list */
2421	addl->proto_data.sasdev_phys =
2422	    (struct ses_elm_sas_device_phy *)&buf[offset];
2423
2424out:
2425	return (err);
2426}
2427
2428/**
2429 * \brief Update the softc with the additional element status data for this
2430 * 	  object, for SAS type 1 objects.
2431 *
2432 * \param enc		SES softc to be updated.
2433 * \param buf		The additional element status response buffer.
2434 * \param bufsiz	Size of the response buffer.
2435 * \param eip		The EIP bit value.
2436 * \param nobj		Number of objects attached to the SES softc.
2437 *
2438 * \return		0 on success, errno otherwise.
2439 */
2440static int
2441ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2442			         uint8_t *buf, int bufsiz, int eip, int nobj)
2443{
2444	int err, offset, physz;
2445	enc_element_t *obj;
2446	ses_element_t *elmpriv;
2447	struct ses_addl_status *addl;
2448
2449	err = offset = 0;
2450
2451	/* basic object setup */
2452	obj = &(enc_cache->elm_map[nobj]);
2453	elmpriv = obj->elm_private;
2454	addl = &(elmpriv->addl);
2455
2456	addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2457
2458	/* Don't assume this object has any phys */
2459	bzero(&addl->proto_data, sizeof(addl->proto_data));
2460	if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2461		goto out;
2462
2463	/* Process expanders differently from other type1 cases */
2464	if (ses_obj_is_expander(enc, obj)) {
2465		offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2466		physz = addl->proto_hdr.sas->base_hdr.num_phys *
2467		    sizeof(struct ses_elm_sas_expander_phy);
2468		if (physz > (bufsiz - offset)) {
2469			ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2470			    "End Of Buffer\n", nobj);
2471			err = EIO;
2472			goto out;
2473		}
2474		addl->proto_data.sasexp_phys =
2475		    (struct ses_elm_sas_expander_phy *)&buf[offset];
2476	} else {
2477		offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2478		physz = addl->proto_hdr.sas->base_hdr.num_phys *
2479		    sizeof(struct ses_elm_sas_port_phy);
2480		if (physz > (bufsiz - offset + 4)) {
2481			ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2482			    "Of Buffer\n", nobj);
2483			err = EIO;
2484			goto out;
2485		}
2486		addl->proto_data.sasport_phys =
2487		    (struct ses_elm_sas_port_phy *)&buf[offset];
2488	}
2489
2490out:
2491	return (err);
2492}
2493
2494/**
2495 * \brief Update the softc with the additional element status data for this
2496 * 	  object, for SAS objects.
2497 *
2498 * \param enc		SES softc to be updated.
2499 * \param buf		The additional element status response buffer.
2500 * \param bufsiz	Size of the response buffer.
2501 * \param eip		The EIP bit value.
2502 * \param tidx		Type index for this object.
2503 * \param nobj		Number of objects attached to the SES softc.
2504 *
2505 * \return		0 on success, errno otherwise.
2506 */
2507static int
2508ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2509			   uint8_t *buf, int bufsiz, int eip, int tidx,
2510			   int nobj)
2511{
2512	int dtype, err;
2513	ses_cache_t *ses_cache;
2514	union ses_elm_sas_hdr *hdr;
2515
2516	/* Need to be able to read the descriptor type! */
2517	if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2518		err = EIO;
2519		goto out;
2520	}
2521
2522	ses_cache = enc_cache->private;
2523
2524	hdr = (union ses_elm_sas_hdr *)buf;
2525	dtype = ses_elm_sas_descr_type(hdr);
2526	switch(dtype) {
2527	case SES_SASOBJ_TYPE_SLOT:
2528		switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2529		case ELMTYP_DEVICE:
2530		case ELMTYP_ARRAY_DEV:
2531			break;
2532		default:
2533			ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2534			    "invalid for SES element type 0x%x\n", nobj,
2535			    ses_cache->ses_types[tidx].hdr->etype_elm_type);
2536			err = ENODEV;
2537			goto out;
2538		}
2539		err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2540						       buf, bufsiz, eip,
2541		    nobj);
2542		break;
2543	case SES_SASOBJ_TYPE_OTHER:
2544		switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2545		case ELMTYP_SAS_EXP:
2546		case ELMTYP_SCSI_INI:
2547		case ELMTYP_SCSI_TGT:
2548		case ELMTYP_ESCC:
2549			break;
2550		default:
2551			ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2552			    "invalid for SES element type 0x%x\n", nobj,
2553			    ses_cache->ses_types[tidx].hdr->etype_elm_type);
2554			err = ENODEV;
2555			goto out;
2556		}
2557		err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2558						       bufsiz, eip, nobj);
2559		break;
2560	default:
2561		ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2562		    "of unknown type 0x%x\n", nobj,
2563		    ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2564		err = ENODEV;
2565		break;
2566	}
2567
2568out:
2569	return (err);
2570}
2571
2572static void
2573ses_softc_invalidate(enc_softc_t *enc)
2574{
2575	ses_softc_t *ses;
2576
2577	ses = enc->enc_private;
2578	ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2579}
2580
2581static void
2582ses_softc_cleanup(enc_softc_t *enc)
2583{
2584
2585	ses_cache_free(enc, &enc->enc_cache);
2586	ses_cache_free(enc, &enc->enc_daemon_cache);
2587	ENC_FREE_AND_NULL(enc->enc_private);
2588	ENC_FREE_AND_NULL(enc->enc_cache.private);
2589	ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2590}
2591
2592static int
2593ses_init_enc(enc_softc_t *enc)
2594{
2595	return (0);
2596}
2597
2598static int
2599ses_get_enc_status(enc_softc_t *enc, int slpflag)
2600{
2601	/* Automatically updated, caller checks enc_cache->encstat itself */
2602	return (0);
2603}
2604
2605static int
2606ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2607{
2608	ses_control_request_t req;
2609	ses_softc_t	     *ses;
2610
2611	ses = enc->enc_private;
2612	req.elm_idx = SES_SETSTATUS_ENC_IDX;
2613	req.elm_stat.comstatus = encstat & 0xf;
2614
2615	TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2616	enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2617	cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2618
2619	return (req.result);
2620}
2621
2622static int
2623ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2624{
2625	unsigned int i = elms->elm_idx;
2626
2627	memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2628	return (0);
2629}
2630
2631static int
2632ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2633{
2634	ses_control_request_t req;
2635	ses_softc_t	     *ses;
2636
2637	/* If this is clear, we don't do diddly.  */
2638	if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2639		return (0);
2640
2641	ses = enc->enc_private;
2642	req.elm_idx = elms->elm_idx;
2643	memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2644
2645	TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2646	enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2647	cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2648
2649	return (req.result);
2650}
2651
2652static int
2653ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2654{
2655	int i = (int)elmd->elm_idx;
2656	ses_element_t *elmpriv;
2657
2658	/* Assume caller has already checked obj_id validity */
2659	elmpriv = enc->enc_cache.elm_map[i].elm_private;
2660	/* object might not have a descriptor */
2661	if (elmpriv == NULL || elmpriv->descr == NULL) {
2662		elmd->elm_desc_len = 0;
2663		return (0);
2664	}
2665	if (elmd->elm_desc_len > elmpriv->descr_len)
2666		elmd->elm_desc_len = elmpriv->descr_len;
2667	copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2668	return (0);
2669}
2670
2671/**
2672 * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2673 *	  given object id if one is available.
2674 *
2675 * \param enc	SES softc to examine.
2676 * \param objdn	ioctl structure to read/write device name info.
2677 *
2678 * \return	0 on success, errno otherwise.
2679 */
2680static int
2681ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2682{
2683	struct sbuf sb;
2684	int len;
2685
2686	len = elmdn->elm_names_size;
2687	if (len < 0)
2688		return (EINVAL);
2689
2690	cam_periph_unlock(enc->periph);
2691	sbuf_new(&sb, NULL, len, SBUF_FIXEDLEN);
2692	ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2693		       ses_elmdevname_callback, &sb);
2694	sbuf_finish(&sb);
2695	elmdn->elm_names_len = sbuf_len(&sb);
2696	copyout(sbuf_data(&sb), elmdn->elm_devnames, elmdn->elm_names_len + 1);
2697	cam_periph_lock(enc->periph);
2698	return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2699}
2700
2701/**
2702 * \brief Send a string to the primary subenclosure using the String Out
2703 * 	  SES diagnostic page.
2704 *
2705 * \param enc	SES enclosure to run the command on.
2706 * \param sstr	SES string structure to operate on
2707 * \param ioc	Ioctl being performed
2708 *
2709 * \return	0 on success, errno otherwise.
2710 */
2711static int
2712ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2713{
2714	ses_softc_t *ses;
2715	enc_cache_t *enc_cache;
2716	ses_cache_t *ses_cache;
2717	const struct ses_enc_desc *enc_desc;
2718	int amt, payload, ret;
2719	char cdb[6];
2720	char str[32];
2721	char vendor[9];
2722	char product[17];
2723	char rev[5];
2724	uint8_t *buf;
2725	size_t size, rsize;
2726
2727	ses = enc->enc_private;
2728	enc_cache = &enc->enc_daemon_cache;
2729	ses_cache = enc_cache->private;
2730
2731	/* Implement SES2r20 6.1.6 */
2732	if (sstr->bufsiz > 0xffff)
2733		return (EINVAL); /* buffer size too large */
2734
2735	if (ioc == ENCIOC_SETSTRING) {
2736		payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2737		amt = 0 - payload;
2738		buf = ENC_MALLOC(payload);
2739		if (buf == NULL)
2740			return ENOMEM;
2741
2742		ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2743		/* Construct the page request */
2744		buf[0] = SesStringOut;
2745		buf[1] = 0;
2746		buf[2] = sstr->bufsiz >> 8;
2747		buf[3] = sstr->bufsiz & 0xff;
2748		memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2749	} else if (ioc == ENCIOC_GETSTRING) {
2750		payload = sstr->bufsiz;
2751		amt = payload;
2752		ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2753		buf = sstr->buf;
2754	} else if (ioc == ENCIOC_GETENCNAME) {
2755		if (ses_cache->ses_nsubencs < 1)
2756			return (ENODEV);
2757		enc_desc = ses_cache->subencs[0];
2758		cam_strvis(vendor, enc_desc->vendor_id,
2759		    sizeof(enc_desc->vendor_id), sizeof(vendor));
2760		cam_strvis(product, enc_desc->product_id,
2761		    sizeof(enc_desc->product_id), sizeof(product));
2762		cam_strvis(rev, enc_desc->product_rev,
2763		    sizeof(enc_desc->product_rev), sizeof(rev));
2764		rsize = snprintf(str, sizeof(str), "%s %s %s",
2765		    vendor, product, rev) + 1;
2766		if (rsize > sizeof(str))
2767			rsize = sizeof(str);
2768		copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2769		size = rsize;
2770		if (size > sstr->bufsiz)
2771			size = sstr->bufsiz;
2772		copyout(str, sstr->buf, size);
2773		return (size == rsize ? 0 : ENOMEM);
2774	} else if (ioc == ENCIOC_GETENCID) {
2775		if (ses_cache->ses_nsubencs < 1)
2776			return (ENODEV);
2777		enc_desc = ses_cache->subencs[0];
2778		rsize = snprintf(str, sizeof(str), "%16jx",
2779		    scsi_8btou64(enc_desc->logical_id)) + 1;
2780		if (rsize > sizeof(str))
2781			rsize = sizeof(str);
2782		copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2783		size = rsize;
2784		if (size > sstr->bufsiz)
2785			size = sstr->bufsiz;
2786		copyout(str, sstr->buf, size);
2787		return (size == rsize ? 0 : ENOMEM);
2788	} else
2789		return EINVAL;
2790
2791	ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2792	if (ioc == ENCIOC_SETSTRING)
2793		ENC_FREE(buf);
2794	return ret;
2795}
2796
2797/**
2798 * \invariant Called with cam_periph mutex held.
2799 */
2800static void
2801ses_poll_status(enc_softc_t *enc)
2802{
2803	ses_softc_t *ses;
2804
2805	ses = enc->enc_private;
2806	enc_update_request(enc, SES_UPDATE_GETSTATUS);
2807	if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2808		enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2809}
2810
2811/**
2812 * \brief Notification received when CAM detects a new device in the
2813 *        SCSI domain in which this SEP resides.
2814 *
2815 * \param enc	SES enclosure instance.
2816 */
2817static void
2818ses_device_found(enc_softc_t *enc)
2819{
2820	ses_poll_status(enc);
2821	enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2822}
2823
2824static struct enc_vec ses_enc_vec =
2825{
2826	.softc_invalidate	= ses_softc_invalidate,
2827	.softc_cleanup		= ses_softc_cleanup,
2828	.init_enc		= ses_init_enc,
2829	.get_enc_status		= ses_get_enc_status,
2830	.set_enc_status		= ses_set_enc_status,
2831	.get_elm_status		= ses_get_elm_status,
2832	.set_elm_status		= ses_set_elm_status,
2833	.get_elm_desc		= ses_get_elm_desc,
2834	.get_elm_devnames	= ses_get_elm_devnames,
2835	.handle_string		= ses_handle_string,
2836	.device_found		= ses_device_found,
2837	.poll_status		= ses_poll_status
2838};
2839
2840/**
2841 * \brief Initialize a new SES instance.
2842 *
2843 * \param enc		SES softc structure to set up the instance in.
2844 * \param doinit	Do the initialization (see main driver).
2845 *
2846 * \return		0 on success, errno otherwise.
2847 */
2848int
2849ses_softc_init(enc_softc_t *enc)
2850{
2851	ses_softc_t *ses_softc;
2852
2853	CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2854	    ("entering enc_softc_init(%p)\n", enc));
2855
2856	enc->enc_vec = ses_enc_vec;
2857	enc->enc_fsm_states = enc_fsm_states;
2858
2859	if (enc->enc_private == NULL)
2860		enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2861	if (enc->enc_cache.private == NULL)
2862		enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2863	if (enc->enc_daemon_cache.private == NULL)
2864		enc->enc_daemon_cache.private =
2865		     ENC_MALLOCZ(sizeof(ses_cache_t));
2866
2867	if (enc->enc_private == NULL
2868	 || enc->enc_cache.private == NULL
2869	 || enc->enc_daemon_cache.private == NULL) {
2870		ENC_FREE_AND_NULL(enc->enc_private);
2871		ENC_FREE_AND_NULL(enc->enc_cache.private);
2872		ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2873		return (ENOMEM);
2874	}
2875
2876	ses_softc = enc->enc_private;
2877	TAILQ_INIT(&ses_softc->ses_requests);
2878	TAILQ_INIT(&ses_softc->ses_pending_requests);
2879
2880	enc_update_request(enc, SES_UPDATE_PAGES);
2881
2882	// XXX: Move this to the FSM so it doesn't hang init
2883	if (0) (void) ses_set_timed_completion(enc, 1);
2884
2885	return (0);
2886}
2887
2888