2 * Copyright (c) 2000 Matthew Jacob
3 * Copyright (c) 2010 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * \file scsi_enc_ses.c
31 * Structures and routines specific && private to SES only
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/ctype.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/queue.h>
48 #include <sys/systm.h>
49 #include <sys/types.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_xpt_periph.h>
54 #include <cam/cam_periph.h>
56 #include <cam/scsi/scsi_message.h>
57 #include <cam/scsi/scsi_enc.h>
58 #include <cam/scsi/scsi_enc_internal.h>
60 /* SES Native Type Device Support */
62 /* SES Diagnostic Page Codes */
64 SesSupportedPages = 0x0,
67 SesStatusPage = SesControlPage,
70 SesStringIn = SesStringOut,
71 SesThresholdOut = 0x5,
72 SesThresholdIn = SesThresholdOut,
73 SesArrayControl = 0x6, /* Obsolete in SES v2 */
74 SesArrayStatus = SesArrayControl,
75 SesElementDescriptor = 0x7,
77 SesEnclosureBusy = 0x9,
78 SesAddlElementStatus = 0xa
81 typedef struct ses_type {
82 const struct ses_elm_type_desc *hdr;
86 typedef struct ses_comstat {
91 typedef union ses_addl_data {
92 struct ses_elm_sas_device_phy *sasdev_phys;
93 struct ses_elm_sas_expander_phy *sasexp_phys;
94 struct ses_elm_sas_port_phy *sasport_phys;
95 struct ses_fcobj_port *fc_ports;
98 typedef struct ses_addl_status {
99 struct ses_elm_addlstatus_base_hdr *hdr;
101 union ses_fcobj_hdr *fc;
102 union ses_elm_sas_hdr *sas;
104 union ses_addl_data proto_data; /* array sizes stored in header */
107 typedef struct ses_element {
108 uint8_t eip; /* eip bit is set */
109 uint16_t descr_len; /* length of the descriptor */
110 char *descr; /* descriptor for this object */
111 struct ses_addl_status addl; /* additional status info */
114 typedef struct ses_control_request {
116 ses_comstat_t elm_stat;
118 TAILQ_ENTRY(ses_control_request) links;
119 } ses_control_request_t;
120 TAILQ_HEAD(ses_control_reqlist, ses_control_request);
121 typedef struct ses_control_reqlist ses_control_reqlist_t;
123 SES_SETSTATUS_ENC_IDX = -1
127 ses_terminate_control_requests(ses_control_reqlist_t *reqlist, int result)
129 ses_control_request_t *req;
131 while ((req = TAILQ_FIRST(reqlist)) != NULL) {
132 TAILQ_REMOVE(reqlist, req, links);
133 req->result = result;
138 enum ses_iter_index_values {
140 * \brief Value of an initialized but invalid index
141 * in a ses_iterator object.
143 * This value is used for the individual_element_index of
144 * overal status elements and for all index types when
145 * an iterator is first initialized.
147 ITERATOR_INDEX_INVALID = -1,
150 * \brief Value of an index in a ses_iterator object
151 * when the iterator has traversed past the last
154 ITERATOR_INDEX_END = INT_MAX
158 * \brief Structure encapsulating all data necessary to traverse the
159 * elements of a SES configuration.
161 * The ses_iterator object simplifies the task of iterating through all
162 * elements detected via the SES configuration page by tracking the numerous
163 * element indexes that, instead of memoizing in the softc, we calculate
164 * on the fly during the traversal of the element objects. The various
165 * indexes are necessary due to the varying needs of matching objects in
166 * the different SES pages. Some pages (e.g. Status/Control) contain all
167 * elements, while others (e.g. Additional Element Status) only contain
168 * individual elements (no overal status elements) of particular types.
170 * To use an iterator, initialize it with ses_iter_init(), and then
171 * use ses_iter_next() to traverse the elements (including the first) in
172 * the configuration. Once an iterator is initiailized with ses_iter_init(),
173 * you may also seek to any particular element by either it's global or
174 * individual element index via the ses_iter_seek_to() function. You may
175 * also return an iterator to the position just before the first element
176 * (i.e. the same state as after an ses_iter_init()), with ses_iter_reset().
178 struct ses_iterator {
180 * \brief Backlink to the overal software configuration structure.
182 * This is included for convenience so the iteration functions
183 * need only take a single, struct ses_iterator *, argument.
190 * \brief Index of the type of the current element within the
191 * ses_cache's ses_types array.
196 * \brief The position (0 based) of this element relative to all other
197 * elements of this type.
199 * This index resets to zero every time the iterator transitions
200 * to elements of a new type in the configuration.
202 int type_element_index;
205 * \brief The position (0 based) of this element relative to all
206 * other individual status elements in the configuration.
208 * This index ranges from 0 through the number of individual
209 * elements in the configuration. When the iterator returns
210 * an overall status element, individual_element_index is
211 * set to ITERATOR_INDEX_INVALID, to indicate that it does
212 * not apply to the current element.
214 int individual_element_index;
217 * \brief The position (0 based) of this element relative to
218 * all elements in the configration.
220 * This index is appropriate for indexing into enc->ses_elm_map.
222 int global_element_index;
225 * \brief The last valid individual element index of this
228 * When an iterator traverses an overal status element, the
229 * individual element index is reset to ITERATOR_INDEX_INVALID
230 * to prevent unintential use of the individual_element_index
231 * field. The saved_individual_element_index allows the iterator
232 * to restore it's position in the individual elements upon
233 * reaching the next individual element.
235 int saved_individual_element_index;
241 SES_UPDATE_GETCONFIG,
242 SES_UPDATE_GETSTATUS,
243 SES_UPDATE_GETELMDESCS,
244 SES_UPDATE_GETELMADDLSTATUS,
245 SES_PROCESS_CONTROL_REQS,
246 SES_PUBLISH_PHYSPATHS,
248 SES_NUM_UPDATE_STATES
251 static enc_softc_cleanup_t ses_softc_cleanup;
255 static fsm_fill_handler_t ses_fill_rcv_diag_io;
256 static fsm_fill_handler_t ses_fill_control_request;
257 static fsm_done_handler_t ses_process_pages;
258 static fsm_done_handler_t ses_process_config;
259 static fsm_done_handler_t ses_process_status;
260 static fsm_done_handler_t ses_process_elm_descs;
261 static fsm_done_handler_t ses_process_elm_addlstatus;
262 static fsm_done_handler_t ses_process_control_request;
263 static fsm_done_handler_t ses_publish_physpaths;
264 static fsm_done_handler_t ses_publish_cache;
266 static struct enc_fsm_state enc_fsm_states[SES_NUM_UPDATE_STATES] =
268 { "SES_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
274 ses_fill_rcv_diag_io,
279 "SES_UPDATE_GETCONFIG",
283 ses_fill_rcv_diag_io,
288 "SES_UPDATE_GETSTATUS",
292 ses_fill_rcv_diag_io,
297 "SES_UPDATE_GETELMDESCS",
298 SesElementDescriptor,
301 ses_fill_rcv_diag_io,
302 ses_process_elm_descs,
306 "SES_UPDATE_GETELMADDLSTATUS",
307 SesAddlElementStatus,
310 ses_fill_rcv_diag_io,
311 ses_process_elm_addlstatus,
315 "SES_PROCESS_CONTROL_REQS",
319 ses_fill_control_request,
320 ses_process_control_request,
324 "SES_PUBLISH_PHYSPATHS",
329 ses_publish_physpaths,
343 typedef struct ses_cache {
344 /* Source for all the configuration data pointers */
345 const struct ses_cfg_page *cfg_page;
347 /* References into the config page. */
348 const struct ses_enc_desc * const *subencs;
350 const ses_type_t *ses_types;
352 /* Source for all the status pointers */
353 const struct ses_status_page *status_page;
355 /* Source for all the object descriptor pointers */
356 const struct ses_elem_descr_page *elm_descs_page;
358 /* Source for all the additional object status pointers */
359 const struct ses_addl_elem_status_page *elm_addlstatus_page;
363 typedef struct ses_softc {
365 #define SES_FLAG_TIMEDCOMP 0x01
366 #define SES_FLAG_ADDLSTATUS 0x02
367 #define SES_FLAG_DESC 0x04
369 ses_control_reqlist_t ses_requests;
370 ses_control_reqlist_t ses_pending_requests;
374 * \brief Reset a SES iterator to just before the first element
375 * in the configuration.
377 * \param iter The iterator object to reset.
379 * The indexes within a reset iterator are invalid and will only
380 * become valid upon completion of a ses_iter_seek_to() or a
384 ses_iter_reset(struct ses_iterator *iter)
387 * Set our indexes to just before the first valid element
388 * of the first type (ITERATOR_INDEX_INVALID == -1). This
389 * simplifies the implementation of ses_iter_next().
391 iter->type_index = 0;
392 iter->type_element_index = ITERATOR_INDEX_INVALID;
393 iter->global_element_index = ITERATOR_INDEX_INVALID;
394 iter->individual_element_index = ITERATOR_INDEX_INVALID;
395 iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
399 * \brief Initialize the storage of a SES iterator and reset it to
400 * the position just before the first element of the
403 * \param enc The SES softc for the SES instance whose configuration
404 * will be enumerated by this iterator.
405 * \param iter The iterator object to initialize.
408 ses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
412 ses_iter_reset(iter);
416 * \brief Traverse the provided SES iterator to the next element
417 * within the configuraiton.
419 * \param iter The iterator to move.
421 * \return If a valid next element exists, a pointer to it's enc_element_t.
424 static enc_element_t *
425 ses_iter_next(struct ses_iterator *iter)
427 ses_cache_t *ses_cache;
428 const ses_type_t *element_type;
430 ses_cache = iter->cache->private;
433 * Note: Treat nelms as signed, so we will hit this case
434 * and immediately terminate the iteration if the
435 * configuration has 0 objects.
437 if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
439 /* Elements exhausted. */
440 iter->type_index = ITERATOR_INDEX_END;
441 iter->type_element_index = ITERATOR_INDEX_END;
442 iter->global_element_index = ITERATOR_INDEX_END;
443 iter->individual_element_index = ITERATOR_INDEX_END;
447 KASSERT((iter->type_index < ses_cache->ses_ntypes),
448 ("Corrupted element iterator. %d not less than %d",
449 iter->type_index, ses_cache->ses_ntypes));
451 element_type = &ses_cache->ses_types[iter->type_index];
452 iter->global_element_index++;
453 iter->type_element_index++;
456 * There is an object for overal type status in addition
457 * to one for each allowed element, but only if the element
460 if (iter->type_element_index > element_type->hdr->etype_maxelt) {
463 * We've exhausted the elements of this type.
464 * This next element belongs to the next type.
467 iter->type_element_index = 0;
468 iter->saved_individual_element_index
469 = iter->individual_element_index;
470 iter->individual_element_index = ITERATOR_INDEX_INVALID;
473 if (iter->type_element_index > 0) {
474 if (iter->type_element_index == 1) {
475 iter->individual_element_index
476 = iter->saved_individual_element_index;
478 iter->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_print_addl_data(enc_softc_t *, enc_element_t *);
554 /*=========================== SES cleanup routines ===========================*/
557 ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
559 ses_cache_t *ses_cache;
560 ses_cache_t *other_ses_cache;
561 enc_element_t *cur_elm;
562 enc_element_t *last_elm;
564 ENC_DLOG(enc, "%s: enter\n", __func__);
565 ses_cache = cache->private;
566 if (ses_cache->elm_addlstatus_page == NULL)
569 for (cur_elm = cache->elm_map,
570 last_elm = &cache->elm_map[cache->nelms];
571 cur_elm != last_elm; cur_elm++) {
572 ses_element_t *elmpriv;
574 elmpriv = cur_elm->elm_private;
576 /* Clear references to the additional status page. */
577 bzero(&elmpriv->addl, sizeof(elmpriv->addl));
580 other_ses_cache = enc_other_cache(enc, cache)->private;
581 if (other_ses_cache->elm_addlstatus_page
582 != ses_cache->elm_addlstatus_page)
583 ENC_FREE(ses_cache->elm_addlstatus_page);
584 ses_cache->elm_addlstatus_page = NULL;
588 ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
590 ses_cache_t *ses_cache;
591 ses_cache_t *other_ses_cache;
592 enc_element_t *cur_elm;
593 enc_element_t *last_elm;
595 ENC_DLOG(enc, "%s: enter\n", __func__);
596 ses_cache = cache->private;
597 if (ses_cache->elm_descs_page == NULL)
600 for (cur_elm = cache->elm_map,
601 last_elm = &cache->elm_map[cache->nelms];
602 cur_elm != last_elm; cur_elm++) {
603 ses_element_t *elmpriv;
605 elmpriv = cur_elm->elm_private;
606 elmpriv->descr_len = 0;
607 elmpriv->descr = NULL;
610 other_ses_cache = enc_other_cache(enc, cache)->private;
611 if (other_ses_cache->elm_descs_page
612 != ses_cache->elm_descs_page)
613 ENC_FREE(ses_cache->elm_descs_page);
614 ses_cache->elm_descs_page = NULL;
618 ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
620 ses_cache_t *ses_cache;
621 ses_cache_t *other_ses_cache;
623 ENC_DLOG(enc, "%s: enter\n", __func__);
624 ses_cache = cache->private;
625 if (ses_cache->status_page == NULL)
628 other_ses_cache = enc_other_cache(enc, cache)->private;
629 if (other_ses_cache->status_page != ses_cache->status_page)
630 ENC_FREE(ses_cache->status_page);
631 ses_cache->status_page = NULL;
635 ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
637 enc_element_t *cur_elm;
638 enc_element_t *last_elm;
640 ENC_DLOG(enc, "%s: enter\n", __func__);
641 if (cache->elm_map == NULL)
644 ses_cache_free_elm_descs(enc, cache);
645 ses_cache_free_elm_addlstatus(enc, cache);
646 for (cur_elm = cache->elm_map,
647 last_elm = &cache->elm_map[cache->nelms];
648 cur_elm != last_elm; cur_elm++) {
650 ENC_FREE_AND_NULL(cur_elm->elm_private);
652 ENC_FREE_AND_NULL(cache->elm_map);
654 ENC_DLOG(enc, "%s: exit\n", __func__);
658 ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
660 ses_cache_t *other_ses_cache;
661 ses_cache_t *ses_cache;
663 ENC_DLOG(enc, "%s: enter\n", __func__);
664 ses_cache_free_elm_addlstatus(enc, cache);
665 ses_cache_free_status(enc, cache);
666 ses_cache_free_elm_map(enc, cache);
668 ses_cache = cache->private;
669 ses_cache->ses_ntypes = 0;
671 other_ses_cache = enc_other_cache(enc, cache)->private;
672 if (other_ses_cache->subencs != ses_cache->subencs)
673 ENC_FREE(ses_cache->subencs);
674 ses_cache->subencs = NULL;
676 if (other_ses_cache->ses_types != ses_cache->ses_types)
677 ENC_FREE(ses_cache->ses_types);
678 ses_cache->ses_types = NULL;
680 if (other_ses_cache->cfg_page != ses_cache->cfg_page)
681 ENC_FREE(ses_cache->cfg_page);
682 ses_cache->cfg_page = NULL;
684 ENC_DLOG(enc, "%s: exit\n", __func__);
688 ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
690 ses_cache_t *dst_ses_cache;
691 ses_cache_t *src_ses_cache;
692 enc_element_t *src_elm;
693 enc_element_t *dst_elm;
694 enc_element_t *last_elm;
696 ses_cache_free(enc, dst);
697 src_ses_cache = src->private;
698 dst_ses_cache = dst->private;
701 * The cloned enclosure cache and ses specific cache are
702 * mostly identical to the source.
705 *dst_ses_cache = *src_ses_cache;
708 * But the ses cache storage is still independent. Restore
709 * the pointer that was clobbered by the structure copy above.
711 dst->private = dst_ses_cache;
714 * The element map is independent even though it starts out
715 * pointing to the same constant page data.
717 dst->elm_map = ENC_MALLOCZ(dst->nelms * sizeof(enc_element_t));
718 memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
719 for (dst_elm = dst->elm_map, src_elm = src->elm_map,
720 last_elm = &src->elm_map[src->nelms];
721 src_elm != last_elm; src_elm++, dst_elm++) {
723 dst_elm->elm_private = ENC_MALLOCZ(sizeof(ses_element_t));
724 memcpy(dst_elm->elm_private, src_elm->elm_private,
725 sizeof(ses_element_t));
729 /* Structure accessors. These are strongly typed to avoid errors. */
732 ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
734 return ((obj)->base_hdr.byte1 >> 6);
737 ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
739 return ((hdr)->byte0 & 0xf);
742 ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
744 return ((hdr)->byte0 >> 4) & 0x1;
747 ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
749 return ((hdr)->byte0 >> 7);
752 ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
754 return ((hdr)->type0_noneip.byte1 & 0x1);
757 ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
759 return ((phy)->target_ports & 0x1);
762 ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
764 return ((phy)->target_ports >> 7);
767 ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
769 return (((phy)->byte0 >> 4) & 0x7);
773 * \brief Verify that the cached configuration data in our softc
774 * is valid for processing the page data corresponding to
775 * the provided page header.
777 * \param ses_cache The SES cache to validate.
778 * \param gen_code The 4 byte generation code from a SES diagnostic
781 * \return non-zero if true, 0 if false.
784 ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
789 if (ses_cache->cfg_page == NULL)
792 cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
793 cur_gc = scsi_4btoul(gen_code);
794 return (cache_gc == cur_gc);
798 * Function signature for consumers of the ses_devids_iter() interface.
800 typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
801 struct scsi_vpd_id_descriptor *, void *);
804 * \brief Iterate over and create vpd device id records from the
805 * additional element status data for elm, passing that data
806 * to the provided callback.
808 * \param enc SES instance containing elm
809 * \param elm Element for which to extract device ID data.
810 * \param callback The callback function to invoke on each generated
811 * device id descriptor for elm.
812 * \param callback_arg Argument passed through to callback on each invocation.
815 ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
816 ses_devid_callback_t *callback, void *callback_arg)
818 ses_element_t *elmpriv;
819 struct ses_addl_status *addl;
821 size_t devid_record_size;
823 elmpriv = elm->elm_private;
824 addl = &(elmpriv->addl);
827 * Don't assume this object has additional status information, or
828 * that it is a SAS device, or that it is a device slot device.
830 if (addl->hdr == NULL || addl->proto_hdr.sas == NULL
831 || addl->proto_data.sasdev_phys == NULL)
834 devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
835 + sizeof(struct scsi_vpd_id_naa_ieee_reg);
836 for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
837 uint8_t devid_buf[devid_record_size];
838 struct scsi_vpd_id_descriptor *devid;
841 devid = (struct scsi_vpd_id_descriptor *)devid_buf;
842 phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
843 devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
844 | SVPD_ID_CODESET_BINARY;
845 devid->id_type = SVPD_ID_PIV
849 devid->length = sizeof(struct scsi_vpd_id_naa_ieee_reg);
850 memcpy(devid->identifier, phy_addr, devid->length);
852 callback(enc, elm, devid, callback_arg);
857 * Function signature for consumers of the ses_paths_iter() interface.
859 typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
860 struct cam_path *, void *);
863 * Argument package passed through ses_devids_iter() by
864 * ses_paths_iter() to ses_path_iter_devid_callback().
866 typedef struct ses_path_iter_args {
867 ses_path_callback_t *callback;
869 } ses_path_iter_args_t;
872 * ses_devids_iter() callback function used by ses_paths_iter()
873 * to map device ids to peripheral driver instances.
875 * \param enc SES instance containing elm
876 * \param elm Element on which device ID matching is active.
877 * \param periph A device ID corresponding to elm.
878 * \param arg Argument passed through to callback on each invocation.
881 ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
882 struct scsi_vpd_id_descriptor *devid,
885 struct ccb_dev_match cdm;
886 struct dev_match_pattern match_pattern;
887 struct dev_match_result match_result;
888 struct device_match_result *device_match;
889 struct device_match_pattern *device_pattern;
890 ses_path_iter_args_t *args;
893 args = (ses_path_iter_args_t *)arg;
894 match_pattern.type = DEV_MATCH_DEVICE;
895 device_pattern = &match_pattern.pattern.device_pattern;
896 device_pattern->flags = DEV_MATCH_DEVID;
897 device_pattern->data.devid_pat.id_len =
898 offsetof(struct scsi_vpd_id_descriptor, identifier)
900 memcpy(device_pattern->data.devid_pat.id, devid,
901 device_pattern->data.devid_pat.id_len);
903 memset(&cdm, 0, sizeof(cdm));
904 if (xpt_create_path_unlocked(&cdm.ccb_h.path, /*periph*/NULL,
907 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
910 cdm.ccb_h.func_code = XPT_DEV_MATCH;
911 cdm.num_patterns = 1;
912 cdm.patterns = &match_pattern;
913 cdm.pattern_buf_len = sizeof(match_pattern);
914 cdm.match_buf_len = sizeof(match_result);
915 cdm.matches = &match_result;
917 sim = xpt_path_sim(cdm.ccb_h.path);
919 xpt_action((union ccb *)&cdm);
920 xpt_free_path(cdm.ccb_h.path);
923 if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
924 || (cdm.status != CAM_DEV_MATCH_LAST
925 && cdm.status != CAM_DEV_MATCH_MORE)
926 || cdm.num_matches == 0)
929 device_match = &match_result.result.device_result;
930 if (xpt_create_path_unlocked(&cdm.ccb_h.path, /*periph*/NULL,
931 device_match->path_id,
932 device_match->target_id,
933 device_match->target_lun) != CAM_REQ_CMP)
936 args->callback(enc, elem, cdm.ccb_h.path, args->callback_arg);
938 sim = xpt_path_sim(cdm.ccb_h.path);
940 xpt_free_path(cdm.ccb_h.path);
945 * \brief Iterate over and find the matching periph objects for the
948 * \param enc SES instance containing elm
949 * \param elm Element for which to perform periph object matching.
950 * \param callback The callback function to invoke with each matching
952 * \param callback_arg Argument passed through to callback on each invocation.
955 ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
956 ses_path_callback_t *callback, void *callback_arg)
958 ses_path_iter_args_t args;
960 args.callback = callback;
961 args.callback_arg = callback_arg;
962 ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
966 * ses_paths_iter() callback function used by ses_get_elmdevname()
967 * to record periph driver instance strings corresponding to a SES
970 * \param enc SES instance containing elm
971 * \param elm Element on which periph matching is active.
972 * \param periph A periph instance that matches elm.
973 * \param arg Argument passed through to callback on each invocation.
976 ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
977 struct cam_path *path, void *arg)
981 sb = (struct sbuf *)arg;
982 cam_periph_list(path, sb);
986 * Argument package passed through ses_paths_iter() to
987 * ses_getcampath_callback.
989 typedef struct ses_setphyspath_callback_args {
990 struct sbuf *physpath;
992 } ses_setphyspath_callback_args_t;
995 * \brief ses_paths_iter() callback to set the physical path on the
996 * CAM EDT entries corresponding to a given SES element.
998 * \param enc SES instance containing elm
999 * \param elm Element on which periph matching is active.
1000 * \param periph A periph instance that matches elm.
1001 * \param arg Argument passed through to callback on each invocation.
1004 ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1005 struct cam_path *path, void *arg)
1007 struct ccb_dev_advinfo cdai;
1008 ses_setphyspath_callback_args_t *args;
1011 args = (ses_setphyspath_callback_args_t *)arg;
1012 old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1013 cam_periph_lock(enc->periph);
1014 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1015 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1016 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1018 cdai.bufsiz = MAXPATHLEN;
1019 cdai.buf = old_physpath;
1020 xpt_action((union ccb *)&cdai);
1021 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1022 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1024 if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1026 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1027 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1028 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1029 cdai.flags |= CDAI_FLAG_STORE;
1030 cdai.bufsiz = sbuf_len(args->physpath);
1031 cdai.buf = sbuf_data(args->physpath);
1032 xpt_action((union ccb *)&cdai);
1033 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1034 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1035 if (cdai.ccb_h.status == CAM_REQ_CMP)
1038 cam_periph_unlock(enc->periph);
1039 free(old_physpath, M_SCSIENC);
1043 * \brief Set a device's physical path string in CAM XPT.
1045 * \param enc SES instance containing elm
1046 * \param elm Element to publish physical path string for
1047 * \param iter Iterator whose state corresponds to elm
1049 * \return 0 on success, errno otherwise.
1052 ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1053 struct ses_iterator *iter)
1055 struct ccb_dev_advinfo cdai;
1056 ses_setphyspath_callback_args_t args;
1059 struct scsi_vpd_id_descriptor *idd;
1061 ses_element_t *elmpriv;
1068 * Assemble the components of the physical path starting with
1069 * the device ID of the enclosure itself.
1071 xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1072 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1073 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1074 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1075 cdai.buf = devid = ENC_MALLOCZ(cdai.bufsiz);
1076 if (devid == NULL) {
1080 cam_periph_lock(enc->periph);
1081 xpt_action((union ccb *)&cdai);
1082 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1083 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1084 cam_periph_unlock(enc->periph);
1085 if (cdai.ccb_h.status != CAM_REQ_CMP)
1088 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1089 cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1093 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1097 /* Next, generate the physical path string */
1098 sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1099 scsi_8btou64(idd->identifier), iter->type_index,
1100 iter->type_element_index);
1101 /* Append the element descriptor if one exists */
1102 elmpriv = elm->elm_private;
1103 if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1104 sbuf_cat(&sb, "/elmdesc@");
1105 for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1107 if (!isprint(*c) || isspace(*c) || *c == '/')
1108 sbuf_putc(&sb, '_');
1116 * Set this physical path on any CAM devices with a device ID
1117 * descriptor that matches one created from the SES additional
1118 * status data for this element.
1122 ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1125 ret = args.num_set == 0 ? ENOENT : 0;
1134 * \brief Helper to set the CDB fields appropriately.
1136 * \param cdb Buffer containing the cdb.
1137 * \param pagenum SES diagnostic page to query for.
1138 * \param dir Direction of query.
1141 ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1144 /* Ref: SPC-4 r25 Section 6.20 Table 223 */
1145 if (dir == CAM_DIR_IN) {
1146 cdb[0] = RECEIVE_DIAGNOSTIC;
1147 cdb[1] = 1; /* Set page code valid bit */
1150 cdb[0] = SEND_DIAGNOSTIC;
1154 cdb[3] = bufsiz >> 8; /* high bits */
1155 cdb[4] = bufsiz & 0xff; /* low bits */
1160 * \brief Discover whether this instance supports timed completion of a
1161 * RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1162 * page, and store the result in the softc, updating if necessary.
1164 * \param enc SES instance to query and update.
1165 * \param tc_en Value of timed completion to set (see \return).
1167 * \return 1 if timed completion enabled, 0 otherwise.
1170 ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1174 struct cam_periph *periph;
1175 struct ses_mgmt_mode_page *mgmt;
1177 size_t mode_buf_len;
1180 periph = enc->periph;
1181 ses = enc->enc_private;
1182 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1184 mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1185 mode_buf = ENC_MALLOCZ(mode_buf_len);
1186 if (mode_buf == NULL)
1189 scsi_mode_sense(&ccb->csio, /*retries*/4, enc_done, MSG_SIMPLE_Q_TAG,
1190 /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1191 mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1194 * Ignore illegal request errors, as they are quite common and we
1195 * will print something out in that case anyway.
1197 err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1198 ENC_FLAGS|SF_QUIET_IR, NULL);
1199 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1200 ENC_VLOG(enc, "Timed Completion Unsupported\n");
1204 /* Skip the mode select if the desired value is already set */
1205 mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1206 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1209 /* Value is not what we wanted, set it */
1211 mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1213 mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1214 /* SES2r20: a completion time of zero means as long as possible */
1215 bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1217 scsi_mode_select(&ccb->csio, 5, enc_done, MSG_SIMPLE_Q_TAG,
1218 /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1219 SSD_FULL_SIZE, /*timeout*/60 * 1000);
1221 err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1222 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1223 ENC_VLOG(enc, "Timed Completion Set Failed\n");
1228 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1229 ENC_LOG(enc, "Timed Completion Enabled\n");
1230 ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1232 ENC_LOG(enc, "Timed Completion Disabled\n");
1233 ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1237 xpt_release_ccb(ccb);
1239 return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1243 * \brief Process the list of supported pages and update flags.
1245 * \param enc SES device to query.
1246 * \param buf Buffer containing the config page.
1247 * \param xfer_len Length of the config page in the buffer.
1249 * \return 0 on success, errno otherwise.
1252 ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1253 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1256 struct scsi_diag_page *page;
1259 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1260 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1261 ses = enc->enc_private;
1268 if (xfer_len < sizeof(*page)) {
1269 ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1273 page = (struct scsi_diag_page *)*bufp;
1274 length = scsi_2btoul(page->length);
1275 if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1276 ENC_VLOG(enc, "Diag Pages List Too Long\n");
1279 ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1280 __func__, length, xfer_len);
1283 for (i = 0; i < length; i++) {
1284 if (page->params[i] == SesElementDescriptor)
1285 ses->ses_flags |= SES_FLAG_DESC;
1286 else if (page->params[i] == SesAddlElementStatus)
1287 ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1291 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1296 * \brief Process the config page and update associated structures.
1298 * \param enc SES device to query.
1299 * \param buf Buffer containing the config page.
1300 * \param xfer_len Length of the config page in the buffer.
1302 * \return 0 on success, errno otherwise.
1305 ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1306 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1308 struct ses_iterator iter;
1310 enc_cache_t *enc_cache;
1311 ses_cache_t *ses_cache;
1317 struct ses_cfg_page *cfg_page;
1318 struct ses_enc_desc *buf_subenc;
1319 const struct ses_enc_desc **subencs;
1320 const struct ses_enc_desc **cur_subenc;
1321 const struct ses_enc_desc **last_subenc;
1322 ses_type_t *ses_types;
1323 ses_type_t *sestype;
1324 const struct ses_elm_type_desc *cur_buf_type;
1325 const struct ses_elm_type_desc *last_buf_type;
1326 uint8_t *last_valid_byte;
1327 enc_element_t *element;
1328 const char *type_text;
1330 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1331 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1332 ses = enc->enc_private;
1333 enc_cache = &enc->enc_daemon_cache;
1334 ses_cache = enc_cache->private;
1342 if (xfer_len < sizeof(cfg_page->hdr)) {
1343 ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1348 cfg_page = (struct ses_cfg_page *)buf;
1349 length = ses_page_length(&cfg_page->hdr);
1350 if (length > xfer_len) {
1351 ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1354 last_valid_byte = &buf[length - 1];
1356 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1357 __func__, length, xfer_len);
1360 if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1362 /* Our cache is still valid. Proceed to fetching status. */
1366 /* Cache is no longer valid. Free old data to make way for new. */
1367 ses_cache_free(enc, enc_cache);
1368 ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1369 scsi_4btoul(cfg_page->hdr.gen_code),
1370 ses_cfg_page_get_num_subenc(cfg_page));
1372 /* Take ownership of the buffer. */
1373 ses_cache->cfg_page = cfg_page;
1377 * Now waltz through all the subenclosures summing the number of
1378 * types available in each.
1380 subencs = ENC_MALLOCZ(ses_cfg_page_get_num_subenc(cfg_page)
1381 * sizeof(*subencs));
1382 if (subencs == NULL) {
1387 * Sub-enclosure data is const after construction (i.e. when
1388 * accessed via our cache object.
1390 * The cast here is not required in C++ but C99 is not so
1391 * sophisticated (see C99 6.5.16.1(1)).
1393 ses_cache->subencs = subencs;
1395 buf_subenc = cfg_page->subencs;
1396 cur_subenc = subencs;
1397 last_subenc = &subencs[ses_cfg_page_get_num_subenc(cfg_page) - 1];
1399 while (cur_subenc <= last_subenc) {
1401 if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1402 ENC_VLOG(enc, "Enclosure %d Beyond End of "
1403 "Descriptors\n", cur_subenc - subencs);
1408 ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1409 "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1410 buf_subenc->num_types, buf_subenc->length,
1411 &buf_subenc->byte0 - buf);
1412 ENC_VLOG(enc, "WWN: %jx\n",
1413 (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1415 ntype += buf_subenc->num_types;
1416 *cur_subenc = buf_subenc;
1418 buf_subenc = ses_enc_desc_next(buf_subenc);
1421 /* Process the type headers. */
1422 ses_types = ENC_MALLOCZ(ntype * sizeof(*ses_types));
1423 if (ses_types == NULL) {
1428 * Type data is const after construction (i.e. when accessed via
1431 ses_cache->ses_types = ses_types;
1433 cur_buf_type = (const struct ses_elm_type_desc *)
1434 (&(*last_subenc)->length + (*last_subenc)->length + 1);
1435 last_buf_type = cur_buf_type + ntype - 1;
1436 type_text = (const uint8_t *)(last_buf_type + 1);
1438 sestype = ses_types;
1439 while (cur_buf_type <= last_buf_type) {
1440 if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1441 ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1442 sestype - ses_types);
1446 sestype->hdr = cur_buf_type;
1447 sestype->text = type_text;
1448 type_text += cur_buf_type->etype_txt_len;
1449 ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1450 "%d, Text Length %d: %.*s\n", sestype - ses_types,
1451 sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1452 sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1453 sestype->hdr->etype_txt_len, sestype->text);
1455 nelm += sestype->hdr->etype_maxelt
1456 + /*overall status element*/1;
1461 /* Create the object map. */
1462 enc_cache->elm_map = ENC_MALLOCZ(nelm * sizeof(enc_element_t));
1463 if (enc_cache->elm_map == NULL) {
1467 ses_cache->ses_ntypes = (uint8_t)ntype;
1468 enc_cache->nelms = nelm;
1470 ses_iter_init(enc, enc_cache, &iter);
1471 while ((element = ses_iter_next(&iter)) != NULL) {
1472 const struct ses_elm_type_desc *thdr;
1474 ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1475 iter.global_element_index, iter.type_index, nelm,
1476 iter.type_element_index);
1477 thdr = ses_cache->ses_types[iter.type_index].hdr;
1478 element->subenclosure = thdr->etype_subenc;
1479 element->enctype = thdr->etype_elm_type;
1480 element->overall_status_elem = iter.type_element_index == 0;
1481 element->elm_private = ENC_MALLOCZ(sizeof(ses_element_t));
1482 if (element->elm_private == NULL) {
1486 ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1487 "type 0x%x\n", __func__, iter.global_element_index,
1488 iter.type_index, iter.type_element_index,
1489 thdr->etype_subenc, thdr->etype_elm_type);
1496 ses_cache_free(enc, enc_cache);
1498 enc_update_request(enc, SES_UPDATE_GETSTATUS);
1499 if (ses->ses_flags & SES_FLAG_DESC)
1500 enc_update_request(enc, SES_UPDATE_GETELMDESCS);
1501 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
1502 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
1503 enc_update_request(enc, SES_PUBLISH_CACHE);
1505 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1510 * \brief Update the status page and associated structures.
1512 * \param enc SES softc to update for.
1513 * \param buf Buffer containing the status page.
1514 * \param bufsz Amount of data in the buffer.
1516 * \return 0 on success, errno otherwise.
1519 ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1520 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1522 struct ses_iterator iter;
1523 enc_element_t *element;
1525 enc_cache_t *enc_cache;
1526 ses_cache_t *ses_cache;
1530 struct ses_status_page *page;
1531 union ses_status_element *cur_stat;
1532 union ses_status_element *last_stat;
1534 ses = enc->enc_private;
1535 enc_cache = &enc->enc_daemon_cache;
1536 ses_cache = enc_cache->private;
1539 ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1540 page = (struct ses_status_page *)buf;
1541 length = ses_page_length(&page->hdr);
1548 * Make sure the length fits in the buffer.
1550 * XXX all this means is that the page is larger than the space
1551 * we allocated. Since we use a statically sized buffer, this
1552 * could happen... Need to use dynamic discovery of the size.
1554 if (length > xfer_len) {
1555 ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1559 /* Check for simple enclosure reporting short enclosure status. */
1560 if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1561 ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1562 ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1563 ses_cache_free(enc, enc_cache);
1564 enc_cache->enc_status = page->hdr.page_specific_flags;
1565 enc_update_request(enc, SES_PUBLISH_CACHE);
1570 /* Make sure the length contains at least one header and status */
1571 if (length < (sizeof(*page) + sizeof(*page->elements))) {
1572 ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1576 if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1577 ENC_DLOG(enc, "%s: Generation count change detected\n",
1579 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1583 ses_cache_free_status(enc, enc_cache);
1584 ses_cache->status_page = page;
1587 enc_cache->enc_status = page->hdr.page_specific_flags;
1590 * Read in individual element status. The element order
1591 * matches the order reported in the config page (i.e. the
1592 * order of an unfiltered iteration of the config objects)..
1594 ses_iter_init(enc, enc_cache, &iter);
1595 cur_stat = page->elements;
1596 last_stat = (union ses_status_element *)
1597 &buf[length - sizeof(*last_stat)];
1598 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1599 __func__, length, xfer_len);
1600 while (cur_stat <= last_stat
1601 && (element = ses_iter_next(&iter)) != NULL) {
1603 ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1604 __func__, iter.global_element_index, iter.type_index,
1605 iter.type_element_index, (uint8_t *)cur_stat - buf,
1606 scsi_4btoul(cur_stat->bytes));
1608 memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1609 element->svalid = 1;
1613 if (ses_iter_next(&iter) != NULL) {
1614 ENC_VLOG(enc, "Status page, length insufficient for "
1615 "expected number of objects\n");
1617 if (cur_stat <= last_stat)
1618 ENC_VLOG(enc, "Status page, exhausted objects before "
1619 "exhausing page\n");
1620 enc_update_request(enc, SES_PUBLISH_CACHE);
1624 ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1630 * The enclosure should not provide additional element
1631 * status for this element type in page 0x0A.
1633 * \note This status is returned for any types not
1634 * listed SES3r02. Further types added in a
1635 * future specification will be incorrectly
1638 TYPE_ADDLSTATUS_NONE,
1641 * The element type provides additional element status
1644 TYPE_ADDLSTATUS_MANDATORY,
1647 * The element type may provide additional element status
1648 * in page 0x0A, but i
1650 TYPE_ADDLSTATUS_OPTIONAL
1651 } ses_addlstatus_avail_t;
1654 * \brief Check to see whether a given type (as obtained via type headers) is
1655 * supported by the additional status command.
1657 * \param enc SES softc to check.
1658 * \param typidx Type index to check for.
1660 * \return An enumeration indicating if additional status is mandatory,
1661 * optional, or not required for this type.
1663 static ses_addlstatus_avail_t
1664 ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1666 enc_cache_t *enc_cache;
1667 ses_cache_t *ses_cache;
1669 enc_cache = &enc->enc_daemon_cache;
1670 ses_cache = enc_cache->private;
1671 switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1673 case ELMTYP_ARRAY_DEV:
1674 case ELMTYP_SAS_EXP:
1675 return (TYPE_ADDLSTATUS_MANDATORY);
1676 case ELMTYP_SCSI_INI:
1677 case ELMTYP_SCSI_TGT:
1679 return (TYPE_ADDLSTATUS_OPTIONAL);
1681 /* No additional status information available. */
1684 return (TYPE_ADDLSTATUS_NONE);
1687 static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1689 static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1690 int, int, int, int);
1693 * \brief Parse the additional status element data for each object.
1695 * \param enc The SES softc to update.
1696 * \param buf The buffer containing the additional status
1698 * \param xfer_len Size of the buffer.
1700 * \return 0 on success, errno otherwise.
1703 ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1704 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1706 struct ses_iterator iter, titer;
1709 int ignore_index = 0;
1712 enc_cache_t *enc_cache;
1713 ses_cache_t *ses_cache;
1715 ses_element_t *elmpriv;
1716 const struct ses_page_hdr *hdr;
1717 enc_element_t *element, *telement;
1719 enc_cache = &enc->enc_daemon_cache;
1720 ses_cache = enc_cache->private;
1728 ses_cache_free_elm_addlstatus(enc, enc_cache);
1729 ses_cache->elm_addlstatus_page =
1730 (struct ses_addl_elem_status_page *)buf;
1734 * The objects appear in the same order here as in Enclosure Status,
1735 * which itself is ordered by the Type Descriptors from the Config
1736 * page. However, it is necessary to skip elements that are not
1737 * supported by this page when counting them.
1739 hdr = &ses_cache->elm_addlstatus_page->hdr;
1740 length = ses_page_length(hdr);
1741 ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1742 /* Make sure the length includes at least one header. */
1743 if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1744 ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1747 if (length > xfer_len) {
1748 ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1752 if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1753 ENC_DLOG(enc, "%s: Generation count change detected\n",
1755 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1759 offset = sizeof(struct ses_page_hdr);
1760 ses_iter_init(enc, enc_cache, &iter);
1761 while (offset < length
1762 && (element = ses_iter_next(&iter)) != NULL) {
1763 struct ses_elm_addlstatus_base_hdr *elm_hdr;
1765 ses_addlstatus_avail_t status_type;
1768 * Additional element status is only provided for
1769 * individual elements (i.e. overal status elements
1770 * are excluded) and those of the types specified
1773 status_type = ses_typehasaddlstatus(enc, iter.type_index);
1774 if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1775 || status_type == TYPE_ADDLSTATUS_NONE)
1778 elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1779 eip = ses_elm_addlstatus_eip(elm_hdr);
1780 if (eip && !ignore_index) {
1781 struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1784 eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1785 expected_index = iter.individual_element_index;
1787 telement = ses_iter_seek_to(&titer,
1788 eip_hdr->element_index,
1789 SES_ELEM_INDEX_INDIVIDUAL);
1790 if (telement != NULL &&
1791 (ses_typehasaddlstatus(enc, titer.type_index) !=
1792 TYPE_ADDLSTATUS_NONE ||
1793 titer.type_index > ELMTYP_SAS_CONN)) {
1799 if (iter.individual_element_index > expected_index
1800 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1801 ENC_VLOG(enc, "%s: provided element "
1802 "index %d skips mandatory status "
1803 " element at index %d\n",
1804 __func__, eip_hdr->element_index,
1808 elmpriv = element->elm_private;
1809 elmpriv->addl.hdr = elm_hdr;
1810 ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1811 "type element index=%d, offset=0x%x, "
1812 "byte0=0x%x, length=0x%x\n", __func__,
1813 iter.global_element_index, iter.type_index,
1814 iter.type_element_index, offset, elmpriv->addl.hdr->byte0,
1815 elmpriv->addl.hdr->length);
1817 /* Skip to after the length field */
1818 offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1820 /* Make sure the descriptor is within bounds */
1821 if ((offset + elmpriv->addl.hdr->length) > length) {
1822 ENC_VLOG(enc, "Element %d Beyond End "
1823 "of Additional Element Status Descriptors\n",
1824 iter.global_element_index);
1828 /* Advance to the protocol data, skipping eip bytes if needed */
1829 offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1830 proto_info_len = elmpriv->addl.hdr->length
1831 - (eip * SES_EIP_HDR_EXTRA_LEN);
1833 /* Errors in this block are ignored as they are non-fatal */
1834 switch(ses_elm_addlstatus_proto(elmpriv->addl.hdr)) {
1836 if (elmpriv->addl.hdr->length == 0)
1838 ses_get_elm_addlstatus_fc(enc, enc_cache,
1839 &buf[offset], proto_info_len);
1841 case SPSP_PROTO_SAS:
1842 if (elmpriv->addl.hdr->length <= 2)
1844 ses_get_elm_addlstatus_sas(enc, enc_cache,
1847 eip, iter.type_index,
1848 iter.global_element_index);
1851 ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1852 "Protocol 0x%x\n", iter.global_element_index,
1853 ses_elm_addlstatus_proto(elmpriv->addl.hdr));
1857 offset += proto_info_len;
1862 ses_cache_free_elm_addlstatus(enc, enc_cache);
1863 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1864 enc_update_request(enc, SES_PUBLISH_CACHE);
1869 ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1870 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1874 ses = enc->enc_private;
1877 * o Generation count wrong.
1878 * o Some SCSI status error.
1880 ses_terminate_control_requests(&ses->ses_pending_requests, error);
1881 enc_update_request(enc, SES_UPDATE_GETSTATUS);
1886 ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1887 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1889 struct ses_iterator iter;
1890 enc_cache_t *enc_cache;
1891 ses_cache_t *ses_cache;
1892 enc_element_t *element;
1894 enc_cache = &enc->enc_daemon_cache;
1895 ses_cache = enc_cache->private;
1897 ses_iter_init(enc, enc_cache, &iter);
1898 while ((element = ses_iter_next(&iter)) != NULL) {
1900 * ses_set_physpath() returns success if we changed
1901 * the physpath of any element. This allows us to
1902 * only announce devices once regardless of how
1903 * many times we process additional element status.
1905 if (ses_set_physpath(enc, element, &iter) == 0)
1906 ses_print_addl_data(enc, element);
1913 ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1914 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1917 sx_xlock(&enc->enc_cache_lock);
1918 ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1919 /*dst*/&enc->enc_cache);
1920 sx_xunlock(&enc->enc_cache_lock);
1926 * \brief Parse the descriptors for each object.
1928 * \param enc The SES softc to update.
1929 * \param buf The buffer containing the descriptor list response.
1930 * \param xfer_len Size of the buffer.
1932 * \return 0 on success, errno otherwise.
1935 ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1936 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1939 struct ses_iterator iter;
1940 enc_element_t *element;
1943 u_long length, plength;
1944 enc_cache_t *enc_cache;
1945 ses_cache_t *ses_cache;
1947 ses_element_t *elmpriv;
1948 const struct ses_page_hdr *phdr;
1949 const struct ses_elm_desc_hdr *hdr;
1951 ses = enc->enc_private;
1952 enc_cache = &enc->enc_daemon_cache;
1953 ses_cache = enc_cache->private;
1961 ses_cache_free_elm_descs(enc, enc_cache);
1962 ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
1965 phdr = &ses_cache->elm_descs_page->hdr;
1966 plength = ses_page_length(phdr);
1967 if (xfer_len < sizeof(struct ses_page_hdr)) {
1968 ENC_VLOG(enc, "Runt Element Descriptor Page\n");
1971 if (plength > xfer_len) {
1972 ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
1976 if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
1977 ENC_VLOG(enc, "%s: Generation count change detected\n",
1979 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1983 offset = sizeof(struct ses_page_hdr);
1985 ses_iter_init(enc, enc_cache, &iter);
1986 while (offset < plength
1987 && (element = ses_iter_next(&iter)) != NULL) {
1989 if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
1990 ENC_VLOG(enc, "Element %d Descriptor Header Past "
1991 "End of Buffer\n", iter.global_element_index);
1994 hdr = (struct ses_elm_desc_hdr *)&buf[offset];
1995 length = scsi_2btoul(hdr->length);
1996 ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
1997 iter.global_element_index, iter.type_index,
1998 iter.type_element_index, length, offset);
1999 if ((offset + sizeof(*hdr) + length) > plength) {
2000 ENC_VLOG(enc, "Element%d Descriptor Past "
2001 "End of Buffer\n", iter.global_element_index);
2004 offset += sizeof(*hdr);
2007 elmpriv = element->elm_private;
2008 elmpriv->descr_len = length;
2009 elmpriv->descr = &buf[offset];
2012 /* skip over the descriptor itself */
2019 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2020 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2022 enc_update_request(enc, SES_PUBLISH_CACHE);
2027 ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2028 union ccb *ccb, uint8_t *buf)
2031 if (enc->enc_type == ENC_SEMB_SES) {
2032 semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2033 enc_done, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2034 state->page_code, buf, state->buf_size,
2037 scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2038 enc_done, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2039 state->page_code, buf, state->buf_size,
2040 SSD_FULL_SIZE, state->timeout);
2046 * \brief Encode the object status into the response buffer, which is
2047 * expected to contain the current enclosure status. This function
2048 * turns off all the 'select' bits for the objects except for the
2049 * object specified, then sends it back to the enclosure.
2051 * \param enc SES enclosure the change is being applied to.
2052 * \param buf Buffer containing the current enclosure status response.
2053 * \param amt Length of the response in the buffer.
2054 * \param req The control request to be applied to buf.
2056 * \return 0 on success, errno otherwise.
2059 ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2061 struct ses_iterator iter;
2062 enc_element_t *element;
2064 struct ses_control_page_hdr *hdr;
2066 ses_iter_init(enc, &enc->enc_cache, &iter);
2067 hdr = (struct ses_control_page_hdr *)buf;
2068 if (req->elm_idx == -1) {
2069 /* for enclosure status, at least 2 bytes are needed */
2072 hdr->control_flags =
2073 req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2074 ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2078 element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2079 if (element == NULL)
2083 * Seek to the type set that corresponds to the requested object.
2084 * The +1 is for the overall status element for the type.
2086 offset = sizeof(struct ses_control_page_hdr)
2087 + (iter.global_element_index * sizeof(struct ses_comstat));
2089 /* Check for buffer overflow. */
2090 if (offset + sizeof(struct ses_comstat) > amt)
2093 /* Set the status. */
2094 memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2096 ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2097 iter.type_index, iter.global_element_index, offset,
2098 req->elm_stat.comstatus, req->elm_stat.comstat[0],
2099 req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2105 ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2106 union ccb *ccb, uint8_t *buf)
2109 enc_cache_t *enc_cache;
2110 ses_cache_t *ses_cache;
2111 struct ses_control_page_hdr *hdr;
2112 ses_control_request_t *req;
2116 ses = enc->enc_private;
2117 enc_cache = &enc->enc_daemon_cache;
2118 ses_cache = enc_cache->private;
2119 hdr = (struct ses_control_page_hdr *)buf;
2121 if (ses_cache->status_page == NULL) {
2122 ses_terminate_control_requests(&ses->ses_requests, EIO);
2126 plength = ses_page_length(&ses_cache->status_page->hdr);
2127 memcpy(buf, ses_cache->status_page, plength);
2129 /* Disable the select bits in all status entries. */
2130 offset = sizeof(struct ses_control_page_hdr);
2131 for (offset = sizeof(struct ses_control_page_hdr);
2132 offset < plength; offset += sizeof(struct ses_comstat)) {
2133 buf[offset] &= ~SESCTL_CSEL;
2136 /* And make sure the INVOP bit is clear. */
2137 hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2139 /* Apply incoming requests. */
2140 while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2142 TAILQ_REMOVE(&ses->ses_requests, req, links);
2143 req->result = ses_encode(enc, buf, plength, req);
2144 if (req->result != 0) {
2148 TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2151 if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2154 /* Fill out the ccb */
2155 if (enc->enc_type == ENC_SEMB_SES) {
2156 semb_send_diagnostic(&ccb->ataio, /*retries*/5, enc_done,
2158 buf, ses_page_length(&ses_cache->status_page->hdr),
2161 scsi_send_diagnostic(&ccb->csio, /*retries*/5, enc_done,
2162 MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2163 /*device_offline*/0, /*self_test*/0,
2164 /*page_format*/1, /*self_test_code*/0,
2165 buf, ses_page_length(&ses_cache->status_page->hdr),
2166 SSD_FULL_SIZE, state->timeout);
2172 ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2173 uint8_t *buf, int bufsiz)
2175 ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2179 #define SES_PRINT_PORTS(p, type) do { \
2180 sbuf_printf(sbp, " %s(", type); \
2181 if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) == 0) \
2182 sbuf_printf(sbp, " None"); \
2184 if ((p) & SES_SASOBJ_DEV_PHY_SMP) \
2185 sbuf_printf(sbp, " SMP"); \
2186 if ((p) & SES_SASOBJ_DEV_PHY_STP) \
2187 sbuf_printf(sbp, " STP"); \
2188 if ((p) & SES_SASOBJ_DEV_PHY_SSP) \
2189 sbuf_printf(sbp, " SSP"); \
2191 sbuf_printf(sbp, " )"); \
2195 * \brief Print the additional element status data for this object, for SAS
2196 * type 0 objects. See SES2 r20 Section 6.1.13.3.2.
2198 * \param sesname SES device name associated with the object.
2199 * \param sbp Sbuf to print to.
2200 * \param obj The object to print the data for.
2201 * \param periph_name Peripheral string associated with the object.
2204 ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2205 enc_element_t *obj, char *periph_name)
2208 ses_element_t *elmpriv;
2209 struct ses_addl_status *addl;
2210 struct ses_elm_sas_device_phy *phy;
2212 elmpriv = obj->elm_private;
2213 addl = &(elmpriv->addl);
2214 if (addl->proto_hdr.sas == NULL)
2216 sbuf_printf(sbp, "%s: %s: SAS Device Slot Element:",
2217 sesname, periph_name);
2218 sbuf_printf(sbp, " %d Phys", addl->proto_hdr.sas->base_hdr.num_phys);
2219 if (ses_elm_addlstatus_eip(addl->hdr))
2220 sbuf_printf(sbp, " at Slot %d",
2221 addl->proto_hdr.sas->type0_eip.dev_slot_num);
2222 if (ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas))
2223 sbuf_printf(sbp, ", Not All Phys");
2224 sbuf_printf(sbp, "\n");
2225 if (addl->proto_data.sasdev_phys == NULL)
2227 for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2228 phy = &addl->proto_data.sasdev_phys[i];
2229 sbuf_printf(sbp, "%s: phy %d:", sesname, i);
2230 if (ses_elm_sas_dev_phy_sata_dev(phy))
2231 /* Spec says all other fields are specific values */
2232 sbuf_printf(sbp, " SATA device\n");
2234 sbuf_printf(sbp, " SAS device type %d id %d\n",
2235 ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2236 sbuf_printf(sbp, "%s: phy %d: protocols:", sesname, i);
2237 SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2238 SES_PRINT_PORTS(phy->target_ports, "Target");
2239 sbuf_printf(sbp, "\n");
2241 sbuf_printf(sbp, "%s: phy %d: parent %jx addr %jx\n",
2243 (uintmax_t)scsi_8btou64(phy->parent_addr),
2244 (uintmax_t)scsi_8btou64(phy->phy_addr));
2247 #undef SES_PRINT_PORTS
2250 * \brief Report whether a given enclosure object is an expander.
2252 * \param enc SES softc associated with object.
2253 * \param obj Enclosure object to report for.
2255 * \return 1 if true, 0 otherwise.
2258 ses_obj_is_expander(enc_softc_t *enc, enc_element_t *obj)
2260 return (obj->enctype == ELMTYP_SAS_EXP);
2264 * \brief Print the additional element status data for this object, for SAS
2265 * type 1 objects. See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2267 * \param enc SES enclosure, needed for type identification.
2268 * \param sesname SES device name associated with the object.
2269 * \param sbp Sbuf to print to.
2270 * \param obj The object to print the data for.
2271 * \param periph_name Peripheral string associated with the object.
2274 ses_print_addl_data_sas_type1(enc_softc_t *enc, char *sesname,
2275 struct sbuf *sbp, enc_element_t *obj, char *periph_name)
2278 ses_element_t *elmpriv;
2279 struct ses_addl_status *addl;
2280 struct ses_elm_sas_expander_phy *exp_phy;
2281 struct ses_elm_sas_port_phy *port_phy;
2283 elmpriv = obj->elm_private;
2284 addl = &(elmpriv->addl);
2285 if (addl->proto_hdr.sas == NULL)
2287 sbuf_printf(sbp, "%s: %s: SAS ", sesname, periph_name);
2288 if (ses_obj_is_expander(enc, obj)) {
2289 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2290 sbuf_printf(sbp, "Expander: %d Phys", num_phys);
2291 if (addl->proto_data.sasexp_phys == NULL)
2293 for (i = 0;i < num_phys;i++) {
2294 exp_phy = &addl->proto_data.sasexp_phys[i];
2295 sbuf_printf(sbp, "%s: phy %d: connector %d other %d\n",
2296 sesname, i, exp_phy->connector_index,
2297 exp_phy->other_index);
2300 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2301 sbuf_printf(sbp, "Port: %d Phys", num_phys);
2302 if (addl->proto_data.sasport_phys == NULL)
2304 for (i = 0;i < num_phys;i++) {
2305 port_phy = &addl->proto_data.sasport_phys[i];
2307 "%s: phy %d: id %d connector %d other %d\n",
2308 sesname, i, port_phy->phy_id,
2309 port_phy->connector_index, port_phy->other_index);
2310 sbuf_printf(sbp, "%s: phy %d: addr %jx\n", sesname, i,
2311 (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2317 * \brief Print the additional element status data for this object.
2319 * \param enc SES softc associated with the object.
2320 * \param obj The object to print the data for.
2323 ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2325 ses_element_t *elmpriv;
2326 struct ses_addl_status *addl;
2327 struct sbuf sesname, name, out;
2329 elmpriv = obj->elm_private;
2330 if (elmpriv == NULL)
2333 addl = &(elmpriv->addl);
2334 if (addl->hdr == NULL)
2337 sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2338 sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2339 sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2340 ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2341 if (sbuf_len(&name) == 0)
2342 sbuf_printf(&name, "(none)");
2344 sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2345 enc->periph->unit_number);
2346 sbuf_finish(&sesname);
2347 if (elmpriv->descr != NULL)
2348 sbuf_printf(&out, "%s: %s: Element descriptor: '%s'\n",
2349 sbuf_data(&sesname), sbuf_data(&name), elmpriv->descr);
2350 switch(ses_elm_addlstatus_proto(addl->hdr)) {
2351 case SPSP_PROTO_SAS:
2352 switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2353 case SES_SASOBJ_TYPE_SLOT:
2354 ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2355 &out, obj, sbuf_data(&name));
2357 case SES_SASOBJ_TYPE_OTHER:
2358 ses_print_addl_data_sas_type1(enc, sbuf_data(&sesname),
2359 &out, obj, sbuf_data(&name));
2365 case SPSP_PROTO_FC: /* stubbed for now */
2371 printf("%s", sbuf_data(&out));
2374 sbuf_delete(&sesname);
2378 * \brief Update the softc with the additional element status data for this
2379 * object, for SAS type 0 objects.
2381 * \param enc SES softc to be updated.
2382 * \param buf The additional element status response buffer.
2383 * \param bufsiz Size of the response buffer.
2384 * \param eip The EIP bit value.
2385 * \param nobj Number of objects attached to the SES softc.
2387 * \return 0 on success, errno otherwise.
2390 ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2391 uint8_t *buf, int bufsiz, int eip, int nobj)
2393 int err, offset, physz;
2395 ses_element_t *elmpriv;
2396 struct ses_addl_status *addl;
2400 /* basic object setup */
2401 obj = &(enc_cache->elm_map[nobj]);
2402 elmpriv = obj->elm_private;
2403 addl = &(elmpriv->addl);
2405 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2407 /* Don't assume this object has any phys */
2408 bzero(&addl->proto_data, sizeof(addl->proto_data));
2409 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2412 /* Skip forward to the phy list */
2414 offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2416 offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2418 /* Make sure the phy list fits in the buffer */
2419 physz = addl->proto_hdr.sas->base_hdr.num_phys;
2420 physz *= sizeof(struct ses_elm_sas_device_phy);
2421 if (physz > (bufsiz - offset + 4)) {
2422 ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2428 /* Point to the phy list */
2429 addl->proto_data.sasdev_phys =
2430 (struct ses_elm_sas_device_phy *)&buf[offset];
2437 * \brief Update the softc with the additional element status data for this
2438 * object, for SAS type 1 objects.
2440 * \param enc SES softc to be updated.
2441 * \param buf The additional element status response buffer.
2442 * \param bufsiz Size of the response buffer.
2443 * \param eip The EIP bit value.
2444 * \param nobj Number of objects attached to the SES softc.
2446 * \return 0 on success, errno otherwise.
2449 ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2450 uint8_t *buf, int bufsiz, int eip, int nobj)
2452 int err, offset, physz;
2454 ses_element_t *elmpriv;
2455 struct ses_addl_status *addl;
2459 /* basic object setup */
2460 obj = &(enc_cache->elm_map[nobj]);
2461 elmpriv = obj->elm_private;
2462 addl = &(elmpriv->addl);
2464 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2466 /* Don't assume this object has any phys */
2467 bzero(&addl->proto_data, sizeof(addl->proto_data));
2468 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2471 /* Process expanders differently from other type1 cases */
2472 if (ses_obj_is_expander(enc, obj)) {
2473 offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2474 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2475 sizeof(struct ses_elm_sas_expander_phy);
2476 if (physz > (bufsiz - offset)) {
2477 ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2478 "End Of Buffer\n", nobj);
2482 addl->proto_data.sasexp_phys =
2483 (struct ses_elm_sas_expander_phy *)&buf[offset];
2485 offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2486 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2487 sizeof(struct ses_elm_sas_port_phy);
2488 if (physz > (bufsiz - offset + 4)) {
2489 ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2490 "Of Buffer\n", nobj);
2494 addl->proto_data.sasport_phys =
2495 (struct ses_elm_sas_port_phy *)&buf[offset];
2503 * \brief Update the softc with the additional element status data for this
2504 * object, for SAS objects.
2506 * \param enc SES softc to be updated.
2507 * \param buf The additional element status response buffer.
2508 * \param bufsiz Size of the response buffer.
2509 * \param eip The EIP bit value.
2510 * \param tidx Type index for this object.
2511 * \param nobj Number of objects attached to the SES softc.
2513 * \return 0 on success, errno otherwise.
2516 ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2517 uint8_t *buf, int bufsiz, int eip, int tidx,
2521 ses_cache_t *ses_cache;
2522 union ses_elm_sas_hdr *hdr;
2524 /* Need to be able to read the descriptor type! */
2525 if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2530 ses_cache = enc_cache->private;
2532 hdr = (union ses_elm_sas_hdr *)buf;
2533 dtype = ses_elm_sas_descr_type(hdr);
2535 case SES_SASOBJ_TYPE_SLOT:
2536 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2538 case ELMTYP_ARRAY_DEV:
2541 ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2542 "invalid for SES element type 0x%x\n", nobj,
2543 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2547 err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2551 case SES_SASOBJ_TYPE_OTHER:
2552 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2553 case ELMTYP_SAS_EXP:
2554 case ELMTYP_SCSI_INI:
2555 case ELMTYP_SCSI_TGT:
2559 ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2560 "invalid for SES element type 0x%x\n", nobj,
2561 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2565 err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2569 ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2570 "of unknown type 0x%x\n", nobj,
2571 ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2581 ses_softc_invalidate(enc_softc_t *enc)
2585 ses = enc->enc_private;
2586 ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2590 ses_softc_cleanup(enc_softc_t *enc)
2593 ses_cache_free(enc, &enc->enc_cache);
2594 ses_cache_free(enc, &enc->enc_daemon_cache);
2595 ENC_FREE_AND_NULL(enc->enc_private);
2596 ENC_FREE_AND_NULL(enc->enc_cache.private);
2597 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2601 ses_init_enc(enc_softc_t *enc)
2607 ses_get_enc_status(enc_softc_t *enc, int slpflag)
2609 /* Automatically updated, caller checks enc_cache->encstat itself */
2614 ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2616 ses_control_request_t req;
2619 ses = enc->enc_private;
2620 req.elm_idx = SES_SETSTATUS_ENC_IDX;
2621 req.elm_stat.comstatus = encstat & 0xf;
2623 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2624 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2625 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2627 return (req.result);
2631 ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2633 unsigned int i = elms->elm_idx;
2635 memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2640 ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2642 ses_control_request_t req;
2645 /* If this is clear, we don't do diddly. */
2646 if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2649 ses = enc->enc_private;
2650 req.elm_idx = elms->elm_idx;
2651 memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2653 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2654 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2655 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2657 return (req.result);
2661 ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2663 int i = (int)elmd->elm_idx;
2664 ses_element_t *elmpriv;
2666 /* Assume caller has already checked obj_id validity */
2667 elmpriv = enc->enc_cache.elm_map[i].elm_private;
2668 /* object might not have a descriptor */
2669 if (elmpriv == NULL || elmpriv->descr == NULL) {
2670 elmd->elm_desc_len = 0;
2673 if (elmd->elm_desc_len > elmpriv->descr_len)
2674 elmd->elm_desc_len = elmpriv->descr_len;
2675 copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2680 * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2681 * given object id if one is available.
2683 * \param enc SES softc to examine.
2684 * \param objdn ioctl structure to read/write device name info.
2686 * \return 0 on success, errno otherwise.
2689 ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2694 len = elmdn->elm_names_size;
2698 sbuf_new(&sb, elmdn->elm_devnames, len, 0);
2700 cam_periph_unlock(enc->periph);
2701 ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2702 ses_elmdevname_callback, &sb);
2704 elmdn->elm_names_len = sbuf_len(&sb);
2705 cam_periph_lock(enc->periph);
2706 return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2710 * \brief Send a string to the primary subenclosure using the String Out
2711 * SES diagnostic page.
2713 * \param enc SES enclosure to run the command on.
2714 * \param sstr SES string structure to operate on
2715 * \param ioc Ioctl being performed
2717 * \return 0 on success, errno otherwise.
2720 ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2722 int amt, payload, ret;
2726 /* Implement SES2r20 6.1.6 */
2727 if (sstr->bufsiz > 0xffff)
2728 return (EINVAL); /* buffer size too large */
2730 if (ioc == ENCIOC_SETSTRING) {
2731 payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2733 buf = ENC_MALLOC(payload);
2737 ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2738 /* Construct the page request */
2739 buf[0] = SesStringOut;
2741 buf[2] = sstr->bufsiz >> 8;
2742 buf[3] = sstr->bufsiz & 0xff;
2743 memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2744 } else if (ioc == ENCIOC_GETSTRING) {
2745 payload = sstr->bufsiz;
2747 ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2752 ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2753 if (ioc == ENCIOC_SETSTRING)
2759 * \invariant Called with cam_periph mutex held.
2762 ses_poll_status(enc_softc_t *enc)
2766 ses = enc->enc_private;
2767 enc_update_request(enc, SES_UPDATE_GETSTATUS);
2768 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2769 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2773 * \brief Notification received when CAM detects a new device in the
2774 * SCSI domain in which this SEP resides.
2776 * \param enc SES enclosure instance.
2779 ses_device_found(enc_softc_t *enc)
2781 ses_poll_status(enc);
2782 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2785 static struct enc_vec ses_enc_vec =
2787 .softc_invalidate = ses_softc_invalidate,
2788 .softc_cleanup = ses_softc_cleanup,
2789 .init_enc = ses_init_enc,
2790 .get_enc_status = ses_get_enc_status,
2791 .set_enc_status = ses_set_enc_status,
2792 .get_elm_status = ses_get_elm_status,
2793 .set_elm_status = ses_set_elm_status,
2794 .get_elm_desc = ses_get_elm_desc,
2795 .get_elm_devnames = ses_get_elm_devnames,
2796 .handle_string = ses_handle_string,
2797 .device_found = ses_device_found,
2798 .poll_status = ses_poll_status
2802 * \brief Initialize a new SES instance.
2804 * \param enc SES softc structure to set up the instance in.
2805 * \param doinit Do the initialization (see main driver).
2807 * \return 0 on success, errno otherwise.
2810 ses_softc_init(enc_softc_t *enc)
2812 ses_softc_t *ses_softc;
2814 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2815 ("entering enc_softc_init(%p)\n", enc));
2817 enc->enc_vec = ses_enc_vec;
2818 enc->enc_fsm_states = enc_fsm_states;
2820 if (enc->enc_private == NULL)
2821 enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2822 if (enc->enc_cache.private == NULL)
2823 enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2824 if (enc->enc_daemon_cache.private == NULL)
2825 enc->enc_daemon_cache.private =
2826 ENC_MALLOCZ(sizeof(ses_cache_t));
2828 if (enc->enc_private == NULL
2829 || enc->enc_cache.private == NULL
2830 || enc->enc_daemon_cache.private == NULL) {
2831 ENC_FREE_AND_NULL(enc->enc_private);
2832 ENC_FREE_AND_NULL(enc->enc_cache.private);
2833 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2837 ses_softc = enc->enc_private;
2838 TAILQ_INIT(&ses_softc->ses_requests);
2839 TAILQ_INIT(&ses_softc->ses_pending_requests);
2841 enc_update_request(enc, SES_UPDATE_PAGES);
2843 // XXX: Move this to the FSM so it doesn't hang init
2844 if (0) (void) ses_set_timed_completion(enc, 1);