2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Matthew Jacob
5 * Copyright (c) 2010 Spectra Logic Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * \file scsi_enc_ses.c
33 * Structures and routines specific && private to SES only
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/ctype.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/queue.h>
50 #include <sys/systm.h>
51 #include <sys/types.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_periph.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/scsi_enc.h>
60 #include <cam/scsi/scsi_enc_internal.h>
62 /* SES Native Type Device Support */
64 /* SES Diagnostic Page Codes */
66 SesSupportedPages = 0x0,
69 SesStatusPage = SesControlPage,
72 SesStringIn = SesStringOut,
73 SesThresholdOut = 0x5,
74 SesThresholdIn = SesThresholdOut,
75 SesArrayControl = 0x6, /* Obsolete in SES v2 */
76 SesArrayStatus = SesArrayControl,
77 SesElementDescriptor = 0x7,
79 SesEnclosureBusy = 0x9,
80 SesAddlElementStatus = 0xa
83 typedef struct ses_type {
84 const struct ses_elm_type_desc *hdr;
88 typedef struct ses_comstat {
93 typedef union ses_addl_data {
94 struct ses_elm_sas_device_phy *sasdev_phys;
95 struct ses_elm_sas_expander_phy *sasexp_phys;
96 struct ses_elm_sas_port_phy *sasport_phys;
97 struct ses_fcobj_port *fc_ports;
100 typedef struct ses_addl_status {
101 struct ses_elm_addlstatus_base_hdr *hdr;
103 union ses_fcobj_hdr *fc;
104 union ses_elm_sas_hdr *sas;
105 struct ses_elm_ata_hdr *ata;
107 union ses_addl_data proto_data; /* array sizes stored in header */
110 typedef struct ses_element {
111 uint8_t eip; /* eip bit is set */
112 uint16_t descr_len; /* length of the descriptor */
113 char *descr; /* descriptor for this object */
114 struct ses_addl_status addl; /* additional status info */
117 typedef struct ses_control_request {
119 ses_comstat_t elm_stat;
121 TAILQ_ENTRY(ses_control_request) links;
122 } ses_control_request_t;
123 TAILQ_HEAD(ses_control_reqlist, ses_control_request);
124 typedef struct ses_control_reqlist ses_control_reqlist_t;
126 SES_SETSTATUS_ENC_IDX = -1
130 ses_terminate_control_requests(ses_control_reqlist_t *reqlist, int result)
132 ses_control_request_t *req;
134 while ((req = TAILQ_FIRST(reqlist)) != NULL) {
135 TAILQ_REMOVE(reqlist, req, links);
136 req->result = result;
141 enum ses_iter_index_values {
143 * \brief Value of an initialized but invalid index
144 * in a ses_iterator object.
146 * This value is used for the individual_element_index of
147 * overal status elements and for all index types when
148 * an iterator is first initialized.
150 ITERATOR_INDEX_INVALID = -1,
153 * \brief Value of an index in a ses_iterator object
154 * when the iterator has traversed past the last
157 ITERATOR_INDEX_END = INT_MAX
161 * \brief Structure encapsulating all data necessary to traverse the
162 * elements of a SES configuration.
164 * The ses_iterator object simplifies the task of iterating through all
165 * elements detected via the SES configuration page by tracking the numerous
166 * element indexes that, instead of memoizing in the softc, we calculate
167 * on the fly during the traversal of the element objects. The various
168 * indexes are necessary due to the varying needs of matching objects in
169 * the different SES pages. Some pages (e.g. Status/Control) contain all
170 * elements, while others (e.g. Additional Element Status) only contain
171 * individual elements (no overal status elements) of particular types.
173 * To use an iterator, initialize it with ses_iter_init(), and then
174 * use ses_iter_next() to traverse the elements (including the first) in
175 * the configuration. Once an iterator is initiailized with ses_iter_init(),
176 * you may also seek to any particular element by either it's global or
177 * individual element index via the ses_iter_seek_to() function. You may
178 * also return an iterator to the position just before the first element
179 * (i.e. the same state as after an ses_iter_init()), with ses_iter_reset().
181 struct ses_iterator {
183 * \brief Backlink to the overal software configuration structure.
185 * This is included for convenience so the iteration functions
186 * need only take a single, struct ses_iterator *, argument.
193 * \brief Index of the type of the current element within the
194 * ses_cache's ses_types array.
199 * \brief The position (0 based) of this element relative to all other
200 * elements of this type.
202 * This index resets to zero every time the iterator transitions
203 * to elements of a new type in the configuration.
205 int type_element_index;
208 * \brief The position (0 based) of this element relative to all
209 * other individual status elements in the configuration.
211 * This index ranges from 0 through the number of individual
212 * elements in the configuration. When the iterator returns
213 * an overall status element, individual_element_index is
214 * set to ITERATOR_INDEX_INVALID, to indicate that it does
215 * not apply to the current element.
217 int individual_element_index;
220 * \brief The position (0 based) of this element relative to
221 * all elements in the configration.
223 * This index is appropriate for indexing into enc->ses_elm_map.
225 int global_element_index;
228 * \brief The last valid individual element index of this
231 * When an iterator traverses an overal status element, the
232 * individual element index is reset to ITERATOR_INDEX_INVALID
233 * to prevent unintential use of the individual_element_index
234 * field. The saved_individual_element_index allows the iterator
235 * to restore it's position in the individual elements upon
236 * reaching the next individual element.
238 int saved_individual_element_index;
244 SES_UPDATE_GETCONFIG,
245 SES_UPDATE_GETSTATUS,
246 SES_UPDATE_GETELMDESCS,
247 SES_UPDATE_GETELMADDLSTATUS,
248 SES_PROCESS_CONTROL_REQS,
249 SES_PUBLISH_PHYSPATHS,
251 SES_NUM_UPDATE_STATES
254 static enc_softc_cleanup_t ses_softc_cleanup;
258 static fsm_fill_handler_t ses_fill_rcv_diag_io;
259 static fsm_fill_handler_t ses_fill_control_request;
260 static fsm_done_handler_t ses_process_pages;
261 static fsm_done_handler_t ses_process_config;
262 static fsm_done_handler_t ses_process_status;
263 static fsm_done_handler_t ses_process_elm_descs;
264 static fsm_done_handler_t ses_process_elm_addlstatus;
265 static fsm_done_handler_t ses_process_control_request;
266 static fsm_done_handler_t ses_publish_physpaths;
267 static fsm_done_handler_t ses_publish_cache;
269 static struct enc_fsm_state enc_fsm_states[SES_NUM_UPDATE_STATES] =
271 { "SES_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
277 ses_fill_rcv_diag_io,
282 "SES_UPDATE_GETCONFIG",
286 ses_fill_rcv_diag_io,
291 "SES_UPDATE_GETSTATUS",
295 ses_fill_rcv_diag_io,
300 "SES_UPDATE_GETELMDESCS",
301 SesElementDescriptor,
304 ses_fill_rcv_diag_io,
305 ses_process_elm_descs,
309 "SES_UPDATE_GETELMADDLSTATUS",
310 SesAddlElementStatus,
313 ses_fill_rcv_diag_io,
314 ses_process_elm_addlstatus,
318 "SES_PROCESS_CONTROL_REQS",
322 ses_fill_control_request,
323 ses_process_control_request,
327 "SES_PUBLISH_PHYSPATHS",
332 ses_publish_physpaths,
346 typedef struct ses_cache {
347 /* Source for all the configuration data pointers */
348 const struct ses_cfg_page *cfg_page;
350 /* References into the config page. */
352 const struct ses_enc_desc * const *subencs;
354 const ses_type_t *ses_types;
356 /* Source for all the status pointers */
357 const struct ses_status_page *status_page;
359 /* Source for all the object descriptor pointers */
360 const struct ses_elem_descr_page *elm_descs_page;
362 /* Source for all the additional object status pointers */
363 const struct ses_addl_elem_status_page *elm_addlstatus_page;
367 typedef struct ses_softc {
369 #define SES_FLAG_TIMEDCOMP 0x01
370 #define SES_FLAG_ADDLSTATUS 0x02
371 #define SES_FLAG_DESC 0x04
373 ses_control_reqlist_t ses_requests;
374 ses_control_reqlist_t ses_pending_requests;
378 * \brief Reset a SES iterator to just before the first element
379 * in the configuration.
381 * \param iter The iterator object to reset.
383 * The indexes within a reset iterator are invalid and will only
384 * become valid upon completion of a ses_iter_seek_to() or a
388 ses_iter_reset(struct ses_iterator *iter)
391 * Set our indexes to just before the first valid element
392 * of the first type (ITERATOR_INDEX_INVALID == -1). This
393 * simplifies the implementation of ses_iter_next().
395 iter->type_index = 0;
396 iter->type_element_index = ITERATOR_INDEX_INVALID;
397 iter->global_element_index = ITERATOR_INDEX_INVALID;
398 iter->individual_element_index = ITERATOR_INDEX_INVALID;
399 iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
403 * \brief Initialize the storage of a SES iterator and reset it to
404 * the position just before the first element of the
407 * \param enc The SES softc for the SES instance whose configuration
408 * will be enumerated by this iterator.
409 * \param iter The iterator object to initialize.
412 ses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
416 ses_iter_reset(iter);
420 * \brief Traverse the provided SES iterator to the next element
421 * within the configuraiton.
423 * \param iter The iterator to move.
425 * \return If a valid next element exists, a pointer to it's enc_element_t.
428 static enc_element_t *
429 ses_iter_next(struct ses_iterator *iter)
431 ses_cache_t *ses_cache;
432 const ses_type_t *element_type;
434 ses_cache = iter->cache->private;
437 * Note: Treat nelms as signed, so we will hit this case
438 * and immediately terminate the iteration if the
439 * configuration has 0 objects.
441 if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
443 /* Elements exhausted. */
444 iter->type_index = ITERATOR_INDEX_END;
445 iter->type_element_index = ITERATOR_INDEX_END;
446 iter->global_element_index = ITERATOR_INDEX_END;
447 iter->individual_element_index = ITERATOR_INDEX_END;
448 iter->saved_individual_element_index = ITERATOR_INDEX_END;
452 KASSERT((iter->type_index < ses_cache->ses_ntypes),
453 ("Corrupted element iterator. %d not less than %d",
454 iter->type_index, ses_cache->ses_ntypes));
456 element_type = &ses_cache->ses_types[iter->type_index];
457 iter->global_element_index++;
458 iter->type_element_index++;
461 * There is an object for overal type status in addition
462 * to one for each allowed element, but only if the element
465 if (iter->type_element_index > element_type->hdr->etype_maxelt) {
468 * We've exhausted the elements of this type.
469 * This next element belongs to the next type.
472 iter->type_element_index = 0;
473 iter->individual_element_index = ITERATOR_INDEX_INVALID;
476 if (iter->type_element_index > 0) {
477 iter->individual_element_index =
478 ++iter->saved_individual_element_index;
481 return (&iter->cache->elm_map[iter->global_element_index]);
485 * Element index types tracked by a SES iterator.
489 * Index relative to all elements (overall and individual)
492 SES_ELEM_INDEX_GLOBAL,
495 * \brief Index relative to all individual elements in the system.
497 * This index counts only individual elements, skipping overall
498 * status elements. This is the index space of the additional
499 * element status page (page 0xa).
501 SES_ELEM_INDEX_INDIVIDUAL
502 } ses_elem_index_type_t;
505 * \brief Move the provided iterator forwards or backwards to the object
506 * having the give index.
508 * \param iter The iterator on which to perform the seek.
509 * \param element_index The index of the element to find.
510 * \param index_type The type (global or individual) of element_index.
512 * \return If the element is found, a pointer to it's enc_element_t.
515 static enc_element_t *
516 ses_iter_seek_to(struct ses_iterator *iter, int element_index,
517 ses_elem_index_type_t index_type)
519 enc_element_t *element;
522 if (index_type == SES_ELEM_INDEX_GLOBAL)
523 cur_index = &iter->global_element_index;
525 cur_index = &iter->individual_element_index;
527 if (*cur_index == element_index) {
529 return (&iter->cache->elm_map[iter->global_element_index]);
532 ses_iter_reset(iter);
533 while ((element = ses_iter_next(iter)) != NULL
534 && *cur_index != element_index)
537 if (*cur_index != element_index)
544 static int ses_encode(enc_softc_t *, uint8_t *, int, int,
545 struct ses_comstat *);
547 static int ses_set_timed_completion(enc_softc_t *, uint8_t);
549 static int ses_putstatus(enc_softc_t *, int, struct ses_comstat *);
552 static void ses_poll_status(enc_softc_t *);
553 static void ses_print_addl_data(enc_softc_t *, enc_element_t *);
555 /*=========================== SES cleanup routines ===========================*/
558 ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
560 ses_cache_t *ses_cache;
561 ses_cache_t *other_ses_cache;
562 enc_element_t *cur_elm;
563 enc_element_t *last_elm;
565 ENC_DLOG(enc, "%s: enter\n", __func__);
566 ses_cache = cache->private;
567 if (ses_cache->elm_addlstatus_page == NULL)
570 for (cur_elm = cache->elm_map,
571 last_elm = &cache->elm_map[cache->nelms];
572 cur_elm != last_elm; cur_elm++) {
573 ses_element_t *elmpriv;
575 elmpriv = cur_elm->elm_private;
577 /* Clear references to the additional status page. */
578 bzero(&elmpriv->addl, sizeof(elmpriv->addl));
581 other_ses_cache = enc_other_cache(enc, cache)->private;
582 if (other_ses_cache->elm_addlstatus_page
583 != ses_cache->elm_addlstatus_page)
584 ENC_FREE(ses_cache->elm_addlstatus_page);
585 ses_cache->elm_addlstatus_page = NULL;
589 ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
591 ses_cache_t *ses_cache;
592 ses_cache_t *other_ses_cache;
593 enc_element_t *cur_elm;
594 enc_element_t *last_elm;
596 ENC_DLOG(enc, "%s: enter\n", __func__);
597 ses_cache = cache->private;
598 if (ses_cache->elm_descs_page == NULL)
601 for (cur_elm = cache->elm_map,
602 last_elm = &cache->elm_map[cache->nelms];
603 cur_elm != last_elm; cur_elm++) {
604 ses_element_t *elmpriv;
606 elmpriv = cur_elm->elm_private;
607 elmpriv->descr_len = 0;
608 elmpriv->descr = NULL;
611 other_ses_cache = enc_other_cache(enc, cache)->private;
612 if (other_ses_cache->elm_descs_page
613 != ses_cache->elm_descs_page)
614 ENC_FREE(ses_cache->elm_descs_page);
615 ses_cache->elm_descs_page = NULL;
619 ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
621 ses_cache_t *ses_cache;
622 ses_cache_t *other_ses_cache;
624 ENC_DLOG(enc, "%s: enter\n", __func__);
625 ses_cache = cache->private;
626 if (ses_cache->status_page == NULL)
629 other_ses_cache = enc_other_cache(enc, cache)->private;
630 if (other_ses_cache->status_page != ses_cache->status_page)
631 ENC_FREE(ses_cache->status_page);
632 ses_cache->status_page = NULL;
636 ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
638 enc_element_t *cur_elm;
639 enc_element_t *last_elm;
641 ENC_DLOG(enc, "%s: enter\n", __func__);
642 if (cache->elm_map == NULL)
645 ses_cache_free_elm_descs(enc, cache);
646 ses_cache_free_elm_addlstatus(enc, cache);
647 for (cur_elm = cache->elm_map,
648 last_elm = &cache->elm_map[cache->nelms];
649 cur_elm != last_elm; cur_elm++) {
651 ENC_FREE_AND_NULL(cur_elm->elm_private);
653 ENC_FREE_AND_NULL(cache->elm_map);
655 ENC_DLOG(enc, "%s: exit\n", __func__);
659 ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
661 ses_cache_t *other_ses_cache;
662 ses_cache_t *ses_cache;
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);
669 ses_cache = cache->private;
670 ses_cache->ses_ntypes = 0;
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;
677 if (other_ses_cache->ses_types != ses_cache->ses_types)
678 ENC_FREE(ses_cache->ses_types);
679 ses_cache->ses_types = NULL;
681 if (other_ses_cache->cfg_page != ses_cache->cfg_page)
682 ENC_FREE(ses_cache->cfg_page);
683 ses_cache->cfg_page = NULL;
685 ENC_DLOG(enc, "%s: exit\n", __func__);
689 ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
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;
697 ses_cache_free(enc, dst);
698 src_ses_cache = src->private;
699 dst_ses_cache = dst->private;
702 * The cloned enclosure cache and ses specific cache are
703 * mostly identical to the source.
706 *dst_ses_cache = *src_ses_cache;
709 * But the ses cache storage is still independent. Restore
710 * the pointer that was clobbered by the structure copy above.
712 dst->private = dst_ses_cache;
715 * The element map is independent even though it starts out
716 * pointing to the same constant page data.
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++) {
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));
732 /* Structure accessors. These are strongly typed to avoid errors. */
735 ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
737 return ((obj)->base_hdr.byte1 >> 6);
740 ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
742 return ((hdr)->byte0 & 0xf);
745 ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
747 return ((hdr)->byte0 >> 4) & 0x1;
750 ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
752 return ((hdr)->byte0 >> 7);
755 ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
757 return ((hdr)->type0_noneip.byte1 & 0x1);
760 ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
762 return ((phy)->target_ports & 0x1);
765 ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
767 return ((phy)->target_ports >> 7);
770 ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
772 return (((phy)->byte0 >> 4) & 0x7);
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.
780 * \param ses_cache The SES cache to validate.
781 * \param gen_code The 4 byte generation code from a SES diagnostic
784 * \return non-zero if true, 0 if false.
787 ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
792 if (ses_cache->cfg_page == NULL)
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);
801 * Function signature for consumers of the ses_devids_iter() interface.
803 typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
804 struct scsi_vpd_id_descriptor *, void *);
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.
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.
818 ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
819 ses_devid_callback_t *callback, void *callback_arg)
821 ses_element_t *elmpriv;
822 struct ses_addl_status *addl;
824 size_t devid_record_size;
826 elmpriv = elm->elm_private;
827 addl = &(elmpriv->addl);
829 devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
830 + sizeof(struct scsi_vpd_id_naa_ieee_reg);
831 for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
832 uint8_t devid_buf[devid_record_size];
833 struct scsi_vpd_id_descriptor *devid;
836 devid = (struct scsi_vpd_id_descriptor *)devid_buf;
837 phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
838 devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
839 | SVPD_ID_CODESET_BINARY;
840 devid->id_type = SVPD_ID_PIV
844 devid->length = sizeof(struct scsi_vpd_id_naa_ieee_reg);
845 memcpy(devid->identifier, phy_addr, devid->length);
847 callback(enc, elm, devid, callback_arg);
852 * Function signature for consumers of the ses_paths_iter() interface.
854 typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
855 struct cam_path *, void *);
858 * Argument package passed through ses_devids_iter() by
859 * ses_paths_iter() to ses_path_iter_devid_callback().
861 typedef struct ses_path_iter_args {
862 ses_path_callback_t *callback;
864 } ses_path_iter_args_t;
867 * ses_devids_iter() callback function used by ses_paths_iter()
868 * to map device ids to peripheral driver instances.
870 * \param enc SES instance containing elm
871 * \param elm Element on which device ID matching is active.
872 * \param periph A device ID corresponding to elm.
873 * \param arg Argument passed through to callback on each invocation.
876 ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
877 struct scsi_vpd_id_descriptor *devid,
880 struct ccb_dev_match cdm;
881 struct dev_match_pattern match_pattern;
882 struct dev_match_result match_result;
883 struct device_match_result *device_match;
884 struct device_match_pattern *device_pattern;
885 ses_path_iter_args_t *args;
887 args = (ses_path_iter_args_t *)arg;
888 match_pattern.type = DEV_MATCH_DEVICE;
889 device_pattern = &match_pattern.pattern.device_pattern;
890 device_pattern->flags = DEV_MATCH_DEVID;
891 device_pattern->data.devid_pat.id_len =
892 offsetof(struct scsi_vpd_id_descriptor, identifier)
894 memcpy(device_pattern->data.devid_pat.id, devid,
895 device_pattern->data.devid_pat.id_len);
897 memset(&cdm, 0, sizeof(cdm));
898 if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
901 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
904 cdm.ccb_h.func_code = XPT_DEV_MATCH;
905 cdm.num_patterns = 1;
906 cdm.patterns = &match_pattern;
907 cdm.pattern_buf_len = sizeof(match_pattern);
908 cdm.match_buf_len = sizeof(match_result);
909 cdm.matches = &match_result;
911 xpt_action((union ccb *)&cdm);
912 xpt_free_path(cdm.ccb_h.path);
914 if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
915 || (cdm.status != CAM_DEV_MATCH_LAST
916 && cdm.status != CAM_DEV_MATCH_MORE)
917 || cdm.num_matches == 0)
920 device_match = &match_result.result.device_result;
921 if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
922 device_match->path_id,
923 device_match->target_id,
924 device_match->target_lun) != CAM_REQ_CMP)
927 args->callback(enc, elem, cdm.ccb_h.path, args->callback_arg);
929 xpt_free_path(cdm.ccb_h.path);
933 * \brief Iterate over and find the matching periph objects for the
936 * \param enc SES instance containing elm
937 * \param elm Element for which to perform periph object matching.
938 * \param callback The callback function to invoke with each matching
940 * \param callback_arg Argument passed through to callback on each invocation.
943 ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
944 ses_path_callback_t *callback, void *callback_arg)
946 ses_element_t *elmpriv;
947 struct ses_addl_status *addl;
949 elmpriv = elm->elm_private;
950 addl = &(elmpriv->addl);
952 if (addl->hdr == NULL)
955 if (addl->proto_hdr.sas != NULL &&
956 addl->proto_data.sasdev_phys != NULL) {
957 ses_path_iter_args_t args;
959 args.callback = callback;
960 args.callback_arg = callback_arg;
961 ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
962 } else if (addl->proto_hdr.ata != NULL) {
963 struct cam_path *path;
964 struct ccb_getdev cgd;
966 if (xpt_create_path(&path, /*periph*/NULL,
967 scsi_4btoul(addl->proto_hdr.ata->bus),
968 scsi_4btoul(addl->proto_hdr.ata->target), 0)
972 xpt_setup_ccb(&cgd.ccb_h, path, CAM_PRIORITY_NORMAL);
973 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
974 xpt_action((union ccb *)&cgd);
975 if (cgd.ccb_h.status == CAM_REQ_CMP)
976 callback(enc, elm, path, callback_arg);
983 * ses_paths_iter() callback function used by ses_get_elmdevname()
984 * to record periph driver instance strings corresponding to a SES
987 * \param enc SES instance containing elm
988 * \param elm Element on which periph matching is active.
989 * \param periph A periph instance that matches elm.
990 * \param arg Argument passed through to callback on each invocation.
993 ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
994 struct cam_path *path, void *arg)
998 sb = (struct sbuf *)arg;
999 cam_periph_list(path, sb);
1003 * Argument package passed through ses_paths_iter() to
1004 * ses_getcampath_callback.
1006 typedef struct ses_setphyspath_callback_args {
1007 struct sbuf *physpath;
1009 } ses_setphyspath_callback_args_t;
1012 * \brief ses_paths_iter() callback to set the physical path on the
1013 * CAM EDT entries corresponding to a given SES element.
1015 * \param enc SES instance containing elm
1016 * \param elm Element on which periph matching is active.
1017 * \param periph A periph instance that matches elm.
1018 * \param arg Argument passed through to callback on each invocation.
1021 ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1022 struct cam_path *path, void *arg)
1024 struct ccb_dev_advinfo cdai;
1025 ses_setphyspath_callback_args_t *args;
1028 args = (ses_setphyspath_callback_args_t *)arg;
1029 old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1030 cam_periph_lock(enc->periph);
1031 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1032 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1033 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1034 cdai.flags = CDAI_FLAG_NONE;
1035 cdai.bufsiz = MAXPATHLEN;
1036 cdai.buf = old_physpath;
1037 xpt_action((union ccb *)&cdai);
1038 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1039 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1041 if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1043 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1044 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1045 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1046 cdai.flags = CDAI_FLAG_STORE;
1047 cdai.bufsiz = sbuf_len(args->physpath);
1048 cdai.buf = sbuf_data(args->physpath);
1049 xpt_action((union ccb *)&cdai);
1050 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1051 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1052 if (cdai.ccb_h.status == CAM_REQ_CMP)
1055 cam_periph_unlock(enc->periph);
1056 free(old_physpath, M_SCSIENC);
1060 * \brief Set a device's physical path string in CAM XPT.
1062 * \param enc SES instance containing elm
1063 * \param elm Element to publish physical path string for
1064 * \param iter Iterator whose state corresponds to elm
1066 * \return 0 on success, errno otherwise.
1069 ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1070 struct ses_iterator *iter)
1072 struct ccb_dev_advinfo cdai;
1073 ses_setphyspath_callback_args_t args;
1076 struct scsi_vpd_id_descriptor *idd;
1078 ses_element_t *elmpriv;
1084 elmpriv = elm->elm_private;
1085 if (elmpriv->addl.hdr == NULL)
1089 * Assemble the components of the physical path starting with
1090 * the device ID of the enclosure itself.
1092 xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1093 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1094 cdai.flags = CDAI_FLAG_NONE;
1095 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1096 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1097 cdai.buf = devid = malloc(cdai.bufsiz, M_SCSIENC, M_WAITOK|M_ZERO);
1098 cam_periph_lock(enc->periph);
1099 xpt_action((union ccb *)&cdai);
1100 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1101 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1102 cam_periph_unlock(enc->periph);
1103 if (cdai.ccb_h.status != CAM_REQ_CMP)
1106 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1107 cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1111 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1115 /* Next, generate the physical path string */
1116 sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1117 scsi_8btou64(idd->identifier), iter->type_index,
1118 iter->type_element_index);
1119 /* Append the element descriptor if one exists */
1120 if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1121 sbuf_cat(&sb, "/elmdesc@");
1122 for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1124 if (!isprint(*c) || isspace(*c) || *c == '/')
1125 sbuf_putc(&sb, '_');
1133 * Set this physical path on any CAM devices with a device ID
1134 * descriptor that matches one created from the SES additional
1135 * status data for this element.
1139 ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1142 ret = args.num_set == 0 ? ENOENT : 0;
1151 * \brief Helper to set the CDB fields appropriately.
1153 * \param cdb Buffer containing the cdb.
1154 * \param pagenum SES diagnostic page to query for.
1155 * \param dir Direction of query.
1158 ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1161 /* Ref: SPC-4 r25 Section 6.20 Table 223 */
1162 if (dir == CAM_DIR_IN) {
1163 cdb[0] = RECEIVE_DIAGNOSTIC;
1164 cdb[1] = 1; /* Set page code valid bit */
1167 cdb[0] = SEND_DIAGNOSTIC;
1171 cdb[3] = bufsiz >> 8; /* high bits */
1172 cdb[4] = bufsiz & 0xff; /* low bits */
1177 * \brief Discover whether this instance supports timed completion of a
1178 * RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1179 * page, and store the result in the softc, updating if necessary.
1181 * \param enc SES instance to query and update.
1182 * \param tc_en Value of timed completion to set (see \return).
1184 * \return 1 if timed completion enabled, 0 otherwise.
1187 ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1190 struct cam_periph *periph;
1191 struct ses_mgmt_mode_page *mgmt;
1193 size_t mode_buf_len;
1196 periph = enc->periph;
1197 ses = enc->enc_private;
1198 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1200 mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1201 mode_buf = ENC_MALLOCZ(mode_buf_len);
1202 if (mode_buf == NULL)
1205 scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1206 /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1207 mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1210 * Ignore illegal request errors, as they are quite common and we
1211 * will print something out in that case anyway.
1213 cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1214 ENC_FLAGS|SF_QUIET_IR, NULL);
1215 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1216 ENC_VLOG(enc, "Timed Completion Unsupported\n");
1220 /* Skip the mode select if the desired value is already set */
1221 mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1222 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1225 /* Value is not what we wanted, set it */
1227 mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1229 mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1230 /* SES2r20: a completion time of zero means as long as possible */
1231 bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1233 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1234 /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1235 SSD_FULL_SIZE, /*timeout*/60 * 1000);
1237 cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1238 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1239 ENC_VLOG(enc, "Timed Completion Set Failed\n");
1244 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1245 ENC_LOG(enc, "Timed Completion Enabled\n");
1246 ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1248 ENC_LOG(enc, "Timed Completion Disabled\n");
1249 ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1253 xpt_release_ccb(ccb);
1255 return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1259 * \brief Process the list of supported pages and update flags.
1261 * \param enc SES device to query.
1262 * \param buf Buffer containing the config page.
1263 * \param xfer_len Length of the config page in the buffer.
1265 * \return 0 on success, errno otherwise.
1268 ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1269 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1272 struct scsi_diag_page *page;
1275 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1276 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1277 ses = enc->enc_private;
1284 if (xfer_len < sizeof(*page)) {
1285 ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1289 page = (struct scsi_diag_page *)*bufp;
1290 length = scsi_2btoul(page->length);
1291 if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1292 ENC_VLOG(enc, "Diag Pages List Too Long\n");
1295 ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1296 __func__, length, xfer_len);
1299 for (i = 0; i < length; i++) {
1300 if (page->params[i] == SesElementDescriptor)
1301 ses->ses_flags |= SES_FLAG_DESC;
1302 else if (page->params[i] == SesAddlElementStatus)
1303 ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1307 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1312 * \brief Process the config page and update associated structures.
1314 * \param enc SES device to query.
1315 * \param buf Buffer containing the config page.
1316 * \param xfer_len Length of the config page in the buffer.
1318 * \return 0 on success, errno otherwise.
1321 ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1322 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1324 struct ses_iterator iter;
1326 enc_cache_t *enc_cache;
1327 ses_cache_t *ses_cache;
1333 struct ses_cfg_page *cfg_page;
1334 struct ses_enc_desc *buf_subenc;
1335 const struct ses_enc_desc **subencs;
1336 const struct ses_enc_desc **cur_subenc;
1337 const struct ses_enc_desc **last_subenc;
1338 ses_type_t *ses_types;
1339 ses_type_t *sestype;
1340 const struct ses_elm_type_desc *cur_buf_type;
1341 const struct ses_elm_type_desc *last_buf_type;
1342 uint8_t *last_valid_byte;
1343 enc_element_t *element;
1344 const char *type_text;
1346 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1347 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1348 ses = enc->enc_private;
1349 enc_cache = &enc->enc_daemon_cache;
1350 ses_cache = enc_cache->private;
1358 if (xfer_len < sizeof(cfg_page->hdr)) {
1359 ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1364 cfg_page = (struct ses_cfg_page *)buf;
1365 length = ses_page_length(&cfg_page->hdr);
1366 if (length > xfer_len) {
1367 ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1370 last_valid_byte = &buf[length - 1];
1372 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1373 __func__, length, xfer_len);
1376 if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1378 /* Our cache is still valid. Proceed to fetching status. */
1382 /* Cache is no longer valid. Free old data to make way for new. */
1383 ses_cache_free(enc, enc_cache);
1384 ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1385 scsi_4btoul(cfg_page->hdr.gen_code),
1386 ses_cfg_page_get_num_subenc(cfg_page));
1388 /* Take ownership of the buffer. */
1389 ses_cache->cfg_page = cfg_page;
1393 * Now waltz through all the subenclosures summing the number of
1394 * types available in each.
1396 subencs = malloc(ses_cfg_page_get_num_subenc(cfg_page)
1397 * sizeof(*subencs), M_SCSIENC, M_WAITOK|M_ZERO);
1399 * Sub-enclosure data is const after construction (i.e. when
1400 * accessed via our cache object.
1402 * The cast here is not required in C++ but C99 is not so
1403 * sophisticated (see C99 6.5.16.1(1)).
1405 ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1406 ses_cache->subencs = subencs;
1408 buf_subenc = cfg_page->subencs;
1409 cur_subenc = subencs;
1410 last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1412 while (cur_subenc <= last_subenc) {
1414 if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1415 ENC_VLOG(enc, "Enclosure %d Beyond End of "
1416 "Descriptors\n", cur_subenc - subencs);
1421 ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1422 "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1423 buf_subenc->num_types, buf_subenc->length,
1424 &buf_subenc->byte0 - buf);
1425 ENC_VLOG(enc, "WWN: %jx\n",
1426 (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1428 ntype += buf_subenc->num_types;
1429 *cur_subenc = buf_subenc;
1431 buf_subenc = ses_enc_desc_next(buf_subenc);
1434 /* Process the type headers. */
1435 ses_types = malloc(ntype * sizeof(*ses_types),
1436 M_SCSIENC, M_WAITOK|M_ZERO);
1438 * Type data is const after construction (i.e. when accessed via
1441 ses_cache->ses_ntypes = ntype;
1442 ses_cache->ses_types = ses_types;
1444 cur_buf_type = (const struct ses_elm_type_desc *)
1445 (&(*last_subenc)->length + (*last_subenc)->length + 1);
1446 last_buf_type = cur_buf_type + ntype - 1;
1447 type_text = (const uint8_t *)(last_buf_type + 1);
1449 sestype = ses_types;
1450 while (cur_buf_type <= last_buf_type) {
1451 if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1452 ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1453 sestype - ses_types);
1457 sestype->hdr = cur_buf_type;
1458 sestype->text = type_text;
1459 type_text += cur_buf_type->etype_txt_len;
1460 ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1461 "%d, Text Length %d: %.*s\n", sestype - ses_types,
1462 sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1463 sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1464 sestype->hdr->etype_txt_len, sestype->text);
1466 nelm += sestype->hdr->etype_maxelt
1467 + /*overall status element*/1;
1472 /* Create the object map. */
1473 enc_cache->elm_map = malloc(nelm * sizeof(enc_element_t),
1474 M_SCSIENC, M_WAITOK|M_ZERO);
1475 enc_cache->nelms = nelm;
1477 ses_iter_init(enc, enc_cache, &iter);
1478 while ((element = ses_iter_next(&iter)) != NULL) {
1479 const struct ses_elm_type_desc *thdr;
1481 ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1482 iter.global_element_index, iter.type_index, nelm,
1483 iter.type_element_index);
1484 thdr = ses_cache->ses_types[iter.type_index].hdr;
1485 element->elm_idx = iter.global_element_index;
1486 element->elm_type = thdr->etype_elm_type;
1487 element->subenclosure = thdr->etype_subenc;
1488 element->type_elm_idx = iter.type_element_index;
1489 element->elm_private = malloc(sizeof(ses_element_t),
1490 M_SCSIENC, M_WAITOK|M_ZERO);
1491 ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1492 "type 0x%x\n", __func__, iter.global_element_index,
1493 iter.type_index, iter.type_element_index,
1494 thdr->etype_subenc, thdr->etype_elm_type);
1501 ses_cache_free(enc, enc_cache);
1503 ses_poll_status(enc);
1504 enc_update_request(enc, SES_PUBLISH_CACHE);
1506 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1511 * \brief Update the status page and associated structures.
1513 * \param enc SES softc to update for.
1514 * \param buf Buffer containing the status page.
1515 * \param bufsz Amount of data in the buffer.
1517 * \return 0 on success, errno otherwise.
1520 ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1521 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1523 struct ses_iterator iter;
1524 enc_element_t *element;
1526 enc_cache_t *enc_cache;
1527 ses_cache_t *ses_cache;
1531 struct ses_status_page *page;
1532 union ses_status_element *cur_stat;
1533 union ses_status_element *last_stat;
1535 ses = enc->enc_private;
1536 enc_cache = &enc->enc_daemon_cache;
1537 ses_cache = enc_cache->private;
1540 ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1541 page = (struct ses_status_page *)buf;
1542 length = ses_page_length(&page->hdr);
1549 * Make sure the length fits in the buffer.
1551 * XXX all this means is that the page is larger than the space
1552 * we allocated. Since we use a statically sized buffer, this
1553 * could happen... Need to use dynamic discovery of the size.
1555 if (length > xfer_len) {
1556 ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1560 /* Check for simple enclosure reporting short enclosure status. */
1561 if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1562 ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1563 ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1564 ses_cache_free(enc, enc_cache);
1565 enc_cache->enc_status = page->hdr.page_specific_flags;
1566 enc_update_request(enc, SES_PUBLISH_CACHE);
1571 /* Make sure the length contains at least one header and status */
1572 if (length < (sizeof(*page) + sizeof(*page->elements))) {
1573 ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1577 if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1578 ENC_DLOG(enc, "%s: Generation count change detected\n",
1580 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1584 ses_cache_free_status(enc, enc_cache);
1585 ses_cache->status_page = page;
1588 enc_cache->enc_status = page->hdr.page_specific_flags;
1591 * Read in individual element status. The element order
1592 * matches the order reported in the config page (i.e. the
1593 * order of an unfiltered iteration of the config objects)..
1595 ses_iter_init(enc, enc_cache, &iter);
1596 cur_stat = page->elements;
1597 last_stat = (union ses_status_element *)
1598 &buf[length - sizeof(*last_stat)];
1599 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1600 __func__, length, xfer_len);
1601 while (cur_stat <= last_stat
1602 && (element = ses_iter_next(&iter)) != NULL) {
1604 ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1605 __func__, iter.global_element_index, iter.type_index,
1606 iter.type_element_index, (uint8_t *)cur_stat - buf,
1607 scsi_4btoul(cur_stat->bytes));
1609 memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1610 element->svalid = 1;
1614 if (ses_iter_next(&iter) != NULL) {
1615 ENC_VLOG(enc, "Status page, length insufficient for "
1616 "expected number of objects\n");
1618 if (cur_stat <= last_stat)
1619 ENC_VLOG(enc, "Status page, exhausted objects before "
1620 "exhausing page\n");
1621 enc_update_request(enc, SES_PUBLISH_CACHE);
1625 ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1631 * The enclosure should not provide additional element
1632 * status for this element type in page 0x0A.
1634 * \note This status is returned for any types not
1635 * listed SES3r02. Further types added in a
1636 * future specification will be incorrectly
1639 TYPE_ADDLSTATUS_NONE,
1642 * The element type provides additional element status
1645 TYPE_ADDLSTATUS_MANDATORY,
1648 * The element type may provide additional element status
1649 * in page 0x0A, but i
1651 TYPE_ADDLSTATUS_OPTIONAL
1652 } ses_addlstatus_avail_t;
1655 * \brief Check to see whether a given type (as obtained via type headers) is
1656 * supported by the additional status command.
1658 * \param enc SES softc to check.
1659 * \param typidx Type index to check for.
1661 * \return An enumeration indicating if additional status is mandatory,
1662 * optional, or not required for this type.
1664 static ses_addlstatus_avail_t
1665 ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1667 enc_cache_t *enc_cache;
1668 ses_cache_t *ses_cache;
1670 enc_cache = &enc->enc_daemon_cache;
1671 ses_cache = enc_cache->private;
1672 switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1674 case ELMTYP_ARRAY_DEV:
1675 case ELMTYP_SAS_EXP:
1676 return (TYPE_ADDLSTATUS_MANDATORY);
1677 case ELMTYP_SCSI_INI:
1678 case ELMTYP_SCSI_TGT:
1680 return (TYPE_ADDLSTATUS_OPTIONAL);
1682 /* No additional status information available. */
1685 return (TYPE_ADDLSTATUS_NONE);
1688 static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1690 static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1691 int, int, int, int);
1692 static int ses_get_elm_addlstatus_ata(enc_softc_t *, enc_cache_t *, uint8_t *,
1693 int, int, int, int);
1696 * \brief Parse the additional status element data for each object.
1698 * \param enc The SES softc to update.
1699 * \param buf The buffer containing the additional status
1701 * \param xfer_len Size of the buffer.
1703 * \return 0 on success, errno otherwise.
1706 ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1707 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1709 struct ses_iterator iter, titer;
1714 enc_cache_t *enc_cache;
1715 ses_cache_t *ses_cache;
1717 ses_element_t *elmpriv;
1718 const struct ses_page_hdr *hdr;
1719 enc_element_t *element, *telement;
1721 enc_cache = &enc->enc_daemon_cache;
1722 ses_cache = enc_cache->private;
1730 ses_cache_free_elm_addlstatus(enc, enc_cache);
1731 ses_cache->elm_addlstatus_page =
1732 (struct ses_addl_elem_status_page *)buf;
1736 * The objects appear in the same order here as in Enclosure Status,
1737 * which itself is ordered by the Type Descriptors from the Config
1738 * page. However, it is necessary to skip elements that are not
1739 * supported by this page when counting them.
1741 hdr = &ses_cache->elm_addlstatus_page->hdr;
1742 length = ses_page_length(hdr);
1743 ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1744 /* Make sure the length includes at least one header. */
1745 if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1746 ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1749 if (length > xfer_len) {
1750 ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1754 if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1755 ENC_DLOG(enc, "%s: Generation count change detected\n",
1757 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1761 offset = sizeof(struct ses_page_hdr);
1762 ses_iter_init(enc, enc_cache, &iter);
1763 while (offset < length
1764 && (element = ses_iter_next(&iter)) != NULL) {
1765 struct ses_elm_addlstatus_base_hdr *elm_hdr;
1767 ses_addlstatus_avail_t status_type;
1770 * Additional element status is only provided for
1771 * individual elements (i.e. overal status elements
1772 * are excluded) and those of the types specified
1775 status_type = ses_typehasaddlstatus(enc, iter.type_index);
1776 if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1777 || status_type == TYPE_ADDLSTATUS_NONE)
1780 elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1781 eip = ses_elm_addlstatus_eip(elm_hdr);
1783 struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1784 int expected_index, index;
1785 ses_elem_index_type_t index_type;
1787 eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1788 if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) {
1789 index_type = SES_ELEM_INDEX_GLOBAL;
1790 expected_index = iter.global_element_index;
1792 index_type = SES_ELEM_INDEX_INDIVIDUAL;
1793 expected_index = iter.individual_element_index;
1795 if (eip_hdr->element_index < expected_index) {
1796 ENC_VLOG(enc, "%s: provided %selement index "
1797 "%d is lower then expected %d\n",
1798 __func__, (eip_hdr->byte2 &
1799 SES_ADDL_EIP_EIIOE) ? "global " : "",
1800 eip_hdr->element_index, expected_index);
1804 telement = ses_iter_seek_to(&titer,
1805 eip_hdr->element_index, index_type);
1806 if (telement == NULL) {
1807 ENC_VLOG(enc, "%s: provided %selement index "
1808 "%d does not exist\n", __func__,
1809 (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) ?
1810 "global " : "", eip_hdr->element_index);
1813 if (ses_typehasaddlstatus(enc, titer.type_index) ==
1814 TYPE_ADDLSTATUS_NONE) {
1815 ENC_VLOG(enc, "%s: provided %selement index "
1816 "%d can't have additional status\n",
1818 (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) ?
1819 "global " : "", eip_hdr->element_index);
1822 * If we expected mandatory element, we may
1823 * guess it was just a wrong index and we may
1824 * use the status. If element was optional,
1825 * then we have no idea where status belongs.
1827 if (status_type == TYPE_ADDLSTATUS_OPTIONAL)
1834 if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE)
1835 index = iter.global_element_index;
1837 index = iter.individual_element_index;
1838 if (index > expected_index
1839 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1840 ENC_VLOG(enc, "%s: provided %s element"
1841 "index %d skips mandatory status "
1842 " element at index %d\n",
1843 __func__, (eip_hdr->byte2 &
1844 SES_ADDL_EIP_EIIOE) ? "global " : "",
1845 index, expected_index);
1848 elmpriv = element->elm_private;
1849 ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1850 "type element index=%d, offset=0x%x, "
1851 "byte0=0x%x, length=0x%x\n", __func__,
1852 iter.global_element_index, iter.type_index,
1853 iter.type_element_index, offset, elm_hdr->byte0,
1856 /* Skip to after the length field */
1857 offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1859 /* Make sure the descriptor is within bounds */
1860 if ((offset + elm_hdr->length) > length) {
1861 ENC_VLOG(enc, "Element %d Beyond End "
1862 "of Additional Element Status Descriptors\n",
1863 iter.global_element_index);
1867 /* Skip elements marked as invalid. */
1868 if (ses_elm_addlstatus_invalid(elm_hdr)) {
1869 offset += elm_hdr->length;
1872 elmpriv->addl.hdr = elm_hdr;
1874 /* Advance to the protocol data, skipping eip bytes if needed */
1875 offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1876 proto_info_len = elm_hdr->length
1877 - (eip * SES_EIP_HDR_EXTRA_LEN);
1879 /* Errors in this block are ignored as they are non-fatal */
1880 switch(ses_elm_addlstatus_proto(elm_hdr)) {
1882 if (elm_hdr->length == 0)
1884 ses_get_elm_addlstatus_fc(enc, enc_cache,
1885 &buf[offset], proto_info_len);
1887 case SPSP_PROTO_SAS:
1888 if (elm_hdr->length <= 2)
1890 ses_get_elm_addlstatus_sas(enc, enc_cache,
1893 eip, iter.type_index,
1894 iter.global_element_index);
1896 case SPSP_PROTO_ATA:
1897 ses_get_elm_addlstatus_ata(enc, enc_cache,
1900 eip, iter.type_index,
1901 iter.global_element_index);
1904 ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1905 "Protocol 0x%x\n", iter.global_element_index,
1906 ses_elm_addlstatus_proto(elm_hdr));
1910 offset += proto_info_len;
1915 ses_cache_free_elm_addlstatus(enc, enc_cache);
1916 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1917 enc_update_request(enc, SES_PUBLISH_CACHE);
1922 ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1923 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1927 ses = enc->enc_private;
1930 * o Generation count wrong.
1931 * o Some SCSI status error.
1933 ses_terminate_control_requests(&ses->ses_pending_requests, error);
1934 ses_poll_status(enc);
1939 ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1940 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1942 struct ses_iterator iter;
1943 enc_cache_t *enc_cache;
1944 enc_element_t *element;
1946 enc_cache = &enc->enc_daemon_cache;
1948 ses_iter_init(enc, enc_cache, &iter);
1949 while ((element = ses_iter_next(&iter)) != NULL) {
1951 * ses_set_physpath() returns success if we changed
1952 * the physpath of any element. This allows us to
1953 * only announce devices once regardless of how
1954 * many times we process additional element status.
1956 if (ses_set_physpath(enc, element, &iter) == 0)
1957 ses_print_addl_data(enc, element);
1964 ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1965 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1968 sx_xlock(&enc->enc_cache_lock);
1969 ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1970 /*dst*/&enc->enc_cache);
1971 sx_xunlock(&enc->enc_cache_lock);
1977 * \brief Parse the descriptors for each object.
1979 * \param enc The SES softc to update.
1980 * \param buf The buffer containing the descriptor list response.
1981 * \param xfer_len Size of the buffer.
1983 * \return 0 on success, errno otherwise.
1986 ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1987 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1990 struct ses_iterator iter;
1991 enc_element_t *element;
1994 u_long length, plength;
1995 enc_cache_t *enc_cache;
1996 ses_cache_t *ses_cache;
1998 ses_element_t *elmpriv;
1999 const struct ses_page_hdr *phdr;
2000 const struct ses_elm_desc_hdr *hdr;
2002 ses = enc->enc_private;
2003 enc_cache = &enc->enc_daemon_cache;
2004 ses_cache = enc_cache->private;
2012 ses_cache_free_elm_descs(enc, enc_cache);
2013 ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
2016 phdr = &ses_cache->elm_descs_page->hdr;
2017 plength = ses_page_length(phdr);
2018 if (xfer_len < sizeof(struct ses_page_hdr)) {
2019 ENC_VLOG(enc, "Runt Element Descriptor Page\n");
2022 if (plength > xfer_len) {
2023 ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
2027 if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
2028 ENC_VLOG(enc, "%s: Generation count change detected\n",
2030 enc_update_request(enc, SES_UPDATE_GETCONFIG);
2034 offset = sizeof(struct ses_page_hdr);
2036 ses_iter_init(enc, enc_cache, &iter);
2037 while (offset < plength
2038 && (element = ses_iter_next(&iter)) != NULL) {
2040 if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
2041 ENC_VLOG(enc, "Element %d Descriptor Header Past "
2042 "End of Buffer\n", iter.global_element_index);
2045 hdr = (struct ses_elm_desc_hdr *)&buf[offset];
2046 length = scsi_2btoul(hdr->length);
2047 ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
2048 iter.global_element_index, iter.type_index,
2049 iter.type_element_index, length, offset);
2050 if ((offset + sizeof(*hdr) + length) > plength) {
2051 ENC_VLOG(enc, "Element%d Descriptor Past "
2052 "End of Buffer\n", iter.global_element_index);
2055 offset += sizeof(*hdr);
2058 elmpriv = element->elm_private;
2059 elmpriv->descr_len = length;
2060 elmpriv->descr = &buf[offset];
2063 /* skip over the descriptor itself */
2070 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2071 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2073 enc_update_request(enc, SES_PUBLISH_CACHE);
2078 ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2079 union ccb *ccb, uint8_t *buf)
2082 if (enc->enc_type == ENC_SEMB_SES) {
2083 semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2084 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2085 state->page_code, buf, state->buf_size,
2088 scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2089 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2090 state->page_code, buf, state->buf_size,
2091 SSD_FULL_SIZE, state->timeout);
2097 * \brief Encode the object status into the response buffer, which is
2098 * expected to contain the current enclosure status. This function
2099 * turns off all the 'select' bits for the objects except for the
2100 * object specified, then sends it back to the enclosure.
2102 * \param enc SES enclosure the change is being applied to.
2103 * \param buf Buffer containing the current enclosure status response.
2104 * \param amt Length of the response in the buffer.
2105 * \param req The control request to be applied to buf.
2107 * \return 0 on success, errno otherwise.
2110 ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2112 struct ses_iterator iter;
2113 enc_element_t *element;
2115 struct ses_control_page_hdr *hdr;
2117 ses_iter_init(enc, &enc->enc_cache, &iter);
2118 hdr = (struct ses_control_page_hdr *)buf;
2119 if (req->elm_idx == -1) {
2120 /* for enclosure status, at least 2 bytes are needed */
2123 hdr->control_flags =
2124 req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2125 ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2129 element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2130 if (element == NULL)
2134 * Seek to the type set that corresponds to the requested object.
2135 * The +1 is for the overall status element for the type.
2137 offset = sizeof(struct ses_control_page_hdr)
2138 + (iter.global_element_index * sizeof(struct ses_comstat));
2140 /* Check for buffer overflow. */
2141 if (offset + sizeof(struct ses_comstat) > amt)
2144 /* Set the status. */
2145 memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2147 ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2148 iter.type_index, iter.global_element_index, offset,
2149 req->elm_stat.comstatus, req->elm_stat.comstat[0],
2150 req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2156 ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2157 union ccb *ccb, uint8_t *buf)
2160 enc_cache_t *enc_cache;
2161 ses_cache_t *ses_cache;
2162 struct ses_control_page_hdr *hdr;
2163 ses_control_request_t *req;
2167 ses = enc->enc_private;
2168 enc_cache = &enc->enc_daemon_cache;
2169 ses_cache = enc_cache->private;
2170 hdr = (struct ses_control_page_hdr *)buf;
2172 if (ses_cache->status_page == NULL) {
2173 ses_terminate_control_requests(&ses->ses_requests, EIO);
2177 plength = ses_page_length(&ses_cache->status_page->hdr);
2178 memcpy(buf, ses_cache->status_page, plength);
2180 /* Disable the select bits in all status entries. */
2181 offset = sizeof(struct ses_control_page_hdr);
2182 for (offset = sizeof(struct ses_control_page_hdr);
2183 offset < plength; offset += sizeof(struct ses_comstat)) {
2184 buf[offset] &= ~SESCTL_CSEL;
2187 /* And make sure the INVOP bit is clear. */
2188 hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2190 /* Apply incoming requests. */
2191 while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2193 TAILQ_REMOVE(&ses->ses_requests, req, links);
2194 req->result = ses_encode(enc, buf, plength, req);
2195 if (req->result != 0) {
2199 TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2202 if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2205 /* Fill out the ccb */
2206 if (enc->enc_type == ENC_SEMB_SES) {
2207 semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2209 buf, ses_page_length(&ses_cache->status_page->hdr),
2212 scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2213 MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2214 /*device_offline*/0, /*self_test*/0,
2215 /*page_format*/1, /*self_test_code*/0,
2216 buf, ses_page_length(&ses_cache->status_page->hdr),
2217 SSD_FULL_SIZE, state->timeout);
2223 ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2224 uint8_t *buf, int bufsiz)
2226 ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2230 #define SES_PRINT_PORTS(p, type) do { \
2231 if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) != 0) { \
2232 sbuf_printf(sbp, " %s (", type); \
2233 if ((p) & SES_SASOBJ_DEV_PHY_SMP) \
2234 sbuf_printf(sbp, " SMP"); \
2235 if ((p) & SES_SASOBJ_DEV_PHY_STP) \
2236 sbuf_printf(sbp, " STP"); \
2237 if ((p) & SES_SASOBJ_DEV_PHY_SSP) \
2238 sbuf_printf(sbp, " SSP"); \
2239 sbuf_printf(sbp, " )"); \
2244 * \brief Print the additional element status data for this object, for SAS
2245 * type 0 objects. See SES2 r20 Section 6.1.13.3.2.
2247 * \param sesname SES device name associated with the object.
2248 * \param sbp Sbuf to print to.
2249 * \param obj The object to print the data for.
2252 ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2256 ses_element_t *elmpriv;
2257 struct ses_addl_status *addl;
2258 struct ses_elm_sas_device_phy *phy;
2260 elmpriv = obj->elm_private;
2261 addl = &(elmpriv->addl);
2262 sbuf_printf(sbp, ", SAS Slot: %d%s phys",
2263 addl->proto_hdr.sas->base_hdr.num_phys,
2264 ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas) ? "+" : "");
2265 if (ses_elm_addlstatus_eip(addl->hdr))
2266 sbuf_printf(sbp, " at slot %d",
2267 addl->proto_hdr.sas->type0_eip.dev_slot_num);
2268 sbuf_printf(sbp, "\n");
2269 if (addl->proto_data.sasdev_phys == NULL)
2271 for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2272 phy = &addl->proto_data.sasdev_phys[i];
2273 sbuf_printf(sbp, "%s: phy %d:", sesname, i);
2274 if (ses_elm_sas_dev_phy_sata_dev(phy))
2275 /* Spec says all other fields are specific values */
2276 sbuf_printf(sbp, " SATA device\n");
2278 sbuf_printf(sbp, " SAS device type %d phy %d",
2279 ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2280 SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2281 SES_PRINT_PORTS(phy->target_ports, "Target");
2282 sbuf_printf(sbp, "\n");
2284 sbuf_printf(sbp, "%s: phy %d: parent %jx addr %jx\n",
2286 (uintmax_t)scsi_8btou64(phy->parent_addr),
2287 (uintmax_t)scsi_8btou64(phy->phy_addr));
2290 #undef SES_PRINT_PORTS
2293 * \brief Print the additional element status data for this object, for SAS
2294 * type 1 objects. See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2296 * \param sesname SES device name associated with the object.
2297 * \param sbp Sbuf to print to.
2298 * \param obj The object to print the data for.
2301 ses_print_addl_data_sas_type1(char *sesname, struct sbuf *sbp,
2305 ses_element_t *elmpriv;
2306 struct ses_addl_status *addl;
2307 struct ses_elm_sas_expander_phy *exp_phy;
2308 struct ses_elm_sas_port_phy *port_phy;
2310 elmpriv = obj->elm_private;
2311 addl = &(elmpriv->addl);
2312 sbuf_printf(sbp, ", SAS ");
2313 if (obj->elm_type == ELMTYP_SAS_EXP) {
2314 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2315 sbuf_printf(sbp, "Expander: %d phys", num_phys);
2316 if (addl->proto_data.sasexp_phys == NULL)
2318 for (i = 0;i < num_phys;i++) {
2319 exp_phy = &addl->proto_data.sasexp_phys[i];
2320 sbuf_printf(sbp, "%s: phy %d: connector %d other %d\n",
2321 sesname, i, exp_phy->connector_index,
2322 exp_phy->other_index);
2325 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2326 sbuf_printf(sbp, "Port: %d phys", num_phys);
2327 if (addl->proto_data.sasport_phys == NULL)
2329 for (i = 0;i < num_phys;i++) {
2330 port_phy = &addl->proto_data.sasport_phys[i];
2332 "%s: phy %d: id %d connector %d other %d\n",
2333 sesname, i, port_phy->phy_id,
2334 port_phy->connector_index, port_phy->other_index);
2335 sbuf_printf(sbp, "%s: phy %d: addr %jx\n", sesname, i,
2336 (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2342 * \brief Print the additional element status data for this object, for
2345 * \param sbp Sbuf to print to.
2346 * \param obj The object to print the data for.
2349 ses_print_addl_data_ata(struct sbuf *sbp, enc_element_t *obj)
2351 ses_element_t *elmpriv = obj->elm_private;
2352 struct ses_addl_status *addl = &elmpriv->addl;
2353 struct ses_elm_ata_hdr *ata = addl->proto_hdr.ata;
2355 sbuf_printf(sbp, ", SATA Slot: scbus%d target %d\n",
2356 scsi_4btoul(ata->bus), scsi_4btoul(ata->target));
2360 * \brief Print the additional element status data for this object.
2362 * \param enc SES softc associated with the object.
2363 * \param obj The object to print the data for.
2366 ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2368 ses_element_t *elmpriv;
2369 struct ses_addl_status *addl;
2370 struct sbuf sesname, name, out;
2372 elmpriv = obj->elm_private;
2373 if (elmpriv == NULL)
2376 addl = &(elmpriv->addl);
2377 if (addl->hdr == NULL)
2380 sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2381 sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2382 sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2383 ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2384 if (sbuf_len(&name) == 0)
2385 sbuf_printf(&name, "(none)");
2387 sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2388 enc->periph->unit_number);
2389 sbuf_finish(&sesname);
2390 sbuf_printf(&out, "%s: %s in ", sbuf_data(&sesname), sbuf_data(&name));
2391 if (elmpriv->descr != NULL)
2392 sbuf_printf(&out, "'%s'", elmpriv->descr);
2394 if (obj->elm_type <= ELMTYP_LAST)
2395 sbuf_cat(&out, elm_type_names[obj->elm_type]);
2397 sbuf_printf(&out, "<Type 0x%02x>", obj->elm_type);
2398 sbuf_printf(&out, " %d", obj->type_elm_idx);
2399 if (obj->subenclosure != 0)
2400 sbuf_printf(&out, " of subenc %d", obj->subenclosure);
2402 switch(ses_elm_addlstatus_proto(addl->hdr)) {
2404 goto noaddl; /* stubbed for now */
2405 case SPSP_PROTO_SAS:
2406 if (addl->proto_hdr.sas == NULL)
2408 switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2409 case SES_SASOBJ_TYPE_SLOT:
2410 ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2413 case SES_SASOBJ_TYPE_OTHER:
2414 ses_print_addl_data_sas_type1(sbuf_data(&sesname),
2421 case SPSP_PROTO_ATA:
2422 if (addl->proto_hdr.ata == NULL)
2424 ses_print_addl_data_ata(&out, obj);
2428 sbuf_cat(&out, "\n");
2432 printf("%s", sbuf_data(&out));
2435 sbuf_delete(&sesname);
2439 * \brief Update the softc with the additional element status data for this
2440 * object, for SAS type 0 objects.
2442 * \param enc SES softc to be updated.
2443 * \param buf The additional element status response buffer.
2444 * \param bufsiz Size of the response buffer.
2445 * \param eip The EIP bit value.
2446 * \param nobj Number of objects attached to the SES softc.
2448 * \return 0 on success, errno otherwise.
2451 ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2452 uint8_t *buf, int bufsiz, int eip, int nobj)
2454 int err, offset, physz;
2456 ses_element_t *elmpriv;
2457 struct ses_addl_status *addl;
2461 /* basic object setup */
2462 obj = &(enc_cache->elm_map[nobj]);
2463 elmpriv = obj->elm_private;
2464 addl = &(elmpriv->addl);
2466 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2468 /* Don't assume this object has any phys */
2469 bzero(&addl->proto_data, sizeof(addl->proto_data));
2470 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2473 /* Skip forward to the phy list */
2475 offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2477 offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2479 /* Make sure the phy list fits in the buffer */
2480 physz = addl->proto_hdr.sas->base_hdr.num_phys;
2481 physz *= sizeof(struct ses_elm_sas_device_phy);
2482 if (physz > (bufsiz - offset + 4)) {
2483 ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2489 /* Point to the phy list */
2490 addl->proto_data.sasdev_phys =
2491 (struct ses_elm_sas_device_phy *)&buf[offset];
2498 * \brief Update the softc with the additional element status data for this
2499 * object, for SAS type 1 objects.
2501 * \param enc SES softc to be updated.
2502 * \param buf The additional element status response buffer.
2503 * \param bufsiz Size of the response buffer.
2504 * \param eip The EIP bit value.
2505 * \param nobj Number of objects attached to the SES softc.
2507 * \return 0 on success, errno otherwise.
2510 ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2511 uint8_t *buf, int bufsiz, int eip, int nobj)
2513 int err, offset, physz;
2515 ses_element_t *elmpriv;
2516 struct ses_addl_status *addl;
2520 /* basic object setup */
2521 obj = &(enc_cache->elm_map[nobj]);
2522 elmpriv = obj->elm_private;
2523 addl = &(elmpriv->addl);
2525 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2527 /* Don't assume this object has any phys */
2528 bzero(&addl->proto_data, sizeof(addl->proto_data));
2529 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2532 /* Process expanders differently from other type1 cases */
2533 if (obj->elm_type == ELMTYP_SAS_EXP) {
2534 offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2535 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2536 sizeof(struct ses_elm_sas_expander_phy);
2537 if (physz > (bufsiz - offset)) {
2538 ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2539 "End Of Buffer\n", nobj);
2543 addl->proto_data.sasexp_phys =
2544 (struct ses_elm_sas_expander_phy *)&buf[offset];
2546 offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2547 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2548 sizeof(struct ses_elm_sas_port_phy);
2549 if (physz > (bufsiz - offset + 4)) {
2550 ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2551 "Of Buffer\n", nobj);
2555 addl->proto_data.sasport_phys =
2556 (struct ses_elm_sas_port_phy *)&buf[offset];
2564 * \brief Update the softc with the additional element status data for this
2565 * object, for SAS objects.
2567 * \param enc SES softc to be updated.
2568 * \param buf The additional element status response buffer.
2569 * \param bufsiz Size of the response buffer.
2570 * \param eip The EIP bit value.
2571 * \param tidx Type index for this object.
2572 * \param nobj Number of objects attached to the SES softc.
2574 * \return 0 on success, errno otherwise.
2577 ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2578 uint8_t *buf, int bufsiz, int eip, int tidx,
2582 ses_cache_t *ses_cache;
2583 union ses_elm_sas_hdr *hdr;
2585 /* Need to be able to read the descriptor type! */
2586 if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2591 ses_cache = enc_cache->private;
2593 hdr = (union ses_elm_sas_hdr *)buf;
2594 dtype = ses_elm_sas_descr_type(hdr);
2596 case SES_SASOBJ_TYPE_SLOT:
2597 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2599 case ELMTYP_ARRAY_DEV:
2602 ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2603 "invalid for SES element type 0x%x\n", nobj,
2604 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2608 err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2612 case SES_SASOBJ_TYPE_OTHER:
2613 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2614 case ELMTYP_SAS_EXP:
2615 case ELMTYP_SCSI_INI:
2616 case ELMTYP_SCSI_TGT:
2620 ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2621 "invalid for SES element type 0x%x\n", nobj,
2622 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2626 err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2630 ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2631 "of unknown type 0x%x\n", nobj,
2632 ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2642 * \brief Update the softc with the additional element status data for this
2643 * object, for ATA objects.
2645 * \param enc SES softc to be updated.
2646 * \param buf The additional element status response buffer.
2647 * \param bufsiz Size of the response buffer.
2648 * \param eip The EIP bit value.
2649 * \param tidx Type index for this object.
2650 * \param nobj Number of objects attached to the SES softc.
2652 * \return 0 on success, errno otherwise.
2655 ses_get_elm_addlstatus_ata(enc_softc_t *enc, enc_cache_t *enc_cache,
2656 uint8_t *buf, int bufsiz, int eip, int tidx,
2660 ses_cache_t *ses_cache;
2662 if (bufsiz < sizeof(struct ses_elm_ata_hdr)) {
2667 ses_cache = enc_cache->private;
2668 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2670 case ELMTYP_ARRAY_DEV:
2673 ENC_VLOG(enc, "Element %d has Additional Status, "
2674 "invalid for SES element type 0x%x\n", nobj,
2675 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2680 ((ses_element_t *)enc_cache->elm_map[nobj].elm_private)
2681 ->addl.proto_hdr.ata = (struct ses_elm_ata_hdr *)buf;
2689 ses_softc_invalidate(enc_softc_t *enc)
2693 ses = enc->enc_private;
2694 ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2698 ses_softc_cleanup(enc_softc_t *enc)
2701 ses_cache_free(enc, &enc->enc_cache);
2702 ses_cache_free(enc, &enc->enc_daemon_cache);
2703 ENC_FREE_AND_NULL(enc->enc_private);
2704 ENC_FREE_AND_NULL(enc->enc_cache.private);
2705 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2709 ses_init_enc(enc_softc_t *enc)
2715 ses_get_enc_status(enc_softc_t *enc, int slpflag)
2717 /* Automatically updated, caller checks enc_cache->encstat itself */
2722 ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2724 ses_control_request_t req;
2727 ses = enc->enc_private;
2728 req.elm_idx = SES_SETSTATUS_ENC_IDX;
2729 req.elm_stat.comstatus = encstat & 0xf;
2731 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2732 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2733 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2735 return (req.result);
2739 ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2741 unsigned int i = elms->elm_idx;
2743 memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2748 ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2750 ses_control_request_t req;
2753 /* If this is clear, we don't do diddly. */
2754 if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2757 ses = enc->enc_private;
2758 req.elm_idx = elms->elm_idx;
2759 memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2761 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2762 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2763 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2765 return (req.result);
2769 ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2771 int i = (int)elmd->elm_idx;
2772 ses_element_t *elmpriv;
2774 /* Assume caller has already checked obj_id validity */
2775 elmpriv = enc->enc_cache.elm_map[i].elm_private;
2776 /* object might not have a descriptor */
2777 if (elmpriv == NULL || elmpriv->descr == NULL) {
2778 elmd->elm_desc_len = 0;
2781 if (elmd->elm_desc_len > elmpriv->descr_len)
2782 elmd->elm_desc_len = elmpriv->descr_len;
2783 copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2788 * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2789 * given object id if one is available.
2791 * \param enc SES softc to examine.
2792 * \param objdn ioctl structure to read/write device name info.
2794 * \return 0 on success, errno otherwise.
2797 ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2802 len = elmdn->elm_names_size;
2806 cam_periph_unlock(enc->periph);
2807 sbuf_new(&sb, NULL, len, SBUF_FIXEDLEN);
2808 ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2809 ses_elmdevname_callback, &sb);
2811 elmdn->elm_names_len = sbuf_len(&sb);
2812 copyout(sbuf_data(&sb), elmdn->elm_devnames, elmdn->elm_names_len + 1);
2814 cam_periph_lock(enc->periph);
2815 return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2819 * \brief Send a string to the primary subenclosure using the String Out
2820 * SES diagnostic page.
2822 * \param enc SES enclosure to run the command on.
2823 * \param sstr SES string structure to operate on
2824 * \param ioc Ioctl being performed
2826 * \return 0 on success, errno otherwise.
2829 ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2832 enc_cache_t *enc_cache;
2833 ses_cache_t *ses_cache;
2834 const struct ses_enc_desc *enc_desc;
2835 int amt, payload, ret;
2844 ses = enc->enc_private;
2845 enc_cache = &enc->enc_daemon_cache;
2846 ses_cache = enc_cache->private;
2848 /* Implement SES2r20 6.1.6 */
2849 if (sstr->bufsiz > 0xffff)
2850 return (EINVAL); /* buffer size too large */
2853 case ENCIOC_SETSTRING:
2854 payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2856 buf = ENC_MALLOC(payload);
2859 ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2860 /* Construct the page request */
2861 buf[0] = SesStringOut;
2863 buf[2] = sstr->bufsiz >> 8;
2864 buf[3] = sstr->bufsiz & 0xff;
2865 memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2867 case ENCIOC_GETSTRING:
2868 payload = sstr->bufsiz;
2870 ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2873 case ENCIOC_GETENCNAME:
2874 if (ses_cache->ses_nsubencs < 1)
2876 enc_desc = ses_cache->subencs[0];
2877 cam_strvis(vendor, enc_desc->vendor_id,
2878 sizeof(enc_desc->vendor_id), sizeof(vendor));
2879 cam_strvis(product, enc_desc->product_id,
2880 sizeof(enc_desc->product_id), sizeof(product));
2881 cam_strvis(rev, enc_desc->product_rev,
2882 sizeof(enc_desc->product_rev), sizeof(rev));
2883 rsize = snprintf(str, sizeof(str), "%s %s %s",
2884 vendor, product, rev) + 1;
2885 if (rsize > sizeof(str))
2886 rsize = sizeof(str);
2887 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2889 if (size > sstr->bufsiz)
2890 size = sstr->bufsiz;
2891 copyout(str, sstr->buf, size);
2892 return (size == rsize ? 0 : ENOMEM);
2893 case ENCIOC_GETENCID:
2894 if (ses_cache->ses_nsubencs < 1)
2896 enc_desc = ses_cache->subencs[0];
2897 rsize = snprintf(str, sizeof(str), "%16jx",
2898 scsi_8btou64(enc_desc->logical_id)) + 1;
2899 if (rsize > sizeof(str))
2900 rsize = sizeof(str);
2901 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2903 if (size > sstr->bufsiz)
2904 size = sstr->bufsiz;
2905 copyout(str, sstr->buf, size);
2906 return (size == rsize ? 0 : ENOMEM);
2910 ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2911 if (ioc == ENCIOC_SETSTRING)
2917 * \invariant Called with cam_periph mutex held.
2920 ses_poll_status(enc_softc_t *enc)
2924 ses = enc->enc_private;
2925 enc_update_request(enc, SES_UPDATE_GETSTATUS);
2926 if (ses->ses_flags & SES_FLAG_DESC)
2927 enc_update_request(enc, SES_UPDATE_GETELMDESCS);
2928 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2929 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2933 * \brief Notification received when CAM detects a new device in the
2934 * SCSI domain in which this SEP resides.
2936 * \param enc SES enclosure instance.
2939 ses_device_found(enc_softc_t *enc)
2941 ses_poll_status(enc);
2942 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2945 static struct enc_vec ses_enc_vec =
2947 .softc_invalidate = ses_softc_invalidate,
2948 .softc_cleanup = ses_softc_cleanup,
2949 .init_enc = ses_init_enc,
2950 .get_enc_status = ses_get_enc_status,
2951 .set_enc_status = ses_set_enc_status,
2952 .get_elm_status = ses_get_elm_status,
2953 .set_elm_status = ses_set_elm_status,
2954 .get_elm_desc = ses_get_elm_desc,
2955 .get_elm_devnames = ses_get_elm_devnames,
2956 .handle_string = ses_handle_string,
2957 .device_found = ses_device_found,
2958 .poll_status = ses_poll_status
2962 * \brief Initialize a new SES instance.
2964 * \param enc SES softc structure to set up the instance in.
2965 * \param doinit Do the initialization (see main driver).
2967 * \return 0 on success, errno otherwise.
2970 ses_softc_init(enc_softc_t *enc)
2972 ses_softc_t *ses_softc;
2974 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2975 ("entering enc_softc_init(%p)\n", enc));
2977 enc->enc_vec = ses_enc_vec;
2978 enc->enc_fsm_states = enc_fsm_states;
2980 if (enc->enc_private == NULL)
2981 enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2982 if (enc->enc_cache.private == NULL)
2983 enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2984 if (enc->enc_daemon_cache.private == NULL)
2985 enc->enc_daemon_cache.private =
2986 ENC_MALLOCZ(sizeof(ses_cache_t));
2988 if (enc->enc_private == NULL
2989 || enc->enc_cache.private == NULL
2990 || enc->enc_daemon_cache.private == NULL) {
2991 ENC_FREE_AND_NULL(enc->enc_private);
2992 ENC_FREE_AND_NULL(enc->enc_cache.private);
2993 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2997 ses_softc = enc->enc_private;
2998 TAILQ_INIT(&ses_softc->ses_requests);
2999 TAILQ_INIT(&ses_softc->ses_pending_requests);
3001 enc_update_request(enc, SES_UPDATE_PAGES);
3003 // XXX: Move this to the FSM so it doesn't hang init
3004 if (0) (void) ses_set_timed_completion(enc, 1);