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 | /** |
Alexander Usyskin | 3908be6 | 2015-02-10 10:39:35 +0200 | [diff] [blame] | 324 | * __mei_io_list_flush - removes and frees cbs belonging to cl. |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 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 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 379 | kfree(cb->buf.data); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 380 | kfree(cb); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * mei_io_cb_init - allocate and initialize io callback |
| 385 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 386 | * @cl: mei client |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 387 | * @type: operation type |
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 | */ |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 392 | struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type, |
| 393 | struct file *fp) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 394 | { |
| 395 | struct mei_cl_cb *cb; |
| 396 | |
| 397 | cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); |
| 398 | if (!cb) |
| 399 | return NULL; |
| 400 | |
| 401 | mei_io_list_init(cb); |
| 402 | |
| 403 | cb->file_object = fp; |
| 404 | cb->cl = cl; |
| 405 | cb->buf_idx = 0; |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 406 | cb->fop_type = type; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 407 | return cb; |
| 408 | } |
| 409 | |
| 410 | /** |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 411 | * mei_io_cb_alloc_buf - allocate callback buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 412 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 413 | * @cb: io callback structure |
| 414 | * @length: size of the buffer |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 415 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 416 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 417 | * -EINVAL if cb is NULL |
| 418 | * -ENOMEM if allocation failed |
| 419 | */ |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 420 | int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 421 | { |
| 422 | if (!cb) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | if (length == 0) |
| 426 | return 0; |
| 427 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 428 | cb->buf.data = kmalloc(length, GFP_KERNEL); |
| 429 | if (!cb->buf.data) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 430 | return -ENOMEM; |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 431 | cb->buf.size = length; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 434 | |
| 435 | /** |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 436 | * mei_cl_alloc_cb - a convenient wrapper for allocating read cb |
| 437 | * |
| 438 | * @cl: host client |
| 439 | * @length: size of the buffer |
| 440 | * @type: operation type |
| 441 | * @fp: associated file pointer (might be NULL) |
| 442 | * |
| 443 | * Return: cb on success and NULL on failure |
| 444 | */ |
| 445 | struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length, |
| 446 | enum mei_cb_file_ops type, struct file *fp) |
| 447 | { |
| 448 | struct mei_cl_cb *cb; |
| 449 | |
| 450 | cb = mei_io_cb_init(cl, type, fp); |
| 451 | if (!cb) |
| 452 | return NULL; |
| 453 | |
| 454 | if (mei_io_cb_alloc_buf(cb, length)) { |
| 455 | mei_io_cb_free(cb); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | return cb; |
| 460 | } |
| 461 | |
| 462 | /** |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 463 | * mei_cl_flush_queues - flushes queue lists belonging to cl. |
| 464 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 465 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 466 | * |
| 467 | * Return: 0 on success, -EINVAL if cl or cl->dev is NULL. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 468 | */ |
| 469 | int mei_cl_flush_queues(struct mei_cl *cl) |
| 470 | { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 471 | struct mei_device *dev; |
| 472 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 473 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 474 | return -EINVAL; |
| 475 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 476 | dev = cl->dev; |
| 477 | |
| 478 | cl_dbg(dev, cl, "remove list entry belonging to cl\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 479 | mei_io_list_flush(&cl->dev->read_list, cl); |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 480 | mei_io_list_free(&cl->dev->write_list, cl); |
| 481 | mei_io_list_free(&cl->dev->write_waiting_list, cl); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 482 | mei_io_list_flush(&cl->dev->ctrl_wr_list, cl); |
| 483 | mei_io_list_flush(&cl->dev->ctrl_rd_list, cl); |
| 484 | mei_io_list_flush(&cl->dev->amthif_cmd_list, cl); |
| 485 | mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl); |
| 486 | return 0; |
| 487 | } |
| 488 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 489 | |
| 490 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 491 | * mei_cl_init - initializes cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 492 | * |
| 493 | * @cl: host client to be initialized |
| 494 | * @dev: mei device |
| 495 | */ |
| 496 | void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) |
| 497 | { |
| 498 | memset(cl, 0, sizeof(struct mei_cl)); |
| 499 | init_waitqueue_head(&cl->wait); |
| 500 | init_waitqueue_head(&cl->rx_wait); |
| 501 | init_waitqueue_head(&cl->tx_wait); |
| 502 | INIT_LIST_HEAD(&cl->link); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 503 | INIT_LIST_HEAD(&cl->device_link); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 504 | cl->reading_state = MEI_IDLE; |
| 505 | cl->writing_state = MEI_IDLE; |
| 506 | cl->dev = dev; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * mei_cl_allocate - allocates cl structure and sets it up. |
| 511 | * |
| 512 | * @dev: mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 513 | * Return: The allocated file or NULL on failure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 514 | */ |
| 515 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) |
| 516 | { |
| 517 | struct mei_cl *cl; |
| 518 | |
| 519 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); |
| 520 | if (!cl) |
| 521 | return NULL; |
| 522 | |
| 523 | mei_cl_init(cl, dev); |
| 524 | |
| 525 | return cl; |
| 526 | } |
| 527 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 528 | /** |
| 529 | * mei_cl_find_read_cb - find this cl's callback in the read list |
| 530 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 531 | * @cl: host client |
| 532 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 533 | * Return: cb on success, NULL on error |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 534 | */ |
| 535 | struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl) |
| 536 | { |
| 537 | struct mei_device *dev = cl->dev; |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 538 | struct mei_cl_cb *cb; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 539 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 540 | list_for_each_entry(cb, &dev->read_list.list, list) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 541 | if (mei_cl_cmp_id(cl, cb->cl)) |
| 542 | return cb; |
| 543 | return NULL; |
| 544 | } |
| 545 | |
Alexander Usyskin | 3908be6 | 2015-02-10 10:39:35 +0200 | [diff] [blame] | 546 | /** |
| 547 | * mei_cl_link - allocate host id in the host map |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 548 | * |
Alexander Usyskin | 3908be6 | 2015-02-10 10:39:35 +0200 | [diff] [blame] | 549 | * @cl: host client |
| 550 | * @id: fixed host id or -1 for generic one |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 551 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 552 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 553 | * -EINVAL on incorrect values |
| 554 | * -ENONET if client not found |
| 555 | */ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 556 | int mei_cl_link(struct mei_cl *cl, int id) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 557 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 558 | struct mei_device *dev; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 559 | long open_handle_count; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 560 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 561 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 562 | return -EINVAL; |
| 563 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 564 | dev = cl->dev; |
| 565 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 566 | /* If Id is not assigned get one*/ |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 567 | if (id == MEI_HOST_CLIENT_ID_ANY) |
| 568 | id = find_first_zero_bit(dev->host_clients_map, |
| 569 | MEI_CLIENTS_MAX); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 570 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 571 | if (id >= MEI_CLIENTS_MAX) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 572 | dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 573 | return -EMFILE; |
| 574 | } |
| 575 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 576 | open_handle_count = dev->open_handle_count + dev->iamthif_open_count; |
| 577 | if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 578 | dev_err(dev->dev, "open_handle_count exceeded %d", |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 579 | MEI_MAX_OPEN_HANDLE_COUNT); |
| 580 | return -EMFILE; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 583 | dev->open_handle_count++; |
| 584 | |
| 585 | cl->host_client_id = id; |
| 586 | list_add_tail(&cl->link, &dev->file_list); |
| 587 | |
| 588 | set_bit(id, dev->host_clients_map); |
| 589 | |
| 590 | cl->state = MEI_FILE_INITIALIZING; |
| 591 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 592 | cl_dbg(dev, cl, "link cl\n"); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 593 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 594 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 595 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 596 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 597 | * mei_cl_unlink - remove me_cl from the list |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 598 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 599 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 600 | * |
| 601 | * Return: always 0 |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 602 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 603 | int mei_cl_unlink(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 604 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 605 | struct mei_device *dev; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 606 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 607 | /* don't shout on error exit path */ |
| 608 | if (!cl) |
| 609 | return 0; |
| 610 | |
Tomas Winkler | 8e9a4a9 | 2013-01-10 17:32:14 +0200 | [diff] [blame] | 611 | /* wd and amthif might not be initialized */ |
| 612 | if (!cl->dev) |
| 613 | return 0; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 614 | |
| 615 | dev = cl->dev; |
| 616 | |
Tomas Winkler | a14c44d | 2013-09-16 23:44:45 +0300 | [diff] [blame] | 617 | cl_dbg(dev, cl, "unlink client"); |
| 618 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 619 | if (dev->open_handle_count > 0) |
| 620 | dev->open_handle_count--; |
| 621 | |
| 622 | /* never clear the 0 bit */ |
| 623 | if (cl->host_client_id) |
| 624 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 625 | |
| 626 | list_del_init(&cl->link); |
| 627 | |
| 628 | cl->state = MEI_FILE_INITIALIZING; |
| 629 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 630 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
| 634 | void mei_host_client_init(struct work_struct *work) |
| 635 | { |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 636 | struct mei_device *dev = |
| 637 | container_of(work, struct mei_device, init_work); |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 638 | struct mei_me_client *me_cl; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 639 | |
| 640 | mutex_lock(&dev->device_lock); |
| 641 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +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_amthif_guid); |
| 644 | if (me_cl) |
| 645 | mei_amthif_host_init(dev); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 646 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 647 | me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid); |
| 648 | if (me_cl) |
| 649 | mei_wd_host_init(dev); |
| 650 | |
| 651 | me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid); |
| 652 | if (me_cl) |
| 653 | mei_nfc_host_init(dev); |
| 654 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 655 | |
| 656 | dev->dev_state = MEI_DEV_ENABLED; |
Tomas Winkler | 6adb8ef | 2014-01-12 00:36:10 +0200 | [diff] [blame] | 657 | dev->reset_count = 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 658 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 659 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 660 | pm_runtime_mark_last_busy(dev->dev); |
| 661 | dev_dbg(dev->dev, "rpm: autosuspend\n"); |
| 662 | pm_runtime_autosuspend(dev->dev); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 665 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 666 | * mei_hbuf_acquire - try to acquire host buffer |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 667 | * |
| 668 | * @dev: the device structure |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 669 | * Return: true if host buffer was acquired |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 670 | */ |
| 671 | bool mei_hbuf_acquire(struct mei_device *dev) |
| 672 | { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 673 | if (mei_pg_state(dev) == MEI_PG_ON || |
| 674 | dev->pg_event == MEI_PG_EVENT_WAIT) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 675 | dev_dbg(dev->dev, "device is in pg\n"); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 679 | if (!dev->hbuf_is_ready) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 680 | dev_dbg(dev->dev, "hbuf is not ready\n"); |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 681 | return false; |
| 682 | } |
| 683 | |
| 684 | dev->hbuf_is_ready = false; |
| 685 | |
| 686 | return true; |
| 687 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 688 | |
| 689 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 690 | * mei_cl_disconnect - disconnect host client from the me one |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 691 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 692 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 693 | * |
| 694 | * Locking: called under "dev->device_lock" lock |
| 695 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 696 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 697 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 698 | int mei_cl_disconnect(struct mei_cl *cl) |
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 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 701 | struct mei_cl_cb *cb; |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 702 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 703 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 704 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 705 | return -ENODEV; |
| 706 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 707 | dev = cl->dev; |
| 708 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 709 | cl_dbg(dev, cl, "disconnecting"); |
| 710 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 711 | if (cl->state != MEI_FILE_DISCONNECTING) |
| 712 | return 0; |
| 713 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 714 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 715 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 716 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 717 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 718 | return rets; |
| 719 | } |
| 720 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 721 | cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL); |
| 722 | rets = cb ? 0 : -ENOMEM; |
| 723 | if (rets) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 724 | goto free; |
Tomas Winkler | 5a8373f | 2014-08-21 14:29:17 +0300 | [diff] [blame] | 725 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 726 | if (mei_hbuf_acquire(dev)) { |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 727 | if (mei_hbm_cl_disconnect_req(dev, cl)) { |
| 728 | rets = -ENODEV; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 729 | cl_err(dev, cl, "failed to disconnect.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 730 | goto free; |
| 731 | } |
Alexander Usyskin | 22b987a | 2014-07-17 10:53:35 +0300 | [diff] [blame] | 732 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 733 | mdelay(10); /* Wait for hardware disconnection ready */ |
| 734 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 735 | } else { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 736 | cl_dbg(dev, cl, "add disconnect cb to control write list\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 737 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 738 | |
| 739 | } |
| 740 | mutex_unlock(&dev->device_lock); |
| 741 | |
Tomas Winkler | 12f45ed | 2014-08-21 14:29:18 +0300 | [diff] [blame] | 742 | wait_event_timeout(cl->wait, |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 743 | MEI_FILE_DISCONNECTED == cl->state, |
| 744 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 745 | |
| 746 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 747 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 748 | if (MEI_FILE_DISCONNECTED == cl->state) { |
| 749 | rets = 0; |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 750 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 751 | } else { |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 752 | cl_dbg(dev, cl, "timeout on disconnect from FW client.\n"); |
| 753 | rets = -ETIME; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 757 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
| 758 | free: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 759 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 760 | pm_runtime_mark_last_busy(dev->dev); |
| 761 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 762 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 763 | mei_io_cb_free(cb); |
| 764 | return rets; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 769 | * mei_cl_is_other_connecting - checks if other |
| 770 | * client with the same me client id is connecting |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 771 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 772 | * @cl: private data of the file object |
| 773 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 774 | * Return: true if other client is connected, false - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 775 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 776 | bool mei_cl_is_other_connecting(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 777 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 778 | struct mei_device *dev; |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 779 | struct mei_cl *ocl; /* the other client */ |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 780 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 781 | if (WARN_ON(!cl || !cl->dev)) |
| 782 | return false; |
| 783 | |
| 784 | dev = cl->dev; |
| 785 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 786 | list_for_each_entry(ocl, &dev->file_list, link) { |
| 787 | if (ocl->state == MEI_FILE_CONNECTING && |
| 788 | ocl != cl && |
| 789 | cl->me_client_id == ocl->me_client_id) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 790 | return true; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 791 | |
| 792 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 793 | |
| 794 | return false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 798 | * mei_cl_connect - connect host client to the me one |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 799 | * |
| 800 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 801 | * @file: pointer to file structure |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 802 | * |
| 803 | * Locking: called under "dev->device_lock" lock |
| 804 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 805 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 806 | */ |
| 807 | int mei_cl_connect(struct mei_cl *cl, struct file *file) |
| 808 | { |
| 809 | struct mei_device *dev; |
| 810 | struct mei_cl_cb *cb; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 811 | int rets; |
| 812 | |
| 813 | if (WARN_ON(!cl || !cl->dev)) |
| 814 | return -ENODEV; |
| 815 | |
| 816 | dev = cl->dev; |
| 817 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 818 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 819 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 820 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 821 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 822 | return rets; |
| 823 | } |
| 824 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 825 | cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file); |
| 826 | rets = cb ? 0 : -ENOMEM; |
| 827 | if (rets) |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 828 | goto out; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 829 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 830 | /* run hbuf acquire last so we don't have to undo */ |
| 831 | if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) { |
Alexander Usyskin | e4d8270 | 2014-04-27 15:42:21 +0300 | [diff] [blame] | 832 | cl->state = MEI_FILE_CONNECTING; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 833 | if (mei_hbm_cl_connect_req(dev, cl)) { |
| 834 | rets = -ENODEV; |
| 835 | goto out; |
| 836 | } |
| 837 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
| 838 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); |
| 839 | } else { |
Alexander Usyskin | 73ab423 | 2014-08-12 18:07:56 +0300 | [diff] [blame] | 840 | cl->state = MEI_FILE_INITIALIZING; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 841 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 842 | } |
| 843 | |
| 844 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 12f45ed | 2014-08-21 14:29:18 +0300 | [diff] [blame] | 845 | wait_event_timeout(cl->wait, |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 846 | (cl->state == MEI_FILE_CONNECTED || |
| 847 | cl->state == MEI_FILE_DISCONNECTED), |
| 848 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 849 | mutex_lock(&dev->device_lock); |
| 850 | |
| 851 | if (cl->state != MEI_FILE_CONNECTED) { |
Alexander Usyskin | 3e37ebb | 2014-07-17 10:53:34 +0300 | [diff] [blame] | 852 | cl->state = MEI_FILE_DISCONNECTED; |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 853 | /* something went really wrong */ |
| 854 | if (!cl->status) |
| 855 | cl->status = -EFAULT; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 856 | |
| 857 | mei_io_list_flush(&dev->ctrl_rd_list, cl); |
| 858 | mei_io_list_flush(&dev->ctrl_wr_list, cl); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | rets = cl->status; |
| 862 | |
| 863 | out: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 864 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 865 | pm_runtime_mark_last_busy(dev->dev); |
| 866 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 867 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 868 | mei_io_cb_free(cb); |
| 869 | return rets; |
| 870 | } |
| 871 | |
| 872 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 873 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 874 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 875 | * @cl: private data of the file object |
| 876 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 877 | * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 878 | * -ENOENT if mei_cl is not present |
| 879 | * -EINVAL if single_recv_buf == 0 |
| 880 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 881 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 882 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 883 | struct mei_device *dev; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 884 | struct mei_me_client *me_cl; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 885 | int rets = 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 886 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 887 | if (WARN_ON(!cl || !cl->dev)) |
| 888 | return -EINVAL; |
| 889 | |
| 890 | dev = cl->dev; |
| 891 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 892 | if (cl->mei_flow_ctrl_creds > 0) |
| 893 | return 1; |
| 894 | |
Tomas Winkler | 2e5df41 | 2014-12-07 16:40:14 +0200 | [diff] [blame] | 895 | 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] | 896 | if (!me_cl) { |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 897 | 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] | 898 | return -ENOENT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 899 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 900 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 901 | if (me_cl->mei_flow_ctrl_creds > 0) { |
| 902 | rets = 1; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 903 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 904 | rets = -EINVAL; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 905 | } |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 906 | mei_me_cl_put(me_cl); |
| 907 | return rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 911 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 912 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 913 | * @cl: private data of the file object |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 914 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 915 | * Return: |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 916 | * 0 on success |
| 917 | * -ENOENT when me client is not found |
| 918 | * -EINVAL when ctrl credits are <= 0 |
| 919 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 920 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 921 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 922 | struct mei_device *dev; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 923 | struct mei_me_client *me_cl; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 924 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 925 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 926 | if (WARN_ON(!cl || !cl->dev)) |
| 927 | return -EINVAL; |
| 928 | |
| 929 | dev = cl->dev; |
| 930 | |
Tomas Winkler | 2e5df41 | 2014-12-07 16:40:14 +0200 | [diff] [blame] | 931 | 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] | 932 | if (!me_cl) { |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 933 | 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] | 934 | return -ENOENT; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 935 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 936 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 937 | if (me_cl->props.single_recv_buf) { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 938 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) { |
| 939 | rets = -EINVAL; |
| 940 | goto out; |
| 941 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 942 | me_cl->mei_flow_ctrl_creds--; |
| 943 | } else { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 944 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) { |
| 945 | rets = -EINVAL; |
| 946 | goto out; |
| 947 | } |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 948 | cl->mei_flow_ctrl_creds--; |
| 949 | } |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 950 | rets = 0; |
| 951 | out: |
| 952 | mei_me_cl_put(me_cl); |
| 953 | return rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 956 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 957 | * mei_cl_read_start - the start read client message function. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 958 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 959 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 960 | * @length: number of bytes to read |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 961 | * @fp: pointer to file structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 962 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 963 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 964 | */ |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 965 | int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 966 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 967 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 968 | struct mei_cl_cb *cb; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 969 | struct mei_me_client *me_cl; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 970 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 971 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 972 | if (WARN_ON(!cl || !cl->dev)) |
| 973 | return -ENODEV; |
| 974 | |
| 975 | dev = cl->dev; |
| 976 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 977 | if (!mei_cl_is_connected(cl)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 978 | return -ENODEV; |
| 979 | |
Tomas Winkler | d91aaed | 2013-01-08 23:07:18 +0200 | [diff] [blame] | 980 | if (cl->read_cb) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 981 | cl_dbg(dev, cl, "read is pending.\n"); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 982 | return -EBUSY; |
| 983 | } |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 984 | 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] | 985 | if (!me_cl) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 986 | 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] | 987 | return -ENOTTY; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 988 | } |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 989 | /* always allocate at least client max message */ |
| 990 | length = max_t(size_t, length, me_cl->props.max_msg_length); |
| 991 | mei_me_cl_put(me_cl); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 992 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 993 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 994 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 995 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 996 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 997 | return rets; |
| 998 | } |
| 999 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame^] | 1000 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp); |
| 1001 | rets = cb ? 0 : -ENOMEM; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1002 | if (rets) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1003 | goto out; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1004 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1005 | if (mei_hbuf_acquire(dev)) { |
Alexander Usyskin | 8611350 | 2014-03-31 17:59:24 +0300 | [diff] [blame] | 1006 | rets = mei_hbm_cl_flow_control_req(dev, cl); |
| 1007 | if (rets < 0) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1008 | goto out; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1009 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1010 | list_add_tail(&cb->list, &dev->read_list.list); |
| 1011 | } else { |
| 1012 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
| 1013 | } |
Chao Bi | accb884 | 2014-02-12 21:27:25 +0200 | [diff] [blame] | 1014 | |
| 1015 | cl->read_cb = cb; |
| 1016 | |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1017 | out: |
| 1018 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1019 | pm_runtime_mark_last_busy(dev->dev); |
| 1020 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1021 | |
| 1022 | if (rets) |
| 1023 | mei_io_cb_free(cb); |
| 1024 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1025 | return rets; |
| 1026 | } |
| 1027 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1028 | /** |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1029 | * mei_cl_irq_write - write a message to device |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1030 | * from the interrupt thread context |
| 1031 | * |
| 1032 | * @cl: client |
| 1033 | * @cb: callback block. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1034 | * @cmpl_list: complete list. |
| 1035 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1036 | * Return: 0, OK; otherwise error. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1037 | */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1038 | int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 1039 | struct mei_cl_cb *cmpl_list) |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1040 | { |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1041 | struct mei_device *dev; |
| 1042 | struct mei_msg_data *buf; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1043 | struct mei_msg_hdr mei_hdr; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1044 | size_t len; |
| 1045 | u32 msg_slots; |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1046 | int slots; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1047 | int rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1048 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1049 | if (WARN_ON(!cl || !cl->dev)) |
| 1050 | return -ENODEV; |
| 1051 | |
| 1052 | dev = cl->dev; |
| 1053 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 1054 | buf = &cb->buf; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1055 | |
| 1056 | rets = mei_cl_flow_ctrl_creds(cl); |
| 1057 | if (rets < 0) |
| 1058 | return rets; |
| 1059 | |
| 1060 | if (rets == 0) { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1061 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
| 1064 | |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1065 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1066 | len = buf->size - cb->buf_idx; |
| 1067 | msg_slots = mei_data2slots(len); |
| 1068 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1069 | mei_hdr.host_addr = cl->host_client_id; |
| 1070 | mei_hdr.me_addr = cl->me_client_id; |
| 1071 | mei_hdr.reserved = 0; |
Tomas Winkler | 479327f | 2013-12-17 15:56:56 +0200 | [diff] [blame] | 1072 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1073 | |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1074 | if (slots >= msg_slots) { |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1075 | mei_hdr.length = len; |
| 1076 | mei_hdr.msg_complete = 1; |
| 1077 | /* Split the message only if we can write the whole host buffer */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1078 | } else if (slots == dev->hbuf_depth) { |
| 1079 | msg_slots = slots; |
| 1080 | len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1081 | mei_hdr.length = len; |
| 1082 | mei_hdr.msg_complete = 0; |
| 1083 | } else { |
| 1084 | /* wait for next time the host buffer is empty */ |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 1088 | cl_dbg(dev, cl, "buf: size = %d idx = %lu\n", |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 1089 | cb->buf.size, cb->buf_idx); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1090 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1091 | rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1092 | if (rets) { |
| 1093 | cl->status = rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1094 | list_move_tail(&cb->list, &cmpl_list->list); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1095 | return rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | cl->status = 0; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 1099 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1100 | cb->buf_idx += mei_hdr.length; |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 1101 | cb->completed = mei_hdr.msg_complete == 1; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 1102 | |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1103 | if (mei_hdr.msg_complete) { |
| 1104 | if (mei_cl_flow_ctrl_reduce(cl)) |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1105 | return -EIO; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1106 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1113 | * mei_cl_write - submit a write cb to mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1114 | * assumes device_lock is locked |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1115 | * |
| 1116 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1117 | * @cb: write callback with filled data |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 1118 | * @blocking: block until completed |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1119 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1120 | * Return: number of bytes sent on success, <0 on failure. |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1121 | */ |
| 1122 | int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking) |
| 1123 | { |
| 1124 | struct mei_device *dev; |
| 1125 | struct mei_msg_data *buf; |
| 1126 | struct mei_msg_hdr mei_hdr; |
| 1127 | int rets; |
| 1128 | |
| 1129 | |
| 1130 | if (WARN_ON(!cl || !cl->dev)) |
| 1131 | return -ENODEV; |
| 1132 | |
| 1133 | if (WARN_ON(!cb)) |
| 1134 | return -EINVAL; |
| 1135 | |
| 1136 | dev = cl->dev; |
| 1137 | |
| 1138 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 1139 | buf = &cb->buf; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1140 | |
Alexander Usyskin | 0a01e97 | 2014-09-29 16:31:47 +0300 | [diff] [blame] | 1141 | cl_dbg(dev, cl, "size=%d\n", buf->size); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1142 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1143 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1144 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1145 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1146 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 1147 | return rets; |
| 1148 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1149 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1150 | cb->buf_idx = 0; |
| 1151 | cl->writing_state = MEI_IDLE; |
| 1152 | |
| 1153 | mei_hdr.host_addr = cl->host_client_id; |
| 1154 | mei_hdr.me_addr = cl->me_client_id; |
| 1155 | mei_hdr.reserved = 0; |
| 1156 | mei_hdr.msg_complete = 0; |
| 1157 | mei_hdr.internal = cb->internal; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1158 | |
| 1159 | rets = mei_cl_flow_ctrl_creds(cl); |
| 1160 | if (rets < 0) |
| 1161 | goto err; |
| 1162 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1163 | if (rets == 0) { |
| 1164 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1165 | rets = buf->size; |
| 1166 | goto out; |
| 1167 | } |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1168 | if (!mei_hbuf_acquire(dev)) { |
| 1169 | cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n"); |
| 1170 | rets = buf->size; |
| 1171 | goto out; |
| 1172 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1173 | |
| 1174 | /* Check for a maximum length */ |
| 1175 | if (buf->size > mei_hbuf_max_len(dev)) { |
| 1176 | mei_hdr.length = mei_hbuf_max_len(dev); |
| 1177 | mei_hdr.msg_complete = 0; |
| 1178 | } else { |
| 1179 | mei_hdr.length = buf->size; |
| 1180 | mei_hdr.msg_complete = 1; |
| 1181 | } |
| 1182 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1183 | rets = mei_write_message(dev, &mei_hdr, buf->data); |
| 1184 | if (rets) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1185 | goto err; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1186 | |
| 1187 | cl->writing_state = MEI_WRITING; |
| 1188 | cb->buf_idx = mei_hdr.length; |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 1189 | cb->completed = mei_hdr.msg_complete == 1; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1190 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1191 | out: |
| 1192 | if (mei_hdr.msg_complete) { |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1193 | rets = mei_cl_flow_ctrl_reduce(cl); |
| 1194 | if (rets < 0) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1195 | goto err; |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1196 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1197 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
| 1198 | } else { |
| 1199 | list_add_tail(&cb->list, &dev->write_list.list); |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { |
| 1204 | |
| 1205 | mutex_unlock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1206 | rets = wait_event_interruptible(cl->tx_wait, |
| 1207 | cl->writing_state == MEI_WRITE_COMPLETE); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1208 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1209 | /* wait_event_interruptible returns -ERESTARTSYS */ |
| 1210 | if (rets) { |
| 1211 | if (signal_pending(current)) |
| 1212 | rets = -EINTR; |
| 1213 | goto err; |
| 1214 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1215 | } |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1216 | |
| 1217 | rets = buf->size; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1218 | err: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1219 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1220 | pm_runtime_mark_last_busy(dev->dev); |
| 1221 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1222 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1223 | return rets; |
| 1224 | } |
| 1225 | |
| 1226 | |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 1227 | /** |
| 1228 | * mei_cl_complete - processes completed operation for a client |
| 1229 | * |
| 1230 | * @cl: private data of the file object. |
| 1231 | * @cb: callback block. |
| 1232 | */ |
| 1233 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 1234 | { |
| 1235 | if (cb->fop_type == MEI_FOP_WRITE) { |
| 1236 | mei_io_cb_free(cb); |
| 1237 | cb = NULL; |
| 1238 | cl->writing_state = MEI_WRITE_COMPLETE; |
| 1239 | if (waitqueue_active(&cl->tx_wait)) |
| 1240 | wake_up_interruptible(&cl->tx_wait); |
| 1241 | |
| 1242 | } else if (cb->fop_type == MEI_FOP_READ && |
| 1243 | MEI_READING == cl->reading_state) { |
| 1244 | cl->reading_state = MEI_READ_COMPLETE; |
| 1245 | if (waitqueue_active(&cl->rx_wait)) |
| 1246 | wake_up_interruptible(&cl->rx_wait); |
| 1247 | else |
| 1248 | mei_cl_bus_rx_event(cl); |
| 1249 | |
| 1250 | } |
| 1251 | } |
| 1252 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1253 | |
| 1254 | /** |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1255 | * mei_cl_all_disconnect - disconnect forcefully all connected clients |
| 1256 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1257 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1258 | */ |
| 1259 | |
| 1260 | void mei_cl_all_disconnect(struct mei_device *dev) |
| 1261 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1262 | struct mei_cl *cl; |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1263 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1264 | list_for_each_entry(cl, &dev->file_list, link) { |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1265 | cl->state = MEI_FILE_DISCONNECTED; |
| 1266 | cl->mei_flow_ctrl_creds = 0; |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1267 | cl->timer_count = 0; |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | /** |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1273 | * 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] | 1274 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1275 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1276 | */ |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1277 | void mei_cl_all_wakeup(struct mei_device *dev) |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1278 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1279 | struct mei_cl *cl; |
Tomas Winkler | 92db155 | 2014-09-29 16:31:37 +0300 | [diff] [blame] | 1280 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 1281 | list_for_each_entry(cl, &dev->file_list, link) { |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1282 | if (waitqueue_active(&cl->rx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 1283 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1284 | wake_up_interruptible(&cl->rx_wait); |
| 1285 | } |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1286 | if (waitqueue_active(&cl->tx_wait)) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 1287 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
Tomas Winkler | 5290801 | 2013-07-24 16:22:57 +0300 | [diff] [blame] | 1288 | wake_up_interruptible(&cl->tx_wait); |
| 1289 | } |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * mei_cl_all_write_clear - clear all pending writes |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1295 | * |
| 1296 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1297 | */ |
| 1298 | void mei_cl_all_write_clear(struct mei_device *dev) |
| 1299 | { |
Tomas Winkler | cc99ecf | 2014-03-10 15:10:40 +0200 | [diff] [blame] | 1300 | mei_io_list_free(&dev->write_list, NULL); |
| 1301 | mei_io_list_free(&dev->write_waiting_list, NULL); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |