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