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; |
Tomas Winkler | db3ed43 | 2012-11-11 17:37:59 +0200 | [diff] [blame^] | 74 | cb->cl = cl; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 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; |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 176 | long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 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 || |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 294 | MEI_FILE_DISCONNECTED == cl->state), timeout); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 295 | |
| 296 | mutex_lock(&dev->device_lock); |
| 297 | if (MEI_FILE_CONNECTED == cl->state) { |
| 298 | dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n"); |
| 299 | rets = cl->status; |
| 300 | goto end; |
| 301 | } else { |
| 302 | dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n", |
| 303 | cl->state); |
| 304 | if (!err) { |
| 305 | dev_dbg(&dev->pdev->dev, |
| 306 | "wait_event_interruptible_timeout failed on client" |
| 307 | " connect message fw response message.\n"); |
| 308 | } |
| 309 | rets = -EFAULT; |
| 310 | |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 311 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 312 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 313 | goto end; |
| 314 | } |
| 315 | rets = 0; |
| 316 | end: |
| 317 | dev_dbg(&dev->pdev->dev, "free connect cb memory."); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 318 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 319 | return rets; |
| 320 | } |
| 321 | |
| 322 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 323 | * mei_start_read - the start read client message function. |
| 324 | * |
| 325 | * @dev: the device structure |
| 326 | * @if_num: minor number |
| 327 | * @cl: private data of the file object |
| 328 | * |
| 329 | * returns 0 on success, <0 on failure. |
| 330 | */ |
| 331 | int mei_start_read(struct mei_device *dev, struct mei_cl *cl) |
| 332 | { |
| 333 | struct mei_cl_cb *cb; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 334 | int rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 335 | int i; |
| 336 | |
| 337 | if (cl->state != MEI_FILE_CONNECTED) |
| 338 | return -ENODEV; |
| 339 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 340 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 341 | return -ENODEV; |
| 342 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 343 | if (cl->read_pending || cl->read_cb) { |
| 344 | dev_dbg(&dev->pdev->dev, "read is pending.\n"); |
| 345 | return -EBUSY; |
| 346 | } |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 347 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 348 | if (i < 0) { |
| 349 | dev_err(&dev->pdev->dev, "no such me client %d\n", |
| 350 | cl->me_client_id); |
| 351 | return -ENODEV; |
| 352 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 353 | |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 354 | cb = mei_io_cb_init(cl, NULL); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 355 | if (!cb) |
| 356 | return -ENOMEM; |
| 357 | |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 358 | rets = mei_io_cb_alloc_resp_buf(cb, |
| 359 | dev->me_clients[i].props.max_msg_length); |
| 360 | if (rets) |
| 361 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 362 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 363 | cb->major_file_operations = MEI_READ; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 364 | cl->read_cb = cb; |
| 365 | if (dev->mei_host_buffer_is_empty) { |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 366 | dev->mei_host_buffer_is_empty = false; |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 367 | if (mei_send_flow_control(dev, cl)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 368 | rets = -ENODEV; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 369 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 370 | } |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 371 | list_add_tail(&cb->list, &dev->read_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 372 | } else { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 373 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 374 | } |
| 375 | return rets; |
Tomas Winkler | 664df38 | 2012-10-11 16:35:08 +0200 | [diff] [blame] | 376 | err: |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 377 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 378 | return rets; |
| 379 | } |
| 380 | |