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