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