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