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