Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
| 4 | * Copyright (c) 2003-2012, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/fcntl.h> |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 22 | #include <linux/ioctl.h> |
| 23 | #include <linux/cdev.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/uuid.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/uaccess.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 30 | #include <linux/slab.h> |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 31 | |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 32 | #include <linux/mei.h> |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 33 | |
| 34 | #include "mei_dev.h" |
Tomas Winkler | 0edb23f | 2013-01-08 23:07:12 +0200 | [diff] [blame] | 35 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 36 | #include "client.h" |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 37 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 38 | const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, |
| 39 | 0xac, 0xa8, 0x46, 0xe0, |
| 40 | 0xff, 0x65, 0x81, 0x4c); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * mei_amthif_reset_params - initializes mei device iamthif |
| 44 | * |
| 45 | * @dev: the device structure |
| 46 | */ |
| 47 | void mei_amthif_reset_params(struct mei_device *dev) |
| 48 | { |
| 49 | /* reset iamthif parameters. */ |
| 50 | dev->iamthif_current_cb = NULL; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 51 | dev->iamthif_canceled = false; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 52 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
Alexander Usyskin | 4a70457 | 2013-09-02 13:29:47 +0300 | [diff] [blame] | 53 | dev->iamthif_stall_timer = 0; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 54 | dev->iamthif_open_count = 0; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 58 | * mei_amthif_host_init - mei initialization amthif client. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 59 | * |
| 60 | * @dev: the device structure |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 61 | * @me_cl: me client |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 62 | * |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 63 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 64 | */ |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 65 | int mei_amthif_host_init(struct mei_device *dev, struct mei_me_client *me_cl) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 66 | { |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 67 | struct mei_cl *cl = &dev->iamthif_cl; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 68 | int ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 69 | |
Tomas Winkler | 6222f7b | 2013-01-08 23:07:23 +0200 | [diff] [blame] | 70 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 71 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 72 | mei_cl_init(cl, dev); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 73 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 74 | ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 75 | if (ret < 0) { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 76 | dev_err(dev->dev, "amthif: failed cl_link %d\n", ret); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 77 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 78 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 79 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 80 | ret = mei_cl_connect(cl, me_cl, NULL); |
Tomas Winkler | 6409285 | 2014-02-17 15:13:21 +0200 | [diff] [blame] | 81 | |
| 82 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 83 | |
| 84 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 88 | * mei_amthif_read - read data from AMTHIF client |
| 89 | * |
| 90 | * @dev: the device structure |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 91 | * @file: pointer to file object |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 92 | * @ubuf: pointer to user data in user space |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 93 | * @length: data length to read |
| 94 | * @offset: data read offset |
| 95 | * |
| 96 | * Locking: called under "dev->device_lock" lock |
| 97 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 98 | * Return: |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 99 | * returned data length on success, |
| 100 | * zero if no data to read, |
| 101 | * negative on failure. |
| 102 | */ |
| 103 | int mei_amthif_read(struct mei_device *dev, struct file *file, |
| 104 | char __user *ubuf, size_t length, loff_t *offset) |
| 105 | { |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 106 | struct mei_cl *cl = file->private_data; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 107 | struct mei_cl_cb *cb; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 108 | int rets; |
| 109 | int wait_ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 110 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 111 | dev_dbg(dev->dev, "checking amthif data\n"); |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 112 | cb = mei_cl_read_cb(cl, file); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 113 | |
| 114 | /* Check for if we can block or not*/ |
| 115 | if (cb == NULL && file->f_flags & O_NONBLOCK) |
| 116 | return -EAGAIN; |
| 117 | |
| 118 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 119 | dev_dbg(dev->dev, "waiting for amthif data\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 120 | while (cb == NULL) { |
| 121 | /* unlock the Mutex */ |
| 122 | mutex_unlock(&dev->device_lock); |
| 123 | |
| 124 | wait_ret = wait_event_interruptible(dev->iamthif_cl.wait, |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 125 | !list_empty(&cl->rd_completed)); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 126 | |
Alexey Khoroshilov | e6028db | 2012-12-22 01:44:16 +0400 | [diff] [blame] | 127 | /* Locking again the Mutex */ |
| 128 | mutex_lock(&dev->device_lock); |
| 129 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 130 | if (wait_ret) |
| 131 | return -ERESTARTSYS; |
| 132 | |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 133 | cb = mei_cl_read_cb(cl, file); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 136 | if (cb->status) { |
| 137 | rets = cb->status; |
| 138 | dev_dbg(dev->dev, "read operation failed %d\n", rets); |
| 139 | goto free; |
| 140 | } |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 141 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 142 | dev_dbg(dev->dev, "Got amthif data\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 143 | /* if the whole message will fit remove it from the list */ |
| 144 | if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset)) |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 145 | list_del_init(&cb->list); |
Alexander Usyskin | 5ba0bf4 | 2016-02-07 23:35:21 +0200 | [diff] [blame] | 146 | else if (cb->buf_idx <= *offset) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 147 | /* end of the message has been reached */ |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 148 | list_del_init(&cb->list); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 149 | rets = 0; |
| 150 | goto free; |
| 151 | } |
| 152 | /* else means that not full buffer will be read and do not |
| 153 | * remove message from deletion list |
| 154 | */ |
| 155 | |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 156 | dev_dbg(dev->dev, "amthif cb->buf.size - %zd cb->buf_idx - %zd\n", |
| 157 | cb->buf.size, cb->buf_idx); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 158 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 159 | /* length is being truncated to PAGE_SIZE, however, |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 160 | * the buf_idx may point beyond */ |
| 161 | length = min_t(size_t, length, (cb->buf_idx - *offset)); |
| 162 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 163 | if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 164 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 165 | rets = -EFAULT; |
Tomas Winkler | 34e267d | 2014-03-16 14:35:57 +0200 | [diff] [blame] | 166 | } else { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 167 | rets = length; |
| 168 | if ((*offset + length) < cb->buf_idx) { |
| 169 | *offset += length; |
| 170 | goto out; |
| 171 | } |
| 172 | } |
| 173 | free: |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 174 | dev_dbg(dev->dev, "free amthif cb memory.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 175 | *offset = 0; |
| 176 | mei_io_cb_free(cb); |
| 177 | out: |
| 178 | return rets; |
| 179 | } |
| 180 | |
| 181 | /** |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 182 | * mei_amthif_read_start - queue message for sending read credential |
| 183 | * |
| 184 | * @cl: host client |
| 185 | * @file: file pointer of message recipient |
| 186 | * |
| 187 | * Return: 0 on success, <0 on failure. |
| 188 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 189 | static int mei_amthif_read_start(struct mei_cl *cl, const struct file *file) |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 190 | { |
| 191 | struct mei_device *dev = cl->dev; |
| 192 | struct mei_cl_cb *cb; |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 193 | int rets; |
| 194 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 195 | cb = mei_io_cb_init(cl, MEI_FOP_READ, file); |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 196 | if (!cb) { |
| 197 | rets = -ENOMEM; |
| 198 | goto err; |
| 199 | } |
| 200 | |
Alexander Usyskin | 9e23936 | 2015-05-04 09:43:58 +0300 | [diff] [blame] | 201 | rets = mei_io_cb_alloc_buf(cb, mei_cl_mtu(cl)); |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 202 | if (rets) |
| 203 | goto err; |
| 204 | |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 205 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 206 | |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 207 | dev->iamthif_state = MEI_IAMTHIF_READING; |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 208 | dev->iamthif_fp = cb->fp; |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 209 | dev->iamthif_current_cb = cb; |
| 210 | |
| 211 | return 0; |
| 212 | err: |
| 213 | mei_io_cb_free(cb); |
| 214 | return rets; |
| 215 | } |
| 216 | |
| 217 | /** |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 218 | * mei_amthif_send_cmd - send amthif command to the ME |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 219 | * |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 220 | * @cl: the host client |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 221 | * @cb: mei call back struct |
| 222 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 223 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 224 | */ |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 225 | static int mei_amthif_send_cmd(struct mei_cl *cl, struct mei_cl_cb *cb) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 226 | { |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 227 | struct mei_device *dev; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 228 | int ret; |
| 229 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 230 | if (!cl->dev || !cb) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 231 | return -ENODEV; |
| 232 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 233 | dev = cl->dev; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 234 | |
| 235 | dev->iamthif_state = MEI_IAMTHIF_WRITING; |
| 236 | dev->iamthif_current_cb = cb; |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 237 | dev->iamthif_fp = cb->fp; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 238 | dev->iamthif_canceled = false; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 239 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 240 | ret = mei_cl_write(cl, cb, false); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 241 | if (ret < 0) |
| 242 | return ret; |
| 243 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 244 | if (cb->completed) |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 245 | cb->status = mei_amthif_read_start(cl, cb->fp); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 246 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 251 | * mei_amthif_run_next_cmd - send next amt command from queue |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 252 | * |
| 253 | * @dev: the device structure |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 254 | * |
| 255 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 256 | */ |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 257 | int mei_amthif_run_next_cmd(struct mei_device *dev) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 258 | { |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 259 | struct mei_cl *cl = &dev->iamthif_cl; |
Alexander Usyskin | 05e314e | 2014-08-14 17:22:21 +0300 | [diff] [blame] | 260 | struct mei_cl_cb *cb; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 261 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 262 | dev->iamthif_canceled = false; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 263 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 264 | dev->iamthif_fp = NULL; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 265 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 266 | dev_dbg(dev->dev, "complete amthif cmd_list cb.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 267 | |
Alexander Usyskin | 140c755 | 2014-10-02 13:39:31 +0300 | [diff] [blame] | 268 | cb = list_first_entry_or_null(&dev->amthif_cmd_list.list, |
| 269 | typeof(*cb), list); |
| 270 | if (!cb) |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 271 | return 0; |
| 272 | |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 273 | list_del_init(&cb->list); |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 274 | return mei_amthif_send_cmd(cl, cb); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 277 | /** |
| 278 | * mei_amthif_write - write amthif data to amthif client |
| 279 | * |
| 280 | * @cl: host client |
| 281 | * @cb: mei call back struct |
| 282 | * |
| 283 | * Return: 0 on success, <0 on failure. |
| 284 | */ |
| 285 | int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 286 | { |
| 287 | |
| 288 | struct mei_device *dev; |
| 289 | |
| 290 | if (WARN_ON(!cl || !cl->dev)) |
| 291 | return -ENODEV; |
| 292 | |
| 293 | if (WARN_ON(!cb)) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | dev = cl->dev; |
| 297 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 298 | list_add_tail(&cb->list, &dev->amthif_cmd_list.list); |
Alexander Usyskin | 5cf8b2a | 2016-02-07 23:35:25 +0200 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * The previous request is still in processing, queue this one. |
| 302 | */ |
| 303 | if (dev->iamthif_state > MEI_IAMTHIF_IDLE && |
| 304 | dev->iamthif_state < MEI_IAMTHIF_READ_COMPLETE) |
| 305 | return 0; |
| 306 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 307 | return mei_amthif_run_next_cmd(dev); |
| 308 | } |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 309 | |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 310 | /** |
| 311 | * mei_amthif_poll - the amthif poll function |
| 312 | * |
| 313 | * @dev: the device structure |
| 314 | * @file: pointer to file structure |
| 315 | * @wait: pointer to poll_table structure |
| 316 | * |
| 317 | * Return: poll mask |
| 318 | * |
| 319 | * Locking: called under "dev->device_lock" lock |
| 320 | */ |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 321 | |
| 322 | unsigned int mei_amthif_poll(struct mei_device *dev, |
| 323 | struct file *file, poll_table *wait) |
| 324 | { |
| 325 | unsigned int mask = 0; |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 326 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 327 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 328 | |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 329 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 330 | dev->iamthif_fp == file) { |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 331 | |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 332 | mask |= POLLIN | POLLRDNORM; |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 333 | mei_amthif_run_next_cmd(dev); |
| 334 | } |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 335 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 336 | return mask; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 341 | /** |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 342 | * mei_amthif_irq_write - write iamthif command in irq thread context. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 343 | * |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 344 | * @cl: private data of the file object. |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 345 | * @cb: callback block. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 346 | * @cmpl_list: complete list. |
| 347 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 348 | * Return: 0, OK; otherwise, error. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 349 | */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 350 | int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 351 | struct mei_cl_cb *cmpl_list) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 352 | { |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 353 | int ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 354 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 355 | ret = mei_cl_irq_write(cl, cb, cmpl_list); |
| 356 | if (ret) |
| 357 | return ret; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 358 | |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 359 | if (cb->completed) |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 360 | cb->status = mei_amthif_read_start(cl, cb->fp); |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 361 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 366 | * mei_amthif_irq_read_msg - read routine after ISR to |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 367 | * handle the read amthif message |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 368 | * |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 369 | * @cl: mei client |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 370 | * @mei_hdr: header of amthif message |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 371 | * @cmpl_list: completed callbacks list |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 372 | * |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 373 | * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 374 | */ |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 375 | int mei_amthif_irq_read_msg(struct mei_cl *cl, |
Tomas Winkler | 5ceb46e | 2013-04-19 21:16:53 +0300 | [diff] [blame] | 376 | struct mei_msg_hdr *mei_hdr, |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 377 | struct mei_cl_cb *cmpl_list) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 378 | { |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 379 | struct mei_device *dev; |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 380 | int ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 381 | |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 382 | dev = cl->dev; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 383 | |
Tomas Winkler | db4756f | 2015-02-10 10:39:37 +0200 | [diff] [blame] | 384 | if (dev->iamthif_state != MEI_IAMTHIF_READING) |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 385 | return 0; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 386 | |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 387 | ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list); |
| 388 | if (ret) |
| 389 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 390 | |
| 391 | if (!mei_hdr->msg_complete) |
| 392 | return 0; |
| 393 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 394 | dev_dbg(dev->dev, "completed amthif read.\n "); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 395 | dev->iamthif_current_cb = NULL; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 396 | dev->iamthif_stall_timer = 0; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 397 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * mei_amthif_complete - complete amthif callback. |
| 403 | * |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 404 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 405 | * @cb: callback block. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 406 | */ |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 407 | void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 408 | { |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 409 | struct mei_device *dev = cl->dev; |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 410 | |
| 411 | if (cb->fop_type == MEI_FOP_WRITE) { |
| 412 | if (!cb->status) { |
| 413 | dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER; |
| 414 | mei_io_cb_free(cb); |
| 415 | return; |
| 416 | } |
| 417 | /* |
| 418 | * in case of error enqueue the write cb to complete read list |
| 419 | * so it can be propagated to the reader |
| 420 | */ |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 421 | list_add_tail(&cb->list, &cl->rd_completed); |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 422 | wake_up_interruptible(&dev->iamthif_cl.wait); |
| 423 | return; |
| 424 | } |
| 425 | |
Alexander Usyskin | edf5600 | 2015-10-13 15:02:41 +0300 | [diff] [blame] | 426 | if (!dev->iamthif_canceled) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 427 | dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE; |
| 428 | dev->iamthif_stall_timer = 0; |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 429 | list_add_tail(&cb->list, &cl->rd_completed); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 430 | dev_dbg(dev->dev, "amthif read completed\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 431 | } else { |
| 432 | mei_amthif_run_next_cmd(dev); |
| 433 | } |
| 434 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 435 | dev_dbg(dev->dev, "completing amthif call back.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 436 | wake_up_interruptible(&dev->iamthif_cl.wait); |
| 437 | } |
| 438 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 439 | /** |
| 440 | * mei_clear_list - removes all callbacks associated with file |
| 441 | * from mei_cb_list |
| 442 | * |
| 443 | * @dev: device structure. |
| 444 | * @file: file structure |
| 445 | * @mei_cb_list: callbacks list |
| 446 | * |
| 447 | * mei_clear_list is called to clear resources associated with file |
| 448 | * when application calls close function or Ctrl-C was pressed |
| 449 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 450 | * Return: true if callback removed from the list, false otherwise |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 451 | */ |
| 452 | static bool mei_clear_list(struct mei_device *dev, |
| 453 | const struct file *file, struct list_head *mei_cb_list) |
| 454 | { |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 455 | struct mei_cl *cl = &dev->iamthif_cl; |
| 456 | struct mei_cl_cb *cb, *next; |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 457 | bool removed = false; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 458 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 459 | /* list all list member */ |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 460 | list_for_each_entry_safe(cb, next, mei_cb_list, list) { |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 461 | /* check if list member associated with a file */ |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 462 | if (file == cb->fp) { |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 463 | /* check if cb equal to current iamthif cb */ |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 464 | if (dev->iamthif_current_cb == cb) { |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 465 | dev->iamthif_current_cb = NULL; |
| 466 | /* send flow control to iamthif client */ |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 467 | mei_hbm_cl_flow_control_req(dev, cl); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 468 | } |
| 469 | /* free all allocated buffers */ |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 470 | mei_io_cb_free(cb); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 471 | removed = true; |
| 472 | } |
| 473 | } |
| 474 | return removed; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * mei_clear_lists - removes all callbacks associated with file |
| 479 | * |
| 480 | * @dev: device structure |
| 481 | * @file: file structure |
| 482 | * |
| 483 | * mei_clear_lists is called to clear resources associated with file |
| 484 | * when application calls close function or Ctrl-C was pressed |
| 485 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 486 | * Return: true if callback removed from the list, false otherwise |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 487 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 488 | static bool mei_clear_lists(struct mei_device *dev, const struct file *file) |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 489 | { |
| 490 | bool removed = false; |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 491 | struct mei_cl *cl = &dev->iamthif_cl; |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 492 | |
| 493 | /* remove callbacks associated with a file */ |
| 494 | mei_clear_list(dev, file, &dev->amthif_cmd_list.list); |
Tomas Winkler | 9abd8b3 | 2016-02-07 23:35:26 +0200 | [diff] [blame^] | 495 | if (mei_clear_list(dev, file, &cl->rd_completed)) |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 496 | removed = true; |
| 497 | |
| 498 | mei_clear_list(dev, file, &dev->ctrl_rd_list.list); |
| 499 | |
| 500 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) |
| 501 | removed = true; |
| 502 | |
| 503 | if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) |
| 504 | removed = true; |
| 505 | |
| 506 | if (mei_clear_list(dev, file, &dev->write_list.list)) |
| 507 | removed = true; |
| 508 | |
| 509 | /* check if iamthif_current_cb not NULL */ |
| 510 | if (dev->iamthif_current_cb && !removed) { |
| 511 | /* check file and iamthif current cb association */ |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 512 | if (dev->iamthif_current_cb->fp == file) { |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 513 | /* remove cb */ |
| 514 | mei_io_cb_free(dev->iamthif_current_cb); |
| 515 | dev->iamthif_current_cb = NULL; |
| 516 | removed = true; |
| 517 | } |
| 518 | } |
| 519 | return removed; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * mei_amthif_release - the release function |
| 524 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 525 | * @dev: device structure |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 526 | * @file: pointer to file structure |
| 527 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 528 | * Return: 0 on success, <0 on error |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 529 | */ |
| 530 | int mei_amthif_release(struct mei_device *dev, struct file *file) |
| 531 | { |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 532 | if (dev->iamthif_open_count > 0) |
| 533 | dev->iamthif_open_count--; |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 534 | |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 535 | if (dev->iamthif_fp == file && |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 536 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 537 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 538 | dev_dbg(dev->dev, "amthif canceled iamthif state %d\n", |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 539 | dev->iamthif_state); |
| 540 | dev->iamthif_canceled = true; |
| 541 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 542 | dev_dbg(dev->dev, "run next amthif iamthif cb\n"); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 543 | mei_amthif_run_next_cmd(dev); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (mei_clear_lists(dev, file)) |
| 548 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 549 | |
| 550 | return 0; |
| 551 | } |