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