1 /*- 2 * Copyright (c) 2008 Yahoo!, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD userland interface 31 */ 32 /*- 33 * Copyright (c) 2011-2015 LSI Corp. 34 * Copyright (c) 2013-2016 Avago Technologies 35 * Copyright 2000-2020 Broadcom Inc. 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD 60 * 61 * $FreeBSD$ 62 */ 63 64 #include <sys/cdefs.h> 65 __FBSDID("$FreeBSD$"); 66 67 /* TODO Move headers to mprvar */ 68 #include <sys/types.h> 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/selinfo.h> 73 #include <sys/module.h> 74 #include <sys/bus.h> 75 #include <sys/conf.h> 76 #include <sys/bio.h> 77 #include <sys/malloc.h> 78 #include <sys/uio.h> 79 #include <sys/sysctl.h> 80 #include <sys/ioccom.h> 81 #include <sys/endian.h> 82 #include <sys/queue.h> 83 #include <sys/kthread.h> 84 #include <sys/taskqueue.h> 85 #include <sys/proc.h> 86 #include <sys/sysent.h> 87 88 #include <machine/bus.h> 89 #include <machine/resource.h> 90 #include <sys/rman.h> 91 92 #include <cam/cam.h> 93 #include <cam/cam_ccb.h> 94 95 #include <dev/mpr/mpi/mpi2_type.h> 96 #include <dev/mpr/mpi/mpi2.h> 97 #include <dev/mpr/mpi/mpi2_ioc.h> 98 #include <dev/mpr/mpi/mpi2_cnfg.h> 99 #include <dev/mpr/mpi/mpi2_init.h> 100 #include <dev/mpr/mpi/mpi2_tool.h> 101 #include <dev/mpr/mpi/mpi2_pci.h> 102 #include <dev/mpr/mpr_ioctl.h> 103 #include <dev/mpr/mprvar.h> 104 #include <dev/mpr/mpr_table.h> 105 #include <dev/mpr/mpr_sas.h> 106 #include <dev/pci/pcivar.h> 107 #include <dev/pci/pcireg.h> 108 109 static d_open_t mpr_open; 110 static d_close_t mpr_close; 111 static d_ioctl_t mpr_ioctl_devsw; 112 113 static struct cdevsw mpr_cdevsw = { 114 .d_version = D_VERSION, 115 .d_flags = 0, 116 .d_open = mpr_open, 117 .d_close = mpr_close, 118 .d_ioctl = mpr_ioctl_devsw, 119 .d_name = "mpr", 120 }; 121 122 typedef int (mpr_user_f)(struct mpr_command *, struct mpr_usr_command *); 123 static mpr_user_f mpi_pre_ioc_facts; 124 static mpr_user_f mpi_pre_port_facts; 125 static mpr_user_f mpi_pre_fw_download; 126 static mpr_user_f mpi_pre_fw_upload; 127 static mpr_user_f mpi_pre_sata_passthrough; 128 static mpr_user_f mpi_pre_smp_passthrough; 129 static mpr_user_f mpi_pre_config; 130 static mpr_user_f mpi_pre_sas_io_unit_control; 131 132 static int mpr_user_read_cfg_header(struct mpr_softc *, 133 struct mpr_cfg_page_req *); 134 static int mpr_user_read_cfg_page(struct mpr_softc *, 135 struct mpr_cfg_page_req *, void *); 136 static int mpr_user_read_extcfg_header(struct mpr_softc *, 137 struct mpr_ext_cfg_page_req *); 138 static int mpr_user_read_extcfg_page(struct mpr_softc *, 139 struct mpr_ext_cfg_page_req *, void *); 140 static int mpr_user_write_cfg_page(struct mpr_softc *, 141 struct mpr_cfg_page_req *, void *); 142 static int mpr_user_setup_request(struct mpr_command *, 143 struct mpr_usr_command *); 144 static int mpr_user_command(struct mpr_softc *, struct mpr_usr_command *); 145 146 static int mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data); 147 static void mpr_user_get_adapter_data(struct mpr_softc *sc, 148 mpr_adapter_data_t *data); 149 static void mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data); 150 static uint8_t mpr_get_fw_diag_buffer_number(struct mpr_softc *sc, 151 uint32_t unique_id); 152 static int mpr_post_fw_diag_buffer(struct mpr_softc *sc, 153 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code); 154 static int mpr_release_fw_diag_buffer(struct mpr_softc *sc, 155 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code, 156 uint32_t diag_type); 157 static int mpr_diag_register(struct mpr_softc *sc, 158 mpr_fw_diag_register_t *diag_register, uint32_t *return_code); 159 static int mpr_diag_unregister(struct mpr_softc *sc, 160 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code); 161 static int mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query, 162 uint32_t *return_code); 163 static int mpr_diag_read_buffer(struct mpr_softc *sc, 164 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf, 165 uint32_t *return_code); 166 static int mpr_diag_release(struct mpr_softc *sc, 167 mpr_fw_diag_release_t *diag_release, uint32_t *return_code); 168 static int mpr_do_diag_action(struct mpr_softc *sc, uint32_t action, 169 uint8_t *diag_action, uint32_t length, uint32_t *return_code); 170 static int mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data); 171 static void mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data); 172 static void mpr_user_event_enable(struct mpr_softc *sc, 173 mpr_event_enable_t *data); 174 static int mpr_user_event_report(struct mpr_softc *sc, 175 mpr_event_report_t *data); 176 static int mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data); 177 static int mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data); 178 179 static MALLOC_DEFINE(M_MPRUSER, "mpr_user", "Buffers for mpr(4) ioctls"); 180 181 /* Macros from compat/freebsd32/freebsd32.h */ 182 #define PTRIN(v) (void *)(uintptr_t)(v) 183 #define PTROUT(v) (uint32_t)(uintptr_t)(v) 184 185 #define CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0) 186 #define PTRIN_CP(src,dst,fld) \ 187 do { (dst).fld = PTRIN((src).fld); } while (0) 188 #define PTROUT_CP(src,dst,fld) \ 189 do { (dst).fld = PTROUT((src).fld); } while (0) 190 191 /* 192 * MPI functions that support IEEE SGLs for SAS3. 193 */ 194 static uint8_t ieee_sgl_func_list[] = { 195 MPI2_FUNCTION_SCSI_IO_REQUEST, 196 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH, 197 MPI2_FUNCTION_SMP_PASSTHROUGH, 198 MPI2_FUNCTION_SATA_PASSTHROUGH, 199 MPI2_FUNCTION_FW_UPLOAD, 200 MPI2_FUNCTION_FW_DOWNLOAD, 201 MPI2_FUNCTION_TARGET_ASSIST, 202 MPI2_FUNCTION_TARGET_STATUS_SEND, 203 MPI2_FUNCTION_TOOLBOX 204 }; 205 206 int 207 mpr_attach_user(struct mpr_softc *sc) 208 { 209 int unit; 210 211 unit = device_get_unit(sc->mpr_dev); 212 sc->mpr_cdev = make_dev(&mpr_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640, 213 "mpr%d", unit); 214 215 if (sc->mpr_cdev == NULL) 216 return (ENOMEM); 217 218 sc->mpr_cdev->si_drv1 = sc; 219 return (0); 220 } 221 222 void 223 mpr_detach_user(struct mpr_softc *sc) 224 { 225 226 /* XXX: do a purge of pending requests? */ 227 if (sc->mpr_cdev != NULL) 228 destroy_dev(sc->mpr_cdev); 229 } 230 231 static int 232 mpr_open(struct cdev *dev, int flags, int fmt, struct thread *td) 233 { 234 235 return (0); 236 } 237 238 static int 239 mpr_close(struct cdev *dev, int flags, int fmt, struct thread *td) 240 { 241 242 return (0); 243 } 244 245 static int 246 mpr_user_read_cfg_header(struct mpr_softc *sc, 247 struct mpr_cfg_page_req *page_req) 248 { 249 MPI2_CONFIG_PAGE_HEADER *hdr; 250 struct mpr_config_params params; 251 int error; 252 253 hdr = ¶ms.hdr.Struct; 254 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER; 255 params.page_address = le32toh(page_req->page_address); 256 hdr->PageVersion = 0; 257 hdr->PageLength = 0; 258 hdr->PageNumber = page_req->header.PageNumber; 259 hdr->PageType = page_req->header.PageType; 260 params.buffer = NULL; 261 params.length = 0; 262 params.callback = NULL; 263 264 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) { 265 /* 266 * Leave the request. Without resetting the chip, it's 267 * still owned by it and we'll just get into trouble 268 * freeing it now. Mark it as abandoned so that if it 269 * shows up later it can be freed. 270 */ 271 mpr_printf(sc, "read_cfg_header timed out\n"); 272 return (ETIMEDOUT); 273 } 274 275 page_req->ioc_status = htole16(params.status); 276 if ((page_req->ioc_status & MPI2_IOCSTATUS_MASK) == 277 MPI2_IOCSTATUS_SUCCESS) { 278 bcopy(hdr, &page_req->header, sizeof(page_req->header)); 279 } 280 281 return (0); 282 } 283 284 static int 285 mpr_user_read_cfg_page(struct mpr_softc *sc, struct mpr_cfg_page_req *page_req, 286 void *buf) 287 { 288 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr; 289 struct mpr_config_params params; 290 int error; 291 292 reqhdr = buf; 293 hdr = ¶ms.hdr.Struct; 294 hdr->PageVersion = reqhdr->PageVersion; 295 hdr->PageLength = reqhdr->PageLength; 296 hdr->PageNumber = reqhdr->PageNumber; 297 hdr->PageType = reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK; 298 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT; 299 params.page_address = le32toh(page_req->page_address); 300 params.buffer = buf; 301 params.length = le32toh(page_req->len); 302 params.callback = NULL; 303 304 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) { 305 mpr_printf(sc, "mpr_user_read_cfg_page timed out\n"); 306 return (ETIMEDOUT); 307 } 308 309 page_req->ioc_status = htole16(params.status); 310 return (0); 311 } 312 313 static int 314 mpr_user_read_extcfg_header(struct mpr_softc *sc, 315 struct mpr_ext_cfg_page_req *ext_page_req) 316 { 317 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr; 318 struct mpr_config_params params; 319 int error; 320 321 hdr = ¶ms.hdr.Ext; 322 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER; 323 hdr->PageVersion = ext_page_req->header.PageVersion; 324 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED; 325 hdr->ExtPageLength = 0; 326 hdr->PageNumber = ext_page_req->header.PageNumber; 327 hdr->ExtPageType = ext_page_req->header.ExtPageType; 328 params.page_address = le32toh(ext_page_req->page_address); 329 params.buffer = NULL; 330 params.length = 0; 331 params.callback = NULL; 332 333 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) { 334 /* 335 * Leave the request. Without resetting the chip, it's 336 * still owned by it and we'll just get into trouble 337 * freeing it now. Mark it as abandoned so that if it 338 * shows up later it can be freed. 339 */ 340 mpr_printf(sc, "mpr_user_read_extcfg_header timed out\n"); 341 return (ETIMEDOUT); 342 } 343 344 ext_page_req->ioc_status = htole16(params.status); 345 if ((ext_page_req->ioc_status & MPI2_IOCSTATUS_MASK) == 346 MPI2_IOCSTATUS_SUCCESS) { 347 ext_page_req->header.PageVersion = hdr->PageVersion; 348 ext_page_req->header.PageNumber = hdr->PageNumber; 349 ext_page_req->header.PageType = hdr->PageType; 350 ext_page_req->header.ExtPageLength = hdr->ExtPageLength; 351 ext_page_req->header.ExtPageType = hdr->ExtPageType; 352 } 353 354 return (0); 355 } 356 357 static int 358 mpr_user_read_extcfg_page(struct mpr_softc *sc, 359 struct mpr_ext_cfg_page_req *ext_page_req, void *buf) 360 { 361 MPI2_CONFIG_EXTENDED_PAGE_HEADER *reqhdr, *hdr; 362 struct mpr_config_params params; 363 int error; 364 365 reqhdr = buf; 366 hdr = ¶ms.hdr.Ext; 367 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT; 368 params.page_address = le32toh(ext_page_req->page_address); 369 hdr->PageVersion = reqhdr->PageVersion; 370 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED; 371 hdr->PageNumber = reqhdr->PageNumber; 372 hdr->ExtPageType = reqhdr->ExtPageType; 373 hdr->ExtPageLength = reqhdr->ExtPageLength; 374 params.buffer = buf; 375 params.length = le32toh(ext_page_req->len); 376 params.callback = NULL; 377 378 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) { 379 mpr_printf(sc, "mpr_user_read_extcfg_page timed out\n"); 380 return (ETIMEDOUT); 381 } 382 383 ext_page_req->ioc_status = htole16(params.status); 384 return (0); 385 } 386 387 static int 388 mpr_user_write_cfg_page(struct mpr_softc *sc, 389 struct mpr_cfg_page_req *page_req, void *buf) 390 { 391 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr; 392 struct mpr_config_params params; 393 u_int hdr_attr; 394 int error; 395 396 reqhdr = buf; 397 hdr = ¶ms.hdr.Struct; 398 hdr_attr = reqhdr->PageType & MPI2_CONFIG_PAGEATTR_MASK; 399 if (hdr_attr != MPI2_CONFIG_PAGEATTR_CHANGEABLE && 400 hdr_attr != MPI2_CONFIG_PAGEATTR_PERSISTENT) { 401 mpr_printf(sc, "page type 0x%x not changeable\n", 402 reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK); 403 return (EINVAL); 404 } 405 406 /* 407 * There isn't any point in restoring stripped out attributes 408 * if you then mask them going down to issue the request. 409 */ 410 411 hdr->PageVersion = reqhdr->PageVersion; 412 hdr->PageLength = reqhdr->PageLength; 413 hdr->PageNumber = reqhdr->PageNumber; 414 hdr->PageType = reqhdr->PageType; 415 params.action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT; 416 params.page_address = le32toh(page_req->page_address); 417 params.buffer = buf; 418 params.length = le32toh(page_req->len); 419 params.callback = NULL; 420 421 if ((error = mpr_write_config_page(sc, ¶ms)) != 0) { 422 mpr_printf(sc, "mpr_write_cfg_page timed out\n"); 423 return (ETIMEDOUT); 424 } 425 426 page_req->ioc_status = htole16(params.status); 427 return (0); 428 } 429 430 void 431 mpr_init_sge(struct mpr_command *cm, void *req, void *sge) 432 { 433 int off, space; 434 435 space = (int)cm->cm_sc->reqframesz; 436 off = (uintptr_t)sge - (uintptr_t)req; 437 438 KASSERT(off < space, ("bad pointers %p %p, off %d, space %d", 439 req, sge, off, space)); 440 441 cm->cm_sge = sge; 442 cm->cm_sglsize = space - off; 443 } 444 445 /* 446 * Prepare the mpr_command for an IOC_FACTS request. 447 */ 448 static int 449 mpi_pre_ioc_facts(struct mpr_command *cm, struct mpr_usr_command *cmd) 450 { 451 MPI2_IOC_FACTS_REQUEST *req = (void *)cm->cm_req; 452 MPI2_IOC_FACTS_REPLY *rpl; 453 454 if (cmd->req_len != sizeof *req) 455 return (EINVAL); 456 if (cmd->rpl_len != sizeof *rpl) 457 return (EINVAL); 458 459 cm->cm_sge = NULL; 460 cm->cm_sglsize = 0; 461 return (0); 462 } 463 464 /* 465 * Prepare the mpr_command for a PORT_FACTS request. 466 */ 467 static int 468 mpi_pre_port_facts(struct mpr_command *cm, struct mpr_usr_command *cmd) 469 { 470 MPI2_PORT_FACTS_REQUEST *req = (void *)cm->cm_req; 471 MPI2_PORT_FACTS_REPLY *rpl; 472 473 if (cmd->req_len != sizeof *req) 474 return (EINVAL); 475 if (cmd->rpl_len != sizeof *rpl) 476 return (EINVAL); 477 478 cm->cm_sge = NULL; 479 cm->cm_sglsize = 0; 480 return (0); 481 } 482 483 /* 484 * Prepare the mpr_command for a FW_DOWNLOAD request. 485 */ 486 static int 487 mpi_pre_fw_download(struct mpr_command *cm, struct mpr_usr_command *cmd) 488 { 489 MPI25_FW_DOWNLOAD_REQUEST *req = (void *)cm->cm_req; 490 MPI2_FW_DOWNLOAD_REPLY *rpl; 491 int error; 492 493 if (cmd->req_len != sizeof *req) 494 return (EINVAL); 495 if (cmd->rpl_len != sizeof *rpl) 496 return (EINVAL); 497 498 if (cmd->len == 0) 499 return (EINVAL); 500 501 error = copyin(cmd->buf, cm->cm_data, cmd->len); 502 if (error != 0) 503 return (error); 504 505 mpr_init_sge(cm, req, &req->SGL); 506 507 /* 508 * For now, the F/W image must be provided in a single request. 509 */ 510 if ((req->MsgFlags & MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT) == 0) 511 return (EINVAL); 512 if (req->TotalImageSize != cmd->len) 513 return (EINVAL); 514 515 req->ImageOffset = 0; 516 req->ImageSize = cmd->len; 517 518 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT; 519 520 return (mpr_push_ieee_sge(cm, &req->SGL, 0)); 521 } 522 523 /* 524 * Prepare the mpr_command for a FW_UPLOAD request. 525 */ 526 static int 527 mpi_pre_fw_upload(struct mpr_command *cm, struct mpr_usr_command *cmd) 528 { 529 MPI25_FW_UPLOAD_REQUEST *req = (void *)cm->cm_req; 530 MPI2_FW_UPLOAD_REPLY *rpl; 531 532 if (cmd->req_len != sizeof *req) 533 return (EINVAL); 534 if (cmd->rpl_len != sizeof *rpl) 535 return (EINVAL); 536 537 mpr_init_sge(cm, req, &req->SGL); 538 if (cmd->len == 0) { 539 /* Perhaps just asking what the size of the fw is? */ 540 return (0); 541 } 542 543 req->ImageOffset = 0; 544 req->ImageSize = cmd->len; 545 546 cm->cm_flags |= MPR_CM_FLAGS_DATAIN; 547 548 return (mpr_push_ieee_sge(cm, &req->SGL, 0)); 549 } 550 551 /* 552 * Prepare the mpr_command for a SATA_PASSTHROUGH request. 553 */ 554 static int 555 mpi_pre_sata_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd) 556 { 557 MPI2_SATA_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req; 558 MPI2_SATA_PASSTHROUGH_REPLY *rpl; 559 560 if (cmd->req_len != sizeof *req) 561 return (EINVAL); 562 if (cmd->rpl_len != sizeof *rpl) 563 return (EINVAL); 564 565 mpr_init_sge(cm, req, &req->SGL); 566 return (0); 567 } 568 569 /* 570 * Prepare the mpr_command for a SMP_PASSTHROUGH request. 571 */ 572 static int 573 mpi_pre_smp_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd) 574 { 575 MPI2_SMP_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req; 576 MPI2_SMP_PASSTHROUGH_REPLY *rpl; 577 578 if (cmd->req_len != sizeof *req) 579 return (EINVAL); 580 if (cmd->rpl_len != sizeof *rpl) 581 return (EINVAL); 582 583 mpr_init_sge(cm, req, &req->SGL); 584 return (0); 585 } 586 587 /* 588 * Prepare the mpr_command for a CONFIG request. 589 */ 590 static int 591 mpi_pre_config(struct mpr_command *cm, struct mpr_usr_command *cmd) 592 { 593 MPI2_CONFIG_REQUEST *req = (void *)cm->cm_req; 594 MPI2_CONFIG_REPLY *rpl; 595 596 if (cmd->req_len != sizeof *req) 597 return (EINVAL); 598 if (cmd->rpl_len != sizeof *rpl) 599 return (EINVAL); 600 601 mpr_init_sge(cm, req, &req->PageBufferSGE); 602 return (0); 603 } 604 605 /* 606 * Prepare the mpr_command for a SAS_IO_UNIT_CONTROL request. 607 */ 608 static int 609 mpi_pre_sas_io_unit_control(struct mpr_command *cm, 610 struct mpr_usr_command *cmd) 611 { 612 613 cm->cm_sge = NULL; 614 cm->cm_sglsize = 0; 615 return (0); 616 } 617 618 /* 619 * A set of functions to prepare an mpr_command for the various 620 * supported requests. 621 */ 622 struct mpr_user_func { 623 U8 Function; 624 mpr_user_f *f_pre; 625 } mpr_user_func_list[] = { 626 { MPI2_FUNCTION_IOC_FACTS, mpi_pre_ioc_facts }, 627 { MPI2_FUNCTION_PORT_FACTS, mpi_pre_port_facts }, 628 { MPI2_FUNCTION_FW_DOWNLOAD, mpi_pre_fw_download }, 629 { MPI2_FUNCTION_FW_UPLOAD, mpi_pre_fw_upload }, 630 { MPI2_FUNCTION_SATA_PASSTHROUGH, mpi_pre_sata_passthrough }, 631 { MPI2_FUNCTION_SMP_PASSTHROUGH, mpi_pre_smp_passthrough}, 632 { MPI2_FUNCTION_CONFIG, mpi_pre_config}, 633 { MPI2_FUNCTION_SAS_IO_UNIT_CONTROL, mpi_pre_sas_io_unit_control }, 634 { 0xFF, NULL } /* list end */ 635 }; 636 637 static int 638 mpr_user_setup_request(struct mpr_command *cm, struct mpr_usr_command *cmd) 639 { 640 MPI2_REQUEST_HEADER *hdr = (MPI2_REQUEST_HEADER *)cm->cm_req; 641 struct mpr_user_func *f; 642 643 for (f = mpr_user_func_list; f->f_pre != NULL; f++) { 644 if (hdr->Function == f->Function) 645 return (f->f_pre(cm, cmd)); 646 } 647 return (EINVAL); 648 } 649 650 static int 651 mpr_user_command(struct mpr_softc *sc, struct mpr_usr_command *cmd) 652 { 653 MPI2_REQUEST_HEADER *hdr; 654 MPI2_DEFAULT_REPLY *rpl = NULL; 655 void *buf = NULL; 656 struct mpr_command *cm = NULL; 657 int err = 0; 658 int sz; 659 660 mpr_lock(sc); 661 cm = mpr_alloc_command(sc); 662 663 if (cm == NULL) { 664 mpr_printf(sc, "%s: no mpr requests\n", __func__); 665 err = ENOMEM; 666 goto RetFree; 667 } 668 mpr_unlock(sc); 669 670 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req; 671 672 mpr_dprint(sc, MPR_USER, "%s: req %p %d rpl %p %d\n", __func__, 673 cmd->req, cmd->req_len, cmd->rpl, cmd->rpl_len); 674 675 if (cmd->req_len > (int)sc->reqframesz) { 676 err = EINVAL; 677 goto RetFreeUnlocked; 678 } 679 err = copyin(cmd->req, hdr, cmd->req_len); 680 if (err != 0) 681 goto RetFreeUnlocked; 682 683 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__, 684 hdr->Function, hdr->MsgFlags); 685 686 if (cmd->len > 0) { 687 buf = malloc(cmd->len, M_MPRUSER, M_WAITOK|M_ZERO); 688 cm->cm_data = buf; 689 cm->cm_length = cmd->len; 690 } else { 691 cm->cm_data = NULL; 692 cm->cm_length = 0; 693 } 694 695 cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE; 696 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 697 698 err = mpr_user_setup_request(cm, cmd); 699 if (err == EINVAL) { 700 mpr_printf(sc, "%s: unsupported parameter or unsupported " 701 "function in request (function = 0x%X)\n", __func__, 702 hdr->Function); 703 } 704 if (err != 0) 705 goto RetFreeUnlocked; 706 707 mpr_lock(sc); 708 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP); 709 710 if (err || (cm == NULL)) { 711 mpr_printf(sc, "%s: invalid request: error %d\n", 712 __func__, err); 713 goto RetFree; 714 } 715 716 if (cm != NULL) 717 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply; 718 if (rpl != NULL) 719 sz = rpl->MsgLength * 4; 720 else 721 sz = 0; 722 723 if (sz > cmd->rpl_len) { 724 mpr_printf(sc, "%s: user reply buffer (%d) smaller than " 725 "returned buffer (%d)\n", __func__, cmd->rpl_len, sz); 726 sz = cmd->rpl_len; 727 } 728 729 mpr_unlock(sc); 730 copyout(rpl, cmd->rpl, sz); 731 if (buf != NULL) 732 copyout(buf, cmd->buf, cmd->len); 733 mpr_dprint(sc, MPR_USER, "%s: reply size %d\n", __func__, sz); 734 735 RetFreeUnlocked: 736 mpr_lock(sc); 737 RetFree: 738 if (cm != NULL) 739 mpr_free_command(sc, cm); 740 mpr_unlock(sc); 741 if (buf != NULL) 742 free(buf, M_MPRUSER); 743 return (err); 744 } 745 746 static int 747 mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data) 748 { 749 MPI2_REQUEST_HEADER *hdr, tmphdr; 750 MPI2_DEFAULT_REPLY *rpl; 751 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply = NULL; 752 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL; 753 struct mpr_command *cm = NULL; 754 int i, err = 0, dir = 0, sz; 755 uint8_t tool, function = 0; 756 u_int sense_len; 757 struct mprsas_target *targ = NULL; 758 759 /* 760 * Only allow one passthru command at a time. Use the MPR_FLAGS_BUSY 761 * bit to denote that a passthru is being processed. 762 */ 763 mpr_lock(sc); 764 if (sc->mpr_flags & MPR_FLAGS_BUSY) { 765 mpr_dprint(sc, MPR_USER, "%s: Only one passthru command " 766 "allowed at a single time.", __func__); 767 mpr_unlock(sc); 768 return (EBUSY); 769 } 770 sc->mpr_flags |= MPR_FLAGS_BUSY; 771 mpr_unlock(sc); 772 773 /* 774 * Do some validation on data direction. Valid cases are: 775 * 1) DataSize is 0 and direction is NONE 776 * 2) DataSize is non-zero and one of: 777 * a) direction is READ or 778 * b) direction is WRITE or 779 * c) direction is BOTH and DataOutSize is non-zero 780 * If valid and the direction is BOTH, change the direction to READ. 781 * if valid and the direction is not BOTH, make sure DataOutSize is 0. 782 */ 783 if (((data->DataSize == 0) && 784 (data->DataDirection == MPR_PASS_THRU_DIRECTION_NONE)) || 785 ((data->DataSize != 0) && 786 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_READ) || 787 (data->DataDirection == MPR_PASS_THRU_DIRECTION_WRITE) || 788 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH) && 789 (data->DataOutSize != 0))))) { 790 if (data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH) 791 data->DataDirection = MPR_PASS_THRU_DIRECTION_READ; 792 else 793 data->DataOutSize = 0; 794 } else 795 return (EINVAL); 796 797 mpr_dprint(sc, MPR_USER, "%s: req 0x%jx %d rpl 0x%jx %d " 798 "data in 0x%jx %d data out 0x%jx %d data dir %d\n", __func__, 799 data->PtrRequest, data->RequestSize, data->PtrReply, 800 data->ReplySize, data->PtrData, data->DataSize, 801 data->PtrDataOut, data->DataOutSize, data->DataDirection); 802 803 /* 804 * copy in the header so we know what we're dealing with before we 805 * commit to allocating a command for it. 806 */ 807 err = copyin(PTRIN(data->PtrRequest), &tmphdr, data->RequestSize); 808 if (err != 0) 809 goto RetFreeUnlocked; 810 811 if (data->RequestSize > (int)sc->reqframesz) { 812 err = EINVAL; 813 goto RetFreeUnlocked; 814 } 815 816 function = tmphdr.Function; 817 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__, 818 function, tmphdr.MsgFlags); 819 820 /* 821 * Handle a passthru TM request. 822 */ 823 if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) { 824 MPI2_SCSI_TASK_MANAGE_REQUEST *task; 825 826 mpr_lock(sc); 827 cm = mprsas_alloc_tm(sc); 828 if (cm == NULL) { 829 err = EINVAL; 830 goto Ret; 831 } 832 833 /* Copy the header in. Only a small fixup is needed. */ 834 task = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req; 835 bcopy(&tmphdr, task, data->RequestSize); 836 task->TaskMID = cm->cm_desc.Default.SMID; 837 838 cm->cm_data = NULL; 839 cm->cm_complete = NULL; 840 cm->cm_complete_data = NULL; 841 842 targ = mprsas_find_target_by_handle(sc->sassc, 0, 843 task->DevHandle); 844 if (targ == NULL) { 845 mpr_dprint(sc, MPR_INFO, 846 "%s %d : invalid handle for requested TM 0x%x \n", 847 __func__, __LINE__, task->DevHandle); 848 err = 1; 849 } else { 850 mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD); 851 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP); 852 } 853 854 if (err != 0) { 855 err = EIO; 856 mpr_dprint(sc, MPR_FAULT, "%s: task management failed", 857 __func__); 858 } 859 /* 860 * Copy the reply data and sense data to user space. 861 */ 862 if ((cm != NULL) && (cm->cm_reply != NULL)) { 863 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply; 864 sz = rpl->MsgLength * 4; 865 866 if (sz > data->ReplySize) { 867 mpr_printf(sc, "%s: user reply buffer (%d) " 868 "smaller than returned buffer (%d)\n", 869 __func__, data->ReplySize, sz); 870 } 871 mpr_unlock(sc); 872 copyout(cm->cm_reply, PTRIN(data->PtrReply), 873 data->ReplySize); 874 mpr_lock(sc); 875 } 876 mprsas_free_tm(sc, cm); 877 goto Ret; 878 } 879 880 mpr_lock(sc); 881 cm = mpr_alloc_command(sc); 882 883 if (cm == NULL) { 884 mpr_printf(sc, "%s: no mpr requests\n", __func__); 885 err = ENOMEM; 886 goto Ret; 887 } 888 mpr_unlock(sc); 889 890 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req; 891 bcopy(&tmphdr, hdr, data->RequestSize); 892 893 /* 894 * Do some checking to make sure the IOCTL request contains a valid 895 * request. Then set the SGL info. 896 */ 897 mpr_init_sge(cm, hdr, (void *)((uint8_t *)hdr + data->RequestSize)); 898 899 /* 900 * Set up for read, write or both. From check above, DataOutSize will 901 * be 0 if direction is READ or WRITE, but it will have some non-zero 902 * value if the direction is BOTH. So, just use the biggest size to get 903 * the cm_data buffer size. If direction is BOTH, 2 SGLs need to be set 904 * up; the first is for the request and the second will contain the 905 * response data. cm_out_len needs to be set here and this will be used 906 * when the SGLs are set up. 907 */ 908 cm->cm_data = NULL; 909 cm->cm_length = MAX(data->DataSize, data->DataOutSize); 910 cm->cm_out_len = data->DataOutSize; 911 cm->cm_flags = 0; 912 if (cm->cm_length != 0) { 913 cm->cm_data = malloc(cm->cm_length, M_MPRUSER, M_WAITOK | 914 M_ZERO); 915 cm->cm_flags = MPR_CM_FLAGS_DATAIN; 916 if (data->DataOutSize) { 917 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT; 918 err = copyin(PTRIN(data->PtrDataOut), 919 cm->cm_data, data->DataOutSize); 920 } else if (data->DataDirection == 921 MPR_PASS_THRU_DIRECTION_WRITE) { 922 cm->cm_flags = MPR_CM_FLAGS_DATAOUT; 923 err = copyin(PTRIN(data->PtrData), 924 cm->cm_data, data->DataSize); 925 } 926 if (err != 0) 927 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy IOCTL " 928 "data from user space\n", __func__); 929 } 930 /* 931 * Set this flag only if processing a command that does not need an 932 * IEEE SGL. The CLI Tool within the Toolbox uses IEEE SGLs, so clear 933 * the flag only for that tool if processing a Toolbox function. 934 */ 935 cm->cm_flags |= MPR_CM_FLAGS_SGE_SIMPLE; 936 for (i = 0; i < sizeof (ieee_sgl_func_list); i++) { 937 if (function == ieee_sgl_func_list[i]) { 938 if (function == MPI2_FUNCTION_TOOLBOX) 939 { 940 tool = (uint8_t)hdr->FunctionDependent1; 941 if (tool != MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) 942 break; 943 } 944 cm->cm_flags &= ~MPR_CM_FLAGS_SGE_SIMPLE; 945 break; 946 } 947 } 948 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 949 950 if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) { 951 nvme_encap_request = 952 (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req; 953 cm->cm_desc.Default.RequestFlags = 954 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED; 955 956 /* 957 * Get the Physical Address of the sense buffer. 958 * Save the user's Error Response buffer address and use that 959 * field to hold the sense buffer address. 960 * Clear the internal sense buffer, which will potentially hold 961 * the Completion Queue Entry on return, or 0 if no Entry. 962 * Build the PRPs and set direction bits. 963 * Send the request. 964 */ 965 cm->nvme_error_response = 966 (uint64_t *)(uintptr_t)(((uint64_t)nvme_encap_request-> 967 ErrorResponseBaseAddress.High << 32) | 968 (uint64_t)nvme_encap_request-> 969 ErrorResponseBaseAddress.Low); 970 nvme_encap_request->ErrorResponseBaseAddress.High = 971 htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32)); 972 nvme_encap_request->ErrorResponseBaseAddress.Low = 973 htole32(cm->cm_sense_busaddr); 974 memset(cm->cm_sense, 0, NVME_ERROR_RESPONSE_SIZE); 975 mpr_build_nvme_prp(sc, cm, nvme_encap_request, cm->cm_data, 976 data->DataSize, data->DataOutSize); 977 } 978 979 /* 980 * Set up Sense buffer and SGL offset for IO passthru. SCSI IO request 981 * uses SCSI IO or Fast Path SCSI IO descriptor. 982 */ 983 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) || 984 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) { 985 MPI2_SCSI_IO_REQUEST *scsi_io_req; 986 987 scsi_io_req = (MPI2_SCSI_IO_REQUEST *)hdr; 988 /* 989 * Put SGE for data and data_out buffer at the end of 990 * scsi_io_request message header (64 bytes in total). 991 * Following above SGEs, the residual space will be used by 992 * sense data. 993 */ 994 scsi_io_req->SenseBufferLength = (uint8_t)(data->RequestSize - 995 64); 996 scsi_io_req->SenseBufferLowAddress = 997 htole32(cm->cm_sense_busaddr); 998 999 /* 1000 * Set SGLOffset0 value. This is the number of dwords that SGL 1001 * is offset from the beginning of MPI2_SCSI_IO_REQUEST struct. 1002 */ 1003 scsi_io_req->SGLOffset0 = 24; 1004 1005 /* 1006 * Setup descriptor info. RAID passthrough must use the 1007 * default request descriptor which is already set, so if this 1008 * is a SCSI IO request, change the descriptor to SCSI IO or 1009 * Fast Path SCSI IO. Also, if this is a SCSI IO request, 1010 * handle the reply in the mprsas_scsio_complete function. 1011 */ 1012 if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) { 1013 targ = mprsas_find_target_by_handle(sc->sassc, 0, 1014 scsi_io_req->DevHandle); 1015 1016 if (!targ) { 1017 printf("No Target found for handle %d\n", 1018 scsi_io_req->DevHandle); 1019 err = EINVAL; 1020 goto RetFreeUnlocked; 1021 } 1022 1023 if (targ->scsi_req_desc_type == 1024 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) { 1025 cm->cm_desc.FastPathSCSIIO.RequestFlags = 1026 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO; 1027 if (!sc->atomic_desc_capable) { 1028 cm->cm_desc.FastPathSCSIIO.DevHandle = 1029 scsi_io_req->DevHandle; 1030 } 1031 scsi_io_req->IoFlags |= 1032 MPI25_SCSIIO_IOFLAGS_FAST_PATH; 1033 } else { 1034 cm->cm_desc.SCSIIO.RequestFlags = 1035 MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO; 1036 if (!sc->atomic_desc_capable) { 1037 cm->cm_desc.SCSIIO.DevHandle = 1038 scsi_io_req->DevHandle; 1039 } 1040 } 1041 1042 /* 1043 * Make sure the DevHandle is not 0 because this is a 1044 * likely error. 1045 */ 1046 if (scsi_io_req->DevHandle == 0) { 1047 err = EINVAL; 1048 goto RetFreeUnlocked; 1049 } 1050 } 1051 } 1052 1053 mpr_lock(sc); 1054 1055 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP); 1056 1057 if (err || (cm == NULL)) { 1058 mpr_printf(sc, "%s: invalid request: error %d\n", __func__, 1059 err); 1060 goto RetFree; 1061 } 1062 1063 /* 1064 * Sync the DMA data, if any. Then copy the data to user space. 1065 */ 1066 if (cm->cm_data != NULL) { 1067 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) 1068 dir = BUS_DMASYNC_POSTREAD; 1069 else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT) 1070 dir = BUS_DMASYNC_POSTWRITE; 1071 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir); 1072 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap); 1073 1074 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) { 1075 mpr_unlock(sc); 1076 err = copyout(cm->cm_data, 1077 PTRIN(data->PtrData), data->DataSize); 1078 mpr_lock(sc); 1079 if (err != 0) 1080 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy " 1081 "IOCTL data to user space\n", __func__); 1082 } 1083 } 1084 1085 /* 1086 * Copy the reply data and sense data to user space. 1087 */ 1088 if (cm->cm_reply != NULL) { 1089 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply; 1090 sz = rpl->MsgLength * 4; 1091 1092 if (sz > data->ReplySize) { 1093 mpr_printf(sc, "%s: user reply buffer (%d) smaller " 1094 "than returned buffer (%d)\n", __func__, 1095 data->ReplySize, sz); 1096 } 1097 mpr_unlock(sc); 1098 copyout(cm->cm_reply, PTRIN(data->PtrReply), data->ReplySize); 1099 mpr_lock(sc); 1100 1101 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) || 1102 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) { 1103 if (((MPI2_SCSI_IO_REPLY *)rpl)->SCSIState & 1104 MPI2_SCSI_STATE_AUTOSENSE_VALID) { 1105 sense_len = 1106 MIN((le32toh(((MPI2_SCSI_IO_REPLY *)rpl)-> 1107 SenseCount)), sizeof(struct 1108 scsi_sense_data)); 1109 mpr_unlock(sc); 1110 copyout(cm->cm_sense, 1111 (PTRIN(data->PtrReply + sizeof(MPI2_SCSI_IO_REPLY))), 1112 sense_len); 1113 mpr_lock(sc); 1114 } 1115 } 1116 1117 /* 1118 * Copy out the NVMe Error Reponse to user. The Error Response 1119 * buffer is given by the user, but a sense buffer is used to 1120 * get that data from the IOC. The user's 1121 * ErrorResponseBaseAddress is saved in the 1122 * 'nvme_error_response' field before the command because that 1123 * field is set to a sense buffer. When the command is 1124 * complete, the Error Response data from the IOC is copied to 1125 * that user address after it is checked for validity. 1126 * Also note that 'sense' buffers are not defined for 1127 * NVMe commands. Sense terminalogy is only used here so that 1128 * the same IOCTL structure and sense buffers can be used for 1129 * NVMe. 1130 */ 1131 if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) { 1132 if (cm->nvme_error_response == NULL) { 1133 mpr_dprint(sc, MPR_INFO, "NVMe Error Response " 1134 "buffer is NULL. Response data will not be " 1135 "returned.\n"); 1136 mpr_unlock(sc); 1137 goto RetFreeUnlocked; 1138 } 1139 1140 nvme_error_reply = 1141 (Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply; 1142 sz = MIN(le32toh(nvme_error_reply->ErrorResponseCount), 1143 NVME_ERROR_RESPONSE_SIZE); 1144 mpr_unlock(sc); 1145 copyout(cm->cm_sense, 1146 (PTRIN(data->PtrReply + 1147 sizeof(MPI2_SCSI_IO_REPLY))), sz); 1148 mpr_lock(sc); 1149 } 1150 } 1151 mpr_unlock(sc); 1152 1153 RetFreeUnlocked: 1154 mpr_lock(sc); 1155 1156 RetFree: 1157 if (cm != NULL) { 1158 if (cm->cm_data) 1159 free(cm->cm_data, M_MPRUSER); 1160 mpr_free_command(sc, cm); 1161 } 1162 Ret: 1163 sc->mpr_flags &= ~MPR_FLAGS_BUSY; 1164 mpr_unlock(sc); 1165 1166 return (err); 1167 } 1168 1169 static void 1170 mpr_user_get_adapter_data(struct mpr_softc *sc, mpr_adapter_data_t *data) 1171 { 1172 Mpi2ConfigReply_t mpi_reply; 1173 Mpi2BiosPage3_t config_page; 1174 1175 /* 1176 * Use the PCI interface functions to get the Bus, Device, and Function 1177 * information. 1178 */ 1179 data->PciInformation.u.bits.BusNumber = pci_get_bus(sc->mpr_dev); 1180 data->PciInformation.u.bits.DeviceNumber = pci_get_slot(sc->mpr_dev); 1181 data->PciInformation.u.bits.FunctionNumber = 1182 pci_get_function(sc->mpr_dev); 1183 1184 /* 1185 * Get the FW version that should already be saved in IOC Facts. 1186 */ 1187 data->MpiFirmwareVersion = sc->facts->FWVersion.Word; 1188 1189 /* 1190 * General device info. 1191 */ 1192 if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC) 1193 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS35; 1194 else 1195 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS3; 1196 data->PCIDeviceHwId = pci_get_device(sc->mpr_dev); 1197 data->PCIDeviceHwRev = pci_read_config(sc->mpr_dev, PCIR_REVID, 1); 1198 data->SubSystemId = pci_get_subdevice(sc->mpr_dev); 1199 data->SubsystemVendorId = pci_get_subvendor(sc->mpr_dev); 1200 1201 /* 1202 * Get the driver version. 1203 */ 1204 strcpy((char *)&data->DriverVersion[0], MPR_DRIVER_VERSION); 1205 1206 /* 1207 * Need to get BIOS Config Page 3 for the BIOS Version. 1208 */ 1209 data->BiosVersion = 0; 1210 mpr_lock(sc); 1211 if (mpr_config_get_bios_pg3(sc, &mpi_reply, &config_page)) 1212 printf("%s: Error while retrieving BIOS Version\n", __func__); 1213 else 1214 data->BiosVersion = config_page.BiosVersion; 1215 mpr_unlock(sc); 1216 } 1217 1218 static void 1219 mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data) 1220 { 1221 int i; 1222 1223 /* 1224 * Use the PCI interface functions to get the Bus, Device, and Function 1225 * information. 1226 */ 1227 data->BusNumber = pci_get_bus(sc->mpr_dev); 1228 data->DeviceNumber = pci_get_slot(sc->mpr_dev); 1229 data->FunctionNumber = pci_get_function(sc->mpr_dev); 1230 1231 /* 1232 * Now get the interrupt vector and the pci header. The vector can 1233 * only be 0 right now. The header is the first 256 bytes of config 1234 * space. 1235 */ 1236 data->InterruptVector = 0; 1237 for (i = 0; i < sizeof (data->PciHeader); i++) { 1238 data->PciHeader[i] = pci_read_config(sc->mpr_dev, i, 1); 1239 } 1240 } 1241 1242 static uint8_t 1243 mpr_get_fw_diag_buffer_number(struct mpr_softc *sc, uint32_t unique_id) 1244 { 1245 uint8_t index; 1246 1247 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) { 1248 if (sc->fw_diag_buffer_list[index].unique_id == unique_id) { 1249 return (index); 1250 } 1251 } 1252 1253 return (MPR_FW_DIAGNOSTIC_UID_NOT_FOUND); 1254 } 1255 1256 static int 1257 mpr_post_fw_diag_buffer(struct mpr_softc *sc, 1258 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code) 1259 { 1260 MPI2_DIAG_BUFFER_POST_REQUEST *req; 1261 MPI2_DIAG_BUFFER_POST_REPLY *reply; 1262 struct mpr_command *cm = NULL; 1263 int i, status; 1264 1265 /* 1266 * If buffer is not enabled, just leave. 1267 */ 1268 *return_code = MPR_FW_DIAG_ERROR_POST_FAILED; 1269 if (!pBuffer->enabled) { 1270 return (MPR_DIAG_FAILURE); 1271 } 1272 1273 /* 1274 * Clear some flags initially. 1275 */ 1276 pBuffer->force_release = FALSE; 1277 pBuffer->valid_data = FALSE; 1278 pBuffer->owned_by_firmware = FALSE; 1279 1280 /* 1281 * Get a command. 1282 */ 1283 cm = mpr_alloc_command(sc); 1284 if (cm == NULL) { 1285 mpr_printf(sc, "%s: no mpr requests\n", __func__); 1286 return (MPR_DIAG_FAILURE); 1287 } 1288 1289 /* 1290 * Build the request for releasing the FW Diag Buffer and send it. 1291 */ 1292 req = (MPI2_DIAG_BUFFER_POST_REQUEST *)cm->cm_req; 1293 req->Function = MPI2_FUNCTION_DIAG_BUFFER_POST; 1294 req->BufferType = pBuffer->buffer_type; 1295 req->ExtendedType = pBuffer->extended_type; 1296 req->BufferLength = pBuffer->size; 1297 for (i = 0; i < (sizeof(req->ProductSpecific) / 4); i++) 1298 req->ProductSpecific[i] = pBuffer->product_specific[i]; 1299 mpr_from_u64(sc->fw_diag_busaddr, &req->BufferAddress); 1300 cm->cm_data = NULL; 1301 cm->cm_length = 0; 1302 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 1303 cm->cm_complete_data = NULL; 1304 1305 /* 1306 * Send command synchronously. 1307 */ 1308 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP); 1309 if (status || (cm == NULL)) { 1310 mpr_printf(sc, "%s: invalid request: error %d\n", __func__, 1311 status); 1312 status = MPR_DIAG_FAILURE; 1313 goto done; 1314 } 1315 1316 /* 1317 * Process POST reply. 1318 */ 1319 reply = (MPI2_DIAG_BUFFER_POST_REPLY *)cm->cm_reply; 1320 if (reply == NULL) { 1321 mpr_printf(sc, "%s: reply is NULL, probably due to " 1322 "reinitialization", __func__); 1323 status = MPR_DIAG_FAILURE; 1324 goto done; 1325 } 1326 1327 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) != 1328 MPI2_IOCSTATUS_SUCCESS) { 1329 status = MPR_DIAG_FAILURE; 1330 mpr_dprint(sc, MPR_FAULT, "%s: post of FW Diag Buffer failed " 1331 "with IOCStatus = 0x%x, IOCLogInfo = 0x%x and " 1332 "TransferLength = 0x%x\n", __func__, 1333 le16toh(reply->IOCStatus), le32toh(reply->IOCLogInfo), 1334 le32toh(reply->TransferLength)); 1335 goto done; 1336 } 1337 1338 /* 1339 * Post was successful. 1340 */ 1341 pBuffer->valid_data = TRUE; 1342 pBuffer->owned_by_firmware = TRUE; 1343 *return_code = MPR_FW_DIAG_ERROR_SUCCESS; 1344 status = MPR_DIAG_SUCCESS; 1345 1346 done: 1347 if (cm != NULL) 1348 mpr_free_command(sc, cm); 1349 return (status); 1350 } 1351 1352 static int 1353 mpr_release_fw_diag_buffer(struct mpr_softc *sc, 1354 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code, 1355 uint32_t diag_type) 1356 { 1357 MPI2_DIAG_RELEASE_REQUEST *req; 1358 MPI2_DIAG_RELEASE_REPLY *reply; 1359 struct mpr_command *cm = NULL; 1360 int status; 1361 1362 /* 1363 * If buffer is not enabled, just leave. 1364 */ 1365 *return_code = MPR_FW_DIAG_ERROR_RELEASE_FAILED; 1366 if (!pBuffer->enabled) { 1367 mpr_dprint(sc, MPR_USER, "%s: This buffer type is not " 1368 "supported by the IOC", __func__); 1369 return (MPR_DIAG_FAILURE); 1370 } 1371 1372 /* 1373 * Clear some flags initially. 1374 */ 1375 pBuffer->force_release = FALSE; 1376 pBuffer->valid_data = FALSE; 1377 pBuffer->owned_by_firmware = FALSE; 1378 1379 /* 1380 * Get a command. 1381 */ 1382 cm = mpr_alloc_command(sc); 1383 if (cm == NULL) { 1384 mpr_printf(sc, "%s: no mpr requests\n", __func__); 1385 return (MPR_DIAG_FAILURE); 1386 } 1387 1388 /* 1389 * Build the request for releasing the FW Diag Buffer and send it. 1390 */ 1391 req = (MPI2_DIAG_RELEASE_REQUEST *)cm->cm_req; 1392 req->Function = MPI2_FUNCTION_DIAG_RELEASE; 1393 req->BufferType = pBuffer->buffer_type; 1394 cm->cm_data = NULL; 1395 cm->cm_length = 0; 1396 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 1397 cm->cm_complete_data = NULL; 1398 1399 /* 1400 * Send command synchronously. 1401 */ 1402 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP); 1403 if (status || (cm == NULL)) { 1404 mpr_printf(sc, "%s: invalid request: error %d\n", __func__, 1405 status); 1406 status = MPR_DIAG_FAILURE; 1407 goto done; 1408 } 1409 1410 /* 1411 * Process RELEASE reply. 1412 */ 1413 reply = (MPI2_DIAG_RELEASE_REPLY *)cm->cm_reply; 1414 if (reply == NULL) { 1415 mpr_printf(sc, "%s: reply is NULL, probably due to " 1416 "reinitialization", __func__); 1417 status = MPR_DIAG_FAILURE; 1418 goto done; 1419 } 1420 if (((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) != 1421 MPI2_IOCSTATUS_SUCCESS) || pBuffer->owned_by_firmware) { 1422 status = MPR_DIAG_FAILURE; 1423 mpr_dprint(sc, MPR_FAULT, "%s: release of FW Diag Buffer " 1424 "failed with IOCStatus = 0x%x and IOCLogInfo = 0x%x\n", 1425 __func__, le16toh(reply->IOCStatus), 1426 le32toh(reply->IOCLogInfo)); 1427 goto done; 1428 } 1429 1430 /* 1431 * Release was successful. 1432 */ 1433 *return_code = MPR_FW_DIAG_ERROR_SUCCESS; 1434 status = MPR_DIAG_SUCCESS; 1435 1436 /* 1437 * If this was for an UNREGISTER diag type command, clear the unique ID. 1438 */ 1439 if (diag_type == MPR_FW_DIAG_TYPE_UNREGISTER) { 1440 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID; 1441 } 1442 1443 done: 1444 if (cm != NULL) 1445 mpr_free_command(sc, cm); 1446 1447 return (status); 1448 } 1449 1450 static int 1451 mpr_diag_register(struct mpr_softc *sc, mpr_fw_diag_register_t *diag_register, 1452 uint32_t *return_code) 1453 { 1454 mpr_fw_diagnostic_buffer_t *pBuffer; 1455 struct mpr_busdma_context *ctx; 1456 uint8_t extended_type, buffer_type, i; 1457 uint32_t buffer_size; 1458 uint32_t unique_id; 1459 int status; 1460 int error; 1461 1462 extended_type = diag_register->ExtendedType; 1463 buffer_type = diag_register->BufferType; 1464 buffer_size = diag_register->RequestedBufferSize; 1465 unique_id = diag_register->UniqueId; 1466 ctx = NULL; 1467 error = 0; 1468 1469 /* 1470 * Check for valid buffer type 1471 */ 1472 if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) { 1473 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1474 return (MPR_DIAG_FAILURE); 1475 } 1476 1477 /* 1478 * Get the current buffer and look up the unique ID. The unique ID 1479 * should not be found. If it is, the ID is already in use. 1480 */ 1481 i = mpr_get_fw_diag_buffer_number(sc, unique_id); 1482 pBuffer = &sc->fw_diag_buffer_list[buffer_type]; 1483 if (i != MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) { 1484 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1485 return (MPR_DIAG_FAILURE); 1486 } 1487 1488 /* 1489 * The buffer's unique ID should not be registered yet, and the given 1490 * unique ID cannot be 0. 1491 */ 1492 if ((pBuffer->unique_id != MPR_FW_DIAG_INVALID_UID) || 1493 (unique_id == MPR_FW_DIAG_INVALID_UID)) { 1494 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1495 return (MPR_DIAG_FAILURE); 1496 } 1497 1498 /* 1499 * If this buffer is already posted as immediate, just change owner. 1500 */ 1501 if (pBuffer->immediate && pBuffer->owned_by_firmware && 1502 (pBuffer->unique_id == MPR_FW_DIAG_INVALID_UID)) { 1503 pBuffer->immediate = FALSE; 1504 pBuffer->unique_id = unique_id; 1505 return (MPR_DIAG_SUCCESS); 1506 } 1507 1508 /* 1509 * Post a new buffer after checking if it's enabled. The DMA buffer 1510 * that is allocated will be contiguous (nsegments = 1). 1511 */ 1512 if (!pBuffer->enabled) { 1513 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1514 return (MPR_DIAG_FAILURE); 1515 } 1516 if (bus_dma_tag_create( sc->mpr_parent_dmat, /* parent */ 1517 1, 0, /* algnmnt, boundary */ 1518 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 1519 BUS_SPACE_MAXADDR, /* highaddr */ 1520 NULL, NULL, /* filter, filterarg */ 1521 buffer_size, /* maxsize */ 1522 1, /* nsegments */ 1523 buffer_size, /* maxsegsize */ 1524 0, /* flags */ 1525 NULL, NULL, /* lockfunc, lockarg */ 1526 &sc->fw_diag_dmat)) { 1527 mpr_dprint(sc, MPR_ERROR, 1528 "Cannot allocate FW diag buffer DMA tag\n"); 1529 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1530 status = MPR_DIAG_FAILURE; 1531 goto bailout; 1532 } 1533 if (bus_dmamem_alloc(sc->fw_diag_dmat, (void **)&sc->fw_diag_buffer, 1534 BUS_DMA_NOWAIT, &sc->fw_diag_map)) { 1535 mpr_dprint(sc, MPR_ERROR, 1536 "Cannot allocate FW diag buffer memory\n"); 1537 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1538 status = MPR_DIAG_FAILURE; 1539 goto bailout; 1540 } 1541 bzero(sc->fw_diag_buffer, buffer_size); 1542 1543 ctx = malloc(sizeof(*ctx), M_MPR, M_WAITOK | M_ZERO); 1544 if (ctx == NULL) { 1545 device_printf(sc->mpr_dev, "%s: context malloc failed\n", 1546 __func__); 1547 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1548 status = MPR_DIAG_FAILURE; 1549 goto bailout; 1550 } 1551 ctx->addr = &sc->fw_diag_busaddr; 1552 ctx->buffer_dmat = sc->fw_diag_dmat; 1553 ctx->buffer_dmamap = sc->fw_diag_map; 1554 ctx->softc = sc; 1555 error = bus_dmamap_load(sc->fw_diag_dmat, sc->fw_diag_map, 1556 sc->fw_diag_buffer, buffer_size, mpr_memaddr_wait_cb, 1557 ctx, 0); 1558 if (error == EINPROGRESS) { 1559 1560 /* XXX KDM */ 1561 device_printf(sc->mpr_dev, "%s: Deferred bus_dmamap_load\n", 1562 __func__); 1563 /* 1564 * Wait for the load to complete. If we're interrupted, 1565 * bail out. 1566 */ 1567 mpr_lock(sc); 1568 if (ctx->completed == 0) { 1569 error = msleep(ctx, &sc->mpr_mtx, PCATCH, "mprwait", 0); 1570 if (error != 0) { 1571 /* 1572 * We got an error from msleep(9). This is 1573 * most likely due to a signal. Tell 1574 * mpr_memaddr_wait_cb() that we've abandoned 1575 * the context, so it needs to clean up when 1576 * it is called. 1577 */ 1578 ctx->abandoned = 1; 1579 1580 /* The callback will free this memory */ 1581 ctx = NULL; 1582 mpr_unlock(sc); 1583 1584 device_printf(sc->mpr_dev, "Cannot " 1585 "bus_dmamap_load FW diag buffer, error = " 1586 "%d returned from msleep\n", error); 1587 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1588 status = MPR_DIAG_FAILURE; 1589 goto bailout; 1590 } 1591 } 1592 mpr_unlock(sc); 1593 } 1594 1595 if ((error != 0) || (ctx->error != 0)) { 1596 device_printf(sc->mpr_dev, "Cannot bus_dmamap_load FW diag " 1597 "buffer, %serror = %d\n", error ? "" : "callback ", 1598 error ? error : ctx->error); 1599 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER; 1600 status = MPR_DIAG_FAILURE; 1601 goto bailout; 1602 } 1603 1604 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map, BUS_DMASYNC_PREREAD); 1605 1606 pBuffer->size = buffer_size; 1607 1608 /* 1609 * Copy the given info to the diag buffer and post the buffer. 1610 */ 1611 pBuffer->buffer_type = buffer_type; 1612 pBuffer->immediate = FALSE; 1613 if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) { 1614 for (i = 0; i < (sizeof (pBuffer->product_specific) / 4); 1615 i++) { 1616 pBuffer->product_specific[i] = 1617 diag_register->ProductSpecific[i]; 1618 } 1619 } 1620 pBuffer->extended_type = extended_type; 1621 pBuffer->unique_id = unique_id; 1622 status = mpr_post_fw_diag_buffer(sc, pBuffer, return_code); 1623 1624 bailout: 1625 1626 /* 1627 * In case there was a failure, free the DMA buffer. 1628 */ 1629 if (status == MPR_DIAG_FAILURE) { 1630 if (sc->fw_diag_busaddr != 0) { 1631 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map); 1632 sc->fw_diag_busaddr = 0; 1633 } 1634 if (sc->fw_diag_buffer != NULL) { 1635 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer, 1636 sc->fw_diag_map); 1637 sc->fw_diag_buffer = NULL; 1638 } 1639 if (sc->fw_diag_dmat != NULL) { 1640 bus_dma_tag_destroy(sc->fw_diag_dmat); 1641 sc->fw_diag_dmat = NULL; 1642 } 1643 } 1644 1645 if (ctx != NULL) 1646 free(ctx, M_MPR); 1647 1648 return (status); 1649 } 1650 1651 static int 1652 mpr_diag_unregister(struct mpr_softc *sc, 1653 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code) 1654 { 1655 mpr_fw_diagnostic_buffer_t *pBuffer; 1656 uint8_t i; 1657 uint32_t unique_id; 1658 int status; 1659 1660 unique_id = diag_unregister->UniqueId; 1661 1662 /* 1663 * Get the current buffer and look up the unique ID. The unique ID 1664 * should be there. 1665 */ 1666 i = mpr_get_fw_diag_buffer_number(sc, unique_id); 1667 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) { 1668 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1669 return (MPR_DIAG_FAILURE); 1670 } 1671 1672 pBuffer = &sc->fw_diag_buffer_list[i]; 1673 1674 /* 1675 * Try to release the buffer from FW before freeing it. If release 1676 * fails, don't free the DMA buffer in case FW tries to access it 1677 * later. If buffer is not owned by firmware, can't release it. 1678 */ 1679 if (!pBuffer->owned_by_firmware) { 1680 status = MPR_DIAG_SUCCESS; 1681 } else { 1682 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code, 1683 MPR_FW_DIAG_TYPE_UNREGISTER); 1684 } 1685 1686 /* 1687 * At this point, return the current status no matter what happens with 1688 * the DMA buffer. 1689 */ 1690 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID; 1691 if (status == MPR_DIAG_SUCCESS) { 1692 if (sc->fw_diag_busaddr != 0) { 1693 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map); 1694 sc->fw_diag_busaddr = 0; 1695 } 1696 if (sc->fw_diag_buffer != NULL) { 1697 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer, 1698 sc->fw_diag_map); 1699 sc->fw_diag_buffer = NULL; 1700 } 1701 if (sc->fw_diag_dmat != NULL) { 1702 bus_dma_tag_destroy(sc->fw_diag_dmat); 1703 sc->fw_diag_dmat = NULL; 1704 } 1705 } 1706 1707 return (status); 1708 } 1709 1710 static int 1711 mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query, 1712 uint32_t *return_code) 1713 { 1714 mpr_fw_diagnostic_buffer_t *pBuffer; 1715 uint8_t i; 1716 uint32_t unique_id; 1717 1718 unique_id = diag_query->UniqueId; 1719 1720 /* 1721 * If ID is valid, query on ID. 1722 * If ID is invalid, query on buffer type. 1723 */ 1724 if (unique_id == MPR_FW_DIAG_INVALID_UID) { 1725 i = diag_query->BufferType; 1726 if (i >= MPI2_DIAG_BUF_TYPE_COUNT) { 1727 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1728 return (MPR_DIAG_FAILURE); 1729 } 1730 } else { 1731 i = mpr_get_fw_diag_buffer_number(sc, unique_id); 1732 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) { 1733 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1734 return (MPR_DIAG_FAILURE); 1735 } 1736 } 1737 1738 /* 1739 * Fill query structure with the diag buffer info. 1740 */ 1741 pBuffer = &sc->fw_diag_buffer_list[i]; 1742 diag_query->BufferType = pBuffer->buffer_type; 1743 diag_query->ExtendedType = pBuffer->extended_type; 1744 if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) { 1745 for (i = 0; i < (sizeof(diag_query->ProductSpecific) / 4); 1746 i++) { 1747 diag_query->ProductSpecific[i] = 1748 pBuffer->product_specific[i]; 1749 } 1750 } 1751 diag_query->TotalBufferSize = pBuffer->size; 1752 diag_query->DriverAddedBufferSize = 0; 1753 diag_query->UniqueId = pBuffer->unique_id; 1754 diag_query->ApplicationFlags = 0; 1755 diag_query->DiagnosticFlags = 0; 1756 1757 /* 1758 * Set/Clear application flags 1759 */ 1760 if (pBuffer->immediate) { 1761 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_APP_OWNED; 1762 } else { 1763 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_APP_OWNED; 1764 } 1765 if (pBuffer->valid_data || pBuffer->owned_by_firmware) { 1766 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_BUFFER_VALID; 1767 } else { 1768 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_BUFFER_VALID; 1769 } 1770 if (pBuffer->owned_by_firmware) { 1771 diag_query->ApplicationFlags |= 1772 MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS; 1773 } else { 1774 diag_query->ApplicationFlags &= 1775 ~MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS; 1776 } 1777 1778 return (MPR_DIAG_SUCCESS); 1779 } 1780 1781 static int 1782 mpr_diag_read_buffer(struct mpr_softc *sc, 1783 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf, 1784 uint32_t *return_code) 1785 { 1786 mpr_fw_diagnostic_buffer_t *pBuffer; 1787 uint8_t i, *pData; 1788 uint32_t unique_id; 1789 int status; 1790 1791 unique_id = diag_read_buffer->UniqueId; 1792 1793 /* 1794 * Get the current buffer and look up the unique ID. The unique ID 1795 * should be there. 1796 */ 1797 i = mpr_get_fw_diag_buffer_number(sc, unique_id); 1798 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) { 1799 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1800 return (MPR_DIAG_FAILURE); 1801 } 1802 1803 pBuffer = &sc->fw_diag_buffer_list[i]; 1804 1805 /* 1806 * Make sure requested read is within limits 1807 */ 1808 if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead > 1809 pBuffer->size) { 1810 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1811 return (MPR_DIAG_FAILURE); 1812 } 1813 1814 /* Sync the DMA map before we copy to userland. */ 1815 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map, 1816 BUS_DMASYNC_POSTREAD); 1817 1818 /* 1819 * Copy the requested data from DMA to the diag_read_buffer. The DMA 1820 * buffer that was allocated is one contiguous buffer. 1821 */ 1822 pData = (uint8_t *)(sc->fw_diag_buffer + 1823 diag_read_buffer->StartingOffset); 1824 if (copyout(pData, ioctl_buf, diag_read_buffer->BytesToRead) != 0) 1825 return (MPR_DIAG_FAILURE); 1826 diag_read_buffer->Status = 0; 1827 1828 /* 1829 * Set or clear the Force Release flag. 1830 */ 1831 if (pBuffer->force_release) { 1832 diag_read_buffer->Flags |= MPR_FW_DIAG_FLAG_FORCE_RELEASE; 1833 } else { 1834 diag_read_buffer->Flags &= ~MPR_FW_DIAG_FLAG_FORCE_RELEASE; 1835 } 1836 1837 /* 1838 * If buffer is to be reregistered, make sure it's not already owned by 1839 * firmware first. 1840 */ 1841 status = MPR_DIAG_SUCCESS; 1842 if (!pBuffer->owned_by_firmware) { 1843 if (diag_read_buffer->Flags & MPR_FW_DIAG_FLAG_REREGISTER) { 1844 status = mpr_post_fw_diag_buffer(sc, pBuffer, 1845 return_code); 1846 } 1847 } 1848 1849 return (status); 1850 } 1851 1852 static int 1853 mpr_diag_release(struct mpr_softc *sc, mpr_fw_diag_release_t *diag_release, 1854 uint32_t *return_code) 1855 { 1856 mpr_fw_diagnostic_buffer_t *pBuffer; 1857 uint8_t i; 1858 uint32_t unique_id; 1859 int status; 1860 1861 unique_id = diag_release->UniqueId; 1862 1863 /* 1864 * Get the current buffer and look up the unique ID. The unique ID 1865 * should be there. 1866 */ 1867 i = mpr_get_fw_diag_buffer_number(sc, unique_id); 1868 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) { 1869 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID; 1870 return (MPR_DIAG_FAILURE); 1871 } 1872 1873 pBuffer = &sc->fw_diag_buffer_list[i]; 1874 1875 /* 1876 * If buffer is not owned by firmware, it's already been released. 1877 */ 1878 if (!pBuffer->owned_by_firmware) { 1879 *return_code = MPR_FW_DIAG_ERROR_ALREADY_RELEASED; 1880 return (MPR_DIAG_FAILURE); 1881 } 1882 1883 /* 1884 * Release the buffer. 1885 */ 1886 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code, 1887 MPR_FW_DIAG_TYPE_RELEASE); 1888 return (status); 1889 } 1890 1891 static int 1892 mpr_do_diag_action(struct mpr_softc *sc, uint32_t action, uint8_t *diag_action, 1893 uint32_t length, uint32_t *return_code) 1894 { 1895 mpr_fw_diag_register_t diag_register; 1896 mpr_fw_diag_unregister_t diag_unregister; 1897 mpr_fw_diag_query_t diag_query; 1898 mpr_diag_read_buffer_t diag_read_buffer; 1899 mpr_fw_diag_release_t diag_release; 1900 int status = MPR_DIAG_SUCCESS; 1901 uint32_t original_return_code; 1902 1903 original_return_code = *return_code; 1904 *return_code = MPR_FW_DIAG_ERROR_SUCCESS; 1905 1906 switch (action) { 1907 case MPR_FW_DIAG_TYPE_REGISTER: 1908 if (!length) { 1909 *return_code = 1910 MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1911 status = MPR_DIAG_FAILURE; 1912 break; 1913 } 1914 if (copyin(diag_action, &diag_register, 1915 sizeof(diag_register)) != 0) 1916 return (MPR_DIAG_FAILURE); 1917 status = mpr_diag_register(sc, &diag_register, 1918 return_code); 1919 break; 1920 1921 case MPR_FW_DIAG_TYPE_UNREGISTER: 1922 if (length < sizeof(diag_unregister)) { 1923 *return_code = 1924 MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1925 status = MPR_DIAG_FAILURE; 1926 break; 1927 } 1928 if (copyin(diag_action, &diag_unregister, 1929 sizeof(diag_unregister)) != 0) 1930 return (MPR_DIAG_FAILURE); 1931 status = mpr_diag_unregister(sc, &diag_unregister, 1932 return_code); 1933 break; 1934 1935 case MPR_FW_DIAG_TYPE_QUERY: 1936 if (length < sizeof (diag_query)) { 1937 *return_code = 1938 MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1939 status = MPR_DIAG_FAILURE; 1940 break; 1941 } 1942 if (copyin(diag_action, &diag_query, sizeof(diag_query)) 1943 != 0) 1944 return (MPR_DIAG_FAILURE); 1945 status = mpr_diag_query(sc, &diag_query, return_code); 1946 if (status == MPR_DIAG_SUCCESS) 1947 if (copyout(&diag_query, diag_action, 1948 sizeof (diag_query)) != 0) 1949 return (MPR_DIAG_FAILURE); 1950 break; 1951 1952 case MPR_FW_DIAG_TYPE_READ_BUFFER: 1953 if (copyin(diag_action, &diag_read_buffer, 1954 sizeof(diag_read_buffer)) != 0) 1955 return (MPR_DIAG_FAILURE); 1956 if (length < diag_read_buffer.BytesToRead) { 1957 *return_code = 1958 MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1959 status = MPR_DIAG_FAILURE; 1960 break; 1961 } 1962 status = mpr_diag_read_buffer(sc, &diag_read_buffer, 1963 PTRIN(diag_read_buffer.PtrDataBuffer), 1964 return_code); 1965 if (status == MPR_DIAG_SUCCESS) { 1966 if (copyout(&diag_read_buffer, diag_action, 1967 sizeof(diag_read_buffer) - 1968 sizeof(diag_read_buffer.PtrDataBuffer)) != 1969 0) 1970 return (MPR_DIAG_FAILURE); 1971 } 1972 break; 1973 1974 case MPR_FW_DIAG_TYPE_RELEASE: 1975 if (length < sizeof(diag_release)) { 1976 *return_code = 1977 MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1978 status = MPR_DIAG_FAILURE; 1979 break; 1980 } 1981 if (copyin(diag_action, &diag_release, 1982 sizeof(diag_release)) != 0) 1983 return (MPR_DIAG_FAILURE); 1984 status = mpr_diag_release(sc, &diag_release, 1985 return_code); 1986 break; 1987 1988 default: 1989 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER; 1990 status = MPR_DIAG_FAILURE; 1991 break; 1992 } 1993 1994 if ((status == MPR_DIAG_FAILURE) && 1995 (original_return_code == MPR_FW_DIAG_NEW) && 1996 (*return_code != MPR_FW_DIAG_ERROR_SUCCESS)) 1997 status = MPR_DIAG_SUCCESS; 1998 1999 return (status); 2000 } 2001 2002 static int 2003 mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data) 2004 { 2005 int status; 2006 2007 /* 2008 * Only allow one diag action at one time. 2009 */ 2010 if (sc->mpr_flags & MPR_FLAGS_BUSY) { 2011 mpr_dprint(sc, MPR_USER, "%s: Only one FW diag command " 2012 "allowed at a single time.", __func__); 2013 return (EBUSY); 2014 } 2015 sc->mpr_flags |= MPR_FLAGS_BUSY; 2016 2017 /* 2018 * Send diag action request 2019 */ 2020 if (data->Action == MPR_FW_DIAG_TYPE_REGISTER || 2021 data->Action == MPR_FW_DIAG_TYPE_UNREGISTER || 2022 data->Action == MPR_FW_DIAG_TYPE_QUERY || 2023 data->Action == MPR_FW_DIAG_TYPE_READ_BUFFER || 2024 data->Action == MPR_FW_DIAG_TYPE_RELEASE) { 2025 status = mpr_do_diag_action(sc, data->Action, 2026 PTRIN(data->PtrDiagAction), data->Length, 2027 &data->ReturnCode); 2028 } else 2029 status = EINVAL; 2030 2031 sc->mpr_flags &= ~MPR_FLAGS_BUSY; 2032 return (status); 2033 } 2034 2035 /* 2036 * Copy the event recording mask and the event queue size out. For 2037 * clarification, the event recording mask (events_to_record) is not the same 2038 * thing as the event mask (event_mask). events_to_record has a bit set for 2039 * every event type that is to be recorded by the driver, and event_mask has a 2040 * bit cleared for every event that is allowed into the driver from the IOC. 2041 * They really have nothing to do with each other. 2042 */ 2043 static void 2044 mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data) 2045 { 2046 uint8_t i; 2047 2048 mpr_lock(sc); 2049 data->Entries = MPR_EVENT_QUEUE_SIZE; 2050 2051 for (i = 0; i < 4; i++) { 2052 data->Types[i] = sc->events_to_record[i]; 2053 } 2054 mpr_unlock(sc); 2055 } 2056 2057 /* 2058 * Set the driver's event mask according to what's been given. See 2059 * mpr_user_event_query for explanation of the event recording mask and the IOC 2060 * event mask. It's the app's responsibility to enable event logging by setting 2061 * the bits in events_to_record. Initially, no events will be logged. 2062 */ 2063 static void 2064 mpr_user_event_enable(struct mpr_softc *sc, mpr_event_enable_t *data) 2065 { 2066 uint8_t i; 2067 2068 mpr_lock(sc); 2069 for (i = 0; i < 4; i++) { 2070 sc->events_to_record[i] = data->Types[i]; 2071 } 2072 mpr_unlock(sc); 2073 } 2074 2075 /* 2076 * Copy out the events that have been recorded, up to the max events allowed. 2077 */ 2078 static int 2079 mpr_user_event_report(struct mpr_softc *sc, mpr_event_report_t *data) 2080 { 2081 int status = 0; 2082 uint32_t size; 2083 2084 mpr_lock(sc); 2085 size = data->Size; 2086 if ((size >= sizeof(sc->recorded_events)) && (status == 0)) { 2087 mpr_unlock(sc); 2088 if (copyout((void *)sc->recorded_events, 2089 PTRIN(data->PtrEvents), size) != 0) 2090 status = EFAULT; 2091 mpr_lock(sc); 2092 } else { 2093 /* 2094 * data->Size value is not large enough to copy event data. 2095 */ 2096 status = EFAULT; 2097 } 2098 2099 /* 2100 * Change size value to match the number of bytes that were copied. 2101 */ 2102 if (status == 0) 2103 data->Size = sizeof(sc->recorded_events); 2104 mpr_unlock(sc); 2105 2106 return (status); 2107 } 2108 2109 /* 2110 * Record events into the driver from the IOC if they are not masked. 2111 */ 2112 void 2113 mprsas_record_event(struct mpr_softc *sc, 2114 MPI2_EVENT_NOTIFICATION_REPLY *event_reply) 2115 { 2116 uint32_t event; 2117 int i, j; 2118 uint16_t event_data_len; 2119 boolean_t sendAEN = FALSE; 2120 2121 event = event_reply->Event; 2122 2123 /* 2124 * Generate a system event to let anyone who cares know that a 2125 * LOG_ENTRY_ADDED event has occurred. This is sent no matter what the 2126 * event mask is set to. 2127 */ 2128 if (event == MPI2_EVENT_LOG_ENTRY_ADDED) { 2129 sendAEN = TRUE; 2130 } 2131 2132 /* 2133 * Record the event only if its corresponding bit is set in 2134 * events_to_record. event_index is the index into recorded_events and 2135 * event_number is the overall number of an event being recorded since 2136 * start-of-day. event_index will roll over; event_number will never 2137 * roll over. 2138 */ 2139 i = (uint8_t)(event / 32); 2140 j = (uint8_t)(event % 32); 2141 if ((i < 4) && ((1 << j) & sc->events_to_record[i])) { 2142 i = sc->event_index; 2143 sc->recorded_events[i].Type = event; 2144 sc->recorded_events[i].Number = ++sc->event_number; 2145 bzero(sc->recorded_events[i].Data, MPR_MAX_EVENT_DATA_LENGTH * 2146 4); 2147 event_data_len = event_reply->EventDataLength; 2148 2149 if (event_data_len > 0) { 2150 /* 2151 * Limit data to size in m_event entry 2152 */ 2153 if (event_data_len > MPR_MAX_EVENT_DATA_LENGTH) { 2154 event_data_len = MPR_MAX_EVENT_DATA_LENGTH; 2155 } 2156 for (j = 0; j < event_data_len; j++) { 2157 sc->recorded_events[i].Data[j] = 2158 event_reply->EventData[j]; 2159 } 2160 2161 /* 2162 * check for index wrap-around 2163 */ 2164 if (++i == MPR_EVENT_QUEUE_SIZE) { 2165 i = 0; 2166 } 2167 sc->event_index = (uint8_t)i; 2168 2169 /* 2170 * Set flag to send the event. 2171 */ 2172 sendAEN = TRUE; 2173 } 2174 } 2175 2176 /* 2177 * Generate a system event if flag is set to let anyone who cares know 2178 * that an event has occurred. 2179 */ 2180 if (sendAEN) { 2181 //SLM-how to send a system event (see kqueue, kevent) 2182 // (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS", 2183 // "SAS", NULL, NULL, DDI_NOSLEEP); 2184 } 2185 } 2186 2187 static int 2188 mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data) 2189 { 2190 int status = 0; 2191 2192 switch (data->Command) { 2193 /* 2194 * IO access is not supported. 2195 */ 2196 case REG_IO_READ: 2197 case REG_IO_WRITE: 2198 mpr_dprint(sc, MPR_USER, "IO access is not supported. " 2199 "Use memory access."); 2200 status = EINVAL; 2201 break; 2202 2203 case REG_MEM_READ: 2204 data->RegData = mpr_regread(sc, data->RegOffset); 2205 break; 2206 2207 case REG_MEM_WRITE: 2208 mpr_regwrite(sc, data->RegOffset, data->RegData); 2209 break; 2210 2211 default: 2212 status = EINVAL; 2213 break; 2214 } 2215 2216 return (status); 2217 } 2218 2219 static int 2220 mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data) 2221 { 2222 uint8_t bt2dh = FALSE; 2223 uint8_t dh2bt = FALSE; 2224 uint16_t dev_handle, bus, target; 2225 2226 bus = data->Bus; 2227 target = data->TargetID; 2228 dev_handle = data->DevHandle; 2229 2230 /* 2231 * When DevHandle is 0xFFFF and Bus/Target are not 0xFFFF, use Bus/ 2232 * Target to get DevHandle. When Bus/Target are 0xFFFF and DevHandle is 2233 * not 0xFFFF, use DevHandle to get Bus/Target. Anything else is 2234 * invalid. 2235 */ 2236 if ((bus == 0xFFFF) && (target == 0xFFFF) && (dev_handle != 0xFFFF)) 2237 dh2bt = TRUE; 2238 if ((dev_handle == 0xFFFF) && (bus != 0xFFFF) && (target != 0xFFFF)) 2239 bt2dh = TRUE; 2240 if (!dh2bt && !bt2dh) 2241 return (EINVAL); 2242 2243 /* 2244 * Only handle bus of 0. Make sure target is within range. 2245 */ 2246 if (bt2dh) { 2247 if (bus != 0) 2248 return (EINVAL); 2249 2250 if (target > sc->max_devices) { 2251 mpr_dprint(sc, MPR_XINFO, "Target ID is out of range " 2252 "for Bus/Target to DevHandle mapping."); 2253 return (EINVAL); 2254 } 2255 dev_handle = sc->mapping_table[target].dev_handle; 2256 if (dev_handle) 2257 data->DevHandle = dev_handle; 2258 } else { 2259 bus = 0; 2260 target = mpr_mapping_get_tid_from_handle(sc, dev_handle); 2261 data->Bus = bus; 2262 data->TargetID = target; 2263 } 2264 2265 return (0); 2266 } 2267 2268 static int 2269 mpr_ioctl(struct cdev *dev, u_long cmd, void *arg, int flag, 2270 struct thread *td) 2271 { 2272 struct mpr_softc *sc; 2273 struct mpr_cfg_page_req *page_req; 2274 struct mpr_ext_cfg_page_req *ext_page_req; 2275 void *mpr_page; 2276 int error, msleep_ret; 2277 2278 mpr_page = NULL; 2279 sc = dev->si_drv1; 2280 page_req = (void *)arg; 2281 ext_page_req = (void *)arg; 2282 2283 switch (cmd) { 2284 case MPRIO_READ_CFG_HEADER: 2285 mpr_lock(sc); 2286 error = mpr_user_read_cfg_header(sc, page_req); 2287 mpr_unlock(sc); 2288 break; 2289 case MPRIO_READ_CFG_PAGE: 2290 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK | M_ZERO); 2291 error = copyin(page_req->buf, mpr_page, 2292 sizeof(MPI2_CONFIG_PAGE_HEADER)); 2293 if (error) 2294 break; 2295 mpr_lock(sc); 2296 error = mpr_user_read_cfg_page(sc, page_req, mpr_page); 2297 mpr_unlock(sc); 2298 if (error) 2299 break; 2300 error = copyout(mpr_page, page_req->buf, page_req->len); 2301 break; 2302 case MPRIO_READ_EXT_CFG_HEADER: 2303 mpr_lock(sc); 2304 error = mpr_user_read_extcfg_header(sc, ext_page_req); 2305 mpr_unlock(sc); 2306 break; 2307 case MPRIO_READ_EXT_CFG_PAGE: 2308 mpr_page = malloc(ext_page_req->len, M_MPRUSER, 2309 M_WAITOK | M_ZERO); 2310 error = copyin(ext_page_req->buf, mpr_page, 2311 sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER)); 2312 if (error) 2313 break; 2314 mpr_lock(sc); 2315 error = mpr_user_read_extcfg_page(sc, ext_page_req, mpr_page); 2316 mpr_unlock(sc); 2317 if (error) 2318 break; 2319 error = copyout(mpr_page, ext_page_req->buf, ext_page_req->len); 2320 break; 2321 case MPRIO_WRITE_CFG_PAGE: 2322 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK|M_ZERO); 2323 error = copyin(page_req->buf, mpr_page, page_req->len); 2324 if (error) 2325 break; 2326 mpr_lock(sc); 2327 error = mpr_user_write_cfg_page(sc, page_req, mpr_page); 2328 mpr_unlock(sc); 2329 break; 2330 case MPRIO_MPR_COMMAND: 2331 error = mpr_user_command(sc, (struct mpr_usr_command *)arg); 2332 break; 2333 case MPTIOCTL_PASS_THRU: 2334 /* 2335 * The user has requested to pass through a command to be 2336 * executed by the MPT firmware. Call our routine which does 2337 * this. Only allow one passthru IOCTL at one time. 2338 */ 2339 error = mpr_user_pass_thru(sc, (mpr_pass_thru_t *)arg); 2340 break; 2341 case MPTIOCTL_GET_ADAPTER_DATA: 2342 /* 2343 * The user has requested to read adapter data. Call our 2344 * routine which does this. 2345 */ 2346 error = 0; 2347 mpr_user_get_adapter_data(sc, (mpr_adapter_data_t *)arg); 2348 break; 2349 case MPTIOCTL_GET_PCI_INFO: 2350 /* 2351 * The user has requested to read pci info. Call 2352 * our routine which does this. 2353 */ 2354 mpr_lock(sc); 2355 error = 0; 2356 mpr_user_read_pci_info(sc, (mpr_pci_info_t *)arg); 2357 mpr_unlock(sc); 2358 break; 2359 case MPTIOCTL_RESET_ADAPTER: 2360 mpr_lock(sc); 2361 sc->port_enable_complete = 0; 2362 uint32_t reinit_start = time_uptime; 2363 error = mpr_reinit(sc); 2364 /* Sleep for 300 second. */ 2365 msleep_ret = msleep(&sc->port_enable_complete, &sc->mpr_mtx, 2366 PRIBIO, "mpr_porten", 300 * hz); 2367 mpr_unlock(sc); 2368 if (msleep_ret) 2369 printf("Port Enable did not complete after Diag " 2370 "Reset msleep error %d.\n", msleep_ret); 2371 else 2372 mpr_dprint(sc, MPR_USER, "Hard Reset with Port Enable " 2373 "completed in %d seconds.\n", 2374 (uint32_t)(time_uptime - reinit_start)); 2375 break; 2376 case MPTIOCTL_DIAG_ACTION: 2377 /* 2378 * The user has done a diag buffer action. Call our routine 2379 * which does this. Only allow one diag action at one time. 2380 */ 2381 mpr_lock(sc); 2382 error = mpr_user_diag_action(sc, (mpr_diag_action_t *)arg); 2383 mpr_unlock(sc); 2384 break; 2385 case MPTIOCTL_EVENT_QUERY: 2386 /* 2387 * The user has done an event query. Call our routine which does 2388 * this. 2389 */ 2390 error = 0; 2391 mpr_user_event_query(sc, (mpr_event_query_t *)arg); 2392 break; 2393 case MPTIOCTL_EVENT_ENABLE: 2394 /* 2395 * The user has done an event enable. Call our routine which 2396 * does this. 2397 */ 2398 error = 0; 2399 mpr_user_event_enable(sc, (mpr_event_enable_t *)arg); 2400 break; 2401 case MPTIOCTL_EVENT_REPORT: 2402 /* 2403 * The user has done an event report. Call our routine which 2404 * does this. 2405 */ 2406 error = mpr_user_event_report(sc, (mpr_event_report_t *)arg); 2407 break; 2408 case MPTIOCTL_REG_ACCESS: 2409 /* 2410 * The user has requested register access. Call our routine 2411 * which does this. 2412 */ 2413 mpr_lock(sc); 2414 error = mpr_user_reg_access(sc, (mpr_reg_access_t *)arg); 2415 mpr_unlock(sc); 2416 break; 2417 case MPTIOCTL_BTDH_MAPPING: 2418 /* 2419 * The user has requested to translate a bus/target to a 2420 * DevHandle or a DevHandle to a bus/target. Call our routine 2421 * which does this. 2422 */ 2423 error = mpr_user_btdh(sc, (mpr_btdh_mapping_t *)arg); 2424 break; 2425 default: 2426 error = ENOIOCTL; 2427 break; 2428 } 2429 2430 if (mpr_page != NULL) 2431 free(mpr_page, M_MPRUSER); 2432 2433 return (error); 2434 } 2435 2436 #ifdef COMPAT_FREEBSD32 2437 2438 struct mpr_cfg_page_req32 { 2439 MPI2_CONFIG_PAGE_HEADER header; 2440 uint32_t page_address; 2441 uint32_t buf; 2442 int len; 2443 uint16_t ioc_status; 2444 }; 2445 2446 struct mpr_ext_cfg_page_req32 { 2447 MPI2_CONFIG_EXTENDED_PAGE_HEADER header; 2448 uint32_t page_address; 2449 uint32_t buf; 2450 int len; 2451 uint16_t ioc_status; 2452 }; 2453 2454 struct mpr_raid_action32 { 2455 uint8_t action; 2456 uint8_t volume_bus; 2457 uint8_t volume_id; 2458 uint8_t phys_disk_num; 2459 uint32_t action_data_word; 2460 uint32_t buf; 2461 int len; 2462 uint32_t volume_status; 2463 uint32_t action_data[4]; 2464 uint16_t action_status; 2465 uint16_t ioc_status; 2466 uint8_t write; 2467 }; 2468 2469 struct mpr_usr_command32 { 2470 uint32_t req; 2471 uint32_t req_len; 2472 uint32_t rpl; 2473 uint32_t rpl_len; 2474 uint32_t buf; 2475 int len; 2476 uint32_t flags; 2477 }; 2478 2479 #define MPRIO_READ_CFG_HEADER32 _IOWR('M', 200, struct mpr_cfg_page_req32) 2480 #define MPRIO_READ_CFG_PAGE32 _IOWR('M', 201, struct mpr_cfg_page_req32) 2481 #define MPRIO_READ_EXT_CFG_HEADER32 _IOWR('M', 202, struct mpr_ext_cfg_page_req32) 2482 #define MPRIO_READ_EXT_CFG_PAGE32 _IOWR('M', 203, struct mpr_ext_cfg_page_req32) 2483 #define MPRIO_WRITE_CFG_PAGE32 _IOWR('M', 204, struct mpr_cfg_page_req32) 2484 #define MPRIO_RAID_ACTION32 _IOWR('M', 205, struct mpr_raid_action32) 2485 #define MPRIO_MPR_COMMAND32 _IOWR('M', 210, struct mpr_usr_command32) 2486 2487 static int 2488 mpr_ioctl32(struct cdev *dev, u_long cmd32, void *_arg, int flag, 2489 struct thread *td) 2490 { 2491 struct mpr_cfg_page_req32 *page32 = _arg; 2492 struct mpr_ext_cfg_page_req32 *ext32 = _arg; 2493 struct mpr_raid_action32 *raid32 = _arg; 2494 struct mpr_usr_command32 *user32 = _arg; 2495 union { 2496 struct mpr_cfg_page_req page; 2497 struct mpr_ext_cfg_page_req ext; 2498 struct mpr_raid_action raid; 2499 struct mpr_usr_command user; 2500 } arg; 2501 u_long cmd; 2502 int error; 2503 2504 switch (cmd32) { 2505 case MPRIO_READ_CFG_HEADER32: 2506 case MPRIO_READ_CFG_PAGE32: 2507 case MPRIO_WRITE_CFG_PAGE32: 2508 if (cmd32 == MPRIO_READ_CFG_HEADER32) 2509 cmd = MPRIO_READ_CFG_HEADER; 2510 else if (cmd32 == MPRIO_READ_CFG_PAGE32) 2511 cmd = MPRIO_READ_CFG_PAGE; 2512 else 2513 cmd = MPRIO_WRITE_CFG_PAGE; 2514 CP(*page32, arg.page, header); 2515 CP(*page32, arg.page, page_address); 2516 PTRIN_CP(*page32, arg.page, buf); 2517 CP(*page32, arg.page, len); 2518 CP(*page32, arg.page, ioc_status); 2519 break; 2520 2521 case MPRIO_READ_EXT_CFG_HEADER32: 2522 case MPRIO_READ_EXT_CFG_PAGE32: 2523 if (cmd32 == MPRIO_READ_EXT_CFG_HEADER32) 2524 cmd = MPRIO_READ_EXT_CFG_HEADER; 2525 else 2526 cmd = MPRIO_READ_EXT_CFG_PAGE; 2527 CP(*ext32, arg.ext, header); 2528 CP(*ext32, arg.ext, page_address); 2529 PTRIN_CP(*ext32, arg.ext, buf); 2530 CP(*ext32, arg.ext, len); 2531 CP(*ext32, arg.ext, ioc_status); 2532 break; 2533 2534 case MPRIO_RAID_ACTION32: 2535 cmd = MPRIO_RAID_ACTION; 2536 CP(*raid32, arg.raid, action); 2537 CP(*raid32, arg.raid, volume_bus); 2538 CP(*raid32, arg.raid, volume_id); 2539 CP(*raid32, arg.raid, phys_disk_num); 2540 CP(*raid32, arg.raid, action_data_word); 2541 PTRIN_CP(*raid32, arg.raid, buf); 2542 CP(*raid32, arg.raid, len); 2543 CP(*raid32, arg.raid, volume_status); 2544 bcopy(raid32->action_data, arg.raid.action_data, 2545 sizeof arg.raid.action_data); 2546 CP(*raid32, arg.raid, ioc_status); 2547 CP(*raid32, arg.raid, write); 2548 break; 2549 2550 case MPRIO_MPR_COMMAND32: 2551 cmd = MPRIO_MPR_COMMAND; 2552 PTRIN_CP(*user32, arg.user, req); 2553 CP(*user32, arg.user, req_len); 2554 PTRIN_CP(*user32, arg.user, rpl); 2555 CP(*user32, arg.user, rpl_len); 2556 PTRIN_CP(*user32, arg.user, buf); 2557 CP(*user32, arg.user, len); 2558 CP(*user32, arg.user, flags); 2559 break; 2560 default: 2561 return (ENOIOCTL); 2562 } 2563 2564 error = mpr_ioctl(dev, cmd, &arg, flag, td); 2565 if (error == 0 && (cmd32 & IOC_OUT) != 0) { 2566 switch (cmd32) { 2567 case MPRIO_READ_CFG_HEADER32: 2568 case MPRIO_READ_CFG_PAGE32: 2569 case MPRIO_WRITE_CFG_PAGE32: 2570 CP(arg.page, *page32, header); 2571 CP(arg.page, *page32, page_address); 2572 PTROUT_CP(arg.page, *page32, buf); 2573 CP(arg.page, *page32, len); 2574 CP(arg.page, *page32, ioc_status); 2575 break; 2576 2577 case MPRIO_READ_EXT_CFG_HEADER32: 2578 case MPRIO_READ_EXT_CFG_PAGE32: 2579 CP(arg.ext, *ext32, header); 2580 CP(arg.ext, *ext32, page_address); 2581 PTROUT_CP(arg.ext, *ext32, buf); 2582 CP(arg.ext, *ext32, len); 2583 CP(arg.ext, *ext32, ioc_status); 2584 break; 2585 2586 case MPRIO_RAID_ACTION32: 2587 CP(arg.raid, *raid32, action); 2588 CP(arg.raid, *raid32, volume_bus); 2589 CP(arg.raid, *raid32, volume_id); 2590 CP(arg.raid, *raid32, phys_disk_num); 2591 CP(arg.raid, *raid32, action_data_word); 2592 PTROUT_CP(arg.raid, *raid32, buf); 2593 CP(arg.raid, *raid32, len); 2594 CP(arg.raid, *raid32, volume_status); 2595 bcopy(arg.raid.action_data, raid32->action_data, 2596 sizeof arg.raid.action_data); 2597 CP(arg.raid, *raid32, ioc_status); 2598 CP(arg.raid, *raid32, write); 2599 break; 2600 2601 case MPRIO_MPR_COMMAND32: 2602 PTROUT_CP(arg.user, *user32, req); 2603 CP(arg.user, *user32, req_len); 2604 PTROUT_CP(arg.user, *user32, rpl); 2605 CP(arg.user, *user32, rpl_len); 2606 PTROUT_CP(arg.user, *user32, buf); 2607 CP(arg.user, *user32, len); 2608 CP(arg.user, *user32, flags); 2609 break; 2610 } 2611 } 2612 2613 return (error); 2614 } 2615 #endif /* COMPAT_FREEBSD32 */ 2616 2617 static int 2618 mpr_ioctl_devsw(struct cdev *dev, u_long com, caddr_t arg, int flag, 2619 struct thread *td) 2620 { 2621 #ifdef COMPAT_FREEBSD32 2622 if (SV_CURPROC_FLAG(SV_ILP32)) 2623 return (mpr_ioctl32(dev, com, arg, flag, td)); 2624 #endif 2625 return (mpr_ioctl(dev, com, arg, flag, td)); 2626 } 2627