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 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 17 | #include <linux/sched.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <linux/delay.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 20 | #include <linux/slab.h> |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 22 | |
| 23 | #include <linux/mei.h> |
| 24 | |
| 25 | #include "mei_dev.h" |
| 26 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 27 | #include "client.h" |
| 28 | |
| 29 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 30 | * mei_me_cl_by_uuid - locate me client by uuid |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 31 | * |
| 32 | * @dev: mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 33 | * @uuid: me client uuid |
Alexander Usyskin | a27a76d | 2014-02-17 15:13:22 +0200 | [diff] [blame] | 34 | * |
| 35 | * Locking: called under "dev->device_lock" lock |
| 36 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 37 | * Return: me client or NULL if not found |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 38 | */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 39 | struct mei_me_client *mei_me_cl_by_uuid(const struct mei_device *dev, |
| 40 | const uuid_le *uuid) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 41 | { |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 42 | struct mei_me_client *me_cl; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 43 | |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 44 | list_for_each_entry(me_cl, &dev->me_clients, list) |
| 45 | if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0) |
| 46 | return me_cl; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 47 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 48 | return NULL; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 51 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 52 | * mei_me_cl_by_id - locate me client by client id |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 53 | * |
| 54 | * @dev: the device structure |
| 55 | * @client_id: me client id |
| 56 | * |
| 57 | * Locking: called under "dev->device_lock" lock |
| 58 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 59 | * Return: me client or NULL if not found |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 60 | */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 61 | struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 62 | { |
Alexander Usyskin | a27a76d | 2014-02-17 15:13:22 +0200 | [diff] [blame] | 63 | |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 64 | struct mei_me_client *me_cl; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 65 | |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 66 | list_for_each_entry(me_cl, &dev->me_clients, list) |
| 67 | if (me_cl->client_id == client_id) |
| 68 | return me_cl; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 69 | return NULL; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 70 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 71 | |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 72 | /** |
| 73 | * mei_me_cl_by_uuid_id - locate me client by client id and uuid |
| 74 | * |
| 75 | * @dev: the device structure |
| 76 | * @uuid: me client uuid |
| 77 | * @client_id: me client id |
| 78 | * |
| 79 | * Locking: called under "dev->device_lock" lock |
| 80 | * |
| 81 | * Return: me client or NULL if not found |
| 82 | */ |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 83 | struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev, |
| 84 | const uuid_le *uuid, u8 client_id) |
| 85 | { |
| 86 | struct mei_me_client *me_cl; |
| 87 | |
| 88 | list_for_each_entry(me_cl, &dev->me_clients, list) |
| 89 | if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 && |
| 90 | me_cl->client_id == client_id) |
| 91 | return me_cl; |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 95 | /** |
| 96 | * mei_me_cl_remove - remove me client matching uuid and client_id |
| 97 | * |
| 98 | * @dev: the device structure |
| 99 | * @uuid: me client uuid |
| 100 | * @client_id: me client address |
| 101 | */ |
| 102 | void mei_me_cl_remove(struct mei_device *dev, const uuid_le *uuid, u8 client_id) |
| 103 | { |
| 104 | struct mei_me_client *me_cl, *next; |
| 105 | |
| 106 | list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) { |
| 107 | if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 && |
| 108 | me_cl->client_id == client_id) { |
| 109 | list_del(&me_cl->list); |
| 110 | kfree(me_cl); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 118 | * mei_cl_cmp_id - tells if the clients are the same |
| 119 | * |
| 120 | * @cl1: host client 1 |
| 121 | * @cl2: host client 2 |
| 122 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 123 | * Return: true - if the clients has same host and me ids |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 124 | * false - otherwise |
| 125 | */ |
| 126 | static inline bool mei_cl_cmp_id(const struct mei_cl *cl1, |
| 127 | const struct mei_cl *cl2) |
| 128 | { |
| 129 | return cl1 && cl2 && |
| 130 | (cl1->host_client_id == cl2->host_client_id) && |
| 131 | (cl1->me_client_id == cl2->me_client_id); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * mei_io_list_flush - removes cbs belonging to cl. |
| 136 | * |
| 137 | * @list: an instance of our list structure |
| 138 | * @cl: host client, can be NULL for flushing the whole list |
| 139 | * @free: whether to free the cbs |
| 140 | */ |
| 141 | static void __mei_io_list_flush(struct mei_cl_cb *list, |
| 142 | struct mei_cl *cl, bool free) |
| 143 | { |
| 144 | struct mei_cl_cb *cb; |
| 145 | struct mei_cl_cb *next; |
| 146 | |
| 147 | /* enable removing everything if no cl is specified */ |
| 148 | list_for_each_entry_safe(cb, next, &list->list, list) { |
| 149 | if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) { |
| 150 | list_del(&cb->list); |
| 151 | if (free) |
| 152 | mei_io_cb_free(cb); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 158 | * mei_io_list_flush - removes list entry belonging to cl. |
| 159 | * |
| 160 | * @list: An instance of our list structure |
| 161 | * @cl: host client |
| 162 | */ |
Alexander Usyskin | 5456796 | 2014-08-14 17:22:20 +0300 | [diff] [blame] | 163 | void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 164 | { |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 165 | __mei_io_list_flush(list, cl, false); |
| 166 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 167 | |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * mei_io_list_free - removes cb belonging to cl and free them |
| 171 | * |
| 172 | * @list: An instance of our list structure |
| 173 | * @cl: host client |
| 174 | */ |
| 175 | static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl) |
| 176 | { |
| 177 | __mei_io_list_flush(list, cl, true); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * mei_io_cb_free - free mei_cb_private related memory |
| 182 | * |
| 183 | * @cb: mei callback struct |
| 184 | */ |
| 185 | void mei_io_cb_free(struct mei_cl_cb *cb) |
| 186 | { |
| 187 | if (cb == NULL) |
| 188 | return; |
| 189 | |
| 190 | kfree(cb->request_buffer.data); |
| 191 | kfree(cb->response_buffer.data); |
| 192 | kfree(cb); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * mei_io_cb_init - allocate and initialize io callback |
| 197 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 198 | * @cl: mei client |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 199 | * @fp: pointer to file structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 200 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 201 | * Return: mei_cl_cb pointer or NULL; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 202 | */ |
| 203 | struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp) |
| 204 | { |
| 205 | struct mei_cl_cb *cb; |
| 206 | |
| 207 | cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); |
| 208 | if (!cb) |
| 209 | return NULL; |
| 210 | |
| 211 | mei_io_list_init(cb); |
| 212 | |
| 213 | cb->file_object = fp; |
| 214 | cb->cl = cl; |
| 215 | cb->buf_idx = 0; |
| 216 | return cb; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * mei_io_cb_alloc_req_buf - allocate request buffer |
| 221 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 222 | * @cb: io callback structure |
| 223 | * @length: size of the buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 224 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 225 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 226 | * -EINVAL if cb is NULL |
| 227 | * -ENOMEM if allocation failed |
| 228 | */ |
| 229 | int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length) |
| 230 | { |
| 231 | if (!cb) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | if (length == 0) |
| 235 | return 0; |
| 236 | |
| 237 | cb->request_buffer.data = kmalloc(length, GFP_KERNEL); |
| 238 | if (!cb->request_buffer.data) |
| 239 | return -ENOMEM; |
| 240 | cb->request_buffer.size = length; |
| 241 | return 0; |
| 242 | } |
| 243 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 244 | * mei_io_cb_alloc_resp_buf - allocate response buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 245 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 246 | * @cb: io callback structure |
| 247 | * @length: size of the buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 248 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 249 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 250 | * -EINVAL if cb is NULL |
| 251 | * -ENOMEM if allocation failed |
| 252 | */ |
| 253 | int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length) |
| 254 | { |
| 255 | if (!cb) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | if (length == 0) |
| 259 | return 0; |
| 260 | |
| 261 | cb->response_buffer.data = kmalloc(length, GFP_KERNEL); |
| 262 | if (!cb->response_buffer.data) |
| 263 | return -ENOMEM; |
| 264 | cb->response_buffer.size = length; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | /** |
| 271 | * mei_cl_flush_queues - flushes queue lists belonging to cl. |
| 272 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 273 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame^] | 274 | * |
| 275 | * Return: 0 on success, -EINVAL if cl or cl->dev is NULL. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 276 | */ |
| 277 | int mei_cl_flush_queues(struct mei_cl *cl) |
| 278 | { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 279 | struct mei_device *dev; |
| 280 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 281 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 282 | return -EINVAL; |
| 283 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 284 | dev = cl->dev; |
| 285 | |
| 286 | cl_dbg(dev, cl, "remove list entry belonging to cl\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 287 | mei_io_list_flush(&cl->dev->read_list, cl); |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 288 | mei_io_list_free(&cl->dev->write_list, cl); |
| 289 | mei_io_list_free(&cl->dev->write_waiting_list, cl); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 290 | mei_io_list_flush(&cl->dev->ctrl_wr_list, cl); |
| 291 | mei_io_list_flush(&cl->dev->ctrl_rd_list, cl); |
| 292 | mei_io_list_flush(&cl->dev->amthif_cmd_list, cl); |
| 293 | mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl); |
| 294 | return 0; |
| 295 | } |
| 296 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 297 | |
| 298 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 299 | * mei_cl_init - initializes cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 300 | * |
| 301 | * @cl: host client to be initialized |
| 302 | * @dev: mei device |
| 303 | */ |
| 304 | void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) |
| 305 | { |
| 306 | memset(cl, 0, sizeof(struct mei_cl)); |
| 307 | init_waitqueue_head(&cl->wait); |
| 308 | init_waitqueue_head(&cl->rx_wait); |
| 309 | init_waitqueue_head(&cl->tx_wait); |
| 310 | INIT_LIST_HEAD(&cl->link); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 311 | INIT_LIST_HEAD(&cl->device_link); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 312 | cl->reading_state = MEI_IDLE; |
| 313 | cl->writing_state = MEI_IDLE; |
| 314 | cl->dev = dev; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * mei_cl_allocate - allocates cl structure and sets it up. |
| 319 | * |
| 320 | * @dev: mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 321 | * Return: The allocated file or NULL on failure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 322 | */ |
| 323 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) |
| 324 | { |
| 325 | struct mei_cl *cl; |
| 326 | |
| 327 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); |
| 328 | if (!cl) |
| 329 | return NULL; |
| 330 | |
| 331 | mei_cl_init(cl, dev); |
| 332 | |
| 333 | return cl; |
| 334 | } |
| 335 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 336 | /** |
| 337 | * mei_cl_find_read_cb - find this cl's callback in the read list |
| 338 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 339 | * @cl: host client |
| 340 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 341 | * Return: cb on success, NULL on error |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 342 | */ |
| 343 | struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl) |
| 344 | { |
| 345 | struct mei_device *dev = cl->dev; |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 346 | struct mei_cl_cb *cb; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 347 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 348 | list_for_each_entry(cb, &dev->read_list.list, list) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 349 | if (mei_cl_cmp_id(cl, cb->cl)) |
| 350 | return cb; |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 354 | /** mei_cl_link: allocate host id in the host map |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 355 | * |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 356 | * @cl - host client |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 357 | * @id - fixed host id or -1 for generic one |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 358 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 359 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 360 | * -EINVAL on incorrect values |
| 361 | * -ENONET if client not found |
| 362 | */ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 363 | int mei_cl_link(struct mei_cl *cl, int id) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 364 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 365 | struct mei_device *dev; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 366 | long open_handle_count; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 367 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 368 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 369 | return -EINVAL; |
| 370 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 371 | dev = cl->dev; |
| 372 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 373 | /* If Id is not assigned get one*/ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 374 | if (id == MEI_HOST_CLIENT_ID_ANY) |
| 375 | id = find_first_zero_bit(dev->host_clients_map, |
| 376 | MEI_CLIENTS_MAX); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 377 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 378 | if (id >= MEI_CLIENTS_MAX) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 379 | dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 380 | return -EMFILE; |
| 381 | } |
| 382 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 383 | open_handle_count = dev->open_handle_count + dev->iamthif_open_count; |
| 384 | if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 385 | dev_err(dev->dev, "open_handle_count exceeded %d", |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 386 | MEI_MAX_OPEN_HANDLE_COUNT); |
| 387 | return -EMFILE; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 390 | dev->open_handle_count++; |
| 391 | |
| 392 | cl->host_client_id = id; |
| 393 | list_add_tail(&cl->link, &dev->file_list); |
| 394 | |
| 395 | set_bit(id, dev->host_clients_map); |
| 396 | |
| 397 | cl->state = MEI_FILE_INITIALIZING; |
| 398 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 399 | cl_dbg(dev, cl, "link cl\n"); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 400 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 401 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 402 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 403 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 404 | * mei_cl_unlink - remove me_cl from the list |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 405 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 406 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame^] | 407 | * |
| 408 | * Return: always 0 |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 409 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 410 | int mei_cl_unlink(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 411 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 412 | struct mei_device *dev; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 413 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 414 | /* don't shout on error exit path */ |
| 415 | if (!cl) |
| 416 | return 0; |
| 417 | |
Tomas Winkler | 8e9a4a9 | 2013-01-10 17:32:14 +0200 | [diff] [blame] | 418 | /* wd and amthif might not be initialized */ |
| 419 | if (!cl->dev) |
| 420 | return 0; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 421 | |
| 422 | dev = cl->dev; |
| 423 | |
Tomas Winkler | a14c44d | 2013-09-16 23:44:45 +0300 | [diff] [blame] | 424 | cl_dbg(dev, cl, "unlink client"); |
| 425 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 426 | if (dev->open_handle_count > 0) |
| 427 | dev->open_handle_count--; |
| 428 | |
| 429 | /* never clear the 0 bit */ |
| 430 | if (cl->host_client_id) |
| 431 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 432 | |
| 433 | list_del_init(&cl->link); |
| 434 | |
| 435 | cl->state = MEI_FILE_INITIALIZING; |
| 436 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 437 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
| 441 | void mei_host_client_init(struct work_struct *work) |
| 442 | { |
| 443 | struct mei_device *dev = container_of(work, |
| 444 | struct mei_device, init_work); |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 445 | struct mei_me_client *me_cl; |
| 446 | struct mei_client_properties *props; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 447 | |
| 448 | mutex_lock(&dev->device_lock); |
| 449 | |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 450 | list_for_each_entry(me_cl, &dev->me_clients, list) { |
| 451 | props = &me_cl->props; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 452 | |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 453 | if (!uuid_le_cmp(props->protocol_name, mei_amthif_guid)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 454 | mei_amthif_host_init(dev); |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 455 | else if (!uuid_le_cmp(props->protocol_name, mei_wd_guid)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 456 | mei_wd_host_init(dev); |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 457 | else if (!uuid_le_cmp(props->protocol_name, mei_nfc_guid)) |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 458 | mei_nfc_host_init(dev); |
| 459 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | dev->dev_state = MEI_DEV_ENABLED; |
Tomas Winkler | 6adb8ef | 2014-01-12 00:36:10 +0200 | [diff] [blame] | 463 | dev->reset_count = 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 464 | |
| 465 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 466 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 467 | pm_runtime_mark_last_busy(dev->dev); |
| 468 | dev_dbg(dev->dev, "rpm: autosuspend\n"); |
| 469 | pm_runtime_autosuspend(dev->dev); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 472 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 473 | * mei_hbuf_acquire - try to acquire host buffer |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 474 | * |
| 475 | * @dev: the device structure |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 476 | * Return: true if host buffer was acquired |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 477 | */ |
| 478 | bool mei_hbuf_acquire(struct mei_device *dev) |
| 479 | { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 480 | if (mei_pg_state(dev) == MEI_PG_ON || |
| 481 | dev->pg_event == MEI_PG_EVENT_WAIT) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 482 | dev_dbg(dev->dev, "device is in pg\n"); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 483 | return false; |
| 484 | } |
| 485 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 486 | if (!dev->hbuf_is_ready) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 487 | dev_dbg(dev->dev, "hbuf is not ready\n"); |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | |
| 491 | dev->hbuf_is_ready = false; |
| 492 | |
| 493 | return true; |
| 494 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 495 | |
| 496 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 497 | * mei_cl_disconnect - disconnect host client from the me one |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 498 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 499 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 500 | * |
| 501 | * Locking: called under "dev->device_lock" lock |
| 502 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 503 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 504 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 505 | int mei_cl_disconnect(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 506 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 507 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 508 | struct mei_cl_cb *cb; |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 509 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 510 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 511 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 512 | return -ENODEV; |
| 513 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 514 | dev = cl->dev; |
| 515 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 516 | cl_dbg(dev, cl, "disconnecting"); |
| 517 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 518 | if (cl->state != MEI_FILE_DISCONNECTING) |
| 519 | return 0; |
| 520 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 521 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 522 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 523 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 524 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 525 | return rets; |
| 526 | } |
| 527 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 528 | cb = mei_io_cb_init(cl, NULL); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 529 | if (!cb) { |
| 530 | rets = -ENOMEM; |
| 531 | goto free; |
| 532 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 533 | |
Tomas Winkler | 5a8373f | 2014-08-21 14:29:17 +0300 | [diff] [blame] | 534 | cb->fop_type = MEI_FOP_DISCONNECT; |
| 535 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 536 | if (mei_hbuf_acquire(dev)) { |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 537 | if (mei_hbm_cl_disconnect_req(dev, cl)) { |
| 538 | rets = -ENODEV; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 539 | cl_err(dev, cl, "failed to disconnect.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 540 | goto free; |
| 541 | } |
Alexander Usyskin | 22b987a | 2014-07-17 10:53:35 +0300 | [diff] [blame] | 542 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 543 | mdelay(10); /* Wait for hardware disconnection ready */ |
| 544 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 545 | } else { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 546 | cl_dbg(dev, cl, "add disconnect cb to control write list\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 547 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 548 | |
| 549 | } |
| 550 | mutex_unlock(&dev->device_lock); |
| 551 | |
Tomas Winkler | 12f45ed | 2014-08-21 14:29:18 +0300 | [diff] [blame] | 552 | wait_event_timeout(cl->wait, |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 553 | MEI_FILE_DISCONNECTED == cl->state, |
| 554 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 555 | |
| 556 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 557 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 558 | if (MEI_FILE_DISCONNECTED == cl->state) { |
| 559 | rets = 0; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 560 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 561 | } else { |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 562 | cl_dbg(dev, cl, "timeout on disconnect from FW client.\n"); |
| 563 | rets = -ETIME; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 567 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 568 | free: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 569 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 570 | pm_runtime_mark_last_busy(dev->dev); |
| 571 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 572 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 573 | mei_io_cb_free(cb); |
| 574 | return rets; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 579 | * mei_cl_is_other_connecting - checks if other |
| 580 | * client with the same me client id is connecting |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 581 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 582 | * @cl: private data of the file object |
| 583 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 584 | * Return: true if other client is connected, false - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 585 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 586 | bool mei_cl_is_other_connecting(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 | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 589 | struct mei_cl *ocl; /* the other client */ |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 590 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 591 | if (WARN_ON(!cl || !cl->dev)) |
| 592 | return false; |
| 593 | |
| 594 | dev = cl->dev; |
| 595 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 596 | list_for_each_entry(ocl, &dev->file_list, link) { |
| 597 | if (ocl->state == MEI_FILE_CONNECTING && |
| 598 | ocl != cl && |
| 599 | cl->me_client_id == ocl->me_client_id) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 600 | return true; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 601 | |
| 602 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 603 | |
| 604 | return false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 608 | * mei_cl_connect - connect host client to the me one |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 609 | * |
| 610 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 611 | * @file: pointer to file structure |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 612 | * |
| 613 | * Locking: called under "dev->device_lock" lock |
| 614 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 615 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 616 | */ |
| 617 | int mei_cl_connect(struct mei_cl *cl, struct file *file) |
| 618 | { |
| 619 | struct mei_device *dev; |
| 620 | struct mei_cl_cb *cb; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 621 | int rets; |
| 622 | |
| 623 | if (WARN_ON(!cl || !cl->dev)) |
| 624 | return -ENODEV; |
| 625 | |
| 626 | dev = cl->dev; |
| 627 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 628 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 629 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 630 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 631 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 632 | return rets; |
| 633 | } |
| 634 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 635 | cb = mei_io_cb_init(cl, file); |
| 636 | if (!cb) { |
| 637 | rets = -ENOMEM; |
| 638 | goto out; |
| 639 | } |
| 640 | |
Tomas Winkler | 02a7eec | 2014-02-12 21:41:51 +0200 | [diff] [blame] | 641 | cb->fop_type = MEI_FOP_CONNECT; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 642 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 643 | /* run hbuf acquire last so we don't have to undo */ |
| 644 | if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) { |
Alexander Usyskin | e4d8270 | 2014-04-27 15:42:21 +0300 | [diff] [blame] | 645 | cl->state = MEI_FILE_CONNECTING; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 646 | if (mei_hbm_cl_connect_req(dev, cl)) { |
| 647 | rets = -ENODEV; |
| 648 | goto out; |
| 649 | } |
| 650 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
| 651 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 652 | } else { |
Alexander Usyskin | 73ab423 | 2014-08-12 18:07:56 +0300 | [diff] [blame] | 653 | cl->state = MEI_FILE_INITIALIZING; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 654 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 655 | } |
| 656 | |
| 657 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 12f45ed | 2014-08-21 14:29:18 +0300 | [diff] [blame] | 658 | wait_event_timeout(cl->wait, |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 659 | (cl->state == MEI_FILE_CONNECTED || |
| 660 | cl->state == MEI_FILE_DISCONNECTED), |
| 661 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 662 | mutex_lock(&dev->device_lock); |
| 663 | |
| 664 | if (cl->state != MEI_FILE_CONNECTED) { |
Alexander Usyskin | 3e37ebb | 2014-07-17 10:53:34 +0300 | [diff] [blame] | 665 | cl->state = MEI_FILE_DISCONNECTED; |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 666 | /* something went really wrong */ |
| 667 | if (!cl->status) |
| 668 | cl->status = -EFAULT; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 669 | |
| 670 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 671 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | rets = cl->status; |
| 675 | |
| 676 | out: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 677 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 678 | pm_runtime_mark_last_busy(dev->dev); |
| 679 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 680 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 681 | mei_io_cb_free(cb); |
| 682 | return rets; |
| 683 | } |
| 684 | |
| 685 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 686 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 687 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 688 | * @cl: private data of the file object |
| 689 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 690 | * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 691 | * -ENOENT if mei_cl is not present |
| 692 | * -EINVAL if single_recv_buf == 0 |
| 693 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 694 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 695 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 696 | struct mei_device *dev; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 697 | struct mei_me_client *me_cl; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 698 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 699 | if (WARN_ON(!cl || !cl->dev)) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | dev = cl->dev; |
| 703 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 704 | if (cl->mei_flow_ctrl_creds > 0) |
| 705 | return 1; |
| 706 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 707 | me_cl = mei_me_cl_by_id(dev, cl->me_client_id); |
| 708 | if (!me_cl) { |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 709 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 710 | return -ENOENT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 711 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 712 | |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 713 | if (me_cl->mei_flow_ctrl_creds) { |
| 714 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
| 715 | return -EINVAL; |
| 716 | return 1; |
| 717 | } |
| 718 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 722 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 723 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 724 | * @cl: private data of the file object |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 725 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 726 | * Return: |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 727 | * 0 on success |
| 728 | * -ENOENT when me client is not found |
| 729 | * -EINVAL when ctrl credits are <= 0 |
| 730 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 731 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 732 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 733 | struct mei_device *dev; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 734 | struct mei_me_client *me_cl; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 735 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 736 | if (WARN_ON(!cl || !cl->dev)) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | dev = cl->dev; |
| 740 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 741 | me_cl = mei_me_cl_by_id(dev, cl->me_client_id); |
| 742 | if (!me_cl) { |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 743 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 744 | return -ENOENT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 745 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 746 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 747 | if (me_cl->props.single_recv_buf) { |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 748 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) |
| 749 | return -EINVAL; |
| 750 | me_cl->mei_flow_ctrl_creds--; |
| 751 | } else { |
| 752 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) |
| 753 | return -EINVAL; |
| 754 | cl->mei_flow_ctrl_creds--; |
| 755 | } |
| 756 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 759 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 760 | * mei_cl_read_start - the start read client message function. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 761 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 762 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame^] | 763 | * @length: number of bytes to read |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 764 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 765 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 766 | */ |
Tomas Winkler | fcb136e | 2013-04-19 22:01:35 +0300 | [diff] [blame] | 767 | int mei_cl_read_start(struct mei_cl *cl, size_t length) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 768 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 769 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 770 | struct mei_cl_cb *cb; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 771 | struct mei_me_client *me_cl; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 772 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 773 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 774 | if (WARN_ON(!cl || !cl->dev)) |
| 775 | return -ENODEV; |
| 776 | |
| 777 | dev = cl->dev; |
| 778 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 779 | if (!mei_cl_is_connected(cl)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 780 | return -ENODEV; |
| 781 | |
Tomas Winkler | d91aaed | 2013-01-08 23:07:18 +0200 | [diff] [blame] | 782 | if (cl->read_cb) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 783 | cl_dbg(dev, cl, "read is pending.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 784 | return -EBUSY; |
| 785 | } |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 786 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 787 | if (!me_cl) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 788 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 789 | return -ENOTTY; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 792 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 793 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 794 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 795 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 796 | return rets; |
| 797 | } |
| 798 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 799 | cb = mei_io_cb_init(cl, NULL); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 800 | if (!cb) { |
| 801 | rets = -ENOMEM; |
| 802 | goto out; |
| 803 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 804 | |
Tomas Winkler | fcb136e | 2013-04-19 22:01:35 +0300 | [diff] [blame] | 805 | /* always allocate at least client max message */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 806 | length = max_t(size_t, length, me_cl->props.max_msg_length); |
Tomas Winkler | fcb136e | 2013-04-19 22:01:35 +0300 | [diff] [blame] | 807 | rets = mei_io_cb_alloc_resp_buf(cb, length); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 808 | if (rets) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 809 | goto out; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 810 | |
| 811 | cb->fop_type = MEI_FOP_READ; |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 812 | if (mei_hbuf_acquire(dev)) { |
Alexander Usyskin | 8611350 | 2014-03-31 17:59:24 +0300 | [diff] [blame] | 813 | rets = mei_hbm_cl_flow_control_req(dev, cl); |
| 814 | if (rets < 0) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 815 | goto out; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 816 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 817 | list_add_tail(&cb->list, &dev->read_list.list); |
| 818 | } else { |
| 819 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 820 | } |
Chao Bi | accb884 | 2014-02-12 21:27:25 +0200 | [diff] [blame] | 821 | |
| 822 | cl->read_cb = cb; |
| 823 | |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 824 | out: |
| 825 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 826 | pm_runtime_mark_last_busy(dev->dev); |
| 827 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 828 | |
| 829 | if (rets) |
| 830 | mei_io_cb_free(cb); |
| 831 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 832 | return rets; |
| 833 | } |
| 834 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 835 | /** |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 836 | * mei_cl_irq_write - write a message to device |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 837 | * from the interrupt thread context |
| 838 | * |
| 839 | * @cl: client |
| 840 | * @cb: callback block. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 841 | * @cmpl_list: complete list. |
| 842 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 843 | * Return: 0, OK; otherwise error. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 844 | */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 845 | int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 846 | struct mei_cl_cb *cmpl_list) |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 847 | { |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 848 | struct mei_device *dev; |
| 849 | struct mei_msg_data *buf; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 850 | struct mei_msg_hdr mei_hdr; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 851 | size_t len; |
| 852 | u32 msg_slots; |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 853 | int slots; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 854 | int rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 855 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 856 | if (WARN_ON(!cl || !cl->dev)) |
| 857 | return -ENODEV; |
| 858 | |
| 859 | dev = cl->dev; |
| 860 | |
| 861 | buf = &cb->request_buffer; |
| 862 | |
| 863 | rets = mei_cl_flow_ctrl_creds(cl); |
| 864 | if (rets < 0) |
| 865 | return rets; |
| 866 | |
| 867 | if (rets == 0) { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 868 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 872 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 873 | len = buf->size - cb->buf_idx; |
| 874 | msg_slots = mei_data2slots(len); |
| 875 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 876 | mei_hdr.host_addr = cl->host_client_id; |
| 877 | mei_hdr.me_addr = cl->me_client_id; |
| 878 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 879 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 880 | |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 881 | if (slots >= msg_slots) { |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 882 | mei_hdr.length = len; |
| 883 | mei_hdr.msg_complete = 1; |
| 884 | /* Split the message only if we can write the whole host buffer */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 885 | } else if (slots == dev->hbuf_depth) { |
| 886 | msg_slots = slots; |
| 887 | len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 888 | mei_hdr.length = len; |
| 889 | mei_hdr.msg_complete = 0; |
| 890 | } else { |
| 891 | /* wait for next time the host buffer is empty */ |
| 892 | return 0; |
| 893 | } |
| 894 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 895 | cl_dbg(dev, cl, "buf: size = %d idx = %lu\n", |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 896 | cb->request_buffer.size, cb->buf_idx); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 897 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 898 | rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 899 | if (rets) { |
| 900 | cl->status = rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 901 | list_move_tail(&cb->list, &cmpl_list->list); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 902 | return rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | cl->status = 0; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 906 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 907 | cb->buf_idx += mei_hdr.length; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 908 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 909 | if (mei_hdr.msg_complete) { |
| 910 | if (mei_cl_flow_ctrl_reduce(cl)) |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 911 | return -EIO; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 912 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
| 913 | } |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | /** |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 919 | * mei_cl_write - submit a write cb to mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 920 | * assumes device_lock is locked |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 921 | * |
| 922 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 923 | * @cb: write callback with filled data |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame^] | 924 | * @blocking: block until completed |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 925 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 926 | * Return: number of bytes sent on success, <0 on failure. |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 927 | */ |
| 928 | int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking) |
| 929 | { |
| 930 | struct mei_device *dev; |
| 931 | struct mei_msg_data *buf; |
| 932 | struct mei_msg_hdr mei_hdr; |
| 933 | int rets; |
| 934 | |
| 935 | |
| 936 | if (WARN_ON(!cl || !cl->dev)) |
| 937 | return -ENODEV; |
| 938 | |
| 939 | if (WARN_ON(!cb)) |
| 940 | return -EINVAL; |
| 941 | |
| 942 | dev = cl->dev; |
| 943 | |
| 944 | |
| 945 | buf = &cb->request_buffer; |
| 946 | |
Alexander Usyskin | 0a01e97 | 2014-09-29 16:31:47 +0300 | [diff] [blame] | 947 | cl_dbg(dev, cl, "size=%d\n", buf->size); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 948 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 949 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 950 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 951 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 952 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 953 | return rets; |
| 954 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 955 | |
| 956 | cb->fop_type = MEI_FOP_WRITE; |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 957 | cb->buf_idx = 0; |
| 958 | cl->writing_state = MEI_IDLE; |
| 959 | |
| 960 | mei_hdr.host_addr = cl->host_client_id; |
| 961 | mei_hdr.me_addr = cl->me_client_id; |
| 962 | mei_hdr.reserved = 0; |
| 963 | mei_hdr.msg_complete = 0; |
| 964 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 965 | |
| 966 | rets = mei_cl_flow_ctrl_creds(cl); |
| 967 | if (rets < 0) |
| 968 | goto err; |
| 969 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 970 | if (rets == 0) { |
| 971 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 972 | rets = buf->size; |
| 973 | goto out; |
| 974 | } |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 975 | if (!mei_hbuf_acquire(dev)) { |
| 976 | cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n"); |
| 977 | rets = buf->size; |
| 978 | goto out; |
| 979 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 980 | |
| 981 | /* Check for a maximum length */ |
| 982 | if (buf->size > mei_hbuf_max_len(dev)) { |
| 983 | mei_hdr.length = mei_hbuf_max_len(dev); |
| 984 | mei_hdr.msg_complete = 0; |
| 985 | } else { |
| 986 | mei_hdr.length = buf->size; |
| 987 | mei_hdr.msg_complete = 1; |
| 988 | } |
| 989 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 990 | rets = mei_write_message(dev, &mei_hdr, buf->data); |
| 991 | if (rets) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 992 | goto err; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 993 | |
| 994 | cl->writing_state = MEI_WRITING; |
| 995 | cb->buf_idx = mei_hdr.length; |
| 996 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 997 | out: |
| 998 | if (mei_hdr.msg_complete) { |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 999 | rets = mei_cl_flow_ctrl_reduce(cl); |
| 1000 | if (rets < 0) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1001 | goto err; |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1002 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1003 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
| 1004 | } else { |
| 1005 | list_add_tail(&cb->list, &dev->write_list.list); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { |
| 1010 | |
| 1011 | mutex_unlock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1012 | rets = wait_event_interruptible(cl->tx_wait, |
| 1013 | cl->writing_state == MEI_WRITE_COMPLETE); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1014 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1015 | /* wait_event_interruptible returns -ERESTARTSYS */ |
| 1016 | if (rets) { |
| 1017 | if (signal_pending(current)) |
| 1018 | rets = -EINTR; |
| 1019 | goto err; |
| 1020 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1021 | } |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1022 | |
| 1023 | rets = buf->size; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1024 | err: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1025 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1026 | pm_runtime_mark_last_busy(dev->dev); |
| 1027 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1028 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1029 | return rets; |
| 1030 | } |
| 1031 | |
| 1032 | |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 1033 | /** |
| 1034 | * mei_cl_complete - processes completed operation for a client |
| 1035 | * |
| 1036 | * @cl: private data of the file object. |
| 1037 | * @cb: callback block. |
| 1038 | */ |
| 1039 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 1040 | { |
| 1041 | if (cb->fop_type == MEI_FOP_WRITE) { |
| 1042 | mei_io_cb_free(cb); |
| 1043 | cb = NULL; |
| 1044 | cl->writing_state = MEI_WRITE_COMPLETE; |
| 1045 | if (waitqueue_active(&cl->tx_wait)) |
| 1046 | wake_up_interruptible(&cl->tx_wait); |
| 1047 | |
| 1048 | } else if (cb->fop_type == MEI_FOP_READ && |
| 1049 | MEI_READING == cl->reading_state) { |
| 1050 | cl->reading_state = MEI_READ_COMPLETE; |
| 1051 | if (waitqueue_active(&cl->rx_wait)) |
| 1052 | wake_up_interruptible(&cl->rx_wait); |
| 1053 | else |
| 1054 | mei_cl_bus_rx_event(cl); |
| 1055 | |
| 1056 | } |
| 1057 | } |
| 1058 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1059 | |
| 1060 | /** |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1061 | * mei_cl_all_disconnect - disconnect forcefully all connected clients |
| 1062 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1063 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1064 | */ |
| 1065 | |
| 1066 | void mei_cl_all_disconnect(struct mei_device *dev) |
| 1067 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1068 | struct mei_cl *cl; |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1069 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1070 | list_for_each_entry(cl, &dev->file_list, link) { |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1071 | cl->state = MEI_FILE_DISCONNECTED; |
| 1072 | cl->mei_flow_ctrl_creds = 0; |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1073 | cl->timer_count = 0; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | /** |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1079 | * 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] | 1080 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1081 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1082 | */ |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1083 | void mei_cl_all_wakeup(struct mei_device *dev) |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1084 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1085 | struct mei_cl *cl; |
Tomas Winkler | 92db155 | 2014-09-29 16:31:37 +0300 | [diff] [blame] | 1086 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1087 | list_for_each_entry(cl, &dev->file_list, link) { |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1088 | if (waitqueue_active(&cl->rx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 1089 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1090 | wake_up_interruptible(&cl->rx_wait); |
| 1091 | } |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1092 | if (waitqueue_active(&cl->tx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 1093 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1094 | wake_up_interruptible(&cl->tx_wait); |
| 1095 | } |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * mei_cl_all_write_clear - clear all pending writes |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1101 | * |
| 1102 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1103 | */ |
| 1104 | void mei_cl_all_write_clear(struct mei_device *dev) |
| 1105 | { |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 1106 | mei_io_list_free(&dev->write_list, NULL); |
| 1107 | mei_io_list_free(&dev->write_waiting_list, NULL); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | |