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 |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 112 | * @fp: pointer to file structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 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 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 135 | * @cb: io callback structure |
| 136 | * @length: size of the buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 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 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 157 | * mei_io_cb_alloc_resp_buf - allocate response buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 158 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 159 | * @cb: io callback structure |
| 160 | * @length: size of the buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 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 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 186 | * @cl: host client |
| 187 | */ |
| 188 | int mei_cl_flush_queues(struct mei_cl *cl) |
| 189 | { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 190 | struct mei_device *dev; |
| 191 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 192 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 193 | return -EINVAL; |
| 194 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 195 | dev = cl->dev; |
| 196 | |
| 197 | cl_dbg(dev, cl, "remove list entry belonging to cl\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 198 | mei_io_list_flush(&cl->dev->read_list, cl); |
| 199 | mei_io_list_flush(&cl->dev->write_list, cl); |
| 200 | mei_io_list_flush(&cl->dev->write_waiting_list, cl); |
| 201 | mei_io_list_flush(&cl->dev->ctrl_wr_list, cl); |
| 202 | mei_io_list_flush(&cl->dev->ctrl_rd_list, cl); |
| 203 | mei_io_list_flush(&cl->dev->amthif_cmd_list, cl); |
| 204 | mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl); |
| 205 | return 0; |
| 206 | } |
| 207 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 208 | |
| 209 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 210 | * mei_cl_init - initializes cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 211 | * |
| 212 | * @cl: host client to be initialized |
| 213 | * @dev: mei device |
| 214 | */ |
| 215 | void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) |
| 216 | { |
| 217 | memset(cl, 0, sizeof(struct mei_cl)); |
| 218 | init_waitqueue_head(&cl->wait); |
| 219 | init_waitqueue_head(&cl->rx_wait); |
| 220 | init_waitqueue_head(&cl->tx_wait); |
| 221 | INIT_LIST_HEAD(&cl->link); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 222 | INIT_LIST_HEAD(&cl->device_link); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 223 | cl->reading_state = MEI_IDLE; |
| 224 | cl->writing_state = MEI_IDLE; |
| 225 | cl->dev = dev; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * mei_cl_allocate - allocates cl structure and sets it up. |
| 230 | * |
| 231 | * @dev: mei device |
| 232 | * returns The allocated file or NULL on failure |
| 233 | */ |
| 234 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) |
| 235 | { |
| 236 | struct mei_cl *cl; |
| 237 | |
| 238 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); |
| 239 | if (!cl) |
| 240 | return NULL; |
| 241 | |
| 242 | mei_cl_init(cl, dev); |
| 243 | |
| 244 | return cl; |
| 245 | } |
| 246 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 247 | /** |
| 248 | * mei_cl_find_read_cb - find this cl's callback in the read list |
| 249 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 250 | * @cl: host client |
| 251 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 252 | * returns cb on success, NULL on error |
| 253 | */ |
| 254 | struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl) |
| 255 | { |
| 256 | struct mei_device *dev = cl->dev; |
| 257 | struct mei_cl_cb *cb = NULL; |
| 258 | struct mei_cl_cb *next = NULL; |
| 259 | |
| 260 | list_for_each_entry_safe(cb, next, &dev->read_list.list, list) |
| 261 | if (mei_cl_cmp_id(cl, cb->cl)) |
| 262 | return cb; |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 266 | /** mei_cl_link: allocate host id in the host map |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 267 | * |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 268 | * @cl - host client |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 269 | * @id - fixed host id or -1 for generic one |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 270 | * |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 271 | * returns 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 272 | * -EINVAL on incorrect values |
| 273 | * -ENONET if client not found |
| 274 | */ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 275 | int mei_cl_link(struct mei_cl *cl, int id) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 276 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 277 | struct mei_device *dev; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 278 | long open_handle_count; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 279 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 280 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 281 | return -EINVAL; |
| 282 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 283 | dev = cl->dev; |
| 284 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 285 | /* If Id is not assigned get one*/ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 286 | if (id == MEI_HOST_CLIENT_ID_ANY) |
| 287 | id = find_first_zero_bit(dev->host_clients_map, |
| 288 | MEI_CLIENTS_MAX); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 289 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 290 | if (id >= MEI_CLIENTS_MAX) { |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 291 | dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 292 | return -EMFILE; |
| 293 | } |
| 294 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 295 | open_handle_count = dev->open_handle_count + dev->iamthif_open_count; |
| 296 | if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 297 | dev_err(&dev->pdev->dev, "open_handle_count exceeded %d", |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 298 | MEI_MAX_OPEN_HANDLE_COUNT); |
| 299 | return -EMFILE; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 302 | dev->open_handle_count++; |
| 303 | |
| 304 | cl->host_client_id = id; |
| 305 | list_add_tail(&cl->link, &dev->file_list); |
| 306 | |
| 307 | set_bit(id, dev->host_clients_map); |
| 308 | |
| 309 | cl->state = MEI_FILE_INITIALIZING; |
| 310 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 311 | cl_dbg(dev, cl, "link cl\n"); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 312 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 313 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 314 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 315 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 316 | * mei_cl_unlink - remove me_cl from the list |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 317 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 318 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 319 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 320 | int mei_cl_unlink(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 321 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 322 | struct mei_device *dev; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 323 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 324 | /* don't shout on error exit path */ |
| 325 | if (!cl) |
| 326 | return 0; |
| 327 | |
Tomas Winkler | 8e9a4a9 | 2013-01-10 17:32:14 +0200 | [diff] [blame] | 328 | /* wd and amthif might not be initialized */ |
| 329 | if (!cl->dev) |
| 330 | return 0; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 331 | |
| 332 | dev = cl->dev; |
| 333 | |
Tomas Winkler | a14c44d | 2013-09-16 23:44:45 +0300 | [diff] [blame] | 334 | cl_dbg(dev, cl, "unlink client"); |
| 335 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 336 | if (dev->open_handle_count > 0) |
| 337 | dev->open_handle_count--; |
| 338 | |
| 339 | /* never clear the 0 bit */ |
| 340 | if (cl->host_client_id) |
| 341 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 342 | |
| 343 | list_del_init(&cl->link); |
| 344 | |
| 345 | cl->state = MEI_FILE_INITIALIZING; |
| 346 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 347 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | void mei_host_client_init(struct work_struct *work) |
| 352 | { |
| 353 | struct mei_device *dev = container_of(work, |
| 354 | struct mei_device, init_work); |
| 355 | struct mei_client_properties *client_props; |
| 356 | int i; |
| 357 | |
| 358 | mutex_lock(&dev->device_lock); |
| 359 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 360 | for (i = 0; i < dev->me_clients_num; i++) { |
| 361 | client_props = &dev->me_clients[i].props; |
| 362 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 363 | if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 364 | mei_amthif_host_init(dev); |
| 365 | else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid)) |
| 366 | mei_wd_host_init(dev); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 367 | else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid)) |
| 368 | mei_nfc_host_init(dev); |
| 369 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | dev->dev_state = MEI_DEV_ENABLED; |
Tomas Winkler | 6adb8ef | 2014-01-12 00:36:10 +0200 | [diff] [blame] | 373 | dev->reset_count = 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 374 | |
| 375 | mutex_unlock(&dev->device_lock); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 380 | * mei_cl_disconnect - disconnect host client from the me one |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 381 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 382 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 383 | * |
| 384 | * Locking: called under "dev->device_lock" lock |
| 385 | * |
| 386 | * returns 0 on success, <0 on failure. |
| 387 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 388 | int mei_cl_disconnect(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 389 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 390 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 391 | struct mei_cl_cb *cb; |
| 392 | int rets, err; |
| 393 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 394 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 395 | return -ENODEV; |
| 396 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 397 | dev = cl->dev; |
| 398 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 399 | cl_dbg(dev, cl, "disconnecting"); |
| 400 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 401 | if (cl->state != MEI_FILE_DISCONNECTING) |
| 402 | return 0; |
| 403 | |
| 404 | cb = mei_io_cb_init(cl, NULL); |
| 405 | if (!cb) |
| 406 | return -ENOMEM; |
| 407 | |
| 408 | cb->fop_type = MEI_FOP_CLOSE; |
Tomas Winkler | 330dd7d | 2013-02-06 14:06:43 +0200 | [diff] [blame] | 409 | if (dev->hbuf_is_ready) { |
| 410 | dev->hbuf_is_ready = false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 411 | if (mei_hbm_cl_disconnect_req(dev, cl)) { |
| 412 | rets = -ENODEV; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 413 | cl_err(dev, cl, "failed to disconnect.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 414 | goto free; |
| 415 | } |
| 416 | mdelay(10); /* Wait for hardware disconnection ready */ |
| 417 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 418 | } else { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 419 | cl_dbg(dev, cl, "add disconnect cb to control write list\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 420 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 421 | |
| 422 | } |
| 423 | mutex_unlock(&dev->device_lock); |
| 424 | |
| 425 | err = wait_event_timeout(dev->wait_recvd_msg, |
| 426 | MEI_FILE_DISCONNECTED == cl->state, |
| 427 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 428 | |
| 429 | mutex_lock(&dev->device_lock); |
| 430 | if (MEI_FILE_DISCONNECTED == cl->state) { |
| 431 | rets = 0; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 432 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 433 | } else { |
| 434 | rets = -ENODEV; |
| 435 | if (MEI_FILE_DISCONNECTED != cl->state) |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 436 | cl_err(dev, cl, "wrong status client disconnect.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 437 | |
| 438 | if (err) |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 439 | cl_dbg(dev, cl, "wait failed disconnect err=%08x\n", |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 440 | err); |
| 441 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 442 | cl_err(dev, cl, "failed to disconnect from FW client.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 446 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 447 | free: |
| 448 | mei_io_cb_free(cb); |
| 449 | return rets; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 454 | * mei_cl_is_other_connecting - checks if other |
| 455 | * client with the same me client id is connecting |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 456 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 457 | * @cl: private data of the file object |
| 458 | * |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 459 | * returns true if other client is connected, false - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 460 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 461 | bool mei_cl_is_other_connecting(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 462 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 463 | struct mei_device *dev; |
| 464 | struct mei_cl *pos; |
| 465 | struct mei_cl *next; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 466 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 467 | if (WARN_ON(!cl || !cl->dev)) |
| 468 | return false; |
| 469 | |
| 470 | dev = cl->dev; |
| 471 | |
| 472 | list_for_each_entry_safe(pos, next, &dev->file_list, link) { |
| 473 | if ((pos->state == MEI_FILE_CONNECTING) && |
| 474 | (pos != cl) && cl->me_client_id == pos->me_client_id) |
| 475 | return true; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 476 | |
| 477 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 478 | |
| 479 | return false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 483 | * mei_cl_connect - connect host client to the me one |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 484 | * |
| 485 | * @cl: host client |
| 486 | * |
| 487 | * Locking: called under "dev->device_lock" lock |
| 488 | * |
| 489 | * returns 0 on success, <0 on failure. |
| 490 | */ |
| 491 | int mei_cl_connect(struct mei_cl *cl, struct file *file) |
| 492 | { |
| 493 | struct mei_device *dev; |
| 494 | struct mei_cl_cb *cb; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 495 | int rets; |
| 496 | |
| 497 | if (WARN_ON(!cl || !cl->dev)) |
| 498 | return -ENODEV; |
| 499 | |
| 500 | dev = cl->dev; |
| 501 | |
| 502 | cb = mei_io_cb_init(cl, file); |
| 503 | if (!cb) { |
| 504 | rets = -ENOMEM; |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | cb->fop_type = MEI_FOP_IOCTL; |
| 509 | |
Tomas Winkler | 330dd7d | 2013-02-06 14:06:43 +0200 | [diff] [blame] | 510 | if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) { |
| 511 | dev->hbuf_is_ready = false; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 512 | |
| 513 | if (mei_hbm_cl_connect_req(dev, cl)) { |
| 514 | rets = -ENODEV; |
| 515 | goto out; |
| 516 | } |
| 517 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
| 518 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 519 | } else { |
| 520 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 521 | } |
| 522 | |
| 523 | mutex_unlock(&dev->device_lock); |
| 524 | rets = wait_event_timeout(dev->wait_recvd_msg, |
| 525 | (cl->state == MEI_FILE_CONNECTED || |
| 526 | cl->state == MEI_FILE_DISCONNECTED), |
Frode Isaksen | 206ecfc | 2013-06-23 09:37:01 +0300 | [diff] [blame] | 527 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 528 | mutex_lock(&dev->device_lock); |
| 529 | |
| 530 | if (cl->state != MEI_FILE_CONNECTED) { |
| 531 | rets = -EFAULT; |
| 532 | |
| 533 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 534 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 535 | goto out; |
| 536 | } |
| 537 | |
| 538 | rets = cl->status; |
| 539 | |
| 540 | out: |
| 541 | mei_io_cb_free(cb); |
| 542 | return rets; |
| 543 | } |
| 544 | |
| 545 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 546 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 547 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 548 | * @cl: private data of the file object |
| 549 | * |
| 550 | * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
| 551 | * -ENOENT if mei_cl is not present |
| 552 | * -EINVAL if single_recv_buf == 0 |
| 553 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 554 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 555 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 556 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 557 | int i; |
| 558 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 559 | if (WARN_ON(!cl || !cl->dev)) |
| 560 | return -EINVAL; |
| 561 | |
| 562 | dev = cl->dev; |
| 563 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 564 | if (!dev->me_clients_num) |
| 565 | return 0; |
| 566 | |
| 567 | if (cl->mei_flow_ctrl_creds > 0) |
| 568 | return 1; |
| 569 | |
| 570 | for (i = 0; i < dev->me_clients_num; i++) { |
| 571 | struct mei_me_client *me_cl = &dev->me_clients[i]; |
| 572 | if (me_cl->client_id == cl->me_client_id) { |
| 573 | if (me_cl->mei_flow_ctrl_creds) { |
| 574 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
| 575 | return -EINVAL; |
| 576 | return 1; |
| 577 | } else { |
| 578 | return 0; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | return -ENOENT; |
| 583 | } |
| 584 | |
| 585 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 586 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 587 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 588 | * @cl: private data of the file object |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 589 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 590 | * @returns |
| 591 | * 0 on success |
| 592 | * -ENOENT when me client is not found |
| 593 | * -EINVAL when ctrl credits are <= 0 |
| 594 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 595 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 596 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 597 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 598 | int i; |
| 599 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 600 | if (WARN_ON(!cl || !cl->dev)) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | dev = cl->dev; |
| 604 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 605 | if (!dev->me_clients_num) |
| 606 | return -ENOENT; |
| 607 | |
| 608 | for (i = 0; i < dev->me_clients_num; i++) { |
| 609 | struct mei_me_client *me_cl = &dev->me_clients[i]; |
| 610 | if (me_cl->client_id == cl->me_client_id) { |
| 611 | if (me_cl->props.single_recv_buf != 0) { |
| 612 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) |
| 613 | return -EINVAL; |
| 614 | dev->me_clients[i].mei_flow_ctrl_creds--; |
| 615 | } else { |
| 616 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) |
| 617 | return -EINVAL; |
| 618 | cl->mei_flow_ctrl_creds--; |
| 619 | } |
| 620 | return 0; |
| 621 | } |
| 622 | } |
| 623 | return -ENOENT; |
| 624 | } |
| 625 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 626 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 627 | * mei_cl_read_start - the start read client message function. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 628 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 629 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 630 | * |
| 631 | * returns 0 on success, <0 on failure. |
| 632 | */ |
Tomas Winkler | fcb136e | 2013-04-19 22:01:35 +0300 | [diff] [blame] | 633 | int mei_cl_read_start(struct mei_cl *cl, size_t length) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 634 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 635 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 636 | struct mei_cl_cb *cb; |
| 637 | int rets; |
| 638 | int i; |
| 639 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 640 | if (WARN_ON(!cl || !cl->dev)) |
| 641 | return -ENODEV; |
| 642 | |
| 643 | dev = cl->dev; |
| 644 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 645 | if (!mei_cl_is_connected(cl)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 646 | return -ENODEV; |
| 647 | |
Tomas Winkler | d91aaed | 2013-01-08 23:07:18 +0200 | [diff] [blame] | 648 | if (cl->read_cb) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 649 | cl_dbg(dev, cl, "read is pending.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 650 | return -EBUSY; |
| 651 | } |
| 652 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 653 | if (i < 0) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 654 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 655 | return -ENODEV; |
| 656 | } |
| 657 | |
| 658 | cb = mei_io_cb_init(cl, NULL); |
| 659 | if (!cb) |
| 660 | return -ENOMEM; |
| 661 | |
Tomas Winkler | fcb136e | 2013-04-19 22:01:35 +0300 | [diff] [blame] | 662 | /* always allocate at least client max message */ |
| 663 | length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length); |
| 664 | rets = mei_io_cb_alloc_resp_buf(cb, length); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 665 | if (rets) |
| 666 | goto err; |
| 667 | |
| 668 | cb->fop_type = MEI_FOP_READ; |
| 669 | cl->read_cb = cb; |
Tomas Winkler | 330dd7d | 2013-02-06 14:06:43 +0200 | [diff] [blame] | 670 | if (dev->hbuf_is_ready) { |
| 671 | dev->hbuf_is_ready = false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 672 | if (mei_hbm_cl_flow_control_req(dev, cl)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 673 | cl_err(dev, cl, "flow control send failed\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 674 | rets = -ENODEV; |
| 675 | goto err; |
| 676 | } |
| 677 | list_add_tail(&cb->list, &dev->read_list.list); |
| 678 | } else { |
| 679 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 680 | } |
| 681 | return rets; |
| 682 | err: |
| 683 | mei_io_cb_free(cb); |
| 684 | return rets; |
| 685 | } |
| 686 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 687 | /** |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 688 | * mei_cl_irq_write_complete - write a message to device |
| 689 | * from the interrupt thread context |
| 690 | * |
| 691 | * @cl: client |
| 692 | * @cb: callback block. |
| 693 | * @slots: free slots. |
| 694 | * @cmpl_list: complete list. |
| 695 | * |
| 696 | * returns 0, OK; otherwise error. |
| 697 | */ |
| 698 | int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 699 | s32 *slots, struct mei_cl_cb *cmpl_list) |
| 700 | { |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 701 | struct mei_device *dev; |
| 702 | struct mei_msg_data *buf; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 703 | struct mei_msg_hdr mei_hdr; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 704 | size_t len; |
| 705 | u32 msg_slots; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 706 | int rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 707 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 708 | |
| 709 | if (WARN_ON(!cl || !cl->dev)) |
| 710 | return -ENODEV; |
| 711 | |
| 712 | dev = cl->dev; |
| 713 | |
| 714 | buf = &cb->request_buffer; |
| 715 | |
| 716 | rets = mei_cl_flow_ctrl_creds(cl); |
| 717 | if (rets < 0) |
| 718 | return rets; |
| 719 | |
| 720 | if (rets == 0) { |
| 721 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | len = buf->size - cb->buf_idx; |
| 726 | msg_slots = mei_data2slots(len); |
| 727 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 728 | mei_hdr.host_addr = cl->host_client_id; |
| 729 | mei_hdr.me_addr = cl->me_client_id; |
| 730 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 731 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 732 | |
| 733 | if (*slots >= msg_slots) { |
| 734 | mei_hdr.length = len; |
| 735 | mei_hdr.msg_complete = 1; |
| 736 | /* Split the message only if we can write the whole host buffer */ |
| 737 | } else if (*slots == dev->hbuf_depth) { |
| 738 | msg_slots = *slots; |
| 739 | len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
| 740 | mei_hdr.length = len; |
| 741 | mei_hdr.msg_complete = 0; |
| 742 | } else { |
| 743 | /* wait for next time the host buffer is empty */ |
| 744 | return 0; |
| 745 | } |
| 746 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 747 | cl_dbg(dev, cl, "buf: size = %d idx = %lu\n", |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 748 | cb->request_buffer.size, cb->buf_idx); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 749 | |
| 750 | *slots -= msg_slots; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 751 | rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 752 | if (rets) { |
| 753 | cl->status = rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 754 | list_move_tail(&cb->list, &cmpl_list->list); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 755 | return rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | cl->status = 0; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 759 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 760 | cb->buf_idx += mei_hdr.length; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 761 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 762 | if (mei_hdr.msg_complete) { |
| 763 | if (mei_cl_flow_ctrl_reduce(cl)) |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 764 | return -EIO; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 765 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | /** |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 772 | * mei_cl_write - submit a write cb to mei device |
| 773 | assumes device_lock is locked |
| 774 | * |
| 775 | * @cl: host client |
| 776 | * @cl: write callback with filled data |
| 777 | * |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 778 | * returns number of bytes sent on success, <0 on failure. |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 779 | */ |
| 780 | int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking) |
| 781 | { |
| 782 | struct mei_device *dev; |
| 783 | struct mei_msg_data *buf; |
| 784 | struct mei_msg_hdr mei_hdr; |
| 785 | int rets; |
| 786 | |
| 787 | |
| 788 | if (WARN_ON(!cl || !cl->dev)) |
| 789 | return -ENODEV; |
| 790 | |
| 791 | if (WARN_ON(!cb)) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | dev = cl->dev; |
| 795 | |
| 796 | |
| 797 | buf = &cb->request_buffer; |
| 798 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 799 | cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 800 | |
| 801 | |
| 802 | cb->fop_type = MEI_FOP_WRITE; |
| 803 | |
| 804 | rets = mei_cl_flow_ctrl_creds(cl); |
| 805 | if (rets < 0) |
| 806 | goto err; |
| 807 | |
| 808 | /* Host buffer is not ready, we queue the request */ |
| 809 | if (rets == 0 || !dev->hbuf_is_ready) { |
| 810 | cb->buf_idx = 0; |
| 811 | /* unseting complete will enqueue the cb for write */ |
| 812 | mei_hdr.msg_complete = 0; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 813 | rets = buf->size; |
| 814 | goto out; |
| 815 | } |
| 816 | |
| 817 | dev->hbuf_is_ready = false; |
| 818 | |
| 819 | /* Check for a maximum length */ |
| 820 | if (buf->size > mei_hbuf_max_len(dev)) { |
| 821 | mei_hdr.length = mei_hbuf_max_len(dev); |
| 822 | mei_hdr.msg_complete = 0; |
| 823 | } else { |
| 824 | mei_hdr.length = buf->size; |
| 825 | mei_hdr.msg_complete = 1; |
| 826 | } |
| 827 | |
| 828 | mei_hdr.host_addr = cl->host_client_id; |
| 829 | mei_hdr.me_addr = cl->me_client_id; |
| 830 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 831 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 832 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 833 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 834 | rets = mei_write_message(dev, &mei_hdr, buf->data); |
| 835 | if (rets) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 836 | goto err; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 837 | |
| 838 | cl->writing_state = MEI_WRITING; |
| 839 | cb->buf_idx = mei_hdr.length; |
| 840 | |
| 841 | rets = buf->size; |
| 842 | out: |
| 843 | if (mei_hdr.msg_complete) { |
| 844 | if (mei_cl_flow_ctrl_reduce(cl)) { |
| 845 | rets = -ENODEV; |
| 846 | goto err; |
| 847 | } |
| 848 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
| 849 | } else { |
| 850 | list_add_tail(&cb->list, &dev->write_list.list); |
| 851 | } |
| 852 | |
| 853 | |
| 854 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { |
| 855 | |
| 856 | mutex_unlock(&dev->device_lock); |
| 857 | if (wait_event_interruptible(cl->tx_wait, |
| 858 | cl->writing_state == MEI_WRITE_COMPLETE)) { |
| 859 | if (signal_pending(current)) |
| 860 | rets = -EINTR; |
| 861 | else |
| 862 | rets = -ERESTARTSYS; |
| 863 | } |
| 864 | mutex_lock(&dev->device_lock); |
| 865 | } |
| 866 | err: |
| 867 | return rets; |
| 868 | } |
| 869 | |
| 870 | |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 871 | /** |
| 872 | * mei_cl_complete - processes completed operation for a client |
| 873 | * |
| 874 | * @cl: private data of the file object. |
| 875 | * @cb: callback block. |
| 876 | */ |
| 877 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 878 | { |
| 879 | if (cb->fop_type == MEI_FOP_WRITE) { |
| 880 | mei_io_cb_free(cb); |
| 881 | cb = NULL; |
| 882 | cl->writing_state = MEI_WRITE_COMPLETE; |
| 883 | if (waitqueue_active(&cl->tx_wait)) |
| 884 | wake_up_interruptible(&cl->tx_wait); |
| 885 | |
| 886 | } else if (cb->fop_type == MEI_FOP_READ && |
| 887 | MEI_READING == cl->reading_state) { |
| 888 | cl->reading_state = MEI_READ_COMPLETE; |
| 889 | if (waitqueue_active(&cl->rx_wait)) |
| 890 | wake_up_interruptible(&cl->rx_wait); |
| 891 | else |
| 892 | mei_cl_bus_rx_event(cl); |
| 893 | |
| 894 | } |
| 895 | } |
| 896 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 897 | |
| 898 | /** |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 899 | * mei_cl_all_disconnect - disconnect forcefully all connected clients |
| 900 | * |
| 901 | * @dev - mei device |
| 902 | */ |
| 903 | |
| 904 | void mei_cl_all_disconnect(struct mei_device *dev) |
| 905 | { |
| 906 | struct mei_cl *cl, *next; |
| 907 | |
| 908 | list_for_each_entry_safe(cl, next, &dev->file_list, link) { |
| 909 | cl->state = MEI_FILE_DISCONNECTED; |
| 910 | cl->mei_flow_ctrl_creds = 0; |
| 911 | cl->read_cb = NULL; |
| 912 | cl->timer_count = 0; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | |
| 917 | /** |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 918 | * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 919 | * |
| 920 | * @dev - mei device |
| 921 | */ |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 922 | void mei_cl_all_wakeup(struct mei_device *dev) |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 923 | { |
| 924 | struct mei_cl *cl, *next; |
| 925 | list_for_each_entry_safe(cl, next, &dev->file_list, link) { |
| 926 | if (waitqueue_active(&cl->rx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 927 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 928 | wake_up_interruptible(&cl->rx_wait); |
| 929 | } |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 930 | if (waitqueue_active(&cl->tx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 931 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 932 | wake_up_interruptible(&cl->tx_wait); |
| 933 | } |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * mei_cl_all_write_clear - clear all pending writes |
| 939 | |
| 940 | * @dev - mei device |
| 941 | */ |
| 942 | void mei_cl_all_write_clear(struct mei_device *dev) |
| 943 | { |
| 944 | struct mei_cl_cb *cb, *next; |
| 945 | |
| 946 | list_for_each_entry_safe(cb, next, &dev->write_list.list, list) { |
| 947 | list_del(&cb->list); |
| 948 | mei_io_cb_free(cb); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |