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