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> |
| 23 | #include <linux/pci.h> |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 24 | #include <linux/ioctl.h> |
| 25 | #include <linux/cdev.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/sched.h> |
| 29 | #include <linux/uuid.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/uaccess.h> |
| 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 | 90e0b5f1 | 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; |
| 52 | dev->iamthif_msg_buf_size = 0; |
| 53 | dev->iamthif_msg_buf_index = 0; |
| 54 | dev->iamthif_canceled = false; |
| 55 | dev->iamthif_ioctl = false; |
| 56 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 57 | dev->iamthif_timer = 0; |
Alexander Usyskin | 4a70457 | 2013-09-02 13:29:47 +0300 | [diff] [blame] | 58 | dev->iamthif_stall_timer = 0; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 59 | dev->iamthif_open_count = 0; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 63 | * mei_amthif_host_init - mei initialization amthif client. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 64 | * |
| 65 | * @dev: the device structure |
| 66 | * |
| 67 | */ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 68 | int mei_amthif_host_init(struct mei_device *dev) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 69 | { |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 70 | struct mei_cl *cl = &dev->iamthif_cl; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 71 | unsigned char *msg_buf; |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 72 | int ret, i; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 73 | |
Tomas Winkler | 6222f7b | 2013-01-08 23:07:23 +0200 | [diff] [blame] | 74 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 75 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 76 | mei_cl_init(cl, dev); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 77 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 78 | i = mei_me_cl_by_uuid(dev, &mei_amthif_guid); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 79 | if (i < 0) { |
Tomas Winkler | af68fb6 | 2013-09-16 23:44:48 +0300 | [diff] [blame] | 80 | dev_info(&dev->pdev->dev, |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 81 | "amthif: failed to find the client %d\n", i); |
| 82 | return -ENOTTY; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 85 | cl->me_client_id = dev->me_clients[i].client_id; |
| 86 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 87 | /* Assign iamthif_mtu to the value received from ME */ |
| 88 | |
| 89 | dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length; |
| 90 | dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n", |
| 91 | dev->me_clients[i].props.max_msg_length); |
| 92 | |
| 93 | kfree(dev->iamthif_msg_buf); |
| 94 | dev->iamthif_msg_buf = NULL; |
| 95 | |
| 96 | /* allocate storage for ME message buffer */ |
| 97 | msg_buf = kcalloc(dev->iamthif_mtu, |
| 98 | sizeof(unsigned char), GFP_KERNEL); |
| 99 | if (!msg_buf) { |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 100 | dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n"); |
| 101 | return -ENOMEM; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | dev->iamthif_msg_buf = msg_buf; |
| 105 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 106 | ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID); |
| 107 | |
| 108 | if (ret < 0) { |
Tomas Winkler | af68fb6 | 2013-09-16 23:44:48 +0300 | [diff] [blame] | 109 | dev_err(&dev->pdev->dev, |
| 110 | "amthif: failed link client %d\n", ret); |
| 111 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 112 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 113 | |
| 114 | cl->state = MEI_FILE_CONNECTING; |
| 115 | |
Tomas Winkler | 6409285 | 2014-02-17 15:13:21 +0200 | [diff] [blame] | 116 | ret = mei_cl_connect(cl, NULL); |
| 117 | |
| 118 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 119 | |
| 120 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * mei_amthif_find_read_list_entry - finds a amthilist entry for current file |
| 125 | * |
| 126 | * @dev: the device structure |
| 127 | * @file: pointer to file object |
| 128 | * |
| 129 | * returns returned a list entry on success, NULL on failure. |
| 130 | */ |
| 131 | struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev, |
| 132 | struct file *file) |
| 133 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 134 | struct mei_cl_cb *cb; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 135 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 136 | list_for_each_entry(cb, &dev->amthif_rd_complete_list.list, list) { |
| 137 | if (cb->cl && cb->cl == &dev->iamthif_cl && |
| 138 | cb->file_object == file) |
| 139 | return cb; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 140 | } |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * mei_amthif_read - read data from AMTHIF client |
| 147 | * |
| 148 | * @dev: the device structure |
| 149 | * @if_num: minor number |
| 150 | * @file: pointer to file object |
| 151 | * @*ubuf: pointer to user data in user space |
| 152 | * @length: data length to read |
| 153 | * @offset: data read offset |
| 154 | * |
| 155 | * Locking: called under "dev->device_lock" lock |
| 156 | * |
| 157 | * returns |
| 158 | * returned data length on success, |
| 159 | * zero if no data to read, |
| 160 | * negative on failure. |
| 161 | */ |
| 162 | int mei_amthif_read(struct mei_device *dev, struct file *file, |
| 163 | char __user *ubuf, size_t length, loff_t *offset) |
| 164 | { |
| 165 | int rets; |
| 166 | int wait_ret; |
| 167 | struct mei_cl_cb *cb = NULL; |
| 168 | struct mei_cl *cl = file->private_data; |
| 169 | unsigned long timeout; |
| 170 | int i; |
| 171 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 172 | /* Only possible if we are in timeout */ |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 173 | if (!cl || cl != &dev->iamthif_cl) { |
| 174 | dev_dbg(&dev->pdev->dev, "bad file ext.\n"); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 175 | return -ETIME; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 179 | if (i < 0) { |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 180 | dev_dbg(&dev->pdev->dev, "amthif client not found.\n"); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 181 | return -ENOTTY; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 182 | } |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 183 | dev_dbg(&dev->pdev->dev, "checking amthif data\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 184 | cb = mei_amthif_find_read_list_entry(dev, file); |
| 185 | |
| 186 | /* Check for if we can block or not*/ |
| 187 | if (cb == NULL && file->f_flags & O_NONBLOCK) |
| 188 | return -EAGAIN; |
| 189 | |
| 190 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 191 | dev_dbg(&dev->pdev->dev, "waiting for amthif data\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 192 | while (cb == NULL) { |
| 193 | /* unlock the Mutex */ |
| 194 | mutex_unlock(&dev->device_lock); |
| 195 | |
| 196 | wait_ret = wait_event_interruptible(dev->iamthif_cl.wait, |
| 197 | (cb = mei_amthif_find_read_list_entry(dev, file))); |
| 198 | |
Alexey Khoroshilov | e6028db | 2012-12-22 01:44:16 +0400 | [diff] [blame] | 199 | /* Locking again the Mutex */ |
| 200 | mutex_lock(&dev->device_lock); |
| 201 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 202 | if (wait_ret) |
| 203 | return -ERESTARTSYS; |
| 204 | |
| 205 | dev_dbg(&dev->pdev->dev, "woke up from sleep\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 209 | dev_dbg(&dev->pdev->dev, "Got amthif data\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 210 | dev->iamthif_timer = 0; |
| 211 | |
| 212 | if (cb) { |
| 213 | timeout = cb->read_time + |
| 214 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 215 | dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n", |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 216 | timeout); |
| 217 | |
| 218 | if (time_after(jiffies, timeout)) { |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 219 | dev_dbg(&dev->pdev->dev, "amthif Time out\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 220 | /* 15 sec for the message has expired */ |
| 221 | list_del(&cb->list); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 222 | rets = -ETIME; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 223 | goto free; |
| 224 | } |
| 225 | } |
| 226 | /* if the whole message will fit remove it from the list */ |
| 227 | if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset)) |
| 228 | list_del(&cb->list); |
| 229 | else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { |
| 230 | /* end of the message has been reached */ |
| 231 | list_del(&cb->list); |
| 232 | rets = 0; |
| 233 | goto free; |
| 234 | } |
| 235 | /* else means that not full buffer will be read and do not |
| 236 | * remove message from deletion list |
| 237 | */ |
| 238 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 239 | dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n", |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 240 | cb->response_buffer.size); |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 241 | dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 242 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 243 | /* length is being truncated to PAGE_SIZE, however, |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 244 | * the buf_idx may point beyond */ |
| 245 | length = min_t(size_t, length, (cb->buf_idx - *offset)); |
| 246 | |
Alexander Usyskin | dbac474 | 2014-03-12 13:19:13 +0200 | [diff] [blame] | 247 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
| 248 | dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 249 | rets = -EFAULT; |
Tomas Winkler | 34e267d | 2014-03-16 14:35:57 +0200 | [diff] [blame^] | 250 | } else { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 251 | rets = length; |
| 252 | if ((*offset + length) < cb->buf_idx) { |
| 253 | *offset += length; |
| 254 | goto out; |
| 255 | } |
| 256 | } |
| 257 | free: |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 258 | dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 259 | *offset = 0; |
| 260 | mei_io_cb_free(cb); |
| 261 | out: |
| 262 | return rets; |
| 263 | } |
| 264 | |
| 265 | /** |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 266 | * mei_amthif_send_cmd - send amthif command to the ME |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 267 | * |
| 268 | * @dev: the device structure |
| 269 | * @cb: mei call back struct |
| 270 | * |
| 271 | * returns 0 on success, <0 on failure. |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 272 | * |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 273 | */ |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 274 | static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 275 | { |
| 276 | struct mei_msg_hdr mei_hdr; |
| 277 | int ret; |
| 278 | |
| 279 | if (!dev || !cb) |
| 280 | return -ENODEV; |
| 281 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 282 | dev_dbg(&dev->pdev->dev, "write data to amthif client.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 283 | |
| 284 | dev->iamthif_state = MEI_IAMTHIF_WRITING; |
| 285 | dev->iamthif_current_cb = cb; |
| 286 | dev->iamthif_file_object = cb->file_object; |
| 287 | dev->iamthif_canceled = false; |
| 288 | dev->iamthif_ioctl = true; |
| 289 | dev->iamthif_msg_buf_size = cb->request_buffer.size; |
| 290 | memcpy(dev->iamthif_msg_buf, cb->request_buffer.data, |
| 291 | cb->request_buffer.size); |
| 292 | |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 293 | ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 294 | if (ret < 0) |
| 295 | return ret; |
| 296 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 297 | if (ret && mei_hbuf_acquire(dev)) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 298 | ret = 0; |
Tomas Winkler | 827eef5 | 2013-02-06 14:06:41 +0200 | [diff] [blame] | 299 | if (cb->request_buffer.size > mei_hbuf_max_len(dev)) { |
| 300 | mei_hdr.length = mei_hbuf_max_len(dev); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 301 | mei_hdr.msg_complete = 0; |
| 302 | } else { |
| 303 | mei_hdr.length = cb->request_buffer.size; |
| 304 | mei_hdr.msg_complete = 1; |
| 305 | } |
| 306 | |
| 307 | mei_hdr.host_addr = dev->iamthif_cl.host_client_id; |
| 308 | mei_hdr.me_addr = dev->iamthif_cl.me_client_id; |
| 309 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 310 | mei_hdr.internal = 0; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 311 | dev->iamthif_msg_buf_index += mei_hdr.length; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 312 | ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf); |
| 313 | if (ret) |
| 314 | return ret; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 315 | |
| 316 | if (mei_hdr.msg_complete) { |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 317 | if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl)) |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 318 | return -EIO; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 319 | dev->iamthif_flow_control_pending = true; |
| 320 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 321 | dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 322 | dev->iamthif_current_cb = cb; |
| 323 | dev->iamthif_file_object = cb->file_object; |
| 324 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
| 325 | } else { |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 326 | dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 327 | list_add_tail(&cb->list, &dev->write_list.list); |
| 328 | } |
| 329 | } else { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 330 | list_add_tail(&cb->list, &dev->write_list.list); |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 336 | * mei_amthif_write - write amthif data to amthif client |
| 337 | * |
| 338 | * @dev: the device structure |
| 339 | * @cb: mei call back struct |
| 340 | * |
| 341 | * returns 0 on success, <0 on failure. |
| 342 | * |
| 343 | */ |
| 344 | int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb) |
| 345 | { |
| 346 | int ret; |
| 347 | |
| 348 | if (!dev || !cb) |
| 349 | return -ENODEV; |
| 350 | |
| 351 | ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu); |
| 352 | if (ret) |
| 353 | return ret; |
| 354 | |
Tomas Winkler | 02a7eec | 2014-02-12 21:41:51 +0200 | [diff] [blame] | 355 | cb->fop_type = MEI_FOP_WRITE; |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 356 | |
Tomas Winkler | e773efc | 2012-11-11 17:37:58 +0200 | [diff] [blame] | 357 | if (!list_empty(&dev->amthif_cmd_list.list) || |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 358 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 359 | dev_dbg(&dev->pdev->dev, |
| 360 | "amthif state = %d\n", dev->iamthif_state); |
| 361 | dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n"); |
Tomas Winkler | e773efc | 2012-11-11 17:37:58 +0200 | [diff] [blame] | 362 | list_add_tail(&cb->list, &dev->amthif_cmd_list.list); |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | return mei_amthif_send_cmd(dev, cb); |
| 366 | } |
| 367 | /** |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 368 | * mei_amthif_run_next_cmd |
| 369 | * |
| 370 | * @dev: the device structure |
| 371 | * |
| 372 | * returns 0 on success, <0 on failure. |
| 373 | */ |
| 374 | void mei_amthif_run_next_cmd(struct mei_device *dev) |
| 375 | { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 376 | struct mei_cl_cb *pos = NULL; |
| 377 | struct mei_cl_cb *next = NULL; |
| 378 | int status; |
| 379 | |
| 380 | if (!dev) |
| 381 | return; |
| 382 | |
| 383 | dev->iamthif_msg_buf_size = 0; |
| 384 | dev->iamthif_msg_buf_index = 0; |
| 385 | dev->iamthif_canceled = false; |
| 386 | dev->iamthif_ioctl = true; |
| 387 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 388 | dev->iamthif_timer = 0; |
| 389 | dev->iamthif_file_object = NULL; |
| 390 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 391 | dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 392 | |
Tomas Winkler | e773efc | 2012-11-11 17:37:58 +0200 | [diff] [blame] | 393 | list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 394 | list_del(&pos->list); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 395 | |
Tomas Winkler | db3ed43 | 2012-11-11 17:37:59 +0200 | [diff] [blame] | 396 | if (pos->cl && pos->cl == &dev->iamthif_cl) { |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 397 | status = mei_amthif_send_cmd(dev, pos); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 398 | if (status) { |
| 399 | dev_dbg(&dev->pdev->dev, |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 400 | "amthif write failed status = %d\n", |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 401 | status); |
| 402 | return; |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 409 | |
| 410 | unsigned int mei_amthif_poll(struct mei_device *dev, |
| 411 | struct file *file, poll_table *wait) |
| 412 | { |
| 413 | unsigned int mask = 0; |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 414 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 415 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 416 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 417 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 418 | if (!mei_cl_is_connected(&dev->iamthif_cl)) { |
| 419 | |
| 420 | mask = POLLERR; |
| 421 | |
| 422 | } else if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
| 423 | dev->iamthif_file_object == file) { |
| 424 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 425 | mask |= (POLLIN | POLLRDNORM); |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 426 | dev_dbg(&dev->pdev->dev, "run next amthif cb\n"); |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 427 | mei_amthif_run_next_cmd(dev); |
| 428 | } |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 429 | mutex_unlock(&dev->device_lock); |
| 430 | |
Tomas Winkler | 744f0f2 | 2012-11-11 17:38:02 +0200 | [diff] [blame] | 431 | return mask; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 436 | /** |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 437 | * mei_amthif_irq_write - write iamthif command in irq thread context. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 438 | * |
| 439 | * @dev: the device structure. |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 440 | * @cb_pos: callback block. |
| 441 | * @cl: private data of the file object. |
| 442 | * @cmpl_list: complete list. |
| 443 | * |
| 444 | * returns 0, OK; otherwise, error. |
| 445 | */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 446 | int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 447 | struct mei_cl_cb *cmpl_list) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 448 | { |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 449 | struct mei_device *dev = cl->dev; |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 450 | struct mei_msg_hdr mei_hdr; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 451 | size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index; |
Tomas Winkler | c8c8d08 | 2013-03-11 18:27:02 +0200 | [diff] [blame] | 452 | u32 msg_slots = mei_data2slots(len); |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 453 | int slots; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 454 | int rets; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 455 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 456 | rets = mei_cl_flow_ctrl_creds(cl); |
| 457 | if (rets < 0) |
| 458 | return rets; |
| 459 | |
| 460 | if (rets == 0) { |
| 461 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 465 | mei_hdr.host_addr = cl->host_client_id; |
| 466 | mei_hdr.me_addr = cl->me_client_id; |
| 467 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 468 | mei_hdr.internal = 0; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 469 | |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 470 | slots = mei_hbuf_empty_slots(dev); |
| 471 | |
| 472 | if (slots >= msg_slots) { |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 473 | mei_hdr.length = len; |
| 474 | mei_hdr.msg_complete = 1; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 475 | /* Split the message only if we can write the whole host buffer */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 476 | } else if (slots == dev->hbuf_depth) { |
| 477 | msg_slots = slots; |
| 478 | len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 479 | mei_hdr.length = len; |
| 480 | mei_hdr.msg_complete = 0; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 481 | } else { |
| 482 | /* wait for next time the host buffer is empty */ |
| 483 | return 0; |
| 484 | } |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 485 | |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 486 | dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr)); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 487 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 488 | rets = mei_write_message(dev, &mei_hdr, |
| 489 | dev->iamthif_msg_buf + dev->iamthif_msg_buf_index); |
| 490 | if (rets) { |
| 491 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 492 | cl->status = rets; |
| 493 | list_del(&cb->list); |
| 494 | return rets; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 497 | if (mei_cl_flow_ctrl_reduce(cl)) |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 498 | return -EIO; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 499 | |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 500 | dev->iamthif_msg_buf_index += mei_hdr.length; |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 501 | cl->status = 0; |
| 502 | |
Tomas Winkler | e46f187 | 2012-12-25 19:06:10 +0200 | [diff] [blame] | 503 | if (mei_hdr.msg_complete) { |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 504 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; |
| 505 | dev->iamthif_flow_control_pending = true; |
| 506 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 507 | /* save iamthif cb sent to amthif client */ |
Tomas Winkler | 24c656e | 2012-11-18 15:13:17 +0200 | [diff] [blame] | 508 | cb->buf_idx = dev->iamthif_msg_buf_index; |
| 509 | dev->iamthif_current_cb = cb; |
| 510 | |
| 511 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
| 512 | } |
| 513 | |
| 514 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * mei_amthif_irq_read_message - read routine after ISR to |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 520 | * handle the read amthif message |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 521 | * |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 522 | * @dev: the device structure |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 523 | * @mei_hdr: header of amthif message |
Tomas Winkler | 5ceb46e | 2013-04-19 21:16:53 +0300 | [diff] [blame] | 524 | * @complete_list: An instance of our list structure |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 525 | * |
| 526 | * returns 0 on success, <0 on failure. |
| 527 | */ |
Tomas Winkler | 5ceb46e | 2013-04-19 21:16:53 +0300 | [diff] [blame] | 528 | int mei_amthif_irq_read_msg(struct mei_device *dev, |
| 529 | struct mei_msg_hdr *mei_hdr, |
| 530 | struct mei_cl_cb *complete_list) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 531 | { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 532 | struct mei_cl_cb *cb; |
| 533 | unsigned char *buffer; |
| 534 | |
| 535 | BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id); |
| 536 | BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING); |
| 537 | |
| 538 | buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index; |
| 539 | BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length); |
| 540 | |
| 541 | mei_read_slots(dev, buffer, mei_hdr->length); |
| 542 | |
| 543 | dev->iamthif_msg_buf_index += mei_hdr->length; |
| 544 | |
| 545 | if (!mei_hdr->msg_complete) |
| 546 | return 0; |
| 547 | |
Tomas Winkler | 5ceb46e | 2013-04-19 21:16:53 +0300 | [diff] [blame] | 548 | dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n", |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 549 | mei_hdr->length); |
| 550 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 551 | dev_dbg(&dev->pdev->dev, "completed amthif read.\n "); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 552 | if (!dev->iamthif_current_cb) |
| 553 | return -ENODEV; |
| 554 | |
| 555 | cb = dev->iamthif_current_cb; |
| 556 | dev->iamthif_current_cb = NULL; |
| 557 | |
Tomas Winkler | db3ed43 | 2012-11-11 17:37:59 +0200 | [diff] [blame] | 558 | if (!cb->cl) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 559 | return -ENODEV; |
| 560 | |
| 561 | dev->iamthif_stall_timer = 0; |
| 562 | cb->buf_idx = dev->iamthif_msg_buf_index; |
| 563 | cb->read_time = jiffies; |
Tomas Winkler | db3ed43 | 2012-11-11 17:37:59 +0200 | [diff] [blame] | 564 | if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 565 | /* found the iamthif cb */ |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 566 | dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n "); |
| 567 | dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n "); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 568 | list_add_tail(&cb->list, &complete_list->list); |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * mei_amthif_irq_read - prepares to read amthif data. |
| 575 | * |
| 576 | * @dev: the device structure. |
| 577 | * @slots: free slots. |
| 578 | * |
| 579 | * returns 0, OK; otherwise, error. |
| 580 | */ |
| 581 | int mei_amthif_irq_read(struct mei_device *dev, s32 *slots) |
| 582 | { |
Tomas Winkler | c8c8d08 | 2013-03-11 18:27:02 +0200 | [diff] [blame] | 583 | u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control)); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 584 | |
Tomas Winkler | c8c8d08 | 2013-03-11 18:27:02 +0200 | [diff] [blame] | 585 | if (*slots < msg_slots) |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 586 | return -EMSGSIZE; |
Tomas Winkler | c8c8d08 | 2013-03-11 18:27:02 +0200 | [diff] [blame] | 587 | |
| 588 | *slots -= msg_slots; |
| 589 | |
Tomas Winkler | 8120e72 | 2012-12-25 19:06:11 +0200 | [diff] [blame] | 590 | if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 591 | dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n"); |
| 592 | return -EIO; |
| 593 | } |
| 594 | |
| 595 | dev_dbg(&dev->pdev->dev, "iamthif flow control success\n"); |
| 596 | dev->iamthif_state = MEI_IAMTHIF_READING; |
| 597 | dev->iamthif_flow_control_pending = false; |
| 598 | dev->iamthif_msg_buf_index = 0; |
| 599 | dev->iamthif_msg_buf_size = 0; |
| 600 | dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER; |
Tomas Winkler | 330dd7d | 2013-02-06 14:06:43 +0200 | [diff] [blame] | 601 | dev->hbuf_is_ready = mei_hbuf_is_ready(dev); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * mei_amthif_complete - complete amthif callback. |
| 607 | * |
| 608 | * @dev: the device structure. |
| 609 | * @cb_pos: callback block. |
| 610 | */ |
| 611 | void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb) |
| 612 | { |
| 613 | if (dev->iamthif_canceled != 1) { |
| 614 | dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE; |
| 615 | dev->iamthif_stall_timer = 0; |
| 616 | memcpy(cb->response_buffer.data, |
| 617 | dev->iamthif_msg_buf, |
| 618 | dev->iamthif_msg_buf_index); |
Tomas Winkler | e773efc | 2012-11-11 17:37:58 +0200 | [diff] [blame] | 619 | list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list); |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 620 | dev_dbg(&dev->pdev->dev, "amthif read completed\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 621 | dev->iamthif_timer = jiffies; |
| 622 | dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n", |
| 623 | dev->iamthif_timer); |
| 624 | } else { |
| 625 | mei_amthif_run_next_cmd(dev); |
| 626 | } |
| 627 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 628 | dev_dbg(&dev->pdev->dev, "completing amthif call back.\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 629 | wake_up_interruptible(&dev->iamthif_cl.wait); |
| 630 | } |
| 631 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 632 | /** |
| 633 | * mei_clear_list - removes all callbacks associated with file |
| 634 | * from mei_cb_list |
| 635 | * |
| 636 | * @dev: device structure. |
| 637 | * @file: file structure |
| 638 | * @mei_cb_list: callbacks list |
| 639 | * |
| 640 | * mei_clear_list is called to clear resources associated with file |
| 641 | * when application calls close function or Ctrl-C was pressed |
| 642 | * |
| 643 | * returns true if callback removed from the list, false otherwise |
| 644 | */ |
| 645 | static bool mei_clear_list(struct mei_device *dev, |
| 646 | const struct file *file, struct list_head *mei_cb_list) |
| 647 | { |
| 648 | struct mei_cl_cb *cb_pos = NULL; |
| 649 | struct mei_cl_cb *cb_next = NULL; |
| 650 | bool removed = false; |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 651 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 652 | /* list all list member */ |
| 653 | list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) { |
| 654 | /* check if list member associated with a file */ |
| 655 | if (file == cb_pos->file_object) { |
| 656 | /* remove member from the list */ |
| 657 | list_del(&cb_pos->list); |
| 658 | /* check if cb equal to current iamthif cb */ |
| 659 | if (dev->iamthif_current_cb == cb_pos) { |
| 660 | dev->iamthif_current_cb = NULL; |
| 661 | /* send flow control to iamthif client */ |
Tomas Winkler | 8120e72 | 2012-12-25 19:06:11 +0200 | [diff] [blame] | 662 | mei_hbm_cl_flow_control_req(dev, |
| 663 | &dev->iamthif_cl); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 664 | } |
| 665 | /* free all allocated buffers */ |
| 666 | mei_io_cb_free(cb_pos); |
| 667 | cb_pos = NULL; |
| 668 | removed = true; |
| 669 | } |
| 670 | } |
| 671 | return removed; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * mei_clear_lists - removes all callbacks associated with file |
| 676 | * |
| 677 | * @dev: device structure |
| 678 | * @file: file structure |
| 679 | * |
| 680 | * mei_clear_lists is called to clear resources associated with file |
| 681 | * when application calls close function or Ctrl-C was pressed |
| 682 | * |
| 683 | * returns true if callback removed from the list, false otherwise |
| 684 | */ |
| 685 | static bool mei_clear_lists(struct mei_device *dev, struct file *file) |
| 686 | { |
| 687 | bool removed = false; |
| 688 | |
| 689 | /* remove callbacks associated with a file */ |
| 690 | mei_clear_list(dev, file, &dev->amthif_cmd_list.list); |
| 691 | if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list)) |
| 692 | removed = true; |
| 693 | |
| 694 | mei_clear_list(dev, file, &dev->ctrl_rd_list.list); |
| 695 | |
| 696 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) |
| 697 | removed = true; |
| 698 | |
| 699 | if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) |
| 700 | removed = true; |
| 701 | |
| 702 | if (mei_clear_list(dev, file, &dev->write_list.list)) |
| 703 | removed = true; |
| 704 | |
| 705 | /* check if iamthif_current_cb not NULL */ |
| 706 | if (dev->iamthif_current_cb && !removed) { |
| 707 | /* check file and iamthif current cb association */ |
| 708 | if (dev->iamthif_current_cb->file_object == file) { |
| 709 | /* remove cb */ |
| 710 | mei_io_cb_free(dev->iamthif_current_cb); |
| 711 | dev->iamthif_current_cb = NULL; |
| 712 | removed = true; |
| 713 | } |
| 714 | } |
| 715 | return removed; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * mei_amthif_release - the release function |
| 720 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 721 | * @dev: device structure |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 722 | * @file: pointer to file structure |
| 723 | * |
| 724 | * returns 0 on success, <0 on error |
| 725 | */ |
| 726 | int mei_amthif_release(struct mei_device *dev, struct file *file) |
| 727 | { |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 728 | if (dev->iamthif_open_count > 0) |
| 729 | dev->iamthif_open_count--; |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 730 | |
| 731 | if (dev->iamthif_file_object == file && |
| 732 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 733 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 734 | dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n", |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 735 | dev->iamthif_state); |
| 736 | dev->iamthif_canceled = true; |
| 737 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 738 | dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n"); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 739 | mei_amthif_run_next_cmd(dev); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (mei_clear_lists(dev, file)) |
| 744 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 745 | |
| 746 | return 0; |
| 747 | } |