Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +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/pci.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/delay.h> |
| 21 | |
| 22 | #include <linux/mei.h> |
| 23 | |
| 24 | #include "mei_dev.h" |
| 25 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 26 | #include "client.h" |
| 27 | |
| 28 | /** |
| 29 | * mei_me_cl_by_uuid - locate index of me client |
| 30 | * |
| 31 | * @dev: mei device |
| 32 | * returns me client index or -ENOENT if not found |
| 33 | */ |
| 34 | int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid) |
| 35 | { |
| 36 | int i, res = -ENOENT; |
| 37 | |
| 38 | for (i = 0; i < dev->me_clients_num; ++i) |
| 39 | if (uuid_le_cmp(*uuid, |
| 40 | dev->me_clients[i].props.protocol_name) == 0) { |
| 41 | res = i; |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | return res; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * mei_me_cl_by_id return index to me_clients for client_id |
| 51 | * |
| 52 | * @dev: the device structure |
| 53 | * @client_id: me client id |
| 54 | * |
| 55 | * Locking: called under "dev->device_lock" lock |
| 56 | * |
| 57 | * returns index on success, -ENOENT on failure. |
| 58 | */ |
| 59 | |
| 60 | int mei_me_cl_by_id(struct mei_device *dev, u8 client_id) |
| 61 | { |
| 62 | int i; |
| 63 | for (i = 0; i < dev->me_clients_num; i++) |
| 64 | if (dev->me_clients[i].client_id == client_id) |
| 65 | break; |
| 66 | if (WARN_ON(dev->me_clients[i].client_id != client_id)) |
| 67 | return -ENOENT; |
| 68 | |
| 69 | if (i == dev->me_clients_num) |
| 70 | return -ENOENT; |
| 71 | |
| 72 | return i; |
| 73 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 74 | |
| 75 | |
| 76 | /** |
| 77 | * mei_io_list_flush - removes list entry belonging to cl. |
| 78 | * |
| 79 | * @list: An instance of our list structure |
| 80 | * @cl: host client |
| 81 | */ |
| 82 | void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl) |
| 83 | { |
| 84 | struct mei_cl_cb *cb; |
| 85 | struct mei_cl_cb *next; |
| 86 | |
| 87 | list_for_each_entry_safe(cb, next, &list->list, list) { |
| 88 | if (cb->cl && mei_cl_cmp_id(cl, cb->cl)) |
| 89 | list_del(&cb->list); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * mei_io_cb_free - free mei_cb_private related memory |
| 95 | * |
| 96 | * @cb: mei callback struct |
| 97 | */ |
| 98 | void mei_io_cb_free(struct mei_cl_cb *cb) |
| 99 | { |
| 100 | if (cb == NULL) |
| 101 | return; |
| 102 | |
| 103 | kfree(cb->request_buffer.data); |
| 104 | kfree(cb->response_buffer.data); |
| 105 | kfree(cb); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * mei_io_cb_init - allocate and initialize io callback |
| 110 | * |
| 111 | * @cl - mei client |
| 112 | * @file: pointer to file structure |
| 113 | * |
| 114 | * returns mei_cl_cb pointer or NULL; |
| 115 | */ |
| 116 | struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp) |
| 117 | { |
| 118 | struct mei_cl_cb *cb; |
| 119 | |
| 120 | cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); |
| 121 | if (!cb) |
| 122 | return NULL; |
| 123 | |
| 124 | mei_io_list_init(cb); |
| 125 | |
| 126 | cb->file_object = fp; |
| 127 | cb->cl = cl; |
| 128 | cb->buf_idx = 0; |
| 129 | return cb; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * mei_io_cb_alloc_req_buf - allocate request buffer |
| 134 | * |
| 135 | * @cb - io callback structure |
| 136 | * @size: size of the buffer |
| 137 | * |
| 138 | * returns 0 on success |
| 139 | * -EINVAL if cb is NULL |
| 140 | * -ENOMEM if allocation failed |
| 141 | */ |
| 142 | int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length) |
| 143 | { |
| 144 | if (!cb) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | if (length == 0) |
| 148 | return 0; |
| 149 | |
| 150 | cb->request_buffer.data = kmalloc(length, GFP_KERNEL); |
| 151 | if (!cb->request_buffer.data) |
| 152 | return -ENOMEM; |
| 153 | cb->request_buffer.size = length; |
| 154 | return 0; |
| 155 | } |
| 156 | /** |
| 157 | * mei_io_cb_alloc_req_buf - allocate respose buffer |
| 158 | * |
| 159 | * @cb - io callback structure |
| 160 | * @size: size of the buffer |
| 161 | * |
| 162 | * returns 0 on success |
| 163 | * -EINVAL if cb is NULL |
| 164 | * -ENOMEM if allocation failed |
| 165 | */ |
| 166 | int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length) |
| 167 | { |
| 168 | if (!cb) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | if (length == 0) |
| 172 | return 0; |
| 173 | |
| 174 | cb->response_buffer.data = kmalloc(length, GFP_KERNEL); |
| 175 | if (!cb->response_buffer.data) |
| 176 | return -ENOMEM; |
| 177 | cb->response_buffer.size = length; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * mei_cl_flush_queues - flushes queue lists belonging to cl. |
| 185 | * |
| 186 | * @dev: the device structure |
| 187 | * @cl: host client |
| 188 | */ |
| 189 | int mei_cl_flush_queues(struct mei_cl *cl) |
| 190 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 191 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | |
| 194 | dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n"); |
| 195 | mei_io_list_flush(&cl->dev->read_list, cl); |
| 196 | mei_io_list_flush(&cl->dev->write_list, cl); |
| 197 | mei_io_list_flush(&cl->dev->write_waiting_list, cl); |
| 198 | mei_io_list_flush(&cl->dev->ctrl_wr_list, cl); |
| 199 | mei_io_list_flush(&cl->dev->ctrl_rd_list, cl); |
| 200 | mei_io_list_flush(&cl->dev->amthif_cmd_list, cl); |
| 201 | mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl); |
| 202 | return 0; |
| 203 | } |
| 204 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * mei_cl_init - initializes intialize cl. |
| 208 | * |
| 209 | * @cl: host client to be initialized |
| 210 | * @dev: mei device |
| 211 | */ |
| 212 | void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) |
| 213 | { |
| 214 | memset(cl, 0, sizeof(struct mei_cl)); |
| 215 | init_waitqueue_head(&cl->wait); |
| 216 | init_waitqueue_head(&cl->rx_wait); |
| 217 | init_waitqueue_head(&cl->tx_wait); |
| 218 | INIT_LIST_HEAD(&cl->link); |
| 219 | cl->reading_state = MEI_IDLE; |
| 220 | cl->writing_state = MEI_IDLE; |
| 221 | cl->dev = dev; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * mei_cl_allocate - allocates cl structure and sets it up. |
| 226 | * |
| 227 | * @dev: mei device |
| 228 | * returns The allocated file or NULL on failure |
| 229 | */ |
| 230 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) |
| 231 | { |
| 232 | struct mei_cl *cl; |
| 233 | |
| 234 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); |
| 235 | if (!cl) |
| 236 | return NULL; |
| 237 | |
| 238 | mei_cl_init(cl, dev); |
| 239 | |
| 240 | return cl; |
| 241 | } |
| 242 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 243 | /** |
| 244 | * mei_cl_find_read_cb - find this cl's callback in the read list |
| 245 | * |
| 246 | * @dev: device structure |
| 247 | * returns cb on success, NULL on error |
| 248 | */ |
| 249 | struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl) |
| 250 | { |
| 251 | struct mei_device *dev = cl->dev; |
| 252 | struct mei_cl_cb *cb = NULL; |
| 253 | struct mei_cl_cb *next = NULL; |
| 254 | |
| 255 | list_for_each_entry_safe(cb, next, &dev->read_list.list, list) |
| 256 | if (mei_cl_cmp_id(cl, cb->cl)) |
| 257 | return cb; |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 261 | /** mei_cl_link: allocte host id in the host map |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 262 | * |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 263 | * @cl - host client |
| 264 | * @id - fixed host id or -1 for genereting one |
| 265 | * returns 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 266 | * -EINVAL on incorrect values |
| 267 | * -ENONET if client not found |
| 268 | */ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 269 | int mei_cl_link(struct mei_cl *cl, int id) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 270 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 271 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 272 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 273 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 274 | return -EINVAL; |
| 275 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 276 | dev = cl->dev; |
| 277 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 278 | /* If Id is not asigned get one*/ |
| 279 | if (id == MEI_HOST_CLIENT_ID_ANY) |
| 280 | id = find_first_zero_bit(dev->host_clients_map, |
| 281 | MEI_CLIENTS_MAX); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 282 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 283 | if (id >= MEI_CLIENTS_MAX) { |
| 284 | dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ; |
| 285 | return -ENOENT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 288 | dev->open_handle_count++; |
| 289 | |
| 290 | cl->host_client_id = id; |
| 291 | list_add_tail(&cl->link, &dev->file_list); |
| 292 | |
| 293 | set_bit(id, dev->host_clients_map); |
| 294 | |
| 295 | cl->state = MEI_FILE_INITIALIZING; |
| 296 | |
| 297 | dev_dbg(&dev->pdev->dev, "link cl host id = %d\n", cl->host_client_id); |
| 298 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 299 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 300 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 301 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 302 | * mei_cl_unlink - remove me_cl from the list |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 303 | * |
| 304 | * @dev: the device structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 305 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 306 | int mei_cl_unlink(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 307 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 308 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 309 | struct mei_cl *pos, *next; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 310 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 311 | /* don't shout on error exit path */ |
| 312 | if (!cl) |
| 313 | return 0; |
| 314 | |
Tomas Winkler | 8e9a4a9 | 2013-01-10 17:32:14 +0200 | [diff] [blame^] | 315 | /* wd and amthif might not be initialized */ |
| 316 | if (!cl->dev) |
| 317 | return 0; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 318 | |
| 319 | dev = cl->dev; |
| 320 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 321 | list_for_each_entry_safe(pos, next, &dev->file_list, link) { |
| 322 | if (cl->host_client_id == pos->host_client_id) { |
| 323 | dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n", |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 324 | pos->host_client_id, pos->me_client_id); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 325 | list_del_init(&pos->link); |
| 326 | break; |
| 327 | } |
| 328 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 329 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | void mei_host_client_init(struct work_struct *work) |
| 334 | { |
| 335 | struct mei_device *dev = container_of(work, |
| 336 | struct mei_device, init_work); |
| 337 | struct mei_client_properties *client_props; |
| 338 | int i; |
| 339 | |
| 340 | mutex_lock(&dev->device_lock); |
| 341 | |
| 342 | bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX); |
| 343 | dev->open_handle_count = 0; |
| 344 | |
| 345 | /* |
| 346 | * Reserving the first three client IDs |
| 347 | * 0: Reserved for MEI Bus Message communications |
| 348 | * 1: Reserved for Watchdog |
| 349 | * 2: Reserved for AMTHI |
| 350 | */ |
| 351 | bitmap_set(dev->host_clients_map, 0, 3); |
| 352 | |
| 353 | for (i = 0; i < dev->me_clients_num; i++) { |
| 354 | client_props = &dev->me_clients[i].props; |
| 355 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 356 | if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 357 | mei_amthif_host_init(dev); |
| 358 | else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid)) |
| 359 | mei_wd_host_init(dev); |
| 360 | } |
| 361 | |
| 362 | dev->dev_state = MEI_DEV_ENABLED; |
| 363 | |
| 364 | mutex_unlock(&dev->device_lock); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 369 | * mei_cl_disconnect - disconnect host clinet form the me one |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 370 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 371 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 372 | * |
| 373 | * Locking: called under "dev->device_lock" lock |
| 374 | * |
| 375 | * returns 0 on success, <0 on failure. |
| 376 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 377 | int mei_cl_disconnect(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 378 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 379 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 380 | struct mei_cl_cb *cb; |
| 381 | int rets, err; |
| 382 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 383 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 384 | return -ENODEV; |
| 385 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 386 | dev = cl->dev; |
| 387 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 388 | if (cl->state != MEI_FILE_DISCONNECTING) |
| 389 | return 0; |
| 390 | |
| 391 | cb = mei_io_cb_init(cl, NULL); |
| 392 | if (!cb) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | cb->fop_type = MEI_FOP_CLOSE; |
| 396 | if (dev->mei_host_buffer_is_empty) { |
| 397 | dev->mei_host_buffer_is_empty = false; |
| 398 | if (mei_hbm_cl_disconnect_req(dev, cl)) { |
| 399 | rets = -ENODEV; |
| 400 | dev_err(&dev->pdev->dev, "failed to disconnect.\n"); |
| 401 | goto free; |
| 402 | } |
| 403 | mdelay(10); /* Wait for hardware disconnection ready */ |
| 404 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 405 | } else { |
| 406 | dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n"); |
| 407 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 408 | |
| 409 | } |
| 410 | mutex_unlock(&dev->device_lock); |
| 411 | |
| 412 | err = wait_event_timeout(dev->wait_recvd_msg, |
| 413 | MEI_FILE_DISCONNECTED == cl->state, |
| 414 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 415 | |
| 416 | mutex_lock(&dev->device_lock); |
| 417 | if (MEI_FILE_DISCONNECTED == cl->state) { |
| 418 | rets = 0; |
| 419 | dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n"); |
| 420 | } else { |
| 421 | rets = -ENODEV; |
| 422 | if (MEI_FILE_DISCONNECTED != cl->state) |
| 423 | dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n"); |
| 424 | |
| 425 | if (err) |
| 426 | dev_dbg(&dev->pdev->dev, |
| 427 | "wait failed disconnect err=%08x\n", |
| 428 | err); |
| 429 | |
| 430 | dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n"); |
| 431 | } |
| 432 | |
| 433 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 434 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 435 | free: |
| 436 | mei_io_cb_free(cb); |
| 437 | return rets; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 442 | * mei_cl_is_other_connecting - checks if other |
| 443 | * client with the same me client id is connecting |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 444 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 445 | * @cl: private data of the file object |
| 446 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 447 | * returns ture if other client is connected, 0 - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 448 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 449 | bool mei_cl_is_other_connecting(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 450 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 451 | struct mei_device *dev; |
| 452 | struct mei_cl *pos; |
| 453 | struct mei_cl *next; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 454 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 455 | if (WARN_ON(!cl || !cl->dev)) |
| 456 | return false; |
| 457 | |
| 458 | dev = cl->dev; |
| 459 | |
| 460 | list_for_each_entry_safe(pos, next, &dev->file_list, link) { |
| 461 | if ((pos->state == MEI_FILE_CONNECTING) && |
| 462 | (pos != cl) && cl->me_client_id == pos->me_client_id) |
| 463 | return true; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 464 | |
| 465 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 466 | |
| 467 | return false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 471 | * mei_cl_connect - connect host clinet to the me one |
| 472 | * |
| 473 | * @cl: host client |
| 474 | * |
| 475 | * Locking: called under "dev->device_lock" lock |
| 476 | * |
| 477 | * returns 0 on success, <0 on failure. |
| 478 | */ |
| 479 | int mei_cl_connect(struct mei_cl *cl, struct file *file) |
| 480 | { |
| 481 | struct mei_device *dev; |
| 482 | struct mei_cl_cb *cb; |
| 483 | long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT); |
| 484 | int rets; |
| 485 | |
| 486 | if (WARN_ON(!cl || !cl->dev)) |
| 487 | return -ENODEV; |
| 488 | |
| 489 | dev = cl->dev; |
| 490 | |
| 491 | cb = mei_io_cb_init(cl, file); |
| 492 | if (!cb) { |
| 493 | rets = -ENOMEM; |
| 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | cb->fop_type = MEI_FOP_IOCTL; |
| 498 | |
| 499 | if (dev->mei_host_buffer_is_empty && |
| 500 | !mei_cl_is_other_connecting(cl)) { |
| 501 | dev->mei_host_buffer_is_empty = false; |
| 502 | |
| 503 | if (mei_hbm_cl_connect_req(dev, cl)) { |
| 504 | rets = -ENODEV; |
| 505 | goto out; |
| 506 | } |
| 507 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
| 508 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 509 | } else { |
| 510 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 511 | } |
| 512 | |
| 513 | mutex_unlock(&dev->device_lock); |
| 514 | rets = wait_event_timeout(dev->wait_recvd_msg, |
| 515 | (cl->state == MEI_FILE_CONNECTED || |
| 516 | cl->state == MEI_FILE_DISCONNECTED), |
| 517 | timeout * HZ); |
| 518 | mutex_lock(&dev->device_lock); |
| 519 | |
| 520 | if (cl->state != MEI_FILE_CONNECTED) { |
| 521 | rets = -EFAULT; |
| 522 | |
| 523 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 524 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 525 | goto out; |
| 526 | } |
| 527 | |
| 528 | rets = cl->status; |
| 529 | |
| 530 | out: |
| 531 | mei_io_cb_free(cb); |
| 532 | return rets; |
| 533 | } |
| 534 | |
| 535 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 536 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 537 | * |
| 538 | * @dev: the device structure |
| 539 | * @cl: private data of the file object |
| 540 | * |
| 541 | * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
| 542 | * -ENOENT if mei_cl is not present |
| 543 | * -EINVAL if single_recv_buf == 0 |
| 544 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 545 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 546 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 547 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 548 | int i; |
| 549 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 550 | if (WARN_ON(!cl || !cl->dev)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | dev = cl->dev; |
| 554 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 555 | if (!dev->me_clients_num) |
| 556 | return 0; |
| 557 | |
| 558 | if (cl->mei_flow_ctrl_creds > 0) |
| 559 | return 1; |
| 560 | |
| 561 | for (i = 0; i < dev->me_clients_num; i++) { |
| 562 | struct mei_me_client *me_cl = &dev->me_clients[i]; |
| 563 | if (me_cl->client_id == cl->me_client_id) { |
| 564 | if (me_cl->mei_flow_ctrl_creds) { |
| 565 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
| 566 | return -EINVAL; |
| 567 | return 1; |
| 568 | } else { |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | return -ENOENT; |
| 574 | } |
| 575 | |
| 576 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 577 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 578 | * |
| 579 | * @dev: the device structure |
| 580 | * @cl: private data of the file object |
| 581 | * @returns |
| 582 | * 0 on success |
| 583 | * -ENOENT when me client is not found |
| 584 | * -EINVAL when ctrl credits are <= 0 |
| 585 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 586 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 587 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 588 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 589 | int i; |
| 590 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 591 | if (WARN_ON(!cl || !cl->dev)) |
| 592 | return -EINVAL; |
| 593 | |
| 594 | dev = cl->dev; |
| 595 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 596 | if (!dev->me_clients_num) |
| 597 | return -ENOENT; |
| 598 | |
| 599 | for (i = 0; i < dev->me_clients_num; i++) { |
| 600 | struct mei_me_client *me_cl = &dev->me_clients[i]; |
| 601 | if (me_cl->client_id == cl->me_client_id) { |
| 602 | if (me_cl->props.single_recv_buf != 0) { |
| 603 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) |
| 604 | return -EINVAL; |
| 605 | dev->me_clients[i].mei_flow_ctrl_creds--; |
| 606 | } else { |
| 607 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) |
| 608 | return -EINVAL; |
| 609 | cl->mei_flow_ctrl_creds--; |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | } |
| 614 | return -ENOENT; |
| 615 | } |
| 616 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 617 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 618 | * mei_cl_start_read - the start read client message function. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 619 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 620 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 621 | * |
| 622 | * returns 0 on success, <0 on failure. |
| 623 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 624 | int mei_cl_read_start(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 625 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 626 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 627 | struct mei_cl_cb *cb; |
| 628 | int rets; |
| 629 | int i; |
| 630 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 631 | if (WARN_ON(!cl || !cl->dev)) |
| 632 | return -ENODEV; |
| 633 | |
| 634 | dev = cl->dev; |
| 635 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 636 | if (cl->state != MEI_FILE_CONNECTED) |
| 637 | return -ENODEV; |
| 638 | |
| 639 | if (dev->dev_state != MEI_DEV_ENABLED) |
| 640 | return -ENODEV; |
| 641 | |
Tomas Winkler | d91aaed | 2013-01-08 23:07:18 +0200 | [diff] [blame] | 642 | if (cl->read_cb) { |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 643 | dev_dbg(&dev->pdev->dev, "read is pending.\n"); |
| 644 | return -EBUSY; |
| 645 | } |
| 646 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 647 | if (i < 0) { |
| 648 | dev_err(&dev->pdev->dev, "no such me client %d\n", |
| 649 | cl->me_client_id); |
| 650 | return -ENODEV; |
| 651 | } |
| 652 | |
| 653 | cb = mei_io_cb_init(cl, NULL); |
| 654 | if (!cb) |
| 655 | return -ENOMEM; |
| 656 | |
| 657 | rets = mei_io_cb_alloc_resp_buf(cb, |
| 658 | dev->me_clients[i].props.max_msg_length); |
| 659 | if (rets) |
| 660 | goto err; |
| 661 | |
| 662 | cb->fop_type = MEI_FOP_READ; |
| 663 | cl->read_cb = cb; |
| 664 | if (dev->mei_host_buffer_is_empty) { |
| 665 | dev->mei_host_buffer_is_empty = false; |
| 666 | if (mei_hbm_cl_flow_control_req(dev, cl)) { |
| 667 | rets = -ENODEV; |
| 668 | goto err; |
| 669 | } |
| 670 | list_add_tail(&cb->list, &dev->read_list.list); |
| 671 | } else { |
| 672 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 673 | } |
| 674 | return rets; |
| 675 | err: |
| 676 | mei_io_cb_free(cb); |
| 677 | return rets; |
| 678 | } |
| 679 | |