]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_enc_ses.c
MFV r345495:
[FreeBSD/FreeBSD.git] / sys / cam / scsi / scsi_enc_ses.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Matthew Jacob
5  * Copyright (c) 2010 Spectra Logic Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /**
31  * \file scsi_enc_ses.c
32  *
33  * Structures and routines specific && private to SES only
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40
41 #include <sys/ctype.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/queue.h>
48 #include <sys/sbuf.h>
49 #include <sys/sx.h>
50 #include <sys/systm.h>
51 #include <sys/types.h>
52
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_periph.h>
57
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/scsi_enc.h>
60 #include <cam/scsi/scsi_enc_internal.h>
61
62 /* SES Native Type Device Support */
63
64 /* SES Diagnostic Page Codes */
65 typedef enum {
66         SesSupportedPages       = 0x0,
67         SesConfigPage           = 0x1,
68         SesControlPage          = 0x2,
69         SesStatusPage           = SesControlPage,
70         SesHelpTxt              = 0x3,
71         SesStringOut            = 0x4,
72         SesStringIn             = SesStringOut,
73         SesThresholdOut         = 0x5,
74         SesThresholdIn          = SesThresholdOut,
75         SesArrayControl         = 0x6,  /* Obsolete in SES v2 */
76         SesArrayStatus          = SesArrayControl,
77         SesElementDescriptor    = 0x7,
78         SesShortStatus          = 0x8,
79         SesEnclosureBusy        = 0x9,
80         SesAddlElementStatus    = 0xa
81 } SesDiagPageCodes;
82
83 typedef struct ses_type {
84         const struct ses_elm_type_desc  *hdr;
85         const char                      *text;
86 } ses_type_t;
87
88 typedef struct ses_comstat {
89         uint8_t comstatus;
90         uint8_t comstat[3];
91 } ses_comstat_t;
92
93 typedef union ses_addl_data {
94         struct ses_elm_sas_device_phy *sasdev_phys;
95         struct ses_elm_sas_expander_phy *sasexp_phys;
96         struct ses_elm_sas_port_phy *sasport_phys;
97         struct ses_fcobj_port *fc_ports;
98 } ses_add_data_t;
99
100 typedef struct ses_addl_status {
101         struct ses_elm_addlstatus_base_hdr *hdr;
102         union {
103                 union ses_fcobj_hdr *fc;
104                 union ses_elm_sas_hdr *sas;
105         } proto_hdr;
106         union ses_addl_data proto_data; /* array sizes stored in header */
107 } ses_add_status_t;
108
109 typedef struct ses_element {
110         uint8_t eip;                    /* eip bit is set */
111         uint16_t descr_len;             /* length of the descriptor */
112         char *descr;                    /* descriptor for this object */
113         struct ses_addl_status addl;    /* additional status info */
114 } ses_element_t;
115
116 typedef struct ses_control_request {
117         int           elm_idx;
118         ses_comstat_t elm_stat;
119         int           result;
120         TAILQ_ENTRY(ses_control_request) links;
121 } ses_control_request_t;
122 TAILQ_HEAD(ses_control_reqlist, ses_control_request);
123 typedef struct ses_control_reqlist ses_control_reqlist_t;
124 enum {
125         SES_SETSTATUS_ENC_IDX = -1
126 };
127
128 static void
129 ses_terminate_control_requests(ses_control_reqlist_t *reqlist, int result)
130 {
131         ses_control_request_t *req;
132
133         while ((req = TAILQ_FIRST(reqlist)) != NULL) {
134                 TAILQ_REMOVE(reqlist, req, links);
135                 req->result = result;
136                 wakeup(req);
137         }
138 }
139
140 enum ses_iter_index_values {
141         /**
142          * \brief  Value of an initialized but invalid index
143          *         in a ses_iterator object.
144          *
145          * This value is used for the  individual_element_index of
146          * overal status elements and for all index types when
147          * an iterator is first initialized.
148          */
149         ITERATOR_INDEX_INVALID = -1,
150
151         /**
152          * \brief  Value of an index in a ses_iterator object
153          *         when the iterator has traversed past the last
154          *         valid element..
155          */
156         ITERATOR_INDEX_END     = INT_MAX
157 };
158
159 /**
160  * \brief Structure encapsulating all data necessary to traverse the
161  *        elements of a SES configuration.
162  *
163  * The ses_iterator object simplifies the task of iterating through all
164  * elements detected via the SES configuration page by tracking the numerous
165  * element indexes that, instead of memoizing in the softc, we calculate
166  * on the fly during the traversal of the element objects.  The various
167  * indexes are necessary due to the varying needs of matching objects in
168  * the different SES pages.  Some pages (e.g. Status/Control) contain all
169  * elements, while others (e.g. Additional Element Status) only contain
170  * individual elements (no overal status elements) of particular types.
171  *
172  * To use an iterator, initialize it with ses_iter_init(), and then
173  * use ses_iter_next() to traverse the elements (including the first) in
174  * the configuration.  Once an iterator is initiailized with ses_iter_init(),
175  * you may also seek to any particular element by either it's global or
176  * individual element index via the ses_iter_seek_to() function.  You may
177  * also return an iterator to the position just before the first element
178  * (i.e. the same state as after an ses_iter_init()), with ses_iter_reset().
179  */
180 struct ses_iterator {
181         /**
182          * \brief Backlink to the overal software configuration structure.
183          *
184          * This is included for convenience so the iteration functions
185          * need only take a single, struct ses_iterator *, argument.
186          */
187         enc_softc_t *enc;
188
189         enc_cache_t *cache;
190
191         /**
192          * \brief Index of the type of the current element within the
193          *        ses_cache's ses_types array.
194          */
195         int               type_index;
196
197         /**
198          * \brief The position (0 based) of this element relative to all other
199          *        elements of this type.
200          *
201          * This index resets to zero every time the iterator transitions
202          * to elements of a new type in the configuration.
203          */
204         int               type_element_index;
205
206         /**
207          * \brief The position (0 based) of this element relative to all
208          *        other individual status elements in the configuration.
209          *
210          * This index ranges from 0 through the number of individual
211          * elements in the configuration.  When the iterator returns
212          * an overall status element, individual_element_index is
213          * set to ITERATOR_INDEX_INVALID, to indicate that it does
214          * not apply to the current element.
215          */
216         int               individual_element_index;
217
218         /**
219          * \brief The position (0 based) of this element relative to
220          *        all elements in the configration.
221          *
222          * This index is appropriate for indexing into enc->ses_elm_map.
223          */
224         int               global_element_index;
225
226         /**
227          * \brief The last valid individual element index of this
228          *        iterator.
229          *
230          * When an iterator traverses an overal status element, the
231          * individual element index is reset to ITERATOR_INDEX_INVALID
232          * to prevent unintential use of the individual_element_index
233          * field.  The saved_individual_element_index allows the iterator
234          * to restore it's position in the individual elements upon
235          * reaching the next individual element.
236          */
237         int               saved_individual_element_index;
238 };
239
240 typedef enum {
241         SES_UPDATE_NONE,
242         SES_UPDATE_PAGES,
243         SES_UPDATE_GETCONFIG,
244         SES_UPDATE_GETSTATUS,
245         SES_UPDATE_GETELMDESCS,
246         SES_UPDATE_GETELMADDLSTATUS,
247         SES_PROCESS_CONTROL_REQS,
248         SES_PUBLISH_PHYSPATHS,
249         SES_PUBLISH_CACHE,
250         SES_NUM_UPDATE_STATES
251 } ses_update_action;
252
253 static enc_softc_cleanup_t ses_softc_cleanup;
254
255 #define SCSZ    0x8000
256
257 static fsm_fill_handler_t ses_fill_rcv_diag_io;
258 static fsm_fill_handler_t ses_fill_control_request;
259 static fsm_done_handler_t ses_process_pages;
260 static fsm_done_handler_t ses_process_config;
261 static fsm_done_handler_t ses_process_status;
262 static fsm_done_handler_t ses_process_elm_descs;
263 static fsm_done_handler_t ses_process_elm_addlstatus;
264 static fsm_done_handler_t ses_process_control_request;
265 static fsm_done_handler_t ses_publish_physpaths;
266 static fsm_done_handler_t ses_publish_cache;
267
268 static struct enc_fsm_state enc_fsm_states[SES_NUM_UPDATE_STATES] =
269 {
270         { "SES_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
271         {
272                 "SES_UPDATE_PAGES",
273                 SesSupportedPages,
274                 SCSZ,
275                 60 * 1000,
276                 ses_fill_rcv_diag_io,
277                 ses_process_pages,
278                 enc_error
279         },
280         {
281                 "SES_UPDATE_GETCONFIG",
282                 SesConfigPage,
283                 SCSZ,
284                 60 * 1000,
285                 ses_fill_rcv_diag_io,
286                 ses_process_config,
287                 enc_error
288         },
289         {
290                 "SES_UPDATE_GETSTATUS",
291                 SesStatusPage,
292                 SCSZ,
293                 60 * 1000,
294                 ses_fill_rcv_diag_io,
295                 ses_process_status,
296                 enc_error
297         },
298         {
299                 "SES_UPDATE_GETELMDESCS",
300                 SesElementDescriptor,
301                 SCSZ,
302                 60 * 1000,
303                 ses_fill_rcv_diag_io,
304                 ses_process_elm_descs,
305                 enc_error
306         },
307         {
308                 "SES_UPDATE_GETELMADDLSTATUS",
309                 SesAddlElementStatus,
310                 SCSZ,
311                 60 * 1000,
312                 ses_fill_rcv_diag_io,
313                 ses_process_elm_addlstatus,
314                 enc_error
315         },
316         {
317                 "SES_PROCESS_CONTROL_REQS",
318                 SesControlPage,
319                 SCSZ,
320                 60 * 1000,
321                 ses_fill_control_request,
322                 ses_process_control_request,
323                 enc_error
324         },
325         {
326                 "SES_PUBLISH_PHYSPATHS",
327                 0,
328                 0,
329                 0,
330                 NULL,
331                 ses_publish_physpaths,
332                 NULL
333         },
334         {
335                 "SES_PUBLISH_CACHE",
336                 0,
337                 0,
338                 0,
339                 NULL,
340                 ses_publish_cache,
341                 NULL
342         }
343 };
344
345 typedef struct ses_cache {
346         /* Source for all the configuration data pointers */
347         const struct ses_cfg_page               *cfg_page;
348
349         /* References into the config page. */
350         int                                      ses_nsubencs;
351         const struct ses_enc_desc * const       *subencs;
352         int                                      ses_ntypes;
353         const ses_type_t                        *ses_types;
354
355         /* Source for all the status pointers */
356         const struct ses_status_page            *status_page;
357
358         /* Source for all the object descriptor pointers */
359         const struct ses_elem_descr_page        *elm_descs_page;
360
361         /* Source for all the additional object status pointers */
362         const struct ses_addl_elem_status_page  *elm_addlstatus_page;
363
364 } ses_cache_t;
365
366 typedef struct ses_softc {
367         uint32_t                ses_flags;
368 #define SES_FLAG_TIMEDCOMP      0x01
369 #define SES_FLAG_ADDLSTATUS     0x02
370 #define SES_FLAG_DESC           0x04
371
372         ses_control_reqlist_t   ses_requests;
373         ses_control_reqlist_t   ses_pending_requests;
374 } ses_softc_t;
375
376 /**
377  * \brief Reset a SES iterator to just before the first element
378  *        in the configuration.
379  *
380  * \param iter  The iterator object to reset.
381  *
382  * The indexes within a reset iterator are invalid and will only
383  * become valid upon completion of a ses_iter_seek_to() or a
384  * ses_iter_next().
385  */
386 static void
387 ses_iter_reset(struct ses_iterator *iter)
388 {
389         /*
390          * Set our indexes to just before the first valid element
391          * of the first type (ITERATOR_INDEX_INVALID == -1).  This
392          * simplifies the implementation of ses_iter_next().
393          */
394         iter->type_index                     = 0;
395         iter->type_element_index             = ITERATOR_INDEX_INVALID;
396         iter->global_element_index           = ITERATOR_INDEX_INVALID;
397         iter->individual_element_index       = ITERATOR_INDEX_INVALID;
398         iter->saved_individual_element_index = ITERATOR_INDEX_INVALID;
399 }
400
401 /**
402  * \brief Initialize the storage of a SES iterator and reset it to
403  *        the position just before the first element of the
404  *        configuration.
405  *
406  * \param enc   The SES softc for the SES instance whose configuration
407  *              will be enumerated by this iterator.
408  * \param iter  The iterator object to initialize.
409  */
410 static void
411 ses_iter_init(enc_softc_t *enc, enc_cache_t *cache, struct ses_iterator *iter)
412 {
413         iter->enc = enc;
414         iter->cache = cache;
415         ses_iter_reset(iter);
416 }
417
418 /**
419  * \brief Traverse the provided SES iterator to the next element
420  *        within the configuraiton.
421  *
422  * \param iter  The iterator to move.
423  *
424  * \return  If a valid next element exists, a pointer to it's enc_element_t.
425  *          Otherwise NULL.
426  */
427 static enc_element_t *
428 ses_iter_next(struct ses_iterator *iter)
429 {
430         ses_cache_t      *ses_cache;
431         const ses_type_t *element_type;
432
433         ses_cache = iter->cache->private;
434
435         /*
436          * Note: Treat nelms as signed, so we will hit this case
437          *       and immediately terminate the iteration if the
438          *       configuration has 0 objects.
439          */
440         if (iter->global_element_index >= (int)iter->cache->nelms - 1) {
441
442                 /* Elements exhausted. */
443                 iter->type_index               = ITERATOR_INDEX_END;
444                 iter->type_element_index       = ITERATOR_INDEX_END;
445                 iter->global_element_index     = ITERATOR_INDEX_END;
446                 iter->individual_element_index = ITERATOR_INDEX_END;
447                 return (NULL);
448         }
449
450         KASSERT((iter->type_index < ses_cache->ses_ntypes),
451                 ("Corrupted element iterator. %d not less than %d",
452                  iter->type_index, ses_cache->ses_ntypes));
453
454         element_type = &ses_cache->ses_types[iter->type_index];
455         iter->global_element_index++;
456         iter->type_element_index++;
457
458         /*
459          * There is an object for overal type status in addition
460          * to one for each allowed element, but only if the element
461          * count is non-zero.
462          */
463         if (iter->type_element_index > element_type->hdr->etype_maxelt) {
464
465                 /*
466                  * We've exhausted the elements of this type.
467                  * This next element belongs to the next type.
468                  */
469                 iter->type_index++;
470                 iter->type_element_index = 0;
471                 iter->saved_individual_element_index
472                     = iter->individual_element_index;
473                 iter->individual_element_index = ITERATOR_INDEX_INVALID;
474         }
475
476         if (iter->type_element_index > 0) {
477                 if (iter->type_element_index == 1) {
478                         iter->individual_element_index
479                             = iter->saved_individual_element_index;
480                 }
481                 iter->individual_element_index++;
482         }
483
484         return (&iter->cache->elm_map[iter->global_element_index]);
485 }
486
487 /**
488  * Element index types tracked by a SES iterator.
489  */
490 typedef enum {
491         /**
492          * Index relative to all elements (overall and individual)
493          * in the system.
494          */
495         SES_ELEM_INDEX_GLOBAL,
496
497         /**
498          * \brief Index relative to all individual elements in the system.
499          *
500          * This index counts only individual elements, skipping overall
501          * status elements.  This is the index space of the additional
502          * element status page (page 0xa).
503          */
504         SES_ELEM_INDEX_INDIVIDUAL
505 } ses_elem_index_type_t;
506
507 /**
508  * \brief Move the provided iterator forwards or backwards to the object 
509  *        having the give index.
510  *
511  * \param iter           The iterator on which to perform the seek.
512  * \param element_index  The index of the element to find.
513  * \param index_type     The type (global or individual) of element_index.
514  *
515  * \return  If the element is found, a pointer to it's enc_element_t.
516  *          Otherwise NULL.
517  */
518 static enc_element_t *
519 ses_iter_seek_to(struct ses_iterator *iter, int element_index,
520                  ses_elem_index_type_t index_type)
521 {
522         enc_element_t   *element;
523         int             *cur_index;
524
525         if (index_type == SES_ELEM_INDEX_GLOBAL)
526                 cur_index = &iter->global_element_index;
527         else
528                 cur_index = &iter->individual_element_index;
529
530         if (*cur_index == element_index) {
531                 /* Already there. */
532                 return (&iter->cache->elm_map[iter->global_element_index]);
533         }
534
535         ses_iter_reset(iter);
536         while ((element = ses_iter_next(iter)) != NULL
537             && *cur_index != element_index)
538                 ;
539
540         if (*cur_index != element_index)
541                 return (NULL);
542
543         return (element);
544 }
545
546 #if 0
547 static int ses_encode(enc_softc_t *, uint8_t *, int, int,
548     struct ses_comstat *);
549 #endif
550 static int ses_set_timed_completion(enc_softc_t *, uint8_t);
551 #if 0
552 static int ses_putstatus(enc_softc_t *, int, struct ses_comstat *);
553 #endif
554
555 static void ses_poll_status(enc_softc_t *);
556 static void ses_print_addl_data(enc_softc_t *, enc_element_t *);
557
558 /*=========================== SES cleanup routines ===========================*/
559
560 static void
561 ses_cache_free_elm_addlstatus(enc_softc_t *enc, enc_cache_t *cache)
562 {
563         ses_cache_t   *ses_cache;
564         ses_cache_t   *other_ses_cache;
565         enc_element_t *cur_elm;
566         enc_element_t *last_elm;
567
568         ENC_DLOG(enc, "%s: enter\n", __func__);
569         ses_cache = cache->private;
570         if (ses_cache->elm_addlstatus_page == NULL)
571                 return;
572
573         for (cur_elm = cache->elm_map,
574              last_elm = &cache->elm_map[cache->nelms];
575              cur_elm != last_elm; cur_elm++) {
576                 ses_element_t *elmpriv;
577
578                 elmpriv = cur_elm->elm_private;
579
580                 /* Clear references to the additional status page. */
581                 bzero(&elmpriv->addl, sizeof(elmpriv->addl));
582         }
583
584         other_ses_cache = enc_other_cache(enc, cache)->private;
585         if (other_ses_cache->elm_addlstatus_page
586          != ses_cache->elm_addlstatus_page)
587                 ENC_FREE(ses_cache->elm_addlstatus_page);
588         ses_cache->elm_addlstatus_page = NULL;
589 }
590
591 static void
592 ses_cache_free_elm_descs(enc_softc_t *enc, enc_cache_t *cache)
593 {
594         ses_cache_t   *ses_cache;
595         ses_cache_t   *other_ses_cache;
596         enc_element_t *cur_elm;
597         enc_element_t *last_elm;
598
599         ENC_DLOG(enc, "%s: enter\n", __func__);
600         ses_cache = cache->private;
601         if (ses_cache->elm_descs_page == NULL)
602                 return;
603
604         for (cur_elm = cache->elm_map,
605              last_elm = &cache->elm_map[cache->nelms];
606              cur_elm != last_elm; cur_elm++) {
607                 ses_element_t *elmpriv;
608
609                 elmpriv = cur_elm->elm_private;
610                 elmpriv->descr_len = 0;
611                 elmpriv->descr = NULL;
612         }
613
614         other_ses_cache = enc_other_cache(enc, cache)->private;
615         if (other_ses_cache->elm_descs_page
616          != ses_cache->elm_descs_page)
617                 ENC_FREE(ses_cache->elm_descs_page);
618         ses_cache->elm_descs_page = NULL;
619 }
620
621 static void
622 ses_cache_free_status(enc_softc_t *enc, enc_cache_t *cache)
623 {
624         ses_cache_t *ses_cache;
625         ses_cache_t *other_ses_cache;
626
627         ENC_DLOG(enc, "%s: enter\n", __func__);
628         ses_cache   = cache->private;
629         if (ses_cache->status_page == NULL)
630                 return;
631         
632         other_ses_cache = enc_other_cache(enc, cache)->private;
633         if (other_ses_cache->status_page != ses_cache->status_page)
634                 ENC_FREE(ses_cache->status_page);
635         ses_cache->status_page = NULL;
636 }
637
638 static void
639 ses_cache_free_elm_map(enc_softc_t *enc, enc_cache_t *cache)
640 {
641         enc_element_t *cur_elm;
642         enc_element_t *last_elm;
643
644         ENC_DLOG(enc, "%s: enter\n", __func__);
645         if (cache->elm_map == NULL)
646                 return;
647
648         ses_cache_free_elm_descs(enc, cache);
649         ses_cache_free_elm_addlstatus(enc, cache);
650         for (cur_elm = cache->elm_map,
651              last_elm = &cache->elm_map[cache->nelms];
652              cur_elm != last_elm; cur_elm++) {
653
654                 ENC_FREE_AND_NULL(cur_elm->elm_private);
655         }
656         ENC_FREE_AND_NULL(cache->elm_map);
657         cache->nelms = 0;
658         ENC_DLOG(enc, "%s: exit\n", __func__);
659 }
660
661 static void
662 ses_cache_free(enc_softc_t *enc, enc_cache_t *cache)
663 {
664         ses_cache_t *other_ses_cache;
665         ses_cache_t *ses_cache;
666
667         ENC_DLOG(enc, "%s: enter\n", __func__);
668         ses_cache_free_elm_addlstatus(enc, cache);
669         ses_cache_free_status(enc, cache);
670         ses_cache_free_elm_map(enc, cache);
671
672         ses_cache = cache->private;
673         ses_cache->ses_ntypes = 0;
674
675         other_ses_cache = enc_other_cache(enc, cache)->private;
676         if (other_ses_cache->subencs != ses_cache->subencs)
677                 ENC_FREE(ses_cache->subencs);
678         ses_cache->subencs = NULL;
679
680         if (other_ses_cache->ses_types != ses_cache->ses_types)
681                 ENC_FREE(ses_cache->ses_types);
682         ses_cache->ses_types = NULL;
683
684         if (other_ses_cache->cfg_page != ses_cache->cfg_page)
685                 ENC_FREE(ses_cache->cfg_page);
686         ses_cache->cfg_page = NULL;
687
688         ENC_DLOG(enc, "%s: exit\n", __func__);
689 }
690
691 static void
692 ses_cache_clone(enc_softc_t *enc, enc_cache_t *src, enc_cache_t *dst)
693 {
694         ses_cache_t   *dst_ses_cache;
695         ses_cache_t   *src_ses_cache;
696         enc_element_t *src_elm;
697         enc_element_t *dst_elm;
698         enc_element_t *last_elm;
699
700         ses_cache_free(enc, dst);
701         src_ses_cache = src->private;
702         dst_ses_cache = dst->private;
703
704         /*
705          * The cloned enclosure cache and ses specific cache are
706          * mostly identical to the source.
707          */
708         *dst = *src;
709         *dst_ses_cache = *src_ses_cache;
710
711         /*
712          * But the ses cache storage is still independent.  Restore
713          * the pointer that was clobbered by the structure copy above.
714          */
715         dst->private = dst_ses_cache;
716
717         /*
718          * The element map is independent even though it starts out
719          * pointing to the same constant page data.
720          */
721         dst->elm_map = malloc(dst->nelms * sizeof(enc_element_t),
722             M_SCSIENC, M_WAITOK);
723         memcpy(dst->elm_map, src->elm_map, dst->nelms * sizeof(enc_element_t));
724         for (dst_elm = dst->elm_map, src_elm = src->elm_map,
725              last_elm = &src->elm_map[src->nelms];
726              src_elm != last_elm; src_elm++, dst_elm++) {
727
728                 dst_elm->elm_private = malloc(sizeof(ses_element_t),
729                     M_SCSIENC, M_WAITOK);
730                 memcpy(dst_elm->elm_private, src_elm->elm_private,
731                        sizeof(ses_element_t));
732         }
733 }
734
735 /* Structure accessors.  These are strongly typed to avoid errors. */
736
737 int
738 ses_elm_sas_descr_type(union ses_elm_sas_hdr *obj)
739 {
740         return ((obj)->base_hdr.byte1 >> 6);
741 }
742 int
743 ses_elm_addlstatus_proto(struct ses_elm_addlstatus_base_hdr *hdr)
744 {
745         return ((hdr)->byte0 & 0xf);
746 }
747 int
748 ses_elm_addlstatus_eip(struct ses_elm_addlstatus_base_hdr *hdr)
749 {
750         return ((hdr)->byte0 >> 4) & 0x1;
751 }
752 int
753 ses_elm_addlstatus_invalid(struct ses_elm_addlstatus_base_hdr *hdr)
754 {
755         return ((hdr)->byte0 >> 7);
756 }
757 int
758 ses_elm_sas_type0_not_all_phys(union ses_elm_sas_hdr *hdr)
759 {
760         return ((hdr)->type0_noneip.byte1 & 0x1);
761 }
762 int
763 ses_elm_sas_dev_phy_sata_dev(struct ses_elm_sas_device_phy *phy)
764 {
765         return ((phy)->target_ports & 0x1);
766 }
767 int
768 ses_elm_sas_dev_phy_sata_port(struct ses_elm_sas_device_phy *phy)
769 {
770         return ((phy)->target_ports >> 7);
771 }
772 int
773 ses_elm_sas_dev_phy_dev_type(struct ses_elm_sas_device_phy *phy)
774 {
775         return (((phy)->byte0 >> 4) & 0x7);
776 }
777
778 /**
779  * \brief Verify that the cached configuration data in our softc
780  *        is valid for processing the page data corresponding to
781  *        the provided page header.
782  *
783  * \param ses_cache The SES cache to validate.
784  * \param gen_code  The 4 byte generation code from a SES diagnostic
785  *                  page header.
786  *
787  * \return  non-zero if true, 0 if false.
788  */
789 static int
790 ses_config_cache_valid(ses_cache_t *ses_cache, const uint8_t *gen_code)
791 {
792         uint32_t cache_gc;
793         uint32_t cur_gc;
794
795         if (ses_cache->cfg_page == NULL)
796                 return (0);
797
798         cache_gc = scsi_4btoul(ses_cache->cfg_page->hdr.gen_code);
799         cur_gc   = scsi_4btoul(gen_code);
800         return (cache_gc == cur_gc);
801 }
802
803 /**
804  * Function signature for consumers of the ses_devids_iter() interface.
805  */
806 typedef void ses_devid_callback_t(enc_softc_t *, enc_element_t *,
807                                   struct scsi_vpd_id_descriptor *, void *);
808
809 /**
810  * \brief Iterate over and create vpd device id records from the
811  *        additional element status data for elm, passing that data
812  *        to the provided callback.
813  *
814  * \param enc           SES instance containing elm
815  * \param elm           Element for which to extract device ID data.
816  * \param callback      The callback function to invoke on each generated
817  *                      device id descriptor for elm.
818  * \param callback_arg  Argument passed through to callback on each invocation.
819  */
820 static void
821 ses_devids_iter(enc_softc_t *enc, enc_element_t *elm,
822                 ses_devid_callback_t *callback, void *callback_arg)
823 {
824         ses_element_t           *elmpriv;
825         struct ses_addl_status *addl;
826         u_int                   i;
827         size_t                  devid_record_size;
828
829         elmpriv = elm->elm_private;
830         addl = &(elmpriv->addl);
831
832         /*
833          * Don't assume this object has additional status information, or
834          * that it is a SAS device, or that it is a device slot device.
835          */
836         if (addl->hdr == NULL || addl->proto_hdr.sas == NULL
837          || addl->proto_data.sasdev_phys == NULL)
838                 return;
839
840         devid_record_size = SVPD_DEVICE_ID_DESC_HDR_LEN
841                           + sizeof(struct scsi_vpd_id_naa_ieee_reg);
842         for (i = 0; i < addl->proto_hdr.sas->base_hdr.num_phys; i++) {
843                 uint8_t                        devid_buf[devid_record_size];
844                 struct scsi_vpd_id_descriptor *devid;
845                 uint8_t                       *phy_addr;
846
847                 devid = (struct scsi_vpd_id_descriptor *)devid_buf;
848                 phy_addr = addl->proto_data.sasdev_phys[i].phy_addr;
849                 devid->proto_codeset = (SCSI_PROTO_SAS << SVPD_ID_PROTO_SHIFT)
850                                      | SVPD_ID_CODESET_BINARY;
851                 devid->id_type       = SVPD_ID_PIV
852                                      | SVPD_ID_ASSOC_PORT
853                                      | SVPD_ID_TYPE_NAA;
854                 devid->reserved      = 0;
855                 devid->length        = sizeof(struct scsi_vpd_id_naa_ieee_reg);
856                 memcpy(devid->identifier, phy_addr, devid->length);
857
858                 callback(enc, elm, devid, callback_arg);
859         }
860 }
861
862 /**
863  * Function signature for consumers of the ses_paths_iter() interface.
864  */
865 typedef void ses_path_callback_t(enc_softc_t *, enc_element_t *,
866                                  struct cam_path *, void *);
867
868 /**
869  * Argument package passed through ses_devids_iter() by
870  * ses_paths_iter() to ses_path_iter_devid_callback().
871  */
872 typedef struct ses_path_iter_args {
873         ses_path_callback_t *callback;
874         void                *callback_arg;
875 } ses_path_iter_args_t;
876
877 /**
878  * ses_devids_iter() callback function used by ses_paths_iter()
879  * to map device ids to peripheral driver instances.
880  *
881  * \param enc     SES instance containing elm
882  * \param elm     Element on which device ID matching is active.
883  * \param periph  A device ID corresponding to elm.
884  * \param arg     Argument passed through to callback on each invocation.
885  */
886 static void
887 ses_path_iter_devid_callback(enc_softc_t *enc, enc_element_t *elem,
888                                struct scsi_vpd_id_descriptor *devid,
889                                void *arg)
890 {
891         struct ccb_dev_match         cdm;
892         struct dev_match_pattern     match_pattern;
893         struct dev_match_result      match_result;
894         struct device_match_result  *device_match;
895         struct device_match_pattern *device_pattern;
896         ses_path_iter_args_t        *args;
897
898         args = (ses_path_iter_args_t *)arg;
899         match_pattern.type = DEV_MATCH_DEVICE;
900         device_pattern = &match_pattern.pattern.device_pattern;
901         device_pattern->flags = DEV_MATCH_DEVID;
902         device_pattern->data.devid_pat.id_len = 
903             offsetof(struct scsi_vpd_id_descriptor, identifier)
904           + devid->length;
905         memcpy(device_pattern->data.devid_pat.id, devid,
906                device_pattern->data.devid_pat.id_len);
907
908         memset(&cdm, 0, sizeof(cdm));
909         if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
910                              CAM_XPT_PATH_ID,
911                              CAM_TARGET_WILDCARD,
912                              CAM_LUN_WILDCARD) != CAM_REQ_CMP)
913                 return;
914
915         cdm.ccb_h.func_code = XPT_DEV_MATCH;
916         cdm.num_patterns    = 1;
917         cdm.patterns        = &match_pattern;
918         cdm.pattern_buf_len = sizeof(match_pattern);
919         cdm.match_buf_len   = sizeof(match_result);
920         cdm.matches         = &match_result;
921
922         xpt_action((union ccb *)&cdm);
923         xpt_free_path(cdm.ccb_h.path);
924
925         if ((cdm.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
926          || (cdm.status != CAM_DEV_MATCH_LAST
927           && cdm.status != CAM_DEV_MATCH_MORE)
928          || cdm.num_matches == 0)
929                 return;
930
931         device_match = &match_result.result.device_result;
932         if (xpt_create_path(&cdm.ccb_h.path, /*periph*/NULL,
933                              device_match->path_id,
934                              device_match->target_id,
935                              device_match->target_lun) != CAM_REQ_CMP)
936                 return;
937
938         args->callback(enc, elem, cdm.ccb_h.path, args->callback_arg);
939
940         xpt_free_path(cdm.ccb_h.path);
941 }
942
943 /**
944  * \brief Iterate over and find the matching periph objects for the
945  *        specified element.
946  *
947  * \param enc           SES instance containing elm
948  * \param elm           Element for which to perform periph object matching.
949  * \param callback      The callback function to invoke with each matching
950  *                      periph object.
951  * \param callback_arg  Argument passed through to callback on each invocation.
952  */
953 static void
954 ses_paths_iter(enc_softc_t *enc, enc_element_t *elm,
955                ses_path_callback_t *callback, void *callback_arg)
956 {
957         ses_path_iter_args_t args;
958
959         args.callback     = callback;
960         args.callback_arg = callback_arg;
961         ses_devids_iter(enc, elm, ses_path_iter_devid_callback, &args);
962 }
963
964 /**
965  * ses_paths_iter() callback function used by ses_get_elmdevname()
966  * to record periph driver instance strings corresponding to a SES
967  * element.
968  *
969  * \param enc     SES instance containing elm
970  * \param elm     Element on which periph matching is active.
971  * \param periph  A periph instance that matches elm.
972  * \param arg     Argument passed through to callback on each invocation.
973  */
974 static void
975 ses_elmdevname_callback(enc_softc_t *enc, enc_element_t *elem,
976                         struct cam_path *path, void *arg)
977 {
978         struct sbuf *sb;
979
980         sb = (struct sbuf *)arg;
981         cam_periph_list(path, sb);
982 }
983
984 /**
985  * Argument package passed through ses_paths_iter() to
986  * ses_getcampath_callback.
987  */
988 typedef struct ses_setphyspath_callback_args {
989         struct sbuf *physpath;
990         int          num_set;
991 } ses_setphyspath_callback_args_t;
992
993 /**
994  * \brief ses_paths_iter() callback to set the physical path on the
995  *        CAM EDT entries corresponding to a given SES element.
996  *
997  * \param enc     SES instance containing elm
998  * \param elm     Element on which periph matching is active.
999  * \param periph  A periph instance that matches elm.
1000  * \param arg     Argument passed through to callback on each invocation.
1001  */
1002 static void
1003 ses_setphyspath_callback(enc_softc_t *enc, enc_element_t *elm,
1004                          struct cam_path *path, void *arg)
1005 {
1006         struct ccb_dev_advinfo cdai;
1007         ses_setphyspath_callback_args_t *args;
1008         char *old_physpath;
1009
1010         args = (ses_setphyspath_callback_args_t *)arg;
1011         old_physpath = malloc(MAXPATHLEN, M_SCSIENC, M_WAITOK|M_ZERO);
1012         cam_periph_lock(enc->periph);
1013         xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1014         cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1015         cdai.buftype = CDAI_TYPE_PHYS_PATH;
1016         cdai.flags = CDAI_FLAG_NONE;
1017         cdai.bufsiz = MAXPATHLEN;
1018         cdai.buf = old_physpath;
1019         xpt_action((union ccb *)&cdai);
1020         if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1021                 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1022
1023         if (strcmp(old_physpath, sbuf_data(args->physpath)) != 0) {
1024
1025                 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1026                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1027                 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1028                 cdai.flags = CDAI_FLAG_STORE;
1029                 cdai.bufsiz = sbuf_len(args->physpath);
1030                 cdai.buf = sbuf_data(args->physpath);
1031                 xpt_action((union ccb *)&cdai);
1032                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1033                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1034                 if (cdai.ccb_h.status == CAM_REQ_CMP)
1035                         args->num_set++;
1036         }
1037         cam_periph_unlock(enc->periph);
1038         free(old_physpath, M_SCSIENC);
1039 }
1040
1041 /**
1042  * \brief Set a device's physical path string in CAM XPT.
1043  *
1044  * \param enc   SES instance containing elm
1045  * \param elm   Element to publish physical path string for
1046  * \param iter  Iterator whose state corresponds to elm
1047  *
1048  * \return      0 on success, errno otherwise.
1049  */
1050 static int
1051 ses_set_physpath(enc_softc_t *enc, enc_element_t *elm,
1052                  struct ses_iterator *iter)
1053 {
1054         struct ccb_dev_advinfo cdai;
1055         ses_setphyspath_callback_args_t args;
1056         int i, ret;
1057         struct sbuf sb;
1058         struct scsi_vpd_id_descriptor *idd;
1059         uint8_t *devid;
1060         ses_element_t *elmpriv;
1061         const char *c;
1062
1063         ret = EIO;
1064         devid = NULL;
1065
1066         /*
1067          * Assemble the components of the physical path starting with
1068          * the device ID of the enclosure itself.
1069          */
1070         xpt_setup_ccb(&cdai.ccb_h, enc->periph->path, CAM_PRIORITY_NORMAL);
1071         cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1072         cdai.flags = CDAI_FLAG_NONE;
1073         cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1074         cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1075         cdai.buf = devid = malloc(cdai.bufsiz, M_SCSIENC, M_WAITOK|M_ZERO);
1076         cam_periph_lock(enc->periph);
1077         xpt_action((union ccb *)&cdai);
1078         if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1079                 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1080         cam_periph_unlock(enc->periph);
1081         if (cdai.ccb_h.status != CAM_REQ_CMP)
1082                 goto out;
1083
1084         idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1085             cdai.provsiz, scsi_devid_is_naa_ieee_reg);
1086         if (idd == NULL)
1087                 goto out;
1088
1089         if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) {
1090                 ret = ENOMEM;
1091                 goto out;
1092         }
1093         /* Next, generate the physical path string */
1094         sbuf_printf(&sb, "id1,enc@n%jx/type@%x/slot@%x",
1095             scsi_8btou64(idd->identifier), iter->type_index,
1096             iter->type_element_index);
1097         /* Append the element descriptor if one exists */
1098         elmpriv = elm->elm_private;
1099         if (elmpriv->descr != NULL && elmpriv->descr_len > 0) {
1100                 sbuf_cat(&sb, "/elmdesc@");
1101                 for (i = 0, c = elmpriv->descr; i < elmpriv->descr_len;
1102                     i++, c++) {
1103                         if (!isprint(*c) || isspace(*c) || *c == '/')
1104                                 sbuf_putc(&sb, '_');
1105                         else
1106                                 sbuf_putc(&sb, *c);
1107                 }
1108         }
1109         sbuf_finish(&sb);
1110
1111         /*
1112          * Set this physical path on any CAM devices with a device ID
1113          * descriptor that matches one created from the SES additional
1114          * status data for this element.
1115          */
1116         args.physpath= &sb;
1117         args.num_set = 0;
1118         ses_paths_iter(enc, elm, ses_setphyspath_callback, &args);
1119         sbuf_delete(&sb);
1120
1121         ret = args.num_set == 0 ? ENOENT : 0;
1122
1123 out:
1124         if (devid != NULL)
1125                 ENC_FREE(devid);
1126         return (ret);
1127 }
1128
1129 /**
1130  * \brief Helper to set the CDB fields appropriately.
1131  *
1132  * \param cdb           Buffer containing the cdb.
1133  * \param pagenum       SES diagnostic page to query for.
1134  * \param dir           Direction of query.
1135  */
1136 static void
1137 ses_page_cdb(char *cdb, int bufsiz, SesDiagPageCodes pagenum, int dir)
1138 {
1139
1140         /* Ref: SPC-4 r25 Section 6.20 Table 223 */
1141         if (dir == CAM_DIR_IN) {
1142                 cdb[0] = RECEIVE_DIAGNOSTIC;
1143                 cdb[1] = 1; /* Set page code valid bit */
1144                 cdb[2] = pagenum;
1145         } else {
1146                 cdb[0] = SEND_DIAGNOSTIC;
1147                 cdb[1] = 0x10;
1148                 cdb[2] = pagenum;
1149         }
1150         cdb[3] = bufsiz >> 8;   /* high bits */
1151         cdb[4] = bufsiz & 0xff; /* low bits */
1152         cdb[5] = 0;
1153 }
1154
1155 /**
1156  * \brief Discover whether this instance supports timed completion of a
1157  *        RECEIVE DIAGNOSTIC RESULTS command requesting the Enclosure Status
1158  *        page, and store the result in the softc, updating if necessary.
1159  *
1160  * \param enc   SES instance to query and update.
1161  * \param tc_en Value of timed completion to set (see \return).
1162  *
1163  * \return      1 if timed completion enabled, 0 otherwise.
1164  */
1165 static int
1166 ses_set_timed_completion(enc_softc_t *enc, uint8_t tc_en)
1167 {
1168         union ccb *ccb;
1169         struct cam_periph *periph;
1170         struct ses_mgmt_mode_page *mgmt;
1171         uint8_t *mode_buf;
1172         size_t mode_buf_len;
1173         ses_softc_t *ses;
1174
1175         periph = enc->periph;
1176         ses = enc->enc_private;
1177         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1178
1179         mode_buf_len = sizeof(struct ses_mgmt_mode_page);
1180         mode_buf = ENC_MALLOCZ(mode_buf_len);
1181         if (mode_buf == NULL)
1182                 goto out;
1183
1184         scsi_mode_sense(&ccb->csio, /*retries*/4, NULL, MSG_SIMPLE_Q_TAG,
1185             /*dbd*/FALSE, SMS_PAGE_CTRL_CURRENT, SES_MGMT_MODE_PAGE_CODE,
1186             mode_buf, mode_buf_len, SSD_FULL_SIZE, /*timeout*/60 * 1000);
1187
1188         /*
1189          * Ignore illegal request errors, as they are quite common and we
1190          * will print something out in that case anyway.
1191          */
1192         cam_periph_runccb(ccb, enc_error, ENC_CFLAGS,
1193             ENC_FLAGS|SF_QUIET_IR, NULL);
1194         if (ccb->ccb_h.status != CAM_REQ_CMP) {
1195                 ENC_VLOG(enc, "Timed Completion Unsupported\n");
1196                 goto release;
1197         }
1198
1199         /* Skip the mode select if the desired value is already set */
1200         mgmt = (struct ses_mgmt_mode_page *)mode_buf;
1201         if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) == tc_en)
1202                 goto done;
1203
1204         /* Value is not what we wanted, set it */
1205         if (tc_en)
1206                 mgmt->byte5 |= SES_MGMT_TIMED_COMP_EN;
1207         else
1208                 mgmt->byte5 &= ~SES_MGMT_TIMED_COMP_EN;
1209         /* SES2r20: a completion time of zero means as long as possible */
1210         bzero(&mgmt->max_comp_time, sizeof(mgmt->max_comp_time));
1211
1212         scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
1213             /*page_fmt*/FALSE, /*save_pages*/TRUE, mode_buf, mode_buf_len,
1214             SSD_FULL_SIZE, /*timeout*/60 * 1000);
1215
1216         cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
1217         if (ccb->ccb_h.status != CAM_REQ_CMP) {
1218                 ENC_VLOG(enc, "Timed Completion Set Failed\n");
1219                 goto release;
1220         }
1221
1222 done:
1223         if ((mgmt->byte5 & SES_MGMT_TIMED_COMP_EN) != 0) {
1224                 ENC_LOG(enc, "Timed Completion Enabled\n");
1225                 ses->ses_flags |= SES_FLAG_TIMEDCOMP;
1226         } else {
1227                 ENC_LOG(enc, "Timed Completion Disabled\n");
1228                 ses->ses_flags &= ~SES_FLAG_TIMEDCOMP;
1229         }
1230 release:
1231         ENC_FREE(mode_buf);
1232         xpt_release_ccb(ccb);
1233 out:
1234         return (ses->ses_flags & SES_FLAG_TIMEDCOMP);
1235 }
1236
1237 /**
1238  * \brief Process the list of supported pages and update flags.
1239  *
1240  * \param enc       SES device to query.
1241  * \param buf       Buffer containing the config page.
1242  * \param xfer_len  Length of the config page in the buffer.
1243  *
1244  * \return  0 on success, errno otherwise.
1245  */
1246 static int
1247 ses_process_pages(enc_softc_t *enc, struct enc_fsm_state *state,
1248     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1249 {
1250         ses_softc_t *ses;
1251         struct scsi_diag_page *page;
1252         int err, i, length;
1253
1254         CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1255             ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1256         ses = enc->enc_private;
1257         err = -1;
1258
1259         if (error != 0) {
1260                 err = error;
1261                 goto out;
1262         }
1263         if (xfer_len < sizeof(*page)) {
1264                 ENC_VLOG(enc, "Unable to parse Diag Pages List Header\n");
1265                 err = EIO;
1266                 goto out;
1267         }
1268         page = (struct scsi_diag_page *)*bufp;
1269         length = scsi_2btoul(page->length);
1270         if (length + offsetof(struct scsi_diag_page, params) > xfer_len) {
1271                 ENC_VLOG(enc, "Diag Pages List Too Long\n");
1272                 goto out;
1273         }
1274         ENC_DLOG(enc, "%s: page length %d, xfer_len %d\n",
1275                  __func__, length, xfer_len);
1276
1277         err = 0;
1278         for (i = 0; i < length; i++) {
1279                 if (page->params[i] == SesElementDescriptor)
1280                         ses->ses_flags |= SES_FLAG_DESC;
1281                 else if (page->params[i] == SesAddlElementStatus)
1282                         ses->ses_flags |= SES_FLAG_ADDLSTATUS;
1283         }
1284
1285 out:
1286         ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1287         return (err);
1288 }
1289
1290 /**
1291  * \brief Process the config page and update associated structures.
1292  *
1293  * \param enc       SES device to query.
1294  * \param buf       Buffer containing the config page.
1295  * \param xfer_len  Length of the config page in the buffer.
1296  *
1297  * \return  0 on success, errno otherwise.
1298  */
1299 static int
1300 ses_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
1301     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1302 {
1303         struct ses_iterator iter;
1304         ses_softc_t *ses;
1305         enc_cache_t *enc_cache;
1306         ses_cache_t *ses_cache;
1307         uint8_t *buf;
1308         int length;
1309         int err;
1310         int nelm;
1311         int ntype;
1312         struct ses_cfg_page *cfg_page;
1313         struct ses_enc_desc *buf_subenc;
1314         const struct ses_enc_desc **subencs;
1315         const struct ses_enc_desc **cur_subenc;
1316         const struct ses_enc_desc **last_subenc;
1317         ses_type_t *ses_types;
1318         ses_type_t *sestype;
1319         const struct ses_elm_type_desc *cur_buf_type;
1320         const struct ses_elm_type_desc *last_buf_type;
1321         uint8_t *last_valid_byte;
1322         enc_element_t *element;
1323         const char *type_text;
1324
1325         CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
1326             ("entering %s(%p, %d)\n", __func__, bufp, xfer_len));
1327         ses = enc->enc_private;
1328         enc_cache = &enc->enc_daemon_cache;
1329         ses_cache = enc_cache->private;
1330         buf = *bufp;
1331         err = -1;
1332
1333         if (error != 0) {
1334                 err = error;
1335                 goto out;
1336         }
1337         if (xfer_len < sizeof(cfg_page->hdr)) {
1338                 ENC_VLOG(enc, "Unable to parse SES Config Header\n");
1339                 err = EIO;
1340                 goto out;
1341         }
1342
1343         cfg_page = (struct ses_cfg_page *)buf;
1344         length = ses_page_length(&cfg_page->hdr);
1345         if (length > xfer_len) {
1346                 ENC_VLOG(enc, "Enclosure Config Page Too Long\n");
1347                 goto out;
1348         }
1349         last_valid_byte = &buf[length - 1];
1350
1351         ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1352                  __func__, length, xfer_len);
1353
1354         err = 0;
1355         if (ses_config_cache_valid(ses_cache, cfg_page->hdr.gen_code)) {
1356
1357                 /* Our cache is still valid.  Proceed to fetching status. */
1358                 goto out;
1359         }
1360
1361         /* Cache is no longer valid.  Free old data to make way for new. */
1362         ses_cache_free(enc, enc_cache);
1363         ENC_VLOG(enc, "Generation Code 0x%x has %d SubEnclosures\n",
1364             scsi_4btoul(cfg_page->hdr.gen_code),
1365             ses_cfg_page_get_num_subenc(cfg_page));
1366
1367         /* Take ownership of the buffer. */
1368         ses_cache->cfg_page = cfg_page;
1369         *bufp = NULL;
1370
1371         /*
1372          * Now waltz through all the subenclosures summing the number of
1373          * types available in each.
1374          */
1375         subencs = malloc(ses_cfg_page_get_num_subenc(cfg_page)
1376             * sizeof(*subencs), M_SCSIENC, M_WAITOK|M_ZERO);
1377         /*
1378          * Sub-enclosure data is const after construction (i.e. when
1379          * accessed via our cache object.
1380          *
1381          * The cast here is not required in C++ but C99 is not so
1382          * sophisticated (see C99 6.5.16.1(1)).
1383          */
1384         ses_cache->ses_nsubencs = ses_cfg_page_get_num_subenc(cfg_page);
1385         ses_cache->subencs = subencs;
1386
1387         buf_subenc = cfg_page->subencs;
1388         cur_subenc = subencs;
1389         last_subenc = &subencs[ses_cache->ses_nsubencs - 1];
1390         ntype = 0;
1391         while (cur_subenc <= last_subenc) {
1392
1393                 if (!ses_enc_desc_is_complete(buf_subenc, last_valid_byte)) {
1394                         ENC_VLOG(enc, "Enclosure %d Beyond End of "
1395                             "Descriptors\n", cur_subenc - subencs);
1396                         err = EIO;
1397                         goto out;
1398                 }
1399
1400                 ENC_VLOG(enc, " SubEnclosure ID %d, %d Types With this ID, "
1401                     "Descriptor Length %d, offset %d\n", buf_subenc->subenc_id,
1402                     buf_subenc->num_types, buf_subenc->length,
1403                     &buf_subenc->byte0 - buf);
1404                 ENC_VLOG(enc, "WWN: %jx\n",
1405                     (uintmax_t)scsi_8btou64(buf_subenc->logical_id));
1406
1407                 ntype += buf_subenc->num_types;
1408                 *cur_subenc = buf_subenc;
1409                 cur_subenc++;
1410                 buf_subenc = ses_enc_desc_next(buf_subenc);
1411         }
1412
1413         /* Process the type headers. */
1414         ses_types = malloc(ntype * sizeof(*ses_types),
1415             M_SCSIENC, M_WAITOK|M_ZERO);
1416         /*
1417          * Type data is const after construction (i.e. when accessed via
1418          * our cache object.
1419          */
1420         ses_cache->ses_ntypes = ntype;
1421         ses_cache->ses_types = ses_types;
1422
1423         cur_buf_type = (const struct ses_elm_type_desc *)
1424             (&(*last_subenc)->length + (*last_subenc)->length + 1);
1425         last_buf_type = cur_buf_type + ntype - 1;
1426         type_text = (const uint8_t *)(last_buf_type + 1);
1427         nelm = 0;
1428         sestype = ses_types;
1429         while (cur_buf_type <= last_buf_type) {
1430                 if (&cur_buf_type->etype_txt_len > last_valid_byte) {
1431                         ENC_VLOG(enc, "Runt Enclosure Type Header %d\n",
1432                             sestype - ses_types);
1433                         err = EIO;
1434                         goto out;
1435                 }
1436                 sestype->hdr  = cur_buf_type;
1437                 sestype->text = type_text;
1438                 type_text += cur_buf_type->etype_txt_len;
1439                 ENC_VLOG(enc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
1440                     "%d, Text Length %d: %.*s\n", sestype - ses_types,
1441                     sestype->hdr->etype_elm_type, sestype->hdr->etype_maxelt,
1442                     sestype->hdr->etype_subenc, sestype->hdr->etype_txt_len,
1443                     sestype->hdr->etype_txt_len, sestype->text);
1444
1445                 nelm += sestype->hdr->etype_maxelt
1446                       + /*overall status element*/1;
1447                 sestype++;
1448                 cur_buf_type++;
1449         }
1450
1451         /* Create the object map. */
1452         enc_cache->elm_map = malloc(nelm * sizeof(enc_element_t),
1453             M_SCSIENC, M_WAITOK|M_ZERO);
1454         enc_cache->nelms = nelm;
1455
1456         ses_iter_init(enc, enc_cache, &iter);
1457         while ((element = ses_iter_next(&iter)) != NULL) {
1458                 const struct ses_elm_type_desc *thdr;
1459
1460                 ENC_DLOG(enc, "%s: checking obj %d(%d,%d)\n", __func__,
1461                     iter.global_element_index, iter.type_index, nelm,
1462                     iter.type_element_index);
1463                 thdr = ses_cache->ses_types[iter.type_index].hdr;
1464                 element->subenclosure = thdr->etype_subenc;
1465                 element->enctype = thdr->etype_elm_type;
1466                 element->overall_status_elem = iter.type_element_index == 0;
1467                 element->elm_private = malloc(sizeof(ses_element_t),
1468                     M_SCSIENC, M_WAITOK|M_ZERO);
1469                 ENC_DLOG(enc, "%s: creating elmpriv %d(%d,%d) subenc %d "
1470                     "type 0x%x\n", __func__, iter.global_element_index,
1471                     iter.type_index, iter.type_element_index,
1472                     thdr->etype_subenc, thdr->etype_elm_type);
1473         }
1474
1475         err = 0;
1476
1477 out:
1478         if (err)
1479                 ses_cache_free(enc, enc_cache);
1480         else {
1481                 ses_poll_status(enc);
1482                 enc_update_request(enc, SES_PUBLISH_CACHE);
1483         }
1484         ENC_DLOG(enc, "%s: exiting with err %d\n", __func__, err);
1485         return (err);
1486 }
1487
1488 /**
1489  * \brief Update the status page and associated structures.
1490  * 
1491  * \param enc   SES softc to update for.
1492  * \param buf   Buffer containing the status page.
1493  * \param bufsz Amount of data in the buffer.
1494  *
1495  * \return      0 on success, errno otherwise.
1496  */
1497 static int
1498 ses_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
1499     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1500 {
1501         struct ses_iterator iter;
1502         enc_element_t *element;
1503         ses_softc_t *ses;
1504         enc_cache_t *enc_cache;
1505         ses_cache_t *ses_cache;
1506         uint8_t *buf;
1507         int err = -1;
1508         int length;
1509         struct ses_status_page *page;
1510         union ses_status_element *cur_stat;
1511         union ses_status_element *last_stat;
1512
1513         ses = enc->enc_private;
1514         enc_cache = &enc->enc_daemon_cache;
1515         ses_cache = enc_cache->private;
1516         buf = *bufp;
1517
1518         ENC_DLOG(enc, "%s: enter (%p, %p, %d)\n", __func__, enc, buf, xfer_len);
1519         page = (struct ses_status_page *)buf;
1520         length = ses_page_length(&page->hdr);
1521
1522         if (error != 0) {
1523                 err = error;
1524                 goto out;
1525         }
1526         /*
1527          * Make sure the length fits in the buffer.
1528          *
1529          * XXX all this means is that the page is larger than the space
1530          * we allocated.  Since we use a statically sized buffer, this
1531          * could happen... Need to use dynamic discovery of the size.
1532          */
1533         if (length > xfer_len) {
1534                 ENC_VLOG(enc, "Enclosure Status Page Too Long\n");
1535                 goto out;
1536         }
1537
1538         /* Check for simple enclosure reporting short enclosure status. */
1539         if (length >= 4 && page->hdr.page_code == SesShortStatus) {
1540                 ENC_DLOG(enc, "Got Short Enclosure Status page\n");
1541                 ses->ses_flags &= ~(SES_FLAG_ADDLSTATUS | SES_FLAG_DESC);
1542                 ses_cache_free(enc, enc_cache);
1543                 enc_cache->enc_status = page->hdr.page_specific_flags;
1544                 enc_update_request(enc, SES_PUBLISH_CACHE);
1545                 err = 0;
1546                 goto out;
1547         }
1548
1549         /* Make sure the length contains at least one header and status */
1550         if (length < (sizeof(*page) + sizeof(*page->elements))) {
1551                 ENC_VLOG(enc, "Enclosure Status Page Too Short\n");
1552                 goto out;
1553         }
1554
1555         if (!ses_config_cache_valid(ses_cache, page->hdr.gen_code)) {
1556                 ENC_DLOG(enc, "%s: Generation count change detected\n",
1557                     __func__);
1558                 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1559                 goto out;
1560         }
1561
1562         ses_cache_free_status(enc, enc_cache);
1563         ses_cache->status_page = page;
1564         *bufp = NULL;
1565
1566         enc_cache->enc_status = page->hdr.page_specific_flags;
1567
1568         /*
1569          * Read in individual element status.  The element order
1570          * matches the order reported in the config page (i.e. the
1571          * order of an unfiltered iteration of the config objects)..
1572          */
1573         ses_iter_init(enc, enc_cache, &iter);
1574         cur_stat  = page->elements;
1575         last_stat = (union ses_status_element *)
1576             &buf[length - sizeof(*last_stat)];
1577         ENC_DLOG(enc, "%s: total page length %d, xfer_len %d\n",
1578                 __func__, length, xfer_len);
1579         while (cur_stat <= last_stat
1580             && (element = ses_iter_next(&iter)) != NULL) {
1581
1582                 ENC_DLOG(enc, "%s: obj %d(%d,%d) off=0x%tx status=%jx\n",
1583                     __func__, iter.global_element_index, iter.type_index,
1584                     iter.type_element_index, (uint8_t *)cur_stat - buf,
1585                     scsi_4btoul(cur_stat->bytes));
1586
1587                 memcpy(&element->encstat, cur_stat, sizeof(element->encstat));
1588                 element->svalid = 1;
1589                 cur_stat++;
1590         }
1591
1592         if (ses_iter_next(&iter) != NULL) {
1593                 ENC_VLOG(enc, "Status page, length insufficient for "
1594                         "expected number of objects\n");
1595         } else {
1596                 if (cur_stat <= last_stat)
1597                         ENC_VLOG(enc, "Status page, exhausted objects before "
1598                                 "exhausing page\n");
1599                 enc_update_request(enc, SES_PUBLISH_CACHE);
1600                 err = 0;
1601         }
1602 out:
1603         ENC_DLOG(enc, "%s: exiting with error %d\n", __func__, err);
1604         return (err);
1605 }
1606
1607 typedef enum {
1608         /**
1609          * The enclosure should not provide additional element
1610          * status for this element type in page 0x0A.
1611          *
1612          * \note  This status is returned for any types not
1613          *        listed SES3r02.  Further types added in a
1614          *        future specification will be incorrectly
1615          *        classified.
1616          */
1617         TYPE_ADDLSTATUS_NONE,
1618
1619         /**
1620          * The element type provides additional element status
1621          * in page 0x0A.
1622          */
1623         TYPE_ADDLSTATUS_MANDATORY,
1624
1625         /**
1626          * The element type may provide additional element status
1627          * in page 0x0A, but i
1628          */
1629         TYPE_ADDLSTATUS_OPTIONAL
1630 } ses_addlstatus_avail_t;
1631
1632 /**
1633  * \brief Check to see whether a given type (as obtained via type headers) is
1634  *        supported by the additional status command.
1635  *
1636  * \param enc     SES softc to check.
1637  * \param typidx  Type index to check for.
1638  *
1639  * \return  An enumeration indicating if additional status is mandatory,
1640  *          optional, or not required for this type.
1641  */
1642 static ses_addlstatus_avail_t
1643 ses_typehasaddlstatus(enc_softc_t *enc, uint8_t typidx)
1644 {
1645         enc_cache_t *enc_cache;
1646         ses_cache_t *ses_cache;
1647
1648         enc_cache = &enc->enc_daemon_cache;
1649         ses_cache = enc_cache->private;
1650         switch(ses_cache->ses_types[typidx].hdr->etype_elm_type) {
1651         case ELMTYP_DEVICE:
1652         case ELMTYP_ARRAY_DEV:
1653         case ELMTYP_SAS_EXP:
1654                 return (TYPE_ADDLSTATUS_MANDATORY);
1655         case ELMTYP_SCSI_INI:
1656         case ELMTYP_SCSI_TGT:
1657         case ELMTYP_ESCC:
1658                 return (TYPE_ADDLSTATUS_OPTIONAL);
1659         default:
1660                 /* No additional status information available. */
1661                 break;
1662         }
1663         return (TYPE_ADDLSTATUS_NONE);
1664 }
1665
1666 static int ses_get_elm_addlstatus_fc(enc_softc_t *, enc_cache_t *,
1667                                      uint8_t *, int);
1668 static int ses_get_elm_addlstatus_sas(enc_softc_t *, enc_cache_t *, uint8_t *,
1669                                       int, int, int, int);
1670
1671 /**
1672  * \brief Parse the additional status element data for each object.
1673  *
1674  * \param enc       The SES softc to update.
1675  * \param buf       The buffer containing the additional status
1676  *                  element response.
1677  * \param xfer_len  Size of the buffer.
1678  *
1679  * \return  0 on success, errno otherwise.
1680  */
1681 static int
1682 ses_process_elm_addlstatus(enc_softc_t *enc, struct enc_fsm_state *state,
1683     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1684 {
1685         struct ses_iterator iter, titer;
1686         int eip;
1687         int err;
1688         int ignore_index = 0;
1689         int length;
1690         int offset;
1691         enc_cache_t *enc_cache;
1692         ses_cache_t *ses_cache;
1693         uint8_t *buf;
1694         ses_element_t *elmpriv;
1695         const struct ses_page_hdr *hdr;
1696         enc_element_t *element, *telement;
1697
1698         enc_cache = &enc->enc_daemon_cache;
1699         ses_cache = enc_cache->private;
1700         buf = *bufp;
1701         err = -1;
1702
1703         if (error != 0) {
1704                 err = error;
1705                 goto out;
1706         }
1707         ses_cache_free_elm_addlstatus(enc, enc_cache);
1708         ses_cache->elm_addlstatus_page =
1709             (struct ses_addl_elem_status_page *)buf;
1710         *bufp = NULL;
1711
1712         /*
1713          * The objects appear in the same order here as in Enclosure Status,
1714          * which itself is ordered by the Type Descriptors from the Config
1715          * page.  However, it is necessary to skip elements that are not
1716          * supported by this page when counting them.
1717          */
1718         hdr = &ses_cache->elm_addlstatus_page->hdr;
1719         length = ses_page_length(hdr);
1720         ENC_DLOG(enc, "Additional Element Status Page Length 0x%x\n", length);
1721         /* Make sure the length includes at least one header. */
1722         if (length < sizeof(*hdr)+sizeof(struct ses_elm_addlstatus_base_hdr)) {
1723                 ENC_VLOG(enc, "Runt Additional Element Status Page\n");
1724                 goto out;
1725         }
1726         if (length > xfer_len) {
1727                 ENC_VLOG(enc, "Additional Element Status Page Too Long\n");
1728                 goto out;
1729         }
1730
1731         if (!ses_config_cache_valid(ses_cache, hdr->gen_code)) {
1732                 ENC_DLOG(enc, "%s: Generation count change detected\n",
1733                     __func__);
1734                 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1735                 goto out;
1736         }
1737
1738         offset = sizeof(struct ses_page_hdr);
1739         ses_iter_init(enc, enc_cache, &iter);
1740         while (offset < length
1741             && (element = ses_iter_next(&iter)) != NULL) {
1742                 struct ses_elm_addlstatus_base_hdr *elm_hdr;
1743                 int proto_info_len;
1744                 ses_addlstatus_avail_t status_type;
1745
1746                 /*
1747                  * Additional element status is only provided for
1748                  * individual elements (i.e. overal status elements
1749                  * are excluded) and those of the types specified
1750                  * in the SES spec.
1751                  */
1752                 status_type = ses_typehasaddlstatus(enc, iter.type_index);
1753                 if (iter.individual_element_index == ITERATOR_INDEX_INVALID
1754                  || status_type == TYPE_ADDLSTATUS_NONE)
1755                         continue;
1756
1757                 elm_hdr = (struct ses_elm_addlstatus_base_hdr *)&buf[offset];
1758                 eip = ses_elm_addlstatus_eip(elm_hdr);
1759                 if (eip && !ignore_index) {
1760                         struct ses_elm_addlstatus_eip_hdr *eip_hdr;
1761                         int expected_index, index;
1762                         ses_elem_index_type_t index_type;
1763
1764                         eip_hdr = (struct ses_elm_addlstatus_eip_hdr *)elm_hdr;
1765                         if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE) {
1766                                 index_type = SES_ELEM_INDEX_GLOBAL;
1767                                 expected_index = iter.global_element_index;
1768                         } else {
1769                                 index_type = SES_ELEM_INDEX_INDIVIDUAL;
1770                                 expected_index = iter.individual_element_index;
1771                         }
1772                         titer = iter;
1773                         telement = ses_iter_seek_to(&titer,
1774                             eip_hdr->element_index, index_type);
1775                         if (telement != NULL &&
1776                             (ses_typehasaddlstatus(enc, titer.type_index) !=
1777                              TYPE_ADDLSTATUS_NONE ||
1778                              titer.type_index > ELMTYP_SAS_CONN)) {
1779                                 iter = titer;
1780                                 element = telement;
1781                         } else
1782                                 ignore_index = 1;
1783
1784                         if (eip_hdr->byte2 & SES_ADDL_EIP_EIIOE)
1785                                 index = iter.global_element_index;
1786                         else
1787                                 index = iter.individual_element_index;
1788                         if (index > expected_index
1789                          && status_type == TYPE_ADDLSTATUS_MANDATORY) {
1790                                 ENC_VLOG(enc, "%s: provided %s element"
1791                                         "index %d skips mandatory status "
1792                                         " element at index %d\n",
1793                                         __func__, (eip_hdr->byte2 &
1794                                         SES_ADDL_EIP_EIIOE) ? "global " : "",
1795                                         index, expected_index);
1796                         }
1797                 }
1798                 elmpriv = element->elm_private;
1799                 elmpriv->addl.hdr = elm_hdr;
1800                 ENC_DLOG(enc, "%s: global element index=%d, type index=%d "
1801                     "type element index=%d, offset=0x%x, "
1802                     "byte0=0x%x, length=0x%x\n", __func__,
1803                     iter.global_element_index, iter.type_index,
1804                     iter.type_element_index, offset, elmpriv->addl.hdr->byte0,
1805                     elmpriv->addl.hdr->length);
1806
1807                 /* Skip to after the length field */
1808                 offset += sizeof(struct ses_elm_addlstatus_base_hdr);
1809
1810                 /* Make sure the descriptor is within bounds */
1811                 if ((offset + elmpriv->addl.hdr->length) > length) {
1812                         ENC_VLOG(enc, "Element %d Beyond End "
1813                             "of Additional Element Status Descriptors\n",
1814                             iter.global_element_index);
1815                         break;
1816                 }
1817
1818                 /* Advance to the protocol data, skipping eip bytes if needed */
1819                 offset += (eip * SES_EIP_HDR_EXTRA_LEN);
1820                 proto_info_len = elmpriv->addl.hdr->length
1821                                - (eip * SES_EIP_HDR_EXTRA_LEN);
1822
1823                 /* Errors in this block are ignored as they are non-fatal */
1824                 switch(ses_elm_addlstatus_proto(elmpriv->addl.hdr)) {
1825                 case SPSP_PROTO_FC:
1826                         if (elmpriv->addl.hdr->length == 0)
1827                                 break;
1828                         ses_get_elm_addlstatus_fc(enc, enc_cache,
1829                                                   &buf[offset], proto_info_len);
1830                         break;
1831                 case SPSP_PROTO_SAS:
1832                         if (elmpriv->addl.hdr->length <= 2)
1833                                 break;
1834                         ses_get_elm_addlstatus_sas(enc, enc_cache,
1835                                                    &buf[offset],
1836                                                    proto_info_len,
1837                                                    eip, iter.type_index,
1838                                                    iter.global_element_index);
1839                         break;
1840                 default:
1841                         ENC_VLOG(enc, "Element %d: Unknown Additional Element "
1842                             "Protocol 0x%x\n", iter.global_element_index,
1843                             ses_elm_addlstatus_proto(elmpriv->addl.hdr));
1844                         break;
1845                 }
1846
1847                 offset += proto_info_len;
1848         }
1849         err = 0;
1850 out:
1851         if (err)
1852                 ses_cache_free_elm_addlstatus(enc, enc_cache);
1853         enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
1854         enc_update_request(enc, SES_PUBLISH_CACHE);
1855         return (err);
1856 }
1857
1858 static int
1859 ses_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
1860     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1861 {
1862         ses_softc_t *ses;
1863
1864         ses = enc->enc_private;
1865         /*
1866          * Possible errors:
1867          *  o Generation count wrong.
1868          *  o Some SCSI status error.
1869          */
1870         ses_terminate_control_requests(&ses->ses_pending_requests, error);
1871         ses_poll_status(enc);
1872         return (0);
1873 }
1874
1875 static int
1876 ses_publish_physpaths(enc_softc_t *enc, struct enc_fsm_state *state,
1877     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1878 {
1879         struct ses_iterator iter;
1880         enc_cache_t *enc_cache;
1881         enc_element_t *element;
1882
1883         enc_cache = &enc->enc_daemon_cache;
1884
1885         ses_iter_init(enc, enc_cache, &iter);
1886         while ((element = ses_iter_next(&iter)) != NULL) {
1887                 /*
1888                  * ses_set_physpath() returns success if we changed
1889                  * the physpath of any element.  This allows us to
1890                  * only announce devices once regardless of how
1891                  * many times we process additional element status.
1892                  */
1893                 if (ses_set_physpath(enc, element, &iter) == 0)
1894                         ses_print_addl_data(enc, element);
1895         }
1896
1897         return (0);
1898 }
1899
1900 static int
1901 ses_publish_cache(enc_softc_t *enc, struct enc_fsm_state *state,
1902     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1903 {
1904
1905         sx_xlock(&enc->enc_cache_lock);
1906         ses_cache_clone(enc, /*src*/&enc->enc_daemon_cache,
1907                         /*dst*/&enc->enc_cache);
1908         sx_xunlock(&enc->enc_cache_lock);
1909
1910         return (0);
1911 }
1912
1913 /**
1914  * \brief Parse the descriptors for each object.
1915  *
1916  * \param enc       The SES softc to update.
1917  * \param buf       The buffer containing the descriptor list response.
1918  * \param xfer_len  Size of the buffer.
1919  * 
1920  * \return      0 on success, errno otherwise.
1921  */
1922 static int
1923 ses_process_elm_descs(enc_softc_t *enc, struct enc_fsm_state *state,
1924     union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
1925 {
1926         ses_softc_t *ses;
1927         struct ses_iterator iter;
1928         enc_element_t *element;
1929         int err;
1930         int offset;
1931         u_long length, plength;
1932         enc_cache_t *enc_cache;
1933         ses_cache_t *ses_cache;
1934         uint8_t *buf;
1935         ses_element_t *elmpriv;
1936         const struct ses_page_hdr *phdr;
1937         const struct ses_elm_desc_hdr *hdr;
1938
1939         ses = enc->enc_private;
1940         enc_cache = &enc->enc_daemon_cache;
1941         ses_cache = enc_cache->private;
1942         buf = *bufp;
1943         err = -1;
1944
1945         if (error != 0) {
1946                 err = error;
1947                 goto out;
1948         }
1949         ses_cache_free_elm_descs(enc, enc_cache);
1950         ses_cache->elm_descs_page = (struct ses_elem_descr_page *)buf;
1951         *bufp = NULL;
1952
1953         phdr = &ses_cache->elm_descs_page->hdr;
1954         plength = ses_page_length(phdr);
1955         if (xfer_len < sizeof(struct ses_page_hdr)) {
1956                 ENC_VLOG(enc, "Runt Element Descriptor Page\n");
1957                 goto out;
1958         }
1959         if (plength > xfer_len) {
1960                 ENC_VLOG(enc, "Element Descriptor Page Too Long\n");
1961                 goto out;
1962         }
1963
1964         if (!ses_config_cache_valid(ses_cache, phdr->gen_code)) {
1965                 ENC_VLOG(enc, "%s: Generation count change detected\n",
1966                     __func__);
1967                 enc_update_request(enc, SES_UPDATE_GETCONFIG);
1968                 goto out;
1969         }
1970
1971         offset = sizeof(struct ses_page_hdr);
1972
1973         ses_iter_init(enc, enc_cache, &iter);
1974         while (offset < plength
1975             && (element = ses_iter_next(&iter)) != NULL) {
1976
1977                 if ((offset + sizeof(struct ses_elm_desc_hdr)) > plength) {
1978                         ENC_VLOG(enc, "Element %d Descriptor Header Past "
1979                             "End of Buffer\n", iter.global_element_index);
1980                         goto out;
1981                 }
1982                 hdr = (struct ses_elm_desc_hdr *)&buf[offset];
1983                 length = scsi_2btoul(hdr->length);
1984                 ENC_DLOG(enc, "%s: obj %d(%d,%d) length=%d off=%d\n", __func__,
1985                     iter.global_element_index, iter.type_index,
1986                     iter.type_element_index, length, offset);
1987                 if ((offset + sizeof(*hdr) + length) > plength) {
1988                         ENC_VLOG(enc, "Element%d Descriptor Past "
1989                             "End of Buffer\n", iter.global_element_index);
1990                         goto out;
1991                 }
1992                 offset += sizeof(*hdr);
1993
1994                 if (length > 0) {
1995                         elmpriv = element->elm_private;
1996                         elmpriv->descr_len = length;
1997                         elmpriv->descr = &buf[offset];
1998                 }
1999
2000                 /* skip over the descriptor itself */
2001                 offset += length;
2002         }
2003
2004         err = 0;
2005 out:
2006         if (err == 0) {
2007                 if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2008                         enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2009         }
2010         enc_update_request(enc, SES_PUBLISH_CACHE);
2011         return (err);
2012 }
2013
2014 static int
2015 ses_fill_rcv_diag_io(enc_softc_t *enc, struct enc_fsm_state *state,
2016                        union ccb *ccb, uint8_t *buf)
2017 {
2018
2019         if (enc->enc_type == ENC_SEMB_SES) {
2020                 semb_receive_diagnostic_results(&ccb->ataio, /*retries*/5,
2021                                         NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2022                                         state->page_code, buf, state->buf_size,
2023                                         state->timeout);
2024         } else {
2025                 scsi_receive_diagnostic_results(&ccb->csio, /*retries*/5,
2026                                         NULL, MSG_SIMPLE_Q_TAG, /*pcv*/1,
2027                                         state->page_code, buf, state->buf_size,
2028                                         SSD_FULL_SIZE, state->timeout);
2029         }
2030         return (0);
2031 }
2032
2033 /**
2034  * \brief Encode the object status into the response buffer, which is
2035  *        expected to contain the current enclosure status.  This function
2036  *        turns off all the 'select' bits for the objects except for the
2037  *        object specified, then sends it back to the enclosure.
2038  *
2039  * \param enc   SES enclosure the change is being applied to.
2040  * \param buf   Buffer containing the current enclosure status response.
2041  * \param amt   Length of the response in the buffer.
2042  * \param req   The control request to be applied to buf.
2043  *
2044  * \return      0 on success, errno otherwise.
2045  */
2046 static int
2047 ses_encode(enc_softc_t *enc, uint8_t *buf, int amt, ses_control_request_t *req)
2048 {
2049         struct ses_iterator iter;
2050         enc_element_t *element;
2051         int offset;
2052         struct ses_control_page_hdr *hdr;
2053
2054         ses_iter_init(enc, &enc->enc_cache, &iter);
2055         hdr = (struct ses_control_page_hdr *)buf;
2056         if (req->elm_idx == -1) {
2057                 /* for enclosure status, at least 2 bytes are needed */
2058                 if (amt < 2)
2059                         return EIO;
2060                 hdr->control_flags =
2061                     req->elm_stat.comstatus & SES_SET_STATUS_MASK;
2062                 ENC_DLOG(enc, "Set EncStat %x\n", hdr->control_flags);
2063                 return (0);
2064         }
2065
2066         element = ses_iter_seek_to(&iter, req->elm_idx, SES_ELEM_INDEX_GLOBAL);
2067         if (element == NULL)
2068                 return (ENXIO);
2069
2070         /*
2071          * Seek to the type set that corresponds to the requested object.
2072          * The +1 is for the overall status element for the type.
2073          */
2074         offset = sizeof(struct ses_control_page_hdr)
2075                + (iter.global_element_index * sizeof(struct ses_comstat));
2076
2077         /* Check for buffer overflow. */
2078         if (offset + sizeof(struct ses_comstat) > amt)
2079                 return (EIO);
2080
2081         /* Set the status. */
2082         memcpy(&buf[offset], &req->elm_stat, sizeof(struct ses_comstat));
2083
2084         ENC_DLOG(enc, "Set Type 0x%x Obj 0x%x (offset %d) with %x %x %x %x\n",
2085             iter.type_index, iter.global_element_index, offset,
2086             req->elm_stat.comstatus, req->elm_stat.comstat[0],
2087             req->elm_stat.comstat[1], req->elm_stat.comstat[2]);
2088
2089         return (0);
2090 }
2091
2092 static int
2093 ses_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
2094                          union ccb *ccb, uint8_t *buf)
2095 {
2096         ses_softc_t                     *ses;
2097         enc_cache_t                     *enc_cache;
2098         ses_cache_t                     *ses_cache;
2099         struct ses_control_page_hdr     *hdr;
2100         ses_control_request_t           *req;
2101         size_t                           plength;
2102         size_t                           offset;
2103
2104         ses = enc->enc_private;
2105         enc_cache = &enc->enc_daemon_cache;
2106         ses_cache = enc_cache->private;
2107         hdr = (struct ses_control_page_hdr *)buf;
2108         
2109         if (ses_cache->status_page == NULL) {
2110                 ses_terminate_control_requests(&ses->ses_requests, EIO);
2111                 return (EIO);
2112         }
2113
2114         plength = ses_page_length(&ses_cache->status_page->hdr);
2115         memcpy(buf, ses_cache->status_page, plength);
2116
2117         /* Disable the select bits in all status entries.  */
2118         offset = sizeof(struct ses_control_page_hdr);
2119         for (offset = sizeof(struct ses_control_page_hdr);
2120              offset < plength; offset += sizeof(struct ses_comstat)) {
2121                 buf[offset] &= ~SESCTL_CSEL;
2122         }
2123
2124         /* And make sure the INVOP bit is clear.  */
2125         hdr->control_flags &= ~SES_ENCSTAT_INVOP;
2126
2127         /* Apply incoming requests. */
2128         while ((req = TAILQ_FIRST(&ses->ses_requests)) != NULL) {
2129
2130                 TAILQ_REMOVE(&ses->ses_requests, req, links);
2131                 req->result = ses_encode(enc, buf, plength, req);
2132                 if (req->result != 0) {
2133                         wakeup(req);
2134                         continue;
2135                 }
2136                 TAILQ_INSERT_TAIL(&ses->ses_pending_requests, req, links);
2137         }
2138
2139         if (TAILQ_EMPTY(&ses->ses_pending_requests) != 0)
2140                 return (ENOENT);
2141
2142         /* Fill out the ccb */
2143         if (enc->enc_type == ENC_SEMB_SES) {
2144                 semb_send_diagnostic(&ccb->ataio, /*retries*/5, NULL,
2145                              MSG_SIMPLE_Q_TAG,
2146                              buf, ses_page_length(&ses_cache->status_page->hdr),
2147                              state->timeout);
2148         } else {
2149                 scsi_send_diagnostic(&ccb->csio, /*retries*/5, NULL,
2150                              MSG_SIMPLE_Q_TAG, /*unit_offline*/0,
2151                              /*device_offline*/0, /*self_test*/0,
2152                              /*page_format*/1, /*self_test_code*/0,
2153                              buf, ses_page_length(&ses_cache->status_page->hdr),
2154                              SSD_FULL_SIZE, state->timeout);
2155         }
2156         return (0);
2157 }
2158
2159 static int
2160 ses_get_elm_addlstatus_fc(enc_softc_t *enc, enc_cache_t *enc_cache,
2161                           uint8_t *buf, int bufsiz)
2162 {
2163         ENC_VLOG(enc, "FC Device Support Stubbed in Additional Status Page\n");
2164         return (ENODEV);
2165 }
2166
2167 #define SES_PRINT_PORTS(p, type) do {                                   \
2168         sbuf_printf(sbp, " %s(", type);                                 \
2169         if (((p) & SES_SASOBJ_DEV_PHY_PROTOMASK) == 0)                  \
2170                 sbuf_printf(sbp, " None");                              \
2171         else {                                                          \
2172                 if ((p) & SES_SASOBJ_DEV_PHY_SMP)                       \
2173                         sbuf_printf(sbp, " SMP");                       \
2174                 if ((p) & SES_SASOBJ_DEV_PHY_STP)                       \
2175                         sbuf_printf(sbp, " STP");                       \
2176                 if ((p) & SES_SASOBJ_DEV_PHY_SSP)                       \
2177                         sbuf_printf(sbp, " SSP");                       \
2178         }                                                               \
2179         sbuf_printf(sbp, " )");                                         \
2180 } while(0)
2181
2182 /**
2183  * \brief Print the additional element status data for this object, for SAS
2184  *        type 0 objects.  See SES2 r20 Section 6.1.13.3.2.
2185  *
2186  * \param sesname       SES device name associated with the object.
2187  * \param sbp           Sbuf to print to.
2188  * \param obj           The object to print the data for.
2189  * \param periph_name   Peripheral string associated with the object.
2190  */
2191 static void
2192 ses_print_addl_data_sas_type0(char *sesname, struct sbuf *sbp,
2193                               enc_element_t *obj, char *periph_name)
2194 {
2195         int i;
2196         ses_element_t *elmpriv;
2197         struct ses_addl_status *addl;
2198         struct ses_elm_sas_device_phy *phy;
2199
2200         elmpriv = obj->elm_private;
2201         addl = &(elmpriv->addl);
2202         if (addl->proto_hdr.sas == NULL)
2203                 return;
2204         sbuf_printf(sbp, "%s: %s: SAS Device Slot Element:",
2205             sesname, periph_name);
2206         sbuf_printf(sbp, " %d Phys", addl->proto_hdr.sas->base_hdr.num_phys);
2207         if (ses_elm_addlstatus_eip(addl->hdr))
2208                 sbuf_printf(sbp, " at Slot %d",
2209                     addl->proto_hdr.sas->type0_eip.dev_slot_num);
2210         if (ses_elm_sas_type0_not_all_phys(addl->proto_hdr.sas))
2211                 sbuf_printf(sbp, ", Not All Phys");
2212         sbuf_printf(sbp, "\n");
2213         if (addl->proto_data.sasdev_phys == NULL)
2214                 return;
2215         for (i = 0;i < addl->proto_hdr.sas->base_hdr.num_phys;i++) {
2216                 phy = &addl->proto_data.sasdev_phys[i];
2217                 sbuf_printf(sbp, "%s:  phy %d:", sesname, i);
2218                 if (ses_elm_sas_dev_phy_sata_dev(phy))
2219                         /* Spec says all other fields are specific values */
2220                         sbuf_printf(sbp, " SATA device\n");
2221                 else {
2222                         sbuf_printf(sbp, " SAS device type %d id %d\n",
2223                             ses_elm_sas_dev_phy_dev_type(phy), phy->phy_id);
2224                         sbuf_printf(sbp, "%s:  phy %d: protocols:", sesname, i);
2225                         SES_PRINT_PORTS(phy->initiator_ports, "Initiator");
2226                         SES_PRINT_PORTS(phy->target_ports, "Target");
2227                         sbuf_printf(sbp, "\n");
2228                 }
2229                 sbuf_printf(sbp, "%s:  phy %d: parent %jx addr %jx\n",
2230                     sesname, i,
2231                     (uintmax_t)scsi_8btou64(phy->parent_addr),
2232                     (uintmax_t)scsi_8btou64(phy->phy_addr));
2233         }
2234 }
2235 #undef SES_PRINT_PORTS
2236
2237 /**
2238  * \brief Report whether a given enclosure object is an expander.
2239  *
2240  * \param enc   SES softc associated with object.
2241  * \param obj   Enclosure object to report for.
2242  *
2243  * \return      1 if true, 0 otherwise.
2244  */
2245 static int
2246 ses_obj_is_expander(enc_softc_t *enc, enc_element_t *obj)
2247 {
2248         return (obj->enctype == ELMTYP_SAS_EXP);
2249 }
2250
2251 /**
2252  * \brief Print the additional element status data for this object, for SAS
2253  *        type 1 objects.  See SES2 r20 Sections 6.1.13.3.3 and 6.1.13.3.4.
2254  *
2255  * \param enc           SES enclosure, needed for type identification.
2256  * \param sesname       SES device name associated with the object.
2257  * \param sbp           Sbuf to print to.
2258  * \param obj           The object to print the data for.
2259  * \param periph_name   Peripheral string associated with the object.
2260  */
2261 static void
2262 ses_print_addl_data_sas_type1(enc_softc_t *enc, char *sesname,
2263     struct sbuf *sbp, enc_element_t *obj, char *periph_name)
2264 {
2265         int i, num_phys;
2266         ses_element_t *elmpriv;
2267         struct ses_addl_status *addl;
2268         struct ses_elm_sas_expander_phy *exp_phy;
2269         struct ses_elm_sas_port_phy *port_phy;
2270
2271         elmpriv = obj->elm_private;
2272         addl = &(elmpriv->addl);
2273         if (addl->proto_hdr.sas == NULL)
2274                 return;
2275         sbuf_printf(sbp, "%s: %s: SAS ", sesname, periph_name);
2276         if (ses_obj_is_expander(enc, obj)) {
2277                 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2278                 sbuf_printf(sbp, "Expander: %d Phys", num_phys);
2279                 if (addl->proto_data.sasexp_phys == NULL)
2280                         return;
2281                 for (i = 0;i < num_phys;i++) {
2282                         exp_phy = &addl->proto_data.sasexp_phys[i];
2283                         sbuf_printf(sbp, "%s:  phy %d: connector %d other %d\n",
2284                             sesname, i, exp_phy->connector_index,
2285                             exp_phy->other_index);
2286                 }
2287         } else {
2288                 num_phys = addl->proto_hdr.sas->base_hdr.num_phys;
2289                 sbuf_printf(sbp, "Port: %d Phys", num_phys);
2290                 if (addl->proto_data.sasport_phys == NULL)
2291                         return;
2292                 for (i = 0;i < num_phys;i++) {
2293                         port_phy = &addl->proto_data.sasport_phys[i];
2294                         sbuf_printf(sbp,
2295                             "%s:  phy %d: id %d connector %d other %d\n",
2296                             sesname, i, port_phy->phy_id,
2297                             port_phy->connector_index, port_phy->other_index);
2298                         sbuf_printf(sbp, "%s:  phy %d: addr %jx\n", sesname, i,
2299                             (uintmax_t)scsi_8btou64(port_phy->phy_addr));
2300                 }
2301         }
2302 }
2303
2304 /**
2305  * \brief Print the additional element status data for this object.
2306  *
2307  * \param enc           SES softc associated with the object.
2308  * \param obj           The object to print the data for.
2309  */
2310 static void
2311 ses_print_addl_data(enc_softc_t *enc, enc_element_t *obj)
2312 {
2313         ses_element_t *elmpriv;
2314         struct ses_addl_status *addl;
2315         struct sbuf sesname, name, out;
2316
2317         elmpriv = obj->elm_private;
2318         if (elmpriv == NULL)
2319                 return;
2320
2321         addl = &(elmpriv->addl);
2322         if (addl->hdr == NULL)
2323                 return;
2324
2325         sbuf_new(&sesname, NULL, 16, SBUF_AUTOEXTEND);
2326         sbuf_new(&name, NULL, 16, SBUF_AUTOEXTEND);
2327         sbuf_new(&out, NULL, 512, SBUF_AUTOEXTEND);
2328         ses_paths_iter(enc, obj, ses_elmdevname_callback, &name);
2329         if (sbuf_len(&name) == 0)
2330                 sbuf_printf(&name, "(none)");
2331         sbuf_finish(&name);
2332         sbuf_printf(&sesname, "%s%d", enc->periph->periph_name,
2333             enc->periph->unit_number);
2334         sbuf_finish(&sesname);
2335         if (elmpriv->descr != NULL)
2336                 sbuf_printf(&out, "%s: %s: Element descriptor: '%s'\n",
2337                     sbuf_data(&sesname), sbuf_data(&name), elmpriv->descr);
2338         switch(ses_elm_addlstatus_proto(addl->hdr)) {
2339         case SPSP_PROTO_SAS:
2340                 switch(ses_elm_sas_descr_type(addl->proto_hdr.sas)) {
2341                 case SES_SASOBJ_TYPE_SLOT:
2342                         ses_print_addl_data_sas_type0(sbuf_data(&sesname),
2343                             &out, obj, sbuf_data(&name));
2344                         break;
2345                 case SES_SASOBJ_TYPE_OTHER:
2346                         ses_print_addl_data_sas_type1(enc, sbuf_data(&sesname),
2347                             &out, obj, sbuf_data(&name));
2348                         break;
2349                 default:
2350                         break;
2351                 }
2352                 break;
2353         case SPSP_PROTO_FC:     /* stubbed for now */
2354                 break;
2355         default:
2356                 break;
2357         }
2358         sbuf_finish(&out);
2359         printf("%s", sbuf_data(&out));
2360         sbuf_delete(&out);
2361         sbuf_delete(&name);
2362         sbuf_delete(&sesname);
2363 }
2364
2365 /**
2366  * \brief Update the softc with the additional element status data for this
2367  *        object, for SAS type 0 objects.
2368  *
2369  * \param enc           SES softc to be updated.
2370  * \param buf           The additional element status response buffer.
2371  * \param bufsiz        Size of the response buffer.
2372  * \param eip           The EIP bit value.
2373  * \param nobj          Number of objects attached to the SES softc.
2374  * 
2375  * \return              0 on success, errno otherwise.
2376  */
2377 static int
2378 ses_get_elm_addlstatus_sas_type0(enc_softc_t *enc, enc_cache_t *enc_cache,
2379                                  uint8_t *buf, int bufsiz, int eip, int nobj)
2380 {
2381         int err, offset, physz;
2382         enc_element_t *obj;
2383         ses_element_t *elmpriv;
2384         struct ses_addl_status *addl;
2385
2386         err = offset = 0;
2387
2388         /* basic object setup */
2389         obj = &(enc_cache->elm_map[nobj]);
2390         elmpriv = obj->elm_private;
2391         addl = &(elmpriv->addl);
2392
2393         addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2394
2395         /* Don't assume this object has any phys */
2396         bzero(&addl->proto_data, sizeof(addl->proto_data));
2397         if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2398                 goto out;
2399
2400         /* Skip forward to the phy list */
2401         if (eip)
2402                 offset += sizeof(struct ses_elm_sas_type0_eip_hdr);
2403         else
2404                 offset += sizeof(struct ses_elm_sas_type0_base_hdr);
2405
2406         /* Make sure the phy list fits in the buffer */
2407         physz = addl->proto_hdr.sas->base_hdr.num_phys;
2408         physz *= sizeof(struct ses_elm_sas_device_phy);
2409         if (physz > (bufsiz - offset + 4)) {
2410                 ENC_VLOG(enc, "Element %d Device Phy List Beyond End Of Buffer\n",
2411                     nobj);
2412                 err = EIO;
2413                 goto out;
2414         }
2415
2416         /* Point to the phy list */
2417         addl->proto_data.sasdev_phys =
2418             (struct ses_elm_sas_device_phy *)&buf[offset];
2419
2420 out:
2421         return (err);
2422 }
2423
2424 /**
2425  * \brief Update the softc with the additional element status data for this
2426  *        object, for SAS type 1 objects.
2427  *
2428  * \param enc           SES softc to be updated.
2429  * \param buf           The additional element status response buffer.
2430  * \param bufsiz        Size of the response buffer.
2431  * \param eip           The EIP bit value.
2432  * \param nobj          Number of objects attached to the SES softc.
2433  * 
2434  * \return              0 on success, errno otherwise.
2435  */
2436 static int
2437 ses_get_elm_addlstatus_sas_type1(enc_softc_t *enc, enc_cache_t *enc_cache,
2438                                  uint8_t *buf, int bufsiz, int eip, int nobj)
2439 {
2440         int err, offset, physz;
2441         enc_element_t *obj;
2442         ses_element_t *elmpriv;
2443         struct ses_addl_status *addl;
2444
2445         err = offset = 0;
2446
2447         /* basic object setup */
2448         obj = &(enc_cache->elm_map[nobj]);
2449         elmpriv = obj->elm_private;
2450         addl = &(elmpriv->addl);
2451
2452         addl->proto_hdr.sas = (union ses_elm_sas_hdr *)&buf[offset];
2453
2454         /* Don't assume this object has any phys */
2455         bzero(&addl->proto_data, sizeof(addl->proto_data));
2456         if (addl->proto_hdr.sas->base_hdr.num_phys == 0)
2457                 goto out;
2458
2459         /* Process expanders differently from other type1 cases */
2460         if (ses_obj_is_expander(enc, obj)) {
2461                 offset += sizeof(struct ses_elm_sas_type1_expander_hdr);
2462                 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2463                     sizeof(struct ses_elm_sas_expander_phy);
2464                 if (physz > (bufsiz - offset)) {
2465                         ENC_VLOG(enc, "Element %d: Expander Phy List Beyond "
2466                             "End Of Buffer\n", nobj);
2467                         err = EIO;
2468                         goto out;
2469                 }
2470                 addl->proto_data.sasexp_phys =
2471                     (struct ses_elm_sas_expander_phy *)&buf[offset];
2472         } else {
2473                 offset += sizeof(struct ses_elm_sas_type1_nonexpander_hdr);
2474                 physz = addl->proto_hdr.sas->base_hdr.num_phys *
2475                     sizeof(struct ses_elm_sas_port_phy);
2476                 if (physz > (bufsiz - offset + 4)) {
2477                         ENC_VLOG(enc, "Element %d: Port Phy List Beyond End "
2478                             "Of Buffer\n", nobj);
2479                         err = EIO;
2480                         goto out;
2481                 }
2482                 addl->proto_data.sasport_phys =
2483                     (struct ses_elm_sas_port_phy *)&buf[offset];
2484         }
2485
2486 out:
2487         return (err);
2488 }
2489
2490 /**
2491  * \brief Update the softc with the additional element status data for this
2492  *        object, for SAS objects.
2493  *
2494  * \param enc           SES softc to be updated.
2495  * \param buf           The additional element status response buffer.
2496  * \param bufsiz        Size of the response buffer.
2497  * \param eip           The EIP bit value.
2498  * \param tidx          Type index for this object.
2499  * \param nobj          Number of objects attached to the SES softc.
2500  * 
2501  * \return              0 on success, errno otherwise.
2502  */
2503 static int
2504 ses_get_elm_addlstatus_sas(enc_softc_t *enc, enc_cache_t *enc_cache,
2505                            uint8_t *buf, int bufsiz, int eip, int tidx,
2506                            int nobj)
2507 {
2508         int dtype, err;
2509         ses_cache_t *ses_cache;
2510         union ses_elm_sas_hdr *hdr;
2511
2512         /* Need to be able to read the descriptor type! */
2513         if (bufsiz < sizeof(union ses_elm_sas_hdr)) {
2514                 err = EIO;
2515                 goto out;
2516         }
2517
2518         ses_cache = enc_cache->private;
2519
2520         hdr = (union ses_elm_sas_hdr *)buf;
2521         dtype = ses_elm_sas_descr_type(hdr);
2522         switch(dtype) {
2523         case SES_SASOBJ_TYPE_SLOT:
2524                 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2525                 case ELMTYP_DEVICE:
2526                 case ELMTYP_ARRAY_DEV:
2527                         break;
2528                 default:
2529                         ENC_VLOG(enc, "Element %d has Additional Status type 0, "
2530                             "invalid for SES element type 0x%x\n", nobj,
2531                             ses_cache->ses_types[tidx].hdr->etype_elm_type);
2532                         err = ENODEV;
2533                         goto out;
2534                 }
2535                 err = ses_get_elm_addlstatus_sas_type0(enc, enc_cache,
2536                                                        buf, bufsiz, eip,
2537                     nobj);
2538                 break;
2539         case SES_SASOBJ_TYPE_OTHER:
2540                 switch(ses_cache->ses_types[tidx].hdr->etype_elm_type) {
2541                 case ELMTYP_SAS_EXP:
2542                 case ELMTYP_SCSI_INI:
2543                 case ELMTYP_SCSI_TGT:
2544                 case ELMTYP_ESCC:
2545                         break;
2546                 default:
2547                         ENC_VLOG(enc, "Element %d has Additional Status type 1, "
2548                             "invalid for SES element type 0x%x\n", nobj,
2549                             ses_cache->ses_types[tidx].hdr->etype_elm_type);
2550                         err = ENODEV;
2551                         goto out;
2552                 }
2553                 err = ses_get_elm_addlstatus_sas_type1(enc, enc_cache, buf,
2554                                                        bufsiz, eip, nobj);
2555                 break;
2556         default:
2557                 ENC_VLOG(enc, "Element %d of type 0x%x has Additional Status "
2558                     "of unknown type 0x%x\n", nobj,
2559                     ses_cache->ses_types[tidx].hdr->etype_elm_type, dtype);
2560                 err = ENODEV;
2561                 break;
2562         }
2563
2564 out:
2565         return (err);
2566 }
2567
2568 static void
2569 ses_softc_invalidate(enc_softc_t *enc)
2570 {
2571         ses_softc_t *ses;
2572
2573         ses = enc->enc_private;
2574         ses_terminate_control_requests(&ses->ses_requests, ENXIO);
2575 }
2576
2577 static void
2578 ses_softc_cleanup(enc_softc_t *enc)
2579 {
2580
2581         ses_cache_free(enc, &enc->enc_cache);
2582         ses_cache_free(enc, &enc->enc_daemon_cache);
2583         ENC_FREE_AND_NULL(enc->enc_private);
2584         ENC_FREE_AND_NULL(enc->enc_cache.private);
2585         ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2586 }
2587
2588 static int
2589 ses_init_enc(enc_softc_t *enc)
2590 {
2591         return (0);
2592 }
2593
2594 static int
2595 ses_get_enc_status(enc_softc_t *enc, int slpflag)
2596 {
2597         /* Automatically updated, caller checks enc_cache->encstat itself */
2598         return (0);
2599 }
2600
2601 static int
2602 ses_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
2603 {
2604         ses_control_request_t req;
2605         ses_softc_t          *ses;
2606
2607         ses = enc->enc_private;
2608         req.elm_idx = SES_SETSTATUS_ENC_IDX;
2609         req.elm_stat.comstatus = encstat & 0xf;
2610         
2611         TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2612         enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2613         cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2614
2615         return (req.result);
2616 }
2617
2618 static int
2619 ses_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2620 {
2621         unsigned int i = elms->elm_idx;
2622
2623         memcpy(elms->cstat, &enc->enc_cache.elm_map[i].encstat, 4);
2624         return (0);
2625 }
2626
2627 static int
2628 ses_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
2629 {
2630         ses_control_request_t req;
2631         ses_softc_t          *ses;
2632
2633         /* If this is clear, we don't do diddly.  */
2634         if ((elms->cstat[0] & SESCTL_CSEL) == 0)
2635                 return (0);
2636
2637         ses = enc->enc_private;
2638         req.elm_idx = elms->elm_idx;
2639         memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
2640
2641         TAILQ_INSERT_TAIL(&ses->ses_requests, &req, links);
2642         enc_update_request(enc, SES_PROCESS_CONTROL_REQS);
2643         cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
2644
2645         return (req.result);
2646 }
2647
2648 static int
2649 ses_get_elm_desc(enc_softc_t *enc, encioc_elm_desc_t *elmd)
2650 {
2651         int i = (int)elmd->elm_idx;
2652         ses_element_t *elmpriv;
2653
2654         /* Assume caller has already checked obj_id validity */
2655         elmpriv = enc->enc_cache.elm_map[i].elm_private;
2656         /* object might not have a descriptor */
2657         if (elmpriv == NULL || elmpriv->descr == NULL) {
2658                 elmd->elm_desc_len = 0;
2659                 return (0);
2660         }
2661         if (elmd->elm_desc_len > elmpriv->descr_len)
2662                 elmd->elm_desc_len = elmpriv->descr_len;
2663         copyout(elmpriv->descr, elmd->elm_desc_str, elmd->elm_desc_len);
2664         return (0);
2665 }
2666
2667 /**
2668  * \brief Respond to ENCIOC_GETELMDEVNAME, providing a device name for the
2669  *        given object id if one is available.
2670  *
2671  * \param enc   SES softc to examine.
2672  * \param objdn ioctl structure to read/write device name info.
2673  *
2674  * \return      0 on success, errno otherwise.
2675  */
2676 static int
2677 ses_get_elm_devnames(enc_softc_t *enc, encioc_elm_devnames_t *elmdn)
2678 {
2679         struct sbuf sb;
2680         int len;
2681
2682         len = elmdn->elm_names_size;
2683         if (len < 0)
2684                 return (EINVAL);
2685
2686         cam_periph_unlock(enc->periph);
2687         sbuf_new(&sb, NULL, len, SBUF_FIXEDLEN);
2688         ses_paths_iter(enc, &enc->enc_cache.elm_map[elmdn->elm_idx],
2689             ses_elmdevname_callback, &sb);
2690         sbuf_finish(&sb);
2691         elmdn->elm_names_len = sbuf_len(&sb);
2692         copyout(sbuf_data(&sb), elmdn->elm_devnames, elmdn->elm_names_len + 1);
2693         sbuf_delete(&sb);
2694         cam_periph_lock(enc->periph);
2695         return (elmdn->elm_names_len > 0 ? 0 : ENODEV);
2696 }
2697
2698 /**
2699  * \brief Send a string to the primary subenclosure using the String Out
2700  *        SES diagnostic page.
2701  *
2702  * \param enc   SES enclosure to run the command on.
2703  * \param sstr  SES string structure to operate on
2704  * \param ioc   Ioctl being performed
2705  *
2706  * \return      0 on success, errno otherwise.
2707  */
2708 static int
2709 ses_handle_string(enc_softc_t *enc, encioc_string_t *sstr, int ioc)
2710 {
2711         ses_softc_t *ses;
2712         enc_cache_t *enc_cache;
2713         ses_cache_t *ses_cache;
2714         const struct ses_enc_desc *enc_desc;
2715         int amt, payload, ret;
2716         char cdb[6];
2717         char str[32];
2718         char vendor[9];
2719         char product[17];
2720         char rev[5];
2721         uint8_t *buf;
2722         size_t size, rsize;
2723
2724         ses = enc->enc_private;
2725         enc_cache = &enc->enc_daemon_cache;
2726         ses_cache = enc_cache->private;
2727
2728         /* Implement SES2r20 6.1.6 */
2729         if (sstr->bufsiz > 0xffff)
2730                 return (EINVAL); /* buffer size too large */
2731
2732         switch (ioc) {
2733         case ENCIOC_SETSTRING:
2734                 payload = sstr->bufsiz + 4; /* header for SEND DIAGNOSTIC */
2735                 amt = 0 - payload;
2736                 buf = ENC_MALLOC(payload);
2737                 if (buf == NULL)
2738                         return (ENOMEM);
2739                 ses_page_cdb(cdb, payload, 0, CAM_DIR_OUT);
2740                 /* Construct the page request */
2741                 buf[0] = SesStringOut;
2742                 buf[1] = 0;
2743                 buf[2] = sstr->bufsiz >> 8;
2744                 buf[3] = sstr->bufsiz & 0xff;
2745                 memcpy(&buf[4], sstr->buf, sstr->bufsiz);
2746                 break;
2747         case ENCIOC_GETSTRING:
2748                 payload = sstr->bufsiz;
2749                 amt = payload;
2750                 ses_page_cdb(cdb, payload, SesStringIn, CAM_DIR_IN);
2751                 buf = sstr->buf;
2752                 break;
2753         case ENCIOC_GETENCNAME:
2754                 if (ses_cache->ses_nsubencs < 1)
2755                         return (ENODEV);
2756                 enc_desc = ses_cache->subencs[0];
2757                 cam_strvis(vendor, enc_desc->vendor_id,
2758                     sizeof(enc_desc->vendor_id), sizeof(vendor));
2759                 cam_strvis(product, enc_desc->product_id,
2760                     sizeof(enc_desc->product_id), sizeof(product));
2761                 cam_strvis(rev, enc_desc->product_rev,
2762                     sizeof(enc_desc->product_rev), sizeof(rev));
2763                 rsize = snprintf(str, sizeof(str), "%s %s %s",
2764                     vendor, product, rev) + 1;
2765                 if (rsize > sizeof(str))
2766                         rsize = sizeof(str);
2767                 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2768                 size = rsize;
2769                 if (size > sstr->bufsiz)
2770                         size = sstr->bufsiz;
2771                 copyout(str, sstr->buf, size);
2772                 return (size == rsize ? 0 : ENOMEM);
2773         case ENCIOC_GETENCID:
2774                 if (ses_cache->ses_nsubencs < 1)
2775                         return (ENODEV);
2776                 enc_desc = ses_cache->subencs[0];
2777                 rsize = snprintf(str, sizeof(str), "%16jx",
2778                     scsi_8btou64(enc_desc->logical_id)) + 1;
2779                 if (rsize > sizeof(str))
2780                         rsize = sizeof(str);
2781                 copyout(&rsize, &sstr->bufsiz, sizeof(rsize));
2782                 size = rsize;
2783                 if (size > sstr->bufsiz)
2784                         size = sstr->bufsiz;
2785                 copyout(str, sstr->buf, size);
2786                 return (size == rsize ? 0 : ENOMEM);
2787         default:
2788                 return (EINVAL);
2789         }
2790         ret = enc_runcmd(enc, cdb, 6, buf, &amt);
2791         if (ioc == ENCIOC_SETSTRING)
2792                 ENC_FREE(buf);
2793         return (ret);
2794 }
2795
2796 /**
2797  * \invariant Called with cam_periph mutex held.
2798  */
2799 static void
2800 ses_poll_status(enc_softc_t *enc)
2801 {
2802         ses_softc_t *ses;
2803
2804         ses = enc->enc_private;
2805         enc_update_request(enc, SES_UPDATE_GETSTATUS);
2806         if (ses->ses_flags & SES_FLAG_DESC)
2807                 enc_update_request(enc, SES_UPDATE_GETELMDESCS);
2808         if (ses->ses_flags & SES_FLAG_ADDLSTATUS)
2809                 enc_update_request(enc, SES_UPDATE_GETELMADDLSTATUS);
2810 }
2811
2812 /**
2813  * \brief Notification received when CAM detects a new device in the
2814  *        SCSI domain in which this SEP resides.
2815  *
2816  * \param enc   SES enclosure instance.
2817  */
2818 static void
2819 ses_device_found(enc_softc_t *enc)
2820 {
2821         ses_poll_status(enc);
2822         enc_update_request(enc, SES_PUBLISH_PHYSPATHS);
2823 }
2824
2825 static struct enc_vec ses_enc_vec =
2826 {
2827         .softc_invalidate       = ses_softc_invalidate,
2828         .softc_cleanup          = ses_softc_cleanup,
2829         .init_enc               = ses_init_enc,
2830         .get_enc_status         = ses_get_enc_status,
2831         .set_enc_status         = ses_set_enc_status,
2832         .get_elm_status         = ses_get_elm_status,
2833         .set_elm_status         = ses_set_elm_status,
2834         .get_elm_desc           = ses_get_elm_desc,
2835         .get_elm_devnames       = ses_get_elm_devnames,
2836         .handle_string          = ses_handle_string,
2837         .device_found           = ses_device_found,
2838         .poll_status            = ses_poll_status
2839 };
2840
2841 /**
2842  * \brief Initialize a new SES instance.
2843  *
2844  * \param enc           SES softc structure to set up the instance in.
2845  * \param doinit        Do the initialization (see main driver).
2846  *
2847  * \return              0 on success, errno otherwise.
2848  */
2849 int
2850 ses_softc_init(enc_softc_t *enc)
2851 {
2852         ses_softc_t *ses_softc;
2853
2854         CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
2855             ("entering enc_softc_init(%p)\n", enc));
2856
2857         enc->enc_vec = ses_enc_vec;
2858         enc->enc_fsm_states = enc_fsm_states;
2859
2860         if (enc->enc_private == NULL)
2861                 enc->enc_private = ENC_MALLOCZ(sizeof(ses_softc_t));
2862         if (enc->enc_cache.private == NULL)
2863                 enc->enc_cache.private = ENC_MALLOCZ(sizeof(ses_cache_t));
2864         if (enc->enc_daemon_cache.private == NULL)
2865                 enc->enc_daemon_cache.private =
2866                      ENC_MALLOCZ(sizeof(ses_cache_t));
2867
2868         if (enc->enc_private == NULL
2869          || enc->enc_cache.private == NULL
2870          || enc->enc_daemon_cache.private == NULL) {
2871                 ENC_FREE_AND_NULL(enc->enc_private);
2872                 ENC_FREE_AND_NULL(enc->enc_cache.private);
2873                 ENC_FREE_AND_NULL(enc->enc_daemon_cache.private);
2874                 return (ENOMEM);
2875         }
2876
2877         ses_softc = enc->enc_private;
2878         TAILQ_INIT(&ses_softc->ses_requests);
2879         TAILQ_INIT(&ses_softc->ses_pending_requests);
2880
2881         enc_update_request(enc, SES_UPDATE_PAGES);
2882
2883         // XXX: Move this to the FSM so it doesn't hang init
2884         if (0) (void) ses_set_timed_completion(enc, 1);
2885
2886         return (0);
2887 }
2888