Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
0004  * Intel Management Engine Interface (Intel MEI) Linux driver
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/device.h>
0009 #include <linux/kernel.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/init.h>
0012 #include <linux/errno.h>
0013 #include <linux/slab.h>
0014 #include <linux/mutex.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/mei_cl_bus.h>
0017 
0018 #include "mei_dev.h"
0019 #include "client.h"
0020 
0021 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
0022 
0023 /**
0024  * __mei_cl_send - internal client send (write)
0025  *
0026  * @cl: host client
0027  * @buf: buffer to send
0028  * @length: buffer length
0029  * @vtag: virtual tag
0030  * @mode: sending mode
0031  *
0032  * Return: written size bytes or < 0 on error
0033  */
0034 ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
0035               unsigned int mode)
0036 {
0037     struct mei_device *bus;
0038     struct mei_cl_cb *cb;
0039     ssize_t rets;
0040 
0041     if (WARN_ON(!cl || !cl->dev))
0042         return -ENODEV;
0043 
0044     bus = cl->dev;
0045 
0046     mutex_lock(&bus->device_lock);
0047     if (bus->dev_state != MEI_DEV_ENABLED &&
0048         bus->dev_state != MEI_DEV_POWERING_DOWN) {
0049         rets = -ENODEV;
0050         goto out;
0051     }
0052 
0053     if (!mei_cl_is_connected(cl)) {
0054         rets = -ENODEV;
0055         goto out;
0056     }
0057 
0058     /* Check if we have an ME client device */
0059     if (!mei_me_cl_is_active(cl->me_cl)) {
0060         rets = -ENOTTY;
0061         goto out;
0062     }
0063 
0064     if (vtag) {
0065         /* Check if vtag is supported by client */
0066         rets = mei_cl_vt_support_check(cl);
0067         if (rets)
0068             goto out;
0069     }
0070 
0071     if (length > mei_cl_mtu(cl)) {
0072         rets = -EFBIG;
0073         goto out;
0074     }
0075 
0076     while (cl->tx_cb_queued >= bus->tx_queue_limit) {
0077         mutex_unlock(&bus->device_lock);
0078         rets = wait_event_interruptible(cl->tx_wait,
0079                 cl->writing_state == MEI_WRITE_COMPLETE ||
0080                 (!mei_cl_is_connected(cl)));
0081         mutex_lock(&bus->device_lock);
0082         if (rets) {
0083             if (signal_pending(current))
0084                 rets = -EINTR;
0085             goto out;
0086         }
0087         if (!mei_cl_is_connected(cl)) {
0088             rets = -ENODEV;
0089             goto out;
0090         }
0091     }
0092 
0093     cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
0094     if (!cb) {
0095         rets = -ENOMEM;
0096         goto out;
0097     }
0098     cb->vtag = vtag;
0099 
0100     cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
0101     cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
0102     memcpy(cb->buf.data, buf, length);
0103 
0104     rets = mei_cl_write(cl, cb);
0105 
0106 out:
0107     mutex_unlock(&bus->device_lock);
0108 
0109     return rets;
0110 }
0111 
0112 /**
0113  * __mei_cl_recv - internal client receive (read)
0114  *
0115  * @cl: host client
0116  * @buf: buffer to receive
0117  * @length: buffer length
0118  * @mode: io mode
0119  * @vtag: virtual tag
0120  * @timeout: recv timeout, 0 for infinite timeout
0121  *
0122  * Return: read size in bytes of < 0 on error
0123  */
0124 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
0125               unsigned int mode, unsigned long timeout)
0126 {
0127     struct mei_device *bus;
0128     struct mei_cl_cb *cb;
0129     size_t r_length;
0130     ssize_t rets;
0131     bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
0132 
0133     if (WARN_ON(!cl || !cl->dev))
0134         return -ENODEV;
0135 
0136     bus = cl->dev;
0137 
0138     mutex_lock(&bus->device_lock);
0139     if (bus->dev_state != MEI_DEV_ENABLED &&
0140         bus->dev_state != MEI_DEV_POWERING_DOWN) {
0141         rets = -ENODEV;
0142         goto out;
0143     }
0144 
0145     cb = mei_cl_read_cb(cl, NULL);
0146     if (cb)
0147         goto copy;
0148 
0149     rets = mei_cl_read_start(cl, length, NULL);
0150     if (rets && rets != -EBUSY)
0151         goto out;
0152 
0153     if (nonblock) {
0154         rets = -EAGAIN;
0155         goto out;
0156     }
0157 
0158     /* wait on event only if there is no other waiter */
0159     /* synchronized under device mutex */
0160     if (!waitqueue_active(&cl->rx_wait)) {
0161 
0162         mutex_unlock(&bus->device_lock);
0163 
0164         if (timeout) {
0165             rets = wait_event_interruptible_timeout
0166                     (cl->rx_wait,
0167                     mei_cl_read_cb(cl, NULL) ||
0168                     (!mei_cl_is_connected(cl)),
0169                     msecs_to_jiffies(timeout));
0170             if (rets == 0)
0171                 return -ETIME;
0172             if (rets < 0) {
0173                 if (signal_pending(current))
0174                     return -EINTR;
0175                 return -ERESTARTSYS;
0176             }
0177         } else {
0178             if (wait_event_interruptible
0179                     (cl->rx_wait,
0180                     mei_cl_read_cb(cl, NULL) ||
0181                     (!mei_cl_is_connected(cl)))) {
0182                 if (signal_pending(current))
0183                     return -EINTR;
0184                 return -ERESTARTSYS;
0185             }
0186         }
0187 
0188         mutex_lock(&bus->device_lock);
0189 
0190         if (!mei_cl_is_connected(cl)) {
0191             rets = -ENODEV;
0192             goto out;
0193         }
0194     }
0195 
0196     cb = mei_cl_read_cb(cl, NULL);
0197     if (!cb) {
0198         rets = 0;
0199         goto out;
0200     }
0201 
0202 copy:
0203     if (cb->status) {
0204         rets = cb->status;
0205         goto free;
0206     }
0207 
0208     r_length = min_t(size_t, length, cb->buf_idx);
0209     memcpy(buf, cb->buf.data, r_length);
0210     rets = r_length;
0211     if (vtag)
0212         *vtag = cb->vtag;
0213 
0214 free:
0215     mei_cl_del_rd_completed(cl, cb);
0216 out:
0217     mutex_unlock(&bus->device_lock);
0218 
0219     return rets;
0220 }
0221 
0222 /**
0223  * mei_cldev_send_vtag - me device send with vtag  (write)
0224  *
0225  * @cldev: me client device
0226  * @buf: buffer to send
0227  * @length: buffer length
0228  * @vtag: virtual tag
0229  *
0230  * Return:
0231  *  * written size in bytes
0232  *  * < 0 on error
0233  */
0234 
0235 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, const u8 *buf,
0236                 size_t length, u8 vtag)
0237 {
0238     struct mei_cl *cl = cldev->cl;
0239 
0240     return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING);
0241 }
0242 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag);
0243 
0244 /**
0245  * mei_cldev_recv_vtag - client receive with vtag (read)
0246  *
0247  * @cldev: me client device
0248  * @buf: buffer to receive
0249  * @length: buffer length
0250  * @vtag: virtual tag
0251  *
0252  * Return:
0253  * * read size in bytes
0254  * *  < 0 on error
0255  */
0256 
0257 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
0258                 u8 *vtag)
0259 {
0260     struct mei_cl *cl = cldev->cl;
0261 
0262     return __mei_cl_recv(cl, buf, length, vtag, 0, 0);
0263 }
0264 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag);
0265 
0266 /**
0267  * mei_cldev_recv_nonblock_vtag - non block client receive with vtag (read)
0268  *
0269  * @cldev: me client device
0270  * @buf: buffer to receive
0271  * @length: buffer length
0272  * @vtag: virtual tag
0273  *
0274  * Return:
0275  * * read size in bytes
0276  * * -EAGAIN if function will block.
0277  * * < 0 on other error
0278  */
0279 ssize_t mei_cldev_recv_nonblock_vtag(struct mei_cl_device *cldev, u8 *buf,
0280                      size_t length, u8 *vtag)
0281 {
0282     struct mei_cl *cl = cldev->cl;
0283 
0284     return __mei_cl_recv(cl, buf, length, vtag, MEI_CL_IO_RX_NONBLOCK, 0);
0285 }
0286 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock_vtag);
0287 
0288 /**
0289  * mei_cldev_send - me device send  (write)
0290  *
0291  * @cldev: me client device
0292  * @buf: buffer to send
0293  * @length: buffer length
0294  *
0295  * Return:
0296  *  * written size in bytes
0297  *  * < 0 on error
0298  */
0299 ssize_t mei_cldev_send(struct mei_cl_device *cldev, const u8 *buf, size_t length)
0300 {
0301     return mei_cldev_send_vtag(cldev, buf, length, 0);
0302 }
0303 EXPORT_SYMBOL_GPL(mei_cldev_send);
0304 
0305 /**
0306  * mei_cldev_recv - client receive (read)
0307  *
0308  * @cldev: me client device
0309  * @buf: buffer to receive
0310  * @length: buffer length
0311  *
0312  * Return: read size in bytes of < 0 on error
0313  */
0314 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
0315 {
0316     return mei_cldev_recv_vtag(cldev, buf, length, NULL);
0317 }
0318 EXPORT_SYMBOL_GPL(mei_cldev_recv);
0319 
0320 /**
0321  * mei_cldev_recv_nonblock - non block client receive (read)
0322  *
0323  * @cldev: me client device
0324  * @buf: buffer to receive
0325  * @length: buffer length
0326  *
0327  * Return: read size in bytes of < 0 on error
0328  *         -EAGAIN if function will block.
0329  */
0330 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
0331                 size_t length)
0332 {
0333     return mei_cldev_recv_nonblock_vtag(cldev, buf, length, NULL);
0334 }
0335 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
0336 
0337 /**
0338  * mei_cl_bus_rx_work - dispatch rx event for a bus device
0339  *
0340  * @work: work
0341  */
0342 static void mei_cl_bus_rx_work(struct work_struct *work)
0343 {
0344     struct mei_cl_device *cldev;
0345     struct mei_device *bus;
0346 
0347     cldev = container_of(work, struct mei_cl_device, rx_work);
0348 
0349     bus = cldev->bus;
0350 
0351     if (cldev->rx_cb)
0352         cldev->rx_cb(cldev);
0353 
0354     mutex_lock(&bus->device_lock);
0355     if (mei_cl_is_connected(cldev->cl))
0356         mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
0357     mutex_unlock(&bus->device_lock);
0358 }
0359 
0360 /**
0361  * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
0362  *
0363  * @work: work
0364  */
0365 static void mei_cl_bus_notif_work(struct work_struct *work)
0366 {
0367     struct mei_cl_device *cldev;
0368 
0369     cldev = container_of(work, struct mei_cl_device, notif_work);
0370 
0371     if (cldev->notif_cb)
0372         cldev->notif_cb(cldev);
0373 }
0374 
0375 /**
0376  * mei_cl_bus_notify_event - schedule notify cb on bus client
0377  *
0378  * @cl: host client
0379  *
0380  * Return: true if event was scheduled
0381  *         false if the client is not waiting for event
0382  */
0383 bool mei_cl_bus_notify_event(struct mei_cl *cl)
0384 {
0385     struct mei_cl_device *cldev = cl->cldev;
0386 
0387     if (!cldev || !cldev->notif_cb)
0388         return false;
0389 
0390     if (!cl->notify_ev)
0391         return false;
0392 
0393     schedule_work(&cldev->notif_work);
0394 
0395     cl->notify_ev = false;
0396 
0397     return true;
0398 }
0399 
0400 /**
0401  * mei_cl_bus_rx_event - schedule rx event
0402  *
0403  * @cl: host client
0404  *
0405  * Return: true if event was scheduled
0406  *         false if the client is not waiting for event
0407  */
0408 bool mei_cl_bus_rx_event(struct mei_cl *cl)
0409 {
0410     struct mei_cl_device *cldev = cl->cldev;
0411 
0412     if (!cldev || !cldev->rx_cb)
0413         return false;
0414 
0415     schedule_work(&cldev->rx_work);
0416 
0417     return true;
0418 }
0419 
0420 /**
0421  * mei_cldev_register_rx_cb - register Rx event callback
0422  *
0423  * @cldev: me client devices
0424  * @rx_cb: callback function
0425  *
0426  * Return: 0 on success
0427  *         -EALREADY if an callback is already registered
0428  *         <0 on other errors
0429  */
0430 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
0431 {
0432     struct mei_device *bus = cldev->bus;
0433     int ret;
0434 
0435     if (!rx_cb)
0436         return -EINVAL;
0437     if (cldev->rx_cb)
0438         return -EALREADY;
0439 
0440     cldev->rx_cb = rx_cb;
0441     INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
0442 
0443     mutex_lock(&bus->device_lock);
0444     if (mei_cl_is_connected(cldev->cl))
0445         ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
0446     else
0447         ret = -ENODEV;
0448     mutex_unlock(&bus->device_lock);
0449     if (ret && ret != -EBUSY) {
0450         cancel_work_sync(&cldev->rx_work);
0451         cldev->rx_cb = NULL;
0452         return ret;
0453     }
0454 
0455     return 0;
0456 }
0457 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
0458 
0459 /**
0460  * mei_cldev_register_notif_cb - register FW notification event callback
0461  *
0462  * @cldev: me client devices
0463  * @notif_cb: callback function
0464  *
0465  * Return: 0 on success
0466  *         -EALREADY if an callback is already registered
0467  *         <0 on other errors
0468  */
0469 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
0470                 mei_cldev_cb_t notif_cb)
0471 {
0472     struct mei_device *bus = cldev->bus;
0473     int ret;
0474 
0475     if (!notif_cb)
0476         return -EINVAL;
0477 
0478     if (cldev->notif_cb)
0479         return -EALREADY;
0480 
0481     cldev->notif_cb = notif_cb;
0482     INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
0483 
0484     mutex_lock(&bus->device_lock);
0485     ret = mei_cl_notify_request(cldev->cl, NULL, 1);
0486     mutex_unlock(&bus->device_lock);
0487     if (ret) {
0488         cancel_work_sync(&cldev->notif_work);
0489         cldev->notif_cb = NULL;
0490         return ret;
0491     }
0492 
0493     return 0;
0494 }
0495 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
0496 
0497 /**
0498  * mei_cldev_get_drvdata - driver data getter
0499  *
0500  * @cldev: mei client device
0501  *
0502  * Return: driver private data
0503  */
0504 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
0505 {
0506     return dev_get_drvdata(&cldev->dev);
0507 }
0508 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
0509 
0510 /**
0511  * mei_cldev_set_drvdata - driver data setter
0512  *
0513  * @cldev: mei client device
0514  * @data: data to store
0515  */
0516 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
0517 {
0518     dev_set_drvdata(&cldev->dev, data);
0519 }
0520 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
0521 
0522 /**
0523  * mei_cldev_uuid - return uuid of the underlying me client
0524  *
0525  * @cldev: mei client device
0526  *
0527  * Return: me client uuid
0528  */
0529 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
0530 {
0531     return mei_me_cl_uuid(cldev->me_cl);
0532 }
0533 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
0534 
0535 /**
0536  * mei_cldev_ver - return protocol version of the underlying me client
0537  *
0538  * @cldev: mei client device
0539  *
0540  * Return: me client protocol version
0541  */
0542 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
0543 {
0544     return mei_me_cl_ver(cldev->me_cl);
0545 }
0546 EXPORT_SYMBOL_GPL(mei_cldev_ver);
0547 
0548 /**
0549  * mei_cldev_enabled - check whether the device is enabled
0550  *
0551  * @cldev: mei client device
0552  *
0553  * Return: true if me client is initialized and connected
0554  */
0555 bool mei_cldev_enabled(const struct mei_cl_device *cldev)
0556 {
0557     return mei_cl_is_connected(cldev->cl);
0558 }
0559 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
0560 
0561 /**
0562  * mei_cl_bus_module_get - acquire module of the underlying
0563  *    hw driver.
0564  *
0565  * @cldev: mei client device
0566  *
0567  * Return: true on success; false if the module was removed.
0568  */
0569 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
0570 {
0571     return try_module_get(cldev->bus->dev->driver->owner);
0572 }
0573 
0574 /**
0575  * mei_cl_bus_module_put -  release the underlying hw module.
0576  *
0577  * @cldev: mei client device
0578  */
0579 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
0580 {
0581     module_put(cldev->bus->dev->driver->owner);
0582 }
0583 
0584 /**
0585  * mei_cl_bus_vtag - get bus vtag entry wrapper
0586  *     The tag for bus client is always first.
0587  *
0588  * @cl: host client
0589  *
0590  * Return: bus vtag or NULL
0591  */
0592 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
0593 {
0594     return list_first_entry_or_null(&cl->vtag_map,
0595                     struct mei_cl_vtag, list);
0596 }
0597 
0598 /**
0599  * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
0600  *
0601  * @cldev: me client device
0602  *
0603  * Return:
0604  * * 0 on success
0605  * * -ENOMEM if memory allocation failed
0606  */
0607 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
0608 {
0609     struct mei_cl *cl = cldev->cl;
0610     struct mei_cl_vtag *cl_vtag;
0611 
0612     /*
0613      * Bail out if the client does not supports vtags
0614      * or has already allocated one
0615      */
0616     if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
0617         return 0;
0618 
0619     cl_vtag = mei_cl_vtag_alloc(NULL, 0);
0620     if (IS_ERR(cl_vtag))
0621         return -ENOMEM;
0622 
0623     list_add_tail(&cl_vtag->list, &cl->vtag_map);
0624 
0625     return 0;
0626 }
0627 
0628 /**
0629  * mei_cl_bus_vtag_free - remove the bus entry from vtag map
0630  *
0631  * @cldev: me client device
0632  */
0633 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
0634 {
0635     struct mei_cl *cl = cldev->cl;
0636     struct mei_cl_vtag *cl_vtag;
0637 
0638     cl_vtag = mei_cl_bus_vtag(cl);
0639     if (!cl_vtag)
0640         return;
0641 
0642     list_del(&cl_vtag->list);
0643     kfree(cl_vtag);
0644 }
0645 
0646 void *mei_cldev_dma_map(struct mei_cl_device *cldev, u8 buffer_id, size_t size)
0647 {
0648     struct mei_device *bus;
0649     struct mei_cl *cl;
0650     int ret;
0651 
0652     if (!cldev || !buffer_id || !size)
0653         return ERR_PTR(-EINVAL);
0654 
0655     if (!IS_ALIGNED(size, MEI_FW_PAGE_SIZE)) {
0656         dev_err(&cldev->dev, "Map size should be aligned to %lu\n",
0657             MEI_FW_PAGE_SIZE);
0658         return ERR_PTR(-EINVAL);
0659     }
0660 
0661     cl = cldev->cl;
0662     bus = cldev->bus;
0663 
0664     mutex_lock(&bus->device_lock);
0665     if (cl->state == MEI_FILE_UNINITIALIZED) {
0666         ret = mei_cl_link(cl);
0667         if (ret)
0668             goto out;
0669         /* update pointers */
0670         cl->cldev = cldev;
0671     }
0672 
0673     ret = mei_cl_dma_alloc_and_map(cl, NULL, buffer_id, size);
0674 out:
0675     mutex_unlock(&bus->device_lock);
0676     if (ret)
0677         return ERR_PTR(ret);
0678     return cl->dma.vaddr;
0679 }
0680 EXPORT_SYMBOL_GPL(mei_cldev_dma_map);
0681 
0682 int mei_cldev_dma_unmap(struct mei_cl_device *cldev)
0683 {
0684     struct mei_device *bus;
0685     struct mei_cl *cl;
0686     int ret;
0687 
0688     if (!cldev)
0689         return -EINVAL;
0690 
0691     cl = cldev->cl;
0692     bus = cldev->bus;
0693 
0694     mutex_lock(&bus->device_lock);
0695     ret = mei_cl_dma_unmap(cl, NULL);
0696 
0697     mei_cl_flush_queues(cl, NULL);
0698     mei_cl_unlink(cl);
0699     mutex_unlock(&bus->device_lock);
0700     return ret;
0701 }
0702 EXPORT_SYMBOL_GPL(mei_cldev_dma_unmap);
0703 
0704 /**
0705  * mei_cldev_enable - enable me client device
0706  *     create connection with me client
0707  *
0708  * @cldev: me client device
0709  *
0710  * Return: 0 on success and < 0 on error
0711  */
0712 int mei_cldev_enable(struct mei_cl_device *cldev)
0713 {
0714     struct mei_device *bus = cldev->bus;
0715     struct mei_cl *cl;
0716     int ret;
0717 
0718     cl = cldev->cl;
0719 
0720     mutex_lock(&bus->device_lock);
0721     if (cl->state == MEI_FILE_UNINITIALIZED) {
0722         ret = mei_cl_link(cl);
0723         if (ret)
0724             goto out;
0725         /* update pointers */
0726         cl->cldev = cldev;
0727     }
0728 
0729     if (mei_cl_is_connected(cl)) {
0730         ret = 0;
0731         goto out;
0732     }
0733 
0734     if (!mei_me_cl_is_active(cldev->me_cl)) {
0735         dev_err(&cldev->dev, "me client is not active\n");
0736         ret = -ENOTTY;
0737         goto out;
0738     }
0739 
0740     ret = mei_cl_bus_vtag_alloc(cldev);
0741     if (ret)
0742         goto out;
0743 
0744     ret = mei_cl_connect(cl, cldev->me_cl, NULL);
0745     if (ret < 0) {
0746         dev_err(&cldev->dev, "cannot connect\n");
0747         mei_cl_bus_vtag_free(cldev);
0748     }
0749 
0750 out:
0751     mutex_unlock(&bus->device_lock);
0752 
0753     return ret;
0754 }
0755 EXPORT_SYMBOL_GPL(mei_cldev_enable);
0756 
0757 /**
0758  * mei_cldev_unregister_callbacks - internal wrapper for unregistering
0759  *  callbacks.
0760  *
0761  * @cldev: client device
0762  */
0763 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
0764 {
0765     if (cldev->rx_cb) {
0766         cancel_work_sync(&cldev->rx_work);
0767         cldev->rx_cb = NULL;
0768     }
0769 
0770     if (cldev->notif_cb) {
0771         cancel_work_sync(&cldev->notif_work);
0772         cldev->notif_cb = NULL;
0773     }
0774 }
0775 
0776 /**
0777  * mei_cldev_disable - disable me client device
0778  *     disconnect form the me client
0779  *
0780  * @cldev: me client device
0781  *
0782  * Return: 0 on success and < 0 on error
0783  */
0784 int mei_cldev_disable(struct mei_cl_device *cldev)
0785 {
0786     struct mei_device *bus;
0787     struct mei_cl *cl;
0788     int err;
0789 
0790     if (!cldev)
0791         return -ENODEV;
0792 
0793     cl = cldev->cl;
0794 
0795     bus = cldev->bus;
0796 
0797     mei_cldev_unregister_callbacks(cldev);
0798 
0799     mutex_lock(&bus->device_lock);
0800 
0801     mei_cl_bus_vtag_free(cldev);
0802 
0803     if (!mei_cl_is_connected(cl)) {
0804         dev_dbg(bus->dev, "Already disconnected\n");
0805         err = 0;
0806         goto out;
0807     }
0808 
0809     err = mei_cl_disconnect(cl);
0810     if (err < 0)
0811         dev_err(bus->dev, "Could not disconnect from the ME client\n");
0812 
0813 out:
0814     /* Flush queues and remove any pending read unless we have mapped DMA */
0815     if (!cl->dma_mapped) {
0816         mei_cl_flush_queues(cl, NULL);
0817         mei_cl_unlink(cl);
0818     }
0819 
0820     mutex_unlock(&bus->device_lock);
0821     return err;
0822 }
0823 EXPORT_SYMBOL_GPL(mei_cldev_disable);
0824 
0825 /**
0826  * mei_cl_device_find - find matching entry in the driver id table
0827  *
0828  * @cldev: me client device
0829  * @cldrv: me client driver
0830  *
0831  * Return: id on success; NULL if no id is matching
0832  */
0833 static const
0834 struct mei_cl_device_id *mei_cl_device_find(const struct mei_cl_device *cldev,
0835                         const struct mei_cl_driver *cldrv)
0836 {
0837     const struct mei_cl_device_id *id;
0838     const uuid_le *uuid;
0839     u8 version;
0840     bool match;
0841 
0842     uuid = mei_me_cl_uuid(cldev->me_cl);
0843     version = mei_me_cl_ver(cldev->me_cl);
0844 
0845     id = cldrv->id_table;
0846     while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
0847         if (!uuid_le_cmp(*uuid, id->uuid)) {
0848             match = true;
0849 
0850             if (cldev->name[0])
0851                 if (strncmp(cldev->name, id->name,
0852                         sizeof(id->name)))
0853                     match = false;
0854 
0855             if (id->version != MEI_CL_VERSION_ANY)
0856                 if (id->version != version)
0857                     match = false;
0858             if (match)
0859                 return id;
0860         }
0861 
0862         id++;
0863     }
0864 
0865     return NULL;
0866 }
0867 
0868 /**
0869  * mei_cl_device_match  - device match function
0870  *
0871  * @dev: device
0872  * @drv: driver
0873  *
0874  * Return:  1 if matching device was found 0 otherwise
0875  */
0876 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
0877 {
0878     const struct mei_cl_device *cldev = to_mei_cl_device(dev);
0879     const struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
0880     const struct mei_cl_device_id *found_id;
0881 
0882     if (!cldev)
0883         return 0;
0884 
0885     if (!cldev->do_match)
0886         return 0;
0887 
0888     if (!cldrv || !cldrv->id_table)
0889         return 0;
0890 
0891     found_id = mei_cl_device_find(cldev, cldrv);
0892     if (found_id)
0893         return 1;
0894 
0895     return 0;
0896 }
0897 
0898 /**
0899  * mei_cl_device_probe - bus probe function
0900  *
0901  * @dev: device
0902  *
0903  * Return:  0 on success; < 0 otherwise
0904  */
0905 static int mei_cl_device_probe(struct device *dev)
0906 {
0907     struct mei_cl_device *cldev;
0908     struct mei_cl_driver *cldrv;
0909     const struct mei_cl_device_id *id;
0910     int ret;
0911 
0912     cldev = to_mei_cl_device(dev);
0913     cldrv = to_mei_cl_driver(dev->driver);
0914 
0915     if (!cldev)
0916         return 0;
0917 
0918     if (!cldrv || !cldrv->probe)
0919         return -ENODEV;
0920 
0921     id = mei_cl_device_find(cldev, cldrv);
0922     if (!id)
0923         return -ENODEV;
0924 
0925     if (!mei_cl_bus_module_get(cldev)) {
0926         dev_err(&cldev->dev, "get hw module failed");
0927         return -ENODEV;
0928     }
0929 
0930     ret = cldrv->probe(cldev, id);
0931     if (ret) {
0932         mei_cl_bus_module_put(cldev);
0933         return ret;
0934     }
0935 
0936     __module_get(THIS_MODULE);
0937     return 0;
0938 }
0939 
0940 /**
0941  * mei_cl_device_remove - remove device from the bus
0942  *
0943  * @dev: device
0944  *
0945  * Return:  0 on success; < 0 otherwise
0946  */
0947 static void mei_cl_device_remove(struct device *dev)
0948 {
0949     struct mei_cl_device *cldev = to_mei_cl_device(dev);
0950     struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver);
0951 
0952     if (cldrv->remove)
0953         cldrv->remove(cldev);
0954 
0955     mei_cldev_unregister_callbacks(cldev);
0956 
0957     mei_cl_bus_module_put(cldev);
0958     module_put(THIS_MODULE);
0959 }
0960 
0961 static ssize_t name_show(struct device *dev, struct device_attribute *a,
0962                  char *buf)
0963 {
0964     struct mei_cl_device *cldev = to_mei_cl_device(dev);
0965 
0966     return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
0967 }
0968 static DEVICE_ATTR_RO(name);
0969 
0970 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
0971                  char *buf)
0972 {
0973     struct mei_cl_device *cldev = to_mei_cl_device(dev);
0974     const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
0975 
0976     return sprintf(buf, "%pUl", uuid);
0977 }
0978 static DEVICE_ATTR_RO(uuid);
0979 
0980 static ssize_t version_show(struct device *dev, struct device_attribute *a,
0981                  char *buf)
0982 {
0983     struct mei_cl_device *cldev = to_mei_cl_device(dev);
0984     u8 version = mei_me_cl_ver(cldev->me_cl);
0985 
0986     return sprintf(buf, "%02X", version);
0987 }
0988 static DEVICE_ATTR_RO(version);
0989 
0990 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
0991                  char *buf)
0992 {
0993     struct mei_cl_device *cldev = to_mei_cl_device(dev);
0994     const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
0995     u8 version = mei_me_cl_ver(cldev->me_cl);
0996 
0997     return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
0998              cldev->name, uuid, version);
0999 }
1000 static DEVICE_ATTR_RO(modalias);
1001 
1002 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
1003                  char *buf)
1004 {
1005     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1006     u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
1007 
1008     return sprintf(buf, "%d", maxconn);
1009 }
1010 static DEVICE_ATTR_RO(max_conn);
1011 
1012 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
1013               char *buf)
1014 {
1015     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1016     u8 fixed = mei_me_cl_fixed(cldev->me_cl);
1017 
1018     return sprintf(buf, "%d", fixed);
1019 }
1020 static DEVICE_ATTR_RO(fixed);
1021 
1022 static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
1023              char *buf)
1024 {
1025     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1026     bool vt = mei_me_cl_vt(cldev->me_cl);
1027 
1028     return sprintf(buf, "%d", vt);
1029 }
1030 static DEVICE_ATTR_RO(vtag);
1031 
1032 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
1033                 char *buf)
1034 {
1035     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1036     u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
1037 
1038     return sprintf(buf, "%u", maxlen);
1039 }
1040 static DEVICE_ATTR_RO(max_len);
1041 
1042 static struct attribute *mei_cldev_attrs[] = {
1043     &dev_attr_name.attr,
1044     &dev_attr_uuid.attr,
1045     &dev_attr_version.attr,
1046     &dev_attr_modalias.attr,
1047     &dev_attr_max_conn.attr,
1048     &dev_attr_fixed.attr,
1049     &dev_attr_vtag.attr,
1050     &dev_attr_max_len.attr,
1051     NULL,
1052 };
1053 ATTRIBUTE_GROUPS(mei_cldev);
1054 
1055 /**
1056  * mei_cl_device_uevent - me client bus uevent handler
1057  *
1058  * @dev: device
1059  * @env: uevent kobject
1060  *
1061  * Return: 0 on success -ENOMEM on when add_uevent_var fails
1062  */
1063 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1064 {
1065     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1066     const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1067     u8 version = mei_me_cl_ver(cldev->me_cl);
1068 
1069     if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
1070         return -ENOMEM;
1071 
1072     if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
1073         return -ENOMEM;
1074 
1075     if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
1076         return -ENOMEM;
1077 
1078     if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
1079                cldev->name, uuid, version))
1080         return -ENOMEM;
1081 
1082     return 0;
1083 }
1084 
1085 static struct bus_type mei_cl_bus_type = {
1086     .name       = "mei",
1087     .dev_groups = mei_cldev_groups,
1088     .match      = mei_cl_device_match,
1089     .probe      = mei_cl_device_probe,
1090     .remove     = mei_cl_device_remove,
1091     .uevent     = mei_cl_device_uevent,
1092 };
1093 
1094 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
1095 {
1096     if (bus)
1097         get_device(bus->dev);
1098 
1099     return bus;
1100 }
1101 
1102 static void mei_dev_bus_put(struct mei_device *bus)
1103 {
1104     if (bus)
1105         put_device(bus->dev);
1106 }
1107 
1108 static void mei_cl_bus_dev_release(struct device *dev)
1109 {
1110     struct mei_cl_device *cldev = to_mei_cl_device(dev);
1111 
1112     if (!cldev)
1113         return;
1114 
1115     mei_cl_flush_queues(cldev->cl, NULL);
1116     mei_me_cl_put(cldev->me_cl);
1117     mei_dev_bus_put(cldev->bus);
1118     mei_cl_unlink(cldev->cl);
1119     kfree(cldev->cl);
1120     kfree(cldev);
1121 }
1122 
1123 static const struct device_type mei_cl_device_type = {
1124     .release = mei_cl_bus_dev_release,
1125 };
1126 
1127 /**
1128  * mei_cl_bus_set_name - set device name for me client device
1129  *  <controller>-<client device>
1130  *  Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
1131  *
1132  * @cldev: me client device
1133  */
1134 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
1135 {
1136     dev_set_name(&cldev->dev, "%s-%pUl",
1137              dev_name(cldev->bus->dev),
1138              mei_me_cl_uuid(cldev->me_cl));
1139 }
1140 
1141 /**
1142  * mei_cl_bus_dev_alloc - initialize and allocate mei client device
1143  *
1144  * @bus: mei device
1145  * @me_cl: me client
1146  *
1147  * Return: allocated device structur or NULL on allocation failure
1148  */
1149 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
1150                           struct mei_me_client *me_cl)
1151 {
1152     struct mei_cl_device *cldev;
1153     struct mei_cl *cl;
1154 
1155     cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1156     if (!cldev)
1157         return NULL;
1158 
1159     cl = mei_cl_allocate(bus);
1160     if (!cl) {
1161         kfree(cldev);
1162         return NULL;
1163     }
1164 
1165     device_initialize(&cldev->dev);
1166     cldev->dev.parent = bus->dev;
1167     cldev->dev.bus    = &mei_cl_bus_type;
1168     cldev->dev.type   = &mei_cl_device_type;
1169     cldev->bus        = mei_dev_bus_get(bus);
1170     cldev->me_cl      = mei_me_cl_get(me_cl);
1171     cldev->cl         = cl;
1172     mei_cl_bus_set_name(cldev);
1173     cldev->is_added   = 0;
1174     INIT_LIST_HEAD(&cldev->bus_list);
1175 
1176     return cldev;
1177 }
1178 
1179 /**
1180  * mei_cl_bus_dev_setup - setup me client device
1181  *    run fix up routines and set the device name
1182  *
1183  * @bus: mei device
1184  * @cldev: me client device
1185  *
1186  * Return: true if the device is eligible for enumeration
1187  */
1188 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
1189                  struct mei_cl_device *cldev)
1190 {
1191     cldev->do_match = 1;
1192     mei_cl_bus_dev_fixup(cldev);
1193 
1194     /* the device name can change during fix up */
1195     if (cldev->do_match)
1196         mei_cl_bus_set_name(cldev);
1197 
1198     return cldev->do_match == 1;
1199 }
1200 
1201 /**
1202  * mei_cl_bus_dev_add - add me client devices
1203  *
1204  * @cldev: me client device
1205  *
1206  * Return: 0 on success; < 0 on failre
1207  */
1208 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
1209 {
1210     int ret;
1211 
1212     dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
1213         mei_me_cl_uuid(cldev->me_cl),
1214         mei_me_cl_ver(cldev->me_cl));
1215     ret = device_add(&cldev->dev);
1216     if (!ret)
1217         cldev->is_added = 1;
1218 
1219     return ret;
1220 }
1221 
1222 /**
1223  * mei_cl_bus_dev_stop - stop the driver
1224  *
1225  * @cldev: me client device
1226  */
1227 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1228 {
1229     if (cldev->is_added)
1230         device_release_driver(&cldev->dev);
1231 }
1232 
1233 /**
1234  * mei_cl_bus_dev_destroy - destroy me client devices object
1235  *
1236  * @cldev: me client device
1237  *
1238  * Locking: called under "dev->cl_bus_lock" lock
1239  */
1240 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1241 {
1242 
1243     WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1244 
1245     if (!cldev->is_added)
1246         return;
1247 
1248     device_del(&cldev->dev);
1249 
1250     list_del_init(&cldev->bus_list);
1251 
1252     cldev->is_added = 0;
1253     put_device(&cldev->dev);
1254 }
1255 
1256 /**
1257  * mei_cl_bus_remove_device - remove a devices form the bus
1258  *
1259  * @cldev: me client device
1260  */
1261 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1262 {
1263     mei_cl_bus_dev_stop(cldev);
1264     mei_cl_bus_dev_destroy(cldev);
1265 }
1266 
1267 /**
1268  * mei_cl_bus_remove_devices - remove all devices form the bus
1269  *
1270  * @bus: mei device
1271  */
1272 void mei_cl_bus_remove_devices(struct mei_device *bus)
1273 {
1274     struct mei_cl_device *cldev, *next;
1275 
1276     mutex_lock(&bus->cl_bus_lock);
1277     list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1278         mei_cl_bus_remove_device(cldev);
1279     mutex_unlock(&bus->cl_bus_lock);
1280 }
1281 
1282 
1283 /**
1284  * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1285  *     based on me client
1286  *
1287  * @bus: mei device
1288  * @me_cl: me client
1289  *
1290  * Locking: called under "dev->cl_bus_lock" lock
1291  */
1292 static void mei_cl_bus_dev_init(struct mei_device *bus,
1293                 struct mei_me_client *me_cl)
1294 {
1295     struct mei_cl_device *cldev;
1296 
1297     WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1298 
1299     dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1300 
1301     if (me_cl->bus_added)
1302         return;
1303 
1304     cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1305     if (!cldev)
1306         return;
1307 
1308     me_cl->bus_added = true;
1309     list_add_tail(&cldev->bus_list, &bus->device_list);
1310 
1311 }
1312 
1313 /**
1314  * mei_cl_bus_rescan - scan me clients list and add create
1315  *    devices for eligible clients
1316  *
1317  * @bus: mei device
1318  */
1319 static void mei_cl_bus_rescan(struct mei_device *bus)
1320 {
1321     struct mei_cl_device *cldev, *n;
1322     struct mei_me_client *me_cl;
1323 
1324     mutex_lock(&bus->cl_bus_lock);
1325 
1326     down_read(&bus->me_clients_rwsem);
1327     list_for_each_entry(me_cl, &bus->me_clients, list)
1328         mei_cl_bus_dev_init(bus, me_cl);
1329     up_read(&bus->me_clients_rwsem);
1330 
1331     list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1332 
1333         if (!mei_me_cl_is_active(cldev->me_cl)) {
1334             mei_cl_bus_remove_device(cldev);
1335             continue;
1336         }
1337 
1338         if (cldev->is_added)
1339             continue;
1340 
1341         if (mei_cl_bus_dev_setup(bus, cldev))
1342             mei_cl_bus_dev_add(cldev);
1343         else {
1344             list_del_init(&cldev->bus_list);
1345             put_device(&cldev->dev);
1346         }
1347     }
1348     mutex_unlock(&bus->cl_bus_lock);
1349 
1350     dev_dbg(bus->dev, "rescan end");
1351 }
1352 
1353 void mei_cl_bus_rescan_work(struct work_struct *work)
1354 {
1355     struct mei_device *bus =
1356         container_of(work, struct mei_device, bus_rescan_work);
1357 
1358     mei_cl_bus_rescan(bus);
1359 }
1360 
1361 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1362                 struct module *owner)
1363 {
1364     int err;
1365 
1366     cldrv->driver.name = cldrv->name;
1367     cldrv->driver.owner = owner;
1368     cldrv->driver.bus = &mei_cl_bus_type;
1369 
1370     err = driver_register(&cldrv->driver);
1371     if (err)
1372         return err;
1373 
1374     pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1375 
1376     return 0;
1377 }
1378 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1379 
1380 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1381 {
1382     driver_unregister(&cldrv->driver);
1383 
1384     pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1385 }
1386 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1387 
1388 
1389 int __init mei_cl_bus_init(void)
1390 {
1391     return bus_register(&mei_cl_bus_type);
1392 }
1393 
1394 void __exit mei_cl_bus_exit(void)
1395 {
1396     bus_unregister(&mei_cl_bus_type);
1397 }