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