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