1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved. 4 * Intel Management Engine Interface (Intel MEI) Linux driver 5 */ 6 #include <linux/export.h> 7 #include <linux/sched.h> 8 #include <linux/wait.h> 9 #include <linux/pm_runtime.h> 10 #include <linux/slab.h> 11 12 #include <linux/mei.h> 13 14 #include "mei_dev.h" 15 #include "hbm.h" 16 #include "client.h" 17 18 static const char *mei_hbm_status_str(enum mei_hbm_status status) 19 { 20 #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status 21 switch (status) { 22 MEI_HBM_STATUS(SUCCESS); 23 MEI_HBM_STATUS(CLIENT_NOT_FOUND); 24 MEI_HBM_STATUS(ALREADY_EXISTS); 25 MEI_HBM_STATUS(REJECTED); 26 MEI_HBM_STATUS(INVALID_PARAMETER); 27 MEI_HBM_STATUS(NOT_ALLOWED); 28 MEI_HBM_STATUS(ALREADY_STARTED); 29 MEI_HBM_STATUS(NOT_STARTED); 30 default: return "unknown"; 31 } 32 #undef MEI_HBM_STATUS 33 }; 34 35 static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status) 36 { 37 #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status 38 switch (status) { 39 MEI_CL_CS(SUCCESS); 40 MEI_CL_CS(NOT_FOUND); 41 MEI_CL_CS(ALREADY_STARTED); 42 MEI_CL_CS(OUT_OF_RESOURCES); 43 MEI_CL_CS(MESSAGE_SMALL); 44 MEI_CL_CS(NOT_ALLOWED); 45 default: return "unknown"; 46 } 47 #undef MEI_CL_CCS 48 } 49 50 const char *mei_hbm_state_str(enum mei_hbm_state state) 51 { 52 #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state 53 switch (state) { 54 MEI_HBM_STATE(IDLE); 55 MEI_HBM_STATE(STARTING); 56 MEI_HBM_STATE(STARTED); 57 MEI_HBM_STATE(DR_SETUP); 58 MEI_HBM_STATE(ENUM_CLIENTS); 59 MEI_HBM_STATE(CLIENT_PROPERTIES); 60 MEI_HBM_STATE(STOPPED); 61 default: 62 return "unknown"; 63 } 64 #undef MEI_HBM_STATE 65 } 66 67 /** 68 * mei_cl_conn_status_to_errno - convert client connect response 69 * status to error code 70 * 71 * @status: client connect response status 72 * 73 * Return: corresponding error code 74 */ 75 static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status) 76 { 77 switch (status) { 78 case MEI_CL_CONN_SUCCESS: return 0; 79 case MEI_CL_CONN_NOT_FOUND: return -ENOTTY; 80 case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY; 81 case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY; 82 case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL; 83 case MEI_CL_CONN_NOT_ALLOWED: return -EBUSY; 84 default: return -EINVAL; 85 } 86 } 87 88 /** 89 * mei_hbm_write_message - wrapper for sending hbm messages. 90 * 91 * @dev: mei device 92 * @hdr: mei header 93 * @data: payload 94 */ 95 static inline int mei_hbm_write_message(struct mei_device *dev, 96 struct mei_msg_hdr *hdr, 97 const void *data) 98 { 99 return mei_write_message(dev, hdr, sizeof(*hdr), data, hdr->length); 100 } 101 102 /** 103 * mei_hbm_idle - set hbm to idle state 104 * 105 * @dev: the device structure 106 */ 107 void mei_hbm_idle(struct mei_device *dev) 108 { 109 dev->init_clients_timer = 0; 110 dev->hbm_state = MEI_HBM_IDLE; 111 } 112 113 /** 114 * mei_hbm_reset - reset hbm counters and book keeping data structurs 115 * 116 * @dev: the device structure 117 */ 118 void mei_hbm_reset(struct mei_device *dev) 119 { 120 mei_me_cl_rm_all(dev); 121 122 mei_hbm_idle(dev); 123 } 124 125 /** 126 * mei_hbm_hdr - construct hbm header 127 * 128 * @hdr: hbm header 129 * @length: payload length 130 */ 131 132 static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length) 133 { 134 hdr->host_addr = 0; 135 hdr->me_addr = 0; 136 hdr->length = length; 137 hdr->msg_complete = 1; 138 hdr->dma_ring = 0; 139 hdr->reserved = 0; 140 hdr->internal = 0; 141 } 142 143 /** 144 * mei_hbm_cl_hdr - construct client hbm header 145 * 146 * @cl: client 147 * @hbm_cmd: host bus message command 148 * @buf: buffer for cl header 149 * @len: buffer length 150 */ 151 static inline 152 void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len) 153 { 154 struct mei_hbm_cl_cmd *cmd = buf; 155 156 memset(cmd, 0, len); 157 158 cmd->hbm_cmd = hbm_cmd; 159 cmd->host_addr = mei_cl_host_addr(cl); 160 cmd->me_addr = mei_cl_me_id(cl); 161 } 162 163 /** 164 * mei_hbm_cl_write - write simple hbm client message 165 * 166 * @dev: the device structure 167 * @cl: client 168 * @hbm_cmd: host bus message command 169 * @buf: message buffer 170 * @len: buffer length 171 * 172 * Return: 0 on success, <0 on failure. 173 */ 174 static inline int mei_hbm_cl_write(struct mei_device *dev, struct mei_cl *cl, 175 u8 hbm_cmd, void *buf, size_t len) 176 { 177 struct mei_msg_hdr mei_hdr; 178 179 mei_hbm_hdr(&mei_hdr, len); 180 mei_hbm_cl_hdr(cl, hbm_cmd, buf, len); 181 182 return mei_hbm_write_message(dev, &mei_hdr, buf); 183 } 184 185 /** 186 * mei_hbm_cl_addr_equal - check if the client's and 187 * the message address match 188 * 189 * @cl: client 190 * @cmd: hbm client message 191 * 192 * Return: true if addresses are the same 193 */ 194 static inline 195 bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd) 196 { 197 return mei_cl_host_addr(cl) == cmd->host_addr && 198 mei_cl_me_id(cl) == cmd->me_addr; 199 } 200 201 /** 202 * mei_hbm_cl_find_by_cmd - find recipient client 203 * 204 * @dev: the device structure 205 * @buf: a buffer with hbm cl command 206 * 207 * Return: the recipient client or NULL if not found 208 */ 209 static inline 210 struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf) 211 { 212 struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf; 213 struct mei_cl *cl; 214 215 list_for_each_entry(cl, &dev->file_list, link) 216 if (mei_hbm_cl_addr_equal(cl, cmd)) 217 return cl; 218 return NULL; 219 } 220 221 222 /** 223 * mei_hbm_start_wait - wait for start response message. 224 * 225 * @dev: the device structure 226 * 227 * Return: 0 on success and < 0 on failure 228 */ 229 int mei_hbm_start_wait(struct mei_device *dev) 230 { 231 int ret; 232 233 if (dev->hbm_state > MEI_HBM_STARTING) 234 return 0; 235 236 mutex_unlock(&dev->device_lock); 237 ret = wait_event_timeout(dev->wait_hbm_start, 238 dev->hbm_state != MEI_HBM_STARTING, 239 mei_secs_to_jiffies(MEI_HBM_TIMEOUT)); 240 mutex_lock(&dev->device_lock); 241 242 if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) { 243 dev->hbm_state = MEI_HBM_IDLE; 244 dev_err(dev->dev, "waiting for mei start failed\n"); 245 return -ETIME; 246 } 247 return 0; 248 } 249 250 /** 251 * mei_hbm_start_req - sends start request message. 252 * 253 * @dev: the device structure 254 * 255 * Return: 0 on success and < 0 on failure 256 */ 257 int mei_hbm_start_req(struct mei_device *dev) 258 { 259 struct mei_msg_hdr mei_hdr; 260 struct hbm_host_version_request req; 261 int ret; 262 263 mei_hbm_reset(dev); 264 265 mei_hbm_hdr(&mei_hdr, sizeof(req)); 266 267 /* host start message */ 268 memset(&req, 0, sizeof(req)); 269 req.hbm_cmd = HOST_START_REQ_CMD; 270 req.host_version.major_version = HBM_MAJOR_VERSION; 271 req.host_version.minor_version = HBM_MINOR_VERSION; 272 273 dev->hbm_state = MEI_HBM_IDLE; 274 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 275 if (ret) { 276 dev_err(dev->dev, "version message write failed: ret = %d\n", 277 ret); 278 return ret; 279 } 280 281 dev->hbm_state = MEI_HBM_STARTING; 282 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; 283 mei_schedule_stall_timer(dev); 284 return 0; 285 } 286 287 /** 288 * mei_hbm_dma_setup_req() - setup DMA request 289 * @dev: the device structure 290 * 291 * Return: 0 on success and < 0 on failure 292 */ 293 static int mei_hbm_dma_setup_req(struct mei_device *dev) 294 { 295 struct mei_msg_hdr mei_hdr; 296 struct hbm_dma_setup_request req; 297 unsigned int i; 298 int ret; 299 300 mei_hbm_hdr(&mei_hdr, sizeof(req)); 301 302 memset(&req, 0, sizeof(req)); 303 req.hbm_cmd = MEI_HBM_DMA_SETUP_REQ_CMD; 304 for (i = 0; i < DMA_DSCR_NUM; i++) { 305 phys_addr_t paddr; 306 307 paddr = dev->dr_dscr[i].daddr; 308 req.dma_dscr[i].addr_hi = upper_32_bits(paddr); 309 req.dma_dscr[i].addr_lo = lower_32_bits(paddr); 310 req.dma_dscr[i].size = dev->dr_dscr[i].size; 311 } 312 313 mei_dma_ring_reset(dev); 314 315 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 316 if (ret) { 317 dev_err(dev->dev, "dma setup request write failed: ret = %d.\n", 318 ret); 319 return ret; 320 } 321 322 dev->hbm_state = MEI_HBM_DR_SETUP; 323 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; 324 mei_schedule_stall_timer(dev); 325 return 0; 326 } 327 328 /** 329 * mei_hbm_enum_clients_req - sends enumeration client request message. 330 * 331 * @dev: the device structure 332 * 333 * Return: 0 on success and < 0 on failure 334 */ 335 static int mei_hbm_enum_clients_req(struct mei_device *dev) 336 { 337 struct mei_msg_hdr mei_hdr; 338 struct hbm_host_enum_request req; 339 int ret; 340 341 /* enumerate clients */ 342 mei_hbm_hdr(&mei_hdr, sizeof(req)); 343 344 memset(&req, 0, sizeof(req)); 345 req.hbm_cmd = HOST_ENUM_REQ_CMD; 346 req.flags |= dev->hbm_f_dc_supported ? MEI_HBM_ENUM_F_ALLOW_ADD : 0; 347 req.flags |= dev->hbm_f_ie_supported ? 348 MEI_HBM_ENUM_F_IMMEDIATE_ENUM : 0; 349 350 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 351 if (ret) { 352 dev_err(dev->dev, "enumeration request write failed: ret = %d.\n", 353 ret); 354 return ret; 355 } 356 dev->hbm_state = MEI_HBM_ENUM_CLIENTS; 357 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; 358 mei_schedule_stall_timer(dev); 359 return 0; 360 } 361 362 /** 363 * mei_hbm_me_cl_add - add new me client to the list 364 * 365 * @dev: the device structure 366 * @res: hbm property response 367 * 368 * Return: 0 on success and -ENOMEM on allocation failure 369 */ 370 371 static int mei_hbm_me_cl_add(struct mei_device *dev, 372 struct hbm_props_response *res) 373 { 374 struct mei_me_client *me_cl; 375 const uuid_le *uuid = &res->client_properties.protocol_name; 376 377 mei_me_cl_rm_by_uuid(dev, uuid); 378 379 me_cl = kzalloc(sizeof(*me_cl), GFP_KERNEL); 380 if (!me_cl) 381 return -ENOMEM; 382 383 mei_me_cl_init(me_cl); 384 385 me_cl->props = res->client_properties; 386 me_cl->client_id = res->me_addr; 387 me_cl->tx_flow_ctrl_creds = 0; 388 389 mei_me_cl_add(dev, me_cl); 390 391 return 0; 392 } 393 394 /** 395 * mei_hbm_add_cl_resp - send response to fw on client add request 396 * 397 * @dev: the device structure 398 * @addr: me address 399 * @status: response status 400 * 401 * Return: 0 on success and < 0 on failure 402 */ 403 static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status) 404 { 405 struct mei_msg_hdr mei_hdr; 406 struct hbm_add_client_response resp; 407 int ret; 408 409 dev_dbg(dev->dev, "adding client response\n"); 410 411 mei_hbm_hdr(&mei_hdr, sizeof(resp)); 412 413 memset(&resp, 0, sizeof(resp)); 414 resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD; 415 resp.me_addr = addr; 416 resp.status = status; 417 418 ret = mei_hbm_write_message(dev, &mei_hdr, &resp); 419 if (ret) 420 dev_err(dev->dev, "add client response write failed: ret = %d\n", 421 ret); 422 return ret; 423 } 424 425 /** 426 * mei_hbm_fw_add_cl_req - request from the fw to add a client 427 * 428 * @dev: the device structure 429 * @req: add client request 430 * 431 * Return: 0 on success and < 0 on failure 432 */ 433 static int mei_hbm_fw_add_cl_req(struct mei_device *dev, 434 struct hbm_add_client_request *req) 435 { 436 int ret; 437 u8 status = MEI_HBMS_SUCCESS; 438 439 BUILD_BUG_ON(sizeof(struct hbm_add_client_request) != 440 sizeof(struct hbm_props_response)); 441 442 ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req); 443 if (ret) 444 status = !MEI_HBMS_SUCCESS; 445 446 if (dev->dev_state == MEI_DEV_ENABLED) 447 schedule_work(&dev->bus_rescan_work); 448 449 return mei_hbm_add_cl_resp(dev, req->me_addr, status); 450 } 451 452 /** 453 * mei_hbm_cl_notify_req - send notification request 454 * 455 * @dev: the device structure 456 * @cl: a client to disconnect from 457 * @start: true for start false for stop 458 * 459 * Return: 0 on success and -EIO on write failure 460 */ 461 int mei_hbm_cl_notify_req(struct mei_device *dev, 462 struct mei_cl *cl, u8 start) 463 { 464 465 struct mei_msg_hdr mei_hdr; 466 struct hbm_notification_request req; 467 int ret; 468 469 mei_hbm_hdr(&mei_hdr, sizeof(req)); 470 mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, &req, sizeof(req)); 471 472 req.start = start; 473 474 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 475 if (ret) 476 dev_err(dev->dev, "notify request failed: ret = %d\n", ret); 477 478 return ret; 479 } 480 481 /** 482 * notify_res_to_fop - convert notification response to the proper 483 * notification FOP 484 * 485 * @cmd: client notification start response command 486 * 487 * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP; 488 */ 489 static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd) 490 { 491 struct hbm_notification_response *rs = 492 (struct hbm_notification_response *)cmd; 493 494 return mei_cl_notify_req2fop(rs->start); 495 } 496 497 /** 498 * mei_hbm_cl_notify_start_res - update the client state according 499 * notify start response 500 * 501 * @dev: the device structure 502 * @cl: mei host client 503 * @cmd: client notification start response command 504 */ 505 static void mei_hbm_cl_notify_start_res(struct mei_device *dev, 506 struct mei_cl *cl, 507 struct mei_hbm_cl_cmd *cmd) 508 { 509 struct hbm_notification_response *rs = 510 (struct hbm_notification_response *)cmd; 511 512 cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status); 513 514 if (rs->status == MEI_HBMS_SUCCESS || 515 rs->status == MEI_HBMS_ALREADY_STARTED) { 516 cl->notify_en = true; 517 cl->status = 0; 518 } else { 519 cl->status = -EINVAL; 520 } 521 } 522 523 /** 524 * mei_hbm_cl_notify_stop_res - update the client state according 525 * notify stop response 526 * 527 * @dev: the device structure 528 * @cl: mei host client 529 * @cmd: client notification stop response command 530 */ 531 static void mei_hbm_cl_notify_stop_res(struct mei_device *dev, 532 struct mei_cl *cl, 533 struct mei_hbm_cl_cmd *cmd) 534 { 535 struct hbm_notification_response *rs = 536 (struct hbm_notification_response *)cmd; 537 538 cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status); 539 540 if (rs->status == MEI_HBMS_SUCCESS || 541 rs->status == MEI_HBMS_NOT_STARTED) { 542 cl->notify_en = false; 543 cl->status = 0; 544 } else { 545 /* TODO: spec is not clear yet about other possible issues */ 546 cl->status = -EINVAL; 547 } 548 } 549 550 /** 551 * mei_hbm_cl_notify - signal notification event 552 * 553 * @dev: the device structure 554 * @cmd: notification client message 555 */ 556 static void mei_hbm_cl_notify(struct mei_device *dev, 557 struct mei_hbm_cl_cmd *cmd) 558 { 559 struct mei_cl *cl; 560 561 cl = mei_hbm_cl_find_by_cmd(dev, cmd); 562 if (cl) 563 mei_cl_notify(cl); 564 } 565 566 /** 567 * mei_hbm_prop_req - request property for a single client 568 * 569 * @dev: the device structure 570 * @start_idx: client index to start search 571 * 572 * Return: 0 on success and < 0 on failure 573 */ 574 static int mei_hbm_prop_req(struct mei_device *dev, unsigned long start_idx) 575 { 576 struct mei_msg_hdr mei_hdr; 577 struct hbm_props_request req; 578 unsigned long addr; 579 int ret; 580 581 addr = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, start_idx); 582 583 /* We got all client properties */ 584 if (addr == MEI_CLIENTS_MAX) { 585 dev->hbm_state = MEI_HBM_STARTED; 586 mei_host_client_init(dev); 587 return 0; 588 } 589 590 mei_hbm_hdr(&mei_hdr, sizeof(req)); 591 592 memset(&req, 0, sizeof(req)); 593 594 req.hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD; 595 req.me_addr = addr; 596 597 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 598 if (ret) { 599 dev_err(dev->dev, "properties request write failed: ret = %d\n", 600 ret); 601 return ret; 602 } 603 604 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; 605 mei_schedule_stall_timer(dev); 606 607 return 0; 608 } 609 610 /** 611 * mei_hbm_pg - sends pg command 612 * 613 * @dev: the device structure 614 * @pg_cmd: the pg command code 615 * 616 * Return: -EIO on write failure 617 * -EOPNOTSUPP if the operation is not supported by the protocol 618 */ 619 int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd) 620 { 621 struct mei_msg_hdr mei_hdr; 622 struct hbm_power_gate req; 623 int ret; 624 625 if (!dev->hbm_f_pg_supported) 626 return -EOPNOTSUPP; 627 628 mei_hbm_hdr(&mei_hdr, sizeof(req)); 629 630 memset(&req, 0, sizeof(req)); 631 req.hbm_cmd = pg_cmd; 632 633 ret = mei_hbm_write_message(dev, &mei_hdr, &req); 634 if (ret) 635 dev_err(dev->dev, "power gate command write failed.\n"); 636 return ret; 637 } 638 EXPORT_SYMBOL_GPL(mei_hbm_pg); 639 640 /** 641 * mei_hbm_stop_req - send stop request message 642 * 643 * @dev: mei device 644 * 645 * Return: -EIO on write failure 646 */ 647 static int mei_hbm_stop_req(struct mei_device *dev) 648 { 649 struct mei_msg_hdr mei_hdr; 650 struct hbm_host_stop_request req; 651 652 mei_hbm_hdr(&mei_hdr, sizeof(req)); 653 654 memset(&req, 0, sizeof(req)); 655 req.hbm_cmd = HOST_STOP_REQ_CMD; 656 req.reason = DRIVER_STOP_REQUEST; 657 658 return mei_hbm_write_message(dev, &mei_hdr, &req); 659 } 660 661 /** 662 * mei_hbm_cl_flow_control_req - sends flow control request. 663 * 664 * @dev: the device structure 665 * @cl: client info 666 * 667 * Return: -EIO on write failure 668 */ 669 int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl) 670 { 671 struct hbm_flow_control req; 672 673 cl_dbg(dev, cl, "sending flow control\n"); 674 return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, 675 &req, sizeof(req)); 676 } 677 678 /** 679 * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials. 680 * 681 * @dev: the device structure 682 * @fctrl: flow control response bus message 683 * 684 * Return: 0 on success, < 0 otherwise 685 */ 686 static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device *dev, 687 struct hbm_flow_control *fctrl) 688 { 689 struct mei_me_client *me_cl; 690 int rets; 691 692 me_cl = mei_me_cl_by_id(dev, fctrl->me_addr); 693 if (!me_cl) { 694 dev_err(dev->dev, "no such me client %d\n", fctrl->me_addr); 695 return -ENOENT; 696 } 697 698 if (WARN_ON(me_cl->props.single_recv_buf == 0)) { 699 rets = -EINVAL; 700 goto out; 701 } 702 703 me_cl->tx_flow_ctrl_creds++; 704 dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n", 705 fctrl->me_addr, me_cl->tx_flow_ctrl_creds); 706 707 rets = 0; 708 out: 709 mei_me_cl_put(me_cl); 710 return rets; 711 } 712 713 /** 714 * mei_hbm_cl_flow_control_res - flow control response from me 715 * 716 * @dev: the device structure 717 * @fctrl: flow control response bus message 718 */ 719 static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device *dev, 720 struct hbm_flow_control *fctrl) 721 { 722 struct mei_cl *cl; 723 724 if (!fctrl->host_addr) { 725 /* single receive buffer */ 726 mei_hbm_add_single_tx_flow_ctrl_creds(dev, fctrl); 727 return; 728 } 729 730 cl = mei_hbm_cl_find_by_cmd(dev, fctrl); 731 if (cl) { 732 cl->tx_flow_ctrl_creds++; 733 cl_dbg(dev, cl, "flow control creds = %d.\n", 734 cl->tx_flow_ctrl_creds); 735 } 736 } 737 738 739 /** 740 * mei_hbm_cl_disconnect_req - sends disconnect message to fw. 741 * 742 * @dev: the device structure 743 * @cl: a client to disconnect from 744 * 745 * Return: -EIO on write failure 746 */ 747 int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl) 748 { 749 struct hbm_client_connect_request req; 750 751 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, 752 &req, sizeof(req)); 753 } 754 755 /** 756 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW 757 * 758 * @dev: the device structure 759 * @cl: a client to disconnect from 760 * 761 * Return: -EIO on write failure 762 */ 763 int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl) 764 { 765 struct hbm_client_connect_response resp; 766 767 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, 768 &resp, sizeof(resp)); 769 } 770 771 /** 772 * mei_hbm_cl_disconnect_res - update the client state according 773 * disconnect response 774 * 775 * @dev: the device structure 776 * @cl: mei host client 777 * @cmd: disconnect client response host bus message 778 */ 779 static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl, 780 struct mei_hbm_cl_cmd *cmd) 781 { 782 struct hbm_client_connect_response *rs = 783 (struct hbm_client_connect_response *)cmd; 784 785 cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status); 786 787 if (rs->status == MEI_CL_DISCONN_SUCCESS) 788 cl->state = MEI_FILE_DISCONNECT_REPLY; 789 cl->status = 0; 790 } 791 792 /** 793 * mei_hbm_cl_connect_req - send connection request to specific me client 794 * 795 * @dev: the device structure 796 * @cl: a client to connect to 797 * 798 * Return: -EIO on write failure 799 */ 800 int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl) 801 { 802 struct hbm_client_connect_request req; 803 804 return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, 805 &req, sizeof(req)); 806 } 807 808 /** 809 * mei_hbm_cl_connect_res - update the client state according 810 * connection response 811 * 812 * @dev: the device structure 813 * @cl: mei host client 814 * @cmd: connect client response host bus message 815 */ 816 static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl, 817 struct mei_hbm_cl_cmd *cmd) 818 { 819 struct hbm_client_connect_response *rs = 820 (struct hbm_client_connect_response *)cmd; 821 822 cl_dbg(dev, cl, "hbm: connect response status=%s\n", 823 mei_cl_conn_status_str(rs->status)); 824 825 if (rs->status == MEI_CL_CONN_SUCCESS) 826 cl->state = MEI_FILE_CONNECTED; 827 else { 828 cl->state = MEI_FILE_DISCONNECT_REPLY; 829 if (rs->status == MEI_CL_CONN_NOT_FOUND) { 830 mei_me_cl_del(dev, cl->me_cl); 831 if (dev->dev_state == MEI_DEV_ENABLED) 832 schedule_work(&dev->bus_rescan_work); 833 } 834 } 835 cl->status = mei_cl_conn_status_to_errno(rs->status); 836 } 837 838 /** 839 * mei_hbm_cl_res - process hbm response received on behalf 840 * an client 841 * 842 * @dev: the device structure 843 * @rs: hbm client message 844 * @fop_type: file operation type 845 */ 846 static void mei_hbm_cl_res(struct mei_device *dev, 847 struct mei_hbm_cl_cmd *rs, 848 enum mei_cb_file_ops fop_type) 849 { 850 struct mei_cl *cl; 851 struct mei_cl_cb *cb, *next; 852 853 cl = NULL; 854 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) { 855 856 cl = cb->cl; 857 858 if (cb->fop_type != fop_type) 859 continue; 860 861 if (mei_hbm_cl_addr_equal(cl, rs)) { 862 list_del_init(&cb->list); 863 break; 864 } 865 } 866 867 if (!cl) 868 return; 869 870 switch (fop_type) { 871 case MEI_FOP_CONNECT: 872 mei_hbm_cl_connect_res(dev, cl, rs); 873 break; 874 case MEI_FOP_DISCONNECT: 875 mei_hbm_cl_disconnect_res(dev, cl, rs); 876 break; 877 case MEI_FOP_NOTIFY_START: 878 mei_hbm_cl_notify_start_res(dev, cl, rs); 879 break; 880 case MEI_FOP_NOTIFY_STOP: 881 mei_hbm_cl_notify_stop_res(dev, cl, rs); 882 break; 883 default: 884 return; 885 } 886 887 cl->timer_count = 0; 888 wake_up(&cl->wait); 889 } 890 891 892 /** 893 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware 894 * host sends disconnect response 895 * 896 * @dev: the device structure. 897 * @disconnect_req: disconnect request bus message from the me 898 * 899 * Return: -ENOMEM on allocation failure 900 */ 901 static int mei_hbm_fw_disconnect_req(struct mei_device *dev, 902 struct hbm_client_connect_request *disconnect_req) 903 { 904 struct mei_cl *cl; 905 struct mei_cl_cb *cb; 906 907 cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req); 908 if (cl) { 909 cl_warn(dev, cl, "fw disconnect request received\n"); 910 cl->state = MEI_FILE_DISCONNECTING; 911 cl->timer_count = 0; 912 913 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT_RSP, 914 NULL); 915 if (!cb) 916 return -ENOMEM; 917 } 918 return 0; 919 } 920 921 /** 922 * mei_hbm_pg_enter_res - PG enter response received 923 * 924 * @dev: the device structure. 925 * 926 * Return: 0 on success, -EPROTO on state mismatch 927 */ 928 static int mei_hbm_pg_enter_res(struct mei_device *dev) 929 { 930 if (mei_pg_state(dev) != MEI_PG_OFF || 931 dev->pg_event != MEI_PG_EVENT_WAIT) { 932 dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n", 933 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event); 934 return -EPROTO; 935 } 936 937 dev->pg_event = MEI_PG_EVENT_RECEIVED; 938 wake_up(&dev->wait_pg); 939 940 return 0; 941 } 942 943 /** 944 * mei_hbm_pg_resume - process with PG resume 945 * 946 * @dev: the device structure. 947 */ 948 void mei_hbm_pg_resume(struct mei_device *dev) 949 { 950 pm_request_resume(dev->dev); 951 } 952 EXPORT_SYMBOL_GPL(mei_hbm_pg_resume); 953 954 /** 955 * mei_hbm_pg_exit_res - PG exit response received 956 * 957 * @dev: the device structure. 958 * 959 * Return: 0 on success, -EPROTO on state mismatch 960 */ 961 static int mei_hbm_pg_exit_res(struct mei_device *dev) 962 { 963 if (mei_pg_state(dev) != MEI_PG_ON || 964 (dev->pg_event != MEI_PG_EVENT_WAIT && 965 dev->pg_event != MEI_PG_EVENT_IDLE)) { 966 dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n", 967 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event); 968 return -EPROTO; 969 } 970 971 switch (dev->pg_event) { 972 case MEI_PG_EVENT_WAIT: 973 dev->pg_event = MEI_PG_EVENT_RECEIVED; 974 wake_up(&dev->wait_pg); 975 break; 976 case MEI_PG_EVENT_IDLE: 977 /* 978 * If the driver is not waiting on this then 979 * this is HW initiated exit from PG. 980 * Start runtime pm resume sequence to exit from PG. 981 */ 982 dev->pg_event = MEI_PG_EVENT_RECEIVED; 983 mei_hbm_pg_resume(dev); 984 break; 985 default: 986 WARN(1, "hbm: pg exit response: unexpected pg event = %d\n", 987 dev->pg_event); 988 return -EPROTO; 989 } 990 991 return 0; 992 } 993 994 /** 995 * mei_hbm_config_features - check what hbm features and commands 996 * are supported by the fw 997 * 998 * @dev: the device structure 999 */ 1000 static void mei_hbm_config_features(struct mei_device *dev) 1001 { 1002 /* Power Gating Isolation Support */ 1003 dev->hbm_f_pg_supported = 0; 1004 if (dev->version.major_version > HBM_MAJOR_VERSION_PGI) 1005 dev->hbm_f_pg_supported = 1; 1006 1007 if (dev->version.major_version == HBM_MAJOR_VERSION_PGI && 1008 dev->version.minor_version >= HBM_MINOR_VERSION_PGI) 1009 dev->hbm_f_pg_supported = 1; 1010 1011 dev->hbm_f_dc_supported = 0; 1012 if (dev->version.major_version >= HBM_MAJOR_VERSION_DC) 1013 dev->hbm_f_dc_supported = 1; 1014 1015 dev->hbm_f_ie_supported = 0; 1016 if (dev->version.major_version >= HBM_MAJOR_VERSION_IE) 1017 dev->hbm_f_ie_supported = 1; 1018 1019 /* disconnect on connect timeout instead of link reset */ 1020 dev->hbm_f_dot_supported = 0; 1021 if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT) 1022 dev->hbm_f_dot_supported = 1; 1023 1024 /* Notification Event Support */ 1025 dev->hbm_f_ev_supported = 0; 1026 if (dev->version.major_version >= HBM_MAJOR_VERSION_EV) 1027 dev->hbm_f_ev_supported = 1; 1028 1029 /* Fixed Address Client Support */ 1030 dev->hbm_f_fa_supported = 0; 1031 if (dev->version.major_version >= HBM_MAJOR_VERSION_FA) 1032 dev->hbm_f_fa_supported = 1; 1033 1034 /* OS ver message Support */ 1035 dev->hbm_f_os_supported = 0; 1036 if (dev->version.major_version >= HBM_MAJOR_VERSION_OS) 1037 dev->hbm_f_os_supported = 1; 1038 1039 /* DMA Ring Support */ 1040 dev->hbm_f_dr_supported = 0; 1041 if (dev->version.major_version > HBM_MAJOR_VERSION_DR || 1042 (dev->version.major_version == HBM_MAJOR_VERSION_DR && 1043 dev->version.minor_version >= HBM_MINOR_VERSION_DR)) 1044 dev->hbm_f_dr_supported = 1; 1045 } 1046 1047 /** 1048 * mei_hbm_version_is_supported - checks whether the driver can 1049 * support the hbm version of the device 1050 * 1051 * @dev: the device structure 1052 * Return: true if driver can support hbm version of the device 1053 */ 1054 bool mei_hbm_version_is_supported(struct mei_device *dev) 1055 { 1056 return (dev->version.major_version < HBM_MAJOR_VERSION) || 1057 (dev->version.major_version == HBM_MAJOR_VERSION && 1058 dev->version.minor_version <= HBM_MINOR_VERSION); 1059 } 1060 1061 /** 1062 * mei_hbm_dispatch - bottom half read routine after ISR to 1063 * handle the read bus message cmd processing. 1064 * 1065 * @dev: the device structure 1066 * @hdr: header of bus message 1067 * 1068 * Return: 0 on success and < 0 on failure 1069 */ 1070 int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr) 1071 { 1072 struct mei_bus_message *mei_msg; 1073 struct hbm_host_version_response *version_res; 1074 struct hbm_props_response *props_res; 1075 struct hbm_host_enum_response *enum_res; 1076 struct hbm_dma_setup_response *dma_setup_res; 1077 struct hbm_add_client_request *add_cl_req; 1078 int ret; 1079 1080 struct mei_hbm_cl_cmd *cl_cmd; 1081 struct hbm_client_connect_request *disconnect_req; 1082 struct hbm_flow_control *fctrl; 1083 1084 /* read the message to our buffer */ 1085 BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf)); 1086 mei_read_slots(dev, dev->rd_msg_buf, hdr->length); 1087 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf; 1088 cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg; 1089 1090 /* ignore spurious message and prevent reset nesting 1091 * hbm is put to idle during system reset 1092 */ 1093 if (dev->hbm_state == MEI_HBM_IDLE) { 1094 dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n"); 1095 return 0; 1096 } 1097 1098 switch (mei_msg->hbm_cmd) { 1099 case HOST_START_RES_CMD: 1100 dev_dbg(dev->dev, "hbm: start: response message received.\n"); 1101 1102 dev->init_clients_timer = 0; 1103 1104 version_res = (struct hbm_host_version_response *)mei_msg; 1105 1106 dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n", 1107 HBM_MAJOR_VERSION, HBM_MINOR_VERSION, 1108 version_res->me_max_version.major_version, 1109 version_res->me_max_version.minor_version); 1110 1111 if (version_res->host_version_supported) { 1112 dev->version.major_version = HBM_MAJOR_VERSION; 1113 dev->version.minor_version = HBM_MINOR_VERSION; 1114 } else { 1115 dev->version.major_version = 1116 version_res->me_max_version.major_version; 1117 dev->version.minor_version = 1118 version_res->me_max_version.minor_version; 1119 } 1120 1121 if (!mei_hbm_version_is_supported(dev)) { 1122 dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n"); 1123 1124 dev->hbm_state = MEI_HBM_STOPPED; 1125 if (mei_hbm_stop_req(dev)) { 1126 dev_err(dev->dev, "hbm: start: failed to send stop request\n"); 1127 return -EIO; 1128 } 1129 break; 1130 } 1131 1132 mei_hbm_config_features(dev); 1133 1134 if (dev->dev_state != MEI_DEV_INIT_CLIENTS || 1135 dev->hbm_state != MEI_HBM_STARTING) { 1136 dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n", 1137 dev->dev_state, dev->hbm_state); 1138 return -EPROTO; 1139 } 1140 1141 if (dev->hbm_f_dr_supported) { 1142 if (mei_dmam_ring_alloc(dev)) 1143 dev_info(dev->dev, "running w/o dma ring\n"); 1144 if (mei_dma_ring_is_allocated(dev)) { 1145 if (mei_hbm_dma_setup_req(dev)) 1146 return -EIO; 1147 1148 wake_up(&dev->wait_hbm_start); 1149 break; 1150 } 1151 } 1152 1153 dev->hbm_f_dr_supported = 0; 1154 mei_dmam_ring_free(dev); 1155 1156 if (mei_hbm_enum_clients_req(dev)) 1157 return -EIO; 1158 1159 wake_up(&dev->wait_hbm_start); 1160 break; 1161 1162 case MEI_HBM_DMA_SETUP_RES_CMD: 1163 dev_dbg(dev->dev, "hbm: dma setup response: message received.\n"); 1164 1165 dev->init_clients_timer = 0; 1166 1167 if (dev->hbm_state != MEI_HBM_DR_SETUP) { 1168 dev_err(dev->dev, "hbm: dma setup response: state mismatch, [%d, %d]\n", 1169 dev->dev_state, dev->hbm_state); 1170 return -EPROTO; 1171 } 1172 1173 dma_setup_res = (struct hbm_dma_setup_response *)mei_msg; 1174 1175 if (dma_setup_res->status) { 1176 u8 status = dma_setup_res->status; 1177 1178 if (status == MEI_HBMS_NOT_ALLOWED) { 1179 dev_dbg(dev->dev, "hbm: dma setup not allowed\n"); 1180 } else { 1181 dev_info(dev->dev, "hbm: dma setup response: failure = %d %s\n", 1182 status, 1183 mei_hbm_status_str(status)); 1184 } 1185 dev->hbm_f_dr_supported = 0; 1186 mei_dmam_ring_free(dev); 1187 } 1188 1189 if (mei_hbm_enum_clients_req(dev)) 1190 return -EIO; 1191 break; 1192 1193 case CLIENT_CONNECT_RES_CMD: 1194 dev_dbg(dev->dev, "hbm: client connect response: message received.\n"); 1195 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT); 1196 break; 1197 1198 case CLIENT_DISCONNECT_RES_CMD: 1199 dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n"); 1200 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT); 1201 break; 1202 1203 case MEI_FLOW_CONTROL_CMD: 1204 dev_dbg(dev->dev, "hbm: client flow control response: message received.\n"); 1205 1206 fctrl = (struct hbm_flow_control *)mei_msg; 1207 mei_hbm_cl_tx_flow_ctrl_creds_res(dev, fctrl); 1208 break; 1209 1210 case MEI_PG_ISOLATION_ENTRY_RES_CMD: 1211 dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n"); 1212 ret = mei_hbm_pg_enter_res(dev); 1213 if (ret) 1214 return ret; 1215 break; 1216 1217 case MEI_PG_ISOLATION_EXIT_REQ_CMD: 1218 dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n"); 1219 ret = mei_hbm_pg_exit_res(dev); 1220 if (ret) 1221 return ret; 1222 break; 1223 1224 case HOST_CLIENT_PROPERTIES_RES_CMD: 1225 dev_dbg(dev->dev, "hbm: properties response: message received.\n"); 1226 1227 dev->init_clients_timer = 0; 1228 1229 if (dev->dev_state != MEI_DEV_INIT_CLIENTS || 1230 dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) { 1231 dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n", 1232 dev->dev_state, dev->hbm_state); 1233 return -EPROTO; 1234 } 1235 1236 props_res = (struct hbm_props_response *)mei_msg; 1237 1238 if (props_res->status == MEI_HBMS_CLIENT_NOT_FOUND) { 1239 dev_dbg(dev->dev, "hbm: properties response: %d CLIENT_NOT_FOUND\n", 1240 props_res->me_addr); 1241 } else if (props_res->status) { 1242 dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n", 1243 props_res->status, 1244 mei_hbm_status_str(props_res->status)); 1245 return -EPROTO; 1246 } else { 1247 mei_hbm_me_cl_add(dev, props_res); 1248 } 1249 1250 /* request property for the next client */ 1251 if (mei_hbm_prop_req(dev, props_res->me_addr + 1)) 1252 return -EIO; 1253 1254 break; 1255 1256 case HOST_ENUM_RES_CMD: 1257 dev_dbg(dev->dev, "hbm: enumeration response: message received\n"); 1258 1259 dev->init_clients_timer = 0; 1260 1261 enum_res = (struct hbm_host_enum_response *) mei_msg; 1262 BUILD_BUG_ON(sizeof(dev->me_clients_map) 1263 < sizeof(enum_res->valid_addresses)); 1264 memcpy(dev->me_clients_map, enum_res->valid_addresses, 1265 sizeof(enum_res->valid_addresses)); 1266 1267 if (dev->dev_state != MEI_DEV_INIT_CLIENTS || 1268 dev->hbm_state != MEI_HBM_ENUM_CLIENTS) { 1269 dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n", 1270 dev->dev_state, dev->hbm_state); 1271 return -EPROTO; 1272 } 1273 1274 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES; 1275 1276 /* first property request */ 1277 if (mei_hbm_prop_req(dev, 0)) 1278 return -EIO; 1279 1280 break; 1281 1282 case HOST_STOP_RES_CMD: 1283 dev_dbg(dev->dev, "hbm: stop response: message received\n"); 1284 1285 dev->init_clients_timer = 0; 1286 1287 if (dev->hbm_state != MEI_HBM_STOPPED) { 1288 dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n", 1289 dev->dev_state, dev->hbm_state); 1290 return -EPROTO; 1291 } 1292 1293 dev->dev_state = MEI_DEV_POWER_DOWN; 1294 dev_info(dev->dev, "hbm: stop response: resetting.\n"); 1295 /* force the reset */ 1296 return -EPROTO; 1297 break; 1298 1299 case CLIENT_DISCONNECT_REQ_CMD: 1300 dev_dbg(dev->dev, "hbm: disconnect request: message received\n"); 1301 1302 disconnect_req = (struct hbm_client_connect_request *)mei_msg; 1303 mei_hbm_fw_disconnect_req(dev, disconnect_req); 1304 break; 1305 1306 case ME_STOP_REQ_CMD: 1307 dev_dbg(dev->dev, "hbm: stop request: message received\n"); 1308 dev->hbm_state = MEI_HBM_STOPPED; 1309 if (mei_hbm_stop_req(dev)) { 1310 dev_err(dev->dev, "hbm: stop request: failed to send stop request\n"); 1311 return -EIO; 1312 } 1313 break; 1314 1315 case MEI_HBM_ADD_CLIENT_REQ_CMD: 1316 dev_dbg(dev->dev, "hbm: add client request received\n"); 1317 /* 1318 * after the host receives the enum_resp 1319 * message clients may be added or removed 1320 */ 1321 if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS || 1322 dev->hbm_state >= MEI_HBM_STOPPED) { 1323 dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n", 1324 dev->dev_state, dev->hbm_state); 1325 return -EPROTO; 1326 } 1327 add_cl_req = (struct hbm_add_client_request *)mei_msg; 1328 ret = mei_hbm_fw_add_cl_req(dev, add_cl_req); 1329 if (ret) { 1330 dev_err(dev->dev, "hbm: add client: failed to send response %d\n", 1331 ret); 1332 return -EIO; 1333 } 1334 dev_dbg(dev->dev, "hbm: add client request processed\n"); 1335 break; 1336 1337 case MEI_HBM_NOTIFY_RES_CMD: 1338 dev_dbg(dev->dev, "hbm: notify response received\n"); 1339 mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd)); 1340 break; 1341 1342 case MEI_HBM_NOTIFICATION_CMD: 1343 dev_dbg(dev->dev, "hbm: notification\n"); 1344 mei_hbm_cl_notify(dev, cl_cmd); 1345 break; 1346 1347 default: 1348 WARN(1, "hbm: wrong command %d\n", mei_msg->hbm_cmd); 1349 return -EPROTO; 1350 1351 } 1352 return 0; 1353 } 1354 1355