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. */
349 const struct ses_enc_desc * const *subencs;
351 const ses_type_t *ses_types;
353 /* Source for all the status pointers */
354 const struct ses_status_page *status_page;
356 /* Source for all the object descriptor pointers */
357 const struct ses_elem_descr_page *elm_descs_page;
359 /* Source for all the additional object status pointers */
360 const struct ses_addl_elem_status_page *elm_addlstatus_page;
364 typedef struct ses_softc {
366 #define SES_FLAG_TIMEDCOMP 0x01
367 #define SES_FLAG_ADDLSTATUS 0x02
368 #define SES_FLAG_DESC 0x04
370 ses_control_reqlist_t ses_requests;
371 ses_control_reqlist_t ses_pending_requests;
375 * \brief Reset a SES iterator to just before the first element
376 * in the configuration.
378 * \param iter The iterator object to reset.
380 * The indexes within a reset iterator are invalid and will only
381 * become valid upon completion of a ses_iter_seek_to() or a
385 ses_iter_reset(struct ses_iterator *iter)
388 * Set our indexes to just before the first valid element
389 * of the first type (ITERATOR_INDEX_INVALID == -1). This
390 * simplifies the implementation of ses_iter_next().
392 iter->type_index = 0;
393 iter->type_element_index = ITERATOR_INDEX_INVALID;
394 iter->global_element_index = ITERATOR_INDEX_INVALID;
395 iter->individual_element_index = ITERATOR_INDEX_INVALID;
396 iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
400 * \brief Initialize the storage of a SES iterator and reset it to
401 * the position just before the first element of the
404 * \param enc The SES softc for the SES instance whose configuration
405 * will be enumerated by this iterator.
406 * \param iter The iterator object to initialize.
409 ses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
413 ses_iter_reset(iter);
417 * \brief Traverse the provided SES iterator to the next element
418 * within the configuraiton.
420 * \param iter The iterator to move.
422 * \return If a valid next element exists, a pointer to it's enc_element_t.
425 static enc_element_t *
426 ses_iter_next(struct ses_iterator *iter)
428 ses_cache_t *ses_cache;
429 const ses_type_t *element_type;
431 ses_cache = iter->cache->private;
434 * Note: Treat nelms as signed, so we will hit this case
435 * and immediately terminate the iteration if the
436 * configuration has 0 objects.
438 if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
440 /* Elements exhausted. */
441 iter->type_index = ITERATOR_INDEX_END;
442 iter->type_element_index = ITERATOR_INDEX_END;
443 iter->global_element_index = ITERATOR_INDEX_END;
444 iter->individual_element_index = ITERATOR_INDEX_END;
448 KASSERT((iter->type_index < ses_cache->ses_ntypes),
449 ("Corrupted element iterator. %d not less than %d",
450 iter->type_index, ses_cache->ses_ntypes));
452 element_type = &ses_cache->ses_types[iter->type_index];
453 iter->global_element_index++;
454 iter->type_element_index++;
457 * There is an object for overal type status in addition
458 * to one for each allowed element, but only if the element
461 if (iter->type_element_index > element_type->hdr->etype_maxelt) {
464 * We've exhausted the elements of this type.
465 * This next element belongs to the next type.
468 iter->type_element_index = 0;
469 iter->saved_individual_element_index
470 = iter->individual_element_index;
471 iter->individual_element_index = ITERATOR_INDEX_INVALID;
474 if (iter->type_element_index > 0) {
475 if (iter->type_element_index == 1) {
476 iter->individual_element_index
477 = iter->saved_individual_element_index;
479 iter->individual_element_index++;
482 return (&iter->cache->elm_map[iter->global_element_index]);
486 * Element index types tracked by a SES iterator.
490 * Index relative to all elements (overall and individual)
493 SES_ELEM_INDEX_GLOBAL,
496 * \brief Index relative to all individual elements in the system.
498 * This index counts only individual elements, skipping overall
499 * status elements. This is the index space of the additional
500 * element status page (page 0xa).
502 SES_ELEM_INDEX_INDIVIDUAL
503 } ses_elem_index_type_t;
506 * \brief Move the provided iterator forwards or backwards to the object
507 * having the give index.
509 * \param iter The iterator on which to perform the seek.
510 * \param element_index The index of the element to find.
511 * \param index_type The type (global or individual) of element_index.
513 * \return If the element is found, a pointer to it's enc_element_t.
516 static enc_element_t *
517 ses_iter_seek_to(struct ses_iterator *iter, int element_index,
518 ses_elem_index_type_t index_type)
520 enc_element_t *element;
523 if (index_type == SES_ELEM_INDEX_GLOBAL)
524 cur_index = &iter->global_element_index;
526 cur_index = &iter->individual_element_index;
528 if (*cur_index == element_index) {
530 return (&iter->cache->elm_map[iter->global_element_index]);
533 ses_iter_reset(iter);
534 while ((element = ses_iter_next(iter)) != NULL
535 && *cur_index != element_index)
538 if (*cur_index != element_index)
545 static int ses_encode(enc_softc_t *, uint8_t *, int, int,
546 struct ses_comstat *);
548 static int ses_set_timed_completion(enc_softc_t *, uint8_t);
550 static int ses_putstatus(enc_softc_t *, int, struct ses_comstat *);
553 static void ses_poll_status(enc_softc_t *);
554 static void ses_print_addl_data(enc_softc_t *, enc_element_t *);
556 /*=========================== SES cleanup routines ===========================*/
559 ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
561 ses_cache_t *ses_cache;
562 ses_cache_t *other_ses_cache;
563 enc_element_t *cur_elm;
564 enc_element_t *last_elm;
566 ENC_DLOG(enc, "%s: enter\n", __func__);
567 ses_cache = cache->private;
568 if (ses_cache->elm_addlstatus_page == NULL)
571 for (cur_elm = cache->elm_map,
572 last_elm = &cache->elm_map[cache->nelms];
573 cur_elm != last_elm; cur_elm++) {
574 ses_element_t *elmpriv;
576 elmpriv = cur_elm->elm_private;
578 /* Clear references to the additional status page. */
579 bzero(&elmpriv->addl, sizeof(elmpriv->addl));
582 other_ses_cache = enc_other_cache(enc, cache)->private;
583 if (other_ses_cache->elm_addlstatus_page
584 != ses_cache->elm_addlstatus_page)
585 ENC_FREE(ses_cache->elm_addlstatus_page);
586 ses_cache->elm_addlstatus_page = NULL;
590 ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
592 ses_cache_t *ses_cache;
593 ses_cache_t *other_ses_cache;
594 enc_element_t *cur_elm;
595 enc_element_t *last_elm;
597 ENC_DLOG(enc, "%s: enter\n", __func__);
598 ses_cache = cache->private;
599 if (ses_cache->elm_descs_page == NULL)
602 for (cur_elm = cache->elm_map,
603 last_elm = &cache->elm_map[cache->nelms];
604 cur_elm != last_elm; cur_elm++) {
605 ses_element_t *elmpriv;
607 elmpriv = cur_elm->elm_private;
608 elmpriv->descr_len = 0;
609 elmpriv->descr = NULL;
612 other_ses_cache = enc_other_cache(enc, cache)->private;
613 if (other_ses_cache->elm_descs_page
614 != ses_cache->elm_descs_page)
615 ENC_FREE(ses_cache->elm_descs_page);
616 ses_cache->elm_descs_page = NULL;
620 ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
622 ses_cache_t *ses_cache;
623 ses_cache_t *other_ses_cache;
625 ENC_DLOG(enc, "%s: enter\n", __func__);
626 ses_cache = cache->private;
627 if (ses_cache->status_page == NULL)
630 other_ses_cache = enc_other_cache(enc, cache)->private;
631 if (other_ses_cache->status_page != ses_cache->status_page)
632 ENC_FREE(ses_cache->status_page);
633 ses_cache->status_page = NULL;
637 ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
639 enc_element_t *cur_elm;
640 enc_element_t *last_elm;
642 ENC_DLOG(enc, "%s: enter\n", __func__);
643 if (cache->elm_map == NULL)
646 ses_cache_free_elm_descs(enc, cache);
647 ses_cache_free_elm_addlstatus(enc, cache);
648 for (cur_elm = cache->elm_map,
649 last_elm = &cache->elm_map[cache->nelms];
650 cur_elm != last_elm; cur_elm++) {
652 ENC_FREE_AND_NULL(cur_elm->elm_private);
654 ENC_FREE_AND_NULL(cache->elm_map);
656 ENC_DLOG(enc, "%s: exit\n", __func__);
660 ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
662 ses_cache_t *other_ses_cache;
663 ses_cache_t *ses_cache;
665 ENC_DLOG(enc, "%s: enter\n", __func__);
666 ses_cache_free_elm_addlstatus(enc, cache);
667 ses_cache_free_status(enc, cache);
668 ses_cache_free_elm_map(enc, cache);
670 ses_cache = cache->private;
671 ses_cache->ses_ntypes = 0;
673 other_ses_cache = enc_other_cache(enc, cache)->private;
674 if (other_ses_cache->subencs != ses_cache->subencs)
675 ENC_FREE(ses_cache->subencs);
676 ses_cache->subencs = NULL;
678 if (other_ses_cache->ses_types != ses_cache->ses_types)
679 ENC_FREE(ses_cache->ses_types);
680 ses_cache->ses_types = NULL;
682 if (other_ses_cache->cfg_page != ses_cache->cfg_page)
683 ENC_FREE(ses_cache->cfg_page);
684 ses_cache->cfg_page = NULL;
686 ENC_DLOG(enc, "%s: exit\n", __func__);
690 ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
692 ses_cache_t *dst_ses_cache;
693 ses_cache_t *src_ses_cache;
694 enc_element_t *src_elm;
695 enc_element_t *dst_elm;
696 enc_element_t *last_elm;
698 ses_cache_free(enc, dst);
699 src_ses_cache = src->private;
700 dst_ses_cache = dst->private;
703 * The cloned enclosure cache and ses specific cache are
704 * mostly identical to the source.
707 *dst_ses_cache = *src_ses_cache;
710 * But the ses cache storage is still independent. Restore
711 * the pointer that was clobbered by the structure copy above.
713 dst->private = dst_ses_cache;
716 * The element map is independent even though it starts out
717 * pointing to the same constant page data.
719 dst->elm_map = malloc(dst->nelms * sizeof(enc_element_t),
720 M_SCSIENC, M_WAITOK);
721 memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
722 for (dst_elm = dst->elm_map, src_elm = src->elm_map,
723 last_elm = &src->elm_map[src->nelms];
724 src_elm != last_elm; src_elm++, dst_elm++) {
726 dst_elm->elm_private = malloc(sizeof(ses_element_t),
727 M_SCSIENC, M_WAITOK);
728 memcpy(dst_elm->elm_private, src_elm->elm_private,
729 sizeof(ses_element_t));
733 /* Structure accessors. These are strongly typed to avoid errors. */
736 ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
738 return ((obj)->base_hdr.byte1 >> 6);
741 ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
743 return ((hdr)->byte0 & 0xf);
746 ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
748 return ((hdr)->byte0 >> 4) & 0x1;
751 ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
753 return ((hdr)->byte0 >> 7);
756 ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
758 return ((hdr)->type0_noneip.byte1 & 0x1);
761 ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
763 return ((phy)->target_ports & 0x1);
766 ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
768 return ((phy)->target_ports >> 7);
771 ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
773 return (((phy)->byte0 >> 4) & 0x7);
777 * \brief Verify that the cached configuration data in our softc
778 * is valid for processing the page data corresponding to
779 * the provided page header.
781 * \param ses_cache The SES cache to validate.
782 * \param gen_code The 4 byte generation code from a SES diagnostic
785 * \return non-zero if true, 0 if false.
788 ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
793 if (ses_cache->cfg_page == NULL)
796 cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
797 cur_gc = scsi_4btoul(gen_code);
798 return (cache_gc == cur_gc);
802 * Function signature for consumers of the ses_devids_iter() interface.
804 typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
805 struct scsi_vpd_id_descriptor *, void *);
808 * \brief Iterate over and create vpd device id records from the
809 * additional element status data for elm, passing that data
810 * to the provided callback.
812 * \param enc SES instance containing elm
813 * \param elm Element for which to extract device ID data.
814 * \param callback The callback function to invoke on each generated
815 * device id descriptor for elm.
816 * \param callback_arg Argument passed through to callback on each invocation.
819 ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
820 ses_devid_callback_t *callback, void *callback_arg)
822 ses_element_t *elmpriv;
823 struct ses_addl_status *addl;
825 size_t devid_record_size;
827 elmpriv = elm->elm_private;
828 addl = &(elmpriv->addl);
831 * Don't assume this object has additional status information, or
832 * that it is a SAS device, or that it is a device slot device.
834 if (addl->hdr == NULL || addl->proto_hdr.sas == NULL
835 || addl->proto_data.sasdev_phys == NULL)
838 devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
839 + sizeof(struct scsi_vpd_id_naa_ieee_reg);
840 for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
841 uint8_t devid_buf[devid_record_size];
842 struct scsi_vpd_id_descriptor *devid;
845 devid = (struct scsi_vpd_id_descriptor *)devid_buf;
846 phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
847 devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
848 | SVPD_ID_CODESET_BINARY;
849 devid->id_type = SVPD_ID_PIV
853 devid->length = sizeof(struct scsi_vpd_id_naa_ieee_reg);
854 memcpy(devid->identifier, phy_addr, devid->length);
856 callback(enc, elm, devid, callback_arg);
861 * Function signature for consumers of the ses_paths_iter() interface.
863 typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
864 struct cam_path *, void *);
867 * Argument package passed through ses_devids_iter() by
868 * ses_paths_iter() to ses_path_iter_devid_callback().
870 typedef struct ses_path_iter_args {
871 ses_path_callback_t *callback;
873 } ses_path_iter_args_t;
876 * ses_devids_iter() callback function used by ses_paths_iter()
877 * to map device ids to peripheral driver instances.
879 * \param enc SES instance containing elm
880 * \param elm Element on which device ID matching is active.
881 * \param periph A device ID corresponding to elm.
882 * \param arg Argument passed through to callback on each invocation.
885 ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
886 struct scsi_vpd_id_descriptor *devid,
889 struct ccb_dev_match cdm;
890 struct dev_match_pattern match_pattern;
891 struct dev_match_result match_result;
892 struct device_match_result *device_match;
893 struct device_match_pattern *device_pattern;
894 ses_path_iter_args_t *args;
896 args = (ses_path_iter_args_t *)arg;
897 match_pattern.type = DEV_MATCH_DEVICE;
898 device_pattern = &match_pattern.pattern.device_pattern;
899 device_pattern->flags = DEV_MATCH_DEVID;
900 device_pattern->data.devid_pat.id_len =
901 offsetof(struct scsi_vpd_id_descriptor, identifier)
903 memcpy(device_pattern->data.devid_pat.id, devid,
904 device_pattern->data.devid_pat.id_len);
906 memset(&cdm, 0, sizeof(cdm));
907 if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
910 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
913 cdm.ccb_h.func_code = XPT_DEV_MATCH;
914 cdm.num_patterns = 1;
915 cdm.patterns = &match_pattern;
916 cdm.pattern_buf_len = sizeof(match_pattern);
917 cdm.match_buf_len = sizeof(match_result);
918 cdm.matches = &match_result;
920 xpt_action((union ccb *)&cdm);
921 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(&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 xpt_free_path(cdm.ccb_h.path);
942 * \brief Iterate over and find the matching periph objects for the
945 * \param enc SES instance containing elm
946 * \param elm Element for which to perform periph object matching.
947 * \param callback The callback function to invoke with each matching
949 * \param callback_arg Argument passed through to callback on each invocation.
952 ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
953 ses_path_callback_t *callback, void *callback_arg)
955 ses_path_iter_args_t args;
957 args.callback = callback;
958 args.callback_arg = callback_arg;
959 ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
963 * ses_paths_iter() callback function used by ses_get_elmdevname()
964 * to record periph driver instance strings corresponding to a SES
967 * \param enc SES instance containing elm
968 * \param elm Element on which periph matching is active.
969 * \param periph A periph instance that matches elm.
970 * \param arg Argument passed through to callback on each invocation.
973 ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
974 struct cam_path *path, void *arg)
978 sb = (struct sbuf *)arg;
979 cam_periph_list(path, sb);
983 * Argument package passed through ses_paths_iter() to
984 * ses_getcampath_callback.
986 typedef struct ses_setphyspath_callback_args {
987 struct sbuf *physpath;
989 } ses_setphyspath_callback_args_t;
992 * \brief ses_paths_iter() callback to set the physical path on the
993 * CAM EDT entries corresponding to a given SES element.
995 * \param enc SES instance containing elm
996 * \param elm Element on which periph matching is active.
997 * \param periph A periph instance that matches elm.
998 * \param arg Argument passed through to callback on each invocation.
1001 ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1002 struct cam_path *path, void *arg)
1004 struct ccb_dev_advinfo cdai;
1005 ses_setphyspath_callback_args_t *args;
1008 args = (ses_setphyspath_callback_args_t *)arg;
1009 old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1010 cam_periph_lock(enc->periph);
1011 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1012 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1013 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1014 cdai.flags = CDAI_FLAG_NONE;
1015 cdai.bufsiz = MAXPATHLEN;
1016 cdai.buf = old_physpath;
1017 xpt_action((union ccb *)&cdai);
1018 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1019 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1021 if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1023 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1024 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1025 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1026 cdai.flags = CDAI_FLAG_STORE;
1027 cdai.bufsiz = sbuf_len(args->physpath);
1028 cdai.buf = sbuf_data(args->physpath);
1029 xpt_action((union ccb *)&cdai);
1030 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1031 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1032 if (cdai.ccb_h.status == CAM_REQ_CMP)
1035 cam_periph_unlock(enc->periph);
1036 free(old_physpath, M_SCSIENC);
1040 * \brief Set a device's physical path string in CAM XPT.
1042 * \param enc SES instance containing elm
1043 * \param elm Element to publish physical path string for
1044 * \param iter Iterator whose state corresponds to elm
1046 * \return 0 on success, errno otherwise.
1049 ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1050 struct ses_iterator *iter)
1052 struct ccb_dev_advinfo cdai;
1053 ses_setphyspath_callback_args_t args;
1056 struct scsi_vpd_id_descriptor *idd;
1058 ses_element_t *elmpriv;
1065 * Assemble the components of the physical path starting with
1066 * the device ID of the enclosure itself.
1068 xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1069 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1070 cdai.flags = CDAI_FLAG_NONE;
1071 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1072 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1073 cdai.buf = devid = malloc(cdai.bufsiz, M_SCSIENC, M_WAITOK|M_ZERO);
1074 cam_periph_lock(enc->periph);
1075 xpt_action((union ccb *)&cdai);
1076 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1077 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1078 cam_periph_unlock(enc->periph);
1079 if (cdai.ccb_h.status != CAM_REQ_CMP)
1082 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1083 cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1087 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1091 /* Next, generate the physical path string */
1092 sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1093 scsi_8btou64(idd->identifier), iter->type_index,
1094 iter->type_element_index);
1095 /* Append the element descriptor if one exists */
1096 elmpriv = elm->elm_private;
1097 if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1098 sbuf_cat(&sb, "/elmdesc@");
1099 for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1101 if (!isprint(*c) || isspace(*c) || *c == '/')
1102 sbuf_putc(&sb, '_');
1110 * Set this physical path on any CAM devices with a device ID
1111 * descriptor that matches one created from the SES additional
1112 * status data for this element.
1116 ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1119 ret = args.num_set == 0 ? ENOENT : 0;
1128 * \brief Helper to set the CDB fields appropriately.
1130 * \param cdb Buffer containing the cdb.
1131 * \param pagenum SES diagnostic page to query for.
1132 * \param dir Direction of query.
1135 ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1138 /* Ref: SPC-4 r25 Section 6.20 Table 223 */
1139 if (dir == CAM_DIR_IN) {
1140 cdb[0] = RECEIVE_DIAGNOSTIC;
1141 cdb[1] = 1; /* Set page code valid bit */
1144 cdb[0] = SEND_DIAGNOSTIC;
1148 cdb[3] = bufsiz >> 8; /* high bits */
1149 cdb[4] = bufsiz & 0xff; /* low bits */
1154 * \brief Discover whether this instance supports timed completion of a
1155 * RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1156 * page, and store the result in the softc, updating if necessary.
1158 * \param enc SES instance to query and update.
1159 * \param tc_en Value of timed completion to set (see \return).
1161 * \return 1 if timed completion enabled, 0 otherwise.
1164 ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1167 struct cam_periph *periph;
1168 struct ses_mgmt_mode_page *mgmt;
1170 size_t mode_buf_len;
1173 periph = enc->periph;
1174 ses = enc->enc_private;
1175 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1177 mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1178 mode_buf = ENC_MALLOCZ(mode_buf_len);
1179 if (mode_buf == NULL)
1182 scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1183 /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1184 mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1187 * Ignore illegal request errors, as they are quite common and we
1188 * will print something out in that case anyway.
1190 cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1191 ENC_FLAGS|SF_QUIET_IR, NULL);
1192 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1193 ENC_VLOG(enc, "Timed Completion Unsupported\n");
1197 /* Skip the mode select if the desired value is already set */
1198 mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1199 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1202 /* Value is not what we wanted, set it */
1204 mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1206 mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1207 /* SES2r20: a completion time of zero means as long as possible */
1208 bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1210 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1211 /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1212 SSD_FULL_SIZE, /*timeout*/60 * 1000);
1214 cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1215 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1216 ENC_VLOG(enc, "Timed Completion Set Failed\n");
1221 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1222 ENC_LOG(enc, "Timed Completion Enabled\n");
1223 ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1225 ENC_LOG(enc, "Timed Completion Disabled\n");
1226 ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1230 xpt_release_ccb(ccb);
1232 return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1236 * \brief Process the list of supported pages and update flags.
1238 * \param enc SES device to query.
1239 * \param buf Buffer containing the config page.
1240 * \param xfer_len Length of the config page in the buffer.
1242 * \return 0 on success, errno otherwise.
1245 ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1246 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1249 struct scsi_diag_page *page;
1252 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1253 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1254 ses = enc->enc_private;
1261 if (xfer_len < sizeof(*page)) {
1262 ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1266 page = (struct scsi_diag_page *)*bufp;
1267 length = scsi_2btoul(page->length);
1268 if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1269 ENC_VLOG(enc, "Diag Pages List Too Long\n");
1272 ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1273 __func__, length, xfer_len);
1276 for (i = 0; i < length; i++) {
1277 if (page->params[i] == SesElementDescriptor)
1278 ses->ses_flags |= SES_FLAG_DESC;
1279 else if (page->params[i] == SesAddlElementStatus)
1280 ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1284 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1289 * \brief Process the config page and update associated structures.
1291 * \param enc SES device to query.
1292 * \param buf Buffer containing the config page.
1293 * \param xfer_len Length of the config page in the buffer.
1295 * \return 0 on success, errno otherwise.
1298 ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1299 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1301 struct ses_iterator iter;
1303 enc_cache_t *enc_cache;
1304 ses_cache_t *ses_cache;
1310 struct ses_cfg_page *cfg_page;
1311 struct ses_enc_desc *buf_subenc;
1312 const struct ses_enc_desc **subencs;
1313 const struct ses_enc_desc **cur_subenc;
1314 const struct ses_enc_desc **last_subenc;
1315 ses_type_t *ses_types;
1316 ses_type_t *sestype;
1317 const struct ses_elm_type_desc *cur_buf_type;
1318 const struct ses_elm_type_desc *last_buf_type;
1319 uint8_t *last_valid_byte;
1320 enc_element_t *element;
1321 const char *type_text;
1323 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1324 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1325 ses = enc->enc_private;
1326 enc_cache = &enc->enc_daemon_cache;
1327 ses_cache = enc_cache->private;
1335 if (xfer_len < sizeof(cfg_page->hdr)) {
1336 ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1341 cfg_page = (struct ses_cfg_page *)buf;
1342 length = ses_page_length(&cfg_page->hdr);
1343 if (length > xfer_len) {
1344 ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1347 last_valid_byte = &buf[length - 1];
1349 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1350 __func__, length, xfer_len);
1353 if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1355 /* Our cache is still valid. Proceed to fetching status. */
1359 /* Cache is no longer valid. Free old data to make way for new. */
1360 ses_cache_free(enc, enc_cache);
1361 ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1362 scsi_4btoul(cfg_page->hdr.gen_code),
1363 ses_cfg_page_get_num_subenc(cfg_page));
1365 /* Take ownership of the buffer. */
1366 ses_cache->cfg_page = cfg_page;
1370 * Now waltz through all the subenclosures summing the number of
1371 * types available in each.
1373 subencs = malloc(ses_cfg_page_get_num_subenc(cfg_page)
1374 * sizeof(*subencs), M_SCSIENC, M_WAITOK|M_ZERO);
1376 * Sub-enclosure data is const after construction (i.e. when
1377 * accessed via our cache object.
1379 * The cast here is not required in C++ but C99 is not so
1380 * sophisticated (see C99 6.5.16.1(1)).
1382 ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1383 ses_cache->subencs = subencs;
1385 buf_subenc = cfg_page->subencs;
1386 cur_subenc = subencs;
1387 last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1389 while (cur_subenc <= last_subenc) {
1391 if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1392 ENC_VLOG(enc, "Enclosure %d Beyond End of "
1393 "Descriptors\n", cur_subenc - subencs);
1398 ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1399 "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1400 buf_subenc->num_types, buf_subenc->length,
1401 &buf_subenc->byte0 - buf);
1402 ENC_VLOG(enc, "WWN: %jx\n",
1403 (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1405 ntype += buf_subenc->num_types;
1406 *cur_subenc = buf_subenc;
1408 buf_subenc = ses_enc_desc_next(buf_subenc);
1411 /* Process the type headers. */
1412 ses_types = malloc(ntype * sizeof(*ses_types),
1413 M_SCSIENC, M_WAITOK|M_ZERO);
1415 * Type data is const after construction (i.e. when accessed via
1418 ses_cache->ses_ntypes = ntype;
1419 ses_cache->ses_types = ses_types;
1421 cur_buf_type = (const struct ses_elm_type_desc *)
1422 (&(*last_subenc)->length + (*last_subenc)->length + 1);
1423 last_buf_type = cur_buf_type + ntype - 1;
1424 type_text = (const uint8_t *)(last_buf_type + 1);
1426 sestype = ses_types;
1427 while (cur_buf_type <= last_buf_type) {
1428 if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1429 ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1430 sestype - ses_types);
1434 sestype->hdr = cur_buf_type;
1435 sestype->text = type_text;
1436 type_text += cur_buf_type->etype_txt_len;
1437 ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1438 "%d, Text Length %d: %.*s\n", sestype - ses_types,
1439 sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1440 sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1441 sestype->hdr->etype_txt_len, sestype->text);
1443 nelm += sestype->hdr->etype_maxelt
1444 + /*overall status element*/1;
1449 /* Create the object map. */
1450 enc_cache->elm_map = malloc(nelm * sizeof(enc_element_t),
1451 M_SCSIENC, M_WAITOK|M_ZERO);
1452 enc_cache->nelms = nelm;
1454 ses_iter_init(enc, enc_cache, &iter);
1455 while ((element = ses_iter_next(&iter)) != NULL) {
1456 const struct ses_elm_type_desc *thdr;
1458 ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1459 iter.global_element_index, iter.type_index, nelm,
1460 iter.type_element_index);
1461 thdr = ses_cache->ses_types[iter.type_index].hdr;
1462 element->subenclosure = thdr->etype_subenc;
1463 element->enctype = thdr->etype_elm_type;
1464 element->overall_status_elem = iter.type_element_index == 0;
1465 element->elm_private = malloc(sizeof(ses_element_t),
1466 M_SCSIENC, M_WAITOK|M_ZERO);
1467 ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1468 "type 0x%x\n", __func__, iter.global_element_index,
1469 iter.type_index, iter.type_element_index,
1470 thdr->etype_subenc, thdr->etype_elm_type);
1477 ses_cache_free(enc, enc_cache);
1479 ses_poll_status(enc);
1480 enc_update_request(enc, SES_PUBLISH_CACHE);
1482 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1487 * \brief Update the status page and associated structures.
1489 * \param enc SES softc to update for.
1490 * \param buf Buffer containing the status page.
1491 * \param bufsz Amount of data in the buffer.
1493 * \return 0 on success, errno otherwise.
1496 ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1497 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1499 struct ses_iterator iter;
1500 enc_element_t *element;
1502 enc_cache_t *enc_cache;
1503 ses_cache_t *ses_cache;
1507 struct ses_status_page *page;
1508 union ses_status_element *cur_stat;
1509 union ses_status_element *last_stat;
1511 ses = enc->enc_private;
1512 enc_cache = &enc->enc_daemon_cache;
1513 ses_cache = enc_cache->private;
1516 ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1517 page = (struct ses_status_page *)buf;
1518 length = ses_page_length(&page->hdr);
1525 * Make sure the length fits in the buffer.
1527 * XXX all this means is that the page is larger than the space
1528 * we allocated. Since we use a statically sized buffer, this
1529 * could happen... Need to use dynamic discovery of the size.
1531 if (length > xfer_len) {
1532 ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1536 /* Check for simple enclosure reporting short enclosure status. */
1537 if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1538 ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1539 ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1540 ses_cache_free(enc, enc_cache);
1541 enc_cache->enc_status = page->hdr.page_specific_flags;
1542 enc_update_request(enc, SES_PUBLISH_CACHE);
1547 /* Make sure the length contains at least one header and status */
1548 if (length < (sizeof(*page) + sizeof(*page->elements))) {
1549 ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1553 if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1554 ENC_DLOG(enc, "%s: Generation count change detected\n",
1556 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1560 ses_cache_free_status(enc, enc_cache);
1561 ses_cache->status_page = page;
1564 enc_cache->enc_status = page->hdr.page_specific_flags;
1567 * Read in individual element status. The element order
1568 * matches the order reported in the config page (i.e. the
1569 * order of an unfiltered iteration of the config objects)..
1571 ses_iter_init(enc, enc_cache, &iter);
1572 cur_stat = page->elements;
1573 last_stat = (union ses_status_element *)
1574 &buf[length - sizeof(*last_stat)];
1575 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1576 __func__, length, xfer_len);
1577 while (cur_stat <= last_stat
1578 && (element = ses_iter_next(&iter)) != NULL) {
1580 ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1581 __func__, iter.global_element_index, iter.type_index,
1582 iter.type_element_index, (uint8_t *)cur_stat - buf,
1583 scsi_4btoul(cur_stat->bytes));
1585 memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1586 element->svalid = 1;
1590 if (ses_iter_next(&iter) != NULL) {
1591 ENC_VLOG(enc, "Status page, length insufficient for "
1592 "expected number of objects\n");
1594 if (cur_stat <= last_stat)
1595 ENC_VLOG(enc, "Status page, exhausted objects before "
1596 "exhausing page\n");
1597 enc_update_request(enc, SES_PUBLISH_CACHE);
1601 ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1607 * The enclosure should not provide additional element
1608 * status for this element type in page 0x0A.
1610 * \note This status is returned for any types not
1611 * listed SES3r02. Further types added in a
1612 * future specification will be incorrectly
1615 TYPE_ADDLSTATUS_NONE,
1618 * The element type provides additional element status
1621 TYPE_ADDLSTATUS_MANDATORY,
1624 * The element type may provide additional element status
1625 * in page 0x0A, but i
1627 TYPE_ADDLSTATUS_OPTIONAL
1628 } ses_addlstatus_avail_t;
1631 * \brief Check to see whether a given type (as obtained via type headers) is
1632 * supported by the additional status command.
1634 * \param enc SES softc to check.
1635 * \param typidx Type index to check for.
1637 * \return An enumeration indicating if additional status is mandatory,
1638 * optional, or not required for this type.
1640 static ses_addlstatus_avail_t
1641 ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1643 enc_cache_t *enc_cache;
1644 ses_cache_t *ses_cache;
1646 enc_cache = &enc->enc_daemon_cache;
1647 ses_cache = enc_cache->private;
1648 switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1650 case ELMTYP_ARRAY_DEV:
1651 case ELMTYP_SAS_EXP:
1652 return (TYPE_ADDLSTATUS_MANDATORY);
1653 case ELMTYP_SCSI_INI:
1654 case ELMTYP_SCSI_TGT:
1656 return (TYPE_ADDLSTATUS_OPTIONAL);
1658 /* No additional status information available. */
1661 return (TYPE_ADDLSTATUS_NONE);
1664 static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1666 static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1667 int, int, int, int);
1670 * \brief Parse the additional status element data for each object.
1672 * \param enc The SES softc to update.
1673 * \param buf The buffer containing the additional status
1675 * \param xfer_len Size of the buffer.
1677 * \return 0 on success, errno otherwise.
1680 ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1681 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1683 struct ses_iterator iter, titer;
1686 int ignore_index = 0;
1689 enc_cache_t *enc_cache;
1690 ses_cache_t *ses_cache;
1692 ses_element_t *elmpriv;
1693 const struct ses_page_hdr *hdr;
1694 enc_element_t *element, *telement;
1696 enc_cache = &enc->enc_daemon_cache;
1697 ses_cache = enc_cache->private;
1705 ses_cache_free_elm_addlstatus(enc, enc_cache);
1706 ses_cache->elm_addlstatus_page =
1707 (struct ses_addl_elem_status_page *)buf;
1711 * The objects appear in the same order here as in Enclosure Status,
1712 * which itself is ordered by the Type Descriptors from the Config
1713 * page. However, it is necessary to skip elements that are not
1714 * supported by this page when counting them.
1716 hdr = &ses_cache->elm_addlstatus_page->hdr;
1717 length = ses_page_length(hdr);
1718 ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1719 /* Make sure the length includes at least one header. */
1720 if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1721 ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1724 if (length > xfer_len) {
1725 ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1729 if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1730 ENC_DLOG(enc, "%s: Generation count change detected\n",
1732 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1736 offset = sizeof(struct ses_page_hdr);
1737 ses_iter_init(enc, enc_cache, &iter);
1738 while (offset < length
1739 && (element = ses_iter_next(&iter)) != NULL) {
1740 struct ses_elm_addlstatus_base_hdr *elm_hdr;
1742 ses_addlstatus_avail_t status_type;
1745 * Additional element status is only provided for
1746 * individual elements (i.e. overal status elements
1747 * are excluded) and those of the types specified
1750 status_type = ses_typehasaddlstatus(enc, iter.type_index);
1751 if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1752 || status_type == TYPE_ADDLSTATUS_NONE)
1755 elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1756 eip = ses_elm_addlstatus_eip(elm_hdr);
1757 if (eip && !ignore_index) {
1758 struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1759 int expected_index, index;
1760 ses_elem_index_type_t index_type;
1762 eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1763 if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) {
1764 index_type = SES_ELEM_INDEX_GLOBAL;
1765 expected_index = iter.global_element_index;
1767 index_type = SES_ELEM_INDEX_INDIVIDUAL;
1768 expected_index = iter.individual_element_index;
1771 telement = ses_iter_seek_to(&titer,
1772 eip_hdr->element_index, index_type);
1773 if (telement != NULL &&
1774 (ses_typehasaddlstatus(enc, titer.type_index) !=
1775 TYPE_ADDLSTATUS_NONE ||
1776 titer.type_index > ELMTYP_SAS_CONN)) {
1782 if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE)
1783 index = iter.global_element_index;
1785 index = iter.individual_element_index;
1786 if (index > expected_index
1787 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1788 ENC_VLOG(enc, "%s: provided %s element"
1789 "index %d skips mandatory status "
1790 " element at index %d\n",
1791 __func__, (eip_hdr->byte2 &
1792 SES_ADDL_EIP_EIIOE) ? "global " : "",
1793 index, expected_index);
1796 elmpriv = element->elm_private;
1797 elmpriv->addl.hdr = elm_hdr;
1798 ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1799 "type element index=%d, offset=0x%x, "
1800 "byte0=0x%x, length=0x%x\n", __func__,
1801 iter.global_element_index, iter.type_index,
1802 iter.type_element_index, offset, elmpriv->addl.hdr->byte0,
1803 elmpriv->addl.hdr->length);
1805 /* Skip to after the length field */
1806 offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1808 /* Make sure the descriptor is within bounds */
1809 if ((offset + elmpriv->addl.hdr->length) > length) {
1810 ENC_VLOG(enc, "Element %d Beyond End "
1811 "of Additional Element Status Descriptors\n",
1812 iter.global_element_index);
1816 /* Advance to the protocol data, skipping eip bytes if needed */
1817 offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1818 proto_info_len = elmpriv->addl.hdr->length
1819 - (eip * SES_EIP_HDR_EXTRA_LEN);
1821 /* Errors in this block are ignored as they are non-fatal */
1822 switch(ses_elm_addlstatus_proto(elmpriv->addl.hdr)) {
1824 if (elmpriv->addl.hdr->length == 0)
1826 ses_get_elm_addlstatus_fc(enc, enc_cache,
1827 &buf[offset], proto_info_len);
1829 case SPSP_PROTO_SAS:
1830 if (elmpriv->addl.hdr->length <= 2)
1832 ses_get_elm_addlstatus_sas(enc, enc_cache,
1835 eip, iter.type_index,
1836 iter.global_element_index);
1839 ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1840 "Protocol 0x%x\n", iter.global_element_index,
1841 ses_elm_addlstatus_proto(elmpriv->addl.hdr));
1845 offset += proto_info_len;
1850 ses_cache_free_elm_addlstatus(enc, enc_cache);
1851 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1852 enc_update_request(enc, SES_PUBLISH_CACHE);
1857 ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1858 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1862 ses = enc->enc_private;
1865 * o Generation count wrong.
1866 * o Some SCSI status error.
1868 ses_terminate_control_requests(&ses->ses_pending_requests, error);
1869 ses_poll_status(enc);
1874 ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1875 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1877 struct ses_iterator iter;
1878 enc_cache_t *enc_cache;
1879 enc_element_t *element;
1881 enc_cache = &enc->enc_daemon_cache;
1883 ses_iter_init(enc, enc_cache, &iter);
1884 while ((element = ses_iter_next(&iter)) != NULL) {
1886 * ses_set_physpath() returns success if we changed
1887 * the physpath of any element. This allows us to
1888 * only announce devices once regardless of how
1889 * many times we process additional element status.
1891 if (ses_set_physpath(enc, element, &iter) == 0)
1892 ses_print_addl_data(enc, element);
1899 ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1900 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1903 sx_xlock(&enc->enc_cache_lock);
1904 ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1905 /*dst*/&enc->enc_cache);
1906 sx_xunlock(&enc->enc_cache_lock);
1912 * \brief Parse the descriptors for each object.
1914 * \param enc The SES softc to update.
1915 * \param buf The buffer containing the descriptor list response.
1916 * \param xfer_len Size of the buffer.
1918 * \return 0 on success, errno otherwise.
1921 ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1922 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1925 struct ses_iterator iter;
1926 enc_element_t *element;
1929 u_long length, plength;
1930 enc_cache_t *enc_cache;
1931 ses_cache_t *ses_cache;
1933 ses_element_t *elmpriv;
1934 const struct ses_page_hdr *phdr;
1935 const struct ses_elm_desc_hdr *hdr;
1937 ses = enc->enc_private;
1938 enc_cache = &enc->enc_daemon_cache;
1939 ses_cache = enc_cache->private;
1947 ses_cache_free_elm_descs(enc, enc_cache);
1948 ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
1951 phdr = &ses_cache->elm_descs_page->hdr;
1952 plength = ses_page_length(phdr);
1953 if (xfer_len < sizeof(struct ses_page_hdr)) {
1954 ENC_VLOG(enc, "Runt Element Descriptor Page\n");
1957 if (plength > xfer_len) {
1958 ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
1962 if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
1963 ENC_VLOG(enc, "%s: Generation count change detected\n",
1965 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1969 offset = sizeof(struct ses_page_hdr);
1971 ses_iter_init(enc, enc_cache, &iter);
1972 while (offset < plength
1973 && (element = ses_iter_next(&iter)) != NULL) {
1975 if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
1976 ENC_VLOG(enc, "Element %d Descriptor Header Past "
1977 "End of Buffer\n", iter.global_element_index);
1980 hdr = (struct ses_elm_desc_hdr *)&buf[offset];
1981 length = scsi_2btoul(hdr->length);
1982 ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
1983 iter.global_element_index, iter.type_index,
1984 iter.type_element_index, length, offset);
1985 if ((offset + sizeof(*hdr) + length) > plength) {
1986 ENC_VLOG(enc, "Element%d Descriptor Past "
1987 "End of Buffer\n", iter.global_element_index);
1990 offset += sizeof(*hdr);
1993 elmpriv = element->elm_private;
1994 elmpriv->descr_len = length;
1995 elmpriv->descr = &buf[offset];
1998 /* skip over the descriptor itself */
2005 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2006 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2008 enc_update_request(enc, SES_PUBLISH_CACHE);
2013 ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2014 union ccb *ccb, uint8_t *buf)
2017 if (enc->enc_type == ENC_SEMB_SES) {
2018 semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2019 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2020 state->page_code, buf, state->buf_size,
2023 scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2024 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2025 state->page_code, buf, state->buf_size,
2026 SSD_FULL_SIZE, state->timeout);
2032 * \brief Encode the object status into the response buffer, which is
2033 * expected to contain the current enclosure status. This function
2034 * turns off all the 'select' bits for the objects except for the
2035 * object specified, then sends it back to the enclosure.
2037 * \param enc SES enclosure the change is being applied to.
2038 * \param buf Buffer containing the current enclosure status response.
2039 * \param amt Length of the response in the buffer.
2040 * \param req The control request to be applied to buf.
2042 * \return 0 on success, errno otherwise.
2045 ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2047 struct ses_iterator iter;
2048 enc_element_t *element;
2050 struct ses_control_page_hdr *hdr;
2052 ses_iter_init(enc, &enc->enc_cache, &iter);
2053 hdr = (struct ses_control_page_hdr *)buf;
2054 if (req->elm_idx == -1) {
2055 /* for enclosure status, at least 2 bytes are needed */
2058 hdr->control_flags =
2059 req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2060 ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2064 element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2065 if (element == NULL)
2069 * Seek to the type set that corresponds to the requested object.
2070 * The +1 is for the overall status element for the type.
2072 offset = sizeof(struct ses_control_page_hdr)
2073 + (iter.global_element_index * sizeof(struct ses_comstat));
2075 /* Check for buffer overflow. */
2076 if (offset + sizeof(struct ses_comstat) > amt)
2079 /* Set the status. */
2080 memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2082 ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2083 iter.type_index, iter.global_element_index, offset,
2084 req->elm_stat.comstatus, req->elm_stat.comstat[0],
2085 req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2091 ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2092 union ccb *ccb, uint8_t *buf)
2095 enc_cache_t *enc_cache;
2096 ses_cache_t *ses_cache;
2097 struct ses_control_page_hdr *hdr;
2098 ses_control_request_t *req;
2102 ses = enc->enc_private;
2103 enc_cache = &enc->enc_daemon_cache;
2104 ses_cache = enc_cache->private;
2105 hdr = (struct ses_control_page_hdr *)buf;
2107 if (ses_cache->status_page == NULL) {
2108 ses_terminate_control_requests(&ses->ses_requests, EIO);
2112 plength = ses_page_length(&ses_cache->status_page->hdr);
2113 memcpy(buf, ses_cache->status_page, plength);
2115 /* Disable the select bits in all status entries. */
2116 offset = sizeof(struct ses_control_page_hdr);
2117 for (offset = sizeof(struct ses_control_page_hdr);
2118 offset < plength; offset += sizeof(struct ses_comstat)) {
2119 buf[offset] &= ~SESCTL_CSEL;
2122 /* And make sure the INVOP bit is clear. */
2123 hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2125 /* Apply incoming requests. */
2126 while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2128 TAILQ_REMOVE(&ses->ses_requests, req, links);
2129 req->result = ses_encode(enc, buf, plength, req);
2130 if (req->result != 0) {
2134 TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2137 if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2140 /* Fill out the ccb */
2141 if (enc->enc_type == ENC_SEMB_SES) {
2142 semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2144 buf, ses_page_length(&ses_cache->status_page->hdr),
2147 scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2148 MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2149 /*device_offline*/0, /*self_test*/0,
2150 /*page_format*/1, /*self_test_code*/0,
2151 buf, ses_page_length(&ses_cache->status_page->hdr),
2152 SSD_FULL_SIZE, state->timeout);
2158 ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2159 uint8_t *buf, int bufsiz)
2161 ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2165 #define SES_PRINT_PORTS(p, type) do { \
2166 sbuf_printf(sbp, " %s(", type); \
2167 if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) == 0) \
2168 sbuf_printf(sbp, " None"); \
2170 if ((p) & SES_SASOBJ_DEV_PHY_SMP) \
2171 sbuf_printf(sbp, " SMP"); \
2172 if ((p) & SES_SASOBJ_DEV_PHY_STP) \
2173 sbuf_printf(sbp, " STP"); \
2174 if ((p) & SES_SASOBJ_DEV_PHY_SSP) \
2175 sbuf_printf(sbp, " SSP"); \
2177 sbuf_printf(sbp, " )"); \
2181 * \brief Print the additional element status data for this object, for SAS
2182 * type 0 objects. See SES2 r20 Section 6.1.13.3.2.
2184 * \param sesname SES device name associated with the object.
2185 * \param sbp Sbuf to print to.
2186 * \param obj The object to print the data for.
2187 * \param periph_name Peripheral string associated with the object.
2190 ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2191 enc_element_t *obj, char *periph_name)
2194 ses_element_t *elmpriv;
2195 struct ses_addl_status *addl;
2196 struct ses_elm_sas_device_phy *phy;
2198 elmpriv = obj->elm_private;
2199 addl = &(elmpriv->addl);
2200 if (addl->proto_hdr.sas == NULL)
2202 sbuf_printf(sbp, "%s: %s: SAS Device Slot Element:",
2203 sesname, periph_name);
2204 sbuf_printf(sbp, " %d Phys", addl->proto_hdr.sas->base_hdr.num_phys);
2205 if (ses_elm_addlstatus_eip(addl->hdr))
2206 sbuf_printf(sbp, " at Slot %d",
2207 addl->proto_hdr.sas->type0_eip.dev_slot_num);
2208 if (ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas))
2209 sbuf_printf(sbp, ", Not All Phys");
2210 sbuf_printf(sbp, "\n");
2211 if (addl->proto_data.sasdev_phys == NULL)
2213 for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2214 phy = &addl->proto_data.sasdev_phys[i];
2215 sbuf_printf(sbp, "%s: phy %d:", sesname, i);
2216 if (ses_elm_sas_dev_phy_sata_dev(phy))
2217 /* Spec says all other fields are specific values */
2218 sbuf_printf(sbp, " SATA device\n");
2220 sbuf_printf(sbp, " SAS device type %d id %d\n",
2221 ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2222 sbuf_printf(sbp, "%s: phy %d: protocols:", sesname, i);
2223 SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2224 SES_PRINT_PORTS(phy->target_ports, "Target");
2225 sbuf_printf(sbp, "\n");
2227 sbuf_printf(sbp, "%s: phy %d: parent %jx addr %jx\n",
2229 (uintmax_t)scsi_8btou64(phy->parent_addr),
2230 (uintmax_t)scsi_8btou64(phy->phy_addr));
2233 #undef SES_PRINT_PORTS
2236 * \brief Report whether a given enclosure object is an expander.
2238 * \param enc SES softc associated with object.
2239 * \param obj Enclosure object to report for.
2241 * \return 1 if true, 0 otherwise.
2244 ses_obj_is_expander(enc_softc_t *enc, enc_element_t *obj)
2246 return (obj->enctype == ELMTYP_SAS_EXP);
2250 * \brief Print the additional element status data for this object, for SAS
2251 * type 1 objects. See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2253 * \param enc SES enclosure, needed for type identification.
2254 * \param sesname SES device name associated with the object.
2255 * \param sbp Sbuf to print to.
2256 * \param obj The object to print the data for.
2257 * \param periph_name Peripheral string associated with the object.
2260 ses_print_addl_data_sas_type1(enc_softc_t *enc, char *sesname,
2261 struct sbuf *sbp, enc_element_t *obj, char *periph_name)
2264 ses_element_t *elmpriv;
2265 struct ses_addl_status *addl;
2266 struct ses_elm_sas_expander_phy *exp_phy;
2267 struct ses_elm_sas_port_phy *port_phy;
2269 elmpriv = obj->elm_private;
2270 addl = &(elmpriv->addl);
2271 if (addl->proto_hdr.sas == NULL)
2273 sbuf_printf(sbp, "%s: %s: SAS ", sesname, periph_name);
2274 if (ses_obj_is_expander(enc, obj)) {
2275 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2276 sbuf_printf(sbp, "Expander: %d Phys", num_phys);
2277 if (addl->proto_data.sasexp_phys == NULL)
2279 for (i = 0;i < num_phys;i++) {
2280 exp_phy = &addl->proto_data.sasexp_phys[i];
2281 sbuf_printf(sbp, "%s: phy %d: connector %d other %d\n",
2282 sesname, i, exp_phy->connector_index,
2283 exp_phy->other_index);
2286 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2287 sbuf_printf(sbp, "Port: %d Phys", num_phys);
2288 if (addl->proto_data.sasport_phys == NULL)
2290 for (i = 0;i < num_phys;i++) {
2291 port_phy = &addl->proto_data.sasport_phys[i];
2293 "%s: phy %d: id %d connector %d other %d\n",
2294 sesname, i, port_phy->phy_id,
2295 port_phy->connector_index, port_phy->other_index);
2296 sbuf_printf(sbp, "%s: phy %d: addr %jx\n", sesname, i,
2297 (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2303 * \brief Print the additional element status data for this object.
2305 * \param enc SES softc associated with the object.
2306 * \param obj The object to print the data for.
2309 ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2311 ses_element_t *elmpriv;
2312 struct ses_addl_status *addl;
2313 struct sbuf sesname, name, out;
2315 elmpriv = obj->elm_private;
2316 if (elmpriv == NULL)
2319 addl = &(elmpriv->addl);
2320 if (addl->hdr == NULL)
2323 sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2324 sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2325 sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2326 ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2327 if (sbuf_len(&name) == 0)
2328 sbuf_printf(&name, "(none)");
2330 sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2331 enc->periph->unit_number);
2332 sbuf_finish(&sesname);
2333 if (elmpriv->descr != NULL)
2334 sbuf_printf(&out, "%s: %s: Element descriptor: '%s'\n",
2335 sbuf_data(&sesname), sbuf_data(&name), elmpriv->descr);
2336 switch(ses_elm_addlstatus_proto(addl->hdr)) {
2337 case SPSP_PROTO_SAS:
2338 switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2339 case SES_SASOBJ_TYPE_SLOT:
2340 ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2341 &out, obj, sbuf_data(&name));
2343 case SES_SASOBJ_TYPE_OTHER:
2344 ses_print_addl_data_sas_type1(enc, sbuf_data(&sesname),
2345 &out, obj, sbuf_data(&name));
2351 case SPSP_PROTO_FC: /* stubbed for now */
2357 printf("%s", sbuf_data(&out));
2360 sbuf_delete(&sesname);
2364 * \brief Update the softc with the additional element status data for this
2365 * object, for SAS type 0 objects.
2367 * \param enc SES softc to be updated.
2368 * \param buf The additional element status response buffer.
2369 * \param bufsiz Size of the response buffer.
2370 * \param eip The EIP bit value.
2371 * \param nobj Number of objects attached to the SES softc.
2373 * \return 0 on success, errno otherwise.
2376 ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2377 uint8_t *buf, int bufsiz, int eip, int nobj)
2379 int err, offset, physz;
2381 ses_element_t *elmpriv;
2382 struct ses_addl_status *addl;
2386 /* basic object setup */
2387 obj = &(enc_cache->elm_map[nobj]);
2388 elmpriv = obj->elm_private;
2389 addl = &(elmpriv->addl);
2391 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2393 /* Don't assume this object has any phys */
2394 bzero(&addl->proto_data, sizeof(addl->proto_data));
2395 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2398 /* Skip forward to the phy list */
2400 offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2402 offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2404 /* Make sure the phy list fits in the buffer */
2405 physz = addl->proto_hdr.sas->base_hdr.num_phys;
2406 physz *= sizeof(struct ses_elm_sas_device_phy);
2407 if (physz > (bufsiz - offset + 4)) {
2408 ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2414 /* Point to the phy list */
2415 addl->proto_data.sasdev_phys =
2416 (struct ses_elm_sas_device_phy *)&buf[offset];
2423 * \brief Update the softc with the additional element status data for this
2424 * object, for SAS type 1 objects.
2426 * \param enc SES softc to be updated.
2427 * \param buf The additional element status response buffer.
2428 * \param bufsiz Size of the response buffer.
2429 * \param eip The EIP bit value.
2430 * \param nobj Number of objects attached to the SES softc.
2432 * \return 0 on success, errno otherwise.
2435 ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2436 uint8_t *buf, int bufsiz, int eip, int nobj)
2438 int err, offset, physz;
2440 ses_element_t *elmpriv;
2441 struct ses_addl_status *addl;
2445 /* basic object setup */
2446 obj = &(enc_cache->elm_map[nobj]);
2447 elmpriv = obj->elm_private;
2448 addl = &(elmpriv->addl);
2450 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2452 /* Don't assume this object has any phys */
2453 bzero(&addl->proto_data, sizeof(addl->proto_data));
2454 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2457 /* Process expanders differently from other type1 cases */
2458 if (ses_obj_is_expander(enc, obj)) {
2459 offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2460 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2461 sizeof(struct ses_elm_sas_expander_phy);
2462 if (physz > (bufsiz - offset)) {
2463 ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2464 "End Of Buffer\n", nobj);
2468 addl->proto_data.sasexp_phys =
2469 (struct ses_elm_sas_expander_phy *)&buf[offset];
2471 offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2472 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2473 sizeof(struct ses_elm_sas_port_phy);
2474 if (physz > (bufsiz - offset + 4)) {
2475 ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2476 "Of Buffer\n", nobj);
2480 addl->proto_data.sasport_phys =
2481 (struct ses_elm_sas_port_phy *)&buf[offset];
2489 * \brief Update the softc with the additional element status data for this
2490 * object, for SAS objects.
2492 * \param enc SES softc to be updated.
2493 * \param buf The additional element status response buffer.
2494 * \param bufsiz Size of the response buffer.
2495 * \param eip The EIP bit value.
2496 * \param tidx Type index for this object.
2497 * \param nobj Number of objects attached to the SES softc.
2499 * \return 0 on success, errno otherwise.
2502 ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2503 uint8_t *buf, int bufsiz, int eip, int tidx,
2507 ses_cache_t *ses_cache;
2508 union ses_elm_sas_hdr *hdr;
2510 /* Need to be able to read the descriptor type! */
2511 if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2516 ses_cache = enc_cache->private;
2518 hdr = (union ses_elm_sas_hdr *)buf;
2519 dtype = ses_elm_sas_descr_type(hdr);
2521 case SES_SASOBJ_TYPE_SLOT:
2522 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2524 case ELMTYP_ARRAY_DEV:
2527 ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2528 "invalid for SES element type 0x%x\n", nobj,
2529 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2533 err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2537 case SES_SASOBJ_TYPE_OTHER:
2538 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2539 case ELMTYP_SAS_EXP:
2540 case ELMTYP_SCSI_INI:
2541 case ELMTYP_SCSI_TGT:
2545 ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2546 "invalid for SES element type 0x%x\n", nobj,
2547 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2551 err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2555 ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2556 "of unknown type 0x%x\n", nobj,
2557 ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2567 ses_softc_invalidate(enc_softc_t *enc)
2571 ses = enc->enc_private;
2572 ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2576 ses_softc_cleanup(enc_softc_t *enc)
2579 ses_cache_free(enc, &enc->enc_cache);
2580 ses_cache_free(enc, &enc->enc_daemon_cache);
2581 ENC_FREE_AND_NULL(enc->enc_private);
2582 ENC_FREE_AND_NULL(enc->enc_cache.private);
2583 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2587 ses_init_enc(enc_softc_t *enc)
2593 ses_get_enc_status(enc_softc_t *enc, int slpflag)
2595 /* Automatically updated, caller checks enc_cache->encstat itself */
2600 ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2602 ses_control_request_t req;
2605 ses = enc->enc_private;
2606 req.elm_idx = SES_SETSTATUS_ENC_IDX;
2607 req.elm_stat.comstatus = encstat & 0xf;
2609 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2610 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2611 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2613 return (req.result);
2617 ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2619 unsigned int i = elms->elm_idx;
2621 memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2626 ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2628 ses_control_request_t req;
2631 /* If this is clear, we don't do diddly. */
2632 if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2635 ses = enc->enc_private;
2636 req.elm_idx = elms->elm_idx;
2637 memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2639 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2640 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2641 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2643 return (req.result);
2647 ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2649 int i = (int)elmd->elm_idx;
2650 ses_element_t *elmpriv;
2652 /* Assume caller has already checked obj_id validity */
2653 elmpriv = enc->enc_cache.elm_map[i].elm_private;
2654 /* object might not have a descriptor */
2655 if (elmpriv == NULL || elmpriv->descr == NULL) {
2656 elmd->elm_desc_len = 0;
2659 if (elmd->elm_desc_len > elmpriv->descr_len)
2660 elmd->elm_desc_len = elmpriv->descr_len;
2661 copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2666 * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2667 * given object id if one is available.
2669 * \param enc SES softc to examine.
2670 * \param objdn ioctl structure to read/write device name info.
2672 * \return 0 on success, errno otherwise.
2675 ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2680 len = elmdn->elm_names_size;
2684 cam_periph_unlock(enc->periph);
2685 sbuf_new(&sb, NULL, len, SBUF_FIXEDLEN);
2686 ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2687 ses_elmdevname_callback, &sb);
2689 elmdn->elm_names_len = sbuf_len(&sb);
2690 copyout(sbuf_data(&sb), elmdn->elm_devnames, elmdn->elm_names_len + 1);
2691 cam_periph_lock(enc->periph);
2692 return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2696 * \brief Send a string to the primary subenclosure using the String Out
2697 * SES diagnostic page.
2699 * \param enc SES enclosure to run the command on.
2700 * \param sstr SES string structure to operate on
2701 * \param ioc Ioctl being performed
2703 * \return 0 on success, errno otherwise.
2706 ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2709 enc_cache_t *enc_cache;
2710 ses_cache_t *ses_cache;
2711 const struct ses_enc_desc *enc_desc;
2712 int amt, payload, ret;
2721 ses = enc->enc_private;
2722 enc_cache = &enc->enc_daemon_cache;
2723 ses_cache = enc_cache->private;
2725 /* Implement SES2r20 6.1.6 */
2726 if (sstr->bufsiz > 0xffff)
2727 return (EINVAL); /* buffer size too large */
2729 if (ioc == ENCIOC_SETSTRING) {
2730 payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2732 buf = ENC_MALLOC(payload);
2736 ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2737 /* Construct the page request */
2738 buf[0] = SesStringOut;
2740 buf[2] = sstr->bufsiz >> 8;
2741 buf[3] = sstr->bufsiz & 0xff;
2742 memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2743 } else if (ioc == ENCIOC_GETSTRING) {
2744 payload = sstr->bufsiz;
2746 ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2748 } else if (ioc == ENCIOC_GETENCNAME) {
2749 if (ses_cache->ses_nsubencs < 1)
2751 enc_desc = ses_cache->subencs[0];
2752 cam_strvis(vendor, enc_desc->vendor_id,
2753 sizeof(enc_desc->vendor_id), sizeof(vendor));
2754 cam_strvis(product, enc_desc->product_id,
2755 sizeof(enc_desc->product_id), sizeof(product));
2756 cam_strvis(rev, enc_desc->product_rev,
2757 sizeof(enc_desc->product_rev), sizeof(rev));
2758 rsize = snprintf(str, sizeof(str), "%s %s %s",
2759 vendor, product, rev) + 1;
2760 if (rsize > sizeof(str))
2761 rsize = sizeof(str);
2762 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2764 if (size > sstr->bufsiz)
2765 size = sstr->bufsiz;
2766 copyout(str, sstr->buf, size);
2767 return (size == rsize ? 0 : ENOMEM);
2768 } else if (ioc == ENCIOC_GETENCID) {
2769 if (ses_cache->ses_nsubencs < 1)
2771 enc_desc = ses_cache->subencs[0];
2772 rsize = snprintf(str, sizeof(str), "%16jx",
2773 scsi_8btou64(enc_desc->logical_id)) + 1;
2774 if (rsize > sizeof(str))
2775 rsize = sizeof(str);
2776 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2778 if (size > sstr->bufsiz)
2779 size = sstr->bufsiz;
2780 copyout(str, sstr->buf, size);
2781 return (size == rsize ? 0 : ENOMEM);
2785 ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2786 if (ioc == ENCIOC_SETSTRING)
2792 * \invariant Called with cam_periph mutex held.
2795 ses_poll_status(enc_softc_t *enc)
2799 ses = enc->enc_private;
2800 enc_update_request(enc, SES_UPDATE_GETSTATUS);
2801 if (ses->ses_flags & SES_FLAG_DESC)
2802 enc_update_request(enc, SES_UPDATE_GETELMDESCS);
2803 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2804 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2808 * \brief Notification received when CAM detects a new device in the
2809 * SCSI domain in which this SEP resides.
2811 * \param enc SES enclosure instance.
2814 ses_device_found(enc_softc_t *enc)
2816 ses_poll_status(enc);
2817 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2820 static struct enc_vec ses_enc_vec =
2822 .softc_invalidate = ses_softc_invalidate,
2823 .softc_cleanup = ses_softc_cleanup,
2824 .init_enc = ses_init_enc,
2825 .get_enc_status = ses_get_enc_status,
2826 .set_enc_status = ses_set_enc_status,
2827 .get_elm_status = ses_get_elm_status,
2828 .set_elm_status = ses_set_elm_status,
2829 .get_elm_desc = ses_get_elm_desc,
2830 .get_elm_devnames = ses_get_elm_devnames,
2831 .handle_string = ses_handle_string,
2832 .device_found = ses_device_found,
2833 .poll_status = ses_poll_status
2837 * \brief Initialize a new SES instance.
2839 * \param enc SES softc structure to set up the instance in.
2840 * \param doinit Do the initialization (see main driver).
2842 * \return 0 on success, errno otherwise.
2845 ses_softc_init(enc_softc_t *enc)
2847 ses_softc_t *ses_softc;
2849 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2850 ("entering enc_softc_init(%p)\n", enc));
2852 enc->enc_vec = ses_enc_vec;
2853 enc->enc_fsm_states = enc_fsm_states;
2855 if (enc->enc_private == NULL)
2856 enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2857 if (enc->enc_cache.private == NULL)
2858 enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2859 if (enc->enc_daemon_cache.private == NULL)
2860 enc->enc_daemon_cache.private =
2861 ENC_MALLOCZ(sizeof(ses_cache_t));
2863 if (enc->enc_private == NULL
2864 || enc->enc_cache.private == NULL
2865 || enc->enc_daemon_cache.private == NULL) {
2866 ENC_FREE_AND_NULL(enc->enc_private);
2867 ENC_FREE_AND_NULL(enc->enc_cache.private);
2868 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2872 ses_softc = enc->enc_private;
2873 TAILQ_INIT(&ses_softc->ses_requests);
2874 TAILQ_INIT(&ses_softc->ses_pending_requests);
2876 enc_update_request(enc, SES_UPDATE_PAGES);
2878 // XXX: Move this to the FSM so it doesn't hang init
2879 if (0) (void) ses_set_timed_completion(enc, 1);