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_print_addl_data(enc_softc_t *, enc_element_t *);
555 /*=========================== SES cleanup routines ===========================*/
558 ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
560 ses_cache_t *ses_cache;
561 ses_cache_t *other_ses_cache;
562 enc_element_t *cur_elm;
563 enc_element_t *last_elm;
565 ENC_DLOG(enc, "%s: enter\n", __func__);
566 ses_cache = cache->private;
567 if (ses_cache->elm_addlstatus_page == NULL)
570 for (cur_elm = cache->elm_map,
571 last_elm = &cache->elm_map[cache->nelms];
572 cur_elm != last_elm; cur_elm++) {
573 ses_element_t *elmpriv;
575 elmpriv = cur_elm->elm_private;
577 /* Clear references to the additional status page. */
578 bzero(&elmpriv->addl, sizeof(elmpriv->addl));
581 other_ses_cache = enc_other_cache(enc, cache)->private;
582 if (other_ses_cache->elm_addlstatus_page
583 != ses_cache->elm_addlstatus_page)
584 ENC_FREE(ses_cache->elm_addlstatus_page);
585 ses_cache->elm_addlstatus_page = NULL;
589 ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
591 ses_cache_t *ses_cache;
592 ses_cache_t *other_ses_cache;
593 enc_element_t *cur_elm;
594 enc_element_t *last_elm;
596 ENC_DLOG(enc, "%s: enter\n", __func__);
597 ses_cache = cache->private;
598 if (ses_cache->elm_descs_page == NULL)
601 for (cur_elm = cache->elm_map,
602 last_elm = &cache->elm_map[cache->nelms];
603 cur_elm != last_elm; cur_elm++) {
604 ses_element_t *elmpriv;
606 elmpriv = cur_elm->elm_private;
607 elmpriv->descr_len = 0;
608 elmpriv->descr = NULL;
611 other_ses_cache = enc_other_cache(enc, cache)->private;
612 if (other_ses_cache->elm_descs_page
613 != ses_cache->elm_descs_page)
614 ENC_FREE(ses_cache->elm_descs_page);
615 ses_cache->elm_descs_page = NULL;
619 ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
621 ses_cache_t *ses_cache;
622 ses_cache_t *other_ses_cache;
624 ENC_DLOG(enc, "%s: enter\n", __func__);
625 ses_cache = cache->private;
626 if (ses_cache->status_page == NULL)
629 other_ses_cache = enc_other_cache(enc, cache)->private;
630 if (other_ses_cache->status_page != ses_cache->status_page)
631 ENC_FREE(ses_cache->status_page);
632 ses_cache->status_page = NULL;
636 ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
638 enc_element_t *cur_elm;
639 enc_element_t *last_elm;
641 ENC_DLOG(enc, "%s: enter\n", __func__);
642 if (cache->elm_map == NULL)
645 ses_cache_free_elm_descs(enc, cache);
646 ses_cache_free_elm_addlstatus(enc, cache);
647 for (cur_elm = cache->elm_map,
648 last_elm = &cache->elm_map[cache->nelms];
649 cur_elm != last_elm; cur_elm++) {
651 ENC_FREE_AND_NULL(cur_elm->elm_private);
653 ENC_FREE_AND_NULL(cache->elm_map);
655 ENC_DLOG(enc, "%s: exit\n", __func__);
659 ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
661 ses_cache_t *other_ses_cache;
662 ses_cache_t *ses_cache;
664 ENC_DLOG(enc, "%s: enter\n", __func__);
665 ses_cache_free_elm_addlstatus(enc, cache);
666 ses_cache_free_status(enc, cache);
667 ses_cache_free_elm_map(enc, cache);
669 ses_cache = cache->private;
670 ses_cache->ses_ntypes = 0;
672 other_ses_cache = enc_other_cache(enc, cache)->private;
673 if (other_ses_cache->subencs != ses_cache->subencs)
674 ENC_FREE(ses_cache->subencs);
675 ses_cache->subencs = NULL;
677 if (other_ses_cache->ses_types != ses_cache->ses_types)
678 ENC_FREE(ses_cache->ses_types);
679 ses_cache->ses_types = NULL;
681 if (other_ses_cache->cfg_page != ses_cache->cfg_page)
682 ENC_FREE(ses_cache->cfg_page);
683 ses_cache->cfg_page = NULL;
685 ENC_DLOG(enc, "%s: exit\n", __func__);
689 ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
691 ses_cache_t *dst_ses_cache;
692 ses_cache_t *src_ses_cache;
693 enc_element_t *src_elm;
694 enc_element_t *dst_elm;
695 enc_element_t *last_elm;
697 ses_cache_free(enc, dst);
698 src_ses_cache = src->private;
699 dst_ses_cache = dst->private;
702 * The cloned enclosure cache and ses specific cache are
703 * mostly identical to the source.
706 *dst_ses_cache = *src_ses_cache;
709 * But the ses cache storage is still independent. Restore
710 * the pointer that was clobbered by the structure copy above.
712 dst->private = dst_ses_cache;
715 * The element map is independent even though it starts out
716 * pointing to the same constant page data.
718 dst->elm_map = ENC_MALLOCZ(dst->nelms * sizeof(enc_element_t));
719 memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
720 for (dst_elm = dst->elm_map, src_elm = src->elm_map,
721 last_elm = &src->elm_map[src->nelms];
722 src_elm != last_elm; src_elm++, dst_elm++) {
724 dst_elm->elm_private = ENC_MALLOCZ(sizeof(ses_element_t));
725 memcpy(dst_elm->elm_private, src_elm->elm_private,
726 sizeof(ses_element_t));
730 /* Structure accessors. These are strongly typed to avoid errors. */
733 ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
735 return ((obj)->base_hdr.byte1 >> 6);
738 ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
740 return ((hdr)->byte0 & 0xf);
743 ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
745 return ((hdr)->byte0 >> 4) & 0x1;
748 ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
750 return ((hdr)->byte0 >> 7);
753 ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
755 return ((hdr)->type0_noneip.byte1 & 0x1);
758 ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
760 return ((phy)->target_ports & 0x1);
763 ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
765 return ((phy)->target_ports >> 7);
768 ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
770 return (((phy)->byte0 >> 4) & 0x7);
774 * \brief Verify that the cached configuration data in our softc
775 * is valid for processing the page data corresponding to
776 * the provided page header.
778 * \param ses_cache The SES cache to validate.
779 * \param gen_code The 4 byte generation code from a SES diagnostic
782 * \return non-zero if true, 0 if false.
785 ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
790 if (ses_cache->cfg_page == NULL)
793 cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
794 cur_gc = scsi_4btoul(gen_code);
795 return (cache_gc == cur_gc);
799 * Function signature for consumers of the ses_devids_iter() interface.
801 typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
802 struct scsi_vpd_id_descriptor *, void *);
805 * \brief Iterate over and create vpd device id records from the
806 * additional element status data for elm, passing that data
807 * to the provided callback.
809 * \param enc SES instance containing elm
810 * \param elm Element for which to extract device ID data.
811 * \param callback The callback function to invoke on each generated
812 * device id descriptor for elm.
813 * \param callback_arg Argument passed through to callback on each invocation.
816 ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
817 ses_devid_callback_t *callback, void *callback_arg)
819 ses_element_t *elmpriv;
820 struct ses_addl_status *addl;
822 size_t devid_record_size;
824 elmpriv = elm->elm_private;
825 addl = &(elmpriv->addl);
828 * Don't assume this object has additional status information, or
829 * that it is a SAS device, or that it is a device slot device.
831 if (addl->hdr == NULL || addl->proto_hdr.sas == NULL
832 || addl->proto_data.sasdev_phys == NULL)
835 devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
836 + sizeof(struct scsi_vpd_id_naa_ieee_reg);
837 for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
838 uint8_t devid_buf[devid_record_size];
839 struct scsi_vpd_id_descriptor *devid;
842 devid = (struct scsi_vpd_id_descriptor *)devid_buf;
843 phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
844 devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
845 | SVPD_ID_CODESET_BINARY;
846 devid->id_type = SVPD_ID_PIV
850 devid->length = sizeof(struct scsi_vpd_id_naa_ieee_reg);
851 memcpy(devid->identifier, phy_addr, devid->length);
853 callback(enc, elm, devid, callback_arg);
858 * Function signature for consumers of the ses_paths_iter() interface.
860 typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
861 struct cam_path *, void *);
864 * Argument package passed through ses_devids_iter() by
865 * ses_paths_iter() to ses_path_iter_devid_callback().
867 typedef struct ses_path_iter_args {
868 ses_path_callback_t *callback;
870 } ses_path_iter_args_t;
873 * ses_devids_iter() callback function used by ses_paths_iter()
874 * to map device ids to peripheral driver instances.
876 * \param enc SES instance containing elm
877 * \param elm Element on which device ID matching is active.
878 * \param periph A device ID corresponding to elm.
879 * \param arg Argument passed through to callback on each invocation.
882 ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
883 struct scsi_vpd_id_descriptor *devid,
886 struct ccb_dev_match cdm;
887 struct dev_match_pattern match_pattern;
888 struct dev_match_result match_result;
889 struct device_match_result *device_match;
890 struct device_match_pattern *device_pattern;
891 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(&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 xpt_action((union ccb *)&cdm);
918 xpt_free_path(cdm.ccb_h.path);
920 if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
921 || (cdm.status != CAM_DEV_MATCH_LAST
922 && cdm.status != CAM_DEV_MATCH_MORE)
923 || cdm.num_matches == 0)
926 device_match = &match_result.result.device_result;
927 if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
928 device_match->path_id,
929 device_match->target_id,
930 device_match->target_lun) != CAM_REQ_CMP)
933 args->callback(enc, elem, cdm.ccb_h.path, args->callback_arg);
935 xpt_free_path(cdm.ccb_h.path);
939 * \brief Iterate over and find the matching periph objects for the
942 * \param enc SES instance containing elm
943 * \param elm Element for which to perform periph object matching.
944 * \param callback The callback function to invoke with each matching
946 * \param callback_arg Argument passed through to callback on each invocation.
949 ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
950 ses_path_callback_t *callback, void *callback_arg)
952 ses_path_iter_args_t args;
954 args.callback = callback;
955 args.callback_arg = callback_arg;
956 ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
960 * ses_paths_iter() callback function used by ses_get_elmdevname()
961 * to record periph driver instance strings corresponding to a SES
964 * \param enc SES instance containing elm
965 * \param elm Element on which periph matching is active.
966 * \param periph A periph instance that matches elm.
967 * \param arg Argument passed through to callback on each invocation.
970 ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
971 struct cam_path *path, void *arg)
975 sb = (struct sbuf *)arg;
976 cam_periph_list(path, sb);
980 * Argument package passed through ses_paths_iter() to
981 * ses_getcampath_callback.
983 typedef struct ses_setphyspath_callback_args {
984 struct sbuf *physpath;
986 } ses_setphyspath_callback_args_t;
989 * \brief ses_paths_iter() callback to set the physical path on the
990 * CAM EDT entries corresponding to a given SES element.
992 * \param enc SES instance containing elm
993 * \param elm Element on which periph matching is active.
994 * \param periph A periph instance that matches elm.
995 * \param arg Argument passed through to callback on each invocation.
998 ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
999 struct cam_path *path, void *arg)
1001 struct ccb_dev_advinfo cdai;
1002 ses_setphyspath_callback_args_t *args;
1005 args = (ses_setphyspath_callback_args_t *)arg;
1006 old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1007 cam_periph_lock(enc->periph);
1008 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1009 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1010 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1011 cdai.flags = CDAI_FLAG_NONE;
1012 cdai.bufsiz = MAXPATHLEN;
1013 cdai.buf = old_physpath;
1014 xpt_action((union ccb *)&cdai);
1015 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1016 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1018 if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1020 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1021 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1022 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1023 cdai.flags = CDAI_FLAG_STORE;
1024 cdai.bufsiz = sbuf_len(args->physpath);
1025 cdai.buf = sbuf_data(args->physpath);
1026 xpt_action((union ccb *)&cdai);
1027 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1028 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1029 if (cdai.ccb_h.status == CAM_REQ_CMP)
1032 cam_periph_unlock(enc->periph);
1033 free(old_physpath, M_SCSIENC);
1037 * \brief Set a device's physical path string in CAM XPT.
1039 * \param enc SES instance containing elm
1040 * \param elm Element to publish physical path string for
1041 * \param iter Iterator whose state corresponds to elm
1043 * \return 0 on success, errno otherwise.
1046 ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1047 struct ses_iterator *iter)
1049 struct ccb_dev_advinfo cdai;
1050 ses_setphyspath_callback_args_t args;
1053 struct scsi_vpd_id_descriptor *idd;
1055 ses_element_t *elmpriv;
1062 * Assemble the components of the physical path starting with
1063 * the device ID of the enclosure itself.
1065 xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1066 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1067 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1068 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1069 cdai.buf = devid = ENC_MALLOCZ(cdai.bufsiz);
1070 if (devid == NULL) {
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)
1168 struct cam_periph *periph;
1169 struct ses_mgmt_mode_page *mgmt;
1171 size_t mode_buf_len;
1174 periph = enc->periph;
1175 ses = enc->enc_private;
1176 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1178 mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1179 mode_buf = ENC_MALLOCZ(mode_buf_len);
1180 if (mode_buf == NULL)
1183 scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1184 /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1185 mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1188 * Ignore illegal request errors, as they are quite common and we
1189 * will print something out in that case anyway.
1191 err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1192 ENC_FLAGS|SF_QUIET_IR, NULL);
1193 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1194 ENC_VLOG(enc, "Timed Completion Unsupported\n");
1198 /* Skip the mode select if the desired value is already set */
1199 mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1200 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1203 /* Value is not what we wanted, set it */
1205 mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1207 mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1208 /* SES2r20: a completion time of zero means as long as possible */
1209 bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1211 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1212 /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1213 SSD_FULL_SIZE, /*timeout*/60 * 1000);
1215 err = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1216 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1217 ENC_VLOG(enc, "Timed Completion Set Failed\n");
1222 if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1223 ENC_LOG(enc, "Timed Completion Enabled\n");
1224 ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1226 ENC_LOG(enc, "Timed Completion Disabled\n");
1227 ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1231 xpt_release_ccb(ccb);
1233 return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1237 * \brief Process the list of supported pages and update flags.
1239 * \param enc SES device to query.
1240 * \param buf Buffer containing the config page.
1241 * \param xfer_len Length of the config page in the buffer.
1243 * \return 0 on success, errno otherwise.
1246 ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1247 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1250 struct scsi_diag_page *page;
1253 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1254 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1255 ses = enc->enc_private;
1262 if (xfer_len < sizeof(*page)) {
1263 ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1267 page = (struct scsi_diag_page *)*bufp;
1268 length = scsi_2btoul(page->length);
1269 if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1270 ENC_VLOG(enc, "Diag Pages List Too Long\n");
1273 ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1274 __func__, length, xfer_len);
1277 for (i = 0; i < length; i++) {
1278 if (page->params[i] == SesElementDescriptor)
1279 ses->ses_flags |= SES_FLAG_DESC;
1280 else if (page->params[i] == SesAddlElementStatus)
1281 ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1285 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1290 * \brief Process the config page and update associated structures.
1292 * \param enc SES device to query.
1293 * \param buf Buffer containing the config page.
1294 * \param xfer_len Length of the config page in the buffer.
1296 * \return 0 on success, errno otherwise.
1299 ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1300 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1302 struct ses_iterator iter;
1304 enc_cache_t *enc_cache;
1305 ses_cache_t *ses_cache;
1311 struct ses_cfg_page *cfg_page;
1312 struct ses_enc_desc *buf_subenc;
1313 const struct ses_enc_desc **subencs;
1314 const struct ses_enc_desc **cur_subenc;
1315 const struct ses_enc_desc **last_subenc;
1316 ses_type_t *ses_types;
1317 ses_type_t *sestype;
1318 const struct ses_elm_type_desc *cur_buf_type;
1319 const struct ses_elm_type_desc *last_buf_type;
1320 uint8_t *last_valid_byte;
1321 enc_element_t *element;
1322 const char *type_text;
1324 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1325 ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1326 ses = enc->enc_private;
1327 enc_cache = &enc->enc_daemon_cache;
1328 ses_cache = enc_cache->private;
1336 if (xfer_len < sizeof(cfg_page->hdr)) {
1337 ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1342 cfg_page = (struct ses_cfg_page *)buf;
1343 length = ses_page_length(&cfg_page->hdr);
1344 if (length > xfer_len) {
1345 ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1348 last_valid_byte = &buf[length - 1];
1350 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1351 __func__, length, xfer_len);
1354 if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1356 /* Our cache is still valid. Proceed to fetching status. */
1360 /* Cache is no longer valid. Free old data to make way for new. */
1361 ses_cache_free(enc, enc_cache);
1362 ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1363 scsi_4btoul(cfg_page->hdr.gen_code),
1364 ses_cfg_page_get_num_subenc(cfg_page));
1366 /* Take ownership of the buffer. */
1367 ses_cache->cfg_page = cfg_page;
1371 * Now waltz through all the subenclosures summing the number of
1372 * types available in each.
1374 subencs = ENC_MALLOCZ(ses_cfg_page_get_num_subenc(cfg_page)
1375 * sizeof(*subencs));
1376 if (subencs == NULL) {
1381 * Sub-enclosure data is const after construction (i.e. when
1382 * accessed via our cache object.
1384 * The cast here is not required in C++ but C99 is not so
1385 * sophisticated (see C99 6.5.16.1(1)).
1387 ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1388 ses_cache->subencs = subencs;
1390 buf_subenc = cfg_page->subencs;
1391 cur_subenc = subencs;
1392 last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1394 while (cur_subenc <= last_subenc) {
1396 if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1397 ENC_VLOG(enc, "Enclosure %d Beyond End of "
1398 "Descriptors\n", cur_subenc - subencs);
1403 ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1404 "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1405 buf_subenc->num_types, buf_subenc->length,
1406 &buf_subenc->byte0 - buf);
1407 ENC_VLOG(enc, "WWN: %jx\n",
1408 (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1410 ntype += buf_subenc->num_types;
1411 *cur_subenc = buf_subenc;
1413 buf_subenc = ses_enc_desc_next(buf_subenc);
1416 /* Process the type headers. */
1417 ses_types = ENC_MALLOCZ(ntype * sizeof(*ses_types));
1418 if (ses_types == NULL) {
1423 * Type data is const after construction (i.e. when accessed via
1426 ses_cache->ses_ntypes = ntype;
1427 ses_cache->ses_types = ses_types;
1429 cur_buf_type = (const struct ses_elm_type_desc *)
1430 (&(*last_subenc)->length + (*last_subenc)->length + 1);
1431 last_buf_type = cur_buf_type + ntype - 1;
1432 type_text = (const uint8_t *)(last_buf_type + 1);
1434 sestype = ses_types;
1435 while (cur_buf_type <= last_buf_type) {
1436 if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1437 ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1438 sestype - ses_types);
1442 sestype->hdr = cur_buf_type;
1443 sestype->text = type_text;
1444 type_text += cur_buf_type->etype_txt_len;
1445 ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1446 "%d, Text Length %d: %.*s\n", sestype - ses_types,
1447 sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1448 sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1449 sestype->hdr->etype_txt_len, sestype->text);
1451 nelm += sestype->hdr->etype_maxelt
1452 + /*overall status element*/1;
1457 /* Create the object map. */
1458 enc_cache->elm_map = ENC_MALLOCZ(nelm * sizeof(enc_element_t));
1459 if (enc_cache->elm_map == NULL) {
1463 enc_cache->nelms = nelm;
1465 ses_iter_init(enc, enc_cache, &iter);
1466 while ((element = ses_iter_next(&iter)) != NULL) {
1467 const struct ses_elm_type_desc *thdr;
1469 ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1470 iter.global_element_index, iter.type_index, nelm,
1471 iter.type_element_index);
1472 thdr = ses_cache->ses_types[iter.type_index].hdr;
1473 element->subenclosure = thdr->etype_subenc;
1474 element->enctype = thdr->etype_elm_type;
1475 element->overall_status_elem = iter.type_element_index == 0;
1476 element->elm_private = ENC_MALLOCZ(sizeof(ses_element_t));
1477 if (element->elm_private == NULL) {
1481 ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1482 "type 0x%x\n", __func__, iter.global_element_index,
1483 iter.type_index, iter.type_element_index,
1484 thdr->etype_subenc, thdr->etype_elm_type);
1491 ses_cache_free(enc, enc_cache);
1493 enc_update_request(enc, SES_UPDATE_GETSTATUS);
1494 if (ses->ses_flags & SES_FLAG_DESC)
1495 enc_update_request(enc, SES_UPDATE_GETELMDESCS);
1496 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
1497 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
1498 enc_update_request(enc, SES_PUBLISH_CACHE);
1500 ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1505 * \brief Update the status page and associated structures.
1507 * \param enc SES softc to update for.
1508 * \param buf Buffer containing the status page.
1509 * \param bufsz Amount of data in the buffer.
1511 * \return 0 on success, errno otherwise.
1514 ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1515 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1517 struct ses_iterator iter;
1518 enc_element_t *element;
1520 enc_cache_t *enc_cache;
1521 ses_cache_t *ses_cache;
1525 struct ses_status_page *page;
1526 union ses_status_element *cur_stat;
1527 union ses_status_element *last_stat;
1529 ses = enc->enc_private;
1530 enc_cache = &enc->enc_daemon_cache;
1531 ses_cache = enc_cache->private;
1534 ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1535 page = (struct ses_status_page *)buf;
1536 length = ses_page_length(&page->hdr);
1543 * Make sure the length fits in the buffer.
1545 * XXX all this means is that the page is larger than the space
1546 * we allocated. Since we use a statically sized buffer, this
1547 * could happen... Need to use dynamic discovery of the size.
1549 if (length > xfer_len) {
1550 ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1554 /* Check for simple enclosure reporting short enclosure status. */
1555 if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1556 ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1557 ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1558 ses_cache_free(enc, enc_cache);
1559 enc_cache->enc_status = page->hdr.page_specific_flags;
1560 enc_update_request(enc, SES_PUBLISH_CACHE);
1565 /* Make sure the length contains at least one header and status */
1566 if (length < (sizeof(*page) + sizeof(*page->elements))) {
1567 ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1571 if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1572 ENC_DLOG(enc, "%s: Generation count change detected\n",
1574 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1578 ses_cache_free_status(enc, enc_cache);
1579 ses_cache->status_page = page;
1582 enc_cache->enc_status = page->hdr.page_specific_flags;
1585 * Read in individual element status. The element order
1586 * matches the order reported in the config page (i.e. the
1587 * order of an unfiltered iteration of the config objects)..
1589 ses_iter_init(enc, enc_cache, &iter);
1590 cur_stat = page->elements;
1591 last_stat = (union ses_status_element *)
1592 &buf[length - sizeof(*last_stat)];
1593 ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1594 __func__, length, xfer_len);
1595 while (cur_stat <= last_stat
1596 && (element = ses_iter_next(&iter)) != NULL) {
1598 ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1599 __func__, iter.global_element_index, iter.type_index,
1600 iter.type_element_index, (uint8_t *)cur_stat - buf,
1601 scsi_4btoul(cur_stat->bytes));
1603 memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1604 element->svalid = 1;
1608 if (ses_iter_next(&iter) != NULL) {
1609 ENC_VLOG(enc, "Status page, length insufficient for "
1610 "expected number of objects\n");
1612 if (cur_stat <= last_stat)
1613 ENC_VLOG(enc, "Status page, exhausted objects before "
1614 "exhausing page\n");
1615 enc_update_request(enc, SES_PUBLISH_CACHE);
1619 ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1625 * The enclosure should not provide additional element
1626 * status for this element type in page 0x0A.
1628 * \note This status is returned for any types not
1629 * listed SES3r02. Further types added in a
1630 * future specification will be incorrectly
1633 TYPE_ADDLSTATUS_NONE,
1636 * The element type provides additional element status
1639 TYPE_ADDLSTATUS_MANDATORY,
1642 * The element type may provide additional element status
1643 * in page 0x0A, but i
1645 TYPE_ADDLSTATUS_OPTIONAL
1646 } ses_addlstatus_avail_t;
1649 * \brief Check to see whether a given type (as obtained via type headers) is
1650 * supported by the additional status command.
1652 * \param enc SES softc to check.
1653 * \param typidx Type index to check for.
1655 * \return An enumeration indicating if additional status is mandatory,
1656 * optional, or not required for this type.
1658 static ses_addlstatus_avail_t
1659 ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1661 enc_cache_t *enc_cache;
1662 ses_cache_t *ses_cache;
1664 enc_cache = &enc->enc_daemon_cache;
1665 ses_cache = enc_cache->private;
1666 switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1668 case ELMTYP_ARRAY_DEV:
1669 case ELMTYP_SAS_EXP:
1670 return (TYPE_ADDLSTATUS_MANDATORY);
1671 case ELMTYP_SCSI_INI:
1672 case ELMTYP_SCSI_TGT:
1674 return (TYPE_ADDLSTATUS_OPTIONAL);
1676 /* No additional status information available. */
1679 return (TYPE_ADDLSTATUS_NONE);
1682 static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1684 static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1685 int, int, int, int);
1688 * \brief Parse the additional status element data for each object.
1690 * \param enc The SES softc to update.
1691 * \param buf The buffer containing the additional status
1693 * \param xfer_len Size of the buffer.
1695 * \return 0 on success, errno otherwise.
1698 ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1699 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1701 struct ses_iterator iter, titer;
1704 int ignore_index = 0;
1707 enc_cache_t *enc_cache;
1708 ses_cache_t *ses_cache;
1710 ses_element_t *elmpriv;
1711 const struct ses_page_hdr *hdr;
1712 enc_element_t *element, *telement;
1714 enc_cache = &enc->enc_daemon_cache;
1715 ses_cache = enc_cache->private;
1723 ses_cache_free_elm_addlstatus(enc, enc_cache);
1724 ses_cache->elm_addlstatus_page =
1725 (struct ses_addl_elem_status_page *)buf;
1729 * The objects appear in the same order here as in Enclosure Status,
1730 * which itself is ordered by the Type Descriptors from the Config
1731 * page. However, it is necessary to skip elements that are not
1732 * supported by this page when counting them.
1734 hdr = &ses_cache->elm_addlstatus_page->hdr;
1735 length = ses_page_length(hdr);
1736 ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1737 /* Make sure the length includes at least one header. */
1738 if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1739 ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1742 if (length > xfer_len) {
1743 ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1747 if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1748 ENC_DLOG(enc, "%s: Generation count change detected\n",
1750 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1754 offset = sizeof(struct ses_page_hdr);
1755 ses_iter_init(enc, enc_cache, &iter);
1756 while (offset < length
1757 && (element = ses_iter_next(&iter)) != NULL) {
1758 struct ses_elm_addlstatus_base_hdr *elm_hdr;
1760 ses_addlstatus_avail_t status_type;
1763 * Additional element status is only provided for
1764 * individual elements (i.e. overal status elements
1765 * are excluded) and those of the types specified
1768 status_type = ses_typehasaddlstatus(enc, iter.type_index);
1769 if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1770 || status_type == TYPE_ADDLSTATUS_NONE)
1773 elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1774 eip = ses_elm_addlstatus_eip(elm_hdr);
1775 if (eip && !ignore_index) {
1776 struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1779 eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1780 expected_index = iter.individual_element_index;
1782 telement = ses_iter_seek_to(&titer,
1783 eip_hdr->element_index,
1784 SES_ELEM_INDEX_INDIVIDUAL);
1785 if (telement != NULL &&
1786 (ses_typehasaddlstatus(enc, titer.type_index) !=
1787 TYPE_ADDLSTATUS_NONE ||
1788 titer.type_index > ELMTYP_SAS_CONN)) {
1794 if (iter.individual_element_index > expected_index
1795 && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1796 ENC_VLOG(enc, "%s: provided element "
1797 "index %d skips mandatory status "
1798 " element at index %d\n",
1799 __func__, eip_hdr->element_index,
1803 elmpriv = element->elm_private;
1804 elmpriv->addl.hdr = elm_hdr;
1805 ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1806 "type element index=%d, offset=0x%x, "
1807 "byte0=0x%x, length=0x%x\n", __func__,
1808 iter.global_element_index, iter.type_index,
1809 iter.type_element_index, offset, elmpriv->addl.hdr->byte0,
1810 elmpriv->addl.hdr->length);
1812 /* Skip to after the length field */
1813 offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1815 /* Make sure the descriptor is within bounds */
1816 if ((offset + elmpriv->addl.hdr->length) > length) {
1817 ENC_VLOG(enc, "Element %d Beyond End "
1818 "of Additional Element Status Descriptors\n",
1819 iter.global_element_index);
1823 /* Advance to the protocol data, skipping eip bytes if needed */
1824 offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1825 proto_info_len = elmpriv->addl.hdr->length
1826 - (eip * SES_EIP_HDR_EXTRA_LEN);
1828 /* Errors in this block are ignored as they are non-fatal */
1829 switch(ses_elm_addlstatus_proto(elmpriv->addl.hdr)) {
1831 if (elmpriv->addl.hdr->length == 0)
1833 ses_get_elm_addlstatus_fc(enc, enc_cache,
1834 &buf[offset], proto_info_len);
1836 case SPSP_PROTO_SAS:
1837 if (elmpriv->addl.hdr->length <= 2)
1839 ses_get_elm_addlstatus_sas(enc, enc_cache,
1842 eip, iter.type_index,
1843 iter.global_element_index);
1846 ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1847 "Protocol 0x%x\n", iter.global_element_index,
1848 ses_elm_addlstatus_proto(elmpriv->addl.hdr));
1852 offset += proto_info_len;
1857 ses_cache_free_elm_addlstatus(enc, enc_cache);
1858 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1859 enc_update_request(enc, SES_PUBLISH_CACHE);
1864 ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1865 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1869 ses = enc->enc_private;
1872 * o Generation count wrong.
1873 * o Some SCSI status error.
1875 ses_terminate_control_requests(&ses->ses_pending_requests, error);
1876 enc_update_request(enc, SES_UPDATE_GETSTATUS);
1881 ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1882 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1884 struct ses_iterator iter;
1885 enc_cache_t *enc_cache;
1886 ses_cache_t *ses_cache;
1887 enc_element_t *element;
1889 enc_cache = &enc->enc_daemon_cache;
1890 ses_cache = enc_cache->private;
1892 ses_iter_init(enc, enc_cache, &iter);
1893 while ((element = ses_iter_next(&iter)) != NULL) {
1895 * ses_set_physpath() returns success if we changed
1896 * the physpath of any element. This allows us to
1897 * only announce devices once regardless of how
1898 * many times we process additional element status.
1900 if (ses_set_physpath(enc, element, &iter) == 0)
1901 ses_print_addl_data(enc, element);
1908 ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1909 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1912 sx_xlock(&enc->enc_cache_lock);
1913 ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1914 /*dst*/&enc->enc_cache);
1915 sx_xunlock(&enc->enc_cache_lock);
1921 * \brief Parse the descriptors for each object.
1923 * \param enc The SES softc to update.
1924 * \param buf The buffer containing the descriptor list response.
1925 * \param xfer_len Size of the buffer.
1927 * \return 0 on success, errno otherwise.
1930 ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1931 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1934 struct ses_iterator iter;
1935 enc_element_t *element;
1938 u_long length, plength;
1939 enc_cache_t *enc_cache;
1940 ses_cache_t *ses_cache;
1942 ses_element_t *elmpriv;
1943 const struct ses_page_hdr *phdr;
1944 const struct ses_elm_desc_hdr *hdr;
1946 ses = enc->enc_private;
1947 enc_cache = &enc->enc_daemon_cache;
1948 ses_cache = enc_cache->private;
1956 ses_cache_free_elm_descs(enc, enc_cache);
1957 ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
1960 phdr = &ses_cache->elm_descs_page->hdr;
1961 plength = ses_page_length(phdr);
1962 if (xfer_len < sizeof(struct ses_page_hdr)) {
1963 ENC_VLOG(enc, "Runt Element Descriptor Page\n");
1966 if (plength > xfer_len) {
1967 ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
1971 if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
1972 ENC_VLOG(enc, "%s: Generation count change detected\n",
1974 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1978 offset = sizeof(struct ses_page_hdr);
1980 ses_iter_init(enc, enc_cache, &iter);
1981 while (offset < plength
1982 && (element = ses_iter_next(&iter)) != NULL) {
1984 if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
1985 ENC_VLOG(enc, "Element %d Descriptor Header Past "
1986 "End of Buffer\n", iter.global_element_index);
1989 hdr = (struct ses_elm_desc_hdr *)&buf[offset];
1990 length = scsi_2btoul(hdr->length);
1991 ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
1992 iter.global_element_index, iter.type_index,
1993 iter.type_element_index, length, offset);
1994 if ((offset + sizeof(*hdr) + length) > plength) {
1995 ENC_VLOG(enc, "Element%d Descriptor Past "
1996 "End of Buffer\n", iter.global_element_index);
1999 offset += sizeof(*hdr);
2002 elmpriv = element->elm_private;
2003 elmpriv->descr_len = length;
2004 elmpriv->descr = &buf[offset];
2007 /* skip over the descriptor itself */
2014 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2015 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2017 enc_update_request(enc, SES_PUBLISH_CACHE);
2022 ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2023 union ccb *ccb, uint8_t *buf)
2026 if (enc->enc_type == ENC_SEMB_SES) {
2027 semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2028 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2029 state->page_code, buf, state->buf_size,
2032 scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2033 NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2034 state->page_code, buf, state->buf_size,
2035 SSD_FULL_SIZE, state->timeout);
2041 * \brief Encode the object status into the response buffer, which is
2042 * expected to contain the current enclosure status. This function
2043 * turns off all the 'select' bits for the objects except for the
2044 * object specified, then sends it back to the enclosure.
2046 * \param enc SES enclosure the change is being applied to.
2047 * \param buf Buffer containing the current enclosure status response.
2048 * \param amt Length of the response in the buffer.
2049 * \param req The control request to be applied to buf.
2051 * \return 0 on success, errno otherwise.
2054 ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2056 struct ses_iterator iter;
2057 enc_element_t *element;
2059 struct ses_control_page_hdr *hdr;
2061 ses_iter_init(enc, &enc->enc_cache, &iter);
2062 hdr = (struct ses_control_page_hdr *)buf;
2063 if (req->elm_idx == -1) {
2064 /* for enclosure status, at least 2 bytes are needed */
2067 hdr->control_flags =
2068 req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2069 ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2073 element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2074 if (element == NULL)
2078 * Seek to the type set that corresponds to the requested object.
2079 * The +1 is for the overall status element for the type.
2081 offset = sizeof(struct ses_control_page_hdr)
2082 + (iter.global_element_index * sizeof(struct ses_comstat));
2084 /* Check for buffer overflow. */
2085 if (offset + sizeof(struct ses_comstat) > amt)
2088 /* Set the status. */
2089 memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2091 ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2092 iter.type_index, iter.global_element_index, offset,
2093 req->elm_stat.comstatus, req->elm_stat.comstat[0],
2094 req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2100 ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2101 union ccb *ccb, uint8_t *buf)
2104 enc_cache_t *enc_cache;
2105 ses_cache_t *ses_cache;
2106 struct ses_control_page_hdr *hdr;
2107 ses_control_request_t *req;
2111 ses = enc->enc_private;
2112 enc_cache = &enc->enc_daemon_cache;
2113 ses_cache = enc_cache->private;
2114 hdr = (struct ses_control_page_hdr *)buf;
2116 if (ses_cache->status_page == NULL) {
2117 ses_terminate_control_requests(&ses->ses_requests, EIO);
2121 plength = ses_page_length(&ses_cache->status_page->hdr);
2122 memcpy(buf, ses_cache->status_page, plength);
2124 /* Disable the select bits in all status entries. */
2125 offset = sizeof(struct ses_control_page_hdr);
2126 for (offset = sizeof(struct ses_control_page_hdr);
2127 offset < plength; offset += sizeof(struct ses_comstat)) {
2128 buf[offset] &= ~SESCTL_CSEL;
2131 /* And make sure the INVOP bit is clear. */
2132 hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2134 /* Apply incoming requests. */
2135 while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2137 TAILQ_REMOVE(&ses->ses_requests, req, links);
2138 req->result = ses_encode(enc, buf, plength, req);
2139 if (req->result != 0) {
2143 TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2146 if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2149 /* Fill out the ccb */
2150 if (enc->enc_type == ENC_SEMB_SES) {
2151 semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2153 buf, ses_page_length(&ses_cache->status_page->hdr),
2156 scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2157 MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2158 /*device_offline*/0, /*self_test*/0,
2159 /*page_format*/1, /*self_test_code*/0,
2160 buf, ses_page_length(&ses_cache->status_page->hdr),
2161 SSD_FULL_SIZE, state->timeout);
2167 ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2168 uint8_t *buf, int bufsiz)
2170 ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2174 #define SES_PRINT_PORTS(p, type) do { \
2175 sbuf_printf(sbp, " %s(", type); \
2176 if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) == 0) \
2177 sbuf_printf(sbp, " None"); \
2179 if ((p) & SES_SASOBJ_DEV_PHY_SMP) \
2180 sbuf_printf(sbp, " SMP"); \
2181 if ((p) & SES_SASOBJ_DEV_PHY_STP) \
2182 sbuf_printf(sbp, " STP"); \
2183 if ((p) & SES_SASOBJ_DEV_PHY_SSP) \
2184 sbuf_printf(sbp, " SSP"); \
2186 sbuf_printf(sbp, " )"); \
2190 * \brief Print the additional element status data for this object, for SAS
2191 * type 0 objects. See SES2 r20 Section 6.1.13.3.2.
2193 * \param sesname SES device name associated with the object.
2194 * \param sbp Sbuf to print to.
2195 * \param obj The object to print the data for.
2196 * \param periph_name Peripheral string associated with the object.
2199 ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2200 enc_element_t *obj, char *periph_name)
2203 ses_element_t *elmpriv;
2204 struct ses_addl_status *addl;
2205 struct ses_elm_sas_device_phy *phy;
2207 elmpriv = obj->elm_private;
2208 addl = &(elmpriv->addl);
2209 if (addl->proto_hdr.sas == NULL)
2211 sbuf_printf(sbp, "%s: %s: SAS Device Slot Element:",
2212 sesname, periph_name);
2213 sbuf_printf(sbp, " %d Phys", addl->proto_hdr.sas->base_hdr.num_phys);
2214 if (ses_elm_addlstatus_eip(addl->hdr))
2215 sbuf_printf(sbp, " at Slot %d",
2216 addl->proto_hdr.sas->type0_eip.dev_slot_num);
2217 if (ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas))
2218 sbuf_printf(sbp, ", Not All Phys");
2219 sbuf_printf(sbp, "\n");
2220 if (addl->proto_data.sasdev_phys == NULL)
2222 for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2223 phy = &addl->proto_data.sasdev_phys[i];
2224 sbuf_printf(sbp, "%s: phy %d:", sesname, i);
2225 if (ses_elm_sas_dev_phy_sata_dev(phy))
2226 /* Spec says all other fields are specific values */
2227 sbuf_printf(sbp, " SATA device\n");
2229 sbuf_printf(sbp, " SAS device type %d id %d\n",
2230 ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2231 sbuf_printf(sbp, "%s: phy %d: protocols:", sesname, i);
2232 SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2233 SES_PRINT_PORTS(phy->target_ports, "Target");
2234 sbuf_printf(sbp, "\n");
2236 sbuf_printf(sbp, "%s: phy %d: parent %jx addr %jx\n",
2238 (uintmax_t)scsi_8btou64(phy->parent_addr),
2239 (uintmax_t)scsi_8btou64(phy->phy_addr));
2242 #undef SES_PRINT_PORTS
2245 * \brief Report whether a given enclosure object is an expander.
2247 * \param enc SES softc associated with object.
2248 * \param obj Enclosure object to report for.
2250 * \return 1 if true, 0 otherwise.
2253 ses_obj_is_expander(enc_softc_t *enc, enc_element_t *obj)
2255 return (obj->enctype == ELMTYP_SAS_EXP);
2259 * \brief Print the additional element status data for this object, for SAS
2260 * type 1 objects. See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2262 * \param enc SES enclosure, needed for type identification.
2263 * \param sesname SES device name associated with the object.
2264 * \param sbp Sbuf to print to.
2265 * \param obj The object to print the data for.
2266 * \param periph_name Peripheral string associated with the object.
2269 ses_print_addl_data_sas_type1(enc_softc_t *enc, char *sesname,
2270 struct sbuf *sbp, enc_element_t *obj, char *periph_name)
2273 ses_element_t *elmpriv;
2274 struct ses_addl_status *addl;
2275 struct ses_elm_sas_expander_phy *exp_phy;
2276 struct ses_elm_sas_port_phy *port_phy;
2278 elmpriv = obj->elm_private;
2279 addl = &(elmpriv->addl);
2280 if (addl->proto_hdr.sas == NULL)
2282 sbuf_printf(sbp, "%s: %s: SAS ", sesname, periph_name);
2283 if (ses_obj_is_expander(enc, obj)) {
2284 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2285 sbuf_printf(sbp, "Expander: %d Phys", num_phys);
2286 if (addl->proto_data.sasexp_phys == NULL)
2288 for (i = 0;i < num_phys;i++) {
2289 exp_phy = &addl->proto_data.sasexp_phys[i];
2290 sbuf_printf(sbp, "%s: phy %d: connector %d other %d\n",
2291 sesname, i, exp_phy->connector_index,
2292 exp_phy->other_index);
2295 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2296 sbuf_printf(sbp, "Port: %d Phys", num_phys);
2297 if (addl->proto_data.sasport_phys == NULL)
2299 for (i = 0;i < num_phys;i++) {
2300 port_phy = &addl->proto_data.sasport_phys[i];
2302 "%s: phy %d: id %d connector %d other %d\n",
2303 sesname, i, port_phy->phy_id,
2304 port_phy->connector_index, port_phy->other_index);
2305 sbuf_printf(sbp, "%s: phy %d: addr %jx\n", sesname, i,
2306 (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2312 * \brief Print the additional element status data for this object.
2314 * \param enc SES softc associated with the object.
2315 * \param obj The object to print the data for.
2318 ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2320 ses_element_t *elmpriv;
2321 struct ses_addl_status *addl;
2322 struct sbuf sesname, name, out;
2324 elmpriv = obj->elm_private;
2325 if (elmpriv == NULL)
2328 addl = &(elmpriv->addl);
2329 if (addl->hdr == NULL)
2332 sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2333 sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2334 sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2335 ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2336 if (sbuf_len(&name) == 0)
2337 sbuf_printf(&name, "(none)");
2339 sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2340 enc->periph->unit_number);
2341 sbuf_finish(&sesname);
2342 if (elmpriv->descr != NULL)
2343 sbuf_printf(&out, "%s: %s: Element descriptor: '%s'\n",
2344 sbuf_data(&sesname), sbuf_data(&name), elmpriv->descr);
2345 switch(ses_elm_addlstatus_proto(addl->hdr)) {
2346 case SPSP_PROTO_SAS:
2347 switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2348 case SES_SASOBJ_TYPE_SLOT:
2349 ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2350 &out, obj, sbuf_data(&name));
2352 case SES_SASOBJ_TYPE_OTHER:
2353 ses_print_addl_data_sas_type1(enc, sbuf_data(&sesname),
2354 &out, obj, sbuf_data(&name));
2360 case SPSP_PROTO_FC: /* stubbed for now */
2366 printf("%s", sbuf_data(&out));
2369 sbuf_delete(&sesname);
2373 * \brief Update the softc with the additional element status data for this
2374 * object, for SAS type 0 objects.
2376 * \param enc SES softc to be updated.
2377 * \param buf The additional element status response buffer.
2378 * \param bufsiz Size of the response buffer.
2379 * \param eip The EIP bit value.
2380 * \param nobj Number of objects attached to the SES softc.
2382 * \return 0 on success, errno otherwise.
2385 ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2386 uint8_t *buf, int bufsiz, int eip, int nobj)
2388 int err, offset, physz;
2390 ses_element_t *elmpriv;
2391 struct ses_addl_status *addl;
2395 /* basic object setup */
2396 obj = &(enc_cache->elm_map[nobj]);
2397 elmpriv = obj->elm_private;
2398 addl = &(elmpriv->addl);
2400 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2402 /* Don't assume this object has any phys */
2403 bzero(&addl->proto_data, sizeof(addl->proto_data));
2404 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2407 /* Skip forward to the phy list */
2409 offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2411 offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2413 /* Make sure the phy list fits in the buffer */
2414 physz = addl->proto_hdr.sas->base_hdr.num_phys;
2415 physz *= sizeof(struct ses_elm_sas_device_phy);
2416 if (physz > (bufsiz - offset + 4)) {
2417 ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2423 /* Point to the phy list */
2424 addl->proto_data.sasdev_phys =
2425 (struct ses_elm_sas_device_phy *)&buf[offset];
2432 * \brief Update the softc with the additional element status data for this
2433 * object, for SAS type 1 objects.
2435 * \param enc SES softc to be updated.
2436 * \param buf The additional element status response buffer.
2437 * \param bufsiz Size of the response buffer.
2438 * \param eip The EIP bit value.
2439 * \param nobj Number of objects attached to the SES softc.
2441 * \return 0 on success, errno otherwise.
2444 ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2445 uint8_t *buf, int bufsiz, int eip, int nobj)
2447 int err, offset, physz;
2449 ses_element_t *elmpriv;
2450 struct ses_addl_status *addl;
2454 /* basic object setup */
2455 obj = &(enc_cache->elm_map[nobj]);
2456 elmpriv = obj->elm_private;
2457 addl = &(elmpriv->addl);
2459 addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2461 /* Don't assume this object has any phys */
2462 bzero(&addl->proto_data, sizeof(addl->proto_data));
2463 if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2466 /* Process expanders differently from other type1 cases */
2467 if (ses_obj_is_expander(enc, obj)) {
2468 offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2469 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2470 sizeof(struct ses_elm_sas_expander_phy);
2471 if (physz > (bufsiz - offset)) {
2472 ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2473 "End Of Buffer\n", nobj);
2477 addl->proto_data.sasexp_phys =
2478 (struct ses_elm_sas_expander_phy *)&buf[offset];
2480 offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2481 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2482 sizeof(struct ses_elm_sas_port_phy);
2483 if (physz > (bufsiz - offset + 4)) {
2484 ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2485 "Of Buffer\n", nobj);
2489 addl->proto_data.sasport_phys =
2490 (struct ses_elm_sas_port_phy *)&buf[offset];
2498 * \brief Update the softc with the additional element status data for this
2499 * object, for SAS objects.
2501 * \param enc SES softc to be updated.
2502 * \param buf The additional element status response buffer.
2503 * \param bufsiz Size of the response buffer.
2504 * \param eip The EIP bit value.
2505 * \param tidx Type index for this object.
2506 * \param nobj Number of objects attached to the SES softc.
2508 * \return 0 on success, errno otherwise.
2511 ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2512 uint8_t *buf, int bufsiz, int eip, int tidx,
2516 ses_cache_t *ses_cache;
2517 union ses_elm_sas_hdr *hdr;
2519 /* Need to be able to read the descriptor type! */
2520 if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2525 ses_cache = enc_cache->private;
2527 hdr = (union ses_elm_sas_hdr *)buf;
2528 dtype = ses_elm_sas_descr_type(hdr);
2530 case SES_SASOBJ_TYPE_SLOT:
2531 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2533 case ELMTYP_ARRAY_DEV:
2536 ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2537 "invalid for SES element type 0x%x\n", nobj,
2538 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2542 err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2546 case SES_SASOBJ_TYPE_OTHER:
2547 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2548 case ELMTYP_SAS_EXP:
2549 case ELMTYP_SCSI_INI:
2550 case ELMTYP_SCSI_TGT:
2554 ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2555 "invalid for SES element type 0x%x\n", nobj,
2556 ses_cache->ses_types[tidx].hdr->etype_elm_type);
2560 err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2564 ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2565 "of unknown type 0x%x\n", nobj,
2566 ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2576 ses_softc_invalidate(enc_softc_t *enc)
2580 ses = enc->enc_private;
2581 ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2585 ses_softc_cleanup(enc_softc_t *enc)
2588 ses_cache_free(enc, &enc->enc_cache);
2589 ses_cache_free(enc, &enc->enc_daemon_cache);
2590 ENC_FREE_AND_NULL(enc->enc_private);
2591 ENC_FREE_AND_NULL(enc->enc_cache.private);
2592 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2596 ses_init_enc(enc_softc_t *enc)
2602 ses_get_enc_status(enc_softc_t *enc, int slpflag)
2604 /* Automatically updated, caller checks enc_cache->encstat itself */
2609 ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2611 ses_control_request_t req;
2614 ses = enc->enc_private;
2615 req.elm_idx = SES_SETSTATUS_ENC_IDX;
2616 req.elm_stat.comstatus = encstat & 0xf;
2618 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2619 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2620 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2622 return (req.result);
2626 ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2628 unsigned int i = elms->elm_idx;
2630 memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2635 ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2637 ses_control_request_t req;
2640 /* If this is clear, we don't do diddly. */
2641 if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2644 ses = enc->enc_private;
2645 req.elm_idx = elms->elm_idx;
2646 memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2648 TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2649 enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2650 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2652 return (req.result);
2656 ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2658 int i = (int)elmd->elm_idx;
2659 ses_element_t *elmpriv;
2661 /* Assume caller has already checked obj_id validity */
2662 elmpriv = enc->enc_cache.elm_map[i].elm_private;
2663 /* object might not have a descriptor */
2664 if (elmpriv == NULL || elmpriv->descr == NULL) {
2665 elmd->elm_desc_len = 0;
2668 if (elmd->elm_desc_len > elmpriv->descr_len)
2669 elmd->elm_desc_len = elmpriv->descr_len;
2670 copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2675 * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2676 * given object id if one is available.
2678 * \param enc SES softc to examine.
2679 * \param objdn ioctl structure to read/write device name info.
2681 * \return 0 on success, errno otherwise.
2684 ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2689 len = elmdn->elm_names_size;
2693 sbuf_new(&sb, elmdn->elm_devnames, len, 0);
2695 cam_periph_unlock(enc->periph);
2696 ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2697 ses_elmdevname_callback, &sb);
2699 elmdn->elm_names_len = sbuf_len(&sb);
2700 cam_periph_lock(enc->periph);
2701 return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2705 * \brief Send a string to the primary subenclosure using the String Out
2706 * SES diagnostic page.
2708 * \param enc SES enclosure to run the command on.
2709 * \param sstr SES string structure to operate on
2710 * \param ioc Ioctl being performed
2712 * \return 0 on success, errno otherwise.
2715 ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2718 enc_cache_t *enc_cache;
2719 ses_cache_t *ses_cache;
2720 const struct ses_enc_desc *enc_desc;
2721 int amt, payload, ret;
2730 ses = enc->enc_private;
2731 enc_cache = &enc->enc_daemon_cache;
2732 ses_cache = enc_cache->private;
2734 /* Implement SES2r20 6.1.6 */
2735 if (sstr->bufsiz > 0xffff)
2736 return (EINVAL); /* buffer size too large */
2738 if (ioc == ENCIOC_SETSTRING) {
2739 payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2741 buf = ENC_MALLOC(payload);
2745 ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2746 /* Construct the page request */
2747 buf[0] = SesStringOut;
2749 buf[2] = sstr->bufsiz >> 8;
2750 buf[3] = sstr->bufsiz & 0xff;
2751 memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2752 } else if (ioc == ENCIOC_GETSTRING) {
2753 payload = sstr->bufsiz;
2755 ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2757 } else if (ioc == ENCIOC_GETENCNAME) {
2758 if (ses_cache->ses_nsubencs < 1)
2760 enc_desc = ses_cache->subencs[0];
2761 cam_strvis(vendor, enc_desc->vendor_id,
2762 sizeof(enc_desc->vendor_id), sizeof(vendor));
2763 cam_strvis(product, enc_desc->product_id,
2764 sizeof(enc_desc->product_id), sizeof(product));
2765 cam_strvis(rev, enc_desc->product_rev,
2766 sizeof(enc_desc->product_rev), sizeof(rev));
2767 rsize = snprintf(str, sizeof(str), "%s %s %s",
2768 vendor, product, rev) + 1;
2769 if (rsize > sizeof(str))
2770 rsize = sizeof(str);
2771 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2773 if (size > sstr->bufsiz)
2774 size = sstr->bufsiz;
2775 copyout(str, sstr->buf, size);
2776 return (size == rsize ? 0 : ENOMEM);
2777 } else if (ioc == ENCIOC_GETENCID) {
2778 if (ses_cache->ses_nsubencs < 1)
2780 enc_desc = ses_cache->subencs[0];
2781 rsize = snprintf(str, sizeof(str), "%16jx",
2782 scsi_8btou64(enc_desc->logical_id)) + 1;
2783 if (rsize > sizeof(str))
2784 rsize = sizeof(str);
2785 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2787 if (size > sstr->bufsiz)
2788 size = sstr->bufsiz;
2789 copyout(str, sstr->buf, size);
2790 return (size == rsize ? 0 : ENOMEM);
2794 ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2795 if (ioc == ENCIOC_SETSTRING)
2801 * \invariant Called with cam_periph mutex held.
2804 ses_poll_status(enc_softc_t *enc)
2808 ses = enc->enc_private;
2809 enc_update_request(enc, SES_UPDATE_GETSTATUS);
2810 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2811 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2815 * \brief Notification received when CAM detects a new device in the
2816 * SCSI domain in which this SEP resides.
2818 * \param enc SES enclosure instance.
2821 ses_device_found(enc_softc_t *enc)
2823 ses_poll_status(enc);
2824 enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2827 static struct enc_vec ses_enc_vec =
2829 .softc_invalidate = ses_softc_invalidate,
2830 .softc_cleanup = ses_softc_cleanup,
2831 .init_enc = ses_init_enc,
2832 .get_enc_status = ses_get_enc_status,
2833 .set_enc_status = ses_set_enc_status,
2834 .get_elm_status = ses_get_elm_status,
2835 .set_elm_status = ses_set_elm_status,
2836 .get_elm_desc = ses_get_elm_desc,
2837 .get_elm_devnames = ses_get_elm_devnames,
2838 .handle_string = ses_handle_string,
2839 .device_found = ses_device_found,
2840 .poll_status = ses_poll_status
2844 * \brief Initialize a new SES instance.
2846 * \param enc SES softc structure to set up the instance in.
2847 * \param doinit Do the initialization (see main driver).
2849 * \return 0 on success, errno otherwise.
2852 ses_softc_init(enc_softc_t *enc)
2854 ses_softc_t *ses_softc;
2856 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2857 ("entering enc_softc_init(%p)\n", enc));
2859 enc->enc_vec = ses_enc_vec;
2860 enc->enc_fsm_states = enc_fsm_states;
2862 if (enc->enc_private == NULL)
2863 enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2864 if (enc->enc_cache.private == NULL)
2865 enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2866 if (enc->enc_daemon_cache.private == NULL)
2867 enc->enc_daemon_cache.private =
2868 ENC_MALLOCZ(sizeof(ses_cache_t));
2870 if (enc->enc_private == NULL
2871 || enc->enc_cache.private == NULL
2872 || enc->enc_daemon_cache.private == NULL) {
2873 ENC_FREE_AND_NULL(enc->enc_private);
2874 ENC_FREE_AND_NULL(enc->enc_cache.private);
2875 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2879 ses_softc = enc->enc_private;
2880 TAILQ_INIT(&ses_softc->ses_requests);
2881 TAILQ_INIT(&ses_softc->ses_pending_requests);
2883 enc_update_request(enc, SES_UPDATE_PAGES);
2885 // XXX: Move this to the FSM so it doesn't hang init
2886 if (0) (void) ses_set_timed_completion(enc, 1);