1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2012-2023, Intel Corporation. All rights reserved. 4 * Intel Management Engine Interface (Intel MEI) Linux driver 5 */ 6 7 #include <linux/module.h> 8 #include <linux/device.h> 9 #include <linux/kernel.h> 10 #include <linux/sched/signal.h> 11 #include <linux/init.h> 12 #include <linux/errno.h> 13 #include <linux/slab.h> 14 #include <linux/mutex.h> 15 #include <linux/interrupt.h> 16 #include <linux/scatterlist.h> 17 #include <linux/mei_cl_bus.h> 18 19 #include "mei_dev.h" 20 #include "client.h" 21 22 #define to_mei_cl_driver(d) container_of_const(d, struct mei_cl_driver, driver) 23 24 /** 25 * __mei_cl_send - internal client send (write) 26 * 27 * @cl: host client 28 * @buf: buffer to send 29 * @length: buffer length 30 * @vtag: virtual tag 31 * @mode: sending mode 32 * 33 * Return: written size bytes or < 0 on error 34 */ 35 ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag, 36 unsigned int mode) 37 { 38 return __mei_cl_send_timeout(cl, buf, length, vtag, mode, MAX_SCHEDULE_TIMEOUT); 39 } 40 41 /** 42 * __mei_cl_send_timeout - internal client send (write) 43 * 44 * @cl: host client 45 * @buf: buffer to send 46 * @length: buffer length 47 * @vtag: virtual tag 48 * @mode: sending mode 49 * @timeout: send timeout in milliseconds. 50 * effective only for blocking writes: the MEI_CL_IO_TX_BLOCKING mode bit is set. 51 * set timeout to the MAX_SCHEDULE_TIMEOUT to maixum allowed wait. 52 * 53 * Return: written size bytes or < 0 on error 54 */ 55 ssize_t __mei_cl_send_timeout(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag, 56 unsigned int mode, unsigned long timeout) 57 { 58 struct mei_device *bus; 59 struct mei_cl_cb *cb; 60 ssize_t rets; 61 62 if (WARN_ON(!cl || !cl->dev)) 63 return -ENODEV; 64 65 bus = cl->dev; 66 67 mutex_lock(&bus->device_lock); 68 if (bus->dev_state != MEI_DEV_ENABLED && 69 bus->dev_state != MEI_DEV_POWERING_DOWN) { 70 rets = -ENODEV; 71 goto out; 72 } 73 74 if (!mei_cl_is_connected(cl)) { 75 rets = -ENODEV; 76 goto out; 77 } 78 79 /* Check if we have an ME client device */ 80 if (!mei_me_cl_is_active(cl->me_cl)) { 81 rets = -ENOTTY; 82 goto out; 83 } 84 85 if (vtag) { 86 /* Check if vtag is supported by client */ 87 rets = mei_cl_vt_support_check(cl); 88 if (rets) 89 goto out; 90 } 91 92 if (length > mei_cl_mtu(cl)) { 93 rets = -EFBIG; 94 goto out; 95 } 96 97 while (cl->tx_cb_queued >= bus->tx_queue_limit) { 98 mutex_unlock(&bus->device_lock); 99 rets = wait_event_interruptible(cl->tx_wait, 100 cl->writing_state == MEI_WRITE_COMPLETE || 101 (!mei_cl_is_connected(cl))); 102 mutex_lock(&bus->device_lock); 103 if (rets) { 104 if (signal_pending(current)) 105 rets = -EINTR; 106 goto out; 107 } 108 if (!mei_cl_is_connected(cl)) { 109 rets = -ENODEV; 110 goto out; 111 } 112 } 113 114 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); 115 if (!cb) { 116 rets = -ENOMEM; 117 goto out; 118 } 119 cb->vtag = vtag; 120 121 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL); 122 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING); 123 memcpy(cb->buf.data, buf, length); 124 /* hack we point data to header */ 125 if (mode & MEI_CL_IO_SGL) { 126 cb->ext_hdr = (struct mei_ext_hdr *)cb->buf.data; 127 cb->buf.data = NULL; 128 cb->buf.size = 0; 129 } 130 131 rets = mei_cl_write(cl, cb, timeout); 132 133 if (mode & MEI_CL_IO_SGL && rets == 0) 134 rets = length; 135 136 out: 137 mutex_unlock(&bus->device_lock); 138 139 return rets; 140 } 141 142 /** 143 * __mei_cl_recv - internal client receive (read) 144 * 145 * @cl: host client 146 * @buf: buffer to receive 147 * @length: buffer length 148 * @vtag: virtual tag 149 * @mode: io mode 150 * @timeout: recv timeout, 0 for infinite timeout 151 * 152 * Return: read size in bytes of < 0 on error 153 */ 154 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag, 155 unsigned int mode, unsigned long timeout) 156 { 157 struct mei_device *bus; 158 struct mei_cl_cb *cb; 159 size_t r_length; 160 ssize_t rets; 161 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK); 162 163 if (WARN_ON(!cl || !cl->dev)) 164 return -ENODEV; 165 166 bus = cl->dev; 167 168 mutex_lock(&bus->device_lock); 169 if (bus->dev_state != MEI_DEV_ENABLED && 170 bus->dev_state != MEI_DEV_POWERING_DOWN) { 171 rets = -ENODEV; 172 goto out; 173 } 174 175 cb = mei_cl_read_cb(cl, NULL); 176 if (cb) 177 goto copy; 178 179 rets = mei_cl_read_start(cl, length, NULL); 180 if (rets && rets != -EBUSY) 181 goto out; 182 183 if (nonblock) { 184 rets = -EAGAIN; 185 goto out; 186 } 187 188 /* wait on event only if there is no other waiter */ 189 /* synchronized under device mutex */ 190 if (!waitqueue_active(&cl->rx_wait)) { 191 192 mutex_unlock(&bus->device_lock); 193 194 if (timeout) { 195 rets = wait_event_interruptible_timeout 196 (cl->rx_wait, 197 mei_cl_read_cb(cl, NULL) || 198 (!mei_cl_is_connected(cl)), 199 msecs_to_jiffies(timeout)); 200 if (rets == 0) 201 return -ETIME; 202 if (rets < 0) { 203 if (signal_pending(current)) 204 return -EINTR; 205 return -ERESTARTSYS; 206 } 207 } else { 208 if (wait_event_interruptible 209 (cl->rx_wait, 210 mei_cl_read_cb(cl, NULL) || 211 (!mei_cl_is_connected(cl)))) { 212 if (signal_pending(current)) 213 return -EINTR; 214 return -ERESTARTSYS; 215 } 216 } 217 218 mutex_lock(&bus->device_lock); 219 220 if (!mei_cl_is_connected(cl)) { 221 rets = -ENODEV; 222 goto out; 223 } 224 } 225 226 cb = mei_cl_read_cb(cl, NULL); 227 if (!cb) { 228 rets = 0; 229 goto out; 230 } 231 232 copy: 233 if (cb->status) { 234 rets = cb->status; 235 goto free; 236 } 237 238 /* for the GSC type - copy the extended header to the buffer */ 239 if (cb->ext_hdr && cb->ext_hdr->type == MEI_EXT_HDR_GSC) { 240 r_length = min_t(size_t, length, cb->ext_hdr->length * sizeof(u32)); 241 memcpy(buf, cb->ext_hdr, r_length); 242 } else { 243 r_length = min_t(size_t, length, cb->buf_idx); 244 memcpy(buf, cb->buf.data, r_length); 245 } 246 rets = r_length; 247 248 if (vtag) 249 *vtag = cb->vtag; 250 251 free: 252 mei_cl_del_rd_completed(cl, cb); 253 out: 254 mutex_unlock(&bus->device_lock); 255 256 return rets; 257 } 258 259 /** 260 * mei_cldev_send_vtag - me device send with vtag (write) 261 * 262 * @cldev: me client device 263 * @buf: buffer to send 264 * @length: buffer length 265 * @vtag: virtual tag 266 * 267 * Return: 268 * * written size in bytes 269 * * < 0 on error 270 */ 271 272 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, const u8 *buf, 273 size_t length, u8 vtag) 274 { 275 struct mei_cl *cl = cldev->cl; 276 277 return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING); 278 } 279 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag); 280 281 /** 282 * mei_cldev_send_vtag_timeout - me device send with vtag and timeout (write) 283 * 284 * @cldev: me client device 285 * @buf: buffer to send 286 * @length: buffer length 287 * @vtag: virtual tag 288 * @timeout: send timeout in milliseconds, 0 for infinite timeout 289 * 290 * Return: 291 * * written size in bytes 292 * * < 0 on error 293 */ 294 295 ssize_t mei_cldev_send_vtag_timeout(struct mei_cl_device *cldev, const u8 *buf, 296 size_t length, u8 vtag, unsigned long timeout) 297 { 298 struct mei_cl *cl = cldev->cl; 299 300 return __mei_cl_send_timeout(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING, timeout); 301 } 302 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag_timeout); 303 304 /** 305 * mei_cldev_recv_vtag - client receive with vtag (read) 306 * 307 * @cldev: me client device 308 * @buf: buffer to receive 309 * @length: buffer length 310 * @vtag: virtual tag 311 * 312 * Return: 313 * * read size in bytes 314 * * < 0 on error 315 */ 316 317 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length, 318 u8 *vtag) 319 { 320 struct mei_cl *cl = cldev->cl; 321 322 return __mei_cl_recv(cl, buf, length, vtag, 0, 0); 323 } 324 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag); 325 326 /** 327 * mei_cldev_recv_timeout - client receive with timeout (read) 328 * 329 * @cldev: me client device 330 * @buf: buffer to receive 331 * @length: buffer length 332 * @timeout: send timeout in milliseconds, 0 for infinite timeout 333 * 334 * Return: 335 * * read size in bytes 336 * * < 0 on error 337 */ 338 ssize_t mei_cldev_recv_timeout(struct mei_cl_device *cldev, u8 *buf, size_t length, 339 unsigned long timeout) 340 { 341 return mei_cldev_recv_vtag_timeout(cldev, buf, length, NULL, timeout); 342 } 343 EXPORT_SYMBOL_GPL(mei_cldev_recv_timeout); 344 345 /** 346 * mei_cldev_recv_vtag_timeout - client receive with vtag (read) 347 * 348 * @cldev: me client device 349 * @buf: buffer to receive 350 * @length: buffer length 351 * @vtag: virtual tag 352 * @timeout: recv timeout in milliseconds, 0 for infinite timeout 353 * 354 * Return: 355 * * read size in bytes 356 * * < 0 on error 357 */ 358 359 ssize_t mei_cldev_recv_vtag_timeout(struct mei_cl_device *cldev, u8 *buf, size_t length, 360 u8 *vtag, unsigned long timeout) 361 { 362 struct mei_cl *cl = cldev->cl; 363 364 return __mei_cl_recv(cl, buf, length, vtag, 0, timeout); 365 } 366 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag_timeout); 367 368 /** 369 * mei_cldev_send - me device send (write) 370 * 371 * @cldev: me client device 372 * @buf: buffer to send 373 * @length: buffer length 374 * 375 * Return: 376 * * written size in bytes 377 * * < 0 on error 378 */ 379 ssize_t mei_cldev_send(struct mei_cl_device *cldev, const u8 *buf, size_t length) 380 { 381 return mei_cldev_send_vtag(cldev, buf, length, 0); 382 } 383 EXPORT_SYMBOL_GPL(mei_cldev_send); 384 385 /** 386 * mei_cldev_send_timeout - me device send with timeout (write) 387 * 388 * @cldev: me client device 389 * @buf: buffer to send 390 * @length: buffer length 391 * @timeout: send timeout in milliseconds, 0 for infinite timeout 392 * 393 * Return: 394 * * written size in bytes 395 * * < 0 on error 396 */ 397 ssize_t mei_cldev_send_timeout(struct mei_cl_device *cldev, const u8 *buf, size_t length, 398 unsigned long timeout) 399 { 400 return mei_cldev_send_vtag_timeout(cldev, buf, length, 0, timeout); 401 } 402 EXPORT_SYMBOL_GPL(mei_cldev_send_timeout); 403 404 /** 405 * mei_cldev_recv - client receive (read) 406 * 407 * @cldev: me client device 408 * @buf: buffer to receive 409 * @length: buffer length 410 * 411 * Return: read size in bytes of < 0 on error 412 */ 413 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) 414 { 415 return mei_cldev_recv_vtag(cldev, buf, length, NULL); 416 } 417 EXPORT_SYMBOL_GPL(mei_cldev_recv); 418 419 /** 420 * mei_cl_bus_rx_work - dispatch rx event for a bus device 421 * 422 * @work: work 423 */ 424 static void mei_cl_bus_rx_work(struct work_struct *work) 425 { 426 struct mei_cl_device *cldev; 427 struct mei_device *bus; 428 429 cldev = container_of(work, struct mei_cl_device, rx_work); 430 431 bus = cldev->bus; 432 433 if (cldev->rx_cb) 434 cldev->rx_cb(cldev); 435 436 mutex_lock(&bus->device_lock); 437 if (mei_cl_is_connected(cldev->cl)) 438 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); 439 mutex_unlock(&bus->device_lock); 440 } 441 442 /** 443 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device 444 * 445 * @work: work 446 */ 447 static void mei_cl_bus_notif_work(struct work_struct *work) 448 { 449 struct mei_cl_device *cldev; 450 451 cldev = container_of(work, struct mei_cl_device, notif_work); 452 453 if (cldev->notif_cb) 454 cldev->notif_cb(cldev); 455 } 456 457 /** 458 * mei_cl_bus_notify_event - schedule notify cb on bus client 459 * 460 * @cl: host client 461 * 462 * Return: true if event was scheduled 463 * false if the client is not waiting for event 464 */ 465 bool mei_cl_bus_notify_event(struct mei_cl *cl) 466 { 467 struct mei_cl_device *cldev = cl->cldev; 468 469 if (!cldev || !cldev->notif_cb) 470 return false; 471 472 if (!cl->notify_ev) 473 return false; 474 475 schedule_work(&cldev->notif_work); 476 477 cl->notify_ev = false; 478 479 return true; 480 } 481 482 /** 483 * mei_cl_bus_rx_event - schedule rx event 484 * 485 * @cl: host client 486 * 487 * Return: true if event was scheduled 488 * false if the client is not waiting for event 489 */ 490 bool mei_cl_bus_rx_event(struct mei_cl *cl) 491 { 492 struct mei_cl_device *cldev = cl->cldev; 493 494 if (!cldev || !cldev->rx_cb) 495 return false; 496 497 schedule_work(&cldev->rx_work); 498 499 return true; 500 } 501 502 /** 503 * mei_cldev_register_rx_cb - register Rx event callback 504 * 505 * @cldev: me client devices 506 * @rx_cb: callback function 507 * 508 * Return: 0 on success 509 * -EALREADY if an callback is already registered 510 * <0 on other errors 511 */ 512 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb) 513 { 514 struct mei_device *bus = cldev->bus; 515 int ret; 516 517 if (!rx_cb) 518 return -EINVAL; 519 if (cldev->rx_cb) 520 return -EALREADY; 521 522 cldev->rx_cb = rx_cb; 523 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work); 524 525 mutex_lock(&bus->device_lock); 526 if (mei_cl_is_connected(cldev->cl)) 527 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); 528 else 529 ret = -ENODEV; 530 mutex_unlock(&bus->device_lock); 531 if (ret && ret != -EBUSY) { 532 cancel_work_sync(&cldev->rx_work); 533 cldev->rx_cb = NULL; 534 return ret; 535 } 536 537 return 0; 538 } 539 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb); 540 541 /** 542 * mei_cldev_register_notif_cb - register FW notification event callback 543 * 544 * @cldev: me client devices 545 * @notif_cb: callback function 546 * 547 * Return: 0 on success 548 * -EALREADY if an callback is already registered 549 * <0 on other errors 550 */ 551 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev, 552 mei_cldev_cb_t notif_cb) 553 { 554 struct mei_device *bus = cldev->bus; 555 int ret; 556 557 if (!notif_cb) 558 return -EINVAL; 559 560 if (cldev->notif_cb) 561 return -EALREADY; 562 563 cldev->notif_cb = notif_cb; 564 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work); 565 566 mutex_lock(&bus->device_lock); 567 ret = mei_cl_notify_request(cldev->cl, NULL, 1); 568 mutex_unlock(&bus->device_lock); 569 if (ret) { 570 cancel_work_sync(&cldev->notif_work); 571 cldev->notif_cb = NULL; 572 return ret; 573 } 574 575 return 0; 576 } 577 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb); 578 579 /** 580 * mei_cldev_get_drvdata - driver data getter 581 * 582 * @cldev: mei client device 583 * 584 * Return: driver private data 585 */ 586 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev) 587 { 588 return dev_get_drvdata(&cldev->dev); 589 } 590 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata); 591 592 /** 593 * mei_cldev_set_drvdata - driver data setter 594 * 595 * @cldev: mei client device 596 * @data: data to store 597 */ 598 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data) 599 { 600 dev_set_drvdata(&cldev->dev, data); 601 } 602 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata); 603 604 /** 605 * mei_cldev_ver - return protocol version of the underlying me client 606 * 607 * @cldev: mei client device 608 * 609 * Return: me client protocol version 610 */ 611 u8 mei_cldev_ver(const struct mei_cl_device *cldev) 612 { 613 return mei_me_cl_ver(cldev->me_cl); 614 } 615 EXPORT_SYMBOL_GPL(mei_cldev_ver); 616 617 /** 618 * mei_cldev_enabled - check whether the device is enabled 619 * 620 * @cldev: mei client device 621 * 622 * Return: true if me client is initialized and connected 623 */ 624 bool mei_cldev_enabled(const struct mei_cl_device *cldev) 625 { 626 return mei_cl_is_connected(cldev->cl); 627 } 628 EXPORT_SYMBOL_GPL(mei_cldev_enabled); 629 630 /** 631 * mei_cl_bus_module_get - acquire module of the underlying 632 * hw driver. 633 * 634 * @cldev: mei client device 635 * 636 * Return: true on success; false if the module was removed. 637 */ 638 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev) 639 { 640 return try_module_get(cldev->bus->dev->driver->owner); 641 } 642 643 /** 644 * mei_cl_bus_module_put - release the underlying hw module. 645 * 646 * @cldev: mei client device 647 */ 648 static void mei_cl_bus_module_put(struct mei_cl_device *cldev) 649 { 650 module_put(cldev->bus->dev->driver->owner); 651 } 652 653 /** 654 * mei_cl_bus_vtag - get bus vtag entry wrapper 655 * The tag for bus client is always first. 656 * 657 * @cl: host client 658 * 659 * Return: bus vtag or NULL 660 */ 661 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl) 662 { 663 return list_first_entry_or_null(&cl->vtag_map, 664 struct mei_cl_vtag, list); 665 } 666 667 /** 668 * mei_cl_bus_vtag_alloc - add bus client entry to vtag map 669 * 670 * @cldev: me client device 671 * 672 * Return: 673 * * 0 on success 674 * * -ENOMEM if memory allocation failed 675 */ 676 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev) 677 { 678 struct mei_cl *cl = cldev->cl; 679 struct mei_cl_vtag *cl_vtag; 680 681 /* 682 * Bail out if the client does not supports vtags 683 * or has already allocated one 684 */ 685 if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl)) 686 return 0; 687 688 cl_vtag = mei_cl_vtag_alloc(NULL, 0); 689 if (IS_ERR(cl_vtag)) 690 return -ENOMEM; 691 692 list_add_tail(&cl_vtag->list, &cl->vtag_map); 693 694 return 0; 695 } 696 697 /** 698 * mei_cl_bus_vtag_free - remove the bus entry from vtag map 699 * 700 * @cldev: me client device 701 */ 702 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev) 703 { 704 struct mei_cl *cl = cldev->cl; 705 struct mei_cl_vtag *cl_vtag; 706 707 cl_vtag = mei_cl_bus_vtag(cl); 708 if (!cl_vtag) 709 return; 710 711 list_del(&cl_vtag->list); 712 kfree(cl_vtag); 713 } 714 715 void *mei_cldev_dma_map(struct mei_cl_device *cldev, u8 buffer_id, size_t size) 716 { 717 struct mei_device *bus; 718 struct mei_cl *cl; 719 int ret; 720 721 if (!cldev || !buffer_id || !size) 722 return ERR_PTR(-EINVAL); 723 724 if (!IS_ALIGNED(size, MEI_FW_PAGE_SIZE)) { 725 dev_err(&cldev->dev, "Map size should be aligned to %lu\n", 726 MEI_FW_PAGE_SIZE); 727 return ERR_PTR(-EINVAL); 728 } 729 730 cl = cldev->cl; 731 bus = cldev->bus; 732 733 mutex_lock(&bus->device_lock); 734 if (cl->state == MEI_FILE_UNINITIALIZED) { 735 ret = mei_cl_link(cl); 736 if (ret) 737 goto notlinked; 738 /* update pointers */ 739 cl->cldev = cldev; 740 } 741 742 ret = mei_cl_dma_alloc_and_map(cl, NULL, buffer_id, size); 743 if (ret) 744 mei_cl_unlink(cl); 745 notlinked: 746 mutex_unlock(&bus->device_lock); 747 if (ret) 748 return ERR_PTR(ret); 749 return cl->dma.vaddr; 750 } 751 EXPORT_SYMBOL_GPL(mei_cldev_dma_map); 752 753 int mei_cldev_dma_unmap(struct mei_cl_device *cldev) 754 { 755 struct mei_device *bus; 756 struct mei_cl *cl; 757 int ret; 758 759 if (!cldev) 760 return -EINVAL; 761 762 cl = cldev->cl; 763 bus = cldev->bus; 764 765 mutex_lock(&bus->device_lock); 766 ret = mei_cl_dma_unmap(cl, NULL); 767 768 mei_cl_flush_queues(cl, NULL); 769 mei_cl_unlink(cl); 770 mutex_unlock(&bus->device_lock); 771 return ret; 772 } 773 EXPORT_SYMBOL_GPL(mei_cldev_dma_unmap); 774 775 /** 776 * mei_cldev_enable - enable me client device 777 * create connection with me client 778 * 779 * @cldev: me client device 780 * 781 * Return: 0 on success and < 0 on error 782 */ 783 int mei_cldev_enable(struct mei_cl_device *cldev) 784 { 785 struct mei_device *bus = cldev->bus; 786 struct mei_cl *cl; 787 int ret; 788 789 cl = cldev->cl; 790 791 mutex_lock(&bus->device_lock); 792 if (cl->state == MEI_FILE_UNINITIALIZED) { 793 ret = mei_cl_link(cl); 794 if (ret) 795 goto notlinked; 796 /* update pointers */ 797 cl->cldev = cldev; 798 } 799 800 if (mei_cl_is_connected(cl)) { 801 ret = 0; 802 goto out; 803 } 804 805 if (!mei_me_cl_is_active(cldev->me_cl)) { 806 dev_err(&cldev->dev, "me client is not active\n"); 807 ret = -ENOTTY; 808 goto out; 809 } 810 811 ret = mei_cl_bus_vtag_alloc(cldev); 812 if (ret) 813 goto out; 814 815 ret = mei_cl_connect(cl, cldev->me_cl, NULL); 816 if (ret < 0) { 817 dev_err(&cldev->dev, "cannot connect\n"); 818 mei_cl_bus_vtag_free(cldev); 819 } 820 821 out: 822 if (ret) 823 mei_cl_unlink(cl); 824 notlinked: 825 mutex_unlock(&bus->device_lock); 826 827 return ret; 828 } 829 EXPORT_SYMBOL_GPL(mei_cldev_enable); 830 831 /** 832 * mei_cldev_unregister_callbacks - internal wrapper for unregistering 833 * callbacks. 834 * 835 * @cldev: client device 836 */ 837 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev) 838 { 839 if (cldev->rx_cb) { 840 cancel_work_sync(&cldev->rx_work); 841 cldev->rx_cb = NULL; 842 } 843 844 if (cldev->notif_cb) { 845 cancel_work_sync(&cldev->notif_work); 846 cldev->notif_cb = NULL; 847 } 848 } 849 850 /** 851 * mei_cldev_disable - disable me client device 852 * disconnect form the me client 853 * 854 * @cldev: me client device 855 * 856 * Return: 0 on success and < 0 on error 857 */ 858 int mei_cldev_disable(struct mei_cl_device *cldev) 859 { 860 struct mei_device *bus; 861 struct mei_cl *cl; 862 int err; 863 864 if (!cldev) 865 return -ENODEV; 866 867 cl = cldev->cl; 868 869 bus = cldev->bus; 870 871 mei_cldev_unregister_callbacks(cldev); 872 873 mutex_lock(&bus->device_lock); 874 875 mei_cl_bus_vtag_free(cldev); 876 877 if (!mei_cl_is_connected(cl)) { 878 dev_dbg(&cldev->dev, "Already disconnected\n"); 879 err = 0; 880 goto out; 881 } 882 883 err = mei_cl_disconnect(cl); 884 if (err < 0) 885 dev_err(&cldev->dev, "Could not disconnect from the ME client\n"); 886 887 out: 888 /* Flush queues and remove any pending read unless we have mapped DMA */ 889 if (!cl->dma_mapped) { 890 mei_cl_flush_queues(cl, NULL); 891 mei_cl_unlink(cl); 892 } 893 894 mutex_unlock(&bus->device_lock); 895 return err; 896 } 897 EXPORT_SYMBOL_GPL(mei_cldev_disable); 898 899 /** 900 * mei_cldev_send_gsc_command - sends a gsc command, by sending 901 * a gsl mei message to gsc and receiving reply from gsc 902 * 903 * @cldev: me client device 904 * @client_id: client id to send the command to 905 * @fence_id: fence id to send the command to 906 * @sg_in: scatter gather list containing addresses for rx message buffer 907 * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes 908 * @sg_out: scatter gather list containing addresses for tx message buffer 909 * 910 * Return: 911 * * written size in bytes 912 * * < 0 on error 913 */ 914 ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev, 915 u8 client_id, u32 fence_id, 916 struct scatterlist *sg_in, 917 size_t total_in_len, 918 struct scatterlist *sg_out) 919 { 920 struct mei_cl *cl; 921 struct mei_device *bus; 922 ssize_t ret = 0; 923 924 struct mei_ext_hdr_gsc_h2f *ext_hdr; 925 size_t buf_sz = sizeof(struct mei_ext_hdr_gsc_h2f); 926 int sg_out_nents, sg_in_nents; 927 int i; 928 struct scatterlist *sg; 929 struct mei_ext_hdr_gsc_f2h rx_msg; 930 unsigned int sg_len; 931 932 if (!cldev || !sg_in || !sg_out) 933 return -EINVAL; 934 935 cl = cldev->cl; 936 bus = cldev->bus; 937 938 dev_dbg(&cldev->dev, "client_id %u, fence_id %u\n", client_id, fence_id); 939 940 if (!bus->hbm_f_gsc_supported) 941 return -EOPNOTSUPP; 942 943 sg_out_nents = sg_nents(sg_out); 944 sg_in_nents = sg_nents(sg_in); 945 /* at least one entry in tx and rx sgls must be present */ 946 if (sg_out_nents <= 0 || sg_in_nents <= 0) 947 return -EINVAL; 948 949 buf_sz += (sg_out_nents + sg_in_nents) * sizeof(struct mei_gsc_sgl); 950 ext_hdr = kzalloc(buf_sz, GFP_KERNEL); 951 if (!ext_hdr) 952 return -ENOMEM; 953 954 /* construct the GSC message */ 955 ext_hdr->hdr.type = MEI_EXT_HDR_GSC; 956 ext_hdr->hdr.length = buf_sz / sizeof(u32); /* length is in dw */ 957 958 ext_hdr->client_id = client_id; 959 ext_hdr->addr_type = GSC_ADDRESS_TYPE_PHYSICAL_SGL; 960 ext_hdr->fence_id = fence_id; 961 ext_hdr->input_address_count = sg_in_nents; 962 ext_hdr->output_address_count = sg_out_nents; 963 ext_hdr->reserved[0] = 0; 964 ext_hdr->reserved[1] = 0; 965 966 /* copy in-sgl to the message */ 967 for (i = 0, sg = sg_in; i < sg_in_nents; i++, sg++) { 968 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg)); 969 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg)); 970 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE); 971 ext_hdr->sgl[i].length = (sg_len <= total_in_len) ? sg_len : total_in_len; 972 total_in_len -= ext_hdr->sgl[i].length; 973 } 974 975 /* copy out-sgl to the message */ 976 for (i = sg_in_nents, sg = sg_out; i < sg_in_nents + sg_out_nents; i++, sg++) { 977 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg)); 978 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg)); 979 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE); 980 ext_hdr->sgl[i].length = sg_len; 981 } 982 983 /* send the message to GSC */ 984 ret = __mei_cl_send(cl, (u8 *)ext_hdr, buf_sz, 0, MEI_CL_IO_SGL); 985 if (ret < 0) { 986 dev_err(&cldev->dev, "__mei_cl_send failed, returned %zd\n", ret); 987 goto end; 988 } 989 if (ret != buf_sz) { 990 dev_err(&cldev->dev, "__mei_cl_send returned %zd instead of expected %zd\n", 991 ret, buf_sz); 992 ret = -EIO; 993 goto end; 994 } 995 996 /* receive the reply from GSC, note that at this point sg_in should contain the reply */ 997 ret = __mei_cl_recv(cl, (u8 *)&rx_msg, sizeof(rx_msg), NULL, MEI_CL_IO_SGL, 0); 998 999 if (ret != sizeof(rx_msg)) { 1000 dev_err(&cldev->dev, "__mei_cl_recv returned %zd instead of expected %zd\n", 1001 ret, sizeof(rx_msg)); 1002 if (ret >= 0) 1003 ret = -EIO; 1004 goto end; 1005 } 1006 1007 /* check rx_msg.client_id and rx_msg.fence_id match the ones we send */ 1008 if (rx_msg.client_id != client_id || rx_msg.fence_id != fence_id) { 1009 dev_err(&cldev->dev, "received client_id/fence_id %u/%u instead of %u/%u sent\n", 1010 rx_msg.client_id, rx_msg.fence_id, client_id, fence_id); 1011 ret = -EFAULT; 1012 goto end; 1013 } 1014 1015 dev_dbg(&cldev->dev, "gsc command: successfully written %u bytes\n", rx_msg.written); 1016 ret = rx_msg.written; 1017 1018 end: 1019 kfree(ext_hdr); 1020 return ret; 1021 } 1022 EXPORT_SYMBOL_GPL(mei_cldev_send_gsc_command); 1023 1024 /** 1025 * mei_cl_device_find - find matching entry in the driver id table 1026 * 1027 * @cldev: me client device 1028 * @cldrv: me client driver 1029 * 1030 * Return: id on success; NULL if no id is matching 1031 */ 1032 static const 1033 struct mei_cl_device_id *mei_cl_device_find(const struct mei_cl_device *cldev, 1034 const struct mei_cl_driver *cldrv) 1035 { 1036 const struct mei_cl_device_id *id; 1037 const uuid_le *uuid; 1038 u8 version; 1039 bool match; 1040 1041 uuid = mei_me_cl_uuid(cldev->me_cl); 1042 version = mei_me_cl_ver(cldev->me_cl); 1043 1044 id = cldrv->id_table; 1045 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { 1046 if (!uuid_le_cmp(*uuid, id->uuid)) { 1047 match = true; 1048 1049 if (cldev->name[0]) 1050 if (strncmp(cldev->name, id->name, 1051 sizeof(id->name))) 1052 match = false; 1053 1054 if (id->version != MEI_CL_VERSION_ANY) 1055 if (id->version != version) 1056 match = false; 1057 if (match) 1058 return id; 1059 } 1060 1061 id++; 1062 } 1063 1064 return NULL; 1065 } 1066 1067 /** 1068 * mei_cl_device_match - device match function 1069 * 1070 * @dev: device 1071 * @drv: driver 1072 * 1073 * Return: 1 if matching device was found 0 otherwise 1074 */ 1075 static int mei_cl_device_match(struct device *dev, const struct device_driver *drv) 1076 { 1077 const struct mei_cl_device *cldev = to_mei_cl_device(dev); 1078 const struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); 1079 const struct mei_cl_device_id *found_id; 1080 1081 if (!cldev->do_match) 1082 return 0; 1083 1084 if (!cldrv || !cldrv->id_table) 1085 return 0; 1086 1087 found_id = mei_cl_device_find(cldev, cldrv); 1088 if (found_id) 1089 return 1; 1090 1091 return 0; 1092 } 1093 1094 /** 1095 * mei_cl_device_probe - bus probe function 1096 * 1097 * @dev: device 1098 * 1099 * Return: 0 on success; < 0 otherwise 1100 */ 1101 static int mei_cl_device_probe(struct device *dev) 1102 { 1103 struct mei_cl_device *cldev; 1104 struct mei_cl_driver *cldrv; 1105 const struct mei_cl_device_id *id; 1106 int ret; 1107 1108 cldev = to_mei_cl_device(dev); 1109 cldrv = to_mei_cl_driver(dev->driver); 1110 1111 if (!cldrv || !cldrv->probe) 1112 return -ENODEV; 1113 1114 id = mei_cl_device_find(cldev, cldrv); 1115 if (!id) 1116 return -ENODEV; 1117 1118 if (!mei_cl_bus_module_get(cldev)) { 1119 dev_err(&cldev->dev, "get hw module failed"); 1120 return -ENODEV; 1121 } 1122 1123 ret = cldrv->probe(cldev, id); 1124 if (ret) { 1125 mei_cl_bus_module_put(cldev); 1126 return ret; 1127 } 1128 1129 __module_get(THIS_MODULE); 1130 return 0; 1131 } 1132 1133 /** 1134 * mei_cl_device_remove - remove device from the bus 1135 * 1136 * @dev: device 1137 * 1138 * Return: 0 on success; < 0 otherwise 1139 */ 1140 static void mei_cl_device_remove(struct device *dev) 1141 { 1142 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1143 struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver); 1144 1145 if (cldrv->remove) 1146 cldrv->remove(cldev); 1147 1148 mei_cldev_unregister_callbacks(cldev); 1149 1150 mei_cl_bus_module_put(cldev); 1151 module_put(THIS_MODULE); 1152 } 1153 1154 static ssize_t name_show(struct device *dev, struct device_attribute *a, 1155 char *buf) 1156 { 1157 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1158 1159 return sysfs_emit(buf, "%s", cldev->name); 1160 } 1161 static DEVICE_ATTR_RO(name); 1162 1163 static ssize_t uuid_show(struct device *dev, struct device_attribute *a, 1164 char *buf) 1165 { 1166 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1167 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 1168 1169 return sysfs_emit(buf, "%pUl", uuid); 1170 } 1171 static DEVICE_ATTR_RO(uuid); 1172 1173 static ssize_t version_show(struct device *dev, struct device_attribute *a, 1174 char *buf) 1175 { 1176 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1177 u8 version = mei_me_cl_ver(cldev->me_cl); 1178 1179 return sysfs_emit(buf, "%02X", version); 1180 } 1181 static DEVICE_ATTR_RO(version); 1182 1183 static ssize_t modalias_show(struct device *dev, struct device_attribute *a, 1184 char *buf) 1185 { 1186 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1187 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 1188 u8 version = mei_me_cl_ver(cldev->me_cl); 1189 1190 return sysfs_emit(buf, "mei:%s:%pUl:%02X:", cldev->name, uuid, version); 1191 } 1192 static DEVICE_ATTR_RO(modalias); 1193 1194 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a, 1195 char *buf) 1196 { 1197 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1198 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl); 1199 1200 return sysfs_emit(buf, "%d", maxconn); 1201 } 1202 static DEVICE_ATTR_RO(max_conn); 1203 1204 static ssize_t fixed_show(struct device *dev, struct device_attribute *a, 1205 char *buf) 1206 { 1207 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1208 u8 fixed = mei_me_cl_fixed(cldev->me_cl); 1209 1210 return sysfs_emit(buf, "%d", fixed); 1211 } 1212 static DEVICE_ATTR_RO(fixed); 1213 1214 static ssize_t vtag_show(struct device *dev, struct device_attribute *a, 1215 char *buf) 1216 { 1217 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1218 bool vt = mei_me_cl_vt(cldev->me_cl); 1219 1220 return sysfs_emit(buf, "%d", vt); 1221 } 1222 static DEVICE_ATTR_RO(vtag); 1223 1224 static ssize_t max_len_show(struct device *dev, struct device_attribute *a, 1225 char *buf) 1226 { 1227 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1228 u32 maxlen = mei_me_cl_max_len(cldev->me_cl); 1229 1230 return sysfs_emit(buf, "%u", maxlen); 1231 } 1232 static DEVICE_ATTR_RO(max_len); 1233 1234 static struct attribute *mei_cldev_attrs[] = { 1235 &dev_attr_name.attr, 1236 &dev_attr_uuid.attr, 1237 &dev_attr_version.attr, 1238 &dev_attr_modalias.attr, 1239 &dev_attr_max_conn.attr, 1240 &dev_attr_fixed.attr, 1241 &dev_attr_vtag.attr, 1242 &dev_attr_max_len.attr, 1243 NULL, 1244 }; 1245 ATTRIBUTE_GROUPS(mei_cldev); 1246 1247 /** 1248 * mei_cl_device_uevent - me client bus uevent handler 1249 * 1250 * @dev: device 1251 * @env: uevent kobject 1252 * 1253 * Return: 0 on success -ENOMEM on when add_uevent_var fails 1254 */ 1255 static int mei_cl_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 1256 { 1257 const struct mei_cl_device *cldev = to_mei_cl_device(dev); 1258 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 1259 u8 version = mei_me_cl_ver(cldev->me_cl); 1260 1261 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version)) 1262 return -ENOMEM; 1263 1264 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) 1265 return -ENOMEM; 1266 1267 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) 1268 return -ENOMEM; 1269 1270 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:", 1271 cldev->name, uuid, version)) 1272 return -ENOMEM; 1273 1274 return 0; 1275 } 1276 1277 static const struct bus_type mei_cl_bus_type = { 1278 .name = "mei", 1279 .dev_groups = mei_cldev_groups, 1280 .match = mei_cl_device_match, 1281 .probe = mei_cl_device_probe, 1282 .remove = mei_cl_device_remove, 1283 .uevent = mei_cl_device_uevent, 1284 }; 1285 1286 static struct mei_device *mei_dev_bus_get(struct mei_device *bus) 1287 { 1288 if (bus) 1289 get_device(bus->dev); 1290 1291 return bus; 1292 } 1293 1294 static void mei_dev_bus_put(struct mei_device *bus) 1295 { 1296 if (bus) 1297 put_device(bus->dev); 1298 } 1299 1300 static void mei_cl_bus_dev_release(struct device *dev) 1301 { 1302 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1303 struct mei_device *mdev = cldev->cl->dev; 1304 struct mei_cl *cl; 1305 1306 mei_cl_flush_queues(cldev->cl, NULL); 1307 mei_me_cl_put(cldev->me_cl); 1308 mei_dev_bus_put(cldev->bus); 1309 1310 list_for_each_entry(cl, &mdev->file_list, link) 1311 WARN_ON(cl == cldev->cl); 1312 1313 kfree(cldev->cl); 1314 kfree(cldev); 1315 } 1316 1317 static const struct device_type mei_cl_device_type = { 1318 .release = mei_cl_bus_dev_release, 1319 }; 1320 1321 /** 1322 * mei_cl_bus_set_name - set device name for me client device 1323 * <controller>-<client device> 1324 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb 1325 * 1326 * @cldev: me client device 1327 */ 1328 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev) 1329 { 1330 dev_set_name(&cldev->dev, "%s-%pUl", 1331 dev_name(cldev->bus->dev), 1332 mei_me_cl_uuid(cldev->me_cl)); 1333 } 1334 1335 /** 1336 * mei_cl_bus_dev_alloc - initialize and allocate mei client device 1337 * 1338 * @bus: mei device 1339 * @me_cl: me client 1340 * 1341 * Return: allocated device structure or NULL on allocation failure 1342 */ 1343 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus, 1344 struct mei_me_client *me_cl) 1345 { 1346 struct mei_cl_device *cldev; 1347 struct mei_cl *cl; 1348 1349 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL); 1350 if (!cldev) 1351 return NULL; 1352 1353 cl = mei_cl_allocate(bus); 1354 if (!cl) { 1355 kfree(cldev); 1356 return NULL; 1357 } 1358 1359 device_initialize(&cldev->dev); 1360 cldev->dev.parent = bus->dev; 1361 cldev->dev.bus = &mei_cl_bus_type; 1362 cldev->dev.type = &mei_cl_device_type; 1363 cldev->bus = mei_dev_bus_get(bus); 1364 cldev->me_cl = mei_me_cl_get(me_cl); 1365 cldev->cl = cl; 1366 mei_cl_bus_set_name(cldev); 1367 cldev->is_added = 0; 1368 INIT_LIST_HEAD(&cldev->bus_list); 1369 device_enable_async_suspend(&cldev->dev); 1370 1371 return cldev; 1372 } 1373 1374 /** 1375 * mei_cl_bus_dev_setup - setup me client device 1376 * run fix up routines and set the device name 1377 * 1378 * @bus: mei device 1379 * @cldev: me client device 1380 * 1381 * Return: true if the device is eligible for enumeration 1382 */ 1383 static bool mei_cl_bus_dev_setup(struct mei_device *bus, 1384 struct mei_cl_device *cldev) 1385 { 1386 cldev->do_match = 1; 1387 mei_cl_bus_dev_fixup(cldev); 1388 1389 /* the device name can change during fix up */ 1390 if (cldev->do_match) 1391 mei_cl_bus_set_name(cldev); 1392 1393 return cldev->do_match == 1; 1394 } 1395 1396 /** 1397 * mei_cl_bus_dev_add - add me client devices 1398 * 1399 * @cldev: me client device 1400 * 1401 * Return: 0 on success; < 0 on failure 1402 */ 1403 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) 1404 { 1405 int ret; 1406 1407 dev_dbg(&cldev->dev, "adding %pUL:%02X\n", 1408 mei_me_cl_uuid(cldev->me_cl), 1409 mei_me_cl_ver(cldev->me_cl)); 1410 ret = device_add(&cldev->dev); 1411 if (!ret) 1412 cldev->is_added = 1; 1413 1414 return ret; 1415 } 1416 1417 /** 1418 * mei_cl_bus_dev_stop - stop the driver 1419 * 1420 * @cldev: me client device 1421 */ 1422 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) 1423 { 1424 cldev->do_match = 0; 1425 if (cldev->is_added) 1426 device_release_driver(&cldev->dev); 1427 } 1428 1429 /** 1430 * mei_cl_bus_dev_destroy - destroy me client devices object 1431 * 1432 * @cldev: me client device 1433 * 1434 * Locking: called under "dev->cl_bus_lock" lock 1435 */ 1436 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) 1437 { 1438 1439 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock)); 1440 1441 if (!cldev->is_added) 1442 return; 1443 1444 device_del(&cldev->dev); 1445 1446 list_del_init(&cldev->bus_list); 1447 1448 cldev->is_added = 0; 1449 put_device(&cldev->dev); 1450 } 1451 1452 /** 1453 * mei_cl_bus_remove_device - remove a devices form the bus 1454 * 1455 * @cldev: me client device 1456 */ 1457 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) 1458 { 1459 mei_cl_bus_dev_stop(cldev); 1460 mei_cl_bus_dev_destroy(cldev); 1461 } 1462 1463 /** 1464 * mei_cl_bus_remove_devices - remove all devices form the bus 1465 * 1466 * @bus: mei device 1467 */ 1468 void mei_cl_bus_remove_devices(struct mei_device *bus) 1469 { 1470 struct mei_cl_device *cldev, *next; 1471 1472 mutex_lock(&bus->cl_bus_lock); 1473 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) 1474 mei_cl_bus_remove_device(cldev); 1475 mutex_unlock(&bus->cl_bus_lock); 1476 } 1477 1478 1479 /** 1480 * mei_cl_bus_dev_init - allocate and initializes an mei client devices 1481 * based on me client 1482 * 1483 * @bus: mei device 1484 * @me_cl: me client 1485 * 1486 * Locking: called under "dev->cl_bus_lock" lock 1487 */ 1488 static void mei_cl_bus_dev_init(struct mei_device *bus, 1489 struct mei_me_client *me_cl) 1490 { 1491 struct mei_cl_device *cldev; 1492 1493 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock)); 1494 1495 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); 1496 1497 if (me_cl->bus_added) 1498 return; 1499 1500 cldev = mei_cl_bus_dev_alloc(bus, me_cl); 1501 if (!cldev) 1502 return; 1503 1504 me_cl->bus_added = true; 1505 list_add_tail(&cldev->bus_list, &bus->device_list); 1506 1507 } 1508 1509 /** 1510 * mei_cl_bus_rescan - scan me clients list and add create 1511 * devices for eligible clients 1512 * 1513 * @bus: mei device 1514 */ 1515 static void mei_cl_bus_rescan(struct mei_device *bus) 1516 { 1517 struct mei_cl_device *cldev, *n; 1518 struct mei_me_client *me_cl; 1519 1520 mutex_lock(&bus->cl_bus_lock); 1521 1522 down_read(&bus->me_clients_rwsem); 1523 list_for_each_entry(me_cl, &bus->me_clients, list) 1524 mei_cl_bus_dev_init(bus, me_cl); 1525 up_read(&bus->me_clients_rwsem); 1526 1527 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { 1528 1529 if (!mei_me_cl_is_active(cldev->me_cl)) { 1530 mei_cl_bus_remove_device(cldev); 1531 continue; 1532 } 1533 1534 if (cldev->is_added) 1535 continue; 1536 1537 if (mei_cl_bus_dev_setup(bus, cldev)) 1538 mei_cl_bus_dev_add(cldev); 1539 else { 1540 list_del_init(&cldev->bus_list); 1541 put_device(&cldev->dev); 1542 } 1543 } 1544 mutex_unlock(&bus->cl_bus_lock); 1545 1546 dev_dbg(bus->dev, "rescan end"); 1547 } 1548 1549 void mei_cl_bus_rescan_work(struct work_struct *work) 1550 { 1551 struct mei_device *bus = 1552 container_of(work, struct mei_device, bus_rescan_work); 1553 1554 mei_cl_bus_rescan(bus); 1555 } 1556 1557 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv, 1558 struct module *owner) 1559 { 1560 int err; 1561 1562 cldrv->driver.name = cldrv->name; 1563 cldrv->driver.owner = owner; 1564 cldrv->driver.bus = &mei_cl_bus_type; 1565 1566 err = driver_register(&cldrv->driver); 1567 if (err) 1568 return err; 1569 1570 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); 1571 1572 return 0; 1573 } 1574 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register); 1575 1576 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv) 1577 { 1578 driver_unregister(&cldrv->driver); 1579 1580 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); 1581 } 1582 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister); 1583 1584 1585 int __init mei_cl_bus_init(void) 1586 { 1587 return bus_register(&mei_cl_bus_type); 1588 } 1589 1590 void __exit mei_cl_bus_exit(void) 1591 { 1592 bus_unregister(&mei_cl_bus_type); 1593 } 1594