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