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