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