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