Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91 | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 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 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/fcntl.h> |
| 23 | #include <linux/aio.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/ioctl.h> |
| 27 | #include <linux/cdev.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/uuid.h> |
| 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | |
| 35 | |
| 36 | #include "mei_dev.h" |
| 37 | #include "hw.h" |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 38 | #include <linux/mei.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 39 | #include "interface.h" |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 40 | |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 41 | /** |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 42 | * mei_io_cb_free - free mei_cb_private related memory |
| 43 | * |
| 44 | * @cb: mei callback struct |
| 45 | */ |
| 46 | void mei_io_cb_free(struct mei_cl_cb *cb) |
| 47 | { |
| 48 | if (cb == NULL) |
| 49 | return; |
| 50 | |
| 51 | kfree(cb->request_buffer.data); |
| 52 | kfree(cb->response_buffer.data); |
| 53 | kfree(cb); |
| 54 | } |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 55 | /** |
| 56 | * mei_io_cb_init - allocate and initialize io callback |
| 57 | * |
| 58 | * @cl - mei client |
| 59 | * @file: pointer to file structure |
| 60 | * |
| 61 | * returns mei_cl_cb pointer or NULL; |
| 62 | */ |
| 63 | struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp) |
| 64 | { |
| 65 | struct mei_cl_cb *cb; |
| 66 | |
| 67 | cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); |
| 68 | if (!cb) |
| 69 | return NULL; |
| 70 | |
| 71 | mei_io_list_init(cb); |
| 72 | |
| 73 | cb->file_object = fp; |
| 74 | cb->file_private = cl; |
| 75 | cb->buf_idx = 0; |
| 76 | return cb; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * mei_io_cb_alloc_req_buf - allocate request buffer |
| 82 | * |
| 83 | * @cb - io callback structure |
| 84 | * @size: size of the buffer |
| 85 | * |
| 86 | * returns 0 on success |
| 87 | * -EINVAL if cb is NULL |
| 88 | * -ENOMEM if allocation failed |
| 89 | */ |
| 90 | int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length) |
| 91 | { |
| 92 | if (!cb) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | if (length == 0) |
| 96 | return 0; |
| 97 | |
| 98 | cb->request_buffer.data = kmalloc(length, GFP_KERNEL); |
| 99 | if (!cb->request_buffer.data) |
| 100 | return -ENOMEM; |
| 101 | cb->request_buffer.size = length; |
| 102 | return 0; |
| 103 | } |
| 104 | /** |
| 105 | * mei_io_cb_alloc_req_buf - allocate respose buffer |
| 106 | * |
| 107 | * @cb - io callback structure |
| 108 | * @size: size of the buffer |
| 109 | * |
| 110 | * returns 0 on success |
| 111 | * -EINVAL if cb is NULL |
| 112 | * -ENOMEM if allocation failed |
| 113 | */ |
| 114 | int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length) |
| 115 | { |
| 116 | if (!cb) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | if (length == 0) |
| 120 | return 0; |
| 121 | |
| 122 | cb->response_buffer.data = kmalloc(length, GFP_KERNEL); |
| 123 | if (!cb->response_buffer.data) |
| 124 | return -ENOMEM; |
| 125 | cb->response_buffer.size = length; |
| 126 | return 0; |
| 127 | } |
| 128 | |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 129 | |
| 130 | /** |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 131 | * mei_me_cl_by_id return index to me_clients for client_id |
| 132 | * |
| 133 | * @dev: the device structure |
| 134 | * @client_id: me client id |
| 135 | * |
| 136 | * Locking: called under "dev->device_lock" lock |
| 137 | * |
| 138 | * returns index on success, -ENOENT on failure. |
| 139 | */ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 140 | |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 141 | int mei_me_cl_by_id(struct mei_device *dev, u8 client_id) |
| 142 | { |
| 143 | int i; |
| 144 | for (i = 0; i < dev->me_clients_num; i++) |
| 145 | if (dev->me_clients[i].client_id == client_id) |
| 146 | break; |
| 147 | if (WARN_ON(dev->me_clients[i].client_id != client_id)) |
| 148 | return -ENOENT; |
| 149 | |
| 150 | if (i == dev->me_clients_num) |
| 151 | return -ENOENT; |
| 152 | |
| 153 | return i; |
| 154 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * mei_ioctl_connect_client - the connect to fw client IOCTL function |
| 158 | * |
| 159 | * @dev: the device structure |
| 160 | * @data: IOCTL connect data, input and output parameters |
| 161 | * @file: private data of the file object |
| 162 | * |
| 163 | * Locking: called under "dev->device_lock" lock |
| 164 | * |
| 165 | * returns 0 on success, <0 on failure. |
| 166 | */ |
| 167 | int mei_ioctl_connect_client(struct file *file, |
| 168 | struct mei_connect_client_data *data) |
| 169 | { |
| 170 | struct mei_device *dev; |
| 171 | struct mei_cl_cb *cb; |
| 172 | struct mei_client *client; |
| 173 | struct mei_cl *cl; |
| 174 | struct mei_cl *cl_pos = NULL; |
| 175 | struct mei_cl *cl_next = NULL; |
| 176 | long timeout = CONNECT_TIMEOUT; |
| 177 | int i; |
| 178 | int err; |
| 179 | int rets; |
| 180 | |
| 181 | cl = file->private_data; |
| 182 | if (WARN_ON(!cl || !cl->dev)) |
| 183 | return -ENODEV; |
| 184 | |
| 185 | dev = cl->dev; |
| 186 | |
| 187 | dev_dbg(&dev->pdev->dev, "mei_ioctl_connect_client() Entry\n"); |
| 188 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 189 | /* buffered ioctl cb */ |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 190 | cb = mei_io_cb_init(cl, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 191 | if (!cb) { |
| 192 | rets = -ENOMEM; |
| 193 | goto end; |
| 194 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 195 | |
| 196 | cb->major_file_operations = MEI_IOCTL; |
| 197 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 198 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 199 | rets = -ENODEV; |
| 200 | goto end; |
| 201 | } |
| 202 | if (cl->state != MEI_FILE_INITIALIZING && |
| 203 | cl->state != MEI_FILE_DISCONNECTED) { |
| 204 | rets = -EBUSY; |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | /* find ME client we're trying to connect to */ |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 209 | i = mei_me_cl_by_uuid(dev, &data->in_client_uuid); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 210 | if (i >= 0 && !dev->me_clients[i].props.fixed_address) { |
| 211 | cl->me_client_id = dev->me_clients[i].client_id; |
| 212 | cl->state = MEI_FILE_CONNECTING; |
| 213 | } |
| 214 | |
| 215 | dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n", |
| 216 | cl->me_client_id); |
| 217 | dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n", |
| 218 | dev->me_clients[i].props.protocol_version); |
| 219 | dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n", |
| 220 | dev->me_clients[i].props.max_msg_length); |
| 221 | |
Justin P. Mattock | 5f9092f | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 222 | /* if we're connecting to amthi client then we will use the |
| 223 | * existing connection |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 224 | */ |
| 225 | if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) { |
| 226 | dev_dbg(&dev->pdev->dev, "FW Client is amthi\n"); |
| 227 | if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) { |
| 228 | rets = -ENODEV; |
| 229 | goto end; |
| 230 | } |
| 231 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 232 | list_for_each_entry_safe(cl_pos, cl_next, |
| 233 | &dev->file_list, link) { |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 234 | if (mei_cl_cmp_id(cl, cl_pos)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 235 | dev_dbg(&dev->pdev->dev, |
| 236 | "remove file private data node host" |
| 237 | " client = %d, ME client = %d.\n", |
| 238 | cl_pos->host_client_id, |
| 239 | cl_pos->me_client_id); |
| 240 | list_del(&cl_pos->link); |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | dev_dbg(&dev->pdev->dev, "free file private data memory.\n"); |
| 245 | kfree(cl); |
| 246 | |
| 247 | cl = NULL; |
| 248 | file->private_data = &dev->iamthif_cl; |
| 249 | |
| 250 | client = &data->out_client_properties; |
| 251 | client->max_msg_length = |
| 252 | dev->me_clients[i].props.max_msg_length; |
| 253 | client->protocol_version = |
| 254 | dev->me_clients[i].props.protocol_version; |
| 255 | rets = dev->iamthif_cl.status; |
| 256 | |
| 257 | goto end; |
| 258 | } |
| 259 | |
| 260 | if (cl->state != MEI_FILE_CONNECTING) { |
| 261 | rets = -ENODEV; |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* prepare the output buffer */ |
| 267 | client = &data->out_client_properties; |
| 268 | client->max_msg_length = dev->me_clients[i].props.max_msg_length; |
| 269 | client->protocol_version = dev->me_clients[i].props.protocol_version; |
| 270 | dev_dbg(&dev->pdev->dev, "Can connect?\n"); |
| 271 | if (dev->mei_host_buffer_is_empty |
| 272 | && !mei_other_client_is_connecting(dev, cl)) { |
| 273 | dev_dbg(&dev->pdev->dev, "Sending Connect Message\n"); |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 274 | dev->mei_host_buffer_is_empty = false; |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 275 | if (mei_connect(dev, cl)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 276 | dev_dbg(&dev->pdev->dev, "Sending connect message - failed\n"); |
| 277 | rets = -ENODEV; |
| 278 | goto end; |
| 279 | } else { |
| 280 | dev_dbg(&dev->pdev->dev, "Sending connect message - succeeded\n"); |
| 281 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 282 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | } else { |
| 287 | dev_dbg(&dev->pdev->dev, "Queuing the connect request due to device busy\n"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 288 | dev_dbg(&dev->pdev->dev, "add connect cb to control write list.\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 289 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 290 | } |
| 291 | mutex_unlock(&dev->device_lock); |
| 292 | err = wait_event_timeout(dev->wait_recvd_msg, |
| 293 | (MEI_FILE_CONNECTED == cl->state || |
| 294 | MEI_FILE_DISCONNECTED == cl->state), |
| 295 | timeout * HZ); |
| 296 | |
| 297 | mutex_lock(&dev->device_lock); |
| 298 | if (MEI_FILE_CONNECTED == cl->state) { |
| 299 | dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n"); |
| 300 | rets = cl->status; |
| 301 | goto end; |
| 302 | } else { |
| 303 | dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n", |
| 304 | cl->state); |
| 305 | if (!err) { |
| 306 | dev_dbg(&dev->pdev->dev, |
| 307 | "wait_event_interruptible_timeout failed on client" |
| 308 | " connect message fw response message.\n"); |
| 309 | } |
| 310 | rets = -EFAULT; |
| 311 | |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 312 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 313 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 314 | goto end; |
| 315 | } |
| 316 | rets = 0; |
| 317 | end: |
| 318 | dev_dbg(&dev->pdev->dev, "free connect cb memory."); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 319 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 320 | return rets; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * find_amthi_read_list_entry - finds a amthilist entry for current file |
| 325 | * |
| 326 | * @dev: the device structure |
| 327 | * @file: pointer to file object |
| 328 | * |
| 329 | * returns returned a list entry on success, NULL on failure. |
| 330 | */ |
| 331 | struct mei_cl_cb *find_amthi_read_list_entry( |
| 332 | struct mei_device *dev, |
| 333 | struct file *file) |
| 334 | { |
| 335 | struct mei_cl *cl_temp; |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 336 | struct mei_cl_cb *pos = NULL; |
| 337 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 338 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 339 | list_for_each_entry_safe(pos, next, |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 340 | &dev->amthi_read_complete_list.list, list) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 341 | cl_temp = (struct mei_cl *)pos->file_private; |
| 342 | if (cl_temp && cl_temp == &dev->iamthif_cl && |
| 343 | pos->file_object == file) |
| 344 | return pos; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 345 | } |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * amthi_read - read data from AMTHI client |
| 351 | * |
| 352 | * @dev: the device structure |
| 353 | * @if_num: minor number |
| 354 | * @file: pointer to file object |
| 355 | * @*ubuf: pointer to user data in user space |
| 356 | * @length: data length to read |
| 357 | * @offset: data read offset |
| 358 | * |
| 359 | * Locking: called under "dev->device_lock" lock |
| 360 | * |
| 361 | * returns |
| 362 | * returned data length on success, |
| 363 | * zero if no data to read, |
| 364 | * negative on failure. |
| 365 | */ |
| 366 | int amthi_read(struct mei_device *dev, struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 367 | char __user *ubuf, size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 368 | { |
| 369 | int rets; |
| 370 | int wait_ret; |
| 371 | struct mei_cl_cb *cb = NULL; |
| 372 | struct mei_cl *cl = file->private_data; |
| 373 | unsigned long timeout; |
| 374 | int i; |
| 375 | |
| 376 | /* Only Posible if we are in timeout */ |
| 377 | if (!cl || cl != &dev->iamthif_cl) { |
| 378 | dev_dbg(&dev->pdev->dev, "bad file ext.\n"); |
| 379 | return -ETIMEDOUT; |
| 380 | } |
| 381 | |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 382 | i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 383 | |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 384 | if (i < 0) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 385 | dev_dbg(&dev->pdev->dev, "amthi client not found.\n"); |
| 386 | return -ENODEV; |
| 387 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 388 | dev_dbg(&dev->pdev->dev, "checking amthi data\n"); |
| 389 | cb = find_amthi_read_list_entry(dev, file); |
| 390 | |
| 391 | /* Check for if we can block or not*/ |
| 392 | if (cb == NULL && file->f_flags & O_NONBLOCK) |
| 393 | return -EAGAIN; |
| 394 | |
| 395 | |
| 396 | dev_dbg(&dev->pdev->dev, "waiting for amthi data\n"); |
| 397 | while (cb == NULL) { |
| 398 | /* unlock the Mutex */ |
| 399 | mutex_unlock(&dev->device_lock); |
| 400 | |
| 401 | wait_ret = wait_event_interruptible(dev->iamthif_cl.wait, |
| 402 | (cb = find_amthi_read_list_entry(dev, file))); |
| 403 | |
| 404 | if (wait_ret) |
| 405 | return -ERESTARTSYS; |
| 406 | |
| 407 | dev_dbg(&dev->pdev->dev, "woke up from sleep\n"); |
| 408 | |
| 409 | /* Locking again the Mutex */ |
| 410 | mutex_lock(&dev->device_lock); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | dev_dbg(&dev->pdev->dev, "Got amthi data\n"); |
| 415 | dev->iamthif_timer = 0; |
| 416 | |
| 417 | if (cb) { |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 418 | timeout = cb->read_time + msecs_to_jiffies(IAMTHIF_READ_TIMER); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 419 | dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n", |
| 420 | timeout); |
| 421 | |
| 422 | if (time_after(jiffies, timeout)) { |
| 423 | dev_dbg(&dev->pdev->dev, "amthi Time out\n"); |
| 424 | /* 15 sec for the message has expired */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 425 | list_del(&cb->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 426 | rets = -ETIMEDOUT; |
| 427 | goto free; |
| 428 | } |
| 429 | } |
| 430 | /* if the whole message will fit remove it from the list */ |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 431 | if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset)) |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 432 | list_del(&cb->list); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 433 | else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 434 | /* end of the message has been reached */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 435 | list_del(&cb->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 436 | rets = 0; |
| 437 | goto free; |
| 438 | } |
| 439 | /* else means that not full buffer will be read and do not |
| 440 | * remove message from deletion list |
| 441 | */ |
| 442 | |
| 443 | dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n", |
| 444 | cb->response_buffer.size); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 445 | dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 446 | |
| 447 | /* length is being turncated to PAGE_SIZE, however, |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 448 | * the buf_idx may point beyond */ |
| 449 | length = min_t(size_t, length, (cb->buf_idx - *offset)); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 450 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 451 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 452 | rets = -EFAULT; |
| 453 | else { |
| 454 | rets = length; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 455 | if ((*offset + length) < cb->buf_idx) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 456 | *offset += length; |
| 457 | goto out; |
| 458 | } |
| 459 | } |
| 460 | free: |
| 461 | dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n"); |
| 462 | *offset = 0; |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 463 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 464 | out: |
| 465 | return rets; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * mei_start_read - the start read client message function. |
| 470 | * |
| 471 | * @dev: the device structure |
| 472 | * @if_num: minor number |
| 473 | * @cl: private data of the file object |
| 474 | * |
| 475 | * returns 0 on success, <0 on failure. |
| 476 | */ |
| 477 | int mei_start_read(struct mei_device *dev, struct mei_cl *cl) |
| 478 | { |
| 479 | struct mei_cl_cb *cb; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 480 | int rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 481 | int i; |
| 482 | |
| 483 | if (cl->state != MEI_FILE_CONNECTED) |
| 484 | return -ENODEV; |
| 485 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 486 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 487 | return -ENODEV; |
| 488 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 489 | if (cl->read_pending || cl->read_cb) { |
| 490 | dev_dbg(&dev->pdev->dev, "read is pending.\n"); |
| 491 | return -EBUSY; |
| 492 | } |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 493 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 494 | if (i < 0) { |
| 495 | dev_err(&dev->pdev->dev, "no such me client %d\n", |
| 496 | cl->me_client_id); |
| 497 | return -ENODEV; |
| 498 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 499 | |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 500 | cb = mei_io_cb_init(cl, NULL); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 501 | if (!cb) |
| 502 | return -ENOMEM; |
| 503 | |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 504 | rets = mei_io_cb_alloc_resp_buf(cb, |
| 505 | dev->me_clients[i].props.max_msg_length); |
| 506 | if (rets) |
| 507 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 508 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 509 | cb->major_file_operations = MEI_READ; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 510 | cl->read_cb = cb; |
| 511 | if (dev->mei_host_buffer_is_empty) { |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 512 | dev->mei_host_buffer_is_empty = false; |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 513 | if (mei_send_flow_control(dev, cl)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 514 | rets = -ENODEV; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 515 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 516 | } |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 517 | list_add_tail(&cb->list, &dev->read_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 518 | } else { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 519 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 520 | } |
| 521 | return rets; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame^] | 522 | err: |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 523 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 524 | return rets; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * amthi_write - write iamthif data to amthi client |
| 529 | * |
| 530 | * @dev: the device structure |
| 531 | * @cb: mei call back struct |
| 532 | * |
| 533 | * returns 0 on success, <0 on failure. |
| 534 | */ |
| 535 | int amthi_write(struct mei_device *dev, struct mei_cl_cb *cb) |
| 536 | { |
| 537 | struct mei_msg_hdr mei_hdr; |
| 538 | int ret; |
| 539 | |
| 540 | if (!dev || !cb) |
| 541 | return -ENODEV; |
| 542 | |
| 543 | dev_dbg(&dev->pdev->dev, "write data to amthi client.\n"); |
| 544 | |
| 545 | dev->iamthif_state = MEI_IAMTHIF_WRITING; |
| 546 | dev->iamthif_current_cb = cb; |
| 547 | dev->iamthif_file_object = cb->file_object; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 548 | dev->iamthif_canceled = false; |
| 549 | dev->iamthif_ioctl = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 550 | dev->iamthif_msg_buf_size = cb->request_buffer.size; |
| 551 | memcpy(dev->iamthif_msg_buf, cb->request_buffer.data, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 552 | cb->request_buffer.size); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 553 | |
| 554 | ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl); |
| 555 | if (ret < 0) |
| 556 | return ret; |
| 557 | |
| 558 | if (ret && dev->mei_host_buffer_is_empty) { |
| 559 | ret = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 560 | dev->mei_host_buffer_is_empty = false; |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 561 | if (cb->request_buffer.size > mei_hbuf_max_data(dev)) { |
| 562 | mei_hdr.length = mei_hbuf_max_data(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 563 | mei_hdr.msg_complete = 0; |
| 564 | } else { |
| 565 | mei_hdr.length = cb->request_buffer.size; |
| 566 | mei_hdr.msg_complete = 1; |
| 567 | } |
| 568 | |
| 569 | mei_hdr.host_addr = dev->iamthif_cl.host_client_id; |
| 570 | mei_hdr.me_addr = dev->iamthif_cl.me_client_id; |
| 571 | mei_hdr.reserved = 0; |
| 572 | dev->iamthif_msg_buf_index += mei_hdr.length; |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 573 | if (mei_write_message(dev, &mei_hdr, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 574 | (unsigned char *)(dev->iamthif_msg_buf), |
| 575 | mei_hdr.length)) |
| 576 | return -ENODEV; |
| 577 | |
| 578 | if (mei_hdr.msg_complete) { |
| 579 | if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl)) |
| 580 | return -ENODEV; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 581 | dev->iamthif_flow_control_pending = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 582 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; |
| 583 | dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n"); |
| 584 | dev->iamthif_current_cb = cb; |
| 585 | dev->iamthif_file_object = cb->file_object; |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 586 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 587 | } else { |
| 588 | dev_dbg(&dev->pdev->dev, "message does not complete, " |
| 589 | "so add amthi cb to write list.\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 590 | list_add_tail(&cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 591 | } |
| 592 | } else { |
| 593 | if (!(dev->mei_host_buffer_is_empty)) |
| 594 | dev_dbg(&dev->pdev->dev, "host buffer is not empty"); |
| 595 | |
| 596 | dev_dbg(&dev->pdev->dev, "No flow control credentials, " |
| 597 | "so add iamthif cb to write list.\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 598 | list_add_tail(&cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * iamthif_ioctl_send_msg - send cmd data to amthi client |
| 605 | * |
| 606 | * @dev: the device structure |
| 607 | * |
| 608 | * returns 0 on success, <0 on failure. |
| 609 | */ |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 610 | void mei_run_next_iamthif_cmd(struct mei_device *dev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 611 | { |
| 612 | struct mei_cl *cl_tmp; |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 613 | struct mei_cl_cb *pos = NULL; |
| 614 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 615 | int status; |
| 616 | |
| 617 | if (!dev) |
| 618 | return; |
| 619 | |
| 620 | dev->iamthif_msg_buf_size = 0; |
| 621 | dev->iamthif_msg_buf_index = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 622 | dev->iamthif_canceled = false; |
| 623 | dev->iamthif_ioctl = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 624 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 625 | dev->iamthif_timer = 0; |
| 626 | dev->iamthif_file_object = NULL; |
| 627 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 628 | dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 629 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 630 | list_for_each_entry_safe(pos, next, &dev->amthi_cmd_list.list, list) { |
| 631 | list_del(&pos->list); |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 632 | cl_tmp = (struct mei_cl *)pos->file_private; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 633 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 634 | if (cl_tmp && cl_tmp == &dev->iamthif_cl) { |
| 635 | status = amthi_write(dev, pos); |
| 636 | if (status) { |
| 637 | dev_dbg(&dev->pdev->dev, |
| 638 | "amthi write failed status = %d\n", |
| 639 | status); |
| 640 | return; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 641 | } |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 642 | break; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | |