Tomas Winkler | 9fff042 | 2019-03-12 00:10:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 2 | /* |
Tomas Winkler | 4b40b22 | 2020-07-23 17:59:25 +0300 | [diff] [blame] | 3 | * Copyright (c) 2003-2020, Intel Corporation. All rights reserved. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 4 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 7 | #include <linux/sched/signal.h> |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 8 | #include <linux/wait.h> |
| 9 | #include <linux/delay.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 10 | #include <linux/slab.h> |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 11 | #include <linux/pm_runtime.h> |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 13 | |
| 14 | #include <linux/mei.h> |
| 15 | |
| 16 | #include "mei_dev.h" |
| 17 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 18 | #include "client.h" |
| 19 | |
| 20 | /** |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 21 | * mei_me_cl_init - initialize me client |
| 22 | * |
| 23 | * @me_cl: me client |
| 24 | */ |
| 25 | void mei_me_cl_init(struct mei_me_client *me_cl) |
| 26 | { |
| 27 | INIT_LIST_HEAD(&me_cl->list); |
| 28 | kref_init(&me_cl->refcnt); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * mei_me_cl_get - increases me client refcount |
| 33 | * |
| 34 | * @me_cl: me client |
| 35 | * |
| 36 | * Locking: called under "dev->device_lock" lock |
| 37 | * |
| 38 | * Return: me client or NULL |
| 39 | */ |
| 40 | struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl) |
| 41 | { |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 42 | if (me_cl && kref_get_unless_zero(&me_cl->refcnt)) |
| 43 | return me_cl; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 44 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 45 | return NULL; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 49 | * mei_me_cl_release - free me client |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 50 | * |
| 51 | * Locking: called under "dev->device_lock" lock |
| 52 | * |
| 53 | * @ref: me_client refcount |
| 54 | */ |
| 55 | static void mei_me_cl_release(struct kref *ref) |
| 56 | { |
| 57 | struct mei_me_client *me_cl = |
| 58 | container_of(ref, struct mei_me_client, refcnt); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 59 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 60 | kfree(me_cl); |
| 61 | } |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 62 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 63 | /** |
| 64 | * mei_me_cl_put - decrease me client refcount and free client if necessary |
| 65 | * |
| 66 | * Locking: called under "dev->device_lock" lock |
| 67 | * |
| 68 | * @me_cl: me client |
| 69 | */ |
| 70 | void mei_me_cl_put(struct mei_me_client *me_cl) |
| 71 | { |
| 72 | if (me_cl) |
| 73 | kref_put(&me_cl->refcnt, mei_me_cl_release); |
| 74 | } |
| 75 | |
| 76 | /** |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 77 | * __mei_me_cl_del - delete me client from the list and decrease |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 78 | * reference counter |
| 79 | * |
| 80 | * @dev: mei device |
| 81 | * @me_cl: me client |
| 82 | * |
| 83 | * Locking: dev->me_clients_rwsem |
| 84 | */ |
| 85 | static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl) |
| 86 | { |
| 87 | if (!me_cl) |
| 88 | return; |
| 89 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 90 | list_del_init(&me_cl->list); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 91 | mei_me_cl_put(me_cl); |
| 92 | } |
| 93 | |
| 94 | /** |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 95 | * mei_me_cl_del - delete me client from the list and decrease |
| 96 | * reference counter |
| 97 | * |
| 98 | * @dev: mei device |
| 99 | * @me_cl: me client |
| 100 | */ |
| 101 | void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl) |
| 102 | { |
| 103 | down_write(&dev->me_clients_rwsem); |
| 104 | __mei_me_cl_del(dev, me_cl); |
| 105 | up_write(&dev->me_clients_rwsem); |
| 106 | } |
| 107 | |
| 108 | /** |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 109 | * mei_me_cl_add - add me client to the list |
| 110 | * |
| 111 | * @dev: mei device |
| 112 | * @me_cl: me client |
| 113 | */ |
| 114 | void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl) |
| 115 | { |
| 116 | down_write(&dev->me_clients_rwsem); |
| 117 | list_add(&me_cl->list, &dev->me_clients); |
| 118 | up_write(&dev->me_clients_rwsem); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * __mei_me_cl_by_uuid - locate me client by uuid |
| 123 | * increases ref count |
| 124 | * |
| 125 | * @dev: mei device |
| 126 | * @uuid: me client uuid |
| 127 | * |
| 128 | * Return: me client or NULL if not found |
| 129 | * |
| 130 | * Locking: dev->me_clients_rwsem |
| 131 | */ |
| 132 | static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev, |
| 133 | const uuid_le *uuid) |
| 134 | { |
| 135 | struct mei_me_client *me_cl; |
| 136 | const uuid_le *pn; |
| 137 | |
| 138 | WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem)); |
| 139 | |
| 140 | list_for_each_entry(me_cl, &dev->me_clients, list) { |
| 141 | pn = &me_cl->props.protocol_name; |
| 142 | if (uuid_le_cmp(*uuid, *pn) == 0) |
| 143 | return mei_me_cl_get(me_cl); |
| 144 | } |
| 145 | |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 150 | * mei_me_cl_by_uuid - locate me client by uuid |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 151 | * increases ref count |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 152 | * |
| 153 | * @dev: mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 154 | * @uuid: me client uuid |
Alexander Usyskin | a27a76d | 2014-02-17 15:13:22 +0200 | [diff] [blame] | 155 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 156 | * Return: me client or NULL if not found |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 157 | * |
| 158 | * Locking: dev->me_clients_rwsem |
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 | 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] | 161 | const uuid_le *uuid) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 162 | { |
Tomas Winkler | 5ca2d38 | 2014-08-21 14:29:13 +0300 | [diff] [blame] | 163 | struct mei_me_client *me_cl; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 164 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 165 | down_read(&dev->me_clients_rwsem); |
| 166 | me_cl = __mei_me_cl_by_uuid(dev, uuid); |
| 167 | up_read(&dev->me_clients_rwsem); |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 168 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 169 | return me_cl; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 172 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 173 | * mei_me_cl_by_id - locate me client by client id |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 174 | * increases ref count |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 175 | * |
| 176 | * @dev: the device structure |
| 177 | * @client_id: me client id |
| 178 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 179 | * Return: me client or NULL if not found |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 180 | * |
| 181 | * Locking: dev->me_clients_rwsem |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 182 | */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 183 | 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] | 184 | { |
Alexander Usyskin | a27a76d | 2014-02-17 15:13:22 +0200 | [diff] [blame] | 185 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 186 | struct mei_me_client *__me_cl, *me_cl = NULL; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 187 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 188 | down_read(&dev->me_clients_rwsem); |
| 189 | list_for_each_entry(__me_cl, &dev->me_clients, list) { |
| 190 | if (__me_cl->client_id == client_id) { |
| 191 | me_cl = mei_me_cl_get(__me_cl); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | up_read(&dev->me_clients_rwsem); |
| 196 | |
| 197 | return me_cl; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * __mei_me_cl_by_uuid_id - locate me client by client id and uuid |
| 202 | * increases ref count |
| 203 | * |
| 204 | * @dev: the device structure |
| 205 | * @uuid: me client uuid |
| 206 | * @client_id: me client id |
| 207 | * |
| 208 | * Return: me client or null if not found |
| 209 | * |
| 210 | * Locking: dev->me_clients_rwsem |
| 211 | */ |
| 212 | static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev, |
| 213 | const uuid_le *uuid, u8 client_id) |
| 214 | { |
| 215 | struct mei_me_client *me_cl; |
| 216 | const uuid_le *pn; |
| 217 | |
| 218 | WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem)); |
| 219 | |
| 220 | list_for_each_entry(me_cl, &dev->me_clients, list) { |
| 221 | pn = &me_cl->props.protocol_name; |
| 222 | if (uuid_le_cmp(*uuid, *pn) == 0 && |
| 223 | me_cl->client_id == client_id) |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 224 | return mei_me_cl_get(me_cl); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 225 | } |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 226 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 227 | return NULL; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 228 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 229 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 230 | |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 231 | /** |
| 232 | * 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] | 233 | * increases ref count |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 234 | * |
| 235 | * @dev: the device structure |
| 236 | * @uuid: me client uuid |
| 237 | * @client_id: me client id |
| 238 | * |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 239 | * Return: me client or null if not found |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 240 | */ |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 241 | struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev, |
| 242 | const uuid_le *uuid, u8 client_id) |
| 243 | { |
| 244 | struct mei_me_client *me_cl; |
| 245 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 246 | down_read(&dev->me_clients_rwsem); |
| 247 | me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id); |
| 248 | up_read(&dev->me_clients_rwsem); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 249 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 250 | return me_cl; |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 251 | } |
| 252 | |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 253 | /** |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 254 | * mei_me_cl_rm_by_uuid - remove all me clients matching uuid |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 255 | * |
| 256 | * @dev: the device structure |
| 257 | * @uuid: me client uuid |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 258 | * |
| 259 | * Locking: called under "dev->device_lock" lock |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 260 | */ |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 261 | 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] | 262 | { |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 263 | struct mei_me_client *me_cl; |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 264 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 265 | dev_dbg(dev->dev, "remove %pUl\n", uuid); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 266 | |
| 267 | down_write(&dev->me_clients_rwsem); |
| 268 | me_cl = __mei_me_cl_by_uuid(dev, uuid); |
| 269 | __mei_me_cl_del(dev, me_cl); |
Alexander Usyskin | fc9c03c | 2020-05-13 01:31:40 +0300 | [diff] [blame] | 270 | mei_me_cl_put(me_cl); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 271 | up_write(&dev->me_clients_rwsem); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id |
| 276 | * |
| 277 | * @dev: the device structure |
| 278 | * @uuid: me client uuid |
| 279 | * @id: me client id |
| 280 | * |
| 281 | * Locking: called under "dev->device_lock" lock |
| 282 | */ |
| 283 | void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id) |
| 284 | { |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 285 | struct mei_me_client *me_cl; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 286 | |
| 287 | dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 288 | |
| 289 | down_write(&dev->me_clients_rwsem); |
| 290 | me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id); |
| 291 | __mei_me_cl_del(dev, me_cl); |
Alexander Usyskin | fc9c03c | 2020-05-13 01:31:40 +0300 | [diff] [blame] | 292 | mei_me_cl_put(me_cl); |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 293 | up_write(&dev->me_clients_rwsem); |
Tomas Winkler | 25ca647 | 2014-08-21 14:29:14 +0300 | [diff] [blame] | 294 | } |
| 295 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 296 | /** |
| 297 | * mei_me_cl_rm_all - remove all me clients |
| 298 | * |
| 299 | * @dev: the device structure |
| 300 | * |
| 301 | * Locking: called under "dev->device_lock" lock |
| 302 | */ |
| 303 | void mei_me_cl_rm_all(struct mei_device *dev) |
| 304 | { |
| 305 | struct mei_me_client *me_cl, *next; |
| 306 | |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 307 | down_write(&dev->me_clients_rwsem); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 308 | list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) |
Tomas Winkler | b7d8851 | 2015-02-10 10:39:31 +0200 | [diff] [blame] | 309 | __mei_me_cl_del(dev, me_cl); |
| 310 | up_write(&dev->me_clients_rwsem); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 313 | /** |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 314 | * mei_io_cb_free - free mei_cb_private related memory |
| 315 | * |
| 316 | * @cb: mei callback struct |
| 317 | */ |
| 318 | void mei_io_cb_free(struct mei_cl_cb *cb) |
| 319 | { |
| 320 | if (cb == NULL) |
| 321 | return; |
| 322 | |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 323 | list_del(&cb->list); |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 324 | kfree(cb->buf.data); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 325 | kfree(cb); |
| 326 | } |
| 327 | |
| 328 | /** |
Tamar Mashiah | 09f8c33 | 2021-06-21 22:37:55 +0300 | [diff] [blame] | 329 | * mei_tx_cb_enqueue - queue tx callback |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 330 | * |
| 331 | * Locking: called under "dev->device_lock" lock |
| 332 | * |
| 333 | * @cb: mei callback struct |
| 334 | * @head: an instance of list to queue on |
| 335 | */ |
| 336 | static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb, |
| 337 | struct list_head *head) |
| 338 | { |
| 339 | list_add_tail(&cb->list, head); |
| 340 | cb->cl->tx_cb_queued++; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * mei_tx_cb_dequeue - dequeue tx callback |
| 345 | * |
| 346 | * Locking: called under "dev->device_lock" lock |
| 347 | * |
| 348 | * @cb: mei callback struct to dequeue and free |
| 349 | */ |
| 350 | static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb) |
| 351 | { |
| 352 | if (!WARN_ON(cb->cl->tx_cb_queued == 0)) |
| 353 | cb->cl->tx_cb_queued--; |
| 354 | |
| 355 | mei_io_cb_free(cb); |
| 356 | } |
| 357 | |
| 358 | /** |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 359 | * mei_cl_set_read_by_fp - set pending_read flag to vtag struct for given fp |
| 360 | * |
| 361 | * Locking: called under "dev->device_lock" lock |
| 362 | * |
| 363 | * @cl: mei client |
| 364 | * @fp: pointer to file structure |
| 365 | */ |
| 366 | static void mei_cl_set_read_by_fp(const struct mei_cl *cl, |
| 367 | const struct file *fp) |
| 368 | { |
| 369 | struct mei_cl_vtag *cl_vtag; |
| 370 | |
| 371 | list_for_each_entry(cl_vtag, &cl->vtag_map, list) { |
| 372 | if (cl_vtag->fp == fp) { |
| 373 | cl_vtag->pending_read = true; |
| 374 | return; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 380 | * mei_io_cb_init - allocate and initialize io callback |
| 381 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 382 | * @cl: mei client |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 383 | * @type: operation type |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 384 | * @fp: pointer to file structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 385 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 386 | * Return: mei_cl_cb pointer or NULL; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 387 | */ |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 388 | static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, |
| 389 | enum mei_cb_file_ops type, |
| 390 | const struct file *fp) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 391 | { |
| 392 | struct mei_cl_cb *cb; |
| 393 | |
Tomas Winkler | 4b40b22 | 2020-07-23 17:59:25 +0300 | [diff] [blame] | 394 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 395 | if (!cb) |
| 396 | return NULL; |
| 397 | |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 398 | INIT_LIST_HEAD(&cb->list); |
Tomas Winkler | 62e8e6a | 2016-02-07 23:35:24 +0200 | [diff] [blame] | 399 | cb->fp = fp; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 400 | cb->cl = cl; |
| 401 | cb->buf_idx = 0; |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 402 | cb->fop_type = type; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 403 | cb->vtag = 0; |
| 404 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 405 | return cb; |
| 406 | } |
| 407 | |
| 408 | /** |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 409 | * mei_io_list_flush_cl - removes cbs belonging to the cl. |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 410 | * |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 411 | * @head: an instance of our list structure |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 412 | * @cl: host client |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 413 | */ |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 414 | static void mei_io_list_flush_cl(struct list_head *head, |
| 415 | const struct mei_cl *cl) |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 416 | { |
| 417 | struct mei_cl_cb *cb, *next; |
| 418 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 419 | list_for_each_entry_safe(cb, next, head, list) { |
Alexander Usyskin | cee4c4d6 | 2019-01-30 10:12:26 +0200 | [diff] [blame] | 420 | if (cl == cb->cl) { |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 421 | list_del_init(&cb->list); |
Alexander Usyskin | cee4c4d6 | 2019-01-30 10:12:26 +0200 | [diff] [blame] | 422 | if (cb->fop_type == MEI_FOP_READ) |
| 423 | mei_io_cb_free(cb); |
| 424 | } |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 429 | * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 430 | * |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 431 | * @head: An instance of our list structure |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 432 | * @cl: host client |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 433 | * @fp: file pointer (matching cb file object), may be NULL |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 434 | */ |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 435 | static void mei_io_tx_list_free_cl(struct list_head *head, |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 436 | const struct mei_cl *cl, |
| 437 | const struct file *fp) |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 438 | { |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 439 | struct mei_cl_cb *cb, *next; |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 440 | |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 441 | list_for_each_entry_safe(cb, next, head, list) { |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 442 | if (cl == cb->cl && (!fp || fp == cb->fp)) |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 443 | mei_tx_cb_dequeue(cb); |
| 444 | } |
Tomas Winkler | f046192 | 2017-01-27 16:32:46 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
| 448 | * mei_io_list_free_fp - free cb from a list that matches file pointer |
| 449 | * |
| 450 | * @head: io list |
| 451 | * @fp: file pointer (matching cb file object), may be NULL |
| 452 | */ |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 453 | static void mei_io_list_free_fp(struct list_head *head, const struct file *fp) |
Tomas Winkler | f046192 | 2017-01-27 16:32:46 +0200 | [diff] [blame] | 454 | { |
| 455 | struct mei_cl_cb *cb, *next; |
| 456 | |
| 457 | list_for_each_entry_safe(cb, next, head, list) |
| 458 | if (!fp || fp == cb->fp) |
| 459 | mei_io_cb_free(cb); |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 463 | * mei_cl_free_pending - free pending cb |
| 464 | * |
| 465 | * @cl: host client |
| 466 | */ |
| 467 | static void mei_cl_free_pending(struct mei_cl *cl) |
| 468 | { |
| 469 | struct mei_cl_cb *cb; |
| 470 | |
| 471 | cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list); |
| 472 | mei_io_cb_free(cb); |
| 473 | } |
| 474 | |
| 475 | /** |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 476 | * mei_cl_alloc_cb - a convenient wrapper for allocating read cb |
| 477 | * |
| 478 | * @cl: host client |
| 479 | * @length: size of the buffer |
Alexander Usyskin | 967b274 | 2016-11-11 03:00:09 +0200 | [diff] [blame] | 480 | * @fop_type: operation type |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 481 | * @fp: associated file pointer (might be NULL) |
| 482 | * |
| 483 | * Return: cb on success and NULL on failure |
| 484 | */ |
| 485 | struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length, |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 486 | enum mei_cb_file_ops fop_type, |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 487 | const struct file *fp) |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 488 | { |
| 489 | struct mei_cl_cb *cb; |
| 490 | |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 491 | cb = mei_io_cb_init(cl, fop_type, fp); |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 492 | if (!cb) |
| 493 | return NULL; |
| 494 | |
Alexander Usyskin | aab3b1a | 2016-05-24 16:03:41 -0400 | [diff] [blame] | 495 | if (length == 0) |
| 496 | return cb; |
| 497 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 498 | cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL); |
Alexander Usyskin | aab3b1a | 2016-05-24 16:03:41 -0400 | [diff] [blame] | 499 | if (!cb->buf.data) { |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 500 | mei_io_cb_free(cb); |
| 501 | return NULL; |
| 502 | } |
Alexander Usyskin | aab3b1a | 2016-05-24 16:03:41 -0400 | [diff] [blame] | 503 | cb->buf.size = length; |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 504 | |
| 505 | return cb; |
| 506 | } |
| 507 | |
| 508 | /** |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 509 | * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating |
| 510 | * and enqueuing of the control commands cb |
| 511 | * |
| 512 | * @cl: host client |
| 513 | * @length: size of the buffer |
Alexander Usyskin | 967b274 | 2016-11-11 03:00:09 +0200 | [diff] [blame] | 514 | * @fop_type: operation type |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 515 | * @fp: associated file pointer (might be NULL) |
| 516 | * |
| 517 | * Return: cb on success and NULL on failure |
| 518 | * Locking: called under "dev->device_lock" lock |
| 519 | */ |
| 520 | struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length, |
| 521 | enum mei_cb_file_ops fop_type, |
| 522 | const struct file *fp) |
| 523 | { |
| 524 | struct mei_cl_cb *cb; |
| 525 | |
| 526 | /* for RX always allocate at least client's mtu */ |
| 527 | if (length) |
| 528 | length = max_t(size_t, length, mei_cl_mtu(cl)); |
| 529 | |
| 530 | cb = mei_cl_alloc_cb(cl, length, fop_type, fp); |
| 531 | if (!cb) |
| 532 | return NULL; |
| 533 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 534 | list_add_tail(&cb->list, &cl->dev->ctrl_wr_list); |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 535 | return cb; |
| 536 | } |
| 537 | |
| 538 | /** |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 539 | * mei_cl_read_cb - find this cl's callback in the read list |
| 540 | * for a specific file |
| 541 | * |
| 542 | * @cl: host client |
| 543 | * @fp: file pointer (matching cb file object), may be NULL |
| 544 | * |
| 545 | * Return: cb on success, NULL if cb is not found |
| 546 | */ |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 547 | struct mei_cl_cb *mei_cl_read_cb(struct mei_cl *cl, const struct file *fp) |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 548 | { |
| 549 | struct mei_cl_cb *cb; |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 550 | struct mei_cl_cb *ret_cb = NULL; |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 551 | |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 552 | spin_lock(&cl->rd_completed_lock); |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 553 | list_for_each_entry(cb, &cl->rd_completed, list) |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 554 | if (!fp || fp == cb->fp) { |
| 555 | ret_cb = cb; |
| 556 | break; |
| 557 | } |
| 558 | spin_unlock(&cl->rd_completed_lock); |
| 559 | return ret_cb; |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 563 | * mei_cl_flush_queues - flushes queue lists belonging to cl. |
| 564 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 565 | * @cl: host client |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 566 | * @fp: file pointer (matching cb file object), may be NULL |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 567 | * |
| 568 | * Return: 0 on success, -EINVAL if cl or cl->dev is NULL. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 569 | */ |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 570 | 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] | 571 | { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 572 | struct mei_device *dev; |
| 573 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 574 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 575 | return -EINVAL; |
| 576 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 577 | dev = cl->dev; |
| 578 | |
| 579 | cl_dbg(dev, cl, "remove list entry belonging to cl\n"); |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 580 | mei_io_tx_list_free_cl(&cl->dev->write_list, cl, fp); |
| 581 | mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl, fp); |
| 582 | /* free pending and control cb only in final flush */ |
| 583 | if (!fp) { |
| 584 | mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl); |
| 585 | mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl); |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 586 | mei_cl_free_pending(cl); |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 587 | } |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 588 | spin_lock(&cl->rd_completed_lock); |
Tomas Winkler | f046192 | 2017-01-27 16:32:46 +0200 | [diff] [blame] | 589 | mei_io_list_free_fp(&cl->rd_completed, fp); |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 590 | spin_unlock(&cl->rd_completed_lock); |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 591 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 595 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 596 | * mei_cl_init - initializes cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 597 | * |
| 598 | * @cl: host client to be initialized |
| 599 | * @dev: mei device |
| 600 | */ |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 601 | static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 602 | { |
Tomas Winkler | 4b40b22 | 2020-07-23 17:59:25 +0300 | [diff] [blame] | 603 | memset(cl, 0, sizeof(*cl)); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 604 | init_waitqueue_head(&cl->wait); |
| 605 | init_waitqueue_head(&cl->rx_wait); |
| 606 | init_waitqueue_head(&cl->tx_wait); |
Tomas Winkler | b38a362 | 2015-07-26 09:54:19 +0300 | [diff] [blame] | 607 | init_waitqueue_head(&cl->ev_wait); |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 608 | INIT_LIST_HEAD(&cl->vtag_map); |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 609 | spin_lock_init(&cl->rd_completed_lock); |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 610 | INIT_LIST_HEAD(&cl->rd_completed); |
| 611 | INIT_LIST_HEAD(&cl->rd_pending); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 612 | INIT_LIST_HEAD(&cl->link); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 613 | cl->writing_state = MEI_IDLE; |
Alexander Usyskin | bd47b52 | 2016-11-16 22:51:27 +0200 | [diff] [blame] | 614 | cl->state = MEI_FILE_UNINITIALIZED; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 615 | cl->dev = dev; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * mei_cl_allocate - allocates cl structure and sets it up. |
| 620 | * |
| 621 | * @dev: mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 622 | * Return: The allocated file or NULL on failure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 623 | */ |
| 624 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) |
| 625 | { |
| 626 | struct mei_cl *cl; |
| 627 | |
Tomas Winkler | 4b40b22 | 2020-07-23 17:59:25 +0300 | [diff] [blame] | 628 | cl = kmalloc(sizeof(*cl), GFP_KERNEL); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 629 | if (!cl) |
| 630 | return NULL; |
| 631 | |
| 632 | mei_cl_init(cl, dev); |
| 633 | |
| 634 | return cl; |
| 635 | } |
| 636 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 637 | /** |
Alexander Usyskin | 3908be6 | 2015-02-10 10:39:35 +0200 | [diff] [blame] | 638 | * mei_cl_link - allocate host id in the host map |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 639 | * |
Alexander Usyskin | 3908be6 | 2015-02-10 10:39:35 +0200 | [diff] [blame] | 640 | * @cl: host client |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 641 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 642 | * Return: 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 643 | * -EINVAL on incorrect values |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 644 | * -EMFILE if open count exceeded. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 645 | */ |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 646 | int mei_cl_link(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 647 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 648 | struct mei_device *dev; |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 649 | int id; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 650 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 651 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 654 | dev = cl->dev; |
| 655 | |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 656 | id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 657 | if (id >= MEI_CLIENTS_MAX) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 658 | dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 659 | return -EMFILE; |
| 660 | } |
| 661 | |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 662 | if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 663 | dev_err(dev->dev, "open_handle_count exceeded %d", |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 664 | MEI_MAX_OPEN_HANDLE_COUNT); |
| 665 | return -EMFILE; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 668 | dev->open_handle_count++; |
| 669 | |
| 670 | cl->host_client_id = id; |
| 671 | list_add_tail(&cl->link, &dev->file_list); |
| 672 | |
| 673 | set_bit(id, dev->host_clients_map); |
| 674 | |
| 675 | cl->state = MEI_FILE_INITIALIZING; |
| 676 | |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 677 | cl_dbg(dev, cl, "link cl\n"); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 678 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 679 | } |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 680 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 681 | /** |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 682 | * mei_cl_unlink - remove host client from the list |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 683 | * |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 684 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 685 | * |
| 686 | * Return: always 0 |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 687 | */ |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 688 | int mei_cl_unlink(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 689 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 690 | struct mei_device *dev; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 691 | |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 692 | /* don't shout on error exit path */ |
| 693 | if (!cl) |
| 694 | return 0; |
| 695 | |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 696 | if (WARN_ON(!cl->dev)) |
Tomas Winkler | 8e9a4a9 | 2013-01-10 17:32:14 +0200 | [diff] [blame] | 697 | return 0; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 698 | |
| 699 | dev = cl->dev; |
| 700 | |
Tomas Winkler | a14c44d | 2013-09-16 23:44:45 +0300 | [diff] [blame] | 701 | cl_dbg(dev, cl, "unlink client"); |
| 702 | |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 703 | if (dev->open_handle_count > 0) |
| 704 | dev->open_handle_count--; |
| 705 | |
| 706 | /* never clear the 0 bit */ |
| 707 | if (cl->host_client_id) |
| 708 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 709 | |
| 710 | list_del_init(&cl->link); |
| 711 | |
Alexander Usyskin | bd47b52 | 2016-11-16 22:51:27 +0200 | [diff] [blame] | 712 | cl->state = MEI_FILE_UNINITIALIZED; |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 713 | cl->writing_state = MEI_IDLE; |
| 714 | |
| 715 | WARN_ON(!list_empty(&cl->rd_completed) || |
| 716 | !list_empty(&cl->rd_pending) || |
| 717 | !list_empty(&cl->link)); |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 718 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 719 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Alexander Usyskin | 025fb79 | 2016-02-07 23:35:43 +0200 | [diff] [blame] | 722 | void mei_host_client_init(struct mei_device *dev) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 723 | { |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 724 | mei_set_devstate(dev, MEI_DEV_ENABLED); |
Tomas Winkler | 6adb8ef | 2014-01-12 00:36:10 +0200 | [diff] [blame] | 725 | dev->reset_count = 0; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 726 | |
Alexander Usyskin | 025fb79 | 2016-02-07 23:35:43 +0200 | [diff] [blame] | 727 | schedule_work(&dev->bus_rescan_work); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 728 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 729 | pm_runtime_mark_last_busy(dev->dev); |
| 730 | dev_dbg(dev->dev, "rpm: autosuspend\n"); |
Alexander Usyskin | d5f8e16 | 2016-11-24 13:34:02 +0200 | [diff] [blame] | 731 | pm_request_autosuspend(dev->dev); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 732 | } |
| 733 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 734 | /** |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 735 | * mei_hbuf_acquire - try to acquire host buffer |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 736 | * |
| 737 | * @dev: the device structure |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 738 | * Return: true if host buffer was acquired |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 739 | */ |
| 740 | bool mei_hbuf_acquire(struct mei_device *dev) |
| 741 | { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 742 | if (mei_pg_state(dev) == MEI_PG_ON || |
Alexander Usyskin | 3dc196e | 2015-06-13 08:51:17 +0300 | [diff] [blame] | 743 | mei_pg_in_transition(dev)) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 744 | dev_dbg(dev->dev, "device is in pg\n"); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 745 | return false; |
| 746 | } |
| 747 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 748 | if (!dev->hbuf_is_ready) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 749 | dev_dbg(dev->dev, "hbuf is not ready\n"); |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 750 | return false; |
| 751 | } |
| 752 | |
| 753 | dev->hbuf_is_ready = false; |
| 754 | |
| 755 | return true; |
| 756 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 757 | |
| 758 | /** |
Alexander Usyskin | a4307fe | 2016-02-07 23:35:35 +0200 | [diff] [blame] | 759 | * mei_cl_wake_all - wake up readers, writers and event waiters so |
| 760 | * they can be interrupted |
| 761 | * |
| 762 | * @cl: host client |
| 763 | */ |
| 764 | static void mei_cl_wake_all(struct mei_cl *cl) |
| 765 | { |
| 766 | struct mei_device *dev = cl->dev; |
| 767 | |
| 768 | /* synchronized under device mutex */ |
| 769 | if (waitqueue_active(&cl->rx_wait)) { |
| 770 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
| 771 | wake_up_interruptible(&cl->rx_wait); |
| 772 | } |
| 773 | /* synchronized under device mutex */ |
| 774 | if (waitqueue_active(&cl->tx_wait)) { |
| 775 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
| 776 | wake_up_interruptible(&cl->tx_wait); |
| 777 | } |
| 778 | /* synchronized under device mutex */ |
| 779 | if (waitqueue_active(&cl->ev_wait)) { |
| 780 | cl_dbg(dev, cl, "Waking up waiting for event clients!\n"); |
| 781 | wake_up_interruptible(&cl->ev_wait); |
| 782 | } |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 783 | /* synchronized under device mutex */ |
| 784 | if (waitqueue_active(&cl->wait)) { |
| 785 | cl_dbg(dev, cl, "Waking up ctrl write clients!\n"); |
Alexander Usyskin | 69f1804 | 2016-05-09 00:07:46 -0400 | [diff] [blame] | 786 | wake_up(&cl->wait); |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 787 | } |
Alexander Usyskin | a4307fe | 2016-02-07 23:35:35 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 791 | * mei_cl_set_disconnected - set disconnected state and clear |
| 792 | * associated states and resources |
| 793 | * |
| 794 | * @cl: host client |
| 795 | */ |
Alexander Usyskin | 669c256 | 2017-01-20 02:17:17 +0200 | [diff] [blame] | 796 | static void mei_cl_set_disconnected(struct mei_cl *cl) |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 797 | { |
| 798 | struct mei_device *dev = cl->dev; |
| 799 | |
| 800 | if (cl->state == MEI_FILE_DISCONNECTED || |
Alexander Usyskin | bd47b52 | 2016-11-16 22:51:27 +0200 | [diff] [blame] | 801 | cl->state <= MEI_FILE_INITIALIZING) |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 802 | return; |
| 803 | |
| 804 | cl->state = MEI_FILE_DISCONNECTED; |
Alexander Usyskin | 15ffa99 | 2020-08-18 14:51:42 +0300 | [diff] [blame] | 805 | mei_io_tx_list_free_cl(&dev->write_list, cl, NULL); |
| 806 | mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL); |
Tomas Winkler | f046192 | 2017-01-27 16:32:46 +0200 | [diff] [blame] | 807 | mei_io_list_flush_cl(&dev->ctrl_rd_list, cl); |
| 808 | mei_io_list_flush_cl(&dev->ctrl_wr_list, cl); |
Alexander Usyskin | a4307fe | 2016-02-07 23:35:35 +0200 | [diff] [blame] | 809 | mei_cl_wake_all(cl); |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 810 | cl->rx_flow_ctrl_creds = 0; |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 811 | cl->tx_flow_ctrl_creds = 0; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 812 | cl->timer_count = 0; |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 813 | |
Alexander Usyskin | a03d77f6 | 2015-05-04 09:43:55 +0300 | [diff] [blame] | 814 | if (!cl->me_cl) |
| 815 | return; |
| 816 | |
| 817 | if (!WARN_ON(cl->me_cl->connect_count == 0)) |
| 818 | cl->me_cl->connect_count--; |
| 819 | |
Alexander Usyskin | c241e9b | 2015-06-13 21:18:40 +0300 | [diff] [blame] | 820 | if (cl->me_cl->connect_count == 0) |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 821 | cl->me_cl->tx_flow_ctrl_creds = 0; |
Alexander Usyskin | c241e9b | 2015-06-13 21:18:40 +0300 | [diff] [blame] | 822 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 823 | mei_me_cl_put(cl->me_cl); |
| 824 | cl->me_cl = NULL; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 825 | } |
| 826 | |
Alexander Usyskin | a03d77f6 | 2015-05-04 09:43:55 +0300 | [diff] [blame] | 827 | static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl) |
| 828 | { |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 829 | if (!mei_me_cl_get(me_cl)) |
Alexander Usyskin | a03d77f6 | 2015-05-04 09:43:55 +0300 | [diff] [blame] | 830 | return -ENOENT; |
| 831 | |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 832 | /* only one connection is allowed for fixed address clients */ |
| 833 | if (me_cl->props.fixed_address) { |
| 834 | if (me_cl->connect_count) { |
| 835 | mei_me_cl_put(me_cl); |
| 836 | return -EBUSY; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | cl->me_cl = me_cl; |
Alexander Usyskin | a03d77f6 | 2015-05-04 09:43:55 +0300 | [diff] [blame] | 841 | cl->state = MEI_FILE_CONNECTING; |
| 842 | cl->me_cl->connect_count++; |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 847 | /* |
| 848 | * mei_cl_send_disconnect - send disconnect request |
| 849 | * |
| 850 | * @cl: host client |
| 851 | * @cb: callback block |
| 852 | * |
| 853 | * Return: 0, OK; otherwise, error. |
| 854 | */ |
| 855 | static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 856 | { |
| 857 | struct mei_device *dev; |
| 858 | int ret; |
| 859 | |
| 860 | dev = cl->dev; |
| 861 | |
| 862 | ret = mei_hbm_cl_disconnect_req(dev, cl); |
| 863 | cl->status = ret; |
| 864 | if (ret) { |
| 865 | cl->state = MEI_FILE_DISCONNECT_REPLY; |
| 866 | return ret; |
| 867 | } |
| 868 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 869 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 870 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 871 | mei_schedule_stall_timer(dev); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * mei_cl_irq_disconnect - processes close related operation from |
| 878 | * interrupt thread context - send disconnect request |
| 879 | * |
| 880 | * @cl: client |
| 881 | * @cb: callback block. |
| 882 | * @cmpl_list: complete list. |
| 883 | * |
| 884 | * Return: 0, OK; otherwise, error. |
| 885 | */ |
| 886 | int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 887 | struct list_head *cmpl_list) |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 888 | { |
| 889 | struct mei_device *dev = cl->dev; |
| 890 | u32 msg_slots; |
| 891 | int slots; |
| 892 | int ret; |
| 893 | |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 894 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request)); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 895 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 896 | if (slots < 0) |
| 897 | return -EOVERFLOW; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 898 | |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 899 | if ((u32)slots < msg_slots) |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 900 | return -EMSGSIZE; |
| 901 | |
| 902 | ret = mei_cl_send_disconnect(cl, cb); |
| 903 | if (ret) |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 904 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 905 | |
| 906 | return ret; |
| 907 | } |
| 908 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 909 | /** |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 910 | * __mei_cl_disconnect - disconnect host client from the me one |
| 911 | * internal function runtime pm has to be already acquired |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 912 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 913 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 914 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 915 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 916 | */ |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 917 | static int __mei_cl_disconnect(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 918 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 919 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 920 | struct mei_cl_cb *cb; |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 921 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 922 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 923 | dev = cl->dev; |
| 924 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 925 | cl->state = MEI_FILE_DISCONNECTING; |
| 926 | |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 927 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL); |
| 928 | if (!cb) { |
| 929 | rets = -ENOMEM; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 930 | goto out; |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 931 | } |
Tomas Winkler | 5a8373f | 2014-08-21 14:29:17 +0300 | [diff] [blame] | 932 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 933 | if (mei_hbuf_acquire(dev)) { |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 934 | rets = mei_cl_send_disconnect(cl, cb); |
| 935 | if (rets) { |
Alexander Usyskin | c0abffb | 2013-09-15 18:11:07 +0300 | [diff] [blame] | 936 | cl_err(dev, cl, "failed to disconnect.\n"); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 937 | goto out; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 938 | } |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 939 | } |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 940 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 941 | mutex_unlock(&dev->device_lock); |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 942 | wait_event_timeout(cl->wait, |
| 943 | cl->state == MEI_FILE_DISCONNECT_REPLY || |
| 944 | cl->state == MEI_FILE_DISCONNECTED, |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 945 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 946 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 947 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 948 | rets = cl->status; |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 949 | if (cl->state != MEI_FILE_DISCONNECT_REPLY && |
| 950 | cl->state != MEI_FILE_DISCONNECTED) { |
Alexander Usyskin | fe2f17eb3 | 2014-07-17 10:53:38 +0300 | [diff] [blame] | 951 | cl_dbg(dev, cl, "timeout on disconnect from FW client.\n"); |
| 952 | rets = -ETIME; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 955 | out: |
| 956 | /* we disconnect also on error */ |
| 957 | mei_cl_set_disconnected(cl); |
| 958 | if (!rets) |
| 959 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); |
| 960 | |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 961 | mei_io_cb_free(cb); |
| 962 | return rets; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * mei_cl_disconnect - disconnect host client from the me one |
| 967 | * |
| 968 | * @cl: host client |
| 969 | * |
| 970 | * Locking: called under "dev->device_lock" lock |
| 971 | * |
| 972 | * Return: 0 on success, <0 on failure. |
| 973 | */ |
| 974 | int mei_cl_disconnect(struct mei_cl *cl) |
| 975 | { |
| 976 | struct mei_device *dev; |
| 977 | int rets; |
| 978 | |
| 979 | if (WARN_ON(!cl || !cl->dev)) |
| 980 | return -ENODEV; |
| 981 | |
| 982 | dev = cl->dev; |
| 983 | |
| 984 | cl_dbg(dev, cl, "disconnecting"); |
| 985 | |
| 986 | if (!mei_cl_is_connected(cl)) |
| 987 | return 0; |
| 988 | |
| 989 | if (mei_cl_is_fixed_address(cl)) { |
| 990 | mei_cl_set_disconnected(cl); |
| 991 | return 0; |
| 992 | } |
| 993 | |
Alexander Usyskin | 36edb14 | 2021-02-06 16:43:21 +0200 | [diff] [blame] | 994 | if (dev->dev_state == MEI_DEV_POWERING_DOWN || |
| 995 | dev->dev_state == MEI_DEV_POWER_DOWN) { |
Tomas Winkler | 7ae079a | 2018-02-14 14:03:29 +0200 | [diff] [blame] | 996 | cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n"); |
| 997 | mei_cl_set_disconnected(cl); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 1001 | rets = pm_runtime_get(dev->dev); |
| 1002 | if (rets < 0 && rets != -EINPROGRESS) { |
| 1003 | pm_runtime_put_noidle(dev->dev); |
| 1004 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 1005 | return rets; |
| 1006 | } |
| 1007 | |
| 1008 | rets = __mei_cl_disconnect(cl); |
| 1009 | |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1010 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1011 | pm_runtime_mark_last_busy(dev->dev); |
| 1012 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1013 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1014 | return rets; |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | /** |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1019 | * mei_cl_is_other_connecting - checks if other |
| 1020 | * client with the same me client id is connecting |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1021 | * |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1022 | * @cl: private data of the file object |
| 1023 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1024 | * Return: true if other client is connected, false - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1025 | */ |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1026 | static bool mei_cl_is_other_connecting(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1027 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1028 | struct mei_device *dev; |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1029 | struct mei_cl_cb *cb; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1030 | |
| 1031 | dev = cl->dev; |
| 1032 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1033 | list_for_each_entry(cb, &dev->ctrl_rd_list, list) { |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1034 | if (cb->fop_type == MEI_FOP_CONNECT && |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1035 | mei_cl_me_id(cl) == mei_cl_me_id(cb->cl)) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1036 | return true; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1037 | } |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1038 | |
| 1039 | return false; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1043 | * mei_cl_send_connect - send connect request |
| 1044 | * |
| 1045 | * @cl: host client |
| 1046 | * @cb: callback block |
| 1047 | * |
| 1048 | * Return: 0, OK; otherwise, error. |
| 1049 | */ |
| 1050 | static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 1051 | { |
| 1052 | struct mei_device *dev; |
| 1053 | int ret; |
| 1054 | |
| 1055 | dev = cl->dev; |
| 1056 | |
| 1057 | ret = mei_hbm_cl_connect_req(dev, cl); |
| 1058 | cl->status = ret; |
| 1059 | if (ret) { |
| 1060 | cl->state = MEI_FILE_DISCONNECT_REPLY; |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1064 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1065 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 1066 | mei_schedule_stall_timer(dev); |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * mei_cl_irq_connect - send connect request in irq_thread context |
| 1072 | * |
| 1073 | * @cl: host client |
| 1074 | * @cb: callback block |
| 1075 | * @cmpl_list: complete list |
| 1076 | * |
| 1077 | * Return: 0, OK; otherwise, error. |
| 1078 | */ |
| 1079 | int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1080 | struct list_head *cmpl_list) |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1081 | { |
| 1082 | struct mei_device *dev = cl->dev; |
| 1083 | u32 msg_slots; |
| 1084 | int slots; |
| 1085 | int rets; |
| 1086 | |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1087 | if (mei_cl_is_other_connecting(cl)) |
| 1088 | return 0; |
| 1089 | |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 1090 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request)); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 1091 | slots = mei_hbuf_empty_slots(dev); |
| 1092 | if (slots < 0) |
| 1093 | return -EOVERFLOW; |
| 1094 | |
| 1095 | if ((u32)slots < msg_slots) |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1096 | return -EMSGSIZE; |
| 1097 | |
| 1098 | rets = mei_cl_send_connect(cl, cb); |
| 1099 | if (rets) |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1100 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1101 | |
| 1102 | return rets; |
| 1103 | } |
| 1104 | |
| 1105 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 1106 | * mei_cl_connect - connect host client to the me one |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1107 | * |
| 1108 | * @cl: host client |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1109 | * @me_cl: me client |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1110 | * @fp: pointer to file structure |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1111 | * |
| 1112 | * Locking: called under "dev->device_lock" lock |
| 1113 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1114 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1115 | */ |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1116 | int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl, |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1117 | const struct file *fp) |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1118 | { |
| 1119 | struct mei_device *dev; |
| 1120 | struct mei_cl_cb *cb; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1121 | int rets; |
| 1122 | |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1123 | if (WARN_ON(!cl || !cl->dev || !me_cl)) |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1124 | return -ENODEV; |
| 1125 | |
| 1126 | dev = cl->dev; |
| 1127 | |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1128 | rets = mei_cl_set_connecting(cl, me_cl); |
| 1129 | if (rets) |
Alexander Usyskin | 5d88246 | 2017-01-27 16:32:39 +0200 | [diff] [blame] | 1130 | goto nortpm; |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1131 | |
| 1132 | if (mei_cl_is_fixed_address(cl)) { |
| 1133 | cl->state = MEI_FILE_CONNECTED; |
Alexander Usyskin | 5d88246 | 2017-01-27 16:32:39 +0200 | [diff] [blame] | 1134 | rets = 0; |
| 1135 | goto nortpm; |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1136 | } |
| 1137 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1138 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1139 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1140 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1141 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1142 | goto nortpm; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1145 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp); |
| 1146 | if (!cb) { |
| 1147 | rets = -ENOMEM; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1148 | goto out; |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1149 | } |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1150 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1151 | /* run hbuf acquire last so we don't have to undo */ |
| 1152 | if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) { |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1153 | rets = mei_cl_send_connect(cl, cb); |
| 1154 | if (rets) |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1155 | goto out; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 12f45ed | 2014-08-21 14:29:18 +0300 | [diff] [blame] | 1159 | wait_event_timeout(cl->wait, |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 1160 | (cl->state == MEI_FILE_CONNECTED || |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 1161 | cl->state == MEI_FILE_DISCONNECTED || |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 1162 | cl->state == MEI_FILE_DISCONNECT_REQUIRED || |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 1163 | cl->state == MEI_FILE_DISCONNECT_REPLY), |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 1164 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1165 | mutex_lock(&dev->device_lock); |
| 1166 | |
Tomas Winkler | f3de9b6 | 2015-03-27 00:27:58 +0200 | [diff] [blame] | 1167 | if (!mei_cl_is_connected(cl)) { |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 1168 | if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) { |
Tomas Winkler | f046192 | 2017-01-27 16:32:46 +0200 | [diff] [blame] | 1169 | mei_io_list_flush_cl(&dev->ctrl_rd_list, cl); |
| 1170 | mei_io_list_flush_cl(&dev->ctrl_wr_list, cl); |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 1171 | /* ignore disconnect return valuue; |
| 1172 | * in case of failure reset will be invoked |
| 1173 | */ |
| 1174 | __mei_cl_disconnect(cl); |
| 1175 | rets = -EFAULT; |
| 1176 | goto out; |
| 1177 | } |
| 1178 | |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1179 | /* timeout or something went really wrong */ |
Alexander Usyskin | 285e299 | 2014-02-17 15:13:20 +0200 | [diff] [blame] | 1180 | if (!cl->status) |
| 1181 | cl->status = -EFAULT; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | rets = cl->status; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1185 | out: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1186 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1187 | pm_runtime_mark_last_busy(dev->dev); |
| 1188 | pm_runtime_put_autosuspend(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1189 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1190 | mei_io_cb_free(cb); |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1191 | |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1192 | nortpm: |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 1193 | if (!mei_cl_is_connected(cl)) |
| 1194 | mei_cl_set_disconnected(cl); |
| 1195 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 1196 | return rets; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 1200 | * mei_cl_alloc_linked - allocate and link host client |
| 1201 | * |
| 1202 | * @dev: the device structure |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 1203 | * |
| 1204 | * Return: cl on success ERR_PTR on failure |
| 1205 | */ |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 1206 | struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev) |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 1207 | { |
| 1208 | struct mei_cl *cl; |
| 1209 | int ret; |
| 1210 | |
| 1211 | cl = mei_cl_allocate(dev); |
| 1212 | if (!cl) { |
| 1213 | ret = -ENOMEM; |
| 1214 | goto err; |
| 1215 | } |
| 1216 | |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 1217 | ret = mei_cl_link(cl); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 1218 | if (ret) |
| 1219 | goto err; |
| 1220 | |
| 1221 | return cl; |
| 1222 | err: |
| 1223 | kfree(cl); |
| 1224 | return ERR_PTR(ret); |
| 1225 | } |
| 1226 | |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 1227 | /** |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1228 | * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1229 | * |
Alexander Usyskin | 06ee536 | 2016-02-07 23:35:37 +0200 | [diff] [blame] | 1230 | * @cl: host client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1231 | * |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1232 | * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1233 | */ |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1234 | static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1235 | { |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1236 | if (WARN_ON(!cl || !cl->me_cl)) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1237 | return -EINVAL; |
| 1238 | |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1239 | if (cl->tx_flow_ctrl_creds > 0) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1240 | return 1; |
| 1241 | |
Alexander Usyskin | a808c80 | 2016-06-16 17:58:58 +0300 | [diff] [blame] | 1242 | if (mei_cl_is_fixed_address(cl)) |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1243 | return 1; |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1244 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1245 | if (mei_cl_is_single_recv_buf(cl)) { |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1246 | if (cl->me_cl->tx_flow_ctrl_creds > 0) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1247 | return 1; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1248 | } |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1249 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | /** |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1253 | * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits |
| 1254 | * for a client |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1255 | * |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1256 | * @cl: host client |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 1257 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1258 | * Return: |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1259 | * 0 on success |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1260 | * -EINVAL when ctrl credits are <= 0 |
| 1261 | */ |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1262 | static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1263 | { |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1264 | if (WARN_ON(!cl || !cl->me_cl)) |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1265 | return -EINVAL; |
| 1266 | |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1267 | if (mei_cl_is_fixed_address(cl)) |
| 1268 | return 0; |
| 1269 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1270 | if (mei_cl_is_single_recv_buf(cl)) { |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1271 | if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0)) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1272 | return -EINVAL; |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1273 | cl->me_cl->tx_flow_ctrl_creds--; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 1274 | } else { |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1275 | if (WARN_ON(cl->tx_flow_ctrl_creds <= 0)) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1276 | return -EINVAL; |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1277 | cl->tx_flow_ctrl_creds--; |
Alexander Usyskin | 12d0066 | 2014-02-17 15:13:23 +0200 | [diff] [blame] | 1278 | } |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1279 | return 0; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1282 | /** |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1283 | * mei_cl_vtag_alloc - allocate and fill the vtag structure |
| 1284 | * |
| 1285 | * @fp: pointer to file structure |
| 1286 | * @vtag: vm tag |
| 1287 | * |
| 1288 | * Return: |
| 1289 | * * Pointer to allocated struct - on success |
| 1290 | * * ERR_PTR(-ENOMEM) on memory allocation failure |
| 1291 | */ |
| 1292 | struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag) |
| 1293 | { |
| 1294 | struct mei_cl_vtag *cl_vtag; |
| 1295 | |
| 1296 | cl_vtag = kzalloc(sizeof(*cl_vtag), GFP_KERNEL); |
| 1297 | if (!cl_vtag) |
| 1298 | return ERR_PTR(-ENOMEM); |
| 1299 | |
| 1300 | INIT_LIST_HEAD(&cl_vtag->list); |
| 1301 | cl_vtag->vtag = vtag; |
| 1302 | cl_vtag->fp = fp; |
| 1303 | |
| 1304 | return cl_vtag; |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * mei_cl_fp_by_vtag - obtain the file pointer by vtag |
| 1309 | * |
| 1310 | * @cl: host client |
Alexander Usyskin | 85261c1 | 2020-11-16 14:56:11 +0200 | [diff] [blame] | 1311 | * @vtag: virtual tag |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1312 | * |
| 1313 | * Return: |
| 1314 | * * A file pointer - on success |
| 1315 | * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list |
| 1316 | */ |
| 1317 | const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag) |
| 1318 | { |
| 1319 | struct mei_cl_vtag *vtag_l; |
| 1320 | |
| 1321 | list_for_each_entry(vtag_l, &cl->vtag_map, list) |
Alexander Usyskin | 85261c1 | 2020-11-16 14:56:11 +0200 | [diff] [blame] | 1322 | /* The client on bus has one fixed fp */ |
| 1323 | if ((cl->cldev && mei_cldev_enabled(cl->cldev)) || |
| 1324 | vtag_l->vtag == vtag) |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1325 | return vtag_l->fp; |
| 1326 | |
| 1327 | return ERR_PTR(-ENOENT); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag |
| 1332 | * |
| 1333 | * @cl: host client |
| 1334 | * @vtag: vm tag |
| 1335 | */ |
| 1336 | static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag) |
| 1337 | { |
| 1338 | struct mei_cl_vtag *vtag_l; |
| 1339 | |
| 1340 | list_for_each_entry(vtag_l, &cl->vtag_map, list) { |
| 1341 | if (vtag_l->vtag == vtag) { |
| 1342 | vtag_l->pending_read = false; |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * mei_cl_read_vtag_add_fc - add flow control for next pending reader |
| 1350 | * in the vtag list |
| 1351 | * |
| 1352 | * @cl: host client |
| 1353 | */ |
| 1354 | static void mei_cl_read_vtag_add_fc(struct mei_cl *cl) |
| 1355 | { |
| 1356 | struct mei_cl_vtag *cl_vtag; |
| 1357 | |
| 1358 | list_for_each_entry(cl_vtag, &cl->vtag_map, list) { |
| 1359 | if (cl_vtag->pending_read) { |
| 1360 | if (mei_cl_enqueue_ctrl_wr_cb(cl, |
| 1361 | mei_cl_mtu(cl), |
| 1362 | MEI_FOP_READ, |
| 1363 | cl_vtag->fp)) |
| 1364 | cl->rx_flow_ctrl_creds++; |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | /** |
| 1371 | * mei_cl_vt_support_check - check if client support vtags |
| 1372 | * |
| 1373 | * @cl: host client |
| 1374 | * |
| 1375 | * Return: |
| 1376 | * * 0 - supported, or not connected at all |
| 1377 | * * -EOPNOTSUPP - vtags are not supported by client |
| 1378 | */ |
| 1379 | int mei_cl_vt_support_check(const struct mei_cl *cl) |
| 1380 | { |
| 1381 | struct mei_device *dev = cl->dev; |
| 1382 | |
| 1383 | if (!dev->hbm_f_vt_supported) |
| 1384 | return -EOPNOTSUPP; |
| 1385 | |
| 1386 | if (!cl->me_cl) |
| 1387 | return 0; |
| 1388 | |
| 1389 | return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP; |
| 1390 | } |
| 1391 | |
| 1392 | /** |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 1393 | * mei_cl_add_rd_completed - add read completed callback to list with lock |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1394 | * and vtag check |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 1395 | * |
| 1396 | * @cl: host client |
| 1397 | * @cb: callback block |
| 1398 | * |
| 1399 | */ |
| 1400 | void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 1401 | { |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1402 | const struct file *fp; |
| 1403 | |
| 1404 | if (!mei_cl_vt_support_check(cl)) { |
| 1405 | fp = mei_cl_fp_by_vtag(cl, cb->vtag); |
| 1406 | if (IS_ERR(fp)) { |
| 1407 | /* client already disconnected, discarding */ |
| 1408 | mei_io_cb_free(cb); |
| 1409 | return; |
| 1410 | } |
| 1411 | cb->fp = fp; |
| 1412 | mei_cl_reset_read_by_vtag(cl, cb->vtag); |
| 1413 | mei_cl_read_vtag_add_fc(cl); |
| 1414 | } |
| 1415 | |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 1416 | spin_lock(&cl->rd_completed_lock); |
| 1417 | list_add_tail(&cb->list, &cl->rd_completed); |
| 1418 | spin_unlock(&cl->rd_completed_lock); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * mei_cl_del_rd_completed - free read completed callback with lock |
| 1423 | * |
| 1424 | * @cl: host client |
| 1425 | * @cb: callback block |
| 1426 | * |
| 1427 | */ |
| 1428 | void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 1429 | { |
| 1430 | spin_lock(&cl->rd_completed_lock); |
| 1431 | mei_io_cb_free(cb); |
| 1432 | spin_unlock(&cl->rd_completed_lock); |
| 1433 | } |
| 1434 | |
| 1435 | /** |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1436 | * mei_cl_notify_fop2req - convert fop to proper request |
| 1437 | * |
| 1438 | * @fop: client notification start response command |
| 1439 | * |
| 1440 | * Return: MEI_HBM_NOTIFICATION_START/STOP |
| 1441 | */ |
| 1442 | u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop) |
| 1443 | { |
| 1444 | if (fop == MEI_FOP_NOTIFY_START) |
| 1445 | return MEI_HBM_NOTIFICATION_START; |
| 1446 | else |
| 1447 | return MEI_HBM_NOTIFICATION_STOP; |
| 1448 | } |
| 1449 | |
| 1450 | /** |
| 1451 | * mei_cl_notify_req2fop - convert notification request top file operation type |
| 1452 | * |
| 1453 | * @req: hbm notification request type |
| 1454 | * |
| 1455 | * Return: MEI_FOP_NOTIFY_START/STOP |
| 1456 | */ |
| 1457 | enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req) |
| 1458 | { |
| 1459 | if (req == MEI_HBM_NOTIFICATION_START) |
| 1460 | return MEI_FOP_NOTIFY_START; |
| 1461 | else |
| 1462 | return MEI_FOP_NOTIFY_STOP; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * mei_cl_irq_notify - send notification request in irq_thread context |
| 1467 | * |
| 1468 | * @cl: client |
| 1469 | * @cb: callback block. |
| 1470 | * @cmpl_list: complete list. |
| 1471 | * |
| 1472 | * Return: 0 on such and error otherwise. |
| 1473 | */ |
| 1474 | int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1475 | struct list_head *cmpl_list) |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1476 | { |
| 1477 | struct mei_device *dev = cl->dev; |
| 1478 | u32 msg_slots; |
| 1479 | int slots; |
| 1480 | int ret; |
| 1481 | bool request; |
| 1482 | |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 1483 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request)); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1484 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 1485 | if (slots < 0) |
| 1486 | return -EOVERFLOW; |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1487 | |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 1488 | if ((u32)slots < msg_slots) |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1489 | return -EMSGSIZE; |
| 1490 | |
| 1491 | request = mei_cl_notify_fop2req(cb->fop_type); |
| 1492 | ret = mei_hbm_cl_notify_req(dev, cl, request); |
| 1493 | if (ret) { |
| 1494 | cl->status = ret; |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1495 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1496 | return ret; |
| 1497 | } |
| 1498 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1499 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /** |
| 1504 | * mei_cl_notify_request - send notification stop/start request |
| 1505 | * |
| 1506 | * @cl: host client |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1507 | * @fp: associate request with file |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1508 | * @request: 1 for start or 0 for stop |
| 1509 | * |
| 1510 | * Locking: called under "dev->device_lock" lock |
| 1511 | * |
| 1512 | * Return: 0 on such and error otherwise. |
| 1513 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 1514 | int mei_cl_notify_request(struct mei_cl *cl, |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1515 | const struct file *fp, u8 request) |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1516 | { |
| 1517 | struct mei_device *dev; |
| 1518 | struct mei_cl_cb *cb; |
| 1519 | enum mei_cb_file_ops fop_type; |
| 1520 | int rets; |
| 1521 | |
| 1522 | if (WARN_ON(!cl || !cl->dev)) |
| 1523 | return -ENODEV; |
| 1524 | |
| 1525 | dev = cl->dev; |
| 1526 | |
| 1527 | if (!dev->hbm_f_ev_supported) { |
| 1528 | cl_dbg(dev, cl, "notifications not supported\n"); |
| 1529 | return -EOPNOTSUPP; |
| 1530 | } |
| 1531 | |
Alexander Usyskin | 7c47d2c | 2017-01-27 16:32:41 +0200 | [diff] [blame] | 1532 | if (!mei_cl_is_connected(cl)) |
| 1533 | return -ENODEV; |
| 1534 | |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1535 | rets = pm_runtime_get(dev->dev); |
| 1536 | if (rets < 0 && rets != -EINPROGRESS) { |
| 1537 | pm_runtime_put_noidle(dev->dev); |
| 1538 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 1539 | return rets; |
| 1540 | } |
| 1541 | |
| 1542 | fop_type = mei_cl_notify_req2fop(request); |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1543 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1544 | if (!cb) { |
| 1545 | rets = -ENOMEM; |
| 1546 | goto out; |
| 1547 | } |
| 1548 | |
| 1549 | if (mei_hbuf_acquire(dev)) { |
| 1550 | if (mei_hbm_cl_notify_req(dev, cl, request)) { |
| 1551 | rets = -ENODEV; |
| 1552 | goto out; |
| 1553 | } |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1554 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | mutex_unlock(&dev->device_lock); |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 1558 | wait_event_timeout(cl->wait, |
Alexander Usyskin | a19bf05 | 2018-11-06 12:04:40 +0200 | [diff] [blame] | 1559 | cl->notify_en == request || |
| 1560 | cl->status || |
| 1561 | !mei_cl_is_connected(cl), |
Alexander Usyskin | 7ff4bdd | 2016-04-20 11:03:55 -0400 | [diff] [blame] | 1562 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1563 | mutex_lock(&dev->device_lock); |
| 1564 | |
Alexander Usyskin | 4a8eaa9 | 2016-04-20 11:03:54 -0400 | [diff] [blame] | 1565 | if (cl->notify_en != request && !cl->status) |
| 1566 | cl->status = -EFAULT; |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 1567 | |
| 1568 | rets = cl->status; |
| 1569 | |
| 1570 | out: |
| 1571 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
| 1572 | pm_runtime_mark_last_busy(dev->dev); |
| 1573 | pm_runtime_put_autosuspend(dev->dev); |
| 1574 | |
| 1575 | mei_io_cb_free(cb); |
| 1576 | return rets; |
| 1577 | } |
| 1578 | |
| 1579 | /** |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 1580 | * mei_cl_notify - raise notification |
| 1581 | * |
| 1582 | * @cl: host client |
| 1583 | * |
| 1584 | * Locking: called under "dev->device_lock" lock |
| 1585 | */ |
| 1586 | void mei_cl_notify(struct mei_cl *cl) |
| 1587 | { |
| 1588 | struct mei_device *dev; |
| 1589 | |
| 1590 | if (!cl || !cl->dev) |
| 1591 | return; |
| 1592 | |
| 1593 | dev = cl->dev; |
| 1594 | |
| 1595 | if (!cl->notify_en) |
| 1596 | return; |
| 1597 | |
| 1598 | cl_dbg(dev, cl, "notify event"); |
| 1599 | cl->notify_ev = true; |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 1600 | if (!mei_cl_bus_notify_event(cl)) |
| 1601 | wake_up_interruptible(&cl->ev_wait); |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 1602 | |
| 1603 | if (cl->ev_async) |
| 1604 | kill_fasync(&cl->ev_async, SIGIO, POLL_PRI); |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 1605 | |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
Tomas Winkler | b38a362 | 2015-07-26 09:54:19 +0300 | [diff] [blame] | 1609 | * mei_cl_notify_get - get or wait for notification event |
| 1610 | * |
| 1611 | * @cl: host client |
| 1612 | * @block: this request is blocking |
| 1613 | * @notify_ev: true if notification event was received |
| 1614 | * |
| 1615 | * Locking: called under "dev->device_lock" lock |
| 1616 | * |
| 1617 | * Return: 0 on such and error otherwise. |
| 1618 | */ |
| 1619 | int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev) |
| 1620 | { |
| 1621 | struct mei_device *dev; |
| 1622 | int rets; |
| 1623 | |
| 1624 | *notify_ev = false; |
| 1625 | |
| 1626 | if (WARN_ON(!cl || !cl->dev)) |
| 1627 | return -ENODEV; |
| 1628 | |
| 1629 | dev = cl->dev; |
| 1630 | |
Alexander Usyskin | 6c0d670 | 2017-01-27 16:32:42 +0200 | [diff] [blame] | 1631 | if (!dev->hbm_f_ev_supported) { |
| 1632 | cl_dbg(dev, cl, "notifications not supported\n"); |
| 1633 | return -EOPNOTSUPP; |
| 1634 | } |
| 1635 | |
Tomas Winkler | b38a362 | 2015-07-26 09:54:19 +0300 | [diff] [blame] | 1636 | if (!mei_cl_is_connected(cl)) |
| 1637 | return -ENODEV; |
| 1638 | |
| 1639 | if (cl->notify_ev) |
| 1640 | goto out; |
| 1641 | |
| 1642 | if (!block) |
| 1643 | return -EAGAIN; |
| 1644 | |
| 1645 | mutex_unlock(&dev->device_lock); |
| 1646 | rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev); |
| 1647 | mutex_lock(&dev->device_lock); |
| 1648 | |
| 1649 | if (rets < 0) |
| 1650 | return rets; |
| 1651 | |
| 1652 | out: |
| 1653 | *notify_ev = cl->notify_ev; |
| 1654 | cl->notify_ev = false; |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 1659 | * mei_cl_read_start - the start read client message function. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1660 | * |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1661 | * @cl: host client |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 1662 | * @length: number of bytes to read |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 1663 | * @fp: pointer to file structure |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1664 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1665 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1666 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 1667 | int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1668 | { |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1669 | struct mei_device *dev; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1670 | struct mei_cl_cb *cb; |
| 1671 | int rets; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1672 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 1673 | if (WARN_ON(!cl || !cl->dev)) |
| 1674 | return -ENODEV; |
| 1675 | |
| 1676 | dev = cl->dev; |
| 1677 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 1678 | if (!mei_cl_is_connected(cl)) |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1679 | return -ENODEV; |
| 1680 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 1681 | if (!mei_me_cl_is_active(cl->me_cl)) { |
| 1682 | cl_err(dev, cl, "no such me client\n"); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 1683 | return -ENOTTY; |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1684 | } |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1685 | |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 1686 | if (mei_cl_is_fixed_address(cl)) |
Alexander Usyskin | e51dfa5 | 2016-07-26 01:06:02 +0300 | [diff] [blame] | 1687 | return 0; |
| 1688 | |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1689 | /* HW currently supports only one pending read */ |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1690 | if (cl->rx_flow_ctrl_creds) { |
| 1691 | mei_cl_set_read_by_fp(cl, fp); |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1692 | return -EBUSY; |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1693 | } |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1694 | |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 1695 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp); |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1696 | if (!cb) |
| 1697 | return -ENOMEM; |
| 1698 | |
Alexander Usyskin | f35fe5f | 2020-08-18 14:51:41 +0300 | [diff] [blame] | 1699 | mei_cl_set_read_by_fp(cl, fp); |
| 1700 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1701 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1702 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1703 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1704 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1705 | goto nortpm; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1708 | rets = 0; |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1709 | if (mei_hbuf_acquire(dev)) { |
Alexander Usyskin | 8611350 | 2014-03-31 17:59:24 +0300 | [diff] [blame] | 1710 | rets = mei_hbm_cl_flow_control_req(dev, cl); |
| 1711 | if (rets < 0) |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1712 | goto out; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1713 | |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1714 | list_move_tail(&cb->list, &cl->rd_pending); |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1715 | } |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 1716 | cl->rx_flow_ctrl_creds++; |
Chao Bi | accb884 | 2014-02-12 21:27:25 +0200 | [diff] [blame] | 1717 | |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1718 | out: |
| 1719 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1720 | pm_runtime_mark_last_busy(dev->dev); |
| 1721 | pm_runtime_put_autosuspend(dev->dev); |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 1722 | nortpm: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1723 | if (rets) |
| 1724 | mei_io_cb_free(cb); |
| 1725 | |
Tomas Winkler | 9ca9050 | 2013-01-08 23:07:13 +0200 | [diff] [blame] | 1726 | return rets; |
| 1727 | } |
| 1728 | |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 1729 | static inline u8 mei_ext_hdr_set_vtag(void *ext, u8 vtag) |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1730 | { |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 1731 | struct mei_ext_hdr_vtag *vtag_hdr = ext; |
| 1732 | |
| 1733 | vtag_hdr->hdr.type = MEI_EXT_HDR_VTAG; |
| 1734 | vtag_hdr->hdr.length = mei_data2slots(sizeof(*vtag_hdr)); |
| 1735 | vtag_hdr->vtag = vtag; |
| 1736 | vtag_hdr->reserved = 0; |
| 1737 | return vtag_hdr->hdr.length; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /** |
| 1741 | * mei_msg_hdr_init - allocate and initialize mei message header |
| 1742 | * |
| 1743 | * @cb: message callback structure |
| 1744 | * |
Tomas Winkler | 7615da2 | 2021-01-29 14:07:47 +0200 | [diff] [blame] | 1745 | * Return: a pointer to initialized header or ERR_PTR on failure |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1746 | */ |
| 1747 | static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb) |
| 1748 | { |
| 1749 | size_t hdr_len; |
| 1750 | struct mei_ext_meta_hdr *meta; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1751 | struct mei_msg_hdr *mei_hdr; |
| 1752 | bool is_ext, is_vtag; |
| 1753 | |
| 1754 | if (!cb) |
| 1755 | return ERR_PTR(-EINVAL); |
| 1756 | |
| 1757 | /* Extended header for vtag is attached only on the first fragment */ |
| 1758 | is_vtag = (cb->vtag && cb->buf_idx == 0); |
| 1759 | is_ext = is_vtag; |
| 1760 | |
| 1761 | /* Compute extended header size */ |
| 1762 | hdr_len = sizeof(*mei_hdr); |
| 1763 | |
| 1764 | if (!is_ext) |
| 1765 | goto setup_hdr; |
| 1766 | |
| 1767 | hdr_len += sizeof(*meta); |
| 1768 | if (is_vtag) |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 1769 | hdr_len += sizeof(struct mei_ext_hdr_vtag); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1770 | |
| 1771 | setup_hdr: |
| 1772 | mei_hdr = kzalloc(hdr_len, GFP_KERNEL); |
| 1773 | if (!mei_hdr) |
| 1774 | return ERR_PTR(-ENOMEM); |
| 1775 | |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1776 | mei_hdr->host_addr = mei_cl_host_addr(cb->cl); |
| 1777 | mei_hdr->me_addr = mei_cl_me_id(cb->cl); |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1778 | mei_hdr->internal = cb->internal; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1779 | mei_hdr->extended = is_ext; |
| 1780 | |
| 1781 | if (!is_ext) |
| 1782 | goto out; |
| 1783 | |
| 1784 | meta = (struct mei_ext_meta_hdr *)mei_hdr->extension; |
| 1785 | if (is_vtag) { |
| 1786 | meta->count++; |
| 1787 | meta->size += mei_ext_hdr_set_vtag(meta->hdrs, cb->vtag); |
| 1788 | } |
| 1789 | out: |
| 1790 | mei_hdr->length = hdr_len - sizeof(*mei_hdr); |
| 1791 | return mei_hdr; |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /** |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1795 | * mei_cl_irq_write - write a message to device |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1796 | * from the interrupt thread context |
| 1797 | * |
| 1798 | * @cl: client |
| 1799 | * @cb: callback block. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1800 | * @cmpl_list: complete list. |
| 1801 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1802 | * Return: 0, OK; otherwise error. |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1803 | */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 1804 | int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1805 | struct list_head *cmpl_list) |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1806 | { |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1807 | struct mei_device *dev; |
| 1808 | struct mei_msg_data *buf; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1809 | struct mei_msg_hdr *mei_hdr = NULL; |
| 1810 | size_t hdr_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1811 | size_t hbuf_len, dr_len; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1812 | size_t buf_len; |
| 1813 | size_t data_len; |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1814 | int hbuf_slots; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1815 | u32 dr_slots; |
| 1816 | u32 dma_len; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 1817 | int rets; |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 1818 | bool first_chunk; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1819 | const void *data; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1820 | |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1821 | if (WARN_ON(!cl || !cl->dev)) |
| 1822 | return -ENODEV; |
| 1823 | |
| 1824 | dev = cl->dev; |
| 1825 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 1826 | buf = &cb->buf; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1827 | |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 1828 | first_chunk = cb->buf_idx == 0; |
| 1829 | |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1830 | rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1831 | if (rets < 0) |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1832 | goto err; |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1833 | |
| 1834 | if (rets == 0) { |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1835 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 136698e | 2013-09-16 23:44:44 +0300 | [diff] [blame] | 1836 | return 0; |
| 1837 | } |
| 1838 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1839 | buf_len = buf->size - cb->buf_idx; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1840 | data = buf->data + cb->buf_idx; |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1841 | hbuf_slots = mei_hbuf_empty_slots(dev); |
| 1842 | if (hbuf_slots < 0) { |
| 1843 | rets = -EOVERFLOW; |
| 1844 | goto err; |
| 1845 | } |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 1846 | |
Tomas Winkler | 3aef021 | 2020-02-11 18:05:22 +0200 | [diff] [blame] | 1847 | hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1848 | dr_slots = mei_dma_ring_empty_slots(dev); |
| 1849 | dr_len = mei_slots2data(dr_slots); |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1850 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1851 | mei_hdr = mei_msg_hdr_init(cb); |
| 1852 | if (IS_ERR(mei_hdr)) { |
| 1853 | rets = PTR_ERR(mei_hdr); |
| 1854 | mei_hdr = NULL; |
| 1855 | goto err; |
| 1856 | } |
| 1857 | |
| 1858 | cl_dbg(dev, cl, "Extended Header %d vtag = %d\n", |
| 1859 | mei_hdr->extended, cb->vtag); |
| 1860 | |
| 1861 | hdr_len = sizeof(*mei_hdr) + mei_hdr->length; |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1862 | |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1863 | /** |
| 1864 | * Split the message only if we can write the whole host buffer |
| 1865 | * otherwise wait for next time the host buffer is empty. |
| 1866 | */ |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1867 | if (hdr_len + buf_len <= hbuf_len) { |
| 1868 | data_len = buf_len; |
| 1869 | mei_hdr->msg_complete = 1; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1870 | } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1871 | mei_hdr->dma_ring = 1; |
| 1872 | if (buf_len > dr_len) |
| 1873 | buf_len = dr_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1874 | else |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1875 | mei_hdr->msg_complete = 1; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1876 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1877 | data_len = sizeof(dma_len); |
| 1878 | dma_len = buf_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1879 | data = &dma_len; |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1880 | } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1881 | buf_len = hbuf_len - hdr_len; |
| 1882 | data_len = buf_len; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1883 | } else { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1884 | kfree(mei_hdr); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1887 | mei_hdr->length += data_len; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1888 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1889 | if (mei_hdr->dma_ring) |
| 1890 | mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len); |
| 1891 | rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1892 | |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1893 | if (rets) |
| 1894 | goto err; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1895 | |
| 1896 | cl->status = 0; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 1897 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1898 | cb->buf_idx += buf_len; |
Tomas Winkler | 4dfaa9f | 2013-06-23 09:37:00 +0300 | [diff] [blame] | 1899 | |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 1900 | if (first_chunk) { |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1901 | if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) { |
| 1902 | rets = -EIO; |
| 1903 | goto err; |
| 1904 | } |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1905 | } |
| 1906 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1907 | if (mei_hdr->msg_complete) |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1908 | list_move_tail(&cb->list, &dev->write_waiting_list); |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 1909 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1910 | kfree(mei_hdr); |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1911 | return 0; |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1912 | |
| 1913 | err: |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1914 | kfree(mei_hdr); |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1915 | cl->status = rets; |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 1916 | list_move_tail(&cb->list, cmpl_list); |
Alexander Usyskin | e09ee85 | 2016-12-14 17:56:52 +0200 | [diff] [blame] | 1917 | return rets; |
Tomas Winkler | 2176754 | 2013-06-23 09:36:59 +0300 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | /** |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1921 | * mei_cl_write - submit a write cb to mei device |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1922 | * assumes device_lock is locked |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1923 | * |
| 1924 | * @cl: host client |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1925 | * @cb: write callback with filled data |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1926 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 1927 | * Return: number of bytes sent on success, <0 on failure. |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1928 | */ |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 1929 | ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1930 | { |
| 1931 | struct mei_device *dev; |
| 1932 | struct mei_msg_data *buf; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1933 | struct mei_msg_hdr *mei_hdr = NULL; |
| 1934 | size_t hdr_len; |
| 1935 | size_t hbuf_len, dr_len; |
| 1936 | size_t buf_len; |
| 1937 | size_t data_len; |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1938 | int hbuf_slots; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1939 | u32 dr_slots; |
| 1940 | u32 dma_len; |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 1941 | ssize_t rets; |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 1942 | bool blocking; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1943 | const void *data; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1944 | |
| 1945 | if (WARN_ON(!cl || !cl->dev)) |
| 1946 | return -ENODEV; |
| 1947 | |
| 1948 | if (WARN_ON(!cb)) |
| 1949 | return -EINVAL; |
| 1950 | |
| 1951 | dev = cl->dev; |
| 1952 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 1953 | buf = &cb->buf; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1954 | buf_len = buf->size; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1955 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1956 | cl_dbg(dev, cl, "buf_len=%zd\n", buf_len); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1957 | |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 1958 | blocking = cb->blocking; |
| 1959 | data = buf->data; |
| 1960 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1961 | rets = pm_runtime_get(dev->dev); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1962 | if (rets < 0 && rets != -EINPROGRESS) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 1963 | pm_runtime_put_noidle(dev->dev); |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 1964 | cl_err(dev, cl, "rpm: get failed %zd\n", rets); |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 1965 | goto free; |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 1966 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1967 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1968 | cb->buf_idx = 0; |
| 1969 | cl->writing_state = MEI_IDLE; |
| 1970 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1971 | |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 1972 | rets = mei_cl_tx_flow_ctrl_creds(cl); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1973 | if (rets < 0) |
| 1974 | goto err; |
| 1975 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1976 | mei_hdr = mei_msg_hdr_init(cb); |
| 1977 | if (IS_ERR(mei_hdr)) { |
| 1978 | rets = -PTR_ERR(mei_hdr); |
| 1979 | mei_hdr = NULL; |
| 1980 | goto err; |
| 1981 | } |
| 1982 | |
| 1983 | cl_dbg(dev, cl, "Extended Header %d vtag = %d\n", |
| 1984 | mei_hdr->extended, cb->vtag); |
| 1985 | |
| 1986 | hdr_len = sizeof(*mei_hdr) + mei_hdr->length; |
Tomas Winkler | a1c4d08 | 2018-07-23 13:21:24 +0300 | [diff] [blame] | 1987 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1988 | if (rets == 0) { |
| 1989 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1990 | rets = buf_len; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1991 | goto out; |
| 1992 | } |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 1993 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1994 | if (!mei_hbuf_acquire(dev)) { |
| 1995 | cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n"); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 1996 | rets = buf_len; |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 1997 | goto out; |
| 1998 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 1999 | |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 2000 | hbuf_slots = mei_hbuf_empty_slots(dev); |
| 2001 | if (hbuf_slots < 0) { |
| 2002 | rets = -EOVERFLOW; |
| 2003 | goto out; |
| 2004 | } |
| 2005 | |
Tomas Winkler | 3aef021 | 2020-02-11 18:05:22 +0200 | [diff] [blame] | 2006 | hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2007 | dr_slots = mei_dma_ring_empty_slots(dev); |
| 2008 | dr_len = mei_slots2data(dr_slots); |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 2009 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2010 | if (hdr_len + buf_len <= hbuf_len) { |
| 2011 | data_len = buf_len; |
| 2012 | mei_hdr->msg_complete = 1; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2013 | } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2014 | mei_hdr->dma_ring = 1; |
| 2015 | if (buf_len > dr_len) |
| 2016 | buf_len = dr_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2017 | else |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2018 | mei_hdr->msg_complete = 1; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2019 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2020 | data_len = sizeof(dma_len); |
| 2021 | dma_len = buf_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2022 | data = &dma_len; |
Tomas Winkler | 8c8d964 | 2018-07-23 13:21:23 +0300 | [diff] [blame] | 2023 | } else { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2024 | buf_len = hbuf_len - hdr_len; |
| 2025 | data_len = buf_len; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2026 | } |
| 2027 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2028 | mei_hdr->length += data_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2029 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2030 | if (mei_hdr->dma_ring) |
| 2031 | mei_dma_ring_write(dev, buf->data, buf_len); |
| 2032 | rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len); |
| 2033 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 2034 | if (rets) |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2035 | goto err; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2036 | |
Tomas Winkler | 4034b81 | 2016-07-26 01:06:04 +0300 | [diff] [blame] | 2037 | rets = mei_cl_tx_flow_ctrl_creds_reduce(cl); |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 2038 | if (rets) |
| 2039 | goto err; |
| 2040 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2041 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2042 | cb->buf_idx = buf_len; |
Tomas Winkler | c30362c | 2018-11-22 13:11:40 +0200 | [diff] [blame] | 2043 | /* restore return value */ |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2044 | buf_len = buf->size; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2045 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2046 | out: |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2047 | if (mei_hdr->msg_complete) |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 2048 | mei_tx_cb_enqueue(cb, &dev->write_waiting_list); |
Alexander Usyskin | b8b730357 | 2015-05-07 15:53:58 +0300 | [diff] [blame] | 2049 | else |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 2050 | mei_tx_cb_enqueue(cb, &dev->write_list); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2051 | |
Alexander Usyskin | 23253c3 | 2015-07-23 10:43:11 +0300 | [diff] [blame] | 2052 | cb = NULL; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2053 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { |
| 2054 | |
| 2055 | mutex_unlock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 2056 | rets = wait_event_interruptible(cl->tx_wait, |
Alexander Usyskin | 0faf6a3 | 2016-02-07 23:35:34 +0200 | [diff] [blame] | 2057 | cl->writing_state == MEI_WRITE_COMPLETE || |
| 2058 | (!mei_cl_is_connected(cl))); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2059 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 2060 | /* wait_event_interruptible returns -ERESTARTSYS */ |
| 2061 | if (rets) { |
| 2062 | if (signal_pending(current)) |
| 2063 | rets = -EINTR; |
| 2064 | goto err; |
| 2065 | } |
Alexander Usyskin | 0faf6a3 | 2016-02-07 23:35:34 +0200 | [diff] [blame] | 2066 | if (cl->writing_state != MEI_WRITE_COMPLETE) { |
| 2067 | rets = -EFAULT; |
| 2068 | goto err; |
| 2069 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2070 | } |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 2071 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2072 | rets = buf_len; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2073 | err: |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 2074 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 2075 | pm_runtime_mark_last_busy(dev->dev); |
| 2076 | pm_runtime_put_autosuspend(dev->dev); |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 2077 | free: |
| 2078 | mei_io_cb_free(cb); |
Tomas Winkler | 04bb139 | 2014-03-18 22:52:04 +0200 | [diff] [blame] | 2079 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 2080 | kfree(mei_hdr); |
| 2081 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2082 | return rets; |
| 2083 | } |
| 2084 | |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2085 | /** |
| 2086 | * mei_cl_complete - processes completed operation for a client |
| 2087 | * |
| 2088 | * @cl: private data of the file object. |
| 2089 | * @cb: callback block. |
| 2090 | */ |
| 2091 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) |
| 2092 | { |
Alexander Usyskin | a1809d3 | 2015-05-07 15:53:59 +0300 | [diff] [blame] | 2093 | struct mei_device *dev = cl->dev; |
| 2094 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2095 | switch (cb->fop_type) { |
| 2096 | case MEI_FOP_WRITE: |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 2097 | mei_tx_cb_dequeue(cb); |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2098 | cl->writing_state = MEI_WRITE_COMPLETE; |
Alexander Usyskin | a1809d3 | 2015-05-07 15:53:59 +0300 | [diff] [blame] | 2099 | if (waitqueue_active(&cl->tx_wait)) { |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2100 | wake_up_interruptible(&cl->tx_wait); |
Alexander Usyskin | a1809d3 | 2015-05-07 15:53:59 +0300 | [diff] [blame] | 2101 | } else { |
| 2102 | pm_runtime_mark_last_busy(dev->dev); |
| 2103 | pm_request_autosuspend(dev->dev); |
| 2104 | } |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2105 | break; |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2106 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2107 | case MEI_FOP_READ: |
Alexander Usyskin | d1376f3 | 2020-08-18 14:51:40 +0300 | [diff] [blame] | 2108 | mei_cl_add_rd_completed(cl, cb); |
Alexander Usyskin | 46978ad | 2016-07-26 01:06:03 +0300 | [diff] [blame] | 2109 | if (!mei_cl_is_fixed_address(cl) && |
| 2110 | !WARN_ON(!cl->rx_flow_ctrl_creds)) |
| 2111 | cl->rx_flow_ctrl_creds--; |
Tomas Winkler | a1f9ae2 | 2016-02-07 23:35:30 +0200 | [diff] [blame] | 2112 | if (!mei_cl_bus_rx_event(cl)) |
| 2113 | wake_up_interruptible(&cl->rx_wait); |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2114 | break; |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2115 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2116 | case MEI_FOP_CONNECT: |
| 2117 | case MEI_FOP_DISCONNECT: |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 2118 | case MEI_FOP_NOTIFY_STOP: |
| 2119 | case MEI_FOP_NOTIFY_START: |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2120 | case MEI_FOP_DMA_MAP: |
| 2121 | case MEI_FOP_DMA_UNMAP: |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2122 | if (waitqueue_active(&cl->wait)) |
| 2123 | wake_up(&cl->wait); |
| 2124 | |
| 2125 | break; |
Alexander Usyskin | 6a8d648 | 2016-04-17 12:16:03 -0400 | [diff] [blame] | 2126 | case MEI_FOP_DISCONNECT_RSP: |
| 2127 | mei_io_cb_free(cb); |
| 2128 | mei_cl_set_disconnected(cl); |
| 2129 | break; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2130 | default: |
| 2131 | BUG_ON(0); |
Tomas Winkler | db086fa | 2013-05-12 15:34:45 +0300 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 2135 | |
| 2136 | /** |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 2137 | * mei_cl_all_disconnect - disconnect forcefully all connected clients |
| 2138 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 2139 | * @dev: mei device |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 2140 | */ |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 2141 | void mei_cl_all_disconnect(struct mei_device *dev) |
| 2142 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 2143 | struct mei_cl *cl; |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 2144 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 2145 | list_for_each_entry(cl, &dev->file_list, link) |
| 2146 | mei_cl_set_disconnected(cl); |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 2147 | } |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2148 | |
| 2149 | static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id) |
| 2150 | { |
| 2151 | struct mei_cl *cl; |
| 2152 | |
| 2153 | list_for_each_entry(cl, &dev->file_list, link) |
| 2154 | if (cl->dma.buffer_id == buffer_id) |
| 2155 | return cl; |
| 2156 | return NULL; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * mei_cl_irq_dma_map - send client dma map request in irq_thread context |
| 2161 | * |
| 2162 | * @cl: client |
| 2163 | * @cb: callback block. |
| 2164 | * @cmpl_list: complete list. |
| 2165 | * |
| 2166 | * Return: 0 on such and error otherwise. |
| 2167 | */ |
| 2168 | int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 2169 | struct list_head *cmpl_list) |
| 2170 | { |
| 2171 | struct mei_device *dev = cl->dev; |
| 2172 | u32 msg_slots; |
| 2173 | int slots; |
| 2174 | int ret; |
| 2175 | |
| 2176 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request)); |
| 2177 | slots = mei_hbuf_empty_slots(dev); |
| 2178 | if (slots < 0) |
| 2179 | return -EOVERFLOW; |
| 2180 | |
| 2181 | if ((u32)slots < msg_slots) |
| 2182 | return -EMSGSIZE; |
| 2183 | |
| 2184 | ret = mei_hbm_cl_dma_map_req(dev, cl); |
| 2185 | if (ret) { |
| 2186 | cl->status = ret; |
| 2187 | list_move_tail(&cb->list, cmpl_list); |
| 2188 | return ret; |
| 2189 | } |
| 2190 | |
| 2191 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
| 2192 | return 0; |
| 2193 | } |
| 2194 | |
| 2195 | /** |
| 2196 | * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context |
| 2197 | * |
| 2198 | * @cl: client |
| 2199 | * @cb: callback block. |
| 2200 | * @cmpl_list: complete list. |
| 2201 | * |
| 2202 | * Return: 0 on such and error otherwise. |
| 2203 | */ |
| 2204 | int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb, |
| 2205 | struct list_head *cmpl_list) |
| 2206 | { |
| 2207 | struct mei_device *dev = cl->dev; |
| 2208 | u32 msg_slots; |
| 2209 | int slots; |
| 2210 | int ret; |
| 2211 | |
| 2212 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request)); |
| 2213 | slots = mei_hbuf_empty_slots(dev); |
| 2214 | if (slots < 0) |
| 2215 | return -EOVERFLOW; |
| 2216 | |
| 2217 | if ((u32)slots < msg_slots) |
| 2218 | return -EMSGSIZE; |
| 2219 | |
| 2220 | ret = mei_hbm_cl_dma_unmap_req(dev, cl); |
| 2221 | if (ret) { |
| 2222 | cl->status = ret; |
| 2223 | list_move_tail(&cb->list, cmpl_list); |
| 2224 | return ret; |
| 2225 | } |
| 2226 | |
| 2227 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
| 2228 | return 0; |
| 2229 | } |
| 2230 | |
| 2231 | static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size) |
| 2232 | { |
| 2233 | cl->dma.vaddr = dmam_alloc_coherent(cl->dev->dev, size, |
| 2234 | &cl->dma.daddr, GFP_KERNEL); |
| 2235 | if (!cl->dma.vaddr) |
| 2236 | return -ENOMEM; |
| 2237 | |
| 2238 | cl->dma.buffer_id = buf_id; |
| 2239 | cl->dma.size = size; |
| 2240 | |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
| 2244 | static void mei_cl_dma_free(struct mei_cl *cl) |
| 2245 | { |
| 2246 | cl->dma.buffer_id = 0; |
| 2247 | dmam_free_coherent(cl->dev->dev, |
| 2248 | cl->dma.size, cl->dma.vaddr, cl->dma.daddr); |
| 2249 | cl->dma.size = 0; |
| 2250 | cl->dma.vaddr = NULL; |
| 2251 | cl->dma.daddr = 0; |
| 2252 | } |
| 2253 | |
| 2254 | /** |
Tamar Mashiah | 09f8c33 | 2021-06-21 22:37:55 +0300 | [diff] [blame] | 2255 | * mei_cl_dma_alloc_and_map - send client dma map request |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2256 | * |
| 2257 | * @cl: host client |
| 2258 | * @fp: pointer to file structure |
| 2259 | * @buffer_id: id of the mapped buffer |
| 2260 | * @size: size of the buffer |
| 2261 | * |
| 2262 | * Locking: called under "dev->device_lock" lock |
| 2263 | * |
| 2264 | * Return: |
| 2265 | * * -ENODEV |
| 2266 | * * -EINVAL |
| 2267 | * * -EOPNOTSUPP |
| 2268 | * * -EPROTO |
| 2269 | * * -ENOMEM; |
| 2270 | */ |
| 2271 | int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp, |
| 2272 | u8 buffer_id, size_t size) |
| 2273 | { |
| 2274 | struct mei_device *dev; |
| 2275 | struct mei_cl_cb *cb; |
| 2276 | int rets; |
| 2277 | |
| 2278 | if (WARN_ON(!cl || !cl->dev)) |
| 2279 | return -ENODEV; |
| 2280 | |
| 2281 | dev = cl->dev; |
| 2282 | |
| 2283 | if (!dev->hbm_f_cd_supported) { |
| 2284 | cl_dbg(dev, cl, "client dma is not supported\n"); |
| 2285 | return -EOPNOTSUPP; |
| 2286 | } |
| 2287 | |
| 2288 | if (buffer_id == 0) |
| 2289 | return -EINVAL; |
| 2290 | |
Tomas Winkler | ce068bc | 2021-03-18 07:59:59 +0200 | [diff] [blame] | 2291 | if (mei_cl_is_connected(cl)) |
| 2292 | return -EPROTO; |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2293 | |
| 2294 | if (cl->dma_mapped) |
| 2295 | return -EPROTO; |
| 2296 | |
| 2297 | if (mei_cl_dma_map_find(dev, buffer_id)) { |
| 2298 | cl_dbg(dev, cl, "client dma with id %d is already allocated\n", |
| 2299 | cl->dma.buffer_id); |
| 2300 | return -EPROTO; |
| 2301 | } |
| 2302 | |
| 2303 | rets = pm_runtime_get(dev->dev); |
| 2304 | if (rets < 0 && rets != -EINPROGRESS) { |
| 2305 | pm_runtime_put_noidle(dev->dev); |
| 2306 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 2307 | return rets; |
| 2308 | } |
| 2309 | |
| 2310 | rets = mei_cl_dma_alloc(cl, buffer_id, size); |
| 2311 | if (rets) { |
| 2312 | pm_runtime_put_noidle(dev->dev); |
| 2313 | return rets; |
| 2314 | } |
| 2315 | |
| 2316 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp); |
| 2317 | if (!cb) { |
| 2318 | rets = -ENOMEM; |
| 2319 | goto out; |
| 2320 | } |
| 2321 | |
| 2322 | if (mei_hbuf_acquire(dev)) { |
| 2323 | if (mei_hbm_cl_dma_map_req(dev, cl)) { |
| 2324 | rets = -ENODEV; |
| 2325 | goto out; |
| 2326 | } |
| 2327 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
| 2328 | } |
| 2329 | |
| 2330 | mutex_unlock(&dev->device_lock); |
| 2331 | wait_event_timeout(cl->wait, |
Tomas Winkler | ce068bc | 2021-03-18 07:59:59 +0200 | [diff] [blame] | 2332 | cl->dma_mapped || cl->status, |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2333 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 2334 | mutex_lock(&dev->device_lock); |
| 2335 | |
| 2336 | if (!cl->dma_mapped && !cl->status) |
| 2337 | cl->status = -EFAULT; |
| 2338 | |
| 2339 | rets = cl->status; |
| 2340 | |
| 2341 | out: |
| 2342 | if (rets) |
| 2343 | mei_cl_dma_free(cl); |
| 2344 | |
| 2345 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
| 2346 | pm_runtime_mark_last_busy(dev->dev); |
| 2347 | pm_runtime_put_autosuspend(dev->dev); |
| 2348 | |
| 2349 | mei_io_cb_free(cb); |
| 2350 | return rets; |
| 2351 | } |
| 2352 | |
| 2353 | /** |
Tamar Mashiah | 09f8c33 | 2021-06-21 22:37:55 +0300 | [diff] [blame] | 2354 | * mei_cl_dma_unmap - send client dma unmap request |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2355 | * |
| 2356 | * @cl: host client |
| 2357 | * @fp: pointer to file structure |
| 2358 | * |
| 2359 | * Locking: called under "dev->device_lock" lock |
| 2360 | * |
| 2361 | * Return: 0 on such and error otherwise. |
| 2362 | */ |
| 2363 | int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp) |
| 2364 | { |
| 2365 | struct mei_device *dev; |
| 2366 | struct mei_cl_cb *cb; |
| 2367 | int rets; |
| 2368 | |
| 2369 | if (WARN_ON(!cl || !cl->dev)) |
| 2370 | return -ENODEV; |
| 2371 | |
| 2372 | dev = cl->dev; |
| 2373 | |
| 2374 | if (!dev->hbm_f_cd_supported) { |
| 2375 | cl_dbg(dev, cl, "client dma is not supported\n"); |
| 2376 | return -EOPNOTSUPP; |
| 2377 | } |
| 2378 | |
Tomas Winkler | ce068bc | 2021-03-18 07:59:59 +0200 | [diff] [blame] | 2379 | /* do not allow unmap for connected client */ |
| 2380 | if (mei_cl_is_connected(cl)) |
| 2381 | return -EPROTO; |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2382 | |
| 2383 | if (!cl->dma_mapped) |
| 2384 | return -EPROTO; |
| 2385 | |
| 2386 | rets = pm_runtime_get(dev->dev); |
| 2387 | if (rets < 0 && rets != -EINPROGRESS) { |
| 2388 | pm_runtime_put_noidle(dev->dev); |
| 2389 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
| 2390 | return rets; |
| 2391 | } |
| 2392 | |
| 2393 | cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp); |
| 2394 | if (!cb) { |
| 2395 | rets = -ENOMEM; |
| 2396 | goto out; |
| 2397 | } |
| 2398 | |
| 2399 | if (mei_hbuf_acquire(dev)) { |
| 2400 | if (mei_hbm_cl_dma_unmap_req(dev, cl)) { |
| 2401 | rets = -ENODEV; |
| 2402 | goto out; |
| 2403 | } |
| 2404 | list_move_tail(&cb->list, &dev->ctrl_rd_list); |
| 2405 | } |
| 2406 | |
| 2407 | mutex_unlock(&dev->device_lock); |
| 2408 | wait_event_timeout(cl->wait, |
Tomas Winkler | ce068bc | 2021-03-18 07:59:59 +0200 | [diff] [blame] | 2409 | !cl->dma_mapped || cl->status, |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 2410 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
| 2411 | mutex_lock(&dev->device_lock); |
| 2412 | |
| 2413 | if (cl->dma_mapped && !cl->status) |
| 2414 | cl->status = -EFAULT; |
| 2415 | |
| 2416 | rets = cl->status; |
| 2417 | |
| 2418 | if (!rets) |
| 2419 | mei_cl_dma_free(cl); |
| 2420 | out: |
| 2421 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
| 2422 | pm_runtime_mark_last_busy(dev->dev); |
| 2423 | pm_runtime_put_autosuspend(dev->dev); |
| 2424 | |
| 2425 | mei_io_cb_free(cb); |
| 2426 | return rets; |
| 2427 | } |